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