From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id B75434D6F5
	for <ffmpegdev@gitmailbox.com>; Sat, 19 Apr 2025 14:27:40 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F0A02687D75;
	Sat, 19 Apr 2025 17:27:34 +0300 (EEST)
Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com
 [209.85.208.45])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7FF2C687A49
 for <ffmpeg-devel@ffmpeg.org>; Sat, 19 Apr 2025 17:27:28 +0300 (EEST)
Received: by mail-ed1-f45.google.com with SMTP id
 4fb4d7f45d1cf-5e61d91a087so4098358a12.0
 for <ffmpeg-devel@ffmpeg.org>; Sat, 19 Apr 2025 07:27:28 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=unified-streaming.com; s=google; t=1745072848; x=1745677648; darn=ffmpeg.org;
 h=message-id:in-reply-to:to:references:date:subject:mime-version
 :content-transfer-encoding:from:from:to:cc:subject:date:message-id
 :reply-to; bh=rixYGr1VtDD00PiR2xnynA+HWEb+pxrh0ARaY6ClkLQ=;
 b=i/i9imj5R+ckMBeinyX3hV7t0OgPYWI9ify6e7sPoQmmCv35ZXlsTXQX3bDSuvyJF0
 MZkr90EheGW2+wQveTdRxqMU3Fn9+PLAxyXbztto892t8y12OZrRhwfeFVSf5vFd0Fyg
 mOlcSN3pIgIbaWVQNBCRYP8X6F9+TNCeb8fOvs7iyQ2Ge9pNXLa1S/0vldNK7hK5Glmt
 KKufX3CQdHsK92LFT//VFAEsyQU5BmoODM3yB3RLie7SD2CqIittBp4fIUI02SkqdmYT
 JI5qxmeI3riRmvQ/wA194KQF7gl8U0/KHFgsapie/d+kiXAyKwB8ovFwaL3F4yeXQBik
 JMhw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1745072848; x=1745677648;
 h=message-id:in-reply-to:to:references:date:subject:mime-version
 :content-transfer-encoding:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=rixYGr1VtDD00PiR2xnynA+HWEb+pxrh0ARaY6ClkLQ=;
 b=Pyds4uzMpc6eVfzmkz2Ijz92HYbZzYocleLaKY9ITHh6zZFkNfhX6ghfjgiyjqPZfO
 RylpJ0Vr9TjaF/gju+D/kKyjLqoFjdKe4N1hm6so7lBVgVy/6K5LKKRNzlbceX/gq6RM
 Q9PjV/lfgIGLlV1FaOFE8ZHk8wRItXGea4nyR+ia0eqhweMz22MGeP0S5/xOhBAV+cWK
 xwoiyCbkCCALyeiJ6QJZBnGT4ZIVLl09b5YFAALBxPibQQF+svlP5GY7VlCqGOl4/e2f
 6U/8FqQ49VR8bht5U5dLf7qgrqQWpVw0/67FVsZ72tMp2CUUh4a2WNdrVFVvE3Nvfi3d
 qPJQ==
X-Gm-Message-State: AOJu0YxSNaE4+KNNt+FEo/T6h0Mtk0JnBs52PjDdqoZpUTHd7gEGWWLn
 1nmjiYHUfi7QZ8PfLdjhzc8B4CMEiSPCOi/miQet6vv3u1SrSqgE+RzsfkRmaTJTNbonnTz2Ihy
 chV3ePFY0h6Jt+Gzs/9U/ybjIH8iyNq8dK/D/HYvlBrZO3vj6sD2MqsofEiZ7TnHRvE8wvV4Y9g
 YuhkEioA6cWpMJQ/mj434auh2FqSDAjjmNgBdjZ0O/SXeyOEFrOcSzSQ==
X-Gm-Gg: ASbGnctRVDkoIEXbqmnInznN2Q4xyqorEW/VLNwjLD3tpVj1EqJlB8145zdsdJfslqV
 XSx601LdcFEKA5dwXYyctN3XqcMwmq2IitB8wDqtYtNeD13XjcaWiGH/JDLEMVYETfy6EqCyDUP
 OjMMy03ebvlOnw5GAz7O7kKAnbNRb5Zl8F9yjN0CGIXg55WpoCAz6UYnSq8nu1Xn81N5NCUX43D
 oruoyE9j3eNReIBTshpEy85tM/s0Zu9KjI2GMoJTc3Fp20PxWfd9Np3iw4k6ri5rPxPClIeXerh
 3/nns+ascqTWLtmp1UcZQrRabmO2/K+HFS6T6o77iKjJTglo7VTPGgG8vwA1iS/72Rxz1gVbnga
 glH+6YZT0VL/3kbgKapg=
X-Google-Smtp-Source: AGHT+IH8731ZCsM7nz+E56iUApgCCPOwP4EQc/DupTNhsBfbBmVzZm75YPvptwO53OLFnaRoy25uxA==
X-Received: by 2002:a05:6402:4346:b0:5e4:92ca:34d0 with SMTP id
 4fb4d7f45d1cf-5f62855b83dmr4945670a12.20.1745072847445; 
 Sat, 19 Apr 2025 07:27:27 -0700 (PDT)
Received: from smtpclient.apple (tensor.andric.com. [87.251.56.140])
 by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-acb6eefd8f1sm268602166b.89.2025.04.19.07.27.26
 for <ffmpeg-devel@ffmpeg.org>
 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
 Sat, 19 Apr 2025 07:27:26 -0700 (PDT)
