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