3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * First version by Francois Revol revol@free.fr
29 * Features and limitations:
30 * - reads most of the QT files I have (at least the structure),
31 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
32 * FIXED, Francois Revol, 07/17/2002
33 * - ffmpeg has nearly none of the usual QuickTime codecs,
34 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
35 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
36 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
37 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
38 * http://mpeg.telecomitalialab.com/faq.htm
39 * - the code is quite ugly... maybe I won't do it recursive next time :-)
41 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
42 * when coding this :) (it's a writer anyway)
44 * Reference documents:
45 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
47 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
48 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
49 * QuickTime is a trademark of Apple (AFAIK :))
54 /* allows chunk splitting - should work now... */
55 /* in case you can't read a file, try commenting */
56 #define MOV_SPLIT_CHUNKS
60 * XXX: static sux, even more in a multithreaded environment...
61 * Avoid them. This is here just to help debugging.
63 static int debug_indent = 0;
64 void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
67 tag = (unsigned int) type;
69 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
73 printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
83 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
84 /* so we first list them as this, then clean up the list of streams we give back, */
85 /* getting rid of these */
86 #define CODEC_TYPE_MOV_OTHER 2
88 static const CodecTag mov_video_tags[] = {
89 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
90 /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
91 /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
92 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
97 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
98 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
99 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
100 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
101 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
103 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
104 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
105 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
106 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
107 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
108 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
109 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
113 static const CodecTag mov_audio_tags[] = {
114 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
115 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
116 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
117 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
118 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
119 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
120 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
121 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
123 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
124 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
125 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
126 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
128 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
129 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
133 /* the QuickTime file format is quite convoluted...
134 * it has lots of index tables, each indexing something in another one...
135 * Here we just use what is needed to read the chunks
138 typedef struct MOV_sample_to_chunk_tbl {
142 } MOV_sample_to_chunk_tbl;
144 typedef struct MOVStreamContext {
145 int ffindex; /* the ffmpeg stream id */
146 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
149 INT64 *chunk_offsets;
150 long sample_to_chunk_sz;
151 MOV_sample_to_chunk_tbl *sample_to_chunk;
152 long sample_to_chunk_index;
158 long left_in_chunk; /* how many samples before next chunk */
161 typedef struct MOVContext {
162 int mp4; /* set to 1 as soon as we are sure that the file is an .mp4 file (even some header parsing depends on this) */
165 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
166 int found_mdat; /* we suppose we have enough data to read the file */
170 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
171 * but we need the info to be able to skip data from those streams in the 'mdat' section
173 MOVStreamContext *streams[MAX_STREAMS];
175 INT64 next_chunk_offset;
176 int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */
180 struct MOVParseTableEntry;
182 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
184 /* those functions parse an atom */
186 1: found what I wanted, exit
187 0: continue to parse next atom
188 -1: error occured, exit
190 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
193 INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
194 INT64 atom_size, /* total size (excluding the size and type fields) */
197 /* links atom IDs to parse functions */
198 typedef struct MOVParseTableEntry {
200 mov_parse_function func;
201 } MOVParseTableEntry;
203 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
206 print_atom("leaf", atom_type, atom_offset, atom_size);
209 url_fskip(pb, atom_size);
210 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
215 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
219 UINT64 total_size = 0;
224 print_atom("default", atom_type, atom_offset, atom_size);
228 offset = atom_offset;
231 atom_size = 0x0FFFFFFFFFFFFFFF;
232 while((total_size < atom_size) && !url_feof(pb) && !err) {
241 // printf("type: %08lx sz: %08lx", type, size);
242 if(size == 1) { /* 64 bit extended size */
249 size = atom_size - total_size;
251 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
253 // printf(" i=%ld\n", i);
254 if (parse_table[i].type == 0) { /* skip leaf atoms data */
255 // url_seek(pb, atom_offset+atom_size, SEEK_SET);
257 print_atom("unknown", type, offset, size);
261 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
273 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
277 print_atom("mvhd", atom_type, atom_offset, atom_size);
279 c = (MOVContext *)param;
281 get_byte(pb); /* version */
282 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
284 get_be32(pb); /* creation time */
285 get_be32(pb); /* modification time */
286 c->time_scale = get_be32(pb); /* time scale */
288 printf("time scale = %li\n", c->time_scale);
290 get_be32(pb); /* duration */
291 get_be32(pb); /* preferred scale */
293 get_be16(pb); /* preferred volume */
295 url_fskip(pb, 10); /* reserved */
297 url_fskip(pb, 36); /* display matrix */
299 get_be32(pb); /* preview time */
300 get_be32(pb); /* preview duration */
301 get_be32(pb); /* poster time */
302 get_be32(pb); /* selection time */
303 get_be32(pb); /* selection duration */
304 get_be32(pb); /* current time */
305 get_be32(pb); /* next track ID */
310 /* this atom should contain all header atoms */
311 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
316 print_atom("moov", atom_type, atom_offset, atom_size);
318 c = (MOVContext *)param;
320 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
321 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
322 /* so we don't parse the whole file if over a network */
325 return 1; /* found both, just go */
326 return 0; /* now go for mdat */
329 /* this atom contains actual media data */
330 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
334 print_atom("mdat", atom_type, atom_offset, atom_size);
336 c = (MOVContext *)param;
338 if(atom_size == 0) /* wrong one (MP4) */
341 c->mdat_offset = atom_offset;
342 c->mdat_size = atom_size;
344 return 1; /* found both, just go */
345 url_fskip(pb, atom_size);
346 return 0; /* now go for moov */
349 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
350 /* like the files created with Adobe Premiere 5.0, for samples see */
351 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
352 static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
357 print_atom("wide", atom_type, atom_offset, atom_size);
361 return 0; /* continue */
362 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
363 url_fskip(pb, atom_size - 4);
367 if (type != MKTAG('m', 'd', 'a', 't')) {
368 url_fskip(pb, atom_size - 8);
371 err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
378 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
382 MOVStreamContext *sc;
384 print_atom("trak", atom_type, atom_offset, atom_size);
387 c = (MOVContext *)param;
388 st = av_new_stream(c->fc, c->fc->nb_streams);
390 sc = av_malloc(sizeof(MOVStreamContext));
391 sc->sample_to_chunk_index = -1;
393 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
394 c->streams[c->fc->nb_streams-1] = sc;
395 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
398 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
403 print_atom("tkhd", atom_type, atom_offset, atom_size);
406 c = (MOVContext *)param;
407 st = c->fc->streams[c->fc->nb_streams-1];
409 get_byte(pb); /* version */
411 get_byte(pb); get_byte(pb);
412 get_byte(pb); /* flags */
414 MOV_TRACK_ENABLED 0x0001
415 MOV_TRACK_IN_MOVIE 0x0002
416 MOV_TRACK_IN_PREVIEW 0x0004
417 MOV_TRACK_IN_POSTER 0x0008
420 get_be32(pb); /* creation time */
421 get_be32(pb); /* modification time */
422 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
423 get_be32(pb); /* reserved */
424 get_be32(pb); /* duration */
425 get_be32(pb); /* reserved */
426 get_be32(pb); /* reserved */
428 get_be16(pb); /* layer */
429 get_be16(pb); /* alternate group */
430 get_be16(pb); /* volume */
431 get_be16(pb); /* reserved */
433 url_fskip(pb, 36); /* display matrix */
435 /* those are fixed-point */
436 st->codec.width = get_be32(pb) >> 16; /* track width */
437 st->codec.height = get_be32(pb) >> 16; /* track height */
442 static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
447 print_atom("mdhd", atom_type, atom_offset, atom_size);
450 c = (MOVContext *)param;
451 st = c->fc->streams[c->fc->nb_streams-1];
453 get_byte(pb); /* version */
455 get_byte(pb); get_byte(pb);
456 get_byte(pb); /* flags */
458 get_be32(pb); /* creation time */
459 get_be32(pb); /* modification time */
461 c->streams[c->total_streams]->time_scale = get_be32(pb);
464 printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
466 get_be32(pb); /* duration */
468 get_be16(pb); /* language */
469 get_be16(pb); /* quality */
474 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
483 print_atom("hdlr", atom_type, atom_offset, atom_size);
485 c = (MOVContext *)param;
486 st = c->fc->streams[c->fc->nb_streams-1];
488 get_byte(pb); /* version */
489 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
492 ctype = get_le32(pb);
493 type = get_le32(pb); /* component subtype */
496 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
497 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
500 /* XXX: yeah this is ugly... */
501 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
502 if(type == MKTAG('v', 'i', 'd', 'e'))
504 else if(type == MKTAG('s', 'o', 'u', 'n'))
506 } else if(ctype == 0) { /* MP4 */
507 if(type == MKTAG('v', 'i', 'd', 'e'))
509 else if(type == MKTAG('s', 'o', 'u', 'n'))
511 else if(type == MKTAG('o', 'd', 's', 'm'))
513 else if(type == MKTAG('s', 'd', 's', 'm'))
515 } else puts("hdlr: meta");
518 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
519 /* helps parsing the string hereafter... */
521 if(type == MKTAG('v', 'i', 'd', 'e'))
522 st->codec.codec_type = CODEC_TYPE_VIDEO;
523 else if(type == MKTAG('s', 'o', 'u', 'n'))
524 st->codec.codec_type = CODEC_TYPE_AUDIO;
525 } else if(ctype == 0) { /* MP4 */
526 /* helps parsing the string hereafter... */
528 if(type == MKTAG('v', 'i', 'd', 'e'))
529 st->codec.codec_type = CODEC_TYPE_VIDEO;
530 else if(type == MKTAG('s', 'o', 'u', 'n'))
531 st->codec.codec_type = CODEC_TYPE_AUDIO;
533 get_be32(pb); /* component manufacture */
534 get_be32(pb); /* component flags */
535 get_be32(pb); /* component flags mask */
538 return 0; /* nothing left to read */
539 /* XXX: MP4 uses a C string, not a pascal one */
544 while(get_byte(pb) && (++len < (atom_size - 24)));
546 /* .mov: PASCAL string */
548 buf = av_malloc(len+1);
549 get_buffer(pb, buf, len);
552 printf("**buf='%s'\n", buf);
558 /* XXX: use a better heuristic */
560 /* assume that it is a Pascal like string */
561 buf = av_malloc(len+1);
562 get_buffer(pb, buf, len);
565 printf("**buf='%s'\n", buf);
581 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
584 int entries, size, samp_sz, frames_per_sample;
588 print_atom("stsd", atom_type, atom_offset, atom_size);
590 c = (MOVContext *)param;
591 st = c->fc->streams[c->fc->nb_streams-1];
593 get_byte(pb); /* version */
594 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
597 entries = get_be32(pb);
600 size = get_be32(pb); /* size */
601 format = get_le32(pb); /* data format */
603 get_be32(pb); /* reserved */
604 get_be16(pb); /* reserved */
605 get_be16(pb); /* index */
606 /* if(format == MKTAG('m', 'p', '4', 'v')) */
607 /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
608 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
609 st->codec.codec_tag = format;
610 st->codec.codec_id = codec_get_id(mov_video_tags, format);
611 get_be16(pb); /* version */
612 get_be16(pb); /* revision level */
613 get_be32(pb); /* vendor */
614 get_be32(pb); /* temporal quality */
615 get_be32(pb); /* spacial quality */
616 st->codec.width = get_be16(pb); /* width */
617 st->codec.height = get_be16(pb); /* height */
618 get_be32(pb); /* horiz resolution */
619 get_be32(pb); /* vert resolution */
620 get_be32(pb); /* data size, always 0 */
621 frames_per_sample = get_be16(pb); /* frame per samples */
623 printf("frames/samples = %d\n", frames_per_sample);
625 url_fskip(pb, 32); /* codec name */
627 get_be16(pb); /* depth */
628 get_be16(pb); /* colortable id */
632 st->codec.sample_rate = 25 * FRAME_RATE_BASE;
635 url_fskip(pb, size-(16+24+18+32));
637 st->codec.codec_tag = format;
639 get_be16(pb); /* version */
640 get_be16(pb); /* revision level */
641 get_be32(pb); /* vendor */
643 st->codec.channels = get_be16(pb);/* channel count */
644 samp_sz = get_be16(pb); /* sample size */
647 puts("!!! stsd: audio sample size is not 16 bit !");
649 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
650 /* handle specific s8 codec */
651 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
652 st->codec.codec_id = CODEC_ID_PCM_S8;
654 get_be16(pb); /* compression id = 0*/
655 get_be16(pb); /* packet size = 0 */
657 st->codec.sample_rate = ((get_be32(pb) >> 16));
658 st->codec.bit_rate = 0;
660 /* this is for .mp4 files */
661 if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
662 st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
663 st->codec.codec_id = CODEC_ID_MPEG4;
664 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
665 st->codec.bit_rate = 100000;
667 // if(format == MKTAG('m', 'p', '4', 'a')) { /* XXX */
668 // st->codec.codec_type=CODEC_TYPE_AUDIO; /* force things */
669 // st->codec.codec_id = CODEC_ID_AAC;
670 //// st->codec.frame_rate = 25 * FRAME_RATE_BASE;
671 //// st->codec.bit_rate = 100000;
676 get_be16(pb); get_be16(pb); /* */
683 url_fskip(pb, size-(16+20));
686 printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
691 buf = av_malloc(len+1);
692 get_buffer(pb, buf, len);
701 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
706 MOVStreamContext *sc;
708 print_atom("stco", atom_type, atom_offset, atom_size);
710 c = (MOVContext *)param;
711 st = c->fc->streams[c->fc->nb_streams-1];
712 sc = (MOVStreamContext *)st->priv_data;
714 get_byte(pb); /* version */
715 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
717 entries = get_be32(pb);
718 sc->chunk_count = entries;
719 sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
720 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
721 for(i=0; i<entries; i++) {
722 sc->chunk_offsets[i] = get_be32(pb);
723 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
725 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
726 for(i=0; i<entries; i++) {
727 sc->chunk_offsets[i] = get_be64(pb);
728 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
735 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
740 MOVStreamContext *sc;
742 print_atom("stsc", atom_type, atom_offset, atom_size);
744 c = (MOVContext *)param;
745 st = c->fc->streams[c->fc->nb_streams-1];
746 sc = (MOVStreamContext *)st->priv_data;
748 get_byte(pb); /* version */
749 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
751 entries = get_be32(pb);
753 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
755 sc->sample_to_chunk_sz = entries;
756 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
757 for(i=0; i<entries; i++) {
758 sc->sample_to_chunk[i].first = get_be32(pb);
759 sc->sample_to_chunk[i].count = get_be32(pb);
760 sc->sample_to_chunk[i].id = get_be32(pb);
762 /* printf("sample_to_chunk first=%ld count=%ld, id=%ld\n", sc->sample_to_chunk[i].first, sc->sample_to_chunk[i].count, sc->sample_to_chunk[i].id); */
768 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
773 MOVStreamContext *sc;
775 print_atom("stsz", atom_type, atom_offset, atom_size);
777 c = (MOVContext *)param;
778 st = c->fc->streams[c->fc->nb_streams-1];
779 sc = (MOVStreamContext *)st->priv_data;
781 get_byte(pb); /* version */
782 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
784 sc->sample_size = get_be32(pb);
785 entries = get_be32(pb);
786 sc->sample_count = entries;
788 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
791 return 0; /* there isn't any table following */
792 sc->sample_sizes = av_malloc(entries * sizeof(long));
793 for(i=0; i<entries; i++) {
794 sc->sample_sizes[i] = get_be32(pb);
796 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
802 static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
807 MOVStreamContext *sc;
809 print_atom("stts", atom_type, atom_offset, atom_size);
811 c = (MOVContext *)param;
812 st = c->fc->streams[c->fc->nb_streams-1];
813 sc = (MOVStreamContext *)st->priv_data;
815 get_byte(pb); /* version */
816 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
817 entries = get_be32(pb);
819 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
821 for(i=0; i<entries; i++) {
825 sample_duration = get_be32(pb);
827 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
828 st->codec.frame_rate = FRAME_RATE_BASE * c->streams[c->total_streams]->time_scale;
830 st->codec.frame_rate /= sample_duration;
832 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
840 static int null_read_packet(void *opaque, UINT8 *buf, int buf_size)
845 static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
850 unsigned char *moov_data; /* uncompressed data */
851 long cmov_len, moov_len;
854 print_atom("cmov", atom_type, atom_offset, atom_size);
856 c = (MOVContext *)param;
858 get_be32(pb); /* dcom atom */
859 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
861 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
862 puts("unknown compression for cmov atom !");
865 get_be32(pb); /* cmvd atom */
866 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
868 moov_len = get_be32(pb); /* uncompressed size */
869 cmov_len = atom_size - 6 * 4;
871 cmov_data = av_malloc(cmov_len);
874 moov_data = av_malloc(moov_len);
879 get_buffer(pb, cmov_data, cmov_len);
880 if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
882 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
884 ctx.buf_end = ctx.buffer + moov_len;
885 ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
892 static const MOVParseTableEntry mov_default_parse_table[] = {
894 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
895 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
896 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
897 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
898 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
899 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
900 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
901 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
902 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
903 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
904 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
905 { MKTAG( 'e', 's', 'd', 's' ), parse_default },
906 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
907 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
908 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
909 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
910 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
911 { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
912 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
913 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
914 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
915 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
916 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
917 { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
918 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
919 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
920 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
921 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
922 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
923 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
924 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
925 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
926 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
927 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
928 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
929 { MKTAG( 's', 't', 's', 'h' ), parse_default },
930 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
931 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
932 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
933 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
934 { MKTAG( 's', 't', 't', 's' ), parse_stts },
935 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
936 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
937 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
938 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
939 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
940 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
942 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
944 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
945 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
946 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
947 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
948 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
949 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
950 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
951 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
952 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
953 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
954 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
955 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
956 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
957 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
958 { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
960 { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
962 { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
967 static void mov_free_stream_context(MOVStreamContext *sc)
970 av_free(sc->chunk_offsets);
971 av_free(sc->sample_to_chunk);
976 /* XXX: is it suffisant ? */
977 static int mov_probe(AVProbeData *p)
981 /* check file header */
982 if (p->buf_size <= 12)
984 if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
985 p->buf[6] == 'o' && p->buf[7] == 'v') ||
986 (p->buf[4] == 'w' && p->buf[5] == 'i' &&
987 p->buf[6] == 'd' && p->buf[7] == 'e') ||
988 (p->buf[4] == 'f' && p->buf[5] == 'r' &&
989 p->buf[6] == 'e' && p->buf[7] == 'e') ||
990 (p->buf[4] == 'm' && p->buf[5] == 'd' &&
991 p->buf[6] == 'a' && p->buf[7] == 't'))
992 return AVPROBE_SCORE_MAX;
993 if (p->buf[4] == 'f' && p->buf[5] == 't' &&
994 p->buf[6] == 'y' && p->buf[7] == 'p') {
995 offset = p->buf[0]; offset <<= 8;
996 offset |= p->buf[1]; offset <<= 8;
997 offset |= p->buf[2]; offset <<= 8;
999 if (offset > p->buf_size)
1001 if ((p->buf[offset+4] == 'm' && p->buf[offset+5] == 'o' &&
1002 p->buf[offset+6] == 'o' && p->buf[offset+7] == 'v') ||
1003 (p->buf[offset+4] == 'w' && p->buf[offset+5] == 'i' &&
1004 p->buf[offset+6] == 'd' && p->buf[offset+7] == 'e') ||
1005 (p->buf[offset+4] == 'm' && p->buf[offset+5] == 'd' &&
1006 p->buf[offset+6] == 'a' && p->buf[offset+7] == 't'))
1007 return AVPROBE_SCORE_MAX;
1013 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1015 MOVContext *mov = s->priv_data;
1016 ByteIOContext *pb = &s->pb;
1022 /* XXX: I think we should auto detect */
1023 if(s->iformat->name[1] == 'p')
1026 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1027 size = url_filesize(url_fileno(pb));
1029 size = 0x7FFFFFFFFFFFFFFF;
1032 printf("filesz=%Ld\n", size);
1035 /* check MOV header */
1036 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
1037 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1038 puts("header not found !!!");
1042 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1044 /* some cleanup : make sure we are on the mdat atom */
1045 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1046 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1048 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1051 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1055 printf("streams= %d\n", s->nb_streams);
1057 mov->total_streams = nb = s->nb_streams;
1060 for(i=0; i<s->nb_streams;) {
1061 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1062 av_free(s->streams[i]);
1063 for(j=i+1; j<s->nb_streams; j++)
1064 s->streams[j-1] = s->streams[j];
1069 for(i=0; i<s->nb_streams;i++) {
1070 MOVStreamContext *sc;
1071 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1073 sc->is_ff_stream = 1;
1077 printf("real streams= %d\n", s->nb_streams);
1082 /* Yes, this is ugly... I didn't write the specs of QT :p */
1083 /* XXX:remove useless commented code sometime */
1084 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1086 MOVContext *mov = s->priv_data;
1087 INT64 offset = 0x0FFFFFFFFFFFFFFF;
1089 int st_id = 0, size;
1092 #ifdef MOV_SPLIT_CHUNKS
1097 st_id = mov->partial - 1;
1098 idx = mov->streams[st_id]->sample_to_chunk_index;
1099 if (idx < 0) return 0;
1100 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1102 mov->streams[st_id]->current_sample++;
1103 mov->streams[st_id]->left_in_chunk--;
1105 if(mov->streams[st_id]->left_in_chunk <= 0)
1107 offset = mov->next_chunk_offset;
1108 /* extract the sample */
1115 for(i=0; i<mov->total_streams; i++) {
1116 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1117 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
1119 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
1122 mov->streams[st_id]->next_chunk++;
1123 if(offset==0x0FFFFFFFFFFFFFFF)
1126 if(mov->next_chunk_offset < offset) { /* some meta data */
1127 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1128 mov->next_chunk_offset = offset;
1131 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1132 if(!mov->streams[st_id]->is_ff_stream) {
1133 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1134 mov->next_chunk_offset = offset;
1135 offset = 0x0FFFFFFFFFFFFFFF;
1139 /* now get the chunk size... */
1141 for(i=0; i<mov->total_streams; i++) {
1142 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1143 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
1144 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
1147 #ifdef MOV_SPLIT_CHUNKS
1148 /* split chunks into samples */
1149 if(mov->streams[st_id]->sample_size == 0) {
1151 idx = mov->streams[st_id]->sample_to_chunk_index;
1152 if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz)
1153 && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first))
1155 mov->streams[st_id]->sample_to_chunk_index = idx;
1156 if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) {
1157 mov->partial = st_id+1;
1158 /* we'll have to get those samples before next chunk */
1159 mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1;
1160 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1163 mov->streams[st_id]->current_sample++;
1169 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1170 if(size == 0x0FFFFFFF)
1171 size = mov->mdat_size + mov->mdat_offset - offset;
1176 av_new_packet(pkt, size);
1177 pkt->stream_index = mov->streams[st_id]->ffindex;
1179 get_buffer(&s->pb, pkt->data, pkt->size);
1183 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1185 printf("%02x ", pkt->data[i]);
1187 printf("%c ", (pkt->data[i]) & 0x7F);
1192 mov->next_chunk_offset = offset + size;
1197 static int mov_read_close(AVFormatContext *s)
1200 MOVContext *mov = s->priv_data;
1201 for(i=0; i<mov->total_streams; i++)
1202 mov_free_stream_context(mov->streams[i]);
1203 for(i=0; i<s->nb_streams; i++)
1204 av_free(s->streams[i]);
1208 static AVInputFormat mov_iformat = {
1210 "QuickTime/MPEG4 format",
1220 av_register_input_format(&mov_iformat);