summaryrefslogtreecommitdiff
path: root/libavformat/mov.c (plain)
blob: 55de08d00fe54a69c6bf7d01ba2ad57aceb25197
1/*
2 * MOV demuxer
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5 *
6 * first version by Francois Revol <revol@free.fr>
7 * seek function by Gael Chardon <gael.dev@4now.net>
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <inttypes.h>
27#include <limits.h>
28#include <stdint.h>
29
30#include "libavutil/attributes.h"
31#include "libavutil/channel_layout.h"
32#include "libavutil/internal.h"
33#include "libavutil/intreadwrite.h"
34#include "libavutil/intfloat.h"
35#include "libavutil/mathematics.h"
36#include "libavutil/time_internal.h"
37#include "libavutil/avassert.h"
38#include "libavutil/avstring.h"
39#include "libavutil/dict.h"
40#include "libavutil/display.h"
41#include "libavutil/opt.h"
42#include "libavutil/aes.h"
43#include "libavutil/aes_ctr.h"
44#include "libavutil/pixdesc.h"
45#include "libavutil/sha.h"
46#include "libavutil/spherical.h"
47#include "libavutil/stereo3d.h"
48#include "libavutil/timecode.h"
49#include "libavcodec/ac3tab.h"
50#include "libavcodec/flac.h"
51#include "libavcodec/mpegaudiodecheader.h"
52#include "avformat.h"
53#include "internal.h"
54#include "avio_internal.h"
55#include "riff.h"
56#include "isom.h"
57#include "libavcodec/get_bits.h"
58#include "id3v1.h"
59#include "id3v2.h"
60#include "mov_chan.h"
61#include "replaygain.h"
62
63#if CONFIG_ZLIB
64#include <zlib.h>
65#endif
66
67#include "qtpalette.h"
68
69/* those functions parse an atom */
70/* links atom IDs to parse functions */
71typedef struct MOVParseTableEntry {
72 uint32_t type;
73 int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
74} MOVParseTableEntry;
75
76static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
77static int mov_read_mfra(MOVContext *c, AVIOContext *f);
78
79static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
80 unsigned len, const char *key)
81{
82 char buf[16];
83
84 short current, total = 0;
85 avio_rb16(pb); // unknown
86 current = avio_rb16(pb);
87 if (len >= 6)
88 total = avio_rb16(pb);
89 if (!total)
90 snprintf(buf, sizeof(buf), "%d", current);
91 else
92 snprintf(buf, sizeof(buf), "%d/%d", current, total);
93 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
94 av_dict_set(&c->fc->metadata, key, buf, 0);
95
96 return 0;
97}
98
99static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
100 unsigned len, const char *key)
101{
102 /* bypass padding bytes */
103 avio_r8(pb);
104 avio_r8(pb);
105 avio_r8(pb);
106
107 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
108 av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
109
110 return 0;
111}
112
113static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
114 unsigned len, const char *key)
115{
116 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
117 av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
118
119 return 0;
120}
121
122static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
123 unsigned len, const char *key)
124{
125 short genre;
126
127 avio_r8(pb); // unknown
128
129 genre = avio_r8(pb);
130 if (genre < 1 || genre > ID3v1_GENRE_MAX)
131 return 0;
132 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
133 av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
134
135 return 0;
136}
137
138static const uint32_t mac_to_unicode[128] = {
139 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
140 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
141 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
142 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
143 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
144 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
145 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
146 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
147 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
148 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
149 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
150 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
151 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
152 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
153 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
154 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
155};
156
157static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
158 char *dst, int dstlen)
159{
160 char *p = dst;
161 char *end = dst+dstlen-1;
162 int i;
163
164 for (i = 0; i < len; i++) {
165 uint8_t t, c = avio_r8(pb);
166 if (c < 0x80 && p < end)
167 *p++ = c;
168 else if (p < end)
169 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
170 }
171 *p = 0;
172 return p - dst;
173}
174
175static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
176{
177 AVPacket pkt;
178 AVStream *st;
179 MOVStreamContext *sc;
180 enum AVCodecID id;
181 int ret;
182
183 switch (type) {
184 case 0xd: id = AV_CODEC_ID_MJPEG; break;
185 case 0xe: id = AV_CODEC_ID_PNG; break;
186 case 0x1b: id = AV_CODEC_ID_BMP; break;
187 default:
188 av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
189 avio_skip(pb, len);
190 return 0;
191 }
192
193 st = avformat_new_stream(c->fc, NULL);
194 if (!st)
195 return AVERROR(ENOMEM);
196 sc = av_mallocz(sizeof(*sc));
197 if (!sc)
198 return AVERROR(ENOMEM);
199 st->priv_data = sc;
200
201 ret = av_get_packet(pb, &pkt, len);
202 if (ret < 0)
203 return ret;
204
205 if (pkt.size >= 8 && id != AV_CODEC_ID_BMP) {
206 if (AV_RB64(pkt.data) == 0x89504e470d0a1a0a) {
207 id = AV_CODEC_ID_PNG;
208 } else {
209 id = AV_CODEC_ID_MJPEG;
210 }
211 }
212
213 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
214
215 st->attached_pic = pkt;
216 st->attached_pic.stream_index = st->index;
217 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
218
219 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
220 st->codecpar->codec_id = id;
221
222 return 0;
223}
224
225// 3GPP TS 26.244
226static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
227{
228 char language[4] = { 0 };
229 char buf[200], place[100];
230 uint16_t langcode = 0;
231 double longitude, latitude, altitude;
232 const char *key = "location";
233
234 if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
235 av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
236 return AVERROR_INVALIDDATA;
237 }
238
239 avio_skip(pb, 4); // version+flags
240 langcode = avio_rb16(pb);
241 ff_mov_lang_to_iso639(langcode, language);
242 len -= 6;
243
244 len -= avio_get_str(pb, len, place, sizeof(place));
245 if (len < 1) {
246 av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
247 return AVERROR_INVALIDDATA;
248 }
249 avio_skip(pb, 1); // role
250 len -= 1;
251
252 if (len < 12) {
253 av_log(c->fc, AV_LOG_ERROR,
254 "loci too short (%u bytes left, need at least %d)\n", len, 12);
255 return AVERROR_INVALIDDATA;
256 }
257 longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
258 latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
259 altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
260
261 // Try to output in the same format as the ?xyz field
262 snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
263 if (altitude)
264 av_strlcatf(buf, sizeof(buf), "%+f", altitude);
265 av_strlcatf(buf, sizeof(buf), "/%s", place);
266
267 if (*language && strcmp(language, "und")) {
268 char key2[16];
269 snprintf(key2, sizeof(key2), "%s-%s", key, language);
270 av_dict_set(&c->fc->metadata, key2, buf, 0);
271 }
272 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
273 return av_dict_set(&c->fc->metadata, key, buf, 0);
274}
275
276static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
277{
278 int i, n_hmmt;
279
280 if (len < 2)
281 return 0;
282 if (c->ignore_chapters)
283 return 0;
284
285 n_hmmt = avio_rb32(pb);
286 for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
287 int moment_time = avio_rb32(pb);
288 avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
289 }
290 return 0;
291}
292
293static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
294{
295 char tmp_key[5];
296 char key2[32], language[4] = {0};
297 char *str = NULL;
298 const char *key = NULL;
299 uint16_t langcode = 0;
300 uint32_t data_type = 0, str_size, str_size_alloc;
301 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
302 int raw = 0;
303 int num = 0;
304
305 switch (atom.type) {
306 case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
307 case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
308 case MKTAG( 'X','M','P','_'):
309 if (c->export_xmp) { key = "xmp"; raw = 1; } break;
310 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
311 case MKTAG( 'a','k','I','D'): key = "account_type";
312 parse = mov_metadata_int8_no_padding; break;
313 case MKTAG( 'a','p','I','D'): key = "account_id"; break;
314 case MKTAG( 'c','a','t','g'): key = "category"; break;
315 case MKTAG( 'c','p','i','l'): key = "compilation";
316 parse = mov_metadata_int8_no_padding; break;
317 case MKTAG( 'c','p','r','t'): key = "copyright"; break;
318 case MKTAG( 'd','e','s','c'): key = "description"; break;
319 case MKTAG( 'd','i','s','k'): key = "disc";
320 parse = mov_metadata_track_or_disc_number; break;
321 case MKTAG( 'e','g','i','d'): key = "episode_uid";
322 parse = mov_metadata_int8_no_padding; break;
323 case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
324 case MKTAG( 'g','n','r','e'): key = "genre";
325 parse = mov_metadata_gnre; break;
326 case MKTAG( 'h','d','v','d'): key = "hd_video";
327 parse = mov_metadata_int8_no_padding; break;
328 case MKTAG( 'H','M','M','T'):
329 return mov_metadata_hmmt(c, pb, atom.size);
330 case MKTAG( 'k','e','y','w'): key = "keywords"; break;
331 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
332 case MKTAG( 'l','o','c','i'):
333 return mov_metadata_loci(c, pb, atom.size);
334 case MKTAG( 'p','c','s','t'): key = "podcast";
335 parse = mov_metadata_int8_no_padding; break;
336 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
337 parse = mov_metadata_int8_no_padding; break;
338 case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
339 case MKTAG( 'r','t','n','g'): key = "rating";
340 parse = mov_metadata_int8_no_padding; break;
341 case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
342 case MKTAG( 's','o','a','l'): key = "sort_album"; break;
343 case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
344 case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
345 case MKTAG( 's','o','n','m'): key = "sort_name"; break;
346 case MKTAG( 's','o','s','n'): key = "sort_show"; break;
347 case MKTAG( 's','t','i','k'): key = "media_type";
348 parse = mov_metadata_int8_no_padding; break;
349 case MKTAG( 't','r','k','n'): key = "track";
350 parse = mov_metadata_track_or_disc_number; break;
351 case MKTAG( 't','v','e','n'): key = "episode_id"; break;
352 case MKTAG( 't','v','e','s'): key = "episode_sort";
353 parse = mov_metadata_int8_bypass_padding; break;
354 case MKTAG( 't','v','n','n'): key = "network"; break;
355 case MKTAG( 't','v','s','h'): key = "show"; break;
356 case MKTAG( 't','v','s','n'): key = "season_number";
357 parse = mov_metadata_int8_bypass_padding; break;
358 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
359 case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
360 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
361 case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
362 case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
363 case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
364 case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
365 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
366 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
367 case MKTAG(0xa9,'d','i','r'): key = "director"; break;
368 case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
369 case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
370 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
371 case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
372 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
373 case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
374 case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
375 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
376 case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
377 case MKTAG(0xa9,'m','a','k'): key = "make"; break;
378 case MKTAG(0xa9,'m','o','d'): key = "model"; break;
379 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
380 case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
381 case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
382 case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
383 case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
384 case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
385 case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
386 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
387 case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
388 case MKTAG(0xa9,'t','r','k'): key = "track"; break;
389 case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
390 case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
391 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
392 case MKTAG(0xa9,'x','y','z'): key = "location"; break;
393 }
394retry:
395 if (c->itunes_metadata && atom.size > 8) {
396 int data_size = avio_rb32(pb);
397 int tag = avio_rl32(pb);
398 if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
399 data_type = avio_rb32(pb); // type
400 avio_rb32(pb); // unknown
401 str_size = data_size - 16;
402 atom.size -= 16;
403
404 if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
405 int ret = mov_read_covr(c, pb, data_type, str_size);
406 if (ret < 0) {
407 av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
408 }
409 return ret;
410 } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
411 uint32_t index = AV_RB32(&atom.type);
412 if (index < c->meta_keys_count && index > 0) {
413 key = c->meta_keys[index];
414 } else {
415 av_log(c->fc, AV_LOG_WARNING,
416 "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
417 index, c->meta_keys_count);
418 }
419 }
420 } else return 0;
421 } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
422 str_size = avio_rb16(pb); // string length
423 if (str_size > atom.size) {
424 raw = 1;
425 avio_seek(pb, -2, SEEK_CUR);
426 av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
427 goto retry;
428 }
429 langcode = avio_rb16(pb);
430 ff_mov_lang_to_iso639(langcode, language);
431 atom.size -= 4;
432 } else
433 str_size = atom.size;
434
435 if (c->export_all && !key) {
436 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
437 key = tmp_key;
438 }
439
440 if (!key)
441 return 0;
442 if (atom.size < 0 || str_size >= INT_MAX/2)
443 return AVERROR_INVALIDDATA;
444
445 // Allocates enough space if data_type is a int32 or float32 number, otherwise
446 // worst-case requirement for output string in case of utf8 coded input
447 num = (data_type >= 21 && data_type <= 23);
448 str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
449 str = av_mallocz(str_size_alloc);
450 if (!str)
451 return AVERROR(ENOMEM);
452
453 if (parse)
454 parse(c, pb, str_size, key);
455 else {
456 if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
457 mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
458 } else if (data_type == 21) { // BE signed integer, variable size
459 int val = 0;
460 if (str_size == 1)
461 val = (int8_t)avio_r8(pb);
462 else if (str_size == 2)
463 val = (int16_t)avio_rb16(pb);
464 else if (str_size == 3)
465 val = ((int32_t)(avio_rb24(pb)<<8))>>8;
466 else if (str_size == 4)
467 val = (int32_t)avio_rb32(pb);
468 if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
469 av_log(c->fc, AV_LOG_ERROR,
470 "Failed to store the number (%d) in string.\n", val);
471 av_free(str);
472 return AVERROR_INVALIDDATA;
473 }
474 } else if (data_type == 22) { // BE unsigned integer, variable size
475 unsigned int val = 0;
476 if (str_size == 1)
477 val = avio_r8(pb);
478 else if (str_size == 2)
479 val = avio_rb16(pb);
480 else if (str_size == 3)
481 val = avio_rb24(pb);
482 else if (str_size == 4)
483 val = avio_rb32(pb);
484 if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
485 av_log(c->fc, AV_LOG_ERROR,
486 "Failed to store the number (%u) in string.\n", val);
487 av_free(str);
488 return AVERROR_INVALIDDATA;
489 }
490 } else if (data_type == 23 && str_size >= 4) { // BE float32
491 float val = av_int2float(avio_rb32(pb));
492 if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
493 av_log(c->fc, AV_LOG_ERROR,
494 "Failed to store the float32 number (%f) in string.\n", val);
495 av_free(str);
496 return AVERROR_INVALIDDATA;
497 }
498 } else {
499 int ret = ffio_read_size(pb, str, str_size);
500 if (ret < 0) {
501 av_free(str);
502 return ret;
503 }
504 str[str_size] = 0;
505 }
506 c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
507 av_dict_set(&c->fc->metadata, key, str, 0);
508 if (*language && strcmp(language, "und")) {
509 snprintf(key2, sizeof(key2), "%s-%s", key, language);
510 av_dict_set(&c->fc->metadata, key2, str, 0);
511 }
512 if (!strcmp(key, "encoder")) {
513 int major, minor, micro;
514 if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
515 c->handbrake_version = 1000000*major + 1000*minor + micro;
516 }
517 }
518 }
519
520 av_freep(&str);
521 return 0;
522}
523
524static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
525{
526 int64_t start;
527 int i, nb_chapters, str_len, version;
528 char str[256+1];
529 int ret;
530
531 if (c->ignore_chapters)
532 return 0;
533
534 if ((atom.size -= 5) < 0)
535 return 0;
536
537 version = avio_r8(pb);
538 avio_rb24(pb);
539 if (version)
540 avio_rb32(pb); // ???
541 nb_chapters = avio_r8(pb);
542
543 for (i = 0; i < nb_chapters; i++) {
544 if (atom.size < 9)
545 return 0;
546
547 start = avio_rb64(pb);
548 str_len = avio_r8(pb);
549
550 if ((atom.size -= 9+str_len) < 0)
551 return 0;
552
553 ret = ffio_read_size(pb, str, str_len);
554 if (ret < 0)
555 return ret;
556 str[str_len] = 0;
557 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
558 }
559 return 0;
560}
561
562#define MIN_DATA_ENTRY_BOX_SIZE 12
563static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
564{
565 AVStream *st;
566 MOVStreamContext *sc;
567 int entries, i, j;
568
569 if (c->fc->nb_streams < 1)
570 return 0;
571 st = c->fc->streams[c->fc->nb_streams-1];
572 sc = st->priv_data;
573
574 avio_rb32(pb); // version + flags
575 entries = avio_rb32(pb);
576 if (!entries ||
577 entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
578 entries >= UINT_MAX / sizeof(*sc->drefs))
579 return AVERROR_INVALIDDATA;
580 sc->drefs_count = 0;
581 av_free(sc->drefs);
582 sc->drefs_count = 0;
583 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
584 if (!sc->drefs)
585 return AVERROR(ENOMEM);
586 sc->drefs_count = entries;
587
588 for (i = 0; i < entries; i++) {
589 MOVDref *dref = &sc->drefs[i];
590 uint32_t size = avio_rb32(pb);
591 int64_t next = avio_tell(pb) + size - 4;
592
593 if (size < 12)
594 return AVERROR_INVALIDDATA;
595
596 dref->type = avio_rl32(pb);
597 avio_rb32(pb); // version + flags
598
599 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
600 /* macintosh alias record */
601 uint16_t volume_len, len;
602 int16_t type;
603 int ret;
604
605 avio_skip(pb, 10);
606
607 volume_len = avio_r8(pb);
608 volume_len = FFMIN(volume_len, 27);
609 ret = ffio_read_size(pb, dref->volume, 27);
610 if (ret < 0)
611 return ret;
612 dref->volume[volume_len] = 0;
613 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
614
615 avio_skip(pb, 12);
616
617 len = avio_r8(pb);
618 len = FFMIN(len, 63);
619 ret = ffio_read_size(pb, dref->filename, 63);
620 if (ret < 0)
621 return ret;
622 dref->filename[len] = 0;
623 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
624
625 avio_skip(pb, 16);
626
627 /* read next level up_from_alias/down_to_target */
628 dref->nlvl_from = avio_rb16(pb);
629 dref->nlvl_to = avio_rb16(pb);
630 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
631 dref->nlvl_from, dref->nlvl_to);
632
633 avio_skip(pb, 16);
634
635 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
636 if(avio_feof(pb))
637 return AVERROR_EOF;
638 type = avio_rb16(pb);
639 len = avio_rb16(pb);
640 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
641 if (len&1)
642 len += 1;
643 if (type == 2) { // absolute path
644 av_free(dref->path);
645 dref->path = av_mallocz(len+1);
646 if (!dref->path)
647 return AVERROR(ENOMEM);
648
649 ret = ffio_read_size(pb, dref->path, len);
650 if (ret < 0) {
651 av_freep(&dref->path);
652 return ret;
653 }
654 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
655 len -= volume_len;
656 memmove(dref->path, dref->path+volume_len, len);
657 dref->path[len] = 0;
658 }
659 // trim string of any ending zeros
660 for (j = len - 1; j >= 0; j--) {
661 if (dref->path[j] == 0)
662 len--;
663 else
664 break;
665 }
666 for (j = 0; j < len; j++)
667 if (dref->path[j] == ':' || dref->path[j] == 0)
668 dref->path[j] = '/';
669 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
670 } else if (type == 0) { // directory name
671 av_free(dref->dir);
672 dref->dir = av_malloc(len+1);
673 if (!dref->dir)
674 return AVERROR(ENOMEM);
675
676 ret = ffio_read_size(pb, dref->dir, len);
677 if (ret < 0) {
678 av_freep(&dref->dir);
679 return ret;
680 }
681 dref->dir[len] = 0;
682 for (j = 0; j < len; j++)
683 if (dref->dir[j] == ':')
684 dref->dir[j] = '/';
685 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
686 } else
687 avio_skip(pb, len);
688 }
689 } else {
690 av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
691 dref->type, size);
692 entries--;
693 i--;
694 }
695 avio_seek(pb, next, SEEK_SET);
696 }
697 return 0;
698}
699
700static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
701{
702 AVStream *st;
703 uint32_t type;
704 uint32_t ctype;
705 int64_t title_size;
706 char *title_str;
707 int ret;
708
709 avio_r8(pb); /* version */
710 avio_rb24(pb); /* flags */
711
712 /* component type */
713 ctype = avio_rl32(pb);
714 type = avio_rl32(pb); /* component subtype */
715
716 av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
717 av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
718
719 if (c->trak_index < 0) { // meta not inside a trak
720 if (type == MKTAG('m','d','t','a')) {
721 c->found_hdlr_mdta = 1;
722 }
723 return 0;
724 }
725
726 st = c->fc->streams[c->fc->nb_streams-1];
727
728 if (type == MKTAG('v','i','d','e'))
729 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
730 else if (type == MKTAG('s','o','u','n'))
731 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
732 else if (type == MKTAG('m','1','a',' '))
733 st->codecpar->codec_id = AV_CODEC_ID_MP2;
734 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
735 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
736
737 avio_rb32(pb); /* component manufacture */
738 avio_rb32(pb); /* component flags */
739 avio_rb32(pb); /* component flags mask */
740
741 title_size = atom.size - 24;
742 if (title_size > 0) {
743 if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
744 return AVERROR_INVALIDDATA;
745 title_str = av_malloc(title_size + 1); /* Add null terminator */
746 if (!title_str)
747 return AVERROR(ENOMEM);
748
749 ret = ffio_read_size(pb, title_str, title_size);
750 if (ret < 0) {
751 av_freep(&title_str);
752 return ret;
753 }
754 title_str[title_size] = 0;
755 if (title_str[0]) {
756 int off = (!c->isom && title_str[0] == title_size - 1);
757 av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
758 }
759 av_freep(&title_str);
760 }
761
762 return 0;
763}
764
765int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
766{
767 AVStream *st;
768 int tag;
769
770 if (fc->nb_streams < 1)
771 return 0;
772 st = fc->streams[fc->nb_streams-1];
773
774 avio_rb32(pb); /* version + flags */
775 ff_mp4_read_descr(fc, pb, &tag);
776 if (tag == MP4ESDescrTag) {
777 ff_mp4_parse_es_descr(pb, NULL);
778 } else
779 avio_rb16(pb); /* ID */
780
781 ff_mp4_read_descr(fc, pb, &tag);
782 if (tag == MP4DecConfigDescrTag)
783 ff_mp4_read_dec_config_descr(fc, st, pb);
784 return 0;
785}
786
787static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
788{
789 return ff_mov_read_esds(c->fc, pb);
790}
791
792static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
793{
794 AVStream *st;
795 enum AVAudioServiceType *ast;
796 int ac3info, acmod, lfeon, bsmod;
797
798 if (c->fc->nb_streams < 1)
799 return 0;
800 st = c->fc->streams[c->fc->nb_streams-1];
801
802 ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
803 sizeof(*ast));
804 if (!ast)
805 return AVERROR(ENOMEM);
806
807 ac3info = avio_rb24(pb);
808 bsmod = (ac3info >> 14) & 0x7;
809 acmod = (ac3info >> 11) & 0x7;
810 lfeon = (ac3info >> 10) & 0x1;
811 st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
812 st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
813 if (lfeon)
814 st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
815 *ast = bsmod;
816 if (st->codecpar->channels > 1 && bsmod == 0x7)
817 *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
818
819#if FF_API_LAVF_AVCTX
820 FF_DISABLE_DEPRECATION_WARNINGS
821 st->codec->audio_service_type = *ast;
822 FF_ENABLE_DEPRECATION_WARNINGS
823#endif
824
825 return 0;
826}
827
828static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
829{
830 AVStream *st;
831 enum AVAudioServiceType *ast;
832 int eac3info, acmod, lfeon, bsmod;
833
834 if (c->fc->nb_streams < 1)
835 return 0;
836 st = c->fc->streams[c->fc->nb_streams-1];
837
838 ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE,
839 sizeof(*ast));
840 if (!ast)
841 return AVERROR(ENOMEM);
842
843 /* No need to parse fields for additional independent substreams and its
844 * associated dependent substreams since libavcodec's E-AC-3 decoder
845 * does not support them yet. */
846 avio_rb16(pb); /* data_rate and num_ind_sub */
847 eac3info = avio_rb24(pb);
848 bsmod = (eac3info >> 12) & 0x1f;
849 acmod = (eac3info >> 9) & 0x7;
850 lfeon = (eac3info >> 8) & 0x1;
851 st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
852 if (lfeon)
853 st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
854 st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
855 *ast = bsmod;
856 if (st->codecpar->channels > 1 && bsmod == 0x7)
857 *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
858
859#if FF_API_LAVF_AVCTX
860 FF_DISABLE_DEPRECATION_WARNINGS
861 st->codec->audio_service_type = *ast;
862 FF_ENABLE_DEPRECATION_WARNINGS
863#endif
864
865 return 0;
866}
867
868static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
869{
870 const uint32_t ddts_size = 20;
871 AVStream *st = NULL;
872 uint8_t *buf = NULL;
873 uint32_t frame_duration_code = 0;
874 uint32_t channel_layout_code = 0;
875 GetBitContext gb;
876
877 buf = av_malloc(ddts_size + AV_INPUT_BUFFER_PADDING_SIZE);
878 if (!buf) {
879 return AVERROR(ENOMEM);
880 }
881 if (avio_read(pb, buf, ddts_size) < ddts_size) {
882 av_free(buf);
883 return AVERROR_INVALIDDATA;
884 }
885
886 init_get_bits(&gb, buf, 8*ddts_size);
887
888 if (c->fc->nb_streams < 1) {
889 return 0;
890 }
891 st = c->fc->streams[c->fc->nb_streams-1];
892
893 st->codecpar->sample_rate = get_bits_long(&gb, 32);
894 if (st->codecpar->sample_rate <= 0) {
895 av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
896 return AVERROR_INVALIDDATA;
897 }
898 skip_bits_long(&gb, 32); /* max bitrate */
899 st->codecpar->bit_rate = get_bits_long(&gb, 32);
900 st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
901 frame_duration_code = get_bits(&gb, 2);
902 skip_bits(&gb, 30); /* various fields */
903 channel_layout_code = get_bits(&gb, 16);
904
905 st->codecpar->frame_size =
906 (frame_duration_code == 0) ? 512 :
907 (frame_duration_code == 1) ? 1024 :
908 (frame_duration_code == 2) ? 2048 :
909 (frame_duration_code == 3) ? 4096 : 0;
910
911 if (channel_layout_code > 0xff) {
912 av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout");
913 }
914 st->codecpar->channel_layout =
915 ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
916 ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
917 ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
918 ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
919 ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
920 ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
921
922 st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
923
924 return 0;
925}
926
927static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
928{
929 AVStream *st;
930
931 if (c->fc->nb_streams < 1)
932 return 0;
933 st = c->fc->streams[c->fc->nb_streams-1];
934
935 if (atom.size < 16)
936 return 0;
937
938 /* skip version and flags */
939 avio_skip(pb, 4);
940
941 ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
942
943 return 0;
944}
945
946static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
947{
948 AVStream *st;
949 int ret;
950
951 if (c->fc->nb_streams < 1)
952 return 0;
953 st = c->fc->streams[c->fc->nb_streams-1];
954
955 if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
956 av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
957
958 return ret;
959}
960
961static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
962{
963 const int num = avio_rb32(pb);
964 const int den = avio_rb32(pb);
965 AVStream *st;
966
967 if (c->fc->nb_streams < 1)
968 return 0;
969 st = c->fc->streams[c->fc->nb_streams-1];
970
971 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
972 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
973 av_log(c->fc, AV_LOG_WARNING,
974 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
975 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
976 num, den);
977 } else if (den != 0) {
978 av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
979 num, den, 32767);
980 }
981 return 0;
982}
983
984/* this atom contains actual media data */
985static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
986{
987 if (atom.size == 0) /* wrong one (MP4) */
988 return 0;
989 c->found_mdat=1;
990 return 0; /* now go for moov */
991}
992
993#define DRM_BLOB_SIZE 56
994
995static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
996{
997 uint8_t intermediate_key[20];
998 uint8_t intermediate_iv[20];
999 uint8_t input[64];
1000 uint8_t output[64];
1001 uint8_t file_checksum[20];
1002 uint8_t calculated_checksum[20];
1003 struct AVSHA *sha;
1004 int i;
1005 int ret = 0;
1006 uint8_t *activation_bytes = c->activation_bytes;
1007 uint8_t *fixed_key = c->audible_fixed_key;
1008
1009 c->aax_mode = 1;
1010
1011 sha = av_sha_alloc();
1012 if (!sha)
1013 return AVERROR(ENOMEM);
1014 c->aes_decrypt = av_aes_alloc();
1015 if (!c->aes_decrypt) {
1016 ret = AVERROR(ENOMEM);
1017 goto fail;
1018 }
1019
1020 /* drm blob processing */
1021 avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1022 avio_read(pb, input, DRM_BLOB_SIZE);
1023 avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1024 avio_read(pb, file_checksum, 20);
1025
1026 av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
1027 for (i = 0; i < 20; i++)
1028 av_log(c->fc, AV_LOG_INFO, "%02x", file_checksum[i]);
1029 av_log(c->fc, AV_LOG_INFO, "\n");
1030
1031 /* verify activation data */
1032 if (!activation_bytes) {
1033 av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1034 ret = 0; /* allow ffprobe to continue working on .aax files */
1035 goto fail;
1036 }
1037 if (c->activation_bytes_size != 4) {
1038 av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1039 ret = AVERROR(EINVAL);
1040 goto fail;
1041 }
1042
1043 /* verify fixed key */
1044 if (c->audible_fixed_key_size != 16) {
1045 av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1046 ret = AVERROR(EINVAL);
1047 goto fail;
1048 }
1049
1050 /* AAX (and AAX+) key derivation */
1051 av_sha_init(sha, 160);
1052 av_sha_update(sha, fixed_key, 16);
1053 av_sha_update(sha, activation_bytes, 4);
1054 av_sha_final(sha, intermediate_key);
1055 av_sha_init(sha, 160);
1056 av_sha_update(sha, fixed_key, 16);
1057 av_sha_update(sha, intermediate_key, 20);
1058 av_sha_update(sha, activation_bytes, 4);
1059 av_sha_final(sha, intermediate_iv);
1060 av_sha_init(sha, 160);
1061 av_sha_update(sha, intermediate_key, 16);
1062 av_sha_update(sha, intermediate_iv, 16);
1063 av_sha_final(sha, calculated_checksum);
1064 if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1065 av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1066 ret = AVERROR_INVALIDDATA;
1067 goto fail;
1068 }
1069 av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1070 av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1071 for (i = 0; i < 4; i++) {
1072 // file data (in output) is stored in big-endian mode
1073 if (activation_bytes[i] != output[3 - i]) { // critical error
1074 av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1075 ret = AVERROR_INVALIDDATA;
1076 goto fail;
1077 }
1078 }
1079 memcpy(c->file_key, output + 8, 16);
1080 memcpy(input, output + 26, 16);
1081 av_sha_init(sha, 160);
1082 av_sha_update(sha, input, 16);
1083 av_sha_update(sha, c->file_key, 16);
1084 av_sha_update(sha, fixed_key, 16);
1085 av_sha_final(sha, c->file_iv);
1086
1087fail:
1088 av_free(sha);
1089
1090 return ret;
1091}
1092
1093// Audible AAX (and AAX+) bytestream decryption
1094static int aax_filter(uint8_t *input, int size, MOVContext *c)
1095{
1096 int blocks = 0;
1097 unsigned char iv[16];
1098
1099 memcpy(iv, c->file_iv, 16); // iv is overwritten
1100 blocks = size >> 4; // trailing bytes are not encrypted!
1101 av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1102 av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1103
1104 return 0;
1105}
1106
1107/* read major brand, minor version and compatible brands and store them as metadata */
1108static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1109{
1110 uint32_t minor_ver;
1111 int comp_brand_size;
1112 char* comp_brands_str;
1113 uint8_t type[5] = {0};
1114 int ret = ffio_read_size(pb, type, 4);
1115 if (ret < 0)
1116 return ret;
1117
1118 if (strcmp(type, "qt "))
1119 c->isom = 1;
1120 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1121 av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1122 minor_ver = avio_rb32(pb); /* minor version */
1123 av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1124
1125 comp_brand_size = atom.size - 8;
1126 if (comp_brand_size < 0)
1127 return AVERROR_INVALIDDATA;
1128 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1129 if (!comp_brands_str)
1130 return AVERROR(ENOMEM);
1131
1132 ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1133 if (ret < 0) {
1134 av_freep(&comp_brands_str);
1135 return ret;
1136 }
1137 comp_brands_str[comp_brand_size] = 0;
1138 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
1139 av_freep(&comp_brands_str);
1140
1141 return 0;
1142}
1143
1144/* this atom should contain all header atoms */
1145static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1146{
1147 int ret;
1148
1149 if (c->found_moov) {
1150 av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1151 avio_skip(pb, atom.size);
1152 return 0;
1153 }
1154
1155 if ((ret = mov_read_default(c, pb, atom)) < 0)
1156 return ret;
1157 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1158 /* so we don't parse the whole file if over a network */
1159 c->found_moov=1;
1160 return 0; /* now go for mdat */
1161}
1162
1163static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1164{
1165 if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1166 c->has_looked_for_mfra = 1;
1167 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1168 int ret;
1169 av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1170 "for a mfra\n");
1171 if ((ret = mov_read_mfra(c, pb)) < 0) {
1172 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1173 "read the mfra (may be a live ismv)\n");
1174 }
1175 } else {
1176 av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1177 "seekable, can not look for mfra\n");
1178 }
1179 }
1180 c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1181 av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1182 return mov_read_default(c, pb, atom);
1183}
1184
1185static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
1186{
1187 if (time) {
1188 if(time >= 2082844800)
1189 time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1190
1191 if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1192 av_log(NULL, AV_LOG_DEBUG, "creation_time is not representable\n");
1193 return;
1194 }
1195
1196 avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1197 }
1198}
1199
1200static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1201{
1202 AVStream *st;
1203 MOVStreamContext *sc;
1204 int version;
1205 char language[4] = {0};
1206 unsigned lang;
1207 int64_t creation_time;
1208
1209 if (c->fc->nb_streams < 1)
1210 return 0;
1211 st = c->fc->streams[c->fc->nb_streams-1];
1212 sc = st->priv_data;
1213
1214 if (sc->time_scale) {
1215 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1216 return AVERROR_INVALIDDATA;
1217 }
1218
1219 version = avio_r8(pb);
1220 if (version > 1) {
1221 avpriv_request_sample(c->fc, "Version %d", version);
1222 return AVERROR_PATCHWELCOME;
1223 }
1224 avio_rb24(pb); /* flags */
1225 if (version == 1) {
1226 creation_time = avio_rb64(pb);
1227 avio_rb64(pb);
1228 } else {
1229 creation_time = avio_rb32(pb);
1230 avio_rb32(pb); /* modification time */
1231 }
1232 mov_metadata_creation_time(&st->metadata, creation_time);
1233
1234 sc->time_scale = avio_rb32(pb);
1235 if (sc->time_scale <= 0) {
1236 av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d\n", sc->time_scale);
1237 return AVERROR_INVALIDDATA;
1238 }
1239 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1240
1241 lang = avio_rb16(pb); /* language */
1242 if (ff_mov_lang_to_iso639(lang, language))
1243 av_dict_set(&st->metadata, "language", language, 0);
1244 avio_rb16(pb); /* quality */
1245
1246 return 0;
1247}
1248
1249static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1250{
1251 int i;
1252 int64_t creation_time;
1253 int version = avio_r8(pb); /* version */
1254 avio_rb24(pb); /* flags */
1255
1256 if (version == 1) {
1257 creation_time = avio_rb64(pb);
1258 avio_rb64(pb);
1259 } else {
1260 creation_time = avio_rb32(pb);
1261 avio_rb32(pb); /* modification time */
1262 }
1263 mov_metadata_creation_time(&c->fc->metadata, creation_time);
1264 c->time_scale = avio_rb32(pb); /* time scale */
1265 if (c->time_scale <= 0) {
1266 av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d\n", c->time_scale);
1267 return AVERROR_INVALIDDATA;
1268 }
1269 av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1270
1271 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1272 // set the AVCodecContext duration because the duration of individual tracks
1273 // may be inaccurate
1274 if (c->time_scale > 0 && !c->trex_data)
1275 c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1276 avio_rb32(pb); /* preferred scale */
1277
1278 avio_rb16(pb); /* preferred volume */
1279
1280 avio_skip(pb, 10); /* reserved */
1281
1282 /* movie display matrix, store it in main context and use it later on */
1283 for (i = 0; i < 3; i++) {
1284 c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1285 c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1286 c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
1287 }
1288
1289 avio_rb32(pb); /* preview time */
1290 avio_rb32(pb); /* preview duration */
1291 avio_rb32(pb); /* poster time */
1292 avio_rb32(pb); /* selection time */
1293 avio_rb32(pb); /* selection duration */
1294 avio_rb32(pb); /* current time */
1295 avio_rb32(pb); /* next track ID */
1296
1297 return 0;
1298}
1299
1300static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1301{
1302 AVStream *st;
1303 int little_endian;
1304
1305 if (c->fc->nb_streams < 1)
1306 return 0;
1307 st = c->fc->streams[c->fc->nb_streams-1];
1308
1309 little_endian = avio_rb16(pb) & 0xFF;
1310 av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1311 if (little_endian == 1) {
1312 switch (st->codecpar->codec_id) {
1313 case AV_CODEC_ID_PCM_S24BE:
1314 st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
1315 break;
1316 case AV_CODEC_ID_PCM_S32BE:
1317 st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
1318 break;
1319 case AV_CODEC_ID_PCM_F32BE:
1320 st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
1321 break;
1322 case AV_CODEC_ID_PCM_F64BE:
1323 st->codecpar->codec_id = AV_CODEC_ID_PCM_F64LE;
1324 break;
1325 default:
1326 break;
1327 }
1328 }
1329 return 0;
1330}
1331
1332static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1333{
1334 AVStream *st;
1335 char color_parameter_type[5] = { 0 };
1336 uint16_t color_primaries, color_trc, color_matrix;
1337 int ret;
1338
1339 if (c->fc->nb_streams < 1)
1340 return 0;
1341 st = c->fc->streams[c->fc->nb_streams - 1];
1342
1343 ret = ffio_read_size(pb, color_parameter_type, 4);
1344 if (ret < 0)
1345 return ret;
1346 if (strncmp(color_parameter_type, "nclx", 4) &&
1347 strncmp(color_parameter_type, "nclc", 4)) {
1348 av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1349 color_parameter_type);
1350 return 0;
1351 }
1352
1353 color_primaries = avio_rb16(pb);
1354 color_trc = avio_rb16(pb);
1355 color_matrix = avio_rb16(pb);
1356
1357 av_log(c->fc, AV_LOG_TRACE,
1358 "%s: pri %d trc %d matrix %d",
1359 color_parameter_type, color_primaries, color_trc, color_matrix);
1360
1361 if (!strncmp(color_parameter_type, "nclx", 4)) {
1362 uint8_t color_range = avio_r8(pb) >> 7;
1363 av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1364 if (color_range)
1365 st->codecpar->color_range = AVCOL_RANGE_JPEG;
1366 else
1367 st->codecpar->color_range = AVCOL_RANGE_MPEG;
1368 }
1369
1370 if (!av_color_primaries_name(color_primaries))
1371 color_primaries = AVCOL_PRI_UNSPECIFIED;
1372 if (!av_color_transfer_name(color_trc))
1373 color_trc = AVCOL_TRC_UNSPECIFIED;
1374 if (!av_color_space_name(color_matrix))
1375 color_matrix = AVCOL_SPC_UNSPECIFIED;
1376
1377 st->codecpar->color_primaries = color_primaries;
1378 st->codecpar->color_trc = color_trc;
1379 st->codecpar->color_space = color_matrix;
1380 av_log(c->fc, AV_LOG_TRACE, "\n");
1381
1382 return 0;
1383}
1384
1385static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1386{
1387 AVStream *st;
1388 unsigned mov_field_order;
1389 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1390
1391 if (c->fc->nb_streams < 1) // will happen with jp2 files
1392 return 0;
1393 st = c->fc->streams[c->fc->nb_streams-1];
1394 if (atom.size < 2)
1395 return AVERROR_INVALIDDATA;
1396 mov_field_order = avio_rb16(pb);
1397 if ((mov_field_order & 0xFF00) == 0x0100)
1398 decoded_field_order = AV_FIELD_PROGRESSIVE;
1399 else if ((mov_field_order & 0xFF00) == 0x0200) {
1400 switch (mov_field_order & 0xFF) {
1401 case 0x01: decoded_field_order = AV_FIELD_TT;
1402 break;
1403 case 0x06: decoded_field_order = AV_FIELD_BB;
1404 break;
1405 case 0x09: decoded_field_order = AV_FIELD_TB;
1406 break;
1407 case 0x0E: decoded_field_order = AV_FIELD_BT;
1408 break;
1409 }
1410 }
1411 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1412 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1413 }
1414 st->codecpar->field_order = decoded_field_order;
1415
1416 return 0;
1417}
1418
1419static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
1420{
1421 int err = 0;
1422 uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1423 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1424 return AVERROR_INVALIDDATA;
1425 if ((err = av_reallocp(&par->extradata, size)) < 0) {
1426 par->extradata_size = 0;
1427 return err;
1428 }
1429 par->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE;
1430 return 0;
1431}
1432
1433/* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1434static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1435 AVCodecParameters *par, uint8_t *buf)
1436{
1437 int64_t result = atom.size;
1438 int err;
1439
1440 AV_WB32(buf , atom.size + 8);
1441 AV_WL32(buf + 4, atom.type);
1442 err = ffio_read_size(pb, buf + 8, atom.size);
1443 if (err < 0) {
1444 par->extradata_size -= atom.size;
1445 return err;
1446 } else if (err < atom.size) {
1447 av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1448 par->extradata_size -= atom.size - err;
1449 result = err;
1450 }
1451 memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1452 return result;
1453}
1454
1455/* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1456static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1457 enum AVCodecID codec_id)
1458{
1459 AVStream *st;
1460 uint64_t original_size;
1461 int err;
1462
1463 if (c->fc->nb_streams < 1) // will happen with jp2 files
1464 return 0;
1465 st = c->fc->streams[c->fc->nb_streams-1];
1466
1467 if (st->codecpar->codec_id != codec_id)
1468 return 0; /* unexpected codec_id - don't mess with extradata */
1469
1470 original_size = st->codecpar->extradata_size;
1471 err = mov_realloc_extradata(st->codecpar, atom);
1472 if (err)
1473 return err;
1474
1475 err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
1476 if (err < 0)
1477 return err;
1478 return 0; // Note: this is the original behavior to ignore truncation.
1479}
1480
1481/* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1482static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1483{
1484 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1485}
1486
1487static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1488{
1489 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1490}
1491
1492static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1493{
1494 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1495}
1496
1497static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1498{
1499 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1500}
1501
1502static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1503{
1504 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1505 if(ret == 0)
1506 ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1507 return ret;
1508}
1509
1510static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1511{
1512 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1513
1514 if (!ret && c->fc->nb_streams >= 1) {
1515 AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1516 if (par->extradata_size >= 40) {
1517 par->height = AV_RB16(&par->extradata[36]);
1518 par->width = AV_RB16(&par->extradata[38]);
1519 }
1520 }
1521 return ret;
1522}
1523
1524static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1525{
1526 if (c->fc->nb_streams >= 1) {
1527 AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1528 if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1529 par->codec_id == AV_CODEC_ID_H264 &&
1530 atom.size > 11) {
1531 int cid;
1532 avio_skip(pb, 10);
1533 cid = avio_rb16(pb);
1534 /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1535 if (cid == 0xd4d || cid == 0xd4e)
1536 par->width = 1440;
1537 return 0;
1538 } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
1539 par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
1540 atom.size >= 24) {
1541 int num, den;
1542 avio_skip(pb, 12);
1543 num = avio_rb32(pb);
1544 den = avio_rb32(pb);
1545 if (num <= 0 || den <= 0)
1546 return 0;
1547 switch (avio_rb32(pb)) {
1548 case 2:
1549 if (den >= INT_MAX / 2)
1550 return 0;
1551 den *= 2;
1552 case 1:
1553 c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.num = num;
1554 c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.den = den;
1555 default:
1556 return 0;
1557 }
1558 }
1559 }
1560
1561 return mov_read_avid(c, pb, atom);
1562}
1563
1564static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1565{
1566 int ret = 0;
1567 int length = 0;
1568 uint64_t original_size;
1569 if (c->fc->nb_streams >= 1) {
1570 AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1571 if (par->codec_id == AV_CODEC_ID_H264)
1572 return 0;
1573 if (atom.size == 16) {
1574 original_size = par->extradata_size;
1575 ret = mov_realloc_extradata(par, atom);
1576 if (!ret) {
1577 length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
1578 if (length == atom.size) {
1579 const uint8_t range_value = par->extradata[original_size + 19];
1580 switch (range_value) {
1581 case 1:
1582 par->color_range = AVCOL_RANGE_MPEG;
1583 break;
1584 case 2:
1585 par->color_range = AVCOL_RANGE_JPEG;
1586 break;
1587 default:
1588 av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1589 break;
1590 }
1591 ff_dlog(c, "color_range: %d\n", par->color_range);
1592 } else {
1593 /* For some reason the whole atom was not added to the extradata */
1594 av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1595 }
1596 } else {
1597 av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1598 }
1599 } else {
1600 av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1601 }
1602 }
1603
1604 return ret;
1605}
1606
1607static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1608{
1609 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1610}
1611
1612static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1613{
1614 AVStream *st;
1615 int ret;
1616
1617 if (c->fc->nb_streams < 1)
1618 return 0;
1619 st = c->fc->streams[c->fc->nb_streams-1];
1620
1621 if ((uint64_t)atom.size > (1<<30))
1622 return AVERROR_INVALIDDATA;
1623
1624 if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
1625 st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
1626 st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
1627 // pass all frma atom to codec, needed at least for QDMC and QDM2
1628 av_freep(&st->codecpar->extradata);
1629 ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1630 if (ret < 0)
1631 return ret;
1632 } else if (atom.size > 8) { /* to read frma, esds atoms */
1633 if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1634 uint64_t buffer;
1635 ret = ffio_ensure_seekback(pb, 8);
1636 if (ret < 0)
1637 return ret;
1638 buffer = avio_rb64(pb);
1639 atom.size -= 8;
1640 if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1641 && buffer >> 32 <= atom.size
1642 && buffer >> 32 >= 8) {
1643 avio_skip(pb, -8);
1644 atom.size += 8;
1645 } else if (!st->codecpar->extradata_size) {
1646#define ALAC_EXTRADATA_SIZE 36
1647 st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
1648 if (!st->codecpar->extradata)
1649 return AVERROR(ENOMEM);
1650 st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
1651 AV_WB32(st->codecpar->extradata , ALAC_EXTRADATA_SIZE);
1652 AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
1653 AV_WB64(st->codecpar->extradata + 12, buffer);
1654 avio_read(pb, st->codecpar->extradata + 20, 16);
1655 avio_skip(pb, atom.size - 24);
1656 return 0;
1657 }
1658 }
1659 if ((ret = mov_read_default(c, pb, atom)) < 0)
1660 return ret;
1661 } else
1662 avio_skip(pb, atom.size);
1663 return 0;
1664}
1665
1666/**
1667 * This function reads atom content and puts data in extradata without tag
1668 * nor size unlike mov_read_extradata.
1669 */
1670static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1671{
1672 AVStream *st;
1673 int ret;
1674
1675 if (c->fc->nb_streams < 1)
1676 return 0;
1677 st = c->fc->streams[c->fc->nb_streams-1];
1678
1679 if ((uint64_t)atom.size > (1<<30))
1680 return AVERROR_INVALIDDATA;
1681
1682 if (atom.size >= 10) {
1683 // Broken files created by legacy versions of libavformat will
1684 // wrap a whole fiel atom inside of a glbl atom.
1685 unsigned size = avio_rb32(pb);
1686 unsigned type = avio_rl32(pb);
1687 avio_seek(pb, -8, SEEK_CUR);
1688 if (type == MKTAG('f','i','e','l') && size == atom.size)
1689 return mov_read_default(c, pb, atom);
1690 }
1691 if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
1692 av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
1693 return 0;
1694 }
1695 av_freep(&st->codecpar->extradata);
1696 ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1697 if (ret < 0)
1698 return ret;
1699
1700 return 0;
1701}
1702
1703static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1704{
1705 AVStream *st;
1706 uint8_t profile_level;
1707 int ret;
1708
1709 if (c->fc->nb_streams < 1)
1710 return 0;
1711 st = c->fc->streams[c->fc->nb_streams-1];
1712
1713 if (atom.size >= (1<<28) || atom.size < 7)
1714 return AVERROR_INVALIDDATA;
1715
1716 profile_level = avio_r8(pb);
1717 if ((profile_level & 0xf0) != 0xc0)
1718 return 0;
1719
1720 avio_seek(pb, 6, SEEK_CUR);
1721 av_freep(&st->codecpar->extradata);
1722 ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
1723 if (ret < 0)
1724 return ret;
1725
1726 return 0;
1727}
1728
1729/**
1730 * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1731 * but can have extradata appended at the end after the 40 bytes belonging
1732 * to the struct.
1733 */
1734static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1735{
1736 AVStream *st;
1737 int ret;
1738
1739 if (c->fc->nb_streams < 1)
1740 return 0;
1741 if (atom.size <= 40)
1742 return 0;
1743 st = c->fc->streams[c->fc->nb_streams-1];
1744
1745 if ((uint64_t)atom.size > (1<<30))
1746 return AVERROR_INVALIDDATA;
1747
1748 avio_skip(pb, 40);
1749 av_freep(&st->codecpar->extradata);
1750 ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
1751 if (ret < 0)
1752 return ret;
1753
1754 return 0;
1755}
1756
1757static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1758{
1759 AVStream *st;
1760 MOVStreamContext *sc;
1761 unsigned int i, entries;
1762
1763 if (c->fc->nb_streams < 1)
1764 return 0;
1765 st = c->fc->streams[c->fc->nb_streams-1];
1766 sc = st->priv_data;
1767
1768 avio_r8(pb); /* version */
1769 avio_rb24(pb); /* flags */
1770
1771 entries = avio_rb32(pb);
1772
1773 if (!entries)
1774 return 0;
1775
1776 if (sc->chunk_offsets)
1777 av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
1778 av_free(sc->chunk_offsets);
1779 sc->chunk_count = 0;
1780 sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
1781 if (!sc->chunk_offsets)
1782 return AVERROR(ENOMEM);
1783 sc->chunk_count = entries;
1784
1785 if (atom.type == MKTAG('s','t','c','o'))
1786 for (i = 0; i < entries && !pb->eof_reached; i++)
1787 sc->chunk_offsets[i] = avio_rb32(pb);
1788 else if (atom.type == MKTAG('c','o','6','4'))
1789 for (i = 0; i < entries && !pb->eof_reached; i++)
1790 sc->chunk_offsets[i] = avio_rb64(pb);
1791 else
1792 return AVERROR_INVALIDDATA;
1793
1794 sc->chunk_count = i;
1795
1796 if (pb->eof_reached)
1797 return AVERROR_EOF;
1798
1799 return 0;
1800}
1801
1802/**
1803 * Compute codec id for 'lpcm' tag.
1804 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1805 */
1806enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1807{
1808 /* lpcm flags:
1809 * 0x1 = float
1810 * 0x2 = big-endian
1811 * 0x4 = signed
1812 */
1813 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1814}
1815
1816static int mov_codec_id(AVStream *st, uint32_t format)
1817{
1818 int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1819
1820 if (id <= 0 &&
1821 ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1822 (format & 0xFFFF) == 'T' + ('S' << 8)))
1823 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1824
1825 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1826 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1827 } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
1828 /* skip old ASF MPEG-4 tag */
1829 format && format != MKTAG('m','p','4','s')) {
1830 id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1831 if (id <= 0)
1832 id = ff_codec_get_id(ff_codec_bmp_tags, format);
1833 if (id > 0)
1834 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
1835 else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
1836 (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1837 st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
1838 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1839 if (id > 0)
1840 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
1841 }
1842 }
1843
1844 st->codecpar->codec_tag = format;
1845
1846 return id;
1847}
1848
1849static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1850 AVStream *st, MOVStreamContext *sc)
1851{
1852 uint8_t codec_name[32] = { 0 };
1853 int64_t stsd_start;
1854 unsigned int len;
1855
1856 /* The first 16 bytes of the video sample description are already
1857 * read in ff_mov_read_stsd_entries() */
1858 stsd_start = avio_tell(pb) - 16;
1859
1860 avio_rb16(pb); /* version */
1861 avio_rb16(pb); /* revision level */
1862 avio_rb32(pb); /* vendor */
1863 avio_rb32(pb); /* temporal quality */
1864 avio_rb32(pb); /* spatial quality */
1865
1866 st->codecpar->width = avio_rb16(pb); /* width */
1867 st->codecpar->height = avio_rb16(pb); /* height */
1868
1869 avio_rb32(pb); /* horiz resolution */
1870 avio_rb32(pb); /* vert resolution */
1871 avio_rb32(pb); /* data size, always 0 */
1872 avio_rb16(pb); /* frames per samples */
1873
1874 len = avio_r8(pb); /* codec name, pascal string */
1875 if (len > 31)
1876 len = 31;
1877 mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1878 if (len < 31)
1879 avio_skip(pb, 31 - len);
1880
1881 if (codec_name[0])
1882 av_dict_set(&st->metadata, "encoder", codec_name, 0);
1883
1884 /* codec_tag YV12 triggers an UV swap in rawdec.c */
1885 if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1886 st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
1887 st->codecpar->width &= ~1;
1888 st->codecpar->height &= ~1;
1889 }
1890 /* Flash Media Server uses tag H.263 with Sorenson Spark */
1891 if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
1892 !memcmp(codec_name, "Sorenson H263", 13))
1893 st->codecpar->codec_id = AV_CODEC_ID_FLV1;
1894
1895 st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
1896
1897 avio_seek(pb, stsd_start, SEEK_SET);
1898
1899 if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
1900 st->codecpar->bits_per_coded_sample &= 0x1F;
1901 sc->has_palette = 1;
1902 }
1903}
1904
1905static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1906 AVStream *st, MOVStreamContext *sc)
1907{
1908 int bits_per_sample, flags;
1909 uint16_t version = avio_rb16(pb);
1910 AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1911
1912 avio_rb16(pb); /* revision level */
1913 avio_rb32(pb); /* vendor */
1914
1915 st->codecpar->channels = avio_rb16(pb); /* channel count */
1916 st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1917 av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
1918
1919 sc->audio_cid = avio_rb16(pb);
1920 avio_rb16(pb); /* packet size = 0 */
1921
1922 st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
1923
1924 // Read QT version 1 fields. In version 0 these do not exist.
1925 av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
1926 if (!c->isom ||
1927 (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1928
1929 if (version == 1) {
1930 sc->samples_per_frame = avio_rb32(pb);
1931 avio_rb32(pb); /* bytes per packet */
1932 sc->bytes_per_frame = avio_rb32(pb);
1933 avio_rb32(pb); /* bytes per sample */
1934 } else if (version == 2) {
1935 avio_rb32(pb); /* sizeof struct only */
1936 st->codecpar->sample_rate = av_int2double(avio_rb64(pb));
1937 st->codecpar->channels = avio_rb32(pb);
1938 avio_rb32(pb); /* always 0x7F000000 */
1939 st->codecpar->bits_per_coded_sample = avio_rb32(pb);
1940
1941 flags = avio_rb32(pb); /* lpcm format specific flag */
1942 sc->bytes_per_frame = avio_rb32(pb);
1943 sc->samples_per_frame = avio_rb32(pb);
1944 if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
1945 st->codecpar->codec_id =
1946 ff_mov_get_lpcm_codec_id(st->codecpar->bits_per_coded_sample,
1947 flags);
1948 }
1949 if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1950 /* can't correctly handle variable sized packet as audio unit */
1951 switch (st->codecpar->codec_id) {
1952 case AV_CODEC_ID_MP2:
1953 case AV_CODEC_ID_MP3:
1954 st->need_parsing = AVSTREAM_PARSE_FULL;
1955 break;
1956 }
1957 }
1958 }
1959
1960 if (sc->format == 0) {
1961 if (st->codecpar->bits_per_coded_sample == 8)
1962 st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
1963 else if (st->codecpar->bits_per_coded_sample == 16)
1964 st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
1965 }
1966
1967 switch (st->codecpar->codec_id) {
1968 case AV_CODEC_ID_PCM_S8:
1969 case AV_CODEC_ID_PCM_U8:
1970 if (st->codecpar->bits_per_coded_sample == 16)
1971 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
1972 break;
1973 case AV_CODEC_ID_PCM_S16LE:
1974 case AV_CODEC_ID_PCM_S16BE:
1975 if (st->codecpar->bits_per_coded_sample == 8)
1976 st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
1977 else if (st->codecpar->bits_per_coded_sample == 24)
1978 st->codecpar->codec_id =
1979 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
1980 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1981 else if (st->codecpar->bits_per_coded_sample == 32)
1982 st->codecpar->codec_id =
1983 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
1984 AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
1985 break;
1986 /* set values for old format before stsd version 1 appeared */
1987 case AV_CODEC_ID_MACE3:
1988 sc->samples_per_frame = 6;
1989 sc->bytes_per_frame = 2 * st->codecpar->channels;
1990 break;
1991 case AV_CODEC_ID_MACE6:
1992 sc->samples_per_frame = 6;
1993 sc->bytes_per_frame = 1 * st->codecpar->channels;
1994 break;
1995 case AV_CODEC_ID_ADPCM_IMA_QT:
1996 sc->samples_per_frame = 64;
1997 sc->bytes_per_frame = 34 * st->codecpar->channels;
1998 break;
1999 case AV_CODEC_ID_GSM:
2000 sc->samples_per_frame = 160;
2001 sc->bytes_per_frame = 33;
2002 break;
2003 default:
2004 break;
2005 }
2006
2007 bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2008 if (bits_per_sample) {
2009 st->codecpar->bits_per_coded_sample = bits_per_sample;
2010 sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
2011 }
2012}
2013
2014static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
2015 AVStream *st, MOVStreamContext *sc,
2016 int64_t size)
2017{
2018 // ttxt stsd contains display flags, justification, background
2019 // color, fonts, and default styles, so fake an atom to read it
2020 MOVAtom fake_atom = { .size = size };
2021 // mp4s contains a regular esds atom
2022 if (st->codecpar->codec_tag != AV_RL32("mp4s"))
2023 mov_read_glbl(c, pb, fake_atom);
2024 st->codecpar->width = sc->width;
2025 st->codecpar->height = sc->height;
2026}
2027
2028static uint32_t yuv_to_rgba(uint32_t ycbcr)
2029{
2030 uint8_t r, g, b;
2031 int y, cb, cr;
2032
2033 y = (ycbcr >> 16) & 0xFF;
2034 cr = (ycbcr >> 8) & 0xFF;
2035 cb = ycbcr & 0xFF;
2036
2037 b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
2038 g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
2039 r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
2040
2041 return (r << 16) | (g << 8) | b;
2042}
2043
2044static int mov_rewrite_dvd_sub_extradata(AVStream *st)
2045{
2046 char buf[256] = {0};
2047 uint8_t *src = st->codecpar->extradata;
2048 int i;
2049
2050 if (st->codecpar->extradata_size != 64)
2051 return 0;
2052
2053 if (st->codecpar->width > 0 && st->codecpar->height > 0)
2054 snprintf(buf, sizeof(buf), "size: %dx%d\n",
2055 st->codecpar->width, st->codecpar->height);
2056 av_strlcat(buf, "palette: ", sizeof(buf));
2057
2058 for (i = 0; i < 16; i++) {
2059 uint32_t yuv = AV_RB32(src + i * 4);
2060 uint32_t rgba = yuv_to_rgba(yuv);
2061
2062 av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
2063 }
2064
2065 if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
2066 return 0;
2067
2068 av_freep(&st->codecpar->extradata);
2069 st->codecpar->extradata_size = 0;
2070 st->codecpar->extradata = av_mallocz(strlen(buf) + AV_INPUT_BUFFER_PADDING_SIZE);
2071 if (!st->codecpar->extradata)
2072 return AVERROR(ENOMEM);
2073 st->codecpar->extradata_size = strlen(buf);
2074 memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
2075
2076 return 0;
2077}
2078
2079static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
2080 AVStream *st, MOVStreamContext *sc,
2081 int64_t size)
2082{
2083 int ret;
2084
2085 if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2086 if ((int)size != size)
2087 return AVERROR(ENOMEM);
2088
2089 ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2090 if (ret < 0)
2091 return ret;
2092 if (size > 16) {
2093 MOVStreamContext *tmcd_ctx = st->priv_data;
2094 int val;
2095 val = AV_RB32(st->codecpar->extradata + 4);
2096 tmcd_ctx->tmcd_flags = val;
2097 st->avg_frame_rate.num = st->codecpar->extradata[16]; /* number of frame */
2098 st->avg_frame_rate.den = 1;
2099#if FF_API_LAVF_AVCTX
2100FF_DISABLE_DEPRECATION_WARNINGS
2101 st->codec->time_base = av_inv_q(st->avg_frame_rate);
2102FF_ENABLE_DEPRECATION_WARNINGS
2103#endif
2104 /* adjust for per frame dur in counter mode */
2105 if (tmcd_ctx->tmcd_flags & 0x0008) {
2106 int timescale = AV_RB32(st->codecpar->extradata + 8);
2107 int framedur = AV_RB32(st->codecpar->extradata + 12);
2108 st->avg_frame_rate.num *= timescale;
2109 st->avg_frame_rate.den *= framedur;
2110#if FF_API_LAVF_AVCTX
2111FF_DISABLE_DEPRECATION_WARNINGS
2112 st->codec->time_base.den *= timescale;
2113 st->codec->time_base.num *= framedur;
2114FF_ENABLE_DEPRECATION_WARNINGS
2115#endif
2116 }
2117 if (size > 30) {
2118 uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2119 uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2120 if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2121 uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2122 if (str_size > 0 && size >= (int)str_size + 26) {
2123 char *reel_name = av_malloc(str_size + 1);
2124 if (!reel_name)
2125 return AVERROR(ENOMEM);
2126 memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2127 reel_name[str_size] = 0; /* Add null terminator */
2128 /* don't add reel_name if emtpy string */
2129 if (*reel_name == 0) {
2130 av_free(reel_name);
2131 } else {
2132 av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL);
2133 }
2134 }
2135 }
2136 }
2137 }
2138 } else {
2139 /* other codec type, just skip (rtp, mp4s ...) */
2140 avio_skip(pb, size);
2141 }
2142 return 0;
2143}
2144
2145static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
2146 AVStream *st, MOVStreamContext *sc)
2147{
2148 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2149 !st->codecpar->sample_rate && sc->time_scale > 1)
2150 st->codecpar->sample_rate = sc->time_scale;
2151
2152 /* special codec parameters handling */
2153 switch (st->codecpar->codec_id) {
2154#if CONFIG_DV_DEMUXER
2155 case AV_CODEC_ID_DVAUDIO:
2156 c->dv_fctx = avformat_alloc_context();
2157 if (!c->dv_fctx) {
2158 av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2159 return AVERROR(ENOMEM);
2160 }
2161 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2162 if (!c->dv_demux) {
2163 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2164 return AVERROR(ENOMEM);
2165 }
2166 sc->dv_audio_container = 1;
2167 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
2168 break;
2169#endif
2170 /* no ifdef since parameters are always those */
2171 case AV_CODEC_ID_QCELP:
2172 st->codecpar->channels = 1;
2173 // force sample rate for qcelp when not stored in mov
2174 if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2175 st->codecpar->sample_rate = 8000;
2176 // FIXME: Why is the following needed for some files?
2177 sc->samples_per_frame = 160;
2178 if (!sc->bytes_per_frame)
2179 sc->bytes_per_frame = 35;
2180 break;
2181 case AV_CODEC_ID_AMR_NB:
2182 st->codecpar->channels = 1;
2183 /* force sample rate for amr, stsd in 3gp does not store sample rate */
2184 st->codecpar->sample_rate = 8000;
2185 break;
2186 case AV_CODEC_ID_AMR_WB:
2187 st->codecpar->channels = 1;
2188 st->codecpar->sample_rate = 16000;
2189 break;
2190 case AV_CODEC_ID_MP2:
2191 case AV_CODEC_ID_MP3:
2192 /* force type after stsd for m1a hdlr */
2193 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
2194 st->need_parsing = AVSTREAM_PARSE_FULL;
2195 break;
2196 case AV_CODEC_ID_GSM:
2197 case AV_CODEC_ID_ADPCM_MS:
2198 case AV_CODEC_ID_ADPCM_IMA_WAV:
2199 case AV_CODEC_ID_ILBC:
2200 case AV_CODEC_ID_MACE3:
2201 case AV_CODEC_ID_MACE6:
2202 case AV_CODEC_ID_QDM2:
2203 st->codecpar->block_align = sc->bytes_per_frame;
2204 break;
2205 case AV_CODEC_ID_ALAC:
2206 if (st->codecpar->extradata_size == 36) {
2207 st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
2208 st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2209 }
2210 break;
2211 case AV_CODEC_ID_AC3:
2212 case AV_CODEC_ID_EAC3:
2213 case AV_CODEC_ID_MPEG1VIDEO:
2214 case AV_CODEC_ID_VC1:
2215 case AV_CODEC_ID_VP9:
2216 st->need_parsing = AVSTREAM_PARSE_FULL;
2217 break;
2218 default:
2219 break;
2220 }
2221 return 0;
2222}
2223
2224static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
2225 int codec_tag, int format,
2226 int64_t size)
2227{
2228 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2229
2230 if (codec_tag &&
2231 (codec_tag != format &&
2232 // AVID 1:1 samples with differing data format and codec tag exist
2233 (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
2234 // prores is allowed to have differing data format and codec tag
2235 codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
2236 // so is dv (sigh)
2237 codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
2238 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2239 : codec_tag != MKTAG('j','p','e','g')))) {
2240 /* Multiple fourcc, we skip JPEG. This is not correct, we should
2241 * export it as a separate AVStream but this needs a few changes
2242 * in the MOV demuxer, patch welcome. */
2243
2244 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2245 avio_skip(pb, size);
2246 return 1;
2247 }
2248
2249 return 0;
2250}
2251
2252int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
2253{
2254 AVStream *st;
2255 MOVStreamContext *sc;
2256 int pseudo_stream_id;
2257
2258 if (c->fc->nb_streams < 1)
2259 return 0;
2260 st = c->fc->streams[c->fc->nb_streams-1];
2261 sc = st->priv_data;
2262
2263 for (pseudo_stream_id = 0;
2264 pseudo_stream_id < entries && !pb->eof_reached;
2265 pseudo_stream_id++) {
2266 //Parsing Sample description table
2267 enum AVCodecID id;
2268 int ret, dref_id = 1;
2269 MOVAtom a = { AV_RL32("stsd") };
2270 int64_t start_pos = avio_tell(pb);
2271 int64_t size = avio_rb32(pb); /* size */
2272 uint32_t format = avio_rl32(pb); /* data format */
2273
2274 if (size >= 16) {
2275 avio_rb32(pb); /* reserved */
2276 avio_rb16(pb); /* reserved */
2277 dref_id = avio_rb16(pb);
2278 } else if (size <= 7) {
2279 av_log(c->fc, AV_LOG_ERROR,
2280 "invalid size %"PRId64" in stsd\n", size);
2281 return AVERROR_INVALIDDATA;
2282 }
2283
2284 if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
2285 size - (avio_tell(pb) - start_pos)))
2286 continue;
2287
2288 sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2289 sc->dref_id= dref_id;
2290 sc->format = format;
2291
2292 id = mov_codec_id(st, format);
2293
2294 av_log(c->fc, AV_LOG_TRACE,
2295 "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
2296 av_fourcc2str(format), st->codecpar->codec_type);
2297
2298 if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
2299 st->codecpar->codec_id = id;
2300 mov_parse_stsd_video(c, pb, st, sc);
2301 } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
2302 st->codecpar->codec_id = id;
2303 mov_parse_stsd_audio(c, pb, st, sc);
2304 if (st->codecpar->sample_rate < 0) {
2305 av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
2306 return AVERROR_INVALIDDATA;
2307 }
2308 } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
2309 st->codecpar->codec_id = id;
2310 mov_parse_stsd_subtitle(c, pb, st, sc,
2311 size - (avio_tell(pb) - start_pos));
2312 } else {
2313 ret = mov_parse_stsd_data(c, pb, st, sc,
2314 size - (avio_tell(pb) - start_pos));
2315 if (ret < 0)
2316 return ret;
2317 }
2318 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2319 a.size = size - (avio_tell(pb) - start_pos);
2320 if (a.size > 8) {
2321 if ((ret = mov_read_default(c, pb, a)) < 0)
2322 return ret;
2323 } else if (a.size > 0)
2324 avio_skip(pb, a.size);
2325
2326 if (sc->extradata) {
2327 int extra_size = st->codecpar->extradata_size;
2328
2329 /* Move the current stream extradata to the stream context one. */
2330 sc->extradata_size[pseudo_stream_id] = extra_size;
2331 sc->extradata[pseudo_stream_id] = av_malloc(extra_size + AV_INPUT_BUFFER_PADDING_SIZE);
2332 if (!sc->extradata[pseudo_stream_id])
2333 return AVERROR(ENOMEM);
2334 memcpy(sc->extradata[pseudo_stream_id], st->codecpar->extradata, extra_size);
2335 av_freep(&st->codecpar->extradata);
2336 st->codecpar->extradata_size = 0;
2337 }
2338 }
2339
2340 if (pb->eof_reached)
2341 return AVERROR_EOF;
2342
2343 return 0;
2344}
2345
2346static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2347{
2348 AVStream *st;
2349 MOVStreamContext *sc;
2350 int ret, entries;
2351
2352 if (c->fc->nb_streams < 1)
2353 return 0;
2354 st = c->fc->streams[c->fc->nb_streams - 1];
2355 sc = st->priv_data;
2356
2357 avio_r8(pb); /* version */
2358 avio_rb24(pb); /* flags */
2359 entries = avio_rb32(pb);
2360
2361 if (entries <= 0) {
2362 av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
2363 return AVERROR_INVALIDDATA;
2364 }
2365
2366 if (sc->extradata) {
2367 av_log(c->fc, AV_LOG_ERROR, "Duplicate STSD\n");
2368 return AVERROR_INVALIDDATA;
2369 }
2370 /* Prepare space for hosting multiple extradata. */
2371 sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
2372 sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
2373 if (!sc->extradata_size || !sc->extradata) {
2374 ret = AVERROR(ENOMEM);
2375 goto fail;
2376 }
2377
2378 ret = ff_mov_read_stsd_entries(c, pb, entries);
2379 if (ret < 0)
2380 return ret;
2381
2382 sc->stsd_count = entries;
2383
2384 /* Restore back the primary extradata. */
2385 av_freep(&st->codecpar->extradata);
2386 st->codecpar->extradata_size = sc->extradata_size[0];
2387 if (sc->extradata_size[0]) {
2388 st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
2389 if (!st->codecpar->extradata)
2390 return AVERROR(ENOMEM);
2391 memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
2392 }
2393
2394 return mov_finalize_stsd_codec(c, pb, st, sc);
2395fail:
2396 av_freep(&sc->extradata);
2397 av_freep(&sc->extradata_size);
2398 return ret;
2399}
2400
2401static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2402{
2403 AVStream *st;
2404 MOVStreamContext *sc;
2405 unsigned int i, entries;
2406
2407 if (c->fc->nb_streams < 1)
2408 return 0;
2409 st = c->fc->streams[c->fc->nb_streams-1];
2410 sc = st->priv_data;
2411
2412 avio_r8(pb); /* version */
2413 avio_rb24(pb); /* flags */
2414
2415 entries = avio_rb32(pb);
2416
2417 av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
2418
2419 if (!entries)
2420 return 0;
2421 if (sc->stsc_data)
2422 av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
2423 av_free(sc->stsc_data);
2424 sc->stsc_count = 0;
2425 sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2426 if (!sc->stsc_data)
2427 return AVERROR(ENOMEM);
2428
2429 for (i = 0; i < entries && !pb->eof_reached; i++) {
2430 sc->stsc_data[i].first = avio_rb32(pb);
2431 sc->stsc_data[i].count = avio_rb32(pb);
2432 sc->stsc_data[i].id = avio_rb32(pb);
2433 }
2434
2435 sc->stsc_count = i;
2436
2437 if (pb->eof_reached)
2438 return AVERROR_EOF;
2439
2440 return 0;
2441}
2442
2443#define mov_stsc_index_valid(index, count) ((index) < (count) - 1)
2444
2445/* Compute the samples value for the stsc entry at the given index. */
2446static inline int mov_get_stsc_samples(MOVStreamContext *sc, int index)
2447{
2448 int chunk_count;
2449
2450 if (mov_stsc_index_valid(index, sc->stsc_count))
2451 chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2452 else
2453 chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2454
2455 return sc->stsc_data[index].count * chunk_count;
2456}
2457
2458static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2459{
2460 AVStream *st;
2461 MOVStreamContext *sc;
2462 unsigned i, entries;
2463
2464 if (c->fc->nb_streams < 1)
2465 return 0;
2466 st = c->fc->streams[c->fc->nb_streams-1];
2467 sc = st->priv_data;
2468
2469 avio_rb32(pb); // version + flags
2470
2471 entries = avio_rb32(pb);
2472 if (sc->stps_data)
2473 av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2474 av_free(sc->stps_data);
2475 sc->stps_count = 0;
2476 sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2477 if (!sc->stps_data)
2478 return AVERROR(ENOMEM);
2479
2480 for (i = 0; i < entries && !pb->eof_reached; i++) {
2481 sc->stps_data[i] = avio_rb32(pb);
2482 }
2483
2484 sc->stps_count = i;
2485
2486 if (pb->eof_reached)
2487 return AVERROR_EOF;
2488
2489 return 0;
2490}
2491
2492static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2493{
2494 AVStream *st;
2495 MOVStreamContext *sc;
2496 unsigned int i, entries;
2497
2498 if (c->fc->nb_streams < 1)
2499 return 0;
2500 st = c->fc->streams[c->fc->nb_streams-1];
2501 sc = st->priv_data;
2502
2503 avio_r8(pb); /* version */
2504 avio_rb24(pb); /* flags */
2505
2506 entries = avio_rb32(pb);
2507
2508 av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
2509
2510 if (!entries)
2511 {
2512 sc->keyframe_absent = 1;
2513 if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2514 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2515 return 0;
2516 }
2517 if (sc->keyframes)
2518 av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2519 if (entries >= UINT_MAX / sizeof(int))
2520 return AVERROR_INVALIDDATA;
2521 av_freep(&sc->keyframes);
2522 sc->keyframe_count = 0;
2523 sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2524 if (!sc->keyframes)
2525 return AVERROR(ENOMEM);
2526
2527 for (i = 0; i < entries && !pb->eof_reached; i++) {
2528 sc->keyframes[i] = avio_rb32(pb);
2529 }
2530
2531 sc->keyframe_count = i;
2532
2533 if (pb->eof_reached)
2534 return AVERROR_EOF;
2535
2536 return 0;
2537}
2538
2539static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2540{
2541 AVStream *st;
2542 MOVStreamContext *sc;
2543 unsigned int i, entries, sample_size, field_size, num_bytes;
2544 GetBitContext gb;
2545 unsigned char* buf;
2546 int ret;
2547
2548 if (c->fc->nb_streams < 1)
2549 return 0;
2550 st = c->fc->streams[c->fc->nb_streams-1];
2551 sc = st->priv_data;
2552
2553 avio_r8(pb); /* version */
2554 avio_rb24(pb); /* flags */
2555
2556 if (atom.type == MKTAG('s','t','s','z')) {
2557 sample_size = avio_rb32(pb);
2558 if (!sc->sample_size) /* do not overwrite value computed in stsd */
2559 sc->sample_size = sample_size;
2560 sc->stsz_sample_size = sample_size;
2561 field_size = 32;
2562 } else {
2563 sample_size = 0;
2564 avio_rb24(pb); /* reserved */
2565 field_size = avio_r8(pb);
2566 }
2567 entries = avio_rb32(pb);
2568
2569 av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
2570
2571 sc->sample_count = entries;
2572 if (sample_size)
2573 return 0;
2574
2575 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2576 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
2577 return AVERROR_INVALIDDATA;
2578 }
2579
2580 if (!entries)
2581 return 0;
2582 if (entries >= (UINT_MAX - 4) / field_size)
2583 return AVERROR_INVALIDDATA;
2584 if (sc->sample_sizes)
2585 av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2586 av_free(sc->sample_sizes);
2587 sc->sample_count = 0;
2588 sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2589 if (!sc->sample_sizes)
2590 return AVERROR(ENOMEM);
2591
2592 num_bytes = (entries*field_size+4)>>3;
2593
2594 buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2595 if (!buf) {
2596 av_freep(&sc->sample_sizes);
2597 return AVERROR(ENOMEM);
2598 }
2599
2600 ret = ffio_read_size(pb, buf, num_bytes);
2601 if (ret < 0) {
2602 av_freep(&sc->sample_sizes);
2603 av_free(buf);
2604 return ret;
2605 }
2606
2607 init_get_bits(&gb, buf, 8*num_bytes);
2608
2609 for (i = 0; i < entries && !pb->eof_reached; i++) {
2610 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2611 sc->data_size += sc->sample_sizes[i];
2612 }
2613
2614 sc->sample_count = i;
2615
2616 av_free(buf);
2617
2618 if (pb->eof_reached)
2619 return AVERROR_EOF;
2620
2621 return 0;
2622}
2623
2624static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2625{
2626 AVStream *st;
2627 MOVStreamContext *sc;
2628 unsigned int i, entries;
2629 int64_t duration=0;
2630 int64_t total_sample_count=0;
2631
2632 if (c->fc->nb_streams < 1)
2633 return 0;
2634 st = c->fc->streams[c->fc->nb_streams-1];
2635 sc = st->priv_data;
2636
2637 avio_r8(pb); /* version */
2638 avio_rb24(pb); /* flags */
2639 entries = avio_rb32(pb);
2640
2641 av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
2642 c->fc->nb_streams-1, entries);
2643
2644 if (sc->stts_data)
2645 av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2646 av_free(sc->stts_data);
2647 sc->stts_count = 0;
2648 sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data));
2649 if (!sc->stts_data)
2650 return AVERROR(ENOMEM);
2651
2652 for (i = 0; i < entries && !pb->eof_reached; i++) {
2653 int sample_duration;
2654 int sample_count;
2655
2656 sample_count=avio_rb32(pb);
2657 sample_duration = avio_rb32(pb);
2658
2659 if (sample_count < 0) {
2660 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2661 return AVERROR_INVALIDDATA;
2662 }
2663 sc->stts_data[i].count= sample_count;
2664 sc->stts_data[i].duration= sample_duration;
2665
2666 av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2667 sample_count, sample_duration);
2668
2669 if ( i+1 == entries
2670 && i
2671 && sample_count == 1
2672 && total_sample_count > 100
2673 && sample_duration/10 > duration / total_sample_count)
2674 sample_duration = duration / total_sample_count;
2675 duration+=(int64_t)sample_duration*sample_count;
2676 total_sample_count+=sample_count;
2677 }
2678
2679 sc->stts_count = i;
2680
2681 sc->duration_for_fps += duration;
2682 sc->nb_frames_for_fps += total_sample_count;
2683
2684 if (pb->eof_reached)
2685 return AVERROR_EOF;
2686
2687 st->nb_frames= total_sample_count;
2688 if (duration)
2689 st->duration= duration;
2690 sc->track_end = duration;
2691 return 0;
2692}
2693
2694static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2695{
2696 if (duration < 0) {
2697 if (duration == INT_MIN) {
2698 av_log(NULL, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
2699 duration++;
2700 }
2701 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2702 }
2703}
2704
2705static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2706{
2707 AVStream *st;
2708 MOVStreamContext *sc;
2709 unsigned int i, entries, ctts_count = 0;
2710
2711 if (c->fc->nb_streams < 1)
2712 return 0;
2713 st = c->fc->streams[c->fc->nb_streams-1];
2714 sc = st->priv_data;
2715
2716 avio_r8(pb); /* version */
2717 avio_rb24(pb); /* flags */
2718 entries = avio_rb32(pb);
2719
2720 av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
2721
2722 if (!entries)
2723 return 0;
2724 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2725 return AVERROR_INVALIDDATA;
2726 av_freep(&sc->ctts_data);
2727 sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
2728 if (!sc->ctts_data)
2729 return AVERROR(ENOMEM);
2730
2731 for (i = 0; i < entries && !pb->eof_reached; i++) {
2732 int count =avio_rb32(pb);
2733 int duration =avio_rb32(pb);
2734
2735 if (count <= 0) {
2736 av_log(c->fc, AV_LOG_TRACE,
2737 "ignoring CTTS entry with count=%d duration=%d\n",
2738 count, duration);
2739 continue;
2740 }
2741
2742 sc->ctts_data[ctts_count].count = count;
2743 sc->ctts_data[ctts_count].duration = duration;
2744 ctts_count++;
2745
2746 av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
2747 count, duration);
2748
2749 if (FFNABS(duration) < -(1<<28) && i+2<entries) {
2750 av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2751 av_freep(&sc->ctts_data);
2752 sc->ctts_count = 0;
2753 return 0;
2754 }
2755
2756 if (i+2<entries)
2757 mov_update_dts_shift(sc, duration);
2758 }
2759
2760 sc->ctts_count = ctts_count;
2761
2762 if (pb->eof_reached)
2763 return AVERROR_EOF;
2764
2765 av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
2766
2767 return 0;
2768}
2769
2770static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2771{
2772 AVStream *st;
2773 MOVStreamContext *sc;
2774 unsigned int i, entries;
2775 uint8_t version;
2776 uint32_t grouping_type;
2777
2778 if (c->fc->nb_streams < 1)
2779 return 0;
2780 st = c->fc->streams[c->fc->nb_streams-1];
2781 sc = st->priv_data;
2782
2783 version = avio_r8(pb); /* version */
2784 avio_rb24(pb); /* flags */
2785 grouping_type = avio_rl32(pb);
2786 if (grouping_type != MKTAG( 'r','a','p',' '))
2787 return 0; /* only support 'rap ' grouping */
2788 if (version == 1)
2789 avio_rb32(pb); /* grouping_type_parameter */
2790
2791 entries = avio_rb32(pb);
2792 if (!entries)
2793 return 0;
2794 if (sc->rap_group)
2795 av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
2796 av_free(sc->rap_group);
2797 sc->rap_group_count = 0;
2798 sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
2799 if (!sc->rap_group)
2800 return AVERROR(ENOMEM);
2801
2802 for (i = 0; i < entries && !pb->eof_reached; i++) {
2803 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2804 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2805 }
2806
2807 sc->rap_group_count = i;
2808
2809 return pb->eof_reached ? AVERROR_EOF : 0;
2810}
2811
2812/**
2813 * Get ith edit list entry (media time, duration).
2814 */
2815static int get_edit_list_entry(MOVContext *mov,
2816 const MOVStreamContext *msc,
2817 unsigned int edit_list_index,
2818 int64_t *edit_list_media_time,
2819 int64_t *edit_list_duration,
2820 int64_t global_timescale)
2821{
2822 if (edit_list_index == msc->elst_count) {
2823 return 0;
2824 }
2825 *edit_list_media_time = msc->elst_data[edit_list_index].time;
2826 *edit_list_duration = msc->elst_data[edit_list_index].duration;
2827
2828 /* duration is in global timescale units;convert to msc timescale */
2829 if (global_timescale == 0) {
2830 avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
2831 return 0;
2832 }
2833 *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
2834 global_timescale);
2835 return 1;
2836}
2837
2838/**
2839 * Find the closest previous frame to the timestamp, in e_old index
2840 * entries. Searching for just any frame / just key frames can be controlled by
2841 * last argument 'flag'.
2842 * Returns the index of the entry in st->index_entries if successful,
2843 * else returns -1.
2844 */
2845static int64_t find_prev_closest_index(AVStream *st,
2846 AVIndexEntry *e_old,
2847 int nb_old,
2848 int64_t timestamp,
2849 int flag)
2850{
2851 AVIndexEntry *e_keep = st->index_entries;
2852 int nb_keep = st->nb_index_entries;
2853 int64_t found = -1;
2854 int64_t i = 0;
2855
2856 st->index_entries = e_old;
2857 st->nb_index_entries = nb_old;
2858 found = av_index_search_timestamp(st, timestamp, flag | AVSEEK_FLAG_BACKWARD);
2859
2860 // Keep going backwards in the index entries until the timestamp is the same.
2861 if (found >= 0) {
2862 for (i = found; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
2863 i--) {
2864 if ((flag & AVSEEK_FLAG_ANY) ||
2865 (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
2866 found = i - 1;
2867 }
2868 }
2869 }
2870
2871 /* restore AVStream state*/
2872 st->index_entries = e_keep;
2873 st->nb_index_entries = nb_keep;
2874 return found;
2875}
2876
2877/**
2878 * Add index entry with the given values, to the end of st->index_entries.
2879 * Returns the new size st->index_entries if successful, else returns -1.
2880 *
2881 * This function is similar to ff_add_index_entry in libavformat/utils.c
2882 * except that here we are always unconditionally adding an index entry to
2883 * the end, instead of searching the entries list and skipping the add if
2884 * there is an existing entry with the same timestamp.
2885 * This is needed because the mov_fix_index calls this func with the same
2886 * unincremented timestamp for successive discarded frames.
2887 */
2888static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
2889 int size, int distance, int flags)
2890{
2891 AVIndexEntry *entries, *ie;
2892 int64_t index = -1;
2893 const size_t min_size_needed = (st->nb_index_entries + 1) * sizeof(AVIndexEntry);
2894
2895 // Double the allocation each time, to lower memory fragmentation.
2896 // Another difference from ff_add_index_entry function.
2897 const size_t requested_size =
2898 min_size_needed > st->index_entries_allocated_size ?
2899 FFMAX(min_size_needed, 2 * st->index_entries_allocated_size) :
2900 min_size_needed;
2901
2902 if((unsigned)st->nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
2903 return -1;
2904
2905 entries = av_fast_realloc(st->index_entries,
2906 &st->index_entries_allocated_size,
2907 requested_size);
2908 if(!entries)
2909 return -1;
2910
2911 st->index_entries= entries;
2912
2913 index= st->nb_index_entries++;
2914 ie= &entries[index];
2915
2916 ie->pos = pos;
2917 ie->timestamp = timestamp;
2918 ie->min_distance= distance;
2919 ie->size= size;
2920 ie->flags = flags;
2921 return index;
2922}
2923
2924/**
2925 * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
2926 * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
2927 */
2928static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
2929 int64_t* frame_duration_buffer,
2930 int frame_duration_buffer_size) {
2931 int i = 0;
2932 av_assert0(end_index >= 0 && end_index <= st->nb_index_entries);
2933 for (i = 0; i < frame_duration_buffer_size; i++) {
2934 end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
2935 st->index_entries[end_index - 1 - i].timestamp = end_ts;
2936 }
2937}
2938
2939/**
2940 * Append a new ctts entry to ctts_data.
2941 * Returns the new ctts_count if successful, else returns -1.
2942 */
2943static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
2944 int count, int duration)
2945{
2946 MOVStts *ctts_buf_new;
2947 const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
2948 const size_t requested_size =
2949 min_size_needed > *allocated_size ?
2950 FFMAX(min_size_needed, 2 * (*allocated_size)) :
2951 min_size_needed;
2952
2953 if((unsigned)(*ctts_count) + 1 >= UINT_MAX / sizeof(MOVStts))
2954 return -1;
2955
2956 ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
2957
2958 if(!ctts_buf_new)
2959 return -1;
2960
2961 *ctts_data = ctts_buf_new;
2962
2963 ctts_buf_new[*ctts_count].count = count;
2964 ctts_buf_new[*ctts_count].duration = duration;
2965
2966 *ctts_count = (*ctts_count) + 1;
2967 return *ctts_count;
2968}
2969
2970static void mov_current_sample_inc(MOVStreamContext *sc)
2971{
2972 sc->current_sample++;
2973 sc->current_index++;
2974 if (sc->index_ranges &&
2975 sc->current_index >= sc->current_index_range->end &&
2976 sc->current_index_range->end) {
2977 sc->current_index_range++;
2978 sc->current_index = sc->current_index_range->start;
2979 }
2980}
2981
2982static void mov_current_sample_dec(MOVStreamContext *sc)
2983{
2984 sc->current_sample--;
2985 sc->current_index--;
2986 if (sc->index_ranges &&
2987 sc->current_index < sc->current_index_range->start &&
2988 sc->current_index_range > sc->index_ranges) {
2989 sc->current_index_range--;
2990 sc->current_index = sc->current_index_range->end - 1;
2991 }
2992}
2993
2994static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
2995{
2996 int64_t range_size;
2997
2998 sc->current_sample = current_sample;
2999 sc->current_index = current_sample;
3000 if (!sc->index_ranges) {
3001 return;
3002 }
3003
3004 for (sc->current_index_range = sc->index_ranges;
3005 sc->current_index_range->end;
3006 sc->current_index_range++) {
3007 range_size = sc->current_index_range->end - sc->current_index_range->start;
3008 if (range_size > current_sample) {
3009 sc->current_index = sc->current_index_range->start + current_sample;
3010 break;
3011 }
3012 current_sample -= range_size;
3013 }
3014}
3015
3016/**
3017 * Fix st->index_entries, so that it contains only the entries (and the entries
3018 * which are needed to decode them) that fall in the edit list time ranges.
3019 * Also fixes the timestamps of the index entries to match the timeline
3020 * specified the edit lists.
3021 */
3022static void mov_fix_index(MOVContext *mov, AVStream *st)
3023{
3024 MOVStreamContext *msc = st->priv_data;
3025 AVIndexEntry *e_old = st->index_entries;
3026 int nb_old = st->nb_index_entries;
3027 const AVIndexEntry *e_old_end = e_old + nb_old;
3028 const AVIndexEntry *current = NULL;
3029 MOVStts *ctts_data_old = msc->ctts_data;
3030 int64_t ctts_index_old = 0;
3031 int64_t ctts_sample_old = 0;
3032 int64_t ctts_count_old = msc->ctts_count;
3033 int64_t edit_list_media_time = 0;
3034 int64_t edit_list_duration = 0;
3035 int64_t frame_duration = 0;
3036 int64_t edit_list_dts_counter = 0;
3037 int64_t edit_list_dts_entry_end = 0;
3038 int64_t edit_list_start_ctts_sample = 0;
3039 int64_t curr_cts;
3040 int64_t edit_list_index = 0;
3041 int64_t index;
3042 int64_t index_ctts_count;
3043 int flags;
3044 unsigned int ctts_allocated_size = 0;
3045 int64_t start_dts = 0;
3046 int64_t edit_list_media_time_dts = 0;
3047 int64_t edit_list_start_encountered = 0;
3048 int64_t search_timestamp = 0;
3049 int64_t* frame_duration_buffer = NULL;
3050 int num_discarded_begin = 0;
3051 int first_non_zero_audio_edit = -1;
3052 int packet_skip_samples = 0;
3053 MOVIndexRange *current_index_range;
3054
3055 if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
3056 return;
3057 }
3058
3059 // allocate the index ranges array
3060 msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
3061 if (!msc->index_ranges) {
3062 av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
3063 return;
3064 }
3065 msc->current_index_range = msc->index_ranges;
3066 current_index_range = msc->index_ranges - 1;
3067
3068 // Clean AVStream from traces of old index
3069 st->index_entries = NULL;
3070 st->index_entries_allocated_size = 0;
3071 st->nb_index_entries = 0;
3072
3073 // Clean ctts fields of MOVStreamContext
3074 msc->ctts_data = NULL;
3075 msc->ctts_count = 0;
3076 msc->ctts_index = 0;
3077 msc->ctts_sample = 0;
3078
3079 // If the dts_shift is positive (in case of negative ctts values in mov),
3080 // then negate the DTS by dts_shift
3081 if (msc->dts_shift > 0)
3082 edit_list_dts_entry_end -= msc->dts_shift;
3083
3084 // Offset the DTS by ctts[0] to make the PTS of the first frame 0
3085 if (ctts_data_old && ctts_count_old > 0) {
3086 edit_list_dts_entry_end -= ctts_data_old[0].duration;
3087 av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by ctts[%d].duration: %d\n", 0, ctts_data_old[0].duration);
3088 }
3089
3090 start_dts = edit_list_dts_entry_end;
3091
3092 while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
3093 &edit_list_duration, mov->time_scale)) {
3094 av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
3095 st->index, edit_list_index, edit_list_media_time, edit_list_duration);
3096 edit_list_index++;
3097 edit_list_dts_counter = edit_list_dts_entry_end;
3098 edit_list_dts_entry_end += edit_list_duration;
3099 num_discarded_begin = 0;
3100 if (edit_list_media_time == -1) {
3101 continue;
3102 }
3103
3104 // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
3105 // according to the edit list below.
3106 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3107 if (first_non_zero_audio_edit < 0) {
3108 first_non_zero_audio_edit = 1;
3109 } else {
3110 first_non_zero_audio_edit = 0;
3111 }
3112
3113 if (first_non_zero_audio_edit > 0)
3114 st->skip_samples = msc->start_pad = 0;
3115 }
3116
3117 //find closest previous key frame
3118 edit_list_media_time_dts = edit_list_media_time;
3119 if (msc->dts_shift > 0) {
3120 edit_list_media_time_dts -= msc->dts_shift;
3121 }
3122
3123 // While reordering frame index according to edit list we must handle properly
3124 // the scenario when edit list entry starts from none key frame.
3125 // We find closest previous key frame and preserve it and consequent frames in index.
3126 // All frames which are outside edit list entry time boundaries will be dropped after decoding.
3127 search_timestamp = edit_list_media_time_dts;
3128 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3129 // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
3130 // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
3131 // edit_list_media_time to cover the decoder delay.
3132 search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
3133 }
3134
3135 index = find_prev_closest_index(st, e_old, nb_old, search_timestamp, 0);
3136 if (index == -1) {
3137 av_log(mov->fc, AV_LOG_WARNING,
3138 "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
3139 st->index, edit_list_index, search_timestamp);
3140 index = find_prev_closest_index(st, e_old, nb_old, search_timestamp, AVSEEK_FLAG_ANY);
3141
3142 if (index == -1) {
3143 av_log(mov->fc, AV_LOG_WARNING,
3144 "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n"
3145 "Rounding edit list media time to zero.\n",
3146 st->index, edit_list_index, search_timestamp);
3147 index = 0;
3148 edit_list_media_time = 0;
3149 }
3150 }
3151 current = e_old + index;
3152
3153 ctts_index_old = 0;
3154 ctts_sample_old = 0;
3155
3156 // set ctts_index properly for the found key frame
3157 for (index_ctts_count = 0; index_ctts_count < index; index_ctts_count++) {
3158 if (ctts_data_old && ctts_index_old < ctts_count_old) {
3159 ctts_sample_old++;
3160 if (ctts_data_old[ctts_index_old].count == ctts_sample_old) {
3161 ctts_index_old++;
3162 ctts_sample_old = 0;
3163 }
3164 }
3165 }
3166
3167 edit_list_start_ctts_sample = ctts_sample_old;
3168
3169 // Iterate over index and arrange it according to edit list
3170 edit_list_start_encountered = 0;
3171 for (; current < e_old_end; current++, index++) {
3172 // check if frame outside edit list mark it for discard
3173 frame_duration = (current + 1 < e_old_end) ?
3174 ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
3175
3176 flags = current->flags;
3177
3178 // frames (pts) before or after edit list
3179 curr_cts = current->timestamp + msc->dts_shift;
3180
3181 if (ctts_data_old && ctts_index_old < ctts_count_old) {
3182 av_log(mov->fc, AV_LOG_DEBUG, "shifted frame pts, curr_cts: %"PRId64" @ %"PRId64", ctts: %d, ctts_count: %"PRId64"\n",
3183 curr_cts, ctts_index_old, ctts_data_old[ctts_index_old].duration, ctts_count_old);
3184 curr_cts += ctts_data_old[ctts_index_old].duration;
3185 ctts_sample_old++;
3186 if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
3187 if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3188 &ctts_allocated_size,
3189 ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3190 ctts_data_old[ctts_index_old].duration) == -1) {
3191 av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3192 ctts_index_old,
3193 ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3194 ctts_data_old[ctts_index_old].duration);
3195 break;
3196 }
3197 ctts_index_old++;
3198 ctts_sample_old = 0;
3199 edit_list_start_ctts_sample = 0;
3200 }
3201 }
3202
3203 if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
3204 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id != AV_CODEC_ID_VORBIS &&
3205 curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
3206 first_non_zero_audio_edit > 0) {
3207 packet_skip_samples = edit_list_media_time - curr_cts;
3208 st->skip_samples += packet_skip_samples;
3209
3210 // Shift the index entry timestamp by packet_skip_samples to be correct.
3211 edit_list_dts_counter -= packet_skip_samples;
3212 if (edit_list_start_encountered == 0) {
3213 edit_list_start_encountered = 1;
3214 // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
3215 // discarded packets.
3216 if (frame_duration_buffer) {
3217 fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3218 frame_duration_buffer, num_discarded_begin);
3219 av_freep(&frame_duration_buffer);
3220 }
3221 }
3222
3223 av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
3224 } else {
3225 flags |= AVINDEX_DISCARD_FRAME;
3226 av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
3227
3228 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && edit_list_start_encountered == 0) {
3229 num_discarded_begin++;
3230 frame_duration_buffer = av_realloc(frame_duration_buffer,
3231 num_discarded_begin * sizeof(int64_t));
3232 if (!frame_duration_buffer) {
3233 av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
3234 break;
3235 }
3236 frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
3237
3238 // Increment skip_samples for the first non-zero audio edit list
3239 if (first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
3240 st->skip_samples += frame_duration;
3241 msc->start_pad = st->skip_samples;
3242 }
3243 }
3244 }
3245 } else if (edit_list_start_encountered == 0) {
3246 edit_list_start_encountered = 1;
3247 // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
3248 // discarded packets.
3249 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && frame_duration_buffer) {
3250 fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3251 frame_duration_buffer, num_discarded_begin);
3252 av_freep(&frame_duration_buffer);
3253 }
3254 }
3255
3256 if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
3257 current->min_distance, flags) == -1) {
3258 av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
3259 break;
3260 }
3261
3262 // Update the index ranges array
3263 if (current_index_range < msc->index_ranges || index != current_index_range->end) {
3264 current_index_range++;
3265 current_index_range->start = index;
3266 }
3267 current_index_range->end = index + 1;
3268
3269 // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
3270 if (edit_list_start_encountered > 0) {
3271 edit_list_dts_counter = edit_list_dts_counter + frame_duration;
3272 }
3273
3274 // Break when found first key frame after edit entry completion
3275 if (((curr_cts + frame_duration) >= (edit_list_duration + edit_list_media_time)) &&
3276 ((flags & AVINDEX_KEYFRAME) || ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)))) {
3277
3278 if (ctts_data_old && ctts_sample_old != 0) {
3279 if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3280 &ctts_allocated_size,
3281 ctts_sample_old - edit_list_start_ctts_sample,
3282 ctts_data_old[ctts_index_old].duration) == -1) {
3283 av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3284 ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
3285 ctts_data_old[ctts_index_old].duration);
3286 break;
3287 }
3288 }
3289 break;
3290 }
3291 }
3292 }
3293 // Update av stream length
3294 st->duration = edit_list_dts_entry_end - start_dts;
3295
3296 // Free the old index and the old CTTS structures
3297 av_free(e_old);
3298 av_free(ctts_data_old);
3299
3300 // Null terminate the index ranges array
3301 current_index_range++;
3302 current_index_range->start = 0;
3303 current_index_range->end = 0;
3304 msc->current_index = msc->index_ranges[0].start;
3305}
3306
3307static void mov_build_index(MOVContext *mov, AVStream *st)
3308{
3309 MOVStreamContext *sc = st->priv_data;
3310 int64_t current_offset;
3311 int64_t current_dts = 0;
3312 unsigned int stts_index = 0;
3313 unsigned int stsc_index = 0;
3314 unsigned int stss_index = 0;
3315 unsigned int stps_index = 0;
3316 unsigned int i, j;
3317 uint64_t stream_size = 0;
3318
3319 if (sc->elst_count) {
3320 int i, edit_start_index = 0, multiple_edits = 0;
3321 int64_t empty_duration = 0; // empty duration of the first edit list entry
3322 int64_t start_time = 0; // start time of the media
3323
3324 for (i = 0; i < sc->elst_count; i++) {
3325 const MOVElst *e = &sc->elst_data[i];
3326 if (i == 0 && e->time == -1) {
3327 /* if empty, the first entry is the start time of the stream
3328 * relative to the presentation itself */
3329 empty_duration = e->duration;
3330 edit_start_index = 1;
3331 } else if (i == edit_start_index && e->time >= 0) {
3332 start_time = e->time;
3333 } else {
3334 multiple_edits = 1;
3335 }
3336 }
3337
3338 if (multiple_edits && !mov->advanced_editlist)
3339 av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
3340 "Use -advanced_editlist to correctly decode otherwise "
3341 "a/v desync might occur\n");
3342
3343 /* adjust first dts according to edit list */
3344 if ((empty_duration || start_time) && mov->time_scale > 0) {
3345 if (empty_duration)
3346 empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
3347 sc->time_offset = start_time - empty_duration;
3348 if (!mov->advanced_editlist)
3349 current_dts = -sc->time_offset;
3350 }
3351
3352 if (!multiple_edits && !mov->advanced_editlist &&
3353 st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
3354 sc->start_pad = start_time;
3355 }
3356
3357 /* only use old uncompressed audio chunk demuxing when stts specifies it */
3358 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3359 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
3360 unsigned int current_sample = 0;
3361 unsigned int stts_sample = 0;
3362 unsigned int sample_size;
3363 unsigned int distance = 0;
3364 unsigned int rap_group_index = 0;
3365 unsigned int rap_group_sample = 0;
3366 int64_t last_dts = 0;
3367 int64_t dts_correction = 0;
3368 int rap_group_present = sc->rap_group_count && sc->rap_group;
3369 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
3370
3371 current_dts -= sc->dts_shift;
3372 last_dts = current_dts;
3373
3374 if (!sc->sample_count || st->nb_index_entries)
3375 return;
3376 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
3377 return;
3378 if (av_reallocp_array(&st->index_entries,
3379 st->nb_index_entries + sc->sample_count,
3380 sizeof(*st->index_entries)) < 0) {
3381 st->nb_index_entries = 0;
3382 return;
3383 }
3384 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
3385
3386 for (i = 0; i < sc->chunk_count; i++) {
3387 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
3388 current_offset = sc->chunk_offsets[i];
3389 while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
3390 i + 1 == sc->stsc_data[stsc_index + 1].first)
3391 stsc_index++;
3392
3393 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
3394 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
3395 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
3396 sc->stsz_sample_size = sc->sample_size;
3397 }
3398 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
3399 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
3400 sc->stsz_sample_size = sc->sample_size;
3401 }
3402
3403 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
3404 int keyframe = 0;
3405 if (current_sample >= sc->sample_count) {
3406 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
3407 return;
3408 }
3409
3410 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
3411 keyframe = 1;
3412 if (stss_index + 1 < sc->keyframe_count)
3413 stss_index++;
3414 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
3415 keyframe = 1;
3416 if (stps_index + 1 < sc->stps_count)
3417 stps_index++;
3418 }
3419 if (rap_group_present && rap_group_index < sc->rap_group_count) {
3420 if (sc->rap_group[rap_group_index].index > 0)
3421 keyframe = 1;
3422 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
3423 rap_group_sample = 0;
3424 rap_group_index++;
3425 }
3426 }
3427 if (sc->keyframe_absent
3428 && !sc->stps_count
3429 && !rap_group_present
3430 && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
3431 keyframe = 1;
3432 if (keyframe)
3433 distance = 0;
3434 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
3435 if (sc->pseudo_stream_id == -1 ||
3436 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
3437 AVIndexEntry *e;
3438 if (sample_size > 0x3FFFFFFF) {
3439 av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
3440 return;
3441 }
3442 e = &st->index_entries[st->nb_index_entries++];
3443 e->pos = current_offset;
3444 e->timestamp = current_dts;
3445 e->size = sample_size;
3446 e->min_distance = distance;
3447 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
3448 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
3449 "size %u, distance %u, keyframe %d\n", st->index, current_sample,
3450 current_offset, current_dts, sample_size, distance, keyframe);
3451 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
3452 ff_rfps_add_frame(mov->fc, st, current_dts);
3453 }
3454
3455 current_offset += sample_size;
3456 stream_size += sample_size;
3457
3458 /* A negative sample duration is invalid based on the spec,
3459 * but some samples need it to correct the DTS. */
3460 if (sc->stts_data[stts_index].duration < 0) {
3461 av_log(mov->fc, AV_LOG_WARNING,
3462 "Invalid SampleDelta %d in STTS, at %d st:%d\n",
3463 sc->stts_data[stts_index].duration, stts_index,
3464 st->index);
3465 dts_correction += sc->stts_data[stts_index].duration - 1;
3466 sc->stts_data[stts_index].duration = 1;
3467 }
3468 current_dts += sc->stts_data[stts_index].duration;
3469 if (!dts_correction || current_dts + dts_correction > last_dts) {
3470 current_dts += dts_correction;
3471 dts_correction = 0;
3472 } else {
3473 /* Avoid creating non-monotonous DTS */
3474 dts_correction += current_dts - last_dts - 1;
3475 current_dts = last_dts + 1;
3476 }
3477 last_dts = current_dts;
3478 distance++;
3479 stts_sample++;
3480 current_sample++;
3481 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
3482 stts_sample = 0;
3483 stts_index++;
3484 }
3485 }
3486 }
3487 if (st->duration > 0)
3488 st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
3489 } else {
3490 unsigned chunk_samples, total = 0;
3491
3492 // compute total chunk count
3493 for (i = 0; i < sc->stsc_count; i++) {
3494 unsigned count, chunk_count;
3495
3496 chunk_samples = sc->stsc_data[i].count;
3497 if (i != sc->stsc_count - 1 &&
3498 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
3499 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
3500 return;
3501 }
3502
3503 if (sc->samples_per_frame >= 160) { // gsm
3504 count = chunk_samples / sc->samples_per_frame;
3505 } else if (sc->samples_per_frame > 1) {
3506 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
3507 count = (chunk_samples+samples-1) / samples;
3508 } else {
3509 count = (chunk_samples+1023) / 1024;
3510 }
3511
3512 if (mov_stsc_index_valid(i, sc->stsc_count))
3513 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
3514 else
3515 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
3516 total += chunk_count * count;
3517 }
3518
3519 av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
3520 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
3521 return;
3522 if (av_reallocp_array(&st->index_entries,
3523 st->nb_index_entries + total,
3524 sizeof(*st->index_entries)) < 0) {
3525 st->nb_index_entries = 0;
3526 return;
3527 }
3528 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
3529
3530 // populate index
3531 for (i = 0; i < sc->chunk_count; i++) {
3532 current_offset = sc->chunk_offsets[i];
3533 if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
3534 i + 1 == sc->stsc_data[stsc_index + 1].first)
3535 stsc_index++;
3536 chunk_samples = sc->stsc_data[stsc_index].count;
3537
3538 while (chunk_samples > 0) {
3539 AVIndexEntry *e;
3540 unsigned size, samples;
3541
3542 if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
3543 avpriv_request_sample(mov->fc,
3544 "Zero bytes per frame, but %d samples per frame",
3545 sc->samples_per_frame);
3546 return;
3547 }
3548
3549 if (sc->samples_per_frame >= 160) { // gsm
3550 samples = sc->samples_per_frame;
3551 size = sc->bytes_per_frame;
3552 } else {
3553 if (sc->samples_per_frame > 1) {
3554 samples = FFMIN((1024 / sc->samples_per_frame)*
3555 sc->samples_per_frame, chunk_samples);
3556 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
3557 } else {
3558 samples = FFMIN(1024, chunk_samples);
3559 size = samples * sc->sample_size;
3560 }
3561 }
3562
3563 if (st->nb_index_entries >= total) {
3564 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
3565 return;
3566 }
3567 if (size > 0x3FFFFFFF) {
3568 av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
3569 return;
3570 }
3571 e = &st->index_entries[st->nb_index_entries++];
3572 e->pos = current_offset;
3573 e->timestamp = current_dts;
3574 e->size = size;
3575 e->min_distance = 0;
3576 e->flags = AVINDEX_KEYFRAME;
3577 av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
3578 "size %u, duration %u\n", st->index, i, current_offset, current_dts,
3579 size, samples);
3580
3581 current_offset += size;
3582 current_dts += samples;
3583 chunk_samples -= samples;
3584 }
3585 }
3586 }
3587
3588 if (!mov->ignore_editlist && mov->advanced_editlist) {
3589 // Fix index according to edit lists.
3590 mov_fix_index(mov, st);
3591 }
3592}
3593
3594static int test_same_origin(const char *src, const char *ref) {
3595 char src_proto[64];
3596 char ref_proto[64];
3597 char src_auth[256];
3598 char ref_auth[256];
3599 char src_host[256];
3600 char ref_host[256];
3601 int src_port=-1;
3602 int ref_port=-1;
3603
3604 av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
3605 av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
3606
3607 if (strlen(src) == 0) {
3608 return -1;
3609 } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
3610 strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
3611 strlen(src_host) + 1 >= sizeof(src_host) ||
3612 strlen(ref_host) + 1 >= sizeof(ref_host)) {
3613 return 0;
3614 } else if (strcmp(src_proto, ref_proto) ||
3615 strcmp(src_auth, ref_auth) ||
3616 strcmp(src_host, ref_host) ||
3617 src_port != ref_port) {
3618 return 0;
3619 } else
3620 return 1;
3621}
3622
3623static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
3624{
3625 /* try relative path, we do not try the absolute because it can leak information about our
3626 system to an attacker */
3627 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
3628 char filename[1025];
3629 const char *src_path;
3630 int i, l;
3631
3632 /* find a source dir */
3633 src_path = strrchr(src, '/');
3634 if (src_path)
3635 src_path++;
3636 else
3637 src_path = src;
3638
3639 /* find a next level down to target */
3640 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
3641 if (ref->path[l] == '/') {
3642 if (i == ref->nlvl_to - 1)
3643 break;
3644 else
3645 i++;
3646 }
3647
3648 /* compose filename if next level down to target was found */
3649 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
3650 memcpy(filename, src, src_path - src);
3651 filename[src_path - src] = 0;
3652
3653 for (i = 1; i < ref->nlvl_from; i++)
3654 av_strlcat(filename, "../", sizeof(filename));
3655
3656 av_strlcat(filename, ref->path + l + 1, sizeof(filename));
3657 if (!c->use_absolute_path) {
3658 int same_origin = test_same_origin(src, filename);
3659
3660 if (!same_origin) {
3661 av_log(c->fc, AV_LOG_ERROR,
3662 "Reference with mismatching origin, %s not tried for security reasons, "
3663 "set demuxer option use_absolute_path to allow it anyway\n",
3664 ref->path);
3665 return AVERROR(ENOENT);
3666 }
3667
3668 if(strstr(ref->path + l + 1, "..") ||
3669 strstr(ref->path + l + 1, ":") ||
3670 (ref->nlvl_from > 1 && same_origin < 0) ||
3671 (filename[0] == '/' && src_path == src))
3672 return AVERROR(ENOENT);
3673 }
3674
3675 if (strlen(filename) + 1 == sizeof(filename))
3676 return AVERROR(ENOENT);
3677 if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
3678 return 0;
3679 }
3680 } else if (c->use_absolute_path) {
3681 av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
3682 "this is a possible security issue\n");
3683 if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
3684 return 0;
3685 } else {
3686 av_log(c->fc, AV_LOG_ERROR,
3687 "Absolute path %s not tried for security reasons, "
3688 "set demuxer option use_absolute_path to allow absolute paths\n",
3689 ref->path);
3690 }
3691
3692 return AVERROR(ENOENT);
3693}
3694
3695static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
3696{
3697 if (sc->time_scale <= 0) {
3698 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
3699 sc->time_scale = c->time_scale;
3700 if (sc->time_scale <= 0)
3701 sc->time_scale = 1;
3702 }
3703}
3704
3705static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3706{
3707 AVStream *st;
3708 MOVStreamContext *sc;
3709 int ret;
3710
3711 st = avformat_new_stream(c->fc, NULL);
3712 if (!st) return AVERROR(ENOMEM);
3713 st->id = c->fc->nb_streams;
3714 sc = av_mallocz(sizeof(MOVStreamContext));
3715 if (!sc) return AVERROR(ENOMEM);
3716
3717 st->priv_data = sc;
3718 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
3719 sc->ffindex = st->index;
3720 c->trak_index = st->index;
3721
3722 if ((ret = mov_read_default(c, pb, atom)) < 0)
3723 return ret;
3724
3725 c->trak_index = -1;
3726
3727 /* sanity checks */
3728 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
3729 (!sc->sample_size && !sc->sample_count))) {
3730 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
3731 st->index);
3732 return 0;
3733 }
3734
3735 fix_timescale(c, sc);
3736
3737 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
3738
3739 mov_build_index(c, st);
3740
3741 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
3742 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
3743 if (c->enable_drefs) {
3744 if (mov_open_dref(c, &sc->pb, c->fc->filename, dref) < 0)
3745 av_log(c->fc, AV_LOG_ERROR,
3746 "stream %d, error opening alias: path='%s', dir='%s', "
3747 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
3748 st->index, dref->path, dref->dir, dref->filename,
3749 dref->volume, dref->nlvl_from, dref->nlvl_to);
3750 } else {
3751 av_log(c->fc, AV_LOG_WARNING,
3752 "Skipped opening external track: "
3753 "stream %d, alias: path='%s', dir='%s', "
3754 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
3755 "Set enable_drefs to allow this.\n",
3756 st->index, dref->path, dref->dir, dref->filename,
3757 dref->volume, dref->nlvl_from, dref->nlvl_to);
3758 }
3759 } else {
3760 sc->pb = c->fc->pb;
3761 sc->pb_is_copied = 1;
3762 }
3763
3764 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3765 if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
3766 sc->height && sc->width &&
3767 (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
3768 st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
3769 ((double)st->codecpar->width * sc->height), INT_MAX);
3770 }
3771
3772#if FF_API_R_FRAME_RATE
3773 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
3774 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
3775 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
3776#endif
3777 }
3778
3779 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
3780 if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
3781 TAG_IS_AVCI(st->codecpar->codec_tag)) {
3782 ret = ff_generate_avci_extradata(st);
3783 if (ret < 0)
3784 return ret;
3785 }
3786
3787 switch (st->codecpar->codec_id) {
3788#if CONFIG_H261_DECODER
3789 case AV_CODEC_ID_H261:
3790#endif
3791#if CONFIG_H263_DECODER
3792 case AV_CODEC_ID_H263:
3793#endif
3794#if CONFIG_MPEG4_DECODER
3795 case AV_CODEC_ID_MPEG4:
3796#endif
3797 st->codecpar->width = 0; /* let decoder init width/height */
3798 st->codecpar->height= 0;
3799 break;
3800 }
3801
3802 // If the duration of the mp3 packets is not constant, then they could need a parser
3803 if (st->codecpar->codec_id == AV_CODEC_ID_MP3
3804 && sc->stts_count > 3
3805 && sc->stts_count*10 > st->nb_frames
3806 && sc->time_scale == st->codecpar->sample_rate) {
3807 st->need_parsing = AVSTREAM_PARSE_FULL;
3808 }
3809 /* Do not need those anymore. */
3810 av_freep(&sc->chunk_offsets);
3811 av_freep(&sc->sample_sizes);
3812 av_freep(&sc->keyframes);
3813 av_freep(&sc->stts_data);
3814 av_freep(&sc->stps_data);
3815 av_freep(&sc->elst_data);
3816 av_freep(&sc->rap_group);
3817
3818 return 0;
3819}
3820
3821static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3822{
3823 int ret;
3824 c->itunes_metadata = 1;
3825 ret = mov_read_default(c, pb, atom);
3826 c->itunes_metadata = 0;
3827 return ret;
3828}
3829
3830static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3831{
3832 uint32_t count;
3833 uint32_t i;
3834
3835 if (atom.size < 8)
3836 return 0;
3837
3838 avio_skip(pb, 4);
3839 count = avio_rb32(pb);
3840 if (count > UINT_MAX / sizeof(*c->meta_keys) - 1) {
3841 av_log(c->fc, AV_LOG_ERROR,
3842 "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
3843 return AVERROR_INVALIDDATA;
3844 }
3845
3846 c->meta_keys_count = count + 1;
3847 c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
3848 if (!c->meta_keys)
3849 return AVERROR(ENOMEM);
3850
3851 for (i = 1; i <= count; ++i) {
3852 uint32_t key_size = avio_rb32(pb);
3853 uint32_t type = avio_rl32(pb);
3854 if (key_size < 8) {
3855 av_log(c->fc, AV_LOG_ERROR,
3856 "The key# %"PRIu32" in meta has invalid size:"
3857 "%"PRIu32"\n", i, key_size);
3858 return AVERROR_INVALIDDATA;
3859 }
3860 key_size -= 8;
3861 if (type != MKTAG('m','d','t','a')) {
3862 avio_skip(pb, key_size);
3863 }
3864 c->meta_keys[i] = av_mallocz(key_size + 1);
3865 if (!c->meta_keys[i])
3866 return AVERROR(ENOMEM);
3867 avio_read(pb, c->meta_keys[i], key_size);
3868 }
3869
3870 return 0;
3871}
3872
3873static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3874{
3875 int64_t end = avio_tell(pb) + atom.size;
3876 uint8_t *key = NULL, *val = NULL, *mean = NULL;
3877 int i;
3878 int ret = 0;
3879 AVStream *st;
3880 MOVStreamContext *sc;
3881
3882 if (c->fc->nb_streams < 1)
3883 return 0;
3884 st = c->fc->streams[c->fc->nb_streams-1];
3885 sc = st->priv_data;
3886
3887 for (i = 0; i < 3; i++) {
3888 uint8_t **p;
3889 uint32_t len, tag;
3890
3891 if (end - avio_tell(pb) <= 12)
3892 break;
3893
3894 len = avio_rb32(pb);
3895 tag = avio_rl32(pb);
3896 avio_skip(pb, 4); // flags
3897
3898 if (len < 12 || len - 12 > end - avio_tell(pb))
3899 break;
3900 len -= 12;
3901
3902 if (tag == MKTAG('m', 'e', 'a', 'n'))
3903 p = &mean;
3904 else if (tag == MKTAG('n', 'a', 'm', 'e'))
3905 p = &key;
3906 else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
3907 avio_skip(pb, 4);
3908 len -= 4;
3909 p = &val;
3910 } else
3911 break;
3912
3913 *p = av_malloc(len + 1);
3914 if (!*p)
3915 break;
3916 ret = ffio_read_size(pb, *p, len);
3917 if (ret < 0) {
3918 av_freep(p);
3919 break;
3920 }
3921 (*p)[len] = 0;
3922 }
3923
3924 if (mean && key && val) {
3925 if (strcmp(key, "iTunSMPB") == 0) {
3926 int priming, remainder, samples;
3927 av_dict_set(&st->metadata, key, val, 0);
3928 if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
3929 if(priming>0 && priming<16384)
3930 sc->start_pad = priming;
3931 }
3932 }
3933 if (strcmp(key, "cdec") != 0) {
3934 av_dict_set(&c->fc->metadata, key, val,
3935 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
3936 key = val = NULL;
3937 }
3938 } else {
3939 av_log(c->fc, AV_LOG_VERBOSE,
3940 "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
3941 }
3942
3943 avio_seek(pb, end, SEEK_SET);
3944 av_freep(&key);
3945 av_freep(&val);
3946 av_freep(&mean);
3947 return ret;
3948}
3949
3950static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3951{
3952 while (atom.size > 8) {
3953 uint32_t tag = avio_rl32(pb);
3954 atom.size -= 4;
3955 if (tag == MKTAG('h','d','l','r')) {
3956 avio_seek(pb, -8, SEEK_CUR);
3957 atom.size += 8;
3958 return mov_read_default(c, pb, atom);
3959 }
3960 }
3961 return 0;
3962}
3963
3964// return 1 when matrix is identity, 0 otherwise
3965#define IS_MATRIX_IDENT(matrix) \
3966 ( (matrix)[0][0] == (1 << 16) && \
3967 (matrix)[1][1] == (1 << 16) && \
3968 (matrix)[2][2] == (1 << 30) && \
3969 !(matrix)[0][1] && !(matrix)[0][2] && \
3970 !(matrix)[1][0] && !(matrix)[1][2] && \
3971 !(matrix)[2][0] && !(matrix)[2][1])
3972
3973static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3974{
3975 int i, j, e;
3976 int width;
3977 int height;
3978 int display_matrix[3][3];
3979 int res_display_matrix[3][3] = { { 0 } };
3980 AVStream *st;
3981 MOVStreamContext *sc;
3982 int version;
3983 int flags;
3984
3985 if (c->fc->nb_streams < 1)
3986 return 0;
3987 st = c->fc->streams[c->fc->nb_streams-1];
3988 sc = st->priv_data;
3989
3990 version = avio_r8(pb);
3991 flags = avio_rb24(pb);
3992 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
3993
3994 if (version == 1) {
3995 avio_rb64(pb);
3996 avio_rb64(pb);
3997 } else {
3998 avio_rb32(pb); /* creation time */
3999 avio_rb32(pb); /* modification time */
4000 }
4001 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
4002 avio_rb32(pb); /* reserved */
4003
4004 /* highlevel (considering edits) duration in movie timebase */
4005 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
4006 avio_rb32(pb); /* reserved */
4007 avio_rb32(pb); /* reserved */
4008
4009 avio_rb16(pb); /* layer */
4010 avio_rb16(pb); /* alternate group */
4011 avio_rb16(pb); /* volume */
4012 avio_rb16(pb); /* reserved */
4013
4014 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
4015 // they're kept in fixed point format through all calculations
4016 // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
4017 // side data, but the scale factor is not needed to calculate aspect ratio
4018 for (i = 0; i < 3; i++) {
4019 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
4020 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
4021 display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
4022 }
4023
4024 width = avio_rb32(pb); // 16.16 fixed point track width
4025 height = avio_rb32(pb); // 16.16 fixed point track height
4026 sc->width = width >> 16;
4027 sc->height = height >> 16;
4028
4029 // apply the moov display matrix (after the tkhd one)
4030 for (i = 0; i < 3; i++) {
4031 const int sh[3] = { 16, 16, 30 };
4032 for (j = 0; j < 3; j++) {
4033 for (e = 0; e < 3; e++) {
4034 res_display_matrix[i][j] +=
4035 ((int64_t) display_matrix[i][e] *
4036 c->movie_display_matrix[e][j]) >> sh[e];
4037 }
4038 }
4039 }
4040
4041 // save the matrix when it is not the default identity
4042 if (!IS_MATRIX_IDENT(res_display_matrix)) {
4043 double rotate;
4044
4045 av_freep(&sc->display_matrix);
4046 sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
4047 if (!sc->display_matrix)
4048 return AVERROR(ENOMEM);
4049
4050 for (i = 0; i < 3; i++)
4051 for (j = 0; j < 3; j++)
4052 sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
4053
4054#if FF_API_OLD_ROTATE_API
4055 rotate = av_display_rotation_get(sc->display_matrix);
4056 if (!isnan(rotate)) {
4057 char rotate_buf[64];
4058 rotate = -rotate;
4059 if (rotate < 0) // for backward compatibility
4060 rotate += 360;
4061 snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
4062 av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
4063 }
4064#endif
4065 }
4066
4067 // transform the display width/height according to the matrix
4068 // to keep the same scale, use [width height 1<<16]
4069 if (width && height && sc->display_matrix) {
4070 double disp_transform[2];
4071
4072 for (i = 0; i < 2; i++)
4073 disp_transform[i] = hypot(sc->display_matrix[0 + i],
4074 sc->display_matrix[3 + i]);
4075
4076 if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
4077 disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
4078 fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
4079 st->sample_aspect_ratio = av_d2q(
4080 disp_transform[0] / disp_transform[1],
4081 INT_MAX);
4082 }
4083 return 0;
4084}
4085
4086static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4087{
4088 MOVFragment *frag = &c->fragment;
4089 MOVTrackExt *trex = NULL;
4090 MOVFragmentIndex* index = NULL;
4091 int flags, track_id, i, found = 0;
4092
4093 avio_r8(pb); /* version */
4094 flags = avio_rb24(pb);
4095
4096 track_id = avio_rb32(pb);
4097 if (!track_id)
4098 return AVERROR_INVALIDDATA;
4099 frag->track_id = track_id;
4100 for (i = 0; i < c->trex_count; i++)
4101 if (c->trex_data[i].track_id == frag->track_id) {
4102 trex = &c->trex_data[i];
4103 break;
4104 }
4105 if (!trex) {
4106 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
4107 return AVERROR_INVALIDDATA;
4108 }
4109
4110 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
4111 avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ?
4112 frag->moof_offset : frag->implicit_offset;
4113 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
4114
4115 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
4116 avio_rb32(pb) : trex->duration;
4117 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
4118 avio_rb32(pb) : trex->size;
4119 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
4120 avio_rb32(pb) : trex->flags;
4121 frag->time = AV_NOPTS_VALUE;
4122 for (i = 0; i < c->fragment_index_count; i++) {
4123 int j;
4124 MOVFragmentIndex* candidate = c->fragment_index_data[i];
4125 if (candidate->track_id == frag->track_id) {
4126 av_log(c->fc, AV_LOG_DEBUG,
4127 "found fragment index for track %u\n", frag->track_id);
4128 index = candidate;
4129 for (j = index->current_item; j < index->item_count; j++) {
4130 if (frag->implicit_offset == index->items[j].moof_offset) {
4131 av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
4132 "for track %u and moof_offset %"PRId64"\n",
4133 frag->track_id, index->items[j].moof_offset);
4134 frag->time = index->items[j].time;
4135 index->current_item = j + 1;
4136 found = 1;
4137 break;
4138 }
4139 }
4140 if (found)
4141 break;
4142 }
4143 }
4144 if (index && !found) {
4145 av_log(c->fc, AV_LOG_DEBUG, "track %u has a fragment index but "
4146 "it doesn't have an (in-order) entry for moof_offset "
4147 "%"PRId64"\n", frag->track_id, frag->implicit_offset);
4148 }
4149 av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
4150 return 0;
4151}
4152
4153static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4154{
4155 unsigned i, num;
4156 void *new_tracks;
4157
4158 num = atom.size / 4;
4159 if (!(new_tracks = av_malloc_array(num, sizeof(int))))
4160 return AVERROR(ENOMEM);
4161
4162 av_free(c->chapter_tracks);
4163 c->chapter_tracks = new_tracks;
4164 c->nb_chapter_tracks = num;
4165
4166 for (i = 0; i < num && !pb->eof_reached; i++)
4167 c->chapter_tracks[i] = avio_rb32(pb);
4168
4169 return 0;
4170}
4171
4172static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4173{
4174 MOVTrackExt *trex;
4175 int err;
4176
4177 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
4178 return AVERROR_INVALIDDATA;
4179 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
4180 sizeof(*c->trex_data))) < 0) {
4181 c->trex_count = 0;
4182 return err;
4183 }
4184
4185 c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
4186
4187 trex = &c->trex_data[c->trex_count++];
4188 avio_r8(pb); /* version */
4189 avio_rb24(pb); /* flags */
4190 trex->track_id = avio_rb32(pb);
4191 trex->stsd_id = avio_rb32(pb);
4192 trex->duration = avio_rb32(pb);
4193 trex->size = avio_rb32(pb);
4194 trex->flags = avio_rb32(pb);
4195 return 0;
4196}
4197
4198static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4199{
4200 MOVFragment *frag = &c->fragment;
4201 AVStream *st = NULL;
4202 MOVStreamContext *sc;
4203 int version, i;
4204
4205 for (i = 0; i < c->fc->nb_streams; i++) {
4206 if (c->fc->streams[i]->id == frag->track_id) {
4207 st = c->fc->streams[i];
4208 break;
4209 }
4210 }
4211 if (!st) {
4212 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %u\n", frag->track_id);
4213 return AVERROR_INVALIDDATA;
4214 }
4215 sc = st->priv_data;
4216 if (sc->pseudo_stream_id + 1 != frag->stsd_id)
4217 return 0;
4218 version = avio_r8(pb);
4219 avio_rb24(pb); /* flags */
4220 if (version) {
4221 sc->track_end = avio_rb64(pb);
4222 } else {
4223 sc->track_end = avio_rb32(pb);
4224 }
4225 return 0;
4226}
4227
4228static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4229{
4230 MOVFragment *frag = &c->fragment;
4231 AVStream *st = NULL;
4232 MOVStreamContext *sc;
4233 MOVStts *ctts_data;
4234 uint64_t offset;
4235 int64_t dts;
4236 int data_offset = 0;
4237 unsigned entries, first_sample_flags = frag->flags;
4238 int flags, distance, i, err;
4239
4240 for (i = 0; i < c->fc->nb_streams; i++) {
4241 if (c->fc->streams[i]->id == frag->track_id) {
4242 st = c->fc->streams[i];
4243 break;
4244 }
4245 }
4246 if (!st) {
4247 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %u\n", frag->track_id);
4248 return AVERROR_INVALIDDATA;
4249 }
4250 sc = st->priv_data;
4251 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
4252 return 0;
4253 avio_r8(pb); /* version */
4254 flags = avio_rb24(pb);
4255 entries = avio_rb32(pb);
4256 av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
4257
4258 /* Always assume the presence of composition time offsets.
4259 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
4260 * 1) in the initial movie, there are no samples.
4261 * 2) in the first movie fragment, there is only one sample without composition time offset.
4262 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
4263 if (!sc->ctts_count && sc->sample_count)
4264 {
4265 /* Complement ctts table if moov atom doesn't have ctts atom. */
4266 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
4267 if (!ctts_data)
4268 return AVERROR(ENOMEM);
4269 sc->ctts_data = ctts_data;
4270 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
4271 sc->ctts_data[sc->ctts_count].duration = 0;
4272 sc->ctts_count++;
4273 }
4274 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
4275 return AVERROR_INVALIDDATA;
4276 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
4277 sizeof(*sc->ctts_data))) < 0) {
4278 sc->ctts_count = 0;
4279 return err;
4280 }
4281 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
4282 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
4283 dts = sc->track_end - sc->time_offset;
4284 offset = frag->base_data_offset + data_offset;
4285 distance = 0;
4286 av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
4287 for (i = 0; i < entries && !pb->eof_reached; i++) {
4288 unsigned sample_size = frag->size;
4289 int sample_flags = i ? frag->flags : first_sample_flags;
4290 unsigned sample_duration = frag->duration;
4291 int keyframe = 0;
4292
4293 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
4294 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
4295 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
4296 sc->ctts_data[sc->ctts_count].count = 1;
4297 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
4298 avio_rb32(pb) : 0;
4299 mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
4300 if (frag->time != AV_NOPTS_VALUE) {
4301 if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
4302 int64_t pts = frag->time;
4303 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
4304 " sc->dts_shift %d ctts.duration %d"
4305 " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
4306 sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
4307 sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
4308 dts = pts - sc->dts_shift;
4309 if (flags & MOV_TRUN_SAMPLE_CTS) {
4310 dts -= sc->ctts_data[sc->ctts_count].duration;
4311 } else {
4312 dts -= sc->time_offset;
4313 }
4314 av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
4315 } else {
4316 dts = frag->time - sc->time_offset;
4317 av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
4318 ", using it for dts\n", dts);
4319 }
4320 frag->time = AV_NOPTS_VALUE;
4321 }
4322 sc->ctts_count++;
4323 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
4324 keyframe = 1;
4325 else
4326 keyframe =
4327 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
4328 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
4329 if (keyframe)
4330 distance = 0;
4331 err = av_add_index_entry(st, offset, dts, sample_size, distance,
4332 keyframe ? AVINDEX_KEYFRAME : 0);
4333 if (err < 0) {
4334 av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
4335 }
4336 av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
4337 "size %u, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
4338 offset, dts, sample_size, distance, keyframe);
4339 distance++;
4340 dts += sample_duration;
4341 offset += sample_size;
4342 sc->data_size += sample_size;
4343 sc->duration_for_fps += sample_duration;
4344 sc->nb_frames_for_fps ++;
4345 }
4346
4347 if (pb->eof_reached)
4348 return AVERROR_EOF;
4349
4350 frag->implicit_offset = offset;
4351
4352 sc->track_end = dts + sc->time_offset;
4353 if (st->duration < sc->track_end)
4354 st->duration = sc->track_end;
4355
4356 return 0;
4357}
4358
4359static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4360{
4361 int64_t offset = avio_tell(pb) + atom.size, pts;
4362 uint8_t version;
4363 unsigned i, track_id;
4364 AVStream *st = NULL;
4365 AVStream *ref_st = NULL;
4366 MOVStreamContext *sc, *ref_sc = NULL;
4367 MOVFragmentIndex *index = NULL;
4368 MOVFragmentIndex **tmp;
4369 AVRational timescale;
4370
4371 version = avio_r8(pb);
4372 if (version > 1) {
4373 avpriv_request_sample(c->fc, "sidx version %u", version);
4374 return 0;
4375 }
4376
4377 avio_rb24(pb); // flags
4378
4379 track_id = avio_rb32(pb); // Reference ID
4380 for (i = 0; i < c->fc->nb_streams; i++) {
4381 if (c->fc->streams[i]->id == track_id) {
4382 st = c->fc->streams[i];
4383 break;
4384 }
4385 }
4386 if (!st) {
4387 av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
4388 return 0;
4389 }
4390
4391 sc = st->priv_data;
4392
4393 timescale = av_make_q(1, avio_rb32(pb));
4394
4395 if (timescale.den <= 0) {
4396 av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
4397 return AVERROR_INVALIDDATA;
4398 }
4399
4400 if (version == 0) {
4401 pts = avio_rb32(pb);
4402 offset += avio_rb32(pb);
4403 } else {
4404 pts = avio_rb64(pb);
4405 offset += avio_rb64(pb);
4406 }
4407
4408 avio_rb16(pb); // reserved
4409
4410 index = av_mallocz(sizeof(MOVFragmentIndex));
4411 if (!index)
4412 return AVERROR(ENOMEM);
4413
4414 index->track_id = track_id;
4415
4416 index->item_count = avio_rb16(pb);
4417 index->items = av_mallocz_array(index->item_count, sizeof(MOVFragmentIndexItem));
4418
4419 if (!index->items) {
4420 av_freep(&index);
4421 return AVERROR(ENOMEM);
4422 }
4423
4424 for (i = 0; i < index->item_count; i++) {
4425 uint32_t size = avio_rb32(pb);
4426 uint32_t duration = avio_rb32(pb);
4427 if (size & 0x80000000) {
4428 avpriv_request_sample(c->fc, "sidx reference_type 1");
4429 av_freep(&index->items);
4430 av_freep(&index);
4431 return AVERROR_PATCHWELCOME;
4432 }
4433 avio_rb32(pb); // sap_flags
4434 index->items[i].moof_offset = offset;
4435 index->items[i].time = av_rescale_q(pts, st->time_base, timescale);
4436 offset += size;
4437 pts += duration;
4438 }
4439
4440 st->duration = sc->track_end = pts;
4441
4442 tmp = av_realloc_array(c->fragment_index_data,
4443 c->fragment_index_count + 1,
4444 sizeof(MOVFragmentIndex*));
4445 if (!tmp) {
4446 av_freep(&index->items);
4447 av_freep(&index);
4448 return AVERROR(ENOMEM);
4449 }
4450
4451 c->fragment_index_data = tmp;
4452 c->fragment_index_data[c->fragment_index_count++] = index;
4453 sc->has_sidx = 1;
4454
4455 if (offset == avio_size(pb)) {
4456 for (i = 0; i < c->fc->nb_streams; i++) {
4457 if (c->fc->streams[i]->id == c->fragment_index_data[0]->track_id) {
4458 ref_st = c->fc->streams[i];
4459 ref_sc = ref_st->priv_data;
4460 break;
4461 }
4462 }
4463 for (i = 0; i < c->fc->nb_streams; i++) {
4464 st = c->fc->streams[i];
4465 sc = st->priv_data;
4466 if (!sc->has_sidx) {
4467 st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
4468 }
4469 }
4470
4471 c->fragment_index_complete = 1;
4472 }
4473
4474 return 0;
4475}
4476
4477/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
4478/* like the files created with Adobe Premiere 5.0, for samples see */
4479/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
4480static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4481{
4482 int err;
4483
4484 if (atom.size < 8)
4485 return 0; /* continue */
4486 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
4487 avio_skip(pb, atom.size - 4);
4488 return 0;
4489 }
4490 atom.type = avio_rl32(pb);
4491 atom.size -= 8;
4492 if (atom.type != MKTAG('m','d','a','t')) {
4493 avio_skip(pb, atom.size);
4494 return 0;
4495 }
4496 err = mov_read_mdat(c, pb, atom);
4497 return err;
4498}
4499
4500static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4501{
4502#if CONFIG_ZLIB
4503 AVIOContext ctx;
4504 uint8_t *cmov_data;
4505 uint8_t *moov_data; /* uncompressed data */
4506 long cmov_len, moov_len;
4507 int ret = -1;
4508
4509 avio_rb32(pb); /* dcom atom */
4510 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
4511 return AVERROR_INVALIDDATA;
4512 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
4513 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
4514 return AVERROR_INVALIDDATA;
4515 }
4516 avio_rb32(pb); /* cmvd atom */
4517 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
4518 return AVERROR_INVALIDDATA;
4519 moov_len = avio_rb32(pb); /* uncompressed size */
4520 cmov_len = atom.size - 6 * 4;
4521
4522 cmov_data = av_malloc(cmov_len);
4523 if (!cmov_data)
4524 return AVERROR(ENOMEM);
4525 moov_data = av_malloc(moov_len);
4526 if (!moov_data) {
4527 av_free(cmov_data);
4528 return AVERROR(ENOMEM);
4529 }
4530 ret = ffio_read_size(pb, cmov_data, cmov_len);
4531 if (ret < 0)
4532 goto free_and_return;
4533
4534 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
4535 goto free_and_return;
4536 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
4537 goto free_and_return;
4538 ctx.seekable = AVIO_SEEKABLE_NORMAL;
4539 atom.type = MKTAG('m','o','o','v');
4540 atom.size = moov_len;
4541 ret = mov_read_default(c, &ctx, atom);
4542free_and_return:
4543 av_free(moov_data);
4544 av_free(cmov_data);
4545 return ret;
4546#else
4547 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
4548 return AVERROR(ENOSYS);
4549#endif
4550}
4551
4552/* edit list atom */
4553static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4554{
4555 MOVStreamContext *sc;
4556 int i, edit_count, version;
4557
4558 if (c->fc->nb_streams < 1 || c->ignore_editlist)
4559 return 0;
4560 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
4561
4562 version = avio_r8(pb); /* version */
4563 avio_rb24(pb); /* flags */
4564 edit_count = avio_rb32(pb); /* entries */
4565
4566 if (!edit_count)
4567 return 0;
4568 if (sc->elst_data)
4569 av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
4570 av_free(sc->elst_data);
4571 sc->elst_count = 0;
4572 sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
4573 if (!sc->elst_data)
4574 return AVERROR(ENOMEM);
4575
4576 av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
4577 for (i = 0; i < edit_count && !pb->eof_reached; i++) {
4578 MOVElst *e = &sc->elst_data[i];
4579
4580 if (version == 1) {
4581 e->duration = avio_rb64(pb);
4582 e->time = avio_rb64(pb);
4583 } else {
4584 e->duration = avio_rb32(pb); /* segment duration */
4585 e->time = (int32_t)avio_rb32(pb); /* media time */
4586 }
4587 e->rate = avio_rb32(pb) / 65536.0;
4588 av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
4589 e->duration, e->time, e->rate);
4590
4591 if (e->time < 0 && e->time != -1 &&
4592 c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
4593 av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
4594 c->fc->nb_streams-1, i, e->time);
4595 return AVERROR_INVALIDDATA;
4596 }
4597 }
4598 sc->elst_count = i;
4599
4600 return 0;
4601}
4602
4603static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4604{
4605 MOVStreamContext *sc;
4606
4607 if (c->fc->nb_streams < 1)
4608 return AVERROR_INVALIDDATA;
4609 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
4610 sc->timecode_track = avio_rb32(pb);
4611 return 0;
4612}
4613
4614static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4615{
4616 AVStream *st;
4617 MOVStreamContext *sc;
4618 enum AVStereo3DType type;
4619 int mode;
4620
4621 if (c->fc->nb_streams < 1)
4622 return 0;
4623
4624 st = c->fc->streams[c->fc->nb_streams - 1];
4625 sc = st->priv_data;
4626
4627 if (atom.size < 5) {
4628 av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
4629 return AVERROR_INVALIDDATA;
4630 }
4631 avio_skip(pb, 4); /* version + flags */
4632
4633 mode = avio_r8(pb);
4634 switch (mode) {
4635 case 0:
4636 type = AV_STEREO3D_2D;
4637 break;
4638 case 1:
4639 type = AV_STEREO3D_TOPBOTTOM;
4640 break;
4641 case 2:
4642 type = AV_STEREO3D_SIDEBYSIDE;
4643 break;
4644 default:
4645 av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
4646 return 0;
4647 }
4648
4649 sc->stereo3d = av_stereo3d_alloc();
4650 if (!sc->stereo3d)
4651 return AVERROR(ENOMEM);
4652
4653 sc->stereo3d->type = type;
4654 return 0;
4655}
4656
4657static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4658{
4659 AVStream *st;
4660 MOVStreamContext *sc;
4661 int size, layout;
4662 int32_t yaw, pitch, roll;
4663 uint32_t l = 0, t = 0, r = 0, b = 0;
4664 uint32_t tag, padding = 0;
4665 enum AVSphericalProjection projection;
4666
4667 if (c->fc->nb_streams < 1)
4668 return 0;
4669
4670 st = c->fc->streams[c->fc->nb_streams - 1];
4671 sc = st->priv_data;
4672
4673 if (atom.size < 8) {
4674 av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
4675 return AVERROR_INVALIDDATA;
4676 }
4677
4678 size = avio_rb32(pb);
4679 if (size <= 12 || size > atom.size)
4680 return AVERROR_INVALIDDATA;
4681
4682 tag = avio_rl32(pb);
4683 if (tag != MKTAG('s','v','h','d')) {
4684 av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
4685 return 0;
4686 }
4687 avio_skip(pb, 4); /* version + flags */
4688 avio_skip(pb, size - 12); /* metadata_source */
4689
4690 size = avio_rb32(pb);
4691 if (size > atom.size)
4692 return AVERROR_INVALIDDATA;
4693
4694 tag = avio_rl32(pb);
4695 if (tag != MKTAG('p','r','o','j')) {
4696 av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
4697 return 0;
4698 }
4699
4700 size = avio_rb32(pb);
4701 if (size > atom.size)
4702 return AVERROR_INVALIDDATA;
4703
4704 tag = avio_rl32(pb);
4705 if (tag != MKTAG('p','r','h','d')) {
4706 av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
4707 return 0;
4708 }
4709 avio_skip(pb, 4); /* version + flags */
4710
4711 /* 16.16 fixed point */
4712 yaw = avio_rb32(pb);
4713 pitch = avio_rb32(pb);
4714 roll = avio_rb32(pb);
4715
4716 size = avio_rb32(pb);
4717 if (size > atom.size)
4718 return AVERROR_INVALIDDATA;
4719
4720 tag = avio_rl32(pb);
4721 avio_skip(pb, 4); /* version + flags */
4722 switch (tag) {
4723 case MKTAG('c','b','m','p'):
4724 layout = avio_rb32(pb);
4725 if (layout) {
4726 av_log(c->fc, AV_LOG_WARNING,
4727 "Unsupported cubemap layout %d\n", layout);
4728 return 0;
4729 }
4730 projection = AV_SPHERICAL_CUBEMAP;
4731 padding = avio_rb32(pb);
4732 break;
4733 case MKTAG('e','q','u','i'):
4734 t = avio_rb32(pb);
4735 b = avio_rb32(pb);
4736 l = avio_rb32(pb);
4737 r = avio_rb32(pb);
4738
4739 if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
4740 av_log(c->fc, AV_LOG_ERROR,
4741 "Invalid bounding rectangle coordinates "
4742 "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
4743 return AVERROR_INVALIDDATA;
4744 }
4745
4746 if (l || t || r || b)
4747 projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
4748 else
4749 projection = AV_SPHERICAL_EQUIRECTANGULAR;
4750 break;
4751 default:
4752 av_log(c->fc, AV_LOG_ERROR, "Unknown projection type\n");
4753 return 0;
4754 }
4755
4756 sc->spherical = av_spherical_alloc(&sc->spherical_size);
4757 if (!sc->spherical)
4758 return AVERROR(ENOMEM);
4759
4760 sc->spherical->projection = projection;
4761
4762 sc->spherical->yaw = yaw;
4763 sc->spherical->pitch = pitch;
4764 sc->spherical->roll = roll;
4765
4766 sc->spherical->padding = padding;
4767
4768 sc->spherical->bound_left = l;
4769 sc->spherical->bound_top = t;
4770 sc->spherical->bound_right = r;
4771 sc->spherical->bound_bottom = b;
4772
4773 return 0;
4774}
4775
4776static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
4777{
4778 int ret = 0;
4779 uint8_t *buffer = av_malloc(len + 1);
4780 const char *val;
4781
4782 if (!buffer)
4783 return AVERROR(ENOMEM);
4784 buffer[len] = '\0';
4785
4786 ret = ffio_read_size(pb, buffer, len);
4787 if (ret < 0)
4788 goto out;
4789
4790 /* Check for mandatory keys and values, try to support XML as best-effort */
4791 if (av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
4792 (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
4793 av_stristr(val, "true") &&
4794 (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
4795 av_stristr(val, "true") &&
4796 (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
4797 av_stristr(val, "equirectangular")) {
4798 sc->spherical = av_spherical_alloc(&sc->spherical_size);
4799 if (!sc->spherical)
4800 goto out;
4801
4802 sc->spherical->projection = AV_SPHERICAL_EQUIRECTANGULAR;
4803
4804 if (av_stristr(buffer, "<GSpherical:StereoMode>")) {
4805 enum AVStereo3DType mode;
4806
4807 if (av_stristr(buffer, "left-right"))
4808 mode = AV_STEREO3D_SIDEBYSIDE;
4809 else if (av_stristr(buffer, "top-bottom"))
4810 mode = AV_STEREO3D_TOPBOTTOM;
4811 else
4812 mode = AV_STEREO3D_2D;
4813
4814 sc->stereo3d = av_stereo3d_alloc();
4815 if (!sc->stereo3d)
4816 goto out;
4817
4818 sc->stereo3d->type = mode;
4819 }
4820
4821 /* orientation */
4822 val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
4823 if (val)
4824 sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
4825 val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
4826 if (val)
4827 sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
4828 val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
4829 if (val)
4830 sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
4831 }
4832
4833out:
4834 av_free(buffer);
4835 return ret;
4836}
4837
4838static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4839{
4840 AVStream *st;
4841 MOVStreamContext *sc;
4842 int64_t ret;
4843 uint8_t uuid[16];
4844 static const uint8_t uuid_isml_manifest[] = {
4845 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
4846 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
4847 };
4848 static const uint8_t uuid_xmp[] = {
4849 0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
4850 0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
4851 };
4852 static const uint8_t uuid_spherical[] = {
4853 0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
4854 0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
4855 };
4856
4857 if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
4858 return AVERROR_INVALIDDATA;
4859
4860 if (c->fc->nb_streams < 1)
4861 return 0;
4862 st = c->fc->streams[c->fc->nb_streams - 1];
4863 sc = st->priv_data;
4864
4865 ret = avio_read(pb, uuid, sizeof(uuid));
4866 if (ret < 0) {
4867 return ret;
4868 } else if (ret != sizeof(uuid)) {
4869 return AVERROR_INVALIDDATA;
4870 }
4871 if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
4872 uint8_t *buffer, *ptr;
4873 char *endptr;
4874 size_t len = atom.size - sizeof(uuid);
4875
4876 if (len < 4) {
4877 return AVERROR_INVALIDDATA;
4878 }
4879 ret = avio_skip(pb, 4); // zeroes
4880 len -= 4;
4881
4882 buffer = av_mallocz(len + 1);
4883 if (!buffer) {
4884 return AVERROR(ENOMEM);
4885 }
4886 ret = avio_read(pb, buffer, len);
4887 if (ret < 0) {
4888 av_free(buffer);
4889 return ret;
4890 } else if (ret != len) {
4891 av_free(buffer);
4892 return AVERROR_INVALIDDATA;
4893 }
4894
4895 ptr = buffer;
4896 while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
4897 ptr += sizeof("systemBitrate=\"") - 1;
4898 c->bitrates_count++;
4899 c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
4900 if (!c->bitrates) {
4901 c->bitrates_count = 0;
4902 av_free(buffer);
4903 return AVERROR(ENOMEM);
4904 }
4905 errno = 0;
4906 ret = strtol(ptr, &endptr, 10);
4907 if (ret < 0 || errno || *endptr != '"') {
4908 c->bitrates[c->bitrates_count - 1] = 0;
4909 } else {
4910 c->bitrates[c->bitrates_count - 1] = ret;
4911 }
4912 }
4913
4914 av_free(buffer);
4915 } else if (!memcmp(uuid, uuid_xmp, sizeof(uuid))) {
4916 uint8_t *buffer;
4917 size_t len = atom.size - sizeof(uuid);
4918 if (c->export_xmp) {
4919 buffer = av_mallocz(len + 1);
4920 if (!buffer) {
4921 return AVERROR(ENOMEM);
4922 }
4923 ret = avio_read(pb, buffer, len);
4924 if (ret < 0) {
4925 av_free(buffer);
4926 return ret;
4927 } else if (ret != len) {
4928 av_free(buffer);
4929 return AVERROR_INVALIDDATA;
4930 }
4931 buffer[len] = '\0';
4932 av_dict_set(&c->fc->metadata, "xmp", buffer, 0);
4933 av_free(buffer);
4934 } else {
4935 // skip all uuid atom, which makes it fast for long uuid-xmp file
4936 ret = avio_skip(pb, len);
4937 if (ret < 0)
4938 return ret;
4939 }
4940 } else if (!memcmp(uuid, uuid_spherical, sizeof(uuid))) {
4941 size_t len = atom.size - sizeof(uuid);
4942 ret = mov_parse_uuid_spherical(sc, pb, len);
4943 if (ret < 0)
4944 return ret;
4945 if (!sc->spherical)
4946 av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n"); }
4947
4948 return 0;
4949}
4950
4951static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4952{
4953 int ret;
4954 uint8_t content[16];
4955
4956 if (atom.size < 8)
4957 return 0;
4958
4959 ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
4960 if (ret < 0)
4961 return ret;
4962
4963 if ( !c->found_moov
4964 && !c->found_mdat
4965 && !memcmp(content, "Anevia\x1A\x1A", 8)
4966 && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
4967 c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
4968 }
4969
4970 return 0;
4971}
4972
4973static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
4974{
4975 uint32_t format = avio_rl32(pb);
4976 MOVStreamContext *sc;
4977 enum AVCodecID id;
4978 AVStream *st;
4979
4980 if (c->fc->nb_streams < 1)
4981 return 0;
4982 st = c->fc->streams[c->fc->nb_streams - 1];
4983 sc = st->priv_data;
4984
4985 switch (sc->format)
4986 {
4987 case MKTAG('e','n','c','v'): // encrypted video
4988 case MKTAG('e','n','c','a'): // encrypted audio
4989 id = mov_codec_id(st, format);
4990 if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
4991 st->codecpar->codec_id != id) {
4992 av_log(c->fc, AV_LOG_WARNING,
4993 "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
4994 (char*)&format, st->codecpar->codec_id);
4995 break;
4996 }
4997
4998 st->codecpar->codec_id = id;
4999 sc->format = format;
5000 break;
5001
5002 default:
5003 if (format != sc->format) {
5004 av_log(c->fc, AV_LOG_WARNING,
5005 "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
5006 (char*)&format, (char*)&sc->format);
5007 }
5008 break;
5009 }
5010
5011 return 0;
5012}
5013
5014static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5015{
5016 AVStream *st;
5017 MOVStreamContext *sc;
5018 size_t auxiliary_info_size;
5019
5020 if (c->decryption_key_len == 0 || c->fc->nb_streams < 1)
5021 return 0;
5022
5023 st = c->fc->streams[c->fc->nb_streams - 1];
5024 sc = st->priv_data;
5025
5026 if (sc->cenc.aes_ctr) {
5027 av_log(c->fc, AV_LOG_ERROR, "duplicate senc atom\n");
5028 return AVERROR_INVALIDDATA;
5029 }
5030
5031 avio_r8(pb); /* version */
5032 sc->cenc.use_subsamples = avio_rb24(pb) & 0x02; /* flags */
5033
5034 avio_rb32(pb); /* entries */
5035
5036 if (atom.size < 8 || atom.size > FFMIN(INT_MAX, SIZE_MAX)) {
5037 av_log(c->fc, AV_LOG_ERROR, "senc atom size %"PRId64" invalid\n", atom.size);
5038 return AVERROR_INVALIDDATA;
5039 }
5040
5041 /* save the auxiliary info as is */
5042 auxiliary_info_size = atom.size - 8;
5043
5044 sc->cenc.auxiliary_info = av_malloc(auxiliary_info_size);
5045 if (!sc->cenc.auxiliary_info) {
5046 return AVERROR(ENOMEM);
5047 }
5048
5049 sc->cenc.auxiliary_info_end = sc->cenc.auxiliary_info + auxiliary_info_size;
5050 sc->cenc.auxiliary_info_pos = sc->cenc.auxiliary_info;
5051 sc->cenc.auxiliary_info_index = 0;
5052
5053 if (avio_read(pb, sc->cenc.auxiliary_info, auxiliary_info_size) != auxiliary_info_size) {
5054 av_log(c->fc, AV_LOG_ERROR, "failed to read the auxiliary info");
5055 return AVERROR_INVALIDDATA;
5056 }
5057
5058 /* initialize the cipher */
5059 sc->cenc.aes_ctr = av_aes_ctr_alloc();
5060 if (!sc->cenc.aes_ctr) {
5061 return AVERROR(ENOMEM);
5062 }
5063
5064 return av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
5065}
5066
5067static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5068{
5069 AVStream *st;
5070 MOVStreamContext *sc;
5071 size_t data_size;
5072 int atom_header_size;
5073 int flags;
5074
5075 if (c->decryption_key_len == 0 || c->fc->nb_streams < 1)
5076 return 0;
5077
5078 st = c->fc->streams[c->fc->nb_streams - 1];
5079 sc = st->priv_data;
5080
5081 if (sc->cenc.auxiliary_info_sizes || sc->cenc.auxiliary_info_default_size) {
5082 av_log(c->fc, AV_LOG_ERROR, "duplicate saiz atom\n");
5083 return AVERROR_INVALIDDATA;
5084 }
5085
5086 atom_header_size = 9;
5087
5088 avio_r8(pb); /* version */
5089 flags = avio_rb24(pb);
5090
5091 if ((flags & 0x01) != 0) {
5092 atom_header_size += 8;
5093
5094 avio_rb32(pb); /* info type */
5095 avio_rb32(pb); /* info type param */
5096 }
5097
5098 sc->cenc.auxiliary_info_default_size = avio_r8(pb);
5099 avio_rb32(pb); /* entries */
5100
5101 if (atom.size <= atom_header_size) {
5102 return 0;
5103 }
5104
5105 if (atom.size > FFMIN(INT_MAX, SIZE_MAX)) {
5106 av_log(c->fc, AV_LOG_ERROR, "saiz atom auxiliary_info_sizes size %"PRId64" invalid\n", atom.size);
5107 return AVERROR_INVALIDDATA;
5108 }
5109
5110 /* save the auxiliary info sizes as is */
5111 data_size = atom.size - atom_header_size;
5112
5113 sc->cenc.auxiliary_info_sizes = av_malloc(data_size);
5114 if (!sc->cenc.auxiliary_info_sizes) {
5115 return AVERROR(ENOMEM);
5116 }
5117
5118 sc->cenc.auxiliary_info_sizes_count = data_size;
5119
5120 if (avio_read(pb, sc->cenc.auxiliary_info_sizes, data_size) != data_size) {
5121 av_log(c->fc, AV_LOG_ERROR, "failed to read the auxiliary info sizes");
5122 return AVERROR_INVALIDDATA;
5123 }
5124
5125 return 0;
5126}
5127
5128static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5129{
5130 AVStream *st;
5131 int last, type, size, ret;
5132 uint8_t buf[4];
5133
5134 if (c->fc->nb_streams < 1)
5135 return 0;
5136 st = c->fc->streams[c->fc->nb_streams-1];
5137
5138 if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
5139 return AVERROR_INVALIDDATA;
5140
5141 /* Check FlacSpecificBox version. */
5142 if (avio_r8(pb) != 0)
5143 return AVERROR_INVALIDDATA;
5144
5145 avio_rb24(pb); /* Flags */
5146
5147 avio_read(pb, buf, sizeof(buf));
5148 flac_parse_block_header(buf, &last, &type, &size);
5149
5150 if (type != FLAC_METADATA_TYPE_STREAMINFO || size != FLAC_STREAMINFO_SIZE) {
5151 av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
5152 return AVERROR_INVALIDDATA;
5153 }
5154
5155 ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
5156 if (ret < 0)
5157 return ret;
5158
5159 if (!last)
5160 av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
5161
5162 return 0;
5163}
5164
5165static void mov_id32_date2year(AVDictionary **m)
5166{
5167 AVDictionaryEntry *t = NULL;
5168 if (t = av_dict_get(*m, "date", t, AV_DICT_MATCH_CASE)) {
5169 av_dict_set(m, "year", t->value, 0);
5170 av_log(NULL, AV_LOG_INFO, "[%s:%d] date:%s\n", __FUNCTION__, __LINE__, t->value);
5171 }
5172}
5173static int mov_read_id32(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5174{
5175 int ret = 0;
5176 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
5177 int len = atom.size;
5178 avio_skip(pb, 6); // version+flags+pad+language
5179 len -=6 ;
5180 ff_id3v2_read(c->fc, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
5181
5182 if (id3v2_extra_meta) {
5183 if ((ret = ff_id3v2_parse_apic(c->fc, &id3v2_extra_meta)) < 0) {
5184 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
5185 return ret;
5186 }
5187 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
5188 }
5189
5190 mov_id32_date2year(&c->fc->metadata);
5191
5192 return 0;
5193}
5194static int mov_seek_auxiliary_info(MOVContext *c, MOVStreamContext *sc, int64_t index)
5195{
5196 size_t auxiliary_info_seek_offset = 0;
5197 int i;
5198
5199 if (sc->cenc.auxiliary_info_default_size) {
5200 auxiliary_info_seek_offset = (size_t)sc->cenc.auxiliary_info_default_size * index;
5201 } else if (sc->cenc.auxiliary_info_sizes) {
5202 if (index > sc->cenc.auxiliary_info_sizes_count) {
5203 av_log(c, AV_LOG_ERROR, "current sample %"PRId64" greater than the number of auxiliary info sample sizes %"SIZE_SPECIFIER"\n",
5204 index, sc->cenc.auxiliary_info_sizes_count);
5205 return AVERROR_INVALIDDATA;
5206 }
5207
5208 for (i = 0; i < index; i++) {
5209 auxiliary_info_seek_offset += sc->cenc.auxiliary_info_sizes[i];
5210 }
5211 }
5212
5213 if (auxiliary_info_seek_offset > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info) {
5214 av_log(c, AV_LOG_ERROR, "auxiliary info offset %"SIZE_SPECIFIER" greater than auxiliary info size %"SIZE_SPECIFIER"\n",
5215 auxiliary_info_seek_offset, (size_t)(sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info));
5216 return AVERROR_INVALIDDATA;
5217 }
5218
5219 sc->cenc.auxiliary_info_pos = sc->cenc.auxiliary_info + auxiliary_info_seek_offset;
5220 sc->cenc.auxiliary_info_index = index;
5221 return 0;
5222}
5223
5224static int cenc_filter(MOVContext *c, MOVStreamContext *sc, int64_t index, uint8_t *input, int size)
5225{
5226 uint32_t encrypted_bytes;
5227 uint16_t subsample_count;
5228 uint16_t clear_bytes;
5229 uint8_t* input_end = input + size;
5230 int ret;
5231
5232 if (index != sc->cenc.auxiliary_info_index) {
5233 ret = mov_seek_auxiliary_info(c, sc, index);
5234 if (ret < 0) {
5235 return ret;
5236 }
5237 }
5238
5239 /* read the iv */
5240 if (AES_CTR_IV_SIZE > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
5241 av_log(c->fc, AV_LOG_ERROR, "failed to read iv from the auxiliary info\n");
5242 return AVERROR_INVALIDDATA;
5243 }
5244
5245 av_aes_ctr_set_iv(sc->cenc.aes_ctr, sc->cenc.auxiliary_info_pos);
5246 sc->cenc.auxiliary_info_pos += AES_CTR_IV_SIZE;
5247
5248 if (!sc->cenc.use_subsamples)
5249 {
5250 /* decrypt the whole packet */
5251 av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
5252 return 0;
5253 }
5254
5255 /* read the subsample count */
5256 if (sizeof(uint16_t) > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
5257 av_log(c->fc, AV_LOG_ERROR, "failed to read subsample count from the auxiliary info\n");
5258 return AVERROR_INVALIDDATA;
5259 }
5260
5261 subsample_count = AV_RB16(sc->cenc.auxiliary_info_pos);
5262 sc->cenc.auxiliary_info_pos += sizeof(uint16_t);
5263
5264 for (; subsample_count > 0; subsample_count--)
5265 {
5266 if (6 > sc->cenc.auxiliary_info_end - sc->cenc.auxiliary_info_pos) {
5267 av_log(c->fc, AV_LOG_ERROR, "failed to read subsample from the auxiliary info\n");
5268 return AVERROR_INVALIDDATA;
5269 }
5270
5271 /* read the number of clear / encrypted bytes */
5272 clear_bytes = AV_RB16(sc->cenc.auxiliary_info_pos);
5273 sc->cenc.auxiliary_info_pos += sizeof(uint16_t);
5274 encrypted_bytes = AV_RB32(sc->cenc.auxiliary_info_pos);
5275 sc->cenc.auxiliary_info_pos += sizeof(uint32_t);
5276
5277 if ((uint64_t)clear_bytes + encrypted_bytes > input_end - input) {
5278 av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
5279 return AVERROR_INVALIDDATA;
5280 }
5281
5282 /* skip the clear bytes */
5283 input += clear_bytes;
5284
5285 /* decrypt the encrypted bytes */
5286 av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, encrypted_bytes);
5287 input += encrypted_bytes;
5288 }
5289
5290 if (input < input_end) {
5291 av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
5292 return AVERROR_INVALIDDATA;
5293 }
5294
5295 sc->cenc.auxiliary_info_index++;
5296 return 0;
5297}
5298
5299static const MOVParseTableEntry mov_default_parse_table[] = {
5300{ MKTAG('A','C','L','R'), mov_read_aclr },
5301{ MKTAG('A','P','R','G'), mov_read_avid },
5302{ MKTAG('A','A','L','P'), mov_read_avid },
5303{ MKTAG('A','R','E','S'), mov_read_ares },
5304{ MKTAG('a','v','s','s'), mov_read_avss },
5305{ MKTAG('c','h','p','l'), mov_read_chpl },
5306{ MKTAG('c','o','6','4'), mov_read_stco },
5307{ MKTAG('c','o','l','r'), mov_read_colr },
5308{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
5309{ MKTAG('d','i','n','f'), mov_read_default },
5310{ MKTAG('D','p','x','E'), mov_read_dpxe },
5311{ MKTAG('d','r','e','f'), mov_read_dref },
5312{ MKTAG('e','d','t','s'), mov_read_default },
5313{ MKTAG('e','l','s','t'), mov_read_elst },
5314{ MKTAG('e','n','d','a'), mov_read_enda },
5315{ MKTAG('f','i','e','l'), mov_read_fiel },
5316{ MKTAG('a','d','r','m'), mov_read_adrm },
5317{ MKTAG('f','t','y','p'), mov_read_ftyp },
5318{ MKTAG('g','l','b','l'), mov_read_glbl },
5319{ MKTAG('h','d','l','r'), mov_read_hdlr },
5320{ MKTAG('i','l','s','t'), mov_read_ilst },
5321{ MKTAG('j','p','2','h'), mov_read_jp2h },
5322{ MKTAG('m','d','a','t'), mov_read_mdat },
5323{ MKTAG('m','d','h','d'), mov_read_mdhd },
5324{ MKTAG('m','d','i','a'), mov_read_default },
5325{ MKTAG('m','e','t','a'), mov_read_meta },
5326{ MKTAG('m','i','n','f'), mov_read_default },
5327{ MKTAG('m','o','o','f'), mov_read_moof },
5328{ MKTAG('m','o','o','v'), mov_read_moov },
5329{ MKTAG('m','v','e','x'), mov_read_default },
5330{ MKTAG('m','v','h','d'), mov_read_mvhd },
5331{ MKTAG('S','M','I',' '), mov_read_svq3 },
5332{ MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
5333{ MKTAG('a','v','c','C'), mov_read_glbl },
5334{ MKTAG('p','a','s','p'), mov_read_pasp },
5335{ MKTAG('s','i','d','x'), mov_read_sidx },
5336{ MKTAG('s','t','b','l'), mov_read_default },
5337{ MKTAG('s','t','c','o'), mov_read_stco },
5338{ MKTAG('s','t','p','s'), mov_read_stps },
5339{ MKTAG('s','t','r','f'), mov_read_strf },
5340{ MKTAG('s','t','s','c'), mov_read_stsc },
5341{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
5342{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
5343{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
5344{ MKTAG('s','t','t','s'), mov_read_stts },
5345{ MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
5346{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
5347{ MKTAG('t','f','d','t'), mov_read_tfdt },
5348{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
5349{ MKTAG('t','r','a','k'), mov_read_trak },
5350{ MKTAG('t','r','a','f'), mov_read_default },
5351{ MKTAG('t','r','e','f'), mov_read_default },
5352{ MKTAG('t','m','c','d'), mov_read_tmcd },
5353{ MKTAG('c','h','a','p'), mov_read_chap },
5354{ MKTAG('t','r','e','x'), mov_read_trex },
5355{ MKTAG('t','r','u','n'), mov_read_trun },
5356{ MKTAG('u','d','t','a'), mov_read_default },
5357{ MKTAG('w','a','v','e'), mov_read_wave },
5358{ MKTAG('e','s','d','s'), mov_read_esds },
5359{ MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
5360{ MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
5361{ MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
5362{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
5363{ MKTAG('w','f','e','x'), mov_read_wfex },
5364{ MKTAG('c','m','o','v'), mov_read_cmov },
5365{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
5366{ MKTAG('d','v','c','1'), mov_read_dvc1 },
5367{ MKTAG('s','b','g','p'), mov_read_sbgp },
5368{ MKTAG('h','v','c','C'), mov_read_glbl },
5369{ MKTAG('u','u','i','d'), mov_read_uuid },
5370{ MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
5371{ MKTAG('f','r','e','e'), mov_read_free },
5372{ MKTAG('-','-','-','-'), mov_read_custom },
5373{ MKTAG('s','i','n','f'), mov_read_default },
5374{ MKTAG('f','r','m','a'), mov_read_frma },
5375{ MKTAG('s','e','n','c'), mov_read_senc },
5376{ MKTAG('s','a','i','z'), mov_read_saiz },
5377{ MKTAG('d','f','L','a'), mov_read_dfla },
5378{ MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
5379{ MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
5380{ MKTAG('I','D','3','2'), mov_read_id32 }, /* id32 video box */
5381{ 0, NULL }
5382};
5383
5384static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
5385{
5386 int64_t total_size = 0;
5387 MOVAtom a;
5388 int i;
5389
5390 if (c->atom_depth > 10) {
5391 av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
5392 return AVERROR_INVALIDDATA;
5393 }
5394 c->atom_depth ++;
5395
5396 if (atom.size < 0)
5397 atom.size = INT64_MAX;
5398 while (total_size + 8 <= atom.size && !avio_feof(pb)) {
5399 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
5400 a.size = atom.size;
5401 a.type=0;
5402 if (atom.size >= 8) {
5403 a.size = avio_rb32(pb);
5404 a.type = avio_rl32(pb);
5405 if (a.type == MKTAG('f','r','e','e') &&
5406 a.size >= 8 &&
5407 c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT &&
5408 c->moov_retry) {
5409 uint8_t buf[8];
5410 uint32_t *type = (uint32_t *)buf + 1;
5411 if (avio_read(pb, buf, 8) != 8)
5412 return AVERROR_INVALIDDATA;
5413 avio_seek(pb, -8, SEEK_CUR);
5414 if (*type == MKTAG('m','v','h','d') ||
5415 *type == MKTAG('c','m','o','v')) {
5416 av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
5417 a.type = MKTAG('m','o','o','v');
5418 }
5419 }
5420 if (atom.type != MKTAG('r','o','o','t') &&
5421 atom.type != MKTAG('m','o','o','v'))
5422 {
5423 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
5424 {
5425 av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
5426 avio_skip(pb, -8);
5427 c->atom_depth --;
5428 return 0;
5429 }
5430 }
5431 total_size += 8;
5432 if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
5433 a.size = avio_rb64(pb) - 8;
5434 total_size += 8;
5435 }
5436 }
5437 av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
5438 av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
5439 if (a.size == 0) {
5440 a.size = atom.size - total_size + 8;
5441 }
5442 a.size -= 8;
5443 if (a.size < 0)
5444 break;
5445 a.size = FFMIN(a.size, atom.size - total_size);
5446
5447 for (i = 0; mov_default_parse_table[i].type; i++)
5448 if (mov_default_parse_table[i].type == a.type) {
5449 parse = mov_default_parse_table[i].parse;
5450 break;
5451 }
5452
5453 // container is user data
5454 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
5455 atom.type == MKTAG('i','l','s','t')))
5456 parse = mov_read_udta_string;
5457
5458 // Supports parsing the QuickTime Metadata Keys.
5459 // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
5460 if (!parse && c->found_hdlr_mdta &&
5461 atom.type == MKTAG('m','e','t','a') &&
5462 a.type == MKTAG('k','e','y','s')) {
5463 parse = mov_read_keys;
5464 }
5465
5466 if (!parse) { /* skip leaf atoms data */
5467 avio_skip(pb, a.size);
5468 } else {
5469 int64_t start_pos = avio_tell(pb);
5470 int64_t left;
5471 int err = parse(c, pb, a);
5472 if (err < 0) {
5473 c->atom_depth --;
5474 return err;
5475 }
5476 if (c->found_moov && c->found_mdat &&
5477 ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete) ||
5478 start_pos + a.size == avio_size(pb))) {
5479 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete)
5480 c->next_root_atom = start_pos + a.size;
5481 c->atom_depth --;
5482 return 0;
5483 }
5484 left = a.size - avio_tell(pb) + start_pos;
5485 if (left > 0) /* skip garbage at atom end */
5486 avio_skip(pb, left);
5487 else if (left < 0) {
5488 av_log(c->fc, AV_LOG_WARNING,
5489 "overread end of atom '%.4s' by %"PRId64" bytes\n",
5490 (char*)&a.type, -left);
5491 avio_seek(pb, left, SEEK_CUR);
5492 }
5493 }
5494
5495 total_size += a.size;
5496 }
5497
5498 if (total_size < atom.size && atom.size < 0x7ffff)
5499 avio_skip(pb, atom.size - total_size);
5500
5501 c->atom_depth --;
5502 return 0;
5503}
5504
5505static int mov_probe(AVProbeData *p)
5506{
5507 int64_t offset;
5508 uint32_t tag;
5509 int score = 0;
5510 int moov_offset = -1;
5511
5512 /* check file header */
5513 offset = 0;
5514 for (;;) {
5515 /* ignore invalid offset */
5516 if ((offset + 8) > (unsigned int)p->buf_size)
5517 break;
5518 tag = AV_RL32(p->buf + offset + 4);
5519 switch(tag) {
5520 /* check for obvious tags */
5521 case MKTAG('m','o','o','v'):
5522 moov_offset = offset + 4;
5523 case MKTAG('m','d','a','t'):
5524 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
5525 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
5526 case MKTAG('f','t','y','p'):
5527 if (AV_RB32(p->buf+offset) < 8 &&
5528 (AV_RB32(p->buf+offset) != 1 ||
5529 offset + 12 > (unsigned int)p->buf_size ||
5530 AV_RB64(p->buf+offset + 8) == 0)) {
5531 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
5532 } else if (tag == MKTAG('f','t','y','p') &&
5533 ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
5534 || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
5535 )) {
5536 score = FFMAX(score, 5);
5537 } else {
5538 score = AVPROBE_SCORE_MAX;
5539 }
5540 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
5541 break;
5542 /* those are more common words, so rate then a bit less */
5543 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
5544 case MKTAG('w','i','d','e'):
5545 case MKTAG('f','r','e','e'):
5546 case MKTAG('j','u','n','k'):
5547 case MKTAG('p','i','c','t'):
5548 score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
5549 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
5550 break;
5551 case MKTAG(0x82,0x82,0x7f,0x7d):
5552 case MKTAG('s','k','i','p'):
5553 case MKTAG('u','u','i','d'):
5554 case MKTAG('p','r','f','l'):
5555 /* if we only find those cause probedata is too small at least rate them */
5556 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
5557 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
5558 break;
5559 default:
5560 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
5561 }
5562 }
5563 if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
5564 /* moov atom in the header - we should make sure that this is not a
5565 * MOV-packed MPEG-PS */
5566 offset = moov_offset;
5567
5568 while(offset < (p->buf_size - 16)){ /* Sufficient space */
5569 /* We found an actual hdlr atom */
5570 if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
5571 AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
5572 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
5573 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
5574 /* We found a media handler reference atom describing an
5575 * MPEG-PS-in-MOV, return a
5576 * low score to force expanding the probe window until
5577 * mpegps_probe finds what it needs */
5578 return 5;
5579 }else
5580 /* Keep looking */
5581 offset+=2;
5582 }
5583 }
5584
5585 return score;
5586}
5587
5588// must be done after parsing all trak because there's no order requirement
5589static void mov_read_chapters(AVFormatContext *s)
5590{
5591 MOVContext *mov = s->priv_data;
5592 AVStream *st;
5593 MOVStreamContext *sc;
5594 int64_t cur_pos;
5595 int i, j;
5596 int chapter_track;
5597
5598 for (j = 0; j < mov->nb_chapter_tracks; j++) {
5599 chapter_track = mov->chapter_tracks[j];
5600 st = NULL;
5601 for (i = 0; i < s->nb_streams; i++)
5602 if (s->streams[i]->id == chapter_track) {
5603 st = s->streams[i];
5604 break;
5605 }
5606 if (!st) {
5607 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
5608 continue;
5609 }
5610
5611 sc = st->priv_data;
5612 cur_pos = avio_tell(sc->pb);
5613
5614 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
5615 st->disposition |= AV_DISPOSITION_ATTACHED_PIC | AV_DISPOSITION_TIMED_THUMBNAILS;
5616 if (st->nb_index_entries) {
5617 // Retrieve the first frame, if possible
5618 AVPacket pkt;
5619 AVIndexEntry *sample = &st->index_entries[0];
5620 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
5621 av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
5622 goto finish;
5623 }
5624
5625 if (av_get_packet(sc->pb, &pkt, sample->size) < 0)
5626 goto finish;
5627
5628 st->attached_pic = pkt;
5629 st->attached_pic.stream_index = st->index;
5630 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
5631 }
5632 } else {
5633 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
5634 st->codecpar->codec_id = AV_CODEC_ID_BIN_DATA;
5635 st->discard = AVDISCARD_ALL;
5636 for (i = 0; i < st->nb_index_entries; i++) {
5637 AVIndexEntry *sample = &st->index_entries[i];
5638 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
5639 uint8_t *title;
5640 uint16_t ch;
5641 int len, title_len;
5642
5643 if (end < sample->timestamp) {
5644 av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
5645 end = AV_NOPTS_VALUE;
5646 }
5647
5648 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
5649 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
5650 goto finish;
5651 }
5652
5653 // the first two bytes are the length of the title
5654 len = avio_rb16(sc->pb);
5655 if (len > sample->size-2)
5656 continue;
5657 title_len = 2*len + 1;
5658 if (!(title = av_mallocz(title_len)))
5659 goto finish;
5660
5661 // The samples could theoretically be in any encoding if there's an encd
5662 // atom following, but in practice are only utf-8 or utf-16, distinguished
5663 // instead by the presence of a BOM
5664 if (!len) {
5665 title[0] = 0;
5666 } else {
5667 ch = avio_rb16(sc->pb);
5668 if (ch == 0xfeff)
5669 avio_get_str16be(sc->pb, len, title, title_len);
5670 else if (ch == 0xfffe)
5671 avio_get_str16le(sc->pb, len, title, title_len);
5672 else {
5673 AV_WB16(title, ch);
5674 if (len == 1 || len == 2)
5675 title[len] = 0;
5676 else
5677 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
5678 }
5679 }
5680
5681 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
5682 av_freep(&title);
5683 }
5684 }
5685finish:
5686 avio_seek(sc->pb, cur_pos, SEEK_SET);
5687 }
5688}
5689
5690static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
5691 uint32_t value, int flags)
5692{
5693 AVTimecode tc;
5694 char buf[AV_TIMECODE_STR_SIZE];
5695 AVRational rate = st->avg_frame_rate;
5696 int ret = av_timecode_init(&tc, rate, flags, 0, s);
5697 if (ret < 0)
5698 return ret;
5699 av_dict_set(&st->metadata, "timecode",
5700 av_timecode_make_string(&tc, buf, value), 0);
5701 return 0;
5702}
5703
5704static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
5705{
5706 MOVStreamContext *sc = st->priv_data;
5707 char buf[AV_TIMECODE_STR_SIZE];
5708 int64_t cur_pos = avio_tell(sc->pb);
5709 int hh, mm, ss, ff, drop;
5710
5711 if (!st->nb_index_entries)
5712 return -1;
5713
5714 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
5715 avio_skip(s->pb, 13);
5716 hh = avio_r8(s->pb);
5717 mm = avio_r8(s->pb);
5718 ss = avio_r8(s->pb);
5719 drop = avio_r8(s->pb);
5720 ff = avio_r8(s->pb);
5721 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
5722 hh, mm, ss, drop ? ';' : ':', ff);
5723 av_dict_set(&st->metadata, "timecode", buf, 0);
5724
5725 avio_seek(sc->pb, cur_pos, SEEK_SET);
5726 return 0;
5727}
5728
5729static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
5730{
5731 MOVStreamContext *sc = st->priv_data;
5732 int flags = 0;
5733 int64_t cur_pos = avio_tell(sc->pb);
5734 uint32_t value;
5735
5736 if (!st->nb_index_entries)
5737 return -1;
5738
5739 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
5740 value = avio_rb32(s->pb);
5741
5742 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
5743 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
5744 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
5745
5746 /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
5747 * not the case) and thus assume "frame number format" instead of QT one.
5748 * No sample with tmcd track can be found with a QT timecode at the moment,
5749 * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
5750 * format). */
5751 parse_timecode_in_framenum_format(s, st, value, flags);
5752
5753 avio_seek(sc->pb, cur_pos, SEEK_SET);
5754 return 0;
5755}
5756
5757static int mov_read_close(AVFormatContext *s)
5758{
5759 MOVContext *mov = s->priv_data;
5760 int i, j;
5761
5762 for (i = 0; i < s->nb_streams; i++) {
5763 AVStream *st = s->streams[i];
5764 MOVStreamContext *sc = st->priv_data;
5765
5766 if (!sc)
5767 continue;
5768
5769 av_freep(&sc->ctts_data);
5770 for (j = 0; j < sc->drefs_count; j++) {
5771 av_freep(&sc->drefs[j].path);
5772 av_freep(&sc->drefs[j].dir);
5773 }
5774 av_freep(&sc->drefs);
5775
5776 sc->drefs_count = 0;
5777
5778 if (!sc->pb_is_copied)
5779 ff_format_io_close(s, &sc->pb);
5780
5781 sc->pb = NULL;
5782 av_freep(&sc->chunk_offsets);
5783 av_freep(&sc->stsc_data);
5784 av_freep(&sc->sample_sizes);
5785 av_freep(&sc->keyframes);
5786 av_freep(&sc->stts_data);
5787 av_freep(&sc->stps_data);
5788 av_freep(&sc->elst_data);
5789 av_freep(&sc->rap_group);
5790 av_freep(&sc->display_matrix);
5791 av_freep(&sc->index_ranges);
5792
5793 if (sc->extradata)
5794 for (j = 0; j < sc->stsd_count; j++)
5795 av_free(sc->extradata[j]);
5796 av_freep(&sc->extradata);
5797 av_freep(&sc->extradata_size);
5798
5799 av_freep(&sc->cenc.auxiliary_info);
5800 av_freep(&sc->cenc.auxiliary_info_sizes);
5801 av_aes_ctr_free(sc->cenc.aes_ctr);
5802
5803 av_freep(&sc->stereo3d);
5804 av_freep(&sc->spherical);
5805 }
5806
5807 if (mov->dv_demux) {
5808 avformat_free_context(mov->dv_fctx);
5809 mov->dv_fctx = NULL;
5810 }
5811
5812 if (mov->meta_keys) {
5813 for (i = 1; i < mov->meta_keys_count; i++) {
5814 av_freep(&mov->meta_keys[i]);
5815 }
5816 av_freep(&mov->meta_keys);
5817 }
5818
5819 av_freep(&mov->trex_data);
5820 av_freep(&mov->bitrates);
5821
5822 for (i = 0; i < mov->fragment_index_count; i++) {
5823 MOVFragmentIndex* index = mov->fragment_index_data[i];
5824 av_freep(&index->items);
5825 av_freep(&mov->fragment_index_data[i]);
5826 }
5827 av_freep(&mov->fragment_index_data);
5828
5829 av_freep(&mov->aes_decrypt);
5830 av_freep(&mov->chapter_tracks);
5831
5832 return 0;
5833}
5834
5835static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
5836{
5837 int i;
5838
5839 for (i = 0; i < s->nb_streams; i++) {
5840 AVStream *st = s->streams[i];
5841 MOVStreamContext *sc = st->priv_data;
5842
5843 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
5844 sc->timecode_track == tmcd_id)
5845 return 1;
5846 }
5847 return 0;
5848}
5849
5850/* look for a tmcd track not referenced by any video track, and export it globally */
5851static void export_orphan_timecode(AVFormatContext *s)
5852{
5853 int i;
5854
5855 for (i = 0; i < s->nb_streams; i++) {
5856 AVStream *st = s->streams[i];
5857
5858 if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
5859 !tmcd_is_referenced(s, i + 1)) {
5860 AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
5861 if (tcr) {
5862 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
5863 break;
5864 }
5865 }
5866 }
5867}
5868
5869static int read_tfra(MOVContext *mov, AVIOContext *f)
5870{
5871 MOVFragmentIndex* index = NULL;
5872 int version, fieldlength, i, j;
5873 int64_t pos = avio_tell(f);
5874 uint32_t size = avio_rb32(f);
5875 void *tmp;
5876
5877 if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
5878 return 1;
5879 }
5880 av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
5881 index = av_mallocz(sizeof(MOVFragmentIndex));
5882 if (!index) {
5883 return AVERROR(ENOMEM);
5884 }
5885
5886 tmp = av_realloc_array(mov->fragment_index_data,
5887 mov->fragment_index_count + 1,
5888 sizeof(MOVFragmentIndex*));
5889 if (!tmp) {
5890 av_freep(&index);
5891 return AVERROR(ENOMEM);
5892 }
5893 mov->fragment_index_data = tmp;
5894 mov->fragment_index_data[mov->fragment_index_count++] = index;
5895
5896 version = avio_r8(f);
5897 avio_rb24(f);
5898 index->track_id = avio_rb32(f);
5899 fieldlength = avio_rb32(f);
5900 index->item_count = avio_rb32(f);
5901 index->items = av_mallocz_array(
5902 index->item_count, sizeof(MOVFragmentIndexItem));
5903 if (!index->items) {
5904 index->item_count = 0;
5905 return AVERROR(ENOMEM);
5906 }
5907 for (i = 0; i < index->item_count; i++) {
5908 int64_t time, offset;
5909 if (version == 1) {
5910 time = avio_rb64(f);
5911 offset = avio_rb64(f);
5912 } else {
5913 time = avio_rb32(f);
5914 offset = avio_rb32(f);
5915 }
5916 index->items[i].time = time;
5917 index->items[i].moof_offset = offset;
5918 for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
5919 avio_r8(f);
5920 for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
5921 avio_r8(f);
5922 for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
5923 avio_r8(f);
5924 }
5925
5926 avio_seek(f, pos + size, SEEK_SET);
5927 return 0;
5928}
5929
5930static int mov_read_mfra(MOVContext *c, AVIOContext *f)
5931{
5932 int64_t stream_size = avio_size(f);
5933 int64_t original_pos = avio_tell(f);
5934 int64_t seek_ret;
5935 int32_t mfra_size;
5936 int ret = -1;
5937 if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
5938 ret = seek_ret;
5939 goto fail;
5940 }
5941 mfra_size = avio_rb32(f);
5942 if (mfra_size < 0 || mfra_size > stream_size) {
5943 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
5944 goto fail;
5945 }
5946 if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
5947 ret = seek_ret;
5948 goto fail;
5949 }
5950 if (avio_rb32(f) != mfra_size) {
5951 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
5952 goto fail;
5953 }
5954 if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
5955 av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
5956 goto fail;
5957 }
5958 av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
5959 do {
5960 ret = read_tfra(c, f);
5961 if (ret < 0)
5962 goto fail;
5963 } while (!ret);
5964 ret = 0;
5965fail:
5966 seek_ret = avio_seek(f, original_pos, SEEK_SET);
5967 if (seek_ret < 0) {
5968 av_log(c->fc, AV_LOG_ERROR,
5969 "failed to seek back after looking for mfra\n");
5970 ret = seek_ret;
5971 }
5972 return ret;
5973}
5974
5975static int mov_read_header(AVFormatContext *s)
5976{
5977 MOVContext *mov = s->priv_data;
5978 AVIOContext *pb = s->pb;
5979 int j, err;
5980 MOVAtom atom = { AV_RL32("root") };
5981 int i;
5982
5983 if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
5984 av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
5985 mov->decryption_key_len, AES_CTR_KEY_SIZE);
5986 return AVERROR(EINVAL);
5987 }
5988
5989 mov->fc = s;
5990 mov->trak_index = -1;
5991 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
5992 if (pb->seekable & AVIO_SEEKABLE_NORMAL)
5993 atom.size = avio_size(pb);
5994 else
5995 atom.size = INT64_MAX;
5996
5997 /* check MOV header */
5998 do {
5999 if (mov->moov_retry)
6000 avio_seek(pb, 0, SEEK_SET);
6001 if ((err = mov_read_default(mov, pb, atom)) < 0) {
6002 av_log(s, AV_LOG_ERROR, "error reading header\n");
6003 mov_read_close(s);
6004 return err;
6005 }
6006 } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mov->found_moov && !mov->moov_retry++);
6007 if (!mov->found_moov) {
6008 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
6009 mov_read_close(s);
6010 return AVERROR_INVALIDDATA;
6011 }
6012 av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
6013
6014 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
6015 if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
6016 mov_read_chapters(s);
6017 for (i = 0; i < s->nb_streams; i++)
6018 if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
6019 mov_read_timecode_track(s, s->streams[i]);
6020 } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
6021 mov_read_rtmd_track(s, s->streams[i]);
6022 }
6023 }
6024
6025 /* copy timecode metadata from tmcd tracks to the related video streams */
6026 for (i = 0; i < s->nb_streams; i++) {
6027 AVStream *st = s->streams[i];
6028 MOVStreamContext *sc = st->priv_data;
6029 if (sc->timecode_track > 0) {
6030 AVDictionaryEntry *tcr;
6031 int tmcd_st_id = -1;
6032
6033 for (j = 0; j < s->nb_streams; j++)
6034 if (s->streams[j]->id == sc->timecode_track)
6035 tmcd_st_id = j;
6036
6037 if (tmcd_st_id < 0 || tmcd_st_id == i)
6038 continue;
6039 tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
6040 if (tcr)
6041 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
6042 }
6043 }
6044 export_orphan_timecode(s);
6045
6046 for (i = 0; i < s->nb_streams; i++) {
6047 AVStream *st = s->streams[i];
6048 MOVStreamContext *sc = st->priv_data;
6049 fix_timescale(mov, sc);
6050 if(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->codec_id == AV_CODEC_ID_AAC) {
6051 st->skip_samples = sc->start_pad;
6052 }
6053 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
6054 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
6055 sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
6056 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
6057 if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
6058 st->codecpar->width = sc->width;
6059 st->codecpar->height = sc->height;
6060 }
6061 if (st->codecpar->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
6062 if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
6063 return err;
6064 }
6065 }
6066 if (mov->handbrake_version &&
6067 mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
6068 st->codecpar->codec_id == AV_CODEC_ID_MP3
6069 ) {
6070 av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
6071 st->need_parsing = AVSTREAM_PARSE_FULL;
6072 }
6073 }
6074
6075 if (mov->trex_data) {
6076 for (i = 0; i < s->nb_streams; i++) {
6077 AVStream *st = s->streams[i];
6078 MOVStreamContext *sc = st->priv_data;
6079 if (st->duration > 0) {
6080 if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
6081 av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
6082 sc->data_size, sc->time_scale);
6083 mov_read_close(s);
6084 return AVERROR_INVALIDDATA;
6085 }
6086 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
6087 }
6088 }
6089 }
6090
6091 if (mov->use_mfra_for > 0) {
6092 for (i = 0; i < s->nb_streams; i++) {
6093 AVStream *st = s->streams[i];
6094 MOVStreamContext *sc = st->priv_data;
6095 if (sc->duration_for_fps > 0) {
6096 if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
6097 av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
6098 sc->data_size, sc->time_scale);
6099 mov_read_close(s);
6100 return AVERROR_INVALIDDATA;
6101 }
6102 st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale /
6103 sc->duration_for_fps;
6104 }
6105 }
6106 }
6107
6108 for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
6109 if (mov->bitrates[i]) {
6110 s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
6111 }
6112 }
6113
6114 ff_rfps_calculate(s);
6115
6116 for (i = 0; i < s->nb_streams; i++) {
6117 AVStream *st = s->streams[i];
6118 MOVStreamContext *sc = st->priv_data;
6119
6120 switch (st->codecpar->codec_type) {
6121 case AVMEDIA_TYPE_AUDIO:
6122 err = ff_replaygain_export(st, s->metadata);
6123 if (err < 0) {
6124 mov_read_close(s);
6125 return err;
6126 }
6127 break;
6128 case AVMEDIA_TYPE_VIDEO:
6129 if (sc->display_matrix) {
6130 err = av_stream_add_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, (uint8_t*)sc->display_matrix,
6131 sizeof(int32_t) * 9);
6132 if (err < 0)
6133 return err;
6134
6135 sc->display_matrix = NULL;
6136 }
6137 if (sc->stereo3d) {
6138 err = av_stream_add_side_data(st, AV_PKT_DATA_STEREO3D,
6139 (uint8_t *)sc->stereo3d,
6140 sizeof(*sc->stereo3d));
6141 if (err < 0)
6142 return err;
6143
6144 sc->stereo3d = NULL;
6145 }
6146 if (sc->spherical) {
6147 err = av_stream_add_side_data(st, AV_PKT_DATA_SPHERICAL,
6148 (uint8_t *)sc->spherical,
6149 sc->spherical_size);
6150 if (err < 0)
6151 return err;
6152
6153 sc->spherical = NULL;
6154 }
6155 break;
6156 }
6157 }
6158 ff_configure_buffers_for_index(s, AV_TIME_BASE);
6159
6160 return 0;
6161}
6162
6163static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
6164{
6165 AVIndexEntry *sample = NULL;
6166 int64_t best_dts = INT64_MAX;
6167 int i;
6168 for (i = 0; i < s->nb_streams; i++) {
6169 AVStream *avst = s->streams[i];
6170 MOVStreamContext *msc = avst->priv_data;
6171 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
6172 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
6173 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
6174 av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
6175 if (!sample || (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && current_sample->pos < sample->pos) ||
6176 ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
6177 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
6178 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
6179 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
6180 sample = current_sample;
6181 best_dts = dts;
6182 *st = avst;
6183 }
6184 }
6185 }
6186 return sample;
6187}
6188
6189static int should_retry(AVIOContext *pb, int error_code) {
6190 if (error_code == AVERROR_EOF || avio_feof(pb))
6191 return 0;
6192
6193 return 1;
6194}
6195
6196static int mov_switch_root(AVFormatContext *s, int64_t target)
6197{
6198 MOVContext *mov = s->priv_data;
6199 int i, j;
6200 int already_read = 0;
6201
6202 if (avio_seek(s->pb, target, SEEK_SET) != target) {
6203 av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
6204 return AVERROR_INVALIDDATA;
6205 }
6206
6207 mov->next_root_atom = 0;
6208
6209 for (i = 0; i < mov->fragment_index_count; i++) {
6210 MOVFragmentIndex *index = mov->fragment_index_data[i];
6211 int found = 0;
6212 for (j = 0; j < index->item_count; j++) {
6213 MOVFragmentIndexItem *item = &index->items[j];
6214 if (found) {
6215 mov->next_root_atom = item->moof_offset;
6216 break; // Advance to next index in outer loop
6217 } else if (item->moof_offset == target) {
6218 index->current_item = FFMIN(j, index->current_item);
6219 if (item->headers_read)
6220 already_read = 1;
6221 item->headers_read = 1;
6222 found = 1;
6223 }
6224 }
6225 if (!found)
6226 index->current_item = 0;
6227 }
6228
6229 if (already_read)
6230 return 0;
6231
6232 mov->found_mdat = 0;
6233
6234 if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
6235 avio_feof(s->pb))
6236 return AVERROR_EOF;
6237 av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
6238
6239 return 1;
6240}
6241
6242static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
6243{
6244 uint8_t *side, *extradata;
6245 int extradata_size;
6246
6247 /* Save the current index. */
6248 sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
6249
6250 /* Notify the decoder that extradata changed. */
6251 extradata_size = sc->extradata_size[sc->last_stsd_index];
6252 extradata = sc->extradata[sc->last_stsd_index];
6253 if (extradata_size > 0 && extradata) {
6254 side = av_packet_new_side_data(pkt,
6255 AV_PKT_DATA_NEW_EXTRADATA,
6256 extradata_size);
6257 if (!side)
6258 return AVERROR(ENOMEM);
6259 memcpy(side, extradata, extradata_size);
6260 }
6261
6262 return 0;
6263}
6264
6265static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
6266{
6267 MOVContext *mov = s->priv_data;
6268 MOVStreamContext *sc;
6269 AVIndexEntry *sample;
6270 AVStream *st = NULL;
6271 int64_t current_index;
6272 int ret;
6273 mov->fc = s;
6274 retry:
6275 sample = mov_find_next_sample(s, &st);
6276 if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
6277 if (!mov->next_root_atom)
6278 return AVERROR_EOF;
6279 if ((ret = mov_switch_root(s, mov->next_root_atom)) < 0)
6280 return ret;
6281 goto retry;
6282 }
6283 sc = st->priv_data;
6284 /* must be done just before reading, to avoid infinite loop on sample */
6285 current_index = sc->current_index;
6286 mov_current_sample_inc(sc);
6287
6288 if (mov->next_root_atom) {
6289 sample->pos = FFMIN(sample->pos, mov->next_root_atom);
6290 sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
6291 }
6292
6293 if (st->discard != AVDISCARD_ALL) {
6294 int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
6295 if (ret64 != sample->pos) {
6296 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
6297 sc->ffindex, sample->pos);
6298 if (should_retry(sc->pb, ret64)) {
6299 mov_current_sample_dec(sc);
6300 }
6301 return AVERROR_INVALIDDATA;
6302 }
6303
6304 if( st->discard == AVDISCARD_NONKEY && 0==(sample->flags & AVINDEX_KEYFRAME) ) {
6305 av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
6306 goto retry;
6307 }
6308
6309 ret = av_get_packet(sc->pb, pkt, sample->size);
6310 if (ret < 0) {
6311 if (should_retry(sc->pb, ret)) {
6312 mov_current_sample_dec(sc);
6313 }
6314 return ret;
6315 }
6316 if (sc->has_palette) {
6317 uint8_t *pal;
6318
6319 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
6320 if (!pal) {
6321 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
6322 } else {
6323 memcpy(pal, sc->palette, AVPALETTE_SIZE);
6324 sc->has_palette = 0;
6325 }
6326 }
6327#if CONFIG_DV_DEMUXER
6328 if (mov->dv_demux && sc->dv_audio_container) {
6329 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
6330 av_freep(&pkt->data);
6331 pkt->size = 0;
6332 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
6333 if (ret < 0)
6334 return ret;
6335 }
6336#endif
6337 if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !st->need_parsing && pkt->size > 4) {
6338 if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
6339 st->need_parsing = AVSTREAM_PARSE_FULL;
6340 }
6341 }
6342
6343 pkt->stream_index = sc->ffindex;
6344 pkt->dts = sample->timestamp;
6345 if (sample->flags & AVINDEX_DISCARD_FRAME) {
6346 pkt->flags |= AV_PKT_FLAG_DISCARD;
6347 }
6348 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
6349 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
6350 /* update ctts context */
6351 sc->ctts_sample++;
6352 if (sc->ctts_index < sc->ctts_count &&
6353 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
6354 sc->ctts_index++;
6355 sc->ctts_sample = 0;
6356 }
6357 } else {
6358 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
6359 st->index_entries[sc->current_sample].timestamp : st->duration;
6360 pkt->duration = next_dts - pkt->dts;
6361 pkt->pts = pkt->dts;
6362 }
6363 if (st->discard == AVDISCARD_ALL)
6364 goto retry;
6365 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
6366 pkt->pos = sample->pos;
6367
6368 /* Multiple stsd handling. */
6369 if (sc->stsc_data) {
6370 /* Keep track of the stsc index for the given sample, then check
6371 * if the stsd index is different from the last used one. */
6372 sc->stsc_sample++;
6373 if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
6374 mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
6375 sc->stsc_index++;
6376 sc->stsc_sample = 0;
6377 /* Do not check indexes after a switch. */
6378 } else if (sc->stsc_data[sc->stsc_index].id > 0 &&
6379 sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
6380 sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
6381 ret = mov_change_extradata(sc, pkt);
6382 if (ret < 0)
6383 return ret;
6384 }
6385 }
6386
6387 if (mov->aax_mode)
6388 aax_filter(pkt->data, pkt->size, mov);
6389
6390 if (sc->cenc.aes_ctr) {
6391 ret = cenc_filter(mov, sc, current_index, pkt->data, pkt->size);
6392 if (ret) {
6393 return ret;
6394 }
6395 }
6396
6397 return 0;
6398}
6399
6400static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
6401{
6402 MOVContext *mov = s->priv_data;
6403 MOVStreamContext *sc = st->priv_data;
6404 int i, j;
6405
6406 if (!mov->fragment_index_complete)
6407 return 0;
6408
6409 for (i = 0; i < mov->fragment_index_count; i++) {
6410 if (mov->fragment_index_data[i]->track_id == st->id || !sc->has_sidx) {
6411 MOVFragmentIndex *index = mov->fragment_index_data[i];
6412 for (j = index->item_count - 1; j >= 0; j--) {
6413 if (index->items[j].time <= timestamp) {
6414 if (index->items[j].headers_read)
6415 return 0;
6416
6417 return mov_switch_root(s, index->items[j].moof_offset);
6418 }
6419 }
6420 }
6421 }
6422
6423 return 0;
6424}
6425
6426static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
6427{
6428 MOVStreamContext *sc = st->priv_data;
6429 int sample, time_sample;
6430 int i;
6431
6432 int ret = mov_seek_fragment(s, st, timestamp);
6433 if (ret < 0)
6434 return ret;
6435
6436 sample = av_index_search_timestamp(st, timestamp, flags);
6437 av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
6438 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
6439 sample = 0;
6440 if (sample < 0) /* not sure what to do */
6441 return AVERROR_INVALIDDATA;
6442 mov_current_sample_set(sc, sample);
6443 av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
6444 /* adjust ctts index */
6445 if (sc->ctts_data) {
6446 time_sample = 0;
6447 for (i = 0; i < sc->ctts_count; i++) {
6448 int next = time_sample + sc->ctts_data[i].count;
6449 if (next > sc->current_sample) {
6450 sc->ctts_index = i;
6451 sc->ctts_sample = sc->current_sample - time_sample;
6452 break;
6453 }
6454 time_sample = next;
6455 }
6456 }
6457
6458 /* adjust stsd index */
6459 time_sample = 0;
6460 for (i = 0; i < sc->stsc_count; i++) {
6461 int next = time_sample + mov_get_stsc_samples(sc, i);
6462 if (next > sc->current_sample) {
6463 sc->stsc_index = i;
6464 sc->stsc_sample = sc->current_sample - time_sample;
6465 break;
6466 }
6467 time_sample = next;
6468 }
6469
6470 return sample;
6471}
6472
6473static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
6474{
6475 MOVContext *mc = s->priv_data;
6476 AVStream *st;
6477 int sample;
6478 int i;
6479
6480 if (stream_index >= s->nb_streams)
6481 return AVERROR_INVALIDDATA;
6482
6483 st = s->streams[stream_index];
6484 sample = mov_seek_stream(s, st, sample_time, flags);
6485 if (sample < 0)
6486 return sample;
6487
6488reseek:
6489 if (mc->seek_individually) {
6490 /* adjust seek timestamp to found sample timestamp */
6491 int64_t seek_timestamp = st->index_entries[sample].timestamp;
6492
6493 for (i = 0; i < s->nb_streams; i++) {
6494 int64_t timestamp;
6495 MOVStreamContext *sc = s->streams[i]->priv_data;
6496 st = s->streams[i];
6497 st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
6498
6499 if (stream_index == i)
6500 continue;
6501
6502 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
6503 if (mov_seek_stream(s, st, timestamp, flags) < 0) {
6504 mc->seek_individually = 0;
6505 goto reseek;
6506 }
6507 }
6508 } else {
6509 for (i = 0; i < s->nb_streams; i++) {
6510 MOVStreamContext *sc;
6511 st = s->streams[i];
6512 sc = st->priv_data;
6513 mov_current_sample_set(sc, 0);
6514 }
6515 while (1) {
6516 MOVStreamContext *sc;
6517 AVIndexEntry *entry = mov_find_next_sample(s, &st);
6518 if (!entry)
6519 return AVERROR_INVALIDDATA;
6520 sc = st->priv_data;
6521 if (sc->ffindex == stream_index && sc->current_sample == sample)
6522 break;
6523 mov_current_sample_inc(sc);
6524 }
6525 }
6526 return 0;
6527}
6528
6529#define OFFSET(x) offsetof(MOVContext, x)
6530#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
6531static const AVOption mov_options[] = {
6532 {"use_absolute_path",
6533 "allow using absolute path when opening alias, this is a possible security issue",
6534 OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
6535 0, 1, FLAGS},
6536 {"seek_streams_individually",
6537 "Seek each stream individually to the to the closest point",
6538 OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
6539 0, 1, FLAGS},
6540 {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
6541 0, 1, FLAGS},
6542 {"advanced_editlist",
6543 "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
6544 OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
6545 0, 1, FLAGS},
6546 {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
6547 0, 1, FLAGS},
6548 {"use_mfra_for",
6549 "use mfra for fragment timestamps",
6550 OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
6551 -1, FF_MOV_FLAG_MFRA_PTS, FLAGS,
6552 "use_mfra_for"},
6553 {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
6554 FLAGS, "use_mfra_for" },
6555 {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
6556 FLAGS, "use_mfra_for" },
6557 {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
6558 FLAGS, "use_mfra_for" },
6559 { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
6560 AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
6561 { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
6562 AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
6563 { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
6564 AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
6565 { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
6566 "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
6567 AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
6568 .flags = AV_OPT_FLAG_DECODING_PARAM },
6569 { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
6570 { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
6571 {.i64 = 0}, 0, 1, FLAGS },
6572
6573 { NULL },
6574};
6575
6576static const AVClass mov_class = {
6577 .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
6578 .item_name = av_default_item_name,
6579 .option = mov_options,
6580 .version = LIBAVUTIL_VERSION_INT,
6581};
6582
6583AVInputFormat ff_mov_demuxer = {
6584 .name = "mov,mp4,m4a,3gp,3g2,mj2",
6585 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
6586 .priv_class = &mov_class,
6587 .priv_data_size = sizeof(MOVContext),
6588 .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
6589 .read_probe = mov_probe,
6590 .read_header = mov_read_header,
6591 .read_packet = mov_read_packet,
6592 .read_close = mov_read_close,
6593 .read_seek = mov_read_seek,
6594 .flags = AVFMT_NO_BYTE_SEEK,
6595};
6596