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