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