1+ From 07045b8245cfc5822005c716db8a84b710579787 Mon Sep 17 00 :00 :00 2001
2+ From: llyyr <llyyr.public@gmail.com>
3+ Date: Sun, 27 Oct 2024 06 :44 :51 +0530
4+ Subject: [PATCH 2 /2 ] avformat/mov: handle stream position resets
5+
6+ If the stream position has been reset, clear fragment index, the index
7+ entries and the current sample and search for the next root.
8+
9+ Co-Authored-by: vectronic <hello.vectronic@gmail.com>
10+ ---
11+ libavformat/mov.c | 37 +++++++++++++++++++++++++++++++++----
12+ 1 file changed, 33 insertions (+), 4 deletions(-)
13+
14+ diff --git a/libavformat/mov.c b/libavformat/mov.c
15+ index 8c3329b81596..78540bec1d11 100644
16+ --- a/libavformat/mov.c
17+ +++ b/libavformat/mov.c
18+ @@ -10612,15 +10612,15 @@ static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
19+
20+ if (index >= 0 && index < mov->frag_index.nb_items)
21+ target = mov->frag_index.item[index].moof_offset;
22+ - if (avio_seek(s->pb, target, SEEK_SET) != target) {
23+ + if (target >= 0 && avio_seek (s->pb , target, SEEK_SET) != target) {
24+ av_log (mov->fc , AV_LOG_ERROR, " root atom offset 0x%" PRIx64" : partial file\n " , target);
25+ return AVERROR_INVALIDDATA;
26+ }
27+
28+ mov->next_root_atom = 0 ;
29+ - if (index < 0 || index >= mov->frag_index .nb_items )
30+ + if ((index < 0 && target >= 0 ) || index >= mov->frag_index .nb_items )
31+ index = search_frag_moof_offset (&mov->frag_index , target);
32+ - if (index < mov->frag_index .nb_items &&
33+ + if (index >= 0 && index < mov->frag_index .nb_items &&
34+ mov->frag_index .item [index].moof_offset == target) {
35+ if (index + 1 < mov->frag_index .nb_items )
36+ mov->next_root_atom = mov->frag_index .item [index + 1 ].moof_offset ;
37+ @@ -10751 ,9 +10751 ,38 @@ static int mov_read_packet (AVFormatContext *s, AVPacket *pkt)
38+ AVIndexEntry *sample;
39+ AVStream *st = NULL ;
40+ int64_t current_index;
41+ - int ret;
42+ + int ret, i;
43+ mov->fc = s;
44+ retry:
45+ + if (s->pb ->pos == 0 ) {
46+ + // Discard current fragment index
47+ + if (mov->frag_index .allocated_size > 0 ) {
48+ + av_freep (&mov->frag_index .item );
49+ + mov->frag_index .nb_items = 0 ;
50+ + mov->frag_index .allocated_size = 0 ;
51+ + mov->frag_index .current = -1 ;
52+ + mov->frag_index .complete = 0 ;
53+ + }
54+ +
55+ + for (i = 0 ; i < s->nb_streams ; i++) {
56+ + AVStream *avst = s->streams [i];
57+ + FFStream *sti = ffstream (avst);
58+ + MOVStreamContext *msc = avst->priv_data ;
59+ +
60+ + // Clear current sample
61+ + mov_current_sample_set (msc, 0 );
62+ +
63+ + // Discard current index entries
64+ + if (sti->index_entries_allocated_size > 0 ) {
65+ + av_freep (&sti->index_entries );
66+ + sti->index_entries_allocated_size = 0 ;
67+ + sti->nb_index_entries = 0 ;
68+ + }
69+ + }
70+ +
71+ + if ((ret = mov_switch_root (s, -1 , -1 )) < 0 )
72+ + return ret;
73+ + }
74+ sample = mov_find_next_sample (s, &st);
75+ if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom )) {
76+ if (!mov->next_root_atom )
77+ --
78+ 2.47.0
0 commit comments