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) */
93 typedef struct MOV_mdat_atom_s {
100 uint32_t flags; // 24bit
102 /* 0x03 ESDescrTag */
104 #define MP4ODescrTag 0x01
105 #define MP4IODescrTag 0x02
106 #define MP4ESDescrTag 0x03
107 #define MP4DecConfigDescrTag 0x04
108 #define MP4DecSpecificDescrTag 0x05
109 #define MP4SLConfigDescrTag 0x06
110 #define MP4ContentIdDescrTag 0x07
111 #define MP4SupplContentIdDescrTag 0x08
112 #define MP4IPIPtrDescrTag 0x09
113 #define MP4IPMPPtrDescrTag 0x0A
114 #define MP4IPMPDescrTag 0x0B
115 #define MP4RegistrationDescrTag 0x0D
116 #define MP4ESIDIncDescrTag 0x0E
117 #define MP4ESIDRefDescrTag 0x0F
118 #define MP4FileIODescrTag 0x10
119 #define MP4FileODescrTag 0x11
120 #define MP4ExtProfileLevelDescrTag 0x13
121 #define MP4ExtDescrTagsStart 0x80
122 #define MP4ExtDescrTagsEnd 0xFE
123 uint8_t stream_priority;
125 /* 0x04 DecConfigDescrTag */
126 uint8_t object_type_id;
128 /* XXX: really streamType is
129 * only 6bit, followed by:
133 uint32_t buffer_size_db; // 24
134 uint32_t max_bitrate;
135 uint32_t avg_bitrate;
137 /* 0x05 DecSpecificDescrTag */
138 uint8_t decoder_cfg_len;
139 uint8_t *decoder_cfg;
141 /* 0x06 SLConfigDescrTag */
142 uint8_t sl_config_len;
146 struct MOVParseTableEntry;
148 typedef struct MOVStreamContext {
149 int ffindex; /* the ffmpeg stream id */
151 unsigned int chunk_count;
152 int64_t *chunk_offsets;
153 unsigned int stts_count;
154 Time2Sample *stts_data;
155 unsigned int ctts_count;
156 Time2Sample *ctts_data;
157 unsigned int edit_count; /* number of 'edit' (elst atom) */
158 unsigned int sample_to_chunk_sz;
159 MOV_sample_to_chunk_tbl *sample_to_chunk;
160 int sample_to_ctime_index;
161 int sample_to_ctime_sample;
162 unsigned int sample_size;
163 unsigned int sample_count;
165 unsigned int keyframe_count;
171 unsigned int bytes_per_frame;
172 unsigned int samples_per_frame;
173 int dv_audio_container;
176 typedef struct MOVContext {
179 int64_t duration; /* duration of the longest track */
180 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
181 int found_mdat; /* we suppose we have enough data to read the file */
184 MOVStreamContext *streams[MAX_STREAMS];
187 MOV_ctab_t **ctab; /* color tables */
188 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
189 /* NOTE: for recursion save to/ restore from local variable! */
191 AVPaletteControl palette_control;
192 MOV_mdat_atom_t *mdat_list;
194 DVDemuxContext *dv_demux;
195 AVFormatContext *dv_fctx;
196 int isom; /* 1 if file is ISO Media (mp4/3gp) */
200 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
202 /* those functions parse an atom */
204 1: found what I wanted, exit
205 0: continue to parse next atom
206 -1: error occured, exit
208 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
210 /* links atom IDs to parse functions */
211 typedef struct MOVParseTableEntry {
213 mov_parse_function func;
214 } MOVParseTableEntry;
216 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
218 int64_t total_size = 0;
223 a.offset = atom.offset;
226 atom.size = 0x7fffffffffffffffLL;
227 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
231 a.size = get_be32(pb);
232 a.type = get_le32(pb);
236 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
237 if (a.size == 1) { /* 64 bit extended size */
238 a.size = get_be64(pb) - 8;
243 a.size = atom.size - total_size;
247 for (i = 0; c->parse_table[i].type != 0L
248 && c->parse_table[i].type != a.type; i++)
256 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
257 url_fskip(pb, a.size);
259 offset_t start_pos = url_ftell(pb);
261 err = (c->parse_table[i].func)(c, pb, a);
262 left = a.size - url_ftell(pb) + start_pos;
263 if (left > 0) /* skip garbage at atom end */
268 total_size += a.size;
271 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
272 url_fskip(pb, atom.size - total_size);
278 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
281 url_fskip(pb, atom.size); // for now
283 VERY VERY BROKEN, NEVER execute this, needs rewrite
286 c->ctab = av_realloc(c->ctab, ++c->ctab_size);
287 t = c->ctab[c->ctab_size];
288 t->seed = get_be32(pb);
289 t->flags = get_be16(pb);
290 t->size = get_be16(pb) + 1;
291 len = 2 * t->size * 4;
293 t->clrs = av_malloc(len); // 16bit A R G B
295 get_buffer(pb, t->clrs, len);
302 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
304 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
308 get_byte(pb); /* version */
309 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
312 ctype = get_le32(pb);
313 type = get_le32(pb); /* component subtype */
315 dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
316 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
319 if(type == MKTAG('v', 'i', 'd', 'e'))
320 st->codec->codec_type = CODEC_TYPE_VIDEO;
321 else if(type == MKTAG('s', 'o', 'u', 'n'))
322 st->codec->codec_type = CODEC_TYPE_AUDIO;
323 else if(type == MKTAG('m', '1', 'a', ' '))
324 st->codec->codec_id = CODEC_ID_MP2;
325 else if(type == MKTAG('s', 'u', 'b', 'p')) {
326 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
327 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
329 get_be32(pb); /* component manufacture */
330 get_be32(pb); /* component flags */
331 get_be32(pb); /* component flags mask */
334 return 0; /* nothing left to read */
336 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
340 static int mov_mp4_read_descr_len(ByteIOContext *pb)
345 int c = get_byte(pb);
346 len = (len << 7) | (c & 0x7f);
353 static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
357 len = mov_mp4_read_descr_len(pb);
358 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
362 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
364 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
365 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
368 /* Well, broken but suffisant for some MP4 streams */
369 get_be32(pb); /* version + flags */
370 len = mov_mp4_read_descr(c, pb, &tag);
371 if (tag == MP4ESDescrTag) {
372 get_be16(pb); /* ID */
373 get_byte(pb); /* priority */
375 get_be16(pb); /* ID */
377 len = mov_mp4_read_descr(c, pb, &tag);
378 if (tag == MP4DecConfigDescrTag) {
379 sc->esds.object_type_id = get_byte(pb);
380 sc->esds.stream_type = get_byte(pb);
381 sc->esds.buffer_size_db = get_be24(pb);
382 sc->esds.max_bitrate = get_be32(pb);
383 sc->esds.avg_bitrate = get_be32(pb);
385 st->codec->codec_id= codec_get_id(ff_mov_obj_type, sc->esds.object_type_id);
386 dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id);
387 len = mov_mp4_read_descr(c, pb, &tag);
388 if (tag == MP4DecSpecificDescrTag) {
389 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
390 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
391 if (st->codec->extradata) {
392 get_buffer(pb, st->codec->extradata, len);
393 st->codec->extradata_size = len;
395 if ((*st->codec->extradata >> 3) == 29) {
396 st->codec->codec_id = CODEC_ID_MP3ON4;
404 /* this atom contains actual media data */
405 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
407 if(atom.size == 0) /* wrong one (MP4) */
409 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
410 c->mdat_list[c->mdat_count].offset = atom.offset;
411 c->mdat_list[c->mdat_count].size = atom.size;
414 c->mdat_offset = atom.offset;
416 return 1; /* found both, just go */
417 url_fskip(pb, atom.size);
418 return 0; /* now go for moov */
421 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
423 uint32_t type = get_le32(pb);
425 if (type != MKTAG('q','t',' ',' '))
427 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
428 get_be32(pb); /* minor version */
429 url_fskip(pb, atom.size - 8);
433 /* this atom should contain all header atoms */
434 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
438 err = mov_read_default(c, pb, atom);
439 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
440 /* so we don't parse the whole file if over a network */
443 return 1; /* found both, just go */
444 return 0; /* now go for mdat */
448 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
450 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
451 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
452 int version = get_byte(pb);
456 return 1; /* unsupported */
458 get_byte(pb); get_byte(pb);
459 get_byte(pb); /* flags */
465 get_be32(pb); /* creation time */
466 get_be32(pb); /* modification time */
469 sc->time_scale = get_be32(pb);
470 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
472 lang = get_be16(pb); /* language */
473 ff_mov_lang_to_iso639(lang, st->language);
474 get_be16(pb); /* quality */
479 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
481 int version = get_byte(pb); /* version */
482 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
488 get_be32(pb); /* creation time */
489 get_be32(pb); /* modification time */
491 c->time_scale = get_be32(pb); /* time scale */
493 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
495 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
496 get_be32(pb); /* preferred scale */
498 get_be16(pb); /* preferred volume */
500 url_fskip(pb, 10); /* reserved */
502 url_fskip(pb, 36); /* display matrix */
504 get_be32(pb); /* preview time */
505 get_be32(pb); /* preview duration */
506 get_be32(pb); /* poster time */
507 get_be32(pb); /* selection time */
508 get_be32(pb); /* selection duration */
509 get_be32(pb); /* current time */
510 get_be32(pb); /* next track ID */
515 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
517 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
519 if((uint64_t)atom.size > (1<<30))
522 // currently SVQ3 decoder expect full STSD header - so let's fake it
523 // this should be fixed and just SMI header should be passed
524 av_free(st->codec->extradata);
525 st->codec->extradata_size = 0x5a + atom.size;
526 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
528 if (st->codec->extradata) {
529 memcpy(st->codec->extradata, "SVQ3", 4); // fake
530 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
531 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
533 url_fskip(pb, atom.size);
538 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
540 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
541 int little_endian = get_be16(pb);
544 switch (st->codec->codec_id) {
545 case CODEC_ID_PCM_S24BE:
546 st->codec->codec_id = CODEC_ID_PCM_S24LE;
548 case CODEC_ID_PCM_S32BE:
549 st->codec->codec_id = CODEC_ID_PCM_S32LE;
558 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
559 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
561 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
562 if((uint64_t)atom.size > (1<<30))
564 av_free(st->codec->extradata);
565 st->codec->extradata_size = atom.size + 8;
566 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
567 if (st->codec->extradata) {
568 AV_WL32(st->codec->extradata + 4, atom.type);
569 get_buffer(pb, st->codec->extradata + 8, atom.size);
571 url_fskip(pb, atom.size);
575 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
577 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
579 if((uint64_t)atom.size > (1<<30))
582 if (st->codec->codec_id == CODEC_ID_QDM2) {
583 // pass all frma atom to codec, needed at least for QDM2
584 av_free(st->codec->extradata);
585 st->codec->extradata_size = atom.size;
586 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
588 if (st->codec->extradata) {
589 get_buffer(pb, st->codec->extradata, atom.size);
591 url_fskip(pb, atom.size);
592 } else if (atom.size > 8) { /* to read frma, esds atoms */
593 mov_read_default(c, pb, atom);
595 url_fskip(pb, atom.size);
599 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
601 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
603 if((uint64_t)atom.size > (1<<30))
606 av_free(st->codec->extradata);
608 st->codec->extradata_size = atom.size;
609 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
611 if (st->codec->extradata) {
612 get_buffer(pb, st->codec->extradata, atom.size);
614 url_fskip(pb, atom.size);
619 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
621 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
622 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
623 unsigned int i, entries;
625 get_byte(pb); /* version */
626 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
628 entries = get_be32(pb);
630 if(entries >= UINT_MAX/sizeof(int64_t))
633 sc->chunk_count = entries;
634 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
635 if (!sc->chunk_offsets)
637 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
638 for(i=0; i<entries; i++) {
639 sc->chunk_offsets[i] = get_be32(pb);
641 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
642 for(i=0; i<entries; i++) {
643 sc->chunk_offsets[i] = get_be64(pb);
651 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
653 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
654 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
655 int entries, frames_per_sample;
657 uint8_t codec_name[32];
659 /* for palette traversal */
667 unsigned char *color_table;
669 unsigned char r, g, b;
671 get_byte(pb); /* version */
672 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
674 entries = get_be32(pb);
676 while(entries--) { //Parsing Sample description table
678 MOV_atom_t a = { 0, 0, 0 };
679 offset_t start_pos = url_ftell(pb);
680 int size = get_be32(pb); /* size */
681 format = get_le32(pb); /* data format */
683 get_be32(pb); /* reserved */
684 get_be16(pb); /* reserved */
685 get_be16(pb); /* index */
687 if (st->codec->codec_tag) {
688 /* multiple fourcc, just skip for now */
689 url_fskip(pb, size - (url_ftell(pb) - start_pos));
693 st->codec->codec_tag = format;
694 id = codec_get_id(codec_movaudio_tags, format);
695 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
696 st->codec->codec_type = CODEC_TYPE_AUDIO;
697 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
698 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
699 id = codec_get_id(codec_movvideo_tags, format);
701 id = codec_get_id(codec_bmp_tags, format);
703 st->codec->codec_type = CODEC_TYPE_VIDEO;
706 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
708 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
709 st->codec->codec_type);
711 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
712 st->codec->codec_id = id;
713 get_be16(pb); /* version */
714 get_be16(pb); /* revision level */
715 get_be32(pb); /* vendor */
716 get_be32(pb); /* temporal quality */
717 get_be32(pb); /* spacial quality */
719 st->codec->width = get_be16(pb); /* width */
720 st->codec->height = get_be16(pb); /* height */
722 get_be32(pb); /* horiz resolution */
723 get_be32(pb); /* vert resolution */
724 get_be32(pb); /* data size, always 0 */
725 frames_per_sample = get_be16(pb); /* frames per samples */
727 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
729 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
730 if (codec_name[0] <= 31) {
731 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
732 st->codec->codec_name[codec_name[0]] = 0;
735 st->codec->bits_per_sample = get_be16(pb); /* depth */
736 st->codec->color_table_id = get_be16(pb); /* colortable id */
738 /* figure out the palette situation */
739 color_depth = st->codec->bits_per_sample & 0x1F;
740 color_greyscale = st->codec->bits_per_sample & 0x20;
742 /* if the depth is 2, 4, or 8 bpp, file is palettized */
743 if ((color_depth == 2) || (color_depth == 4) ||
744 (color_depth == 8)) {
746 if (color_greyscale) {
748 /* compute the greyscale palette */
749 color_count = 1 << color_depth;
751 color_dec = 256 / (color_count - 1);
752 for (j = 0; j < color_count; j++) {
753 r = g = b = color_index;
754 c->palette_control.palette[j] =
755 (r << 16) | (g << 8) | (b);
756 color_index -= color_dec;
761 } else if (st->codec->color_table_id & 0x08) {
763 /* if flag bit 3 is set, use the default palette */
764 color_count = 1 << color_depth;
765 if (color_depth == 2)
766 color_table = ff_qt_default_palette_4;
767 else if (color_depth == 4)
768 color_table = ff_qt_default_palette_16;
770 color_table = ff_qt_default_palette_256;
772 for (j = 0; j < color_count; j++) {
773 r = color_table[j * 4 + 0];
774 g = color_table[j * 4 + 1];
775 b = color_table[j * 4 + 2];
776 c->palette_control.palette[j] =
777 (r << 16) | (g << 8) | (b);
782 /* load the palette from the file */
783 color_start = get_be32(pb);
784 color_count = get_be16(pb);
785 color_end = get_be16(pb);
786 for (j = color_start; j <= color_end; j++) {
787 /* each R, G, or B component is 16 bits;
788 * only use the top 8 bits; skip alpha bytes
798 c->palette_control.palette[j] =
799 (r << 16) | (g << 8) | (b);
803 st->codec->palctrl = &c->palette_control;
804 st->codec->palctrl->palette_changed = 1;
806 st->codec->palctrl = NULL;
807 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
809 uint16_t version = get_be16(pb);
811 st->codec->codec_id = id;
812 get_be16(pb); /* revision level */
813 get_be32(pb); /* vendor */
815 st->codec->channels = get_be16(pb); /* channel count */
816 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
817 st->codec->bits_per_sample = get_be16(pb); /* sample size */
818 /* do we need to force to 16 for AMR ? */
820 /* handle specific s8 codec */
821 get_be16(pb); /* compression id = 0*/
822 get_be16(pb); /* packet size = 0 */
824 st->codec->sample_rate = ((get_be32(pb) >> 16));
826 switch (st->codec->codec_id) {
827 case CODEC_ID_PCM_S8:
828 case CODEC_ID_PCM_U8:
829 if (st->codec->bits_per_sample == 16)
830 st->codec->codec_id = CODEC_ID_PCM_S16BE;
832 case CODEC_ID_PCM_S16LE:
833 case CODEC_ID_PCM_S16BE:
834 if (st->codec->bits_per_sample == 8)
835 st->codec->codec_id = CODEC_ID_PCM_S8;
836 else if (st->codec->bits_per_sample == 24)
837 st->codec->codec_id = CODEC_ID_PCM_S24BE;
843 //Read QT version 1 fields. In version 0 theese dont exist
844 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
847 sc->samples_per_frame = get_be32(pb);
848 get_be32(pb); /* bytes per packet */
849 sc->bytes_per_frame = get_be32(pb);
850 get_be32(pb); /* bytes per sample */
851 } else if(version==2) {
852 get_be32(pb); /* sizeof struct only */
853 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
854 st->codec->channels = get_be32(pb);
855 get_be32(pb); /* always 0x7F000000 */
856 get_be32(pb); /* bits per channel if sound is uncompressed */
857 get_be32(pb); /* lcpm format specific flag */
858 get_be32(pb); /* bytes per audio packet if constant */
859 get_be32(pb); /* lpcm frames per audio packet if constant */
863 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
864 if (bits_per_sample) {
865 st->codec->bits_per_sample = bits_per_sample;
866 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
869 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
870 url_fskip(pb, size - (url_ftell(pb) - start_pos));
872 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
873 a.size = size - (url_ftell(pb) - start_pos);
875 mov_read_default(c, pb, a);
877 url_fskip(pb, a.size);
880 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
881 st->codec->sample_rate= sc->time_scale;
884 /* special codec parameters handling */
885 switch (st->codec->codec_id) {
886 #ifdef CONFIG_H261_DECODER
889 #ifdef CONFIG_H263_DECODER
892 #ifdef CONFIG_MPEG4_DECODER
895 st->codec->width= 0; /* let decoder init width/height */
896 st->codec->height= 0;
898 #ifdef CONFIG_LIBFAAD
901 #ifdef CONFIG_VORBIS_DECODER
902 case CODEC_ID_VORBIS:
904 case CODEC_ID_MP3ON4:
905 st->codec->sample_rate= 0; /* let decoder init parameters properly */
907 #ifdef CONFIG_DV_DEMUXER
908 case CODEC_ID_DVAUDIO:
909 c->dv_fctx = av_alloc_format_context();
910 c->dv_demux = dv_init_demux(c->dv_fctx);
912 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
915 sc->dv_audio_container = 1;
916 st->codec->codec_id = CODEC_ID_PCM_S16LE;
919 /* no ifdef since parameters are always those */
920 case CODEC_ID_AMR_WB:
921 st->codec->sample_rate= 16000;
922 st->codec->channels= 1; /* really needed */
924 case CODEC_ID_AMR_NB:
925 st->codec->sample_rate= 8000;
926 st->codec->channels= 1; /* really needed */
929 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
930 st->need_parsing = 1;
939 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
941 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
942 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
943 unsigned int i, entries;
945 get_byte(pb); /* version */
946 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
948 entries = get_be32(pb);
950 if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
954 av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
956 sc->sample_to_chunk_sz = entries;
957 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
958 if (!sc->sample_to_chunk)
960 for(i=0; i<entries; i++) {
961 sc->sample_to_chunk[i].first = get_be32(pb);
962 sc->sample_to_chunk[i].count = get_be32(pb);
963 sc->sample_to_chunk[i].id = get_be32(pb);
968 static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
970 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
971 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
972 unsigned int i, entries;
974 get_byte(pb); /* version */
975 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
977 entries = get_be32(pb);
979 if(entries >= UINT_MAX / sizeof(long))
982 sc->keyframe_count = entries;
984 av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %d\n", sc->keyframe_count);
986 sc->keyframes = av_malloc(entries * sizeof(long));
989 for(i=0; i<entries; i++) {
990 sc->keyframes[i] = get_be32(pb);
992 /* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
998 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1000 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1001 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1002 unsigned int i, entries, sample_size;
1004 get_byte(pb); /* version */
1005 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1007 sample_size = get_be32(pb);
1008 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1009 sc->sample_size = sample_size;
1010 entries = get_be32(pb);
1011 if(entries >= UINT_MAX / sizeof(long))
1014 sc->sample_count = entries;
1019 av_log(NULL, AV_LOG_DEBUG, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
1021 sc->sample_sizes = av_malloc(entries * sizeof(long));
1022 if (!sc->sample_sizes)
1024 for(i=0; i<entries; i++) {
1025 sc->sample_sizes[i] = get_be32(pb);
1027 av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
1033 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1035 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1036 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1037 unsigned int i, entries;
1039 int64_t total_sample_count=0;
1041 get_byte(pb); /* version */
1042 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1043 entries = get_be32(pb);
1044 if(entries >= UINT_MAX / sizeof(Time2Sample))
1047 sc->stts_count = entries;
1048 sc->stts_data = av_malloc(entries * sizeof(Time2Sample));
1051 av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1056 for(i=0; i<entries; i++) {
1057 int sample_duration;
1060 sample_count=get_be32(pb);
1061 sample_duration = get_be32(pb);
1062 sc->stts_data[i].count= sample_count;
1063 sc->stts_data[i].duration= sample_duration;
1065 sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
1067 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1069 duration+=(int64_t)sample_duration*sample_count;
1070 total_sample_count+=sample_count;
1073 st->nb_frames= total_sample_count;
1075 st->duration= duration;
1079 static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1081 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1082 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1083 unsigned int i, entries;
1085 get_byte(pb); /* version */
1086 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1087 entries = get_be32(pb);
1088 if(entries >= UINT_MAX / sizeof(Time2Sample))
1091 sc->ctts_count = entries;
1092 sc->ctts_data = av_malloc(entries * sizeof(Time2Sample));
1094 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1096 for(i=0; i<entries; i++) {
1097 int count =get_be32(pb);
1098 int duration =get_be32(pb);
1101 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1103 url_fskip(pb, 8 * (entries - i - 1));
1106 sc->ctts_data[i].count = count;
1107 sc->ctts_data[i].duration= duration;
1109 sc->time_rate= ff_gcd(sc->time_rate, duration);
1114 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1117 MOVStreamContext *sc;
1119 st = av_new_stream(c->fc, c->fc->nb_streams);
1121 sc = av_mallocz(sizeof(MOVStreamContext));
1128 st->codec->codec_type = CODEC_TYPE_DATA;
1129 st->start_time = 0; /* XXX: check */
1130 c->streams[c->fc->nb_streams-1] = sc;
1132 return mov_read_default(c, pb, atom);
1135 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1137 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1138 int version = get_byte(pb);
1140 get_byte(pb); get_byte(pb);
1141 get_byte(pb); /* flags */
1143 MOV_TRACK_ENABLED 0x0001
1144 MOV_TRACK_IN_MOVIE 0x0002
1145 MOV_TRACK_IN_PREVIEW 0x0004
1146 MOV_TRACK_IN_POSTER 0x0008
1153 get_be32(pb); /* creation time */
1154 get_be32(pb); /* modification time */
1156 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1157 get_be32(pb); /* reserved */
1158 st->start_time = 0; /* check */
1159 (version == 1) ? get_be64(pb) : get_be32(pb); /* highlevel (considering edits) duration in movie timebase */
1160 get_be32(pb); /* reserved */
1161 get_be32(pb); /* reserved */
1163 get_be16(pb); /* layer */
1164 get_be16(pb); /* alternate group */
1165 get_be16(pb); /* volume */
1166 get_be16(pb); /* reserved */
1168 url_fskip(pb, 36); /* display matrix */
1170 /* those are fixed-point */
1171 get_be32(pb); /* track width */
1172 get_be32(pb); /* track height */
1177 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1178 /* like the files created with Adobe Premiere 5.0, for samples see */
1179 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1180 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1185 return 0; /* continue */
1186 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1187 url_fskip(pb, atom.size - 4);
1190 atom.type = get_le32(pb);
1193 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1194 url_fskip(pb, atom.size);
1197 err = mov_read_mdat(c, pb, atom);
1201 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1206 uint8_t *moov_data; /* uncompressed data */
1207 long cmov_len, moov_len;
1210 get_be32(pb); /* dcom atom */
1211 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1213 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1214 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
1217 get_be32(pb); /* cmvd atom */
1218 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1220 moov_len = get_be32(pb); /* uncompressed size */
1221 cmov_len = atom.size - 6 * 4;
1223 cmov_data = av_malloc(cmov_len);
1226 moov_data = av_malloc(moov_len);
1231 get_buffer(pb, cmov_data, cmov_len);
1232 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1234 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
1236 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1238 atom.size = moov_len;
1240 // { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1242 ret = mov_read_default(c, &ctx, atom);
1247 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1252 /* edit list atom */
1253 static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1257 get_byte(pb); /* version */
1258 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1259 edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
1261 for(i=0; i<edit_count; i++){
1262 get_be32(pb); /* Track duration */
1263 get_be32(pb); /* Media time */
1264 get_be32(pb); /* Media rate */
1266 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1270 static const MOVParseTableEntry mov_default_parse_table[] = {
1272 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1273 { MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
1274 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1275 { MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
1276 { MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
1277 { MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
1278 { MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
1279 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1280 { MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
1281 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1282 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1283 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1284 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1285 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1286 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1287 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1288 { MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
1289 { MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
1290 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1291 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1292 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1293 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1294 { MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
1295 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1296 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1297 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1298 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1299 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
1300 { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1301 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1302 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1303 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1307 static void mov_free_stream_context(MOVStreamContext *sc)
1310 av_freep(&sc->ctts_data);
1315 /* XXX: is it sufficient ? */
1316 static int mov_probe(AVProbeData *p)
1318 unsigned int offset;
1322 /* check file header */
1323 if (p->buf_size <= 12)
1327 /* ignore invalid offset */
1328 if ((offset + 8) > (unsigned int)p->buf_size)
1330 tag = AV_RL32(p->buf + offset + 4);
1332 /* check for obvious tags */
1333 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
1334 case MKTAG( 'm', 'o', 'o', 'v' ):
1335 case MKTAG( 'm', 'd', 'a', 't' ):
1336 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1337 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1338 return AVPROBE_SCORE_MAX;
1339 /* those are more common words, so rate then a bit less */
1340 case MKTAG( 'w', 'i', 'd', 'e' ):
1341 case MKTAG( 'f', 'r', 'e', 'e' ):
1342 case MKTAG( 'j', 'u', 'n', 'k' ):
1343 case MKTAG( 'p', 'i', 'c', 't' ):
1344 return AVPROBE_SCORE_MAX - 5;
1345 case MKTAG( 'f', 't', 'y', 'p' ):
1346 case MKTAG( 's', 'k', 'i', 'p' ):
1347 case MKTAG( 'u', 'u', 'i', 'd' ):
1348 offset = AV_RB32(p->buf+offset) + offset;
1349 /* if we only find those cause probedata is too small at least rate them */
1350 score = AVPROBE_SCORE_MAX - 50;
1353 /* unrecognized tag */
1360 static void mov_build_index(MOVContext *mov, AVStream *st)
1362 MOVStreamContext *sc = st->priv_data;
1363 offset_t current_offset;
1364 int64_t current_dts = 0;
1365 unsigned int stts_index = 0;
1366 unsigned int stsc_index = 0;
1367 unsigned int stss_index = 0;
1368 unsigned int i, j, k;
1370 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO || sc->dv_audio_container) {
1371 unsigned int current_sample = 0;
1372 unsigned int stts_sample = 0;
1373 unsigned int keyframe, sample_size;
1374 unsigned int distance = 0;
1376 st->nb_frames = sc->sample_count;
1377 for (i = 0; i < sc->chunk_count; i++) {
1378 current_offset = sc->chunk_offsets[i];
1379 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1381 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1382 if (current_sample >= sc->sample_count) {
1383 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1386 keyframe = !sc->keyframe_count || current_sample + 1 == sc->keyframes[stss_index];
1389 if (stss_index + 1 < sc->keyframe_count)
1392 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1393 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", size %d, distance %d, keyframe %d\n",
1394 st->index, current_sample, current_offset, current_dts, sample_size, distance, keyframe);
1395 av_add_index_entry(st, current_offset, current_dts, sample_size, distance, keyframe ? AVINDEX_KEYFRAME : 0);
1396 current_offset += sample_size;
1397 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1398 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1402 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1408 } else { /* read whole chunk */
1409 unsigned int chunk_samples, chunk_size, chunk_duration;
1411 for (i = 0; i < sc->chunk_count; i++) {
1412 current_offset = sc->chunk_offsets[i];
1413 if (stsc_index + 1 < sc->sample_to_chunk_sz && i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1415 chunk_samples = sc->sample_to_chunk[stsc_index].count;
1416 /* get chunk size */
1417 if (sc->sample_size > 1 || st->codec->codec_id == CODEC_ID_PCM_U8 || st->codec->codec_id == CODEC_ID_PCM_S8)
1418 chunk_size = chunk_samples * sc->sample_size;
1419 else if (sc->samples_per_frame > 0 && (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0))
1420 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1421 else { /* workaround to find nearest next chunk offset */
1422 chunk_size = INT_MAX;
1423 for (j = 0; j < mov->total_streams; j++) {
1424 MOVStreamContext *msc = mov->streams[j];
1426 for (k = msc->next_chunk; k < msc->chunk_count; k++) {
1427 if (msc->chunk_offsets[k] > current_offset && msc->chunk_offsets[k] - current_offset < chunk_size) {
1428 chunk_size = msc->chunk_offsets[k] - current_offset;
1429 msc->next_chunk = k;
1434 /* check for last chunk */
1435 if (chunk_size == INT_MAX)
1436 for (j = 0; j < mov->mdat_count; j++) {
1437 dprintf(mov->fc, "mdat %d, offset %"PRIx64", size %"PRId64", current offset %"PRIx64"\n",
1438 j, mov->mdat_list[j].offset, mov->mdat_list[j].size, current_offset);
1439 if (mov->mdat_list[j].offset <= current_offset && mov->mdat_list[j].offset + mov->mdat_list[j].size > current_offset)
1440 chunk_size = mov->mdat_list[j].offset + mov->mdat_list[j].size - current_offset;
1442 assert(chunk_size != INT_MAX);
1443 for (j = 0; j < mov->total_streams; j++) {
1444 mov->streams[j]->next_chunk = 0;
1447 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1448 /* get chunk duration */
1450 while (chunk_samples > 0) {
1451 if (chunk_samples < sc->stts_data[stts_index].count) {
1452 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1453 sc->stts_data[stts_index].count -= chunk_samples;
1456 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1457 chunk_samples -= sc->stts_data[stts_index].count;
1458 if (stts_index + 1 < sc->stts_count) {
1463 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", size %d, duration %d\n",
1464 st->index, i, current_offset, current_dts, chunk_size, chunk_duration);
1465 assert(chunk_duration % sc->time_rate == 0);
1466 current_dts += chunk_duration / sc->time_rate;
1470 /* adjust sample count to avindex entries */
1471 sc->sample_count = st->nb_index_entries;
1474 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1476 MOVContext *mov = (MOVContext *) s->priv_data;
1477 ByteIOContext *pb = &s->pb;
1479 MOV_atom_t atom = { 0, 0, 0 };
1482 mov->parse_table = mov_default_parse_table;
1484 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1485 atom.size = url_fsize(pb);
1487 atom.size = 0x7FFFFFFFFFFFFFFFLL;
1489 /* check MOV header */
1490 err = mov_read_default(mov, pb, atom);
1491 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1492 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
1493 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1496 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1498 /* some cleanup : make sure we are on the mdat atom */
1499 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1500 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1502 mov->total_streams = s->nb_streams;
1504 for(i=0; i<mov->total_streams; i++) {
1505 MOVStreamContext *sc = mov->streams[i];
1506 AVStream *st = s->streams[i];
1508 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
1509 (!sc->sample_size && !sc->sample_count)){
1510 av_log(s, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
1516 sc->time_scale= mov->time_scale;
1517 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1519 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1520 st->codec->frame_size = sc->stts_data[0].duration;
1522 if(st->duration != AV_NOPTS_VALUE){
1523 assert(st->duration % sc->time_rate == 0);
1524 st->duration /= sc->time_rate;
1527 mov_build_index(mov, st);
1530 for(i=0; i<mov->total_streams; i++) {
1531 /* dont need those anymore */
1532 av_freep(&mov->streams[i]->chunk_offsets);
1533 av_freep(&mov->streams[i]->sample_to_chunk);
1534 av_freep(&mov->streams[i]->sample_sizes);
1535 av_freep(&mov->streams[i]->keyframes);
1536 av_freep(&mov->streams[i]->stts_data);
1538 av_freep(&mov->mdat_list);
1542 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1544 MOVContext *mov = s->priv_data;
1545 MOVStreamContext *sc = 0;
1546 AVIndexEntry *sample = 0;
1547 int64_t best_dts = INT64_MAX;
1550 for (i = 0; i < mov->total_streams; i++) {
1551 MOVStreamContext *msc = mov->streams[i];
1553 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1554 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1555 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1557 dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
1558 if (dts < best_dts) {
1559 sample = current_sample;
1567 /* must be done just before reading, to avoid infinite loop on sample */
1568 sc->current_sample++;
1569 if (sample->pos >= url_fsize(&s->pb)) {
1570 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1573 #ifdef CONFIG_DV_DEMUXER
1574 if (sc->dv_audio_container) {
1575 dv_get_packet(mov->dv_demux, pkt);
1576 dprintf(s, "dv audio pkt size %d\n", pkt->size);
1579 url_fseek(&s->pb, sample->pos, SEEK_SET);
1580 av_get_packet(&s->pb, pkt, sample->size);
1581 #ifdef CONFIG_DV_DEMUXER
1582 if (mov->dv_demux) {
1583 void *pkt_destruct_func = pkt->destruct;
1584 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1585 pkt->destruct = pkt_destruct_func;
1589 pkt->stream_index = sc->ffindex;
1590 pkt->dts = sample->timestamp;
1591 if (sc->ctts_data) {
1592 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1593 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1594 /* update ctts context */
1595 sc->sample_to_ctime_sample++;
1596 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1597 sc->sample_to_ctime_index++;
1598 sc->sample_to_ctime_sample = 0;
1601 pkt->pts = pkt->dts;
1603 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1604 pkt->pos = sample->pos;
1605 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);
1609 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1611 MOVStreamContext *sc = st->priv_data;
1612 int sample, time_sample;
1615 sample = av_index_search_timestamp(st, timestamp, flags);
1616 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1617 if (sample < 0) /* not sure what to do */
1619 sc->current_sample = sample;
1620 dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample);
1621 /* adjust ctts index */
1622 if (sc->ctts_data) {
1624 for (i = 0; i < sc->ctts_count; i++) {
1625 time_sample += sc->ctts_data[i].count;
1626 if (time_sample >= sc->current_sample) {
1627 sc->sample_to_ctime_index = i;
1628 sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1636 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1639 int64_t seek_timestamp, timestamp;
1643 if (stream_index >= s->nb_streams)
1646 st = s->streams[stream_index];
1647 sample = mov_seek_stream(st, sample_time, flags);
1651 /* adjust seek timestamp to found sample timestamp */
1652 seek_timestamp = st->index_entries[sample].timestamp;
1654 for (i = 0; i < s->nb_streams; i++) {
1656 if (stream_index == i || st->discard == AVDISCARD_ALL)
1659 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1660 mov_seek_stream(st, timestamp, flags);
1665 static int mov_read_close(AVFormatContext *s)
1668 MOVContext *mov = (MOVContext *) s->priv_data;
1669 for(i=0; i<mov->total_streams; i++)
1670 mov_free_stream_context(mov->streams[i]);
1671 /* free color tabs */
1672 for(i=0; i<mov->ctab_size; i++)
1673 av_freep(&mov->ctab[i]);
1675 for(i=0; i<mov->dv_fctx->nb_streams; i++){
1676 av_freep(&mov->dv_fctx->streams[i]->codec);
1677 av_freep(&mov->dv_fctx->streams[i]);
1679 av_freep(&mov->dv_fctx);
1680 av_freep(&mov->dv_demux);
1682 av_freep(&mov->ctab);
1686 AVInputFormat mov_demuxer = {
1687 "mov,mp4,m4a,3gp,3g2,mj2",
1688 "QuickTime/MPEG4/Motion JPEG 2000 format",