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