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