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);
72 static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
73 unsigned len, const char *key)
77 short current, total = 0;
78 avio_rb16(pb); // unknown
79 current = avio_rb16(pb);
81 total = avio_rb16(pb);
83 snprintf(buf, sizeof(buf), "%d", current);
85 snprintf(buf, sizeof(buf), "%d/%d", current, total);
86 av_dict_set(&c->fc->metadata, key, buf, 0);
91 static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
92 unsigned len, const char *key)
96 /* bypass padding bytes */
101 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
102 av_dict_set(&c->fc->metadata, key, buf, 0);
107 static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
108 unsigned len, const char *key)
112 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
113 av_dict_set(&c->fc->metadata, key, buf, 0);
118 static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
119 unsigned len, const char *key)
124 avio_r8(pb); // unknown
127 if (genre < 1 || genre > ID3v1_GENRE_MAX)
129 snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
130 av_dict_set(&c->fc->metadata, key, buf, 0);
135 static const uint32_t mac_to_unicode[128] = {
136 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
137 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
138 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
139 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
140 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
141 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
142 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
143 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
144 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
145 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
146 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
147 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
148 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
149 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
150 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
151 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
154 static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
155 char *dst, int dstlen)
158 char *end = dst+dstlen-1;
161 for (i = 0; i < len; i++) {
162 uint8_t t, c = avio_r8(pb);
163 if (c < 0x80 && p < end)
166 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
172 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
176 MOVStreamContext *sc;
181 case 0xd: id = AV_CODEC_ID_MJPEG; break;
182 case 0xe: id = AV_CODEC_ID_PNG; break;
183 case 0x1b: id = AV_CODEC_ID_BMP; break;
185 av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
190 st = avformat_new_stream(c->fc, NULL);
192 return AVERROR(ENOMEM);
193 sc = av_mallocz(sizeof(*sc));
195 return AVERROR(ENOMEM);
198 ret = av_get_packet(pb, &pkt, len);
202 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
204 st->attached_pic = pkt;
205 st->attached_pic.stream_index = st->index;
206 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
208 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
209 st->codec->codec_id = id;
214 static int mov_metadata_raw(MOVContext *c, AVIOContext *pb,
215 unsigned len, const char *key)
217 char *value = av_malloc(len + 1);
219 return AVERROR(ENOMEM);
220 avio_read(pb, value, len);
222 return av_dict_set(&c->fc->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
225 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
227 char language[4] = { 0 };
229 uint16_t langcode = 0;
230 double longitude, latitude;
231 const char *key = "location";
233 if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4)
234 return AVERROR_INVALIDDATA;
236 avio_skip(pb, 4); // version+flags
237 langcode = avio_rb16(pb);
238 ff_mov_lang_to_iso639(langcode, language);
241 len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name
243 return AVERROR_INVALIDDATA;
244 avio_skip(pb, 1); // role
248 return AVERROR_INVALIDDATA;
249 longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
250 latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
252 // Try to output in the same format as the ?xyz field
253 snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude);
254 if (*language && strcmp(language, "und")) {
256 snprintf(key2, sizeof(key2), "%s-%s", key, language);
257 av_dict_set(&c->fc->metadata, key2, buf, 0);
259 return av_dict_set(&c->fc->metadata, key, buf, 0);
262 static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
264 #ifdef MOV_EXPORT_ALL_METADATA
267 char str[1024], key2[16], language[4] = {0};
268 const char *key = NULL;
269 uint16_t langcode = 0;
270 uint32_t data_type = 0, str_size;
271 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
274 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
275 case MKTAG(0xa9,'a','u','t'):
276 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
277 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
278 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
279 case MKTAG( 'c','p','i','l'): key = "compilation";
280 parse = mov_metadata_int8_no_padding; break;
281 case MKTAG( 'c','p','r','t'):
282 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
283 case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
284 case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
285 case MKTAG(0xa9,'c','m','t'):
286 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
287 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
288 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
289 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
290 case MKTAG( 'g','n','r','e'): key = "genre";
291 parse = mov_metadata_gnre; break;
292 case MKTAG(0xa9,'t','o','o'):
293 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
294 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
295 case MKTAG(0xa9,'m','a','k'): key = "make"; break;
296 case MKTAG(0xa9,'m','o','d'): key = "model"; break;
297 case MKTAG(0xa9,'x','y','z'): key = "location"; break;
298 case MKTAG( 'd','e','s','c'): key = "description";break;
299 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
300 case MKTAG( 't','v','s','h'): key = "show"; break;
301 case MKTAG( 't','v','e','n'): key = "episode_id";break;
302 case MKTAG( 't','v','n','n'): key = "network"; break;
303 case MKTAG( 't','r','k','n'): key = "track";
304 parse = mov_metadata_track_or_disc_number; break;
305 case MKTAG( 'd','i','s','k'): key = "disc";
306 parse = mov_metadata_track_or_disc_number; break;
307 case MKTAG( 't','v','e','s'): key = "episode_sort";
308 parse = mov_metadata_int8_bypass_padding; break;
309 case MKTAG( 't','v','s','n'): key = "season_number";
310 parse = mov_metadata_int8_bypass_padding; break;
311 case MKTAG( 's','t','i','k'): key = "media_type";
312 parse = mov_metadata_int8_no_padding; break;
313 case MKTAG( 'h','d','v','d'): key = "hd_video";
314 parse = mov_metadata_int8_no_padding; break;
315 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
316 parse = mov_metadata_int8_no_padding; break;
317 case MKTAG( '@','P','R','M'):
318 return mov_metadata_raw(c, pb, atom.size, "premiere_version");
319 case MKTAG( '@','P','R','Q'):
320 return mov_metadata_raw(c, pb, atom.size, "quicktime_version");
321 case MKTAG( 'l','o','c','i'):
322 return mov_metadata_loci(c, pb, atom.size);
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 av_dict_set(&c->fc->metadata, key, str, 0);
376 if (*language && strcmp(language, "und")) {
377 snprintf(key2, sizeof(key2), "%s-%s", key, language);
378 av_dict_set(&c->fc->metadata, key2, str, 0);
381 av_dlog(c->fc, "lang \"%3s\" ", language);
382 av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
383 key, str, (char*)&atom.type, str_size, atom.size);
388 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
391 int i, nb_chapters, str_len, version;
394 if ((atom.size -= 5) < 0)
397 version = avio_r8(pb);
400 avio_rb32(pb); // ???
401 nb_chapters = avio_r8(pb);
403 for (i = 0; i < nb_chapters; i++) {
407 start = avio_rb64(pb);
408 str_len = avio_r8(pb);
410 if ((atom.size -= 9+str_len) < 0)
413 avio_read(pb, str, str_len);
415 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
420 #define MIN_DATA_ENTRY_BOX_SIZE 12
421 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
424 MOVStreamContext *sc;
427 if (c->fc->nb_streams < 1)
429 st = c->fc->streams[c->fc->nb_streams-1];
432 avio_rb32(pb); // version + flags
433 entries = avio_rb32(pb);
434 if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
435 entries >= UINT_MAX / sizeof(*sc->drefs))
436 return AVERROR_INVALIDDATA;
439 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
441 return AVERROR(ENOMEM);
442 sc->drefs_count = entries;
444 for (i = 0; i < sc->drefs_count; i++) {
445 MOVDref *dref = &sc->drefs[i];
446 uint32_t size = avio_rb32(pb);
447 int64_t next = avio_tell(pb) + size - 4;
450 return AVERROR_INVALIDDATA;
452 dref->type = avio_rl32(pb);
453 avio_rb32(pb); // version + flags
454 av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
456 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
457 /* macintosh alias record */
458 uint16_t volume_len, len;
463 volume_len = avio_r8(pb);
464 volume_len = FFMIN(volume_len, 27);
465 avio_read(pb, dref->volume, 27);
466 dref->volume[volume_len] = 0;
467 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
472 len = FFMIN(len, 63);
473 avio_read(pb, dref->filename, 63);
474 dref->filename[len] = 0;
475 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
479 /* read next level up_from_alias/down_to_target */
480 dref->nlvl_from = avio_rb16(pb);
481 dref->nlvl_to = avio_rb16(pb);
482 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
483 dref->nlvl_from, dref->nlvl_to);
487 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
490 type = avio_rb16(pb);
492 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
495 if (type == 2) { // absolute path
497 dref->path = av_mallocz(len+1);
499 return AVERROR(ENOMEM);
500 avio_read(pb, dref->path, len);
501 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
503 memmove(dref->path, dref->path+volume_len, len);
506 for (j = 0; j < len; j++)
507 if (dref->path[j] == ':')
509 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
510 } else if (type == 0) { // directory name
512 dref->dir = av_malloc(len+1);
514 return AVERROR(ENOMEM);
515 if (avio_read(pb, dref->dir, len) != len)
516 return AVERROR_INVALIDDATA;
518 for (j = 0; j < len; j++)
519 if (dref->dir[j] == ':')
521 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
526 avio_seek(pb, next, SEEK_SET);
531 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
535 uint32_t av_unused ctype;
539 if (c->fc->nb_streams < 1) // meta before first trak
542 st = c->fc->streams[c->fc->nb_streams-1];
544 avio_r8(pb); /* version */
545 avio_rb24(pb); /* flags */
548 ctype = avio_rl32(pb);
549 type = avio_rl32(pb); /* component subtype */
551 av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
552 av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
554 if (type == MKTAG('v','i','d','e'))
555 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
556 else if (type == MKTAG('s','o','u','n'))
557 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
558 else if (type == MKTAG('m','1','a',' '))
559 st->codec->codec_id = AV_CODEC_ID_MP2;
560 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
561 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
563 avio_rb32(pb); /* component manufacture */
564 avio_rb32(pb); /* component flags */
565 avio_rb32(pb); /* component flags mask */
567 title_size = atom.size - 24;
568 if (title_size > 0) {
569 title_str = av_malloc(title_size + 1); /* Add null terminator */
571 return AVERROR(ENOMEM);
572 avio_read(pb, title_str, title_size);
573 title_str[title_size] = 0;
575 av_dict_set(&st->metadata, "handler_name", title_str +
576 (!c->isom && title_str[0] == title_size - 1), 0);
577 av_freep(&title_str);
583 int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
588 if (fc->nb_streams < 1)
590 st = fc->streams[fc->nb_streams-1];
592 avio_rb32(pb); /* version + flags */
593 ff_mp4_read_descr(fc, pb, &tag);
594 if (tag == MP4ESDescrTag) {
595 ff_mp4_parse_es_descr(pb, NULL);
597 avio_rb16(pb); /* ID */
599 ff_mp4_read_descr(fc, pb, &tag);
600 if (tag == MP4DecConfigDescrTag)
601 ff_mp4_read_dec_config_descr(fc, st, pb);
605 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
607 return ff_mov_read_esds(c->fc, pb, atom);
610 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
613 int ac3info, acmod, lfeon, bsmod;
615 if (c->fc->nb_streams < 1)
617 st = c->fc->streams[c->fc->nb_streams-1];
619 ac3info = avio_rb24(pb);
620 bsmod = (ac3info >> 14) & 0x7;
621 acmod = (ac3info >> 11) & 0x7;
622 lfeon = (ac3info >> 10) & 0x1;
623 st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
624 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
626 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
627 st->codec->audio_service_type = bsmod;
628 if (st->codec->channels > 1 && bsmod == 0x7)
629 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
634 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
637 int eac3info, acmod, lfeon, bsmod;
639 if (c->fc->nb_streams < 1)
641 st = c->fc->streams[c->fc->nb_streams-1];
643 /* No need to parse fields for additional independent substreams and its
644 * associated dependent substreams since libavcodec's E-AC-3 decoder
645 * does not support them yet. */
646 avio_rb16(pb); /* data_rate and num_ind_sub */
647 eac3info = avio_rb24(pb);
648 bsmod = (eac3info >> 12) & 0x1f;
649 acmod = (eac3info >> 9) & 0x7;
650 lfeon = (eac3info >> 8) & 0x1;
651 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
653 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
654 st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
655 st->codec->audio_service_type = bsmod;
656 if (st->codec->channels > 1 && bsmod == 0x7)
657 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
662 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
666 if (c->fc->nb_streams < 1)
668 st = c->fc->streams[c->fc->nb_streams-1];
673 /* skip version and flags */
676 ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
681 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
685 if (c->fc->nb_streams < 1)
687 st = c->fc->streams[c->fc->nb_streams-1];
689 if (ff_get_wav_header(pb, st->codec, atom.size) < 0) {
690 av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
696 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
698 const int num = avio_rb32(pb);
699 const int den = avio_rb32(pb);
702 if (c->fc->nb_streams < 1)
704 st = c->fc->streams[c->fc->nb_streams-1];
706 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
707 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
708 av_log(c->fc, AV_LOG_WARNING,
709 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
710 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
712 } else if (den != 0) {
713 st->sample_aspect_ratio.num = num;
714 st->sample_aspect_ratio.den = den;
719 /* this atom contains actual media data */
720 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
722 if (atom.size == 0) /* wrong one (MP4) */
725 return 0; /* now go for moov */
728 /* read major brand, minor version and compatible brands and store them as metadata */
729 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
733 char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
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 snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
744 av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
746 comp_brand_size = atom.size - 8;
747 if (comp_brand_size < 0)
748 return AVERROR_INVALIDDATA;
749 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
750 if (!comp_brands_str)
751 return AVERROR(ENOMEM);
752 avio_read(pb, comp_brands_str, comp_brand_size);
753 comp_brands_str[comp_brand_size] = 0;
754 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
755 av_freep(&comp_brands_str);
760 /* this atom should contain all header atoms */
761 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
766 av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
767 avio_skip(pb, atom.size);
771 if ((ret = mov_read_default(c, pb, atom)) < 0)
773 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
774 /* so we don't parse the whole file if over a network */
776 return 0; /* now go for mdat */
779 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
781 c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
782 av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
783 return mov_read_default(c, pb, atom);
786 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
792 if(time >= 2082844800)
793 time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
795 ptm = gmtime(&timet);
797 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
798 av_dict_set(metadata, "creation_time", buffer, 0);
802 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
805 MOVStreamContext *sc;
807 char language[4] = {0};
809 int64_t creation_time;
811 if (c->fc->nb_streams < 1)
813 st = c->fc->streams[c->fc->nb_streams-1];
816 if (sc->time_scale) {
817 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
818 return AVERROR_INVALIDDATA;
821 version = avio_r8(pb);
823 avpriv_request_sample(c->fc, "Version %d", version);
824 return AVERROR_PATCHWELCOME;
826 avio_rb24(pb); /* flags */
828 creation_time = avio_rb64(pb);
831 creation_time = avio_rb32(pb);
832 avio_rb32(pb); /* modification time */
834 mov_metadata_creation_time(&st->metadata, creation_time);
836 sc->time_scale = avio_rb32(pb);
837 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
839 lang = avio_rb16(pb); /* language */
840 if (ff_mov_lang_to_iso639(lang, language))
841 av_dict_set(&st->metadata, "language", language, 0);
842 avio_rb16(pb); /* quality */
847 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
849 int64_t creation_time;
850 int version = avio_r8(pb); /* version */
851 avio_rb24(pb); /* flags */
854 creation_time = avio_rb64(pb);
857 creation_time = avio_rb32(pb);
858 avio_rb32(pb); /* modification time */
860 mov_metadata_creation_time(&c->fc->metadata, creation_time);
861 c->time_scale = avio_rb32(pb); /* time scale */
863 av_dlog(c->fc, "time scale = %i\n", c->time_scale);
865 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
866 // set the AVCodecContext duration because the duration of individual tracks
868 if (c->time_scale > 0 && !c->trex_data)
869 c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
870 avio_rb32(pb); /* preferred scale */
872 avio_rb16(pb); /* preferred volume */
874 avio_skip(pb, 10); /* reserved */
876 avio_skip(pb, 36); /* display matrix */
878 avio_rb32(pb); /* preview time */
879 avio_rb32(pb); /* preview duration */
880 avio_rb32(pb); /* poster time */
881 avio_rb32(pb); /* selection time */
882 avio_rb32(pb); /* selection duration */
883 avio_rb32(pb); /* current time */
884 avio_rb32(pb); /* next track ID */
888 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
893 if (c->fc->nb_streams < 1)
895 st = c->fc->streams[c->fc->nb_streams-1];
897 little_endian = avio_rb16(pb) & 0xFF;
898 av_dlog(c->fc, "enda %d\n", little_endian);
899 if (little_endian == 1) {
900 switch (st->codec->codec_id) {
901 case AV_CODEC_ID_PCM_S24BE:
902 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
904 case AV_CODEC_ID_PCM_S32BE:
905 st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
907 case AV_CODEC_ID_PCM_F32BE:
908 st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
910 case AV_CODEC_ID_PCM_F64BE:
911 st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
920 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
923 unsigned mov_field_order;
924 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
926 if (c->fc->nb_streams < 1) // will happen with jp2 files
928 st = c->fc->streams[c->fc->nb_streams-1];
930 return AVERROR_INVALIDDATA;
931 mov_field_order = avio_rb16(pb);
932 if ((mov_field_order & 0xFF00) == 0x0100)
933 decoded_field_order = AV_FIELD_PROGRESSIVE;
934 else if ((mov_field_order & 0xFF00) == 0x0200) {
935 switch (mov_field_order & 0xFF) {
936 case 0x01: decoded_field_order = AV_FIELD_TT;
938 case 0x06: decoded_field_order = AV_FIELD_BB;
940 case 0x09: decoded_field_order = AV_FIELD_TB;
942 case 0x0E: decoded_field_order = AV_FIELD_BT;
946 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
947 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
949 st->codec->field_order = decoded_field_order;
954 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
955 static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
956 enum AVCodecID codec_id)
963 if (c->fc->nb_streams < 1) // will happen with jp2 files
965 st= c->fc->streams[c->fc->nb_streams-1];
967 if (st->codec->codec_id != codec_id)
968 return 0; /* unexpected codec_id - don't mess with extradata */
970 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
971 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
972 return AVERROR_INVALIDDATA;
973 if ((err = av_reallocp(&st->codec->extradata, size)) < 0) {
974 st->codec->extradata_size = 0;
977 buf = st->codec->extradata + st->codec->extradata_size;
978 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
979 AV_WB32( buf , atom.size + 8);
980 AV_WL32( buf + 4, atom.type);
981 err = avio_read(pb, buf + 8, atom.size);
984 } else if (err < atom.size) {
985 av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
986 st->codec->extradata_size -= atom.size - err;
988 memset(buf + 8 + err, 0, FF_INPUT_BUFFER_PADDING_SIZE);
992 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
993 static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
995 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
998 static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1000 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1003 static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1005 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1008 static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1010 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1013 static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1015 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1017 if (!ret && c->fc->nb_streams >= 1) {
1018 AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1019 if (avctx->extradata_size >= 40) {
1020 avctx->height = AV_RB16(&avctx->extradata[36]);
1021 avctx->width = AV_RB16(&avctx->extradata[38]);
1027 static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1029 if (c->fc->nb_streams >= 1) {
1030 AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1031 if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1032 codec->codec_id == AV_CODEC_ID_H264 &&
1035 /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1036 if (avio_rb16(pb) == 0xd4d)
1037 codec->width = 1440;
1042 return mov_read_avid(c, pb, atom);
1045 static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1047 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1050 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1054 if (c->fc->nb_streams < 1)
1056 st = c->fc->streams[c->fc->nb_streams-1];
1058 if ((uint64_t)atom.size > (1<<30))
1059 return AVERROR_INVALIDDATA;
1061 if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1062 st->codec->codec_id == AV_CODEC_ID_QDMC ||
1063 st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1064 // pass all frma atom to codec, needed at least for QDMC and QDM2
1065 av_free(st->codec->extradata);
1066 if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1067 return AVERROR(ENOMEM);
1068 } else if (atom.size > 8) { /* to read frma, esds atoms */
1070 if ((ret = mov_read_default(c, pb, atom)) < 0)
1073 avio_skip(pb, atom.size);
1078 * This function reads atom content and puts data in extradata without tag
1079 * nor size unlike mov_read_extradata.
1081 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1085 if (c->fc->nb_streams < 1)
1087 st = c->fc->streams[c->fc->nb_streams-1];
1089 if ((uint64_t)atom.size > (1<<30))
1090 return AVERROR_INVALIDDATA;
1092 if (atom.size >= 10) {
1093 // Broken files created by legacy versions of libavformat will
1094 // wrap a whole fiel atom inside of a glbl atom.
1095 unsigned size = avio_rb32(pb);
1096 unsigned type = avio_rl32(pb);
1097 avio_seek(pb, -8, SEEK_CUR);
1098 if (type == MKTAG('f','i','e','l') && size == atom.size)
1099 return mov_read_default(c, pb, atom);
1101 av_free(st->codec->extradata);
1102 if (ff_get_extradata(st->codec, pb, atom.size) < 0)
1103 return AVERROR(ENOMEM);
1108 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1111 uint8_t profile_level;
1114 if (c->fc->nb_streams < 1)
1116 st = c->fc->streams[c->fc->nb_streams-1];
1118 if (atom.size >= (1<<28) || atom.size < 7)
1119 return AVERROR_INVALIDDATA;
1121 profile_level = avio_r8(pb);
1122 if ((profile_level & 0xf0) != 0xc0)
1125 avio_seek(pb, 6, SEEK_CUR);
1126 av_free(st->codec->extradata);
1127 if ((ret = ff_get_extradata(st->codec, pb, atom.size - 7)) < 0)
1134 * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1135 * but can have extradata appended at the end after the 40 bytes belonging
1138 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1142 if (c->fc->nb_streams < 1)
1144 if (atom.size <= 40)
1146 st = c->fc->streams[c->fc->nb_streams-1];
1148 if ((uint64_t)atom.size > (1<<30))
1149 return AVERROR_INVALIDDATA;
1152 av_free(st->codec->extradata);
1153 if (ff_get_extradata(st->codec, pb, atom.size - 40) < 0)
1154 return AVERROR(ENOMEM);
1158 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1161 MOVStreamContext *sc;
1162 unsigned int i, entries;
1164 if (c->fc->nb_streams < 1)
1166 st = c->fc->streams[c->fc->nb_streams-1];
1169 avio_r8(pb); /* version */
1170 avio_rb24(pb); /* flags */
1172 entries = avio_rb32(pb);
1176 if (entries >= UINT_MAX/sizeof(int64_t))
1177 return AVERROR_INVALIDDATA;
1179 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
1180 if (!sc->chunk_offsets)
1181 return AVERROR(ENOMEM);
1182 sc->chunk_count = entries;
1184 if (atom.type == MKTAG('s','t','c','o'))
1185 for (i = 0; i < entries && !pb->eof_reached; i++)
1186 sc->chunk_offsets[i] = avio_rb32(pb);
1187 else if (atom.type == MKTAG('c','o','6','4'))
1188 for (i = 0; i < entries && !pb->eof_reached; i++)
1189 sc->chunk_offsets[i] = avio_rb64(pb);
1191 return AVERROR_INVALIDDATA;
1193 sc->chunk_count = i;
1195 if (pb->eof_reached)
1202 * Compute codec id for 'lpcm' tag.
1203 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1205 enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1212 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1215 static int mov_codec_id(AVStream *st, uint32_t format)
1217 int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1220 ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1221 (format & 0xFFFF) == 'T' + ('S' << 8)))
1222 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1224 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1225 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1226 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1227 /* skip old asf mpeg4 tag */
1228 format && format != MKTAG('m','p','4','s')) {
1229 id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1231 id = ff_codec_get_id(ff_codec_bmp_tags, format);
1233 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1234 else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1235 (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1236 st->codec->codec_id == AV_CODEC_ID_NONE)) {
1237 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1239 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1243 st->codec->codec_tag = format;
1248 static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1249 AVStream *st, MOVStreamContext *sc)
1251 uint8_t codec_name[32];
1252 unsigned int color_depth, len, j;
1253 int color_greyscale;
1256 avio_rb16(pb); /* version */
1257 avio_rb16(pb); /* revision level */
1258 avio_rb32(pb); /* vendor */
1259 avio_rb32(pb); /* temporal quality */
1260 avio_rb32(pb); /* spatial quality */
1262 st->codec->width = avio_rb16(pb); /* width */
1263 st->codec->height = avio_rb16(pb); /* height */
1265 avio_rb32(pb); /* horiz resolution */
1266 avio_rb32(pb); /* vert resolution */
1267 avio_rb32(pb); /* data size, always 0 */
1268 avio_rb16(pb); /* frames per samples */
1270 len = avio_r8(pb); /* codec name, pascal string */
1273 mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1275 avio_skip(pb, 31 - len);
1278 av_dict_set(&st->metadata, "encoder", codec_name, 0);
1280 /* codec_tag YV12 triggers an UV swap in rawdec.c */
1281 if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1282 st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1283 st->codec->width &= ~1;
1284 st->codec->height &= ~1;
1286 /* Flash Media Server uses tag H263 with Sorenson Spark */
1287 if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1288 !memcmp(codec_name, "Sorenson H263", 13))
1289 st->codec->codec_id = AV_CODEC_ID_FLV1;
1291 st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1292 color_table_id = avio_rb16(pb); /* colortable id */
1293 av_dlog(c->fc, "depth %d, ctab id %d\n",
1294 st->codec->bits_per_coded_sample, color_table_id);
1295 /* figure out the palette situation */
1296 color_depth = st->codec->bits_per_coded_sample & 0x1F;
1297 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1298 /* Do not create a greyscale palette for cinepak */
1299 if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
1302 /* if the depth is 2, 4, or 8 bpp, file is palettized */
1303 if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1304 /* for palette traversal */
1305 unsigned int color_start, color_count, color_end;
1306 unsigned char a, r, g, b;
1308 if (color_greyscale) {
1309 int color_index, color_dec;
1310 /* compute the greyscale palette */
1311 st->codec->bits_per_coded_sample = color_depth;
1312 color_count = 1 << color_depth;
1314 color_dec = 256 / (color_count - 1);
1315 for (j = 0; j < color_count; j++) {
1316 r = g = b = color_index;
1317 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1318 color_index -= color_dec;
1319 if (color_index < 0)
1322 } else if (color_table_id) {
1323 const uint8_t *color_table;
1324 /* if flag bit 3 is set, use the default palette */
1325 color_count = 1 << color_depth;
1326 if (color_depth == 2)
1327 color_table = ff_qt_default_palette_4;
1328 else if (color_depth == 4)
1329 color_table = ff_qt_default_palette_16;
1331 color_table = ff_qt_default_palette_256;
1333 for (j = 0; j < color_count; j++) {
1334 r = color_table[j * 3 + 0];
1335 g = color_table[j * 3 + 1];
1336 b = color_table[j * 3 + 2];
1337 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1340 /* load the palette from the file */
1341 color_start = avio_rb32(pb);
1342 color_count = avio_rb16(pb);
1343 color_end = avio_rb16(pb);
1344 if ((color_start <= 255) && (color_end <= 255)) {
1345 for (j = color_start; j <= color_end; j++) {
1346 /* each A, R, G, or B component is 16 bits;
1347 * only use the top 8 bits */
1356 sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1360 sc->has_palette = 1;
1364 static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1365 AVStream *st, MOVStreamContext *sc)
1367 int bits_per_sample, flags;
1368 uint16_t version = avio_rb16(pb);
1369 AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1371 avio_rb16(pb); /* revision level */
1372 avio_rb32(pb); /* vendor */
1374 st->codec->channels = avio_rb16(pb); /* channel count */
1375 st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1376 av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
1378 sc->audio_cid = avio_rb16(pb);
1379 avio_rb16(pb); /* packet size = 0 */
1381 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1383 // Read QT version 1 fields. In version 0 these do not exist.
1384 av_dlog(c->fc, "version =%d, isom =%d\n", version, c->isom);
1386 (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1389 sc->samples_per_frame = avio_rb32(pb);
1390 avio_rb32(pb); /* bytes per packet */
1391 sc->bytes_per_frame = avio_rb32(pb);
1392 avio_rb32(pb); /* bytes per sample */
1393 } else if (version == 2) {
1394 avio_rb32(pb); /* sizeof struct only */
1395 st->codec->sample_rate = av_int2double(avio_rb64(pb));
1396 st->codec->channels = avio_rb32(pb);
1397 avio_rb32(pb); /* always 0x7F000000 */
1398 st->codec->bits_per_coded_sample = avio_rb32(pb);
1400 flags = avio_rb32(pb); /* lpcm format specific flag */
1401 sc->bytes_per_frame = avio_rb32(pb);
1402 sc->samples_per_frame = avio_rb32(pb);
1403 if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1404 st->codec->codec_id =
1405 ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
1410 switch (st->codec->codec_id) {
1411 case AV_CODEC_ID_PCM_S8:
1412 case AV_CODEC_ID_PCM_U8:
1413 if (st->codec->bits_per_coded_sample == 16)
1414 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
1416 case AV_CODEC_ID_PCM_S16LE:
1417 case AV_CODEC_ID_PCM_S16BE:
1418 if (st->codec->bits_per_coded_sample == 8)
1419 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
1420 else if (st->codec->bits_per_coded_sample == 24)
1421 st->codec->codec_id =
1422 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1423 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1425 /* set values for old format before stsd version 1 appeared */
1426 case AV_CODEC_ID_MACE3:
1427 sc->samples_per_frame = 6;
1428 sc->bytes_per_frame = 2 * st->codec->channels;
1430 case AV_CODEC_ID_MACE6:
1431 sc->samples_per_frame = 6;
1432 sc->bytes_per_frame = 1 * st->codec->channels;
1434 case AV_CODEC_ID_ADPCM_IMA_QT:
1435 sc->samples_per_frame = 64;
1436 sc->bytes_per_frame = 34 * st->codec->channels;
1438 case AV_CODEC_ID_GSM:
1439 sc->samples_per_frame = 160;
1440 sc->bytes_per_frame = 33;
1446 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1447 if (bits_per_sample) {
1448 st->codec->bits_per_coded_sample = bits_per_sample;
1449 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1453 static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1454 AVStream *st, MOVStreamContext *sc,
1457 // ttxt stsd contains display flags, justification, background
1458 // color, fonts, and default styles, so fake an atom to read it
1459 MOVAtom fake_atom = { .size = size };
1460 // mp4s contains a regular esds atom
1461 if (st->codec->codec_tag != AV_RL32("mp4s"))
1462 mov_read_glbl(c, pb, fake_atom);
1463 st->codec->width = sc->width;
1464 st->codec->height = sc->height;
1467 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1472 y = (ycbcr >> 16) & 0xFF;
1473 cr = (ycbcr >> 8) & 0xFF;
1476 b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128));
1477 g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
1478 r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
1480 return (r << 16) | (g << 8) | b;
1483 static int mov_rewrite_dvd_sub_extradata(AVStream *st)
1485 char buf[256] = {0};
1486 uint8_t *src = st->codec->extradata;
1489 if (st->codec->extradata_size != 64)
1492 if (st->codec->width > 0 && st->codec->height > 0)
1493 snprintf(buf, sizeof(buf), "size: %dx%d\n",
1494 st->codec->width, st->codec->height);
1495 av_strlcat(buf, "palette: ", sizeof(buf));
1497 for (i = 0; i < 16; i++) {
1498 uint32_t yuv = AV_RB32(src + i * 4);
1499 uint32_t rgba = yuv_to_rgba(yuv);
1501 av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1504 if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1507 av_freep(&st->codec->extradata);
1508 st->codec->extradata_size = 0;
1509 st->codec->extradata = av_mallocz(strlen(buf) + FF_INPUT_BUFFER_PADDING_SIZE);
1510 if (!st->codec->extradata)
1511 return AVERROR(ENOMEM);
1512 st->codec->extradata_size = strlen(buf);
1513 memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1518 static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
1519 AVStream *st, MOVStreamContext *sc,
1522 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1523 if (ff_get_extradata(st->codec, pb, size) < 0)
1524 return AVERROR(ENOMEM);
1526 MOVStreamContext *tmcd_ctx = st->priv_data;
1528 val = AV_RB32(st->codec->extradata + 4);
1529 tmcd_ctx->tmcd_flags = val;
1531 st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
1532 st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1533 st->codec->time_base.num = 1;
1536 /* other codec type, just skip (rtp, mp4s ...) */
1537 avio_skip(pb, size);
1542 static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
1543 AVStream *st, MOVStreamContext *sc)
1545 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1546 !st->codec->sample_rate && sc->time_scale > 1)
1547 st->codec->sample_rate = sc->time_scale;
1549 /* special codec parameters handling */
1550 switch (st->codec->codec_id) {
1551 #if CONFIG_DV_DEMUXER
1552 case AV_CODEC_ID_DVAUDIO:
1553 c->dv_fctx = avformat_alloc_context();
1554 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
1556 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1557 return AVERROR(ENOMEM);
1559 sc->dv_audio_container = 1;
1560 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
1563 /* no ifdef since parameters are always those */
1564 case AV_CODEC_ID_QCELP:
1565 st->codec->channels = 1;
1566 // force sample rate for qcelp when not stored in mov
1567 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
1568 st->codec->sample_rate = 8000;
1569 // FIXME: Why is the following needed for some files?
1570 sc->samples_per_frame = 160;
1571 if (!sc->bytes_per_frame)
1572 sc->bytes_per_frame = 35;
1574 case AV_CODEC_ID_AMR_NB:
1575 st->codec->channels = 1;
1576 /* force sample rate for amr, stsd in 3gp does not store sample rate */
1577 st->codec->sample_rate = 8000;
1579 case AV_CODEC_ID_AMR_WB:
1580 st->codec->channels = 1;
1581 st->codec->sample_rate = 16000;
1583 case AV_CODEC_ID_MP2:
1584 case AV_CODEC_ID_MP3:
1585 /* force type after stsd for m1a hdlr */
1586 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1587 st->need_parsing = AVSTREAM_PARSE_FULL;
1589 case AV_CODEC_ID_GSM:
1590 case AV_CODEC_ID_ADPCM_MS:
1591 case AV_CODEC_ID_ADPCM_IMA_WAV:
1592 case AV_CODEC_ID_ILBC:
1593 case AV_CODEC_ID_MACE3:
1594 case AV_CODEC_ID_MACE6:
1595 case AV_CODEC_ID_QDM2:
1596 st->codec->block_align = sc->bytes_per_frame;
1598 case AV_CODEC_ID_ALAC:
1599 if (st->codec->extradata_size == 36) {
1600 st->codec->channels = AV_RB8 (st->codec->extradata + 21);
1601 st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
1604 case AV_CODEC_ID_AC3:
1605 case AV_CODEC_ID_MPEG1VIDEO:
1606 case AV_CODEC_ID_VC1:
1607 st->need_parsing = AVSTREAM_PARSE_FULL;
1615 static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1616 int codec_tag, int format,
1619 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1622 (codec_tag != format &&
1623 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1624 : codec_tag != MKTAG('j','p','e','g')))) {
1625 /* Multiple fourcc, we skip JPEG. This is not correct, we should
1626 * export it as a separate AVStream but this needs a few changes
1627 * in the MOV demuxer, patch welcome. */
1629 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1630 avio_skip(pb, size);
1633 if ( codec_tag == AV_RL32("avc1") ||
1634 codec_tag == AV_RL32("hvc1") ||
1635 codec_tag == AV_RL32("hev1")
1637 av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
1642 int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
1645 MOVStreamContext *sc;
1646 int pseudo_stream_id;
1648 if (c->fc->nb_streams < 1)
1650 st = c->fc->streams[c->fc->nb_streams-1];
1653 for (pseudo_stream_id = 0;
1654 pseudo_stream_id < entries && !pb->eof_reached;
1655 pseudo_stream_id++) {
1656 //Parsing Sample description table
1658 int ret, dref_id = 1;
1659 MOVAtom a = { AV_RL32("stsd") };
1660 int64_t start_pos = avio_tell(pb);
1661 int64_t size = avio_rb32(pb); /* size */
1662 uint32_t format = avio_rl32(pb); /* data format */
1665 avio_rb32(pb); /* reserved */
1666 avio_rb16(pb); /* reserved */
1667 dref_id = avio_rb16(pb);
1668 }else if (size <= 7){
1669 av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
1670 return AVERROR_INVALIDDATA;
1673 if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1674 size - (avio_tell(pb) - start_pos)))
1677 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
1678 sc->dref_id= dref_id;
1680 id = mov_codec_id(st, format);
1682 av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
1683 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
1684 (format >> 24) & 0xff, st->codec->codec_type);
1686 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
1687 st->codec->codec_id = id;
1688 mov_parse_stsd_video(c, pb, st, sc);
1689 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
1690 st->codec->codec_id = id;
1691 mov_parse_stsd_audio(c, pb, st, sc);
1692 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
1693 st->codec->codec_id = id;
1694 mov_parse_stsd_subtitle(c, pb, st, sc,
1695 size - (avio_tell(pb) - start_pos));
1697 ret = mov_parse_stsd_data(c, pb, st, sc,
1698 size - (avio_tell(pb) - start_pos));
1702 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1703 a.size = size - (avio_tell(pb) - start_pos);
1705 if ((ret = mov_read_default(c, pb, a)) < 0)
1707 } else if (a.size > 0)
1708 avio_skip(pb, a.size);
1711 if (pb->eof_reached)
1714 return mov_finalize_stsd_codec(c, pb, st, sc);
1717 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1721 avio_r8(pb); /* version */
1722 avio_rb24(pb); /* flags */
1723 entries = avio_rb32(pb);
1725 return ff_mov_read_stsd_entries(c, pb, entries);
1728 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1731 MOVStreamContext *sc;
1732 unsigned int i, entries;
1734 if (c->fc->nb_streams < 1)
1736 st = c->fc->streams[c->fc->nb_streams-1];
1739 avio_r8(pb); /* version */
1740 avio_rb24(pb); /* flags */
1742 entries = avio_rb32(pb);
1744 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1748 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1749 return AVERROR_INVALIDDATA;
1750 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1752 return AVERROR(ENOMEM);
1754 for (i = 0; i < entries && !pb->eof_reached; i++) {
1755 sc->stsc_data[i].first = avio_rb32(pb);
1756 sc->stsc_data[i].count = avio_rb32(pb);
1757 sc->stsc_data[i].id = avio_rb32(pb);
1762 if (pb->eof_reached)
1768 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1771 MOVStreamContext *sc;
1772 unsigned i, entries;
1774 if (c->fc->nb_streams < 1)
1776 st = c->fc->streams[c->fc->nb_streams-1];
1779 avio_rb32(pb); // version + flags
1781 entries = avio_rb32(pb);
1782 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1783 return AVERROR_INVALIDDATA;
1784 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1786 return AVERROR(ENOMEM);
1788 for (i = 0; i < entries && !pb->eof_reached; i++) {
1789 sc->stps_data[i] = avio_rb32(pb);
1790 //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1795 if (pb->eof_reached)
1801 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1804 MOVStreamContext *sc;
1805 unsigned int i, entries;
1807 if (c->fc->nb_streams < 1)
1809 st = c->fc->streams[c->fc->nb_streams-1];
1812 avio_r8(pb); /* version */
1813 avio_rb24(pb); /* flags */
1815 entries = avio_rb32(pb);
1817 av_dlog(c->fc, "keyframe_count = %d\n", entries);
1821 sc->keyframe_absent = 1;
1822 if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1823 st->need_parsing = AVSTREAM_PARSE_HEADERS;
1826 if (entries >= UINT_MAX / sizeof(int))
1827 return AVERROR_INVALIDDATA;
1828 sc->keyframes = av_malloc(entries * sizeof(int));
1830 return AVERROR(ENOMEM);
1832 for (i = 0; i < entries && !pb->eof_reached; i++) {
1833 sc->keyframes[i] = avio_rb32(pb);
1834 //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1837 sc->keyframe_count = i;
1839 if (pb->eof_reached)
1845 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1848 MOVStreamContext *sc;
1849 unsigned int i, entries, sample_size, field_size, num_bytes;
1853 if (c->fc->nb_streams < 1)
1855 st = c->fc->streams[c->fc->nb_streams-1];
1858 avio_r8(pb); /* version */
1859 avio_rb24(pb); /* flags */
1861 if (atom.type == MKTAG('s','t','s','z')) {
1862 sample_size = avio_rb32(pb);
1863 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1864 sc->sample_size = sample_size;
1865 sc->stsz_sample_size = sample_size;
1869 avio_rb24(pb); /* reserved */
1870 field_size = avio_r8(pb);
1872 entries = avio_rb32(pb);
1874 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1876 sc->sample_count = entries;
1880 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
1881 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
1882 return AVERROR_INVALIDDATA;
1887 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
1888 return AVERROR_INVALIDDATA;
1889 sc->sample_sizes = av_malloc(entries * sizeof(int));
1890 if (!sc->sample_sizes)
1891 return AVERROR(ENOMEM);
1893 num_bytes = (entries*field_size+4)>>3;
1895 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
1897 av_freep(&sc->sample_sizes);
1898 return AVERROR(ENOMEM);
1901 if (avio_read(pb, buf, num_bytes) < num_bytes) {
1902 av_freep(&sc->sample_sizes);
1904 return AVERROR_INVALIDDATA;
1907 init_get_bits(&gb, buf, 8*num_bytes);
1909 for (i = 0; i < entries && !pb->eof_reached; i++) {
1910 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
1911 sc->data_size += sc->sample_sizes[i];
1914 sc->sample_count = i;
1916 if (pb->eof_reached)
1923 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1926 MOVStreamContext *sc;
1927 unsigned int i, entries;
1929 int64_t total_sample_count=0;
1931 if (c->fc->nb_streams < 1)
1933 st = c->fc->streams[c->fc->nb_streams-1];
1936 avio_r8(pb); /* version */
1937 avio_rb24(pb); /* flags */
1938 entries = avio_rb32(pb);
1940 av_dlog(c->fc, "track[%i].stts.entries = %i\n",
1941 c->fc->nb_streams-1, entries);
1943 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
1946 av_free(sc->stts_data);
1947 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1949 return AVERROR(ENOMEM);
1951 for (i = 0; i < entries && !pb->eof_reached; i++) {
1952 int sample_duration;
1955 sample_count=avio_rb32(pb);
1956 sample_duration = avio_rb32(pb);
1958 /* sample_duration < 0 is invalid based on the spec */
1959 if (sample_duration < 0) {
1960 av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta %d in STTS, at %d st:%d\n",
1961 sample_duration, i, c->fc->nb_streams-1);
1962 sample_duration = 1;
1964 if (sample_count < 0) {
1965 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
1966 return AVERROR_INVALIDDATA;
1968 sc->stts_data[i].count= sample_count;
1969 sc->stts_data[i].duration= sample_duration;
1971 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
1972 sample_count, sample_duration);
1976 && sample_count == 1
1977 && total_sample_count > 100
1978 && sample_duration/10 > duration / total_sample_count)
1979 sample_duration = duration / total_sample_count;
1980 duration+=(int64_t)sample_duration*sample_count;
1981 total_sample_count+=sample_count;
1986 sc->duration_for_fps += duration;
1987 sc->nb_frames_for_fps += total_sample_count;
1989 if (pb->eof_reached)
1992 st->nb_frames= total_sample_count;
1994 st->duration= duration;
1995 sc->track_end = duration;
1999 static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2002 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2006 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2009 MOVStreamContext *sc;
2010 unsigned int i, entries;
2012 if (c->fc->nb_streams < 1)
2014 st = c->fc->streams[c->fc->nb_streams-1];
2017 avio_r8(pb); /* version */
2018 avio_rb24(pb); /* flags */
2019 entries = avio_rb32(pb);
2021 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2025 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2026 return AVERROR_INVALIDDATA;
2027 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
2029 return AVERROR(ENOMEM);
2031 for (i = 0; i < entries && !pb->eof_reached; i++) {
2032 int count =avio_rb32(pb);
2033 int duration =avio_rb32(pb);
2035 sc->ctts_data[i].count = count;
2036 sc->ctts_data[i].duration= duration;
2038 av_dlog(c->fc, "count=%d, duration=%d\n",
2041 if (FFABS(duration) > (1<<28) && i+2<entries) {
2042 av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2043 av_freep(&sc->ctts_data);
2049 mov_update_dts_shift(sc, duration);
2054 if (pb->eof_reached)
2057 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
2062 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2065 MOVStreamContext *sc;
2066 unsigned int i, entries;
2068 uint32_t grouping_type;
2070 if (c->fc->nb_streams < 1)
2072 st = c->fc->streams[c->fc->nb_streams-1];
2075 version = avio_r8(pb); /* version */
2076 avio_rb24(pb); /* flags */
2077 grouping_type = avio_rl32(pb);
2078 if (grouping_type != MKTAG( 'r','a','p',' '))
2079 return 0; /* only support 'rap ' grouping */
2081 avio_rb32(pb); /* grouping_type_parameter */
2083 entries = avio_rb32(pb);
2086 if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2087 return AVERROR_INVALIDDATA;
2088 sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2090 return AVERROR(ENOMEM);
2092 for (i = 0; i < entries && !pb->eof_reached; i++) {
2093 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2094 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2097 sc->rap_group_count = i;
2099 return pb->eof_reached ? AVERROR_EOF : 0;
2102 static void mov_build_index(MOVContext *mov, AVStream *st)
2104 MOVStreamContext *sc = st->priv_data;
2105 int64_t current_offset;
2106 int64_t current_dts = 0;
2107 unsigned int stts_index = 0;
2108 unsigned int stsc_index = 0;
2109 unsigned int stss_index = 0;
2110 unsigned int stps_index = 0;
2112 uint64_t stream_size = 0;
2114 /* adjust first dts according to edit list */
2115 if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
2116 if (sc->empty_duration)
2117 sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
2118 sc->time_offset = sc->start_time - sc->empty_duration;
2119 current_dts = -sc->time_offset;
2120 if (sc->ctts_count>0 && sc->stts_count>0 &&
2121 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2122 /* more than 16 frames delay, dts are likely wrong
2123 this happens with files created by iMovie */
2125 st->codec->has_b_frames = 1;
2129 /* only use old uncompressed audio chunk demuxing when stts specifies it */
2130 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2131 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2132 unsigned int current_sample = 0;
2133 unsigned int stts_sample = 0;
2134 unsigned int sample_size;
2135 unsigned int distance = 0;
2136 unsigned int rap_group_index = 0;
2137 unsigned int rap_group_sample = 0;
2138 int rap_group_present = sc->rap_group_count && sc->rap_group;
2139 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2141 current_dts -= sc->dts_shift;
2143 if (!sc->sample_count || st->nb_index_entries)
2145 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2147 if (av_reallocp_array(&st->index_entries,
2148 st->nb_index_entries + sc->sample_count,
2149 sizeof(*st->index_entries)) < 0) {
2150 st->nb_index_entries = 0;
2153 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2155 for (i = 0; i < sc->chunk_count; i++) {
2156 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2157 current_offset = sc->chunk_offsets[i];
2158 while (stsc_index + 1 < sc->stsc_count &&
2159 i + 1 == sc->stsc_data[stsc_index + 1].first)
2162 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2163 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2164 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2165 sc->stsz_sample_size = sc->sample_size;
2167 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2168 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2169 sc->stsz_sample_size = sc->sample_size;
2172 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2174 if (current_sample >= sc->sample_count) {
2175 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2179 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2181 if (stss_index + 1 < sc->keyframe_count)
2183 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2185 if (stps_index + 1 < sc->stps_count)
2188 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2189 if (sc->rap_group[rap_group_index].index > 0)
2191 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2192 rap_group_sample = 0;
2196 if (sc->keyframe_absent
2198 && !rap_group_present
2199 && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2203 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2204 if (sc->pseudo_stream_id == -1 ||
2205 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2206 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2207 e->pos = current_offset;
2208 e->timestamp = current_dts;
2209 e->size = sample_size;
2210 e->min_distance = distance;
2211 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2212 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2213 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2214 current_offset, current_dts, sample_size, distance, keyframe);
2215 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2216 ff_rfps_add_frame(mov->fc, st, current_dts);
2219 current_offset += sample_size;
2220 stream_size += sample_size;
2221 current_dts += sc->stts_data[stts_index].duration;
2225 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2231 if (st->duration > 0)
2232 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2234 unsigned chunk_samples, total = 0;
2236 // compute total chunk count
2237 for (i = 0; i < sc->stsc_count; i++) {
2238 unsigned count, chunk_count;
2240 chunk_samples = sc->stsc_data[i].count;
2241 if (i != sc->stsc_count - 1 &&
2242 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2243 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2247 if (sc->samples_per_frame >= 160) { // gsm
2248 count = chunk_samples / sc->samples_per_frame;
2249 } else if (sc->samples_per_frame > 1) {
2250 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2251 count = (chunk_samples+samples-1) / samples;
2253 count = (chunk_samples+1023) / 1024;
2256 if (i < sc->stsc_count - 1)
2257 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2259 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2260 total += chunk_count * count;
2263 av_dlog(mov->fc, "chunk count %d\n", total);
2264 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2266 if (av_reallocp_array(&st->index_entries,
2267 st->nb_index_entries + total,
2268 sizeof(*st->index_entries)) < 0) {
2269 st->nb_index_entries = 0;
2272 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2275 for (i = 0; i < sc->chunk_count; i++) {
2276 current_offset = sc->chunk_offsets[i];
2277 if (stsc_index + 1 < sc->stsc_count &&
2278 i + 1 == sc->stsc_data[stsc_index + 1].first)
2280 chunk_samples = sc->stsc_data[stsc_index].count;
2282 while (chunk_samples > 0) {
2284 unsigned size, samples;
2286 if (sc->samples_per_frame >= 160) { // gsm
2287 samples = sc->samples_per_frame;
2288 size = sc->bytes_per_frame;
2290 if (sc->samples_per_frame > 1) {
2291 samples = FFMIN((1024 / sc->samples_per_frame)*
2292 sc->samples_per_frame, chunk_samples);
2293 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2295 samples = FFMIN(1024, chunk_samples);
2296 size = samples * sc->sample_size;
2300 if (st->nb_index_entries >= total) {
2301 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2304 e = &st->index_entries[st->nb_index_entries++];
2305 e->pos = current_offset;
2306 e->timestamp = current_dts;
2308 e->min_distance = 0;
2309 e->flags = AVINDEX_KEYFRAME;
2310 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2311 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2314 current_offset += size;
2315 current_dts += samples;
2316 chunk_samples -= samples;
2322 static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
2323 AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
2325 /* try relative path, we do not try the absolute because it can leak information about our
2326 system to an attacker */
2327 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2328 char filename[1024];
2329 const char *src_path;
2332 /* find a source dir */
2333 src_path = strrchr(src, '/');
2339 /* find a next level down to target */
2340 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2341 if (ref->path[l] == '/') {
2342 if (i == ref->nlvl_to - 1)
2348 /* compose filename if next level down to target was found */
2349 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2350 memcpy(filename, src, src_path - src);
2351 filename[src_path - src] = 0;
2353 for (i = 1; i < ref->nlvl_from; i++)
2354 av_strlcat(filename, "../", 1024);
2356 av_strlcat(filename, ref->path + l + 1, 1024);
2358 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2361 } else if (use_absolute_path) {
2362 av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
2363 "this is a possible security issue\n");
2364 if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2368 return AVERROR(ENOENT);
2371 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
2373 if (sc->time_scale <= 0) {
2374 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
2375 sc->time_scale = c->time_scale;
2376 if (sc->time_scale <= 0)
2381 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2384 MOVStreamContext *sc;
2387 st = avformat_new_stream(c->fc, NULL);
2388 if (!st) return AVERROR(ENOMEM);
2389 st->id = c->fc->nb_streams;
2390 sc = av_mallocz(sizeof(MOVStreamContext));
2391 if (!sc) return AVERROR(ENOMEM);
2394 st->codec->codec_type = AVMEDIA_TYPE_DATA;
2395 sc->ffindex = st->index;
2397 if ((ret = mov_read_default(c, pb, atom)) < 0)
2401 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2402 (!sc->sample_size && !sc->sample_count))) {
2403 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2408 fix_timescale(c, sc);
2410 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2412 mov_build_index(c, st);
2414 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2415 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2416 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
2417 c->use_absolute_path, c->fc) < 0)
2418 av_log(c->fc, AV_LOG_ERROR,
2419 "stream %d, error opening alias: path='%s', dir='%s', "
2420 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2421 st->index, dref->path, dref->dir, dref->filename,
2422 dref->volume, dref->nlvl_from, dref->nlvl_to);
2425 sc->pb_is_copied = 1;
2428 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2429 if (!st->sample_aspect_ratio.num &&
2430 (st->codec->width != sc->width || st->codec->height != sc->height)) {
2431 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2432 ((double)st->codec->width * sc->height), INT_MAX);
2435 #if FF_API_R_FRAME_RATE
2436 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
2437 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2438 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
2442 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2443 if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2444 TAG_IS_AVCI(st->codec->codec_tag)) {
2445 ret = ff_generate_avci_extradata(st);
2450 switch (st->codec->codec_id) {
2451 #if CONFIG_H261_DECODER
2452 case AV_CODEC_ID_H261:
2454 #if CONFIG_H263_DECODER
2455 case AV_CODEC_ID_H263:
2457 #if CONFIG_MPEG4_DECODER
2458 case AV_CODEC_ID_MPEG4:
2460 st->codec->width = 0; /* let decoder init width/height */
2461 st->codec->height= 0;
2465 /* Do not need those anymore. */
2466 av_freep(&sc->chunk_offsets);
2467 av_freep(&sc->stsc_data);
2468 av_freep(&sc->sample_sizes);
2469 av_freep(&sc->keyframes);
2470 av_freep(&sc->stts_data);
2471 av_freep(&sc->stps_data);
2472 av_freep(&sc->rap_group);
2477 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2480 c->itunes_metadata = 1;
2481 ret = mov_read_default(c, pb, atom);
2482 c->itunes_metadata = 0;
2486 static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
2488 int64_t end = avio_tell(pb) + size;
2489 uint8_t *key = NULL, *val = NULL;
2492 MOVStreamContext *sc;
2494 if (c->fc->nb_streams < 1)
2496 st = c->fc->streams[c->fc->nb_streams-1];
2499 for (i = 0; i < 2; i++) {
2503 if (end - avio_tell(pb) <= 12)
2506 len = avio_rb32(pb);
2507 tag = avio_rl32(pb);
2508 avio_skip(pb, 4); // flags
2510 if (len < 12 || len - 12 > end - avio_tell(pb))
2514 if (tag == MKTAG('n', 'a', 'm', 'e'))
2516 else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2523 *p = av_malloc(len + 1);
2526 avio_read(pb, *p, len);
2531 if (strcmp(key, "iTunSMPB") == 0) {
2532 int priming, remainder, samples;
2533 if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
2534 if(priming>0 && priming<16384)
2535 sc->start_pad = priming;
2537 } else if (strcmp(key, "cdec") == 0) {
2539 av_dict_set(&c->fc->metadata, key, val,
2540 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
2545 avio_seek(pb, end, SEEK_SET);
2551 static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2553 int64_t end = avio_tell(pb) + atom.size;
2559 len = avio_rb32(pb);
2560 tag = avio_rl32(pb);
2562 if (len > atom.size)
2565 if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2566 uint8_t domain[128];
2569 avio_skip(pb, 4); // flags
2572 domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2573 avio_skip(pb, len - domain_len);
2574 return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
2578 av_log(c->fc, AV_LOG_VERBOSE,
2579 "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2583 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2585 while (atom.size > 8) {
2586 uint32_t tag = avio_rl32(pb);
2588 if (tag == MKTAG('h','d','l','r')) {
2589 avio_seek(pb, -8, SEEK_CUR);
2591 return mov_read_default(c, pb, atom);
2597 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2602 int64_t disp_transform[2];
2603 int display_matrix[3][3];
2605 MOVStreamContext *sc;
2609 if (c->fc->nb_streams < 1)
2611 st = c->fc->streams[c->fc->nb_streams-1];
2614 version = avio_r8(pb);
2615 flags = avio_rb24(pb);
2616 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
2622 avio_rb32(pb); /* creation time */
2623 avio_rb32(pb); /* modification time */
2625 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2626 avio_rb32(pb); /* reserved */
2628 /* highlevel (considering edits) duration in movie timebase */
2629 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2630 avio_rb32(pb); /* reserved */
2631 avio_rb32(pb); /* reserved */
2633 avio_rb16(pb); /* layer */
2634 avio_rb16(pb); /* alternate group */
2635 avio_rb16(pb); /* volume */
2636 avio_rb16(pb); /* reserved */
2638 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2639 // they're kept in fixed point format through all calculations
2640 // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2641 // side data, but the scale factor is not needed to calculate aspect ratio
2642 for (i = 0; i < 3; i++) {
2643 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2644 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2645 display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
2648 width = avio_rb32(pb); // 16.16 fixed point track width
2649 height = avio_rb32(pb); // 16.16 fixed point track height
2650 sc->width = width >> 16;
2651 sc->height = height >> 16;
2653 // save the matrix and add rotate metadata when it is not the default
2655 if (display_matrix[0][0] != (1 << 16) ||
2656 display_matrix[1][1] != (1 << 16) ||
2657 display_matrix[2][2] != (1 << 30) ||
2658 display_matrix[0][1] || display_matrix[0][2] ||
2659 display_matrix[1][0] || display_matrix[1][2] ||
2660 display_matrix[2][0] || display_matrix[2][1]) {
2664 av_freep(&sc->display_matrix);
2665 sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2666 if (!sc->display_matrix)
2667 return AVERROR(ENOMEM);
2669 for (i = 0; i < 3; i++)
2670 for (j = 0; j < 3; j++)
2671 sc->display_matrix[i * 3 + j] = display_matrix[j][i];
2673 rotate = av_display_rotation_get(sc->display_matrix);
2674 if (!isnan(rotate)) {
2675 char rotate_buf[64];
2677 if (rotate < 0) // for backward compatibility
2679 snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
2680 av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
2684 // transform the display width/height according to the matrix
2685 // skip this if the display matrix is the default identity matrix
2686 // or if it is rotating the picture, ex iPhone 3GS
2687 // to keep the same scale, use [width height 1<<16]
2688 if (width && height &&
2689 ((display_matrix[0][0] != 65536 ||
2690 display_matrix[1][1] != 65536) &&
2691 !display_matrix[0][1] &&
2692 !display_matrix[1][0] &&
2693 !display_matrix[2][0] && !display_matrix[2][1])) {
2694 for (i = 0; i < 2; i++)
2696 (int64_t) width * display_matrix[0][i] +
2697 (int64_t) height * display_matrix[1][i] +
2698 ((int64_t) display_matrix[2][i] << 16);
2700 //sample aspect ratio is new width/height divided by old width/height
2701 st->sample_aspect_ratio = av_d2q(
2702 ((double) disp_transform[0] * height) /
2703 ((double) disp_transform[1] * width), INT_MAX);
2708 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2710 MOVFragment *frag = &c->fragment;
2711 MOVTrackExt *trex = NULL;
2712 int flags, track_id, i;
2714 avio_r8(pb); /* version */
2715 flags = avio_rb24(pb);
2717 track_id = avio_rb32(pb);
2719 return AVERROR_INVALIDDATA;
2720 frag->track_id = track_id;
2721 for (i = 0; i < c->trex_count; i++)
2722 if (c->trex_data[i].track_id == frag->track_id) {
2723 trex = &c->trex_data[i];
2727 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2728 return AVERROR_INVALIDDATA;
2731 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
2732 avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
2733 frag->moof_offset : frag->implicit_offset;
2734 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2736 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2737 avio_rb32(pb) : trex->duration;
2738 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2739 avio_rb32(pb) : trex->size;
2740 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2741 avio_rb32(pb) : trex->flags;
2742 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2746 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2748 c->chapter_track = avio_rb32(pb);
2752 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2757 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2758 return AVERROR_INVALIDDATA;
2759 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2760 sizeof(*c->trex_data))) < 0) {
2765 c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
2767 trex = &c->trex_data[c->trex_count++];
2768 avio_r8(pb); /* version */
2769 avio_rb24(pb); /* flags */
2770 trex->track_id = avio_rb32(pb);
2771 trex->stsd_id = avio_rb32(pb);
2772 trex->duration = avio_rb32(pb);
2773 trex->size = avio_rb32(pb);
2774 trex->flags = avio_rb32(pb);
2778 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2780 MOVFragment *frag = &c->fragment;
2781 AVStream *st = NULL;
2782 MOVStreamContext *sc;
2786 int data_offset = 0;
2787 unsigned entries, first_sample_flags = frag->flags;
2788 int flags, distance, i, found_keyframe = 0, err;
2790 for (i = 0; i < c->fc->nb_streams; i++) {
2791 if (c->fc->streams[i]->id == frag->track_id) {
2792 st = c->fc->streams[i];
2797 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2798 return AVERROR_INVALIDDATA;
2801 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
2803 avio_r8(pb); /* version */
2804 flags = avio_rb24(pb);
2805 entries = avio_rb32(pb);
2806 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2808 /* Always assume the presence of composition time offsets.
2809 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2810 * 1) in the initial movie, there are no samples.
2811 * 2) in the first movie fragment, there is only one sample without composition time offset.
2812 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2813 if (!sc->ctts_count && sc->sample_count)
2815 /* Complement ctts table if moov atom doesn't have ctts atom. */
2816 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2818 return AVERROR(ENOMEM);
2819 sc->ctts_data = ctts_data;
2820 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2821 sc->ctts_data[sc->ctts_count].duration = 0;
2824 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2825 return AVERROR_INVALIDDATA;
2826 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2827 sizeof(*sc->ctts_data))) < 0) {
2831 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
2832 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
2833 dts = sc->track_end - sc->time_offset;
2834 offset = frag->base_data_offset + data_offset;
2836 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
2837 for (i = 0; i < entries && !pb->eof_reached; i++) {
2838 unsigned sample_size = frag->size;
2839 int sample_flags = i ? frag->flags : first_sample_flags;
2840 unsigned sample_duration = frag->duration;
2843 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2844 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
2845 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
2846 sc->ctts_data[sc->ctts_count].count = 1;
2847 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2849 mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
2851 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2853 else if (!found_keyframe)
2854 keyframe = found_keyframe =
2855 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2856 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
2859 av_add_index_entry(st, offset, dts, sample_size, distance,
2860 keyframe ? AVINDEX_KEYFRAME : 0);
2861 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2862 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
2863 offset, dts, sample_size, distance, keyframe);
2865 dts += sample_duration;
2866 offset += sample_size;
2867 sc->data_size += sample_size;
2868 sc->duration_for_fps += sample_duration;
2869 sc->nb_frames_for_fps ++;
2872 if (pb->eof_reached)
2875 frag->implicit_offset = offset;
2876 st->duration = sc->track_end = dts + sc->time_offset;
2880 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2881 /* like the files created with Adobe Premiere 5.0, for samples see */
2882 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
2883 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2888 return 0; /* continue */
2889 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
2890 avio_skip(pb, atom.size - 4);
2893 atom.type = avio_rl32(pb);
2895 if (atom.type != MKTAG('m','d','a','t')) {
2896 avio_skip(pb, atom.size);
2899 err = mov_read_mdat(c, pb, atom);
2903 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2908 uint8_t *moov_data; /* uncompressed data */
2909 long cmov_len, moov_len;
2912 avio_rb32(pb); /* dcom atom */
2913 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
2914 return AVERROR_INVALIDDATA;
2915 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
2916 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
2917 return AVERROR_INVALIDDATA;
2919 avio_rb32(pb); /* cmvd atom */
2920 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
2921 return AVERROR_INVALIDDATA;
2922 moov_len = avio_rb32(pb); /* uncompressed size */
2923 cmov_len = atom.size - 6 * 4;
2925 cmov_data = av_malloc(cmov_len);
2927 return AVERROR(ENOMEM);
2928 moov_data = av_malloc(moov_len);
2931 return AVERROR(ENOMEM);
2933 avio_read(pb, cmov_data, cmov_len);
2934 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
2935 goto free_and_return;
2936 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
2937 goto free_and_return;
2938 atom.type = MKTAG('m','o','o','v');
2939 atom.size = moov_len;
2940 ret = mov_read_default(c, &ctx, atom);
2946 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
2947 return AVERROR(ENOSYS);
2951 /* edit list atom */
2952 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2954 MOVStreamContext *sc;
2955 int i, edit_count, version, edit_start_index = 0;
2956 int unsupported = 0;
2958 if (c->fc->nb_streams < 1 || c->ignore_editlist)
2960 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
2962 version = avio_r8(pb); /* version */
2963 avio_rb24(pb); /* flags */
2964 edit_count = avio_rb32(pb); /* entries */
2966 if ((uint64_t)edit_count*12+8 > atom.size)
2967 return AVERROR_INVALIDDATA;
2969 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
2970 for (i=0; i<edit_count; i++){
2975 duration = avio_rb64(pb);
2976 time = avio_rb64(pb);
2978 duration = avio_rb32(pb); /* segment duration */
2979 time = (int32_t)avio_rb32(pb); /* media time */
2981 rate = avio_rb32(pb);
2982 if (i == 0 && time == -1) {
2983 sc->empty_duration = duration;
2984 edit_start_index = 1;
2985 } else if (i == edit_start_index && time >= 0)
2986 sc->start_time = time;
2990 av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
2991 duration, time, rate / 65536.0);
2995 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
2996 "a/v desync might occur, patch welcome\n");
3001 static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3003 MOVStreamContext *sc;
3005 if (c->fc->nb_streams < 1)
3006 return AVERROR_INVALIDDATA;
3007 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3008 sc->timecode_track = avio_rb32(pb);
3012 static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3016 static const uint8_t uuid_isml_manifest[] = {
3017 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3018 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3021 if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3022 return AVERROR_INVALIDDATA;
3024 ret = avio_read(pb, uuid, sizeof(uuid));
3027 } else if (ret != sizeof(uuid)) {
3028 return AVERROR_INVALIDDATA;
3030 if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3031 uint8_t *buffer, *ptr;
3033 size_t len = atom.size - sizeof(uuid);
3036 return AVERROR_INVALIDDATA;
3038 ret = avio_skip(pb, 4); // zeroes
3041 buffer = av_mallocz(len + 1);
3043 return AVERROR(ENOMEM);
3045 ret = avio_read(pb, buffer, len);
3049 } else if (ret != len) {
3051 return AVERROR_INVALIDDATA;
3055 while ((ptr = av_stristr(ptr, "systemBitrate=\"")) != NULL) {
3056 ptr += sizeof("systemBitrate=\"") - 1;
3057 c->bitrates_count++;
3058 c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3060 c->bitrates_count = 0;
3062 return AVERROR(ENOMEM);
3065 ret = strtol(ptr, &endptr, 10);
3066 if (ret < 0 || errno || *endptr != '"') {
3067 c->bitrates[c->bitrates_count - 1] = 0;
3069 c->bitrates[c->bitrates_count - 1] = ret;
3078 static const MOVParseTableEntry mov_default_parse_table[] = {
3079 { MKTAG('A','C','L','R'), mov_read_avid },
3080 { MKTAG('A','P','R','G'), mov_read_avid },
3081 { MKTAG('A','A','L','P'), mov_read_avid },
3082 { MKTAG('A','R','E','S'), mov_read_ares },
3083 { MKTAG('a','v','s','s'), mov_read_avss },
3084 { MKTAG('c','h','p','l'), mov_read_chpl },
3085 { MKTAG('c','o','6','4'), mov_read_stco },
3086 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3087 { MKTAG('d','i','n','f'), mov_read_default },
3088 { MKTAG('d','r','e','f'), mov_read_dref },
3089 { MKTAG('e','d','t','s'), mov_read_default },
3090 { MKTAG('e','l','s','t'), mov_read_elst },
3091 { MKTAG('e','n','d','a'), mov_read_enda },
3092 { MKTAG('f','i','e','l'), mov_read_fiel },
3093 { MKTAG('f','t','y','p'), mov_read_ftyp },
3094 { MKTAG('g','l','b','l'), mov_read_glbl },
3095 { MKTAG('h','d','l','r'), mov_read_hdlr },
3096 { MKTAG('i','l','s','t'), mov_read_ilst },
3097 { MKTAG('j','p','2','h'), mov_read_jp2h },
3098 { MKTAG('m','d','a','t'), mov_read_mdat },
3099 { MKTAG('m','d','h','d'), mov_read_mdhd },
3100 { MKTAG('m','d','i','a'), mov_read_default },
3101 { MKTAG('m','e','t','a'), mov_read_meta },
3102 { MKTAG('m','i','n','f'), mov_read_default },
3103 { MKTAG('m','o','o','f'), mov_read_moof },
3104 { MKTAG('m','o','o','v'), mov_read_moov },
3105 { MKTAG('m','v','e','x'), mov_read_default },
3106 { MKTAG('m','v','h','d'), mov_read_mvhd },
3107 { MKTAG('S','M','I',' '), mov_read_svq3 },
3108 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3109 { MKTAG('a','v','c','C'), mov_read_glbl },
3110 { MKTAG('p','a','s','p'), mov_read_pasp },
3111 { MKTAG('s','t','b','l'), mov_read_default },
3112 { MKTAG('s','t','c','o'), mov_read_stco },
3113 { MKTAG('s','t','p','s'), mov_read_stps },
3114 { MKTAG('s','t','r','f'), mov_read_strf },
3115 { MKTAG('s','t','s','c'), mov_read_stsc },
3116 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3117 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3118 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3119 { MKTAG('s','t','t','s'), mov_read_stts },
3120 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3121 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3122 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3123 { MKTAG('t','r','a','k'), mov_read_trak },
3124 { MKTAG('t','r','a','f'), mov_read_default },
3125 { MKTAG('t','r','e','f'), mov_read_default },
3126 { MKTAG('t','m','c','d'), mov_read_tmcd },
3127 { MKTAG('c','h','a','p'), mov_read_chap },
3128 { MKTAG('t','r','e','x'), mov_read_trex },
3129 { MKTAG('t','r','u','n'), mov_read_trun },
3130 { MKTAG('u','d','t','a'), mov_read_default },
3131 { MKTAG('w','a','v','e'), mov_read_wave },
3132 { MKTAG('e','s','d','s'), mov_read_esds },
3133 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3134 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3135 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3136 { MKTAG('w','f','e','x'), mov_read_wfex },
3137 { MKTAG('c','m','o','v'), mov_read_cmov },
3138 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3139 { MKTAG('d','v','c','1'), mov_read_dvc1 },
3140 { MKTAG('s','b','g','p'), mov_read_sbgp },
3141 { MKTAG('h','v','c','C'), mov_read_glbl },
3142 { MKTAG('u','u','i','d'), mov_read_uuid },
3143 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
3144 { MKTAG('-','-','-','-'), mov_read_custom },
3148 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3150 int64_t total_size = 0;
3155 atom.size = INT64_MAX;
3156 while (total_size + 8 <= atom.size && !url_feof(pb)) {
3157 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3160 if (atom.size >= 8) {
3161 a.size = avio_rb32(pb);
3162 a.type = avio_rl32(pb);
3163 if (a.type == MKTAG('f','r','e','e') &&
3167 uint32_t *type = (uint32_t *)buf + 1;
3168 avio_read(pb, buf, 8);
3169 avio_seek(pb, -8, SEEK_CUR);
3170 if (*type == MKTAG('m','v','h','d') ||
3171 *type == MKTAG('c','m','o','v')) {
3172 av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
3173 a.type = MKTAG('m','o','o','v');
3176 if (atom.type != MKTAG('r','o','o','t') &&
3177 atom.type != MKTAG('m','o','o','v'))
3179 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
3181 av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
3187 if (a.size == 1) { /* 64 bit extended size */
3188 a.size = avio_rb64(pb) - 8;
3192 av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3193 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3195 a.size = atom.size - total_size + 8;
3200 a.size = FFMIN(a.size, atom.size - total_size);
3202 for (i = 0; mov_default_parse_table[i].type; i++)
3203 if (mov_default_parse_table[i].type == a.type) {
3204 parse = mov_default_parse_table[i].parse;
3208 // container is user data
3209 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3210 atom.type == MKTAG('i','l','s','t')))
3211 parse = mov_read_udta_string;
3213 if (!parse) { /* skip leaf atoms data */
3214 avio_skip(pb, a.size);
3216 int64_t start_pos = avio_tell(pb);
3218 int err = parse(c, pb, a);
3221 if (c->found_moov && c->found_mdat &&
3222 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3223 start_pos + a.size == avio_size(pb))) {
3224 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3225 c->next_root_atom = start_pos + a.size;
3228 left = a.size - avio_tell(pb) + start_pos;
3229 if (left > 0) /* skip garbage at atom end */
3230 avio_skip(pb, left);
3231 else if (left < 0) {
3232 av_log(c->fc, AV_LOG_WARNING,
3233 "overread end of atom '%.4s' by %"PRId64" bytes\n",
3234 (char*)&a.type, -left);
3235 avio_seek(pb, left, SEEK_CUR);
3239 total_size += a.size;
3242 if (total_size < atom.size && atom.size < 0x7ffff)
3243 avio_skip(pb, atom.size - total_size);
3248 static int mov_probe(AVProbeData *p)
3253 int moov_offset = -1;
3255 /* check file header */
3258 /* ignore invalid offset */
3259 if ((offset + 8) > (unsigned int)p->buf_size)
3261 tag = AV_RL32(p->buf + offset + 4);
3263 /* check for obvious tags */
3264 case MKTAG('m','o','o','v'):
3265 moov_offset = offset + 4;
3266 case MKTAG('m','d','a','t'):
3267 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3268 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3269 case MKTAG('f','t','y','p'):
3270 if (AV_RB32(p->buf+offset) < 8 &&
3271 (AV_RB32(p->buf+offset) != 1 ||
3272 offset + 12 > (unsigned int)p->buf_size ||
3273 AV_RB64(p->buf+offset + 8) == 0)) {
3274 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3275 } else if (tag == MKTAG('f','t','y','p') &&
3276 AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')) {
3277 score = FFMAX(score, 5);
3279 score = AVPROBE_SCORE_MAX;
3281 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3283 /* those are more common words, so rate then a bit less */
3284 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3285 case MKTAG('w','i','d','e'):
3286 case MKTAG('f','r','e','e'):
3287 case MKTAG('j','u','n','k'):
3288 case MKTAG('p','i','c','t'):
3289 score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
3290 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3292 case MKTAG(0x82,0x82,0x7f,0x7d):
3293 case MKTAG('s','k','i','p'):
3294 case MKTAG('u','u','i','d'):
3295 case MKTAG('p','r','f','l'):
3296 /* if we only find those cause probedata is too small at least rate them */
3297 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3298 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3301 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3304 if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
3305 /* moov atom in the header - we should make sure that this is not a
3306 * MOV-packed MPEG-PS */
3307 offset = moov_offset;
3309 while(offset < (p->buf_size - 16)){ /* Sufficient space */
3310 /* We found an actual hdlr atom */
3311 if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
3312 AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
3313 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
3314 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
3315 /* We found a media handler reference atom describing an
3316 * MPEG-PS-in-MOV, return a
3317 * low score to force expanding the probe window until
3318 * mpegps_probe finds what it needs */
3329 // must be done after parsing all trak because there's no order requirement
3330 static void mov_read_chapters(AVFormatContext *s)
3332 MOVContext *mov = s->priv_data;
3333 AVStream *st = NULL;
3334 MOVStreamContext *sc;
3338 for (i = 0; i < s->nb_streams; i++)
3339 if (s->streams[i]->id == mov->chapter_track) {
3344 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3348 st->discard = AVDISCARD_ALL;
3350 cur_pos = avio_tell(sc->pb);
3352 for (i = 0; i < st->nb_index_entries; i++) {
3353 AVIndexEntry *sample = &st->index_entries[i];
3354 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3359 if (end < sample->timestamp) {
3360 av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
3361 end = AV_NOPTS_VALUE;
3364 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3365 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3369 // the first two bytes are the length of the title
3370 len = avio_rb16(sc->pb);
3371 if (len > sample->size-2)
3373 title_len = 2*len + 1;
3374 if (!(title = av_mallocz(title_len)))
3377 // The samples could theoretically be in any encoding if there's an encd
3378 // atom following, but in practice are only utf-8 or utf-16, distinguished
3379 // instead by the presence of a BOM
3383 ch = avio_rb16(sc->pb);
3385 avio_get_str16be(sc->pb, len, title, title_len);
3386 else if (ch == 0xfffe)
3387 avio_get_str16le(sc->pb, len, title, title_len);
3390 if (len == 1 || len == 2)
3393 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
3397 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3401 avio_seek(sc->pb, cur_pos, SEEK_SET);
3404 static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
3405 uint32_t value, int flags)
3408 char buf[AV_TIMECODE_STR_SIZE];
3409 AVRational rate = {st->codec->time_base.den,
3410 st->codec->time_base.num};
3411 int ret = av_timecode_init(&tc, rate, flags, 0, s);
3414 av_dict_set(&st->metadata, "timecode",
3415 av_timecode_make_string(&tc, buf, value), 0);
3419 static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
3421 MOVStreamContext *sc = st->priv_data;
3423 int64_t cur_pos = avio_tell(sc->pb);
3426 if (!st->nb_index_entries)
3429 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
3430 value = avio_rb32(s->pb);
3432 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
3433 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
3434 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
3436 /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
3437 * not the case) and thus assume "frame number format" instead of QT one.
3438 * No sample with tmcd track can be found with a QT timecode at the moment,
3439 * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
3441 parse_timecode_in_framenum_format(s, st, value, flags);
3443 avio_seek(sc->pb, cur_pos, SEEK_SET);
3447 static int mov_read_close(AVFormatContext *s)
3449 MOVContext *mov = s->priv_data;
3452 for (i = 0; i < s->nb_streams; i++) {
3453 AVStream *st = s->streams[i];
3454 MOVStreamContext *sc = st->priv_data;
3456 av_freep(&sc->ctts_data);
3457 for (j = 0; j < sc->drefs_count; j++) {
3458 av_freep(&sc->drefs[j].path);
3459 av_freep(&sc->drefs[j].dir);
3461 av_freep(&sc->drefs);
3463 sc->drefs_count = 0;
3465 if (!sc->pb_is_copied)
3469 av_freep(&sc->chunk_offsets);
3470 av_freep(&sc->stsc_data);
3471 av_freep(&sc->sample_sizes);
3472 av_freep(&sc->keyframes);
3473 av_freep(&sc->stts_data);
3474 av_freep(&sc->stps_data);
3475 av_freep(&sc->rap_group);
3476 av_freep(&sc->display_matrix);
3479 if (mov->dv_demux) {
3480 avformat_free_context(mov->dv_fctx);
3481 mov->dv_fctx = NULL;
3484 av_freep(&mov->trex_data);
3485 av_freep(&mov->bitrates);
3490 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
3494 for (i = 0; i < s->nb_streams; i++) {
3495 AVStream *st = s->streams[i];
3496 MOVStreamContext *sc = st->priv_data;
3498 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3499 sc->timecode_track == tmcd_id)
3505 /* look for a tmcd track not referenced by any video track, and export it globally */
3506 static void export_orphan_timecode(AVFormatContext *s)
3510 for (i = 0; i < s->nb_streams; i++) {
3511 AVStream *st = s->streams[i];
3513 if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
3514 !tmcd_is_referenced(s, i + 1)) {
3515 AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
3517 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
3524 static int mov_read_header(AVFormatContext *s)
3526 MOVContext *mov = s->priv_data;
3527 AVIOContext *pb = s->pb;
3529 MOVAtom atom = { AV_RL32("root") };
3533 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3535 atom.size = avio_size(pb);
3537 atom.size = INT64_MAX;
3539 /* check MOV header */
3541 if (mov->moov_retry)
3542 avio_seek(pb, 0, SEEK_SET);
3543 if ((err = mov_read_default(mov, pb, atom)) < 0) {
3544 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3548 } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
3549 if (!mov->found_moov) {
3550 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3552 return AVERROR_INVALIDDATA;
3554 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3557 if (mov->chapter_track > 0)
3558 mov_read_chapters(s);
3559 for (i = 0; i < s->nb_streams; i++)
3560 if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
3561 mov_read_timecode_track(s, s->streams[i]);
3564 /* copy timecode metadata from tmcd tracks to the related video streams */
3565 for (i = 0; i < s->nb_streams; i++) {
3566 AVStream *st = s->streams[i];
3567 MOVStreamContext *sc = st->priv_data;
3568 if (sc->timecode_track > 0) {
3569 AVDictionaryEntry *tcr;
3570 int tmcd_st_id = -1;
3572 for (j = 0; j < s->nb_streams; j++)
3573 if (s->streams[j]->id == sc->timecode_track)
3576 if (tmcd_st_id < 0 || tmcd_st_id == i)
3578 tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
3580 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
3583 export_orphan_timecode(s);
3585 for (i = 0; i < s->nb_streams; i++) {
3586 AVStream *st = s->streams[i];
3587 MOVStreamContext *sc = st->priv_data;
3588 fix_timescale(mov, sc);
3589 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
3590 st->skip_samples = sc->start_pad;
3592 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
3593 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
3594 sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
3595 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3596 if (st->codec->width <= 0 && st->codec->height <= 0) {
3597 st->codec->width = sc->width;
3598 st->codec->height = sc->height;
3600 if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
3601 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
3607 if (mov->trex_data) {
3608 for (i = 0; i < s->nb_streams; i++) {
3609 AVStream *st = s->streams[i];
3610 MOVStreamContext *sc = st->priv_data;
3611 if (st->duration > 0)
3612 st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3616 for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
3617 if (mov->bitrates[i]) {
3618 s->streams[i]->codec->bit_rate = mov->bitrates[i];
3622 ff_rfps_calculate(s);
3624 for (i = 0; i < s->nb_streams; i++) {
3625 AVStream *st = s->streams[i];
3626 MOVStreamContext *sc = st->priv_data;
3628 switch (st->codec->codec_type) {
3629 case AVMEDIA_TYPE_AUDIO:
3630 err = ff_replaygain_export(st, s->metadata);
3636 case AVMEDIA_TYPE_VIDEO:
3637 if (sc->display_matrix) {
3638 AVPacketSideData *sd, *tmp;
3640 tmp = av_realloc_array(st->side_data,
3641 st->nb_side_data + 1, sizeof(*tmp));
3643 return AVERROR(ENOMEM);
3645 st->side_data = tmp;
3648 sd = &st->side_data[st->nb_side_data - 1];
3649 sd->type = AV_PKT_DATA_DISPLAYMATRIX;
3650 sd->size = sizeof(int32_t) * 9;
3651 sd->data = (uint8_t*)sc->display_matrix;
3652 sc->display_matrix = NULL;
3661 static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
3663 AVIndexEntry *sample = NULL;
3664 int64_t best_dts = INT64_MAX;
3666 for (i = 0; i < s->nb_streams; i++) {
3667 AVStream *avst = s->streams[i];
3668 MOVStreamContext *msc = avst->priv_data;
3669 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3670 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3671 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3672 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3673 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3675 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3676 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3677 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3678 sample = current_sample;
3687 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
3689 MOVContext *mov = s->priv_data;
3690 MOVStreamContext *sc;
3691 AVIndexEntry *sample;
3692 AVStream *st = NULL;
3696 sample = mov_find_next_sample(s, &st);
3698 mov->found_mdat = 0;
3699 if (!mov->next_root_atom)
3701 avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3702 mov->next_root_atom = 0;
3703 if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3706 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3710 /* must be done just before reading, to avoid infinite loop on sample */
3711 sc->current_sample++;
3713 if (mov->next_root_atom) {
3714 sample->pos = FFMIN(sample->pos, mov->next_root_atom);
3715 sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
3718 if (st->discard != AVDISCARD_ALL) {
3719 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3720 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3721 sc->ffindex, sample->pos);
3722 return AVERROR_INVALIDDATA;
3724 ret = av_get_packet(sc->pb, pkt, sample->size);
3727 if (sc->has_palette) {
3730 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
3732 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3734 memcpy(pal, sc->palette, AVPALETTE_SIZE);
3735 sc->has_palette = 0;
3738 #if CONFIG_DV_DEMUXER
3739 if (mov->dv_demux && sc->dv_audio_container) {
3740 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
3743 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3750 pkt->stream_index = sc->ffindex;
3751 pkt->dts = sample->timestamp;
3752 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3753 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3754 /* update ctts context */
3756 if (sc->ctts_index < sc->ctts_count &&
3757 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3759 sc->ctts_sample = 0;
3762 pkt->dts = AV_NOPTS_VALUE;
3764 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3765 st->index_entries[sc->current_sample].timestamp : st->duration;
3766 pkt->duration = next_dts - pkt->dts;
3767 pkt->pts = pkt->dts;
3769 if (st->discard == AVDISCARD_ALL)
3771 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3772 pkt->pos = sample->pos;
3773 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
3774 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3778 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3780 MOVStreamContext *sc = st->priv_data;
3781 int sample, time_sample;
3784 sample = av_index_search_timestamp(st, timestamp, flags);
3785 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3786 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3788 if (sample < 0) /* not sure what to do */
3789 return AVERROR_INVALIDDATA;
3790 sc->current_sample = sample;
3791 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
3792 /* adjust ctts index */
3793 if (sc->ctts_data) {
3795 for (i = 0; i < sc->ctts_count; i++) {
3796 int next = time_sample + sc->ctts_data[i].count;
3797 if (next > sc->current_sample) {
3799 sc->ctts_sample = sc->current_sample - time_sample;
3808 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3811 int64_t seek_timestamp, timestamp;
3815 if (stream_index >= s->nb_streams)
3816 return AVERROR_INVALIDDATA;
3818 st = s->streams[stream_index];
3819 sample = mov_seek_stream(s, st, sample_time, flags);
3823 /* adjust seek timestamp to found sample timestamp */
3824 seek_timestamp = st->index_entries[sample].timestamp;
3826 for (i = 0; i < s->nb_streams; i++) {
3827 MOVStreamContext *sc = s->streams[i]->priv_data;
3829 st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
3831 if (stream_index == i)
3834 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
3835 mov_seek_stream(s, st, timestamp, flags);
3840 static const AVOption options[] = {
3841 {"use_absolute_path",
3842 "allow using absolute path when opening alias, this is a possible security issue",
3843 offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
3844 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
3845 {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
3846 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
3850 static const AVClass mov_class = {
3851 .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
3852 .item_name = av_default_item_name,
3854 .version = LIBAVUTIL_VERSION_INT,
3857 AVInputFormat ff_mov_demuxer = {
3858 .name = "mov,mp4,m4a,3gp,3g2,mj2",
3859 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3860 .priv_data_size = sizeof(MOVContext),
3861 .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
3862 .read_probe = mov_probe,
3863 .read_header = mov_read_header,
3864 .read_packet = mov_read_packet,
3865 .read_close = mov_read_close,
3866 .read_seek = mov_read_seek,
3867 .priv_class = &mov_class,
3868 .flags = AVFMT_NO_BYTE_SEEK,