Revert the fixed-size-sample patch as it brokes and others
[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 18 */
d957696f
MN
19
20#include <limits.h>
21
6cea494e
ZK
22#include "avformat.h"
23#include "avi.h"
24
0147f198
FR
25#ifdef CONFIG_ZLIB
26#include <zlib.h>
27#endif
28
6cea494e
ZK
29/*
30 * First version by Francois Revol revol@free.fr
baf25c9d 31 * Seek function by Gael Chardon gael.dev@4now.net
5ca1d879 32 *
6cea494e 33 * Features and limitations:
5ca1d879 34 * - reads most of the QT files I have (at least the structure),
6cea494e 35 * the exceptions are .mov with zlib compressed headers ('cmov' section). It shouldn't be hard to implement.
0147f198 36 * FIXED, Francois Revol, 07/17/2002
6cea494e
ZK
37 * - ffmpeg has nearly none of the usual QuickTime codecs,
38 * although I succesfully dumped raw and mp3 audio tracks off .mov files.
39 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
40 * - .mp4 parsing is still hazardous, although the format really is QuickTime with some minor changes
41 * (to make .mov parser crash maybe ?), despite what they say in the MPEG FAQ at
42 * http://mpeg.telecomitalialab.com/faq.htm
43 * - the code is quite ugly... maybe I won't do it recursive next time :-)
baf25c9d 44 * - seek is not supported with files that contain edit list
5ca1d879 45 *
6cea494e
ZK
46 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
47 * when coding this :) (it's a writer anyway)
5ca1d879 48 *
6cea494e
ZK
49 * Reference documents:
50 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
51 * Apple:
baf25c9d 52 * http://developer.apple.com/documentation/QuickTime/QTFF/
15c8dbe7 53 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
6cea494e
ZK
54 * QuickTime is a trademark of Apple (AFAIK :))
55 */
56
c9a65ca8 57//#define DEBUG
9ed83b0a
ZK
58#ifdef DEBUG
59#include <stdio.h>
60#include <fcntl.h>
61#endif
6cea494e 62
b595afaa
MM
63#include "qtpalette.h"
64
baf25c9d
GC
65
66/* Allows seeking (MOV_SPLIT_CHUNKS should also be defined) */
67#define MOV_SEEK
68
3ffe3793
FR
69/* allows chunk splitting - should work now... */
70/* in case you can't read a file, try commenting */
71#define MOV_SPLIT_CHUNKS
72
14342fd5
JC
73/* Special handling for movies created with Minolta Dimaxe Xi*/
74/* this fix should not interfere with other .mov files, but just in case*/
75#define MOV_MINOLTA_FIX
76
6cea494e
ZK
77/* some streams in QT (and in MP4 mostly) aren't either video nor audio */
78/* so we first list them as this, then clean up the list of streams we give back, */
79/* getting rid of these */
b6a17df4 80#define CODEC_TYPE_MOV_OTHER (enum CodecType) 2
6cea494e 81
a266644f 82static const CodecTag mov_video_tags[] = {
6cea494e 83/* { CODEC_ID_, MKTAG('c', 'v', 'i', 'd') }, *//* Cinepak */
0147f198
FR
84/* { CODEC_ID_H263, MKTAG('r', 'a', 'w', ' ') }, *//* Uncompressed RGB */
85/* { CODEC_ID_H263, MKTAG('Y', 'u', 'v', '2') }, *//* Uncompressed YUV422 */
53e27dd5 86/* { CODEC_ID_RAWVIDEO, MKTAG('A', 'V', 'U', 'I') }, *//* YUV with alpha-channel (AVID Uncompressed) */
6cea494e
ZK
87/* Graphics */
88/* Animation */
89/* Apple video */
90/* Kodak Photo CD */
3ffe3793 91 { CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
6cea494e 92 { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'e', 'g') }, /* MPEG */
3ffe3793 93 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
6cea494e 94 { CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'b') }, /* Motion-JPEG (format B) */
53e27dd5
SB
95 { CODEC_ID_MJPEG, MKTAG('A', 'V', 'D', 'J') }, /* MJPEG with alpha-channel (AVID JFIF meridien compressed) */
96/* { CODEC_ID_MJPEG, MKTAG('A', 'V', 'R', 'n') }, *//* MJPEG with alpha-channel (AVID ABVB/Truevision NuVista) */
6cea494e
ZK
97/* { CODEC_ID_GIF, MKTAG('g', 'i', 'f', ' ') }, *//* embedded gif files as frames (usually one "click to play movie" frame) */
98/* Sorenson video */
0147f198
FR
99 { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') }, /* Sorenson Video v1 */
100 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') }, /* Sorenson Video v1 */
3ffe3793 101 { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', 'i') }, /* Sorenson Video v1 (from QT specs)*/
8b82a956 102 { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') }, /* Sorenson Video v3 */
0e7eed09 103 { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
6cea494e 104 { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') }, /* OpenDiVX *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
8dafdb88 105 { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
6cea494e 106/* { CODEC_ID_, MKTAG('I', 'V', '5', '0') }, *//* Indeo 5.0 */
0147f198 107 { CODEC_ID_H263, MKTAG('h', '2', '6', '3') }, /* H263 */
25fa62e1 108 { CODEC_ID_H263, MKTAG('s', '2', '6', '3') }, /* H263 ?? works */
e1687cc0
FB
109 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
110 { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
53e27dd5 111/* { CODEC_ID_DVVIDEO, MKTAG('A', 'V', 'd', 'v') }, *//* AVID dv */
d86053a4 112 { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') }, /* On2 VP3 */
2fdf638b
MM
113 { CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') }, /* Apple Video (RPZA) */
114 { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') }, /* Cinepak */
1dc1ed99 115 { CODEC_ID_8BPS, MKTAG('8', 'B', 'P', 'S') }, /* Planar RGB (8BPS) */
b595afaa 116 { CODEC_ID_SMC, MKTAG('s', 'm', 'c', ' ') }, /* Apple Graphics (SMC) */
070ed1bc 117 { CODEC_ID_QTRLE, MKTAG('r', 'l', 'e', ' ') }, /* Apple Animation (RLE) */
d08d7142 118 { CODEC_ID_QDRAW, MKTAG('q', 'd', 'r', 'w') }, /* QuickDraw */
169eb021 119 { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') }, /* AVC-1/H.264 */
5ca1d879 120 { CODEC_ID_NONE, 0 },
6cea494e
ZK
121};
122
a266644f 123static const CodecTag mov_audio_tags[] = {
6cea494e
ZK
124/* { CODEC_ID_PCM_S16BE, MKTAG('N', 'O', 'N', 'E') }, *//* uncompressed */
125 { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') }, /* 16 bits */
5ca1d879 126 /* { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's') },*/ /* 8 bits */
5cd62665 127 { CODEC_ID_PCM_U8, MKTAG('r', 'a', 'w', ' ') }, /* 8 bits unsigned */
6cea494e
ZK
128 { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') }, /* */
129 { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') }, /* */
130 { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') }, /* */
0147f198 131 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') }, /* IMA-4 ADPCM */
3f95e843
FR
132 { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') }, /* Macintosh Audio Compression and Expansion 3:1 */
133 { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') }, /* Macintosh Audio Compression and Expansion 6:1 */
6cea494e
ZK
134
135 { CODEC_ID_MP2, MKTAG('.', 'm', 'p', '3') }, /* MPEG layer 3 */ /* sample files at http://www.3ivx.com/showcase.html use this tag */
136 { CODEC_ID_MP2, 0x6D730055 }, /* MPEG layer 3 */
137 { CODEC_ID_MP2, 0x5500736D }, /* MPEG layer 3 *//* XXX: check endianness */
138/* { CODEC_ID_OGG_VORBIS, MKTAG('O', 'g', 'g', 'S') }, *//* sample files at http://heroinewarrior.com/xmovie.php3 use this tag */
139/* MP4 tags */
4cb3f3b6 140 { CODEC_ID_MPEG4AAC, MKTAG('m', 'p', '4', 'a') }, /* MPEG-4 AAC */
5ca1d879 141 /* The standard for mpeg4 audio is still not normalised AFAIK anyway */
891f64b3 142 { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') }, /* AMR-NB 3gp */
d663a1fd 143 { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') }, /* AMR-WB 3gp */
8dafdb88 144 { CODEC_ID_AC3, MKTAG('m', 's', 0x20, 0x00) }, /* Dolby AC-3 */
5ca1d879 145 { CODEC_ID_NONE, 0 },
6cea494e
ZK
146};
147
148/* the QuickTime file format is quite convoluted...
149 * it has lots of index tables, each indexing something in another one...
150 * Here we just use what is needed to read the chunks
151 */
152
153typedef struct MOV_sample_to_chunk_tbl {
154 long first;
155 long count;
156 long id;
157} MOV_sample_to_chunk_tbl;
158
5cd62665 159typedef struct {
5ca1d879
ZK
160 uint32_t type;
161 int64_t offset;
162 int64_t size; /* total size (excluding the size and type fields) */
163} MOV_atom_t;
164
165typedef struct {
166 int seed;
167 int flags;
168 int size;
169 void* clrs;
170} MOV_ctab_t;
171
172typedef struct {
5cd62665
ZK
173 uint8_t version;
174 uint32_t flags; // 24bit
175
176 /* 0x03 ESDescrTag */
177 uint16_t es_id;
178#define MP4ODescrTag 0x01
179#define MP4IODescrTag 0x02
180#define MP4ESDescrTag 0x03
181#define MP4DecConfigDescrTag 0x04
182#define MP4DecSpecificDescrTag 0x05
183#define MP4SLConfigDescrTag 0x06
184#define MP4ContentIdDescrTag 0x07
185#define MP4SupplContentIdDescrTag 0x08
186#define MP4IPIPtrDescrTag 0x09
187#define MP4IPMPPtrDescrTag 0x0A
188#define MP4IPMPDescrTag 0x0B
189#define MP4RegistrationDescrTag 0x0D
190#define MP4ESIDIncDescrTag 0x0E
191#define MP4ESIDRefDescrTag 0x0F
192#define MP4FileIODescrTag 0x10
193#define MP4FileODescrTag 0x11
194#define MP4ExtProfileLevelDescrTag 0x13
195#define MP4ExtDescrTagsStart 0x80
196#define MP4ExtDescrTagsEnd 0xFE
197 uint8_t stream_priority;
198
199 /* 0x04 DecConfigDescrTag */
200 uint8_t object_type_id;
201 uint8_t stream_type;
202 /* XXX: really streamType is
203 * only 6bit, followed by:
204 * 1bit upStream
205 * 1bit reserved
206 */
207 uint32_t buffer_size_db; // 24
208 uint32_t max_bitrate;
209 uint32_t avg_bitrate;
210
211 /* 0x05 DecSpecificDescrTag */
212 uint8_t decoder_cfg_len;
213 uint8_t *decoder_cfg;
214
215 /* 0x06 SLConfigDescrTag */
216 uint8_t sl_config_len;
217 uint8_t *sl_config;
218} MOV_esds_t;
219
b6a17df4
ZK
220struct MOVParseTableEntry;
221
6cea494e
ZK
222typedef struct MOVStreamContext {
223 int ffindex; /* the ffmpeg stream id */
224 int is_ff_stream; /* Is this stream presented to ffmpeg ? i.e. is this an audio or video stream ? */
225 long next_chunk;
226 long chunk_count;
0c1a9eda 227 int64_t *chunk_offsets;
baf25c9d
GC
228 int32_t stts_count;
229 uint64_t *stts_data; /* concatenated data from the time-to-sample atom (count|duration) */
230 int32_t edit_count; /* number of 'edit' (elst atom) */
6cea494e
ZK
231 long sample_to_chunk_sz;
232 MOV_sample_to_chunk_tbl *sample_to_chunk;
3ffe3793 233 long sample_to_chunk_index;
6cea494e
ZK
234 long sample_size;
235 long sample_count;
236 long *sample_sizes;
247d56f5
BB
237 long keyframe_count;
238 long *keyframes;
5cd62665 239 int time_scale;
3ffe3793
FR
240 long current_sample;
241 long left_in_chunk; /* how many samples before next chunk */
0e7eed09 242 /* specific MPEG4 header which is added at the beginning of the stream */
7fea94ce 243 unsigned int header_len;
0e7eed09 244 uint8_t *header_data;
5ca1d879 245 MOV_esds_t esds;
6cea494e
ZK
246} MOVStreamContext;
247
248typedef struct MOVContext {
249 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) */
250 AVFormatContext *fc;
5cd62665
ZK
251 int time_scale;
252 int duration; /* duration of the longest track */
6cea494e
ZK
253 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
254 int found_mdat; /* we suppose we have enough data to read the file */
0c1a9eda
ZK
255 int64_t mdat_size;
256 int64_t mdat_offset;
6cea494e
ZK
257 int total_streams;
258 /* some streams listed here aren't presented to the ffmpeg API, since they aren't either video nor audio
259 * but we need the info to be able to skip data from those streams in the 'mdat' section
260 */
261 MOVStreamContext *streams[MAX_STREAMS];
5cd62665 262
0c1a9eda 263 int64_t next_chunk_offset;
5cd62665 264 MOVStreamContext *partial; /* != 0 : there is still to read in the current chunk */
5ca1d879
ZK
265 int ctab_size;
266 MOV_ctab_t **ctab; /* color tables */
b6a17df4
ZK
267 const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
268 /* NOTE: for recursion save to/ restore from local variable! */
b595afaa
MM
269
270 AVPaletteControl palette_control;
6cea494e
ZK
271} MOVContext;
272
273
6cea494e
ZK
274/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
275
276/* those functions parse an atom */
277/* return code:
278 1: found what I wanted, exit
279 0: continue to parse next atom
280 -1: error occured, exit
281 */
5ca1d879 282typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
6cea494e
ZK
283
284/* links atom IDs to parse functions */
285typedef struct MOVParseTableEntry {
0c1a9eda 286 uint32_t type;
6cea494e
ZK
287 mov_parse_function func;
288} MOVParseTableEntry;
289
5ca1d879
ZK
290#ifdef DEBUG
291/*
292 * XXX: static sux, even more in a multithreaded environment...
293 * Avoid them. This is here just to help debugging.
294 */
295static int debug_indent = 0;
296void print_atom(const char *str, MOV_atom_t atom)
297{
298 unsigned int tag, i;
299 tag = (unsigned int) atom.type;
300 i=debug_indent;
301 if(tag == 0) tag = MKTAG('N', 'U', 'L', 'L');
302 while(i--)
baf25c9d
GC
303 av_log(NULL, AV_LOG_DEBUG, "|");
304 av_log(NULL, AV_LOG_DEBUG, "parse:");
305 av_log(NULL, AV_LOG_DEBUG, " %s: tag=%c%c%c%c offset=0x%x size=0x%x\n",
5ca1d879
ZK
306 str, tag & 0xff,
307 (tag >> 8) & 0xff,
308 (tag >> 16) & 0xff,
309 (tag >> 24) & 0xff,
310 (unsigned int)atom.offset,
311 (unsigned int)atom.size);
312 assert((unsigned int)atom.size < 0x7fffffff);// catching errors
313}
314#else
315#define print_atom(a,b)
316#endif
317
318
319static int mov_read_leaf(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 320{
5ca1d879 321 print_atom("leaf", atom);
b6a17df4 322
5ca1d879
ZK
323 if (atom.size>1)
324 url_fskip(pb, atom.size);
325/* url_seek(pb, atom_offset+atom.size, SEEK_SET); */
6cea494e
ZK
326 return 0;
327}
328
5ca1d879 329static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 330{
b6a17df4 331 int64_t total_size = 0;
5ca1d879 332 MOV_atom_t a;
6cea494e
ZK
333 int i;
334 int err = 0;
5ca1d879 335
6cea494e 336#ifdef DEBUG
5ca1d879 337 print_atom("default", atom);
6cea494e
ZK
338 debug_indent++;
339#endif
5cd62665 340
5ca1d879 341 a.offset = atom.offset;
6cea494e 342
9ed83b0a
ZK
343 if (atom.size < 0)
344 atom.size = 0x7fffffffffffffffLL;
5ca1d879
ZK
345 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
346 a.size = atom.size;
347 a.type=0L;
348 if(atom.size >= 8) {
349 a.size = get_be32(pb);
350 a.type = get_le32(pb);
6cea494e 351 }
5cd62665 352 total_size += 8;
5ca1d879 353 a.offset += 8;
baf25c9d 354 //av_log(NULL, AV_LOG_DEBUG, "type: %08x %.4s sz: %Lx %Lx %Lx\n", type, (char*)&type, size, atom.size, total_size);
5ca1d879
ZK
355 if (a.size == 1) { /* 64 bit extended size */
356 a.size = get_be64(pb) - 8;
357 a.offset += 8;
358 total_size += 8;
6cea494e 359 }
5ca1d879
ZK
360 if (a.size == 0) {
361 a.size = atom.size - total_size;
362 if (a.size <= 8)
5cd62665
ZK
363 break;
364 }
5ca1d879
ZK
365 for (i = 0; c->parse_table[i].type != 0L
366 && c->parse_table[i].type != a.type; i++)
5cd62665
ZK
367 /* empty */;
368
5ca1d879 369 a.size -= 8;
568e18b1
MN
370
371 if(a.size < 0)
372 break;
373
baf25c9d 374// av_log(NULL, AV_LOG_DEBUG, " i=%ld\n", i);
b6a17df4 375 if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
5ca1d879 376// url_seek(pb, atom.offset+atom.size, SEEK_SET);
6cea494e 377#ifdef DEBUG
5ca1d879 378 print_atom("unknown", a);
6cea494e 379#endif
5ca1d879 380 url_fskip(pb, a.size);
b6a17df4
ZK
381 } else {
382#ifdef DEBUG
383 //char b[5] = { type & 0xff, (type >> 8) & 0xff, (type >> 16) & 0xff, (type >> 24) & 0xff, 0 };
384 //print_atom(b, type, offset, size);
385#endif
5ca1d879 386 err = (c->parse_table[i].func)(c, pb, a);
b6a17df4 387 }
6cea494e 388
5ca1d879
ZK
389 a.offset += a.size;
390 total_size += a.size;
6cea494e
ZK
391 }
392
5ca1d879 393 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
baf25c9d 394 //av_log(NULL, AV_LOG_DEBUG, "RESET %Ld %Ld err:%d\n", atom.size, total_size, err);
5ca1d879 395 url_fskip(pb, atom.size - total_size);
5cd62665
ZK
396 }
397
6cea494e
ZK
398#ifdef DEBUG
399 debug_indent--;
400#endif
401 return err;
402}
403
5ca1d879 404static int mov_read_ctab(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 405{
5ca1d879
ZK
406 unsigned int len;
407 MOV_ctab_t *t;
568e18b1
MN
408#if 1
409 url_fskip(pb, atom.size); // for now
410#else
411 VERY VERY BROKEN, NEVER execute this, needs rewrite
5ca1d879
ZK
412 c->ctab = av_realloc(c->ctab, ++c->ctab_size);
413 t = c->ctab[c->ctab_size];
414 t->seed = get_be32(pb);
415 t->flags = get_be16(pb);
416 t->size = get_be16(pb) + 1;
417 len = 2 * t->size * 4;
418 if (len > 0) {
419 t->clrs = av_malloc(len); // 16bit A R G B
420 if (t->clrs)
421 get_buffer(pb, t->clrs, len);
3ffe3793 422 }
568e18b1 423#endif
5cd62665 424
0147f198
FR
425 return 0;
426}
427
5ca1d879 428static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 429{
5ca1d879 430 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
3ffe3793 431 int len = 0;
0c1a9eda 432 uint32_t type;
0c1a9eda 433 uint32_t ctype;
b6a17df4 434
5ca1d879 435 print_atom("hdlr", atom);
6cea494e
ZK
436
437 get_byte(pb); /* version */
438 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
439
440 /* component type */
441 ctype = get_le32(pb);
442 type = get_le32(pb); /* component subtype */
443
444#ifdef DEBUG
baf25c9d
GC
445 av_log(NULL, AV_LOG_DEBUG, "ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);
446 av_log(NULL, AV_LOG_DEBUG, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
6cea494e
ZK
447#endif
448#ifdef DEBUG
449/* XXX: yeah this is ugly... */
450 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
451 if(type == MKTAG('v', 'i', 'd', 'e'))
452 puts("hdlr: vide");
453 else if(type == MKTAG('s', 'o', 'u', 'n'))
454 puts("hdlr: soun");
455 } else if(ctype == 0) { /* MP4 */
456 if(type == MKTAG('v', 'i', 'd', 'e'))
457 puts("hdlr: vide");
458 else if(type == MKTAG('s', 'o', 'u', 'n'))
459 puts("hdlr: soun");
460 else if(type == MKTAG('o', 'd', 's', 'm'))
461 puts("hdlr: odsm");
462 else if(type == MKTAG('s', 'd', 's', 'm'))
463 puts("hdlr: sdsm");
464 } else puts("hdlr: meta");
465#endif
466
467 if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */
0147f198
FR
468 /* helps parsing the string hereafter... */
469 c->mp4 = 0;
6cea494e
ZK
470 if(type == MKTAG('v', 'i', 'd', 'e'))
471 st->codec.codec_type = CODEC_TYPE_VIDEO;
472 else if(type == MKTAG('s', 'o', 'u', 'n'))
473 st->codec.codec_type = CODEC_TYPE_AUDIO;
474 } else if(ctype == 0) { /* MP4 */
0147f198
FR
475 /* helps parsing the string hereafter... */
476 c->mp4 = 1;
6cea494e
ZK
477 if(type == MKTAG('v', 'i', 'd', 'e'))
478 st->codec.codec_type = CODEC_TYPE_VIDEO;
479 else if(type == MKTAG('s', 'o', 'u', 'n'))
480 st->codec.codec_type = CODEC_TYPE_AUDIO;
481 }
482 get_be32(pb); /* component manufacture */
483 get_be32(pb); /* component flags */
484 get_be32(pb); /* component flags mask */
485
5ca1d879 486 if(atom.size <= 24)
6cea494e
ZK
487 return 0; /* nothing left to read */
488 /* XXX: MP4 uses a C string, not a pascal one */
489 /* component name */
0147f198
FR
490
491 if(c->mp4) {
492 /* .mp4: C string */
5ca1d879 493 while(get_byte(pb) && (++len < (atom.size - 24)));
0147f198
FR
494 } else {
495 /* .mov: PASCAL string */
9ed83b0a
ZK
496#ifdef DEBUG
497 char* buf;
498#endif
0147f198 499 len = get_byte(pb);
5ca1d879 500#ifdef DEBUG
b6a17df4
ZK
501 buf = (uint8_t*) av_malloc(len+1);
502 if (buf) {
503 get_buffer(pb, buf, len);
b6a17df4 504 buf[len] = '\0';
baf25c9d 505 av_log(NULL, AV_LOG_DEBUG, "**buf='%s'\n", buf);
b6a17df4
ZK
506 av_free(buf);
507 } else
6cea494e 508#endif
5ca1d879 509 url_fskip(pb, len);
6cea494e 510 }
5cd62665 511
3c13647a 512 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
6cea494e
ZK
513 return 0;
514}
515
5ca1d879 516static int mov_mp4_read_descr_len(ByteIOContext *pb)
0e7eed09 517{
5cd62665 518 int len = 0;
5ca1d879
ZK
519 int count = 4;
520 while (count--) {
5cd62665
ZK
521 int c = get_byte(pb);
522 len = (len << 7) | (c & 0x7f);
5ca1d879
ZK
523 if (!(c & 0x80))
524 break;
0e7eed09
FB
525 }
526 return len;
527}
528
5ca1d879 529static int mov_mp4_read_descr(ByteIOContext *pb, int *tag)
0e7eed09
FB
530{
531 int len;
532 *tag = get_byte(pb);
5ca1d879 533 len = mov_mp4_read_descr_len(pb);
0e7eed09 534#ifdef DEBUG
baf25c9d 535 av_log(NULL, AV_LOG_DEBUG, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
0e7eed09
FB
536#endif
537 return len;
538}
539
5cd62665
ZK
540static inline unsigned int get_be24(ByteIOContext *s)
541{
542 unsigned int val;
543 val = get_byte(s) << 16;
544 val |= get_byte(s) << 8;
545 val |= get_byte(s);
546 return val;
547}
548
5ca1d879 549static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
5cd62665 550{
5cd62665
ZK
551 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
552 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
5ca1d879 553 int64_t start_pos = url_ftell(pb);
5cd62665 554 int tag, len;
b6a17df4 555
5ca1d879 556 print_atom("esds", atom);
5cd62665
ZK
557
558 /* Well, broken but suffisant for some MP4 streams */
559 get_be32(pb); /* version + flags */
5ca1d879 560 len = mov_mp4_read_descr(pb, &tag);
5cd62665
ZK
561 if (tag == MP4ESDescrTag) {
562 get_be16(pb); /* ID */
563 get_byte(pb); /* priority */
564 } else
565 get_be16(pb); /* ID */
566
5ca1d879 567 len = mov_mp4_read_descr(pb, &tag);
5cd62665
ZK
568 if (tag == MP4DecConfigDescrTag) {
569 sc->esds.object_type_id = get_byte(pb);
5ca1d879
ZK
570 sc->esds.stream_type = get_byte(pb);
571 sc->esds.buffer_size_db = get_be24(pb);
5cd62665
ZK
572 sc->esds.max_bitrate = get_be32(pb);
573 sc->esds.avg_bitrate = get_be32(pb);
574
5ca1d879 575 len = mov_mp4_read_descr(pb, &tag);
baf25c9d 576 //av_log(NULL, AV_LOG_DEBUG, "LEN %d TAG %d m:%d a:%d\n", len, tag, sc->esds.max_bitrate, sc->esds.avg_bitrate);
5cd62665
ZK
577 if (tag == MP4DecSpecificDescrTag) {
578#ifdef DEBUG
baf25c9d 579 av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
5cd62665 580#endif
3129cd01 581 st->codec.extradata = (uint8_t*) av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
5ca1d879
ZK
582 if (st->codec.extradata) {
583 get_buffer(pb, st->codec.extradata, len);
584 st->codec.extradata_size = len;
5cd62665
ZK
585 }
586 }
587 }
588 /* in any case, skip garbage */
5ca1d879 589 url_fskip(pb, atom.size - ((url_ftell(pb) - start_pos)));
5cd62665
ZK
590 return 0;
591}
592
5ca1d879
ZK
593/* this atom contains actual media data */
594static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 595{
5ca1d879
ZK
596 print_atom("mdat", atom);
597
598 if(atom.size == 0) /* wrong one (MP4) */
599 return 0;
600 c->found_mdat=1;
601 c->mdat_offset = atom.offset;
602 c->mdat_size = atom.size;
603 if(c->found_moov)
604 return 1; /* found both, just go */
605 url_fskip(pb, atom.size);
606 return 0; /* now go for moov */
607}
608
609/* this atom should contain all header atoms */
610static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
611{
612 int err;
b6a17df4 613
5ca1d879 614 print_atom("moov", atom);
b6a17df4 615
5ca1d879
ZK
616 err = mov_read_default(c, pb, atom);
617 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
618 /* so we don't parse the whole file if over a network */
619 c->found_moov=1;
620 if(c->found_mdat)
621 return 1; /* found both, just go */
622 return 0; /* now go for mdat */
623}
624
625
626static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
627{
5ca1d879
ZK
628 print_atom("mdhd", atom);
629
630 get_byte(pb); /* version */
631
632 get_byte(pb); get_byte(pb);
633 get_byte(pb); /* flags */
634
635 get_be32(pb); /* creation time */
636 get_be32(pb); /* modification time */
637
baf25c9d 638 c->streams[c->fc->nb_streams-1]->time_scale = get_be32(pb);
5ca1d879
ZK
639
640#ifdef DEBUG
baf25c9d 641 av_log(NULL, AV_LOG_DEBUG, "track[%i].time_scale = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->time_scale); /* time scale */
5ca1d879
ZK
642#endif
643 get_be32(pb); /* duration */
644
645 get_be16(pb); /* language */
646 get_be16(pb); /* quality */
647
648 return 0;
649}
650
651static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
652{
653 print_atom("mvhd", atom);
654
655 get_byte(pb); /* version */
656 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
657
658 get_be32(pb); /* creation time */
659 get_be32(pb); /* modification time */
660 c->time_scale = get_be32(pb); /* time scale */
661#ifdef DEBUG
baf25c9d 662 av_log(NULL, AV_LOG_DEBUG, "time scale = %i\n", c->time_scale);
5ca1d879
ZK
663#endif
664 c->duration = get_be32(pb); /* duration */
665 get_be32(pb); /* preferred scale */
666
667 get_be16(pb); /* preferred volume */
668
669 url_fskip(pb, 10); /* reserved */
670
671 url_fskip(pb, 36); /* display matrix */
672
673 get_be32(pb); /* preview time */
674 get_be32(pb); /* preview duration */
675 get_be32(pb); /* poster time */
676 get_be32(pb); /* selection time */
677 get_be32(pb); /* selection duration */
678 get_be32(pb); /* current time */
679 get_be32(pb); /* next track ID */
680
681 return 0;
682}
683
9ed83b0a
ZK
684static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
685{
686 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
687
568e18b1
MN
688 if((uint64_t)atom.size > (1<<30))
689 return -1;
690
9ed83b0a
ZK
691 // currently SVQ3 decoder expect full STSD header - so let's fake it
692 // this should be fixed and just SMI header should be passed
693 av_free(st->codec.extradata);
694 st->codec.extradata_size = 0x5a + atom.size;
3129cd01 695 st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
9ed83b0a
ZK
696
697 if (st->codec.extradata) {
9ed83b0a
ZK
698 strcpy(st->codec.extradata, "SVQ3"); // fake
699 get_buffer(pb, st->codec.extradata + 0x5a, atom.size);
baf25c9d 700 //av_log(NULL, AV_LOG_DEBUG, "Reading SMI %Ld %s\n", atom.size, (char*)st->codec.extradata + 0x5a);
9ed83b0a
ZK
701 } else
702 url_fskip(pb, atom.size);
703
704 return 0;
705}
5ca1d879 706
169eb021
MM
707static int mov_read_avcC(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
708{
709 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
710
568e18b1
MN
711 if((uint64_t)atom.size > (1<<30))
712 return -1;
713
169eb021
MM
714 av_free(st->codec.extradata);
715
716 st->codec.extradata_size = atom.size;
717 st->codec.extradata = (uint8_t*) av_mallocz(st->codec.extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
718
719 if (st->codec.extradata) {
720 get_buffer(pb, st->codec.extradata, atom.size);
721 } else
722 url_fskip(pb, atom.size);
723
724 return 0;
725}
726
5ca1d879
ZK
727static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
728{
729 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
730 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
568e18b1 731 unsigned int i, entries;
5ca1d879
ZK
732
733 print_atom("stco", atom);
734
735 get_byte(pb); /* version */
736 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
737
738 entries = get_be32(pb);
568e18b1
MN
739
740 if(entries >= UINT_MAX/sizeof(int64_t))
741 return -1;
742
5ca1d879
ZK
743 sc->chunk_count = entries;
744 sc->chunk_offsets = (int64_t*) av_malloc(entries * sizeof(int64_t));
745 if (!sc->chunk_offsets)
746 return -1;
747 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
748 for(i=0; i<entries; i++) {
749 sc->chunk_offsets[i] = get_be32(pb);
750 }
751 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
752 for(i=0; i<entries; i++) {
753 sc->chunk_offsets[i] = get_be64(pb);
754 }
755 } else
756 return -1;
757#ifdef DEBUG
758/*
759 for(i=0; i<entries; i++) {
baf25c9d 760 av_log(NULL, AV_LOG_DEBUG, "chunk offset=0x%Lx\n", sc->chunk_offsets[i]);
5ca1d879
ZK
761 }
762*/
763#endif
764 return 0;
765}
766
767static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
768{
769 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
fd6e513e 770 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
5ca1d879
ZK
771 int entries, frames_per_sample;
772 uint32_t format;
773
b595afaa
MM
774 /* for palette traversal */
775 int color_depth;
776 int color_start;
777 int color_count;
778 int color_end;
779 int color_index;
780 int color_dec;
781 int color_greyscale;
782 unsigned char *color_table;
783 int j;
784 unsigned char r, g, b;
785
5ca1d879 786 print_atom("stsd", atom);
6cea494e
ZK
787
788 get_byte(pb); /* version */
789 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
790
6cea494e
ZK
791 entries = get_be32(pb);
792
891f64b3 793 while(entries--) { //Parsing Sample description table
b6a17df4 794 enum CodecID id;
5ca1d879 795 int size = get_be32(pb); /* size */
6cea494e 796 format = get_le32(pb); /* data format */
5cd62665 797
6cea494e
ZK
798 get_be32(pb); /* reserved */
799 get_be16(pb); /* reserved */
800 get_be16(pb); /* index */
0e7eed09
FB
801
802 /* for MPEG4: set codec type by looking for it */
803 id = codec_get_id(mov_video_tags, format);
804 if (id >= 0) {
805 AVCodec *codec;
b6a17df4 806 codec = avcodec_find_decoder(id);
0e7eed09 807 if (codec)
b6a17df4 808 st->codec.codec_type = codec->type;
0e7eed09
FB
809 }
810#ifdef DEBUG
baf25c9d 811 av_log(NULL, AV_LOG_DEBUG, "size=%d 4CC= %c%c%c%c codec_type=%d\n",
5cd62665 812 size,
0e7eed09
FB
813 (format >> 0) & 0xff,
814 (format >> 8) & 0xff,
815 (format >> 16) & 0xff,
816 (format >> 24) & 0xff,
817 st->codec.codec_type);
818#endif
5cd62665 819 st->codec.codec_tag = format;
5ca1d879
ZK
820 if(st->codec.codec_type==CODEC_TYPE_VIDEO) {
821 MOV_atom_t a = { 0, 0, 0 };
822 st->codec.codec_id = id;
6cea494e
ZK
823 get_be16(pb); /* version */
824 get_be16(pb); /* revision level */
825 get_be32(pb); /* vendor */
826 get_be32(pb); /* temporal quality */
827 get_be32(pb); /* spacial quality */
828 st->codec.width = get_be16(pb); /* width */
829 st->codec.height = get_be16(pb); /* height */
0e7eed09
FB
830#if 1
831 if (st->codec.codec_id == CODEC_ID_MPEG4) {
832 /* in some MPEG4 the width/height are not correct, so
833 we ignore this info */
834 st->codec.width = 0;
835 st->codec.height = 0;
836 }
837#endif
6cea494e
ZK
838 get_be32(pb); /* horiz resolution */
839 get_be32(pb); /* vert resolution */
840 get_be32(pb); /* data size, always 0 */
5cd62665 841 frames_per_sample = get_be16(pb); /* frames per samples */
6cea494e 842#ifdef DEBUG
baf25c9d 843 av_log(NULL, AV_LOG_DEBUG, "frames/samples = %d\n", frames_per_sample);
6cea494e 844#endif
b6a17df4 845 get_buffer(pb, (uint8_t *)st->codec.codec_name, 32); /* codec name */
5cd62665
ZK
846
847 st->codec.bits_per_sample = get_be16(pb); /* depth */
848 st->codec.color_table_id = get_be16(pb); /* colortable id */
6cea494e 849
14342fd5 850/* These are set in mov_read_stts and might already be set!
14bea432
MN
851 st->codec.frame_rate = 25;
852 st->codec.frame_rate_base = 1;
14342fd5 853*/
5ca1d879 854 size -= (16+8*4+2+32+2*2);
5cd62665
ZK
855#if 0
856 while (size >= 8) {
5ca1d879 857 MOV_atom_t a;
0c1a9eda 858 int64_t start_pos;
5cd62665 859
5ca1d879
ZK
860 a.size = get_be32(pb);
861 a.type = get_le32(pb);
5cd62665 862 size -= 8;
0e7eed09 863#ifdef DEBUG
baf25c9d 864 av_log(NULL, AV_LOG_DEBUG, "VIDEO: atom_type=%c%c%c%c atom.size=%Ld size_left=%d\n",
5ca1d879
ZK
865 (a.type >> 0) & 0xff,
866 (a.type >> 8) & 0xff,
867 (a.type >> 16) & 0xff,
868 (a.type >> 24) & 0xff,
869 a.size, size);
0e7eed09
FB
870#endif
871 start_pos = url_ftell(pb);
872
5ca1d879 873 switch(a.type) {
0e7eed09
FB
874 case MKTAG('e', 's', 'd', 's'):
875 {
876 int tag, len;
877 /* Well, broken but suffisant for some MP4 streams */
878 get_be32(pb); /* version + flags */
5ca1d879 879 len = mov_mp4_read_descr(pb, &tag);
0e7eed09
FB
880 if (tag == 0x03) {
881 /* MP4ESDescrTag */
882 get_be16(pb); /* ID */
883 get_byte(pb); /* priority */
5ca1d879 884 len = mov_mp4_read_descr(pb, &tag);
0e7eed09
FB
885 if (tag != 0x04)
886 goto fail;
887 /* MP4DecConfigDescrTag */
888 get_byte(pb); /* objectTypeId */
889 get_be32(pb); /* streamType + buffer size */
5cd62665 890 get_be32(pb); /* max bit rate */
0e7eed09 891 get_be32(pb); /* avg bit rate */
891f64b3 892 len = mov_mp4_read_descr(pb, &tag);
0e7eed09
FB
893 if (tag != 0x05)
894 goto fail;
895 /* MP4DecSpecificDescrTag */
896#ifdef DEBUG
baf25c9d 897 av_log(NULL, AV_LOG_DEBUG, "Specific MPEG4 header len=%d\n", len);
0e7eed09
FB
898#endif
899 sc->header_data = av_mallocz(len);
900 if (sc->header_data) {
901 get_buffer(pb, sc->header_data, len);
5cd62665 902 sc->header_len = len;
0e7eed09
FB
903 }
904 }
905 /* in any case, skip garbage */
906 }
907 break;
908 default:
909 break;
910 }
5cd62665 911 fail:
baf25c9d 912 av_log(NULL, AV_LOG_DEBUG, "ATOMENEWSIZE %Ld %d\n", atom.size, url_ftell(pb) - start_pos);
5ca1d879
ZK
913 if (atom.size > 8) {
914 url_fskip(pb, (atom.size - 8) -
5cd62665 915 ((url_ftell(pb) - start_pos)));
5ca1d879 916 size -= atom.size - 8;
5cd62665
ZK
917 }
918 }
0e7eed09
FB
919 if (size > 0) {
920 /* unknown extension */
921 url_fskip(pb, size);
922 }
5cd62665 923#else
b595afaa
MM
924
925 /* figure out the palette situation */
926 color_depth = st->codec.bits_per_sample & 0x1F;
927 color_greyscale = st->codec.bits_per_sample & 0x20;
928
929 /* if the depth is 2, 4, or 8 bpp, file is palettized */
930 if ((color_depth == 2) || (color_depth == 4) ||
931 (color_depth == 8)) {
932
933 if (color_greyscale) {
934
935 /* compute the greyscale palette */
936 color_count = 1 << color_depth;
937 color_index = 255;
938 color_dec = 256 / (color_count - 1);
939 for (j = 0; j < color_count; j++) {
940 r = g = b = color_index;
941 c->palette_control.palette[j] =
942 (r << 16) | (g << 8) | (b);
943 color_index -= color_dec;
944 if (color_index < 0)
945 color_index = 0;
946 }
947
948 } else if (st->codec.color_table_id & 0x08) {
949
950 /* if flag bit 3 is set, use the default palette */
951 color_count = 1 << color_depth;
952 if (color_depth == 2)
a90466f7 953 color_table = ff_qt_default_palette_4;
b595afaa 954 else if (color_depth == 4)
a90466f7 955 color_table = ff_qt_default_palette_16;
b595afaa 956 else
a90466f7 957 color_table = ff_qt_default_palette_256;
b595afaa
MM
958
959 for (j = 0; j < color_count; j++) {
960 r = color_table[j * 4 + 0];
961 g = color_table[j * 4 + 1];
962 b = color_table[j * 4 + 2];
963 c->palette_control.palette[j] =
964 (r << 16) | (g << 8) | (b);
965 }
966
967 } else {
968
969 /* load the palette from the file */
970 color_start = get_be32(pb);
971 color_count = get_be16(pb);
972 color_end = get_be16(pb);
973 for (j = color_start; j <= color_end; j++) {
974 /* each R, G, or B component is 16 bits;
975 * only use the top 8 bits; skip alpha bytes
976 * up front */
977 get_byte(pb);
978 get_byte(pb);
979 r = get_byte(pb);
980 get_byte(pb);
981 g = get_byte(pb);
982 get_byte(pb);
983 b = get_byte(pb);
984 get_byte(pb);
985 c->palette_control.palette[j] =
986 (r << 16) | (g << 8) | (b);
987 }
988 }
989
990 st->codec.palctrl = &c->palette_control;
991 st->codec.palctrl->palette_changed = 1;
992 } else
993 st->codec.palctrl = NULL;
994
5ca1d879
ZK
995 a.size = size;
996 mov_read_default(c, pb, a);
5cd62665
ZK
997#endif
998 } else {
891f64b3 999 st->codec.codec_id = codec_get_id(mov_audio_tags, format);
d663a1fd 1000 if(st->codec.codec_id==CODEC_ID_AMR_NB || st->codec.codec_id==CODEC_ID_AMR_WB) //from TS26.244
891f64b3 1001 {
1002#ifdef DEBUG
baf25c9d 1003 av_log(NULL, AV_LOG_DEBUG, "AMR-NB or AMR-WB audio identified!!\n");
891f64b3 1004#endif
1005 get_be32(pb);get_be32(pb); //Reserved_8
1006 get_be16(pb);//Reserved_2
1007 get_be16(pb);//Reserved_2
1008 get_be32(pb);//Reserved_4
1009 get_be16(pb);//TimeScale
1010 get_be16(pb);//Reserved_2
1011
1012 //AMRSpecificBox.(10 bytes)
d663a1fd 1013
891f64b3 1014 get_be32(pb); //size
891f64b3 1015 get_be32(pb); //type=='damr'
891f64b3 1016 get_be32(pb); //vendor
1017 get_byte(pb); //decoder version
1018 get_be16(pb); //mode_set
1019 get_byte(pb); //mode_change_period
1020 get_byte(pb); //frames_per_sample
6cea494e 1021
25c4950e 1022 st->duration = AV_NOPTS_VALUE;//Not possible to get from this info, must count number of AMR frames
d663a1fd
MN
1023 if(st->codec.codec_id==CODEC_ID_AMR_NB)
1024 {
1025 st->codec.sample_rate=8000;
1026 st->codec.channels=1;
1027 }
1028 else //AMR-WB
1029 {
1030 st->codec.sample_rate=16000;
1031 st->codec.channels=1;
1032 }
891f64b3 1033 st->codec.bits_per_sample=16;
1034 st->codec.bit_rate=0; /*It is not possible to tell this before we have
1035 an audio frame and even then every frame can be different*/
5cd62665 1036 }
14342fd5
JC
1037 else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 's' ))
1038 {
1039 //This is some stuff for the hint track, lets ignore it!
1040 //Do some mp4 auto detect.
1041 c->mp4=1;
1042 size-=(16);
1043 url_fskip(pb, size); /* The mp4s atom also contians a esds atom that we can skip*/
1044 }
2768b0d9
TR
1045 else if( st->codec.codec_tag == MKTAG( 'm', 'p', '4', 'a' ))
1046 {
1047 /* Handle mp4 audio tag */
1048 get_be32(pb); /* version */
1049 get_be32(pb);
1050 st->codec.channels = get_be16(pb); /* channels */
1051 st->codec.bits_per_sample = get_be16(pb); /* bits per sample */
1052 get_be32(pb);
1053 st->codec.sample_rate = get_be16(pb); /* sample rate, not always correct */
1054 get_be16(pb);
1055 c->mp4=1;
3ec34bfd 1056 {
2768b0d9
TR
1057 MOV_atom_t a = { format, url_ftell(pb), size - (20 + 20 + 8) };
1058 mov_read_default(c, pb, a);
3ec34bfd 1059 }
2768b0d9
TR
1060 /* Get correct sample rate from extradata */
1061 if(st->codec.extradata_size) {
1062 const int samplerate_table[] = {
1063 96000, 88200, 64000, 48000, 44100, 32000,
1064 24000, 22050, 16000, 12000, 11025, 8000,
1065 7350, 0, 0, 0
1066 };
1067 unsigned char *px = st->codec.extradata;
1068 // 5 bits objectTypeIndex, 4 bits sampleRateIndex, 4 bits channels
1069 int samplerate_index = ((px[0] & 7) << 1) + ((px[1] >> 7) & 1);
1070 st->codec.sample_rate = samplerate_table[samplerate_index];
1071 }
1072 }
14342fd5
JC
1073 else if(size>=(16+20))
1074 {//16 bytes read, reading atleast 20 more
c3ea71a0 1075 uint16_t version;
14342fd5 1076#ifdef DEBUG
baf25c9d 1077 av_log(NULL, AV_LOG_DEBUG, "audio size=0x%X\n",size);
14342fd5 1078#endif
c3ea71a0 1079 version = get_be16(pb); /* version */
891f64b3 1080 get_be16(pb); /* revision level */
1081 get_be32(pb); /* vendor */
1082
1083 st->codec.channels = get_be16(pb); /* channel count */
1084 st->codec.bits_per_sample = get_be16(pb); /* sample size */
1085
1086 /* handle specific s8 codec */
1087 get_be16(pb); /* compression id = 0*/
1088 get_be16(pb); /* packet size = 0 */
1089
1090 st->codec.sample_rate = ((get_be32(pb) >> 16));
baf25c9d 1091 //av_log(NULL, AV_LOG_DEBUG, "CODECID %d %d %.4s\n", st->codec.codec_id, CODEC_ID_PCM_S16BE, (char*)&format);
891f64b3 1092
1093 switch (st->codec.codec_id) {
1094 case CODEC_ID_PCM_S16BE:
1095 if (st->codec.bits_per_sample == 8)
1096 st->codec.codec_id = CODEC_ID_PCM_S8;
1097 /* fall */
1098 case CODEC_ID_PCM_U8:
1099 st->codec.bit_rate = st->codec.sample_rate * 8;
1100 break;
1101 default:
1102 ;
1103 }
14342fd5
JC
1104
1105 //Read QT version 1 fields. In version 0 theese dont exist
1106#ifdef DEBUG
baf25c9d
GC
1107 av_log(NULL, AV_LOG_DEBUG, "version =%d mp4=%d\n",version,c->mp4);
1108 av_log(NULL, AV_LOG_DEBUG, "size-(16+20+16)=%d\n",size-(16+20+16));
14342fd5
JC
1109#endif
1110 if((version==1) && size>=(16+20+16))
1111 {
1112 get_be32(pb); /* samples per packet */
1113 get_be32(pb); /* bytes per packet */
1114 get_be32(pb); /* bytes per frame */
1115 get_be32(pb); /* bytes per sample */
1116 if(size>(16+20+16))
1117 {
1118 //Optional, additional atom-based fields
c3ea71a0 1119 MOV_atom_t a = { format, url_ftell(pb), size - (16 + 20 + 16 + 8) };
14342fd5 1120#ifdef DEBUG
baf25c9d 1121 av_log(NULL, AV_LOG_DEBUG, "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 ),
14342fd5
JC
1122 (format >> 0) & 0xff,
1123 (format >> 8) & 0xff,
1124 (format >> 16) & 0xff,
1125 (format >> 24) & 0xff);
1126#endif
14342fd5
JC
1127 mov_read_default(c, pb, a);
1128 }
1129 }
1130 else
1131 {
1132 //We should be down to 0 bytes here, but lets make sure.
1133 size-=(16+20);
1134#ifdef DEBUG
1135 if(size>0)
baf25c9d 1136 av_log(NULL, AV_LOG_DEBUG, "skipping 0x%X bytes\n",size-(16+20));
14342fd5
JC
1137#endif
1138 url_fskip(pb, size);
1139 }
1140 }
1141 else
1142 {
1143 size-=16;
1144 //Unknown size, but lets do our best and skip the rest.
1145#ifdef DEBUG
baf25c9d 1146 av_log(NULL, AV_LOG_DEBUG, "Strange size, skipping 0x%X bytes\n",size);
14342fd5
JC
1147#endif
1148 url_fskip(pb, size);
891f64b3 1149 }
6cea494e 1150 }
6cea494e 1151 }
5cd62665 1152
6cea494e
ZK
1153 return 0;
1154}
1155
5ca1d879 1156static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 1157{
5ca1d879
ZK
1158 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1159 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
568e18b1 1160 unsigned int i, entries;
b6a17df4 1161
5ca1d879 1162 print_atom("stsc", atom);
5cd62665 1163
6cea494e
ZK
1164 get_byte(pb); /* version */
1165 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1166
1167 entries = get_be32(pb);
568e18b1
MN
1168
1169 if(entries >= UINT_MAX / sizeof(MOV_sample_to_chunk_tbl))
1170 return -1;
1171
3ffe3793 1172#ifdef DEBUG
baf25c9d 1173av_log(NULL, AV_LOG_DEBUG, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
3ffe3793 1174#endif
6cea494e 1175 sc->sample_to_chunk_sz = entries;
b6a17df4
ZK
1176 sc->sample_to_chunk = (MOV_sample_to_chunk_tbl*) av_malloc(entries * sizeof(MOV_sample_to_chunk_tbl));
1177 if (!sc->sample_to_chunk)
1178 return -1;
6cea494e
ZK
1179 for(i=0; i<entries; i++) {
1180 sc->sample_to_chunk[i].first = get_be32(pb);
1181 sc->sample_to_chunk[i].count = get_be32(pb);
1182 sc->sample_to_chunk[i].id = get_be32(pb);
1183#ifdef DEBUG
baf25c9d 1184/* av_log(NULL, AV_LOG_DEBUG, "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); */
6cea494e
ZK
1185#endif
1186 }
1187 return 0;
1188}
1189
247d56f5
BB
1190static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1191{
1192 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1193 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
568e18b1 1194 unsigned int i, entries;
247d56f5
BB
1195
1196 print_atom("stss", atom);
1197
1198 get_byte(pb); /* version */
1199 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1200
1201 entries = get_be32(pb);
568e18b1
MN
1202
1203 if(entries >= UINT_MAX / sizeof(long))
1204 return -1;
1205
247d56f5
BB
1206 sc->keyframe_count = entries;
1207#ifdef DEBUG
1208 av_log(NULL, AV_LOG_DEBUG, "keyframe_count = %ld\n", sc->keyframe_count);
1209#endif
1210 sc->keyframes = (long*) av_malloc(entries * sizeof(long));
1211 if (!sc->keyframes)
1212 return -1;
1213 for(i=0; i<entries; i++) {
1214 sc->keyframes[i] = get_be32(pb);
1215#ifdef DEBUG
1216/* av_log(NULL, AV_LOG_DEBUG, "keyframes[]=%ld\n", sc->keyframes[i]); */
1217#endif
1218 }
1219 return 0;
1220}
1221
5ca1d879 1222static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 1223{
5ca1d879
ZK
1224 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1225 MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
568e18b1 1226 unsigned int i, entries;
b6a17df4 1227
5ca1d879 1228 print_atom("stsz", atom);
5cd62665 1229
6cea494e
ZK
1230 get_byte(pb); /* version */
1231 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
5cd62665 1232
6cea494e
ZK
1233 sc->sample_size = get_be32(pb);
1234 entries = get_be32(pb);
568e18b1
MN
1235 if(entries >= UINT_MAX / sizeof(long))
1236 return -1;
1237
6cea494e 1238 sc->sample_count = entries;
0147f198 1239#ifdef DEBUG
baf25c9d 1240 av_log(NULL, AV_LOG_DEBUG, "sample_size = %ld sample_count = %ld\n", sc->sample_size, sc->sample_count);
0147f198 1241#endif
6cea494e
ZK
1242 if(sc->sample_size)
1243 return 0; /* there isn't any table following */
b6a17df4
ZK
1244 sc->sample_sizes = (long*) av_malloc(entries * sizeof(long));
1245 if (!sc->sample_sizes)
1246 return -1;
6cea494e
ZK
1247 for(i=0; i<entries; i++) {
1248 sc->sample_sizes[i] = get_be32(pb);
1249#ifdef DEBUG
7dbab4a9 1250 av_log(NULL, AV_LOG_DEBUG, "sample_sizes[]=%ld\n", sc->sample_sizes[i]);
6cea494e
ZK
1251#endif
1252 }
1253 return 0;
1254}
1255
5ca1d879 1256static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
0147f198 1257{
5ca1d879 1258 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
fd6e513e 1259 //MOVStreamContext *sc = (MOVStreamContext *)st->priv_data;
568e18b1 1260 unsigned int i, entries;
d957696f
MN
1261 int64_t duration=0;
1262 int64_t total_sample_count=0;
b6a17df4 1263
5ca1d879 1264 print_atom("stts", atom);
0147f198
FR
1265
1266 get_byte(pb); /* version */
1267 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
1268 entries = get_be32(pb);
568e18b1
MN
1269 if(entries >= UINT_MAX / sizeof(uint64_t))
1270 return -1;
891f64b3 1271
baf25c9d
GC
1272 c->streams[c->fc->nb_streams-1]->stts_count = entries;
1273 c->streams[c->fc->nb_streams-1]->stts_data = (uint64_t*) av_malloc(entries * sizeof(uint64_t));
891f64b3 1274
3ffe3793 1275#ifdef DEBUG
baf25c9d 1276av_log(NULL, AV_LOG_DEBUG, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
3ffe3793 1277#endif
0147f198 1278 for(i=0; i<entries; i++) {
baf25c9d
GC
1279 int32_t sample_duration;
1280 int32_t sample_count;
0147f198 1281
891f64b3 1282 sample_count=get_be32(pb);
0147f198 1283 sample_duration = get_be32(pb);
baf25c9d 1284 c->streams[c->fc->nb_streams - 1]->stts_data[i] = (uint64_t)sample_count<<32 | (uint64_t)sample_duration;
891f64b3 1285#ifdef DEBUG
baf25c9d 1286 av_log(NULL, AV_LOG_DEBUG, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
891f64b3 1287#endif
1288 duration+=sample_duration*sample_count;
1289 total_sample_count+=sample_count;
1290
1291#if 0 //We calculate an average instead, needed by .mp4-files created with nec e606 3g phone
0147f198
FR
1292
1293 if (!i && st->codec.codec_type==CODEC_TYPE_VIDEO) {
14bea432 1294 st->codec.frame_rate_base = sample_duration ? sample_duration : 1;
baf25c9d 1295 st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
0147f198 1296#ifdef DEBUG
baf25c9d 1297 av_log(NULL, AV_LOG_DEBUG, "VIDEO FRAME RATE= %i (sd= %i)\n", st->codec.frame_rate, sample_duration);
0147f198
FR
1298#endif
1299 }
891f64b3 1300#endif
1301 }
1302
14342fd5
JC
1303 /*The stsd atom which contain codec type sometimes comes after the stts so we cannot check for codec_type*/
1304 if(duration>0)
891f64b3 1305 {
d957696f
MN
1306 av_reduce(
1307 &st->codec.frame_rate,
1308 &st->codec.frame_rate_base,
baf25c9d 1309 c->streams[c->fc->nb_streams-1]->time_scale * total_sample_count,
d957696f
MN
1310 duration,
1311 INT_MAX
1312 );
14342fd5 1313
891f64b3 1314#ifdef DEBUG
baf25c9d 1315 av_log(NULL, AV_LOG_DEBUG, "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->fc->nb_streams-1]->time_scale);
891f64b3 1316#endif
0147f198 1317 }
14342fd5
JC
1318 else
1319 {
1320 st->codec.frame_rate_base = 1;
baf25c9d 1321 st->codec.frame_rate = c->streams[c->fc->nb_streams-1]->time_scale;
14342fd5 1322 }
0147f198
FR
1323 return 0;
1324}
1325
5ca1d879
ZK
1326static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1327{
1328 AVStream *st;
1329 MOVStreamContext *sc;
1330
1331 print_atom("trak", atom);
1332
1333 st = av_new_stream(c->fc, c->fc->nb_streams);
1334 if (!st) return -2;
1335 sc = (MOVStreamContext*) av_mallocz(sizeof(MOVStreamContext));
1336 if (!sc) {
1337 av_free(st);
1338 return -1;
1339 }
1340
1341 sc->sample_to_chunk_index = -1;
1342 st->priv_data = sc;
1343 st->codec.codec_type = CODEC_TYPE_MOV_OTHER;
25c4950e
FB
1344 st->start_time = 0; /* XXX: check */
1345 st->duration = (c->duration * (int64_t)AV_TIME_BASE) / c->time_scale;
5ca1d879
ZK
1346 c->streams[c->fc->nb_streams-1] = sc;
1347
1348 return mov_read_default(c, pb, atom);
1349}
1350
1351static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1352{
1353 AVStream *st;
1354
1355 print_atom("tkhd", atom);
1356
1357 st = c->fc->streams[c->fc->nb_streams-1];
1358
1359 get_byte(pb); /* version */
1360
1361 get_byte(pb); get_byte(pb);
1362 get_byte(pb); /* flags */
1363 /*
1364 MOV_TRACK_ENABLED 0x0001
1365 MOV_TRACK_IN_MOVIE 0x0002
1366 MOV_TRACK_IN_PREVIEW 0x0004
1367 MOV_TRACK_IN_POSTER 0x0008
1368 */
1369
1370 get_be32(pb); /* creation time */
1371 get_be32(pb); /* modification time */
1372 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1373 get_be32(pb); /* reserved */
25c4950e
FB
1374 st->start_time = 0; /* check */
1375 st->duration = (get_be32(pb) * (int64_t)AV_TIME_BASE) / c->time_scale; /* duration */
5ca1d879
ZK
1376 get_be32(pb); /* reserved */
1377 get_be32(pb); /* reserved */
1378
1379 get_be16(pb); /* layer */
1380 get_be16(pb); /* alternate group */
1381 get_be16(pb); /* volume */
1382 get_be16(pb); /* reserved */
1383
1384 url_fskip(pb, 36); /* display matrix */
1385
1386 /* those are fixed-point */
1387 st->codec.width = get_be32(pb) >> 16; /* track width */
1388 st->codec.height = get_be32(pb) >> 16; /* track height */
1389
1390 return 0;
1391}
1392
1393/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1394/* like the files created with Adobe Premiere 5.0, for samples see */
1395/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1396static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1397{
1398 int err;
5ca1d879
ZK
1399
1400#ifdef DEBUG
1401 print_atom("wide", atom);
1402 debug_indent++;
1403#endif
1404 if (atom.size < 8)
1405 return 0; /* continue */
1406 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1407 url_fskip(pb, atom.size - 4);
1408 return 0;
1409 }
1410 atom.type = get_le32(pb);
1411 atom.offset += 8;
1412 atom.size -= 8;
fd6e513e 1413 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
5ca1d879
ZK
1414 url_fskip(pb, atom.size);
1415 return 0;
1416 }
1417 err = mov_read_mdat(c, pb, atom);
1418#ifdef DEBUG
1419 debug_indent--;
1420#endif
1421 return err;
1422}
1423
1424
0147f198 1425#ifdef CONFIG_ZLIB
0c1a9eda 1426static int null_read_packet(void *opaque, uint8_t *buf, int buf_size)
0147f198
FR
1427{
1428 return -1;
1429}
1430
5ca1d879 1431static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
0147f198 1432{
0147f198 1433 ByteIOContext ctx;
b6a17df4
ZK
1434 uint8_t *cmov_data;
1435 uint8_t *moov_data; /* uncompressed data */
0147f198
FR
1436 long cmov_len, moov_len;
1437 int ret;
b6a17df4 1438
5ca1d879 1439 print_atom("cmov", atom);
0147f198
FR
1440
1441 get_be32(pb); /* dcom atom */
1442 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1443 return -1;
1444 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
baf25c9d 1445 av_log(NULL, AV_LOG_DEBUG, "unknown compression for cmov atom !");
0147f198
FR
1446 return -1;
1447 }
1448 get_be32(pb); /* cmvd atom */
1449 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1450 return -1;
1451 moov_len = get_be32(pb); /* uncompressed size */
5ca1d879 1452 cmov_len = atom.size - 6 * 4;
5cd62665 1453
b6a17df4 1454 cmov_data = (uint8_t *) av_malloc(cmov_len);
0147f198
FR
1455 if (!cmov_data)
1456 return -1;
b6a17df4 1457 moov_data = (uint8_t *) av_malloc(moov_len);
0147f198
FR
1458 if (!moov_data) {
1459 av_free(cmov_data);
1460 return -1;
1461 }
1462 get_buffer(pb, cmov_data, cmov_len);
b6a17df4 1463 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
0147f198
FR
1464 return -1;
1465 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, null_read_packet, NULL, NULL) != 0)
1466 return -1;
1467 ctx.buf_end = ctx.buffer + moov_len;
5ca1d879
ZK
1468 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1469 atom.offset = 0;
1470 atom.size = moov_len;
9ed83b0a
ZK
1471#ifdef DEBUG
1472 { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
1473#endif
5ca1d879 1474 ret = mov_read_default(c, &ctx, atom);
0147f198
FR
1475 av_free(moov_data);
1476 av_free(cmov_data);
9ed83b0a 1477
0147f198
FR
1478 return ret;
1479}
1480#endif
1481
baf25c9d
GC
1482/* edit list atom */
1483static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1484{
58e555d4 1485 int i, edit_count;
baf25c9d
GC
1486 print_atom("elst", atom);
1487
1488 get_byte(pb); /* version */
1489 get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
58e555d4
MN
1490 edit_count= c->streams[c->fc->nb_streams-1]->edit_count = get_be32(pb); /* entries */
1491
1492 for(i=0; i<edit_count; i++){
1493 get_be32(pb); /* Track duration */
1494 get_be32(pb); /* Media time */
1495 get_be32(pb); /* Media rate */
1496 }
baf25c9d
GC
1497#ifdef DEBUG
1498 av_log(NULL, AV_LOG_DEBUG, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, c->streams[c->fc->nb_streams-1]->edit_count);
1499#endif
1500 return 0;
1501}
1502
6cea494e
ZK
1503static const MOVParseTableEntry mov_default_parse_table[] = {
1504/* mp4 atoms */
5ca1d879
ZK
1505{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
1506{ MKTAG( 'c', 'p', 'r', 't' ), mov_read_default },
1507{ MKTAG( 'c', 'r', 'h', 'd' ), mov_read_default },
1508{ MKTAG( 'c', 't', 't', 's' ), mov_read_leaf }, /* composition time to sample */
1509{ MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default }, /* data information */
1510{ MKTAG( 'd', 'p', 'n', 'd' ), mov_read_leaf },
1511{ MKTAG( 'd', 'r', 'e', 'f' ), mov_read_leaf },
1512{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
58e555d4 1513{ MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
5ca1d879
ZK
1514{ MKTAG( 'f', 'r', 'e', 'e' ), mov_read_leaf },
1515{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
1516{ MKTAG( 'h', 'i', 'n', 't' ), mov_read_leaf },
1517{ MKTAG( 'h', 'm', 'h', 'd' ), mov_read_leaf },
1518{ MKTAG( 'i', 'o', 'd', 's' ), mov_read_leaf },
1519{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1520{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1521{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1522{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1523{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
1524{ MKTAG( 'm', 'p', '4', 'a' ), mov_read_default },
1525{ MKTAG( 'm', 'p', '4', 's' ), mov_read_default },
1526{ MKTAG( 'm', 'p', '4', 'v' ), mov_read_default },
1527{ MKTAG( 'm', 'p', 'o', 'd' ), mov_read_leaf },
1528{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
1529{ MKTAG( 'n', 'm', 'h', 'd' ), mov_read_leaf },
1530{ MKTAG( 'o', 'd', 'h', 'd' ), mov_read_default },
1531{ MKTAG( 's', 'd', 'h', 'd' ), mov_read_default },
3c13647a 1532{ MKTAG( 's', 'k', 'i', 'p' ), mov_read_leaf },
5ca1d879 1533{ MKTAG( 's', 'm', 'h', 'd' ), mov_read_leaf }, /* sound media info header */
169eb021
MM
1534{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
1535{ MKTAG( 'a', 'v', 'c', 'C' ), mov_read_avcC },
5ca1d879
ZK
1536{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1537{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
1538{ MKTAG( 's', 't', 'd', 'p' ), mov_read_default },
1539{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1540{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
1541{ MKTAG( 's', 't', 's', 'h' ), mov_read_default },
247d56f5 1542{ MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
5ca1d879
ZK
1543{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1544{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1545{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1546{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
1547{ MKTAG( 't', 'r', 'e', 'f' ), mov_read_default }, /* not really */
1548{ MKTAG( 'u', 'd', 't', 'a' ), mov_read_leaf },
1549{ MKTAG( 'u', 'r', 'l', ' ' ), mov_read_leaf },
1550{ MKTAG( 'u', 'r', 'n', ' ' ), mov_read_leaf },
1551{ MKTAG( 'u', 'u', 'i', 'd' ), mov_read_default },
1552{ MKTAG( 'v', 'm', 'h', 'd' ), mov_read_leaf }, /* video media info header */
1553{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_default },
6cea494e 1554/* extra mp4 */
5ca1d879 1555{ MKTAG( 'M', 'D', 'E', 'S' ), mov_read_leaf },
6cea494e 1556/* QT atoms */
5ca1d879
ZK
1557{ MKTAG( 'c', 'h', 'a', 'p' ), mov_read_leaf },
1558{ MKTAG( 'c', 'l', 'i', 'p' ), mov_read_default },
1559{ MKTAG( 'c', 'r', 'g', 'n' ), mov_read_leaf },
1560{ MKTAG( 'c', 't', 'a', 'b' ), mov_read_ctab },
1561{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
1562{ MKTAG( 'k', 'm', 'a', 't' ), mov_read_leaf },
1563{ MKTAG( 'm', 'a', 't', 't' ), mov_read_default },
1564{ MKTAG( 'r', 'd', 'r', 'f' ), mov_read_leaf },
1565{ MKTAG( 'r', 'm', 'd', 'a' ), mov_read_default },
1566{ MKTAG( 'r', 'm', 'd', 'r' ), mov_read_leaf },
1567{ MKTAG( 'r', 'm', 'r', 'a' ), mov_read_default },
1568{ MKTAG( 's', 'c', 'p', 't' ), mov_read_leaf },
1569{ MKTAG( 's', 's', 'r', 'c' ), mov_read_leaf },
1570{ MKTAG( 's', 'y', 'n', 'c' ), mov_read_leaf },
1571{ MKTAG( 't', 'c', 'm', 'd' ), mov_read_leaf },
1572{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
1573//{ MKTAG( 'r', 'm', 'q', 'u' ), mov_read_leaf },
0147f198 1574#ifdef CONFIG_ZLIB
5ca1d879 1575{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
0147f198 1576#else
5ca1d879 1577{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_leaf },
0147f198 1578#endif
5ca1d879 1579{ 0L, mov_read_leaf }
6cea494e
ZK
1580};
1581
1582static void mov_free_stream_context(MOVStreamContext *sc)
1583{
1584 if(sc) {
1ea4f593
FB
1585 av_free(sc->chunk_offsets);
1586 av_free(sc->sample_to_chunk);
5cd62665 1587 av_free(sc->sample_sizes);
247d56f5 1588 av_free(sc->keyframes);
0e7eed09 1589 av_free(sc->header_data);
baf25c9d 1590 av_free(sc->stts_data);
1ea4f593 1591 av_free(sc);
6cea494e
ZK
1592 }
1593}
1594
5ca1d879 1595static inline uint32_t mov_to_tag(uint8_t *buf)
0e7eed09 1596{
5ca1d879 1597 return MKTAG(buf[0], buf[1], buf[2], buf[3]);
0e7eed09
FB
1598}
1599
5cd62665 1600static inline uint32_t to_be32(uint8_t *buf)
0e7eed09
FB
1601{
1602 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1603}
1604
b6a17df4 1605/* XXX: is it sufficient ? */
c9a65ca8
FB
1606static int mov_probe(AVProbeData *p)
1607{
0e7eed09
FB
1608 unsigned int offset;
1609 uint32_t tag;
3ffe3793 1610
c9a65ca8
FB
1611 /* check file header */
1612 if (p->buf_size <= 12)
1613 return 0;
0e7eed09
FB
1614 offset = 0;
1615 for(;;) {
1616 /* ignore invalid offset */
1617 if ((offset + 8) > (unsigned int)p->buf_size)
3ffe3793 1618 return 0;
5ca1d879 1619 tag = mov_to_tag(p->buf + offset + 4);
0e7eed09
FB
1620 switch(tag) {
1621 case MKTAG( 'm', 'o', 'o', 'v' ):
1622 case MKTAG( 'w', 'i', 'd', 'e' ):
1623 case MKTAG( 'f', 'r', 'e', 'e' ):
8b879f18
FR
1624 case MKTAG( 'm', 'd', 'a', 't' ):
1625 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
bc634f6f 1626 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
3ffe3793 1627 return AVPROBE_SCORE_MAX;
0e7eed09 1628 case MKTAG( 'f', 't', 'y', 'p' ):
5ca1d879 1629 case MKTAG( 's', 'k', 'i', 'p' ):
14342fd5 1630 offset = to_be32(p->buf+offset) + offset;
0e7eed09
FB
1631 break;
1632 default:
1633 /* unrecognized tag */
1634 return 0;
1635 }
3ffe3793
FR
1636 }
1637 return 0;
c9a65ca8
FB
1638}
1639
a266644f 1640static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
6cea494e 1641{
b6a17df4 1642 MOVContext *mov = (MOVContext *) s->priv_data;
6cea494e 1643 ByteIOContext *pb = &s->pb;
684f44d9 1644 int i, j, nb, err;
5ca1d879 1645 MOV_atom_t atom = { 0, 0, 0 };
6cea494e 1646
6cea494e 1647 mov->fc = s;
b6a17df4 1648 mov->parse_table = mov_default_parse_table;
c9a65ca8
FB
1649#if 0
1650 /* XXX: I think we should auto detect */
1651 if(s->iformat->name[1] == 'p')
6cea494e 1652 mov->mp4 = 1;
c9a65ca8 1653#endif
6cea494e 1654 if(!url_is_streamed(pb)) /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
5ca1d879 1655 atom.size = url_filesize(url_fileno(pb));
6cea494e 1656 else
9ed83b0a 1657 atom.size = 0x7FFFFFFFFFFFFFFFLL;
6cea494e
ZK
1658
1659#ifdef DEBUG
baf25c9d 1660 av_log(NULL, AV_LOG_DEBUG, "filesz=%Ld\n", atom.size);
6cea494e
ZK
1661#endif
1662
1663 /* check MOV header */
5ca1d879 1664 err = mov_read_default(mov, pb, atom);
25fa62e1 1665 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
bc874dae 1666 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%lld\n",
25fa62e1
ZK
1667 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
1668 return -1;
6cea494e
ZK
1669 }
1670#ifdef DEBUG
baf25c9d 1671 av_log(NULL, AV_LOG_DEBUG, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
6cea494e
ZK
1672#endif
1673 /* some cleanup : make sure we are on the mdat atom */
1674 if(!url_is_streamed(pb) && (url_ftell(pb) != mov->mdat_offset))
1675 url_fseek(pb, mov->mdat_offset, SEEK_SET);
1676
1677 mov->next_chunk_offset = mov->mdat_offset; /* initialise reading */
1678
1679#ifdef DEBUG
baf25c9d 1680 av_log(NULL, AV_LOG_DEBUG, "mdat_reset_offset=%d\n", (int) url_ftell(pb));
6cea494e
ZK
1681#endif
1682
1683#ifdef DEBUG
baf25c9d 1684 av_log(NULL, AV_LOG_DEBUG, "streams= %d\n", s->nb_streams);
6cea494e
ZK
1685#endif
1686 mov->total_streams = nb = s->nb_streams;
5ca1d879 1687
6cea494e
ZK
1688#if 1
1689 for(i=0; i<s->nb_streams;) {
1690 if(s->streams[i]->codec.codec_type == CODEC_TYPE_MOV_OTHER) {/* not audio, not video, delete */
1ea4f593 1691 av_free(s->streams[i]);
6cea494e
ZK
1692 for(j=i+1; j<s->nb_streams; j++)
1693 s->streams[j-1] = s->streams[j];
1694 s->nb_streams--;
1695 } else
1696 i++;
1697 }
1698 for(i=0; i<s->nb_streams;i++) {
1699 MOVStreamContext *sc;
1700 sc = (MOVStreamContext *)s->streams[i]->priv_data;
1701 sc->ffindex = i;
1702 sc->is_ff_stream = 1;
1703 }
1704#endif
1705#ifdef DEBUG
baf25c9d 1706 av_log(NULL, AV_LOG_DEBUG, "real streams= %d\n", s->nb_streams);
6cea494e
ZK
1707#endif
1708 return 0;
1709}
1710
1711/* Yes, this is ugly... I didn't write the specs of QT :p */
1712/* XXX:remove useless commented code sometime */
a266644f 1713static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
6cea494e 1714{
b6a17df4 1715 MOVContext *mov = (MOVContext *) s->priv_data;
0e7eed09 1716 MOVStreamContext *sc;
9ed83b0a 1717 int64_t offset = 0x0FFFFFFFFFFFFFFFLL;
247d56f5 1718 int i, a, b, m;
5cd62665 1719 int size;
7619ed2b 1720 int idx;
6cea494e 1721 size = 0x0FFFFFFF;
5cd62665 1722
3ffe3793
FR
1723#ifdef MOV_SPLIT_CHUNKS
1724 if (mov->partial) {
5cd62665
ZK
1725 sc = mov->partial;
1726 idx = sc->sample_to_chunk_index;
1727
3ffe3793 1728 if (idx < 0) return 0;
7dbab4a9
FR
1729#ifdef DEBUG
1730 fprintf(stderr, "sc[ffid %d]->sample_size = %d\n", sc->ffindex, sc->sample_size);
1731#endif
1732 //size = sc->sample_sizes[sc->current_sample];
1733 // that ain't working...
1734 //size = (sc->sample_size)?sc->sample_size:sc->sample_sizes[sc->current_sample];
1735 size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
3ffe3793 1736
5cd62665
ZK
1737 sc->current_sample++;
1738 sc->left_in_chunk--;
3ffe3793 1739
5cd62665 1740 if (sc->left_in_chunk <= 0)
3ffe3793
FR
1741 mov->partial = 0;
1742 offset = mov->next_chunk_offset;
1743 /* extract the sample */
1744
1745 goto readchunk;
1746 }
1747#endif
1748
6cea494e 1749again:
5cd62665 1750 sc = 0;
6cea494e 1751 for(i=0; i<mov->total_streams; i++) {
5cd62665 1752 MOVStreamContext *msc = mov->streams[i];
baf25c9d 1753 //av_log(NULL, AV_LOG_DEBUG, "MOCHUNK %ld %d %p pos:%Ld\n", mov->streams[i]->next_chunk, mov->total_streams, mov->streams[i], url_ftell(&s->pb));
5cd62665
ZK
1754 if ((msc->next_chunk < msc->chunk_count) && msc->next_chunk >= 0
1755 && (msc->chunk_offsets[msc->next_chunk] < offset)) {
1756 sc = msc;
1757 offset = msc->chunk_offsets[msc->next_chunk];
baf25c9d 1758 //av_log(NULL, AV_LOG_DEBUG, "SELETED %Ld i:%d\n", offset, i);
6cea494e 1759 }
6cea494e 1760 }
9ed83b0a 1761 if (!sc || offset==0x0FFFFFFFFFFFFFFFLL)
5cd62665
ZK
1762 return -1;
1763
1764 sc->next_chunk++;
1765
3ffe3793 1766 if(mov->next_chunk_offset < offset) { /* some meta data */
6cea494e 1767 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
3ffe3793
FR
1768 mov->next_chunk_offset = offset;
1769 }
1770
baf25c9d 1771//av_log(NULL, AV_LOG_DEBUG, "chunk: [%i] %lli -> %lli\n", st_id, mov->next_chunk_offset, offset);
b9866ebc 1772 if(!sc->is_ff_stream || s->streams[sc->ffindex]->discard) {
6cea494e 1773 url_fskip(&s->pb, (offset - mov->next_chunk_offset));
3ffe3793 1774 mov->next_chunk_offset = offset;
9ed83b0a 1775 offset = 0x0FFFFFFFFFFFFFFFLL;
6cea494e
ZK
1776 goto again;
1777 }
6cea494e
ZK
1778
1779 /* now get the chunk size... */
1780
1781 for(i=0; i<mov->total_streams; i++) {
5cd62665
ZK
1782 MOVStreamContext *msc = mov->streams[i];
1783 if ((msc->next_chunk < msc->chunk_count)
1784 && ((msc->chunk_offsets[msc->next_chunk] - offset) < size))
1785 size = msc->chunk_offsets[msc->next_chunk] - offset;
6cea494e 1786 }
bc634f6f
ZK
1787
1788#ifdef MOV_MINOLTA_FIX
1789 //Make sure that size is according to sample_size (Needed by .mov files
1790 //created on a Minolta Dimage Xi where audio chunks contains waste data in the end)
1791 //Maybe we should really not only check sc->sample_size, but also sc->sample_sizes
1792 //but I have no such movies
1793 if (sc->sample_size > 0) {
1794 int foundsize=0;
1795 for(i=0; i<(sc->sample_to_chunk_sz); i++) {
1796 if( (sc->sample_to_chunk[i].first)<=(sc->next_chunk) && (sc->sample_size>0) )
1797 {
cac0a56c
RS
1798 // I can't figure out why for PCM audio sample_size is always 1
1799 // (it should actually be channels*bits_per_second/8) but it is.
1800 AVCodecContext* cod = &s->streams[sc->ffindex]->codec;
1801 if (sc->sample_size == 1 && (cod->codec_id == CODEC_ID_PCM_S16BE || cod->codec_id == CODEC_ID_PCM_S16LE))
1802 foundsize=(sc->sample_to_chunk[i].count*cod->channels*cod->bits_per_sample)/8;
1803 else
1804 foundsize=sc->sample_to_chunk[i].count*sc->sample_size;
bc634f6f
ZK
1805 }
1806#ifdef DEBUG
c3ea71a0 1807 av_log(NULL, AV_LOG_DEBUG, "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);
bc634f6f
ZK
1808#endif
1809 }
1810 if( (foundsize>0) && (foundsize<size) )
1811 {
1812#ifdef DEBUG
baf25c9d 1813 /*av_log(NULL, AV_LOG_DEBUG, "this size should actually be %d\n",foundsize);*/
bc634f6f
ZK
1814#endif
1815 size=foundsize;
1816 }
1817 }
1818#endif //MOV_MINOLTA_FIX
1819
3ffe3793
FR
1820#ifdef MOV_SPLIT_CHUNKS
1821 /* split chunks into samples */
7dbab4a9
FR
1822 if (sc->sample_size == 0) {
1823 idx = sc->sample_to_chunk_index;
1824 if ((idx + 1 < sc->sample_to_chunk_sz)
1825 && (sc->next_chunk >= sc->sample_to_chunk[idx + 1].first))
1826 idx++;
1827 sc->sample_to_chunk_index = idx;
1828 if (idx >= 0 && sc->sample_to_chunk[idx].count != 1) {
1829 mov->partial = sc;
1830 /* we'll have to get those samples before next chunk */
1831 sc->left_in_chunk = sc->sample_to_chunk[idx].count - 1;
1832 size = (sc->sample_size > 1)?sc->sample_size:sc->sample_sizes[sc->current_sample];
1833 }
7619ed2b 1834
7dbab4a9
FR
1835 sc->current_sample++;
1836 }
3ffe3793
FR
1837#endif
1838
1839readchunk:
c3ea71a0
FR
1840#ifdef DEBUG
1841 av_log(NULL, AV_LOG_DEBUG, "chunk: %lli -> %lli (%i)\n", offset, offset + size, size);
1842#endif
6cea494e
ZK
1843 if(size == 0x0FFFFFFF)
1844 size = mov->mdat_size + mov->mdat_offset - offset;
1845 if(size < 0)
1846 return -1;
1847 if(size == 0)
1848 return -1;
0e7eed09 1849 url_fseek(&s->pb, offset, SEEK_SET);
5cd62665 1850
baf25c9d 1851 //av_log(NULL, AV_LOG_DEBUG, "READCHUNK hlen: %d %d off: %Ld pos:%Ld\n", size, sc->header_len, offset, url_ftell(&s->pb));
0e7eed09
FB
1852 if (sc->header_len > 0) {
1853 av_new_packet(pkt, size + sc->header_len);
1854 memcpy(pkt->data, sc->header_data, sc->header_len);
1855 get_buffer(&s->pb, pkt->data + sc->header_len, size);
1856 /* free header */
1857 av_freep(&sc->header_data);
1858 sc->header_len = 0;
1859 } else {
1860 av_new_packet(pkt, size);
1861 get_buffer(&s->pb, pkt->data, pkt->size);
1862 }
1863 pkt->stream_index = sc->ffindex;
247d56f5
BB
1864
1865 // If the keyframes table exists, mark any samples that are in the table as key frames.
1866 // If no table exists, treat very sample as a key frame.
1867 if (sc->keyframes) {
1868 a = 0;
1869 b = sc->keyframe_count - 1;
1870
1871 while (a < b) {
1872 m = (a + b + 1) >> 1;
1873 if (sc->keyframes[m] > sc->current_sample) {
1874 b = m - 1;
1875 } else {
1876 a = m;
1877 }
1878 }
1879
1880 if (sc->keyframes[a] == sc->current_sample)
1881 pkt->flags |= PKT_FLAG_KEY;
1882 }
1883 else
1884 pkt->flags |= PKT_FLAG_KEY;
6cea494e
ZK
1885
1886#ifdef DEBUG
7dbab4a9 1887/*
c3ea71a0 1888 av_log(NULL, AV_LOG_DEBUG, "Packet (%d, %ld) ", pkt->stream_index, pkt->size);
6cea494e 1889 for(i=0; i<8; i++)
baf25c9d 1890 av_log(NULL, AV_LOG_DEBUG, "%02x ", pkt->data[i]);
6cea494e 1891 for(i=0; i<8; i++)
baf25c9d 1892 av_log(NULL, AV_LOG_DEBUG, "%c ", (pkt->data[i]) & 0x7F);
c3ea71a0 1893 av_log(NULL, AV_LOG_DEBUG, "\n");
7dbab4a9 1894*/
6cea494e
ZK
1895#endif
1896
1897 mov->next_chunk_offset = offset + size;
1898
1899 return 0;
1900}
1901
baf25c9d
GC
1902#if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
1903/**
1904 * Seek method based on the one described in the Appendix C of QTFileFormat.pdf
1905 */
7b3c1382 1906static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
baf25c9d
GC
1907{
1908 MOVContext* mov = (MOVContext *) s->priv_data;
1909 MOVStreamContext* sc;
1910 int32_t i, a, b, m;
1911 int64_t sample_time;
1912 int64_t start_time;
1913 int32_t seek_sample, sample;
1914 int32_t duration;
1915 int32_t count;
1916 int32_t chunk;
1917 int32_t left_in_chunk;
1918 int64_t chunk_file_offset;
1919 int64_t sample_file_offset;
1920 int32_t first_chunk_sample;
1921 int32_t sample_to_chunk_idx;
1922 int mov_idx;
1923
1924 // Find the corresponding mov stream
1925 for (mov_idx = 0; mov_idx < mov->total_streams; mov_idx++)
1926 if (mov->streams[mov_idx]->ffindex == stream_index)
1927 break;
1928 if (mov_idx == mov->total_streams) {
1929 av_log(s, AV_LOG_ERROR, "mov: requested stream was not found in mov streams (idx=%i)\n", stream_index);
1930 return -1;
1931 }
1932 sc = mov->streams[mov_idx];
1933
1934 // Step 1. Find the edit that contains the requested time (elst)
1935 if (sc->edit_count) {
1936 // FIXME should handle edit list
1937 av_log(s, AV_LOG_ERROR, "mov: does not handle seeking in files that contain edit list (c:%d)\n", sc->edit_count);
1938 return -1;
1939 }
1940
1941 // Step 2. Find the corresponding sample using the Time-to-sample atom (stts) */
1942#ifdef DEBUG
1943 av_log(s, AV_LOG_DEBUG, "Searching for time %li in stream #%i (time_scale=%i)\n", (long)timestamp, mov_idx, sc->time_scale);
1944#endif
1945 // convert timestamp from time_base unit to timescale unit
1946 sample_time = av_rescale( timestamp,
1947 (int64_t)sc->time_scale * s->streams[stream_index]->time_base.num,
1948 (int64_t)s->streams[stream_index]->time_base.den);
1949 start_time = 0; // FIXME use elst atom
1950 sample = 1; // sample are 0 based in table
1951#ifdef DEBUG
1952 av_log(s, AV_LOG_DEBUG, "Searching for sample_time %li \n", (long)sample_time);
1953#endif
1954 for (i = 0; i < sc->stts_count; i++) {
1955 count = (uint32_t)(sc->stts_data[i]>>32);
1956 duration = (uint32_t)(sc->stts_data[i]&0xffff);
1957//av_log(s, AV_LOG_DEBUG, "> sample_time %lli \n", (long)sample_time);
1958//av_log(s, AV_LOG_DEBUG, "> count=%i duration=%i\n", count, duration);
1959 if ((start_time + count*duration) > sample_time) {
1960 sample += (sample_time - start_time) / duration;
1961 break;
1962 }
1963 sample += count;
1964 start_time += count * duration;
1965 }
1966 /* NOTE: despite what qt doc say, the dt value (Display Time in qt vocabulary) computed with the stts atom
1967 is a decoding time stamp (dts) not a presentation time stamp. And as usual dts != pts for stream with b frames */
1968
1969#ifdef DEBUG
1970 av_log(s, AV_LOG_DEBUG, "Found time %li at sample #%u\n", (long)sample_time, sample);
1971#endif
1972 if (sample > sc->sample_count) {
1973 av_log(s, AV_LOG_ERROR, "mov: sample pos is too high, unable to seek (req. sample=%i, sample count=%ld)\n", sample, sc->sample_count);
1974 return -1;
1975 }
1976
1977 // Step 3. Find the prior sync. sample using the Sync sample atom (stss)
1978 if (sc->keyframes) {
1979 a = 0;
1980 b = sc->keyframe_count - 1;
1981 while (a < b) {
1982 m = (a + b + 1) >> 1;
1983 if (sc->keyframes[m] > sample) {
1984 b = m - 1;
1985 } else {
1986 a = m;
1987 }
1988#ifdef DEBUG
1989 // av_log(s, AV_LOG_DEBUG, "a=%i (%i) b=%i (%i) m=%i (%i) stream #%i\n", a, sc->keyframes[a], b, sc->keyframes[b], m, sc->keyframes[m], mov_idx);
1990#endif
1991 }
1992 seek_sample = sc->keyframes[a];
1993 }
1994 else
1995 seek_sample = sample; // else all samples are key frames
1996#ifdef DEBUG
1997 av_log(s, AV_LOG_DEBUG, "Found nearest keyframe at sample #%i \n", seek_sample);
1998#endif
1999
2000 // Step 4. Find the chunk of the sample using the Sample-to-chunk-atom (stsc)
2001 for (first_chunk_sample = 1, i = 0; i < (sc->sample_to_chunk_sz - 1); i++) {
2002 b = (sc->sample_to_chunk[i + 1].first - sc->sample_to_chunk[i].first) * sc->sample_to_chunk[i].count;
2003 if (seek_sample >= first_chunk_sample && seek_sample < (first_chunk_sample + b))
2004 break;
2005 first_chunk_sample += b;
2006 }
2007 chunk = sc->sample_to_chunk[i].first + (seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count;
2008 left_in_chunk = sc->sample_to_chunk[i].count - (seek_sample - first_chunk_sample) % sc->sample_to_chunk[i].count;
2009 first_chunk_sample += ((seek_sample - first_chunk_sample) / sc->sample_to_chunk[i].count) * sc->sample_to_chunk[i].count;
2010 sample_to_chunk_idx = i;
2011#ifdef DEBUG
2012 av_log(s, AV_LOG_DEBUG, "Sample was found in chunk #%i at sample offset %i (idx %i)\n", chunk, seek_sample - first_chunk_sample, sample_to_chunk_idx);
2013#endif
2014
2015 // Step 5. Find the offset of the chunk using the chunk offset atom
2016 if (!sc->chunk_offsets) {
2017 av_log(s, AV_LOG_ERROR, "mov: no chunk offset atom, unable to seek\n");
2018 return -1;
2019 }
2020 if (chunk > sc->chunk_count) {
2021 av_log(s, AV_LOG_ERROR, "mov: chunk offset atom too short, unable to seek (req. chunk=%i, chunk count=%li)\n", chunk, sc->chunk_count);
2022 return -1;
2023 }
2024 chunk_file_offset = sc->chunk_offsets[chunk - 1];
2025#ifdef DEBUG
2026 av_log(s, AV_LOG_DEBUG, "Chunk file offset is #%llu \n", chunk_file_offset);
2027#endif
2028
2029 // Step 6. Find the byte offset within the chunk using the sample size atom
2030 sample_file_offset = chunk_file_offset;
2031 if (sc->sample_size)
2032 sample_file_offset += (seek_sample - first_chunk_sample) * sc->sample_size;
2033 else {
2034 for (i = 0; i < (seek_sample - first_chunk_sample); i++) {
2035 sample_file_offset += sc->sample_sizes[first_chunk_sample + i - 1];
2036 }
2037 }
2038#ifdef DEBUG
2039 av_log(s, AV_LOG_DEBUG, "Sample file offset is #%llu \n", sample_file_offset);
2040#endif
2041
2042 // Step 6. Update the parser
2043 mov->partial = sc;
2044 mov->next_chunk_offset = sample_file_offset;
2045 // Update current stream state
2046 sc->current_sample = seek_sample - 1; // zero based
2047 sc->left_in_chunk = left_in_chunk;
2048 sc->next_chunk = chunk; // +1 -1 (zero based)
2049 sc->sample_to_chunk_index = sample_to_chunk_idx;
2050
2051 // Update other streams
2052 for (i = 0; i<mov->total_streams; i++) {
5c030d3e 2053 MOVStreamContext *msc;
baf25c9d
GC
2054 if (i == mov_idx) continue;
2055 // Find the nearest 'next' chunk
5c030d3e 2056 msc = mov->streams[i];
baf25c9d
GC
2057 a = 0;
2058 b = msc->chunk_count - 1;
2059 while (a < b) {
2060 m = (a + b + 1) >> 1;
2061 if (msc->chunk_offsets[m] > chunk_file_offset) {
2062 b = m - 1;
2063 } else {
2064 a = m;
2065 }
2066#ifdef DEBUG
2067/* av_log(s, AV_LOG_DEBUG, "a=%i (%li) b=%i (%li) m=%i (%li) stream #%i\n"
2068 , a, (long)msc->chunk_offsets[a], b, (long)msc->chunk_offsets[b], m, (long)msc->chunk_offsets[m], i); */
2069#endif
2070 }
2071 msc->next_chunk = a;
2072 if (msc->chunk_offsets[a] < chunk_file_offset && a < (msc->chunk_count-1))
2073 msc->next_chunk ++;
2074#ifdef DEBUG
2075 av_log(s, AV_LOG_DEBUG, "Nearest next chunk for stream #%i is #%i @%lli\n", i, msc->next_chunk+1, msc->chunk_offsets[msc->next_chunk]);
2076#endif
2077 // Compute sample count and index in the sample_to_chunk table (what a pity)
2078 msc->sample_to_chunk_index = 0;
2079 msc->current_sample = 0;
2080 for(; msc->sample_to_chunk_index < (msc->sample_to_chunk_sz - 1)
2081 && msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first <= (1 + msc->next_chunk); msc->sample_to_chunk_index++) {
2082 msc->current_sample += (msc->sample_to_chunk[msc->sample_to_chunk_index + 1].first - msc->sample_to_chunk[msc->sample_to_chunk_index].first) \
2083 * msc->sample_to_chunk[msc->sample_to_chunk_index].count;
2084 }
2085 msc->current_sample += (msc->next_chunk - (msc->sample_to_chunk[msc->sample_to_chunk_index].first - 1)) * sc->sample_to_chunk[msc->sample_to_chunk_index].count;
2086 msc->left_in_chunk = msc->sample_to_chunk[msc->sample_to_chunk_index].count - 1;
2087#ifdef DEBUG
2088 av_log(s, AV_LOG_DEBUG, "Next Sample for stream #%i is #%i @%i\n", i, msc->current_sample + 1, msc->sample_to_chunk_index + 1);
2089#endif
2090 }
2091 return 0;
2092}
2093#endif
2094
a266644f 2095static int mov_read_close(AVFormatContext *s)
6cea494e
ZK
2096{
2097 int i;
b6a17df4 2098 MOVContext *mov = (MOVContext *) s->priv_data;
6cea494e
ZK
2099 for(i=0; i<mov->total_streams; i++)
2100 mov_free_stream_context(mov->streams[i]);
5ca1d879
ZK
2101 /* free color tabs */
2102 for(i=0; i<mov->ctab_size; i++)
2103 av_freep(&mov->ctab[i]);
2104 av_freep(&mov->ctab);
6cea494e
ZK
2105 return 0;
2106}
2107
c9a65ca8 2108static AVInputFormat mov_iformat = {
4cb3f3b6 2109 "mov,mp4,m4a,3gp",
c9a65ca8
FB
2110 "QuickTime/MPEG4 format",
2111 sizeof(MOVContext),
2112 mov_probe,
6cea494e
ZK
2113 mov_read_header,
2114 mov_read_packet,
2115 mov_read_close,
baf25c9d
GC
2116#if defined(MOV_SPLIT_CHUNKS) && defined(MOV_SEEK)
2117 mov_read_seek,
2118#endif
6cea494e
ZK
2119};
2120
c9a65ca8
FB
2121int mov_init(void)
2122{
2123 av_register_input_format(&mov_iformat);
2124 return 0;
2125}