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