Add missed call to ff_cavsdsp_init_3dnow() in dsputil_init_mmx()
[libav.git] / libavformat / mov.c
CommitLineData
6cea494e 1/*
7fbde343 2 * MOV demuxer
19720f15 3 * Copyright (c) 2001 Fabrice Bellard.
6cea494e 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
6cea494e 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
6cea494e 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
6cea494e 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6cea494e 20 */
d957696f
MN
21
22#include <limits.h>
115329f1 23
bfc2a19d 24//#define DEBUG
4e5ef14f 25
6cea494e 26#include "avformat.h"
9d9f4119 27#include "riff.h"
e40ee6a2 28#include "isom.h"
b60c0454 29#include "dv.h"
6cea494e 30
0147f198
FR
31#ifdef CONFIG_ZLIB
32#include <zlib.h>
33#endif
34
6cea494e
ZK
35/*
36 * First version by Francois Revol revol@free.fr
115329f1 37 * Seek function by Gael Chardon gael.dev@4now.net
5ca1d879 38 *
6cea494e 39 * Features and limitations:
5ca1d879 40 * - reads most of the QT files I have (at least the structure),
6cea494e 41 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
6cea494e 42 * - the code is quite ugly... maybe I won't do it recursive next time :-)
5ca1d879 43 *
6cea494e
ZK
44 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
45 * when coding this :) (it's a writer anyway)
5ca1d879 46 *
6cea494e
ZK
47 * Reference documents:
48 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
49 * Apple:
baf25c9d 50 * http://developer.apple.com/documentation/QuickTime/QTFF/
15c8dbe7 51 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
6cea494e
ZK
52 * QuickTime is a trademark of Apple (AFAIK :))
53 */
54
b595afaa
MM
55#include "qtpalette.h"
56
baf25c9d 57
cd7352d5
MN
58#undef NDEBUG
59#include <assert.h>
60
6cea494e
ZK
61/* the QuickTime file format is quite convoluted...
62 * it has lots of index tables, each indexing something in another one...
63 * Here we just use what is needed to read the chunks
64 */
65
e23848a4 66typedef struct {
1c02d96f
BC
67 int first;
68 int count;
69 int id;
e23848a4 70} MOV_stsc_t;
6cea494e 71
5cd62665 72typedef struct {
5ca1d879 73 uint32_t type;
221e21b7
BC
74 char *path;
75} MOV_dref_t;
76
77typedef struct {
78 uint32_t type;
5ca1d879
ZK
79 int64_t offset;
80 int64_t size; /* total size (excluding the size and type fields) */
81} MOV_atom_t;
82
b6a17df4
ZK
83struct MOVParseTableEntry;
84
6cea494e 85typedef struct MOVStreamContext {
221e21b7 86 ByteIOContext *pb;
6cea494e 87 int ffindex; /* the ffmpeg stream id */
1c02d96f 88 int next_chunk;
44d3fea5 89 unsigned int chunk_count;
0c1a9eda 90 int64_t *chunk_offsets;
44d3fea5 91 unsigned int stts_count;
e23848a4 92 MOV_stts_t *stts_data;
44d3fea5 93 unsigned int ctts_count;
e23848a4 94 MOV_stts_t *ctts_data;
44d3fea5
BC
95 unsigned int edit_count; /* number of 'edit' (elst atom) */
96 unsigned int sample_to_chunk_sz;
e23848a4 97 MOV_stsc_t *sample_to_chunk;
c4ac052b
MN
98 int sample_to_ctime_index;
99 int sample_to_ctime_sample;
44d3fea5
BC
100 unsigned int sample_size;
101 unsigned int sample_count;
1c02d96f 102 int *sample_sizes;
44d3fea5 103 unsigned int keyframe_count;
1c02d96f 104 int *keyframes;
5cd62665 105 int time_scale;
cd7352d5 106 int time_rate;
1c02d96f 107 int current_sample;
e14f79ed
BC
108 unsigned int bytes_per_frame;
109 unsigned int samples_per_frame;
b60c0454 110 int dv_audio_container;
ee6e2dbe 111 int pseudo_stream_id;
77c75437 112 int16_t audio_cid; ///< stsd audio compression id
221e21b7
BC
113 unsigned drefs_count;
114 MOV_dref_t *drefs;
115 int dref_id;
6cea494e
ZK
116} MOVStreamContext;
117
118typedef struct MOVContext {
6cea494e 119 AVFormatContext *fc;
5cd62665 120 int time_scale;
7e815047 121 int64_t duration; /* duration of the longest track */
6cea494e
ZK
122 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
123 int found_mdat; /* we suppose we have enough data to read the file */
b595afaa 124 AVPaletteControl palette_control;
b60c0454
BC
125 DVDemuxContext *dv_demux;
126 AVFormatContext *dv_fctx;
152e9a43 127 int isom; /* 1 if file is ISO Media (mp4/3gp) */
6cea494e
ZK
128} MOVContext;
129
130
6cea494e
ZK
131/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
132
133/* those functions parse an atom */
134/* return code:
e3c4740f
BC
135 1: found what I wanted, exit
136 0: continue to parse next atom
9c55b772 137 <0: error occured, exit
e3c4740f 138*/
6cea494e
ZK
139/* links atom IDs to parse functions */
140typedef struct MOVParseTableEntry {
0c1a9eda 141 uint32_t type;
03dc32f6 142 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
6cea494e
ZK
143} MOVParseTableEntry;
144
73d07c27
BC
145static const MOVParseTableEntry mov_default_parse_table[];
146
5ca1d879 147static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 148{
b6a17df4 149 int64_t total_size = 0;
5ca1d879 150 MOV_atom_t a;
6cea494e
ZK
151 int i;
152 int err = 0;
5ca1d879 153
5ca1d879 154 a.offset = atom.offset;
6cea494e 155
9ed83b0a 156 if (atom.size < 0)
fa22ca22 157 atom.size = INT64_MAX;
5ca1d879 158 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
bb270c08 159 a.size = atom.size;
bcfe2ba0 160 a.type=0;
5ca1d879 161 if(atom.size >= 8) {
bb270c08 162 a.size = get_be32(pb);
5ca1d879 163 a.type = get_le32(pb);
6cea494e 164 }
bb270c08 165 total_size += 8;
5ca1d879 166 a.offset += 8;
29c90869
BC
167 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
168 a.type, (char*)&a.type, a.size, atom.size, total_size);
5ca1d879 169 if (a.size == 1) { /* 64 bit extended size */
bb270c08 170 a.size = get_be64(pb) - 8;
5ca1d879
ZK
171 a.offset += 8;
172 total_size += 8;
6cea494e 173 }
bb270c08
DB
174 if (a.size == 0) {
175 a.size = atom.size - total_size;
176 if (a.size <= 8)
5cd62665 177 break;
bb270c08 178 }
bb270c08 179 a.size -= 8;
8622613d 180 if(a.size < 0)
568e18b1 181 break;
3d2308b0 182 a.size = FFMIN(a.size, atom.size - total_size);
115329f1 183
bcfe2ba0 184 for (i = 0; mov_default_parse_table[i].type != 0
73d07c27 185 && mov_default_parse_table[i].type != a.type; i++)
11979c46
BC
186 /* empty */;
187
73d07c27 188 if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
5ca1d879 189 url_fskip(pb, a.size);
bb270c08 190 } else {
5c72cad8
BC
191 offset_t start_pos = url_ftell(pb);
192 int64_t left;
03dc32f6 193 err = mov_default_parse_table[i].parse(c, pb, a);
db3ee6cc 194 if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
687f35f3 195 break;
5c72cad8
BC
196 left = a.size - url_ftell(pb) + start_pos;
197 if (left > 0) /* skip garbage at atom end */
198 url_fskip(pb, left);
bb270c08 199 }
6cea494e 200
bb270c08 201 a.offset += a.size;
5ca1d879 202 total_size += a.size;
6cea494e
ZK
203 }
204
5ca1d879 205 if (!err && total_size < atom.size && atom.size < 0x7ffff) {
5ca1d879 206 url_fskip(pb, atom.size - total_size);
5cd62665
ZK
207 }
208
6cea494e
ZK
209 return err;
210}
211
221e21b7
BC
212static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
213{
214 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
215 MOVStreamContext *sc = st->priv_data;
216 int entries, i, j;
217
218 get_be32(pb); // version + flags
219 entries = get_be32(pb);
220 if (entries >= UINT_MAX / sizeof(*sc->drefs))
221 return -1;
222 sc->drefs_count = entries;
223 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
224
225 for (i = 0; i < sc->drefs_count; i++) {
226 MOV_dref_t *dref = &sc->drefs[i];
227 uint32_t size = get_be32(pb);
228 offset_t next = url_ftell(pb) + size - 4;
229
230 dref->type = get_le32(pb);
231 get_be32(pb); // version + flags
232 dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
233
234 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
235 /* macintosh alias record */
236 uint16_t volume_len, len;
237 char volume[28];
238 int16_t type;
239
240 url_fskip(pb, 10);
241
242 volume_len = get_byte(pb);
243 volume_len = FFMIN(volume_len, 27);
244 get_buffer(pb, volume, 27);
245 volume[volume_len] = 0;
246 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
247
248 url_fskip(pb, 112);
249
250 for (type = 0; type != -1 && url_ftell(pb) < next; ) {
251 type = get_be16(pb);
252 len = get_be16(pb);
253 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
254 if (len&1)
255 len += 1;
256 if (type == 2) { // absolute path
dbb37657 257 av_free(dref->path);
221e21b7 258 dref->path = av_mallocz(len+1);
2f4568e5
BC
259 if (!dref->path)
260 return AVERROR(ENOMEM);
221e21b7 261 get_buffer(pb, dref->path, len);
dbb7cbf2 262 if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
221e21b7
BC
263 len -= volume_len;
264 memmove(dref->path, dref->path+volume_len, len);
265 dref->path[len] = 0;
266 }
267 for (j = 0; j < len; j++)
268 if (dref->path[j] == ':')
269 dref->path[j] = '/';
270 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
271 } else
272 url_fskip(pb, len);
273 }
274 }
275 url_fseek(pb, next, SEEK_SET);
276 }
277 return 0;
278}
279
5ca1d879 280static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 281{
5ca1d879 282 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
0c1a9eda 283 uint32_t type;
0c1a9eda 284 uint32_t ctype;
b6a17df4 285
6cea494e 286 get_byte(pb); /* version */
23f08617 287 get_be24(pb); /* flags */
6cea494e
ZK
288
289 /* component type */
290 ctype = get_le32(pb);
291 type = get_le32(pb); /* component subtype */
292
29c90869
BC
293 dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
294 ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
295 dprintf(c->fc, "stype= %c%c%c%c\n",
296 *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
152e9a43
BC
297 if(!ctype)
298 c->isom = 1;
73920f07
BC
299 if(type == MKTAG('v', 'i', 'd', 'e'))
300 st->codec->codec_type = CODEC_TYPE_VIDEO;
301 else if(type == MKTAG('s', 'o', 'u', 'n'))
302 st->codec->codec_type = CODEC_TYPE_AUDIO;
95a07973
BC
303 else if(type == MKTAG('m', '1', 'a', ' '))
304 st->codec->codec_id = CODEC_ID_MP2;
8cb97693
BC
305 else if(type == MKTAG('s', 'u', 'b', 'p')) {
306 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
8cb97693 307 }
6cea494e
ZK
308 get_be32(pb); /* component manufacture */
309 get_be32(pb); /* component flags */
310 get_be32(pb); /* component flags mask */
311
5ca1d879 312 if(atom.size <= 24)
6cea494e 313 return 0; /* nothing left to read */
5cd62665 314
3c13647a 315 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
6cea494e
ZK
316 return 0;
317}
318
9e40addd 319static int mp4_read_descr_len(ByteIOContext *pb)
0e7eed09 320{
5cd62665 321 int len = 0;
5ca1d879
ZK
322 int count = 4;
323 while (count--) {
5cd62665 324 int c = get_byte(pb);
bb270c08
DB
325 len = (len << 7) | (c & 0x7f);
326 if (!(c & 0x80))
327 break;
0e7eed09
FB
328 }
329 return len;
330}
331
9e40addd 332static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
0e7eed09
FB
333{
334 int len;
335 *tag = get_byte(pb);
9e40addd 336 len = mp4_read_descr_len(pb);
318c5e05 337 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
0e7eed09
FB
338 return len;
339}
340
0b07ac42
BC
341#define MP4ESDescrTag 0x03
342#define MP4DecConfigDescrTag 0x04
343#define MP4DecSpecificDescrTag 0x05
344
5ca1d879 345static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
5cd62665 346{
5cd62665 347 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
5cd62665 348 int tag, len;
b6a17df4 349
5cd62665 350 get_be32(pb); /* version + flags */
9e40addd 351 len = mp4_read_descr(c, pb, &tag);
5cd62665 352 if (tag == MP4ESDescrTag) {
bb270c08
DB
353 get_be16(pb); /* ID */
354 get_byte(pb); /* priority */
5cd62665 355 } else
bb270c08 356 get_be16(pb); /* ID */
5cd62665 357
9e40addd 358 len = mp4_read_descr(c, pb, &tag);
5cd62665 359 if (tag == MP4DecConfigDescrTag) {
0b07ac42
BC
360 int object_type_id = get_byte(pb);
361 get_byte(pb); /* stream type */
362 get_be24(pb); /* buffer size db */
363 get_be32(pb); /* max bitrate */
364 get_be32(pb); /* avg bitrate */
365
366 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
367 dprintf(c->fc, "esds object type id %d\n", object_type_id);
9e40addd 368 len = mp4_read_descr(c, pb, &tag);
bb270c08 369 if (tag == MP4DecSpecificDescrTag) {
318c5e05 370 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
852859ff
BC
371 if((uint64_t)len > (1<<30))
372 return -1;
9a630c25 373 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
b014dd76
BC
374 if (!st->codec->extradata)
375 return AVERROR(ENOMEM);
17871a02
BC
376 get_buffer(pb, st->codec->extradata, len);
377 st->codec->extradata_size = len;
378 /* from mplayer */
379 if ((*st->codec->extradata >> 3) == 29) {
380 st->codec->codec_id = CODEC_ID_MP3ON4;
381 }
bb270c08 382 }
5cd62665 383 }
5cd62665
ZK
384 return 0;
385}
386
5ca1d879
ZK
387/* this atom contains actual media data */
388static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 389{
5ca1d879
ZK
390 if(atom.size == 0) /* wrong one (MP4) */
391 return 0;
392 c->found_mdat=1;
5ca1d879
ZK
393 return 0; /* now go for moov */
394}
395
4ea28253
BC
396static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
397{
398 uint32_t type = get_le32(pb);
399
152e9a43
BC
400 if (type != MKTAG('q','t',' ',' '))
401 c->isom = 1;
a512446e 402 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
4ea28253
BC
403 get_be32(pb); /* minor version */
404 url_fskip(pb, atom.size - 8);
405 return 0;
406}
407
5ca1d879
ZK
408/* this atom should contain all header atoms */
409static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
410{
9cf0419b
PI
411 if (mov_read_default(c, pb, atom) < 0)
412 return -1;
5ca1d879
ZK
413 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
414 /* so we don't parse the whole file if over a network */
415 c->found_moov=1;
5ca1d879
ZK
416 return 0; /* now go for mdat */
417}
418
419
420static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
421{
f444b977 422 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 423 MOVStreamContext *sc = st->priv_data;
f444b977 424 int version = get_byte(pb);
b9a87c4d 425 int lang;
5ca1d879 426
b9a87c4d
FR
427 if (version > 1)
428 return 1; /* unsupported */
5ca1d879 429
23f08617 430 get_be24(pb); /* flags */
f444b977
BC
431 if (version == 1) {
432 get_be64(pb);
433 get_be64(pb);
434 } else {
435 get_be32(pb); /* creation time */
436 get_be32(pb); /* modification time */
437 }
5ca1d879 438
f444b977
BC
439 sc->time_scale = get_be32(pb);
440 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
5ca1d879 441
b9a87c4d 442 lang = get_be16(pb); /* language */
f444b977 443 ff_mov_lang_to_iso639(lang, st->language);
5ca1d879
ZK
444 get_be16(pb); /* quality */
445
446 return 0;
447}
448
449static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
450{
1175561e 451 int version = get_byte(pb); /* version */
23f08617 452 get_be24(pb); /* flags */
5ca1d879 453
1175561e
BC
454 if (version == 1) {
455 get_be64(pb);
456 get_be64(pb);
457 } else {
458 get_be32(pb); /* creation time */
459 get_be32(pb); /* modification time */
460 }
5ca1d879 461 c->time_scale = get_be32(pb); /* time scale */
f8c18cd7
BC
462
463 dprintf(c->fc, "time scale = %i\n", c->time_scale);
464
1175561e 465 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
5ca1d879
ZK
466 get_be32(pb); /* preferred scale */
467
468 get_be16(pb); /* preferred volume */
469
470 url_fskip(pb, 10); /* reserved */
471
472 url_fskip(pb, 36); /* display matrix */
473
474 get_be32(pb); /* preview time */
475 get_be32(pb); /* preview duration */
476 get_be32(pb); /* poster time */
477 get_be32(pb); /* selection time */
478 get_be32(pb); /* selection duration */
479 get_be32(pb); /* current time */
480 get_be32(pb); /* next track ID */
481
482 return 0;
483}
484
9ed83b0a
ZK
485static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
486{
487 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
488
568e18b1
MN
489 if((uint64_t)atom.size > (1<<30))
490 return -1;
115329f1 491
9ed83b0a
ZK
492 // currently SVQ3 decoder expect full STSD header - so let's fake it
493 // this should be fixed and just SMI header should be passed
01f4895c 494 av_free(st->codec->extradata);
b014dd76
BC
495 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
496 if (!st->codec->extradata)
497 return AVERROR(ENOMEM);
17871a02
BC
498 st->codec->extradata_size = 0x5a + atom.size;
499 memcpy(st->codec->extradata, "SVQ3", 4); // fake
500 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
501 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
9ed83b0a
ZK
502 return 0;
503}
5ca1d879 504
de23f234
BC
505static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
506{
507 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
508 int little_endian = get_be16(pb);
509
510 if (little_endian) {
511 switch (st->codec->codec_id) {
512 case CODEC_ID_PCM_S24BE:
513 st->codec->codec_id = CODEC_ID_PCM_S24LE;
514 break;
515 case CODEC_ID_PCM_S32BE:
516 st->codec->codec_id = CODEC_ID_PCM_S32LE;
517 break;
518 default:
519 break;
520 }
521 }
522 return 0;
523}
524
014a5102
BC
525/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
526static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
de23f234
BC
527{
528 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
68bc33fa
BC
529 uint64_t size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
530 uint8_t *buf;
531 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
014a5102 532 return -1;
68bc33fa
BC
533 buf= av_realloc(st->codec->extradata, size);
534 if(!buf)
535 return -1;
536 st->codec->extradata= buf;
537 buf+= st->codec->extradata_size;
538 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
539 AV_WB32( buf , atom.size + 8);
540 AV_WL32( buf + 4, atom.type);
541 get_buffer(pb, buf + 8, atom.size);
de23f234
BC
542 return 0;
543}
544
d9b1c197
RT
545static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
546{
547 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
548
549 if((uint64_t)atom.size > (1<<30))
550 return -1;
115329f1 551
3840147e
MN
552 if (st->codec->codec_id == CODEC_ID_QDM2) {
553 // pass all frma atom to codec, needed at least for QDM2
554 av_free(st->codec->extradata);
b014dd76
BC
555 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
556 if (!st->codec->extradata)
557 return AVERROR(ENOMEM);
3840147e 558 st->codec->extradata_size = atom.size;
b014dd76 559 get_buffer(pb, st->codec->extradata, atom.size);
3840147e 560 } else if (atom.size > 8) { /* to read frma, esds atoms */
9cf0419b
PI
561 if (mov_read_default(c, pb, atom) < 0)
562 return -1;
5c72cad8 563 } else
bb270c08 564 url_fskip(pb, atom.size);
d9b1c197
RT
565 return 0;
566}
567
bde24601
BC
568/**
569 * This function reads atom content and puts data in extradata without tag
570 * nor size unlike mov_read_extradata.
571 */
572static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
169eb021
MM
573{
574 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
575
568e18b1
MN
576 if((uint64_t)atom.size > (1<<30))
577 return -1;
578
01f4895c 579 av_free(st->codec->extradata);
b014dd76
BC
580 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
581 if (!st->codec->extradata)
582 return AVERROR(ENOMEM);
01f4895c 583 st->codec->extradata_size = atom.size;
b014dd76 584 get_buffer(pb, st->codec->extradata, atom.size);
169eb021
MM
585 return 0;
586}
587
5ca1d879
ZK
588static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
589{
590 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 591 MOVStreamContext *sc = st->priv_data;
568e18b1 592 unsigned int i, entries;
5ca1d879 593
5ca1d879 594 get_byte(pb); /* version */
23f08617 595 get_be24(pb); /* flags */
5ca1d879
ZK
596
597 entries = get_be32(pb);
115329f1 598
568e18b1
MN
599 if(entries >= UINT_MAX/sizeof(int64_t))
600 return -1;
115329f1 601
5ca1d879 602 sc->chunk_count = entries;
9a630c25 603 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
5ca1d879
ZK
604 if (!sc->chunk_offsets)
605 return -1;
606 if (atom.type == MKTAG('s', 't', 'c', 'o')) {
607 for(i=0; i<entries; i++) {
608 sc->chunk_offsets[i] = get_be32(pb);
609 }
610 } else if (atom.type == MKTAG('c', 'o', '6', '4')) {
611 for(i=0; i<entries; i++) {
612 sc->chunk_offsets[i] = get_be64(pb);
613 }
614 } else
615 return -1;
115329f1 616
5ca1d879
ZK
617 return 0;
618}
619
620static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
621{
622 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 623 MOVStreamContext *sc = st->priv_data;
5ca1d879
ZK
624 int entries, frames_per_sample;
625 uint32_t format;
53ffdd14 626 uint8_t codec_name[32];
5ca1d879 627
b595afaa 628 /* for palette traversal */
8b35bd80
MM
629 unsigned int color_depth;
630 unsigned int color_start;
631 unsigned int color_count;
632 unsigned int color_end;
b595afaa
MM
633 int color_index;
634 int color_dec;
635 int color_greyscale;
1fe47470 636 const uint8_t *color_table;
ee6e2dbe 637 int j, pseudo_stream_id;
b595afaa
MM
638 unsigned char r, g, b;
639
6cea494e 640 get_byte(pb); /* version */
23f08617 641 get_be24(pb); /* flags */
6cea494e 642
6cea494e
ZK
643 entries = get_be32(pb);
644
117a9190
BC
645 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
646 //Parsing Sample description table
b6a17df4 647 enum CodecID id;
221e21b7 648 int dref_id;
de23f234 649 MOV_atom_t a = { 0, 0, 0 };
3840147e 650 offset_t start_pos = url_ftell(pb);
bb270c08 651 int size = get_be32(pb); /* size */
6cea494e 652 format = get_le32(pb); /* data format */
5cd62665 653
6cea494e
ZK
654 get_be32(pb); /* reserved */
655 get_be16(pb); /* reserved */
221e21b7 656 dref_id = get_be16(pb);
0e7eed09 657
744a9c75
MN
658 if (st->codec->codec_tag &&
659 (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
660 : st->codec->codec_tag != MKTAG('j', 'p', 'e', 'g'))
661 ){
7ce68923
DB
662 /* Multiple fourcc, we skip JPEG. This is not correct, we should
663 * export it as a separate AVStream but this needs a few changes
664 * in the MOV demuxer, patch welcome. */
e7cc4b52
BC
665 url_fskip(pb, size - (url_ftell(pb) - start_pos));
666 continue;
667 }
ee6e2dbe 668 sc->pseudo_stream_id= pseudo_stream_id;
221e21b7 669 sc->dref_id= dref_id;
e7cc4b52 670
bb270c08 671 st->codec->codec_tag = format;
1e5f5e3b 672 id = codec_get_id(codec_movaudio_tags, format);
bca7db35
MN
673 if (id<=0 && (format&0xFFFF) == 'm' + ('s'<<8))
674 id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
675
48855b26 676 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
99487f42 677 st->codec->codec_type = CODEC_TYPE_AUDIO;
48855b26
BC
678 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
679 format && format != MKTAG('m', 'p', '4', 's')) { /* skip old asf mpeg4 tag */
1e5f5e3b 680 id = codec_get_id(codec_movvideo_tags, format);
99487f42 681 if (id <= 0)
de23f234 682 id = codec_get_id(codec_bmp_tags, format);
99487f42
BC
683 if (id > 0)
684 st->codec->codec_type = CODEC_TYPE_VIDEO;
1e3c9307
MN
685 else if(st->codec->codec_type == CODEC_TYPE_DATA){
686 id = codec_get_id(ff_codec_movsubtitle_tags, format);
687 if(id > 0)
688 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
689 }
99487f42
BC
690 }
691
29c90869
BC
692 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
693 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
694 (format >> 24) & 0xff, st->codec->codec_type);
a20da52c 695
99487f42 696 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
01f4895c 697 st->codec->codec_id = id;
6cea494e
ZK
698 get_be16(pb); /* version */
699 get_be16(pb); /* revision level */
700 get_be32(pb); /* vendor */
701 get_be32(pb); /* temporal quality */
94472c1d 702 get_be32(pb); /* spatial quality */
3cb4ee51
BC
703
704 st->codec->width = get_be16(pb); /* width */
705 st->codec->height = get_be16(pb); /* height */
706
6cea494e
ZK
707 get_be32(pb); /* horiz resolution */
708 get_be32(pb); /* vert resolution */
709 get_be32(pb); /* data size, always 0 */
5cd62665 710 frames_per_sample = get_be16(pb); /* frames per samples */
f8c18cd7
BC
711
712 dprintf(c->fc, "frames/samples = %d\n", frames_per_sample);
713
576f1445
BC
714 get_buffer(pb, codec_name, 32); /* codec name, pascal string (FIXME: true for mp4?) */
715 if (codec_name[0] <= 31) {
716 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
717 st->codec->codec_name[codec_name[0]] = 0;
718 }
5cd62665 719
bb270c08 720 st->codec->bits_per_sample = get_be16(pb); /* depth */
01f4895c 721 st->codec->color_table_id = get_be16(pb); /* colortable id */
0de2157f
BC
722 dprintf(c->fc, "depth %d, ctab id %d\n",
723 st->codec->bits_per_sample, st->codec->color_table_id);
b595afaa 724 /* figure out the palette situation */
01f4895c
MN
725 color_depth = st->codec->bits_per_sample & 0x1F;
726 color_greyscale = st->codec->bits_per_sample & 0x20;
b595afaa
MM
727
728 /* if the depth is 2, 4, or 8 bpp, file is palettized */
115329f1 729 if ((color_depth == 2) || (color_depth == 4) ||
b595afaa 730 (color_depth == 8)) {
b595afaa 731 if (color_greyscale) {
b595afaa 732 /* compute the greyscale palette */
0de2157f 733 st->codec->bits_per_sample = color_depth;
b595afaa
MM
734 color_count = 1 << color_depth;
735 color_index = 255;
736 color_dec = 256 / (color_count - 1);
737 for (j = 0; j < color_count; j++) {
738 r = g = b = color_index;
739 c->palette_control.palette[j] =
740 (r << 16) | (g << 8) | (b);
741 color_index -= color_dec;
742 if (color_index < 0)
743 color_index = 0;
744 }
01f4895c 745 } else if (st->codec->color_table_id & 0x08) {
b595afaa
MM
746 /* if flag bit 3 is set, use the default palette */
747 color_count = 1 << color_depth;
748 if (color_depth == 2)
a90466f7 749 color_table = ff_qt_default_palette_4;
b595afaa 750 else if (color_depth == 4)
a90466f7 751 color_table = ff_qt_default_palette_16;
b595afaa 752 else
a90466f7 753 color_table = ff_qt_default_palette_256;
b595afaa
MM
754
755 for (j = 0; j < color_count; j++) {
756 r = color_table[j * 4 + 0];
757 g = color_table[j * 4 + 1];
758 b = color_table[j * 4 + 2];
759 c->palette_control.palette[j] =
760 (r << 16) | (g << 8) | (b);
761 }
b595afaa 762 } else {
b595afaa
MM
763 /* load the palette from the file */
764 color_start = get_be32(pb);
765 color_count = get_be16(pb);
766 color_end = get_be16(pb);
8b35bd80
MM
767 if ((color_start <= 255) &&
768 (color_end <= 255)) {
9de2919c
MM
769 for (j = color_start; j <= color_end; j++) {
770 /* each R, G, or B component is 16 bits;
771 * only use the top 8 bits; skip alpha bytes
772 * up front */
773 get_byte(pb);
774 get_byte(pb);
775 r = get_byte(pb);
776 get_byte(pb);
777 g = get_byte(pb);
778 get_byte(pb);
779 b = get_byte(pb);
780 get_byte(pb);
781 c->palette_control.palette[j] =
782 (r << 16) | (g << 8) | (b);
8b35bd80 783 }
b595afaa
MM
784 }
785 }
01f4895c
MN
786 st->codec->palctrl = &c->palette_control;
787 st->codec->palctrl->palette_changed = 1;
b595afaa 788 } else
01f4895c 789 st->codec->palctrl = NULL;
de23f234 790 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
b72708f8 791 int bits_per_sample;
de23f234 792 uint16_t version = get_be16(pb);
b595afaa 793
99487f42 794 st->codec->codec_id = id;
de23f234
BC
795 get_be16(pb); /* revision level */
796 get_be32(pb); /* vendor */
6d6d7970 797
de23f234 798 st->codec->channels = get_be16(pb); /* channel count */
318c5e05 799 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
de23f234 800 st->codec->bits_per_sample = get_be16(pb); /* sample size */
de23f234 801
77c75437 802 sc->audio_cid = get_be16(pb);
de23f234
BC
803 get_be16(pb); /* packet size = 0 */
804
805 st->codec->sample_rate = ((get_be32(pb) >> 16));
806
807 switch (st->codec->codec_id) {
b72708f8
BC
808 case CODEC_ID_PCM_S8:
809 case CODEC_ID_PCM_U8:
810 if (st->codec->bits_per_sample == 16)
811 st->codec->codec_id = CODEC_ID_PCM_S16BE;
812 break;
0dd39bfe 813 case CODEC_ID_PCM_S16LE:
de23f234
BC
814 case CODEC_ID_PCM_S16BE:
815 if (st->codec->bits_per_sample == 8)
816 st->codec->codec_id = CODEC_ID_PCM_S8;
0299a87c
BC
817 else if (st->codec->bits_per_sample == 24)
818 st->codec->codec_id = CODEC_ID_PCM_S24BE;
de23f234 819 break;
53152765
BC
820 /* set values for old format before stsd version 1 appeared */
821 case CODEC_ID_MACE3:
822 sc->samples_per_frame = 6;
823 sc->bytes_per_frame = 2*st->codec->channels;
824 break;
825 case CODEC_ID_MACE6:
826 sc->samples_per_frame = 6;
827 sc->bytes_per_frame = 1*st->codec->channels;
828 break;
829 case CODEC_ID_ADPCM_IMA_QT:
830 sc->samples_per_frame = 64;
831 sc->bytes_per_frame = 34*st->codec->channels;
832 break;
de23f234
BC
833 default:
834 break;
6d6d7970 835 }
14342fd5 836
755bfeab 837 //Read QT version 1 fields. In version 0 these do not exist.
318c5e05 838 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
152e9a43 839 if(!c->isom) {
80c5b9a1 840 if(version==1) {
e14f79ed 841 sc->samples_per_frame = get_be32(pb);
80c5b9a1 842 get_be32(pb); /* bytes per packet */
e14f79ed 843 sc->bytes_per_frame = get_be32(pb);
80c5b9a1
BC
844 get_be32(pb); /* bytes per sample */
845 } else if(version==2) {
846 get_be32(pb); /* sizeof struct only */
847 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
848 st->codec->channels = get_be32(pb);
849 get_be32(pb); /* always 0x7F000000 */
850 get_be32(pb); /* bits per channel if sound is uncompressed */
851 get_be32(pb); /* lcpm format specific flag */
852 get_be32(pb); /* bytes per audio packet if constant */
853 get_be32(pb); /* lpcm frames per audio packet if constant */
854 }
152e9a43 855 }
9770089d
BC
856
857 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
858 if (bits_per_sample) {
859 st->codec->bits_per_sample = bits_per_sample;
860 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
861 }
cc8d87b7
MN
862 } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
863 st->codec->codec_id= id;
de23f234
BC
864 } else {
865 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
866 url_fskip(pb, size - (url_ftell(pb) - start_pos));
6cea494e 867 }
de23f234
BC
868 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
869 a.size = size - (url_ftell(pb) - start_pos);
9cf0419b
PI
870 if (a.size > 8) {
871 if (mov_read_default(c, pb, a) < 0)
872 return -1;
873 } else if (a.size > 0)
de23f234 874 url_fskip(pb, a.size);
6cea494e 875 }
115329f1 876
60f5c96e 877 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
302c389e 878 st->codec->sample_rate= sc->time_scale;
e31bd3e3 879
d00f8e17 880 /* special codec parameters handling */
e31bd3e3 881 switch (st->codec->codec_id) {
989ac5a6 882#ifdef CONFIG_DV_DEMUXER
b60c0454
BC
883 case CODEC_ID_DVAUDIO:
884 c->dv_fctx = av_alloc_format_context();
885 c->dv_demux = dv_init_demux(c->dv_fctx);
886 if (!c->dv_demux) {
887 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
888 return -1;
889 }
890 sc->dv_audio_container = 1;
891 st->codec->codec_id = CODEC_ID_PCM_S16LE;
892 break;
989ac5a6 893#endif
b95319a2
BC
894 /* no ifdef since parameters are always those */
895 case CODEC_ID_AMR_WB:
896 st->codec->sample_rate= 16000;
897 st->codec->channels= 1; /* really needed */
898 break;
899 case CODEC_ID_AMR_NB:
900 st->codec->sample_rate= 8000;
901 st->codec->channels= 1; /* really needed */
902 break;
a41104f8 903 case CODEC_ID_MP2:
c59f24e6 904 case CODEC_ID_MP3:
95a07973 905 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
57004ff1 906 st->need_parsing = AVSTREAM_PARSE_FULL;
a41104f8 907 break;
74e9b9ae
BC
908 case CODEC_ID_ADPCM_MS:
909 case CODEC_ID_ADPCM_IMA_WAV:
910 st->codec->block_align = sc->bytes_per_frame;
911 break;
e31bd3e3
BC
912 default:
913 break;
914 }
5cd62665 915
6cea494e
ZK
916 return 0;
917}
918
5ca1d879 919static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 920{
5ca1d879 921 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 922 MOVStreamContext *sc = st->priv_data;
568e18b1 923 unsigned int i, entries;
b6a17df4 924
6cea494e 925 get_byte(pb); /* version */
23f08617 926 get_be24(pb); /* flags */
6cea494e
ZK
927
928 entries = get_be32(pb);
115329f1 929
e23848a4 930 if(entries >= UINT_MAX / sizeof(MOV_stsc_t))
568e18b1 931 return -1;
115329f1 932
f8c18cd7
BC
933 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
934
6cea494e 935 sc->sample_to_chunk_sz = entries;
e23848a4 936 sc->sample_to_chunk = av_malloc(entries * sizeof(MOV_stsc_t));
b6a17df4
ZK
937 if (!sc->sample_to_chunk)
938 return -1;
6cea494e
ZK
939 for(i=0; i<entries; i++) {
940 sc->sample_to_chunk[i].first = get_be32(pb);
941 sc->sample_to_chunk[i].count = get_be32(pb);
942 sc->sample_to_chunk[i].id = get_be32(pb);
6cea494e
ZK
943 }
944 return 0;
945}
946
247d56f5
BB
947static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
948{
949 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 950 MOVStreamContext *sc = st->priv_data;
568e18b1 951 unsigned int i, entries;
247d56f5 952
247d56f5 953 get_byte(pb); /* version */
23f08617 954 get_be24(pb); /* flags */
247d56f5
BB
955
956 entries = get_be32(pb);
115329f1 957
1c02d96f 958 if(entries >= UINT_MAX / sizeof(int))
568e18b1 959 return -1;
115329f1 960
247d56f5 961 sc->keyframe_count = entries;
f8c18cd7
BC
962
963 dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
964
1c02d96f 965 sc->keyframes = av_malloc(entries * sizeof(int));
247d56f5
BB
966 if (!sc->keyframes)
967 return -1;
968 for(i=0; i<entries; i++) {
969 sc->keyframes[i] = get_be32(pb);
f8c18cd7 970 //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
247d56f5
BB
971 }
972 return 0;
973}
974
5ca1d879 975static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
6cea494e 976{
5ca1d879 977 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 978 MOVStreamContext *sc = st->priv_data;
b72708f8 979 unsigned int i, entries, sample_size;
b6a17df4 980
6cea494e 981 get_byte(pb); /* version */
23f08617 982 get_be24(pb); /* flags */
5cd62665 983
b72708f8
BC
984 sample_size = get_be32(pb);
985 if (!sc->sample_size) /* do not overwrite value computed in stsd */
986 sc->sample_size = sample_size;
6cea494e 987 entries = get_be32(pb);
1c02d96f 988 if(entries >= UINT_MAX / sizeof(int))
568e18b1
MN
989 return -1;
990
6cea494e 991 sc->sample_count = entries;
b72708f8
BC
992 if (sample_size)
993 return 0;
994
f8c18cd7
BC
995 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
996
1c02d96f 997 sc->sample_sizes = av_malloc(entries * sizeof(int));
b6a17df4
ZK
998 if (!sc->sample_sizes)
999 return -1;
eb61405a 1000 for(i=0; i<entries; i++)
6cea494e 1001 sc->sample_sizes[i] = get_be32(pb);
6cea494e
ZK
1002 return 0;
1003}
1004
5ca1d879 1005static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
0147f198 1006{
5ca1d879 1007 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1008 MOVStreamContext *sc = st->priv_data;
568e18b1 1009 unsigned int i, entries;
d957696f
MN
1010 int64_t duration=0;
1011 int64_t total_sample_count=0;
b6a17df4 1012
0147f198 1013 get_byte(pb); /* version */
23f08617 1014 get_be24(pb); /* flags */
0147f198 1015 entries = get_be32(pb);
e23848a4 1016 if(entries >= UINT_MAX / sizeof(MOV_stts_t))
568e18b1 1017 return -1;
891f64b3 1018
cd7352d5 1019 sc->stts_count = entries;
e23848a4 1020 sc->stts_data = av_malloc(entries * sizeof(MOV_stts_t));
c1da59fa
BC
1021 if (!sc->stts_data)
1022 return -1;
f8c18cd7 1023 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
cd7352d5
MN
1024
1025 sc->time_rate=0;
1026
0147f198 1027 for(i=0; i<entries; i++) {
cd461d48
MN
1028 int sample_duration;
1029 int sample_count;
0147f198 1030
891f64b3 1031 sample_count=get_be32(pb);
0147f198 1032 sample_duration = get_be32(pb);
cd7352d5
MN
1033 sc->stts_data[i].count= sample_count;
1034 sc->stts_data[i].duration= sample_duration;
1035
1036 sc->time_rate= ff_gcd(sc->time_rate, sample_duration);
961e0ccd 1037
318c5e05 1038 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
4e5ef14f 1039
7e815047 1040 duration+=(int64_t)sample_duration*sample_count;
891f64b3 1041 total_sample_count+=sample_count;
891f64b3 1042 }
1043
961e0ccd
MN
1044 st->nb_frames= total_sample_count;
1045 if(duration)
1046 st->duration= duration;
0147f198
FR
1047 return 0;
1048}
1049
c4ac052b
MN
1050static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1051{
70a61ed4 1052 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1053 MOVStreamContext *sc = st->priv_data;
c4ac052b
MN
1054 unsigned int i, entries;
1055
c4ac052b 1056 get_byte(pb); /* version */
23f08617 1057 get_be24(pb); /* flags */
c4ac052b 1058 entries = get_be32(pb);
e23848a4 1059 if(entries >= UINT_MAX / sizeof(MOV_stts_t))
c4ac052b
MN
1060 return -1;
1061
70a61ed4 1062 sc->ctts_count = entries;
e23848a4 1063 sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
c1da59fa
BC
1064 if (!sc->ctts_data)
1065 return -1;
318c5e05 1066 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
4e5ef14f 1067
c4ac052b 1068 for(i=0; i<entries; i++) {
70a61ed4
MN
1069 int count =get_be32(pb);
1070 int duration =get_be32(pb);
1071
b0519015
BC
1072 if (duration < 0) {
1073 av_log(c->fc, AV_LOG_ERROR, "negative ctts, ignoring\n");
1074 sc->ctts_count = 0;
1075 url_fskip(pb, 8 * (entries - i - 1));
1076 break;
1077 }
70a61ed4
MN
1078 sc->ctts_data[i].count = count;
1079 sc->ctts_data[i].duration= duration;
1080
1081 sc->time_rate= ff_gcd(sc->time_rate, duration);
c4ac052b
MN
1082 }
1083 return 0;
1084}
1085
1e77810d
BC
1086static void mov_build_index(MOVContext *mov, AVStream *st)
1087{
1088 MOVStreamContext *sc = st->priv_data;
1089 offset_t current_offset;
1090 int64_t current_dts = 0;
1091 unsigned int stts_index = 0;
1092 unsigned int stsc_index = 0;
1093 unsigned int stss_index = 0;
1094 unsigned int i, j;
1095
1096 if (sc->sample_sizes || st->codec->codec_type == CODEC_TYPE_VIDEO ||
1097 sc->audio_cid == -2) {
1098 unsigned int current_sample = 0;
1099 unsigned int stts_sample = 0;
1100 unsigned int keyframe, sample_size;
1101 unsigned int distance = 0;
1102 int key_off = sc->keyframes && sc->keyframes[0] == 1;
1103
1104 st->nb_frames = sc->sample_count;
1105 for (i = 0; i < sc->chunk_count; i++) {
1106 current_offset = sc->chunk_offsets[i];
1107 if (stsc_index + 1 < sc->sample_to_chunk_sz &&
1108 i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1109 stsc_index++;
1110 for (j = 0; j < sc->sample_to_chunk[stsc_index].count; j++) {
1111 if (current_sample >= sc->sample_count) {
1112 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1113 goto out;
1114 }
1115 keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
1116 if (keyframe) {
1117 distance = 0;
1118 if (stss_index + 1 < sc->keyframe_count)
1119 stss_index++;
1120 }
1121 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
1122 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1123 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
1124 current_offset, current_dts, sample_size, distance, keyframe);
1125 if(sc->sample_to_chunk[stsc_index].id - 1 == sc->pseudo_stream_id)
1126 av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
1127 keyframe ? AVINDEX_KEYFRAME : 0);
1128 current_offset += sample_size;
1129 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1130 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1131 distance++;
1132 stts_sample++;
1133 current_sample++;
1134 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1135 stts_sample = 0;
1136 stts_index++;
1137 }
1138 }
1139 }
1140 } else { /* read whole chunk */
1141 unsigned int chunk_samples, chunk_size, chunk_duration;
1142 unsigned int frames = 1;
1143 for (i = 0; i < sc->chunk_count; i++) {
1144 current_offset = sc->chunk_offsets[i];
1145 if (stsc_index + 1 < sc->sample_to_chunk_sz &&
1146 i + 1 == sc->sample_to_chunk[stsc_index + 1].first)
1147 stsc_index++;
1148 chunk_samples = sc->sample_to_chunk[stsc_index].count;
1149 /* get chunk size, beware of alaw/ulaw/mace */
1150 if (sc->samples_per_frame > 0 &&
1151 (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
1152 if (sc->samples_per_frame < 1024)
1153 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1154 else {
1155 chunk_size = sc->bytes_per_frame;
1156 frames = chunk_samples / sc->samples_per_frame;
1157 chunk_samples = sc->samples_per_frame;
1158 }
1159 } else if (sc->sample_size > 1 || st->codec->bits_per_sample == 8) {
1160 chunk_size = chunk_samples * sc->sample_size;
1161 } else {
1162 av_log(mov->fc, AV_LOG_ERROR, "could not determine chunk size, report problem\n");
1163 goto out;
1164 }
1165 for (j = 0; j < frames; j++) {
1166 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1167 /* get chunk duration */
1168 chunk_duration = 0;
1169 while (chunk_samples > 0) {
1170 if (chunk_samples < sc->stts_data[stts_index].count) {
1171 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1172 sc->stts_data[stts_index].count -= chunk_samples;
1173 break;
1174 } else {
1175 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1176 chunk_samples -= sc->stts_data[stts_index].count;
1177 if (stts_index + 1 < sc->stts_count)
1178 stts_index++;
1179 }
1180 }
1181 current_offset += sc->bytes_per_frame;
117a9190
BC
1182 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
1183 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
1184 chunk_size, chunk_duration);
1e77810d
BC
1185 assert(chunk_duration % sc->time_rate == 0);
1186 current_dts += chunk_duration / sc->time_rate;
1187 }
1188 }
1189 }
1190 out:
1191 /* adjust sample count to avindex entries */
1192 sc->sample_count = st->nb_index_entries;
1193}
bd991df2 1194
5ca1d879
ZK
1195static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1196{
1197 AVStream *st;
1198 MOVStreamContext *sc;
bd991df2 1199 int ret;
5ca1d879 1200
5ca1d879 1201 st = av_new_stream(c->fc, c->fc->nb_streams);
3efe8848 1202 if (!st) return AVERROR(ENOMEM);
9a630c25 1203 sc = av_mallocz(sizeof(MOVStreamContext));
2922cbdb 1204 if (!sc) return AVERROR(ENOMEM);
5ca1d879 1205
5ca1d879 1206 st->priv_data = sc;
05edc1a7 1207 st->codec->codec_type = CODEC_TYPE_DATA;
25c4950e 1208 st->start_time = 0; /* XXX: check */
5ca1d879 1209
bd991df2
BC
1210 if ((ret = mov_read_default(c, pb, atom)) < 0)
1211 return ret;
1212
1213 /* sanity checks */
1214 if(!sc->stts_count || !sc->chunk_count || !sc->sample_to_chunk_sz ||
1215 (!sc->sample_size && !sc->sample_count)){
1216 av_log(c->fc, AV_LOG_ERROR, "missing mandatory atoms, broken header\n");
1217 sc->sample_count = 0; //ignore track
1218 return 0;
1219 }
1220 if(!sc->time_rate)
1221 sc->time_rate=1;
1222 if(!sc->time_scale)
1223 sc->time_scale= c->time_scale;
1224 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1225
1226 if (st->codec->codec_type == CODEC_TYPE_AUDIO && sc->stts_count == 1)
1227 st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
1228
1229 if(st->duration != AV_NOPTS_VALUE){
1230 assert(st->duration % sc->time_rate == 0);
1231 st->duration /= sc->time_rate;
1232 }
1233 sc->ffindex = st->index;
1234 mov_build_index(c, st);
1235
1236 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
1237 if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
1238 av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening external essence: %s\n",
1239 st->index, strerror(errno));
1240 } else
1241 sc->pb = c->fc->pb;
1242
1243 switch (st->codec->codec_id) {
1244#ifdef CONFIG_H261_DECODER
1245 case CODEC_ID_H261:
1246#endif
1247#ifdef CONFIG_H263_DECODER
1248 case CODEC_ID_H263:
1249#endif
1250#ifdef CONFIG_MPEG4_DECODER
1251 case CODEC_ID_MPEG4:
1252#endif
1253 st->codec->width= 0; /* let decoder init width/height */
1254 st->codec->height= 0;
1255 break;
1256#ifdef CONFIG_LIBFAAD
1257 case CODEC_ID_AAC:
1258#endif
1259#ifdef CONFIG_VORBIS_DECODER
1260 case CODEC_ID_VORBIS:
1261#endif
1262 case CODEC_ID_MP3ON4:
1263 st->codec->sample_rate= 0; /* let decoder init parameters properly */
1264 break;
1265 }
f9900374
BC
1266
1267 /* Do not need those anymore. */
1268 av_freep(&sc->chunk_offsets);
1269 av_freep(&sc->sample_to_chunk);
1270 av_freep(&sc->sample_sizes);
1271 av_freep(&sc->keyframes);
1272 av_freep(&sc->stts_data);
1273
bd991df2 1274 return 0;
5ca1d879
ZK
1275}
1276
d2ace376
BF
1277static void mov_parse_udta_string(ByteIOContext *pb, char *str, int size)
1278{
1279 uint16_t str_size = get_be16(pb); /* string length */;
1280
1281 get_be16(pb); /* skip language */
1282 get_buffer(pb, str, FFMIN(size, str_size));
1283}
1284
1285static int mov_read_udta(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1286{
1287 uint64_t end = url_ftell(pb) + atom.size;
1288
1289 while (url_ftell(pb) + 8 < end) {
1290 uint32_t tag_size = get_be32(pb);
1291 uint32_t tag = get_le32(pb);
1292 uint64_t next = url_ftell(pb) + tag_size - 8;
1293
aaac6c29
BC
1294 if (next > end) // stop if tag_size is wrong
1295 break;
1296
d2ace376
BF
1297 switch (tag) {
1298 case MKTAG(0xa9,'n','a','m'):
1299 mov_parse_udta_string(pb, c->fc->title, sizeof(c->fc->title));
1300 break;
1301 case MKTAG(0xa9,'w','r','t'):
1302 mov_parse_udta_string(pb, c->fc->author, sizeof(c->fc->author));
1303 break;
1304 case MKTAG(0xa9,'c','p','y'):
1305 mov_parse_udta_string(pb, c->fc->copyright, sizeof(c->fc->copyright));
1306 break;
1307 case MKTAG(0xa9,'i','n','f'):
1308 mov_parse_udta_string(pb, c->fc->comment, sizeof(c->fc->comment));
1309 break;
1310 default:
1311 break;
1312 }
1313
1314 url_fseek(pb, next, SEEK_SET);
1315 }
1316
1317 return 0;
1318}
1319
5ca1d879
ZK
1320static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1321{
f444b977
BC
1322 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
1323 int version = get_byte(pb);
5ca1d879 1324
23f08617 1325 get_be24(pb); /* flags */
5ca1d879
ZK
1326 /*
1327 MOV_TRACK_ENABLED 0x0001
1328 MOV_TRACK_IN_MOVIE 0x0002
1329 MOV_TRACK_IN_PREVIEW 0x0004
1330 MOV_TRACK_IN_POSTER 0x0008
1331 */
1332
1175561e
BC
1333 if (version == 1) {
1334 get_be64(pb);
1335 get_be64(pb);
1336 } else {
1337 get_be32(pb); /* creation time */
1338 get_be32(pb); /* modification time */
1339 }
5ca1d879
ZK
1340 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1341 get_be32(pb); /* reserved */
25c4950e 1342 st->start_time = 0; /* check */
117a9190
BC
1343 /* highlevel (considering edits) duration in movie timebase */
1344 (version == 1) ? get_be64(pb) : get_be32(pb);
5ca1d879
ZK
1345 get_be32(pb); /* reserved */
1346 get_be32(pb); /* reserved */
1347
1348 get_be16(pb); /* layer */
1349 get_be16(pb); /* alternate group */
1350 get_be16(pb); /* volume */
1351 get_be16(pb); /* reserved */
1352
1353 url_fskip(pb, 36); /* display matrix */
1354
1355 /* those are fixed-point */
c3daf8d8
BC
1356 get_be32(pb); /* track width */
1357 get_be32(pb); /* track height */
5ca1d879
ZK
1358
1359 return 0;
1360}
1361
1362/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1363/* like the files created with Adobe Premiere 5.0, for samples see */
1364/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
1365static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1366{
1367 int err;
5ca1d879 1368
5ca1d879
ZK
1369 if (atom.size < 8)
1370 return 0; /* continue */
1371 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1372 url_fskip(pb, atom.size - 4);
1373 return 0;
1374 }
1375 atom.type = get_le32(pb);
1376 atom.offset += 8;
1377 atom.size -= 8;
fd6e513e 1378 if (atom.type != MKTAG('m', 'd', 'a', 't')) {
5ca1d879
ZK
1379 url_fskip(pb, atom.size);
1380 return 0;
1381 }
1382 err = mov_read_mdat(c, pb, atom);
5ca1d879
ZK
1383 return err;
1384}
1385
5ca1d879 1386static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
0147f198 1387{
d966b2f0 1388#ifdef CONFIG_ZLIB
0147f198 1389 ByteIOContext ctx;
b6a17df4
ZK
1390 uint8_t *cmov_data;
1391 uint8_t *moov_data; /* uncompressed data */
0147f198
FR
1392 long cmov_len, moov_len;
1393 int ret;
b6a17df4 1394
0147f198
FR
1395 get_be32(pb); /* dcom atom */
1396 if (get_le32(pb) != MKTAG( 'd', 'c', 'o', 'm' ))
1397 return -1;
1398 if (get_le32(pb) != MKTAG( 'z', 'l', 'i', 'b' )) {
4e5ef14f 1399 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
0147f198
FR
1400 return -1;
1401 }
1402 get_be32(pb); /* cmvd atom */
1403 if (get_le32(pb) != MKTAG( 'c', 'm', 'v', 'd' ))
1404 return -1;
1405 moov_len = get_be32(pb); /* uncompressed size */
5ca1d879 1406 cmov_len = atom.size - 6 * 4;
5cd62665 1407
9a630c25 1408 cmov_data = av_malloc(cmov_len);
0147f198
FR
1409 if (!cmov_data)
1410 return -1;
9a630c25 1411 moov_data = av_malloc(moov_len);
0147f198
FR
1412 if (!moov_data) {
1413 av_free(cmov_data);
1414 return -1;
1415 }
1416 get_buffer(pb, cmov_data, cmov_len);
b6a17df4 1417 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
0147f198 1418 return -1;
942f3bb5 1419 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
0147f198 1420 return -1;
5ca1d879
ZK
1421 atom.type = MKTAG( 'm', 'o', 'o', 'v' );
1422 atom.offset = 0;
1423 atom.size = moov_len;
9ed83b0a 1424#ifdef DEBUG
dbb4f00a 1425// { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
9ed83b0a 1426#endif
5ca1d879 1427 ret = mov_read_default(c, &ctx, atom);
0147f198
FR
1428 av_free(moov_data);
1429 av_free(cmov_data);
1430 return ret;
d966b2f0
BC
1431#else
1432 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1433 return -1;
0147f198 1434#endif
d966b2f0 1435}
0147f198 1436
baf25c9d
GC
1437/* edit list atom */
1438static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
1439{
7c622eed 1440 MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
e0977c80
BC
1441 int i, edit_count;
1442
1443 get_byte(pb); /* version */
23f08617 1444 get_be24(pb); /* flags */
7c622eed 1445 edit_count= sc->edit_count = get_be32(pb); /* entries */
e0977c80
BC
1446
1447 for(i=0; i<edit_count; i++){
d435e520 1448 int time;
e0977c80 1449 get_be32(pb); /* Track duration */
d435e520 1450 time = get_be32(pb); /* Media time */
e0977c80 1451 get_be32(pb); /* Media rate */
d435e520
BC
1452 if (time != 0)
1453 av_log(c->fc, AV_LOG_WARNING, "edit list not starting at 0, "
1454 "a/v desync might occur, patch welcome\n");
e0977c80 1455 }
7c622eed 1456 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
e0977c80 1457 return 0;
baf25c9d
GC
1458}
1459
6cea494e 1460static const MOVParseTableEntry mov_default_parse_table[] = {
5ca1d879 1461{ MKTAG( 'c', 'o', '6', '4' ), mov_read_stco },
c4ac052b 1462{ MKTAG( 'c', 't', 't', 's' ), mov_read_ctts }, /* composition time to sample */
221e21b7
BC
1463{ MKTAG( 'd', 'i', 'n', 'f' ), mov_read_default },
1464{ MKTAG( 'd', 'r', 'e', 'f' ), mov_read_dref },
5ca1d879 1465{ MKTAG( 'e', 'd', 't', 's' ), mov_read_default },
58e555d4 1466{ MKTAG( 'e', 'l', 's', 't' ), mov_read_elst },
de23f234 1467{ MKTAG( 'e', 'n', 'd', 'a' ), mov_read_enda },
9a63497b 1468{ MKTAG( 'f', 'i', 'e', 'l' ), mov_read_extradata },
4ea28253 1469{ MKTAG( 'f', 't', 'y', 'p' ), mov_read_ftyp },
bde24601 1470{ MKTAG( 'g', 'l', 'b', 'l' ), mov_read_glbl },
5ca1d879 1471{ MKTAG( 'h', 'd', 'l', 'r' ), mov_read_hdlr },
014a5102 1472{ MKTAG( 'j', 'p', '2', 'h' ), mov_read_extradata },
5ca1d879
ZK
1473{ MKTAG( 'm', 'd', 'a', 't' ), mov_read_mdat },
1474{ MKTAG( 'm', 'd', 'h', 'd' ), mov_read_mdhd },
1475{ MKTAG( 'm', 'd', 'i', 'a' ), mov_read_default },
1476{ MKTAG( 'm', 'i', 'n', 'f' ), mov_read_default },
1477{ MKTAG( 'm', 'o', 'o', 'v' ), mov_read_moov },
5ca1d879 1478{ MKTAG( 'm', 'v', 'h', 'd' ), mov_read_mvhd },
169eb021 1479{ MKTAG( 'S', 'M', 'I', ' ' ), mov_read_smi }, /* Sorenson extension ??? */
014a5102 1480{ MKTAG( 'a', 'l', 'a', 'c' ), mov_read_extradata }, /* alac specific atom */
bde24601 1481{ MKTAG( 'a', 'v', 'c', 'C' ), mov_read_glbl },
5ca1d879
ZK
1482{ MKTAG( 's', 't', 'b', 'l' ), mov_read_default },
1483{ MKTAG( 's', 't', 'c', 'o' ), mov_read_stco },
5ca1d879
ZK
1484{ MKTAG( 's', 't', 's', 'c' ), mov_read_stsc },
1485{ MKTAG( 's', 't', 's', 'd' ), mov_read_stsd }, /* sample description */
247d56f5 1486{ MKTAG( 's', 't', 's', 's' ), mov_read_stss }, /* sync sample */
5ca1d879
ZK
1487{ MKTAG( 's', 't', 's', 'z' ), mov_read_stsz }, /* sample size */
1488{ MKTAG( 's', 't', 't', 's' ), mov_read_stts },
1489{ MKTAG( 't', 'k', 'h', 'd' ), mov_read_tkhd }, /* track header */
1490{ MKTAG( 't', 'r', 'a', 'k' ), mov_read_trak },
d2ace376 1491{ MKTAG( 'u', 'd', 't', 'a' ), mov_read_udta },
d9b1c197 1492{ MKTAG( 'w', 'a', 'v', 'e' ), mov_read_wave },
5ca1d879 1493{ MKTAG( 'e', 's', 'd', 's' ), mov_read_esds },
5ca1d879 1494{ MKTAG( 'w', 'i', 'd', 'e' ), mov_read_wide }, /* place holder */
5ca1d879 1495{ MKTAG( 'c', 'm', 'o', 'v' ), mov_read_cmov },
bcfe2ba0 1496{ 0, NULL }
6cea494e
ZK
1497};
1498
c9a65ca8
FB
1499static int mov_probe(AVProbeData *p)
1500{
0e7eed09
FB
1501 unsigned int offset;
1502 uint32_t tag;
2497479f 1503 int score = 0;
3ffe3793 1504
c9a65ca8 1505 /* check file header */
0e7eed09
FB
1506 offset = 0;
1507 for(;;) {
1508 /* ignore invalid offset */
1509 if ((offset + 8) > (unsigned int)p->buf_size)
2497479f 1510 return score;
fead30d4 1511 tag = AV_RL32(p->buf + offset + 4);
0e7eed09 1512 switch(tag) {
2497479f 1513 /* check for obvious tags */
fc5188f3 1514 case MKTAG( 'j', 'P', ' ', ' ' ): /* jpeg 2000 signature */
0e7eed09 1515 case MKTAG( 'm', 'o', 'o', 'v' ):
8b879f18
FR
1516 case MKTAG( 'm', 'd', 'a', 't' ):
1517 case MKTAG( 'p', 'n', 'o', 't' ): /* detect movs with preview pics like ew.mov and april.mov */
bc634f6f 1518 case MKTAG( 'u', 'd', 't', 'a' ): /* Packet Video PVAuthor adds this and a lot of more junk */
3ffe3793 1519 return AVPROBE_SCORE_MAX;
2497479f 1520 /* those are more common words, so rate then a bit less */
263a93ec 1521 case MKTAG( 'e', 'd', 'i', 'w' ): /* xdcam files have reverted first tags */
2497479f
FR
1522 case MKTAG( 'w', 'i', 'd', 'e' ):
1523 case MKTAG( 'f', 'r', 'e', 'e' ):
1524 case MKTAG( 'j', 'u', 'n', 'k' ):
1525 case MKTAG( 'p', 'i', 'c', 't' ):
1526 return AVPROBE_SCORE_MAX - 5;
da00a1bd 1527 case MKTAG(0x82,0x82,0x7f,0x7d ):
0e7eed09 1528 case MKTAG( 'f', 't', 'y', 'p' ):
5ca1d879 1529 case MKTAG( 's', 'k', 'i', 'p' ):
0d23cb84 1530 case MKTAG( 'u', 'u', 'i', 'd' ):
fead30d4 1531 offset = AV_RB32(p->buf+offset) + offset;
2497479f
FR
1532 /* if we only find those cause probedata is too small at least rate them */
1533 score = AVPROBE_SCORE_MAX - 50;
0e7eed09
FB
1534 break;
1535 default:
1536 /* unrecognized tag */
2497479f 1537 return score;
0e7eed09 1538 }
3ffe3793 1539 }
2497479f 1540 return score;
c9a65ca8
FB
1541}
1542
a266644f 1543static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
6cea494e 1544{
e4141433 1545 MOVContext *mov = s->priv_data;
899681cd 1546 ByteIOContext *pb = s->pb;
f9900374 1547 int err;
5ca1d879 1548 MOV_atom_t atom = { 0, 0, 0 };
6cea494e 1549
6cea494e 1550 mov->fc = s;
117a9190
BC
1551 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1552 if(!url_is_streamed(pb))
bb270c08 1553 atom.size = url_fsize(pb);
6cea494e 1554 else
fa22ca22 1555 atom.size = INT64_MAX;
6cea494e 1556
6cea494e 1557 /* check MOV header */
5ca1d879 1558 err = mov_read_default(mov, pb, atom);
25fa62e1 1559 if (err<0 || (!mov->found_moov && !mov->found_mdat)) {
bb270c08 1560 av_log(s, AV_LOG_ERROR, "mov: header not found !!! (err:%d, moov:%d, mdat:%d) pos:%"PRId64"\n",
e3c4740f 1561 err, mov->found_moov, mov->found_mdat, url_ftell(pb));
bb270c08 1562 return -1;
6cea494e 1563 }
318c5e05 1564 dprintf(mov->fc, "on_parse_exit_offset=%d\n", (int) url_ftell(pb));
4e5ef14f 1565
6cea494e
ZK
1566 return 0;
1567}
1568
a266644f 1569static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
6cea494e 1570{
b72708f8
BC
1571 MOVContext *mov = s->priv_data;
1572 MOVStreamContext *sc = 0;
1573 AVIndexEntry *sample = 0;
86d8602f 1574 int64_t best_dts = INT64_MAX;
b72708f8 1575 int i;
3ffe3793 1576
11f16b66 1577 for (i = 0; i < s->nb_streams; i++) {
7c622eed
BC
1578 AVStream *st = s->streams[i];
1579 MOVStreamContext *msc = st->priv_data;
221e21b7 1580 if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
7c622eed 1581 AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
29c90869
BC
1582 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
1583 AV_TIME_BASE, msc->time_scale);
1c02d96f 1584 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
687f35f3
BC
1585 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
1586 (!url_is_streamed(s->pb) &&
221e21b7 1587 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
687f35f3 1588 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
221e21b7 1589 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
b72708f8
BC
1590 sample = current_sample;
1591 best_dts = dts;
1592 sc = msc;
86d8602f 1593 }
6cea494e 1594 }
6cea494e 1595 }
b72708f8
BC
1596 if (!sample)
1597 return -1;
1598 /* must be done just before reading, to avoid infinite loop on sample */
1599 sc->current_sample++;
221e21b7 1600 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
29c90869
BC
1601 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
1602 sc->ffindex, sample->pos);
bb270c08 1603 return -1;
6cea494e 1604 }
221e21b7 1605 av_get_packet(sc->pb, pkt, sample->size);
312954f0 1606#ifdef CONFIG_DV_DEMUXER
56ea717b
BC
1607 if (mov->dv_demux && sc->dv_audio_container) {
1608 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1609 av_free(pkt->data);
300aa2b0
BC
1610 pkt->size = 0;
1611 if (dv_get_packet(mov->dv_demux, pkt) < 0)
1612 return -1;
56ea717b 1613 }
312954f0 1614#endif
b72708f8
BC
1615 pkt->stream_index = sc->ffindex;
1616 pkt->dts = sample->timestamp;
1617 if (sc->ctts_data) {
1618 assert(sc->ctts_data[sc->sample_to_ctime_index].duration % sc->time_rate == 0);
1619 pkt->pts = pkt->dts + sc->ctts_data[sc->sample_to_ctime_index].duration / sc->time_rate;
1620 /* update ctts context */
1621 sc->sample_to_ctime_sample++;
29c90869
BC
1622 if (sc->sample_to_ctime_index < sc->ctts_count &&
1623 sc->ctts_data[sc->sample_to_ctime_index].count == sc->sample_to_ctime_sample) {
b72708f8
BC
1624 sc->sample_to_ctime_index++;
1625 sc->sample_to_ctime_sample = 0;
b565ea09 1626 }
b72708f8
BC
1627 } else {
1628 pkt->pts = pkt->dts;
b565ea09 1629 }
b72708f8
BC
1630 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1631 pkt->pos = sample->pos;
29c90869
BC
1632 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
1633 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
b72708f8
BC
1634 return 0;
1635}
3ffe3793 1636
b72708f8
BC
1637static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1638{
1639 MOVStreamContext *sc = st->priv_data;
1640 int sample, time_sample;
1641 int i;
115329f1 1642
b72708f8 1643 sample = av_index_search_timestamp(st, timestamp, flags);
318c5e05 1644 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
b72708f8
BC
1645 if (sample < 0) /* not sure what to do */
1646 return -1;
1647 sc->current_sample = sample;
1c02d96f 1648 dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
b72708f8
BC
1649 /* adjust ctts index */
1650 if (sc->ctts_data) {
1651 time_sample = 0;
1652 for (i = 0; i < sc->ctts_count; i++) {
54a5c719
BC
1653 int next = time_sample + sc->ctts_data[i].count;
1654 if (next > sc->current_sample) {
b72708f8 1655 sc->sample_to_ctime_index = i;
54a5c719 1656 sc->sample_to_ctime_sample = sc->current_sample - time_sample;
b72708f8 1657 break;
115329f1 1658 }
54a5c719 1659 time_sample = next;
247d56f5 1660 }
247d56f5 1661 }
b72708f8 1662 return sample;
6cea494e
ZK
1663}
1664
961e0ccd 1665static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
baf25c9d 1666{
b72708f8
BC
1667 AVStream *st;
1668 int64_t seek_timestamp, timestamp;
1669 int sample;
1670 int i;
baf25c9d 1671
b72708f8 1672 if (stream_index >= s->nb_streams)
baf25c9d 1673 return -1;
baf25c9d 1674
b72708f8
BC
1675 st = s->streams[stream_index];
1676 sample = mov_seek_stream(st, sample_time, flags);
1677 if (sample < 0)
baf25c9d 1678 return -1;
baf25c9d 1679
b72708f8
BC
1680 /* adjust seek timestamp to found sample timestamp */
1681 seek_timestamp = st->index_entries[sample].timestamp;
baf25c9d 1682
b72708f8
BC
1683 for (i = 0; i < s->nb_streams; i++) {
1684 st = s->streams[i];
1685 if (stream_index == i || st->discard == AVDISCARD_ALL)
1686 continue;
baf25c9d 1687
b72708f8
BC
1688 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
1689 mov_seek_stream(st, timestamp, flags);
115329f1 1690 }
baf25c9d
GC
1691 return 0;
1692}
baf25c9d 1693
a266644f 1694static int mov_read_close(AVFormatContext *s)
6cea494e 1695{
221e21b7 1696 int i, j;
e4141433 1697 MOVContext *mov = s->priv_data;
11f16b66 1698 for(i=0; i<s->nb_streams; i++) {
7c622eed
BC
1699 MOVStreamContext *sc = s->streams[i]->priv_data;
1700 av_freep(&sc->ctts_data);
221e21b7
BC
1701 for (j=0; j<sc->drefs_count; j++)
1702 av_freep(&sc->drefs[j].path);
1703 av_freep(&sc->drefs);
1704 if (sc->pb && sc->pb != s->pb)
1705 url_fclose(sc->pb);
4440b118 1706 }
b60c0454
BC
1707 if(mov->dv_demux){
1708 for(i=0; i<mov->dv_fctx->nb_streams; i++){
1709 av_freep(&mov->dv_fctx->streams[i]->codec);
1710 av_freep(&mov->dv_fctx->streams[i]);
1711 }
1712 av_freep(&mov->dv_fctx);
1713 av_freep(&mov->dv_demux);
1714 }
6cea494e
ZK
1715 return 0;
1716}
1717
ff70e601 1718AVInputFormat mov_demuxer = {
fc5188f3
BC
1719 "mov,mp4,m4a,3gp,3g2,mj2",
1720 "QuickTime/MPEG4/Motion JPEG 2000 format",
c9a65ca8
FB
1721 sizeof(MOVContext),
1722 mov_probe,
6cea494e
ZK
1723 mov_read_header,
1724 mov_read_packet,
1725 mov_read_close,
baf25c9d 1726 mov_read_seek,
6cea494e 1727};