3 * Copyright (c) 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * First version by Francois Revol revol@free.fr
25 * Features and limitations:
26 * - reads most of the QT files I have (at least the structure),
27 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
28 * - ffmpeg has nearly none of the usual QuickTime codecs,
29 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
30 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
31 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
32 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
33 * http://mpeg.telecomitalialab.com/faq.htm
34 * - the code is quite ugly... maybe I won't do it recursive next time :-)
36 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
37 * when coding this :) (it's a writer anyway)
39 * Reference documents:
40 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
42 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
43 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
44 * QuickTime is a trademark of Apple (AFAIK :))
51 * XXX: static sux, even more in a multithreaded environment...
52 * Avoid them. This is here just to help debugging.
54 static int debug_indent = 0;
55 void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
58 tag = (unsigned int) type;
60 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
64 printf(" %s: tag=%c%c%c%c offset=%d size=0x%x\n",
74 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
75 /* so we first list them as this, then clean up the list of streams we give back, */
76 /* getting rid of these */
77 #define CODEC_TYPE_MOV_OTHER 2
79 static const CodecTag mov_video_tags[] = {
80 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
81 /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
82 { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, /* Uncompressed RGB */
83 { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, /* Uncompressed YUV422 */
88 /* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG ? */
89 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
90 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format A) */
91 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
92 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
94 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
95 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
96 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
100 static const CodecTag mov_audio_tags[] = {
101 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
102 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
103 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
104 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
105 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
106 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
107 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
108 /* { CODEC_ID_, MKTAG('i', 'm', 'a', '4') }, *//* IMA-4 */
110 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
111 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
112 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
113 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
115 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
116 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
120 /* the QuickTime file format is quite convoluted...
121 * it has lots of index tables, each indexing something in another one...
122 * Here we just use what is needed to read the chunks
125 typedef struct MOV_sample_to_chunk_tbl {
129 } MOV_sample_to_chunk_tbl;
131 typedef struct MOVStreamContext {
132 int ffindex; /* the ffmpeg stream id */
133 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
136 INT64 *chunk_offsets;
137 long sample_to_chunk_sz;
138 MOV_sample_to_chunk_tbl *sample_to_chunk;
144 typedef struct MOVContext {
145 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) */
148 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
149 int found_mdat; /* we suppose we have enough data to read the file */
153 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
154 * but we need the info to be able to skip data from those streams in the 'mdat' section
156 MOVStreamContext *streams[MAX_STREAMS];
158 INT64 next_chunk_offset;
162 struct MOVParseTableEntry;
164 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
166 /* those functions parse an atom */
168 1: found what I wanted, exit
169 0: continue to parse next atom
170 -1: error occured, exit
172 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
175 INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
176 INT64 atom_size, /* total size (excluding the size and type fields) */
179 /* links atom IDs to parse functions */
180 typedef struct MOVParseTableEntry {
182 mov_parse_function func;
183 } MOVParseTableEntry;
185 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
188 print_atom("leaf", atom_type, atom_offset, atom_size);
191 url_fskip(pb, atom_size);
192 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
197 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
201 UINT64 total_size = 0;
206 print_atom("default", atom_type, atom_offset, atom_size);
210 offset = atom_offset;
213 atom_size = 0x0FFFFFFFFFFFFFFF;
214 while((total_size < atom_size) && !url_feof(pb) && !err) {
223 // printf("type: %08lx sz: %08lx", type, size);
224 if(size == 1) { /* 64 bit extended size */
231 size = atom_size - total_size;
233 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
235 // printf(" i=%ld\n", i);
236 if (parse_table[i].type == 0) { /* skip leaf atoms data */
237 // url_seek(pb, atom_offset+atom_size, SEEK_SET);
239 print_atom("unknown", type, offset, size);
243 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
255 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
259 print_atom("mvhd", atom_type, atom_offset, atom_size);
261 c = (MOVContext *)param;
263 get_byte(pb); /* version */
264 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
266 get_be32(pb); /* creation time */
267 get_be32(pb); /* modification time */
268 c->time_scale = get_be32(pb); /* time scale */
269 get_be32(pb); /* duration */
270 get_be32(pb); /* preferred scale */
272 get_be16(pb); /* preferred volume */
274 url_fskip(pb, 10); /* reserved */
276 url_fskip(pb, 36); /* display matrix */
278 get_be32(pb); /* preview time */
279 get_be32(pb); /* preview duration */
280 get_be32(pb); /* poster time */
281 get_be32(pb); /* selection time */
282 get_be32(pb); /* selection duration */
283 get_be32(pb); /* current time */
284 get_be32(pb); /* next track ID */
289 /* this atom should contain all header atoms */
290 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
295 print_atom("moov", atom_type, atom_offset, atom_size);
297 c = (MOVContext *)param;
299 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
300 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
301 /* so we don't parse the whole file if over a network */
304 return 1; /* found both, just go */
305 return 0; /* now go for mdat */
308 /* this atom contains actual media data */
309 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
313 print_atom("mdat", atom_type, atom_offset, atom_size);
315 c = (MOVContext *)param;
317 if(atom_size == 0) /* wrong one (MP4) */
320 c->mdat_offset = atom_offset;
321 c->mdat_size = atom_size;
323 return 1; /* found both, just go */
324 url_fskip(pb, atom_size);
325 return 0; /* now go for moov */
328 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
332 MOVStreamContext *sc;
334 print_atom("trak", atom_type, atom_offset, atom_size);
337 c = (MOVContext *)param;
338 st = malloc(sizeof(AVStream));
340 memset(st, 0, sizeof(AVStream));
341 c->fc->streams[c->fc->nb_streams] = st;
342 sc = malloc(sizeof(MOVStreamContext));
344 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
345 c->streams[c->fc->nb_streams++] = sc;
346 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
349 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
354 print_atom("tkhd", atom_type, atom_offset, atom_size);
357 c = (MOVContext *)param;
358 st = c->fc->streams[c->fc->nb_streams-1];
360 get_byte(pb); /* version */
362 get_byte(pb); get_byte(pb);
363 get_byte(pb); /* flags */
365 MOV_TRACK_ENABLED 0x0001
366 MOV_TRACK_IN_MOVIE 0x0002
367 MOV_TRACK_IN_PREVIEW 0x0004
368 MOV_TRACK_IN_POSTER 0x0008
371 get_be32(pb); /* creation time */
372 get_be32(pb); /* modification time */
373 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
374 get_be32(pb); /* reserved */
375 get_be32(pb); /* duration */
376 get_be32(pb); /* reserved */
377 get_be32(pb); /* reserved */
379 get_be16(pb); /* layer */
380 get_be16(pb); /* alternate group */
381 get_be16(pb); /* volume */
382 get_be16(pb); /* reserved */
384 url_fskip(pb, 36); /* display matrix */
386 /* those are fixed-point */
387 st->codec.width = get_be32(pb) >> 16; /* track width */
388 st->codec.height = get_be32(pb) >> 16; /* track height */
393 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
402 print_atom("hdlr", atom_type, atom_offset, atom_size);
404 c = (MOVContext *)param;
405 st = c->fc->streams[c->fc->nb_streams-1];
407 get_byte(pb); /* version */
408 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
411 ctype = get_le32(pb);
412 type = get_le32(pb); /* component subtype */
415 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
416 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
419 /* XXX: yeah this is ugly... */
420 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
421 if(type == MKTAG('v', 'i', 'd', 'e'))
423 else if(type == MKTAG('s', 'o', 'u', 'n'))
425 } else if(ctype == 0) { /* MP4 */
426 if(type == MKTAG('v', 'i', 'd', 'e'))
428 else if(type == MKTAG('s', 'o', 'u', 'n'))
430 else if(type == MKTAG('o', 'd', 's', 'm'))
432 else if(type == MKTAG('s', 'd', 's', 'm'))
434 } else puts("hdlr: meta");
437 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
438 if(type == MKTAG('v', 'i', 'd', 'e'))
439 st->codec.codec_type = CODEC_TYPE_VIDEO;
440 else if(type == MKTAG('s', 'o', 'u', 'n'))
441 st->codec.codec_type = CODEC_TYPE_AUDIO;
442 } else if(ctype == 0) { /* MP4 */
443 if(type == MKTAG('v', 'i', 'd', 'e'))
444 st->codec.codec_type = CODEC_TYPE_VIDEO;
445 else if(type == MKTAG('s', 'o', 'u', 'n'))
446 st->codec.codec_type = CODEC_TYPE_AUDIO;
448 get_be32(pb); /* component manufacture */
449 get_be32(pb); /* component flags */
450 get_be32(pb); /* component flags mask */
453 return 0; /* nothing left to read */
454 /* XXX: MP4 uses a C string, not a pascal one */
460 while ((ch = get_byte(pb)));
465 get_buffer(pb, buf, len);
477 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
480 int entries, size, samp_sz, frames_per_sample;
484 print_atom("stsd", atom_type, atom_offset, atom_size);
486 c = (MOVContext *)param;
487 st = c->fc->streams[c->fc->nb_streams-1];
489 get_byte(pb); /* version */
490 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
493 entries = get_be32(pb);
496 size = get_be32(pb); /* size */
497 format = get_le32(pb); /* data format */
499 get_be32(pb); /* reserved */
500 get_be16(pb); /* reserved */
501 get_be16(pb); /* index */
502 /* if(format == MKTAG('m', 'p', '4', 'v')) */
503 /* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
504 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
505 st->codec.codec_tag = format;
506 st->codec.codec_id = codec_get_id(mov_video_tags, format);
507 get_be16(pb); /* version */
508 get_be16(pb); /* revision level */
509 get_be32(pb); /* vendor */
510 get_be32(pb); /* temporal quality */
511 get_be32(pb); /* spacial quality */
512 st->codec.width = get_be16(pb); /* width */
513 st->codec.height = get_be16(pb); /* height */
514 get_be32(pb); /* horiz resolution */
515 get_be32(pb); /* vert resolution */
516 get_be32(pb); /* data size, always 0 */
517 frames_per_sample = get_be16(pb); /* frame per samples */
519 printf("frames/samples = %d\n", frames_per_sample);
521 url_fskip(pb, 32); /* codec name */
523 get_be16(pb); /* depth */
524 get_be16(pb); /* colortable id */
528 st->codec.sample_rate = 25 * FRAME_RATE_BASE;
531 url_fskip(pb, size-(16+24+18+32));
533 st->codec.codec_tag = format;
535 get_be16(pb); /* version */
536 get_be16(pb); /* revision level */
537 get_be32(pb); /* vendor */
539 st->codec.channels = get_be16(pb);/* channel count */
540 samp_sz = get_be16(pb); /* sample size */
543 puts("!!! stsd: audio sample size is not 16 bit !");
545 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
546 /* handle specific s8 codec */
547 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
548 st->codec.codec_id = CODEC_ID_PCM_S8;
550 get_be16(pb); /* compression id = 0*/
551 get_be16(pb); /* packet size = 0 */
553 st->codec.sample_rate = ((get_be32(pb) >> 16));
554 st->codec.bit_rate = 0;
556 /* this is for .mp4 files */
557 if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
558 st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
559 st->codec.codec_id = CODEC_ID_MPEG4;
560 st->codec.frame_rate = 25;
561 st->codec.bit_rate = 100000;
566 get_be16(pb); get_be16(pb); /* */
573 url_fskip(pb, size-(16+20));
576 printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
582 get_buffer(pb, buf, len);
591 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
596 MOVStreamContext *sc;
598 print_atom("stco", atom_type, atom_offset, atom_size);
600 c = (MOVContext *)param;
601 st = c->fc->streams[c->fc->nb_streams-1];
602 sc = (MOVStreamContext *)st->priv_data;
604 get_byte(pb); /* version */
605 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
607 entries = get_be32(pb);
608 sc->chunk_count = entries;
609 sc->chunk_offsets = malloc(entries * sizeof(INT64));
610 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
611 for(i=0; i<entries; i++) {
612 sc->chunk_offsets[i] = get_be32(pb);
613 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
615 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
616 for(i=0; i<entries; i++) {
617 sc->chunk_offsets[i] = get_be64(pb);
618 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
625 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
630 MOVStreamContext *sc;
632 print_atom("stsc", atom_type, atom_offset, atom_size);
634 c = (MOVContext *)param;
635 st = c->fc->streams[c->fc->nb_streams-1];
636 sc = (MOVStreamContext *)st->priv_data;
638 get_byte(pb); /* version */
639 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
641 entries = get_be32(pb);
642 sc->sample_to_chunk_sz = entries;
643 sc->sample_to_chunk = malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
644 for(i=0; i<entries; i++) {
645 sc->sample_to_chunk[i].first = get_be32(pb);
646 sc->sample_to_chunk[i].count = get_be32(pb);
647 sc->sample_to_chunk[i].id = get_be32(pb);
649 /* 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); */
655 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
660 MOVStreamContext *sc;
662 print_atom("stsz", atom_type, atom_offset, atom_size);
664 c = (MOVContext *)param;
665 st = c->fc->streams[c->fc->nb_streams-1];
666 sc = (MOVStreamContext *)st->priv_data;
668 get_byte(pb); /* version */
669 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
671 sc->sample_size = get_be32(pb);
672 entries = get_be32(pb);
673 sc->sample_count = entries;
674 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
676 return 0; /* there isn't any table following */
677 sc->sample_sizes = malloc(entries * sizeof(long));
678 for(i=0; i<entries; i++) {
679 sc->sample_sizes[i] = get_be32(pb);
681 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
687 static const MOVParseTableEntry mov_default_parse_table[] = {
689 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
690 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
691 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
692 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
693 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
694 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
695 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
696 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
697 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
698 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
699 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
700 { MKTAG( 'e', 's', 'd', 's' ), parse_default },
701 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
702 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
703 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
704 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
705 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
706 { MKTAG( 'h', 'm', 'h', 'd' ), parse_default },
707 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
708 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
709 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
710 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
711 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
712 { MKTAG( 'm', 'd', 'h', 'd' ), parse_leaf },
713 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
714 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
715 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
716 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
717 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
718 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
719 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
720 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
721 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
722 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
723 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
724 { MKTAG( 's', 't', 's', 'h' ), parse_default },
725 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
726 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
727 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
728 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
729 { MKTAG( 's', 't', 't', 's' ), parse_leaf },
730 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
731 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
732 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
733 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
734 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
735 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
737 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
739 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
740 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
741 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
742 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
743 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
744 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
745 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
746 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
747 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
748 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
749 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
750 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
751 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
752 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
753 { MKTAG( 'w', 'i', 'd', 'e' ), parse_leaf }, /* place holder */
757 static void mov_free_stream_context(MOVStreamContext *sc)
760 if(sc->chunk_offsets)
761 free(sc->chunk_offsets);
762 if(sc->sample_to_chunk)
763 free(sc->sample_to_chunk);
768 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
771 ByteIOContext *pb = &s->pb;
775 mov = malloc(sizeof(MOVContext));
778 memset(mov, 0, sizeof(MOVContext));
782 if(s->format->name[1] == 'p')
784 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
785 size = url_filesize(url_fileno(pb));
787 size = 0x7FFFFFFFFFFFFFFF;
790 printf("filesz=%Ld\n", size);
793 /* check MOV header */
794 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
795 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
796 puts("header not found !!!");
800 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
802 /* some cleanup : make sure we are on the mdat atom */
803 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
804 url_fseek(pb, mov->mdat_offset, SEEK_SET);
806 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
809 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
813 printf("streams= %d\n", s->nb_streams);
815 mov->total_streams = nb = s->nb_streams;
818 for(i=0; i<s->nb_streams;) {
819 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
821 for(j=i+1; j<s->nb_streams; j++)
822 s->streams[j-1] = s->streams[j];
827 for(i=0; i<s->nb_streams;i++) {
828 MOVStreamContext *sc;
829 sc = (MOVStreamContext *)s->streams[i]->priv_data;
831 sc->is_ff_stream = 1;
835 printf("real streams= %d\n", s->nb_streams);
840 /* Yes, this is ugly... I didn't write the specs of QT :p */
841 /* XXX:remove useless commented code sometime */
842 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
844 MOVContext *mov = s->priv_data;
845 INT64 offset = 0x0FFFFFFFFFFFFFFF;
851 for(i=0; i<mov->total_streams; i++) {
852 /* printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
853 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
854 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
857 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
859 /* else printf("n"); */
861 mov->streams[st_id]->next_chunk++;
862 if(offset==0x0FFFFFFFFFFFFFFF)
865 if(mov->next_chunk_offset < offset) /* some meta data */
866 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
867 if(!mov->streams[st_id]->is_ff_stream) {
868 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
869 offset = 0x0FFFFFFFFFFFFFFF;
873 /* printf("\nchunk offset = %ld\n", offset); */
875 /* now get the chunk size... */
877 for(i=0; i<mov->total_streams; i++) {
878 /* printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
879 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
880 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
882 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
884 /* else printf("n"); */
886 /* printf("\nchunk size = %ld\n", size); */
887 if(size == 0x0FFFFFFF)
888 size = mov->mdat_size + mov->mdat_offset - offset;
893 av_new_packet(pkt, size);
894 pkt->stream_index = mov->streams[st_id]->ffindex;
896 get_buffer(&s->pb, pkt->data, pkt->size);
900 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
902 printf("%02x ", pkt->data[i]);
904 printf("%c ", (pkt->data[i]) & 0x7F);
909 mov->next_chunk_offset = offset + size;
914 static int mov_read_close(AVFormatContext *s)
917 MOVContext *mov = s->priv_data;
918 for(i=0; i<mov->total_streams; i++)
919 mov_free_stream_context(mov->streams[i]);
920 for(i=0; i<s->nb_streams; i++)
926 AVFormat mov_format = {
942 AVFormat mp4_format = {