summaryrefslogtreecommitdiff
path: root/libavformat/mov.c (plain)
blob: 2b6c4bff6d732dc94577723fa159dfd327d0db02
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 st->need_parsing = AVSTREAM_PARSE_FULL;
1788 break;
1789 case AV_CODEC_ID_MPEG1VIDEO:
1790 st->need_parsing = AVSTREAM_PARSE_FULL;
1791 break;
1792 case AV_CODEC_ID_VC1:
1793 st->need_parsing = AVSTREAM_PARSE_FULL;
1794 break;
1795 case AV_CODEC_ID_HEVC:
1796 st->need_parsing = AVSTREAM_PARSE_HEADERS;
1797 break;
1798 default:
1799 break;
1800 }
1801 return 0;
1802}
1803
1804static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb,
1805 int codec_tag, int format,
1806 int size)
1807{
1808 int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1809
1810 if (codec_tag &&
1811 (codec_tag != format &&
1812 (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1813 : codec_tag != MKTAG('j','p','e','g')))) {
1814 /* Multiple fourcc, we skip JPEG. This is not correct, we should
1815 * export it as a separate AVStream but this needs a few changes
1816 * in the MOV demuxer, patch welcome. */
1817
1818 av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1819 avio_skip(pb, size);
1820 return 1;
1821 }
1822 if ( codec_tag == AV_RL32("avc1") ||
1823 codec_tag == AV_RL32("hvc1") ||
1824 codec_tag == AV_RL32("hev1")
1825 )
1826 av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
1827
1828 return 0;
1829}
1830
1831int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
1832{
1833 AVStream *st;
1834 MOVStreamContext *sc;
1835 int pseudo_stream_id;
1836
1837 if (c->fc->nb_streams < 1)
1838 return 0;
1839 st = c->fc->streams[c->fc->nb_streams-1];
1840 sc = st->priv_data;
1841
1842 for (pseudo_stream_id = 0;
1843 pseudo_stream_id < entries && !pb->eof_reached;
1844 pseudo_stream_id++) {
1845 //Parsing Sample description table
1846 enum AVCodecID id;
1847 int ret, dref_id = 1;
1848 MOVAtom a = { AV_RL32("stsd") };
1849 int64_t start_pos = avio_tell(pb);
1850 int64_t size = avio_rb32(pb); /* size */
1851 uint32_t format = avio_rl32(pb); /* data format */
1852
1853 if (size >= 16) {
1854 avio_rb32(pb); /* reserved */
1855 avio_rb16(pb); /* reserved */
1856 dref_id = avio_rb16(pb);
1857 }else if (size <= 7){
1858 av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
1859 return AVERROR_INVALIDDATA;
1860 }
1861
1862 if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
1863 size - (avio_tell(pb) - start_pos)))
1864 continue;
1865
1866 sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
1867 sc->dref_id= dref_id;
1868
1869 id = mov_codec_id(st, format);
1870
1871 av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
1872 (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
1873 (format >> 24) & 0xff, st->codec->codec_type);
1874
1875 if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
1876 st->codec->codec_id = id;
1877 mov_parse_stsd_video(c, pb, st, sc);
1878 } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
1879 st->codec->codec_id = id;
1880 mov_parse_stsd_audio(c, pb, st, sc);
1881 } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
1882 st->codec->codec_id = id;
1883 mov_parse_stsd_subtitle(c, pb, st, sc,
1884 size - (avio_tell(pb) - start_pos));
1885 } else {
1886 ret = mov_parse_stsd_data(c, pb, st, sc,
1887 size - (avio_tell(pb) - start_pos));
1888 if (ret < 0)
1889 return ret;
1890 }
1891 /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1892 a.size = size - (avio_tell(pb) - start_pos);
1893 if (a.size > 8) {
1894 if ((ret = mov_read_default(c, pb, a)) < 0)
1895 return ret;
1896 } else if (a.size > 0)
1897 avio_skip(pb, a.size);
1898 }
1899
1900 if (pb->eof_reached)
1901 return AVERROR_EOF;
1902
1903 return mov_finalize_stsd_codec(c, pb, st, sc);
1904}
1905
1906static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1907{
1908 int entries;
1909
1910 avio_r8(pb); /* version */
1911 avio_rb24(pb); /* flags */
1912 entries = avio_rb32(pb);
1913
1914 return ff_mov_read_stsd_entries(c, pb, entries);
1915}
1916
1917static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1918{
1919 AVStream *st;
1920 MOVStreamContext *sc;
1921 unsigned int i, entries;
1922
1923 if (c->fc->nb_streams < 1)
1924 return 0;
1925 st = c->fc->streams[c->fc->nb_streams-1];
1926 sc = st->priv_data;
1927
1928 avio_r8(pb); /* version */
1929 avio_rb24(pb); /* flags */
1930
1931 entries = avio_rb32(pb);
1932
1933 av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1934
1935 if (!entries)
1936 return 0;
1937 if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1938 return AVERROR_INVALIDDATA;
1939 sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1940 if (!sc->stsc_data)
1941 return AVERROR(ENOMEM);
1942
1943 for (i = 0; i < entries && !pb->eof_reached; i++) {
1944 sc->stsc_data[i].first = avio_rb32(pb);
1945 sc->stsc_data[i].count = avio_rb32(pb);
1946 sc->stsc_data[i].id = avio_rb32(pb);
1947 }
1948
1949 sc->stsc_count = i;
1950
1951 if (pb->eof_reached)
1952 return AVERROR_EOF;
1953
1954 return 0;
1955}
1956
1957static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1958{
1959 AVStream *st;
1960 MOVStreamContext *sc;
1961 unsigned i, entries;
1962
1963 if (c->fc->nb_streams < 1)
1964 return 0;
1965 st = c->fc->streams[c->fc->nb_streams-1];
1966 sc = st->priv_data;
1967
1968 avio_rb32(pb); // version + flags
1969
1970 entries = avio_rb32(pb);
1971 if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1972 return AVERROR_INVALIDDATA;
1973 sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1974 if (!sc->stps_data)
1975 return AVERROR(ENOMEM);
1976
1977 for (i = 0; i < entries && !pb->eof_reached; i++) {
1978 sc->stps_data[i] = avio_rb32(pb);
1979 //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1980 }
1981
1982 sc->stps_count = i;
1983
1984 if (pb->eof_reached)
1985 return AVERROR_EOF;
1986
1987 return 0;
1988}
1989
1990static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1991{
1992 AVStream *st;
1993 MOVStreamContext *sc;
1994 unsigned int i, entries;
1995
1996 if (c->fc->nb_streams < 1)
1997 return 0;
1998 st = c->fc->streams[c->fc->nb_streams-1];
1999 sc = st->priv_data;
2000
2001 avio_r8(pb); /* version */
2002 avio_rb24(pb); /* flags */
2003
2004 entries = avio_rb32(pb);
2005
2006 av_dlog(c->fc, "keyframe_count = %d\n", entries);
2007
2008 if (!entries)
2009 {
2010 sc->keyframe_absent = 1;
2011 if (!st->need_parsing)
2012 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2013 return 0;
2014 }
2015 if (entries >= UINT_MAX / sizeof(int))
2016 return AVERROR_INVALIDDATA;
2017 sc->keyframes = av_malloc(entries * sizeof(int));
2018 if (!sc->keyframes)
2019 return AVERROR(ENOMEM);
2020
2021 for (i = 0; i < entries && !pb->eof_reached; i++) {
2022 sc->keyframes[i] = avio_rb32(pb);
2023 //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
2024 }
2025
2026 sc->keyframe_count = i;
2027
2028 if (pb->eof_reached)
2029 return AVERROR_EOF;
2030
2031 return 0;
2032}
2033
2034static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2035{
2036 AVStream *st;
2037 MOVStreamContext *sc;
2038 unsigned int i, entries, sample_size, field_size, num_bytes;
2039 GetBitContext gb;
2040 unsigned char* buf;
2041
2042 if (c->fc->nb_streams < 1)
2043 return 0;
2044 st = c->fc->streams[c->fc->nb_streams-1];
2045 sc = st->priv_data;
2046
2047 avio_r8(pb); /* version */
2048 avio_rb24(pb); /* flags */
2049
2050 if (atom.type == MKTAG('s','t','s','z')) {
2051 sample_size = avio_rb32(pb);
2052 if (!sc->sample_size) /* do not overwrite value computed in stsd */
2053 sc->sample_size = sample_size;
2054 sc->stsz_sample_size = sample_size;
2055 field_size = 32;
2056 } else {
2057 sample_size = 0;
2058 avio_rb24(pb); /* reserved */
2059 field_size = avio_r8(pb);
2060 }
2061 entries = avio_rb32(pb);
2062
2063 av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2064
2065 sc->sample_count = entries;
2066 if (sample_size)
2067 return 0;
2068
2069 if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2070 av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2071 return AVERROR_INVALIDDATA;
2072 }
2073
2074 if (!entries)
2075 return 0;
2076 if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
2077 return AVERROR_INVALIDDATA;
2078 sc->sample_sizes = av_malloc(entries * sizeof(int));
2079 if (!sc->sample_sizes)
2080 return AVERROR(ENOMEM);
2081
2082 num_bytes = (entries*field_size+4)>>3;
2083
2084 buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
2085 if (!buf) {
2086 av_freep(&sc->sample_sizes);
2087 return AVERROR(ENOMEM);
2088 }
2089
2090 if (avio_read(pb, buf, num_bytes) < num_bytes) {
2091 av_freep(&sc->sample_sizes);
2092 av_free(buf);
2093 return AVERROR_INVALIDDATA;
2094 }
2095
2096 init_get_bits(&gb, buf, 8*num_bytes);
2097
2098 for (i = 0; i < entries && !pb->eof_reached; i++) {
2099 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2100 sc->data_size += sc->sample_sizes[i];
2101 }
2102
2103 sc->sample_count = i;
2104
2105 if (pb->eof_reached)
2106 return AVERROR_EOF;
2107
2108 av_free(buf);
2109 return 0;
2110}
2111
2112static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2113{
2114 AVStream *st;
2115 MOVStreamContext *sc;
2116 unsigned int i, entries;
2117 int64_t duration=0;
2118 int64_t total_sample_count=0;
2119
2120 if (c->fc->nb_streams < 1)
2121 return 0;
2122 st = c->fc->streams[c->fc->nb_streams-1];
2123 sc = st->priv_data;
2124
2125 avio_r8(pb); /* version */
2126 avio_rb24(pb); /* flags */
2127 entries = avio_rb32(pb);
2128
2129 av_dlog(c->fc, "track[%i].stts.entries = %i\n",
2130 c->fc->nb_streams-1, entries);
2131
2132 if (entries >= UINT_MAX / sizeof(*sc->stts_data))
2133 return -1;
2134
2135 sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
2136 if (!sc->stts_data)
2137 return AVERROR(ENOMEM);
2138
2139 for (i = 0; i < entries && !pb->eof_reached; i++) {
2140 int sample_duration;
2141 int sample_count;
2142
2143 sample_count=avio_rb32(pb);
2144 sample_duration = avio_rb32(pb);
2145
2146 /* sample_duration < 0 is invalid based on the spec */
2147 if (sample_duration < 0) {
2148 av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration);
2149 sample_duration = 1;
2150 }
2151 if (sample_count < 0) {
2152 av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2153 return AVERROR_INVALIDDATA;
2154 }
2155 sc->stts_data[i].count= sample_count;
2156 sc->stts_data[i].duration= sample_duration;
2157
2158 av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
2159 sample_count, sample_duration);
2160
2161 duration+=(int64_t)sample_duration*sample_count;
2162 total_sample_count+=sample_count;
2163 }
2164
2165 sc->stts_count = i;
2166
2167 if (pb->eof_reached)
2168 return AVERROR_EOF;
2169
2170 st->nb_frames= total_sample_count;
2171 if (duration)
2172 st->duration= duration;
2173 sc->track_end = duration;
2174 return 0;
2175}
2176
2177static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
2178{
2179 if (duration < 0) {
2180 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2181 }
2182}
2183
2184static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2185{
2186 AVStream *st;
2187 MOVStreamContext *sc;
2188 unsigned int i, entries;
2189
2190 if (c->fc->nb_streams < 1)
2191 return 0;
2192 st = c->fc->streams[c->fc->nb_streams-1];
2193 sc = st->priv_data;
2194
2195 avio_r8(pb); /* version */
2196 avio_rb24(pb); /* flags */
2197 entries = avio_rb32(pb);
2198
2199 av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2200
2201 if (!entries)
2202 return 0;
2203 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2204 return AVERROR_INVALIDDATA;
2205 sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
2206 if (!sc->ctts_data)
2207 return AVERROR(ENOMEM);
2208
2209 for (i = 0; i < entries && !pb->eof_reached; i++) {
2210 int count =avio_rb32(pb);
2211 int duration =avio_rb32(pb);
2212
2213 sc->ctts_data[i].count = count;
2214 sc->ctts_data[i].duration= duration;
2215
2216 av_dlog(c->fc, "count=%d, duration=%d\n",
2217 count, duration);
2218
2219 if (FFABS(duration) > (1<<28) && i+2<entries) {
2220 av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2221 av_freep(&sc->ctts_data);
2222 sc->ctts_count = 0;
2223 return 0;
2224 }
2225
2226 if (i+2<entries)
2227 mov_update_dts_shift(sc, duration);
2228 }
2229
2230 sc->ctts_count = i;
2231
2232 if (pb->eof_reached)
2233 return AVERROR_EOF;
2234
2235 av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
2236
2237 return 0;
2238}
2239
2240static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2241{
2242 AVStream *st;
2243 MOVStreamContext *sc;
2244 unsigned int i, entries;
2245 uint8_t version;
2246 uint32_t grouping_type;
2247
2248 if (c->fc->nb_streams < 1)
2249 return 0;
2250 st = c->fc->streams[c->fc->nb_streams-1];
2251 sc = st->priv_data;
2252
2253 version = avio_r8(pb); /* version */
2254 avio_rb24(pb); /* flags */
2255 grouping_type = avio_rl32(pb);
2256 if (grouping_type != MKTAG( 'r','a','p',' '))
2257 return 0; /* only support 'rap ' grouping */
2258 if (version == 1)
2259 avio_rb32(pb); /* grouping_type_parameter */
2260
2261 entries = avio_rb32(pb);
2262 if (!entries)
2263 return 0;
2264 if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2265 return AVERROR_INVALIDDATA;
2266 sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2267 if (!sc->rap_group)
2268 return AVERROR(ENOMEM);
2269
2270 for (i = 0; i < entries && !pb->eof_reached; i++) {
2271 sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2272 sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2273 }
2274
2275 sc->rap_group_count = i;
2276
2277 return pb->eof_reached ? AVERROR_EOF : 0;
2278}
2279
2280static void mov_build_index(MOVContext *mov, AVStream *st)
2281{
2282 MOVStreamContext *sc = st->priv_data;
2283 int64_t current_offset;
2284 int64_t current_dts = 0;
2285 unsigned int stts_index = 0;
2286 unsigned int stsc_index = 0;
2287 unsigned int stss_index = 0;
2288 unsigned int stps_index = 0;
2289 unsigned int i, j;
2290 uint64_t stream_size = 0;
2291
2292 /* adjust first dts according to edit list */
2293 if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
2294 if (sc->empty_duration)
2295 sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
2296 sc->time_offset = sc->start_time - sc->empty_duration;
2297 current_dts = -sc->time_offset;
2298 if (sc->ctts_count>0 && sc->stts_count>0 &&
2299 sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2300 /* more than 16 frames delay, dts are likely wrong
2301 this happens with files created by iMovie */
2302 sc->wrong_dts = 1;
2303 st->codec->has_b_frames = 1;
2304 }
2305 }
2306
2307 /* only use old uncompressed audio chunk demuxing when stts specifies it */
2308 if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2309 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2310 unsigned int current_sample = 0;
2311 unsigned int stts_sample = 0;
2312 unsigned int sample_size;
2313 unsigned int distance = 0;
2314 unsigned int rap_group_index = 0;
2315 unsigned int rap_group_sample = 0;
2316 int rap_group_present = sc->rap_group_count && sc->rap_group;
2317 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2318
2319 current_dts -= sc->dts_shift;
2320
2321 if (!sc->sample_count || st->nb_index_entries)
2322 return;
2323 if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2324 return;
2325 if (av_reallocp_array(&st->index_entries,
2326 st->nb_index_entries + sc->sample_count,
2327 sizeof(*st->index_entries)) < 0) {
2328 st->nb_index_entries = 0;
2329 return;
2330 }
2331 st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
2332
2333 for (i = 0; i < sc->chunk_count; i++) {
2334 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2335 current_offset = sc->chunk_offsets[i];
2336 while (stsc_index + 1 < sc->stsc_count &&
2337 i + 1 == sc->stsc_data[stsc_index + 1].first)
2338 stsc_index++;
2339
2340 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2341 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2342 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2343 sc->stsz_sample_size = sc->sample_size;
2344 }
2345 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2346 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2347 sc->stsz_sample_size = sc->sample_size;
2348 }
2349
2350 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2351 int keyframe = 0;
2352 if (current_sample >= sc->sample_count) {
2353 av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2354 return;
2355 }
2356
2357 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2358 keyframe = 1;
2359 if (stss_index + 1 < sc->keyframe_count)
2360 stss_index++;
2361 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2362 keyframe = 1;
2363 if (stps_index + 1 < sc->stps_count)
2364 stps_index++;
2365 }
2366 if (rap_group_present && rap_group_index < sc->rap_group_count) {
2367 if (sc->rap_group[rap_group_index].index > 0)
2368 keyframe = 1;
2369 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2370 rap_group_sample = 0;
2371 rap_group_index++;
2372 }
2373 }
2374 if (keyframe)
2375 distance = 0;
2376 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2377 if (sc->pseudo_stream_id == -1 ||
2378 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2379 AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2380 e->pos = current_offset;
2381 e->timestamp = current_dts;
2382 e->size = sample_size;
2383 e->min_distance = distance;
2384 e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2385 av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2386 "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2387 current_offset, current_dts, sample_size, distance, keyframe);
2388 }
2389
2390 current_offset += sample_size;
2391 stream_size += sample_size;
2392 current_dts += sc->stts_data[stts_index].duration;
2393 distance++;
2394 stts_sample++;
2395 current_sample++;
2396 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2397 stts_sample = 0;
2398 stts_index++;
2399 }
2400 }
2401 }
2402 if (st->duration > 0)
2403 st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2404 } else {
2405 unsigned chunk_samples, total = 0;
2406
2407 // compute total chunk count
2408 for (i = 0; i < sc->stsc_count; i++) {
2409 unsigned count, chunk_count;
2410
2411 chunk_samples = sc->stsc_data[i].count;
2412 if (i != sc->stsc_count - 1 &&
2413 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2414 av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2415 return;
2416 }
2417
2418 if (sc->samples_per_frame >= 160) { // gsm
2419 count = chunk_samples / sc->samples_per_frame;
2420 } else if (sc->samples_per_frame > 1) {
2421 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2422 count = (chunk_samples+samples-1) / samples;
2423 } else {
2424 count = (chunk_samples+1023) / 1024;
2425 }
2426
2427 if (i < sc->stsc_count - 1)
2428 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2429 else
2430 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2431 total += chunk_count * count;
2432 }
2433
2434 av_dlog(mov->fc, "chunk count %d\n", total);
2435 if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2436 return;
2437 if (av_reallocp_array(&st->index_entries,
2438 st->nb_index_entries + total,
2439 sizeof(*st->index_entries)) < 0) {
2440 st->nb_index_entries = 0;
2441 return;
2442 }
2443 st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2444
2445 // populate index
2446 for (i = 0; i < sc->chunk_count; i++) {
2447 current_offset = sc->chunk_offsets[i];
2448 if (stsc_index + 1 < sc->stsc_count &&
2449 i + 1 == sc->stsc_data[stsc_index + 1].first)
2450 stsc_index++;
2451 chunk_samples = sc->stsc_data[stsc_index].count;
2452
2453 while (chunk_samples > 0) {
2454 AVIndexEntry *e;
2455 unsigned size, samples;
2456
2457 if (sc->samples_per_frame >= 160) { // gsm
2458 samples = sc->samples_per_frame;
2459 size = sc->bytes_per_frame;
2460 } else {
2461 if (sc->samples_per_frame > 1) {
2462 samples = FFMIN((1024 / sc->samples_per_frame)*
2463 sc->samples_per_frame, chunk_samples);
2464 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2465 } else {
2466 samples = FFMIN(1024, chunk_samples);
2467 size = samples * sc->sample_size;
2468 }
2469 }
2470
2471 if (st->nb_index_entries >= total) {
2472 av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2473 return;
2474 }
2475 e = &st->index_entries[st->nb_index_entries++];
2476 e->pos = current_offset;
2477 e->timestamp = current_dts;
2478 e->size = size;
2479 e->min_distance = 0;
2480 e->flags = AVINDEX_KEYFRAME;
2481 av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2482 "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2483 size, samples);
2484
2485 current_offset += size;
2486 current_dts += samples;
2487 chunk_samples -= samples;
2488 }
2489 }
2490 }
2491}
2492
2493static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
2494 AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
2495{
2496 /* try relative path, we do not try the absolute because it can leak information about our
2497 system to an attacker */
2498 if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2499 char filename[1024];
2500 const char *src_path;
2501 int i, l;
2502
2503 /* find a source dir */
2504 src_path = strrchr(src, '/');
2505 if (src_path)
2506 src_path++;
2507 else
2508 src_path = src;
2509
2510 /* find a next level down to target */
2511 for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2512 if (ref->path[l] == '/') {
2513 if (i == ref->nlvl_to - 1)
2514 break;
2515 else
2516 i++;
2517 }
2518
2519 /* compose filename if next level down to target was found */
2520 if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2521 memcpy(filename, src, src_path - src);
2522 filename[src_path - src] = 0;
2523
2524 for (i = 1; i < ref->nlvl_from; i++)
2525 av_strlcat(filename, "../", 1024);
2526
2527 av_strlcat(filename, ref->path + l + 1, 1024);
2528
2529 if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2530 return 0;
2531 }
2532 } else if (use_absolute_path) {
2533 av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
2534 "this is a possible security issue\n");
2535 if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2536 return 0;
2537 }
2538
2539 return AVERROR(ENOENT);
2540}
2541
2542static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
2543{
2544 if (sc->time_scale <= 0) {
2545 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
2546 sc->time_scale = c->time_scale;
2547 if (sc->time_scale <= 0)
2548 sc->time_scale = 1;
2549 }
2550}
2551
2552static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2553{
2554 AVStream *st;
2555 MOVStreamContext *sc;
2556 int ret;
2557
2558 st = avformat_new_stream(c->fc, NULL);
2559 if (!st) return AVERROR(ENOMEM);
2560 st->id = c->fc->nb_streams;
2561 sc = av_mallocz(sizeof(MOVStreamContext));
2562 if (!sc) return AVERROR(ENOMEM);
2563
2564 st->priv_data = sc;
2565 st->codec->codec_type = AVMEDIA_TYPE_DATA;
2566 sc->ffindex = st->index;
2567 c->trak_index = st->index;
2568
2569 if ((ret = mov_read_default(c, pb, atom)) < 0)
2570 return ret;
2571
2572 c->trak_index = -1;
2573
2574 /* sanity checks */
2575 if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2576 (!sc->sample_size && !sc->sample_count))) {
2577 av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2578 st->index);
2579 return 0;
2580 }
2581
2582 fix_timescale(c, sc);
2583
2584 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2585
2586 mov_build_index(c, st);
2587
2588 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2589 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2590 if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
2591 c->use_absolute_path, c->fc) < 0)
2592 av_log(c->fc, AV_LOG_ERROR,
2593 "stream %d, error opening alias: path='%s', dir='%s', "
2594 "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2595 st->index, dref->path, dref->dir, dref->filename,
2596 dref->volume, dref->nlvl_from, dref->nlvl_to);
2597 } else {
2598 sc->pb = c->fc->pb;
2599 sc->pb_is_copied = 1;
2600 }
2601
2602 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2603 if (!st->sample_aspect_ratio.num &&
2604 (st->codec->width != sc->width || st->codec->height != sc->height)) {
2605 st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2606 ((double)st->codec->width * sc->height), INT_MAX);
2607 }
2608
2609 if (st->duration > 0)
2610 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2611 sc->time_scale*st->nb_frames, st->duration, INT_MAX);
2612
2613#if FF_API_R_FRAME_RATE
2614 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
2615 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
2616 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
2617#endif
2618 }
2619
2620 // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2621 if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2622 st->codec->codec_tag != MKTAG('a', 'v', 'c', '1')) {
2623 ff_generate_avci_extradata(st);
2624 }
2625
2626 switch (st->codec->codec_id) {
2627#if CONFIG_H261_DECODER
2628 case AV_CODEC_ID_H261:
2629#endif
2630#if CONFIG_H263_DECODER
2631 case AV_CODEC_ID_H263:
2632#endif
2633#if CONFIG_MPEG4_DECODER
2634 case AV_CODEC_ID_MPEG4:
2635#endif
2636 st->codec->width = 0; /* let decoder init width/height */
2637 st->codec->height= 0;
2638 break;
2639 }
2640
2641 /* Do not need those anymore. */
2642 av_freep(&sc->chunk_offsets);
2643 av_freep(&sc->stsc_data);
2644 av_freep(&sc->sample_sizes);
2645 av_freep(&sc->keyframes);
2646 av_freep(&sc->stts_data);
2647 av_freep(&sc->stps_data);
2648 av_freep(&sc->rap_group);
2649
2650 return 0;
2651}
2652
2653static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2654{
2655 int ret;
2656 c->itunes_metadata = 1;
2657 ret = mov_read_default(c, pb, atom);
2658 c->itunes_metadata = 0;
2659 return ret;
2660}
2661
2662static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2663{
2664 uint32_t count;
2665 uint32_t i;
2666
2667 if (atom.size < 8)
2668 return 0;
2669
2670 avio_skip(pb, 4);
2671 count = avio_rb32(pb);
2672 if (count > UINT_MAX / sizeof(*c->meta_keys)) {
2673 av_log(c->fc, AV_LOG_ERROR,
2674 "The 'keys' atom with the invalid key count: %d\n", count);
2675 return AVERROR_INVALIDDATA;
2676 }
2677
2678 c->meta_keys_count = count + 1;
2679 c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
2680 if (!c->meta_keys)
2681 return AVERROR(ENOMEM);
2682
2683 for (i = 1; i <= count; ++i) {
2684 uint32_t key_size = avio_rb32(pb);
2685 uint32_t type = avio_rl32(pb);
2686 if (key_size < 8) {
2687 av_log(c->fc, AV_LOG_ERROR,
2688 "The key# %d in meta has invalid size: %d\n", i, key_size);
2689 return AVERROR_INVALIDDATA;
2690 }
2691 key_size -= 8;
2692 if (type != MKTAG('m','d','t','a')) {
2693 avio_skip(pb, key_size);
2694 }
2695 c->meta_keys[i] = av_mallocz(key_size + 1);
2696 if (!c->meta_keys[i])
2697 return AVERROR(ENOMEM);
2698 avio_read(pb, c->meta_keys[i], key_size);
2699 }
2700
2701 return 0;
2702}
2703
2704static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2705{
2706 while (atom.size > 8) {
2707 uint32_t tag = avio_rl32(pb);
2708 atom.size -= 4;
2709 if (tag == MKTAG('h','d','l','r')) {
2710 avio_seek(pb, -8, SEEK_CUR);
2711 atom.size += 8;
2712 return mov_read_default(c, pb, atom);
2713 }
2714 }
2715 return 0;
2716}
2717
2718static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2719{
2720 int i;
2721 int width;
2722 int height;
2723 int64_t disp_transform[2];
2724 int display_matrix[3][2];
2725 AVStream *st;
2726 MOVStreamContext *sc;
2727 int version;
2728 int flags;
2729
2730 if (c->fc->nb_streams < 1)
2731 return 0;
2732 st = c->fc->streams[c->fc->nb_streams-1];
2733 sc = st->priv_data;
2734
2735 version = avio_r8(pb);
2736 flags = avio_rb24(pb);
2737 st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0;
2738
2739 if (version == 1) {
2740 avio_rb64(pb);
2741 avio_rb64(pb);
2742 } else {
2743 avio_rb32(pb); /* creation time */
2744 avio_rb32(pb); /* modification time */
2745 }
2746 st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2747 avio_rb32(pb); /* reserved */
2748
2749 /* highlevel (considering edits) duration in movie timebase */
2750 (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2751 avio_rb32(pb); /* reserved */
2752 avio_rb32(pb); /* reserved */
2753
2754 avio_rb16(pb); /* layer */
2755 avio_rb16(pb); /* alternate group */
2756 avio_rb16(pb); /* volume */
2757 avio_rb16(pb); /* reserved */
2758
2759 //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2760 // they're kept in fixed point format through all calculations
2761 // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
2762 for (i = 0; i < 3; i++) {
2763 display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2764 display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2765 avio_rb32(pb); // 2.30 fixed point (not used)
2766 }
2767
2768 width = avio_rb32(pb); // 16.16 fixed point track width
2769 height = avio_rb32(pb); // 16.16 fixed point track height
2770 sc->width = width >> 16;
2771 sc->height = height >> 16;
2772
2773 //Assign clockwise rotate values based on transform matrix so that
2774 //we can compensate for iPhone orientation during capture.
2775
2776 if (display_matrix[0][0] == 65536 && display_matrix[1][1] == 65536) {
2777 av_dict_set(&st->metadata, "rotate", "0", 0);
2778 st->rotation_degree = 0;
2779 }
2780
2781 if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) {
2782 av_dict_set(&st->metadata, "rotate", "90", 0);
2783 st->rotation_degree = 1;
2784 }
2785
2786 if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
2787 av_dict_set(&st->metadata, "rotate", "180", 0);
2788 st->rotation_degree = 2;
2789 }
2790
2791 if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) {
2792 av_dict_set(&st->metadata, "rotate", "270", 0);
2793 st->rotation_degree = 3;
2794 }
2795
2796 // transform the display width/height according to the matrix
2797 // skip this if the display matrix is the default identity matrix
2798 // or if it is rotating the picture, ex iPhone 3GS
2799 // to keep the same scale, use [width height 1<<16]
2800 if (width && height &&
2801 ((display_matrix[0][0] != 65536 ||
2802 display_matrix[1][1] != 65536) &&
2803 !display_matrix[0][1] &&
2804 !display_matrix[1][0] &&
2805 !display_matrix[2][0] && !display_matrix[2][1])) {
2806 for (i = 0; i < 2; i++)
2807 disp_transform[i] =
2808 (int64_t) width * display_matrix[0][i] +
2809 (int64_t) height * display_matrix[1][i] +
2810 ((int64_t) display_matrix[2][i] << 16);
2811
2812 //sample aspect ratio is new width/height divided by old width/height
2813 st->sample_aspect_ratio = av_d2q(
2814 ((double) disp_transform[0] * height) /
2815 ((double) disp_transform[1] * width), INT_MAX);
2816 }
2817 return 0;
2818}
2819
2820static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2821{
2822 MOVFragment *frag = &c->fragment;
2823 MOVTrackExt *trex = NULL;
2824 int flags, track_id, i;
2825
2826 avio_r8(pb); /* version */
2827 flags = avio_rb24(pb);
2828
2829 track_id = avio_rb32(pb);
2830 if (!track_id)
2831 return AVERROR_INVALIDDATA;
2832 frag->track_id = track_id;
2833 for (i = 0; i < c->trex_count; i++)
2834 if (c->trex_data[i].track_id == frag->track_id) {
2835 trex = &c->trex_data[i];
2836 break;
2837 }
2838 if (!trex) {
2839 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2840 return AVERROR_INVALIDDATA;
2841 }
2842
2843 frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
2844 avio_rb64(pb) : frag->moof_offset;
2845 frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2846
2847 frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2848 avio_rb32(pb) : trex->duration;
2849 frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2850 avio_rb32(pb) : trex->size;
2851 frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2852 avio_rb32(pb) : trex->flags;
2853 av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2854 return 0;
2855}
2856
2857static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2858{
2859 c->chapter_track = avio_rb32(pb);
2860 return 0;
2861}
2862
2863static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2864{
2865 MOVTrackExt *trex;
2866 int err;
2867
2868 if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2869 return AVERROR_INVALIDDATA;
2870 if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2871 sizeof(*c->trex_data))) < 0) {
2872 c->trex_count = 0;
2873 return err;
2874 }
2875
2876 c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
2877
2878 trex = &c->trex_data[c->trex_count++];
2879 avio_r8(pb); /* version */
2880 avio_rb24(pb); /* flags */
2881 trex->track_id = avio_rb32(pb);
2882 trex->stsd_id = avio_rb32(pb);
2883 trex->duration = avio_rb32(pb);
2884 trex->size = avio_rb32(pb);
2885 trex->flags = avio_rb32(pb);
2886 return 0;
2887}
2888
2889static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2890{
2891 MOVFragment *frag = &c->fragment;
2892 AVStream *st = NULL;
2893 MOVStreamContext *sc;
2894 MOVStts *ctts_data;
2895 uint64_t offset;
2896 int64_t dts;
2897 int data_offset = 0;
2898 unsigned entries, first_sample_flags = frag->flags;
2899 int flags, distance, i, found_keyframe = 0, err;
2900
2901 for (i = 0; i < c->fc->nb_streams; i++) {
2902 if (c->fc->streams[i]->id == frag->track_id) {
2903 st = c->fc->streams[i];
2904 break;
2905 }
2906 }
2907 if (!st) {
2908 av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2909 return AVERROR_INVALIDDATA;
2910 }
2911 sc = st->priv_data;
2912 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
2913 return 0;
2914 avio_r8(pb); /* version */
2915 flags = avio_rb24(pb);
2916 entries = avio_rb32(pb);
2917 av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2918
2919 /* Always assume the presence of composition time offsets.
2920 * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2921 * 1) in the initial movie, there are no samples.
2922 * 2) in the first movie fragment, there is only one sample without composition time offset.
2923 * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2924 if (!sc->ctts_count && sc->sample_count)
2925 {
2926 /* Complement ctts table if moov atom doesn't have ctts atom. */
2927 ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2928 if (!ctts_data)
2929 return AVERROR(ENOMEM);
2930 sc->ctts_data = ctts_data;
2931 sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2932 sc->ctts_data[sc->ctts_count].duration = 0;
2933 sc->ctts_count++;
2934 }
2935 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2936 return AVERROR_INVALIDDATA;
2937 if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2938 sizeof(*sc->ctts_data))) < 0) {
2939 sc->ctts_count = 0;
2940 return err;
2941 }
2942 if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
2943 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
2944 dts = sc->track_end - sc->time_offset;
2945 offset = frag->base_data_offset + data_offset;
2946 distance = 0;
2947 av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
2948 for (i = 0; i < entries && !pb->eof_reached; i++) {
2949 unsigned sample_size = frag->size;
2950 int sample_flags = i ? frag->flags : first_sample_flags;
2951 unsigned sample_duration = frag->duration;
2952 int keyframe = 0;
2953
2954 if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2955 if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
2956 if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
2957 sc->ctts_data[sc->ctts_count].count = 1;
2958 sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2959 avio_rb32(pb) : 0;
2960 mov_update_dts_shift(sc, sc->ctts_data[sc->ctts_count].duration);
2961 sc->ctts_count++;
2962 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2963 keyframe = 1;
2964 else if (!found_keyframe)
2965 keyframe = found_keyframe =
2966 !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2967 MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
2968 if (keyframe)
2969 distance = 0;
2970 av_add_index_entry(st, offset, dts, sample_size, distance,
2971 keyframe ? AVINDEX_KEYFRAME : 0);
2972 av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2973 "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
2974 offset, dts, sample_size, distance, keyframe);
2975 distance++;
2976 dts += sample_duration;
2977 offset += sample_size;
2978 sc->data_size += sample_size;
2979 }
2980
2981 if (pb->eof_reached)
2982 return AVERROR_EOF;
2983
2984 frag->moof_offset = offset;
2985 st->duration = sc->track_end = dts + sc->time_offset;
2986 return 0;
2987}
2988
2989/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2990/* like the files created with Adobe Premiere 5.0, for samples see */
2991/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
2992static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2993{
2994 int err;
2995
2996 if (atom.size < 8)
2997 return 0; /* continue */
2998 if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
2999 avio_skip(pb, atom.size - 4);
3000 return 0;
3001 }
3002 atom.type = avio_rl32(pb);
3003 atom.size -= 8;
3004 if (atom.type != MKTAG('m','d','a','t')) {
3005 avio_skip(pb, atom.size);
3006 return 0;
3007 }
3008 err = mov_read_mdat(c, pb, atom);
3009 return err;
3010}
3011
3012static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3013{
3014#if CONFIG_ZLIB
3015 AVIOContext ctx;
3016 uint8_t *cmov_data;
3017 uint8_t *moov_data; /* uncompressed data */
3018 long cmov_len, moov_len;
3019 int ret = -1;
3020
3021 avio_rb32(pb); /* dcom atom */
3022 if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3023 return AVERROR_INVALIDDATA;
3024 if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3025 av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3026 return AVERROR_INVALIDDATA;
3027 }
3028 avio_rb32(pb); /* cmvd atom */
3029 if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3030 return AVERROR_INVALIDDATA;
3031 moov_len = avio_rb32(pb); /* uncompressed size */
3032 cmov_len = atom.size - 6 * 4;
3033
3034 cmov_data = av_malloc(cmov_len);
3035 if (!cmov_data)
3036 return AVERROR(ENOMEM);
3037 moov_data = av_malloc(moov_len);
3038 if (!moov_data) {
3039 av_free(cmov_data);
3040 return AVERROR(ENOMEM);
3041 }
3042 avio_read(pb, cmov_data, cmov_len);
3043 if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3044 goto free_and_return;
3045 if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3046 goto free_and_return;
3047 atom.type = MKTAG('m','o','o','v');
3048 atom.size = moov_len;
3049 ret = mov_read_default(c, &ctx, atom);
3050free_and_return:
3051 av_free(moov_data);
3052 av_free(cmov_data);
3053 return ret;
3054#else
3055 av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3056 return AVERROR(ENOSYS);
3057#endif
3058}
3059
3060/* edit list atom */
3061static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3062{
3063 MOVStreamContext *sc;
3064 int i, edit_count, version, edit_start_index = 0;
3065 int unsupported = 0;
3066
3067 if (c->fc->nb_streams < 1 || c->ignore_editlist)
3068 return 0;
3069 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3070
3071 version = avio_r8(pb); /* version */
3072 avio_rb24(pb); /* flags */
3073 edit_count = avio_rb32(pb); /* entries */
3074
3075 if ((uint64_t)edit_count*12+8 > atom.size)
3076 return AVERROR_INVALIDDATA;
3077
3078 av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3079 for (i=0; i<edit_count; i++){
3080 int64_t time;
3081 int64_t duration;
3082 int rate;
3083 if (version == 1) {
3084 duration = avio_rb64(pb);
3085 time = avio_rb64(pb);
3086 } else {
3087 duration = avio_rb32(pb); /* segment duration */
3088 time = (int32_t)avio_rb32(pb); /* media time */
3089 }
3090 rate = avio_rb32(pb);
3091 if (i == 0 && time == -1) {
3092 sc->empty_duration = duration;
3093 edit_start_index = 1;
3094 } else if (i == edit_start_index && time >= 0)
3095 sc->start_time = time;
3096 else
3097 unsupported = 1;
3098
3099 av_dlog(c->fc, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3100 duration, time, rate / 65536.0);
3101 }
3102
3103 if (unsupported)
3104 av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
3105 "a/v desync might occur, patch welcome\n");
3106
3107 return 0;
3108}
3109
3110static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3111{
3112 MOVStreamContext *sc;
3113
3114 if (c->fc->nb_streams < 1)
3115 return AVERROR_INVALIDDATA;
3116 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3117 sc->timecode_track = avio_rb32(pb);
3118 return 0;
3119}
3120
3121static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3122{
3123 int ret;
3124 uint8_t uuid[16];
3125 static const uint8_t uuid_isml_manifest[] = {
3126 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3127 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3128 };
3129
3130 if (atom.size < sizeof(uuid) || atom.size == INT64_MAX)
3131 return AVERROR_INVALIDDATA;
3132
3133 ret = avio_read(pb, uuid, sizeof(uuid));
3134 if (ret < 0) {
3135 return ret;
3136 } else if (ret != sizeof(uuid)) {
3137 return AVERROR_INVALIDDATA;
3138 }
3139 if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3140 uint8_t *buffer, *ptr;
3141 char *endptr;
3142 size_t len = atom.size - sizeof(uuid);
3143
3144 if (len < 4) {
3145 return AVERROR_INVALIDDATA;
3146 }
3147 ret = avio_skip(pb, 4); // zeroes
3148 len -= 4;
3149
3150 buffer = av_mallocz(len + 1);
3151 if (!buffer) {
3152 return AVERROR(ENOMEM);
3153 }
3154 ret = avio_read(pb, buffer, len);
3155 if (ret < 0) {
3156 av_free(buffer);
3157 return ret;
3158 } else if (ret != len) {
3159 av_free(buffer);
3160 return AVERROR_INVALIDDATA;
3161 }
3162
3163 ptr = buffer;
3164 while ((ptr = av_stristr(ptr, "systemBitrate=\"")) != NULL) {
3165 ptr += sizeof("systemBitrate=\"") - 1;
3166 c->bitrates_count++;
3167 c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3168 if (!c->bitrates) {
3169 c->bitrates_count = 0;
3170 av_free(buffer);
3171 return AVERROR(ENOMEM);
3172 }
3173 errno = 0;
3174 ret = strtol(ptr, &endptr, 10);
3175 if (ret < 0 || errno || *endptr != '"') {
3176 c->bitrates[c->bitrates_count - 1] = 0;
3177 } else {
3178 c->bitrates[c->bitrates_count - 1] = ret;
3179 }
3180 }
3181
3182 av_free(buffer);
3183 }
3184 return 0;
3185}
3186
3187static void mov_id32_date2year(AVDictionary **m)
3188{
3189 AVDictionaryEntry *t = NULL;
3190 if (t = av_dict_get(*m, "date", t, AV_DICT_MATCH_CASE)) {
3191 av_dict_set(m, "year", t->value, 0);
3192 av_log(NULL, AV_LOG_INFO, "[%s:%d]========date:%s\n", __FUNCTION__, __LINE__, t->value);
3193 }
3194}
3195
3196static int mov_read_id32(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3197{
3198 uint8_t version;
3199 uint32_t flags;
3200 uint8_t pad;
3201 uint16_t langcode;
3202 uint16_t shortbytes;
3203 char language[4] = {0};
3204 uint32_t str_size;
3205 AVFormatContext *s = c->fc;
3206 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
3207
3208 str_size = atom.size;
3209 version = avio_r8(pb); // version
3210 flags = avio_rb24(pb); //flags
3211 shortbytes = avio_rb16(pb);
3212 pad = (shortbytes & 0x8000) >> 15; //pad
3213 langcode = shortbytes & 0x7ffff; //language
3214 ff_mov_lang_to_iso639(langcode, language);
3215 str_size -= 6;
3216 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
3217 if (id3v2_extra_meta) {
3218 int err = ff_id3v2_parse_apic(s, &id3v2_extra_meta);
3219 if (err < 0) {
3220 av_log(NULL, AV_LOG_INFO, "[%s:%d]ff_id3v2_parse_apic err:%d\n", __FUNCTION__, __LINE__, err);
3221 return err;
3222 }
3223 }
3224 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
3225 mov_id32_date2year(&s->metadata);
3226
3227 return 0;
3228}
3229
3230static const MOVParseTableEntry mov_default_parse_table[] = {
3231{ MKTAG('A','C','L','R'), mov_read_avid },
3232{ MKTAG('A','P','R','G'), mov_read_avid },
3233{ MKTAG('A','A','L','P'), mov_read_avid },
3234{ MKTAG('A','R','E','S'), mov_read_ares },
3235{ MKTAG('a','v','s','s'), mov_read_avss },
3236{ MKTAG('c','h','p','l'), mov_read_chpl },
3237{ MKTAG('c','o','6','4'), mov_read_stco },
3238{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3239{ MKTAG('d','i','n','f'), mov_read_default },
3240{ MKTAG('d','r','e','f'), mov_read_dref },
3241{ MKTAG('e','d','t','s'), mov_read_default },
3242{ MKTAG('e','l','s','t'), mov_read_elst },
3243{ MKTAG('e','n','d','a'), mov_read_enda },
3244{ MKTAG('f','i','e','l'), mov_read_fiel },
3245{ MKTAG('f','t','y','p'), mov_read_ftyp },
3246{ MKTAG('g','l','b','l'), mov_read_glbl },
3247{ MKTAG('h','d','l','r'), mov_read_hdlr },
3248{ MKTAG('i','l','s','t'), mov_read_ilst },
3249{ MKTAG('j','p','2','h'), mov_read_jp2h },
3250{ MKTAG('m','d','a','t'), mov_read_mdat },
3251{ MKTAG('m','d','h','d'), mov_read_mdhd },
3252{ MKTAG('m','d','i','a'), mov_read_default },
3253{ MKTAG('m','e','t','a'), mov_read_meta },
3254{ MKTAG('m','i','n','f'), mov_read_default },
3255{ MKTAG('m','o','o','f'), mov_read_moof },
3256{ MKTAG('m','o','o','v'), mov_read_moov },
3257{ MKTAG('m','v','e','x'), mov_read_default },
3258{ MKTAG('m','v','h','d'), mov_read_mvhd },
3259{ MKTAG('S','M','I',' '), mov_read_svq3 },
3260{ MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3261{ MKTAG('a','v','c','C'), mov_read_glbl },
3262{ MKTAG('p','a','s','p'), mov_read_pasp },
3263{ MKTAG('s','t','b','l'), mov_read_default },
3264{ MKTAG('s','t','c','o'), mov_read_stco },
3265{ MKTAG('s','t','p','s'), mov_read_stps },
3266{ MKTAG('s','t','r','f'), mov_read_strf },
3267{ MKTAG('s','t','s','c'), mov_read_stsc },
3268{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3269{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3270{ MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3271{ MKTAG('s','t','t','s'), mov_read_stts },
3272{ MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3273{ MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3274{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3275{ MKTAG('t','r','a','k'), mov_read_trak },
3276{ MKTAG('t','r','a','f'), mov_read_default },
3277{ MKTAG('t','r','e','f'), mov_read_default },
3278{ MKTAG('t','m','c','d'), mov_read_tmcd },
3279{ MKTAG('c','h','a','p'), mov_read_chap },
3280{ MKTAG('t','r','e','x'), mov_read_trex },
3281{ MKTAG('t','r','u','n'), mov_read_trun },
3282{ MKTAG('u','d','t','a'), mov_read_default },
3283{ MKTAG('w','a','v','e'), mov_read_wave },
3284{ MKTAG('e','s','d','s'), mov_read_esds },
3285{ MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3286{ MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3287{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3288{ MKTAG('w','f','e','x'), mov_read_wfex },
3289{ MKTAG('c','m','o','v'), mov_read_cmov },
3290{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3291{ MKTAG('d','v','c','1'), mov_read_dvc1 },
3292{ MKTAG('s','b','g','p'), mov_read_sbgp },
3293{ MKTAG('h','v','c','C'), mov_read_glbl },
3294{ MKTAG('u','u','i','d'), mov_read_uuid },
3295{ MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
3296{ MKTAG('I','D','3','2'), mov_read_id32 },
3297
3298{ 0, NULL }
3299};
3300
3301static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3302{
3303 int64_t total_size = 0;
3304 MOVAtom a;
3305 int i;
3306
3307 if (atom.size < 0)
3308 atom.size = INT64_MAX;
3309 while (total_size + 8 <= atom.size && !url_feof(pb)) {
3310 int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3311 a.size = atom.size;
3312 a.type=0;
3313 if (atom.size >= 8) {
3314 a.size = avio_rb32(pb);
3315 a.type = avio_rl32(pb);
3316 if (atom.type != MKTAG('r','o','o','t') &&
3317 atom.type != MKTAG('m','o','o','v'))
3318 {
3319 if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
3320 {
3321 av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
3322 avio_skip(pb, -8);
3323 return 0;
3324 }
3325 }
3326 total_size += 8;
3327 if (a.size == 1) { /* 64 bit extended size */
3328 a.size = avio_rb64(pb) - 8;
3329 total_size += 8;
3330 }
3331 }
3332 av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3333 a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3334 if (a.size == 0) {
3335 a.size = atom.size - total_size + 8;
3336 }
3337 a.size -= 8;
3338 if (a.size < 0)
3339 break;
3340 a.size = FFMIN(a.size, atom.size - total_size);
3341
3342 for (i = 0; mov_default_parse_table[i].type; i++)
3343 if (mov_default_parse_table[i].type == a.type) {
3344 parse = mov_default_parse_table[i].parse;
3345 break;
3346 }
3347
3348 // container is user data
3349 if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3350 atom.type == MKTAG('i','l','s','t')))
3351 parse = mov_read_udta_string;
3352
3353 // Supports parsing the QuickTime Metadata Keys.
3354 // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
3355 if (!parse && c->found_hdlr_mdta &&
3356 atom.type == MKTAG('m','e','t','a') &&
3357 a.type == MKTAG('k','e','y','s')) {
3358 parse = mov_read_keys;
3359 }
3360
3361 if (!parse) { /* skip leaf atoms data */
3362 avio_skip(pb, a.size);
3363 } else {
3364 int64_t start_pos = avio_tell(pb);
3365 int64_t left;
3366 int err = parse(c, pb, a);
3367 if (err < 0)
3368 return err;
3369 if (c->found_moov && c->found_mdat &&
3370 ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3371 start_pos + a.size == avio_size(pb))) {
3372 if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3373 c->next_root_atom = start_pos + a.size;
3374 return 0;
3375 }
3376 left = a.size - avio_tell(pb) + start_pos;
3377 if (left > 0) /* skip garbage at atom end */
3378 avio_skip(pb, left);
3379 else if (left < 0) {
3380 av_log(c->fc, AV_LOG_WARNING,
3381 "overread end of atom '%.4s' by %"PRId64" bytes\n",
3382 (char*)&a.type, -left);
3383 avio_seek(pb, left, SEEK_CUR);
3384 }
3385 }
3386
3387 total_size += a.size;
3388 }
3389
3390 if (total_size < atom.size && atom.size < 0x7ffff)
3391 avio_skip(pb, atom.size - total_size);
3392
3393 return 0;
3394}
3395
3396static int mov_probe(AVProbeData *p)
3397{
3398 int64_t offset;
3399 uint32_t tag;
3400 int score = 0;
3401 int moov_offset = -1;
3402
3403 /* check file header */
3404 offset = 0;
3405 for (;;) {
3406 /* ignore invalid offset */
3407 if ((offset + 8) > p->buf_size)
3408 break;
3409 tag = AV_RL32(p->buf + offset + 4);
3410 switch(tag) {
3411 /* check for obvious tags */
3412 case MKTAG('m','o','o','v'):
3413 moov_offset = offset + 4;
3414 case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
3415 case MKTAG('m','d','a','t'):
3416 case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3417 case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3418 case MKTAG('f','t','y','p'):
3419 if (AV_RB32(p->buf+offset) < 8 &&
3420 (AV_RB32(p->buf+offset) != 1 ||
3421 offset + 12 > (unsigned int)p->buf_size ||
3422 AV_RB64(p->buf+offset + 8) == 0)) {
3423 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3424 } else {
3425 score = AVPROBE_SCORE_MAX;
3426 }
3427 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3428 break;
3429 /* those are more common words, so rate then a bit less */
3430 case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3431 case MKTAG('w','i','d','e'):
3432 case MKTAG('f','r','e','e'):
3433 case MKTAG('j','u','n','k'):
3434 case MKTAG('p','i','c','t'):
3435 score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
3436 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3437 break;
3438 case MKTAG(0x82,0x82,0x7f,0x7d):
3439 case MKTAG('s','k','i','p'):
3440 case MKTAG('u','u','i','d'):
3441 case MKTAG('p','r','f','l'):
3442 /* if we only find those cause probedata is too small at least rate them */
3443 score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
3444 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3445 break;
3446 default:
3447 offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
3448 }
3449 }
3450 if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
3451 /* moov atom in the header - we should make sure that this is not a
3452 * MOV-packed MPEG-PS */
3453 offset = moov_offset;
3454
3455 while(offset < (p->buf_size - 16)){ /* Sufficient space */
3456 /* We found an actual hdlr atom */
3457 if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
3458 AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
3459 AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
3460 av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
3461 /* We found a media handler reference atom describing an
3462 * MPEG-PS-in-MOV, return a
3463 * low score to force expanding the probe window until
3464 * mpegps_probe finds what it needs */
3465 return 5;
3466 }else
3467 /* Keep looking */
3468 offset+=2;
3469 }
3470 }
3471
3472 return score;
3473}
3474
3475// must be done after parsing all trak because there's no order requirement
3476static void mov_read_chapters(AVFormatContext *s)
3477{
3478 MOVContext *mov = s->priv_data;
3479 AVStream *st = NULL;
3480 MOVStreamContext *sc;
3481 int64_t cur_pos;
3482 int i;
3483
3484 for (i = 0; i < s->nb_streams; i++)
3485 if (s->streams[i]->id == mov->chapter_track) {
3486 st = s->streams[i];
3487 break;
3488 }
3489 if (!st) {
3490 av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3491 return;
3492 }
3493
3494 st->discard = AVDISCARD_ALL;
3495 sc = st->priv_data;
3496 cur_pos = avio_tell(sc->pb);
3497
3498 for (i = 0; i < st->nb_index_entries; i++) {
3499 AVIndexEntry *sample = &st->index_entries[i];
3500 int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3501 uint8_t *title;
3502 uint16_t ch;
3503 int len, title_len;
3504
3505 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3506 av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3507 goto finish;
3508 }
3509
3510 // the first two bytes are the length of the title
3511 len = avio_rb16(sc->pb);
3512 if (len > sample->size-2)
3513 continue;
3514 title_len = 2*len + 1;
3515 if (!(title = av_mallocz(title_len)))
3516 goto finish;
3517
3518 // The samples could theoretically be in any encoding if there's an encd
3519 // atom following, but in practice are only utf-8 or utf-16, distinguished
3520 // instead by the presence of a BOM
3521 if (!len) {
3522 title[0] = 0;
3523 } else {
3524 ch = avio_rb16(sc->pb);
3525 if (ch == 0xfeff)
3526 avio_get_str16be(sc->pb, len, title, title_len);
3527 else if (ch == 0xfffe)
3528 avio_get_str16le(sc->pb, len, title, title_len);
3529 else {
3530 AV_WB16(title, ch);
3531 if (len == 1 || len == 2)
3532 title[len] = 0;
3533 else
3534 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
3535 }
3536 }
3537
3538 avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3539 av_freep(&title);
3540 }
3541finish:
3542 avio_seek(sc->pb, cur_pos, SEEK_SET);
3543}
3544
3545static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
3546 uint32_t value, int flags)
3547{
3548 AVTimecode tc;
3549 char buf[AV_TIMECODE_STR_SIZE];
3550 AVRational rate = {st->codec->time_base.den,
3551 st->codec->time_base.num};
3552 int ret = av_timecode_init(&tc, rate, flags, 0, s);
3553 if (ret < 0)
3554 return ret;
3555 av_dict_set(&st->metadata, "timecode",
3556 av_timecode_make_string(&tc, buf, value), 0);
3557 return 0;
3558}
3559
3560static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
3561{
3562 MOVStreamContext *sc = st->priv_data;
3563 int flags = 0;
3564 int64_t cur_pos = avio_tell(sc->pb);
3565 uint32_t value;
3566
3567 if (!st->nb_index_entries)
3568 return -1;
3569
3570 avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
3571 value = avio_rb32(s->pb);
3572
3573 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
3574 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
3575 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
3576
3577 /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
3578 * not the case) and thus assume "frame number format" instead of QT one.
3579 * No sample with tmcd track can be found with a QT timecode at the moment,
3580 * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
3581 * format). */
3582 parse_timecode_in_framenum_format(s, st, value, flags);
3583
3584 avio_seek(sc->pb, cur_pos, SEEK_SET);
3585 return 0;
3586}
3587
3588static int mov_read_close(AVFormatContext *s)
3589{
3590 MOVContext *mov = s->priv_data;
3591 int i, j;
3592
3593 for (i = 0; i < s->nb_streams; i++) {
3594 AVStream *st = s->streams[i];
3595 MOVStreamContext *sc = st->priv_data;
3596
3597 av_freep(&sc->ctts_data);
3598 for (j = 0; j < sc->drefs_count; j++) {
3599 av_freep(&sc->drefs[j].path);
3600 av_freep(&sc->drefs[j].dir);
3601 }
3602 av_freep(&sc->drefs);
3603 if (!sc->pb_is_copied)
3604 avio_close(sc->pb);
3605 sc->pb = NULL;
3606 av_freep(&sc->chunk_offsets);
3607 av_freep(&sc->keyframes);
3608 av_freep(&sc->sample_sizes);
3609 av_freep(&sc->stps_data);
3610 av_freep(&sc->stsc_data);
3611 av_freep(&sc->stts_data);
3612 }
3613
3614 if (mov->dv_demux) {
3615 for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
3616 av_freep(&mov->dv_fctx->streams[i]->codec);
3617 av_freep(&mov->dv_fctx->streams[i]);
3618 }
3619 av_freep(&mov->dv_fctx);
3620 av_freep(&mov->dv_demux);
3621 }
3622
3623 if (mov->meta_keys) {
3624 for (i = 1; i < mov->meta_keys_count; i++) {
3625 av_freep(&mov->meta_keys[i]);
3626 }
3627 av_freep(&mov->meta_keys);
3628 }
3629
3630 av_freep(&mov->trex_data);
3631 av_freep(&mov->bitrates);
3632
3633 return 0;
3634}
3635
3636static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
3637{
3638 int i;
3639
3640 for (i = 0; i < s->nb_streams; i++) {
3641 AVStream *st = s->streams[i];
3642 MOVStreamContext *sc = st->priv_data;
3643
3644 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
3645 sc->timecode_track == tmcd_id)
3646 return 1;
3647 }
3648 return 0;
3649}
3650
3651/* look for a tmcd track not referenced by any video track, and export it globally */
3652static void export_orphan_timecode(AVFormatContext *s)
3653{
3654 int i;
3655
3656 for (i = 0; i < s->nb_streams; i++) {
3657 AVStream *st = s->streams[i];
3658
3659 if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
3660 !tmcd_is_referenced(s, i + 1)) {
3661 AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
3662 if (tcr) {
3663 av_dict_set(&s->metadata, "timecode", tcr->value, 0);
3664 break;
3665 }
3666 }
3667 }
3668}
3669
3670static int mov_read_header(AVFormatContext *s)
3671{
3672 MOVContext *mov = s->priv_data;
3673 AVIOContext *pb = s->pb;
3674 int i, j, err;
3675 MOVAtom atom = { AV_RL32("root") };
3676
3677 mov->fc = s;
3678 mov->trak_index = -1;
3679 /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3680 if (pb->seekable)
3681 atom.size = avio_size(pb);
3682 else
3683 atom.size = INT64_MAX;
3684
3685 /* check MOV header */
3686 if ((err = mov_read_default(mov, pb, atom)) < 0) {
3687 av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3688 mov_read_close(s);
3689 return err;
3690 }
3691 if (!mov->found_moov) {
3692 av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3693 mov_read_close(s);
3694 return AVERROR_INVALIDDATA;
3695 }
3696 av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3697
3698 if (pb->seekable) {
3699 if (mov->chapter_track > 0)
3700 mov_read_chapters(s);
3701 for (i = 0; i < s->nb_streams; i++)
3702 if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
3703 mov_read_timecode_track(s, s->streams[i]);
3704 }
3705
3706 /* copy timecode metadata from tmcd tracks to the related video streams */
3707 for (i = 0; i < s->nb_streams; i++) {
3708 AVStream *st = s->streams[i];
3709 MOVStreamContext *sc = st->priv_data;
3710 if (sc->timecode_track > 0) {
3711 AVDictionaryEntry *tcr;
3712 int tmcd_st_id = -1;
3713
3714 for (j = 0; j < s->nb_streams; j++)
3715 if (s->streams[j]->id == sc->timecode_track)
3716 tmcd_st_id = j;
3717
3718 if (tmcd_st_id < 0 || tmcd_st_id == i)
3719 continue;
3720 tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
3721 if (tcr)
3722 av_dict_set(&st->metadata, "timecode", tcr->value, 0);
3723 }
3724 }
3725 export_orphan_timecode(s);
3726
3727 for (i = 0; i < s->nb_streams; i++) {
3728 AVStream *st = s->streams[i];
3729 MOVStreamContext *sc = st->priv_data;
3730 fix_timescale(mov, sc);
3731 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
3732 st->skip_samples = sc->start_pad;
3733 }
3734 }
3735
3736 if (mov->trex_data) {
3737 for (i = 0; i < s->nb_streams; i++) {
3738 AVStream *st = s->streams[i];
3739 MOVStreamContext *sc = st->priv_data;
3740 if (st->duration > 0)
3741 st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3742 }
3743 }
3744
3745 for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
3746 if (mov->bitrates[i]) {
3747 s->streams[i]->codec->bit_rate = mov->bitrates[i];
3748 }
3749 }
3750
3751 return 0;
3752}
3753
3754static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
3755{
3756 AVIndexEntry *sample = NULL;
3757 int64_t best_dts = INT64_MAX;
3758 int i;
3759 for (i = 0; i < s->nb_streams; i++) {
3760 AVStream *avst = s->streams[i];
3761 MOVStreamContext *msc = avst->priv_data;
3762 if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3763 AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3764 int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3765 av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3766 if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3767 (s->pb->seekable &&
3768 ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3769 ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3770 (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3771 sample = current_sample;
3772 best_dts = dts;
3773 *st = avst;
3774 }
3775 }
3776 }
3777 return sample;
3778}
3779
3780static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
3781{
3782 MOVContext *mov = s->priv_data;
3783 MOVStreamContext *sc;
3784 AVIndexEntry *sample;
3785 AVStream *st = NULL;
3786 int ret;
3787 mov->fc = s;
3788 retry:
3789 sample = mov_find_next_sample(s, &st);
3790 if (!sample) {
3791 mov->found_mdat = 0;
3792 if (!mov->next_root_atom)
3793 return AVERROR_EOF;
3794 avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3795 mov->next_root_atom = 0;
3796 if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3797 url_feof(s->pb))
3798 return AVERROR_EOF;
3799 av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3800 goto retry;
3801 }
3802 sc = st->priv_data;
3803 /* must be done just before reading, to avoid infinite loop on sample */
3804 sc->current_sample++;
3805
3806 if (mov->next_root_atom) {
3807 sample->pos = FFMIN(sample->pos, mov->next_root_atom);
3808 sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
3809 }
3810
3811 if (st->discard != AVDISCARD_ALL) {
3812 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3813 av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3814 sc->ffindex, sample->pos);
3815 return AVERROR_INVALIDDATA;
3816 }
3817 ret = av_get_packet(sc->pb, pkt, sample->size);
3818 if (ret < 0)
3819 return ret;
3820 if (sc->has_palette) {
3821 uint8_t *pal;
3822
3823 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
3824 if (!pal) {
3825 av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3826 } else {
3827 memcpy(pal, sc->palette, AVPALETTE_SIZE);
3828 sc->has_palette = 0;
3829 }
3830 }
3831#if CONFIG_DV_DEMUXER
3832 if (mov->dv_demux && sc->dv_audio_container) {
3833 avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
3834 av_free(pkt->data);
3835 pkt->size = 0;
3836 ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3837 if (ret < 0)
3838 return ret;
3839 }
3840#endif
3841 }
3842
3843 pkt->stream_index = sc->ffindex;
3844 pkt->dts = sample->timestamp;
3845 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3846 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3847 /* update ctts context */
3848 sc->ctts_sample++;
3849 if (sc->ctts_index < sc->ctts_count &&
3850 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3851 sc->ctts_index++;
3852 sc->ctts_sample = 0;
3853 }
3854 if (sc->wrong_dts)
3855 pkt->dts = AV_NOPTS_VALUE;
3856 } else {
3857 int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3858 st->index_entries[sc->current_sample].timestamp : st->duration;
3859 pkt->duration = next_dts - pkt->dts;
3860 pkt->pts = pkt->dts;
3861 }
3862 if (st->discard == AVDISCARD_ALL)
3863 goto retry;
3864 pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3865 pkt->pos = sample->pos;
3866 av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
3867 pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3868 return 0;
3869}
3870
3871
3872static int64_t mov_read_seek_sync(AVFormatContext *s,
3873 int stream_index,
3874 int64_t min_ts,
3875 int64_t target_ts,
3876 int64_t max_ts,
3877 int flags)
3878{
3879 int64_t pos, t_pos;
3880
3881 int64_t ts_ret, ts_adj;
3882 int stream_index_gen_search = stream_index;
3883 int sample, i;
3884 AVStream *st;
3885 AVParserState *backup;
3886
3887 backup = ff_store_parser_state(s);
3888
3889 // detect direction of seeking for search purposes
3890 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
3891 AVSEEK_FLAG_BACKWARD : 0;
3892
3893 st = s->streams[stream_index_gen_search];
3894 sample = av_index_search_timestamp(st, target_ts, AVSEEK_FLAG_ANY);
3895 pos = st->index_entries[sample].pos;
3896 target_ts = st->index_entries[sample].timestamp;
3897 for (i = 0; i < s->nb_streams; i++) {
3898 MOVStreamContext *sc = s->streams[i]->priv_data;
3899 sc->current_sample = (sample - 500) > 0 ? (sample - 500) : 0; // hard code for mov, repos the sample.
3900 }
3901
3902 // search for actual matching keyframe/starting position for all streams
3903 if ((t_pos = ff_gen_syncpoint_search(s, stream_index, pos,
3904 min_ts, target_ts, max_ts,
3905 flags)) < 0) {
3906 ff_restore_parser_state(s, backup);
3907 return -1;
3908 }
3909
3910 ff_free_parser_state(s, backup);
3911 return t_pos;
3912}
3913
3914static int64_t mov_read_seek2(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
3915 int ret;
3916 if (flags & AVSEEK_FLAG_BACKWARD) {
3917 flags &= ~AVSEEK_FLAG_BACKWARD;
3918 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
3919 if (ret < 0) {
3920 // for compatibility reasons, seek to the best-fitting timestamp
3921 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
3922 }
3923 } else {
3924 ret = mov_read_seek_sync(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
3925 if (ret < 0)
3926 // for compatibility reasons, seek to the best-fitting timestamp
3927 ret = mov_read_seek_sync(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
3928 }
3929 return ret;
3930}
3931
3932static int mov_index_search_pos(const AVIndexEntry *entries, int nb_entries,
3933 int64_t pos, int flags)
3934{
3935 int a, b, m;
3936 int64_t ppos;
3937
3938 a = - 1;
3939 b = nb_entries;
3940
3941 //optimize appending index entries at the end
3942 if(b && entries[b-1].pos < pos)
3943 a= b-1;
3944
3945 while (b - a > 1) {
3946 m = (a + b) >> 1;
3947 ppos = entries[m].pos;
3948 if(ppos >= pos)
3949 b = m;
3950 if(ppos <= pos)
3951 a = m;
3952 }
3953
3954 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
3955 return m;
3956}
3957
3958static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3959{
3960 MOVStreamContext *sc = st->priv_data;
3961 int sample, time_sample;
3962 int i;
3963
3964 sample = av_index_search_timestamp(st, timestamp, flags);
3965
3966 // mov's stss is wrong sometimes, need to read seek
3967 // added by senbai.tao
3968 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sample <=0 && st->nb_index_entries && sc->keyframe_count <= 1) {
3969 int64_t sync_point = mov_read_seek2(s, st->index, timestamp, flags);
3970 sample = mov_index_search_pos(st->index_entries, st->nb_index_entries, sync_point, AVSEEK_FLAG_ANY);
3971 }
3972
3973 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3974 if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3975 sample = 0;
3976 if (sample < 0) /* not sure what to do */
3977 return AVERROR_INVALIDDATA;
3978 sc->current_sample = sample;
3979 av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
3980 /* adjust ctts index */
3981 if (sc->ctts_data) {
3982 time_sample = 0;
3983 for (i = 0; i < sc->ctts_count; i++) {
3984 int next = time_sample + sc->ctts_data[i].count;
3985 if (next > sc->current_sample) {
3986 sc->ctts_index = i;
3987 sc->ctts_sample = sc->current_sample - time_sample;
3988 break;
3989 }
3990 time_sample = next;
3991 }
3992 }
3993 return sample;
3994}
3995
3996static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3997{
3998 AVStream *st;
3999 int64_t seek_timestamp, timestamp;
4000 int sample;
4001 int i;
4002
4003 if (stream_index >= s->nb_streams)
4004 return AVERROR_INVALIDDATA;
4005
4006 st = s->streams[stream_index];
4007 sample = mov_seek_stream(s, st, sample_time, flags);
4008 if (sample < 0)
4009 return sample;
4010
4011 /* adjust seek timestamp to found sample timestamp */
4012 seek_timestamp = st->index_entries[sample].timestamp;
4013
4014 for (i = 0; i < s->nb_streams; i++) {
4015 MOVStreamContext *sc = s->streams[i]->priv_data;
4016 st = s->streams[i];
4017 st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
4018
4019 if (stream_index == i)
4020 continue;
4021
4022 timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
4023 mov_seek_stream(s, st, timestamp, flags);
4024 }
4025 return 0;
4026}
4027
4028static const AVOption options[] = {
4029 {"use_absolute_path",
4030 "allow using absolute path when opening alias, this is a possible security issue",
4031 offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.i64 = 0},
4032 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4033 {"ignore_editlist", "", offsetof(MOVContext, ignore_editlist), FF_OPT_TYPE_INT, {.i64 = 0},
4034 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
4035 {NULL}
4036};
4037
4038static const AVClass mov_class = {
4039 .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
4040 .item_name = av_default_item_name,
4041 .option = options,
4042 .version = LIBAVUTIL_VERSION_INT,
4043};
4044
4045AVInputFormat ff_mov_demuxer = {
4046 .name = "mov,mp4,m4a,3gp,3g2,mj2",
4047 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
4048 .priv_data_size = sizeof(MOVContext),
4049 .read_probe = mov_probe,
4050 .read_header = mov_read_header,
4051 .read_packet = mov_read_packet,
4052 .read_close = mov_read_close,
4053 .read_seek = mov_read_seek,
4054 .priv_class = &mov_class,
4055 .flags = AVFMT_NO_BYTE_SEEK,
4056};
4057