]> git.sesse.net Git - ffmpeg/blob - libavformat/mov.c
Merge commit 'd34826c33d401929b2ff8aee161fd39ad0a73613'
[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/avstring.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/display.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/aes.h"
42 #include "libavutil/aes_ctr.h"
43 #include "libavutil/sha.h"
44 #include "libavutil/timecode.h"
45 #include "libavcodec/ac3tab.h"
46 #include "avformat.h"
47 #include "internal.h"
48 #include "avio_internal.h"
49 #include "riff.h"
50 #include "isom.h"
51 #include "libavcodec/get_bits.h"
52 #include "id3v1.h"
53 #include "mov_chan.h"
54 #include "replaygain.h"
55
56 #if CONFIG_ZLIB
57 #include <zlib.h>
58 #endif
59
60 #include "qtpalette.h"
61
62 /* those functions parse an atom */
63 /* links atom IDs to parse functions */
64 typedef struct MOVParseTableEntry {
65     uint32_t type;
66     int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
67 } MOVParseTableEntry;
68
69 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
70 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
71
72 static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
73                                              unsigned len, const char *key)
74 {
75     char buf[16];
76
77     short current, total = 0;
78     avio_rb16(pb); // unknown
79     current = avio_rb16(pb);
80     if (len >= 6)
81         total = avio_rb16(pb);
82     if (!total)
83         snprintf(buf, sizeof(buf), "%d", current);
84     else
85         snprintf(buf, sizeof(buf), "%d/%d", current, total);
86     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
87     av_dict_set(&c->fc->metadata, key, buf, 0);
88
89     return 0;
90 }
91
92 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
93                                             unsigned len, const char *key)
94 {
95     /* bypass padding bytes */
96     avio_r8(pb);
97     avio_r8(pb);
98     avio_r8(pb);
99
100     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
101     av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
102
103     return 0;
104 }
105
106 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
107                                         unsigned len, const char *key)
108 {
109     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
110     av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
111
112     return 0;
113 }
114
115 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
116                              unsigned len, const char *key)
117 {
118     short genre;
119
120     avio_r8(pb); // unknown
121
122     genre = avio_r8(pb);
123     if (genre < 1 || genre > ID3v1_GENRE_MAX)
124         return 0;
125     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
126     av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
127
128     return 0;
129 }
130
131 static const uint32_t mac_to_unicode[128] = {
132     0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
133     0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
134     0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
135     0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
136     0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
137     0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
138     0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
139     0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
140     0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
141     0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
142     0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
143     0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
144     0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
145     0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
146     0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
147     0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
148 };
149
150 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
151                                char *dst, int dstlen)
152 {
153     char *p = dst;
154     char *end = dst+dstlen-1;
155     int i;
156
157     for (i = 0; i < len; i++) {
158         uint8_t t, c = avio_r8(pb);
159         if (c < 0x80 && p < end)
160             *p++ = c;
161         else if (p < end)
162             PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
163     }
164     *p = 0;
165     return p - dst;
166 }
167
168 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
169 {
170     AVPacket pkt;
171     AVStream *st;
172     MOVStreamContext *sc;
173     enum AVCodecID id;
174     int ret;
175
176     switch (type) {
177     case 0xd:  id = AV_CODEC_ID_MJPEG; break;
178     case 0xe:  id = AV_CODEC_ID_PNG;   break;
179     case 0x1b: id = AV_CODEC_ID_BMP;   break;
180     default:
181         av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
182         avio_skip(pb, len);
183         return 0;
184     }
185
186     st = avformat_new_stream(c->fc, NULL);
187     if (!st)
188         return AVERROR(ENOMEM);
189     sc = av_mallocz(sizeof(*sc));
190     if (!sc)
191         return AVERROR(ENOMEM);
192     st->priv_data = sc;
193
194     ret = av_get_packet(pb, &pkt, len);
195     if (ret < 0)
196         return ret;
197
198     if (pkt.size >= 8 && id != AV_CODEC_ID_BMP) {
199         if (AV_RB64(pkt.data) == 0x89504e470d0a1a0a) {
200             id = AV_CODEC_ID_PNG;
201         } else {
202             id = AV_CODEC_ID_MJPEG;
203         }
204     }
205
206     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
207
208     st->attached_pic              = pkt;
209     st->attached_pic.stream_index = st->index;
210     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
211
212     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
213     st->codecpar->codec_id   = id;
214
215     return 0;
216 }
217
218 // 3GPP TS 26.244
219 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
220 {
221     char language[4] = { 0 };
222     char buf[200], place[100];
223     uint16_t langcode = 0;
224     double longitude, latitude, altitude;
225     const char *key = "location";
226
227     if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
228         av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
229         return AVERROR_INVALIDDATA;
230     }
231
232     avio_skip(pb, 4); // version+flags
233     langcode = avio_rb16(pb);
234     ff_mov_lang_to_iso639(langcode, language);
235     len -= 6;
236
237     len -= avio_get_str(pb, len, place, sizeof(place));
238     if (len < 1) {
239         av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
240         return AVERROR_INVALIDDATA;
241     }
242     avio_skip(pb, 1); // role
243     len -= 1;
244
245     if (len < 12) {
246         av_log(c->fc, AV_LOG_ERROR, "no space for coordinates left (%d)\n", len);
247         return AVERROR_INVALIDDATA;
248     }
249     longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
250     latitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
251     altitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
252
253     // Try to output in the same format as the ?xyz field
254     snprintf(buf, sizeof(buf), "%+08.4f%+09.4f",  latitude, longitude);
255     if (altitude)
256         av_strlcatf(buf, sizeof(buf), "%+f", altitude);
257     av_strlcatf(buf, sizeof(buf), "/%s", place);
258
259     if (*language && strcmp(language, "und")) {
260         char key2[16];
261         snprintf(key2, sizeof(key2), "%s-%s", key, language);
262         av_dict_set(&c->fc->metadata, key2, buf, 0);
263     }
264     c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
265     return av_dict_set(&c->fc->metadata, key, buf, 0);
266 }
267
268 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
269 {
270     char tmp_key[5];
271     char key2[32], language[4] = {0};
272     char *str = NULL;
273     const char *key = NULL;
274     uint16_t langcode = 0;
275     uint32_t data_type = 0, str_size, str_size_alloc;
276     int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
277     int raw = 0;
278     int num = 0;
279
280     switch (atom.type) {
281     case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
282     case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
283     case MKTAG( 'X','M','P','_'):
284         if (c->export_xmp) { key = "xmp"; raw = 1; } break;
285     case MKTAG( 'a','A','R','T'): key = "album_artist";    break;
286     case MKTAG( 'a','k','I','D'): key = "account_type";
287         parse = mov_metadata_int8_no_padding; break;
288     case MKTAG( 'a','p','I','D'): key = "account_id"; break;
289     case MKTAG( 'c','a','t','g'): key = "category"; break;
290     case MKTAG( 'c','p','i','l'): key = "compilation";
291         parse = mov_metadata_int8_no_padding; break;
292     case MKTAG( 'c','p','r','t'): key = "copyright"; break;
293     case MKTAG( 'd','e','s','c'): key = "description"; break;
294     case MKTAG( 'd','i','s','k'): key = "disc";
295         parse = mov_metadata_track_or_disc_number; break;
296     case MKTAG( 'e','g','i','d'): key = "episode_uid";
297         parse = mov_metadata_int8_no_padding; break;
298     case MKTAG( 'g','n','r','e'): key = "genre";
299         parse = mov_metadata_gnre; break;
300     case MKTAG( 'h','d','v','d'): key = "hd_video";
301         parse = mov_metadata_int8_no_padding; break;
302     case MKTAG( 'k','e','y','w'): key = "keywords";  break;
303     case MKTAG( 'l','d','e','s'): key = "synopsis";  break;
304     case MKTAG( 'l','o','c','i'):
305         return mov_metadata_loci(c, pb, atom.size);
306     case MKTAG( 'p','c','s','t'): key = "podcast";
307         parse = mov_metadata_int8_no_padding; break;
308     case MKTAG( 'p','g','a','p'): key = "gapless_playback";
309         parse = mov_metadata_int8_no_padding; break;
310     case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
311     case MKTAG( 'r','t','n','g'): key = "rating";
312         parse = mov_metadata_int8_no_padding; break;
313     case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
314     case MKTAG( 's','o','a','l'): key = "sort_album";   break;
315     case MKTAG( 's','o','a','r'): key = "sort_artist";  break;
316     case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
317     case MKTAG( 's','o','n','m'): key = "sort_name";    break;
318     case MKTAG( 's','o','s','n'): key = "sort_show";    break;
319     case MKTAG( 's','t','i','k'): key = "media_type";
320         parse = mov_metadata_int8_no_padding; break;
321     case MKTAG( 't','r','k','n'): key = "track";
322         parse = mov_metadata_track_or_disc_number; break;
323     case MKTAG( 't','v','e','n'): key = "episode_id"; break;
324     case MKTAG( 't','v','e','s'): key = "episode_sort";
325         parse = mov_metadata_int8_bypass_padding; break;
326     case MKTAG( 't','v','n','n'): key = "network";   break;
327     case MKTAG( 't','v','s','h'): key = "show";      break;
328     case MKTAG( 't','v','s','n'): key = "season_number";
329         parse = mov_metadata_int8_bypass_padding; break;
330     case MKTAG(0xa9,'A','R','T'): key = "artist";    break;
331     case MKTAG(0xa9,'P','R','D'): key = "producer";  break;
332     case MKTAG(0xa9,'a','l','b'): key = "album";     break;
333     case MKTAG(0xa9,'a','u','t'): key = "artist";    break;
334     case MKTAG(0xa9,'c','h','p'): key = "chapter";   break;
335     case MKTAG(0xa9,'c','m','t'): key = "comment";   break;
336     case MKTAG(0xa9,'c','o','m'): key = "composer";  break;
337     case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
338     case MKTAG(0xa9,'d','a','y'): key = "date";      break;
339     case MKTAG(0xa9,'d','i','r'): key = "director";  break;
340     case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
341     case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
342     case MKTAG(0xa9,'e','n','c'): key = "encoder";   break;
343     case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
344     case MKTAG(0xa9,'g','e','n'): key = "genre";     break;
345     case MKTAG(0xa9,'g','r','p'): key = "grouping";  break;
346     case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
347     case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
348     case MKTAG(0xa9,'l','y','r'): key = "lyrics";    break;
349     case MKTAG(0xa9,'m','a','k'): key = "make";      break;
350     case MKTAG(0xa9,'m','o','d'): key = "model";     break;
351     case MKTAG(0xa9,'n','a','m'): key = "title";     break;
352     case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
353     case MKTAG(0xa9,'p','r','d'): key = "producer";  break;
354     case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
355     case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
356     case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
357     case MKTAG(0xa9,'s','t','3'): key = "subtitle";  break;
358     case MKTAG(0xa9,'s','w','r'): key = "encoder";   break;
359     case MKTAG(0xa9,'t','o','o'): key = "encoder";   break;
360     case MKTAG(0xa9,'t','r','k'): key = "track";     break;
361     case MKTAG(0xa9,'u','r','l'): key = "URL";       break;
362     case MKTAG(0xa9,'w','r','n'): key = "warning";   break;
363     case MKTAG(0xa9,'w','r','t'): key = "composer";  break;
364     case MKTAG(0xa9,'x','y','z'): key = "location";  break;
365     }
366 retry:
367     if (c->itunes_metadata && atom.size > 8) {
368         int data_size = avio_rb32(pb);
369         int tag = avio_rl32(pb);
370         if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
371             data_type = avio_rb32(pb); // type
372             avio_rb32(pb); // unknown
373             str_size = data_size - 16;
374             atom.size -= 16;
375
376             if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
377                 int ret = mov_read_covr(c, pb, data_type, str_size);
378                 if (ret < 0) {
379                     av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
380                 }
381                 return ret;
382             } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
383                 uint32_t index = AV_RB32(&atom.type);
384                 if (index < c->meta_keys_count) {
385                     key = c->meta_keys[index];
386                 } else {
387                     av_log(c->fc, AV_LOG_WARNING,
388                            "The index of 'data' is out of range: %d >= %d.\n",
389                            index, c->meta_keys_count);
390                 }
391             }
392         } else return 0;
393     } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
394         str_size = avio_rb16(pb); // string length
395         if (str_size > atom.size) {
396             raw = 1;
397             avio_seek(pb, -2, SEEK_CUR);
398             av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
399             goto retry;
400         }
401         langcode = avio_rb16(pb);
402         ff_mov_lang_to_iso639(langcode, language);
403         atom.size -= 4;
404     } else
405         str_size = atom.size;
406
407     if (c->export_all && !key) {
408         snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
409         key = tmp_key;
410     }
411
412     if (!key)
413         return 0;
414     if (atom.size < 0 || str_size >= INT_MAX/2)
415         return AVERROR_INVALIDDATA;
416
417     // Allocates enough space if data_type is a int32 or float32 number, otherwise
418     // worst-case requirement for output string in case of utf8 coded input
419     num = (data_type >= 21 && data_type <= 23);
420     str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
421     str = av_mallocz(str_size_alloc);
422     if (!str)
423         return AVERROR(ENOMEM);
424
425     if (parse)
426         parse(c, pb, str_size, key);
427     else {
428         if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
429             mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
430         } else if (data_type == 21) { // BE signed integer, variable size
431             int val = 0;
432             if (str_size == 1)
433                 val = (int8_t)avio_r8(pb);
434             else if (str_size == 2)
435                 val = (int16_t)avio_rb16(pb);
436             else if (str_size == 3)
437                 val = ((int32_t)(avio_rb24(pb)<<8))>>8;
438             else if (str_size == 4)
439                 val = (int32_t)avio_rb32(pb);
440             if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
441                 av_log(c->fc, AV_LOG_ERROR,
442                        "Failed to store the number (%d) in string.\n", val);
443                 av_free(str);
444                 return AVERROR_INVALIDDATA;
445             }
446         } else if (data_type == 22) { // BE unsigned integer, variable size
447             unsigned int val = 0;
448             if (str_size == 1)
449                 val = avio_r8(pb);
450             else if (str_size == 2)
451                 val = avio_rb16(pb);
452             else if (str_size == 3)
453                 val = avio_rb24(pb);
454             else if (str_size == 4)
455                 val = avio_rb32(pb);
456             if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
457                 av_log(c->fc, AV_LOG_ERROR,
458                        "Failed to store the number (%u) in string.\n", val);
459                 av_free(str);
460                 return AVERROR_INVALIDDATA;
461             }
462         } else if (data_type == 23 && str_size >= 4) {  // BE float32
463             float val = av_int2float(avio_rb32(pb));
464             if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
465                 av_log(c->fc, AV_LOG_ERROR,
466                        "Failed to store the float32 number (%f) in string.\n", val);
467                 av_free(str);
468                 return AVERROR_INVALIDDATA;
469             }
470         } else {
471             int ret = ffio_read_size(pb, str, str_size);
472             if (ret < 0) {
473                 av_free(str);
474                 return ret;
475             }
476             str[str_size] = 0;
477         }
478         c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
479         av_dict_set(&c->fc->metadata, key, str, 0);
480         if (*language && strcmp(language, "und")) {
481             snprintf(key2, sizeof(key2), "%s-%s", key, language);
482             av_dict_set(&c->fc->metadata, key2, str, 0);
483         }
484         if (!strcmp(key, "encoder")) {
485             int major, minor, micro;
486             if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
487                 c->handbrake_version = 1000000*major + 1000*minor + micro;
488             }
489         }
490     }
491     av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
492     av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
493             key, str, (char*)&atom.type, str_size_alloc, atom.size);
494
495     av_freep(&str);
496     return 0;
497 }
498
499 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
500 {
501     int64_t start;
502     int i, nb_chapters, str_len, version;
503     char str[256+1];
504     int ret;
505
506     if (c->ignore_chapters)
507         return 0;
508
509     if ((atom.size -= 5) < 0)
510         return 0;
511
512     version = avio_r8(pb);
513     avio_rb24(pb);
514     if (version)
515         avio_rb32(pb); // ???
516     nb_chapters = avio_r8(pb);
517
518     for (i = 0; i < nb_chapters; i++) {
519         if (atom.size < 9)
520             return 0;
521
522         start = avio_rb64(pb);
523         str_len = avio_r8(pb);
524
525         if ((atom.size -= 9+str_len) < 0)
526             return 0;
527
528         ret = ffio_read_size(pb, str, str_len);
529         if (ret < 0)
530             return ret;
531         str[str_len] = 0;
532         avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
533     }
534     return 0;
535 }
536
537 #define MIN_DATA_ENTRY_BOX_SIZE 12
538 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
539 {
540     AVStream *st;
541     MOVStreamContext *sc;
542     int entries, i, j;
543
544     if (c->fc->nb_streams < 1)
545         return 0;
546     st = c->fc->streams[c->fc->nb_streams-1];
547     sc = st->priv_data;
548
549     avio_rb32(pb); // version + flags
550     entries = avio_rb32(pb);
551     if (!entries ||
552         entries >  (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
553         entries >= UINT_MAX / sizeof(*sc->drefs))
554         return AVERROR_INVALIDDATA;
555     sc->drefs_count = 0;
556     av_free(sc->drefs);
557     sc->drefs_count = 0;
558     sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
559     if (!sc->drefs)
560         return AVERROR(ENOMEM);
561     sc->drefs_count = entries;
562
563     for (i = 0; i < entries; i++) {
564         MOVDref *dref = &sc->drefs[i];
565         uint32_t size = avio_rb32(pb);
566         int64_t next = avio_tell(pb) + size - 4;
567
568         if (size < 12)
569             return AVERROR_INVALIDDATA;
570
571         dref->type = avio_rl32(pb);
572         avio_rb32(pb); // version + flags
573         av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
574
575         if (dref->type == MKTAG('a','l','i','s') && size > 150) {
576             /* macintosh alias record */
577             uint16_t volume_len, len;
578             int16_t type;
579             int ret;
580
581             avio_skip(pb, 10);
582
583             volume_len = avio_r8(pb);
584             volume_len = FFMIN(volume_len, 27);
585             ret = ffio_read_size(pb, dref->volume, 27);
586             if (ret < 0)
587                 return ret;
588             dref->volume[volume_len] = 0;
589             av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
590
591             avio_skip(pb, 12);
592
593             len = avio_r8(pb);
594             len = FFMIN(len, 63);
595             ret = ffio_read_size(pb, dref->filename, 63);
596             if (ret < 0)
597                 return ret;
598             dref->filename[len] = 0;
599             av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
600
601             avio_skip(pb, 16);
602
603             /* read next level up_from_alias/down_to_target */
604             dref->nlvl_from = avio_rb16(pb);
605             dref->nlvl_to   = avio_rb16(pb);
606             av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
607                    dref->nlvl_from, dref->nlvl_to);
608
609             avio_skip(pb, 16);
610
611             for (type = 0; type != -1 && avio_tell(pb) < next; ) {
612                 if(avio_feof(pb))
613                     return AVERROR_EOF;
614                 type = avio_rb16(pb);
615                 len = avio_rb16(pb);
616                 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
617                 if (len&1)
618                     len += 1;
619                 if (type == 2) { // absolute path
620                     av_free(dref->path);
621                     dref->path = av_mallocz(len+1);
622                     if (!dref->path)
623                         return AVERROR(ENOMEM);
624
625                     ret = ffio_read_size(pb, dref->path, len);
626                     if (ret < 0) {
627                         av_freep(&dref->path);
628                         return ret;
629                     }
630                     if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
631                         len -= volume_len;
632                         memmove(dref->path, dref->path+volume_len, len);
633                         dref->path[len] = 0;
634                     }
635                     // trim string of any ending zeros
636                     for (j = len - 1; j >= 0; j--) {
637                         if (dref->path[j] == 0)
638                             len--;
639                         else
640                             break;
641                     }
642                     for (j = 0; j < len; j++)
643                         if (dref->path[j] == ':' || dref->path[j] == 0)
644                             dref->path[j] = '/';
645                     av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
646                 } else if (type == 0) { // directory name
647                     av_free(dref->dir);
648                     dref->dir = av_malloc(len+1);
649                     if (!dref->dir)
650                         return AVERROR(ENOMEM);
651
652                     ret = ffio_read_size(pb, dref->dir, len);
653                     if (ret < 0) {
654                         av_freep(&dref->dir);
655                         return ret;
656                     }
657                     dref->dir[len] = 0;
658                     for (j = 0; j < len; j++)
659                         if (dref->dir[j] == ':')
660                             dref->dir[j] = '/';
661                     av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
662                 } else
663                     avio_skip(pb, len);
664             }
665         } else {
666             av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x08%x size %d\n",
667                    dref->type, size);
668             entries--;
669             i--;
670         }
671         avio_seek(pb, next, SEEK_SET);
672     }
673     return 0;
674 }
675
676 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
677 {
678     AVStream *st;
679     uint32_t type;
680     uint32_t av_unused ctype;
681     int64_t title_size;
682     char *title_str;
683     int ret;
684
685     avio_r8(pb); /* version */
686     avio_rb24(pb); /* flags */
687
688     /* component type */
689     ctype = avio_rl32(pb);
690     type = avio_rl32(pb); /* component subtype */
691
692     av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
693     av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
694
695     if (c->trak_index < 0) {  // meta not inside a trak
696         if (type == MKTAG('m','d','t','a')) {
697             c->found_hdlr_mdta = 1;
698         }
699         return 0;
700     }
701
702     st = c->fc->streams[c->fc->nb_streams-1];
703
704     if     (type == MKTAG('v','i','d','e'))
705         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
706     else if (type == MKTAG('s','o','u','n'))
707         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
708     else if (type == MKTAG('m','1','a',' '))
709         st->codecpar->codec_id = AV_CODEC_ID_MP2;
710     else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
711         st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
712
713     avio_rb32(pb); /* component  manufacture */
714     avio_rb32(pb); /* component flags */
715     avio_rb32(pb); /* component flags mask */
716
717     title_size = atom.size - 24;
718     if (title_size > 0) {
719         title_str = av_malloc(title_size + 1); /* Add null terminator */
720         if (!title_str)
721             return AVERROR(ENOMEM);
722
723         ret = ffio_read_size(pb, title_str, title_size);
724         if (ret < 0) {
725             av_freep(&title_str);
726             return ret;
727         }
728         title_str[title_size] = 0;
729         if (title_str[0]) {
730             int off = (!c->isom && title_str[0] == title_size - 1);
731             av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
732         }
733         av_freep(&title_str);
734     }
735
736     return 0;
737 }
738
739 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
740 {
741     AVStream *st;
742     int tag;
743
744     if (fc->nb_streams < 1)
745         return 0;
746     st = fc->streams[fc->nb_streams-1];
747
748     avio_rb32(pb); /* version + flags */
749     ff_mp4_read_descr(fc, pb, &tag);
750     if (tag == MP4ESDescrTag) {
751         ff_mp4_parse_es_descr(pb, NULL);
752     } else
753         avio_rb16(pb); /* ID */
754
755     ff_mp4_read_descr(fc, pb, &tag);
756     if (tag == MP4DecConfigDescrTag)
757         ff_mp4_read_dec_config_descr(fc, st, pb);
758     return 0;
759 }
760
761 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
762 {
763     return ff_mov_read_esds(c->fc, pb);
764 }
765
766 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
767 {
768     AVStream *st;
769     enum AVAudioServiceType *ast;
770     int ac3info, acmod, lfeon, bsmod;
771
772     if (c->fc->nb_streams < 1)
773         return 0;
774     st = c->fc->streams[c->fc->nb_streams-1];
775
776     ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
777                                                             sizeof(*ast));
778     if (!ast)
779         return AVERROR(ENOMEM);
780
781     ac3info = avio_rb24(pb);
782     bsmod = (ac3info >> 14) & 0x7;
783     acmod = (ac3info >> 11) & 0x7;
784     lfeon = (ac3info >> 10) & 0x1;
785     st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
786     st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
787     if (lfeon)
788         st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
789     *ast = bsmod;
790     if (st->codecpar->channels > 1 && bsmod == 0x7)
791         *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
792
793 #if FF_API_LAVF_AVCTX
794     FF_DISABLE_DEPRECATION_WARNINGS
795     st->codec->audio_service_type = *ast;
796     FF_ENABLE_DEPRECATION_WARNINGS
797 #endif
798
799     return 0;
800 }
801
802 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
803 {
804     AVStream *st;
805     enum AVAudioServiceType *ast;
806     int eac3info, acmod, lfeon, bsmod;
807
808     if (c->fc->nb_streams < 1)
809         return 0;
810     st = c->fc->streams[c->fc->nb_streams-1];
811
812     ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
813                                                             sizeof(*ast));
814     if (!ast)
815         return AVERROR(ENOMEM);
816
817     /* No need to parse fields for additional independent substreams and its
818      * associated dependent substreams since libavcodec's E-AC-3 decoder
819      * does not support them yet. */
820     avio_rb16(pb); /* data_rate and num_ind_sub */
821     eac3info = avio_rb24(pb);
822     bsmod = (eac3info >> 12) & 0x1f;
823     acmod = (eac3info >>  9) & 0x7;
824     lfeon = (eac3info >>  8) & 0x1;
825     st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
826     if (lfeon)
827         st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
828     st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
829     *ast = bsmod;
830     if (st->codecpar->channels > 1 && bsmod == 0x7)
831         *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
832
833 #if FF_API_LAVF_AVCTX
834     FF_DISABLE_DEPRECATION_WARNINGS
835     st->codec->audio_service_type = *ast;
836     FF_ENABLE_DEPRECATION_WARNINGS
837 #endif
838
839     return 0;
840 }
841
842 static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
843 {
844     const uint32_t ddts_size = 20;
845     AVStream *st = NULL;
846     uint8_t *buf = NULL;
847     uint32_t frame_duration_code = 0;
848     uint32_t channel_layout_code = 0;
849     GetBitContext gb;
850
851     buf = av_malloc(ddts_size + FF_INPUT_BUFFER_PADDING_SIZE);
852     if (!buf) {
853         return AVERROR(ENOMEM);
854     }
855     if (avio_read(pb, buf, ddts_size) < ddts_size) {
856         av_free(buf);
857         return AVERROR_INVALIDDATA;
858     }
859
860     init_get_bits(&gb, buf, 8*ddts_size);
861
862     if (c->fc->nb_streams < 1) {
863         return 0;
864     }
865     st = c->fc->streams[c->fc->nb_streams-1];
866
867     st->codecpar->sample_rate = get_bits_long(&gb, 32);
868     skip_bits_long(&gb, 32); /* max bitrate */
869     st->codecpar->bit_rate = get_bits_long(&gb, 32);
870     st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
871     frame_duration_code = get_bits(&gb, 2);
872     skip_bits(&gb, 30); /* various fields */
873     channel_layout_code = get_bits(&gb, 16);
874
875     st->codecpar->frame_size =
876             (frame_duration_code == 0) ? 512 :
877             (frame_duration_code == 1) ? 1024 :
878             (frame_duration_code == 2) ? 2048 :
879             (frame_duration_code == 3) ? 4096 : 0;
880
881     if (channel_layout_code > 0xff) {
882         av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout");
883     }
884     st->codecpar->channel_layout =
885             ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
886             ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
887             ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
888             ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
889             ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
890             ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
891
892     st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
893
894     return 0;
895 }
896
897 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
898 {
899     AVStream *st;
900
901     if (c->fc->nb_streams < 1)
902         return 0;
903     st = c->fc->streams[c->fc->nb_streams-1];
904
905     if (atom.size < 16)
906         return 0;
907
908     /* skip version and flags */
909     avio_skip(pb, 4);
910
911     ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
912
913     return 0;
914 }
915
916 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
917 {
918     AVStream *st;
919     int ret;
920
921     if (c->fc->nb_streams < 1)
922         return 0;
923     st = c->fc->streams[c->fc->nb_streams-1];
924
925     if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
926         av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
927
928     return ret;
929 }
930
931 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
932 {
933     const int num = avio_rb32(pb);
934     const int den = avio_rb32(pb);
935     AVStream *st;
936
937     if (c->fc->nb_streams < 1)
938         return 0;
939     st = c->fc->streams[c->fc->nb_streams-1];
940
941     if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
942         (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
943         av_log(c->fc, AV_LOG_WARNING,
944                "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
945                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
946                num, den);
947     } else if (den != 0) {
948         av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
949                   num, den, 32767);
950     }
951     return 0;
952 }
953
954 /* this atom contains actual media data */
955 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
956 {
957     if (atom.size == 0) /* wrong one (MP4) */
958         return 0;
959     c->found_mdat=1;
960     return 0; /* now go for moov */
961 }
962
963 #define DRM_BLOB_SIZE 56
964
965 static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
966 {
967     uint8_t intermediate_key[20];
968     uint8_t intermediate_iv[20];
969     uint8_t input[64];
970     uint8_t output[64];
971     uint8_t file_checksum[20];
972     uint8_t calculated_checksum[20];
973     struct AVSHA *sha;
974     int i;
975     int ret = 0;
976     uint8_t *activation_bytes = c->activation_bytes;
977     uint8_t *fixed_key = c->audible_fixed_key;
978
979     c->aax_mode = 1;
980
981     sha = av_sha_alloc();
982     if (!sha)
983         return AVERROR(ENOMEM);
984     c->aes_decrypt = av_aes_alloc();
985     if (!c->aes_decrypt) {
986         ret = AVERROR(ENOMEM);
987         goto fail;
988     }
989
990     /* drm blob processing */
991     avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
992     avio_read(pb, input, DRM_BLOB_SIZE);
993     avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
994     avio_read(pb, file_checksum, 20);
995
996     av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
997     for (i = 0; i < 20; i++)
998         av_log(sha, AV_LOG_INFO, "%02x", file_checksum[i]);
999     av_log(c->fc, AV_LOG_INFO, "\n");
1000
1001     /* verify activation data */
1002     if (!activation_bytes) {
1003         av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1004         ret = 0;  /* allow ffprobe to continue working on .aax files */
1005         goto fail;
1006     }
1007     if (c->activation_bytes_size != 4) {
1008         av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1009         ret = AVERROR(EINVAL);
1010         goto fail;
1011     }
1012
1013     /* verify fixed key */
1014     if (c->audible_fixed_key_size != 16) {
1015         av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1016         ret = AVERROR(EINVAL);
1017         goto fail;
1018     }
1019
1020     /* AAX (and AAX+) key derivation */
1021     av_sha_init(sha, 160);
1022     av_sha_update(sha, fixed_key, 16);
1023     av_sha_update(sha, activation_bytes, 4);
1024     av_sha_final(sha, intermediate_key);
1025     av_sha_init(sha, 160);
1026     av_sha_update(sha, fixed_key, 16);
1027     av_sha_update(sha, intermediate_key, 20);
1028     av_sha_update(sha, activation_bytes, 4);
1029     av_sha_final(sha, intermediate_iv);
1030     av_sha_init(sha, 160);
1031     av_sha_update(sha, intermediate_key, 16);
1032     av_sha_update(sha, intermediate_iv, 16);
1033     av_sha_final(sha, calculated_checksum);
1034     if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1035         av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1036         ret = AVERROR_INVALIDDATA;
1037         goto fail;
1038     }
1039     av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1040     av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1041     for (i = 0; i < 4; i++) {
1042         // file data (in output) is stored in big-endian mode
1043         if (activation_bytes[i] != output[3 - i]) { // critical error
1044             av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1045             ret = AVERROR_INVALIDDATA;
1046             goto fail;
1047         }
1048     }
1049     memcpy(c->file_key, output + 8, 16);
1050     memcpy(input, output + 26, 16);
1051     av_sha_init(sha, 160);
1052     av_sha_update(sha, input, 16);
1053     av_sha_update(sha, c->file_key, 16);
1054     av_sha_update(sha, fixed_key, 16);
1055     av_sha_final(sha, c->file_iv);
1056
1057 fail:
1058     av_free(sha);
1059
1060     return ret;
1061 }
1062
1063 // Audible AAX (and AAX+) bytestream decryption
1064 static int aax_filter(uint8_t *input, int size, MOVContext *c)
1065 {
1066     int blocks = 0;
1067     unsigned char iv[16];
1068
1069     memcpy(iv, c->file_iv, 16); // iv is overwritten
1070     blocks = size >> 4; // trailing bytes are not encrypted!
1071     av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1072     av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1073
1074     return 0;
1075 }
1076
1077 /* read major brand, minor version and compatible brands and store them as metadata */
1078 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1079 {
1080     uint32_t minor_ver;
1081     int comp_brand_size;
1082     char* comp_brands_str;
1083     uint8_t type[5] = {0};
1084     int ret = ffio_read_size(pb, type, 4);
1085     if (ret < 0)
1086         return ret;
1087
1088     if (strcmp(type, "qt  "))
1089         c->isom = 1;
1090     av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1091     av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1092     minor_ver = avio_rb32(pb); /* minor version */
1093     av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1094
1095     comp_brand_size = atom.size - 8;
1096     if (comp_brand_size < 0)
1097         return AVERROR_INVALIDDATA;
1098     comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1099     if (!comp_brands_str)
1100         return AVERROR(ENOMEM);
1101
1102     ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1103     if (ret < 0) {
1104         av_freep(&comp_brands_str);
1105         return ret;
1106     }
1107     comp_brands_str[comp_brand_size] = 0;
1108     av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
1109     av_freep(&comp_brands_str);
1110
1111     return 0;
1112 }
1113
1114 /* this atom should contain all header atoms */
1115 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1116 {
1117     int ret;
1118
1119     if (c->found_moov) {
1120         av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1121         avio_skip(pb, atom.size);
1122         return 0;
1123     }
1124
1125     if ((ret = mov_read_default(c, pb, atom)) < 0)
1126         return ret;
1127     /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1128     /* so we don't parse the whole file if over a network */
1129     c->found_moov=1;
1130     return 0; /* now go for mdat */
1131 }
1132
1133 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1134 {
1135     if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1136         c->has_looked_for_mfra = 1;
1137         if (pb->seekable) {
1138             int ret;
1139             av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1140                     "for a mfra\n");
1141             if ((ret = mov_read_mfra(c, pb)) < 0) {
1142                 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1143                         "read the mfra (may be a live ismv)\n");
1144             }
1145         } else {
1146             av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1147                     "seekable, can not look for mfra\n");
1148         }
1149     }
1150     c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1151     av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1152     return mov_read_default(c, pb, atom);
1153 }
1154
1155 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
1156 {
1157     char buffer[32];
1158     if (time) {
1159         struct tm *ptm, tmbuf;
1160         time_t timet;
1161         if(time >= 2082844800)
1162             time -= 2082844800;  /* seconds between 1904-01-01 and Epoch */
1163         timet = time;
1164         ptm = gmtime_r(&timet, &tmbuf);
1165         if (!ptm) return;
1166         if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
1167             av_dict_set(metadata, "creation_time", buffer, 0);
1168     }
1169 }
1170
1171 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1172 {
1173     AVStream *st;
1174     MOVStreamContext *sc;
1175     int version;
1176     char language[4] = {0};
1177     unsigned lang;
1178     int64_t creation_time;
1179
1180     if (c->fc->nb_streams < 1)
1181         return 0;
1182     st = c->fc->streams[c->fc->nb_streams-1];
1183     sc = st->priv_data;
1184
1185     if (sc->time_scale) {
1186         av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1187         return AVERROR_INVALIDDATA;
1188     }
1189
1190     version = avio_r8(pb);
1191     if (version > 1) {
1192         avpriv_request_sample(c->fc, "Version %d", version);
1193         return AVERROR_PATCHWELCOME;
1194     }
1195     avio_rb24(pb); /* flags */
1196     if (version == 1) {
1197         creation_time = avio_rb64(pb);
1198         avio_rb64(pb);
1199     } else {
1200         creation_time = avio_rb32(pb);
1201         avio_rb32(pb); /* modification time */
1202     }
1203     mov_metadata_creation_time(&st->metadata, creation_time);
1204
1205     sc->time_scale = avio_rb32(pb);
1206     st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1207
1208     lang = avio_rb16(pb); /* language */
1209     if (ff_mov_lang_to_iso639(lang, language))
1210         av_dict_set(&st->metadata, "language", language, 0);
1211     avio_rb16(pb); /* quality */
1212
1213     return 0;
1214 }
1215
1216 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1217 {
1218     int64_t creation_time;
1219     int version = avio_r8(pb); /* version */
1220     avio_rb24(pb); /* flags */
1221
1222     if (version == 1) {
1223         creation_time = avio_rb64(pb);
1224         avio_rb64(pb);
1225     } else {
1226         creation_time = avio_rb32(pb);
1227         avio_rb32(pb); /* modification time */
1228     }
1229     mov_metadata_creation_time(&c->fc->metadata, creation_time);
1230     c->time_scale = avio_rb32(pb); /* time scale */
1231
1232     av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1233
1234     c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1235     // set the AVCodecContext duration because the duration of individual tracks
1236     // may be inaccurate
1237     if (c->time_scale > 0 && !c->trex_data)
1238         c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1239     avio_rb32(pb); /* preferred scale */
1240
1241     avio_rb16(pb); /* preferred volume */
1242
1243     avio_skip(pb, 10); /* reserved */
1244
1245     avio_skip(pb, 36); /* display matrix */
1246
1247     avio_rb32(pb); /* preview time */
1248     avio_rb32(pb); /* preview duration */
1249     avio_rb32(pb); /* poster time */
1250     avio_rb32(pb); /* selection time */
1251     avio_rb32(pb); /* selection duration */
1252     avio_rb32(pb); /* current time */
1253     avio_rb32(pb); /* next track ID */
1254
1255     return 0;
1256 }
1257
1258 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1259 {
1260     AVStream *st;
1261     int little_endian;
1262
1263     if (c->fc->nb_streams < 1)
1264         return 0;
1265     st = c->fc->streams[c->fc->nb_streams-1];
1266
1267     little_endian = avio_rb16(pb) & 0xFF;
1268     av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1269     if (little_endian == 1) {
1270         switch (st->codecpar->codec_id) {
1271         case AV_CODEC_ID_PCM_S24BE:
1272             st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
1273             break;
1274         case AV_CODEC_ID_PCM_S32BE:
1275             st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
1276             break;
1277         case AV_CODEC_ID_PCM_F32BE:
1278             st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
1279             break;
1280         case AV_CODEC_ID_PCM_F64BE:
1281             st->codecpar->codec_id = AV_CODEC_ID_PCM_F64LE;
1282             break;
1283         default:
1284             break;
1285         }
1286     }
1287     return 0;
1288 }
1289
1290 static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1291 {
1292     AVStream *st;
1293     char color_parameter_type[5] = { 0 };
1294     uint16_t color_primaries, color_trc, color_matrix;
1295     int ret;
1296
1297     if (c->fc->nb_streams < 1)
1298         return 0;
1299     st = c->fc->streams[c->fc->nb_streams - 1];
1300
1301     ret = ffio_read_size(pb, color_parameter_type, 4);
1302     if (ret < 0)
1303         return ret;
1304     if (strncmp(color_parameter_type, "nclx", 4) &&
1305         strncmp(color_parameter_type, "nclc", 4)) {
1306         av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1307                color_parameter_type);
1308         return 0;
1309     }
1310
1311     color_primaries = avio_rb16(pb);
1312     color_trc = avio_rb16(pb);
1313     color_matrix = avio_rb16(pb);
1314
1315     av_log(c->fc, AV_LOG_TRACE,
1316            "%s: pri %d trc %d matrix %d",
1317            color_parameter_type, color_primaries, color_trc, color_matrix);
1318
1319     if (!strncmp(color_parameter_type, "nclx", 4)) {
1320         uint8_t color_range = avio_r8(pb) >> 7;
1321         av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1322         if (color_range)
1323             st->codecpar->color_range = AVCOL_RANGE_JPEG;
1324         else
1325             st->codecpar->color_range = AVCOL_RANGE_MPEG;
1326         /* 14496-12 references JPEG XR specs (rather than the more complete
1327          * 23001-8) so some adjusting is required */
1328         if (color_primaries >= AVCOL_PRI_FILM)
1329             color_primaries = AVCOL_PRI_UNSPECIFIED;
1330         if ((color_trc >= AVCOL_TRC_LINEAR &&
1331              color_trc <= AVCOL_TRC_LOG_SQRT) ||
1332             color_trc >= AVCOL_TRC_BT2020_10)
1333             color_trc = AVCOL_TRC_UNSPECIFIED;
1334         if (color_matrix >= AVCOL_SPC_BT2020_NCL)
1335             color_matrix = AVCOL_SPC_UNSPECIFIED;
1336         st->codecpar->color_primaries = color_primaries;
1337         st->codecpar->color_trc       = color_trc;
1338         st->codecpar->color_space     = color_matrix;
1339     } else if (!strncmp(color_parameter_type, "nclc", 4)) {
1340         /* color primaries, Table 4-4 */
1341         switch (color_primaries) {
1342         case 1: st->codecpar->color_primaries = AVCOL_PRI_BT709; break;
1343         case 5: st->codecpar->color_primaries = AVCOL_PRI_SMPTE170M; break;
1344         case 6: st->codecpar->color_primaries = AVCOL_PRI_SMPTE240M; break;
1345         }
1346         /* color transfer, Table 4-5 */
1347         switch (color_trc) {
1348         case 1: st->codecpar->color_trc = AVCOL_TRC_BT709; break;
1349         case 7: st->codecpar->color_trc = AVCOL_TRC_SMPTE240M; break;
1350         }
1351         /* color matrix, Table 4-6 */
1352         switch (color_matrix) {
1353         case 1: st->codecpar->color_space = AVCOL_SPC_BT709; break;
1354         case 6: st->codecpar->color_space = AVCOL_SPC_BT470BG; break;
1355         case 7: st->codecpar->color_space = AVCOL_SPC_SMPTE240M; break;
1356         }
1357     }
1358     av_log(c->fc, AV_LOG_TRACE, "\n");
1359
1360     return 0;
1361 }
1362
1363 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1364 {
1365     AVStream *st;
1366     unsigned mov_field_order;
1367     enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1368
1369     if (c->fc->nb_streams < 1) // will happen with jp2 files
1370         return 0;
1371     st = c->fc->streams[c->fc->nb_streams-1];
1372     if (atom.size < 2)
1373         return AVERROR_INVALIDDATA;
1374     mov_field_order = avio_rb16(pb);
1375     if ((mov_field_order & 0xFF00) == 0x0100)
1376         decoded_field_order = AV_FIELD_PROGRESSIVE;
1377     else if ((mov_field_order & 0xFF00) == 0x0200) {
1378         switch (mov_field_order & 0xFF) {
1379         case 0x01: decoded_field_order = AV_FIELD_TT;
1380                    break;
1381         case 0x06: decoded_field_order = AV_FIELD_BB;
1382                    break;
1383         case 0x09: decoded_field_order = AV_FIELD_TB;
1384                    break;
1385         case 0x0E: decoded_field_order = AV_FIELD_BT;
1386                    break;
1387         }
1388     }
1389     if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1390         av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1391     }
1392     st->codecpar->field_order = decoded_field_order;
1393
1394     return 0;
1395 }
1396
1397 static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
1398 {
1399     int err = 0;
1400     uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1401     if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1402         return AVERROR_INVALIDDATA;
1403     if ((err = av_reallocp(&par->extradata, size)) < 0) {
1404         par->extradata_size = 0;
1405         return err;
1406     }
1407     par->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE;
1408     return 0;
1409 }
1410
1411 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1412 static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1413                                         AVCodecParameters *par, uint8_t *buf)
1414 {
1415     int64_t result = atom.size;
1416     int err;
1417
1418     AV_WB32(buf    , atom.size + 8);
1419     AV_WL32(buf + 4, atom.type);
1420     err = ffio_read_size(pb, buf + 8, atom.size);
1421     if (err < 0) {
1422         par->extradata_size -= atom.size;
1423         return err;
1424     } else if (err < atom.size) {
1425         av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1426         par->extradata_size -= atom.size - err;
1427         result = err;
1428     }
1429     memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1430     return result;
1431 }
1432
1433 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1434 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1435                               enum AVCodecID codec_id)
1436 {
1437     AVStream *st;
1438     uint64_t original_size;
1439     int err;
1440
1441     if (c->fc->nb_streams < 1) // will happen with jp2 files
1442         return 0;
1443     st = c->fc->streams[c->fc->nb_streams-1];
1444
1445     if (st->codecpar->codec_id != codec_id)
1446         return 0; /* unexpected codec_id - don't mess with extradata */
1447
1448     original_size = st->codecpar->extradata_size;
1449     err = mov_realloc_extradata(st->codecpar, atom);
1450     if (err)
1451         return err;
1452
1453     err =  mov_read_atom_into_extradata(c, pb, atom, st->codecpar,  st->codecpar->extradata + original_size);
1454     if (err < 0)
1455         return err;
1456     return 0; // Note: this is the original behavior to ignore truncation.
1457 }
1458
1459 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1460 static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1461 {
1462     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1463 }
1464
1465 static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1466 {
1467     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1468 }
1469
1470 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1471 {
1472     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1473 }
1474
1475 static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1476 {
1477     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1478 }
1479
1480 static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1481 {
1482     int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1483     if(ret == 0)
1484         ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1485     return ret;
1486 }
1487
1488 static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1489 {
1490     int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1491
1492     if (!ret && c->fc->nb_streams >= 1) {
1493         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1494         if (par->extradata_size >= 40) {
1495             par->height = AV_RB16(&par->extradata[36]);
1496             par->width  = AV_RB16(&par->extradata[38]);
1497         }
1498     }
1499     return ret;
1500 }
1501
1502 static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1503 {
1504     if (c->fc->nb_streams >= 1) {
1505         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1506         if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1507             par->codec_id == AV_CODEC_ID_H264 &&
1508             atom.size > 11) {
1509             int cid;
1510             avio_skip(pb, 10);
1511             cid = avio_rb16(pb);
1512             /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1513             if (cid == 0xd4d || cid == 0xd4e)
1514                 par->width = 1440;
1515             return 0;
1516         } else if (par->codec_tag == MKTAG('A', 'V', 'd', '1') &&
1517                    atom.size >= 24) {
1518             int num, den;
1519             avio_skip(pb, 12);
1520             num = avio_rb32(pb);
1521             den = avio_rb32(pb);
1522             if (num <= 0 || den <= 0)
1523                 return 0;
1524             switch (avio_rb32(pb)) {
1525             case 2:
1526                 if (den >= INT_MAX / 2)
1527                     return 0;
1528                 den *= 2;
1529             case 1:
1530                 c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.num = num;
1531                 c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.den = den;
1532             default:
1533                 return 0;
1534             }
1535         }
1536     }
1537
1538     return mov_read_avid(c, pb, atom);
1539 }
1540
1541 static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1542 {
1543     int ret = 0;
1544     int length = 0;
1545     uint64_t original_size;
1546     if (c->fc->nb_streams >= 1) {
1547         AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1548         if (par->codec_id == AV_CODEC_ID_H264)
1549             return 0;
1550         if (atom.size == 16) {
1551             original_size = par->extradata_size;
1552             ret = mov_realloc_extradata(par, atom);
1553             if (!ret) {
1554                 length =  mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
1555                 if (length == atom.size) {
1556                     const uint8_t range_value = par->extradata[original_size + 19];
1557                     switch (range_value) {
1558                     case 1:
1559                         par->color_range = AVCOL_RANGE_MPEG;
1560                         break;
1561                     case 2:
1562                         par->color_range = AVCOL_RANGE_JPEG;
1563                         break;
1564                     default:
1565                         av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1566                         break;
1567                     }
1568                     ff_dlog(c, "color_range: %d\n", par->color_range);
1569                 } else {
1570                   /* For some reason the whole atom was not added to the extradata */
1571                   av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1572                 }
1573             } else {
1574                 av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1575             }
1576         } else {
1577             av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1578         }
1579     }
1580
1581     return ret;
1582 }
1583
1584 static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1585 {
1586     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1587 }
1588
1589 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1590 {
1591     AVStream *st;
1592     int ret;
1593
1594     if (c->fc->nb_streams < 1)
1595         return 0;
1596     st = c->fc->streams[c->fc->nb_streams-1];
1597
1598     if ((uint64_t)atom.size > (1<<30))
1599         return AVERROR_INVALIDDATA;
1600
1601     if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
1602         st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
1603         st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
1604         // pass all frma atom to codec, needed at least for QDMC and QDM2
1605         av_freep(&st->codecpar->extradata);
1606         ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1607         if (ret < 0)
1608             return ret;
1609     } else if (atom.size > 8) { /* to read frma, esds atoms */
1610         if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1611             uint64_t buffer;
1612             ret = ffio_ensure_seekback(pb, 8);
1613             if (ret < 0)
1614                 return ret;
1615             buffer = avio_rb64(pb);
1616             atom.size -= 8;
1617             if (  (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1618                 && buffer >> 32 <= atom.size
1619                 && buffer >> 32 >= 8) {
1620                 avio_skip(pb, -8);
1621                 atom.size += 8;
1622             } else if (!st->codecpar->extradata_size) {
1623 #define ALAC_EXTRADATA_SIZE 36
1624                 st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
1625                 if (!st->codecpar->extradata)
1626                     return AVERROR(ENOMEM);
1627                 st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
1628                 AV_WB32(st->codecpar->extradata    , ALAC_EXTRADATA_SIZE);
1629                 AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
1630                 AV_WB64(st->codecpar->extradata + 12, buffer);
1631                 avio_read(pb, st->codecpar->extradata + 20, 16);
1632                 avio_skip(pb, atom.size - 24);
1633                 return 0;
1634             }
1635         }
1636         if ((ret = mov_read_default(c, pb, atom)) < 0)
1637             return ret;
1638     } else
1639         avio_skip(pb, atom.size);
1640     return 0;
1641 }
1642
1643 /**
1644  * This function reads atom content and puts data in extradata without tag
1645  * nor size unlike mov_read_extradata.
1646  */
1647 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1648 {
1649     AVStream *st;
1650     int ret;
1651
1652     if (c->fc->nb_streams < 1)
1653         return 0;
1654     st = c->fc->streams[c->fc->nb_streams-1];
1655
1656     if ((uint64_t)atom.size > (1<<30))
1657         return AVERROR_INVALIDDATA;
1658
1659     if (atom.size >= 10) {
1660         // Broken files created by legacy versions of libavformat will
1661         // wrap a whole fiel atom inside of a glbl atom.
1662         unsigned size = avio_rb32(pb);
1663         unsigned type = avio_rl32(pb);
1664         avio_seek(pb, -8, SEEK_CUR);
1665         if (type == MKTAG('f','i','e','l') && size == atom.size)
1666             return mov_read_default(c, pb, atom);
1667     }
1668     if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
1669         av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
1670         return 0;
1671     }
1672     av_freep(&st->codecpar->extradata);
1673     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1674     if (ret < 0)
1675         return ret;
1676
1677     return 0;
1678 }
1679
1680 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1681 {
1682     AVStream *st;
1683     uint8_t profile_level;
1684     int ret;
1685
1686     if (c->fc->nb_streams < 1)
1687         return 0;
1688     st = c->fc->streams[c->fc->nb_streams-1];
1689
1690     if (atom.size >= (1<<28) || atom.size < 7)
1691         return AVERROR_INVALIDDATA;
1692
1693     profile_level = avio_r8(pb);
1694     if ((profile_level & 0xf0) != 0xc0)
1695         return 0;
1696
1697     avio_seek(pb, 6, SEEK_CUR);
1698     av_freep(&st->codecpar->extradata);
1699     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
1700     if (ret < 0)
1701         return ret;
1702
1703     return 0;
1704 }
1705
1706 /**
1707  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1708  * but can have extradata appended at the end after the 40 bytes belonging
1709  * to the struct.
1710  */
1711 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1712 {
1713     AVStream *st;
1714     int ret;
1715
1716     if (c->fc->nb_streams < 1)
1717         return 0;
1718     if (atom.size <= 40)
1719         return 0;
1720     st = c->fc->streams[c->fc->nb_streams-1];
1721
1722     if ((uint64_t)atom.size > (1<<30))
1723         return AVERROR_INVALIDDATA;
1724
1725     avio_skip(pb, 40);
1726     av_freep(&st->codecpar->extradata);
1727     ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
1728     if (ret < 0)
1729         return ret;
1730
1731     return 0;
1732 }
1733
1734 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1735 {
1736     AVStream *st;
1737     MOVStreamContext *sc;
1738     unsigned int i, entries;
1739
1740     if (c->fc->nb_streams < 1)
1741         return 0;
1742     st = c->fc->streams[c->fc->nb_streams-1];
1743     sc = st->priv_data;
1744
1745     avio_r8(pb); /* version */
1746     avio_rb24(pb); /* flags */
1747
1748     entries = avio_rb32(pb);
1749
1750     if (!entries)
1751         return 0;
1752
1753     if (sc->chunk_offsets)
1754         av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
1755     av_free(sc->chunk_offsets);
1756     sc->chunk_count = 0;
1757     sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
1758     if (!sc->chunk_offsets)
1759         return AVERROR(ENOMEM);
1760     sc->chunk_count = entries;
1761
1762     if      (atom.type == MKTAG('s','t','c','o'))
1763         for (i = 0; i < entries && !pb->eof_reached; i++)
1764             sc->chunk_offsets[i] = avio_rb32(pb);
1765     else if (atom.type == MKTAG('c','o','6','4'))
1766         for (i = 0; i < entries && !pb->eof_reached; i++)
1767             sc->chunk_offsets[i] = avio_rb64(pb);
1768     else
1769         return AVERROR_INVALIDDATA;
1770
1771     sc->chunk_count = i;
1772
1773     if (pb->eof_reached)
1774         return AVERROR_EOF;
1775
1776     return 0;
1777 }
1778
1779 /**
1780  * Compute codec id for 'lpcm' tag.
1781  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1782  */
1783 enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1784 {
1785     /* lpcm flags:
1786      * 0x1 = float
1787      * 0x2 = big-endian
1788      * 0x4 = signed
1789      */
1790     return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1791 }
1792
1793 static int mov_codec_id(AVStream *st, uint32_t format)
1794 {
1795     int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1796
1797     if (id <= 0 &&
1798         ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1799          (format & 0xFFFF) == 'T' + ('S' << 8)))
1800         id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1801
1802     if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1803         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1804     } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
1805                /* skip old ASF MPEG-4 tag */
1806                format && format != MKTAG('m','p','4','s')) {
1807         id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1808         if (id <= 0)
1809             id = ff_codec_get_id(ff_codec_bmp_tags, format);
1810         if (id > 0)
1811             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
1812         else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
1813                     (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1814                     st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
1815             id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1816             if (id > 0)
1817                 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
1818         }
1819     }
1820
1821     st->codecpar->codec_tag = format;
1822
1823     return id;
1824 }
1825
1826 static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1827                                  AVStream *st, MOVStreamContext *sc)
1828 {
1829     uint8_t codec_name[32];
1830     int64_t stsd_start;
1831     unsigned int len;
1832
1833     /* The first 16 bytes of the video sample description are already
1834      * read in ff_mov_read_stsd_entries() */
1835     stsd_start = avio_tell(pb) - 16;
1836
1837     avio_rb16(pb); /* version */
1838     avio_rb16(pb); /* revision level */
1839     avio_rb32(pb); /* vendor */
1840     avio_rb32(pb); /* temporal quality */
1841     avio_rb32(pb); /* spatial quality */
1842
1843     st->codecpar->width  = avio_rb16(pb); /* width */
1844     st->codecpar->height = avio_rb16(pb); /* height */
1845
1846     avio_rb32(pb); /* horiz resolution */
1847     avio_rb32(pb); /* vert resolution */
1848     avio_rb32(pb); /* data size, always 0 */
1849     avio_rb16(pb); /* frames per samples */
1850
1851     len = avio_r8(pb); /* codec name, pascal string */
1852     if (len > 31)
1853         len = 31;
1854     mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1855     if (len < 31)
1856         avio_skip(pb, 31 - len);
1857
1858     if (codec_name[0])
1859         av_dict_set(&st->metadata, "encoder", codec_name, 0);
1860
1861     /* codec_tag YV12 triggers an UV swap in rawdec.c */
1862     if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1863         st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
1864         st->codecpar->width &= ~1;
1865         st->codecpar->height &= ~1;
1866     }
1867     /* Flash Media Server uses tag H.263 with Sorenson Spark */
1868     if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
1869         !memcmp(codec_name, "Sorenson H263", 13))
1870         st->codecpar->codec_id = AV_CODEC_ID_FLV1;
1871
1872     st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
1873
1874     avio_seek(pb, stsd_start, SEEK_SET);
1875
1876     if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
1877         st->codecpar->bits_per_coded_sample &= 0x1F;
1878         sc->has_palette = 1;
1879     }
1880 }
1881
1882 static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1883                                  AVStream *st, MOVStreamContext *sc)
1884 {
1885     int bits_per_sample, flags;
1886     uint16_t version = avio_rb16(pb);
1887     AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1888
1889     avio_rb16(pb); /* revision level */
1890     avio_rb32(pb); /* vendor */
1891
1892     st->codecpar->channels              = avio_rb16(pb); /* channel count */
1893     st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1894     av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
1895
1896     sc->audio_cid = avio_rb16(pb);
1897     avio_rb16(pb); /* packet size = 0 */
1898
1899     st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
1900
1901     // Read QT version 1 fields. In version 0 these do not exist.
1902     av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
1903     if (!c->isom ||
1904         (compatible_brands && strstr(compatible_brands->value, "qt  "))) {
1905
1906         if (version == 1) {
1907             sc->samples_per_frame = avio_rb32(pb);
1908             avio_rb32(pb); /* bytes per packet */
1909             sc->bytes_per_frame = avio_rb32(pb);
1910             avio_rb32(pb); /* bytes per sample */
1911         } else if (version == 2) {
1912             avio_rb32(pb); /* sizeof struct only */
1913             st->codecpar->sample_rate = av_int2double(avio_rb64(pb));
1914             st->codecpar->channels    = avio_rb32(pb);
1915             avio_rb32(pb); /* always 0x7F000000 */
1916             st->codecpar->bits_per_coded_sample = avio_rb32(pb);
1917
1918             flags = avio_rb32(pb); /* lpcm format specific flag */
1919             sc->bytes_per_frame   = avio_rb32(pb);
1920             sc->samples_per_frame = avio_rb32(pb);
1921             if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
1922                 st->codecpar->codec_id =
1923                     ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample,
1924                                              flags);
1925         }
1926         if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1927             /* can't correctly handle variable sized packet as audio unit */
1928             switch (st->codecpar->codec_id) {
1929             case AV_CODEC_ID_MP2:
1930             case AV_CODEC_ID_MP3:
1931                 st->need_parsing = AVSTREAM_PARSE_FULL;
1932                 break;
1933             }
1934         }
1935     }
1936
1937     if (sc->format == 0) {
1938         if (st->codecpar->bits_per_coded_sample == 8)
1939             st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
1940         else if (st->codecpar->bits_per_coded_sample == 16)
1941             st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
1942     }
1943
1944     switch (st->codecpar->codec_id) {
1945     case AV_CODEC_ID_PCM_S8:
1946     case AV_CODEC_ID_PCM_U8:
1947         if (st->codecpar->bits_per_coded_sample == 16)
1948             st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
1949         break;
1950     case AV_CODEC_ID_PCM_S16LE:
1951     case AV_CODEC_ID_PCM_S16BE:
1952         if (st->codecpar->bits_per_coded_sample == 8)
1953             st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
1954         else if (st->codecpar->bits_per_coded_sample == 24)
1955             st->codecpar->codec_id =
1956                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
1957                 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1958         else if (st->codecpar->bits_per_coded_sample == 32)
1959              st->codecpar->codec_id =
1960                 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
1961                 AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
1962         break;
1963     /* set values for old format before stsd version 1 appeared */
1964     case AV_CODEC_ID_MACE3:
1965         sc->samples_per_frame = 6;
1966         sc->bytes_per_frame   = 2 * st->codecpar->channels;
1967         break;
1968     case AV_CODEC_ID_MACE6:
1969         sc->samples_per_frame = 6;
1970         sc->bytes_per_frame   = 1 * st->codecpar->channels;
1971         break;
1972     case AV_CODEC_ID_ADPCM_IMA_QT:
1973         sc->samples_per_frame = 64;
1974         sc->bytes_per_frame   = 34 * st->codecpar->channels;
1975         break;
1976     case AV_CODEC_ID_GSM:
1977         sc->samples_per_frame = 160;
1978         sc->bytes_per_frame   = 33;
1979         break;
1980     default:
1981         break;
1982     }
1983
1984     bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
1985     if (bits_per_sample) {
1986         st->codecpar->bits_per_coded_sample = bits_per_sample;
1987         sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
1988     }
1989 }
1990
1991 static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1992                                     AVStream *st, MOVStreamContext *sc,
1993                                     int64_t size)
1994 {
1995     // ttxt stsd contains display flags, justification, background
1996     // color, fonts, and default styles, so fake an atom to read it
1997     MOVAtom fake_atom = { .size = size };
1998     // mp4s contains a regular esds atom
1999     if (st->codecpar->codec_tag != AV_RL32("mp4s"))
2000         mov_read_glbl(c, pb, fake_atom);
2001     st->codecpar->width  = sc->width;
2002     st->codecpar->height = sc->height;
2003 }
2004
2005 static uint32_t yuv_to_rgba(uint32_t ycbcr)
2006 {
2007     uint8_t r, g, b;
2008     int y, cb, cr;
2009
2010     y  = (ycbcr >> 16) & 0xFF;
2011     cr = (ycbcr >> 8)  & 0xFF;
2012     cb =  ycbcr        & 0xFF;
2013
2014     b = av_clip_uint8((1164 * (y - 16)                     + 2018 * (cb - 128)) / 1000);
2015     g = av_clip_uint8((1164 * (y - 16) -  813 * (cr - 128) -  391 * (cb - 128)) / 1000);
2016     r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128)                    ) / 1000);
2017
2018     return (r << 16) | (g << 8) | b;
2019 }
2020
2021 static int mov_rewrite_dvd_sub_extradata(AVStream *st)
2022 {
2023     char buf[256] = {0};
2024     uint8_t *src = st->codecpar->extradata;
2025     int i;
2026
2027     if (st->codecpar->extradata_size != 64)
2028         return 0;
2029
2030     if (st->codecpar->width > 0 &&  st->codecpar->height > 0)
2031         snprintf(buf, sizeof(buf), "size: %dx%d\n",
2032                  st->codecpar->width, st->codecpar->height);
2033     av_strlcat(buf, "palette: ", sizeof(buf));
2034
2035     for (i = 0; i < 16; i++) {
2036         uint32_t yuv = AV_RB32(src + i * 4);
2037         uint32_t rgba = yuv_to_rgba(yuv);
2038
2039         av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
2040     }
2041
2042     if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
2043         return 0;
2044
2045     av_freep(&st->codecpar->extradata);
2046     st->codecpar->extradata_size = 0;
2047     st->codecpar->extradata = av_mallocz(strlen(buf) + AV_INPUT_BUFFER_PADDING_SIZE);
2048     if (!st->codecpar->extradata)
2049         return AVERROR(ENOMEM);
2050     st->codecpar->extradata_size = strlen(buf);
2051     memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
2052
2053     return 0;
2054 }
2055
2056 static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
2057                                 AVStream *st, MOVStreamContext *sc,
2058                                 int64_t size)
2059 {
2060     int ret;
2061
2062     if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2063         if ((int)size != size)
2064             return AVERROR(ENOMEM);
2065
2066         ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2067         if (ret < 0)
2068             return ret;
2069         if (size > 16) {
2070             MOVStreamContext *tmcd_ctx = st->priv_data;
2071             int val;
2072             val = AV_RB32(st->codecpar->extradata + 4);
2073             tmcd_ctx->tmcd_flags = val;
2074             st->avg_frame_rate.num = st->codecpar->extradata[16]; /* number of frame */
2075             st->avg_frame_rate.den = 1;
2076 #if FF_API_LAVF_AVCTX
2077 FF_DISABLE_DEPRECATION_WARNINGS
2078             st->codec->time_base = av_inv_q(st->avg_frame_rate);
2079 FF_ENABLE_DEPRECATION_WARNINGS
2080 #endif
2081             /* adjust for per frame dur in counter mode */
2082             if (tmcd_ctx->tmcd_flags & 0x0008) {
2083                 int timescale = AV_RB32(st->codecpar->extradata + 8);
2084                 int framedur = AV_RB32(st->codecpar->extradata + 12);
2085                 st->avg_frame_rate.num *= timescale;
2086                 st->avg_frame_rate.den *= framedur;
2087 #if FF_API_LAVF_AVCTX
2088 FF_DISABLE_DEPRECATION_WARNINGS
2089                 st->codec->time_base.den *= timescale;
2090                 st->codec->time_base.num *= framedur;
2091 FF_ENABLE_DEPRECATION_WARNINGS
2092 #endif
2093             }
2094             if (size > 30) {
2095                 uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2096                 uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2097                 if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2098                     uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2099                     if (str_size > 0 && size >= (int)str_size + 26) {
2100                         char *reel_name = av_malloc(str_size + 1);
2101                         if (!reel_name)
2102                             return AVERROR(ENOMEM);
2103                         memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2104                         reel_name[str_size] = 0; /* Add null terminator */
2105                         /* don't add reel_name if emtpy string */
2106                         if (*reel_name == 0) {
2107                             av_free(reel_name);
2108                         } else {
2109                             av_dict_set(&st->metadata, "reel_name", reel_name,  AV_DICT_DONT_STRDUP_VAL);
2110                         }
2111                     }
2112                 }
2113             }
2114         }
2115     } else {
2116         /* other codec type, just skip (rtp, mp4s ...) */
2117         avio_skip(pb, size);
2118     }
2119     return 0;
2120 }
2121
2122 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
2123                                    AVStream *st, MOVStreamContext *sc)
2124 {
2125     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2126         !st->codecpar->sample_rate && sc->time_scale > 1)
2127         st->codecpar->sample_rate = sc->time_scale;
2128
2129     /* special codec parameters handling */
2130     switch (st->codecpar->codec_id) {
2131 #if CONFIG_DV_DEMUXER
2132     case AV_CODEC_ID_DVAUDIO:
2133         c->dv_fctx = avformat_alloc_context();
2134         if (!c->dv_fctx) {
2135             av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2136             return AVERROR(ENOMEM);
2137         }
2138         c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2139         if (!c->dv_demux) {
2140             av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2141             return AVERROR(ENOMEM);
2142         }
2143         sc->dv_audio_container = 1;
2144         st->codecpar->codec_id    = AV_CODEC_ID_PCM_S16LE;
2145         break;
2146 #endif
2147     /* no ifdef since parameters are always those */
2148     case AV_CODEC_ID_QCELP:
2149         st->codecpar->channels = 1;
2150         // force sample rate for qcelp when not stored in mov
2151         if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2152             st->codecpar->sample_rate = 8000;
2153         // FIXME: Why is the following needed for some files?
2154         sc->samples_per_frame = 160;
2155         if (!sc->bytes_per_frame)
2156             sc->bytes_per_frame = 35;
2157         break;
2158     case AV_CODEC_ID_AMR_NB:
2159         st->codecpar->channels    = 1;
2160         /* force sample rate for amr, stsd in 3gp does not store sample rate */
2161         st->codecpar->sample_rate = 8000;
2162         break;
2163     case AV_CODEC_ID_AMR_WB:
2164         st->codecpar->channels    = 1;
2165         st->codecpar->sample_rate = 16000;
2166         break;
2167     case AV_CODEC_ID_MP2:
2168     case AV_CODEC_ID_MP3:
2169         /* force type after stsd for m1a hdlr */
2170         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
2171         break;
2172     case AV_CODEC_ID_GSM:
2173     case AV_CODEC_ID_ADPCM_MS:
2174     case AV_CODEC_ID_ADPCM_IMA_WAV:
2175     case AV_CODEC_ID_ILBC:
2176     case AV_CODEC_ID_MACE3:
2177     case AV_CODEC_ID_MACE6:
2178     case AV_CODEC_ID_QDM2:
2179         st->codecpar->block_align = sc->bytes_per_frame;
2180         break;
2181     case AV_CODEC_ID_ALAC:
2182         if (st->codecpar->extradata_size == 36) {
2183             st->codecpar->channels    = AV_RB8 (st->codecpar->extradata + 21);
2184             st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2185         }
2186         break;
2187     case AV_CODEC_ID_AC3:
2188     case AV_CODEC_ID_EAC3:
2189     case AV_CODEC_ID_MPEG1VIDEO:
2190     case AV_CODEC_ID_VC1:
2191         st->need_parsing = AVSTREAM_PARSE_FULL;
2192         break;
2193     default:
2194         break;
2195     }
2196     return 0;
2197 }
2198
2199 static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
2200                                   int codec_tag, int format,
2201                                   int64_t size)
2202 {
2203     int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2204
2205     if (codec_tag &&
2206          (codec_tag != format &&
2207           (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2208                                  : codec_tag != MKTAG('j','p','e','g')))) {
2209         /* Multiple fourcc, we skip JPEG. This is not correct, we should
2210          * export it as a separate AVStream but this needs a few changes
2211          * in the MOV demuxer, patch welcome. */
2212
2213         av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2214         avio_skip(pb, size);
2215         return 1;
2216     }
2217     if ( codec_tag == AV_RL32("avc1") ||
2218          codec_tag == AV_RL32("hvc1") ||
2219          codec_tag == AV_RL32("hev1")
2220     )
2221         av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
2222
2223     return 0;
2224 }
2225
2226 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
2227 {
2228     AVStream *st;
2229     MOVStreamContext *sc;
2230     int pseudo_stream_id;
2231
2232     if (c->fc->nb_streams < 1)
2233         return 0;
2234     st = c->fc->streams[c->fc->nb_streams-1];
2235     sc = st->priv_data;
2236
2237     for (pseudo_stream_id = 0;
2238          pseudo_stream_id < entries && !pb->eof_reached;
2239          pseudo_stream_id++) {
2240         //Parsing Sample description table
2241         enum AVCodecID id;
2242         int ret, dref_id = 1;
2243         MOVAtom a = { AV_RL32("stsd") };
2244         int64_t start_pos = avio_tell(pb);
2245         int64_t size    = avio_rb32(pb); /* size */
2246         uint32_t format = avio_rl32(pb); /* data format */
2247
2248         if (size >= 16) {
2249             avio_rb32(pb); /* reserved */
2250             avio_rb16(pb); /* reserved */
2251             dref_id = avio_rb16(pb);
2252         } else if (size <= 7) {
2253             av_log(c->fc, AV_LOG_ERROR,
2254                    "invalid size %"PRId64" in stsd\n", size);
2255             return AVERROR_INVALIDDATA;
2256         }
2257
2258         if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
2259                                    size - (avio_tell(pb) - start_pos)))
2260             continue;
2261
2262         sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2263         sc->dref_id= dref_id;
2264         sc->format = format;
2265
2266         id = mov_codec_id(st, format);
2267
2268         av_log(c->fc, AV_LOG_TRACE,
2269                "size=%"PRId64" 4CC= %c%c%c%c/0x%08x codec_type=%d\n", size,
2270                 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
2271                 (format >> 24) & 0xff, format, st->codecpar->codec_type);
2272
2273         if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
2274             st->codecpar->codec_id = id;
2275             mov_parse_stsd_video(c, pb, st, sc);
2276         } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
2277             st->codecpar->codec_id = id;
2278             mov_parse_stsd_audio(c, pb, st, sc);
2279         } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
2280             st->codecpar->codec_id = id;
2281             mov_parse_stsd_subtitle(c, pb, st, sc,
2282                                     size - (avio_tell(pb) - start_pos));
2283         } else {
2284             ret = mov_parse_stsd_data(c, pb, st, sc,
2285                                       size - (avio_tell(pb) - start_pos));
2286             if (ret < 0)
2287                 return ret;
2288         }
2289         /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2290         a.size = size - (avio_tell(pb) - start_pos);
2291         if (a.size > 8) {
2292             if ((ret = mov_read_default(c, pb, a)) < 0)
2293                 return ret;
2294         } else if (a.size > 0)
2295             avio_skip(pb, a.size);
2296     }
2297
2298     if (pb->eof_reached)
2299         return AVERROR_EOF;
2300
2301     return mov_finalize_stsd_codec(c, pb, st, sc);
2302 }
2303
2304 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2305 {
2306     int entries;
2307
2308     avio_r8(pb); /* version */
2309     avio_rb24(pb); /* flags */
2310     entries = avio_rb32(pb);
2311
2312     return ff_mov_read_stsd_entries(c, pb, entries);
2313 }
2314
2315 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2316 {
2317     AVStream *st;
2318     MOVStreamContext *sc;
2319     unsigned int i, entries;
2320
2321     if (c->fc->nb_streams < 1)
2322         return 0;
2323     st = c->fc->streams[c->fc->nb_streams-1];
2324     sc = st->priv_data;
2325
2326     avio_r8(pb); /* version */
2327     avio_rb24(pb); /* flags */
2328
2329     entries = avio_rb32(pb);
2330
2331     av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
2332
2333     if (!entries)
2334         return 0;
2335     if (sc->stsc_data)
2336         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
2337     av_free(sc->stsc_data);
2338     sc->stsc_count = 0;
2339     sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2340     if (!sc->stsc_data)
2341         return AVERROR(ENOMEM);
2342
2343     for (i = 0; i < entries && !pb->eof_reached; i++) {
2344         sc->stsc_data[i].first = avio_rb32(pb);
2345         sc->stsc_data[i].count = avio_rb32(pb);
2346         sc->stsc_data[i].id = avio_rb32(pb);
2347     }
2348
2349     sc->stsc_count = i;
2350
2351     if (pb->eof_reached)
2352         return AVERROR_EOF;
2353
2354     return 0;
2355 }
2356
2357 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2358 {
2359     AVStream *st;
2360     MOVStreamContext *sc;
2361     unsigned i, entries;
2362
2363     if (c->fc->nb_streams < 1)
2364         return 0;
2365     st = c->fc->streams[c->fc->nb_streams-1];
2366     sc = st->priv_data;
2367
2368     avio_rb32(pb); // version + flags
2369
2370     entries = avio_rb32(pb);
2371     if (sc->stps_data)
2372         av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2373     av_free(sc->stps_data);
2374     sc->stps_count = 0;
2375     sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2376     if (!sc->stps_data)
2377         return AVERROR(ENOMEM);
2378
2379     for (i = 0; i < entries && !pb->eof_reached; i++) {
2380         sc->stps_data[i] = avio_rb32(pb);
2381         //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
2382     }
2383
2384     sc->stps_count = i;
2385
2386     if (pb->eof_reached)
2387         return AVERROR_EOF;
2388
2389     return 0;
2390 }
2391
2392 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2393 {
2394     AVStream *st;
2395     MOVStreamContext *sc;
2396     unsigned int i, entries;
2397
2398     if (c->fc->nb_streams < 1)
2399         return 0;
2400     st = c->fc->streams[c->fc->nb_streams-1];
2401     sc = st->priv_data;
2402
2403     avio_r8(pb); /* version */
2404     avio_rb24(pb); /* flags */
2405
2406     entries = avio_rb32(pb);
2407
2408     av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
2409
2410     if (!entries)
2411     {
2412         sc->keyframe_absent = 1;
2413         if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2414             st->need_parsing = AVSTREAM_PARSE_HEADERS;
2415         return 0;
2416     }
2417     if (sc->keyframes)
2418         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2419     if (entries >= UINT_MAX / sizeof(int))
2420         return AVERROR_INVALIDDATA;
2421     av_freep(&sc->keyframes);
2422     sc->keyframe_count = 0;
2423     sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2424     if (!sc->keyframes)
2425         return AVERROR(ENOMEM);
2426
2427     for (i = 0; i < entries && !pb->eof_reached; i++) {
2428         sc->keyframes[i] = avio_rb32(pb);
2429         //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
2430     }
2431
2432     sc->keyframe_count = i;
2433
2434     if (pb->eof_reached)
2435         return AVERROR_EOF;
2436
2437     return 0;
2438 }
2439
2440 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2441 {
2442     AVStream *st;
2443     MOVStreamContext *sc;
2444     unsigned int i, entries, sample_size, field_size, num_bytes;
2445     GetBitContext gb;
2446     unsigned char* buf;
2447     int ret;
2448
2449     if (c->fc->nb_streams < 1)
2450         return 0;
2451     st = c->fc->streams[c->fc->nb_streams-1];
2452     sc = st->priv_data;
2453
2454     avio_r8(pb); /* version */
2455     avio_rb24(pb); /* flags */
2456
2457     if (atom.type == MKTAG('s','t','s','z')) {
2458         sample_size = avio_rb32(pb);
2459         if (!sc->sample_size) /* do not overwrite value computed in stsd */
2460             sc->sample_size = sample_size;
2461         sc->stsz_sample_size = sample_size;
2462         field_size = 32;
2463     } else {
2464         sample_size = 0;
2465         avio_rb24(pb); /* reserved */
2466         field_size = avio_r8(pb);
2467     }
2468     entries = avio_rb32(pb);
2469
2470     av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2471
2472     sc->sample_count = entries;
2473     if (sample_size)
2474         return 0;
2475
2476     if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2477         av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2478         return AVERROR_INVALIDDATA;
2479     }
2480
2481     if (!entries)
2482         return 0;
2483     if (entries >= (UINT_MAX - 4) / field_size)
2484         return AVERROR_INVALIDDATA;
2485     if (sc->sample_sizes)
2486         av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2487     av_free(sc->sample_sizes);
2488     sc->sample_count = 0;
2489     sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2490     if (!sc->sample_sizes)
2491         return AVERROR(ENOMEM);
2492
2493     num_bytes = (entries*field_size+4)>>3;
2494
2495     buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2496     if (!buf) {
2497         av_freep(&sc->sample_sizes);
2498         return AVERROR(ENOMEM);
2499     }
2500
2501     ret = ffio_read_size(pb, buf, num_bytes);
2502     if (ret < 0) {
2503         av_freep(&sc->sample_sizes);
2504         av_free(buf);
2505         return ret;
2506     }
2507
2508     init_get_bits(&gb, buf, 8*num_bytes);
2509
2510     for (i = 0; i < entries && !pb->eof_reached; i++) {
2511         sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2512         sc->data_size += sc->sample_sizes[i];
2513     }
2514
2515     sc->sample_count = i;
2516
2517     av_free(buf);
2518
2519     if (pb->eof_reached)
2520         return AVERROR_EOF;
2521
2522     return 0;
2523 }
2524
2525 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2526 {
2527     AVStream *st;
2528     MOVStreamContext *sc;
2529     unsigned int i, entries;
2530     int64_t duration=0;
2531     int64_t total_sample_count=0;
2532
2533     if (c->fc->nb_streams < 1)
2534         return 0;
2535     st = c->fc->streams[c->fc->nb_streams-1];
2536     sc = st->priv_data;
2537
2538     avio_r8(pb); /* version */
2539     avio_rb24(pb); /* flags */
2540     entries = avio_rb32(pb);
2541
2542     av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
2543             c->fc->nb_streams-1, entries);
2544
2545     if (sc->stts_data)
2546         av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2547     av_free(sc->stts_data);
2548     sc->stts_count = 0;
2549     sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data));
2550     if (!sc->stts_data)
2551         return AVERROR(ENOMEM);
2552
2553     for (i = 0; i < entries && !pb->eof_reached; i++) {
2554         int sample_duration;
2555         int sample_count;
2556
2557         sample_count=avio_rb32(pb);
2558         sample_duration = avio_rb32(pb);
2559
2560         if (sample_count < 0) {
2561             av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2562             return AVERROR_INVALIDDATA;
2563         }
2564         sc->stts_data[i].count= sample_count;
2565         sc->stts_data[i].duration= sample_duration;
2566
2567         av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2568                 sample_count, sample_duration);
2569
2570         if (   i+1 == entries
2571             && i
2572             && sample_count == 1
2573             && total_sample_count > 100
2574             && sample_duration/10 > duration / total_sample_count)
2575             sample_duration = duration / total_sample_count;
2576         duration+=(int64_t)sample_duration*sample_count;
2577         total_sample_count+=sample_count;
2578     }
2579
2580     sc->stts_count = i;
2581
2582     sc->duration_for_fps  += duration;
2583     sc->nb_frames_for_fps += total_sample_count;
2584
2585     if (pb->eof_reached)
2586         return AVERROR_EOF;
2587
2588     st->nb_frames= total_sample_count;
2589     if (duration)
2590         st->duration= duration;
2591     sc->track_end = duration;
2592     return 0;
2593 }
2594
2595 static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2596 {
2597     if (duration < 0) {
2598         if (duration == INT_MIN) {
2599             av_log(NULL, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
2600             duration++;
2601         }
2602         sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2603     }
2604 }
2605
2606 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2607 {
2608     AVStream *st;
2609     MOVStreamContext *sc;
2610     unsigned int i, entries, ctts_count = 0;
2611
2612     if (c->fc->nb_streams < 1)
2613         return 0;
2614     st = c->fc->streams[c->fc->nb_streams-1];
2615     sc = st->priv_data;
2616
2617     avio_r8(pb); /* version */
2618     avio_rb24(pb); /* flags */
2619     entries = avio_rb32(pb);
2620
2621     av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2622
2623     if (!entries)
2624         return 0;
2625     if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2626         return AVERROR_INVALIDDATA;
2627     av_freep(&sc->ctts_data);
2628     sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
2629     if (!sc->ctts_data)
2630         return AVERROR(ENOMEM);
2631
2632     for (i = 0; i < entries && !pb->eof_reached; i++) {
2633         int count    =avio_rb32(pb);
2634         int duration =avio_rb32(pb);
2635
2636         if (count <= 0) {
2637             av_log(c->fc, AV_LOG_TRACE,
2638                    "ignoring CTTS entry with count=%d duration=%d\n",
2639                    count, duration);
2640             continue;
2641         }
2642
2643         sc->ctts_data[ctts_count].count    = count;
2644         sc->ctts_data[ctts_count].duration = duration;
2645         ctts_count++;
2646
2647         av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
2648                 count, duration);
2649
2650         if (FFNABS(duration) < -(1<<28) && i+2<entries) {
2651             av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2652             av_freep(&sc->ctts_data);
2653             sc->ctts_count = 0;
2654             return 0;
2655         }
2656
2657         if (i+2<entries)
2658             mov_update_dts_shift(sc, duration);
2659     }
2660
2661     sc->ctts_count = ctts_count;
2662
2663     if (pb->eof_reached)
2664         return AVERROR_EOF;
2665
2666     av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
2667
2668     return 0;
2669 }
2670
2671 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2672 {
2673     AVStream *st;
2674     MOVStreamContext *sc;
2675     unsigned int i, entries;
2676     uint8_t version;
2677     uint32_t grouping_type;
2678
2679     if (c->fc->nb_streams < 1)
2680         return 0;
2681     st = c->fc->streams[c->fc->nb_streams-1];
2682     sc = st->priv_data;
2683
2684     version = avio_r8(pb); /* version */
2685     avio_rb24(pb); /* flags */
2686     grouping_type = avio_rl32(pb);
2687     if (grouping_type != MKTAG( 'r','a','p',' '))
2688         return 0; /* only support 'rap ' grouping */
2689     if (version == 1)
2690         avio_rb32(pb); /* grouping_type_parameter */
2691
2692     entries = avio_rb32(pb);
2693     if (!entries)
2694         return 0;
2695     if (sc->rap_group)
2696         av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
2697     av_free(sc->rap_group);
2698     sc->rap_group_count = 0;
2699     sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
2700     if (!sc->rap_group)
2701         return AVERROR(ENOMEM);
2702
2703     for (i = 0; i < entries && !pb->eof_reached; i++) {
2704         sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2705         sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2706     }
2707
2708     sc->rap_group_count = i;
2709
2710     return pb->eof_reached ? AVERROR_EOF : 0;
2711 }
2712
2713 static void mov_build_index(MOVContext *mov, AVStream *st)
2714 {
2715     MOVStreamContext *sc = st->priv_data;
2716     int64_t current_offset;
2717     int64_t current_dts = 0;
2718     unsigned int stts_index = 0;
2719     unsigned int stsc_index = 0;
2720     unsigned int stss_index = 0;
2721     unsigned int stps_index = 0;
2722     unsigned int i, j;
2723     uint64_t stream_size = 0;
2724
2725     if (sc->elst_count) {
2726         int i, edit_start_index = 0, unsupported = 0;
2727         int64_t empty_duration = 0; // empty duration of the first edit list entry
2728         int64_t start_time = 0; // start time of the media
2729
2730         for (i = 0; i < sc->elst_count; i++) {
2731             const MOVElst *e = &sc->elst_data[i];
2732             if (i == 0 && e->time == -1) {
2733                 /* if empty, the first entry is the start time of the stream
2734                  * relative to the presentation itself */
2735                 empty_duration = e->duration;
2736                 edit_start_index = 1;
2737             } else if (i == edit_start_index && e->time >= 0) {
2738                 start_time = e->time;
2739             } else
2740                 unsupported = 1;
2741         }
2742         if (unsupported)
2743             av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
2744                    "a/v desync might occur, patch welcome\n");
2745
2746         /* adjust first dts according to edit list */
2747         if ((empty_duration || start_time) && mov->time_scale > 0) {
2748             if (empty_duration)
2749                 empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
2750             sc->time_offset = start_time - empty_duration;
2751             current_dts = -sc->time_offset;
2752             if (sc->ctts_count>0 && sc->stts_count>0 &&
2753                 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2754                 /* more than 16 frames delay, dts are likely wrong
2755                    this happens with files created by iMovie */
2756                 sc->wrong_dts = 1;
2757                 st->codecpar->video_delay = 1;
2758             }
2759         }
2760
2761         if (!unsupported && st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
2762             sc->start_pad = start_time;
2763     }
2764
2765     /* only use old uncompressed audio chunk demuxing when stts specifies it */
2766     if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2767           sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2768         unsigned int current_sample = 0;
2769         unsigned int stts_sample = 0;
2770         unsigned int sample_size;
2771         unsigned int distance = 0;
2772         unsigned int rap_group_index = 0;
2773         unsigned int rap_group_sample = 0;
2774         int64_t last_dts = 0;
2775         int64_t dts_correction = 0;
2776         int rap_group_present = sc->rap_group_count && sc->rap_group;
2777         int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2778
2779         current_dts -= sc->dts_shift;
2780         last_dts     = current_dts;
2781
2782         if (!sc->sample_count || st->nb_index_entries)
2783             return;
2784         if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2785             return;
2786         if (av_reallocp_array(&st->index_entries,
2787                               st->nb_index_entries + sc->sample_count,
2788                               sizeof(*st->index_entries)) < 0) {
2789             st->nb_index_entries = 0;
2790             return;
2791         }
2792         st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2793
2794         for (i = 0; i < sc->chunk_count; i++) {
2795             int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2796             current_offset = sc->chunk_offsets[i];
2797             while (stsc_index + 1 < sc->stsc_count &&
2798                 i + 1 == sc->stsc_data[stsc_index + 1].first)
2799                 stsc_index++;
2800
2801             if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2802                 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2803                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2804                 sc->stsz_sample_size = sc->sample_size;
2805             }
2806             if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2807                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2808                 sc->stsz_sample_size = sc->sample_size;
2809             }
2810
2811             for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2812                 int keyframe = 0;
2813                 if (current_sample >= sc->sample_count) {
2814                     av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2815                     return;
2816                 }
2817
2818                 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2819                     keyframe = 1;
2820                     if (stss_index + 1 < sc->keyframe_count)
2821                         stss_index++;
2822                 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2823                     keyframe = 1;
2824                     if (stps_index + 1 < sc->stps_count)
2825                         stps_index++;
2826                 }
2827                 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2828                     if (sc->rap_group[rap_group_index].index > 0)
2829                         keyframe = 1;
2830                     if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2831                         rap_group_sample = 0;
2832                         rap_group_index++;
2833                     }
2834                 }
2835                 if (sc->keyframe_absent
2836                     && !sc->stps_count
2837                     && !rap_group_present
2838                     && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2839                      keyframe = 1;
2840                 if (keyframe)
2841                     distance = 0;
2842                 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2843                 if (sc->pseudo_stream_id == -1 ||
2844                    sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2845                     AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2846                     e->pos = current_offset;
2847                     e->timestamp = current_dts;
2848                     e->size = sample_size;
2849                     e->min_distance = distance;
2850                     e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2851                     av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2852                             "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2853                             current_offset, current_dts, sample_size, distance, keyframe);
2854                     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2855                         ff_rfps_add_frame(mov->fc, st, current_dts);
2856                 }
2857
2858                 current_offset += sample_size;
2859                 stream_size += sample_size;
2860
2861                 /* A negative sample duration is invalid based on the spec,
2862                  * but some samples need it to correct the DTS. */
2863                 if (sc->stts_data[stts_index].duration < 0) {
2864                     av_log(mov->fc, AV_LOG_WARNING,
2865                            "Invalid SampleDelta %d in STTS, at %d st:%d\n",
2866                            sc->stts_data[stts_index].duration, stts_index,
2867                            st->index);
2868                     dts_correction += sc->stts_data[stts_index].duration - 1;
2869                     sc->stts_data[stts_index].duration = 1;
2870                 }
2871                 current_dts += sc->stts_data[stts_index].duration;
2872                 if (!dts_correction || current_dts + dts_correction > last_dts) {
2873                     current_dts += dts_correction;
2874                     dts_correction = 0;
2875                 } else {
2876                     /* Avoid creating non-monotonous DTS */
2877                     dts_correction += current_dts - last_dts - 1;
2878                     current_dts = last_dts + 1;
2879                 }
2880                 last_dts = current_dts;
2881                 distance++;
2882                 stts_sample++;
2883                 current_sample++;
2884                 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2885                     stts_sample = 0;
2886                     stts_index++;
2887                 }
2888             }
2889         }
2890         if (st->duration > 0)
2891             st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
2892     } else {
2893         unsigned chunk_samples, total = 0;
2894
2895         // compute total chunk count
2896         for (i = 0; i < sc->stsc_count; i++) {
2897             unsigned count, chunk_count;
2898
2899             chunk_samples = sc->stsc_data[i].count;
2900             if (i != sc->stsc_count - 1 &&
2901                 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2902                 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2903                 return;
2904             }
2905
2906             if (sc->samples_per_frame >= 160) { // gsm
2907                 count = chunk_samples / sc->samples_per_frame;
2908             } else if (sc->samples_per_frame > 1) {
2909                 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2910                 count = (chunk_samples+samples-1) / samples;
2911             } else {
2912                 count = (chunk_samples+1023) / 1024;
2913             }
2914
2915             if (i < sc->stsc_count - 1)
2916                 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2917             else
2918                 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2919             total += chunk_count * count;
2920         }
2921
2922         av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
2923         if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2924             return;
2925         if (av_reallocp_array(&st->index_entries,
2926                               st->nb_index_entries + total,
2927                               sizeof(*st->index_entries)) < 0) {
2928             st->nb_index_entries = 0;
2929             return;
2930         }
2931         st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2932
2933         // populate index
2934         for (i = 0; i < sc->chunk_count; i++) {
2935             current_offset = sc->chunk_offsets[i];
2936             if (stsc_index + 1 < sc->stsc_count &&
2937                 i + 1 == sc->stsc_data[stsc_index + 1].first)
2938                 stsc_index++;
2939             chunk_samples = sc->stsc_data[stsc_index].count;
2940
2941             while (chunk_samples > 0) {
2942                 AVIndexEntry *e;
2943                 unsigned size, samples;
2944
2945                 if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
2946                     avpriv_request_sample(mov->fc,
2947                            "Zero bytes per frame, but %d samples per frame",
2948                            sc->samples_per_frame);
2949                     return;
2950                 }
2951
2952                 if (sc->samples_per_frame >= 160) { // gsm
2953                     samples = sc->samples_per_frame;
2954                     size = sc->bytes_per_frame;
2955                 } else {
2956                     if (sc->samples_per_frame > 1) {
2957                         samples = FFMIN((1024 / sc->samples_per_frame)*
2958                                         sc->samples_per_frame, chunk_samples);
2959                         size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2960                     } else {
2961                         samples = FFMIN(1024, chunk_samples);
2962                         size = samples * sc->sample_size;
2963                     }
2964                 }
2965
2966                 if (st->nb_index_entries >= total) {
2967                     av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2968                     return;
2969                 }
2970                 e = &st->index_entries[st->nb_index_entries++];
2971                 e->pos = current_offset;
2972                 e->timestamp = current_dts;
2973                 e->size = size;
2974                 e->min_distance = 0;
2975                 e->flags = AVINDEX_KEYFRAME;
2976                 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2977                         "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2978                         size, samples);
2979
2980                 current_offset += size;
2981                 current_dts += samples;
2982                 chunk_samples -= samples;
2983             }
2984         }
2985     }
2986 }
2987
2988 static int test_same_origin(const char *src, const char *ref) {
2989     char src_proto[64];
2990     char ref_proto[64];
2991     char src_auth[256];
2992     char ref_auth[256];
2993     char src_host[256];
2994     char ref_host[256];
2995     int src_port=-1;
2996     int ref_port=-1;
2997
2998     av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
2999     av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
3000
3001     if (strlen(src) == 0) {
3002         return -1;
3003     } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
3004         strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
3005         strlen(src_host) + 1 >= sizeof(src_host) ||
3006         strlen(ref_host) + 1 >= sizeof(ref_host)) {
3007         return 0;
3008     } else if (strcmp(src_proto, ref_proto) ||
3009                strcmp(src_auth, ref_auth) ||
3010                strcmp(src_host, ref_host) ||
3011                src_port != ref_port) {
3012         return 0;
3013     } else
3014         return 1;
3015 }
3016
3017 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
3018 {
3019     /* try relative path, we do not try the absolute because it can leak information about our
3020        system to an attacker */
3021     if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
3022         char filename[1025];
3023         const char *src_path;
3024         int i, l;
3025
3026         /* find a source dir */
3027         src_path = strrchr(src, '/');
3028         if (src_path)
3029             src_path++;
3030         else
3031             src_path = src;
3032
3033         /* find a next level down to target */
3034         for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
3035             if (ref->path[l] == '/') {
3036                 if (i == ref->nlvl_to - 1)
3037                     break;
3038                 else
3039                     i++;
3040             }
3041
3042         /* compose filename if next level down to target was found */
3043         if (i == ref->nlvl_to - 1 && src_path - src  < sizeof(filename)) {
3044             memcpy(filename, src, src_path - src);
3045             filename[src_path - src] = 0;
3046
3047             for (i = 1; i < ref->nlvl_from; i++)
3048                 av_strlcat(filename, "../", sizeof(filename));
3049
3050             av_strlcat(filename, ref->path + l + 1, sizeof(filename));
3051             if (!c->use_absolute_path) {
3052                 int same_origin = test_same_origin(src, filename);
3053
3054                 if (!same_origin) {
3055                     av_log(c->fc, AV_LOG_ERROR,
3056                         "Reference with mismatching origin, %s not tried for security reasons, "
3057                         "set demuxer option use_absolute_path to allow it anyway\n",
3058                         ref->path);
3059                     return AVERROR(ENOENT);
3060                 }
3061
3062                 if(strstr(ref->path + l + 1, "..") ||
3063                    strstr(ref->path + l + 1, ":") ||
3064                    (ref->nlvl_from > 1 && same_origin < 0) ||
3065                    (filename[0] == '/' && src_path == src))
3066                     return AVERROR(ENOENT);
3067             }
3068
3069             if (strlen(filename) + 1 == sizeof(filename))
3070                 return AVERROR(ENOENT);
3071             if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
3072                 return 0;
3073         }
3074     } else if (c->use_absolute_path) {
3075         av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
3076                "this is a possible security issue\n");
3077         if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
3078             return 0;
3079     } else {
3080         av_log(c->fc, AV_LOG_ERROR,
3081                "Absolute path %s not tried for security reasons, "
3082                "set demuxer option use_absolute_path to allow absolute paths\n",
3083                ref->path);
3084     }
3085
3086     return AVERROR(ENOENT);
3087 }
3088
3089 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
3090 {
3091     if (sc->time_scale <= 0) {
3092         av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
3093         sc->time_scale = c->time_scale;
3094         if (sc->time_scale <= 0)
3095             sc->time_scale = 1;
3096     }
3097 }
3098
3099 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3100 {
3101     AVStream *st;
3102     MOVStreamContext *sc;
3103     int ret;
3104
3105     st = avformat_new_stream(c->fc, NULL);
3106     if (!st) return AVERROR(ENOMEM);
3107     st->id = c->fc->nb_streams;
3108     sc = av_mallocz(sizeof(MOVStreamContext));
3109     if (!sc) return AVERROR(ENOMEM);
3110
3111     st->priv_data = sc;
3112     st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
3113     sc->ffindex = st->index;
3114     c->trak_index = st->index;
3115
3116     if ((ret = mov_read_default(c, pb, atom)) < 0)
3117         return ret;
3118
3119     c->trak_index = -1;
3120
3121     /* sanity checks */
3122     if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
3123                             (!sc->sample_size && !sc->sample_count))) {
3124         av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
3125                st->index);
3126         return 0;
3127     }
3128
3129     fix_timescale(c, sc);
3130
3131     avpriv_set_pts_info(st, 64, 1, sc->time_scale);
3132
3133     mov_build_index(c, st);
3134
3135     if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
3136         MOVDref *dref = &sc->drefs[sc->dref_id - 1];
3137         if (c->enable_drefs) {
3138             if (mov_open_dref(c, &sc->pb, c->fc->filename, dref) < 0)
3139                 av_log(c->fc, AV_LOG_ERROR,
3140                        "stream %d, error opening alias: path='%s', dir='%s', "
3141                        "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
3142                        st->index, dref->path, dref->dir, dref->filename,
3143                        dref->volume, dref->nlvl_from, dref->nlvl_to);
3144         } else {
3145             av_log(c->fc, AV_LOG_WARNING,
3146                    "Skipped opening external track: "
3147                    "stream %d, alias: path='%s', dir='%s', "
3148                    "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
3149                    "Set enable_drefs to allow this.\n",
3150                    st->index, dref->path, dref->dir, dref->filename,
3151                    dref->volume, dref->nlvl_from, dref->nlvl_to);
3152         }
3153     } else {
3154         sc->pb = c->fc->pb;
3155         sc->pb_is_copied = 1;
3156     }
3157
3158     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3159         if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
3160             sc->height && sc->width &&
3161             (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
3162             st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
3163                                              ((double)st->codecpar->width * sc->height), INT_MAX);
3164         }
3165
3166 #if FF_API_R_FRAME_RATE
3167         if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
3168             av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
3169                       sc->time_scale, sc->stts_data[0].duration, INT_MAX);
3170 #endif
3171     }
3172
3173     // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
3174     if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
3175         TAG_IS_AVCI(st->codecpar->codec_tag)) {
3176         ret = ff_generate_avci_extradata(st);
3177         if (ret < 0)
3178             return ret;
3179     }
3180
3181     switch (st->codecpar->codec_id) {
3182 #if CONFIG_H261_DECODER
3183     case AV_CODEC_ID_H261:
3184 #endif
3185 #if CONFIG_H263_DECODER
3186     case AV_CODEC_ID_H263:
3187 #endif
3188 #if CONFIG_MPEG4_DECODER
3189     case AV_CODEC_ID_MPEG4:
3190 #endif
3191         st->codecpar->width = 0; /* let decoder init width/height */
3192         st->codecpar->height= 0;
3193         break;
3194     }
3195
3196     // If the duration of the mp3 packets is not constant, then they could need a parser
3197     if (st->codecpar->codec_id == AV_CODEC_ID_MP3
3198         && sc->stts_count > 3
3199         && sc->stts_count*10 > st->nb_frames
3200         && sc->time_scale == st->codecpar->sample_rate) {
3201             st->need_parsing = AVSTREAM_PARSE_FULL;
3202     }
3203     /* Do not need those anymore. */
3204     av_freep(&sc->chunk_offsets);
3205     av_freep(&sc->stsc_data);
3206     av_freep(&sc->sample_sizes);
3207     av_freep(&sc->keyframes);
3208     av_freep(&sc->stts_data);
3209     av_freep(&sc->stps_data);
3210     av_freep(&sc->elst_data);
3211     av_freep(&sc->rap_group);
3212
3213     return 0;
3214 }
3215
3216 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3217 {
3218     int ret;
3219     c->itunes_metadata = 1;
3220     ret = mov_read_default(c, pb, atom);
3221     c->itunes_metadata = 0;
3222     return ret;
3223 }
3224
3225 static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3226 {
3227     uint32_t count;
3228     uint32_t i;
3229
3230     if (atom.size < 8)
3231         return 0;
3232
3233     avio_skip(pb, 4);
3234     count = avio_rb32(pb);
3235     if (count > UINT_MAX / sizeof(*c->meta_keys)) {
3236         av_log(c->fc, AV_LOG_ERROR,
3237                "The 'keys' atom with the invalid key count: %d\n", count);
3238         return AVERROR_INVALIDDATA;
3239     }
3240
3241     c->meta_keys_count = count + 1;
3242     c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
3243     if (!c->meta_keys)
3244         return AVERROR(ENOMEM);
3245
3246     for (i = 1; i <= count; ++i) {
3247         uint32_t key_size = avio_rb32(pb);
3248         uint32_t type = avio_rl32(pb);
3249         if (key_size < 8) {
3250             av_log(c->fc, AV_LOG_ERROR,
3251                    "The key# %d in meta has invalid size: %d\n", i, key_size);
3252             return AVERROR_INVALIDDATA;
3253         }
3254         key_size -= 8;
3255         if (type != MKTAG('m','d','t','a')) {
3256             avio_skip(pb, key_size);
3257         }
3258         c->meta_keys[i] = av_mallocz(key_size + 1);
3259         if (!c->meta_keys[i])
3260             return AVERROR(ENOMEM);
3261         avio_read(pb, c->meta_keys[i], key_size);
3262     }
3263
3264     return 0;
3265 }
3266
3267 static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3268 {
3269     int64_t end = avio_tell(pb) + atom.size;
3270     uint8_t *key = NULL, *val = NULL, *mean = NULL;
3271     int i;
3272     int ret = 0;
3273     AVStream *st;
3274     MOVStreamContext *sc;
3275
3276     if (c->fc->nb_streams < 1)
3277         return 0;
3278     st = c->fc->streams[c->fc->nb_streams-1];
3279     sc = st->priv_data;
3280
3281     for (i = 0; i < 3; i++) {
3282         uint8_t **p;
3283         uint32_t len, tag;
3284
3285         if (end - avio_tell(pb) <= 12)
3286             break;
3287
3288         len = avio_rb32(pb);
3289         tag = avio_rl32(pb);
3290         avio_skip(pb, 4); // flags
3291
3292         if (len < 12 || len - 12 > end - avio_tell(pb))
3293             break;
3294         len -= 12;
3295
3296         if (tag == MKTAG('m', 'e', 'a', 'n'))
3297             p = &mean;
3298         else if (tag == MKTAG('n', 'a', 'm', 'e'))
3299             p = &key;
3300         else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
3301             avio_skip(pb, 4);
3302             len -= 4;
3303             p = &val;
3304         } else
3305             break;
3306
3307         *p = av_malloc(len + 1);
3308         if (!*p)
3309             break;
3310         ret = ffio_read_size(pb, *p, len);
3311         if (ret < 0) {
3312             av_freep(p);
3313             break;
3314         }
3315         (*p)[len] = 0;
3316     }
3317
3318     if (mean && key && val) {
3319         if (strcmp(key, "iTunSMPB") == 0) {
3320             int priming, remainder, samples;
3321             if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
3322                 if(priming>0 && priming<16384)
3323                     sc->start_pad = priming;
3324             }
3325         }
3326         if (strcmp(key, "cdec") != 0) {
3327             av_dict_set(&c->fc->metadata, key, val,
3328                         AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
3329             key = val = NULL;
3330         }
3331     } else {
3332         av_log(c->fc, AV_LOG_VERBOSE,
3333                "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
3334     }
3335
3336     avio_seek(pb, end, SEEK_SET);
3337     av_freep(&key);
3338     av_freep(&val);
3339     av_freep(&mean);
3340     return ret;
3341 }
3342
3343 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3344 {
3345     while (atom.size > 8) {
3346         uint32_t tag = avio_rl32(pb);
3347         atom.size -= 4;
3348         if (tag == MKTAG('h','d','l','r')) {
3349             avio_seek(pb, -8, SEEK_CUR);
3350             atom.size += 8;
3351             return mov_read_default(c, pb, atom);
3352         }
3353     }
3354     return 0;
3355 }
3356
3357 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3358 {
3359     int i;
3360     int width;
3361     int height;
3362     int display_matrix[3][3];
3363     AVStream *st;
3364     MOVStreamContext *sc;
3365     int version;
3366     int flags;
3367
3368     if (c->fc->nb_streams < 1)
3369         return 0;
3370     st = c->fc->streams[c->fc->nb_streams-1];
3371     sc = st->priv_data;
3372
3373     version = avio_r8(pb);
3374     flags = avio_rb24(pb);
3375     st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
3376
3377     if (version == 1) {
3378         avio_rb64(pb);
3379         avio_rb64(pb);
3380     } else {
3381         avio_rb32(pb); /* creation time */
3382         avio_rb32(pb); /* modification time */
3383     }
3384     st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
3385     avio_rb32(pb); /* reserved */
3386
3387     /* highlevel (considering edits) duration in movie timebase */
3388     (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
3389     avio_rb32(pb); /* reserved */
3390     avio_rb32(pb); /* reserved */
3391
3392     avio_rb16(pb); /* layer */
3393     avio_rb16(pb); /* alternate group */
3394     avio_rb16(pb); /* volume */
3395     avio_rb16(pb); /* reserved */
3396
3397     //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
3398     // they're kept in fixed point format through all calculations
3399     // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
3400     // side data, but the scale factor is not needed to calculate aspect ratio
3401     for (i = 0; i < 3; i++) {
3402         display_matrix[i][0] = avio_rb32(pb);   // 16.16 fixed point
3403         display_matrix[i][1] = avio_rb32(pb);   // 16.16 fixed point
3404         display_matrix[i][2] = avio_rb32(pb);   //  2.30 fixed point
3405     }
3406
3407     width = avio_rb32(pb);       // 16.16 fixed point track width
3408     height = avio_rb32(pb);      // 16.16 fixed point track height
3409     sc->width = width >> 16;
3410     sc->height = height >> 16;
3411
3412     // save the matrix and add rotate metadata when it is not the default
3413     // identity
3414     if (display_matrix[0][0] != (1 << 16) ||
3415         display_matrix[1][1] != (1 << 16) ||
3416         display_matrix[2][2] != (1 << 30) ||
3417         display_matrix[0][1] || display_matrix[0][2] ||
3418         display_matrix[1][0] || display_matrix[1][2] ||
3419         display_matrix[2][0] || display_matrix[2][1]) {
3420         int i, j;
3421         double rotate;
3422
3423         av_freep(&sc->display_matrix);
3424         sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
3425         if (!sc->display_matrix)
3426             return AVERROR(ENOMEM);
3427
3428         for (i = 0; i < 3; i++)
3429             for (j = 0; j < 3; j++)
3430                 sc->display_matrix[i * 3 + j] = display_matrix[i][j];
3431
3432         rotate = av_display_rotation_get(sc->display_matrix);
3433         if (!isnan(rotate)) {
3434             char rotate_buf[64];
3435             rotate = -rotate;
3436             if (rotate < 0) // for backward compatibility
3437                 rotate += 360;
3438             snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
3439             av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
3440         }
3441     }
3442
3443     // transform the display width/height according to the matrix
3444     // to keep the same scale, use [width height 1<<16]
3445     if (width && height && sc->display_matrix) {
3446         double disp_transform[2];
3447
3448         for (i = 0; i < 2; i++)
3449             disp_transform[i] = hypot(display_matrix[i][0], display_matrix[i][1]);
3450
3451         if (disp_transform[0] > 0       && disp_transform[1] > 0 &&
3452             disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
3453             fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
3454             st->sample_aspect_ratio = av_d2q(
3455                 disp_transform[0] / disp_transform[1],
3456                 INT_MAX);
3457     }
3458     return 0;
3459 }
3460
3461 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3462 {
3463     MOVFragment *frag = &c->fragment;
3464     MOVTrackExt *trex = NULL;
3465     MOVFragmentIndex* index = NULL;
3466     int flags, track_id, i, found = 0;
3467
3468     avio_r8(pb); /* version */
3469     flags = avio_rb24(pb);
3470
3471     track_id = avio_rb32(pb);
3472     if (!track_id)
3473         return AVERROR_INVALIDDATA;
3474     frag->track_id = track_id;
3475     for (i = 0; i < c->trex_count; i++)
3476         if (c->trex_data[i].track_id == frag->track_id) {
3477             trex = &c->trex_data[i];
3478             break;
3479         }
3480     if (!trex) {
3481         av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
3482         return AVERROR_INVALIDDATA;
3483     }
3484
3485     frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
3486                              avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
3487                              frag->moof_offset : frag->implicit_offset;
3488     frag->stsd_id  = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
3489
3490     frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
3491                      avio_rb32(pb) : trex->duration;
3492     frag->size     = flags & MOV_TFHD_DEFAULT_SIZE ?
3493                      avio_rb32(pb) : trex->size;
3494     frag->flags    = flags & MOV_TFHD_DEFAULT_FLAGS ?
3495                      avio_rb32(pb) : trex->flags;
3496     frag->time     = AV_NOPTS_VALUE;
3497     for (i = 0; i < c->fragment_index_count; i++) {
3498         int j;
3499         MOVFragmentIndex* candidate = c->fragment_index_data[i];
3500         if (candidate->track_id == frag->track_id) {
3501             av_log(c->fc, AV_LOG_DEBUG,
3502                    "found fragment index for track %u\n", frag->track_id);
3503             index = candidate;
3504             for (j = index->current_item; j < index->item_count; j++) {
3505                 if (frag->implicit_offset == index->items[j].moof_offset) {
3506                     av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
3507                             "for track %u and moof_offset %"PRId64"\n",
3508                             frag->track_id, index->items[j].moof_offset);
3509                     frag->time = index->items[j].time;
3510                     index->current_item = j + 1;
3511                     found = 1;
3512                     break;
3513                 }
3514             }
3515             if (found)
3516                 break;
3517         }
3518     }
3519     if (index && !found) {
3520         av_log(c->fc, AV_LOG_DEBUG, "track %u has a fragment index but "
3521                "it doesn't have an (in-order) entry for moof_offset "
3522                "%"PRId64"\n", frag->track_id, frag->implicit_offset);
3523     }
3524     av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
3525     return 0;
3526 }
3527
3528 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3529 {
3530     c->chapter_track = avio_rb32(pb);
3531     return 0;
3532 }
3533
3534 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3535 {
3536     MOVTrackExt *trex;
3537     int err;
3538
3539     if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
3540         return AVERROR_INVALIDDATA;
3541     if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
3542                                  sizeof(*c->trex_data))) < 0) {
3543         c->trex_count = 0;
3544         return err;
3545     }
3546
3547     c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
3548
3549     trex = &c->trex_data[c->trex_count++];
3550     avio_r8(pb); /* version */
3551     avio_rb24(pb); /* flags */
3552     trex->track_id = avio_rb32(pb);
3553     trex->stsd_id  = avio_rb32(pb);
3554     trex->duration = avio_rb32(pb);
3555     trex->size     = avio_rb32(pb);
3556     trex->flags    = avio_rb32(pb);
3557     return 0;
3558 }
3559
3560 static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3561 {
3562     MOVFragment *frag = &c->fragment;
3563     AVStream *st = NULL;
3564     MOVStreamContext *sc;
3565     int version, i;
3566
3567     for (i = 0; i < c->fc->nb_streams; i++) {
3568         if (c->fc->streams[i]->id == frag->track_id) {
3569             st = c->fc->streams[i];
3570             break;
3571         }
3572     }
3573     if (!st) {
3574         av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3575         return AVERROR_INVALIDDATA;
3576     }
3577     sc = st->priv_data;
3578     if (sc->pseudo_stream_id + 1 != frag->stsd_id)
3579         return 0;
3580     version = avio_r8(pb);
3581     avio_rb24(pb); /* flags */
3582     if (version) {
3583         sc->track_end = avio_rb64(pb);
3584     } else {
3585         sc->track_end = avio_rb32(pb);
3586     }
3587     return 0;
3588 }
3589
3590 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3591 {
3592     MOVFragment *frag = &c->fragment;
3593     AVStream *st = NULL;
3594     MOVStreamContext *sc;
3595     MOVStts *ctts_data;
3596     uint64_t offset;
3597     int64_t dts;
3598     int data_offset = 0;
3599     unsigned entries, first_sample_flags = frag->flags;
3600     int flags, distance, i, err;
3601
3602     for (i = 0; i < c->fc->nb_streams; i++) {
3603         if (c->fc->streams[i]->id == frag->track_id) {
3604             st = c->fc->streams[i];
3605             break;
3606         }
3607     }
3608     if (!st) {
3609         av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3610         return AVERROR_INVALIDDATA;
3611     }
3612     sc = st->priv_data;
3613     if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
3614         return 0;
3615     avio_r8(pb); /* version */
3616     flags = avio_rb24(pb);
3617     entries = avio_rb32(pb);
3618     av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
3619
3620     /* Always assume the presence of composition time offsets.
3621      * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
3622      *  1) in the initial movie, there are no samples.
3623      *  2) in the first movie fragment, there is only one sample without composition time offset.
3624      *  3) in the subsequent movie fragments, there are samples with composition time offset. */
3625     if (!sc->ctts_count && sc->sample_count)
3626     {
3627         /* Complement ctts table if moov atom doesn't have ctts atom. */
3628         ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
3629         if (!ctts_data)
3630             return AVERROR(ENOMEM);
3631         sc->ctts_data = ctts_data;
3632         sc->ctts_data[sc->ctts_count].count = sc->sample_count;
3633         sc->ctts_data[sc->ctts_count].duration = 0;
3634         sc->ctts_count++;
3635     }
3636     if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
3637         return AVERROR_INVALIDDATA;
3638     if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
3639                                  sizeof(*sc->ctts_data))) < 0) {
3640         sc->ctts_count = 0;
3641         return err;
3642     }
3643     if (flags & MOV_TRUN_DATA_OFFSET)        data_offset        = avio_rb32(pb);
3644     if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
3645     dts    = sc->track_end - sc->time_offset;
3646     offset = frag->base_data_offset + data_offset;
3647     distance = 0;
3648     av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
3649     for (i = 0; i < entries && !pb->eof_reached; i++) {
3650         unsigned sample_size = frag->size;
3651         int sample_flags = i ? frag->flags : first_sample_flags;
3652         unsigned sample_duration = frag->duration;
3653         int keyframe = 0;
3654
3655         if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
3656         if (flags & MOV_TRUN_SAMPLE_SIZE)     sample_size     = avio_rb32(pb);
3657         if (flags & MOV_TRUN_SAMPLE_FLAGS)    sample_flags    = avio_rb32(pb);
3658         sc->ctts_data[sc->ctts_count].count = 1;
3659         sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
3660                                                   avio_rb32(pb) : 0;
3661         mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
3662         if (frag->time != AV_NOPTS_VALUE) {
3663             if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
3664                 int64_t pts = frag->time;
3665                 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3666                         " sc->dts_shift %d ctts.duration %d"
3667                         " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
3668                         sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
3669                         sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
3670                 dts = pts - sc->dts_shift;
3671                 if (flags & MOV_TRUN_SAMPLE_CTS) {
3672                     dts -= sc->ctts_data[sc->ctts_count].duration;
3673                 } else {
3674                     dts -= sc->time_offset;
3675                 }
3676                 av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
3677             } else {
3678                 dts = frag->time - sc->time_offset;
3679                 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3680                         ", using it for dts\n", dts);
3681             }
3682             frag->time = AV_NOPTS_VALUE;
3683         }
3684         sc->ctts_count++;
3685         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
3686             keyframe = 1;
3687         else
3688             keyframe =
3689                 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
3690                                   MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
3691         if (keyframe)
3692             distance = 0;
3693         err = av_add_index_entry(st, offset, dts, sample_size, distance,
3694                                  keyframe ? AVINDEX_KEYFRAME : 0);
3695         if (err < 0) {
3696             av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
3697         }
3698         av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
3699                 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
3700                 offset, dts, sample_size, distance, keyframe);
3701         distance++;
3702         dts += sample_duration;
3703         offset += sample_size;
3704         sc->data_size += sample_size;
3705         sc->duration_for_fps += sample_duration;
3706         sc->nb_frames_for_fps ++;
3707     }
3708
3709     if (pb->eof_reached)
3710         return AVERROR_EOF;
3711
3712     frag->implicit_offset = offset;
3713
3714     sc->track_end = dts + sc->time_offset;
3715     if (st->duration < sc->track_end)
3716         st->duration = sc->track_end;
3717
3718     return 0;
3719 }
3720
3721 static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3722 {
3723     int64_t offset = avio_tell(pb) + atom.size, pts;
3724     uint8_t version;
3725     unsigned i, track_id;
3726     AVStream *st = NULL;
3727     MOVStreamContext *sc;
3728     MOVFragmentIndex *index = NULL;
3729     MOVFragmentIndex **tmp;
3730     AVRational timescale;
3731
3732     version = avio_r8(pb);
3733     if (version > 1) {
3734         avpriv_request_sample(c->fc, "sidx version %u", version);
3735         return 0;
3736     }
3737
3738     avio_rb24(pb); // flags
3739
3740     track_id = avio_rb32(pb); // Reference ID
3741     for (i = 0; i < c->fc->nb_streams; i++) {
3742         if (c->fc->streams[i]->id == track_id) {
3743             st = c->fc->streams[i];
3744             break;
3745         }
3746     }
3747     if (!st) {
3748         av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
3749         return 0;
3750     }
3751
3752     sc = st->priv_data;
3753
3754     timescale = av_make_q(1, avio_rb32(pb));
3755
3756     if (version == 0) {
3757         pts = avio_rb32(pb);
3758         offset += avio_rb32(pb);
3759     } else {
3760         pts = avio_rb64(pb);
3761         offset += avio_rb64(pb);
3762     }
3763
3764     avio_rb16(pb); // reserved
3765
3766     index = av_mallocz(sizeof(MOVFragmentIndex));
3767     if (!index)
3768         return AVERROR(ENOMEM);
3769
3770     index->track_id = track_id;
3771
3772     index->item_count = avio_rb16(pb);
3773     index->items = av_mallocz_array(index->item_count, sizeof(MOVFragmentIndexItem));
3774
3775     if (!index->items) {
3776         av_freep(&index);
3777         return AVERROR(ENOMEM);
3778     }
3779
3780     for (i = 0; i < index->item_count; i++) {
3781         uint32_t size = avio_rb32(pb);
3782         uint32_t duration = avio_rb32(pb);
3783         if (size & 0x80000000) {
3784             avpriv_request_sample(c->fc, "sidx reference_type 1");
3785             av_freep(&index->items);
3786             av_freep(&index);
3787             return AVERROR_PATCHWELCOME;
3788         }
3789         avio_rb32(pb); // sap_flags
3790         index->items[i].moof_offset = offset;
3791         index->items[i].time = av_rescale_q(pts, st->time_base, timescale);
3792         offset += size;
3793         pts += duration;
3794     }
3795
3796     st->duration = sc->track_end = pts;
3797
3798     tmp = av_realloc_array(c->fragment_index_data,
3799                            c->fragment_index_count + 1,
3800                            sizeof(MOVFragmentIndex*));
3801     if (!tmp) {
3802         av_freep(&index->items);
3803         av_freep(&index);
3804         return AVERROR(ENOMEM);
3805     }
3806
3807     c->fragment_index_data = tmp;
3808     c->fragment_index_data[c->fragment_index_count++] = index;
3809
3810     if (offset == avio_size(pb))
3811         c->fragment_index_complete = 1;
3812
3813     return 0;
3814 }
3815
3816 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
3817 /* like the files created with Adobe Premiere 5.0, for samples see */
3818 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
3819 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3820 {
3821     int err;
3822
3823     if (atom.size < 8)
3824         return 0; /* continue */
3825     if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3826         avio_skip(pb, atom.size - 4);
3827         return 0;
3828     }
3829     atom.type = avio_rl32(pb);
3830     atom.size -= 8;
3831     if (atom.type != MKTAG('m','d','a','t')) {
3832         avio_skip(pb, atom.size);
3833         return 0;
3834     }
3835     err = mov_read_mdat(c, pb, atom);
3836     return err;
3837 }
3838
3839 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3840 {
3841 #if CONFIG_ZLIB
3842     AVIOContext ctx;
3843     uint8_t *cmov_data;
3844     uint8_t *moov_data; /* uncompressed data */
3845     long cmov_len, moov_len;
3846     int ret = -1;
3847
3848     avio_rb32(pb); /* dcom atom */
3849     if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3850         return AVERROR_INVALIDDATA;
3851     if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3852         av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3853         return AVERROR_INVALIDDATA;
3854     }
3855     avio_rb32(pb); /* cmvd atom */
3856     if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3857         return AVERROR_INVALIDDATA;
3858     moov_len = avio_rb32(pb); /* uncompressed size */
3859     cmov_len = atom.size - 6 * 4;
3860
3861     cmov_data = av_malloc(cmov_len);
3862     if (!cmov_data)
3863         return AVERROR(ENOMEM);
3864     moov_data = av_malloc(moov_len);
3865     if (!moov_data) {
3866         av_free(cmov_data);
3867         return AVERROR(ENOMEM);
3868     }
3869     ret = ffio_read_size(pb, cmov_data, cmov_len);
3870     if (ret < 0)
3871         goto free_and_return;
3872
3873     if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3874         goto free_and_return;
3875     if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3876         goto free_and_return;
3877     ctx.seekable = AVIO_SEEKABLE_NORMAL;
3878     atom.type = MKTAG('m','o','o','v');
3879     atom.size = moov_len;
3880     ret = mov_read_default(c, &ctx, atom);
3881 free_and_return:
3882     av_free(moov_data);
3883     av_free(cmov_data);
3884     return ret;
3885 #else
3886     av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3887     return AVERROR(ENOSYS);
3888 #endif
3889 }
3890
3891 /* edit list atom */
3892 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3893 {
3894     MOVStreamContext *sc;
3895     int i, edit_count, version;
3896
3897     if (c->fc->nb_streams < 1 || c->ignore_editlist)
3898         return 0;
3899     sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3900
3901     version = avio_r8(pb); /* version */
3902     avio_rb24(pb); /* flags */
3903     edit_count = avio_rb32(pb); /* entries */
3904
3905     if (!edit_count)
3906         return 0;
3907     if (sc->elst_data)
3908         av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
3909     av_free(sc->elst_data);
3910     sc->elst_count = 0;
3911     sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
3912     if (!sc->elst_data)
3913         return AVERROR(ENOMEM);
3914
3915     av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3916     for (i = 0; i < edit_count && !pb->eof_reached; i++) {
3917         MOVElst *e = &sc->elst_data[i];
3918
3919         if (version == 1) {
3920             e->duration = avio_rb64(pb);
3921             e->time     = avio_rb64(pb);
3922         } else {
3923             e->duration = avio_rb32(pb); /* segment duration */
3924             e->time     = (int32_t)avio_rb32(pb); /* media time */
3925         }
3926         e->rate = avio_rb32(pb) / 65536.0;
3927         av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3928                 e->duration, e->time, e->rate);
3929     }
3930     sc->elst_count = i;
3931
3932     return 0;
3933 }
3934
3935 static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3936 {
3937     MOVStreamContext *sc;
3938
3939     if (c->fc->nb_streams < 1)
3940         return AVERROR_INVALIDDATA;
3941     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3942     sc->timecode_track = avio_rb32(pb);
3943     return 0;
3944 }
3945
3946 static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3947 {
3948     int ret;
3949     uint8_t uuid[16];
3950     static const uint8_t uuid_isml_manifest[] = {
3951         0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3952         0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3953     };
3954     static const uint8_t uuid_xmp[] = {
3955         0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
3956         0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
3957     };
3958
3959     if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3960         return AVERROR_INVALIDDATA;
3961
3962     ret = avio_read(pb, uuid, sizeof(uuid));
3963     if (ret < 0) {
3964         return ret;
3965     } else if (ret != sizeof(uuid)) {
3966         return AVERROR_INVALIDDATA;
3967     }
3968     if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3969         uint8_t *buffer, *ptr;
3970         char *endptr;
3971         size_t len = atom.size - sizeof(uuid);
3972
3973         if (len < 4) {
3974             return AVERROR_INVALIDDATA;
3975         }
3976         ret = avio_skip(pb, 4); // zeroes
3977         len -= 4;
3978
3979         buffer = av_mallocz(len + 1);
3980         if (!buffer) {
3981             return AVERROR(ENOMEM);
3982         }
3983         ret = avio_read(pb, buffer, len);
3984         if (ret < 0) {
3985             av_free(buffer);
3986             return ret;
3987         } else if (ret != len) {
3988             av_free(buffer);
3989             return AVERROR_INVALIDDATA;
3990         }
3991
3992         ptr = buffer;
3993         while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
3994             ptr += sizeof("systemBitrate=\"") - 1;
3995             c->bitrates_count++;
3996             c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3997             if (!c->bitrates) {
3998                 c->bitrates_count = 0;
3999                 av_free(buffer);
4000                 return AVERROR(ENOMEM);
4001             }
4002             errno = 0;
4003             ret = strtol(ptr, &endptr, 10);
4004             if (ret < 0 || errno || *endptr != '"') {
4005                 c->bitrates[c->bitrates_count - 1] = 0;
4006             } else {
4007                 c->bitrates[c->bitrates_count - 1] = ret;
4008             }
4009         }
4010
4011         av_free(buffer);
4012     } else if (!memcmp(uuid, uuid_xmp, sizeof(uuid))) {
4013         uint8_t *buffer;
4014         size_t len = atom.size - sizeof(uuid);
4015
4016         buffer = av_mallocz(len + 1);
4017         if (!buffer) {
4018             return AVERROR(ENOMEM);
4019         }
4020         ret = avio_read(pb, buffer, len);
4021         if (ret < 0) {
4022             av_free(buffer);
4023             return ret;
4024         } else if (ret != len) {
4025             av_free(buffer);
4026             return AVERROR_INVALIDDATA;
4027         }
4028         if (c->export_xmp) {
4029             buffer[len] = '\0';
4030             av_dict_set(&c->fc->metadata, "xmp", buffer, 0);
4031         }
4032         av_free(buffer);
4033     }
4034     return 0;
4035 }
4036
4037 static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4038 {
4039     int ret;
4040     uint8_t content[16];
4041
4042     if (atom.size < 8)
4043         return 0;
4044
4045     ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
4046     if (ret < 0)
4047         return ret;
4048
4049     if (   !c->found_moov
4050         && !c->found_mdat
4051         && !memcmp(content, "Anevia\x1A\x1A", 8)
4052         && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
4053         c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
4054     }
4055
4056     return 0;
4057 }
4058
4059 static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4060 {
4061     uint32_t format = avio_rl32(pb);
4062     MOVStreamContext *sc;
4063     enum AVCodecID id;
4064     AVStream *st;
4065
4066     if (c->fc->nb_streams < 1)
4067         return 0;
4068     st = c->fc->streams[c->fc->nb_streams - 1];
4069     sc = st->priv_data;
4070
4071     switch (sc->format)
4072     {
4073     case MKTAG('e','n','c','v'):        // encrypted video
4074     case MKTAG('e','n','c','a'):        // encrypted audio
4075         id = mov_codec_id(st, format);
4076         if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
4077             st->codecpar->codec_id != id) {
4078             av_log(c->fc, AV_LOG_WARNING,
4079                    "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
4080                    (char*)&format, st->codecpar->codec_id);
4081             break;
4082         }
4083
4084         st->codecpar->codec_id = id;
4085         sc->format = format;
4086         break;
4087
4088     default:
4089         if (format != sc->format) {
4090             av_log(c->fc, AV_LOG_WARNING,
4091                    "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
4092                    (char*)&format, (char*)&sc->format);
4093         }
4094         break;
4095     }
4096
4097     return 0;
4098 }
4099
4100 static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4101 {
4102     AVStream *st;
4103     MOVStreamContext *sc;
4104     size_t auxiliary_info_size;
4105
4106     if (c->decryption_key_len == 0 || c->fc->nb_streams < 1)
4107         return 0;
4108
4109     st = c->fc->streams[c->fc->nb_streams - 1];
4110     sc = st->priv_data;
4111
4112     if (sc->cenc.aes_ctr) {
4113         av_log(c->fc, AV_LOG_ERROR, "duplicate senc atom\n");
4114         return AVERROR_INVALIDDATA;
4115     }
4116
4117     avio_r8(pb); /* version */
4118     sc->cenc.use_subsamples = avio_rb24(pb) & 0x02; /* flags */
4119
4120     avio_rb32(pb);        /* entries */
4121
4122     if (atom.size < 8) {
4123         av_log(c->fc, AV_LOG_ERROR, "senc atom size %"PRId64" too small\n", atom.size);
4124         return AVERROR_INVALIDDATA;
4125     }
4126
4127     /* save the auxiliary info as is */
4128     auxiliary_info_size = atom.size - 8;
4129
4130     sc->cenc.auxiliary_info = av_malloc(auxiliary_info_size);
4131     if (!sc->cenc.auxiliary_info) {
4132         return AVERROR(ENOMEM);
4133     }
4134
4135     sc->cenc.auxiliary_info_end = sc->cenc.auxiliary_info + auxiliary_info_size;
4136
4137     sc->cenc.auxiliary_info_pos = sc->cenc.auxiliary_info;
4138
4139     if (avio_read(pb, sc->cenc.auxiliary_info, auxiliary_info_size) != auxiliary_info_size) {
4140         av_log(c->fc, AV_LOG_ERROR, "failed to read the auxiliary info");
4141         return AVERROR_INVALIDDATA;
4142     }
4143
4144     /* initialize the cipher */
4145     sc->cenc.aes_ctr = av_aes_ctr_alloc();
4146     if (!sc->cenc.aes_ctr) {
4147         return AVERROR(ENOMEM);
4148     }
4149
4150     return av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
4151 }
4152
4153 static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4154 {
4155     AVStream *st;
4156     MOVStreamContext *sc;
4157     size_t data_size;
4158     int atom_header_size;
4159     int flags;
4160
4161     if (c->decryption_key_len == 0 || c->fc->nb_streams < 1)
4162         return 0;
4163
4164     st = c->fc->streams[c->fc->nb_streams - 1];
4165     sc = st->priv_data;
4166
4167     if (sc->cenc.auxiliary_info_sizes || sc->cenc.auxiliary_info_default_size) {
4168         av_log(c->fc, AV_LOG_ERROR, "duplicate saiz atom\n");
4169         return AVERROR_INVALIDDATA;
4170     }
4171
4172     atom_header_size = 9;
4173
4174     avio_r8(pb); /* version */
4175     flags = avio_rb24(pb);
4176
4177     if ((flags & 0x01) != 0) {
4178         atom_header_size += 8;
4179
4180         avio_rb32(pb);    /* info type */
4181         avio_rb32(pb);    /* info type param */
4182     }
4183
4184     sc->cenc.auxiliary_info_default_size = avio_r8(pb);
4185     avio_rb32(pb);    /* entries */
4186
4187     if (atom.size <= atom_header_size) {
4188         return 0;
4189     }
4190
4191     /* save the auxiliary info sizes as is */
4192     data_size = atom.size - atom_header_size;
4193
4194     sc->cenc.auxiliary_info_sizes = av_malloc(data_size);
4195     if (!sc->cenc.auxiliary_info_sizes) {
4196         return AVERROR(ENOMEM);
4197     }
4198
4199     sc->cenc.auxiliary_info_sizes_count = data_size;
4200
4201     if (avio_read(pb, sc->cenc.auxiliary_info_sizes, data_size) != data_size) {
4202         av_log(c->fc, AV_LOG_ERROR, "failed to read the auxiliary info sizes");
4203         return AVERROR_INVALIDDATA;
4204     }
4205
4206     return 0;
4207 }
4208
4209 static int cenc_filter(MOVContext *c, MOVStreamContext *sc, uint8_t *input, int size)
4210 {
4211     uint32_t encrypted_bytes;
4212     uint16_t subsample_count;
4213     uint16_t clear_bytes;
4214     uint8_t* input_end = input + size;
4215
4216     /* read the iv */
4217     if (AES_CTR_IV_SIZE > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
4218         av_log(c->fc, AV_LOG_ERROR, "failed to read iv from the auxiliary info\n");
4219         return AVERROR_INVALIDDATA;
4220     }
4221
4222     av_aes_ctr_set_iv(sc->cenc.aes_ctr, sc->cenc.auxiliary_info_pos);
4223     sc->cenc.auxiliary_info_pos += AES_CTR_IV_SIZE;
4224
4225     if (!sc->cenc.use_subsamples)
4226     {
4227         /* decrypt the whole packet */
4228         av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
4229         return 0;
4230     }
4231
4232     /* read the subsample count */
4233     if (sizeof(uint16_t) > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
4234         av_log(c->fc, AV_LOG_ERROR, "failed to read subsample count from the auxiliary info\n");
4235         return AVERROR_INVALIDDATA;
4236     }
4237
4238     subsample_count = AV_RB16(sc->cenc.auxiliary_info_pos);
4239     sc->cenc.auxiliary_info_pos += sizeof(uint16_t);
4240
4241     for (; subsample_count > 0; subsample_count--)
4242     {
4243         if (6 > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
4244             av_log(c->fc, AV_LOG_ERROR, "failed to read subsample from the auxiliary info\n");
4245             return AVERROR_INVALIDDATA;
4246         }
4247
4248         /* read the number of clear / encrypted bytes */
4249         clear_bytes = AV_RB16(sc->cenc.auxiliary_info_pos);
4250         sc->cenc.auxiliary_info_pos += sizeof(uint16_t);
4251         encrypted_bytes = AV_RB32(sc->cenc.auxiliary_info_pos);
4252         sc->cenc.auxiliary_info_pos += sizeof(uint32_t);
4253
4254         if ((uint64_t)clear_bytes + encrypted_bytes > input_end - input) {
4255             av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
4256             return AVERROR_INVALIDDATA;
4257         }
4258
4259         /* skip the clear bytes */
4260         input += clear_bytes;
4261
4262         /* decrypt the encrypted bytes */
4263         av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, encrypted_bytes);
4264         input += encrypted_bytes;
4265     }
4266
4267     if (input < input_end) {
4268         av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
4269         return AVERROR_INVALIDDATA;
4270     }
4271
4272     return 0;
4273 }
4274
4275 static int mov_seek_auxiliary_info(AVFormatContext *s, MOVStreamContext *sc)
4276 {
4277     size_t auxiliary_info_seek_offset = 0;
4278     int i;
4279
4280     if (sc->cenc.auxiliary_info_default_size) {
4281         auxiliary_info_seek_offset = (size_t)sc->cenc.auxiliary_info_default_size * sc->current_sample;
4282     } else if (sc->cenc.auxiliary_info_sizes) {
4283         if (sc->current_sample > sc->cenc.auxiliary_info_sizes_count) {
4284             av_log(s, AV_LOG_ERROR, "current sample %d greater than the number of auxiliary info sample sizes %"SIZE_SPECIFIER"\n",
4285                 sc->current_sample, sc->cenc.auxiliary_info_sizes_count);
4286             return AVERROR_INVALIDDATA;
4287         }
4288
4289         for (i = 0; i < sc->current_sample; i++) {
4290             auxiliary_info_seek_offset += sc->cenc.auxiliary_info_sizes[i];
4291         }
4292     }
4293
4294     if (auxiliary_info_seek_offset > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info) {
4295         av_log(s, AV_LOG_ERROR, "auxiliary info offset %"SIZE_SPECIFIER" greater than auxiliary info size %"SIZE_SPECIFIER"\n",
4296             auxiliary_info_seek_offset, (size_t)(sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info));
4297         return AVERROR_INVALIDDATA;
4298     }
4299
4300     sc->cenc.auxiliary_info_pos = sc->cenc.auxiliary_info + auxiliary_info_seek_offset;
4301
4302     return 0;
4303 }
4304
4305 static const MOVParseTableEntry mov_default_parse_table[] = {
4306 { MKTAG('A','C','L','R'), mov_read_aclr },
4307 { MKTAG('A','P','R','G'), mov_read_avid },
4308 { MKTAG('A','A','L','P'), mov_read_avid },
4309 { MKTAG('A','R','E','S'), mov_read_ares },
4310 { MKTAG('a','v','s','s'), mov_read_avss },
4311 { MKTAG('c','h','p','l'), mov_read_chpl },
4312 { MKTAG('c','o','6','4'), mov_read_stco },
4313 { MKTAG('c','o','l','r'), mov_read_colr },
4314 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
4315 { MKTAG('d','i','n','f'), mov_read_default },
4316 { MKTAG('D','p','x','E'), mov_read_dpxe },
4317 { MKTAG('d','r','e','f'), mov_read_dref },
4318 { MKTAG('e','d','t','s'), mov_read_default },
4319 { MKTAG('e','l','s','t'), mov_read_elst },
4320 { MKTAG('e','n','d','a'), mov_read_enda },
4321 { MKTAG('f','i','e','l'), mov_read_fiel },
4322 { MKTAG('a','d','r','m'), mov_read_adrm },
4323 { MKTAG('f','t','y','p'), mov_read_ftyp },
4324 { MKTAG('g','l','b','l'), mov_read_glbl },
4325 { MKTAG('h','d','l','r'), mov_read_hdlr },
4326 { MKTAG('i','l','s','t'), mov_read_ilst },
4327 { MKTAG('j','p','2','h'), mov_read_jp2h },
4328 { MKTAG('m','d','a','t'), mov_read_mdat },
4329 { MKTAG('m','d','h','d'), mov_read_mdhd },
4330 { MKTAG('m','d','i','a'), mov_read_default },
4331 { MKTAG('m','e','t','a'), mov_read_meta },
4332 { MKTAG('m','i','n','f'), mov_read_default },
4333 { MKTAG('m','o','o','f'), mov_read_moof },
4334 { MKTAG('m','o','o','v'), mov_read_moov },
4335 { MKTAG('m','v','e','x'), mov_read_default },
4336 { MKTAG('m','v','h','d'), mov_read_mvhd },
4337 { MKTAG('S','M','I',' '), mov_read_svq3 },
4338 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
4339 { MKTAG('a','v','c','C'), mov_read_glbl },
4340 { MKTAG('p','a','s','p'), mov_read_pasp },
4341 { MKTAG('s','i','d','x'), mov_read_sidx },
4342 { MKTAG('s','t','b','l'), mov_read_default },
4343 { MKTAG('s','t','c','o'), mov_read_stco },
4344 { MKTAG('s','t','p','s'), mov_read_stps },
4345 { MKTAG('s','t','r','f'), mov_read_strf },
4346 { MKTAG('s','t','s','c'), mov_read_stsc },
4347 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
4348 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
4349 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
4350 { MKTAG('s','t','t','s'), mov_read_stts },
4351 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
4352 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
4353 { MKTAG('t','f','d','t'), mov_read_tfdt },
4354 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
4355 { MKTAG('t','r','a','k'), mov_read_trak },
4356 { MKTAG('t','r','a','f'), mov_read_default },
4357 { MKTAG('t','r','e','f'), mov_read_default },
4358 { MKTAG('t','m','c','d'), mov_read_tmcd },
4359 { MKTAG('c','h','a','p'), mov_read_chap },
4360 { MKTAG('t','r','e','x'), mov_read_trex },
4361 { MKTAG('t','r','u','n'), mov_read_trun },
4362 { MKTAG('u','d','t','a'), mov_read_default },
4363 { MKTAG('w','a','v','e'), mov_read_wave },
4364 { MKTAG('e','s','d','s'), mov_read_esds },
4365 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
4366 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
4367 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
4368 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
4369 { MKTAG('w','f','e','x'), mov_read_wfex },
4370 { MKTAG('c','m','o','v'), mov_read_cmov },
4371 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
4372 { MKTAG('d','v','c','1'), mov_read_dvc1 },
4373 { MKTAG('s','b','g','p'), mov_read_sbgp },
4374 { MKTAG('h','v','c','C'), mov_read_glbl },
4375 { MKTAG('u','u','i','d'), mov_read_uuid },
4376 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
4377 { MKTAG('f','r','e','e'), mov_read_free },
4378 { MKTAG('-','-','-','-'), mov_read_custom },
4379 { MKTAG('s','i','n','f'), mov_read_default },
4380 { MKTAG('f','r','m','a'), mov_read_frma },
4381 { MKTAG('s','e','n','c'), mov_read_senc },
4382 { MKTAG('s','a','i','z'), mov_read_saiz },
4383 { 0, NULL }
4384 };
4385
4386 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4387 {
4388     int64_t total_size = 0;
4389     MOVAtom a;
4390     int i;
4391
4392     if (c->atom_depth > 10) {
4393         av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
4394         return AVERROR_INVALIDDATA;
4395     }
4396     c->atom_depth ++;
4397
4398     if (atom.size < 0)
4399         atom.size = INT64_MAX;
4400     while (total_size + 8 <= atom.size && !avio_feof(pb)) {
4401         int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
4402         a.size = atom.size;
4403         a.type=0;
4404         if (atom.size >= 8) {
4405             a.size = avio_rb32(pb);
4406             a.type = avio_rl32(pb);
4407             if (a.type == MKTAG('f','r','e','e') &&
4408                 a.size >= 8 &&
4409                 c->moov_retry) {
4410                 uint8_t buf[8];
4411                 uint32_t *type = (uint32_t *)buf + 1;
4412                 if (avio_read(pb, buf, 8) != 8)
4413                     return AVERROR_INVALIDDATA;
4414                 avio_seek(pb, -8, SEEK_CUR);
4415                 if (*type == MKTAG('m','v','h','d') ||
4416                     *type == MKTAG('c','m','o','v')) {
4417                     av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
4418                     a.type = MKTAG('m','o','o','v');
4419                 }
4420             }
4421             if (atom.type != MKTAG('r','o','o','t') &&
4422                 atom.type != MKTAG('m','o','o','v'))
4423             {
4424                 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
4425                 {
4426                     av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
4427                     avio_skip(pb, -8);
4428                     c->atom_depth --;
4429                     return 0;
4430                 }
4431             }
4432             total_size += 8;
4433             if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
4434                 a.size = avio_rb64(pb) - 8;
4435                 total_size += 8;
4436             }
4437         }
4438         av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
4439                 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
4440         if (a.size == 0) {
4441             a.size = atom.size - total_size + 8;
4442         }
4443         a.size -= 8;
4444         if (a.size < 0)
4445             break;
4446         a.size = FFMIN(a.size, atom.size - total_size);
4447
4448         for (i = 0; mov_default_parse_table[i].type; i++)
4449             if (mov_default_parse_table[i].type == a.type) {
4450                 parse = mov_default_parse_table[i].parse;
4451                 break;
4452             }
4453
4454         // container is user data
4455         if (!parse && (atom.type == MKTAG('u','d','t','a') ||
4456                        atom.type == MKTAG('i','l','s','t')))
4457             parse = mov_read_udta_string;
4458
4459         // Supports parsing the QuickTime Metadata Keys.
4460         // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
4461         if (!parse && c->found_hdlr_mdta &&
4462             atom.type == MKTAG('m','e','t','a') &&
4463             a.type == MKTAG('k','e','y','s')) {
4464             parse = mov_read_keys;
4465         }
4466
4467         if (!parse) { /* skip leaf atoms data */
4468             avio_skip(pb, a.size);
4469         } else {
4470             int64_t start_pos = avio_tell(pb);
4471             int64_t left;
4472             int err = parse(c, pb, a);
4473             if (err < 0) {
4474                 c->atom_depth --;
4475                 return err;
4476             }
4477             if (c->found_moov && c->found_mdat &&
4478                 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete) ||
4479                  start_pos + a.size == avio_size(pb))) {
4480                 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete)
4481                     c->next_root_atom = start_pos + a.size;
4482                 c->atom_depth --;
4483                 return 0;
4484             }
4485             left = a.size - avio_tell(pb) + start_pos;
4486             if (left > 0) /* skip garbage at atom end */
4487                 avio_skip(pb, left);
4488             else if (left < 0) {
4489                 av_log(c->fc, AV_LOG_WARNING,
4490                        "overread end of atom '%.4s' by %"PRId64" bytes\n",
4491                        (char*)&a.type, -left);
4492                 avio_seek(pb, left, SEEK_CUR);
4493             }
4494         }
4495
4496         total_size += a.size;
4497     }
4498
4499     if (total_size < atom.size && atom.size < 0x7ffff)
4500         avio_skip(pb, atom.size - total_size);
4501
4502     c->atom_depth --;
4503     return 0;
4504 }
4505
4506 static int mov_probe(AVProbeData *p)
4507 {
4508     int64_t offset;
4509     uint32_t tag;
4510     int score = 0;
4511     int moov_offset = -1;
4512
4513     /* check file header */
4514     offset = 0;
4515     for (;;) {
4516         /* ignore invalid offset */
4517         if ((offset + 8) > (unsigned int)p->buf_size)
4518             break;
4519         tag = AV_RL32(p->buf + offset + 4);
4520         switch(tag) {
4521         /* check for obvious tags */
4522         case MKTAG('m','o','o','v'):
4523             moov_offset = offset + 4;
4524         case MKTAG('m','d','a','t'):
4525         case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
4526         case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
4527         case MKTAG('f','t','y','p'):
4528             if (AV_RB32(p->buf+offset) < 8 &&
4529                 (AV_RB32(p->buf+offset) != 1 ||
4530                  offset + 12 > (unsigned int)p->buf_size ||
4531                  AV_RB64(p->buf+offset + 8) == 0)) {
4532                 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4533             } else if (tag == MKTAG('f','t','y','p') &&
4534                        (   AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
4535                         || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
4536                     )) {
4537                 score = FFMAX(score, 5);
4538             } else {
4539                 score = AVPROBE_SCORE_MAX;
4540             }
4541             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4542             break;
4543         /* those are more common words, so rate then a bit less */
4544         case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
4545         case MKTAG('w','i','d','e'):
4546         case MKTAG('f','r','e','e'):
4547         case MKTAG('j','u','n','k'):
4548         case MKTAG('p','i','c','t'):
4549             score  = FFMAX(score, AVPROBE_SCORE_MAX - 5);
4550             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4551             break;
4552         case MKTAG(0x82,0x82,0x7f,0x7d):
4553         case MKTAG('s','k','i','p'):
4554         case MKTAG('u','u','i','d'):
4555         case MKTAG('p','r','f','l'):
4556             /* if we only find those cause probedata is too small at least rate them */
4557             score  = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4558             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4559             break;
4560         default:
4561             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4562         }
4563     }
4564     if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
4565         /* moov atom in the header - we should make sure that this is not a
4566          * MOV-packed MPEG-PS */
4567         offset = moov_offset;
4568
4569         while(offset < (p->buf_size - 16)){ /* Sufficient space */
4570                /* We found an actual hdlr atom */
4571             if(AV_RL32(p->buf + offset     ) == MKTAG('h','d','l','r') &&
4572                AV_RL32(p->buf + offset +  8) == MKTAG('m','h','l','r') &&
4573                AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
4574                 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
4575                 /* We found a media handler reference atom describing an
4576                  * MPEG-PS-in-MOV, return a
4577                  * low score to force expanding the probe window until
4578                  * mpegps_probe finds what it needs */
4579                 return 5;
4580             }else
4581                 /* Keep looking */
4582                 offset+=2;
4583         }
4584     }
4585
4586     return score;
4587 }
4588
4589 // must be done after parsing all trak because there's no order requirement
4590 static void mov_read_chapters(AVFormatContext *s)
4591 {
4592     MOVContext *mov = s->priv_data;
4593     AVStream *st = NULL;
4594     MOVStreamContext *sc;
4595     int64_t cur_pos;
4596     int i;
4597
4598     for (i = 0; i < s->nb_streams; i++)
4599         if (s->streams[i]->id == mov->chapter_track) {
4600             st = s->streams[i];
4601             break;
4602         }
4603     if (!st) {
4604         av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
4605         return;
4606     }
4607
4608     st->discard = AVDISCARD_ALL;
4609     sc = st->priv_data;
4610     cur_pos = avio_tell(sc->pb);
4611
4612     for (i = 0; i < st->nb_index_entries; i++) {
4613         AVIndexEntry *sample = &st->index_entries[i];
4614         int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
4615         uint8_t *title;
4616         uint16_t ch;
4617         int len, title_len;
4618
4619         if (end < sample->timestamp) {
4620             av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
4621             end = AV_NOPTS_VALUE;
4622         }
4623
4624         if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
4625             av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
4626             goto finish;
4627         }
4628
4629         // the first two bytes are the length of the title
4630         len = avio_rb16(sc->pb);
4631         if (len > sample->size-2)
4632             continue;
4633         title_len = 2*len + 1;
4634         if (!(title = av_mallocz(title_len)))
4635             goto finish;
4636
4637         // The samples could theoretically be in any encoding if there's an encd
4638         // atom following, but in practice are only utf-8 or utf-16, distinguished
4639         // instead by the presence of a BOM
4640         if (!len) {
4641             title[0] = 0;
4642         } else {
4643             ch = avio_rb16(sc->pb);
4644             if (ch == 0xfeff)
4645                 avio_get_str16be(sc->pb, len, title, title_len);
4646             else if (ch == 0xfffe)
4647                 avio_get_str16le(sc->pb, len, title, title_len);
4648             else {
4649                 AV_WB16(title, ch);
4650                 if (len == 1 || len == 2)
4651                     title[len] = 0;
4652                 else
4653                     avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
4654             }
4655         }
4656
4657         avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
4658         av_freep(&title);
4659     }
4660 finish:
4661     avio_seek(sc->pb, cur_pos, SEEK_SET);
4662 }
4663
4664 static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
4665                                              uint32_t value, int flags)
4666 {
4667     AVTimecode tc;
4668     char buf[AV_TIMECODE_STR_SIZE];
4669     AVRational rate = st->avg_frame_rate;
4670     int ret = av_timecode_init(&tc, rate, flags, 0, s);
4671     if (ret < 0)
4672         return ret;
4673     av_dict_set(&st->metadata, "timecode",
4674                 av_timecode_make_string(&tc, buf, value), 0);
4675     return 0;
4676 }
4677
4678 static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
4679 {
4680     MOVStreamContext *sc = st->priv_data;
4681     int flags = 0;
4682     int64_t cur_pos = avio_tell(sc->pb);
4683     uint32_t value;
4684
4685     if (!st->nb_index_entries)
4686         return -1;
4687
4688     avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
4689     value = avio_rb32(s->pb);
4690
4691     if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
4692     if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
4693     if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
4694
4695     /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
4696      * not the case) and thus assume "frame number format" instead of QT one.
4697      * No sample with tmcd track can be found with a QT timecode at the moment,
4698      * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
4699      * format). */
4700     parse_timecode_in_framenum_format(s, st, value, flags);
4701
4702     avio_seek(sc->pb, cur_pos, SEEK_SET);
4703     return 0;
4704 }
4705
4706 static int mov_read_close(AVFormatContext *s)
4707 {
4708     MOVContext *mov = s->priv_data;
4709     int i, j;
4710
4711     for (i = 0; i < s->nb_streams; i++) {
4712         AVStream *st = s->streams[i];
4713         MOVStreamContext *sc = st->priv_data;
4714
4715         if (!sc)
4716             continue;
4717
4718         av_freep(&sc->ctts_data);
4719         for (j = 0; j < sc->drefs_count; j++) {
4720             av_freep(&sc->drefs[j].path);
4721             av_freep(&sc->drefs[j].dir);
4722         }
4723         av_freep(&sc->drefs);
4724
4725         sc->drefs_count = 0;
4726
4727         if (!sc->pb_is_copied)
4728             ff_format_io_close(s, &sc->pb);
4729
4730         sc->pb = NULL;
4731         av_freep(&sc->chunk_offsets);
4732         av_freep(&sc->stsc_data);
4733         av_freep(&sc->sample_sizes);
4734         av_freep(&sc->keyframes);
4735         av_freep(&sc->stts_data);
4736         av_freep(&sc->stps_data);
4737         av_freep(&sc->elst_data);
4738         av_freep(&sc->rap_group);
4739         av_freep(&sc->display_matrix);
4740
4741         av_freep(&sc->cenc.auxiliary_info);
4742         av_freep(&sc->cenc.auxiliary_info_sizes);
4743         av_aes_ctr_free(sc->cenc.aes_ctr);
4744     }
4745
4746     if (mov->dv_demux) {
4747         avformat_free_context(mov->dv_fctx);
4748         mov->dv_fctx = NULL;
4749     }
4750
4751     if (mov->meta_keys) {
4752         for (i = 1; i < mov->meta_keys_count; i++) {
4753             av_freep(&mov->meta_keys[i]);
4754         }
4755         av_freep(&mov->meta_keys);
4756     }
4757
4758     av_freep(&mov->trex_data);
4759     av_freep(&mov->bitrates);
4760
4761     for (i = 0; i < mov->fragment_index_count; i++) {
4762         MOVFragmentIndex* index = mov->fragment_index_data[i];
4763         av_freep(&index->items);
4764         av_freep(&mov->fragment_index_data[i]);
4765     }
4766     av_freep(&mov->fragment_index_data);
4767
4768     av_freep(&mov->aes_decrypt);
4769
4770     return 0;
4771 }
4772
4773 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
4774 {
4775     int i;
4776
4777     for (i = 0; i < s->nb_streams; i++) {
4778         AVStream *st = s->streams[i];
4779         MOVStreamContext *sc = st->priv_data;
4780
4781         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
4782             sc->timecode_track == tmcd_id)
4783             return 1;
4784     }
4785     return 0;
4786 }
4787
4788 /* look for a tmcd track not referenced by any video track, and export it globally */
4789 static void export_orphan_timecode(AVFormatContext *s)
4790 {
4791     int i;
4792
4793     for (i = 0; i < s->nb_streams; i++) {
4794         AVStream *st = s->streams[i];
4795
4796         if (st->codecpar->codec_tag  == MKTAG('t','m','c','d') &&
4797             !tmcd_is_referenced(s, i + 1)) {
4798             AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
4799             if (tcr) {
4800                 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
4801                 break;
4802             }
4803         }
4804     }
4805 }
4806
4807 static int read_tfra(MOVContext *mov, AVIOContext *f)
4808 {
4809     MOVFragmentIndex* index = NULL;
4810     int version, fieldlength, i, j;
4811     int64_t pos = avio_tell(f);
4812     uint32_t size = avio_rb32(f);
4813     void *tmp;
4814
4815     if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
4816         return 1;
4817     }
4818     av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
4819     index = av_mallocz(sizeof(MOVFragmentIndex));
4820     if (!index) {
4821         return AVERROR(ENOMEM);
4822     }
4823
4824     tmp = av_realloc_array(mov->fragment_index_data,
4825                            mov->fragment_index_count + 1,
4826                            sizeof(MOVFragmentIndex*));
4827     if (!tmp) {
4828         av_freep(&index);
4829         return AVERROR(ENOMEM);
4830     }
4831     mov->fragment_index_data = tmp;
4832     mov->fragment_index_data[mov->fragment_index_count++] = index;
4833
4834     version = avio_r8(f);
4835     avio_rb24(f);
4836     index->track_id = avio_rb32(f);
4837     fieldlength = avio_rb32(f);
4838     index->item_count = avio_rb32(f);
4839     index->items = av_mallocz_array(
4840             index->item_count, sizeof(MOVFragmentIndexItem));
4841     if (!index->items) {
4842         index->item_count = 0;
4843         return AVERROR(ENOMEM);
4844     }
4845     for (i = 0; i < index->item_count; i++) {
4846         int64_t time, offset;
4847         if (version == 1) {
4848             time   = avio_rb64(f);
4849             offset = avio_rb64(f);
4850         } else {
4851             time   = avio_rb32(f);
4852             offset = avio_rb32(f);
4853         }
4854         index->items[i].time = time;
4855         index->items[i].moof_offset = offset;
4856         for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
4857             avio_r8(f);
4858         for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
4859             avio_r8(f);
4860         for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
4861             avio_r8(f);
4862     }
4863
4864     avio_seek(f, pos + size, SEEK_SET);
4865     return 0;
4866 }
4867
4868 static int mov_read_mfra(MOVContext *c, AVIOContext *f)
4869 {
4870     int64_t stream_size = avio_size(f);
4871     int64_t original_pos = avio_tell(f);
4872     int64_t seek_ret;
4873     int32_t mfra_size;
4874     int ret = -1;
4875     if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
4876         ret = seek_ret;
4877         goto fail;
4878     }
4879     mfra_size = avio_rb32(f);
4880     if (mfra_size < 0 || mfra_size > stream_size) {
4881         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
4882         goto fail;
4883     }
4884     if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
4885         ret = seek_ret;
4886         goto fail;
4887     }
4888     if (avio_rb32(f) != mfra_size) {
4889         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
4890         goto fail;
4891     }
4892     if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
4893         av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
4894         goto fail;
4895     }
4896     av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
4897     do {
4898         ret = read_tfra(c, f);
4899         if (ret < 0)
4900             goto fail;
4901     } while (!ret);
4902     ret = 0;
4903 fail:
4904     seek_ret = avio_seek(f, original_pos, SEEK_SET);
4905     if (seek_ret < 0) {
4906         av_log(c->fc, AV_LOG_ERROR,
4907                "failed to seek back after looking for mfra\n");
4908         ret = seek_ret;
4909     }
4910     return ret;
4911 }
4912
4913 static int mov_read_header(AVFormatContext *s)
4914 {
4915     MOVContext *mov = s->priv_data;
4916     AVIOContext *pb = s->pb;
4917     int j, err;
4918     MOVAtom atom = { AV_RL32("root") };
4919     int i;
4920
4921     if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
4922         av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
4923             mov->decryption_key_len, AES_CTR_KEY_SIZE);
4924         return AVERROR(EINVAL);
4925     }
4926
4927     mov->fc = s;
4928     mov->trak_index = -1;
4929     /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
4930     if (pb->seekable)
4931         atom.size = avio_size(pb);
4932     else
4933         atom.size = INT64_MAX;
4934
4935     /* check MOV header */
4936     do {
4937     if (mov->moov_retry)
4938         avio_seek(pb, 0, SEEK_SET);
4939     if ((err = mov_read_default(mov, pb, atom)) < 0) {
4940         av_log(s, AV_LOG_ERROR, "error reading header\n");
4941         mov_read_close(s);
4942         return err;
4943     }
4944     } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
4945     if (!mov->found_moov) {
4946         av_log(s, AV_LOG_ERROR, "moov atom not found\n");
4947         mov_read_close(s);
4948         return AVERROR_INVALIDDATA;
4949     }
4950     av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
4951
4952     if (pb->seekable) {
4953         if (mov->chapter_track > 0 && !mov->ignore_chapters)
4954             mov_read_chapters(s);
4955         for (i = 0; i < s->nb_streams; i++)
4956             if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd"))
4957                 mov_read_timecode_track(s, s->streams[i]);
4958     }
4959
4960     /* copy timecode metadata from tmcd tracks to the related video streams */
4961     for (i = 0; i < s->nb_streams; i++) {
4962         AVStream *st = s->streams[i];
4963         MOVStreamContext *sc = st->priv_data;
4964         if (sc->timecode_track > 0) {
4965             AVDictionaryEntry *tcr;
4966             int tmcd_st_id = -1;
4967
4968             for (j = 0; j < s->nb_streams; j++)
4969                 if (s->streams[j]->id == sc->timecode_track)
4970                     tmcd_st_id = j;
4971
4972             if (tmcd_st_id < 0 || tmcd_st_id == i)
4973                 continue;
4974             tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
4975             if (tcr)
4976                 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
4977         }
4978     }
4979     export_orphan_timecode(s);
4980
4981     for (i = 0; i < s->nb_streams; i++) {
4982         AVStream *st = s->streams[i];
4983         MOVStreamContext *sc = st->priv_data;
4984         fix_timescale(mov, sc);
4985         if(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
4986             st->skip_samples = sc->start_pad;
4987         }
4988         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
4989             av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
4990                       sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
4991         if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4992             if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
4993                 st->codecpar->width  = sc->width;
4994                 st->codecpar->height = sc->height;
4995             }
4996             if (st->codecpar->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
4997                 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
4998                     return err;
4999             }
5000         }
5001         if (mov->handbrake_version &&
5002             mov->handbrake_version <= 1000000*0 + 1000*10 + 2 &&  // 0.10.2
5003             st->codecpar->codec_id == AV_CODEC_ID_MP3
5004         ) {
5005             av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
5006             st->need_parsing = AVSTREAM_PARSE_FULL;
5007         }
5008     }
5009
5010     if (mov->trex_data) {
5011         for (i = 0; i < s->nb_streams; i++) {
5012             AVStream *st = s->streams[i];
5013             MOVStreamContext *sc = st->priv_data;
5014             if (st->duration > 0)
5015                 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
5016         }
5017     }
5018
5019     if (mov->use_mfra_for > 0) {
5020         for (i = 0; i < s->nb_streams; i++) {
5021             AVStream *st = s->streams[i];
5022             MOVStreamContext *sc = st->priv_data;
5023             if (sc->duration_for_fps > 0) {
5024                 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale /
5025                     sc->duration_for_fps;
5026             }
5027         }
5028     }
5029
5030     for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
5031         if (mov->bitrates[i]) {
5032             s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
5033         }
5034     }
5035
5036     ff_rfps_calculate(s);
5037
5038     for (i = 0; i < s->nb_streams; i++) {
5039         AVStream *st = s->streams[i];
5040         MOVStreamContext *sc = st->priv_data;
5041
5042         switch (st->codecpar->codec_type) {
5043         case AVMEDIA_TYPE_AUDIO:
5044             err = ff_replaygain_export(st, s->metadata);
5045             if (err < 0) {
5046                 mov_read_close(s);
5047                 return err;
5048             }
5049             break;
5050         case AVMEDIA_TYPE_VIDEO:
5051             if (sc->display_matrix) {
5052                 AVPacketSideData *sd, *tmp;
5053
5054                 tmp = av_realloc_array(st->side_data,
5055                                        st->nb_side_data + 1, sizeof(*tmp));
5056                 if (!tmp)
5057                     return AVERROR(ENOMEM);
5058
5059                 st->side_data = tmp;
5060                 st->nb_side_data++;
5061
5062                 sd = &st->side_data[st->nb_side_data - 1];
5063                 sd->type = AV_PKT_DATA_DISPLAYMATRIX;
5064                 sd->size = sizeof(int32_t) * 9;
5065                 sd->data = (uint8_t*)sc->display_matrix;
5066                 sc->display_matrix = NULL;
5067             }
5068             break;
5069         }
5070     }
5071     ff_configure_buffers_for_index(s, AV_TIME_BASE);
5072
5073     return 0;
5074 }
5075
5076 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
5077 {
5078     AVIndexEntry *sample = NULL;
5079     int64_t best_dts = INT64_MAX;
5080     int i;
5081     for (i = 0; i < s->nb_streams; i++) {
5082         AVStream *avst = s->streams[i];
5083         MOVStreamContext *msc = avst->priv_data;
5084         if (msc->pb && msc->current_sample < avst->nb_index_entries) {
5085             AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
5086             int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
5087             av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
5088             if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
5089                 (s->pb->seekable &&
5090                  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
5091                  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
5092                   (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
5093                 sample = current_sample;
5094                 best_dts = dts;
5095                 *st = avst;
5096             }
5097         }
5098     }
5099     return sample;
5100 }
5101
5102 static int should_retry(AVIOContext *pb, int error_code) {
5103     if (error_code == AVERROR_EOF || avio_feof(pb))
5104         return 0;
5105
5106     return 1;
5107 }
5108
5109 static int mov_switch_root(AVFormatContext *s, int64_t target)
5110 {
5111     MOVContext *mov = s->priv_data;
5112     int i, j;
5113     int already_read = 0;
5114
5115     if (avio_seek(s->pb, target, SEEK_SET) != target) {
5116         av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
5117         return AVERROR_INVALIDDATA;
5118     }
5119
5120     mov->next_root_atom = 0;
5121
5122     for (i = 0; i < mov->fragment_index_count; i++) {
5123         MOVFragmentIndex *index = mov->fragment_index_data[i];
5124         int found = 0;
5125         for (j = 0; j < index->item_count; j++) {
5126             MOVFragmentIndexItem *item = &index->items[j];
5127             if (found) {
5128                 mov->next_root_atom = item->moof_offset;
5129                 break; // Advance to next index in outer loop
5130             } else if (item->moof_offset == target) {
5131                 index->current_item = FFMIN(j, index->current_item);
5132                 if (item->headers_read)
5133                     already_read = 1;
5134                 item->headers_read = 1;
5135                 found = 1;
5136             }
5137         }
5138         if (!found)
5139             index->current_item = 0;
5140     }
5141
5142     if (already_read)
5143         return 0;
5144
5145     mov->found_mdat = 0;
5146
5147     if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
5148         avio_feof(s->pb))
5149         return AVERROR_EOF;
5150     av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
5151
5152     return 1;
5153 }
5154
5155 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
5156 {
5157     MOVContext *mov = s->priv_data;
5158     MOVStreamContext *sc;
5159     AVIndexEntry *sample;
5160     AVStream *st = NULL;
5161     int ret;
5162     mov->fc = s;
5163  retry:
5164     sample = mov_find_next_sample(s, &st);
5165     if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
5166         if (!mov->next_root_atom)
5167             return AVERROR_EOF;
5168         if ((ret = mov_switch_root(s, mov->next_root_atom)) < 0)
5169             return ret;
5170         goto retry;
5171     }
5172     sc = st->priv_data;
5173     /* must be done just before reading, to avoid infinite loop on sample */
5174     sc->current_sample++;
5175
5176     if (mov->next_root_atom) {
5177         sample->pos = FFMIN(sample->pos, mov->next_root_atom);
5178         sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
5179     }
5180
5181     if (st->discard != AVDISCARD_ALL) {
5182         int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
5183         if (ret64 != sample->pos) {
5184             av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
5185                    sc->ffindex, sample->pos);
5186             sc->current_sample -= should_retry(sc->pb, ret64);
5187             return AVERROR_INVALIDDATA;
5188         }
5189         ret = av_get_packet(sc->pb, pkt, sample->size);
5190         if (ret < 0) {
5191             sc->current_sample -= should_retry(sc->pb, ret);
5192             return ret;
5193         }
5194         if (sc->has_palette) {
5195             uint8_t *pal;
5196
5197             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
5198             if (!pal) {
5199                 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
5200             } else {
5201                 memcpy(pal, sc->palette, AVPALETTE_SIZE);
5202                 sc->has_palette = 0;
5203             }
5204         }
5205 #if CONFIG_DV_DEMUXER
5206         if (mov->dv_demux && sc->dv_audio_container) {
5207             avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
5208             av_freep(&pkt->data);
5209             pkt->size = 0;
5210             ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
5211             if (ret < 0)
5212                 return ret;
5213         }
5214 #endif
5215     }
5216
5217     pkt->stream_index = sc->ffindex;
5218     pkt->dts = sample->timestamp;
5219     if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
5220         pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
5221         /* update ctts context */
5222         sc->ctts_sample++;
5223         if (sc->ctts_index < sc->ctts_count &&
5224             sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
5225             sc->ctts_index++;
5226             sc->ctts_sample = 0;
5227         }
5228         if (sc->wrong_dts)
5229             pkt->dts = AV_NOPTS_VALUE;
5230     } else {
5231         int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
5232             st->index_entries[sc->current_sample].timestamp : st->duration;
5233         pkt->duration = next_dts - pkt->dts;
5234         pkt->pts = pkt->dts;
5235     }
5236     if (st->discard == AVDISCARD_ALL)
5237         goto retry;
5238     pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
5239     pkt->pos = sample->pos;
5240
5241     if (mov->aax_mode)
5242         aax_filter(pkt->data, pkt->size, mov);
5243
5244     if (sc->cenc.aes_ctr) {
5245         ret = cenc_filter(mov, sc, pkt->data, pkt->size);
5246         if (ret) {
5247             return ret;
5248         }
5249     }
5250
5251     return 0;
5252 }
5253
5254 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
5255 {
5256     MOVContext *mov = s->priv_data;
5257     int i, j;
5258
5259     if (!mov->fragment_index_complete)
5260         return 0;
5261
5262     for (i = 0; i < mov->fragment_index_count; i++) {
5263         if (mov->fragment_index_data[i]->track_id == st->id) {
5264             MOVFragmentIndex *index = mov->fragment_index_data[i];
5265             for (j = index->item_count - 1; j >= 0; j--) {
5266                 if (index->items[j].time <= timestamp) {
5267                     if (index->items[j].headers_read)
5268                         return 0;
5269
5270                     return mov_switch_root(s, index->items[j].moof_offset);
5271                 }
5272             }
5273         }
5274     }
5275
5276     return 0;
5277 }
5278
5279 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
5280 {
5281     MOVStreamContext *sc = st->priv_data;
5282     int sample, time_sample;
5283     int i;
5284
5285     int ret = mov_seek_fragment(s, st, timestamp);
5286     if (ret < 0)
5287         return ret;
5288
5289     sample = av_index_search_timestamp(st, timestamp, flags);
5290     av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
5291     if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
5292         sample = 0;
5293     if (sample < 0) /* not sure what to do */
5294         return AVERROR_INVALIDDATA;
5295     sc->current_sample = sample;
5296     av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
5297     /* adjust ctts index */
5298     if (sc->ctts_data) {
5299         time_sample = 0;
5300         for (i = 0; i < sc->ctts_count; i++) {
5301             int next = time_sample + sc->ctts_data[i].count;
5302             if (next > sc->current_sample) {
5303                 sc->ctts_index = i;
5304                 sc->ctts_sample = sc->current_sample - time_sample;
5305                 break;
5306             }
5307             time_sample = next;
5308         }
5309     }
5310
5311     ret = mov_seek_auxiliary_info(s, sc);
5312     if (ret < 0) {
5313         return ret;
5314     }
5315
5316     return sample;
5317 }
5318
5319 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
5320 {
5321     MOVContext *mc = s->priv_data;
5322     AVStream *st;
5323     int sample;
5324     int i;
5325
5326     if (stream_index >= s->nb_streams)
5327         return AVERROR_INVALIDDATA;
5328
5329     st = s->streams[stream_index];
5330     sample = mov_seek_stream(s, st, sample_time, flags);
5331     if (sample < 0)
5332         return sample;
5333
5334     if (mc->seek_individually) {
5335         /* adjust seek timestamp to found sample timestamp */
5336         int64_t seek_timestamp = st->index_entries[sample].timestamp;
5337
5338         for (i = 0; i < s->nb_streams; i++) {
5339             int64_t timestamp;
5340             MOVStreamContext *sc = s->streams[i]->priv_data;
5341             st = s->streams[i];
5342             st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
5343
5344             if (stream_index == i)
5345                 continue;
5346
5347             timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
5348             mov_seek_stream(s, st, timestamp, flags);
5349         }
5350     } else {
5351         for (i = 0; i < s->nb_streams; i++) {
5352             MOVStreamContext *sc;
5353             st = s->streams[i];
5354             sc = st->priv_data;
5355             sc->current_sample = 0;
5356         }
5357         while (1) {
5358             MOVStreamContext *sc;
5359             AVIndexEntry *entry = mov_find_next_sample(s, &st);
5360             if (!entry)
5361                 return AVERROR_INVALIDDATA;
5362             sc = st->priv_data;
5363             if (sc->ffindex == stream_index && sc->current_sample == sample)
5364                 break;
5365             sc->current_sample++;
5366         }
5367     }
5368     return 0;
5369 }
5370
5371 #define OFFSET(x) offsetof(MOVContext, x)
5372 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
5373 static const AVOption mov_options[] = {
5374     {"use_absolute_path",
5375         "allow using absolute path when opening alias, this is a possible security issue",
5376         OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
5377         0, 1, FLAGS},
5378     {"seek_streams_individually",
5379         "Seek each stream individually to the to the closest point",
5380         OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
5381         0, 1, FLAGS},
5382     {"ignore_editlist", "", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
5383         0, 1, FLAGS},
5384     {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
5385         0, 1, FLAGS},
5386     {"use_mfra_for",
5387         "use mfra for fragment timestamps",
5388         OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
5389         -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
5390         "use_mfra_for"},
5391     {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
5392         FLAGS, "use_mfra_for" },
5393     {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
5394         FLAGS, "use_mfra_for" },
5395     {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
5396         FLAGS, "use_mfra_for" },
5397     { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
5398         AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
5399     { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
5400         AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
5401     { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
5402         AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
5403     { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
5404         "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
5405         AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
5406         .flags = AV_OPT_FLAG_DECODING_PARAM },
5407     { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
5408     { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
5409         {.i64 = 0}, 0, 1, FLAGS },
5410
5411     { NULL },
5412 };
5413
5414 static const AVClass mov_class = {
5415     .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
5416     .item_name  = av_default_item_name,
5417     .option     = mov_options,
5418     .version    = LIBAVUTIL_VERSION_INT,
5419 };
5420
5421 AVInputFormat ff_mov_demuxer = {
5422     .name           = "mov,mp4,m4a,3gp,3g2,mj2",
5423     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
5424     .priv_class     = &mov_class,
5425     .priv_data_size = sizeof(MOVContext),
5426     .extensions     = "mov,mp4,m4a,3gp,3g2,mj2",
5427     .read_probe     = mov_probe,
5428     .read_header    = mov_read_header,
5429     .read_packet    = mov_read_packet,
5430     .read_close     = mov_read_close,
5431     .read_seek      = mov_read_seek,
5432     .flags          = AVFMT_NO_BYTE_SEEK,
5433 };