fixed double free - fixed probe function
[libav.git] / libav / mov.c
CommitLineData
6cea494e
ZK
1/*
2 * MOV decoder.
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#include "avformat.h"
20#include "avi.h"
21
22/*
23 * First version by Francois Revol revol@free.fr
24 *
25 * Features and limitations:
26 * - reads most of the QT files I have (at least the structure),
27 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
28 * - ffmpeg has nearly none of the usual QuickTime codecs,
29 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
30 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
31 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
32 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
33 * http://mpeg.telecomitalialab.com/faq.htm
34 * - the code is quite ugly... maybe I won't do it recursive next time :-)
35 *
36 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
37 * when coding this :) (it's a writer anyway)
38 *
39 * Reference documents:
40 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
41 * Apple:
42 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/QTFF/qtff.html
43 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
44 * QuickTime is a trademark of Apple (AFAIK :))
45 */
46
c9a65ca8 47//#define DEBUG
6cea494e
ZK
48
49#ifdef DEBUG
50/*
51 * XXX: static sux, even more in a multithreaded environment...
52 * Avoid them. This is here just to help debugging.
53 */
54static int debug_indent = 0;
55void print_atom(const char *str, UINT32 type, UINT64 offset, UINT64 size)
56{
57 unsigned int tag, i;
58 tag = (unsigned int) type;
59 i=debug_indent;
60 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
61 while(i--)
62 printf("|");
63 printf("parse:");
64 printf(" %s: tag=%c%c%c%c offset=%d size=0x%x\n",
65 str, tag & 0xff,
66 (tag >> 8) & 0xff,
67 (tag >> 16) & 0xff,
68 (tag >> 24) & 0xff,
69 (unsigned int)offset,
70 (unsigned int)size);
71}
72#endif
73
74/* some streams in QT (and in MP4 mostly) aren't either video nor audio */
75/* so we first list them as this, then clean up the list of streams we give back, */
76/* getting rid of these */
77#define CODEC_TYPE_MOV_OTHER 2
78
a266644f 79static const CodecTag mov_video_tags[] = {
6cea494e
ZK
80/* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
81/* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG */
82 { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, /* Uncompressed RGB */
83 { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, /* Uncompressed YUV422 */
84/* Graphics */
85/* Animation */
86/* Apple video */
87/* Kodak Photo CD */
88/* { CODEC_ID_JPEG, MKTAG('j', 'p', 'e', 'g') }, *//* JPEG ? */
89 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
90 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format A) */
91 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
92/* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
93/* Sorenson video */
94 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') }, /* OpenDiVX *//* yeah ! */
95 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
96/* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
97 { 0, 0 },
98};
99
a266644f 100static const CodecTag mov_audio_tags[] = {
6cea494e
ZK
101/* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
102 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
103 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') }, /* 8 bits */
104 { CODEC_ID_PCM_U8, 0x20776172 }, /* 8 bits unsigned */
105 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
106 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
107 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
108/* { CODEC_ID_, MKTAG('i', 'm', 'a', '4') }, *//* IMA-4 */
109
110 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
111 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
112 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
113/* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
114/* MP4 tags */
115/* { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') }, *//* MPEG 4 AAC or audio ? */
116 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
117 { 0, 0 },
118};
119
120/* the QuickTime file format is quite convoluted...
121 * it has lots of index tables, each indexing something in another one...
122 * Here we just use what is needed to read the chunks
123 */
124
125typedef struct MOV_sample_to_chunk_tbl {
126 long first;
127 long count;
128 long id;
129} MOV_sample_to_chunk_tbl;
130
131typedef struct MOVStreamContext {
132 int ffindex; /* the ffmpeg stream id */
133 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
134 long next_chunk;
135 long chunk_count;
136 INT64 *chunk_offsets;
137 long sample_to_chunk_sz;
138 MOV_sample_to_chunk_tbl *sample_to_chunk;
139 long sample_size;
140 long sample_count;
141 long *sample_sizes;
142} MOVStreamContext;
143
144typedef struct MOVContext {
145 int mp4; /* set to 1 as soon as we are sure that the file is an .mp4 file (even some header parsing depends on this) */
146 AVFormatContext *fc;
147 long time_scale;
148 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
149 int found_mdat; /* we suppose we have enough data to read the file */
150 INT64 mdat_size;
151 INT64 mdat_offset;
152 int total_streams;
153 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
154 * but we need the info to be able to skip data from those streams in the 'mdat' section
155 */
156 MOVStreamContext *streams[MAX_STREAMS];
157
158 INT64 next_chunk_offset;
159} MOVContext;
160
161
162struct MOVParseTableEntry;
163
164/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
165
166/* those functions parse an atom */
167/* return code:
168 1: found what I wanted, exit
169 0: continue to parse next atom
170 -1: error occured, exit
171 */
684f44d9 172typedef int (*mov_parse_function)(const struct MOVParseTableEntry *parse_table,
6cea494e
ZK
173 ByteIOContext *pb,
174 UINT32 atom_type,
175 INT64 atom_offset, /* after the size and type field (and eventually the extended size) */
176 INT64 atom_size, /* total size (excluding the size and type fields) */
177 void *param);
178
179/* links atom IDs to parse functions */
180typedef struct MOVParseTableEntry {
181 UINT32 type;
182 mov_parse_function func;
183} MOVParseTableEntry;
184
684f44d9 185static int parse_leaf(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
186{
187#ifdef DEBUG
188 print_atom("leaf", atom_type, atom_offset, atom_size);
189#endif
190 if(atom_size>1)
191 url_fskip(pb, atom_size);
192/* url_seek(pb, atom_offset+atom_size, SEEK_SET); */
193 return 0;
194}
195
196
684f44d9 197static int parse_default(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
198{
199 UINT32 type, foo=0;
200 UINT64 offset, size;
201 UINT64 total_size = 0;
202 int i;
203 int err = 0;
204 foo=0;
205#ifdef DEBUG
206 print_atom("default", atom_type, atom_offset, atom_size);
207 debug_indent++;
208#endif
209
210 offset = atom_offset;
211
212 if(atom_size < 0)
213 atom_size = 0x0FFFFFFFFFFFFFFF;
214 while((total_size < atom_size) && !url_feof(pb) && !err) {
215 size=atom_size;
216 type=0L;
217 if(atom_size >= 8) {
218 size = get_be32(pb);
219 type = get_le32(pb);
220 }
221 total_size += 8;
222 offset+=8;
223// printf("type: %08lx sz: %08lx", type, size);
224 if(size == 1) { /* 64 bit extended size */
225 size = get_be64(pb);
226 offset+=8;
227 total_size+=8;
228 size-=8;
229 }
230 if(size == 0)
231 size = atom_size - total_size;
232 size-=8;
233 for(i=0; parse_table[i].type != 0L && parse_table[i].type != type; i++);
234
235// printf(" i=%ld\n", i);
684f44d9 236 if (parse_table[i].type == 0) { /* skip leaf atoms data */
6cea494e
ZK
237// url_seek(pb, atom_offset+atom_size, SEEK_SET);
238#ifdef DEBUG
239 print_atom("unknown", type, offset, size);
240#endif
241 url_fskip(pb, size);
242 } else
243 err = (parse_table[i].func)(parse_table, pb, type, offset, size, param);
244
245 offset+=size;
246 total_size+=size;
247 }
248
249#ifdef DEBUG
250 debug_indent--;
251#endif
252 return err;
253}
254
684f44d9 255static int parse_mvhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
256{
257 MOVContext *c;
258#ifdef DEBUG
259 print_atom("mvhd", atom_type, atom_offset, atom_size);
260#endif
261 c = (MOVContext *)param;
262
263 get_byte(pb); /* version */
264 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
265
266 get_be32(pb); /* creation time */
267 get_be32(pb); /* modification time */
268 c->time_scale = get_be32(pb); /* time scale */
269 get_be32(pb); /* duration */
270 get_be32(pb); /* preferred scale */
271
272 get_be16(pb); /* preferred volume */
273
274 url_fskip(pb, 10); /* reserved */
275
276 url_fskip(pb, 36); /* display matrix */
277
278 get_be32(pb); /* preview time */
279 get_be32(pb); /* preview duration */
280 get_be32(pb); /* poster time */
281 get_be32(pb); /* selection time */
282 get_be32(pb); /* selection duration */
283 get_be32(pb); /* current time */
284 get_be32(pb); /* next track ID */
285
286 return 0;
287}
288
289/* this atom should contain all header atoms */
684f44d9 290static int parse_moov(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
291{
292 int err;
293 MOVContext *c;
294#ifdef DEBUG
295 print_atom("moov", atom_type, atom_offset, atom_size);
296#endif
297 c = (MOVContext *)param;
298
299 err = parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
300 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
301 /* so we don't parse the whole file if over a network */
302 c->found_moov=1;
303 if(c->found_mdat)
304 return 1; /* found both, just go */
305 return 0; /* now go for mdat */
306}
307
308/* this atom contains actual media data */
684f44d9 309static int parse_mdat(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e 310{
6cea494e
ZK
311 MOVContext *c;
312#ifdef DEBUG
313 print_atom("mdat", atom_type, atom_offset, atom_size);
314#endif
315 c = (MOVContext *)param;
316
317 if(atom_size == 0) /* wrong one (MP4) */
318 return 0;
319 c->found_mdat=1;
320 c->mdat_offset = atom_offset;
321 c->mdat_size = atom_size;
322 if(c->found_moov)
323 return 1; /* found both, just go */
324 url_fskip(pb, atom_size);
325 return 0; /* now go for moov */
326}
327
684f44d9 328static int parse_trak(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
329{
330 MOVContext *c;
331 AVStream *st;
332 MOVStreamContext *sc;
333#ifdef DEBUG
334 print_atom("trak", atom_type, atom_offset, atom_size);
335#endif
336
337 c = (MOVContext *)param;
1ea4f593
FB
338 st = av_malloc(sizeof(AVStream));
339 if (!st) return -2;
6cea494e
ZK
340 memset(st, 0, sizeof(AVStream));
341 c->fc->streams[c->fc->nb_streams] = st;
1ea4f593 342 sc = av_malloc(sizeof(MOVStreamContext));
6cea494e
ZK
343 st->priv_data = sc;
344 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
345 c->streams[c->fc->nb_streams++] = sc;
346 return parse_default(parse_table, pb, atom_type, atom_offset, atom_size, param);
347}
348
684f44d9 349static int parse_tkhd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
350{
351 MOVContext *c;
352 AVStream *st;
353#ifdef DEBUG
354 print_atom("tkhd", atom_type, atom_offset, atom_size);
355#endif
356
357 c = (MOVContext *)param;
358 st = c->fc->streams[c->fc->nb_streams-1];
359
360 get_byte(pb); /* version */
361
362 get_byte(pb); get_byte(pb);
363 get_byte(pb); /* flags */
364 /*
365 MOV_TRACK_ENABLED 0x0001
366 MOV_TRACK_IN_MOVIE 0x0002
367 MOV_TRACK_IN_PREVIEW 0x0004
368 MOV_TRACK_IN_POSTER 0x0008
369 */
370
371 get_be32(pb); /* creation time */
372 get_be32(pb); /* modification time */
373 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
374 get_be32(pb); /* reserved */
375 get_be32(pb); /* duration */
376 get_be32(pb); /* reserved */
377 get_be32(pb); /* reserved */
378
379 get_be16(pb); /* layer */
380 get_be16(pb); /* alternate group */
381 get_be16(pb); /* volume */
382 get_be16(pb); /* reserved */
383
384 url_fskip(pb, 36); /* display matrix */
385
386 /* those are fixed-point */
387 st->codec.width = get_be32(pb) >> 16; /* track width */
388 st->codec.height = get_be32(pb) >> 16; /* track height */
389
390 return 0;
391}
392
684f44d9 393static int parse_hdlr(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
394{
395 MOVContext *c;
396 int len;
c9a65ca8 397 char *buf;
6cea494e
ZK
398 UINT32 type;
399 AVStream *st;
400 UINT32 ctype;
401#ifdef DEBUG
402 print_atom("hdlr", atom_type, atom_offset, atom_size);
403#endif
404 c = (MOVContext *)param;
405 st = c->fc->streams[c->fc->nb_streams-1];
406
407 get_byte(pb); /* version */
408 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
409
410 /* component type */
411 ctype = get_le32(pb);
412 type = get_le32(pb); /* component subtype */
413
414#ifdef DEBUG
684f44d9 415 printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
6cea494e
ZK
416 printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
417#endif
418#ifdef DEBUG
419/* XXX: yeah this is ugly... */
420 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
421 if(type == MKTAG('v', 'i', 'd', 'e'))
422 puts("hdlr: vide");
423 else if(type == MKTAG('s', 'o', 'u', 'n'))
424 puts("hdlr: soun");
425 } else if(ctype == 0) { /* MP4 */
426 if(type == MKTAG('v', 'i', 'd', 'e'))
427 puts("hdlr: vide");
428 else if(type == MKTAG('s', 'o', 'u', 'n'))
429 puts("hdlr: soun");
430 else if(type == MKTAG('o', 'd', 's', 'm'))
431 puts("hdlr: odsm");
432 else if(type == MKTAG('s', 'd', 's', 'm'))
433 puts("hdlr: sdsm");
434 } else puts("hdlr: meta");
435#endif
436
437 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
438 if(type == MKTAG('v', 'i', 'd', 'e'))
439 st->codec.codec_type = CODEC_TYPE_VIDEO;
440 else if(type == MKTAG('s', 'o', 'u', 'n'))
441 st->codec.codec_type = CODEC_TYPE_AUDIO;
442 } else if(ctype == 0) { /* MP4 */
443 if(type == MKTAG('v', 'i', 'd', 'e'))
444 st->codec.codec_type = CODEC_TYPE_VIDEO;
445 else if(type == MKTAG('s', 'o', 'u', 'n'))
446 st->codec.codec_type = CODEC_TYPE_AUDIO;
447 }
448 get_be32(pb); /* component manufacture */
449 get_be32(pb); /* component flags */
450 get_be32(pb); /* component flags mask */
451
452 if(atom_size <= 24)
453 return 0; /* nothing left to read */
454 /* XXX: MP4 uses a C string, not a pascal one */
455 /* component name */
c9a65ca8
FB
456 len = get_byte(pb);
457 /* XXX: use a better heuristic */
458 if(len < 32) {
459 /* assume that it is a Pascal like string */
460 buf = av_malloc(len+1);
461 get_buffer(pb, buf, len);
462 buf[len] = '\0';
6cea494e 463#ifdef DEBUG
c9a65ca8 464 printf("**buf='%s'\n", buf);
6cea494e 465#endif
c9a65ca8 466 av_free(buf);
6cea494e 467 } else {
c9a65ca8
FB
468 /* MP4 string */
469 for(;;) {
470 if (len == 0)
471 break;
472 len = get_byte(pb);
6cea494e
ZK
473 }
474 }
475
476 return 0;
477}
478
684f44d9 479static int parse_stsd(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
480{
481 MOVContext *c;
482 int entries, size, samp_sz, frames_per_sample;
6cea494e
ZK
483 UINT32 format;
484 AVStream *st;
485#ifdef DEBUG
486 print_atom("stsd", atom_type, atom_offset, atom_size);
487#endif
488 c = (MOVContext *)param;
489 st = c->fc->streams[c->fc->nb_streams-1];
490
491 get_byte(pb); /* version */
492 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
493
494
495 entries = get_be32(pb);
496
497 while(entries--) {
498 size = get_be32(pb); /* size */
499 format = get_le32(pb); /* data format */
500
501 get_be32(pb); /* reserved */
502 get_be16(pb); /* reserved */
503 get_be16(pb); /* index */
504/* if(format == MKTAG('m', 'p', '4', 'v')) */
505/* st->codec.codec_type=CODEC_TYPE_VIDEO; *//* force things (XXX: was this for .mp4 ?) */
506 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
507 st->codec.codec_tag = format;
508 st->codec.codec_id = codec_get_id(mov_video_tags, format);
509 get_be16(pb); /* version */
510 get_be16(pb); /* revision level */
511 get_be32(pb); /* vendor */
512 get_be32(pb); /* temporal quality */
513 get_be32(pb); /* spacial quality */
514 st->codec.width = get_be16(pb); /* width */
515 st->codec.height = get_be16(pb); /* height */
516 get_be32(pb); /* horiz resolution */
517 get_be32(pb); /* vert resolution */
518 get_be32(pb); /* data size, always 0 */
519 frames_per_sample = get_be16(pb); /* frame per samples */
520#ifdef DEBUG
684f44d9 521 printf("frames/samples = %d\n", frames_per_sample);
6cea494e
ZK
522#endif
523 url_fskip(pb, 32); /* codec name */
524
525 get_be16(pb); /* depth */
526 get_be16(pb); /* colortable id */
527 get_be16(pb); /* */
528 get_be16(pb); /* */
529
530 st->codec.sample_rate = 25 * FRAME_RATE_BASE;
531
532 if(size > 16)
533 url_fskip(pb, size-(16+24+18+32));
534 } else {
535 st->codec.codec_tag = format;
536
537 get_be16(pb); /* version */
538 get_be16(pb); /* revision level */
539 get_be32(pb); /* vendor */
540
541 st->codec.channels = get_be16(pb);/* channel count */
542 samp_sz = get_be16(pb); /* sample size */
543#ifdef DEBUG
544 if(samp_sz != 16)
545 puts("!!! stsd: audio sample size is not 16 bit !");
546#endif
547 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
548 /* handle specific s8 codec */
549 if (st->codec.codec_id == CODEC_ID_PCM_S16BE && samp_sz == 8)
550 st->codec.codec_id = CODEC_ID_PCM_S8;
551
552 get_be16(pb); /* compression id = 0*/
553 get_be16(pb); /* packet size = 0 */
554
555 st->codec.sample_rate = ((get_be32(pb) >> 16));
556 st->codec.bit_rate = 0;
557
558 /* this is for .mp4 files */
559 if(format == MKTAG('m', 'p', '4', 'v')) { /* XXX */
560 st->codec.codec_type=CODEC_TYPE_VIDEO; /* force things */
561 st->codec.codec_id = CODEC_ID_MPEG4;
562 st->codec.frame_rate = 25;
563 st->codec.bit_rate = 100000;
564 }
565
566#if 0
567
568 get_be16(pb); get_be16(pb); /* */
569 get_be16(pb); /* */
570 get_be16(pb); /* */
571 get_be16(pb); /* */
572 get_be16(pb); /* */
573#endif
574 if(size > 16)
575 url_fskip(pb, size-(16+20));
576 }
577#ifdef DEBUG
578 printf("4CC= %c%c%c%c\n", *((char *)&format), ((char *)&format)[1], ((char *)&format)[2], ((char *)&format)[3]);
579#endif
580 }
581/*
582 if(len) {
1ea4f593 583 buf = av_malloc(len+1);
6cea494e
ZK
584 get_buffer(pb, buf, len);
585 buf[len] = '\0';
586 puts(buf);
1ea4f593 587 av_free(buf);
6cea494e
ZK
588 }
589*/
590 return 0;
591}
592
684f44d9 593static int parse_stco(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
594{
595 MOVContext *c;
596 int entries, i;
597 AVStream *st;
598 MOVStreamContext *sc;
599#ifdef DEBUG
600 print_atom("stco", atom_type, atom_offset, atom_size);
601#endif
602 c = (MOVContext *)param;
603 st = c->fc->streams[c->fc->nb_streams-1];
604 sc = (MOVStreamContext *)st->priv_data;
605
606 get_byte(pb); /* version */
607 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
608
609 entries = get_be32(pb);
610 sc->chunk_count = entries;
1ea4f593 611 sc->chunk_offsets = av_malloc(entries * sizeof(INT64));
6cea494e
ZK
612 if(atom_type == MKTAG('s', 't', 'c', 'o')) {
613 for(i=0; i<entries; i++) {
614 sc->chunk_offsets[i] = get_be32(pb);
615 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
616 }
617 } else if(atom_type == MKTAG('c', 'o', '6', '4')) {
618 for(i=0; i<entries; i++) {
619 sc->chunk_offsets[i] = get_be64(pb);
620 /*printf("chunk offset=%ld\n", sc->chunk_offsets[i]);*/
621 }
622 } else
623 return -1;
624 return 0;
625}
626
684f44d9 627static int parse_stsc(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
628{
629 MOVContext *c;
630 int entries, i;
631 AVStream *st;
632 MOVStreamContext *sc;
633#ifdef DEBUG
634 print_atom("stsc", atom_type, atom_offset, atom_size);
635#endif
636 c = (MOVContext *)param;
637 st = c->fc->streams[c->fc->nb_streams-1];
638 sc = (MOVStreamContext *)st->priv_data;
639
640 get_byte(pb); /* version */
641 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
642
643 entries = get_be32(pb);
644 sc->sample_to_chunk_sz = entries;
1ea4f593 645 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
6cea494e
ZK
646 for(i=0; i<entries; i++) {
647 sc->sample_to_chunk[i].first = get_be32(pb);
648 sc->sample_to_chunk[i].count = get_be32(pb);
649 sc->sample_to_chunk[i].id = get_be32(pb);
650#ifdef DEBUG
651/* 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); */
652#endif
653 }
654 return 0;
655}
656
684f44d9 657static int parse_stsz(const MOVParseTableEntry *parse_table, ByteIOContext *pb, UINT32 atom_type, INT64 atom_offset, INT64 atom_size, void *param)
6cea494e
ZK
658{
659 MOVContext *c;
660 int entries, i;
661 AVStream *st;
662 MOVStreamContext *sc;
663#ifdef DEBUG
664 print_atom("stsz", atom_type, atom_offset, atom_size);
665#endif
666 c = (MOVContext *)param;
667 st = c->fc->streams[c->fc->nb_streams-1];
668 sc = (MOVStreamContext *)st->priv_data;
669
670 get_byte(pb); /* version */
671 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
672
673 sc->sample_size = get_be32(pb);
674 entries = get_be32(pb);
675 sc->sample_count = entries;
676 printf("sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
677 if(sc->sample_size)
678 return 0; /* there isn't any table following */
1ea4f593 679 sc->sample_sizes = av_malloc(entries * sizeof(long));
6cea494e
ZK
680 for(i=0; i<entries; i++) {
681 sc->sample_sizes[i] = get_be32(pb);
682#ifdef DEBUG
683/* printf("sample_sizes[]=%ld\n", sc->sample_sizes[i]); */
684#endif
685 }
686 return 0;
687}
688
689static const MOVParseTableEntry mov_default_parse_table[] = {
690/* mp4 atoms */
691{ MKTAG( 'm', 'p', '4', 'a' ), parse_default },
692{ MKTAG( 'c', 'o', '6', '4' ), parse_stco },
693{ MKTAG( 's', 't', 'c', 'o' ), parse_stco },
694{ MKTAG( 'c', 'r', 'h', 'd' ), parse_default },
695{ MKTAG( 'c', 't', 't', 's' ), parse_leaf },
696{ MKTAG( 'c', 'p', 'r', 't' ), parse_default },
697{ MKTAG( 'u', 'r', 'l', ' ' ), parse_leaf },
698{ MKTAG( 'u', 'r', 'n', ' ' ), parse_leaf },
699{ MKTAG( 'd', 'i', 'n', 'f' ), parse_default },
700{ MKTAG( 'd', 'r', 'e', 'f' ), parse_leaf },
701{ MKTAG( 's', 't', 'd', 'p' ), parse_default },
702{ MKTAG( 'e', 's', 'd', 's' ), parse_default },
703{ MKTAG( 'e', 'd', 't', 's' ), parse_default },
704{ MKTAG( 'e', 'l', 's', 't' ), parse_leaf },
705{ MKTAG( 'u', 'u', 'i', 'd' ), parse_default },
706{ MKTAG( 'f', 'r', 'e', 'e' ), parse_leaf },
707{ MKTAG( 'h', 'd', 'l', 'r' ), parse_hdlr },
708{ MKTAG( 'h', 'm', 'h', 'd' ), parse_default },
709{ MKTAG( 'h', 'i', 'n', 't' ), parse_leaf },
710{ MKTAG( 'n', 'm', 'h', 'd' ), parse_leaf },
711{ MKTAG( 'm', 'p', '4', 's' ), parse_default },
712{ MKTAG( 'm', 'd', 'i', 'a' ), parse_default },
713{ MKTAG( 'm', 'd', 'a', 't' ), parse_mdat },
714{ MKTAG( 'm', 'd', 'h', 'd' ), parse_leaf },
715{ MKTAG( 'm', 'i', 'n', 'f' ), parse_default },
716{ MKTAG( 'm', 'o', 'o', 'v' ), parse_moov },
717{ MKTAG( 'm', 'v', 'h', 'd' ), parse_mvhd },
718{ MKTAG( 'i', 'o', 'd', 's' ), parse_leaf },
719{ MKTAG( 'o', 'd', 'h', 'd' ), parse_default },
720{ MKTAG( 'm', 'p', 'o', 'd' ), parse_leaf },
721{ MKTAG( 's', 't', 's', 'd' ), parse_stsd },
722{ MKTAG( 's', 't', 's', 'z' ), parse_stsz },
723{ MKTAG( 's', 't', 'b', 'l' ), parse_default },
724{ MKTAG( 's', 't', 's', 'c' ), parse_stsc },
725{ MKTAG( 's', 'd', 'h', 'd' ), parse_default },
726{ MKTAG( 's', 't', 's', 'h' ), parse_default },
727{ MKTAG( 's', 'k', 'i', 'p' ), parse_default },
728{ MKTAG( 's', 'm', 'h', 'd' ), parse_leaf },
729{ MKTAG( 'd', 'p', 'n', 'd' ), parse_leaf },
730{ MKTAG( 's', 't', 's', 's' ), parse_leaf },
731{ MKTAG( 's', 't', 't', 's' ), parse_leaf },
732{ MKTAG( 't', 'r', 'a', 'k' ), parse_trak },
733{ MKTAG( 't', 'k', 'h', 'd' ), parse_tkhd },
734{ MKTAG( 't', 'r', 'e', 'f' ), parse_default }, /* not really */
735{ MKTAG( 'u', 'd', 't', 'a' ), parse_leaf },
736{ MKTAG( 'v', 'm', 'h', 'd' ), parse_leaf },
737{ MKTAG( 'm', 'p', '4', 'v' ), parse_default },
738/* extra mp4 */
739{ MKTAG( 'M', 'D', 'E', 'S' ), parse_leaf },
740/* QT atoms */
741{ MKTAG( 'c', 'h', 'a', 'p' ), parse_leaf },
742{ MKTAG( 'c', 'l', 'i', 'p' ), parse_default },
743{ MKTAG( 'c', 'r', 'g', 'n' ), parse_leaf },
744{ MKTAG( 'k', 'm', 'a', 't' ), parse_leaf },
745{ MKTAG( 'm', 'a', 't', 't' ), parse_default },
746{ MKTAG( 'r', 'd', 'r', 'f' ), parse_leaf },
747{ MKTAG( 'r', 'm', 'd', 'a' ), parse_default },
748{ MKTAG( 'r', 'm', 'd', 'r' ), parse_leaf },
749//{ MKTAG( 'r', 'm', 'q', 'u' ), parse_leaf },
750{ MKTAG( 'r', 'm', 'r', 'a' ), parse_default },
751{ MKTAG( 's', 'c', 'p', 't' ), parse_leaf },
752{ MKTAG( 's', 'y', 'n', 'c' ), parse_leaf },
753{ MKTAG( 's', 's', 'r', 'c' ), parse_leaf },
754{ MKTAG( 't', 'c', 'm', 'd' ), parse_leaf },
755{ MKTAG( 'w', 'i', 'd', 'e' ), parse_leaf }, /* place holder */
756{ 0L, parse_leaf }
757};
758
759static void mov_free_stream_context(MOVStreamContext *sc)
760{
761 if(sc) {
1ea4f593
FB
762 av_free(sc->chunk_offsets);
763 av_free(sc->sample_to_chunk);
764 av_free(sc);
6cea494e
ZK
765 }
766}
767
c9a65ca8
FB
768/* XXX: is it suffisant ? */
769static int mov_probe(AVProbeData *p)
770{
771 /* check file header */
772 if (p->buf_size <= 12)
773 return 0;
774 if ((p->buf[4] == 'm' && p->buf[5] == 'o' &&
775 p->buf[6] == 'o' && p->buf[7] == 'v') ||
776 (p->buf[4] == 'm' && p->buf[5] == 'd' &&
777 p->buf[6] == 'a' && p->buf[7] == 't'))
778 return AVPROBE_SCORE_MAX;
779 else
780 return 0;
781}
782
a266644f 783static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
6cea494e 784{
c9a65ca8 785 MOVContext *mov = s->priv_data;
6cea494e 786 ByteIOContext *pb = &s->pb;
684f44d9 787 int i, j, nb, err;
6cea494e 788 INT64 size;
6cea494e 789
6cea494e 790 mov->fc = s;
c9a65ca8
FB
791#if 0
792 /* XXX: I think we should auto detect */
793 if(s->iformat->name[1] == 'p')
6cea494e 794 mov->mp4 = 1;
c9a65ca8 795#endif
6cea494e
ZK
796 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
797 size = url_filesize(url_fileno(pb));
798 else
799 size = 0x7FFFFFFFFFFFFFFF;
800
801#ifdef DEBUG
684f44d9 802 printf("filesz=%Ld\n", size);
6cea494e
ZK
803#endif
804
805 /* check MOV header */
806 err = parse_default(mov_default_parse_table, pb, 0L, 0LL, size, mov);
807 if(err<0 || (!mov->found_moov || !mov->found_mdat)) {
808 puts("header not found !!!");
809 exit(1);
810 }
811#ifdef DEBUG
684f44d9 812 printf("on_parse_exit_offset=%d\n", (int) url_ftell(pb));
6cea494e
ZK
813#endif
814 /* some cleanup : make sure we are on the mdat atom */
815 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
816 url_fseek(pb, mov->mdat_offset, SEEK_SET);
817
818 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
819
820#ifdef DEBUG
684f44d9 821 printf("mdat_reset_offset=%d\n", (int) url_ftell(pb));
6cea494e
ZK
822#endif
823
824#ifdef DEBUG
684f44d9 825 printf("streams= %d\n", s->nb_streams);
6cea494e
ZK
826#endif
827 mov->total_streams = nb = s->nb_streams;
828
829#if 1
830 for(i=0; i<s->nb_streams;) {
831 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1ea4f593 832 av_free(s->streams[i]);
6cea494e
ZK
833 for(j=i+1; j<s->nb_streams; j++)
834 s->streams[j-1] = s->streams[j];
835 s->nb_streams--;
836 } else
837 i++;
838 }
839 for(i=0; i<s->nb_streams;i++) {
840 MOVStreamContext *sc;
841 sc = (MOVStreamContext *)s->streams[i]->priv_data;
842 sc->ffindex = i;
843 sc->is_ff_stream = 1;
844 }
845#endif
846#ifdef DEBUG
684f44d9 847 printf("real streams= %d\n", s->nb_streams);
6cea494e
ZK
848#endif
849 return 0;
850}
851
852/* Yes, this is ugly... I didn't write the specs of QT :p */
853/* XXX:remove useless commented code sometime */
a266644f 854static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
6cea494e
ZK
855{
856 MOVContext *mov = s->priv_data;
857 INT64 offset = 0x0FFFFFFFFFFFFFFF;
684f44d9 858 int i;
6cea494e
ZK
859 int st_id = 0, size;
860 size = 0x0FFFFFFF;
861
862again:
863 for(i=0; i<mov->total_streams; i++) {
864/* printf("%8ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk]); */
865 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
866 && (mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] < offset)) {
867/* printf("y"); */
868 st_id = i;
869 offset = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk];
870 }
871/* else printf("n"); */
872 }
873 mov->streams[st_id]->next_chunk++;
874 if(offset==0x0FFFFFFFFFFFFFFF)
875 return -1;
876
877 if(mov->next_chunk_offset < offset) /* some meta data */
878 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
879 if(!mov->streams[st_id]->is_ff_stream) {
880 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
881 offset = 0x0FFFFFFFFFFFFFFF;
882/* puts("*"); */
883 goto again;
884 }
885/* printf("\nchunk offset = %ld\n", offset); */
886
887 /* now get the chunk size... */
888
889 for(i=0; i<mov->total_streams; i++) {
890/* printf("%ld ", mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset); */
891 if((mov->streams[i]->next_chunk < mov->streams[i]->chunk_count)
892 && ((mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset) < size)) {
893/* printf("y"); */
894 size = mov->streams[i]->chunk_offsets[mov->streams[i]->next_chunk] - offset;
895 }
896/* else printf("n"); */
897 }
898/* printf("\nchunk size = %ld\n", size); */
899 if(size == 0x0FFFFFFF)
900 size = mov->mdat_size + mov->mdat_offset - offset;
901 if(size < 0)
902 return -1;
903 if(size == 0)
904 return -1;
905 av_new_packet(pkt, size);
906 pkt->stream_index = mov->streams[st_id]->ffindex;
907
908 get_buffer(&s->pb, pkt->data, pkt->size);
909
910#ifdef DEBUG
911/*
912 printf("Packet (%d, %d, %ld) ", pkt->stream_index, st_id, pkt->size);
913 for(i=0; i<8; i++)
914 printf("%02x ", pkt->data[i]);
915 for(i=0; i<8; i++)
916 printf("%c ", (pkt->data[i]) & 0x7F);
917 puts("");
918*/
919#endif
920
921 mov->next_chunk_offset = offset + size;
922
923 return 0;
924}
925
a266644f 926static int mov_read_close(AVFormatContext *s)
6cea494e
ZK
927{
928 int i;
929 MOVContext *mov = s->priv_data;
930 for(i=0; i<mov->total_streams; i++)
931 mov_free_stream_context(mov->streams[i]);
932 for(i=0; i<s->nb_streams; i++)
1ea4f593 933 av_free(s->streams[i]);
6cea494e
ZK
934 return 0;
935}
936
c9a65ca8 937static AVInputFormat mov_iformat = {
6cea494e 938 "mov",
c9a65ca8
FB
939 "QuickTime/MPEG4 format",
940 sizeof(MOVContext),
941 mov_probe,
6cea494e
ZK
942 mov_read_header,
943 mov_read_packet,
944 mov_read_close,
945};
946
c9a65ca8
FB
947int mov_init(void)
948{
949 av_register_input_format(&mov_iformat);
950 return 0;
951}