* support for AAC audio (esds decoding - using extradata)
[libav.git] / libavformat / mov.c
1 /*
2 * MOV decoder.
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19 #include "avformat.h"
20 #include "avi.h"
21
22 #ifdef CONFIG_ZLIB
23 #include <zlib.h>
24 #endif
25
26 /*
27 * First version by Francois Revol revol@free.fr
28 *
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 :-)
40 *
41 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
42 * when coding this :) (it's a writer anyway)
43 *
44 * Reference documents:
45 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
46 * Apple:
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 :))
50 */
51
52 //#define DEBUG
53
54 /* allows chunk splitting - should work now... */
55 /* in case you can't read a file, try commenting */
56 #define MOV_SPLIT_CHUNKS
57
58 /* some streams in QT (and in MP4 mostly) aren't either video nor audio */
59 /* so we first list them as this, then clean up the list of streams we give back, */
60 /* getting rid of these */
61 #define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
62
63 static const CodecTag mov_video_tags[] = {
64 /* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
65 /* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
66 /* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
67 /* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
68 /* Graphics */
69 /* Animation */
70 /* Apple video */
71 /* Kodak Photo CD */
72 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
73 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
74 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
75 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
76 { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
77 /* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
78 /* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
79 /* Sorenson video */
80 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
81 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
82 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
83 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
84 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
85 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
86 /* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
87 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
88 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
89 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
90 /* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
91 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
92 { CODEC_ID_NONE, 0 },
93 };
94
95 static const CodecTag mov_audio_tags[] = {
96 /* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
97 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
98 /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
99 { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
100 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
101 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
102 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
103 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
104 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
105 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
106
107 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
108 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
109 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
110 /* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
111 /* MP4 tags */
112 { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG 4 AAC or audio ? */
113 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
114 { CODEC_ID_NONE, 0 },
115 };
116
117 /* the QuickTime file format is quite convoluted...
118 * it has lots of index tables, each indexing something in another one...
119 * Here we just use what is needed to read the chunks
120 */
121
122 typedef struct MOV_sample_to_chunk_tbl {
123 long first;
124 long count;
125 long id;
126 } MOV_sample_to_chunk_tbl;
127
128 typedef struct {
129 uint32_t type;
130 int64_t offset;
131 int64_t size; /* total size (excluding the size and type fields) */
132 } MOV_atom_t;
133
134 typedef struct {
135 int seed;
136 int flags;
137 int size;
138 void* clrs;
139 } MOV_ctab_t;
140
141 typedef struct {
142 uint8_t version;
143 uint32_t flags; // 24bit
144
145 /* 0x03 ESDescrTag */
146 uint16_t es_id;
147 #define MP4ODescrTag 0x01
148 #define MP4IODescrTag 0x02
149 #define MP4ESDescrTag 0x03
150 #define MP4DecConfigDescrTag 0x04
151 #define MP4DecSpecificDescrTag 0x05
152 #define MP4SLConfigDescrTag 0x06
153 #define MP4ContentIdDescrTag 0x07
154 #define MP4SupplContentIdDescrTag 0x08
155 #define MP4IPIPtrDescrTag 0x09
156 #define MP4IPMPPtrDescrTag 0x0A
157 #define MP4IPMPDescrTag 0x0B
158 #define MP4RegistrationDescrTag 0x0D
159 #define MP4ESIDIncDescrTag 0x0E
160 #define MP4ESIDRefDescrTag 0x0F
161 #define MP4FileIODescrTag 0x10
162 #define MP4FileODescrTag 0x11
163 #define MP4ExtProfileLevelDescrTag 0x13
164 #define MP4ExtDescrTagsStart 0x80
165 #define MP4ExtDescrTagsEnd 0xFE
166 uint8_t stream_priority;
167
168 /* 0x04 DecConfigDescrTag */
169 uint8_t object_type_id;
170 uint8_t stream_type;
171 /* XXX: really streamType is
172 * only 6bit, followed by:
173 * 1bit upStream
174 * 1bit reserved
175 */
176 uint32_t buffer_size_db; // 24
177 uint32_t max_bitrate;
178 uint32_t avg_bitrate;
179
180 /* 0x05 DecSpecificDescrTag */
181 uint8_t decoder_cfg_len;
182 uint8_t *decoder_cfg;
183
184 /* 0x06 SLConfigDescrTag */
185 uint8_t sl_config_len;
186 uint8_t *sl_config;
187 } MOV_esds_t;
188
189 struct MOVParseTableEntry;
190
191 typedef struct MOVStreamContext {
192 int ffindex; /* the ffmpeg stream id */
193 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
194 long next_chunk;
195 long chunk_count;
196 int64_t *chunk_offsets;
197 long sample_to_chunk_sz;
198 MOV_sample_to_chunk_tbl *sample_to_chunk;
199 long sample_to_chunk_index;
200 long sample_size;
201 long sample_count;
202 long *sample_sizes;
203 int time_scale;
204 long current_sample;
205 long left_in_chunk; /* how many samples before next chunk */
206 /* specific MPEG4 header which is added at the beginning of the stream */
207 int header_len;
208 uint8_t *header_data;
209 MOV_esds_t esds;
210 } MOVStreamContext;
211
212 typedef struct MOVContext {
213 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) */
214 AVFormatContext *fc;
215 int time_scale;
216 int duration; /* duration of the longest track */
217 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
218 int found_mdat; /* we suppose we have enough data to read the file */
219 int64_t mdat_size;
220 int64_t mdat_offset;
221 int total_streams;
222 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
223 * but we need the info to be able to skip data from those streams in the 'mdat' section
224 */
225 MOVStreamContext *streams[MAX_STREAMS];
226
227 int64_t next_chunk_offset;
228 MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
229 int ctab_size;
230 MOV_ctab_t **ctab; /* color tables */
231 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
232 /* NOTE: for recursion save to/ restore from local variable! */
233 } MOVContext;
234
235
236 /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
237
238 /* those functions parse an atom */
239 /* return code:
240 1: found what I wanted, exit
241 0: continue to parse next atom
242 -1: error occured, exit
243 */
244 typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
245
246 /* links atom IDs to parse functions */
247 typedef struct MOVParseTableEntry {
248 uint32_t type;
249 mov_parse_function func;
250 } MOVParseTableEntry;
251
252 #ifdef DEBUG
253 /*
254 * XXX: static sux, even more in a multithreaded environment...
255 * Avoid them. This is here just to help debugging.
256 */
257 static int debug_indent = 0;
258 void print_atom(const char *str, MOV_atom_t atom)
259 {
260 unsigned int tag, i;
261 tag = (unsigned int) atom.type;
262 i=debug_indent;
263 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
264 while(i--)
265 printf("|");
266 printf("parse:");
267 printf(" %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
268 str, tag & 0xff,
269 (tag >> 8) & 0xff,
270 (tag >> 16) & 0xff,
271 (tag >> 24) & 0xff,
272 (unsigned int)atom.offset,
273 (unsigned int)atom.size);
274 assert((unsigned int)atom.size < 0x7fffffff);// catching errors
275 }
276 #else
277 #define print_atom(a,b)
278 #endif
279
280
281 static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
282 {
283 print_atom("leaf", atom);
284
285 if (atom.size>1)
286 url_fskip(pb, atom.size);
287 /* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
288 return 0;
289 }
290
291 static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
292 {
293 int64_t total_size = 0;
294 MOV_atom_t a;
295 int i;
296 int err = 0;
297
298 #ifdef DEBUG
299 print_atom("default", atom);
300 debug_indent++;
301 #endif
302
303 a.offset = atom.offset;
304
305 if(atom.size < 0)
306 atom.size = 0x0FFFFFFFFFFFFFFF;
307 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
308 a.size = atom.size;
309 a.type=0L;
310 if(atom.size >= 8) {
311 a.size = get_be32(pb);
312 a.type = get_le32(pb);
313 }
314 total_size += 8;
315 a.offset += 8;
316 //printf("type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
317 if (a.size == 1) { /* 64 bit extended size */
318 a.size = get_be64(pb) - 8;
319 a.offset += 8;
320 total_size += 8;
321 }
322 if (a.size == 0) {
323 a.size = atom.size - total_size;
324 if (a.size <= 8)
325 break;
326 }
327 for (i = 0; c->parse_table[i].type != 0L
328 && c->parse_table[i].type != a.type; i++)
329 /* empty */;
330
331 a.size -= 8;
332 // printf(" i=%ld\n", i);
333 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
334 // url_seek(pb, atom.offset+atom.size, SEEK_SET);
335 #ifdef DEBUG
336 print_atom("unknown", a);
337 #endif
338 url_fskip(pb, a.size);
339 } else {
340 #ifdef DEBUG
341 //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
342 //print_atom(b, type, offset, size);
343 #endif
344 err = (c->parse_table[i].func)(c, pb, a);
345 }
346
347 a.offset += a.size;
348 total_size += a.size;
349 }
350
351 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
352 //printf("RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
353 url_fskip(pb, atom.size - total_size);
354 }
355
356 #ifdef DEBUG
357 debug_indent--;
358 #endif
359 return err;
360 }
361
362 static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
363 {
364 unsigned int len;
365 MOV_ctab_t *t;
366 //url_fskip(pb, atom.size); // for now
367 c->ctab = av_realloc(c->ctab, ++c->ctab_size);
368 t = c->ctab[c->ctab_size];
369 t->seed = get_be32(pb);
370 t->flags = get_be16(pb);
371 t->size = get_be16(pb) + 1;
372 len = 2 * t->size * 4;
373 if (len > 0) {
374 t->clrs = av_malloc(len); // 16bit A R G B
375 if (t->clrs)
376 get_buffer(pb, t->clrs, len);
377 }
378
379 return 0;
380 }
381
382 static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
383 {
384 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
385 int len = 0;
386 uint8_t *buf;
387 uint32_t type;
388 uint32_t ctype;
389
390 print_atom("hdlr", atom);
391
392 get_byte(pb); /* version */
393 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
394
395 /* component type */
396 ctype = get_le32(pb);
397 type = get_le32(pb); /* component subtype */
398
399 #ifdef DEBUG
400 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
401 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
402 #endif
403 #ifdef DEBUG
404 /* XXX: yeah this is ugly... */
405 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
406 if(type == MKTAG('v', 'i', 'd', 'e'))
407 puts("hdlr: vide");
408 else if(type == MKTAG('s', 'o', 'u', 'n'))
409 puts("hdlr: soun");
410 } else if(ctype == 0) { /* MP4 */
411 if(type == MKTAG('v', 'i', 'd', 'e'))
412 puts("hdlr: vide");
413 else if(type == MKTAG('s', 'o', 'u', 'n'))
414 puts("hdlr: soun");
415 else if(type == MKTAG('o', 'd', 's', 'm'))
416 puts("hdlr: odsm");
417 else if(type == MKTAG('s', 'd', 's', 'm'))
418 puts("hdlr: sdsm");
419 } else puts("hdlr: meta");
420 #endif
421
422 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
423 /* helps parsing the string hereafter... */
424 c->mp4 = 0;
425 if(type == MKTAG('v', 'i', 'd', 'e'))
426 st->codec.codec_type = CODEC_TYPE_VIDEO;
427 else if(type == MKTAG('s', 'o', 'u', 'n'))
428 st->codec.codec_type = CODEC_TYPE_AUDIO;
429 } else if(ctype == 0) { /* MP4 */
430 /* helps parsing the string hereafter... */
431 c->mp4 = 1;
432 if(type == MKTAG('v', 'i', 'd', 'e'))
433 st->codec.codec_type = CODEC_TYPE_VIDEO;
434 else if(type == MKTAG('s', 'o', 'u', 'n'))
435 st->codec.codec_type = CODEC_TYPE_AUDIO;
436 }
437 get_be32(pb); /* component manufacture */
438 get_be32(pb); /* component flags */
439 get_be32(pb); /* component flags mask */
440
441 if(atom.size <= 24)
442 return 0; /* nothing left to read */
443 /* XXX: MP4 uses a C string, not a pascal one */
444 /* component name */
445
446 if(c->mp4) {
447 /* .mp4: C string */
448 while(get_byte(pb) && (++len < (atom.size - 24)));
449 } else {
450 /* .mov: PASCAL string */
451 len = get_byte(pb);
452 #ifdef DEBUG
453 buf = (uint8_t*) av_malloc(len+1);
454 if (buf) {
455 get_buffer(pb, buf, len);
456 buf[len] = '\0';
457 printf("**buf='%s'\n", buf);
458 av_free(buf);
459 } else
460 #endif
461 url_fskip(pb, len);
462 }
463
464 return 0;
465 }
466
467 static int mov_mp4_read_descr_len(ByteIOContext *pb)
468 {
469 int len = 0;
470 int count = 4;
471 while (count--) {
472 int c = get_byte(pb);
473 len = (len << 7) | (c & 0x7f);
474 if (!(c & 0x80))
475 break;
476 }
477 return len;
478 }
479
480 static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
481 {
482 int len;
483 *tag = get_byte(pb);
484 len = mov_mp4_read_descr_len(pb);
485 #ifdef DEBUG
486 printf("MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
487 #endif
488 return len;
489 }
490
491 static inline unsigned int get_be24(ByteIOContext *s)
492 {
493 unsigned int val;
494 val = get_byte(s) << 16;
495 val |= get_byte(s) << 8;
496 val |= get_byte(s);
497 return val;
498 }
499
500 static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
501 {
502 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
503 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
504 int64_t start_pos = url_ftell(pb);
505 int tag, len;
506
507 print_atom("esds", atom);
508
509 /* Well, broken but suffisant for some MP4 streams */
510 get_be32(pb); /* version + flags */
511 len = mov_mp4_read_descr(pb, &tag);
512 if (tag == MP4ESDescrTag) {
513 get_be16(pb); /* ID */
514 get_byte(pb); /* priority */
515 } else
516 get_be16(pb); /* ID */
517
518 len = mov_mp4_read_descr(pb, &tag);
519 if (tag == MP4DecConfigDescrTag) {
520 sc->esds.object_type_id = get_byte(pb);
521 sc->esds.stream_type = get_byte(pb);
522 sc->esds.buffer_size_db = get_be24(pb);
523 sc->esds.max_bitrate = get_be32(pb);
524 sc->esds.avg_bitrate = get_be32(pb);
525
526 len = mov_mp4_read_descr(pb, &tag);
527 //printf("LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
528 if (tag == MP4DecSpecificDescrTag) {
529 #ifdef DEBUG
530 printf("Specific MPEG4 header len=%d\n", len);
531 #endif
532 st->codec.extradata = (uint8_t*) av_mallocz(len);
533 if (st->codec.extradata) {
534 get_buffer(pb, st->codec.extradata, len);
535 st->codec.extradata_size = len;
536 }
537 }
538 }
539 /* in any case, skip garbage */
540 url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
541 return 0;
542 }
543
544 /* this atom contains actual media data */
545 static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
546 {
547 print_atom("mdat", atom);
548
549 if(atom.size == 0) /* wrong one (MP4) */
550 return 0;
551 c->found_mdat=1;
552 c->mdat_offset = atom.offset;
553 c->mdat_size = atom.size;
554 if(c->found_moov)
555 return 1; /* found both, just go */
556 url_fskip(pb, atom.size);
557 return 0; /* now go for moov */
558 }
559
560 /* this atom should contain all header atoms */
561 static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
562 {
563 int err;
564
565 print_atom("moov", atom);
566
567 err = mov_read_default(c, pb, atom);
568 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
569 /* so we don't parse the whole file if over a network */
570 c->found_moov=1;
571 if(c->found_mdat)
572 return 1; /* found both, just go */
573 return 0; /* now go for mdat */
574 }
575
576
577 static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
578 {
579 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
580
581 print_atom("mdhd", atom);
582
583 get_byte(pb); /* version */
584
585 get_byte(pb); get_byte(pb);
586 get_byte(pb); /* flags */
587
588 get_be32(pb); /* creation time */
589 get_be32(pb); /* modification time */
590
591 c->streams[c->total_streams]->time_scale = get_be32(pb);
592
593 #ifdef DEBUG
594 printf("track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->total_streams]->time_scale); /* time scale */
595 #endif
596 get_be32(pb); /* duration */
597
598 get_be16(pb); /* language */
599 get_be16(pb); /* quality */
600
601 return 0;
602 }
603
604 static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
605 {
606 print_atom("mvhd", atom);
607
608 get_byte(pb); /* version */
609 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
610
611 get_be32(pb); /* creation time */
612 get_be32(pb); /* modification time */
613 c->time_scale = get_be32(pb); /* time scale */
614 #ifdef DEBUG
615 printf("time scale = %i\n", c->time_scale);
616 #endif
617 c->duration = get_be32(pb); /* duration */
618 get_be32(pb); /* preferred scale */
619
620 get_be16(pb); /* preferred volume */
621
622 url_fskip(pb, 10); /* reserved */
623
624 url_fskip(pb, 36); /* display matrix */
625
626 get_be32(pb); /* preview time */
627 get_be32(pb); /* preview duration */
628 get_be32(pb); /* poster time */
629 get_be32(pb); /* selection time */
630 get_be32(pb); /* selection duration */
631 get_be32(pb); /* current time */
632 get_be32(pb); /* next track ID */
633
634 return 0;
635 }
636
637
638 static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
639 {
640 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
641 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
642 int entries, i;
643
644 print_atom("stco", atom);
645
646 get_byte(pb); /* version */
647 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
648
649 entries = get_be32(pb);
650 sc->chunk_count = entries;
651 sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
652 if (!sc->chunk_offsets)
653 return -1;
654 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
655 for(i=0; i<entries; i++) {
656 sc->chunk_offsets[i] = get_be32(pb);
657 }
658 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
659 for(i=0; i<entries; i++) {
660 sc->chunk_offsets[i] = get_be64(pb);
661 }
662 } else
663 return -1;
664 #ifdef DEBUG
665 /*
666 for(i=0; i<entries; i++) {
667 printf("chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
668 }
669 */
670 #endif
671 return 0;
672 }
673
674 static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
675 {
676 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
677 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
678 int entries, frames_per_sample;
679 uint32_t format;
680
681 print_atom("stsd", atom);
682
683 get_byte(pb); /* version */
684 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
685
686 entries = get_be32(pb);
687
688 while(entries--) {
689 enum CodecID id;
690 int size = get_be32(pb); /* size */
691 format = get_le32(pb); /* data format */
692
693 get_be32(pb); /* reserved */
694 get_be16(pb); /* reserved */
695 get_be16(pb); /* index */
696
697 /* for MPEG4: set codec type by looking for it */
698 id = codec_get_id(mov_video_tags, format);
699 if (id >= 0) {
700 AVCodec *codec;
701 codec = avcodec_find_decoder(id);
702 if (codec)
703 st->codec.codec_type = codec->type;
704 }
705 #ifdef DEBUG
706 printf("size=%d 4CC= %c%c%c%c codec_type=%d\n",
707 size,
708 (format >> 0) & 0xff,
709 (format >> 8) & 0xff,
710 (format >> 16) & 0xff,
711 (format >> 24) & 0xff,
712 st->codec.codec_type);
713 #endif
714 st->codec.codec_tag = format;
715 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
716 MOV_atom_t a = { 0, 0, 0 };
717 st->codec.codec_id = id;
718 get_be16(pb); /* version */
719 get_be16(pb); /* revision level */
720 get_be32(pb); /* vendor */
721 get_be32(pb); /* temporal quality */
722 get_be32(pb); /* spacial quality */
723 st->codec.width = get_be16(pb); /* width */
724 st->codec.height = get_be16(pb); /* height */
725 #if 1
726 if (st->codec.codec_id == CODEC_ID_MPEG4) {
727 /* in some MPEG4 the width/height are not correct, so
728 we ignore this info */
729 st->codec.width = 0;
730 st->codec.height = 0;
731 }
732 #endif
733 get_be32(pb); /* horiz resolution */
734 get_be32(pb); /* vert resolution */
735 get_be32(pb); /* data size, always 0 */
736 frames_per_sample = get_be16(pb); /* frames per samples */
737 #ifdef DEBUG
738 printf("frames/samples = %d\n", frames_per_sample);
739 #endif
740 get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
741
742 st->codec.bits_per_sample = get_be16(pb); /* depth */
743 st->codec.color_table_id = get_be16(pb); /* colortable id */
744
745 st->codec.frame_rate = 25;
746 st->codec.frame_rate_base = 1;
747
748 size -= (16+8*4+2+32+2*2);
749 #if 0
750 while (size >= 8) {
751 MOV_atom_t a;
752 int64_t start_pos;
753
754 a.size = get_be32(pb);
755 a.type = get_le32(pb);
756 size -= 8;
757 #ifdef DEBUG
758 printf("VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
759 (a.type >> 0) & 0xff,
760 (a.type >> 8) & 0xff,
761 (a.type >> 16) & 0xff,
762 (a.type >> 24) & 0xff,
763 a.size, size);
764 #endif
765 start_pos = url_ftell(pb);
766
767 switch(a.type) {
768 case MKTAG('e', 's', 'd', 's'):
769 {
770 int tag, len;
771 /* Well, broken but suffisant for some MP4 streams */
772 get_be32(pb); /* version + flags */
773 len = mov_mp4_read_descr(pb, &tag);
774 if (tag == 0x03) {
775 /* MP4ESDescrTag */
776 get_be16(pb); /* ID */
777 get_byte(pb); /* priority */
778 len = mov_mp4_read_descr(pb, &tag);
779 if (tag != 0x04)
780 goto fail;
781 /* MP4DecConfigDescrTag */
782 get_byte(pb); /* objectTypeId */
783 get_be32(pb); /* streamType + buffer size */
784 get_be32(pb); /* max bit rate */
785 get_be32(pb); /* avg bit rate */
786 len = mp4_read_descr(pb, &tag);
787 if (tag != 0x05)
788 goto fail;
789 /* MP4DecSpecificDescrTag */
790 #ifdef DEBUG
791 printf("Specific MPEG4 header len=%d\n", len);
792 #endif
793 sc->header_data = av_mallocz(len);
794 if (sc->header_data) {
795 get_buffer(pb, sc->header_data, len);
796 sc->header_len = len;
797 }
798 }
799 /* in any case, skip garbage */
800 }
801 break;
802 default:
803 break;
804 }
805 fail:
806 printf("ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
807 if (atom.size > 8) {
808 url_fskip(pb, (atom.size - 8) -
809 ((url_ftell(pb) - start_pos)));
810 size -= atom.size - 8;
811 }
812 }
813 if (size > 0) {
814 /* unknown extension */
815 url_fskip(pb, size);
816 }
817 #else
818 a.size = size;
819 mov_read_default(c, pb, a);
820 #endif
821 } else {
822 get_be16(pb); /* version */
823 get_be16(pb); /* revision level */
824 get_be32(pb); /* vendor */
825
826 st->codec.channels = get_be16(pb); /* channel count */
827 st->codec.bits_per_sample = get_be16(pb); /* sample size */
828
829 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
830 /* handle specific s8 codec */
831 get_be16(pb); /* compression id = 0*/
832 get_be16(pb); /* packet size = 0 */
833
834 st->codec.sample_rate = ((get_be32(pb) >> 16));
835 //printf("CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
836
837 switch (st->codec.codec_id) {
838 case CODEC_ID_PCM_S16BE:
839 if (st->codec.bits_per_sample == 8)
840 st->codec.codec_id = CODEC_ID_PCM_S8;
841 /* fall */
842 case CODEC_ID_PCM_U8:
843 st->codec.bit_rate = st->codec.sample_rate * 8;
844 break;
845 default:
846 ;
847 }
848 get_be32(pb); /* samples per packet */
849 get_be32(pb); /* bytes per packet */
850 get_be32(pb); /* bytes per frame */
851 get_be32(pb); /* bytes per sample */
852
853 {
854 MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
855 mov_read_default(c, pb, a);
856 }
857 }
858 }
859
860 return 0;
861 }
862
863 static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
864 {
865 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
866 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
867 int entries, i;
868
869 print_atom("stsc", atom);
870
871 get_byte(pb); /* version */
872 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
873
874 entries = get_be32(pb);
875 #ifdef DEBUG
876 printf("track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
877 #endif
878 sc->sample_to_chunk_sz = entries;
879 sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
880 if (!sc->sample_to_chunk)
881 return -1;
882 for(i=0; i<entries; i++) {
883 sc->sample_to_chunk[i].first = get_be32(pb);
884 sc->sample_to_chunk[i].count = get_be32(pb);
885 sc->sample_to_chunk[i].id = get_be32(pb);
886 #ifdef DEBUG
887 /* 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); */
888 #endif
889 }
890 return 0;
891 }
892
893 static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
894 {
895 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
896 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
897 int entries, i;
898
899 print_atom("stsz", atom);
900
901 get_byte(pb); /* version */
902 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
903
904 sc->sample_size = get_be32(pb);
905 entries = get_be32(pb);
906 sc->sample_count = entries;
907 #ifdef DEBUG
908 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
909 #endif
910 if(sc->sample_size)
911 return 0; /* there isn't any table following */
912 sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
913 if (!sc->sample_sizes)
914 return -1;
915 for(i=0; i<entries; i++) {
916 sc->sample_sizes[i] = get_be32(pb);
917 #ifdef DEBUG
918 /* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
919 #endif
920 }
921 return 0;
922 }
923
924 static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
925 {
926 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
927 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
928 int entries, i;
929
930 print_atom("stts", atom);
931
932 get_byte(pb); /* version */
933 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
934 entries = get_be32(pb);
935 #ifdef DEBUG
936 printf("track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
937 #endif
938 for(i=0; i<entries; i++) {
939 int sample_duration;
940
941 get_be32(pb);
942 sample_duration = get_be32(pb);
943
944 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
945 st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
946 st->codec.frame_rate = c->streams[c->total_streams]->time_scale;
947 #ifdef DEBUG
948 printf("VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
949 #endif
950 }
951 }
952 return 0;
953 }
954
955 static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
956 {
957 AVStream *st;
958 MOVStreamContext *sc;
959
960 print_atom("trak", atom);
961
962 st = av_new_stream(c->fc, c->fc->nb_streams);
963 if (!st) return -2;
964 sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
965 if (!sc) {
966 av_free(st);
967 return -1;
968 }
969
970 sc->sample_to_chunk_index = -1;
971 st->priv_data = sc;
972 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
973 st->time_length = (c->duration * 1000) / c->time_scale; // time in miliseconds
974 c->streams[c->fc->nb_streams-1] = sc;
975
976 return mov_read_default(c, pb, atom);
977 }
978
979 static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
980 {
981 AVStream *st;
982
983 print_atom("tkhd", atom);
984
985 st = c->fc->streams[c->fc->nb_streams-1];
986
987 get_byte(pb); /* version */
988
989 get_byte(pb); get_byte(pb);
990 get_byte(pb); /* flags */
991 /*
992 MOV_TRACK_ENABLED 0x0001
993 MOV_TRACK_IN_MOVIE 0x0002
994 MOV_TRACK_IN_PREVIEW 0x0004
995 MOV_TRACK_IN_POSTER 0x0008
996 */
997
998 get_be32(pb); /* creation time */
999 get_be32(pb); /* modification time */
1000 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1001 get_be32(pb); /* reserved */
1002 st->time_length = get_be32(pb) * 1000 / c->time_scale; /* duration */
1003 get_be32(pb); /* reserved */
1004 get_be32(pb); /* reserved */
1005
1006 get_be16(pb); /* layer */
1007 get_be16(pb); /* alternate group */
1008 get_be16(pb); /* volume */
1009 get_be16(pb); /* reserved */
1010
1011 url_fskip(pb, 36); /* display matrix */
1012
1013 /* those are fixed-point */
1014 st->codec.width = get_be32(pb) >> 16; /* track width */
1015 st->codec.height = get_be32(pb) >> 16; /* track height */
1016
1017 return 0;
1018 }
1019
1020 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1021 /* like the files created with Adobe Premiere 5.0, for samples see */
1022 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1023 static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1024 {
1025 int err;
1026 uint32_t type;
1027
1028 #ifdef DEBUG
1029 print_atom("wide", atom);
1030 debug_indent++;
1031 #endif
1032 if (atom.size < 8)
1033 return 0; /* continue */
1034 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1035 url_fskip(pb, atom.size - 4);
1036 return 0;
1037 }
1038 atom.type = get_le32(pb);
1039 atom.offset += 8;
1040 atom.size -= 8;
1041 if (type != MKTAG('m', 'd', 'a', 't')) {
1042 url_fskip(pb, atom.size);
1043 return 0;
1044 }
1045 err = mov_read_mdat(c, pb, atom);
1046 #ifdef DEBUG
1047 debug_indent--;
1048 #endif
1049 return err;
1050 }
1051
1052
1053 #ifdef CONFIG_ZLIB
1054 static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
1055 {
1056 return -1;
1057 }
1058
1059 static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1060 {
1061 ByteIOContext ctx;
1062 uint8_t *cmov_data;
1063 uint8_t *moov_data; /* uncompressed data */
1064 long cmov_len, moov_len;
1065 int ret;
1066
1067 print_atom("cmov", atom);
1068
1069 get_be32(pb); /* dcom atom */
1070 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1071 return -1;
1072 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
1073 dprintf("unknown compression for cmov atom !");
1074 return -1;
1075 }
1076 get_be32(pb); /* cmvd atom */
1077 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1078 return -1;
1079 moov_len = get_be32(pb); /* uncompressed size */
1080 cmov_len = atom.size - 6 * 4;
1081
1082 cmov_data = (uint8_t *) av_malloc(cmov_len);
1083 if (!cmov_data)
1084 return -1;
1085 moov_data = (uint8_t *) av_malloc(moov_len);
1086 if (!moov_data) {
1087 av_free(cmov_data);
1088 return -1;
1089 }
1090 get_buffer(pb, cmov_data, cmov_len);
1091 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
1092 return -1;
1093 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1094 return -1;
1095 ctx.buf_end = ctx.buffer + moov_len;
1096 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1097 atom.offset = 0;
1098 atom.size = moov_len;
1099 ret = mov_read_default(c, &ctx, atom);
1100 av_free(moov_data);
1101 av_free(cmov_data);
1102 return ret;
1103 }
1104 #endif
1105
1106 static const MOVParseTableEntry mov_default_parse_table[] = {
1107 /* mp4 atoms */
1108 { MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1109 { MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
1110 { MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
1111 { MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
1112 { MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
1113 { MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
1114 { MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
1115 { MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
1116 { MKTAG( 'e', 'l', 's', 't' ), mov_read_leaf },
1117 { MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
1118 { MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1119 { MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
1120 { MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
1121 { MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
1122 { MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1123 { MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1124 { MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1125 { MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1126 { MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1127 { MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
1128 { MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
1129 { MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
1130 { MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
1131 { MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1132 { MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
1133 { MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
1134 { MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
1135 { MKTAG( 's', 'k', 'i', 'p' ), mov_read_default },
1136 { MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
1137 { MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1138 { MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1139 { MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
1140 { MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1141 { MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1142 { MKTAG( 's', 't', 's', 'h' ), mov_read_default },
1143 { MKTAG( 's', 't', 's', 's' ), mov_read_leaf }, /* sync sample */
1144 { MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1145 { MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1146 { MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1147 { MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1148 { MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
1149 { MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
1150 { MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
1151 { MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
1152 { MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
1153 { MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
1154 { MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
1155 /* extra mp4 */
1156 { MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
1157 /* QT atoms */
1158 { MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
1159 { MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
1160 { MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
1161 { MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1162 { MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1163 { MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
1164 { MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
1165 { MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
1166 { MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
1167 { MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
1168 { MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
1169 { MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
1170 { MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
1171 { MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
1172 { MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
1173 { MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1174 //{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
1175 #ifdef CONFIG_ZLIB
1176 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
1177 #else
1178 { MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
1179 #endif
1180 { 0L, mov_read_leaf }
1181 };
1182
1183 static void mov_free_stream_context(MOVStreamContext *sc)
1184 {
1185 if(sc) {
1186 av_free(sc->chunk_offsets);
1187 av_free(sc->sample_to_chunk);
1188 av_free(sc->sample_sizes);
1189 av_free(sc->header_data);
1190 av_free(sc);
1191 }
1192 }
1193
1194 static inline uint32_t mov_to_tag(uint8_t *buf)
1195 {
1196 return MKTAG(buf[0], buf[1], buf[2], buf[3]);
1197 }
1198
1199 static inline uint32_t to_be32(uint8_t *buf)
1200 {
1201 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1202 }
1203
1204 /* XXX: is it sufficient ? */
1205 static int mov_probe(AVProbeData *p)
1206 {
1207 unsigned int offset;
1208 uint32_t tag;
1209
1210 /* check file header */
1211 if (p->buf_size <= 12)
1212 return 0;
1213 offset = 0;
1214 for(;;) {
1215 /* ignore invalid offset */
1216 if ((offset + 8) > (unsigned int)p->buf_size)
1217 return 0;
1218 tag = mov_to_tag(p->buf + offset + 4);
1219 switch(tag) {
1220 case MKTAG( 'm', 'o', 'o', 'v' ):
1221 case MKTAG( 'w', 'i', 'd', 'e' ):
1222 case MKTAG( 'f', 'r', 'e', 'e' ):
1223 case MKTAG( 'm', 'd', 'a', 't' ):
1224 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
1225 return AVPROBE_SCORE_MAX;
1226 case MKTAG( 'f', 't', 'y', 'p' ):
1227 case MKTAG( 's', 'k', 'i', 'p' ):
1228 offset = to_be32(p->buf) + offset;
1229 break;
1230 default:
1231 /* unrecognized tag */
1232 return 0;
1233 }
1234 }
1235 return 0;
1236 }
1237
1238 static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
1239 {
1240 MOVContext *mov = (MOVContext *) s->priv_data;
1241 ByteIOContext *pb = &s->pb;
1242 int i, j, nb, err;
1243 MOV_atom_t atom = { 0, 0, 0 };
1244
1245 mov->fc = s;
1246 mov->parse_table = mov_default_parse_table;
1247 #if 0
1248 /* XXX: I think we should auto detect */
1249 if(s->iformat->name[1] == 'p')
1250 mov->mp4 = 1;
1251 #endif
1252 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1253 atom.size = url_filesize(url_fileno(pb));
1254 else
1255 atom.size = 0x7FFFFFFFFFFFFFFF;
1256
1257 #ifdef DEBUG
1258 printf("filesz=%Ld\n", atom.size);
1259 #endif
1260
1261 /* check MOV header */
1262 err = mov_read_default(mov, pb, atom);
1263 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
1264 puts("header not found !!!");
1265 exit(1);
1266 }
1267 #ifdef DEBUG
1268 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
1269 #endif
1270 /* some cleanup : make sure we are on the mdat atom */
1271 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1272 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1273
1274 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1275
1276 #ifdef DEBUG
1277 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
1278 #endif
1279
1280 #ifdef DEBUG
1281 printf("streams= %d\n", s->nb_streams);
1282 #endif
1283 mov->total_streams = nb = s->nb_streams;
1284
1285 #if 1
1286 for(i=0; i<s->nb_streams;) {
1287 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1288 av_free(s->streams[i]);
1289 for(j=i+1; j<s->nb_streams; j++)
1290 s->streams[j-1] = s->streams[j];
1291 s->nb_streams--;
1292 } else
1293 i++;
1294 }
1295 for(i=0; i<s->nb_streams;i++) {
1296 MOVStreamContext *sc;
1297 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1298 sc->ffindex = i;
1299 sc->is_ff_stream = 1;
1300 }
1301 #endif
1302 #ifdef DEBUG
1303 printf("real streams= %d\n", s->nb_streams);
1304 #endif
1305 return 0;
1306 }
1307
1308 /* Yes, this is ugly... I didn't write the specs of QT :p */
1309 /* XXX:remove useless commented code sometime */
1310 static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
1311 {
1312 MOVContext *mov = (MOVContext *) s->priv_data;
1313 MOVStreamContext *sc;
1314 int64_t offset = 0x0FFFFFFFFFFFFFFF;
1315 int i;
1316 int size;
1317 size = 0x0FFFFFFF;
1318
1319 #ifdef MOV_SPLIT_CHUNKS
1320 if (mov->partial) {
1321
1322 int idx;
1323
1324 sc = mov->partial;
1325 idx = sc->sample_to_chunk_index;
1326
1327 if (idx < 0) return 0;
1328 size = sc->sample_sizes[sc->current_sample];
1329
1330 sc->current_sample++;
1331 sc->left_in_chunk--;
1332
1333 if (sc->left_in_chunk <= 0)
1334 mov->partial = 0;
1335 offset = mov->next_chunk_offset;
1336 /* extract the sample */
1337
1338 goto readchunk;
1339 }
1340 #endif
1341
1342 again:
1343 sc = 0;
1344 for(i=0; i<mov->total_streams; i++) {
1345 MOVStreamContext *msc = mov->streams[i];
1346 //printf("MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
1347 if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1348 && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1349 sc = msc;
1350 offset = msc->chunk_offsets[msc->next_chunk];
1351 //printf("SELETED %Ld i:%d\n", offset, i);
1352 }
1353 }
1354 if (!sc || offset==0x0FFFFFFFFFFFFFFF)
1355 return -1;
1356
1357 sc->next_chunk++;
1358
1359 if(mov->next_chunk_offset < offset) { /* some meta data */
1360 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1361 mov->next_chunk_offset = offset;
1362 }
1363
1364 //printf("chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
1365 if(!sc->is_ff_stream) {
1366 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
1367 mov->next_chunk_offset = offset;
1368 offset = 0x0FFFFFFFFFFFFFFF;
1369 goto again;
1370 }
1371
1372 /* now get the chunk size... */
1373
1374 for(i=0; i<mov->total_streams; i++) {
1375 MOVStreamContext *msc = mov->streams[i];
1376 if ((msc->next_chunk < msc->chunk_count)
1377 && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1378 size = msc->chunk_offsets[msc->next_chunk] - offset;
1379 }
1380 #ifdef MOV_SPLIT_CHUNKS
1381 /* split chunks into samples */
1382 if (sc->sample_size == 0) {
1383 int idx = sc->sample_to_chunk_index;
1384 if ((idx + 1 < sc->sample_to_chunk_sz)
1385 && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1386 idx++;
1387 sc->sample_to_chunk_index = idx;
1388 if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1389 mov->partial = sc;
1390 /* we'll have to get those samples before next chunk */
1391 sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1392 size = sc->sample_sizes[sc->current_sample];
1393 }
1394
1395 sc->current_sample++;
1396 }
1397 #endif
1398
1399 readchunk:
1400 //printf("chunk: [%i] %lli -> %lli (%i)\n", st_id, offset, offset + size, size);
1401 if(size == 0x0FFFFFFF)
1402 size = mov->mdat_size + mov->mdat_offset - offset;
1403 if(size < 0)
1404 return -1;
1405 if(size == 0)
1406 return -1;
1407 url_fseek(&s->pb, offset, SEEK_SET);
1408
1409 //printf("READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
1410 if (sc->header_len > 0) {
1411 av_new_packet(pkt, size + sc->header_len);
1412 memcpy(pkt->data, sc->header_data, sc->header_len);
1413 get_buffer(&s->pb, pkt->data + sc->header_len, size);
1414 /* free header */
1415 av_freep(&sc->header_data);
1416 sc->header_len = 0;
1417 } else {
1418 av_new_packet(pkt, size);
1419 get_buffer(&s->pb, pkt->data, pkt->size);
1420 }
1421 pkt->stream_index = sc->ffindex;
1422
1423 #ifdef DEBUG
1424 /*
1425 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
1426 for(i=0; i<8; i++)
1427 printf("%02x ", pkt->data[i]);
1428 for(i=0; i<8; i++)
1429 printf("%c ", (pkt->data[i]) & 0x7F);
1430 puts("");
1431 */
1432 #endif
1433
1434 mov->next_chunk_offset = offset + size;
1435
1436 return 0;
1437 }
1438
1439 static int mov_read_close(AVFormatContext *s)
1440 {
1441 int i;
1442 MOVContext *mov = (MOVContext *) s->priv_data;
1443 for(i=0; i<mov->total_streams; i++)
1444 mov_free_stream_context(mov->streams[i]);
1445 for(i=0; i<s->nb_streams; i++)
1446 av_freep(&s->streams[i]);
1447 /* free color tabs */
1448 for(i=0; i<mov->ctab_size; i++)
1449 av_freep(&mov->ctab[i]);
1450 av_freep(&mov->ctab);
1451 return 0;
1452 }
1453
1454 static AVInputFormat mov_iformat = {
1455 "mov",
1456 "QuickTime/MPEG4 format",
1457 sizeof(MOVContext),
1458 mov_probe,
1459 mov_read_header,
1460 mov_read_packet,
1461 mov_read_close,
1462 };
1463
1464 int mov_init(void)
1465 {
1466 av_register_input_format(&mov_iformat);
1467 return 0;
1468 }