3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6 * first version by Francois Revol <revol@free.fr>
7 * seek function by Gael Chardon <gael.dev@4now.net>
9 * This file is part of FFmpeg.
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.
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.
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
30 //#define MOV_EXPORT_ALL_METADATA
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"
45 #include "avio_internal.h"
48 #include "libavcodec/get_bits.h"
51 #include "replaygain.h"
57 #include "qtpalette.h"
63 /* those functions parse an atom */
64 /* links atom IDs to parse functions */
65 typedef struct MOVParseTableEntry {
67 int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
70 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
71 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
73 static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
74 unsigned len, const char *key)
78 short current, total = 0;
79 avio_rb16(pb); // unknown
80 current = avio_rb16(pb);
82 total = avio_rb16(pb);
84 snprintf(buf, sizeof(buf), "%d", current);
86 snprintf(buf, sizeof(buf), "%d/%d", current, total);
87 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
88 av_dict_set(&c->fc->metadata, key, buf, 0);
93 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
94 unsigned len, const char *key)
96 /* bypass padding bytes */
101 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
102 av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
107 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
108 unsigned len, const char *key)
110 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
111 av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
116 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
117 unsigned len, const char *key)
121 avio_r8(pb); // unknown
124 if (genre < 1 || genre > ID3v1_GENRE_MAX)
126 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
127 av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
132 static const uint32_t mac_to_unicode[128] = {
133 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
134 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
135 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
136 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
137 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
138 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
139 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
140 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
141 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
142 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
143 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
144 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
145 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
146 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
147 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
148 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
151 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
152 char *dst, int dstlen)
155 char *end = dst+dstlen-1;
158 for (i = 0; i < len; i++) {
159 uint8_t t, c = avio_r8(pb);
160 if (c < 0x80 && p < end)
163 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
169 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
173 MOVStreamContext *sc;
178 case 0xd: id = AV_CODEC_ID_MJPEG; break;
179 case 0xe: id = AV_CODEC_ID_PNG; break;
180 case 0x1b: id = AV_CODEC_ID_BMP; break;
182 av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
187 st = avformat_new_stream(c->fc, NULL);
189 return AVERROR(ENOMEM);
190 sc = av_mallocz(sizeof(*sc));
192 return AVERROR(ENOMEM);
195 ret = av_get_packet(pb, &pkt, len);
199 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
201 st->attached_pic = pkt;
202 st->attached_pic.stream_index = st->index;
203 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
205 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
206 st->codec->codec_id = id;
211 static int mov_metadata_raw(MOVContext *c, AVIOContext *pb,
212 unsigned len, const char *key)
214 char *value = av_malloc(len + 1);
216 return AVERROR(ENOMEM);
217 avio_read(pb, value, len);
219 return av_dict_set(&c->fc->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
222 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
224 char language[4] = { 0 };
226 uint16_t langcode = 0;
227 double longitude, latitude;
228 const char *key = "location";
230 if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4)
231 return AVERROR_INVALIDDATA;
233 avio_skip(pb, 4); // version+flags
234 langcode = avio_rb16(pb);
235 ff_mov_lang_to_iso639(langcode, language);
238 len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name
240 return AVERROR_INVALIDDATA;
241 avio_skip(pb, 1); // role
245 return AVERROR_INVALIDDATA;
246 longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
247 latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
249 // Try to output in the same format as the ?xyz field
250 snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude);
251 if (*language && strcmp(language, "und")) {
253 snprintf(key2, sizeof(key2), "%s-%s", key, language);
254 av_dict_set(&c->fc->metadata, key2, buf, 0);
256 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
257 return av_dict_set(&c->fc->metadata, key, buf, 0);
260 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
262 #ifdef MOV_EXPORT_ALL_METADATA
265 char str[1024], key2[16], language[4] = {0};
266 const char *key = NULL;
267 uint16_t langcode = 0;
268 uint32_t data_type = 0, str_size;
269 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
272 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
273 case MKTAG(0xa9,'a','u','t'):
274 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
275 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
276 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
277 case MKTAG( 'c','p','i','l'): key = "compilation";
278 parse = mov_metadata_int8_no_padding; break;
279 case MKTAG( 'c','p','r','t'):
280 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
281 case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
282 case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
283 case MKTAG(0xa9,'c','m','t'):
284 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
285 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
286 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
287 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
288 case MKTAG( 'g','n','r','e'): key = "genre";
289 parse = mov_metadata_gnre; break;
290 case MKTAG(0xa9,'t','o','o'):
291 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
292 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
293 case MKTAG(0xa9,'m','a','k'): key = "make"; break;
294 case MKTAG(0xa9,'m','o','d'): key = "model"; break;
295 case MKTAG(0xa9,'x','y','z'): key = "location"; break;
296 case MKTAG( 'd','e','s','c'): key = "description";break;
297 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
298 case MKTAG( 't','v','s','h'): key = "show"; break;
299 case MKTAG( 't','v','e','n'): key = "episode_id";break;
300 case MKTAG( 't','v','n','n'): key = "network"; break;
301 case MKTAG( 't','r','k','n'): key = "track";
302 parse = mov_metadata_track_or_disc_number; break;
303 case MKTAG( 'd','i','s','k'): key = "disc";
304 parse = mov_metadata_track_or_disc_number; break;
305 case MKTAG( 't','v','e','s'): key = "episode_sort";
306 parse = mov_metadata_int8_bypass_padding; break;
307 case MKTAG( 't','v','s','n'): key = "season_number";
308 parse = mov_metadata_int8_bypass_padding; break;
309 case MKTAG( 's','t','i','k'): key = "media_type";
310 parse = mov_metadata_int8_no_padding; break;
311 case MKTAG( 'h','d','v','d'): key = "hd_video";
312 parse = mov_metadata_int8_no_padding; break;
313 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
314 parse = mov_metadata_int8_no_padding; break;
315 case MKTAG( '@','P','R','M'):
316 return mov_metadata_raw(c, pb, atom.size, "premiere_version");
317 case MKTAG( '@','P','R','Q'):
318 return mov_metadata_raw(c, pb, atom.size, "quicktime_version");
319 case MKTAG( 'l','o','c','i'):
320 return mov_metadata_loci(c, pb, atom.size);
321 case MKTAG( 'X','M','P','_'):
322 return mov_metadata_raw(c, pb, atom.size, "xmp");
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;
334 if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
335 int ret = mov_read_covr(c, pb, data_type, str_size);
337 av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
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);
348 str_size = atom.size;
350 #ifdef MOV_EXPORT_ALL_METADATA
352 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
360 return AVERROR_INVALIDDATA;
362 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
365 parse(c, pb, str_size, key);
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));
370 int ret = avio_read(pb, str, str_size);
372 return ret < 0 ? ret : AVERROR_INVALIDDATA;
375 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
376 av_dict_set(&c->fc->metadata, key, str, 0);
377 if (*language && strcmp(language, "und")) {
378 snprintf(key2, sizeof(key2), "%s-%s", key, language);
379 av_dict_set(&c->fc->metadata, key2, str, 0);
382 av_dlog(c->fc, "lang \"%3s\" ", language);
383 av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
384 key, str, (char*)&atom.type, str_size, atom.size);
389 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
392 int i, nb_chapters, str_len, version;
395 if ((atom.size -= 5) < 0)
398 version = avio_r8(pb);
401 avio_rb32(pb); // ???
402 nb_chapters = avio_r8(pb);
404 for (i = 0; i < nb_chapters; i++) {
408 start = avio_rb64(pb);
409 str_len = avio_r8(pb);
411 if ((atom.size -= 9+str_len) < 0)
414 avio_read(pb, str, str_len);
416 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
421 #define MIN_DATA_ENTRY_BOX_SIZE 12
422 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
425 MOVStreamContext *sc;
428 if (c->fc->nb_streams < 1)
430 st = c->fc->streams[c->fc->nb_streams-1];
433 avio_rb32(pb); // version + flags
434 entries = avio_rb32(pb);
435 if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
436 entries >= UINT_MAX / sizeof(*sc->drefs))
437 return AVERROR_INVALIDDATA;
440 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
442 return AVERROR(ENOMEM);
443 sc->drefs_count = entries;
445 for (i = 0; i < sc->drefs_count; i++) {
446 MOVDref *dref = &sc->drefs[i];
447 uint32_t size = avio_rb32(pb);
448 int64_t next = avio_tell(pb) + size - 4;
451 return AVERROR_INVALIDDATA;
453 dref->type = avio_rl32(pb);
454 avio_rb32(pb); // version + flags
455 av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
457 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
458 /* macintosh alias record */
459 uint16_t volume_len, len;
464 volume_len = avio_r8(pb);
465 volume_len = FFMIN(volume_len, 27);
466 avio_read(pb, dref->volume, 27);
467 dref->volume[volume_len] = 0;
468 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
473 len = FFMIN(len, 63);
474 avio_read(pb, dref->filename, 63);
475 dref->filename[len] = 0;
476 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
480 /* read next level up_from_alias/down_to_target */
481 dref->nlvl_from = avio_rb16(pb);
482 dref->nlvl_to = avio_rb16(pb);
483 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
484 dref->nlvl_from, dref->nlvl_to);
488 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
491 type = avio_rb16(pb);
493 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
496 if (type == 2) { // absolute path
498 dref->path = av_mallocz(len+1);
500 return AVERROR(ENOMEM);
501 avio_read(pb, dref->path, len);
502 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
504 memmove(dref->path, dref->path+volume_len, len);
507 for (j = 0; j < len; j++)
508 if (dref->path[j] == ':')
510 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
511 } else if (type == 0) { // directory name
513 dref->dir = av_malloc(len+1);
515 return AVERROR(ENOMEM);
516 if (avio_read(pb, dref->dir, len) != len)
517 return AVERROR_INVALIDDATA;
519 for (j = 0; j < len; j++)
520 if (dref->dir[j] == ':')
522 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
527 avio_seek(pb, next, SEEK_SET);
532 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
536 uint32_t av_unused ctype;
540 if (c->fc->nb_streams < 1) // meta before first trak
543 st = c->fc->streams[c->fc->nb_streams-1];
545 avio_r8(pb); /* version */
546 avio_rb24(pb); /* flags */
549 ctype = avio_rl32(pb);
550 type = avio_rl32(pb); /* component subtype */
552 av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
553 av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
555 if (type == MKTAG('v','i','d','e'))
556 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
557 else if (type == MKTAG('s','o','u','n'))
558 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
559 else if (type == MKTAG('m','1','a',' '))
560 st->codec->codec_id = AV_CODEC_ID_MP2;
561 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
562 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
564 avio_rb32(pb); /* component manufacture */
565 avio_rb32(pb); /* component flags */
566 avio_rb32(pb); /* component flags mask */
568 title_size = atom.size - 24;
569 if (title_size > 0) {
570 title_str = av_malloc(title_size + 1); /* Add null terminator */
572 return AVERROR(ENOMEM);
573 avio_read(pb, title_str, title_size);
574 title_str[title_size] = 0;
576 av_dict_set(&st->metadata, "handler_name", title_str +
577 (!c->isom && title_str[0] == title_size - 1), 0);
578 av_freep(&title_str);
584 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
589 if (fc->nb_streams < 1)
591 st = fc->streams[fc->nb_streams-1];
593 avio_rb32(pb); /* version + flags */
594 ff_mp4_read_descr(fc, pb, &tag);
595 if (tag == MP4ESDescrTag) {
596 ff_mp4_parse_es_descr(pb, NULL);
598 avio_rb16(pb); /* ID */
600 ff_mp4_read_descr(fc, pb, &tag);
601 if (tag == MP4DecConfigDescrTag)
602 ff_mp4_read_dec_config_descr(fc, st, pb);
606 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
608 return ff_mov_read_esds(c->fc, pb);
611 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
614 int ac3info, acmod, lfeon, bsmod;
616 if (c->fc->nb_streams < 1)
618 st = c->fc->streams[c->fc->nb_streams-1];
620 ac3info = avio_rb24(pb);
621 bsmod = (ac3info >> 14) & 0x7;
622 acmod = (ac3info >> 11) & 0x7;
623 lfeon = (ac3info >> 10) & 0x1;
624 st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
625 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
627 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
628 st->codec->audio_service_type = bsmod;
629 if (st->codec->channels > 1 && bsmod == 0x7)
630 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
635 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
638 int eac3info, acmod, lfeon, bsmod;
640 if (c->fc->nb_streams < 1)
642 st = c->fc->streams[c->fc->nb_streams-1];
644 /* No need to parse fields for additional independent substreams and its
645 * associated dependent substreams since libavcodec's E-AC-3 decoder
646 * does not support them yet. */
647 avio_rb16(pb); /* data_rate and num_ind_sub */
648 eac3info = avio_rb24(pb);
649 bsmod = (eac3info >> 12) & 0x1f;
650 acmod = (eac3info >> 9) & 0x7;
651 lfeon = (eac3info >> 8) & 0x1;
652 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
654 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
655 st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
656 st->codec->audio_service_type = bsmod;
657 if (st->codec->channels > 1 && bsmod == 0x7)
658 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
663 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
667 if (c->fc->nb_streams < 1)
669 st = c->fc->streams[c->fc->nb_streams-1];
674 /* skip version and flags */
677 ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
682 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
686 if (c->fc->nb_streams < 1)
688 st = c->fc->streams[c->fc->nb_streams-1];
690 if (ff_get_wav_header(pb, st->codec, atom.size) < 0) {
691 av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
697 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
699 const int num = avio_rb32(pb);
700 const int den = avio_rb32(pb);
703 if (c->fc->nb_streams < 1)
705 st = c->fc->streams[c->fc->nb_streams-1];
707 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
708 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
709 av_log(c->fc, AV_LOG_WARNING,
710 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
711 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
713 } else if (den != 0) {
714 st->sample_aspect_ratio.num = num;
715 st->sample_aspect_ratio.den = den;
720 /* this atom contains actual media data */
721 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
723 if (atom.size == 0) /* wrong one (MP4) */
726 return 0; /* now go for moov */
729 /* read major brand, minor version and compatible brands and store them as metadata */
730 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
734 char* comp_brands_str;
735 uint8_t type[5] = {0};
737 avio_read(pb, type, 4);
738 if (strcmp(type, "qt "))
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 av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
745 comp_brand_size = atom.size - 8;
746 if (comp_brand_size < 0)
747 return AVERROR_INVALIDDATA;
748 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
749 if (!comp_brands_str)
750 return AVERROR(ENOMEM);
751 avio_read(pb, comp_brands_str, comp_brand_size);
752 comp_brands_str[comp_brand_size] = 0;
753 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
754 av_freep(&comp_brands_str);
759 /* this atom should contain all header atoms */
760 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
765 av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
766 avio_skip(pb, atom.size);
770 if ((ret = mov_read_default(c, pb, atom)) < 0)
772 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
773 /* so we don't parse the whole file if over a network */
775 return 0; /* now go for mdat */
778 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
780 if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
781 c->has_looked_for_mfra = 1;
784 av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
786 if ((ret = mov_read_mfra(c, pb)) < 0) {
787 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
788 "read the mfra (may be a live ismv)\n");
791 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
792 "seekable, can not look for mfra\n");
795 c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
796 av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
797 return mov_read_default(c, pb, atom);
800 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
806 if(time >= 2082844800)
807 time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
809 ptm = gmtime(&timet);
811 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
812 av_dict_set(metadata, "creation_time", buffer, 0);
816 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
819 MOVStreamContext *sc;
821 char language[4] = {0};
823 int64_t creation_time;
825 if (c->fc->nb_streams < 1)
827 st = c->fc->streams[c->fc->nb_streams-1];
830 if (sc->time_scale) {
831 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
832 return AVERROR_INVALIDDATA;
835 version = avio_r8(pb);
837 avpriv_request_sample(c->fc, "Version %d", version);
838 return AVERROR_PATCHWELCOME;
840 avio_rb24(pb); /* flags */
842 creation_time = avio_rb64(pb);
845 creation_time = avio_rb32(pb);
846 avio_rb32(pb); /* modification time */
848 mov_metadata_creation_time(&st->metadata, creation_time);
850 sc->time_scale = avio_rb32(pb);
851 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
853 lang = avio_rb16(pb); /* language */
854 if (ff_mov_lang_to_iso639(lang, language))
855 av_dict_set(&st->metadata, "language", language, 0);
856 avio_rb16(pb); /* quality */
861 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
863 int64_t creation_time;
864 int version = avio_r8(pb); /* version */
865 avio_rb24(pb); /* flags */
868 creation_time = avio_rb64(pb);
871 creation_time = avio_rb32(pb);
872 avio_rb32(pb); /* modification time */
874 mov_metadata_creation_time(&c->fc->metadata, creation_time);
875 c->time_scale = avio_rb32(pb); /* time scale */
877 av_dlog(c->fc, "time scale = %i\n", c->time_scale);
879 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
880 // set the AVCodecContext duration because the duration of individual tracks
882 if (c->time_scale > 0 && !c->trex_data)
883 c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
884 avio_rb32(pb); /* preferred scale */
886 avio_rb16(pb); /* preferred volume */
888 avio_skip(pb, 10); /* reserved */
890 avio_skip(pb, 36); /* display matrix */
892 avio_rb32(pb); /* preview time */
893 avio_rb32(pb); /* preview duration */
894 avio_rb32(pb); /* poster time */
895 avio_rb32(pb); /* selection time */
896 avio_rb32(pb); /* selection duration */
897 avio_rb32(pb); /* current time */
898 avio_rb32(pb); /* next track ID */
902 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
907 if (c->fc->nb_streams < 1)
909 st = c->fc->streams[c->fc->nb_streams-1];
911 little_endian = avio_rb16(pb) & 0xFF;
912 av_dlog(c->fc, "enda %d\n", little_endian);
913 if (little_endian == 1) {
914 switch (st->codec->codec_id) {
915 case AV_CODEC_ID_PCM_S24BE:
916 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
918 case AV_CODEC_ID_PCM_S32BE:
919 st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
921 case AV_CODEC_ID_PCM_F32BE:
922 st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
924 case AV_CODEC_ID_PCM_F64BE:
925 st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
934 static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
937 char color_parameter_type[5] = { 0 };
938 int color_primaries, color_trc, color_matrix;
940 if (c->fc->nb_streams < 1)
942 st = c->fc->streams[c->fc->nb_streams - 1];
944 avio_read(pb, color_parameter_type, 4);
945 if (strncmp(color_parameter_type, "nclx", 4) &&
946 strncmp(color_parameter_type, "nclc", 4)) {
947 av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
948 color_parameter_type);
952 color_primaries = avio_rb16(pb);
953 color_trc = avio_rb16(pb);
954 color_matrix = avio_rb16(pb);
956 av_dlog(c->fc, "%s: pri %d trc %d matrix %d",
957 color_parameter_type, color_primaries, color_trc, color_matrix);
960 uint8_t color_range = avio_r8(pb) >> 7;
961 av_dlog(c->fc, " full %"PRIu8"", color_range);
963 st->codec->color_range = AVCOL_RANGE_JPEG;
965 st->codec->color_range = AVCOL_RANGE_MPEG;
966 /* 14496-12 references JPEG XR specs (rather than the more complete
967 * 23001-8) so some adjusting is required */
968 if (color_primaries >= AVCOL_PRI_FILM)
969 color_primaries = AVCOL_PRI_UNSPECIFIED;
970 if ((color_trc >= AVCOL_TRC_LINEAR &&
971 color_trc <= AVCOL_TRC_LOG_SQRT) ||
972 color_trc >= AVCOL_TRC_BT2020_10)
973 color_trc = AVCOL_TRC_UNSPECIFIED;
974 if (color_matrix >= AVCOL_SPC_BT2020_NCL)
975 color_matrix = AVCOL_SPC_UNSPECIFIED;
976 st->codec->color_primaries = color_primaries;
977 st->codec->color_trc = color_trc;
978 st->codec->colorspace = color_matrix;
980 /* color primaries, Table 4-4 */
981 switch (color_primaries) {
982 case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break;
983 case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break;
984 case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break;
986 /* color transfer, Table 4-5 */
988 case 1: st->codec->color_trc = AVCOL_TRC_BT709; break;
989 case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break;
991 /* color matrix, Table 4-6 */
992 switch (color_matrix) {
993 case 1: st->codec->colorspace = AVCOL_SPC_BT709; break;
994 case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break;
995 case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break;
998 av_dlog(c->fc, "\n");
1003 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1006 unsigned mov_field_order;
1007 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1009 if (c->fc->nb_streams < 1) // will happen with jp2 files
1011 st = c->fc->streams[c->fc->nb_streams-1];
1013 return AVERROR_INVALIDDATA;
1014 mov_field_order = avio_rb16(pb);
1015 if ((mov_field_order & 0xFF00) == 0x0100)
1016 decoded_field_order = AV_FIELD_PROGRESSIVE;
1017 else if ((mov_field_order & 0xFF00) == 0x0200) {
1018 switch (mov_field_order & 0xFF) {
1019 case 0x01: decoded_field_order = AV_FIELD_TT;
1021 case 0x06: decoded_field_order = AV_FIELD_BB;
1023 case 0x09: decoded_field_order = AV_FIELD_TB;
1025 case 0x0E: decoded_field_order = AV_FIELD_BT;
1029 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1030 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1032 st->codec->field_order = decoded_field_order;
1037 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
1038 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1039 enum AVCodecID codec_id)
1046 if (c->fc->nb_streams < 1) // will happen with jp2 files
1048 st= c->fc->streams[c->fc->nb_streams-1];
1050 if (st->codec->codec_id != codec_id)
1051 return 0; /* unexpected codec_id - don't mess with extradata */
1053 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
1054 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1055 return AVERROR_INVALIDDATA;
1056 if ((err = av_reallocp(&st->codec->extradata, size)) < 0) {
1057 st->codec->extradata_size = 0;
1060 buf = st->codec->extradata + st->codec->extradata_size;
1061 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
1062 AV_WB32( buf , atom.size + 8);
1063 AV_WL32( buf + 4, atom.type);
1064 err = avio_read(pb, buf + 8, atom.size);
1067 } else if (err < atom.size) {
1068 av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1069 st->codec->extradata_size -= atom.size - err;
1071 memset(buf + 8 + err, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1075 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1076 static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1078 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1081 static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1083 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1086 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1088 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1091 static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1093 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1095 ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1099 static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1101 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1103 if (!ret && c->fc->nb_streams >= 1) {
1104 AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1105 if (avctx->extradata_size >= 40) {
1106 avctx->height = AV_RB16(&avctx->extradata[36]);
1107 avctx->width = AV_RB16(&avctx->extradata[38]);
1113 static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1115 if (c->fc->nb_streams >= 1) {
1116 AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1117 if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1118 codec->codec_id == AV_CODEC_ID_H264 &&
1121 /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1122 if (avio_rb16(pb) == 0xd4d)
1123 codec->width = 1440;
1128 return mov_read_avid(c, pb, atom);
1131 static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1133 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1136 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1140 if (c->fc->nb_streams < 1)
1142 st = c->fc->streams[c->fc->nb_streams-1];
1144 if ((uint64_t)atom.size > (1<<30))
1145 return AVERROR_INVALIDDATA;
1147 if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1148 st->codec->codec_id == AV_CODEC_ID_QDMC ||
1149 st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1150 // pass all frma atom to codec, needed at least for QDMC and QDM2
1151 av_free(st->codec->extradata);
1152 if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1153 return AVERROR(ENOMEM);
1154 } else if (atom.size > 8) { /* to read frma, esds atoms */
1156 if ((ret = mov_read_default(c, pb, atom)) < 0)
1159 avio_skip(pb, atom.size);
1164 * This function reads atom content and puts data in extradata without tag
1165 * nor size unlike mov_read_extradata.
1167 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1171 if (c->fc->nb_streams < 1)
1173 st = c->fc->streams[c->fc->nb_streams-1];
1175 if ((uint64_t)atom.size > (1<<30))
1176 return AVERROR_INVALIDDATA;
1178 if (atom.size >= 10) {
1179 // Broken files created by legacy versions of libavformat will
1180 // wrap a whole fiel atom inside of a glbl atom.
1181 unsigned size = avio_rb32(pb);
1182 unsigned type = avio_rl32(pb);
1183 avio_seek(pb, -8, SEEK_CUR);
1184 if (type == MKTAG('f','i','e','l') && size == atom.size)
1185 return mov_read_default(c, pb, atom);
1187 if (st->codec->extradata_size > 1 && st->codec->extradata) {
1188 av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
1191 av_free(st->codec->extradata);
1192 if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1193 return AVERROR(ENOMEM);
1198 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1201 uint8_t profile_level;
1204 if (c->fc->nb_streams < 1)
1206 st = c->fc->streams[c->fc->nb_streams-1];
1208 if (atom.size >= (1<<28) || atom.size < 7)
1209 return AVERROR_INVALIDDATA;
1211 profile_level = avio_r8(pb);
1212 if ((profile_level & 0xf0) != 0xc0)
1215 avio_seek(pb, 6, SEEK_CUR);
1216 av_free(st->codec->extradata);
1217 if ((ret = ff_get_extradata(st->codec, pb, atom.size - 7)) < 0)
1224 * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1225 * but can have extradata appended at the end after the 40 bytes belonging
1228 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1232 if (c->fc->nb_streams < 1)
1234 if (atom.size <= 40)
1236 st = c->fc->streams[c->fc->nb_streams-1];
1238 if ((uint64_t)atom.size > (1<<30))
1239 return AVERROR_INVALIDDATA;
1242 av_free(st->codec->extradata);
1243 if (ff_get_extradata(st->codec, pb, atom.size - 40) < 0)
1244 return AVERROR(ENOMEM);
1248 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1251 MOVStreamContext *sc;
1252 unsigned int i, entries;
1254 if (c->fc->nb_streams < 1)
1256 st = c->fc->streams[c->fc->nb_streams-1];
1259 avio_r8(pb); /* version */
1260 avio_rb24(pb); /* flags */
1262 entries = avio_rb32(pb);
1266 if (entries >= UINT_MAX/sizeof(int64_t))
1267 return AVERROR_INVALIDDATA;
1269 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
1270 if (!sc->chunk_offsets)
1271 return AVERROR(ENOMEM);
1272 sc->chunk_count = entries;
1274 if (atom.type == MKTAG('s','t','c','o'))
1275 for (i = 0; i < entries && !pb->eof_reached; i++)
1276 sc->chunk_offsets[i] = avio_rb32(pb);
1277 else if (atom.type == MKTAG('c','o','6','4'))
1278 for (i = 0; i < entries && !pb->eof_reached; i++)
1279 sc->chunk_offsets[i] = avio_rb64(pb);
1281 return AVERROR_INVALIDDATA;
1283 sc->chunk_count = i;
1285 if (pb->eof_reached)
1292 * Compute codec id for 'lpcm' tag.
1293 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1295 enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1302 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1305 static int mov_codec_id(AVStream *st, uint32_t format)
1307 int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1310 ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1311 (format & 0xFFFF) == 'T' + ('S' << 8)))
1312 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1314 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1315 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1316 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1317 /* skip old asf mpeg4 tag */
1318 format && format != MKTAG('m','p','4','s')) {
1319 id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1321 id = ff_codec_get_id(ff_codec_bmp_tags, format);
1323 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1324 else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1325 (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1326 st->codec->codec_id == AV_CODEC_ID_NONE)) {
1327 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1329 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1333 st->codec->codec_tag = format;
1338 static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1339 AVStream *st, MOVStreamContext *sc)
1341 uint8_t codec_name[32];
1342 unsigned int color_depth, len, j;
1343 int color_greyscale;
1346 avio_rb16(pb); /* version */
1347 avio_rb16(pb); /* revision level */
1348 avio_rb32(pb); /* vendor */
1349 avio_rb32(pb); /* temporal quality */
1350 avio_rb32(pb); /* spatial quality */
1352 st->codec->width = avio_rb16(pb); /* width */
1353 st->codec->height = avio_rb16(pb); /* height */
1355 avio_rb32(pb); /* horiz resolution */
1356 avio_rb32(pb); /* vert resolution */
1357 avio_rb32(pb); /* data size, always 0 */
1358 avio_rb16(pb); /* frames per samples */
1360 len = avio_r8(pb); /* codec name, pascal string */
1363 mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1365 avio_skip(pb, 31 - len);
1368 av_dict_set(&st->metadata, "encoder", codec_name, 0);
1370 /* codec_tag YV12 triggers an UV swap in rawdec.c */
1371 if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1372 st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1373 st->codec->width &= ~1;
1374 st->codec->height &= ~1;
1376 /* Flash Media Server uses tag H263 with Sorenson Spark */
1377 if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1378 !memcmp(codec_name, "Sorenson H263", 13))
1379 st->codec->codec_id = AV_CODEC_ID_FLV1;
1381 st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1382 color_table_id = avio_rb16(pb); /* colortable id */
1383 av_dlog(c->fc, "depth %d, ctab id %d\n",
1384 st->codec->bits_per_coded_sample, color_table_id);
1385 /* figure out the palette situation */
1386 color_depth = st->codec->bits_per_coded_sample & 0x1F;
1387 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1388 /* Do not create a greyscale palette for cinepak */
1389 if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
1392 /* if the depth is 2, 4, or 8 bpp, file is palettized */
1393 if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1394 /* for palette traversal */
1395 unsigned int color_start, color_count, color_end;
1396 unsigned char a, r, g, b;
1398 if (color_greyscale) {
1399 int color_index, color_dec;
1400 /* compute the greyscale palette */
1401 st->codec->bits_per_coded_sample = color_depth;
1402 color_count = 1 << color_depth;
1404 color_dec = 256 / (color_count - 1);
1405 for (j = 0; j < color_count; j++) {
1406 r = g = b = color_index;
1407 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1408 color_index -= color_dec;
1409 if (color_index < 0)
1412 } else if (color_table_id) {
1413 const uint8_t *color_table;
1414 /* if flag bit 3 is set, use the default palette */
1415 color_count = 1 << color_depth;
1416 if (color_depth == 2)
1417 color_table = ff_qt_default_palette_4;
1418 else if (color_depth == 4)
1419 color_table = ff_qt_default_palette_16;
1421 color_table = ff_qt_default_palette_256;
1423 for (j = 0; j < color_count; j++) {
1424 r = color_table[j * 3 + 0];
1425 g = color_table[j * 3 + 1];
1426 b = color_table[j * 3 + 2];
1427 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1430 /* load the palette from the file */
1431 color_start = avio_rb32(pb);
1432 color_count = avio_rb16(pb);
1433 color_end = avio_rb16(pb);
1434 if ((color_start <= 255) && (color_end <= 255)) {
1435 for (j = color_start; j <= color_end; j++) {
1436 /* each A, R, G, or B component is 16 bits;
1437 * only use the top 8 bits */
1446 sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1450 sc->has_palette = 1;
1454 static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1455 AVStream *st, MOVStreamContext *sc)
1457 int bits_per_sample, flags;
1458 uint16_t version = avio_rb16(pb);
1459 AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1461 avio_rb16(pb); /* revision level */
1462 avio_rb32(pb); /* vendor */
1464 st->codec->channels = avio_rb16(pb); /* channel count */
1465 st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1466 av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
1468 sc->audio_cid = avio_rb16(pb);
1469 avio_rb16(pb); /* packet size = 0 */
1471 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1473 // Read QT version 1 fields. In version 0 these do not exist.
1474 av_dlog(c->fc, "version =%d, isom =%d\n", version, c->isom);
1476 (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1479 sc->samples_per_frame = avio_rb32(pb);
1480 avio_rb32(pb); /* bytes per packet */
1481 sc->bytes_per_frame = avio_rb32(pb);
1482 avio_rb32(pb); /* bytes per sample */
1483 } else if (version == 2) {
1484 avio_rb32(pb); /* sizeof struct only */
1485 st->codec->sample_rate = av_int2double(avio_rb64(pb));
1486 st->codec->channels = avio_rb32(pb);
1487 avio_rb32(pb); /* always 0x7F000000 */
1488 st->codec->bits_per_coded_sample = avio_rb32(pb);
1490 flags = avio_rb32(pb); /* lpcm format specific flag */
1491 sc->bytes_per_frame = avio_rb32(pb);
1492 sc->samples_per_frame = avio_rb32(pb);
1493 if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1494 st->codec->codec_id =
1495 ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
1500 switch (st->codec->codec_id) {
1501 case AV_CODEC_ID_PCM_S8:
1502 case AV_CODEC_ID_PCM_U8:
1503 if (st->codec->bits_per_coded_sample == 16)
1504 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
1506 case AV_CODEC_ID_PCM_S16LE:
1507 case AV_CODEC_ID_PCM_S16BE:
1508 if (st->codec->bits_per_coded_sample == 8)
1509 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
1510 else if (st->codec->bits_per_coded_sample == 24)
1511 st->codec->codec_id =
1512 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1513 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1514 else if (st->codec->bits_per_coded_sample == 32)
1515 st->codec->codec_id =
1516 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1517 AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
1519 /* set values for old format before stsd version 1 appeared */
1520 case AV_CODEC_ID_MACE3:
1521 sc->samples_per_frame = 6;
1522 sc->bytes_per_frame = 2 * st->codec->channels;
1524 case AV_CODEC_ID_MACE6:
1525 sc->samples_per_frame = 6;
1526 sc->bytes_per_frame = 1 * st->codec->channels;
1528 case AV_CODEC_ID_ADPCM_IMA_QT:
1529 sc->samples_per_frame = 64;
1530 sc->bytes_per_frame = 34 * st->codec->channels;
1532 case AV_CODEC_ID_GSM:
1533 sc->samples_per_frame = 160;
1534 sc->bytes_per_frame = 33;
1540 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1541 if (bits_per_sample) {
1542 st->codec->bits_per_coded_sample = bits_per_sample;
1543 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1547 static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1548 AVStream *st, MOVStreamContext *sc,
1551 // ttxt stsd contains display flags, justification, background
1552 // color, fonts, and default styles, so fake an atom to read it
1553 MOVAtom fake_atom = { .size = size };
1554 // mp4s contains a regular esds atom
1555 if (st->codec->codec_tag != AV_RL32("mp4s"))
1556 mov_read_glbl(c, pb, fake_atom);
1557 st->codec->width = sc->width;
1558 st->codec->height = sc->height;
1561 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1566 y = (ycbcr >> 16) & 0xFF;
1567 cr = (ycbcr >> 8) & 0xFF;
1570 b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128));
1571 g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
1572 r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
1574 return (r << 16) | (g << 8) | b;
1577 static int mov_rewrite_dvd_sub_extradata(AVStream *st)
1579 char buf[256] = {0};
1580 uint8_t *src = st->codec->extradata;
1583 if (st->codec->extradata_size != 64)
1586 if (st->codec->width > 0 && st->codec->height > 0)
1587 snprintf(buf, sizeof(buf), "size: %dx%d\n",
1588 st->codec->width, st->codec->height);
1589 av_strlcat(buf, "palette: ", sizeof(buf));
1591 for (i = 0; i < 16; i++) {
1592 uint32_t yuv = AV_RB32(src + i * 4);
1593 uint32_t rgba = yuv_to_rgba(yuv);
1595 av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1598 if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1601 av_freep(&st->codec->extradata);
1602 st->codec->extradata_size = 0;
1603 st->codec->extradata = av_mallocz(strlen(buf) + FF_INPUT_BUFFER_PADDING_SIZE);
1604 if (!st->codec->extradata)
1605 return AVERROR(ENOMEM);
1606 st->codec->extradata_size = strlen(buf);
1607 memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1612 static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
1613 AVStream *st, MOVStreamContext *sc,
1616 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1617 if (ff_get_extradata(st->codec, pb, size) < 0)
1618 return AVERROR(ENOMEM);
1620 MOVStreamContext *tmcd_ctx = st->priv_data;
1622 val = AV_RB32(st->codec->extradata + 4);
1623 tmcd_ctx->tmcd_flags = val;
1625 st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
1626 st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1627 st->codec->time_base.num = 1;
1629 uint32_t len = AV_RB32(st->codec->extradata + 18); /* name atom length */
1630 uint32_t format = AV_RB32(st->codec->extradata + 22);
1631 if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
1632 uint16_t str_size = AV_RB16(st->codec->extradata + 26); /* string length */
1633 if (str_size > 0 && size >= (int)str_size + 26) {
1634 char *reel_name = av_malloc(str_size + 1);
1636 return AVERROR(ENOMEM);
1637 memcpy(reel_name, st->codec->extradata + 30, str_size);
1638 reel_name[str_size] = 0; /* Add null terminator */
1639 /* don't add reel_name if emtpy string */
1640 if (*reel_name == 0) {
1643 av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL);
1650 /* other codec type, just skip (rtp, mp4s ...) */
1651 avio_skip(pb, size);
1656 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
1657 AVStream *st, MOVStreamContext *sc)
1659 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1660 !st->codec->sample_rate && sc->time_scale > 1)
1661 st->codec->sample_rate = sc->time_scale;
1663 /* special codec parameters handling */
1664 switch (st->codec->codec_id) {
1665 #if CONFIG_DV_DEMUXER
1666 case AV_CODEC_ID_DVAUDIO:
1667 c->dv_fctx = avformat_alloc_context();
1668 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
1670 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1671 return AVERROR(ENOMEM);
1673 sc->dv_audio_container = 1;
1674 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
1677 /* no ifdef since parameters are always those */
1678 case AV_CODEC_ID_QCELP:
1679 st->codec->channels = 1;
1680 // force sample rate for qcelp when not stored in mov
1681 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
1682 st->codec->sample_rate = 8000;
1683 // FIXME: Why is the following needed for some files?
1684 sc->samples_per_frame = 160;
1685 if (!sc->bytes_per_frame)
1686 sc->bytes_per_frame = 35;
1688 case AV_CODEC_ID_AMR_NB:
1689 st->codec->channels = 1;
1690 /* force sample rate for amr, stsd in 3gp does not store sample rate */
1691 st->codec->sample_rate = 8000;
1693 case AV_CODEC_ID_AMR_WB:
1694 st->codec->channels = 1;
1695 st->codec->sample_rate = 16000;
1697 case AV_CODEC_ID_MP2:
1698 case AV_CODEC_ID_MP3:
1699 /* force type after stsd for m1a hdlr */
1700 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1701 st->need_parsing = AVSTREAM_PARSE_FULL;
1703 case AV_CODEC_ID_GSM:
1704 case AV_CODEC_ID_ADPCM_MS:
1705 case AV_CODEC_ID_ADPCM_IMA_WAV:
1706 case AV_CODEC_ID_ILBC:
1707 case AV_CODEC_ID_MACE3:
1708 case AV_CODEC_ID_MACE6:
1709 case AV_CODEC_ID_QDM2:
1710 st->codec->block_align = sc->bytes_per_frame;
1712 case AV_CODEC_ID_ALAC:
1713 if (st->codec->extradata_size == 36) {
1714 st->codec->channels = AV_RB8 (st->codec->extradata + 21);
1715 st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
1718 case AV_CODEC_ID_AC3:
1719 case AV_CODEC_ID_EAC3:
1720 case AV_CODEC_ID_MPEG1VIDEO:
1721 case AV_CODEC_ID_VC1:
1722 st->need_parsing = AVSTREAM_PARSE_FULL;
1730 static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1731 int codec_tag, int format,
1734 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1737 (codec_tag != format &&
1738 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1739 : codec_tag != MKTAG('j','p','e','g')))) {
1740 /* Multiple fourcc, we skip JPEG. This is not correct, we should
1741 * export it as a separate AVStream but this needs a few changes
1742 * in the MOV demuxer, patch welcome. */
1744 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1745 avio_skip(pb, size);
1748 if ( codec_tag == AV_RL32("avc1") ||
1749 codec_tag == AV_RL32("hvc1") ||
1750 codec_tag == AV_RL32("hev1")
1752 av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
1757 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
1760 MOVStreamContext *sc;
1761 int pseudo_stream_id;
1763 if (c->fc->nb_streams < 1)
1765 st = c->fc->streams[c->fc->nb_streams-1];
1768 for (pseudo_stream_id = 0;
1769 pseudo_stream_id < entries && !pb->eof_reached;
1770 pseudo_stream_id++) {
1771 //Parsing Sample description table
1773 int ret, dref_id = 1;
1774 MOVAtom a = { AV_RL32("stsd") };
1775 int64_t start_pos = avio_tell(pb);
1776 int64_t size = avio_rb32(pb); /* size */
1777 uint32_t format = avio_rl32(pb); /* data format */
1780 avio_rb32(pb); /* reserved */
1781 avio_rb16(pb); /* reserved */
1782 dref_id = avio_rb16(pb);
1783 }else if (size <= 7){
1784 av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
1785 return AVERROR_INVALIDDATA;
1788 if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1789 size - (avio_tell(pb) - start_pos)))
1792 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
1793 sc->dref_id= dref_id;
1795 id = mov_codec_id(st, format);
1797 av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
1798 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
1799 (format >> 24) & 0xff, st->codec->codec_type);
1801 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
1802 st->codec->codec_id = id;
1803 mov_parse_stsd_video(c, pb, st, sc);
1804 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
1805 st->codec->codec_id = id;
1806 mov_parse_stsd_audio(c, pb, st, sc);
1807 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
1808 st->codec->codec_id = id;
1809 mov_parse_stsd_subtitle(c, pb, st, sc,
1810 size - (avio_tell(pb) - start_pos));
1812 ret = mov_parse_stsd_data(c, pb, st, sc,
1813 size - (avio_tell(pb) - start_pos));
1817 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1818 a.size = size - (avio_tell(pb) - start_pos);
1820 if ((ret = mov_read_default(c, pb, a)) < 0)
1822 } else if (a.size > 0)
1823 avio_skip(pb, a.size);
1826 if (pb->eof_reached)
1829 return mov_finalize_stsd_codec(c, pb, st, sc);
1832 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1836 avio_r8(pb); /* version */
1837 avio_rb24(pb); /* flags */
1838 entries = avio_rb32(pb);
1840 return ff_mov_read_stsd_entries(c, pb, entries);
1843 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1846 MOVStreamContext *sc;
1847 unsigned int i, entries;
1849 if (c->fc->nb_streams < 1)
1851 st = c->fc->streams[c->fc->nb_streams-1];
1854 avio_r8(pb); /* version */
1855 avio_rb24(pb); /* flags */
1857 entries = avio_rb32(pb);
1859 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1863 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1864 return AVERROR_INVALIDDATA;
1865 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1867 return AVERROR(ENOMEM);
1869 for (i = 0; i < entries && !pb->eof_reached; i++) {
1870 sc->stsc_data[i].first = avio_rb32(pb);
1871 sc->stsc_data[i].count = avio_rb32(pb);
1872 sc->stsc_data[i].id = avio_rb32(pb);
1877 if (pb->eof_reached)
1883 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1886 MOVStreamContext *sc;
1887 unsigned i, entries;
1889 if (c->fc->nb_streams < 1)
1891 st = c->fc->streams[c->fc->nb_streams-1];
1894 avio_rb32(pb); // version + flags
1896 entries = avio_rb32(pb);
1897 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1898 return AVERROR_INVALIDDATA;
1899 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1901 return AVERROR(ENOMEM);
1903 for (i = 0; i < entries && !pb->eof_reached; i++) {
1904 sc->stps_data[i] = avio_rb32(pb);
1905 //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1910 if (pb->eof_reached)
1916 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1919 MOVStreamContext *sc;
1920 unsigned int i, entries;
1922 if (c->fc->nb_streams < 1)
1924 st = c->fc->streams[c->fc->nb_streams-1];
1927 avio_r8(pb); /* version */
1928 avio_rb24(pb); /* flags */
1930 entries = avio_rb32(pb);
1932 av_dlog(c->fc, "keyframe_count = %d\n", entries);
1936 sc->keyframe_absent = 1;
1937 if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1938 st->need_parsing = AVSTREAM_PARSE_HEADERS;
1941 if (entries >= UINT_MAX / sizeof(int))
1942 return AVERROR_INVALIDDATA;
1943 sc->keyframes = av_malloc(entries * sizeof(int));
1945 return AVERROR(ENOMEM);
1947 for (i = 0; i < entries && !pb->eof_reached; i++) {
1948 sc->keyframes[i] = avio_rb32(pb);
1949 //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1952 sc->keyframe_count = i;
1954 if (pb->eof_reached)
1960 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1963 MOVStreamContext *sc;
1964 unsigned int i, entries, sample_size, field_size, num_bytes;
1968 if (c->fc->nb_streams < 1)
1970 st = c->fc->streams[c->fc->nb_streams-1];
1973 avio_r8(pb); /* version */
1974 avio_rb24(pb); /* flags */
1976 if (atom.type == MKTAG('s','t','s','z')) {
1977 sample_size = avio_rb32(pb);
1978 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1979 sc->sample_size = sample_size;
1980 sc->stsz_sample_size = sample_size;
1984 avio_rb24(pb); /* reserved */
1985 field_size = avio_r8(pb);
1987 entries = avio_rb32(pb);
1989 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1991 sc->sample_count = entries;
1995 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
1996 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
1997 return AVERROR_INVALIDDATA;
2002 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
2003 return AVERROR_INVALIDDATA;
2004 sc->sample_sizes = av_malloc(entries * sizeof(int));
2005 if (!sc->sample_sizes)
2006 return AVERROR(ENOMEM);
2008 num_bytes = (entries*field_size+4)>>3;
2010 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
2012 av_freep(&sc->sample_sizes);
2013 return AVERROR(ENOMEM);
2016 if (avio_read(pb, buf, num_bytes) < num_bytes) {
2017 av_freep(&sc->sample_sizes);
2019 return AVERROR_INVALIDDATA;
2022 init_get_bits(&gb, buf, 8*num_bytes);
2024 for (i = 0; i < entries && !pb->eof_reached; i++) {
2025 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2026 sc->data_size += sc->sample_sizes[i];
2029 sc->sample_count = i;
2031 if (pb->eof_reached)
2038 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2041 MOVStreamContext *sc;
2042 unsigned int i, entries;
2044 int64_t total_sample_count=0;
2046 if (c->fc->nb_streams < 1)
2048 st = c->fc->streams[c->fc->nb_streams-1];
2051 avio_r8(pb); /* version */
2052 avio_rb24(pb); /* flags */
2053 entries = avio_rb32(pb);
2055 av_dlog(c->fc, "track[%i].stts.entries = %i\n",
2056 c->fc->nb_streams-1, entries);
2058 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
2061 av_free(sc->stts_data);
2062 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
2064 return AVERROR(ENOMEM);
2066 for (i = 0; i < entries && !pb->eof_reached; i++) {
2067 int sample_duration;
2070 sample_count=avio_rb32(pb);
2071 sample_duration = avio_rb32(pb);
2073 /* sample_duration < 0 is invalid based on the spec */
2074 if (sample_duration < 0) {
2075 av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta %d in STTS, at %d st:%d\n",
2076 sample_duration, i, c->fc->nb_streams-1);
2077 sample_duration = 1;
2079 if (sample_count < 0) {
2080 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2081 return AVERROR_INVALIDDATA;
2083 sc->stts_data[i].count= sample_count;
2084 sc->stts_data[i].duration= sample_duration;
2086 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
2087 sample_count, sample_duration);
2091 && sample_count == 1
2092 && total_sample_count > 100
2093 && sample_duration/10 > duration / total_sample_count)
2094 sample_duration = duration / total_sample_count;
2095 duration+=(int64_t)sample_duration*sample_count;
2096 total_sample_count+=sample_count;
2101 sc->duration_for_fps += duration;
2102 sc->nb_frames_for_fps += total_sample_count;
2104 if (pb->eof_reached)
2107 st->nb_frames= total_sample_count;
2109 st->duration= duration;
2110 sc->track_end = duration;
2114 static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2117 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2121 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2124 MOVStreamContext *sc;
2125 unsigned int i, entries;
2127 if (c->fc->nb_streams < 1)
2129 st = c->fc->streams[c->fc->nb_streams-1];
2132 avio_r8(pb); /* version */
2133 avio_rb24(pb); /* flags */
2134 entries = avio_rb32(pb);
2136 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2140 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2141 return AVERROR_INVALIDDATA;
2142 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
2144 return AVERROR(ENOMEM);
2146 for (i = 0; i < entries && !pb->eof_reached; i++) {
2147 int count =avio_rb32(pb);
2148 int duration =avio_rb32(pb);
2150 sc->ctts_data[i].count = count;
2151 sc->ctts_data[i].duration= duration;
2153 av_dlog(c->fc, "count=%d, duration=%d\n",
2156 if (FFABS(duration) > (1<<28) && i+2<entries) {
2157 av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2158 av_freep(&sc->ctts_data);
2164 mov_update_dts_shift(sc, duration);
2169 if (pb->eof_reached)
2172 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
2177 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2180 MOVStreamContext *sc;
2181 unsigned int i, entries;
2183 uint32_t grouping_type;
2185 if (c->fc->nb_streams < 1)
2187 st = c->fc->streams[c->fc->nb_streams-1];
2190 version = avio_r8(pb); /* version */
2191 avio_rb24(pb); /* flags */
2192 grouping_type = avio_rl32(pb);
2193 if (grouping_type != MKTAG( 'r','a','p',' '))
2194 return 0; /* only support 'rap ' grouping */
2196 avio_rb32(pb); /* grouping_type_parameter */
2198 entries = avio_rb32(pb);
2201 if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2202 return AVERROR_INVALIDDATA;
2203 sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2205 return AVERROR(ENOMEM);
2207 for (i = 0; i < entries && !pb->eof_reached; i++) {
2208 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2209 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2212 sc->rap_group_count = i;
2214 return pb->eof_reached ? AVERROR_EOF : 0;
2217 static void mov_build_index(MOVContext *mov, AVStream *st)
2219 MOVStreamContext *sc = st->priv_data;
2220 int64_t current_offset;
2221 int64_t current_dts = 0;
2222 unsigned int stts_index = 0;
2223 unsigned int stsc_index = 0;
2224 unsigned int stss_index = 0;
2225 unsigned int stps_index = 0;
2227 uint64_t stream_size = 0;
2229 /* adjust first dts according to edit list */
2230 if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
2231 if (sc->empty_duration)
2232 sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
2233 sc->time_offset = sc->start_time - sc->empty_duration;
2234 current_dts = -sc->time_offset;
2235 if (sc->ctts_count>0 && sc->stts_count>0 &&
2236 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2237 /* more than 16 frames delay, dts are likely wrong
2238 this happens with files created by iMovie */
2240 st->codec->has_b_frames = 1;
2244 /* only use old uncompressed audio chunk demuxing when stts specifies it */
2245 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2246 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2247 unsigned int current_sample = 0;
2248 unsigned int stts_sample = 0;
2249 unsigned int sample_size;
2250 unsigned int distance = 0;
2251 unsigned int rap_group_index = 0;
2252 unsigned int rap_group_sample = 0;
2253 int rap_group_present = sc->rap_group_count && sc->rap_group;
2254 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2256 current_dts -= sc->dts_shift;
2258 if (!sc->sample_count || st->nb_index_entries)
2260 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2262 if (av_reallocp_array(&st->index_entries,
2263 st->nb_index_entries + sc->sample_count,
2264 sizeof(*st->index_entries)) < 0) {
2265 st->nb_index_entries = 0;
2268 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2270 for (i = 0; i < sc->chunk_count; i++) {
2271 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2272 current_offset = sc->chunk_offsets[i];
2273 while (stsc_index + 1 < sc->stsc_count &&
2274 i + 1 == sc->stsc_data[stsc_index + 1].first)
2277 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2278 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2279 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2280 sc->stsz_sample_size = sc->sample_size;
2282 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2283 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2284 sc->stsz_sample_size = sc->sample_size;
2287 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2289 if (current_sample >= sc->sample_count) {
2290 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2294 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2296 if (stss_index + 1 < sc->keyframe_count)
2298 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2300 if (stps_index + 1 < sc->stps_count)
2303 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2304 if (sc->rap_group[rap_group_index].index > 0)
2306 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2307 rap_group_sample = 0;
2311 if (sc->keyframe_absent
2313 && !rap_group_present
2314 && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2318 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2319 if (sc->pseudo_stream_id == -1 ||
2320 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2321 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2322 e->pos = current_offset;
2323 e->timestamp = current_dts;
2324 e->size = sample_size;
2325 e->min_distance = distance;
2326 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2327 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2328 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2329 current_offset, current_dts, sample_size, distance, keyframe);
2330 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2331 ff_rfps_add_frame(mov->fc, st, current_dts);
2334 current_offset += sample_size;
2335 stream_size += sample_size;
2336 current_dts += sc->stts_data[stts_index].duration;
2340 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2346 if (st->duration > 0)
2347 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2349 unsigned chunk_samples, total = 0;
2351 // compute total chunk count
2352 for (i = 0; i < sc->stsc_count; i++) {
2353 unsigned count, chunk_count;
2355 chunk_samples = sc->stsc_data[i].count;
2356 if (i != sc->stsc_count - 1 &&
2357 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2358 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2362 if (sc->samples_per_frame >= 160) { // gsm
2363 count = chunk_samples / sc->samples_per_frame;
2364 } else if (sc->samples_per_frame > 1) {
2365 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2366 count = (chunk_samples+samples-1) / samples;
2368 count = (chunk_samples+1023) / 1024;
2371 if (i < sc->stsc_count - 1)
2372 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2374 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2375 total += chunk_count * count;
2378 av_dlog(mov->fc, "chunk count %d\n", total);
2379 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2381 if (av_reallocp_array(&st->index_entries,
2382 st->nb_index_entries + total,
2383 sizeof(*st->index_entries)) < 0) {
2384 st->nb_index_entries = 0;
2387 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2390 for (i = 0; i < sc->chunk_count; i++) {
2391 current_offset = sc->chunk_offsets[i];
2392 if (stsc_index + 1 < sc->stsc_count &&
2393 i + 1 == sc->stsc_data[stsc_index + 1].first)
2395 chunk_samples = sc->stsc_data[stsc_index].count;
2397 while (chunk_samples > 0) {
2399 unsigned size, samples;
2401 if (sc->samples_per_frame >= 160) { // gsm
2402 samples = sc->samples_per_frame;
2403 size = sc->bytes_per_frame;
2405 if (sc->samples_per_frame > 1) {
2406 samples = FFMIN((1024 / sc->samples_per_frame)*
2407 sc->samples_per_frame, chunk_samples);
2408 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2410 samples = FFMIN(1024, chunk_samples);
2411 size = samples * sc->sample_size;
2415 if (st->nb_index_entries >= total) {
2416 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2419 e = &st->index_entries[st->nb_index_entries++];
2420 e->pos = current_offset;
2421 e->timestamp = current_dts;
2423 e->min_distance = 0;
2424 e->flags = AVINDEX_KEYFRAME;
2425 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2426 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2429 current_offset += size;
2430 current_dts += samples;
2431 chunk_samples -= samples;
2437 static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
2438 AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
2440 /* try relative path, we do not try the absolute because it can leak information about our
2441 system to an attacker */
2442 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2443 char filename[1024];
2444 const char *src_path;
2447 /* find a source dir */
2448 src_path = strrchr(src, '/');
2454 /* find a next level down to target */
2455 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2456 if (ref->path[l] == '/') {
2457 if (i == ref->nlvl_to - 1)
2463 /* compose filename if next level down to target was found */
2464 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2465 memcpy(filename, src, src_path - src);
2466 filename[src_path - src] = 0;
2468 for (i = 1; i < ref->nlvl_from; i++)
2469 av_strlcat(filename, "../", 1024);
2471 av_strlcat(filename, ref->path + l + 1, 1024);
2473 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2476 } else if (use_absolute_path) {
2477 av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
2478 "this is a possible security issue\n");
2479 if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2483 return AVERROR(ENOENT);
2486 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
2488 if (sc->time_scale <= 0) {
2489 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
2490 sc->time_scale = c->time_scale;
2491 if (sc->time_scale <= 0)
2496 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2499 MOVStreamContext *sc;
2502 st = avformat_new_stream(c->fc, NULL);
2503 if (!st) return AVERROR(ENOMEM);
2504 st->id = c->fc->nb_streams;
2505 sc = av_mallocz(sizeof(MOVStreamContext));
2506 if (!sc) return AVERROR(ENOMEM);
2509 st->codec->codec_type = AVMEDIA_TYPE_DATA;
2510 sc->ffindex = st->index;
2512 if ((ret = mov_read_default(c, pb, atom)) < 0)
2516 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2517 (!sc->sample_size && !sc->sample_count))) {
2518 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2523 fix_timescale(c, sc);
2525 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2527 mov_build_index(c, st);
2529 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2530 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2531 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
2532 c->use_absolute_path, c->fc) < 0)
2533 av_log(c->fc, AV_LOG_ERROR,
2534 "stream %d, error opening alias: path='%s', dir='%s', "
2535 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2536 st->index, dref->path, dref->dir, dref->filename,
2537 dref->volume, dref->nlvl_from, dref->nlvl_to);
2540 sc->pb_is_copied = 1;
2543 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2544 if (!st->sample_aspect_ratio.num &&
2545 (st->codec->width != sc->width || st->codec->height != sc->height)) {
2546 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2547 ((double)st->codec->width * sc->height), INT_MAX);
2550 #if FF_API_R_FRAME_RATE
2551 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
2552 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2553 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
2557 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2558 if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2559 TAG_IS_AVCI(st->codec->codec_tag)) {
2560 ret = ff_generate_avci_extradata(st);
2565 switch (st->codec->codec_id) {
2566 #if CONFIG_H261_DECODER
2567 case AV_CODEC_ID_H261:
2569 #if CONFIG_H263_DECODER
2570 case AV_CODEC_ID_H263:
2572 #if CONFIG_MPEG4_DECODER
2573 case AV_CODEC_ID_MPEG4:
2575 st->codec->width = 0; /* let decoder init width/height */
2576 st->codec->height= 0;
2580 /* Do not need those anymore. */
2581 av_freep(&sc->chunk_offsets);
2582 av_freep(&sc->stsc_data);
2583 av_freep(&sc->sample_sizes);
2584 av_freep(&sc->keyframes);
2585 av_freep(&sc->stts_data);
2586 av_freep(&sc->stps_data);
2587 av_freep(&sc->rap_group);
2592 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2595 c->itunes_metadata = 1;
2596 ret = mov_read_default(c, pb, atom);
2597 c->itunes_metadata = 0;
2601 static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
2603 int64_t end = avio_tell(pb) + size;
2604 uint8_t *key = NULL, *val = NULL;
2607 MOVStreamContext *sc;
2609 if (c->fc->nb_streams < 1)
2611 st = c->fc->streams[c->fc->nb_streams-1];
2614 for (i = 0; i < 2; i++) {
2618 if (end - avio_tell(pb) <= 12)
2621 len = avio_rb32(pb);
2622 tag = avio_rl32(pb);
2623 avio_skip(pb, 4); // flags
2625 if (len < 12 || len - 12 > end - avio_tell(pb))
2629 if (tag == MKTAG('n', 'a', 'm', 'e'))
2631 else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2638 *p = av_malloc(len + 1);
2641 avio_read(pb, *p, len);
2646 if (strcmp(key, "iTunSMPB") == 0) {
2647 int priming, remainder, samples;
2648 if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
2649 if(priming>0 && priming<16384)
2650 sc->start_pad = priming;
2653 if (strcmp(key, "cdec") != 0) {
2654 av_dict_set(&c->fc->metadata, key, val,
2655 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
2660 avio_seek(pb, end, SEEK_SET);
2666 static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2668 int64_t end = avio_tell(pb) + atom.size;
2674 len = avio_rb32(pb);
2675 tag = avio_rl32(pb);
2677 if (len > atom.size)
2680 if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2681 uint8_t domain[128];
2684 avio_skip(pb, 4); // flags
2687 domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2688 avio_skip(pb, len - domain_len);
2689 return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
2693 av_log(c->fc, AV_LOG_VERBOSE,
2694 "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2698 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2700 while (atom.size > 8) {
2701 uint32_t tag = avio_rl32(pb);
2703 if (tag == MKTAG('h','d','l','r')) {
2704 avio_seek(pb, -8, SEEK_CUR);
2706 return mov_read_default(c, pb, atom);
2712 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2717 int64_t disp_transform[2];
2718 int display_matrix[3][3];
2720 MOVStreamContext *sc;
2724 if (c->fc->nb_streams < 1)
2726 st = c->fc->streams[c->fc->nb_streams-1];
2729 version = avio_r8(pb);
2730 flags = avio_rb24(pb);
2731 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
2737 avio_rb32(pb); /* creation time */
2738 avio_rb32(pb); /* modification time */
2740 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2741 avio_rb32(pb); /* reserved */
2743 /* highlevel (considering edits) duration in movie timebase */
2744 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2745 avio_rb32(pb); /* reserved */
2746 avio_rb32(pb); /* reserved */
2748 avio_rb16(pb); /* layer */
2749 avio_rb16(pb); /* alternate group */
2750 avio_rb16(pb); /* volume */
2751 avio_rb16(pb); /* reserved */
2753 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2754 // they're kept in fixed point format through all calculations
2755 // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2756 // side data, but the scale factor is not needed to calculate aspect ratio
2757 for (i = 0; i < 3; i++) {
2758 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2759 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2760 display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
2763 width = avio_rb32(pb); // 16.16 fixed point track width
2764 height = avio_rb32(pb); // 16.16 fixed point track height
2765 sc->width = width >> 16;
2766 sc->height = height >> 16;
2768 // save the matrix and add rotate metadata when it is not the default
2770 if (display_matrix[0][0] != (1 << 16) ||
2771 display_matrix[1][1] != (1 << 16) ||
2772 display_matrix[2][2] != (1 << 30) ||
2773 display_matrix[0][1] || display_matrix[0][2] ||
2774 display_matrix[1][0] || display_matrix[1][2] ||
2775 display_matrix[2][0] || display_matrix[2][1]) {
2779 av_freep(&sc->display_matrix);
2780 sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2781 if (!sc->display_matrix)
2782 return AVERROR(ENOMEM);
2784 for (i = 0; i < 3; i++)
2785 for (j = 0; j < 3; j++)
2786 sc->display_matrix[i * 3 + j] = display_matrix[j][i];
2788 rotate = av_display_rotation_get(sc->display_matrix);
2789 if (!isnan(rotate)) {
2790 char rotate_buf[64];
2792 if (rotate < 0) // for backward compatibility
2794 snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
2795 av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
2799 // transform the display width/height according to the matrix
2800 // skip this if the display matrix is the default identity matrix
2801 // or if it is rotating the picture, ex iPhone 3GS
2802 // to keep the same scale, use [width height 1<<16]
2803 if (width && height &&
2804 ((display_matrix[0][0] != 65536 ||
2805 display_matrix[1][1] != 65536) &&
2806 !display_matrix[0][1] &&
2807 !display_matrix[1][0] &&
2808 !display_matrix[2][0] && !display_matrix[2][1])) {
2809 for (i = 0; i < 2; i++)
2811 (int64_t) width * display_matrix[0][i] +
2812 (int64_t) height * display_matrix[1][i] +
2813 ((int64_t) display_matrix[2][i] << 16);
2815 //sample aspect ratio is new width/height divided by old width/height
2816 st->sample_aspect_ratio = av_d2q(
2817 ((double) disp_transform[0] * height) /
2818 ((double) disp_transform[1] * width), INT_MAX);
2823 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2825 MOVFragment *frag = &c->fragment;
2826 MOVTrackExt *trex = NULL;
2827 MOVFragmentIndex* index = NULL;
2828 int flags, track_id, i;
2830 avio_r8(pb); /* version */
2831 flags = avio_rb24(pb);
2833 track_id = avio_rb32(pb);
2835 return AVERROR_INVALIDDATA;
2836 frag->track_id = track_id;
2837 for (i = 0; i < c->trex_count; i++)
2838 if (c->trex_data[i].track_id == frag->track_id) {
2839 trex = &c->trex_data[i];
2843 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2844 return AVERROR_INVALIDDATA;
2846 for (i = 0; i < c->fragment_index_count; i++) {
2847 MOVFragmentIndex* candidate = c->fragment_index_data[i];
2848 if (candidate->track_id == frag->track_id) {
2849 av_log(c->fc, AV_LOG_DEBUG,
2850 "found fragment index for track %u\n", frag->track_id);
2856 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
2857 avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
2858 frag->moof_offset : frag->implicit_offset;
2859 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2861 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2862 avio_rb32(pb) : trex->duration;
2863 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2864 avio_rb32(pb) : trex->size;
2865 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2866 avio_rb32(pb) : trex->flags;
2867 frag->time = AV_NOPTS_VALUE;
2870 for (i = index->current_item; i < index->item_count; i++) {
2871 if (frag->implicit_offset == index->items[i].moof_offset) {
2872 av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
2873 "for track %u and moof_offset %"PRId64"\n",
2874 frag->track_id, index->items[i].moof_offset);
2875 frag->time = index->items[i].time;
2876 index->current_item = i + 1;
2881 av_log(c->fc, AV_LOG_WARNING, "track %u has a fragment index "
2882 "but it doesn't have an (in-order) entry for moof_offset "
2883 "%"PRId64"\n", frag->track_id, frag->implicit_offset);
2886 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2890 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2892 c->chapter_track = avio_rb32(pb);
2896 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2901 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2902 return AVERROR_INVALIDDATA;
2903 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2904 sizeof(*c->trex_data))) < 0) {
2909 c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
2911 trex = &c->trex_data[c->trex_count++];
2912 avio_r8(pb); /* version */
2913 avio_rb24(pb); /* flags */
2914 trex->track_id = avio_rb32(pb);
2915 trex->stsd_id = avio_rb32(pb);
2916 trex->duration = avio_rb32(pb);
2917 trex->size = avio_rb32(pb);
2918 trex->flags = avio_rb32(pb);
2922 static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2924 MOVFragment *frag = &c->fragment;
2925 AVStream *st = NULL;
2926 MOVStreamContext *sc;
2929 for (i = 0; i < c->fc->nb_streams; i++) {
2930 if (c->fc->streams[i]->id == frag->track_id) {
2931 st = c->fc->streams[i];
2936 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2937 return AVERROR_INVALIDDATA;
2940 if (sc->pseudo_stream_id + 1 != frag->stsd_id)
2942 version = avio_r8(pb);
2943 avio_rb24(pb); /* flags */
2945 sc->track_end = avio_rb64(pb);
2947 sc->track_end = avio_rb32(pb);
2952 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2954 MOVFragment *frag = &c->fragment;
2955 AVStream *st = NULL;
2956 MOVStreamContext *sc;
2960 int data_offset = 0;
2961 unsigned entries, first_sample_flags = frag->flags;
2962 int flags, distance, i, found_keyframe = 0, err;
2964 for (i = 0; i < c->fc->nb_streams; i++) {
2965 if (c->fc->streams[i]->id == frag->track_id) {
2966 st = c->fc->streams[i];
2971 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2972 return AVERROR_INVALIDDATA;
2975 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
2977 avio_r8(pb); /* version */
2978 flags = avio_rb24(pb);
2979 entries = avio_rb32(pb);
2980 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2982 /* Always assume the presence of composition time offsets.
2983 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2984 * 1) in the initial movie, there are no samples.
2985 * 2) in the first movie fragment, there is only one sample without composition time offset.
2986 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2987 if (!sc->ctts_count && sc->sample_count)
2989 /* Complement ctts table if moov atom doesn't have ctts atom. */
2990 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2992 return AVERROR(ENOMEM);
2993 sc->ctts_data = ctts_data;
2994 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2995 sc->ctts_data[sc->ctts_count].duration = 0;
2998 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2999 return AVERROR_INVALIDDATA;
3000 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
3001 sizeof(*sc->ctts_data))) < 0) {
3005 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
3006 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
3007 dts = sc->track_end - sc->time_offset;
3008 offset = frag->base_data_offset + data_offset;
3010 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
3011 for (i = 0; i < entries && !pb->eof_reached; i++) {
3012 unsigned sample_size = frag->size;
3013 int sample_flags = i ? frag->flags : first_sample_flags;
3014 unsigned sample_duration = frag->duration;
3017 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
3018 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
3019 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
3020 sc->ctts_data[sc->ctts_count].count = 1;
3021 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
3023 mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
3024 if (frag->time != AV_NOPTS_VALUE) {
3025 if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
3026 int64_t pts = frag->time;
3027 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3028 " sc->dts_shift %d ctts.duration %d"
3029 " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
3030 sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
3031 sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
3032 dts = pts - sc->dts_shift;
3033 if (flags & MOV_TRUN_SAMPLE_CTS) {
3034 dts -= sc->ctts_data[sc->ctts_count].duration;
3036 dts -= sc->time_offset;
3038 av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
3041 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3042 ", using it for dts\n", dts);
3044 frag->time = AV_NOPTS_VALUE;
3047 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3049 else if (!found_keyframe)
3050 keyframe = found_keyframe =
3051 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
3052 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
3055 av_add_index_entry(st, offset, dts, sample_size, distance,
3056 keyframe ? AVINDEX_KEYFRAME : 0);
3057 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
3058 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
3059 offset, dts, sample_size, distance, keyframe);
3061 dts += sample_duration;
3062 offset += sample_size;
3063 sc->data_size += sample_size;
3064 sc->duration_for_fps += sample_duration;
3065 sc->nb_frames_for_fps ++;
3068 if (pb->eof_reached)
3071 frag->implicit_offset = offset;
3072 st->duration = sc->track_end = dts + sc->time_offset;
3076 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
3077 /* like the files created with Adobe Premiere 5.0, for samples see */
3078 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
3079 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3084 return 0; /* continue */
3085 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3086 avio_skip(pb, atom.size - 4);
3089 atom.type = avio_rl32(pb);
3091 if (atom.type != MKTAG('m','d','a','t')) {
3092 avio_skip(pb, atom.size);
3095 err = mov_read_mdat(c, pb, atom);
3099 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3104 uint8_t *moov_data; /* uncompressed data */
3105 long cmov_len, moov_len;
3108 avio_rb32(pb); /* dcom atom */
3109 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3110 return AVERROR_INVALIDDATA;
3111 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3112 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3113 return AVERROR_INVALIDDATA;
3115 avio_rb32(pb); /* cmvd atom */
3116 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3117 return AVERROR_INVALIDDATA;
3118 moov_len = avio_rb32(pb); /* uncompressed size */
3119 cmov_len = atom.size - 6 * 4;
3121 cmov_data = av_malloc(cmov_len);
3123 return AVERROR(ENOMEM);
3124 moov_data = av_malloc(moov_len);
3127 return AVERROR(ENOMEM);
3129 avio_read(pb, cmov_data, cmov_len);
3130 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3131 goto free_and_return;
3132 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3133 goto free_and_return;
3134 atom.type = MKTAG('m','o','o','v');
3135 atom.size = moov_len;
3136 ret = mov_read_default(c, &ctx, atom);
3142 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3143 return AVERROR(ENOSYS);
3147 /* edit list atom */
3148 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3150 MOVStreamContext *sc;
3151 int i, edit_count, version, edit_start_index = 0;
3152 int unsupported = 0;
3154 if (c->fc->nb_streams < 1 || c->ignore_editlist)
3156 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3158 version = avio_r8(pb); /* version */
3159 avio_rb24(pb); /* flags */
3160 edit_count = avio_rb32(pb); /* entries */
3162 if ((uint64_t)edit_count*12+8 > atom.size)
3163 return AVERROR_INVALIDDATA;
3165 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3166 for (i=0; i<edit_count; i++){
3171 duration = avio_rb64(pb);
3172 time = avio_rb64(pb);
3174 duration = avio_rb32(pb); /* segment duration */
3175 time = (int32_t)avio_rb32(pb); /* media time */
3177 rate = avio_rb32(pb);
3178 if (i == 0 && time == -1) {
3179 sc->empty_duration = duration;
3180 edit_start_index = 1;
3181 } else if (i == edit_start_index && time >= 0)
3182 sc->start_time = time;
3186 av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3187 duration, time, rate / 65536.0);
3191 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
3192 "a/v desync might occur, patch welcome\n");
3197 static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3199 MOVStreamContext *sc;
3201 if (c->fc->nb_streams < 1)
3202 return AVERROR_INVALIDDATA;
3203 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3204 sc->timecode_track = avio_rb32(pb);
3208 static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3212 static const uint8_t uuid_isml_manifest[] = {
3213 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3214 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3217 if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3218 return AVERROR_INVALIDDATA;
3220 ret = avio_read(pb, uuid, sizeof(uuid));
3223 } else if (ret != sizeof(uuid)) {
3224 return AVERROR_INVALIDDATA;
3226 if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3227 uint8_t *buffer, *ptr;
3229 size_t len = atom.size - sizeof(uuid);
3232 return AVERROR_INVALIDDATA;
3234 ret = avio_skip(pb, 4); // zeroes
3237 buffer = av_mallocz(len + 1);
3239 return AVERROR(ENOMEM);
3241 ret = avio_read(pb, buffer, len);
3245 } else if (ret != len) {
3247 return AVERROR_INVALIDDATA;
3251 while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
3252 ptr += sizeof("systemBitrate=\"") - 1;
3253 c->bitrates_count++;
3254 c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3256 c->bitrates_count = 0;
3258 return AVERROR(ENOMEM);
3261 ret = strtol(ptr, &endptr, 10);
3262 if (ret < 0 || errno || *endptr != '"') {
3263 c->bitrates[c->bitrates_count - 1] = 0;
3265 c->bitrates[c->bitrates_count - 1] = ret;
3274 static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3277 uint8_t content[16];
3282 ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
3288 && !memcmp(content, "Anevia\x1A\x1A", 8)
3289 && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
3290 c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
3296 static const MOVParseTableEntry mov_default_parse_table[] = {
3297 { MKTAG('A','C','L','R'), mov_read_avid },
3298 { MKTAG('A','P','R','G'), mov_read_avid },
3299 { MKTAG('A','A','L','P'), mov_read_avid },
3300 { MKTAG('A','R','E','S'), mov_read_ares },
3301 { MKTAG('a','v','s','s'), mov_read_avss },
3302 { MKTAG('c','h','p','l'), mov_read_chpl },
3303 { MKTAG('c','o','6','4'), mov_read_stco },
3304 { MKTAG('c','o','l','r'), mov_read_colr },
3305 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3306 { MKTAG('d','i','n','f'), mov_read_default },
3307 { MKTAG('d','r','e','f'), mov_read_dref },
3308 { MKTAG('e','d','t','s'), mov_read_default },
3309 { MKTAG('e','l','s','t'), mov_read_elst },
3310 { MKTAG('e','n','d','a'), mov_read_enda },
3311 { MKTAG('f','i','e','l'), mov_read_fiel },
3312 { MKTAG('f','t','y','p'), mov_read_ftyp },
3313 { MKTAG('g','l','b','l'), mov_read_glbl },
3314 { MKTAG('h','d','l','r'), mov_read_hdlr },
3315 { MKTAG('i','l','s','t'), mov_read_ilst },
3316 { MKTAG('j','p','2','h'), mov_read_jp2h },
3317 { MKTAG('m','d','a','t'), mov_read_mdat },
3318 { MKTAG('m','d','h','d'), mov_read_mdhd },
3319 { MKTAG('m','d','i','a'), mov_read_default },
3320 { MKTAG('m','e','t','a'), mov_read_meta },
3321 { MKTAG('m','i','n','f'), mov_read_default },
3322 { MKTAG('m','o','o','f'), mov_read_moof },
3323 { MKTAG('m','o','o','v'), mov_read_moov },
3324 { MKTAG('m','v','e','x'), mov_read_default },
3325 { MKTAG('m','v','h','d'), mov_read_mvhd },
3326 { MKTAG('S','M','I',' '), mov_read_svq3 },
3327 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3328 { MKTAG('a','v','c','C'), mov_read_glbl },
3329 { MKTAG('p','a','s','p'), mov_read_pasp },
3330 { MKTAG('s','t','b','l'), mov_read_default },
3331 { MKTAG('s','t','c','o'), mov_read_stco },
3332 { MKTAG('s','t','p','s'), mov_read_stps },
3333 { MKTAG('s','t','r','f'), mov_read_strf },
3334 { MKTAG('s','t','s','c'), mov_read_stsc },
3335 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3336 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3337 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3338 { MKTAG('s','t','t','s'), mov_read_stts },
3339 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3340 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3341 { MKTAG('t','f','d','t'), mov_read_tfdt },
3342 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3343 { MKTAG('t','r','a','k'), mov_read_trak },
3344 { MKTAG('t','r','a','f'), mov_read_default },
3345 { MKTAG('t','r','e','f'), mov_read_default },
3346 { MKTAG('t','m','c','d'), mov_read_tmcd },
3347 { MKTAG('c','h','a','p'), mov_read_chap },
3348 { MKTAG('t','r','e','x'), mov_read_trex },
3349 { MKTAG('t','r','u','n'), mov_read_trun },
3350 { MKTAG('u','d','t','a'), mov_read_default },
3351 { MKTAG('w','a','v','e'), mov_read_wave },
3352 { MKTAG('e','s','d','s'), mov_read_esds },
3353 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3354 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3355 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3356 { MKTAG('w','f','e','x'), mov_read_wfex },
3357 { MKTAG('c','m','o','v'), mov_read_cmov },
3358 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3359 { MKTAG('d','v','c','1'), mov_read_dvc1 },
3360 { MKTAG('s','b','g','p'), mov_read_sbgp },
3361 { MKTAG('h','v','c','C'), mov_read_glbl },
3362 { MKTAG('u','u','i','d'), mov_read_uuid },
3363 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
3364 { MKTAG('f','r','e','e'), mov_read_free },
3365 { MKTAG('-','-','-','-'), mov_read_custom },
3369 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3371 int64_t total_size = 0;
3376 atom.size = INT64_MAX;
3377 while (total_size + 8 <= atom.size && !avio_feof(pb)) {
3378 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3381 if (atom.size >= 8) {
3382 a.size = avio_rb32(pb);
3383 a.type = avio_rl32(pb);
3384 if (a.type == MKTAG('f','r','e','e') &&
3388 uint32_t *type = (uint32_t *)buf + 1;
3389 avio_read(pb, buf, 8);
3390 avio_seek(pb, -8, SEEK_CUR);
3391 if (*type == MKTAG('m','v','h','d') ||
3392 *type == MKTAG('c','m','o','v')) {
3393 av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
3394 a.type = MKTAG('m','o','o','v');
3397 if (atom.type != MKTAG('r','o','o','t') &&
3398 atom.type != MKTAG('m','o','o','v'))
3400 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
3402 av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
3408 if (a.size == 1) { /* 64 bit extended size */
3409 a.size = avio_rb64(pb) - 8;
3413 av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3414 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3416 a.size = atom.size - total_size + 8;
3421 a.size = FFMIN(a.size, atom.size - total_size);
3423 for (i = 0; mov_default_parse_table[i].type; i++)
3424 if (mov_default_parse_table[i].type == a.type) {
3425 parse = mov_default_parse_table[i].parse;
3429 // container is user data
3430 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3431 atom.type == MKTAG('i','l','s','t')))
3432 parse = mov_read_udta_string;
3434 if (!parse) { /* skip leaf atoms data */
3435 avio_skip(pb, a.size);
3437 int64_t start_pos = avio_tell(pb);
3439 int err = parse(c, pb, a);
3442 if (c->found_moov && c->found_mdat &&
3443 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3444 start_pos + a.size == avio_size(pb))) {
3445 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3446 c->next_root_atom = start_pos + a.size;
3449 left = a.size - avio_tell(pb) + start_pos;
3450 if (left > 0) /* skip garbage at atom end */
3451 avio_skip(pb, left);
3452 else if (left < 0) {
3453 av_log(c->fc, AV_LOG_WARNING,
3454 "overread end of atom '%.4s' by %"PRId64" bytes\n",
3455 (char*)&a.type, -left);
3456 avio_seek(pb, left, SEEK_CUR);
3460 total_size += a.size;
3463 if (total_size < atom.size && atom.size < 0x7ffff)
3464 avio_skip(pb, atom.size - total_size);
3469 static int mov_probe(AVProbeData *p)
3474 int moov_offset = -1;
3476 /* check file header */
3479 /* ignore invalid offset */
3480 if ((offset + 8) > (unsigned int)p->buf_size)
3482 tag = AV_RL32(p->buf + offset + 4);
3484 /* check for obvious tags */
3485 case MKTAG('m','o','o','v'):
3486 moov_offset = offset + 4;
3487 case MKTAG('m','d','a','t'):
3488 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3489 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3490 case MKTAG('f','t','y','p'):
3491 if (AV_RB32(p->buf+offset) < 8 &&
3492 (AV_RB32(p->buf+offset) != 1 ||
3493 offset + 12 > (unsigned int)p->buf_size ||
3494 AV_RB64(p->buf+offset + 8) == 0)) {
3495 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3496 } else if (tag == MKTAG('f','t','y','p') &&
3497 AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')) {
3498 score = FFMAX(score, 5);
3500 score = AVPROBE_SCORE_MAX;
3502 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3504 /* those are more common words, so rate then a bit less */
3505 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3506 case MKTAG('w','i','d','e'):
3507 case MKTAG('f','r','e','e'):
3508 case MKTAG('j','u','n','k'):
3509 case MKTAG('p','i','c','t'):
3510 score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
3511 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3513 case MKTAG(0x82,0x82,0x7f,0x7d):
3514 case MKTAG('s','k','i','p'):
3515 case MKTAG('u','u','i','d'):
3516 case MKTAG('p','r','f','l'):
3517 /* if we only find those cause probedata is too small at least rate them */
3518 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3519 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3522 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3525 if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
3526 /* moov atom in the header - we should make sure that this is not a
3527 * MOV-packed MPEG-PS */
3528 offset = moov_offset;
3530 while(offset < (p->buf_size - 16)){ /* Sufficient space */
3531 /* We found an actual hdlr atom */
3532 if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
3533 AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
3534 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
3535 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
3536 /* We found a media handler reference atom describing an
3537 * MPEG-PS-in-MOV, return a
3538 * low score to force expanding the probe window until
3539 * mpegps_probe finds what it needs */
3550 // must be done after parsing all trak because there's no order requirement
3551 static void mov_read_chapters(AVFormatContext *s)
3553 MOVContext *mov = s->priv_data;
3554 AVStream *st = NULL;
3555 MOVStreamContext *sc;
3559 for (i = 0; i < s->nb_streams; i++)
3560 if (s->streams[i]->id == mov->chapter_track) {
3565 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3569 st->discard = AVDISCARD_ALL;
3571 cur_pos = avio_tell(sc->pb);
3573 for (i = 0; i < st->nb_index_entries; i++) {
3574 AVIndexEntry *sample = &st->index_entries[i];
3575 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3580 if (end < sample->timestamp) {
3581 av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
3582 end = AV_NOPTS_VALUE;
3585 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3586 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3590 // the first two bytes are the length of the title
3591 len = avio_rb16(sc->pb);
3592 if (len > sample->size-2)
3594 title_len = 2*len + 1;
3595 if (!(title = av_mallocz(title_len)))
3598 // The samples could theoretically be in any encoding if there's an encd
3599 // atom following, but in practice are only utf-8 or utf-16, distinguished
3600 // instead by the presence of a BOM
3604 ch = avio_rb16(sc->pb);
3606 avio_get_str16be(sc->pb, len, title, title_len);
3607 else if (ch == 0xfffe)
3608 avio_get_str16le(sc->pb, len, title, title_len);
3611 if (len == 1 || len == 2)
3614 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
3618 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3622 avio_seek(sc->pb, cur_pos, SEEK_SET);
3625 static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
3626 uint32_t value, int flags)
3629 char buf[AV_TIMECODE_STR_SIZE];
3630 AVRational rate = {st->codec->time_base.den,
3631 st->codec->time_base.num};
3632 int ret = av_timecode_init(&tc, rate, flags, 0, s);
3635 av_dict_set(&st->metadata, "timecode",
3636 av_timecode_make_string(&tc, buf, value), 0);
3640 static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
3642 MOVStreamContext *sc = st->priv_data;
3644 int64_t cur_pos = avio_tell(sc->pb);
3647 if (!st->nb_index_entries)
3650 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
3651 value = avio_rb32(s->pb);
3653 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
3654 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
3655 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
3657 /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
3658 * not the case) and thus assume "frame number format" instead of QT one.
3659 * No sample with tmcd track can be found with a QT timecode at the moment,
3660 * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
3662 parse_timecode_in_framenum_format(s, st, value, flags);
3664 avio_seek(sc->pb, cur_pos, SEEK_SET);
3668 static int mov_read_close(AVFormatContext *s)
3670 MOVContext *mov = s->priv_data;
3673 for (i = 0; i < s->nb_streams; i++) {
3674 AVStream *st = s->streams[i];
3675 MOVStreamContext *sc = st->priv_data;
3677 av_freep(&sc->ctts_data);
3678 for (j = 0; j < sc->drefs_count; j++) {
3679 av_freep(&sc->drefs[j].path);
3680 av_freep(&sc->drefs[j].dir);
3682 av_freep(&sc->drefs);
3684 sc->drefs_count = 0;
3686 if (!sc->pb_is_copied)
3690 av_freep(&sc->chunk_offsets);
3691 av_freep(&sc->stsc_data);
3692 av_freep(&sc->sample_sizes);
3693 av_freep(&sc->keyframes);
3694 av_freep(&sc->stts_data);
3695 av_freep(&sc->stps_data);
3696 av_freep(&sc->rap_group);
3697 av_freep(&sc->display_matrix);
3700 if (mov->dv_demux) {
3701 avformat_free_context(mov->dv_fctx);
3702 mov->dv_fctx = NULL;
3705 av_freep(&mov->trex_data);
3706 av_freep(&mov->bitrates);
3708 for (i = 0; i < mov->fragment_index_count; i++) {
3709 MOVFragmentIndex* index = mov->fragment_index_data[i];
3710 av_freep(&index->items);
3711 av_freep(&mov->fragment_index_data[i]);
3713 av_freep(&mov->fragment_index_data);
3718 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
3722 for (i = 0; i < s->nb_streams; i++) {
3723 AVStream *st = s->streams[i];
3724 MOVStreamContext *sc = st->priv_data;
3726 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3727 sc->timecode_track == tmcd_id)
3733 /* look for a tmcd track not referenced by any video track, and export it globally */
3734 static void export_orphan_timecode(AVFormatContext *s)
3738 for (i = 0; i < s->nb_streams; i++) {
3739 AVStream *st = s->streams[i];
3741 if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
3742 !tmcd_is_referenced(s, i + 1)) {
3743 AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
3745 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
3752 static int read_tfra(MOVContext *mov, AVIOContext *f)
3754 MOVFragmentIndex* index = NULL;
3755 int version, fieldlength, i, j, err;
3756 int64_t pos = avio_tell(f);
3757 uint32_t size = avio_rb32(f);
3758 if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
3761 av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
3762 index = av_mallocz(sizeof(MOVFragmentIndex));
3764 return AVERROR(ENOMEM);
3766 mov->fragment_index_count++;
3767 if ((err = av_reallocp(&mov->fragment_index_data,
3768 mov->fragment_index_count *
3769 sizeof(MOVFragmentIndex*))) < 0) {
3773 mov->fragment_index_data[mov->fragment_index_count - 1] =
3776 version = avio_r8(f);
3778 index->track_id = avio_rb32(f);
3779 fieldlength = avio_rb32(f);
3780 index->item_count = avio_rb32(f);
3781 index->items = av_mallocz(
3782 index->item_count * sizeof(MOVFragmentIndexItem));
3783 if (!index->items) {
3784 return AVERROR(ENOMEM);
3786 for (i = 0; i < index->item_count; i++) {
3787 int64_t time, offset;
3789 time = avio_rb64(f);
3790 offset = avio_rb64(f);
3792 time = avio_rb32(f);
3793 offset = avio_rb32(f);
3795 index->items[i].time = time;
3796 index->items[i].moof_offset = offset;
3797 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
3799 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
3801 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
3805 avio_seek(f, pos + size, SEEK_SET);
3809 static int mov_read_mfra(MOVContext *c, AVIOContext *f)
3811 int64_t stream_size = avio_size(f);
3812 int64_t original_pos = avio_tell(f);
3815 if ((ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) goto fail;
3816 mfra_size = avio_rb32(f);
3817 if (mfra_size < 0 || mfra_size > stream_size) {
3818 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
3822 if ((ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) goto fail;
3823 if (avio_rb32(f) != mfra_size) {
3824 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
3828 if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
3829 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
3832 av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
3833 while (!read_tfra(c, f)) {
3837 ret = avio_seek(f, original_pos, SEEK_SET);
3839 av_log(c->fc, AV_LOG_ERROR,
3840 "failed to seek back after looking for mfra\n");
3846 static int mov_read_header(AVFormatContext *s)
3848 MOVContext *mov = s->priv_data;
3849 AVIOContext *pb = s->pb;
3851 MOVAtom atom = { AV_RL32("root") };
3855 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3857 atom.size = avio_size(pb);
3859 atom.size = INT64_MAX;
3861 /* check MOV header */
3863 if (mov->moov_retry)
3864 avio_seek(pb, 0, SEEK_SET);
3865 if ((err = mov_read_default(mov, pb, atom)) < 0) {
3866 av_log(s, AV_LOG_ERROR, "error reading header\n");
3870 } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
3871 if (!mov->found_moov) {
3872 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3874 return AVERROR_INVALIDDATA;
3876 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3879 if (mov->chapter_track > 0)
3880 mov_read_chapters(s);
3881 for (i = 0; i < s->nb_streams; i++)
3882 if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
3883 mov_read_timecode_track(s, s->streams[i]);
3886 /* copy timecode metadata from tmcd tracks to the related video streams */
3887 for (i = 0; i < s->nb_streams; i++) {
3888 AVStream *st = s->streams[i];
3889 MOVStreamContext *sc = st->priv_data;
3890 if (sc->timecode_track > 0) {
3891 AVDictionaryEntry *tcr;
3892 int tmcd_st_id = -1;
3894 for (j = 0; j < s->nb_streams; j++)
3895 if (s->streams[j]->id == sc->timecode_track)
3898 if (tmcd_st_id < 0 || tmcd_st_id == i)
3900 tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
3902 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
3905 export_orphan_timecode(s);
3907 for (i = 0; i < s->nb_streams; i++) {
3908 AVStream *st = s->streams[i];
3909 MOVStreamContext *sc = st->priv_data;
3910 fix_timescale(mov, sc);
3911 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
3912 st->skip_samples = sc->start_pad;
3914 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
3915 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3916 sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
3917 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3918 if (st->codec->width <= 0 && st->codec->height <= 0) {
3919 st->codec->width = sc->width;
3920 st->codec->height = sc->height;
3922 if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
3923 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
3929 if (mov->trex_data) {
3930 for (i = 0; i < s->nb_streams; i++) {
3931 AVStream *st = s->streams[i];
3932 MOVStreamContext *sc = st->priv_data;
3933 if (st->duration > 0)
3934 st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3938 for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
3939 if (mov->bitrates[i]) {
3940 s->streams[i]->codec->bit_rate = mov->bitrates[i];
3944 ff_rfps_calculate(s);
3946 for (i = 0; i < s->nb_streams; i++) {
3947 AVStream *st = s->streams[i];
3948 MOVStreamContext *sc = st->priv_data;
3950 switch (st->codec->codec_type) {
3951 case AVMEDIA_TYPE_AUDIO:
3952 err = ff_replaygain_export(st, s->metadata);
3958 case AVMEDIA_TYPE_VIDEO:
3959 if (sc->display_matrix) {
3960 AVPacketSideData *sd, *tmp;
3962 tmp = av_realloc_array(st->side_data,
3963 st->nb_side_data + 1, sizeof(*tmp));
3965 return AVERROR(ENOMEM);
3967 st->side_data = tmp;
3970 sd = &st->side_data[st->nb_side_data - 1];
3971 sd->type = AV_PKT_DATA_DISPLAYMATRIX;
3972 sd->size = sizeof(int32_t) * 9;
3973 sd->data = (uint8_t*)sc->display_matrix;
3974 sc->display_matrix = NULL;
3983 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
3985 AVIndexEntry *sample = NULL;
3986 int64_t best_dts = INT64_MAX;
3988 for (i = 0; i < s->nb_streams; i++) {
3989 AVStream *avst = s->streams[i];
3990 MOVStreamContext *msc = avst->priv_data;
3991 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3992 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3993 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3994 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3995 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3997 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3998 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3999 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
4000 sample = current_sample;
4009 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
4011 MOVContext *mov = s->priv_data;
4012 MOVStreamContext *sc;
4013 AVIndexEntry *sample;
4014 AVStream *st = NULL;
4018 sample = mov_find_next_sample(s, &st);
4020 mov->found_mdat = 0;
4021 if (!mov->next_root_atom)
4023 avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
4024 mov->next_root_atom = 0;
4025 if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
4028 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
4032 /* must be done just before reading, to avoid infinite loop on sample */
4033 sc->current_sample++;
4035 if (mov->next_root_atom) {
4036 sample->pos = FFMIN(sample->pos, mov->next_root_atom);
4037 sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
4040 if (st->discard != AVDISCARD_ALL) {
4041 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
4042 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
4043 sc->ffindex, sample->pos);
4044 return AVERROR_INVALIDDATA;
4046 ret = av_get_packet(sc->pb, pkt, sample->size);
4049 if (sc->has_palette) {
4052 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
4054 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
4056 memcpy(pal, sc->palette, AVPALETTE_SIZE);
4057 sc->has_palette = 0;
4060 #if CONFIG_DV_DEMUXER
4061 if (mov->dv_demux && sc->dv_audio_container) {
4062 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
4065 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
4072 pkt->stream_index = sc->ffindex;
4073 pkt->dts = sample->timestamp;
4074 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
4075 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
4076 /* update ctts context */
4078 if (sc->ctts_index < sc->ctts_count &&
4079 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
4081 sc->ctts_sample = 0;
4084 pkt->dts = AV_NOPTS_VALUE;
4086 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
4087 st->index_entries[sc->current_sample].timestamp : st->duration;
4088 pkt->duration = next_dts - pkt->dts;
4089 pkt->pts = pkt->dts;
4091 if (st->discard == AVDISCARD_ALL)
4093 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
4094 pkt->pos = sample->pos;
4095 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
4096 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
4100 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
4102 MOVStreamContext *sc = st->priv_data;
4103 int sample, time_sample;
4106 sample = av_index_search_timestamp(st, timestamp, flags);
4107 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
4108 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
4110 if (sample < 0) /* not sure what to do */
4111 return AVERROR_INVALIDDATA;
4112 sc->current_sample = sample;
4113 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
4114 /* adjust ctts index */
4115 if (sc->ctts_data) {
4117 for (i = 0; i < sc->ctts_count; i++) {
4118 int next = time_sample + sc->ctts_data[i].count;
4119 if (next > sc->current_sample) {
4121 sc->ctts_sample = sc->current_sample - time_sample;
4130 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
4133 int64_t seek_timestamp, timestamp;
4137 if (stream_index >= s->nb_streams)
4138 return AVERROR_INVALIDDATA;
4140 st = s->streams[stream_index];
4141 sample = mov_seek_stream(s, st, sample_time, flags);
4145 /* adjust seek timestamp to found sample timestamp */
4146 seek_timestamp = st->index_entries[sample].timestamp;
4148 for (i = 0; i < s->nb_streams; i++) {
4149 MOVStreamContext *sc = s->streams[i]->priv_data;
4151 st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
4153 if (stream_index == i)
4156 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
4157 mov_seek_stream(s, st, timestamp, flags);
4162 static const AVOption options[] = {
4163 {"use_absolute_path",
4164 "allow using absolute path when opening alias, this is a possible security issue",
4165 offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
4166 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4167 {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
4168 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4170 "use mfra for fragment timestamps",
4171 offsetof(MOVContext, use_mfra_for), FF_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
4172 -1, FF_MOV_FLAG_MFRA_PTS, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM,
4174 {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
4175 AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM, "use_mfra_for" },
4176 {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
4177 AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM, "use_mfra_for" },
4178 {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
4179 AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM, "use_mfra_for" },
4183 static const AVClass mov_class = {
4184 .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
4185 .item_name = av_default_item_name,
4187 .version = LIBAVUTIL_VERSION_INT,
4190 AVInputFormat ff_mov_demuxer = {
4191 .name = "mov,mp4,m4a,3gp,3g2,mj2",
4192 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4193 .priv_data_size = sizeof(MOVContext),
4194 .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
4195 .read_probe = mov_probe,
4196 .read_header = mov_read_header,
4197 .read_packet = mov_read_packet,
4198 .read_close = mov_read_close,
4199 .read_seek = mov_read_seek,
4200 .priv_class = &mov_class,
4201 .flags = AVFMT_NO_BYTE_SEEK,