3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/avstring.h"
32 #include "libavcodec/mpeg4audio.h"
33 #include "libavcodec/mpegaudiodata.h"
40 * First version by Francois Revol revol@free.fr
41 * Seek function by Gael Chardon gael.dev@4now.net
43 * Features and limitations:
44 * - reads most of the QT files I have (at least the structure),
45 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
46 * - the code is quite ugly... maybe I won't do it recursive next time :-)
48 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
49 * when coding this :) (it's a writer anyway)
51 * Reference documents:
52 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
54 * http://developer.apple.com/documentation/QuickTime/QTFF/
55 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
56 * QuickTime is a trademark of Apple (AFAIK :))
59 #include "qtpalette.h"
65 /* the QuickTime file format is quite convoluted...
66 * it has lots of index tables, each indexing something in another one...
67 * Here we just use what is needed to read the chunks
84 int64_t size; /* total size (excluding the size and type fields) */
87 struct MOVParseTableEntry;
91 uint64_t base_data_offset;
107 typedef struct MOVStreamContext {
109 int ffindex; /* the ffmpeg stream id */
111 unsigned int chunk_count;
112 int64_t *chunk_offsets;
113 unsigned int stts_count;
115 unsigned int ctts_count;
117 unsigned int stsc_count;
121 unsigned int sample_size;
122 unsigned int sample_count;
124 unsigned int keyframe_count;
128 int time_offset; ///< time offset of the first edit list entry
130 unsigned int bytes_per_frame;
131 unsigned int samples_per_frame;
132 int dv_audio_container;
133 int pseudo_stream_id; ///< -1 means demux all ids
134 int16_t audio_cid; ///< stsd audio compression id
135 unsigned drefs_count;
138 int wrong_dts; ///< dts are wrong due to negative ctts
139 int width; ///< tkhd width
140 int height; ///< tkhd height
143 typedef struct MOVContext {
146 int64_t duration; /* duration of the longest track */
147 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
148 int found_mdat; /* we suppose we have enough data to read the file */
149 AVPaletteControl palette_control;
150 DVDemuxContext *dv_demux;
151 AVFormatContext *dv_fctx;
152 int isom; /* 1 if file is ISO Media (mp4/3gp) */
153 MOVFragment fragment; ///< current fragment in moof atom
154 MOVTrackExt *trex_data;
156 int itunes_metadata; ///< metadata are itunes style
160 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
162 /* those functions parse an atom */
164 0: continue to parse next atom
165 <0: error occurred, exit
167 /* links atom IDs to parse functions */
168 typedef struct MOVParseTableEntry {
170 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
171 } MOVParseTableEntry;
173 static const MOVParseTableEntry mov_default_parse_table[];
175 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
177 int64_t total_size = 0;
182 a.offset = atom.offset;
185 atom.size = INT64_MAX;
186 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
190 a.size = get_be32(pb);
191 a.type = get_le32(pb);
195 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
196 a.type, (char*)&a.type, a.size, atom.size, total_size);
197 if (a.size == 1) { /* 64 bit extended size */
198 a.size = get_be64(pb) - 8;
203 a.size = atom.size - total_size;
210 a.size = FFMIN(a.size, atom.size - total_size);
212 for (i = 0; mov_default_parse_table[i].type != 0
213 && mov_default_parse_table[i].type != a.type; i++)
216 if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
217 url_fskip(pb, a.size);
219 int64_t start_pos = url_ftell(pb);
221 err = mov_default_parse_table[i].parse(c, pb, a);
222 if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
224 left = a.size - url_ftell(pb) + start_pos;
225 if (left > 0) /* skip garbage at atom end */
230 total_size += a.size;
233 if (!err && total_size < atom.size && atom.size < 0x7ffff)
234 url_fskip(pb, atom.size - total_size);
239 static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
241 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
242 MOVStreamContext *sc = st->priv_data;
245 get_be32(pb); // version + flags
246 entries = get_be32(pb);
247 if (entries >= UINT_MAX / sizeof(*sc->drefs))
249 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
251 return AVERROR(ENOMEM);
252 sc->drefs_count = entries;
254 for (i = 0; i < sc->drefs_count; i++) {
255 MOVDref *dref = &sc->drefs[i];
256 uint32_t size = get_be32(pb);
257 int64_t next = url_ftell(pb) + size - 4;
259 dref->type = get_le32(pb);
260 get_be32(pb); // version + flags
261 dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
263 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
264 /* macintosh alias record */
265 uint16_t volume_len, len;
271 volume_len = get_byte(pb);
272 volume_len = FFMIN(volume_len, 27);
273 get_buffer(pb, volume, 27);
274 volume[volume_len] = 0;
275 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
279 for (type = 0; type != -1 && url_ftell(pb) < next; ) {
282 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
285 if (type == 2) { // absolute path
287 dref->path = av_mallocz(len+1);
289 return AVERROR(ENOMEM);
290 get_buffer(pb, dref->path, len);
291 if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
293 memmove(dref->path, dref->path+volume_len, len);
296 for (j = 0; j < len; j++)
297 if (dref->path[j] == ':')
299 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
304 url_fseek(pb, next, SEEK_SET);
309 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
311 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
315 get_byte(pb); /* version */
316 get_be24(pb); /* flags */
319 ctype = get_le32(pb);
320 type = get_le32(pb); /* component subtype */
322 dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
323 ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
324 dprintf(c->fc, "stype= %c%c%c%c\n",
325 *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
328 if (type == MKTAG('v','i','d','e'))
329 st->codec->codec_type = CODEC_TYPE_VIDEO;
330 else if(type == MKTAG('s','o','u','n'))
331 st->codec->codec_type = CODEC_TYPE_AUDIO;
332 else if(type == MKTAG('m','1','a',' '))
333 st->codec->codec_id = CODEC_ID_MP2;
334 else if(type == MKTAG('s','u','b','p')) {
335 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
337 get_be32(pb); /* component manufacture */
338 get_be32(pb); /* component flags */
339 get_be32(pb); /* component flags mask */
342 return 0; /* nothing left to read */
344 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
348 static int mp4_read_descr_len(ByteIOContext *pb)
353 int c = get_byte(pb);
354 len = (len << 7) | (c & 0x7f);
361 static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
365 len = mp4_read_descr_len(pb);
366 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
370 #define MP4ESDescrTag 0x03
371 #define MP4DecConfigDescrTag 0x04
372 #define MP4DecSpecificDescrTag 0x05
374 static const AVCodecTag mp4_audio_types[] = {
375 { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */
376 { CODEC_ID_MP3ON4, 32 }, /* layer 1 */
377 { CODEC_ID_MP3ON4, 33 }, /* layer 2 */
378 { CODEC_ID_MP3ON4, 34 }, /* layer 3 */
379 { CODEC_ID_NONE, 0 },
382 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
384 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
387 get_be32(pb); /* version + flags */
388 len = mp4_read_descr(c, pb, &tag);
389 if (tag == MP4ESDescrTag) {
390 get_be16(pb); /* ID */
391 get_byte(pb); /* priority */
393 get_be16(pb); /* ID */
395 len = mp4_read_descr(c, pb, &tag);
396 if (tag == MP4DecConfigDescrTag) {
397 int object_type_id = get_byte(pb);
398 get_byte(pb); /* stream type */
399 get_be24(pb); /* buffer size db */
400 get_be32(pb); /* max bitrate */
401 get_be32(pb); /* avg bitrate */
403 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
404 dprintf(c->fc, "esds object type id %d\n", object_type_id);
405 len = mp4_read_descr(c, pb, &tag);
406 if (tag == MP4DecSpecificDescrTag) {
407 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
408 if((uint64_t)len > (1<<30))
410 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
411 if (!st->codec->extradata)
412 return AVERROR(ENOMEM);
413 get_buffer(pb, st->codec->extradata, len);
414 st->codec->extradata_size = len;
415 if (st->codec->codec_id == CODEC_ID_AAC) {
416 MPEG4AudioConfig cfg;
417 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
418 st->codec->extradata_size);
419 if (cfg.chan_config > 7)
421 st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
422 if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
423 st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
425 st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
426 dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d "
427 "sample rate %d ext sample rate %d\n", st->codec->channels,
428 cfg.object_type, cfg.ext_object_type,
429 cfg.sample_rate, cfg.ext_sample_rate);
430 if (!(st->codec->codec_id = codec_get_id(mp4_audio_types,
432 st->codec->codec_id = CODEC_ID_AAC;
439 static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
441 const int num = get_be32(pb);
442 const int den = get_be32(pb);
443 AVStream * const st = c->fc->streams[c->fc->nb_streams-1];
445 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
446 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
447 av_log(c->fc, AV_LOG_WARNING,
448 "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
449 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
450 st->sample_aspect_ratio.num = num;
451 st->sample_aspect_ratio.den = den;
456 /* this atom contains actual media data */
457 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
459 if(atom.size == 0) /* wrong one (MP4) */
462 return 0; /* now go for moov */
465 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
467 uint32_t type = get_le32(pb);
469 if (type != MKTAG('q','t',' ',' '))
471 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
472 get_be32(pb); /* minor version */
473 url_fskip(pb, atom.size - 8);
477 /* this atom should contain all header atoms */
478 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
480 if (mov_read_default(c, pb, atom) < 0)
482 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
483 /* so we don't parse the whole file if over a network */
485 return 0; /* now go for mdat */
488 static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
490 c->fragment.moof_offset = url_ftell(pb) - 8;
491 dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
492 return mov_read_default(c, pb, atom);
495 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
497 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
498 MOVStreamContext *sc = st->priv_data;
499 int version = get_byte(pb);
500 char language[4] = {0};
504 return -1; /* unsupported */
506 get_be24(pb); /* flags */
511 get_be32(pb); /* creation time */
512 get_be32(pb); /* modification time */
515 sc->time_scale = get_be32(pb);
516 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
518 lang = get_be16(pb); /* language */
519 if (ff_mov_lang_to_iso639(lang, language))
520 av_metadata_set(&st->metadata, "language", language);
521 get_be16(pb); /* quality */
526 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
528 int version = get_byte(pb); /* version */
529 get_be24(pb); /* flags */
535 get_be32(pb); /* creation time */
536 get_be32(pb); /* modification time */
538 c->time_scale = get_be32(pb); /* time scale */
540 dprintf(c->fc, "time scale = %i\n", c->time_scale);
542 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
543 get_be32(pb); /* preferred scale */
545 get_be16(pb); /* preferred volume */
547 url_fskip(pb, 10); /* reserved */
549 url_fskip(pb, 36); /* display matrix */
551 get_be32(pb); /* preview time */
552 get_be32(pb); /* preview duration */
553 get_be32(pb); /* poster time */
554 get_be32(pb); /* selection time */
555 get_be32(pb); /* selection duration */
556 get_be32(pb); /* current time */
557 get_be32(pb); /* next track ID */
562 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
564 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
566 if((uint64_t)atom.size > (1<<30))
569 // currently SVQ3 decoder expect full STSD header - so let's fake it
570 // this should be fixed and just SMI header should be passed
571 av_free(st->codec->extradata);
572 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
573 if (!st->codec->extradata)
574 return AVERROR(ENOMEM);
575 st->codec->extradata_size = 0x5a + atom.size;
576 memcpy(st->codec->extradata, "SVQ3", 4); // fake
577 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
578 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
582 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
584 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
585 int little_endian = get_be16(pb);
587 dprintf(c->fc, "enda %d\n", little_endian);
588 if (little_endian == 1) {
589 switch (st->codec->codec_id) {
590 case CODEC_ID_PCM_S24BE:
591 st->codec->codec_id = CODEC_ID_PCM_S24LE;
593 case CODEC_ID_PCM_S32BE:
594 st->codec->codec_id = CODEC_ID_PCM_S32LE;
596 case CODEC_ID_PCM_F32BE:
597 st->codec->codec_id = CODEC_ID_PCM_F32LE;
599 case CODEC_ID_PCM_F64BE:
600 st->codec->codec_id = CODEC_ID_PCM_F64LE;
609 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
610 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
616 if (c->fc->nb_streams < 1) // will happen with jp2 files
618 st= c->fc->streams[c->fc->nb_streams-1];
619 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
620 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
622 buf= av_realloc(st->codec->extradata, size);
625 st->codec->extradata= buf;
626 buf+= st->codec->extradata_size;
627 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
628 AV_WB32( buf , atom.size + 8);
629 AV_WL32( buf + 4, atom.type);
630 get_buffer(pb, buf + 8, atom.size);
634 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
636 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
638 if((uint64_t)atom.size > (1<<30))
641 if (st->codec->codec_id == CODEC_ID_QDM2) {
642 // pass all frma atom to codec, needed at least for QDM2
643 av_free(st->codec->extradata);
644 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
645 if (!st->codec->extradata)
646 return AVERROR(ENOMEM);
647 st->codec->extradata_size = atom.size;
648 get_buffer(pb, st->codec->extradata, atom.size);
649 } else if (atom.size > 8) { /* to read frma, esds atoms */
650 if (mov_read_default(c, pb, atom) < 0)
653 url_fskip(pb, atom.size);
658 * This function reads atom content and puts data in extradata without tag
659 * nor size unlike mov_read_extradata.
661 static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
663 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
665 if((uint64_t)atom.size > (1<<30))
668 av_free(st->codec->extradata);
669 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
670 if (!st->codec->extradata)
671 return AVERROR(ENOMEM);
672 st->codec->extradata_size = atom.size;
673 get_buffer(pb, st->codec->extradata, atom.size);
677 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
679 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
680 MOVStreamContext *sc = st->priv_data;
681 unsigned int i, entries;
683 get_byte(pb); /* version */
684 get_be24(pb); /* flags */
686 entries = get_be32(pb);
688 if(entries >= UINT_MAX/sizeof(int64_t))
691 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
692 if (!sc->chunk_offsets)
693 return AVERROR(ENOMEM);
694 sc->chunk_count = entries;
696 if (atom.type == MKTAG('s','t','c','o'))
697 for(i=0; i<entries; i++)
698 sc->chunk_offsets[i] = get_be32(pb);
699 else if (atom.type == MKTAG('c','o','6','4'))
700 for(i=0; i<entries; i++)
701 sc->chunk_offsets[i] = get_be64(pb);
709 * Compute codec id for 'lpcm' tag.
710 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
712 static enum CodecID mov_get_lpcm_codec_id(int bps, int flags)
714 if (flags & 1) { // floating point
715 if (flags & 2) { // big endian
716 if (bps == 32) return CODEC_ID_PCM_F32BE;
717 else if (bps == 64) return CODEC_ID_PCM_F64BE;
719 if (bps == 32) return CODEC_ID_PCM_F32LE;
720 else if (bps == 64) return CODEC_ID_PCM_F64LE;
726 if (flags & 4) return CODEC_ID_PCM_S8;
727 else return CODEC_ID_PCM_U8;
728 else if (bps == 16) return CODEC_ID_PCM_S16BE;
729 else if (bps == 24) return CODEC_ID_PCM_S24BE;
730 else if (bps == 32) return CODEC_ID_PCM_S32BE;
733 if (flags & 4) return CODEC_ID_PCM_S8;
734 else return CODEC_ID_PCM_U8;
735 else if (bps == 16) return CODEC_ID_PCM_S16LE;
736 else if (bps == 24) return CODEC_ID_PCM_S24LE;
737 else if (bps == 32) return CODEC_ID_PCM_S32LE;
740 return CODEC_ID_NONE;
743 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
745 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
746 MOVStreamContext *sc = st->priv_data;
747 int j, entries, pseudo_stream_id;
749 get_byte(pb); /* version */
750 get_be24(pb); /* flags */
752 entries = get_be32(pb);
754 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
755 //Parsing Sample description table
758 MOVAtom a = { 0, 0, 0 };
759 int64_t start_pos = url_ftell(pb);
760 int size = get_be32(pb); /* size */
761 uint32_t format = get_le32(pb); /* data format */
763 get_be32(pb); /* reserved */
764 get_be16(pb); /* reserved */
765 dref_id = get_be16(pb);
767 if (st->codec->codec_tag &&
768 st->codec->codec_tag != format &&
769 (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
770 : st->codec->codec_tag != MKTAG('j','p','e','g'))
772 /* Multiple fourcc, we skip JPEG. This is not correct, we should
773 * export it as a separate AVStream but this needs a few changes
774 * in the MOV demuxer, patch welcome. */
775 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
776 url_fskip(pb, size - (url_ftell(pb) - start_pos));
779 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
780 sc->dref_id= dref_id;
782 st->codec->codec_tag = format;
783 id = codec_get_id(codec_movaudio_tags, format);
784 if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8))
785 id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
787 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
788 st->codec->codec_type = CODEC_TYPE_AUDIO;
789 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
790 format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
791 id = codec_get_id(codec_movvideo_tags, format);
793 id = codec_get_id(codec_bmp_tags, format);
795 st->codec->codec_type = CODEC_TYPE_VIDEO;
796 else if(st->codec->codec_type == CODEC_TYPE_DATA){
797 id = codec_get_id(ff_codec_movsubtitle_tags, format);
799 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
803 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
804 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
805 (format >> 24) & 0xff, st->codec->codec_type);
807 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
808 uint8_t codec_name[32];
809 unsigned int color_depth;
812 st->codec->codec_id = id;
813 get_be16(pb); /* version */
814 get_be16(pb); /* revision level */
815 get_be32(pb); /* vendor */
816 get_be32(pb); /* temporal quality */
817 get_be32(pb); /* spatial quality */
819 st->codec->width = get_be16(pb); /* width */
820 st->codec->height = get_be16(pb); /* height */
822 get_be32(pb); /* horiz resolution */
823 get_be32(pb); /* vert resolution */
824 get_be32(pb); /* data size, always 0 */
825 get_be16(pb); /* frames per samples */
827 get_buffer(pb, codec_name, 32); /* codec name, pascal string */
828 if (codec_name[0] <= 31) {
829 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
830 st->codec->codec_name[codec_name[0]] = 0;
833 st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
834 st->codec->color_table_id = get_be16(pb); /* colortable id */
835 dprintf(c->fc, "depth %d, ctab id %d\n",
836 st->codec->bits_per_coded_sample, st->codec->color_table_id);
837 /* figure out the palette situation */
838 color_depth = st->codec->bits_per_coded_sample & 0x1F;
839 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
841 /* if the depth is 2, 4, or 8 bpp, file is palettized */
842 if ((color_depth == 2) || (color_depth == 4) ||
843 (color_depth == 8)) {
844 /* for palette traversal */
845 unsigned int color_start, color_count, color_end;
846 unsigned char r, g, b;
848 if (color_greyscale) {
849 int color_index, color_dec;
850 /* compute the greyscale palette */
851 st->codec->bits_per_coded_sample = color_depth;
852 color_count = 1 << color_depth;
854 color_dec = 256 / (color_count - 1);
855 for (j = 0; j < color_count; j++) {
856 r = g = b = color_index;
857 c->palette_control.palette[j] =
858 (r << 16) | (g << 8) | (b);
859 color_index -= color_dec;
863 } else if (st->codec->color_table_id) {
864 const uint8_t *color_table;
865 /* if flag bit 3 is set, use the default palette */
866 color_count = 1 << color_depth;
867 if (color_depth == 2)
868 color_table = ff_qt_default_palette_4;
869 else if (color_depth == 4)
870 color_table = ff_qt_default_palette_16;
872 color_table = ff_qt_default_palette_256;
874 for (j = 0; j < color_count; j++) {
875 r = color_table[j * 4 + 0];
876 g = color_table[j * 4 + 1];
877 b = color_table[j * 4 + 2];
878 c->palette_control.palette[j] =
879 (r << 16) | (g << 8) | (b);
882 /* load the palette from the file */
883 color_start = get_be32(pb);
884 color_count = get_be16(pb);
885 color_end = get_be16(pb);
886 if ((color_start <= 255) &&
887 (color_end <= 255)) {
888 for (j = color_start; j <= color_end; j++) {
889 /* each R, G, or B component is 16 bits;
890 * only use the top 8 bits; skip alpha bytes
900 c->palette_control.palette[j] =
901 (r << 16) | (g << 8) | (b);
905 st->codec->palctrl = &c->palette_control;
906 st->codec->palctrl->palette_changed = 1;
908 st->codec->palctrl = NULL;
909 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
910 int bits_per_sample, flags;
911 uint16_t version = get_be16(pb);
913 st->codec->codec_id = id;
914 get_be16(pb); /* revision level */
915 get_be32(pb); /* vendor */
917 st->codec->channels = get_be16(pb); /* channel count */
918 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
919 st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
921 sc->audio_cid = get_be16(pb);
922 get_be16(pb); /* packet size = 0 */
924 st->codec->sample_rate = ((get_be32(pb) >> 16));
926 //Read QT version 1 fields. In version 0 these do not exist.
927 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
930 sc->samples_per_frame = get_be32(pb);
931 get_be32(pb); /* bytes per packet */
932 sc->bytes_per_frame = get_be32(pb);
933 get_be32(pb); /* bytes per sample */
934 } else if(version==2) {
935 get_be32(pb); /* sizeof struct only */
936 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
937 st->codec->channels = get_be32(pb);
938 get_be32(pb); /* always 0x7F000000 */
939 st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
940 flags = get_be32(pb); /* lcpm format specific flag */
941 sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
942 sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
943 if (format == MKTAG('l','p','c','m'))
944 st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
948 switch (st->codec->codec_id) {
949 case CODEC_ID_PCM_S8:
950 case CODEC_ID_PCM_U8:
951 if (st->codec->bits_per_coded_sample == 16)
952 st->codec->codec_id = CODEC_ID_PCM_S16BE;
954 case CODEC_ID_PCM_S16LE:
955 case CODEC_ID_PCM_S16BE:
956 if (st->codec->bits_per_coded_sample == 8)
957 st->codec->codec_id = CODEC_ID_PCM_S8;
958 else if (st->codec->bits_per_coded_sample == 24)
959 st->codec->codec_id =
960 st->codec->codec_id == CODEC_ID_PCM_S16BE ?
961 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
963 /* set values for old format before stsd version 1 appeared */
965 sc->samples_per_frame = 6;
966 sc->bytes_per_frame = 2*st->codec->channels;
969 sc->samples_per_frame = 6;
970 sc->bytes_per_frame = 1*st->codec->channels;
972 case CODEC_ID_ADPCM_IMA_QT:
973 sc->samples_per_frame = 64;
974 sc->bytes_per_frame = 34*st->codec->channels;
977 sc->samples_per_frame = 160;
978 sc->bytes_per_frame = 33;
984 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
985 if (bits_per_sample) {
986 st->codec->bits_per_coded_sample = bits_per_sample;
987 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
989 } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
990 // ttxt stsd contains display flags, justification, background
991 // color, fonts, and default styles, so fake an atom to read it
992 MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
993 mov_read_glbl(c, pb, fake_atom);
994 st->codec->codec_id= id;
995 st->codec->width = sc->width;
996 st->codec->height = sc->height;
998 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
999 url_fskip(pb, size - (url_ftell(pb) - start_pos));
1001 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
1002 a.size = size - (url_ftell(pb) - start_pos);
1004 if (mov_read_default(c, pb, a) < 0)
1006 } else if (a.size > 0)
1007 url_fskip(pb, a.size);
1010 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
1011 st->codec->sample_rate= sc->time_scale;
1013 /* special codec parameters handling */
1014 switch (st->codec->codec_id) {
1015 #if CONFIG_DV_DEMUXER
1016 case CODEC_ID_DVAUDIO:
1017 c->dv_fctx = avformat_alloc_context();
1018 c->dv_demux = dv_init_demux(c->dv_fctx);
1020 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1023 sc->dv_audio_container = 1;
1024 st->codec->codec_id = CODEC_ID_PCM_S16LE;
1027 /* no ifdef since parameters are always those */
1028 case CODEC_ID_QCELP:
1029 st->codec->frame_size= 160;
1030 st->codec->channels= 1; /* really needed */
1032 case CODEC_ID_AMR_NB:
1033 case CODEC_ID_AMR_WB:
1034 st->codec->frame_size= sc->samples_per_frame;
1035 st->codec->channels= 1; /* really needed */
1036 /* force sample rate for amr, stsd in 3gp does not store sample rate */
1037 if (st->codec->codec_id == CODEC_ID_AMR_NB)
1038 st->codec->sample_rate = 8000;
1039 else if (st->codec->codec_id == CODEC_ID_AMR_WB)
1040 st->codec->sample_rate = 16000;
1044 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
1045 st->need_parsing = AVSTREAM_PARSE_FULL;
1048 case CODEC_ID_ADPCM_MS:
1049 case CODEC_ID_ADPCM_IMA_WAV:
1050 st->codec->block_align = sc->bytes_per_frame;
1053 if (st->codec->extradata_size == 36) {
1054 st->codec->frame_size = AV_RB32(st->codec->extradata+12);
1055 st->codec->channels = AV_RB8 (st->codec->extradata+21);
1065 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1067 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1068 MOVStreamContext *sc = st->priv_data;
1069 unsigned int i, entries;
1071 get_byte(pb); /* version */
1072 get_be24(pb); /* flags */
1074 entries = get_be32(pb);
1076 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1078 if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
1080 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1082 return AVERROR(ENOMEM);
1083 sc->stsc_count = entries;
1085 for(i=0; i<entries; i++) {
1086 sc->stsc_data[i].first = get_be32(pb);
1087 sc->stsc_data[i].count = get_be32(pb);
1088 sc->stsc_data[i].id = get_be32(pb);
1093 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1095 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1096 MOVStreamContext *sc = st->priv_data;
1097 unsigned int i, entries;
1099 get_byte(pb); /* version */
1100 get_be24(pb); /* flags */
1102 entries = get_be32(pb);
1104 dprintf(c->fc, "keyframe_count = %d\n", entries);
1106 if(entries >= UINT_MAX / sizeof(int))
1108 sc->keyframes = av_malloc(entries * sizeof(int));
1110 return AVERROR(ENOMEM);
1111 sc->keyframe_count = entries;
1113 for(i=0; i<entries; i++) {
1114 sc->keyframes[i] = get_be32(pb);
1115 //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1120 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1122 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1123 MOVStreamContext *sc = st->priv_data;
1124 unsigned int i, entries, sample_size;
1126 get_byte(pb); /* version */
1127 get_be24(pb); /* flags */
1129 sample_size = get_be32(pb);
1130 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1131 sc->sample_size = sample_size;
1132 entries = get_be32(pb);
1134 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1136 sc->sample_count = entries;
1140 if(entries >= UINT_MAX / sizeof(int))
1142 sc->sample_sizes = av_malloc(entries * sizeof(int));
1143 if (!sc->sample_sizes)
1144 return AVERROR(ENOMEM);
1146 for(i=0; i<entries; i++)
1147 sc->sample_sizes[i] = get_be32(pb);
1151 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1153 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1154 MOVStreamContext *sc = st->priv_data;
1155 unsigned int i, entries;
1157 int64_t total_sample_count=0;
1159 get_byte(pb); /* version */
1160 get_be24(pb); /* flags */
1161 entries = get_be32(pb);
1163 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1165 if(entries >= UINT_MAX / sizeof(*sc->stts_data))
1167 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1169 return AVERROR(ENOMEM);
1170 sc->stts_count = entries;
1172 for(i=0; i<entries; i++) {
1173 int sample_duration;
1176 sample_count=get_be32(pb);
1177 sample_duration = get_be32(pb);
1178 sc->stts_data[i].count= sample_count;
1179 sc->stts_data[i].duration= sample_duration;
1181 sc->time_rate= av_gcd(sc->time_rate, sample_duration);
1183 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1185 duration+=(int64_t)sample_duration*sample_count;
1186 total_sample_count+=sample_count;
1189 st->nb_frames= total_sample_count;
1191 st->duration= duration;
1195 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1197 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1198 MOVStreamContext *sc = st->priv_data;
1199 unsigned int i, entries;
1201 get_byte(pb); /* version */
1202 get_be24(pb); /* flags */
1203 entries = get_be32(pb);
1205 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1207 if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
1209 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
1211 return AVERROR(ENOMEM);
1212 sc->ctts_count = entries;
1214 for(i=0; i<entries; i++) {
1215 int count =get_be32(pb);
1216 int duration =get_be32(pb);
1220 st->codec->has_b_frames = 1;
1222 sc->ctts_data[i].count = count;
1223 sc->ctts_data[i].duration= duration;
1225 sc->time_rate= av_gcd(sc->time_rate, FFABS(duration));
1230 static void mov_build_index(MOVContext *mov, AVStream *st)
1232 MOVStreamContext *sc = st->priv_data;
1233 int64_t current_offset;
1234 int64_t current_dts = 0;
1235 unsigned int stts_index = 0;
1236 unsigned int stsc_index = 0;
1237 unsigned int stss_index = 0;
1240 /* adjust first dts according to edit list */
1241 if (sc->time_offset) {
1242 assert(sc->time_offset % sc->time_rate == 0);
1243 current_dts = - (sc->time_offset / sc->time_rate);
1246 /* only use old uncompressed audio chunk demuxing when stts specifies it */
1247 if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
1248 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
1249 unsigned int current_sample = 0;
1250 unsigned int stts_sample = 0;
1251 unsigned int keyframe, sample_size;
1252 unsigned int distance = 0;
1253 int key_off = sc->keyframes && sc->keyframes[0] == 1;
1255 st->nb_frames = sc->sample_count;
1256 for (i = 0; i < sc->chunk_count; i++) {
1257 current_offset = sc->chunk_offsets[i];
1258 if (stsc_index + 1 < sc->stsc_count &&
1259 i + 1 == sc->stsc_data[stsc_index + 1].first)
1261 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
1262 if (current_sample >= sc->sample_count) {
1263 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1266 keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
1269 if (stss_index + 1 < sc->keyframe_count)
1272 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1273 if(sc->pseudo_stream_id == -1 ||
1274 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
1275 av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
1276 keyframe ? AVINDEX_KEYFRAME : 0);
1277 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1278 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
1279 current_offset, current_dts, sample_size, distance, keyframe);
1281 current_offset += sample_size;
1282 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1283 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1287 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1293 } else { /* read whole chunk */
1294 unsigned int chunk_samples, chunk_size, chunk_duration;
1295 unsigned int frames = 1;
1296 for (i = 0; i < sc->chunk_count; i++) {
1297 current_offset = sc->chunk_offsets[i];
1298 if (stsc_index + 1 < sc->stsc_count &&
1299 i + 1 == sc->stsc_data[stsc_index + 1].first)
1301 chunk_samples = sc->stsc_data[stsc_index].count;
1302 /* get chunk size, beware of alaw/ulaw/mace */
1303 if (sc->samples_per_frame > 0 &&
1304 (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
1305 if (sc->samples_per_frame < 160)
1306 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1308 chunk_size = sc->bytes_per_frame;
1309 frames = chunk_samples / sc->samples_per_frame;
1310 chunk_samples = sc->samples_per_frame;
1313 chunk_size = chunk_samples * sc->sample_size;
1314 for (j = 0; j < frames; j++) {
1315 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1316 /* get chunk duration */
1318 while (chunk_samples > 0) {
1319 if (chunk_samples < sc->stts_data[stts_index].count) {
1320 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1321 sc->stts_data[stts_index].count -= chunk_samples;
1324 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1325 chunk_samples -= sc->stts_data[stts_index].count;
1326 if (stts_index + 1 < sc->stts_count)
1330 current_offset += sc->bytes_per_frame;
1331 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
1332 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
1333 chunk_size, chunk_duration);
1334 assert(chunk_duration % sc->time_rate == 0);
1335 current_dts += chunk_duration / sc->time_rate;
1340 /* adjust sample count to avindex entries */
1341 sc->sample_count = st->nb_index_entries;
1344 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1347 MOVStreamContext *sc;
1350 st = av_new_stream(c->fc, c->fc->nb_streams);
1351 if (!st) return AVERROR(ENOMEM);
1352 sc = av_mallocz(sizeof(MOVStreamContext));
1353 if (!sc) return AVERROR(ENOMEM);
1356 st->codec->codec_type = CODEC_TYPE_DATA;
1357 sc->ffindex = st->index;
1359 if ((ret = mov_read_default(c, pb, atom)) < 0)
1363 if(sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
1364 (!sc->sample_size && !sc->sample_count))){
1365 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
1367 sc->sample_count = 0; //ignore track
1373 sc->time_scale= c->time_scale;
1374 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1376 if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
1377 !st->codec->frame_size && sc->stts_count == 1) {
1378 st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
1379 st->codec->sample_rate, sc->time_scale);
1380 dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
1383 if(st->duration != AV_NOPTS_VALUE){
1384 assert(st->duration % sc->time_rate == 0);
1385 st->duration /= sc->time_rate;
1388 mov_build_index(c, st);
1390 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
1391 if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
1392 av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
1393 st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
1397 switch (st->codec->codec_id) {
1398 #if CONFIG_H261_DECODER
1401 #if CONFIG_H263_DECODER
1404 #if CONFIG_MPEG4_DECODER
1405 case CODEC_ID_MPEG4:
1407 st->codec->width= 0; /* let decoder init width/height */
1408 st->codec->height= 0;
1412 /* Do not need those anymore. */
1413 av_freep(&sc->chunk_offsets);
1414 av_freep(&sc->stsc_data);
1415 av_freep(&sc->sample_sizes);
1416 av_freep(&sc->keyframes);
1417 av_freep(&sc->stts_data);
1422 static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1425 c->itunes_metadata = 1;
1426 ret = mov_read_default(c, pb, atom);
1427 c->itunes_metadata = 0;
1431 static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1433 url_fskip(pb, 4); // version + flags
1435 return mov_read_default(c, pb, atom);
1438 static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1440 get_be32(pb); // type
1441 get_be32(pb); // unknown
1442 c->fc->track = get_be32(pb);
1443 dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track);
1447 static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1449 char str[1024], key2[16], language[4] = {0};
1450 const char *key = NULL;
1453 if (c->itunes_metadata) {
1454 int data_size = get_be32(pb);
1455 int tag = get_le32(pb);
1456 if (tag == MKTAG('d','a','t','a')) {
1457 get_be32(pb); // type
1458 get_be32(pb); // unknown
1459 str_size = data_size - 16;
1463 str_size = get_be16(pb); // string length
1464 ff_mov_lang_to_iso639(get_be16(pb), language);
1467 switch (atom.type) {
1468 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
1469 case MKTAG(0xa9,'a','u','t'):
1470 case MKTAG(0xa9,'A','R','T'):
1471 case MKTAG(0xa9,'w','r','t'): key = "author"; break;
1472 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
1473 case MKTAG(0xa9,'c','m','t'):
1474 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
1475 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
1476 case MKTAG(0xa9,'d','a','y'): key = "year"; break;
1477 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
1478 case MKTAG(0xa9,'t','o','o'):
1479 case MKTAG(0xa9,'e','n','c'): key = "muxer"; break;
1486 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
1487 get_buffer(pb, str, str_size);
1489 av_metadata_set(&c->fc->metadata, key, str);
1490 if (*language && strcmp(language, "und")) {
1491 snprintf(key2, sizeof(key2), "%s-%s", key, language);
1492 av_metadata_set(&c->fc->metadata, key2, str);
1494 dprintf(c->fc, "%.4s %s %d %lld\n", (char*)&atom.type, str, str_size, atom.size);
1498 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1503 int64_t disp_transform[2];
1504 int display_matrix[3][2];
1505 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1506 MOVStreamContext *sc = st->priv_data;
1507 int version = get_byte(pb);
1509 get_be24(pb); /* flags */
1511 MOV_TRACK_ENABLED 0x0001
1512 MOV_TRACK_IN_MOVIE 0x0002
1513 MOV_TRACK_IN_PREVIEW 0x0004
1514 MOV_TRACK_IN_POSTER 0x0008
1521 get_be32(pb); /* creation time */
1522 get_be32(pb); /* modification time */
1524 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1525 get_be32(pb); /* reserved */
1527 /* highlevel (considering edits) duration in movie timebase */
1528 (version == 1) ? get_be64(pb) : get_be32(pb);
1529 get_be32(pb); /* reserved */
1530 get_be32(pb); /* reserved */
1532 get_be16(pb); /* layer */
1533 get_be16(pb); /* alternate group */
1534 get_be16(pb); /* volume */
1535 get_be16(pb); /* reserved */
1537 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
1538 // they're kept in fixed point format through all calculations
1539 // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
1540 for (i = 0; i < 3; i++) {
1541 display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
1542 display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
1543 get_be32(pb); // 2.30 fixed point (not used)
1546 width = get_be32(pb); // 16.16 fixed point track width
1547 height = get_be32(pb); // 16.16 fixed point track height
1548 sc->width = width >> 16;
1549 sc->height = height >> 16;
1551 //transform the display width/height according to the matrix
1552 // skip this if the display matrix is the default identity matrix
1553 // to keep the same scale, use [width height 1<<16]
1554 if (width && height &&
1555 (display_matrix[0][0] != 65536 || display_matrix[0][1] ||
1556 display_matrix[1][0] || display_matrix[1][1] != 65536 ||
1557 display_matrix[2][0] || display_matrix[2][1])) {
1558 for (i = 0; i < 2; i++)
1560 (int64_t) width * display_matrix[0][i] +
1561 (int64_t) height * display_matrix[1][i] +
1562 ((int64_t) display_matrix[2][i] << 16);
1564 //sample aspect ratio is new width/height divided by old width/height
1565 st->sample_aspect_ratio = av_d2q(
1566 ((double) disp_transform[0] * height) /
1567 ((double) disp_transform[1] * width), INT_MAX);
1572 static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1574 MOVFragment *frag = &c->fragment;
1575 MOVTrackExt *trex = NULL;
1576 int flags, track_id, i;
1578 get_byte(pb); /* version */
1579 flags = get_be24(pb);
1581 track_id = get_be32(pb);
1582 if (!track_id || track_id > c->fc->nb_streams)
1584 frag->track_id = track_id;
1585 for (i = 0; i < c->trex_count; i++)
1586 if (c->trex_data[i].track_id == frag->track_id) {
1587 trex = &c->trex_data[i];
1591 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
1595 if (flags & 0x01) frag->base_data_offset = get_be64(pb);
1596 else frag->base_data_offset = frag->moof_offset;
1597 if (flags & 0x02) frag->stsd_id = get_be32(pb);
1598 else frag->stsd_id = trex->stsd_id;
1600 frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
1601 frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
1602 frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
1603 dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
1607 static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1611 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
1613 trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
1615 return AVERROR(ENOMEM);
1616 c->trex_data = trex;
1617 trex = &c->trex_data[c->trex_count++];
1618 get_byte(pb); /* version */
1619 get_be24(pb); /* flags */
1620 trex->track_id = get_be32(pb);
1621 trex->stsd_id = get_be32(pb);
1622 trex->duration = get_be32(pb);
1623 trex->size = get_be32(pb);
1624 trex->flags = get_be32(pb);
1628 static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1630 MOVFragment *frag = &c->fragment;
1632 MOVStreamContext *sc;
1635 int data_offset = 0;
1636 unsigned entries, first_sample_flags = frag->flags;
1637 int flags, distance, i;
1639 if (!frag->track_id || frag->track_id > c->fc->nb_streams)
1641 st = c->fc->streams[frag->track_id-1];
1643 if (sc->pseudo_stream_id+1 != frag->stsd_id)
1645 get_byte(pb); /* version */
1646 flags = get_be24(pb);
1647 entries = get_be32(pb);
1648 dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
1649 if (flags & 0x001) data_offset = get_be32(pb);
1650 if (flags & 0x004) first_sample_flags = get_be32(pb);
1651 if (flags & 0x800) {
1653 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
1655 ctts_data = av_realloc(sc->ctts_data,
1656 (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
1658 return AVERROR(ENOMEM);
1659 sc->ctts_data = ctts_data;
1662 offset = frag->base_data_offset + data_offset;
1664 dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
1665 for (i = 0; i < entries; i++) {
1666 unsigned sample_size = frag->size;
1667 int sample_flags = i ? frag->flags : first_sample_flags;
1668 unsigned sample_duration = frag->duration;
1671 if (flags & 0x100) sample_duration = get_be32(pb);
1672 if (flags & 0x200) sample_size = get_be32(pb);
1673 if (flags & 0x400) sample_flags = get_be32(pb);
1674 if (flags & 0x800) {
1675 sc->ctts_data[sc->ctts_count].count = 1;
1676 sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
1679 if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
1680 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
1682 av_add_index_entry(st, offset, dts, sample_size, distance,
1683 keyframe ? AVINDEX_KEYFRAME : 0);
1684 dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1685 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
1686 offset, dts, sample_size, distance, keyframe);
1688 assert(sample_duration % sc->time_rate == 0);
1689 dts += sample_duration / sc->time_rate;
1690 offset += sample_size;
1692 frag->moof_offset = offset;
1693 sc->sample_count = st->nb_index_entries;
1698 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1699 /* like the files created with Adobe Premiere 5.0, for samples see */
1700 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1701 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1706 return 0; /* continue */
1707 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1708 url_fskip(pb, atom.size - 4);
1711 atom.type = get_le32(pb);
1714 if (atom.type != MKTAG('m','d','a','t')) {
1715 url_fskip(pb, atom.size);
1718 err = mov_read_mdat(c, pb, atom);
1722 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1727 uint8_t *moov_data; /* uncompressed data */
1728 long cmov_len, moov_len;
1731 get_be32(pb); /* dcom atom */
1732 if (get_le32(pb) != MKTAG('d','c','o','m'))
1734 if (get_le32(pb) != MKTAG('z','l','i','b')) {
1735 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1738 get_be32(pb); /* cmvd atom */
1739 if (get_le32(pb) != MKTAG('c','m','v','d'))
1741 moov_len = get_be32(pb); /* uncompressed size */
1742 cmov_len = atom.size - 6 * 4;
1744 cmov_data = av_malloc(cmov_len);
1746 return AVERROR(ENOMEM);
1747 moov_data = av_malloc(moov_len);
1750 return AVERROR(ENOMEM);
1752 get_buffer(pb, cmov_data, cmov_len);
1753 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1754 goto free_and_return;
1755 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1756 goto free_and_return;
1757 atom.type = MKTAG('m','o','o','v');
1759 atom.size = moov_len;
1761 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1763 ret = mov_read_default(c, &ctx, atom);
1769 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1774 /* edit list atom */
1775 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
1777 MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
1780 get_byte(pb); /* version */
1781 get_be24(pb); /* flags */
1782 edit_count = get_be32(pb); /* entries */
1784 for(i=0; i<edit_count; i++){
1786 get_be32(pb); /* Track duration */
1787 time = get_be32(pb); /* Media time */
1788 get_be32(pb); /* Media rate */
1789 if (i == 0 && time != -1) {
1790 sc->time_offset = time;
1791 sc->time_rate = av_gcd(sc->time_rate, time);
1796 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
1797 "a/v desync might occur, patch welcome\n");
1799 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
1803 static const MOVParseTableEntry mov_default_parse_table[] = {
1804 { MKTAG('a','v','s','s'), mov_read_extradata },
1805 { MKTAG('c','o','6','4'), mov_read_stco },
1806 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
1807 { MKTAG('d','i','n','f'), mov_read_default },
1808 { MKTAG('d','r','e','f'), mov_read_dref },
1809 { MKTAG('e','d','t','s'), mov_read_default },
1810 { MKTAG('e','l','s','t'), mov_read_elst },
1811 { MKTAG('e','n','d','a'), mov_read_enda },
1812 { MKTAG('f','i','e','l'), mov_read_extradata },
1813 { MKTAG('f','t','y','p'), mov_read_ftyp },
1814 { MKTAG('g','l','b','l'), mov_read_glbl },
1815 { MKTAG('h','d','l','r'), mov_read_hdlr },
1816 { MKTAG('i','l','s','t'), mov_read_ilst },
1817 { MKTAG('j','p','2','h'), mov_read_extradata },
1818 { MKTAG('m','d','a','t'), mov_read_mdat },
1819 { MKTAG('m','d','h','d'), mov_read_mdhd },
1820 { MKTAG('m','d','i','a'), mov_read_default },
1821 { MKTAG('m','e','t','a'), mov_read_meta },
1822 { MKTAG('m','i','n','f'), mov_read_default },
1823 { MKTAG('m','o','o','f'), mov_read_moof },
1824 { MKTAG('m','o','o','v'), mov_read_moov },
1825 { MKTAG('m','v','e','x'), mov_read_default },
1826 { MKTAG('m','v','h','d'), mov_read_mvhd },
1827 { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
1828 { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
1829 { MKTAG('a','v','c','C'), mov_read_glbl },
1830 { MKTAG('p','a','s','p'), mov_read_pasp },
1831 { MKTAG('s','t','b','l'), mov_read_default },
1832 { MKTAG('s','t','c','o'), mov_read_stco },
1833 { MKTAG('s','t','s','c'), mov_read_stsc },
1834 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
1835 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
1836 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
1837 { MKTAG('s','t','t','s'), mov_read_stts },
1838 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
1839 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
1840 { MKTAG('t','r','a','k'), mov_read_trak },
1841 { MKTAG('t','r','a','f'), mov_read_default },
1842 { MKTAG('t','r','e','x'), mov_read_trex },
1843 { MKTAG('t','r','k','n'), mov_read_trkn },
1844 { MKTAG('t','r','u','n'), mov_read_trun },
1845 { MKTAG('u','d','t','a'), mov_read_default },
1846 { MKTAG('w','a','v','e'), mov_read_wave },
1847 { MKTAG('e','s','d','s'), mov_read_esds },
1848 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
1849 { MKTAG('c','m','o','v'), mov_read_cmov },
1850 { MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
1851 { MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
1852 { MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
1853 { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
1854 { MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
1855 { MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
1856 { MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
1857 { MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
1858 { MKTAG(0xa9,'a','u','t'), mov_read_udta_string },
1859 { MKTAG(0xa9,'d','a','y'), mov_read_udta_string },
1860 { MKTAG(0xa9,'g','e','n'), mov_read_udta_string },
1861 { MKTAG(0xa9,'e','n','c'), mov_read_udta_string },
1862 { MKTAG(0xa9,'t','o','o'), mov_read_udta_string },
1866 static int mov_probe(AVProbeData *p)
1868 unsigned int offset;
1872 /* check file header */
1875 /* ignore invalid offset */
1876 if ((offset + 8) > (unsigned int)p->buf_size)
1878 tag = AV_RL32(p->buf + offset + 4);
1880 /* check for obvious tags */
1881 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
1882 case MKTAG('m','o','o','v'):
1883 case MKTAG('m','d','a','t'):
1884 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
1885 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
1886 case MKTAG('f','t','y','p'):
1887 return AVPROBE_SCORE_MAX;
1888 /* those are more common words, so rate then a bit less */
1889 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
1890 case MKTAG('w','i','d','e'):
1891 case MKTAG('f','r','e','e'):
1892 case MKTAG('j','u','n','k'):
1893 case MKTAG('p','i','c','t'):
1894 return AVPROBE_SCORE_MAX - 5;
1895 case MKTAG(0x82,0x82,0x7f,0x7d):
1896 case MKTAG('s','k','i','p'):
1897 case MKTAG('u','u','i','d'):
1898 case MKTAG('p','r','f','l'):
1899 offset = AV_RB32(p->buf+offset) + offset;
1900 /* if we only find those cause probedata is too small at least rate them */
1901 score = AVPROBE_SCORE_MAX - 50;
1904 /* unrecognized tag */
1911 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1913 MOVContext *mov = s->priv_data;
1914 ByteIOContext *pb = s->pb;
1916 MOVAtom atom = { 0, 0, 0 };
1919 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1920 if(!url_is_streamed(pb))
1921 atom.size = url_fsize(pb);
1923 atom.size = INT64_MAX;
1925 /* check MOV header */
1926 if ((err = mov_read_default(mov, pb, atom)) < 0) {
1927 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
1930 if (!mov->found_moov) {
1931 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
1934 dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
1939 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1941 MOVContext *mov = s->priv_data;
1942 MOVStreamContext *sc = 0;
1943 AVIndexEntry *sample = 0;
1944 int64_t best_dts = INT64_MAX;
1947 for (i = 0; i < s->nb_streams; i++) {
1948 AVStream *st = s->streams[i];
1949 MOVStreamContext *msc = st->priv_data;
1950 if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
1951 AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
1952 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
1953 AV_TIME_BASE, msc->time_scale);
1954 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
1955 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
1956 (!url_is_streamed(s->pb) &&
1957 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
1958 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
1959 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
1960 sample = current_sample;
1967 mov->found_mdat = 0;
1968 if (!url_is_streamed(s->pb) ||
1969 mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
1972 dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
1975 /* must be done just before reading, to avoid infinite loop on sample */
1976 sc->current_sample++;
1977 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
1978 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
1979 sc->ffindex, sample->pos);
1982 av_get_packet(sc->pb, pkt, sample->size);
1983 #if CONFIG_DV_DEMUXER
1984 if (mov->dv_demux && sc->dv_audio_container) {
1985 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1988 if (dv_get_packet(mov->dv_demux, pkt) < 0)
1992 pkt->stream_index = sc->ffindex;
1993 pkt->dts = sample->timestamp;
1994 if (sc->ctts_data) {
1995 assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0);
1996 pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
1997 /* update ctts context */
1999 if (sc->ctts_index < sc->ctts_count &&
2000 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
2002 sc->ctts_sample = 0;
2005 pkt->dts = AV_NOPTS_VALUE;
2007 AVStream *st = s->streams[sc->ffindex];
2008 int64_t next_dts = (sc->current_sample < sc->sample_count) ?
2009 st->index_entries[sc->current_sample].timestamp : st->duration;
2010 pkt->duration = next_dts - pkt->dts;
2011 pkt->pts = pkt->dts;
2013 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
2014 pkt->pos = sample->pos;
2015 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
2016 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
2020 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
2022 MOVStreamContext *sc = st->priv_data;
2023 int sample, time_sample;
2026 sample = av_index_search_timestamp(st, timestamp, flags);
2027 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
2028 if (sample < 0) /* not sure what to do */
2030 sc->current_sample = sample;
2031 dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
2032 /* adjust ctts index */
2033 if (sc->ctts_data) {
2035 for (i = 0; i < sc->ctts_count; i++) {
2036 int next = time_sample + sc->ctts_data[i].count;
2037 if (next > sc->current_sample) {
2039 sc->ctts_sample = sc->current_sample - time_sample;
2048 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
2051 int64_t seek_timestamp, timestamp;
2055 if (stream_index >= s->nb_streams)
2057 if (sample_time < 0)
2060 st = s->streams[stream_index];
2061 sample = mov_seek_stream(st, sample_time, flags);
2065 /* adjust seek timestamp to found sample timestamp */
2066 seek_timestamp = st->index_entries[sample].timestamp;
2068 for (i = 0; i < s->nb_streams; i++) {
2070 if (stream_index == i || st->discard == AVDISCARD_ALL)
2073 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
2074 mov_seek_stream(st, timestamp, flags);
2079 static int mov_read_close(AVFormatContext *s)
2082 MOVContext *mov = s->priv_data;
2083 for(i=0; i<s->nb_streams; i++) {
2084 MOVStreamContext *sc = s->streams[i]->priv_data;
2085 av_freep(&sc->ctts_data);
2086 for (j=0; j<sc->drefs_count; j++)
2087 av_freep(&sc->drefs[j].path);
2088 av_freep(&sc->drefs);
2089 if (sc->pb && sc->pb != s->pb)
2093 for(i=0; i<mov->dv_fctx->nb_streams; i++){
2094 av_freep(&mov->dv_fctx->streams[i]->codec);
2095 av_freep(&mov->dv_fctx->streams[i]);
2097 av_freep(&mov->dv_fctx);
2098 av_freep(&mov->dv_demux);
2100 av_freep(&mov->trex_data);
2104 AVInputFormat mov_demuxer = {
2105 "mov,mp4,m4a,3gp,3g2,mj2",
2106 NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),