From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 84A854C886 for ; Fri, 14 Feb 2025 12:11:35 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D919F68BF7C; Fri, 14 Feb 2025 14:11:31 +0200 (EET) Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D9AC268B87F for ; Fri, 14 Feb 2025 14:11:24 +0200 (EET) Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5de4a8b4f86so2835932a12.2 for ; Fri, 14 Feb 2025 04:11:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739535084; x=1740139884; darn=ffmpeg.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=VDAZ4KIysJLe5neykJO0J1c4Lf0tMZC/Z9yUHx1qcj0=; b=bO2tcANYky2tIamR52nAjoZc6b1TJml6hWdkOZPKLCEQaWoNvaain9SrvBDTbiAjSy g+O26tdYPiGwf6iX2E2QqFIFZImOrOR86/P3P6J3fa6QsMjE5BYm3IKM6GBnoAILFZGD GLh0yeBNhvjxmulsN+jFvSrrNKDpwx+RNIEXgHeJqbkyoGOjsxM52P4pztwqujgnNVET A6PwgmJNWD25AbASk7Et90JB3LguDJNgUnOn6P5KKvc/5ajj41zzzqi7ERA0y8hdLqes KrcaVN1Teo6bmqelHjw0vROhEflt1MHH91vejZFwu2s1xhIToHOKaM5AJNDnUW58tElr XiNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739535084; x=1740139884; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VDAZ4KIysJLe5neykJO0J1c4Lf0tMZC/Z9yUHx1qcj0=; b=nLzEncQt6QEHX+AWr/SBm/vGWj1hRGcy4dn4TCGee9OlLA7MnH/YFRnOjZ6hrYHWzZ pAPPg5tZHBln/CSbJycq7JKFD3QZrbFB27TGpDVupUalMnTIcd5BiZ/J+B+NzIWTv4fV LWcgxCyqlaPe4J7apIwfHiW6XHuBqK5KMejj7knAe7Yk5DExMEJs2+PgaFl95I4BQmOf LvZbaFSK642W49xtKQkLyA782P4Sbc4yqVq1B8B/g33idQx3M8SBN+fhjA3jGhyYIyTy H5sBO8uCbHN0ZMrTcFm3fj3ixxu/HxyRVqNjzdIhmUNkwAh2klaiqAq50g6w2/71ZWwB ys5Q== X-Gm-Message-State: AOJu0Yx9DYFuqIaEVd8f4kKvwkLqxyiAxVWGy/c6KgZFBX5p1D9whBAe Ufz03K3oKMTu7NZew1f57DtbnqLmYD7IKxPQUyvz8VXevmsJ6PofzexoFQKlkrA7eNemqMkYwDl NrL1yLUmVezE8RgbdhYR1/purhMODNQ== X-Gm-Gg: ASbGncsDAm0EAyyKUrpBG2dDF41H2bhgyMHhcJSHLZFWy4qrgOaCb0Lb4phd8cM+CYE 27sHapcO/I9rwecFehzlf+CgFL+t+Q8dMzRgQz1+1wTTr5r0UJHWZ5xkSJlwtjBRA4zL2x3VsY2 WsyDF3wtJCFGiP5012ptJSP1kxtatQpxo= X-Google-Smtp-Source: AGHT+IFGdoyMJNLW4y58Qc2kWbuuNwcliTbfvRIqV8TduDc65/INHngKil0YdzSnNsyYUzSH5+pNM08BHwnFLjkf1ho= X-Received: by 2002:a17:907:3f1e:b0:aa6:7220:f12f with SMTP id a640c23a62f3a-ab7f33bb68dmr1120141166b.18.1739535083791; Fri, 14 Feb 2025 04:11:23 -0800 (PST) MIME-Version: 1.0 References: <20250213212208.29414-1-pkoshevoy@gmail.com> In-Reply-To: From: Pavel Koshevoy Date: Fri, 14 Feb 2025 05:11:15 -0700 X-Gm-Features: AWEUYZkawIHObtUErz32F6yD802EgUhXmMWvnBaPjicqov4h3gbIHCn9YfGt9SM Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: Re: [FFmpeg-devel] [PATCH] avformat/mov: (v4) fix get_eia608_packet X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On Thu, Feb 13, 2025, 22:04 Andreas Rheinhardt < andreas.rheinhardt@outlook.com> wrote: > Pavel Koshevoy: > > The problem is reproducible with "Test for Quicktime 608 CC file.mov" > > from https://samples.ffmpeg.org/MPEG2/subcc/ > > > > ffmpeg -i "Test for Quicktime 608 CC file.mov" -map 0 -c copy -y > remuxed.mov > > > > Prior to the fix QuickTime Player playback of remuxed.mov would > > render garbage text for "English CC" subtitles. > > Is remuxing necessary for there being garbage? > > > --- > > libavformat/mov.c | 70 +++++++++++++++++++++++++++++++++++++++-------- > > 1 file changed, 59 insertions(+), 11 deletions(-) > > > > diff --git a/libavformat/mov.c b/libavformat/mov.c > > index 85aef33b19..5a91ef5b8c 100644 > > --- a/libavformat/mov.c > > +++ b/libavformat/mov.c > > @@ -10788,25 +10788,73 @@ static int mov_change_extradata(AVStream *st, > AVPacket *pkt) > > return 0; > > } > > > > -static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size) > > +static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int > src_size) > > { > > - int new_size, ret; > > + /* We can't make assumptions about the structure of the payload, > > + because it may include multiple cdat and cdt2 samples. */ > > + const uint32_t cdat = AV_RB32("cdat"); > > + const uint32_t cdt2 = AV_RB32("cdt2"); > > I don't think that using (non-variable) variables for these improves > clarity (e.g. it means that the definition of the actual values used for > the comparisons below is now further away from its use). Why not simply > use MKBETAG('c','d','a','t') below? > > > + int ret, out_size = 0; > > > > - if (size <= 8) > > + /* a valid payload must have size, 4cc, and at least 1 byte pair: */ > > + if (src_size < 10) > > return AVERROR_INVALIDDATA; > > - new_size = ((size - 8) / 2) * 3; > > - ret = av_new_packet(pkt, new_size); > > + > > + /* avoid an int overflow: */ > > + if ((src_size - 8) / 2 >= INT_MAX / 3) > > + return AVERROR_INVALIDDATA; > > + > > + ret = av_new_packet(pkt, ((src_size - 8) / 2) * 3); > > if (ret < 0) > > return ret; > > > > - avio_skip(pb, 8); > > - for (int j = 0; j < new_size; j += 3) { > > - pkt->data[j] = 0xFC; > > - pkt->data[j+1] = avio_r8(pb); > > - pkt->data[j+2] = avio_r8(pb); > > + /* parse and re-format the c608 payload in one pass. */ > > + while (src_size >= 10) { > > + const uint32_t atom_size = avio_rb32(pb); > > + const uint32_t atom_type = avio_rb32(pb); > > + const uint32_t data_size = atom_size - 8; > > This may wrap around (if atom_size is < 8). If int is 32 bits, then the > data_size > src_size check will catch this, but in case of 64 bit ints > it may not. Relying on (unsigned, defined) integer wraparound should be > avoided unless it is advantageous to use it; in this case, this is just > not true: Just compare atom_size to 10 below. > > > + const uint8_t cc_field = > > + atom_type == cdat ? 1 : > > + atom_type == cdt2 ? 2 : > > + 0; > > + > > + /* account for bytes consumed for atom size and type. */ > > + src_size -= 8; > > + > > + /* make sure the data size stays within the buffer boundaries. > */ > > + if (data_size < 2 || data_size > src_size) { > > + ret = AVERROR_INVALIDDATA; > > + break; > > + } > > + > > + /* make sure the data size is consistent with N byte pairs. */ > > + if (data_size % 2 != 0) { > > We typically try to avoid redundant "!= 0". > > > + ret = AVERROR_INVALIDDATA; > > + break; > > + } > > + > > + if (!cc_field) { > > + /* neither cdat or cdt2 ... skip it */ > > + avio_skip(pb, data_size); > > + src_size -= data_size; > > + continue; > > + } > > + > > + for (int32_t i = 0; i < data_size; i += 2) { > > int32_t? Why signed? (And why use a separate loop counter at all? Simply > decrement data_size by 2 in each iteration. > > > + pkt->data[out_size] = (0x1F << 3) | (1 << 2) | (cc_field - > 1); > > + pkt->data[out_size + 1] = avio_r8(pb); > > + pkt->data[out_size + 2] = avio_r8(pb); > > + out_size += 3; > > + src_size -= 2; > > + } > > } > > > > - return 0; > > + if (src_size > 0) > > + /* skip any remaining unread portion of the input payload */ > > + avio_skip(pb, src_size); > > + > > + av_shrink_packet(pkt, out_size); > > + return ret; > > } > > > > static int mov_finalize_packet(AVFormatContext *s, AVStream *st, > AVIndexEntry *sample, > > Generally, I believe that reading the input into pkt->data[size / 2] > would be advantageous: It would make it simple to check for EOF and I/O > errors (notice that the avio_r* reads above are unchecked) and would > read the data in one go, avoiding all the avio_skip(). > > - Andreas > Then perhaps you would find v2 of the patch more agreeable to your taste, could you review that instead? This function has been corrupting closed captions since 2020. There was a different fix posted in 2023 (mentioned by Devin in the 1st version of this patch), perhaps that should be merged instead, as it also solves the problem. Pavel. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".