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') },
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 */
110 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
111 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
115 static const CodecTag mov_audio_tags[] = {
116 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
117 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
118 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
119 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
120 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
121 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
122 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
123 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
124 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
125 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
127 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
128 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
129 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
130 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
132 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
133 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
137 /* the QuickTime file format is quite convoluted...
138 * it has lots of index tables, each indexing something in another one...
139 * Here we just use what is needed to read the chunks
142 typedef struct MOV_sample_to_chunk_tbl {
146 } MOV_sample_to_chunk_tbl;
148 typedef struct MOVStreamContext {
149 int ffindex; /* the ffmpeg stream id */
150 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
153 INT64 *chunk_offsets;
154 long sample_to_chunk_sz;
155 MOV_sample_to_chunk_tbl *sample_to_chunk;
156 long sample_to_chunk_index;
162 long left_in_chunk; /* how many samples before next chunk */
163 /* specific MPEG4 header which is added at the beginning of the stream */
165 uint8_t *header_data;
168 typedef struct MOVContext {
169 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) */
172 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
173 int found_mdat; /* we suppose we have enough data to read the file */
177 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
178 * but we need the info to be able to skip data from those streams in the 'mdat' section
180 MOVStreamContext *streams[MAX_STREAMS];
182 INT64 next_chunk_offset;
183 int partial; /* != 0 : there is still to read in the current chunk (=id of the stream + 1) */
187 struct MOVParseTableEntry;
189 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
191 /* those functions parse an atom */
193 1: found what I wanted, exit
194 0: continue to parse next atom
195 -1: error occured, exit
197 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
200 INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
201 INT64 atom_size, /* total size (excluding the size and type fields) */
204 /* links atom IDs to parse functions */
205 typedef struct MOVParseTableEntry {
207 mov_parse_function func;
208 } MOVParseTableEntry;
210 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
213 print_atom("leaf", atom_type, atom_offset, atom_size);
216 url_fskip(pb, atom_size);
217 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
222 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
226 UINT64 total_size = 0;
231 print_atom("default", atom_type, atom_offset, atom_size);
235 offset = atom_offset;
238 atom_size = 0x0FFFFFFFFFFFFFFF;
239 while((total_size < atom_size) && !url_feof(pb) && !err) {
248 // printf("type: %08lx sz: %08lx", type, size);
249 if(size == 1) { /* 64 bit extended size */
256 size = atom_size - total_size;
258 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
260 // printf(" i=%ld\n", i);
261 if (parse_table[i].type == 0) { /* skip leaf atoms data */
262 // url_seek(pb, atom_offset+atom_size, SEEK_SET);
264 print_atom("unknown", type, offset, size);
268 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
280 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
284 print_atom("mvhd", atom_type, atom_offset, atom_size);
286 c = (MOVContext *)param;
288 get_byte(pb); /* version */
289 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
291 get_be32(pb); /* creation time */
292 get_be32(pb); /* modification time */
293 c->time_scale = get_be32(pb); /* time scale */
295 printf("time scale = %li\n", c->time_scale);
297 get_be32(pb); /* duration */
298 get_be32(pb); /* preferred scale */
300 get_be16(pb); /* preferred volume */
302 url_fskip(pb, 10); /* reserved */
304 url_fskip(pb, 36); /* display matrix */
306 get_be32(pb); /* preview time */
307 get_be32(pb); /* preview duration */
308 get_be32(pb); /* poster time */
309 get_be32(pb); /* selection time */
310 get_be32(pb); /* selection duration */
311 get_be32(pb); /* current time */
312 get_be32(pb); /* next track ID */
317 /* this atom should contain all header atoms */
318 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
323 print_atom("moov", atom_type, atom_offset, atom_size);
325 c = (MOVContext *)param;
327 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
328 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
329 /* so we don't parse the whole file if over a network */
332 return 1; /* found both, just go */
333 return 0; /* now go for mdat */
336 /* this atom contains actual media data */
337 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
341 print_atom("mdat", atom_type, atom_offset, atom_size);
343 c = (MOVContext *)param;
345 if(atom_size == 0) /* wrong one (MP4) */
348 c->mdat_offset = atom_offset;
349 c->mdat_size = atom_size;
351 return 1; /* found both, just go */
352 url_fskip(pb, atom_size);
353 return 0; /* now go for moov */
356 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
357 /* like the files created with Adobe Premiere 5.0, for samples see */
358 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
359 static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
364 print_atom("wide", atom_type, atom_offset, atom_size);
368 return 0; /* continue */
369 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
370 url_fskip(pb, atom_size - 4);
374 if (type != MKTAG('m', 'd', 'a', 't')) {
375 url_fskip(pb, atom_size - 8);
378 err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
385 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
389 MOVStreamContext *sc;
391 print_atom("trak", atom_type, atom_offset, atom_size);
394 c = (MOVContext *)param;
395 st = av_new_stream(c->fc, c->fc->nb_streams);
397 sc = av_malloc(sizeof(MOVStreamContext));
398 sc->sample_to_chunk_index = -1;
400 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
401 c->streams[c->fc->nb_streams-1] = sc;
402 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
405 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
410 print_atom("tkhd", atom_type, atom_offset, atom_size);
413 c = (MOVContext *)param;
414 st = c->fc->streams[c->fc->nb_streams-1];
416 get_byte(pb); /* version */
418 get_byte(pb); get_byte(pb);
419 get_byte(pb); /* flags */
421 MOV_TRACK_ENABLED 0x0001
422 MOV_TRACK_IN_MOVIE 0x0002
423 MOV_TRACK_IN_PREVIEW 0x0004
424 MOV_TRACK_IN_POSTER 0x0008
427 get_be32(pb); /* creation time */
428 get_be32(pb); /* modification time */
429 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
430 get_be32(pb); /* reserved */
431 get_be32(pb); /* duration */
432 get_be32(pb); /* reserved */
433 get_be32(pb); /* reserved */
435 get_be16(pb); /* layer */
436 get_be16(pb); /* alternate group */
437 get_be16(pb); /* volume */
438 get_be16(pb); /* reserved */
440 url_fskip(pb, 36); /* display matrix */
442 /* those are fixed-point */
443 st->codec.width = get_be32(pb) >> 16; /* track width */
444 st->codec.height = get_be32(pb) >> 16; /* track height */
449 static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
454 print_atom("mdhd", atom_type, atom_offset, atom_size);
457 c = (MOVContext *)param;
458 st = c->fc->streams[c->fc->nb_streams-1];
460 get_byte(pb); /* version */
462 get_byte(pb); get_byte(pb);
463 get_byte(pb); /* flags */
465 get_be32(pb); /* creation time */
466 get_be32(pb); /* modification time */
468 c->streams[c->total_streams]->time_scale = get_be32(pb);
471 printf("track[%i].time_scale = %li\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
473 get_be32(pb); /* duration */
475 get_be16(pb); /* language */
476 get_be16(pb); /* quality */
481 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
490 print_atom("hdlr", atom_type, atom_offset, atom_size);
492 c = (MOVContext *)param;
493 st = c->fc->streams[c->fc->nb_streams-1];
495 get_byte(pb); /* version */
496 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
499 ctype = get_le32(pb);
500 type = get_le32(pb); /* component subtype */
503 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
504 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
507 /* XXX: yeah this is ugly... */
508 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
509 if(type == MKTAG('v', 'i', 'd', 'e'))
511 else if(type == MKTAG('s', 'o', 'u', 'n'))
513 } else if(ctype == 0) { /* MP4 */
514 if(type == MKTAG('v', 'i', 'd', 'e'))
516 else if(type == MKTAG('s', 'o', 'u', 'n'))
518 else if(type == MKTAG('o', 'd', 's', 'm'))
520 else if(type == MKTAG('s', 'd', 's', 'm'))
522 } else puts("hdlr: meta");
525 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
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;
532 } else if(ctype == 0) { /* MP4 */
533 /* helps parsing the string hereafter... */
535 if(type == MKTAG('v', 'i', 'd', 'e'))
536 st->codec.codec_type = CODEC_TYPE_VIDEO;
537 else if(type == MKTAG('s', 'o', 'u', 'n'))
538 st->codec.codec_type = CODEC_TYPE_AUDIO;
540 get_be32(pb); /* component manufacture */
541 get_be32(pb); /* component flags */
542 get_be32(pb); /* component flags mask */
545 return 0; /* nothing left to read */
546 /* XXX: MP4 uses a C string, not a pascal one */
551 while(get_byte(pb) && (++len < (atom_size - 24)));
553 /* .mov: PASCAL string */
555 buf = av_malloc(len+1);
556 get_buffer(pb, buf, len);
559 printf("**buf='%s'\n", buf);
565 /* XXX: use a better heuristic */
567 /* assume that it is a Pascal like string */
568 buf = av_malloc(len+1);
569 get_buffer(pb, buf, len);
572 printf("**buf='%s'\n", buf);
588 static int mp4_read_descr_len(ByteIOContext *pb)
596 len = (len << 7) | (c & 0x7f);
605 static int mp4_read_descr(ByteIOContext *pb, int *tag)
609 len = mp4_read_descr_len(pb);
611 printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
616 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
619 int entries, size, samp_sz, frames_per_sample, id;
622 MOVStreamContext *sc;
624 print_atom("stsd", atom_type, atom_offset, atom_size);
626 c = (MOVContext *)param;
627 st = c->fc->streams[c->fc->nb_streams-1];
628 sc = (MOVStreamContext *)st->priv_data;
630 get_byte(pb); /* version */
631 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
633 entries = get_be32(pb);
636 size = get_be32(pb); /* size */
637 format = get_le32(pb); /* data format */
639 get_be32(pb); /* reserved */
640 get_be16(pb); /* reserved */
641 get_be16(pb); /* index */
643 /* for MPEG4: set codec type by looking for it */
644 id = codec_get_id(mov_video_tags, format);
647 codec = avcodec_find_decoder(id);
649 st->codec.codec_type = codec->type;
652 printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
654 (format >> 0) & 0xff,
655 (format >> 8) & 0xff,
656 (format >> 16) & 0xff,
657 (format >> 24) & 0xff,
658 st->codec.codec_type);
660 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
661 st->codec.codec_tag = format;
662 st->codec.codec_id = codec_get_id(mov_video_tags, format);
663 get_be16(pb); /* version */
664 get_be16(pb); /* revision level */
665 get_be32(pb); /* vendor */
666 get_be32(pb); /* temporal quality */
667 get_be32(pb); /* spacial quality */
668 st->codec.width = get_be16(pb); /* width */
669 st->codec.height = get_be16(pb); /* height */
671 if (st->codec.codec_id == CODEC_ID_MPEG4) {
672 /* in some MPEG4 the width/height are not correct, so
673 we ignore this info */
675 st->codec.height = 0;
678 get_be32(pb); /* horiz resolution */
679 get_be32(pb); /* vert resolution */
680 get_be32(pb); /* data size, always 0 */
681 frames_per_sample = get_be16(pb); /* frame per samples */
683 printf("frames/samples = %d\n", frames_per_sample);
685 url_fskip(pb, 32); /* codec name */
687 get_be16(pb); /* depth */
688 get_be16(pb); /* colortable id */
690 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
692 size -= (16+8*4+2+32+2*2);
694 int atom_size, atom_type;
697 atom_size = get_be32(pb);
698 atom_type = get_le32(pb);
701 printf("VIDEO: atom_type=%c%c%c%c atom_size=%d size_left=%d\n",
702 (atom_type >> 0) & 0xff,
703 (atom_type >> 8) & 0xff,
704 (atom_type >> 16) & 0xff,
705 (atom_type >> 24) & 0xff,
708 start_pos = url_ftell(pb);
711 case MKTAG('e', 's', 'd', 's'):
714 /* Well, broken but suffisant for some MP4 streams */
715 get_be32(pb); /* version + flags */
716 len = mp4_read_descr(pb, &tag);
719 get_be16(pb); /* ID */
720 get_byte(pb); /* priority */
721 len = mp4_read_descr(pb, &tag);
724 /* MP4DecConfigDescrTag */
725 get_byte(pb); /* objectTypeId */
726 get_be32(pb); /* streamType + buffer size */
727 get_be32(pb); /* max bit rate */
728 get_be32(pb); /* avg bit rate */
729 len = mp4_read_descr(pb, &tag);
732 /* MP4DecSpecificDescrTag */
734 printf("Specific MPEG4 header len=%d\n", len);
736 sc->header_data = av_mallocz(len);
737 if (sc->header_data) {
738 get_buffer(pb, sc->header_data, len);
739 sc->header_len = len;
742 /* in any case, skip garbage */
749 url_fskip(pb, (atom_size - 8) -
750 ((url_ftell(pb) - start_pos)));
751 size -= atom_size - 8;
754 /* unknown extension */
758 st->codec.codec_tag = format;
760 get_be16(pb); /* version */
761 get_be16(pb); /* revision level */
762 get_be32(pb); /* vendor */
764 st->codec.channels = get_be16(pb);/* channel count */
765 samp_sz = get_be16(pb); /* sample size */
768 puts("!!! stsd: audio sample size is not 16 bit !");
770 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
771 /* handle specific s8 codec */
772 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
773 st->codec.codec_id = CODEC_ID_PCM_S8;
775 get_be16(pb); /* compression id = 0*/
776 get_be16(pb); /* packet size = 0 */
778 st->codec.sample_rate = ((get_be32(pb) >> 16));
779 st->codec.bit_rate = 0;
782 get_be16(pb); get_be16(pb); /* */
789 url_fskip(pb, size-(16+20));
794 buf = av_malloc(len+1);
795 get_buffer(pb, buf, len);
804 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
809 MOVStreamContext *sc;
811 print_atom("stco", atom_type, atom_offset, atom_size);
813 c = (MOVContext *)param;
814 st = c->fc->streams[c->fc->nb_streams-1];
815 sc = (MOVStreamContext *)st->priv_data;
817 get_byte(pb); /* version */
818 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
820 entries = get_be32(pb);
821 sc->chunk_count = entries;
822 sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
823 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
824 for(i=0; i<entries; i++) {
825 sc->chunk_offsets[i] = get_be32(pb);
827 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
828 for(i=0; i<entries; i++) {
829 sc->chunk_offsets[i] = get_be64(pb);
835 for(i=0; i<entries; i++) {
836 printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
843 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
848 MOVStreamContext *sc;
850 print_atom("stsc", atom_type, atom_offset, atom_size);
852 c = (MOVContext *)param;
853 st = c->fc->streams[c->fc->nb_streams-1];
854 sc = (MOVStreamContext *)st->priv_data;
856 get_byte(pb); /* version */
857 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
859 entries = get_be32(pb);
861 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
863 sc->sample_to_chunk_sz = entries;
864 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
865 for(i=0; i<entries; i++) {
866 sc->sample_to_chunk[i].first = get_be32(pb);
867 sc->sample_to_chunk[i].count = get_be32(pb);
868 sc->sample_to_chunk[i].id = get_be32(pb);
870 /* 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); */
876 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
881 MOVStreamContext *sc;
883 print_atom("stsz", atom_type, atom_offset, atom_size);
885 c = (MOVContext *)param;
886 st = c->fc->streams[c->fc->nb_streams-1];
887 sc = (MOVStreamContext *)st->priv_data;
889 get_byte(pb); /* version */
890 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
892 sc->sample_size = get_be32(pb);
893 entries = get_be32(pb);
894 sc->sample_count = entries;
896 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
899 return 0; /* there isn't any table following */
900 sc->sample_sizes = av_malloc(entries * sizeof(long));
901 for(i=0; i<entries; i++) {
902 sc->sample_sizes[i] = get_be32(pb);
904 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
910 static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
915 MOVStreamContext *sc;
917 print_atom("stts", atom_type, atom_offset, atom_size);
919 c = (MOVContext *)param;
920 st = c->fc->streams[c->fc->nb_streams-1];
921 sc = (MOVStreamContext *)st->priv_data;
923 get_byte(pb); /* version */
924 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
925 entries = get_be32(pb);
927 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
929 for(i=0; i<entries; i++) {
933 sample_duration = get_be32(pb);
935 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
936 st->codec.frame_rate = FRAME_RATE_BASE * c->streams[c->total_streams]->time_scale;
938 st->codec.frame_rate /= sample_duration;
940 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
948 static int null_read_packet(void *opaque, UINT8 *buf, int buf_size)
953 static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
958 unsigned char *moov_data; /* uncompressed data */
959 long cmov_len, moov_len;
962 print_atom("cmov", atom_type, atom_offset, atom_size);
964 c = (MOVContext *)param;
966 get_be32(pb); /* dcom atom */
967 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
969 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
970 puts("unknown compression for cmov atom !");
973 get_be32(pb); /* cmvd atom */
974 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
976 moov_len = get_be32(pb); /* uncompressed size */
977 cmov_len = atom_size - 6 * 4;
979 cmov_data = av_malloc(cmov_len);
982 moov_data = av_malloc(moov_len);
987 get_buffer(pb, cmov_data, cmov_len);
988 if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
990 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
992 ctx.buf_end = ctx.buffer + moov_len;
993 ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
1000 static const MOVParseTableEntry mov_default_parse_table[] = {
1002 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
1003 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
1004 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
1005 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
1006 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
1007 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
1008 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
1009 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
1010 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
1011 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
1012 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
1013 { MKTAG( 'e', 's', 'd', 's' ), parse_default },
1014 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
1015 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
1016 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
1017 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
1018 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
1019 { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
1020 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
1021 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
1022 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
1023 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
1024 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
1025 { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
1026 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
1027 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
1028 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
1029 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
1030 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
1031 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
1032 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
1033 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
1034 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
1035 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
1036 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
1037 { MKTAG( 's', 't', 's', 'h' ), parse_default },
1038 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
1039 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
1040 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
1041 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
1042 { MKTAG( 's', 't', 't', 's' ), parse_stts },
1043 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
1044 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
1045 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
1046 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
1047 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
1048 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
1050 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
1052 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
1053 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
1054 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
1055 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
1056 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
1057 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
1058 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
1059 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
1060 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
1061 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
1062 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
1063 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
1064 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
1065 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
1066 { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
1068 { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
1070 { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
1075 static void mov_free_stream_context(MOVStreamContext *sc)
1078 av_free(sc->chunk_offsets);
1079 av_free(sc->sample_to_chunk);
1080 av_free(sc->header_data);
1085 static uint32_t to_tag(uint8_t *buf)
1087 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
1090 static uint32_t to_be32(uint8_t *buf)
1092 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1095 /* XXX: is it suffisant ? */
1096 static int mov_probe(AVProbeData *p)
1098 unsigned int offset;
1101 /* check file header */
1102 if (p->buf_size <= 12)
1106 /* ignore invalid offset */
1107 if ((offset + 8) > (unsigned int)p->buf_size)
1109 tag = to_tag(p->buf + offset + 4);
1111 case MKTAG( 'm', 'o', 'o', 'v' ):
1112 case MKTAG( 'w', 'i', 'd', 'e' ):
1113 case MKTAG( 'f', 'r', 'e', 'e' ):
1114 case MKTAG( 'm', 'd', 'a', 't'):
1115 return AVPROBE_SCORE_MAX;
1116 case MKTAG( 'f', 't', 'y', 'p' ):
1117 case MKTAG( 's', 'k', 'i', 'p' ):
1118 offset = to_be32(p->buf) + offset;
1121 /* unrecognized tag */
1128 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1130 MOVContext *mov = s->priv_data;
1131 ByteIOContext *pb = &s->pb;
1137 /* XXX: I think we should auto detect */
1138 if(s->iformat->name[1] == 'p')
1141 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1142 size = url_filesize(url_fileno(pb));
1144 size = 0x7FFFFFFFFFFFFFFF;
1147 printf("filesz=%Ld\n", size);
1150 /* check MOV header */
1151 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
1152 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1153 puts("header not found !!!");
1157 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1159 /* some cleanup : make sure we are on the mdat atom */
1160 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1161 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1163 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1166 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1170 printf("streams= %d\n", s->nb_streams);
1172 mov->total_streams = nb = s->nb_streams;
1175 for(i=0; i<s->nb_streams;) {
1176 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1177 av_free(s->streams[i]);
1178 for(j=i+1; j<s->nb_streams; j++)
1179 s->streams[j-1] = s->streams[j];
1184 for(i=0; i<s->nb_streams;i++) {
1185 MOVStreamContext *sc;
1186 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1188 sc->is_ff_stream = 1;
1192 printf("real streams= %d\n", s->nb_streams);
1197 /* Yes, this is ugly... I didn't write the specs of QT :p */
1198 /* XXX:remove useless commented code sometime */
1199 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1201 MOVContext *mov = s->priv_data;
1202 MOVStreamContext *sc;
1203 INT64 offset = 0x0FFFFFFFFFFFFFFF;
1205 int st_id = 0, size;
1208 #ifdef MOV_SPLIT_CHUNKS
1213 st_id = mov->partial - 1;
1214 idx = mov->streams[st_id]->sample_to_chunk_index;
1215 if (idx < 0) return 0;
1216 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1218 mov->streams[st_id]->current_sample++;
1219 mov->streams[st_id]->left_in_chunk--;
1221 if(mov->streams[st_id]->left_in_chunk <= 0)
1223 offset = mov->next_chunk_offset;
1224 /* extract the sample */
1231 for(i=0; i<mov->total_streams; i++) {
1232 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1233 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
1235 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
1238 mov->streams[st_id]->next_chunk++;
1239 if(offset==0x0FFFFFFFFFFFFFFF)
1242 if(mov->next_chunk_offset < offset) { /* some meta data */
1243 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1244 mov->next_chunk_offset = offset;
1247 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1248 if(!mov->streams[st_id]->is_ff_stream) {
1249 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1250 mov->next_chunk_offset = offset;
1251 offset = 0x0FFFFFFFFFFFFFFF;
1255 /* now get the chunk size... */
1257 for(i=0; i<mov->total_streams; i++) {
1258 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
1259 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
1260 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
1263 #ifdef MOV_SPLIT_CHUNKS
1264 /* split chunks into samples */
1265 if(mov->streams[st_id]->sample_size == 0) {
1267 idx = mov->streams[st_id]->sample_to_chunk_index;
1268 if ((idx + 1 < mov->streams[st_id]->sample_to_chunk_sz)
1269 && (mov->streams[st_id]->next_chunk >= mov->streams[st_id]->sample_to_chunk[idx + 1].first))
1271 mov->streams[st_id]->sample_to_chunk_index = idx;
1272 if(idx >= 0 && mov->streams[st_id]->sample_to_chunk[idx].count != 1) {
1273 mov->partial = st_id+1;
1274 /* we'll have to get those samples before next chunk */
1275 mov->streams[st_id]->left_in_chunk = (mov->streams[st_id]->sample_to_chunk[idx].count) - 1;
1276 size = mov->streams[st_id]->sample_sizes[mov->streams[st_id]->current_sample];
1279 mov->streams[st_id]->current_sample++;
1284 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1285 if(size == 0x0FFFFFFF)
1286 size = mov->mdat_size + mov->mdat_offset - offset;
1291 url_fseek(&s->pb, offset, SEEK_SET);
1292 sc = mov->streams[st_id];
1293 if (sc->header_len > 0) {
1294 av_new_packet(pkt, size + sc->header_len);
1295 memcpy(pkt->data, sc->header_data, sc->header_len);
1296 get_buffer(&s->pb, pkt->data + sc->header_len, size);
1298 av_freep(&sc->header_data);
1301 av_new_packet(pkt, size);
1302 get_buffer(&s->pb, pkt->data, pkt->size);
1304 pkt->stream_index = sc->ffindex;
1308 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1310 printf("%02x ", pkt->data[i]);
1312 printf("%c ", (pkt->data[i]) & 0x7F);
1317 mov->next_chunk_offset = offset + size;
1322 static int mov_read_close(AVFormatContext *s)
1325 MOVContext *mov = s->priv_data;
1326 for(i=0; i<mov->total_streams; i++)
1327 mov_free_stream_context(mov->streams[i]);
1328 for(i=0; i<s->nb_streams; i++)
1329 av_freep(&s->streams[i]);
1333 static AVInputFormat mov_iformat = {
1335 "QuickTime/MPEG4 format",
1345 av_register_input_format(&mov_iformat);