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