From: Dimitry Andric <dimitry@unified-streaming.com>
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6.1.10\))
Date: Sat, 19 Apr 2025 16:27:26 +0200
References: <20250403200239.226898-1-dimitry@unified-streaming.com>
 <9ADA5613-0335-455A-B75C-8ADD42D860E6@unified-streaming.com>
To: ffmpeg-devel@ffmpeg.org
In-Reply-To: <9ADA5613-0335-455A-B75C-8ADD42D860E6@unified-streaming.com>
Message-Id: <039BFB64-4F2B-4644-BA12-AE7ECE262393@unified-streaming.com>
X-Mailer: Apple Mail (2.3731.700.6.1.10)
Subject: Re: [FFmpeg-devel] [PATCH] avformat/mov: Fix decoding fragmented
 MP4 with multiple sample entries and empty stsc
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/039BFB64-4F2B-4644-BA12-AE7ECE262393@unified-streaming.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

On 10 Apr 2025, at 11:03, Dimitry Andric <dimitry@unified-streaming.com> wrote:
> 
> On 3 Apr 2025, at 22:02, Dimitry Andric <dimitry@unified-streaming.com> wrote:
>> 
>> When decoding fragmented MP4 files that have an empty stsc box, and
>> instead contain sample description indexes in their tfhd boxes, the mov
>> demuxer does not notify the decoder whenever the current sample
>> description index changes. If the SPS or PPS changed sufficiently, this
>> can lead to unexpected decoding errors.
>> 
>> To fix this, in mov_finalize_packet(), when stsc_data is not available,
>> use get_frag_stream_info_from_pkt() to get at the current fragment
>> stream info, and retrieve the current sample description index from
>> there. Then use that index in a similar manner as the stsc case.
>> 
>> Signed-off-by: Dimitry Andric <dimitry@unified-streaming.com>
>> ---
>> libavformat/mov.c | 50 ++++++++++++++++++++++++++++-------------------
>> 1 file changed, 30 insertions(+), 20 deletions(-)
>> 
>> diff --git a/libavformat/mov.c b/libavformat/mov.c
>> index 452690090c..ead89192f4 100644
>> --- a/libavformat/mov.c
>> +++ b/libavformat/mov.c
>> @@ -10756,25 +10756,29 @@ static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
>>    return 1;
>> }
>> 
>> -static int mov_change_extradata(AVStream *st, AVPacket *pkt)
>> +static int mov_change_extradata(AVStream *st, AVPacket *pkt, int stsd_id)
>> {
>>    MOVStreamContext *sc = st->priv_data;
>>    uint8_t *side, *extradata;
>>    int extradata_size;
>> 
>> -    /* Save the current index. */
>> -    sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
>> +    if (stsd_id > 0 &&
>> +        stsd_id - 1 < sc->stsd_count &&
>> +        stsd_id - 1 != sc->last_stsd_index) {
>> +        /* Save the current index. */
>> +        sc->last_stsd_index = stsd_id - 1;
>> 
>> -    /* Notify the decoder that extradata changed. */
>> -    extradata_size = sc->extradata_size[sc->last_stsd_index];
>> -    extradata = sc->extradata[sc->last_stsd_index];
>> -    if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
>> -        side = av_packet_new_side_data(pkt,
>> -                                       AV_PKT_DATA_NEW_EXTRADATA,
>> -                                       extradata_size);
>> -        if (!side)
>> -            return AVERROR(ENOMEM);
>> -        memcpy(side, extradata, extradata_size);
>> +        /* Notify the decoder that extradata changed. */
>> +        extradata_size = sc->extradata_size[sc->last_stsd_index];
>> +        extradata = sc->extradata[sc->last_stsd_index];
>> +        if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
>> +            side = av_packet_new_side_data(pkt,
>> +                                           AV_PKT_DATA_NEW_EXTRADATA,
>> +                                           extradata_size);
>> +            if (!side)
>> +                return AVERROR(ENOMEM);
>> +            memcpy(side, extradata, extradata_size);
>> +        }
>>    }
>> 
>>    return 0;
>> @@ -10893,13 +10897,10 @@ static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *s
>> 
>>    /* Multiple stsd handling. */
>>    if (sc->stsc_data) {
>> -        if (sc->stsc_data[sc->stsc_index].id > 0 &&
>> -            sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
>> -            sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
>> -            int ret = mov_change_extradata(st, pkt);
>> -            if (ret < 0)
>> -                return ret;
>> -        }
>> +        int stsd_id = sc->stsc_data[sc->stsc_index].id;
>> +        int ret = mov_change_extradata(st, pkt, stsd_id);
>> +        if (ret < 0)
>> +            return ret;
>> 
>>        /* Update the stsc index for the next sample */
>>        sc->stsc_sample++;
>> @@ -10908,6 +10909,15 @@ static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *s
>>            sc->stsc_index++;
>>            sc->stsc_sample = 0;
>>        }
>> +    } else {
>> +        MOVContext *mov = s->priv_data;
>> +        MOVFragmentStreamInfo *frag_stream_info = get_frag_stream_info_from_pkt(&mov->frag_index, pkt, sc->id);
>> +        if (frag_stream_info) {
>> +            int stsd_id = frag_stream_info->stsd_id;
>> +            int ret = mov_change_extradata(st, pkt, stsd_id);
>> +            if (ret < 0)
>> +                return ret;
>> +        }
>>    }
>> 
>>    return 0;
>> -- 
>> 2.43.0
>> 
> 
> Any comments on this patch?

Ping :)

-Dimitry

_______________________________________________
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".