FFmpeg
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/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/display.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/aes.h"
43 #include "libavutil/aes_ctr.h"
44 #include "libavutil/pixdesc.h"
45 #include "libavutil/sha.h"
46 #include "libavutil/spherical.h"
47 #include "libavutil/stereo3d.h"
48 #include "libavutil/timecode.h"
49 #include "libavutil/dovi_meta.h"
50 #include "libavcodec/ac3tab.h"
51 #include "libavcodec/flac.h"
53 #include "libavcodec/mlp_parse.h"
54 #include "avformat.h"
55 #include "internal.h"
56 #include "avio_internal.h"
57 #include "riff.h"
58 #include "isom.h"
59 #include "libavcodec/get_bits.h"
60 #include "id3v1.h"
61 #include "mov_chan.h"
62 #include "replaygain.h"
63 
64 #if CONFIG_ZLIB
65 #include <zlib.h>
66 #endif
67 
68 #include "qtpalette.h"
69 
70 /* those functions parse an atom */
71 /* links atom IDs to parse functions */
72 typedef struct MOVParseTableEntry {
73  uint32_t type;
76 
77 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
78 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
79 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
80  int count, int duration);
81 
83  unsigned len, const char *key)
84 {
85  char buf[16];
86 
87  short current, total = 0;
88  avio_rb16(pb); // unknown
89  current = avio_rb16(pb);
90  if (len >= 6)
91  total = avio_rb16(pb);
92  if (!total)
93  snprintf(buf, sizeof(buf), "%d", current);
94  else
95  snprintf(buf, sizeof(buf), "%d/%d", current, total);
96  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
97  av_dict_set(&c->fc->metadata, key, buf, 0);
98 
99  return 0;
100 }
101 
103  unsigned len, const char *key)
104 {
105  /* bypass padding bytes */
106  avio_r8(pb);
107  avio_r8(pb);
108  avio_r8(pb);
109 
110  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
111  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
112 
113  return 0;
114 }
115 
117  unsigned len, const char *key)
118 {
119  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
120  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
121 
122  return 0;
123 }
124 
126  unsigned len, const char *key)
127 {
128  short genre;
129 
130  avio_r8(pb); // unknown
131 
132  genre = avio_r8(pb);
133  if (genre < 1 || genre > ID3v1_GENRE_MAX)
134  return 0;
135  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
136  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
137 
138  return 0;
139 }
140 
141 static const uint32_t mac_to_unicode[128] = {
142  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
143  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
144  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
145  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
146  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
147  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
148  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
149  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
150  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
151  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
152  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
153  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
154  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
155  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
156  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
157  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
158 };
159 
161  char *dst, int dstlen)
162 {
163  char *p = dst;
164  char *end = dst+dstlen-1;
165  int i;
166 
167  for (i = 0; i < len; i++) {
168  uint8_t t, c = avio_r8(pb);
169 
170  if (p >= end)
171  continue;
172 
173  if (c < 0x80)
174  *p++ = c;
175  else if (p < end)
176  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
177  }
178  *p = 0;
179  return p - dst;
180 }
181 
182 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
183 {
184  AVStream *st;
185  MOVStreamContext *sc;
186  enum AVCodecID id;
187  int ret;
188 
189  switch (type) {
190  case 0xd: id = AV_CODEC_ID_MJPEG; break;
191  case 0xe: id = AV_CODEC_ID_PNG; break;
192  case 0x1b: id = AV_CODEC_ID_BMP; break;
193  default:
194  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
195  avio_skip(pb, len);
196  return 0;
197  }
198 
199  st = avformat_new_stream(c->fc, NULL);
200  if (!st)
201  return AVERROR(ENOMEM);
202  sc = av_mallocz(sizeof(*sc));
203  if (!sc)
204  return AVERROR(ENOMEM);
205  st->priv_data = sc;
206 
207  ret = av_get_packet(pb, &st->attached_pic, len);
208  if (ret < 0)
209  return ret;
210 
211  if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
212  if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
213  id = AV_CODEC_ID_PNG;
214  } else {
215  id = AV_CODEC_ID_MJPEG;
216  }
217  }
218 
220 
221  st->attached_pic.stream_index = st->index;
223 
225  st->codecpar->codec_id = id;
226 
227  return 0;
228 }
229 
230 // 3GPP TS 26.244
231 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
232 {
233  char language[4] = { 0 };
234  char buf[200], place[100];
235  uint16_t langcode = 0;
236  double longitude, latitude, altitude;
237  const char *key = "location";
238 
239  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
240  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
241  return AVERROR_INVALIDDATA;
242  }
243 
244  avio_skip(pb, 4); // version+flags
245  langcode = avio_rb16(pb);
246  ff_mov_lang_to_iso639(langcode, language);
247  len -= 6;
248 
249  len -= avio_get_str(pb, len, place, sizeof(place));
250  if (len < 1) {
251  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
252  return AVERROR_INVALIDDATA;
253  }
254  avio_skip(pb, 1); // role
255  len -= 1;
256 
257  if (len < 12) {
258  av_log(c->fc, AV_LOG_ERROR,
259  "loci too short (%u bytes left, need at least %d)\n", len, 12);
260  return AVERROR_INVALIDDATA;
261  }
262  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
263  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
264  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
265 
266  // Try to output in the same format as the ?xyz field
267  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
268  if (altitude)
269  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
270  av_strlcatf(buf, sizeof(buf), "/%s", place);
271 
272  if (*language && strcmp(language, "und")) {
273  char key2[16];
274  snprintf(key2, sizeof(key2), "%s-%s", key, language);
275  av_dict_set(&c->fc->metadata, key2, buf, 0);
276  }
277  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
278  return av_dict_set(&c->fc->metadata, key, buf, 0);
279 }
280 
281 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
282 {
283  int i, n_hmmt;
284 
285  if (len < 2)
286  return 0;
287  if (c->ignore_chapters)
288  return 0;
289 
290  n_hmmt = avio_rb32(pb);
291  if (n_hmmt > len / 4)
292  return AVERROR_INVALIDDATA;
293  for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
294  int moment_time = avio_rb32(pb);
295  avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
296  }
297  if (avio_feof(pb))
298  return AVERROR_INVALIDDATA;
299  return 0;
300 }
301 
303 {
304  char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
305  char key2[32], language[4] = {0};
306  char *str = NULL;
307  const char *key = NULL;
308  uint16_t langcode = 0;
309  uint32_t data_type = 0, str_size, str_size_alloc;
310  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
311  int raw = 0;
312  int num = 0;
313 
314  switch (atom.type) {
315  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
316  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
317  case MKTAG( 'X','M','P','_'):
318  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
319  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
320  case MKTAG( 'a','k','I','D'): key = "account_type";
322  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
323  case MKTAG( 'c','a','t','g'): key = "category"; break;
324  case MKTAG( 'c','p','i','l'): key = "compilation";
326  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
327  case MKTAG( 'd','e','s','c'): key = "description"; break;
328  case MKTAG( 'd','i','s','k'): key = "disc";
330  case MKTAG( 'e','g','i','d'): key = "episode_uid";
332  case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
333  case MKTAG( 'g','n','r','e'): key = "genre";
334  parse = mov_metadata_gnre; break;
335  case MKTAG( 'h','d','v','d'): key = "hd_video";
337  case MKTAG( 'H','M','M','T'):
338  return mov_metadata_hmmt(c, pb, atom.size);
339  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
340  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
341  case MKTAG( 'l','o','c','i'):
342  return mov_metadata_loci(c, pb, atom.size);
343  case MKTAG( 'm','a','n','u'): key = "make"; break;
344  case MKTAG( 'm','o','d','l'): key = "model"; break;
345  case MKTAG( 'p','c','s','t'): key = "podcast";
347  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
349  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
350  case MKTAG( 'r','t','n','g'): key = "rating";
352  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
353  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
354  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
355  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
356  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
357  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
358  case MKTAG( 's','t','i','k'): key = "media_type";
360  case MKTAG( 't','r','k','n'): key = "track";
362  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
363  case MKTAG( 't','v','e','s'): key = "episode_sort";
365  case MKTAG( 't','v','n','n'): key = "network"; break;
366  case MKTAG( 't','v','s','h'): key = "show"; break;
367  case MKTAG( 't','v','s','n'): key = "season_number";
369  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
370  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
371  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
372  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
373  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
374  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
375  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
376  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
377  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
378  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
379  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
380  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
381  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
382  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
383  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
384  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
385  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
386  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
387  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
388  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
389  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
390  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
391  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
392  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
393  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
394  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
395  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
396  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
397  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
398  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
399  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
400  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
401  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
402  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
403  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
404  }
405 retry:
406  if (c->itunes_metadata && atom.size > 8) {
407  int data_size = avio_rb32(pb);
408  int tag = avio_rl32(pb);
409  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size && data_size >= 16) {
410  data_type = avio_rb32(pb); // type
411  avio_rb32(pb); // unknown
412  str_size = data_size - 16;
413  atom.size -= 16;
414 
415  if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
416  int ret = mov_read_covr(c, pb, data_type, str_size);
417  if (ret < 0) {
418  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
419  return ret;
420  }
421  atom.size -= str_size;
422  if (atom.size > 8)
423  goto retry;
424  return ret;
425  } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
426  uint32_t index = AV_RB32(&atom.type);
427  if (index < c->meta_keys_count && index > 0) {
428  key = c->meta_keys[index];
429  } else {
430  av_log(c->fc, AV_LOG_WARNING,
431  "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
432  index, c->meta_keys_count);
433  }
434  }
435  } else return 0;
436  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
437  str_size = avio_rb16(pb); // string length
438  if (str_size > atom.size) {
439  raw = 1;
440  avio_seek(pb, -2, SEEK_CUR);
441  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
442  goto retry;
443  }
444  langcode = avio_rb16(pb);
445  ff_mov_lang_to_iso639(langcode, language);
446  atom.size -= 4;
447  } else
448  str_size = atom.size;
449 
450  if (c->export_all && !key) {
451  key = av_fourcc_make_string(tmp_key, atom.type);
452  }
453 
454  if (!key)
455  return 0;
456  if (atom.size < 0 || str_size >= INT_MAX/2)
457  return AVERROR_INVALIDDATA;
458 
459  // Allocates enough space if data_type is a int32 or float32 number, otherwise
460  // worst-case requirement for output string in case of utf8 coded input
461  num = (data_type >= 21 && data_type <= 23);
462  str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
463  str = av_mallocz(str_size_alloc);
464  if (!str)
465  return AVERROR(ENOMEM);
466 
467  if (parse)
468  parse(c, pb, str_size, key);
469  else {
470  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
471  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
472  } else if (data_type == 21) { // BE signed integer, variable size
473  int val = 0;
474  if (str_size == 1)
475  val = (int8_t)avio_r8(pb);
476  else if (str_size == 2)
477  val = (int16_t)avio_rb16(pb);
478  else if (str_size == 3)
479  val = ((int32_t)(avio_rb24(pb)<<8))>>8;
480  else if (str_size == 4)
481  val = (int32_t)avio_rb32(pb);
482  if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
483  av_log(c->fc, AV_LOG_ERROR,
484  "Failed to store the number (%d) in string.\n", val);
485  av_free(str);
486  return AVERROR_INVALIDDATA;
487  }
488  } else if (data_type == 22) { // BE unsigned integer, variable size
489  unsigned int val = 0;
490  if (str_size == 1)
491  val = avio_r8(pb);
492  else if (str_size == 2)
493  val = avio_rb16(pb);
494  else if (str_size == 3)
495  val = avio_rb24(pb);
496  else if (str_size == 4)
497  val = avio_rb32(pb);
498  if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
499  av_log(c->fc, AV_LOG_ERROR,
500  "Failed to store the number (%u) in string.\n", val);
501  av_free(str);
502  return AVERROR_INVALIDDATA;
503  }
504  } else if (data_type == 23 && str_size >= 4) { // BE float32
505  float val = av_int2float(avio_rb32(pb));
506  if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
507  av_log(c->fc, AV_LOG_ERROR,
508  "Failed to store the float32 number (%f) in string.\n", val);
509  av_free(str);
510  return AVERROR_INVALIDDATA;
511  }
512  } else {
513  int ret = ffio_read_size(pb, str, str_size);
514  if (ret < 0) {
515  av_free(str);
516  return ret;
517  }
518  str[str_size] = 0;
519  }
520  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
521  av_dict_set(&c->fc->metadata, key, str, 0);
522  if (*language && strcmp(language, "und")) {
523  snprintf(key2, sizeof(key2), "%s-%s", key, language);
524  av_dict_set(&c->fc->metadata, key2, str, 0);
525  }
526  if (!strcmp(key, "encoder")) {
527  int major, minor, micro;
528  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
529  c->handbrake_version = 1000000*major + 1000*minor + micro;
530  }
531  }
532  }
533 
534  av_freep(&str);
535  return 0;
536 }
537 
539 {
540  int64_t start;
541  int i, nb_chapters, str_len, version;
542  char str[256+1];
543  int ret;
544 
545  if (c->ignore_chapters)
546  return 0;
547 
548  if ((atom.size -= 5) < 0)
549  return 0;
550 
551  version = avio_r8(pb);
552  avio_rb24(pb);
553  if (version)
554  avio_rb32(pb); // ???
555  nb_chapters = avio_r8(pb);
556 
557  for (i = 0; i < nb_chapters; i++) {
558  if (atom.size < 9)
559  return 0;
560 
561  start = avio_rb64(pb);
562  str_len = avio_r8(pb);
563 
564  if ((atom.size -= 9+str_len) < 0)
565  return 0;
566 
567  ret = ffio_read_size(pb, str, str_len);
568  if (ret < 0)
569  return ret;
570  str[str_len] = 0;
571  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
572  }
573  return 0;
574 }
575 
576 #define MIN_DATA_ENTRY_BOX_SIZE 12
578 {
579  AVStream *st;
580  MOVStreamContext *sc;
581  int entries, i, j;
582 
583  if (c->fc->nb_streams < 1)
584  return 0;
585  st = c->fc->streams[c->fc->nb_streams-1];
586  sc = st->priv_data;
587 
588  avio_rb32(pb); // version + flags
589  entries = avio_rb32(pb);
590  if (!entries ||
591  entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
592  entries >= UINT_MAX / sizeof(*sc->drefs))
593  return AVERROR_INVALIDDATA;
594 
595  for (i = 0; i < sc->drefs_count; i++) {
596  MOVDref *dref = &sc->drefs[i];
597  av_freep(&dref->path);
598  av_freep(&dref->dir);
599  }
600  av_free(sc->drefs);
601  sc->drefs_count = 0;
602  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
603  if (!sc->drefs)
604  return AVERROR(ENOMEM);
605  sc->drefs_count = entries;
606 
607  for (i = 0; i < entries; i++) {
608  MOVDref *dref = &sc->drefs[i];
609  uint32_t size = avio_rb32(pb);
610  int64_t next = avio_tell(pb);
611 
612  if (size < 12 || next < 0 || next > INT64_MAX - size)
613  return AVERROR_INVALIDDATA;
614 
615  next += size - 4;
616 
617  dref->type = avio_rl32(pb);
618  avio_rb32(pb); // version + flags
619 
620  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
621  /* macintosh alias record */
622  uint16_t volume_len, len;
623  int16_t type;
624  int ret;
625 
626  avio_skip(pb, 10);
627 
628  volume_len = avio_r8(pb);
629  volume_len = FFMIN(volume_len, 27);
630  ret = ffio_read_size(pb, dref->volume, 27);
631  if (ret < 0)
632  return ret;
633  dref->volume[volume_len] = 0;
634  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
635 
636  avio_skip(pb, 12);
637 
638  len = avio_r8(pb);
639  len = FFMIN(len, 63);
640  ret = ffio_read_size(pb, dref->filename, 63);
641  if (ret < 0)
642  return ret;
643  dref->filename[len] = 0;
644  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
645 
646  avio_skip(pb, 16);
647 
648  /* read next level up_from_alias/down_to_target */
649  dref->nlvl_from = avio_rb16(pb);
650  dref->nlvl_to = avio_rb16(pb);
651  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
652  dref->nlvl_from, dref->nlvl_to);
653 
654  avio_skip(pb, 16);
655 
656  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
657  if (avio_feof(pb))
658  return AVERROR_EOF;
659  type = avio_rb16(pb);
660  len = avio_rb16(pb);
661  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
662  if (len&1)
663  len += 1;
664  if (type == 2) { // absolute path
665  av_free(dref->path);
666  dref->path = av_mallocz(len+1);
667  if (!dref->path)
668  return AVERROR(ENOMEM);
669 
670  ret = ffio_read_size(pb, dref->path, len);
671  if (ret < 0) {
672  av_freep(&dref->path);
673  return ret;
674  }
675  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
676  len -= volume_len;
677  memmove(dref->path, dref->path+volume_len, len);
678  dref->path[len] = 0;
679  }
680  // trim string of any ending zeros
681  for (j = len - 1; j >= 0; j--) {
682  if (dref->path[j] == 0)
683  len--;
684  else
685  break;
686  }
687  for (j = 0; j < len; j++)
688  if (dref->path[j] == ':' || dref->path[j] == 0)
689  dref->path[j] = '/';
690  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
691  } else if (type == 0) { // directory name
692  av_free(dref->dir);
693  dref->dir = av_malloc(len+1);
694  if (!dref->dir)
695  return AVERROR(ENOMEM);
696 
697  ret = ffio_read_size(pb, dref->dir, len);
698  if (ret < 0) {
699  av_freep(&dref->dir);
700  return ret;
701  }
702  dref->dir[len] = 0;
703  for (j = 0; j < len; j++)
704  if (dref->dir[j] == ':')
705  dref->dir[j] = '/';
706  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
707  } else
708  avio_skip(pb, len);
709  }
710  } else {
711  av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
712  dref->type, size);
713  entries--;
714  i--;
715  }
716  avio_seek(pb, next, SEEK_SET);
717  }
718  return 0;
719 }
720 
722 {
723  AVStream *st;
724  uint32_t type;
725  uint32_t ctype;
726  int64_t title_size;
727  char *title_str;
728  int ret;
729 
730  avio_r8(pb); /* version */
731  avio_rb24(pb); /* flags */
732 
733  /* component type */
734  ctype = avio_rl32(pb);
735  type = avio_rl32(pb); /* component subtype */
736 
737  av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
738  av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
739 
740  if (c->trak_index < 0) { // meta not inside a trak
741  if (type == MKTAG('m','d','t','a')) {
742  c->found_hdlr_mdta = 1;
743  }
744  return 0;
745  }
746 
747  st = c->fc->streams[c->fc->nb_streams-1];
748 
749  if (type == MKTAG('v','i','d','e'))
751  else if (type == MKTAG('s','o','u','n'))
753  else if (type == MKTAG('m','1','a',' '))
755  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
757 
758  avio_rb32(pb); /* component manufacture */
759  avio_rb32(pb); /* component flags */
760  avio_rb32(pb); /* component flags mask */
761 
762  title_size = atom.size - 24;
763  if (title_size > 0) {
764  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
765  return AVERROR_INVALIDDATA;
766  title_str = av_malloc(title_size + 1); /* Add null terminator */
767  if (!title_str)
768  return AVERROR(ENOMEM);
769 
770  ret = ffio_read_size(pb, title_str, title_size);
771  if (ret < 0) {
772  av_freep(&title_str);
773  return ret;
774  }
775  title_str[title_size] = 0;
776  if (title_str[0]) {
777  int off = (!c->isom && title_str[0] == title_size - 1);
778  // flag added so as to not set stream handler name if already set from mdia->hdlr
779  av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
780  }
781  av_freep(&title_str);
782  }
783 
784  return 0;
785 }
786 
788 {
789  return ff_mov_read_esds(c->fc, pb);
790 }
791 
793 {
794  AVStream *st;
795  enum AVAudioServiceType *ast;
796  int ac3info, acmod, lfeon, bsmod;
797 
798  if (c->fc->nb_streams < 1)
799  return 0;
800  st = c->fc->streams[c->fc->nb_streams-1];
801 
803  sizeof(*ast));
804  if (!ast)
805  return AVERROR(ENOMEM);
806 
807  ac3info = avio_rb24(pb);
808  bsmod = (ac3info >> 14) & 0x7;
809  acmod = (ac3info >> 11) & 0x7;
810  lfeon = (ac3info >> 10) & 0x1;
811  st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
813  if (lfeon)
815  *ast = bsmod;
816  if (st->codecpar->channels > 1 && bsmod == 0x7)
818 
819 #if FF_API_LAVF_AVCTX
821  st->codec->audio_service_type = *ast;
823 #endif
824 
825  return 0;
826 }
827 
829 {
830  AVStream *st;
831  enum AVAudioServiceType *ast;
832  int eac3info, acmod, lfeon, bsmod;
833 
834  if (c->fc->nb_streams < 1)
835  return 0;
836  st = c->fc->streams[c->fc->nb_streams-1];
837 
839  sizeof(*ast));
840  if (!ast)
841  return AVERROR(ENOMEM);
842 
843  /* No need to parse fields for additional independent substreams and its
844  * associated dependent substreams since libavcodec's E-AC-3 decoder
845  * does not support them yet. */
846  avio_rb16(pb); /* data_rate and num_ind_sub */
847  eac3info = avio_rb24(pb);
848  bsmod = (eac3info >> 12) & 0x1f;
849  acmod = (eac3info >> 9) & 0x7;
850  lfeon = (eac3info >> 8) & 0x1;
852  if (lfeon)
855  *ast = bsmod;
856  if (st->codecpar->channels > 1 && bsmod == 0x7)
858 
859 #if FF_API_LAVF_AVCTX
861  st->codec->audio_service_type = *ast;
863 #endif
864 
865  return 0;
866 }
867 
869 {
870 #define DDTS_SIZE 20
872  AVStream *st = NULL;
873  uint32_t frame_duration_code = 0;
874  uint32_t channel_layout_code = 0;
875  GetBitContext gb;
876  int ret;
877 
878  if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
879  return ret;
880 
881  init_get_bits(&gb, buf, 8 * DDTS_SIZE);
882 
883  if (c->fc->nb_streams < 1) {
884  return 0;
885  }
886  st = c->fc->streams[c->fc->nb_streams-1];
887 
888  st->codecpar->sample_rate = get_bits_long(&gb, 32);
889  if (st->codecpar->sample_rate <= 0) {
890  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
891  return AVERROR_INVALIDDATA;
892  }
893  skip_bits_long(&gb, 32); /* max bitrate */
894  st->codecpar->bit_rate = get_bits_long(&gb, 32);
895  st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
896  frame_duration_code = get_bits(&gb, 2);
897  skip_bits(&gb, 30); /* various fields */
898  channel_layout_code = get_bits(&gb, 16);
899 
900  st->codecpar->frame_size =
901  (frame_duration_code == 0) ? 512 :
902  (frame_duration_code == 1) ? 1024 :
903  (frame_duration_code == 2) ? 2048 :
904  (frame_duration_code == 3) ? 4096 : 0;
905 
906  if (channel_layout_code > 0xff) {
907  av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
908  }
909  st->codecpar->channel_layout =
910  ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
911  ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
912  ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
913  ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
914  ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
915  ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
916 
918 
919  return 0;
920 }
921 
923 {
924  AVStream *st;
925 
926  if (c->fc->nb_streams < 1)
927  return 0;
928  st = c->fc->streams[c->fc->nb_streams-1];
929 
930  if (atom.size < 16)
931  return 0;
932 
933  /* skip version and flags */
934  avio_skip(pb, 4);
935 
936  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
937 
938  return 0;
939 }
940 
942 {
943  AVStream *st;
944  int ret;
945 
946  if (c->fc->nb_streams < 1)
947  return 0;
948  st = c->fc->streams[c->fc->nb_streams-1];
949 
950  if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
951  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
952 
953  return ret;
954 }
955 
957 {
958  const int num = avio_rb32(pb);
959  const int den = avio_rb32(pb);
960  AVStream *st;
961 
962  if (c->fc->nb_streams < 1)
963  return 0;
964  st = c->fc->streams[c->fc->nb_streams-1];
965 
966  if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
967  (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
968  av_log(c->fc, AV_LOG_WARNING,
969  "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
971  num, den);
972  } else if (den != 0) {
974  num, den, 32767);
975  }
976  return 0;
977 }
978 
979 /* this atom contains actual media data */
981 {
982  if (atom.size == 0) /* wrong one (MP4) */
983  return 0;
984  c->found_mdat=1;
985  return 0; /* now go for moov */
986 }
987 
988 #define DRM_BLOB_SIZE 56
989 
991 {
992  uint8_t intermediate_key[20];
993  uint8_t intermediate_iv[20];
994  uint8_t input[64];
995  uint8_t output[64];
996  uint8_t file_checksum[20];
997  uint8_t calculated_checksum[20];
998  struct AVSHA *sha;
999  int i;
1000  int ret = 0;
1001  uint8_t *activation_bytes = c->activation_bytes;
1002  uint8_t *fixed_key = c->audible_fixed_key;
1003 
1004  c->aax_mode = 1;
1005 
1006  sha = av_sha_alloc();
1007  if (!sha)
1008  return AVERROR(ENOMEM);
1009  av_free(c->aes_decrypt);
1010  c->aes_decrypt = av_aes_alloc();
1011  if (!c->aes_decrypt) {
1012  ret = AVERROR(ENOMEM);
1013  goto fail;
1014  }
1015 
1016  /* drm blob processing */
1017  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1019  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1020  avio_read(pb, file_checksum, 20);
1021 
1022  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
1023  for (i = 0; i < 20; i++)
1024  av_log(c->fc, AV_LOG_INFO, "%02x", file_checksum[i]);
1025  av_log(c->fc, AV_LOG_INFO, "\n");
1026 
1027  /* verify activation data */
1028  if (!activation_bytes) {
1029  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1030  ret = 0; /* allow ffprobe to continue working on .aax files */
1031  goto fail;
1032  }
1033  if (c->activation_bytes_size != 4) {
1034  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1035  ret = AVERROR(EINVAL);
1036  goto fail;
1037  }
1038 
1039  /* verify fixed key */
1040  if (c->audible_fixed_key_size != 16) {
1041  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1042  ret = AVERROR(EINVAL);
1043  goto fail;
1044  }
1045 
1046  /* AAX (and AAX+) key derivation */
1047  av_sha_init(sha, 160);
1048  av_sha_update(sha, fixed_key, 16);
1049  av_sha_update(sha, activation_bytes, 4);
1050  av_sha_final(sha, intermediate_key);
1051  av_sha_init(sha, 160);
1052  av_sha_update(sha, fixed_key, 16);
1053  av_sha_update(sha, intermediate_key, 20);
1054  av_sha_update(sha, activation_bytes, 4);
1055  av_sha_final(sha, intermediate_iv);
1056  av_sha_init(sha, 160);
1057  av_sha_update(sha, intermediate_key, 16);
1058  av_sha_update(sha, intermediate_iv, 16);
1059  av_sha_final(sha, calculated_checksum);
1060  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1061  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1063  goto fail;
1064  }
1065  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1066  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1067  for (i = 0; i < 4; i++) {
1068  // file data (in output) is stored in big-endian mode
1069  if (activation_bytes[i] != output[3 - i]) { // critical error
1070  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1072  goto fail;
1073  }
1074  }
1075  memcpy(c->file_key, output + 8, 16);
1076  memcpy(input, output + 26, 16);
1077  av_sha_init(sha, 160);
1078  av_sha_update(sha, input, 16);
1079  av_sha_update(sha, c->file_key, 16);
1080  av_sha_update(sha, fixed_key, 16);
1081  av_sha_final(sha, c->file_iv);
1082 
1083 fail:
1084  av_free(sha);
1085 
1086  return ret;
1087 }
1088 
1090 {
1091  if (c->audible_key_size != 16) {
1092  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
1093  return AVERROR(EINVAL);
1094  }
1095 
1096  if (c->audible_iv_size != 16) {
1097  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
1098  return AVERROR(EINVAL);
1099  }
1100 
1101  c->aes_decrypt = av_aes_alloc();
1102  if (!c->aes_decrypt) {
1103  return AVERROR(ENOMEM);
1104  }
1105 
1106  memcpy(c->file_key, c->audible_key, 16);
1107  memcpy(c->file_iv, c->audible_iv, 16);
1108  c->aax_mode = 1;
1109 
1110  return 0;
1111 }
1112 
1113 // Audible AAX (and AAX+) bytestream decryption
1115 {
1116  int blocks = 0;
1117  unsigned char iv[16];
1118 
1119  memcpy(iv, c->file_iv, 16); // iv is overwritten
1120  blocks = size >> 4; // trailing bytes are not encrypted!
1121  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1122  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1123 
1124  return 0;
1125 }
1126 
1127 /* read major brand, minor version and compatible brands and store them as metadata */
1129 {
1130  uint32_t minor_ver;
1131  int comp_brand_size;
1132  char* comp_brands_str;
1133  uint8_t type[5] = {0};
1134  int ret = ffio_read_size(pb, type, 4);
1135  if (ret < 0)
1136  return ret;
1137 
1138  if (strcmp(type, "qt "))
1139  c->isom = 1;
1140  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1141  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1142  minor_ver = avio_rb32(pb); /* minor version */
1143  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1144 
1145  comp_brand_size = atom.size - 8;
1146  if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
1147  return AVERROR_INVALIDDATA;
1148  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1149  if (!comp_brands_str)
1150  return AVERROR(ENOMEM);
1151 
1152  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1153  if (ret < 0) {
1154  av_freep(&comp_brands_str);
1155  return ret;
1156  }
1157  comp_brands_str[comp_brand_size] = 0;
1158  av_dict_set(&c->fc->metadata, "compatible_brands",
1159  comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
1160 
1161  // Logic for handling Audible's .aaxc files
1162  if (!strcmp(type, "aaxc")) {
1163  mov_aaxc_crypto(c);
1164  }
1165 
1166  return 0;
1167 }
1168 
1169 /* this atom should contain all header atoms */
1171 {
1172  int ret;
1173 
1174  if (c->found_moov) {
1175  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1176  avio_skip(pb, atom.size);
1177  return 0;
1178  }
1179 
1180  if ((ret = mov_read_default(c, pb, atom)) < 0)
1181  return ret;
1182  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1183  /* so we don't parse the whole file if over a network */
1184  c->found_moov=1;
1185  return 0; /* now go for mdat */
1186 }
1187 
1189  MOVFragmentIndex *frag_index,
1190  int index,
1191  int id)
1192 {
1193  int i;
1194  MOVFragmentIndexItem * item;
1195 
1196  if (index < 0 || index >= frag_index->nb_items)
1197  return NULL;
1198  item = &frag_index->item[index];
1199  for (i = 0; i < item->nb_stream_info; i++)
1200  if (item->stream_info[i].id == id)
1201  return &item->stream_info[i];
1202 
1203  // This shouldn't happen
1204  return NULL;
1205 }
1206 
1207 static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
1208 {
1209  int i;
1210  MOVFragmentIndexItem * item;
1211 
1212  if (frag_index->current < 0 ||
1213  frag_index->current >= frag_index->nb_items)
1214  return;
1215 
1216  item = &frag_index->item[frag_index->current];
1217  for (i = 0; i < item->nb_stream_info; i++)
1218  if (item->stream_info[i].id == id) {
1219  item->current = i;
1220  return;
1221  }
1222 
1223  // id not found. This shouldn't happen.
1224  item->current = -1;
1225 }
1226 
1228  MOVFragmentIndex *frag_index)
1229 {
1230  MOVFragmentIndexItem *item;
1231  if (frag_index->current < 0 ||
1232  frag_index->current >= frag_index->nb_items)
1233  return NULL;
1234 
1235  item = &frag_index->item[frag_index->current];
1236  if (item->current >= 0 && item->current < item->nb_stream_info)
1237  return &item->stream_info[item->current];
1238 
1239  // This shouldn't happen
1240  return NULL;
1241 }
1242 
1243 static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
1244 {
1245  int a, b, m;
1246  int64_t moof_offset;
1247 
1248  // Optimize for appending new entries
1249  if (!frag_index->nb_items ||
1250  frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
1251  return frag_index->nb_items;
1252 
1253  a = -1;
1254  b = frag_index->nb_items;
1255 
1256  while (b - a > 1) {
1257  m = (a + b) >> 1;
1258  moof_offset = frag_index->item[m].moof_offset;
1259  if (moof_offset >= offset)
1260  b = m;
1261  if (moof_offset <= offset)
1262  a = m;
1263  }
1264  return b;
1265 }
1266 
1267 static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
1268 {
1269  av_assert0(frag_stream_info);
1270  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1271  return frag_stream_info->sidx_pts;
1272  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1273  return frag_stream_info->first_tfra_pts;
1274  return frag_stream_info->tfdt_dts;
1275 }
1276 
1277 static int64_t get_frag_time(MOVFragmentIndex *frag_index,
1278  int index, int track_id)
1279 {
1280  MOVFragmentStreamInfo * frag_stream_info;
1281  int64_t timestamp;
1282  int i;
1283 
1284  if (track_id >= 0) {
1285  frag_stream_info = get_frag_stream_info(frag_index, index, track_id);
1286  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1287  return frag_stream_info->sidx_pts;
1288  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1289  return frag_stream_info->first_tfra_pts;
1290  return frag_stream_info->sidx_pts;
1291  }
1292 
1293  for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
1294  frag_stream_info = &frag_index->item[index].stream_info[i];
1295  timestamp = get_stream_info_time(frag_stream_info);
1296  if (timestamp != AV_NOPTS_VALUE)
1297  return timestamp;
1298  }
1299  return AV_NOPTS_VALUE;
1300 }
1301 
1303  AVStream *st, int64_t timestamp)
1304 {
1305  int a, b, m, m0;
1306  int64_t frag_time;
1307  int id = -1;
1308 
1309  if (st) {
1310  // If the stream is referenced by any sidx, limit the search
1311  // to fragments that referenced this stream in the sidx
1312  MOVStreamContext *sc = st->priv_data;
1313  if (sc->has_sidx)
1314  id = st->id;
1315  }
1316 
1317  a = -1;
1318  b = frag_index->nb_items;
1319 
1320  while (b - a > 1) {
1321  m0 = m = (a + b) >> 1;
1322 
1323  while (m < b &&
1324  (frag_time = get_frag_time(frag_index, m, id)) == AV_NOPTS_VALUE)
1325  m++;
1326 
1327  if (m < b && frag_time <= timestamp)
1328  a = m;
1329  else
1330  b = m0;
1331  }
1332 
1333  return a;
1334 }
1335 
1336 static int update_frag_index(MOVContext *c, int64_t offset)
1337 {
1338  int index, i;
1339  MOVFragmentIndexItem * item;
1340  MOVFragmentStreamInfo * frag_stream_info;
1341 
1342  // If moof_offset already exists in frag_index, return index to it
1343  index = search_frag_moof_offset(&c->frag_index, offset);
1344  if (index < c->frag_index.nb_items &&
1345  c->frag_index.item[index].moof_offset == offset)
1346  return index;
1347 
1348  // offset is not yet in frag index.
1349  // Insert new item at index (sorted by moof offset)
1350  item = av_fast_realloc(c->frag_index.item,
1351  &c->frag_index.allocated_size,
1352  (c->frag_index.nb_items + 1) *
1353  sizeof(*c->frag_index.item));
1354  if (!item)
1355  return -1;
1356  c->frag_index.item = item;
1357 
1358  frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
1359  sizeof(*item->stream_info));
1360  if (!frag_stream_info)
1361  return -1;
1362 
1363  for (i = 0; i < c->fc->nb_streams; i++) {
1364  // Avoid building frag index if streams lack track id.
1365  if (c->fc->streams[i]->id < 0) {
1366  av_free(frag_stream_info);
1367  return AVERROR_INVALIDDATA;
1368  }
1369 
1370  frag_stream_info[i].id = c->fc->streams[i]->id;
1371  frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
1372  frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
1373  frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
1374  frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
1375  frag_stream_info[i].index_entry = -1;
1376  frag_stream_info[i].encryption_index = NULL;
1377  }
1378 
1379  if (index < c->frag_index.nb_items)
1380  memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
1381  (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
1382 
1383  item = &c->frag_index.item[index];
1384  item->headers_read = 0;
1385  item->current = 0;
1386  item->nb_stream_info = c->fc->nb_streams;
1387  item->moof_offset = offset;
1388  item->stream_info = frag_stream_info;
1389  c->frag_index.nb_items++;
1390 
1391  return index;
1392 }
1393 
1394 static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
1395  int id, int entries)
1396 {
1397  int i;
1398  MOVFragmentStreamInfo * frag_stream_info;
1399 
1400  if (index < 0)
1401  return;
1402  for (i = index; i < frag_index->nb_items; i++) {
1403  frag_stream_info = get_frag_stream_info(frag_index, i, id);
1404  if (frag_stream_info && frag_stream_info->index_entry >= 0)
1405  frag_stream_info->index_entry += entries;
1406  }
1407 }
1408 
1410 {
1411  // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
1412  c->fragment.found_tfhd = 0;
1413 
1414  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1415  c->has_looked_for_mfra = 1;
1416  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1417  int ret;
1418  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1419  "for a mfra\n");
1420  if ((ret = mov_read_mfra(c, pb)) < 0) {
1421  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1422  "read the mfra (may be a live ismv)\n");
1423  }
1424  } else {
1425  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1426  "seekable, can not look for mfra\n");
1427  }
1428  }
1429  c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1430  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1431  c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
1432  return mov_read_default(c, pb, atom);
1433 }
1434 
1435 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time, void *logctx)
1436 {
1437  if (time) {
1438  if (time >= 2082844800)
1439  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1440 
1441  if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1442  av_log(logctx, AV_LOG_DEBUG, "creation_time is not representable\n");
1443  return;
1444  }
1445 
1446  avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1447  }
1448 }
1449 
1451 {
1452  AVStream *st;
1453  MOVStreamContext *sc;
1454  int version;
1455  char language[4] = {0};
1456  unsigned lang;
1457  int64_t creation_time;
1458 
1459  if (c->fc->nb_streams < 1)
1460  return 0;
1461  st = c->fc->streams[c->fc->nb_streams-1];
1462  sc = st->priv_data;
1463 
1464  if (sc->time_scale) {
1465  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1466  return AVERROR_INVALIDDATA;
1467  }
1468 
1469  version = avio_r8(pb);
1470  if (version > 1) {
1471  avpriv_request_sample(c->fc, "Version %d", version);
1472  return AVERROR_PATCHWELCOME;
1473  }
1474  avio_rb24(pb); /* flags */
1475  if (version == 1) {
1476  creation_time = avio_rb64(pb);
1477  avio_rb64(pb);
1478  } else {
1479  creation_time = avio_rb32(pb);
1480  avio_rb32(pb); /* modification time */
1481  }
1482  mov_metadata_creation_time(&st->metadata, creation_time, c->fc);
1483 
1484  sc->time_scale = avio_rb32(pb);
1485  if (sc->time_scale <= 0) {
1486  av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1487  sc->time_scale = 1;
1488  }
1489  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1490 
1491  lang = avio_rb16(pb); /* language */
1492  if (ff_mov_lang_to_iso639(lang, language))
1493  av_dict_set(&st->metadata, "language", language, 0);
1494  avio_rb16(pb); /* quality */
1495 
1496  return 0;
1497 }
1498 
1500 {
1501  int i;
1502  int64_t creation_time;
1503  int version = avio_r8(pb); /* version */
1504  avio_rb24(pb); /* flags */
1505 
1506  if (version == 1) {
1507  creation_time = avio_rb64(pb);
1508  avio_rb64(pb);
1509  } else {
1510  creation_time = avio_rb32(pb);
1511  avio_rb32(pb); /* modification time */
1512  }
1513  mov_metadata_creation_time(&c->fc->metadata, creation_time, c->fc);
1514  c->time_scale = avio_rb32(pb); /* time scale */
1515  if (c->time_scale <= 0) {
1516  av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
1517  c->time_scale = 1;
1518  }
1519  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1520 
1521  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1522  // set the AVFormatContext duration because the duration of individual tracks
1523  // may be inaccurate
1524  if (!c->trex_data)
1525  c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1526  avio_rb32(pb); /* preferred scale */
1527 
1528  avio_rb16(pb); /* preferred volume */
1529 
1530  avio_skip(pb, 10); /* reserved */
1531 
1532  /* movie display matrix, store it in main context and use it later on */
1533  for (i = 0; i < 3; i++) {
1534  c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1535  c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1536  c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
1537  }
1538 
1539  avio_rb32(pb); /* preview time */
1540  avio_rb32(pb); /* preview duration */
1541  avio_rb32(pb); /* poster time */
1542  avio_rb32(pb); /* selection time */
1543  avio_rb32(pb); /* selection duration */
1544  avio_rb32(pb); /* current time */
1545  avio_rb32(pb); /* next track ID */
1546 
1547  return 0;
1548 }
1549 
1551 {
1552  AVStream *st;
1553  int little_endian;
1554 
1555  if (c->fc->nb_streams < 1)
1556  return 0;
1557  st = c->fc->streams[c->fc->nb_streams-1];
1558 
1559  little_endian = avio_rb16(pb) & 0xFF;
1560  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1561  if (little_endian == 1) {
1562  switch (st->codecpar->codec_id) {
1563  case AV_CODEC_ID_PCM_S24BE:
1565  break;
1566  case AV_CODEC_ID_PCM_S32BE:
1568  break;
1569  case AV_CODEC_ID_PCM_F32BE:
1571  break;
1572  case AV_CODEC_ID_PCM_F64BE:
1574  break;
1575  default:
1576  break;
1577  }
1578  }
1579  return 0;
1580 }
1581 
1583 {
1584  AVStream *st;
1585  uint8_t *icc_profile;
1586  char color_parameter_type[5] = { 0 };
1587  uint16_t color_primaries, color_trc, color_matrix;
1588  int ret;
1589 
1590  if (c->fc->nb_streams < 1)
1591  return 0;
1592  st = c->fc->streams[c->fc->nb_streams - 1];
1593 
1594  ret = ffio_read_size(pb, color_parameter_type, 4);
1595  if (ret < 0)
1596  return ret;
1597  if (strncmp(color_parameter_type, "nclx", 4) &&
1598  strncmp(color_parameter_type, "nclc", 4) &&
1599  strncmp(color_parameter_type, "prof", 4)) {
1600  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1601  color_parameter_type);
1602  return 0;
1603  }
1604 
1605  if (!strncmp(color_parameter_type, "prof", 4)) {
1606  icc_profile = av_stream_new_side_data(st, AV_PKT_DATA_ICC_PROFILE, atom.size - 4);
1607  if (!icc_profile)
1608  return AVERROR(ENOMEM);
1609  ret = ffio_read_size(pb, icc_profile, atom.size - 4);
1610  if (ret < 0)
1611  return ret;
1612  } else {
1613  color_primaries = avio_rb16(pb);
1614  color_trc = avio_rb16(pb);
1615  color_matrix = avio_rb16(pb);
1616 
1617  av_log(c->fc, AV_LOG_TRACE,
1618  "%s: pri %d trc %d matrix %d",
1619  color_parameter_type, color_primaries, color_trc, color_matrix);
1620 
1621  if (!strncmp(color_parameter_type, "nclx", 4)) {
1622  uint8_t color_range = avio_r8(pb) >> 7;
1623  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1624  if (color_range)
1626  else
1628  }
1629 
1632  if (!av_color_transfer_name(color_trc))
1633  color_trc = AVCOL_TRC_UNSPECIFIED;
1634  if (!av_color_space_name(color_matrix))
1635  color_matrix = AVCOL_SPC_UNSPECIFIED;
1636 
1638  st->codecpar->color_trc = color_trc;
1639  st->codecpar->color_space = color_matrix;
1640  av_log(c->fc, AV_LOG_TRACE, "\n");
1641  }
1642  return 0;
1643 }
1644 
1646 {
1647  AVStream *st;
1648  unsigned mov_field_order;
1649  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1650 
1651  if (c->fc->nb_streams < 1) // will happen with jp2 files
1652  return 0;
1653  st = c->fc->streams[c->fc->nb_streams-1];
1654  if (atom.size < 2)
1655  return AVERROR_INVALIDDATA;
1656  mov_field_order = avio_rb16(pb);
1657  if ((mov_field_order & 0xFF00) == 0x0100)
1658  decoded_field_order = AV_FIELD_PROGRESSIVE;
1659  else if ((mov_field_order & 0xFF00) == 0x0200) {
1660  switch (mov_field_order & 0xFF) {
1661  case 0x01: decoded_field_order = AV_FIELD_TT;
1662  break;
1663  case 0x06: decoded_field_order = AV_FIELD_BB;
1664  break;
1665  case 0x09: decoded_field_order = AV_FIELD_TB;
1666  break;
1667  case 0x0E: decoded_field_order = AV_FIELD_BT;
1668  break;
1669  }
1670  }
1671  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1672  av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1673  }
1674  st->codecpar->field_order = decoded_field_order;
1675 
1676  return 0;
1677 }
1678 
1680 {
1681  int err = 0;
1682  uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1683  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1684  return AVERROR_INVALIDDATA;
1685  if ((err = av_reallocp(&par->extradata, size)) < 0) {
1686  par->extradata_size = 0;
1687  return err;
1688  }
1690  return 0;
1691 }
1692 
1693 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1695  AVCodecParameters *par, uint8_t *buf)
1696 {
1697  int64_t result = atom.size;
1698  int err;
1699 
1700  AV_WB32(buf , atom.size + 8);
1701  AV_WL32(buf + 4, atom.type);
1702  err = ffio_read_size(pb, buf + 8, atom.size);
1703  if (err < 0) {
1704  par->extradata_size -= atom.size;
1705  return err;
1706  } else if (err < atom.size) {
1707  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1708  par->extradata_size -= atom.size - err;
1709  result = err;
1710  }
1711  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1712  return result;
1713 }
1714 
1715 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1717  enum AVCodecID codec_id)
1718 {
1719  AVStream *st;
1720  uint64_t original_size;
1721  int err;
1722 
1723  if (c->fc->nb_streams < 1) // will happen with jp2 files
1724  return 0;
1725  st = c->fc->streams[c->fc->nb_streams-1];
1726 
1727  if (st->codecpar->codec_id != codec_id)
1728  return 0; /* unexpected codec_id - don't mess with extradata */
1729 
1730  original_size = st->codecpar->extradata_size;
1731  err = mov_realloc_extradata(st->codecpar, atom);
1732  if (err)
1733  return err;
1734 
1735  err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
1736  if (err < 0)
1737  return err;
1738  return 0; // Note: this is the original behavior to ignore truncation.
1739 }
1740 
1741 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1743 {
1744  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1745 }
1746 
1748 {
1749  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1750 }
1751 
1753 {
1754  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1755 }
1756 
1758 {
1759  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1760 }
1761 
1763 {
1764  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1765  if (!ret)
1766  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1767  return ret;
1768 }
1769 
1771 {
1772  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1773 
1774  if (!ret && c->fc->nb_streams >= 1) {
1775  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1776  if (par->extradata_size >= 40) {
1777  par->height = AV_RB16(&par->extradata[36]);
1778  par->width = AV_RB16(&par->extradata[38]);
1779  }
1780  }
1781  return ret;
1782 }
1783 
1785 {
1786  if (c->fc->nb_streams >= 1) {
1787  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1788  if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1789  par->codec_id == AV_CODEC_ID_H264 &&
1790  atom.size > 11) {
1791  int cid;
1792  avio_skip(pb, 10);
1793  cid = avio_rb16(pb);
1794  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1795  if (cid == 0xd4d || cid == 0xd4e)
1796  par->width = 1440;
1797  return 0;
1798  } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
1799  par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
1800  par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
1801  atom.size >= 24) {
1802  int num, den;
1803  avio_skip(pb, 12);
1804  num = avio_rb32(pb);
1805  den = avio_rb32(pb);
1806  if (num <= 0 || den <= 0)
1807  return 0;
1808  switch (avio_rb32(pb)) {
1809  case 2:
1810  if (den >= INT_MAX / 2)
1811  return 0;
1812  den *= 2;
1813  case 1:
1814  c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.num = num;
1815  c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.den = den;
1816  default:
1817  return 0;
1818  }
1819  }
1820  }
1821 
1822  return mov_read_avid(c, pb, atom);
1823 }
1824 
1826 {
1827  int ret = 0;
1828  int length = 0;
1829  uint64_t original_size;
1830  if (c->fc->nb_streams >= 1) {
1831  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1832  if (par->codec_id == AV_CODEC_ID_H264)
1833  return 0;
1834  if (atom.size == 16) {
1835  original_size = par->extradata_size;
1836  ret = mov_realloc_extradata(par, atom);
1837  if (!ret) {
1838  length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
1839  if (length == atom.size) {
1840  const uint8_t range_value = par->extradata[original_size + 19];
1841  switch (range_value) {
1842  case 1:
1844  break;
1845  case 2:
1847  break;
1848  default:
1849  av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1850  break;
1851  }
1852  ff_dlog(c->fc, "color_range: %d\n", par->color_range);
1853  } else {
1854  /* For some reason the whole atom was not added to the extradata */
1855  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1856  }
1857  } else {
1858  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1859  }
1860  } else {
1861  av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1862  }
1863  }
1864 
1865  return ret;
1866 }
1867 
1869 {
1870  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1871 }
1872 
1874 {
1875  AVStream *st;
1876  int ret;
1877 
1878  if (c->fc->nb_streams < 1)
1879  return 0;
1880  st = c->fc->streams[c->fc->nb_streams-1];
1881 
1882  if ((uint64_t)atom.size > (1<<30))
1883  return AVERROR_INVALIDDATA;
1884 
1885  if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
1888  // pass all frma atom to codec, needed at least for QDMC and QDM2
1889  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1890  if (ret < 0)
1891  return ret;
1892  } else if (atom.size > 8) { /* to read frma, esds atoms */
1893  if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1894  uint64_t buffer;
1895  ret = ffio_ensure_seekback(pb, 8);
1896  if (ret < 0)
1897  return ret;
1898  buffer = avio_rb64(pb);
1899  atom.size -= 8;
1900  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1901  && buffer >> 32 <= atom.size
1902  && buffer >> 32 >= 8) {
1903  avio_skip(pb, -8);
1904  atom.size += 8;
1905  } else if (!st->codecpar->extradata_size) {
1906 #define ALAC_EXTRADATA_SIZE 36
1908  if (!st->codecpar->extradata)
1909  return AVERROR(ENOMEM);
1912  AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
1913  AV_WB64(st->codecpar->extradata + 12, buffer);
1914  avio_read(pb, st->codecpar->extradata + 20, 16);
1915  avio_skip(pb, atom.size - 24);
1916  return 0;
1917  }
1918  }
1919  if ((ret = mov_read_default(c, pb, atom)) < 0)
1920  return ret;
1921  } else
1922  avio_skip(pb, atom.size);
1923  return 0;
1924 }
1925 
1926 /**
1927  * This function reads atom content and puts data in extradata without tag
1928  * nor size unlike mov_read_extradata.
1929  */
1931 {
1932  AVStream *st;
1933  int ret;
1934 
1935  if (c->fc->nb_streams < 1)
1936  return 0;
1937  st = c->fc->streams[c->fc->nb_streams-1];
1938 
1939  if ((uint64_t)atom.size > (1<<30))
1940  return AVERROR_INVALIDDATA;
1941 
1942  if (atom.size >= 10) {
1943  // Broken files created by legacy versions of libavformat will
1944  // wrap a whole fiel atom inside of a glbl atom.
1945  unsigned size = avio_rb32(pb);
1946  unsigned type = avio_rl32(pb);
1947  if (avio_feof(pb))
1948  return AVERROR_INVALIDDATA;
1949  avio_seek(pb, -8, SEEK_CUR);
1950  if (type == MKTAG('f','i','e','l') && size == atom.size)
1951  return mov_read_default(c, pb, atom);
1952  }
1953  if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
1954  av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
1955  return 0;
1956  }
1957  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1958  if (ret < 0)
1959  return ret;
1960  if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
1961  /* HEVC-based Dolby Vision derived from hvc1.
1962  Happens to match with an identifier
1963  previously utilized for DV. Thus, if we have
1964  the hvcC extradata box available as specified,
1965  set codec to HEVC */
1967 
1968  return 0;
1969 }
1970 
1972 {
1973  AVStream *st;
1974  uint8_t profile_level;
1975  int ret;
1976 
1977  if (c->fc->nb_streams < 1)
1978  return 0;
1979  st = c->fc->streams[c->fc->nb_streams-1];
1980 
1981  if (atom.size >= (1<<28) || atom.size < 7)
1982  return AVERROR_INVALIDDATA;
1983 
1984  profile_level = avio_r8(pb);
1985  if ((profile_level & 0xf0) != 0xc0)
1986  return 0;
1987 
1988  avio_seek(pb, 6, SEEK_CUR);
1989  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
1990  if (ret < 0)
1991  return ret;
1992 
1993  return 0;
1994 }
1995 
1996 /**
1997  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1998  * but can have extradata appended at the end after the 40 bytes belonging
1999  * to the struct.
2000  */
2002 {
2003  AVStream *st;
2004  int ret;
2005 
2006  if (c->fc->nb_streams < 1)
2007  return 0;
2008  if (atom.size <= 40)
2009  return 0;
2010  st = c->fc->streams[c->fc->nb_streams-1];
2011 
2012  if ((uint64_t)atom.size > (1<<30))
2013  return AVERROR_INVALIDDATA;
2014 
2015  avio_skip(pb, 40);
2016  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
2017  if (ret < 0)
2018  return ret;
2019 
2020  return 0;
2021 }
2022 
2024 {
2025  AVStream *st;
2026  MOVStreamContext *sc;
2027  unsigned int i, entries;
2028 
2029  if (c->trak_index < 0) {
2030  av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
2031  return 0;
2032  }
2033  if (c->fc->nb_streams < 1)
2034  return 0;
2035  st = c->fc->streams[c->fc->nb_streams-1];
2036  sc = st->priv_data;
2037 
2038  avio_r8(pb); /* version */
2039  avio_rb24(pb); /* flags */
2040 
2041  entries = avio_rb32(pb);
2042 
2043  if (!entries)
2044  return 0;
2045 
2046  if (sc->chunk_offsets) {
2047  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STCO atom\n");
2048  return 0;
2049  }
2050  av_free(sc->chunk_offsets);
2051  sc->chunk_count = 0;
2052  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2053  if (!sc->chunk_offsets)
2054  return AVERROR(ENOMEM);
2055  sc->chunk_count = entries;
2056 
2057  if (atom.type == MKTAG('s','t','c','o'))
2058  for (i = 0; i < entries && !pb->eof_reached; i++)
2059  sc->chunk_offsets[i] = avio_rb32(pb);
2060  else if (atom.type == MKTAG('c','o','6','4'))
2061  for (i = 0; i < entries && !pb->eof_reached; i++)
2062  sc->chunk_offsets[i] = avio_rb64(pb);
2063  else
2064  return AVERROR_INVALIDDATA;
2065 
2066  sc->chunk_count = i;
2067 
2068  if (pb->eof_reached) {
2069  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
2070  return AVERROR_EOF;
2071  }
2072 
2073  return 0;
2074 }
2075 
2076 static int mov_codec_id(AVStream *st, uint32_t format)
2077 {
2079 
2080  if (id <= 0 &&
2081  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
2082  (format & 0xFFFF) == 'T' + ('S' << 8)))
2084 
2085  if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
2087  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
2088  /* skip old ASF MPEG-4 tag */
2089  format && format != MKTAG('m','p','4','s')) {
2091  if (id <= 0)
2093  if (id > 0)
2095  else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
2097  st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
2099  if (id > 0)
2101  else
2103  }
2104  }
2105 
2106  st->codecpar->codec_tag = format;
2107 
2108  return id;
2109 }
2110 
2112  AVStream *st, MOVStreamContext *sc)
2113 {
2114  uint8_t codec_name[32] = { 0 };
2115  int64_t stsd_start;
2116  unsigned int len;
2117  uint32_t id = 0;
2118 
2119  /* The first 16 bytes of the video sample description are already
2120  * read in ff_mov_read_stsd_entries() */
2121  stsd_start = avio_tell(pb) - 16;
2122 
2123  avio_rb16(pb); /* version */
2124  avio_rb16(pb); /* revision level */
2125  id = avio_rl32(pb); /* vendor */
2126  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2127  avio_rb32(pb); /* temporal quality */
2128  avio_rb32(pb); /* spatial quality */
2129 
2130  st->codecpar->width = avio_rb16(pb); /* width */
2131  st->codecpar->height = avio_rb16(pb); /* height */
2132 
2133  avio_rb32(pb); /* horiz resolution */
2134  avio_rb32(pb); /* vert resolution */
2135  avio_rb32(pb); /* data size, always 0 */
2136  avio_rb16(pb); /* frames per samples */
2137 
2138  len = avio_r8(pb); /* codec name, pascal string */
2139  if (len > 31)
2140  len = 31;
2141  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
2142  if (len < 31)
2143  avio_skip(pb, 31 - len);
2144 
2145  if (codec_name[0])
2146  av_dict_set(&st->metadata, "encoder", codec_name, 0);
2147 
2148  /* codec_tag YV12 triggers an UV swap in rawdec.c */
2149  if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
2150  st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
2151  st->codecpar->width &= ~1;
2152  st->codecpar->height &= ~1;
2153  }
2154  /* Flash Media Server uses tag H.263 with Sorenson Spark */
2155  if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
2156  !strncmp(codec_name, "Sorenson H263", 13))
2158 
2159  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
2160 
2161  avio_seek(pb, stsd_start, SEEK_SET);
2162 
2163  if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2164  st->codecpar->bits_per_coded_sample &= 0x1F;
2165  sc->has_palette = 1;
2166  }
2167 }
2168 
2170  AVStream *st, MOVStreamContext *sc)
2171 {
2172  int bits_per_sample, flags;
2173  uint16_t version = avio_rb16(pb);
2174  uint32_t id = 0;
2175  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
2176 
2177  avio_rb16(pb); /* revision level */
2178  id = avio_rl32(pb); /* vendor */
2179  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2180 
2181  st->codecpar->channels = avio_rb16(pb); /* channel count */
2182  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
2183  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
2184 
2185  sc->audio_cid = avio_rb16(pb);
2186  avio_rb16(pb); /* packet size = 0 */
2187 
2188  st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
2189 
2190  // Read QT version 1 fields. In version 0 these do not exist.
2191  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
2192  if (!c->isom ||
2193  (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
2194  (sc->stsd_version == 0 && version > 0)) {
2195  if (version == 1) {
2196  sc->samples_per_frame = avio_rb32(pb);
2197  avio_rb32(pb); /* bytes per packet */
2198  sc->bytes_per_frame = avio_rb32(pb);
2199  avio_rb32(pb); /* bytes per sample */
2200  } else if (version == 2) {
2201  avio_rb32(pb); /* sizeof struct only */
2203  st->codecpar->channels = avio_rb32(pb);
2204  avio_rb32(pb); /* always 0x7F000000 */
2206 
2207  flags = avio_rb32(pb); /* lpcm format specific flag */
2208  sc->bytes_per_frame = avio_rb32(pb);
2209  sc->samples_per_frame = avio_rb32(pb);
2210  if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
2211  st->codecpar->codec_id =
2213  flags);
2214  }
2215  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2216  /* can't correctly handle variable sized packet as audio unit */
2217  switch (st->codecpar->codec_id) {
2218  case AV_CODEC_ID_MP2:
2219  case AV_CODEC_ID_MP3:
2221  break;
2222  }
2223  }
2224  }
2225 
2226  if (sc->format == 0) {
2227  if (st->codecpar->bits_per_coded_sample == 8)
2228  st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
2229  else if (st->codecpar->bits_per_coded_sample == 16)
2230  st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
2231  }
2232 
2233  switch (st->codecpar->codec_id) {
2234  case AV_CODEC_ID_PCM_S8:
2235  case AV_CODEC_ID_PCM_U8:
2236  if (st->codecpar->bits_per_coded_sample == 16)
2238  break;
2239  case AV_CODEC_ID_PCM_S16LE:
2240  case AV_CODEC_ID_PCM_S16BE:
2241  if (st->codecpar->bits_per_coded_sample == 8)
2243  else if (st->codecpar->bits_per_coded_sample == 24)
2244  st->codecpar->codec_id =
2247  else if (st->codecpar->bits_per_coded_sample == 32)
2248  st->codecpar->codec_id =
2251  break;
2252  /* set values for old format before stsd version 1 appeared */
2253  case AV_CODEC_ID_MACE3:
2254  sc->samples_per_frame = 6;
2255  sc->bytes_per_frame = 2 * st->codecpar->channels;
2256  break;
2257  case AV_CODEC_ID_MACE6:
2258  sc->samples_per_frame = 6;
2259  sc->bytes_per_frame = 1 * st->codecpar->channels;
2260  break;
2262  sc->samples_per_frame = 64;
2263  sc->bytes_per_frame = 34 * st->codecpar->channels;
2264  break;
2265  case AV_CODEC_ID_GSM:
2266  sc->samples_per_frame = 160;
2267  sc->bytes_per_frame = 33;
2268  break;
2269  default:
2270  break;
2271  }
2272 
2273  bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2274  if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->channels <= INT_MAX) {
2275  st->codecpar->bits_per_coded_sample = bits_per_sample;
2276  sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
2277  }
2278 }
2279 
2281  AVStream *st, MOVStreamContext *sc,
2282  int64_t size)
2283 {
2284  // ttxt stsd contains display flags, justification, background
2285  // color, fonts, and default styles, so fake an atom to read it
2286  MOVAtom fake_atom = { .size = size };
2287  // mp4s contains a regular esds atom
2288  if (st->codecpar->codec_tag != AV_RL32("mp4s"))
2289  mov_read_glbl(c, pb, fake_atom);
2290  st->codecpar->width = sc->width;
2291  st->codecpar->height = sc->height;
2292 }
2293 
2294 static uint32_t yuv_to_rgba(uint32_t ycbcr)
2295 {
2296  uint8_t r, g, b;
2297  int y, cb, cr;
2298 
2299  y = (ycbcr >> 16) & 0xFF;
2300  cr = (ycbcr >> 8) & 0xFF;
2301  cb = ycbcr & 0xFF;
2302 
2303  b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
2304  g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
2305  r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
2306 
2307  return (r << 16) | (g << 8) | b;
2308 }
2309 
2311 {
2312  char buf[256] = {0};
2313  uint8_t *src = st->codecpar->extradata;
2314  int i, ret;
2315 
2316  if (st->codecpar->extradata_size != 64)
2317  return 0;
2318 
2319  if (st->codecpar->width > 0 && st->codecpar->height > 0)
2320  snprintf(buf, sizeof(buf), "size: %dx%d\n",
2321  st->codecpar->width, st->codecpar->height);
2322  av_strlcat(buf, "palette: ", sizeof(buf));
2323 
2324  for (i = 0; i < 16; i++) {
2325  uint32_t yuv = AV_RB32(src + i * 4);
2326  uint32_t rgba = yuv_to_rgba(yuv);
2327 
2328  av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
2329  }
2330 
2331  if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
2332  return 0;
2333 
2334  ret = ff_alloc_extradata(st->codecpar, strlen(buf));
2335  if (ret < 0)
2336  return ret;
2337  memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
2338 
2339  return 0;
2340 }
2341 
2343  AVStream *st, MOVStreamContext *sc,
2344  int64_t size)
2345 {
2346  int ret;
2347 
2348  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2349  if ((int)size != size)
2350  return AVERROR(ENOMEM);
2351 
2352  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2353  if (ret < 0)
2354  return ret;
2355  if (size > 16) {
2356  MOVStreamContext *tmcd_ctx = st->priv_data;
2357  int val;
2358  val = AV_RB32(st->codecpar->extradata + 4);
2359  tmcd_ctx->tmcd_flags = val;
2360  st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
2361  st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
2362 #if FF_API_LAVF_AVCTX
2364  st->codec->time_base = av_inv_q(st->avg_frame_rate);
2366 #endif
2367  if (size > 30) {
2368  uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2369  uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2370  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2371  uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2372  if (str_size > 0 && size >= (int)str_size + 30 &&
2373  st->codecpar->extradata[30] /* Don't add empty string */) {
2374  char *reel_name = av_malloc(str_size + 1);
2375  if (!reel_name)
2376  return AVERROR(ENOMEM);
2377  memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2378  reel_name[str_size] = 0; /* Add null terminator */
2379  av_dict_set(&st->metadata, "reel_name", reel_name,
2381  }
2382  }
2383  }
2384  }
2385  } else {
2386  /* other codec type, just skip (rtp, mp4s ...) */
2387  avio_skip(pb, size);
2388  }
2389  return 0;
2390 }
2391 
2393  AVStream *st, MOVStreamContext *sc)
2394 {
2395  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2396  !st->codecpar->sample_rate && sc->time_scale > 1)
2397  st->codecpar->sample_rate = sc->time_scale;
2398 
2399  /* special codec parameters handling */
2400  switch (st->codecpar->codec_id) {
2401 #if CONFIG_DV_DEMUXER
2402  case AV_CODEC_ID_DVAUDIO:
2403  c->dv_fctx = avformat_alloc_context();
2404  if (!c->dv_fctx) {
2405  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2406  return AVERROR(ENOMEM);
2407  }
2408  c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2409  if (!c->dv_demux) {
2410  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2411  return AVERROR(ENOMEM);
2412  }
2413  sc->dv_audio_container = 1;
2415  break;
2416 #endif
2417  /* no ifdef since parameters are always those */
2418  case AV_CODEC_ID_QCELP:
2419  st->codecpar->channels = 1;
2420  // force sample rate for qcelp when not stored in mov
2421  if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2422  st->codecpar->sample_rate = 8000;
2423  // FIXME: Why is the following needed for some files?
2424  sc->samples_per_frame = 160;
2425  if (!sc->bytes_per_frame)
2426  sc->bytes_per_frame = 35;
2427  break;
2428  case AV_CODEC_ID_AMR_NB:
2429  st->codecpar->channels = 1;
2430  /* force sample rate for amr, stsd in 3gp does not store sample rate */
2431  st->codecpar->sample_rate = 8000;
2432  break;
2433  case AV_CODEC_ID_AMR_WB:
2434  st->codecpar->channels = 1;
2435  st->codecpar->sample_rate = 16000;
2436  break;
2437  case AV_CODEC_ID_MP2:
2438  case AV_CODEC_ID_MP3:
2439  /* force type after stsd for m1a hdlr */
2441  break;
2442  case AV_CODEC_ID_GSM:
2443  case AV_CODEC_ID_ADPCM_MS:
2445  case AV_CODEC_ID_ILBC:
2446  case AV_CODEC_ID_MACE3:
2447  case AV_CODEC_ID_MACE6:
2448  case AV_CODEC_ID_QDM2:
2450  break;
2451  case AV_CODEC_ID_ALAC:
2452  if (st->codecpar->extradata_size == 36) {
2453  st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
2454  st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2455  }
2456  break;
2457  case AV_CODEC_ID_AC3:
2458  case AV_CODEC_ID_EAC3:
2460  case AV_CODEC_ID_VC1:
2461  case AV_CODEC_ID_VP8:
2462  case AV_CODEC_ID_VP9:
2464  break;
2465  case AV_CODEC_ID_AV1:
2467  break;
2468  default:
2469  break;
2470  }
2471  return 0;
2472 }
2473 
2475  int codec_tag, int format,
2476  int64_t size)
2477 {
2478  int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2479 
2480  if (codec_tag &&
2481  (codec_tag != format &&
2482  // AVID 1:1 samples with differing data format and codec tag exist
2483  (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
2484  // prores is allowed to have differing data format and codec tag
2485  codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
2486  // so is dv (sigh)
2487  codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
2488  (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2489  : codec_tag != MKTAG('j','p','e','g')))) {
2490  /* Multiple fourcc, we skip JPEG. This is not correct, we should
2491  * export it as a separate AVStream but this needs a few changes
2492  * in the MOV demuxer, patch welcome. */
2493 
2494  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2495  avio_skip(pb, size);
2496  return 1;
2497  }
2498 
2499  return 0;
2500 }
2501 
2503 {
2504  AVStream *st;
2505  MOVStreamContext *sc;
2506  int pseudo_stream_id;
2507 
2508  av_assert0 (c->fc->nb_streams >= 1);
2509  st = c->fc->streams[c->fc->nb_streams-1];
2510  sc = st->priv_data;
2511 
2512  for (pseudo_stream_id = 0;
2513  pseudo_stream_id < entries && !pb->eof_reached;
2514  pseudo_stream_id++) {
2515  //Parsing Sample description table
2516  enum AVCodecID id;
2517  int ret, dref_id = 1;
2518  MOVAtom a = { AV_RL32("stsd") };
2519  int64_t start_pos = avio_tell(pb);
2520  int64_t size = avio_rb32(pb); /* size */
2521  uint32_t format = avio_rl32(pb); /* data format */
2522 
2523  if (size >= 16) {
2524  avio_rb32(pb); /* reserved */
2525  avio_rb16(pb); /* reserved */
2526  dref_id = avio_rb16(pb);
2527  } else if (size <= 7) {
2528  av_log(c->fc, AV_LOG_ERROR,
2529  "invalid size %"PRId64" in stsd\n", size);
2530  return AVERROR_INVALIDDATA;
2531  }
2532 
2534  size - (avio_tell(pb) - start_pos))) {
2535  sc->stsd_count++;
2536  continue;
2537  }
2538 
2539  sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2540  sc->dref_id= dref_id;
2541  sc->format = format;
2542 
2543  id = mov_codec_id(st, format);
2544 
2545  av_log(c->fc, AV_LOG_TRACE,
2546  "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
2548 
2549  st->codecpar->codec_id = id;
2551  mov_parse_stsd_video(c, pb, st, sc);
2552  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
2553  mov_parse_stsd_audio(c, pb, st, sc);
2554  if (st->codecpar->sample_rate < 0) {
2555  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
2556  return AVERROR_INVALIDDATA;
2557  }
2558  if (st->codecpar->channels < 0) {
2559  av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->channels);
2560  return AVERROR_INVALIDDATA;
2561  }
2562  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
2563  mov_parse_stsd_subtitle(c, pb, st, sc,
2564  size - (avio_tell(pb) - start_pos));
2565  } else {
2566  ret = mov_parse_stsd_data(c, pb, st, sc,
2567  size - (avio_tell(pb) - start_pos));
2568  if (ret < 0)
2569  return ret;
2570  }
2571  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2572  a.size = size - (avio_tell(pb) - start_pos);
2573  if (a.size > 8) {
2574  if ((ret = mov_read_default(c, pb, a)) < 0)
2575  return ret;
2576  } else if (a.size > 0)
2577  avio_skip(pb, a.size);
2578 
2579  if (sc->extradata && st->codecpar->extradata) {
2580  int extra_size = st->codecpar->extradata_size;
2581 
2582  /* Move the current stream extradata to the stream context one. */
2583  sc->extradata_size[pseudo_stream_id] = extra_size;
2584  sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
2585  st->codecpar->extradata = NULL;
2586  st->codecpar->extradata_size = 0;
2587  }
2588  sc->stsd_count++;
2589  }
2590 
2591  if (pb->eof_reached) {
2592  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
2593  return AVERROR_EOF;
2594  }
2595 
2596  return 0;
2597 }
2598 
2600 {
2601  AVStream *st;
2602  MOVStreamContext *sc;
2603  int ret, entries;
2604 
2605  if (c->fc->nb_streams < 1)
2606  return 0;
2607  st = c->fc->streams[c->fc->nb_streams - 1];
2608  sc = st->priv_data;
2609 
2610  sc->stsd_version = avio_r8(pb);
2611  avio_rb24(pb); /* flags */
2612  entries = avio_rb32(pb);
2613 
2614  /* Each entry contains a size (4 bytes) and format (4 bytes). */
2615  if (entries <= 0 || entries > atom.size / 8 || entries > 1024) {
2616  av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
2617  return AVERROR_INVALIDDATA;
2618  }
2619 
2620  if (sc->extradata) {
2621  av_log(c->fc, AV_LOG_ERROR,
2622  "Duplicate stsd found in this track.\n");
2623  return AVERROR_INVALIDDATA;
2624  }
2625 
2626  /* Prepare space for hosting multiple extradata. */
2627  sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
2628  if (!sc->extradata)
2629  return AVERROR(ENOMEM);
2630 
2631  sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
2632  if (!sc->extradata_size) {
2633  ret = AVERROR(ENOMEM);
2634  goto fail;
2635  }
2636 
2637  ret = ff_mov_read_stsd_entries(c, pb, entries);
2638  if (ret < 0)
2639  goto fail;
2640 
2641  /* Restore back the primary extradata. */
2642  av_freep(&st->codecpar->extradata);
2643  st->codecpar->extradata_size = sc->extradata_size[0];
2644  if (sc->extradata_size[0]) {
2646  if (!st->codecpar->extradata)
2647  return AVERROR(ENOMEM);
2648  memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
2649  }
2650 
2651  return mov_finalize_stsd_codec(c, pb, st, sc);
2652 fail:
2653  if (sc->extradata) {
2654  int j;
2655  for (j = 0; j < sc->stsd_count; j++)
2656  av_freep(&sc->extradata[j]);
2657  }
2658 
2659  av_freep(&sc->extradata);
2660  av_freep(&sc->extradata_size);
2661  return ret;
2662 }
2663 
2665 {
2666  AVStream *st;
2667  MOVStreamContext *sc;
2668  unsigned int i, entries;
2669 
2670  if (c->fc->nb_streams < 1)
2671  return 0;
2672  st = c->fc->streams[c->fc->nb_streams-1];
2673  sc = st->priv_data;
2674 
2675  avio_r8(pb); /* version */
2676  avio_rb24(pb); /* flags */
2677 
2678  entries = avio_rb32(pb);
2679  if ((uint64_t)entries * 12 + 4 > atom.size)
2680  return AVERROR_INVALIDDATA;
2681 
2682  av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
2683 
2684  if (!entries)
2685  return 0;
2686  if (sc->stsc_data) {
2687  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STSC atom\n");
2688  return 0;
2689  }
2690  av_free(sc->stsc_data);
2691  sc->stsc_count = 0;
2692  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2693  if (!sc->stsc_data)
2694  return AVERROR(ENOMEM);
2695 
2696  for (i = 0; i < entries && !pb->eof_reached; i++) {
2697  sc->stsc_data[i].first = avio_rb32(pb);
2698  sc->stsc_data[i].count = avio_rb32(pb);
2699  sc->stsc_data[i].id = avio_rb32(pb);
2700  }
2701 
2702  sc->stsc_count = i;
2703  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2704  int64_t first_min = i + 1;
2705  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2706  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2707  sc->stsc_data[i].first < first_min ||
2708  sc->stsc_data[i].count < 1 ||
2709  sc->stsc_data[i].id < 1) {
2710  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);
2711  if (i+1 >= sc->stsc_count) {
2712  if (sc->stsc_data[i].count == 0 && i > 0) {
2713  sc->stsc_count --;
2714  continue;
2715  }
2716  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2717  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2718  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2719  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2720  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
2721  continue;
2722  }
2723  av_assert0(sc->stsc_data[i+1].first >= 2);
2724  // We replace this entry by the next valid
2725  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2726  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2727  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
2728  }
2729  }
2730 
2731  if (pb->eof_reached) {
2732  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
2733  return AVERROR_EOF;
2734  }
2735 
2736  return 0;
2737 }
2738 
2739 static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
2740 {
2741  return index < count - 1;
2742 }
2743 
2744 /* Compute the samples value for the stsc entry at the given index. */
2745 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
2746 {
2747  int chunk_count;
2748 
2750  chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2751  else {
2752  // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
2754  chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2755  }
2756 
2757  return sc->stsc_data[index].count * (int64_t)chunk_count;
2758 }
2759 
2761 {
2762  AVStream *st;
2763  MOVStreamContext *sc;
2764  unsigned i, entries;
2765 
2766  if (c->fc->nb_streams < 1)
2767  return 0;
2768  st = c->fc->streams[c->fc->nb_streams-1];
2769  sc = st->priv_data;
2770 
2771  avio_rb32(pb); // version + flags
2772 
2773  entries = avio_rb32(pb);
2774  if (sc->stps_data)
2775  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2776  av_free(sc->stps_data);
2777  sc->stps_count = 0;
2778  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2779  if (!sc->stps_data)
2780  return AVERROR(ENOMEM);
2781 
2782  for (i = 0; i < entries && !pb->eof_reached; i++) {
2783  sc->stps_data[i] = avio_rb32(pb);
2784  }
2785 
2786  sc->stps_count = i;
2787 
2788  if (pb->eof_reached) {
2789  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
2790  return AVERROR_EOF;
2791  }
2792 
2793  return 0;
2794 }
2795 
2797 {
2798  AVStream *st;
2799  MOVStreamContext *sc;
2800  unsigned int i, entries;
2801 
2802  if (c->fc->nb_streams < 1)
2803  return 0;
2804  st = c->fc->streams[c->fc->nb_streams-1];
2805  sc = st->priv_data;
2806 
2807  avio_r8(pb); /* version */
2808  avio_rb24(pb); /* flags */
2809 
2810  entries = avio_rb32(pb);
2811 
2812  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
2813 
2814  if (!entries) {
2815  sc->keyframe_absent = 1;
2816  if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2818  return 0;
2819  }
2820  if (sc->keyframes)
2821  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2822  if (entries >= UINT_MAX / sizeof(int))
2823  return AVERROR_INVALIDDATA;
2824  av_freep(&sc->keyframes);
2825  sc->keyframe_count = 0;
2826  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2827  if (!sc->keyframes)
2828  return AVERROR(ENOMEM);
2829 
2830  for (i = 0; i < entries && !pb->eof_reached; i++) {
2831  sc->keyframes[i] = avio_rb32(pb);
2832  }
2833 
2834  sc->keyframe_count = i;
2835 
2836  if (pb->eof_reached) {
2837  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
2838  return AVERROR_EOF;
2839  }
2840 
2841  return 0;
2842 }
2843 
2845 {
2846  AVStream *st;
2847  MOVStreamContext *sc;
2848  unsigned int i, entries, sample_size, field_size, num_bytes;
2849  GetBitContext gb;
2850  unsigned char* buf;
2851  int ret;
2852 
2853  if (c->fc->nb_streams < 1)
2854  return 0;
2855  st = c->fc->streams[c->fc->nb_streams-1];
2856  sc = st->priv_data;
2857 
2858  avio_r8(pb); /* version */
2859  avio_rb24(pb); /* flags */
2860 
2861  if (atom.type == MKTAG('s','t','s','z')) {
2862  sample_size = avio_rb32(pb);
2863  if (!sc->sample_size) /* do not overwrite value computed in stsd */
2864  sc->sample_size = sample_size;
2865  sc->stsz_sample_size = sample_size;
2866  field_size = 32;
2867  } else {
2868  sample_size = 0;
2869  avio_rb24(pb); /* reserved */
2870  field_size = avio_r8(pb);
2871  }
2872  entries = avio_rb32(pb);
2873 
2874  av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
2875 
2876  sc->sample_count = entries;
2877  if (sample_size)
2878  return 0;
2879 
2880  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2881  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
2882  return AVERROR_INVALIDDATA;
2883  }
2884 
2885  if (!entries)
2886  return 0;
2887  if (entries >= (UINT_MAX - 4) / field_size)
2888  return AVERROR_INVALIDDATA;
2889  if (sc->sample_sizes)
2890  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2891  av_free(sc->sample_sizes);
2892  sc->sample_count = 0;
2893  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2894  if (!sc->sample_sizes)
2895  return AVERROR(ENOMEM);
2896 
2897  num_bytes = (entries*field_size+4)>>3;
2898 
2899  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2900  if (!buf) {
2901  av_freep(&sc->sample_sizes);
2902  return AVERROR(ENOMEM);
2903  }
2904 
2905  ret = ffio_read_size(pb, buf, num_bytes);
2906  if (ret < 0) {
2907  av_freep(&sc->sample_sizes);
2908  av_free(buf);
2909  av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
2910  return 0;
2911  }
2912 
2913  init_get_bits(&gb, buf, 8*num_bytes);
2914 
2915  for (i = 0; i < entries && !pb->eof_reached; i++) {
2916  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2917  if (sc->sample_sizes[i] < 0) {
2918  av_free(buf);
2919  av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
2920  return AVERROR_INVALIDDATA;
2921  }
2922  sc->data_size += sc->sample_sizes[i];
2923  }
2924 
2925  sc->sample_count = i;
2926 
2927  av_free(buf);
2928 
2929  if (pb->eof_reached) {
2930  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSZ atom\n");
2931  return AVERROR_EOF;
2932  }
2933 
2934  return 0;
2935 }
2936 
2938 {
2939  AVStream *st;
2940  MOVStreamContext *sc;
2941  unsigned int i, entries, alloc_size = 0;
2942  int64_t duration = 0;
2943  int64_t total_sample_count = 0;
2944 
2945  if (c->fc->nb_streams < 1)
2946  return 0;
2947  st = c->fc->streams[c->fc->nb_streams-1];
2948  sc = st->priv_data;
2949 
2950  avio_r8(pb); /* version */
2951  avio_rb24(pb); /* flags */
2952  entries = avio_rb32(pb);
2953 
2954  av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
2955  c->fc->nb_streams-1, entries);
2956 
2957  if (sc->stts_data)
2958  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2959  av_freep(&sc->stts_data);
2960  sc->stts_count = 0;
2961  if (entries >= INT_MAX / sizeof(*sc->stts_data))
2962  return AVERROR(ENOMEM);
2963 
2964  for (i = 0; i < entries && !pb->eof_reached; i++) {
2965  int sample_duration;
2966  unsigned int sample_count;
2967  unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
2968  MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
2969  min_entries * sizeof(*sc->stts_data));
2970  if (!stts_data) {
2971  av_freep(&sc->stts_data);
2972  sc->stts_count = 0;
2973  return AVERROR(ENOMEM);
2974  }
2975  sc->stts_count = min_entries;
2976  sc->stts_data = stts_data;
2977 
2978  sample_count = avio_rb32(pb);
2979  sample_duration = avio_rb32(pb);
2980 
2981  sc->stts_data[i].count= sample_count;
2982  sc->stts_data[i].duration= sample_duration;
2983 
2984  av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2985  sample_count, sample_duration);
2986 
2987  duration+=(int64_t)sample_duration*(uint64_t)sample_count;
2988  total_sample_count+=sample_count;
2989  }
2990 
2991  sc->stts_count = i;
2992 
2993  if (duration > 0 &&
2994  duration <= INT64_MAX - sc->duration_for_fps &&
2995  total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
2996  sc->duration_for_fps += duration;
2997  sc->nb_frames_for_fps += total_sample_count;
2998  }
2999 
3000  if (pb->eof_reached) {
3001  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
3002  return AVERROR_EOF;
3003  }
3004 
3005  st->nb_frames= total_sample_count;
3006  if (duration)
3007  st->duration= FFMIN(st->duration, duration);
3008  sc->track_end = duration;
3009  return 0;
3010 }
3011 
3013 {
3014  AVStream *st;
3015  MOVStreamContext *sc;
3016  int64_t i, entries;
3017 
3018  if (c->fc->nb_streams < 1)
3019  return 0;
3020  st = c->fc->streams[c->fc->nb_streams - 1];
3021  sc = st->priv_data;
3022 
3023  avio_r8(pb); /* version */
3024  avio_rb24(pb); /* flags */
3025  entries = atom.size - 4;
3026 
3027  av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
3028  c->fc->nb_streams - 1, entries);
3029 
3030  if (sc->sdtp_data)
3031  av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
3032  av_freep(&sc->sdtp_data);
3033  sc->sdtp_count = 0;
3034 
3035  sc->sdtp_data = av_malloc(entries);
3036  if (!sc->sdtp_data)
3037  return AVERROR(ENOMEM);
3038 
3039  for (i = 0; i < entries && !pb->eof_reached; i++)
3040  sc->sdtp_data[i] = avio_r8(pb);
3041  sc->sdtp_count = i;
3042 
3043  return 0;
3044 }
3045 
3046 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3047 {
3048  if (duration < 0) {
3049  if (duration == INT_MIN) {
3050  av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
3051  duration++;
3052  }
3053  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3054  }
3055 }
3056 
3058 {
3059  AVStream *st;
3060  MOVStreamContext *sc;
3061  unsigned int i, entries, ctts_count = 0;
3062 
3063  if (c->fc->nb_streams < 1)
3064  return 0;
3065  st = c->fc->streams[c->fc->nb_streams-1];
3066  sc = st->priv_data;
3067 
3068  avio_r8(pb); /* version */
3069  avio_rb24(pb); /* flags */
3070  entries = avio_rb32(pb);
3071 
3072  av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
3073 
3074  if (!entries)
3075  return 0;
3076  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3077  return AVERROR_INVALIDDATA;
3078  av_freep(&sc->ctts_data);
3079  sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3080  if (!sc->ctts_data)
3081  return AVERROR(ENOMEM);
3082 
3083  for (i = 0; i < entries && !pb->eof_reached; i++) {
3084  int count = avio_rb32(pb);
3085  int duration = avio_rb32(pb);
3086 
3087  if (count <= 0) {
3088  av_log(c->fc, AV_LOG_TRACE,
3089  "ignoring CTTS entry with count=%d duration=%d\n",
3090  count, duration);
3091  continue;
3092  }
3093 
3094  add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
3095  count, duration);
3096 
3097  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
3098  count, duration);
3099 
3100  if (FFNABS(duration) < -(1<<28) && i+2<entries) {
3101  av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
3102  av_freep(&sc->ctts_data);
3103  sc->ctts_count = 0;
3104  return 0;
3105  }
3106 
3107  if (i+2<entries)
3108  mov_update_dts_shift(sc, duration, c->fc);
3109  }
3110 
3111  sc->ctts_count = ctts_count;
3112 
3113  if (pb->eof_reached) {
3114  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
3115  return AVERROR_EOF;
3116  }
3117 
3118  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3119 
3120  return 0;
3121 }
3122 
3124 {
3125  AVStream *st;
3126  MOVStreamContext *sc;
3127  unsigned int i, entries;
3128  uint8_t version;
3129  uint32_t grouping_type;
3130 
3131  if (c->fc->nb_streams < 1)
3132  return 0;
3133  st = c->fc->streams[c->fc->nb_streams-1];
3134  sc = st->priv_data;
3135 
3136  version = avio_r8(pb); /* version */
3137  avio_rb24(pb); /* flags */
3138  grouping_type = avio_rl32(pb);
3139  if (grouping_type != MKTAG( 'r','a','p',' '))
3140  return 0; /* only support 'rap ' grouping */
3141  if (version == 1)
3142  avio_rb32(pb); /* grouping_type_parameter */
3143 
3144  entries = avio_rb32(pb);
3145  if (!entries)
3146  return 0;
3147  if (sc->rap_group)
3148  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
3149  av_free(sc->rap_group);
3150  sc->rap_group_count = 0;
3151  sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
3152  if (!sc->rap_group)
3153  return AVERROR(ENOMEM);
3154 
3155  for (i = 0; i < entries && !pb->eof_reached; i++) {
3156  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
3157  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
3158  }
3159 
3160  sc->rap_group_count = i;
3161 
3162  if (pb->eof_reached) {
3163  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
3164  return AVERROR_EOF;
3165  }
3166 
3167  return 0;
3168 }
3169 
3170 /**
3171  * Get ith edit list entry (media time, duration).
3172  */
3174  const MOVStreamContext *msc,
3175  unsigned int edit_list_index,
3176  int64_t *edit_list_media_time,
3177  int64_t *edit_list_duration,
3178  int64_t global_timescale)
3179 {
3180  if (edit_list_index == msc->elst_count) {
3181  return 0;
3182  }
3183  *edit_list_media_time = msc->elst_data[edit_list_index].time;
3184  *edit_list_duration = msc->elst_data[edit_list_index].duration;
3185 
3186  /* duration is in global timescale units;convert to msc timescale */
3187  if (global_timescale == 0) {
3188  avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
3189  return 0;
3190  }
3191  *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
3192  global_timescale);
3193  return 1;
3194 }
3195 
3196 /**
3197  * Find the closest previous frame to the timestamp_pts, in e_old index
3198  * entries. Searching for just any frame / just key frames can be controlled by
3199  * last argument 'flag'.
3200  * Note that if ctts_data is not NULL, we will always search for a key frame
3201  * irrespective of the value of 'flag'. If we don't find any keyframe, we will
3202  * return the first frame of the video.
3203  *
3204  * Here the timestamp_pts is considered to be a presentation timestamp and
3205  * the timestamp of index entries are considered to be decoding timestamps.
3206  *
3207  * Returns 0 if successful in finding a frame, else returns -1.
3208  * Places the found index corresponding output arg.
3209  *
3210  * If ctts_old is not NULL, then refines the searched entry by searching
3211  * backwards from the found timestamp, to find the frame with correct PTS.
3212  *
3213  * Places the found ctts_index and ctts_sample in corresponding output args.
3214  */
3216  AVIndexEntry *e_old,
3217  int nb_old,
3218  MOVStts* ctts_data,
3219  int64_t ctts_count,
3220  int64_t timestamp_pts,
3221  int flag,
3222  int64_t* index,
3223  int64_t* ctts_index,
3224  int64_t* ctts_sample)
3225 {
3226  MOVStreamContext *msc = st->priv_data;
3227  AVIndexEntry *e_keep = st->index_entries;
3228  int nb_keep = st->nb_index_entries;
3229  int64_t i = 0;
3230  int64_t index_ctts_count;
3231 
3232  av_assert0(index);
3233 
3234  // If dts_shift > 0, then all the index timestamps will have to be offset by
3235  // at least dts_shift amount to obtain PTS.
3236  // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
3237  if (msc->dts_shift > 0) {
3238  timestamp_pts -= msc->dts_shift;
3239  }
3240 
3241  st->index_entries = e_old;
3242  st->nb_index_entries = nb_old;
3243  *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
3244 
3245  // Keep going backwards in the index entries until the timestamp is the same.
3246  if (*index >= 0) {
3247  for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
3248  i--) {
3249  if ((flag & AVSEEK_FLAG_ANY) ||
3250  (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
3251  *index = i - 1;
3252  }
3253  }
3254  }
3255 
3256  // If we have CTTS then refine the search, by searching backwards over PTS
3257  // computed by adding corresponding CTTS durations to index timestamps.
3258  if (ctts_data && *index >= 0) {
3259  av_assert0(ctts_index);
3260  av_assert0(ctts_sample);
3261  // Find out the ctts_index for the found frame.
3262  *ctts_index = 0;
3263  *ctts_sample = 0;
3264  for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
3265  if (*ctts_index < ctts_count) {
3266  (*ctts_sample)++;
3267  if (ctts_data[*ctts_index].count == *ctts_sample) {
3268  (*ctts_index)++;
3269  *ctts_sample = 0;
3270  }
3271  }
3272  }
3273 
3274  while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
3275  // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
3276  // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
3277  // compensated by dts_shift above.
3278  if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
3279  (e_old[*index].flags & AVINDEX_KEYFRAME)) {
3280  break;
3281  }
3282 
3283  (*index)--;
3284  if (*ctts_sample == 0) {
3285  (*ctts_index)--;
3286  if (*ctts_index >= 0)
3287  *ctts_sample = ctts_data[*ctts_index].count - 1;
3288  } else {
3289  (*ctts_sample)--;
3290  }
3291  }
3292  }
3293 
3294  /* restore AVStream state*/
3295  st->index_entries = e_keep;
3296  st->nb_index_entries = nb_keep;
3297  return *index >= 0 ? 0 : -1;
3298 }
3299 
3300 /**
3301  * Add index entry with the given values, to the end of st->index_entries.
3302  * Returns the new size st->index_entries if successful, else returns -1.
3303  *
3304  * This function is similar to ff_add_index_entry in libavformat/utils.c
3305  * except that here we are always unconditionally adding an index entry to
3306  * the end, instead of searching the entries list and skipping the add if
3307  * there is an existing entry with the same timestamp.
3308  * This is needed because the mov_fix_index calls this func with the same
3309  * unincremented timestamp for successive discarded frames.
3310  */
3311 static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
3312  int size, int distance, int flags)
3313 {
3314  AVIndexEntry *entries, *ie;
3315  int64_t index = -1;
3316  const size_t min_size_needed = (st->nb_index_entries + 1) * sizeof(AVIndexEntry);
3317 
3318  // Double the allocation each time, to lower memory fragmentation.
3319  // Another difference from ff_add_index_entry function.
3320  const size_t requested_size =
3321  min_size_needed > st->index_entries_allocated_size ?
3322  FFMAX(min_size_needed, 2 * st->index_entries_allocated_size) :
3323  min_size_needed;
3324 
3325  if (st->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
3326  return -1;
3327 
3328  entries = av_fast_realloc(st->index_entries,
3330  requested_size);
3331  if (!entries)
3332  return -1;
3333 
3334  st->index_entries= entries;
3335 
3336  index= st->nb_index_entries++;
3337  ie= &entries[index];
3338 
3339  ie->pos = pos;
3340  ie->timestamp = timestamp;
3341  ie->min_distance= distance;
3342  ie->size= size;
3343  ie->flags = flags;
3344  return index;
3345 }
3346 
3347 /**
3348  * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
3349  * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
3350  */
3351 static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
3352  int64_t* frame_duration_buffer,
3353  int frame_duration_buffer_size) {
3354  int i = 0;
3355  av_assert0(end_index >= 0 && end_index <= st->nb_index_entries);
3356  for (i = 0; i < frame_duration_buffer_size; i++) {
3357  end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
3358  st->index_entries[end_index - 1 - i].timestamp = end_ts;
3359  }
3360 }
3361 
3362 /**
3363  * Append a new ctts entry to ctts_data.
3364  * Returns the new ctts_count if successful, else returns -1.
3365  */
3366 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
3367  int count, int duration)
3368 {
3369  MOVStts *ctts_buf_new;
3370  const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
3371  const size_t requested_size =
3372  min_size_needed > *allocated_size ?
3373  FFMAX(min_size_needed, 2 * (*allocated_size)) :
3374  min_size_needed;
3375 
3376  if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1)
3377  return -1;
3378 
3379  ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
3380 
3381  if (!ctts_buf_new)
3382  return -1;
3383 
3384  *ctts_data = ctts_buf_new;
3385 
3386  ctts_buf_new[*ctts_count].count = count;
3387  ctts_buf_new[*ctts_count].duration = duration;
3388 
3389  *ctts_count = (*ctts_count) + 1;
3390  return *ctts_count;
3391 }
3392 
3393 #define MAX_REORDER_DELAY 16
3395 {
3396  MOVStreamContext *msc = st->priv_data;
3397  int ind;
3398  int ctts_ind = 0;
3399  int ctts_sample = 0;
3400  int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
3401  int buf_start = 0;
3402  int j, r, num_swaps;
3403 
3404  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
3405  pts_buf[j] = INT64_MIN;
3406 
3407  if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
3409  st->codecpar->video_delay = 0;
3410  for (ind = 0; ind < st->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
3411  // Point j to the last elem of the buffer and insert the current pts there.
3412  j = buf_start;
3413  buf_start = (buf_start + 1);
3414  if (buf_start == MAX_REORDER_DELAY + 1)
3415  buf_start = 0;
3416 
3417  pts_buf[j] = st->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
3418 
3419  // The timestamps that are already in the sorted buffer, and are greater than the
3420  // current pts, are exactly the timestamps that need to be buffered to output PTS
3421  // in correct sorted order.
3422  // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
3423  // can be computed as the maximum no. of swaps any particular timestamp needs to
3424  // go through, to keep this buffer in sorted order.
3425  num_swaps = 0;
3426  while (j != buf_start) {
3427  r = j - 1;
3428  if (r < 0) r = MAX_REORDER_DELAY;
3429  if (pts_buf[j] < pts_buf[r]) {
3430  FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
3431  ++num_swaps;
3432  } else {
3433  break;
3434  }
3435  j = r;
3436  }
3437  st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
3438 
3439  ctts_sample++;
3440  if (ctts_sample == msc->ctts_data[ctts_ind].count) {
3441  ctts_ind++;
3442  ctts_sample = 0;
3443  }
3444  }
3445  av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
3446  st->codecpar->video_delay, st->index);
3447  }
3448 }
3449 
3451 {
3452  sc->current_sample++;
3453  sc->current_index++;
3454  if (sc->index_ranges &&
3455  sc->current_index >= sc->current_index_range->end &&
3456  sc->current_index_range->end) {
3457  sc->current_index_range++;
3459  }
3460 }
3461 
3463 {
3464  sc->current_sample--;
3465  sc->current_index--;
3466  if (sc->index_ranges &&
3468  sc->current_index_range > sc->index_ranges) {
3469  sc->current_index_range--;
3470  sc->current_index = sc->current_index_range->end - 1;
3471  }
3472 }
3473 
3474 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
3475 {
3476  int64_t range_size;
3477 
3478  sc->current_sample = current_sample;
3479  sc->current_index = current_sample;
3480  if (!sc->index_ranges) {
3481  return;
3482  }
3483 
3484  for (sc->current_index_range = sc->index_ranges;
3485  sc->current_index_range->end;
3486  sc->current_index_range++) {
3487  range_size = sc->current_index_range->end - sc->current_index_range->start;
3488  if (range_size > current_sample) {
3489  sc->current_index = sc->current_index_range->start + current_sample;
3490  break;
3491  }
3492  current_sample -= range_size;
3493  }
3494 }
3495 
3496 /**
3497  * Fix st->index_entries, so that it contains only the entries (and the entries
3498  * which are needed to decode them) that fall in the edit list time ranges.
3499  * Also fixes the timestamps of the index entries to match the timeline
3500  * specified the edit lists.
3501  */
3502 static void mov_fix_index(MOVContext *mov, AVStream *st)
3503 {
3504  MOVStreamContext *msc = st->priv_data;
3505  AVIndexEntry *e_old = st->index_entries;
3506  int nb_old = st->nb_index_entries;
3507  const AVIndexEntry *e_old_end = e_old + nb_old;
3508  const AVIndexEntry *current = NULL;
3509  MOVStts *ctts_data_old = msc->ctts_data;
3510  int64_t ctts_index_old = 0;
3511  int64_t ctts_sample_old = 0;
3512  int64_t ctts_count_old = msc->ctts_count;
3513  int64_t edit_list_media_time = 0;
3514  int64_t edit_list_duration = 0;
3515  int64_t frame_duration = 0;
3516  int64_t edit_list_dts_counter = 0;
3517  int64_t edit_list_dts_entry_end = 0;
3518  int64_t edit_list_start_ctts_sample = 0;
3519  int64_t curr_cts;
3520  int64_t curr_ctts = 0;
3521  int64_t empty_edits_sum_duration = 0;
3522  int64_t edit_list_index = 0;
3523  int64_t index;
3524  int flags;
3525  int64_t start_dts = 0;
3526  int64_t edit_list_start_encountered = 0;
3527  int64_t search_timestamp = 0;
3528  int64_t* frame_duration_buffer = NULL;
3529  int num_discarded_begin = 0;
3530  int first_non_zero_audio_edit = -1;
3531  int packet_skip_samples = 0;
3532  MOVIndexRange *current_index_range;
3533  int i;
3534  int found_keyframe_after_edit = 0;
3535  int found_non_empty_edit = 0;
3536 
3537  if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
3538  return;
3539  }
3540 
3541  // allocate the index ranges array
3542  msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
3543  if (!msc->index_ranges) {
3544  av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
3545  return;
3546  }
3547  msc->current_index_range = msc->index_ranges;
3548  current_index_range = msc->index_ranges - 1;
3549 
3550  // Clean AVStream from traces of old index
3551  st->index_entries = NULL;
3553  st->nb_index_entries = 0;
3554 
3555  // Clean ctts fields of MOVStreamContext
3556  msc->ctts_data = NULL;
3557  msc->ctts_count = 0;
3558  msc->ctts_index = 0;
3559  msc->ctts_sample = 0;
3560  msc->ctts_allocated_size = 0;
3561 
3562  // Reinitialize min_corrected_pts so that it can be computed again.
3563  msc->min_corrected_pts = -1;
3564 
3565  // If the dts_shift is positive (in case of negative ctts values in mov),
3566  // then negate the DTS by dts_shift
3567  if (msc->dts_shift > 0) {
3568  edit_list_dts_entry_end -= msc->dts_shift;
3569  av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
3570  }
3571 
3572  start_dts = edit_list_dts_entry_end;
3573 
3574  while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
3575  &edit_list_duration, mov->time_scale)) {
3576  av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
3577  st->index, edit_list_index, edit_list_media_time, edit_list_duration);
3578  edit_list_index++;
3579  edit_list_dts_counter = edit_list_dts_entry_end;
3580  edit_list_dts_entry_end += edit_list_duration;
3581  num_discarded_begin = 0;
3582  if (!found_non_empty_edit && edit_list_media_time == -1) {
3583  empty_edits_sum_duration += edit_list_duration;
3584  continue;
3585  }
3586  found_non_empty_edit = 1;
3587 
3588  // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
3589  // according to the edit list below.
3590  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3591  if (first_non_zero_audio_edit < 0) {
3592  first_non_zero_audio_edit = 1;
3593  } else {
3594  first_non_zero_audio_edit = 0;
3595  }
3596 
3597  if (first_non_zero_audio_edit > 0)
3598  st->internal->skip_samples = msc->start_pad = 0;
3599  }
3600 
3601  // While reordering frame index according to edit list we must handle properly
3602  // the scenario when edit list entry starts from none key frame.
3603  // We find closest previous key frame and preserve it and consequent frames in index.
3604  // All frames which are outside edit list entry time boundaries will be dropped after decoding.
3605  search_timestamp = edit_list_media_time;
3606  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3607  // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
3608  // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
3609  // edit_list_media_time to cover the decoder delay.
3610  search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
3611  }
3612 
3613  if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, 0,
3614  &index, &ctts_index_old, &ctts_sample_old) < 0) {
3615  av_log(mov->fc, AV_LOG_WARNING,
3616  "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
3617  st->index, edit_list_index, search_timestamp);
3618  if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
3619  &index, &ctts_index_old, &ctts_sample_old) < 0) {
3620  av_log(mov->fc, AV_LOG_WARNING,
3621  "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
3622  st->index, edit_list_index, search_timestamp);
3623  index = 0;
3624  ctts_index_old = 0;
3625  ctts_sample_old = 0;
3626  }
3627  }
3628  current = e_old + index;
3629  edit_list_start_ctts_sample = ctts_sample_old;
3630 
3631  // Iterate over index and arrange it according to edit list
3632  edit_list_start_encountered = 0;
3633  found_keyframe_after_edit = 0;
3634  for (; current < e_old_end; current++, index++) {
3635  // check if frame outside edit list mark it for discard
3636  frame_duration = (current + 1 < e_old_end) ?
3637  ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
3638 
3639  flags = current->flags;
3640 
3641  // frames (pts) before or after edit list
3642  curr_cts = current->timestamp + msc->dts_shift;
3643  curr_ctts = 0;
3644 
3645  if (ctts_data_old && ctts_index_old < ctts_count_old) {
3646  curr_ctts = ctts_data_old[ctts_index_old].duration;
3647  av_log(mov->fc, AV_LOG_DEBUG, "stts: %"PRId64" ctts: %"PRId64", ctts_index: %"PRId64", ctts_count: %"PRId64"\n",
3648  curr_cts, curr_ctts, ctts_index_old, ctts_count_old);
3649  curr_cts += curr_ctts;
3650  ctts_sample_old++;
3651  if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
3652  if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3653  &msc->ctts_allocated_size,
3654  ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3655  ctts_data_old[ctts_index_old].duration) == -1) {
3656  av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3657  ctts_index_old,
3658  ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3659  ctts_data_old[ctts_index_old].duration);
3660  break;
3661  }
3662  ctts_index_old++;
3663  ctts_sample_old = 0;
3664  edit_list_start_ctts_sample = 0;
3665  }
3666  }
3667 
3668  if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
3670  curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
3671  first_non_zero_audio_edit > 0) {
3672  packet_skip_samples = edit_list_media_time - curr_cts;
3673  st->internal->skip_samples += packet_skip_samples;
3674 
3675  // Shift the index entry timestamp by packet_skip_samples to be correct.
3676  edit_list_dts_counter -= packet_skip_samples;
3677  if (edit_list_start_encountered == 0) {
3678  edit_list_start_encountered = 1;
3679  // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
3680  // discarded packets.
3681  if (frame_duration_buffer) {
3682  fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3683  frame_duration_buffer, num_discarded_begin);
3684  av_freep(&frame_duration_buffer);
3685  }
3686  }
3687 
3688  av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
3689  } else {
3691  av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
3692 
3693  if (edit_list_start_encountered == 0) {
3694  num_discarded_begin++;
3695  frame_duration_buffer = av_realloc(frame_duration_buffer,
3696  num_discarded_begin * sizeof(int64_t));
3697  if (!frame_duration_buffer) {
3698  av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
3699  break;
3700  }
3701  frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
3702 
3703  // Increment skip_samples for the first non-zero audio edit list
3704  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3705  first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
3706  st->internal->skip_samples += frame_duration;
3707  }
3708  }
3709  }
3710  } else {
3711  if (msc->min_corrected_pts < 0) {
3712  msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
3713  } else {
3714  msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
3715  }
3716  if (edit_list_start_encountered == 0) {
3717  edit_list_start_encountered = 1;
3718  // Make timestamps strictly monotonically increasing by rewriting timestamps for
3719  // discarded packets.
3720  if (frame_duration_buffer) {
3721  fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3722  frame_duration_buffer, num_discarded_begin);
3723  av_freep(&frame_duration_buffer);
3724  }
3725  }
3726  }
3727 
3728  if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
3729  current->min_distance, flags) == -1) {
3730  av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
3731  break;
3732  }
3733 
3734  // Update the index ranges array
3735  if (current_index_range < msc->index_ranges || index != current_index_range->end) {
3736  current_index_range++;
3737  current_index_range->start = index;
3738  }
3739  current_index_range->end = index + 1;
3740 
3741  // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
3742  if (edit_list_start_encountered > 0) {
3743  edit_list_dts_counter = edit_list_dts_counter + frame_duration;
3744  }
3745 
3746  // Break when found first key frame after edit entry completion
3747  if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
3749  if (ctts_data_old) {
3750  // If we have CTTS and this is the first keyframe after edit elist,
3751  // wait for one more, because there might be trailing B-frames after this I-frame
3752  // that do belong to the edit.
3753  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
3754  found_keyframe_after_edit = 1;
3755  continue;
3756  }
3757  if (ctts_sample_old != 0) {
3758  if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3759  &msc->ctts_allocated_size,
3760  ctts_sample_old - edit_list_start_ctts_sample,
3761  ctts_data_old[ctts_index_old].duration) == -1) {
3762  av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3763  ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
3764  ctts_data_old[ctts_index_old].duration);
3765  break;
3766  }
3767  }
3768  }
3769  break;
3770  }
3771  }
3772  }
3773  // If there are empty edits, then msc->min_corrected_pts might be positive
3774  // intentionally. So we subtract the sum duration of emtpy edits here.
3775  msc->min_corrected_pts -= empty_edits_sum_duration;
3776 
3777  // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
3778  // dts by that amount to make the first pts zero.
3779  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3780  if (msc->min_corrected_pts > 0) {
3781  av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
3782  for (i = 0; i < st->nb_index_entries; ++i) {
3784  }
3785  }
3786  }
3787  // Start time should be equal to zero or the duration of any empty edits.
3788  st->start_time = empty_edits_sum_duration;
3789 
3790  // Update av stream length, if it ends up shorter than the track's media duration
3791  st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
3792  msc->start_pad = st->internal->skip_samples;
3793 
3794  // Free the old index and the old CTTS structures
3795  av_free(e_old);
3796  av_free(ctts_data_old);
3797  av_freep(&frame_duration_buffer);
3798 
3799  // Null terminate the index ranges array
3800  current_index_range++;
3801  current_index_range->start = 0;
3802  current_index_range->end = 0;
3803  msc->current_index = msc->index_ranges[0].start;
3804 }
3805 
3806 static void mov_build_index(MOVContext *mov, AVStream *st)
3807 {
3808  MOVStreamContext *sc = st->priv_data;
3809  int64_t current_offset;
3810  int64_t current_dts = 0;
3811  unsigned int stts_index = 0;
3812  unsigned int stsc_index = 0;
3813  unsigned int stss_index = 0;
3814  unsigned int stps_index = 0;
3815  unsigned int i, j;
3816  uint64_t stream_size = 0;
3817  MOVStts *ctts_data_old = sc->ctts_data;
3818  unsigned int ctts_count_old = sc->ctts_count;
3819 
3820  if (sc->elst_count) {
3821  int i, edit_start_index = 0, multiple_edits = 0;
3822  int64_t empty_duration = 0; // empty duration of the first edit list entry
3823  int64_t start_time = 0; // start time of the media
3824 
3825  for (i = 0; i < sc->elst_count; i++) {
3826  const MOVElst *e = &sc->elst_data[i];
3827  if (i == 0 && e->time == -1) {
3828  /* if empty, the first entry is the start time of the stream
3829  * relative to the presentation itself */
3830  empty_duration = e->duration;
3831  edit_start_index = 1;
3832  } else if (i == edit_start_index && e->time >= 0) {
3833  start_time = e->time;
3834  } else {
3835  multiple_edits = 1;
3836  }
3837  }
3838 
3839  if (multiple_edits && !mov->advanced_editlist)
3840  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
3841  "Use -advanced_editlist to correctly decode otherwise "
3842  "a/v desync might occur\n");
3843 
3844  /* adjust first dts according to edit list */
3845  if ((empty_duration || start_time) && mov->time_scale > 0) {
3846  if (empty_duration)
3847  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
3848 
3849  if (av_sat_sub64(start_time, empty_duration) != start_time - (uint64_t)empty_duration)
3850  av_log(mov->fc, AV_LOG_WARNING, "start_time - empty_duration is not representable\n");
3851 
3852  sc->time_offset = start_time - (uint64_t)empty_duration;
3854  if (!mov->advanced_editlist)
3855  current_dts = -sc->time_offset;
3856  }
3857 
3858  if (!multiple_edits && !mov->advanced_editlist &&
3860  sc->start_pad = start_time;
3861  }
3862 
3863  /* only use old uncompressed audio chunk demuxing when stts specifies it */
3864  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3865  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
3866  unsigned int current_sample = 0;
3867  unsigned int stts_sample = 0;
3868  unsigned int sample_size;
3869  unsigned int distance = 0;
3870  unsigned int rap_group_index = 0;
3871  unsigned int rap_group_sample = 0;
3872  int64_t last_dts = 0;
3873  int64_t dts_correction = 0;
3874  int rap_group_present = sc->rap_group_count && sc->rap_group;
3875  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
3876 
3877  current_dts -= sc->dts_shift;
3878  last_dts = current_dts;
3879 
3880  if (!sc->sample_count || st->nb_index_entries)
3881  return;
3882  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
3883  return;
3885  st->nb_index_entries + sc->sample_count,
3886  sizeof(*st->index_entries)) < 0) {
3887  st->nb_index_entries = 0;
3888  return;
3889  }
3891 
3892  if (ctts_data_old) {
3893  // Expand ctts entries such that we have a 1-1 mapping with samples
3894  if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
3895  return;
3896  sc->ctts_count = 0;
3897  sc->ctts_allocated_size = 0;
3899  sc->sample_count * sizeof(*sc->ctts_data));
3900  if (!sc->ctts_data) {
3901  av_free(ctts_data_old);
3902  return;
3903  }
3904 
3905  memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
3906 
3907  for (i = 0; i < ctts_count_old &&
3908  sc->ctts_count < sc->sample_count; i++)
3909  for (j = 0; j < ctts_data_old[i].count &&
3910  sc->ctts_count < sc->sample_count; j++)
3911  add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
3912  &sc->ctts_allocated_size, 1,
3913  ctts_data_old[i].duration);
3914  av_free(ctts_data_old);
3915  }
3916 
3917  for (i = 0; i < sc->chunk_count; i++) {
3918  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
3919  current_offset = sc->chunk_offsets[i];
3920  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
3921  i + 1 == sc->stsc_data[stsc_index + 1].first)
3922  stsc_index++;
3923 
3924  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
3925  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
3926  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
3927  sc->stsz_sample_size = sc->sample_size;
3928  }
3929  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
3930  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
3931  sc->stsz_sample_size = sc->sample_size;
3932  }
3933 
3934  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
3935  int keyframe = 0;
3936  if (current_sample >= sc->sample_count) {
3937  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
3938  return;
3939  }
3940 
3941  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
3942  keyframe = 1;
3943  if (stss_index + 1 < sc->keyframe_count)
3944  stss_index++;
3945  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
3946  keyframe = 1;
3947  if (stps_index + 1 < sc->stps_count)
3948  stps_index++;
3949  }
3950  if (rap_group_present && rap_group_index < sc->rap_group_count) {
3951  if (sc->rap_group[rap_group_index].index > 0)
3952  keyframe = 1;
3953  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
3954  rap_group_sample = 0;
3955  rap_group_index++;
3956  }
3957  }
3958  if (sc->keyframe_absent
3959  && !sc->stps_count
3960  && !rap_group_present
3961  && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
3962  keyframe = 1;
3963  if (keyframe)
3964  distance = 0;
3965  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
3966  if (sc->pseudo_stream_id == -1 ||
3967  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
3968  AVIndexEntry *e;
3969  if (sample_size > 0x3FFFFFFF) {
3970  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
3971  return;
3972  }
3973  e = &st->index_entries[st->nb_index_entries++];
3974  e->pos = current_offset;
3975  e->timestamp = current_dts;
3976  e->size = sample_size;
3977  e->min_distance = distance;
3978  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
3979  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
3980  "size %u, distance %u, keyframe %d\n", st->index, current_sample,
3981  current_offset, current_dts, sample_size, distance, keyframe);
3982  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
3983  ff_rfps_add_frame(mov->fc, st, current_dts);
3984  }
3985 
3986  current_offset += sample_size;
3987  stream_size += sample_size;
3988 
3989  /* A negative sample duration is invalid based on the spec,
3990  * but some samples need it to correct the DTS. */
3991  if (sc->stts_data[stts_index].duration < 0) {
3992  av_log(mov->fc, AV_LOG_WARNING,
3993  "Invalid SampleDelta %d in STTS, at %d st:%d\n",
3994  sc->stts_data[stts_index].duration, stts_index,
3995  st->index);
3996  dts_correction += sc->stts_data[stts_index].duration - 1;
3997  sc->stts_data[stts_index].duration = 1;
3998  }
3999  current_dts += sc->stts_data[stts_index].duration;
4000  if (!dts_correction || current_dts + dts_correction > last_dts) {
4001  current_dts += dts_correction;
4002  dts_correction = 0;
4003  } else {
4004  /* Avoid creating non-monotonous DTS */
4005  dts_correction += current_dts - last_dts - 1;
4006  current_dts = last_dts + 1;
4007  }
4008  last_dts = current_dts;
4009  distance++;
4010  stts_sample++;
4011  current_sample++;
4012  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
4013  stts_sample = 0;
4014  stts_index++;
4015  }
4016  }
4017  }
4018  if (st->duration > 0)
4019  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
4020  } else {
4021  unsigned chunk_samples, total = 0;
4022 
4023  if (!sc->chunk_count)
4024  return;
4025 
4026  // compute total chunk count
4027  for (i = 0; i < sc->stsc_count; i++) {
4028  unsigned count, chunk_count;
4029 
4030  chunk_samples = sc->stsc_data[i].count;
4031  if (i != sc->stsc_count - 1 &&
4032  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
4033  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
4034  return;
4035  }
4036 
4037  if (sc->samples_per_frame >= 160) { // gsm
4038  count = chunk_samples / sc->samples_per_frame;
4039  } else if (sc->samples_per_frame > 1) {
4040  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
4041  count = (chunk_samples+samples-1) / samples;
4042  } else {
4043  count = (chunk_samples+1023) / 1024;
4044  }
4045 
4046  if (mov_stsc_index_valid(i, sc->stsc_count))
4047  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
4048  else
4049  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
4050  total += chunk_count * count;
4051  }
4052 
4053  av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
4054  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
4055  return;
4057  st->nb_index_entries + total,
4058  sizeof(*st->index_entries)) < 0) {
4059  st->nb_index_entries = 0;
4060  return;
4061  }
4062  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
4063 
4064  // populate index
4065  for (i = 0; i < sc->chunk_count; i++) {
4066  current_offset = sc->chunk_offsets[i];
4067  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4068  i + 1 == sc->stsc_data[stsc_index + 1].first)
4069  stsc_index++;
4070  chunk_samples = sc->stsc_data[stsc_index].count;
4071 
4072  while (chunk_samples > 0) {
4073  AVIndexEntry *e;
4074  unsigned size, samples;
4075 
4076  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
4078  "Zero bytes per frame, but %d samples per frame",
4079  sc->samples_per_frame);
4080  return;
4081  }
4082 
4083  if (sc->samples_per_frame >= 160) { // gsm
4084  samples = sc->samples_per_frame;
4085  size = sc->bytes_per_frame;
4086  } else {
4087  if (sc->samples_per_frame > 1) {
4088  samples = FFMIN((1024 / sc->samples_per_frame)*
4089  sc->samples_per_frame, chunk_samples);
4091  } else {
4092  samples = FFMIN(1024, chunk_samples);
4093  size = samples * sc->sample_size;
4094  }
4095  }
4096 
4097  if (st->nb_index_entries >= total) {
4098  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
4099  return;
4100  }
4101  if (size > 0x3FFFFFFF) {
4102  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
4103  return;
4104  }
4105  e = &st->index_entries[st->nb_index_entries++];
4106  e->pos = current_offset;
4107  e->timestamp = current_dts;
4108  e->size = size;
4109  e->min_distance = 0;
4110  e->flags = AVINDEX_KEYFRAME;
4111  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
4112  "size %u, duration %u\n", st->index, i, current_offset, current_dts,
4113  size, samples);
4114 
4115  current_offset += size;
4116  current_dts += samples;
4117  chunk_samples -= samples;
4118  }
4119  }
4120  }
4121 
4122  if (!mov->ignore_editlist && mov->advanced_editlist) {
4123  // Fix index according to edit lists.
4124  mov_fix_index(mov, st);
4125  }
4126 
4127  // Update start time of the stream.
4129  st->start_time = st->index_entries[0].timestamp + sc->dts_shift;
4130  if (sc->ctts_data) {
4131  st->start_time += sc->ctts_data[0].duration;
4132  }
4133  }
4134 
4135  mov_estimate_video_delay(mov, st);
4136 }
4137 
4138 static int test_same_origin(const char *src, const char *ref) {
4139  char src_proto[64];
4140  char ref_proto[64];
4141  char src_auth[256];
4142  char ref_auth[256];
4143  char src_host[256];
4144  char ref_host[256];
4145  int src_port=-1;
4146  int ref_port=-1;
4147 
4148  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
4149  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
4150 
4151  if (strlen(src) == 0) {
4152  return -1;
4153  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
4154  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
4155  strlen(src_host) + 1 >= sizeof(src_host) ||
4156  strlen(ref_host) + 1 >= sizeof(ref_host)) {
4157  return 0;
4158  } else if (strcmp(src_proto, ref_proto) ||
4159  strcmp(src_auth, ref_auth) ||
4160  strcmp(src_host, ref_host) ||
4161  src_port != ref_port) {
4162  return 0;
4163  } else
4164  return 1;
4165 }
4166 
4167 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
4168 {
4169  /* try relative path, we do not try the absolute because it can leak information about our
4170  system to an attacker */
4171  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
4172  char filename[1025];
4173  const char *src_path;
4174  int i, l;
4175 
4176  /* find a source dir */
4177  src_path = strrchr(src, '/');
4178  if (src_path)
4179  src_path++;
4180  else
4181  src_path = src;
4182 
4183  /* find a next level down to target */
4184  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
4185  if (ref->path[l] == '/') {
4186  if (i == ref->nlvl_to - 1)
4187  break;
4188  else
4189  i++;
4190  }
4191 
4192  /* compose filename if next level down to target was found */
4193  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
4194  memcpy(filename, src, src_path - src);
4195  filename[src_path - src] = 0;
4196 
4197  for (i = 1; i < ref->nlvl_from; i++)
4198  av_strlcat(filename, "../", sizeof(filename));
4199 
4200  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
4201  if (!c->use_absolute_path) {
4202  int same_origin = test_same_origin(src, filename);
4203 
4204  if (!same_origin) {
4205  av_log(c->fc, AV_LOG_ERROR,
4206  "Reference with mismatching origin, %s not tried for security reasons, "
4207  "set demuxer option use_absolute_path to allow it anyway\n",
4208  ref->path);
4209  return AVERROR(ENOENT);
4210  }
4211 
4212  if (strstr(ref->path + l + 1, "..") ||
4213  strstr(ref->path + l + 1, ":") ||
4214  (ref->nlvl_from > 1 && same_origin < 0) ||
4215  (filename[0] == '/' && src_path == src))
4216  return AVERROR(ENOENT);
4217  }
4218 
4219  if (strlen(filename) + 1 == sizeof(filename))
4220  return AVERROR(ENOENT);
4221  if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
4222  return 0;
4223  }
4224  } else if (c->use_absolute_path) {
4225  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
4226  "this is a possible security issue\n");
4227  if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
4228  return 0;
4229  } else {
4230  av_log(c->fc, AV_LOG_ERROR,
4231  "Absolute path %s not tried for security reasons, "
4232  "set demuxer option use_absolute_path to allow absolute paths\n",
4233  ref->path);
4234  }
4235 
4236  return AVERROR(ENOENT);
4237 }
4238 
4240 {
4241  if (sc->time_scale <= 0) {
4242  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
4243  sc->time_scale = c->time_scale;
4244  if (sc->time_scale <= 0)
4245  sc->time_scale = 1;
4246  }
4247 }
4248 
4250 {
4251  AVStream *st;
4252  MOVStreamContext *sc;
4253  int ret;
4254 
4255  st = avformat_new_stream(c->fc, NULL);
4256  if (!st) return AVERROR(ENOMEM);
4257  st->id = -1;
4258  sc = av_mallocz(sizeof(MOVStreamContext));
4259  if (!sc) return AVERROR(ENOMEM);
4260 
4261  st->priv_data = sc;
4263  sc->ffindex = st->index;
4264  c->trak_index = st->index;
4265 
4266  if ((ret = mov_read_default(c, pb, atom)) < 0)
4267  return ret;
4268 
4269  c->trak_index = -1;
4270 
4271  // Here stsc refers to a chunk not described in stco. This is technically invalid,
4272  // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
4273  if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
4274  sc->stsc_count = 0;
4275  av_freep(&sc->stsc_data);
4276  }
4277 
4278  /* sanity checks */
4279  if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
4280  (!sc->sample_size && !sc->sample_count))) ||
4281  (!sc->chunk_count && sc->sample_count)) {
4282  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
4283  st->index);
4284  return 0;
4285  }
4286  if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
4287  av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
4288  st->index);
4289  return AVERROR_INVALIDDATA;
4290  }
4291 
4292  fix_timescale(c, sc);
4293 
4294  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
4295 
4296  mov_build_index(c, st);
4297 
4298  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
4299  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
4300  if (c->enable_drefs) {
4301  if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
4302  av_log(c->fc, AV_LOG_ERROR,
4303  "stream %d, error opening alias: path='%s', dir='%s', "
4304  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
4305  st->index, dref->path, dref->dir, dref->filename,
4306  dref->volume, dref->nlvl_from, dref->nlvl_to);
4307  } else {
4308  av_log(c->fc, AV_LOG_WARNING,
4309  "Skipped opening external track: "
4310  "stream %d, alias: path='%s', dir='%s', "
4311  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
4312  "Set enable_drefs to allow this.\n",
4313  st->index, dref->path, dref->dir, dref->filename,
4314  dref->volume, dref->nlvl_from, dref->nlvl_to);
4315  }
4316  } else {
4317  sc->pb = c->fc->pb;
4318  sc->pb_is_copied = 1;
4319  }
4320 
4321  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4322  if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
4323  sc->height && sc->width &&
4324  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
4325  st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
4326  ((double)st->codecpar->width * sc->height), INT_MAX);
4327  }
4328 
4329 #if FF_API_R_FRAME_RATE
4330  if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
4332  sc->time_scale, sc->stts_data[0].duration, INT_MAX);
4333 #endif
4334  }
4335 
4336  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
4337  if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
4338  TAG_IS_AVCI(st->codecpar->codec_tag)) {
4340  if (ret < 0)
4341  return ret;
4342  }
4343 
4344  switch (st->codecpar->codec_id) {
4345 #if CONFIG_H261_DECODER
4346  case AV_CODEC_ID_H261:
4347 #endif
4348 #if CONFIG_H263_DECODER
4349  case AV_CODEC_ID_H263:
4350 #endif
4351 #if CONFIG_MPEG4_DECODER
4352  case AV_CODEC_ID_MPEG4:
4353 #endif
4354  st->codecpar->width = 0; /* let decoder init width/height */
4355  st->codecpar->height= 0;
4356  break;
4357  }
4358 
4359  // If the duration of the mp3 packets is not constant, then they could need a parser
4360  if (st->codecpar->codec_id == AV_CODEC_ID_MP3
4361  && sc->stts_count > 3
4362  && sc->stts_count*10 > st->nb_frames
4363  && sc->time_scale == st->codecpar->sample_rate) {
4365  }
4366  /* Do not need those anymore. */
4367  av_freep(&sc->chunk_offsets);
4368  av_freep(&sc->sample_sizes);
4369  av_freep(&sc->keyframes);
4370  av_freep(&sc->stts_data);
4371  av_freep(&sc->stps_data);
4372  av_freep(&sc->elst_data);
4373  av_freep(&sc->rap_group);
4374 
4375  return 0;
4376 }
4377 
4379 {
4380  int ret;
4381  c->itunes_metadata = 1;
4382  ret = mov_read_default(c, pb, atom);
4383  c->itunes_metadata = 0;
4384  return ret;
4385 }
4386 
4388 {
4389  uint32_t count;
4390  uint32_t i;
4391 
4392  if (atom.size < 8)
4393  return 0;
4394 
4395  avio_skip(pb, 4);
4396  count = avio_rb32(pb);
4397  if (count > UINT_MAX / sizeof(*c->meta_keys) - 1) {
4398  av_log(c->fc, AV_LOG_ERROR,
4399  "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
4400  return AVERROR_INVALIDDATA;
4401  }
4402 
4403  c->meta_keys_count = count + 1;
4404  c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
4405  if (!c->meta_keys)
4406  return AVERROR(ENOMEM);
4407 
4408  for (i = 1; i <= count; ++i) {
4409  uint32_t key_size = avio_rb32(pb);
4410  uint32_t type = avio_rl32(pb);
4411  if (key_size < 8) {
4412  av_log(c->fc, AV_LOG_ERROR,
4413  "The key# %"PRIu32" in meta has invalid size:"
4414  "%"PRIu32"\n", i, key_size);
4415  return AVERROR_INVALIDDATA;
4416  }
4417  key_size -= 8;
4418  if (type != MKTAG('m','d','t','a')) {
4419  avio_skip(pb, key_size);
4420  }
4421  c->meta_keys[i] = av_mallocz(key_size + 1);
4422  if (!c->meta_keys[i])
4423  return AVERROR(ENOMEM);
4424  avio_read(pb, c->meta_keys[i], key_size);
4425  }
4426 
4427  return 0;
4428 }
4429 
4431 {
4432  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
4433  uint8_t *key = NULL, *val = NULL, *mean = NULL;
4434  int i;
4435  int ret = 0;
4436  AVStream *st;
4437  MOVStreamContext *sc;
4438 
4439  if (c->fc->nb_streams < 1)
4440  return 0;
4441  st = c->fc->streams[c->fc->nb_streams-1];
4442  sc = st->priv_data;
4443 
4444  for (i = 0; i < 3; i++) {
4445  uint8_t **p;
4446  uint32_t len, tag;
4447 
4448  if (end - avio_tell(pb) <= 12)
4449  break;
4450 
4451  len = avio_rb32(pb);
4452  tag = avio_rl32(pb);
4453  avio_skip(pb, 4); // flags
4454 
4455  if (len < 12 || len - 12 > end - avio_tell(pb))
4456  break;
4457  len -= 12;
4458 
4459  if (tag == MKTAG('m', 'e', 'a', 'n'))
4460  p = &mean;
4461  else if (tag == MKTAG('n', 'a', 'm', 'e'))
4462  p = &key;
4463  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
4464  avio_skip(pb, 4);
4465  len -= 4;
4466  p = &val;
4467  } else
4468  break;
4469 
4470  if (*p)
4471  break;
4472 
4473  *p = av_malloc(len + 1);
4474  if (!*p) {
4475  ret = AVERROR(ENOMEM);
4476  break;
4477  }
4478  ret = ffio_read_size(pb, *p, len);
4479  if (ret < 0) {
4480  av_freep(p);
4481  break;
4482  }
4483  (*p)[len] = 0;
4484  }
4485 
4486  if (mean && key && val) {
4487  if (strcmp(key, "iTunSMPB") == 0) {
4488  int priming, remainder, samples;
4489  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
4490  if(priming>0 && priming<16384)
4491  sc->start_pad = priming;
4492  }
4493  }
4494  if (strcmp(key, "cdec") != 0) {
4495  av_dict_set(&c->fc->metadata, key, val,
4497  key = val = NULL;
4498  }
4499  } else {
4500  av_log(c->fc, AV_LOG_VERBOSE,
4501  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
4502  }
4503 
4504  avio_seek(pb, end, SEEK_SET);
4505  av_freep(&key);
4506  av_freep(&val);
4507  av_freep(&mean);
4508  return ret;
4509 }
4510 
4512 {
4513  while (atom.size > 8) {
4514  uint32_t tag;
4515  if (avio_feof(pb))
4516  return AVERROR_EOF;
4517  tag = avio_rl32(pb);
4518  atom.size -= 4;
4519  if (tag == MKTAG('h','d','l','r')) {
4520  avio_seek(pb, -8, SEEK_CUR);
4521  atom.size += 8;
4522  return mov_read_default(c, pb, atom);
4523  }
4524  }
4525  return 0;
4526 }
4527 
4528 // return 1 when matrix is identity, 0 otherwise
4529 #define IS_MATRIX_IDENT(matrix) \
4530  ( (matrix)[0][0] == (1 << 16) && \
4531  (matrix)[1][1] == (1 << 16) && \
4532  (matrix)[2][2] == (1 << 30) && \
4533  !(matrix)[0][1] && !(matrix)[0][2] && \
4534  !(matrix)[1][0] && !(matrix)[1][2] && \
4535  !(matrix)[2][0] && !(matrix)[2][1])
4536 
4538 {
4539  int i, j, e;
4540  int width;
4541  int height;
4542  int display_matrix[3][3];
4543  int res_display_matrix[3][3] = { { 0 } };
4544  AVStream *st;
4545  MOVStreamContext *sc;
4546  int version;
4547  int flags;
4548 
4549  if (c->fc->nb_streams < 1)
4550  return 0;
4551  st = c->fc->streams[c->fc->nb_streams-1];
4552  sc = st->priv_data;
4553 
4554  // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
4555  // avoids corrupting AVStreams mapped to an earlier tkhd.
4556  if (st->id != -1)
4557  return AVERROR_INVALIDDATA;
4558 
4559  version = avio_r8(pb);
4560  flags = avio_rb24(pb);
4562 
4563  if (version == 1) {
4564  avio_rb64(pb);
4565  avio_rb64(pb);
4566  } else {
4567  avio_rb32(pb); /* creation time */
4568  avio_rb32(pb); /* modification time */
4569  }
4570  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
4571  avio_rb32(pb); /* reserved */
4572 
4573  /* highlevel (considering edits) duration in movie timebase */
4574  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
4575  avio_rb32(pb); /* reserved */
4576  avio_rb32(pb); /* reserved */
4577 
4578  avio_rb16(pb); /* layer */
4579  avio_rb16(pb); /* alternate group */
4580  avio_rb16(pb); /* volume */
4581  avio_rb16(pb); /* reserved */
4582 
4583  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
4584  // they're kept in fixed point format through all calculations
4585  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
4586  // side data, but the scale factor is not needed to calculate aspect ratio
4587  for (i = 0; i < 3; i++) {
4588  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
4589  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
4590  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
4591  }
4592 
4593  width = avio_rb32(pb); // 16.16 fixed point track width
4594  height = avio_rb32(pb); // 16.16 fixed point track height
4595  sc->width = width >> 16;
4596  sc->height = height >> 16;
4597 
4598  // apply the moov display matrix (after the tkhd one)
4599  for (i = 0; i < 3; i++) {
4600  const int sh[3] = { 16, 16, 30 };
4601  for (j = 0; j < 3; j++) {
4602  for (e = 0; e < 3; e++) {
4603  res_display_matrix[i][j] +=
4604  ((int64_t) display_matrix[i][e] *
4605  c->movie_display_matrix[e][j]) >> sh[e];
4606  }
4607  }
4608  }
4609 
4610  // save the matrix when it is not the default identity
4611  if (!IS_MATRIX_IDENT(res_display_matrix)) {
4612  double rotate;
4613 
4614  av_freep(&sc->display_matrix);
4615  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
4616  if (!sc->display_matrix)
4617  return AVERROR(ENOMEM);
4618 
4619  for (i = 0; i < 3; i++)
4620  for (j = 0; j < 3; j++)
4621  sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
4622 
4625  if (!isnan(rotate)) {
4626  char rotate_buf[64];
4627  rotate = -rotate;
4628  if (rotate < 0) // for backward compatibility
4629  rotate += 360;
4630  snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
4631  av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
4632  }
4633 #endif
4634  }
4635 
4636  // transform the display width/height according to the matrix
4637  // to keep the same scale, use [width height 1<<16]
4638  if (width && height && sc->display_matrix) {
4639  double disp_transform[2];
4640 
4641  for (i = 0; i < 2; i++)
4642  disp_transform[i] = hypot(sc->display_matrix[0 + i],
4643  sc->display_matrix[3 + i]);
4644 
4645  if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
4646  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
4647  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
4649  disp_transform[0] / disp_transform[1],
4650  INT_MAX);
4651  }
4652  return 0;
4653 }
4654 
4656 {
4657  MOVFragment *frag = &c->fragment;
4658  MOVTrackExt *trex = NULL;
4659  int flags, track_id, i;
4660  MOVFragmentStreamInfo * frag_stream_info;
4661 
4662  avio_r8(pb); /* version */
4663  flags = avio_rb24(pb);
4664 
4665  track_id = avio_rb32(pb);
4666  if (!track_id)
4667  return AVERROR_INVALIDDATA;
4668  for (i = 0; i < c->trex_count; i++)
4669  if (c->trex_data[i].track_id == track_id) {
4670  trex = &c->trex_data[i];
4671  break;
4672  }
4673  if (!trex) {
4674  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
4675  return 0;
4676  }
4677  c->fragment.found_tfhd = 1;
4678  frag->track_id = track_id;
4679  set_frag_stream(&c->frag_index, track_id);
4680 
4683  frag->moof_offset : frag->implicit_offset;
4684  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
4685 
4687  avio_rb32(pb) : trex->duration;
4688  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
4689  avio_rb32(pb) : trex->size;
4690  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
4691  avio_rb32(pb) : trex->flags;
4692  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
4693 
4694  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4695  if (frag_stream_info)
4696  frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
4697 
4698  return 0;
4699 }
4700 
4702 {
4703  unsigned i, num;
4704  void *new_tracks;
4705 
4706  num = atom.size / 4;
4707  if (!(new_tracks = av_malloc_array(num, sizeof(int))))
4708  return AVERROR(ENOMEM);
4709 
4710  av_free(c->chapter_tracks);
4711  c->chapter_tracks = new_tracks;
4712  c->nb_chapter_tracks = num;
4713 
4714  for (i = 0; i < num && !pb->eof_reached; i++)
4715  c->chapter_tracks[i] = avio_rb32(pb);
4716 
4717  c->nb_chapter_tracks = i;
4718 
4719  return 0;
4720 }
4721 
4723 {
4724  MOVTrackExt *trex;
4725  int err;
4726 
4727  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
4728  return AVERROR_INVALIDDATA;
4729  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
4730  sizeof(*c->trex_data))) < 0) {
4731  c->trex_count = 0;
4732  return err;
4733  }
4734 
4735  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
4736 
4737  trex = &c->trex_data[c->trex_count++];
4738  avio_r8(pb); /* version */
4739  avio_rb24(pb); /* flags */
4740  trex->track_id = avio_rb32(pb);
4741  trex->stsd_id = avio_rb32(pb);
4742  trex->duration = avio_rb32(pb);
4743  trex->size = avio_rb32(pb);
4744  trex->flags = avio_rb32(pb);
4745  return 0;
4746 }
4747 
4749 {
4750  MOVFragment *frag = &c->fragment;
4751  AVStream *st = NULL;
4752  MOVStreamContext *sc;
4753  int version, i;
4754  MOVFragmentStreamInfo * frag_stream_info;
4755  int64_t base_media_decode_time;
4756 
4757  for (i = 0; i < c->fc->nb_streams; i++) {
4758  if (c->fc->streams[i]->id == frag->track_id) {
4759  st = c->fc->streams[i];
4760  break;
4761  }
4762  }
4763  if (!st) {
4764  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
4765  return 0;
4766  }
4767  sc = st->priv_data;
4768  if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
4769  return 0;
4770  version = avio_r8(pb);
4771  avio_rb24(pb); /* flags */
4772  if (version) {
4773  base_media_decode_time = avio_rb64(pb);
4774  } else {
4775  base_media_decode_time = avio_rb32(pb);
4776  }
4777 
4778  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4779  if (frag_stream_info)
4780  frag_stream_info->tfdt_dts = base_media_decode_time;
4781  sc->track_end = base_media_decode_time;
4782 
4783  return 0;
4784 }
4785 
4787 {
4788  MOVFragment *frag = &c->fragment;
4789  AVStream *st = NULL;
4790  MOVStreamContext *sc;
4791  MOVStts *ctts_data;
4792  uint64_t offset;
4793  int64_t dts, pts = AV_NOPTS_VALUE;
4794  int data_offset = 0;
4795  unsigned entries, first_sample_flags = frag->flags;
4796  int flags, distance, i;
4797  int64_t prev_dts = AV_NOPTS_VALUE;
4798  int next_frag_index = -1, index_entry_pos;
4799  size_t requested_size;
4800  size_t old_ctts_allocated_size;
4801  AVIndexEntry *new_entries;
4802  MOVFragmentStreamInfo * frag_stream_info;
4803 
4804  if (!frag->found_tfhd) {
4805  av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
4806  return AVERROR_INVALIDDATA;
4807  }
4808 
4809  for (i = 0; i < c->fc->nb_streams; i++) {
4810  if (c->fc->streams[i]->id == frag->track_id) {
4811  st = c->fc->streams[i];
4812  break;
4813  }
4814  }
4815  if (!st) {
4816  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
4817  return 0;
4818  }
4819  sc = st->priv_data;
4820  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
4821  return 0;
4822 
4823  // Find the next frag_index index that has a valid index_entry for
4824  // the current track_id.
4825  //
4826  // A valid index_entry means the trun for the fragment was read
4827  // and it's samples are in index_entries at the given position.
4828  // New index entries will be inserted before the index_entry found.
4829  index_entry_pos = st->nb_index_entries;
4830  for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
4831  frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
4832  if (frag_stream_info && frag_stream_info->index_entry >= 0) {
4833  next_frag_index = i;
4834  index_entry_pos = frag_stream_info->index_entry;
4835  break;
4836  }
4837  }
4838  av_assert0(index_entry_pos <= st->nb_index_entries);
4839 
4840  avio_r8(pb); /* version */
4841  flags = avio_rb24(pb);
4842  entries = avio_rb32(pb);
4843  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
4844 
4845  if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
4846  return AVERROR_INVALIDDATA;
4847  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
4848  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
4849 
4850  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
4851  if (frag_stream_info) {
4852  if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
4853  dts = frag_stream_info->next_trun_dts - sc->time_offset;
4854  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
4855  c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
4856  pts = frag_stream_info->first_tfra_pts;
4857  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
4858  ", using it for pts\n", pts);
4859  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
4860  c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
4861  dts = frag_stream_info->first_tfra_pts;
4862  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
4863  ", using it for dts\n", pts);
4864  } else if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE) {
4865  // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
4866  // pts = frag_stream_info->sidx_pts;
4867  dts = frag_stream_info->sidx_pts - sc->time_offset;
4868  av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
4869  ", using it for pts\n", pts);
4870  } else if (frag_stream_info->tfdt_dts != AV_NOPTS_VALUE) {
4871  dts = frag_stream_info->tfdt_dts - sc->time_offset;
4872  av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
4873  ", using it for dts\n", dts);
4874  } else {
4875  dts = sc->track_end - sc->time_offset;
4876  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
4877  ", using it for dts\n", dts);
4878  }
4879  } else {
4880  dts = sc->track_end - sc->time_offset;
4881  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
4882  ", using it for dts\n", dts);
4883  }
4884  offset = frag->base_data_offset + data_offset;
4885  distance = 0;
4886  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
4887 
4888  // realloc space for new index entries
4889  if((uint64_t)st->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
4890  entries = UINT_MAX / sizeof(AVIndexEntry) - st->nb_index_entries;
4891  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
4892  }
4893  if (entries == 0)
4894  return 0;
4895 
4896  requested_size = (st->nb_index_entries + entries) * sizeof(AVIndexEntry);
4897  new_entries = av_fast_realloc(st->index_entries,
4899  requested_size);
4900  if (!new_entries)
4901  return AVERROR(ENOMEM);
4902  st->index_entries= new_entries;
4903 
4904  requested_size = (st->nb_index_entries + entries) * sizeof(*sc->ctts_data);
4905  old_ctts_allocated_size = sc->ctts_allocated_size;
4906  ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
4907  requested_size);
4908  if (!ctts_data)
4909  return AVERROR(ENOMEM);
4910  sc->ctts_data = ctts_data;
4911 
4912  // In case there were samples without ctts entries, ensure they get
4913  // zero valued entries. This ensures clips which mix boxes with and
4914  // without ctts entries don't pickup uninitialized data.
4915  memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
4916  sc->ctts_allocated_size - old_ctts_allocated_size);
4917 
4918  if (index_entry_pos < st->nb_index_entries) {
4919  // Make hole in index_entries and ctts_data for new samples
4920  memmove(st->index_entries + index_entry_pos + entries,
4921  st->index_entries + index_entry_pos,
4922  sizeof(*st->index_entries) *
4923  (st->nb_index_entries - index_entry_pos));
4924  memmove(sc->ctts_data + index_entry_pos + entries,
4925  sc->ctts_data + index_entry_pos,
4926  sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
4927  if (index_entry_pos < sc->current_sample) {
4928  sc->current_sample += entries;
4929  }
4930  }
4931 
4932  st->nb_index_entries += entries;
4933  sc->ctts_count = st->nb_index_entries;
4934 
4935  // Record the index_entry position in frag_index of this fragment
4936  if (frag_stream_info)
4937  frag_stream_info->index_entry = index_entry_pos;
4938 
4939  if (index_entry_pos > 0)
4940  prev_dts = st->index_entries[index_entry_pos-1].timestamp;
4941 
4942  for (i = 0; i < entries && !pb->eof_reached; i++) {
4943  unsigned sample_size = frag->size;
4944  int sample_flags = i ? frag->flags : first_sample_flags;
4945  unsigned sample_duration = frag->duration;
4946  unsigned ctts_duration = 0;
4947  int keyframe = 0;
4948  int index_entry_flags = 0;
4949 
4950  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
4951  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
4952  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
4953  if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
4954 
4955  mov_update_dts_shift(sc, ctts_duration, c->fc);
4956  if (pts != AV_NOPTS_VALUE) {
4957  dts = pts - sc->dts_shift;
4958  if (flags & MOV_TRUN_SAMPLE_CTS) {
4959  dts -= ctts_duration;
4960  } else {
4961  dts -= sc->time_offset;
4962  }
4963  av_log(c->fc, AV_LOG_DEBUG,
4964  "pts %"PRId64" calculated dts %"PRId64
4965  " sc->dts_shift %d ctts.duration %d"
4966  " sc->time_offset %"PRId64
4967  " flags & MOV_TRUN_SAMPLE_CTS %d\n",
4968  pts, dts,
4969  sc->dts_shift, ctts_duration,
4971  pts = AV_NOPTS_VALUE;
4972  }
4973 
4975  keyframe = 1;
4976  else
4977  keyframe =
4978  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
4980  if (keyframe) {
4981  distance = 0;
4982  index_entry_flags |= AVINDEX_KEYFRAME;
4983  }
4984  // Fragments can overlap in time. Discard overlapping frames after
4985  // decoding.
4986  if (prev_dts >= dts)
4987  index_entry_flags |= AVINDEX_DISCARD_FRAME;
4988 
4989  st->index_entries[index_entry_pos].pos = offset;
4990  st->index_entries[index_entry_pos].timestamp = dts;
4991  st->index_entries[index_entry_pos].size= sample_size;
4992  st->index_entries[index_entry_pos].min_distance= distance;
4993  st->index_entries[index_entry_pos].flags = index_entry_flags;
4994 
4995  sc->ctts_data[index_entry_pos].count = 1;
4996  sc->ctts_data[index_entry_pos].duration = ctts_duration;
4997  index_entry_pos++;
4998 
4999  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
5000  "size %u, distance %d, keyframe %d\n", st->index,
5001  index_entry_pos, offset, dts, sample_size, distance, keyframe);
5002  distance++;
5003  if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
5004  return AVERROR_INVALIDDATA;
5005  dts += sample_duration;
5006  offset += sample_size;
5007  sc->data_size += sample_size;
5008 
5009  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
5010  1 <= INT_MAX - sc->nb_frames_for_fps
5011  ) {
5012  sc->duration_for_fps += sample_duration;
5013  sc->nb_frames_for_fps ++;
5014  }
5015  }
5016  if (frag_stream_info)
5017  frag_stream_info->next_trun_dts = dts + sc->time_offset;
5018  if (i < entries) {
5019  // EOF found before reading all entries. Fix the hole this would
5020  // leave in index_entries and ctts_data
5021  int gap = entries - i;
5022  memmove(st->index_entries + index_entry_pos,
5023  st->index_entries + index_entry_pos + gap,
5024  sizeof(*st->index_entries) *
5025  (st->nb_index_entries - (index_entry_pos + gap)));
5026  memmove(sc->ctts_data + index_entry_pos,
5027  sc->ctts_data + index_entry_pos + gap,
5028  sizeof(*sc->ctts_data) *
5029  (sc->ctts_count - (index_entry_pos + gap)));
5030 
5031  st->nb_index_entries -= gap;
5032  sc->ctts_count -= gap;
5033  if (index_entry_pos < sc->current_sample) {
5034  sc->current_sample -= gap;
5035  }
5036  entries = i;
5037  }
5038 
5039  // The end of this new fragment may overlap in time with the start
5040  // of the next fragment in index_entries. Mark the samples in the next
5041  // fragment that overlap with AVINDEX_DISCARD_FRAME
5042  prev_dts = AV_NOPTS_VALUE;
5043  if (index_entry_pos > 0)
5044  prev_dts = st->index_entries[index_entry_pos-1].timestamp;
5045  for (i = index_entry_pos; i < st->nb_index_entries; i++) {
5046  if (prev_dts < st->index_entries[i].timestamp)
5047  break;
5049  }
5050 
5051  // If a hole was created to insert the new index_entries into,
5052  // the index_entry recorded for all subsequent moof must
5053  // be incremented by the number of entries inserted.
5054  fix_frag_index_entries(&c->frag_index, next_frag_index,
5055  frag->track_id, entries);
5056 
5057  if (pb->eof_reached) {
5058  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
5059  return AVERROR_EOF;
5060  }
5061 
5062  frag->implicit_offset = offset;
5063 
5064  sc->track_end = dts + sc->time_offset;
5065  if (st->duration < sc->track_end)
5066  st->duration = sc->track_end;
5067 
5068  return 0;
5069 }
5070 
5072 {
5073  int64_t stream_size = avio_size(pb);
5074  int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
5075  uint8_t version, is_complete;
5076  int64_t offadd;
5077  unsigned i, j, track_id, item_count;
5078  AVStream *st = NULL;
5079  AVStream *ref_st = NULL;
5080  MOVStreamContext *sc, *ref_sc = NULL;
5081  AVRational timescale;
5082 
5083  version = avio_r8(pb);
5084  if (version > 1) {
5085  avpriv_request_sample(c->fc, "sidx version %u", version);
5086  return 0;
5087  }
5088 
5089  avio_rb24(pb); // flags
5090 
5091  track_id = avio_rb32(pb); // Reference ID
5092  for (i = 0; i < c->fc->nb_streams; i++) {
5093  if (c->fc->streams[i]->id == track_id) {
5094  st = c->fc->streams[i];
5095  break;
5096  }
5097  }
5098  if (!st) {
5099  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
5100  return 0;
5101  }
5102 
5103  sc = st->priv_data;
5104 
5105  timescale = av_make_q(1, avio_rb32(pb));
5106 
5107  if (timescale.den <= 0) {
5108  av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
5109  return AVERROR_INVALIDDATA;
5110  }
5111 
5112  if (version == 0) {
5113  pts = avio_rb32(pb);
5114  offadd= avio_rb32(pb);
5115  } else {
5116  pts = avio_rb64(pb);
5117  offadd= avio_rb64(pb);
5118  }
5119  if (av_sat_add64(offset, offadd) != offset + (uint64_t)offadd)
5120  return AVERROR_INVALIDDATA;
5121 
5122  offset += (uint64_t)offadd;
5123 
5124  avio_rb16(pb); // reserved
5125 
5126  item_count = avio_rb16(pb);
5127  if (item_count == 0)
5128  return AVERROR_INVALIDDATA;
5129 
5130  for (i = 0; i < item_count; i++) {
5131  int index;
5132  MOVFragmentStreamInfo * frag_stream_info;
5133  uint32_t size = avio_rb32(pb);
5134  uint32_t duration = avio_rb32(pb);
5135  if (size & 0x80000000) {
5136  avpriv_request_sample(c->fc, "sidx reference_type 1");
5137  return AVERROR_PATCHWELCOME;
5138  }
5139  avio_rb32(pb); // sap_flags
5140  timestamp = av_rescale_q(pts, timescale, st->time_base);
5141 
5143  frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
5144  if (frag_stream_info)
5145  frag_stream_info->sidx_pts = timestamp;
5146 
5147  if (av_sat_add64(offset, size) != offset + (uint64_t)size ||
5148  av_sat_add64(pts, duration) != pts + (uint64_t)duration
5149  )
5150  return AVERROR_INVALIDDATA;
5151  offset += size;
5152  pts += duration;
5153  }
5154 
5155  st->duration = sc->track_end = pts;
5156 
5157  sc->has_sidx = 1;
5158 
5159  // See if the remaining bytes are just an mfra which we can ignore.
5160  is_complete = offset == stream_size;
5161  if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL) && stream_size > 0 ) {
5162  int64_t ret;
5163  int64_t original_pos = avio_tell(pb);
5164  if (!c->have_read_mfra_size) {
5165  if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
5166  return ret;
5167  c->mfra_size = avio_rb32(pb);
5168  c->have_read_mfra_size = 1;
5169  if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
5170  return ret;
5171  }
5172  if (offset == stream_size - c->mfra_size)
5173  is_complete = 1;
5174  }
5175 
5176  if (is_complete) {
5177  // Find first entry in fragment index that came from an sidx.
5178  // This will pretty much always be the first entry.
5179  for (i = 0; i < c->frag_index.nb_items; i++) {
5180  MOVFragmentIndexItem * item = &c->frag_index.item[i];
5181  for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
5182  MOVFragmentStreamInfo * si;
5183  si = &item->stream_info[j];
5184  if (si->sidx_pts != AV_NOPTS_VALUE) {
5185  ref_st = c->fc->streams[j];
5186  ref_sc = ref_st->priv_data;
5187  break;
5188  }
5189  }
5190  }
5191  if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
5192  st = c->fc->streams[i];
5193  sc = st->priv_data;
5194  if (!sc->has_sidx) {
5195  st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
5196  }
5197  }
5198 
5199  c->frag_index.complete = 1;
5200  }
5201 
5202  return 0;
5203 }
5204 
5205 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
5206 /* like the files created with Adobe Premiere 5.0, for samples see */
5207 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
5209 {
5210  int err;
5211 
5212  if (atom.size < 8)
5213  return 0; /* continue */
5214  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
5215  avio_skip(pb, atom.size - 4);
5216  return 0;
5217  }
5218  atom.type = avio_rl32(pb);
5219  atom.size -= 8;
5220  if (atom.type != MKTAG('m','d','a','t')) {
5221  avio_skip(pb, atom.size);
5222  return 0;
5223  }
5224  err = mov_read_mdat(c, pb, atom);
5225  return err;
5226 }
5227 
5229 {
5230 #if CONFIG_ZLIB
5231  AVIOContext ctx;
5232  uint8_t *cmov_data;
5233  uint8_t *moov_data; /* uncompressed data */
5234  long cmov_len, moov_len;
5235  int ret = -1;
5236 
5237  avio_rb32(pb); /* dcom atom */
5238  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
5239  return AVERROR_INVALIDDATA;
5240  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
5241  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
5242  return AVERROR_INVALIDDATA;
5243  }
5244  avio_rb32(pb); /* cmvd atom */
5245  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
5246  return AVERROR_INVALIDDATA;
5247  moov_len = avio_rb32(pb); /* uncompressed size */
5248  cmov_len = atom.size - 6 * 4;
5249 
5250  cmov_data = av_malloc(cmov_len);
5251  if (!cmov_data)
5252  return AVERROR(ENOMEM);
5253  moov_data = av_malloc(moov_len);
5254  if (!moov_data) {
5255  av_free(cmov_data);
5256  return AVERROR(ENOMEM);
5257  }
5258  ret = ffio_read_size(pb, cmov_data, cmov_len);
5259  if (ret < 0)
5260  goto free_and_return;
5261 
5263  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
5264  goto free_and_return;
5265  if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
5266  goto free_and_return;
5267  ctx.seekable = AVIO_SEEKABLE_NORMAL;
5268  atom.type = MKTAG('m','o','o','v');
5269  atom.size = moov_len;
5270  ret = mov_read_default(c, &ctx, atom);
5271 free_and_return:
5272  av_free(moov_data);
5273  av_free(cmov_data);
5274  return ret;
5275 #else
5276  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
5277  return AVERROR(ENOSYS);
5278 #endif
5279 }
5280 
5281 /* edit list atom */
5283 {
5284  MOVStreamContext *sc;
5285  int i, edit_count, version;
5286  int64_t elst_entry_size;
5287 
5288  if (c->fc->nb_streams < 1 || c->ignore_editlist)
5289  return 0;
5290  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
5291 
5292  version = avio_r8(pb); /* version */
5293  avio_rb24(pb); /* flags */
5294  edit_count = avio_rb32(pb); /* entries */
5295  atom.size -= 8;
5296 
5297  elst_entry_size = version == 1 ? 20 : 12;
5298  if (atom.size != edit_count * elst_entry_size) {
5299  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
5300  av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
5301  edit_count, atom.size + 8);
5302  return AVERROR_INVALIDDATA;
5303  } else {
5304  edit_count = atom.size / elst_entry_size;
5305  if (edit_count * elst_entry_size != atom.size) {
5306  av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
5307  }
5308  }
5309  }
5310 
5311  if (!edit_count)
5312  return 0;
5313  if (sc->elst_data)
5314  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
5315  av_free(sc->elst_data);
5316  sc->elst_count = 0;
5317  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
5318  if (!sc->elst_data)
5319  return AVERROR(ENOMEM);
5320 
5321  av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
5322  for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
5323  MOVElst *e = &sc->elst_data[i];
5324 
5325  if (version == 1) {
5326  e->duration = avio_rb64(pb);
5327  e->time = avio_rb64(pb);
5328  atom.size -= 16;
5329  } else {
5330  e->duration = avio_rb32(pb); /* segment duration */
5331  e->time = (int32_t)avio_rb32(pb); /* media time */
5332  atom.size -= 8;
5333  }
5334  e->rate = avio_rb32(pb) / 65536.0;
5335  atom.size -= 4;
5336  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
5337  e->duration, e->time, e->rate);
5338 
5339  if (e->time < 0 && e->time != -1 &&
5340  c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
5341  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
5342  c->fc->nb_streams-1, i, e->time);
5343  return AVERROR_INVALIDDATA;
5344  }
5345  }
5346  sc->elst_count = i;
5347 
5348  return 0;
5349 }
5350 
5352 {
5353  MOVStreamContext *sc;
5354 
5355  if (c->fc->nb_streams < 1)
5356  return AVERROR_INVALIDDATA;
5357  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5358  sc->timecode_track = avio_rb32(pb);
5359  return 0;
5360 }
5361 
5363 {
5364  AVStream *st;
5365  int ret;
5366 
5367  if (c->fc->nb_streams < 1)
5368  return 0;
5369  st = c->fc->streams[c->fc->nb_streams - 1];
5370 
5371  if (atom.size < 4) {
5372  av_log(c->fc, AV_LOG_ERROR, "Empty AV1 Codec Configuration Box\n");
5373  return AVERROR_INVALIDDATA;
5374  }
5375 
5376  /* For now, propagate only the OBUs, if any. Once libavcodec is
5377  updated to handle isobmff style extradata this can be removed. */
5378  avio_skip(pb, 4);
5379 
5380  if (atom.size == 4)
5381  return 0;
5382 
5383  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 4);
5384  if (ret < 0)
5385  return ret;
5386 
5387  return 0;
5388 }
5389 
5391 {
5392  AVStream *st;
5393  int version, color_range, color_primaries, color_trc, color_space;
5394 
5395  if (c->fc->nb_streams < 1)
5396  return 0;
5397  st = c->fc->streams[c->fc->nb_streams - 1];
5398 
5399  if (atom.size < 5) {
5400  av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
5401  return AVERROR_INVALIDDATA;
5402  }
5403 
5404  version = avio_r8(pb);
5405  if (version != 1) {
5406  av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
5407  return 0;
5408  }
5409  avio_skip(pb, 3); /* flags */
5410 
5411  avio_skip(pb, 2); /* profile + level */
5412  color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
5413  color_primaries = avio_r8(pb);
5414  color_trc = avio_r8(pb);
5415  color_space = avio_r8(pb);
5416  if (avio_rb16(pb)) /* codecIntializationDataSize */
5417  return AVERROR_INVALIDDATA;
5418 
5421  if (!av_color_transfer_name(color_trc))
5422  color_trc = AVCOL_TRC_UNSPECIFIED;
5423  if (!av_color_space_name(color_space))
5424  color_space = AVCOL_SPC_UNSPECIFIED;
5425 
5428  st->codecpar->color_trc = color_trc;
5429  st->codecpar->color_space = color_space;
5430 
5431  return 0;
5432 }
5433 
5435 {
5436  MOVStreamContext *sc;
5437  int i, version;
5438 
5439  if (c->fc->nb_streams < 1)
5440  return AVERROR_INVALIDDATA;
5441 
5442  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5443 
5444  if (atom.size < 5) {
5445  av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
5446  return AVERROR_INVALIDDATA;
5447  }
5448 
5449  version = avio_r8(pb);
5450  if (version) {
5451  av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
5452  return 0;
5453  }
5454  if (sc->mastering)
5455  return AVERROR_INVALIDDATA;
5456 
5457  avio_skip(pb, 3); /* flags */
5458 
5460  if (!sc->mastering)
5461  return AVERROR(ENOMEM);
5462 
5463  for (i = 0; i < 3; i++) {
5464  sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
5465  sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
5466  }
5467  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
5468  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
5469 
5470  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
5471  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
5472 
5473  sc->mastering->has_primaries = 1;
5474  sc->mastering->has_luminance = 1;
5475 
5476  return 0;
5477 }
5478 
5480 {
5481  MOVStreamContext *sc;
5482  const int mapping[3] = {1, 2, 0};
5483  const int chroma_den = 50000;
5484  const int luma_den = 10000;
5485  int i;
5486 
5487  if (c->fc->nb_streams < 1)
5488  return AVERROR_INVALIDDATA;
5489 
5490  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5491 
5492  if (atom.size < 24 || sc->mastering) {
5493  av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
5494  return AVERROR_INVALIDDATA;
5495  }
5496 
5498  if (!sc->mastering)
5499  return AVERROR(ENOMEM);
5500 
5501  for (i = 0; i < 3; i++) {
5502  const int j = mapping[i];
5503  sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
5504  sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
5505  }
5506  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
5507  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
5508 
5509  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
5510  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
5511 
5512  sc->mastering->has_luminance = 1;
5513  sc->mastering->has_primaries = 1;
5514 
5515  return 0;
5516 }
5517 
5519 {
5520  MOVStreamContext *sc;
5521  int version;
5522 
5523  if (c->fc->nb_streams < 1)
5524  return AVERROR_INVALIDDATA;
5525 
5526  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5527 
5528  if (atom.size < 5) {
5529  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
5530  return AVERROR_INVALIDDATA;
5531  }
5532 
5533  version = avio_r8(pb);
5534  if (version) {
5535  av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
5536  return 0;
5537  }
5538  avio_skip(pb, 3); /* flags */
5539 
5540  if (sc->coll){
5541  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate COLL\n");
5542  return 0;
5543  }
5544 
5546  if (!sc->coll)
5547  return AVERROR(ENOMEM);
5548 
5549  sc->coll->MaxCLL = avio_rb16(pb);
5550  sc->coll->MaxFALL = avio_rb16(pb);
5551 
5552  return 0;
5553 }
5554 
5556 {
5557  MOVStreamContext *sc;
5558 
5559  if (c->fc->nb_streams < 1)
5560  return AVERROR_INVALIDDATA;
5561 
5562  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5563 
5564  if (atom.size < 4) {
5565  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
5566  return AVERROR_INVALIDDATA;
5567  }
5568 
5569  if (sc->coll){
5570  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate CLLI/COLL\n");
5571  return 0;
5572  }
5573 
5575  if (!sc->coll)
5576  return AVERROR(ENOMEM);
5577 
5578  sc->coll->MaxCLL = avio_rb16(pb);
5579  sc->coll->MaxFALL = avio_rb16(pb);
5580 
5581  return 0;
5582 }
5583 
5585 {
5586  AVStream *st;
5587  MOVStreamContext *sc;
5588  enum AVStereo3DType type;
5589  int mode;
5590 
5591  if (c->fc->nb_streams < 1)
5592  return 0;
5593 
5594  st = c->fc->streams[c->fc->nb_streams - 1];
5595  sc = st->priv_data;
5596 
5597  if (atom.size < 5) {
5598  av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
5599  return AVERROR_INVALIDDATA;
5600  }
5601 
5602  if (sc->stereo3d)
5603  return AVERROR_INVALIDDATA;
5604 
5605  avio_skip(pb, 4); /* version + flags */
5606 
5607  mode = avio_r8(pb);
5608  switch (mode) {
5609  case 0:
5610  type = AV_STEREO3D_2D;
5611  break;
5612  case 1:
5614  break;
5615  case 2:
5617  break;
5618  default:
5619  av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
5620  return 0;
5621  }
5622 
5623  sc->stereo3d = av_stereo3d_alloc();
5624  if (!sc->stereo3d)
5625  return AVERROR(ENOMEM);
5626 
5627  sc->stereo3d->type = type;
5628  return 0;
5629 }
5630 
5632 {
5633  AVStream *st;
5634  MOVStreamContext *sc;
5635  int size, version, layout;
5636  int32_t yaw, pitch, roll;
5637  uint32_t l = 0, t = 0, r = 0, b = 0;
5638  uint32_t tag, padding = 0;
5639  enum AVSphericalProjection projection;
5640 
5641  if (c->fc->nb_streams < 1)
5642  return 0;
5643 
5644  st = c->fc->streams[c->fc->nb_streams - 1];
5645  sc = st->priv_data;
5646 
5647  if (atom.size < 8) {
5648  av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
5649  return AVERROR_INVALIDDATA;
5650  }
5651 
5652  size = avio_rb32(pb);
5653  if (size <= 12 || size > atom.size)
5654  return AVERROR_INVALIDDATA;
5655 
5656  tag = avio_rl32(pb);
5657  if (tag != MKTAG('s','v','h','d')) {
5658  av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
5659  return 0;
5660  }
5661  version = avio_r8(pb);
5662  if (version != 0) {
5663  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5664  version);
5665  return 0;
5666  }
5667  avio_skip(pb, 3); /* flags */
5668  avio_skip(pb, size - 12); /* metadata_source */
5669 
5670  size = avio_rb32(pb);
5671  if (size > atom.size)
5672  return AVERROR_INVALIDDATA;
5673 
5674  tag = avio_rl32(pb);
5675  if (tag != MKTAG('p','r','o','j')) {
5676  av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
5677  return 0;
5678  }
5679 
5680  size = avio_rb32(pb);
5681  if (size > atom.size)
5682  return AVERROR_INVALIDDATA;
5683 
5684  tag = avio_rl32(pb);
5685  if (tag != MKTAG('p','r','h','d')) {
5686  av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
5687  return 0;
5688  }
5689  version = avio_r8(pb);
5690  if (version != 0) {
5691  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5692  version);
5693  return 0;
5694  }
5695  avio_skip(pb, 3); /* flags */
5696 
5697  /* 16.16 fixed point */
5698  yaw = avio_rb32(pb);
5699  pitch = avio_rb32(pb);
5700  roll = avio_rb32(pb);
5701 
5702  size = avio_rb32(pb);
5703  if (size > atom.size)
5704  return AVERROR_INVALIDDATA;
5705 
5706  tag = avio_rl32(pb);
5707  version = avio_r8(pb);
5708  if (version != 0) {
5709  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
5710  version);
5711  return 0;
5712  }
5713  avio_skip(pb, 3); /* flags */
5714  switch (tag) {
5715  case MKTAG('c','b','m','p'):
5716  layout = avio_rb32(pb);
5717  if (layout) {
5718  av_log(c->fc, AV_LOG_WARNING,
5719  "Unsupported cubemap layout %d\n", layout);
5720  return 0;
5721  }
5722  projection = AV_SPHERICAL_CUBEMAP;
5723  padding = avio_rb32(pb);
5724  break;
5725  case MKTAG('e','q','u','i'):
5726  t = avio_rb32(pb);
5727  b = avio_rb32(pb);
5728  l = avio_rb32(pb);
5729  r = avio_rb32(pb);
5730 
5731  if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
5732  av_log(c->fc, AV_LOG_ERROR,
5733  "Invalid bounding rectangle coordinates "
5734  "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
5735  return AVERROR_INVALIDDATA;
5736  }
5737 
5738  if (l || t || r || b)
5739  projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
5740  else
5741  projection = AV_SPHERICAL_EQUIRECTANGULAR;
5742  break;
5743  default:
5744  av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
5745  return 0;
5746  }
5747 
5749  if (!sc->spherical)
5750  return AVERROR(ENOMEM);
5751 
5752  sc->spherical->projection = projection;
5753 
5754  sc->spherical->yaw = yaw;
5755  sc->spherical->pitch = pitch;
5756  sc->spherical->roll = roll;
5757 
5758  sc->spherical->padding = padding;
5759 
5760  sc->spherical->bound_left = l;
5761  sc->spherical->bound_top = t;
5762  sc->spherical->bound_right = r;
5763  sc->spherical->bound_bottom = b;
5764 
5765  return 0;
5766 }
5767 
5769 {
5770  int ret = 0;
5771  uint8_t *buffer = av_malloc(len + 1);
5772  const char *val;
5773 
5774  if (!buffer)
5775  return AVERROR(ENOMEM);
5776  buffer[len] = '\0';
5777 
5778  ret = ffio_read_size(pb, buffer, len);
5779  if (ret < 0)
5780  goto out;
5781 
5782  /* Check for mandatory keys and values, try to support XML as best-effort */
5783  if (!sc->spherical &&
5784  av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
5785  (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
5786  av_stristr(val, "true") &&
5787  (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
5788  av_stristr(val, "true") &&
5789  (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
5790  av_stristr(val, "equirectangular")) {
5792  if (!sc->spherical)
5793  goto out;
5794 
5796 
5797  if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
5798  enum AVStereo3DType mode;
5799 
5800  if (av_stristr(buffer, "left-right"))
5802  else if (av_stristr(buffer, "top-bottom"))
5804  else
5805  mode = AV_STEREO3D_2D;
5806 
5807  sc->stereo3d = av_stereo3d_alloc();
5808  if (!sc->stereo3d)
5809  goto out;
5810 
5811  sc->stereo3d->type = mode;
5812  }
5813 
5814  /* orientation */
5815  val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
5816  if (val)
5817  sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
5818  val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
5819  if (val)
5820  sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
5821  val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
5822  if (val)
5823  sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
5824  }
5825 
5826 out:
5827  av_free(buffer);
5828  return ret;
5829 }
5830 
5832 {
5833  AVStream *st;
5834  MOVStreamContext *sc;
5835  int64_t ret;
5836  uint8_t uuid[16];
5837  static const uint8_t uuid_isml_manifest[] = {
5838  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
5839  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
5840  };
5841  static const uint8_t uuid_xmp[] = {
5842  0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
5843  0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
5844  };
5845  static const uint8_t uuid_spherical[] = {
5846  0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
5847  0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
5848  };
5849 
5850  if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
5851  return AVERROR_INVALIDDATA;
5852 
5853  if (c->fc->nb_streams < 1)
5854  return 0;
5855  st = c->fc->streams[c->fc->nb_streams - 1];
5856  sc = st->priv_data;
5857 
5858  ret = ffio_read_size(pb, uuid, sizeof(uuid));
5859  if (ret < 0)
5860  return ret;
5861  if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
5862  uint8_t *buffer, *ptr;
5863  char *endptr;
5864  size_t len = atom.size - sizeof(uuid);
5865 
5866  if (len < 4) {
5867  return AVERROR_INVALIDDATA;
5868  }
5869  ret = avio_skip(pb, 4); // zeroes
5870  len -= 4;
5871 
5872  buffer = av_mallocz(len + 1);
5873  if (!buffer) {
5874  return AVERROR(ENOMEM);
5875  }
5876  ret = ffio_read_size(pb, buffer, len);
5877  if (ret < 0) {
5878  av_free(buffer);
5879  return ret;
5880  }
5881 
5882  ptr = buffer;
5883  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
5884  ptr += sizeof("systemBitrate=\"") - 1;
5885  c->bitrates_count++;
5886  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
5887  if (!c->bitrates) {
5888  c->bitrates_count = 0;
5889  av_free(buffer);
5890  return AVERROR(ENOMEM);
5891  }
5892  errno = 0;
5893  ret = strtol(ptr, &endptr, 10);
5894  if (ret < 0 || errno || *endptr != '"') {
5895  c->bitrates[c->bitrates_count - 1] = 0;
5896  } else {
5897  c->bitrates[c->bitrates_count - 1] = ret;
5898  }
5899  }
5900 
5901  av_free(buffer);
5902  } else if (!memcmp(uuid, uuid_xmp, sizeof(uuid))) {
5903  uint8_t *buffer;
5904  size_t len = atom.size - sizeof(uuid);
5905  if (c->export_xmp) {
5906  buffer = av_mallocz(len + 1);
5907  if (!buffer) {
5908  return AVERROR(ENOMEM);
5909  }
5910  ret = ffio_read_size(pb, buffer, len);
5911  if (ret < 0) {
5912  av_free(buffer);
5913  return ret;
5914  }
5915  buffer[len] = '\0';
5916  av_dict_set(&c->fc->metadata, "xmp",
5918  } else {
5919  // skip all uuid atom, which makes it fast for long uuid-xmp file
5920  ret = avio_skip(pb, len);
5921  if (ret < 0)
5922  return ret;
5923  }
5924  } else if (!memcmp(uuid, uuid_spherical, sizeof(uuid))) {
5925  size_t len = atom.size - sizeof(uuid);
5926  ret = mov_parse_uuid_spherical(sc, pb, len);
5927  if (ret < 0)
5928  return ret;
5929  if (!sc->spherical)
5930  av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
5931  }
5932 
5933  return 0;
5934 }
5935 
5937 {
5938  int ret;
5939  uint8_t content[16];
5940 
5941  if (atom.size < 8)
5942  return 0;
5943 
5944  ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
5945  if (ret < 0)
5946  return ret;
5947 
5948  if ( !c->found_moov
5949  && !c->found_mdat
5950  && !memcmp(content, "Anevia\x1A\x1A", 8)
5951  && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
5952  c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
5953  }
5954 
5955  return 0;
5956 }
5957 
5959 {
5960  uint32_t format = avio_rl32(pb);
5961  MOVStreamContext *sc;
5962  enum AVCodecID id;
5963  AVStream *st;
5964 
5965  if (c->fc->nb_streams < 1)
5966  return 0;
5967  st = c->fc->streams[c->fc->nb_streams - 1];
5968  sc = st->priv_data;
5969 
5970  switch (sc->format)
5971  {
5972  case MKTAG('e','n','c','v'): // encrypted video
5973  case MKTAG('e','n','c','a'): // encrypted audio
5974  id = mov_codec_id(st, format);
5975  if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
5976  st->codecpar->codec_id != id) {
5977  av_log(c->fc, AV_LOG_WARNING,
5978  "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
5979  (char*)&format, st->codecpar->codec_id);
5980  break;
5981  }
5982 
5983  st->codecpar->codec_id = id;
5984  sc->format = format;
5985  break;
5986 
5987  default:
5988  if (format != sc->format) {
5989  av_log(c->fc, AV_LOG_WARNING,
5990  "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
5991  (char*)&format, (char*)&sc->format);
5992  }
5993  break;
5994  }
5995 
5996  return 0;
5997 }
5998 
5999 /**
6000  * Gets the current encryption info and associated current stream context. If
6001  * we are parsing a track fragment, this will return the specific encryption
6002  * info for this fragment; otherwise this will return the global encryption
6003  * info for the current stream.
6004  */
6006 {
6007  MOVFragmentStreamInfo *frag_stream_info;
6008  AVStream *st;
6009  int i;
6010 
6011  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
6012  if (frag_stream_info) {
6013  for (i = 0; i < c->fc->nb_streams; i++) {
6014  if (c->fc->streams[i]->id == frag_stream_info->id) {
6015  st = c->fc->streams[i];
6016  break;
6017  }
6018  }
6019  if (i == c->fc->nb_streams)
6020  return 0;
6021  *sc = st->priv_data;
6022 
6023  if (!frag_stream_info->encryption_index) {
6024  // If this stream isn't encrypted, don't create the index.
6025  if (!(*sc)->cenc.default_encrypted_sample)
6026  return 0;
6027  frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
6028  if (!frag_stream_info->encryption_index)
6029  return AVERROR(ENOMEM);
6030  }
6031  *encryption_index = frag_stream_info->encryption_index;
6032  return 1;
6033  } else {
6034  // No current track fragment, using stream level encryption info.
6035 
6036  if (c->fc->nb_streams < 1)
6037  return 0;
6038  st = c->fc->streams[c->fc->nb_streams - 1];
6039  *sc = st->priv_data;
6040 
6041  if (!(*sc)->cenc.encryption_index) {
6042  // If this stream isn't encrypted, don't create the index.
6043  if (!(*sc)->cenc.default_encrypted_sample)
6044  return 0;
6045  (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
6046  if (!(*sc)->cenc.encryption_index)
6047  return AVERROR(ENOMEM);
6048  }
6049 
6050  *encryption_index = (*sc)->cenc.encryption_index;
6051  return 1;
6052  }
6053 }
6054 
6056 {
6057  int i, ret;
6058  unsigned int subsample_count;
6059  AVSubsampleEncryptionInfo *subsamples;
6060 
6061  if (!sc->cenc.default_encrypted_sample) {
6062  av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
6063  return AVERROR_INVALIDDATA;
6064  }
6065 
6067  if (!*sample)
6068  return AVERROR(ENOMEM);
6069 
6070  if (sc->cenc.per_sample_iv_size != 0) {
6071  if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
6072  av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
6074  *sample = NULL;
6075  return ret;
6076  }
6077  }
6078 
6079  if (use_subsamples) {
6080  subsample_count = avio_rb16(pb);
6081  av_free((*sample)->subsamples);
6082  (*sample)->subsamples = av_mallocz_array(subsample_count, sizeof(*subsamples));
6083  if (!(*sample)->subsamples) {
6085  *sample = NULL;
6086  return AVERROR(ENOMEM);
6087  }
6088 
6089  for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
6090  (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
6091  (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
6092  }
6093 
6094  if (pb->eof_reached) {
6095  av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
6097  *sample = NULL;
6098  return AVERROR_INVALIDDATA;
6099  }
6100  (*sample)->subsample_count = subsample_count;
6101  }
6102 
6103  return 0;
6104 }
6105 
6107 {
6108  AVEncryptionInfo **encrypted_samples;
6109  MOVEncryptionIndex *encryption_index;
6110  MOVStreamContext *sc;
6111  int use_subsamples, ret;
6112  unsigned int sample_count, i, alloc_size = 0;
6113 
6114  ret = get_current_encryption_info(c, &encryption_index, &sc);
6115  if (ret != 1)
6116  return ret;
6117 
6118  if (encryption_index->nb_encrypted_samples) {
6119  // This can happen if we have both saio/saiz and senc atoms.
6120  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
6121  return 0;
6122  }
6123 
6124  avio_r8(pb); /* version */
6125  use_subsamples = avio_rb24(pb) & 0x02; /* flags */
6126 
6127  sample_count = avio_rb32(pb);
6128  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
6129  return AVERROR(ENOMEM);
6130 
6131  for (i = 0; i < sample_count; i++) {
6132  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
6133  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
6134  min_samples * sizeof(*encrypted_samples));
6135  if (encrypted_samples) {
6136  encryption_index->encrypted_samples = encrypted_samples;
6137 
6139  c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
6140  } else {
6141  ret = AVERROR(ENOMEM);
6142  }
6143  if (pb->eof_reached) {
6144  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
6145  if (ret >= 0)
6146  av_encryption_info_free(encryption_index->encrypted_samples[i]);
6148  }
6149 
6150  if (ret < 0) {
6151  for (; i > 0; i--)
6152  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
6153  av_freep(&encryption_index->encrypted_samples);
6154  return ret;
6155  }
6156  }
6157  encryption_index->nb_encrypted_samples = sample_count;
6158 
6159  return 0;
6160 }
6161 
6163 {
6164  AVEncryptionInfo **sample, **encrypted_samples;
6165  int64_t prev_pos;
6166  size_t sample_count, sample_info_size, i;
6167  int ret = 0;
6168  unsigned int alloc_size = 0;
6169 
6170  if (encryption_index->nb_encrypted_samples)
6171  return 0;
6172  sample_count = encryption_index->auxiliary_info_sample_count;
6173  if (encryption_index->auxiliary_offsets_count != 1) {
6174  av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
6175  return AVERROR_PATCHWELCOME;
6176  }
6177  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
6178  return AVERROR(ENOMEM);
6179 
6180  prev_pos = avio_tell(pb);
6181  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
6182  avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
6183  av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
6184  goto finish;
6185  }
6186 
6187  for (i = 0; i < sample_count && !pb->eof_reached; i++) {
6188  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
6189  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
6190  min_samples * sizeof(*encrypted_samples));
6191  if (!encrypted_samples) {
6192  ret = AVERROR(ENOMEM);
6193  goto finish;
6194  }
6195  encryption_index->encrypted_samples = encrypted_samples;
6196 
6197  sample = &encryption_index->encrypted_samples[i];
6198  sample_info_size = encryption_index->auxiliary_info_default_size
6199  ? encryption_index->auxiliary_info_default_size
6200  : encryption_index->auxiliary_info_sizes[i];
6201 
6202  ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
6203  if (ret < 0)
6204  goto finish;
6205  }
6206  if (pb->eof_reached) {
6207  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
6209  } else {
6210  encryption_index->nb_encrypted_samples = sample_count;
6211  }
6212 
6213 finish:
6214  avio_seek(pb, prev_pos, SEEK_SET);
6215  if (ret < 0) {
6216  for (; i > 0; i--) {
6217  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
6218  }
6219  av_freep(&encryption_index->encrypted_samples);
6220  }
6221  return ret;
6222 }
6223 
6224 /**
6225  * Tries to read the given number of bytes from the stream and puts it in a
6226  * newly allocated buffer. This reads in small chunks to avoid allocating large
6227  * memory if the file contains an invalid/malicious size value.
6228  */
6229 static int mov_try_read_block(AVIOContext *pb, size_t size, uint8_t **data)
6230 {
6231  const unsigned int block_size = 1024 * 1024;
6232  uint8_t *buffer = NULL;
6233  unsigned int alloc_size = 0, offset = 0;
6234  while (offset < size) {
6235  unsigned int new_size =
6236  alloc_size >= INT_MAX - block_size ? INT_MAX : alloc_size + block_size;
6237  uint8_t *new_buffer = av_fast_realloc(buffer, &alloc_size, new_size);
6238  unsigned int to_read = FFMIN(size, alloc_size) - offset;
6239  if (!new_buffer) {
6240  av_free(buffer);
6241  return AVERROR(ENOMEM);
6242  }
6243  buffer = new_buffer;
6244 
6245  if (avio_read(pb, buffer + offset, to_read) != to_read) {
6246  av_free(buffer);
6247  return AVERROR_INVALIDDATA;
6248  }
6249  offset += to_read;
6250  }
6251 
6252  *data = buffer;
6253  return 0;
6254 }
6255 
6257 {
6258  MOVEncryptionIndex *encryption_index;
6259  MOVStreamContext *sc;
6260  int ret;
6261  unsigned int sample_count, aux_info_type, aux_info_param;
6262 
6263  ret = get_current_encryption_info(c, &encryption_index, &sc);
6264  if (ret != 1)
6265  return ret;
6266 
6267  if (encryption_index->nb_encrypted_samples) {
6268  // This can happen if we have both saio/saiz and senc atoms.
6269  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
6270  return 0;
6271  }
6272 
6273  if (encryption_index->auxiliary_info_sample_count) {
6274  av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
6275  return AVERROR_INVALIDDATA;
6276  }
6277 
6278  avio_r8(pb); /* version */
6279  if (avio_rb24(pb) & 0x01) { /* flags */
6280  aux_info_type = avio_rb32(pb);
6281  aux_info_param = avio_rb32(pb);
6282  if (sc->cenc.default_encrypted_sample) {
6283  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6284  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
6285  return 0;
6286  }
6287  if (aux_info_param != 0) {
6288  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
6289  return 0;
6290  }
6291  } else {
6292  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6293  if ((aux_info_type == MKBETAG('c','e','n','c') ||
6294  aux_info_type == MKBETAG('c','e','n','s') ||
6295  aux_info_type == MKBETAG('c','b','c','1') ||
6296  aux_info_type == MKBETAG('c','b','c','s')) &&
6297  aux_info_param == 0) {
6298  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
6299  return AVERROR_INVALIDDATA;
6300  } else {
6301  return 0;
6302  }
6303  }
6304  } else if (!sc->cenc.default_encrypted_sample) {
6305  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6306  return 0;
6307  }
6308 
6309  encryption_index->auxiliary_info_default_size = avio_r8(pb);
6310  sample_count = avio_rb32(pb);
6311  encryption_index->auxiliary_info_sample_count = sample_count;
6312 
6313  if (encryption_index->auxiliary_info_default_size == 0) {
6314  ret = mov_try_read_block(pb, sample_count, &encryption_index->auxiliary_info_sizes);
6315  if (ret < 0) {
6316  av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info\n");
6317  return ret;
6318  }
6319  }
6320 
6321  if (encryption_index->auxiliary_offsets_count) {
6322  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6323  }
6324 
6325  return 0;
6326 }
6327 
6329 {
6330  uint64_t *auxiliary_offsets;
6331  MOVEncryptionIndex *encryption_index;
6332  MOVStreamContext *sc;
6333  int i, ret;
6334  unsigned int version, entry_count, aux_info_type, aux_info_param;
6335  unsigned int alloc_size = 0;
6336 
6337  ret = get_current_encryption_info(c, &encryption_index, &sc);
6338  if (ret != 1)
6339  return ret;
6340 
6341  if (encryption_index->nb_encrypted_samples) {
6342  // This can happen if we have both saio/saiz and senc atoms.
6343  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
6344  return 0;
6345  }
6346 
6347  if (encryption_index->auxiliary_offsets_count) {
6348  av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
6349  return AVERROR_INVALIDDATA;
6350  }
6351 
6352  version = avio_r8(pb); /* version */
6353  if (avio_rb24(pb) & 0x01) { /* flags */
6354  aux_info_type = avio_rb32(pb);
6355  aux_info_param = avio_rb32(pb);
6356  if (sc->cenc.default_encrypted_sample) {
6357  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6358  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
6359  return 0;
6360  }
6361  if (aux_info_param != 0) {
6362  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
6363  return 0;
6364  }
6365  } else {
6366  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6367  if ((aux_info_type == MKBETAG('c','e','n','c') ||
6368  aux_info_type == MKBETAG('c','e','n','s') ||
6369  aux_info_type == MKBETAG('c','b','c','1') ||
6370  aux_info_type == MKBETAG('c','b','c','s')) &&
6371  aux_info_param == 0) {
6372  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
6373  return AVERROR_INVALIDDATA;
6374  } else {
6375  return 0;
6376  }
6377  }
6378  } else if (!sc->cenc.default_encrypted_sample) {
6379  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
6380  return 0;
6381  }
6382 
6383  entry_count = avio_rb32(pb);
6384  if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
6385  return AVERROR(ENOMEM);
6386 
6387  for (i = 0; i < entry_count && !pb->eof_reached; i++) {
6388  unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
6389  auxiliary_offsets = av_fast_realloc(
6390  encryption_index->auxiliary_offsets, &alloc_size,
6391  min_offsets * sizeof(*auxiliary_offsets));
6392  if (!auxiliary_offsets) {
6393  av_freep(&encryption_index->auxiliary_offsets);
6394  return AVERROR(ENOMEM);
6395  }
6396  encryption_index->auxiliary_offsets = auxiliary_offsets;
6397 
6398  if (version == 0) {
6399  encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
6400  } else {
6401  encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
6402  }
6403  if (c->frag_index.current >= 0) {
6404  encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
6405  }
6406  }
6407 
6408  if (pb->eof_reached) {
6409  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
6410  av_freep(&encryption_index->auxiliary_offsets);
6411  return AVERROR_INVALIDDATA;
6412  }
6413 
6414  encryption_index->auxiliary_offsets_count = entry_count;
6415 
6416  if (encryption_index->auxiliary_info_sample_count) {
6417  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6418  }
6419 
6420  return 0;
6421 }
6422 
6424 {
6425  AVEncryptionInitInfo *info, *old_init_info;
6426  uint8_t **key_ids;
6427  AVStream *st;
6428  uint8_t *side_data, *extra_data, *old_side_data;
6429  size_t side_data_size;
6430  buffer_size_t old_side_data_size;
6431  int ret = 0;
6432  unsigned int version, kid_count, extra_data_size, alloc_size = 0;
6433 
6434  if (c->fc->nb_streams < 1)
6435  return 0;
6436  st = c->fc->streams[c->fc->nb_streams-1];
6437 
6438  version = avio_r8(pb); /* version */
6439  avio_rb24(pb); /* flags */
6440 
6441  info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
6442  /* key_id_size */ 16, /* data_size */ 0);
6443  if (!info)
6444  return AVERROR(ENOMEM);
6445 
6446  if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
6447  av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
6448  goto finish;
6449  }
6450 
6451  if (version > 0) {
6452  kid_count = avio_rb32(pb);
6453  if (kid_count >= INT_MAX / sizeof(*key_ids)) {
6454  ret = AVERROR(ENOMEM);
6455  goto finish;
6456  }
6457 
6458  for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
6459  unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
6460  key_ids = av_fast_realloc(info->key_ids, &alloc_size,
6461  min_kid_count * sizeof(*key_ids));
6462  if (!key_ids) {
6463  ret = AVERROR(ENOMEM);
6464  goto finish;
6465  }
6466  info->key_ids = key_ids;
6467 
6468  info->key_ids[i] = av_mallocz(16);
6469  if (!info->key_ids[i]) {
6470  ret = AVERROR(ENOMEM);
6471  goto finish;
6472  }
6473  info->num_key_ids = i + 1;
6474 
6475  if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
6476  av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
6477  goto finish;
6478  }
6479  }
6480 
6481  if (pb->eof_reached) {
6482  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
6484  goto finish;
6485  }
6486  }
6487 
6488  extra_data_size = avio_rb32(pb);
6489  ret = mov_try_read_block(pb, extra_data_size, &extra_data);
6490  if (ret < 0)
6491  goto finish;
6492 
6493  av_freep(&info->data); // malloc(0) may still allocate something.
6494  info->data = extra_data;
6495  info->data_size = extra_data_size;
6496 
6497  // If there is existing initialization data, append to the list.
6498  old_side_data = av_stream_get_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
6499  if (old_side_data) {
6500  old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
6501  if (old_init_info) {
6502  // Append to the end of the list.
6503  for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
6504  if (!cur->next) {
6505  cur->next = info;
6506  break;
6507  }
6508  }
6509  info = old_init_info;
6510  } else {
6511  // Assume existing side-data will be valid, so the only error we could get is OOM.
6512  ret = AVERROR(ENOMEM);
6513  goto finish;
6514  }
6515  }
6516 
6517  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
6518  if (!side_data) {
6519  ret = AVERROR(ENOMEM);
6520  goto finish;
6521  }
6523  side_data, side_data_size);
6524  if (ret < 0)
6525  av_free(side_data);
6526 
6527 finish:
6529  return ret;
6530 }
6531 
6533 {
6534  AVStream *st;
6535  MOVStreamContext *sc;
6536 
6537  if (c->fc->nb_streams < 1)
6538  return 0;
6539  st = c->fc->streams[c->fc->nb_streams-1];
6540  sc = st->priv_data;
6541 
6542  if (sc->pseudo_stream_id != 0) {
6543  av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
6544  return AVERROR_PATCHWELCOME;
6545  }
6546 
6547  if (atom.size < 8)
6548  return AVERROR_INVALIDDATA;
6549 
6550  avio_rb32(pb); /* version and flags */
6551 
6552  if (!sc->cenc.default_encrypted_sample) {
6554  if (!sc->cenc.default_encrypted_sample) {
6555  return AVERROR(ENOMEM);
6556  }
6557  }
6558 
6560  return 0;
6561 }
6562 
6564 {
6565  AVStream *st;
6566  MOVStreamContext *sc;
6567  unsigned int version, pattern, is_protected, iv_size;
6568 
6569  if (c->fc->nb_streams < 1)
6570  return 0;
6571  st = c->fc->streams[c->fc->nb_streams-1];
6572  sc = st->priv_data;
6573 
6574  if (sc->pseudo_stream_id != 0) {
6575  av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
6576  return AVERROR_PATCHWELCOME;
6577  }
6578 
6579  if (!sc->cenc.default_encrypted_sample) {
6581  if (!sc->cenc.default_encrypted_sample) {
6582  return AVERROR(ENOMEM);
6583  }
6584  }
6585 
6586  if (atom.size < 20)
6587  return AVERROR_INVALIDDATA;
6588 
6589  version = avio_r8(pb); /* version */
6590  avio_rb24(pb); /* flags */
6591 
6592  avio_r8(pb); /* reserved */
6593  pattern = avio_r8(pb);
6594 
6595  if (version > 0) {
6596  sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
6597  sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
6598  }
6599 
6600  is_protected = avio_r8(pb);
6601  if (is_protected && !sc->cenc.encryption_index) {
6602  // The whole stream should be by-default encrypted.
6604  if (!sc->cenc.encryption_index)
6605  return AVERROR(ENOMEM);
6606  }
6607  sc->cenc.per_sample_iv_size = avio_r8(pb);
6608  if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
6609  sc->cenc.per_sample_iv_size != 16) {
6610  av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
6611  return AVERROR_INVALIDDATA;
6612  }
6613  if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
6614  av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
6615  return AVERROR_INVALIDDATA;
6616  }
6617 
6618  if (is_protected && !sc->cenc.per_sample_iv_size) {
6619  iv_size = avio_r8(pb);
6620  if (iv_size != 8 && iv_size != 16) {
6621  av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
6622  return AVERROR_INVALIDDATA;
6623  }
6624 
6625  if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
6626  av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
6627  return AVERROR_INVALIDDATA;
6628  }
6629  }
6630 
6631  return 0;
6632 }
6633 
6635 {
6636  AVStream *st;
6637  int last, type, size, ret;
6638  uint8_t buf[4];
6639 
6640  if (c->fc->nb_streams < 1)
6641  return 0;
6642  st = c->fc->streams[c->fc->nb_streams-1];
6643 
6644  if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
6645  return AVERROR_INVALIDDATA;
6646 
6647  /* Check FlacSpecificBox version. */
6648  if (avio_r8(pb) != 0)
6649  return AVERROR_INVALIDDATA;
6650 
6651  avio_rb24(pb); /* Flags */
6652 
6653  avio_read(pb, buf, sizeof(buf));
6654  flac_parse_block_header(buf, &last, &type, &size);
6655 
6657  av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
6658  return AVERROR_INVALIDDATA;
6659  }
6660 
6661  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
6662  if (ret < 0)
6663  return ret;
6664 
6665  if (!last)
6666  av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
6667 
6668  return 0;
6669 }
6670 
6672 {
6673  int i, ret;
6674 
6675  if (sample->scheme != MKBETAG('c','e','n','c') || sample->crypt_byte_block != 0 || sample->skip_byte_block != 0) {
6676  av_log(c->fc, AV_LOG_ERROR, "Only the 'cenc' encryption scheme is supported\n");
6677  return AVERROR_PATCHWELCOME;
6678  }
6679 
6680  if (!sc->cenc.aes_ctr) {
6681  /* initialize the cipher */
6682  sc->cenc.aes_ctr = av_aes_ctr_alloc();
6683  if (!sc->cenc.aes_ctr) {
6684  return AVERROR(ENOMEM);
6685  }
6686 
6687  ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
6688  if (ret < 0) {
6689  return ret;
6690  }
6691  }
6692 
6694 
6695  if (!sample->subsample_count) {
6696  /* decrypt the whole packet */
6698  return 0;
6699  }
6700 
6701  for (i = 0; i < sample->subsample_count; i++) {
6702  if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
6703  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
6704  return AVERROR_INVALIDDATA;
6705  }
6706 
6707  /* skip the clear bytes */
6708  input += sample->subsamples[i].bytes_of_clear_data;
6709  size -= sample->subsamples[i].bytes_of_clear_data;
6710 
6711  /* decrypt the encrypted bytes */
6712  av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, sample->subsamples[i].bytes_of_protected_data);
6713  input += sample->subsamples[i].bytes_of_protected_data;
6714  size -= sample->subsamples[i].bytes_of_protected_data;
6715  }
6716 
6717  if (size > 0) {
6718  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
6719  return AVERROR_INVALIDDATA;
6720  }
6721 
6722  return 0;
6723 }
6724 
6725 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
6726 {
6727  MOVFragmentStreamInfo *frag_stream_info;
6728  MOVEncryptionIndex *encryption_index;
6729  AVEncryptionInfo *encrypted_sample;
6730  int encrypted_index, ret;
6731 
6732  frag_stream_info = get_frag_stream_info(&mov->frag_index, mov->frag_index.current, st->id);
6733  encrypted_index = current_index;
6734  encryption_index = NULL;
6735  if (frag_stream_info) {
6736  // Note this only supports encryption info in the first sample descriptor.
6737  if (mov->fragment.stsd_id == 1) {
6738  if (frag_stream_info->encryption_index) {
6739  encrypted_index = current_index - frag_stream_info->index_entry;
6740  encryption_index = frag_stream_info->encryption_index;
6741  } else {
6742  encryption_index = sc->cenc.encryption_index;
6743  }
6744  }
6745  } else {
6746  encryption_index = sc->cenc.encryption_index;
6747  }
6748 
6749  if (encryption_index) {
6750  if (encryption_index->auxiliary_info_sample_count &&
6751  !encryption_index->nb_encrypted_samples) {
6752  av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
6753  return AVERROR_INVALIDDATA;
6754  }
6755  if (encryption_index->auxiliary_offsets_count &&
6756  !encryption_index->nb_encrypted_samples) {
6757  av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
6758  return AVERROR_INVALIDDATA;
6759  }
6760 
6761  if (!encryption_index->nb_encrypted_samples) {
6762  // Full-sample encryption with default settings.
6763  encrypted_sample = sc->cenc.default_encrypted_sample;
6764  } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
6765  // Per-sample setting override.
6766  encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
6767  } else {
6768  av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
6769  return AVERROR_INVALIDDATA;
6770  }
6771 
6772  if (mov->decryption_key) {
6773  return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
6774  } else {
6775  size_t size;
6776  uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
6777  if (!side_data)
6778  return AVERROR(ENOMEM);
6780  if (ret < 0)
6781  av_free(side_data);
6782  return ret;
6783  }
6784  }
6785 
6786  return 0;
6787 }
6788 
6790 {
6791  const int OPUS_SEEK_PREROLL_MS = 80;
6792  int ret;
6793  AVStream *st;
6794  size_t size;
6795  uint16_t pre_skip;
6796 
6797  if (c->fc->nb_streams < 1)
6798  return 0;
6799  st = c->fc->streams[c->fc->nb_streams-1];
6800 
6801  if ((uint64_t)atom.size > (1<<30) || atom.size < 11)
6802  return AVERROR_INVALIDDATA;
6803 
6804  /* Check OpusSpecificBox version. */
6805  if (avio_r8(pb) != 0) {
6806  av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
6807  return AVERROR_INVALIDDATA;
6808  }
6809 
6810  /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
6811  size = atom.size + 8;
6812 
6813  if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
6814  return ret;
6815 
6816  AV_WL32(st->codecpar->extradata, MKTAG('O','p','u','s'));
6817  AV_WL32(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
6818  AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
6819  avio_read(pb, st->codecpar->extradata + 9, size - 9);
6820 
6821  /* OpusSpecificBox is stored in big-endian, but OpusHead is
6822  little-endian; aside from the preceeding magic and version they're
6823  otherwise currently identical. Data after output gain at offset 16
6824  doesn't need to be bytewapped. */
6825  pre_skip = AV_RB16(st->codecpar->extradata + 10);
6826  AV_WL16(st->codecpar->extradata + 10, pre_skip);
6827  AV_WL32(st->codecpar->extradata + 12, AV_RB32(st->codecpar->extradata + 12));
6828  AV_WL16(st->codecpar->extradata + 16, AV_RB16(st->codecpar->extradata + 16));
6829 
6830  st->codecpar->initial_padding = pre_skip;
6832  (AVRational){1, 1000},
6833  (AVRational){1, 48000});
6834 
6835  return 0;
6836 }
6837 
6839 {
6840  AVStream *st;
6841  unsigned format_info;
6842  int channel_assignment, channel_assignment1, channel_assignment2;
6843  int ratebits;
6844 
6845  if (c->fc->nb_streams < 1)
6846  return 0;
6847  st = c->fc->streams[c->fc->nb_streams-1];
6848 
6849  if (atom.size < 10)
6850  return AVERROR_INVALIDDATA;
6851 
6852  format_info = avio_rb32(pb);
6853 
6854  ratebits = (format_info >> 28) & 0xF;
6855  channel_assignment1 = (format_info >> 15) & 0x1F;
6856  channel_assignment2 = format_info & 0x1FFF;
6857  if (channel_assignment2)
6858  channel_assignment = channel_assignment2;
6859  else
6860  channel_assignment = channel_assignment1;
6861 
6862  st->codecpar->frame_size = 40 << (ratebits & 0x7);
6863  st->codecpar->sample_rate = mlp_samplerate(ratebits);
6864  st->codecpar->channels = truehd_channels(channel_assignment);
6865  st->codecpar->channel_layout = truehd_layout(channel_assignment);
6866 
6867  return 0;
6868 }
6869 
6871 {
6872  AVStream *st;
6873  uint32_t buf;
6875  size_t dovi_size;
6876  int ret;
6877 
6878  if (c->fc->nb_streams < 1)
6879  return 0;
6880  st = c->fc->streams[c->fc->nb_streams-1];
6881 
6882  if ((uint64_t)atom.size > (1<<30) || atom.size < 4)
6883  return AVERROR_INVALIDDATA;
6884 
6885  dovi = av_dovi_alloc(&dovi_size);
6886  if (!dovi)
6887  return AVERROR(ENOMEM);
6888 
6889  dovi->dv_version_major = avio_r8(pb);
6890  dovi->dv_version_minor = avio_r8(pb);
6891 
6892  buf = avio_rb16(pb);
6893  dovi->dv_profile = (buf >> 9) & 0x7f; // 7 bits
6894  dovi->dv_level = (buf >> 3) & 0x3f; // 6 bits
6895  dovi->rpu_present_flag = (buf >> 2) & 0x01; // 1 bit
6896  dovi->el_present_flag = (buf >> 1) & 0x01; // 1 bit
6897  dovi->bl_present_flag = buf & 0x01; // 1 bit
6898  if (atom.size >= 24) { // 4 + 4 + 4 * 4
6899  buf = avio_r8(pb);
6900  dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
6901  } else {
6902  // 0 stands for None
6903  // Dolby Vision V1.2.93 profiles and levels
6905  }
6906 
6908  (uint8_t *)dovi, dovi_size);
6909  if (ret < 0) {
6910  av_free(dovi);
6911  return ret;
6912  }
6913 
6914  av_log(c, AV_LOG_TRACE, "DOVI in dvcC/dvvC box, version: %d.%d, profile: %d, level: %d, "
6915  "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
6916  dovi->dv_version_major, dovi->dv_version_minor,
6917  dovi->dv_profile, dovi->dv_level,
6918  dovi->rpu_present_flag,
6919  dovi->el_present_flag,
6920  dovi->bl_present_flag,
6921  dovi->dv_bl_signal_compatibility_id
6922  );
6923 
6924  return 0;
6925 }
6926 
6928 { MKTAG('A','C','L','R'), mov_read_aclr },
6929 { MKTAG('A','P','R','G'), mov_read_avid },
6930 { MKTAG('A','A','L','P'), mov_read_avid },
6931 { MKTAG('A','R','E','S'), mov_read_ares },
6932 { MKTAG('a','v','s','s'), mov_read_avss },
6933 { MKTAG('a','v','1','C'), mov_read_av1c },
6934 { MKTAG('c','h','p','l'), mov_read_chpl },
6935 { MKTAG('c','o','6','4'), mov_read_stco },
6936 { MKTAG('c','o','l','r'), mov_read_colr },
6937 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
6938 { MKTAG('d','i','n','f'), mov_read_default },
6939 { MKTAG('D','p','x','E'), mov_read_dpxe },
6940 { MKTAG('d','r','e','f'), mov_read_dref },
6941 { MKTAG('e','d','t','s'), mov_read_default },
6942 { MKTAG('e','l','s','t'), mov_read_elst },
6943 { MKTAG('e','n','d','a'), mov_read_enda },
6944 { MKTAG('f','i','e','l'), mov_read_fiel },
6945 { MKTAG('a','d','r','m'), mov_read_adrm },
6946 { MKTAG('f','t','y','p'), mov_read_ftyp },
6947 { MKTAG('g','l','b','l'), mov_read_glbl },
6948 { MKTAG('h','d','l','r'), mov_read_hdlr },
6949 { MKTAG('i','l','s','t'), mov_read_ilst },
6950 { MKTAG('j','p','2','h'), mov_read_jp2h },
6951 { MKTAG('m','d','a','t'), mov_read_mdat },
6952 { MKTAG('m','d','h','d'), mov_read_mdhd },
6953 { MKTAG('m','d','i','a'), mov_read_default },
6954 { MKTAG('m','e','t','a'), mov_read_meta },
6955 { MKTAG('m','i','n','f'), mov_read_default },
6956 { MKTAG('m','o','o','f'), mov_read_moof },
6957 { MKTAG('m','o','o','v'), mov_read_moov },
6958 { MKTAG('m','v','e','x'), mov_read_default },
6959 { MKTAG('m','v','h','d'), mov_read_mvhd },
6960 { MKTAG('S','M','I',' '), mov_read_svq3 },
6961 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
6962 { MKTAG('a','v','c','C'), mov_read_glbl },
6963 { MKTAG('p','a','s','p'), mov_read_pasp },
6964 { MKTAG('s','i','d','x'), mov_read_sidx },
6965 { MKTAG('s','t','b','l'), mov_read_default },
6966 { MKTAG('s','t','c','o'), mov_read_stco },
6967 { MKTAG('s','t','p','s'), mov_read_stps },
6968 { MKTAG('s','t','r','f'), mov_read_strf },
6969 { MKTAG('s','t','s','c'), mov_read_stsc },
6970 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
6971 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
6972 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
6973 { MKTAG('s','t','t','s'), mov_read_stts },
6974 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
6975 { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
6976 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
6977 { MKTAG('t','f','d','t'), mov_read_tfdt },
6978 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
6979 { MKTAG('t','r','a','k'), mov_read_trak },
6980 { MKTAG('t','r','a','f'), mov_read_default },
6981 { MKTAG('t','r','e','f'), mov_read_default },
6982 { MKTAG('t','m','c','d'), mov_read_tmcd },
6983 { MKTAG('c','h','a','p'), mov_read_chap },
6984 { MKTAG('t','r','e','x'), mov_read_trex },
6985 { MKTAG('t','r','u','n'), mov_read_trun },
6986 { MKTAG('u','d','t','a'), mov_read_default },
6987 { MKTAG('w','a','v','e'), mov_read_wave },
6988 { MKTAG('e','s','d','s'), mov_read_esds },
6989 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
6990 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
6991 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
6992 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
6993 { MKTAG('w','f','e','x'), mov_read_wfex },
6994 { MKTAG('c','m','o','v'), mov_read_cmov },
6995 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
6996 { MKTAG('d','v','c','1'), mov_read_dvc1 },
6997 { MKTAG('s','b','g','p'), mov_read_sbgp },
6998 { MKTAG('h','v','c','C'), mov_read_glbl },
6999 { MKTAG('u','u','i','d'), mov_read_uuid },
7000 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
7001 { MKTAG('f','r','e','e'), mov_read_free },
7002 { MKTAG('-','-','-','-'), mov_read_custom },
7003 { MKTAG('s','i','n','f'), mov_read_default },
7004 { MKTAG('f','r','m','a'), mov_read_frma },
7005 { MKTAG('s','e','n','c'), mov_read_senc },
7006 { MKTAG('s','a','i','z'), mov_read_saiz },
7007 { MKTAG('s','a','i','o'), mov_read_saio },
7008 { MKTAG('p','s','s','h'), mov_read_pssh },
7009 { MKTAG('s','c','h','m'), mov_read_schm },
7010 { MKTAG('s','c','h','i'), mov_read_default },
7011 { MKTAG('t','e','n','c'), mov_read_tenc },
7012 { MKTAG('d','f','L','a'), mov_read_dfla },
7013 { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
7014 { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
7015 { MKTAG('d','O','p','s'), mov_read_dops },
7016 { MKTAG('d','m','l','p'), mov_read_dmlp },
7017 { MKTAG('S','m','D','m'), mov_read_smdm },
7018 { MKTAG('C','o','L','L'), mov_read_coll },
7019 { MKTAG('v','p','c','C'), mov_read_vpcc },
7020 { MKTAG('m','d','c','v'), mov_read_mdcv },
7021 { MKTAG('c','l','l','i'), mov_read_clli },
7022 { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
7023 { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
7024 { 0, NULL }
7025 };
7026 
7028 {
7029  int64_t total_size = 0;
7030  MOVAtom a;
7031  int i;
7032 
7033  if (c->atom_depth > 10) {
7034  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
7035  return AVERROR_INVALIDDATA;
7036  }
7037  c->atom_depth ++;
7038 
7039  if (atom.size < 0)
7040  atom.size = INT64_MAX;
7041  while (total_size <= atom.size - 8 && !avio_feof(pb)) {
7043  a.size = atom.size;
7044  a.type=0;
7045  if (atom.size >= 8) {
7046  a.size = avio_rb32(pb);
7047  a.type = avio_rl32(pb);
7048  if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
7049  a.type == MKTAG('h','o','o','v')) &&
7050  a.size >= 8 &&
7051  c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
7052  uint32_t type;
7053  avio_skip(pb, 4);
7054  type = avio_rl32(pb);
7055  if (avio_feof(pb))
7056  break;
7057  avio_seek(pb, -8, SEEK_CUR);
7058  if (type == MKTAG('m','v','h','d') ||
7059  type == MKTAG('c','m','o','v')) {
7060  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
7061  a.type = MKTAG('m','o','o','v');
7062  }
7063  }
7064  if (atom.type != MKTAG('r','o','o','t') &&
7065  atom.type != MKTAG('m','o','o','v')) {
7066  if (a.type == MKTAG('t','r','a','k') ||
7067  a.type == MKTAG('m','d','a','t')) {
7068  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
7069  avio_skip(pb, -8);
7070  c->atom_depth --;
7071  return 0;
7072  }
7073  }
7074  total_size += 8;
7075  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
7076  a.size = avio_rb64(pb) - 8;
7077  total_size += 8;
7078  }
7079  }
7080  av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
7081  av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
7082  if (a.size == 0) {
7083  a.size = atom.size - total_size + 8;
7084  }
7085  if (a.size < 0)
7086  break;
7087  a.size -= 8;
7088  if (a.size < 0)
7089  break;
7090  a.size = FFMIN(a.size, atom.size - total_size);
7091 
7092  for (i = 0; mov_default_parse_table[i].type; i++)
7093  if (mov_default_parse_table[i].type == a.type) {
7095  break;
7096  }
7097 
7098  // container is user data
7099  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
7100  atom.type == MKTAG('i','l','s','t')))
7102 
7103  // Supports parsing the QuickTime Metadata Keys.
7104  // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
7105  if (!parse && c->found_hdlr_mdta &&
7106  atom.type == MKTAG('m','e','t','a') &&
7107  a.type == MKTAG('k','e','y','s') &&
7108  c->meta_keys_count == 0) {
7109  parse = mov_read_keys;
7110  }
7111 
7112  if (!parse) { /* skip leaf atoms data */
7113  avio_skip(pb, a.size);
7114  } else {
7115  int64_t start_pos = avio_tell(pb);
7116  int64_t left;
7117  int err = parse(c, pb, a);
7118  if (err < 0) {
7119  c->atom_depth --;
7120  return err;
7121  }
7122  if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
7123  ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
7124  start_pos + a.size == avio_size(pb))) {
7125  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
7126  c->next_root_atom = start_pos + a.size;
7127  c->atom_depth --;
7128  return 0;
7129  }
7130  left = a.size - avio_tell(pb) + start_pos;
7131  if (left > 0) /* skip garbage at atom end */
7132  avio_skip(pb, left);
7133  else if (left < 0) {
7134  av_log(c->fc, AV_LOG_WARNING,
7135  "overread end of atom '%s' by %"PRId64" bytes\n",
7136  av_fourcc2str(a.type), -left);
7137  avio_seek(pb, left, SEEK_CUR);
7138  }
7139  }
7140 
7141  total_size += a.size;
7142  }
7143 
7144  if (total_size < atom.size && atom.size < 0x7ffff)
7145  avio_skip(pb, atom.size - total_size);
7146 
7147  c->atom_depth --;
7148  return 0;
7149 }
7150 
7151 static int mov_probe(const AVProbeData *p)
7152 {
7153  int64_t offset;
7154  uint32_t tag;
7155  int score = 0;
7156  int moov_offset = -1;
7157 
7158  /* check file header */
7159  offset = 0;
7160  for (;;) {
7161  int64_t size;
7162  int minsize = 8;
7163  /* ignore invalid offset */
7164  if ((offset + 8ULL) > (unsigned int)p->buf_size)
7165  break;
7166  size = AV_RB32(p->buf + offset);
7167  if (size == 1 && offset + 16 <= (unsigned int)p->buf_size) {
7168  size = AV_RB64(p->buf+offset + 8);
7169  minsize = 16;
7170  } else if (size == 0) {
7171  size = p->buf_size - offset;
7172  }
7173  if (size < minsize) {
7174  offset += 4;
7175  continue;
7176  }
7177  tag = AV_RL32(p->buf + offset + 4);
7178  switch(tag) {
7179  /* check for obvious tags */
7180  case MKTAG('m','o','o','v'):
7181  moov_offset = offset + 4;
7182  case MKTAG('m','d','a','t'):
7183  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
7184  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
7185  case MKTAG('f','t','y','p'):
7186  if (tag == MKTAG('f','t','y','p') &&
7187  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
7188  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
7189  )) {
7190  score = FFMAX(score, 5);
7191  } else {
7192  score = AVPROBE_SCORE_MAX;
7193  }
7194  break;
7195  /* those are more common words, so rate then a bit less */
7196  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
7197  case MKTAG('w','i','d','e'):
7198  case MKTAG('f','r','e','e'):
7199  case MKTAG('j','u','n','k'):
7200  case MKTAG('p','i','c','t'):
7201  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
7202  break;
7203  case MKTAG(0x82,0x82,0x7f,0x7d):
7204  case MKTAG('s','k','i','p'):
7205  case MKTAG('u','u','i','d'):
7206  case MKTAG('p','r','f','l'):
7207  /* if we only find those cause probedata is too small at least rate them */
7208  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
7209  break;
7210  }
7211  if (size > INT64_MAX - offset)
7212  break;
7213  offset += size;
7214  }
7215  if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
7216  /* moov atom in the header - we should make sure that this is not a
7217  * MOV-packed MPEG-PS */
7218  offset = moov_offset;
7219 
7220  while (offset < (p->buf_size - 16)) { /* Sufficient space */
7221  /* We found an actual hdlr atom */
7222  if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
7223  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
7224  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
7225  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
7226  /* We found a media handler reference atom describing an
7227  * MPEG-PS-in-MOV, return a
7228  * low score to force expanding the probe window until
7229  * mpegps_probe finds what it needs */
7230  return 5;
7231  } else {
7232  /* Keep looking */
7233  offset += 2;
7234  }
7235  }
7236  }
7237 
7238  return score;
7239 }
7240 
7241 // must be done after parsing all trak because there's no order requirement
7243 {
7244  MOVContext *mov = s->priv_data;
7245  AVStream *st;
7246  MOVStreamContext *sc;
7247  int64_t cur_pos;
7248  int i, j;
7249  int chapter_track;
7250 
7251  for (j = 0; j < mov->nb_chapter_tracks; j++) {
7252  chapter_track = mov->chapter_tracks[j];
7253  st = NULL;
7254  for (i = 0; i < s->nb_streams; i++)
7255  if (s->streams[i]->id == chapter_track) {
7256  st = s->streams[i];
7257  break;
7258  }
7259  if (!st) {
7260  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
7261  continue;
7262  }
7263 
7264  sc = st->priv_data;
7265  cur_pos = avio_tell(sc->pb);
7266 
7267  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
7269  if (st->nb_index_entries) {
7270  // Retrieve the first frame, if possible
7271  AVIndexEntry *sample = &st->index_entries[0];
7272  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
7273  av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
7274  goto finish;
7275  }
7276 
7277  if (av_get_packet(sc->pb, &st->attached_pic, sample->size) < 0)
7278  goto finish;
7279 
7280  st->attached_pic.stream_index = st->index;
7282  }
7283  } else {
7286  st->discard = AVDISCARD_ALL;
7287  for (i = 0; i < st->nb_index_entries; i++) {
7289  int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
7290  uint8_t *title;
7291  uint16_t ch;
7292  int len, title_len;
7293 
7294  if (end < sample->timestamp) {
7295  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
7296  end = AV_NOPTS_VALUE;
7297  }
7298 
7299  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
7300  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
7301  goto finish;
7302  }
7303 
7304  // the first two bytes are the length of the title
7305  len = avio_rb16(sc->pb);
7306  if (len > sample->size-2)
7307  continue;
7308  title_len = 2*len + 1;
7309  if (!(title = av_mallocz(title_len)))
7310  goto finish;
7311 
7312  // The samples could theoretically be in any encoding if there's an encd
7313  // atom following, but in practice are only utf-8 or utf-16, distinguished
7314  // instead by the presence of a BOM
7315  if (!len) {
7316  title[0] = 0;
7317  } else {
7318  ch = avio_rb16(sc->pb);
7319  if (ch == 0xfeff)
7320  avio_get_str16be(sc->pb, len, title, title_len);
7321  else if (ch == 0xfffe)
7322  avio_get_str16le(sc->pb, len, title, title_len);
7323  else {
7324  AV_WB16(title, ch);
7325  if (len == 1 || len == 2)
7326  title[len] = 0;
7327  else
7328  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
7329  }
7330  }
7331 
7332  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
7333  av_freep(&title);
7334  }
7335  }
7336 finish:
7337  avio_seek(sc->pb, cur_pos, SEEK_SET);
7338  }
7339 }
7340 
7342  uint32_t value, int flags)
7343 {
7344  AVTimecode tc;
7345  char buf[AV_TIMECODE_STR_SIZE];
7346  AVRational rate = st->avg_frame_rate;
7347  int ret = av_timecode_init(&tc, rate, flags, 0, s);
7348  if (ret < 0)
7349  return ret;
7350  av_dict_set(&st->metadata, "timecode",
7351  av_timecode_make_string(&tc, buf, value), 0);
7352  return 0;
7353 }
7354 
7356 {
7357  MOVStreamContext *sc = st->priv_data;
7358  char buf[AV_TIMECODE_STR_SIZE];
7359  int64_t cur_pos = avio_tell(sc->pb);
7360  int hh, mm, ss, ff, drop;
7361 
7362  if (!st->nb_index_entries)
7363  return -1;
7364 
7365  avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
7366  avio_skip(s->pb, 13);
7367  hh = avio_r8(s->pb);
7368  mm = avio_r8(s->pb);
7369  ss = avio_r8(s->pb);
7370  drop = avio_r8(s->pb);
7371  ff = avio_r8(s->pb);
7372  snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
7373  hh, mm, ss, drop ? ';' : ':', ff);
7374  av_dict_set(&st->metadata, "timecode", buf, 0);
7375 
7376  avio_seek(sc->pb, cur_pos, SEEK_SET);
7377  return 0;
7378 }
7379 
7381 {
7382  MOVStreamContext *sc = st->priv_data;
7383  int flags = 0;
7384  int64_t cur_pos = avio_tell(sc->pb);
7385  uint32_t value;
7386 
7387  if (!st->nb_index_entries)
7388  return -1;
7389 
7390  avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
7391  value = avio_rb32(s->pb);
7392 
7393  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
7394  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
7395  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
7396 
7397  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
7398  * not the case) and thus assume "frame number format" instead of QT one.
7399  * No sample with tmcd track can be found with a QT timecode at the moment,
7400  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
7401  * format). */
7403 
7404  avio_seek(sc->pb, cur_pos, SEEK_SET);
7405  return 0;
7406 }
7407 
7409  int i;
7410  if (!index || !*index) return;
7411  for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
7412  av_encryption_info_free((*index)->encrypted_samples[i]);
7413  }
7414  av_freep(&(*index)->encrypted_samples);
7415  av_freep(&(*index)->auxiliary_info_sizes);
7416  av_freep(&(*index)->auxiliary_offsets);
7417  av_freep(index);
7418 }
7419 
7421 {
7422  MOVContext *mov = s->priv_data;
7423  int i, j;
7424 
7425  for (i = 0; i < s->nb_streams; i++) {
7426  AVStream *st = s->streams[i];
7427  MOVStreamContext *sc = st->priv_data;
7428 
7429  if (!sc)
7430  continue;
7431 
7432  av_freep(&sc->ctts_data);
7433  for (j = 0; j < sc->drefs_count; j++) {
7434  av_freep(&sc->drefs[j].path);
7435  av_freep(&sc->drefs[j].dir);
7436  }
7437  av_freep(&sc->drefs);
7438 
7439  sc->drefs_count = 0;
7440 
7441  if (!sc->pb_is_copied)
7442  ff_format_io_close(s, &sc->pb);
7443 
7444  sc->pb = NULL;
7445  av_freep(&sc->chunk_offsets);
7446  av_freep(&sc->stsc_data);
7447  av_freep(&sc->sample_sizes);
7448  av_freep(&sc->keyframes);
7449  av_freep(&sc->stts_data);
7450  av_freep(&sc->sdtp_data);
7451  av_freep(&sc->stps_data);
7452  av_freep(&sc->elst_data);
7453  av_freep(&sc->rap_group);
7454  av_freep(&sc->display_matrix);
7455  av_freep(&sc->index_ranges);
7456 
7457  if (sc->extradata)
7458  for (j = 0; j < sc->stsd_count; j++)
7459  av_free(sc->extradata[j]);
7460  av_freep(&sc->extradata);
7461  av_freep(&sc->extradata_size);
7462 
7466 
7467  av_freep(&sc->stereo3d);
7468  av_freep(&sc->spherical);
7469  av_freep(&sc->mastering);
7470  av_freep(&sc->coll);
7471  }
7472 
7473  av_freep(&mov->dv_demux);
7475  mov->dv_fctx = NULL;
7476 
7477  if (mov->meta_keys) {
7478  for (i = 1; i < mov->meta_keys_count; i++) {
7479  av_freep(&mov->meta_keys[i]);
7480  }
7481  av_freep(&mov->meta_keys);
7482  }
7483 
7484  av_freep(&mov->trex_data);
7485  av_freep(&mov->bitrates);
7486 
7487  for (i = 0; i < mov->frag_index.nb_items; i++) {
7489  for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
7490  mov_free_encryption_index(&frag[j].encryption_index);
7491  }
7493  }
7494  av_freep(&mov->frag_index.item);
7495 
7496  av_freep(&mov->aes_decrypt);
7497  av_freep(&mov->chapter_tracks);
7498 
7499  return 0;
7500 }
7501 
7502 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
7503 {
7504  int i;
7505 
7506  for (i = 0; i < s->nb_streams; i++) {
7507  AVStream *st = s->streams[i];
7508  MOVStreamContext *sc = st->priv_data;
7509 
7510  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
7511  sc->timecode_track == tmcd_id)
7512  return 1;
7513  }
7514  return 0;
7515 }
7516 
7517 /* look for a tmcd track not referenced by any video track, and export it globally */
7519 {
7520  int i;
7521 
7522  for (i = 0; i < s->nb_streams; i++) {
7523  AVStream *st = s->streams[i];
7524 
7525  if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
7526  !tmcd_is_referenced(s, i + 1)) {
7527  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
7528  if (tcr) {
7529  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
7530  break;
7531  }
7532  }
7533  }
7534 }
7535 
7536 static int read_tfra(MOVContext *mov, AVIOContext *f)
7537 {
7538  int version, fieldlength, i, j;
7539  int64_t pos = avio_tell(f);
7540  uint32_t size = avio_rb32(f);
7541  unsigned track_id, item_count;
7542 
7543  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
7544  return 1;
7545  }
7546  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
7547 
7548  version = avio_r8(f);
7549  avio_rb24(f);
7550  track_id = avio_rb32(f);
7551  fieldlength = avio_rb32(f);
7552  item_count = avio_rb32(f);
7553  for (i = 0; i < item_count; i++) {
7554  int64_t time, offset;
7555  int index;
7556  MOVFragmentStreamInfo * frag_stream_info;
7557 
7558  if (avio_feof(f)) {
7559  return AVERROR_INVALIDDATA;
7560  }
7561 
7562  if (version == 1) {
7563  time = avio_rb64(f);
7564  offset = avio_rb64(f);
7565  } else {
7566  time = avio_rb32(f);
7567  offset = avio_rb32(f);
7568  }
7569 
7570  // The first sample of each stream in a fragment is always a random
7571  // access sample. So it's entry in the tfra can be used as the
7572  // initial PTS of the fragment.
7573  index = update_frag_index(mov, offset);
7574  frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
7575  if (frag_stream_info &&
7576  frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
7577  frag_stream_info->first_tfra_pts = time;
7578 
7579  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
7580  avio_r8(f);
7581  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
7582  avio_r8(f);
7583  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
7584  avio_r8(f);
7585  }
7586 
7587  avio_seek(f, pos + size, SEEK_SET);
7588  return 0;
7589 }
7590 
7592 {
7593  int64_t stream_size = avio_size(f);
7594  int64_t original_pos = avio_tell(f);
7595  int64_t seek_ret;
7596  int ret = -1;
7597  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
7598  ret = seek_ret;
7599  goto fail;
7600  }
7601  c->mfra_size = avio_rb32(f);
7602  c->have_read_mfra_size = 1;
7603  if (!c->mfra_size || c->mfra_size > stream_size) {
7604  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
7605  goto fail;
7606  }
7607  if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
7608  ret = seek_ret;
7609  goto fail;
7610  }
7611  if (avio_rb32(f) != c->mfra_size) {
7612  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
7613  goto fail;
7614  }
7615  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
7616  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
7617  goto fail;
7618  }
7619  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
7620  do {
7621  ret = read_tfra(c, f);
7622  if (ret < 0)
7623  goto fail;
7624  } while (!ret);
7625  ret = 0;
7626  c->frag_index.complete = 1;
7627 fail:
7628  seek_ret = avio_seek(f, original_pos, SEEK_SET);
7629  if (seek_ret < 0) {
7630  av_log(c->fc, AV_LOG_ERROR,
7631  "failed to seek back after looking for mfra\n");
7632  ret = seek_ret;
7633  }
7634  return ret;
7635 }
7636 
7638 {
7639  MOVContext *mov = s->priv_data;
7640  AVIOContext *pb = s->pb;
7641  int j, err;
7642  MOVAtom atom = { AV_RL32("root") };
7643  int i;
7644 
7645  if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
7646  av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
7648  return AVERROR(EINVAL);
7649  }
7650 
7651  mov->fc = s;
7652  mov->trak_index = -1;
7653  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
7654  if (pb->seekable & AVIO_SEEKABLE_NORMAL)
7655  atom.size = avio_size(pb);
7656  else
7657  atom.size = INT64_MAX;
7658 
7659  /* check MOV header */
7660  do {
7661  if (mov->moov_retry)
7662  avio_seek(pb, 0, SEEK_SET);
7663  if ((err = mov_read_default(mov, pb, atom)) < 0) {
7664  av_log(s, AV_LOG_ERROR, "error reading header\n");
7665  goto fail;
7666  }
7667  } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !mov->found_moov && !mov->moov_retry++);
7668  if (!mov->found_moov) {
7669  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
7670  err = AVERROR_INVALIDDATA;
7671  goto fail;
7672  }
7673  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
7674 
7675  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
7676  if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
7678  for (i = 0; i < s->nb_streams; i++)
7679  if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
7680  mov_read_timecode_track(s, s->streams[i]);
7681  } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
7682  mov_read_rtmd_track(s, s->streams[i]);
7683  }
7684  }
7685 
7686  /* copy timecode metadata from tmcd tracks to the related video streams */
7687  for (i = 0; i < s->nb_streams; i++) {
7688  AVStream *st = s->streams[i];
7689  MOVStreamContext *sc = st->priv_data;
7690  if (sc->timecode_track > 0) {
7691  AVDictionaryEntry *tcr;
7692  int tmcd_st_id = -1;
7693 
7694  for (j = 0; j < s->nb_streams; j++)
7695  if (s->streams[j]->id == sc->timecode_track)
7696  tmcd_st_id = j;
7697 
7698  if (tmcd_st_id < 0 || tmcd_st_id == i)
7699  continue;
7700  tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
7701  if (tcr)
7702  av_dict_set(&st->metadata, "timecode", tcr->value, 0);
7703  }
7704  }
7706 
7707  for (i = 0; i < s->nb_streams; i++) {
7708  AVStream *st = s->streams[i];
7709  MOVStreamContext *sc = st->priv_data;
7710  fix_timescale(mov, sc);
7711  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
7712  st->codecpar->codec_id == AV_CODEC_ID_AAC) {
7713  st->internal->skip_samples = sc->start_pad;
7714  }
7715  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
7717  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
7719  if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
7720  st->codecpar->width = sc->width;
7721  st->codecpar->height = sc->height;
7722  }
7724  if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
7725  goto fail;
7726  }
7727  }
7728  if (mov->handbrake_version &&
7729  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
7730  st->codecpar->codec_id == AV_CODEC_ID_MP3) {
7731  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
7733  }
7734  }
7735 
7736  if (mov->trex_data) {
7737  for (i = 0; i < s->nb_streams; i++) {
7738  AVStream *st = s->streams[i];
7739  MOVStreamContext *sc = st->priv_data;
7740  if (st->duration > 0) {
7741  if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
7742  av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
7743  sc->data_size, sc->time_scale);
7744  err = AVERROR_INVALIDDATA;
7745  goto fail;
7746  }
7747  st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
7748  }
7749  }
7750  }
7751 
7752  if (mov->use_mfra_for > 0) {
7753  for (i = 0; i < s->nb_streams; i++) {
7754  AVStream *st = s->streams[i];
7755  MOVStreamContext *sc = st->priv_data;
7756  if (sc->duration_for_fps > 0) {
7757  if (sc->data_size > INT64_MAX / sc->time_scale / 8) {
7758  av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
7759  sc->data_size, sc->time_scale);
7760  err = AVERROR_INVALIDDATA;
7761  goto fail;
7762  }
7763  st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale /
7764  sc->duration_for_fps;
7765  }
7766  }
7767  }
7768 
7769  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
7770  if (mov->bitrates[i]) {
7771  s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
7772  }
7773  }
7774 
7776 
7777  for (i = 0; i < s->nb_streams; i++) {
7778  AVStream *st = s->streams[i];
7779  MOVStreamContext *sc = st->priv_data;
7780 
7781  switch (st->codecpar->codec_type) {
7782  case AVMEDIA_TYPE_AUDIO:
7783  err = ff_replaygain_export(st, s->metadata);
7784  if (err < 0)
7785  goto fail;
7786  break;
7787  case AVMEDIA_TYPE_VIDEO:
7788  if (sc->display_matrix) {
7790  sizeof(int32_t) * 9);
7791  if (err < 0)
7792  goto fail;
7793 
7794  sc->display_matrix = NULL;
7795  }
7796  if (sc->stereo3d) {
7798  (uint8_t *)sc->stereo3d,
7799  sizeof(*sc->stereo3d));
7800  if (err < 0)
7801  goto fail;
7802 
7803  sc->stereo3d = NULL;
7804  }
7805  if (sc->spherical) {
7807  (uint8_t *)sc->spherical,
7808  sc->spherical_size);
7809  if (err < 0)
7810  goto fail;
7811 
7812  sc->spherical = NULL;
7813  }
7814  if (sc->mastering) {
7816  (uint8_t *)sc->mastering,
7817  sizeof(*sc->mastering));
7818  if (err < 0)
7819  goto fail;
7820 
7821  sc->mastering = NULL;
7822  }
7823  if (sc->coll) {
7825  (uint8_t *)sc->coll,
7826  sc->coll_size);
7827  if (err < 0)
7828  goto fail;
7829 
7830  sc->coll = NULL;
7831  }
7832  break;
7833  }
7834  }
7836 
7837  for (i = 0; i < mov->frag_index.nb_items; i++)
7838  if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
7839  mov->frag_index.item[i].headers_read = 1;
7840 
7841  return 0;
7842 fail:
7843  mov_read_close(s);
7844  return err;
7845 }
7846 
7848 {
7850  int64_t best_dts = INT64_MAX;
7851  int i;
7852  for (i = 0; i < s->nb_streams; i++) {
7853  AVStream *avst = s->streams[i];
7854  MOVStreamContext *msc = avst->priv_data;
7855  if (msc->pb && msc->current_sample < avst->nb_index_entries) {
7856  AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
7857  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
7858  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
7859  if (!sample || (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && current_sample->pos < sample->pos) ||
7860  ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
7861  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
7862  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
7863  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
7864  sample = current_sample;
7865  best_dts = dts;
7866  *st = avst;
7867  }
7868  }
7869  }
7870  return sample;
7871 }
7872 
7873 static int should_retry(AVIOContext *pb, int error_code) {
7874  if (error_code == AVERROR_EOF || avio_feof(pb))
7875  return 0;
7876 
7877  return 1;
7878 }
7879 
7880 static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
7881 {
7882  int ret;
7883  MOVContext *mov = s->priv_data;
7884 
7885  if (index >= 0 && index < mov->frag_index.nb_items)
7886  target = mov->frag_index.item[index].moof_offset;
7887  if (avio_seek(s->pb, target, SEEK_SET) != target) {
7888  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
7889  return AVERROR_INVALIDDATA;
7890  }
7891 
7892  mov->next_root_atom = 0;
7893  if (index < 0 || index >= mov->frag_index.nb_items)
7894  index = search_frag_moof_offset(&mov->frag_index, target);
7895  if (index < mov->frag_index.nb_items &&
7896  mov->frag_index.item[index].moof_offset == target) {
7897  if (index + 1 < mov->frag_index.nb_items)
7898  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
7899  if (mov->frag_index.item[index].headers_read)
7900  return 0;
7901  mov->frag_index.item[index].headers_read = 1;
7902  }
7903 
7904  mov->found_mdat = 0;
7905 
7906  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
7907  if (ret < 0)
7908  return ret;
7909  if (avio_feof(s->pb))
7910  return AVERROR_EOF;
7911  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
7912 
7913  return 1;
7914 }
7915 
7917 {
7918  uint8_t *side, *extradata;
7919  int extradata_size;
7920 
7921  /* Save the current index. */
7922  sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
7923 
7924  /* Notify the decoder that extradata changed. */
7925  extradata_size = sc->extradata_size[sc->last_stsd_index];
7926  extradata = sc->extradata[sc->last_stsd_index];
7927  if (extradata_size > 0 && extradata) {
7930  extradata_size);
7931  if (!side)
7932  return AVERROR(ENOMEM);
7933  memcpy(side, extradata, extradata_size);
7934  }
7935 
7936  return 0;
7937 }
7938 
7940 {
7941  int new_size, ret;
7942 
7943  if (size <= 8)
7944  return AVERROR_INVALIDDATA;
7945  new_size = ((size - 8) / 2) * 3;
7946  ret = av_new_packet(pkt, new_size);
7947  if (ret < 0)
7948  return ret;
7949 
7950  avio_skip(pb, 8);
7951  for (int j = 0; j < new_size; j += 3) {
7952  pkt->data[j] = 0xFC;
7953  pkt->data[j+1] = avio_r8(pb);
7954  pkt->data[j+2] = avio_r8(pb);
7955  }
7956 
7957  return 0;
7958 }
7959 
7961 {
7962  MOVContext *mov = s->priv_data;
7963  MOVStreamContext *sc;
7965  AVStream *st = NULL;
7966  int64_t current_index;
7967  int ret;
7968  mov->fc = s;
7969  retry:
7970  sample = mov_find_next_sample(s, &st);
7971  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
7972  if (!mov->next_root_atom)
7973  return AVERROR_EOF;
7974  if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
7975  return ret;
7976  goto retry;
7977  }
7978  sc = st->priv_data;
7979  /* must be done just before reading, to avoid infinite loop on sample */
7980  current_index = sc->current_index;
7982 
7983  if (mov->next_root_atom) {
7984  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
7985  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
7986  }
7987 
7988  if (st->discard != AVDISCARD_ALL) {
7989  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
7990  if (ret64 != sample->pos) {
7991  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
7992  sc->ffindex, sample->pos);
7993  if (should_retry(sc->pb, ret64)) {
7995  }
7996  return AVERROR_INVALIDDATA;
7997  }
7998 
7999  if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
8000  av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
8001  goto retry;
8002  }
8003 
8004  if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
8005  ret = get_eia608_packet(sc->pb, pkt, sample->size);
8006  else
8007  ret = av_get_packet(sc->pb, pkt, sample->size);
8008  if (ret < 0) {
8009  if (should_retry(sc->pb, ret)) {
8011  }
8012  return ret;
8013  }
8014 #if CONFIG_DV_DEMUXER
8015  if (mov->dv_demux && sc->dv_audio_container) {
8016  AVBufferRef *buf = pkt->buf;
8018  pkt->buf = buf;
8020  if (ret < 0)
8021  return ret;
8023  if (ret < 0)
8024  return ret;
8025  }
8026 #endif
8027  if (sc->has_palette) {
8028  uint8_t *pal;
8029 
8031  if (!pal) {
8032  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
8033  } else {
8034  memcpy(pal, sc->palette, AVPALETTE_SIZE);
8035  sc->has_palette = 0;
8036  }
8037  }
8038  if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !st->need_parsing && pkt->size > 4) {
8039  if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
8041  }
8042  }
8043 
8044  pkt->stream_index = sc->ffindex;
8045  pkt->dts = sample->timestamp;
8046  if (sample->flags & AVINDEX_DISCARD_FRAME) {
8048  }
8049  if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
8050  pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
8051  /* update ctts context */
8052  sc->ctts_sample++;
8053  if (sc->ctts_index < sc->ctts_count &&
8054  sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
8055  sc->ctts_index++;
8056  sc->ctts_sample = 0;
8057  }
8058  } else {
8059  int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
8061 
8062  if (next_dts >= pkt->dts)
8063  pkt->duration = next_dts - pkt->dts;
8064  pkt->pts = pkt->dts;
8065  }
8066  if (st->discard == AVDISCARD_ALL)
8067  goto retry;
8068  if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
8069  uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
8070  uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
8071  pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
8072  }
8073  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
8074  pkt->pos = sample->pos;
8075 
8076  /* Multiple stsd handling. */
8077  if (sc->stsc_data) {
8078  /* Keep track of the stsc index for the given sample, then check
8079  * if the stsd index is different from the last used one. */
8080  sc->stsc_sample++;
8081  if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
8082  mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
8083  sc->stsc_index++;
8084  sc->stsc_sample = 0;
8085  /* Do not check indexes after a switch. */
8086  } else if (sc->stsc_data[sc->stsc_index].id > 0 &&
8087  sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
8088  sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
8089  ret = mov_change_extradata(sc, pkt);
8090  if (ret < 0)
8091  return ret;
8092  }
8093  }
8094 
8095  if (mov->aax_mode)
8096  aax_filter(pkt->data, pkt->size, mov);
8097 
8098  ret = cenc_filter(mov, st, sc, pkt, current_index);
8099  if (ret < 0) {
8100  return ret;
8101  }
8102 
8103  return 0;
8104 }
8105 
8106 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
8107 {
8108  MOVContext *mov = s->priv_data;
8109  int index;
8110 
8111  if (!mov->frag_index.complete)
8112  return 0;
8113 
8114  index = search_frag_timestamp(&mov->frag_index, st, timestamp);
8115  if (index < 0)
8116  index = 0;
8117  if (!mov->frag_index.item[index].headers_read)
8118  return mov_switch_root(s, -1, index);
8119  if (index + 1 < mov->frag_index.nb_items)
8120  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
8121 
8122  return 0;
8123 }
8124 
8125 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
8126 {
8127  MOVStreamContext *sc = st->priv_data;
8128  int sample, time_sample, ret;
8129  unsigned int i;
8130 
8131  // Here we consider timestamp to be PTS, hence try to offset it so that we
8132  // can search over the DTS timeline.
8133  timestamp -= (sc->min_corrected_pts + sc->dts_shift);
8134 
8135  ret = mov_seek_fragment(s, st, timestamp);
8136  if (ret < 0)
8137  return ret;
8138 
8139  sample = av_index_search_timestamp(st, timestamp, flags);
8140  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
8141  if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
8142  sample = 0;
8143  if (sample < 0) /* not sure what to do */
8144  return AVERROR_INVALIDDATA;
8146  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
8147  /* adjust ctts index */
8148  if (sc->ctts_data) {
8149  time_sample = 0;
8150  for (i = 0; i < sc->ctts_count; i++) {
8151  int next = time_sample + sc->ctts_data[i].count;
8152  if (next > sc->current_sample) {
8153  sc->ctts_index = i;
8154  sc->ctts_sample = sc->current_sample - time_sample;
8155  break;
8156  }
8157  time_sample = next;
8158  }
8159  }
8160 
8161  /* adjust stsd index */
8162  if (sc->chunk_count) {
8163  time_sample = 0;
8164  for (i = 0; i < sc->stsc_count; i++) {
8165  int64_t next = time_sample + mov_get_stsc_samples(sc, i);
8166  if (next > sc->current_sample) {
8167  sc->stsc_index = i;
8168  sc->stsc_sample = sc->current_sample - time_sample;
8169  break;
8170  }
8171  av_assert0(next == (int)next);
8172  time_sample = next;
8173  }
8174  }
8175 
8176  return sample;
8177 }
8178 
8179 static int64_t mov_get_skip_samples(AVStream *st, int sample)
8180 {
8181  MOVStreamContext *sc = st->priv_data;
8182  int64_t first_ts = st->index_entries[0].timestamp;
8183  int64_t ts = st->index_entries[sample].timestamp;
8184  int64_t off;
8185 
8187  return 0;
8188 
8189  /* compute skip samples according to stream start_pad, seek ts and first ts */
8190  off = av_rescale_q(ts - first_ts, st->time_base,
8191  (AVRational){1, st->codecpar->sample_rate});
8192  return FFMAX(sc->start_pad - off, 0);
8193 }
8194 
8195 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
8196 {
8197  MOVContext *mc = s->priv_data;
8198  AVStream *st;
8199  int sample;
8200  int i;
8201 
8202  if (stream_index >= s->nb_streams)
8203  return AVERROR_INVALIDDATA;
8204 
8205  st = s->streams[stream_index];
8206  sample = mov_seek_stream(s, st, sample_time, flags);
8207  if (sample < 0)
8208  return sample;
8209 
8210  if (mc->seek_individually) {
8211  /* adjust seek timestamp to found sample timestamp */
8212  int64_t seek_timestamp = st->index_entries[sample].timestamp;
8214 
8215  for (i = 0; i < s->nb_streams; i++) {
8216  int64_t timestamp;
8217  st = s->streams[i];
8218 
8219  if (stream_index == i)
8220  continue;
8221 
8222  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
8223  sample = mov_seek_stream(s, st, timestamp, flags);
8224  if (sample >= 0)
8226  }
8227  } else {
8228  for (i = 0; i < s->nb_streams; i++) {
8229  MOVStreamContext *sc;
8230  st = s->streams[i];
8231  sc = st->priv_data;
8232  mov_current_sample_set(sc, 0);
8233  }
8234  while (1) {
8235  MOVStreamContext *sc;
8236  AVIndexEntry *entry = mov_find_next_sample(s, &st);
8237  if (!entry)
8238  return AVERROR_INVALIDDATA;
8239  sc = st->priv_data;
8240  if (sc->ffindex == stream_index && sc->current_sample == sample)
8241  break;
8243  }
8244  }
8245  return 0;
8246 }
8247 
8248 #define OFFSET(x) offsetof(MOVContext, x)
8249 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
8250 static const AVOption mov_options[] = {
8251  {"use_absolute_path",
8252  "allow using absolute path when opening alias, this is a possible security issue",
8253  OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
8254  0, 1, FLAGS},
8255  {"seek_streams_individually",
8256  "Seek each stream individually to the closest point",
8257  OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
8258  0, 1, FLAGS},
8259  {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
8260  0, 1, FLAGS},
8261  {"advanced_editlist",
8262  "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
8263  OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
8264  0, 1, FLAGS},
8265  {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
8266  0, 1, FLAGS},
8267  {"use_mfra_for",
8268  "use mfra for fragment timestamps",
8269  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
8271  "use_mfra_for"},
8272  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
8273  FLAGS, "use_mfra_for" },
8274  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
8275  FLAGS, "use_mfra_for" },
8276  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
8277  FLAGS, "use_mfra_for" },
8278  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
8279  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
8280  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
8281  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
8282  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
8284  { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
8286  { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
8288  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
8289  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
8290  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
8291  .flags = AV_OPT_FLAG_DECODING_PARAM },
8292  { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
8293  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
8294  {.i64 = 0}, 0, 1, FLAGS },
8295 
8296  { NULL },
8297 };
8298 
8299 static const AVClass mov_class = {
8300  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
8301  .item_name = av_default_item_name,
8302  .option = mov_options,
8303  .version = LIBAVUTIL_VERSION_INT,
8304 };
8305 
8307  .name = "mov,mp4,m4a,3gp,3g2,mj2",
8308  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
8309  .priv_class = &mov_class,
8310  .priv_data_size = sizeof(MOVContext),
8311  .extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v",
8312  .read_probe = mov_probe,
8318 };
AVStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1090
MOVStreamContext::ctts_allocated_size
unsigned int ctts_allocated_size
Definition: isom.h:170
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
add_ctts_entry
static int64_t add_ctts_entry(MOVStts **ctts_data, unsigned int *ctts_count, unsigned int *allocated_size, int count, int duration)
Append a new ctts entry to ctts_data.
Definition: mov.c:3366
mov_read_chpl
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:538
AVMasteringDisplayMetadata::has_primaries
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
Definition: mastering_display_metadata.h:62
mov_read_frma
static int mov_read_frma(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5958
mov_read_meta
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4511
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:534
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
AV_CODEC_ID_MACE6
@ AV_CODEC_ID_MACE6
Definition: codec_id.h:434
MOVFragmentStreamInfo::first_tfra_pts
int64_t first_tfra_pts
Definition: isom.h:130
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
MOV_TFHD_DEFAULT_FLAGS
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:318
PUT_UTF8
#define PUT_UTF8(val, tmp, PUT_BYTE)
Definition: common.h:552
AV_TIMECODE_STR_SIZE
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
av_aes_init
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:195
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:333
MOVStreamContext::audio_cid
int16_t audio_cid
stsd audio compression id
Definition: isom.h:200
AVMasteringDisplayMetadata::max_luminance
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:57
AVSphericalProjection
AVSphericalProjection
Projection of the video surface(s) on a sphere.
Definition: spherical.h:51
AV_CODEC_ID_ADPCM_MS
@ AV_CODEC_ID_ADPCM_MS
Definition: codec_id.h:359
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
mov_read_dops
static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6789
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:353
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts)
add frame for rfps calculation.
Definition: utils.c:3348
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:470
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:427
MOVStreamContext::height
int height
tkhd height
Definition: isom.h:206
MOVContext::moov_retry
int moov_retry
Definition: isom.h:277
MOV_TRUN_SAMPLE_FLAGS
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:326
MOVContext::nb_chapter_tracks
unsigned int nb_chapter_tracks
Definition: isom.h:266
MOVStreamContext::last_stsd_index
int last_stsd_index
Definition: isom.h:223
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
MOV_TKHD_FLAG_ENABLED
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:339
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVSphericalMapping::projection
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:86
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
MOVStreamContext::extradata
uint8_t ** extradata
extradata array (and size) for multiple stsd
Definition: isom.h:221
mov_class
static const AVClass mov_class
Definition: mov.c:8299
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:170
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, 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:3332
out
FILE * out
Definition: movenc.c:54
MOVFragmentStreamInfo
Definition: isom.h:127
mov_read_targa_y216
static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1770
FFSWAP
#define FFSWAP(type, a, b)
Definition: common.h:108
mov_read_moof
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1409
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_FLAG_IGNIDX
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1365
av_stristr
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
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:149
ff_replaygain_export
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
tmcd_is_referenced
static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
Definition: mov.c:7502
AVStream::priv_data
void * priv_data
Definition: avformat.h:888
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:478
AV_DISPOSITION_ATTACHED_PIC
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:841
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:928
mov_options
static const AVOption mov_options[]
Definition: mov.c:8250
mov_codec_id
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:2076
av_int2double
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
AVMasteringDisplayMetadata::display_primaries
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
Definition: mastering_display_metadata.h:42
AV_PKT_FLAG_DISCARD
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition: packet.h:417
AVMasteringDisplayMetadata::has_luminance
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
Definition: mastering_display_metadata.h:67
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
av_aes_ctr_init
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:69
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
mov_read_alac
static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1742
test_same_origin
static int test_same_origin(const char *src, const char *ref)
Definition: mov.c:4138
MOVFragment::base_data_offset
uint64_t base_data_offset
Definition: isom.h:92
MOVStreamContext
Definition: isom.h:158
MOVStreamContext::stsc_data
MOVStsc * stsc_data
Definition: isom.h:173
IS_MATRIX_IDENT
#define IS_MATRIX_IDENT(matrix)
Definition: mov.c:4529
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:818
mov_update_dts_shift
static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
Definition: mov.c:3046
MOVStreamContext::spherical
AVSphericalMapping * spherical
Definition: isom.h:229
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:61
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:102
avio_get_str16be
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
MOVEncryptionIndex
Definition: isom.h:114
av_encryption_init_info_free
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
Definition: encryption_info.c:214
mov_read_avss
static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1747
AV_PKT_DATA_ENCRYPTION_INFO
@ AV_PKT_DATA_ENCRYPTION_INFO
This side data contains encryption info for how to decrypt the packet.
Definition: packet.h:255
MOVContext::found_moov
int found_moov
'moov' atom has been found
Definition: isom.h:251
mov_read_custom
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4430
pixdesc.h
ID3v1_GENRE_MAX
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
mov_read_extradata
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, enum AVCodecID codec_id)
Definition: mov.c:1716
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
mpegaudiodecheader.h
MOVStreamContext::rap_group_count
unsigned int rap_group_count
Definition: isom.h:214
av_sha_init
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
mov_read_mvhd
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1499
AVPacket::data
uint8_t * data
Definition: packet.h:369
MOVContext::found_mdat
int found_mdat
'mdat' atom has been found
Definition: isom.h:252
MOVStreamContext::drefs_count
unsigned drefs_count
Definition: isom.h:201
AVEncryptionInfo::crypt_byte_block
uint32_t crypt_byte_block
Only used for pattern encryption.
Definition: encryption_info.h:51
mov_metadata_creation_time
static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time, void *logctx)
Definition: mov.c:1435
mov_read_avid
static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1762
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:200
AVStream::internal
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1113
AVOption
AVOption.
Definition: opt.h:248
MOVContext::trex_data
MOVTrackExt * trex_data
Definition: isom.h:261
parse
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
mov_read_stps
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2760
MOVContext::bitrates
int * bitrates
bitrates read before streams creation
Definition: isom.h:275
b
#define b
Definition: input.c:41
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:486
truehd_channels
static int truehd_channels(int chanmap)
Definition: mlp_parse.h:88
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
mov_read_tkhd
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4537
MOVElst::rate
float rate
Definition: isom.h:70
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
spherical.h
mov_read_colr
static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1582
data
const char data[16]
Definition: mxf.c:142
mov_read_strf
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:2001
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:440
yuv_to_rgba
static uint32_t yuv_to_rgba(uint32_t ycbcr)
Definition: mov.c:2294
AV_PKT_DATA_ENCRYPTION_INIT_INFO
@ AV_PKT_DATA_ENCRYPTION_INIT_INFO
This side data is encryption initialization data.
Definition: packet.h:249
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:406
mov_parse_auxiliary_info
static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
Definition: mov.c:6162
mov_seek_stream
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:8125
mov_read_saio
static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6328
mov_get_stsc_samples
static int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
Definition: mov.c:2745
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:508
get_edit_list_entry
static int get_edit_list_entry(MOVContext *mov, const MOVStreamContext *msc, unsigned int edit_list_index, int64_t *edit_list_media_time, int64_t *edit_list_duration, int64_t global_timescale)
Get ith edit list entry (media time, duration).
Definition: mov.c:3173
MOVFragmentIndexItem::moof_offset
int64_t moof_offset
Definition: isom.h:138
MOVStreamContext::spherical_size
size_t spherical_size
Definition: isom.h:230
AVINDEX_DISCARD_FRAME
#define AVINDEX_DISCARD_FRAME
Definition: avformat.h:812
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AV_SPHERICAL_EQUIRECTANGULAR_TILE
@ AV_SPHERICAL_EQUIRECTANGULAR_TILE
Video represents a portion of a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:72
MOVDref::dir
char * dir
Definition: isom.h:76
mov_current_sample_set
static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
Definition: mov.c:3474
mathematics.h
AVDictionary
Definition: dict.c:30
AV_PKT_FLAG_DISPOSABLE
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder.
Definition: packet.h:429
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
MOVAtom
Definition: isom.h:82
mov_read_moov
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1170
FFNABS
#define FFNABS(a)
Negative Absolute value.
Definition: common.h:81
AV_PKT_DATA_SPHERICAL
@ AV_PKT_DATA_SPHERICAL
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition: packet.h:228
mov_read_esds
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:787
MOVStreamContext::sample_count
unsigned int sample_count
Definition: isom.h:184
MOVTrackExt::flags
unsigned flags
Definition: isom.h:106
AV_SPHERICAL_EQUIRECTANGULAR
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:56
intfloat.h
id3v1.h
AV_CODEC_ID_R10K
@ AV_CODEC_ID_R10K
Definition: codec_id.h:194
av_encryption_init_info_get_side_data
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
Definition: encryption_info.c:229
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
mov_read_stco
static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2023
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
MOVStreamContext::aes_ctr
struct AVAESCTR * aes_ctr
Definition: isom.h:239
cenc_filter
static int cenc_filter(MOVContext *mov, AVStream *st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
Definition: mov.c:6725
mov_read_sdtp
static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3012
AVStreamInternal::skip_samples
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
Definition: internal.h:258
mov_read_jp2h
static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1752
AVIndexEntry
Definition: avformat.h:803
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
MOVStreamContext::dv_audio_container
int dv_audio_container
Definition: isom.h:198
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:407
mov_read_tfhd
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4655
AV_CODEC_ID_BIN_DATA
@ AV_CODEC_ID_BIN_DATA
Definition: codec_id.h:564
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
OPUS_SEEK_PREROLL_MS
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:35
AV_CODEC_ID_H261
@ AV_CODEC_ID_H261
Definition: codec_id.h:52
mov_read_wide
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5208
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:811
MOVStreamContext::stsd_count
int stsd_count
Definition: isom.h:224
ff_mov_lang_to_iso639
int ff_mov_lang_to_iso639(unsigned code, char to[4])
Definition: isom.c:260
ff_mov_demuxer
AVInputFormat ff_mov_demuxer
Definition: mov.c:8306
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
AVEncryptionInfo::scheme
uint32_t scheme
The fourcc encryption scheme, in big-endian byte order.
Definition: encryption_info.h:45
MOVStreamContext::stsc_count
unsigned int stsc_count
Definition: isom.h:172
MOVStreamContext::has_palette
int has_palette
Definition: isom.h:209
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
AV_STEREO3D_SIDEBYSIDE
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:67
MOVIndexRange::start
int64_t start
Definition: isom.h:154
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
OFFSET
#define OFFSET(x)
Definition: mov.c:8248
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
MOVStreamContext::nb_frames_for_fps
int nb_frames_for_fps
Definition: isom.h:217
avpriv_dv_produce_packet
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:371
AV_FIELD_TT
@ AV_FIELD_TT
Definition: codec_par.h:39
AVEncryptionInfo::skip_byte_block
uint32_t skip_byte_block
Only used for pattern encryption.
Definition: encryption_info.h:57
finish
static void finish(void)
Definition: movenc.c:342
aax_filter
static int aax_filter(uint8_t *input, int size, MOVContext *c)
Definition: mov.c:1114
AV_OPT_TYPE_BINARY
@ AV_OPT_TYPE_BINARY
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:231
av_color_space_name
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:2961
av_packet_add_side_data
FF_ENABLE_DEPRECATION_WARNINGS int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:309
MOVStreamContext::mastering
AVMasteringDisplayMetadata * mastering
Definition: isom.h:231
AV_CODEC_ID_SPEEX
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:459
AV_FOURCC_MAX_STRING_SIZE
#define AV_FOURCC_MAX_STRING_SIZE
Definition: avutil.h:346
MOVFragmentIndexItem::current
int current
Definition: isom.h:140
AVFMT_SEEK_TO_PTS
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition: avformat.h:484
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:314
mov_read_mdhd
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1450
U
#define U(x)
Definition: vp56_arith.h:37
mov_read_ctts
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3057
MOVTrackExt
Definition: isom.h:101
MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:330
fail
#define fail()
Definition: checkasm.h:133
mov_read_aclr
static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1825
AVSEEK_FLAG_ANY
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2417
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
MOVFragment::found_tfhd
int found_tfhd
Definition: isom.h:90
MOVContext::decryption_key
uint8_t * decryption_key
Definition: isom.h:294
AV_STEREO3D_2D
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
timecode.h
get_current_frag_stream_info
static MOVFragmentStreamInfo * get_current_frag_stream_info(MOVFragmentIndex *frag_index)
Definition: mov.c:1227
MOVStreamContext::ctts_index
int ctts_index
Definition: isom.h:180
GetBitContext
Definition: get_bits.h:61
av_timecode_make_string
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:102
av_aes_ctr_alloc
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:36
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
mov_read_enda
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1550
mov_read_chap
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4701
MOVParseTableEntry
Definition: mov.c:72
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
MOV_TRUN_SAMPLE_DURATION
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:324
val
static double val(void *priv, double ch)
Definition: aeval.c:76
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
MOVContext
Definition: isom.h:246
AV_DISPOSITION_TIMED_THUMBNAILS
#define AV_DISPOSITION_TIMED_THUMBNAILS
The stream is sparse, and contains thumbnail images, often corresponding to chapter markers.
Definition: avformat.h:846
pts
static int64_t pts
Definition: transcode_aac.c:652
AVEncryptionInfo::iv
uint8_t * iv
The initialization vector.
Definition: encryption_info.h:71
AV_PKT_DATA_DISPLAYMATRIX
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: packet.h:108
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:425
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
MOVStreamContext::width
int width
tkhd width
Definition: isom.h:205
MOVContext::meta_keys
char ** meta_keys
Definition: isom.h:255
AV_FIELD_TB
@ AV_FIELD_TB
Definition: codec_par.h:41
MOVStreamContext::extradata_size
int * extradata_size
Definition: isom.h:222
av_reduce
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
update_frag_index
static int update_frag_index(MOVContext *c, int64_t offset)
Definition: mov.c:1336
AVRational::num
int num
Numerator.
Definition: rational.h:59
MOVStreamContext::keyframes
int * keyframes
Definition: isom.h:188
MOVEncryptionIndex::auxiliary_info_sample_count
size_t auxiliary_info_sample_count
Definition: isom.h:121
AVStream::attached_pic
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:955
MOVStsc::id
int id
Definition: isom.h:64
mov_read_saiz
static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6256
av_dovi_alloc
AVDOVIDecoderConfigurationRecord * av_dovi_alloc(size_t *size)
Allocate a AVDOVIDecoderConfigurationRecord structure and initialize its fields to default values.
Definition: dovi_meta.c:24
MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:337
MOV_TRUN_DATA_OFFSET
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:322
av_encryption_info_clone
AVEncryptionInfo * av_encryption_info_clone(const AVEncryptionInfo *info)
Allocates an AVEncryptionInfo structure with a copy of the given data.
Definition: encryption_info.c:63
AV_DICT_DONT_STRDUP_VAL
#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:74
rotate
static void rotate(const float rot_quaternion[2][4], float *vec)
Rotate vector with given rotation quaternion.
Definition: vf_v360.c:3919
av_bswap32
#define av_bswap32
Definition: bswap.h:33
MOVStreamContext::elst_data
MOVElst * elst_data
Definition: isom.h:178
mov_read_ares
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1784
mov_read_adrm
static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:990
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
MOVFragmentIndex::complete
int complete
Definition: isom.h:147
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:317
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:781
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
MOVStreamContext::stsc_sample
int stsc_sample
Definition: isom.h:175
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AV_CODEC_ID_MACE3
@ AV_CODEC_ID_MACE3
Definition: codec_id.h:433
AVInputFormat
Definition: avformat.h:640
MOVTrackExt::track_id
unsigned track_id
Definition: isom.h:102
mov_free_encryption_index
static void mov_free_encryption_index(MOVEncryptionIndex **index)
Definition: mov.c:7408
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
duration
int64_t duration
Definition: movenc.c:64
MOVEncryptionIndex::auxiliary_offsets
uint64_t * auxiliary_offsets
Absolute seek position.
Definition: isom.h:123
MOVStreamContext::dts_shift
int dts_shift
dts shift when ctts is negative
Definition: isom.h:207
AV_PKT_DATA_AUDIO_SERVICE_TYPE
@ AV_PKT_DATA_AUDIO_SERVICE_TYPE
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: packet.h:120
av_timecode_init
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:219
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:181
avio_get_str16le
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.
mov_metadata_track_or_disc_number
static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:82
av_dict_get
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:40
MOVStreamContext::stsd_version
int stsd_version
Definition: isom.h:225
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:478
FF_MOV_FLAG_MFRA_PTS
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:373
width
#define width
ff_mov_read_esds
int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
Definition: mov_esds.c:23
stereo3d.h
AVMasteringDisplayMetadata::white_point
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
Definition: mastering_display_metadata.h:47
intreadwrite.h
mov_read_coll
static int mov_read_coll(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5518
s
#define s(width, name)
Definition: cbs_vp9.c:257
MOVFragmentStreamInfo::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:134
mov_read_trak
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4249
MOVContext::fc
AVFormatContext * fc
Definition: isom.h:248
MOV_TFHD_DEFAULT_BASE_IS_MOOF
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:320
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AV_PKT_DATA_STEREO3D
@ AV_PKT_DATA_STEREO3D
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: packet.h:114
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:127
MOV_TFHD_DEFAULT_DURATION
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:316
ALAC_EXTRADATA_SIZE
#define ALAC_EXTRADATA_SIZE
DRM_BLOB_SIZE
#define DRM_BLOB_SIZE
Definition: mov.c:988
MOVStreamContext::drefs
MOVDref * drefs
Definition: isom.h:202
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:198
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:222
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
buffer_size_t
int buffer_size_t
Definition: internal.h:306
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
MOVContext::aes_decrypt
struct AVAES * aes_decrypt
Definition: isom.h:293
g
const char * g
Definition: vf_curves.c:117
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVSphericalMapping::bound_top
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:168
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:217
MOVFragmentIndex::nb_items
int nb_items
Definition: isom.h:149
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:424
MOV_TRUN_FIRST_SAMPLE_FLAGS
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:323
AVIndexEntry::size
int size
Definition: avformat.h:814
MOVStreamContext::keyframe_absent
int keyframe_absent
Definition: isom.h:186
info
MIPS optimizations info
Definition: mips.txt:2
MOVStreamContext::coll_size
size_t coll_size
Definition: isom.h:233
av_mastering_display_metadata_alloc
AVMasteringDisplayMetadata * av_mastering_display_metadata_alloc(void)
Copyright (c) 2016 Neil Birkbeck neil.birkbeck@gmail.com
Definition: mastering_display_metadata.c:27
mov_estimate_video_delay
static void mov_estimate_video_delay(MOVContext *c, AVStream *st)
Definition: mov.c:3394
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:805
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
av_aes_ctr_crypt
void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
Process a buffer using a previously initialized context.
Definition: aes_ctr.c:111
MOVStreamContext::min_corrected_pts
int64_t min_corrected_pts
minimum Composition time shown by the edits excluding empty edits.
Definition: isom.h:191
AV_PKT_DATA_ICC_PROFILE
@ AV_PKT_DATA_ICC_PROFILE
ICC profile data consisting of an opaque octet buffer following the format described by ISO 15076-1.
Definition: packet.h:274
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:37
MOVStreamContext::sdtp_count
unsigned int sdtp_count
Definition: isom.h:167
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
AVStream::need_parsing
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:54
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
limits.h
mov_read_sidx
static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5071
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVAudioServiceType
AVAudioServiceType
Definition: avcodec.h:239
mov_find_next_sample
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:7847
AV_CODEC_ID_TARGA_Y216
@ AV_CODEC_ID_TARGA_Y216
Definition: codec_id.h:255
AVIndexEntry::min_distance
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:815
mov_read_dvcc_dvvc
static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6870
MOVParseTableEntry::parse
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:74
mov_try_read_block
static int mov_try_read_block(AVIOContext *pb, size_t size, uint8_t **data)
Tries to read the given number of bytes from the stream and puts it in a newly allocated buffer.
Definition: mov.c:6229
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:72
key
const char * key
Definition: hwcontext_opencl.c:168
search_frag_timestamp
static int search_frag_timestamp(MOVFragmentIndex *frag_index, AVStream *st, int64_t timestamp)
Definition: mov.c:1302
MOVStreamContext::sdtp_data
uint8_t * sdtp_data
Definition: isom.h:168
color_range
color_range
Definition: vf_selectivecolor.c:44
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
mov_read_udta_string
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:302
mov_read_stss
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2796
mov_read_ddts
static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:868
mov_read_uuid
static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5831
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:461
AV_FIELD_BT
@ AV_FIELD_BT
Definition: codec_par.h:42
MOVTrackExt::duration
unsigned duration
Definition: isom.h:104
f
#define f(width, name)
Definition: cbs_vp9.c:255
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
AVDOVIDecoderConfigurationRecord::dv_version_major
uint8_t dv_version_major
Definition: dovi_meta.h:52
av_content_light_metadata_alloc
AVContentLightMetadata * av_content_light_metadata_alloc(size_t *size)
Allocate an AVContentLightMetadata structure and set its fields to default values.
Definition: mastering_display_metadata.c:45
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:48
av_sha_final
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:345
MOVStreamContext::current_sample
int current_sample
Definition: isom.h:192
MOVFragmentStreamInfo::sidx_pts
int64_t sidx_pts
Definition: isom.h:129
MAX_REORDER_DELAY
#define MAX_REORDER_DELAY
Definition: mov.c:3393
MOVFragmentIndex::current
int current
Definition: isom.h:148
int32_t
int32_t
Definition: audio_convert.c:194
MOVEncryptionIndex::encrypted_samples
AVEncryptionInfo ** encrypted_samples
Definition: isom.h:118
mov_read_close
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:7420
MOVAtom::size
int64_t size
Definition: isom.h:84
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
ff_mov_get_lpcm_codec_id
static enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
Compute codec id for 'lpcm' tag.
Definition: isom.h:379
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:110
AV_CODEC_ID_AVUI
@ AV_CODEC_ID_AVUI
Definition: codec_id.h:253
time_internal.h
if
if(ret)
Definition: filter_design.txt:179
mov_read_cmov
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5228
mov_read_sample_encryption_info
static int mov_read_sample_encryption_info(MOVContext *c, AVIOContext *pb, MOVStreamContext *sc, AVEncryptionInfo **sample, int use_subsamples)
Definition: mov.c:6055
MOVStreamContext::keyframe_count
unsigned int keyframe_count
Definition: isom.h:187
MOVStreamContext::ctts_data
MOVStts * ctts_data
Definition: isom.h:171
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:33
mov_read_stts
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2937
MOVStreamContext::index_ranges
MOVIndexRange * index_ranges
Definition: isom.h:194
DDTS_SIZE
#define DDTS_SIZE
internal.h
MOVTrackExt::stsd_id
unsigned stsd_id
Definition: isom.h:103
set_frag_stream
static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
Definition: mov.c:1207
mov_read_free
static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5936
mov_realloc_extradata
static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
Definition: mov.c:1679
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:352
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
aes.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
avpriv_dv_get_packet
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:354
MOVContext::ignore_editlist
int ignore_editlist
Definition: isom.h:268
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
NULL
#define NULL
Definition: coverity.c:32
sha.h
truehd_layout
static uint64_t truehd_layout(int chanmap)
Definition: mlp_parse.h:98
MOVDref
Definition: isom.h:73
AVDOVIDecoderConfigurationRecord::dv_level
uint8_t dv_level
Definition: dovi_meta.h:55
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVDOVIDecoderConfigurationRecord::dv_bl_signal_compatibility_id
uint8_t dv_bl_signal_compatibility_id
Definition: dovi_meta.h:59
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
MOVStreamContext::ctts_count
unsigned int ctts_count
Definition: isom.h:169
AVEncryptionInitInfo
This describes info used to initialize an encryption key system.
Definition: encryption_info.h:88
isom.h
mov_read_ftyp
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1128
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
MOVElst
Definition: isom.h:67
flac_parse_block_header
static av_always_inline void flac_parse_block_header(const uint8_t *block_header, int *last, int *type, int *size)
Parse the metadata block parameters from the header.
Definition: flac.h:145
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
isnan
#define isnan(x)
Definition: libm.h:340
mov_probe
static int mov_probe(const AVProbeData *p)
Definition: mov.c:7151
av_stream_add_side_data
int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as stream side data.
Definition: utils.c:5522
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
MOVDref::nlvl_to
int16_t nlvl_to
Definition: isom.h:79
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:523
AVIndexEntry::flags
int flags
Definition: avformat.h:813
MOVStreamContext::time_offset
int64_t time_offset
time offset of the edit list entries
Definition: isom.h:190
mov_read_smdm
static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5434
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
avio_rb64
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:902
av_aes_crypt
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:163
src
#define src
Definition: vp8dsp.c:255
MOVStreamContext::current_index_range
MOVIndexRange * current_index_range
Definition: isom.h:195
mov_open_dref
static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
Definition: mov.c:4167
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
av_aes_alloc
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:31
TAG_IS_AVCI
#define TAG_IS_AVCI(tag)
Definition: isom.h:349
MOVStreamContext::timecode_track
int timecode_track
Definition: isom.h:204
mov_read_schm
static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6532
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:937
FLAC_STREAMINFO_SIZE
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
av_color_primaries_name
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition: pixdesc.c:2919
FF_MOV_FLAG_MFRA_DTS
#define FF_MOV_FLAG_MFRA_DTS
Definition: isom.h:372
MOV_SAMPLE_DEPENDENCY_NO
#define MOV_SAMPLE_DEPENDENCY_NO
Definition: isom.h:346
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:76
ff_codec_movvideo_tags
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom_tags.c:29
MOVStreamContext::rap_group
MOVSbgp * rap_group
Definition: isom.h:215
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:443
mov_read_ilst
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4378
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:51
mov_read_fiel
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1645
convert_header.major
int major
Definition: convert_header.py:23
av_encryption_info_add_side_data
uint8_t * av_encryption_info_add_side_data(const AVEncryptionInfo *info, size_t *size)
Allocates and initializes side data that holds a copy of the given encryption info.
Definition: encryption_info.c:125
MOV_TFHD_BASE_DATA_OFFSET
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:314
ff_codec_movaudio_tags
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom_tags.c:278
ff_codec_movdata_tags
const AVCodecTag ff_codec_movdata_tags[]
Definition: isom.c:82
mov_read_wfex
static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:941
av_sha_update
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:315
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
MOVSbgp::count
unsigned int count
Definition: isom.h:110
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:226
mov_parse_stsd_subtitle
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:2280
mov_skip_multiple_stsd
static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, int codec_tag, int format, int64_t size)
Definition: mov.c:2474
MOVStts
Definition: isom.h:56
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:50
AV_CODEC_ID_GSM
@ AV_CODEC_ID_GSM
as in Berlin toast format
Definition: codec_id.h:442
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:924
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:464
should_retry
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:7873
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: avcodec.h:248
mov_read_pasp
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:956
MOVContext::dv_demux
DVDemuxContext * dv_demux
Definition: isom.h:257
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:29
av_aes_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:84
mov_read_elst
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5282
MOVEncryptionIndex::auxiliary_info_default_size
uint8_t auxiliary_info_default_size
Definition: isom.h:122
cid
int cid
Definition: mxfenc.c:2039
av_sat_sub64
#define av_sat_sub64
Definition: common.h:167
mov_read_header
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:7637
AV_CODEC_ID_QCELP
@ AV_CODEC_ID_QCELP
Definition: codec_id.h:448
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:630
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: avcodec.h:235
MOVFragment::flags
unsigned flags
Definition: isom.h:98
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:325
mov_read_wave
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1873
ffio_init_context
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:88
AV_SPHERICAL_CUBEMAP
@ AV_SPHERICAL_CUBEMAP
Video frame is split into 6 faces of a cube, and arranged on a 3x2 layout.
Definition: spherical.h:65
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:774
ff_mpa_check_header
static int ff_mpa_check_header(uint32_t header)
Definition: mpegaudiodecheader.h:61
MOVContext::handbrake_version
int handbrake_version
Definition: isom.h:264
AVPacket::size
int size
Definition: packet.h:370
MOVStreamContext::ctts_sample
int ctts_sample
Definition: isom.h:181
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:325
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3131
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
MOVFragmentIndexItem
Definition: isom.h:137
get_current_encryption_info
static int get_current_encryption_info(MOVContext *c, MOVEncryptionIndex **encryption_index, MOVStreamContext **sc)
Gets the current encryption info and associated current stream context.
Definition: mov.c:6005
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
AVStream::nb_index_entries
int nb_index_entries
Definition: avformat.h:1092
qtpalette.h
mov_read_dref
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:577
mov_current_sample_dec
static void mov_current_sample_dec(MOVStreamContext *sc)
Definition: mov.c:3462
AVSphericalMapping::bound_right
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:169
MOVStsc::first
int first
Definition: isom.h:62
MOVStreamContext::stsz_sample_size
unsigned int stsz_sample_size
always contains sample size from stsz atom
Definition: isom.h:183
FF_MOV_FLAG_MFRA_AUTO
#define FF_MOV_FLAG_MFRA_AUTO
Definition: isom.h:371
FF_COMPLIANCE_STRICT
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: avcodec.h:1603
start_time
static int64_t start_time
Definition: ffplay.c:332
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
avpriv_set_pts_info
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:4945
mov_read_trun
static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4786
avio_get_str
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:860
sample
#define sample
Definition: flacdsp_template.c:44
hypot
static av_const double hypot(double x, double y)
Definition: libm.h:366
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:53
MOV_TFHD_STSD_ID
#define MOV_TFHD_STSD_ID
Definition: isom.h:315
size
int size
Definition: twinvq_data.h:10344
mov_read_chan
static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:922
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
mov_read_stsc
static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2664
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
ff_get_qtpalette
int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
Retrieve the palette (or "color table" in QuickTime terms), either from the video sample description,...
Definition: qtpalette.c:323
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:474
av_fourcc_make_string
char * av_fourcc_make_string(char *buf, uint32_t fourcc)
Fill the provided buffer with a string containing a FourCC (four-character code) representation.
Definition: utils.c:116
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AES_CTR_KEY_SIZE
#define AES_CTR_KEY_SIZE
Definition: aes_ctr.h:30
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: common.h:479
MOVStreamContext::coll
AVContentLightMetadata * coll
Definition: isom.h:232
aes_ctr.h
add_index_entry
static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add index entry with the given values, to the end of st->index_entries.
Definition: mov.c:3311
MOVDref::path
char * path
Definition: isom.h:75
mov_current_sample_inc
static void mov_current_sample_inc(MOVStreamContext *sc)
Definition: mov.c:3450
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
get_frag_time
static int64_t get_frag_time(MOVFragmentIndex *frag_index, int index, int track_id)
Definition: mov.c:1277
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
fix_timescale
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:4239
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
height
#define height
AVSphericalMapping::padding
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:182
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:206
mov_read_default
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7027
AV_TIMECODE_FLAG_24HOURSMAX
@ AV_TIMECODE_FLAG_24HOURSMAX
timecode wraps after 24 hours
Definition: timecode.h:37
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:998
mov_read_packet
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:7960
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
AV_PKT_DATA_CONTENT_LIGHT_LEVEL
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: packet.h:235
MOVEncryptionIndex::auxiliary_offsets_count
size_t auxiliary_offsets_count
Definition: isom.h:124
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
av_encryption_info_free
void av_encryption_info_free(AVEncryptionInfo *info)
Frees the given encryption info object.
Definition: encryption_info.c:80
AVSubsampleEncryptionInfo
This file is part of FFmpeg.
Definition: encryption_info.h:25
MOVFragmentIndexItem::stream_info
MOVFragmentStreamInfo * stream_info
Definition: isom.h:142
version
version
Definition: libkvazaar.c:326
AVEncryptionInitInfo::next
struct AVEncryptionInitInfo * next
An optional pointer to the next initialization info in the list.
Definition: encryption_info.h:122
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
mov_read_clli
static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5555
MOVStreamContext::chunk_offsets
int64_t * chunk_offsets
Definition: isom.h:164
MOVFragmentIndex::item
MOVFragmentIndexItem * item
Definition: isom.h:150
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
av_encryption_init_info_alloc
AVEncryptionInitInfo * av_encryption_init_info_alloc(uint32_t system_id_size, uint32_t num_key_ids, uint32_t key_id_size, uint32_t data_size)
Allocates an AVEncryptionInitInfo structure and sub-pointers to hold the given sizes.
Definition: encryption_info.c:176
MOVContext::decryption_key_len
int decryption_key_len
Definition: isom.h:295
mov_read_dfla
static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6634
AVSHA::count
uint64_t count
number of bytes in buffer
Definition: sha.c:36
mov_default_parse_table
static const MOVParseTableEntry mov_default_parse_table[]
Definition: mov.c:6927
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
MOVDref::nlvl_from
int16_t nlvl_from
Definition: isom.h:79
flag
#define flag(name)
Definition: cbs_av1.c:553
parse_timecode_in_framenum_format
static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, uint32_t value, int flags)
Definition: mov.c:7341
AV_FIELD_BB
@ AV_FIELD_BB
Definition: codec_par.h:40
convert_header.minor
int minor
Definition: convert_header.py:26
mov_metadata_hmmt
static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:281
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
MOVFragmentStreamInfo::next_trun_dts
int64_t next_trun_dts
Definition: isom.h:132
MOVStreamContext::stsc_index
unsigned int stsc_index
Definition: isom.h:174
av_sha_alloc
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
mov_read_tenc
static int mov_read_tenc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6563
mov_stsc_index_valid
static int mov_stsc_index_valid(unsigned int index, unsigned int count)
Definition: mov.c:2739
MOVIndexRange
Definition: isom.h:153
mov_read_seek
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:8195
MOVContext::advanced_editlist
int advanced_editlist
Definition: isom.h:269
MOVStreamContext::time_scale
int time_scale
Definition: isom.h:189
mlp_parse.h
mac_to_unicode
static const uint32_t mac_to_unicode[128]
Definition: mov.c:141
MOVStreamContext::bytes_per_frame
unsigned int bytes_per_frame
Definition: isom.h:196
AVSphericalMapping::roll
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:128
i
int i
Definition: input.c:407
MOVIndexRange::end
int64_t end
Definition: isom.h:155
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
avio_internal.h
mov_read_trex
static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4722
FLAGS
#define FLAGS
Definition: mov.c:8249
AV_CODEC_ID_AVS
@ AV_CODEC_ID_AVS
Definition: codec_id.h:131
MOVStreamContext::stereo3d
AVStereo3D * stereo3d
Definition: isom.h:228
mov_fix_index
static void mov_fix_index(MOVContext *mov, AVStream *st)
Fix st->index_entries, so that it contains only the entries (and the entries which are needed to deco...
Definition: mov.c:3502
mov_read_pssh
static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6423
MOVDref::volume
char volume[28]
Definition: isom.h:77
mov_read_stsd
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2599
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
mov_rewrite_dvd_sub_extradata
static int mov_rewrite_dvd_sub_extradata(AVStream *st)
Definition: mov.c:2310
MOVStts::duration
int duration
Definition: isom.h:58
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
MOVStreamContext::stps_count
unsigned int stps_count
Definition: isom.h:176
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
display.h
ff_mov_read_stsd_entries
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:2502
avpriv_ac3_channel_layout_tab
const uint16_t avpriv_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3tab.c:89
AV_STEREO3D_TOPBOTTOM
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition: stereo3d.h:79
MOVFragment::duration
unsigned duration
Definition: isom.h:96
ff_id3v1_genre_str
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:27
MOVContext::frag_index
MOVFragmentIndex frag_index
Definition: isom.h:280
mov_read_vpcc
static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5390
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:335
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
av_url_split
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:4799
MOVStreamContext::dref_id
int dref_id
Definition: isom.h:203
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
fix_frag_index_entries
static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index, int id, int entries)
Definition: mov.c:1394
mov_finalize_stsd_codec
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2392
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:49
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:322
uint8_t
uint8_t
Definition: audio_convert.c:194
mov_read_mdcv
static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5479
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
AV_TIMECODE_FLAG_ALLOWNEGATIVE
@ AV_TIMECODE_FLAG_ALLOWNEGATIVE
negative time values are allowed
Definition: timecode.h:38
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
mov_read_mdat
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:980
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:119
MOVStreamContext::pb
AVIOContext * pb
Definition: isom.h:159
mov_read_keys
static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4387
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:59
len
int len
Definition: vorbis_enc_data.h:452
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:137
MOVFragment::size
unsigned size
Definition: isom.h:97
MOV_TFHD_DEFAULT_SIZE
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:317
ff_get_wav_header
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:515
avpriv_new_chapter
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4641
av_rescale
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:129
mov_build_index
static void mov_build_index(MOVContext *mov, AVStream *st)
Definition: mov.c:3806
mov_read_svq3
static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1868
AVSHA
hash context
Definition: sha.c:34
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
MOVFragmentStreamInfo::tfdt_dts
int64_t tfdt_dts
Definition: isom.h:131
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
MOVStreamContext::sample_sizes
int * sample_sizes
Definition: isom.h:185
av_get_packet
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:310
get_stream_info_time
static int64_t get_stream_info_time(MOVFragmentStreamInfo *frag_stream_info)
Definition: mov.c:1267
fix_index_entry_timestamps
static void fix_index_entry_timestamps(AVStream *st, int end_index, int64_t end_ts, int64_t *frame_duration_buffer, int frame_duration_buffer_size)
Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size,...
Definition: mov.c:3351
AV_WB8
#define AV_WB8(p, d)
Definition: intreadwrite.h:396
MOVStreamContext::chunk_count
unsigned int chunk_count
Definition: isom.h:163
MOVStreamContext::data_size
int64_t data_size
Definition: isom.h:210
AV_TIMECODE_FLAG_DROPFRAME
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition: timecode.h:36
language
Undefined Behavior In the C language
Definition: undefined.txt:3
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: utils.c:3408
mov_read_tmcd
static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5351
mov_chan.h
AVStream::disposition
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
tag
uint32_t tag
Definition: movenc.c:1611
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:880
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:873
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:188
AV_PKT_DATA_DOVI_CONF
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition: packet.h:283
MOVEncryptionIndex::nb_encrypted_samples
unsigned int nb_encrypted_samples
Definition: isom.h:117
av_stereo3d_alloc
AVStereo3D * av_stereo3d_alloc(void)
Allocate an AVStereo3D structure and set its fields to default values.
Definition: stereo3d.c:28
mov_read_senc
static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6106
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
AVClass::class_name
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
AVEncryptionInfo::key_id
uint8_t * key_id
The ID of the key used to encrypt the packet.
Definition: encryption_info.h:63
av_strlcat
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
MOVStreamContext::stts_data
MOVStts * stts_data
Definition: isom.h:166
AVSphericalMapping::pitch
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:127
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:766
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
MOVSbgp::index
unsigned int index
Definition: isom.h:111
MOVContext::chapter_tracks
int * chapter_tracks
Definition: isom.h:265
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:795
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
dovi_meta.h
av_stream_get_side_data
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size)
Get side information from stream.
MOVFragment::implicit_offset
uint64_t implicit_offset
Definition: isom.h:94
dict.h
mov_read_dmlp
static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6838
av_aes_ctr_set_full_iv
void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the "full" 16-byte iv, including the counter.
Definition: aes_ctr.c:48
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
MOVStreamContext::pseudo_stream_id
int pseudo_stream_id
-1 means demux all ids
Definition: isom.h:199
MOVContext::time_scale
int time_scale
Definition: isom.h:249
mov_read_tfdt
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4748
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
av_sat_add64
#define av_sat_add64
Definition: common.h:164
search_frag_moof_offset
static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
Definition: mov.c:1243
MOVFragment
Definition: isom.h:89
AV_DICT_MATCH_CASE
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:69
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
mov_switch_root
static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
Definition: mov.c:7880
MOVContext::use_mfra_for
int use_mfra_for
Definition: isom.h:278
AVEncryptionInfo
This describes encryption info for a packet.
Definition: encryption_info.h:43
MOVStreamContext::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:242
MIN_DATA_ENTRY_BOX_SIZE
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:576
avpriv_dv_init_demux
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:338
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:874
mov_seek_fragment
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
Definition: mov.c:8106
MOVStreamContext::cenc
struct MOVStreamContext::@261 cenc
mov_parse_stsd_video
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2111
ff_codec_bmp_tags
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:33
mov_read_dec3
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:828
MOVStreamContext::sample_size
unsigned int sample_size
may contain value calculated from stsd or value from stsz atom
Definition: isom.h:182
mlp_samplerate
static int mlp_samplerate(int in)
Definition: mlp_parse.h:80
channel_layout.h
MOVStreamContext::duration_for_fps
int64_t duration_for_fps
Definition: isom.h:218
mov_read_sbgp
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3123
MOVFragment::moof_offset
uint64_t moof_offset
Definition: isom.h:93
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
mov_read_glbl
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:1930
mov_change_extradata
static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
Definition: mov.c:7916
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
AVRational::den
int den
Denominator.
Definition: rational.h:60
mode
mode
Definition: ebur128.h:83
mov_parse_uuid_spherical
static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
Definition: mov.c:5768
MOVTrackExt::size
unsigned size
Definition: isom.h:105
AVDOVIDecoderConfigurationRecord::bl_present_flag
uint8_t bl_present_flag
Definition: dovi_meta.h:58
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
MOVContext::dv_fctx
AVFormatContext * dv_fctx
Definition: isom.h:258
AV_CODEC_ID_DVAUDIO
@ AV_CODEC_ID_DVAUDIO
Definition: codec_id.h:430
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4436
MOVContext::aax_mode
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:282
mov_read_sv3d
static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5631
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
mov_aaxc_crypto
static int mov_aaxc_crypto(MOVContext *c)
Definition: mov.c:1089
mov_get_skip_samples
static int64_t mov_get_skip_samples(AVStream *st, int sample)
Definition: mov.c:8179
MOVFragmentIndex
Definition: isom.h:145
AV_RB8
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL AV_RB8
Definition: bytestream.h:99
mov_read_av1c
static int mov_read_av1c(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5362
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1015
MOVStreamContext::track_end
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:212
MOVContext::fragment
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:260
AVDOVIDecoderConfigurationRecord::rpu_present_flag
uint8_t rpu_present_flag
Definition: dovi_meta.h:56
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:804
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
mov_metadata_int8_bypass_padding
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:102
AVDOVIDecoderConfigurationRecord::el_present_flag
uint8_t el_present_flag
Definition: dovi_meta.h:57
MOVDref::type
uint32_t type
Definition: isom.h:74
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
mean
static float mean(const float *input, int size)
Definition: vf_nnedi.c:864
mov_read_covr
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:182
MOVParseTableEntry::type
uint32_t type
Definition: mov.c:73
AVMasteringDisplayMetadata::min_luminance
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
Definition: mastering_display_metadata.h:52
MOVStreamContext::per_sample_iv_size
unsigned int per_sample_iv_size
Definition: isom.h:240
ff_codec_movsubtitle_tags
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:75
AVPacket::stream_index
int stream_index
Definition: packet.h:371
av_clip_uint8
#define av_clip_uint8
Definition: common.h:128
MOVFragmentIndexItem::nb_stream_info
int nb_stream_info
Definition: isom.h:141
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
export_orphan_timecode
static void export_orphan_timecode(AVFormatContext *s)
Definition: mov.c:7518
MOVStreamContext::has_sidx
int has_sidx
Definition: isom.h:237
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:50
mov_metadata_gnre
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:125
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
av_dict_set_int
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:147
mov_read_dpxe
static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1757
ff_format_io_close
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5692
MOVFragmentStreamInfo::id
int id
Definition: isom.h:128
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
tc
#define tc
Definition: regdef.h:69
av_spherical_alloc
AVSphericalMapping * av_spherical_alloc(size_t *size)
Allocate a AVSphericalVideo structure and initialize its fields to default values.
Definition: spherical.c:25
AVDOVIDecoderConfigurationRecord::dv_version_minor
uint8_t dv_version_minor
Definition: dovi_meta.h:53
mov_read_rtmd_track
static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:7355
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
MOVStreamContext::pb_is_copied
int pb_is_copied
Definition: isom.h:160
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:321
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
MOVElst::time
int64_t time
Definition: isom.h:69
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:84
mov_parse_stsd_audio
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2169
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
MOVContext::trak_index
int trak_index
Index of the current 'trak'.
Definition: isom.h:254
mov_read_timecode_track
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:7380
AVSphericalMapping::bound_left
uint32_t bound_left
Distance from the left edge.
Definition: spherical.h:167
mov_read_mac_string
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:160
MOVEncryptionIndex::auxiliary_info_sizes
uint8_t * auxiliary_info_sizes
Definition: isom.h:120
MOVFragment::stsd_id
unsigned stsd_id
Definition: isom.h:95
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:155
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:38
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
ff_generate_avci_extradata
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:5370
AV_CODEC_ID_PCM_F64LE
@ AV_CODEC_ID_PCM_F64LE
Definition: codec_id.h:336
read_tfra
static int read_tfra(MOVContext *mov, AVIOContext *f)
Definition: mov.c:7536
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVDictionaryEntry
Definition: dict.h:81
AVFieldOrder
AVFieldOrder
Definition: codec_par.h:36
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:346
AVContentLightMetadata::MaxFALL
unsigned MaxFALL
Max average light level per frame (cd/m^2).
Definition: mastering_display_metadata.h:107
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
cr
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:216
MOVStreamContext::stps_data
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:177
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:354
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
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:70
riff.h
av_encryption_info_alloc
AVEncryptionInfo * av_encryption_info_alloc(uint32_t subsample_count, uint32_t key_id_size, uint32_t iv_size)
Allocates an AVEncryptionInfo structure and sub-pointers to hold the given number of subsamples.
Definition: encryption_info.c:39
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
mov_metadata_loci
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:231
av_stream_new_side_data
uint8_t * av_stream_new_side_data(AVStream *stream, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:483
MOV_TRUN_SAMPLE_SIZE
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:325
MOVStreamContext::tmcd_flags
uint32_t tmcd_flags
tmcd track flags
Definition: isom.h:211
convert_header.str
string str
Definition: convert_header.py:20
MOVAtom::type
uint32_t type
Definition: isom.h:83
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:232
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:794
replaygain.h
MOVFragmentIndexItem::headers_read
int headers_read
Definition: isom.h:139
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:189
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
MOVStreamContext::start_pad
int start_pad
amount of samples to skip due to enc-dec delay
Definition: isom.h:213
AVStereo3DType
AVStereo3DType
List of possible 3D Types.
Definition: stereo3d.h:51
MOVDref::filename
char filename[64]
Definition: isom.h:78
MOVStsc::count
int count
Definition: isom.h:63
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:334
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
MOVStts::count
unsigned int count
Definition: isom.h:57
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
MOVStreamContext::display_matrix
int32_t * display_matrix
Definition: isom.h:227
MOVStreamContext::current_index
int64_t current_index
Definition: isom.h:193
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
find_prev_closest_index
static int find_prev_closest_index(AVStream *st, AVIndexEntry *e_old, int nb_old, MOVStts *ctts_data, int64_t ctts_count, int64_t timestamp_pts, int flag, int64_t *index, int64_t *ctts_index, int64_t *ctts_sample)
Find the closest previous frame to the timestamp_pts, in e_old index entries.
Definition: mov.c:3215
MOVFragmentStreamInfo::index_entry
int index_entry
Definition: isom.h:133
cenc_decrypt
static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:6671
MOVStreamContext::format
uint32_t format
Definition: isom.h:235
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:682
MOVContext::bitrates_count
int bitrates_count
Definition: isom.h:276
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:429
AVDictionaryEntry::value
char * value
Definition: dict.h:83
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
MOVStreamContext::samples_per_frame
unsigned int samples_per_frame
Definition: isom.h:197
MOVElst::duration
int64_t duration
Definition: isom.h:68
ac3tab.h
avstring.h
mov_read_mfra
static int mov_read_mfra(MOVContext *c, AVIOContext *f)
Definition: mov.c:7591
AV_CODEC_ID_FLV1
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:70
mov_metadata_int8_no_padding
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:116
flac.h
AVTimecode
Definition: timecode.h:41
get_frag_stream_info
static MOVFragmentStreamInfo * get_frag_stream_info(MOVFragmentIndex *frag_index, int index, int id)
Definition: mov.c:1188
int
int
Definition: ffmpeg_filter.c:170
MOVStreamContext::stts_count
unsigned int stts_count
Definition: isom.h:165
MOV_TRUN_SAMPLE_CTS
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:327
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
snprintf
#define snprintf
Definition: snprintf.h:34
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:189
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:326
color_primaries
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
Definition: vf_colorspace.c:211
get_eia608_packet
static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size)
Definition: mov.c:7939
mov_read_st3d
static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5584
mov_read_dvc1
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1971
MOVStreamContext::elst_count
unsigned int elst_count
Definition: isom.h:179
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:2940
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:258
FF_API_OLD_ROTATE_API
#define FF_API_OLD_ROTATE_API
Definition: version.h:80
mov_read_atom_into_extradata
static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, AVCodecParameters *par, uint8_t *buf)
Definition: mov.c:1694
AV_RB64
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:95
AVSphericalMapping::yaw
int32_t yaw
Rotation around the up vector [-180, 180].
Definition: spherical.h:126
mov_read_chapters
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:7242
AV_CODEC_ID_DNXHD
@ AV_CODEC_ID_DNXHD
Definition: codec_id.h:148
MOVStreamContext::default_encrypted_sample
AVEncryptionInfo * default_encrypted_sample
Definition: isom.h:241
AVStream::index_entries_allocated_size
unsigned int index_entries_allocated_size
Definition: avformat.h:1093
AVFMT_EVENT_FLAG_METADATA_UPDATED
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1562
AV_DICT_DONT_STRDUP_KEY
#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:72
MOVContext::next_root_atom
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:272
MOVContext::meta_keys_count
unsigned meta_keys_count
Definition: isom.h:256
MOVStreamContext::palette
uint32_t palette[256]
Definition: isom.h:208
ff_configure_buffers_for_index
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: utils.c:2065
MOVFragment::track_id
unsigned track_id
Definition: isom.h:91
mov_read_hdlr
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:721
mov_parse_stsd_data
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:2342
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:58
av_display_rotation_get
double av_display_rotation_get(const int32_t matrix[9])
Extract the rotation component of the transformation matrix.
Definition: display.c:34
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
av_encryption_init_info_add_side_data
uint8_t * av_encryption_init_info_add_side_data(const AVEncryptionInitInfo *info, size_t *side_data_size)
Allocates and initializes side data that holds a copy of the given encryption init info.
Definition: encryption_info.c:290
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
ff_mov_read_chan
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
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2130
MOVContext::ignore_chapters
int ignore_chapters
Definition: isom.h:270
mov_read_dac3
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:792
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3314
AVDOVIDecoderConfigurationRecord
Definition: dovi_meta.h:51
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
mc
#define mc
Definition: vf_colormatrix.c:102
MOVStreamContext::ffindex
int ffindex
AVStream index.
Definition: isom.h:161
mov_read_stsz
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2844