mov: Allow more than one keyframe per trun
[libav.git] / libavformat / mov.c
CommitLineData
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 *
ac9362c5
DB
6 * first version by Francois Revol <revol@free.fr>
7 * seek function by Gael Chardon <gael.dev@4now.net>
8 *
2912e87a 9 * This file is part of Libav.
b78e7197 10 *
2912e87a 11 * Libav is free software; you can redistribute it and/or
19720f15
FB
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
b78e7197 14 * version 2.1 of the License, or (at your option) any later version.
6cea494e 15 *
2912e87a 16 * Libav is distributed in the hope that it will be useful,
6cea494e 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
6cea494e 20 *
19720f15 21 * You should have received a copy of the GNU Lesser General Public
2912e87a 22 * License along with Libav; if not, write to the Free Software
5509bffa 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6cea494e 24 */
d957696f 25
d92024f1 26#include <inttypes.h>
d957696f 27#include <limits.h>
8f8bc923 28#include <stdint.h>
115329f1 29
30b1961c 30#include "libavutil/attributes.h"
a903f8f0 31#include "libavutil/channel_layout.h"
6a5d31ac 32#include "libavutil/intreadwrite.h"
3383a53e 33#include "libavutil/intfloat.h"
0ebcdf5c 34#include "libavutil/mathematics.h"
82ee7d0d 35#include "libavutil/time_internal.h"
49674dd0 36#include "libavutil/avstring.h"
d2d67e42 37#include "libavutil/dict.h"
e352b293 38#include "libavutil/opt.h"
3798205a 39#include "libavcodec/ac3tab.h"
6cea494e 40#include "avformat.h"
c3f9ebf7 41#include "internal.h"
e731b8d8 42#include "avio_internal.h"
9d9f4119 43#include "riff.h"
e40ee6a2 44#include "isom.h"
9106a698 45#include "libavcodec/get_bits.h"
94395fbf 46#include "id3v1.h"
1fdf18f4 47#include "mov_chan.h"
9a07fac6 48#include "replaygain.h"
6cea494e 49
b250f9c6 50#if CONFIG_ZLIB
0147f198
FR
51#include <zlib.h>
52#endif
53
b595afaa
MM
54#include "qtpalette.h"
55
baf25c9d 56
cd7352d5
MN
57#undef NDEBUG
58#include <assert.h>
59
6cea494e 60/* those functions parse an atom */
6cea494e
ZK
61/* links atom IDs to parse functions */
62typedef struct MOVParseTableEntry {
0c1a9eda 63 uint32_t type;
ae628ec1 64 int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
6cea494e
ZK
65} MOVParseTableEntry;
66
1ce1e636 67static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
73d07c27 68
dff41775
RH
69static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
70 unsigned len, const char *key)
c6026e43
BC
71{
72 char buf[16];
73
fbcaceb1 74 short current, total = 0;
b7effd4e 75 avio_rb16(pb); // unknown
dff41775 76 current = avio_rb16(pb);
fbcaceb1
CEH
77 if (len >= 6)
78 total = avio_rb16(pb);
dff41775
RH
79 if (!total)
80 snprintf(buf, sizeof(buf), "%d", current);
81 else
82 snprintf(buf, sizeof(buf), "%d/%d", current, total);
cc3e88a2 83 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
dff41775 84 av_dict_set(&c->fc->metadata, key, buf, 0);
c6026e43
BC
85
86 return 0;
87}
88
80951f5c
RH
89static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
90 unsigned len, const char *key)
f0556353 91{
80951f5c 92 char buf[16];
f0556353 93
80951f5c
RH
94 /* bypass padding bytes */
95 avio_r8(pb);
96 avio_r8(pb);
97 avio_r8(pb);
f0556353 98
028a2375 99 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
cc3e88a2 100 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
80951f5c 101 av_dict_set(&c->fc->metadata, key, buf, 0);
f0556353 102
80951f5c 103 return 0;
f0556353
RH
104}
105
5da35d1c
RH
106static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
107 unsigned len, const char *key)
f0556353 108{
5da35d1c 109 char buf[16];
f0556353 110
028a2375 111 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
cc3e88a2 112 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
5da35d1c 113 av_dict_set(&c->fc->metadata, key, buf, 0);
f0556353 114
5da35d1c 115 return 0;
f0556353
RH
116}
117
94395fbf
RH
118static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
119 unsigned len, const char *key)
120{
121 short genre;
122 char buf[20];
123
124 avio_r8(pb); // unknown
125
126 genre = avio_r8(pb);
127 if (genre < 1 || genre > ID3v1_GENRE_MAX)
128 return 0;
129 snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
cc3e88a2 130 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
94395fbf
RH
131 av_dict_set(&c->fc->metadata, key, buf, 0);
132
133 return 0;
134}
135
08a186c6
BC
136static const uint32_t mac_to_unicode[128] = {
137 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
138 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
139 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
140 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
141 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
142 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
143 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
144 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
145 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
146 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
147 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
148 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
149 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
150 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
151 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
152 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
153};
154
ae628ec1 155static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
08a186c6
BC
156 char *dst, int dstlen)
157{
158 char *p = dst;
159 char *end = dst+dstlen-1;
160 int i;
161
162 for (i = 0; i < len; i++) {
b7effd4e 163 uint8_t t, c = avio_r8(pb);
08a186c6
BC
164 if (c < 0x80 && p < end)
165 *p++ = c;
166 else
167 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
168 }
169 *p = 0;
170 return p - dst;
171}
172
a02b8c69
AK
173static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
174{
175 AVPacket pkt;
176 AVStream *st;
177 MOVStreamContext *sc;
36ef5369 178 enum AVCodecID id;
a02b8c69
AK
179 int ret;
180
181 switch (type) {
36ef5369
AK
182 case 0xd: id = AV_CODEC_ID_MJPEG; break;
183 case 0xe: id = AV_CODEC_ID_PNG; break;
184 case 0x1b: id = AV_CODEC_ID_BMP; break;
a02b8c69
AK
185 default:
186 av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
187 avio_skip(pb, len);
188 return 0;
189 }
190
191 st = avformat_new_stream(c->fc, NULL);
192 if (!st)
193 return AVERROR(ENOMEM);
194 sc = av_mallocz(sizeof(*sc));
195 if (!sc)
196 return AVERROR(ENOMEM);
197 st->priv_data = sc;
198
199 ret = av_get_packet(pb, &pkt, len);
200 if (ret < 0)
201 return ret;
202
203 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
204
205 st->attached_pic = pkt;
206 st->attached_pic.stream_index = st->index;
207 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
208
209 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
210 st->codec->codec_id = id;
211
212 return 0;
213}
214
e7d6d0bf
MS
215static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
216{
217 char language[4] = { 0 };
218 char buf[100];
219 uint16_t langcode = 0;
18fb38fb 220 double longitude, latitude;
e7d6d0bf
MS
221 const char *key = "location";
222
223 if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4)
224 return AVERROR_INVALIDDATA;
225
226 avio_skip(pb, 4); // version+flags
227 langcode = avio_rb16(pb);
228 ff_mov_lang_to_iso639(langcode, language);
229 len -= 6;
230
231 len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name
232 if (len < 1)
233 return AVERROR_INVALIDDATA;
234 avio_skip(pb, 1); // role
235 len -= 1;
236
237 if (len < 14)
238 return AVERROR_INVALIDDATA;
239 longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
240 latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
e7d6d0bf
MS
241
242 // Try to output in the same format as the ?xyz field
243 snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude);
244 if (*language && strcmp(language, "und")) {
245 char key2[16];
246 snprintf(key2, sizeof(key2), "%s-%s", key, language);
247 av_dict_set(&c->fc->metadata, key2, buf, 0);
248 }
cc3e88a2 249 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
e7d6d0bf
MS
250 return av_dict_set(&c->fc->metadata, key, buf, 0);
251}
252
ae628ec1 253static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
0fb44a4e 254{
0fb44a4e 255 char tmp_key[5];
3cec81f4 256 char *str, key2[32], language[4] = {0};
0fb44a4e 257 const char *key = NULL;
a7b8ff94 258 uint16_t langcode = 0;
3cec81f4 259 uint32_t data_type = 0, str_size, str_size_alloc;
dff41775 260 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
174c5fde 261 int raw = 0;
0fb44a4e
BC
262
263 switch (atom.type) {
174c5fde
VG
264 case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
265 case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
b704b648
VG
266 case MKTAG( 'X','M','P','_'):
267 if (c->export_xmp) { key = "xmp"; raw = 1; } break;
2804d320 268 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
e2e07dba
VG
269 case MKTAG( 'a','k','I','D'): key = "account_type";
270 parse = mov_metadata_int8_no_padding; break;
271 case MKTAG( 'a','p','I','D'): key = "account_id"; break;
272 case MKTAG( 'c','a','t','g'): key = "category"; break;
273 case MKTAG( 'c','p','i','l'): key = "compilation";
274 parse = mov_metadata_int8_no_padding; break;
35384934
VG
275 case MKTAG( 'c','p','r','t'): key = "copyright"; break;
276 case MKTAG( 'd','e','s','c'): key = "description"; break;
277 case MKTAG( 'd','i','s','k'): key = "disc";
278 parse = mov_metadata_track_or_disc_number; break;
e2e07dba
VG
279 case MKTAG( 'e','g','i','d'): key = "episode_uid";
280 parse = mov_metadata_int8_no_padding; break;
94395fbf
RH
281 case MKTAG( 'g','n','r','e'): key = "genre";
282 parse = mov_metadata_gnre; break;
35384934
VG
283 case MKTAG( 'h','d','v','d'): key = "hd_video";
284 parse = mov_metadata_int8_no_padding; break;
e2e07dba 285 case MKTAG( 'k','e','y','w'): key = "keywords"; break;
7382902b 286 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
35384934
VG
287 case MKTAG( 'l','o','c','i'):
288 return mov_metadata_loci(c, pb, atom.size);
e2e07dba
VG
289 case MKTAG( 'p','c','s','t'): key = "podcast";
290 parse = mov_metadata_int8_no_padding; break;
35384934
VG
291 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
292 parse = mov_metadata_int8_no_padding; break;
e2e07dba
VG
293 case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
294 case MKTAG( 'r','t','n','g'): key = "rating";
295 parse = mov_metadata_int8_no_padding; break;
296 case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
297 case MKTAG( 's','o','a','l'): key = "sort_album"; break;
298 case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
299 case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
300 case MKTAG( 's','o','n','m'): key = "sort_name"; break;
301 case MKTAG( 's','o','s','n'): key = "sort_show"; break;
35384934
VG
302 case MKTAG( 's','t','i','k'): key = "media_type";
303 parse = mov_metadata_int8_no_padding; break;
c6026e43 304 case MKTAG( 't','r','k','n'): key = "track";
dff41775 305 parse = mov_metadata_track_or_disc_number; break;
35384934 306 case MKTAG( 't','v','e','n'): key = "episode_id"; break;
f0556353 307 case MKTAG( 't','v','e','s'): key = "episode_sort";
80951f5c 308 parse = mov_metadata_int8_bypass_padding; break;
35384934
VG
309 case MKTAG( 't','v','n','n'): key = "network"; break;
310 case MKTAG( 't','v','s','h'): key = "show"; break;
f0556353 311 case MKTAG( 't','v','s','n'): key = "season_number";
80951f5c 312 parse = mov_metadata_int8_bypass_padding; break;
35384934 313 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
3c01039e 314 case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
35384934
VG
315 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
316 case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
3c01039e 317 case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
35384934 318 case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
3c01039e 319 case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
35384934
VG
320 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
321 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
3c01039e
DR
322 case MKTAG(0xa9,'d','i','r'): key = "director"; break;
323 case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
324 case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
35384934 325 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
3c01039e 326 case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
35384934 327 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
e2e07dba 328 case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
3c01039e 329 case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
35384934 330 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
e2e07dba 331 case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
3c01039e
DR
332 case MKTAG(0xa9,'m','a','k'): key = "make"; break;
333 case MKTAG(0xa9,'m','o','d'): key = "model"; break;
35384934 334 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
3c01039e
DR
335 case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
336 case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
337 case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
338 case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
339 case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
e2e07dba 340 case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
35384934
VG
341 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
342 case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
3c01039e
DR
343 case MKTAG(0xa9,'t','r','k'): key = "track"; break;
344 case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
345 case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
35384934
VG
346 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
347 case MKTAG(0xa9,'x','y','z'): key = "location"; break;
0fb44a4e
BC
348 }
349
350 if (c->itunes_metadata && atom.size > 8) {
b7effd4e
AK
351 int data_size = avio_rb32(pb);
352 int tag = avio_rl32(pb);
0fb44a4e 353 if (tag == MKTAG('d','a','t','a')) {
b7effd4e
AK
354 data_type = avio_rb32(pb); // type
355 avio_rb32(pb); // unknown
0fb44a4e
BC
356 str_size = data_size - 16;
357 atom.size -= 16;
a02b8c69
AK
358
359 if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
360 int ret = mov_read_covr(c, pb, data_type, str_size);
361 if (ret < 0) {
362 av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
363 return ret;
364 }
365 }
0fb44a4e 366 } else return 0;
174c5fde 367 } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
b7effd4e
AK
368 str_size = avio_rb16(pb); // string length
369 langcode = avio_rb16(pb);
08a186c6 370 ff_mov_lang_to_iso639(langcode, language);
0fb44a4e
BC
371 atom.size -= 4;
372 } else
373 str_size = atom.size;
374
e352b293 375 if (c->export_all && !key) {
0fb44a4e
BC
376 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
377 key = tmp_key;
378 }
0fb44a4e
BC
379
380 if (!key)
381 return 0;
382 if (atom.size < 0)
bcd5d979 383 return AVERROR_INVALIDDATA;
0fb44a4e 384
3cec81f4 385 // allocate twice as much as worst-case
6f4364ab 386 str_size_alloc = (raw ? str_size : str_size * 2) + 1;
3cec81f4
TB
387 str = av_malloc(str_size_alloc);
388 if (!str)
389 return AVERROR(ENOMEM);
c6026e43
BC
390
391 if (parse)
dff41775 392 parse(c, pb, str_size, key);
c6026e43 393 else {
174c5fde 394 if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
3cec81f4 395 mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
08a186c6 396 } else {
5c720657
AC
397 int ret = ffio_read_size(pb, str, str_size);
398 if (ret < 0) {
399 av_free(str);
400 return ret;
401 }
08a186c6
BC
402 str[str_size] = 0;
403 }
cc3e88a2 404 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
d2d67e42 405 av_dict_set(&c->fc->metadata, key, str, 0);
f584386d
BC
406 if (*language && strcmp(language, "und")) {
407 snprintf(key2, sizeof(key2), "%s-%s", key, language);
d2d67e42 408 av_dict_set(&c->fc->metadata, key2, str, 0);
f584386d 409 }
c6026e43 410 }
1a3eb042
VG
411 av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
412 av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
3cec81f4 413 key, str, (char*)&atom.type, str_size_alloc, atom.size);
0fb44a4e 414
3cec81f4 415 av_freep(&str);
0fb44a4e
BC
416 return 0;
417}
873358e5 418
ae628ec1 419static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1cf9f6ed
DC
420{
421 int64_t start;
105b3785 422 int i, nb_chapters, str_len, version;
1cf9f6ed 423 char str[256+1];
5c720657 424 int ret;
1cf9f6ed
DC
425
426 if ((atom.size -= 5) < 0)
427 return 0;
428
b7effd4e
AK
429 version = avio_r8(pb);
430 avio_rb24(pb);
105b3785 431 if (version)
b7effd4e
AK
432 avio_rb32(pb); // ???
433 nb_chapters = avio_r8(pb);
1cf9f6ed
DC
434
435 for (i = 0; i < nb_chapters; i++) {
436 if (atom.size < 9)
437 return 0;
438
b7effd4e
AK
439 start = avio_rb64(pb);
440 str_len = avio_r8(pb);
1cf9f6ed
DC
441
442 if ((atom.size -= 9+str_len) < 0)
443 return 0;
444
5c720657
AC
445 ret = ffio_read_size(pb, str, str_len);
446 if (ret < 0)
447 return ret;
1cf9f6ed 448 str[str_len] = 0;
1fa395e4 449 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
1cf9f6ed
DC
450 }
451 return 0;
452}
453
8cc2fa1e 454#define MIN_DATA_ENTRY_BOX_SIZE 12
ae628ec1 455static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
221e21b7 456{
6a63ff19
BC
457 AVStream *st;
458 MOVStreamContext *sc;
221e21b7
BC
459 int entries, i, j;
460
6a63ff19
BC
461 if (c->fc->nb_streams < 1)
462 return 0;
463 st = c->fc->streams[c->fc->nb_streams-1];
464 sc = st->priv_data;
465
b7effd4e
AK
466 avio_rb32(pb); // version + flags
467 entries = avio_rb32(pb);
8cc2fa1e
JG
468 if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
469 entries >= UINT_MAX / sizeof(*sc->drefs))
bcd5d979 470 return AVERROR_INVALIDDATA;
4ebd422c 471 av_free(sc->drefs);
221e21b7 472 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
0d8f0abf
BC
473 if (!sc->drefs)
474 return AVERROR(ENOMEM);
475 sc->drefs_count = entries;
221e21b7
BC
476
477 for (i = 0; i < sc->drefs_count; i++) {
c3e92a6c 478 MOVDref *dref = &sc->drefs[i];
b7effd4e 479 uint32_t size = avio_rb32(pb);
a2704c97 480 int64_t next = avio_tell(pb) + size - 4;
221e21b7 481
0e7d436d 482 if (size < 12)
bcd5d979 483 return AVERROR_INVALIDDATA;
0e7d436d 484
b7effd4e
AK
485 dref->type = avio_rl32(pb);
486 avio_rb32(pb); // version + flags
1a3eb042 487 av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
221e21b7
BC
488
489 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
490 /* macintosh alias record */
491 uint16_t volume_len, len;
221e21b7 492 int16_t type;
5c720657 493 int ret;
221e21b7 494
45a8a02a 495 avio_skip(pb, 10);
221e21b7 496
b7effd4e 497 volume_len = avio_r8(pb);
221e21b7 498 volume_len = FFMIN(volume_len, 27);
5c720657
AC
499 ret = ffio_read_size(pb, dref->volume, 27);
500 if (ret < 0)
501 return ret;
adeb9071
BC
502 dref->volume[volume_len] = 0;
503 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
221e21b7 504
45a8a02a 505 avio_skip(pb, 12);
adeb9071 506
b7effd4e 507 len = avio_r8(pb);
adeb9071 508 len = FFMIN(len, 63);
5c720657
AC
509 ret = ffio_read_size(pb, dref->filename, 63);
510 if (ret < 0)
511 return ret;
adeb9071
BC
512 dref->filename[len] = 0;
513 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
514
45a8a02a 515 avio_skip(pb, 16);
adeb9071
BC
516
517 /* read next level up_from_alias/down_to_target */
b7effd4e
AK
518 dref->nlvl_from = avio_rb16(pb);
519 dref->nlvl_to = avio_rb16(pb);
adeb9071
BC
520 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
521 dref->nlvl_from, dref->nlvl_to);
522
45a8a02a 523 avio_skip(pb, 16);
221e21b7 524
a2704c97 525 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
9db67bed
MN
526 if (pb->eof_reached)
527 return AVERROR_EOF;
b7effd4e
AK
528 type = avio_rb16(pb);
529 len = avio_rb16(pb);
221e21b7
BC
530 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
531 if (len&1)
532 len += 1;
376589e5 533 if (type == 2 || type == 18) { // absolute path
dbb37657 534 av_free(dref->path);
221e21b7 535 dref->path = av_mallocz(len+1);
2f4568e5
BC
536 if (!dref->path)
537 return AVERROR(ENOMEM);
5c720657
AC
538
539 ret = ffio_read_size(pb, dref->path, len);
540 if (ret < 0) {
541 av_freep(&dref->path);
542 return ret;
543 }
376589e5
VG
544 if (type == 18) // no additional processing needed
545 continue;
adeb9071 546 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
221e21b7
BC
547 len -= volume_len;
548 memmove(dref->path, dref->path+volume_len, len);
549 dref->path[len] = 0;
550 }
551 for (j = 0; j < len; j++)
552 if (dref->path[j] == ':')
553 dref->path[j] = '/';
554 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
adeb9071
BC
555 } else if (type == 0) { // directory name
556 av_free(dref->dir);
557 dref->dir = av_malloc(len+1);
558 if (!dref->dir)
559 return AVERROR(ENOMEM);
5c720657
AC
560
561 ret = ffio_read_size(pb, dref->dir, len);
562 if (ret < 0) {
563 av_freep(&dref->dir);
564 return ret;
565 }
adeb9071
BC
566 dref->dir[len] = 0;
567 for (j = 0; j < len; j++)
568 if (dref->dir[j] == ':')
569 dref->dir[j] = '/';
570 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
221e21b7 571 } else
45a8a02a 572 avio_skip(pb, len);
221e21b7
BC
573 }
574 }
6b4aa5da 575 avio_seek(pb, next, SEEK_SET);
221e21b7
BC
576 }
577 return 0;
578}
579
ae628ec1 580static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6cea494e 581{
fefe43ff 582 AVStream *st;
0c1a9eda 583 uint32_t type;
5e1166b3 584 uint32_t av_unused ctype;
e3528d2a
HL
585 int64_t title_size;
586 char *title_str;
5c720657 587 int ret;
b6a17df4 588
fefe43ff
AC
589 if (c->fc->nb_streams < 1) // meta before first trak
590 return 0;
591
592 st = c->fc->streams[c->fc->nb_streams-1];
593
b7effd4e
AK
594 avio_r8(pb); /* version */
595 avio_rb24(pb); /* flags */
6cea494e
ZK
596
597 /* component type */
b7effd4e
AK
598 ctype = avio_rl32(pb);
599 type = avio_rl32(pb); /* component subtype */
6cea494e 600
1a3eb042
VG
601 av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
602 av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
2229a5c6 603
37ffe34b 604 if (type == MKTAG('v','i','d','e'))
72415b2a 605 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
5a7ba586 606 else if (type == MKTAG('s','o','u','n'))
72415b2a 607 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
5a7ba586 608 else if (type == MKTAG('m','1','a',' '))
36ef5369 609 st->codec->codec_id = AV_CODEC_ID_MP2;
5a7ba586 610 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
72415b2a 611 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
f05783c2 612
b7effd4e
AK
613 avio_rb32(pb); /* component manufacture */
614 avio_rb32(pb); /* component flags */
615 avio_rb32(pb); /* component flags mask */
6cea494e 616
e3528d2a
HL
617 title_size = atom.size - 24;
618 if (title_size > 0) {
619 title_str = av_malloc(title_size + 1); /* Add null terminator */
620 if (!title_str)
621 return AVERROR(ENOMEM);
5c720657
AC
622
623 ret = ffio_read_size(pb, title_str, title_size);
624 if (ret < 0) {
625 av_freep(&title_str);
626 return ret;
627 }
e3528d2a
HL
628 title_str[title_size] = 0;
629 if (title_str[0]) {
630 int off = (!c->isom && title_str[0] == title_size - 1);
631 av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
632 }
633 av_freep(&title_str);
634 }
635
6cea494e
ZK
636 return 0;
637}
638
86dfcfd0 639int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
5cd62665 640{
6a63ff19 641 AVStream *st;
e65ab9d9 642 int tag;
b6a17df4 643
2d423666 644 if (fc->nb_streams < 1)
6a63ff19 645 return 0;
2d423666 646 st = fc->streams[fc->nb_streams-1];
6a63ff19 647
b7effd4e 648 avio_rb32(pb); /* version + flags */
e65ab9d9 649 ff_mp4_read_descr(fc, pb, &tag);
5cd62665 650 if (tag == MP4ESDescrTag) {
ad6c7c1b 651 ff_mp4_parse_es_descr(pb, NULL);
5cd62665 652 } else
b7effd4e 653 avio_rb16(pb); /* ID */
5cd62665 654
e65ab9d9 655 ff_mp4_read_descr(fc, pb, &tag);
798c6fac
BC
656 if (tag == MP4DecConfigDescrTag)
657 ff_mp4_read_dec_config_descr(fc, st, pb);
5cd62665
ZK
658 return 0;
659}
660
ae628ec1 661static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2d423666 662{
86dfcfd0 663 return ff_mov_read_esds(c->fc, pb);
2d423666
PR
664}
665
ae628ec1 666static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
bdecdd2b
JR
667{
668 AVStream *st;
32125781 669 enum AVAudioServiceType *ast;
eb81cf86 670 int ac3info, acmod, lfeon, bsmod;
bdecdd2b 671
6d19fd5c
BC
672 if (c->fc->nb_streams < 1)
673 return 0;
bdecdd2b
JR
674 st = c->fc->streams[c->fc->nb_streams-1];
675
32125781
AK
676 ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
677 sizeof(*ast));
678 if (!ast)
679 return AVERROR(ENOMEM);
680
b7effd4e 681 ac3info = avio_rb24(pb);
eb81cf86 682 bsmod = (ac3info >> 14) & 0x7;
bdecdd2b
JR
683 acmod = (ac3info >> 11) & 0x7;
684 lfeon = (ac3info >> 10) & 0x1;
685 st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
3798205a
JR
686 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
687 if (lfeon)
688 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
32125781 689 *ast = bsmod;
eb81cf86 690 if (st->codec->channels > 1 && bsmod == 0x7)
32125781
AK
691 *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
692
693 st->codec->audio_service_type = *ast;
bdecdd2b
JR
694
695 return 0;
696}
697
546adc1f
YN
698static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
699{
700 AVStream *st;
32125781 701 enum AVAudioServiceType *ast;
546adc1f
YN
702 int eac3info, acmod, lfeon, bsmod;
703
704 if (c->fc->nb_streams < 1)
705 return 0;
706 st = c->fc->streams[c->fc->nb_streams-1];
707
32125781
AK
708 ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
709 sizeof(*ast));
710 if (!ast)
711 return AVERROR(ENOMEM);
712
546adc1f
YN
713 /* No need to parse fields for additional independent substreams and its
714 * associated dependent substreams since libavcodec's E-AC-3 decoder
715 * does not support them yet. */
716 avio_rb16(pb); /* data_rate and num_ind_sub */
717 eac3info = avio_rb24(pb);
718 bsmod = (eac3info >> 12) & 0x1f;
719 acmod = (eac3info >> 9) & 0x7;
720 lfeon = (eac3info >> 8) & 0x1;
721 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
722 if (lfeon)
723 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
724 st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
32125781 725 *ast = bsmod;
546adc1f 726 if (st->codec->channels > 1 && bsmod == 0x7)
32125781
AK
727 *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
728
729 st->codec->audio_service_type = *ast;
546adc1f
YN
730
731 return 0;
732}
733
1fdf18f4
JR
734static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
735{
736 AVStream *st;
1fdf18f4
JR
737
738 if (c->fc->nb_streams < 1)
739 return 0;
740 st = c->fc->streams[c->fc->nb_streams-1];
741
742 if (atom.size < 16)
743 return 0;
744
8c0a865a
MB
745 /* skip version and flags */
746 avio_skip(pb, 4);
747
9afb7061 748 ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
1fdf18f4
JR
749
750 return 0;
751}
752
85e9e3a9
AC
753static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
754{
755 AVStream *st;
756
757 if (c->fc->nb_streams < 1)
758 return 0;
759 st = c->fc->streams[c->fc->nb_streams-1];
760
d80811c9 761 return ff_get_wav_header(c->fc, pb, st->codec, atom.size);
85e9e3a9
AC
762}
763
ae628ec1 764static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6da54074 765{
b7effd4e
AK
766 const int num = avio_rb32(pb);
767 const int den = avio_rb32(pb);
6a63ff19
BC
768 AVStream *st;
769
770 if (c->fc->nb_streams < 1)
771 return 0;
772 st = c->fc->streams[c->fc->nb_streams-1];
773
9044dd83
BC
774 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
775 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
776 av_log(c->fc, AV_LOG_WARNING,
777 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
778 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
779 num, den);
780 } else if (den != 0) {
6da54074
BC
781 st->sample_aspect_ratio.num = num;
782 st->sample_aspect_ratio.den = den;
783 }
784 return 0;
785}
786
5ca1d879 787/* this atom contains actual media data */
ae628ec1 788static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6cea494e 789{
5a7ba586 790 if (atom.size == 0) /* wrong one (MP4) */
5ca1d879
ZK
791 return 0;
792 c->found_mdat=1;
5ca1d879
ZK
793 return 0; /* now go for moov */
794}
795
7b5252ce 796/* read major brand, minor version and compatible brands and store them as metadata */
ae628ec1 797static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4ea28253 798{
7b5252ce 799 uint32_t minor_ver;
800 int comp_brand_size;
801 char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
802 char* comp_brands_str;
803 uint8_t type[5] = {0};
5c720657
AC
804 int ret = ffio_read_size(pb, type, 4);
805 if (ret < 0)
806 return ret;
7b5252ce 807
a42bf191 808 if (strcmp(type, "qt "))
152e9a43 809 c->isom = 1;
a512446e 810 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
d2d67e42 811 av_dict_set(&c->fc->metadata, "major_brand", type, 0);
b7effd4e 812 minor_ver = avio_rb32(pb); /* minor version */
d92024f1 813 snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
d2d67e42 814 av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
7b5252ce 815
816 comp_brand_size = atom.size - 8;
817 if (comp_brand_size < 0)
bcd5d979 818 return AVERROR_INVALIDDATA;
7b5252ce 819 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
820 if (!comp_brands_str)
821 return AVERROR(ENOMEM);
5c720657
AC
822
823 ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
824 if (ret < 0) {
825 av_freep(&comp_brands_str);
826 return ret;
827 }
7b5252ce 828 comp_brands_str[comp_brand_size] = 0;
d2d67e42 829 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
7b5252ce 830 av_freep(&comp_brands_str);
831
4ea28253
BC
832 return 0;
833}
834
5ca1d879 835/* this atom should contain all header atoms */
ae628ec1 836static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879 837{
bcd5d979
DH
838 int ret;
839
840 if ((ret = mov_read_default(c, pb, atom)) < 0)
841 return ret;
5ca1d879
ZK
842 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
843 /* so we don't parse the whole file if over a network */
844 c->found_moov=1;
5ca1d879
ZK
845 return 0; /* now go for mdat */
846}
847
ae628ec1 848static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
61aedb0f 849{
20f95f21 850 c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1a3eb042 851 av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
61aedb0f
BC
852 return mov_read_default(c, pb, atom);
853}
5ca1d879 854
d2d67e42 855static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
7df22143
AJ
856{
857 char buffer[32];
858 if (time) {
82ee7d0d 859 struct tm *ptm, tmbuf;
7df22143 860 time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
82ee7d0d 861 ptm = gmtime_r(&time, &tmbuf);
5e2202d6 862 if (!ptm) return;
9dcf2397
MS
863 if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
864 av_dict_set(metadata, "creation_time", buffer, 0);
7df22143
AJ
865 }
866}
867
ae628ec1 868static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879 869{
6a63ff19
BC
870 AVStream *st;
871 MOVStreamContext *sc;
872 int version;
49674dd0 873 char language[4] = {0};
dce25564 874 unsigned lang;
7df22143 875 time_t creation_time;
5ca1d879 876
6a63ff19
BC
877 if (c->fc->nb_streams < 1)
878 return 0;
879 st = c->fc->streams[c->fc->nb_streams-1];
880 sc = st->priv_data;
881
31931520
MS
882 if (sc->time_scale) {
883 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
884 return AVERROR_INVALIDDATA;
885 }
886
b7effd4e 887 version = avio_r8(pb);
bcd5d979 888 if (version > 1) {
1ecdf891 889 avpriv_request_sample(c->fc, "Version %d", version);
bcd5d979
DH
890 return AVERROR_PATCHWELCOME;
891 }
b7effd4e 892 avio_rb24(pb); /* flags */
f444b977 893 if (version == 1) {
b7effd4e
AK
894 creation_time = avio_rb64(pb);
895 avio_rb64(pb);
f444b977 896 } else {
b7effd4e
AK
897 creation_time = avio_rb32(pb);
898 avio_rb32(pb); /* modification time */
f444b977 899 }
7df22143 900 mov_metadata_creation_time(&st->metadata, creation_time);
5ca1d879 901
b7effd4e
AK
902 sc->time_scale = avio_rb32(pb);
903 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
5ca1d879 904
b7effd4e 905 lang = avio_rb16(pb); /* language */
49674dd0 906 if (ff_mov_lang_to_iso639(lang, language))
d2d67e42 907 av_dict_set(&st->metadata, "language", language, 0);
b7effd4e 908 avio_rb16(pb); /* quality */
5ca1d879
ZK
909
910 return 0;
911}
912
ae628ec1 913static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879 914{
7df22143 915 time_t creation_time;
b7effd4e
AK
916 int version = avio_r8(pb); /* version */
917 avio_rb24(pb); /* flags */
5ca1d879 918
1175561e 919 if (version == 1) {
b7effd4e
AK
920 creation_time = avio_rb64(pb);
921 avio_rb64(pb);
1175561e 922 } else {
b7effd4e
AK
923 creation_time = avio_rb32(pb);
924 avio_rb32(pb); /* modification time */
1175561e 925 }
7df22143 926 mov_metadata_creation_time(&c->fc->metadata, creation_time);
b7effd4e 927 c->time_scale = avio_rb32(pb); /* time scale */
f8c18cd7 928
1a3eb042 929 av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
f8c18cd7 930
b7effd4e
AK
931 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
932 avio_rb32(pb); /* preferred scale */
5ca1d879 933
b7effd4e 934 avio_rb16(pb); /* preferred volume */
5ca1d879 935
45a8a02a 936 avio_skip(pb, 10); /* reserved */
5ca1d879 937
45a8a02a 938 avio_skip(pb, 36); /* display matrix */
5ca1d879 939
b7effd4e
AK
940 avio_rb32(pb); /* preview time */
941 avio_rb32(pb); /* preview duration */
942 avio_rb32(pb); /* poster time */
943 avio_rb32(pb); /* selection time */
944 avio_rb32(pb); /* selection duration */
945 avio_rb32(pb); /* current time */
946 avio_rb32(pb); /* next track ID */
5ca1d879
ZK
947
948 return 0;
949}
950
ae628ec1 951static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
9ed83b0a 952{
6a63ff19 953 AVStream *st;
5c720657 954 int ret;
6a63ff19
BC
955
956 if (c->fc->nb_streams < 1)
957 return 0;
958 st = c->fc->streams[c->fc->nb_streams-1];
9ed83b0a 959
5a7ba586 960 if ((uint64_t)atom.size > (1<<30))
bcd5d979 961 return AVERROR_INVALIDDATA;
115329f1 962
9ed83b0a
ZK
963 // currently SVQ3 decoder expect full STSD header - so let's fake it
964 // this should be fixed and just SMI header should be passed
01f4895c 965 av_free(st->codec->extradata);
059a9348 966 st->codec->extradata = av_mallocz(atom.size + 0x5a + AV_INPUT_BUFFER_PADDING_SIZE);
b014dd76
BC
967 if (!st->codec->extradata)
968 return AVERROR(ENOMEM);
17871a02
BC
969 st->codec->extradata_size = 0x5a + atom.size;
970 memcpy(st->codec->extradata, "SVQ3", 4); // fake
5c720657
AC
971
972 ret = ffio_read_size(pb, st->codec->extradata + 0x5a, atom.size);
973 if (ret < 0)
974 return ret;
975
1a3eb042 976 av_log(c->fc, AV_LOG_TRACE, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
9ed83b0a
ZK
977 return 0;
978}
5ca1d879 979
ae628ec1 980static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
de23f234 981{
6a63ff19
BC
982 AVStream *st;
983 int little_endian;
984
985 if (c->fc->nb_streams < 1)
986 return 0;
987 st = c->fc->streams[c->fc->nb_streams-1];
de23f234 988
423f5d50 989 little_endian = !!avio_rb16(pb);
1a3eb042 990 av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
fa50a027 991 if (little_endian == 1) {
de23f234 992 switch (st->codec->codec_id) {
36ef5369
AK
993 case AV_CODEC_ID_PCM_S24BE:
994 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
de23f234 995 break;
36ef5369
AK
996 case AV_CODEC_ID_PCM_S32BE:
997 st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
de23f234 998 break;
36ef5369
AK
999 case AV_CODEC_ID_PCM_F32BE:
1000 st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
a1ef2c4b 1001 break;
36ef5369
AK
1002 case AV_CODEC_ID_PCM_F64BE:
1003 st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
a1ef2c4b 1004 break;
de23f234
BC
1005 default:
1006 break;
1007 }
1008 }
1009 return 0;
1010}
1011
0d8a3656
VG
1012static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1013{
1014 AVStream *st;
1015 char color_parameter_type[5] = { 0 };
2eef75fd 1016 uint16_t color_primaries, color_trc, color_matrix;
5c720657 1017 int ret;
0d8a3656
VG
1018
1019 if (c->fc->nb_streams < 1)
1020 return 0;
1021 st = c->fc->streams[c->fc->nb_streams - 1];
1022
5c720657
AC
1023 ret = ffio_read_size(pb, color_parameter_type, 4);
1024 if (ret < 0)
1025 return ret;
0d8a3656
VG
1026 if (strncmp(color_parameter_type, "nclx", 4) &&
1027 strncmp(color_parameter_type, "nclc", 4)) {
1028 av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1029 color_parameter_type);
1030 return 0;
1031 }
1032
1033 color_primaries = avio_rb16(pb);
1034 color_trc = avio_rb16(pb);
1035 color_matrix = avio_rb16(pb);
1036
1a3eb042
VG
1037 av_log(c->fc, AV_LOG_TRACE,
1038 "%s: pri %"PRIu16" trc %"PRIu16" matrix %"PRIu16"",
1039 color_parameter_type, color_primaries, color_trc, color_matrix);
0d8a3656 1040
be089af3 1041 if (!strncmp(color_parameter_type, "nclx", 4)) {
0d8a3656 1042 uint8_t color_range = avio_r8(pb) >> 7;
1a3eb042 1043 av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
0d8a3656
VG
1044 if (color_range)
1045 st->codec->color_range = AVCOL_RANGE_JPEG;
1046 else
1047 st->codec->color_range = AVCOL_RANGE_MPEG;
1048 /* 14496-12 references JPEG XR specs (rather than the more complete
1049 * 23001-8) so some adjusting is required */
1050 if (color_primaries >= AVCOL_PRI_FILM)
1051 color_primaries = AVCOL_PRI_UNSPECIFIED;
74b02377
VG
1052 if ((color_trc >= AVCOL_TRC_LINEAR &&
1053 color_trc <= AVCOL_TRC_LOG_SQRT) ||
0d8a3656
VG
1054 color_trc >= AVCOL_TRC_BT2020_10)
1055 color_trc = AVCOL_TRC_UNSPECIFIED;
1056 if (color_matrix >= AVCOL_SPC_BT2020_NCL)
1057 color_matrix = AVCOL_SPC_UNSPECIFIED;
1058 st->codec->color_primaries = color_primaries;
1059 st->codec->color_trc = color_trc;
1060 st->codec->colorspace = color_matrix;
be089af3 1061 } else if (!strncmp(color_parameter_type, "nclc", 4)) {
0d8a3656
VG
1062 /* color primaries, Table 4-4 */
1063 switch (color_primaries) {
1064 case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break;
1065 case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break;
1066 case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break;
1067 }
1068 /* color transfer, Table 4-5 */
1069 switch (color_trc) {
1070 case 1: st->codec->color_trc = AVCOL_TRC_BT709; break;
1071 case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break;
1072 }
1073 /* color matrix, Table 4-6 */
1074 switch (color_matrix) {
1075 case 1: st->codec->colorspace = AVCOL_SPC_BT709; break;
1076 case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break;
1077 case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break;
1078 }
1079 }
1a3eb042 1080 av_log(c->fc, AV_LOG_TRACE, "\n");
0d8a3656
VG
1081
1082 return 0;
1083}
1084
4bf3c8f2
AC
1085static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1086{
1087 AVStream *st;
1088 unsigned mov_field_order;
1089 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1090
1091 if (c->fc->nb_streams < 1) // will happen with jp2 files
1092 return 0;
1093 st = c->fc->streams[c->fc->nb_streams-1];
1094 if (atom.size < 2)
1095 return AVERROR_INVALIDDATA;
1096 mov_field_order = avio_rb16(pb);
1097 if ((mov_field_order & 0xFF00) == 0x0100)
1098 decoded_field_order = AV_FIELD_PROGRESSIVE;
1099 else if ((mov_field_order & 0xFF00) == 0x0200) {
1100 switch (mov_field_order & 0xFF) {
1101 case 0x01: decoded_field_order = AV_FIELD_TT;
1102 break;
1103 case 0x06: decoded_field_order = AV_FIELD_BB;
1104 break;
1105 case 0x09: decoded_field_order = AV_FIELD_TB;
1106 break;
1107 case 0x0E: decoded_field_order = AV_FIELD_BT;
1108 break;
1109 }
1110 }
1111 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1112 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1113 }
1114 st->codec->field_order = decoded_field_order;
1115
1116 return 0;
1117}
1118
014a5102 1119/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
ae628ec1 1120static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
de23f234 1121{
27134437
BC
1122 AVStream *st;
1123 uint64_t size;
68bc33fa 1124 uint8_t *buf;
5626f994 1125 int err;
27134437
BC
1126
1127 if (c->fc->nb_streams < 1) // will happen with jp2 files
1128 return 0;
1129 st= c->fc->streams[c->fc->nb_streams-1];
059a9348 1130 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
5a7ba586 1131 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
bcd5d979 1132 return AVERROR_INVALIDDATA;
d872fb0f
MS
1133 if ((err = av_reallocp(&st->codec->extradata, size)) < 0) {
1134 st->codec->extradata_size = 0;
5626f994 1135 return err;
d872fb0f 1136 }
5626f994 1137 buf = st->codec->extradata + st->codec->extradata_size;
059a9348 1138 st->codec->extradata_size= size - AV_INPUT_BUFFER_PADDING_SIZE;
68bc33fa
BC
1139 AV_WB32( buf , atom.size + 8);
1140 AV_WL32( buf + 4, atom.type);
5c720657
AC
1141
1142 err = ffio_read_size(pb, buf + 8, atom.size);
1143 if (err < 0)
1144 return err;
1145
de23f234
BC
1146 return 0;
1147}
1148
ae628ec1 1149static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
d9b1c197 1150{
6a63ff19 1151 AVStream *st;
5c720657 1152 int ret;
6a63ff19
BC
1153
1154 if (c->fc->nb_streams < 1)
1155 return 0;
1156 st = c->fc->streams[c->fc->nb_streams-1];
d9b1c197 1157
5a7ba586 1158 if ((uint64_t)atom.size > (1<<30))
bcd5d979 1159 return AVERROR_INVALIDDATA;
115329f1 1160
36ef5369 1161 if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) {
ed19fafd 1162 // pass all frma atom to codec, needed at least for QDMC and QDM2
3840147e 1163 av_free(st->codec->extradata);
059a9348 1164 st->codec->extradata = av_mallocz(atom.size + AV_INPUT_BUFFER_PADDING_SIZE);
b014dd76
BC
1165 if (!st->codec->extradata)
1166 return AVERROR(ENOMEM);
3840147e 1167 st->codec->extradata_size = atom.size;
5c720657
AC
1168
1169 ret = ffio_read_size(pb, st->codec->extradata, atom.size);
1170 if (ret < 0)
1171 return ret;
3840147e 1172 } else if (atom.size > 8) { /* to read frma, esds atoms */
bcd5d979
DH
1173 if ((ret = mov_read_default(c, pb, atom)) < 0)
1174 return ret;
5c72cad8 1175 } else
45a8a02a 1176 avio_skip(pb, atom.size);
d9b1c197
RT
1177 return 0;
1178}
1179
bde24601
BC
1180/**
1181 * This function reads atom content and puts data in extradata without tag
1182 * nor size unlike mov_read_extradata.
1183 */
ae628ec1 1184static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
169eb021 1185{
6a63ff19 1186 AVStream *st;
5c720657 1187 int ret;
6a63ff19
BC
1188
1189 if (c->fc->nb_streams < 1)
1190 return 0;
1191 st = c->fc->streams[c->fc->nb_streams-1];
169eb021 1192
5a7ba586 1193 if ((uint64_t)atom.size > (1<<30))
bcd5d979 1194 return AVERROR_INVALIDDATA;
568e18b1 1195
4bf3c8f2 1196 if (atom.size >= 10) {
59cbc4ee 1197 // Broken files created by legacy versions of libavformat will
4bf3c8f2
AC
1198 // wrap a whole fiel atom inside of a glbl atom.
1199 unsigned size = avio_rb32(pb);
1200 unsigned type = avio_rl32(pb);
1201 avio_seek(pb, -8, SEEK_CUR);
1202 if (type == MKTAG('f','i','e','l') && size == atom.size)
1203 return mov_read_default(c, pb, atom);
1204 }
01f4895c 1205 av_free(st->codec->extradata);
059a9348 1206 st->codec->extradata = av_mallocz(atom.size + AV_INPUT_BUFFER_PADDING_SIZE);
b014dd76
BC
1207 if (!st->codec->extradata)
1208 return AVERROR(ENOMEM);
01f4895c 1209 st->codec->extradata_size = atom.size;
5c720657
AC
1210
1211 ret = ffio_read_size(pb, st->codec->extradata, atom.size);
1212 if (ret < 0)
1213 return ret;
1214
169eb021
MM
1215 return 0;
1216}
1217
89415b8e
MS
1218static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1219{
1220 AVStream *st;
1221 uint8_t profile_level;
5c720657 1222 int ret;
89415b8e
MS
1223
1224 if (c->fc->nb_streams < 1)
1225 return 0;
1226 st = c->fc->streams[c->fc->nb_streams-1];
1227
1228 if (atom.size >= (1<<28) || atom.size < 7)
1229 return AVERROR_INVALIDDATA;
1230
1231 profile_level = avio_r8(pb);
f1f37c70 1232 if ((profile_level & 0xf0) != 0xc0)
89415b8e
MS
1233 return 0;
1234
1235 av_free(st->codec->extradata);
059a9348 1236 st->codec->extradata = av_mallocz(atom.size - 7 + AV_INPUT_BUFFER_PADDING_SIZE);
89415b8e
MS
1237 if (!st->codec->extradata)
1238 return AVERROR(ENOMEM);
1239 st->codec->extradata_size = atom.size - 7;
1240 avio_seek(pb, 6, SEEK_CUR);
5c720657
AC
1241
1242 ret = ffio_read_size(pb, st->codec->extradata, st->codec->extradata_size);
1243 if (ret < 0)
1244 return ret;
1245
89415b8e
MS
1246 return 0;
1247}
1248
653d7aeb
MS
1249/**
1250 * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1251 * but can have extradata appended at the end after the 40 bytes belonging
1252 * to the struct.
1253 */
ae628ec1 1254static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
653d7aeb
MS
1255{
1256 AVStream *st;
5c720657 1257 int ret;
653d7aeb
MS
1258
1259 if (c->fc->nb_streams < 1)
1260 return 0;
1261 if (atom.size <= 40)
1262 return 0;
1263 st = c->fc->streams[c->fc->nb_streams-1];
1264
5a7ba586 1265 if ((uint64_t)atom.size > (1<<30))
bcd5d979 1266 return AVERROR_INVALIDDATA;
653d7aeb
MS
1267
1268 av_free(st->codec->extradata);
059a9348 1269 st->codec->extradata = av_mallocz(atom.size - 40 + AV_INPUT_BUFFER_PADDING_SIZE);
653d7aeb
MS
1270 if (!st->codec->extradata)
1271 return AVERROR(ENOMEM);
1272 st->codec->extradata_size = atom.size - 40;
45a8a02a 1273 avio_skip(pb, 40);
5c720657
AC
1274
1275 ret = ffio_read_size(pb, st->codec->extradata, atom.size - 40);
1276 if (ret < 0)
1277 return ret;
1278
653d7aeb
MS
1279 return 0;
1280}
1281
ae628ec1 1282static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879 1283{
6a63ff19
BC
1284 AVStream *st;
1285 MOVStreamContext *sc;
568e18b1 1286 unsigned int i, entries;
5ca1d879 1287
6a63ff19
BC
1288 if (c->fc->nb_streams < 1)
1289 return 0;
1290 st = c->fc->streams[c->fc->nb_streams-1];
1291 sc = st->priv_data;
1292
b7effd4e
AK
1293 avio_r8(pb); /* version */
1294 avio_rb24(pb); /* flags */
5ca1d879 1295
b7effd4e 1296 entries = avio_rb32(pb);
115329f1 1297
52401b82
AC
1298 if (!entries)
1299 return 0;
5a7ba586 1300 if (entries >= UINT_MAX/sizeof(int64_t))
bcd5d979 1301 return AVERROR_INVALIDDATA;
115329f1 1302
9a630c25 1303 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
5ca1d879 1304 if (!sc->chunk_offsets)
0d8f0abf
BC
1305 return AVERROR(ENOMEM);
1306 sc->chunk_count = entries;
1307
37ffe34b 1308 if (atom.type == MKTAG('s','t','c','o'))
9888ffb1 1309 for (i = 0; i < entries && !pb->eof_reached; i++)
b7effd4e 1310 sc->chunk_offsets[i] = avio_rb32(pb);
37ffe34b 1311 else if (atom.type == MKTAG('c','o','6','4'))
9888ffb1 1312 for (i = 0; i < entries && !pb->eof_reached; i++)
b7effd4e 1313 sc->chunk_offsets[i] = avio_rb64(pb);
37ffe34b 1314 else
bcd5d979 1315 return AVERROR_INVALIDDATA;
115329f1 1316
9888ffb1
LB
1317 sc->chunk_count = i;
1318
1319 if (pb->eof_reached)
1320 return AVERROR_EOF;
1321
5ca1d879
ZK
1322 return 0;
1323}
1324
2288834f
BC
1325/**
1326 * Compute codec id for 'lpcm' tag.
1327 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1328 */
36ef5369 1329enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
2288834f 1330{
261e9348
JR
1331 /* lpcm flags:
1332 * 0x1 = float
1333 * 0x2 = big-endian
1334 * 0x4 = signed
1335 */
1336 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
2288834f
BC
1337}
1338
bf985625
LB
1339static int mov_codec_id(AVStream *st, uint32_t format)
1340{
1341 int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1342
1343 if (id <= 0 &&
1344 ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1345 (format & 0xFFFF) == 'T' + ('S' << 8)))
1346 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1347
1348 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1349 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1350 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1351 /* skip old asf mpeg4 tag */
1352 format && format != MKTAG('m','p','4','s')) {
1353 id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1354 if (id <= 0)
1355 id = ff_codec_get_id(ff_codec_bmp_tags, format);
1356 if (id > 0)
1357 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1358 else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
1359 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1360 if (id > 0)
1361 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1362 }
1363 }
1364
1365 st->codec->codec_tag = format;
1366
1367 return id;
1368}
1369
a3b53ff0
LB
1370static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1371 AVStream *st, MOVStreamContext *sc)
1372{
18f2514c 1373 uint8_t codec_name[32];
a3b53ff0
LB
1374 unsigned int color_depth, len, j;
1375 int color_greyscale;
1376 int color_table_id;
1377
1378 avio_rb16(pb); /* version */
1379 avio_rb16(pb); /* revision level */
1380 avio_rb32(pb); /* vendor */
1381 avio_rb32(pb); /* temporal quality */
1382 avio_rb32(pb); /* spatial quality */
1383
1384 st->codec->width = avio_rb16(pb); /* width */
1385 st->codec->height = avio_rb16(pb); /* height */
1386
1387 avio_rb32(pb); /* horiz resolution */
1388 avio_rb32(pb); /* vert resolution */
1389 avio_rb32(pb); /* data size, always 0 */
1390 avio_rb16(pb); /* frames per samples */
1391
1392 len = avio_r8(pb); /* codec name, pascal string */
1393 if (len > 31)
1394 len = 31;
18f2514c 1395 mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
a3b53ff0
LB
1396 if (len < 31)
1397 avio_skip(pb, 31 - len);
18f2514c
AK
1398
1399 if (codec_name[0])
1400 av_dict_set(&st->metadata, "encoder", codec_name, 0);
1401
a3b53ff0 1402 /* codec_tag YV12 triggers an UV swap in rawdec.c */
18f2514c 1403 if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
a3b53ff0
LB
1404 st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1405 /* Flash Media Server uses tag H263 with Sorenson Spark */
1406 if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
18f2514c 1407 !memcmp(codec_name, "Sorenson H263", 13))
a3b53ff0
LB
1408 st->codec->codec_id = AV_CODEC_ID_FLV1;
1409
1410 st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1411 color_table_id = avio_rb16(pb); /* colortable id */
1a3eb042 1412 av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
a3b53ff0
LB
1413 st->codec->bits_per_coded_sample, color_table_id);
1414 /* figure out the palette situation */
1415 color_depth = st->codec->bits_per_coded_sample & 0x1F;
1416 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1417
1418 /* if the depth is 2, 4, or 8 bpp, file is palettized */
1419 if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1420 /* for palette traversal */
1421 unsigned int color_start, color_count, color_end;
1422 unsigned char r, g, b;
1423
1424 if (color_greyscale) {
1425 int color_index, color_dec;
1426 /* compute the greyscale palette */
1427 st->codec->bits_per_coded_sample = color_depth;
1428 color_count = 1 << color_depth;
1429 color_index = 255;
1430 color_dec = 256 / (color_count - 1);
1431 for (j = 0; j < color_count; j++) {
1432 r = g = b = color_index;
1433 sc->palette[j] = (r << 16) | (g << 8) | (b);
1434 color_index -= color_dec;
1435 if (color_index < 0)
1436 color_index = 0;
1437 }
1438 } else if (color_table_id) {
1439 const uint8_t *color_table;
1440 /* if flag bit 3 is set, use the default palette */
1441 color_count = 1 << color_depth;
1442 if (color_depth == 2)
1443 color_table = ff_qt_default_palette_4;
1444 else if (color_depth == 4)
1445 color_table = ff_qt_default_palette_16;
1446 else
1447 color_table = ff_qt_default_palette_256;
1448
1449 for (j = 0; j < color_count; j++) {
1450 r = color_table[j * 3 + 0];
1451 g = color_table[j * 3 + 1];
1452 b = color_table[j * 3 + 2];
1453 sc->palette[j] = (r << 16) | (g << 8) | (b);
1454 }
1455 } else {
1456 /* load the palette from the file */
1457 color_start = avio_rb32(pb);
1458 color_count = avio_rb16(pb);
1459 color_end = avio_rb16(pb);
1460 if ((color_start <= 255) && (color_end <= 255)) {
1461 for (j = color_start; j <= color_end; j++) {
1462 /* each R, G, or B component is 16 bits;
1463 * only use the top 8 bits; skip alpha bytes
1464 * up front */
1465 avio_r8(pb);
1466 avio_r8(pb);
1467 r = avio_r8(pb);
1468 avio_r8(pb);
1469 g = avio_r8(pb);
1470 avio_r8(pb);
1471 b = avio_r8(pb);
1472 avio_r8(pb);
1473 sc->palette[j] = (r << 16) | (g << 8) | (b);
1474 }
1475 }
1476 }
1477 sc->has_palette = 1;
1478 }
1479}
1480
ef196bee
LB
1481static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1482 AVStream *st, MOVStreamContext *sc)
1483{
1484 int bits_per_sample, flags;
1485 uint16_t version = avio_rb16(pb);
1486
1487 avio_rb16(pb); /* revision level */
1488 avio_rb32(pb); /* vendor */
1489
1490 st->codec->channels = avio_rb16(pb); /* channel count */
1491 st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1a3eb042 1492 av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels);
ef196bee
LB
1493
1494 sc->audio_cid = avio_rb16(pb);
1495 avio_rb16(pb); /* packet size = 0 */
1496
1497 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1498
1499 // Read QT version 1 fields. In version 0 these do not exist.
1a3eb042 1500 av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
ef196bee
LB
1501 if (!c->isom) {
1502 if (version == 1) {
1503 sc->samples_per_frame = avio_rb32(pb);
1504 avio_rb32(pb); /* bytes per packet */
1505 sc->bytes_per_frame = avio_rb32(pb);
1506 avio_rb32(pb); /* bytes per sample */
1507 } else if (version == 2) {
1508 avio_rb32(pb); /* sizeof struct only */
1509 st->codec->sample_rate = av_int2double(avio_rb64(pb));
1510 st->codec->channels = avio_rb32(pb);
1511 avio_rb32(pb); /* always 0x7F000000 */
1512 st->codec->bits_per_coded_sample = avio_rb32(pb);
1513
1514 flags = avio_rb32(pb); /* lpcm format specific flag */
1515 sc->bytes_per_frame = avio_rb32(pb);
1516 sc->samples_per_frame = avio_rb32(pb);
1517 if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1518 st->codec->codec_id =
1519 ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
1520 flags);
1521 }
6ec688e1 1522 if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1523 /* can't correctly handle variable sized packet as audio unit */
1524 switch (st->codec->codec_id) {
1525 case AV_CODEC_ID_MP2:
1526 case AV_CODEC_ID_MP3:
1527 st->need_parsing = AVSTREAM_PARSE_FULL;
1528 break;
1529 }
1530 }
ef196bee
LB
1531 }
1532
1533 switch (st->codec->codec_id) {
1534 case AV_CODEC_ID_PCM_S8:
1535 case AV_CODEC_ID_PCM_U8:
1536 if (st->codec->bits_per_coded_sample == 16)
1537 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
1538 break;
1539 case AV_CODEC_ID_PCM_S16LE:
1540 case AV_CODEC_ID_PCM_S16BE:
1541 if (st->codec->bits_per_coded_sample == 8)
1542 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
1543 else if (st->codec->bits_per_coded_sample == 24)
1544 st->codec->codec_id =
1545 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1546 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1547 break;
1548 /* set values for old format before stsd version 1 appeared */
1549 case AV_CODEC_ID_MACE3:
1550 sc->samples_per_frame = 6;
1551 sc->bytes_per_frame = 2 * st->codec->channels;
1552 break;
1553 case AV_CODEC_ID_MACE6:
1554 sc->samples_per_frame = 6;
1555 sc->bytes_per_frame = 1 * st->codec->channels;
1556 break;
1557 case AV_CODEC_ID_ADPCM_IMA_QT:
1558 sc->samples_per_frame = 64;
1559 sc->bytes_per_frame = 34 * st->codec->channels;
1560 break;
1561 case AV_CODEC_ID_GSM:
1562 sc->samples_per_frame = 160;
1563 sc->bytes_per_frame = 33;
1564 break;
1565 default:
1566 break;
1567 }
1568
1569 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1570 if (bits_per_sample) {
1571 st->codec->bits_per_coded_sample = bits_per_sample;
1572 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1573 }
1574}
1575
dc518a3a
LB
1576static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1577 AVStream *st, MOVStreamContext *sc,
1578 int size)
1579{
1580 // ttxt stsd contains display flags, justification, background
1581 // color, fonts, and default styles, so fake an atom to read it
1582 MOVAtom fake_atom = { .size = size };
1583 // mp4s contains a regular esds atom
1584 if (st->codec->codec_tag != AV_RL32("mp4s"))
1585 mov_read_glbl(c, pb, fake_atom);
1586 st->codec->width = sc->width;
1587 st->codec->height = sc->height;
1588}
1589
12789d96
MN
1590static uint32_t yuv_to_rgba(uint32_t ycbcr)
1591{
1592 uint8_t r, g, b;
1593 int y, cb, cr;
1594
1595 y = (ycbcr >> 16) & 0xFF;
1596 cr = (ycbcr >> 8) & 0xFF;
1597 cb = ycbcr & 0xFF;
1598
1599 b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128));
1600 g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
1601 r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
1602
1603 return (r << 16) | (g << 8) | b;
1604}
1605
1606static int mov_rewrite_dvd_sub_extradata(AVStream *st)
1607{
1608 char buf[256] = {0};
1609 uint8_t *src = st->codec->extradata;
1610 int i;
1611
1612 if (st->codec->extradata_size != 64)
1613 return 0;
1614
1615 if (st->codec->width > 0 && st->codec->height > 0)
1616 snprintf(buf, sizeof(buf), "size: %dx%d\n",
1617 st->codec->width, st->codec->height);
1618 av_strlcat(buf, "palette: ", sizeof(buf));
1619
1620 for (i = 0; i < 16; i++) {
1621 uint32_t yuv = AV_RB32(src + i * 4);
1622 uint32_t rgba = yuv_to_rgba(yuv);
1623
cba4e606 1624 av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
12789d96
MN
1625 }
1626
1627 if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1628 return 0;
1629
1630 av_freep(&st->codec->extradata);
1631 st->codec->extradata_size = 0;
059a9348 1632 st->codec->extradata = av_mallocz(strlen(buf) + AV_INPUT_BUFFER_PADDING_SIZE);
12789d96
MN
1633 if (!st->codec->extradata)
1634 return AVERROR(ENOMEM);
1635 st->codec->extradata_size = strlen(buf);
1636 memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1637
1638 return 0;
1639}
1640
5b41eb91
LB
1641static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
1642 AVStream *st, MOVStreamContext *sc,
1643 int size)
1644{
5c720657
AC
1645 int ret;
1646
5b41eb91
LB
1647 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1648 st->codec->extradata_size = size;
059a9348 1649 st->codec->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
5b41eb91
LB
1650 if (!st->codec->extradata)
1651 return AVERROR(ENOMEM);
5c720657
AC
1652 ret = ffio_read_size(pb, st->codec->extradata, size);
1653 if (ret < 0)
1654 return ret;
5b41eb91
LB
1655 } else {
1656 /* other codec type, just skip (rtp, mp4s ...) */
1657 avio_skip(pb, size);
1658 }
1659 return 0;
1660}
1661
08504380
LB
1662static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
1663 AVStream *st, MOVStreamContext *sc)
1664{
1665 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1666 !st->codec->sample_rate && sc->time_scale > 1)
1667 st->codec->sample_rate = sc->time_scale;
1668
1669 /* special codec parameters handling */
1670 switch (st->codec->codec_id) {
1671#if CONFIG_DV_DEMUXER
1672 case AV_CODEC_ID_DVAUDIO:
6308cd48
VG
1673 c->dv_fctx = avformat_alloc_context();
1674 if (!c->dv_fctx) {
1675 av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
1676 return AVERROR(ENOMEM);
1677 }
08504380
LB
1678 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
1679 if (!c->dv_demux) {
1680 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1681 return AVERROR(ENOMEM);
1682 }
1683 sc->dv_audio_container = 1;
1684 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
1685 break;
1686#endif
1687 /* no ifdef since parameters are always those */
1688 case AV_CODEC_ID_QCELP:
1689 st->codec->channels = 1;
1690 // force sample rate for qcelp when not stored in mov
1691 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
1692 st->codec->sample_rate = 8000;
1693 break;
1694 case AV_CODEC_ID_AMR_NB:
1695 st->codec->channels = 1;
1696 /* force sample rate for amr, stsd in 3gp does not store sample rate */
1697 st->codec->sample_rate = 8000;
1698 break;
1699 case AV_CODEC_ID_AMR_WB:
1700 st->codec->channels = 1;
1701 st->codec->sample_rate = 16000;
1702 break;
1703 case AV_CODEC_ID_MP2:
1704 case AV_CODEC_ID_MP3:
1705 /* force type after stsd for m1a hdlr */
1706 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
08504380
LB
1707 break;
1708 case AV_CODEC_ID_GSM:
1709 case AV_CODEC_ID_ADPCM_MS:
1710 case AV_CODEC_ID_ADPCM_IMA_WAV:
1711 case AV_CODEC_ID_ILBC:
1712 st->codec->block_align = sc->bytes_per_frame;
1713 break;
1714 case AV_CODEC_ID_ALAC:
1715 if (st->codec->extradata_size == 36) {
1716 st->codec->channels = AV_RB8 (st->codec->extradata + 21);
1717 st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
1718 }
1719 break;
1720 case AV_CODEC_ID_VC1:
1721 st->need_parsing = AVSTREAM_PARSE_FULL;
1722 break;
1723 default:
1724 break;
1725 }
1726 return 0;
1727}
1728
0b5af5cf
LB
1729static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1730 int codec_tag, int format,
1731 int size)
1732{
1733 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1734
1735 if (codec_tag &&
1736 (codec_tag == AV_RL32("avc1") ||
ea29f965
YN
1737 codec_tag == AV_RL32("hvc1") ||
1738 codec_tag == AV_RL32("hev1") ||
0b5af5cf
LB
1739 (codec_tag != format &&
1740 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1741 : codec_tag != MKTAG('j','p','e','g'))))) {
1742 /* Multiple fourcc, we skip JPEG. This is not correct, we should
1743 * export it as a separate AVStream but this needs a few changes
1744 * in the MOV demuxer, patch welcome. */
1745
1746 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1747 avio_skip(pb, size);
1748 return 1;
1749 }
1750
1751 return 0;
1752}
1753
ae628ec1 1754int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
5ca1d879 1755{
6a63ff19
BC
1756 AVStream *st;
1757 MOVStreamContext *sc;
a3b53ff0 1758 int pseudo_stream_id;
b595afaa 1759
6a63ff19
BC
1760 if (c->fc->nb_streams < 1)
1761 return 0;
1762 st = c->fc->streams[c->fc->nb_streams-1];
1763 sc = st->priv_data;
1764
9888ffb1
LB
1765 for (pseudo_stream_id = 0;
1766 pseudo_stream_id < entries && !pb->eof_reached;
1767 pseudo_stream_id++) {
117a9190 1768 //Parsing Sample description table
36ef5369 1769 enum AVCodecID id;
5b41eb91 1770 int ret, dref_id = 1;
3491866a 1771 MOVAtom a = { AV_RL32("stsd") };
a2704c97 1772 int64_t start_pos = avio_tell(pb);
a5ea623b 1773 uint32_t size = avio_rb32(pb); /* size */
b7effd4e 1774 uint32_t format = avio_rl32(pb); /* data format */
5cd62665 1775
28155113 1776 if (size >= 16) {
b7effd4e
AK
1777 avio_rb32(pb); /* reserved */
1778 avio_rb16(pb); /* reserved */
1779 dref_id = avio_rb16(pb);
a5ea623b 1780 } else {
d92024f1 1781 av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRIu32" in stsd\n", size);
a5ea623b 1782 return AVERROR_INVALIDDATA;
28155113 1783 }
0e7eed09 1784
0b5af5cf
LB
1785 if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1786 size - (avio_tell(pb) - start_pos)))
e7cc4b52 1787 continue;
0b5af5cf 1788
978677a7 1789 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
221e21b7 1790 sc->dref_id= dref_id;
e7cc4b52 1791
bf985625 1792 id = mov_codec_id(st, format);
99487f42 1793
a1e2caa9
VG
1794 av_log(c->fc, AV_LOG_TRACE,
1795 "size=%"PRIu32" format=0x%08x codec_type=%d\n",
1796 size, format, st->codec->codec_type);
a20da52c 1797
5a7ba586 1798 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
01f4895c 1799 st->codec->codec_id = id;
a3b53ff0 1800 mov_parse_stsd_video(c, pb, st, sc);
5a7ba586 1801 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
99487f42 1802 st->codec->codec_id = id;
ef196bee 1803 mov_parse_stsd_audio(c, pb, st, sc);
5a7ba586 1804 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
dc518a3a
LB
1805 st->codec->codec_id = id;
1806 mov_parse_stsd_subtitle(c, pb, st, sc,
1807 size - (avio_tell(pb) - start_pos));
de23f234 1808 } else {
5b41eb91
LB
1809 ret = mov_parse_stsd_data(c, pb, st, sc,
1810 size - (avio_tell(pb) - start_pos));
1811 if (ret < 0)
1812 return ret;
6cea494e 1813 }
ea29f965 1814 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
a2704c97 1815 a.size = size - (avio_tell(pb) - start_pos);
9cf0419b 1816 if (a.size > 8) {
bcd5d979
DH
1817 if ((ret = mov_read_default(c, pb, a)) < 0)
1818 return ret;
9cf0419b 1819 } else if (a.size > 0)
45a8a02a 1820 avio_skip(pb, a.size);
6cea494e 1821 }
115329f1 1822
9888ffb1
LB
1823 if (pb->eof_reached)
1824 return AVERROR_EOF;
1825
08504380 1826 return mov_finalize_stsd_codec(c, pb, st, sc);
6cea494e
ZK
1827}
1828
ae628ec1 1829static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
dc2cabd0
MS
1830{
1831 int entries;
1832
b7effd4e
AK
1833 avio_r8(pb); /* version */
1834 avio_rb24(pb); /* flags */
1835 entries = avio_rb32(pb);
dc2cabd0
MS
1836
1837 return ff_mov_read_stsd_entries(c, pb, entries);
1838}
1839
ae628ec1 1840static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6cea494e 1841{
6a63ff19
BC
1842 AVStream *st;
1843 MOVStreamContext *sc;
568e18b1 1844 unsigned int i, entries;
b6a17df4 1845
6a63ff19
BC
1846 if (c->fc->nb_streams < 1)
1847 return 0;
1848 st = c->fc->streams[c->fc->nb_streams-1];
1849 sc = st->priv_data;
1850
b7effd4e
AK
1851 avio_r8(pb); /* version */
1852 avio_rb24(pb); /* flags */
6cea494e 1853
b7effd4e 1854 entries = avio_rb32(pb);
115329f1 1855
1a3eb042 1856 av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
f8c18cd7 1857
52401b82
AC
1858 if (!entries)
1859 return 0;
5a7ba586 1860 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
bcd5d979 1861 return AVERROR_INVALIDDATA;
04e06cfa
BC
1862 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1863 if (!sc->stsc_data)
0d8f0abf 1864 return AVERROR(ENOMEM);
0d8f0abf 1865
9888ffb1 1866 for (i = 0; i < entries && !pb->eof_reached; i++) {
b7effd4e
AK
1867 sc->stsc_data[i].first = avio_rb32(pb);
1868 sc->stsc_data[i].count = avio_rb32(pb);
1869 sc->stsc_data[i].id = avio_rb32(pb);
6cea494e 1870 }
9888ffb1
LB
1871
1872 sc->stsc_count = i;
1873
1874 if (pb->eof_reached)
1875 return AVERROR_EOF;
1876
6cea494e
ZK
1877 return 0;
1878}
1879
ae628ec1 1880static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
171a3531
BC
1881{
1882 AVStream *st;
1883 MOVStreamContext *sc;
1884 unsigned i, entries;
1885
1886 if (c->fc->nb_streams < 1)
1887 return 0;
1888 st = c->fc->streams[c->fc->nb_streams-1];
1889 sc = st->priv_data;
1890
b7effd4e 1891 avio_rb32(pb); // version + flags
171a3531 1892
b7effd4e 1893 entries = avio_rb32(pb);
171a3531 1894 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
bcd5d979 1895 return AVERROR_INVALIDDATA;
171a3531
BC
1896 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1897 if (!sc->stps_data)
1898 return AVERROR(ENOMEM);
171a3531 1899
9888ffb1 1900 for (i = 0; i < entries && !pb->eof_reached; i++) {
b7effd4e 1901 sc->stps_data[i] = avio_rb32(pb);
1a3eb042 1902 //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
171a3531
BC
1903 }
1904
9888ffb1
LB
1905 sc->stps_count = i;
1906
1907 if (pb->eof_reached)
1908 return AVERROR_EOF;
1909
171a3531
BC
1910 return 0;
1911}
1912
ae628ec1 1913static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
247d56f5 1914{
6a63ff19
BC
1915 AVStream *st;
1916 MOVStreamContext *sc;
568e18b1 1917 unsigned int i, entries;
247d56f5 1918
6a63ff19
BC
1919 if (c->fc->nb_streams < 1)
1920 return 0;
1921 st = c->fc->streams[c->fc->nb_streams-1];
1922 sc = st->priv_data;
1923
b7effd4e
AK
1924 avio_r8(pb); /* version */
1925 avio_rb24(pb); /* flags */
247d56f5 1926
b7effd4e 1927 entries = avio_rb32(pb);
115329f1 1928
1a3eb042 1929 av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
0d8f0abf 1930
29a20ac4 1931 if (!entries)
accea4d9
YN
1932 {
1933 sc->keyframe_absent = 1;
29a20ac4 1934 return 0;
accea4d9 1935 }
5a7ba586 1936 if (entries >= UINT_MAX / sizeof(int))
bcd5d979 1937 return AVERROR_INVALIDDATA;
64f7575f 1938 av_freep(&sc->keyframes);
1c02d96f 1939 sc->keyframes = av_malloc(entries * sizeof(int));
247d56f5 1940 if (!sc->keyframes)
0d8f0abf 1941 return AVERROR(ENOMEM);
0d8f0abf 1942
9888ffb1 1943 for (i = 0; i < entries && !pb->eof_reached; i++) {
b7effd4e 1944 sc->keyframes[i] = avio_rb32(pb);
1a3eb042 1945 //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
247d56f5 1946 }
9888ffb1
LB
1947
1948 sc->keyframe_count = i;
1949
1950 if (pb->eof_reached)
1951 return AVERROR_EOF;
1952
247d56f5
BB
1953 return 0;
1954}
1955
ae628ec1 1956static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
6cea494e 1957{
6a63ff19
BC
1958 AVStream *st;
1959 MOVStreamContext *sc;
bd27eed6
AC
1960 unsigned int i, entries, sample_size, field_size, num_bytes;
1961 GetBitContext gb;
1962 unsigned char* buf;
5c720657 1963 int ret;
b6a17df4 1964
6a63ff19
BC
1965 if (c->fc->nb_streams < 1)
1966 return 0;
1967 st = c->fc->streams[c->fc->nb_streams-1];
1968 sc = st->priv_data;
1969
b7effd4e
AK
1970 avio_r8(pb); /* version */
1971 avio_rb24(pb); /* flags */
5cd62665 1972
bd27eed6 1973 if (atom.type == MKTAG('s','t','s','z')) {
b7effd4e 1974 sample_size = avio_rb32(pb);
d0dab0ec
AC
1975 if (!sc->sample_size) /* do not overwrite value computed in stsd */
1976 sc->sample_size = sample_size;
1977 field_size = 32;
bd27eed6
AC
1978 } else {
1979 sample_size = 0;
b7effd4e
AK
1980 avio_rb24(pb); /* reserved */
1981 field_size = avio_r8(pb);
bd27eed6 1982 }
b7effd4e 1983 entries = avio_rb32(pb);
0d8f0abf 1984
1a3eb042 1985 av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
568e18b1 1986
6cea494e 1987 sc->sample_count = entries;
b72708f8
BC
1988 if (sample_size)
1989 return 0;
1990
bd27eed6
AC
1991 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
1992 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
bcd5d979 1993 return AVERROR_INVALIDDATA;
bd27eed6
AC
1994 }
1995
52401b82
AC
1996 if (!entries)
1997 return 0;
e4bc8af1 1998 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
bcd5d979 1999 return AVERROR_INVALIDDATA;
1c02d96f 2000 sc->sample_sizes = av_malloc(entries * sizeof(int));
b6a17df4 2001 if (!sc->sample_sizes)
0d8f0abf
BC
2002 return AVERROR(ENOMEM);
2003
bd27eed6
AC
2004 num_bytes = (entries*field_size+4)>>3;
2005
059a9348 2006 buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
bd27eed6
AC
2007 if (!buf) {
2008 av_freep(&sc->sample_sizes);
2009 return AVERROR(ENOMEM);
2010 }
2011
5c720657
AC
2012 ret = ffio_read_size(pb, buf, num_bytes);
2013 if (ret < 0) {
bd27eed6
AC
2014 av_freep(&sc->sample_sizes);
2015 av_free(buf);
5c720657 2016 return ret;
bd27eed6
AC
2017 }
2018
2019 init_get_bits(&gb, buf, 8*num_bytes);
2020
9888ffb1 2021 for (i = 0; i < entries && !pb->eof_reached; i++) {
bd27eed6 2022 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
bc7d0517
MS
2023 sc->data_size += sc->sample_sizes[i];
2024 }
bd27eed6 2025
9888ffb1
LB
2026 sc->sample_count = i;
2027
f261a55d
AC
2028 av_free(buf);
2029
9888ffb1
LB
2030 if (pb->eof_reached)
2031 return AVERROR_EOF;
2032
6cea494e
ZK
2033 return 0;
2034}
2035
ae628ec1 2036static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
0147f198 2037{
6a63ff19
BC
2038 AVStream *st;
2039 MOVStreamContext *sc;
568e18b1 2040 unsigned int i, entries;
d957696f
MN
2041 int64_t duration=0;
2042 int64_t total_sample_count=0;
b6a17df4 2043
6a63ff19
BC
2044 if (c->fc->nb_streams < 1)
2045 return 0;
2046 st = c->fc->streams[c->fc->nb_streams-1];
2047 sc = st->priv_data;
2048
b7effd4e
AK
2049 avio_r8(pb); /* version */
2050 avio_rb24(pb); /* flags */
2051 entries = avio_rb32(pb);
0d8f0abf 2052
1a3eb042 2053 av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
5a7ba586 2054 c->fc->nb_streams-1, entries);
0d8f0abf 2055
6d23d197
AC
2056 if (!entries)
2057 return 0;
2058 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
30c3d976 2059 return AVERROR(EINVAL);
5a7ba586 2060
2620df13 2061 av_free(sc->stts_data);
c3e92a6c 2062 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
c1da59fa 2063 if (!sc->stts_data)
0d8f0abf 2064 return AVERROR(ENOMEM);
5a7ba586 2065
9888ffb1 2066 for (i = 0; i < entries && !pb->eof_reached; i++) {
cd461d48
MN
2067 int sample_duration;
2068 int sample_count;
0147f198 2069
b7effd4e
AK
2070 sample_count=avio_rb32(pb);
2071 sample_duration = avio_rb32(pb);
c2319876
MS
2072 if (sample_count < 0) {
2073 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2074 return AVERROR_INVALIDDATA;
2075 }
cd7352d5
MN
2076 sc->stts_data[i].count= sample_count;
2077 sc->stts_data[i].duration= sample_duration;
2078
1a3eb042 2079 av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
5a7ba586 2080 sample_count, sample_duration);
4e5ef14f 2081
7e815047 2082 duration+=(int64_t)sample_duration*sample_count;
891f64b3 2083 total_sample_count+=sample_count;
891f64b3 2084 }
2085
9888ffb1
LB
2086 sc->stts_count = i;
2087
2088 if (pb->eof_reached)
2089 return AVERROR_EOF;
2090
961e0ccd 2091 st->nb_frames= total_sample_count;
5a7ba586 2092 if (duration)
961e0ccd 2093 st->duration= duration;
7f19bdc2 2094 sc->track_end = duration;
0147f198
FR
2095 return 0;
2096}
2097
ae628ec1 2098static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
c4ac052b 2099{
6a63ff19
BC
2100 AVStream *st;
2101 MOVStreamContext *sc;
c4ac052b
MN
2102 unsigned int i, entries;
2103
6a63ff19
BC
2104 if (c->fc->nb_streams < 1)
2105 return 0;
2106 st = c->fc->streams[c->fc->nb_streams-1];
2107 sc = st->priv_data;
2108
b7effd4e
AK
2109 avio_r8(pb); /* version */
2110 avio_rb24(pb); /* flags */
2111 entries = avio_rb32(pb);
0d8f0abf 2112
1a3eb042 2113 av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
0d8f0abf 2114
f261a55d
AC
2115 av_freep(&sc->ctts_data);
2116
52401b82
AC
2117 if (!entries)
2118 return 0;
5a7ba586 2119 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
bcd5d979 2120 return AVERROR_INVALIDDATA;
a8a90906 2121 sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
c1da59fa 2122 if (!sc->ctts_data)
0d8f0abf 2123 return AVERROR(ENOMEM);
4e5ef14f 2124
9888ffb1 2125 for (i = 0; i < entries && !pb->eof_reached; i++) {
b7effd4e
AK
2126 int count =avio_rb32(pb);
2127 int duration =avio_rb32(pb);
70a61ed4
MN
2128
2129 sc->ctts_data[i].count = count;
2130 sc->ctts_data[i].duration= duration;
e7452721
BC
2131 if (duration < 0)
2132 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
c4ac052b 2133 }
e7452721 2134
9888ffb1
LB
2135 sc->ctts_count = i;
2136
2137 if (pb->eof_reached)
2138 return AVERROR_EOF;
2139
1a3eb042 2140 av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
e7452721 2141
c4ac052b
MN
2142 return 0;
2143}
2144
d17d0ec8
YN
2145static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2146{
2147 AVStream *st;
2148 MOVStreamContext *sc;
2149 unsigned int i, entries;
2150 uint8_t version;
2151 uint32_t grouping_type;
2152
2153 if (c->fc->nb_streams < 1)
2154 return 0;
2155 st = c->fc->streams[c->fc->nb_streams-1];
2156 sc = st->priv_data;
2157
2158 version = avio_r8(pb); /* version */
2159 avio_rb24(pb); /* flags */
2160 grouping_type = avio_rl32(pb);
2161 if (grouping_type != MKTAG( 'r','a','p',' '))
2162 return 0; /* only support 'rap ' grouping */
2163 if (version == 1)
2164 avio_rb32(pb); /* grouping_type_parameter */
2165
2166 entries = avio_rb32(pb);
2167 if (!entries)
2168 return 0;
2169 if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2170 return AVERROR_INVALIDDATA;
2171 sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2172 if (!sc->rap_group)
2173 return AVERROR(ENOMEM);
2174
2175 for (i = 0; i < entries && !pb->eof_reached; i++) {
2176 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2177 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2178 }
2179
2180 sc->rap_group_count = i;
2181
2182 return pb->eof_reached ? AVERROR_EOF : 0;
2183}
2184
1e77810d
BC
2185static void mov_build_index(MOVContext *mov, AVStream *st)
2186{
2187 MOVStreamContext *sc = st->priv_data;
bc5c918e 2188 int64_t current_offset;
1e77810d
BC
2189 int64_t current_dts = 0;
2190 unsigned int stts_index = 0;
2191 unsigned int stsc_index = 0;
2192 unsigned int stss_index = 0;
171a3531 2193 unsigned int stps_index = 0;
1e77810d 2194 unsigned int i, j;
c5898e86 2195 uint64_t stream_size = 0;
1e77810d 2196
baf2ffd3 2197 /* adjust first dts according to edit list */
c3024f9e 2198 if (sc->time_offset && mov->time_scale > 0) {
f33a6a22
YN
2199 if (sc->time_offset < 0)
2200 sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
2201 current_dts = -sc->time_offset;
69e7ad8d 2202 if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration &&
4af7166f 2203 sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
f03a081d
BC
2204 /* more than 16 frames delay, dts are likely wrong
2205 this happens with files created by iMovie */
2206 sc->wrong_dts = 1;
2207 st->codec->has_b_frames = 1;
2208 }
baf2ffd3
BC
2209 }
2210
d3bc61ac 2211 /* only use old uncompressed audio chunk demuxing when stts specifies it */
72415b2a 2212 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
d3bc61ac 2213 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
1e77810d
BC
2214 unsigned int current_sample = 0;
2215 unsigned int stts_sample = 0;
171a3531 2216 unsigned int sample_size;
1e77810d 2217 unsigned int distance = 0;
d17d0ec8
YN
2218 unsigned int rap_group_index = 0;
2219 unsigned int rap_group_sample = 0;
2220 int rap_group_present = sc->rap_group_count && sc->rap_group;
94c9bf88 2221 int key_off = (sc->keyframes && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
1e77810d 2222
91eee2af
MS
2223 current_dts -= sc->dts_shift;
2224
52401b82
AC
2225 if (!sc->sample_count)
2226 return;
4f7c7624 2227 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
987e857f 2228 return;
f369b935
AK
2229 if (av_reallocp_array(&st->index_entries,
2230 st->nb_index_entries + sc->sample_count,
2231 sizeof(*st->index_entries)) < 0) {
2232 st->nb_index_entries = 0;
987e857f 2233 return;
f369b935 2234 }
4f7c7624 2235 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
987e857f 2236
1e77810d
BC
2237 for (i = 0; i < sc->chunk_count; i++) {
2238 current_offset = sc->chunk_offsets[i];
9dd94f83 2239 while (stsc_index + 1 < sc->stsc_count &&
04e06cfa 2240 i + 1 == sc->stsc_data[stsc_index + 1].first)
1e77810d 2241 stsc_index++;
04e06cfa 2242 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
171a3531 2243 int keyframe = 0;
1e77810d
BC
2244 if (current_sample >= sc->sample_count) {
2245 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
f0f2218d 2246 return;
1e77810d 2247 }
171a3531 2248
accea4d9 2249 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
171a3531 2250 keyframe = 1;
1e77810d
BC
2251 if (stss_index + 1 < sc->keyframe_count)
2252 stss_index++;
171a3531
BC
2253 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2254 keyframe = 1;
2255 if (stps_index + 1 < sc->stps_count)
2256 stps_index++;
1e77810d 2257 }
d17d0ec8
YN
2258 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2259 if (sc->rap_group[rap_group_index].index > 0)
2260 keyframe = 1;
2261 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2262 rap_group_sample = 0;
2263 rap_group_index++;
2264 }
2265 }
171a3531
BC
2266 if (keyframe)
2267 distance = 0;
1e77810d 2268 sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
5a7ba586 2269 if (sc->pseudo_stream_id == -1 ||
04e06cfa 2270 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
987e857f
BC
2271 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2272 e->pos = current_offset;
91eee2af 2273 e->timestamp = current_dts;
987e857f
BC
2274 e->size = sample_size;
2275 e->min_distance = distance;
2276 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
1a3eb042 2277 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
585dac65 2278 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
91eee2af 2279 current_offset, current_dts, sample_size, distance, keyframe);
585dac65 2280 }
6c00a9de 2281
1e77810d 2282 current_offset += sample_size;
c5898e86 2283 stream_size += sample_size;
bbe46bc4 2284 current_dts += sc->stts_data[stts_index].duration;
1e77810d
BC
2285 distance++;
2286 stts_sample++;
2287 current_sample++;
2288 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2289 stts_sample = 0;
2290 stts_index++;
2291 }
2292 }
2293 }
fedb1eca
BC
2294 if (st->duration > 0)
2295 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
7e69621f 2296 } else {
987e857f 2297 unsigned chunk_samples, total = 0;
7e69621f 2298
987e857f
BC
2299 // compute total chunk count
2300 for (i = 0; i < sc->stsc_count; i++) {
2301 unsigned count, chunk_count;
7e69621f 2302
987e857f 2303 chunk_samples = sc->stsc_data[i].count;
a294a7a1
CEH
2304 if (i != sc->stsc_count - 1 &&
2305 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
7e69621f
BC
2306 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2307 return;
2308 }
2309
987e857f
BC
2310 if (sc->samples_per_frame >= 160) { // gsm
2311 count = chunk_samples / sc->samples_per_frame;
2312 } else if (sc->samples_per_frame > 1) {
2313 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2314 count = (chunk_samples+samples-1) / samples;
2315 } else {
2316 count = (chunk_samples+1023) / 1024;
2317 }
2318
2319 if (i < sc->stsc_count - 1)
2320 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2321 else
2322 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2323 total += chunk_count * count;
2324 }
2325
1a3eb042 2326 av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
4f7c7624 2327 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
987e857f 2328 return;
f369b935
AK
2329 if (av_reallocp_array(&st->index_entries,
2330 st->nb_index_entries + total,
2331 sizeof(*st->index_entries)) < 0) {
2332 st->nb_index_entries = 0;
987e857f 2333 return;
f369b935 2334 }
4f7c7624 2335 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
987e857f
BC
2336
2337 // populate index
2338 for (i = 0; i < sc->chunk_count; i++) {
2339 current_offset = sc->chunk_offsets[i];
2340 if (stsc_index + 1 < sc->stsc_count &&
2341 i + 1 == sc->stsc_data[stsc_index + 1].first)
2342 stsc_index++;
2343 chunk_samples = sc->stsc_data[stsc_index].count;
2344
7e69621f 2345 while (chunk_samples > 0) {
987e857f 2346 AVIndexEntry *e;
7e69621f
BC
2347 unsigned size, samples;
2348
cffb9ea8
AC
2349 if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
2350 avpriv_request_sample(mov->fc,
2351 "Zero bytes per frame, but %d samples per frame",
2352 sc->samples_per_frame);
2353 return;
2354 }
2355
7e69621f
BC
2356 if (sc->samples_per_frame >= 160) { // gsm
2357 samples = sc->samples_per_frame;
2358 size = sc->bytes_per_frame;
2359 } else {
2360 if (sc->samples_per_frame > 1) {
2361 samples = FFMIN((1024 / sc->samples_per_frame)*
2362 sc->samples_per_frame, chunk_samples);
2363 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
1e77810d 2364 } else {
7e69621f
BC
2365 samples = FFMIN(1024, chunk_samples);
2366 size = samples * sc->sample_size;
1e77810d
BC
2367 }
2368 }
7e69621f 2369
987e857f
BC
2370 if (st->nb_index_entries >= total) {
2371 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2372 return;
2373 }
2374 e = &st->index_entries[st->nb_index_entries++];
2375 e->pos = current_offset;
2376 e->timestamp = current_dts;
2377 e->size = size;
2378 e->min_distance = 0;
2379 e->flags = AVINDEX_KEYFRAME;
1a3eb042 2380 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
117a9190 2381 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
7e69621f
BC
2382 size, samples);
2383
2384 current_offset += size;
bbe46bc4 2385 current_dts += samples;
7e69621f 2386 chunk_samples -= samples;
1e77810d
BC
2387 }
2388 }
2389 }
1e77810d 2390}
bd991df2 2391
9d77a8fa
MS
2392static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
2393 AVIOInterruptCB *int_cb)
adeb9071 2394{
6a245905
MN
2395 /* try relative path, we do not try the absolute because it can leak information about our
2396 system to an attacker */
f1840b07 2397 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
adeb9071
BC
2398 char filename[1024];
2399 char *src_path;
2400 int i, l;
2401
2402 /* find a source dir */
2403 src_path = strrchr(src, '/');
2404 if (src_path)
2405 src_path++;
2406 else
2407 src_path = src;
2408
2409 /* find a next level down to target */
2410 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2411 if (ref->path[l] == '/') {
2412 if (i == ref->nlvl_to - 1)
2413 break;
2414 else
2415 i++;
2416 }
2417
2418 /* compose filename if next level down to target was found */
bbdf0d22 2419 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
adeb9071
BC
2420 memcpy(filename, src, src_path - src);
2421 filename[src_path - src] = 0;
2422
2423 for (i = 1; i < ref->nlvl_from; i++)
2424 av_strlcat(filename, "../", 1024);
2425
2426 av_strlcat(filename, ref->path + l + 1, 1024);
2427
9d77a8fa 2428 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
adeb9071
BC
2429 return 0;
2430 }
2431 }
2432
2433 return AVERROR(ENOENT);
628b16f4 2434}
adeb9071 2435
ae628ec1 2436static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879
ZK
2437{
2438 AVStream *st;
2439 MOVStreamContext *sc;
bd991df2 2440 int ret;
5ca1d879 2441
84ad31ff 2442 st = avformat_new_stream(c->fc, NULL);
3efe8848 2443 if (!st) return AVERROR(ENOMEM);
84ad31ff 2444 st->id = c->fc->nb_streams;
9a630c25 2445 sc = av_mallocz(sizeof(MOVStreamContext));
2922cbdb 2446 if (!sc) return AVERROR(ENOMEM);
5ca1d879 2447
5ca1d879 2448 st->priv_data = sc;
72415b2a 2449 st->codec->codec_type = AVMEDIA_TYPE_DATA;
64d50fa5 2450 sc->ffindex = st->index;
5ca1d879 2451
bd991df2
BC
2452 if ((ret = mov_read_default(c, pb, atom)) < 0)
2453 return ret;
2454
2455 /* sanity checks */
6c00a9de
BC
2456 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2457 (!sc->sample_size && !sc->sample_count))) {
6282c5f4
BC
2458 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2459 st->index);
bd991df2
BC
2460 return 0;
2461 }
6c00a9de 2462
c3024f9e 2463 if (sc->time_scale <= 0) {
507330b9 2464 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
6c00a9de 2465 sc->time_scale = c->time_scale;
c3024f9e 2466 if (sc->time_scale <= 0)
507330b9
BC
2467 sc->time_scale = 1;
2468 }
6c00a9de 2469
c3f9ebf7 2470 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
bd991df2 2471
bd991df2
BC
2472 mov_build_index(c, st);
2473
2474 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
adeb9071 2475 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
9d77a8fa 2476 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0)
adeb9071
BC
2477 av_log(c->fc, AV_LOG_ERROR,
2478 "stream %d, error opening alias: path='%s', dir='%s', "
2479 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2480 st->index, dref->path, dref->dir, dref->filename,
2481 dref->volume, dref->nlvl_from, dref->nlvl_to);
bd991df2
BC
2482 } else
2483 sc->pb = c->fc->pb;
2484
72415b2a 2485 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
525fcb27
AW
2486 if (!st->sample_aspect_ratio.num &&
2487 (st->codec->width != sc->width || st->codec->height != sc->height)) {
2488 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2489 ((double)st->codec->width * sc->height), INT_MAX);
c3aeaa54 2490 }
c9566115
BC
2491 }
2492
ecf442a5
RD
2493 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2494 if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2495 TAG_IS_AVCI(st->codec->codec_tag)) {
2496 ret = ff_generate_avci_extradata(st);
2497 if (ret < 0)
2498 return ret;
2499 }
2500
bd991df2 2501 switch (st->codec->codec_id) {
b250f9c6 2502#if CONFIG_H261_DECODER
36ef5369 2503 case AV_CODEC_ID_H261:
bd991df2 2504#endif
b250f9c6 2505#if CONFIG_H263_DECODER
36ef5369 2506 case AV_CODEC_ID_H263:
bd991df2 2507#endif
b250f9c6 2508#if CONFIG_MPEG4_DECODER
36ef5369 2509 case AV_CODEC_ID_MPEG4:
bd991df2 2510#endif
6c00a9de 2511 st->codec->width = 0; /* let decoder init width/height */
bd991df2
BC
2512 st->codec->height= 0;
2513 break;
bd991df2 2514 }
f9900374
BC
2515
2516 /* Do not need those anymore. */
2517 av_freep(&sc->chunk_offsets);
04e06cfa 2518 av_freep(&sc->stsc_data);
f9900374
BC
2519 av_freep(&sc->sample_sizes);
2520 av_freep(&sc->keyframes);
2521 av_freep(&sc->stts_data);
171a3531 2522 av_freep(&sc->stps_data);
d17d0ec8 2523 av_freep(&sc->rap_group);
f9900374 2524
bd991df2 2525 return 0;
5ca1d879
ZK
2526}
2527
ae628ec1 2528static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
51c15201
BC
2529{
2530 int ret;
2531 c->itunes_metadata = 1;
2532 ret = mov_read_default(c, pb, atom);
2533 c->itunes_metadata = 0;
2534 return ret;
2535}
2536
9a07fac6
AK
2537static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int size)
2538{
2539 int64_t end = avio_tell(pb) + size;
2540 uint8_t *key = NULL, *val = NULL;
2541 int i;
2542
2543 for (i = 0; i < 2; i++) {
2544 uint8_t **p;
2545 uint32_t len, tag;
5c720657 2546 int ret;
9a07fac6
AK
2547
2548 if (end - avio_tell(pb) <= 12)
2549 break;
2550
2551 len = avio_rb32(pb);
2552 tag = avio_rl32(pb);
2553 avio_skip(pb, 4); // flags
2554
2555 if (len < 12 || len - 12 > end - avio_tell(pb))
2556 break;
2557 len -= 12;
2558
2559 if (tag == MKTAG('n', 'a', 'm', 'e'))
2560 p = &key;
2561 else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2562 avio_skip(pb, 4);
2563 len -= 4;
2564 p = &val;
2565 } else
2566 break;
2567
2568 *p = av_malloc(len + 1);
2569 if (!*p)
2570 break;
5c720657
AC
2571 ret = ffio_read_size(pb, *p, len);
2572 if (ret < 0) {
2573 av_freep(p);
2574 return ret;
2575 }
9a07fac6
AK
2576 (*p)[len] = 0;
2577 }
2578
2579 if (key && val) {
2580 av_dict_set(&c->fc->metadata, key, val,
2581 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
2582 key = val = NULL;
2583 }
2584
2585 avio_seek(pb, end, SEEK_SET);
2586 av_freep(&key);
2587 av_freep(&val);
2588 return 0;
2589}
2590
2591static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2592{
2593 int64_t end = avio_tell(pb) + atom.size;
2594 uint32_t tag, len;
2595
2596 if (atom.size < 8)
2597 goto fail;
2598
2599 len = avio_rb32(pb);
2600 tag = avio_rl32(pb);
2601
2602 if (len > atom.size)
2603 goto fail;
2604
2605 if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2606 uint8_t domain[128];
2607 int domain_len;
2608
2609 avio_skip(pb, 4); // flags
2610 len -= 12;
2611
2612 domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2613 avio_skip(pb, len - domain_len);
2614 if (!strcmp(domain, "org.hydrogenaudio.replaygain"))
2615 return mov_read_replaygain(c, pb, end - avio_tell(pb));
2616 }
2617
2618fail:
2619 av_log(c->fc, AV_LOG_VERBOSE,
2620 "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2621 return 0;
2622}
2623
ae628ec1 2624static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
51c15201 2625{
df8843c6 2626 while (atom.size > 8) {
b7effd4e 2627 uint32_t tag = avio_rl32(pb);
df8843c6
BC
2628 atom.size -= 4;
2629 if (tag == MKTAG('h','d','l','r')) {
6b4aa5da 2630 avio_seek(pb, -8, SEEK_CUR);
df8843c6
BC
2631 atom.size += 8;
2632 return mov_read_default(c, pb, atom);
2633 }
2634 }
2635 return 0;
51c15201
BC
2636}
2637
ae628ec1 2638static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879 2639{
ec072669
JS
2640 int i;
2641 int width;
2642 int height;
2643 int64_t disp_transform[2];
853cc025 2644 int display_matrix[3][3];
6a63ff19
BC
2645 AVStream *st;
2646 MOVStreamContext *sc;
2647 int version;
1f70a5ad 2648 int flags;
6a63ff19
BC
2649
2650 if (c->fc->nb_streams < 1)
2651 return 0;
2652 st = c->fc->streams[c->fc->nb_streams-1];
2653 sc = st->priv_data;
5ca1d879 2654
b7effd4e 2655 version = avio_r8(pb);
1f70a5ad
JS
2656 flags = avio_rb24(pb);
2657 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
5ca1d879 2658
1175561e 2659 if (version == 1) {
b7effd4e
AK
2660 avio_rb64(pb);
2661 avio_rb64(pb);
1175561e 2662 } else {
b7effd4e
AK
2663 avio_rb32(pb); /* creation time */
2664 avio_rb32(pb); /* modification time */
1175561e 2665 }
b7effd4e
AK
2666 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2667 avio_rb32(pb); /* reserved */
fcdd622c 2668
117a9190 2669 /* highlevel (considering edits) duration in movie timebase */
b7effd4e
AK
2670 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2671 avio_rb32(pb); /* reserved */
2672 avio_rb32(pb); /* reserved */
5ca1d879 2673
b7effd4e
AK
2674 avio_rb16(pb); /* layer */
2675 avio_rb16(pb); /* alternate group */
2676 avio_rb16(pb); /* volume */
2677 avio_rb16(pb); /* reserved */
5ca1d879 2678
ec072669
JS
2679 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2680 // they're kept in fixed point format through all calculations
853cc025
VG
2681 // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2682 // side data, but the scale factor is not needed to calculate aspect ratio
ec072669 2683 for (i = 0; i < 3; i++) {
b7effd4e
AK
2684 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2685 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
853cc025 2686 display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
ec072669 2687 }
5ca1d879 2688
b7effd4e
AK
2689 width = avio_rb32(pb); // 16.16 fixed point track width
2690 height = avio_rb32(pb); // 16.16 fixed point track height
6cdbff63
DC
2691 sc->width = width >> 16;
2692 sc->height = height >> 16;
ec072669 2693
853cc025
VG
2694 // save the matrix when it is not the default identity
2695 if (display_matrix[0][0] != (1 << 16) ||
2696 display_matrix[1][1] != (1 << 16) ||
2697 display_matrix[2][2] != (1 << 30) ||
2698 display_matrix[0][1] || display_matrix[0][2] ||
2699 display_matrix[1][0] || display_matrix[1][2] ||
2700 display_matrix[2][0] || display_matrix[2][1]) {
2701 int i, j;
2702
2703 av_freep(&sc->display_matrix);
2704 sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2705 if (!sc->display_matrix)
2706 return AVERROR(ENOMEM);
2707
2708 for (i = 0; i < 3; i++)
2709 for (j = 0; j < 3; j++)
e4fe535d 2710 sc->display_matrix[i * 3 + j] = display_matrix[i][j];
853cc025
VG
2711 }
2712
d862fce7 2713 // transform the display width/height according to the matrix
4c5fa628 2714 // skip this when the display matrix is the identity one
ec072669 2715 // to keep the same scale, use [width height 1<<16]
4c5fa628 2716 if (width && height && sc->display_matrix) {
ec072669
JS
2717 for (i = 0; i < 2; i++)
2718 disp_transform[i] =
2719 (int64_t) width * display_matrix[0][i] +
2720 (int64_t) height * display_matrix[1][i] +
2721 ((int64_t) display_matrix[2][i] << 16);
2722
2723 //sample aspect ratio is new width/height divided by old width/height
4c5fa628
VG
2724 if (disp_transform[0] > 0 && disp_transform[1] > 0)
2725 st->sample_aspect_ratio = av_d2q(
2726 ((double) disp_transform[0] * height) /
2727 ((double) disp_transform[1] * width), INT_MAX);
ec072669 2728 }
5ca1d879
ZK
2729 return 0;
2730}
2731
ae628ec1 2732static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
61aedb0f
BC
2733{
2734 MOVFragment *frag = &c->fragment;
2735 MOVTrackExt *trex = NULL;
2736 int flags, track_id, i;
2737
b7effd4e
AK
2738 avio_r8(pb); /* version */
2739 flags = avio_rb24(pb);
61aedb0f 2740
b7effd4e 2741 track_id = avio_rb32(pb);
63581eb1 2742 if (!track_id)
bcd5d979 2743 return AVERROR_INVALIDDATA;
61aedb0f
BC
2744 frag->track_id = track_id;
2745 for (i = 0; i < c->trex_count; i++)
2746 if (c->trex_data[i].track_id == frag->track_id) {
2747 trex = &c->trex_data[i];
2748 break;
2749 }
2750 if (!trex) {
2751 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
bcd5d979 2752 return AVERROR_INVALIDDATA;
61aedb0f
BC
2753 }
2754
73328f24 2755 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
20f95f21
YN
2756 avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
2757 frag->moof_offset : frag->implicit_offset;
73328f24
MS
2758 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2759
2760 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2761 avio_rb32(pb) : trex->duration;
2762 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2763 avio_rb32(pb) : trex->size;
2764 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2765 avio_rb32(pb) : trex->flags;
1a3eb042 2766 av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
61aedb0f
BC
2767 return 0;
2768}
2769
ae628ec1 2770static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
7221579b 2771{
b7effd4e 2772 c->chapter_track = avio_rb32(pb);
7221579b
DC
2773 return 0;
2774}
2775
ae628ec1 2776static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
61aedb0f
BC
2777{
2778 MOVTrackExt *trex;
f369b935 2779 int err;
61aedb0f
BC
2780
2781 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
bcd5d979 2782 return AVERROR_INVALIDDATA;
f369b935
AK
2783 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2784 sizeof(*c->trex_data))) < 0) {
2785 c->trex_count = 0;
2786 return err;
2787 }
61aedb0f 2788 trex = &c->trex_data[c->trex_count++];
b7effd4e
AK
2789 avio_r8(pb); /* version */
2790 avio_rb24(pb); /* flags */
2791 trex->track_id = avio_rb32(pb);
2792 trex->stsd_id = avio_rb32(pb);
2793 trex->duration = avio_rb32(pb);
2794 trex->size = avio_rb32(pb);
2795 trex->flags = avio_rb32(pb);
61aedb0f
BC
2796 return 0;
2797}
2798
a74f8121
MS
2799static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2800{
2801 MOVFragment *frag = &c->fragment;
2802 AVStream *st = NULL;
2803 MOVStreamContext *sc;
2804 int version, i;
2805
2806 for (i = 0; i < c->fc->nb_streams; i++) {
2807 if (c->fc->streams[i]->id == frag->track_id) {
2808 st = c->fc->streams[i];
2809 break;
2810 }
2811 }
2812 if (!st) {
2813 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2814 return AVERROR_INVALIDDATA;
2815 }
2816 sc = st->priv_data;
2817 if (sc->pseudo_stream_id + 1 != frag->stsd_id)
2818 return 0;
2819 version = avio_r8(pb);
2820 avio_rb24(pb); /* flags */
2821 if (version) {
2822 sc->track_end = avio_rb64(pb);
2823 } else {
2824 sc->track_end = avio_rb32(pb);
2825 }
2826 return 0;
2827}
2828
ae628ec1 2829static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
61aedb0f
BC
2830{
2831 MOVFragment *frag = &c->fragment;
63581eb1 2832 AVStream *st = NULL;
44e43aab 2833 MOVStreamContext *sc;
45a811b5 2834 MOVStts *ctts_data;
61aedb0f
BC
2835 uint64_t offset;
2836 int64_t dts;
2837 int data_offset = 0;
2838 unsigned entries, first_sample_flags = frag->flags;
7cad1bf0 2839 int flags, distance, i, err;
61aedb0f 2840
63581eb1
AC
2841 for (i = 0; i < c->fc->nb_streams; i++) {
2842 if (c->fc->streams[i]->id == frag->track_id) {
2843 st = c->fc->streams[i];
2844 break;
2845 }
2846 }
2847 if (!st) {
2848 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
bcd5d979 2849 return AVERROR_INVALIDDATA;
63581eb1 2850 }
44e43aab 2851 sc = st->priv_data;
61aedb0f
BC
2852 if (sc->pseudo_stream_id+1 != frag->stsd_id)
2853 return 0;
b7effd4e
AK
2854 avio_r8(pb); /* version */
2855 flags = avio_rb24(pb);
2856 entries = avio_rb32(pb);
1a3eb042 2857 av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
45a811b5
YN
2858
2859 /* Always assume the presence of composition time offsets.
2860 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2861 * 1) in the initial movie, there are no samples.
2862 * 2) in the first movie fragment, there is only one sample without composition time offset.
2863 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2864 if (!sc->ctts_count && sc->sample_count)
2865 {
2866 /* Complement ctts table if moov atom doesn't have ctts atom. */
b698542a 2867 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
0d8f0abf 2868 if (!ctts_data)
61aedb0f 2869 return AVERROR(ENOMEM);
0d8f0abf 2870 sc->ctts_data = ctts_data;
45a811b5
YN
2871 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2872 sc->ctts_data[sc->ctts_count].duration = 0;
2873 sc->ctts_count++;
61aedb0f 2874 }
45a811b5 2875 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
bcd5d979 2876 return AVERROR_INVALIDDATA;
f369b935
AK
2877 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2878 sizeof(*sc->ctts_data))) < 0) {
2879 sc->ctts_count = 0;
2880 return err;
2881 }
3eec23f3
MS
2882 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
2883 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
7f19bdc2 2884 dts = sc->track_end - sc->time_offset;
61aedb0f
BC
2885 offset = frag->base_data_offset + data_offset;
2886 distance = 0;
1a3eb042 2887 av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
9888ffb1 2888 for (i = 0; i < entries && !pb->eof_reached; i++) {
61aedb0f
BC
2889 unsigned sample_size = frag->size;
2890 int sample_flags = i ? frag->flags : first_sample_flags;
2891 unsigned sample_duration = frag->duration;
a5c50913 2892 int keyframe = 0;
61aedb0f 2893
3eec23f3
MS
2894 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2895 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
2896 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
91eee2af
MS
2897 sc->ctts_data[sc->ctts_count].count = 1;
2898 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2899 avio_rb32(pb) : 0;
45a811b5 2900 sc->ctts_count++;
a5c50913
MS
2901 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2902 keyframe = 1;
7cad1bf0
MS
2903 else
2904 keyframe =
a5c50913
MS
2905 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2906 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
2907 if (keyframe)
61aedb0f 2908 distance = 0;
91eee2af 2909 av_add_index_entry(st, offset, dts, sample_size, distance,
61aedb0f 2910 keyframe ? AVINDEX_KEYFRAME : 0);
1a3eb042 2911 av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
61aedb0f 2912 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
91eee2af 2913 offset, dts, sample_size, distance, keyframe);
61aedb0f 2914 distance++;
bbe46bc4 2915 dts += sample_duration;
61aedb0f 2916 offset += sample_size;
bc7d0517 2917 sc->data_size += sample_size;
61aedb0f 2918 }
9888ffb1
LB
2919
2920 if (pb->eof_reached)
2921 return AVERROR_EOF;
2922
20f95f21 2923 frag->implicit_offset = offset;
7f19bdc2 2924 st->duration = sc->track_end = dts + sc->time_offset;
61aedb0f
BC
2925 return 0;
2926}
2927
5ca1d879
ZK
2928/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2929/* like the files created with Adobe Premiere 5.0, for samples see */
2930/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
ae628ec1 2931static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5ca1d879
ZK
2932{
2933 int err;
5ca1d879 2934
5ca1d879
ZK
2935 if (atom.size < 8)
2936 return 0; /* continue */
b7effd4e 2937 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
45a8a02a 2938 avio_skip(pb, atom.size - 4);
5ca1d879
ZK
2939 return 0;
2940 }
b7effd4e 2941 atom.type = avio_rl32(pb);
5ca1d879 2942 atom.size -= 8;
1c126b4f 2943 if (atom.type != MKTAG('m','d','a','t')) {
45a8a02a 2944 avio_skip(pb, atom.size);
5ca1d879
ZK
2945 return 0;
2946 }
2947 err = mov_read_mdat(c, pb, atom);
5ca1d879
ZK
2948 return err;
2949}
2950
ae628ec1 2951static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
0147f198 2952{
b250f9c6 2953#if CONFIG_ZLIB
ae628ec1 2954 AVIOContext ctx;
b6a17df4
ZK
2955 uint8_t *cmov_data;
2956 uint8_t *moov_data; /* uncompressed data */
0147f198 2957 long cmov_len, moov_len;
6f04eb1e 2958 int ret = -1;
b6a17df4 2959
b7effd4e
AK
2960 avio_rb32(pb); /* dcom atom */
2961 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
bcd5d979 2962 return AVERROR_INVALIDDATA;
b7effd4e 2963 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
f3592353 2964 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
bcd5d979 2965 return AVERROR_INVALIDDATA;
0147f198 2966 }
b7effd4e
AK
2967 avio_rb32(pb); /* cmvd atom */
2968 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
bcd5d979 2969 return AVERROR_INVALIDDATA;
b7effd4e 2970 moov_len = avio_rb32(pb); /* uncompressed size */
5ca1d879 2971 cmov_len = atom.size - 6 * 4;
5cd62665 2972
9a630c25 2973 cmov_data = av_malloc(cmov_len);
0147f198 2974 if (!cmov_data)
0d8f0abf 2975 return AVERROR(ENOMEM);
9a630c25 2976 moov_data = av_malloc(moov_len);
0147f198
FR
2977 if (!moov_data) {
2978 av_free(cmov_data);
0d8f0abf 2979 return AVERROR(ENOMEM);
0147f198 2980 }
5c720657
AC
2981 ret = ffio_read_size(pb, cmov_data, cmov_len);
2982 if (ret < 0)
2983 goto free_and_return;
2984
5a7ba586 2985 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
6f04eb1e 2986 goto free_and_return;
5a7ba586 2987 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
6f04eb1e 2988 goto free_and_return;
1c126b4f 2989 atom.type = MKTAG('m','o','o','v');
5ca1d879
ZK
2990 atom.size = moov_len;
2991 ret = mov_read_default(c, &ctx, atom);
6f04eb1e 2992free_and_return:
0147f198
FR
2993 av_free(moov_data);
2994 av_free(cmov_data);
2995 return ret;
d966b2f0
BC
2996#else
2997 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
bcd5d979 2998 return AVERROR(ENOSYS);
0147f198 2999#endif
d966b2f0 3000}
0147f198 3001
baf25c9d 3002/* edit list atom */
ae628ec1 3003static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
baf25c9d 3004{
1c4bf2ec 3005 MOVStreamContext *sc;
5f0bb0ba 3006 int i, edit_count, version;
e0977c80 3007
1c4bf2ec
BC
3008 if (c->fc->nb_streams < 1)
3009 return 0;
3010 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3011
5f0bb0ba 3012 version = avio_r8(pb); /* version */
b7effd4e
AK
3013 avio_rb24(pb); /* flags */
3014 edit_count = avio_rb32(pb); /* entries */
e0977c80 3015
5a7ba586 3016 if ((uint64_t)edit_count*12+8 > atom.size)
bcd5d979 3017 return AVERROR_INVALIDDATA;
53e099de 3018
5a7ba586 3019 for (i=0; i<edit_count; i++){
5f0bb0ba
YN
3020 int64_t time;
3021 int64_t duration;
3022 if (version == 1) {
3023 duration = avio_rb64(pb);
3024 time = avio_rb64(pb);
3025 } else {
3026 duration = avio_rb32(pb); /* segment duration */
ae88e9cf 3027 time = (int32_t)avio_rb32(pb); /* media time */
5f0bb0ba 3028 }
b7effd4e 3029 avio_rb32(pb); /* Media rate */
2ac736a6
RD
3030 if (i == 0 && time >= -1) {
3031 sc->time_offset = time != -1 ? time : -duration;
a5929abe 3032 }
e0977c80 3033 }
baf2ffd3 3034
5a7ba586 3035 if (edit_count > 1)
baf2ffd3
BC
3036 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
3037 "a/v desync might occur, patch welcome\n");
3038
1a3eb042 3039 av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
e0977c80 3040 return 0;
baf25c9d
GC
3041}
3042
6cea494e 3043static const MOVParseTableEntry mov_default_parse_table[] = {
d4fdba0d 3044{ MKTAG('a','v','s','s'), mov_read_extradata },
1cf9f6ed 3045{ MKTAG('c','h','p','l'), mov_read_chpl },
1c126b4f 3046{ MKTAG('c','o','6','4'), mov_read_stco },
0d8a3656 3047{ MKTAG('c','o','l','r'), mov_read_colr },
1c126b4f
BC
3048{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3049{ MKTAG('d','i','n','f'), mov_read_default },
3050{ MKTAG('d','r','e','f'), mov_read_dref },
3051{ MKTAG('e','d','t','s'), mov_read_default },
3052{ MKTAG('e','l','s','t'), mov_read_elst },
3053{ MKTAG('e','n','d','a'), mov_read_enda },
4bf3c8f2 3054{ MKTAG('f','i','e','l'), mov_read_fiel },
1c126b4f
BC
3055{ MKTAG('f','t','y','p'), mov_read_ftyp },
3056{ MKTAG('g','l','b','l'), mov_read_glbl },
3057{ MKTAG('h','d','l','r'), mov_read_hdlr },
51c15201 3058{ MKTAG('i','l','s','t'), mov_read_ilst },
1c126b4f
BC
3059{ MKTAG('j','p','2','h'), mov_read_extradata },
3060{ MKTAG('m','d','a','t'), mov_read_mdat },
3061{ MKTAG('m','d','h','d'), mov_read_mdhd },
3062{ MKTAG('m','d','i','a'), mov_read_default },
51c15201 3063{ MKTAG('m','e','t','a'), mov_read_meta },
1c126b4f
BC
3064{ MKTAG('m','i','n','f'), mov_read_default },
3065{ MKTAG('m','o','o','f'), mov_read_moof },
3066{ MKTAG('m','o','o','v'), mov_read_moov },
3067{ MKTAG('m','v','e','x'), mov_read_default },
3068{ MKTAG('m','v','h','d'), mov_read_mvhd },
3069{ MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
3070{ MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
3071{ MKTAG('a','v','c','C'), mov_read_glbl },
6da54074 3072{ MKTAG('p','a','s','p'), mov_read_pasp },
1c126b4f
BC
3073{ MKTAG('s','t','b','l'), mov_read_default },
3074{ MKTAG('s','t','c','o'), mov_read_stco },
171a3531 3075{ MKTAG('s','t','p','s'), mov_read_stps },
653d7aeb 3076{ MKTAG('s','t','r','f'), mov_read_strf },
1c126b4f
BC
3077{ MKTAG('s','t','s','c'), mov_read_stsc },
3078{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3079{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3080{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3081{ MKTAG('s','t','t','s'), mov_read_stts },
bd27eed6 3082{ MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
1c126b4f 3083{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
a74f8121 3084{ MKTAG('t','f','d','t'), mov_read_tfdt },
1c126b4f
BC
3085{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3086{ MKTAG('t','r','a','k'), mov_read_trak },
3087{ MKTAG('t','r','a','f'), mov_read_default },
7221579b
DC
3088{ MKTAG('t','r','e','f'), mov_read_default },
3089{ MKTAG('c','h','a','p'), mov_read_chap },
1c126b4f
BC
3090{ MKTAG('t','r','e','x'), mov_read_trex },
3091{ MKTAG('t','r','u','n'), mov_read_trun },
86b0affd 3092{ MKTAG('u','d','t','a'), mov_read_default },
1c126b4f
BC
3093{ MKTAG('w','a','v','e'), mov_read_wave },
3094{ MKTAG('e','s','d','s'), mov_read_esds },
bdecdd2b 3095{ MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
546adc1f 3096{ MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
1c126b4f 3097{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
85e9e3a9 3098{ MKTAG('w','f','e','x'), mov_read_wfex },
1c126b4f 3099{ MKTAG('c','m','o','v'), mov_read_cmov },
1fdf18f4 3100{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
89415b8e 3101{ MKTAG('d','v','c','1'), mov_read_dvc1 },
d17d0ec8 3102{ MKTAG('s','b','g','p'), mov_read_sbgp },
ea29f965 3103{ MKTAG('h','v','c','C'), mov_read_glbl },
9a07fac6 3104{ MKTAG('-','-','-','-'), mov_read_custom },
bcfe2ba0 3105{ 0, NULL }
6cea494e
ZK
3106};
3107
1ce1e636
RB
3108static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3109{
3110 int64_t total_size = 0;
3111 MOVAtom a;
3112 int i;
3113
3114 if (atom.size < 0)
3115 atom.size = INT64_MAX;
3116 while (total_size + 8 < atom.size && !pb->eof_reached) {
3117 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3118 a.size = atom.size;
3119 a.type=0;
3120 if (atom.size >= 8) {
3121 a.size = avio_rb32(pb);
3122 a.type = avio_rl32(pb);
3123 }
1a3eb042 3124 av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
1ce1e636
RB
3125 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3126 total_size += 8;
3127 if (a.size == 1) { /* 64 bit extended size */
3128 a.size = avio_rb64(pb) - 8;
3129 total_size += 8;
3130 }
3131 if (a.size == 0) {
3132 a.size = atom.size - total_size;
3133 if (a.size <= 8)
3134 break;
3135 }
3136 a.size -= 8;
3137 if (a.size < 0)
3138 break;
3139 a.size = FFMIN(a.size, atom.size - total_size);
3140
3141 for (i = 0; mov_default_parse_table[i].type; i++)
3142 if (mov_default_parse_table[i].type == a.type) {
3143 parse = mov_default_parse_table[i].parse;
3144 break;
3145 }
3146
3147 // container is user data
3148 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3149 atom.type == MKTAG('i','l','s','t')))
3150 parse = mov_read_udta_string;
3151
3152 if (!parse) { /* skip leaf atoms data */
3153 avio_skip(pb, a.size);
3154 } else {
3155 int64_t start_pos = avio_tell(pb);
3156 int64_t left;
3157 int err = parse(c, pb, a);
3158 if (err < 0)
3159 return err;
3160 if (c->found_moov && c->found_mdat &&
3161 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3162 start_pos + a.size == avio_size(pb))) {
3163 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3164 c->next_root_atom = start_pos + a.size;
3165 return 0;
3166 }
3167 left = a.size - avio_tell(pb) + start_pos;
3168 if (left > 0) /* skip garbage at atom end */
3169 avio_skip(pb, left);
5b4eb243
MS
3170 else if (left < 0) {
3171 av_log(c->fc, AV_LOG_WARNING,
3172 "overread end of atom '%.4s' by %"PRId64" bytes\n",
3173 (char*)&a.type, -left);
3174 avio_seek(pb, left, SEEK_CUR);
3175 }
1ce1e636
RB
3176 }
3177
3178 total_size += a.size;
3179 }
3180
3181 if (total_size < atom.size && atom.size < 0x7ffff)
3182 avio_skip(pb, atom.size - total_size);
3183
3184 return 0;
3185}
3186
c9a65ca8
FB
3187static int mov_probe(AVProbeData *p)
3188{
0e7eed09
FB
3189 unsigned int offset;
3190 uint32_t tag;
2497479f 3191 int score = 0;
3ffe3793 3192
c9a65ca8 3193 /* check file header */
0e7eed09 3194 offset = 0;
5a7ba586 3195 for (;;) {
0e7eed09
FB
3196 /* ignore invalid offset */
3197 if ((offset + 8) > (unsigned int)p->buf_size)
2497479f 3198 return score;
fead30d4 3199 tag = AV_RL32(p->buf + offset + 4);
0e7eed09 3200 switch(tag) {
2497479f 3201 /* check for obvious tags */
1c126b4f
BC
3202 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
3203 case MKTAG('m','o','o','v'):
3204 case MKTAG('m','d','a','t'):
3205 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3206 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
40e7aaa3 3207 case MKTAG('f','t','y','p'):
3ffe3793 3208 return AVPROBE_SCORE_MAX;
2497479f 3209 /* those are more common words, so rate then a bit less */
1c126b4f
BC
3210 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3211 case MKTAG('w','i','d','e'):
3212 case MKTAG('f','r','e','e'):
3213 case MKTAG('j','u','n','k'):
3214 case MKTAG('p','i','c','t'):
2497479f 3215 return AVPROBE_SCORE_MAX - 5;
5469b788 3216 case MKTAG(0x82,0x82,0x7f,0x7d):
1c126b4f
BC
3217 case MKTAG('s','k','i','p'):
3218 case MKTAG('u','u','i','d'):
3219 case MKTAG('p','r','f','l'):
fead30d4 3220 offset = AV_RB32(p->buf+offset) + offset;
2497479f 3221 /* if we only find those cause probedata is too small at least rate them */
e0f8be64 3222 score = AVPROBE_SCORE_EXTENSION;
0e7eed09
FB
3223 break;
3224 default:
3225 /* unrecognized tag */
2497479f 3226 return score;
0e7eed09 3227 }
3ffe3793 3228 }
c9a65ca8
FB
3229}
3230
7221579b
DC
3231// must be done after parsing all trak because there's no order requirement
3232static void mov_read_chapters(AVFormatContext *s)
3233{
3234 MOVContext *mov = s->priv_data;
3235 AVStream *st = NULL;
3236 MOVStreamContext *sc;