cosmetics: sample_to_chunk -> stsc
[libav.git] / libavformat / mov.c
CommitLineData
6cea494e 1/*
7fbde343 2 * MOV demuxer
406792e7 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
6a5d31ac 26#include "libavutil/intreadwrite.h"
6cea494e 27#include "avformat.h"
9d9f4119 28#include "riff.h"
e40ee6a2 29#include "isom.h"
b60c0454 30#include "dv.h"
245976da
DB
31#include "libavcodec/mpeg4audio.h"
32#include "libavcodec/mpegaudiodata.h"
6cea494e 33
b250f9c6 34#if CONFIG_ZLIB
0147f198
FR
35#include <zlib.h>
36#endif
37
6cea494e
ZK
38/*
39 * First version by Francois Revol revol@free.fr
115329f1 40 * Seek function by Gael Chardon gael.dev@4now.net
5ca1d879 41 *
6cea494e 42 * Features and limitations:
5ca1d879 43 * - reads most of the QT files I have (at least the structure),
6cea494e 44 * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
6cea494e 45 * - the code is quite ugly... maybe I won't do it recursive next time :-)
5ca1d879 46 *
6cea494e
ZK
47 * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
48 * when coding this :) (it's a writer anyway)
5ca1d879 49 *
6cea494e
ZK
50 * Reference documents:
51 * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
52 * Apple:
baf25c9d 53 * http://developer.apple.com/documentation/QuickTime/QTFF/
15c8dbe7 54 * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
6cea494e
ZK
55 * QuickTime is a trademark of Apple (AFAIK :))
56 */
57
b595afaa
MM
58#include "qtpalette.h"
59
baf25c9d 60
cd7352d5
MN
61#undef NDEBUG
62#include <assert.h>
63
6cea494e
ZK
64/* the QuickTime file format is quite convoluted...
65 * it has lots of index tables, each indexing something in another one...
66 * Here we just use what is needed to read the chunks
67 */
68
e23848a4 69typedef struct {
1c02d96f
BC
70 int first;
71 int count;
72 int id;
c3e92a6c 73} MOVStsc;
6cea494e 74
5cd62665 75typedef struct {
5ca1d879 76 uint32_t type;
221e21b7 77 char *path;
c3e92a6c 78} MOVDref;
221e21b7
BC
79
80typedef struct {
81 uint32_t type;
5ca1d879
ZK
82 int64_t offset;
83 int64_t size; /* total size (excluding the size and type fields) */
c3e92a6c 84} MOVAtom;
5ca1d879 85
b6a17df4
ZK
86struct MOVParseTableEntry;
87
61aedb0f
BC
88typedef struct {
89 unsigned track_id;
90 uint64_t base_data_offset;
91 uint64_t moof_offset;
92 unsigned stsd_id;
93 unsigned duration;
94 unsigned size;
95 unsigned flags;
96} MOVFragment;
97
98typedef struct {
99 unsigned track_id;
100 unsigned stsd_id;
101 unsigned duration;
102 unsigned size;
103 unsigned flags;
104} MOVTrackExt;
105
6cea494e 106typedef struct MOVStreamContext {
221e21b7 107 ByteIOContext *pb;
6cea494e 108 int ffindex; /* the ffmpeg stream id */
1c02d96f 109 int next_chunk;
44d3fea5 110 unsigned int chunk_count;
0c1a9eda 111 int64_t *chunk_offsets;
44d3fea5 112 unsigned int stts_count;
c3e92a6c 113 MOVStts *stts_data;
44d3fea5 114 unsigned int ctts_count;
c3e92a6c 115 MOVStts *ctts_data;
04e06cfa
BC
116 unsigned int stsc_count;
117 MOVStsc *stsc_data;
a234e38d
BC
118 int ctts_index;
119 int ctts_sample;
44d3fea5
BC
120 unsigned int sample_size;
121 unsigned int sample_count;
1c02d96f 122 int *sample_sizes;
44d3fea5 123 unsigned int keyframe_count;
1c02d96f 124 int *keyframes;
5cd62665 125 int time_scale;
cd7352d5 126 int time_rate;
425c9962 127 int time_offset; ///< time offset of the first edit list entry
1c02d96f 128 int current_sample;
e14f79ed
BC
129 unsigned int bytes_per_frame;
130 unsigned int samples_per_frame;
b60c0454 131 int dv_audio_container;
978677a7 132 int pseudo_stream_id; ///< -1 means demux all ids
77c75437 133 int16_t audio_cid; ///< stsd audio compression id
221e21b7 134 unsigned drefs_count;
c3e92a6c 135 MOVDref *drefs;
221e21b7 136 int dref_id;
0c5f76f7 137 int wrong_dts; ///< dts are wrong due to negative ctts
6cdbff63
DC
138 int width; ///< tkhd width
139 int height; ///< tkhd height
6cea494e
ZK
140} MOVStreamContext;
141
142typedef struct MOVContext {
6cea494e 143 AVFormatContext *fc;
5cd62665 144 int time_scale;
7e815047 145 int64_t duration; /* duration of the longest track */
6cea494e
ZK
146 int found_moov; /* when both 'moov' and 'mdat' sections has been found */
147 int found_mdat; /* we suppose we have enough data to read the file */
b595afaa 148 AVPaletteControl palette_control;
b60c0454
BC
149 DVDemuxContext *dv_demux;
150 AVFormatContext *dv_fctx;
152e9a43 151 int isom; /* 1 if file is ISO Media (mp4/3gp) */
61aedb0f
BC
152 MOVFragment fragment; ///< current fragment in moof atom
153 MOVTrackExt *trex_data;
154 unsigned trex_count;
51c15201 155 int itunes_metadata; ///< metadata are itunes style
6cea494e
ZK
156} MOVContext;
157
158
6cea494e
ZK
159/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
160
161/* those functions parse an atom */
162/* return code:
e3c4740f 163 0: continue to parse next atom
d9526386 164 <0: error occurred, exit
e3c4740f 165*/
6cea494e
ZK
166/* links atom IDs to parse functions */
167typedef struct MOVParseTableEntry {
0c1a9eda 168 uint32_t type;
c3e92a6c 169 int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
6cea494e
ZK
170} MOVParseTableEntry;
171
73d07c27
BC
172static const MOVParseTableEntry mov_default_parse_table[];
173
c3e92a6c 174static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
6cea494e 175{
b6a17df4 176 int64_t total_size = 0;
c3e92a6c 177 MOVAtom a;
6cea494e
ZK
178 int i;
179 int err = 0;
5ca1d879 180
5ca1d879 181 a.offset = atom.offset;
6cea494e 182
9ed83b0a 183 if (atom.size < 0)
fa22ca22 184 atom.size = INT64_MAX;
5ca1d879 185 while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
bb270c08 186 a.size = atom.size;
bcfe2ba0 187 a.type=0;
5ca1d879 188 if(atom.size >= 8) {
bb270c08 189 a.size = get_be32(pb);
5ca1d879 190 a.type = get_le32(pb);
6cea494e 191 }
bb270c08 192 total_size += 8;
5ca1d879 193 a.offset += 8;
29c90869
BC
194 dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n",
195 a.type, (char*)&a.type, a.size, atom.size, total_size);
5ca1d879 196 if (a.size == 1) { /* 64 bit extended size */
bb270c08 197 a.size = get_be64(pb) - 8;
5ca1d879
ZK
198 a.offset += 8;
199 total_size += 8;
6cea494e 200 }
bb270c08
DB
201 if (a.size == 0) {
202 a.size = atom.size - total_size;
203 if (a.size <= 8)
5cd62665 204 break;
bb270c08 205 }
bb270c08 206 a.size -= 8;
8622613d 207 if(a.size < 0)
568e18b1 208 break;
3d2308b0 209 a.size = FFMIN(a.size, atom.size - total_size);
115329f1 210
bcfe2ba0 211 for (i = 0; mov_default_parse_table[i].type != 0
73d07c27 212 && mov_default_parse_table[i].type != a.type; i++)
11979c46
BC
213 /* empty */;
214
73d07c27 215 if (mov_default_parse_table[i].type == 0) { /* skip leaf atoms data */
5ca1d879 216 url_fskip(pb, a.size);
bb270c08 217 } else {
bc5c918e 218 int64_t start_pos = url_ftell(pb);
5c72cad8 219 int64_t left;
03dc32f6 220 err = mov_default_parse_table[i].parse(c, pb, a);
db3ee6cc 221 if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
687f35f3 222 break;
5c72cad8
BC
223 left = a.size - url_ftell(pb) + start_pos;
224 if (left > 0) /* skip garbage at atom end */
225 url_fskip(pb, left);
bb270c08 226 }
6cea494e 227
bb270c08 228 a.offset += a.size;
5ca1d879 229 total_size += a.size;
6cea494e
ZK
230 }
231
9ce84dd8 232 if (!err && total_size < atom.size && atom.size < 0x7ffff)
5ca1d879 233 url_fskip(pb, atom.size - total_size);
5cd62665 234
6cea494e
ZK
235 return err;
236}
237
c3e92a6c 238static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
221e21b7
BC
239{
240 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
241 MOVStreamContext *sc = st->priv_data;
242 int entries, i, j;
243
244 get_be32(pb); // version + flags
245 entries = get_be32(pb);
246 if (entries >= UINT_MAX / sizeof(*sc->drefs))
247 return -1;
248 sc->drefs_count = entries;
249 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
250
251 for (i = 0; i < sc->drefs_count; i++) {
c3e92a6c 252 MOVDref *dref = &sc->drefs[i];
221e21b7 253 uint32_t size = get_be32(pb);
bc5c918e 254 int64_t next = url_ftell(pb) + size - 4;
221e21b7
BC
255
256 dref->type = get_le32(pb);
257 get_be32(pb); // version + flags
258 dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
259
260 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
261 /* macintosh alias record */
262 uint16_t volume_len, len;
263 char volume[28];
264 int16_t type;
265
266 url_fskip(pb, 10);
267
268 volume_len = get_byte(pb);
269 volume_len = FFMIN(volume_len, 27);
270 get_buffer(pb, volume, 27);
271 volume[volume_len] = 0;
272 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
273
274 url_fskip(pb, 112);
275
276 for (type = 0; type != -1 && url_ftell(pb) < next; ) {
277 type = get_be16(pb);
278 len = get_be16(pb);
279 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
280 if (len&1)
281 len += 1;
282 if (type == 2) { // absolute path
dbb37657 283 av_free(dref->path);
221e21b7 284 dref->path = av_mallocz(len+1);
2f4568e5
BC
285 if (!dref->path)
286 return AVERROR(ENOMEM);
221e21b7 287 get_buffer(pb, dref->path, len);
dbb7cbf2 288 if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
221e21b7
BC
289 len -= volume_len;
290 memmove(dref->path, dref->path+volume_len, len);
291 dref->path[len] = 0;
292 }
293 for (j = 0; j < len; j++)
294 if (dref->path[j] == ':')
295 dref->path[j] = '/';
296 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
297 } else
298 url_fskip(pb, len);
299 }
300 }
301 url_fseek(pb, next, SEEK_SET);
302 }
303 return 0;
304}
305
c3e92a6c 306static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
6cea494e 307{
5ca1d879 308 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
0c1a9eda 309 uint32_t type;
0c1a9eda 310 uint32_t ctype;
b6a17df4 311
6cea494e 312 get_byte(pb); /* version */
23f08617 313 get_be24(pb); /* flags */
6cea494e
ZK
314
315 /* component type */
316 ctype = get_le32(pb);
317 type = get_le32(pb); /* component subtype */
318
29c90869
BC
319 dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1],
320 ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
321 dprintf(c->fc, "stype= %c%c%c%c\n",
322 *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
152e9a43
BC
323 if(!ctype)
324 c->isom = 1;
37ffe34b 325 if (type == MKTAG('v','i','d','e'))
73920f07 326 st->codec->codec_type = CODEC_TYPE_VIDEO;
1c126b4f 327 else if(type == MKTAG('s','o','u','n'))
73920f07 328 st->codec->codec_type = CODEC_TYPE_AUDIO;
1c126b4f 329 else if(type == MKTAG('m','1','a',' '))
95a07973 330 st->codec->codec_id = CODEC_ID_MP2;
1c126b4f 331 else if(type == MKTAG('s','u','b','p')) {
8cb97693 332 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
8cb97693 333 }
6cea494e
ZK
334 get_be32(pb); /* component manufacture */
335 get_be32(pb); /* component flags */
336 get_be32(pb); /* component flags mask */
337
5ca1d879 338 if(atom.size <= 24)
6cea494e 339 return 0; /* nothing left to read */
5cd62665 340
3c13647a 341 url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
6cea494e
ZK
342 return 0;
343}
344
9e40addd 345static int mp4_read_descr_len(ByteIOContext *pb)
0e7eed09 346{
5cd62665 347 int len = 0;
5ca1d879
ZK
348 int count = 4;
349 while (count--) {
5cd62665 350 int c = get_byte(pb);
bb270c08
DB
351 len = (len << 7) | (c & 0x7f);
352 if (!(c & 0x80))
353 break;
0e7eed09
FB
354 }
355 return len;
356}
357
9e40addd 358static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
0e7eed09
FB
359{
360 int len;
361 *tag = get_byte(pb);
9e40addd 362 len = mp4_read_descr_len(pb);
318c5e05 363 dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
0e7eed09
FB
364 return len;
365}
366
0b07ac42
BC
367#define MP4ESDescrTag 0x03
368#define MP4DecConfigDescrTag 0x04
369#define MP4DecSpecificDescrTag 0x05
370
1dec3994
BC
371static const AVCodecTag mp4_audio_types[] = {
372 { CODEC_ID_MP3ON4, 29 }, /* old mp3on4 draft */
373 { CODEC_ID_MP3ON4, 32 }, /* layer 1 */
374 { CODEC_ID_MP3ON4, 33 }, /* layer 2 */
375 { CODEC_ID_MP3ON4, 34 }, /* layer 3 */
376 { CODEC_ID_NONE, 0 },
377};
378
c3e92a6c 379static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5cd62665 380{
5cd62665 381 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
5cd62665 382 int tag, len;
b6a17df4 383
5cd62665 384 get_be32(pb); /* version + flags */
9e40addd 385 len = mp4_read_descr(c, pb, &tag);
5cd62665 386 if (tag == MP4ESDescrTag) {
bb270c08
DB
387 get_be16(pb); /* ID */
388 get_byte(pb); /* priority */
5cd62665 389 } else
bb270c08 390 get_be16(pb); /* ID */
5cd62665 391
9e40addd 392 len = mp4_read_descr(c, pb, &tag);
5cd62665 393 if (tag == MP4DecConfigDescrTag) {
0b07ac42
BC
394 int object_type_id = get_byte(pb);
395 get_byte(pb); /* stream type */
396 get_be24(pb); /* buffer size db */
397 get_be32(pb); /* max bitrate */
398 get_be32(pb); /* avg bitrate */
399
400 st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
401 dprintf(c->fc, "esds object type id %d\n", object_type_id);
9e40addd 402 len = mp4_read_descr(c, pb, &tag);
bb270c08 403 if (tag == MP4DecSpecificDescrTag) {
318c5e05 404 dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
852859ff
BC
405 if((uint64_t)len > (1<<30))
406 return -1;
9a630c25 407 st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
b014dd76
BC
408 if (!st->codec->extradata)
409 return AVERROR(ENOMEM);
17871a02
BC
410 get_buffer(pb, st->codec->extradata, len);
411 st->codec->extradata_size = len;
1dec3994
BC
412 if (st->codec->codec_id == CODEC_ID_AAC) {
413 MPEG4AudioConfig cfg;
414 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
415 st->codec->extradata_size);
391872e4 416 if (cfg.chan_config > 7)
45a97d30
BC
417 return -1;
418 st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
419 if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
420 st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
421 else
422 st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
423 dprintf(c->fc, "mp4a config channels %d obj %d ext obj %d "
424 "sample rate %d ext sample rate %d\n", st->codec->channels,
425 cfg.object_type, cfg.ext_object_type,
426 cfg.sample_rate, cfg.ext_sample_rate);
1dec3994
BC
427 if (!(st->codec->codec_id = codec_get_id(mp4_audio_types,
428 cfg.object_type)))
429 st->codec->codec_id = CODEC_ID_AAC;
17871a02 430 }
bb270c08 431 }
5cd62665 432 }
5cd62665
ZK
433 return 0;
434}
435
6da54074
BC
436static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
437{
438 const int num = get_be32(pb);
439 const int den = get_be32(pb);
440 AVStream * const st = c->fc->streams[c->fc->nb_streams-1];
441 if (den != 0) {
f53ee312
BC
442 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
443 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
6da54074 444 av_log(c->fc, AV_LOG_WARNING,
f53ee312
BC
445 "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
446 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
6da54074
BC
447 st->sample_aspect_ratio.num = num;
448 st->sample_aspect_ratio.den = den;
449 }
450 return 0;
451}
452
5ca1d879 453/* this atom contains actual media data */
c3e92a6c 454static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
6cea494e 455{
5ca1d879
ZK
456 if(atom.size == 0) /* wrong one (MP4) */
457 return 0;
458 c->found_mdat=1;
5ca1d879
ZK
459 return 0; /* now go for moov */
460}
461
c3e92a6c 462static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
4ea28253
BC
463{
464 uint32_t type = get_le32(pb);
465
152e9a43
BC
466 if (type != MKTAG('q','t',' ',' '))
467 c->isom = 1;
a512446e 468 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
4ea28253
BC
469 get_be32(pb); /* minor version */
470 url_fskip(pb, atom.size - 8);
471 return 0;
472}
473
5ca1d879 474/* this atom should contain all header atoms */
c3e92a6c 475static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879 476{
9cf0419b
PI
477 if (mov_read_default(c, pb, atom) < 0)
478 return -1;
5ca1d879
ZK
479 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
480 /* so we don't parse the whole file if over a network */
481 c->found_moov=1;
5ca1d879
ZK
482 return 0; /* now go for mdat */
483}
484
c3e92a6c 485static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
61aedb0f
BC
486{
487 c->fragment.moof_offset = url_ftell(pb) - 8;
488 dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
489 return mov_read_default(c, pb, atom);
490}
5ca1d879 491
c3e92a6c 492static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879 493{
f444b977 494 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 495 MOVStreamContext *sc = st->priv_data;
f444b977 496 int version = get_byte(pb);
dce25564 497 unsigned lang;
5ca1d879 498
b9a87c4d 499 if (version > 1)
7e627332 500 return -1; /* unsupported */
5ca1d879 501
23f08617 502 get_be24(pb); /* flags */
f444b977
BC
503 if (version == 1) {
504 get_be64(pb);
505 get_be64(pb);
506 } else {
507 get_be32(pb); /* creation time */
508 get_be32(pb); /* modification time */
509 }
5ca1d879 510
f444b977
BC
511 sc->time_scale = get_be32(pb);
512 st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
5ca1d879 513
b9a87c4d 514 lang = get_be16(pb); /* language */
f444b977 515 ff_mov_lang_to_iso639(lang, st->language);
5ca1d879
ZK
516 get_be16(pb); /* quality */
517
518 return 0;
519}
520
c3e92a6c 521static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879 522{
1175561e 523 int version = get_byte(pb); /* version */
23f08617 524 get_be24(pb); /* flags */
5ca1d879 525
1175561e
BC
526 if (version == 1) {
527 get_be64(pb);
528 get_be64(pb);
529 } else {
530 get_be32(pb); /* creation time */
531 get_be32(pb); /* modification time */
532 }
5ca1d879 533 c->time_scale = get_be32(pb); /* time scale */
f8c18cd7
BC
534
535 dprintf(c->fc, "time scale = %i\n", c->time_scale);
536
1175561e 537 c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
5ca1d879
ZK
538 get_be32(pb); /* preferred scale */
539
540 get_be16(pb); /* preferred volume */
541
542 url_fskip(pb, 10); /* reserved */
543
544 url_fskip(pb, 36); /* display matrix */
545
546 get_be32(pb); /* preview time */
547 get_be32(pb); /* preview duration */
548 get_be32(pb); /* poster time */
549 get_be32(pb); /* selection time */
550 get_be32(pb); /* selection duration */
551 get_be32(pb); /* current time */
552 get_be32(pb); /* next track ID */
553
554 return 0;
555}
556
c3e92a6c 557static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
9ed83b0a
ZK
558{
559 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
560
568e18b1
MN
561 if((uint64_t)atom.size > (1<<30))
562 return -1;
115329f1 563
9ed83b0a
ZK
564 // currently SVQ3 decoder expect full STSD header - so let's fake it
565 // this should be fixed and just SMI header should be passed
01f4895c 566 av_free(st->codec->extradata);
b014dd76
BC
567 st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
568 if (!st->codec->extradata)
569 return AVERROR(ENOMEM);
17871a02
BC
570 st->codec->extradata_size = 0x5a + atom.size;
571 memcpy(st->codec->extradata, "SVQ3", 4); // fake
572 get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
573 dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
9ed83b0a
ZK
574 return 0;
575}
5ca1d879 576
c3e92a6c 577static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
de23f234
BC
578{
579 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
580 int little_endian = get_be16(pb);
581
fa50a027
BC
582 dprintf(c->fc, "enda %d\n", little_endian);
583 if (little_endian == 1) {
de23f234
BC
584 switch (st->codec->codec_id) {
585 case CODEC_ID_PCM_S24BE:
586 st->codec->codec_id = CODEC_ID_PCM_S24LE;
587 break;
588 case CODEC_ID_PCM_S32BE:
589 st->codec->codec_id = CODEC_ID_PCM_S32LE;
590 break;
a1ef2c4b
PR
591 case CODEC_ID_PCM_F32BE:
592 st->codec->codec_id = CODEC_ID_PCM_F32LE;
593 break;
594 case CODEC_ID_PCM_F64BE:
595 st->codec->codec_id = CODEC_ID_PCM_F64LE;
596 break;
de23f234
BC
597 default:
598 break;
599 }
600 }
601 return 0;
602}
603
014a5102 604/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
c3e92a6c 605static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
de23f234 606{
27134437
BC
607 AVStream *st;
608 uint64_t size;
68bc33fa 609 uint8_t *buf;
27134437
BC
610
611 if (c->fc->nb_streams < 1) // will happen with jp2 files
612 return 0;
613 st= c->fc->streams[c->fc->nb_streams-1];
614 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
68bc33fa 615 if(size > INT_MAX || (uint64_t)atom.size > INT_MAX)
014a5102 616 return -1;
68bc33fa
BC
617 buf= av_realloc(st->codec->extradata, size);
618 if(!buf)
619 return -1;
620 st->codec->extradata= buf;
621 buf+= st->codec->extradata_size;
622 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
623 AV_WB32( buf , atom.size + 8);
624 AV_WL32( buf + 4, atom.type);
625 get_buffer(pb, buf + 8, atom.size);
de23f234
BC
626 return 0;
627}
628
c3e92a6c 629static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
d9b1c197
RT
630{
631 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
632
633 if((uint64_t)atom.size > (1<<30))
634 return -1;
115329f1 635
3840147e
MN
636 if (st->codec->codec_id == CODEC_ID_QDM2) {
637 // pass all frma atom to codec, needed at least for QDM2
638 av_free(st->codec->extradata);
b014dd76
BC
639 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
640 if (!st->codec->extradata)
641 return AVERROR(ENOMEM);
3840147e 642 st->codec->extradata_size = atom.size;
b014dd76 643 get_buffer(pb, st->codec->extradata, atom.size);
3840147e 644 } else if (atom.size > 8) { /* to read frma, esds atoms */
9cf0419b
PI
645 if (mov_read_default(c, pb, atom) < 0)
646 return -1;
5c72cad8 647 } else
bb270c08 648 url_fskip(pb, atom.size);
d9b1c197
RT
649 return 0;
650}
651
bde24601
BC
652/**
653 * This function reads atom content and puts data in extradata without tag
654 * nor size unlike mov_read_extradata.
655 */
c3e92a6c 656static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
169eb021
MM
657{
658 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
659
568e18b1
MN
660 if((uint64_t)atom.size > (1<<30))
661 return -1;
662
01f4895c 663 av_free(st->codec->extradata);
b014dd76
BC
664 st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
665 if (!st->codec->extradata)
666 return AVERROR(ENOMEM);
01f4895c 667 st->codec->extradata_size = atom.size;
b014dd76 668 get_buffer(pb, st->codec->extradata, atom.size);
169eb021
MM
669 return 0;
670}
671
c3e92a6c 672static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879
ZK
673{
674 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 675 MOVStreamContext *sc = st->priv_data;
568e18b1 676 unsigned int i, entries;
5ca1d879 677
5ca1d879 678 get_byte(pb); /* version */
23f08617 679 get_be24(pb); /* flags */
5ca1d879
ZK
680
681 entries = get_be32(pb);
115329f1 682
568e18b1
MN
683 if(entries >= UINT_MAX/sizeof(int64_t))
684 return -1;
115329f1 685
5ca1d879 686 sc->chunk_count = entries;
9a630c25 687 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
5ca1d879
ZK
688 if (!sc->chunk_offsets)
689 return -1;
37ffe34b
BC
690 if (atom.type == MKTAG('s','t','c','o'))
691 for(i=0; i<entries; i++)
5ca1d879 692 sc->chunk_offsets[i] = get_be32(pb);
37ffe34b
BC
693 else if (atom.type == MKTAG('c','o','6','4'))
694 for(i=0; i<entries; i++)
5ca1d879 695 sc->chunk_offsets[i] = get_be64(pb);
37ffe34b 696 else
5ca1d879 697 return -1;
115329f1 698
5ca1d879
ZK
699 return 0;
700}
701
2288834f
BC
702/**
703 * Compute codec id for 'lpcm' tag.
704 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
705 */
fb65d2ca 706static enum CodecID mov_get_lpcm_codec_id(int bps, int flags)
2288834f
BC
707{
708 if (flags & 1) { // floating point
709 if (flags & 2) { // big endian
710 if (bps == 32) return CODEC_ID_PCM_F32BE;
7e4b3fb8 711 else if (bps == 64) return CODEC_ID_PCM_F64BE;
2288834f 712 } else {
7e4b3fb8
BC
713 if (bps == 32) return CODEC_ID_PCM_F32LE;
714 else if (bps == 64) return CODEC_ID_PCM_F64LE;
2288834f
BC
715 }
716 } else {
717 if (flags & 2) {
718 if (bps == 8)
719 // signed integer
720 if (flags & 4) return CODEC_ID_PCM_S8;
721 else return CODEC_ID_PCM_U8;
722 else if (bps == 16) return CODEC_ID_PCM_S16BE;
723 else if (bps == 24) return CODEC_ID_PCM_S24BE;
724 else if (bps == 32) return CODEC_ID_PCM_S32BE;
725 } else {
726 if (bps == 8)
727 if (flags & 4) return CODEC_ID_PCM_S8;
728 else return CODEC_ID_PCM_U8;
021b8ae3 729 else if (bps == 16) return CODEC_ID_PCM_S16LE;
2288834f
BC
730 else if (bps == 24) return CODEC_ID_PCM_S24LE;
731 else if (bps == 32) return CODEC_ID_PCM_S32LE;
732 }
733 }
fb65d2ca 734 return CODEC_ID_NONE;
2288834f
BC
735}
736
c3e92a6c 737static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879
ZK
738{
739 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 740 MOVStreamContext *sc = st->priv_data;
baf9fb32 741 int j, entries, pseudo_stream_id;
b595afaa 742
6cea494e 743 get_byte(pb); /* version */
23f08617 744 get_be24(pb); /* flags */
6cea494e 745
6cea494e
ZK
746 entries = get_be32(pb);
747
117a9190
BC
748 for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
749 //Parsing Sample description table
b6a17df4 750 enum CodecID id;
221e21b7 751 int dref_id;
c3e92a6c 752 MOVAtom a = { 0, 0, 0 };
bc5c918e 753 int64_t start_pos = url_ftell(pb);
bb270c08 754 int size = get_be32(pb); /* size */
baf9fb32 755 uint32_t format = get_le32(pb); /* data format */
5cd62665 756
6cea494e
ZK
757 get_be32(pb); /* reserved */
758 get_be16(pb); /* reserved */
221e21b7 759 dref_id = get_be16(pb);
0e7eed09 760
744a9c75 761 if (st->codec->codec_tag &&
978677a7 762 st->codec->codec_tag != format &&
744a9c75 763 (c->fc->video_codec_id ? codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
1c126b4f 764 : st->codec->codec_tag != MKTAG('j','p','e','g'))
744a9c75 765 ){
7ce68923
DB
766 /* Multiple fourcc, we skip JPEG. This is not correct, we should
767 * export it as a separate AVStream but this needs a few changes
768 * in the MOV demuxer, patch welcome. */
285a3da9 769 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
e7cc4b52
BC
770 url_fskip(pb, size - (url_ftell(pb) - start_pos));
771 continue;
772 }
978677a7 773 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
221e21b7 774 sc->dref_id= dref_id;
e7cc4b52 775
bb270c08 776 st->codec->codec_tag = format;
1e5f5e3b 777 id = codec_get_id(codec_movaudio_tags, format);
1c126b4f 778 if (id<=0 && (format&0xFFFF) == 'm'+('s'<<8))
bca7db35
MN
779 id = codec_get_id(codec_wav_tags, bswap_32(format)&0xFFFF);
780
48855b26 781 if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
99487f42 782 st->codec->codec_type = CODEC_TYPE_AUDIO;
48855b26 783 } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
1c126b4f 784 format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
1e5f5e3b 785 id = codec_get_id(codec_movvideo_tags, format);
99487f42 786 if (id <= 0)
de23f234 787 id = codec_get_id(codec_bmp_tags, format);
99487f42
BC
788 if (id > 0)
789 st->codec->codec_type = CODEC_TYPE_VIDEO;
1e3c9307
MN
790 else if(st->codec->codec_type == CODEC_TYPE_DATA){
791 id = codec_get_id(ff_codec_movsubtitle_tags, format);
792 if(id > 0)
793 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
794 }
99487f42
BC
795 }
796
29c90869
BC
797 dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
798 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
799 (format >> 24) & 0xff, st->codec->codec_type);
a20da52c 800
99487f42 801 if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
baf9fb32
BC
802 uint8_t codec_name[32];
803 unsigned int color_depth;
804 int color_greyscale;
baf9fb32 805
01f4895c 806 st->codec->codec_id = id;
6cea494e
ZK
807 get_be16(pb); /* version */
808 get_be16(pb); /* revision level */
809 get_be32(pb); /* vendor */
810 get_be32(pb); /* temporal quality */
94472c1d 811 get_be32(pb); /* spatial quality */
3cb4ee51
BC
812
813 st->codec->width = get_be16(pb); /* width */
814 st->codec->height = get_be16(pb); /* height */
815
6cea494e
ZK
816 get_be32(pb); /* horiz resolution */
817 get_be32(pb); /* vert resolution */
818 get_be32(pb); /* data size, always 0 */
43612ffe 819 get_be16(pb); /* frames per samples */
f8c18cd7 820
eb034aca 821 get_buffer(pb, codec_name, 32); /* codec name, pascal string */
576f1445
BC
822 if (codec_name[0] <= 31) {
823 memcpy(st->codec->codec_name, &codec_name[1],codec_name[0]);
824 st->codec->codec_name[codec_name[0]] = 0;
825 }
5cd62665 826
dd1c8f3e 827 st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
01f4895c 828 st->codec->color_table_id = get_be16(pb); /* colortable id */
0de2157f 829 dprintf(c->fc, "depth %d, ctab id %d\n",
dd1c8f3e 830 st->codec->bits_per_coded_sample, st->codec->color_table_id);
b595afaa 831 /* figure out the palette situation */
dd1c8f3e
LA
832 color_depth = st->codec->bits_per_coded_sample & 0x1F;
833 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
b595afaa
MM
834
835 /* if the depth is 2, 4, or 8 bpp, file is palettized */
115329f1 836 if ((color_depth == 2) || (color_depth == 4) ||
b595afaa 837 (color_depth == 8)) {
47e1d7ef
BC
838 /* for palette traversal */
839 unsigned int color_start, color_count, color_end;
840 unsigned char r, g, b;
841
b595afaa 842 if (color_greyscale) {
47e1d7ef 843 int color_index, color_dec;
b595afaa 844 /* compute the greyscale palette */
dd1c8f3e 845 st->codec->bits_per_coded_sample = color_depth;
b595afaa
MM
846 color_count = 1 << color_depth;
847 color_index = 255;
848 color_dec = 256 / (color_count - 1);
849 for (j = 0; j < color_count; j++) {
850 r = g = b = color_index;
851 c->palette_control.palette[j] =
852 (r << 16) | (g << 8) | (b);
853 color_index -= color_dec;
854 if (color_index < 0)
855 color_index = 0;
856 }
71f36235 857 } else if (st->codec->color_table_id) {
47e1d7ef 858 const uint8_t *color_table;
b595afaa
MM
859 /* if flag bit 3 is set, use the default palette */
860 color_count = 1 << color_depth;
861 if (color_depth == 2)
a90466f7 862 color_table = ff_qt_default_palette_4;
b595afaa 863 else if (color_depth == 4)
a90466f7 864 color_table = ff_qt_default_palette_16;
b595afaa 865 else
a90466f7 866 color_table = ff_qt_default_palette_256;
b595afaa
MM
867
868 for (j = 0; j < color_count; j++) {
869 r = color_table[j * 4 + 0];
870 g = color_table[j * 4 + 1];
871 b = color_table[j * 4 + 2];
872 c->palette_control.palette[j] =
873 (r << 16) | (g << 8) | (b);
874 }
b595afaa 875 } else {
b595afaa
MM
876 /* load the palette from the file */
877 color_start = get_be32(pb);
878 color_count = get_be16(pb);
879 color_end = get_be16(pb);
8b35bd80
MM
880 if ((color_start <= 255) &&
881 (color_end <= 255)) {
9de2919c
MM
882 for (j = color_start; j <= color_end; j++) {
883 /* each R, G, or B component is 16 bits;
884 * only use the top 8 bits; skip alpha bytes
885 * up front */
886 get_byte(pb);
887 get_byte(pb);
888 r = get_byte(pb);
889 get_byte(pb);
890 g = get_byte(pb);
891 get_byte(pb);
892 b = get_byte(pb);
893 get_byte(pb);
894 c->palette_control.palette[j] =
895 (r << 16) | (g << 8) | (b);
8b35bd80 896 }
b595afaa
MM
897 }
898 }
01f4895c
MN
899 st->codec->palctrl = &c->palette_control;
900 st->codec->palctrl->palette_changed = 1;
b595afaa 901 } else
01f4895c 902 st->codec->palctrl = NULL;
de23f234 903 } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
aaef2bb3 904 int bits_per_sample, flags;
de23f234 905 uint16_t version = get_be16(pb);
b595afaa 906
99487f42 907 st->codec->codec_id = id;
de23f234
BC
908 get_be16(pb); /* revision level */
909 get_be32(pb); /* vendor */
6d6d7970 910
de23f234 911 st->codec->channels = get_be16(pb); /* channel count */
318c5e05 912 dprintf(c->fc, "audio channels %d\n", st->codec->channels);
dd1c8f3e 913 st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
de23f234 914
77c75437 915 sc->audio_cid = get_be16(pb);
de23f234
BC
916 get_be16(pb); /* packet size = 0 */
917
918 st->codec->sample_rate = ((get_be32(pb) >> 16));
919
5ef3ad59
BC
920 //Read QT version 1 fields. In version 0 these do not exist.
921 dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
922 if(!c->isom) {
923 if(version==1) {
924 sc->samples_per_frame = get_be32(pb);
925 get_be32(pb); /* bytes per packet */
926 sc->bytes_per_frame = get_be32(pb);
927 get_be32(pb); /* bytes per sample */
928 } else if(version==2) {
929 get_be32(pb); /* sizeof struct only */
930 st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
931 st->codec->channels = get_be32(pb);
932 get_be32(pb); /* always 0x7F000000 */
dd1c8f3e 933 st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
aaef2bb3 934 flags = get_be32(pb); /* lcpm format specific flag */
9184d53a
BC
935 sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
936 sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
2288834f 937 if (format == MKTAG('l','p','c','m'))
dd1c8f3e 938 st->codec->codec_id = mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
5ef3ad59
BC
939 }
940 }
941
de23f234 942 switch (st->codec->codec_id) {
b72708f8
BC
943 case CODEC_ID_PCM_S8:
944 case CODEC_ID_PCM_U8:
dd1c8f3e 945 if (st->codec->bits_per_coded_sample == 16)
b72708f8
BC
946 st->codec->codec_id = CODEC_ID_PCM_S16BE;
947 break;
0dd39bfe 948 case CODEC_ID_PCM_S16LE:
de23f234 949 case CODEC_ID_PCM_S16BE:
dd1c8f3e 950 if (st->codec->bits_per_coded_sample == 8)
de23f234 951 st->codec->codec_id = CODEC_ID_PCM_S8;
dd1c8f3e 952 else if (st->codec->bits_per_coded_sample == 24)
6b477e1b
BC
953 st->codec->codec_id =
954 st->codec->codec_id == CODEC_ID_PCM_S16BE ?
955 CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
de23f234 956 break;
53152765
BC
957 /* set values for old format before stsd version 1 appeared */
958 case CODEC_ID_MACE3:
959 sc->samples_per_frame = 6;
960 sc->bytes_per_frame = 2*st->codec->channels;
961 break;
962 case CODEC_ID_MACE6:
963 sc->samples_per_frame = 6;
964 sc->bytes_per_frame = 1*st->codec->channels;
965 break;
966 case CODEC_ID_ADPCM_IMA_QT:
967 sc->samples_per_frame = 64;
968 sc->bytes_per_frame = 34*st->codec->channels;
969 break;
cc326d2b
BC
970 case CODEC_ID_GSM:
971 sc->samples_per_frame = 160;
972 sc->bytes_per_frame = 33;
973 break;
de23f234
BC
974 default:
975 break;
6d6d7970 976 }
14342fd5 977
9770089d
BC
978 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
979 if (bits_per_sample) {
dd1c8f3e 980 st->codec->bits_per_coded_sample = bits_per_sample;
9770089d
BC
981 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
982 }
cc8d87b7 983 } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
58e9f2ed
DC
984 // ttxt stsd contains display flags, justification, background
985 // color, fonts, and default styles, so fake an atom to read it
986 MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
987 mov_read_glbl(c, pb, fake_atom);
cc8d87b7 988 st->codec->codec_id= id;
6cdbff63
DC
989 st->codec->width = sc->width;
990 st->codec->height = sc->height;
de23f234
BC
991 } else {
992 /* other codec type, just skip (rtp, mp4s, tmcd ...) */
993 url_fskip(pb, size - (url_ftell(pb) - start_pos));
6cea494e 994 }
de23f234
BC
995 /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
996 a.size = size - (url_ftell(pb) - start_pos);
9cf0419b
PI
997 if (a.size > 8) {
998 if (mov_read_default(c, pb, a) < 0)
999 return -1;
1000 } else if (a.size > 0)
de23f234 1001 url_fskip(pb, a.size);
6cea494e 1002 }
115329f1 1003
60f5c96e 1004 if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
302c389e 1005 st->codec->sample_rate= sc->time_scale;
e31bd3e3 1006
d00f8e17 1007 /* special codec parameters handling */
e31bd3e3 1008 switch (st->codec->codec_id) {
b250f9c6 1009#if CONFIG_DV_DEMUXER
b60c0454 1010 case CODEC_ID_DVAUDIO:
8e2fd8e1 1011 c->dv_fctx = avformat_alloc_context();
b60c0454
BC
1012 c->dv_demux = dv_init_demux(c->dv_fctx);
1013 if (!c->dv_demux) {
1014 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1015 return -1;
1016 }
1017 sc->dv_audio_container = 1;
1018 st->codec->codec_id = CODEC_ID_PCM_S16LE;
1019 break;
989ac5a6 1020#endif
b95319a2 1021 /* no ifdef since parameters are always those */
3f78a3a9 1022 case CODEC_ID_QCELP:
73b458e3 1023 st->codec->frame_size= 160;
061f407e 1024 st->codec->channels= 1; /* really needed */
73b458e3 1025 break;
b95319a2 1026 case CODEC_ID_AMR_NB:
aeb62788 1027 case CODEC_ID_AMR_WB:
cfb5a2ab 1028 st->codec->frame_size= sc->samples_per_frame;
b95319a2 1029 st->codec->channels= 1; /* really needed */
f06188d5 1030 /* force sample rate for amr, stsd in 3gp does not store sample rate */
5b9ce252 1031 if (st->codec->codec_id == CODEC_ID_AMR_NB)
f06188d5 1032 st->codec->sample_rate = 8000;
5b9ce252
BC
1033 else if (st->codec->codec_id == CODEC_ID_AMR_WB)
1034 st->codec->sample_rate = 16000;
b95319a2 1035 break;
a41104f8 1036 case CODEC_ID_MP2:
c59f24e6 1037 case CODEC_ID_MP3:
95a07973 1038 st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
57004ff1 1039 st->need_parsing = AVSTREAM_PARSE_FULL;
a41104f8 1040 break;
5610f20e 1041 case CODEC_ID_GSM:
74e9b9ae
BC
1042 case CODEC_ID_ADPCM_MS:
1043 case CODEC_ID_ADPCM_IMA_WAV:
1044 st->codec->block_align = sc->bytes_per_frame;
1045 break;
be511925 1046 case CODEC_ID_ALAC:
a1532824 1047 if (st->codec->extradata_size == 36) {
364df7b7
BC
1048 st->codec->frame_size = AV_RB32(st->codec->extradata+12);
1049 st->codec->channels = AV_RB8 (st->codec->extradata+21);
a1532824 1050 }
be511925 1051 break;
e31bd3e3
BC
1052 default:
1053 break;
1054 }
5cd62665 1055
6cea494e
ZK
1056 return 0;
1057}
1058
c3e92a6c 1059static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
6cea494e 1060{
5ca1d879 1061 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1062 MOVStreamContext *sc = st->priv_data;
568e18b1 1063 unsigned int i, entries;
b6a17df4 1064
6cea494e 1065 get_byte(pb); /* version */
23f08617 1066 get_be24(pb); /* flags */
6cea494e
ZK
1067
1068 entries = get_be32(pb);
115329f1 1069
04e06cfa 1070 if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
568e18b1 1071 return -1;
115329f1 1072
f8c18cd7
BC
1073 dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1074
04e06cfa
BC
1075 sc->stsc_count = entries;
1076 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1077 if (!sc->stsc_data)
b6a17df4 1078 return -1;
6cea494e 1079 for(i=0; i<entries; i++) {
04e06cfa
BC
1080 sc->stsc_data[i].first = get_be32(pb);
1081 sc->stsc_data[i].count = get_be32(pb);
1082 sc->stsc_data[i].id = get_be32(pb);
6cea494e
ZK
1083 }
1084 return 0;
1085}
1086
c3e92a6c 1087static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
247d56f5
BB
1088{
1089 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1090 MOVStreamContext *sc = st->priv_data;
568e18b1 1091 unsigned int i, entries;
247d56f5 1092
247d56f5 1093 get_byte(pb); /* version */
23f08617 1094 get_be24(pb); /* flags */
247d56f5
BB
1095
1096 entries = get_be32(pb);
115329f1 1097
1c02d96f 1098 if(entries >= UINT_MAX / sizeof(int))
568e18b1 1099 return -1;
115329f1 1100
247d56f5 1101 sc->keyframe_count = entries;
f8c18cd7
BC
1102
1103 dprintf(c->fc, "keyframe_count = %d\n", sc->keyframe_count);
1104
1c02d96f 1105 sc->keyframes = av_malloc(entries * sizeof(int));
247d56f5
BB
1106 if (!sc->keyframes)
1107 return -1;
1108 for(i=0; i<entries; i++) {
1109 sc->keyframes[i] = get_be32(pb);
f8c18cd7 1110 //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
247d56f5
BB
1111 }
1112 return 0;
1113}
1114
c3e92a6c 1115static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
6cea494e 1116{
5ca1d879 1117 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1118 MOVStreamContext *sc = st->priv_data;
b72708f8 1119 unsigned int i, entries, sample_size;
b6a17df4 1120
6cea494e 1121 get_byte(pb); /* version */
23f08617 1122 get_be24(pb); /* flags */
5cd62665 1123
b72708f8
BC
1124 sample_size = get_be32(pb);
1125 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1126 sc->sample_size = sample_size;
6cea494e 1127 entries = get_be32(pb);
1c02d96f 1128 if(entries >= UINT_MAX / sizeof(int))
568e18b1
MN
1129 return -1;
1130
6cea494e 1131 sc->sample_count = entries;
b72708f8
BC
1132 if (sample_size)
1133 return 0;
1134
f8c18cd7
BC
1135 dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, sc->sample_count);
1136
1c02d96f 1137 sc->sample_sizes = av_malloc(entries * sizeof(int));
b6a17df4
ZK
1138 if (!sc->sample_sizes)
1139 return -1;
eb61405a 1140 for(i=0; i<entries; i++)
6cea494e 1141 sc->sample_sizes[i] = get_be32(pb);
6cea494e
ZK
1142 return 0;
1143}
1144
c3e92a6c 1145static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
0147f198 1146{
5ca1d879 1147 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1148 MOVStreamContext *sc = st->priv_data;
568e18b1 1149 unsigned int i, entries;
d957696f
MN
1150 int64_t duration=0;
1151 int64_t total_sample_count=0;
b6a17df4 1152
0147f198 1153 get_byte(pb); /* version */
23f08617 1154 get_be24(pb); /* flags */
0147f198 1155 entries = get_be32(pb);
c3e92a6c 1156 if(entries >= UINT_MAX / sizeof(*sc->stts_data))
568e18b1 1157 return -1;
891f64b3 1158
cd7352d5 1159 sc->stts_count = entries;
c3e92a6c 1160 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
c1da59fa
BC
1161 if (!sc->stts_data)
1162 return -1;
f8c18cd7 1163 dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
cd7352d5
MN
1164
1165 sc->time_rate=0;
1166
0147f198 1167 for(i=0; i<entries; i++) {
cd461d48
MN
1168 int sample_duration;
1169 int sample_count;
0147f198 1170
891f64b3 1171 sample_count=get_be32(pb);
0147f198 1172 sample_duration = get_be32(pb);
cd7352d5
MN
1173 sc->stts_data[i].count= sample_count;
1174 sc->stts_data[i].duration= sample_duration;
1175
9ce6c138 1176 sc->time_rate= av_gcd(sc->time_rate, sample_duration);
961e0ccd 1177
318c5e05 1178 dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
4e5ef14f 1179
7e815047 1180 duration+=(int64_t)sample_duration*sample_count;
891f64b3 1181 total_sample_count+=sample_count;
891f64b3 1182 }
1183
961e0ccd
MN
1184 st->nb_frames= total_sample_count;
1185 if(duration)
1186 st->duration= duration;
0147f198
FR
1187 return 0;
1188}
1189
c3e92a6c 1190static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
c4ac052b 1191{
70a61ed4 1192 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
e4141433 1193 MOVStreamContext *sc = st->priv_data;
c4ac052b
MN
1194 unsigned int i, entries;
1195
c4ac052b 1196 get_byte(pb); /* version */
23f08617 1197 get_be24(pb); /* flags */
c4ac052b 1198 entries = get_be32(pb);
c3e92a6c 1199 if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
c4ac052b
MN
1200 return -1;
1201
70a61ed4 1202 sc->ctts_count = entries;
c3e92a6c 1203 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
c1da59fa
BC
1204 if (!sc->ctts_data)
1205 return -1;
318c5e05 1206 dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
4e5ef14f 1207
c4ac052b 1208 for(i=0; i<entries; i++) {
70a61ed4
MN
1209 int count =get_be32(pb);
1210 int duration =get_be32(pb);
1211
b0519015 1212 if (duration < 0) {
0c5f76f7
BC
1213 sc->wrong_dts = 1;
1214 st->codec->has_b_frames = 1;
b0519015 1215 }
70a61ed4
MN
1216 sc->ctts_data[i].count = count;
1217 sc->ctts_data[i].duration= duration;
1218
9ce6c138 1219 sc->time_rate= av_gcd(sc->time_rate, FFABS(duration));
c4ac052b
MN
1220 }
1221 return 0;
1222}
1223
1e77810d
BC
1224static void mov_build_index(MOVContext *mov, AVStream *st)
1225{
1226 MOVStreamContext *sc = st->priv_data;
bc5c918e 1227 int64_t current_offset;
1e77810d
BC
1228 int64_t current_dts = 0;
1229 unsigned int stts_index = 0;
1230 unsigned int stsc_index = 0;
1231 unsigned int stss_index = 0;
1232 unsigned int i, j;
1233
baf2ffd3
BC
1234 /* adjust first dts according to edit list */
1235 if (sc->time_offset) {
1236 assert(sc->time_offset % sc->time_rate == 0);
1237 current_dts = - (sc->time_offset / sc->time_rate);
1238 }
1239
d3bc61ac
BC
1240 /* only use old uncompressed audio chunk demuxing when stts specifies it */
1241 if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
1242 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
1e77810d
BC
1243 unsigned int current_sample = 0;
1244 unsigned int stts_sample = 0;
1245 unsigned int keyframe, sample_size;
1246 unsigned int distance = 0;
1247 int key_off = sc->keyframes && sc->keyframes[0] == 1;
1248
1249 st->nb_frames = sc->sample_count;
1250 for (i = 0; i < sc->chunk_count; i++) {
1251 current_offset = sc->chunk_offsets[i];
04e06cfa
BC
1252 if (stsc_index + 1 < sc->stsc_count &&
1253 i + 1 == sc->stsc_data[stsc_index + 1].first)
1e77810d 1254 stsc_index++;
04e06cfa 1255 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
1e77810d
BC
1256 if (current_sample >= sc->sample_count) {
1257 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
1258 goto out;
1259 }
1260 keyframe = !sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index];
1261 if (keyframe) {
1262 distance = 0;
1263 if (stss_index + 1 < sc->keyframe_count)
1264 stss_index++;
1265 }
1266 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
978677a7 1267 if(sc->pseudo_stream_id == -1 ||
04e06cfa 1268 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
1e77810d
BC
1269 av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
1270 keyframe ? AVINDEX_KEYFRAME : 0);
585dac65
BC
1271 dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1272 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
1273 current_offset, current_dts, sample_size, distance, keyframe);
1274 }
1e77810d
BC
1275 current_offset += sample_size;
1276 assert(sc->stts_data[stts_index].duration % sc->time_rate == 0);
1277 current_dts += sc->stts_data[stts_index].duration / sc->time_rate;
1278 distance++;
1279 stts_sample++;
1280 current_sample++;
1281 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
1282 stts_sample = 0;
1283 stts_index++;
1284 }
1285 }
1286 }
1287 } else { /* read whole chunk */
1288 unsigned int chunk_samples, chunk_size, chunk_duration;
1289 unsigned int frames = 1;
1290 for (i = 0; i < sc->chunk_count; i++) {
1291 current_offset = sc->chunk_offsets[i];
04e06cfa
BC
1292 if (stsc_index + 1 < sc->stsc_count &&
1293 i + 1 == sc->stsc_data[stsc_index + 1].first)
1e77810d 1294 stsc_index++;
04e06cfa 1295 chunk_samples = sc->stsc_data[stsc_index].count;
1e77810d
BC
1296 /* get chunk size, beware of alaw/ulaw/mace */
1297 if (sc->samples_per_frame > 0 &&
1298 (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
3b8c000d 1299 if (sc->samples_per_frame < 160)
1e77810d
BC
1300 chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
1301 else {
1302 chunk_size = sc->bytes_per_frame;
1303 frames = chunk_samples / sc->samples_per_frame;
1304 chunk_samples = sc->samples_per_frame;
1305 }
48a55290 1306 } else
1e77810d 1307 chunk_size = chunk_samples * sc->sample_size;
1e77810d
BC
1308 for (j = 0; j < frames; j++) {
1309 av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
1310 /* get chunk duration */
1311 chunk_duration = 0;
1312 while (chunk_samples > 0) {
1313 if (chunk_samples < sc->stts_data[stts_index].count) {
1314 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1315 sc->stts_data[stts_index].count -= chunk_samples;
1316 break;
1317 } else {
1318 chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
1319 chunk_samples -= sc->stts_data[stts_index].count;
1320 if (stts_index + 1 < sc->stts_count)
1321 stts_index++;
1322 }
1323 }
1324 current_offset += sc->bytes_per_frame;
117a9190
BC
1325 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
1326 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
1327 chunk_size, chunk_duration);
1e77810d
BC
1328 assert(chunk_duration % sc->time_rate == 0);
1329 current_dts += chunk_duration / sc->time_rate;
1330 }
1331 }
1332 }
1333 out:
1334 /* adjust sample count to avindex entries */
1335 sc->sample_count = st->nb_index_entries;
1336}
bd991df2 1337
c3e92a6c 1338static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879
ZK
1339{
1340 AVStream *st;
1341 MOVStreamContext *sc;
bd991df2 1342 int ret;
5ca1d879 1343
5ca1d879 1344 st = av_new_stream(c->fc, c->fc->nb_streams);
3efe8848 1345 if (!st) return AVERROR(ENOMEM);
9a630c25 1346 sc = av_mallocz(sizeof(MOVStreamContext));
2922cbdb 1347 if (!sc) return AVERROR(ENOMEM);
5ca1d879 1348
5ca1d879 1349 st->priv_data = sc;
05edc1a7 1350 st->codec->codec_type = CODEC_TYPE_DATA;
25c4950e 1351 st->start_time = 0; /* XXX: check */
64d50fa5 1352 sc->ffindex = st->index;
5ca1d879 1353
bd991df2
BC
1354 if ((ret = mov_read_default(c, pb, atom)) < 0)
1355 return ret;
1356
1357 /* sanity checks */
04e06cfa 1358 if(sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
5e788d58 1359 (!sc->sample_size && !sc->sample_count))){
6282c5f4
BC
1360 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
1361 st->index);
bd991df2
BC
1362 sc->sample_count = 0; //ignore track
1363 return 0;
1364 }
1365 if(!sc->time_rate)
1366 sc->time_rate=1;
1367 if(!sc->time_scale)
1368 sc->time_scale= c->time_scale;
1369 av_set_pts_info(st, 64, sc->time_rate, sc->time_scale);
1370
3f78a3a9
BC
1371 if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
1372 !st->codec->frame_size && sc->stts_count == 1)
bd991df2
BC
1373 st->codec->frame_size = av_rescale(sc->time_rate, st->codec->sample_rate, sc->time_scale);
1374
1375 if(st->duration != AV_NOPTS_VALUE){
1376 assert(st->duration % sc->time_rate == 0);
1377 st->duration /= sc->time_rate;
1378 }
64d50fa5 1379
bd991df2
BC
1380 mov_build_index(c, st);
1381
1382 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
1383 if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
9e81a0ce
BC
1384 av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
1385 st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
bd991df2
BC
1386 } else
1387 sc->pb = c->fc->pb;
1388
1389 switch (st->codec->codec_id) {
b250f9c6 1390#if CONFIG_H261_DECODER
bd991df2
BC
1391 case CODEC_ID_H261:
1392#endif
b250f9c6 1393#if CONFIG_H263_DECODER
bd991df2
BC
1394 case CODEC_ID_H263:
1395#endif
b250f9c6 1396#if CONFIG_MPEG4_DECODER
bd991df2
BC
1397 case CODEC_ID_MPEG4:
1398#endif
1399 st->codec->width= 0; /* let decoder init width/height */
1400 st->codec->height= 0;
1401 break;
bd991df2 1402 }
f9900374
BC
1403
1404 /* Do not need those anymore. */
1405 av_freep(&sc->chunk_offsets);
04e06cfa 1406 av_freep(&sc->stsc_data);
f9900374
BC
1407 av_freep(&sc->sample_sizes);
1408 av_freep(&sc->keyframes);
1409 av_freep(&sc->stts_data);
1410
bd991df2 1411 return 0;
5ca1d879
ZK
1412}
1413
c3e92a6c 1414static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
51c15201
BC
1415{
1416 int ret;
1417 c->itunes_metadata = 1;
1418 ret = mov_read_default(c, pb, atom);
1419 c->itunes_metadata = 0;
1420 return ret;
1421}
1422
c3e92a6c 1423static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
51c15201 1424{
12e416c8
BC
1425 url_fskip(pb, 4); // version + flags
1426 atom.size -= 4;
51c15201
BC
1427 return mov_read_default(c, pb, atom);
1428}
1429
c3e92a6c 1430static int mov_read_trkn(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
51c15201
BC
1431{
1432 get_be32(pb); // type
1433 get_be32(pb); // unknown
1434 c->fc->track = get_be32(pb);
1435 dprintf(c->fc, "%.4s %d\n", (char*)&atom.type, c->fc->track);
1436 return 0;
1437}
1438
c3e92a6c 1439static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
d2ace376 1440{
86b0affd
BC
1441 char *str = NULL;
1442 int size;
51c15201
BC
1443 uint16_t str_size;
1444
1445 if (c->itunes_metadata) {
1446 int data_size = get_be32(pb);
1447 int tag = get_le32(pb);
1448 if (tag == MKTAG('d','a','t','a')) {
1449 get_be32(pb); // type
1450 get_be32(pb); // unknown
1451 str_size = data_size - 16;
bf252f7f 1452 atom.size -= 16;
51c15201
BC
1453 } else return 0;
1454 } else {
1455 str_size = get_be16(pb); // string length
1456 get_be16(pb); // language
bf252f7f 1457 atom.size -= 4;
51c15201 1458 }
86b0affd
BC
1459 switch (atom.type) {
1460 case MKTAG(0xa9,'n','a','m'):
1461 str = c->fc->title; size = sizeof(c->fc->title); break;
51c15201 1462 case MKTAG(0xa9,'A','R','T'):
86b0affd
BC
1463 case MKTAG(0xa9,'w','r','t'):
1464 str = c->fc->author; size = sizeof(c->fc->author); break;
1465 case MKTAG(0xa9,'c','p','y'):
1466 str = c->fc->copyright; size = sizeof(c->fc->copyright); break;
51c15201 1467 case MKTAG(0xa9,'c','m','t'):
86b0affd
BC
1468 case MKTAG(0xa9,'i','n','f'):
1469 str = c->fc->comment; size = sizeof(c->fc->comment); break;
51c15201
BC
1470 case MKTAG(0xa9,'a','l','b'):
1471 str = c->fc->album; size = sizeof(c->fc->album); break;
86b0affd
BC
1472 }
1473 if (!str)
1474 return 0;
bf252f7f
BC
1475 if (atom.size < 0)
1476 return -1;
1477
1478 get_buffer(pb, str, FFMIN3(size, str_size, atom.size));
1479 dprintf(c->fc, "%.4s %s %d %lld\n", (char*)&atom.type, str, str_size, atom.size);
d2ace376
BF
1480 return 0;
1481}
1482
c3e92a6c 1483static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879 1484{
ec072669
JS
1485 int i;
1486 int width;
1487 int height;
1488 int64_t disp_transform[2];
1489 int display_matrix[3][2];
f444b977 1490 AVStream *st = c->fc->streams[c->fc->nb_streams-1];
6cdbff63 1491 MOVStreamContext *sc = st->priv_data;
f444b977 1492 int version = get_byte(pb);
5ca1d879 1493
23f08617 1494 get_be24(pb); /* flags */
5ca1d879
ZK
1495 /*
1496 MOV_TRACK_ENABLED 0x0001
1497 MOV_TRACK_IN_MOVIE 0x0002
1498 MOV_TRACK_IN_PREVIEW 0x0004
1499 MOV_TRACK_IN_POSTER 0x0008
1500 */
1501
1175561e
BC
1502 if (version == 1) {
1503 get_be64(pb);
1504 get_be64(pb);
1505 } else {
1506 get_be32(pb); /* creation time */
1507 get_be32(pb); /* modification time */
1508 }
5ca1d879
ZK
1509 st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
1510 get_be32(pb); /* reserved */
25c4950e 1511 st->start_time = 0; /* check */
117a9190
BC
1512 /* highlevel (considering edits) duration in movie timebase */
1513 (version == 1) ? get_be64(pb) : get_be32(pb);
5ca1d879
ZK
1514 get_be32(pb); /* reserved */
1515 get_be32(pb); /* reserved */
1516
1517 get_be16(pb); /* layer */
1518 get_be16(pb); /* alternate group */
1519 get_be16(pb); /* volume */
1520 get_be16(pb); /* reserved */
1521
ec072669
JS
1522 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
1523 // they're kept in fixed point format through all calculations
1524 // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
1525 for (i = 0; i < 3; i++) {
1526 display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
1527 display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
1528 get_be32(pb); // 2.30 fixed point (not used)
1529 }
5ca1d879 1530
ec072669
JS
1531 width = get_be32(pb); // 16.16 fixed point track width
1532 height = get_be32(pb); // 16.16 fixed point track height
6cdbff63
DC
1533 sc->width = width >> 16;
1534 sc->height = height >> 16;
ec072669
JS
1535
1536 //transform the display width/height according to the matrix
1537 // skip this if the display matrix is the default identity matrix
1538 // to keep the same scale, use [width height 1<<16]
1539 if (width && height &&
1540 (display_matrix[0][0] != 65536 || display_matrix[0][1] ||
1541 display_matrix[1][0] || display_matrix[1][1] != 65536 ||
1542 display_matrix[2][0] || display_matrix[2][1])) {
1543 for (i = 0; i < 2; i++)
1544 disp_transform[i] =
1545 (int64_t) width * display_matrix[0][i] +
1546 (int64_t) height * display_matrix[1][i] +
1547 ((int64_t) display_matrix[2][i] << 16);
1548
1549 //sample aspect ratio is new width/height divided by old width/height
59729451 1550 st->sample_aspect_ratio = av_d2q(
ec072669
JS
1551 ((double) disp_transform[0] * height) /
1552 ((double) disp_transform[1] * width), INT_MAX);
1553 }
5ca1d879
ZK
1554 return 0;
1555}
1556
c3e92a6c 1557static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
61aedb0f
BC
1558{
1559 MOVFragment *frag = &c->fragment;
1560 MOVTrackExt *trex = NULL;
1561 int flags, track_id, i;
1562
1563 get_byte(pb); /* version */
1564 flags = get_be24(pb);
1565
1566 track_id = get_be32(pb);
1567 if (!track_id || track_id > c->fc->nb_streams)
1568 return -1;
1569 frag->track_id = track_id;
1570 for (i = 0; i < c->trex_count; i++)
1571 if (c->trex_data[i].track_id == frag->track_id) {
1572 trex = &c->trex_data[i];
1573 break;
1574 }
1575 if (!trex) {
1576 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
1577 return -1;
1578 }
1579
1580 if (flags & 0x01) frag->base_data_offset = get_be64(pb);
1581 else frag->base_data_offset = frag->moof_offset;
1582 if (flags & 0x02) frag->stsd_id = get_be32(pb);
1583 else frag->stsd_id = trex->stsd_id;
1584
1585 frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
1586 frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
1587 frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
1588 dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
1589 return 0;
1590}
1591
c3e92a6c 1592static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
61aedb0f
BC
1593{
1594 MOVTrackExt *trex;
1595
1596 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
1597 return -1;
1598 c->trex_data = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
1599 if (!c->trex_data)
1600 return AVERROR(ENOMEM);
1601 trex = &c->trex_data[c->trex_count++];
1602 get_byte(pb); /* version */
1603 get_be24(pb); /* flags */
1604 trex->track_id = get_be32(pb);
1605 trex->stsd_id = get_be32(pb);
1606 trex->duration = get_be32(pb);
1607 trex->size = get_be32(pb);
1608 trex->flags = get_be32(pb);
1609 return 0;
1610}
1611
c3e92a6c 1612static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
61aedb0f
BC
1613{
1614 MOVFragment *frag = &c->fragment;
44e43aab
BC
1615 AVStream *st;
1616 MOVStreamContext *sc;
61aedb0f
BC
1617 uint64_t offset;
1618 int64_t dts;
1619 int data_offset = 0;
1620 unsigned entries, first_sample_flags = frag->flags;
1621 int flags, distance, i;
1622
44e43aab
BC
1623 if (!frag->track_id || frag->track_id > c->fc->nb_streams)
1624 return -1;
1625 st = c->fc->streams[frag->track_id-1];
1626 sc = st->priv_data;
61aedb0f
BC
1627 if (sc->pseudo_stream_id+1 != frag->stsd_id)
1628 return 0;
61aedb0f
BC
1629 get_byte(pb); /* version */
1630 flags = get_be24(pb);
1631 entries = get_be32(pb);
1632 dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
1633 if (flags & 0x001) data_offset = get_be32(pb);
1634 if (flags & 0x004) first_sample_flags = get_be32(pb);
1635 if (flags & 0x800) {
1636 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
1637 return -1;
1638 sc->ctts_data = av_realloc(sc->ctts_data,
1639 (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
1640 if (!sc->ctts_data)
1641 return AVERROR(ENOMEM);
1642 }
1643 dts = st->duration;
1644 offset = frag->base_data_offset + data_offset;
1645 distance = 0;
1646 dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
1647 for (i = 0; i < entries; i++) {
1648 unsigned sample_size = frag->size;
1649 int sample_flags = i ? frag->flags : first_sample_flags;
1650 unsigned sample_duration = frag->duration;
1651 int keyframe;
1652
1653 if (flags & 0x100) sample_duration = get_be32(pb);
1654 if (flags & 0x200) sample_size = get_be32(pb);
1655 if (flags & 0x400) sample_flags = get_be32(pb);
1656 if (flags & 0x800) {
1657 sc->ctts_data[sc->ctts_count].count = 1;
1658 sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
1659 sc->ctts_count++;
1660 }
1661 if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
1662 (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
1663 distance = 0;
1664 av_add_index_entry(st, offset, dts, sample_size, distance,
1665 keyframe ? AVINDEX_KEYFRAME : 0);
1666 dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
1667 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
1668 offset, dts, sample_size, distance, keyframe);
1669 distance++;
1670 assert(sample_duration % sc->time_rate == 0);
1671 dts += sample_duration / sc->time_rate;
1672 offset += sample_size;
1673 }
1674 frag->moof_offset = offset;
1675 sc->sample_count = st->nb_index_entries;
1676 st->duration = dts;
1677 return 0;
1678}
1679
5ca1d879
ZK
1680/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
1681/* like the files created with Adobe Premiere 5.0, for samples see */
1682/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
c3e92a6c 1683static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
5ca1d879
ZK
1684{
1685 int err;
5ca1d879 1686
5ca1d879
ZK
1687 if (atom.size < 8)
1688 return 0; /* continue */
1689 if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
1690 url_fskip(pb, atom.size - 4);
1691 return 0;
1692 }
1693 atom.type = get_le32(pb);
1694 atom.offset += 8;
1695 atom.size -= 8;
1c126b4f 1696 if (atom.type != MKTAG('m','d','a','t')) {
5ca1d879
ZK
1697 url_fskip(pb, atom.size);
1698 return 0;
1699 }
1700 err = mov_read_mdat(c, pb, atom);
5ca1d879
ZK
1701 return err;
1702}
1703
c3e92a6c 1704static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
0147f198 1705{
b250f9c6 1706#if CONFIG_ZLIB
0147f198 1707 ByteIOContext ctx;
b6a17df4
ZK
1708 uint8_t *cmov_data;
1709 uint8_t *moov_data; /* uncompressed data */
0147f198 1710 long cmov_len, moov_len;
6f04eb1e 1711 int ret = -1;
b6a17df4 1712
0147f198 1713 get_be32(pb); /* dcom atom */
1c126b4f 1714 if (get_le32(pb) != MKTAG('d','c','o','m'))
0147f198 1715 return -1;
1c126b4f 1716 if (get_le32(pb) != MKTAG('z','l','i','b')) {
4e5ef14f 1717 av_log(NULL, AV_LOG_ERROR, "unknown compression for cmov atom !");
0147f198
FR
1718 return -1;
1719 }
1720 get_be32(pb); /* cmvd atom */
1c126b4f 1721 if (get_le32(pb) != MKTAG('c','m','v','d'))
0147f198
FR
1722 return -1;
1723 moov_len = get_be32(pb); /* uncompressed size */
5ca1d879 1724 cmov_len = atom.size - 6 * 4;
5cd62665 1725
9a630c25 1726 cmov_data = av_malloc(cmov_len);
0147f198
FR
1727 if (!cmov_data)
1728 return -1;
9a630c25 1729 moov_data = av_malloc(moov_len);
0147f198
FR
1730 if (!moov_data) {
1731 av_free(cmov_data);
1732 return -1;
1733 }
1734 get_buffer(pb, cmov_data, cmov_len);
b6a17df4 1735 if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
6f04eb1e 1736 goto free_and_return;
942f3bb5 1737 if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
6f04eb1e 1738 goto free_and_return;
1c126b4f 1739 atom.type = MKTAG('m','o','o','v');
5ca1d879
ZK
1740 atom.offset = 0;
1741 atom.size = moov_len;
9ed83b0a 1742#ifdef DEBUG
dbb4f00a 1743// { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
9ed83b0a 1744#endif
5ca1d879 1745 ret = mov_read_default(c, &ctx, atom);
6f04eb1e 1746free_and_return:
0147f198
FR
1747 av_free(moov_data);
1748 av_free(cmov_data);
1749 return ret;
d966b2f0
BC
1750#else
1751 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
1752 return -1;
0147f198 1753#endif
d966b2f0 1754}
0147f198 1755
baf25c9d 1756/* edit list atom */
c3e92a6c 1757static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
baf25c9d 1758{
7c622eed 1759 MOVStreamContext *sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
e0977c80
BC
1760 int i, edit_count;
1761
1762 get_byte(pb); /* version */
23f08617 1763 get_be24(pb); /* flags */
1975c52c 1764 edit_count = get_be32(pb); /* entries */
e0977c80
BC
1765
1766 for(i=0; i<edit_count; i++){
d435e520 1767 int time;
e0977c80 1768 get_be32(pb); /* Track duration */
d435e520 1769 time = get_be32(pb); /* Media time */
e0977c80 1770 get_be32(pb); /* Media rate */
baf2ffd3
BC
1771 if (i == 0 && time != -1)
1772 sc->time_offset = time;
e0977c80 1773 }
baf2ffd3
BC
1774
1775 if(edit_count > 1)
1776 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
1777 "a/v desync might occur, patch welcome\n");
1778
7c622eed 1779 dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, sc->edit_count);
e0977c80 1780 return 0;
baf25c9d
GC
1781}
1782
6cea494e 1783static const MOVParseTableEntry mov_default_parse_table[] = {
d4fdba0d 1784{ MKTAG('a','v','s','s'), mov_read_extradata },
1c126b4f
BC
1785{ MKTAG('c','o','6','4'), mov_read_stco },
1786{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
1787{ MKTAG('d','i','n','f'), mov_read_default },
1788{ MKTAG('d','r','e','f'), mov_read_dref },
1789{ MKTAG('e','d','t','s'), mov_read_default },
1790{ MKTAG('e','l','s','t'), mov_read_elst },
1791{ MKTAG('e','n','d','a'), mov_read_enda },
1792{ MKTAG('f','i','e','l'), mov_read_extradata },
1793{ MKTAG('f','t','y','p'), mov_read_ftyp },
1794{ MKTAG('g','l','b','l'), mov_read_glbl },
1795{ MKTAG('h','d','l','r'), mov_read_hdlr },
51c15201 1796{ MKTAG('i','l','s','t'), mov_read_ilst },
1c126b4f
BC
1797{ MKTAG('j','p','2','h'), mov_read_extradata },
1798{ MKTAG('m','d','a','t'), mov_read_mdat },
1799{ MKTAG('m','d','h','d'), mov_read_mdhd },
1800{ MKTAG('m','d','i','a'), mov_read_default },
51c15201 1801{ MKTAG('m','e','t','a'), mov_read_meta },
1c126b4f
BC
1802{ MKTAG('m','i','n','f'), mov_read_default },
1803{ MKTAG('m','o','o','f'), mov_read_moof },
1804{ MKTAG('m','o','o','v'), mov_read_moov },
1805{ MKTAG('m','v','e','x'), mov_read_default },
1806{ MKTAG('m','v','h','d'), mov_read_mvhd },
1807{ MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
1808{ MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
1809{ MKTAG('a','v','c','C'), mov_read_glbl },
6da54074 1810{ MKTAG('p','a','s','p'), mov_read_pasp },
1c126b4f
BC
1811{ MKTAG('s','t','b','l'), mov_read_default },
1812{ MKTAG('s','t','c','o'), mov_read_stco },
1813{ MKTAG('s','t','s','c'), mov_read_stsc },
1814{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
1815{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
1816{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
1817{ MKTAG('s','t','t','s'), mov_read_stts },
1818{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
1819{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
1820{ MKTAG('t','r','a','k'), mov_read_trak },
1821{ MKTAG('t','r','a','f'), mov_read_default },
1822{ MKTAG('t','r','e','x'), mov_read_trex },
51c15201 1823{ MKTAG('t','r','k','n'), mov_read_trkn },
1c126b4f 1824{ MKTAG('t','r','u','n'), mov_read_trun },
86b0affd 1825{ MKTAG('u','d','t','a'), mov_read_default },
1c126b4f
BC
1826{ MKTAG('w','a','v','e'), mov_read_wave },
1827{ MKTAG('e','s','d','s'), mov_read_esds },
1828{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
1829{ MKTAG('c','m','o','v'), mov_read_cmov },
86b0affd
BC
1830{ MKTAG(0xa9,'n','a','m'), mov_read_udta_string },
1831{ MKTAG(0xa9,'w','r','t'), mov_read_udta_string },
1832{ MKTAG(0xa9,'c','p','y'), mov_read_udta_string },
1833{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
51c15201
BC
1834{ MKTAG(0xa9,'i','n','f'), mov_read_udta_string },
1835{ MKTAG(0xa9,'A','R','T'), mov_read_udta_string },
1836{ MKTAG(0xa9,'a','l','b'), mov_read_udta_string },
1837{ MKTAG(0xa9,'c','m','t'), mov_read_udta_string },
bcfe2ba0 1838{ 0, NULL }
6cea494e
ZK
1839};
1840
c9a65ca8
FB
1841static int mov_probe(AVProbeData *p)
1842{
0e7eed09
FB
1843 unsigned int offset;
1844 uint32_t tag;
2497479f 1845 int score = 0;
3ffe3793 1846
c9a65ca8 1847 /* check file header */
0e7eed09
FB
1848 offset = 0;
1849 for(;;) {
1850 /* ignore invalid offset */
1851 if ((offset + 8) > (unsigned int)p->buf_size)
2497479f 1852 return score;
fead30d4 1853 tag = AV_RL32(p->buf + offset + 4);
0e7eed09 1854 switch(tag) {
2497479f 1855 /* check for obvious tags */
1c126b4f
BC
1856 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
1857 case MKTAG('m','o','o','v'):
1858 case MKTAG('m','d','a','t'):
1859 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
1860 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
40e7aaa3 1861 case MKTAG('f','t','y','p'):
3ffe3793 1862 return AVPROBE_SCORE_MAX;
2497479f 1863 /* those are more common words, so rate then a bit less */
1c126b4f
BC
1864 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
1865 case MKTAG('w','i','d','e'):
1866 case MKTAG('f','r','e','e'):
1867 case MKTAG('j','u','n','k'):
1868 case MKTAG('p','i','c','t'):
2497479f 1869 return AVPROBE_SCORE_MAX - 5;
5469b788 1870 case MKTAG(0x82,0x82,0x7f,0x7d):
1c126b4f
BC
1871 case MKTAG('s','k','i','p'):
1872 case MKTAG('u','u','i','d'):
1873 case MKTAG('p','r','f','l'):
fead30d4 1874 offset = AV_RB32(p->buf+offset) + offset;
2497479f
FR
1875 /* if we only find those cause probedata is too small at least rate them */
1876 score = AVPROBE_SCORE_MAX - 50;
0e7eed09
FB
1877 break;
1878 default:
1879 /* unrecognized tag */
2497479f 1880 return score;
0e7eed09 1881 }
3ffe3793 1882 }
2497479f 1883 return score;
c9a65ca8
FB
1884}
1885
a266644f 1886static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
6cea494e 1887{
e4141433 1888 MOVContext *mov = s->priv_data;
899681cd 1889 ByteIOContext *pb = s->pb;
f9900374 1890 int err;
c3e92a6c 1891 MOVAtom atom = { 0, 0, 0 };
6cea494e 1892
6cea494e 1893 mov->fc = s;
117a9190
BC
1894 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
1895 if(!url_is_streamed(pb))
bb270c08 1896 atom.size = url_fsize(pb);
6cea494e 1897 else
fa22ca22 1898 atom.size = INT64_MAX;
6cea494e 1899
6cea494e 1900 /* check MOV header */
612b5cbb
BC
1901 if ((err = mov_read_default(mov, pb, atom)) < 0) {
1902 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
1903 return err;
1904 }
1905 if (!mov->found_moov) {
1906 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
bb270c08 1907 return -1;
6cea494e 1908 }
0265fe81 1909 dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
4e5ef14f 1910
6cea494e
ZK
1911 return 0;
1912}
1913
a266644f 1914static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
6cea494e 1915{
b72708f8
BC
1916 MOVContext *mov = s->priv_data;
1917 MOVStreamContext *sc = 0;
1918 AVIndexEntry *sample = 0;
86d8602f 1919 int64_t best_dts = INT64_MAX;
b72708f8 1920 int i;
433aeb62 1921 retry:
11f16b66 1922 for (i = 0; i < s->nb_streams; i++) {
7c622eed
BC
1923 AVStream *st = s->streams[i];
1924 MOVStreamContext *msc = st->priv_data;
221e21b7 1925 if (st->discard != AVDISCARD_ALL && msc->pb && msc->current_sample < msc->sample_count) {
7c622eed 1926 AVIndexEntry *current_sample = &st->index_entries[msc->current_sample];
29c90869
BC
1927 int64_t dts = av_rescale(current_sample->timestamp * (int64_t)msc->time_rate,
1928 AV_TIME_BASE, msc->time_scale);
1c02d96f 1929 dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
687f35f3
BC
1930 if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
1931 (!url_is_streamed(s->pb) &&
221e21b7 1932 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
687f35f3 1933 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
221e21b7 1934 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
b72708f8
BC
1935 sample = current_sample;
1936 best_dts = dts;
1937 sc = msc;
86d8602f 1938 }
6cea494e 1939 }
6cea494e 1940 }
433aeb62
BC
1941 if (!sample) {
1942 mov->found_mdat = 0;
1943 if (!url_is_streamed(s->pb) ||
c3e92a6c 1944 mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
433aeb62
BC
1945 url_feof(s->pb))
1946 return -1;
1947 dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
1948 goto retry;
1949 }
b72708f8
BC
1950 /* must be done just before reading, to avoid infinite loop on sample */
1951 sc->current_sample++;
221e21b7 1952 if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
29c90869
BC
1953 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
1954 sc->ffindex, sample->pos);
bb270c08 1955 return -1;
6cea494e 1956 }
221e21b7 1957 av_get_packet(sc->pb, pkt, sample->size);
b250f9c6 1958#if CONFIG_DV_DEMUXER
56ea717b
BC
1959 if (mov->dv_demux && sc->dv_audio_container) {
1960 dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
1961 av_free(pkt->data);
300aa2b0
BC
1962 pkt->size = 0;
1963 if (dv_get_packet(mov->dv_demux, pkt) < 0)
1964 return -1;
56ea717b 1965 }
312954f0 1966#endif
b72708f8
BC
1967 pkt->stream_index = sc->ffindex;
1968 pkt->dts = sample->timestamp;
1969 if (sc->ctts_data) {
a234e38d
BC
1970 assert(sc->ctts_data[sc->ctts_index].duration % sc->time_rate == 0);
1971 pkt->pts = pkt->dts + sc->ctts_data[sc->ctts_index].duration / sc->time_rate;
b72708f8 1972 /* update ctts context */
a234e38d
BC
1973 sc->ctts_sample++;
1974 if (sc->ctts_index < sc->ctts_count &&
1975 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
1976 sc->ctts_index++;
1977 sc->ctts_sample = 0;
b565ea09 1978 }
0c5f76f7
BC
1979 if (sc->wrong_dts)
1980 pkt->dts = AV_NOPTS_VALUE;
b72708f8 1981 } else {
e8430214
HG
1982 AVStream *st = s->streams[sc->ffindex];
1983 int64_t next_dts = (sc->current_sample < sc->sample_count) ?
1984 st->index_entries[sc->current_sample].timestamp : st->duration;
1985 pkt->duration = next_dts - pkt->dts;
b72708f8 1986 pkt->pts = pkt->dts;
b565ea09 1987 }
b72708f8
BC
1988 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
1989 pkt->pos = sample->pos;
29c90869
BC
1990 dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
1991 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
b72708f8
BC
1992 return 0;
1993}
3ffe3793 1994
b72708f8
BC
1995static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
1996{
1997 MOVStreamContext *sc = st->priv_data;
1998 int sample, time_sample;
1999 int i;
115329f1 2000
b72708f8 2001 sample = av_index_search_timestamp(st, timestamp, flags);
318c5e05 2002 dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
b72708f8
BC
2003 if (sample < 0) /* not sure what to do */
2004 return -1;
2005 sc->current_sample = sample;
1c02d96f 2006 dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
b72708f8
BC
2007 /* adjust ctts index */
2008 if (sc->ctts_data) {
2009 time_sample = 0;
2010 for (i = 0; i < sc->ctts_count; i++) {
54a5c719
BC
2011 int next = time_sample + sc->ctts_data[i].count;
2012 if (next > sc->current_sample) {
a234e38d
BC
2013 sc->ctts_index = i;
2014 sc->ctts_sample = sc->current_sample - time_sample;
b72708f8 2015 break;
115329f1 2016 }
54a5c719 2017 time_sample = next;
247d56f5 2018 }
247d56f5 2019 }
b72708f8 2020 return sample;
6cea494e
ZK
2021}
2022
961e0ccd 2023static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
baf25c9d 2024{
b72708f8
BC
2025 AVStream *st;
2026 int64_t seek_timestamp, timestamp;
2027 int sample;
2028 int i;
baf25c9d 2029
b72708f8 2030 if (stream_index >= s->nb_streams)
baf25c9d 2031 return -1;
8dc81a45
BC
2032 if (sample_time < 0)
2033 sample_time = 0;
baf25c9d 2034
b72708f8
BC
2035 st = s->streams[stream_index];
2036 sample = mov_seek_stream(st, sample_time, flags);
2037 if (sample < 0)
baf25c9d 2038 return -1;
baf25c9d 2039
b72708f8
BC
2040 /* adjust seek timestamp to found sample timestamp */
2041 seek_timestamp = st->index_entries[sample].timestamp;
baf25c9d 2042
b72708f8
BC
2043 for (i = 0; i < s->nb_streams; i++) {
2044 st = s->streams[i];
2045 if (stream_index == i || st->discard == AVDISCARD_ALL)
2046 continue;
baf25c9d 2047
b72708f8
BC
2048 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
2049 mov_seek_stream(st, timestamp, flags);
115329f1 2050 }
baf25c9d
GC
2051 return 0;
2052}
baf25c9d 2053
a266644f 2054static int mov_read_close(AVFormatContext *s)
6cea494e 2055{
221e21b7 2056 int i, j;
e4141433 2057 MOVContext *mov = s->priv_data;
11f16b66 2058 for(i=0; i<s->nb_streams; i++) {
7c622eed
BC
2059 MOVStreamContext *sc = s->streams[i]->priv_data;
2060 av_freep(&sc->ctts_data);
221e21b7
BC
2061 for (j=0; j<sc->drefs_count; j++)
2062 av_freep(&sc->drefs[j].path);
2063 av_freep(&sc->drefs);
2064 if (sc->pb && sc->pb != s->pb)
2065 url_fclose(sc->pb);
4440b118 2066 }
b60c0454
BC
2067 if(mov->dv_demux){
2068 for(i=0; i<mov->dv_fctx->nb_streams; i++){
2069 av_freep(&mov->dv_fctx->streams[i]->codec);
2070 av_freep(&mov->dv_fctx->streams[i]);
2071 }
2072 av_freep(&mov->dv_fctx);
2073 av_freep(&mov->dv_demux);
2074 }
61aedb0f 2075 av_freep(&mov->trex_data);
6cea494e
ZK
2076 return 0;
2077}
2078
ff70e601 2079AVInputFormat mov_demuxer = {
fc5188f3 2080 "mov,mp4,m4a,3gp,3g2,mj2",
bde15e74 2081 NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
c9a65ca8
FB
2082 sizeof(MOVContext),
2083 mov_probe,
6cea494e
ZK
2084 mov_read_header,
2085 mov_read_packet,
2086 mov_read_close,
baf25c9d 2087 mov_read_seek,
6cea494e 2088};