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