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