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