]> git.sesse.net Git - ffmpeg/blob - libavformat/mov.c
avformat/mov: simplify size code in probing a bit
[ffmpeg] / libavformat / mov.c
1 /*
2  * MOV demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5  *
6  * first version by Francois Revol <revol@free.fr>
7  * seek function by Gael Chardon <gael.dev@4now.net>
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <stdint.h>
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/intfloat.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/time_internal.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/display.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/aes.h"
43 #include "libavutil/aes_ctr.h"
44 #include "libavutil/pixdesc.h"
45 #include "libavutil/sha.h"
46 #include "libavutil/spherical.h"
47 #include "libavutil/stereo3d.h"
48 #include "libavutil/timecode.h"
49 #include "libavutil/dovi_meta.h"
50 #include "libavcodec/ac3tab.h"
51 #include "libavcodec/flac.h"
52 #include "libavcodec/mpegaudiodecheader.h"
53 #include "libavcodec/mlp_parse.h"
54 #include "avformat.h"
55 #include "internal.h"
56 #include "avio_internal.h"
57 #include "riff.h"
58 #include "isom.h"
59 #include "libavcodec/get_bits.h"
60 #include "id3v1.h"
61 #include "mov_chan.h"
62 #include "replaygain.h"
63
64 #if CONFIG_ZLIB
65 #include <zlib.h>
66 #endif
67
68 #include "qtpalette.h"
69
70 /* those functions parse an atom */
71 /* links atom IDs to parse functions */
72 typedef struct MOVParseTableEntry {
73     uint32_t type;
74     int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
75 } MOVParseTableEntry;
76
77 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
78 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
79 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
80                               int count, int duration);
81
82 static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
83                                              unsigned len, const char *key)
84 {
85     char buf[16];
86
87     short current, total = 0;
88     avio_rb16(pb); // unknown
89     current = avio_rb16(pb);
90     if (len >= 6)
91         total = avio_rb16(pb);
92     if (!total)
93         snprintf(buf, sizeof(buf), "%d", current);
94     else
95         snprintf(buf, sizeof(buf), "%d/%d", current, total);
96     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
97     av_dict_set(&c->fc->metadata, key, buf, 0);
98
99     return 0;
100 }
101
102 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
103                                             unsigned len, const char *key)
104 {
105     /* bypass padding bytes */
106     avio_r8(pb);
107     avio_r8(pb);
108     avio_r8(pb);
109
110     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
111     av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
112
113     return 0;
114 }
115
116 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
117                                         unsigned len, const char *key)
118 {
119     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
120     av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
121
122     return 0;
123 }
124
125 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
126                              unsigned len, const char *key)
127 {
128     short genre;
129
130     avio_r8(pb); // unknown
131
132     genre = avio_r8(pb);
133     if (genre < 1 || genre > ID3v1_GENRE_MAX)
134         return 0;
135     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
136     av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
137
138     return 0;
139 }
140
141 static const uint32_t mac_to_unicode[128] = {
142     0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
143     0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
144     0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
145     0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
146     0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
147     0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
148     0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
149     0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
150     0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
151     0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
152     0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
153     0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
154     0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
155     0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
156     0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
157     0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
158 };
159
160 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
161                                char *dst, int dstlen)
162 {
163     char *p = dst;
164     char *end = dst+dstlen-1;
165     int i;
166
167     for (i = 0; i < len; i++) {
168         uint8_t t, c = avio_r8(pb);
169
170         if (p >= end)
171             continue;
172
173         if (c < 0x80)
174             *p++ = c;
175         else if (p < end)
176             PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
177     }
178     *p = 0;
179     return p - dst;
180 }
181
182 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
183 {
184     AVStream *st;
185     MOVStreamContext *sc;
186     enum AVCodecID id;
187     int ret;
188
189     switch (type) {
190     case 0xd:  id = AV_CODEC_ID_MJPEG; break;
191     case 0xe:  id = AV_CODEC_ID_PNG;   break;
192     case 0x1b: id = AV_CODEC_ID_BMP;   break;
193     default:
194         av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
195         avio_skip(pb, len);
196         return 0;
197     }
198
199     st = avformat_new_stream(c->fc, NULL);
200     if (!st)
201         return AVERROR(ENOMEM);
202     sc = av_mallocz(sizeof(*sc));
203     if (!sc)
204         return AVERROR(ENOMEM);
205     st->priv_data = sc;
206
207     ret = av_get_packet(pb, &st->attached_pic, len);
208     if (ret < 0)
209         return ret;
210
211     if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
212         if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
213             id = AV_CODEC_ID_PNG;
214         } else {
215             id = AV_CODEC_ID_MJPEG;
216         }
217     }
218
219     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
220
221     st->attached_pic.stream_index = st->index;
222     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
223
224     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
225     st->codecpar->codec_id   = id;
226
227     return 0;
228 }
229
230 // 3GPP TS 26.244
231 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
232 {
233     char language[4] = { 0 };
234     char buf[200], place[100];
235     uint16_t langcode = 0;
236     double longitude, latitude, altitude;
237     const char *key = "location";
238
239     if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
240         av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
241         return AVERROR_INVALIDDATA;
242     }
243
244     avio_skip(pb, 4); // version+flags
245     langcode = avio_rb16(pb);
246     ff_mov_lang_to_iso639(langcode, language);
247     len -= 6;
248
249     len -= avio_get_str(pb, len, place, sizeof(place));
250     if (len < 1) {
251         av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
252         return AVERROR_INVALIDDATA;
253     }
254     avio_skip(pb, 1); // role
255     len -= 1;
256
257     if (len < 12) {
258         av_log(c->fc, AV_LOG_ERROR,
259                "loci too short (%u bytes left, need at least %d)\n", len, 12);
260         return AVERROR_INVALIDDATA;
261     }
262     longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
263     latitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
264     altitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
265
266     // Try to output in the same format as the ?xyz field
267     snprintf(buf, sizeof(buf), "%+08.4f%+09.4f",  latitude, longitude);
268     if (altitude)
269         av_strlcatf(buf, sizeof(buf), "%+f", altitude);
270     av_strlcatf(buf, sizeof(buf), "/%s", place);
271
272     if (*language && strcmp(language, "und")) {
273         char key2[16];
274         snprintf(key2, sizeof(key2), "%s-%s", key, language);
275         av_dict_set(&c->fc->metadata, key2, buf, 0);
276     }
277     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
278     return av_dict_set(&c->fc->metadata, key, buf, 0);
279 }
280
281 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
282 {
283     int i, n_hmmt;
284
285     if (len < 2)
286         return 0;
287     if (c->ignore_chapters)
288         return 0;
289
290     n_hmmt = avio_rb32(pb);
291     for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
292         int moment_time = avio_rb32(pb);
293         avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
294     }
295     return 0;
296 }
297
298 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
299 {
300     char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
301     char key2[32], language[4] = {0};
302     char *str = NULL;
303     const char *key = NULL;
304     uint16_t langcode = 0;
305     uint32_t data_type = 0, str_size, str_size_alloc;
306     int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
307     int raw = 0;
308     int num = 0;
309
310     switch (atom.type) {
311     case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
312     case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
313     case MKTAG( 'X','M','P','_'):
314         if (c->export_xmp) { key = "xmp"; raw = 1; } break;
315     case MKTAG( 'a','A','R','T'): key = "album_artist";    break;
316     case MKTAG( 'a','k','I','D'): key = "account_type";
317         parse = mov_metadata_int8_no_padding; break;
318     case MKTAG( 'a','p','I','D'): key = "account_id"; break;
319     case MKTAG( 'c','a','t','g'): key = "category"; break;
320     case MKTAG( 'c','p','i','l'): key = "compilation";
321         parse = mov_metadata_int8_no_padding; break;
322     case MKTAG( 'c','p','r','t'): key = "copyright"; break;
323     case MKTAG( 'd','e','s','c'): key = "description"; break;
324     case MKTAG( 'd','i','s','k'): key = "disc";
325         parse = mov_metadata_track_or_disc_number; break;
326     case MKTAG( 'e','g','i','d'): key = "episode_uid";
327         parse = mov_metadata_int8_no_padding; break;
328     case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
329     case MKTAG( 'g','n','r','e'): key = "genre";
330         parse = mov_metadata_gnre; break;
331     case MKTAG( 'h','d','v','d'): key = "hd_video";
332         parse = mov_metadata_int8_no_padding; break;
333     case MKTAG( 'H','M','M','T'):
334         return mov_metadata_hmmt(c, pb, atom.size);
335     case MKTAG( 'k','e','y','w'): key = "keywords";  break;
336     case MKTAG( 'l','d','e','s'): key = "synopsis";  break;
337     case MKTAG( 'l','o','c','i'):
338         return mov_metadata_loci(c, pb, atom.size);
339     case MKTAG( 'm','a','n','u'): key = "make"; break;
340     case MKTAG( 'm','o','d','l'): key = "model"; break;
341     case MKTAG( 'p','c','s','t'): key = "podcast";
342         parse = mov_metadata_int8_no_padding; break;
343     case MKTAG( 'p','g','a','p'): key = "gapless_playback";
344         parse = mov_metadata_int8_no_padding; break;
345     case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
346     case MKTAG( 'r','t','n','g'): key = "rating";
347         parse = mov_metadata_int8_no_padding; break;
348     case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
349     case MKTAG( 's','o','a','l'): key = "sort_album";   break;
350     case MKTAG( 's','o','a','r'): key = "sort_artist";  break;
351     case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
352     case MKTAG( 's','o','n','m'): key = "sort_name";    break;
353     case MKTAG( 's','o','s','n'): key = "sort_show";    break;
354     case MKTAG( 's','t','i','k'): key = "media_type";
355         parse = mov_metadata_int8_no_padding; break;
356     case MKTAG( 't','r','k','n'): key = "track";
357         parse = mov_metadata_track_or_disc_number; break;
358     case MKTAG( 't','v','e','n'): key = "episode_id"; break;
359     case MKTAG( 't','v','e','s'): key = "episode_sort";
360         parse = mov_metadata_int8_bypass_padding; break;
361     case MKTAG( 't','v','n','n'): key = "network";   break;
362     case MKTAG( 't','v','s','h'): key = "show";      break;
363     case MKTAG( 't','v','s','n'): key = "season_number";
364         parse = mov_metadata_int8_bypass_padding; break;
365     case MKTAG(0xa9,'A','R','T'): key = "artist";    break;
366     case MKTAG(0xa9,'P','R','D'): key = "producer";  break;
367     case MKTAG(0xa9,'a','l','b'): key = "album";     break;
368     case MKTAG(0xa9,'a','u','t'): key = "artist";    break;
369     case MKTAG(0xa9,'c','h','p'): key = "chapter";   break;
370     case MKTAG(0xa9,'c','m','t'): key = "comment";   break;
371     case MKTAG(0xa9,'c','o','m'): key = "composer";  break;
372     case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
373     case MKTAG(0xa9,'d','a','y'): key = "date";      break;
374     case MKTAG(0xa9,'d','i','r'): key = "director";  break;
375     case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
376     case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
377     case MKTAG(0xa9,'e','n','c'): key = "encoder";   break;
378     case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
379     case MKTAG(0xa9,'g','e','n'): key = "genre";     break;
380     case MKTAG(0xa9,'g','r','p'): key = "grouping";  break;
381     case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
382     case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
383     case MKTAG(0xa9,'l','y','r'): key = "lyrics";    break;
384     case MKTAG(0xa9,'m','a','k'): key = "make";      break;
385     case MKTAG(0xa9,'m','o','d'): key = "model";     break;
386     case MKTAG(0xa9,'n','a','m'): key = "title";     break;
387     case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
388     case MKTAG(0xa9,'p','r','d'): key = "producer";  break;
389     case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
390     case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
391     case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
392     case MKTAG(0xa9,'s','t','3'): key = "subtitle";  break;
393     case MKTAG(0xa9,'s','w','r'): key = "encoder";   break;
394     case MKTAG(0xa9,'t','o','o'): key = "encoder";   break;
395     case MKTAG(0xa9,'t','r','k'): key = "track";     break;
396     case MKTAG(0xa9,'u','r','l'): key = "URL";       break;
397     case MKTAG(0xa9,'w','r','n'): key = "warning";   break;
398     case MKTAG(0xa9,'w','r','t'): key = "composer";  break;
399     case MKTAG(0xa9,'x','y','z'): key = "location";  break;
400     }
401 retry:
402     if (c->itunes_metadata && atom.size > 8) {
403         int data_size = avio_rb32(pb);
404         int tag = avio_rl32(pb);
405         if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
406             data_type = avio_rb32(pb); // type
407             avio_rb32(pb); // unknown
408             str_size = data_size - 16;
409             atom.size -= 16;
410
411             if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
412                 int ret = mov_read_covr(c, pb, data_type, str_size);
413                 if (ret < 0) {
414                     av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
415                     return ret;
416                 }
417                 atom.size -= str_size;
418                 if (atom.size > 8)
419                     goto retry;
420                 return ret;
421             } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
422                 uint32_t index = AV_RB32(&atom.type);
423                 if (index < c->meta_keys_count && index > 0) {
424                     key = c->meta_keys[index];
425                 } else {
426                     av_log(c->fc, AV_LOG_WARNING,
427                            "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
428                            index, c->meta_keys_count);
429                 }
430             }
431         } else return 0;
432     } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
433         str_size = avio_rb16(pb); // string length
434         if (str_size > atom.size) {
435             raw = 1;
436             avio_seek(pb, -2, SEEK_CUR);
437             av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
438             goto retry;
439         }
440         langcode = avio_rb16(pb);
441         ff_mov_lang_to_iso639(langcode, language);
442         atom.size -= 4;
443     } else
444         str_size = atom.size;
445
446     if (c->export_all && !key) {
447         key = av_fourcc_make_string(tmp_key, atom.type);
448     }
449
450     if (!key)
451         return 0;
452     if (atom.size < 0 || str_size >= INT_MAX/2)
453         return AVERROR_INVALIDDATA;
454
455     // Allocates enough space if data_type is a int32 or float32 number, otherwise
456     // worst-case requirement for output string in case of utf8 coded input
457     num = (data_type >= 21 && data_type <= 23);
458     str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
459     str = av_mallocz(str_size_alloc);
460     if (!str)
461         return AVERROR(ENOMEM);
462
463     if (parse)
464         parse(c, pb, str_size, key);
465     else {
466         if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
467             mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
468         } else if (data_type == 21) { // BE signed integer, variable size
469             int val = 0;
470             if (str_size == 1)
471                 val = (int8_t)avio_r8(pb);
472             else if (str_size == 2)
473                 val = (int16_t)avio_rb16(pb);
474             else if (str_size == 3)
475                 val = ((int32_t)(avio_rb24(pb)<<8))>>8;
476             else if (str_size == 4)
477                 val = (int32_t)avio_rb32(pb);
478             if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
479                 av_log(c->fc, AV_LOG_ERROR,
480                        "Failed to store the number (%d) in string.\n", val);
481                 av_free(str);
482                 return AVERROR_INVALIDDATA;
483             }
484         } else if (data_type == 22) { // BE unsigned integer, variable size
485             unsigned int val = 0;
486             if (str_size == 1)
487                 val = avio_r8(pb);
488             else if (str_size == 2)
489                 val = avio_rb16(pb);
490             else if (str_size == 3)
491                 val = avio_rb24(pb);
492             else if (str_size == 4)
493                 val = avio_rb32(pb);
494             if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
495                 av_log(c->fc, AV_LOG_ERROR,
496                        "Failed to store the number (%u) in string.\n", val);
497                 av_free(str);
498                 return AVERROR_INVALIDDATA;
499             }
500         } else if (data_type == 23 && str_size >= 4) {  // BE float32
501             float val = av_int2float(avio_rb32(pb));
502             if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
503                 av_log(c->fc, AV_LOG_ERROR,
504                        "Failed to store the float32 number (%f) in string.\n", val);
505                 av_free(str);
506                 return AVERROR_INVALIDDATA;
507             }
508         } else {
509             int ret = ffio_read_size(pb, str, str_size);
510             if (ret < 0) {
511                 av_free(str);
512                 return ret;
513             }
514             str[str_size] = 0;
515         }
516         c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
517         av_dict_set(&c->fc->metadata, key, str, 0);
518         if (*language && strcmp(language, "und")) {
519             snprintf(key2, sizeof(key2), "%s-%s", key, language);
520             av_dict_set(&c->fc->metadata, key2, str, 0);
521         }
522         if (!strcmp(key, "encoder")) {
523             int major, minor, micro;
524             if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
525                 c->handbrake_version = 1000000*major + 1000*minor + micro;
526             }
527         }
528     }
529
530     av_freep(&str);
531     return 0;
532 }
533
534 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
535 {
536     int64_t start;
537     int i, nb_chapters, str_len, version;
538     char str[256+1];
539     int ret;
540
541     if (c->ignore_chapters)
542         return 0;
543
544     if ((atom.size -= 5) < 0)
545         return 0;
546
547     version = avio_r8(pb);
548     avio_rb24(pb);
549     if (version)
550         avio_rb32(pb); // ???
551     nb_chapters = avio_r8(pb);
552
553     for (i = 0; i < nb_chapters; i++) {
554         if (atom.size < 9)
555             return 0;
556
557         start = avio_rb64(pb);
558         str_len = avio_r8(pb);
559
560         if ((atom.size -= 9+str_len) < 0)
561             return 0;
562
563         ret = ffio_read_size(pb, str, str_len);
564         if (ret < 0)
565             return ret;
566         str[str_len] = 0;
567         avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
568     }
569     return 0;
570 }
571
572 #define MIN_DATA_ENTRY_BOX_SIZE 12
573 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
574 {
575     AVStream *st;
576     MOVStreamContext *sc;
577     int entries, i, j;
578
579     if (c->fc->nb_streams < 1)
580         return 0;
581     st = c->fc->streams[c->fc->nb_streams-1];
582     sc = st->priv_data;
583
584     avio_rb32(pb); // version + flags
585     entries = avio_rb32(pb);
586     if (!entries ||
587         entries >  (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
588         entries >= UINT_MAX / sizeof(*sc->drefs))
589         return AVERROR_INVALIDDATA;
590
591     for (i = 0; i < sc->drefs_count; i++) {
592         MOVDref *dref = &sc->drefs[i];
593         av_freep(&dref->path);
594         av_freep(&dref->dir);
595     }
596     av_free(sc->drefs);
597     sc->drefs_count = 0;
598     sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
599     if (!sc->drefs)
600         return AVERROR(ENOMEM);
601     sc->drefs_count = entries;
602
603     for (i = 0; i < entries; i++) {
604         MOVDref *dref = &sc->drefs[i];
605         uint32_t size = avio_rb32(pb);
606         int64_t next = avio_tell(pb) + size - 4;
607
608         if (size < 12)
609             return AVERROR_INVALIDDATA;
610
611         dref->type = avio_rl32(pb);
612         avio_rb32(pb); // version + flags
613
614         if (dref->type == MKTAG('a','l','i','s') && size > 150) {
615             /* macintosh alias record */
616             uint16_t volume_len, len;
617             int16_t type;
618             int ret;
619
620             avio_skip(pb, 10);
621
622             volume_len = avio_r8(pb);
623             volume_len = FFMIN(volume_len, 27);
624             ret = ffio_read_size(pb, dref->volume, 27);
625             if (ret < 0)
626                 return ret;
627             dref->volume[volume_len] = 0;
628             av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
629
630             avio_skip(pb, 12);
631
632             len = avio_r8(pb);
633             len = FFMIN(len, 63);
634             ret = ffio_read_size(pb, dref->filename, 63);
635             if (ret < 0)
636                 return ret;
637             dref->filename[len] = 0;
638             av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
639
640             avio_skip(pb, 16);
641
642             /* read next level up_from_alias/down_to_target */
643             dref->nlvl_from = avio_rb16(pb);
644             dref->nlvl_to   = avio_rb16(pb);
645             av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
646                    dref->nlvl_from, dref->nlvl_to);
647
648             avio_skip(pb, 16);
649
650             for (type = 0; type != -1 && avio_tell(pb) < next; ) {
651                 if (avio_feof(pb))
652                     return AVERROR_EOF;
653                 type = avio_rb16(pb);
654                 len = avio_rb16(pb);
655                 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
656                 if (len&1)
657                     len += 1;
658                 if (type == 2) { // absolute path
659                     av_free(dref->path);
660                     dref->path = av_mallocz(len+1);
661                     if (!dref->path)
662                         return AVERROR(ENOMEM);
663
664                     ret = ffio_read_size(pb, dref->path, len);
665                     if (ret < 0) {
666                         av_freep(&dref->path);
667                         return ret;
668                     }
669                     if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
670                         len -= volume_len;
671                         memmove(dref->path, dref->path+volume_len, len);
672                         dref->path[len] = 0;
673                     }
674                     // trim string of any ending zeros
675                     for (j = len - 1; j >= 0; j--) {
676                         if (dref->path[j] == 0)
677                             len--;
678                         else
679                             break;
680                     }
681                     for (j = 0; j < len; j++)
682                         if (dref->path[j] == ':' || dref->path[j] == 0)
683                             dref->path[j] = '/';
684                     av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
685                 } else if (type == 0) { // directory name
686                     av_free(dref->dir);
687                     dref->dir = av_malloc(len+1);
688                     if (!dref->dir)
689                         return AVERROR(ENOMEM);
690
691                     ret = ffio_read_size(pb, dref->dir, len);
692                     if (ret < 0) {
693                         av_freep(&dref->dir);
694                         return ret;
695                     }
696                     dref->dir[len] = 0;
697                     for (j = 0; j < len; j++)
698                         if (dref->dir[j] == ':')
699                             dref->dir[j] = '/';
700                     av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
701                 } else
702                     avio_skip(pb, len);
703             }
704         } else {
705             av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
706                    dref->type, size);
707             entries--;
708             i--;
709         }
710         avio_seek(pb, next, SEEK_SET);
711     }
712     return 0;
713 }
714
715 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
716 {
717     AVStream *st;
718     uint32_t type;
719     uint32_t ctype;
720     int64_t title_size;
721     char *title_str;
722     int ret;
723
724     avio_r8(pb); /* version */
725     avio_rb24(pb); /* flags */
726
727     /* component type */
728     ctype = avio_rl32(pb);
729     type = avio_rl32(pb); /* component subtype */
730
731     av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
732     av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
733
734     if (c->trak_index < 0) {  // meta not inside a trak
735         if (type == MKTAG('m','d','t','a')) {
736             c->found_hdlr_mdta = 1;
737         }
738         return 0;
739     }
740
741     st = c->fc->streams[c->fc->nb_streams-1];
742
743     if     (type == MKTAG('v','i','d','e'))
744         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
745     else if (type == MKTAG('s','o','u','n'))
746         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
747     else if (type == MKTAG('m','1','a',' '))
748         st->codecpar->codec_id = AV_CODEC_ID_MP2;
749     else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
750         st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
751
752     avio_rb32(pb); /* component  manufacture */
753     avio_rb32(pb); /* component flags */
754     avio_rb32(pb); /* component flags mask */
755
756     title_size = atom.size - 24;
757     if (title_size > 0) {
758         if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
759             return AVERROR_INVALIDDATA;
760         title_str = av_malloc(title_size + 1); /* Add null terminator */
761         if (!title_str)
762             return AVERROR(ENOMEM);
763
764         ret = ffio_read_size(pb, title_str, title_size);
765         if (ret < 0) {
766             av_freep(&title_str);
767             return ret;
768         }
769         title_str[title_size] = 0;
770         if (title_str[0]) {
771             int off = (!c->isom && title_str[0] == title_size - 1);
772             // flag added so as to not set stream handler name if already set from mdia->hdlr
773             av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
774         }
775         av_freep(&title_str);
776     }
777
778     return 0;
779 }
780
781 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
782 {
783     return ff_mov_read_esds(c->fc, pb);
784 }
785
786 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
787 {
788     AVStream *st;
789     enum AVAudioServiceType *ast;
790     int ac3info, acmod, lfeon, bsmod;
791
792     if (c->fc->nb_streams < 1)
793         return 0;
794     st = c->fc->streams[c->fc->nb_streams-1];
795
796     ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
797                                                             sizeof(*ast));
798     if (!ast)
799         return AVERROR(ENOMEM);
800
801     ac3info = avio_rb24(pb);
802     bsmod = (ac3info >> 14) & 0x7;
803     acmod = (ac3info >> 11) & 0x7;
804     lfeon = (ac3info >> 10) & 0x1;
805     st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
806     st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
807     if (lfeon)
808         st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
809     *ast = bsmod;
810     if (st->codecpar->channels > 1 && bsmod == 0x7)
811         *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
812
813 #if FF_API_LAVF_AVCTX
814     FF_DISABLE_DEPRECATION_WARNINGS
815     st->codec->audio_service_type = *ast;
816     FF_ENABLE_DEPRECATION_WARNINGS
817 #endif
818
819     return 0;
820 }
821
822 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
823 {
824     AVStream *st;
825     enum AVAudioServiceType *ast;
826     int eac3info, acmod, lfeon, bsmod;
827
828     if (c->fc->nb_streams < 1)
829         return 0;
830     st = c->fc->streams[c->fc->nb_streams-1];
831
832     ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
833                                                             sizeof(*ast));
834     if (!ast)
835         return AVERROR(ENOMEM);
836
837     /* No need to parse fields for additional independent substreams and its
838      * associated dependent substreams since libavcodec's E-AC-3 decoder
839      * does not support them yet. */
840     avio_rb16(pb); /* data_rate and num_ind_sub */
841     eac3info = avio_rb24(pb);
842     bsmod = (eac3info >> 12) & 0x1f;
843     acmod = (eac3info >>  9) & 0x7;
844     lfeon = (eac3info >>  8) & 0x1;
845     st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
846     if (lfeon)
847         st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
848     st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
849     *ast = bsmod;
850     if (st->codecpar->channels > 1 && bsmod == 0x7)
851         *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
852
853 #if FF_API_LAVF_AVCTX
854     FF_DISABLE_DEPRECATION_WARNINGS
855     st->codec->audio_service_type = *ast;
856     FF_ENABLE_DEPRECATION_WARNINGS
857 #endif
858
859     return 0;
860 }
861
862 static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
863 {
864 #define DDTS_SIZE 20
865     uint8_t buf[DDTS_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
866     AVStream *st = NULL;
867     uint32_t frame_duration_code = 0;
868     uint32_t channel_layout_code = 0;
869     GetBitContext gb;
870     int ret;
871
872     if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
873         return ret;
874
875     init_get_bits(&gb, buf, 8 * DDTS_SIZE);
876
877     if (c->fc->nb_streams < 1) {
878         return 0;
879     }
880     st = c->fc->streams[c->fc->nb_streams-1];
881
882     st->codecpar->sample_rate = get_bits_long(&gb, 32);
883     if (st->codecpar->sample_rate <= 0) {
884         av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
885         return AVERROR_INVALIDDATA;
886     }
887     skip_bits_long(&gb, 32); /* max bitrate */
888     st->codecpar->bit_rate = get_bits_long(&gb, 32);
889     st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
890     frame_duration_code = get_bits(&gb, 2);
891     skip_bits(&gb, 30); /* various fields */
892     channel_layout_code = get_bits(&gb, 16);
893
894     st->codecpar->frame_size =
895             (frame_duration_code == 0) ? 512 :
896             (frame_duration_code == 1) ? 1024 :
897             (frame_duration_code == 2) ? 2048 :
898             (frame_duration_code == 3) ? 4096 : 0;
899
900     if (channel_layout_code > 0xff) {
901         av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
902     }
903     st->codecpar->channel_layout =
904             ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
905             ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
906             ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
907             ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
908             ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
909             ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
910
911     st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
912
913     return 0;
914 }
915
916 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
917 {
918     AVStream *st;
919
920     if (c->fc->nb_streams < 1)
921         return 0;
922     st = c->fc->streams[c->fc->nb_streams-1];
923
924     if (atom.size < 16)
925         return 0;
926
927     /* skip version and flags */
928     avio_skip(pb, 4);
929
930     ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
931
932     return 0;
933 }
934
935 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
936 {
937     AVStream *st;
938     int ret;
939
940     if (c->fc->nb_streams < 1)
941         return 0;
942     st = c->fc->streams[c->fc->nb_streams-1];
943
944     if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
945         av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
946
947     return ret;
948 }
949
950 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
951 {
952     const int num = avio_rb32(pb);
953     const int den = avio_rb32(pb);
954     AVStream *st;
955
956     if (c->fc->nb_streams < 1)
957         return 0;
958     st = c->fc->streams[c->fc->nb_streams-1];
959
960     if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
961         (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
962         av_log(c->fc, AV_LOG_WARNING,
963                "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
964                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
965                num, den);
966     } else if (den != 0) {
967         av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
968                   num, den, 32767);
969     }
970     return 0;
971 }
972
973 /* this atom contains actual media data */
974 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
975 {
976     if (atom.size == 0) /* wrong one (MP4) */
977         return 0;
978     c->found_mdat=1;
979     return 0; /* now go for moov */
980 }
981
982 #define DRM_BLOB_SIZE 56
983
984 static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
985 {
986     uint8_t intermediate_key[20];
987     uint8_t intermediate_iv[20];
988     uint8_t input[64];
989     uint8_t output[64];
990     uint8_t file_checksum[20];
991     uint8_t calculated_checksum[20];
992     struct AVSHA *sha;
993     int i;
994     int ret = 0;
995     uint8_t *activation_bytes = c->activation_bytes;
996     uint8_t *fixed_key = c->audible_fixed_key;
997
998     c->aax_mode = 1;
999
1000     sha = av_sha_alloc();
1001     if (!sha)
1002         return AVERROR(ENOMEM);
1003     av_free(c->aes_decrypt);
1004     c->aes_decrypt = av_aes_alloc();
1005     if (!c->aes_decrypt) {
1006         ret = AVERROR(ENOMEM);
1007         goto fail;
1008     }
1009
1010     /* drm blob processing */
1011     avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1012     avio_read(pb, input, DRM_BLOB_SIZE);
1013     avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1014     avio_read(pb, file_checksum, 20);
1015
1016     av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
1017     for (i = 0; i < 20; i++)
1018         av_log(c->fc, AV_LOG_INFO, "%02x", file_checksum[i]);
1019     av_log(c->fc, AV_LOG_INFO, "\n");
1020
1021     /* verify activation data */
1022     if (!activation_bytes) {
1023         av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1024         ret = 0;  /* allow ffprobe to continue working on .aax files */
1025         goto fail;
1026     }
1027     if (c->activation_bytes_size != 4) {
1028         av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1029         ret = AVERROR(EINVAL);
1030         goto fail;
1031     }
1032
1033     /* verify fixed key */
1034     if (c->audible_fixed_key_size != 16) {
1035         av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1036         ret = AVERROR(EINVAL);
1037         goto fail;
1038     }
1039
1040     /* AAX (and AAX+) key derivation */
1041     av_sha_init(sha, 160);
1042     av_sha_update(sha, fixed_key, 16);
1043     av_sha_update(sha, activation_bytes, 4);
1044     av_sha_final(sha, intermediate_key);
1045     av_sha_init(sha, 160);
1046     av_sha_update(sha, fixed_key, 16);
1047     av_sha_update(sha, intermediate_key, 20);
1048     av_sha_update(sha, activation_bytes, 4);
1049     av_sha_final(sha, intermediate_iv);
1050     av_sha_init(sha, 160);
1051     av_sha_update(sha, intermediate_key, 16);
1052     av_sha_update(sha, intermediate_iv, 16);
1053     av_sha_final(sha, calculated_checksum);
1054     if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1055         av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1056         ret = AVERROR_INVALIDDATA;
1057         goto fail;
1058     }
1059     av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1060     av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1061     for (i = 0; i < 4; i++) {
1062         // file data (in output) is stored in big-endian mode
1063         if (activation_bytes[i] != output[3 - i]) { // critical error
1064             av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1065             ret = AVERROR_INVALIDDATA;
1066             goto fail;
1067         }
1068     }
1069     memcpy(c->file_key, output + 8, 16);
1070     memcpy(input, output + 26, 16);
1071     av_sha_init(sha, 160);
1072     av_sha_update(sha, input, 16);
1073     av_sha_update(sha, c->file_key, 16);
1074     av_sha_update(sha, fixed_key, 16);
1075     av_sha_final(sha, c->file_iv);
1076
1077 fail:
1078     av_free(sha);
1079
1080     return ret;
1081 }
1082
1083 static int mov_aaxc_crypto(MOVContext *c)
1084 {
1085     if (c->audible_key_size != 16) {
1086         av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
1087         return AVERROR(EINVAL);
1088     }
1089
1090     if (c->audible_iv_size != 16) {
1091         av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
1092         return AVERROR(EINVAL);
1093     }
1094
1095     c->aes_decrypt = av_aes_alloc();
1096     if (!c->aes_decrypt) {
1097         return AVERROR(ENOMEM);
1098     }
1099
1100     memcpy(c->file_key, c->audible_key, 16);
1101     memcpy(c->file_iv, c->audible_iv, 16);
1102     c->aax_mode = 1;
1103
1104     return 0;
1105 }
1106
1107 // Audible AAX (and AAX+) bytestream decryption
1108 static int aax_filter(uint8_t *input, int size, MOVContext *c)
1109 {
1110     int blocks = 0;
1111     unsigned char iv[16];
1112
1113     memcpy(iv, c->file_iv, 16); // iv is overwritten
1114     blocks = size >> 4; // trailing bytes are not encrypted!
1115     av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1116     av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1117
1118     return 0;
1119 }
1120
1121 /* read major brand, minor version and compatible brands and store them as metadata */
1122 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1123 {
1124     uint32_t minor_ver;
1125     int comp_brand_size;
1126     char* comp_brands_str;
1127     uint8_t type[5] = {0};
1128     int ret = ffio_read_size(pb, type, 4);
1129     if (ret < 0)
1130         return ret;
1131
1132     if (strcmp(type, "qt  "))
1133         c->isom = 1;
1134     av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1135     av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1136     minor_ver = avio_rb32(pb); /* minor version */
1137     av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1138
1139     comp_brand_size = atom.size - 8;
1140     if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
1141         return AVERROR_INVALIDDATA;
1142     comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1143     if (!comp_brands_str)
1144         return AVERROR(ENOMEM);
1145
1146     ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1147     if (ret < 0) {
1148         av_freep(&comp_brands_str);
1149         return ret;
1150     }
1151     comp_brands_str[comp_brand_size] = 0;
1152     av_dict_set(&c->fc->metadata, "compatible_brands",
1153                 comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
1154
1155     // Logic for handling Audible's .aaxc files
1156     if (!strcmp(type, "aaxc")) {
1157         mov_aaxc_crypto(c);
1158     }
1159
1160     return 0;
1161 }
1162
1163 /* this atom should contain all header atoms */
1164 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1165 {
1166     int ret;
1167
1168     if (c->found_moov) {
1169         av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1170         avio_skip(pb, atom.size);
1171         return 0;
1172     }
1173
1174     if ((ret = mov_read_default(c, pb, atom)) < 0)
1175         return ret;
1176     /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1177     /* so we don't parse the whole file if over a network */
1178     c->found_moov=1;
1179     return 0; /* now go for mdat */
1180 }
1181
1182 static MOVFragmentStreamInfo * get_frag_stream_info(
1183     MOVFragmentIndex *frag_index,
1184     int index,
1185     int id)
1186 {
1187     int i;
1188     MOVFragmentIndexItem * item;
1189
1190     if (index < 0 || index >= frag_index->nb_items)
1191         return NULL;
1192     item = &frag_index->item[index];
1193     for (i = 0; i < item->nb_stream_info; i++)
1194         if (item->stream_info[i].id == id)
1195             return &item->stream_info[i];
1196
1197     // This shouldn't happen
1198     return NULL;
1199 }
1200
1201 static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
1202 {
1203     int i;
1204     MOVFragmentIndexItem * item;
1205
1206     if (frag_index->current < 0 ||
1207         frag_index->current >= frag_index->nb_items)
1208         return;
1209
1210     item = &frag_index->item[frag_index->current];
1211     for (i = 0; i < item->nb_stream_info; i++)
1212         if (item->stream_info[i].id == id) {
1213             item->current = i;
1214             return;
1215         }
1216
1217     // id not found.  This shouldn't happen.
1218     item->current = -1;
1219 }
1220
1221 static MOVFragmentStreamInfo * get_current_frag_stream_info(
1222     MOVFragmentIndex *frag_index)
1223 {
1224     MOVFragmentIndexItem *item;
1225     if (frag_index->current < 0 ||
1226         frag_index->current >= frag_index->nb_items)
1227         return NULL;
1228
1229     item = &frag_index->item[frag_index->current];
1230     if (item->current >= 0 && item->current < item->nb_stream_info)
1231         return &item->stream_info[item->current];
1232
1233     // This shouldn't happen
1234     return NULL;
1235 }
1236
1237 static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
1238 {
1239     int a, b, m;
1240     int64_t moof_offset;
1241
1242     // Optimize for appending new entries
1243     if (!frag_index->nb_items ||
1244         frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
1245         return frag_index->nb_items;
1246
1247     a = -1;
1248     b = frag_index->nb_items;
1249
1250     while (b - a > 1) {
1251         m = (a + b) >> 1;
1252         moof_offset = frag_index->item[m].moof_offset;
1253         if (moof_offset >= offset)
1254             b = m;
1255         if (moof_offset <= offset)
1256             a = m;
1257     }
1258     return b;
1259 }
1260
1261 static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
1262 {
1263     av_assert0(frag_stream_info);
1264     if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1265         return frag_stream_info->sidx_pts;
1266     if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1267         return frag_stream_info->first_tfra_pts;
1268     return frag_stream_info->tfdt_dts;
1269 }
1270
1271 static int64_t get_frag_time(MOVFragmentIndex *frag_index,
1272                              int index, int track_id)
1273 {
1274     MOVFragmentStreamInfo * frag_stream_info;
1275     int64_t timestamp;
1276     int i;
1277
1278     if (track_id >= 0) {
1279         frag_stream_info = get_frag_stream_info(frag_index, index, track_id);
1280         return frag_stream_info->sidx_pts;
1281     }
1282
1283     for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
1284         frag_stream_info = &frag_index->item[index].stream_info[i];
1285         timestamp = get_stream_info_time(frag_stream_info);
1286         if (timestamp != AV_NOPTS_VALUE)
1287             return timestamp;
1288     }
1289     return AV_NOPTS_VALUE;
1290 }
1291
1292 static int search_frag_timestamp(MOVFragmentIndex *frag_index,
1293                                  AVStream *st, int64_t timestamp)
1294 {
1295     int a, b, m, m0;
1296     int64_t frag_time;
1297     int id = -1;
1298
1299     if (st) {
1300         // If the stream is referenced by any sidx, limit the search
1301         // to fragments that referenced this stream in the sidx
1302         MOVStreamContext *sc = st->priv_data;
1303         if (sc->has_sidx)
1304             id = st->id;
1305     }
1306
1307     a = -1;
1308     b = frag_index->nb_items;
1309
1310     while (b - a > 1) {
1311         m0 = m = (a + b) >> 1;
1312
1313         while (m < b &&
1314                (frag_time = get_frag_time(frag_index, m, id)) == AV_NOPTS_VALUE)
1315             m++;
1316
1317         if (m < b && frag_time <= timestamp)
1318             a = m;
1319         else
1320             b = m0;
1321     }
1322
1323     return a;
1324 }
1325
1326 static int update_frag_index(MOVContext *c, int64_t offset)
1327 {
1328     int index, i;
1329     MOVFragmentIndexItem * item;
1330     MOVFragmentStreamInfo * frag_stream_info;
1331
1332     // If moof_offset already exists in frag_index, return index to it
1333     index = search_frag_moof_offset(&c->frag_index, offset);
1334     if (index < c->frag_index.nb_items &&
1335         c->frag_index.item[index].moof_offset == offset)
1336         return index;
1337
1338     // offset is not yet in frag index.
1339     // Insert new item at index (sorted by moof offset)
1340     item = av_fast_realloc(c->frag_index.item,
1341                            &c->frag_index.allocated_size,
1342                            (c->frag_index.nb_items + 1) *
1343                            sizeof(*c->frag_index.item));
1344     if (!item)
1345         return -1;
1346     c->frag_index.item = item;
1347
1348     frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
1349                                         sizeof(*item->stream_info));
1350     if (!frag_stream_info)
1351         return -1;
1352
1353     for (i = 0; i < c->fc->nb_streams; i++) {
1354         // Avoid building frag index if streams lack track id.
1355         if (c->fc->streams[i]->id < 0) {
1356             av_free(frag_stream_info);
1357             return AVERROR_INVALIDDATA;
1358         }
1359
1360         frag_stream_info[i].id = c->fc->streams[i]->id;
1361         frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
1362         frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
1363         frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
1364         frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
1365         frag_stream_info[i].index_entry = -1;
1366         frag_stream_info[i].encryption_index = NULL;
1367     }
1368
1369     if (index < c->frag_index.nb_items)
1370         memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
1371                 (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
1372
1373     item = &c->frag_index.item[index];
1374     item->headers_read = 0;
1375     item->current = 0;
1376     item->nb_stream_info = c->fc->nb_streams;
1377     item->moof_offset = offset;
1378     item->stream_info = frag_stream_info;
1379     c->frag_index.nb_items++;
1380
1381     return index;
1382 }
1383
1384 static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
1385                                    int id, int entries)
1386 {
1387     int i;
1388     MOVFragmentStreamInfo * frag_stream_info;
1389
1390     if (index < 0)
1391         return;
1392     for (i = index; i < frag_index->nb_items; i++) {
1393         frag_stream_info = get_frag_stream_info(frag_index, i, id);
1394         if (frag_stream_info && frag_stream_info->index_entry >= 0)
1395             frag_stream_info->index_entry += entries;
1396     }
1397 }
1398
1399 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1400 {
1401     // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
1402     c->fragment.found_tfhd = 0;
1403
1404     if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1405         c->has_looked_for_mfra = 1;
1406         if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1407             int ret;
1408             av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1409                     "for a mfra\n");
1410             if ((ret = mov_read_mfra(c, pb)) < 0) {
1411                 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1412                         "read the mfra (may be a live ismv)\n");
1413             }
1414         } else {
1415             av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1416                     "seekable, can not look for mfra\n");
1417         }
1418     }
1419     c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1420     av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1421     c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
1422     return mov_read_default(c, pb, atom);
1423 }
1424
1425 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time, void *logctx)
1426 {
1427     if (time) {
1428         if (time >= 2082844800)
1429             time -= 2082844800;  /* seconds between 1904-01-01 and Epoch */
1430
1431         if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1432             av_log(logctx, AV_LOG_DEBUG, "creation_time is not representable\n");
1433             return;
1434         }
1435
1436         avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1437     }
1438 }
1439
1440 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1441 {
1442     AVStream *st;
1443     MOVStreamContext *sc;
1444     int version;
1445     char language[4] = {0};
1446     unsigned lang;
1447     int64_t creation_time;
1448
1449     if (c->fc->nb_streams < 1)
1450         return 0;
1451     st = c->fc->streams[c->fc->nb_streams-1];
1452     sc = st->priv_data;
1453
1454     if (sc->time_scale) {
1455         av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1456         return AVERROR_INVALIDDATA;
1457     }
1458
1459     version = avio_r8(pb);
1460     if (version > 1) {
1461         avpriv_request_sample(c->fc, "Version %d", version);
1462         return AVERROR_PATCHWELCOME;
1463     }
1464     avio_rb24(pb); /* flags */
1465     if (version == 1) {
1466         creation_time = avio_rb64(pb);
1467         avio_rb64(pb);
1468     } else {
1469         creation_time = avio_rb32(pb);
1470         avio_rb32(pb); /* modification time */
1471     }
1472     mov_metadata_creation_time(&st->metadata, creation_time, c->fc);
1473
1474     sc->time_scale = avio_rb32(pb);
1475     if (sc->time_scale <= 0) {
1476         av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1477         sc->time_scale = 1;
1478     }
1479     st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1480
1481     lang = avio_rb16(pb); /* language */
1482     if (ff_mov_lang_to_iso639(lang, language))
1483         av_dict_set(&st->metadata, "language", language, 0);
1484     avio_rb16(pb); /* quality */
1485
1486     return 0;
1487 }
1488
1489 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1490 {
1491     int i;
1492     int64_t creation_time;
1493     int version = avio_r8(pb); /* version */
1494     avio_rb24(pb); /* flags */
1495
1496     if (version == 1) {
1497         creation_time = avio_rb64(pb);
1498         avio_rb64(pb);
1499     } else {
1500         creation_time = avio_rb32(pb);
1501         avio_rb32(pb); /* modification time */
1502     }
1503     mov_metadata_creation_time(&c->fc->metadata, creation_time, c->fc);
1504     c->time_scale = avio_rb32(pb); /* time scale */
1505     if (c->time_scale <= 0) {
1506         av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
1507         c->time_scale = 1;
1508     }
1509     av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1510
1511     c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1512     // set the AVFormatContext duration because the duration of individual tracks
1513     // may be inaccurate
1514     if (!c->trex_data)
1515         c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1516     avio_rb32(pb); /* preferred scale */
1517
1518     avio_rb16(pb); /* preferred volume */
1519
1520     avio_skip(pb, 10); /* reserved */
1521
1522     /* movie display matrix, store it in main context and use it later on */
1523     for (i = 0; i < 3; i++) {
1524         c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1525         c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1526         c->movie_display_matrix[i][2] = avio_rb32(pb); //  2.30 fixed point
1527     }
1528
1529     avio_rb32(pb); /* preview time */
1530     avio_rb32(pb); /* preview duration */
1531     avio_rb32(pb); /* poster time */
1532     avio_rb32(pb); /* selection time */
1533     avio_rb32(pb); /* selection duration */
1534     avio_rb32(pb); /* current time */
1535     avio_rb32(pb); /* next track ID */
1536
1537     return 0;
1538 }
1539
1540 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1541 {
1542     AVStream *st;
1543     int little_endian;
1544
1545     if (c->fc->nb_streams < 1)
1546         return 0;
1547     st = c->fc->streams[c->fc->nb_streams-1];
1548
1549     little_endian = avio_rb16(pb) & 0xFF;
1550     av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1551     if (little_endian == 1) {
1552         switch (st->codecpar->codec_id) {
1553         case AV_CODEC_ID_PCM_S24BE:
1554             st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
1555             break;
1556         case AV_CODEC_ID_PCM_S32BE:
1557             st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
1558             break;
1559         case AV_CODEC_ID_PCM_F32BE:
1560             st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
1561             break;
1562         case AV_CODEC_ID_PCM_F64BE:
1563             st->codecpar->codec_id = AV_CODEC_ID_PCM_F64LE;
1564             break;
1565         default:
1566             break;
1567         }
1568     }
1569     return 0;
1570 }
1571
1572 static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1573 {
1574     AVStream *st;
1575     uint8_t *icc_profile;
1576     char color_parameter_type[5] = { 0 };
1577     uint16_t color_primaries, color_trc, color_matrix;
1578     int ret;
1579
1580     if (c->fc->nb_streams < 1)
1581         return 0;
1582     st = c->fc->streams[c->fc->nb_streams - 1];
1583
1584     ret = ffio_read_size(pb, color_parameter_type, 4);
1585     if (ret < 0)
1586         return ret;
1587     if (strncmp(color_parameter_type, "nclx", 4) &&
1588         strncmp(color_parameter_type, "nclc", 4) &&
1589         strncmp(color_parameter_type, "prof", 4)) {
1590         av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1591                color_parameter_type);
1592         return 0;
1593     }
1594
1595     if (!strncmp(color_parameter_type, "prof", 4)) {
1596         icc_profile = av_stream_new_side_data(st, AV_PKT_DATA_ICC_PROFILE, atom.size - 4);
1597         if (!icc_profile)
1598             return AVERROR(ENOMEM);
1599         ret = ffio_read_size(pb, icc_profile, atom.size - 4);
1600         if (ret < 0)
1601             return ret;
1602     } else {
1603         color_primaries = avio_rb16(pb);
1604         color_trc = avio_rb16(pb);
1605         color_matrix = avio_rb16(pb);
1606
1607         av_log(c->fc, AV_LOG_TRACE,
1608                "%s: pri %d trc %d matrix %d",
1609                color_parameter_type, color_primaries, color_trc, color_matrix);
1610
1611         if (!strncmp(color_parameter_type, "nclx", 4)) {
1612             uint8_t color_range = avio_r8(pb) >> 7;
1613             av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1614             if (color_range)
1615                 st->codecpar->color_range = AVCOL_RANGE_JPEG;
1616             else
1617                 st->codecpar->color_range = AVCOL_RANGE_MPEG;
1618         }
1619
1620         if (!av_color_primaries_name(color_primaries))
1621             color_primaries = AVCOL_PRI_UNSPECIFIED;
1622         if (!av_color_transfer_name(color_trc))
1623             color_trc = AVCOL_TRC_UNSPECIFIED;
1624         if (!av_color_space_name(color_matrix))
1625             color_matrix = AVCOL_SPC_UNSPECIFIED;
1626
1627         st->codecpar->color_primaries = color_primaries;
1628         st->codecpar->color_trc       = color_trc;
1629         st->codecpar->color_space     = color_matrix;
1630         av_log(c->fc, AV_LOG_TRACE, "\n");
1631     }
1632     return 0;
1633 }
1634
1635 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1636 {
1637     AVStream *st;
1638     unsigned mov_field_order;
1639     enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1640
1641     if (c->fc->nb_streams < 1) // will happen with jp2 files
1642         return 0;
1643     st = c->fc->streams[c->fc->nb_streams-1];
1644     if (atom.size < 2)
1645         return AVERROR_INVALIDDATA;
1646     mov_field_order = avio_rb16(pb);
1647     if ((mov_field_order & 0xFF00) == 0x0100)
1648         decoded_field_order = AV_FIELD_PROGRESSIVE;
1649     else if ((mov_field_order & 0xFF00) == 0x0200) {
1650         switch (mov_field_order & 0xFF) {
1651         case 0x01: decoded_field_order = AV_FIELD_TT;
1652                    break;
1653         case 0x06: decoded_field_order = AV_FIELD_BB;
1654                    break;
1655         case 0x09: decoded_field_order = AV_FIELD_TB;
1656                    break;
1657         case 0x0E: decoded_field_order = AV_FIELD_BT;
1658                    break;
1659         }
1660     }
1661     if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1662         av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1663     }
1664     st->codecpar->field_order = decoded_field_order;
1665
1666     return 0;
1667 }
1668
1669 static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
1670 {
1671     int err = 0;
1672     uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1673     if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1674         return AVERROR_INVALIDDATA;
1675     if ((err = av_reallocp(&par->extradata, size)) < 0) {
1676         par->extradata_size = 0;
1677         return err;
1678     }
1679     par->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE;
1680     return 0;
1681 }
1682
1683 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1684 static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1685                                         AVCodecParameters *par, uint8_t *buf)
1686 {
1687     int64_t result = atom.size;
1688     int err;
1689
1690     AV_WB32(buf    , atom.size + 8);
1691     AV_WL32(buf + 4, atom.type);
1692     err = ffio_read_size(pb, buf + 8, atom.size);
1693     if (err < 0) {
1694         par->extradata_size -= atom.size;
1695         return err;
1696     } else if (err < atom.size) {
1697         av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1698         par->extradata_size -= atom.size - err;
1699         result = err;
1700     }
1701     memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1702     return result;
1703 }
1704
1705 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1706 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1707                               enum AVCodecID codec_id)
1708 {
1709     AVStream *st;
1710     uint64_t original_size;
1711     int err;
1712
1713     if (c->fc->nb_streams < 1) // will happen with jp2 files
1714         return 0;
1715     st = c->fc->streams[c->fc->nb_streams-1];
1716
1717     if (st->codecpar->codec_id != codec_id)
1718         return 0; /* unexpected codec_id - don't mess with extradata */
1719
1720     original_size = st->codecpar->extradata_size;
1721     err = mov_realloc_extradata(st->codecpar, atom);
1722     if (err)
1723         return err;
1724
1725     err =  mov_read_atom_into_extradata(c, pb, atom, st->codecpar,  st->codecpar->extradata + original_size);
1726     if (err < 0)
1727         return err;
1728     return 0; // Note: this is the original behavior to ignore truncation.
1729 }
1730
1731 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1732 static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1733 {
1734     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1735 }
1736
1737 static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1738 {
1739     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1740 }
1741
1742 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1743 {
1744     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1745 }
1746
1747 static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1748 {
1749     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1750 }
1751
1752 static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1753 {
1754     int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1755     if (!ret)
1756         ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1757     return ret;
1758 }
1759
1760 static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1761 {
1762     int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1763
1764     if (!ret && c->fc->nb_streams >= 1) {
1765         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1766         if (par->extradata_size >= 40) {
1767             par->height = AV_RB16(&par->extradata[36]);
1768             par->width  = AV_RB16(&par->extradata[38]);
1769         }
1770     }
1771     return ret;
1772 }
1773
1774 static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1775 {
1776     if (c->fc->nb_streams >= 1) {
1777         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1778         if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1779             par->codec_id == AV_CODEC_ID_H264 &&
1780             atom.size > 11) {
1781             int cid;
1782             avio_skip(pb, 10);
1783             cid = avio_rb16(pb);
1784             /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1785             if (cid == 0xd4d || cid == 0xd4e)
1786                 par->width = 1440;
1787             return 0;
1788         } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
1789                     par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
1790                     par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
1791                    atom.size >= 24) {
1792             int num, den;
1793             avio_skip(pb, 12);
1794             num = avio_rb32(pb);
1795             den = avio_rb32(pb);
1796             if (num <= 0 || den <= 0)
1797                 return 0;
1798             switch (avio_rb32(pb)) {
1799             case 2:
1800                 if (den >= INT_MAX / 2)
1801                     return 0;
1802                 den *= 2;
1803             case 1:
1804                 c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.num = num;
1805                 c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.den = den;
1806             default:
1807                 return 0;
1808             }
1809         }
1810     }
1811
1812     return mov_read_avid(c, pb, atom);
1813 }
1814
1815 static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1816 {
1817     int ret = 0;
1818     int length = 0;
1819     uint64_t original_size;
1820     if (c->fc->nb_streams >= 1) {
1821         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1822         if (par->codec_id == AV_CODEC_ID_H264)
1823             return 0;
1824         if (atom.size == 16) {
1825             original_size = par->extradata_size;
1826             ret = mov_realloc_extradata(par, atom);
1827             if (!ret) {
1828                 length =  mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
1829                 if (length == atom.size) {
1830                     const uint8_t range_value = par->extradata[original_size + 19];
1831                     switch (range_value) {
1832                     case 1:
1833                         par->color_range = AVCOL_RANGE_MPEG;
1834                         break;
1835                     case 2:
1836                         par->color_range = AVCOL_RANGE_JPEG;
1837                         break;
1838                     default:
1839                         av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1840                         break;
1841                     }
1842                     ff_dlog(c->fc, "color_range: %d\n", par->color_range);
1843                 } else {
1844                   /* For some reason the whole atom was not added to the extradata */
1845                   av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1846                 }
1847             } else {
1848                 av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1849             }
1850         } else {
1851             av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1852         }
1853     }
1854
1855     return ret;
1856 }
1857
1858 static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1859 {
1860     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1861 }
1862
1863 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1864 {
1865     AVStream *st;
1866     int ret;
1867
1868     if (c->fc->nb_streams < 1)
1869         return 0;
1870     st = c->fc->streams[c->fc->nb_streams-1];
1871
1872     if ((uint64_t)atom.size > (1<<30))
1873         return AVERROR_INVALIDDATA;
1874
1875     if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
1876         st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
1877         st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
1878         // pass all frma atom to codec, needed at least for QDMC and QDM2
1879         ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1880         if (ret < 0)
1881             return ret;
1882     } else if (atom.size > 8) { /* to read frma, esds atoms */
1883         if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1884             uint64_t buffer;
1885             ret = ffio_ensure_seekback(pb, 8);
1886             if (ret < 0)
1887                 return ret;
1888             buffer = avio_rb64(pb);
1889             atom.size -= 8;
1890             if (  (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1891                 && buffer >> 32 <= atom.size
1892                 && buffer >> 32 >= 8) {
1893                 avio_skip(pb, -8);
1894                 atom.size += 8;
1895             } else if (!st->codecpar->extradata_size) {
1896 #define ALAC_EXTRADATA_SIZE 36
1897                 st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
1898                 if (!st->codecpar->extradata)
1899                     return AVERROR(ENOMEM);
1900                 st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
1901                 AV_WB32(st->codecpar->extradata    , ALAC_EXTRADATA_SIZE);
1902                 AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
1903                 AV_WB64(st->codecpar->extradata + 12, buffer);
1904                 avio_read(pb, st->codecpar->extradata + 20, 16);
1905                 avio_skip(pb, atom.size - 24);
1906                 return 0;
1907             }
1908         }
1909         if ((ret = mov_read_default(c, pb, atom)) < 0)
1910             return ret;
1911     } else
1912         avio_skip(pb, atom.size);
1913     return 0;
1914 }
1915
1916 /**
1917  * This function reads atom content and puts data in extradata without tag
1918  * nor size unlike mov_read_extradata.
1919  */
1920 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1921 {
1922     AVStream *st;
1923     int ret;
1924
1925     if (c->fc->nb_streams < 1)
1926         return 0;
1927     st = c->fc->streams[c->fc->nb_streams-1];
1928
1929     if ((uint64_t)atom.size > (1<<30))
1930         return AVERROR_INVALIDDATA;
1931
1932     if (atom.size >= 10) {
1933         // Broken files created by legacy versions of libavformat will
1934         // wrap a whole fiel atom inside of a glbl atom.
1935         unsigned size = avio_rb32(pb);
1936         unsigned type = avio_rl32(pb);
1937         avio_seek(pb, -8, SEEK_CUR);
1938         if (type == MKTAG('f','i','e','l') && size == atom.size)
1939             return mov_read_default(c, pb, atom);
1940     }
1941     if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
1942         av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
1943         return 0;
1944     }
1945     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1946     if (ret < 0)
1947         return ret;
1948     if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
1949         /* HEVC-based Dolby Vision derived from hvc1.
1950            Happens to match with an identifier
1951            previously utilized for DV. Thus, if we have
1952            the hvcC extradata box available as specified,
1953            set codec to HEVC */
1954         st->codecpar->codec_id = AV_CODEC_ID_HEVC;
1955
1956     return 0;
1957 }
1958
1959 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1960 {
1961     AVStream *st;
1962     uint8_t profile_level;
1963     int ret;
1964
1965     if (c->fc->nb_streams < 1)
1966         return 0;
1967     st = c->fc->streams[c->fc->nb_streams-1];
1968
1969     if (atom.size >= (1<<28) || atom.size < 7)
1970         return AVERROR_INVALIDDATA;
1971
1972     profile_level = avio_r8(pb);
1973     if ((profile_level & 0xf0) != 0xc0)
1974         return 0;
1975
1976     avio_seek(pb, 6, SEEK_CUR);
1977     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
1978     if (ret < 0)
1979         return ret;
1980
1981     return 0;
1982 }
1983
1984 /**
1985  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1986  * but can have extradata appended at the end after the 40 bytes belonging
1987  * to the struct.
1988  */
1989 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1990 {
1991     AVStream *st;
1992     int ret;
1993
1994     if (c->fc->nb_streams < 1)
1995         return 0;
1996     if (atom.size <= 40)
1997         return 0;
1998     st = c->fc->streams[c->fc->nb_streams-1];
1999
2000     if ((uint64_t)atom.size > (1<<30))
2001         return AVERROR_INVALIDDATA;
2002
2003     avio_skip(pb, 40);
2004     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
2005     if (ret < 0)
2006         return ret;
2007
2008     return 0;
2009 }
2010
2011 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2012 {
2013     AVStream *st;
2014     MOVStreamContext *sc;
2015     unsigned int i, entries;
2016
2017     if (c->trak_index < 0) {
2018         av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
2019         return 0;
2020     }
2021     if (c->fc->nb_streams < 1)
2022         return 0;
2023     st = c->fc->streams[c->fc->nb_streams-1];
2024     sc = st->priv_data;
2025
2026     avio_r8(pb); /* version */
2027     avio_rb24(pb); /* flags */
2028
2029     entries = avio_rb32(pb);
2030
2031     if (!entries)
2032         return 0;
2033
2034     if (sc->chunk_offsets)
2035         av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
2036     av_free(sc->chunk_offsets);
2037     sc->chunk_count = 0;
2038     sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2039     if (!sc->chunk_offsets)
2040         return AVERROR(ENOMEM);
2041     sc->chunk_count = entries;
2042
2043     if      (atom.type == MKTAG('s','t','c','o'))
2044         for (i = 0; i < entries && !pb->eof_reached; i++)
2045             sc->chunk_offsets[i] = avio_rb32(pb);
2046     else if (atom.type == MKTAG('c','o','6','4'))
2047         for (i = 0; i < entries && !pb->eof_reached; i++)
2048             sc->chunk_offsets[i] = avio_rb64(pb);
2049     else
2050         return AVERROR_INVALIDDATA;
2051
2052     sc->chunk_count = i;
2053
2054     if (pb->eof_reached) {
2055         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
2056         return AVERROR_EOF;
2057     }
2058
2059     return 0;
2060 }
2061
2062 static int mov_codec_id(AVStream *st, uint32_t format)
2063 {
2064     int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
2065
2066     if (id <= 0 &&
2067         ((format & 0xFFFF) == 'm' + ('s' << 8) ||
2068          (format & 0xFFFF) == 'T' + ('S' << 8)))
2069         id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
2070
2071     if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
2072         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
2073     } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
2074                /* skip old ASF MPEG-4 tag */
2075                format && format != MKTAG('m','p','4','s')) {
2076         id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2077         if (id <= 0)
2078             id = ff_codec_get_id(ff_codec_bmp_tags, format);
2079         if (id > 0)
2080             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
2081         else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
2082                     (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2083                     st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
2084             id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
2085             if (id > 0)
2086                 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
2087             else
2088                 id = ff_codec_get_id(ff_codec_movdata_tags, format);
2089         }
2090     }
2091
2092     st->codecpar->codec_tag = format;
2093
2094     return id;
2095 }
2096
2097 static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
2098                                  AVStream *st, MOVStreamContext *sc)
2099 {
2100     uint8_t codec_name[32] = { 0 };
2101     int64_t stsd_start;
2102     unsigned int len;
2103     uint32_t id = 0;
2104
2105     /* The first 16 bytes of the video sample description are already
2106      * read in ff_mov_read_stsd_entries() */
2107     stsd_start = avio_tell(pb) - 16;
2108
2109     avio_rb16(pb); /* version */
2110     avio_rb16(pb); /* revision level */
2111     id = avio_rl32(pb); /* vendor */
2112     av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2113     avio_rb32(pb); /* temporal quality */
2114     avio_rb32(pb); /* spatial quality */
2115
2116     st->codecpar->width  = avio_rb16(pb); /* width */
2117     st->codecpar->height = avio_rb16(pb); /* height */
2118
2119     avio_rb32(pb); /* horiz resolution */
2120     avio_rb32(pb); /* vert resolution */
2121     avio_rb32(pb); /* data size, always 0 */
2122     avio_rb16(pb); /* frames per samples */
2123
2124     len = avio_r8(pb); /* codec name, pascal string */
2125     if (len > 31)
2126         len = 31;
2127     mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
2128     if (len < 31)
2129         avio_skip(pb, 31 - len);
2130
2131     if (codec_name[0])
2132         av_dict_set(&st->metadata, "encoder", codec_name, 0);
2133
2134     /* codec_tag YV12 triggers an UV swap in rawdec.c */
2135     if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
2136         st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
2137         st->codecpar->width &= ~1;
2138         st->codecpar->height &= ~1;
2139     }
2140     /* Flash Media Server uses tag H.263 with Sorenson Spark */
2141     if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
2142         !strncmp(codec_name, "Sorenson H263", 13))
2143         st->codecpar->codec_id = AV_CODEC_ID_FLV1;
2144
2145     st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
2146
2147     avio_seek(pb, stsd_start, SEEK_SET);
2148
2149     if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2150         st->codecpar->bits_per_coded_sample &= 0x1F;
2151         sc->has_palette = 1;
2152     }
2153 }
2154
2155 static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
2156                                  AVStream *st, MOVStreamContext *sc)
2157 {
2158     int bits_per_sample, flags;
2159     uint16_t version = avio_rb16(pb);
2160     uint32_t id = 0;
2161     AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
2162
2163     avio_rb16(pb); /* revision level */
2164     id = avio_rl32(pb); /* vendor */
2165     av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2166
2167     st->codecpar->channels              = avio_rb16(pb); /* channel count */
2168     st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
2169     av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
2170
2171     sc->audio_cid = avio_rb16(pb);
2172     avio_rb16(pb); /* packet size = 0 */
2173
2174     st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
2175
2176     // Read QT version 1 fields. In version 0 these do not exist.
2177     av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
2178     if (!c->isom ||
2179         (compatible_brands && strstr(compatible_brands->value, "qt  ")) ||
2180         (sc->stsd_version == 0 && version > 0)) {
2181         if (version == 1) {
2182             sc->samples_per_frame = avio_rb32(pb);
2183             avio_rb32(pb); /* bytes per packet */
2184             sc->bytes_per_frame = avio_rb32(pb);
2185             avio_rb32(pb); /* bytes per sample */
2186         } else if (version == 2) {
2187             avio_rb32(pb); /* sizeof struct only */
2188             st->codecpar->sample_rate = av_int2double(avio_rb64(pb));
2189             st->codecpar->channels    = avio_rb32(pb);
2190             avio_rb32(pb); /* always 0x7F000000 */
2191             st->codecpar->bits_per_coded_sample = avio_rb32(pb);
2192
2193             flags = avio_rb32(pb); /* lpcm format specific flag */
2194             sc->bytes_per_frame   = avio_rb32(pb);
2195             sc->samples_per_frame = avio_rb32(pb);
2196             if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
2197                 st->codecpar->codec_id =
2198                     ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample,
2199                                              flags);
2200         }
2201         if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2202             /* can't correctly handle variable sized packet as audio unit */
2203             switch (st->codecpar->codec_id) {
2204             case AV_CODEC_ID_MP2:
2205             case AV_CODEC_ID_MP3:
2206                 st->need_parsing = AVSTREAM_PARSE_FULL;
2207                 break;
2208             }
2209         }
2210     }
2211
2212     if (sc->format == 0) {
2213         if (st->codecpar->bits_per_coded_sample == 8)
2214             st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
2215         else if (st->codecpar->bits_per_coded_sample == 16)
2216             st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
2217     }
2218
2219     switch (st->codecpar->codec_id) {
2220     case AV_CODEC_ID_PCM_S8:
2221     case AV_CODEC_ID_PCM_U8:
2222         if (st->codecpar->bits_per_coded_sample == 16)
2223             st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
2224         break;
2225     case AV_CODEC_ID_PCM_S16LE:
2226     case AV_CODEC_ID_PCM_S16BE:
2227         if (st->codecpar->bits_per_coded_sample == 8)
2228             st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
2229         else if (st->codecpar->bits_per_coded_sample == 24)
2230             st->codecpar->codec_id =
2231                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
2232                 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
2233         else if (st->codecpar->bits_per_coded_sample == 32)
2234              st->codecpar->codec_id =
2235                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
2236                 AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
2237         break;
2238     /* set values for old format before stsd version 1 appeared */
2239     case AV_CODEC_ID_MACE3:
2240         sc->samples_per_frame = 6;
2241         sc->bytes_per_frame   = 2 * st->codecpar->channels;
2242         break;
2243     case AV_CODEC_ID_MACE6:
2244         sc->samples_per_frame = 6;
2245         sc->bytes_per_frame   = 1 * st->codecpar->channels;
2246         break;
2247     case AV_CODEC_ID_ADPCM_IMA_QT:
2248         sc->samples_per_frame = 64;
2249         sc->bytes_per_frame   = 34 * st->codecpar->channels;
2250         break;
2251     case AV_CODEC_ID_GSM:
2252         sc->samples_per_frame = 160;
2253         sc->bytes_per_frame   = 33;
2254         break;
2255     default:
2256         break;
2257     }
2258
2259     bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2260     if (bits_per_sample) {
2261         st->codecpar->bits_per_coded_sample = bits_per_sample;
2262         sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
2263     }
2264 }
2265
2266 static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
2267                                     AVStream *st, MOVStreamContext *sc,
2268                                     int64_t size)
2269 {
2270     // ttxt stsd contains display flags, justification, background
2271     // color, fonts, and default styles, so fake an atom to read it
2272     MOVAtom fake_atom = { .size = size };
2273     // mp4s contains a regular esds atom
2274     if (st->codecpar->codec_tag != AV_RL32("mp4s"))
2275         mov_read_glbl(c, pb, fake_atom);
2276     st->codecpar->width  = sc->width;
2277     st->codecpar->height = sc->height;
2278 }
2279
2280 static uint32_t yuv_to_rgba(uint32_t ycbcr)
2281 {
2282     uint8_t r, g, b;
2283     int y, cb, cr;
2284
2285     y  = (ycbcr >> 16) & 0xFF;
2286     cr = (ycbcr >> 8)  & 0xFF;
2287     cb =  ycbcr        & 0xFF;
2288
2289     b = av_clip_uint8((1164 * (y - 16)                     + 2018 * (cb - 128)) / 1000);
2290     g = av_clip_uint8((1164 * (y - 16) -  813 * (cr - 128) -  391 * (cb - 128)) / 1000);
2291     r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128)                    ) / 1000);
2292
2293     return (r << 16) | (g << 8) | b;
2294 }
2295
2296 static int mov_rewrite_dvd_sub_extradata(AVStream *st)
2297 {
2298     char buf[256] = {0};
2299     uint8_t *src = st->codecpar->extradata;
2300     int i, ret;
2301
2302     if (st->codecpar->extradata_size != 64)
2303         return 0;
2304
2305     if (st->codecpar->width > 0 &&  st->codecpar->height > 0)
2306         snprintf(buf, sizeof(buf), "size: %dx%d\n",
2307                  st->codecpar->width, st->codecpar->height);
2308     av_strlcat(buf, "palette: ", sizeof(buf));
2309
2310     for (i = 0; i < 16; i++) {
2311         uint32_t yuv = AV_RB32(src + i * 4);
2312         uint32_t rgba = yuv_to_rgba(yuv);
2313
2314         av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
2315     }
2316
2317     if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
2318         return 0;
2319
2320     ret = ff_alloc_extradata(st->codecpar, strlen(buf));
2321     if (ret < 0)
2322         return ret;
2323     memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
2324
2325     return 0;
2326 }
2327
2328 static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
2329                                 AVStream *st, MOVStreamContext *sc,
2330                                 int64_t size)
2331 {
2332     int ret;
2333
2334     if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2335         if ((int)size != size)
2336             return AVERROR(ENOMEM);
2337
2338         ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2339         if (ret < 0)
2340             return ret;
2341         if (size > 16) {
2342             MOVStreamContext *tmcd_ctx = st->priv_data;
2343             int val;
2344             val = AV_RB32(st->codecpar->extradata + 4);
2345             tmcd_ctx->tmcd_flags = val;
2346             st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
2347             st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
2348 #if FF_API_LAVF_AVCTX
2349 FF_DISABLE_DEPRECATION_WARNINGS
2350             st->codec->time_base = av_inv_q(st->avg_frame_rate);
2351 FF_ENABLE_DEPRECATION_WARNINGS
2352 #endif
2353             /* adjust for per frame dur in counter mode */
2354             if (tmcd_ctx->tmcd_flags & 0x0008) {
2355                 int timescale = AV_RB32(st->codecpar->extradata + 8);
2356                 int framedur = AV_RB32(st->codecpar->extradata + 12);
2357                 st->avg_frame_rate = av_mul_q(st->avg_frame_rate, (AVRational){timescale, framedur});
2358 #if FF_API_LAVF_AVCTX
2359 FF_DISABLE_DEPRECATION_WARNINGS
2360                 st->codec->time_base = av_mul_q(st->codec->time_base , (AVRational){framedur, timescale});
2361 FF_ENABLE_DEPRECATION_WARNINGS
2362 #endif
2363             }
2364             if (size > 30) {
2365                 uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2366                 uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2367                 if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2368                     uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2369                     if (str_size > 0 && size >= (int)str_size + 30 &&
2370                         st->codecpar->extradata[30] /* Don't add empty string */) {
2371                         char *reel_name = av_malloc(str_size + 1);
2372                         if (!reel_name)
2373                             return AVERROR(ENOMEM);
2374                         memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2375                         reel_name[str_size] = 0; /* Add null terminator */
2376                         av_dict_set(&st->metadata, "reel_name", reel_name,
2377                                     AV_DICT_DONT_STRDUP_VAL);
2378                     }
2379                 }
2380             }
2381         }
2382     } else {
2383         /* other codec type, just skip (rtp, mp4s ...) */
2384         avio_skip(pb, size);
2385     }
2386     return 0;
2387 }
2388
2389 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
2390                                    AVStream *st, MOVStreamContext *sc)
2391 {
2392     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2393         !st->codecpar->sample_rate && sc->time_scale > 1)
2394         st->codecpar->sample_rate = sc->time_scale;
2395
2396     /* special codec parameters handling */
2397     switch (st->codecpar->codec_id) {
2398 #if CONFIG_DV_DEMUXER
2399     case AV_CODEC_ID_DVAUDIO:
2400         c->dv_fctx = avformat_alloc_context();
2401         if (!c->dv_fctx) {
2402             av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2403             return AVERROR(ENOMEM);
2404         }
2405         c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2406         if (!c->dv_demux) {
2407             av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2408             return AVERROR(ENOMEM);
2409         }
2410         sc->dv_audio_container = 1;
2411         st->codecpar->codec_id    = AV_CODEC_ID_PCM_S16LE;
2412         break;
2413 #endif
2414     /* no ifdef since parameters are always those */
2415     case AV_CODEC_ID_QCELP:
2416         st->codecpar->channels = 1;
2417         // force sample rate for qcelp when not stored in mov
2418         if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2419             st->codecpar->sample_rate = 8000;
2420         // FIXME: Why is the following needed for some files?
2421         sc->samples_per_frame = 160;
2422         if (!sc->bytes_per_frame)
2423             sc->bytes_per_frame = 35;
2424         break;
2425     case AV_CODEC_ID_AMR_NB:
2426         st->codecpar->channels    = 1;
2427         /* force sample rate for amr, stsd in 3gp does not store sample rate */
2428         st->codecpar->sample_rate = 8000;
2429         break;
2430     case AV_CODEC_ID_AMR_WB:
2431         st->codecpar->channels    = 1;
2432         st->codecpar->sample_rate = 16000;
2433         break;
2434     case AV_CODEC_ID_MP2:
2435     case AV_CODEC_ID_MP3:
2436         /* force type after stsd for m1a hdlr */
2437         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
2438         break;
2439     case AV_CODEC_ID_GSM:
2440     case AV_CODEC_ID_ADPCM_MS:
2441     case AV_CODEC_ID_ADPCM_IMA_WAV:
2442     case AV_CODEC_ID_ILBC:
2443     case AV_CODEC_ID_MACE3:
2444     case AV_CODEC_ID_MACE6:
2445     case AV_CODEC_ID_QDM2:
2446         st->codecpar->block_align = sc->bytes_per_frame;
2447         break;
2448     case AV_CODEC_ID_ALAC:
2449         if (st->codecpar->extradata_size == 36) {
2450             st->codecpar->channels    = AV_RB8 (st->codecpar->extradata + 21);
2451             st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2452         }
2453         break;
2454     case AV_CODEC_ID_AC3:
2455     case AV_CODEC_ID_EAC3:
2456     case AV_CODEC_ID_MPEG1VIDEO:
2457     case AV_CODEC_ID_VC1:
2458     case AV_CODEC_ID_VP8:
2459     case AV_CODEC_ID_VP9:
2460         st->need_parsing = AVSTREAM_PARSE_FULL;
2461         break;
2462     case AV_CODEC_ID_AV1:
2463         st->need_parsing = AVSTREAM_PARSE_HEADERS;
2464         break;
2465     default:
2466         break;
2467     }
2468     return 0;
2469 }
2470
2471 static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
2472                                   int codec_tag, int format,
2473                                   int64_t size)
2474 {
2475     int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2476
2477     if (codec_tag &&
2478          (codec_tag != format &&
2479           // AVID 1:1 samples with differing data format and codec tag exist
2480           (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
2481           // prores is allowed to have differing data format and codec tag
2482           codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
2483           // so is dv (sigh)
2484           codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
2485           (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2486                                  : codec_tag != MKTAG('j','p','e','g')))) {
2487         /* Multiple fourcc, we skip JPEG. This is not correct, we should
2488          * export it as a separate AVStream but this needs a few changes
2489          * in the MOV demuxer, patch welcome. */
2490
2491         av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2492         avio_skip(pb, size);
2493         return 1;
2494     }
2495
2496     return 0;
2497 }
2498
2499 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
2500 {
2501     AVStream *st;
2502     MOVStreamContext *sc;
2503     int pseudo_stream_id;
2504
2505     av_assert0 (c->fc->nb_streams >= 1);
2506     st = c->fc->streams[c->fc->nb_streams-1];
2507     sc = st->priv_data;
2508
2509     for (pseudo_stream_id = 0;
2510          pseudo_stream_id < entries && !pb->eof_reached;
2511          pseudo_stream_id++) {
2512         //Parsing Sample description table
2513         enum AVCodecID id;
2514         int ret, dref_id = 1;
2515         MOVAtom a = { AV_RL32("stsd") };
2516         int64_t start_pos = avio_tell(pb);
2517         int64_t size    = avio_rb32(pb); /* size */
2518         uint32_t format = avio_rl32(pb); /* data format */
2519
2520         if (size >= 16) {
2521             avio_rb32(pb); /* reserved */
2522             avio_rb16(pb); /* reserved */
2523             dref_id = avio_rb16(pb);
2524         } else if (size <= 7) {
2525             av_log(c->fc, AV_LOG_ERROR,
2526                    "invalid size %"PRId64" in stsd\n", size);
2527             return AVERROR_INVALIDDATA;
2528         }
2529
2530         if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
2531                                    size - (avio_tell(pb) - start_pos))) {
2532             sc->stsd_count++;
2533             continue;
2534         }
2535
2536         sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2537         sc->dref_id= dref_id;
2538         sc->format = format;
2539
2540         id = mov_codec_id(st, format);
2541
2542         av_log(c->fc, AV_LOG_TRACE,
2543                "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
2544                av_fourcc2str(format), st->codecpar->codec_type);
2545
2546         st->codecpar->codec_id = id;
2547         if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
2548             mov_parse_stsd_video(c, pb, st, sc);
2549         } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
2550             mov_parse_stsd_audio(c, pb, st, sc);
2551             if (st->codecpar->sample_rate < 0) {
2552                 av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
2553                 return AVERROR_INVALIDDATA;
2554             }
2555         } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
2556             mov_parse_stsd_subtitle(c, pb, st, sc,
2557                                     size - (avio_tell(pb) - start_pos));
2558         } else {
2559             ret = mov_parse_stsd_data(c, pb, st, sc,
2560                                       size - (avio_tell(pb) - start_pos));
2561             if (ret < 0)
2562                 return ret;
2563         }
2564         /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2565         a.size = size - (avio_tell(pb) - start_pos);
2566         if (a.size > 8) {
2567             if ((ret = mov_read_default(c, pb, a)) < 0)
2568                 return ret;
2569         } else if (a.size > 0)
2570             avio_skip(pb, a.size);
2571
2572         if (sc->extradata && st->codecpar->extradata) {
2573             int extra_size = st->codecpar->extradata_size;
2574
2575             /* Move the current stream extradata to the stream context one. */
2576             sc->extradata_size[pseudo_stream_id] = extra_size;
2577             sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
2578             st->codecpar->extradata      = NULL;
2579             st->codecpar->extradata_size = 0;
2580         }
2581         sc->stsd_count++;
2582     }
2583
2584     if (pb->eof_reached) {
2585         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
2586         return AVERROR_EOF;
2587     }
2588
2589     return 0;
2590 }
2591
2592 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2593 {
2594     AVStream *st;
2595     MOVStreamContext *sc;
2596     int ret, entries;
2597
2598     if (c->fc->nb_streams < 1)
2599         return 0;
2600     st = c->fc->streams[c->fc->nb_streams - 1];
2601     sc = st->priv_data;
2602
2603     sc->stsd_version = avio_r8(pb);
2604     avio_rb24(pb); /* flags */
2605     entries = avio_rb32(pb);
2606
2607     /* Each entry contains a size (4 bytes) and format (4 bytes). */
2608     if (entries <= 0 || entries > atom.size / 8) {
2609         av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
2610         return AVERROR_INVALIDDATA;
2611     }
2612
2613     if (sc->extradata) {
2614         av_log(c->fc, AV_LOG_ERROR,
2615                "Duplicate stsd found in this track.\n");
2616         return AVERROR_INVALIDDATA;
2617     }
2618
2619     /* Prepare space for hosting multiple extradata. */
2620     sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
2621     if (!sc->extradata)
2622         return AVERROR(ENOMEM);
2623
2624     sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
2625     if (!sc->extradata_size) {
2626         ret = AVERROR(ENOMEM);
2627         goto fail;
2628     }
2629
2630     ret = ff_mov_read_stsd_entries(c, pb, entries);
2631     if (ret < 0)
2632         goto fail;
2633
2634     /* Restore back the primary extradata. */
2635     av_freep(&st->codecpar->extradata);
2636     st->codecpar->extradata_size = sc->extradata_size[0];
2637     if (sc->extradata_size[0]) {
2638         st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
2639         if (!st->codecpar->extradata)
2640             return AVERROR(ENOMEM);
2641         memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
2642     }
2643
2644     return mov_finalize_stsd_codec(c, pb, st, sc);
2645 fail:
2646     if (sc->extradata) {
2647         int j;
2648         for (j = 0; j < sc->stsd_count; j++)
2649             av_freep(&sc->extradata[j]);
2650     }
2651
2652     av_freep(&sc->extradata);
2653     av_freep(&sc->extradata_size);
2654     return ret;
2655 }
2656
2657 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2658 {
2659     AVStream *st;
2660     MOVStreamContext *sc;
2661     unsigned int i, entries;
2662
2663     if (c->fc->nb_streams < 1)
2664         return 0;
2665     st = c->fc->streams[c->fc->nb_streams-1];
2666     sc = st->priv_data;
2667
2668     avio_r8(pb); /* version */
2669     avio_rb24(pb); /* flags */
2670
2671     entries = avio_rb32(pb);
2672     if ((uint64_t)entries * 12 + 4 > atom.size)
2673         return AVERROR_INVALIDDATA;
2674
2675     av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
2676
2677     if (!entries)
2678         return 0;
2679     if (sc->stsc_data)
2680         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
2681     av_free(sc->stsc_data);
2682     sc->stsc_count = 0;
2683     sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2684     if (!sc->stsc_data)
2685         return AVERROR(ENOMEM);
2686
2687     for (i = 0; i < entries && !pb->eof_reached; i++) {
2688         sc->stsc_data[i].first = avio_rb32(pb);
2689         sc->stsc_data[i].count = avio_rb32(pb);
2690         sc->stsc_data[i].id = avio_rb32(pb);
2691     }
2692
2693     sc->stsc_count = i;
2694     for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2695         int64_t first_min = i + 1;
2696         if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2697             (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2698             sc->stsc_data[i].first < first_min ||
2699             sc->stsc_data[i].count < 1 ||
2700             sc->stsc_data[i].id < 1) {
2701             av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
2702             if (i+1 >= sc->stsc_count) {
2703                 if (sc->stsc_data[i].count == 0 && i > 0) {
2704                     sc->stsc_count --;
2705                     continue;
2706                 }
2707                 sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2708                 if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2709                     sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2710                 sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2711                 sc->stsc_data[i].id    = FFMAX(sc->stsc_data[i].id, 1);
2712                 continue;
2713             }
2714             av_assert0(sc->stsc_data[i+1].first >= 2);
2715             // We replace this entry by the next valid
2716             sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2717             sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2718             sc->stsc_data[i].id    = sc->stsc_data[i+1].id;
2719         }
2720     }
2721
2722     if (pb->eof_reached) {
2723         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
2724         return AVERROR_EOF;
2725     }
2726
2727     return 0;
2728 }
2729
2730 static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
2731 {
2732     return index < count - 1;
2733 }
2734
2735 /* Compute the samples value for the stsc entry at the given index. */
2736 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
2737 {
2738     int chunk_count;
2739
2740     if (mov_stsc_index_valid(index, sc->stsc_count))
2741         chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2742     else {
2743         // Validation for stsc / stco  happens earlier in mov_read_stsc + mov_read_trak.
2744         av_assert0(sc->stsc_data[index].first <= sc->chunk_count);
2745         chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2746     }
2747
2748     return sc->stsc_data[index].count * (int64_t)chunk_count;
2749 }
2750
2751 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2752 {
2753     AVStream *st;
2754     MOVStreamContext *sc;
2755     unsigned i, entries;
2756
2757     if (c->fc->nb_streams < 1)
2758         return 0;
2759     st = c->fc->streams[c->fc->nb_streams-1];
2760     sc = st->priv_data;
2761
2762     avio_rb32(pb); // version + flags
2763
2764     entries = avio_rb32(pb);
2765     if (sc->stps_data)
2766         av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2767     av_free(sc->stps_data);
2768     sc->stps_count = 0;
2769     sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2770     if (!sc->stps_data)
2771         return AVERROR(ENOMEM);
2772
2773     for (i = 0; i < entries && !pb->eof_reached; i++) {
2774         sc->stps_data[i] = avio_rb32(pb);
2775     }
2776
2777     sc->stps_count = i;
2778
2779     if (pb->eof_reached) {
2780         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
2781         return AVERROR_EOF;
2782     }
2783
2784     return 0;
2785 }
2786
2787 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2788 {
2789     AVStream *st;
2790     MOVStreamContext *sc;
2791     unsigned int i, entries;
2792
2793     if (c->fc->nb_streams < 1)
2794         return 0;
2795     st = c->fc->streams[c->fc->nb_streams-1];
2796     sc = st->priv_data;
2797
2798     avio_r8(pb); /* version */
2799     avio_rb24(pb); /* flags */
2800
2801     entries = avio_rb32(pb);
2802
2803     av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
2804
2805     if (!entries) {
2806         sc->keyframe_absent = 1;
2807         if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2808             st->need_parsing = AVSTREAM_PARSE_HEADERS;
2809         return 0;
2810     }
2811     if (sc->keyframes)
2812         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2813     if (entries >= UINT_MAX / sizeof(int))
2814         return AVERROR_INVALIDDATA;
2815     av_freep(&sc->keyframes);
2816     sc->keyframe_count = 0;
2817     sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2818     if (!sc->keyframes)
2819         return AVERROR(ENOMEM);
2820
2821     for (i = 0; i < entries && !pb->eof_reached; i++) {
2822         sc->keyframes[i] = avio_rb32(pb);
2823     }
2824
2825     sc->keyframe_count = i;
2826
2827     if (pb->eof_reached) {
2828         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
2829         return AVERROR_EOF;
2830     }
2831
2832     return 0;
2833 }
2834
2835 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2836 {
2837     AVStream *st;
2838     MOVStreamContext *sc;
2839     unsigned int i, entries, sample_size, field_size, num_bytes;
2840     GetBitContext gb;
2841     unsigned char* buf;
2842     int ret;
2843
2844     if (c->fc->nb_streams < 1)
2845         return 0;
2846     st = c->fc->streams[c->fc->nb_streams-1];
2847     sc = st->priv_data;
2848
2849     avio_r8(pb); /* version */
2850     avio_rb24(pb); /* flags */
2851
2852     if (atom.type == MKTAG('s','t','s','z')) {
2853         sample_size = avio_rb32(pb);
2854         if (!sc->sample_size) /* do not overwrite value computed in stsd */
2855             sc->sample_size = sample_size;
2856         sc->stsz_sample_size = sample_size;
2857         field_size = 32;
2858     } else {
2859         sample_size = 0;
2860         avio_rb24(pb); /* reserved */
2861         field_size = avio_r8(pb);
2862     }
2863     entries = avio_rb32(pb);
2864
2865     av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
2866
2867     sc->sample_count = entries;
2868     if (sample_size)
2869         return 0;
2870
2871     if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2872         av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
2873         return AVERROR_INVALIDDATA;
2874     }
2875
2876     if (!entries)
2877         return 0;
2878     if (entries >= (UINT_MAX - 4) / field_size)
2879         return AVERROR_INVALIDDATA;
2880     if (sc->sample_sizes)
2881         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2882     av_free(sc->sample_sizes);
2883     sc->sample_count = 0;
2884     sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2885     if (!sc->sample_sizes)
2886         return AVERROR(ENOMEM);
2887
2888     num_bytes = (entries*field_size+4)>>3;
2889
2890     buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2891     if (!buf) {
2892         av_freep(&sc->sample_sizes);
2893         return AVERROR(ENOMEM);
2894     }
2895
2896     ret = ffio_read_size(pb, buf, num_bytes);
2897     if (ret < 0) {
2898         av_freep(&sc->sample_sizes);
2899         av_free(buf);
2900         av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
2901         return 0;
2902     }
2903
2904     init_get_bits(&gb, buf, 8*num_bytes);
2905
2906     for (i = 0; i < entries && !pb->eof_reached; i++) {
2907         sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2908         if (sc->sample_sizes[i] < 0) {
2909             av_free(buf);
2910             av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
2911             return AVERROR_INVALIDDATA;
2912         }
2913         sc->data_size += sc->sample_sizes[i];
2914     }
2915
2916     sc->sample_count = i;
2917
2918     av_free(buf);
2919
2920     if (pb->eof_reached) {
2921         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSZ atom\n");
2922         return AVERROR_EOF;
2923     }
2924
2925     return 0;
2926 }
2927
2928 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2929 {
2930     AVStream *st;
2931     MOVStreamContext *sc;
2932     unsigned int i, entries, alloc_size = 0;
2933     int64_t duration = 0;
2934     int64_t total_sample_count = 0;
2935
2936     if (c->fc->nb_streams < 1)
2937         return 0;
2938     st = c->fc->streams[c->fc->nb_streams-1];
2939     sc = st->priv_data;
2940
2941     avio_r8(pb); /* version */
2942     avio_rb24(pb); /* flags */
2943     entries = avio_rb32(pb);
2944
2945     av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
2946             c->fc->nb_streams-1, entries);
2947
2948     if (sc->stts_data)
2949         av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2950     av_freep(&sc->stts_data);
2951     sc->stts_count = 0;
2952     if (entries >= INT_MAX / sizeof(*sc->stts_data))
2953         return AVERROR(ENOMEM);
2954
2955     for (i = 0; i < entries && !pb->eof_reached; i++) {
2956         int sample_duration;
2957         unsigned int sample_count;
2958         unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
2959         MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
2960                                              min_entries * sizeof(*sc->stts_data));
2961         if (!stts_data) {
2962             av_freep(&sc->stts_data);
2963             sc->stts_count = 0;
2964             return AVERROR(ENOMEM);
2965         }
2966         sc->stts_count = min_entries;
2967         sc->stts_data = stts_data;
2968
2969         sample_count    = avio_rb32(pb);
2970         sample_duration = avio_rb32(pb);
2971
2972         sc->stts_data[i].count= sample_count;
2973         sc->stts_data[i].duration= sample_duration;
2974
2975         av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2976                 sample_count, sample_duration);
2977
2978         duration+=(int64_t)sample_duration*(uint64_t)sample_count;
2979         total_sample_count+=sample_count;
2980     }
2981
2982     sc->stts_count = i;
2983
2984     if (duration > 0 &&
2985         duration <= INT64_MAX - sc->duration_for_fps &&
2986         total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
2987         sc->duration_for_fps  += duration;
2988         sc->nb_frames_for_fps += total_sample_count;
2989     }
2990
2991     if (pb->eof_reached) {
2992         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
2993         return AVERROR_EOF;
2994     }
2995
2996     st->nb_frames= total_sample_count;
2997     if (duration)
2998         st->duration= FFMIN(st->duration, duration);
2999     sc->track_end = duration;
3000     return 0;
3001 }
3002
3003 static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3004 {
3005     AVStream *st;
3006     MOVStreamContext *sc;
3007     int64_t i, entries;
3008
3009     if (c->fc->nb_streams < 1)
3010         return 0;
3011     st = c->fc->streams[c->fc->nb_streams - 1];
3012     sc = st->priv_data;
3013
3014     avio_r8(pb); /* version */
3015     avio_rb24(pb); /* flags */
3016     entries = atom.size - 4;
3017
3018     av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
3019            c->fc->nb_streams - 1, entries);
3020
3021     if (sc->sdtp_data)
3022         av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
3023     av_freep(&sc->sdtp_data);
3024     sc->sdtp_count = 0;
3025
3026     sc->sdtp_data = av_mallocz(entries);
3027     if (!sc->sdtp_data)
3028         return AVERROR(ENOMEM);
3029
3030     for (i = 0; i < entries && !pb->eof_reached; i++)
3031         sc->sdtp_data[i] = avio_r8(pb);
3032     sc->sdtp_count = i;
3033
3034     return 0;
3035 }
3036
3037 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3038 {
3039     if (duration < 0) {
3040         if (duration == INT_MIN) {
3041             av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
3042             duration++;
3043         }
3044         sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3045     }
3046 }
3047
3048 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3049 {
3050     AVStream *st;
3051     MOVStreamContext *sc;
3052     unsigned int i, entries, ctts_count = 0;
3053
3054     if (c->fc->nb_streams < 1)
3055         return 0;
3056     st = c->fc->streams[c->fc->nb_streams-1];
3057     sc = st->priv_data;
3058
3059     avio_r8(pb); /* version */
3060     avio_rb24(pb); /* flags */
3061     entries = avio_rb32(pb);
3062
3063     av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
3064
3065     if (!entries)
3066         return 0;
3067     if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3068         return AVERROR_INVALIDDATA;
3069     av_freep(&sc->ctts_data);
3070     sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3071     if (!sc->ctts_data)
3072         return AVERROR(ENOMEM);
3073
3074     for (i = 0; i < entries && !pb->eof_reached; i++) {
3075         int count    = avio_rb32(pb);
3076         int duration = avio_rb32(pb);
3077
3078         if (count <= 0) {
3079             av_log(c->fc, AV_LOG_TRACE,
3080                    "ignoring CTTS entry with count=%d duration=%d\n",
3081                    count, duration);
3082             continue;
3083         }
3084
3085         add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
3086                        count, duration);
3087
3088         av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
3089                 count, duration);
3090
3091         if (FFNABS(duration) < -(1<<28) && i+2<entries) {
3092             av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
3093             av_freep(&sc->ctts_data);
3094             sc->ctts_count = 0;
3095             return 0;
3096         }
3097
3098         if (i+2<entries)
3099             mov_update_dts_shift(sc, duration, c->fc);
3100     }
3101
3102     sc->ctts_count = ctts_count;
3103
3104     if (pb->eof_reached) {
3105         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
3106         return AVERROR_EOF;
3107     }
3108
3109     av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3110
3111     return 0;
3112 }
3113
3114 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3115 {
3116     AVStream *st;
3117     MOVStreamContext *sc;
3118     unsigned int i, entries;
3119     uint8_t version;
3120     uint32_t grouping_type;
3121
3122     if (c->fc->nb_streams < 1)
3123         return 0;
3124     st = c->fc->streams[c->fc->nb_streams-1];
3125     sc = st->priv_data;
3126
3127     version = avio_r8(pb); /* version */
3128     avio_rb24(pb); /* flags */
3129     grouping_type = avio_rl32(pb);
3130     if (grouping_type != MKTAG( 'r','a','p',' '))
3131         return 0; /* only support 'rap ' grouping */
3132     if (version == 1)
3133         avio_rb32(pb); /* grouping_type_parameter */
3134
3135     entries = avio_rb32(pb);
3136     if (!entries)
3137         return 0;
3138     if (sc->rap_group)
3139         av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
3140     av_free(sc->rap_group);
3141     sc->rap_group_count = 0;
3142     sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
3143     if (!sc->rap_group)
3144         return AVERROR(ENOMEM);
3145
3146     for (i = 0; i < entries && !pb->eof_reached; i++) {
3147         sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
3148         sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
3149     }
3150
3151     sc->rap_group_count = i;
3152
3153     if (pb->eof_reached) {
3154         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
3155         return AVERROR_EOF;
3156     }
3157
3158     return 0;
3159 }
3160
3161 /**
3162  * Get ith edit list entry (media time, duration).
3163  */
3164 static int get_edit_list_entry(MOVContext *mov,
3165                                const MOVStreamContext *msc,
3166                                unsigned int edit_list_index,
3167                                int64_t *edit_list_media_time,
3168                                int64_t *edit_list_duration,
3169                                int64_t global_timescale)
3170 {
3171     if (edit_list_index == msc->elst_count) {
3172         return 0;
3173     }
3174     *edit_list_media_time = msc->elst_data[edit_list_index].time;
3175     *edit_list_duration = msc->elst_data[edit_list_index].duration;
3176
3177     /* duration is in global timescale units;convert to msc timescale */
3178     if (global_timescale == 0) {
3179       avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
3180       return 0;
3181     }
3182     *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
3183                                      global_timescale);
3184     return 1;
3185 }
3186
3187 /**
3188  * Find the closest previous frame to the timestamp_pts, in e_old index
3189  * entries. Searching for just any frame / just key frames can be controlled by
3190  * last argument 'flag'.
3191  * Note that if ctts_data is not NULL, we will always search for a key frame
3192  * irrespective of the value of 'flag'. If we don't find any keyframe, we will
3193  * return the first frame of the video.
3194  *
3195  * Here the timestamp_pts is considered to be a presentation timestamp and
3196  * the timestamp of index entries are considered to be decoding timestamps.
3197  *
3198  * Returns 0 if successful in finding a frame, else returns -1.
3199  * Places the found index corresponding output arg.
3200  *
3201  * If ctts_old is not NULL, then refines the searched entry by searching
3202  * backwards from the found timestamp, to find the frame with correct PTS.
3203  *
3204  * Places the found ctts_index and ctts_sample in corresponding output args.
3205  */
3206 static int find_prev_closest_index(AVStream *st,
3207                                    AVIndexEntry *e_old,
3208                                    int nb_old,
3209                                    MOVStts* ctts_data,
3210                                    int64_t ctts_count,
3211                                    int64_t timestamp_pts,
3212                                    int flag,
3213                                    int64_t* index,
3214                                    int64_t* ctts_index,
3215                                    int64_t* ctts_sample)
3216 {
3217     MOVStreamContext *msc = st->priv_data;
3218     AVIndexEntry *e_keep = st->internal->index_entries;
3219     int nb_keep = st->internal->nb_index_entries;
3220     int64_t i = 0;
3221     int64_t index_ctts_count;
3222
3223     av_assert0(index);
3224
3225     // If dts_shift > 0, then all the index timestamps will have to be offset by
3226     // at least dts_shift amount to obtain PTS.
3227     // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
3228     if (msc->dts_shift > 0) {
3229         timestamp_pts -= msc->dts_shift;
3230     }
3231
3232     st->internal->index_entries = e_old;
3233     st->internal->nb_index_entries = nb_old;
3234     *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
3235
3236     // Keep going backwards in the index entries until the timestamp is the same.
3237     if (*index >= 0) {
3238         for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
3239              i--) {
3240             if ((flag & AVSEEK_FLAG_ANY) ||
3241                 (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
3242                 *index = i - 1;
3243             }
3244         }
3245     }
3246
3247     // If we have CTTS then refine the search, by searching backwards over PTS
3248     // computed by adding corresponding CTTS durations to index timestamps.
3249     if (ctts_data && *index >= 0) {
3250         av_assert0(ctts_index);
3251         av_assert0(ctts_sample);
3252         // Find out the ctts_index for the found frame.
3253         *ctts_index = 0;
3254         *ctts_sample = 0;
3255         for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
3256             if (*ctts_index < ctts_count) {
3257                 (*ctts_sample)++;
3258                 if (ctts_data[*ctts_index].count == *ctts_sample) {
3259                     (*ctts_index)++;
3260                     *ctts_sample = 0;
3261                 }
3262             }
3263         }
3264
3265         while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
3266             // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
3267             // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
3268             // compensated by dts_shift above.
3269             if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
3270                 (e_old[*index].flags & AVINDEX_KEYFRAME)) {
3271                 break;
3272             }
3273
3274             (*index)--;
3275             if (*ctts_sample == 0) {
3276                 (*ctts_index)--;
3277                 if (*ctts_index >= 0)
3278                   *ctts_sample = ctts_data[*ctts_index].count - 1;
3279             } else {
3280                 (*ctts_sample)--;
3281             }
3282         }
3283     }
3284
3285     /* restore AVStream state*/
3286     st->internal->index_entries = e_keep;
3287     st->internal->nb_index_entries = nb_keep;
3288     return *index >= 0 ? 0 : -1;
3289 }
3290
3291 /**
3292  * Add index entry with the given values, to the end of st->internal->index_entries.
3293  * Returns the new size st->internal->index_entries if successful, else returns -1.
3294  *
3295  * This function is similar to ff_add_index_entry in libavformat/utils.c
3296  * except that here we are always unconditionally adding an index entry to
3297  * the end, instead of searching the entries list and skipping the add if
3298  * there is an existing entry with the same timestamp.
3299  * This is needed because the mov_fix_index calls this func with the same
3300  * unincremented timestamp for successive discarded frames.
3301  */
3302 static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
3303                                int size, int distance, int flags)
3304 {
3305     AVIndexEntry *entries, *ie;
3306     int64_t index = -1;
3307     const size_t min_size_needed = (st->internal->nb_index_entries + 1) * sizeof(AVIndexEntry);
3308
3309     // Double the allocation each time, to lower memory fragmentation.
3310     // Another difference from ff_add_index_entry function.
3311     const size_t requested_size =
3312         min_size_needed > st->internal->index_entries_allocated_size ?
3313         FFMAX(min_size_needed, 2 * st->internal->index_entries_allocated_size) :
3314         min_size_needed;
3315
3316     if (st->internal->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
3317         return -1;
3318
3319     entries = av_fast_realloc(st->internal->index_entries,
3320                               &st->internal->index_entries_allocated_size,
3321                               requested_size);
3322     if (!entries)
3323         return -1;
3324
3325     st->internal->index_entries= entries;
3326
3327     index= st->internal->nb_index_entries++;
3328     ie= &entries[index];
3329
3330     ie->pos = pos;
3331     ie->timestamp = timestamp;
3332     ie->min_distance= distance;
3333     ie->size= size;
3334     ie->flags = flags;
3335     return index;
3336 }
3337
3338 /**
3339  * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
3340  * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
3341  */
3342 static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
3343                                        int64_t* frame_duration_buffer,
3344                                        int frame_duration_buffer_size) {
3345     int i = 0;
3346     av_assert0(end_index >= 0 && end_index <= st->internal->nb_index_entries);
3347     for (i = 0; i < frame_duration_buffer_size; i++) {
3348         end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
3349         st->internal->index_entries[end_index - 1 - i].timestamp = end_ts;
3350     }
3351 }
3352
3353 /**
3354  * Append a new ctts entry to ctts_data.
3355  * Returns the new ctts_count if successful, else returns -1.
3356  */
3357 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
3358                               int count, int duration)
3359 {
3360     MOVStts *ctts_buf_new;
3361     const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
3362     const size_t requested_size =
3363         min_size_needed > *allocated_size ?
3364         FFMAX(min_size_needed, 2 * (*allocated_size)) :
3365         min_size_needed;
3366
3367     if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1)
3368         return -1;
3369
3370     ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
3371
3372     if (!ctts_buf_new)
3373         return -1;
3374
3375     *ctts_data = ctts_buf_new;
3376
3377     ctts_buf_new[*ctts_count].count = count;
3378     ctts_buf_new[*ctts_count].duration = duration;
3379
3380     *ctts_count = (*ctts_count) + 1;
3381     return *ctts_count;
3382 }
3383
3384 #define MAX_REORDER_DELAY 16
3385 static void mov_estimate_video_delay(MOVContext *c, AVStream* st)
3386 {
3387     MOVStreamContext *msc = st->priv_data;
3388     int ind;
3389     int ctts_ind = 0;
3390     int ctts_sample = 0;
3391     int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
3392     int buf_start = 0;
3393     int j, r, num_swaps;
3394
3395     for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
3396         pts_buf[j] = INT64_MIN;
3397
3398     if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
3399         st->codecpar->codec_id == AV_CODEC_ID_H264) {
3400         st->codecpar->video_delay = 0;
3401         for (ind = 0; ind < st->internal->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
3402             // Point j to the last elem of the buffer and insert the current pts there.
3403             j = buf_start;
3404             buf_start = (buf_start + 1);
3405             if (buf_start == MAX_REORDER_DELAY + 1)
3406                 buf_start = 0;
3407
3408             pts_buf[j] = st->internal->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
3409
3410             // The timestamps that are already in the sorted buffer, and are greater than the
3411             // current pts, are exactly the timestamps that need to be buffered to output PTS
3412             // in correct sorted order.
3413             // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
3414             // can be computed as the maximum no. of swaps any particular timestamp needs to
3415             // go through, to keep this buffer in sorted order.
3416             num_swaps = 0;
3417             while (j != buf_start) {
3418                 r = j - 1;
3419                 if (r < 0) r = MAX_REORDER_DELAY;
3420                 if (pts_buf[j] < pts_buf[r]) {
3421                     FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
3422                     ++num_swaps;
3423                 } else {
3424                     break;
3425                 }
3426                 j = r;
3427             }
3428             st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
3429
3430             ctts_sample++;
3431             if (ctts_sample == msc->ctts_data[ctts_ind].count) {
3432                 ctts_ind++;
3433                 ctts_sample = 0;
3434             }
3435         }
3436         av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
3437                st->codecpar->video_delay, st->index);
3438     }
3439 }
3440
3441 static void mov_current_sample_inc(MOVStreamContext *sc)
3442 {
3443     sc->current_sample++;
3444     sc->current_index++;
3445     if (sc->index_ranges &&
3446         sc->current_index >= sc->current_index_range->end &&
3447         sc->current_index_range->end) {
3448         sc->current_index_range++;
3449         sc->current_index = sc->current_index_range->start;
3450     }
3451 }
3452
3453 static void mov_current_sample_dec(MOVStreamContext *sc)
3454 {
3455     sc->current_sample--;
3456     sc->current_index--;
3457     if (sc->index_ranges &&
3458         sc->current_index < sc->current_index_range->start &&
3459         sc->current_index_range > sc->index_ranges) {
3460         sc->current_index_range--;
3461         sc->current_index = sc->current_index_range->end - 1;
3462     }
3463 }
3464
3465 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
3466 {
3467     int64_t range_size;
3468
3469     sc->current_sample = current_sample;
3470     sc->current_index = current_sample;
3471     if (!sc->index_ranges) {
3472         return;
3473     }
3474
3475     for (sc->current_index_range = sc->index_ranges;
3476         sc->current_index_range->end;
3477         sc->current_index_range++) {
3478         range_size = sc->current_index_range->end - sc->current_index_range->start;
3479         if (range_size > current_sample) {
3480             sc->current_index = sc->current_index_range->start + current_sample;
3481             break;
3482         }
3483         current_sample -= range_size;
3484     }
3485 }
3486
3487 /**
3488  * Fix st->internal->index_entries, so that it contains only the entries (and the entries
3489  * which are needed to decode them) that fall in the edit list time ranges.
3490  * Also fixes the timestamps of the index entries to match the timeline
3491  * specified the edit lists.
3492  */
3493 static void mov_fix_index(MOVContext *mov, AVStream *st)
3494 {
3495     MOVStreamContext *msc = st->priv_data;
3496     AVIndexEntry *e_old = st->internal->index_entries;
3497     int nb_old = st->internal->nb_index_entries;
3498     const AVIndexEntry *e_old_end = e_old + nb_old;
3499     const AVIndexEntry *current = NULL;
3500     MOVStts *ctts_data_old = msc->ctts_data;
3501     int64_t ctts_index_old = 0;
3502     int64_t ctts_sample_old = 0;
3503     int64_t ctts_count_old = msc->ctts_count;
3504     int64_t edit_list_media_time = 0;
3505     int64_t edit_list_duration = 0;
3506     int64_t frame_duration = 0;
3507     int64_t edit_list_dts_counter = 0;
3508     int64_t edit_list_dts_entry_end = 0;
3509     int64_t edit_list_start_ctts_sample = 0;
3510     int64_t curr_cts;
3511     int64_t curr_ctts = 0;
3512     int64_t empty_edits_sum_duration = 0;
3513     int64_t edit_list_index = 0;
3514     int64_t index;
3515     int flags;
3516     int64_t start_dts = 0;
3517     int64_t edit_list_start_encountered = 0;
3518     int64_t search_timestamp = 0;
3519     int64_t* frame_duration_buffer = NULL;
3520     int num_discarded_begin = 0;
3521     int first_non_zero_audio_edit = -1;
3522     int packet_skip_samples = 0;
3523     MOVIndexRange *current_index_range;
3524     int i;
3525     int found_keyframe_after_edit = 0;
3526     int found_non_empty_edit = 0;
3527
3528     if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
3529         return;
3530     }
3531
3532     // allocate the index ranges array
3533     msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
3534     if (!msc->index_ranges) {
3535         av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
3536         return;
3537     }
3538     msc->current_index_range = msc->index_ranges;
3539     current_index_range = msc->index_ranges - 1;
3540
3541     // Clean AVStream from traces of old index
3542     st->internal->index_entries = NULL;
3543     st->internal->index_entries_allocated_size = 0;
3544     st->internal->nb_index_entries = 0;
3545
3546     // Clean ctts fields of MOVStreamContext
3547     msc->ctts_data = NULL;
3548     msc->ctts_count = 0;
3549     msc->ctts_index = 0;
3550     msc->ctts_sample = 0;
3551     msc->ctts_allocated_size = 0;
3552
3553     // Reinitialize min_corrected_pts so that it can be computed again.
3554     msc->min_corrected_pts = -1;
3555
3556     // If the dts_shift is positive (in case of negative ctts values in mov),
3557     // then negate the DTS by dts_shift
3558     if (msc->dts_shift > 0) {
3559         edit_list_dts_entry_end -= msc->dts_shift;
3560         av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
3561     }
3562
3563     start_dts = edit_list_dts_entry_end;
3564
3565     while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
3566                                &edit_list_duration, mov->time_scale)) {
3567         av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
3568                st->index, edit_list_index, edit_list_media_time, edit_list_duration);
3569         edit_list_index++;
3570         edit_list_dts_counter = edit_list_dts_entry_end;
3571         edit_list_dts_entry_end += edit_list_duration;
3572         num_discarded_begin = 0;
3573         if (!found_non_empty_edit && edit_list_media_time == -1) {
3574             empty_edits_sum_duration += edit_list_duration;
3575             continue;
3576         }
3577         found_non_empty_edit = 1;
3578
3579         // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
3580         // according to the edit list below.
3581         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3582             if (first_non_zero_audio_edit < 0) {
3583                 first_non_zero_audio_edit = 1;
3584             } else {
3585                 first_non_zero_audio_edit = 0;
3586             }
3587
3588             if (first_non_zero_audio_edit > 0)
3589                 st->internal->skip_samples = msc->start_pad = 0;
3590         }
3591
3592         // While reordering frame index according to edit list we must handle properly
3593         // the scenario when edit list entry starts from none key frame.
3594         // We find closest previous key frame and preserve it and consequent frames in index.
3595         // All frames which are outside edit list entry time boundaries will be dropped after decoding.
3596         search_timestamp = edit_list_media_time;
3597         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3598             // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
3599             // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
3600             // edit_list_media_time to cover the decoder delay.
3601             search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
3602         }
3603
3604         if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, 0,
3605                                     &index, &ctts_index_old, &ctts_sample_old) < 0) {
3606             av_log(mov->fc, AV_LOG_WARNING,
3607                    "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
3608                    st->index, edit_list_index, search_timestamp);
3609             if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
3610                                         &index, &ctts_index_old, &ctts_sample_old) < 0) {
3611                 av_log(mov->fc, AV_LOG_WARNING,
3612                        "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
3613                        st->index, edit_list_index, search_timestamp);
3614                 index = 0;
3615                 ctts_index_old = 0;
3616                 ctts_sample_old = 0;
3617             }
3618         }
3619         current = e_old + index;
3620         edit_list_start_ctts_sample = ctts_sample_old;
3621
3622         // Iterate over index and arrange it according to edit list
3623         edit_list_start_encountered = 0;
3624         found_keyframe_after_edit = 0;
3625         for (; current < e_old_end; current++, index++) {
3626             // check  if frame outside edit list mark it for discard
3627             frame_duration = (current + 1 <  e_old_end) ?
3628                              ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
3629
3630             flags = current->flags;
3631
3632             // frames (pts) before or after edit list
3633             curr_cts = current->timestamp + msc->dts_shift;
3634             curr_ctts = 0;
3635
3636             if (ctts_data_old && ctts_index_old < ctts_count_old) {
3637                 curr_ctts = ctts_data_old[ctts_index_old].duration;
3638                 av_log(mov->fc, AV_LOG_DEBUG, "stts: %"PRId64" ctts: %"PRId64", ctts_index: %"PRId64", ctts_count: %"PRId64"\n",
3639                        curr_cts, curr_ctts, ctts_index_old, ctts_count_old);
3640                 curr_cts += curr_ctts;
3641                 ctts_sample_old++;
3642                 if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
3643                     if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3644                                        &msc->ctts_allocated_size,
3645                                        ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3646                                        ctts_data_old[ctts_index_old].duration) == -1) {
3647                         av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3648                                ctts_index_old,
3649                                ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3650                                ctts_data_old[ctts_index_old].duration);
3651                         break;
3652                     }
3653                     ctts_index_old++;
3654                     ctts_sample_old = 0;
3655                     edit_list_start_ctts_sample = 0;
3656                 }
3657             }
3658
3659             if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
3660                 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
3661                     curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
3662                     first_non_zero_audio_edit > 0) {
3663                     packet_skip_samples = edit_list_media_time - curr_cts;
3664                     st->internal->skip_samples += packet_skip_samples;
3665
3666                     // Shift the index entry timestamp by packet_skip_samples to be correct.
3667                     edit_list_dts_counter -= packet_skip_samples;
3668                     if (edit_list_start_encountered == 0)  {
3669                         edit_list_start_encountered = 1;
3670                         // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
3671                         // discarded packets.
3672                         if (frame_duration_buffer) {
3673                             fix_index_entry_timestamps(st, st->internal->nb_index_entries, edit_list_dts_counter,
3674                                                        frame_duration_buffer, num_discarded_begin);
3675                             av_freep(&frame_duration_buffer);
3676                         }
3677                     }
3678
3679                     av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
3680                 } else {
3681                     flags |= AVINDEX_DISCARD_FRAME;
3682                     av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
3683
3684                     if (edit_list_start_encountered == 0) {
3685                         num_discarded_begin++;
3686                         frame_duration_buffer = av_realloc(frame_duration_buffer,
3687                                                            num_discarded_begin * sizeof(int64_t));
3688                         if (!frame_duration_buffer) {
3689                             av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
3690                             break;
3691                         }
3692                         frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
3693
3694                         // Increment skip_samples for the first non-zero audio edit list
3695                         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3696                             first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
3697                             st->internal->skip_samples += frame_duration;
3698                         }
3699                     }
3700                 }
3701             } else {
3702                 if (msc->min_corrected_pts < 0) {
3703                     msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
3704                 } else {
3705                     msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
3706                 }
3707                 if (edit_list_start_encountered == 0) {
3708                     edit_list_start_encountered = 1;
3709                     // Make timestamps strictly monotonically increasing by rewriting timestamps for
3710                     // discarded packets.
3711                     if (frame_duration_buffer) {
3712                         fix_index_entry_timestamps(st, st->internal->nb_index_entries, edit_list_dts_counter,
3713                                                    frame_duration_buffer, num_discarded_begin);
3714                         av_freep(&frame_duration_buffer);
3715                     }
3716                 }
3717             }
3718
3719             if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
3720                                 current->min_distance, flags) == -1) {
3721                 av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
3722                 break;
3723             }
3724
3725             // Update the index ranges array
3726             if (current_index_range < msc->index_ranges || index != current_index_range->end) {
3727                 current_index_range++;
3728                 current_index_range->start = index;
3729             }
3730             current_index_range->end = index + 1;
3731
3732             // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
3733             if (edit_list_start_encountered > 0) {
3734                 edit_list_dts_counter = edit_list_dts_counter + frame_duration;
3735             }
3736
3737             // Break when found first key frame after edit entry completion
3738             if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
3739                 ((flags & AVINDEX_KEYFRAME) || ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)))) {
3740                 if (ctts_data_old) {
3741                     // If we have CTTS and this is the first keyframe after edit elist,
3742                     // wait for one more, because there might be trailing B-frames after this I-frame
3743                     // that do belong to the edit.
3744                     if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
3745                         found_keyframe_after_edit = 1;
3746                         continue;
3747                     }
3748                     if (ctts_sample_old != 0) {
3749                         if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3750                                            &msc->ctts_allocated_size,
3751                                            ctts_sample_old - edit_list_start_ctts_sample,
3752                                            ctts_data_old[ctts_index_old].duration) == -1) {
3753                             av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3754                                    ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
3755                                    ctts_data_old[ctts_index_old].duration);
3756                             break;
3757                         }
3758                     }
3759                 }
3760                 break;
3761             }
3762         }
3763     }
3764     // If there are empty edits, then msc->min_corrected_pts might be positive
3765     // intentionally. So we subtract the sum duration of emtpy edits here.
3766     msc->min_corrected_pts -= empty_edits_sum_duration;
3767
3768     // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
3769     // dts by that amount to make the first pts zero.
3770     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3771         if (msc->min_corrected_pts > 0) {
3772             av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
3773             for (i = 0; i < st->internal->nb_index_entries; ++i) {
3774                 st->internal->index_entries[i].timestamp -= msc->min_corrected_pts;
3775             }
3776         }
3777     }
3778     // Start time should be equal to zero or the duration of any empty edits.
3779     st->start_time = empty_edits_sum_duration;
3780
3781     // Update av stream length, if it ends up shorter than the track's media duration
3782     st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
3783     msc->start_pad = st->internal->skip_samples;
3784
3785     // Free the old index and the old CTTS structures
3786     av_free(e_old);
3787     av_free(ctts_data_old);
3788     av_freep(&frame_duration_buffer);
3789
3790     // Null terminate the index ranges array
3791     current_index_range++;
3792     current_index_range->start = 0;
3793     current_index_range->end = 0;
3794     msc->current_index = msc->index_ranges[0].start;
3795 }
3796
3797 static void mov_build_index(MOVContext *mov, AVStream *st)
3798 {
3799     MOVStreamContext *sc = st->priv_data;
3800     int64_t current_offset;
3801     int64_t current_dts = 0;
3802     unsigned int stts_index = 0;
3803     unsigned int stsc_index = 0;
3804     unsigned int stss_index = 0;
3805     unsigned int stps_index = 0;
3806     unsigned int i, j;
3807     uint64_t stream_size = 0;
3808     MOVStts *ctts_data_old = sc->ctts_data;
3809     unsigned int ctts_count_old = sc->ctts_count;
3810
3811     if (sc->elst_count) {
3812         int i, edit_start_index = 0, multiple_edits = 0;
3813         int64_t empty_duration = 0; // empty duration of the first edit list entry
3814         int64_t start_time = 0; // start time of the media
3815
3816         for (i = 0; i < sc->elst_count; i++) {
3817             const MOVElst *e = &sc->elst_data[i];
3818             if (i == 0 && e->time == -1) {
3819                 /* if empty, the first entry is the start time of the stream
3820                  * relative to the presentation itself */
3821                 empty_duration = e->duration;
3822                 edit_start_index = 1;
3823             } else if (i == edit_start_index && e->time >= 0) {
3824                 start_time = e->time;
3825             } else {
3826                 multiple_edits = 1;
3827             }
3828         }
3829
3830         if (multiple_edits && !mov->advanced_editlist)
3831             av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
3832                    "Use -advanced_editlist to correctly decode otherwise "
3833                    "a/v desync might occur\n");
3834
3835         /* adjust first dts according to edit list */
3836         if ((empty_duration || start_time) && mov->time_scale > 0) {
3837             if (empty_duration)
3838                 empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
3839             sc->time_offset = start_time - empty_duration;
3840             sc->min_corrected_pts = start_time;
3841             if (!mov->advanced_editlist)
3842                 current_dts = -sc->time_offset;
3843         }
3844
3845         if (!multiple_edits && !mov->advanced_editlist &&
3846             st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
3847             sc->start_pad = start_time;
3848     }
3849
3850     /* only use old uncompressed audio chunk demuxing when stts specifies it */
3851     if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3852           sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
3853         unsigned int current_sample = 0;
3854         unsigned int stts_sample = 0;
3855         unsigned int sample_size;
3856         unsigned int distance = 0;
3857         unsigned int rap_group_index = 0;
3858         unsigned int rap_group_sample = 0;
3859         int64_t last_dts = 0;
3860         int64_t dts_correction = 0;
3861         int rap_group_present = sc->rap_group_count && sc->rap_group;
3862         int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
3863
3864         current_dts -= sc->dts_shift;
3865         last_dts     = current_dts;
3866
3867         if (!sc->sample_count || st->internal->nb_index_entries)
3868             return;
3869         if (sc->sample_count >= UINT_MAX / sizeof(*st->internal->index_entries) - st->internal->nb_index_entries)
3870             return;
3871         if (av_reallocp_array(&st->internal->index_entries,
3872                               st->internal->nb_index_entries + sc->sample_count,
3873                               sizeof(*st->internal->index_entries)) < 0) {
3874             st->internal->nb_index_entries = 0;
3875             return;
3876         }
3877         st->internal->index_entries_allocated_size = (st->internal->nb_index_entries + sc->sample_count) * sizeof(*st->internal->index_entries);
3878
3879         if (ctts_data_old) {
3880             // Expand ctts entries such that we have a 1-1 mapping with samples
3881             if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
3882                 return;
3883             sc->ctts_count = 0;
3884             sc->ctts_allocated_size = 0;
3885             sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size,
3886                                     sc->sample_count * sizeof(*sc->ctts_data));
3887             if (!sc->ctts_data) {
3888                 av_free(ctts_data_old);
3889                 return;
3890             }
3891
3892             memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
3893
3894             for (i = 0; i < ctts_count_old &&
3895                         sc->ctts_count < sc->sample_count; i++)
3896                 for (j = 0; j < ctts_data_old[i].count &&
3897                             sc->ctts_count < sc->sample_count; j++)
3898                     add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
3899                                    &sc->ctts_allocated_size, 1,
3900                                    ctts_data_old[i].duration);
3901             av_free(ctts_data_old);
3902         }
3903
3904         for (i = 0; i < sc->chunk_count; i++) {
3905             int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
3906             current_offset = sc->chunk_offsets[i];
3907             while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
3908                 i + 1 == sc->stsc_data[stsc_index + 1].first)
3909                 stsc_index++;
3910
3911             if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
3912                 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
3913                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
3914                 sc->stsz_sample_size = sc->sample_size;
3915             }
3916             if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
3917                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
3918                 sc->stsz_sample_size = sc->sample_size;
3919             }
3920
3921             for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
3922                 int keyframe = 0;
3923                 if (current_sample >= sc->sample_count) {
3924                     av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
3925                     return;
3926                 }
3927
3928                 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
3929                     keyframe = 1;
3930                     if (stss_index + 1 < sc->keyframe_count)
3931                         stss_index++;
3932                 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
3933                     keyframe = 1;
3934                     if (stps_index + 1 < sc->stps_count)
3935                         stps_index++;
3936                 }
3937                 if (rap_group_present && rap_group_index < sc->rap_group_count) {
3938                     if (sc->rap_group[rap_group_index].index > 0)
3939                         keyframe = 1;
3940                     if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
3941                         rap_group_sample = 0;
3942                         rap_group_index++;
3943                     }
3944                 }
3945                 if (sc->keyframe_absent
3946                     && !sc->stps_count
3947                     && !rap_group_present
3948                     && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
3949                      keyframe = 1;
3950                 if (keyframe)
3951                     distance = 0;
3952                 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
3953                 if (sc->pseudo_stream_id == -1 ||
3954                    sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
3955                     AVIndexEntry *e;
3956                     if (sample_size > 0x3FFFFFFF) {
3957                         av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
3958                         return;
3959                     }
3960                     e = &st->internal->index_entries[st->internal->nb_index_entries++];
3961                     e->pos = current_offset;
3962                     e->timestamp = current_dts;
3963                     e->size = sample_size;
3964                     e->min_distance = distance;
3965                     e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
3966                     av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
3967                             "size %u, distance %u, keyframe %d\n", st->index, current_sample,
3968                             current_offset, current_dts, sample_size, distance, keyframe);
3969                     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->internal->nb_index_entries < 100)
3970                         ff_rfps_add_frame(mov->fc, st, current_dts);
3971                 }
3972
3973                 current_offset += sample_size;
3974                 stream_size += sample_size;
3975
3976                 /* A negative sample duration is invalid based on the spec,
3977                  * but some samples need it to correct the DTS. */
3978                 if (sc->stts_data[stts_index].duration < 0) {
3979                     av_log(mov->fc, AV_LOG_WARNING,
3980                            "Invalid SampleDelta %d in STTS, at %d st:%d\n",
3981                            sc->stts_data[stts_index].duration, stts_index,
3982                            st->index);
3983                     dts_correction += sc->stts_data[stts_index].duration - 1;
3984                     sc->stts_data[stts_index].duration = 1;
3985                 }
3986                 current_dts += sc->stts_data[stts_index].duration;
3987                 if (!dts_correction || current_dts + dts_correction > last_dts) {
3988                     current_dts += dts_correction;
3989                     dts_correction = 0;
3990                 } else {
3991                     /* Avoid creating non-monotonous DTS */
3992                     dts_correction += current_dts - last_dts - 1;
3993                     current_dts = last_dts + 1;
3994                 }
3995                 last_dts = current_dts;
3996                 distance++;
3997                 stts_sample++;
3998                 current_sample++;
3999                 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
4000                     stts_sample = 0;
4001                     stts_index++;
4002                 }
4003             }
4004         }
4005         if (st->duration > 0)
4006             st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
4007     } else {
4008         unsigned chunk_samples, total = 0;
4009
4010         if (!sc->chunk_count)
4011             return;
4012
4013         // compute total chunk count
4014         for (i = 0; i < sc->stsc_count; i++) {
4015             unsigned count, chunk_count;
4016
4017             chunk_samples = sc->stsc_data[i].count;
4018             if (i != sc->stsc_count - 1 &&
4019                 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
4020                 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
4021                 return;
4022             }
4023
4024             if (sc->samples_per_frame >= 160) { // gsm
4025                 count = chunk_samples / sc->samples_per_frame;
4026             } else if (sc->samples_per_frame > 1) {
4027                 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
4028                 count = (chunk_samples+samples-1) / samples;
4029             } else {
4030                 count = (chunk_samples+1023) / 1024;
4031             }
4032
4033             if (mov_stsc_index_valid(i, sc->stsc_count))
4034                 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
4035             else
4036                 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
4037             total += chunk_count * count;
4038         }
4039
4040         av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
4041         if (total >= UINT_MAX / sizeof(*st->internal->index_entries) - st->internal->nb_index_entries)
4042             return;
4043         if (av_reallocp_array(&st->internal->index_entries,
4044                               st->internal->nb_index_entries + total,
4045                               sizeof(*st->internal->index_entries)) < 0) {
4046             st->internal->nb_index_entries = 0;
4047             return;
4048         }
4049         st->internal->index_entries_allocated_size = (st->internal->nb_index_entries + total) * sizeof(*st->internal->index_entries);
4050
4051         // populate index
4052         for (i = 0; i < sc->chunk_count; i++) {
4053             current_offset = sc->chunk_offsets[i];
4054             if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4055                 i + 1 == sc->stsc_data[stsc_index + 1].first)
4056                 stsc_index++;
4057             chunk_samples = sc->stsc_data[stsc_index].count;
4058
4059             while (chunk_samples > 0) {
4060                 AVIndexEntry *e;
4061                 unsigned size, samples;
4062
4063                 if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
4064                     avpriv_request_sample(mov->fc,
4065                            "Zero bytes per frame, but %d samples per frame",
4066                            sc->samples_per_frame);
4067                     return;
4068                 }
4069
4070                 if (sc->samples_per_frame >= 160) { // gsm
4071                     samples = sc->samples_per_frame;
4072                     size = sc->bytes_per_frame;
4073                 } else {
4074                     if (sc->samples_per_frame > 1) {
4075                         samples = FFMIN((1024 / sc->samples_per_frame)*
4076                                         sc->samples_per_frame, chunk_samples);
4077                         size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
4078                     } else {
4079                         samples = FFMIN(1024, chunk_samples);
4080                         size = samples * sc->sample_size;
4081                     }
4082                 }
4083
4084                 if (st->internal->nb_index_entries >= total) {
4085                     av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
4086                     return;
4087                 }
4088                 if (size > 0x3FFFFFFF) {
4089                     av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
4090                     return;
4091                 }
4092                 e = &st->internal->index_entries[st->internal->nb_index_entries++];
4093                 e->pos = current_offset;
4094                 e->timestamp = current_dts;
4095                 e->size = size;
4096                 e->min_distance = 0;
4097                 e->flags = AVINDEX_KEYFRAME;
4098                 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
4099                        "size %u, duration %u\n", st->index, i, current_offset, current_dts,
4100                        size, samples);
4101
4102                 current_offset += size;
4103                 current_dts += samples;
4104                 chunk_samples -= samples;
4105             }
4106         }
4107     }
4108
4109     if (!mov->ignore_editlist && mov->advanced_editlist) {
4110         // Fix index according to edit lists.
4111         mov_fix_index(mov, st);
4112     }
4113
4114     // Update start time of the stream.
4115     if (st->start_time == AV_NOPTS_VALUE && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->internal->nb_index_entries > 0) {
4116         st->start_time = st->internal->index_entries[0].timestamp + sc->dts_shift;
4117         if (sc->ctts_data) {
4118             st->start_time += sc->ctts_data[0].duration;
4119         }
4120     }
4121
4122     mov_estimate_video_delay(mov, st);
4123 }
4124
4125 static int test_same_origin(const char *src, const char *ref) {
4126     char src_proto[64];
4127     char ref_proto[64];
4128     char src_auth[256];
4129     char ref_auth[256];
4130     char src_host[256];
4131     char ref_host[256];
4132     int src_port=-1;
4133     int ref_port=-1;
4134
4135     av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
4136     av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
4137
4138     if (strlen(src) == 0) {
4139         return -1;
4140     } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
4141         strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
4142         strlen(src_host) + 1 >= sizeof(src_host) ||
4143         strlen(ref_host) + 1 >= sizeof(ref_host)) {
4144         return 0;
4145     } else if (strcmp(src_proto, ref_proto) ||
4146                strcmp(src_auth, ref_auth) ||
4147                strcmp(src_host, ref_host) ||
4148                src_port != ref_port) {
4149         return 0;
4150     } else
4151         return 1;
4152 }
4153
4154 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
4155 {
4156     /* try relative path, we do not try the absolute because it can leak information about our
4157        system to an attacker */
4158     if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
4159         char filename[1025];
4160         const char *src_path;
4161         int i, l;
4162
4163         /* find a source dir */
4164         src_path = strrchr(src, '/');
4165         if (src_path)
4166             src_path++;
4167         else
4168             src_path = src;
4169
4170         /* find a next level down to target */
4171         for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
4172             if (ref->path[l] == '/') {
4173                 if (i == ref->nlvl_to - 1)
4174                     break;
4175                 else
4176                     i++;
4177             }
4178
4179         /* compose filename if next level down to target was found */
4180         if (i == ref->nlvl_to - 1 && src_path - src  < sizeof(filename)) {
4181             memcpy(filename, src, src_path - src);
4182             filename[src_path - src] = 0;
4183
4184             for (i = 1; i < ref->nlvl_from; i++)
4185                 av_strlcat(filename, "../", sizeof(filename));
4186
4187             av_strlcat(filename, ref->path + l + 1, sizeof(filename));
4188             if (!c->use_absolute_path) {
4189                 int same_origin = test_same_origin(src, filename);
4190
4191                 if (!same_origin) {
4192                     av_log(c->fc, AV_LOG_ERROR,
4193                         "Reference with mismatching origin, %s not tried for security reasons, "
4194                         "set demuxer option use_absolute_path to allow it anyway\n",
4195                         ref->path);
4196                     return AVERROR(ENOENT);
4197                 }
4198
4199                 if (strstr(ref->path + l + 1, "..") ||
4200                     strstr(ref->path + l + 1, ":") ||
4201                     (ref->nlvl_from > 1 && same_origin < 0) ||
4202                     (filename[0] == '/' && src_path == src))
4203                     return AVERROR(ENOENT);
4204             }
4205
4206             if (strlen(filename) + 1 == sizeof(filename))
4207                 return AVERROR(ENOENT);
4208             if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
4209                 return 0;
4210         }
4211     } else if (c->use_absolute_path) {
4212         av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
4213                "this is a possible security issue\n");
4214         if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
4215             return 0;
4216     } else {
4217         av_log(c->fc, AV_LOG_ERROR,
4218                "Absolute path %s not tried for security reasons, "
4219                "set demuxer option use_absolute_path to allow absolute paths\n",
4220                ref->path);
4221     }
4222
4223     return AVERROR(ENOENT);
4224 }
4225
4226 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
4227 {
4228     if (sc->time_scale <= 0) {
4229         av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
4230         sc->time_scale = c->time_scale;
4231         if (sc->time_scale <= 0)
4232             sc->time_scale = 1;
4233     }
4234 }
4235
4236 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4237 {
4238     AVStream *st;
4239     MOVStreamContext *sc;
4240     int ret;
4241
4242     st = avformat_new_stream(c->fc, NULL);
4243     if (!st) return AVERROR(ENOMEM);
4244     st->id = -1;
4245     sc = av_mallocz(sizeof(MOVStreamContext));
4246     if (!sc) return AVERROR(ENOMEM);
4247
4248     st->priv_data = sc;
4249     st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
4250     sc->ffindex = st->index;
4251     c->trak_index = st->index;
4252
4253     if ((ret = mov_read_default(c, pb, atom)) < 0)
4254         return ret;
4255
4256     c->trak_index = -1;
4257
4258     // Here stsc refers to a chunk not described in stco. This is technically invalid,
4259     // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
4260     if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
4261         sc->stsc_count = 0;
4262         av_freep(&sc->stsc_data);
4263     }
4264
4265     /* sanity checks */
4266     if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
4267                             (!sc->sample_size && !sc->sample_count))) ||
4268         (!sc->chunk_count && sc->sample_count)) {
4269         av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
4270                st->index);
4271         return 0;
4272     }
4273     if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
4274         av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
4275                st->index);
4276         return AVERROR_INVALIDDATA;
4277     }
4278
4279     fix_timescale(c, sc);
4280
4281     avpriv_set_pts_info(st, 64, 1, sc->time_scale);
4282
4283     mov_build_index(c, st);
4284
4285     if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
4286         MOVDref *dref = &sc->drefs[sc->dref_id - 1];
4287         if (c->enable_drefs) {
4288             if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
4289                 av_log(c->fc, AV_LOG_ERROR,
4290                        "stream %d, error opening alias: path='%s', dir='%s', "
4291                        "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
4292                        st->index, dref->path, dref->dir, dref->filename,
4293                        dref->volume, dref->nlvl_from, dref->nlvl_to);
4294         } else {
4295             av_log(c->fc, AV_LOG_WARNING,
4296                    "Skipped opening external track: "
4297                    "stream %d, alias: path='%s', dir='%s', "
4298                    "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
4299                    "Set enable_drefs to allow this.\n",
4300                    st->index, dref->path, dref->dir, dref->filename,
4301                    dref->volume, dref->nlvl_from, dref->nlvl_to);
4302         }
4303     } else {
4304         sc->pb = c->fc->pb;
4305         sc->pb_is_copied = 1;
4306     }
4307
4308     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4309         if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
4310             sc->height && sc->width &&
4311             (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
4312             st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
4313                                              ((double)st->codecpar->width * sc->height), INT_MAX);
4314         }
4315
4316 #if FF_API_R_FRAME_RATE
4317         if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
4318             av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
4319                       sc->time_scale, sc->stts_data[0].duration, INT_MAX);
4320 #endif
4321     }
4322
4323     // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
4324     if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
4325         TAG_IS_AVCI(st->codecpar->codec_tag)) {
4326         ret = ff_generate_avci_extradata(st);
4327         if (ret < 0)
4328             return ret;
4329     }
4330
4331     switch (st->codecpar->codec_id) {
4332 #if CONFIG_H261_DECODER
4333     case AV_CODEC_ID_H261:
4334 #endif
4335 #if CONFIG_H263_DECODER
4336     case AV_CODEC_ID_H263:
4337 #endif
4338 #if CONFIG_MPEG4_DECODER
4339     case AV_CODEC_ID_MPEG4:
4340 #endif
4341         st->codecpar->width = 0; /* let decoder init width/height */
4342         st->codecpar->height= 0;
4343         break;
4344     }
4345
4346     // If the duration of the mp3 packets is not constant, then they could need a parser
4347     if (st->codecpar->codec_id == AV_CODEC_ID_MP3
4348         && sc->stts_count > 3
4349         && sc->stts_count*10 > st->nb_frames
4350         && sc->time_scale == st->codecpar->sample_rate) {
4351             st->need_parsing = AVSTREAM_PARSE_FULL;
4352     }
4353     /* Do not need those anymore. */
4354     av_freep(&sc->chunk_offsets);
4355     av_freep(&sc->sample_sizes);
4356     av_freep(&sc->keyframes);
4357     av_freep(&sc->stts_data);
4358     av_freep(&sc->stps_data);
4359     av_freep(&sc->elst_data);
4360     av_freep(&sc->rap_group);
4361
4362     return 0;
4363 }
4364
4365 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4366 {
4367     int ret;
4368     c->itunes_metadata = 1;
4369     ret = mov_read_default(c, pb, atom);
4370     c->itunes_metadata = 0;
4371     return ret;
4372 }
4373
4374 static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4375 {
4376     uint32_t count;
4377     uint32_t i;
4378
4379     if (atom.size < 8)
4380         return 0;
4381
4382     avio_skip(pb, 4);
4383     count = avio_rb32(pb);
4384     if (count > UINT_MAX / sizeof(*c->meta_keys) - 1) {
4385         av_log(c->fc, AV_LOG_ERROR,
4386                "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
4387         return AVERROR_INVALIDDATA;
4388     }
4389
4390     c->meta_keys_count = count + 1;
4391     c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
4392     if (!c->meta_keys)
4393         return AVERROR(ENOMEM);
4394
4395     for (i = 1; i <= count; ++i) {
4396         uint32_t key_size = avio_rb32(pb);
4397         uint32_t type = avio_rl32(pb);
4398         if (key_size < 8) {
4399             av_log(c->fc, AV_LOG_ERROR,
4400                    "The key# %"PRIu32" in meta has invalid size:"
4401                    "%"PRIu32"\n", i, key_size);
4402             return AVERROR_INVALIDDATA;
4403         }
4404         key_size -= 8;
4405         if (type != MKTAG('m','d','t','a')) {
4406             avio_skip(pb, key_size);
4407         }
4408         c->meta_keys[i] = av_mallocz(key_size + 1);
4409         if (!c->meta_keys[i])
4410             return AVERROR(ENOMEM);
4411         avio_read(pb, c->meta_keys[i], key_size);
4412     }
4413
4414     return 0;
4415 }
4416
4417 static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4418 {
4419     int64_t end = av_sat_add64(avio_tell(pb), atom.size);
4420     uint8_t *key = NULL, *val = NULL, *mean = NULL;
4421     int i;
4422     int ret = 0;
4423     AVStream *st;
4424     MOVStreamContext *sc;
4425
4426     if (c->fc->nb_streams < 1)
4427         return 0;
4428     st = c->fc->streams[c->fc->nb_streams-1];
4429     sc = st->priv_data;
4430
4431     for (i = 0; i < 3; i++) {
4432         uint8_t **p;
4433         uint32_t len, tag;
4434
4435         if (end - avio_tell(pb) <= 12)
4436             break;
4437
4438         len = avio_rb32(pb);
4439         tag = avio_rl32(pb);
4440         avio_skip(pb, 4); // flags
4441
4442         if (len < 12 || len - 12 > end - avio_tell(pb))
4443             break;
4444         len -= 12;
4445
4446         if (tag == MKTAG('m', 'e', 'a', 'n'))
4447             p = &mean;
4448         else if (tag == MKTAG('n', 'a', 'm', 'e'))
4449             p = &key;
4450         else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
4451             avio_skip(pb, 4);
4452             len -= 4;
4453             p = &val;
4454         } else
4455             break;
4456
4457         if (*p)
4458             break;
4459
4460         *p = av_malloc(len + 1);
4461         if (!*p) {
4462             ret = AVERROR(ENOMEM);
4463             break;
4464         }
4465         ret = ffio_read_size(pb, *p, len);
4466         if (ret < 0) {
4467             av_freep(p);
4468             break;
4469         }
4470         (*p)[len] = 0;
4471     }
4472
4473     if (mean && key && val) {
4474         if (strcmp(key, "iTunSMPB") == 0) {
4475             int priming, remainder, samples;
4476             if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
4477                 if(priming>0 && priming<16384)
4478                     sc->start_pad = priming;
4479             }
4480         }
4481         if (strcmp(key, "cdec") != 0) {
4482             av_dict_set(&c->fc->metadata, key, val,
4483                         AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
4484             key = val = NULL;
4485         }
4486     } else {
4487         av_log(c->fc, AV_LOG_VERBOSE,
4488                "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
4489     }
4490
4491     avio_seek(pb, end, SEEK_SET);
4492     av_freep(&key);
4493     av_freep(&val);
4494     av_freep(&mean);
4495     return ret;
4496 }
4497
4498 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4499 {
4500     while (atom.size > 8) {
4501         uint32_t tag;
4502         if (avio_feof(pb))
4503             return AVERROR_EOF;
4504         tag = avio_rl32(pb);
4505         atom.size -= 4;
4506         if (tag == MKTAG('h','d','l','r')) {
4507             avio_seek(pb, -8, SEEK_CUR);
4508             atom.size += 8;
4509             return mov_read_default(c, pb, atom);
4510         }
4511     }
4512     return 0;
4513 }
4514
4515 // return 1 when matrix is identity, 0 otherwise
4516 #define IS_MATRIX_IDENT(matrix)            \
4517     ( (matrix)[0][0] == (1 << 16) &&       \
4518       (matrix)[1][1] == (1 << 16) &&       \
4519       (matrix)[2][2] == (1 << 30) &&       \
4520      !(matrix)[0][1] && !(matrix)[0][2] && \
4521      !(matrix)[1][0] && !(matrix)[1][2] && \
4522      !(matrix)[2][0] && !(matrix)[2][1])
4523
4524 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4525 {
4526     int i, j, e;
4527     int width;
4528     int height;
4529     int display_matrix[3][3];
4530     int res_display_matrix[3][3] = { { 0 } };
4531     AVStream *st;
4532     MOVStreamContext *sc;
4533     int version;
4534     int flags;
4535
4536     if (c->fc->nb_streams < 1)
4537         return 0;
4538     st = c->fc->streams[c->fc->nb_streams-1];
4539     sc = st->priv_data;
4540
4541     // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
4542     // avoids corrupting AVStreams mapped to an earlier tkhd.
4543     if (st->id != -1)
4544         return AVERROR_INVALIDDATA;
4545
4546     version = avio_r8(pb);
4547     flags = avio_rb24(pb);
4548     st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
4549
4550     if (version == 1) {
4551         avio_rb64(pb);
4552         avio_rb64(pb);
4553     } else {
4554         avio_rb32(pb); /* creation time */
4555         avio_rb32(pb); /* modification time */
4556     }
4557     st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
4558     avio_rb32(pb); /* reserved */
4559
4560     /* highlevel (considering edits) duration in movie timebase */
4561     (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
4562     avio_rb32(pb); /* reserved */
4563     avio_rb32(pb); /* reserved */
4564
4565     avio_rb16(pb); /* layer */
4566     avio_rb16(pb); /* alternate group */
4567     avio_rb16(pb); /* volume */
4568     avio_rb16(pb); /* reserved */
4569
4570     //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
4571     // they're kept in fixed point format through all calculations
4572     // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
4573     // side data, but the scale factor is not needed to calculate aspect ratio
4574     for (i = 0; i < 3; i++) {
4575         display_matrix[i][0] = avio_rb32(pb);   // 16.16 fixed point
4576         display_matrix[i][1] = avio_rb32(pb);   // 16.16 fixed point
4577         display_matrix[i][2] = avio_rb32(pb);   //  2.30 fixed point
4578     }
4579
4580     width = avio_rb32(pb);       // 16.16 fixed point track width
4581     height = avio_rb32(pb);      // 16.16 fixed point track height
4582     sc->width = width >> 16;
4583     sc->height = height >> 16;
4584
4585     // apply the moov display matrix (after the tkhd one)
4586     for (i = 0; i < 3; i++) {
4587         const int sh[3] = { 16, 16, 30 };
4588         for (j = 0; j < 3; j++) {
4589             for (e = 0; e < 3; e++) {
4590                 res_display_matrix[i][j] +=
4591                     ((int64_t) display_matrix[i][e] *
4592                      c->movie_display_matrix[e][j]) >> sh[e];
4593             }
4594         }
4595     }
4596
4597     // save the matrix when it is not the default identity
4598     if (!IS_MATRIX_IDENT(res_display_matrix)) {
4599         double rotate;
4600
4601         av_freep(&sc->display_matrix);
4602         sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
4603         if (!sc->display_matrix)
4604             return AVERROR(ENOMEM);
4605
4606         for (i = 0; i < 3; i++)
4607             for (j = 0; j < 3; j++)
4608                 sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
4609
4610 #if FF_API_OLD_ROTATE_API
4611         rotate = av_display_rotation_get(sc->display_matrix);
4612         if (!isnan(rotate)) {
4613             char rotate_buf[64];
4614             rotate = -rotate;
4615             if (rotate < 0) // for backward compatibility
4616                 rotate += 360;
4617             snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
4618             av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
4619         }
4620 #endif
4621     }
4622
4623     // transform the display width/height according to the matrix
4624     // to keep the same scale, use [width height 1<<16]
4625     if (width && height && sc->display_matrix) {
4626         double disp_transform[2];
4627
4628         for (i = 0; i < 2; i++)
4629             disp_transform[i] = hypot(sc->display_matrix[0 + i],
4630                                       sc->display_matrix[3 + i]);
4631
4632         if (disp_transform[0] > 0       && disp_transform[1] > 0 &&
4633             disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
4634             fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
4635             st->sample_aspect_ratio = av_d2q(
4636                 disp_transform[0] / disp_transform[1],
4637                 INT_MAX);
4638     }
4639     return 0;
4640 }
4641
4642 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4643 {
4644     MOVFragment *frag = &c->fragment;
4645     MOVTrackExt *trex = NULL;
4646     int flags, track_id, i;
4647     MOVFragmentStreamInfo * frag_stream_info;
4648
4649     avio_r8(pb); /* version */
4650     flags = avio_rb24(pb);
4651
4652     track_id = avio_rb32(pb);
4653     if (!track_id)
4654         return AVERROR_INVALIDDATA;
4655     for (i = 0; i < c->trex_count; i++)
4656         if (c->trex_data[i].track_id == track_id) {
4657             trex = &c->trex_data[i];
4658             break;
4659         }
4660     if (!trex) {
4661         av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
4662         return 0;
4663     }
4664     c->fragment.found_tfhd = 1;
4665     frag->track_id = track_id;
4666     set_frag_stream(&c->frag_index, track_id);
4667
4668     frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
4669                              avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
4670                              frag->moof_offset : frag->implicit_offset;
4671     frag->stsd_id  = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
4672
4673     frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
4674                      avio_rb32(pb) : trex->duration;
4675     frag->size     = flags & MOV_TFHD_DEFAULT_SIZE ?
4676                      avio_rb32(pb) : trex->size;
4677     frag->flags    = flags & MOV_TFHD_DEFAULT_FLAGS ?
4678                      avio_rb32(pb) : trex->flags;
4679     av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
4680
4681     frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4682     if (frag_stream_info)
4683         frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
4684
4685     return 0;
4686 }
4687
4688 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4689 {
4690     unsigned i, num;
4691     void *new_tracks;
4692
4693     num = atom.size / 4;
4694     if (!(new_tracks = av_malloc_array(num, sizeof(int))))
4695         return AVERROR(ENOMEM);
4696
4697     av_free(c->chapter_tracks);
4698     c->chapter_tracks = new_tracks;
4699     c->nb_chapter_tracks = num;
4700
4701     for (i = 0; i < num && !pb->eof_reached; i++)
4702         c->chapter_tracks[i] = avio_rb32(pb);
4703
4704     return 0;
4705 }
4706
4707 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4708 {
4709     MOVTrackExt *trex;
4710     int err;
4711
4712     if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
4713         return AVERROR_INVALIDDATA;
4714     if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
4715                                  sizeof(*c->trex_data))) < 0) {
4716         c->trex_count = 0;
4717         return err;
4718     }
4719
4720     c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
4721
4722     trex = &c->trex_data[c->trex_count++];
4723     avio_r8(pb); /* version */
4724     avio_rb24(pb); /* flags */
4725     trex->track_id = avio_rb32(pb);
4726     trex->stsd_id  = avio_rb32(pb);
4727     trex->duration = avio_rb32(pb);
4728     trex->size     = avio_rb32(pb);
4729     trex->flags    = avio_rb32(pb);
4730     return 0;
4731 }
4732
4733 static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4734 {
4735     MOVFragment *frag = &c->fragment;
4736     AVStream *st = NULL;
4737     MOVStreamContext *sc;
4738     int version, i;
4739     MOVFragmentStreamInfo * frag_stream_info;
4740     int64_t base_media_decode_time;
4741
4742     for (i = 0; i < c->fc->nb_streams; i++) {
4743         if (c->fc->streams[i]->id == frag->track_id) {
4744             st = c->fc->streams[i];
4745             break;
4746         }
4747     }
4748     if (!st) {
4749         av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
4750         return 0;
4751     }
4752     sc = st->priv_data;
4753     if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
4754         return 0;
4755     version = avio_r8(pb);
4756     avio_rb24(pb); /* flags */
4757     if (version) {
4758         base_media_decode_time = avio_rb64(pb);
4759     } else {
4760         base_media_decode_time = avio_rb32(pb);
4761     }
4762
4763     frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4764     if (frag_stream_info)
4765         frag_stream_info->tfdt_dts = base_media_decode_time;
4766     sc->track_end = base_media_decode_time;
4767
4768     return 0;
4769 }
4770
4771 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4772 {
4773     MOVFragment *frag = &c->fragment;
4774     AVStream *st = NULL;
4775     MOVStreamContext *sc;
4776     MOVStts *ctts_data;
4777     uint64_t offset;
4778     int64_t dts, pts = AV_NOPTS_VALUE;
4779     int data_offset = 0;
4780     unsigned entries, first_sample_flags = frag->flags;
4781     int flags, distance, i;
4782     int64_t prev_dts = AV_NOPTS_VALUE;
4783     int next_frag_index = -1, index_entry_pos;
4784     size_t requested_size;
4785     size_t old_ctts_allocated_size;
4786     AVIndexEntry *new_entries;
4787     MOVFragmentStreamInfo * frag_stream_info;
4788
4789     if (!frag->found_tfhd) {
4790         av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
4791         return AVERROR_INVALIDDATA;
4792     }
4793
4794     for (i = 0; i < c->fc->nb_streams; i++) {
4795         if (c->fc->streams[i]->id == frag->track_id) {
4796             st = c->fc->streams[i];
4797             break;
4798         }
4799     }
4800     if (!st) {
4801         av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
4802         return 0;
4803     }
4804     sc = st->priv_data;
4805     if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
4806         return 0;
4807
4808     // Find the next frag_index index that has a valid index_entry for
4809     // the current track_id.
4810     //
4811     // A valid index_entry means the trun for the fragment was read
4812     // and it's samples are in index_entries at the given position.
4813     // New index entries will be inserted before the index_entry found.
4814     index_entry_pos = st->internal->nb_index_entries;
4815     for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
4816         frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
4817         if (frag_stream_info && frag_stream_info->index_entry >= 0) {
4818             next_frag_index = i;
4819             index_entry_pos = frag_stream_info->index_entry;
4820             break;
4821         }
4822     }
4823     av_assert0(index_entry_pos <= st->internal->nb_index_entries);
4824
4825     avio_r8(pb); /* version */
4826     flags = avio_rb24(pb);
4827     entries = avio_rb32(pb);
4828     av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
4829
4830     if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
4831         return AVERROR_INVALIDDATA;
4832     if (flags & MOV_TRUN_DATA_OFFSET)        data_offset        = avio_rb32(pb);
4833     if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
4834
4835     frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4836     if (frag_stream_info) {
4837         if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
4838             dts = frag_stream_info->next_trun_dts - sc->time_offset;
4839         } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
4840             c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
4841             pts = frag_stream_info->first_tfra_pts;
4842             av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
4843                     ", using it for pts\n", pts);
4844         } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
4845             c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
4846             dts = frag_stream_info->first_tfra_pts;
4847             av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
4848                     ", using it for dts\n", pts);
4849         } else if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE) {
4850             // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
4851             // pts = frag_stream_info->sidx_pts;
4852             dts = frag_stream_info->sidx_pts - sc->time_offset;
4853             av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
4854                     ", using it for pts\n", pts);
4855         } else if (frag_stream_info->tfdt_dts != AV_NOPTS_VALUE) {
4856             dts = frag_stream_info->tfdt_dts - sc->time_offset;
4857             av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
4858                     ", using it for dts\n", dts);
4859         } else {
4860             dts = sc->track_end - sc->time_offset;
4861             av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
4862                     ", using it for dts\n", dts);
4863         }
4864     } else {
4865         dts = sc->track_end - sc->time_offset;
4866         av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
4867                 ", using it for dts\n", dts);
4868     }
4869     offset   = frag->base_data_offset + data_offset;
4870     distance = 0;
4871     av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
4872
4873     // realloc space for new index entries
4874     if((uint64_t)st->internal->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
4875         entries = UINT_MAX / sizeof(AVIndexEntry) - st->internal->nb_index_entries;
4876         av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
4877     }
4878     if (entries == 0)
4879         return 0;
4880
4881     requested_size = (st->internal->nb_index_entries + entries) * sizeof(AVIndexEntry);
4882     new_entries = av_fast_realloc(st->internal->index_entries,
4883                                   &st->internal->index_entries_allocated_size,
4884                                   requested_size);
4885     if (!new_entries)
4886         return AVERROR(ENOMEM);
4887     st->internal->index_entries= new_entries;
4888
4889     requested_size = (st->internal->nb_index_entries + entries) * sizeof(*sc->ctts_data);
4890     old_ctts_allocated_size = sc->ctts_allocated_size;
4891     ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
4892                                 requested_size);
4893     if (!ctts_data)
4894         return AVERROR(ENOMEM);
4895     sc->ctts_data = ctts_data;
4896
4897     // In case there were samples without ctts entries, ensure they get
4898     // zero valued entries. This ensures clips which mix boxes with and
4899     // without ctts entries don't pickup uninitialized data.
4900     memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
4901            sc->ctts_allocated_size - old_ctts_allocated_size);
4902
4903     if (index_entry_pos < st->internal->nb_index_entries) {
4904         // Make hole in index_entries and ctts_data for new samples
4905         memmove(st->internal->index_entries + index_entry_pos + entries,
4906                 st->internal->index_entries + index_entry_pos,
4907                 sizeof(*st->internal->index_entries) *
4908                 (st->internal->nb_index_entries - index_entry_pos));
4909         memmove(sc->ctts_data + index_entry_pos + entries,
4910                 sc->ctts_data + index_entry_pos,
4911                 sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
4912         if (index_entry_pos < sc->current_sample) {
4913             sc->current_sample += entries;
4914         }
4915     }
4916
4917     st->internal->nb_index_entries += entries;
4918     sc->ctts_count = st->internal->nb_index_entries;
4919
4920     // Record the index_entry position in frag_index of this fragment
4921     if (frag_stream_info)
4922         frag_stream_info->index_entry = index_entry_pos;
4923
4924     if (index_entry_pos > 0)
4925         prev_dts = st->internal->index_entries[index_entry_pos-1].timestamp;
4926
4927     for (i = 0; i < entries && !pb->eof_reached; i++) {
4928         unsigned sample_size = frag->size;
4929         int sample_flags = i ? frag->flags : first_sample_flags;
4930         unsigned sample_duration = frag->duration;
4931         unsigned ctts_duration = 0;
4932         int keyframe = 0;
4933         int index_entry_flags = 0;
4934
4935         if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
4936         if (flags & MOV_TRUN_SAMPLE_SIZE)     sample_size     = avio_rb32(pb);
4937         if (flags & MOV_TRUN_SAMPLE_FLAGS)    sample_flags    = avio_rb32(pb);
4938         if (flags & MOV_TRUN_SAMPLE_CTS)      ctts_duration   = avio_rb32(pb);
4939
4940         mov_update_dts_shift(sc, ctts_duration, c->fc);
4941         if (pts != AV_NOPTS_VALUE) {
4942             dts = pts - sc->dts_shift;
4943             if (flags & MOV_TRUN_SAMPLE_CTS) {
4944                 dts -= ctts_duration;
4945             } else {
4946                 dts -= sc->time_offset;
4947             }
4948             av_log(c->fc, AV_LOG_DEBUG,
4949                    "pts %"PRId64" calculated dts %"PRId64
4950                    " sc->dts_shift %d ctts.duration %d"
4951                    " sc->time_offset %"PRId64
4952                    " flags & MOV_TRUN_SAMPLE_CTS %d\n",
4953                    pts, dts,
4954                    sc->dts_shift, ctts_duration,
4955                    sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
4956             pts = AV_NOPTS_VALUE;
4957         }
4958
4959         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
4960             keyframe = 1;
4961         else
4962             keyframe =
4963                 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
4964                                   MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
4965         if (keyframe) {
4966             distance = 0;
4967             index_entry_flags |= AVINDEX_KEYFRAME;
4968         }
4969         // Fragments can overlap in time.  Discard overlapping frames after
4970         // decoding.
4971         if (prev_dts >= dts)
4972             index_entry_flags |= AVINDEX_DISCARD_FRAME;
4973
4974         st->internal->index_entries[index_entry_pos].pos = offset;
4975         st->internal->index_entries[index_entry_pos].timestamp = dts;
4976         st->internal->index_entries[index_entry_pos].size= sample_size;
4977         st->internal->index_entries[index_entry_pos].min_distance= distance;
4978         st->internal->index_entries[index_entry_pos].flags = index_entry_flags;
4979
4980         sc->ctts_data[index_entry_pos].count = 1;
4981         sc->ctts_data[index_entry_pos].duration = ctts_duration;
4982         index_entry_pos++;
4983
4984         av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
4985                 "size %u, distance %d, keyframe %d\n", st->index,
4986                 index_entry_pos, offset, dts, sample_size, distance, keyframe);
4987         distance++;
4988         dts += sample_duration;
4989         offset += sample_size;
4990         sc->data_size += sample_size;
4991
4992         if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
4993             1 <= INT_MAX - sc->nb_frames_for_fps
4994         ) {
4995             sc->duration_for_fps += sample_duration;
4996             sc->nb_frames_for_fps ++;
4997         }
4998     }
4999     if (frag_stream_info)
5000         frag_stream_info->next_trun_dts = dts + sc->time_offset;
5001     if (i < entries) {
5002         // EOF found before reading all entries.  Fix the hole this would
5003         // leave in index_entries and ctts_data
5004         int gap = entries - i;
5005         memmove(st->internal->index_entries + index_entry_pos,
5006                 st->internal->index_entries + index_entry_pos + gap,
5007                 sizeof(*st->internal->index_entries) *
5008                 (st->internal->nb_index_entries - (index_entry_pos + gap)));
5009         memmove(sc->ctts_data + index_entry_pos,
5010                 sc->ctts_data + index_entry_pos + gap,
5011                 sizeof(*sc->ctts_data) *
5012                 (sc->ctts_count - (index_entry_pos + gap)));
5013
5014         st->internal->nb_index_entries -= gap;
5015         sc->ctts_count -= gap;
5016         if (index_entry_pos < sc->current_sample) {
5017             sc->current_sample -= gap;
5018         }
5019         entries = i;
5020     }
5021
5022     // The end of this new fragment may overlap in time with the start
5023     // of the next fragment in index_entries. Mark the samples in the next
5024     // fragment that overlap with AVINDEX_DISCARD_FRAME
5025     prev_dts = AV_NOPTS_VALUE;
5026     if (index_entry_pos > 0)
5027         prev_dts = st->internal->index_entries[index_entry_pos-1].timestamp;
5028     for (i = index_entry_pos; i < st->internal->nb_index_entries; i++) {
5029         if (prev_dts < st->internal->index_entries[i].timestamp)
5030             break;
5031         st->internal->index_entries[i].flags |= AVINDEX_DISCARD_FRAME;
5032     }
5033
5034     // If a hole was created to insert the new index_entries into,
5035     // the index_entry recorded for all subsequent moof must
5036     // be incremented by the number of entries inserted.
5037     fix_frag_index_entries(&c->frag_index, next_frag_index,
5038                            frag->track_id, entries);
5039
5040     if (pb->eof_reached) {
5041         av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
5042         return AVERROR_EOF;
5043     }
5044
5045     frag->implicit_offset = offset;
5046
5047     sc->track_end = dts + sc->time_offset;
5048     if (st->duration < sc->track_end)
5049         st->duration = sc->track_end;
5050
5051     return 0;
5052 }
5053
5054 static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5055 {
5056     int64_t stream_size = avio_size(pb);
5057     int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
5058     uint8_t version, is_complete;
5059     unsigned i, j, track_id, item_count;
5060     AVStream *st = NULL;
5061     AVStream *ref_st = NULL;
5062     MOVStreamContext *sc, *ref_sc = NULL;
5063     AVRational timescale;
5064
5065     version = avio_r8(pb);
5066     if (version > 1) {
5067         avpriv_request_sample(c->fc, "sidx version %u", version);
5068         return 0;
5069     }
5070
5071     avio_rb24(pb); // flags
5072
5073     track_id = avio_rb32(pb); // Reference ID
5074     for (i = 0; i < c->fc->nb_streams; i++) {
5075         if (c->fc->streams[i]->id == track_id) {
5076             st = c->fc->streams[i];
5077             break;
5078         }
5079     }
5080     if (!st) {
5081         av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
5082         return 0;
5083     }
5084
5085     sc = st->priv_data;
5086
5087     timescale = av_make_q(1, avio_rb32(pb));
5088
5089     if (timescale.den <= 0) {
5090         av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
5091         return AVERROR_INVALIDDATA;
5092     }
5093
5094     if (version == 0) {
5095         pts = avio_rb32(pb);
5096         offset += avio_rb32(pb);
5097     } else {
5098         pts = avio_rb64(pb);
5099         offset += avio_rb64(pb);
5100     }
5101
5102     avio_rb16(pb); // reserved
5103
5104     item_count = avio_rb16(pb);
5105
5106     for (i = 0; i < item_count; i++) {
5107         int index;
5108         MOVFragmentStreamInfo * frag_stream_info;
5109         uint32_t size = avio_rb32(pb);
5110         uint32_t duration = avio_rb32(pb);
5111         if (size & 0x80000000) {
5112             avpriv_request_sample(c->fc, "sidx reference_type 1");
5113             return AVERROR_PATCHWELCOME;
5114         }
5115         avio_rb32(pb); // sap_flags
5116         timestamp = av_rescale_q(pts, timescale, st->time_base);
5117
5118         index = update_frag_index(c, offset);
5119         frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
5120         if (frag_stream_info)
5121             frag_stream_info->sidx_pts = timestamp;
5122
5123         offset += size;
5124         pts += duration;
5125     }
5126
5127     st->duration = sc->track_end = pts;
5128
5129     sc->has_sidx = 1;
5130
5131     // See if the remaining bytes are just an mfra which we can ignore.
5132     is_complete = offset == stream_size;
5133     if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL)) {
5134         int64_t ret;
5135         int64_t original_pos = avio_tell(pb);
5136         if (!c->have_read_mfra_size) {
5137             if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
5138                 return ret;
5139             c->mfra_size = avio_rb32(pb);
5140             c->have_read_mfra_size = 1;
5141             if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
5142                 return ret;
5143         }
5144         if (offset + c->mfra_size == stream_size)
5145             is_complete = 1;
5146     }
5147
5148     if (is_complete) {
5149         // Find first entry in fragment index that came from an sidx.
5150         // This will pretty much always be the first entry.
5151         for (i = 0; i < c->frag_index.nb_items; i++) {
5152             MOVFragmentIndexItem * item = &c->frag_index.item[i];
5153             for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
5154                 MOVFragmentStreamInfo * si;
5155                 si = &item->stream_info[j];
5156                 if (si->sidx_pts != AV_NOPTS_VALUE) {
5157                     ref_st = c->fc->streams[j];
5158                     ref_sc = ref_st->priv_data;
5159                     break;
5160                 }
5161             }
5162         }
5163         if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
5164             st = c->fc->streams[i];
5165             sc = st->priv_data;
5166             if (!sc->has_sidx) {
5167                 st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
5168             }
5169         }
5170
5171         c->frag_index.complete = 1;
5172     }
5173
5174     return 0;
5175 }
5176
5177 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
5178 /* like the files created with Adobe Premiere 5.0, for samples see */
5179 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
5180 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5181 {
5182     int err;
5183
5184     if (atom.size < 8)
5185         return 0; /* continue */
5186     if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
5187         avio_skip(pb, atom.size - 4);
5188         return 0;
5189     }
5190     atom.type = avio_rl32(pb);
5191     atom.size -= 8;
5192     if (atom.type != MKTAG('m','d','a','t')) {
5193         avio_skip(pb, atom.size);
5194         return 0;
5195     }
5196     err = mov_read_mdat(c, pb, atom);
5197     return err;
5198 }
5199
5200 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5201 {
5202 #if CONFIG_ZLIB
5203     AVIOContext ctx;
5204     uint8_t *cmov_data;
5205     uint8_t *moov_data; /* uncompressed data */
5206     long cmov_len, moov_len;
5207     int ret = -1;
5208
5209     avio_rb32(pb); /* dcom atom */
5210     if (avio_rl32(pb) != MKTAG('d','c','o','m'))
5211         return AVERROR_INVALIDDATA;
5212     if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
5213         av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
5214         return AVERROR_INVALIDDATA;
5215     }
5216     avio_rb32(pb); /* cmvd atom */
5217     if (avio_rl32(pb) != MKTAG('c','m','v','d'))
5218         return AVERROR_INVALIDDATA;
5219     moov_len = avio_rb32(pb); /* uncompressed size */
5220     cmov_len = atom.size - 6 * 4;
5221
5222     cmov_data = av_malloc(cmov_len);
5223     if (!cmov_data)
5224         return AVERROR(ENOMEM);
5225     moov_data = av_malloc(moov_len);
5226     if (!moov_data) {
5227         av_free(cmov_data);
5228         return AVERROR(ENOMEM);
5229     }
5230     ret = ffio_read_size(pb, cmov_data, cmov_len);
5231     if (ret < 0)
5232         goto free_and_return;
5233
5234     ret = AVERROR_INVALIDDATA;
5235     if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
5236         goto free_and_return;
5237     if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
5238         goto free_and_return;
5239     ctx.seekable = AVIO_SEEKABLE_NORMAL;
5240     atom.type = MKTAG('m','o','o','v');
5241     atom.size = moov_len;
5242     ret = mov_read_default(c, &ctx, atom);
5243 free_and_return:
5244     av_free(moov_data);
5245     av_free(cmov_data);
5246     return ret;
5247 #else
5248     av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
5249     return AVERROR(ENOSYS);
5250 #endif
5251 }
5252
5253 /* edit list atom */
5254 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5255 {
5256     MOVStreamContext *sc;
5257     int i, edit_count, version;
5258     int64_t elst_entry_size;
5259
5260     if (c->fc->nb_streams < 1 || c->ignore_editlist)
5261         return 0;
5262     sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
5263
5264     version = avio_r8(pb); /* version */
5265     avio_rb24(pb); /* flags */
5266     edit_count = avio_rb32(pb); /* entries */
5267     atom.size -= 8;
5268
5269     elst_entry_size = version == 1 ? 20 : 12;
5270     if (atom.size != edit_count * elst_entry_size) {
5271         if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
5272             av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
5273                    edit_count, atom.size + 8);
5274             return AVERROR_INVALIDDATA;
5275         } else {
5276             edit_count = atom.size / elst_entry_size;
5277             if (edit_count * elst_entry_size != atom.size) {
5278                 av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
5279             }
5280         }
5281     }
5282
5283     if (!edit_count)
5284         return 0;
5285     if (sc->elst_data)
5286         av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
5287     av_free(sc->elst_data);
5288     sc->elst_count = 0;
5289     sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
5290     if (!sc->elst_data)
5291         return AVERROR(ENOMEM);
5292
5293     av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
5294     for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
5295         MOVElst *e = &sc->elst_data[i];
5296
5297         if (version == 1) {
5298             e->duration = avio_rb64(pb);
5299             e->time     = avio_rb64(pb);
5300             atom.size -= 16;
5301         } else {
5302             e->duration = avio_rb32(pb); /* segment duration */
5303             e->time     = (int32_t)avio_rb32(pb); /* media time */
5304             atom.size -= 8;
5305         }
5306         e->rate = avio_rb32(pb) / 65536.0;
5307         atom.size -= 4;
5308         av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
5309                e->duration, e->time, e->rate);
5310
5311         if (e->time < 0 && e->time != -1 &&
5312             c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
5313             av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
5314                    c->fc->nb_streams-1, i, e->time);
5315             return AVERROR_INVALIDDATA;
5316         }
5317     }
5318     sc->elst_count = i;
5319
5320     return 0;
5321 }
5322
5323 static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5324 {
5325     MOVStreamContext *sc;
5326
5327     if (c->fc->nb_streams < 1)
5328         return AVERROR_INVALIDDATA;
5329     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5330     sc->timecode_track = avio_rb32(pb);
5331     return 0;
5332 }
5333
5334 static int mov_read_av1c(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5335 {
5336     AVStream *st;
5337     int ret;
5338
5339     if (c->fc->nb_streams < 1)
5340         return 0;
5341     st = c->fc->streams[c->fc->nb_streams - 1];
5342
5343     if (atom.size < 4) {
5344         av_log(c->fc, AV_LOG_ERROR, "Empty AV1 Codec Configuration Box\n");
5345         return AVERROR_INVALIDDATA;
5346     }
5347
5348     /* For now, propagate only the OBUs, if any. Once libavcodec is
5349        updated to handle isobmff style extradata this can be removed. */
5350     avio_skip(pb, 4);
5351
5352     if (atom.size == 4)
5353         return 0;
5354
5355     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 4);
5356     if (ret < 0)
5357         return ret;
5358
5359     return 0;
5360 }
5361
5362 static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5363 {
5364     AVStream *st;
5365     int version, color_range, color_primaries, color_trc, color_space;
5366
5367     if (c->fc->nb_streams < 1)
5368         return 0;
5369     st = c->fc->streams[c->fc->nb_streams - 1];
5370
5371     if (atom.size < 5) {
5372         av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
5373         return AVERROR_INVALIDDATA;
5374     }
5375
5376     version = avio_r8(pb);
5377     if (version != 1) {
5378         av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
5379         return 0;
5380     }
5381     avio_skip(pb, 3); /* flags */
5382
5383     avio_skip(pb, 2); /* profile + level */
5384     color_range     = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
5385     color_primaries = avio_r8(pb);
5386     color_trc       = avio_r8(pb);
5387     color_space     = avio_r8(pb);
5388     if (avio_rb16(pb)) /* codecIntializationDataSize */
5389         return AVERROR_INVALIDDATA;
5390
5391     if (!av_color_primaries_name(color_primaries))
5392         color_primaries = AVCOL_PRI_UNSPECIFIED;
5393     if (!av_color_transfer_name(color_trc))
5394         color_trc = AVCOL_TRC_UNSPECIFIED;
5395     if (!av_color_space_name(color_space))
5396         color_space = AVCOL_SPC_UNSPECIFIED;
5397
5398     st->codecpar->color_range     = (color_range & 1) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
5399     st->codecpar->color_primaries = color_primaries;
5400     st->codecpar->color_trc       = color_trc;
5401     st->codecpar->color_space     = color_space;
5402
5403     return 0;
5404 }
5405
5406 static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5407 {
5408     MOVStreamContext *sc;
5409     int i, version;
5410
5411     if (c->fc->nb_streams < 1)
5412         return AVERROR_INVALIDDATA;
5413
5414     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5415
5416     if (atom.size < 5) {
5417         av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
5418         return AVERROR_INVALIDDATA;
5419     }
5420
5421     version = avio_r8(pb);
5422     if (version) {
5423         av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
5424         return 0;
5425     }
5426     avio_skip(pb, 3); /* flags */
5427
5428     sc->mastering = av_mastering_display_metadata_alloc();
5429     if (!sc->mastering)
5430         return AVERROR(ENOMEM);
5431
5432     for (i = 0; i < 3; i++) {
5433         sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
5434         sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
5435     }
5436     sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
5437     sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
5438
5439     sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
5440     sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
5441
5442     sc->mastering->has_primaries = 1;
5443     sc->mastering->has_luminance = 1;
5444
5445     return 0;
5446 }
5447
5448 static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5449 {
5450     MOVStreamContext *sc;
5451     const int mapping[3] = {1, 2, 0};
5452     const int chroma_den = 50000;
5453     const int luma_den = 10000;
5454     int i;
5455
5456     if (c->fc->nb_streams < 1)
5457         return AVERROR_INVALIDDATA;
5458
5459     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5460
5461     if (atom.size < 24) {
5462         av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
5463         return AVERROR_INVALIDDATA;
5464     }
5465
5466     sc->mastering = av_mastering_display_metadata_alloc();
5467     if (!sc->mastering)
5468         return AVERROR(ENOMEM);
5469
5470     for (i = 0; i < 3; i++) {
5471         const int j = mapping[i];
5472         sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
5473         sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
5474     }
5475     sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
5476     sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
5477
5478     sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
5479     sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
5480
5481     sc->mastering->has_luminance = 1;
5482     sc->mastering->has_primaries = 1;
5483
5484     return 0;
5485 }
5486
5487 static int mov_read_coll(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5488 {
5489     MOVStreamContext *sc;
5490     int version;
5491
5492     if (c->fc->nb_streams < 1)
5493         return AVERROR_INVALIDDATA;
5494
5495     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5496
5497     if (atom.size < 5) {
5498         av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
5499         return AVERROR_INVALIDDATA;
5500     }
5501
5502     version = avio_r8(pb);
5503     if (version) {
5504         av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
5505         return 0;
5506     }
5507     avio_skip(pb, 3); /* flags */
5508
5509     sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
5510     if (!sc->coll)
5511         return AVERROR(ENOMEM);
5512
5513     sc->coll->MaxCLL  = avio_rb16(pb);
5514     sc->coll->MaxFALL = avio_rb16(pb);
5515
5516     return 0;
5517 }
5518
5519 static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5520 {
5521     MOVStreamContext *sc;
5522
5523     if (c->fc->nb_streams < 1)
5524         return AVERROR_INVALIDDATA;
5525
5526     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5527
5528     if (atom.size < 4) {
5529         av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
5530         return AVERROR_INVALIDDATA;
5531     }
5532
5533     sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
5534     if (!sc->coll)
5535         return AVERROR(ENOMEM);
5536
5537     sc->coll->MaxCLL  = avio_rb16(pb);
5538     sc->coll->MaxFALL = avio_rb16(pb);
5539
5540     return 0;
5541 }
5542
5543 static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5544 {
5545     AVStream *st;
5546     MOVStreamContext *sc;
5547     enum AVStereo3DType type;
5548     int mode;
5549
5550     if (c->fc->nb_streams < 1)
5551         return 0;
5552
5553     st = c->fc->streams[c->fc->nb_streams - 1];
5554     sc = st->priv_data;
5555
5556     if (atom.size < 5) {
5557         av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
5558         return AVERROR_INVALIDDATA;
5559     }
5560
5561     if (sc->stereo3d)
5562         return AVERROR_INVALIDDATA;
5563
5564     avio_skip(pb, 4); /* version + flags */
5565
5566     mode = avio_r8(pb);
5567     switch (mode) {
5568     case 0:
5569         type = AV_STEREO3D_2D;
5570         break;
5571     case 1:
5572         type = AV_STEREO3D_TOPBOTTOM;
5573         break;
5574     case 2:
5575         type = AV_STEREO3D_SIDEBYSIDE;
5576         break;
5577     default:
5578         av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
5579         return 0;
5580     }
5581
5582     sc->stereo3d = av_stereo3d_alloc();
5583     if (!sc->stereo3d)
5584         return AVERROR(ENOMEM);
5585
5586     sc->stereo3d->type = type;
5587     return 0;
5588 }
5589
5590 static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5591 {
5592     AVStream *st;
5593     MOVStreamContext *sc;
5594     int size, version, layout;
5595     int32_t yaw, pitch, roll;
5596     uint32_t l = 0, t = 0, r = 0, b = 0;
5597     uint32_t tag, padding = 0;
5598     enum AVSphericalProjection projection;
5599
5600     if (c->fc->nb_streams < 1)
5601         return 0;
5602
5603     st = c->fc->streams[c->fc->nb_streams - 1];
5604     sc = st->priv_data;
5605
5606     if (atom.size < 8) {
5607         av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
5608         return AVERROR_INVALIDDATA;
5609     }
5610
5611     size = avio_rb32(pb);
5612     if (size <= 12 || size > atom.size)
5613         return AVERROR_INVALIDDATA;
5614
5615     tag = avio_rl32(pb);
5616     if (tag != MKTAG('s','v','h','d')) {
5617         av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
5618         return 0;
5619     }
5620     version = avio_r8(pb);
5621     if (version != 0) {
5622         av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5623                version);
5624         return 0;
5625     }
5626     avio_skip(pb, 3); /* flags */
5627     avio_skip(pb, size - 12); /* metadata_source */
5628
5629     size = avio_rb32(pb);
5630     if (size > atom.size)
5631         return AVERROR_INVALIDDATA;
5632
5633     tag = avio_rl32(pb);
5634     if (tag != MKTAG('p','r','o','j')) {
5635         av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
5636         return 0;
5637     }
5638
5639     size = avio_rb32(pb);
5640     if (size > atom.size)
5641         return AVERROR_INVALIDDATA;
5642
5643     tag = avio_rl32(pb);
5644     if (tag != MKTAG('p','r','h','d')) {
5645         av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
5646         return 0;
5647     }
5648     version = avio_r8(pb);
5649     if (version != 0) {
5650         av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5651                version);
5652         return 0;
5653     }
5654     avio_skip(pb, 3); /* flags */
5655
5656     /* 16.16 fixed point */
5657     yaw   = avio_rb32(pb);
5658     pitch = avio_rb32(pb);
5659     roll  = avio_rb32(pb);
5660
5661     size = avio_rb32(pb);
5662     if (size > atom.size)
5663         return AVERROR_INVALIDDATA;
5664
5665     tag = avio_rl32(pb);
5666     version = avio_r8(pb);
5667     if (version != 0) {
5668         av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5669                version);
5670         return 0;
5671     }
5672     avio_skip(pb, 3); /* flags */
5673     switch (tag) {
5674     case MKTAG('c','b','m','p'):
5675         layout = avio_rb32(pb);
5676         if (layout) {
5677             av_log(c->fc, AV_LOG_WARNING,
5678                    "Unsupported cubemap layout %d\n", layout);
5679             return 0;
5680         }
5681         projection = AV_SPHERICAL_CUBEMAP;
5682         padding = avio_rb32(pb);
5683         break;
5684     case MKTAG('e','q','u','i'):
5685         t = avio_rb32(pb);
5686         b = avio_rb32(pb);
5687         l = avio_rb32(pb);
5688         r = avio_rb32(pb);
5689
5690         if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
5691             av_log(c->fc, AV_LOG_ERROR,
5692                    "Invalid bounding rectangle coordinates "
5693                    "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
5694             return AVERROR_INVALIDDATA;
5695         }
5696
5697         if (l || t || r || b)
5698             projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
5699         else
5700             projection = AV_SPHERICAL_EQUIRECTANGULAR;
5701         break;
5702     default:
5703         av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
5704         return 0;
5705     }
5706
5707     sc->spherical = av_spherical_alloc(&sc->spherical_size);
5708     if (!sc->spherical)
5709         return AVERROR(ENOMEM);
5710
5711     sc->spherical->projection = projection;
5712
5713     sc->spherical->yaw   = yaw;
5714     sc->spherical->pitch = pitch;
5715     sc->spherical->roll  = roll;
5716
5717     sc->spherical->padding = padding;
5718
5719     sc->spherical->bound_left   = l;
5720     sc->spherical->bound_top    = t;
5721     sc->spherical->bound_right  = r;
5722     sc->spherical->bound_bottom = b;
5723
5724     return 0;
5725 }
5726
5727 static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
5728 {
5729     int ret = 0;
5730     uint8_t *buffer = av_malloc(len + 1);
5731     const char *val;
5732
5733     if (!buffer)
5734         return AVERROR(ENOMEM);
5735     buffer[len] = '\0';
5736
5737     ret = ffio_read_size(pb, buffer, len);
5738     if (ret < 0)
5739         goto out;
5740
5741     /* Check for mandatory keys and values, try to support XML as best-effort */
5742     if (!sc->spherical &&
5743         av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
5744         (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
5745         av_stristr(val, "true") &&
5746         (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
5747         av_stristr(val, "true") &&
5748         (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
5749         av_stristr(val, "equirectangular")) {
5750         sc->spherical = av_spherical_alloc(&sc->spherical_size);
5751         if (!sc->spherical)
5752             goto out;
5753
5754         sc->spherical->projection = AV_SPHERICAL_EQUIRECTANGULAR;
5755
5756         if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
5757             enum AVStereo3DType mode;
5758
5759             if (av_stristr(buffer, "left-right"))
5760                 mode = AV_STEREO3D_SIDEBYSIDE;
5761             else if (av_stristr(buffer, "top-bottom"))
5762                 mode = AV_STEREO3D_TOPBOTTOM;
5763             else
5764                 mode = AV_STEREO3D_2D;
5765
5766             sc->stereo3d = av_stereo3d_alloc();
5767             if (!sc->stereo3d)
5768                 goto out;
5769
5770             sc->stereo3d->type = mode;
5771         }
5772
5773         /* orientation */
5774         val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
5775         if (val)
5776             sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
5777         val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
5778         if (val)
5779             sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
5780         val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
5781         if (val)
5782             sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
5783     }
5784
5785 out:
5786     av_free(buffer);
5787     return ret;
5788 }
5789
5790 static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5791 {
5792     AVStream *st;
5793     MOVStreamContext *sc;
5794     int64_t ret;
5795     uint8_t uuid[16];
5796     static const uint8_t uuid_isml_manifest[] = {
5797         0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
5798         0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
5799     };
5800     static const uint8_t uuid_xmp[] = {
5801         0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
5802         0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
5803     };
5804     static const uint8_t uuid_spherical[] = {
5805         0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
5806         0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
5807     };
5808
5809     if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
5810         return AVERROR_INVALIDDATA;
5811
5812     if (c->fc->nb_streams < 1)
5813         return 0;
5814     st = c->fc->streams[c->fc->nb_streams - 1];
5815     sc = st->priv_data;
5816
5817     ret = ffio_read_size(pb, uuid, sizeof(uuid));
5818     if (ret < 0)
5819         return ret;
5820     if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
5821         uint8_t *buffer, *ptr;
5822         char *endptr;
5823         size_t len = atom.size - sizeof(uuid);
5824
5825         if (len < 4) {
5826             return AVERROR_INVALIDDATA;
5827         }
5828         ret = avio_skip(pb, 4); // zeroes
5829         len -= 4;
5830
5831         buffer = av_mallocz(len + 1);
5832         if (!buffer) {
5833             return AVERROR(ENOMEM);
5834         }
5835         ret = ffio_read_size(pb, buffer, len);
5836         if (ret < 0) {
5837             av_free(buffer);
5838             return ret;
5839         }
5840
5841         ptr = buffer;
5842         while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
5843             ptr += sizeof("systemBitrate=\"") - 1;
5844             c->bitrates_count++;
5845             c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
5846             if (!c->bitrates) {
5847                 c->bitrates_count = 0;
5848                 av_free(buffer);
5849                 return AVERROR(ENOMEM);
5850             }
5851             errno = 0;
5852             ret = strtol(ptr, &endptr, 10);
5853             if (ret < 0 || errno || *endptr != '"') {
5854                 c->bitrates[c->bitrates_count - 1] = 0;
5855             } else {
5856                 c->bitrates[c->bitrates_count - 1] = ret;
5857             }
5858         }
5859
5860         av_free(buffer);
5861     } else if (!memcmp(uuid, uuid_xmp, sizeof(uuid))) {
5862         uint8_t *buffer;
5863         size_t len = atom.size - sizeof(uuid);
5864         if (c->export_xmp) {
5865             buffer = av_mallocz(len + 1);
5866             if (!buffer) {
5867                 return AVERROR(ENOMEM);
5868             }
5869             ret = ffio_read_size(pb, buffer, len);
5870             if (ret < 0) {
5871                 av_free(buffer);
5872                 return ret;
5873             }
5874             buffer[len] = '\0';
5875             av_dict_set(&c->fc->metadata, "xmp",
5876                         buffer, AV_DICT_DONT_STRDUP_VAL);
5877         } else {
5878             // skip all uuid atom, which makes it fast for long uuid-xmp file
5879             ret = avio_skip(pb, len);
5880             if (ret < 0)
5881                 return ret;
5882         }
5883     } else if (!memcmp(uuid, uuid_spherical, sizeof(uuid))) {
5884         size_t len = atom.size - sizeof(uuid);
5885         ret = mov_parse_uuid_spherical(sc, pb, len);
5886         if (ret < 0)
5887             return ret;
5888         if (!sc->spherical)
5889             av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
5890     }
5891
5892     return 0;
5893 }
5894
5895 static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5896 {
5897     int ret;
5898     uint8_t content[16];
5899
5900     if (atom.size < 8)
5901         return 0;
5902
5903     ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
5904     if (ret < 0)
5905         return ret;
5906
5907     if (   !c->found_moov
5908         && !c->found_mdat
5909         && !memcmp(content, "Anevia\x1A\x1A", 8)
5910         && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
5911         c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
5912     }
5913
5914     return 0;
5915 }
5916
5917 static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5918 {
5919     uint32_t format = avio_rl32(pb);
5920     MOVStreamContext *sc;
5921     enum AVCodecID id;
5922     AVStream *st;
5923
5924     if (c->fc->nb_streams < 1)
5925         return 0;
5926     st = c->fc->streams[c->fc->nb_streams - 1];
5927     sc = st->priv_data;
5928
5929     switch (sc->format)
5930     {
5931     case MKTAG('e','n','c','v'):        // encrypted video
5932     case MKTAG('e','n','c','a'):        // encrypted audio
5933         id = mov_codec_id(st, format);
5934         if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
5935             st->codecpar->codec_id != id) {
5936             av_log(c->fc, AV_LOG_WARNING,
5937                    "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
5938                    (char*)&format, st->codecpar->codec_id);
5939             break;
5940         }
5941
5942         st->codecpar->codec_id = id;
5943         sc->format = format;
5944         break;
5945
5946     default:
5947         if (format != sc->format) {
5948             av_log(c->fc, AV_LOG_WARNING,
5949                    "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
5950                    (char*)&format, (char*)&sc->format);
5951         }
5952         break;
5953     }
5954
5955     return 0;
5956 }
5957
5958 /**
5959  * Gets the current encryption info and associated current stream context.  If
5960  * we are parsing a track fragment, this will return the specific encryption
5961  * info for this fragment; otherwise this will return the global encryption
5962  * info for the current stream.
5963  */
5964 static int get_current_encryption_info(MOVContext *c, MOVEncryptionIndex **encryption_index, MOVStreamContext **sc)
5965 {
5966     MOVFragmentStreamInfo *frag_stream_info;
5967     AVStream *st;
5968     int i;
5969
5970     frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5971     if (frag_stream_info) {
5972         for (i = 0; i < c->fc->nb_streams; i++) {
5973             if (c->fc->streams[i]->id == frag_stream_info->id) {
5974               st = c->fc->streams[i];
5975               break;
5976             }
5977         }
5978         if (i == c->fc->nb_streams)
5979             return 0;
5980         *sc = st->priv_data;
5981
5982         if (!frag_stream_info->encryption_index) {
5983             // If this stream isn't encrypted, don't create the index.
5984             if (!(*sc)->cenc.default_encrypted_sample)
5985                 return 0;
5986             frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
5987             if (!frag_stream_info->encryption_index)
5988                 return AVERROR(ENOMEM);
5989         }
5990         *encryption_index = frag_stream_info->encryption_index;
5991         return 1;
5992     } else {
5993         // No current track fragment, using stream level encryption info.
5994
5995         if (c->fc->nb_streams < 1)
5996             return 0;
5997         st = c->fc->streams[c->fc->nb_streams - 1];
5998         *sc = st->priv_data;
5999
6000         if (!(*sc)->cenc.encryption_index) {
6001             // If this stream isn't encrypted, don't create the index.
6002             if (!(*sc)->cenc.default_encrypted_sample)
6003                 return 0;
6004             (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
6005             if (!(*sc)->cenc.encryption_index)
6006                 return AVERROR(ENOMEM);
6007         }
6008
6009         *encryption_index = (*sc)->cenc.encryption_index;
6010         return 1;
6011     }
6012 }
6013
6014 static int mov_read_sample_encryption_info(MOVContext *c, AVIOContext *pb, MOVStreamContext *sc, AVEncryptionInfo **sample, int use_subsamples)
6015 {
6016     int i, ret;
6017     unsigned int subsample_count;
6018     AVSubsampleEncryptionInfo *subsamples;
6019
6020     if (!sc->cenc.default_encrypted_sample) {
6021         av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
6022         return AVERROR_INVALIDDATA;
6023     }
6024
6025     *sample = av_encryption_info_clone(sc->cenc.default_encrypted_sample);
6026     if (!*sample)
6027         return AVERROR(ENOMEM);
6028
6029     if (sc->cenc.per_sample_iv_size != 0) {
6030         if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
6031             av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
6032             av_encryption_info_free(*sample);
6033             *sample = NULL;
6034             return ret;
6035         }
6036     }
6037
6038     if (use_subsamples) {
6039         subsample_count = avio_rb16(pb);
6040         av_free((*sample)->subsamples);
6041         (*sample)->subsamples = av_mallocz_array(subsample_count, sizeof(*subsamples));
6042         if (!(*sample)->subsamples) {
6043             av_encryption_info_free(*sample);
6044             *sample = NULL;
6045             return AVERROR(ENOMEM);
6046         }
6047
6048         for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
6049             (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
6050             (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
6051         }
6052
6053         if (pb->eof_reached) {
6054             av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
6055             av_encryption_info_free(*sample);
6056             *sample = NULL;
6057             return AVERROR_INVALIDDATA;
6058         }
6059         (*sample)->subsample_count = subsample_count;
6060     }
6061
6062     return 0;
6063 }
6064
6065 static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6066 {
6067     AVEncryptionInfo **encrypted_samples;
6068     MOVEncryptionIndex *encryption_index;
6069     MOVStreamContext *sc;
6070     int use_subsamples, ret;
6071     unsigned int sample_count, i, alloc_size = 0;
6072
6073     ret = get_current_encryption_info(c, &encryption_index, &sc);
6074     if (ret != 1)
6075         return ret;
6076
6077     if (encryption_index->nb_encrypted_samples) {
6078         // This can happen if we have both saio/saiz and senc atoms.
6079         av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
6080         return 0;
6081     }
6082
6083     avio_r8(pb); /* version */
6084     use_subsamples = avio_rb24(pb) & 0x02; /* flags */
6085
6086     sample_count = avio_rb32(pb);
6087     if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
6088         return AVERROR(ENOMEM);
6089
6090     for (i = 0; i < sample_count; i++) {
6091         unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
6092         encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
6093                                             min_samples * sizeof(*encrypted_samples));
6094         if (encrypted_samples) {
6095             encryption_index->encrypted_samples = encrypted_samples;
6096
6097             ret = mov_read_sample_encryption_info(
6098                 c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
6099         } else {
6100             ret = AVERROR(ENOMEM);
6101         }
6102         if (pb->eof_reached) {
6103             av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
6104             ret = AVERROR_INVALIDDATA;
6105         }
6106
6107         if (ret < 0) {
6108             for (; i > 0; i--)
6109                 av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
6110             av_freep(&encryption_index->encrypted_samples);
6111             return ret;
6112         }
6113     }
6114     encryption_index->nb_encrypted_samples = sample_count;
6115
6116     return 0;
6117 }
6118
6119 static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
6120 {
6121     AVEncryptionInfo **sample, **encrypted_samples;
6122     int64_t prev_pos;
6123     size_t sample_count, sample_info_size, i;
6124     int ret = 0;
6125     unsigned int alloc_size = 0;
6126
6127     if (encryption_index->nb_encrypted_samples)
6128         return 0;
6129     sample_count = encryption_index->auxiliary_info_sample_count;
6130     if (encryption_index->auxiliary_offsets_count != 1) {
6131         av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
6132         return AVERROR_PATCHWELCOME;
6133     }
6134     if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
6135         return AVERROR(ENOMEM);
6136
6137     prev_pos = avio_tell(pb);
6138     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
6139         avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
6140         av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
6141         goto finish;
6142     }
6143
6144     for (i = 0; i < sample_count && !pb->eof_reached; i++) {
6145         unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
6146         encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
6147                                             min_samples * sizeof(*encrypted_samples));
6148         if (!encrypted_samples) {
6149             ret = AVERROR(ENOMEM);
6150             goto finish;
6151         }
6152         encryption_index->encrypted_samples = encrypted_samples;
6153
6154         sample = &encryption_index->encrypted_samples[i];
6155         sample_info_size = encryption_index->auxiliary_info_default_size
6156                                ? encryption_index->auxiliary_info_default_size
6157                                : encryption_index->auxiliary_info_sizes[i];
6158
6159         ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
6160         if (ret < 0)
6161             goto finish;
6162     }
6163     if (pb->eof_reached) {
6164         av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
6165         ret = AVERROR_INVALIDDATA;
6166     } else {
6167         encryption_index->nb_encrypted_samples = sample_count;
6168     }
6169
6170 finish:
6171     avio_seek(pb, prev_pos, SEEK_SET);
6172     if (ret < 0) {
6173         for (; i > 0; i--) {
6174             av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
6175         }
6176         av_freep(&encryption_index->encrypted_samples);
6177     }
6178     return ret;
6179 }
6180
6181 /**
6182  * Tries to read the given number of bytes from the stream and puts it in a
6183  * newly allocated buffer.  This reads in small chunks to avoid allocating large
6184  * memory if the file contains an invalid/malicious size value.
6185  */
6186 static int mov_try_read_block(AVIOContext *pb, size_t size, uint8_t **data)
6187 {
6188     const unsigned int block_size = 1024 * 1024;
6189     uint8_t *buffer = NULL;
6190     unsigned int alloc_size = 0, offset = 0;
6191     while (offset < size) {
6192         unsigned int new_size =
6193             alloc_size >= INT_MAX - block_size ? INT_MAX : alloc_size + block_size;
6194         uint8_t *new_buffer = av_fast_realloc(buffer, &alloc_size, new_size);
6195         unsigned int to_read = FFMIN(size, alloc_size) - offset;
6196         if (!new_buffer) {
6197             av_free(buffer);
6198             return AVERROR(ENOMEM);
6199         }
6200         buffer = new_buffer;
6201
6202         if (avio_read(pb, buffer + offset, to_read) != to_read) {
6203             av_free(buffer);
6204             return AVERROR_INVALIDDATA;
6205         }
6206         offset += to_read;
6207     }
6208
6209     *data = buffer;
6210     return 0;
6211 }
6212
6213 static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6214 {
6215     MOVEncryptionIndex *encryption_index;
6216     MOVStreamContext *sc;
6217     int ret;
6218     unsigned int sample_count, aux_info_type, aux_info_param;
6219
6220     ret = get_current_encryption_info(c, &encryption_index, &sc);
6221     if (ret != 1)
6222         return ret;
6223
6224     if (encryption_index->nb_encrypted_samples) {
6225         // This can happen if we have both saio/saiz and senc atoms.
6226         av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
6227         return 0;
6228     }
6229
6230     if (encryption_index->auxiliary_info_sample_count) {
6231         av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
6232         return AVERROR_INVALIDDATA;
6233     }
6234
6235     avio_r8(pb); /* version */
6236     if (avio_rb24(pb) & 0x01) {  /* flags */
6237         aux_info_type = avio_rb32(pb);
6238         aux_info_param = avio_rb32(pb);
6239         if (sc->cenc.default_encrypted_sample) {
6240             if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6241                 av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
6242                 return 0;
6243             }
6244             if (aux_info_param != 0) {
6245                 av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
6246                 return 0;
6247             }
6248         } else {
6249             // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6250             if ((aux_info_type == MKBETAG('c','e','n','c') ||
6251                  aux_info_type == MKBETAG('c','e','n','s') ||
6252                  aux_info_type == MKBETAG('c','b','c','1') ||
6253                  aux_info_type == MKBETAG('c','b','c','s')) &&
6254                 aux_info_param == 0) {
6255                 av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
6256                 return AVERROR_INVALIDDATA;
6257             } else {
6258                 return 0;
6259             }
6260         }
6261     } else if (!sc->cenc.default_encrypted_sample) {
6262         // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6263         return 0;
6264     }
6265
6266     encryption_index->auxiliary_info_default_size = avio_r8(pb);
6267     sample_count = avio_rb32(pb);
6268     encryption_index->auxiliary_info_sample_count = sample_count;
6269
6270     if (encryption_index->auxiliary_info_default_size == 0) {
6271         ret = mov_try_read_block(pb, sample_count, &encryption_index->auxiliary_info_sizes);
6272         if (ret < 0) {
6273             av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info\n");
6274             return ret;
6275         }
6276     }
6277
6278     if (encryption_index->auxiliary_offsets_count) {
6279         return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6280     }
6281
6282     return 0;
6283 }
6284
6285 static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6286 {
6287     uint64_t *auxiliary_offsets;
6288     MOVEncryptionIndex *encryption_index;
6289     MOVStreamContext *sc;
6290     int i, ret;
6291     unsigned int version, entry_count, aux_info_type, aux_info_param;
6292     unsigned int alloc_size = 0;
6293
6294     ret = get_current_encryption_info(c, &encryption_index, &sc);
6295     if (ret != 1)
6296         return ret;
6297
6298     if (encryption_index->nb_encrypted_samples) {
6299         // This can happen if we have both saio/saiz and senc atoms.
6300         av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
6301         return 0;
6302     }
6303
6304     if (encryption_index->auxiliary_offsets_count) {
6305         av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
6306         return AVERROR_INVALIDDATA;
6307     }
6308
6309     version = avio_r8(pb); /* version */
6310     if (avio_rb24(pb) & 0x01) {  /* flags */
6311         aux_info_type = avio_rb32(pb);
6312         aux_info_param = avio_rb32(pb);
6313         if (sc->cenc.default_encrypted_sample) {
6314             if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6315                 av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
6316                 return 0;
6317             }
6318             if (aux_info_param != 0) {
6319                 av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
6320                 return 0;
6321             }
6322         } else {
6323             // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6324             if ((aux_info_type == MKBETAG('c','e','n','c') ||
6325                  aux_info_type == MKBETAG('c','e','n','s') ||
6326                  aux_info_type == MKBETAG('c','b','c','1') ||
6327                  aux_info_type == MKBETAG('c','b','c','s')) &&
6328                 aux_info_param == 0) {
6329                 av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
6330                 return AVERROR_INVALIDDATA;
6331             } else {
6332                 return 0;
6333             }
6334         }
6335     } else if (!sc->cenc.default_encrypted_sample) {
6336         // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6337         return 0;
6338     }
6339
6340     entry_count = avio_rb32(pb);
6341     if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
6342         return AVERROR(ENOMEM);
6343
6344     for (i = 0; i < entry_count && !pb->eof_reached; i++) {
6345         unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
6346         auxiliary_offsets = av_fast_realloc(
6347             encryption_index->auxiliary_offsets, &alloc_size,
6348             min_offsets * sizeof(*auxiliary_offsets));
6349         if (!auxiliary_offsets) {
6350             av_freep(&encryption_index->auxiliary_offsets);
6351             return AVERROR(ENOMEM);
6352         }
6353         encryption_index->auxiliary_offsets = auxiliary_offsets;
6354
6355         if (version == 0) {
6356             encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
6357         } else {
6358             encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
6359         }
6360         if (c->frag_index.current >= 0) {
6361             encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
6362         }
6363     }
6364
6365     if (pb->eof_reached) {
6366         av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
6367         av_freep(&encryption_index->auxiliary_offsets);
6368         return AVERROR_INVALIDDATA;
6369     }
6370
6371     encryption_index->auxiliary_offsets_count = entry_count;
6372
6373     if (encryption_index->auxiliary_info_sample_count) {
6374         return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6375     }
6376
6377     return 0;
6378 }
6379
6380 static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6381 {
6382     AVEncryptionInitInfo *info, *old_init_info;
6383     uint8_t **key_ids;
6384     AVStream *st;
6385     uint8_t *side_data, *extra_data, *old_side_data;
6386     size_t side_data_size;
6387     int ret = 0, old_side_data_size;
6388     unsigned int version, kid_count, extra_data_size, alloc_size = 0;
6389
6390     if (c->fc->nb_streams < 1)
6391         return 0;
6392     st = c->fc->streams[c->fc->nb_streams-1];
6393
6394     version = avio_r8(pb); /* version */
6395     avio_rb24(pb);  /* flags */
6396
6397     info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
6398                                          /* key_id_size */ 16, /* data_size */ 0);
6399     if (!info)
6400         return AVERROR(ENOMEM);
6401
6402     if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
6403         av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
6404         goto finish;
6405     }
6406
6407     if (version > 0) {
6408         kid_count = avio_rb32(pb);
6409         if (kid_count >= INT_MAX / sizeof(*key_ids)) {
6410             ret = AVERROR(ENOMEM);
6411             goto finish;
6412         }
6413
6414         for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
6415             unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
6416             key_ids = av_fast_realloc(info->key_ids, &alloc_size,
6417                                       min_kid_count * sizeof(*key_ids));
6418             if (!key_ids) {
6419                 ret = AVERROR(ENOMEM);
6420                 goto finish;
6421             }
6422             info->key_ids = key_ids;
6423
6424             info->key_ids[i] = av_mallocz(16);
6425             if (!info->key_ids[i]) {
6426                 ret = AVERROR(ENOMEM);
6427                 goto finish;
6428             }
6429             info->num_key_ids = i + 1;
6430
6431             if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
6432                 av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
6433                 goto finish;
6434             }
6435         }
6436
6437         if (pb->eof_reached) {
6438             av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
6439             ret = AVERROR_INVALIDDATA;
6440             goto finish;
6441         }
6442     }
6443
6444     extra_data_size = avio_rb32(pb);
6445     ret = mov_try_read_block(pb, extra_data_size, &extra_data);
6446     if (ret < 0)
6447         goto finish;
6448
6449     av_freep(&info->data);  // malloc(0) may still allocate something.
6450     info->data = extra_data;
6451     info->data_size = extra_data_size;
6452
6453     // If there is existing initialization data, append to the list.
6454     old_side_data = av_stream_get_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
6455     if (old_side_data) {
6456         old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
6457         if (old_init_info) {
6458             // Append to the end of the list.
6459             for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
6460                 if (!cur->next) {
6461                     cur->next = info;
6462                     break;
6463                 }
6464             }
6465             info = old_init_info;
6466         } else {
6467             // Assume existing side-data will be valid, so the only error we could get is OOM.
6468             ret = AVERROR(ENOMEM);
6469             goto finish;
6470         }
6471     }
6472
6473     side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
6474     if (!side_data) {
6475         ret = AVERROR(ENOMEM);
6476         goto finish;
6477     }
6478     ret = av_stream_add_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO,
6479                                   side_data, side_data_size);
6480     if (ret < 0)
6481         av_free(side_data);
6482
6483 finish:
6484     av_encryption_init_info_free(info);
6485     return ret;
6486 }
6487
6488 static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6489 {
6490     AVStream *st;
6491     MOVStreamContext *sc;
6492
6493     if (c->fc->nb_streams < 1)
6494         return 0;
6495     st = c->fc->streams[c->fc->nb_streams-1];
6496     sc = st->priv_data;
6497
6498     if (sc->pseudo_stream_id != 0) {
6499         av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
6500         return AVERROR_PATCHWELCOME;
6501     }
6502
6503     if (atom.size < 8)
6504         return AVERROR_INVALIDDATA;
6505
6506     avio_rb32(pb); /* version and flags */
6507
6508     if (!sc->cenc.default_encrypted_sample) {
6509         sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
6510         if (!sc->cenc.default_encrypted_sample) {
6511             return AVERROR(ENOMEM);
6512         }
6513     }
6514
6515     sc->cenc.default_encrypted_sample->scheme = avio_rb32(pb);
6516     return 0;
6517 }
6518
6519 static int mov_read_tenc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6520 {
6521     AVStream *st;
6522     MOVStreamContext *sc;
6523     unsigned int version, pattern, is_protected, iv_size;
6524
6525     if (c->fc->nb_streams < 1)
6526         return 0;
6527     st = c->fc->streams[c->fc->nb_streams-1];
6528     sc = st->priv_data;
6529
6530     if (sc->pseudo_stream_id != 0) {
6531         av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
6532         return AVERROR_PATCHWELCOME;
6533     }
6534
6535     if (!sc->cenc.default_encrypted_sample) {
6536         sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
6537         if (!sc->cenc.default_encrypted_sample) {
6538             return AVERROR(ENOMEM);
6539         }
6540     }
6541
6542     if (atom.size < 20)
6543         return AVERROR_INVALIDDATA;
6544
6545     version = avio_r8(pb); /* version */
6546     avio_rb24(pb); /* flags */
6547
6548     avio_r8(pb); /* reserved */
6549     pattern = avio_r8(pb);
6550
6551     if (version > 0) {
6552         sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
6553         sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
6554     }
6555
6556     is_protected = avio_r8(pb);
6557     if (is_protected && !sc->cenc.encryption_index) {
6558         // The whole stream should be by-default encrypted.
6559         sc->cenc.encryption_index = av_mallocz(sizeof(MOVEncryptionIndex));
6560         if (!sc->cenc.encryption_index)
6561             return AVERROR(ENOMEM);
6562     }
6563     sc->cenc.per_sample_iv_size = avio_r8(pb);
6564     if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
6565         sc->cenc.per_sample_iv_size != 16) {
6566         av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
6567         return AVERROR_INVALIDDATA;
6568     }
6569     if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
6570         av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
6571         return AVERROR_INVALIDDATA;
6572     }
6573
6574     if (is_protected && !sc->cenc.per_sample_iv_size) {
6575         iv_size = avio_r8(pb);
6576         if (iv_size != 8 && iv_size != 16) {
6577             av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
6578             return AVERROR_INVALIDDATA;
6579         }
6580
6581         if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
6582             av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
6583             return AVERROR_INVALIDDATA;
6584         }
6585     }
6586
6587     return 0;
6588 }
6589
6590 static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6591 {
6592     AVStream *st;
6593     int last, type, size, ret;
6594     uint8_t buf[4];
6595
6596     if (c->fc->nb_streams < 1)
6597         return 0;
6598     st = c->fc->streams[c->fc->nb_streams-1];
6599
6600     if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
6601         return AVERROR_INVALIDDATA;
6602
6603     /* Check FlacSpecificBox version. */
6604     if (avio_r8(pb) != 0)
6605         return AVERROR_INVALIDDATA;
6606
6607     avio_rb24(pb); /* Flags */
6608
6609     avio_read(pb, buf, sizeof(buf));
6610     flac_parse_block_header(buf, &last, &type, &size);
6611
6612     if (type != FLAC_METADATA_TYPE_STREAMINFO || size != FLAC_STREAMINFO_SIZE) {
6613         av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
6614         return AVERROR_INVALIDDATA;
6615     }
6616
6617     ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
6618     if (ret < 0)
6619         return ret;
6620
6621     if (!last)
6622         av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
6623
6624     return 0;
6625 }
6626
6627 static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
6628 {
6629     int i, ret;
6630
6631     if (sample->scheme != MKBETAG('c','e','n','c') || sample->crypt_byte_block != 0 || sample->skip_byte_block != 0) {
6632         av_log(c->fc, AV_LOG_ERROR, "Only the 'cenc' encryption scheme is supported\n");
6633         return AVERROR_PATCHWELCOME;
6634     }
6635
6636     if (!sc->cenc.aes_ctr) {
6637         /* initialize the cipher */
6638         sc->cenc.aes_ctr = av_aes_ctr_alloc();
6639         if (!sc->cenc.aes_ctr) {
6640             return AVERROR(ENOMEM);
6641         }
6642
6643         ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
6644         if (ret < 0) {
6645             return ret;
6646         }
6647     }
6648
6649     av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
6650
6651     if (!sample->subsample_count) {
6652         /* decrypt the whole packet */
6653         av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
6654         return 0;
6655     }
6656
6657     for (i = 0; i < sample->subsample_count; i++) {
6658         if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
6659             av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
6660             return AVERROR_INVALIDDATA;
6661         }
6662
6663         /* skip the clear bytes */
6664         input += sample->subsamples[i].bytes_of_clear_data;
6665         size -= sample->subsamples[i].bytes_of_clear_data;
6666
6667         /* decrypt the encrypted bytes */
6668         av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, sample->subsamples[i].bytes_of_protected_data);
6669         input += sample->subsamples[i].bytes_of_protected_data;
6670         size -= sample->subsamples[i].bytes_of_protected_data;
6671     }
6672
6673     if (size > 0) {
6674         av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
6675         return AVERROR_INVALIDDATA;
6676     }
6677
6678     return 0;
6679 }
6680
6681 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
6682 {
6683     MOVFragmentStreamInfo *frag_stream_info;
6684     MOVEncryptionIndex *encryption_index;
6685     AVEncryptionInfo *encrypted_sample;
6686     int encrypted_index, ret;
6687
6688     frag_stream_info = get_frag_stream_info(&mov->frag_index, mov->frag_index.current, st->id);
6689     encrypted_index = current_index;
6690     encryption_index = NULL;
6691     if (frag_stream_info) {
6692         // Note this only supports encryption info in the first sample descriptor.
6693         if (mov->fragment.stsd_id == 1) {
6694             if (frag_stream_info->encryption_index) {
6695                 encrypted_index = current_index - frag_stream_info->index_entry;
6696                 encryption_index = frag_stream_info->encryption_index;
6697             } else {
6698                 encryption_index = sc->cenc.encryption_index;
6699             }
6700         }
6701     } else {
6702         encryption_index = sc->cenc.encryption_index;
6703     }
6704
6705     if (encryption_index) {
6706         if (encryption_index->auxiliary_info_sample_count &&
6707             !encryption_index->nb_encrypted_samples) {
6708             av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
6709             return AVERROR_INVALIDDATA;
6710         }
6711         if (encryption_index->auxiliary_offsets_count &&
6712             !encryption_index->nb_encrypted_samples) {
6713             av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
6714             return AVERROR_INVALIDDATA;
6715         }
6716
6717         if (!encryption_index->nb_encrypted_samples) {
6718             // Full-sample encryption with default settings.
6719             encrypted_sample = sc->cenc.default_encrypted_sample;
6720         } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
6721             // Per-sample setting override.
6722             encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
6723         } else {
6724             av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
6725             return AVERROR_INVALIDDATA;
6726         }
6727
6728         if (mov->decryption_key) {
6729             return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
6730         } else {
6731             size_t size;
6732             uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
6733             if (!side_data)
6734                 return AVERROR(ENOMEM);
6735             ret = av_packet_add_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, side_data, size);
6736             if (ret < 0)
6737                 av_free(side_data);
6738             return ret;
6739         }
6740     }
6741
6742     return 0;
6743 }
6744
6745 static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6746 {
6747     const int OPUS_SEEK_PREROLL_MS = 80;
6748     int ret;
6749     AVStream *st;
6750     size_t size;
6751     uint16_t pre_skip;
6752
6753     if (c->fc->nb_streams < 1)
6754         return 0;
6755     st = c->fc->streams[c->fc->nb_streams-1];
6756
6757     if ((uint64_t)atom.size > (1<<30) || atom.size < 11)
6758         return AVERROR_INVALIDDATA;
6759
6760     /* Check OpusSpecificBox version. */
6761     if (avio_r8(pb) != 0) {
6762         av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
6763         return AVERROR_INVALIDDATA;
6764     }
6765
6766     /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
6767     size = atom.size + 8;
6768
6769     if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
6770         return ret;
6771
6772     AV_WL32(st->codecpar->extradata, MKTAG('O','p','u','s'));
6773     AV_WL32(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
6774     AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
6775     avio_read(pb, st->codecpar->extradata + 9, size - 9);
6776
6777     /* OpusSpecificBox is stored in big-endian, but OpusHead is
6778        little-endian; aside from the preceeding magic and version they're
6779        otherwise currently identical.  Data after output gain at offset 16
6780        doesn't need to be bytewapped. */
6781     pre_skip = AV_RB16(st->codecpar->extradata + 10);
6782     AV_WL16(st->codecpar->extradata + 10, pre_skip);
6783     AV_WL32(st->codecpar->extradata + 12, AV_RB32(st->codecpar->extradata + 12));
6784     AV_WL16(st->codecpar->extradata + 16, AV_RB16(st->codecpar->extradata + 16));
6785
6786     st->codecpar->initial_padding = pre_skip;
6787     st->codecpar->seek_preroll = av_rescale_q(OPUS_SEEK_PREROLL_MS,
6788                                               (AVRational){1, 1000},
6789                                               (AVRational){1, 48000});
6790
6791     return 0;
6792 }
6793
6794 static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6795 {
6796     AVStream *st;
6797     unsigned format_info;
6798     int channel_assignment, channel_assignment1, channel_assignment2;
6799     int ratebits;
6800
6801     if (c->fc->nb_streams < 1)
6802         return 0;
6803     st = c->fc->streams[c->fc->nb_streams-1];
6804
6805     if (atom.size < 10)
6806         return AVERROR_INVALIDDATA;
6807
6808     format_info = avio_rb32(pb);
6809
6810     ratebits            = (format_info >> 28) & 0xF;
6811     channel_assignment1 = (format_info >> 15) & 0x1F;
6812     channel_assignment2 = format_info & 0x1FFF;
6813     if (channel_assignment2)
6814         channel_assignment = channel_assignment2;
6815     else
6816         channel_assignment = channel_assignment1;
6817
6818     st->codecpar->frame_size = 40 << (ratebits & 0x7);
6819     st->codecpar->sample_rate = mlp_samplerate(ratebits);
6820     st->codecpar->channels = truehd_channels(channel_assignment);
6821     st->codecpar->channel_layout = truehd_layout(channel_assignment);
6822
6823     return 0;
6824 }
6825
6826 static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6827 {
6828     AVStream *st;
6829     uint32_t buf;
6830     AVDOVIDecoderConfigurationRecord *dovi;
6831     size_t dovi_size;
6832     int ret;
6833
6834     if (c->fc->nb_streams < 1)
6835         return 0;
6836     st = c->fc->streams[c->fc->nb_streams-1];
6837
6838     if ((uint64_t)atom.size > (1<<30) || atom.size < 4)
6839         return AVERROR_INVALIDDATA;
6840
6841     dovi = av_dovi_alloc(&dovi_size);
6842     if (!dovi)
6843         return AVERROR(ENOMEM);
6844
6845     dovi->dv_version_major = avio_r8(pb);
6846     dovi->dv_version_minor = avio_r8(pb);
6847
6848     buf = avio_rb16(pb);
6849     dovi->dv_profile        = (buf >> 9) & 0x7f;    // 7 bits
6850     dovi->dv_level          = (buf >> 3) & 0x3f;    // 6 bits
6851     dovi->rpu_present_flag  = (buf >> 2) & 0x01;    // 1 bit
6852     dovi->el_present_flag   = (buf >> 1) & 0x01;    // 1 bit
6853     dovi->bl_present_flag   =  buf       & 0x01;    // 1 bit
6854     if (atom.size >= 24) {  // 4 + 4 + 4 * 4
6855         buf = avio_r8(pb);
6856         dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
6857     } else {
6858         // 0 stands for None
6859         // Dolby Vision V1.2.93 profiles and levels
6860         dovi->dv_bl_signal_compatibility_id = 0;
6861     }
6862
6863     ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
6864                                   (uint8_t *)dovi, dovi_size);
6865     if (ret < 0) {
6866         av_free(dovi);
6867         return ret;
6868     }
6869
6870     av_log(c, AV_LOG_TRACE, "DOVI in dvcC/dvvC box, version: %d.%d, profile: %d, level: %d, "
6871            "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
6872            dovi->dv_version_major, dovi->dv_version_minor,
6873            dovi->dv_profile, dovi->dv_level,
6874            dovi->rpu_present_flag,
6875            dovi->el_present_flag,
6876            dovi->bl_present_flag,
6877            dovi->dv_bl_signal_compatibility_id
6878         );
6879
6880     return 0;
6881 }
6882
6883 static const MOVParseTableEntry mov_default_parse_table[] = {
6884 { MKTAG('A','C','L','R'), mov_read_aclr },
6885 { MKTAG('A','P','R','G'), mov_read_avid },
6886 { MKTAG('A','A','L','P'), mov_read_avid },
6887 { MKTAG('A','R','E','S'), mov_read_ares },
6888 { MKTAG('a','v','s','s'), mov_read_avss },
6889 { MKTAG('a','v','1','C'), mov_read_av1c },
6890 { MKTAG('c','h','p','l'), mov_read_chpl },
6891 { MKTAG('c','o','6','4'), mov_read_stco },
6892 { MKTAG('c','o','l','r'), mov_read_colr },
6893 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
6894 { MKTAG('d','i','n','f'), mov_read_default },
6895 { MKTAG('D','p','x','E'), mov_read_dpxe },
6896 { MKTAG('d','r','e','f'), mov_read_dref },
6897 { MKTAG('e','d','t','s'), mov_read_default },
6898 { MKTAG('e','l','s','t'), mov_read_elst },
6899 { MKTAG('e','n','d','a'), mov_read_enda },
6900 { MKTAG('f','i','e','l'), mov_read_fiel },
6901 { MKTAG('a','d','r','m'), mov_read_adrm },
6902 { MKTAG('f','t','y','p'), mov_read_ftyp },
6903 { MKTAG('g','l','b','l'), mov_read_glbl },
6904 { MKTAG('h','d','l','r'), mov_read_hdlr },
6905 { MKTAG('i','l','s','t'), mov_read_ilst },
6906 { MKTAG('j','p','2','h'), mov_read_jp2h },
6907 { MKTAG('m','d','a','t'), mov_read_mdat },
6908 { MKTAG('m','d','h','d'), mov_read_mdhd },
6909 { MKTAG('m','d','i','a'), mov_read_default },
6910 { MKTAG('m','e','t','a'), mov_read_meta },
6911 { MKTAG('m','i','n','f'), mov_read_default },
6912 { MKTAG('m','o','o','f'), mov_read_moof },
6913 { MKTAG('m','o','o','v'), mov_read_moov },
6914 { MKTAG('m','v','e','x'), mov_read_default },
6915 { MKTAG('m','v','h','d'), mov_read_mvhd },
6916 { MKTAG('S','M','I',' '), mov_read_svq3 },
6917 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
6918 { MKTAG('a','v','c','C'), mov_read_glbl },
6919 { MKTAG('p','a','s','p'), mov_read_pasp },
6920 { MKTAG('s','i','d','x'), mov_read_sidx },
6921 { MKTAG('s','t','b','l'), mov_read_default },
6922 { MKTAG('s','t','c','o'), mov_read_stco },
6923 { MKTAG('s','t','p','s'), mov_read_stps },
6924 { MKTAG('s','t','r','f'), mov_read_strf },
6925 { MKTAG('s','t','s','c'), mov_read_stsc },
6926 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
6927 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
6928 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
6929 { MKTAG('s','t','t','s'), mov_read_stts },
6930 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
6931 { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
6932 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
6933 { MKTAG('t','f','d','t'), mov_read_tfdt },
6934 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
6935 { MKTAG('t','r','a','k'), mov_read_trak },
6936 { MKTAG('t','r','a','f'), mov_read_default },
6937 { MKTAG('t','r','e','f'), mov_read_default },
6938 { MKTAG('t','m','c','d'), mov_read_tmcd },
6939 { MKTAG('c','h','a','p'), mov_read_chap },
6940 { MKTAG('t','r','e','x'), mov_read_trex },
6941 { MKTAG('t','r','u','n'), mov_read_trun },
6942 { MKTAG('u','d','t','a'), mov_read_default },
6943 { MKTAG('w','a','v','e'), mov_read_wave },
6944 { MKTAG('e','s','d','s'), mov_read_esds },
6945 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
6946 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
6947 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
6948 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
6949 { MKTAG('w','f','e','x'), mov_read_wfex },
6950 { MKTAG('c','m','o','v'), mov_read_cmov },
6951 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
6952 { MKTAG('d','v','c','1'), mov_read_dvc1 },
6953 { MKTAG('s','b','g','p'), mov_read_sbgp },
6954 { MKTAG('h','v','c','C'), mov_read_glbl },
6955 { MKTAG('u','u','i','d'), mov_read_uuid },
6956 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
6957 { MKTAG('f','r','e','e'), mov_read_free },
6958 { MKTAG('-','-','-','-'), mov_read_custom },
6959 { MKTAG('s','i','n','f'), mov_read_default },
6960 { MKTAG('f','r','m','a'), mov_read_frma },
6961 { MKTAG('s','e','n','c'), mov_read_senc },
6962 { MKTAG('s','a','i','z'), mov_read_saiz },
6963 { MKTAG('s','a','i','o'), mov_read_saio },
6964 { MKTAG('p','s','s','h'), mov_read_pssh },
6965 { MKTAG('s','c','h','m'), mov_read_schm },
6966 { MKTAG('s','c','h','i'), mov_read_default },
6967 { MKTAG('t','e','n','c'), mov_read_tenc },
6968 { MKTAG('d','f','L','a'), mov_read_dfla },
6969 { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
6970 { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
6971 { MKTAG('d','O','p','s'), mov_read_dops },
6972 { MKTAG('d','m','l','p'), mov_read_dmlp },
6973 { MKTAG('S','m','D','m'), mov_read_smdm },
6974 { MKTAG('C','o','L','L'), mov_read_coll },
6975 { MKTAG('v','p','c','C'), mov_read_vpcc },
6976 { MKTAG('m','d','c','v'), mov_read_mdcv },
6977 { MKTAG('c','l','l','i'), mov_read_clli },
6978 { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
6979 { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
6980 { 0, NULL }
6981 };
6982
6983 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6984 {
6985     int64_t total_size = 0;
6986     MOVAtom a;
6987     int i;
6988
6989     if (c->atom_depth > 10) {
6990         av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
6991         return AVERROR_INVALIDDATA;
6992     }
6993     c->atom_depth ++;
6994
6995     if (atom.size < 0)
6996         atom.size = INT64_MAX;
6997     while (total_size <= atom.size - 8 && !avio_feof(pb)) {
6998         int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
6999         a.size = atom.size;
7000         a.type=0;
7001         if (atom.size >= 8) {
7002             a.size = avio_rb32(pb);
7003             a.type = avio_rl32(pb);
7004             if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
7005                   a.type == MKTAG('h','o','o','v')) &&
7006                 a.size >= 8 &&
7007                 c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
7008                 uint32_t type;
7009                 avio_skip(pb, 4);
7010                 type = avio_rl32(pb);
7011                 if (avio_feof(pb))
7012                     break;
7013                 avio_seek(pb, -8, SEEK_CUR);
7014                 if (type == MKTAG('m','v','h','d') ||
7015                     type == MKTAG('c','m','o','v')) {
7016                     av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
7017                     a.type = MKTAG('m','o','o','v');
7018                 }
7019             }
7020             if (atom.type != MKTAG('r','o','o','t') &&
7021                 atom.type != MKTAG('m','o','o','v')) {
7022                 if (a.type == MKTAG('t','r','a','k') ||
7023                     a.type == MKTAG('m','d','a','t')) {
7024                     av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
7025                     avio_skip(pb, -8);
7026                     c->atom_depth --;
7027                     return 0;
7028                 }
7029             }
7030             total_size += 8;
7031             if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
7032                 a.size = avio_rb64(pb) - 8;
7033                 total_size += 8;
7034             }
7035         }
7036         av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
7037                av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
7038         if (a.size == 0) {
7039             a.size = atom.size - total_size + 8;
7040         }
7041         a.size -= 8;
7042         if (a.size < 0)
7043             break;
7044         a.size = FFMIN(a.size, atom.size - total_size);
7045
7046         for (i = 0; mov_default_parse_table[i].type; i++)
7047             if (mov_default_parse_table[i].type == a.type) {
7048                 parse = mov_default_parse_table[i].parse;
7049                 break;
7050             }
7051
7052         // container is user data
7053         if (!parse && (atom.type == MKTAG('u','d','t','a') ||
7054                        atom.type == MKTAG('i','l','s','t')))
7055             parse = mov_read_udta_string;
7056
7057         // Supports parsing the QuickTime Metadata Keys.
7058         // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
7059         if (!parse && c->found_hdlr_mdta &&
7060             atom.type == MKTAG('m','e','t','a') &&
7061             a.type == MKTAG('k','e','y','s') &&
7062             c->meta_keys_count == 0) {
7063             parse = mov_read_keys;
7064         }
7065
7066         if (!parse) { /* skip leaf atoms data */
7067             avio_skip(pb, a.size);
7068         } else {
7069             int64_t start_pos = avio_tell(pb);
7070             int64_t left;
7071             int err = parse(c, pb, a);
7072             if (err < 0) {
7073                 c->atom_depth --;
7074                 return err;
7075             }
7076             if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
7077                 ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
7078                  start_pos + a.size == avio_size(pb))) {
7079                 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
7080                     c->next_root_atom = start_pos + a.size;
7081                 c->atom_depth --;
7082                 return 0;
7083             }
7084             left = a.size - avio_tell(pb) + start_pos;
7085             if (left > 0) /* skip garbage at atom end */
7086                 avio_skip(pb, left);
7087             else if (left < 0) {
7088                 av_log(c->fc, AV_LOG_WARNING,
7089                        "overread end of atom '%s' by %"PRId64" bytes\n",
7090                        av_fourcc2str(a.type), -left);
7091                 avio_seek(pb, left, SEEK_CUR);
7092             }
7093         }
7094
7095         total_size += a.size;
7096     }
7097
7098     if (total_size < atom.size && atom.size < 0x7ffff)
7099         avio_skip(pb, atom.size - total_size);
7100
7101     c->atom_depth --;
7102     return 0;
7103 }
7104
7105 static int mov_probe(const AVProbeData *p)
7106 {
7107     int64_t offset;
7108     uint32_t tag;
7109     int score = 0;
7110     int moov_offset = -1;
7111
7112     /* check file header */
7113     offset = 0;
7114     for (;;) {
7115         int64_t size;
7116         /* ignore invalid offset */
7117         if ((offset + 8) > (unsigned int)p->buf_size)
7118             break;
7119         size = AV_RB32(p->buf + offset);
7120         if (size == 1 && offset + 16 > (unsigned int)p->buf_size) {
7121             size = AV_RB64(p->buf+offset + 8);
7122         } else if (size == 0) {
7123             size = p->buf_size - offset;
7124         }
7125         tag = AV_RL32(p->buf + offset + 4);
7126         switch(tag) {
7127         /* check for obvious tags */
7128         case MKTAG('m','o','o','v'):
7129             moov_offset = offset + 4;
7130         case MKTAG('m','d','a','t'):
7131         case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
7132         case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
7133         case MKTAG('f','t','y','p'):
7134             if (size < 8) {
7135                 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
7136             } else if (tag == MKTAG('f','t','y','p') &&
7137                        (   AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
7138                         || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
7139                     )) {
7140                 score = FFMAX(score, 5);
7141             } else {
7142                 score = AVPROBE_SCORE_MAX;
7143             }
7144             offset = FFMAX(4, size) + offset;
7145             break;
7146         /* those are more common words, so rate then a bit less */
7147         case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
7148         case MKTAG('w','i','d','e'):
7149         case MKTAG('f','r','e','e'):
7150         case MKTAG('j','u','n','k'):
7151         case MKTAG('p','i','c','t'):
7152             score  = FFMAX(score, AVPROBE_SCORE_MAX - 5);
7153             offset = FFMAX(4, size) + offset;
7154             break;
7155         case MKTAG(0x82,0x82,0x7f,0x7d):
7156         case MKTAG('s','k','i','p'):
7157         case MKTAG('u','u','i','d'):
7158         case MKTAG('p','r','f','l'):
7159             /* if we only find those cause probedata is too small at least rate them */
7160             score  = FFMAX(score, AVPROBE_SCORE_EXTENSION);
7161             offset = FFMAX(4, size) + offset;
7162             break;
7163         default:
7164             offset = FFMAX(4, size) + offset;
7165         }
7166     }
7167     if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
7168         /* moov atom in the header - we should make sure that this is not a
7169          * MOV-packed MPEG-PS */
7170         offset = moov_offset;
7171
7172         while (offset < (p->buf_size - 16)) { /* Sufficient space */
7173                /* We found an actual hdlr atom */
7174             if (AV_RL32(p->buf + offset     ) == MKTAG('h','d','l','r') &&
7175                 AV_RL32(p->buf + offset +  8) == MKTAG('m','h','l','r') &&
7176                 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
7177                 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
7178                 /* We found a media handler reference atom describing an
7179                  * MPEG-PS-in-MOV, return a
7180                  * low score to force expanding the probe window until
7181                  * mpegps_probe finds what it needs */
7182                 return 5;
7183             } else {
7184                 /* Keep looking */
7185                 offset += 2;
7186             }
7187         }
7188     }
7189
7190     return score;
7191 }
7192
7193 // must be done after parsing all trak because there's no order requirement
7194 static void mov_read_chapters(AVFormatContext *s)
7195 {
7196     MOVContext *mov = s->priv_data;
7197     AVStream *st;
7198     MOVStreamContext *sc;
7199     int64_t cur_pos;
7200     int i, j;
7201     int chapter_track;
7202
7203     for (j = 0; j < mov->nb_chapter_tracks; j++) {
7204         chapter_track = mov->chapter_tracks[j];
7205         st = NULL;
7206         for (i = 0; i < s->nb_streams; i++)
7207             if (s->streams[i]->id == chapter_track) {
7208                 st = s->streams[i];
7209                 break;
7210             }
7211         if (!st) {
7212             av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
7213             continue;
7214         }
7215
7216         sc = st->priv_data;
7217         cur_pos = avio_tell(sc->pb);
7218
7219         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
7220             st->disposition |= AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS;
7221             if (st->internal->nb_index_entries) {
7222                 // Retrieve the first frame, if possible
7223                 AVIndexEntry *sample = &st->internal->index_entries[0];
7224                 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
7225                     av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
7226                     goto finish;
7227                 }
7228
7229                 if (av_get_packet(sc->pb, &st->attached_pic, sample->size) < 0)
7230                     goto finish;
7231
7232                 st->attached_pic.stream_index = st->index;
7233                 st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
7234             }
7235         } else {
7236             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
7237             st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
7238             st->discard = AVDISCARD_ALL;
7239             for (i = 0; i < st->internal->nb_index_entries; i++) {
7240                 AVIndexEntry *sample = &st->internal->index_entries[i];
7241                 int64_t end = i+1 < st->internal->nb_index_entries ? st->internal->index_entries[i+1].timestamp : st->duration;
7242                 uint8_t *title;
7243                 uint16_t ch;
7244                 int len, title_len;
7245
7246                 if (end < sample->timestamp) {
7247                     av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
7248                     end = AV_NOPTS_VALUE;
7249                 }
7250
7251                 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
7252                     av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
7253                     goto finish;
7254                 }
7255
7256                 // the first two bytes are the length of the title
7257                 len = avio_rb16(sc->pb);
7258                 if (len > sample->size-2)
7259                     continue;
7260                 title_len = 2*len + 1;
7261                 if (!(title = av_mallocz(title_len)))
7262                     goto finish;
7263
7264                 // The samples could theoretically be in any encoding if there's an encd
7265                 // atom following, but in practice are only utf-8 or utf-16, distinguished
7266                 // instead by the presence of a BOM
7267                 if (!len) {
7268                     title[0] = 0;
7269                 } else {
7270                     ch = avio_rb16(sc->pb);
7271                     if (ch == 0xfeff)
7272                         avio_get_str16be(sc->pb, len, title, title_len);
7273                     else if (ch == 0xfffe)
7274                         avio_get_str16le(sc->pb, len, title, title_len);
7275                     else {
7276                         AV_WB16(title, ch);
7277                         if (len == 1 || len == 2)
7278                             title[len] = 0;
7279                         else
7280                             avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
7281                     }
7282                 }
7283
7284                 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
7285                 av_freep(&title);
7286             }
7287         }
7288 finish:
7289         avio_seek(sc->pb, cur_pos, SEEK_SET);
7290     }
7291 }
7292
7293 static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
7294                                              uint32_t value, int flags)
7295 {
7296     AVTimecode tc;
7297     char buf[AV_TIMECODE_STR_SIZE];
7298     AVRational rate = st->avg_frame_rate;
7299     int ret = av_timecode_init(&tc, rate, flags, 0, s);
7300     if (ret < 0)
7301         return ret;
7302     av_dict_set(&st->metadata, "timecode",
7303                 av_timecode_make_string(&tc, buf, value), 0);
7304     return 0;
7305 }
7306
7307 static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
7308 {
7309     MOVStreamContext *sc = st->priv_data;
7310     char buf[AV_TIMECODE_STR_SIZE];
7311     int64_t cur_pos = avio_tell(sc->pb);
7312     int hh, mm, ss, ff, drop;
7313
7314     if (!st->internal->nb_index_entries)
7315         return -1;
7316
7317     avio_seek(sc->pb, st->internal->index_entries->pos, SEEK_SET);
7318     avio_skip(s->pb, 13);
7319     hh = avio_r8(s->pb);
7320     mm = avio_r8(s->pb);
7321     ss = avio_r8(s->pb);
7322     drop = avio_r8(s->pb);
7323     ff = avio_r8(s->pb);
7324     snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
7325              hh, mm, ss, drop ? ';' : ':', ff);
7326     av_dict_set(&st->metadata, "timecode", buf, 0);
7327
7328     avio_seek(sc->pb, cur_pos, SEEK_SET);
7329     return 0;
7330 }
7331
7332 static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
7333 {
7334     MOVStreamContext *sc = st->priv_data;
7335     int flags = 0;
7336     int64_t cur_pos = avio_tell(sc->pb);
7337     uint32_t value;
7338
7339     if (!st->internal->nb_index_entries)
7340         return -1;
7341
7342     avio_seek(sc->pb, st->internal->index_entries->pos, SEEK_SET);
7343     value = avio_rb32(s->pb);
7344
7345     if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
7346     if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
7347     if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
7348
7349     /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
7350      * not the case) and thus assume "frame number format" instead of QT one.
7351      * No sample with tmcd track can be found with a QT timecode at the moment,
7352      * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
7353      * format). */
7354     parse_timecode_in_framenum_format(s, st, value, flags);
7355
7356     avio_seek(sc->pb, cur_pos, SEEK_SET);
7357     return 0;
7358 }
7359
7360 static void mov_free_encryption_index(MOVEncryptionIndex **index) {
7361     int i;
7362     if (!index || !*index) return;
7363     for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
7364         av_encryption_info_free((*index)->encrypted_samples[i]);
7365     }
7366     av_freep(&(*index)->encrypted_samples);
7367     av_freep(&(*index)->auxiliary_info_sizes);
7368     av_freep(&(*index)->auxiliary_offsets);
7369     av_freep(index);
7370 }
7371
7372 static int mov_read_close(AVFormatContext *s)
7373 {
7374     MOVContext *mov = s->priv_data;
7375     int i, j;
7376
7377     for (i = 0; i < s->nb_streams; i++) {
7378         AVStream *st = s->streams[i];
7379         MOVStreamContext *sc = st->priv_data;
7380
7381         if (!sc)
7382             continue;
7383
7384         av_freep(&sc->ctts_data);
7385         for (j = 0; j < sc->drefs_count; j++) {
7386             av_freep(&sc->drefs[j].path);
7387             av_freep(&sc->drefs[j].dir);
7388         }
7389         av_freep(&sc->drefs);
7390
7391         sc->drefs_count = 0;
7392
7393         if (!sc->pb_is_copied)
7394             ff_format_io_close(s, &sc->pb);
7395
7396         sc->pb = NULL;
7397         av_freep(&sc->chunk_offsets);
7398         av_freep(&sc->stsc_data);
7399         av_freep(&sc->sample_sizes);
7400         av_freep(&sc->keyframes);
7401         av_freep(&sc->stts_data);
7402         av_freep(&sc->sdtp_data);
7403         av_freep(&sc->stps_data);
7404         av_freep(&sc->elst_data);
7405         av_freep(&sc->rap_group);
7406         av_freep(&sc->display_matrix);
7407         av_freep(&sc->index_ranges);
7408
7409         if (sc->extradata)
7410             for (j = 0; j < sc->stsd_count; j++)
7411                 av_free(sc->extradata[j]);
7412         av_freep(&sc->extradata);
7413         av_freep(&sc->extradata_size);
7414
7415         mov_free_encryption_index(&sc->cenc.encryption_index);
7416         av_encryption_info_free(sc->cenc.default_encrypted_sample);
7417         av_aes_ctr_free(sc->cenc.aes_ctr);
7418
7419         av_freep(&sc->stereo3d);
7420         av_freep(&sc->spherical);
7421         av_freep(&sc->mastering);
7422         av_freep(&sc->coll);
7423     }
7424
7425     av_freep(&mov->dv_demux);
7426     avformat_free_context(mov->dv_fctx);
7427     mov->dv_fctx = NULL;
7428
7429     if (mov->meta_keys) {
7430         for (i = 1; i < mov->meta_keys_count; i++) {
7431             av_freep(&mov->meta_keys[i]);
7432         }
7433         av_freep(&mov->meta_keys);
7434     }
7435
7436     av_freep(&mov->trex_data);
7437     av_freep(&mov->bitrates);
7438
7439     for (i = 0; i < mov->frag_index.nb_items; i++) {
7440         MOVFragmentStreamInfo *frag = mov->frag_index.item[i].stream_info;
7441         for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
7442             mov_free_encryption_index(&frag[j].encryption_index);
7443         }
7444         av_freep(&mov->frag_index.item[i].stream_info);
7445     }
7446     av_freep(&mov->frag_index.item);
7447
7448     av_freep(&mov->aes_decrypt);
7449     av_freep(&mov->chapter_tracks);
7450
7451     return 0;
7452 }
7453
7454 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
7455 {
7456     int i;
7457
7458     for (i = 0; i < s->nb_streams; i++) {
7459         AVStream *st = s->streams[i];
7460         MOVStreamContext *sc = st->priv_data;
7461
7462         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
7463             sc->timecode_track == tmcd_id)
7464             return 1;
7465     }
7466     return 0;
7467 }
7468
7469 /* look for a tmcd track not referenced by any video track, and export it globally */
7470 static void export_orphan_timecode(AVFormatContext *s)
7471 {
7472     int i;
7473
7474     for (i = 0; i < s->nb_streams; i++) {
7475         AVStream *st = s->streams[i];
7476
7477         if (st->codecpar->codec_tag  == MKTAG('t','m','c','d') &&
7478             !tmcd_is_referenced(s, i + 1)) {
7479             AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
7480             if (tcr) {
7481                 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
7482                 break;
7483             }
7484         }
7485     }
7486 }
7487
7488 static int read_tfra(MOVContext *mov, AVIOContext *f)
7489 {
7490     int version, fieldlength, i, j;
7491     int64_t pos = avio_tell(f);
7492     uint32_t size = avio_rb32(f);
7493     unsigned track_id, item_count;
7494
7495     if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
7496         return 1;
7497     }
7498     av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
7499
7500     version = avio_r8(f);
7501     avio_rb24(f);
7502     track_id = avio_rb32(f);
7503     fieldlength = avio_rb32(f);
7504     item_count = avio_rb32(f);
7505     for (i = 0; i < item_count; i++) {
7506         int64_t time, offset;
7507         int index;
7508         MOVFragmentStreamInfo * frag_stream_info;
7509
7510         if (avio_feof(f)) {
7511             return AVERROR_INVALIDDATA;
7512         }
7513
7514         if (version == 1) {
7515             time   = avio_rb64(f);
7516             offset = avio_rb64(f);
7517         } else {
7518             time   = avio_rb32(f);
7519             offset = avio_rb32(f);
7520         }
7521
7522         // The first sample of each stream in a fragment is always a random
7523         // access sample.  So it's entry in the tfra can be used as the
7524         // initial PTS of the fragment.
7525         index = update_frag_index(mov, offset);
7526         frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
7527         if (frag_stream_info &&
7528             frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
7529             frag_stream_info->first_tfra_pts = time;
7530
7531         for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
7532             avio_r8(f);
7533         for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
7534             avio_r8(f);
7535         for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
7536             avio_r8(f);
7537     }
7538
7539     avio_seek(f, pos + size, SEEK_SET);
7540     return 0;
7541 }
7542
7543 static int mov_read_mfra(MOVContext *c, AVIOContext *f)
7544 {
7545     int64_t stream_size = avio_size(f);
7546     int64_t original_pos = avio_tell(f);
7547     int64_t seek_ret;
7548     int ret = -1;
7549     if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
7550         ret = seek_ret;
7551         goto fail;
7552     }
7553     c->mfra_size = avio_rb32(f);
7554     c->have_read_mfra_size = 1;
7555     if (!c->mfra_size || c->mfra_size > stream_size) {
7556         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
7557         goto fail;
7558     }
7559     if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
7560         ret = seek_ret;
7561         goto fail;
7562     }
7563     if (avio_rb32(f) != c->mfra_size) {
7564         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
7565         goto fail;
7566     }
7567     if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
7568         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
7569         goto fail;
7570     }
7571     av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
7572     do {
7573         ret = read_tfra(c, f);
7574         if (ret < 0)
7575             goto fail;
7576     } while (!ret);
7577     ret = 0;
7578     c->frag_index.complete = 1;
7579 fail:
7580     seek_ret = avio_seek(f, original_pos, SEEK_SET);
7581     if (seek_ret < 0) {
7582         av_log(c->fc, AV_LOG_ERROR,
7583                "failed to seek back after looking for mfra\n");
7584         ret = seek_ret;
7585     }
7586     return ret;
7587 }
7588
7589 static int mov_read_header(AVFormatContext *s)
7590 {
7591     MOVContext *mov = s->priv_data;
7592     AVIOContext *pb = s->pb;
7593     int j, err;
7594     MOVAtom atom = { AV_RL32("root") };
7595     int i;
7596
7597     if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
7598         av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
7599             mov->decryption_key_len, AES_CTR_KEY_SIZE);
7600         return AVERROR(EINVAL);
7601     }
7602
7603     mov->fc = s;
7604     mov->trak_index = -1;
7605     /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
7606     if (pb->seekable & AVIO_SEEKABLE_NORMAL)
7607         atom.size = avio_size(pb);
7608     else
7609         atom.size = INT64_MAX;
7610
7611     /* check MOV header */
7612     do {
7613         if (mov->moov_retry)
7614             avio_seek(pb, 0, SEEK_SET);
7615         if ((err = mov_read_default(mov, pb, atom)) < 0) {
7616             av_log(s, AV_LOG_ERROR, "error reading header\n");
7617             goto fail;
7618         }
7619     } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mov->found_moov && !mov->moov_retry++);
7620     if (!mov->found_moov) {
7621         av_log(s, AV_LOG_ERROR, "moov atom not found\n");
7622         err = AVERROR_INVALIDDATA;
7623         goto fail;
7624     }
7625     av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
7626
7627     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
7628         if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
7629             mov_read_chapters(s);
7630         for (i = 0; i < s->nb_streams; i++)
7631             if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
7632                 mov_read_timecode_track(s, s->streams[i]);
7633             } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
7634                 mov_read_rtmd_track(s, s->streams[i]);
7635             }
7636     }
7637
7638     /* copy timecode metadata from tmcd tracks to the related video streams */
7639     for (i = 0; i < s->nb_streams; i++) {
7640         AVStream *st = s->streams[i];
7641         MOVStreamContext *sc = st->priv_data;
7642         if (sc->timecode_track > 0) {
7643             AVDictionaryEntry *tcr;
7644             int tmcd_st_id = -1;
7645
7646             for (j = 0; j < s->nb_streams; j++)
7647                 if (s->streams[j]->id == sc->timecode_track)
7648                     tmcd_st_id = j;
7649
7650             if (tmcd_st_id < 0 || tmcd_st_id == i)
7651                 continue;
7652             tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
7653             if (tcr)
7654                 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
7655         }
7656     }
7657     export_orphan_timecode(s);
7658
7659     for (i = 0; i < s->nb_streams; i++) {
7660         AVStream *st = s->streams[i];
7661         MOVStreamContext *sc = st->priv_data;
7662         fix_timescale(mov, sc);
7663         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
7664             st->codecpar->codec_id   == AV_CODEC_ID_AAC) {
7665             st->internal->skip_samples = sc->start_pad;
7666         }
7667         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
7668             av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
7669                       sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
7670         if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
7671             if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
7672                 st->codecpar->width  = sc->width;
7673                 st->codecpar->height = sc->height;
7674             }
7675             if (st->codecpar->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
7676                 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
7677                     goto fail;
7678             }
7679         }
7680         if (mov->handbrake_version &&
7681             mov->handbrake_version <= 1000000*0 + 1000*10 + 2 &&  // 0.10.2
7682             st->codecpar->codec_id == AV_CODEC_ID_MP3) {
7683             av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
7684             st->need_parsing = AVSTREAM_PARSE_FULL;
7685         }
7686     }
7687
7688     if (mov->trex_data) {
7689         for (i = 0; i < s->nb_streams; i++) {
7690             AVStream *st = s->streams[i];
7691             MOVStreamContext *sc = st->priv_data;
7692             if (st->duration > 0) {
7693                 if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
7694                     av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
7695                            sc->data_size, sc->time_scale);
7696                     err = AVERROR_INVALIDDATA;
7697                     goto fail;
7698                 }
7699                 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
7700             }
7701         }
7702     }
7703
7704     if (mov->use_mfra_for > 0) {
7705         for (i = 0; i < s->nb_streams; i++) {
7706             AVStream *st = s->streams[i];
7707             MOVStreamContext *sc = st->priv_data;
7708             if (sc->duration_for_fps > 0) {
7709                 if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
7710                     av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
7711                            sc->data_size, sc->time_scale);
7712                     err = AVERROR_INVALIDDATA;
7713                     goto fail;
7714                 }
7715                 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale /
7716                     sc->duration_for_fps;
7717             }
7718         }
7719     }
7720
7721     for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
7722         if (mov->bitrates[i]) {
7723             s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
7724         }
7725     }
7726
7727     ff_rfps_calculate(s);
7728
7729     for (i = 0; i < s->nb_streams; i++) {
7730         AVStream *st = s->streams[i];
7731         MOVStreamContext *sc = st->priv_data;
7732
7733         switch (st->codecpar->codec_type) {
7734         case AVMEDIA_TYPE_AUDIO:
7735             err = ff_replaygain_export(st, s->metadata);
7736             if (err < 0)
7737                 goto fail;
7738             break;
7739         case AVMEDIA_TYPE_VIDEO:
7740             if (sc->display_matrix) {
7741                 err = av_stream_add_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, (uint8_t*)sc->display_matrix,
7742                                               sizeof(int32_t) * 9);
7743                 if (err < 0)
7744                     goto fail;
7745
7746                 sc->display_matrix = NULL;
7747             }
7748             if (sc->stereo3d) {
7749                 err = av_stream_add_side_data(st, AV_PKT_DATA_STEREO3D,
7750                                               (uint8_t *)sc->stereo3d,
7751                                               sizeof(*sc->stereo3d));
7752                 if (err < 0)
7753                     goto fail;
7754
7755                 sc->stereo3d = NULL;
7756             }
7757             if (sc->spherical) {
7758                 err = av_stream_add_side_data(st, AV_PKT_DATA_SPHERICAL,
7759                                               (uint8_t *)sc->spherical,
7760                                               sc->spherical_size);
7761                 if (err < 0)
7762                     goto fail;
7763
7764                 sc->spherical = NULL;
7765             }
7766             if (sc->mastering) {
7767                 err = av_stream_add_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
7768                                               (uint8_t *)sc->mastering,
7769                                               sizeof(*sc->mastering));
7770                 if (err < 0)
7771                     goto fail;
7772
7773                 sc->mastering = NULL;
7774             }
7775             if (sc->coll) {
7776                 err = av_stream_add_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
7777                                               (uint8_t *)sc->coll,
7778                                               sc->coll_size);
7779                 if (err < 0)
7780                     goto fail;
7781
7782                 sc->coll = NULL;
7783             }
7784             break;
7785         }
7786     }
7787     ff_configure_buffers_for_index(s, AV_TIME_BASE);
7788
7789     for (i = 0; i < mov->frag_index.nb_items; i++)
7790         if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
7791             mov->frag_index.item[i].headers_read = 1;
7792
7793     return 0;
7794 fail:
7795     mov_read_close(s);
7796     return err;
7797 }
7798
7799 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
7800 {
7801     AVIndexEntry *sample = NULL;
7802     int64_t best_dts = INT64_MAX;
7803     int i;
7804     for (i = 0; i < s->nb_streams; i++) {
7805         AVStream *avst = s->streams[i];
7806         MOVStreamContext *msc = avst->priv_data;
7807         if (msc->pb && msc->current_sample < avst->internal->nb_index_entries) {
7808             AVIndexEntry *current_sample = &avst->internal->index_entries[msc->current_sample];
7809             int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
7810             av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
7811             if (!sample || (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && current_sample->pos < sample->pos) ||
7812                 ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
7813                  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
7814                  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
7815                   (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
7816                 sample = current_sample;
7817                 best_dts = dts;
7818                 *st = avst;
7819             }
7820         }
7821     }
7822     return sample;
7823 }
7824
7825 static int should_retry(AVIOContext *pb, int error_code) {
7826     if (error_code == AVERROR_EOF || avio_feof(pb))
7827         return 0;
7828
7829     return 1;
7830 }
7831
7832 static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
7833 {
7834     int ret;
7835     MOVContext *mov = s->priv_data;
7836
7837     if (index >= 0 && index < mov->frag_index.nb_items)
7838         target = mov->frag_index.item[index].moof_offset;
7839     if (avio_seek(s->pb, target, SEEK_SET) != target) {
7840         av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
7841         return AVERROR_INVALIDDATA;
7842     }
7843
7844     mov->next_root_atom = 0;
7845     if (index < 0 || index >= mov->frag_index.nb_items)
7846         index = search_frag_moof_offset(&mov->frag_index, target);
7847     if (index < mov->frag_index.nb_items &&
7848         mov->frag_index.item[index].moof_offset == target) {
7849         if (index + 1 < mov->frag_index.nb_items)
7850             mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
7851         if (mov->frag_index.item[index].headers_read)
7852             return 0;
7853         mov->frag_index.item[index].headers_read = 1;
7854     }
7855
7856     mov->found_mdat = 0;
7857
7858     ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
7859     if (ret < 0)
7860         return ret;
7861     if (avio_feof(s->pb))
7862         return AVERROR_EOF;
7863     av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
7864
7865     return 1;
7866 }
7867
7868 static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
7869 {
7870     uint8_t *side, *extradata;
7871     int extradata_size;
7872
7873     /* Save the current index. */
7874     sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
7875
7876     /* Notify the decoder that extradata changed. */
7877     extradata_size = sc->extradata_size[sc->last_stsd_index];
7878     extradata = sc->extradata[sc->last_stsd_index];
7879     if (extradata_size > 0 && extradata) {
7880         side = av_packet_new_side_data(pkt,
7881                                        AV_PKT_DATA_NEW_EXTRADATA,
7882                                        extradata_size);
7883         if (!side)
7884             return AVERROR(ENOMEM);
7885         memcpy(side, extradata, extradata_size);
7886     }
7887
7888     return 0;
7889 }
7890
7891 static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size)
7892 {
7893     int new_size, ret;
7894
7895     if (size <= 8)
7896         return AVERROR_INVALIDDATA;
7897     new_size = ((size - 8) / 2) * 3;
7898     ret = av_new_packet(pkt, new_size);
7899     if (ret < 0)
7900         return ret;
7901
7902     avio_skip(pb, 8);
7903     for (int j = 0; j < new_size; j += 3) {
7904         pkt->data[j] = 0xFC;
7905         pkt->data[j+1] = avio_r8(pb);
7906         pkt->data[j+2] = avio_r8(pb);
7907     }
7908
7909     return 0;
7910 }
7911
7912 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
7913 {
7914     MOVContext *mov = s->priv_data;
7915     MOVStreamContext *sc;
7916     AVIndexEntry *sample;
7917     AVStream *st = NULL;
7918     int64_t current_index;
7919     int ret;
7920     mov->fc = s;
7921  retry:
7922     sample = mov_find_next_sample(s, &st);
7923     if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
7924         if (!mov->next_root_atom)
7925             return AVERROR_EOF;
7926         if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
7927             return ret;
7928         goto retry;
7929     }
7930     sc = st->priv_data;
7931     /* must be done just before reading, to avoid infinite loop on sample */
7932     current_index = sc->current_index;
7933     mov_current_sample_inc(sc);
7934
7935     if (mov->next_root_atom) {
7936         sample->pos = FFMIN(sample->pos, mov->next_root_atom);
7937         sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
7938     }
7939
7940     if (st->discard != AVDISCARD_ALL) {
7941         int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
7942         if (ret64 != sample->pos) {
7943             av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
7944                    sc->ffindex, sample->pos);
7945             if (should_retry(sc->pb, ret64)) {
7946                 mov_current_sample_dec(sc);
7947             }
7948             return AVERROR_INVALIDDATA;
7949         }
7950
7951         if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
7952             av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
7953             goto retry;
7954         }
7955
7956         if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
7957             ret = get_eia608_packet(sc->pb, pkt, sample->size);
7958         else
7959             ret = av_get_packet(sc->pb, pkt, sample->size);
7960         if (ret < 0) {
7961             if (should_retry(sc->pb, ret)) {
7962                 mov_current_sample_dec(sc);
7963             }
7964             return ret;
7965         }
7966 #if CONFIG_DV_DEMUXER
7967         if (mov->dv_demux && sc->dv_audio_container) {
7968             AVBufferRef *buf = pkt->buf;
7969             ret = avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
7970             pkt->buf = buf;
7971             av_packet_unref(pkt);
7972             if (ret < 0)
7973                 return ret;
7974             ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
7975             if (ret < 0)
7976                 return ret;
7977         }
7978 #endif
7979         if (sc->has_palette) {
7980             uint8_t *pal;
7981
7982             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
7983             if (!pal) {
7984                 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
7985             } else {
7986                 memcpy(pal, sc->palette, AVPALETTE_SIZE);
7987                 sc->has_palette = 0;
7988             }
7989         }
7990         if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !st->need_parsing && pkt->size > 4) {
7991             if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
7992                 st->need_parsing = AVSTREAM_PARSE_FULL;
7993         }
7994     }
7995
7996     pkt->stream_index = sc->ffindex;
7997     pkt->dts = sample->timestamp;
7998     if (sample->flags & AVINDEX_DISCARD_FRAME) {
7999         pkt->flags |= AV_PKT_FLAG_DISCARD;
8000     }
8001     if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
8002         pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
8003         /* update ctts context */
8004         sc->ctts_sample++;
8005         if (sc->ctts_index < sc->ctts_count &&
8006             sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
8007             sc->ctts_index++;
8008             sc->ctts_sample = 0;
8009         }
8010     } else {
8011         int64_t next_dts = (sc->current_sample < st->internal->nb_index_entries) ?
8012             st->internal->index_entries[sc->current_sample].timestamp : st->duration;
8013
8014         if (next_dts >= pkt->dts)
8015             pkt->duration = next_dts - pkt->dts;
8016         pkt->pts = pkt->dts;
8017     }
8018     if (st->discard == AVDISCARD_ALL)
8019         goto retry;
8020     if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
8021         uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
8022         uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
8023         pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
8024     }
8025     pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
8026     pkt->pos = sample->pos;
8027
8028     /* Multiple stsd handling. */
8029     if (sc->stsc_data) {
8030         /* Keep track of the stsc index for the given sample, then check
8031         * if the stsd index is different from the last used one. */
8032         sc->stsc_sample++;
8033         if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
8034             mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
8035             sc->stsc_index++;
8036             sc->stsc_sample = 0;
8037         /* Do not check indexes after a switch. */
8038         } else if (sc->stsc_data[sc->stsc_index].id > 0 &&
8039                    sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
8040                    sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
8041             ret = mov_change_extradata(sc, pkt);
8042             if (ret < 0)
8043                 return ret;
8044         }
8045     }
8046
8047     if (mov->aax_mode)
8048         aax_filter(pkt->data, pkt->size, mov);
8049
8050     ret = cenc_filter(mov, st, sc, pkt, current_index);
8051     if (ret < 0) {
8052         return ret;
8053     }
8054
8055     return 0;
8056 }
8057
8058 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
8059 {
8060     MOVContext *mov = s->priv_data;
8061     int index;
8062
8063     if (!mov->frag_index.complete)
8064         return 0;
8065
8066     index = search_frag_timestamp(&mov->frag_index, st, timestamp);
8067     if (index < 0)
8068         index = 0;
8069     if (!mov->frag_index.item[index].headers_read)
8070         return mov_switch_root(s, -1, index);
8071     if (index + 1 < mov->frag_index.nb_items)
8072         mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
8073
8074     return 0;
8075 }
8076
8077 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
8078 {
8079     MOVStreamContext *sc = st->priv_data;
8080     int sample, time_sample, ret;
8081     unsigned int i;
8082
8083     // Here we consider timestamp to be PTS, hence try to offset it so that we
8084     // can search over the DTS timeline.
8085     timestamp -= (sc->min_corrected_pts + sc->dts_shift);
8086
8087     ret = mov_seek_fragment(s, st, timestamp);
8088     if (ret < 0)
8089         return ret;
8090
8091     sample = av_index_search_timestamp(st, timestamp, flags);
8092     av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
8093     if (sample < 0 && st->internal->nb_index_entries && timestamp < st->internal->index_entries[0].timestamp)
8094         sample = 0;
8095     if (sample < 0) /* not sure what to do */
8096         return AVERROR_INVALIDDATA;
8097     mov_current_sample_set(sc, sample);
8098     av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
8099     /* adjust ctts index */
8100     if (sc->ctts_data) {
8101         time_sample = 0;
8102         for (i = 0; i < sc->ctts_count; i++) {
8103             int next = time_sample + sc->ctts_data[i].count;
8104             if (next > sc->current_sample) {
8105                 sc->ctts_index = i;
8106                 sc->ctts_sample = sc->current_sample - time_sample;
8107                 break;
8108             }
8109             time_sample = next;
8110         }
8111     }
8112
8113     /* adjust stsd index */
8114     if (sc->chunk_count) {
8115         time_sample = 0;
8116         for (i = 0; i < sc->stsc_count; i++) {
8117             int64_t next = time_sample + mov_get_stsc_samples(sc, i);
8118             if (next > sc->current_sample) {
8119                 sc->stsc_index = i;
8120                 sc->stsc_sample = sc->current_sample - time_sample;
8121                 break;
8122             }
8123             av_assert0(next == (int)next);
8124             time_sample = next;
8125         }
8126     }
8127
8128     return sample;
8129 }
8130
8131 static int64_t mov_get_skip_samples(AVStream *st, int sample)
8132 {
8133     MOVStreamContext *sc = st->priv_data;
8134     int64_t first_ts = st->internal->index_entries[0].timestamp;
8135     int64_t ts = st->internal->index_entries[sample].timestamp;
8136     int64_t off;
8137
8138     if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
8139         return 0;
8140
8141     /* compute skip samples according to stream start_pad, seek ts and first ts */
8142     off = av_rescale_q(ts - first_ts, st->time_base,
8143                        (AVRational){1, st->codecpar->sample_rate});
8144     return FFMAX(sc->start_pad - off, 0);
8145 }
8146
8147 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
8148 {
8149     MOVContext *mc = s->priv_data;
8150     AVStream *st;
8151     int sample;
8152     int i;
8153
8154     if (stream_index >= s->nb_streams)
8155         return AVERROR_INVALIDDATA;
8156
8157     st = s->streams[stream_index];
8158     sample = mov_seek_stream(s, st, sample_time, flags);
8159     if (sample < 0)
8160         return sample;
8161
8162     if (mc->seek_individually) {
8163         /* adjust seek timestamp to found sample timestamp */
8164         int64_t seek_timestamp = st->internal->index_entries[sample].timestamp;
8165         st->internal->skip_samples = mov_get_skip_samples(st, sample);
8166
8167         for (i = 0; i < s->nb_streams; i++) {
8168             int64_t timestamp;
8169             st = s->streams[i];
8170
8171             if (stream_index == i)
8172                 continue;
8173
8174             timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
8175             sample = mov_seek_stream(s, st, timestamp, flags);
8176             if (sample >= 0)
8177                 st->internal->skip_samples = mov_get_skip_samples(st, sample);
8178         }
8179     } else {
8180         for (i = 0; i < s->nb_streams; i++) {
8181             MOVStreamContext *sc;
8182             st = s->streams[i];
8183             sc = st->priv_data;
8184             mov_current_sample_set(sc, 0);
8185         }
8186         while (1) {
8187             MOVStreamContext *sc;
8188             AVIndexEntry *entry = mov_find_next_sample(s, &st);
8189             if (!entry)
8190                 return AVERROR_INVALIDDATA;
8191             sc = st->priv_data;
8192             if (sc->ffindex == stream_index && sc->current_sample == sample)
8193                 break;
8194             mov_current_sample_inc(sc);
8195         }
8196     }
8197     return 0;
8198 }
8199
8200 #define OFFSET(x) offsetof(MOVContext, x)
8201 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
8202 static const AVOption mov_options[] = {
8203     {"use_absolute_path",
8204         "allow using absolute path when opening alias, this is a possible security issue",
8205         OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
8206         0, 1, FLAGS},
8207     {"seek_streams_individually",
8208         "Seek each stream individually to the closest point",
8209         OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
8210         0, 1, FLAGS},
8211     {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
8212         0, 1, FLAGS},
8213     {"advanced_editlist",
8214         "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
8215         OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
8216         0, 1, FLAGS},
8217     {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
8218         0, 1, FLAGS},
8219     {"use_mfra_for",
8220         "use mfra for fragment timestamps",
8221         OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
8222         -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
8223         "use_mfra_for"},
8224     {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
8225         FLAGS, "use_mfra_for" },
8226     {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
8227         FLAGS, "use_mfra_for" },
8228     {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
8229         FLAGS, "use_mfra_for" },
8230     { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
8231         AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
8232     { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
8233         AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
8234     { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
8235         AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
8236     { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
8237         AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
8238     { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
8239         AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
8240     { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
8241         "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
8242         AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
8243         .flags = AV_OPT_FLAG_DECODING_PARAM },
8244     { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
8245     { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
8246         {.i64 = 0}, 0, 1, FLAGS },
8247
8248     { NULL },
8249 };
8250
8251 static const AVClass mov_class = {
8252     .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
8253     .item_name  = av_default_item_name,
8254     .option     = mov_options,
8255     .version    = LIBAVUTIL_VERSION_INT,
8256 };
8257
8258 AVInputFormat ff_mov_demuxer = {
8259     .name           = "mov,mp4,m4a,3gp,3g2,mj2",
8260     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
8261     .priv_class     = &mov_class,
8262     .priv_data_size = sizeof(MOVContext),
8263     .extensions     = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v",
8264     .read_probe     = mov_probe,
8265     .read_header    = mov_read_header,
8266     .read_packet    = mov_read_packet,
8267     .read_close     = mov_read_close,
8268     .read_seek      = mov_read_seek,
8269     .flags          = AVFMT_NO_BYTE_SEEK | AVFMT_SEEK_TO_PTS,
8270 };