FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/intfloat.h"
35 #include "libavutil/mathematics.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/display.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/aes.h"
42 #include "libavutil/sha.h"
43 #include "libavutil/timecode.h"
44 #include "libavcodec/ac3tab.h"
45 #include "avformat.h"
46 #include "internal.h"
47 #include "avio_internal.h"
48 #include "riff.h"
49 #include "isom.h"
50 #include "libavcodec/get_bits.h"
51 #include "id3v1.h"
52 #include "mov_chan.h"
53 #include "replaygain.h"
54 
55 #if CONFIG_ZLIB
56 #include <zlib.h>
57 #endif
58 
59 #include "qtpalette.h"
60 
61 /* those functions parse an atom */
62 /* links atom IDs to parse functions */
63 typedef struct MOVParseTableEntry {
64  uint32_t type;
65  int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
67 
68 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
69 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
70 
72  unsigned len, const char *key)
73 {
74  char buf[16];
75 
76  short current, total = 0;
77  avio_rb16(pb); // unknown
78  current = avio_rb16(pb);
79  if (len >= 6)
80  total = avio_rb16(pb);
81  if (!total)
82  snprintf(buf, sizeof(buf), "%d", current);
83  else
84  snprintf(buf, sizeof(buf), "%d/%d", current, total);
86  av_dict_set(&c->fc->metadata, key, buf, 0);
87 
88  return 0;
89 }
90 
92  unsigned len, const char *key)
93 {
94  /* bypass padding bytes */
95  avio_r8(pb);
96  avio_r8(pb);
97  avio_r8(pb);
98 
100  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
101 
102  return 0;
103 }
104 
106  unsigned len, const char *key)
107 {
109  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
110 
111  return 0;
112 }
113 
115  unsigned len, const char *key)
116 {
117  short genre;
118 
119  avio_r8(pb); // unknown
120 
121  genre = avio_r8(pb);
122  if (genre < 1 || genre > ID3v1_GENRE_MAX)
123  return 0;
125  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
126 
127  return 0;
128 }
129 
130 static const uint32_t mac_to_unicode[128] = {
131  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
132  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
133  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
134  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
135  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
136  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
137  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
138  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
139  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
140  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
141  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
142  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
143  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
144  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
145  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
146  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
147 };
148 
150  char *dst, int dstlen)
151 {
152  char *p = dst;
153  char *end = dst+dstlen-1;
154  int i;
155 
156  for (i = 0; i < len; i++) {
157  uint8_t t, c = avio_r8(pb);
158  if (c < 0x80 && p < end)
159  *p++ = c;
160  else if (p < end)
161  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
162  }
163  *p = 0;
164  return p - dst;
165 }
166 
167 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
168 {
169  AVPacket pkt;
170  AVStream *st;
171  MOVStreamContext *sc;
172  enum AVCodecID id;
173  int ret;
174 
175  switch (type) {
176  case 0xd: id = AV_CODEC_ID_MJPEG; break;
177  case 0xe: id = AV_CODEC_ID_PNG; break;
178  case 0x1b: id = AV_CODEC_ID_BMP; break;
179  default:
180  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
181  avio_skip(pb, len);
182  return 0;
183  }
184 
185  st = avformat_new_stream(c->fc, NULL);
186  if (!st)
187  return AVERROR(ENOMEM);
188  sc = av_mallocz(sizeof(*sc));
189  if (!sc)
190  return AVERROR(ENOMEM);
191  st->priv_data = sc;
192 
193  ret = av_get_packet(pb, &pkt, len);
194  if (ret < 0)
195  return ret;
196 
198 
199  st->attached_pic = pkt;
200  st->attached_pic.stream_index = st->index;
202 
204  st->codec->codec_id = id;
205 
206  return 0;
207 }
208 
209 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
210 {
211  char language[4] = { 0 };
212  char buf[200], place[100];
213  uint16_t langcode = 0;
214  double longitude, latitude, altitude;
215  const char *key = "location";
216 
217  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
218  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
219  return AVERROR_INVALIDDATA;
220  }
221 
222  avio_skip(pb, 4); // version+flags
223  langcode = avio_rb16(pb);
224  ff_mov_lang_to_iso639(langcode, language);
225  len -= 6;
226 
227  len -= avio_get_str(pb, len, place, sizeof(place));
228  if (len < 1) {
229  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
230  return AVERROR_INVALIDDATA;
231  }
232  avio_skip(pb, 1); // role
233  len -= 1;
234 
235  if (len < 12) {
236  av_log(c->fc, AV_LOG_ERROR, "no space for coordinates left (%d)\n", len);
237  return AVERROR_INVALIDDATA;
238  }
239  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
240  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
241  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
242 
243  // Try to output in the same format as the ?xyz field
244  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
245  if (altitude)
246  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
247  av_strlcatf(buf, sizeof(buf), "/%s", place);
248 
249  if (*language && strcmp(language, "und")) {
250  char key2[16];
251  snprintf(key2, sizeof(key2), "%s-%s", key, language);
252  av_dict_set(&c->fc->metadata, key2, buf, 0);
253  }
255  return av_dict_set(&c->fc->metadata, key, buf, 0);
256 }
257 
259 {
260  char tmp_key[5];
261  char key2[32], language[4] = {0};
262  char *str = NULL;
263  const char *key = NULL;
264  uint16_t langcode = 0;
265  uint32_t data_type = 0, str_size, str_size_alloc;
266  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
267  int raw = 0;
268 
269  switch (atom.type) {
270  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
271  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
272  case MKTAG( 'X','M','P','_'):
273  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
274  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
275  case MKTAG( 'a','k','I','D'): key = "account_type";
277  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
278  case MKTAG( 'c','a','t','g'): key = "category"; break;
279  case MKTAG( 'c','p','i','l'): key = "compilation";
281  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
282  case MKTAG( 'd','e','s','c'): key = "description"; break;
283  case MKTAG( 'd','i','s','k'): key = "disc";
285  case MKTAG( 'e','g','i','d'): key = "episode_uid";
287  case MKTAG( 'g','n','r','e'): key = "genre";
288  parse = mov_metadata_gnre; break;
289  case MKTAG( 'h','d','v','d'): key = "hd_video";
291  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
292  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
293  case MKTAG( 'l','o','c','i'):
294  return mov_metadata_loci(c, pb, atom.size);
295  case MKTAG( 'p','c','s','t'): key = "podcast";
297  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
299  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
300  case MKTAG( 'r','t','n','g'): key = "rating";
302  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
303  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
304  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
305  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
306  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
307  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
308  case MKTAG( 's','t','i','k'): key = "media_type";
310  case MKTAG( 't','r','k','n'): key = "track";
312  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
313  case MKTAG( 't','v','e','s'): key = "episode_sort";
315  case MKTAG( 't','v','n','n'): key = "network"; break;
316  case MKTAG( 't','v','s','h'): key = "show"; break;
317  case MKTAG( 't','v','s','n'): key = "season_number";
319  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
320  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
321  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
322  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
323  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
324  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
325  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
326  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
327  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
328  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
329  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
330  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
331  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
332  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
333  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
334  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
335  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
336  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
337  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
338  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
339  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
340  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
341  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
342  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
343  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
344  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
345  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
346  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
347  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
348  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
349  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
350  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
351  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
352  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
353  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
354  }
355 retry:
356  if (c->itunes_metadata && atom.size > 8) {
357  int data_size = avio_rb32(pb);
358  int tag = avio_rl32(pb);
359  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
360  data_type = avio_rb32(pb); // type
361  avio_rb32(pb); // unknown
362  str_size = data_size - 16;
363  atom.size -= 16;
364 
365  if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
366  int ret = mov_read_covr(c, pb, data_type, str_size);
367  if (ret < 0) {
368  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
369  }
370  return ret;
371  }
372  } else return 0;
373  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
374  str_size = avio_rb16(pb); // string length
375  if (str_size > atom.size) {
376  raw = 1;
377  avio_seek(pb, -2, SEEK_CUR);
378  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
379  goto retry;
380  }
381  langcode = avio_rb16(pb);
382  ff_mov_lang_to_iso639(langcode, language);
383  atom.size -= 4;
384  } else
385  str_size = atom.size;
386 
387  if (c->export_all && !key) {
388  snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
389  key = tmp_key;
390  }
391 
392  if (!key)
393  return 0;
394  if (atom.size < 0 || str_size >= INT_MAX/2)
395  return AVERROR_INVALIDDATA;
396 
397  // worst-case requirement for output string in case of utf8 coded input
398  str_size_alloc = (raw ? str_size : str_size * 2) + 1;
399  str = av_mallocz(str_size_alloc);
400  if (!str)
401  return AVERROR(ENOMEM);
402 
403  if (parse)
404  parse(c, pb, str_size, key);
405  else {
406  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
407  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
408  } else {
409  int ret = ffio_read_size(pb, str, str_size);
410  if (ret < 0) {
411  av_free(str);
412  return ret;
413  }
414  str[str_size] = 0;
415  }
417  av_dict_set(&c->fc->metadata, key, str, 0);
418  if (*language && strcmp(language, "und")) {
419  snprintf(key2, sizeof(key2), "%s-%s", key, language);
420  av_dict_set(&c->fc->metadata, key2, str, 0);
421  }
422  if (!strcmp(key, "encoder")) {
423  int major, minor, micro;
424  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
425  c->handbrake_version = 1000000*major + 1000*minor + micro;
426  }
427  }
428  }
429  av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
430  av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
431  key, str, (char*)&atom.type, str_size_alloc, atom.size);
432 
433  av_freep(&str);
434  return 0;
435 }
436 
438 {
439  int64_t start;
440  int i, nb_chapters, str_len, version;
441  char str[256+1];
442  int ret;
443 
444  if ((atom.size -= 5) < 0)
445  return 0;
446 
447  version = avio_r8(pb);
448  avio_rb24(pb);
449  if (version)
450  avio_rb32(pb); // ???
451  nb_chapters = avio_r8(pb);
452 
453  for (i = 0; i < nb_chapters; i++) {
454  if (atom.size < 9)
455  return 0;
456 
457  start = avio_rb64(pb);
458  str_len = avio_r8(pb);
459 
460  if ((atom.size -= 9+str_len) < 0)
461  return 0;
462 
463  ret = ffio_read_size(pb, str, str_len);
464  if (ret < 0)
465  return ret;
466  str[str_len] = 0;
467  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
468  }
469  return 0;
470 }
471 
472 #define MIN_DATA_ENTRY_BOX_SIZE 12
474 {
475  AVStream *st;
476  MOVStreamContext *sc;
477  int entries, i, j;
478 
479  if (c->fc->nb_streams < 1)
480  return 0;
481  st = c->fc->streams[c->fc->nb_streams-1];
482  sc = st->priv_data;
483 
484  avio_rb32(pb); // version + flags
485  entries = avio_rb32(pb);
486  if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
487  entries >= UINT_MAX / sizeof(*sc->drefs))
488  return AVERROR_INVALIDDATA;
489  av_free(sc->drefs);
490  sc->drefs_count = 0;
491  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
492  if (!sc->drefs)
493  return AVERROR(ENOMEM);
494  sc->drefs_count = entries;
495 
496  for (i = 0; i < sc->drefs_count; i++) {
497  MOVDref *dref = &sc->drefs[i];
498  uint32_t size = avio_rb32(pb);
499  int64_t next = avio_tell(pb) + size - 4;
500 
501  if (size < 12)
502  return AVERROR_INVALIDDATA;
503 
504  dref->type = avio_rl32(pb);
505  avio_rb32(pb); // version + flags
506  av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
507 
508  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
509  /* macintosh alias record */
510  uint16_t volume_len, len;
511  int16_t type;
512  int ret;
513 
514  avio_skip(pb, 10);
515 
516  volume_len = avio_r8(pb);
517  volume_len = FFMIN(volume_len, 27);
518  ret = ffio_read_size(pb, dref->volume, 27);
519  if (ret < 0)
520  return ret;
521  dref->volume[volume_len] = 0;
522  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
523 
524  avio_skip(pb, 12);
525 
526  len = avio_r8(pb);
527  len = FFMIN(len, 63);
528  ret = ffio_read_size(pb, dref->filename, 63);
529  if (ret < 0)
530  return ret;
531  dref->filename[len] = 0;
532  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
533 
534  avio_skip(pb, 16);
535 
536  /* read next level up_from_alias/down_to_target */
537  dref->nlvl_from = avio_rb16(pb);
538  dref->nlvl_to = avio_rb16(pb);
539  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
540  dref->nlvl_from, dref->nlvl_to);
541 
542  avio_skip(pb, 16);
543 
544  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
545  if(avio_feof(pb))
546  return AVERROR_EOF;
547  type = avio_rb16(pb);
548  len = avio_rb16(pb);
549  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
550  if (len&1)
551  len += 1;
552  if (type == 2 || type == 18) { // absolute path
553  av_free(dref->path);
554  dref->path = av_mallocz(len+1);
555  if (!dref->path)
556  return AVERROR(ENOMEM);
557 
558  ret = ffio_read_size(pb, dref->path, len);
559  if (ret < 0) {
560  av_freep(&dref->path);
561  return ret;
562  }
563  if (type == 18) // no additional processing needed
564  continue;
565  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
566  len -= volume_len;
567  memmove(dref->path, dref->path+volume_len, len);
568  dref->path[len] = 0;
569  }
570  for (j = 0; j < len; j++)
571  if (dref->path[j] == ':')
572  dref->path[j] = '/';
573  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
574  } else if (type == 0) { // directory name
575  av_free(dref->dir);
576  dref->dir = av_malloc(len+1);
577  if (!dref->dir)
578  return AVERROR(ENOMEM);
579 
580  ret = ffio_read_size(pb, dref->dir, len);
581  if (ret < 0) {
582  av_freep(&dref->dir);
583  return ret;
584  }
585  dref->dir[len] = 0;
586  for (j = 0; j < len; j++)
587  if (dref->dir[j] == ':')
588  dref->dir[j] = '/';
589  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
590  } else
591  avio_skip(pb, len);
592  }
593  }
594  avio_seek(pb, next, SEEK_SET);
595  }
596  return 0;
597 }
598 
600 {
601  AVStream *st;
602  uint32_t type;
603  uint32_t av_unused ctype;
604  int64_t title_size;
605  char *title_str;
606  int ret;
607 
608  if (c->fc->nb_streams < 1) // meta before first trak
609  return 0;
610 
611  st = c->fc->streams[c->fc->nb_streams-1];
612 
613  avio_r8(pb); /* version */
614  avio_rb24(pb); /* flags */
615 
616  /* component type */
617  ctype = avio_rl32(pb);
618  type = avio_rl32(pb); /* component subtype */
619 
620  av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
621  av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
622 
623  if (type == MKTAG('v','i','d','e'))
625  else if (type == MKTAG('s','o','u','n'))
627  else if (type == MKTAG('m','1','a',' '))
629  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
631 
632  avio_rb32(pb); /* component manufacture */
633  avio_rb32(pb); /* component flags */
634  avio_rb32(pb); /* component flags mask */
635 
636  title_size = atom.size - 24;
637  if (title_size > 0) {
638  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
639  return AVERROR_INVALIDDATA;
640  title_str = av_malloc(title_size + 1); /* Add null terminator */
641  if (!title_str)
642  return AVERROR(ENOMEM);
643 
644  ret = ffio_read_size(pb, title_str, title_size);
645  if (ret < 0) {
646  av_freep(&title_str);
647  return ret;
648  }
649  title_str[title_size] = 0;
650  if (title_str[0]) {
651  int off = (!c->isom && title_str[0] == title_size - 1);
652  av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
653  }
654  av_freep(&title_str);
655  }
656 
657  return 0;
658 }
659 
661 {
662  AVStream *st;
663  int tag;
664 
665  if (fc->nb_streams < 1)
666  return 0;
667  st = fc->streams[fc->nb_streams-1];
668 
669  avio_rb32(pb); /* version + flags */
670  ff_mp4_read_descr(fc, pb, &tag);
671  if (tag == MP4ESDescrTag) {
673  } else
674  avio_rb16(pb); /* ID */
675 
676  ff_mp4_read_descr(fc, pb, &tag);
677  if (tag == MP4DecConfigDescrTag)
678  ff_mp4_read_dec_config_descr(fc, st, pb);
679  return 0;
680 }
681 
683 {
684  return ff_mov_read_esds(c->fc, pb);
685 }
686 
688 {
689  AVStream *st;
690  enum AVAudioServiceType *ast;
691  int ac3info, acmod, lfeon, bsmod;
692 
693  if (c->fc->nb_streams < 1)
694  return 0;
695  st = c->fc->streams[c->fc->nb_streams-1];
696 
698  sizeof(*ast));
699  if (!ast)
700  return AVERROR(ENOMEM);
701 
702  ac3info = avio_rb24(pb);
703  bsmod = (ac3info >> 14) & 0x7;
704  acmod = (ac3info >> 11) & 0x7;
705  lfeon = (ac3info >> 10) & 0x1;
706  st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
708  if (lfeon)
710  *ast = bsmod;
711  if (st->codec->channels > 1 && bsmod == 0x7)
713 
714  st->codec->audio_service_type = *ast;
715 
716  return 0;
717 }
718 
720 {
721  AVStream *st;
722  enum AVAudioServiceType *ast;
723  int eac3info, acmod, lfeon, bsmod;
724 
725  if (c->fc->nb_streams < 1)
726  return 0;
727  st = c->fc->streams[c->fc->nb_streams-1];
728 
730  sizeof(*ast));
731  if (!ast)
732  return AVERROR(ENOMEM);
733 
734  /* No need to parse fields for additional independent substreams and its
735  * associated dependent substreams since libavcodec's E-AC-3 decoder
736  * does not support them yet. */
737  avio_rb16(pb); /* data_rate and num_ind_sub */
738  eac3info = avio_rb24(pb);
739  bsmod = (eac3info >> 12) & 0x1f;
740  acmod = (eac3info >> 9) & 0x7;
741  lfeon = (eac3info >> 8) & 0x1;
743  if (lfeon)
746  *ast = bsmod;
747  if (st->codec->channels > 1 && bsmod == 0x7)
749 
750  st->codec->audio_service_type = *ast;
751 
752  return 0;
753 }
754 
756 {
757  AVStream *st;
758 
759  if (c->fc->nb_streams < 1)
760  return 0;
761  st = c->fc->streams[c->fc->nb_streams-1];
762 
763  if (atom.size < 16)
764  return 0;
765 
766  /* skip version and flags */
767  avio_skip(pb, 4);
768 
769  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
770 
771  return 0;
772 }
773 
775 {
776  AVStream *st;
777  int ret;
778 
779  if (c->fc->nb_streams < 1)
780  return 0;
781  st = c->fc->streams[c->fc->nb_streams-1];
782 
783  if ((ret = ff_get_wav_header(c->fc, pb, st->codec, atom.size, 0)) < 0)
784  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
785 
786  return ret;
787 }
788 
790 {
791  const int num = avio_rb32(pb);
792  const int den = avio_rb32(pb);
793  AVStream *st;
794 
795  if (c->fc->nb_streams < 1)
796  return 0;
797  st = c->fc->streams[c->fc->nb_streams-1];
798 
799  if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
800  (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
802  "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
804  num, den);
805  } else if (den != 0) {
807  num, den, 32767);
808  }
809  return 0;
810 }
811 
812 /* this atom contains actual media data */
814 {
815  if (atom.size == 0) /* wrong one (MP4) */
816  return 0;
817  c->found_mdat=1;
818  return 0; /* now go for moov */
819 }
820 
821 #define DRM_BLOB_SIZE 56
822 
824 {
825  uint8_t intermediate_key[20];
826  uint8_t intermediate_iv[20];
827  uint8_t input[64];
828  uint8_t output[64];
829  uint8_t file_checksum[20];
830  uint8_t calculated_checksum[20];
831  struct AVSHA *sha;
832  int i;
833  int ret = 0;
834  uint8_t *activation_bytes = c->activation_bytes;
835  uint8_t *fixed_key = c->audible_fixed_key;
836 
837  c->aax_mode = 1;
838 
839  sha = av_sha_alloc();
840  if (!sha)
841  return AVERROR(ENOMEM);
842  av_free(c->aes_decrypt);
843  c->aes_decrypt = av_aes_alloc();
844  if (!c->aes_decrypt) {
845  ret = AVERROR(ENOMEM);
846  goto fail;
847  }
848 
849  /* drm blob processing */
850  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
851  avio_read(pb, input, DRM_BLOB_SIZE);
852  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
853  avio_read(pb, file_checksum, 20);
854 
855  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
856  for (i = 0; i < 20; i++)
857  av_log(sha, AV_LOG_INFO, "%02x", file_checksum[i]);
858  av_log(c->fc, AV_LOG_INFO, "\n");
859 
860  /* verify activation data */
861  if (!activation_bytes) {
862  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
863  ret = 0; /* allow ffprobe to continue working on .aax files */
864  goto fail;
865  }
866  if (c->activation_bytes_size != 4) {
867  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
868  ret = AVERROR(EINVAL);
869  goto fail;
870  }
871 
872  /* verify fixed key */
873  if (c->audible_fixed_key_size != 16) {
874  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
875  ret = AVERROR(EINVAL);
876  goto fail;
877  }
878 
879  /* AAX (and AAX+) key derivation */
880  av_sha_init(sha, 160);
881  av_sha_update(sha, fixed_key, 16);
882  av_sha_update(sha, activation_bytes, 4);
883  av_sha_final(sha, intermediate_key);
884  av_sha_init(sha, 160);
885  av_sha_update(sha, fixed_key, 16);
886  av_sha_update(sha, intermediate_key, 20);
887  av_sha_update(sha, activation_bytes, 4);
888  av_sha_final(sha, intermediate_iv);
889  av_sha_init(sha, 160);
890  av_sha_update(sha, intermediate_key, 16);
891  av_sha_update(sha, intermediate_iv, 16);
892  av_sha_final(sha, calculated_checksum);
893  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
894  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
895  ret = AVERROR_INVALIDDATA;
896  goto fail;
897  }
898  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
899  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
900  for (i = 0; i < 4; i++) {
901  // file data (in output) is stored in big-endian mode
902  if (activation_bytes[i] != output[3 - i]) { // critical error
903  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
904  ret = AVERROR_INVALIDDATA;
905  goto fail;
906  }
907  }
908  memcpy(c->file_key, output + 8, 16);
909  memcpy(input, output + 26, 16);
910  av_sha_init(sha, 160);
911  av_sha_update(sha, input, 16);
912  av_sha_update(sha, c->file_key, 16);
913  av_sha_update(sha, fixed_key, 16);
914  av_sha_final(sha, c->file_iv);
915 
916 fail:
917  av_free(sha);
918 
919  return ret;
920 }
921 
922 // Audible AAX (and AAX+) bytestream decryption
923 static int aax_filter(uint8_t *input, int size, MOVContext *c)
924 {
925  int blocks = 0;
926  unsigned char iv[16];
927 
928  memcpy(iv, c->file_iv, 16); // iv is overwritten
929  blocks = size >> 4; // trailing bytes are not encrypted!
930  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
931  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
932 
933  return 0;
934 }
935 
936 /* read major brand, minor version and compatible brands and store them as metadata */
938 {
939  uint32_t minor_ver;
940  int comp_brand_size;
941  char* comp_brands_str;
942  uint8_t type[5] = {0};
943  int ret = ffio_read_size(pb, type, 4);
944  if (ret < 0)
945  return ret;
946 
947  if (strcmp(type, "qt "))
948  c->isom = 1;
949  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
950  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
951  minor_ver = avio_rb32(pb); /* minor version */
952  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
953 
954  comp_brand_size = atom.size - 8;
955  if (comp_brand_size < 0)
956  return AVERROR_INVALIDDATA;
957  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
958  if (!comp_brands_str)
959  return AVERROR(ENOMEM);
960 
961  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
962  if (ret < 0) {
963  av_freep(&comp_brands_str);
964  return ret;
965  }
966  comp_brands_str[comp_brand_size] = 0;
967  av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
968  av_freep(&comp_brands_str);
969 
970  return 0;
971 }
972 
973 /* this atom should contain all header atoms */
975 {
976  int ret;
977 
978  if (c->found_moov) {
979  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
980  avio_skip(pb, atom.size);
981  return 0;
982  }
983 
984  if ((ret = mov_read_default(c, pb, atom)) < 0)
985  return ret;
986  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
987  /* so we don't parse the whole file if over a network */
988  c->found_moov=1;
989  return 0; /* now go for mdat */
990 }
991 
993 {
994  // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
995  c->fragment.found_tfhd = 0;
996 
997  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
998  c->has_looked_for_mfra = 1;
999  if (pb->seekable) {
1000  int ret;
1001  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1002  "for a mfra\n");
1003  if ((ret = mov_read_mfra(c, pb)) < 0) {
1004  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1005  "read the mfra (may be a live ismv)\n");
1006  }
1007  } else {
1008  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1009  "seekable, can not look for mfra\n");
1010  }
1011  }
1013  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1014  return mov_read_default(c, pb, atom);
1015 }
1016 
1017 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
1018 {
1019  char buffer[32];
1020  if (time) {
1021  struct tm *ptm, tmbuf;
1022  time_t timet;
1023  if(time >= 2082844800)
1024  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1025  timet = time;
1026  ptm = gmtime_r(&timet, &tmbuf);
1027  if (!ptm) return;
1028  if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
1029  av_dict_set(metadata, "creation_time", buffer, 0);
1030  }
1031 }
1032 
1034 {
1035  AVStream *st;
1036  MOVStreamContext *sc;
1037  int version;
1038  char language[4] = {0};
1039  unsigned lang;
1040  int64_t creation_time;
1041 
1042  if (c->fc->nb_streams < 1)
1043  return 0;
1044  st = c->fc->streams[c->fc->nb_streams-1];
1045  sc = st->priv_data;
1046 
1047  if (sc->time_scale) {
1048  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1049  return AVERROR_INVALIDDATA;
1050  }
1051 
1052  version = avio_r8(pb);
1053  if (version > 1) {
1054  avpriv_request_sample(c->fc, "Version %d", version);
1055  return AVERROR_PATCHWELCOME;
1056  }
1057  avio_rb24(pb); /* flags */
1058  if (version == 1) {
1059  creation_time = avio_rb64(pb);
1060  avio_rb64(pb);
1061  } else {
1062  creation_time = avio_rb32(pb);
1063  avio_rb32(pb); /* modification time */
1064  }
1065  mov_metadata_creation_time(&st->metadata, creation_time);
1066 
1067  sc->time_scale = avio_rb32(pb);
1068  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1069 
1070  lang = avio_rb16(pb); /* language */
1071  if (ff_mov_lang_to_iso639(lang, language))
1072  av_dict_set(&st->metadata, "language", language, 0);
1073  avio_rb16(pb); /* quality */
1074 
1075  return 0;
1076 }
1077 
1079 {
1080  int64_t creation_time;
1081  int version = avio_r8(pb); /* version */
1082  avio_rb24(pb); /* flags */
1083 
1084  if (version == 1) {
1085  creation_time = avio_rb64(pb);
1086  avio_rb64(pb);
1087  } else {
1088  creation_time = avio_rb32(pb);
1089  avio_rb32(pb); /* modification time */
1090  }
1091  mov_metadata_creation_time(&c->fc->metadata, creation_time);
1092  c->time_scale = avio_rb32(pb); /* time scale */
1093 
1094  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1095 
1096  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1097  // set the AVCodecContext duration because the duration of individual tracks
1098  // may be inaccurate
1099  if (c->time_scale > 0 && !c->trex_data)
1101  avio_rb32(pb); /* preferred scale */
1102 
1103  avio_rb16(pb); /* preferred volume */
1104 
1105  avio_skip(pb, 10); /* reserved */
1106 
1107  avio_skip(pb, 36); /* display matrix */
1108 
1109  avio_rb32(pb); /* preview time */
1110  avio_rb32(pb); /* preview duration */
1111  avio_rb32(pb); /* poster time */
1112  avio_rb32(pb); /* selection time */
1113  avio_rb32(pb); /* selection duration */
1114  avio_rb32(pb); /* current time */
1115  avio_rb32(pb); /* next track ID */
1116 
1117  return 0;
1118 }
1119 
1121 {
1122  AVStream *st;
1123  int little_endian;
1124 
1125  if (c->fc->nb_streams < 1)
1126  return 0;
1127  st = c->fc->streams[c->fc->nb_streams-1];
1128 
1129  little_endian = avio_rb16(pb) & 0xFF;
1130  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1131  if (little_endian == 1) {
1132  switch (st->codec->codec_id) {
1133  case AV_CODEC_ID_PCM_S24BE:
1135  break;
1136  case AV_CODEC_ID_PCM_S32BE:
1138  break;
1139  case AV_CODEC_ID_PCM_F32BE:
1141  break;
1142  case AV_CODEC_ID_PCM_F64BE:
1144  break;
1145  default:
1146  break;
1147  }
1148  }
1149  return 0;
1150 }
1151 
1153 {
1154  AVStream *st;
1155  char color_parameter_type[5] = { 0 };
1156  uint16_t color_primaries, color_trc, color_matrix;
1157  int ret;
1158 
1159  if (c->fc->nb_streams < 1)
1160  return 0;
1161  st = c->fc->streams[c->fc->nb_streams - 1];
1162 
1163  ret = ffio_read_size(pb, color_parameter_type, 4);
1164  if (ret < 0)
1165  return ret;
1166  if (strncmp(color_parameter_type, "nclx", 4) &&
1167  strncmp(color_parameter_type, "nclc", 4)) {
1168  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1169  color_parameter_type);
1170  return 0;
1171  }
1172 
1173  color_primaries = avio_rb16(pb);
1174  color_trc = avio_rb16(pb);
1175  color_matrix = avio_rb16(pb);
1176 
1177  av_log(c->fc, AV_LOG_TRACE,
1178  "%s: pri %d trc %d matrix %d",
1179  color_parameter_type, color_primaries, color_trc, color_matrix);
1180 
1181  if (!strncmp(color_parameter_type, "nclx", 4)) {
1182  uint8_t color_range = avio_r8(pb) >> 7;
1183  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1184  if (color_range)
1186  else
1188  /* 14496-12 references JPEG XR specs (rather than the more complete
1189  * 23001-8) so some adjusting is required */
1190  if (color_primaries >= AVCOL_PRI_FILM)
1191  color_primaries = AVCOL_PRI_UNSPECIFIED;
1192  if ((color_trc >= AVCOL_TRC_LINEAR &&
1193  color_trc <= AVCOL_TRC_LOG_SQRT) ||
1194  color_trc >= AVCOL_TRC_BT2020_10)
1195  color_trc = AVCOL_TRC_UNSPECIFIED;
1196  if (color_matrix >= AVCOL_SPC_BT2020_NCL)
1197  color_matrix = AVCOL_SPC_UNSPECIFIED;
1199  st->codec->color_trc = color_trc;
1200  st->codec->colorspace = color_matrix;
1201  } else if (!strncmp(color_parameter_type, "nclc", 4)) {
1202  /* color primaries, Table 4-4 */
1203  switch (color_primaries) {
1204  case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break;
1205  case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break;
1206  case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break;
1207  }
1208  /* color transfer, Table 4-5 */
1209  switch (color_trc) {
1210  case 1: st->codec->color_trc = AVCOL_TRC_BT709; break;
1211  case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break;
1212  }
1213  /* color matrix, Table 4-6 */
1214  switch (color_matrix) {
1215  case 1: st->codec->colorspace = AVCOL_SPC_BT709; break;
1216  case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break;
1217  case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break;
1218  }
1219  }
1220  av_log(c->fc, AV_LOG_TRACE, "\n");
1221 
1222  return 0;
1223 }
1224 
1226 {
1227  AVStream *st;
1228  unsigned mov_field_order;
1229  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1230 
1231  if (c->fc->nb_streams < 1) // will happen with jp2 files
1232  return 0;
1233  st = c->fc->streams[c->fc->nb_streams-1];
1234  if (atom.size < 2)
1235  return AVERROR_INVALIDDATA;
1236  mov_field_order = avio_rb16(pb);
1237  if ((mov_field_order & 0xFF00) == 0x0100)
1238  decoded_field_order = AV_FIELD_PROGRESSIVE;
1239  else if ((mov_field_order & 0xFF00) == 0x0200) {
1240  switch (mov_field_order & 0xFF) {
1241  case 0x01: decoded_field_order = AV_FIELD_TT;
1242  break;
1243  case 0x06: decoded_field_order = AV_FIELD_BB;
1244  break;
1245  case 0x09: decoded_field_order = AV_FIELD_TB;
1246  break;
1247  case 0x0E: decoded_field_order = AV_FIELD_BT;
1248  break;
1249  }
1250  }
1251  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1252  av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1253  }
1254  st->codec->field_order = decoded_field_order;
1255 
1256  return 0;
1257 }
1258 
1260 {
1261  int err = 0;
1262  uint64_t size = (uint64_t)codec->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1263  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1264  return AVERROR_INVALIDDATA;
1265  if ((err = av_reallocp(&codec->extradata, size)) < 0) {
1266  codec->extradata_size = 0;
1267  return err;
1268  }
1270  return 0;
1271 }
1272 
1273 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1275  AVCodecContext *codec, uint8_t *buf)
1276 {
1277  int64_t result = atom.size;
1278  int err;
1279 
1280  AV_WB32(buf , atom.size + 8);
1281  AV_WL32(buf + 4, atom.type);
1282  err = ffio_read_size(pb, buf + 8, atom.size);
1283  if (err < 0) {
1284  codec->extradata_size -= atom.size;
1285  return err;
1286  } else if (err < atom.size) {
1287  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1288  codec->extradata_size -= atom.size - err;
1289  result = err;
1290  }
1291  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1292  return result;
1293 }
1294 
1295 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
1297  enum AVCodecID codec_id)
1298 {
1299  AVStream *st;
1300  uint64_t original_size;
1301  int err;
1302 
1303  if (c->fc->nb_streams < 1) // will happen with jp2 files
1304  return 0;
1305  st = c->fc->streams[c->fc->nb_streams-1];
1306 
1307  if (st->codec->codec_id != codec_id)
1308  return 0; /* unexpected codec_id - don't mess with extradata */
1309 
1310  original_size = st->codec->extradata_size;
1311  err = mov_realloc_extradata(st->codec, atom);
1312  if (err)
1313  return err;
1314 
1315  err = mov_read_atom_into_extradata(c, pb, atom, st->codec, st->codec->extradata + original_size);
1316  if (err < 0)
1317  return err;
1318  return 0; // Note: this is the original behavior to ignore truncation.
1319 }
1320 
1321 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1323 {
1324  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1325 }
1326 
1328 {
1329  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1330 }
1331 
1333 {
1334  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1335 }
1336 
1338 {
1339  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1340 }
1341 
1343 {
1344  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1345  if(ret == 0)
1346  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1347  return ret;
1348 }
1349 
1351 {
1352  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1353 
1354  if (!ret && c->fc->nb_streams >= 1) {
1355  AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1356  if (avctx->extradata_size >= 40) {
1357  avctx->height = AV_RB16(&avctx->extradata[36]);
1358  avctx->width = AV_RB16(&avctx->extradata[38]);
1359  }
1360  }
1361  return ret;
1362 }
1363 
1365 {
1366  if (c->fc->nb_streams >= 1) {
1367  AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1368  if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1369  codec->codec_id == AV_CODEC_ID_H264 &&
1370  atom.size > 11) {
1371  avio_skip(pb, 10);
1372  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1373  if (avio_rb16(pb) == 0xd4d)
1374  codec->width = 1440;
1375  return 0;
1376  }
1377  }
1378 
1379  return mov_read_avid(c, pb, atom);
1380 }
1381 
1383 {
1384  int ret = 0;
1385  int length = 0;
1386  uint64_t original_size;
1387  if (c->fc->nb_streams >= 1) {
1388  AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1389  if (codec->codec_id == AV_CODEC_ID_H264)
1390  return 0;
1391  if (atom.size == 16) {
1392  original_size = codec->extradata_size;
1393  ret = mov_realloc_extradata(codec, atom);
1394  if (!ret) {
1395  length = mov_read_atom_into_extradata(c, pb, atom, codec, codec->extradata + original_size);
1396  if (length == atom.size) {
1397  const uint8_t range_value = codec->extradata[original_size + 19];
1398  switch (range_value) {
1399  case 1:
1400  codec->color_range = AVCOL_RANGE_MPEG;
1401  break;
1402  case 2:
1403  codec->color_range = AVCOL_RANGE_JPEG;
1404  break;
1405  default:
1406  av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1407  break;
1408  }
1409  ff_dlog(c, "color_range: %d\n", codec->color_range);
1410  } else {
1411  /* For some reason the whole atom was not added to the extradata */
1412  av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1413  }
1414  } else {
1415  av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1416  }
1417  } else {
1418  av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1419  }
1420  }
1421 
1422  return ret;
1423 }
1424 
1426 {
1427  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1428 }
1429 
1431 {
1432  AVStream *st;
1433  int ret;
1434 
1435  if (c->fc->nb_streams < 1)
1436  return 0;
1437  st = c->fc->streams[c->fc->nb_streams-1];
1438 
1439  if ((uint64_t)atom.size > (1<<30))
1440  return AVERROR_INVALIDDATA;
1441 
1442  if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1443  st->codec->codec_id == AV_CODEC_ID_QDMC ||
1444  st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1445  // pass all frma atom to codec, needed at least for QDMC and QDM2
1446  av_freep(&st->codec->extradata);
1447  ret = ff_get_extradata(st->codec, pb, atom.size);
1448  if (ret < 0)
1449  return ret;
1450  } else if (atom.size > 8) { /* to read frma, esds atoms */
1451  if (st->codec->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1452  uint64_t buffer;
1453  ret = ffio_ensure_seekback(pb, 8);
1454  if (ret < 0)
1455  return ret;
1456  buffer = avio_rb64(pb);
1457  atom.size -= 8;
1458  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1459  && buffer >> 32 <= atom.size
1460  && buffer >> 32 >= 8) {
1461  avio_skip(pb, -8);
1462  atom.size += 8;
1463  } else if (!st->codec->extradata_size) {
1464 #define ALAC_EXTRADATA_SIZE 36
1466  if (!st->codec->extradata)
1467  return AVERROR(ENOMEM);
1470  AV_WB32(st->codec->extradata + 4, MKTAG('a','l','a','c'));
1471  AV_WB64(st->codec->extradata + 12, buffer);
1472  avio_read(pb, st->codec->extradata + 20, 16);
1473  avio_skip(pb, atom.size - 24);
1474  return 0;
1475  }
1476  }
1477  if ((ret = mov_read_default(c, pb, atom)) < 0)
1478  return ret;
1479  } else
1480  avio_skip(pb, atom.size);
1481  return 0;
1482 }
1483 
1484 /**
1485  * This function reads atom content and puts data in extradata without tag
1486  * nor size unlike mov_read_extradata.
1487  */
1489 {
1490  AVStream *st;
1491  int ret;
1492 
1493  if (c->fc->nb_streams < 1)
1494  return 0;
1495  st = c->fc->streams[c->fc->nb_streams-1];
1496 
1497  if ((uint64_t)atom.size > (1<<30))
1498  return AVERROR_INVALIDDATA;
1499 
1500  if (atom.size >= 10) {
1501  // Broken files created by legacy versions of libavformat will
1502  // wrap a whole fiel atom inside of a glbl atom.
1503  unsigned size = avio_rb32(pb);
1504  unsigned type = avio_rl32(pb);
1505  avio_seek(pb, -8, SEEK_CUR);
1506  if (type == MKTAG('f','i','e','l') && size == atom.size)
1507  return mov_read_default(c, pb, atom);
1508  }
1509  if (st->codec->extradata_size > 1 && st->codec->extradata) {
1510  av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
1511  return 0;
1512  }
1513  av_freep(&st->codec->extradata);
1514  ret = ff_get_extradata(st->codec, pb, atom.size);
1515  if (ret < 0)
1516  return ret;
1517 
1518  return 0;
1519 }
1520 
1522 {
1523  AVStream *st;
1524  uint8_t profile_level;
1525  int ret;
1526 
1527  if (c->fc->nb_streams < 1)
1528  return 0;
1529  st = c->fc->streams[c->fc->nb_streams-1];
1530 
1531  if (atom.size >= (1<<28) || atom.size < 7)
1532  return AVERROR_INVALIDDATA;
1533 
1534  profile_level = avio_r8(pb);
1535  if ((profile_level & 0xf0) != 0xc0)
1536  return 0;
1537 
1538  avio_seek(pb, 6, SEEK_CUR);
1539  av_freep(&st->codec->extradata);
1540  ret = ff_get_extradata(st->codec, pb, atom.size - 7);
1541  if (ret < 0)
1542  return ret;
1543 
1544  return 0;
1545 }
1546 
1547 /**
1548  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1549  * but can have extradata appended at the end after the 40 bytes belonging
1550  * to the struct.
1551  */
1553 {
1554  AVStream *st;
1555  int ret;
1556 
1557  if (c->fc->nb_streams < 1)
1558  return 0;
1559  if (atom.size <= 40)
1560  return 0;
1561  st = c->fc->streams[c->fc->nb_streams-1];
1562 
1563  if ((uint64_t)atom.size > (1<<30))
1564  return AVERROR_INVALIDDATA;
1565 
1566  avio_skip(pb, 40);
1567  av_freep(&st->codec->extradata);
1568  ret = ff_get_extradata(st->codec, pb, atom.size - 40);
1569  if (ret < 0)
1570  return ret;
1571 
1572  return 0;
1573 }
1574 
1576 {
1577  AVStream *st;
1578  MOVStreamContext *sc;
1579  unsigned int i, entries;
1580 
1581  if (c->fc->nb_streams < 1)
1582  return 0;
1583  st = c->fc->streams[c->fc->nb_streams-1];
1584  sc = st->priv_data;
1585 
1586  avio_r8(pb); /* version */
1587  avio_rb24(pb); /* flags */
1588 
1589  entries = avio_rb32(pb);
1590 
1591  if (!entries)
1592  return 0;
1593 
1594  if (sc->chunk_offsets)
1595  av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
1596  av_free(sc->chunk_offsets);
1597  sc->chunk_count = 0;
1598  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
1599  if (!sc->chunk_offsets)
1600  return AVERROR(ENOMEM);
1601  sc->chunk_count = entries;
1602 
1603  if (atom.type == MKTAG('s','t','c','o'))
1604  for (i = 0; i < entries && !pb->eof_reached; i++)
1605  sc->chunk_offsets[i] = avio_rb32(pb);
1606  else if (atom.type == MKTAG('c','o','6','4'))
1607  for (i = 0; i < entries && !pb->eof_reached; i++)
1608  sc->chunk_offsets[i] = avio_rb64(pb);
1609  else
1610  return AVERROR_INVALIDDATA;
1611 
1612  sc->chunk_count = i;
1613 
1614  if (pb->eof_reached)
1615  return AVERROR_EOF;
1616 
1617  return 0;
1618 }
1619 
1620 /**
1621  * Compute codec id for 'lpcm' tag.
1622  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1623  */
1625 {
1626  /* lpcm flags:
1627  * 0x1 = float
1628  * 0x2 = big-endian
1629  * 0x4 = signed
1630  */
1631  return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1632 }
1633 
1634 static int mov_codec_id(AVStream *st, uint32_t format)
1635 {
1636  int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1637 
1638  if (id <= 0 &&
1639  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1640  (format & 0xFFFF) == 'T' + ('S' << 8)))
1641  id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1642 
1643  if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1645  } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1646  /* skip old asf mpeg4 tag */
1647  format && format != MKTAG('m','p','4','s')) {
1649  if (id <= 0)
1650  id = ff_codec_get_id(ff_codec_bmp_tags, format);
1651  if (id > 0)
1653  else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1655  st->codec->codec_id == AV_CODEC_ID_NONE)) {
1657  if (id > 0)
1659  }
1660  }
1661 
1662  st->codec->codec_tag = format;
1663 
1664  return id;
1665 }
1666 
1668  AVStream *st, MOVStreamContext *sc)
1669 {
1670  uint8_t codec_name[32];
1671  unsigned int color_depth, len, j;
1672  int color_greyscale;
1673  int color_table_id;
1674 
1675  avio_rb16(pb); /* version */
1676  avio_rb16(pb); /* revision level */
1677  avio_rb32(pb); /* vendor */
1678  avio_rb32(pb); /* temporal quality */
1679  avio_rb32(pb); /* spatial quality */
1680 
1681  st->codec->width = avio_rb16(pb); /* width */
1682  st->codec->height = avio_rb16(pb); /* height */
1683 
1684  avio_rb32(pb); /* horiz resolution */
1685  avio_rb32(pb); /* vert resolution */
1686  avio_rb32(pb); /* data size, always 0 */
1687  avio_rb16(pb); /* frames per samples */
1688 
1689  len = avio_r8(pb); /* codec name, pascal string */
1690  if (len > 31)
1691  len = 31;
1692  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1693  if (len < 31)
1694  avio_skip(pb, 31 - len);
1695 
1696  if (codec_name[0])
1697  av_dict_set(&st->metadata, "encoder", codec_name, 0);
1698 
1699  /* codec_tag YV12 triggers an UV swap in rawdec.c */
1700  if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1701  st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1702  st->codec->width &= ~1;
1703  st->codec->height &= ~1;
1704  }
1705  /* Flash Media Server uses tag H263 with Sorenson Spark */
1706  if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1707  !memcmp(codec_name, "Sorenson H263", 13))
1709 
1710  st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1711  color_table_id = avio_rb16(pb); /* colortable id */
1712  av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
1713  st->codec->bits_per_coded_sample, color_table_id);
1714  /* figure out the palette situation */
1715  color_depth = st->codec->bits_per_coded_sample & 0x1F;
1716  color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1717  /* Do not create a greyscale palette for cinepak */
1718  if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
1719  return;
1720 
1721  /* if the depth is 2, 4, or 8 bpp, file is palettized */
1722  if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1723  /* for palette traversal */
1724  unsigned int color_start, color_count, color_end;
1725  unsigned int a, r, g, b;
1726 
1727  if (color_greyscale) {
1728  int color_index, color_dec;
1729  /* compute the greyscale palette */
1730  st->codec->bits_per_coded_sample = color_depth;
1731  color_count = 1 << color_depth;
1732  color_index = 255;
1733  color_dec = 256 / (color_count - 1);
1734  for (j = 0; j < color_count; j++) {
1735  r = g = b = color_index;
1736  sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1737  color_index -= color_dec;
1738  if (color_index < 0)
1739  color_index = 0;
1740  }
1741  } else if (color_table_id) {
1742  const uint8_t *color_table;
1743  /* if flag bit 3 is set, use the default palette */
1744  color_count = 1 << color_depth;
1745  if (color_depth == 2)
1746  color_table = ff_qt_default_palette_4;
1747  else if (color_depth == 4)
1748  color_table = ff_qt_default_palette_16;
1749  else
1750  color_table = ff_qt_default_palette_256;
1751 
1752  for (j = 0; j < color_count; j++) {
1753  r = color_table[j * 3 + 0];
1754  g = color_table[j * 3 + 1];
1755  b = color_table[j * 3 + 2];
1756  sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1757  }
1758  } else {
1759  /* load the palette from the file */
1760  color_start = avio_rb32(pb);
1761  color_count = avio_rb16(pb);
1762  color_end = avio_rb16(pb);
1763  if ((color_start <= 255) && (color_end <= 255)) {
1764  for (j = color_start; j <= color_end; j++) {
1765  /* each A, R, G, or B component is 16 bits;
1766  * only use the top 8 bits */
1767  a = avio_r8(pb);
1768  avio_r8(pb);
1769  r = avio_r8(pb);
1770  avio_r8(pb);
1771  g = avio_r8(pb);
1772  avio_r8(pb);
1773  b = avio_r8(pb);
1774  avio_r8(pb);
1775  sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1776  }
1777  }
1778  }
1779  sc->has_palette = 1;
1780  }
1781 }
1782 
1784  AVStream *st, MOVStreamContext *sc)
1785 {
1786  int bits_per_sample, flags;
1787  uint16_t version = avio_rb16(pb);
1788  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1789 
1790  avio_rb16(pb); /* revision level */
1791  avio_rb32(pb); /* vendor */
1792 
1793  st->codec->channels = avio_rb16(pb); /* channel count */
1794  st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1795  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels);
1796 
1797  sc->audio_cid = avio_rb16(pb);
1798  avio_rb16(pb); /* packet size = 0 */
1799 
1800  st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1801 
1802  // Read QT version 1 fields. In version 0 these do not exist.
1803  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
1804  if (!c->isom ||
1805  (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1806 
1807  if (version == 1) {
1808  sc->samples_per_frame = avio_rb32(pb);
1809  avio_rb32(pb); /* bytes per packet */
1810  sc->bytes_per_frame = avio_rb32(pb);
1811  avio_rb32(pb); /* bytes per sample */
1812  } else if (version == 2) {
1813  avio_rb32(pb); /* sizeof struct only */
1815  st->codec->channels = avio_rb32(pb);
1816  avio_rb32(pb); /* always 0x7F000000 */
1818 
1819  flags = avio_rb32(pb); /* lpcm format specific flag */
1820  sc->bytes_per_frame = avio_rb32(pb);
1821  sc->samples_per_frame = avio_rb32(pb);
1822  if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1823  st->codec->codec_id =
1825  flags);
1826  }
1827  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1828  /* can't correctly handle variable sized packet as audio unit */
1829  switch (st->codec->codec_id) {
1830  case AV_CODEC_ID_MP2:
1831  case AV_CODEC_ID_MP3:
1833  break;
1834  }
1835  }
1836  }
1837 
1838  switch (st->codec->codec_id) {
1839  case AV_CODEC_ID_PCM_S8:
1840  case AV_CODEC_ID_PCM_U8:
1841  if (st->codec->bits_per_coded_sample == 16)
1843  break;
1844  case AV_CODEC_ID_PCM_S16LE:
1845  case AV_CODEC_ID_PCM_S16BE:
1846  if (st->codec->bits_per_coded_sample == 8)
1848  else if (st->codec->bits_per_coded_sample == 24)
1849  st->codec->codec_id =
1852  else if (st->codec->bits_per_coded_sample == 32)
1853  st->codec->codec_id =
1856  break;
1857  /* set values for old format before stsd version 1 appeared */
1858  case AV_CODEC_ID_MACE3:
1859  sc->samples_per_frame = 6;
1860  sc->bytes_per_frame = 2 * st->codec->channels;
1861  break;
1862  case AV_CODEC_ID_MACE6:
1863  sc->samples_per_frame = 6;
1864  sc->bytes_per_frame = 1 * st->codec->channels;
1865  break;
1867  sc->samples_per_frame = 64;
1868  sc->bytes_per_frame = 34 * st->codec->channels;
1869  break;
1870  case AV_CODEC_ID_GSM:
1871  sc->samples_per_frame = 160;
1872  sc->bytes_per_frame = 33;
1873  break;
1874  default:
1875  break;
1876  }
1877 
1878  bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1879  if (bits_per_sample) {
1880  st->codec->bits_per_coded_sample = bits_per_sample;
1881  sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1882  }
1883 }
1884 
1886  AVStream *st, MOVStreamContext *sc,
1887  int64_t size)
1888 {
1889  // ttxt stsd contains display flags, justification, background
1890  // color, fonts, and default styles, so fake an atom to read it
1891  MOVAtom fake_atom = { .size = size };
1892  // mp4s contains a regular esds atom
1893  if (st->codec->codec_tag != AV_RL32("mp4s"))
1894  mov_read_glbl(c, pb, fake_atom);
1895  st->codec->width = sc->width;
1896  st->codec->height = sc->height;
1897 }
1898 
1899 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1900 {
1901  uint8_t r, g, b;
1902  int y, cb, cr;
1903 
1904  y = (ycbcr >> 16) & 0xFF;
1905  cr = (ycbcr >> 8) & 0xFF;
1906  cb = ycbcr & 0xFF;
1907 
1908  b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
1909  g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
1910  r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
1911 
1912  return (r << 16) | (g << 8) | b;
1913 }
1914 
1916 {
1917  char buf[256] = {0};
1918  uint8_t *src = st->codec->extradata;
1919  int i;
1920 
1921  if (st->codec->extradata_size != 64)
1922  return 0;
1923 
1924  if (st->codec->width > 0 && st->codec->height > 0)
1925  snprintf(buf, sizeof(buf), "size: %dx%d\n",
1926  st->codec->width, st->codec->height);
1927  av_strlcat(buf, "palette: ", sizeof(buf));
1928 
1929  for (i = 0; i < 16; i++) {
1930  uint32_t yuv = AV_RB32(src + i * 4);
1931  uint32_t rgba = yuv_to_rgba(yuv);
1932 
1933  av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1934  }
1935 
1936  if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1937  return 0;
1938 
1939  av_freep(&st->codec->extradata);
1940  st->codec->extradata_size = 0;
1942  if (!st->codec->extradata)
1943  return AVERROR(ENOMEM);
1944  st->codec->extradata_size = strlen(buf);
1945  memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1946 
1947  return 0;
1948 }
1949 
1951  AVStream *st, MOVStreamContext *sc,
1952  int64_t size)
1953 {
1954  int ret;
1955 
1956  if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1957  if ((int)size != size)
1958  return AVERROR(ENOMEM);
1959 
1960  ret = ff_get_extradata(st->codec, pb, size);
1961  if (ret < 0)
1962  return ret;
1963  if (size > 16) {
1964  MOVStreamContext *tmcd_ctx = st->priv_data;
1965  int val;
1966  val = AV_RB32(st->codec->extradata + 4);
1967  tmcd_ctx->tmcd_flags = val;
1968  if (val & 1)
1970  st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1971  st->codec->time_base.num = 1;
1972  /* adjust for per frame dur in counter mode */
1973  if (tmcd_ctx->tmcd_flags & 0x0008) {
1974  int timescale = AV_RB32(st->codec->extradata + 8);
1975  int framedur = AV_RB32(st->codec->extradata + 12);
1976  st->codec->time_base.den *= timescale;
1977  st->codec->time_base.num *= framedur;
1978  }
1979  if (size > 30) {
1980  uint32_t len = AV_RB32(st->codec->extradata + 18); /* name atom length */
1981  uint32_t format = AV_RB32(st->codec->extradata + 22);
1982  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
1983  uint16_t str_size = AV_RB16(st->codec->extradata + 26); /* string length */
1984  if (str_size > 0 && size >= (int)str_size + 26) {
1985  char *reel_name = av_malloc(str_size + 1);
1986  if (!reel_name)
1987  return AVERROR(ENOMEM);
1988  memcpy(reel_name, st->codec->extradata + 30, str_size);
1989  reel_name[str_size] = 0; /* Add null terminator */
1990  /* don't add reel_name if emtpy string */
1991  if (*reel_name == 0) {
1992  av_free(reel_name);
1993  } else {
1994  av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL);
1995  }
1996  }
1997  }
1998  }
1999  }
2000  } else {
2001  /* other codec type, just skip (rtp, mp4s ...) */
2002  avio_skip(pb, size);
2003  }
2004  return 0;
2005 }
2006 
2008  AVStream *st, MOVStreamContext *sc)
2009 {
2010  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2011  !st->codec->sample_rate && sc->time_scale > 1)
2012  st->codec->sample_rate = sc->time_scale;
2013 
2014  /* special codec parameters handling */
2015  switch (st->codec->codec_id) {
2016 #if CONFIG_DV_DEMUXER
2017  case AV_CODEC_ID_DVAUDIO:
2019  if (!c->dv_fctx) {
2020  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2021  return AVERROR(ENOMEM);
2022  }
2024  if (!c->dv_demux) {
2025  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2026  return AVERROR(ENOMEM);
2027  }
2028  sc->dv_audio_container = 1;
2030  break;
2031 #endif
2032  /* no ifdef since parameters are always those */
2033  case AV_CODEC_ID_QCELP:
2034  st->codec->channels = 1;
2035  // force sample rate for qcelp when not stored in mov
2036  if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
2037  st->codec->sample_rate = 8000;
2038  // FIXME: Why is the following needed for some files?
2039  sc->samples_per_frame = 160;
2040  if (!sc->bytes_per_frame)
2041  sc->bytes_per_frame = 35;
2042  break;
2043  case AV_CODEC_ID_AMR_NB:
2044  st->codec->channels = 1;
2045  /* force sample rate for amr, stsd in 3gp does not store sample rate */
2046  st->codec->sample_rate = 8000;
2047  break;
2048  case AV_CODEC_ID_AMR_WB:
2049  st->codec->channels = 1;
2050  st->codec->sample_rate = 16000;
2051  break;
2052  case AV_CODEC_ID_MP2:
2053  case AV_CODEC_ID_MP3:
2054  /* force type after stsd for m1a hdlr */
2056  break;
2057  case AV_CODEC_ID_GSM:
2058  case AV_CODEC_ID_ADPCM_MS:
2060  case AV_CODEC_ID_ILBC:
2061  case AV_CODEC_ID_MACE3:
2062  case AV_CODEC_ID_MACE6:
2063  case AV_CODEC_ID_QDM2:
2064  st->codec->block_align = sc->bytes_per_frame;
2065  break;
2066  case AV_CODEC_ID_ALAC:
2067  if (st->codec->extradata_size == 36) {
2068  st->codec->channels = AV_RB8 (st->codec->extradata + 21);
2069  st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
2070  }
2071  break;
2072  case AV_CODEC_ID_AC3:
2073  case AV_CODEC_ID_EAC3:
2075  case AV_CODEC_ID_VC1:
2077  break;
2078  default:
2079  break;
2080  }
2081  return 0;
2082 }
2083 
2085  int codec_tag, int format,
2086  int64_t size)
2087 {
2088  int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2089 
2090  if (codec_tag &&
2091  (codec_tag != format &&
2092  (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2093  : codec_tag != MKTAG('j','p','e','g')))) {
2094  /* Multiple fourcc, we skip JPEG. This is not correct, we should
2095  * export it as a separate AVStream but this needs a few changes
2096  * in the MOV demuxer, patch welcome. */
2097 
2098  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2099  avio_skip(pb, size);
2100  return 1;
2101  }
2102  if ( codec_tag == AV_RL32("avc1") ||
2103  codec_tag == AV_RL32("hvc1") ||
2104  codec_tag == AV_RL32("hev1")
2105  )
2106  av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
2107 
2108  return 0;
2109 }
2110 
2112 {
2113  AVStream *st;
2114  MOVStreamContext *sc;
2115  int pseudo_stream_id;
2116 
2117  if (c->fc->nb_streams < 1)
2118  return 0;
2119  st = c->fc->streams[c->fc->nb_streams-1];
2120  sc = st->priv_data;
2121 
2122  for (pseudo_stream_id = 0;
2123  pseudo_stream_id < entries && !pb->eof_reached;
2124  pseudo_stream_id++) {
2125  //Parsing Sample description table
2126  enum AVCodecID id;
2127  int ret, dref_id = 1;
2128  MOVAtom a = { AV_RL32("stsd") };
2129  int64_t start_pos = avio_tell(pb);
2130  int64_t size = avio_rb32(pb); /* size */
2131  uint32_t format = avio_rl32(pb); /* data format */
2132 
2133  if (size >= 16) {
2134  avio_rb32(pb); /* reserved */
2135  avio_rb16(pb); /* reserved */
2136  dref_id = avio_rb16(pb);
2137  }else if (size <= 7){
2138  av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
2139  return AVERROR_INVALIDDATA;
2140  }
2141 
2142  if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
2143  size - (avio_tell(pb) - start_pos)))
2144  continue;
2145 
2146  sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
2147  sc->dref_id= dref_id;
2148 
2149  id = mov_codec_id(st, format);
2150 
2151  av_log(c->fc, AV_LOG_TRACE,
2152  "size=%"PRId64" 4CC= %c%c%c%c/0x%08x codec_type=%d\n", size,
2153  (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
2154  (format >> 24) & 0xff, format, st->codec->codec_type);
2155 
2156  if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
2157  st->codec->codec_id = id;
2158  mov_parse_stsd_video(c, pb, st, sc);
2159  } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
2160  st->codec->codec_id = id;
2161  mov_parse_stsd_audio(c, pb, st, sc);
2162  } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
2163  st->codec->codec_id = id;
2164  mov_parse_stsd_subtitle(c, pb, st, sc,
2165  size - (avio_tell(pb) - start_pos));
2166  } else {
2167  ret = mov_parse_stsd_data(c, pb, st, sc,
2168  size - (avio_tell(pb) - start_pos));
2169  if (ret < 0)
2170  return ret;
2171  }
2172  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2173  a.size = size - (avio_tell(pb) - start_pos);
2174  if (a.size > 8) {
2175  if ((ret = mov_read_default(c, pb, a)) < 0)
2176  return ret;
2177  } else if (a.size > 0)
2178  avio_skip(pb, a.size);
2179  }
2180 
2181  if (pb->eof_reached)
2182  return AVERROR_EOF;
2183 
2184  return mov_finalize_stsd_codec(c, pb, st, sc);
2185 }
2186 
2188 {
2189  int entries;
2190 
2191  avio_r8(pb); /* version */
2192  avio_rb24(pb); /* flags */
2193  entries = avio_rb32(pb);
2194 
2195  return ff_mov_read_stsd_entries(c, pb, entries);
2196 }
2197 
2199 {
2200  AVStream *st;
2201  MOVStreamContext *sc;
2202  unsigned int i, entries;
2203 
2204  if (c->fc->nb_streams < 1)
2205  return 0;
2206  st = c->fc->streams[c->fc->nb_streams-1];
2207  sc = st->priv_data;
2208 
2209  avio_r8(pb); /* version */
2210  avio_rb24(pb); /* flags */
2211 
2212  entries = avio_rb32(pb);
2213  if ((uint64_t)entries * 12 + 4 > atom.size)
2214  return AVERROR_INVALIDDATA;
2215 
2216  av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
2217 
2218  if (!entries)
2219  return 0;
2220  if (sc->stsc_data)
2221  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
2222  av_free(sc->stsc_data);
2223  sc->stsc_count = 0;
2224  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2225  if (!sc->stsc_data)
2226  return AVERROR(ENOMEM);
2227 
2228  for (i = 0; i < entries && !pb->eof_reached; i++) {
2229  sc->stsc_data[i].first = avio_rb32(pb);
2230  sc->stsc_data[i].count = avio_rb32(pb);
2231  sc->stsc_data[i].id = avio_rb32(pb);
2232  }
2233 
2234  sc->stsc_count = i;
2235  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2236  int64_t first_min = i + 1;
2237  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2238  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2239  sc->stsc_data[i].first < first_min ||
2240  sc->stsc_data[i].count < 1 ||
2241  sc->stsc_data[i].id < 1) {
2242  av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
2243  if (i+1 >= sc->stsc_count) {
2244  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2245  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2246  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2247  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2248  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
2249  continue;
2250  }
2251  av_assert0(sc->stsc_data[i+1].first >= 2);
2252  // We replace this entry by the next valid
2253  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2254  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2255  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
2256  }
2257  }
2258 
2259  if (pb->eof_reached)
2260  return AVERROR_EOF;
2261 
2262  return 0;
2263 }
2264 
2266 {
2267  AVStream *st;
2268  MOVStreamContext *sc;
2269  unsigned 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_rb32(pb); // version + flags
2277 
2278  entries = avio_rb32(pb);
2279  if (sc->stps_data)
2280  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2281  av_free(sc->stps_data);
2282  sc->stps_count = 0;
2283  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2284  if (!sc->stps_data)
2285  return AVERROR(ENOMEM);
2286 
2287  for (i = 0; i < entries && !pb->eof_reached; i++) {
2288  sc->stps_data[i] = avio_rb32(pb);
2289  //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
2290  }
2291 
2292  sc->stps_count = i;
2293 
2294  if (pb->eof_reached)
2295  return AVERROR_EOF;
2296 
2297  return 0;
2298 }
2299 
2301 {
2302  AVStream *st;
2303  MOVStreamContext *sc;
2304  unsigned int i, entries;
2305 
2306  if (c->fc->nb_streams < 1)
2307  return 0;
2308  st = c->fc->streams[c->fc->nb_streams-1];
2309  sc = st->priv_data;
2310 
2311  avio_r8(pb); /* version */
2312  avio_rb24(pb); /* flags */
2313 
2314  entries = avio_rb32(pb);
2315 
2316  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
2317 
2318  if (!entries)
2319  {
2320  sc->keyframe_absent = 1;
2321  if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2323  return 0;
2324  }
2325  if (sc->keyframes)
2326  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2327  if (entries >= UINT_MAX / sizeof(int))
2328  return AVERROR_INVALIDDATA;
2329  av_freep(&sc->keyframes);
2330  sc->keyframe_count = 0;
2331  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2332  if (!sc->keyframes)
2333  return AVERROR(ENOMEM);
2334 
2335  for (i = 0; i < entries && !pb->eof_reached; i++) {
2336  sc->keyframes[i] = avio_rb32(pb);
2337  //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
2338  }
2339 
2340  sc->keyframe_count = i;
2341 
2342  if (pb->eof_reached)
2343  return AVERROR_EOF;
2344 
2345  return 0;
2346 }
2347 
2349 {
2350  AVStream *st;
2351  MOVStreamContext *sc;
2352  unsigned int i, entries, sample_size, field_size, num_bytes;
2353  GetBitContext gb;
2354  unsigned char* buf;
2355  int ret;
2356 
2357  if (c->fc->nb_streams < 1)
2358  return 0;
2359  st = c->fc->streams[c->fc->nb_streams-1];
2360  sc = st->priv_data;
2361 
2362  avio_r8(pb); /* version */
2363  avio_rb24(pb); /* flags */
2364 
2365  if (atom.type == MKTAG('s','t','s','z')) {
2366  sample_size = avio_rb32(pb);
2367  if (!sc->sample_size) /* do not overwrite value computed in stsd */
2368  sc->sample_size = sample_size;
2369  sc->stsz_sample_size = sample_size;
2370  field_size = 32;
2371  } else {
2372  sample_size = 0;
2373  avio_rb24(pb); /* reserved */
2374  field_size = avio_r8(pb);
2375  }
2376  entries = avio_rb32(pb);
2377 
2378  av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2379 
2380  sc->sample_count = entries;
2381  if (sample_size)
2382  return 0;
2383 
2384  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2385  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2386  return AVERROR_INVALIDDATA;
2387  }
2388 
2389  if (!entries)
2390  return 0;
2391  if (entries >= (UINT_MAX - 4) / field_size)
2392  return AVERROR_INVALIDDATA;
2393  if (sc->sample_sizes)
2394  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2395  av_free(sc->sample_sizes);
2396  sc->sample_count = 0;
2397  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2398  if (!sc->sample_sizes)
2399  return AVERROR(ENOMEM);
2400 
2401  num_bytes = (entries*field_size+4)>>3;
2402 
2403  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2404  if (!buf) {
2405  av_freep(&sc->sample_sizes);
2406  return AVERROR(ENOMEM);
2407  }
2408 
2409  ret = ffio_read_size(pb, buf, num_bytes);
2410  if (ret < 0) {
2411  av_freep(&sc->sample_sizes);
2412  av_free(buf);
2413  return ret;
2414  }
2415 
2416  init_get_bits(&gb, buf, 8*num_bytes);
2417 
2418  for (i = 0; i < entries && !pb->eof_reached; i++) {
2419  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2420  if (sc->sample_sizes[i] < 0) {
2421  av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
2422  return AVERROR_INVALIDDATA;
2423  }
2424  sc->data_size += sc->sample_sizes[i];
2425  }
2426 
2427  sc->sample_count = i;
2428 
2429  av_free(buf);
2430 
2431  if (pb->eof_reached)
2432  return AVERROR_EOF;
2433 
2434  return 0;
2435 }
2436 
2438 {
2439  AVStream *st;
2440  MOVStreamContext *sc;
2441  unsigned int i, entries;
2442  int64_t duration=0;
2443  int64_t total_sample_count=0;
2444 
2445  if (c->fc->nb_streams < 1)
2446  return 0;
2447  st = c->fc->streams[c->fc->nb_streams-1];
2448  sc = st->priv_data;
2449 
2450  avio_r8(pb); /* version */
2451  avio_rb24(pb); /* flags */
2452  entries = avio_rb32(pb);
2453 
2454  av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
2455  c->fc->nb_streams-1, entries);
2456 
2457  if (sc->stts_data)
2458  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2459  av_free(sc->stts_data);
2460  sc->stts_count = 0;
2461  sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data));
2462  if (!sc->stts_data)
2463  return AVERROR(ENOMEM);
2464 
2465  for (i = 0; i < entries && !pb->eof_reached; i++) {
2466  int sample_duration;
2467  int sample_count;
2468 
2469  sample_count=avio_rb32(pb);
2470  sample_duration = avio_rb32(pb);
2471 
2472  if (sample_count < 0) {
2473  av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2474  return AVERROR_INVALIDDATA;
2475  }
2476  sc->stts_data[i].count= sample_count;
2477  sc->stts_data[i].duration= sample_duration;
2478 
2479  av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2480  sample_count, sample_duration);
2481 
2482  if ( i+1 == entries
2483  && i
2484  && sample_count == 1
2485  && total_sample_count > 100
2486  && sample_duration/10 > duration / total_sample_count)
2487  sample_duration = duration / total_sample_count;
2488  duration+=(int64_t)sample_duration*(uint64_t)sample_count;
2489  total_sample_count+=sample_count;
2490  }
2491 
2492  sc->stts_count = i;
2493 
2494  if (duration > 0 &&
2495  duration <= INT64_MAX - sc->duration_for_fps &&
2496  total_sample_count <= INT64_MAX - sc->nb_frames_for_fps
2497  ) {
2498  sc->duration_for_fps += duration;
2499  sc->nb_frames_for_fps += total_sample_count;
2500  }
2501 
2502  if (pb->eof_reached)
2503  return AVERROR_EOF;
2504 
2505  st->nb_frames= total_sample_count;
2506  if (duration)
2507  st->duration= duration;
2508  sc->track_end = duration;
2509  return 0;
2510 }
2511 
2513 {
2514  if (duration < 0) {
2515  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2516  }
2517 }
2518 
2520 {
2521  AVStream *st;
2522  MOVStreamContext *sc;
2523  unsigned int i, entries;
2524 
2525  if (c->fc->nb_streams < 1)
2526  return 0;
2527  st = c->fc->streams[c->fc->nb_streams-1];
2528  sc = st->priv_data;
2529 
2530  avio_r8(pb); /* version */
2531  avio_rb24(pb); /* flags */
2532  entries = avio_rb32(pb);
2533 
2534  av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2535 
2536  if (!entries)
2537  return 0;
2538  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2539  return AVERROR_INVALIDDATA;
2540  av_freep(&sc->ctts_data);
2541  sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
2542  if (!sc->ctts_data)
2543  return AVERROR(ENOMEM);
2544 
2545  for (i = 0; i < entries && !pb->eof_reached; i++) {
2546  int count =avio_rb32(pb);
2547  int duration =avio_rb32(pb);
2548 
2549  sc->ctts_data[i].count = count;
2550  sc->ctts_data[i].duration= duration;
2551 
2552  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
2553  count, duration);
2554 
2555  if (FFNABS(duration) < -(1<<28) && i+2<entries) {
2556  av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2557  av_freep(&sc->ctts_data);
2558  sc->ctts_count = 0;
2559  return 0;
2560  }
2561 
2562  if (i+2<entries)
2563  mov_update_dts_shift(sc, duration);
2564  }
2565 
2566  sc->ctts_count = i;
2567 
2568  if (pb->eof_reached)
2569  return AVERROR_EOF;
2570 
2571  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
2572 
2573  return 0;
2574 }
2575 
2577 {
2578  AVStream *st;
2579  MOVStreamContext *sc;
2580  unsigned int i, entries;
2581  uint8_t version;
2582  uint32_t grouping_type;
2583 
2584  if (c->fc->nb_streams < 1)
2585  return 0;
2586  st = c->fc->streams[c->fc->nb_streams-1];
2587  sc = st->priv_data;
2588 
2589  version = avio_r8(pb); /* version */
2590  avio_rb24(pb); /* flags */
2591  grouping_type = avio_rl32(pb);
2592  if (grouping_type != MKTAG( 'r','a','p',' '))
2593  return 0; /* only support 'rap ' grouping */
2594  if (version == 1)
2595  avio_rb32(pb); /* grouping_type_parameter */
2596 
2597  entries = avio_rb32(pb);
2598  if (!entries)
2599  return 0;
2600  if (sc->rap_group)
2601  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
2602  av_free(sc->rap_group);
2603  sc->rap_group_count = 0;
2604  sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
2605  if (!sc->rap_group)
2606  return AVERROR(ENOMEM);
2607 
2608  for (i = 0; i < entries && !pb->eof_reached; i++) {
2609  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2610  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2611  }
2612 
2613  sc->rap_group_count = i;
2614 
2615  return pb->eof_reached ? AVERROR_EOF : 0;
2616 }
2617 
2618 static void mov_build_index(MOVContext *mov, AVStream *st)
2619 {
2620  MOVStreamContext *sc = st->priv_data;
2621  int64_t current_offset;
2622  int64_t current_dts = 0;
2623  unsigned int stts_index = 0;
2624  unsigned int stsc_index = 0;
2625  unsigned int stss_index = 0;
2626  unsigned int stps_index = 0;
2627  unsigned int i, j;
2628  uint64_t stream_size = 0;
2629 
2630  if (sc->elst_count) {
2631  int i, edit_start_index = 0, unsupported = 0;
2632  int64_t empty_duration = 0; // empty duration of the first edit list entry
2633  int64_t start_time = 0; // start time of the media
2634 
2635  for (i = 0; i < sc->elst_count; i++) {
2636  const MOVElst *e = &sc->elst_data[i];
2637  if (i == 0 && e->time == -1) {
2638  /* if empty, the first entry is the start time of the stream
2639  * relative to the presentation itself */
2640  empty_duration = e->duration;
2641  edit_start_index = 1;
2642  } else if (i == edit_start_index && e->time >= 0) {
2643  start_time = e->time;
2644  } else
2645  unsupported = 1;
2646  }
2647  if (unsupported)
2648  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
2649  "a/v desync might occur, patch welcome\n");
2650 
2651  /* adjust first dts according to edit list */
2652  if ((empty_duration || start_time) && mov->time_scale > 0) {
2653  if (empty_duration)
2654  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
2655  sc->time_offset = start_time - empty_duration;
2656  current_dts = -sc->time_offset;
2657  if (sc->ctts_count>0 && sc->stts_count>0 &&
2658  sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2659  /* more than 16 frames delay, dts are likely wrong
2660  this happens with files created by iMovie */
2661  sc->wrong_dts = 1;
2662  st->codec->has_b_frames = 1;
2663  }
2664  }
2665  }
2666 
2667  /* only use old uncompressed audio chunk demuxing when stts specifies it */
2668  if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2669  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2670  unsigned int current_sample = 0;
2671  unsigned int stts_sample = 0;
2672  unsigned int sample_size;
2673  unsigned int distance = 0;
2674  unsigned int rap_group_index = 0;
2675  unsigned int rap_group_sample = 0;
2676  int64_t last_dts = 0;
2677  int64_t dts_correction = 0;
2678  int rap_group_present = sc->rap_group_count && sc->rap_group;
2679  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2680 
2681  current_dts -= sc->dts_shift;
2682  last_dts = current_dts;
2683 
2684  if (!sc->sample_count || st->nb_index_entries)
2685  return;
2686  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2687  return;
2689  st->nb_index_entries + sc->sample_count,
2690  sizeof(*st->index_entries)) < 0) {
2691  st->nb_index_entries = 0;
2692  return;
2693  }
2695 
2696  for (i = 0; i < sc->chunk_count; i++) {
2697  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2698  current_offset = sc->chunk_offsets[i];
2699  while (stsc_index + 1 < sc->stsc_count &&
2700  i + 1 == sc->stsc_data[stsc_index + 1].first)
2701  stsc_index++;
2702 
2703  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2704  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2705  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2706  sc->stsz_sample_size = sc->sample_size;
2707  }
2708  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2709  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2710  sc->stsz_sample_size = sc->sample_size;
2711  }
2712 
2713  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2714  int keyframe = 0;
2715  if (current_sample >= sc->sample_count) {
2716  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2717  return;
2718  }
2719 
2720  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2721  keyframe = 1;
2722  if (stss_index + 1 < sc->keyframe_count)
2723  stss_index++;
2724  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2725  keyframe = 1;
2726  if (stps_index + 1 < sc->stps_count)
2727  stps_index++;
2728  }
2729  if (rap_group_present && rap_group_index < sc->rap_group_count) {
2730  if (sc->rap_group[rap_group_index].index > 0)
2731  keyframe = 1;
2732  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2733  rap_group_sample = 0;
2734  rap_group_index++;
2735  }
2736  }
2737  if (sc->keyframe_absent
2738  && !sc->stps_count
2739  && !rap_group_present
2740  && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2741  keyframe = 1;
2742  if (keyframe)
2743  distance = 0;
2744  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2745  if (sc->pseudo_stream_id == -1 ||
2746  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2747  AVIndexEntry *e;
2748  if (sample_size > 0x3FFFFFFF) {
2749  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
2750  return;
2751  }
2752  e = &st->index_entries[st->nb_index_entries++];
2753  e->pos = current_offset;
2754  e->timestamp = current_dts;
2755  e->size = sample_size;
2756  e->min_distance = distance;
2757  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2758  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2759  "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2760  current_offset, current_dts, sample_size, distance, keyframe);
2761  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2762  ff_rfps_add_frame(mov->fc, st, current_dts);
2763  }
2764 
2765  current_offset += sample_size;
2766  stream_size += sample_size;
2767 
2768  /* A negative sample duration is invalid based on the spec,
2769  * but some samples need it to correct the DTS. */
2770  if (sc->stts_data[stts_index].duration < 0) {
2771  av_log(mov->fc, AV_LOG_WARNING,
2772  "Invalid SampleDelta %d in STTS, at %d st:%d\n",
2773  sc->stts_data[stts_index].duration, stts_index,
2774  st->index);
2775  dts_correction += sc->stts_data[stts_index].duration - 1;
2776  sc->stts_data[stts_index].duration = 1;
2777  }
2778  current_dts += sc->stts_data[stts_index].duration;
2779  if (!dts_correction || current_dts + dts_correction > last_dts) {
2780  current_dts += dts_correction;
2781  dts_correction = 0;
2782  } else {
2783  /* Avoid creating non-monotonous DTS */
2784  dts_correction += current_dts - last_dts - 1;
2785  current_dts = last_dts + 1;
2786  }
2787  last_dts = current_dts;
2788  distance++;
2789  stts_sample++;
2790  current_sample++;
2791  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2792  stts_sample = 0;
2793  stts_index++;
2794  }
2795  }
2796  }
2797  if (st->duration > 0)
2798  st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2799  } else {
2800  unsigned chunk_samples, total = 0;
2801 
2802  if (!sc->chunk_count)
2803  return;
2804 
2805  // compute total chunk count
2806  for (i = 0; i < sc->stsc_count; i++) {
2807  unsigned count, chunk_count;
2808 
2809  chunk_samples = sc->stsc_data[i].count;
2810  if (i != sc->stsc_count - 1 &&
2811  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2812  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2813  return;
2814  }
2815 
2816  if (sc->samples_per_frame >= 160) { // gsm
2817  count = chunk_samples / sc->samples_per_frame;
2818  } else if (sc->samples_per_frame > 1) {
2819  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2820  count = (chunk_samples+samples-1) / samples;
2821  } else {
2822  count = (chunk_samples+1023) / 1024;
2823  }
2824 
2825  if (i < sc->stsc_count - 1)
2826  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2827  else
2828  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2829  total += chunk_count * count;
2830  }
2831 
2832  av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
2833  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2834  return;
2836  st->nb_index_entries + total,
2837  sizeof(*st->index_entries)) < 0) {
2838  st->nb_index_entries = 0;
2839  return;
2840  }
2841  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2842 
2843  // populate index
2844  for (i = 0; i < sc->chunk_count; i++) {
2845  current_offset = sc->chunk_offsets[i];
2846  if (stsc_index + 1 < sc->stsc_count &&
2847  i + 1 == sc->stsc_data[stsc_index + 1].first)
2848  stsc_index++;
2849  chunk_samples = sc->stsc_data[stsc_index].count;
2850 
2851  while (chunk_samples > 0) {
2852  AVIndexEntry *e;
2853  unsigned size, samples;
2854 
2855  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
2857  "Zero bytes per frame, but %d samples per frame",
2858  sc->samples_per_frame);
2859  return;
2860  }
2861 
2862  if (sc->samples_per_frame >= 160) { // gsm
2863  samples = sc->samples_per_frame;
2864  size = sc->bytes_per_frame;
2865  } else {
2866  if (sc->samples_per_frame > 1) {
2867  samples = FFMIN((1024 / sc->samples_per_frame)*
2868  sc->samples_per_frame, chunk_samples);
2869  size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2870  } else {
2871  samples = FFMIN(1024, chunk_samples);
2872  size = samples * sc->sample_size;
2873  }
2874  }
2875 
2876  if (st->nb_index_entries >= total) {
2877  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2878  return;
2879  }
2880  if (size > 0x3FFFFFFF) {
2881  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
2882  return;
2883  }
2884  e = &st->index_entries[st->nb_index_entries++];
2885  e->pos = current_offset;
2886  e->timestamp = current_dts;
2887  e->size = size;
2888  e->min_distance = 0;
2889  e->flags = AVINDEX_KEYFRAME;
2890  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2891  "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2892  size, samples);
2893 
2894  current_offset += size;
2895  current_dts += samples;
2896  chunk_samples -= samples;
2897  }
2898  }
2899  }
2900 }
2901 
2902 static int test_same_origin(const char *src, const char *ref) {
2903  char src_proto[64];
2904  char ref_proto[64];
2905  char src_auth[256];
2906  char ref_auth[256];
2907  char src_host[256];
2908  char ref_host[256];
2909  int src_port=-1;
2910  int ref_port=-1;
2911 
2912  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
2913  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
2914 
2915  if (strlen(src) == 0) {
2916  return -1;
2917  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
2918  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
2919  strlen(src_host) + 1 >= sizeof(src_host) ||
2920  strlen(ref_host) + 1 >= sizeof(ref_host)) {
2921  return 0;
2922  } else if (strcmp(src_proto, ref_proto) ||
2923  strcmp(src_auth, ref_auth) ||
2924  strcmp(src_host, ref_host) ||
2925  src_port != ref_port) {
2926  return 0;
2927  } else
2928  return 1;
2929 }
2930 
2931 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref,
2933 {
2934  AVOpenCallback open_func = c->fc->open_cb;
2935 
2936  if (!open_func)
2937  open_func = ffio_open2_wrapper;
2938 
2939  /* try relative path, we do not try the absolute because it can leak information about our
2940  system to an attacker */
2941  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2942  char filename[1025];
2943  const char *src_path;
2944  int i, l;
2945 
2946  /* find a source dir */
2947  src_path = strrchr(src, '/');
2948  if (src_path)
2949  src_path++;
2950  else
2951  src_path = src;
2952 
2953  /* find a next level down to target */
2954  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2955  if (ref->path[l] == '/') {
2956  if (i == ref->nlvl_to - 1)
2957  break;
2958  else
2959  i++;
2960  }
2961 
2962  /* compose filename if next level down to target was found */
2963  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2964  memcpy(filename, src, src_path - src);
2965  filename[src_path - src] = 0;
2966 
2967  for (i = 1; i < ref->nlvl_from; i++)
2968  av_strlcat(filename, "../", sizeof(filename));
2969 
2970  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
2971  if (!c->use_absolute_path && !c->fc->open_cb) {
2972  int same_origin = test_same_origin(src, filename);
2973 
2974  if (!same_origin) {
2975  av_log(c->fc, AV_LOG_ERROR,
2976  "Reference with mismatching origin, %s not tried for security reasons, "
2977  "set demuxer option use_absolute_path to allow it anyway\n",
2978  ref->path);
2979  return AVERROR(ENOENT);
2980  }
2981 
2982  if(strstr(ref->path + l + 1, "..") ||
2983  strstr(ref->path + l + 1, ":") ||
2984  (ref->nlvl_from > 1 && same_origin < 0) ||
2985  (filename[0] == '/' && src_path == src))
2986  return AVERROR(ENOENT);
2987  }
2988 
2989  if (strlen(filename) + 1 == sizeof(filename))
2990  return AVERROR(ENOENT);
2991  if (!open_func(c->fc, pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2992  return 0;
2993  }
2994  } else if (c->use_absolute_path) {
2995  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
2996  "this is a possible security issue\n");
2997  if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2998  return 0;
2999  } else if (c->fc->open_cb) {
3000  if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
3001  return 0;
3002  } else {
3003  av_log(c->fc, AV_LOG_ERROR,
3004  "Absolute path %s not tried for security reasons, "
3005  "set demuxer option use_absolute_path to allow absolute paths\n",
3006  ref->path);
3007  }
3008 
3009  return AVERROR(ENOENT);
3010 }
3011 
3013 {
3014  if (sc->time_scale <= 0) {
3015  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
3016  sc->time_scale = c->time_scale;
3017  if (sc->time_scale <= 0)
3018  sc->time_scale = 1;
3019  }
3020 }
3021 
3023 {
3024  AVStream *st;
3025  MOVStreamContext *sc;
3026  int ret;
3027 
3028  st = avformat_new_stream(c->fc, NULL);
3029  if (!st) return AVERROR(ENOMEM);
3030  st->id = -1;
3031  sc = av_mallocz(sizeof(MOVStreamContext));
3032  if (!sc) return AVERROR(ENOMEM);
3033 
3034  st->priv_data = sc;
3036  sc->ffindex = st->index;
3037 
3038  if ((ret = mov_read_default(c, pb, atom)) < 0)
3039  return ret;
3040 
3041  /* sanity checks */
3042  if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
3043  (!sc->sample_size && !sc->sample_count))) {
3044  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
3045  st->index);
3046  return 0;
3047  }
3048  if (sc->chunk_count && sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
3049  av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
3050  st->index);
3051  return AVERROR_INVALIDDATA;
3052  }
3053 
3054  fix_timescale(c, sc);
3055 
3056  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
3057 
3058  mov_build_index(c, st);
3059 
3060  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
3061  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
3062  if (c->enable_drefs) {
3063  if (mov_open_dref(c, &sc->pb, c->fc->filename, dref,
3064  &c->fc->interrupt_callback) < 0)
3065  av_log(c->fc, AV_LOG_ERROR,
3066  "stream %d, error opening alias: path='%s', dir='%s', "
3067  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
3068  st->index, dref->path, dref->dir, dref->filename,
3069  dref->volume, dref->nlvl_from, dref->nlvl_to);
3070  } else {
3071  av_log(c->fc, AV_LOG_WARNING,
3072  "Skipped opening external track: "
3073  "stream %d, alias: path='%s', dir='%s', "
3074  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
3075  "Set enable_drefs to allow this.\n",
3076  st->index, dref->path, dref->dir, dref->filename,
3077  dref->volume, dref->nlvl_from, dref->nlvl_to);
3078  }
3079  } else {
3080  sc->pb = c->fc->pb;
3081  sc->pb_is_copied = 1;
3082  }
3083 
3084  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3085  if (!st->sample_aspect_ratio.num && st->codec->width && st->codec->height &&
3086  sc->height && sc->width &&
3087  (st->codec->width != sc->width || st->codec->height != sc->height)) {
3088  st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
3089  ((double)st->codec->width * sc->height), INT_MAX);
3090  }
3091 
3092 #if FF_API_R_FRAME_RATE
3093  if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
3095  sc->time_scale, sc->stts_data[0].duration, INT_MAX);
3096 #endif
3097  }
3098 
3099  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
3100  if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
3101  TAG_IS_AVCI(st->codec->codec_tag)) {
3102  ret = ff_generate_avci_extradata(st);
3103  if (ret < 0)
3104  return ret;
3105  }
3106 
3107  switch (st->codec->codec_id) {
3108 #if CONFIG_H261_DECODER
3109  case AV_CODEC_ID_H261:
3110 #endif
3111 #if CONFIG_H263_DECODER
3112  case AV_CODEC_ID_H263:
3113 #endif
3114 #if CONFIG_MPEG4_DECODER
3115  case AV_CODEC_ID_MPEG4:
3116 #endif
3117  st->codec->width = 0; /* let decoder init width/height */
3118  st->codec->height= 0;
3119  break;
3120  }
3121 
3122  /* Do not need those anymore. */
3123  av_freep(&sc->chunk_offsets);
3124  av_freep(&sc->stsc_data);
3125  av_freep(&sc->sample_sizes);
3126  av_freep(&sc->keyframes);
3127  av_freep(&sc->stts_data);
3128  av_freep(&sc->stps_data);
3129  av_freep(&sc->elst_data);
3130  av_freep(&sc->rap_group);
3131 
3132  return 0;
3133 }
3134 
3136 {
3137  int ret;
3138  c->itunes_metadata = 1;
3139  ret = mov_read_default(c, pb, atom);
3140  c->itunes_metadata = 0;
3141  return ret;
3142 }
3143 
3145 {
3146  int64_t end = avio_tell(pb) + size;
3147  uint8_t *key = NULL, *val = NULL;
3148  int i;
3149  AVStream *st;
3150  MOVStreamContext *sc;
3151 
3152  if (c->fc->nb_streams < 1)
3153  return 0;
3154  st = c->fc->streams[c->fc->nb_streams-1];
3155  sc = st->priv_data;
3156 
3157  for (i = 0; i < 2; i++) {
3158  uint8_t **p;
3159  uint32_t len, tag;
3160  int ret;
3161 
3162  if (end - avio_tell(pb) <= 12)
3163  break;
3164 
3165  len = avio_rb32(pb);
3166  tag = avio_rl32(pb);
3167  avio_skip(pb, 4); // flags
3168 
3169  if (len < 12 || len - 12 > end - avio_tell(pb))
3170  break;
3171  len -= 12;
3172 
3173  if (tag == MKTAG('n', 'a', 'm', 'e'))
3174  p = &key;
3175  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
3176  avio_skip(pb, 4);
3177  len -= 4;
3178  p = &val;
3179  } else
3180  break;
3181 
3182  *p = av_malloc(len + 1);
3183  if (!*p)
3184  break;
3185  ret = ffio_read_size(pb, *p, len);
3186  if (ret < 0) {
3187  av_freep(p);
3188  return ret;
3189  }
3190  (*p)[len] = 0;
3191  }
3192 
3193  if (key && val) {
3194  if (strcmp(key, "iTunSMPB") == 0) {
3195  int priming, remainder, samples;
3196  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
3197  if(priming>0 && priming<16384)
3198  sc->start_pad = priming;
3199  }
3200  }
3201  if (strcmp(key, "cdec") != 0) {
3202  av_dict_set(&c->fc->metadata, key, val,
3204  key = val = NULL;
3205  }
3206  }
3207 
3208  avio_seek(pb, end, SEEK_SET);
3209  av_freep(&key);
3210  av_freep(&val);
3211  return 0;
3212 }
3213 
3215 {
3216  int64_t end = avio_tell(pb) + atom.size;
3217  uint32_t tag, len;
3218 
3219  if (atom.size < 8)
3220  goto fail;
3221 
3222  len = avio_rb32(pb);
3223  tag = avio_rl32(pb);
3224 
3225  if (len > atom.size)
3226  goto fail;
3227 
3228  if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
3229  uint8_t domain[128];
3230  int domain_len;
3231 
3232  avio_skip(pb, 4); // flags
3233  len -= 12;
3234 
3235  domain_len = avio_get_str(pb, len, domain, sizeof(domain));
3236  avio_skip(pb, len - domain_len);
3237  return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
3238  }
3239 
3240 fail:
3241  av_log(c->fc, AV_LOG_VERBOSE,
3242  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
3243  return 0;
3244 }
3245 
3247 {
3248  while (atom.size > 8) {
3249  uint32_t tag = avio_rl32(pb);
3250  atom.size -= 4;
3251  if (tag == MKTAG('h','d','l','r')) {
3252  avio_seek(pb, -8, SEEK_CUR);
3253  atom.size += 8;
3254  return mov_read_default(c, pb, atom);
3255  }
3256  }
3257  return 0;
3258 }
3259 
3261 {
3262  int i;
3263  int width;
3264  int height;
3265  int display_matrix[3][3];
3266  AVStream *st;
3267  MOVStreamContext *sc;
3268  int version;
3269  int flags;
3270 
3271  if (c->fc->nb_streams < 1)
3272  return 0;
3273  st = c->fc->streams[c->fc->nb_streams-1];
3274  sc = st->priv_data;
3275 
3276  // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
3277  // avoids corrupting AVStreams mapped to an earlier tkhd.
3278  if (st->id != -1)
3279  return AVERROR_INVALIDDATA;
3280 
3281  version = avio_r8(pb);
3282  flags = avio_rb24(pb);
3284 
3285  if (version == 1) {
3286  avio_rb64(pb);
3287  avio_rb64(pb);
3288  } else {
3289  avio_rb32(pb); /* creation time */
3290  avio_rb32(pb); /* modification time */
3291  }
3292  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
3293  avio_rb32(pb); /* reserved */
3294 
3295  /* highlevel (considering edits) duration in movie timebase */
3296  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
3297  avio_rb32(pb); /* reserved */
3298  avio_rb32(pb); /* reserved */
3299 
3300  avio_rb16(pb); /* layer */
3301  avio_rb16(pb); /* alternate group */
3302  avio_rb16(pb); /* volume */
3303  avio_rb16(pb); /* reserved */
3304 
3305  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
3306  // they're kept in fixed point format through all calculations
3307  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
3308  // side data, but the scale factor is not needed to calculate aspect ratio
3309  for (i = 0; i < 3; i++) {
3310  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
3311  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
3312  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
3313  }
3314 
3315  width = avio_rb32(pb); // 16.16 fixed point track width
3316  height = avio_rb32(pb); // 16.16 fixed point track height
3317  sc->width = width >> 16;
3318  sc->height = height >> 16;
3319 
3320  // save the matrix and add rotate metadata when it is not the default
3321  // identity
3322  if (display_matrix[0][0] != (1 << 16) ||
3323  display_matrix[1][1] != (1 << 16) ||
3324  display_matrix[2][2] != (1 << 30) ||
3325  display_matrix[0][1] || display_matrix[0][2] ||
3326  display_matrix[1][0] || display_matrix[1][2] ||
3327  display_matrix[2][0] || display_matrix[2][1]) {
3328  int i, j;
3329  double rotate;
3330 
3331  av_freep(&sc->display_matrix);
3332  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
3333  if (!sc->display_matrix)
3334  return AVERROR(ENOMEM);
3335 
3336  for (i = 0; i < 3; i++)
3337  for (j = 0; j < 3; j++)
3338  sc->display_matrix[i * 3 + j] = display_matrix[i][j];
3339 
3341  if (!isnan(rotate)) {
3342  char rotate_buf[64];
3343  rotate = -rotate;
3344  if (rotate < 0) // for backward compatibility
3345  rotate += 360;
3346  snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
3347  av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
3348  }
3349  }
3350 
3351  // transform the display width/height according to the matrix
3352  // to keep the same scale, use [width height 1<<16]
3353  if (width && height && sc->display_matrix) {
3354  double disp_transform[2];
3355 
3356 #define SQR(a) ((a)*(double)(a))
3357  for (i = 0; i < 2; i++)
3358  disp_transform[i] = sqrt(SQR(display_matrix[i][0]) + SQR(display_matrix[i][1]));
3359 
3360  if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
3361  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
3362  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
3364  disp_transform[0] / disp_transform[1],
3365  INT_MAX);
3366  }
3367  return 0;
3368 }
3369 
3371 {
3372  MOVFragment *frag = &c->fragment;
3373  MOVTrackExt *trex = NULL;
3375  int flags, track_id, i, found = 0;
3376 
3377  avio_r8(pb); /* version */
3378  flags = avio_rb24(pb);
3379 
3380  track_id = avio_rb32(pb);
3381  if (!track_id)
3382  return AVERROR_INVALIDDATA;
3383  frag->track_id = track_id;
3384  for (i = 0; i < c->trex_count; i++)
3385  if (c->trex_data[i].track_id == frag->track_id) {
3386  trex = &c->trex_data[i];
3387  break;
3388  }
3389  if (!trex) {
3390  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
3391  return AVERROR_INVALIDDATA;
3392  }
3393  c->fragment.found_tfhd = 1;
3394 
3397  frag->moof_offset : frag->implicit_offset;
3398  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
3399 
3400  frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
3401  avio_rb32(pb) : trex->duration;
3402  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
3403  avio_rb32(pb) : trex->size;
3404  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
3405  avio_rb32(pb) : trex->flags;
3406  frag->time = AV_NOPTS_VALUE;
3407  for (i = 0; i < c->fragment_index_count; i++) {
3408  int j;
3409  MOVFragmentIndex* candidate = c->fragment_index_data[i];
3410  if (candidate->track_id == frag->track_id) {
3411  av_log(c->fc, AV_LOG_DEBUG,
3412  "found fragment index for track %u\n", frag->track_id);
3413  index = candidate;
3414  for (j = index->current_item; j < index->item_count; j++) {
3415  if (frag->implicit_offset == index->items[j].moof_offset) {
3416  av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
3417  "for track %u and moof_offset %"PRId64"\n",
3418  frag->track_id, index->items[j].moof_offset);
3419  frag->time = index->items[j].time;
3420  index->current_item = j + 1;
3421  found = 1;
3422  break;
3423  }
3424  }
3425  if (found)
3426  break;
3427  }
3428  }
3429  if (index && !found) {
3430  av_log(c->fc, AV_LOG_DEBUG, "track %u has a fragment index but "
3431  "it doesn't have an (in-order) entry for moof_offset "
3432  "%"PRId64"\n", frag->track_id, frag->implicit_offset);
3433  }
3434  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
3435  return 0;
3436 }
3437 
3439 {
3440  c->chapter_track = avio_rb32(pb);
3441  return 0;
3442 }
3443 
3445 {
3446  MOVTrackExt *trex;
3447  int err;
3448 
3449  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
3450  return AVERROR_INVALIDDATA;
3451  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
3452  sizeof(*c->trex_data))) < 0) {
3453  c->trex_count = 0;
3454  return err;
3455  }
3456 
3457  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
3458 
3459  trex = &c->trex_data[c->trex_count++];
3460  avio_r8(pb); /* version */
3461  avio_rb24(pb); /* flags */
3462  trex->track_id = avio_rb32(pb);
3463  trex->stsd_id = avio_rb32(pb);
3464  trex->duration = avio_rb32(pb);
3465  trex->size = avio_rb32(pb);
3466  trex->flags = avio_rb32(pb);
3467  return 0;
3468 }
3469 
3471 {
3472  MOVFragment *frag = &c->fragment;
3473  AVStream *st = NULL;
3474  MOVStreamContext *sc;
3475  int version, i;
3476 
3477  for (i = 0; i < c->fc->nb_streams; i++) {
3478  if (c->fc->streams[i]->id == frag->track_id) {
3479  st = c->fc->streams[i];
3480  break;
3481  }
3482  }
3483  if (!st) {
3484  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3485  return AVERROR_INVALIDDATA;
3486  }
3487  sc = st->priv_data;
3488  if (sc->pseudo_stream_id + 1 != frag->stsd_id)
3489  return 0;
3490  version = avio_r8(pb);
3491  avio_rb24(pb); /* flags */
3492  if (version) {
3493  sc->track_end = avio_rb64(pb);
3494  } else {
3495  sc->track_end = avio_rb32(pb);
3496  }
3497  return 0;
3498 }
3499 
3501 {
3502  MOVFragment *frag = &c->fragment;
3503  AVStream *st = NULL;
3504  MOVStreamContext *sc;
3505  MOVStts *ctts_data;
3506  uint64_t offset;
3507  int64_t dts;
3508  int data_offset = 0;
3509  unsigned entries, first_sample_flags = frag->flags;
3510  int flags, distance, i, found_keyframe = 0, err;
3511 
3512  if (!frag->found_tfhd) {
3513  av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
3514  return AVERROR_INVALIDDATA;
3515  }
3516 
3517  for (i = 0; i < c->fc->nb_streams; i++) {
3518  if (c->fc->streams[i]->id == frag->track_id) {
3519  st = c->fc->streams[i];
3520  break;
3521  }
3522  }
3523  if (!st) {
3524  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3525  return AVERROR_INVALIDDATA;
3526  }
3527  sc = st->priv_data;
3528  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
3529  return 0;
3530  avio_r8(pb); /* version */
3531  flags = avio_rb24(pb);
3532  entries = avio_rb32(pb);
3533  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
3534 
3535  /* Always assume the presence of composition time offsets.
3536  * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
3537  * 1) in the initial movie, there are no samples.
3538  * 2) in the first movie fragment, there is only one sample without composition time offset.
3539  * 3) in the subsequent movie fragments, there are samples with composition time offset. */
3540  if (!sc->ctts_count && sc->sample_count)
3541  {
3542  /* Complement ctts table if moov atom doesn't have ctts atom. */
3543  ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
3544  if (!ctts_data)
3545  return AVERROR(ENOMEM);
3546  sc->ctts_data = ctts_data;
3547  sc->ctts_data[sc->ctts_count].count = sc->sample_count;
3548  sc->ctts_data[sc->ctts_count].duration = 0;
3549  sc->ctts_count++;
3550  }
3551  if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
3552  return AVERROR_INVALIDDATA;
3553  if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
3554  sizeof(*sc->ctts_data))) < 0) {
3555  sc->ctts_count = 0;
3556  return err;
3557  }
3558  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
3559  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
3560  dts = sc->track_end - sc->time_offset;
3561  offset = frag->base_data_offset + data_offset;
3562  distance = 0;
3563  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
3564  for (i = 0; i < entries && !pb->eof_reached; i++) {
3565  unsigned sample_size = frag->size;
3566  int sample_flags = i ? frag->flags : first_sample_flags;
3567  unsigned sample_duration = frag->duration;
3568  int keyframe = 0;
3569 
3570  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
3571  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
3572  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
3573  sc->ctts_data[sc->ctts_count].count = 1;
3574  sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
3575  avio_rb32(pb) : 0;
3577  if (frag->time != AV_NOPTS_VALUE) {
3578  if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
3579  int64_t pts = frag->time;
3580  av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3581  " sc->dts_shift %d ctts.duration %d"
3582  " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
3583  sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
3584  sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
3585  dts = pts - sc->dts_shift;
3586  if (flags & MOV_TRUN_SAMPLE_CTS) {
3587  dts -= sc->ctts_data[sc->ctts_count].duration;
3588  } else {
3589  dts -= sc->time_offset;
3590  }
3591  av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
3592  } else {
3593  dts = frag->time - sc->time_offset;
3594  av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3595  ", using it for dts\n", dts);
3596  }
3597  frag->time = AV_NOPTS_VALUE;
3598  }
3599  sc->ctts_count++;
3600  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3601  keyframe = 1;
3602  else if (!found_keyframe)
3603  keyframe = found_keyframe =
3604  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
3606  if (keyframe)
3607  distance = 0;
3608  err = av_add_index_entry(st, offset, dts, sample_size, distance,
3609  keyframe ? AVINDEX_KEYFRAME : 0);
3610  if (err < 0) {
3611  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
3612  }
3613  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
3614  "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
3615  offset, dts, sample_size, distance, keyframe);
3616  distance++;
3617  dts += sample_duration;
3618  offset += sample_size;
3619  sc->data_size += sample_size;
3620 
3621  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
3622  1 <= INT64_MAX - sc->nb_frames_for_fps
3623  ) {
3624  sc->duration_for_fps += sample_duration;
3625  sc->nb_frames_for_fps ++;
3626  }
3627  }
3628 
3629  if (pb->eof_reached)
3630  return AVERROR_EOF;
3631 
3632  frag->implicit_offset = offset;
3633 
3634  sc->track_end = dts + sc->time_offset;
3635  if (st->duration < sc->track_end)
3636  st->duration = sc->track_end;
3637 
3638  return 0;
3639 }
3640 
3642 {
3643  int64_t offset = avio_tell(pb) + atom.size, pts;
3644  uint8_t version;
3645  unsigned i, track_id;
3646  AVStream *st = NULL;
3647  MOVStreamContext *sc;
3649  MOVFragmentIndex **tmp;
3650  AVRational timescale;
3651 
3652  version = avio_r8(pb);
3653  if (version > 1) {
3654  avpriv_request_sample(c->fc, "sidx version %u", version);
3655  return 0;
3656  }
3657 
3658  avio_rb24(pb); // flags
3659 
3660  track_id = avio_rb32(pb); // Reference ID
3661  for (i = 0; i < c->fc->nb_streams; i++) {
3662  if (c->fc->streams[i]->id == track_id) {
3663  st = c->fc->streams[i];
3664  break;
3665  }
3666  }
3667  if (!st) {
3668  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
3669  return 0;
3670  }
3671 
3672  sc = st->priv_data;
3673 
3674  timescale = av_make_q(1, avio_rb32(pb));
3675 
3676  if (version == 0) {
3677  pts = avio_rb32(pb);
3678  offset += avio_rb32(pb);
3679  } else {
3680  pts = avio_rb64(pb);
3681  offset += avio_rb64(pb);
3682  }
3683 
3684  avio_rb16(pb); // reserved
3685 
3686  index = av_mallocz(sizeof(MOVFragmentIndex));
3687  if (!index)
3688  return AVERROR(ENOMEM);
3689 
3690  index->track_id = track_id;
3691 
3692  index->item_count = avio_rb16(pb);
3693  index->items = av_mallocz_array(index->item_count, sizeof(MOVFragmentIndexItem));
3694 
3695  if (!index->items) {
3696  av_freep(&index);
3697  return AVERROR(ENOMEM);
3698  }
3699 
3700  for (i = 0; i < index->item_count; i++) {
3701  uint32_t size = avio_rb32(pb);
3702  uint32_t duration = avio_rb32(pb);
3703  if (size & 0x80000000) {
3704  avpriv_request_sample(c->fc, "sidx reference_type 1");
3705  av_freep(&index->items);
3706  av_freep(&index);
3707  return AVERROR_PATCHWELCOME;
3708  }
3709  avio_rb32(pb); // sap_flags
3710  index->items[i].moof_offset = offset;
3711  index->items[i].time = av_rescale_q(pts, st->time_base, timescale);
3712  offset += size;
3713  pts += duration;
3714  }
3715 
3716  st->duration = sc->track_end = pts;
3717 
3719  c->fragment_index_count + 1,
3720  sizeof(MOVFragmentIndex*));
3721  if (!tmp) {
3722  av_freep(&index->items);
3723  av_freep(&index);
3724  return AVERROR(ENOMEM);
3725  }
3726 
3727  c->fragment_index_data = tmp;
3729 
3730  if (offset == avio_size(pb))
3731  c->fragment_index_complete = 1;
3732 
3733  return 0;
3734 }
3735 
3736 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
3737 /* like the files created with Adobe Premiere 5.0, for samples see */
3738 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
3740 {
3741  int err;
3742 
3743  if (atom.size < 8)
3744  return 0; /* continue */
3745  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3746  avio_skip(pb, atom.size - 4);
3747  return 0;
3748  }
3749  atom.type = avio_rl32(pb);
3750  atom.size -= 8;
3751  if (atom.type != MKTAG('m','d','a','t')) {
3752  avio_skip(pb, atom.size);
3753  return 0;
3754  }
3755  err = mov_read_mdat(c, pb, atom);
3756  return err;
3757 }
3758 
3760 {
3761 #if CONFIG_ZLIB
3762  AVIOContext ctx;
3763  uint8_t *cmov_data;
3764  uint8_t *moov_data; /* uncompressed data */
3765  long cmov_len, moov_len;
3766  int ret = -1;
3767 
3768  avio_rb32(pb); /* dcom atom */
3769  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3770  return AVERROR_INVALIDDATA;
3771  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3772  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3773  return AVERROR_INVALIDDATA;
3774  }
3775  avio_rb32(pb); /* cmvd atom */
3776  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3777  return AVERROR_INVALIDDATA;
3778  moov_len = avio_rb32(pb); /* uncompressed size */
3779  cmov_len = atom.size - 6 * 4;
3780 
3781  cmov_data = av_malloc(cmov_len);
3782  if (!cmov_data)
3783  return AVERROR(ENOMEM);
3784  moov_data = av_malloc(moov_len);
3785  if (!moov_data) {
3786  av_free(cmov_data);
3787  return AVERROR(ENOMEM);
3788  }
3789  ret = ffio_read_size(pb, cmov_data, cmov_len);
3790  if (ret < 0)
3791  goto free_and_return;
3792 
3793  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3794  goto free_and_return;
3795  if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3796  goto free_and_return;
3798  atom.type = MKTAG('m','o','o','v');
3799  atom.size = moov_len;
3800  ret = mov_read_default(c, &ctx, atom);
3801 free_and_return:
3802  av_free(moov_data);
3803  av_free(cmov_data);
3804  return ret;
3805 #else
3806  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3807  return AVERROR(ENOSYS);
3808 #endif
3809 }
3810 
3811 /* edit list atom */
3813 {
3814  MOVStreamContext *sc;
3815  int i, edit_count, version;
3816 
3817  if (c->fc->nb_streams < 1 || c->ignore_editlist)
3818  return 0;
3819  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3820 
3821  version = avio_r8(pb); /* version */
3822  avio_rb24(pb); /* flags */
3823  edit_count = avio_rb32(pb); /* entries */
3824 
3825  if (!edit_count)
3826  return 0;
3827  if (sc->elst_data)
3828  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
3829  av_free(sc->elst_data);
3830  sc->elst_count = 0;
3831  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
3832  if (!sc->elst_data)
3833  return AVERROR(ENOMEM);
3834 
3835  av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3836  for (i = 0; i < edit_count && !pb->eof_reached; i++) {
3837  MOVElst *e = &sc->elst_data[i];
3838 
3839  if (version == 1) {
3840  e->duration = avio_rb64(pb);
3841  e->time = avio_rb64(pb);
3842  } else {
3843  e->duration = avio_rb32(pb); /* segment duration */
3844  e->time = (int32_t)avio_rb32(pb); /* media time */
3845  }
3846  e->rate = avio_rb32(pb) / 65536.0;
3847  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3848  e->duration, e->time, e->rate);
3849  }
3850  sc->elst_count = i;
3851 
3852  return 0;
3853 }
3854 
3856 {
3857  MOVStreamContext *sc;
3858 
3859  if (c->fc->nb_streams < 1)
3860  return AVERROR_INVALIDDATA;
3861  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3862  sc->timecode_track = avio_rb32(pb);
3863  return 0;
3864 }
3865 
3867 {
3868  int ret;
3869  uint8_t uuid[16];
3870  static const uint8_t uuid_isml_manifest[] = {
3871  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3872  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3873  };
3874 
3875  if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
3876  return AVERROR_INVALIDDATA;
3877 
3878  ret = avio_read(pb, uuid, sizeof(uuid));
3879  if (ret < 0) {
3880  return ret;
3881  } else if (ret != sizeof(uuid)) {
3882  return AVERROR_INVALIDDATA;
3883  }
3884  if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3885  uint8_t *buffer, *ptr;
3886  char *endptr;
3887  size_t len = atom.size - sizeof(uuid);
3888 
3889  if (len < 4) {
3890  return AVERROR_INVALIDDATA;
3891  }
3892  ret = avio_skip(pb, 4); // zeroes
3893  len -= 4;
3894 
3895  buffer = av_mallocz(len + 1);
3896  if (!buffer) {
3897  return AVERROR(ENOMEM);
3898  }
3899  ret = avio_read(pb, buffer, len);
3900  if (ret < 0) {
3901  av_free(buffer);
3902  return ret;
3903  } else if (ret != len) {
3904  av_free(buffer);
3905  return AVERROR_INVALIDDATA;
3906  }
3907 
3908  ptr = buffer;
3909  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
3910  ptr += sizeof("systemBitrate=\"") - 1;
3911  c->bitrates_count++;
3912  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3913  if (!c->bitrates) {
3914  c->bitrates_count = 0;
3915  av_free(buffer);
3916  return AVERROR(ENOMEM);
3917  }
3918  errno = 0;
3919  ret = strtol(ptr, &endptr, 10);
3920  if (ret < 0 || errno || *endptr != '"') {
3921  c->bitrates[c->bitrates_count - 1] = 0;
3922  } else {
3923  c->bitrates[c->bitrates_count - 1] = ret;
3924  }
3925  }
3926 
3927  av_free(buffer);
3928  }
3929  return 0;
3930 }
3931 
3933 {
3934  int ret;
3935  uint8_t content[16];
3936 
3937  if (atom.size < 8)
3938  return 0;
3939 
3940  ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
3941  if (ret < 0)
3942  return ret;
3943 
3944  if ( !c->found_moov
3945  && !c->found_mdat
3946  && !memcmp(content, "Anevia\x1A\x1A", 8)
3949  }
3950 
3951  return 0;
3952 }
3953 
3955 { MKTAG('A','C','L','R'), mov_read_aclr },
3956 { MKTAG('A','P','R','G'), mov_read_avid },
3957 { MKTAG('A','A','L','P'), mov_read_avid },
3958 { MKTAG('A','R','E','S'), mov_read_ares },
3959 { MKTAG('a','v','s','s'), mov_read_avss },
3960 { MKTAG('c','h','p','l'), mov_read_chpl },
3961 { MKTAG('c','o','6','4'), mov_read_stco },
3962 { MKTAG('c','o','l','r'), mov_read_colr },
3963 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3964 { MKTAG('d','i','n','f'), mov_read_default },
3965 { MKTAG('D','p','x','E'), mov_read_dpxe },
3966 { MKTAG('d','r','e','f'), mov_read_dref },
3967 { MKTAG('e','d','t','s'), mov_read_default },
3968 { MKTAG('e','l','s','t'), mov_read_elst },
3969 { MKTAG('e','n','d','a'), mov_read_enda },
3970 { MKTAG('f','i','e','l'), mov_read_fiel },
3971 { MKTAG('a','d','r','m'), mov_read_adrm },
3972 { MKTAG('f','t','y','p'), mov_read_ftyp },
3973 { MKTAG('g','l','b','l'), mov_read_glbl },
3974 { MKTAG('h','d','l','r'), mov_read_hdlr },
3975 { MKTAG('i','l','s','t'), mov_read_ilst },
3976 { MKTAG('j','p','2','h'), mov_read_jp2h },
3977 { MKTAG('m','d','a','t'), mov_read_mdat },
3978 { MKTAG('m','d','h','d'), mov_read_mdhd },
3979 { MKTAG('m','d','i','a'), mov_read_default },
3980 { MKTAG('m','e','t','a'), mov_read_meta },
3981 { MKTAG('m','i','n','f'), mov_read_default },
3982 { MKTAG('m','o','o','f'), mov_read_moof },
3983 { MKTAG('m','o','o','v'), mov_read_moov },
3984 { MKTAG('m','v','e','x'), mov_read_default },
3985 { MKTAG('m','v','h','d'), mov_read_mvhd },
3986 { MKTAG('S','M','I',' '), mov_read_svq3 },
3987 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3988 { MKTAG('a','v','c','C'), mov_read_glbl },
3989 { MKTAG('p','a','s','p'), mov_read_pasp },
3990 { MKTAG('s','i','d','x'), mov_read_sidx },
3991 { MKTAG('s','t','b','l'), mov_read_default },
3992 { MKTAG('s','t','c','o'), mov_read_stco },
3993 { MKTAG('s','t','p','s'), mov_read_stps },
3994 { MKTAG('s','t','r','f'), mov_read_strf },
3995 { MKTAG('s','t','s','c'), mov_read_stsc },
3996 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3997 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3998 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3999 { MKTAG('s','t','t','s'), mov_read_stts },
4000 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
4001 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
4002 { MKTAG('t','f','d','t'), mov_read_tfdt },
4003 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
4004 { MKTAG('t','r','a','k'), mov_read_trak },
4005 { MKTAG('t','r','a','f'), mov_read_default },
4006 { MKTAG('t','r','e','f'), mov_read_default },
4007 { MKTAG('t','m','c','d'), mov_read_tmcd },
4008 { MKTAG('c','h','a','p'), mov_read_chap },
4009 { MKTAG('t','r','e','x'), mov_read_trex },
4010 { MKTAG('t','r','u','n'), mov_read_trun },
4011 { MKTAG('u','d','t','a'), mov_read_default },
4012 { MKTAG('w','a','v','e'), mov_read_wave },
4013 { MKTAG('e','s','d','s'), mov_read_esds },
4014 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
4015 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
4016 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
4017 { MKTAG('w','f','e','x'), mov_read_wfex },
4018 { MKTAG('c','m','o','v'), mov_read_cmov },
4019 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
4020 { MKTAG('d','v','c','1'), mov_read_dvc1 },
4021 { MKTAG('s','b','g','p'), mov_read_sbgp },
4022 { MKTAG('h','v','c','C'), mov_read_glbl },
4023 { MKTAG('u','u','i','d'), mov_read_uuid },
4024 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
4025 { MKTAG('f','r','e','e'), mov_read_free },
4026 { MKTAG('-','-','-','-'), mov_read_custom },
4027 { 0, NULL }
4028 };
4029 
4031 {
4032  int64_t total_size = 0;
4033  MOVAtom a;
4034  int i;
4035 
4036  if (c->atom_depth > 10) {
4037  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
4038  return AVERROR_INVALIDDATA;
4039  }
4040  c->atom_depth ++;
4041 
4042  if (atom.size < 0)
4043  atom.size = INT64_MAX;
4044  while (total_size <= atom.size - 8 && !avio_feof(pb)) {
4045  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
4046  a.size = atom.size;
4047  a.type=0;
4048  if (atom.size >= 8) {
4049  a.size = avio_rb32(pb);
4050  a.type = avio_rl32(pb);
4051  if (a.type == MKTAG('f','r','e','e') &&
4052  a.size >= 8 &&
4053  c->moov_retry) {
4054  uint8_t buf[8];
4055  uint32_t *type = (uint32_t *)buf + 1;
4056  if (avio_read(pb, buf, 8) != 8)
4057  return AVERROR_INVALIDDATA;
4058  avio_seek(pb, -8, SEEK_CUR);
4059  if (*type == MKTAG('m','v','h','d') ||
4060  *type == MKTAG('c','m','o','v')) {
4061  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
4062  a.type = MKTAG('m','o','o','v');
4063  }
4064  }
4065  if (atom.type != MKTAG('r','o','o','t') &&
4066  atom.type != MKTAG('m','o','o','v'))
4067  {
4068  if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
4069  {
4070  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
4071  avio_skip(pb, -8);
4072  c->atom_depth --;
4073  return 0;
4074  }
4075  }
4076  total_size += 8;
4077  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
4078  a.size = avio_rb64(pb) - 8;
4079  total_size += 8;
4080  }
4081  }
4082  av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
4083  a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
4084  if (a.size == 0) {
4085  a.size = atom.size - total_size + 8;
4086  }
4087  a.size -= 8;
4088  if (a.size < 0)
4089  break;
4090  a.size = FFMIN(a.size, atom.size - total_size);
4091 
4092  for (i = 0; mov_default_parse_table[i].type; i++)
4093  if (mov_default_parse_table[i].type == a.type) {
4094  parse = mov_default_parse_table[i].parse;
4095  break;
4096  }
4097 
4098  // container is user data
4099  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
4100  atom.type == MKTAG('i','l','s','t')))
4102 
4103  if (!parse) { /* skip leaf atoms data */
4104  avio_skip(pb, a.size);
4105  } else {
4106  int64_t start_pos = avio_tell(pb);
4107  int64_t left;
4108  int err = parse(c, pb, a);
4109  if (err < 0) {
4110  c->atom_depth --;
4111  return err;
4112  }
4113  if (c->found_moov && c->found_mdat &&
4114  ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete) ||
4115  start_pos + a.size == avio_size(pb))) {
4116  if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete)
4117  c->next_root_atom = start_pos + a.size;
4118  c->atom_depth --;
4119  return 0;
4120  }
4121  left = a.size - avio_tell(pb) + start_pos;
4122  if (left > 0) /* skip garbage at atom end */
4123  avio_skip(pb, left);
4124  else if (left < 0) {
4125  av_log(c->fc, AV_LOG_WARNING,
4126  "overread end of atom '%.4s' by %"PRId64" bytes\n",
4127  (char*)&a.type, -left);
4128  avio_seek(pb, left, SEEK_CUR);
4129  }
4130  }
4131 
4132  total_size += a.size;
4133  }
4134 
4135  if (total_size < atom.size && atom.size < 0x7ffff)
4136  avio_skip(pb, atom.size - total_size);
4137 
4138  c->atom_depth --;
4139  return 0;
4140 }
4141 
4142 static int mov_probe(AVProbeData *p)
4143 {
4144  int64_t offset;
4145  uint32_t tag;
4146  int score = 0;
4147  int moov_offset = -1;
4148 
4149  /* check file header */
4150  offset = 0;
4151  for (;;) {
4152  /* ignore invalid offset */
4153  if ((offset + 8) > (unsigned int)p->buf_size)
4154  break;
4155  tag = AV_RL32(p->buf + offset + 4);
4156  switch(tag) {
4157  /* check for obvious tags */
4158  case MKTAG('m','o','o','v'):
4159  moov_offset = offset + 4;
4160  case MKTAG('m','d','a','t'):
4161  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
4162  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
4163  case MKTAG('f','t','y','p'):
4164  if (AV_RB32(p->buf+offset) < 8 &&
4165  (AV_RB32(p->buf+offset) != 1 ||
4166  offset + 12 > (unsigned int)p->buf_size ||
4167  AV_RB64(p->buf+offset + 8) == 0)) {
4168  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4169  } else if (tag == MKTAG('f','t','y','p') &&
4170  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
4171  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
4172  )) {
4173  score = FFMAX(score, 5);
4174  } else {
4175  score = AVPROBE_SCORE_MAX;
4176  }
4177  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4178  break;
4179  /* those are more common words, so rate then a bit less */
4180  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
4181  case MKTAG('w','i','d','e'):
4182  case MKTAG('f','r','e','e'):
4183  case MKTAG('j','u','n','k'):
4184  case MKTAG('p','i','c','t'):
4185  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
4186  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4187  break;
4188  case MKTAG(0x82,0x82,0x7f,0x7d):
4189  case MKTAG('s','k','i','p'):
4190  case MKTAG('u','u','i','d'):
4191  case MKTAG('p','r','f','l'):
4192  /* if we only find those cause probedata is too small at least rate them */
4193  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4194  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4195  break;
4196  default:
4197  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4198  }
4199  }
4200  if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
4201  /* moov atom in the header - we should make sure that this is not a
4202  * MOV-packed MPEG-PS */
4203  offset = moov_offset;
4204 
4205  while(offset < (p->buf_size - 16)){ /* Sufficient space */
4206  /* We found an actual hdlr atom */
4207  if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
4208  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
4209  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
4210  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
4211  /* We found a media handler reference atom describing an
4212  * MPEG-PS-in-MOV, return a
4213  * low score to force expanding the probe window until
4214  * mpegps_probe finds what it needs */
4215  return 5;
4216  }else
4217  /* Keep looking */
4218  offset+=2;
4219  }
4220  }
4221 
4222  return score;
4223 }
4224 
4225 // must be done after parsing all trak because there's no order requirement
4227 {
4228  MOVContext *mov = s->priv_data;
4229  AVStream *st = NULL;
4230  MOVStreamContext *sc;
4231  int64_t cur_pos;
4232  int i;
4233 
4234  for (i = 0; i < s->nb_streams; i++)
4235  if (s->streams[i]->id == mov->chapter_track) {
4236  st = s->streams[i];
4237  break;
4238  }
4239  if (!st) {
4240  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
4241  return;
4242  }
4243 
4244  st->discard = AVDISCARD_ALL;
4245  sc = st->priv_data;
4246  cur_pos = avio_tell(sc->pb);
4247 
4248  for (i = 0; i < st->nb_index_entries; i++) {
4249  AVIndexEntry *sample = &st->index_entries[i];
4250  int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
4251  uint8_t *title;
4252  uint16_t ch;
4253  int len, title_len;
4254 
4255  if (end < sample->timestamp) {
4256  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
4257  end = AV_NOPTS_VALUE;
4258  }
4259 
4260  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
4261  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
4262  goto finish;
4263  }
4264 
4265  // the first two bytes are the length of the title
4266  len = avio_rb16(sc->pb);
4267  if (len > sample->size-2)
4268  continue;
4269  title_len = 2*len + 1;
4270  if (!(title = av_mallocz(title_len)))
4271  goto finish;
4272 
4273  // The samples could theoretically be in any encoding if there's an encd
4274  // atom following, but in practice are only utf-8 or utf-16, distinguished
4275  // instead by the presence of a BOM
4276  if (!len) {
4277  title[0] = 0;
4278  } else {
4279  ch = avio_rb16(sc->pb);
4280  if (ch == 0xfeff)
4281  avio_get_str16be(sc->pb, len, title, title_len);
4282  else if (ch == 0xfffe)
4283  avio_get_str16le(sc->pb, len, title, title_len);
4284  else {
4285  AV_WB16(title, ch);
4286  if (len == 1 || len == 2)
4287  title[len] = 0;
4288  else
4289  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
4290  }
4291  }
4292 
4293  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
4294  av_freep(&title);
4295  }
4296 finish:
4297  avio_seek(sc->pb, cur_pos, SEEK_SET);
4298 }
4299 
4301  uint32_t value, int flags)
4302 {
4303  AVTimecode tc;
4304  char buf[AV_TIMECODE_STR_SIZE];
4305  AVRational rate = {st->codec->time_base.den,
4306  st->codec->time_base.num};
4307  int ret = av_timecode_init(&tc, rate, flags, 0, s);
4308  if (ret < 0)
4309  return ret;
4310  av_dict_set(&st->metadata, "timecode",
4311  av_timecode_make_string(&tc, buf, value), 0);
4312  return 0;
4313 }
4314 
4316 {
4317  MOVStreamContext *sc = st->priv_data;
4318  int flags = 0;
4319  int64_t cur_pos = avio_tell(sc->pb);
4320  uint32_t value;
4321 
4322  if (!st->nb_index_entries)
4323  return -1;
4324 
4325  avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
4326  value = avio_rb32(s->pb);
4327 
4328  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
4329  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
4330  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
4331 
4332  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
4333  * not the case) and thus assume "frame number format" instead of QT one.
4334  * No sample with tmcd track can be found with a QT timecode at the moment,
4335  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
4336  * format). */
4337  parse_timecode_in_framenum_format(s, st, value, flags);
4338 
4339  avio_seek(sc->pb, cur_pos, SEEK_SET);
4340  return 0;
4341 }
4342 
4344 {
4345  MOVContext *mov = s->priv_data;
4346  int i, j;
4347 
4348  for (i = 0; i < s->nb_streams; i++) {
4349  AVStream *st = s->streams[i];
4350  MOVStreamContext *sc = st->priv_data;
4351 
4352  if (!sc)
4353  continue;
4354 
4355  av_freep(&sc->ctts_data);
4356  for (j = 0; j < sc->drefs_count; j++) {
4357  av_freep(&sc->drefs[j].path);
4358  av_freep(&sc->drefs[j].dir);
4359  }
4360  av_freep(&sc->drefs);
4361 
4362  sc->drefs_count = 0;
4363 
4364  if (!sc->pb_is_copied)
4365  avio_closep(&sc->pb);
4366 
4367  sc->pb = NULL;
4368  av_freep(&sc->chunk_offsets);
4369  av_freep(&sc->stsc_data);
4370  av_freep(&sc->sample_sizes);
4371  av_freep(&sc->keyframes);
4372  av_freep(&sc->stts_data);
4373  av_freep(&sc->stps_data);
4374  av_freep(&sc->elst_data);
4375  av_freep(&sc->rap_group);
4376  av_freep(&sc->display_matrix);
4377  }
4378 
4379  av_freep(&mov->dv_demux);
4381  mov->dv_fctx = NULL;
4382 
4383  av_freep(&mov->trex_data);
4384  av_freep(&mov->bitrates);
4385 
4386  for (i = 0; i < mov->fragment_index_count; i++) {
4388  av_freep(&index->items);
4389  av_freep(&mov->fragment_index_data[i]);
4390  }
4392 
4393  av_freep(&mov->aes_decrypt);
4394 
4395  return 0;
4396 }
4397 
4398 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
4399 {
4400  int i;
4401 
4402  for (i = 0; i < s->nb_streams; i++) {
4403  AVStream *st = s->streams[i];
4404  MOVStreamContext *sc = st->priv_data;
4405 
4406  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
4407  sc->timecode_track == tmcd_id)
4408  return 1;
4409  }
4410  return 0;
4411 }
4412 
4413 /* look for a tmcd track not referenced by any video track, and export it globally */
4415 {
4416  int i;
4417 
4418  for (i = 0; i < s->nb_streams; i++) {
4419  AVStream *st = s->streams[i];
4420 
4421  if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
4422  !tmcd_is_referenced(s, i + 1)) {
4423  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
4424  if (tcr) {
4425  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
4426  break;
4427  }
4428  }
4429  }
4430 }
4431 
4432 static int read_tfra(MOVContext *mov, AVIOContext *f)
4433 {
4435  int version, fieldlength, i, j;
4436  int64_t pos = avio_tell(f);
4437  uint32_t size = avio_rb32(f);
4438  void *tmp;
4439 
4440  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
4441  return 1;
4442  }
4443  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
4444  index = av_mallocz(sizeof(MOVFragmentIndex));
4445  if (!index) {
4446  return AVERROR(ENOMEM);
4447  }
4448 
4450  mov->fragment_index_count + 1,
4451  sizeof(MOVFragmentIndex*));
4452  if (!tmp) {
4453  av_freep(&index);
4454  return AVERROR(ENOMEM);
4455  }
4456  mov->fragment_index_data = tmp;
4458 
4459  version = avio_r8(f);
4460  avio_rb24(f);
4461  index->track_id = avio_rb32(f);
4462  fieldlength = avio_rb32(f);
4463  index->item_count = avio_rb32(f);
4464  index->items = av_mallocz_array(
4465  index->item_count, sizeof(MOVFragmentIndexItem));
4466  if (!index->items) {
4467  index->item_count = 0;
4468  return AVERROR(ENOMEM);
4469  }
4470  for (i = 0; i < index->item_count; i++) {
4471  int64_t time, offset;
4472 
4473  if (avio_feof(f)) {
4474  index->item_count = 0;
4475  av_freep(&index->items);
4476  return AVERROR_INVALIDDATA;
4477  }
4478 
4479  if (version == 1) {
4480  time = avio_rb64(f);
4481  offset = avio_rb64(f);
4482  } else {
4483  time = avio_rb32(f);
4484  offset = avio_rb32(f);
4485  }
4486  index->items[i].time = time;
4487  index->items[i].moof_offset = offset;
4488  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
4489  avio_r8(f);
4490  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
4491  avio_r8(f);
4492  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
4493  avio_r8(f);
4494  }
4495 
4496  avio_seek(f, pos + size, SEEK_SET);
4497  return 0;
4498 }
4499 
4501 {
4502  int64_t stream_size = avio_size(f);
4503  int64_t original_pos = avio_tell(f);
4504  int64_t seek_ret;
4505  int32_t mfra_size;
4506  int ret = -1;
4507  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
4508  ret = seek_ret;
4509  goto fail;
4510  }
4511  mfra_size = avio_rb32(f);
4512  if (mfra_size < 0 || mfra_size > stream_size) {
4513  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
4514  goto fail;
4515  }
4516  if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
4517  ret = seek_ret;
4518  goto fail;
4519  }
4520  if (avio_rb32(f) != mfra_size) {
4521  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
4522  goto fail;
4523  }
4524  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
4525  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
4526  goto fail;
4527  }
4528  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
4529  do {
4530  ret = read_tfra(c, f);
4531  if (ret < 0)
4532  goto fail;
4533  } while (!ret);
4534  ret = 0;
4535 fail:
4536  seek_ret = avio_seek(f, original_pos, SEEK_SET);
4537  if (seek_ret < 0) {
4538  av_log(c->fc, AV_LOG_ERROR,
4539  "failed to seek back after looking for mfra\n");
4540  ret = seek_ret;
4541  }
4542  return ret;
4543 }
4544 
4546 {
4547  MOVContext *mov = s->priv_data;
4548  AVIOContext *pb = s->pb;
4549  int j, err;
4550  MOVAtom atom = { AV_RL32("root") };
4551  int i;
4552 
4553  mov->fc = s;
4554  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
4555  if (pb->seekable)
4556  atom.size = avio_size(pb);
4557  else
4558  atom.size = INT64_MAX;
4559 
4560  /* check MOV header */
4561  do {
4562  if (mov->moov_retry)
4563  avio_seek(pb, 0, SEEK_SET);
4564  if ((err = mov_read_default(mov, pb, atom)) < 0) {
4565  av_log(s, AV_LOG_ERROR, "error reading header\n");
4566  mov_read_close(s);
4567  return err;
4568  }
4569  } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
4570  if (!mov->found_moov) {
4571  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
4572  mov_read_close(s);
4573  return AVERROR_INVALIDDATA;
4574  }
4575  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
4576 
4577  if (pb->seekable) {
4578  if (mov->chapter_track > 0)
4579  mov_read_chapters(s);
4580  for (i = 0; i < s->nb_streams; i++)
4581  if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
4582  mov_read_timecode_track(s, s->streams[i]);
4583  }
4584 
4585  /* copy timecode metadata from tmcd tracks to the related video streams */
4586  for (i = 0; i < s->nb_streams; i++) {
4587  AVStream *st = s->streams[i];
4588  MOVStreamContext *sc = st->priv_data;
4589  if (sc->timecode_track > 0) {
4590  AVDictionaryEntry *tcr;
4591  int tmcd_st_id = -1;
4592 
4593  for (j = 0; j < s->nb_streams; j++)
4594  if (s->streams[j]->id == sc->timecode_track)
4595  tmcd_st_id = j;
4596 
4597  if (tmcd_st_id < 0 || tmcd_st_id == i)
4598  continue;
4599  tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
4600  if (tcr)
4601  av_dict_set(&st->metadata, "timecode", tcr->value, 0);
4602  }
4603  }
4605 
4606  for (i = 0; i < s->nb_streams; i++) {
4607  AVStream *st = s->streams[i];
4608  MOVStreamContext *sc = st->priv_data;
4609  fix_timescale(mov, sc);
4611  st->skip_samples = sc->start_pad;
4612  }
4613  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
4615  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
4616  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4617  if (st->codec->width <= 0 || st->codec->height <= 0) {
4618  st->codec->width = sc->width;
4619  st->codec->height = sc->height;
4620  }
4621  if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
4622  if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
4623  return err;
4624  }
4625  }
4626  if (mov->handbrake_version &&
4627  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
4629  ) {
4630  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
4632  }
4633  }
4634 
4635  if (mov->trex_data) {
4636  for (i = 0; i < s->nb_streams; i++) {
4637  AVStream *st = s->streams[i];
4638  MOVStreamContext *sc = st->priv_data;
4639  if (st->duration > 0)
4640  st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
4641  }
4642  }
4643 
4644  if (mov->use_mfra_for > 0) {
4645  for (i = 0; i < s->nb_streams; i++) {
4646  AVStream *st = s->streams[i];
4647  MOVStreamContext *sc = st->priv_data;
4648  if (sc->duration_for_fps > 0) {
4649  st->codec->bit_rate = sc->data_size * 8 * sc->time_scale /
4650  sc->duration_for_fps;
4651  }
4652  }
4653  }
4654 
4655  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
4656  if (mov->bitrates[i]) {
4657  s->streams[i]->codec->bit_rate = mov->bitrates[i];
4658  }
4659  }
4660 
4661  ff_rfps_calculate(s);
4662 
4663  for (i = 0; i < s->nb_streams; i++) {
4664  AVStream *st = s->streams[i];
4665  MOVStreamContext *sc = st->priv_data;
4666 
4667  switch (st->codec->codec_type) {
4668  case AVMEDIA_TYPE_AUDIO:
4669  err = ff_replaygain_export(st, s->metadata);
4670  if (err < 0) {
4671  mov_read_close(s);
4672  return err;
4673  }
4674  break;
4675  case AVMEDIA_TYPE_VIDEO:
4676  if (sc->display_matrix) {
4677  AVPacketSideData *sd, *tmp;
4678 
4679  tmp = av_realloc_array(st->side_data,
4680  st->nb_side_data + 1, sizeof(*tmp));
4681  if (!tmp)
4682  return AVERROR(ENOMEM);
4683 
4684  st->side_data = tmp;
4685  st->nb_side_data++;
4686 
4687  sd = &st->side_data[st->nb_side_data - 1];
4689  sd->size = sizeof(int32_t) * 9;
4690  sd->data = (uint8_t*)sc->display_matrix;
4691  sc->display_matrix = NULL;
4692  }
4693  break;
4694  }
4695  }
4697 
4698  return 0;
4699 }
4700 
4702 {
4704  int64_t best_dts = INT64_MAX;
4705  int i;
4706  for (i = 0; i < s->nb_streams; i++) {
4707  AVStream *avst = s->streams[i];
4708  MOVStreamContext *msc = avst->priv_data;
4709  if (msc->pb && msc->current_sample < avst->nb_index_entries) {
4710  AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
4711  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
4712  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
4713  if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
4714  (s->pb->seekable &&
4715  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
4716  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
4717  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
4718  sample = current_sample;
4719  best_dts = dts;
4720  *st = avst;
4721  }
4722  }
4723  }
4724  return sample;
4725 }
4726 
4727 static int should_retry(AVIOContext *pb, int error_code) {
4728  if (error_code == AVERROR_EOF || avio_feof(pb))
4729  return 0;
4730 
4731  return 1;
4732 }
4733 
4734 static int mov_switch_root(AVFormatContext *s, int64_t target)
4735 {
4736  int ret;
4737  MOVContext *mov = s->priv_data;
4738  int i, j;
4739  int already_read = 0;
4740 
4741  if (avio_seek(s->pb, target, SEEK_SET) != target) {
4742  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
4743  return AVERROR_INVALIDDATA;
4744  }
4745 
4746  mov->next_root_atom = 0;
4747 
4748  for (i = 0; i < mov->fragment_index_count; i++) {
4750  int found = 0;
4751  for (j = 0; j < index->item_count; j++) {
4752  MOVFragmentIndexItem *item = &index->items[j];
4753  if (found) {
4754  mov->next_root_atom = item->moof_offset;
4755  break; // Advance to next index in outer loop
4756  } else if (item->moof_offset == target) {
4757  index->current_item = FFMIN(j, index->current_item);
4758  if (item->headers_read)
4759  already_read = 1;
4760  item->headers_read = 1;
4761  found = 1;
4762  }
4763  }
4764  if (!found)
4765  index->current_item = 0;
4766  }
4767 
4768  if (already_read)
4769  return 0;
4770 
4771  mov->found_mdat = 0;
4772 
4773  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
4774  if (ret < 0)
4775  return ret;
4776  if (avio_feof(s->pb))
4777  return AVERROR_EOF;
4778  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
4779 
4780  return 1;
4781 }
4782 
4784 {
4785  MOVContext *mov = s->priv_data;
4786  MOVStreamContext *sc;
4788  AVStream *st = NULL;
4789  int ret;
4790  mov->fc = s;
4791  retry:
4792  sample = mov_find_next_sample(s, &st);
4793  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
4794  if (!mov->next_root_atom)
4795  return AVERROR_EOF;
4796  if ((ret = mov_switch_root(s, mov->next_root_atom)) < 0)
4797  return ret;
4798  goto retry;
4799  }
4800  sc = st->priv_data;
4801  /* must be done just before reading, to avoid infinite loop on sample */
4802  sc->current_sample++;
4803 
4804  if (mov->next_root_atom) {
4805  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
4806  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
4807  }
4808 
4809  if (st->discard != AVDISCARD_ALL) {
4810  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
4811  if (ret64 != sample->pos) {
4812  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
4813  sc->ffindex, sample->pos);
4814  sc->current_sample -= should_retry(sc->pb, ret64);
4815  return AVERROR_INVALIDDATA;
4816  }
4817  ret = av_get_packet(sc->pb, pkt, sample->size);
4818  if (ret < 0) {
4819  sc->current_sample -= should_retry(sc->pb, ret);
4820  return ret;
4821  }
4822 #if CONFIG_DV_DEMUXER
4823  if (mov->dv_demux && sc->dv_audio_container) {
4824  AVBufferRef *buf = pkt->buf;
4825  ret = avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
4826  pkt->buf = buf;
4827  av_packet_unref(pkt);
4828  if (ret < 0)
4829  return ret;
4830  ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
4831  if (ret < 0)
4832  return ret;
4833  }
4834 #endif
4835  if (sc->has_palette) {
4836  uint8_t *pal;
4837 
4839  if (!pal) {
4840  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
4841  } else {
4842  memcpy(pal, sc->palette, AVPALETTE_SIZE);
4843  sc->has_palette = 0;
4844  }
4845  }
4846  }
4847 
4848  pkt->stream_index = sc->ffindex;
4849  pkt->dts = sample->timestamp;
4850  if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
4851  pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
4852  /* update ctts context */
4853  sc->ctts_sample++;
4854  if (sc->ctts_index < sc->ctts_count &&
4855  sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
4856  sc->ctts_index++;
4857  sc->ctts_sample = 0;
4858  }
4859  if (sc->wrong_dts)
4860  pkt->dts = AV_NOPTS_VALUE;
4861  } else {
4862  int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
4864 
4865  if (next_dts >= pkt->dts)
4866  pkt->duration = next_dts - pkt->dts;
4867  pkt->pts = pkt->dts;
4868  }
4869  if (st->discard == AVDISCARD_ALL)
4870  goto retry;
4871  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
4872  pkt->pos = sample->pos;
4873 
4874  if (mov->aax_mode)
4875  aax_filter(pkt->data, pkt->size, mov);
4876 
4877  return 0;
4878 }
4879 
4880 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
4881 {
4882  MOVContext *mov = s->priv_data;
4883  int i, j;
4884 
4885  if (!mov->fragment_index_complete)
4886  return 0;
4887 
4888  for (i = 0; i < mov->fragment_index_count; i++) {
4889  if (mov->fragment_index_data[i]->track_id == st->id) {
4890  MOVFragmentIndex *index = index = mov->fragment_index_data[i];
4891  for (j = index->item_count - 1; j >= 0; j--) {
4892  if (index->items[j].time <= timestamp) {
4893  if (index->items[j].headers_read)
4894  return 0;
4895 
4896  return mov_switch_root(s, index->items[j].moof_offset);
4897  }
4898  }
4899  }
4900  }
4901 
4902  return 0;
4903 }
4904 
4905 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
4906 {
4907  MOVStreamContext *sc = st->priv_data;
4908  int sample, time_sample;
4909  int i;
4910 
4911  int ret = mov_seek_fragment(s, st, timestamp);
4912  if (ret < 0)
4913  return ret;
4914 
4915  sample = av_index_search_timestamp(st, timestamp, flags);
4916  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
4917  if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
4918  sample = 0;
4919  if (sample < 0) /* not sure what to do */
4920  return AVERROR_INVALIDDATA;
4921  sc->current_sample = sample;
4922  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
4923  /* adjust ctts index */
4924  if (sc->ctts_data) {
4925  time_sample = 0;
4926  for (i = 0; i < sc->ctts_count; i++) {
4927  int next = time_sample + sc->ctts_data[i].count;
4928  if (next > sc->current_sample) {
4929  sc->ctts_index = i;
4930  sc->ctts_sample = sc->current_sample - time_sample;
4931  break;
4932  }
4933  time_sample = next;
4934  }
4935  }
4936  return sample;
4937 }
4938 
4939 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
4940 {
4941  MOVContext *mc = s->priv_data;
4942  AVStream *st;
4943  int sample;
4944  int i;
4945 
4946  if (stream_index >= s->nb_streams)
4947  return AVERROR_INVALIDDATA;
4948 
4949  st = s->streams[stream_index];
4950  sample = mov_seek_stream(s, st, sample_time, flags);
4951  if (sample < 0)
4952  return sample;
4953 
4954  if (mc->seek_individually) {
4955  /* adjust seek timestamp to found sample timestamp */
4956  int64_t seek_timestamp = st->index_entries[sample].timestamp;
4957 
4958  for (i = 0; i < s->nb_streams; i++) {
4959  int64_t timestamp;
4960  MOVStreamContext *sc = s->streams[i]->priv_data;
4961  st = s->streams[i];
4962  st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
4963 
4964  if (stream_index == i)
4965  continue;
4966 
4967  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
4968  mov_seek_stream(s, st, timestamp, flags);
4969  }
4970  } else {
4971  for (i = 0; i < s->nb_streams; i++) {
4972  MOVStreamContext *sc;
4973  st = s->streams[i];
4974  sc = st->priv_data;
4975  sc->current_sample = 0;
4976  }
4977  while (1) {
4978  MOVStreamContext *sc;
4979  AVIndexEntry *entry = mov_find_next_sample(s, &st);
4980  if (!entry)
4981  return AVERROR_INVALIDDATA;
4982  sc = st->priv_data;
4983  if (sc->ffindex == stream_index && sc->current_sample == sample)
4984  break;
4985  sc->current_sample++;
4986  }
4987  }
4988  return 0;
4989 }
4990 
4991 #define OFFSET(x) offsetof(MOVContext, x)
4992 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
4993 static const AVOption mov_options[] = {
4994  {"use_absolute_path",
4995  "allow using absolute path when opening alias, this is a possible security issue",
4996  OFFSET(use_absolute_path), AV_OPT_TYPE_INT, {.i64 = 0},
4997  0, 1, FLAGS},
4998  {"seek_streams_individually",
4999  "Seek each stream individually to the to the closest point",
5000  OFFSET(seek_individually), AV_OPT_TYPE_INT, { .i64 = 1 },
5001  0, 1, FLAGS},
5002  {"ignore_editlist", "", OFFSET(ignore_editlist), AV_OPT_TYPE_INT, {.i64 = 0},
5003  0, 1, FLAGS},
5004  {"use_mfra_for",
5005  "use mfra for fragment timestamps",
5006  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
5008  "use_mfra_for"},
5009  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
5010  FLAGS, "use_mfra_for" },
5011  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
5012  FLAGS, "use_mfra_for" },
5013  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
5014  FLAGS, "use_mfra_for" },
5015  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
5016  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
5017  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
5018  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
5019  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
5021  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
5022  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
5023  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
5024  .flags = AV_OPT_FLAG_DECODING_PARAM },
5025  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_INT,
5026  {.i64 = 0}, 0, 1, FLAGS },
5027  { NULL },
5028 };
5029 
5030 static const AVClass mov_class = {
5031  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
5032  .item_name = av_default_item_name,
5033  .option = mov_options,
5034  .version = LIBAVUTIL_VERSION_INT,
5035 };
5036 
5038  .name = "mov,mp4,m4a,3gp,3g2,mj2",
5039  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
5040  .priv_class = &mov_class,
5041  .priv_data_size = sizeof(MOVContext),
5042  .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
5043  .read_probe = mov_probe,
5049 };
int chapter_track
Definition: isom.h:189
static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1350
int itunes_metadata
metadata are itunes style
Definition: isom.h:187
int * bitrates
bitrates read before streams creation
Definition: isom.h:196
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:523
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:992
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:3978
int64_t moof_offset
Definition: isom.h:105
#define AV_RB8(x)
Definition: intreadwrite.h:395
#define NULL
Definition: coverity.c:32
#define DRM_BLOB_SIZE
Definition: mov.c:821
const char const char void * val
Definition: avisynth_c.h:634
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:682
const char * s
Definition: avisynth_c.h:631
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1120
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:284
#define MP4ESDescrTag
Definition: isom.h:223
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:719
#define av_realloc_f(p, o, n)
uint32_t tmcd_flags
tmcd track flags
Definition: isom.h:162
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:314
unsigned int rap_group_count
Definition: isom.h:165
int found_tfhd
Definition: isom.h:79
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition: isom.c:426
unsigned int elst_count
Definition: isom.h:133
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1535
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3739
AVOption.
Definition: opt.h:255
hash context
Definition: sha.c:34
MOVTrackExt * trex_data
Definition: isom.h:185
int export_all
Definition: isom.h:194
"Linear transfer characteristics"
Definition: pixfmt.h:507
unsigned track_id
Definition: isom.h:80
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1749
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:230
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:4701
enum AVCodecID id
Definition: mxfenc.c:102
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:437
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2783
unsigned int samples_per_frame
Definition: isom.h:147
static void mov_build_index(MOVContext *mov, AVStream *st)
Definition: mov.c:2618
int dv_audio_container
Definition: isom.h:148
Definition: isom.h:45
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
float rate
Definition: isom.h:59
uint64_t base_data_offset
Definition: isom.h:81
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int64_t duration
Definition: isom.h:57
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1458
#define AV_RB64
Definition: intreadwrite.h:164
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4098
const char * g
Definition: vf_curves.c:108
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:590
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
int64_t pos
Definition: avformat.h:796
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2962
unsigned int stsc_count
Definition: isom.h:128
int has_palette
Definition: isom.h:160
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:527
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:114
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2247
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3022
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:926
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:855
unsigned item_count
Definition: isom.h:112
int size
Definition: avcodec.h:1434
const char * b
Definition: vf_curves.c:109
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:241
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AVIO_FLAG_READ
read-only
Definition: avio.h:485
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: aes.c:143
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1057
static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
Definition: mov.c:4398
void * activation_bytes
Definition: isom.h:208
#define tc
Definition: regdef.h:69
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1572
static const uint8_t ff_qt_default_palette_256[256 *3]
Definition: qtpalette.h:54
void * priv_data
Definition: avformat.h:874
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:279
static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
Definition: mov.c:3144
int version
Definition: avisynth_c.h:629
int16_t audio_cid
stsd audio compression id
Definition: isom.h:150
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition: common.h:413
discard all
Definition: avcodec.h:689
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:966
static AVPacket pkt
int height
tkhd height
Definition: isom.h:157
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:679
static AVRational av_make_q(int num, int den)
Create a rational.
Definition: rational.h:53
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:258
#define sample
uint32_t type
Definition: isom.h:72
static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3866
MOVElst * elst_data
Definition: isom.h:132
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2309
MOVStsc * stsc_data
Definition: isom.h:129
enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
Compute codec id for 'lpcm' tag.
Definition: mov.c:1624
unsigned fragment_index_count
Definition: isom.h:202
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1078
int ctts_index
Definition: isom.h:134
unsigned stsd_id
Definition: isom.h:93
int(* open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Called to open further IO contexts when needed for demuxing.
Definition: avformat.h:1833
int found_moov
'moov' atom has been found
Definition: isom.h:179
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2265
static void export_orphan_timecode(AVFormatContext *s)
Definition: mov.c:4414
static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1382
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:234
Macro definitions for various function/variable attributes.
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:4727
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1641
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:75
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
unsigned flags
Definition: isom.h:96
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:253
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:3860
int isom
1 if file is ISO Media (mp4/3gp)
Definition: isom.h:183
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:2347
int found_mdat
'mdat' atom has been found
Definition: isom.h:180
int width
tkhd width
Definition: isom.h:156
unsigned drefs_count
Definition: isom.h:151
Format I/O context.
Definition: avformat.h:1285
static int mov_probe(AVProbeData *p)
Definition: mov.c:4142
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:4545
char filename[64]
Definition: isom.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1398
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: vp3_parser.c:23
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1950
Public dictionary API.
MOVDref * drefs
Definition: isom.h:152
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:97
int fragment_index_complete
Definition: isom.h:203
enum AVColorTransferCharacteristic color_trc
Definition: dirac.c:103
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:974
static const AVOption mov_options[]
Definition: mov.c:4993
static int64_t start_time
Definition: ffplay.c:325
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
int first
Definition: isom.h:51
uint8_t
#define FF_MOV_FLAG_MFRA_DTS
Definition: isom.h:282
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:195
AVOptions.
static av_always_inline av_const int isnan(float x)
Definition: libm.h:96
unsigned int sample_count
Definition: isom.h:138
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:71
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:694
int count
Definition: isom.h:52
int dts_shift
dts shift when ctts is negative
Definition: isom.h:158
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
timecode is drop frame
Definition: timecode.h:36
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1279
int id
Format-specific stream ID.
Definition: avformat.h:861
enum AVStreamParseType need_parsing
Definition: avformat.h:1046
AVInputFormat ff_mov_demuxer
Definition: mov.c:5037
enum AVColorRange color_range
Definition: dirac.c:86
unsigned int count
Definition: isom.h:100
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3759
int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
Definition: mov.c:660
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:970
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3761
int16_t nlvl_to
Definition: isom.h:68
#define AV_CH_LOW_FREQUENCY
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
char volume[28]
Definition: isom.h:66
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:105
static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:774
uint8_t * ff_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, int size)
Add new side data to a stream.
Definition: utils.c:4589
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, int codec_tag, int format, int64_t size)
Definition: mov.c:2084
int activation_bytes_size
Definition: isom.h:209
MOVFragmentIndex ** fragment_index_data
Definition: isom.h:201
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
#define ALAC_EXTRADATA_SIZE
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1396
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:328
uint8_t * data
Definition: avcodec.h:1433
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:239
uint32_t tag
Definition: movenc.c:1339
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: utils.c:1791
int nb_frames_for_fps
Definition: isom.h:168
#define ff_dlog(a,...)
#define AVERROR_EOF
End of file.
Definition: error.h:55
bitstream reader API header.
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
uint8_t * data
Definition: avcodec.h:1383
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:244
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:599
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:761
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3006
static int64_t duration
Definition: ffplay.c:326
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
static int test_same_origin(const char *src, const char *ref)
Definition: mov.c:2902
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1443
#define av_log(a,...)
int current_sample
Definition: isom.h:145
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:231
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
unsigned track_id
Definition: isom.h:92
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
void ff_rfps_calculate(AVFormatContext *ic)
Definition: utils.c:3038
int64_t time_offset
time offset of the edit list entries
Definition: isom.h:144
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3214
unsigned int keyframe_count
Definition: isom.h:141
static const uint8_t ff_qt_default_palette_4[4 *3]
Definition: qtpalette.h:28
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition: timecode.c:182
const uint16_t avpriv_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3tab.c:89
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1225
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2187
#define U(x)
Definition: vp56_arith.h:37
Callback for checking whether to abort blocking functions.
Definition: avio.h:50
static int mov_rewrite_dvd_sub_extradata(AVStream *st)
Definition: mov.c:1915
#define AVINDEX_KEYFRAME
Definition: avformat.h:803
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb)
Definition: isom.c:451
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate or reallocate an array through a pointer to a pointer.
Definition: mem.c:215
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1885
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:71
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1822
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3414
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1497
unsigned int ctts_count
Definition: isom.h:126
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1850
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:464
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:789
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3812
AVAudioServiceType
Definition: avcodec.h:692
av_default_item_name
#define AV_RB16
Definition: intreadwrite.h:53
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:663
#define AVERROR(e)
Definition: error.h:43
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2519
int atom_depth
Definition: isom.h:204
static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1425
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1430
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:797
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, AVCodecContext *codec, uint8_t *buf)
Definition: mov.c:1274
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int * keyframes
Definition: isom.h:142
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:425
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1416
AVFormatContext * fc
Definition: isom.h:176
enum AVColorPrimaries color_primaries
Definition: dirac.c:101
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:482
static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3500
enum AVPacketSideDataType type
Definition: avcodec.h:1385
#define FFNABS(a)
Negative Absolute value.
Definition: common.h:77
GLsizei GLsizei * length
Definition: opengl_enc.c:115
int handbrake_version
Definition: isom.h:188
int ctts_sample
Definition: isom.h:135
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:45
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet. ...
Definition: avformat.h:1102
enum AVCodecID codec_id
Definition: mov_chan.c:433
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
int keyframe_absent
Definition: isom.h:140
static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3641
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:937
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:378
#define FFMAX(a, b)
Definition: common.h:90
int16_t nlvl_from
Definition: isom.h:68
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:251
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:806
#define fail()
Definition: checkasm.h:57
static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
Definition: mov.c:1017
void * audible_fixed_key
Definition: isom.h:210
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:322
static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:823
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:472
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2333
Only parse headers, do not repack.
Definition: avformat.h:787
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:2111
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:533
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
static float distance(float x, float y, int band)
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3260
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4030
static int aax_filter(uint8_t *input, int size, MOVContext *c)
Definition: mov.c:923
static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1152
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int64_t time
Definition: isom.h:106
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1341
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1033
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:4438
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:4905
uint8_t file_key[20]
Definition: isom.h:206
static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:71
struct AVAES * aes_decrypt
Definition: isom.h:212
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2437
static const uint16_t fc[]
Definition: dcaenc.h:41
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1577
audio channel layout utility functions
static int mov_realloc_extradata(AVCodecContext *codec, MOVAtom atom)
Definition: mov.c:1259
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:687
char filename[1024]
input or output filename
Definition: avformat.h:1361
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3438
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:134
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
#define FFMIN(a, b)
Definition: common.h:92
float y
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:937
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:78
static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3855
colour filters using Illuminant C
Definition: pixfmt.h:490
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
char * dir
Definition: isom.h:65
int width
picture width / height.
Definition: avcodec.h:1691
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int id
Definition: isom.h:53
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:531
static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1332
#define FLAGS
Definition: mov.c:4992
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:4315
int audible_fixed_key_size
Definition: isom.h:211
int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: aviobuf.c:939
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3370
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1364
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2226
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3470
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1573
char * path
Definition: isom.h:64
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int time_scale
Definition: isom.h:143
int64_t time
Definition: isom.h:88
MOVFragmentIndexItem * items
Definition: isom.h:114
#define AV_RL32
Definition: intreadwrite.h:146
uint64_t moof_offset
Definition: isom.h:82
MOVStts * ctts_data
Definition: isom.h:127
#define OFFSET(x)
Definition: mov.c:4991
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:238
AVDictionary * metadata
Definition: avformat.h:928
unsigned size
Definition: isom.h:95
int ignore_editlist
Definition: isom.h:191
int64_t * chunk_offsets
Definition: isom.h:123
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:4783
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:4343
unsigned int index
Definition: isom.h:101
static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
This function reads atom content and puts data in extradata without tag nor size unlike mov_read_extr...
Definition: mov.c:1488
static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref, AVIOInterruptCB *int_cb)
Definition: mov.c:2931
int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size)
Read 'chan' tag from the input stream.
Definition: mov_chan.c:547
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:831
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:283
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:544
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
#define mc
unsigned current_item
Definition: isom.h:113
int64_t duration
duration of the longest track
Definition: isom.h:178
int ff_mov_lang_to_iso639(unsigned code, char to[4])
Definition: isom.c:382
Stream structure.
Definition: avformat.h:854
static const ColorEntry color_table[]
Definition: parseutils.c:198
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:167
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:194
unsigned duration
Definition: isom.h:94
DVDemuxContext * dv_demux
Definition: isom.h:181
also ITU-R BT1361
Definition: pixfmt.h:500
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:37
int timecode_track
Definition: isom.h:154
int * sample_sizes
Definition: isom.h:139
#define AV_CODEC_FLAG2_DROP_FRAME_TIMECODE
timecode is in drop frame format.
Definition: avcodec.h:816
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:4939
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts)
add frame for rfps calculation.
Definition: utils.c:2978
static const uint32_t mac_to_unicode[128]
Definition: mov.c:130
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:809
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:473
#define av_bswap32
Definition: bswap.h:33
AVS_Value src
Definition: avisynth_c.h:482
#define SQR(a)
enum AVMediaType codec_type
Definition: avcodec.h:1520
unsigned duration
Definition: isom.h:85
#define MP4DecConfigDescrTag
Definition: isom.h:224
MOVSbgp * rap_group
Definition: isom.h:166
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:368
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:268
functionally identical to above
Definition: pixfmt.h:489
int duration
Definition: isom.h:47
enum AVCodecID codec_id
Definition: avcodec.h:1529
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:4226
Timecode helpers header.
int sample_rate
samples per second
Definition: avcodec.h:2272
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1667
unsigned int stsz_sample_size
always contains sample size from stsz atom
Definition: isom.h:137
static int read_tfra(MOVContext *mov, AVIOContext *f)
Definition: mov.c:4432
main external API structure.
Definition: avcodec.h:1512
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:687
unsigned trex_count
Definition: isom.h:186
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:237
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:562
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3135
timecode wraps after 24 hours
Definition: timecode.h:37
int enable_drefs
Definition: isom.h:213
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1544
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
int64_t data_size
Definition: isom.h:161
static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3932
int has_looked_for_mfra
Definition: isom.h:200
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1628
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
uint32_t type
Definition: isom.h:63
int nb_index_entries
Definition: avformat.h:1059
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
MOVStts * stts_data
Definition: isom.h:125
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int count
Definition: isom.h:46
int index
Definition: gxfenc.c:89
enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
Select a PCM codec based on the given parameters.
Definition: utils.c:2795
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2240
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2233
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:205
static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1337
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:286
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition: pixfmt.h:509
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:184
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:415
AVFieldOrder
Definition: avcodec.h:1494
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:163
Definition: isom.h:71
static const uint8_t ff_qt_default_palette_16[16 *3]
Definition: qtpalette.h:35
int use_mfra_for
Definition: isom.h:199
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1783
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:149
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:232
#define snprintf
Definition: snprintf.h:34
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3698
#define FF_MOV_FLAG_MFRA_AUTO
Definition: isom.h:281
int64_t time
Definition: isom.h:58
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int size, int big_endian)
Definition: riffdec.c:86
int pb_is_copied
Definition: isom.h:119
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:240
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:338
#define TAG_IS_AVCI(tag)
Definition: isom.h:258
#define MOV_TFHD_STSD_ID
Definition: isom.h:229
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2300
AVFormatContext * dv_fctx
Definition: isom.h:182
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
uint64_t implicit_offset
Definition: isom.h:83
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
Definition: mov.c:2512
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3246
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:84
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:131
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:543
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:65
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:813
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:91
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:814
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:913
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1342
A reference to a data buffer.
Definition: buffer.h:81
int start_pad
amount of samples to skip due to enc-dec delay
Definition: isom.h:164
negative time values are allowed
Definition: timecode.h:38
full parsing and repack
Definition: avformat.h:786
Main libavformat public API header.
int32_t * display_matrix
Definition: isom.h:171
int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag)
Definition: isom.c:417
AVIOContext * pb
Definition: isom.h:118
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:1552
unsigned int bytes_per_frame
Definition: isom.h:146
static void finish(void)
Definition: ffhash.c:62
unsigned flags
Definition: isom.h:87
int bitrates_count
Definition: isom.h:197
int wrong_dts
dts are wrong due to huge ctts offset (iMovie files)
Definition: isom.h:155
if(ret< 0)
Definition: vf_mcdeint.c:280
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:228
int64_t size
Definition: isom.h:73
int export_xmp
Definition: isom.h:195
int seek_individually
Definition: isom.h:192
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:72
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
static const MOVParseTableEntry mov_default_parse_table[]
Definition: mov.c:3954
static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, uint32_t value, int flags)
Definition: mov.c:4300
static double c[64]
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:143
static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:755
unsigned int stps_count
Definition: isom.h:130
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:917
unsigned int chunk_count
Definition: isom.h:122
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:208
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:1634
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:915
int den
denominator
Definition: rational.h:45
unsigned bps
Definition: movenc.c:1340
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:351
double av_display_rotation_get(const int32_t matrix[9])
The display transformation matrix specifies an affine transformation that should be applied to video ...
Definition: display.c:34
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:209
#define MKBETAG(a, b, c, d)
Definition: common.h:342
static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1575
uint32_t palette[256]
Definition: isom.h:159
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
unsigned stsd_id
Definition: isom.h:84
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
Definition: mov.c:4880
unsigned int index_entries_allocated_size
Definition: avformat.h:1060
#define av_free(p)
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:492
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2007
char * value
Definition: dict.h:88
int eof_reached
true if eof reached
Definition: avio.h:139
static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1327
int(* AVOpenCallback)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: avformat.h:1262
as in Berlin toast format
Definition: avcodec.h:442
int len
int channels
number of audio channels
Definition: avcodec.h:2273
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, enum AVCodecID codec_id)
Definition: mov.c:1296
unsigned int stts_count
Definition: isom.h:124
unsigned int sample_size
may contain value calculated from stsd or value from stsz atom
Definition: isom.h:136
uint32_t type
Definition: mov.c:64
void * priv_data
Format private data.
Definition: avformat.h:1313
static int mov_read_mfra(MOVContext *c, AVIOContext *f)
Definition: mov.c:4500
uint8_t file_iv[20]
Definition: isom.h:207
static const AVClass mov_class
Definition: mov.c:5030
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1614
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:236
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:193
ITU-R BT2020 for 10 bit system.
Definition: pixfmt.h:513
int time_scale
Definition: isom.h:177
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1521
Definition: isom.h:56
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:488
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
int pseudo_stream_id
-1 means demux all ids
Definition: isom.h:149
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1380
int ffindex
AVStream index.
Definition: isom.h:120
int use_absolute_path
Definition: isom.h:190
#define av_freep(p)
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2269
static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3444
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2198
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
unsigned size
Definition: isom.h:86
#define av_malloc_array(a, b)
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:719
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:303
static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1322
int64_t duration_for_fps
Definition: isom.h:169
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2348
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:303
int stream_index
Definition: avcodec.h:1435
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:896
static int unsupported(AVCodecContext *avctx)
Definition: iff.c:681
unsigned track_id
Definition: isom.h:111
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:98
#define MKTAG(a, b, c, d)
Definition: common.h:341
int moov_retry
Definition: isom.h:198
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:919
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:3012
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1073
static int mov_switch_root(AVFormatContext *s, int64_t target)
Definition: mov.c:4734
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:27
This structure stores compressed data.
Definition: avcodec.h:1410
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:963
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2576
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:946
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:118
Definition: isom.h:62
static uint32_t yuv_to_rgba(uint32_t ycbcr)
Definition: mov.c:1899
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:244
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: avcodec.h:1291
static int width