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 {
93 uint32_t flags; // 24bit
97 #define MP4ODescrTag 0x01
98 #define MP4IODescrTag 0x02
99 #define MP4ESDescrTag 0x03
100 #define MP4DecConfigDescrTag 0x04
101 #define MP4DecSpecificDescrTag 0x05
102 #define MP4SLConfigDescrTag 0x06
103 #define MP4ContentIdDescrTag 0x07
104 #define MP4SupplContentIdDescrTag 0x08
105 #define MP4IPIPtrDescrTag 0x09
106 #define MP4IPMPPtrDescrTag 0x0A
107 #define MP4IPMPDescrTag 0x0B
108 #define MP4RegistrationDescrTag 0x0D
109 #define MP4ESIDIncDescrTag 0x0E
110 #define MP4ESIDRefDescrTag 0x0F
111 #define MP4FileIODescrTag 0x10
112 #define MP4FileODescrTag 0x11
113 #define MP4ExtProfileLevelDescrTag 0x13
114 #define MP4ExtDescrTagsStart 0x80
115 #define MP4ExtDescrTagsEnd 0xFE
116 uint8_t stream_priority;
118 /* 0x04 DecConfigDescrTag */
119 uint8_t object_type_id;
121 /* XXX: really streamType is
122 * only 6bit, followed by:
126 uint32_t buffer_size_db; // 24
127 uint32_t max_bitrate;
128 uint32_t avg_bitrate;
130 /* 0x05 DecSpecificDescrTag */
131 uint8_t decoder_cfg_len;
132 uint8_t *decoder_cfg;
134 /* 0x06 SLConfigDescrTag */
135 uint8_t sl_config_len;
139 struct MOVParseTableEntry;
141 typedef struct MOVStreamContext {
142 int ffindex; /* the ffmpeg stream id */
144 unsigned int chunk_count;
145 int64_t *chunk_offsets;
146 unsigned int stts_count;
147 Time2Sample *stts_data;
148 unsigned int ctts_count;
149 Time2Sample *ctts_data;
150 unsigned int edit_count; /* number of 'edit' (elst atom) */
151 unsigned int sample_to_chunk_sz;
152 MOV_sample_to_chunk_tbl *sample_to_chunk;
153 int sample_to_ctime_index;
154 int sample_to_ctime_sample;
155 unsigned int sample_size;
156 unsigned int sample_count;
158 unsigned int keyframe_count;
164 unsigned int bytes_per_frame;
165 unsigned int samples_per_frame;
166 int dv_audio_container;
169 typedef struct MOVContext {
172 int64_t duration; /* duration of the longest track */
173 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
174 int found_mdat; /* we suppose we have enough data to read the file */
177 MOVStreamContext *streams[MAX_STREAMS];
179 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
180 /* NOTE: for recursion save to/ restore from local variable! */
182 AVPaletteControl palette_control;
183 MOV_mdat_atom_t *mdat_list;
185 DVDemuxContext *dv_demux;
186 AVFormatContext *dv_fctx;
187 int isom; /* 1 if file is ISO Media (mp4/3gp) */
191 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
193 /* those functions parse an atom */
195 1: found what I wanted, exit
196 0: continue to parse next atom
197 -1: error occured, exit
199 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
201 /* links atom IDs to parse functions */
202 typedef struct MOVParseTableEntry {
204 mov_parse_function func;
205 } MOVParseTableEntry;
207 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
209 int64_t total_size = 0;
214 a.offset = atom.offset;
217 atom.size = INT64_MAX;
218 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
222 a.size = get_be32(pb);
223 a.type = get_le32(pb);
227 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
228 if (a.size == 1) { /* 64 bit extended size */
229 a.size = get_be64(pb) - 8;
234 a.size = atom.size - total_size;
239 if(a.size < 0 || a.size > atom.size - total_size)
242 for (i = 0; c->parse_table[i].type != 0L
243 && c->parse_table[i].type != a.type; i++)
246 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
247 url_fskip(pb, a.size);
249 offset_t start_pos = url_ftell(pb);
251 err = (c->parse_table[i].func)(c, pb, a);
252 left = a.size - url_ftell(pb) + start_pos;
253 if (left > 0) /* skip garbage at atom end */
258 total_size += a.size;
261 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
262 url_fskip(pb, atom.size - total_size);
268 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
270 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
274 get_byte(pb); /* version */
275 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
278 ctype = get_le32(pb);
279 type = get_le32(pb); /* component subtype */
281 dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
282 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
285 if(type == MKTAG('v', 'i', 'd', 'e'))
286 st->codec->codec_type = CODEC_TYPE_VIDEO;
287 else if(type == MKTAG('s', 'o', 'u', 'n'))
288 st->codec->codec_type = CODEC_TYPE_AUDIO;
289 else if(type == MKTAG('m', '1', 'a', ' '))
290 st->codec->codec_id = CODEC_ID_MP2;
291 else if(type == MKTAG('s', 'u', 'b', 'p')) {
292 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
293 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
295 get_be32(pb); /* component manufacture */
296 get_be32(pb); /* component flags */
297 get_be32(pb); /* component flags mask */
300 return 0; /* nothing left to read */
302 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
306 static int mov_mp4_read_descr_len(ByteIOContext *pb)
311 int c = get_byte(pb);
312 len = (len << 7) | (c & 0x7f);
319 static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
323 len = mov_mp4_read_descr_len(pb);
324 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
328 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
330 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
331 MOVStreamContext *sc = st->priv_data;
334 /* Well, broken but suffisant for some MP4 streams */
335 get_be32(pb); /* version + flags */
336 len = mov_mp4_read_descr(c, pb, &tag);
337 if (tag == MP4ESDescrTag) {
338 get_be16(pb); /* ID */
339 get_byte(pb); /* priority */
341 get_be16(pb); /* ID */
343 len = mov_mp4_read_descr(c, pb, &tag);
344 if (tag == MP4DecConfigDescrTag) {
345 sc->esds.object_type_id = get_byte(pb);
346 sc->esds.stream_type = get_byte(pb);
347 sc->esds.buffer_size_db = get_be24(pb);
348 sc->esds.max_bitrate = get_be32(pb);
349 sc->esds.avg_bitrate = get_be32(pb);
351 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, sc->esds.object_type_id);
352 dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id);
353 len = mov_mp4_read_descr(c, pb, &tag);
354 if (tag == MP4DecSpecificDescrTag) {
355 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
356 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
357 if (st->codec->extradata) {
358 get_buffer(pb, st->codec->extradata, len);
359 st->codec->extradata_size = len;
361 if ((*st->codec->extradata >> 3) == 29) {
362 st->codec->codec_id = CODEC_ID_MP3ON4;
370 /* this atom contains actual media data */
371 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
373 if(atom.size == 0) /* wrong one (MP4) */
375 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
376 c->mdat_list[c->mdat_count].offset = atom.offset;
377 c->mdat_list[c->mdat_count].size = atom.size;
380 c->mdat_offset = atom.offset;
382 return 1; /* found both, just go */
383 url_fskip(pb, atom.size);
384 return 0; /* now go for moov */
387 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
389 uint32_t type = get_le32(pb);
391 if (type != MKTAG('q','t',' ',' '))
393 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
394 get_be32(pb); /* minor version */
395 url_fskip(pb, atom.size - 8);
399 /* this atom should contain all header atoms */
400 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
404 err = mov_read_default(c, pb, atom);
405 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
406 /* so we don't parse the whole file if over a network */
409 return 1; /* found both, just go */
410 return 0; /* now go for mdat */
414 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
416 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
417 MOVStreamContext *sc = st->priv_data;
418 int version = get_byte(pb);
422 return 1; /* unsupported */
424 get_byte(pb); get_byte(pb);
425 get_byte(pb); /* flags */
431 get_be32(pb); /* creation time */
432 get_be32(pb); /* modification time */
435 sc->time_scale = get_be32(pb);
436 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
438 lang = get_be16(pb); /* language */
439 ff_mov_lang_to_iso639(lang, st->language);
440 get_be16(pb); /* quality */
445 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
447 int version = get_byte(pb); /* version */
448 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
454 get_be32(pb); /* creation time */
455 get_be32(pb); /* modification time */
457 c->time_scale = get_be32(pb); /* time scale */
459 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
461 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
462 get_be32(pb); /* preferred scale */
464 get_be16(pb); /* preferred volume */
466 url_fskip(pb, 10); /* reserved */
468 url_fskip(pb, 36); /* display matrix */
470 get_be32(pb); /* preview time */
471 get_be32(pb); /* preview duration */
472 get_be32(pb); /* poster time */
473 get_be32(pb); /* selection time */
474 get_be32(pb); /* selection duration */
475 get_be32(pb); /* current time */
476 get_be32(pb); /* next track ID */
481 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
483 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
485 if((uint64_t)atom.size > (1<<30))
488 // currently SVQ3 decoder expect full STSD header - so let's fake it
489 // this should be fixed and just SMI header should be passed
490 av_free(st->codec->extradata);
491 st->codec->extradata_size = 0x5a + atom.size;
492 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
494 if (st->codec->extradata) {
495 memcpy(st->codec->extradata, "SVQ3", 4); // fake
496 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
497 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
499 url_fskip(pb, atom.size);
504 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
506 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
507 int little_endian = get_be16(pb);
510 switch (st->codec->codec_id) {
511 case CODEC_ID_PCM_S24BE:
512 st->codec->codec_id = CODEC_ID_PCM_S24LE;
514 case CODEC_ID_PCM_S32BE:
515 st->codec->codec_id = CODEC_ID_PCM_S32LE;
524 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
525 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
527 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
528 if((uint64_t)atom.size > (1<<30))
530 av_free(st->codec->extradata);
531 st->codec->extradata_size = atom.size + 8;
532 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
533 if (st->codec->extradata) {
534 AV_WL32(st->codec->extradata + 4, atom.type);
535 get_buffer(pb, st->codec->extradata + 8, atom.size);
537 url_fskip(pb, atom.size);
541 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
543 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
545 if((uint64_t)atom.size > (1<<30))
548 if (st->codec->codec_id == CODEC_ID_QDM2) {
549 // pass all frma atom to codec, needed at least for QDM2
550 av_free(st->codec->extradata);
551 st->codec->extradata_size = atom.size;
552 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
554 if (st->codec->extradata) {
555 get_buffer(pb, st->codec->extradata, atom.size);
557 url_fskip(pb, atom.size);
558 } else if (atom.size > 8) { /* to read frma, esds atoms */
559 mov_read_default(c, pb, atom);
561 url_fskip(pb, atom.size);
565 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
567 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
569 if((uint64_t)atom.size > (1<<30))
572 av_free(st->codec->extradata);
574 st->codec->extradata_size = atom.size;
575 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
577 if (st->codec->extradata) {
578 get_buffer(pb, st->codec->extradata, atom.size);
580 url_fskip(pb, atom.size);
585 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
587 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
588 MOVStreamContext *sc = st->priv_data;
589 unsigned int i, entries;
591 get_byte(pb); /* version */
592 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
594 entries = get_be32(pb);
596 if(entries >= UINT_MAX/sizeof(int64_t))
599 sc->chunk_count = entries;
600 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
601 if (!sc->chunk_offsets)
603 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
604 for(i=0; i<entries; i++) {
605 sc->chunk_offsets[i] = get_be32(pb);
607 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
608 for(i=0; i<entries; i++) {
609 sc->chunk_offsets[i] = get_be64(pb);
617 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
619 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
620 MOVStreamContext *sc = st->priv_data;
621 int entries, frames_per_sample;
623 uint8_t codec_name[32];
625 /* for palette traversal */
633 unsigned char *color_table;
635 unsigned char r, g, b;
637 get_byte(pb); /* version */
638 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
640 entries = get_be32(pb);
642 while(entries--) { //Parsing Sample description table
644 MOV_atom_t a = { 0, 0, 0 };
645 offset_t start_pos = url_ftell(pb);
646 int size = get_be32(pb); /* size */
647 format = get_le32(pb); /* data format */
649 get_be32(pb); /* reserved */
650 get_be16(pb); /* reserved */
651 get_be16(pb); /* index */
653 if (st->codec->codec_tag) {
654 /* multiple fourcc, just skip for now */
655 url_fskip(pb, size - (url_ftell(pb) - start_pos));
659 st->codec->codec_tag = format;
660 id = codec_get_id(codec_movaudio_tags, format);
661 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
662 st->codec->codec_type = CODEC_TYPE_AUDIO;
663 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
664 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
665 id = codec_get_id(codec_movvideo_tags, format);
667 id = codec_get_id(codec_bmp_tags, format);
669 st->codec->codec_type = CODEC_TYPE_VIDEO;
672 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
674 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
675 st->codec->codec_type);
677 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
678 st->codec->codec_id = id;
679 get_be16(pb); /* version */
680 get_be16(pb); /* revision level */
681 get_be32(pb); /* vendor */
682 get_be32(pb); /* temporal quality */
683 get_be32(pb); /* spacial quality */
685 st->codec->width = get_be16(pb); /* width */
686 st->codec->height = get_be16(pb); /* height */
688 get_be32(pb); /* horiz resolution */
689 get_be32(pb); /* vert resolution */
690 get_be32(pb); /* data size, always 0 */
691 frames_per_sample = get_be16(pb); /* frames per samples */
693 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
695 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
696 if (codec_name[0] <= 31) {
697 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
698 st->codec->codec_name[codec_name[0]] = 0;
701 st->codec->bits_per_sample = get_be16(pb); /* depth */
702 st->codec->color_table_id = get_be16(pb); /* colortable id */
704 /* figure out the palette situation */
705 color_depth = st->codec->bits_per_sample & 0x1F;
706 color_greyscale = st->codec->bits_per_sample & 0x20;
708 /* if the depth is 2, 4, or 8 bpp, file is palettized */
709 if ((color_depth == 2) || (color_depth == 4) ||
710 (color_depth == 8)) {
712 if (color_greyscale) {
714 /* compute the greyscale palette */
715 color_count = 1 << color_depth;
717 color_dec = 256 / (color_count - 1);
718 for (j = 0; j < color_count; j++) {
719 r = g = b = color_index;
720 c->palette_control.palette[j] =
721 (r << 16) | (g << 8) | (b);
722 color_index -= color_dec;
727 } else if (st->codec->color_table_id & 0x08) {
729 /* if flag bit 3 is set, use the default palette */
730 color_count = 1 << color_depth;
731 if (color_depth == 2)
732 color_table = ff_qt_default_palette_4;
733 else if (color_depth == 4)
734 color_table = ff_qt_default_palette_16;
736 color_table = ff_qt_default_palette_256;
738 for (j = 0; j < color_count; j++) {
739 r = color_table[j * 4 + 0];
740 g = color_table[j * 4 + 1];
741 b = color_table[j * 4 + 2];
742 c->palette_control.palette[j] =
743 (r << 16) | (g << 8) | (b);
748 /* load the palette from the file */
749 color_start = get_be32(pb);
750 color_count = get_be16(pb);
751 color_end = get_be16(pb);
752 for (j = color_start; j <= color_end; j++) {
753 /* each R, G, or B component is 16 bits;
754 * only use the top 8 bits; skip alpha bytes
764 c->palette_control.palette[j] =
765 (r << 16) | (g << 8) | (b);
769 st->codec->palctrl = &c->palette_control;
770 st->codec->palctrl->palette_changed = 1;
772 st->codec->palctrl = NULL;
773 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
775 uint16_t version = get_be16(pb);
777 st->codec->codec_id = id;
778 get_be16(pb); /* revision level */
779 get_be32(pb); /* vendor */
781 st->codec->channels = get_be16(pb); /* channel count */
782 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
783 st->codec->bits_per_sample = get_be16(pb); /* sample size */
784 /* do we need to force to 16 for AMR ? */
786 /* handle specific s8 codec */
787 get_be16(pb); /* compression id = 0*/
788 get_be16(pb); /* packet size = 0 */
790 st->codec->sample_rate = ((get_be32(pb) >> 16));
792 switch (st->codec->codec_id) {
793 case CODEC_ID_PCM_S8:
794 case CODEC_ID_PCM_U8:
795 if (st->codec->bits_per_sample == 16)
796 st->codec->codec_id = CODEC_ID_PCM_S16BE;
798 case CODEC_ID_PCM_S16LE:
799 case CODEC_ID_PCM_S16BE:
800 if (st->codec->bits_per_sample == 8)
801 st->codec->codec_id = CODEC_ID_PCM_S8;
802 else if (st->codec->bits_per_sample == 24)
803 st->codec->codec_id = CODEC_ID_PCM_S24BE;
809 //Read QT version 1 fields. In version 0 theese dont exist
810 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
813 sc->samples_per_frame = get_be32(pb);
814 get_be32(pb); /* bytes per packet */
815 sc->bytes_per_frame = get_be32(pb);
816 get_be32(pb); /* bytes per sample */
817 } else if(version==2) {
818 get_be32(pb); /* sizeof struct only */
819 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
820 st->codec->channels = get_be32(pb);
821 get_be32(pb); /* always 0x7F000000 */
822 get_be32(pb); /* bits per channel if sound is uncompressed */
823 get_be32(pb); /* lcpm format specific flag */
824 get_be32(pb); /* bytes per audio packet if constant */
825 get_be32(pb); /* lpcm frames per audio packet if constant */
829 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
830 if (bits_per_sample) {
831 st->codec->bits_per_sample = bits_per_sample;
832 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
835 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
836 url_fskip(pb, size - (url_ftell(pb) - start_pos));
838 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
839 a.size = size - (url_ftell(pb) - start_pos);
841 mov_read_default(c, pb, a);
843 url_fskip(pb, a.size);
846 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
847 st->codec->sample_rate= sc->time_scale;
850 /* special codec parameters handling */
851 switch (st->codec->codec_id) {
852 #ifdef CONFIG_H261_DECODER
855 #ifdef CONFIG_H263_DECODER
858 #ifdef CONFIG_MPEG4_DECODER
861 st->codec->width= 0; /* let decoder init width/height */
862 st->codec->height= 0;
864 #ifdef CONFIG_LIBFAAD
867 #ifdef CONFIG_VORBIS_DECODER
868 case CODEC_ID_VORBIS:
870 case CODEC_ID_MP3ON4:
871 st->codec->sample_rate= 0; /* let decoder init parameters properly */
873 #ifdef CONFIG_DV_DEMUXER
874 case CODEC_ID_DVAUDIO:
875 c->dv_fctx = av_alloc_format_context();
876 c->dv_demux = dv_init_demux(c->dv_fctx);
878 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
881 sc->dv_audio_container = 1;
882 st->codec->codec_id = CODEC_ID_PCM_S16LE;
885 /* no ifdef since parameters are always those */
886 case CODEC_ID_AMR_WB:
887 st->codec->sample_rate= 16000;
888 st->codec->channels= 1; /* really needed */
890 case CODEC_ID_AMR_NB:
891 st->codec->sample_rate= 8000;
892 st->codec->channels= 1; /* really needed */
896 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
897 st->need_parsing = AVSTREAM_PARSE_FULL;
906 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
908 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
909 MOVStreamContext *sc = st->priv_data;
910 unsigned int i, entries;
912 get_byte(pb); /* version */
913 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
915 entries = get_be32(pb);
917 if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
921 av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
923 sc->sample_to_chunk_sz = entries;
924 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
925 if (!sc->sample_to_chunk)
927 for(i=0; i<entries; i++) {
928 sc->sample_to_chunk[i].first = get_be32(pb);
929 sc->sample_to_chunk[i].count = get_be32(pb);
930 sc->sample_to_chunk[i].id = get_be32(pb);
935 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
937 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
938 MOVStreamContext *sc = st->priv_data;
939 unsigned int i, entries;
941 get_byte(pb); /* version */
942 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
944 entries = get_be32(pb);
946 if(entries >= UINT_MAX / sizeof(long))
949 sc->keyframe_count = entries;
951 av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count);
953 sc->keyframes = av_malloc(entries * sizeof(long));
956 for(i=0; i<entries; i++) {
957 sc->keyframes[i] = get_be32(pb);
959 /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
965 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
967 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
968 MOVStreamContext *sc = st->priv_data;
969 unsigned int i, entries, sample_size;
971 get_byte(pb); /* version */
972 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
974 sample_size = get_be32(pb);
975 if (!sc->sample_size) /* do not overwrite value computed in stsd */
976 sc->sample_size = sample_size;
977 entries = get_be32(pb);
978 if(entries >= UINT_MAX / sizeof(long))
981 sc->sample_count = entries;
986 av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
988 sc->sample_sizes = av_malloc(entries * sizeof(long));
989 if (!sc->sample_sizes)
991 for(i=0; i<entries; i++) {
992 sc->sample_sizes[i] = get_be32(pb);
994 av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
1000 static int mov_read_stts(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 int64_t total_sample_count=0;
1008 get_byte(pb); /* version */
1009 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1010 entries = get_be32(pb);
1011 if(entries >= UINT_MAX / sizeof(Time2Sample))
1014 sc->stts_count = entries;
1015 sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
1018 av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1023 for(i=0; i<entries; i++) {
1024 int sample_duration;
1027 sample_count=get_be32(pb);
1028 sample_duration = get_be32(pb);
1029 sc->stts_data[i].count= sample_count;
1030 sc->stts_data[i].duration= sample_duration;
1032 sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
1034 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1036 duration+=(int64_t)sample_duration*sample_count;
1037 total_sample_count+=sample_count;
1040 st->nb_frames= total_sample_count;
1042 st->duration= duration;
1046 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1048 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1049 MOVStreamContext *sc = st->priv_data;
1050 unsigned int i, entries;
1052 get_byte(pb); /* version */
1053 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1054 entries = get_be32(pb);
1055 if(entries >= UINT_MAX / sizeof(Time2Sample))
1058 sc->ctts_count = entries;
1059 sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
1061 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1063 for(i=0; i<entries; i++) {
1064 int count =get_be32(pb);
1065 int duration =get_be32(pb);
1068 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1070 url_fskip(pb, 8 * (entries - i - 1));
1073 sc->ctts_data[i].count = count;
1074 sc->ctts_data[i].duration= duration;
1076 sc->time_rate= ff_gcd(sc->time_rate, duration);
1081 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1084 MOVStreamContext *sc;
1086 st = av_new_stream(c->fc, c->fc->nb_streams);
1088 sc = av_mallocz(sizeof(MOVStreamContext));
1095 st->codec->codec_type = CODEC_TYPE_DATA;
1096 st->start_time = 0; /* XXX: check */
1097 c->streams[c->fc->nb_streams-1] = sc;
1099 return mov_read_default(c, pb, atom);
1102 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1104 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1105 int version = get_byte(pb);
1107 get_byte(pb); get_byte(pb);
1108 get_byte(pb); /* flags */
1110 MOV_TRACK_ENABLED 0x0001
1111 MOV_TRACK_IN_MOVIE 0x0002
1112 MOV_TRACK_IN_PREVIEW 0x0004
1113 MOV_TRACK_IN_POSTER 0x0008
1120 get_be32(pb); /* creation time */
1121 get_be32(pb); /* modification time */
1123 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1124 get_be32(pb); /* reserved */
1125 st->start_time = 0; /* check */
1126 (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
1127 get_be32(pb); /* reserved */
1128 get_be32(pb); /* reserved */
1130 get_be16(pb); /* layer */
1131 get_be16(pb); /* alternate group */
1132 get_be16(pb); /* volume */
1133 get_be16(pb); /* reserved */
1135 url_fskip(pb, 36); /* display matrix */
1137 /* those are fixed-point */
1138 get_be32(pb); /* track width */
1139 get_be32(pb); /* track height */
1144 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1145 /* like the files created with Adobe Premiere 5.0, for samples see */
1146 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1147 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1152 return 0; /* continue */
1153 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1154 url_fskip(pb, atom.size - 4);
1157 atom.type = get_le32(pb);
1160 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1161 url_fskip(pb, atom.size);
1164 err = mov_read_mdat(c, pb, atom);
1168 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1173 uint8_t *moov_data; /* uncompressed data */
1174 long cmov_len, moov_len;
1177 get_be32(pb); /* dcom atom */
1178 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1180 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1181 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1184 get_be32(pb); /* cmvd atom */
1185 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1187 moov_len = get_be32(pb); /* uncompressed size */
1188 cmov_len = atom.size - 6 * 4;
1190 cmov_data = av_malloc(cmov_len);
1193 moov_data = av_malloc(moov_len);
1198 get_buffer(pb, cmov_data, cmov_len);
1199 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1201 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1203 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1205 atom.size = moov_len;
1207 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1209 ret = mov_read_default(c, &ctx, atom);
1214 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1219 /* edit list atom */
1220 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1224 get_byte(pb); /* version */
1225 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1226 edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
1228 for(i=0; i<edit_count; i++){
1229 get_be32(pb); /* Track duration */
1230 get_be32(pb); /* Media time */
1231 get_be32(pb); /* Media rate */
1233 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1237 static const MOVParseTableEntry mov_default_parse_table[] = {
1239 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1240 { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
1241 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1242 { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
1243 { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
1244 { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
1245 { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
1246 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1247 { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
1248 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1249 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1250 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1251 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1252 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1253 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1254 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1255 { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
1256 { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
1257 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1258 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1259 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1260 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1261 { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
1262 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1263 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1264 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1265 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1266 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
1267 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1268 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1269 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1273 static void mov_free_stream_context(MOVStreamContext *sc)
1276 av_freep(&sc->ctts_data);
1281 /* XXX: is it sufficient ? */
1282 static int mov_probe(AVProbeData *p)
1284 unsigned int offset;
1288 /* check file header */
1291 /* ignore invalid offset */
1292 if ((offset + 8) > (unsigned int)p->buf_size)
1294 tag = AV_RL32(p->buf + offset + 4);
1296 /* check for obvious tags */
1297 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
1298 case MKTAG( 'm', 'o', 'o', 'v' ):
1299 case MKTAG( 'm', 'd', 'a', 't' ):
1300 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1301 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1302 return AVPROBE_SCORE_MAX;
1303 /* those are more common words, so rate then a bit less */
1304 case MKTAG( 'w', 'i', 'd', 'e' ):
1305 case MKTAG( 'f', 'r', 'e', 'e' ):
1306 case MKTAG( 'j', 'u', 'n', 'k' ):
1307 case MKTAG( 'p', 'i', 'c', 't' ):
1308 return AVPROBE_SCORE_MAX - 5;
1309 case MKTAG( 'f', 't', 'y', 'p' ):
1310 case MKTAG( 's', 'k', 'i', 'p' ):
1311 case MKTAG( 'u', 'u', 'i', 'd' ):
1312 offset = AV_RB32(p->buf+offset) + offset;
1313 /* if we only find those cause probedata is too small at least rate them */
1314 score = AVPROBE_SCORE_MAX - 50;
1317 /* unrecognized tag */
1324 static void mov_build_index(MOVContext *mov, AVStream *st)
1326 MOVStreamContext *sc = st->priv_data;
1327 offset_t current_offset;
1328 int64_t current_dts = 0;
1329 unsigned int stts_index = 0;
1330 unsigned int stsc_index = 0;
1331 unsigned int stss_index = 0;
1332 unsigned int i, j, k;
1334 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
1335 unsigned int current_sample = 0;
1336 unsigned int stts_sample = 0;
1337 unsigned int keyframe, sample_size;
1338 unsigned int distance = 0;
1340 st->nb_frames = sc->sample_count;
1341 for (i = 0; i < sc->chunk_count; i++) {
1342 current_offset = sc->chunk_offsets[i];
1343 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1345 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1346 if (current_sample >= sc->sample_count) {
1347 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1350 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
1353 if (stss_index + 1 < sc->keyframe_count)
1356 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1357 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
1358 st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
1359 av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
1360 current_offset += sample_size;
1361 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1362 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1366 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1372 } else { /* read whole chunk */
1373 unsigned int chunk_samples, chunk_size, chunk_duration;
1375 for (i = 0; i < sc->chunk_count; i++) {
1376 current_offset = sc->chunk_offsets[i];
1377 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1379 chunk_samples = sc->sample_to_chunk[stsc_index].count;
1380 /* get chunk size */
1381 if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
1382 chunk_size = chunk_samples * sc->sample_size;
1383 else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
1384 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1385 else { /* workaround to find nearest next chunk offset */
1386 chunk_size = INT_MAX;
1387 for (j = 0; j < mov->total_streams; j++) {
1388 MOVStreamContext *msc = mov->streams[j];
1390 for (k = msc->next_chunk; k < msc->chunk_count; k++) {
1391 if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
1392 chunk_size = msc->chunk_offsets[k] - current_offset;
1393 msc->next_chunk = k;
1398 /* check for last chunk */
1399 if (chunk_size == INT_MAX)
1400 for (j = 0; j < mov->mdat_count; j++) {
1401 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
1402 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
1403 if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
1404 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
1406 assert(chunk_size != INT_MAX);
1407 for (j = 0; j < mov->total_streams; j++) {
1408 mov->streams[j]->next_chunk = 0;
1411 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1412 /* get chunk duration */
1414 while (chunk_samples > 0) {
1415 if (chunk_samples < sc->stts_data[stts_index].count) {
1416 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1417 sc->stts_data[stts_index].count -= chunk_samples;
1420 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1421 chunk_samples -= sc->stts_data[stts_index].count;
1422 if (stts_index + 1 < sc->stts_count) {
1427 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
1428 st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
1429 assert(chunk_duration % sc->time_rate == 0);
1430 current_dts += chunk_duration / sc->time_rate;
1434 /* adjust sample count to avindex entries */
1435 sc->sample_count = st->nb_index_entries;
1438 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1440 MOVContext *mov = s->priv_data;
1441 ByteIOContext *pb = &s->pb;
1443 MOV_atom_t atom = { 0, 0, 0 };
1446 mov->parse_table = mov_default_parse_table;
1448 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1449 atom.size = url_fsize(pb);
1451 atom.size = INT64_MAX;
1453 /* check MOV header */
1454 err = mov_read_default(mov, pb, atom);
1455 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1456 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
1457 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1460 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1462 /* some cleanup : make sure we are on the mdat atom */
1463 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1464 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1466 mov->total_streams = s->nb_streams;
1468 for(i=0; i<mov->total_streams; i++) {
1469 MOVStreamContext *sc = mov->streams[i];
1470 AVStream *st = s->streams[i];
1472 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
1473 (!sc->sample_size && !sc->sample_count)){
1474 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
1475 sc->sample_count = 0; //ignore track
1481 sc->time_scale= mov->time_scale;
1482 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1484 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1485 st->codec->frame_size = sc->stts_data[0].duration;
1487 if(st->duration != AV_NOPTS_VALUE){
1488 assert(st->duration % sc->time_rate == 0);
1489 st->duration /= sc->time_rate;
1492 mov_build_index(mov, st);
1495 for(i=0; i<mov->total_streams; i++) {
1496 /* dont need those anymore */
1497 av_freep(&mov->streams[i]->chunk_offsets);
1498 av_freep(&mov->streams[i]->sample_to_chunk);
1499 av_freep(&mov->streams[i]->sample_sizes);
1500 av_freep(&mov->streams[i]->keyframes);
1501 av_freep(&mov->streams[i]->stts_data);
1503 av_freep(&mov->mdat_list);
1507 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1509 MOVContext *mov = s->priv_data;
1510 MOVStreamContext *sc = 0;
1511 AVIndexEntry *sample = 0;
1512 int64_t best_dts = INT64_MAX;
1515 for (i = 0; i < mov->total_streams; i++) {
1516 MOVStreamContext *msc = mov->streams[i];
1518 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1519 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1520 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1522 dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
1523 if (dts < best_dts) {
1524 sample = current_sample;
1532 /* must be done just before reading, to avoid infinite loop on sample */
1533 sc->current_sample++;
1534 if (sample->pos >= url_fsize(&s->pb)) {
1535 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1538 #ifdef CONFIG_DV_DEMUXER
1539 if (sc->dv_audio_container) {
1540 dv_get_packet(mov->dv_demux, pkt);
1541 dprintf(s, "dv audio pkt size %d\n", pkt->size);
1544 url_fseek(&s->pb, sample->pos, SEEK_SET);
1545 av_get_packet(&s->pb, pkt, sample->size);
1546 #ifdef CONFIG_DV_DEMUXER
1547 if (mov->dv_demux) {
1548 void *pkt_destruct_func = pkt->destruct;
1549 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1550 pkt->destruct = pkt_destruct_func;
1554 pkt->stream_index = sc->ffindex;
1555 pkt->dts = sample->timestamp;
1556 if (sc->ctts_data) {
1557 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1558 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1559 /* update ctts context */
1560 sc->sample_to_ctime_sample++;
1561 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1562 sc->sample_to_ctime_index++;
1563 sc->sample_to_ctime_sample = 0;
1566 pkt->pts = pkt->dts;
1568 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1569 pkt->pos = sample->pos;
1570 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);
1574 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1576 MOVStreamContext *sc = st->priv_data;
1577 int sample, time_sample;
1580 sample = av_index_search_timestamp(st, timestamp, flags);
1581 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1582 if (sample < 0) /* not sure what to do */
1584 sc->current_sample = sample;
1585 dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample);
1586 /* adjust ctts index */
1587 if (sc->ctts_data) {
1589 for (i = 0; i < sc->ctts_count; i++) {
1590 time_sample += sc->ctts_data[i].count;
1591 if (time_sample >= sc->current_sample) {
1592 sc->sample_to_ctime_index = i;
1593 sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1601 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1604 int64_t seek_timestamp, timestamp;
1608 if (stream_index >= s->nb_streams)
1611 st = s->streams[stream_index];
1612 sample = mov_seek_stream(st, sample_time, flags);
1616 /* adjust seek timestamp to found sample timestamp */
1617 seek_timestamp = st->index_entries[sample].timestamp;
1619 for (i = 0; i < s->nb_streams; i++) {
1621 if (stream_index == i || st->discard == AVDISCARD_ALL)
1624 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1625 mov_seek_stream(st, timestamp, flags);
1630 static int mov_read_close(AVFormatContext *s)
1633 MOVContext *mov = s->priv_data;
1634 for(i=0; i<mov->total_streams; i++)
1635 mov_free_stream_context(mov->streams[i]);
1637 for(i=0; i<mov->dv_fctx->nb_streams; i++){
1638 av_freep(&mov->dv_fctx->streams[i]->codec);
1639 av_freep(&mov->dv_fctx->streams[i]);
1641 av_freep(&mov->dv_fctx);
1642 av_freep(&mov->dv_demux);
1647 AVInputFormat mov_demuxer = {
1648 "mov,mp4,m4a,3gp,3g2,mj2",
1649 "QuickTime/MPEG4/Motion JPEG 2000 format",