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;
248 if(a.size < 0 || a.size > atom.size - total_size)
251 for (i = 0; c->parse_table[i].type != 0L
252 && c->parse_table[i].type != a.type; i++)
255 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
256 url_fskip(pb, a.size);
258 offset_t start_pos = url_ftell(pb);
260 err = (c->parse_table[i].func)(c, pb, a);
261 left = a.size - url_ftell(pb) + start_pos;
262 if (left > 0) /* skip garbage at atom end */
267 total_size += a.size;
270 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
271 url_fskip(pb, atom.size - total_size);
277 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
280 url_fskip(pb, atom.size); // for now
282 VERY VERY BROKEN, NEVER execute this, needs rewrite
285 c->ctab = av_realloc(c->ctab, ++c->ctab_size);
286 t = c->ctab[c->ctab_size];
287 t->seed = get_be32(pb);
288 t->flags = get_be16(pb);
289 t->size = get_be16(pb) + 1;
290 len = 2 * t->size * 4;
292 t->clrs = av_malloc(len); // 16bit A R G B
294 get_buffer(pb, t->clrs, len);
301 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
303 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
307 get_byte(pb); /* version */
308 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
311 ctype = get_le32(pb);
312 type = get_le32(pb); /* component subtype */
314 dprintf(c->fc, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
315 dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
318 if(type == MKTAG('v', 'i', 'd', 'e'))
319 st->codec->codec_type = CODEC_TYPE_VIDEO;
320 else if(type == MKTAG('s', 'o', 'u', 'n'))
321 st->codec->codec_type = CODEC_TYPE_AUDIO;
322 else if(type == MKTAG('m', '1', 'a', ' '))
323 st->codec->codec_id = CODEC_ID_MP2;
324 else if(type == MKTAG('s', 'u', 'b', 'p')) {
325 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
326 st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
328 get_be32(pb); /* component manufacture */
329 get_be32(pb); /* component flags */
330 get_be32(pb); /* component flags mask */
333 return 0; /* nothing left to read */
335 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
339 static int mov_mp4_read_descr_len(ByteIOContext *pb)
344 int c = get_byte(pb);
345 len = (len << 7) | (c & 0x7f);
352 static int mov_mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
356 len = mov_mp4_read_descr_len(pb);
357 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
361 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
363 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
364 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
367 /* Well, broken but suffisant for some MP4 streams */
368 get_be32(pb); /* version + flags */
369 len = mov_mp4_read_descr(c, pb, &tag);
370 if (tag == MP4ESDescrTag) {
371 get_be16(pb); /* ID */
372 get_byte(pb); /* priority */
374 get_be16(pb); /* ID */
376 len = mov_mp4_read_descr(c, pb, &tag);
377 if (tag == MP4DecConfigDescrTag) {
378 sc->esds.object_type_id = get_byte(pb);
379 sc->esds.stream_type = get_byte(pb);
380 sc->esds.buffer_size_db = get_be24(pb);
381 sc->esds.max_bitrate = get_be32(pb);
382 sc->esds.avg_bitrate = get_be32(pb);
384 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, sc->esds.object_type_id);
385 dprintf(c->fc, "esds object type id %d\n", sc->esds.object_type_id);
386 len = mov_mp4_read_descr(c, pb, &tag);
387 if (tag == MP4DecSpecificDescrTag) {
388 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
389 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
390 if (st->codec->extradata) {
391 get_buffer(pb, st->codec->extradata, len);
392 st->codec->extradata_size = len;
394 if ((*st->codec->extradata >> 3) == 29) {
395 st->codec->codec_id = CODEC_ID_MP3ON4;
403 /* this atom contains actual media data */
404 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
406 if(atom.size == 0) /* wrong one (MP4) */
408 c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
409 c->mdat_list[c->mdat_count].offset = atom.offset;
410 c->mdat_list[c->mdat_count].size = atom.size;
413 c->mdat_offset = atom.offset;
415 return 1; /* found both, just go */
416 url_fskip(pb, atom.size);
417 return 0; /* now go for moov */
420 static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
422 uint32_t type = get_le32(pb);
424 if (type != MKTAG('q','t',' ',' '))
426 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
427 get_be32(pb); /* minor version */
428 url_fskip(pb, atom.size - 8);
432 /* this atom should contain all header atoms */
433 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
437 err = mov_read_default(c, pb, atom);
438 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
439 /* so we don't parse the whole file if over a network */
442 return 1; /* found both, just go */
443 return 0; /* now go for mdat */
447 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
449 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
450 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
451 int version = get_byte(pb);
455 return 1; /* unsupported */
457 get_byte(pb); get_byte(pb);
458 get_byte(pb); /* flags */
464 get_be32(pb); /* creation time */
465 get_be32(pb); /* modification time */
468 sc->time_scale = get_be32(pb);
469 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
471 lang = get_be16(pb); /* language */
472 ff_mov_lang_to_iso639(lang, st->language);
473 get_be16(pb); /* quality */
478 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
480 int version = get_byte(pb); /* version */
481 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
487 get_be32(pb); /* creation time */
488 get_be32(pb); /* modification time */
490 c->time_scale = get_be32(pb); /* time scale */
492 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
494 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
495 get_be32(pb); /* preferred scale */
497 get_be16(pb); /* preferred volume */
499 url_fskip(pb, 10); /* reserved */
501 url_fskip(pb, 36); /* display matrix */
503 get_be32(pb); /* preview time */
504 get_be32(pb); /* preview duration */
505 get_be32(pb); /* poster time */
506 get_be32(pb); /* selection time */
507 get_be32(pb); /* selection duration */
508 get_be32(pb); /* current time */
509 get_be32(pb); /* next track ID */
514 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
516 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
518 if((uint64_t)atom.size > (1<<30))
521 // currently SVQ3 decoder expect full STSD header - so let's fake it
522 // this should be fixed and just SMI header should be passed
523 av_free(st->codec->extradata);
524 st->codec->extradata_size = 0x5a + atom.size;
525 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
527 if (st->codec->extradata) {
528 memcpy(st->codec->extradata, "SVQ3", 4); // fake
529 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
530 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
532 url_fskip(pb, atom.size);
537 static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
539 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
540 int little_endian = get_be16(pb);
543 switch (st->codec->codec_id) {
544 case CODEC_ID_PCM_S24BE:
545 st->codec->codec_id = CODEC_ID_PCM_S24LE;
547 case CODEC_ID_PCM_S32BE:
548 st->codec->codec_id = CODEC_ID_PCM_S32LE;
557 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
558 static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
560 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
561 if((uint64_t)atom.size > (1<<30))
563 av_free(st->codec->extradata);
564 st->codec->extradata_size = atom.size + 8;
565 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
566 if (st->codec->extradata) {
567 AV_WL32(st->codec->extradata + 4, atom.type);
568 get_buffer(pb, st->codec->extradata + 8, atom.size);
570 url_fskip(pb, atom.size);
574 static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
576 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
578 if((uint64_t)atom.size > (1<<30))
581 if (st->codec->codec_id == CODEC_ID_QDM2) {
582 // pass all frma atom to codec, needed at least for QDM2
583 av_free(st->codec->extradata);
584 st->codec->extradata_size = atom.size;
585 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
587 if (st->codec->extradata) {
588 get_buffer(pb, st->codec->extradata, atom.size);
590 url_fskip(pb, atom.size);
591 } else if (atom.size > 8) { /* to read frma, esds atoms */
592 mov_read_default(c, pb, atom);
594 url_fskip(pb, atom.size);
598 static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
600 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
602 if((uint64_t)atom.size > (1<<30))
605 av_free(st->codec->extradata);
607 st->codec->extradata_size = atom.size;
608 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
610 if (st->codec->extradata) {
611 get_buffer(pb, st->codec->extradata, atom.size);
613 url_fskip(pb, atom.size);
618 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
620 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
621 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
622 unsigned int i, entries;
624 get_byte(pb); /* version */
625 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
627 entries = get_be32(pb);
629 if(entries >= UINT_MAX/sizeof(int64_t))
632 sc->chunk_count = entries;
633 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
634 if (!sc->chunk_offsets)
636 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
637 for(i=0; i<entries; i++) {
638 sc->chunk_offsets[i] = get_be32(pb);
640 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
641 for(i=0; i<entries; i++) {
642 sc->chunk_offsets[i] = get_be64(pb);
650 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
652 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
653 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
654 int entries, frames_per_sample;
656 uint8_t codec_name[32];
658 /* for palette traversal */
666 unsigned char *color_table;
668 unsigned char r, g, b;
670 get_byte(pb); /* version */
671 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
673 entries = get_be32(pb);
675 while(entries--) { //Parsing Sample description table
677 MOV_atom_t a = { 0, 0, 0 };
678 offset_t start_pos = url_ftell(pb);
679 int size = get_be32(pb); /* size */
680 format = get_le32(pb); /* data format */
682 get_be32(pb); /* reserved */
683 get_be16(pb); /* reserved */
684 get_be16(pb); /* index */
686 if (st->codec->codec_tag) {
687 /* multiple fourcc, just skip for now */
688 url_fskip(pb, size - (url_ftell(pb) - start_pos));
692 st->codec->codec_tag = format;
693 id = codec_get_id(codec_movaudio_tags, format);
694 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
695 st->codec->codec_type = CODEC_TYPE_AUDIO;
696 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
697 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
698 id = codec_get_id(codec_movvideo_tags, format);
700 id = codec_get_id(codec_bmp_tags, format);
702 st->codec->codec_type = CODEC_TYPE_VIDEO;
705 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
707 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff,
708 st->codec->codec_type);
710 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
711 st->codec->codec_id = id;
712 get_be16(pb); /* version */
713 get_be16(pb); /* revision level */
714 get_be32(pb); /* vendor */
715 get_be32(pb); /* temporal quality */
716 get_be32(pb); /* spacial quality */
718 st->codec->width = get_be16(pb); /* width */
719 st->codec->height = get_be16(pb); /* height */
721 get_be32(pb); /* horiz resolution */
722 get_be32(pb); /* vert resolution */
723 get_be32(pb); /* data size, always 0 */
724 frames_per_sample = get_be16(pb); /* frames per samples */
726 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
728 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
729 if (codec_name[0] <= 31) {
730 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
731 st->codec->codec_name[codec_name[0]] = 0;
734 st->codec->bits_per_sample = get_be16(pb); /* depth */
735 st->codec->color_table_id = get_be16(pb); /* colortable id */
737 /* figure out the palette situation */
738 color_depth = st->codec->bits_per_sample & 0x1F;
739 color_greyscale = st->codec->bits_per_sample & 0x20;
741 /* if the depth is 2, 4, or 8 bpp, file is palettized */
742 if ((color_depth == 2) || (color_depth == 4) ||
743 (color_depth == 8)) {
745 if (color_greyscale) {
747 /* compute the greyscale palette */
748 color_count = 1 << color_depth;
750 color_dec = 256 / (color_count - 1);
751 for (j = 0; j < color_count; j++) {
752 r = g = b = color_index;
753 c->palette_control.palette[j] =
754 (r << 16) | (g << 8) | (b);
755 color_index -= color_dec;
760 } else if (st->codec->color_table_id & 0x08) {
762 /* if flag bit 3 is set, use the default palette */
763 color_count = 1 << color_depth;
764 if (color_depth == 2)
765 color_table = ff_qt_default_palette_4;
766 else if (color_depth == 4)
767 color_table = ff_qt_default_palette_16;
769 color_table = ff_qt_default_palette_256;
771 for (j = 0; j < color_count; j++) {
772 r = color_table[j * 4 + 0];
773 g = color_table[j * 4 + 1];
774 b = color_table[j * 4 + 2];
775 c->palette_control.palette[j] =
776 (r << 16) | (g << 8) | (b);
781 /* load the palette from the file */
782 color_start = get_be32(pb);
783 color_count = get_be16(pb);
784 color_end = get_be16(pb);
785 for (j = color_start; j <= color_end; j++) {
786 /* each R, G, or B component is 16 bits;
787 * only use the top 8 bits; skip alpha bytes
797 c->palette_control.palette[j] =
798 (r << 16) | (g << 8) | (b);
802 st->codec->palctrl = &c->palette_control;
803 st->codec->palctrl->palette_changed = 1;
805 st->codec->palctrl = NULL;
806 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
808 uint16_t version = get_be16(pb);
810 st->codec->codec_id = id;
811 get_be16(pb); /* revision level */
812 get_be32(pb); /* vendor */
814 st->codec->channels = get_be16(pb); /* channel count */
815 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
816 st->codec->bits_per_sample = get_be16(pb); /* sample size */
817 /* do we need to force to 16 for AMR ? */
819 /* handle specific s8 codec */
820 get_be16(pb); /* compression id = 0*/
821 get_be16(pb); /* packet size = 0 */
823 st->codec->sample_rate = ((get_be32(pb) >> 16));
825 switch (st->codec->codec_id) {
826 case CODEC_ID_PCM_S8:
827 case CODEC_ID_PCM_U8:
828 if (st->codec->bits_per_sample == 16)
829 st->codec->codec_id = CODEC_ID_PCM_S16BE;
831 case CODEC_ID_PCM_S16LE:
832 case CODEC_ID_PCM_S16BE:
833 if (st->codec->bits_per_sample == 8)
834 st->codec->codec_id = CODEC_ID_PCM_S8;
835 else if (st->codec->bits_per_sample == 24)
836 st->codec->codec_id = CODEC_ID_PCM_S24BE;
842 //Read QT version 1 fields. In version 0 theese dont exist
843 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
846 sc->samples_per_frame = get_be32(pb);
847 get_be32(pb); /* bytes per packet */
848 sc->bytes_per_frame = get_be32(pb);
849 get_be32(pb); /* bytes per sample */
850 } else if(version==2) {
851 get_be32(pb); /* sizeof struct only */
852 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
853 st->codec->channels = get_be32(pb);
854 get_be32(pb); /* always 0x7F000000 */
855 get_be32(pb); /* bits per channel if sound is uncompressed */
856 get_be32(pb); /* lcpm format specific flag */
857 get_be32(pb); /* bytes per audio packet if constant */
858 get_be32(pb); /* lpcm frames per audio packet if constant */
862 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
863 if (bits_per_sample) {
864 st->codec->bits_per_sample = bits_per_sample;
865 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
868 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
869 url_fskip(pb, size - (url_ftell(pb) - start_pos));
871 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
872 a.size = size - (url_ftell(pb) - start_pos);
874 mov_read_default(c, pb, a);
876 url_fskip(pb, a.size);
879 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1) {
880 st->codec->sample_rate= sc->time_scale;
883 /* special codec parameters handling */
884 switch (st->codec->codec_id) {
885 #ifdef CONFIG_H261_DECODER
888 #ifdef CONFIG_H263_DECODER
891 #ifdef CONFIG_MPEG4_DECODER
894 st->codec->width= 0; /* let decoder init width/height */
895 st->codec->height= 0;
897 #ifdef CONFIG_LIBFAAD
900 #ifdef CONFIG_VORBIS_DECODER
901 case CODEC_ID_VORBIS:
903 case CODEC_ID_MP3ON4:
904 st->codec->sample_rate= 0; /* let decoder init parameters properly */
906 #ifdef CONFIG_DV_DEMUXER
907 case CODEC_ID_DVAUDIO:
908 c->dv_fctx = av_alloc_format_context();
909 c->dv_demux = dv_init_demux(c->dv_fctx);
911 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
914 sc->dv_audio_container = 1;
915 st->codec->codec_id = CODEC_ID_PCM_S16LE;
918 /* no ifdef since parameters are always those */
919 case CODEC_ID_AMR_WB:
920 st->codec->sample_rate= 16000;
921 st->codec->channels= 1; /* really needed */
923 case CODEC_ID_AMR_NB:
924 st->codec->sample_rate= 8000;
925 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");
1511 sc->sample_count = 0; //ignore track
1517 sc->time_scale= mov->time_scale;
1518 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1520 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1521 st->codec->frame_size = sc->stts_data[0].duration;
1523 if(st->duration != AV_NOPTS_VALUE){
1524 assert(st->duration % sc->time_rate == 0);
1525 st->duration /= sc->time_rate;
1528 mov_build_index(mov, st);
1531 for(i=0; i<mov->total_streams; i++) {
1532 /* dont need those anymore */
1533 av_freep(&mov->streams[i]->chunk_offsets);
1534 av_freep(&mov->streams[i]->sample_to_chunk);
1535 av_freep(&mov->streams[i]->sample_sizes);
1536 av_freep(&mov->streams[i]->keyframes);
1537 av_freep(&mov->streams[i]->stts_data);
1539 av_freep(&mov->mdat_list);
1543 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1545 MOVContext *mov = s->priv_data;
1546 MOVStreamContext *sc = 0;
1547 AVIndexEntry *sample = 0;
1548 int64_t best_dts = INT64_MAX;
1551 for (i = 0; i < mov->total_streams; i++) {
1552 MOVStreamContext *msc = mov->streams[i];
1554 if (s->streams[i]->discard != AVDISCARD_ALL && msc->current_sample < msc->sample_count) {
1555 AVIndexEntry *current_sample = &s->streams[i]->index_entries[msc->current_sample];
1556 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate, AV_TIME_BASE, msc->time_scale);
1558 dprintf(s, "stream %d, sample %ld, dts %"PRId64"\n", i, msc->current_sample, dts);
1559 if (dts < best_dts) {
1560 sample = current_sample;
1568 /* must be done just before reading, to avoid infinite loop on sample */
1569 sc->current_sample++;
1570 if (sample->pos >= url_fsize(&s->pb)) {
1571 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n", sc->ffindex, sample->pos);
1574 #ifdef CONFIG_DV_DEMUXER
1575 if (sc->dv_audio_container) {
1576 dv_get_packet(mov->dv_demux, pkt);
1577 dprintf(s, "dv audio pkt size %d\n", pkt->size);
1580 url_fseek(&s->pb, sample->pos, SEEK_SET);
1581 av_get_packet(&s->pb, pkt, sample->size);
1582 #ifdef CONFIG_DV_DEMUXER
1583 if (mov->dv_demux) {
1584 void *pkt_destruct_func = pkt->destruct;
1585 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1586 pkt->destruct = pkt_destruct_func;
1590 pkt->stream_index = sc->ffindex;
1591 pkt->dts = sample->timestamp;
1592 if (sc->ctts_data) {
1593 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1594 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1595 /* update ctts context */
1596 sc->sample_to_ctime_sample++;
1597 if (sc->sample_to_ctime_index < sc->ctts_count && sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
1598 sc->sample_to_ctime_index++;
1599 sc->sample_to_ctime_sample = 0;
1602 pkt->pts = pkt->dts;
1604 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1605 pkt->pos = sample->pos;
1606 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);
1610 static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1612 MOVStreamContext *sc = st->priv_data;
1613 int sample, time_sample;
1616 sample = av_index_search_timestamp(st, timestamp, flags);
1617 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
1618 if (sample < 0) /* not sure what to do */
1620 sc->current_sample = sample;
1621 dprintf(st->codec, "stream %d, found sample %ld\n", st->index, sc->current_sample);
1622 /* adjust ctts index */
1623 if (sc->ctts_data) {
1625 for (i = 0; i < sc->ctts_count; i++) {
1626 time_sample += sc->ctts_data[i].count;
1627 if (time_sample >= sc->current_sample) {
1628 sc->sample_to_ctime_index = i;
1629 sc->sample_to_ctime_sample = time_sample - sc->current_sample;
1637 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
1640 int64_t seek_timestamp, timestamp;
1644 if (stream_index >= s->nb_streams)
1647 st = s->streams[stream_index];
1648 sample = mov_seek_stream(st, sample_time, flags);
1652 /* adjust seek timestamp to found sample timestamp */
1653 seek_timestamp = st->index_entries[sample].timestamp;
1655 for (i = 0; i < s->nb_streams; i++) {
1657 if (stream_index == i || st->discard == AVDISCARD_ALL)
1660 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1661 mov_seek_stream(st, timestamp, flags);
1666 static int mov_read_close(AVFormatContext *s)
1669 MOVContext *mov = (MOVContext *) s->priv_data;
1670 for(i=0; i<mov->total_streams; i++)
1671 mov_free_stream_context(mov->streams[i]);
1672 /* free color tabs */
1673 for(i=0; i<mov->ctab_size; i++)
1674 av_freep(&mov->ctab[i]);
1676 for(i=0; i<mov->dv_fctx->nb_streams; i++){
1677 av_freep(&mov->dv_fctx->streams[i]->codec);
1678 av_freep(&mov->dv_fctx->streams[i]);
1680 av_freep(&mov->dv_fctx);
1681 av_freep(&mov->dv_demux);
1683 av_freep(&mov->ctab);
1687 AVInputFormat mov_demuxer = {
1688 "mov,mp4,m4a,3gp,3g2,mj2",
1689 "QuickTime/MPEG4/Motion JPEG 2000 format",