summaryrefslogtreecommitdiff
path: root/libavformat/mov.c (plain)
blob: 041d8ad4a57f9e62e876e8838129e8e2b6042d07
1/*
2 * MOV demuxer
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5 *
6 * first version by Francois Revol <revol@free.fr>
7 * seek function by Gael Chardon <gael.dev@4now.net>
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <limits.h>
27
28//#define MOV_EXPORT_ALL_METADATA
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/avstring.h"
36#include "libavutil/dict.h"
37#include "libavutil/opt.h"
38#include "libavutil/timecode.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 "seek.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#include "id3v2.h"
61/* those functions parse an atom */
62/* links atom IDs to parse functions */
63typedef struct MOVParseTableEntry {
64 uint32_t type;
65 int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
66} MOVParseTableEntry;
67
68static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
69
70static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
71 unsigned len, const char *key)
72{
73 char buf[16];
74
75 short current, total = 0;
76 avio_rb16(pb); // unknown
77 current = avio_rb16(pb);
78 if (len >= 6)
79 total = avio_rb16(pb);
80 if (!total)
81 snprintf(buf, sizeof(buf), "%d", current);
82 else
83 snprintf(buf, sizeof(buf), "%d/%d", current, total);
84 av_dict_set(&c->fc->metadata, key, buf, 0);
85
86 return 0;
87}
88
89static 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 av_dict_set(&c->fc->metadata, key, buf, 0);
101
102 return 0;
103}
104
105static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
106 unsigned len, const char *key)
107{
108 char buf[16];
109
110 snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
111 av_dict_set(&c->fc->metadata, key, buf, 0);
112
113 return 0;
114}
115
116static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
117 unsigned len, const char *key)
118{
119 short genre;
120 char buf[20];
121
122 avio_r8(pb); // unknown
123
124 genre = avio_r8(pb);
125 if (genre < 1 || genre > ID3v1_GENRE_MAX)
126 return 0;
127 snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
128 av_dict_set(&c->fc->metadata, key, buf, 0);
129
130 return 0;
131}
132
133static int mov_read_custom_metadata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
134{
135 char key[1024]={0}, data[1024]={0};
136 int i;
137 AVStream *st;
138 MOVStreamContext *sc;
139
140 if (c->fc->nb_streams < 1)
141 return 0;
142 st = c->fc->streams[c->fc->nb_streams-1];
143 sc = st->priv_data;
144
145 if (atom.size <= 8) return 0;
146
147 for (i = 0; i < 3; i++) { // Parse up to three sub-atoms looking for name and data.
148 int data_size = avio_rb32(pb);
149 int tag = avio_rl32(pb);
150 int str_size = 0, skip_size = 0;
151 char *target = NULL;
152
153 switch (tag) {
154 case MKTAG('n','a','m','e'):
155 avio_rb32(pb); // version/flags
156 str_size = skip_size = data_size - 12;
157 atom.size -= 12;
158 target = key;
159 break;
160 case MKTAG('d','a','t','a'):
161 avio_rb32(pb); // version/flags
162 avio_rb32(pb); // reserved (zero)
163 str_size = skip_size = data_size - 16;
164 atom.size -= 16;
165 target = data;
166 break;
167 default:
168 skip_size = data_size - 8;
169 str_size = 0;
170 break;
171 }
172
173 if (target) {
174 str_size = FFMIN3(sizeof(data)-1, str_size, atom.size);
175 avio_read(pb, target, str_size);
176 target[str_size] = 0;
177 }
178 atom.size -= skip_size;
179
180 // If we didn't read the full data chunk for the sub-atom, skip to the end of it.
181 if (skip_size > str_size) avio_skip(pb, skip_size - str_size);
182 }
183
184 if (*key && *data) {
185 if (strcmp(key, "iTunSMPB") == 0) {
186 av_dict_set(&st->metadata, key, data, 0);
187 int priming, remainder, samples;
188 if(sscanf(data, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
189 if(priming>0 && priming<16384)
190 sc->start_pad = priming;
191 return 1;
192 }
193 }
194 if (strcmp(key, "cdec") == 0) {
195// av_dict_set(&st->metadata, key, data, 0);
196 return 1;
197 }
198 }
199 return 0;
200}
201
202static const uint32_t mac_to_unicode[128] = {
203 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
204 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
205 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
206 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
207 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
208 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
209 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
210 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
211 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
212 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
213 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
214 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
215 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
216 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
217 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
218 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
219};
220
221static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
222 char *dst, int dstlen)
223{
224 char *p = dst;
225 char *end = dst+dstlen-1;
226 int i;
227
228 for (i = 0; i < len; i++) {
229 uint8_t t, c = avio_r8(pb);
230 if (c < 0x80 && p < end)
231 *p++ = c;
232 else if (p < end)
233 PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
234 }
235 *p = 0;
236 return p - dst;
237}
238
239static int mov_extract_cover_pic(AVFormatContext *s, AVIOContext *pb, int type, int size, char *value)
240{
241 if(s->cover_data){
242 av_log(s, AV_LOG_INFO, "Extract cover picture in other atom!\n");
243 return 0;
244 }
245
246 s->cover_data = av_malloc(size);
247 if(!s->cover_data){
248 av_log(s, AV_LOG_INFO, "no memery, av_alloc failed!\n");
249 return -1;
250 }
251 s->cover_data_len = size;
252 avio_read(pb, s->cover_data, size);
253
254 if (type == 13)
255 strcpy(value, "image/jpeg"); // jpeg
256 else if (type == 14)
257 strcpy(value, "image/png"); // png
258
259 return 0;
260}
261
262static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
263{
264 AVPacket pkt;
265 AVStream *st;
266 MOVStreamContext *sc;
267 enum AVCodecID id;
268 int ret;
269
270 switch (type) {
271 case 0xd: id = AV_CODEC_ID_MJPEG; break;
272 case 0xe: id = AV_CODEC_ID_PNG; break;
273 case 0x1b: id = AV_CODEC_ID_BMP; break;
274 default:
275 av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
276 avio_skip(pb, len);
277 return 0;
278 }
279
280 st = avformat_new_stream(c->fc, NULL);
281 if (!st)
282 return AVERROR(ENOMEM);
283 sc = av_mallocz(sizeof(*sc));
284 if (!sc)
285 return AVERROR(ENOMEM);
286 st->priv_data = sc;
287
288 ret = av_get_packet(pb, &pkt, len);
289 if (ret < 0)
290 return ret;
291
292 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
293
294 st->attached_pic = pkt;
295 st->attached_pic.stream_index = st->index;
296 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
297
298 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
299 st->codec->codec_id = id;
300
301 return 0;
302}
303
304static int mov_metadata_raw(MOVContext *c, AVIOContext *pb,
305 unsigned len, const char *key)
306{
307 char *value = av_malloc(len + 1);
308 if (!value)
309 return AVERROR(ENOMEM);
310 avio_read(pb, value, len);
311 value[len] = 0;
312 return av_dict_set(&c->fc->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
313}
314
315static int mov_metadata_3gpp_general(MOVContext *c, AVIOContext *pb,
316 unsigned len, const char *key)
317{
318 char key2[16];
319 uint8_t version;
320 uint32_t flags;
321 uint8_t pad;
322 uint16_t langcode;
323 uint16_t shortbytes;
324 char language[4] = {0};
325 char byte = 0;
326 char str[128] = {0};
327 int offset = 0;
328 uint16_t byteOrderMark = 0;
329
330 version = avio_r8(pb); // version
331 flags = avio_rb24(pb); //flags
332 shortbytes = avio_rb16(pb);
333 pad = (shortbytes & 0x8000) >> 15;
334 langcode = shortbytes & 0x7ffff;
335 ff_mov_lang_to_iso639(langcode, language);
336 len -= 6;
337
338 //read BYTE ORDER MARK
339 byteOrderMark = avio_rb16(pb);
340 if (byteOrderMark == 0xFEFF) {//UTF-16BE
341 len -= 2;
342 offset += 2;
343 avio_get_str16be(pb, len, str, 128);
344 av_dict_set(&c->fc->metadata, key, str, 0);
345 } else if (byteOrderMark == 0xFFFE) {//UTF-16LE
346 len -= 2;
347 offset += 2;
348 avio_get_str16le(pb, len, str, 128);
349 av_dict_set(&c->fc->metadata, key, str, 0);
350 } else { //not BYTE ORDER MARK,UTF-8 format
351 avio_seek(pb, -2, SEEK_CUR);
352 avio_read(pb, str+offset, len);
353 str[len] = 0;
354 av_dict_set(&c->fc->metadata, key, str, 0);
355 if (*language && strcmp(language, "und")) {
356 snprintf(key2, sizeof(key2), "%s-%s", key, language);
357 av_dict_set(&c->fc->metadata, key2, str, 0);
358 }
359 }
360 return 0;
361}
362
363static int mov_metadata_3gpp_yrrc(MOVContext *c, AVIOContext *pb,
364 unsigned len, const char *key)
365{
366 uint8_t version;
367 uint32_t flags;
368 char byte = 0;
369 char str[128] = {0};
370 char year[32] = {0};
371 uint16_t yearCode = 0;
372
373 version = avio_r8(pb); // version
374 flags = avio_rb24(pb); //flags
375 len -= 4;
376
377 //get the year when the media was recorded
378 yearCode = avio_rb16(pb);
379 snprintf(year, sizeof(year), "%d", yearCode);
380 av_dict_set(&c->fc->metadata, key, year, 0);
381
382 return 0;
383}
384
385static int mov_metadata_3gpp_album(MOVContext *c, AVIOContext *pb,
386 unsigned len, const char *key)
387{
388 char key2[16];
389 uint8_t version;
390 uint32_t flags;
391 uint8_t pad;
392 uint16_t langcode;
393 uint16_t shortbytes;
394 char language[4] = {0};
395 char byte = 0;
396 int read_byte = 0;
397 char str[128] = {0};
398 int offset = 0;
399 uint16_t byteOrderMark = 0;
400 uint8_t trackNum[16] ={0};
401
402 version = avio_r8(pb); // version
403 flags = avio_rb24(pb); //flags
404 shortbytes = avio_rb16(pb);
405 pad = (shortbytes & 0x8000) >> 15; //pad
406 langcode = shortbytes & 0x7ffff; //language
407 ff_mov_lang_to_iso639(langcode, language);
408 len -= 6;
409
410 //get track num
411 do {
412 byte = avio_r8(pb);
413 read_byte++;
414 } while(byte != '\0');
415 byte = avio_r8(pb);
416 read_byte++;
417 snprintf(trackNum, sizeof(trackNum), "%d", byte);
418 av_dict_set(&c->fc->metadata, "track", trackNum, 0);
419
420 //seek back to get Text of album title
421 avio_seek(pb, -read_byte, SEEK_CUR);
422
423 //read BYTE ORDER MARK
424 byteOrderMark = avio_rb16(pb);
425 if (byteOrderMark == 0xFEFF) {//UTF-16BE
426 len -= 2;
427 offset += 2;
428 avio_get_str16be(pb, len, str, 128);
429 av_dict_set(&c->fc->metadata, key, str, 0);
430 } else if (byteOrderMark == 0xFFFE) {//UTF-16LE
431 len -= 2;
432 offset += 2;
433 avio_get_str16le(pb, len, str, 128);
434 av_dict_set(&c->fc->metadata, key, str, 0);
435 } else { //not BYTE ORDER MARK,UTF-8 format
436 avio_seek(pb, -2, SEEK_CUR);
437 avio_read(pb, str+offset, len);
438 str[len] = 0;
439 av_dict_set(&c->fc->metadata, key, str, 0);
440 if (*language && strcmp(language, "und")) {
441 snprintf(key2, sizeof(key2), "%s-%s", key, language);
442 av_dict_set(&c->fc->metadata, key2, str, 0);
443 }
444 }
445
446 return 0;
447}
448
449static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
450{
451#ifdef MOV_EXPORT_ALL_METADATA
452 char tmp_key[5];
453#endif
454 char str[1024], key2[16], language[4] = {0};
455 const char *key = NULL;
456 uint16_t langcode = 0;
457 uint32_t data_type = 0, str_size;
458 uint32_t cover_size = 0;
459 int skip_read = 0;
460 int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
461
462 if (c->itunes_metadata && atom.type == MKTAG('-','-','-','-'))
463 return mov_read_custom_metadata(c, pb, atom);
464
465 switch (atom.type) {
466 case MKTAG('t', 'i', 't', 'l'):
467 key = "title";
468 skip_read = 1;
469 parse = mov_metadata_3gpp_general;
470 break;
471 case MKTAG('a', 'l', 'b', 'm'):
472 key = "album";
473 skip_read = 1;
474 parse = mov_metadata_3gpp_album;
475 break;
476 case MKTAG('d', 's', 'c', 'p'):
477 key = "description";
478 skip_read = 1;
479 parse = mov_metadata_3gpp_general;
480 break;
481 case MKTAG('p', 'e', 'r', 'f'):
482 key = "artist";
483 skip_read = 1;
484 parse = mov_metadata_3gpp_general;
485 break;
486 case MKTAG('y', 'r', 'r', 'c'):
487 key = "year";
488 skip_read = 1;
489 parse = mov_metadata_3gpp_yrrc;
490 break;
491
492 case MKTAG(0xa9,'n','a','m'): key = "title"; break;
493 case MKTAG(0xa9,'a','u','t'):
494 case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
495 case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
496 case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
497 case MKTAG( 'c','p','r','t'):
498 case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
499 case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
500 case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
501 case MKTAG(0xa9,'c','m','t'):
502 case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
503 case MKTAG(0xa9,'a','l','b'): key = "album"; break;
504 case MKTAG(0xa9,'d','a','y'): key = "date"; break;
505 case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
506 case MKTAG( 'g','n','r','e'): key = "genre";
507 parse = mov_metadata_gnre; break;
508 case MKTAG(0xa9,'t','o','o'):
509 case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
510 case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
511 case MKTAG(0xa9,'m','a','k'): key = "make"; break;
512 case MKTAG(0xa9,'m','o','d'): key = "model"; break;
513 case MKTAG(0xa9,'x','y','z'): key = "location"; break;
514 case MKTAG( 'd','e','s','c'): key = "description";break;
515 case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
516 case MKTAG( 't','v','s','h'): key = "show"; break;
517 case MKTAG( 't','v','e','n'): key = "episode_id";break;
518 case MKTAG( 't','v','n','n'): key = "network"; break;
519 case MKTAG( 't','r','k','n'): key = "track";
520 parse = mov_metadata_track_or_disc_number; break;
521 case MKTAG( 'd','i','s','k'): key = "disc";
522 parse = mov_metadata_track_or_disc_number; break;
523 case MKTAG( 't','v','e','s'): key = "episode_sort";
524 parse = mov_metadata_int8_bypass_padding; break;
525 case MKTAG( 't','v','s','n'): key = "season_number";
526 parse = mov_metadata_int8_bypass_padding; break;
527 case MKTAG( 's','t','i','k'): key = "media_type";
528 parse = mov_metadata_int8_no_padding; break;
529 case MKTAG( 'h','d','v','d'): key = "hd_video";
530 parse = mov_metadata_int8_no_padding; break;
531 case MKTAG( 'p','g','a','p'): key = "gapless_playback";
532 parse = mov_metadata_int8_no_padding; break;
533 case MKTAG( '@','P','R','M'):
534 return mov_metadata_raw(c, pb, atom.size, "premiere_version");
535 case MKTAG( '@','P','R','Q'):
536 return mov_metadata_raw(c, pb, atom.size, "quicktime_version");
537 }
538
539 if (c->itunes_metadata && atom.size > 8) {
540 int data_size = avio_rb32(pb);
541 int tag = avio_rl32(pb);
542 if (tag == MKTAG('d','a','t','a')) {
543 data_type = avio_rb32(pb); // type
544 avio_rb32(pb); // unknown
545 str_size = data_size - 16;
546 cover_size = data_size -16;
547 atom.size -= 16;
548
549 if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
550 int ret = mov_read_covr(c, pb, data_type, str_size);
551 if (ret < 0) {
552 av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
553 return ret;
554 }
555 } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
556 uint32_t index = AV_RB32(&atom.type);
557 if (index < c->meta_keys_count) {
558 key = c->meta_keys[index];
559 } else {
560 av_log(c->fc, AV_LOG_WARNING,
561 "The index of 'data' is out of range: %d >= %d.\n",
562 index, c->meta_keys_count);
563 }
564
565 }
566 } else return 0;
567 } else if (atom.size > 4 && key && !c->itunes_metadata) {
568 if (skip_read) {
569 str_size = atom.size;
570 }
571 else{
572 str_size = avio_rb16(pb); // string length
573 langcode = avio_rb16(pb);
574 ff_mov_lang_to_iso639(langcode, language);
575 atom.size -= 4;
576 }
577 } else
578 str_size = atom.size;
579
580#ifdef MOV_EXPORT_ALL_METADATA
581 if (!key) {
582 snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
583 key = tmp_key;
584 }
585#endif
586
587 if (!key)
588 return 0;
589 if (atom.size < 0)
590 return AVERROR_INVALIDDATA;
591
592 str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
593
594 if (parse)
595 parse(c, pb, str_size, key);
596 else {
597 if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded
598 mov_read_mac_string(c, pb, str_size, str, sizeof(str));
599 } else if (data_type == 13 || data_type == 14){
600 mov_extract_cover_pic(c->fc, pb, data_type, cover_size, str);
601 } else if (data_type == 23 && str_size >= 4) { // BE float32
602 // Allocates enough space if data_type is a float32 number
603 int str_size_alloc = 512 + 1;
604 float val = av_int2float(avio_rb32(pb));
605 if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
606 av_log(c->fc, AV_LOG_ERROR,
607 "Failed to store the float32 number (%f) in string.\n", val);
608 av_free(str);
609 return AVERROR_INVALIDDATA;
610 }
611
612 } else {
613 avio_read(pb, str, str_size);
614 str[str_size] = 0;
615 }
616 // Android MP4 writer put an additional '/' at the end, discard it.
617 // The CTS test seems the added '/' is not needed.
618 if ((atom.type == MKTAG(0xa9,'x','y','z')) && (str[str_size-1] == 0x2f)) {
619 str[str_size-1] = 0;
620 }
621 av_dict_set(&c->fc->metadata, key, str, 0);
622 if (*language && strcmp(language, "und")) {
623 snprintf(key2, sizeof(key2), "%s-%s", key, language);
624 av_dict_set(&c->fc->metadata, key2, str, 0);
625 }
626 }
627 av_dlog(c->fc, "lang \"%3s\" ", language);
628 av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
629 key, str, (char*)&atom.type, str_size, atom.size);
630
631 return 0;
632}
633
634static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
635{
636 int64_t start;
637 int i, nb_chapters, str_len, version;
638 char str[256+1];
639
640 if ((atom.size -= 5) < 0)
641 return 0;
642
643 version = avio_r8(pb);
644 avio_rb24(pb);
645 if (version)
646 avio_rb32(pb); // ???
647 nb_chapters = avio_r8(pb);
648
649 for (i = 0; i < nb_chapters; i++) {
650 if (atom.size < 9)
651 return 0;
652
653 start = avio_rb64(pb);
654 str_len = avio_r8(pb);
655
656 if ((atom.size -= 9+str_len) < 0)
657 return 0;
658
659 avio_read(pb, str, str_len);
660 str[str_len] = 0;
661 avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
662 }
663 return 0;
664}
665
666#define MIN_DATA_ENTRY_BOX_SIZE 12
667static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
668{
669 AVStream *st;
670 MOVStreamContext *sc;
671 int entries, i, j;
672
673 if (c->fc->nb_streams < 1)
674 return 0;
675 st = c->fc->streams[c->fc->nb_streams-1];
676 sc = st->priv_data;
677
678 avio_rb32(pb); // version + flags
679 entries = avio_rb32(pb);
680 if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
681 entries >= UINT_MAX / sizeof(*sc->drefs))
682 return AVERROR_INVALIDDATA;
683 av_free(sc->drefs);
684 sc->drefs_count = 0;
685 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
686 if (!sc->drefs)
687 return AVERROR(ENOMEM);
688 sc->drefs_count = entries;
689
690 for (i = 0; i < sc->drefs_count; i++) {
691 MOVDref *dref = &sc->drefs[i];
692 uint32_t size = avio_rb32(pb);
693 int64_t next = avio_tell(pb) + size - 4;
694
695 if (size < 12)
696 return AVERROR_INVALIDDATA;
697
698 dref->type = avio_rl32(pb);
699 avio_rb32(pb); // version + flags
700 av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
701
702 if (dref->type == MKTAG('a','l','i','s') && size > 150) {
703 /* macintosh alias record */
704 uint16_t volume_len, len;
705 int16_t type;
706
707 avio_skip(pb, 10);
708
709 volume_len = avio_r8(pb);
710 volume_len = FFMIN(volume_len, 27);
711 avio_read(pb, dref->volume, 27);
712 dref->volume[volume_len] = 0;
713 av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
714
715 avio_skip(pb, 12);
716
717 len = avio_r8(pb);
718 len = FFMIN(len, 63);
719 avio_read(pb, dref->filename, 63);
720 dref->filename[len] = 0;
721 av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
722
723 avio_skip(pb, 16);
724
725 /* read next level up_from_alias/down_to_target */
726 dref->nlvl_from = avio_rb16(pb);
727 dref->nlvl_to = avio_rb16(pb);
728 av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
729 dref->nlvl_from, dref->nlvl_to);
730
731 avio_skip(pb, 16);
732
733 for (type = 0; type != -1 && avio_tell(pb) < next; ) {
734 if(url_feof(pb))
735 return AVERROR_EOF;
736 type = avio_rb16(pb);
737 len = avio_rb16(pb);
738 av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
739 if (len&1)
740 len += 1;
741 if (type == 2) { // absolute path
742 av_free(dref->path);
743 dref->path = av_mallocz(len+1);
744 if (!dref->path)
745 return AVERROR(ENOMEM);
746 avio_read(pb, dref->path, len);
747 if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
748 len -= volume_len;
749 memmove(dref->path, dref->path+volume_len, len);
750 dref->path[len] = 0;
751 }
752 for (j = 0; j < len; j++)
753 if (dref->path[j] == ':')
754 dref->path[j] = '/';
755 av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
756 } else if (type == 0) { // directory name
757 av_free(dref->dir);
758 dref->dir = av_malloc(len+1);
759 if (!dref->dir)
760 return AVERROR(ENOMEM);
761 avio_read(pb, dref->dir, len);
762 dref->dir[len] = 0;
763 for (j = 0; j < len; j++)
764 if (dref->dir[j] == ':')
765 dref->dir[j] = '/';
766 av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
767 } else
768 avio_skip(pb, len);
769 }
770 }
771 avio_seek(pb, next, SEEK_SET);
772 }
773 return 0;
774}
775
776static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
777{
778 AVStream *st;
779 uint32_t type;
780 uint32_t av_unused ctype;
781 int title_size;
782 char *title_str;
783
784 avio_r8(pb); /* version */
785 avio_rb24(pb); /* flags */
786
787 /* component type */
788 ctype = avio_rl32(pb);
789 type = avio_rl32(pb); /* component subtype */
790
791 av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
792 av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
793
794 if (c->trak_index < 0) { // meta not inside a trak
795 if (type == MKTAG('m','d','t','a')) {
796 c->found_hdlr_mdta = 1;
797 }
798 return 0;
799 }
800
801 st = c->fc->streams[c->fc->nb_streams-1];
802
803 if (type == MKTAG('v','i','d','e'))
804 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
805 else if (type == MKTAG('s','o','u','n'))
806 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
807 else if (type == MKTAG('m','1','a',' '))
808 st->codec->codec_id = AV_CODEC_ID_MP2;
809 else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
810 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
811
812 avio_rb32(pb); /* component manufacture */
813 avio_rb32(pb); /* component flags */
814 avio_rb32(pb); /* component flags mask */
815
816 title_size = atom.size - 24;
817 if (title_size > 0) {
818 title_str = av_malloc(title_size + 1); /* Add null terminator */
819 if (!title_str)
820 return AVERROR(ENOMEM);
821 avio_read(pb, title_str, title_size);
822 title_str[title_size] = 0;
823 if (title_str[0])
824 av_dict_set(&st->metadata, "handler_name", title_str +
825 (!c->isom && title_str[0] == title_size - 1), 0);
826 av_freep(&title_str);
827 }
828
829 return 0;
830}
831
832int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
833{
834 AVStream *st;
835 int tag;
836
837 if (fc->nb_streams < 1)
838 return 0;
839 st = fc->streams[fc->nb_streams-1];
840
841 avio_rb32(pb); /* version + flags */
842 ff_mp4_read_descr(fc, pb, &tag);
843 if (tag == MP4ESDescrTag) {
844 ff_mp4_parse_es_descr(pb, NULL);
845 } else
846 avio_rb16(pb); /* ID */
847
848 ff_mp4_read_descr(fc, pb, &tag);
849 if (tag == MP4DecConfigDescrTag)
850 ff_mp4_read_dec_config_descr(fc, st, pb);
851 return 0;
852}
853
854static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
855{
856 return ff_mov_read_esds(c->fc, pb, atom);
857}
858
859static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
860{
861 AVStream *st;
862 int ac3info, acmod, lfeon, bsmod;
863
864 if (c->fc->nb_streams < 1)
865 return 0;
866 st = c->fc->streams[c->fc->nb_streams-1];
867
868 ac3info = avio_rb24(pb);
869 bsmod = (ac3info >> 14) & 0x7;
870 acmod = (ac3info >> 11) & 0x7;
871 lfeon = (ac3info >> 10) & 0x1;
872 st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
873 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
874 if (lfeon)
875 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
876 st->codec->audio_service_type = bsmod;
877 if (st->codec->channels > 1 && bsmod == 0x7)
878 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
879
880 return 0;
881}
882
883static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
884{
885 AVStream *st;
886 int eac3info, acmod, lfeon, bsmod;
887
888 if (c->fc->nb_streams < 1)
889 return 0;
890 st = c->fc->streams[c->fc->nb_streams-1];
891
892 /* No need to parse fields for additional independent substreams and its
893 * associated dependent substreams since libavcodec's E-AC-3 decoder
894 * does not support them yet. */
895 avio_rb16(pb); /* data_rate and num_ind_sub */
896 eac3info = avio_rb24(pb);
897 bsmod = (eac3info >> 12) & 0x1f;
898 acmod = (eac3info >> 9) & 0x7;
899 lfeon = (eac3info >> 8) & 0x1;
900 st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
901 if (lfeon)
902 st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
903 st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
904 st->codec->audio_service_type = bsmod;
905 if (st->codec->channels > 1 && bsmod == 0x7)
906 st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
907
908 return 0;
909}
910
911static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
912{
913 AVStream *st;
914
915 if (c->fc->nb_streams < 1)
916 return 0;
917 st = c->fc->streams[c->fc->nb_streams-1];
918
919 if (atom.size < 16)
920 return 0;
921
922 /* skip version and flags */
923 avio_skip(pb, 4);
924
925 ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
926
927 return 0;
928}
929
930static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
931{
932 AVStream *st;
933
934 if (c->fc->nb_streams < 1)
935 return 0;
936 st = c->fc->streams[c->fc->nb_streams-1];
937
938 if (ff_get_wav_header(pb, st->codec, atom.size) < 0) {
939 av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
940 }
941
942 return 0;
943}
944
945static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
946{
947 const int num = avio_rb32(pb);
948 const int den = avio_rb32(pb);
949 AVStream *st;
950
951 if (c->fc->nb_streams < 1)
952 return 0;
953 st = c->fc->streams[c->fc->nb_streams-1];
954
955 if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
956 (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
957 av_log(c->fc, AV_LOG_WARNING,
958 "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
959 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
960 num, den);
961 } else if (den != 0) {
962 st->sample_aspect_ratio.num = num;
963 st->sample_aspect_ratio.den = den;
964 }
965 return 0;
966}
967
968/* this atom contains actual media data */
969static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
970{
971 if (atom.size == 0) /* wrong one (MP4) */
972 return 0;
973 c->found_mdat=1;
974 return 0; /* now go for moov */
975}
976
977/* read major brand, minor version and compatible brands and store them as metadata */
978static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
979{
980 uint32_t minor_ver;
981 int comp_brand_size;
982 char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
983 char* comp_brands_str;
984 uint8_t type[5] = {0};
985
986 avio_read(pb, type, 4);
987 if (strcmp(type, "qt "))
988 c->isom = 1;
989 av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
990 av_dict_set(&c->fc->metadata, "major_brand", type, 0);
991 minor_ver = avio_rb32(pb); /* minor version */
992 snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
993 av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
994
995 comp_brand_size = atom.size - 8;
996 if (comp_brand_size < 0)
997 return AVERROR_INVALIDDATA;
998 comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
999 if (!comp_brands_str)
1000 return AVERROR(ENOMEM);
1001 avio_read(pb, comp_brands_str, comp_brand_size);
1002 comp_brands_str[comp_brand_size] = 0;
1003 av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
1004 av_freep(&comp_brands_str);
1005
1006 return 0;
1007}
1008
1009/* this atom should contain all header atoms */
1010static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1011{
1012 int ret;
1013
1014 if (c->found_moov) {
1015 av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1016 avio_skip(pb, atom.size);
1017 return 0;
1018 }
1019
1020 if ((ret = mov_read_default(c, pb, atom)) < 0)
1021 return ret;
1022 /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1023 /* so we don't parse the whole file if over a network */
1024 c->found_moov=1;
1025 return 0; /* now go for mdat */
1026}
1027
1028static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1029{
1030 c->fragment.moof_offset = avio_tell(pb) - 8;
1031 av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1032 return mov_read_default(c, pb, atom);
1033}
1034
1035static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
1036{
1037 char buffer[32];
1038 if (time) {
1039 struct tm *ptm;
1040 time_t timet;
1041 if(time >= 2082844800)
1042 time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1043 timet = time;
1044 ptm = gmtime(&timet);
1045 if (!ptm) return;
1046 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
1047 av_dict_set(metadata, "creation_time", buffer, 0);
1048 }
1049}
1050
1051static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1052{
1053 AVStream *st;
1054 MOVStreamContext *sc;
1055 int version;
1056 char language[4] = {0};
1057 unsigned lang;
1058 int64_t creation_time;
1059
1060 if (c->fc->nb_streams < 1)
1061 return 0;
1062 st = c->fc->streams[c->fc->nb_streams-1];
1063 sc = st->priv_data;
1064
1065 if (sc->time_scale) {
1066 av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1067 return AVERROR_INVALIDDATA;
1068 }
1069
1070 version = avio_r8(pb);
1071 if (version > 1) {
1072 avpriv_request_sample(c->fc, "Version %d", version);
1073 return AVERROR_PATCHWELCOME;
1074 }
1075 avio_rb24(pb); /* flags */
1076 if (version == 1) {
1077 creation_time = avio_rb64(pb);
1078 avio_rb64(pb);
1079 } else {
1080 creation_time = avio_rb32(pb);
1081 avio_rb32(pb); /* modification time */
1082 }
1083 mov_metadata_creation_time(&st->metadata, creation_time);
1084
1085 sc->time_scale = avio_rb32(pb);
1086 st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1087
1088 lang = avio_rb16(pb); /* language */
1089 if (ff_mov_lang_to_iso639(lang, language))
1090 av_dict_set(&st->metadata, "language", language, 0);
1091 avio_rb16(pb); /* quality */
1092
1093 return 0;
1094}
1095
1096static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1097{
1098 int64_t creation_time;
1099 int version = avio_r8(pb); /* version */
1100 avio_rb24(pb); /* flags */
1101
1102 if (version == 1) {
1103 creation_time = avio_rb64(pb);
1104 avio_rb64(pb);
1105 } else {
1106 creation_time = avio_rb32(pb);
1107 avio_rb32(pb); /* modification time */
1108 }
1109 mov_metadata_creation_time(&c->fc->metadata, creation_time);
1110 c->time_scale = avio_rb32(pb); /* time scale */
1111
1112 av_dlog(c->fc, "time scale = %i\n", c->time_scale);
1113
1114 c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1115 // set the AVCodecContext duration because the duration of individual tracks
1116 // may be inaccurate
1117 if (c->time_scale > 0 && !c->trex_data)
1118 c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1119 avio_rb32(pb); /* preferred scale */
1120
1121 avio_rb16(pb); /* preferred volume */
1122
1123 avio_skip(pb, 10); /* reserved */
1124
1125 avio_skip(pb, 36); /* display matrix */
1126
1127 avio_rb32(pb); /* preview time */
1128 avio_rb32(pb); /* preview duration */
1129 avio_rb32(pb); /* poster time */
1130 avio_rb32(pb); /* selection time */
1131 avio_rb32(pb); /* selection duration */
1132 avio_rb32(pb); /* current time */
1133 avio_rb32(pb); /* next track ID */
1134 return 0;
1135}
1136
1137static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1138{
1139 AVStream *st;
1140 int little_endian;
1141
1142 if (c->fc->nb_streams < 1)
1143 return 0;
1144 st = c->fc->streams[c->fc->nb_streams-1];
1145
1146 little_endian = avio_rb16(pb) & 0xFF;
1147 av_dlog(c->fc, "enda %d\n", little_endian);
1148 if (little_endian == 1) {
1149 switch (st->codec->codec_id) {
1150 case AV_CODEC_ID_PCM_S24BE:
1151 st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
1152 break;
1153 case AV_CODEC_ID_PCM_S32BE:
1154 st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
1155 break;
1156 case AV_CODEC_ID_PCM_F32BE:
1157 st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
1158 break;
1159 case AV_CODEC_ID_PCM_F64BE:
1160 st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
1161 break;
1162 default:
1163 break;
1164 }
1165 }
1166 return 0;
1167}
1168
1169static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1170{
1171 AVStream *st;
1172 unsigned mov_field_order;
1173 enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1174
1175 if (c->fc->nb_streams < 1) // will happen with jp2 files
1176 return 0;
1177 st = c->fc->streams[c->fc->nb_streams-1];
1178 if (atom.size < 2)
1179 return AVERROR_INVALIDDATA;
1180 mov_field_order = avio_rb16(pb);
1181 if ((mov_field_order & 0xFF00) == 0x0100)
1182 decoded_field_order = AV_FIELD_PROGRESSIVE;
1183 else if ((mov_field_order & 0xFF00) == 0x0200) {
1184 switch (mov_field_order & 0xFF) {
1185 case 0x01: decoded_field_order = AV_FIELD_TT;
1186 break;
1187 case 0x06: decoded_field_order = AV_FIELD_BB;
1188 break;
1189 case 0x09: decoded_field_order = AV_FIELD_TB;
1190 break;
1191 case 0x0E: decoded_field_order = AV_FIELD_BT;
1192 break;
1193 }
1194 }
1195 if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1196 av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1197 }
1198 st->codec->field_order = decoded_field_order;
1199
1200 return 0;
1201}
1202
1203/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
1204static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
1205 enum AVCodecID codec_id)
1206{
1207 AVStream *st;
1208 uint64_t size;
1209 uint8_t *buf;
1210 int err;
1211
1212 if (c->fc->nb_streams < 1) // will happen with jp2 files
1213 return 0;
1214 st= c->fc->streams[c->fc->nb_streams-1];
1215
1216 if (st->codec->codec_id != codec_id)
1217 return 0; /* unexpected codec_id - don't mess with extradata */
1218
1219 size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
1220 if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1221 return AVERROR_INVALIDDATA;
1222 if ((err = av_reallocp(&st->codec->extradata, size)) < 0) {
1223 st->codec->extradata_size = 0;
1224 return err;
1225 }
1226 buf = st->codec->extradata + st->codec->extradata_size;
1227 st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
1228 AV_WB32( buf , atom.size + 8);
1229 AV_WL32( buf + 4, atom.type);
1230 avio_read(pb, buf + 8, atom.size);
1231 return 0;
1232}
1233
1234/* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1235static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1236{
1237 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1238}
1239
1240static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1241{
1242 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1243}
1244
1245static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1246{
1247 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1248}
1249
1250static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1251{
1252 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1253}
1254
1255static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1256{
1257 int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1258
1259 if (!ret && c->fc->nb_streams >= 1) {
1260 AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1261 if (avctx->extradata_size >= 40) {
1262 avctx->height = AV_RB16(&avctx->extradata[36]);
1263 avctx->width = AV_RB16(&avctx->extradata[38]);
1264 }
1265 }
1266 return ret;
1267}
1268
1269static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1270{
1271 AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1272 if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1273 codec->codec_id == AV_CODEC_ID_H264 &&
1274 atom.size > 11) {
1275 avio_skip(pb, 10);
1276 /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1277 if (avio_rb16(pb) == 0xd4d)
1278 codec->width = 1440;
1279 return 0;
1280 }
1281
1282 return mov_read_avid(c, pb, atom);
1283}
1284
1285static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1286{
1287 return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1288}
1289
1290static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1291{
1292 AVStream *st;
1293
1294 if (c->fc->nb_streams < 1)
1295 return 0;
1296 st = c->fc->streams[c->fc->nb_streams-1];
1297
1298 if ((uint64_t)atom.size > (1<<30))
1299 return AVERROR_INVALIDDATA;
1300
1301 if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1302 st->codec->codec_id == AV_CODEC_ID_QDMC ||
1303 st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1304 // pass all frma atom to codec, needed at least for QDMC and QDM2
1305 av_free(st->codec->extradata);
1306 if (ff_alloc_extradata(st->codec, atom.size))
1307 return AVERROR(ENOMEM);
1308 avio_read(pb, st->codec->extradata, atom.size);
1309 } else if (atom.size > 8) { /* to read frma, esds atoms */
1310 int ret;
1311 if ((ret = mov_read_default(c, pb, atom)) < 0)
1312 return ret;
1313 } else
1314 avio_skip(pb, atom.size);
1315 return 0;
1316}
1317
1318/**
1319 * This function reads atom content and puts data in extradata without tag
1320 * nor size unlike mov_read_extradata.
1321 */
1322static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1323{
1324 AVStream *st;
1325
1326 if (c->fc->nb_streams < 1)
1327 return 0;
1328 st = c->fc->streams[c->fc->nb_streams-1];
1329
1330 if ((uint64_t)atom.size > (1<<30))
1331 return AVERROR_INVALIDDATA;
1332
1333 if (atom.size >= 10) {
1334 // Broken files created by legacy versions of libavformat will
1335 // wrap a whole fiel atom inside of a glbl atom.
1336 unsigned size = avio_rb32(pb);
1337 unsigned type = avio_rl32(pb);
1338 avio_seek(pb, -8, SEEK_CUR);
1339 if (type == MKTAG('f','i','e','l') && size == atom.size)
1340 return mov_read_default(c, pb, atom);
1341 }
1342 av_free(st->codec->extradata);
1343 if (ff_alloc_extradata(st->codec, atom.size))
1344 return AVERROR(ENOMEM);
1345 avio_read(pb, st->codec->extradata, atom.size);
1346 return 0;
1347}
1348
1349static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1350{
1351 AVStream *st;
1352 uint8_t profile_level;
1353
1354 if (c->fc->nb_streams < 1)
1355 return 0;
1356 st = c->fc->streams[c->fc->nb_streams-1];
1357
1358 if (atom.size >= (1<<28) || atom.size < 7)
1359 return AVERROR_INVALIDDATA;
1360
1361 profile_level = avio_r8(pb);
1362 if ((profile_level & 0xf0) != 0xc0)
1363 return 0;
1364
1365 av_free(st->codec->extradata);
1366 if (ff_alloc_extradata(st->codec, atom.size - 7))
1367 return AVERROR(ENOMEM);
1368 avio_seek(pb, 6, SEEK_CUR);
1369 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
1370 return 0;
1371}
1372
1373/**
1374 * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1375 * but can have extradata appended at the end after the 40 bytes belonging
1376 * to the struct.
1377 */
1378static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1379{
1380 AVStream *st;
1381
1382 if (c->fc->nb_streams < 1)
1383 return 0;
1384 if (atom.size <= 40)
1385 return 0;
1386 st = c->fc->streams[c->fc->nb_streams-1];
1387
1388 if ((uint64_t)atom.size > (1<<30))
1389 return AVERROR_INVALIDDATA;
1390
1391 av_free(st->codec->extradata);
1392 if (ff_alloc_extradata(st->codec, atom.size - 40))
1393 return AVERROR(ENOMEM);
1394 avio_skip(pb, 40);
1395 avio_read(pb, st->codec->extradata, atom.size - 40);
1396 return 0;
1397}
1398
1399static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1400{
1401 AVStream *st;
1402 MOVStreamContext *sc;
1403 unsigned int i, entries;
1404
1405 if (c->fc->nb_streams < 1)
1406 return 0;
1407 st = c->fc->streams[c->fc->nb_streams-1];
1408 sc = st->priv_data;
1409
1410 avio_r8(pb); /* version */
1411 avio_rb24(pb); /* flags */
1412
1413 entries = avio_rb32(pb);
1414
1415 if (!entries)
1416 return 0;
1417 if (entries >= UINT_MAX/sizeof(int64_t))
1418 return AVERROR_INVALIDDATA;
1419
1420 sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
1421 if (!sc->chunk_offsets)
1422 return AVERROR(ENOMEM);
1423 sc->chunk_count = entries;
1424
1425 if (atom.type == MKTAG('s','t','c','o'))
1426 for (i = 0; i < entries && !pb->eof_reached; i++)
1427 sc->chunk_offsets[i] = avio_rb32(pb);
1428 else if (atom.type == MKTAG('c','o','6','4'))
1429 for (i = 0; i < entries && !pb->eof_reached; i++)
1430 sc->chunk_offsets[i] = avio_rb64(pb);
1431 else
1432 return AVERROR_INVALIDDATA;
1433
1434 sc->chunk_count = i;
1435
1436 if (pb->eof_reached)
1437 return AVERROR_EOF;
1438
1439 return 0;
1440}
1441
1442/**
1443 * Compute codec id for 'lpcm' tag.
1444 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1445 */
1446enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
1447{
1448 /* lpcm flags:
1449 * 0x1 = float
1450 * 0x2 = big-endian
1451 * 0x4 = signed
1452 */
1453 return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1454}
1455
1456static int mov_codec_id(AVStream *st, uint32_t format)
1457{
1458 int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1459
1460 if (id <= 0 &&
1461 ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1462 (format & 0xFFFF) == 'T' + ('S' << 8)))
1463 id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1464
1465 if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1466 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1467 } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1468 /* skip old asf mpeg4 tag */
1469 format && format != MKTAG('m','p','4','s')) {
1470 id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1471 if (id <= 0)
1472 id = ff_codec_get_id(ff_codec_bmp_tags, format);
1473 if (id > 0)
1474 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1475 else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1476 (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1477 st->codec->codec_id == AV_CODEC_ID_NONE)) {
1478 id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
1479 if (id > 0)
1480 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1481 }
1482 }
1483
1484 st->codec->codec_tag = format;
1485
1486 return id;
1487}
1488
1489static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
1490 AVStream *st, MOVStreamContext *sc)
1491{
1492 unsigned int color_depth, len, j;
1493 int color_greyscale;
1494 int color_table_id;
1495
1496 avio_rb16(pb); /* version */
1497 avio_rb16(pb); /* revision level */
1498 avio_rb32(pb); /* vendor */
1499 avio_rb32(pb); /* temporal quality */
1500 avio_rb32(pb); /* spatial quality */
1501
1502 st->codec->width = avio_rb16(pb); /* width */
1503 st->codec->height = avio_rb16(pb); /* height */
1504
1505 avio_rb32(pb); /* horiz resolution */
1506 avio_rb32(pb); /* vert resolution */
1507 avio_rb32(pb); /* data size, always 0 */
1508 avio_rb16(pb); /* frames per samples */
1509
1510 len = avio_r8(pb); /* codec name, pascal string */
1511 if (len > 31)
1512 len = 31;
1513 mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
1514 if (len < 31)
1515 avio_skip(pb, 31 - len);
1516 /* codec_tag YV12 triggers an UV swap in rawdec.c */
1517 if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1518 st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1519 st->codec->width &= ~1;
1520 st->codec->height &= ~1;
1521 }
1522 /* Flash Media Server uses tag H263 with Sorenson Spark */
1523 if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1524 !memcmp(st->codec->codec_name, "Sorenson H263", 13))
1525 st->codec->codec_id = AV_CODEC_ID_FLV1;
1526
1527 st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1528 color_table_id = avio_rb16(pb); /* colortable id */
1529 av_dlog(c->fc, "depth %d, ctab id %d\n",
1530 st->codec->bits_per_coded_sample, color_table_id);
1531 /* figure out the palette situation */
1532 color_depth = st->codec->bits_per_coded_sample & 0x1F;
1533 color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1534
1535 /* if the depth is 2, 4, or 8 bpp, file is palettized */
1536 if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1537 /* for palette traversal */
1538 unsigned int color_start, color_count, color_end;
1539 unsigned char a, r, g, b;
1540
1541 if (color_greyscale) {
1542 int color_index, color_dec;
1543 /* compute the greyscale palette */
1544 st->codec->bits_per_coded_sample = color_depth;
1545 color_count = 1 << color_depth;
1546 color_index = 255;
1547 color_dec = 256 / (color_count - 1);
1548 for (j = 0; j < color_count; j++) {
1549 if (st->codec->codec_id == AV_CODEC_ID_CINEPAK){
1550 r = g = b = color_count - 1 - color_index;
1551 } else
1552 r = g = b = color_index;
1553 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1554 color_index -= color_dec;
1555 if (color_index < 0)
1556 color_index = 0;
1557 }
1558 } else if (color_table_id) {
1559 const uint8_t *color_table;
1560 /* if flag bit 3 is set, use the default palette */
1561 color_count = 1 << color_depth;
1562 if (color_depth == 2)
1563 color_table = ff_qt_default_palette_4;
1564 else if (color_depth == 4)
1565 color_table = ff_qt_default_palette_16;
1566 else
1567 color_table = ff_qt_default_palette_256;
1568
1569 for (j = 0; j < color_count; j++) {
1570 r = color_table[j * 3 + 0];
1571 g = color_table[j * 3 + 1];
1572 b = color_table[j * 3 + 2];
1573 sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1574 }
1575 } else {
1576 /* load the palette from the file */
1577 color_start = avio_rb32(pb);
1578 color_count = avio_rb16(pb);
1579 color_end = avio_rb16(pb);
1580 if ((color_start <= 255) && (color_end <= 255)) {
1581 for (j = color_start; j <= color_end; j++) {
1582 /* each A, R, G, or B component is 16 bits;
1583 * only use the top 8 bits */
1584 a = avio_r8(pb);
1585 avio_r8(pb);
1586 r = avio_r8(pb);
1587 avio_r8(pb);
1588 g = avio_r8(pb);
1589 avio_r8(pb);
1590 b = avio_r8(pb);
1591 avio_r8(pb);
1592 sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1593 }
1594 }
1595 }
1596 sc->has_palette = 1;
1597 }
1598}
1599
1600static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb,
1601 AVStream *st, MOVStreamContext *sc)
1602{
1603 int bits_per_sample, flags;
1604 uint16_t version = avio_rb16(pb);
1605 AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1606
1607 avio_rb16(pb); /* revision level */
1608 avio_rb32(pb); /* vendor */
1609
1610 st->codec->channels = avio_rb16(pb); /* channel count */
1611 st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1612 av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
1613
1614 sc->audio_cid = avio_rb16(pb);
1615 avio_rb16(pb); /* packet size = 0 */
1616
1617 st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1618
1619 // Read QT version 1 fields. In version 0 these do not exist.
1620 av_dlog(c->fc, "version =%d, isom =%d\n", version, c->isom);
1621 if (!c->isom ||
1622 (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1623
1624 if (version == 1) {
1625 sc->samples_per_frame = avio_rb32(pb);
1626 avio_rb32(pb); /* bytes per packet */
1627 sc->bytes_per_frame = avio_rb32(pb);
1628 avio_rb32(pb); /* bytes per sample */
1629 } else if (version == 2) {
1630 avio_rb32(pb); /* sizeof struct only */
1631 st->codec->sample_rate = av_int2double(avio_rb64(pb));
1632 st->codec->channels = avio_rb32(pb);
1633 avio_rb32(pb); /* always 0x7F000000 */
1634 st->codec->bits_per_coded_sample = avio_rb32(pb);
1635
1636 flags = avio_rb32(pb); /* lpcm format specific flag */
1637 sc->bytes_per_frame = avio_rb32(pb);
1638 sc->samples_per_frame = avio_rb32(pb);
1639 if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1640 st->codec->codec_id =
1641 ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample,
1642 flags);
1643 }
1644 }
1645
1646 switch (st->codec->codec_id) {
1647 case AV_CODEC_ID_PCM_S8:
1648 case AV_CODEC_ID_PCM_U8:
1649 if (st->codec->bits_per_coded_sample == 16)
1650 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
1651 break;
1652 case AV_CODEC_ID_PCM_S16LE:
1653 case AV_CODEC_ID_PCM_S16BE:
1654 if (st->codec->bits_per_coded_sample == 8)
1655 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
1656 else if (st->codec->bits_per_coded_sample == 24)
1657 st->codec->codec_id =
1658 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
1659 AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
1660 break;
1661 /* set values for old format before stsd version 1 appeared */
1662 case AV_CODEC_ID_MACE3:
1663 sc->samples_per_frame = 6;
1664 sc->bytes_per_frame = 2 * st->codec->channels;
1665 break;
1666 case AV_CODEC_ID_MACE6:
1667 sc->samples_per_frame = 6;
1668 sc->bytes_per_frame = 1 * st->codec->channels;
1669 break;
1670 case AV_CODEC_ID_ADPCM_IMA_QT:
1671 sc->samples_per_frame = 64;
1672 sc->bytes_per_frame = 34 * st->codec->channels;
1673 break;
1674 case AV_CODEC_ID_GSM:
1675 sc->samples_per_frame = 160;
1676 sc->bytes_per_frame = 33;
1677 break;
1678 default:
1679 break;
1680 }
1681
1682 bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1683 if (bits_per_sample) {
1684 st->codec->bits_per_coded_sample = bits_per_sample;
1685 sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1686 }
1687}
1688
1689static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb,
1690 AVStream *st, MOVStreamContext *sc,
1691 int size)
1692{
1693 // ttxt stsd contains display flags, justification, background
1694 // color, fonts, and default styles, so fake an atom to read it
1695 MOVAtom fake_atom = { .size = size };
1696 // mp4s contains a regular esds atom
1697 if (st->codec->codec_tag != AV_RL32("mp4s"))
1698 mov_read_glbl(c, pb, fake_atom);
1699 st->codec->width = sc->width;
1700 st->codec->height = sc->height;
1701}
1702
1703static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb,
1704 AVStream *st, MOVStreamContext *sc,
1705 int size)
1706{
1707 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1708 if (ff_alloc_extradata(st->codec, size))
1709 return AVERROR(ENOMEM);
1710 avio_read(pb, st->codec->extradata, size);
1711 if (size > 16) {
1712 MOVStreamContext *tmcd_ctx = st->priv_data;
1713 int val;
1714 val = AV_RB32(st->codec->extradata + 4);
1715 tmcd_ctx->tmcd_flags = val;
1716 if (val & 1)
1717 st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
1718 st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1719 st->codec->time_base.num = 1;
1720 }
1721 } else {
1722 /* other codec type, just skip (rtp, mp4s ...) */
1723 avio_skip(pb, size);
1724 }
1725 return 0;
1726}
1727
1728static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
1729 AVStream *st, MOVStreamContext *sc)
1730{
1731 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1732 !st->codec->sample_rate && sc->time_scale > 1)
1733 st->codec->sample_rate = sc->time_scale;
1734
1735 /* special codec parameters handling */
1736 switch (st->codec->codec_id) {
1737#if CONFIG_DV_DEMUXER
1738 case AV_CODEC_ID_DVAUDIO:
1739 c->dv_fctx = avformat_alloc_context();
1740 c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
1741 if (!c->dv_demux) {
1742 av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1743 return AVERROR(ENOMEM);
1744 }
1745 sc->dv_audio_container = 1;
1746 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
1747 break;
1748#endif
1749 /* no ifdef since parameters are always those */
1750 case AV_CODEC_ID_QCELP:
1751 st->codec->channels = 1;
1752 // force sample rate for qcelp when not stored in mov
1753 if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
1754 st->codec->sample_rate = 8000;
1755 break;
1756 case AV_CODEC_ID_AMR_NB:
1757 st->codec->channels = 1;
1758 /* force sample rate for amr, stsd in 3gp does not store sample rate */
1759 st->codec->sample_rate = 8000;
1760 break;
1761 case AV_CODEC_ID_AMR_WB:
1762 st->codec->channels = 1;
1763 st->codec->sample_rate = 16000;
1764 break;
1765 case AV_CODEC_ID_MP2:
1766 case AV_CODEC_ID_MP3:
1767 /* force type after stsd for m1a hdlr */
1768 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1769 st->need_parsing = AVSTREAM_PARSE_FULL;
1770 break;
1771 case AV_CODEC_ID_GSM:
1772 case AV_CODEC_ID_ADPCM_MS:
1773 case AV_CODEC_ID_ADPCM_IMA_WAV:
1774 case AV_CODEC_ID_ILBC:
1775 case AV_CODEC_ID_MACE3:
1776 case AV_CODEC_ID_MACE6:
1777 case AV_CODEC_ID_QDM2:
1778 st->codec->block_align = sc->bytes_per_frame;
1779 break;
1780 case AV_CODEC_ID_ALAC:
1781 if (st->codec->extradata_size == 36) {
1782 st->codec->channels = AV_RB8 (st->codec->extradata + 21);
1783 st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
1784 }
1785 break;
1786 case AV_CODEC_ID_AC3:
1787 case AV_CODEC_ID_EAC3:
1788 st->need_parsing = AVSTREAM_PARSE_FULL;
1789 break;
1790 case AV_CODEC_ID_MPEG1VIDEO:
1791 st->need_parsing = AVSTREAM_PARSE_FULL;
1792 break;
1793 case AV_CODEC_ID_VC1:
1794 st->need_parsing = AVSTREAM_PARSE_FULL;
1795 break;
1796 case AV_CODEC_ID_HEVC:
1797 st->need_parsing = AVSTREAM_PARSE_HEADERS;
1798 break;
1799 default:
1800 break;
1801 }
1802 return 0;
1803}
1804
1805static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1806 int codec_tag, int format,
1807 int size)
1808{
1809 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1810
1811 if (codec_tag &&
1812 (codec_tag != format &&
1813 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1814 : codec_tag != MKTAG('j','p','e','g')))) {
1815 /* Multiple fourcc, we skip JPEG. This is not correct, we should
1816 * export it as a separate AVStream but this needs a few changes
1817 * in the MOV demuxer, patch welcome. */
1818
1819 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1820 avio_skip(pb, size);
1821 return 1;
1822 }
1823 if ( codec_tag == AV_RL32("avc1") ||
1824 codec_tag == AV_RL32("hvc1") ||
1825 codec_tag == AV_RL32("hev1")
1826 )
1827 av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
1828
1829 return 0;
1830}
1831
1832int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
1833{
1834 AVStream *st;
1835 MOVStreamContext *sc;
1836 int pseudo_stream_id;
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 for (pseudo_stream_id = 0;
1844 pseudo_stream_id < entries && !pb->eof_reached;
1845 pseudo_stream_id++) {
1846 //Parsing Sample description table
1847 enum AVCodecID id;
1848 int ret, dref_id = 1;
1849 MOVAtom a = { AV_RL32("stsd") };
1850 int64_t start_pos = avio_tell(pb);
1851 int64_t size = avio_rb32(pb); /* size */
1852 uint32_t format = avio_rl32(pb); /* data format */
1853
1854 if (size >= 16) {
1855 avio_rb32(pb); /* reserved */
1856 avio_rb16(pb); /* reserved */
1857 dref_id = avio_rb16(pb);
1858 }else if (size <= 7){
1859 av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
1860 return AVERROR_INVALIDDATA;
1861 }
1862
1863 if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1864 size - (avio_tell(pb) - start_pos)))
1865 continue;
1866
1867 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
1868 sc->dref_id= dref_id;
1869
1870 id = mov_codec_id(st, format);
1871
1872 av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
1873 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
1874 (format >> 24) & 0xff, st->codec->codec_type);
1875
1876 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
1877 st->codec->codec_id = id;
1878 mov_parse_stsd_video(c, pb, st, sc);
1879 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
1880 st->codec->codec_id = id;
1881 mov_parse_stsd_audio(c, pb, st, sc);
1882 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
1883 st->codec->codec_id = id;
1884 mov_parse_stsd_subtitle(c, pb, st, sc,
1885 size - (avio_tell(pb) - start_pos));
1886 } else {
1887 ret = mov_parse_stsd_data(c, pb, st, sc,
1888 size - (avio_tell(pb) - start_pos));
1889 if (ret < 0)
1890 return ret;
1891 }
1892 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1893 a.size = size - (avio_tell(pb) - start_pos);
1894 if (a.size > 8) {
1895 if ((ret = mov_read_default(c, pb, a)) < 0)
1896 return ret;
1897 } else if (a.size > 0)
1898 avio_skip(pb, a.size);
1899 }
1900
1901 if (pb->eof_reached)
1902 return AVERROR_EOF;
1903
1904 return mov_finalize_stsd_codec(c, pb, st, sc);
1905}
1906
1907static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1908{
1909 int entries;
1910
1911 avio_r8(pb); /* version */
1912 avio_rb24(pb); /* flags */
1913 entries = avio_rb32(pb);
1914
1915 return ff_mov_read_stsd_entries(c, pb, entries);
1916}
1917
1918static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1919{
1920 AVStream *st;
1921 MOVStreamContext *sc;
1922 unsigned int i, entries;
1923
1924 if (c->fc->nb_streams < 1)
1925 return 0;
1926 st = c->fc->streams[c->fc->nb_streams-1];
1927 sc = st->priv_data;
1928
1929 avio_r8(pb); /* version */
1930 avio_rb24(pb); /* flags */
1931
1932 entries = avio_rb32(pb);
1933
1934 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1935
1936 if (!entries)
1937 return 0;
1938 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1939 return AVERROR_INVALIDDATA;
1940 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1941 if (!sc->stsc_data)
1942 return AVERROR(ENOMEM);
1943
1944 for (i = 0; i < entries && !pb->eof_reached; i++) {
1945 sc->stsc_data[i].first = avio_rb32(pb);
1946 sc->stsc_data[i].count = avio_rb32(pb);
1947 sc->stsc_data[i].id = avio_rb32(pb);
1948 }
1949
1950 sc->stsc_count = i;
1951
1952 if (pb->eof_reached)
1953 return AVERROR_EOF;
1954
1955 return 0;
1956}
1957
1958static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1959{
1960 AVStream *st;
1961 MOVStreamContext *sc;
1962 unsigned i, entries;
1963
1964 if (c->fc->nb_streams < 1)
1965 return 0;
1966 st = c->fc->streams[c->fc->nb_streams-1];
1967 sc = st->priv_data;
1968
1969 avio_rb32(pb); // version + flags
1970
1971 entries = avio_rb32(pb);
1972 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1973 return AVERROR_INVALIDDATA;
1974 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1975 if (!sc->stps_data)
1976 return AVERROR(ENOMEM);
1977
1978 for (i = 0; i < entries && !pb->eof_reached; i++) {
1979 sc->stps_data[i] = avio_rb32(pb);
1980 //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1981 }
1982
1983 sc->stps_count = i;
1984
1985 if (pb->eof_reached)
1986 return AVERROR_EOF;
1987
1988 return 0;
1989}
1990
1991static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1992{
1993 AVStream *st;
1994 MOVStreamContext *sc;
1995 unsigned int i, entries;
1996
1997 if (c->fc->nb_streams < 1)
1998 return 0;
1999 st = c->fc->streams[c->fc->nb_streams-1];
2000 sc = st->priv_data;
2001
2002 avio_r8(pb); /* version */
2003 avio_rb24(pb); /* flags */
2004
2005 entries = avio_rb32(pb);
2006
2007 av_dlog(c->fc, "keyframe_count = %d\n", entries);
2008
2009 if (!entries)
2010 {
2011 sc->keyframe_absent = 1;
2012 if (!st->need_parsing)
2013 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2014 return 0;
2015 }
2016 if (entries >= UINT_MAX / sizeof(int))
2017 return AVERROR_INVALIDDATA;
2018 sc->keyframes = av_malloc(entries * sizeof(int));
2019 if (!sc->keyframes)
2020 return AVERROR(ENOMEM);
2021
2022 for (i = 0; i < entries && !pb->eof_reached; i++) {
2023 sc->keyframes[i] = avio_rb32(pb);
2024 //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
2025 }
2026
2027 sc->keyframe_count = i;
2028
2029 if (pb->eof_reached)
2030 return AVERROR_EOF;
2031
2032 return 0;
2033}
2034
2035static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2036{
2037 AVStream *st;
2038 MOVStreamContext *sc;
2039 unsigned int i, entries, sample_size, field_size, num_bytes;
2040 GetBitContext gb;
2041 unsigned char* buf;
2042
2043 if (c->fc->nb_streams < 1)
2044 return 0;
2045 st = c->fc->streams[c->fc->nb_streams-1];
2046 sc = st->priv_data;
2047
2048 avio_r8(pb); /* version */
2049 avio_rb24(pb); /* flags */
2050
2051 if (atom.type == MKTAG('s','t','s','z')) {
2052 sample_size = avio_rb32(pb);
2053 if (!sc->sample_size) /* do not overwrite value computed in stsd */
2054 sc->sample_size = sample_size;
2055 sc->stsz_sample_size = sample_size;
2056 field_size = 32;
2057 } else {
2058 sample_size = 0;
2059 avio_rb24(pb); /* reserved */
2060 field_size = avio_r8(pb);
2061 }
2062 entries = avio_rb32(pb);
2063
2064 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2065
2066 sc->sample_count = entries;
2067 if (sample_size)
2068 return 0;
2069
2070 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2071 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2072 return AVERROR_INVALIDDATA;
2073 }
2074
2075 if (!entries)
2076 return 0;
2077 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
2078 return AVERROR_INVALIDDATA;
2079 sc->sample_sizes = av_malloc(entries * sizeof(int));
2080 if (!sc->sample_sizes)
2081 return AVERROR(ENOMEM);
2082
2083 num_bytes = (entries*field_size+4)>>3;
2084
2085 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
2086 if (!buf) {
2087 av_freep(&sc->sample_sizes);
2088 return AVERROR(ENOMEM);
2089 }
2090
2091 if (avio_read(pb, buf, num_bytes) < num_bytes) {
2092 av_freep(&sc->sample_sizes);
2093 av_free(buf);
2094 return AVERROR_INVALIDDATA;
2095 }
2096
2097 init_get_bits(&gb, buf, 8*num_bytes);
2098
2099 for (i = 0; i < entries && !pb->eof_reached; i++) {
2100 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2101 sc->data_size += sc->sample_sizes[i];
2102 }
2103
2104 sc->sample_count = i;
2105
2106 if (pb->eof_reached)
2107 return AVERROR_EOF;
2108
2109 av_free(buf);
2110 return 0;
2111}
2112
2113static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2114{
2115 AVStream *st;
2116 MOVStreamContext *sc;
2117 unsigned int i, entries;
2118 int64_t duration=0;
2119 int64_t total_sample_count=0;
2120
2121 if (c->fc->nb_streams < 1)
2122 return 0;
2123 st = c->fc->streams[c->fc->nb_streams-1];
2124 sc = st->priv_data;
2125
2126 avio_r8(pb); /* version */
2127 avio_rb24(pb); /* flags */
2128 entries = avio_rb32(pb);
2129
2130 av_dlog(c->fc, "track[%i].stts.entries = %i\n",
2131 c->fc->nb_streams-1, entries);
2132
2133 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
2134 return -1;
2135
2136 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
2137 if (!sc->stts_data)
2138 return AVERROR(ENOMEM);
2139
2140 for (i = 0; i < entries && !pb->eof_reached; i++) {
2141 int sample_duration;
2142 int sample_count;
2143
2144 sample_count=avio_rb32(pb);
2145 sample_duration = avio_rb32(pb);
2146
2147 /* sample_duration < 0 is invalid based on the spec */
2148 if (sample_duration < 0) {
2149 av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration);
2150 sample_duration = 1;
2151 }
2152 if (sample_count < 0) {
2153 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2154 return AVERROR_INVALIDDATA;
2155 }
2156 sc->stts_data[i].count= sample_count;
2157 sc->stts_data[i].duration= sample_duration;
2158
2159 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
2160 sample_count, sample_duration);
2161
2162 duration+=(int64_t)sample_duration*sample_count;
2163 total_sample_count+=sample_count;
2164 }
2165
2166 sc->stts_count = i;
2167
2168 if (pb->eof_reached)
2169 return AVERROR_EOF;
2170
2171 st->nb_frames= total_sample_count;
2172 if (duration)
2173 st->duration= duration;
2174 sc->track_end = duration;
2175 return 0;
2176}
2177
2178static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2179{
2180 if (duration < 0) {
2181 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2182 }
2183}
2184
2185static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2186{
2187 AVStream *st;
2188 MOVStreamContext *sc;
2189 unsigned int i, entries;
2190
2191 if (c->fc->nb_streams < 1)
2192 return 0;
2193 st = c->fc->streams[c->fc->nb_streams-1];
2194 sc = st->priv_data;
2195
2196 avio_r8(pb); /* version */
2197 avio_rb24(pb); /* flags */
2198 entries = avio_rb32(pb);
2199
2200 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2201
2202 if (!entries)
2203 return 0;
2204 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2205 return AVERROR_INVALIDDATA;
2206 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
2207 if (!sc->ctts_data)
2208 return AVERROR(ENOMEM);
2209
2210 for (i = 0; i < entries && !pb->eof_reached; i++) {
2211 int count =avio_rb32(pb);
2212 int duration =avio_rb32(pb);
2213
2214 sc->ctts_data[i].count = count;
2215 sc->ctts_data[i].duration= duration;
2216
2217 av_dlog(c->fc, "count=%d, duration=%d\n",
2218 count, duration);
2219
2220 if (FFABS(duration) > (1<<28) && i+2<entries) {
2221 av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2222 av_freep(&sc->ctts_data);
2223 sc->ctts_count = 0;
2224 return 0;
2225 }
2226
2227 if (i+2<entries)
2228 mov_update_dts_shift(sc, duration);
2229 }
2230
2231 sc->ctts_count = i;
2232
2233 if (pb->eof_reached)
2234 return AVERROR_EOF;
2235
2236 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
2237
2238 return 0;
2239}
2240
2241static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2242{
2243 AVStream *st;
2244 MOVStreamContext *sc;
2245 unsigned int i, entries;
2246 uint8_t version;
2247 uint32_t grouping_type;
2248
2249 if (c->fc->nb_streams < 1)
2250 return 0;
2251 st = c->fc->streams[c->fc->nb_streams-1];
2252 sc = st->priv_data;
2253
2254 version = avio_r8(pb); /* version */
2255 avio_rb24(pb); /* flags */
2256 grouping_type = avio_rl32(pb);
2257 if (grouping_type != MKTAG( 'r','a','p',' '))
2258 return 0; /* only support 'rap ' grouping */
2259 if (version == 1)
2260 avio_rb32(pb); /* grouping_type_parameter */
2261
2262 entries = avio_rb32(pb);
2263 if (!entries)
2264 return 0;
2265 if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2266 return AVERROR_INVALIDDATA;
2267 sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2268 if (!sc->rap_group)
2269 return AVERROR(ENOMEM);
2270
2271 for (i = 0; i < entries && !pb->eof_reached; i++) {
2272 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2273 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2274 }
2275
2276 sc->rap_group_count = i;
2277
2278 return pb->eof_reached ? AVERROR_EOF : 0;
2279}
2280
2281static void mov_build_index(MOVContext *mov, AVStream *st)
2282{
2283 MOVStreamContext *sc = st->priv_data;
2284 int64_t current_offset;
2285 int64_t current_dts = 0;
2286 unsigned int stts_index = 0;
2287 unsigned int stsc_index = 0;
2288 unsigned int stss_index = 0;
2289 unsigned int stps_index = 0;
2290 unsigned int i, j;
2291 uint64_t stream_size = 0;
2292
2293 /* adjust first dts according to edit list */
2294 if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
2295 if (sc->empty_duration)
2296 sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
2297 sc->time_offset = sc->start_time - sc->empty_duration;
2298 current_dts = -sc->time_offset;
2299 if (sc->ctts_count>0 && sc->stts_count>0 &&
2300 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2301 /* more than 16 frames delay, dts are likely wrong
2302 this happens with files created by iMovie */
2303 sc->wrong_dts = 1;
2304 st->codec->has_b_frames = 1;
2305 }
2306 }
2307
2308 /* only use old uncompressed audio chunk demuxing when stts specifies it */
2309 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2310 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2311 unsigned int current_sample = 0;
2312 unsigned int stts_sample = 0;
2313 unsigned int sample_size;
2314 unsigned int distance = 0;
2315 unsigned int rap_group_index = 0;
2316 unsigned int rap_group_sample = 0;
2317 int rap_group_present = sc->rap_group_count && sc->rap_group;
2318 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2319
2320 current_dts -= sc->dts_shift;
2321
2322 if (!sc->sample_count || st->nb_index_entries)
2323 return;
2324 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2325 return;
2326 if (av_reallocp_array(&st->index_entries,
2327 st->nb_index_entries + sc->sample_count,
2328 sizeof(*st->index_entries)) < 0) {
2329 st->nb_index_entries = 0;
2330 return;
2331 }
2332 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2333
2334 for (i = 0; i < sc->chunk_count; i++) {
2335 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2336 current_offset = sc->chunk_offsets[i];
2337 while (stsc_index + 1 < sc->stsc_count &&
2338 i + 1 == sc->stsc_data[stsc_index + 1].first)
2339 stsc_index++;
2340
2341 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2342 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2343 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2344 sc->stsz_sample_size = sc->sample_size;
2345 }
2346 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2347 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2348 sc->stsz_sample_size = sc->sample_size;
2349 }
2350
2351 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2352 int keyframe = 0;
2353 if (current_sample >= sc->sample_count) {
2354 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2355 return;
2356 }
2357
2358 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2359 keyframe = 1;
2360 if (stss_index + 1 < sc->keyframe_count)
2361 stss_index++;
2362 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2363 keyframe = 1;
2364 if (stps_index + 1 < sc->stps_count)
2365 stps_index++;
2366 }
2367 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2368 if (sc->rap_group[rap_group_index].index > 0)
2369 keyframe = 1;
2370 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2371 rap_group_sample = 0;
2372 rap_group_index++;
2373 }
2374 }
2375 if (keyframe)
2376 distance = 0;
2377 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2378 if (sc->pseudo_stream_id == -1 ||
2379 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2380 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2381 e->pos = current_offset;
2382 e->timestamp = current_dts;
2383 e->size = sample_size;
2384 e->min_distance = distance;
2385 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2386 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2387 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2388 current_offset, current_dts, sample_size, distance, keyframe);
2389 }
2390
2391 current_offset += sample_size;
2392 stream_size += sample_size;
2393 current_dts += sc->stts_data[stts_index].duration;
2394 distance++;
2395 stts_sample++;
2396 current_sample++;
2397 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2398 stts_sample = 0;
2399 stts_index++;
2400 }
2401 }
2402 }
2403 if (st->duration > 0)
2404 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2405 } else {
2406 unsigned chunk_samples, total = 0;
2407
2408 // compute total chunk count
2409 for (i = 0; i < sc->stsc_count; i++) {
2410 unsigned count, chunk_count;
2411
2412 chunk_samples = sc->stsc_data[i].count;
2413 if (i != sc->stsc_count - 1 &&
2414 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2415 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2416 return;
2417 }
2418
2419 if (sc->samples_per_frame >= 160) { // gsm
2420 count = chunk_samples / sc->samples_per_frame;
2421 } else if (sc->samples_per_frame > 1) {
2422 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2423 count = (chunk_samples+samples-1) / samples;
2424 } else {
2425 count = (chunk_samples+1023) / 1024;
2426 }
2427
2428 if (i < sc->stsc_count - 1)
2429 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2430 else
2431 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2432 total += chunk_count * count;
2433 }
2434
2435 av_dlog(mov->fc, "chunk count %d\n", total);
2436 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2437 return;
2438 if (av_reallocp_array(&st->index_entries,
2439 st->nb_index_entries + total,
2440 sizeof(*st->index_entries)) < 0) {
2441 st->nb_index_entries = 0;
2442 return;
2443 }
2444 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2445
2446 // populate index
2447 for (i = 0; i < sc->chunk_count; i++) {
2448 current_offset = sc->chunk_offsets[i];
2449 if (stsc_index + 1 < sc->stsc_count &&
2450 i + 1 == sc->stsc_data[stsc_index + 1].first)
2451 stsc_index++;
2452 chunk_samples = sc->stsc_data[stsc_index].count;
2453
2454 while (chunk_samples > 0) {
2455 AVIndexEntry *e;
2456 unsigned size, samples;
2457
2458 if (sc->samples_per_frame >= 160) { // gsm
2459 samples = sc->samples_per_frame;
2460 size = sc->bytes_per_frame;
2461 } else {
2462 if (sc->samples_per_frame > 1) {
2463 samples = FFMIN((1024 / sc->samples_per_frame)*
2464 sc->samples_per_frame, chunk_samples);
2465 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2466 } else {
2467 samples = FFMIN(1024, chunk_samples);
2468 size = samples * sc->sample_size;
2469 }
2470 }
2471
2472 if (st->nb_index_entries >= total) {
2473 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2474 return;
2475 }
2476 e = &st->index_entries[st->nb_index_entries++];
2477 e->pos = current_offset;
2478 e->timestamp = current_dts;
2479 e->size = size;
2480 e->min_distance = 0;
2481 e->flags = AVINDEX_KEYFRAME;
2482 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2483 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2484 size, samples);
2485
2486 current_offset += size;
2487 current_dts += samples;
2488 chunk_samples -= samples;
2489 }
2490 }
2491 }
2492}
2493
2494static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
2495 AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
2496{
2497 /* try relative path, we do not try the absolute because it can leak information about our
2498 system to an attacker */
2499 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2500 char filename[1024];
2501 const char *src_path;
2502 int i, l;
2503
2504 /* find a source dir */
2505 src_path = strrchr(src, '/');
2506 if (src_path)
2507 src_path++;
2508 else
2509 src_path = src;
2510
2511 /* find a next level down to target */
2512 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2513 if (ref->path[l] == '/') {
2514 if (i == ref->nlvl_to - 1)
2515 break;
2516 else
2517 i++;
2518 }
2519
2520 /* compose filename if next level down to target was found */
2521 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2522 memcpy(filename, src, src_path - src);
2523 filename[src_path - src] = 0;
2524
2525 for (i = 1; i < ref->nlvl_from; i++)
2526 av_strlcat(filename, "../", 1024);
2527
2528 av_strlcat(filename, ref->path + l + 1, 1024);
2529
2530 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2531 return 0;
2532 }
2533 } else if (use_absolute_path) {
2534 av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
2535 "this is a possible security issue\n");
2536 if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2537 return 0;
2538 }
2539
2540 return AVERROR(ENOENT);
2541}
2542
2543static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
2544{
2545 if (sc->time_scale <= 0) {
2546 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
2547 sc->time_scale = c->time_scale;
2548 if (sc->time_scale <= 0)
2549 sc->time_scale = 1;
2550 }
2551}
2552
2553static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2554{
2555 AVStream *st;
2556 MOVStreamContext *sc;
2557 int ret;
2558
2559 st = avformat_new_stream(c->fc, NULL);
2560 if (!st) return AVERROR(ENOMEM);
2561 st->id = c->fc->nb_streams;
2562 sc = av_mallocz(sizeof(MOVStreamContext));
2563 if (!sc) return AVERROR(ENOMEM);
2564
2565 st->priv_data = sc;
2566 st->codec->codec_type = AVMEDIA_TYPE_DATA;
2567 sc->ffindex = st->index;
2568 c->trak_index = st->index;
2569
2570 if ((ret = mov_read_default(c, pb, atom)) < 0)
2571 return ret;
2572
2573 c->trak_index = -1;
2574
2575 /* sanity checks */
2576 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2577 (!sc->sample_size && !sc->sample_count))) {
2578 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2579 st->index);
2580 return 0;
2581 }
2582
2583 fix_timescale(c, sc);
2584
2585 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2586
2587 mov_build_index(c, st);
2588
2589 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2590 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2591 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
2592 c->use_absolute_path, c->fc) < 0)
2593 av_log(c->fc, AV_LOG_ERROR,
2594 "stream %d, error opening alias: path='%s', dir='%s', "
2595 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2596 st->index, dref->path, dref->dir, dref->filename,
2597 dref->volume, dref->nlvl_from, dref->nlvl_to);
2598 } else {
2599 sc->pb = c->fc->pb;
2600 sc->pb_is_copied = 1;
2601 }
2602
2603 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2604 if (!st->sample_aspect_ratio.num &&
2605 (st->codec->width != sc->width || st->codec->height != sc->height)) {
2606 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2607 ((double)st->codec->width * sc->height), INT_MAX);
2608 }
2609
2610 if (st->duration > 0)
2611 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2612 sc->time_scale*st->nb_frames, st->duration, INT_MAX);
2613
2614#if FF_API_R_FRAME_RATE
2615 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
2616 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2617 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
2618#endif
2619 }
2620
2621 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2622 if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2623 st->codec->codec_tag != MKTAG('a', 'v', 'c', '1')) {
2624 ff_generate_avci_extradata(st);
2625 }
2626
2627 switch (st->codec->codec_id) {
2628#if CONFIG_H261_DECODER
2629 case AV_CODEC_ID_H261:
2630#endif
2631#if CONFIG_H263_DECODER
2632 case AV_CODEC_ID_H263:
2633#endif
2634#if CONFIG_MPEG4_DECODER
2635 case AV_CODEC_ID_MPEG4:
2636#endif
2637 st->codec->width = 0; /* let decoder init width/height */
2638 st->codec->height= 0;
2639 break;
2640 }
2641
2642 /* Do not need those anymore. */
2643 av_freep(&sc->chunk_offsets);
2644 av_freep(&sc->stsc_data);
2645 av_freep(&sc->sample_sizes);
2646 av_freep(&sc->keyframes);
2647 av_freep(&sc->stts_data);
2648 av_freep(&sc->stps_data);
2649 av_freep(&sc->rap_group);
2650
2651 return 0;
2652}
2653
2654static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2655{
2656 int ret;
2657 c->itunes_metadata = 1;
2658 ret = mov_read_default(c, pb, atom);
2659 c->itunes_metadata = 0;
2660 return ret;
2661}
2662
2663static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2664{
2665 uint32_t count;
2666 uint32_t i;
2667
2668 if (atom.size < 8)
2669 return 0;
2670
2671 avio_skip(pb, 4);
2672 count = avio_rb32(pb);
2673 if (count > UINT_MAX / sizeof(*c->meta_keys)) {
2674 av_log(c->fc, AV_LOG_ERROR,
2675 "The 'keys' atom with the invalid key count: %d\n", count);
2676 return AVERROR_INVALIDDATA;
2677 }
2678
2679 c->meta_keys_count = count + 1;
2680 c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
2681 if (!c->meta_keys)
2682 return AVERROR(ENOMEM);
2683
2684 for (i = 1; i <= count; ++i) {
2685 uint32_t key_size = avio_rb32(pb);
2686 uint32_t type = avio_rl32(pb);
2687 if (key_size < 8) {
2688 av_log(c->fc, AV_LOG_ERROR,
2689 "The key# %d in meta has invalid size: %d\n", i, key_size);
2690 return AVERROR_INVALIDDATA;
2691 }
2692 key_size -= 8;
2693 if (type != MKTAG('m','d','t','a')) {
2694 avio_skip(pb, key_size);
2695 }
2696 c->meta_keys[i] = av_mallocz(key_size + 1);
2697 if (!c->meta_keys[i])
2698 return AVERROR(ENOMEM);
2699 avio_read(pb, c->meta_keys[i], key_size);
2700 }
2701
2702 return 0;
2703}
2704
2705static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2706{
2707 while (atom.size > 8) {
2708 uint32_t tag = avio_rl32(pb);
2709 atom.size -= 4;
2710 if (tag == MKTAG('h','d','l','r')) {
2711 avio_seek(pb, -8, SEEK_CUR);
2712 atom.size += 8;
2713 return mov_read_default(c, pb, atom);
2714 }
2715 }
2716 return 0;
2717}
2718
2719static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2720{
2721 int i;
2722 int width;
2723 int height;
2724 int64_t disp_transform[2];
2725 int display_matrix[3][2];
2726 AVStream *st;
2727 MOVStreamContext *sc;
2728 int version;
2729 int flags;
2730
2731 if (c->fc->nb_streams < 1)
2732 return 0;
2733 st = c->fc->streams[c->fc->nb_streams-1];
2734 sc = st->priv_data;
2735
2736 version = avio_r8(pb);
2737 flags = avio_rb24(pb);
2738 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
2739
2740 if (version == 1) {
2741 avio_rb64(pb);
2742 avio_rb64(pb);
2743 } else {
2744 avio_rb32(pb); /* creation time */
2745 avio_rb32(pb); /* modification time */
2746 }
2747 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2748 avio_rb32(pb); /* reserved */
2749
2750 /* highlevel (considering edits) duration in movie timebase */
2751 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2752 avio_rb32(pb); /* reserved */
2753 avio_rb32(pb); /* reserved */
2754
2755 avio_rb16(pb); /* layer */
2756 avio_rb16(pb); /* alternate group */
2757 avio_rb16(pb); /* volume */
2758 avio_rb16(pb); /* reserved */
2759
2760 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2761 // they're kept in fixed point format through all calculations
2762 // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
2763 for (i = 0; i < 3; i++) {
2764 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2765 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2766 avio_rb32(pb); // 2.30 fixed point (not used)
2767 }
2768
2769 width = avio_rb32(pb); // 16.16 fixed point track width
2770 height = avio_rb32(pb); // 16.16 fixed point track height
2771 sc->width = width >> 16;
2772 sc->height = height >> 16;
2773
2774 //Assign clockwise rotate values based on transform matrix so that
2775 //we can compensate for iPhone orientation during capture.
2776
2777 if (display_matrix[0][0] == 65536 && display_matrix[1][1] == 65536) {
2778 av_dict_set(&st->metadata, "rotate", "0", 0);
2779 st->rotation_degree = 0;
2780 }
2781
2782 if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) {
2783 av_dict_set(&st->metadata, "rotate", "90", 0);
2784 st->rotation_degree = 1;
2785 }
2786
2787 if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
2788 av_dict_set(&st->metadata, "rotate", "180", 0);
2789 st->rotation_degree = 2;
2790 }
2791
2792 if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) {
2793 av_dict_set(&st->metadata, "rotate", "270", 0);
2794 st->rotation_degree = 3;
2795 }
2796
2797 // transform the display width/height according to the matrix
2798 // skip this if the display matrix is the default identity matrix
2799 // or if it is rotating the picture, ex iPhone 3GS
2800 // to keep the same scale, use [width height 1<<16]
2801 if (width && height &&
2802 ((display_matrix[0][0] != 65536 ||
2803 display_matrix[1][1] != 65536) &&
2804 !display_matrix[0][1] &&
2805 !display_matrix[1][0] &&
2806 !display_matrix[2][0] && !display_matrix[2][1])) {
2807 for (i = 0; i < 2; i++)
2808 disp_transform[i] =
2809 (int64_t) width * display_matrix[0][i] +
2810 (int64_t) height * display_matrix[1][i] +
2811 ((int64_t) display_matrix[2][i] << 16);
2812
2813 //sample aspect ratio is new width/height divided by old width/height
2814 st->sample_aspect_ratio = av_d2q(
2815 ((double) disp_transform[0] * height) /
2816 ((double) disp_transform[1] * width), INT_MAX);
2817 }
2818 return 0;
2819}
2820
2821static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2822{
2823 MOVFragment *frag = &c->fragment;
2824 MOVTrackExt *trex = NULL;
2825 int flags, track_id, i;
2826
2827 avio_r8(pb); /* version */
2828 flags = avio_rb24(pb);
2829
2830 track_id = avio_rb32(pb);
2831 if (!track_id)
2832 return AVERROR_INVALIDDATA;
2833 frag->track_id = track_id;
2834 for (i = 0; i < c->trex_count; i++)
2835 if (c->trex_data[i].track_id == frag->track_id) {
2836 trex = &c->trex_data[i];
2837 break;
2838 }
2839 if (!trex) {
2840 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2841 return AVERROR_INVALIDDATA;
2842 }
2843
2844 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
2845 avio_rb64(pb) : frag->moof_offset;
2846 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2847
2848 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2849 avio_rb32(pb) : trex->duration;
2850 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2851 avio_rb32(pb) : trex->size;
2852 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2853 avio_rb32(pb) : trex->flags;
2854 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2855 return 0;
2856}
2857
2858static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2859{
2860 c->chapter_track = avio_rb32(pb);
2861 return 0;
2862}
2863
2864static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2865{
2866 MOVTrackExt *trex;
2867 int err;
2868
2869 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2870 return AVERROR_INVALIDDATA;
2871 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2872 sizeof(*c->trex_data))) < 0) {
2873 c->trex_count = 0;
2874 return err;
2875 }
2876
2877 c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
2878
2879 trex = &c->trex_data[c->trex_count++];
2880 avio_r8(pb); /* version */
2881 avio_rb24(pb); /* flags */
2882 trex->track_id = avio_rb32(pb);
2883 trex->stsd_id = avio_rb32(pb);
2884 trex->duration = avio_rb32(pb);
2885 trex->size = avio_rb32(pb);
2886 trex->flags = avio_rb32(pb);
2887 return 0;
2888}
2889
2890static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2891{
2892 MOVFragment *frag = &c->fragment;
2893 AVStream *st = NULL;
2894 MOVStreamContext *sc;
2895 MOVStts *ctts_data;
2896 uint64_t offset;
2897 int64_t dts;
2898 int data_offset = 0;
2899 unsigned entries, first_sample_flags = frag->flags;
2900 int flags, distance, i, found_keyframe = 0, err;
2901
2902 for (i = 0; i < c->fc->nb_streams; i++) {
2903 if (c->fc->streams[i]->id == frag->track_id) {
2904 st = c->fc->streams[i];
2905 break;
2906 }
2907 }
2908 if (!st) {
2909 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2910 return AVERROR_INVALIDDATA;
2911 }
2912 sc = st->priv_data;
2913 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
2914 return 0;
2915 avio_r8(pb); /* version */
2916 flags = avio_rb24(pb);
2917 entries = avio_rb32(pb);
2918 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2919
2920 /* Always assume the presence of composition time offsets.
2921 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2922 * 1) in the initial movie, there are no samples.
2923 * 2) in the first movie fragment, there is only one sample without composition time offset.
2924 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2925 if (!sc->ctts_count && sc->sample_count)
2926 {
2927 /* Complement ctts table if moov atom doesn't have ctts atom. */
2928 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2929 if (!ctts_data)
2930 return AVERROR(ENOMEM);
2931 sc->ctts_data = ctts_data;
2932 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2933 sc->ctts_data[sc->ctts_count].duration = 0;
2934 sc->ctts_count++;
2935 }
2936 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2937 return AVERROR_INVALIDDATA;
2938 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2939 sizeof(*sc->ctts_data))) < 0) {
2940 sc->ctts_count = 0;
2941 return err;
2942 }
2943 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
2944 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
2945 dts = sc->track_end - sc->time_offset;
2946 offset = frag->base_data_offset + data_offset;
2947 distance = 0;
2948 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
2949 for (i = 0; i < entries && !pb->eof_reached; i++) {
2950 unsigned sample_size = frag->size;
2951 int sample_flags = i ? frag->flags : first_sample_flags;
2952 unsigned sample_duration = frag->duration;
2953 int keyframe = 0;
2954
2955 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2956 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
2957 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
2958 sc->ctts_data[sc->ctts_count].count = 1;
2959 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2960 avio_rb32(pb) : 0;
2961 mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
2962 sc->ctts_count++;
2963 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2964 keyframe = 1;
2965 else if (!found_keyframe)
2966 keyframe = found_keyframe =
2967 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2968 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
2969 if (keyframe)
2970 distance = 0;
2971 av_add_index_entry(st, offset, dts, sample_size, distance,
2972 keyframe ? AVINDEX_KEYFRAME : 0);
2973 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2974 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
2975 offset, dts, sample_size, distance, keyframe);
2976 distance++;
2977 dts += sample_duration;
2978 offset += sample_size;
2979 sc->data_size += sample_size;
2980 }
2981
2982 if (pb->eof_reached)
2983 return AVERROR_EOF;
2984
2985 frag->moof_offset = offset;
2986 st->duration = sc->track_end = dts + sc->time_offset;
2987 return 0;
2988}
2989
2990/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2991/* like the files created with Adobe Premiere 5.0, for samples see */
2992/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
2993static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2994{
2995 int err;
2996
2997 if (atom.size < 8)
2998 return 0; /* continue */
2999 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3000 avio_skip(pb, atom.size - 4);
3001 return 0;
3002 }
3003 atom.type = avio_rl32(pb);
3004 atom.size -= 8;
3005 if (atom.type != MKTAG('m','d','a','t')) {
3006 avio_skip(pb, atom.size);
3007 return 0;
3008 }
3009 err = mov_read_mdat(c, pb, atom);
3010 return err;
3011}
3012
3013static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3014{
3015#if CONFIG_ZLIB
3016 AVIOContext ctx;
3017 uint8_t *cmov_data;
3018 uint8_t *moov_data; /* uncompressed data */
3019 long cmov_len, moov_len;
3020 int ret = -1;
3021
3022 avio_rb32(pb); /* dcom atom */
3023 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3024 return AVERROR_INVALIDDATA;
3025 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3026 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3027 return AVERROR_INVALIDDATA;
3028 }
3029 avio_rb32(pb); /* cmvd atom */
3030 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3031 return AVERROR_INVALIDDATA;
3032 moov_len = avio_rb32(pb); /* uncompressed size */
3033 cmov_len = atom.size - 6 * 4;
3034
3035 cmov_data = av_malloc(cmov_len);
3036 if (!cmov_data)
3037 return AVERROR(ENOMEM);
3038 moov_data = av_malloc(moov_len);
3039 if (!moov_data) {
3040 av_free(cmov_data);
3041 return AVERROR(ENOMEM);
3042 }
3043 avio_read(pb, cmov_data, cmov_len);
3044 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3045 goto free_and_return;
3046 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3047 goto free_and_return;
3048 atom.type = MKTAG('m','o','o','v');
3049 atom.size = moov_len;
3050 ret = mov_read_default(c, &ctx, atom);
3051free_and_return:
3052 av_free(moov_data);
3053 av_free(cmov_data);
3054 return ret;
3055#else
3056 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3057 return AVERROR(ENOSYS);
3058#endif
3059}
3060
3061/* edit list atom */
3062static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3063{
3064 MOVStreamContext *sc;
3065 int i, edit_count, version, edit_start_index = 0;
3066 int unsupported = 0;
3067
3068 if (c->fc->nb_streams < 1 || c->ignore_editlist)
3069 return 0;
3070 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3071
3072 version = avio_r8(pb); /* version */
3073 avio_rb24(pb); /* flags */
3074 edit_count = avio_rb32(pb); /* entries */
3075
3076 if ((uint64_t)edit_count*12+8 > atom.size)
3077 return AVERROR_INVALIDDATA;
3078
3079 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3080 for (i=0; i<edit_count; i++){
3081 int64_t time;
3082 int64_t duration;
3083 int rate;
3084 if (version == 1) {
3085 duration = avio_rb64(pb);
3086 time = avio_rb64(pb);
3087 } else {
3088 duration = avio_rb32(pb); /* segment duration */
3089 time = (int32_t)avio_rb32(pb); /* media time */
3090 }
3091 rate = avio_rb32(pb);
3092 if (i == 0 && time == -1) {
3093 sc->empty_duration = duration;
3094 edit_start_index = 1;
3095 } else if (i == edit_start_index && time >= 0)
3096 sc->start_time = time;
3097 else
3098 unsupported = 1;
3099
3100 av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3101 duration, time, rate / 65536.0);
3102 }
3103
3104 if (unsupported)
3105 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
3106 "a/v desync might occur, patch welcome\n");
3107
3108 return 0;
3109}
3110
3111static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3112{
3113 MOVStreamContext *sc;
3114
3115 if (c->fc->nb_streams < 1)
3116 return AVERROR_INVALIDDATA;
3117 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3118 sc->timecode_track = avio_rb32(pb);
3119 return 0;
3120}
3121
3122static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3123{
3124 int ret;
3125 uint8_t uuid[16];
3126 static const uint8_t uuid_isml_manifest[] = {
3127 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3128 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3129 };
3130
3131 if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3132 return AVERROR_INVALIDDATA;
3133
3134 ret = avio_read(pb, uuid, sizeof(uuid));
3135 if (ret < 0) {
3136 return ret;
3137 } else if (ret != sizeof(uuid)) {
3138 return AVERROR_INVALIDDATA;
3139 }
3140 if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3141 uint8_t *buffer, *ptr;
3142 char *endptr;
3143 size_t len = atom.size - sizeof(uuid);
3144
3145 if (len < 4) {
3146 return AVERROR_INVALIDDATA;
3147 }
3148 ret = avio_skip(pb, 4); // zeroes
3149 len -= 4;
3150
3151 buffer = av_mallocz(len + 1);
3152 if (!buffer) {
3153 return AVERROR(ENOMEM);
3154 }
3155 ret = avio_read(pb, buffer, len);
3156 if (ret < 0) {
3157 av_free(buffer);
3158 return ret;
3159 } else if (ret != len) {
3160 av_free(buffer);
3161 return AVERROR_INVALIDDATA;
3162 }
3163
3164 ptr = buffer;
3165 while ((ptr = av_stristr(ptr, "systemBitrate=\"")) != NULL) {
3166 ptr += sizeof("systemBitrate=\"") - 1;
3167 c->bitrates_count++;
3168 c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3169 if (!c->bitrates) {
3170 c->bitrates_count = 0;
3171 av_free(buffer);
3172 return AVERROR(ENOMEM);
3173 }
3174 errno = 0;
3175 ret = strtol(ptr, &endptr, 10);
3176 if (ret < 0 || errno || *endptr != '"') {
3177 c->bitrates[c->bitrates_count - 1] = 0;
3178 } else {
3179 c->bitrates[c->bitrates_count - 1] = ret;
3180 }
3181 }
3182
3183 av_free(buffer);
3184 }
3185 return 0;
3186}
3187
3188static void mov_id32_date2year(AVDictionary **m)
3189{
3190 AVDictionaryEntry *t = NULL;
3191 if (t = av_dict_get(*m, "date", t, AV_DICT_MATCH_CASE)) {
3192 av_dict_set(m, "year", t->value, 0);
3193 av_log(NULL, AV_LOG_INFO, "[%s:%d]========date:%s\n", __FUNCTION__, __LINE__, t->value);
3194 }
3195}
3196
3197static int mov_read_id32(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3198{
3199 uint8_t version;
3200 uint32_t flags;
3201 uint8_t pad;
3202 uint16_t langcode;
3203 uint16_t shortbytes;
3204 char language[4] = {0};
3205 uint32_t str_size;
3206 AVFormatContext *s = c->fc;
3207 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
3208
3209 str_size = atom.size;
3210 version = avio_r8(pb); // version
3211 flags = avio_rb24(pb); //flags
3212 shortbytes = avio_rb16(pb);
3213 pad = (shortbytes & 0x8000) >> 15; //pad
3214 langcode = shortbytes & 0x7ffff; //language
3215 ff_mov_lang_to_iso639(langcode, language);
3216 str_size -= 6;
3217 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
3218 if (id3v2_extra_meta) {
3219 int err = ff_id3v2_parse_apic(s, &id3v2_extra_meta);
3220 if (err < 0) {
3221 av_log(NULL, AV_LOG_INFO, "[%s:%d]ff_id3v2_parse_apic err:%d\n", __FUNCTION__, __LINE__, err);
3222 return err;
3223 }
3224 }
3225 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
3226 mov_id32_date2year(&s->metadata);
3227
3228 return 0;
3229}
3230
3231static const MOVParseTableEntry mov_default_parse_table[] = {
3232{ MKTAG('A','C','L','R'), mov_read_avid },
3233{ MKTAG('A','P','R','G'), mov_read_avid },
3234{ MKTAG('A','A','L','P'), mov_read_avid },
3235{ MKTAG('A','R','E','S'), mov_read_ares },
3236{ MKTAG('a','v','s','s'), mov_read_avss },
3237{ MKTAG('c','h','p','l'), mov_read_chpl },
3238{ MKTAG('c','o','6','4'), mov_read_stco },
3239{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3240{ MKTAG('d','i','n','f'), mov_read_default },
3241{ MKTAG('d','r','e','f'), mov_read_dref },
3242{ MKTAG('e','d','t','s'), mov_read_default },
3243{ MKTAG('e','l','s','t'), mov_read_elst },
3244{ MKTAG('e','n','d','a'), mov_read_enda },
3245{ MKTAG('f','i','e','l'), mov_read_fiel },
3246{ MKTAG('f','t','y','p'), mov_read_ftyp },
3247{ MKTAG('g','l','b','l'), mov_read_glbl },
3248{ MKTAG('h','d','l','r'), mov_read_hdlr },
3249{ MKTAG('i','l','s','t'), mov_read_ilst },
3250{ MKTAG('j','p','2','h'), mov_read_jp2h },
3251{ MKTAG('m','d','a','t'), mov_read_mdat },
3252{ MKTAG('m','d','h','d'), mov_read_mdhd },
3253{ MKTAG('m','d','i','a'), mov_read_default },
3254{ MKTAG('m','e','t','a'), mov_read_meta },
3255{ MKTAG('m','i','n','f'), mov_read_default },
3256{ MKTAG('m','o','o','f'), mov_read_moof },
3257{ MKTAG('m','o','o','v'), mov_read_moov },
3258{ MKTAG('m','v','e','x'), mov_read_default },
3259{ MKTAG('m','v','h','d'), mov_read_mvhd },
3260{ MKTAG('S','M','I',' '), mov_read_svq3 },
3261{ MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3262{ MKTAG('a','v','c','C'), mov_read_glbl },
3263{ MKTAG('p','a','s','p'), mov_read_pasp },
3264{ MKTAG('s','t','b','l'), mov_read_default },
3265{ MKTAG('s','t','c','o'), mov_read_stco },
3266{ MKTAG('s','t','p','s'), mov_read_stps },
3267{ MKTAG('s','t','r','f'), mov_read_strf },
3268{ MKTAG('s','t','s','c'), mov_read_stsc },
3269{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3270{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3271{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3272{ MKTAG('s','t','t','s'), mov_read_stts },
3273{ MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3274{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3275{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3276{ MKTAG('t','r','a','k'), mov_read_trak },
3277{ MKTAG('t','r','a','f'), mov_read_default },
3278{ MKTAG('t','r','e','f'), mov_read_default },
3279{ MKTAG('t','m','c','d'), mov_read_tmcd },
3280{ MKTAG('c','h','a','p'), mov_read_chap },
3281{ MKTAG('t','r','e','x'), mov_read_trex },
3282{ MKTAG('t','r','u','n'), mov_read_trun },
3283{ MKTAG('u','d','t','a'), mov_read_default },
3284{ MKTAG('w','a','v','e'), mov_read_wave },
3285{ MKTAG('e','s','d','s'), mov_read_esds },
3286{ MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3287{ MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3288{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3289{ MKTAG('w','f','e','x'), mov_read_wfex },
3290{ MKTAG('c','m','o','v'), mov_read_cmov },
3291{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3292{ MKTAG('d','v','c','1'), mov_read_dvc1 },
3293{ MKTAG('s','b','g','p'), mov_read_sbgp },
3294{ MKTAG('h','v','c','C'), mov_read_glbl },
3295{ MKTAG('u','u','i','d'), mov_read_uuid },
3296{ MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
3297{ MKTAG('I','D','3','2'), mov_read_id32 },
3298
3299{ 0, NULL }
3300};
3301
3302static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3303{
3304 int64_t total_size = 0;
3305 MOVAtom a;
3306 int i;
3307
3308 if (atom.size < 0)
3309 atom.size = INT64_MAX;
3310 while (total_size + 8 <= atom.size && !url_feof(pb)) {
3311 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3312 a.size = atom.size;
3313 a.type=0;
3314 if (atom.size >= 8) {
3315 a.size = avio_rb32(pb);
3316 a.type = avio_rl32(pb);
3317 if (atom.type != MKTAG('r','o','o','t') &&
3318 atom.type != MKTAG('m','o','o','v'))
3319 {
3320 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
3321 {
3322 av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
3323 avio_skip(pb, -8);
3324 return 0;
3325 }
3326 }
3327 total_size += 8;
3328 if (a.size == 1) { /* 64 bit extended size */
3329 a.size = avio_rb64(pb) - 8;
3330 total_size += 8;
3331 }
3332 }
3333 av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3334 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3335 if (a.size == 0) {
3336 a.size = atom.size - total_size + 8;
3337 }
3338 a.size -= 8;
3339 if (a.size < 0)
3340 break;
3341 a.size = FFMIN(a.size, atom.size - total_size);
3342
3343 for (i = 0; mov_default_parse_table[i].type; i++)
3344 if (mov_default_parse_table[i].type == a.type) {
3345 parse = mov_default_parse_table[i].parse;
3346 break;
3347 }
3348
3349 // container is user data
3350 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3351 atom.type == MKTAG('i','l','s','t')))
3352 parse = mov_read_udta_string;
3353
3354 // Supports parsing the QuickTime Metadata Keys.
3355 // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
3356 if (!parse && c->found_hdlr_mdta &&
3357 atom.type == MKTAG('m','e','t','a') &&
3358 a.type == MKTAG('k','e','y','s')) {
3359 parse = mov_read_keys;
3360 }
3361
3362 if (!parse) { /* skip leaf atoms data */
3363 avio_skip(pb, a.size);
3364 } else {
3365 int64_t start_pos = avio_tell(pb);
3366 int64_t left;
3367 int err = parse(c, pb, a);
3368 if (err < 0)
3369 return err;
3370 if (c->found_moov && c->found_mdat &&
3371 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3372 start_pos + a.size == avio_size(pb))) {
3373 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3374 c->next_root_atom = start_pos + a.size;
3375 return 0;
3376 }
3377 left = a.size - avio_tell(pb) + start_pos;
3378 if (left > 0) /* skip garbage at atom end */
3379 avio_skip(pb, left);
3380 else if (left < 0) {
3381 av_log(c->fc, AV_LOG_WARNING,
3382 "overread end of atom '%.4s' by %"PRId64" bytes\n",
3383 (char*)&a.type, -left);
3384 avio_seek(pb, left, SEEK_CUR);
3385 }
3386 }
3387
3388 total_size += a.size;
3389 }
3390
3391 if (total_size < atom.size && atom.size < 0x7ffff)
3392 avio_skip(pb, atom.size - total_size);
3393
3394 return 0;
3395}
3396
3397static int mov_probe(AVProbeData *p)
3398{
3399 int64_t offset;
3400 uint32_t tag;
3401 int score = 0;
3402 int moov_offset = -1;
3403
3404 /* check file header */
3405 offset = 0;
3406 for (;;) {
3407 /* ignore invalid offset */
3408 if ((offset + 8) > p->buf_size)
3409 break;
3410 tag = AV_RL32(p->buf + offset + 4);
3411 switch(tag) {
3412 /* check for obvious tags */
3413 case MKTAG('m','o','o','v'):
3414 moov_offset = offset + 4;
3415 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
3416 case MKTAG('m','d','a','t'):
3417 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3418 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3419 case MKTAG('f','t','y','p'):
3420 if (AV_RB32(p->buf+offset) < 8 &&
3421 (AV_RB32(p->buf+offset) != 1 ||
3422 offset + 12 > (unsigned int)p->buf_size ||
3423 AV_RB64(p->buf+offset + 8) == 0)) {
3424 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3425 } else {
3426 score = AVPROBE_SCORE_MAX;
3427 }
3428 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3429 break;
3430 /* those are more common words, so rate then a bit less */
3431 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3432 case MKTAG('w','i','d','e'):
3433 case MKTAG('f','r','e','e'):
3434 case MKTAG('j','u','n','k'):
3435 case MKTAG('p','i','c','t'):
3436 score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
3437 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3438 break;
3439 case MKTAG(0x82,0x82,0x7f,0x7d):
3440 case MKTAG('s','k','i','p'):
3441 case MKTAG('u','u','i','d'):
3442 case MKTAG('p','r','f','l'):
3443 /* if we only find those cause probedata is too small at least rate them */
3444 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3445 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3446 break;
3447 default:
3448 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3449 }
3450 }
3451 if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
3452 /* moov atom in the header - we should make sure that this is not a
3453 * MOV-packed MPEG-PS */
3454 offset = moov_offset;
3455
3456 while(offset < (p->buf_size - 16)){ /* Sufficient space */
3457 /* We found an actual hdlr atom */
3458 if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
3459 AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
3460 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
3461 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
3462 /* We found a media handler reference atom describing an
3463 * MPEG-PS-in-MOV, return a
3464 * low score to force expanding the probe window until
3465 * mpegps_probe finds what it needs */
3466 return 5;
3467 }else
3468 /* Keep looking */
3469 offset+=2;
3470 }
3471 }
3472
3473 return score;
3474}
3475
3476// must be done after parsing all trak because there's no order requirement
3477static void mov_read_chapters(AVFormatContext *s)
3478{
3479 MOVContext *mov = s->priv_data;
3480 AVStream *st = NULL;
3481 MOVStreamContext *sc;
3482 int64_t cur_pos;
3483 int i;
3484
3485 for (i = 0; i < s->nb_streams; i++)
3486 if (s->streams[i]->id == mov->chapter_track) {
3487 st = s->streams[i];
3488 break;
3489 }
3490 if (!st) {
3491 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3492 return;
3493 }
3494
3495 st->discard = AVDISCARD_ALL;
3496 sc = st->priv_data;
3497 cur_pos = avio_tell(sc->pb);
3498
3499 for (i = 0; i < st->nb_index_entries; i++) {
3500 AVIndexEntry *sample = &st->index_entries[i];
3501 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3502 uint8_t *title;
3503 uint16_t ch;
3504 int len, title_len;
3505
3506 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3507 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3508 goto finish;
3509 }
3510
3511 // the first two bytes are the length of the title
3512 len = avio_rb16(sc->pb);
3513 if (len > sample->size-2)
3514 continue;
3515 title_len = 2*len + 1;
3516 if (!(title = av_mallocz(title_len)))
3517 goto finish;
3518
3519 // The samples could theoretically be in any encoding if there's an encd
3520 // atom following, but in practice are only utf-8 or utf-16, distinguished
3521 // instead by the presence of a BOM
3522 if (!len) {
3523 title[0] = 0;
3524 } else {
3525 ch = avio_rb16(sc->pb);
3526 if (ch == 0xfeff)
3527 avio_get_str16be(sc->pb, len, title, title_len);
3528 else if (ch == 0xfffe)
3529 avio_get_str16le(sc->pb, len, title, title_len);
3530 else {
3531 AV_WB16(title, ch);
3532 if (len == 1 || len == 2)
3533 title[len] = 0;
3534 else
3535 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
3536 }
3537 }
3538
3539 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3540 av_freep(&title);
3541 }
3542finish:
3543 avio_seek(sc->pb, cur_pos, SEEK_SET);
3544}
3545
3546static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
3547 uint32_t value, int flags)
3548{
3549 AVTimecode tc;
3550 char buf[AV_TIMECODE_STR_SIZE];
3551 AVRational rate = {st->codec->time_base.den,
3552 st->codec->time_base.num};
3553 int ret = av_timecode_init(&tc, rate, flags, 0, s);
3554 if (ret < 0)
3555 return ret;
3556 av_dict_set(&st->metadata, "timecode",
3557 av_timecode_make_string(&tc, buf, value), 0);
3558 return 0;
3559}
3560
3561static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
3562{
3563 MOVStreamContext *sc = st->priv_data;
3564 int flags = 0;
3565 int64_t cur_pos = avio_tell(sc->pb);
3566 uint32_t value;
3567
3568 if (!st->nb_index_entries)
3569 return -1;
3570
3571 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
3572 value = avio_rb32(s->pb);
3573
3574 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
3575 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
3576 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
3577
3578 /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
3579 * not the case) and thus assume "frame number format" instead of QT one.
3580 * No sample with tmcd track can be found with a QT timecode at the moment,
3581 * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
3582 * format). */
3583 parse_timecode_in_framenum_format(s, st, value, flags);
3584
3585 avio_seek(sc->pb, cur_pos, SEEK_SET);
3586 return 0;
3587}
3588
3589static int mov_read_close(AVFormatContext *s)
3590{
3591 MOVContext *mov = s->priv_data;
3592 int i, j;
3593
3594 for (i = 0; i < s->nb_streams; i++) {
3595 AVStream *st = s->streams[i];
3596 MOVStreamContext *sc = st->priv_data;
3597
3598 av_freep(&sc->ctts_data);
3599 for (j = 0; j < sc->drefs_count; j++) {
3600 av_freep(&sc->drefs[j].path);
3601 av_freep(&sc->drefs[j].dir);
3602 }
3603 av_freep(&sc->drefs);
3604 if (!sc->pb_is_copied)
3605 avio_close(sc->pb);
3606 sc->pb = NULL;
3607 av_freep(&sc->chunk_offsets);
3608 av_freep(&sc->keyframes);
3609 av_freep(&sc->sample_sizes);
3610 av_freep(&sc->stps_data);
3611 av_freep(&sc->stsc_data);
3612 av_freep(&sc->stts_data);
3613 }
3614
3615 if (mov->dv_demux) {
3616 for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
3617 av_freep(&mov->dv_fctx->streams[i]->codec);
3618 av_freep(&mov->dv_fctx->streams[i]);
3619 }
3620 av_freep(&mov->dv_fctx);
3621 av_freep(&mov->dv_demux);
3622 }
3623
3624 if (mov->meta_keys) {
3625 for (i = 1; i < mov->meta_keys_count; i++) {
3626 av_freep(&mov->meta_keys[i]);
3627 }
3628 av_freep(&mov->meta_keys);
3629 }
3630
3631 av_freep(&mov->trex_data);
3632 av_freep(&mov->bitrates);
3633
3634 return 0;
3635}
3636
3637static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
3638{
3639 int i;
3640
3641 for (i = 0; i < s->nb_streams; i++) {
3642 AVStream *st = s->streams[i];
3643 MOVStreamContext *sc = st->priv_data;
3644
3645 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3646 sc->timecode_track == tmcd_id)
3647 return 1;
3648 }
3649 return 0;
3650}
3651
3652/* look for a tmcd track not referenced by any video track, and export it globally */
3653static void export_orphan_timecode(AVFormatContext *s)
3654{
3655 int i;
3656
3657 for (i = 0; i < s->nb_streams; i++) {
3658 AVStream *st = s->streams[i];
3659
3660 if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
3661 !tmcd_is_referenced(s, i + 1)) {
3662 AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
3663 if (tcr) {
3664 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
3665 break;
3666 }
3667 }
3668 }
3669}
3670
3671static int mov_read_header(AVFormatContext *s)
3672{
3673 MOVContext *mov = s->priv_data;
3674 AVIOContext *pb = s->pb;
3675 int i, j, err;
3676 MOVAtom atom = { AV_RL32("root") };
3677
3678 mov->fc = s;
3679 mov->trak_index = -1;
3680 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3681 if (pb->seekable)
3682 atom.size = avio_size(pb);
3683 else
3684 atom.size = INT64_MAX;
3685
3686 /* check MOV header */
3687 if ((err = mov_read_default(mov, pb, atom)) < 0) {
3688 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3689 mov_read_close(s);
3690 return err;
3691 }
3692 if (!mov->found_moov) {
3693 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3694 mov_read_close(s);
3695 return AVERROR_INVALIDDATA;
3696 }
3697 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3698
3699 if (pb->seekable) {
3700 if (mov->chapter_track > 0)
3701 mov_read_chapters(s);
3702 for (i = 0; i < s->nb_streams; i++)
3703 if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
3704 mov_read_timecode_track(s, s->streams[i]);
3705 }
3706
3707 /* copy timecode metadata from tmcd tracks to the related video streams */
3708 for (i = 0; i < s->nb_streams; i++) {
3709 AVStream *st = s->streams[i];
3710 MOVStreamContext *sc = st->priv_data;
3711 if (sc->timecode_track > 0) {
3712 AVDictionaryEntry *tcr;
3713 int tmcd_st_id = -1;
3714
3715 for (j = 0; j < s->nb_streams; j++)
3716 if (s->streams[j]->id == sc->timecode_track)
3717 tmcd_st_id = j;
3718
3719 if (tmcd_st_id < 0 || tmcd_st_id == i)
3720 continue;
3721 tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
3722 if (tcr)
3723 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
3724 }
3725 }
3726 export_orphan_timecode(s);
3727
3728 for (i = 0; i < s->nb_streams; i++) {
3729 AVStream *st = s->streams[i];
3730 MOVStreamContext *sc = st->priv_data;
3731 fix_timescale(mov, sc);
3732 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
3733 st->skip_samples = sc->start_pad;
3734 }
3735 }
3736
3737 if (mov->trex_data) {
3738 for (i = 0; i < s->nb_streams; i++) {
3739 AVStream *st = s->streams[i];
3740 MOVStreamContext *sc = st->priv_data;
3741 if (st->duration > 0)
3742 st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3743 }
3744 }
3745
3746 for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
3747 if (mov->bitrates[i]) {
3748 s->streams[i]->codec->bit_rate = mov->bitrates[i];
3749 }
3750 }
3751
3752 return 0;
3753}
3754
3755static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
3756{
3757 AVIndexEntry *sample = NULL;
3758 int64_t best_dts = INT64_MAX;
3759 int i;
3760 for (i = 0; i < s->nb_streams; i++) {
3761 AVStream *avst = s->streams[i];
3762 MOVStreamContext *msc = avst->priv_data;
3763 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3764 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3765 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3766 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3767 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3768 (s->pb->seekable &&
3769 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3770 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3771 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3772 sample = current_sample;
3773 best_dts = dts;
3774 *st = avst;
3775 }
3776 }
3777 }
3778 return sample;
3779}
3780
3781static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
3782{
3783 MOVContext *mov = s->priv_data;
3784 MOVStreamContext *sc;
3785 AVIndexEntry *sample;
3786 AVStream *st = NULL;
3787 int ret;
3788 mov->fc = s;
3789 retry:
3790 sample = mov_find_next_sample(s, &st);
3791 if (!sample) {
3792 mov->found_mdat = 0;
3793 if (!mov->next_root_atom)
3794 return AVERROR_EOF;
3795 avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3796 mov->next_root_atom = 0;
3797 if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3798 url_feof(s->pb))
3799 return AVERROR_EOF;
3800 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3801 goto retry;
3802 }
3803 sc = st->priv_data;
3804 /* must be done just before reading, to avoid infinite loop on sample */
3805 sc->current_sample++;
3806
3807 if (mov->next_root_atom) {
3808 sample->pos = FFMIN(sample->pos, mov->next_root_atom);
3809 sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
3810 }
3811
3812 if (st->discard != AVDISCARD_ALL) {
3813 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3814 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3815 sc->ffindex, sample->pos);
3816 return AVERROR_INVALIDDATA;
3817 }
3818 ret = av_get_packet(sc->pb, pkt, sample->size);
3819 if (ret < 0)
3820 return ret;
3821 if (sc->has_palette) {
3822 uint8_t *pal;
3823
3824 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
3825 if (!pal) {
3826 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3827 } else {
3828 memcpy(pal, sc->palette, AVPALETTE_SIZE);
3829 sc->has_palette = 0;
3830 }
3831 }
3832#if CONFIG_DV_DEMUXER
3833 if (mov->dv_demux && sc->dv_audio_container) {
3834 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
3835 av_free(pkt->data);
3836 pkt->size = 0;
3837 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3838 if (ret < 0)
3839 return ret;
3840 }
3841#endif
3842 }
3843
3844 pkt->stream_index = sc->ffindex;
3845 pkt->dts = sample->timestamp;
3846 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3847 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3848 /* update ctts context */
3849 sc->ctts_sample++;
3850 if (sc->ctts_index < sc->ctts_count &&
3851 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3852 sc->ctts_index++;
3853 sc->ctts_sample = 0;
3854 }
3855 if (sc->wrong_dts)
3856 pkt->dts = AV_NOPTS_VALUE;
3857 } else {
3858 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3859 st->index_entries[sc->current_sample].timestamp : st->duration;
3860 pkt->duration = next_dts - pkt->dts;
3861 pkt->pts = pkt->dts;
3862 }
3863 if (st->discard == AVDISCARD_ALL)
3864 goto retry;
3865 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3866 pkt->pos = sample->pos;
3867 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
3868 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3869 return 0;
3870}
3871
3872
3873static int64_t mov_read_seek_sync(AVFormatContext *s,
3874 int stream_index,
3875 int64_t min_ts,
3876 int64_t target_ts,
3877 int64_t max_ts,
3878 int flags)
3879{
3880 int64_t pos, t_pos;
3881
3882 int64_t ts_ret, ts_adj;
3883 int stream_index_gen_search = stream_index;
3884 int sample, i;
3885 AVStream *st;
3886 AVParserState *backup;
3887
3888 backup = ff_store_parser_state(s);
3889
3890 // detect direction of seeking for search purposes
3891 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
3892 AVSEEK_FLAG_BACKWARD : 0;
3893
3894 st = s->streams[stream_index_gen_search];
3895 sample = av_index_search_timestamp(st, target_ts, AVSEEK_FLAG_ANY);
3896 pos = st->index_entries[sample].pos;
3897 target_ts = st->index_entries[sample].timestamp;
3898 for (i = 0; i < s->nb_streams; i++) {
3899 MOVStreamContext *sc = s->streams[i]->priv_data;
3900 sc->current_sample = (sample - 500) > 0 ? (sample - 500) : 0; // hard code for mov, repos the sample.
3901 }
3902
3903 // search for actual matching keyframe/starting position for all streams
3904 if ((t_pos = ff_gen_syncpoint_search(s, stream_index, pos,
3905 min_ts, target_ts, max_ts,
3906 flags)) < 0) {
3907 ff_restore_parser_state(s, backup);
3908 return -1;
3909 }
3910
3911 ff_free_parser_state(s, backup);
3912 return t_pos;
3913}
3914
3915static int64_t mov_read_seek2(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
3916 int ret;
3917 if (flags & AVSEEK_FLAG_BACKWARD) {
3918 flags &= ~AVSEEK_FLAG_BACKWARD;
3919 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
3920 if (ret < 0) {
3921 // for compatibility reasons, seek to the best-fitting timestamp
3922 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
3923 }
3924 } else {
3925 ret = mov_read_seek_sync(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
3926 if (ret < 0)
3927 // for compatibility reasons, seek to the best-fitting timestamp
3928 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
3929 }
3930 return ret;
3931}
3932
3933static int mov_index_search_pos(const AVIndexEntry *entries, int nb_entries,
3934 int64_t pos, int flags)
3935{
3936 int a, b, m;
3937 int64_t ppos;
3938
3939 a = - 1;
3940 b = nb_entries;
3941
3942 //optimize appending index entries at the end
3943 if(b && entries[b-1].pos < pos)
3944 a= b-1;
3945
3946 while (b - a > 1) {
3947 m = (a + b) >> 1;
3948 ppos = entries[m].pos;
3949 if(ppos >= pos)
3950 b = m;
3951 if(ppos <= pos)
3952 a = m;
3953 }
3954
3955 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
3956 return m;
3957}
3958
3959static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3960{
3961 MOVStreamContext *sc = st->priv_data;
3962 int sample, time_sample;
3963 int i;
3964
3965 sample = av_index_search_timestamp(st, timestamp, flags);
3966
3967 // mov's stss is wrong sometimes, need to read seek
3968 // added by senbai.tao
3969 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sample <=0 && st->nb_index_entries && sc->keyframe_count <= 1) {
3970 int64_t sync_point = mov_read_seek2(s, st->index, timestamp, flags);
3971 sample = mov_index_search_pos(st->index_entries, st->nb_index_entries, sync_point, AVSEEK_FLAG_ANY);
3972 }
3973
3974 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3975 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3976 sample = 0;
3977 if (sample < 0) /* not sure what to do */
3978 return AVERROR_INVALIDDATA;
3979 sc->current_sample = sample;
3980 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
3981 /* adjust ctts index */
3982 if (sc->ctts_data) {
3983 time_sample = 0;
3984 for (i = 0; i < sc->ctts_count; i++) {
3985 int next = time_sample + sc->ctts_data[i].count;
3986 if (next > sc->current_sample) {
3987 sc->ctts_index = i;
3988 sc->ctts_sample = sc->current_sample - time_sample;
3989 break;
3990 }
3991 time_sample = next;
3992 }
3993 }
3994 return sample;
3995}
3996
3997static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3998{
3999 AVStream *st;
4000 int64_t seek_timestamp, timestamp;
4001 int sample;
4002 int i;
4003
4004 if (stream_index >= s->nb_streams)
4005 return AVERROR_INVALIDDATA;
4006
4007 st = s->streams[stream_index];
4008 sample = mov_seek_stream(s, st, sample_time, flags);
4009 if (sample < 0)
4010 return sample;
4011
4012 /* adjust seek timestamp to found sample timestamp */
4013 seek_timestamp = st->index_entries[sample].timestamp;
4014
4015 for (i = 0; i < s->nb_streams; i++) {
4016 MOVStreamContext *sc = s->streams[i]->priv_data;
4017 st = s->streams[i];
4018 st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
4019
4020 if (stream_index == i)
4021 continue;
4022
4023 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
4024 mov_seek_stream(s, st, timestamp, flags);
4025 }
4026 return 0;
4027}
4028
4029static const AVOption options[] = {
4030 {"use_absolute_path",
4031 "allow using absolute path when opening alias, this is a possible security issue",
4032 offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
4033 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4034 {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
4035 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4036 {NULL}
4037};
4038
4039static const AVClass mov_class = {
4040 .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
4041 .item_name = av_default_item_name,
4042 .option = options,
4043 .version = LIBAVUTIL_VERSION_INT,
4044};
4045
4046AVInputFormat ff_mov_demuxer = {
4047 .name = "mov,mp4,m4a,3gp,3g2,mj2",
4048 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4049 .priv_data_size = sizeof(MOVContext),
4050 .read_probe = mov_probe,
4051 .read_header = mov_read_header,
4052 .read_packet = mov_read_packet,
4053 .read_close = mov_read_close,
4054 .read_seek = mov_read_seek,
4055 .priv_class = &mov_class,
4056 .flags = AVFMT_NO_BYTE_SEEK,
4057};
4058