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 :))
58 /* allows chunk splitting - should work now... */
59 /* in case you can't read a file, try commenting */
60 #define MOV_SPLIT_CHUNKS
62 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
63 /* so we first list them as this, then clean up the list of streams we give back, */
64 /* getting rid of these */
65 #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
67 static const CodecTag mov_video_tags[] = {
68 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
69 /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
70 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
71 /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
76 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
77 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
78 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
79 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
80 { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
81 /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
82 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
84 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
85 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
86 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
87 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
88 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
89 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
90 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
91 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
92 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
93 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
94 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
95 /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
96 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
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, MKTAG('r', 'a', 'w', ' ') }, /* 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_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
109 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
110 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
112 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
113 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
114 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
115 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
117 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG 4 AAC or audio ? */
118 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
119 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
120 { CODEC_ID_NONE, 0 },
123 /* the QuickTime file format is quite convoluted...
124 * it has lots of index tables, each indexing something in another one...
125 * Here we just use what is needed to read the chunks
128 typedef struct MOV_sample_to_chunk_tbl {
132 } MOV_sample_to_chunk_tbl;
137 int64_t size; /* total size (excluding the size and type fields) */
149 uint32_t flags; // 24bit
151 /* 0x03 ESDescrTag */
153 #define MP4ODescrTag 0x01
154 #define MP4IODescrTag 0x02
155 #define MP4ESDescrTag 0x03
156 #define MP4DecConfigDescrTag 0x04
157 #define MP4DecSpecificDescrTag 0x05
158 #define MP4SLConfigDescrTag 0x06
159 #define MP4ContentIdDescrTag 0x07
160 #define MP4SupplContentIdDescrTag 0x08
161 #define MP4IPIPtrDescrTag 0x09
162 #define MP4IPMPPtrDescrTag 0x0A
163 #define MP4IPMPDescrTag 0x0B
164 #define MP4RegistrationDescrTag 0x0D
165 #define MP4ESIDIncDescrTag 0x0E
166 #define MP4ESIDRefDescrTag 0x0F
167 #define MP4FileIODescrTag 0x10
168 #define MP4FileODescrTag 0x11
169 #define MP4ExtProfileLevelDescrTag 0x13
170 #define MP4ExtDescrTagsStart 0x80
171 #define MP4ExtDescrTagsEnd 0xFE
172 uint8_t stream_priority;
174 /* 0x04 DecConfigDescrTag */
175 uint8_t object_type_id;
177 /* XXX: really streamType is
178 * only 6bit, followed by:
182 uint32_t buffer_size_db; // 24
183 uint32_t max_bitrate;
184 uint32_t avg_bitrate;
186 /* 0x05 DecSpecificDescrTag */
187 uint8_t decoder_cfg_len;
188 uint8_t *decoder_cfg;
190 /* 0x06 SLConfigDescrTag */
191 uint8_t sl_config_len;
195 struct MOVParseTableEntry;
197 typedef struct MOVStreamContext {
198 int ffindex; /* the ffmpeg stream id */
199 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
202 int64_t *chunk_offsets;
203 long sample_to_chunk_sz;
204 MOV_sample_to_chunk_tbl *sample_to_chunk;
205 long sample_to_chunk_index;
211 long left_in_chunk; /* how many samples before next chunk */
212 /* specific MPEG4 header which is added at the beginning of the stream */
214 uint8_t *header_data;
218 typedef struct MOVContext {
219 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) */
222 int duration; /* duration of the longest track */
223 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
224 int found_mdat; /* we suppose we have enough data to read the file */
228 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
229 * but we need the info to be able to skip data from those streams in the 'mdat' section
231 MOVStreamContext *streams[MAX_STREAMS];
233 int64_t next_chunk_offset;
234 MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
236 MOV_ctab_t **ctab; /* color tables */
237 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
238 /* NOTE: for recursion save to/ restore from local variable! */
242 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
244 /* those functions parse an atom */
246 1: found what I wanted, exit
247 0: continue to parse next atom
248 -1: error occured, exit
250 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
252 /* links atom IDs to parse functions */
253 typedef struct MOVParseTableEntry {
255 mov_parse_function func;
256 } MOVParseTableEntry;
260 * XXX: static sux, even more in a multithreaded environment...
261 * Avoid them. This is here just to help debugging.
263 static int debug_indent = 0;
264 void print_atom(const char *str, MOV_atom_t atom)
267 tag = (unsigned int) atom.type;
269 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
273 printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
278 (unsigned int)atom.offset,
279 (unsigned int)atom.size);
280 assert((unsigned int)atom.size < 0x7fffffff);// catching errors
283 #define print_atom(a,b)
287 static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
289 print_atom("leaf", atom);
292 url_fskip(pb, atom.size);
293 /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
297 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
299 int64_t total_size = 0;
305 print_atom("default", atom);
309 a.offset = atom.offset;
312 atom.size = 0x7fffffffffffffffLL;
313 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
317 a.size = get_be32(pb);
318 a.type = get_le32(pb);
322 //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
323 if (a.size == 1) { /* 64 bit extended size */
324 a.size = get_be64(pb) - 8;
329 a.size = atom.size - total_size;
333 for (i = 0; c->parse_table[i].type != 0L
334 && c->parse_table[i].type != a.type; i++)
338 // printf(" i=%ld\n", i);
339 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
340 // url_seek(pb, atom.offset+atom.size, SEEK_SET);
342 print_atom("unknown", a);
344 url_fskip(pb, a.size);
347 //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
348 //print_atom(b, type, offset, size);
350 err = (c->parse_table[i].func)(c, pb, a);
354 total_size += a.size;
357 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
358 //printf("RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
359 url_fskip(pb, atom.size - total_size);
368 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
372 //url_fskip(pb, atom.size); // for now
373 c->ctab = av_realloc(c->ctab, ++c->ctab_size);
374 t = c->ctab[c->ctab_size];
375 t->seed = get_be32(pb);
376 t->flags = get_be16(pb);
377 t->size = get_be16(pb) + 1;
378 len = 2 * t->size * 4;
380 t->clrs = av_malloc(len); // 16bit A R G B
382 get_buffer(pb, t->clrs, len);
388 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
390 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
395 print_atom("hdlr", atom);
397 get_byte(pb); /* version */
398 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
401 ctype = get_le32(pb);
402 type = get_le32(pb); /* component subtype */
405 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
406 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
409 /* XXX: yeah this is ugly... */
410 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
411 if(type == MKTAG('v', 'i', 'd', 'e'))
413 else if(type == MKTAG('s', 'o', 'u', 'n'))
415 } else if(ctype == 0) { /* MP4 */
416 if(type == MKTAG('v', 'i', 'd', 'e'))
418 else if(type == MKTAG('s', 'o', 'u', 'n'))
420 else if(type == MKTAG('o', 'd', 's', 'm'))
422 else if(type == MKTAG('s', 'd', 's', 'm'))
424 } else puts("hdlr: meta");
427 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
428 /* helps parsing the string hereafter... */
430 if(type == MKTAG('v', 'i', 'd', 'e'))
431 st->codec.codec_type = CODEC_TYPE_VIDEO;
432 else if(type == MKTAG('s', 'o', 'u', 'n'))
433 st->codec.codec_type = CODEC_TYPE_AUDIO;
434 } else if(ctype == 0) { /* MP4 */
435 /* helps parsing the string hereafter... */
437 if(type == MKTAG('v', 'i', 'd', 'e'))
438 st->codec.codec_type = CODEC_TYPE_VIDEO;
439 else if(type == MKTAG('s', 'o', 'u', 'n'))
440 st->codec.codec_type = CODEC_TYPE_AUDIO;
442 get_be32(pb); /* component manufacture */
443 get_be32(pb); /* component flags */
444 get_be32(pb); /* component flags mask */
447 return 0; /* nothing left to read */
448 /* XXX: MP4 uses a C string, not a pascal one */
453 while(get_byte(pb) && (++len < (atom.size - 24)));
455 /* .mov: PASCAL string */
461 buf = (uint8_t*) av_malloc(len+1);
463 get_buffer(pb, buf, len);
465 printf("**buf='%s'\n", buf);
475 static int mov_mp4_read_descr_len(ByteIOContext *pb)
480 int c = get_byte(pb);
481 len = (len << 7) | (c & 0x7f);
488 static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
492 len = mov_mp4_read_descr_len(pb);
494 printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
499 static inline unsigned int get_be24(ByteIOContext *s)
502 val = get_byte(s) << 16;
503 val |= get_byte(s) << 8;
508 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
510 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
511 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
512 int64_t start_pos = url_ftell(pb);
515 print_atom("esds", atom);
517 /* Well, broken but suffisant for some MP4 streams */
518 get_be32(pb); /* version + flags */
519 len = mov_mp4_read_descr(pb, &tag);
520 if (tag == MP4ESDescrTag) {
521 get_be16(pb); /* ID */
522 get_byte(pb); /* priority */
524 get_be16(pb); /* ID */
526 len = mov_mp4_read_descr(pb, &tag);
527 if (tag == MP4DecConfigDescrTag) {
528 sc->esds.object_type_id = get_byte(pb);
529 sc->esds.stream_type = get_byte(pb);
530 sc->esds.buffer_size_db = get_be24(pb);
531 sc->esds.max_bitrate = get_be32(pb);
532 sc->esds.avg_bitrate = get_be32(pb);
534 len = mov_mp4_read_descr(pb, &tag);
535 //printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
536 if (tag == MP4DecSpecificDescrTag) {
538 printf("Specific MPEG4 header len=%d\n", len);
540 st->codec.extradata = (uint8_t*) av_mallocz(len);
541 if (st->codec.extradata) {
542 get_buffer(pb, st->codec.extradata, len);
543 st->codec.extradata_size = len;
547 /* in any case, skip garbage */
548 url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
552 /* this atom contains actual media data */
553 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
555 print_atom("mdat", atom);
557 if(atom.size == 0) /* wrong one (MP4) */
560 c->mdat_offset = atom.offset;
561 c->mdat_size = atom.size;
563 return 1; /* found both, just go */
564 url_fskip(pb, atom.size);
565 return 0; /* now go for moov */
568 /* this atom should contain all header atoms */
569 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
573 print_atom("moov", atom);
575 err = mov_read_default(c, pb, atom);
576 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
577 /* so we don't parse the whole file if over a network */
580 return 1; /* found both, just go */
581 return 0; /* now go for mdat */
585 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
587 print_atom("mdhd", atom);
589 get_byte(pb); /* version */
591 get_byte(pb); get_byte(pb);
592 get_byte(pb); /* flags */
594 get_be32(pb); /* creation time */
595 get_be32(pb); /* modification time */
597 c->streams[c->total_streams]->time_scale = get_be32(pb);
600 printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
602 get_be32(pb); /* duration */
604 get_be16(pb); /* language */
605 get_be16(pb); /* quality */
610 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
612 print_atom("mvhd", atom);
614 get_byte(pb); /* version */
615 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
617 get_be32(pb); /* creation time */
618 get_be32(pb); /* modification time */
619 c->time_scale = get_be32(pb); /* time scale */
621 printf("time scale = %i\n", c->time_scale);
623 c->duration = get_be32(pb); /* duration */
624 get_be32(pb); /* preferred scale */
626 get_be16(pb); /* preferred volume */
628 url_fskip(pb, 10); /* reserved */
630 url_fskip(pb, 36); /* display matrix */
632 get_be32(pb); /* preview time */
633 get_be32(pb); /* preview duration */
634 get_be32(pb); /* poster time */
635 get_be32(pb); /* selection time */
636 get_be32(pb); /* selection duration */
637 get_be32(pb); /* current time */
638 get_be32(pb); /* next track ID */
643 static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
645 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
647 // currently SVQ3 decoder expect full STSD header - so let's fake it
648 // this should be fixed and just SMI header should be passed
649 av_free(st->codec.extradata);
650 st->codec.extradata_size = 0x5a + atom.size;
651 st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size);
653 if (st->codec.extradata) {
655 strcpy(st->codec.extradata, "SVQ3"); // fake
656 get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
657 //printf("Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
659 url_fskip(pb, atom.size);
664 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
666 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
667 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
670 print_atom("stco", atom);
672 get_byte(pb); /* version */
673 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
675 entries = get_be32(pb);
676 sc->chunk_count = entries;
677 sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
678 if (!sc->chunk_offsets)
680 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
681 for(i=0; i<entries; i++) {
682 sc->chunk_offsets[i] = get_be32(pb);
684 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
685 for(i=0; i<entries; i++) {
686 sc->chunk_offsets[i] = get_be64(pb);
692 for(i=0; i<entries; i++) {
693 printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
700 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
702 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
703 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
704 int entries, frames_per_sample;
707 print_atom("stsd", atom);
709 get_byte(pb); /* version */
710 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
712 entries = get_be32(pb);
714 while(entries--) { //Parsing Sample description table
716 int size = get_be32(pb); /* size */
717 format = get_le32(pb); /* data format */
719 get_be32(pb); /* reserved */
720 get_be16(pb); /* reserved */
721 get_be16(pb); /* index */
723 /* for MPEG4: set codec type by looking for it */
724 id = codec_get_id(mov_video_tags, format);
727 codec = avcodec_find_decoder(id);
729 st->codec.codec_type = codec->type;
732 printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
734 (format >> 0) & 0xff,
735 (format >> 8) & 0xff,
736 (format >> 16) & 0xff,
737 (format >> 24) & 0xff,
738 st->codec.codec_type);
740 st->codec.codec_tag = format;
741 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
742 MOV_atom_t a = { 0, 0, 0 };
743 st->codec.codec_id = id;
744 get_be16(pb); /* version */
745 get_be16(pb); /* revision level */
746 get_be32(pb); /* vendor */
747 get_be32(pb); /* temporal quality */
748 get_be32(pb); /* spacial quality */
749 st->codec.width = get_be16(pb); /* width */
750 st->codec.height = get_be16(pb); /* height */
752 if (st->codec.codec_id == CODEC_ID_MPEG4) {
753 /* in some MPEG4 the width/height are not correct, so
754 we ignore this info */
756 st->codec.height = 0;
759 get_be32(pb); /* horiz resolution */
760 get_be32(pb); /* vert resolution */
761 get_be32(pb); /* data size, always 0 */
762 frames_per_sample = get_be16(pb); /* frames per samples */
764 printf("frames/samples = %d\n", frames_per_sample);
766 get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
768 st->codec.bits_per_sample = get_be16(pb); /* depth */
769 st->codec.color_table_id = get_be16(pb); /* colortable id */
771 st->codec.frame_rate = 25;
772 st->codec.frame_rate_base = 1;
774 size -= (16+8*4+2+32+2*2);
780 a.size = get_be32(pb);
781 a.type = get_le32(pb);
784 printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
785 (a.type >> 0) & 0xff,
786 (a.type >> 8) & 0xff,
787 (a.type >> 16) & 0xff,
788 (a.type >> 24) & 0xff,
791 start_pos = url_ftell(pb);
794 case MKTAG('e', 's', 'd', 's'):
797 /* Well, broken but suffisant for some MP4 streams */
798 get_be32(pb); /* version + flags */
799 len = mov_mp4_read_descr(pb, &tag);
802 get_be16(pb); /* ID */
803 get_byte(pb); /* priority */
804 len = mov_mp4_read_descr(pb, &tag);
807 /* MP4DecConfigDescrTag */
808 get_byte(pb); /* objectTypeId */
809 get_be32(pb); /* streamType + buffer size */
810 get_be32(pb); /* max bit rate */
811 get_be32(pb); /* avg bit rate */
812 len = mov_mp4_read_descr(pb, &tag);
815 /* MP4DecSpecificDescrTag */
817 printf("Specific MPEG4 header len=%d\n", len);
819 sc->header_data = av_mallocz(len);
820 if (sc->header_data) {
821 get_buffer(pb, sc->header_data, len);
822 sc->header_len = len;
825 /* in any case, skip garbage */
832 printf("ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
834 url_fskip(pb, (atom.size - 8) -
835 ((url_ftell(pb) - start_pos)));
836 size -= atom.size - 8;
840 /* unknown extension */
845 mov_read_default(c, pb, a);
848 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
849 if(st->codec.codec_id==CODEC_ID_AMR_NB) //from TS26.244
852 printf("AMR-NB audio identified!!\n");
854 get_be32(pb);get_be32(pb); //Reserved_8
855 get_be16(pb);//Reserved_2
856 get_be16(pb);//Reserved_2
857 get_be32(pb);//Reserved_4
858 get_be16(pb);//TimeScale
859 get_be16(pb);//Reserved_2
861 //AMRSpecificBox.(10 bytes)
870 get_be32(pb); //type=='damr'
874 get_be32(pb); //vendor
875 get_byte(pb); //decoder version
876 get_be16(pb); //mode_set
877 get_byte(pb); //mode_change_period
878 get_byte(pb); //frames_per_sample
881 printf("Audio: damr_type=%c%c%c%c damr_size=%Ld damr_vendor=%c%c%c%c\n",
882 (damr_type >> 24) & 0xff,
883 (damr_type >> 16) & 0xff,
884 (damr_type >> 8) & 0xff,
885 (damr_type >> 0) & 0xff,
887 (damr_vendor >> 24) & 0xff,
888 (damr_vendor >> 16) & 0xff,
889 (damr_vendor >> 8) & 0xff,
890 (damr_vendor >> 0) & 0xff
894 st->time_length=0;//Not possible to get from this info, must count number of AMR frames
895 st->codec.sample_rate=8000;
896 st->codec.channels=1;
897 st->codec.bits_per_sample=16;
898 st->codec.bit_rate=0; /*It is not possible to tell this before we have
899 an audio frame and even then every frame can be different*/
903 get_be16(pb); /* version */
904 get_be16(pb); /* revision level */
905 get_be32(pb); /* vendor */
907 st->codec.channels = get_be16(pb); /* channel count */
908 st->codec.bits_per_sample = get_be16(pb); /* sample size */
910 /* handle specific s8 codec */
911 get_be16(pb); /* compression id = 0*/
912 get_be16(pb); /* packet size = 0 */
914 st->codec.sample_rate = ((get_be32(pb) >> 16));
915 //printf("CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
917 switch (st->codec.codec_id) {
918 case CODEC_ID_PCM_S16BE:
919 if (st->codec.bits_per_sample == 8)
920 st->codec.codec_id = CODEC_ID_PCM_S8;
922 case CODEC_ID_PCM_U8:
923 st->codec.bit_rate = st->codec.sample_rate * 8;
928 get_be32(pb); /* samples per packet */
929 get_be32(pb); /* bytes per packet */
930 get_be32(pb); /* bytes per frame */
931 get_be32(pb); /* bytes per sample */
933 MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
934 mov_read_default(c, pb, a);
943 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
945 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
946 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
949 print_atom("stsc", atom);
951 get_byte(pb); /* version */
952 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
954 entries = get_be32(pb);
956 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
958 sc->sample_to_chunk_sz = entries;
959 sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
960 if (!sc->sample_to_chunk)
962 for(i=0; i<entries; i++) {
963 sc->sample_to_chunk[i].first = get_be32(pb);
964 sc->sample_to_chunk[i].count = get_be32(pb);
965 sc->sample_to_chunk[i].id = get_be32(pb);
967 /* 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); */
973 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
975 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
976 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
979 print_atom("stsz", atom);
981 get_byte(pb); /* version */
982 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
984 sc->sample_size = get_be32(pb);
985 entries = get_be32(pb);
986 sc->sample_count = entries;
988 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
991 return 0; /* there isn't any table following */
992 sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
993 if (!sc->sample_sizes)
995 for(i=0; i<entries; i++) {
996 sc->sample_sizes[i] = get_be32(pb);
998 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
1004 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1006 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1007 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
1010 int total_sample_count=0;
1012 print_atom("stts", atom);
1014 get_byte(pb); /* version */
1015 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1016 entries = get_be32(pb);
1020 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1022 for(i=0; i<entries; i++) {
1023 int sample_duration;
1026 sample_count=get_be32(pb);
1027 sample_duration = get_be32(pb);
1029 printf("sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
1031 duration+=sample_duration*sample_count;
1032 total_sample_count+=sample_count;
1034 #if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
1036 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
1037 st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
1038 st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
1040 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
1046 if(st->codec.codec_type==CODEC_TYPE_VIDEO)
1048 //Only support constant frame rate. But lets calculate the average. Needed by .mp4-files created with nec e606 3g phone.
1049 //Hmm, lets set base to 1
1050 st->codec.frame_rate_base=1;
1051 st->codec.frame_rate = st->codec.frame_rate_base * c->streams[c->total_streams]->time_scale * total_sample_count / duration;
1053 printf("VIDEO FRAME RATE average= %i (tot sample count= %i ,tot dur= %i)\n", st->codec.frame_rate,total_sample_count,duration);
1059 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1062 MOVStreamContext *sc;
1064 print_atom("trak", atom);
1066 st = av_new_stream(c->fc, c->fc->nb_streams);
1068 sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
1074 sc->sample_to_chunk_index = -1;
1076 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
1077 st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
1078 c->streams[c->fc->nb_streams-1] = sc;
1080 return mov_read_default(c, pb, atom);
1083 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1087 print_atom("tkhd", atom);
1089 st = c->fc->streams[c->fc->nb_streams-1];
1091 get_byte(pb); /* version */
1093 get_byte(pb); get_byte(pb);
1094 get_byte(pb); /* flags */
1096 MOV_TRACK_ENABLED 0x0001
1097 MOV_TRACK_IN_MOVIE 0x0002
1098 MOV_TRACK_IN_PREVIEW 0x0004
1099 MOV_TRACK_IN_POSTER 0x0008
1102 get_be32(pb); /* creation time */
1103 get_be32(pb); /* modification time */
1104 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1105 get_be32(pb); /* reserved */
1106 st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
1107 get_be32(pb); /* reserved */
1108 get_be32(pb); /* reserved */
1110 get_be16(pb); /* layer */
1111 get_be16(pb); /* alternate group */
1112 get_be16(pb); /* volume */
1113 get_be16(pb); /* reserved */
1115 url_fskip(pb, 36); /* display matrix */
1117 /* those are fixed-point */
1118 st->codec.width = get_be32(pb) >> 16; /* track width */
1119 st->codec.height = get_be32(pb) >> 16; /* track height */
1124 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1125 /* like the files created with Adobe Premiere 5.0, for samples see */
1126 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1127 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1132 print_atom("wide", atom);
1136 return 0; /* continue */
1137 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1138 url_fskip(pb, atom.size - 4);
1141 atom.type = get_le32(pb);
1144 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
1145 url_fskip(pb, atom.size);
1148 err = mov_read_mdat(c, pb, atom);
1157 static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
1162 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1166 uint8_t *moov_data; /* uncompressed data */
1167 long cmov_len, moov_len;
1170 print_atom("cmov", atom);
1172 get_be32(pb); /* dcom atom */
1173 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1175 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1176 dprintf("unknown compression for cmov atom !");
1179 get_be32(pb); /* cmvd atom */
1180 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1182 moov_len = get_be32(pb); /* uncompressed size */
1183 cmov_len = atom.size - 6 * 4;
1185 cmov_data = (uint8_t *) av_malloc(cmov_len);
1188 moov_data = (uint8_t *) av_malloc(moov_len);
1193 get_buffer(pb, cmov_data, cmov_len);
1194 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1196 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1198 ctx.buf_end = ctx.buffer + moov_len;
1199 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1201 atom.size = moov_len;
1203 { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1205 ret = mov_read_default(c, &ctx, atom);
1213 static const MOVParseTableEntry mov_default_parse_table[] = {
1215 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1216 { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
1217 { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
1218 { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
1219 { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
1220 { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
1221 { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
1222 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1223 { MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
1224 { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
1225 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1226 { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
1227 { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
1228 { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
1229 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1230 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1231 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1232 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1233 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1234 { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
1235 { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
1236 { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
1237 { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
1238 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1239 { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
1240 { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
1241 { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
1242 { MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
1243 { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
1244 { MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorrenson extension ??? */
1245 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1246 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1247 { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
1248 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1249 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1250 { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
1251 { MKTAG( 's', 't', 's', 's' ), mov_read_leaf }, /* sync sample */
1252 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1253 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1254 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1255 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1256 { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
1257 { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
1258 { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
1259 { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
1260 { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
1261 { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
1262 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
1264 { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
1266 { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
1267 { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
1268 { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
1269 { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1270 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1271 { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
1272 { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
1273 { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
1274 { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
1275 { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
1276 { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
1277 { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
1278 { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
1279 { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
1280 { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
1281 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1282 //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
1284 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1286 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
1288 { 0L, mov_read_leaf }
1291 static void mov_free_stream_context(MOVStreamContext *sc)
1294 av_free(sc->chunk_offsets);
1295 av_free(sc->sample_to_chunk);
1296 av_free(sc->sample_sizes);
1297 av_free(sc->header_data);
1302 static inline uint32_t mov_to_tag(uint8_t *buf)
1304 return MKTAG(buf[0], buf[1], buf[2], buf[3]);
1307 static inline uint32_t to_be32(uint8_t *buf)
1309 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1312 /* XXX: is it sufficient ? */
1313 static int mov_probe(AVProbeData *p)
1315 unsigned int offset;
1318 /* check file header */
1319 if (p->buf_size <= 12)
1323 /* ignore invalid offset */
1324 if ((offset + 8) > (unsigned int)p->buf_size)
1326 tag = mov_to_tag(p->buf + offset + 4);
1328 case MKTAG( 'm', 'o', 'o', 'v' ):
1329 case MKTAG( 'w', 'i', 'd', 'e' ):
1330 case MKTAG( 'f', 'r', 'e', 'e' ):
1331 case MKTAG( 'm', 'd', 'a', 't' ):
1332 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1333 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
1334 return AVPROBE_SCORE_MAX;
1335 case MKTAG( 'f', 't', 'y', 'p' ):
1336 case MKTAG( 's', 'k', 'i', 'p' ):
1337 offset = to_be32(p->buf) + offset;
1340 /* unrecognized tag */
1347 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1349 MOVContext *mov = (MOVContext *) s->priv_data;
1350 ByteIOContext *pb = &s->pb;
1352 MOV_atom_t atom = { 0, 0, 0 };
1355 mov->parse_table = mov_default_parse_table;
1357 /* XXX: I think we should auto detect */
1358 if(s->iformat->name[1] == 'p')
1361 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1362 atom.size = url_filesize(url_fileno(pb));
1364 atom.size = 0x7FFFFFFFFFFFFFFFLL;
1367 printf("filesz=%Ld\n", atom.size);
1370 /* check MOV header */
1371 err = mov_read_default(mov, pb, atom);
1372 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
1373 fprintf(stderr, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
1374 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1378 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1380 /* some cleanup : make sure we are on the mdat atom */
1381 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1382 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1384 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1387 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1391 printf("streams= %d\n", s->nb_streams);
1393 mov->total_streams = nb = s->nb_streams;
1396 for(i=0; i<s->nb_streams;) {
1397 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1398 av_free(s->streams[i]);
1399 for(j=i+1; j<s->nb_streams; j++)
1400 s->streams[j-1] = s->streams[j];
1405 for(i=0; i<s->nb_streams;i++) {
1406 MOVStreamContext *sc;
1407 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1409 sc->is_ff_stream = 1;
1413 printf("real streams= %d\n", s->nb_streams);
1418 /* Yes, this is ugly... I didn't write the specs of QT :p */
1419 /* XXX:remove useless commented code sometime */
1420 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1422 MOVContext *mov = (MOVContext *) s->priv_data;
1423 MOVStreamContext *sc;
1424 int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
1429 #ifdef MOV_SPLIT_CHUNKS
1435 idx = sc->sample_to_chunk_index;
1437 if (idx < 0) return 0;
1438 size = sc->sample_sizes[sc->current_sample];
1440 sc->current_sample++;
1441 sc->left_in_chunk--;
1443 if (sc->left_in_chunk <= 0)
1445 offset = mov->next_chunk_offset;
1446 /* extract the sample */
1454 for(i=0; i<mov->total_streams; i++) {
1455 MOVStreamContext *msc = mov->streams[i];
1456 //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
1457 if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1458 && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1460 offset = msc->chunk_offsets[msc->next_chunk];
1461 //printf("SELETED %Ld i:%d\n", offset, i);
1464 if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
1469 if(mov->next_chunk_offset < offset) { /* some meta data */
1470 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1471 mov->next_chunk_offset = offset;
1474 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1475 if(!sc->is_ff_stream) {
1476 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1477 mov->next_chunk_offset = offset;
1478 offset = 0x0FFFFFFFFFFFFFFFLL;
1482 /* now get the chunk size... */
1484 for(i=0; i<mov->total_streams; i++) {
1485 MOVStreamContext *msc = mov->streams[i];
1486 if ((msc->next_chunk < msc->chunk_count)
1487 && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1488 size = msc->chunk_offsets[msc->next_chunk] - offset;
1491 #ifdef MOV_MINOLTA_FIX
1492 //Make sure that size is according to sample_size (Needed by .mov files
1493 //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
1494 //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
1495 //but I have no such movies
1496 if (sc->sample_size > 0) {
1498 for(i=0; i<(sc->sample_to_chunk_sz); i++) {
1499 if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) )
1501 foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
1504 /*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);*/
1507 if( (foundsize>0) && (foundsize<size) )
1510 /*printf("this size should actually be %d\n",foundsize);*/
1515 #endif //MOV_MINOLTA_FIX
1517 #ifdef MOV_SPLIT_CHUNKS
1518 /* split chunks into samples */
1519 if (sc->sample_size == 0) {
1520 int idx = sc->sample_to_chunk_index;
1521 if ((idx + 1 < sc->sample_to_chunk_sz)
1522 && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1524 sc->sample_to_chunk_index = idx;
1525 if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1527 /* we'll have to get those samples before next chunk */
1528 sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1529 size = sc->sample_sizes[sc->current_sample];
1532 sc->current_sample++;
1537 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1538 if(size == 0x0FFFFFFF)
1539 size = mov->mdat_size + mov->mdat_offset - offset;
1544 url_fseek(&s->pb, offset, SEEK_SET);
1546 //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
1547 if (sc->header_len > 0) {
1548 av_new_packet(pkt, size + sc->header_len);
1549 memcpy(pkt->data, sc->header_data, sc->header_len);
1550 get_buffer(&s->pb, pkt->data + sc->header_len, size);
1552 av_freep(&sc->header_data);
1555 av_new_packet(pkt, size);
1556 get_buffer(&s->pb, pkt->data, pkt->size);
1558 pkt->stream_index = sc->ffindex;
1562 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1564 printf("%02x ", pkt->data[i]);
1566 printf("%c ", (pkt->data[i]) & 0x7F);
1571 mov->next_chunk_offset = offset + size;
1576 static int mov_read_close(AVFormatContext *s)
1579 MOVContext *mov = (MOVContext *) s->priv_data;
1580 for(i=0; i<mov->total_streams; i++)
1581 mov_free_stream_context(mov->streams[i]);
1582 for(i=0; i<s->nb_streams; i++)
1583 av_freep(&s->streams[i]);
1584 /* free color tabs */
1585 for(i=0; i<mov->ctab_size; i++)
1586 av_freep(&mov->ctab[i]);
1587 av_freep(&mov->ctab);
1591 static AVInputFormat mov_iformat = {
1593 "QuickTime/MPEG4 format",
1603 av_register_input_format(&mov_iformat);