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