]> git.sesse.net Git - ffmpeg/blob - libavformat/mov.c
avformat/m4vdec: raise threshold slightly for detection
[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 //#define MOV_EXPORT_ALL_METADATA
31
32 #include "libavutil/attributes.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/display.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/intfloat.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/timecode.h"
42 #include "libavcodec/ac3tab.h"
43 #include "avformat.h"
44 #include "internal.h"
45 #include "avio_internal.h"
46 #include "riff.h"
47 #include "isom.h"
48 #include "libavcodec/get_bits.h"
49 #include "id3v1.h"
50 #include "mov_chan.h"
51 #include "replaygain.h"
52
53 #if CONFIG_ZLIB
54 #include <zlib.h>
55 #endif
56
57 #include "qtpalette.h"
58
59
60 #undef NDEBUG
61 #include <assert.h>
62
63 /* those functions parse an atom */
64 /* links atom IDs to parse functions */
65 typedef struct MOVParseTableEntry {
66     uint32_t type;
67     int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
68 } MOVParseTableEntry;
69
70 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
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     av_dict_set(&c->fc->metadata, key, buf, 0);
87
88     return 0;
89 }
90
91 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
92                                             unsigned len, const char *key)
93 {
94     char buf[16];
95
96     /* bypass padding bytes */
97     avio_r8(pb);
98     avio_r8(pb);
99     avio_r8(pb);
100
101     snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
102     av_dict_set(&c->fc->metadata, key, buf, 0);
103
104     return 0;
105 }
106
107 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
108                                         unsigned len, const char *key)
109 {
110     char buf[16];
111
112     snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
113     av_dict_set(&c->fc->metadata, key, buf, 0);
114
115     return 0;
116 }
117
118 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
119                              unsigned len, const char *key)
120 {
121     short genre;
122     char buf[20];
123
124     avio_r8(pb); // unknown
125
126     genre = avio_r8(pb);
127     if (genre < 1 || genre > ID3v1_GENRE_MAX)
128         return 0;
129     snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
130     av_dict_set(&c->fc->metadata, key, buf, 0);
131
132     return 0;
133 }
134
135 static const uint32_t mac_to_unicode[128] = {
136     0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
137     0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
138     0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
139     0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
140     0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
141     0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
142     0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
143     0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
144     0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
145     0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
146     0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
147     0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
148     0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
149     0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
150     0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
151     0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
152 };
153
154 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
155                                char *dst, int dstlen)
156 {
157     char *p = dst;
158     char *end = dst+dstlen-1;
159     int i;
160
161     for (i = 0; i < len; i++) {
162         uint8_t t, c = avio_r8(pb);
163         if (c < 0x80 && p < end)
164             *p++ = c;
165         else if (p < end)
166             PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
167     }
168     *p = 0;
169     return p - dst;
170 }
171
172 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
173 {
174     AVPacket pkt;
175     AVStream *st;
176     MOVStreamContext *sc;
177     enum AVCodecID id;
178     int ret;
179
180     switch (type) {
181     case 0xd:  id = AV_CODEC_ID_MJPEG; break;
182     case 0xe:  id = AV_CODEC_ID_PNG;   break;
183     case 0x1b: id = AV_CODEC_ID_BMP;   break;
184     default:
185         av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
186         avio_skip(pb, len);
187         return 0;
188     }
189
190     st = avformat_new_stream(c->fc, NULL);
191     if (!st)
192         return AVERROR(ENOMEM);
193     sc = av_mallocz(sizeof(*sc));
194     if (!sc)
195         return AVERROR(ENOMEM);
196     st->priv_data = sc;
197
198     ret = av_get_packet(pb, &pkt, len);
199     if (ret < 0)
200         return ret;
201
202     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
203
204     st->attached_pic              = pkt;
205     st->attached_pic.stream_index = st->index;
206     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
207
208     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
209     st->codec->codec_id   = id;
210
211     return 0;
212 }
213
214 static int mov_metadata_raw(MOVContext *c, AVIOContext *pb,
215                             unsigned len, const char *key)
216 {
217     char *value = av_malloc(len + 1);
218     if (!value)
219         return AVERROR(ENOMEM);
220     avio_read(pb, value, len);
221     value[len] = 0;
222     return av_dict_set(&c->fc->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
223 }
224
225 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
226 {
227     char language[4] = { 0 };
228     char buf[100];
229     uint16_t langcode = 0;
230     double longitude, latitude;
231     const char *key = "location";
232
233     if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4)
234         return AVERROR_INVALIDDATA;
235
236     avio_skip(pb, 4); // version+flags
237     langcode = avio_rb16(pb);
238     ff_mov_lang_to_iso639(langcode, language);
239     len -= 6;
240
241     len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name
242     if (len < 1)
243         return AVERROR_INVALIDDATA;
244     avio_skip(pb, 1); // role
245     len -= 1;
246
247     if (len < 14)
248         return AVERROR_INVALIDDATA;
249     longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
250     latitude  = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
251
252     // Try to output in the same format as the ?xyz field
253     snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude);
254     if (*language && strcmp(language, "und")) {
255         char key2[16];
256         snprintf(key2, sizeof(key2), "%s-%s", key, language);
257         av_dict_set(&c->fc->metadata, key2, buf, 0);
258     }
259     return av_dict_set(&c->fc->metadata, key, buf, 0);
260 }
261
262 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
263 {
264 #ifdef MOV_EXPORT_ALL_METADATA
265     char tmp_key[5];
266 #endif
267     char str[1024], key2[16], language[4] = {0};
268     const char *key = NULL;
269     uint16_t langcode = 0;
270     uint32_t data_type = 0, str_size;
271     int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
272
273     switch (atom.type) {
274     case MKTAG(0xa9,'n','a','m'): key = "title";     break;
275     case MKTAG(0xa9,'a','u','t'):
276     case MKTAG(0xa9,'A','R','T'): key = "artist";    break;
277     case MKTAG( 'a','A','R','T'): key = "album_artist";    break;
278     case MKTAG(0xa9,'w','r','t'): key = "composer";  break;
279     case MKTAG( 'c','p','i','l'): key = "compilation";
280         parse = mov_metadata_int8_no_padding; break;
281     case MKTAG( 'c','p','r','t'):
282     case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
283     case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
284     case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
285     case MKTAG(0xa9,'c','m','t'):
286     case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
287     case MKTAG(0xa9,'a','l','b'): key = "album";     break;
288     case MKTAG(0xa9,'d','a','y'): key = "date";      break;
289     case MKTAG(0xa9,'g','e','n'): key = "genre";     break;
290     case MKTAG( 'g','n','r','e'): key = "genre";
291         parse = mov_metadata_gnre; break;
292     case MKTAG(0xa9,'t','o','o'):
293     case MKTAG(0xa9,'s','w','r'): key = "encoder";   break;
294     case MKTAG(0xa9,'e','n','c'): key = "encoder";   break;
295     case MKTAG(0xa9,'m','a','k'): key = "make";      break;
296     case MKTAG(0xa9,'m','o','d'): key = "model";     break;
297     case MKTAG(0xa9,'x','y','z'): key = "location";  break;
298     case MKTAG( 'd','e','s','c'): key = "description";break;
299     case MKTAG( 'l','d','e','s'): key = "synopsis";  break;
300     case MKTAG( 't','v','s','h'): key = "show";      break;
301     case MKTAG( 't','v','e','n'): key = "episode_id";break;
302     case MKTAG( 't','v','n','n'): key = "network";   break;
303     case MKTAG( 't','r','k','n'): key = "track";
304         parse = mov_metadata_track_or_disc_number; break;
305     case MKTAG( 'd','i','s','k'): key = "disc";
306         parse = mov_metadata_track_or_disc_number; break;
307     case MKTAG( 't','v','e','s'): key = "episode_sort";
308         parse = mov_metadata_int8_bypass_padding; break;
309     case MKTAG( 't','v','s','n'): key = "season_number";
310         parse = mov_metadata_int8_bypass_padding; break;
311     case MKTAG( 's','t','i','k'): key = "media_type";
312         parse = mov_metadata_int8_no_padding; break;
313     case MKTAG( 'h','d','v','d'): key = "hd_video";
314         parse = mov_metadata_int8_no_padding; break;
315     case MKTAG( 'p','g','a','p'): key = "gapless_playback";
316         parse = mov_metadata_int8_no_padding; break;
317     case MKTAG( '@','P','R','M'):
318         return mov_metadata_raw(c, pb, atom.size, "premiere_version");
319     case MKTAG( '@','P','R','Q'):
320         return mov_metadata_raw(c, pb, atom.size, "quicktime_version");
321     case MKTAG( 'l','o','c','i'):
322         return mov_metadata_loci(c, pb, atom.size);
323     }
324
325     if (c->itunes_metadata && atom.size > 8) {
326         int data_size = avio_rb32(pb);
327         int tag = avio_rl32(pb);
328         if (tag == MKTAG('d','a','t','a')) {
329             data_type = avio_rb32(pb); // type
330             avio_rb32(pb); // unknown
331             str_size = data_size - 16;
332             atom.size -= 16;
333
334             if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
335                 int ret = mov_read_covr(c, pb, data_type, str_size);
336                 if (ret < 0) {
337                     av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
338                 }
339                 return ret;
340             }
341         } else return 0;
342     } else if (atom.size > 4 && key && !c->itunes_metadata) {
343         str_size = avio_rb16(pb); // string length
344         langcode = avio_rb16(pb);
345         ff_mov_lang_to_iso639(langcode, language);
346         atom.size -= 4;
347     } else
348         str_size = atom.size;
349
350 #ifdef MOV_EXPORT_ALL_METADATA
351     if (!key) {
352         snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
353         key = tmp_key;
354     }
355 #endif
356
357     if (!key)
358         return 0;
359     if (atom.size < 0)
360         return AVERROR_INVALIDDATA;
361
362     str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
363
364     if (parse)
365         parse(c, pb, str_size, key);
366     else {
367         if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded
368             mov_read_mac_string(c, pb, str_size, str, sizeof(str));
369         } else {
370             int ret = avio_read(pb, str, str_size);
371             if (ret != str_size)
372                 return ret < 0 ? ret : AVERROR_INVALIDDATA;
373             str[str_size] = 0;
374         }
375         av_dict_set(&c->fc->metadata, key, str, 0);
376         if (*language && strcmp(language, "und")) {
377             snprintf(key2, sizeof(key2), "%s-%s", key, language);
378             av_dict_set(&c->fc->metadata, key2, str, 0);
379         }
380     }
381     av_dlog(c->fc, "lang \"%3s\" ", language);
382     av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
383             key, str, (char*)&atom.type, str_size, atom.size);
384
385     return 0;
386 }
387
388 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
389 {
390     int64_t start;
391     int i, nb_chapters, str_len, version;
392     char str[256+1];
393
394     if ((atom.size -= 5) < 0)
395         return 0;
396
397     version = avio_r8(pb);
398     avio_rb24(pb);
399     if (version)
400         avio_rb32(pb); // ???
401     nb_chapters = avio_r8(pb);
402
403     for (i = 0; i < nb_chapters; i++) {
404         if (atom.size < 9)
405             return 0;
406
407         start = avio_rb64(pb);
408         str_len = avio_r8(pb);
409
410         if ((atom.size -= 9+str_len) < 0)
411             return 0;
412
413         avio_read(pb, str, str_len);
414         str[str_len] = 0;
415         avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
416     }
417     return 0;
418 }
419
420 #define MIN_DATA_ENTRY_BOX_SIZE 12
421 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
422 {
423     AVStream *st;
424     MOVStreamContext *sc;
425     int entries, i, j;
426
427     if (c->fc->nb_streams < 1)
428         return 0;
429     st = c->fc->streams[c->fc->nb_streams-1];
430     sc = st->priv_data;
431
432     avio_rb32(pb); // version + flags
433     entries = avio_rb32(pb);
434     if (entries >  (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
435         entries >= UINT_MAX / sizeof(*sc->drefs))
436         return AVERROR_INVALIDDATA;
437     av_free(sc->drefs);
438     sc->drefs_count = 0;
439     sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
440     if (!sc->drefs)
441         return AVERROR(ENOMEM);
442     sc->drefs_count = entries;
443
444     for (i = 0; i < sc->drefs_count; i++) {
445         MOVDref *dref = &sc->drefs[i];
446         uint32_t size = avio_rb32(pb);
447         int64_t next = avio_tell(pb) + size - 4;
448
449         if (size < 12)
450             return AVERROR_INVALIDDATA;
451
452         dref->type = avio_rl32(pb);
453         avio_rb32(pb); // version + flags
454         av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
455
456         if (dref->type == MKTAG('a','l','i','s') && size > 150) {
457             /* macintosh alias record */
458             uint16_t volume_len, len;
459             int16_t type;
460
461             avio_skip(pb, 10);
462
463             volume_len = avio_r8(pb);
464             volume_len = FFMIN(volume_len, 27);
465             avio_read(pb, dref->volume, 27);
466             dref->volume[volume_len] = 0;
467             av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
468
469             avio_skip(pb, 12);
470
471             len = avio_r8(pb);
472             len = FFMIN(len, 63);
473             avio_read(pb, dref->filename, 63);
474             dref->filename[len] = 0;
475             av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
476
477             avio_skip(pb, 16);
478
479             /* read next level up_from_alias/down_to_target */
480             dref->nlvl_from = avio_rb16(pb);
481             dref->nlvl_to   = avio_rb16(pb);
482             av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
483                    dref->nlvl_from, dref->nlvl_to);
484
485             avio_skip(pb, 16);
486
487             for (type = 0; type != -1 && avio_tell(pb) < next; ) {
488                 if(url_feof(pb))
489                     return AVERROR_EOF;
490                 type = avio_rb16(pb);
491                 len = avio_rb16(pb);
492                 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
493                 if (len&1)
494                     len += 1;
495                 if (type == 2) { // absolute path
496                     av_free(dref->path);
497                     dref->path = av_mallocz(len+1);
498                     if (!dref->path)
499                         return AVERROR(ENOMEM);
500                     avio_read(pb, dref->path, len);
501                     if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
502                         len -= volume_len;
503                         memmove(dref->path, dref->path+volume_len, len);
504                         dref->path[len] = 0;
505                     }
506                     for (j = 0; j < len; j++)
507                         if (dref->path[j] == ':')
508                             dref->path[j] = '/';
509                     av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
510                 } else if (type == 0) { // directory name
511                     av_free(dref->dir);
512                     dref->dir = av_malloc(len+1);
513                     if (!dref->dir)
514                         return AVERROR(ENOMEM);
515                     if (avio_read(pb, dref->dir, len) != len)
516                         return AVERROR_INVALIDDATA;
517                     dref->dir[len] = 0;
518                     for (j = 0; j < len; j++)
519                         if (dref->dir[j] == ':')
520                             dref->dir[j] = '/';
521                     av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
522                 } else
523                     avio_skip(pb, len);
524             }
525         }
526         avio_seek(pb, next, SEEK_SET);
527     }
528     return 0;
529 }
530
531 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
532 {
533     AVStream *st;
534     uint32_t type;
535     uint32_t av_unused ctype;
536     int title_size;
537     char *title_str;
538
539     if (c->fc->nb_streams < 1) // meta before first trak
540         return 0;
541
542     st = c->fc->streams[c->fc->nb_streams-1];
543
544     avio_r8(pb); /* version */
545     avio_rb24(pb); /* flags */
546
547     /* component type */
548     ctype = avio_rl32(pb);
549     type = avio_rl32(pb); /* component subtype */
550
551     av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
552     av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
553
554     if     (type == MKTAG('v','i','d','e'))
555         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
556     else if (type == MKTAG('s','o','u','n'))
557         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
558     else if (type == MKTAG('m','1','a',' '))
559         st->codec->codec_id = AV_CODEC_ID_MP2;
560     else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
561         st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
562
563     avio_rb32(pb); /* component  manufacture */
564     avio_rb32(pb); /* component flags */
565     avio_rb32(pb); /* component flags mask */
566
567     title_size = atom.size - 24;
568     if (title_size > 0) {
569         title_str = av_malloc(title_size + 1); /* Add null terminator */
570         if (!title_str)
571             return AVERROR(ENOMEM);
572         avio_read(pb, title_str, title_size);
573         title_str[title_size] = 0;
574         if (title_str[0])
575             av_dict_set(&st->metadata, "handler_name", title_str +
576                         (!c->isom && title_str[0] == title_size - 1), 0);
577         av_freep(&title_str);
578     }
579
580     return 0;
581 }
582
583 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
584 {
585     AVStream *st;
586     int tag;
587
588     if (fc->nb_streams < 1)
589         return 0;
590     st = fc->streams[fc->nb_streams-1];
591
592     avio_rb32(pb); /* version + flags */
593     ff_mp4_read_descr(fc, pb, &tag);
594     if (tag == MP4ESDescrTag) {
595         ff_mp4_parse_es_descr(pb, NULL);
596     } else
597         avio_rb16(pb); /* ID */
598
599     ff_mp4_read_descr(fc, pb, &tag);
600     if (tag == MP4DecConfigDescrTag)
601         ff_mp4_read_dec_config_descr(fc, st, pb);
602     return 0;
603 }
604
605 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
606 {
607     return ff_mov_read_esds(c->fc, pb, atom);
608 }
609
610 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
611 {
612     AVStream *st;
613     int ac3info, acmod, lfeon, bsmod;
614
615     if (c->fc->nb_streams < 1)
616         return 0;
617     st = c->fc->streams[c->fc->nb_streams-1];
618
619     ac3info = avio_rb24(pb);
620     bsmod = (ac3info >> 14) & 0x7;
621     acmod = (ac3info >> 11) & 0x7;
622     lfeon = (ac3info >> 10) & 0x1;
623     st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
624     st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
625     if (lfeon)
626         st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
627     st->codec->audio_service_type = bsmod;
628     if (st->codec->channels > 1 && bsmod == 0x7)
629         st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
630
631     return 0;
632 }
633
634 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
635 {
636     AVStream *st;
637     int eac3info, acmod, lfeon, bsmod;
638
639     if (c->fc->nb_streams < 1)
640         return 0;
641     st = c->fc->streams[c->fc->nb_streams-1];
642
643     /* No need to parse fields for additional independent substreams and its
644      * associated dependent substreams since libavcodec's E-AC-3 decoder
645      * does not support them yet. */
646     avio_rb16(pb); /* data_rate and num_ind_sub */
647     eac3info = avio_rb24(pb);
648     bsmod = (eac3info >> 12) & 0x1f;
649     acmod = (eac3info >>  9) & 0x7;
650     lfeon = (eac3info >>  8) & 0x1;
651     st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
652     if (lfeon)
653         st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
654     st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
655     st->codec->audio_service_type = bsmod;
656     if (st->codec->channels > 1 && bsmod == 0x7)
657         st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
658
659     return 0;
660 }
661
662 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
663 {
664     AVStream *st;
665
666     if (c->fc->nb_streams < 1)
667         return 0;
668     st = c->fc->streams[c->fc->nb_streams-1];
669
670     if (atom.size < 16)
671         return 0;
672
673     /* skip version and flags */
674     avio_skip(pb, 4);
675
676     ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
677
678     return 0;
679 }
680
681 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
682 {
683     AVStream *st;
684
685     if (c->fc->nb_streams < 1)
686         return 0;
687     st = c->fc->streams[c->fc->nb_streams-1];
688
689     if (ff_get_wav_header(pb, st->codec, atom.size) < 0) {
690         av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
691     }
692
693     return 0;
694 }
695
696 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
697 {
698     const int num = avio_rb32(pb);
699     const int den = avio_rb32(pb);
700     AVStream *st;
701
702     if (c->fc->nb_streams < 1)
703         return 0;
704     st = c->fc->streams[c->fc->nb_streams-1];
705
706     if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
707         (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
708         av_log(c->fc, AV_LOG_WARNING,
709                "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
710                st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
711                num, den);
712     } else if (den != 0) {
713         st->sample_aspect_ratio.num = num;
714         st->sample_aspect_ratio.den = den;
715     }
716     return 0;
717 }
718
719 /* this atom contains actual media data */
720 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
721 {
722     if (atom.size == 0) /* wrong one (MP4) */
723         return 0;
724     c->found_mdat=1;
725     return 0; /* now go for moov */
726 }
727
728 /* read major brand, minor version and compatible brands and store them as metadata */
729 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
730 {
731     uint32_t minor_ver;
732     int comp_brand_size;
733     char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
734     char* comp_brands_str;
735     uint8_t type[5] = {0};
736
737     avio_read(pb, type, 4);
738     if (strcmp(type, "qt  "))
739         c->isom = 1;
740     av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
741     av_dict_set(&c->fc->metadata, "major_brand", type, 0);
742     minor_ver = avio_rb32(pb); /* minor version */
743     snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
744     av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
745
746     comp_brand_size = atom.size - 8;
747     if (comp_brand_size < 0)
748         return AVERROR_INVALIDDATA;
749     comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
750     if (!comp_brands_str)
751         return AVERROR(ENOMEM);
752     avio_read(pb, comp_brands_str, comp_brand_size);
753     comp_brands_str[comp_brand_size] = 0;
754     av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
755     av_freep(&comp_brands_str);
756
757     return 0;
758 }
759
760 /* this atom should contain all header atoms */
761 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
762 {
763     int ret;
764
765     if (c->found_moov) {
766         av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
767         avio_skip(pb, atom.size);
768         return 0;
769     }
770
771     if ((ret = mov_read_default(c, pb, atom)) < 0)
772         return ret;
773     /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
774     /* so we don't parse the whole file if over a network */
775     c->found_moov=1;
776     return 0; /* now go for mdat */
777 }
778
779 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
780 {
781     c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
782     av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
783     return mov_read_default(c, pb, atom);
784 }
785
786 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
787 {
788     char buffer[32];
789     if (time) {
790         struct tm *ptm;
791         time_t timet;
792         if(time >= 2082844800)
793             time -= 2082844800;  /* seconds between 1904-01-01 and Epoch */
794         timet = time;
795         ptm = gmtime(&timet);
796         if (!ptm) return;
797         strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
798         av_dict_set(metadata, "creation_time", buffer, 0);
799     }
800 }
801
802 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
803 {
804     AVStream *st;
805     MOVStreamContext *sc;
806     int version;
807     char language[4] = {0};
808     unsigned lang;
809     int64_t creation_time;
810
811     if (c->fc->nb_streams < 1)
812         return 0;
813     st = c->fc->streams[c->fc->nb_streams-1];
814     sc = st->priv_data;
815
816     if (sc->time_scale) {
817         av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
818         return AVERROR_INVALIDDATA;
819     }
820
821     version = avio_r8(pb);
822     if (version > 1) {
823         avpriv_request_sample(c->fc, "Version %d", version);
824         return AVERROR_PATCHWELCOME;
825     }
826     avio_rb24(pb); /* flags */
827     if (version == 1) {
828         creation_time = avio_rb64(pb);
829         avio_rb64(pb);
830     } else {
831         creation_time = avio_rb32(pb);
832         avio_rb32(pb); /* modification time */
833     }
834     mov_metadata_creation_time(&st->metadata, creation_time);
835
836     sc->time_scale = avio_rb32(pb);
837     st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
838
839     lang = avio_rb16(pb); /* language */
840     if (ff_mov_lang_to_iso639(lang, language))
841         av_dict_set(&st->metadata, "language", language, 0);
842     avio_rb16(pb); /* quality */
843
844     return 0;
845 }
846
847 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
848 {
849     int64_t creation_time;
850     int version = avio_r8(pb); /* version */
851     avio_rb24(pb); /* flags */
852
853     if (version == 1) {
854         creation_time = avio_rb64(pb);
855         avio_rb64(pb);
856     } else {
857         creation_time = avio_rb32(pb);
858         avio_rb32(pb); /* modification time */
859     }
860     mov_metadata_creation_time(&c->fc->metadata, creation_time);
861     c->time_scale = avio_rb32(pb); /* time scale */
862
863     av_dlog(c->fc, "time scale = %i\n", c->time_scale);
864
865     c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
866     // set the AVCodecContext duration because the duration of individual tracks
867     // may be inaccurate
868     if (c->time_scale > 0 && !c->trex_data)
869         c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
870     avio_rb32(pb); /* preferred scale */
871
872     avio_rb16(pb); /* preferred volume */
873
874     avio_skip(pb, 10); /* reserved */
875
876     avio_skip(pb, 36); /* display matrix */
877
878     avio_rb32(pb); /* preview time */
879     avio_rb32(pb); /* preview duration */
880     avio_rb32(pb); /* poster time */
881     avio_rb32(pb); /* selection time */
882     avio_rb32(pb); /* selection duration */
883     avio_rb32(pb); /* current time */
884     avio_rb32(pb); /* next track ID */
885     return 0;
886 }
887
888 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
889 {
890     AVStream *st;
891     int little_endian;
892
893     if (c->fc->nb_streams < 1)
894         return 0;
895     st = c->fc->streams[c->fc->nb_streams-1];
896
897     little_endian = avio_rb16(pb) & 0xFF;
898     av_dlog(c->fc, "enda %d\n", little_endian);
899     if (little_endian == 1) {
900         switch (st->codec->codec_id) {
901         case AV_CODEC_ID_PCM_S24BE:
902             st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
903             break;
904         case AV_CODEC_ID_PCM_S32BE:
905             st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
906             break;
907         case AV_CODEC_ID_PCM_F32BE:
908             st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
909             break;
910         case AV_CODEC_ID_PCM_F64BE:
911             st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
912             break;
913         default:
914             break;
915         }
916     }
917     return 0;
918 }
919
920 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
921 {
922     AVStream *st;
923     unsigned mov_field_order;
924     enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
925
926     if (c->fc->nb_streams < 1) // will happen with jp2 files
927         return 0;
928     st = c->fc->streams[c->fc->nb_streams-1];
929     if (atom.size < 2)
930         return AVERROR_INVALIDDATA;
931     mov_field_order = avio_rb16(pb);
932     if ((mov_field_order & 0xFF00) == 0x0100)
933         decoded_field_order = AV_FIELD_PROGRESSIVE;
934     else if ((mov_field_order & 0xFF00) == 0x0200) {
935         switch (mov_field_order & 0xFF) {
936         case 0x01: decoded_field_order = AV_FIELD_TT;
937                    break;
938         case 0x06: decoded_field_order = AV_FIELD_BB;
939                    break;
940         case 0x09: decoded_field_order = AV_FIELD_TB;
941                    break;
942         case 0x0E: decoded_field_order = AV_FIELD_BT;
943                    break;
944         }
945     }
946     if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
947         av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
948     }
949     st->codec->field_order = decoded_field_order;
950
951     return 0;
952 }
953
954 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
955 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
956                               enum AVCodecID codec_id)
957 {
958     AVStream *st;
959     uint64_t size;
960     uint8_t *buf;
961     int err;
962
963     if (c->fc->nb_streams < 1) // will happen with jp2 files
964         return 0;
965     st= c->fc->streams[c->fc->nb_streams-1];
966
967     if (st->codec->codec_id != codec_id)
968         return 0; /* unexpected codec_id - don't mess with extradata */
969
970     size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
971     if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
972         return AVERROR_INVALIDDATA;
973     if ((err = av_reallocp(&st->codec->extradata, size)) < 0) {
974         st->codec->extradata_size = 0;
975         return err;
976     }
977     buf = st->codec->extradata + st->codec->extradata_size;
978     st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
979     AV_WB32(       buf    , atom.size + 8);
980     AV_WL32(       buf + 4, atom.type);
981     err = avio_read(pb, buf + 8, atom.size);
982     if (err < 0) {
983         return err;
984     } else if (err < atom.size) {
985         av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
986         st->codec->extradata_size -= atom.size - err;
987     }
988     memset(buf + 8 + err, 0, FF_INPUT_BUFFER_PADDING_SIZE);
989     return 0;
990 }
991
992 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
993 static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
994 {
995     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
996 }
997
998 static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
999 {
1000     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1001 }
1002
1003 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1004 {
1005     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1006 }
1007
1008 static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1009 {
1010     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1011 }
1012
1013 static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1014 {
1015     int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1016
1017     if (!ret && c->fc->nb_streams >= 1) {
1018         AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1019         if (avctx->extradata_size >= 40) {
1020             avctx->height = AV_RB16(&avctx->extradata[36]);
1021             avctx->width  = AV_RB16(&avctx->extradata[38]);
1022         }
1023     }
1024     return ret;
1025 }
1026
1027 static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1028 {
1029     if (c->fc->nb_streams >= 1) {
1030         AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1031         if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1032             codec->codec_id == AV_CODEC_ID_H264 &&
1033             atom.size > 11) {
1034             avio_skip(pb, 10);
1035             /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1036             if (avio_rb16(pb) == 0xd4d)
1037                 codec->width = 1440;
1038             return 0;
1039         }
1040     }
1041
1042     return mov_read_avid(c, pb, atom);
1043 }
1044
1045 static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1046 {
1047     return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1048 }
1049
1050 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1051 {
1052     AVStream *st;
1053
1054     if (c->fc->nb_streams < 1)
1055         return 0;
1056     st = c->fc->streams[c->fc->nb_streams-1];
1057
1058     if ((uint64_t)atom.size > (1<<30))
1059         return AVERROR_INVALIDDATA;
1060
1061     if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1062         st->codec->codec_id == AV_CODEC_ID_QDMC ||
1063         st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1064         // pass all frma atom to codec, needed at least for QDMC and QDM2
1065         av_free(st->codec->extradata);
1066         if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1067             return AVERROR(ENOMEM);
1068     } else if (atom.size > 8) { /* to read frma, esds atoms */
1069         int ret;
1070         if ((ret = mov_read_default(c, pb, atom)) < 0)
1071             return ret;
1072     } else
1073         avio_skip(pb, atom.size);
1074     return 0;
1075 }
1076
1077 /**
1078  * This function reads atom content and puts data in extradata without tag
1079  * nor size unlike mov_read_extradata.
1080  */
1081 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1082 {
1083     AVStream *st;
1084
1085     if (c->fc->nb_streams < 1)
1086         return 0;
1087     st = c->fc->streams[c->fc->nb_streams-1];
1088
1089     if ((uint64_t)atom.size > (1<<30))
1090         return AVERROR_INVALIDDATA;
1091
1092     if (atom.size >= 10) {
1093         // Broken files created by legacy versions of libavformat will
1094         // wrap a whole fiel atom inside of a glbl atom.
1095         unsigned size = avio_rb32(pb);
1096         unsigned type = avio_rl32(pb);
1097         avio_seek(pb, -8, SEEK_CUR);
1098         if (type == MKTAG('f','i','e','l') && size == atom.size)
1099             return mov_read_default(c, pb, atom);
1100     }
1101     av_free(st->codec->extradata);
1102     if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1103         return AVERROR(ENOMEM);
1104
1105     return 0;
1106 }
1107
1108 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1109 {
1110     AVStream *st;
1111     uint8_t profile_level;
1112     int ret;
1113
1114     if (c->fc->nb_streams < 1)
1115         return 0;
1116     st = c->fc->streams[c->fc->nb_streams-1];
1117
1118     if (atom.size >= (1<<28) || atom.size < 7)
1119         return AVERROR_INVALIDDATA;
1120
1121     profile_level = avio_r8(pb);
1122     if ((profile_level & 0xf0) != 0xc0)
1123         return 0;
1124
1125     avio_seek(pb, 6, SEEK_CUR);
1126     av_free(st->codec->extradata);
1127     if ((ret = ff_get_extradata(st->codec, pb, atom.size - 7)) < 0)
1128         return ret;
1129
1130     return 0;
1131 }
1132
1133 /**
1134  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1135  * but can have extradata appended at the end after the 40 bytes belonging
1136  * to the struct.
1137  */
1138 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1139 {
1140     AVStream *st;
1141
1142     if (c->fc->nb_streams < 1)
1143         return 0;
1144     if (atom.size <= 40)
1145         return 0;
1146     st = c->fc->streams[c->fc->nb_streams-1];
1147
1148     if ((uint64_t)atom.size > (1<<30))
1149         return AVERROR_INVALIDDATA;
1150
1151     avio_skip(pb, 40);
1152     av_free(st->codec->extradata);
1153     if (ff_get_extradata(st->codec, pb, atom.size - 40) < 0)
1154         return AVERROR(ENOMEM);
1155     return 0;
1156 }
1157
1158 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1159 {
1160     AVStream *st;
1161     MOVStreamContext *sc;
1162     unsigned int i, entries;
1163
1164     if (c->fc->nb_streams < 1)
1165         return 0;
1166     st = c->fc->streams[c->fc->nb_streams-1];
1167     sc = st->priv_data;
1168
1169     avio_r8(pb); /* version */
1170     avio_rb24(pb); /* flags */
1171
1172     entries = avio_rb32(pb);
1173
1174     if (!entries)
1175         return 0;
1176     if (entries >= UINT_MAX/sizeof(int64_t))
1177         return AVERROR_INVALIDDATA;
1178
1179     sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
1180     if (!sc->chunk_offsets)
1181         return AVERROR(ENOMEM);
1182     sc->chunk_count = entries;
1183
1184     if      (atom.type == MKTAG('s','t','c','o'))
1185         for (i = 0; i < entries && !pb->eof_reached; i++)
1186             sc->chunk_offsets[i] = avio_rb32(pb);
1187     else if (atom.type == MKTAG('c','o','6','4'))
1188         for (i = 0; i < entries && !pb->eof_reached; i++)
1189             sc->chunk_offsets[i] = avio_rb64(pb);
1190     else
1191         return AVERROR_INVALIDDATA;
1192
1193     sc->chunk_count = i;
1194
1195     if (pb->eof_reached)
1196         return AVERROR_EOF;
1197
1198     return 0;
1199 }
1200
1201 /**
1202  * Compute codec id for 'lpcm' tag.
1203  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1204  */
1205 enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1206 {
1207     /* lpcm flags:
1208      * 0x1 = float
1209      * 0x2 = big-endian
1210      * 0x4 = signed
1211      */
1212     return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1213 }
1214
1215 static int mov_codec_id(AVStream *st, uint32_t format)
1216 {
1217     int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1218
1219     if (id <= 0 &&
1220         ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1221          (format & 0xFFFF) == 'T' + ('S' << 8)))
1222         id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1223
1224     if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1225         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1226     } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1227                /* skip old asf mpeg4 tag */
1228                format && format != MKTAG('m','p','4','s')) {
1229         id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1230         if (id <= 0)
1231             id = ff_codec_get_id(ff_codec_bmp_tags, format);
1232         if (id > 0)
1233             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1234         else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1235                     (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1236                     st->codec->codec_id == AV_CODEC_ID_NONE)) {
1237             id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1238             if (id > 0)
1239                 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1240         }
1241     }
1242
1243     st->codec->codec_tag = format;
1244
1245     return id;
1246 }
1247
1248 static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1249                                  AVStream *st, MOVStreamContext *sc)
1250 {
1251     uint8_t codec_name[32];
1252     unsigned int color_depth, len, j;
1253     int color_greyscale;
1254     int color_table_id;
1255
1256     avio_rb16(pb); /* version */
1257     avio_rb16(pb); /* revision level */
1258     avio_rb32(pb); /* vendor */
1259     avio_rb32(pb); /* temporal quality */
1260     avio_rb32(pb); /* spatial quality */
1261
1262     st->codec->width  = avio_rb16(pb); /* width */
1263     st->codec->height = avio_rb16(pb); /* height */
1264
1265     avio_rb32(pb); /* horiz resolution */
1266     avio_rb32(pb); /* vert resolution */
1267     avio_rb32(pb); /* data size, always 0 */
1268     avio_rb16(pb); /* frames per samples */
1269
1270     len = avio_r8(pb); /* codec name, pascal string */
1271     if (len > 31)
1272         len = 31;
1273     mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1274     if (len < 31)
1275         avio_skip(pb, 31 - len);
1276
1277     if (codec_name[0])
1278         av_dict_set(&st->metadata, "encoder", codec_name, 0);
1279
1280     /* codec_tag YV12 triggers an UV swap in rawdec.c */
1281     if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1282         st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1283         st->codec->width &= ~1;
1284         st->codec->height &= ~1;
1285     }
1286     /* Flash Media Server uses tag H263 with Sorenson Spark */
1287     if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1288         !memcmp(codec_name, "Sorenson H263", 13))
1289         st->codec->codec_id = AV_CODEC_ID_FLV1;
1290
1291     st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1292     color_table_id = avio_rb16(pb); /* colortable id */
1293     av_dlog(c->fc, "depth %d, ctab id %d\n",
1294             st->codec->bits_per_coded_sample, color_table_id);
1295     /* figure out the palette situation */
1296     color_depth     = st->codec->bits_per_coded_sample & 0x1F;
1297     color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1298     /* Do not create a greyscale palette for cinepak */
1299     if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
1300         return;
1301
1302     /* if the depth is 2, 4, or 8 bpp, file is palettized */
1303     if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1304         /* for palette traversal */
1305         unsigned int color_start, color_count, color_end;
1306         unsigned char a, r, g, b;
1307
1308         if (color_greyscale) {
1309             int color_index, color_dec;
1310             /* compute the greyscale palette */
1311             st->codec->bits_per_coded_sample = color_depth;
1312             color_count = 1 << color_depth;
1313             color_index = 255;
1314             color_dec   = 256 / (color_count - 1);
1315             for (j = 0; j < color_count; j++) {
1316                 r = g = b = color_index;
1317                 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1318                 color_index -= color_dec;
1319                 if (color_index < 0)
1320                     color_index = 0;
1321             }
1322         } else if (color_table_id) {
1323             const uint8_t *color_table;
1324             /* if flag bit 3 is set, use the default palette */
1325             color_count = 1 << color_depth;
1326             if (color_depth == 2)
1327                 color_table = ff_qt_default_palette_4;
1328             else if (color_depth == 4)
1329                 color_table = ff_qt_default_palette_16;
1330             else
1331                 color_table = ff_qt_default_palette_256;
1332
1333             for (j = 0; j < color_count; j++) {
1334                 r = color_table[j * 3 + 0];
1335                 g = color_table[j * 3 + 1];
1336                 b = color_table[j * 3 + 2];
1337                 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1338             }
1339         } else {
1340             /* load the palette from the file */
1341             color_start = avio_rb32(pb);
1342             color_count = avio_rb16(pb);
1343             color_end   = avio_rb16(pb);
1344             if ((color_start <= 255) && (color_end <= 255)) {
1345                 for (j = color_start; j <= color_end; j++) {
1346                     /* each A, R, G, or B component is 16 bits;
1347                         * only use the top 8 bits */
1348                     a = avio_r8(pb);
1349                     avio_r8(pb);
1350                     r = avio_r8(pb);
1351                     avio_r8(pb);
1352                     g = avio_r8(pb);
1353                     avio_r8(pb);
1354                     b = avio_r8(pb);
1355                     avio_r8(pb);
1356                     sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1357                 }
1358             }
1359         }
1360         sc->has_palette = 1;
1361     }
1362 }
1363
1364 static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1365                                  AVStream *st, MOVStreamContext *sc)
1366 {
1367     int bits_per_sample, flags;
1368     uint16_t version = avio_rb16(pb);
1369     AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1370
1371     avio_rb16(pb); /* revision level */
1372     avio_rb32(pb); /* vendor */
1373
1374     st->codec->channels              = avio_rb16(pb); /* channel count */
1375     st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1376     av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
1377
1378     sc->audio_cid = avio_rb16(pb);
1379     avio_rb16(pb); /* packet size = 0 */
1380
1381     st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1382
1383     // Read QT version 1 fields. In version 0 these do not exist.
1384     av_dlog(c->fc, "version =%d, isom =%d\n", version, c->isom);
1385     if (!c->isom ||
1386         (compatible_brands && strstr(compatible_brands->value, "qt  "))) {
1387
1388         if (version == 1) {
1389             sc->samples_per_frame = avio_rb32(pb);
1390             avio_rb32(pb); /* bytes per packet */
1391             sc->bytes_per_frame = avio_rb32(pb);
1392             avio_rb32(pb); /* bytes per sample */
1393         } else if (version == 2) {
1394             avio_rb32(pb); /* sizeof struct only */
1395             st->codec->sample_rate = av_int2double(avio_rb64(pb));
1396             st->codec->channels    = avio_rb32(pb);
1397             avio_rb32(pb); /* always 0x7F000000 */
1398             st->codec->bits_per_coded_sample = avio_rb32(pb);
1399
1400             flags = avio_rb32(pb); /* lpcm format specific flag */
1401             sc->bytes_per_frame   = avio_rb32(pb);
1402             sc->samples_per_frame = avio_rb32(pb);
1403             if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1404                 st->codec->codec_id =
1405                     ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
1406                                              flags);
1407         }
1408     }
1409
1410     switch (st->codec->codec_id) {
1411     case AV_CODEC_ID_PCM_S8:
1412     case AV_CODEC_ID_PCM_U8:
1413         if (st->codec->bits_per_coded_sample == 16)
1414             st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
1415         break;
1416     case AV_CODEC_ID_PCM_S16LE:
1417     case AV_CODEC_ID_PCM_S16BE:
1418         if (st->codec->bits_per_coded_sample == 8)
1419             st->codec->codec_id = AV_CODEC_ID_PCM_S8;
1420         else if (st->codec->bits_per_coded_sample == 24)
1421             st->codec->codec_id =
1422                 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1423                 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1424         break;
1425     /* set values for old format before stsd version 1 appeared */
1426     case AV_CODEC_ID_MACE3:
1427         sc->samples_per_frame = 6;
1428         sc->bytes_per_frame   = 2 * st->codec->channels;
1429         break;
1430     case AV_CODEC_ID_MACE6:
1431         sc->samples_per_frame = 6;
1432         sc->bytes_per_frame   = 1 * st->codec->channels;
1433         break;
1434     case AV_CODEC_ID_ADPCM_IMA_QT:
1435         sc->samples_per_frame = 64;
1436         sc->bytes_per_frame   = 34 * st->codec->channels;
1437         break;
1438     case AV_CODEC_ID_GSM:
1439         sc->samples_per_frame = 160;
1440         sc->bytes_per_frame   = 33;
1441         break;
1442     default:
1443         break;
1444     }
1445
1446     bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1447     if (bits_per_sample) {
1448         st->codec->bits_per_coded_sample = bits_per_sample;
1449         sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1450     }
1451 }
1452
1453 static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1454                                     AVStream *st, MOVStreamContext *sc,
1455                                     int size)
1456 {
1457     // ttxt stsd contains display flags, justification, background
1458     // color, fonts, and default styles, so fake an atom to read it
1459     MOVAtom fake_atom = { .size = size };
1460     // mp4s contains a regular esds atom
1461     if (st->codec->codec_tag != AV_RL32("mp4s"))
1462         mov_read_glbl(c, pb, fake_atom);
1463     st->codec->width  = sc->width;
1464     st->codec->height = sc->height;
1465 }
1466
1467 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1468 {
1469     uint8_t r, g, b;
1470     int y, cb, cr;
1471
1472     y  = (ycbcr >> 16) & 0xFF;
1473     cr = (ycbcr >> 8)  & 0xFF;
1474     cb =  ycbcr        & 0xFF;
1475
1476     b = av_clip_uint8(1.164 * (y - 16)                      + 2.018 * (cb - 128));
1477     g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
1478     r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
1479
1480     return (r << 16) | (g << 8) | b;
1481 }
1482
1483 static int mov_rewrite_dvd_sub_extradata(AVStream *st)
1484 {
1485     char buf[256] = {0};
1486     uint8_t *src = st->codec->extradata;
1487     int i;
1488
1489     if (st->codec->extradata_size != 64)
1490         return 0;
1491
1492     if (st->codec->width > 0 &&  st->codec->height > 0)
1493         snprintf(buf, sizeof(buf), "size: %dx%d\n",
1494                  st->codec->width, st->codec->height);
1495     av_strlcat(buf, "palette: ", sizeof(buf));
1496
1497     for (i = 0; i < 16; i++) {
1498         uint32_t yuv = AV_RB32(src + i * 4);
1499         uint32_t rgba = yuv_to_rgba(yuv);
1500
1501         av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1502     }
1503
1504     if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1505         return 0;
1506
1507     av_freep(&st->codec->extradata);
1508     st->codec->extradata_size = 0;
1509     st->codec->extradata = av_mallocz(strlen(buf) + FF_INPUT_BUFFER_PADDING_SIZE);
1510     if (!st->codec->extradata)
1511         return AVERROR(ENOMEM);
1512     st->codec->extradata_size = strlen(buf);
1513     memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1514
1515     return 0;
1516 }
1517
1518 static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
1519                                 AVStream *st, MOVStreamContext *sc,
1520                                 int size)
1521 {
1522     if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1523         if (ff_get_extradata(st->codec, pb, size) < 0)
1524             return AVERROR(ENOMEM);
1525         if (size > 16) {
1526             MOVStreamContext *tmcd_ctx = st->priv_data;
1527             int val;
1528             val = AV_RB32(st->codec->extradata + 4);
1529             tmcd_ctx->tmcd_flags = val;
1530             if (val & 1)
1531                 st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
1532             st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1533             st->codec->time_base.num = 1;
1534         }
1535     } else {
1536         /* other codec type, just skip (rtp, mp4s ...) */
1537         avio_skip(pb, size);
1538     }
1539     return 0;
1540 }
1541
1542 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
1543                                    AVStream *st, MOVStreamContext *sc)
1544 {
1545     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1546         !st->codec->sample_rate && sc->time_scale > 1)
1547         st->codec->sample_rate = sc->time_scale;
1548
1549     /* special codec parameters handling */
1550     switch (st->codec->codec_id) {
1551 #if CONFIG_DV_DEMUXER
1552     case AV_CODEC_ID_DVAUDIO:
1553         c->dv_fctx  = avformat_alloc_context();
1554         c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
1555         if (!c->dv_demux) {
1556             av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1557             return AVERROR(ENOMEM);
1558         }
1559         sc->dv_audio_container = 1;
1560         st->codec->codec_id    = AV_CODEC_ID_PCM_S16LE;
1561         break;
1562 #endif
1563     /* no ifdef since parameters are always those */
1564     case AV_CODEC_ID_QCELP:
1565         st->codec->channels = 1;
1566         // force sample rate for qcelp when not stored in mov
1567         if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
1568             st->codec->sample_rate = 8000;
1569         // FIXME: Why is the following needed for some files?
1570         sc->samples_per_frame = 160;
1571         if (!sc->bytes_per_frame)
1572             sc->bytes_per_frame = 35;
1573         break;
1574     case AV_CODEC_ID_AMR_NB:
1575         st->codec->channels    = 1;
1576         /* force sample rate for amr, stsd in 3gp does not store sample rate */
1577         st->codec->sample_rate = 8000;
1578         break;
1579     case AV_CODEC_ID_AMR_WB:
1580         st->codec->channels    = 1;
1581         st->codec->sample_rate = 16000;
1582         break;
1583     case AV_CODEC_ID_MP2:
1584     case AV_CODEC_ID_MP3:
1585         /* force type after stsd for m1a hdlr */
1586         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1587         st->need_parsing      = AVSTREAM_PARSE_FULL;
1588         break;
1589     case AV_CODEC_ID_GSM:
1590     case AV_CODEC_ID_ADPCM_MS:
1591     case AV_CODEC_ID_ADPCM_IMA_WAV:
1592     case AV_CODEC_ID_ILBC:
1593     case AV_CODEC_ID_MACE3:
1594     case AV_CODEC_ID_MACE6:
1595     case AV_CODEC_ID_QDM2:
1596         st->codec->block_align = sc->bytes_per_frame;
1597         break;
1598     case AV_CODEC_ID_ALAC:
1599         if (st->codec->extradata_size == 36) {
1600             st->codec->channels    = AV_RB8 (st->codec->extradata + 21);
1601             st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
1602         }
1603         break;
1604     case AV_CODEC_ID_AC3:
1605     case AV_CODEC_ID_MPEG1VIDEO:
1606     case AV_CODEC_ID_VC1:
1607         st->need_parsing = AVSTREAM_PARSE_FULL;
1608         break;
1609     default:
1610         break;
1611     }
1612     return 0;
1613 }
1614
1615 static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1616                                   int codec_tag, int format,
1617                                   int size)
1618 {
1619     int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1620
1621     if (codec_tag &&
1622          (codec_tag != format &&
1623           (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1624                                  : codec_tag != MKTAG('j','p','e','g')))) {
1625         /* Multiple fourcc, we skip JPEG. This is not correct, we should
1626          * export it as a separate AVStream but this needs a few changes
1627          * in the MOV demuxer, patch welcome. */
1628
1629         av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1630         avio_skip(pb, size);
1631         return 1;
1632     }
1633     if ( codec_tag == AV_RL32("avc1") ||
1634          codec_tag == AV_RL32("hvc1") ||
1635          codec_tag == AV_RL32("hev1")
1636     )
1637         av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
1638
1639     return 0;
1640 }
1641
1642 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
1643 {
1644     AVStream *st;
1645     MOVStreamContext *sc;
1646     int pseudo_stream_id;
1647
1648     if (c->fc->nb_streams < 1)
1649         return 0;
1650     st = c->fc->streams[c->fc->nb_streams-1];
1651     sc = st->priv_data;
1652
1653     for (pseudo_stream_id = 0;
1654          pseudo_stream_id < entries && !pb->eof_reached;
1655          pseudo_stream_id++) {
1656         //Parsing Sample description table
1657         enum AVCodecID id;
1658         int ret, dref_id = 1;
1659         MOVAtom a = { AV_RL32("stsd") };
1660         int64_t start_pos = avio_tell(pb);
1661         int64_t size = avio_rb32(pb); /* size */
1662         uint32_t format = avio_rl32(pb); /* data format */
1663
1664         if (size >= 16) {
1665             avio_rb32(pb); /* reserved */
1666             avio_rb16(pb); /* reserved */
1667             dref_id = avio_rb16(pb);
1668         }else if (size <= 7){
1669             av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
1670             return AVERROR_INVALIDDATA;
1671         }
1672
1673         if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1674                                    size - (avio_tell(pb) - start_pos)))
1675             continue;
1676
1677         sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
1678         sc->dref_id= dref_id;
1679
1680         id = mov_codec_id(st, format);
1681
1682         av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
1683                 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
1684                 (format >> 24) & 0xff, st->codec->codec_type);
1685
1686         if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
1687             st->codec->codec_id = id;
1688             mov_parse_stsd_video(c, pb, st, sc);
1689         } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
1690             st->codec->codec_id = id;
1691             mov_parse_stsd_audio(c, pb, st, sc);
1692         } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
1693             st->codec->codec_id = id;
1694             mov_parse_stsd_subtitle(c, pb, st, sc,
1695                                     size - (avio_tell(pb) - start_pos));
1696         } else {
1697             ret = mov_parse_stsd_data(c, pb, st, sc,
1698                                       size - (avio_tell(pb) - start_pos));
1699             if (ret < 0)
1700                 return ret;
1701         }
1702         /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1703         a.size = size - (avio_tell(pb) - start_pos);
1704         if (a.size > 8) {
1705             if ((ret = mov_read_default(c, pb, a)) < 0)
1706                 return ret;
1707         } else if (a.size > 0)
1708             avio_skip(pb, a.size);
1709     }
1710
1711     if (pb->eof_reached)
1712         return AVERROR_EOF;
1713
1714     return mov_finalize_stsd_codec(c, pb, st, sc);
1715 }
1716
1717 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1718 {
1719     int entries;
1720
1721     avio_r8(pb); /* version */
1722     avio_rb24(pb); /* flags */
1723     entries = avio_rb32(pb);
1724
1725     return ff_mov_read_stsd_entries(c, pb, entries);
1726 }
1727
1728 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1729 {
1730     AVStream *st;
1731     MOVStreamContext *sc;
1732     unsigned int i, entries;
1733
1734     if (c->fc->nb_streams < 1)
1735         return 0;
1736     st = c->fc->streams[c->fc->nb_streams-1];
1737     sc = st->priv_data;
1738
1739     avio_r8(pb); /* version */
1740     avio_rb24(pb); /* flags */
1741
1742     entries = avio_rb32(pb);
1743
1744     av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1745
1746     if (!entries)
1747         return 0;
1748     if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1749         return AVERROR_INVALIDDATA;
1750     sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1751     if (!sc->stsc_data)
1752         return AVERROR(ENOMEM);
1753
1754     for (i = 0; i < entries && !pb->eof_reached; i++) {
1755         sc->stsc_data[i].first = avio_rb32(pb);
1756         sc->stsc_data[i].count = avio_rb32(pb);
1757         sc->stsc_data[i].id = avio_rb32(pb);
1758     }
1759
1760     sc->stsc_count = i;
1761
1762     if (pb->eof_reached)
1763         return AVERROR_EOF;
1764
1765     return 0;
1766 }
1767
1768 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1769 {
1770     AVStream *st;
1771     MOVStreamContext *sc;
1772     unsigned i, entries;
1773
1774     if (c->fc->nb_streams < 1)
1775         return 0;
1776     st = c->fc->streams[c->fc->nb_streams-1];
1777     sc = st->priv_data;
1778
1779     avio_rb32(pb); // version + flags
1780
1781     entries = avio_rb32(pb);
1782     if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1783         return AVERROR_INVALIDDATA;
1784     sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1785     if (!sc->stps_data)
1786         return AVERROR(ENOMEM);
1787
1788     for (i = 0; i < entries && !pb->eof_reached; i++) {
1789         sc->stps_data[i] = avio_rb32(pb);
1790         //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1791     }
1792
1793     sc->stps_count = i;
1794
1795     if (pb->eof_reached)
1796         return AVERROR_EOF;
1797
1798     return 0;
1799 }
1800
1801 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1802 {
1803     AVStream *st;
1804     MOVStreamContext *sc;
1805     unsigned int i, entries;
1806
1807     if (c->fc->nb_streams < 1)
1808         return 0;
1809     st = c->fc->streams[c->fc->nb_streams-1];
1810     sc = st->priv_data;
1811
1812     avio_r8(pb); /* version */
1813     avio_rb24(pb); /* flags */
1814
1815     entries = avio_rb32(pb);
1816
1817     av_dlog(c->fc, "keyframe_count = %d\n", entries);
1818
1819     if (!entries)
1820     {
1821         sc->keyframe_absent = 1;
1822         if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1823             st->need_parsing = AVSTREAM_PARSE_HEADERS;
1824         return 0;
1825     }
1826     if (entries >= UINT_MAX / sizeof(int))
1827         return AVERROR_INVALIDDATA;
1828     sc->keyframes = av_malloc(entries * sizeof(int));
1829     if (!sc->keyframes)
1830         return AVERROR(ENOMEM);
1831
1832     for (i = 0; i < entries && !pb->eof_reached; i++) {
1833         sc->keyframes[i] = avio_rb32(pb);
1834         //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1835     }
1836
1837     sc->keyframe_count = i;
1838
1839     if (pb->eof_reached)
1840         return AVERROR_EOF;
1841
1842     return 0;
1843 }
1844
1845 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1846 {
1847     AVStream *st;
1848     MOVStreamContext *sc;
1849     unsigned int i, entries, sample_size, field_size, num_bytes;
1850     GetBitContext gb;
1851     unsigned char* buf;
1852
1853     if (c->fc->nb_streams < 1)
1854         return 0;
1855     st = c->fc->streams[c->fc->nb_streams-1];
1856     sc = st->priv_data;
1857
1858     avio_r8(pb); /* version */
1859     avio_rb24(pb); /* flags */
1860
1861     if (atom.type == MKTAG('s','t','s','z')) {
1862         sample_size = avio_rb32(pb);
1863         if (!sc->sample_size) /* do not overwrite value computed in stsd */
1864             sc->sample_size = sample_size;
1865         sc->stsz_sample_size = sample_size;
1866         field_size = 32;
1867     } else {
1868         sample_size = 0;
1869         avio_rb24(pb); /* reserved */
1870         field_size = avio_r8(pb);
1871     }
1872     entries = avio_rb32(pb);
1873
1874     av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1875
1876     sc->sample_count = entries;
1877     if (sample_size)
1878         return 0;
1879
1880     if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
1881         av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
1882         return AVERROR_INVALIDDATA;
1883     }
1884
1885     if (!entries)
1886         return 0;
1887     if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
1888         return AVERROR_INVALIDDATA;
1889     sc->sample_sizes = av_malloc(entries * sizeof(int));
1890     if (!sc->sample_sizes)
1891         return AVERROR(ENOMEM);
1892
1893     num_bytes = (entries*field_size+4)>>3;
1894
1895     buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
1896     if (!buf) {
1897         av_freep(&sc->sample_sizes);
1898         return AVERROR(ENOMEM);
1899     }
1900
1901     if (avio_read(pb, buf, num_bytes) < num_bytes) {
1902         av_freep(&sc->sample_sizes);
1903         av_free(buf);
1904         return AVERROR_INVALIDDATA;
1905     }
1906
1907     init_get_bits(&gb, buf, 8*num_bytes);
1908
1909     for (i = 0; i < entries && !pb->eof_reached; i++) {
1910         sc->sample_sizes[i] = get_bits_long(&gb, field_size);
1911         sc->data_size += sc->sample_sizes[i];
1912     }
1913
1914     sc->sample_count = i;
1915
1916     if (pb->eof_reached)
1917         return AVERROR_EOF;
1918
1919     av_free(buf);
1920     return 0;
1921 }
1922
1923 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1924 {
1925     AVStream *st;
1926     MOVStreamContext *sc;
1927     unsigned int i, entries;
1928     int64_t duration=0;
1929     int64_t total_sample_count=0;
1930
1931     if (c->fc->nb_streams < 1)
1932         return 0;
1933     st = c->fc->streams[c->fc->nb_streams-1];
1934     sc = st->priv_data;
1935
1936     avio_r8(pb); /* version */
1937     avio_rb24(pb); /* flags */
1938     entries = avio_rb32(pb);
1939
1940     av_dlog(c->fc, "track[%i].stts.entries = %i\n",
1941             c->fc->nb_streams-1, entries);
1942
1943     if (entries >= UINT_MAX / sizeof(*sc->stts_data))
1944         return -1;
1945
1946     av_free(sc->stts_data);
1947     sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1948     if (!sc->stts_data)
1949         return AVERROR(ENOMEM);
1950
1951     for (i = 0; i < entries && !pb->eof_reached; i++) {
1952         int sample_duration;
1953         int sample_count;
1954
1955         sample_count=avio_rb32(pb);
1956         sample_duration = avio_rb32(pb);
1957
1958         /* sample_duration < 0 is invalid based on the spec */
1959         if (sample_duration < 0) {
1960             av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta %d in STTS, at %d st:%d\n",
1961                    sample_duration, i, c->fc->nb_streams-1);
1962             sample_duration = 1;
1963         }
1964         if (sample_count < 0) {
1965             av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
1966             return AVERROR_INVALIDDATA;
1967         }
1968         sc->stts_data[i].count= sample_count;
1969         sc->stts_data[i].duration= sample_duration;
1970
1971         av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
1972                 sample_count, sample_duration);
1973
1974         if (   i+1 == entries
1975             && i
1976             && sample_count == 1
1977             && total_sample_count > 100
1978             && sample_duration/10 > duration / total_sample_count)
1979             sample_duration = duration / total_sample_count;
1980         duration+=(int64_t)sample_duration*sample_count;
1981         total_sample_count+=sample_count;
1982     }
1983
1984     sc->stts_count = i;
1985
1986     sc->duration_for_fps  += duration;
1987     sc->nb_frames_for_fps += total_sample_count;
1988
1989     if (pb->eof_reached)
1990         return AVERROR_EOF;
1991
1992     st->nb_frames= total_sample_count;
1993     if (duration)
1994         st->duration= duration;
1995     sc->track_end = duration;
1996     return 0;
1997 }
1998
1999 static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2000 {
2001     if (duration < 0) {
2002         sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2003     }
2004 }
2005
2006 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2007 {
2008     AVStream *st;
2009     MOVStreamContext *sc;
2010     unsigned int i, entries;
2011
2012     if (c->fc->nb_streams < 1)
2013         return 0;
2014     st = c->fc->streams[c->fc->nb_streams-1];
2015     sc = st->priv_data;
2016
2017     avio_r8(pb); /* version */
2018     avio_rb24(pb); /* flags */
2019     entries = avio_rb32(pb);
2020
2021     av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2022
2023     if (!entries)
2024         return 0;
2025     if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2026         return AVERROR_INVALIDDATA;
2027     sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
2028     if (!sc->ctts_data)
2029         return AVERROR(ENOMEM);
2030
2031     for (i = 0; i < entries && !pb->eof_reached; i++) {
2032         int count    =avio_rb32(pb);
2033         int duration =avio_rb32(pb);
2034
2035         sc->ctts_data[i].count   = count;
2036         sc->ctts_data[i].duration= duration;
2037
2038         av_dlog(c->fc, "count=%d, duration=%d\n",
2039                 count, duration);
2040
2041         if (FFABS(duration) > (1<<28) && i+2<entries) {
2042             av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2043             av_freep(&sc->ctts_data);
2044             sc->ctts_count = 0;
2045             return 0;
2046         }
2047
2048         if (i+2<entries)
2049             mov_update_dts_shift(sc, duration);
2050     }
2051
2052     sc->ctts_count = i;
2053
2054     if (pb->eof_reached)
2055         return AVERROR_EOF;
2056
2057     av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
2058
2059     return 0;
2060 }
2061
2062 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2063 {
2064     AVStream *st;
2065     MOVStreamContext *sc;
2066     unsigned int i, entries;
2067     uint8_t version;
2068     uint32_t grouping_type;
2069
2070     if (c->fc->nb_streams < 1)
2071         return 0;
2072     st = c->fc->streams[c->fc->nb_streams-1];
2073     sc = st->priv_data;
2074
2075     version = avio_r8(pb); /* version */
2076     avio_rb24(pb); /* flags */
2077     grouping_type = avio_rl32(pb);
2078     if (grouping_type != MKTAG( 'r','a','p',' '))
2079         return 0; /* only support 'rap ' grouping */
2080     if (version == 1)
2081         avio_rb32(pb); /* grouping_type_parameter */
2082
2083     entries = avio_rb32(pb);
2084     if (!entries)
2085         return 0;
2086     if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2087         return AVERROR_INVALIDDATA;
2088     sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2089     if (!sc->rap_group)
2090         return AVERROR(ENOMEM);
2091
2092     for (i = 0; i < entries && !pb->eof_reached; i++) {
2093         sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2094         sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2095     }
2096
2097     sc->rap_group_count = i;
2098
2099     return pb->eof_reached ? AVERROR_EOF : 0;
2100 }
2101
2102 static void mov_build_index(MOVContext *mov, AVStream *st)
2103 {
2104     MOVStreamContext *sc = st->priv_data;
2105     int64_t current_offset;
2106     int64_t current_dts = 0;
2107     unsigned int stts_index = 0;
2108     unsigned int stsc_index = 0;
2109     unsigned int stss_index = 0;
2110     unsigned int stps_index = 0;
2111     unsigned int i, j;
2112     uint64_t stream_size = 0;
2113
2114     /* adjust first dts according to edit list */
2115     if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
2116         if (sc->empty_duration)
2117             sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
2118         sc->time_offset = sc->start_time - sc->empty_duration;
2119         current_dts = -sc->time_offset;
2120         if (sc->ctts_count>0 && sc->stts_count>0 &&
2121             sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2122             /* more than 16 frames delay, dts are likely wrong
2123                this happens with files created by iMovie */
2124             sc->wrong_dts = 1;
2125             st->codec->has_b_frames = 1;
2126         }
2127     }
2128
2129     /* only use old uncompressed audio chunk demuxing when stts specifies it */
2130     if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2131           sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2132         unsigned int current_sample = 0;
2133         unsigned int stts_sample = 0;
2134         unsigned int sample_size;
2135         unsigned int distance = 0;
2136         unsigned int rap_group_index = 0;
2137         unsigned int rap_group_sample = 0;
2138         int rap_group_present = sc->rap_group_count && sc->rap_group;
2139         int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2140
2141         current_dts -= sc->dts_shift;
2142
2143         if (!sc->sample_count || st->nb_index_entries)
2144             return;
2145         if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2146             return;
2147         if (av_reallocp_array(&st->index_entries,
2148                               st->nb_index_entries + sc->sample_count,
2149                               sizeof(*st->index_entries)) < 0) {
2150             st->nb_index_entries = 0;
2151             return;
2152         }
2153         st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2154
2155         for (i = 0; i < sc->chunk_count; i++) {
2156             int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2157             current_offset = sc->chunk_offsets[i];
2158             while (stsc_index + 1 < sc->stsc_count &&
2159                 i + 1 == sc->stsc_data[stsc_index + 1].first)
2160                 stsc_index++;
2161
2162             if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2163                 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2164                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2165                 sc->stsz_sample_size = sc->sample_size;
2166             }
2167             if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2168                 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2169                 sc->stsz_sample_size = sc->sample_size;
2170             }
2171
2172             for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2173                 int keyframe = 0;
2174                 if (current_sample >= sc->sample_count) {
2175                     av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2176                     return;
2177                 }
2178
2179                 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2180                     keyframe = 1;
2181                     if (stss_index + 1 < sc->keyframe_count)
2182                         stss_index++;
2183                 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2184                     keyframe = 1;
2185                     if (stps_index + 1 < sc->stps_count)
2186                         stps_index++;
2187                 }
2188                 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2189                     if (sc->rap_group[rap_group_index].index > 0)
2190                         keyframe = 1;
2191                     if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2192                         rap_group_sample = 0;
2193                         rap_group_index++;
2194                     }
2195                 }
2196                 if (sc->keyframe_absent
2197                     && !sc->stps_count
2198                     && !rap_group_present
2199                     && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2200                      keyframe = 1;
2201                 if (keyframe)
2202                     distance = 0;
2203                 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2204                 if (sc->pseudo_stream_id == -1 ||
2205                    sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2206                     AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2207                     e->pos = current_offset;
2208                     e->timestamp = current_dts;
2209                     e->size = sample_size;
2210                     e->min_distance = distance;
2211                     e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2212                     av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2213                             "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2214                             current_offset, current_dts, sample_size, distance, keyframe);
2215                     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2216                         ff_rfps_add_frame(mov->fc, st, current_dts);
2217                 }
2218
2219                 current_offset += sample_size;
2220                 stream_size += sample_size;
2221                 current_dts += sc->stts_data[stts_index].duration;
2222                 distance++;
2223                 stts_sample++;
2224                 current_sample++;
2225                 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2226                     stts_sample = 0;
2227                     stts_index++;
2228                 }
2229             }
2230         }
2231         if (st->duration > 0)
2232             st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2233     } else {
2234         unsigned chunk_samples, total = 0;
2235
2236         // compute total chunk count
2237         for (i = 0; i < sc->stsc_count; i++) {
2238             unsigned count, chunk_count;
2239
2240             chunk_samples = sc->stsc_data[i].count;
2241             if (i != sc->stsc_count - 1 &&
2242                 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2243                 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2244                 return;
2245             }
2246
2247             if (sc->samples_per_frame >= 160) { // gsm
2248                 count = chunk_samples / sc->samples_per_frame;
2249             } else if (sc->samples_per_frame > 1) {
2250                 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2251                 count = (chunk_samples+samples-1) / samples;
2252             } else {
2253                 count = (chunk_samples+1023) / 1024;
2254             }
2255
2256             if (i < sc->stsc_count - 1)
2257                 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2258             else
2259                 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2260             total += chunk_count * count;
2261         }
2262
2263         av_dlog(mov->fc, "chunk count %d\n", total);
2264         if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2265             return;
2266         if (av_reallocp_array(&st->index_entries,
2267                               st->nb_index_entries + total,
2268                               sizeof(*st->index_entries)) < 0) {
2269             st->nb_index_entries = 0;
2270             return;
2271         }
2272         st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2273
2274         // populate index
2275         for (i = 0; i < sc->chunk_count; i++) {
2276             current_offset = sc->chunk_offsets[i];
2277             if (stsc_index + 1 < sc->stsc_count &&
2278                 i + 1 == sc->stsc_data[stsc_index + 1].first)
2279                 stsc_index++;
2280             chunk_samples = sc->stsc_data[stsc_index].count;
2281
2282             while (chunk_samples > 0) {
2283                 AVIndexEntry *e;
2284                 unsigned size, samples;
2285
2286                 if (sc->samples_per_frame >= 160) { // gsm
2287                     samples = sc->samples_per_frame;
2288                     size = sc->bytes_per_frame;
2289                 } else {
2290                     if (sc->samples_per_frame > 1) {
2291                         samples = FFMIN((1024 / sc->samples_per_frame)*
2292                                         sc->samples_per_frame, chunk_samples);
2293                         size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2294                     } else {
2295                         samples = FFMIN(1024, chunk_samples);
2296                         size = samples * sc->sample_size;
2297                     }
2298                 }
2299
2300                 if (st->nb_index_entries >= total) {
2301                     av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2302                     return;
2303                 }
2304                 e = &st->index_entries[st->nb_index_entries++];
2305                 e->pos = current_offset;
2306                 e->timestamp = current_dts;
2307                 e->size = size;
2308                 e->min_distance = 0;
2309                 e->flags = AVINDEX_KEYFRAME;
2310                 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2311                         "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2312                         size, samples);
2313
2314                 current_offset += size;
2315                 current_dts += samples;
2316                 chunk_samples -= samples;
2317             }
2318         }
2319     }
2320 }
2321
2322 static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
2323                          AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
2324 {
2325     /* try relative path, we do not try the absolute because it can leak information about our
2326        system to an attacker */
2327     if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2328         char filename[1024];
2329         const char *src_path;
2330         int i, l;
2331
2332         /* find a source dir */
2333         src_path = strrchr(src, '/');
2334         if (src_path)
2335             src_path++;
2336         else
2337             src_path = src;
2338
2339         /* find a next level down to target */
2340         for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2341             if (ref->path[l] == '/') {
2342                 if (i == ref->nlvl_to - 1)
2343                     break;
2344                 else
2345                     i++;
2346             }
2347
2348         /* compose filename if next level down to target was found */
2349         if (i == ref->nlvl_to - 1 && src_path - src  < sizeof(filename)) {
2350             memcpy(filename, src, src_path - src);
2351             filename[src_path - src] = 0;
2352
2353             for (i = 1; i < ref->nlvl_from; i++)
2354                 av_strlcat(filename, "../", 1024);
2355
2356             av_strlcat(filename, ref->path + l + 1, 1024);
2357
2358             if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2359                 return 0;
2360         }
2361     } else if (use_absolute_path) {
2362         av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
2363                "this is a possible security issue\n");
2364         if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2365             return 0;
2366     }
2367
2368     return AVERROR(ENOENT);
2369 }
2370
2371 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
2372 {
2373     if (sc->time_scale <= 0) {
2374         av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
2375         sc->time_scale = c->time_scale;
2376         if (sc->time_scale <= 0)
2377             sc->time_scale = 1;
2378     }
2379 }
2380
2381 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2382 {
2383     AVStream *st;
2384     MOVStreamContext *sc;
2385     int ret;
2386
2387     st = avformat_new_stream(c->fc, NULL);
2388     if (!st) return AVERROR(ENOMEM);
2389     st->id = c->fc->nb_streams;
2390     sc = av_mallocz(sizeof(MOVStreamContext));
2391     if (!sc) return AVERROR(ENOMEM);
2392
2393     st->priv_data = sc;
2394     st->codec->codec_type = AVMEDIA_TYPE_DATA;
2395     sc->ffindex = st->index;
2396
2397     if ((ret = mov_read_default(c, pb, atom)) < 0)
2398         return ret;
2399
2400     /* sanity checks */
2401     if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2402                             (!sc->sample_size && !sc->sample_count))) {
2403         av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2404                st->index);
2405         return 0;
2406     }
2407
2408     fix_timescale(c, sc);
2409
2410     avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2411
2412     mov_build_index(c, st);
2413
2414     if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2415         MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2416         if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
2417             c->use_absolute_path, c->fc) < 0)
2418             av_log(c->fc, AV_LOG_ERROR,
2419                    "stream %d, error opening alias: path='%s', dir='%s', "
2420                    "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2421                    st->index, dref->path, dref->dir, dref->filename,
2422                    dref->volume, dref->nlvl_from, dref->nlvl_to);
2423     } else {
2424         sc->pb = c->fc->pb;
2425         sc->pb_is_copied = 1;
2426     }
2427
2428     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2429         if (!st->sample_aspect_ratio.num &&
2430             (st->codec->width != sc->width || st->codec->height != sc->height)) {
2431             st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2432                                              ((double)st->codec->width * sc->height), INT_MAX);
2433         }
2434
2435 #if FF_API_R_FRAME_RATE
2436         if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
2437             av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2438                       sc->time_scale, sc->stts_data[0].duration, INT_MAX);
2439 #endif
2440     }
2441
2442     // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2443     if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2444         TAG_IS_AVCI(st->codec->codec_tag)) {
2445         ret = ff_generate_avci_extradata(st);
2446         if (ret < 0)
2447             return ret;
2448     }
2449
2450     switch (st->codec->codec_id) {
2451 #if CONFIG_H261_DECODER
2452     case AV_CODEC_ID_H261:
2453 #endif
2454 #if CONFIG_H263_DECODER
2455     case AV_CODEC_ID_H263:
2456 #endif
2457 #if CONFIG_MPEG4_DECODER
2458     case AV_CODEC_ID_MPEG4:
2459 #endif
2460         st->codec->width = 0; /* let decoder init width/height */
2461         st->codec->height= 0;
2462         break;
2463     }
2464
2465     /* Do not need those anymore. */
2466     av_freep(&sc->chunk_offsets);
2467     av_freep(&sc->stsc_data);
2468     av_freep(&sc->sample_sizes);
2469     av_freep(&sc->keyframes);
2470     av_freep(&sc->stts_data);
2471     av_freep(&sc->stps_data);
2472     av_freep(&sc->rap_group);
2473
2474     return 0;
2475 }
2476
2477 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2478 {
2479     int ret;
2480     c->itunes_metadata = 1;
2481     ret = mov_read_default(c, pb, atom);
2482     c->itunes_metadata = 0;
2483     return ret;
2484 }
2485
2486 static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
2487 {
2488     int64_t end = avio_tell(pb) + size;
2489     uint8_t *key = NULL, *val = NULL;
2490     int i;
2491     AVStream *st;
2492     MOVStreamContext *sc;
2493
2494     if (c->fc->nb_streams < 1)
2495         return 0;
2496     st = c->fc->streams[c->fc->nb_streams-1];
2497     sc = st->priv_data;
2498
2499     for (i = 0; i < 2; i++) {
2500         uint8_t **p;
2501         uint32_t len, tag;
2502
2503         if (end - avio_tell(pb) <= 12)
2504             break;
2505
2506         len = avio_rb32(pb);
2507         tag = avio_rl32(pb);
2508         avio_skip(pb, 4); // flags
2509
2510         if (len < 12 || len - 12 > end - avio_tell(pb))
2511             break;
2512         len -= 12;
2513
2514         if (tag == MKTAG('n', 'a', 'm', 'e'))
2515             p = &key;
2516         else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2517             avio_skip(pb, 4);
2518             len -= 4;
2519             p = &val;
2520         } else
2521             break;
2522
2523         *p = av_malloc(len + 1);
2524         if (!*p)
2525             break;
2526         avio_read(pb, *p, len);
2527         (*p)[len] = 0;
2528     }
2529
2530     if (key && val) {
2531         if (strcmp(key, "iTunSMPB") == 0) {
2532             int priming, remainder, samples;
2533             if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
2534                 if(priming>0 && priming<16384)
2535                     sc->start_pad = priming;
2536             }
2537         } else if (strcmp(key, "cdec") == 0) {
2538         } else {
2539             av_dict_set(&c->fc->metadata, key, val,
2540                         AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
2541             key = val = NULL;
2542         }
2543     }
2544
2545     avio_seek(pb, end, SEEK_SET);
2546     av_freep(&key);
2547     av_freep(&val);
2548     return 0;
2549 }
2550
2551 static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2552 {
2553     int64_t end = avio_tell(pb) + atom.size;
2554     uint32_t tag, len;
2555
2556     if (atom.size < 8)
2557         goto fail;
2558
2559     len = avio_rb32(pb);
2560     tag = avio_rl32(pb);
2561
2562     if (len > atom.size)
2563         goto fail;
2564
2565     if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2566         uint8_t domain[128];
2567         int domain_len;
2568
2569         avio_skip(pb, 4); // flags
2570         len -= 12;
2571
2572         domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2573         avio_skip(pb, len - domain_len);
2574         return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
2575     }
2576
2577 fail:
2578     av_log(c->fc, AV_LOG_VERBOSE,
2579            "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2580     return 0;
2581 }
2582
2583 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2584 {
2585     while (atom.size > 8) {
2586         uint32_t tag = avio_rl32(pb);
2587         atom.size -= 4;
2588         if (tag == MKTAG('h','d','l','r')) {
2589             avio_seek(pb, -8, SEEK_CUR);
2590             atom.size += 8;
2591             return mov_read_default(c, pb, atom);
2592         }
2593     }
2594     return 0;
2595 }
2596
2597 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2598 {
2599     int i;
2600     int width;
2601     int height;
2602     int64_t disp_transform[2];
2603     int display_matrix[3][3];
2604     AVStream *st;
2605     MOVStreamContext *sc;
2606     int version;
2607     int flags;
2608
2609     if (c->fc->nb_streams < 1)
2610         return 0;
2611     st = c->fc->streams[c->fc->nb_streams-1];
2612     sc = st->priv_data;
2613
2614     version = avio_r8(pb);
2615     flags = avio_rb24(pb);
2616     st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
2617
2618     if (version == 1) {
2619         avio_rb64(pb);
2620         avio_rb64(pb);
2621     } else {
2622         avio_rb32(pb); /* creation time */
2623         avio_rb32(pb); /* modification time */
2624     }
2625     st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2626     avio_rb32(pb); /* reserved */
2627
2628     /* highlevel (considering edits) duration in movie timebase */
2629     (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2630     avio_rb32(pb); /* reserved */
2631     avio_rb32(pb); /* reserved */
2632
2633     avio_rb16(pb); /* layer */
2634     avio_rb16(pb); /* alternate group */
2635     avio_rb16(pb); /* volume */
2636     avio_rb16(pb); /* reserved */
2637
2638     //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2639     // they're kept in fixed point format through all calculations
2640     // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2641     // side data, but the scale factor is not needed to calculate aspect ratio
2642     for (i = 0; i < 3; i++) {
2643         display_matrix[i][0] = avio_rb32(pb);   // 16.16 fixed point
2644         display_matrix[i][1] = avio_rb32(pb);   // 16.16 fixed point
2645         display_matrix[i][2] = avio_rb32(pb);   //  2.30 fixed point
2646     }
2647
2648     width = avio_rb32(pb);       // 16.16 fixed point track width
2649     height = avio_rb32(pb);      // 16.16 fixed point track height
2650     sc->width = width >> 16;
2651     sc->height = height >> 16;
2652
2653     // save the matrix and add rotate metadata when it is not the default
2654     // identity
2655     if (display_matrix[0][0] != (1 << 16) ||
2656         display_matrix[1][1] != (1 << 16) ||
2657         display_matrix[2][2] != (1 << 30) ||
2658         display_matrix[0][1] || display_matrix[0][2] ||
2659         display_matrix[1][0] || display_matrix[1][2] ||
2660         display_matrix[2][0] || display_matrix[2][1]) {
2661         int i, j;
2662         double rotate;
2663
2664         av_freep(&sc->display_matrix);
2665         sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2666         if (!sc->display_matrix)
2667             return AVERROR(ENOMEM);
2668
2669         for (i = 0; i < 3; i++)
2670             for (j = 0; j < 3; j++)
2671                 sc->display_matrix[i * 3 + j] = display_matrix[j][i];
2672
2673         rotate = av_display_rotation_get(sc->display_matrix);
2674         if (!isnan(rotate)) {
2675             char rotate_buf[64];
2676             rotate = -rotate;
2677             if (rotate < 0) // for backward compatibility
2678                 rotate += 360;
2679             snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
2680             av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
2681         }
2682     }
2683
2684     // transform the display width/height according to the matrix
2685     // skip this if the display matrix is the default identity matrix
2686     // or if it is rotating the picture, ex iPhone 3GS
2687     // to keep the same scale, use [width height 1<<16]
2688     if (width && height &&
2689         ((display_matrix[0][0] != 65536  ||
2690           display_matrix[1][1] != 65536) &&
2691          !display_matrix[0][1] &&
2692          !display_matrix[1][0] &&
2693          !display_matrix[2][0] && !display_matrix[2][1])) {
2694         for (i = 0; i < 2; i++)
2695             disp_transform[i] =
2696                 (int64_t)  width  * display_matrix[0][i] +
2697                 (int64_t)  height * display_matrix[1][i] +
2698                 ((int64_t) display_matrix[2][i] << 16);
2699
2700         //sample aspect ratio is new width/height divided by old width/height
2701         st->sample_aspect_ratio = av_d2q(
2702             ((double) disp_transform[0] * height) /
2703             ((double) disp_transform[1] * width), INT_MAX);
2704     }
2705     return 0;
2706 }
2707
2708 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2709 {
2710     MOVFragment *frag = &c->fragment;
2711     MOVTrackExt *trex = NULL;
2712     int flags, track_id, i;
2713
2714     avio_r8(pb); /* version */
2715     flags = avio_rb24(pb);
2716
2717     track_id = avio_rb32(pb);
2718     if (!track_id)
2719         return AVERROR_INVALIDDATA;
2720     frag->track_id = track_id;
2721     for (i = 0; i < c->trex_count; i++)
2722         if (c->trex_data[i].track_id == frag->track_id) {
2723             trex = &c->trex_data[i];
2724             break;
2725         }
2726     if (!trex) {
2727         av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2728         return AVERROR_INVALIDDATA;
2729     }
2730
2731     frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
2732                              avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
2733                              frag->moof_offset : frag->implicit_offset;
2734     frag->stsd_id  = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2735
2736     frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2737                      avio_rb32(pb) : trex->duration;
2738     frag->size     = flags & MOV_TFHD_DEFAULT_SIZE ?
2739                      avio_rb32(pb) : trex->size;
2740     frag->flags    = flags & MOV_TFHD_DEFAULT_FLAGS ?
2741                      avio_rb32(pb) : trex->flags;
2742     av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2743     return 0;
2744 }
2745
2746 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2747 {
2748     c->chapter_track = avio_rb32(pb);
2749     return 0;
2750 }
2751
2752 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2753 {
2754     MOVTrackExt *trex;
2755     int err;
2756
2757     if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2758         return AVERROR_INVALIDDATA;
2759     if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2760                                  sizeof(*c->trex_data))) < 0) {
2761         c->trex_count = 0;
2762         return err;
2763     }
2764
2765     c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
2766
2767     trex = &c->trex_data[c->trex_count++];
2768     avio_r8(pb); /* version */
2769     avio_rb24(pb); /* flags */
2770     trex->track_id = avio_rb32(pb);
2771     trex->stsd_id  = avio_rb32(pb);
2772     trex->duration = avio_rb32(pb);
2773     trex->size     = avio_rb32(pb);
2774     trex->flags    = avio_rb32(pb);
2775     return 0;
2776 }
2777
2778 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2779 {
2780     MOVFragment *frag = &c->fragment;
2781     AVStream *st = NULL;
2782     MOVStreamContext *sc;
2783     MOVStts *ctts_data;
2784     uint64_t offset;
2785     int64_t dts;
2786     int data_offset = 0;
2787     unsigned entries, first_sample_flags = frag->flags;
2788     int flags, distance, i, found_keyframe = 0, err;
2789
2790     for (i = 0; i < c->fc->nb_streams; i++) {
2791         if (c->fc->streams[i]->id == frag->track_id) {
2792             st = c->fc->streams[i];
2793             break;
2794         }
2795     }
2796     if (!st) {
2797         av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2798         return AVERROR_INVALIDDATA;
2799     }
2800     sc = st->priv_data;
2801     if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
2802         return 0;
2803     avio_r8(pb); /* version */
2804     flags = avio_rb24(pb);
2805     entries = avio_rb32(pb);
2806     av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2807
2808     /* Always assume the presence of composition time offsets.
2809      * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2810      *  1) in the initial movie, there are no samples.
2811      *  2) in the first movie fragment, there is only one sample without composition time offset.
2812      *  3) in the subsequent movie fragments, there are samples with composition time offset. */
2813     if (!sc->ctts_count && sc->sample_count)
2814     {
2815         /* Complement ctts table if moov atom doesn't have ctts atom. */
2816         ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2817         if (!ctts_data)
2818             return AVERROR(ENOMEM);
2819         sc->ctts_data = ctts_data;
2820         sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2821         sc->ctts_data[sc->ctts_count].duration = 0;
2822         sc->ctts_count++;
2823     }
2824     if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2825         return AVERROR_INVALIDDATA;
2826     if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2827                                  sizeof(*sc->ctts_data))) < 0) {
2828         sc->ctts_count = 0;
2829         return err;
2830     }
2831     if (flags & MOV_TRUN_DATA_OFFSET)        data_offset        = avio_rb32(pb);
2832     if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
2833     dts    = sc->track_end - sc->time_offset;
2834     offset = frag->base_data_offset + data_offset;
2835     distance = 0;
2836     av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
2837     for (i = 0; i < entries && !pb->eof_reached; i++) {
2838         unsigned sample_size = frag->size;
2839         int sample_flags = i ? frag->flags : first_sample_flags;
2840         unsigned sample_duration = frag->duration;
2841         int keyframe = 0;
2842
2843         if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2844         if (flags & MOV_TRUN_SAMPLE_SIZE)     sample_size     = avio_rb32(pb);
2845         if (flags & MOV_TRUN_SAMPLE_FLAGS)    sample_flags    = avio_rb32(pb);
2846         sc->ctts_data[sc->ctts_count].count = 1;
2847         sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2848                                                   avio_rb32(pb) : 0;
2849         mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
2850         sc->ctts_count++;
2851         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2852             keyframe = 1;
2853         else if (!found_keyframe)
2854             keyframe = found_keyframe =
2855                 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2856                                   MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
2857         if (keyframe)
2858             distance = 0;
2859         av_add_index_entry(st, offset, dts, sample_size, distance,
2860                            keyframe ? AVINDEX_KEYFRAME : 0);
2861         av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2862                 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
2863                 offset, dts, sample_size, distance, keyframe);
2864         distance++;
2865         dts += sample_duration;
2866         offset += sample_size;
2867         sc->data_size += sample_size;
2868         sc->duration_for_fps += sample_duration;
2869         sc->nb_frames_for_fps ++;
2870     }
2871
2872     if (pb->eof_reached)
2873         return AVERROR_EOF;
2874
2875     frag->implicit_offset = offset;
2876     st->duration = sc->track_end = dts + sc->time_offset;
2877     return 0;
2878 }
2879
2880 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2881 /* like the files created with Adobe Premiere 5.0, for samples see */
2882 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
2883 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2884 {
2885     int err;
2886
2887     if (atom.size < 8)
2888         return 0; /* continue */
2889     if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
2890         avio_skip(pb, atom.size - 4);
2891         return 0;
2892     }
2893     atom.type = avio_rl32(pb);
2894     atom.size -= 8;
2895     if (atom.type != MKTAG('m','d','a','t')) {
2896         avio_skip(pb, atom.size);
2897         return 0;
2898     }
2899     err = mov_read_mdat(c, pb, atom);
2900     return err;
2901 }
2902
2903 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2904 {
2905 #if CONFIG_ZLIB
2906     AVIOContext ctx;
2907     uint8_t *cmov_data;
2908     uint8_t *moov_data; /* uncompressed data */
2909     long cmov_len, moov_len;
2910     int ret = -1;
2911
2912     avio_rb32(pb); /* dcom atom */
2913     if (avio_rl32(pb) != MKTAG('d','c','o','m'))
2914         return AVERROR_INVALIDDATA;
2915     if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
2916         av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
2917         return AVERROR_INVALIDDATA;
2918     }
2919     avio_rb32(pb); /* cmvd atom */
2920     if (avio_rl32(pb) != MKTAG('c','m','v','d'))
2921         return AVERROR_INVALIDDATA;
2922     moov_len = avio_rb32(pb); /* uncompressed size */
2923     cmov_len = atom.size - 6 * 4;
2924
2925     cmov_data = av_malloc(cmov_len);
2926     if (!cmov_data)
2927         return AVERROR(ENOMEM);
2928     moov_data = av_malloc(moov_len);
2929     if (!moov_data) {
2930         av_free(cmov_data);
2931         return AVERROR(ENOMEM);
2932     }
2933     avio_read(pb, cmov_data, cmov_len);
2934     if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
2935         goto free_and_return;
2936     if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
2937         goto free_and_return;
2938     atom.type = MKTAG('m','o','o','v');
2939     atom.size = moov_len;
2940     ret = mov_read_default(c, &ctx, atom);
2941 free_and_return:
2942     av_free(moov_data);
2943     av_free(cmov_data);
2944     return ret;
2945 #else
2946     av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
2947     return AVERROR(ENOSYS);
2948 #endif
2949 }
2950
2951 /* edit list atom */
2952 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2953 {
2954     MOVStreamContext *sc;
2955     int i, edit_count, version, edit_start_index = 0;
2956     int unsupported = 0;
2957
2958     if (c->fc->nb_streams < 1 || c->ignore_editlist)
2959         return 0;
2960     sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
2961
2962     version = avio_r8(pb); /* version */
2963     avio_rb24(pb); /* flags */
2964     edit_count = avio_rb32(pb); /* entries */
2965
2966     if ((uint64_t)edit_count*12+8 > atom.size)
2967         return AVERROR_INVALIDDATA;
2968
2969     av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
2970     for (i=0; i<edit_count; i++){
2971         int64_t time;
2972         int64_t duration;
2973         int rate;
2974         if (version == 1) {
2975             duration = avio_rb64(pb);
2976             time     = avio_rb64(pb);
2977         } else {
2978             duration = avio_rb32(pb); /* segment duration */
2979             time     = (int32_t)avio_rb32(pb); /* media time */
2980         }
2981         rate = avio_rb32(pb);
2982         if (i == 0 && time == -1) {
2983             sc->empty_duration = duration;
2984             edit_start_index = 1;
2985         } else if (i == edit_start_index && time >= 0)
2986             sc->start_time = time;
2987         else
2988             unsupported = 1;
2989
2990         av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
2991                 duration, time, rate / 65536.0);
2992     }
2993
2994     if (unsupported)
2995         av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
2996                "a/v desync might occur, patch welcome\n");
2997
2998     return 0;
2999 }
3000
3001 static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3002 {
3003     MOVStreamContext *sc;
3004
3005     if (c->fc->nb_streams < 1)
3006         return AVERROR_INVALIDDATA;
3007     sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3008     sc->timecode_track = avio_rb32(pb);
3009     return 0;
3010 }
3011
3012 static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3013 {
3014     int ret;
3015     uint8_t uuid[16];
3016     static const uint8_t uuid_isml_manifest[] = {
3017         0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3018         0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3019     };
3020
3021     if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3022         return AVERROR_INVALIDDATA;
3023
3024     ret = avio_read(pb, uuid, sizeof(uuid));
3025     if (ret < 0) {
3026         return ret;
3027     } else if (ret != sizeof(uuid)) {
3028         return AVERROR_INVALIDDATA;
3029     }
3030     if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3031         uint8_t *buffer, *ptr;
3032         char *endptr;
3033         size_t len = atom.size - sizeof(uuid);
3034
3035         if (len < 4) {
3036             return AVERROR_INVALIDDATA;
3037         }
3038         ret = avio_skip(pb, 4); // zeroes
3039         len -= 4;
3040
3041         buffer = av_mallocz(len + 1);
3042         if (!buffer) {
3043             return AVERROR(ENOMEM);
3044         }
3045         ret = avio_read(pb, buffer, len);
3046         if (ret < 0) {
3047             av_free(buffer);
3048             return ret;
3049         } else if (ret != len) {
3050             av_free(buffer);
3051             return AVERROR_INVALIDDATA;
3052         }
3053
3054         ptr = buffer;
3055         while ((ptr = av_stristr(ptr, "systemBitrate=\"")) != NULL) {
3056             ptr += sizeof("systemBitrate=\"") - 1;
3057             c->bitrates_count++;
3058             c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3059             if (!c->bitrates) {
3060                 c->bitrates_count = 0;
3061                 av_free(buffer);
3062                 return AVERROR(ENOMEM);
3063             }
3064             errno = 0;
3065             ret = strtol(ptr, &endptr, 10);
3066             if (ret < 0 || errno || *endptr != '"') {
3067                 c->bitrates[c->bitrates_count - 1] = 0;
3068             } else {
3069                 c->bitrates[c->bitrates_count - 1] = ret;
3070             }
3071         }
3072
3073         av_free(buffer);
3074     }
3075     return 0;
3076 }
3077
3078 static const MOVParseTableEntry mov_default_parse_table[] = {
3079 { MKTAG('A','C','L','R'), mov_read_avid },
3080 { MKTAG('A','P','R','G'), mov_read_avid },
3081 { MKTAG('A','A','L','P'), mov_read_avid },
3082 { MKTAG('A','R','E','S'), mov_read_ares },
3083 { MKTAG('a','v','s','s'), mov_read_avss },
3084 { MKTAG('c','h','p','l'), mov_read_chpl },
3085 { MKTAG('c','o','6','4'), mov_read_stco },
3086 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3087 { MKTAG('d','i','n','f'), mov_read_default },
3088 { MKTAG('d','r','e','f'), mov_read_dref },
3089 { MKTAG('e','d','t','s'), mov_read_default },
3090 { MKTAG('e','l','s','t'), mov_read_elst },
3091 { MKTAG('e','n','d','a'), mov_read_enda },
3092 { MKTAG('f','i','e','l'), mov_read_fiel },
3093 { MKTAG('f','t','y','p'), mov_read_ftyp },
3094 { MKTAG('g','l','b','l'), mov_read_glbl },
3095 { MKTAG('h','d','l','r'), mov_read_hdlr },
3096 { MKTAG('i','l','s','t'), mov_read_ilst },
3097 { MKTAG('j','p','2','h'), mov_read_jp2h },
3098 { MKTAG('m','d','a','t'), mov_read_mdat },
3099 { MKTAG('m','d','h','d'), mov_read_mdhd },
3100 { MKTAG('m','d','i','a'), mov_read_default },
3101 { MKTAG('m','e','t','a'), mov_read_meta },
3102 { MKTAG('m','i','n','f'), mov_read_default },
3103 { MKTAG('m','o','o','f'), mov_read_moof },
3104 { MKTAG('m','o','o','v'), mov_read_moov },
3105 { MKTAG('m','v','e','x'), mov_read_default },
3106 { MKTAG('m','v','h','d'), mov_read_mvhd },
3107 { MKTAG('S','M','I',' '), mov_read_svq3 },
3108 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3109 { MKTAG('a','v','c','C'), mov_read_glbl },
3110 { MKTAG('p','a','s','p'), mov_read_pasp },
3111 { MKTAG('s','t','b','l'), mov_read_default },
3112 { MKTAG('s','t','c','o'), mov_read_stco },
3113 { MKTAG('s','t','p','s'), mov_read_stps },
3114 { MKTAG('s','t','r','f'), mov_read_strf },
3115 { MKTAG('s','t','s','c'), mov_read_stsc },
3116 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3117 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3118 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3119 { MKTAG('s','t','t','s'), mov_read_stts },
3120 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3121 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3122 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3123 { MKTAG('t','r','a','k'), mov_read_trak },
3124 { MKTAG('t','r','a','f'), mov_read_default },
3125 { MKTAG('t','r','e','f'), mov_read_default },
3126 { MKTAG('t','m','c','d'), mov_read_tmcd },
3127 { MKTAG('c','h','a','p'), mov_read_chap },
3128 { MKTAG('t','r','e','x'), mov_read_trex },
3129 { MKTAG('t','r','u','n'), mov_read_trun },
3130 { MKTAG('u','d','t','a'), mov_read_default },
3131 { MKTAG('w','a','v','e'), mov_read_wave },
3132 { MKTAG('e','s','d','s'), mov_read_esds },
3133 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3134 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3135 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3136 { MKTAG('w','f','e','x'), mov_read_wfex },
3137 { MKTAG('c','m','o','v'), mov_read_cmov },
3138 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3139 { MKTAG('d','v','c','1'), mov_read_dvc1 },
3140 { MKTAG('s','b','g','p'), mov_read_sbgp },
3141 { MKTAG('h','v','c','C'), mov_read_glbl },
3142 { MKTAG('u','u','i','d'), mov_read_uuid },
3143 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
3144 { MKTAG('-','-','-','-'), mov_read_custom },
3145 { 0, NULL }
3146 };
3147
3148 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3149 {
3150     int64_t total_size = 0;
3151     MOVAtom a;
3152     int i;
3153
3154     if (atom.size < 0)
3155         atom.size = INT64_MAX;
3156     while (total_size + 8 <= atom.size && !url_feof(pb)) {
3157         int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3158         a.size = atom.size;
3159         a.type=0;
3160         if (atom.size >= 8) {
3161             a.size = avio_rb32(pb);
3162             a.type = avio_rl32(pb);
3163             if (a.type == MKTAG('f','r','e','e') &&
3164                 a.size >= 8 &&
3165                 c->moov_retry) {
3166                 uint8_t buf[8];
3167                 uint32_t *type = (uint32_t *)buf + 1;
3168                 avio_read(pb, buf, 8);
3169                 avio_seek(pb, -8, SEEK_CUR);
3170                 if (*type == MKTAG('m','v','h','d') ||
3171                     *type == MKTAG('c','m','o','v')) {
3172                     av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
3173                     a.type = MKTAG('m','o','o','v');
3174                 }
3175             }
3176             if (atom.type != MKTAG('r','o','o','t') &&
3177                 atom.type != MKTAG('m','o','o','v'))
3178             {
3179                 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
3180                 {
3181                     av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
3182                     avio_skip(pb, -8);
3183                     return 0;
3184                 }
3185             }
3186             total_size += 8;
3187             if (a.size == 1) { /* 64 bit extended size */
3188                 a.size = avio_rb64(pb) - 8;
3189                 total_size += 8;
3190             }
3191         }
3192         av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3193                 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3194         if (a.size == 0) {
3195             a.size = atom.size - total_size + 8;
3196         }
3197         a.size -= 8;
3198         if (a.size < 0)
3199             break;
3200         a.size = FFMIN(a.size, atom.size - total_size);
3201
3202         for (i = 0; mov_default_parse_table[i].type; i++)
3203             if (mov_default_parse_table[i].type == a.type) {
3204                 parse = mov_default_parse_table[i].parse;
3205                 break;
3206             }
3207
3208         // container is user data
3209         if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3210                        atom.type == MKTAG('i','l','s','t')))
3211             parse = mov_read_udta_string;
3212
3213         if (!parse) { /* skip leaf atoms data */
3214             avio_skip(pb, a.size);
3215         } else {
3216             int64_t start_pos = avio_tell(pb);
3217             int64_t left;
3218             int err = parse(c, pb, a);
3219             if (err < 0)
3220                 return err;
3221             if (c->found_moov && c->found_mdat &&
3222                 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3223                  start_pos + a.size == avio_size(pb))) {
3224                 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3225                     c->next_root_atom = start_pos + a.size;
3226                 return 0;
3227             }
3228             left = a.size - avio_tell(pb) + start_pos;
3229             if (left > 0) /* skip garbage at atom end */
3230                 avio_skip(pb, left);
3231             else if (left < 0) {
3232                 av_log(c->fc, AV_LOG_WARNING,
3233                        "overread end of atom '%.4s' by %"PRId64" bytes\n",
3234                        (char*)&a.type, -left);
3235                 avio_seek(pb, left, SEEK_CUR);
3236             }
3237         }
3238
3239         total_size += a.size;
3240     }
3241
3242     if (total_size < atom.size && atom.size < 0x7ffff)
3243         avio_skip(pb, atom.size - total_size);
3244
3245     return 0;
3246 }
3247
3248 static int mov_probe(AVProbeData *p)
3249 {
3250     int64_t offset;
3251     uint32_t tag;
3252     int score = 0;
3253     int moov_offset = -1;
3254
3255     /* check file header */
3256     offset = 0;
3257     for (;;) {
3258         /* ignore invalid offset */
3259         if ((offset + 8) > (unsigned int)p->buf_size)
3260             break;
3261         tag = AV_RL32(p->buf + offset + 4);
3262         switch(tag) {
3263         /* check for obvious tags */
3264         case MKTAG('m','o','o','v'):
3265             moov_offset = offset + 4;
3266         case MKTAG('m','d','a','t'):
3267         case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3268         case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3269         case MKTAG('f','t','y','p'):
3270             if (AV_RB32(p->buf+offset) < 8 &&
3271                 (AV_RB32(p->buf+offset) != 1 ||
3272                  offset + 12 > (unsigned int)p->buf_size ||
3273                  AV_RB64(p->buf+offset + 8) == 0)) {
3274                 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3275             } else if (tag == MKTAG('f','t','y','p') &&
3276                        AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')) {
3277                 score = FFMAX(score, 5);
3278             } else {
3279                 score = AVPROBE_SCORE_MAX;
3280             }
3281             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3282             break;
3283         /* those are more common words, so rate then a bit less */
3284         case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3285         case MKTAG('w','i','d','e'):
3286         case MKTAG('f','r','e','e'):
3287         case MKTAG('j','u','n','k'):
3288         case MKTAG('p','i','c','t'):
3289             score  = FFMAX(score, AVPROBE_SCORE_MAX - 5);
3290             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3291             break;
3292         case MKTAG(0x82,0x82,0x7f,0x7d):
3293         case MKTAG('s','k','i','p'):
3294         case MKTAG('u','u','i','d'):
3295         case MKTAG('p','r','f','l'):
3296             /* if we only find those cause probedata is too small at least rate them */
3297             score  = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3298             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3299             break;
3300         default:
3301             offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3302         }
3303     }
3304     if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
3305         /* moov atom in the header - we should make sure that this is not a
3306          * MOV-packed MPEG-PS */
3307         offset = moov_offset;
3308
3309         while(offset < (p->buf_size - 16)){ /* Sufficient space */
3310                /* We found an actual hdlr atom */
3311             if(AV_RL32(p->buf + offset     ) == MKTAG('h','d','l','r') &&
3312                AV_RL32(p->buf + offset +  8) == MKTAG('m','h','l','r') &&
3313                AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
3314                 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
3315                 /* We found a media handler reference atom describing an
3316                  * MPEG-PS-in-MOV, return a
3317                  * low score to force expanding the probe window until
3318                  * mpegps_probe finds what it needs */
3319                 return 5;
3320             }else
3321                 /* Keep looking */
3322                 offset+=2;
3323         }
3324     }
3325
3326     return score;
3327 }
3328
3329 // must be done after parsing all trak because there's no order requirement
3330 static void mov_read_chapters(AVFormatContext *s)
3331 {
3332     MOVContext *mov = s->priv_data;
3333     AVStream *st = NULL;
3334     MOVStreamContext *sc;
3335     int64_t cur_pos;
3336     int i;
3337
3338     for (i = 0; i < s->nb_streams; i++)
3339         if (s->streams[i]->id == mov->chapter_track) {
3340             st = s->streams[i];
3341             break;
3342         }
3343     if (!st) {
3344         av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3345         return;
3346     }
3347
3348     st->discard = AVDISCARD_ALL;
3349     sc = st->priv_data;
3350     cur_pos = avio_tell(sc->pb);
3351
3352     for (i = 0; i < st->nb_index_entries; i++) {
3353         AVIndexEntry *sample = &st->index_entries[i];
3354         int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3355         uint8_t *title;
3356         uint16_t ch;
3357         int len, title_len;
3358
3359         if (end < sample->timestamp) {
3360             av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
3361             end = AV_NOPTS_VALUE;
3362         }
3363
3364         if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3365             av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3366             goto finish;
3367         }
3368
3369         // the first two bytes are the length of the title
3370         len = avio_rb16(sc->pb);
3371         if (len > sample->size-2)
3372             continue;
3373         title_len = 2*len + 1;
3374         if (!(title = av_mallocz(title_len)))
3375             goto finish;
3376
3377         // The samples could theoretically be in any encoding if there's an encd
3378         // atom following, but in practice are only utf-8 or utf-16, distinguished
3379         // instead by the presence of a BOM
3380         if (!len) {
3381             title[0] = 0;
3382         } else {
3383             ch = avio_rb16(sc->pb);
3384             if (ch == 0xfeff)
3385                 avio_get_str16be(sc->pb, len, title, title_len);
3386             else if (ch == 0xfffe)
3387                 avio_get_str16le(sc->pb, len, title, title_len);
3388             else {
3389                 AV_WB16(title, ch);
3390                 if (len == 1 || len == 2)
3391                     title[len] = 0;
3392                 else
3393                     avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
3394             }
3395         }
3396
3397         avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3398         av_freep(&title);
3399     }
3400 finish:
3401     avio_seek(sc->pb, cur_pos, SEEK_SET);
3402 }
3403
3404 static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
3405                                              uint32_t value, int flags)
3406 {
3407     AVTimecode tc;
3408     char buf[AV_TIMECODE_STR_SIZE];
3409     AVRational rate = {st->codec->time_base.den,
3410                        st->codec->time_base.num};
3411     int ret = av_timecode_init(&tc, rate, flags, 0, s);
3412     if (ret < 0)
3413         return ret;
3414     av_dict_set(&st->metadata, "timecode",
3415                 av_timecode_make_string(&tc, buf, value), 0);
3416     return 0;
3417 }
3418
3419 static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
3420 {
3421     MOVStreamContext *sc = st->priv_data;
3422     int flags = 0;
3423     int64_t cur_pos = avio_tell(sc->pb);
3424     uint32_t value;
3425
3426     if (!st->nb_index_entries)
3427         return -1;
3428
3429     avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
3430     value = avio_rb32(s->pb);
3431
3432     if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
3433     if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
3434     if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
3435
3436     /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
3437      * not the case) and thus assume "frame number format" instead of QT one.
3438      * No sample with tmcd track can be found with a QT timecode at the moment,
3439      * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
3440      * format). */
3441     parse_timecode_in_framenum_format(s, st, value, flags);
3442
3443     avio_seek(sc->pb, cur_pos, SEEK_SET);
3444     return 0;
3445 }
3446
3447 static int mov_read_close(AVFormatContext *s)
3448 {
3449     MOVContext *mov = s->priv_data;
3450     int i, j;
3451
3452     for (i = 0; i < s->nb_streams; i++) {
3453         AVStream *st = s->streams[i];
3454         MOVStreamContext *sc = st->priv_data;
3455
3456         av_freep(&sc->ctts_data);
3457         for (j = 0; j < sc->drefs_count; j++) {
3458             av_freep(&sc->drefs[j].path);
3459             av_freep(&sc->drefs[j].dir);
3460         }
3461         av_freep(&sc->drefs);
3462
3463         sc->drefs_count = 0;
3464
3465         if (!sc->pb_is_copied)
3466             avio_close(sc->pb);
3467
3468         sc->pb = NULL;
3469         av_freep(&sc->chunk_offsets);
3470         av_freep(&sc->stsc_data);
3471         av_freep(&sc->sample_sizes);
3472         av_freep(&sc->keyframes);
3473         av_freep(&sc->stts_data);
3474         av_freep(&sc->stps_data);
3475         av_freep(&sc->rap_group);
3476         av_freep(&sc->display_matrix);
3477     }
3478
3479     if (mov->dv_demux) {
3480         for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
3481             av_freep(&mov->dv_fctx->streams[i]->codec);
3482             av_freep(&mov->dv_fctx->streams[i]);
3483         }
3484         av_freep(&mov->dv_fctx);
3485         av_freep(&mov->dv_demux);
3486     }
3487
3488     av_freep(&mov->trex_data);
3489     av_freep(&mov->bitrates);
3490
3491     return 0;
3492 }
3493
3494 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
3495 {
3496     int i;
3497
3498     for (i = 0; i < s->nb_streams; i++) {
3499         AVStream *st = s->streams[i];
3500         MOVStreamContext *sc = st->priv_data;
3501
3502         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3503             sc->timecode_track == tmcd_id)
3504             return 1;
3505     }
3506     return 0;
3507 }
3508
3509 /* look for a tmcd track not referenced by any video track, and export it globally */
3510 static void export_orphan_timecode(AVFormatContext *s)
3511 {
3512     int i;
3513
3514     for (i = 0; i < s->nb_streams; i++) {
3515         AVStream *st = s->streams[i];
3516
3517         if (st->codec->codec_tag  == MKTAG('t','m','c','d') &&
3518             !tmcd_is_referenced(s, i + 1)) {
3519             AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
3520             if (tcr) {
3521                 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
3522                 break;
3523             }
3524         }
3525     }
3526 }
3527
3528 static int mov_read_header(AVFormatContext *s)
3529 {
3530     MOVContext *mov = s->priv_data;
3531     AVIOContext *pb = s->pb;
3532     int j, err;
3533     MOVAtom atom = { AV_RL32("root") };
3534     int i;
3535
3536     mov->fc = s;
3537     /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3538     if (pb->seekable)
3539         atom.size = avio_size(pb);
3540     else
3541         atom.size = INT64_MAX;
3542
3543     /* check MOV header */
3544     do {
3545     if (mov->moov_retry)
3546         avio_seek(pb, 0, SEEK_SET);
3547     if ((err = mov_read_default(mov, pb, atom)) < 0) {
3548         av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3549         mov_read_close(s);
3550         return err;
3551     }
3552     } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
3553     if (!mov->found_moov) {
3554         av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3555         mov_read_close(s);
3556         return AVERROR_INVALIDDATA;
3557     }
3558     av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3559
3560     if (pb->seekable) {
3561         if (mov->chapter_track > 0)
3562             mov_read_chapters(s);
3563         for (i = 0; i < s->nb_streams; i++)
3564             if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
3565                 mov_read_timecode_track(s, s->streams[i]);
3566     }
3567
3568     /* copy timecode metadata from tmcd tracks to the related video streams */
3569     for (i = 0; i < s->nb_streams; i++) {
3570         AVStream *st = s->streams[i];
3571         MOVStreamContext *sc = st->priv_data;
3572         if (sc->timecode_track > 0) {
3573             AVDictionaryEntry *tcr;
3574             int tmcd_st_id = -1;
3575
3576             for (j = 0; j < s->nb_streams; j++)
3577                 if (s->streams[j]->id == sc->timecode_track)
3578                     tmcd_st_id = j;
3579
3580             if (tmcd_st_id < 0 || tmcd_st_id == i)
3581                 continue;
3582             tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
3583             if (tcr)
3584                 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
3585         }
3586     }
3587     export_orphan_timecode(s);
3588
3589     for (i = 0; i < s->nb_streams; i++) {
3590         AVStream *st = s->streams[i];
3591         MOVStreamContext *sc = st->priv_data;
3592         fix_timescale(mov, sc);
3593         if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
3594             st->skip_samples = sc->start_pad;
3595         }
3596         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
3597             av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3598                       sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
3599         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3600             if (st->codec->width <= 0 && st->codec->height <= 0) {
3601                 st->codec->width  = sc->width;
3602                 st->codec->height = sc->height;
3603             }
3604             if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
3605                 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
3606                     return err;
3607             }
3608         }
3609     }
3610
3611     if (mov->trex_data) {
3612         for (i = 0; i < s->nb_streams; i++) {
3613             AVStream *st = s->streams[i];
3614             MOVStreamContext *sc = st->priv_data;
3615             if (st->duration > 0)
3616                 st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3617         }
3618     }
3619
3620     for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
3621         if (mov->bitrates[i]) {
3622             s->streams[i]->codec->bit_rate = mov->bitrates[i];
3623         }
3624     }
3625
3626     ff_rfps_calculate(s);
3627
3628     for (i = 0; i < s->nb_streams; i++) {
3629         AVStream *st = s->streams[i];
3630         MOVStreamContext *sc = st->priv_data;
3631
3632         switch (st->codec->codec_type) {
3633         case AVMEDIA_TYPE_AUDIO:
3634             err = ff_replaygain_export(st, s->metadata);
3635             if (err < 0) {
3636                 mov_read_close(s);
3637                 return err;
3638             }
3639             break;
3640         case AVMEDIA_TYPE_VIDEO:
3641             if (sc->display_matrix) {
3642                 AVPacketSideData *sd, *tmp;
3643
3644                 tmp = av_realloc_array(st->side_data,
3645                                        st->nb_side_data + 1, sizeof(*tmp));
3646                 if (!tmp)
3647                     return AVERROR(ENOMEM);
3648
3649                 st->side_data = tmp;
3650                 st->nb_side_data++;
3651
3652                 sd = &st->side_data[st->nb_side_data - 1];
3653                 sd->type = AV_PKT_DATA_DISPLAYMATRIX;
3654                 sd->size = sizeof(int32_t) * 9;
3655                 sd->data = (uint8_t*)sc->display_matrix;
3656                 sc->display_matrix = NULL;
3657             }
3658             break;
3659         }
3660     }
3661
3662     return 0;
3663 }
3664
3665 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
3666 {
3667     AVIndexEntry *sample = NULL;
3668     int64_t best_dts = INT64_MAX;
3669     int i;
3670     for (i = 0; i < s->nb_streams; i++) {
3671         AVStream *avst = s->streams[i];
3672         MOVStreamContext *msc = avst->priv_data;
3673         if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3674             AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3675             int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3676             av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3677             if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3678                 (s->pb->seekable &&
3679                  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3680                  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3681                   (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3682                 sample = current_sample;
3683                 best_dts = dts;
3684                 *st = avst;
3685             }
3686         }
3687     }
3688     return sample;
3689 }
3690
3691 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
3692 {
3693     MOVContext *mov = s->priv_data;
3694     MOVStreamContext *sc;
3695     AVIndexEntry *sample;
3696     AVStream *st = NULL;
3697     int ret;
3698     mov->fc = s;
3699  retry:
3700     sample = mov_find_next_sample(s, &st);
3701     if (!sample) {
3702         mov->found_mdat = 0;
3703         if (!mov->next_root_atom)
3704             return AVERROR_EOF;
3705         avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3706         mov->next_root_atom = 0;
3707         if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3708             url_feof(s->pb))
3709             return AVERROR_EOF;
3710         av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3711         goto retry;
3712     }
3713     sc = st->priv_data;
3714     /* must be done just before reading, to avoid infinite loop on sample */
3715     sc->current_sample++;
3716
3717     if (mov->next_root_atom) {
3718         sample->pos = FFMIN(sample->pos, mov->next_root_atom);
3719         sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
3720     }
3721
3722     if (st->discard != AVDISCARD_ALL) {
3723         if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3724             av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3725                    sc->ffindex, sample->pos);
3726             return AVERROR_INVALIDDATA;
3727         }
3728         ret = av_get_packet(sc->pb, pkt, sample->size);
3729         if (ret < 0)
3730             return ret;
3731         if (sc->has_palette) {
3732             uint8_t *pal;
3733
3734             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
3735             if (!pal) {
3736                 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3737             } else {
3738                 memcpy(pal, sc->palette, AVPALETTE_SIZE);
3739                 sc->has_palette = 0;
3740             }
3741         }
3742 #if CONFIG_DV_DEMUXER
3743         if (mov->dv_demux && sc->dv_audio_container) {
3744             avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
3745             av_free(pkt->data);
3746             pkt->size = 0;
3747             ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3748             if (ret < 0)
3749                 return ret;
3750         }
3751 #endif
3752     }
3753
3754     pkt->stream_index = sc->ffindex;
3755     pkt->dts = sample->timestamp;
3756     if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3757         pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3758         /* update ctts context */
3759         sc->ctts_sample++;
3760         if (sc->ctts_index < sc->ctts_count &&
3761             sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3762             sc->ctts_index++;
3763             sc->ctts_sample = 0;
3764         }
3765         if (sc->wrong_dts)
3766             pkt->dts = AV_NOPTS_VALUE;
3767     } else {
3768         int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3769             st->index_entries[sc->current_sample].timestamp : st->duration;
3770         pkt->duration = next_dts - pkt->dts;
3771         pkt->pts = pkt->dts;
3772     }
3773     if (st->discard == AVDISCARD_ALL)
3774         goto retry;
3775     pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3776     pkt->pos = sample->pos;
3777     av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
3778             pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3779     return 0;
3780 }
3781
3782 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3783 {
3784     MOVStreamContext *sc = st->priv_data;
3785     int sample, time_sample;
3786     int i;
3787
3788     sample = av_index_search_timestamp(st, timestamp, flags);
3789     av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3790     if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3791         sample = 0;
3792     if (sample < 0) /* not sure what to do */
3793         return AVERROR_INVALIDDATA;
3794     sc->current_sample = sample;
3795     av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
3796     /* adjust ctts index */
3797     if (sc->ctts_data) {
3798         time_sample = 0;
3799         for (i = 0; i < sc->ctts_count; i++) {
3800             int next = time_sample + sc->ctts_data[i].count;
3801             if (next > sc->current_sample) {
3802                 sc->ctts_index = i;
3803                 sc->ctts_sample = sc->current_sample - time_sample;
3804                 break;
3805             }
3806             time_sample = next;
3807         }
3808     }
3809     return sample;
3810 }
3811
3812 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3813 {
3814     AVStream *st;
3815     int64_t seek_timestamp, timestamp;
3816     int sample;
3817     int i;
3818
3819     if (stream_index >= s->nb_streams)
3820         return AVERROR_INVALIDDATA;
3821
3822     st = s->streams[stream_index];
3823     sample = mov_seek_stream(s, st, sample_time, flags);
3824     if (sample < 0)
3825         return sample;
3826
3827     /* adjust seek timestamp to found sample timestamp */
3828     seek_timestamp = st->index_entries[sample].timestamp;
3829
3830     for (i = 0; i < s->nb_streams; i++) {
3831         MOVStreamContext *sc = s->streams[i]->priv_data;
3832         st = s->streams[i];
3833         st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
3834
3835         if (stream_index == i)
3836             continue;
3837
3838         timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
3839         mov_seek_stream(s, st, timestamp, flags);
3840     }
3841     return 0;
3842 }
3843
3844 static const AVOption options[] = {
3845     {"use_absolute_path",
3846         "allow using absolute path when opening alias, this is a possible security issue",
3847         offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
3848         0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
3849     {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
3850         0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
3851     {NULL}
3852 };
3853
3854 static const AVClass mov_class = {
3855     .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
3856     .item_name  = av_default_item_name,
3857     .option     = options,
3858     .version    = LIBAVUTIL_VERSION_INT,
3859 };
3860
3861 AVInputFormat ff_mov_demuxer = {
3862     .name           = "mov,mp4,m4a,3gp,3g2,mj2",
3863     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3864     .priv_data_size = sizeof(MOVContext),
3865     .extensions     = "mov,mp4,m4a,3gp,3g2,mj2",
3866     .read_probe     = mov_probe,
3867     .read_header    = mov_read_header,
3868     .read_packet    = mov_read_packet,
3869     .read_close     = mov_read_close,
3870     .read_seek      = mov_read_seek,
3871     .priv_class     = &mov_class,
3872     .flags          = AVFMT_NO_BYTE_SEEK,
3873 };