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
36 * First version by Francois Revol revol@free.fr
37 * Seek function by Gael Chardon gael.dev@4now.net
39 * Features and limitations:
40 * - reads most of the QT files I have (at least the structure),
41 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
42 * FIXED, Francois Revol, 07/17/2002
43 * - ffmpeg has nearly none of the usual QuickTime codecs,
44 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
45 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
46 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
47 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
48 * http://mpeg.telecomitalialab.com/faq.htm
49 * - the code is quite ugly... maybe I won't do it recursive next time :-)
50 * - seek is not supported with files that contain edit list
52 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
53 * when coding this :) (it's a writer anyway)
55 * Reference documents:
56 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
58 * http://developer.apple.com/documentation/QuickTime/QTFF/
59 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
60 * QuickTime is a trademark of Apple (AFAIK :))
63 #include "qtpalette.h"
69 /* the QuickTime file format is quite convoluted...
70 * it has lots of index tables, each indexing something in another one...
71 * Here we just use what is needed to read the chunks
74 typedef struct MOV_sample_to_chunk_tbl {
78 } MOV_sample_to_chunk_tbl;
83 int64_t size; /* total size (excluding the size and type fields) */
86 typedef struct MOV_mdat_atom_s {
91 struct MOVParseTableEntry;
93 typedef struct MOVStreamContext {
94 int ffindex; /* the ffmpeg stream id */
96 unsigned int chunk_count;
97 int64_t *chunk_offsets;
98 unsigned int stts_count;
99 Time2Sample *stts_data;
100 unsigned int ctts_count;
101 Time2Sample *ctts_data;
102 unsigned int edit_count; /* number of 'edit' (elst atom) */
103 unsigned int sample_to_chunk_sz;
104 MOV_sample_to_chunk_tbl *sample_to_chunk;
105 int sample_to_ctime_index;
106 int sample_to_ctime_sample;
107 unsigned int sample_size;
108 unsigned int sample_count;
110 unsigned int keyframe_count;
115 unsigned int bytes_per_frame;
116 unsigned int samples_per_frame;
117 int dv_audio_container;
120 typedef struct MOVContext {
123 int64_t duration; /* duration of the longest track */
124 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
125 int found_mdat; /* we suppose we have enough data to read the file */
128 MOVStreamContext *streams[MAX_STREAMS];
130 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
131 /* NOTE: for recursion save to/ restore from local variable! */
133 AVPaletteControl palette_control;
134 MOV_mdat_atom_t *mdat_list;
136 DVDemuxContext *dv_demux;
137 AVFormatContext *dv_fctx;
138 int isom; /* 1 if file is ISO Media (mp4/3gp) */
142 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
144 /* those functions parse an atom */
146 1: found what I wanted, exit
147 0: continue to parse next atom
148 -1: error occured, exit
150 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
152 /* links atom IDs to parse functions */
153 typedef struct MOVParseTableEntry {
155 mov_parse_function func;
156 } MOVParseTableEntry;
158 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
160 int64_t total_size = 0;
165 a.offset = atom.offset;
168 atom.size = INT64_MAX;
169 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
173 a.size = get_be32(pb);
174 a.type = get_le32(pb);
178 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
179 if (a.size == 1) { /* 64 bit extended size */
180 a.size = get_be64(pb) - 8;
185 a.size = atom.size - total_size;
190 if(a.size < 0 || a.size > atom.size - total_size)
193 for (i = 0; c->parse_table[i].type != 0L
194 && c->parse_table[i].type != a.type; i++)
197 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
198 url_fskip(pb, a.size);
200 offset_t start_pos = url_ftell(pb);
202 err = (c->parse_table[i].func)(c, pb, a);
203 left = a.size - url_ftell(pb) + start_pos;
204 if (left > 0) /* skip garbage at atom end */
209 total_size += a.size;
212 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
213 url_fskip(pb, atom.size - total_size);
219 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
221 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
225 get_byte(pb); /* version */
226 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
229 ctype = get_le32(pb);
230 type = get_le32(pb); /* component subtype */
232 dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
233 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
236 if(type == MKTAG('v', 'i', 'd', 'e'))
237 st->codec->codec_type = CODEC_TYPE_VIDEO;
238 else if(type == MKTAG('s', 'o', 'u', 'n'))
239 st->codec->codec_type = CODEC_TYPE_AUDIO;
240 else if(type == MKTAG('m', '1', 'a', ' '))
241 st->codec->codec_id = CODEC_ID_MP2;
242 else if(type == MKTAG('s', 'u', 'b', 'p')) {
243 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
244 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
246 get_be32(pb); /* component manufacture */
247 get_be32(pb); /* component flags */
248 get_be32(pb); /* component flags mask */
251 return 0; /* nothing left to read */
253 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
257 static int mp4_read_descr_len(ByteIOContext *pb)
262 int c = get_byte(pb);
263 len = (len << 7) | (c & 0x7f);
270 static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
274 len = mp4_read_descr_len(pb);
275 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
279 #define MP4ESDescrTag 0x03
280 #define MP4DecConfigDescrTag 0x04
281 #define MP4DecSpecificDescrTag 0x05
283 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
285 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
288 /* Well, broken but suffisant for some MP4 streams */
289 get_be32(pb); /* version + flags */
290 len = mp4_read_descr(c, pb, &tag);
291 if (tag == MP4ESDescrTag) {
292 get_be16(pb); /* ID */
293 get_byte(pb); /* priority */
295 get_be16(pb); /* ID */
297 len = mp4_read_descr(c, pb, &tag);
298 if (tag == MP4DecConfigDescrTag) {
299 int object_type_id = get_byte(pb);
300 get_byte(pb); /* stream type */
301 get_be24(pb); /* buffer size db */
302 get_be32(pb); /* max bitrate */
303 get_be32(pb); /* avg bitrate */
305 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
306 dprintf(c->fc, "esds object type id %d\n", object_type_id);
307 len = mp4_read_descr(c, pb, &tag);
308 if (tag == MP4DecSpecificDescrTag) {
309 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
310 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
311 if (st->codec->extradata) {
312 get_buffer(pb, st->codec->extradata, len);
313 st->codec->extradata_size = len;
315 if ((*st->codec->extradata >> 3) == 29) {
316 st->codec->codec_id = CODEC_ID_MP3ON4;
324 /* this atom contains actual media data */
325 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
327 if(atom.size == 0) /* wrong one (MP4) */
329 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
330 c->mdat_list[c->mdat_count].offset = atom.offset;
331 c->mdat_list[c->mdat_count].size = atom.size;
334 c->mdat_offset = atom.offset;
336 return 1; /* found both, just go */
337 url_fskip(pb, atom.size);
338 return 0; /* now go for moov */
341 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
343 uint32_t type = get_le32(pb);
345 if (type != MKTAG('q','t',' ',' '))
347 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
348 get_be32(pb); /* minor version */
349 url_fskip(pb, atom.size - 8);
353 /* this atom should contain all header atoms */
354 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
358 err = mov_read_default(c, pb, atom);
359 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
360 /* so we don't parse the whole file if over a network */
363 return 1; /* found both, just go */
364 return 0; /* now go for mdat */
368 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
370 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
371 MOVStreamContext *sc = st->priv_data;
372 int version = get_byte(pb);
376 return 1; /* unsupported */
378 get_byte(pb); get_byte(pb);
379 get_byte(pb); /* flags */
385 get_be32(pb); /* creation time */
386 get_be32(pb); /* modification time */
389 sc->time_scale = get_be32(pb);
390 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
392 lang = get_be16(pb); /* language */
393 ff_mov_lang_to_iso639(lang, st->language);
394 get_be16(pb); /* quality */
399 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
401 int version = get_byte(pb); /* version */
402 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
408 get_be32(pb); /* creation time */
409 get_be32(pb); /* modification time */
411 c->time_scale = get_be32(pb); /* time scale */
413 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
415 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
416 get_be32(pb); /* preferred scale */
418 get_be16(pb); /* preferred volume */
420 url_fskip(pb, 10); /* reserved */
422 url_fskip(pb, 36); /* display matrix */
424 get_be32(pb); /* preview time */
425 get_be32(pb); /* preview duration */
426 get_be32(pb); /* poster time */
427 get_be32(pb); /* selection time */
428 get_be32(pb); /* selection duration */
429 get_be32(pb); /* current time */
430 get_be32(pb); /* next track ID */
435 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
437 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
439 if((uint64_t)atom.size > (1<<30))
442 // currently SVQ3 decoder expect full STSD header - so let's fake it
443 // this should be fixed and just SMI header should be passed
444 av_free(st->codec->extradata);
445 st->codec->extradata_size = 0x5a + atom.size;
446 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
448 if (st->codec->extradata) {
449 memcpy(st->codec->extradata, "SVQ3", 4); // fake
450 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
451 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
453 url_fskip(pb, atom.size);
458 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
460 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
461 int little_endian = get_be16(pb);
464 switch (st->codec->codec_id) {
465 case CODEC_ID_PCM_S24BE:
466 st->codec->codec_id = CODEC_ID_PCM_S24LE;
468 case CODEC_ID_PCM_S32BE:
469 st->codec->codec_id = CODEC_ID_PCM_S32LE;
478 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
479 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
481 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
482 if((uint64_t)atom.size > (1<<30))
484 av_free(st->codec->extradata);
485 st->codec->extradata_size = atom.size + 8;
486 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
487 if (st->codec->extradata) {
488 AV_WL32(st->codec->extradata + 4, atom.type);
489 get_buffer(pb, st->codec->extradata + 8, atom.size);
491 url_fskip(pb, atom.size);
495 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
497 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
499 if((uint64_t)atom.size > (1<<30))
502 if (st->codec->codec_id == CODEC_ID_QDM2) {
503 // pass all frma atom to codec, needed at least for QDM2
504 av_free(st->codec->extradata);
505 st->codec->extradata_size = atom.size;
506 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
508 if (st->codec->extradata) {
509 get_buffer(pb, st->codec->extradata, atom.size);
511 url_fskip(pb, atom.size);
512 } else if (atom.size > 8) { /* to read frma, esds atoms */
513 mov_read_default(c, pb, atom);
515 url_fskip(pb, atom.size);
519 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
521 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
523 if((uint64_t)atom.size > (1<<30))
526 av_free(st->codec->extradata);
528 st->codec->extradata_size = atom.size;
529 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
531 if (st->codec->extradata) {
532 get_buffer(pb, st->codec->extradata, atom.size);
534 url_fskip(pb, atom.size);
539 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
541 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
542 MOVStreamContext *sc = st->priv_data;
543 unsigned int i, entries;
545 get_byte(pb); /* version */
546 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
548 entries = get_be32(pb);
550 if(entries >= UINT_MAX/sizeof(int64_t))
553 sc->chunk_count = entries;
554 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
555 if (!sc->chunk_offsets)
557 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
558 for(i=0; i<entries; i++) {
559 sc->chunk_offsets[i] = get_be32(pb);
561 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
562 for(i=0; i<entries; i++) {
563 sc->chunk_offsets[i] = get_be64(pb);
571 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
573 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
574 MOVStreamContext *sc = st->priv_data;
575 int entries, frames_per_sample;
577 uint8_t codec_name[32];
579 /* for palette traversal */
587 unsigned char *color_table;
589 unsigned char r, g, b;
591 get_byte(pb); /* version */
592 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
594 entries = get_be32(pb);
596 while(entries--) { //Parsing Sample description table
598 MOV_atom_t a = { 0, 0, 0 };
599 offset_t start_pos = url_ftell(pb);
600 int size = get_be32(pb); /* size */
601 format = get_le32(pb); /* data format */
603 get_be32(pb); /* reserved */
604 get_be16(pb); /* reserved */
605 get_be16(pb); /* index */
607 if (st->codec->codec_tag) {
608 /* multiple fourcc, just skip for now */
609 url_fskip(pb, size - (url_ftell(pb) - start_pos));
613 st->codec->codec_tag = format;
614 id = codec_get_id(codec_movaudio_tags, format);
615 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
616 st->codec->codec_type = CODEC_TYPE_AUDIO;
617 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
618 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
619 id = codec_get_id(codec_movvideo_tags, format);
621 id = codec_get_id(codec_bmp_tags, format);
623 st->codec->codec_type = CODEC_TYPE_VIDEO;
626 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
628 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
629 st->codec->codec_type);
631 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
632 st->codec->codec_id = id;
633 get_be16(pb); /* version */
634 get_be16(pb); /* revision level */
635 get_be32(pb); /* vendor */
636 get_be32(pb); /* temporal quality */
637 get_be32(pb); /* spacial quality */
639 st->codec->width = get_be16(pb); /* width */
640 st->codec->height = get_be16(pb); /* height */
642 get_be32(pb); /* horiz resolution */
643 get_be32(pb); /* vert resolution */
644 get_be32(pb); /* data size, always 0 */
645 frames_per_sample = get_be16(pb); /* frames per samples */
647 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
649 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
650 if (codec_name[0] <= 31) {
651 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
652 st->codec->codec_name[codec_name[0]] = 0;
655 st->codec->bits_per_sample = get_be16(pb); /* depth */
656 st->codec->color_table_id = get_be16(pb); /* colortable id */
658 /* figure out the palette situation */
659 color_depth = st->codec->bits_per_sample & 0x1F;
660 color_greyscale = st->codec->bits_per_sample & 0x20;
662 /* if the depth is 2, 4, or 8 bpp, file is palettized */
663 if ((color_depth == 2) || (color_depth == 4) ||
664 (color_depth == 8)) {
666 if (color_greyscale) {
668 /* compute the greyscale palette */
669 color_count = 1 << color_depth;
671 color_dec = 256 / (color_count - 1);
672 for (j = 0; j < color_count; j++) {
673 r = g = b = color_index;
674 c->palette_control.palette[j] =
675 (r << 16) | (g << 8) | (b);
676 color_index -= color_dec;
681 } else if (st->codec->color_table_id & 0x08) {
683 /* if flag bit 3 is set, use the default palette */
684 color_count = 1 << color_depth;
685 if (color_depth == 2)
686 color_table = ff_qt_default_palette_4;
687 else if (color_depth == 4)
688 color_table = ff_qt_default_palette_16;
690 color_table = ff_qt_default_palette_256;
692 for (j = 0; j < color_count; j++) {
693 r = color_table[j * 4 + 0];
694 g = color_table[j * 4 + 1];
695 b = color_table[j * 4 + 2];
696 c->palette_control.palette[j] =
697 (r << 16) | (g << 8) | (b);
702 /* load the palette from the file */
703 color_start = get_be32(pb);
704 color_count = get_be16(pb);
705 color_end = get_be16(pb);
706 for (j = color_start; j <= color_end; j++) {
707 /* each R, G, or B component is 16 bits;
708 * only use the top 8 bits; skip alpha bytes
718 c->palette_control.palette[j] =
719 (r << 16) | (g << 8) | (b);
723 st->codec->palctrl = &c->palette_control;
724 st->codec->palctrl->palette_changed = 1;
726 st->codec->palctrl = NULL;
727 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
729 uint16_t version = get_be16(pb);
731 st->codec->codec_id = id;
732 get_be16(pb); /* revision level */
733 get_be32(pb); /* vendor */
735 st->codec->channels = get_be16(pb); /* channel count */
736 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
737 st->codec->bits_per_sample = get_be16(pb); /* sample size */
738 /* do we need to force to 16 for AMR ? */
740 /* handle specific s8 codec */
741 get_be16(pb); /* compression id = 0*/
742 get_be16(pb); /* packet size = 0 */
744 st->codec->sample_rate = ((get_be32(pb) >> 16));
746 switch (st->codec->codec_id) {
747 case CODEC_ID_PCM_S8:
748 case CODEC_ID_PCM_U8:
749 if (st->codec->bits_per_sample == 16)
750 st->codec->codec_id = CODEC_ID_PCM_S16BE;
752 case CODEC_ID_PCM_S16LE:
753 case CODEC_ID_PCM_S16BE:
754 if (st->codec->bits_per_sample == 8)
755 st->codec->codec_id = CODEC_ID_PCM_S8;
756 else if (st->codec->bits_per_sample == 24)
757 st->codec->codec_id = CODEC_ID_PCM_S24BE;
763 //Read QT version 1 fields. In version 0 theese dont exist
764 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
767 sc->samples_per_frame = get_be32(pb);
768 get_be32(pb); /* bytes per packet */
769 sc->bytes_per_frame = get_be32(pb);
770 get_be32(pb); /* bytes per sample */
771 } else if(version==2) {
772 get_be32(pb); /* sizeof struct only */
773 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
774 st->codec->channels = get_be32(pb);
775 get_be32(pb); /* always 0x7F000000 */
776 get_be32(pb); /* bits per channel if sound is uncompressed */
777 get_be32(pb); /* lcpm format specific flag */
778 get_be32(pb); /* bytes per audio packet if constant */
779 get_be32(pb); /* lpcm frames per audio packet if constant */
783 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
784 if (bits_per_sample) {
785 st->codec->bits_per_sample = bits_per_sample;
786 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
789 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
790 url_fskip(pb, size - (url_ftell(pb) - start_pos));
792 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
793 a.size = size - (url_ftell(pb) - start_pos);
795 mov_read_default(c, pb, a);
797 url_fskip(pb, a.size);
800 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
801 st->codec->sample_rate= sc->time_scale;
804 /* special codec parameters handling */
805 switch (st->codec->codec_id) {
806 #ifdef CONFIG_H261_DECODER
809 #ifdef CONFIG_H263_DECODER
812 #ifdef CONFIG_MPEG4_DECODER
815 st->codec->width= 0; /* let decoder init width/height */
816 st->codec->height= 0;
818 #ifdef CONFIG_LIBFAAD
821 #ifdef CONFIG_VORBIS_DECODER
822 case CODEC_ID_VORBIS:
824 case CODEC_ID_MP3ON4:
825 st->codec->sample_rate= 0; /* let decoder init parameters properly */
827 #ifdef CONFIG_DV_DEMUXER
828 case CODEC_ID_DVAUDIO:
829 c->dv_fctx = av_alloc_format_context();
830 c->dv_demux = dv_init_demux(c->dv_fctx);
832 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
835 sc->dv_audio_container = 1;
836 st->codec->codec_id = CODEC_ID_PCM_S16LE;
839 /* no ifdef since parameters are always those */
840 case CODEC_ID_AMR_WB:
841 st->codec->sample_rate= 16000;
842 st->codec->channels= 1; /* really needed */
844 case CODEC_ID_AMR_NB:
845 st->codec->sample_rate= 8000;
846 st->codec->channels= 1; /* really needed */
850 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
851 st->need_parsing = AVSTREAM_PARSE_FULL;
860 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
862 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
863 MOVStreamContext *sc = st->priv_data;
864 unsigned int i, entries;
866 get_byte(pb); /* version */
867 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
869 entries = get_be32(pb);
871 if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
875 av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
877 sc->sample_to_chunk_sz = entries;
878 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
879 if (!sc->sample_to_chunk)
881 for(i=0; i<entries; i++) {
882 sc->sample_to_chunk[i].first = get_be32(pb);
883 sc->sample_to_chunk[i].count = get_be32(pb);
884 sc->sample_to_chunk[i].id = get_be32(pb);
889 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
891 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
892 MOVStreamContext *sc = st->priv_data;
893 unsigned int i, entries;
895 get_byte(pb); /* version */
896 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
898 entries = get_be32(pb);
900 if(entries >= UINT_MAX / sizeof(int))
903 sc->keyframe_count = entries;
905 av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count);
907 sc->keyframes = av_malloc(entries * sizeof(int));
910 for(i=0; i<entries; i++) {
911 sc->keyframes[i] = get_be32(pb);
913 /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%d\n", sc->keyframes[i]); */
919 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
921 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
922 MOVStreamContext *sc = st->priv_data;
923 unsigned int i, entries, sample_size;
925 get_byte(pb); /* version */
926 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
928 sample_size = get_be32(pb);
929 if (!sc->sample_size) /* do not overwrite value computed in stsd */
930 sc->sample_size = sample_size;
931 entries = get_be32(pb);
932 if(entries >= UINT_MAX / sizeof(int))
935 sc->sample_count = entries;
940 av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
942 sc->sample_sizes = av_malloc(entries * sizeof(int));
943 if (!sc->sample_sizes)
945 for(i=0; i<entries; i++) {
946 sc->sample_sizes[i] = get_be32(pb);
948 av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%d\n", sc->sample_sizes[i]);
954 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
956 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
957 MOVStreamContext *sc = st->priv_data;
958 unsigned int i, entries;
960 int64_t total_sample_count=0;
962 get_byte(pb); /* version */
963 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
964 entries = get_be32(pb);
965 if(entries >= UINT_MAX / sizeof(Time2Sample))
968 sc->stts_count = entries;
969 sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
972 av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
977 for(i=0; i<entries; i++) {
981 sample_count=get_be32(pb);
982 sample_duration = get_be32(pb);
983 sc->stts_data[i].count= sample_count;
984 sc->stts_data[i].duration= sample_duration;
986 sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
988 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
990 duration+=(int64_t)sample_duration*sample_count;
991 total_sample_count+=sample_count;
994 st->nb_frames= total_sample_count;
996 st->duration= duration;
1000 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1002 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1003 MOVStreamContext *sc = st->priv_data;
1004 unsigned int i, entries;
1006 get_byte(pb); /* version */
1007 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1008 entries = get_be32(pb);
1009 if(entries >= UINT_MAX / sizeof(Time2Sample))
1012 sc->ctts_count = entries;
1013 sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
1015 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1017 for(i=0; i<entries; i++) {
1018 int count =get_be32(pb);
1019 int duration =get_be32(pb);
1022 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1024 url_fskip(pb, 8 * (entries - i - 1));
1027 sc->ctts_data[i].count = count;
1028 sc->ctts_data[i].duration= duration;
1030 sc->time_rate= ff_gcd(sc->time_rate, duration);
1035 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1038 MOVStreamContext *sc;
1040 st = av_new_stream(c->fc, c->fc->nb_streams);
1042 sc = av_mallocz(sizeof(MOVStreamContext));
1049 st->codec->codec_type = CODEC_TYPE_DATA;
1050 st->start_time = 0; /* XXX: check */
1051 c->streams[c->fc->nb_streams-1] = sc;
1053 return mov_read_default(c, pb, atom);
1056 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1058 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1059 int version = get_byte(pb);
1061 get_byte(pb); get_byte(pb);
1062 get_byte(pb); /* flags */
1064 MOV_TRACK_ENABLED 0x0001
1065 MOV_TRACK_IN_MOVIE 0x0002
1066 MOV_TRACK_IN_PREVIEW 0x0004
1067 MOV_TRACK_IN_POSTER 0x0008
1074 get_be32(pb); /* creation time */
1075 get_be32(pb); /* modification time */
1077 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1078 get_be32(pb); /* reserved */
1079 st->start_time = 0; /* check */
1080 (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
1081 get_be32(pb); /* reserved */
1082 get_be32(pb); /* reserved */
1084 get_be16(pb); /* layer */
1085 get_be16(pb); /* alternate group */
1086 get_be16(pb); /* volume */
1087 get_be16(pb); /* reserved */
1089 url_fskip(pb, 36); /* display matrix */
1091 /* those are fixed-point */
1092 get_be32(pb); /* track width */
1093 get_be32(pb); /* track height */
1098 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1099 /* like the files created with Adobe Premiere 5.0, for samples see */
1100 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1101 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1106 return 0; /* continue */
1107 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1108 url_fskip(pb, atom.size - 4);
1111 atom.type = get_le32(pb);
1114 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1115 url_fskip(pb, atom.size);
1118 err = mov_read_mdat(c, pb, atom);
1122 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1127 uint8_t *moov_data; /* uncompressed data */
1128 long cmov_len, moov_len;
1131 get_be32(pb); /* dcom atom */
1132 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1134 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1135 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1138 get_be32(pb); /* cmvd atom */
1139 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1141 moov_len = get_be32(pb); /* uncompressed size */
1142 cmov_len = atom.size - 6 * 4;
1144 cmov_data = av_malloc(cmov_len);
1147 moov_data = av_malloc(moov_len);
1152 get_buffer(pb, cmov_data, cmov_len);
1153 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1155 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1157 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1159 atom.size = moov_len;
1161 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1163 ret = mov_read_default(c, &ctx, atom);
1168 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1173 /* edit list atom */
1174 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1178 get_byte(pb); /* version */
1179 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1180 edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
1182 for(i=0; i<edit_count; i++){
1183 get_be32(pb); /* Track duration */
1184 get_be32(pb); /* Media time */
1185 get_be32(pb); /* Media rate */
1187 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1191 static const MOVParseTableEntry mov_default_parse_table[] = {
1193 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1194 { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
1195 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1196 { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
1197 { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
1198 { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
1199 { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
1200 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1201 { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
1202 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1203 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1204 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1205 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1206 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1207 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1208 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1209 { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
1210 { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
1211 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1212 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1213 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1214 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1215 { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
1216 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1217 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1218 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1219 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1220 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
1221 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1222 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1223 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1227 static void mov_free_stream_context(MOVStreamContext *sc)
1230 av_freep(&sc->ctts_data);
1235 /* XXX: is it sufficient ? */
1236 static int mov_probe(AVProbeData *p)
1238 unsigned int offset;
1242 /* check file header */
1245 /* ignore invalid offset */
1246 if ((offset + 8) > (unsigned int)p->buf_size)
1248 tag = AV_RL32(p->buf + offset + 4);
1250 /* check for obvious tags */
1251 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
1252 case MKTAG( 'm', 'o', 'o', 'v' ):
1253 case MKTAG( 'm', 'd', 'a', 't' ):
1254 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1255 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1256 return AVPROBE_SCORE_MAX;
1257 /* those are more common words, so rate then a bit less */
1258 case MKTAG( 'e', 'd', 'i', 'w' ): /* xdcam files have reverted first tags */
1259 case MKTAG( 'w', 'i', 'd', 'e' ):
1260 case MKTAG( 'f', 'r', 'e', 'e' ):
1261 case MKTAG( 'j', 'u', 'n', 'k' ):
1262 case MKTAG( 'p', 'i', 'c', 't' ):
1263 return AVPROBE_SCORE_MAX - 5;
1264 case MKTAG( 'f', 't', 'y', 'p' ):
1265 case MKTAG( 's', 'k', 'i', 'p' ):
1266 case MKTAG( 'u', 'u', 'i', 'd' ):
1267 offset = AV_RB32(p->buf+offset) + offset;
1268 /* if we only find those cause probedata is too small at least rate them */
1269 score = AVPROBE_SCORE_MAX - 50;
1272 /* unrecognized tag */
1279 static void mov_build_index(MOVContext *mov, AVStream *st)
1281 MOVStreamContext *sc = st->priv_data;
1282 offset_t current_offset;
1283 int64_t current_dts = 0;
1284 unsigned int stts_index = 0;
1285 unsigned int stsc_index = 0;
1286 unsigned int stss_index = 0;
1287 unsigned int i, j, k;
1289 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
1290 unsigned int current_sample = 0;
1291 unsigned int stts_sample = 0;
1292 unsigned int keyframe, sample_size;
1293 unsigned int distance = 0;
1295 st->nb_frames = sc->sample_count;
1296 for (i = 0; i < sc->chunk_count; i++) {
1297 current_offset = sc->chunk_offsets[i];
1298 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1300 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1301 if (current_sample >= sc->sample_count) {
1302 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1305 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
1308 if (stss_index + 1 < sc->keyframe_count)
1311 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1312 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
1313 st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
1314 av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
1315 current_offset += sample_size;
1316 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1317 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1321 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1327 } else { /* read whole chunk */
1328 unsigned int chunk_samples, chunk_size, chunk_duration;
1330 for (i = 0; i < sc->chunk_count; i++) {
1331 current_offset = sc->chunk_offsets[i];
1332 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1334 chunk_samples = sc->sample_to_chunk[stsc_index].count;
1335 /* get chunk size */
1336 if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
1337 chunk_size = chunk_samples * sc->sample_size;
1338 else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
1339 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1340 else { /* workaround to find nearest next chunk offset */
1341 chunk_size = INT_MAX;
1342 for (j = 0; j < mov->total_streams; j++) {
1343 MOVStreamContext *msc = mov->streams[j];
1345 for (k = msc->next_chunk; k < msc->chunk_count; k++) {
1346 if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
1347 chunk_size = msc->chunk_offsets[k] - current_offset;
1348 msc->next_chunk = k;
1353 /* check for last chunk */
1354 if (chunk_size == INT_MAX)
1355 for (j = 0; j < mov->mdat_count; j++) {
1356 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
1357 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
1358 if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
1359 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
1361 assert(chunk_size != INT_MAX);
1362 for (j = 0; j < mov->total_streams; j++) {
1363 mov->streams[j]->next_chunk = 0;
1366 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1367 /* get chunk duration */
1369 while (chunk_samples > 0) {
1370 if (chunk_samples < sc->stts_data[stts_index].count) {
1371 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1372 sc->stts_data[stts_index].count -= chunk_samples;
1375 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1376 chunk_samples -= sc->stts_data[stts_index].count;
1377 if (stts_index + 1 < sc->stts_count) {
1382 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
1383 st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
1384 assert(chunk_duration % sc->time_rate == 0);
1385 current_dts += chunk_duration / sc->time_rate;
1389 /* adjust sample count to avindex entries */
1390 sc->sample_count = st->nb_index_entries;
1393 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1395 MOVContext *mov = s->priv_data;
1396 ByteIOContext *pb = &s->pb;
1398 MOV_atom_t atom = { 0, 0, 0 };
1401 mov->parse_table = mov_default_parse_table;
1403 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1404 atom.size = url_fsize(pb);
1406 atom.size = INT64_MAX;
1408 /* check MOV header */
1409 err = mov_read_default(mov, pb, atom);
1410 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1411 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
1412 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1415 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1417 /* some cleanup : make sure we are on the mdat atom */
1418 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1419 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1421 mov->total_streams = s->nb_streams;
1423 for(i=0; i<mov->total_streams; i++) {
1424 MOVStreamContext *sc = mov->streams[i];
1425 AVStream *st = s->streams[i];
1427 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
1428 (!sc->sample_size && !sc->sample_count)){
1429 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
1430 sc->sample_count = 0; //ignore track
1436 sc->time_scale= mov->time_scale;
1437 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1439 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1440 st->codec->frame_size = sc->stts_data[0].duration;
1442 if(st->duration != AV_NOPTS_VALUE){
1443 assert(st->duration % sc->time_rate == 0);
1444 st->duration /= sc->time_rate;
1447 mov_build_index(mov, st);
1450 for(i=0; i<mov->total_streams; i++) {
1451 /* dont need those anymore */
1452 av_freep(&mov->streams[i]->chunk_offsets);
1453 av_freep(&mov->streams[i]->sample_to_chunk);
1454 av_freep(&mov->streams[i]->sample_sizes);
1455 av_freep(&mov->streams[i]->keyframes);
1456 av_freep(&mov->streams[i]->stts_data);
1458 av_freep(&mov->mdat_list);
1462 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1464 MOVContext *mov = s->priv_data;
1465 MOVStreamContext *sc = 0;
1466 AVIndexEntry *sample = 0;
1467 int64_t best_dts = INT64_MAX;
1470 for (i = 0; i < mov->total_streams; i++) {
1471 MOVStreamContext *msc = mov->streams[i];
1473 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1474 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1475 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1477 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
1478 if (dts < best_dts) {
1479 sample = current_sample;
1487 /* must be done just before reading, to avoid infinite loop on sample */
1488 sc->current_sample++;
1489 if (sample->pos >= url_fsize(&s->pb)) {
1490 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1493 #ifdef CONFIG_DV_DEMUXER
1494 if (sc->dv_audio_container) {
1495 dv_get_packet(mov->dv_demux, pkt);
1496 dprintf(s, "dv audio pkt size %d\n", pkt->size);
1499 url_fseek(&s->pb, sample->pos, SEEK_SET);
1500 av_get_packet(&s->pb, pkt, sample->size);
1501 #ifdef CONFIG_DV_DEMUXER
1502 if (mov->dv_demux) {
1503 void *pkt_destruct_func = pkt->destruct;
1504 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1505 pkt->destruct = pkt_destruct_func;
1509 pkt->stream_index = sc->ffindex;
1510 pkt->dts = sample->timestamp;
1511 if (sc->ctts_data) {
1512 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1513 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1514 /* update ctts context */
1515 sc->sample_to_ctime_sample++;
1516 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1517 sc->sample_to_ctime_index++;
1518 sc->sample_to_ctime_sample = 0;
1521 pkt->pts = pkt->dts;
1523 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1524 pkt->pos = sample->pos;
1525 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n", pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
1529 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1531 MOVStreamContext *sc = st->priv_data;
1532 int sample, time_sample;
1535 sample = av_index_search_timestamp(st, timestamp, flags);
1536 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1537 if (sample < 0) /* not sure what to do */
1539 sc->current_sample = sample;
1540 dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
1541 /* adjust ctts index */
1542 if (sc->ctts_data) {
1544 for (i = 0; i < sc->ctts_count; i++) {
1545 time_sample += sc->ctts_data[i].count;
1546 if (time_sample >= sc->current_sample) {
1547 sc->sample_to_ctime_index = i;
1548 sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1556 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1559 int64_t seek_timestamp, timestamp;
1563 if (stream_index >= s->nb_streams)
1566 st = s->streams[stream_index];
1567 sample = mov_seek_stream(st, sample_time, flags);
1571 /* adjust seek timestamp to found sample timestamp */
1572 seek_timestamp = st->index_entries[sample].timestamp;
1574 for (i = 0; i < s->nb_streams; i++) {
1576 if (stream_index == i || st->discard == AVDISCARD_ALL)
1579 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1580 mov_seek_stream(st, timestamp, flags);
1585 static int mov_read_close(AVFormatContext *s)
1588 MOVContext *mov = s->priv_data;
1589 for(i=0; i<mov->total_streams; i++)
1590 mov_free_stream_context(mov->streams[i]);
1592 for(i=0; i<mov->dv_fctx->nb_streams; i++){
1593 av_freep(&mov->dv_fctx->streams[i]->codec);
1594 av_freep(&mov->dv_fctx->streams[i]);
1596 av_freep(&mov->dv_fctx);
1597 av_freep(&mov->dv_demux);
1602 AVInputFormat mov_demuxer = {
1603 "mov,mp4,m4a,3gp,3g2,mj2",
1604 "QuickTime/MPEG4/Motion JPEG 2000 format",