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_t type, uint64_t offset, uint64_t 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",
80 assert((unsigned int)size < 0x7fffffff);// catching errors
84 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
85 /* so we first list them as this, then clean up the list of streams we give back, */
86 /* getting rid of these */
87 #define CODEC_TYPE_MOV_OTHER 2
89 static const CodecTag mov_video_tags[] = {
90 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
91 /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
92 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
93 /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
98 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
99 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
100 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
101 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
102 { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
103 /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
104 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
106 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
107 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
108 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
109 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
110 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
111 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
112 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
113 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
114 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
115 /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
119 static const CodecTag mov_audio_tags[] = {
120 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
121 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
122 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
123 { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
124 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
125 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
126 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
127 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
128 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
129 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
131 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
132 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
133 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
134 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
136 /* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
137 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
141 /* the QuickTime file format is quite convoluted...
142 * it has lots of index tables, each indexing something in another one...
143 * Here we just use what is needed to read the chunks
146 typedef struct MOV_sample_to_chunk_tbl {
150 } MOV_sample_to_chunk_tbl;
154 uint32_t flags; // 24bit
156 /* 0x03 ESDescrTag */
158 #define MP4ODescrTag 0x01
159 #define MP4IODescrTag 0x02
160 #define MP4ESDescrTag 0x03
161 #define MP4DecConfigDescrTag 0x04
162 #define MP4DecSpecificDescrTag 0x05
163 #define MP4SLConfigDescrTag 0x06
164 #define MP4ContentIdDescrTag 0x07
165 #define MP4SupplContentIdDescrTag 0x08
166 #define MP4IPIPtrDescrTag 0x09
167 #define MP4IPMPPtrDescrTag 0x0A
168 #define MP4IPMPDescrTag 0x0B
169 #define MP4RegistrationDescrTag 0x0D
170 #define MP4ESIDIncDescrTag 0x0E
171 #define MP4ESIDRefDescrTag 0x0F
172 #define MP4FileIODescrTag 0x10
173 #define MP4FileODescrTag 0x11
174 #define MP4ExtProfileLevelDescrTag 0x13
175 #define MP4ExtDescrTagsStart 0x80
176 #define MP4ExtDescrTagsEnd 0xFE
177 uint8_t stream_priority;
179 /* 0x04 DecConfigDescrTag */
180 uint8_t object_type_id;
182 /* XXX: really streamType is
183 * only 6bit, followed by:
187 uint32_t buffer_size_db; // 24
188 uint32_t max_bitrate;
189 uint32_t avg_bitrate;
191 /* 0x05 DecSpecificDescrTag */
192 uint8_t decoder_cfg_len;
193 uint8_t *decoder_cfg;
195 /* 0x06 SLConfigDescrTag */
196 uint8_t sl_config_len;
200 typedef struct MOVStreamContext {
201 int ffindex; /* the ffmpeg stream id */
202 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
205 int64_t *chunk_offsets;
206 long sample_to_chunk_sz;
207 MOV_sample_to_chunk_tbl *sample_to_chunk;
208 long sample_to_chunk_index;
214 long left_in_chunk; /* how many samples before next chunk */
215 /* specific MPEG4 header which is added at the beginning of the stream */
218 uint8_t *header_data;
221 typedef struct MOVContext {
222 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) */
225 int duration; /* duration of the longest track */
226 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
227 int found_mdat; /* we suppose we have enough data to read the file */
231 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
232 * but we need the info to be able to skip data from those streams in the 'mdat' section
234 MOVStreamContext *streams[MAX_STREAMS];
236 int64_t next_chunk_offset;
237 MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
241 struct MOVParseTableEntry;
243 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
245 /* those functions parse an atom */
247 1: found what I wanted, exit
248 0: continue to parse next atom
249 -1: error occured, exit
251 typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
254 int64_t atom_offset, /* after the size and type field (and eventually the extended size) */
255 int64_t atom_size, /* total size (excluding the size and type fields) */
258 /* links atom IDs to parse functions */
259 typedef struct MOVParseTableEntry {
261 mov_parse_function func;
262 } MOVParseTableEntry;
263 static const MOVParseTableEntry mov_default_parse_table[];
265 static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
268 print_atom("leaf", atom_type, atom_offset, atom_size);
271 url_fskip(pb, atom_size);
272 /* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
277 static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
279 uint32_t type, foo=0;
280 uint64_t offset, size;
281 uint64_t total_size = 0;
286 print_atom("default", atom_type, atom_offset, atom_size);
290 offset = atom_offset;
293 atom_size = 0x0FFFFFFFFFFFFFFF;
294 while(((total_size + 8) < atom_size) && !url_feof(pb) && !err) {
303 //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom_size, total_size);
304 if(size == 1) { /* 64 bit extended size */
305 size = get_be64(pb) - 8;
310 size = atom_size - total_size;
314 for (i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++)
318 // printf(" i=%ld\n", i);
319 if (parse_table[i].type == 0) { /* skip leaf atoms data */
320 // url_seek(pb, atom_offset+atom_size, SEEK_SET);
322 print_atom("unknown", type, offset, size);
326 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
332 if (!err && total_size < atom_size && atom_size < 0x7ffff) {
333 printf("RESET %Ld %Ld err:%d\n", atom_size, total_size, err);
334 url_fskip(pb, atom_size - total_size);
343 static int parse_ctab(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
345 url_fskip(pb, atom_size); // for now
349 static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
353 print_atom("mvhd", atom_type, atom_offset, atom_size);
355 c = (MOVContext *)param;
357 get_byte(pb); /* version */
358 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
360 get_be32(pb); /* creation time */
361 get_be32(pb); /* modification time */
362 c->time_scale = get_be32(pb); /* time scale */
364 printf("time scale = %i\n", c->time_scale);
366 c->duration = get_be32(pb); /* duration */
367 get_be32(pb); /* preferred scale */
369 get_be16(pb); /* preferred volume */
371 url_fskip(pb, 10); /* reserved */
373 url_fskip(pb, 36); /* display matrix */
375 get_be32(pb); /* preview time */
376 get_be32(pb); /* preview duration */
377 get_be32(pb); /* poster time */
378 get_be32(pb); /* selection time */
379 get_be32(pb); /* selection duration */
380 get_be32(pb); /* current time */
381 get_be32(pb); /* next track ID */
386 /* this atom should contain all header atoms */
387 static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
392 print_atom("moov", atom_type, atom_offset, atom_size);
394 c = (MOVContext *)param;
396 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
397 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
398 /* so we don't parse the whole file if over a network */
401 return 1; /* found both, just go */
402 return 0; /* now go for mdat */
405 /* this atom contains actual media data */
406 static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
410 print_atom("mdat", atom_type, atom_offset, atom_size);
412 c = (MOVContext *)param;
414 if(atom_size == 0) /* wrong one (MP4) */
417 c->mdat_offset = atom_offset;
418 c->mdat_size = atom_size;
420 return 1; /* found both, just go */
421 url_fskip(pb, atom_size);
422 return 0; /* now go for moov */
425 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
426 /* like the files created with Adobe Premiere 5.0, for samples see */
427 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
428 static int parse_wide(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
433 print_atom("wide", atom_type, atom_offset, atom_size);
437 return 0; /* continue */
438 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
439 url_fskip(pb, atom_size - 4);
443 if (type != MKTAG('m', 'd', 'a', 't')) {
444 url_fskip(pb, atom_size - 8);
447 err = parse_mdat(parse_table, pb, type, atom_offset + 8, atom_size - 8, param);
454 static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
458 MOVStreamContext *sc;
460 print_atom("trak", atom_type, atom_offset, atom_size);
463 c = (MOVContext *)param;
464 st = av_new_stream(c->fc, c->fc->nb_streams);
466 sc = av_malloc(sizeof(MOVStreamContext));
467 memset(sc, 0, sizeof(MOVStreamContext));
468 sc->sample_to_chunk_index = -1;
470 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
471 st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
472 c->streams[c->fc->nb_streams-1] = sc;
473 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
476 static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
481 print_atom("tkhd", atom_type, atom_offset, atom_size);
484 c = (MOVContext *)param;
485 st = c->fc->streams[c->fc->nb_streams-1];
487 get_byte(pb); /* version */
489 get_byte(pb); get_byte(pb);
490 get_byte(pb); /* flags */
492 MOV_TRACK_ENABLED 0x0001
493 MOV_TRACK_IN_MOVIE 0x0002
494 MOV_TRACK_IN_PREVIEW 0x0004
495 MOV_TRACK_IN_POSTER 0x0008
498 get_be32(pb); /* creation time */
499 get_be32(pb); /* modification time */
500 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
501 get_be32(pb); /* reserved */
502 st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
503 get_be32(pb); /* reserved */
504 get_be32(pb); /* reserved */
506 get_be16(pb); /* layer */
507 get_be16(pb); /* alternate group */
508 get_be16(pb); /* volume */
509 get_be16(pb); /* reserved */
511 url_fskip(pb, 36); /* display matrix */
513 /* those are fixed-point */
514 st->codec.width = get_be32(pb) >> 16; /* track width */
515 st->codec.height = get_be32(pb) >> 16; /* track height */
520 static int parse_mdhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
525 print_atom("mdhd", atom_type, atom_offset, atom_size);
528 c = (MOVContext *)param;
529 st = c->fc->streams[c->fc->nb_streams-1];
531 get_byte(pb); /* version */
533 get_byte(pb); get_byte(pb);
534 get_byte(pb); /* flags */
536 get_be32(pb); /* creation time */
537 get_be32(pb); /* modification time */
539 c->streams[c->total_streams]->time_scale = get_be32(pb);
542 printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
544 get_be32(pb); /* duration */
546 get_be16(pb); /* language */
547 get_be16(pb); /* quality */
552 static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
561 print_atom("hdlr", atom_type, atom_offset, atom_size);
563 c = (MOVContext *)param;
564 st = c->fc->streams[c->fc->nb_streams-1];
566 get_byte(pb); /* version */
567 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
570 ctype = get_le32(pb);
571 type = get_le32(pb); /* component subtype */
574 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
575 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
578 /* XXX: yeah this is ugly... */
579 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
580 if(type == MKTAG('v', 'i', 'd', 'e'))
582 else if(type == MKTAG('s', 'o', 'u', 'n'))
584 } else if(ctype == 0) { /* MP4 */
585 if(type == MKTAG('v', 'i', 'd', 'e'))
587 else if(type == MKTAG('s', 'o', 'u', 'n'))
589 else if(type == MKTAG('o', 'd', 's', 'm'))
591 else if(type == MKTAG('s', 'd', 's', 'm'))
593 } else puts("hdlr: meta");
596 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
597 /* helps parsing the string hereafter... */
599 if(type == MKTAG('v', 'i', 'd', 'e'))
600 st->codec.codec_type = CODEC_TYPE_VIDEO;
601 else if(type == MKTAG('s', 'o', 'u', 'n'))
602 st->codec.codec_type = CODEC_TYPE_AUDIO;
603 } else if(ctype == 0) { /* MP4 */
604 /* helps parsing the string hereafter... */
606 if(type == MKTAG('v', 'i', 'd', 'e'))
607 st->codec.codec_type = CODEC_TYPE_VIDEO;
608 else if(type == MKTAG('s', 'o', 'u', 'n'))
609 st->codec.codec_type = CODEC_TYPE_AUDIO;
611 get_be32(pb); /* component manufacture */
612 get_be32(pb); /* component flags */
613 get_be32(pb); /* component flags mask */
616 return 0; /* nothing left to read */
617 /* XXX: MP4 uses a C string, not a pascal one */
622 while(get_byte(pb) && (++len < (atom_size - 24)));
624 /* .mov: PASCAL string */
626 buf = av_malloc(len+1);
627 get_buffer(pb, buf, len);
630 printf("**buf='%s'\n", buf);
636 /* XXX: use a better heuristic */
638 /* assume that it is a Pascal like string */
639 buf = av_malloc(len+1);
640 get_buffer(pb, buf, len);
643 printf("**buf='%s'\n", buf);
659 static int mp4_read_descr_len(ByteIOContext *pb)
664 int c = get_byte(pb);
665 len = (len << 7) | (c & 0x7f);
674 static int mp4_read_descr(ByteIOContext *pb, int *tag)
678 len = mp4_read_descr_len(pb);
680 printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
685 static inline unsigned int get_be24(ByteIOContext *s)
688 val = get_byte(s) << 16;
689 val |= get_byte(s) << 8;
694 static int parse_esds(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
697 int64_t start_pos = url_ftell(pb);
698 MOVContext *c = (MOVContext *)param;
699 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
700 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
703 print_atom("esds", atom_type, atom_offset, atom_size);
706 /* Well, broken but suffisant for some MP4 streams */
707 get_be32(pb); /* version + flags */
708 len = mp4_read_descr(pb, &tag);
709 if (tag == MP4ESDescrTag) {
710 get_be16(pb); /* ID */
711 get_byte(pb); /* priority */
713 get_be16(pb); /* ID */
715 len = mp4_read_descr(pb, &tag);
716 if (tag == MP4DecConfigDescrTag) {
717 sc->esds.object_type_id = get_byte(pb);
718 sc->esds.stream_type = get_be24(pb);
719 sc->esds.max_bitrate = get_be32(pb);
720 sc->esds.avg_bitrate = get_be32(pb);
722 len = mp4_read_descr(pb, &tag);
723 printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
724 if (tag == MP4DecSpecificDescrTag) {
726 printf("Specific MPEG4 header len=%d\n", len);
728 sc->header_data = av_mallocz(len);
729 if (sc->header_data) {
730 get_buffer(pb, sc->header_data, len);
731 sc->header_len = len;
735 /* in any case, skip garbage */
736 url_fskip(pb, (atom_size - 8) - ((url_ftell(pb) - start_pos)));
740 static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
743 int entries, size, frames_per_sample, id;
746 MOVStreamContext *sc;
748 print_atom("stsd", atom_type, atom_offset, atom_size);
750 c = (MOVContext *)param;
751 st = c->fc->streams[c->fc->nb_streams-1];
752 sc = (MOVStreamContext *)st->priv_data;
754 get_byte(pb); /* version */
755 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
757 entries = get_be32(pb);
760 size = get_be32(pb); /* size */
761 format = get_le32(pb); /* data format */
763 get_be32(pb); /* reserved */
764 get_be16(pb); /* reserved */
765 get_be16(pb); /* index */
767 /* for MPEG4: set codec type by looking for it */
768 id = codec_get_id(mov_video_tags, format);
771 codec = avcodec_find_decoder(id);
773 st->codec.codec_type = codec->type;
776 printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
778 (format >> 0) & 0xff,
779 (format >> 8) & 0xff,
780 (format >> 16) & 0xff,
781 (format >> 24) & 0xff,
782 st->codec.codec_type);
784 st->codec.codec_tag = format;
785 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
786 st->codec.codec_id = codec_get_id(mov_video_tags, format);
787 get_be16(pb); /* version */
788 get_be16(pb); /* revision level */
789 get_be32(pb); /* vendor */
790 get_be32(pb); /* temporal quality */
791 get_be32(pb); /* spacial quality */
792 st->codec.width = get_be16(pb); /* width */
793 st->codec.height = get_be16(pb); /* height */
795 if (st->codec.codec_id == CODEC_ID_MPEG4) {
796 /* in some MPEG4 the width/height are not correct, so
797 we ignore this info */
799 st->codec.height = 0;
802 get_be32(pb); /* horiz resolution */
803 get_be32(pb); /* vert resolution */
804 get_be32(pb); /* data size, always 0 */
805 frames_per_sample = get_be16(pb); /* frames per samples */
807 printf("frames/samples = %d\n", frames_per_sample);
809 get_buffer(pb, st->codec.codec_name, 32); /* codec name */
811 st->codec.bits_per_sample = get_be16(pb); /* depth */
812 st->codec.color_table_id = get_be16(pb); /* colortable id */
814 st->codec.frame_rate = 25;
815 st->codec.frame_rate_base = 1;
817 size -= (16+8*4+2+32+2*2);
820 int atom_size, atom_type;
823 atom_size = get_be32(pb);
824 atom_type = get_le32(pb);
826 printf("NEWSIZE %d\n", size);
828 printf("VIDEO: atom_type=%c%c%c%c atom_size=%d size_left=%d\n",
829 (atom_type >> 0) & 0xff,
830 (atom_type >> 8) & 0xff,
831 (atom_type >> 16) & 0xff,
832 (atom_type >> 24) & 0xff,
835 start_pos = url_ftell(pb);
838 case MKTAG('e', 's', 'd', 's'):
841 /* Well, broken but suffisant for some MP4 streams */
842 get_be32(pb); /* version + flags */
843 len = mp4_read_descr(pb, &tag);
846 get_be16(pb); /* ID */
847 get_byte(pb); /* priority */
848 len = mp4_read_descr(pb, &tag);
851 /* MP4DecConfigDescrTag */
852 get_byte(pb); /* objectTypeId */
853 get_be32(pb); /* streamType + buffer size */
854 get_be32(pb); /* max bit rate */
855 get_be32(pb); /* avg bit rate */
856 len = mp4_read_descr(pb, &tag);
859 /* MP4DecSpecificDescrTag */
861 printf("Specific MPEG4 header len=%d\n", len);
863 sc->header_data = av_mallocz(len);
864 if (sc->header_data) {
865 get_buffer(pb, sc->header_data, len);
866 sc->header_len = len;
869 /* in any case, skip garbage */
876 printf("ATOMENEWSIZE %d %d\n", atom_size, url_ftell(pb) - start_pos);
878 url_fskip(pb, (atom_size - 8) -
879 ((url_ftell(pb) - start_pos)));
880 size -= atom_size - 8;
881 printf("NEWSIZE %d\n", size);
885 /* unknown extension */
889 parse_default(mov_default_parse_table, pb, 0L, 0LL, size, param);
892 get_be16(pb); /* version */
893 get_be16(pb); /* revision level */
894 get_be32(pb); /* vendor */
896 st->codec.channels = get_be16(pb);/* channel count */
897 st->codec.bits_per_sample = get_be16(pb); /* sample size */
899 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
900 /* handle specific s8 codec */
901 get_be16(pb); /* compression id = 0*/
902 get_be16(pb); /* packet size = 0 */
904 st->codec.sample_rate = ((get_be32(pb) >> 16));
906 if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
907 if (st->codec.bits_per_sample == 8)
908 st->codec.codec_id = CODEC_ID_PCM_S8;
909 st->codec.bit_rate = st->codec.sample_rate;
911 get_be32(pb); /* samples per packet */
912 get_be32(pb); /* bytes per packet */
913 get_be32(pb); /* bytes per frame */
914 get_be32(pb); /* bytes per sample */
916 //if (size > 16) url_fskip(pb, size-(16+20));
918 if (size >= 44 + 8) {
920 st->codec.extradata_size = get_be32(pb) - 8;
921 fcc = get_le32(pb); // evaw
922 //printf("%x %.4s %d\n", fcc, (char*)&fcc, st->codec.extradata_size);
923 st->codec.extradata = av_mallocz(st->codec.extradata_size);
924 get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
925 url_fskip(pb, size-(16 + 20 + 16 + 8 + st->codec.extradata_size));
928 url_fskip(pb, size-(16 + 20 + 16));
930 parse_default(mov_default_parse_table, pb, 0L, 0LL, size - (16 + 20 + 16 + 8), param);
936 buf = av_malloc(len+1);
937 get_buffer(pb, buf, len);
946 static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
951 MOVStreamContext *sc;
953 print_atom("stco", atom_type, atom_offset, atom_size);
955 c = (MOVContext *)param;
956 st = c->fc->streams[c->fc->nb_streams-1];
957 sc = (MOVStreamContext *)st->priv_data;
959 get_byte(pb); /* version */
960 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
962 entries = get_be32(pb);
963 sc->chunk_count = entries;
964 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
965 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
966 for(i=0; i<entries; i++) {
967 sc->chunk_offsets[i] = get_be32(pb);
969 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
970 for(i=0; i<entries; i++) {
971 sc->chunk_offsets[i] = get_be64(pb);
977 for(i=0; i<entries; i++) {
978 printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
985 static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
990 MOVStreamContext *sc;
992 print_atom("stsc", atom_type, atom_offset, atom_size);
994 c = (MOVContext *)param;
995 st = c->fc->streams[c->fc->nb_streams-1];
996 sc = (MOVStreamContext *)st->priv_data;
998 get_byte(pb); /* version */
999 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1001 entries = get_be32(pb);
1003 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1005 sc->sample_to_chunk_sz = entries;
1006 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
1007 for(i=0; i<entries; i++) {
1008 sc->sample_to_chunk[i].first = get_be32(pb);
1009 sc->sample_to_chunk[i].count = get_be32(pb);
1010 sc->sample_to_chunk[i].id = get_be32(pb);
1012 /* 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); */
1018 static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
1023 MOVStreamContext *sc;
1025 print_atom("stsz", atom_type, atom_offset, atom_size);
1027 c = (MOVContext *)param;
1028 st = c->fc->streams[c->fc->nb_streams-1];
1029 sc = (MOVStreamContext *)st->priv_data;
1031 get_byte(pb); /* version */
1032 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1034 sc->sample_size = get_be32(pb);
1035 entries = get_be32(pb);
1036 sc->sample_count = entries;
1038 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
1041 return 0; /* there isn't any table following */
1042 sc->sample_sizes = av_malloc(entries * sizeof(long));
1043 for(i=0; i<entries; i++) {
1044 sc->sample_sizes[i] = get_be32(pb);
1046 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
1052 static int parse_stts(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
1057 MOVStreamContext *sc;
1059 print_atom("stts", atom_type, atom_offset, atom_size);
1061 c = (MOVContext *)param;
1062 st = c->fc->streams[c->fc->nb_streams-1];
1063 sc = (MOVStreamContext *)st->priv_data;
1065 get_byte(pb); /* version */
1066 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1067 entries = get_be32(pb);
1069 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
1071 for(i=0; i<entries; i++) {
1072 int sample_duration;
1075 sample_duration = get_be32(pb);
1077 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
1078 st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
1079 st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
1081 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
1089 static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
1094 static int parse_cmov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, uint32_t atom_type, int64_t atom_offset, int64_t atom_size, void *param)
1099 unsigned char *moov_data; /* uncompressed data */
1100 long cmov_len, moov_len;
1103 print_atom("cmov", atom_type, atom_offset, atom_size);
1105 c = (MOVContext *)param;
1107 get_be32(pb); /* dcom atom */
1108 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1110 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1111 puts("unknown compression for cmov atom !");
1114 get_be32(pb); /* cmvd atom */
1115 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1117 moov_len = get_be32(pb); /* uncompressed size */
1118 cmov_len = atom_size - 6 * 4;
1120 cmov_data = av_malloc(cmov_len);
1123 moov_data = av_malloc(moov_len);
1128 get_buffer(pb, cmov_data, cmov_len);
1129 if(uncompress (moov_data, &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1131 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1133 ctx.buf_end = ctx.buffer + moov_len;
1134 ret = parse_default(parse_table, &ctx, MKTAG( 'm', 'o', 'o', 'v' ), 0, moov_len, param);
1141 static const MOVParseTableEntry mov_default_parse_table[] = {
1143 { MKTAG( 'm', 'p', '4', 'a' ), parse_default },
1144 { MKTAG( 'c', 'o', '6', '4' ), parse_stco },
1145 { MKTAG( 's', 't', 'c', 'o' ), parse_stco },
1146 { MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
1147 { MKTAG( 'c', 't', 't', 's' ), parse_leaf },
1148 { MKTAG( 'c', 'p', 'r', 't' ), parse_default },
1149 { MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
1150 { MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
1151 { MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
1152 { MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
1153 { MKTAG( 's', 't', 'd', 'p' ), parse_default },
1154 { MKTAG( 'e', 'd', 't', 's' ), parse_default },
1155 { MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
1156 { MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
1157 { MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
1158 { MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
1159 { MKTAG( 'h', 'm', 'h', 'd' ), parse_leaf },
1160 { MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
1161 { MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
1162 { MKTAG( 'm', 'p', '4', 's' ), parse_default },
1163 { MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
1164 { MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
1165 { MKTAG( 'm', 'd', 'h', 'd' ), parse_mdhd },
1166 { MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
1167 { MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
1168 { MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
1169 { MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
1170 { MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
1171 { MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
1172 { MKTAG( 's', 't', 's', 'd' ), parse_stsd },
1173 { MKTAG( 's', 't', 's', 'z' ), parse_stsz },
1174 { MKTAG( 's', 't', 'b', 'l' ), parse_default },
1175 { MKTAG( 's', 't', 's', 'c' ), parse_stsc },
1176 { MKTAG( 's', 'd', 'h', 'd' ), parse_default },
1177 { MKTAG( 's', 't', 's', 'h' ), parse_default },
1178 { MKTAG( 's', 'k', 'i', 'p' ), parse_default },
1179 { MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
1180 { MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
1181 { MKTAG( 's', 't', 's', 's' ), parse_leaf },
1182 { MKTAG( 's', 't', 't', 's' ), parse_stts },
1183 { MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
1184 { MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
1185 { MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
1186 { MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
1187 { MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
1188 { MKTAG( 'm', 'p', '4', 'v' ), parse_default },
1190 { MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
1192 { MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
1193 { MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
1194 { MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
1195 { MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
1196 { MKTAG( 'm', 'a', 't', 't' ), parse_default },
1197 { MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
1198 { MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
1199 { MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
1200 //{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
1201 { MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
1202 { MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
1203 { MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
1204 { MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
1205 { MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
1206 { MKTAG( 'w', 'i', 'd', 'e' ), parse_wide }, /* place holder */
1207 { MKTAG( 'c', 't', 'a', 'b' ), parse_ctab },
1208 { MKTAG( 'e', 's', 'd', 's' ), parse_esds },
1210 { MKTAG( 'c', 'm', 'o', 'v' ), parse_cmov },
1212 { MKTAG( 'c', 'm', 'o', 'v' ), parse_leaf },
1217 static void mov_free_stream_context(MOVStreamContext *sc)
1220 av_free(sc->chunk_offsets);
1221 av_free(sc->sample_to_chunk);
1222 av_free(sc->sample_sizes);
1223 av_free(sc->header_data);
1228 static inline uint32_t to_tag(uint8_t *buf)
1230 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
1233 static inline uint32_t to_be32(uint8_t *buf)
1235 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1238 /* XXX: is it suffisant ? */
1239 static int mov_probe(AVProbeData *p)
1241 unsigned int offset;
1244 /* check file header */
1245 if (p->buf_size <= 12)
1249 /* ignore invalid offset */
1250 if ((offset + 8) > (unsigned int)p->buf_size)
1252 tag = to_tag(p->buf + offset + 4);
1254 case MKTAG( 'm', 'o', 'o', 'v' ):
1255 case MKTAG( 'w', 'i', 'd', 'e' ):
1256 case MKTAG( 'f', 'r', 'e', 'e' ):
1257 case MKTAG( 'm', 'd', 'a', 't' ):
1258 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1259 return AVPROBE_SCORE_MAX;
1260 case MKTAG( 'f', 't', 'y', 'p' ):
1261 case MKTAG( 's', 'k', 'i', 'p' ):
1262 offset = to_be32(p->buf) + offset;
1265 /* unrecognized tag */
1272 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1274 MOVContext *mov = s->priv_data;
1275 ByteIOContext *pb = &s->pb;
1281 /* XXX: I think we should auto detect */
1282 if(s->iformat->name[1] == 'p')
1285 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1286 size = url_filesize(url_fileno(pb));
1288 size = 0x7FFFFFFFFFFFFFFF;
1291 printf("filesz=%Ld\n", size);
1294 /* check MOV header */
1295 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
1296 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1297 puts("header not found !!!");
1301 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1303 /* some cleanup : make sure we are on the mdat atom */
1304 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1305 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1307 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1310 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1314 printf("streams= %d\n", s->nb_streams);
1316 mov->total_streams = nb = s->nb_streams;
1319 for(i=0; i<s->nb_streams;) {
1320 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1321 av_free(s->streams[i]);
1322 for(j=i+1; j<s->nb_streams; j++)
1323 s->streams[j-1] = s->streams[j];
1328 for(i=0; i<s->nb_streams;i++) {
1329 MOVStreamContext *sc;
1330 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1332 sc->is_ff_stream = 1;
1336 printf("real streams= %d\n", s->nb_streams);
1341 /* Yes, this is ugly... I didn't write the specs of QT :p */
1342 /* XXX:remove useless commented code sometime */
1343 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1345 MOVContext *mov = s->priv_data;
1346 MOVStreamContext *sc;
1347 int64_t offset = 0x0FFFFFFFFFFFFFFF;
1352 #ifdef MOV_SPLIT_CHUNKS
1358 idx = sc->sample_to_chunk_index;
1360 if (idx < 0) return 0;
1361 size = sc->sample_sizes[sc->current_sample];
1363 sc->current_sample++;
1364 sc->left_in_chunk--;
1366 if (sc->left_in_chunk <= 0)
1368 offset = mov->next_chunk_offset;
1369 /* extract the sample */
1377 for(i=0; i<mov->total_streams; i++) {
1378 MOVStreamContext *msc = mov->streams[i];
1379 //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
1380 if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1381 && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1383 offset = msc->chunk_offsets[msc->next_chunk];
1384 //printf("SELETED %Ld i:%d\n", offset, i);
1387 if (!sc || offset==0x0FFFFFFFFFFFFFFF)
1392 if(mov->next_chunk_offset < offset) { /* some meta data */
1393 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1394 mov->next_chunk_offset = offset;
1397 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1398 if(!sc->is_ff_stream) {
1399 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1400 mov->next_chunk_offset = offset;
1401 offset = 0x0FFFFFFFFFFFFFFF;
1405 /* now get the chunk size... */
1407 for(i=0; i<mov->total_streams; i++) {
1408 MOVStreamContext *msc = mov->streams[i];
1409 if ((msc->next_chunk < msc->chunk_count)
1410 && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1411 size = msc->chunk_offsets[msc->next_chunk] - offset;
1413 #ifdef MOV_SPLIT_CHUNKS
1414 /* split chunks into samples */
1415 if (sc->sample_size == 0) {
1416 int idx = sc->sample_to_chunk_index;
1417 if ((idx + 1 < sc->sample_to_chunk_sz)
1418 && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1420 sc->sample_to_chunk_index = idx;
1421 if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1423 /* we'll have to get those samples before next chunk */
1424 sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1425 size = sc->sample_sizes[sc->current_sample];
1428 sc->current_sample++;
1433 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1434 if(size == 0x0FFFFFFF)
1435 size = mov->mdat_size + mov->mdat_offset - offset;
1440 url_fseek(&s->pb, offset, SEEK_SET);
1442 //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
1443 if (sc->header_len > 0) {
1444 av_new_packet(pkt, size + sc->header_len);
1445 memcpy(pkt->data, sc->header_data, sc->header_len);
1446 get_buffer(&s->pb, pkt->data + sc->header_len, size);
1448 av_freep(&sc->header_data);
1451 av_new_packet(pkt, size);
1452 get_buffer(&s->pb, pkt->data, pkt->size);
1454 pkt->stream_index = sc->ffindex;
1458 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1460 printf("%02x ", pkt->data[i]);
1462 printf("%c ", (pkt->data[i]) & 0x7F);
1467 mov->next_chunk_offset = offset + size;
1472 static int mov_read_close(AVFormatContext *s)
1475 MOVContext *mov = s->priv_data;
1476 for(i=0; i<mov->total_streams; i++)
1477 mov_free_stream_context(mov->streams[i]);
1478 for(i=0; i<s->nb_streams; i++)
1479 av_freep(&s->streams[i]);
1483 static AVInputFormat mov_iformat = {
1485 "QuickTime/MPEG4 format",
1495 av_register_input_format(&mov_iformat);