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