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 "config_components.h"
27 
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <stdint.h>
31 
32 #include "libavutil/attributes.h"
33 #include "libavutil/bprint.h"
35 #include "libavutil/dovi_meta.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/intfloat.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/avstring.h"
42 #include "libavutil/dict.h"
43 #include "libavutil/display.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/aes.h"
47 #include "libavutil/aes_ctr.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/sha.h"
50 #include "libavutil/spherical.h"
51 #include "libavutil/stereo3d.h"
52 #include "libavutil/timecode.h"
53 #include "libavutil/uuid.h"
54 #include "libavcodec/ac3tab.h"
55 #include "libavcodec/exif.h"
56 #include "libavcodec/flac.h"
57 #include "libavcodec/hevc/hevc.h"
59 #include "libavcodec/mlp_parse.h"
60 #include "avformat.h"
61 #include "internal.h"
62 #include "avio_internal.h"
63 #include "demux.h"
64 #include "dvdclut.h"
65 #include "iamf_parse.h"
66 #include "iamf_reader.h"
67 #include "dovi_isom.h"
68 #include "riff.h"
69 #include "isom.h"
70 #include "libavcodec/get_bits.h"
71 #include "id3v1.h"
72 #include "mov_chan.h"
73 #include "replaygain.h"
74 
75 #if CONFIG_ZLIB
76 #include <zlib.h>
77 #endif
78 
79 #include "qtpalette.h"
80 
81 /* those functions parse an atom */
82 /* links atom IDs to parse functions */
83 typedef struct MOVParseTableEntry {
84  uint32_t type;
85  int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
87 
88 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
89 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
91 
93  unsigned len, const char *key)
94 {
95  char buf[16];
96 
97  short current, total = 0;
98  avio_rb16(pb); // unknown
99  current = avio_rb16(pb);
100  if (len >= 6)
101  total = avio_rb16(pb);
102  if (!total)
103  snprintf(buf, sizeof(buf), "%d", current);
104  else
105  snprintf(buf, sizeof(buf), "%d/%d", current, total);
106  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
107  av_dict_set(&c->fc->metadata, key, buf, 0);
108 
109  return 0;
110 }
111 
113  unsigned len, const char *key)
114 {
115  /* bypass padding bytes */
116  avio_r8(pb);
117  avio_r8(pb);
118  avio_r8(pb);
119 
120  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
121  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
122 
123  return 0;
124 }
125 
127  unsigned len, const char *key)
128 {
129  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
130  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
131 
132  return 0;
133 }
134 
136  unsigned len, const char *key)
137 {
138  short genre;
139 
140  avio_r8(pb); // unknown
141 
142  genre = avio_r8(pb);
143  if (genre < 1 || genre > ID3v1_GENRE_MAX)
144  return 0;
145  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
146  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
147 
148  return 0;
149 }
150 
151 static const uint32_t mac_to_unicode[128] = {
152  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
153  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
154  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
155  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
156  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
157  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
158  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
159  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
160  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
161  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
162  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
163  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
164  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
165  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
166  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
167  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
168 };
169 
171  char *dst, int dstlen)
172 {
173  char *p = dst;
174  char *end = dst+dstlen-1;
175  int i;
176 
177  for (i = 0; i < len; i++) {
178  uint8_t t, c = avio_r8(pb);
179 
180  if (p >= end)
181  continue;
182 
183  if (c < 0x80)
184  *p++ = c;
185  else if (p < end)
186  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
187  }
188  *p = 0;
189  return p - dst;
190 }
191 
192 /**
193  * Get the requested item.
194  */
195 static HEIFItem *get_heif_item(MOVContext *c, unsigned id)
196 {
197  HEIFItem *item = NULL;
198 
199  for (int i = 0; i < c->nb_heif_item; i++) {
200  if (!c->heif_item[i] || c->heif_item[i]->item_id != id)
201  continue;
202 
203  item = c->heif_item[i];
204  break;
205  }
206 
207  return item;
208 }
209 
210 /**
211  * Get the current stream in the parsing process. This can either be the
212  * latest stream added to the context, or the stream referenced by an item.
213  */
215 {
216  AVStream *st = NULL;
217  HEIFItem *item;
218 
219  if (c->fc->nb_streams < 1)
220  return NULL;
221 
222  if (c->cur_item_id == -1)
223  return c->fc->streams[c->fc->nb_streams-1];
224 
225  item = get_heif_item(c, c->cur_item_id);
226  if (item)
227  st = item->st;
228 
229  return st;
230 }
231 
232 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
233 {
234  AVStream *st;
235  MOVStreamContext *sc;
236  enum AVCodecID id;
237  int ret;
238 
239  switch (type) {
240  case 0xd: id = AV_CODEC_ID_MJPEG; break;
241  case 0xe: id = AV_CODEC_ID_PNG; break;
242  case 0x1b: id = AV_CODEC_ID_BMP; break;
243  default:
244  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
245  avio_skip(pb, len);
246  return 0;
247  }
248 
249  sc = av_mallocz(sizeof(*sc));
250  if (!sc)
251  return AVERROR(ENOMEM);
252  ret = ff_add_attached_pic(c->fc, NULL, pb, NULL, len);
253  if (ret < 0) {
254  av_free(sc);
255  return ret;
256  }
257  st = c->fc->streams[c->fc->nb_streams - 1];
258  st->priv_data = sc;
259  sc->id = st->id;
260  sc->refcount = 1;
261 
262  if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
263  if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
264  id = AV_CODEC_ID_PNG;
265  } else {
266  id = AV_CODEC_ID_MJPEG;
267  }
268  }
269  st->codecpar->codec_id = id;
270 
271  return 0;
272 }
273 
274 // 3GPP TS 26.244
275 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
276 {
277  char language[4] = { 0 };
278  char buf[200], place[100];
279  uint16_t langcode = 0;
280  double longitude, latitude, altitude;
281  const char *key = "location";
282 
283  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
284  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
285  return AVERROR_INVALIDDATA;
286  }
287 
288  avio_skip(pb, 4); // version+flags
289  langcode = avio_rb16(pb);
290  ff_mov_lang_to_iso639(langcode, language);
291  len -= 6;
292 
293  len -= avio_get_str(pb, len, place, sizeof(place));
294  if (len < 1) {
295  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
296  return AVERROR_INVALIDDATA;
297  }
298  avio_skip(pb, 1); // role
299  len -= 1;
300 
301  if (len < 12) {
302  av_log(c->fc, AV_LOG_ERROR,
303  "loci too short (%u bytes left, need at least %d)\n", len, 12);
304  return AVERROR_INVALIDDATA;
305  }
306  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
307  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
308  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
309 
310  // Try to output in the same format as the ?xyz field
311  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
312  if (altitude)
313  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
314  av_strlcatf(buf, sizeof(buf), "/%s", place);
315 
316  if (*language && strcmp(language, "und")) {
317  char key2[16];
318  snprintf(key2, sizeof(key2), "%s-%s", key, language);
319  av_dict_set(&c->fc->metadata, key2, buf, 0);
320  }
321  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
322  return av_dict_set(&c->fc->metadata, key, buf, 0);
323 }
324 
325 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
326 {
327  int i, n_hmmt;
328 
329  if (len < 2)
330  return 0;
331  if (c->ignore_chapters)
332  return 0;
333 
334  n_hmmt = avio_rb32(pb);
335  if (n_hmmt > len / 4)
336  return AVERROR_INVALIDDATA;
337  for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
338  int moment_time = avio_rb32(pb);
339  avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
340  }
341  if (avio_feof(pb))
342  return AVERROR_INVALIDDATA;
343  return 0;
344 }
345 
347 {
348  char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
349  char key2[32], language[4] = {0};
350  char *str = NULL;
351  const char *key = NULL;
352  uint16_t langcode = 0;
353  uint32_t data_type = 0, str_size_alloc;
354  uint64_t str_size;
355  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
356  int raw = 0;
357  int num = 0;
359 
360  if (c->trak_index >= 0 && c->trak_index < c->fc->nb_streams)
361  metadata = &c->fc->streams[c->trak_index]->metadata;
362  else
363  metadata = &c->fc->metadata;
364 
365  switch (atom.type) {
366  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
367  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
368  case MKTAG( 'X','M','P','_'):
369  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
370  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
371  case MKTAG( 'a','k','I','D'): key = "account_type";
373  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
374  case MKTAG( 'c','a','t','g'): key = "category"; break;
375  case MKTAG( 'c','p','i','l'): key = "compilation";
377  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
378  case MKTAG( 'd','e','s','c'): key = "description"; break;
379  case MKTAG( 'd','i','s','k'): key = "disc";
381  case MKTAG( 'e','g','i','d'): key = "episode_uid";
383  case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
384  case MKTAG( 'g','n','r','e'): key = "genre";
385  parse = mov_metadata_gnre; break;
386  case MKTAG( 'h','d','v','d'): key = "hd_video";
388  case MKTAG( 'H','M','M','T'):
389  return mov_metadata_hmmt(c, pb, atom.size);
390  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
391  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
392  case MKTAG( 'l','o','c','i'):
393  return mov_metadata_loci(c, pb, atom.size);
394  case MKTAG( 'm','a','n','u'): key = "make"; break;
395  case MKTAG( 'm','o','d','l'): key = "model"; break;
396  case MKTAG( 'n','a','m','e'): key = "name"; break;
397  case MKTAG( 'p','c','s','t'): key = "podcast";
399  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
401  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
402  case MKTAG( 'r','t','n','g'): key = "rating";
404  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
405  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
406  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
407  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
408  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
409  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
410  case MKTAG( 's','t','i','k'): key = "media_type";
412  case MKTAG( 't','r','k','n'): key = "track";
414  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
415  case MKTAG( 't','v','e','s'): key = "episode_sort";
417  case MKTAG( 't','v','n','n'): key = "network"; break;
418  case MKTAG( 't','v','s','h'): key = "show"; break;
419  case MKTAG( 't','v','s','n'): key = "season_number";
421  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
422  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
423  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
424  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
425  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
426  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
427  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
428  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
429  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
430  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
431  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
432  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
433  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
434  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
435  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
436  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
437  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
438  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
439  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
440  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
441  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
442  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
443  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
444  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
445  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
446  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
447  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
448  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
449  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
450  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
451  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
452  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
453  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
454  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
455  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
456  }
457 retry:
458  if (c->itunes_metadata && atom.size > 8) {
459  int data_size = avio_rb32(pb);
460  int tag = avio_rl32(pb);
461  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size && data_size >= 16) {
462  data_type = avio_rb32(pb); // type
463  avio_rb32(pb); // unknown
464  str_size = data_size - 16;
465  atom.size -= 16;
466 
467  if (!key && c->found_hdlr_mdta && c->meta_keys) {
468  uint32_t index = av_bswap32(atom.type); // BE number has been read as LE
469  if (index < c->meta_keys_count && index > 0) {
470  key = c->meta_keys[index];
471  } else if (atom.type != MKTAG('c', 'o', 'v', 'r')) {
472  av_log(c->fc, AV_LOG_WARNING,
473  "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
474  index, c->meta_keys_count);
475  }
476  }
477  if (atom.type == MKTAG('c', 'o', 'v', 'r') ||
478  (key && !strcmp(key, "com.apple.quicktime.artwork"))) {
479  int ret = mov_read_covr(c, pb, data_type, str_size);
480  if (ret < 0) {
481  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
482  return ret;
483  }
484  atom.size -= str_size;
485  if (atom.size > 8)
486  goto retry;
487  return ret;
488  }
489  } else return 0;
490  } else if (atom.size > 4 && (key || c->export_all) && !c->itunes_metadata && !raw) {
491  str_size = avio_rb16(pb); // string length
492  if (str_size > atom.size) {
493  raw = 1;
494  avio_seek(pb, -2, SEEK_CUR);
495  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
496  goto retry;
497  }
498  langcode = avio_rb16(pb);
499  ff_mov_lang_to_iso639(langcode, language);
500  atom.size -= 4;
501  } else
502  str_size = atom.size;
503 
504  if (c->export_all && !key) {
505  key = av_fourcc_make_string(tmp_key, atom.type);
506  }
507 
508  if (!key)
509  return 0;
510  if (atom.size < 0 || str_size >= INT_MAX/2)
511  return AVERROR_INVALIDDATA;
512 
513  // Allocates enough space if data_type is a int32 or float32 number, otherwise
514  // worst-case requirement for output string in case of utf8 coded input
515  num = (data_type >= 21 && data_type <= 23);
516  str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
517  str = av_mallocz(str_size_alloc);
518  if (!str)
519  return AVERROR(ENOMEM);
520 
521  if (parse)
522  parse(c, pb, str_size, key);
523  else {
524  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
525  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
526  } else if (data_type == 21) { // BE signed integer, variable size
527  int val = 0;
528  if (str_size == 1)
529  val = (int8_t)avio_r8(pb);
530  else if (str_size == 2)
531  val = (int16_t)avio_rb16(pb);
532  else if (str_size == 3)
533  val = ((int32_t)(avio_rb24(pb)<<8))>>8;
534  else if (str_size == 4)
535  val = (int32_t)avio_rb32(pb);
536  if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
537  av_log(c->fc, AV_LOG_ERROR,
538  "Failed to store the number (%d) in string.\n", val);
539  av_free(str);
540  return AVERROR_INVALIDDATA;
541  }
542  } else if (data_type == 22) { // BE unsigned integer, variable size
543  unsigned int val = 0;
544  if (str_size == 1)
545  val = avio_r8(pb);
546  else if (str_size == 2)
547  val = avio_rb16(pb);
548  else if (str_size == 3)
549  val = avio_rb24(pb);
550  else if (str_size == 4)
551  val = avio_rb32(pb);
552  if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
553  av_log(c->fc, AV_LOG_ERROR,
554  "Failed to store the number (%u) in string.\n", val);
555  av_free(str);
556  return AVERROR_INVALIDDATA;
557  }
558  } else if (data_type == 23 && str_size >= 4) { // BE float32
559  float val = av_int2float(avio_rb32(pb));
560  if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
561  av_log(c->fc, AV_LOG_ERROR,
562  "Failed to store the float32 number (%f) in string.\n", val);
563  av_free(str);
564  return AVERROR_INVALIDDATA;
565  }
566  } else if (data_type > 1 && data_type != 4) {
567  // data_type can be 0 if not set at all above. data_type 1 means
568  // UTF8 and 4 means "UTF8 sort". For any other type (UTF16 or e.g.
569  // a picture), don't return it blindly in a string that is supposed
570  // to be UTF8 text.
571  av_log(c->fc, AV_LOG_WARNING, "Skipping unhandled metadata %s of type %"PRIu32"\n", key, data_type);
572  av_free(str);
573  return 0;
574  } else {
575  int ret = ffio_read_size(pb, str, str_size);
576  if (ret < 0) {
577  av_free(str);
578  return ret;
579  }
580  str[str_size] = 0;
581  }
582  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
583  if (c->itunes_metadata && av_dict_get(*metadata, key, NULL, 0))
585  av_dict_set(metadata, key, str, c->itunes_metadata ? AV_DICT_APPEND : 0);
586  if (*language && strcmp(language, "und")) {
587  snprintf(key2, sizeof(key2), "%s-%s", key, language);
588  av_dict_set(metadata, key2, str, 0);
589  }
590  if (!strcmp(key, "encoder")) {
591  int major, minor, micro;
592  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
593  c->handbrake_version = 1000000*major + 1000*minor + micro;
594  }
595  }
596 
597  atom.size -= str_size;
598  av_freep(&str);
599 
600  // Read remaining data atoms for multi-valued iTunes tags (e.g. multiple
601  // artists stored as multiple data atoms within one tag atom), consistent
602  // with the existing covr path.
603  // Note: multiple sibling tag atoms with the same key (e.g. three separate
604  // ©ART atoms under ilst) are handled by the AV_DICT_APPEND logic above,
605  // since mov_read_udta_string() is called once per tag atom.
606  if (c->itunes_metadata && atom.size > 8)
607  goto retry;
608  }
609 
610  av_freep(&str);
611  return 0;
612 }
613 
615 {
616  int64_t start;
617  int i, nb_chapters, str_len, version;
618  char str[256+1];
619  int ret;
620 
621  if (c->ignore_chapters)
622  return 0;
623 
624  if ((atom.size -= 5) < 0)
625  return 0;
626 
627  version = avio_r8(pb);
628  avio_rb24(pb);
629  if (version)
630  avio_rb32(pb); // ???
631  nb_chapters = avio_r8(pb);
632 
633  for (i = 0; i < nb_chapters; i++) {
634  if (atom.size < 9)
635  return 0;
636 
637  start = avio_rb64(pb);
638  str_len = avio_r8(pb);
639 
640  if ((atom.size -= 9+str_len) < 0)
641  return 0;
642 
643  ret = ffio_read_size(pb, str, str_len);
644  if (ret < 0)
645  return ret;
646  str[str_len] = 0;
647  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
648  }
649  return 0;
650 }
651 
652 #define MIN_DATA_ENTRY_BOX_SIZE 12
654 {
655  AVStream *st;
656  MOVStreamContext *sc;
657  int entries, i, j;
658 
659  if (c->fc->nb_streams < 1)
660  return 0;
661  st = c->fc->streams[c->fc->nb_streams-1];
662  sc = st->priv_data;
663 
664  avio_rb32(pb); // version + flags
665  entries = avio_rb32(pb);
666  if (!entries ||
667  entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
668  entries >= UINT_MAX / sizeof(*sc->drefs))
669  return AVERROR_INVALIDDATA;
670 
671  for (i = 0; i < sc->drefs_count; i++) {
672  MOVDref *dref = &sc->drefs[i];
673  av_freep(&dref->path);
674  av_freep(&dref->dir);
675  }
676  av_free(sc->drefs);
677  sc->drefs_count = 0;
678  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
679  if (!sc->drefs)
680  return AVERROR(ENOMEM);
681  sc->drefs_count = entries;
682 
683  for (i = 0; i < entries; i++) {
684  MOVDref *dref = &sc->drefs[i];
685  uint32_t size = avio_rb32(pb);
686  int64_t next = avio_tell(pb);
687 
688  if (size < 12 || next < 0 || next > INT64_MAX - size)
689  return AVERROR_INVALIDDATA;
690 
691  next += size - 4;
692 
693  dref->type = avio_rl32(pb);
694  avio_rb32(pb); // version + flags
695 
696  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
697  /* macintosh alias record */
698  uint16_t volume_len, len;
699  int16_t type;
700  int ret;
701 
702  avio_skip(pb, 10);
703 
704  volume_len = avio_r8(pb);
705  volume_len = FFMIN(volume_len, 27);
706  ret = ffio_read_size(pb, dref->volume, 27);
707  if (ret < 0)
708  return ret;
709  dref->volume[volume_len] = 0;
710  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
711 
712  avio_skip(pb, 12);
713 
714  len = avio_r8(pb);
715  len = FFMIN(len, 63);
716  ret = ffio_read_size(pb, dref->filename, 63);
717  if (ret < 0)
718  return ret;
719  dref->filename[len] = 0;
720  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
721 
722  avio_skip(pb, 16);
723 
724  /* read next level up_from_alias/down_to_target */
725  dref->nlvl_from = avio_rb16(pb);
726  dref->nlvl_to = avio_rb16(pb);
727  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
728  dref->nlvl_from, dref->nlvl_to);
729 
730  avio_skip(pb, 16);
731 
732  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
733  if (avio_feof(pb))
734  return AVERROR_EOF;
735  type = avio_rb16(pb);
736  len = avio_rb16(pb);
737  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
738  if (len&1)
739  len += 1;
740  if (type == 2) { // absolute path
741  av_free(dref->path);
742  dref->path = av_mallocz(len+1);
743  if (!dref->path)
744  return AVERROR(ENOMEM);
745 
746  ret = ffio_read_size(pb, dref->path, len);
747  if (ret < 0) {
748  av_freep(&dref->path);
749  return ret;
750  }
751  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
752  len -= volume_len;
753  memmove(dref->path, dref->path+volume_len, len);
754  dref->path[len] = 0;
755  }
756  // trim string of any ending zeros
757  for (j = len - 1; j >= 0; j--) {
758  if (dref->path[j] == 0)
759  len--;
760  else
761  break;
762  }
763  for (j = 0; j < len; j++)
764  if (dref->path[j] == ':' || dref->path[j] == 0)
765  dref->path[j] = '/';
766  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
767  } else if (type == 0) { // directory name
768  av_free(dref->dir);
769  dref->dir = av_malloc(len+1);
770  if (!dref->dir)
771  return AVERROR(ENOMEM);
772 
773  ret = ffio_read_size(pb, dref->dir, len);
774  if (ret < 0) {
775  av_freep(&dref->dir);
776  return ret;
777  }
778  dref->dir[len] = 0;
779  for (j = 0; j < len; j++)
780  if (dref->dir[j] == ':')
781  dref->dir[j] = '/';
782  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
783  } else
784  avio_skip(pb, len);
785  }
786  } else {
787  av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
788  dref->type, size);
789  entries--;
790  i--;
791  }
792  avio_seek(pb, next, SEEK_SET);
793  }
794  return 0;
795 }
796 
798 {
799  AVStream *st;
800  uint32_t type;
801  uint32_t ctype;
802  int64_t title_size;
803  char *title_str;
804  int ret;
805 
806  avio_r8(pb); /* version */
807  avio_rb24(pb); /* flags */
808 
809  /* component type */
810  ctype = avio_rl32(pb);
811  type = avio_rl32(pb); /* component subtype */
812 
813  av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
814  av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
815 
816  if (c->trak_index < 0) { // meta not inside a trak
817  if (type == MKTAG('m','d','t','a')) {
818  c->found_hdlr_mdta = 1;
819  }
820  return 0;
821  }
822 
823  st = c->fc->streams[c->fc->nb_streams-1];
824 
825  if (type == MKTAG('v','i','d','e'))
827  else if (type == MKTAG('s','o','u','n'))
829  else if (type == MKTAG('m','1','a',' '))
831  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
833 
834  avio_rb32(pb); /* component manufacture */
835  avio_rb32(pb); /* component flags */
836  avio_rb32(pb); /* component flags mask */
837 
838  title_size = atom.size - 24;
839  if (title_size > 0) {
840  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
841  return AVERROR_INVALIDDATA;
842  title_str = av_malloc(title_size + 1); /* Add null terminator */
843  if (!title_str)
844  return AVERROR(ENOMEM);
845 
846  ret = ffio_read_size(pb, title_str, title_size);
847  if (ret < 0) {
848  av_freep(&title_str);
849  return ret;
850  }
851  title_str[title_size] = 0;
852  if (title_str[0]) {
853  int off = (!c->isom && title_str[0] == title_size - 1);
854  // flag added so as to not set stream handler name if already set from mdia->hdlr
855  av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
856  }
857  av_freep(&title_str);
858  }
859 
860  return 0;
861 }
862 
864 {
865  return ff_mov_read_esds(c->fc, pb);
866 }
867 
869 {
870  AVStream *st;
871  AVPacketSideData *sd;
872  enum AVAudioServiceType *ast;
873  int ac3info, acmod, lfeon, bsmod;
874  uint64_t mask;
875 
876  if (c->fc->nb_streams < 1)
877  return 0;
878  st = c->fc->streams[c->fc->nb_streams-1];
879 
883  sizeof(*ast), 0);
884  if (!sd)
885  return AVERROR(ENOMEM);
886 
887  ast = (enum AVAudioServiceType*)sd->data;
888  ac3info = avio_rb24(pb);
889  bsmod = (ac3info >> 14) & 0x7;
890  acmod = (ac3info >> 11) & 0x7;
891  lfeon = (ac3info >> 10) & 0x1;
892 
894  if (lfeon)
898 
899  *ast = bsmod;
900  if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
902 
903  return 0;
904 }
905 
906 #if CONFIG_IAMFDEC
907 static int mov_read_iacb(MOVContext *c, AVIOContext *pb, MOVAtom atom)
908 {
909  AVStream *st;
910  MOVStreamContext *sc;
911  FFIOContext b;
912  AVIOContext *descriptor_pb;
914  IAMFContext *iamf;
916  unsigned descriptors_size;
917  int nb_frames, disposition;
918  int version, ret;
919 
920  if (atom.size < 5)
921  return AVERROR_INVALIDDATA;
922 
923  if (c->fc->nb_streams < 1)
924  return 0;
925 
926  version = avio_r8(pb);
927  if (version != 1) {
928  av_log(c->fc, AV_LOG_ERROR, "%s configurationVersion %d",
929  version < 1 ? "invalid" : "unsupported", version);
930  return AVERROR_INVALIDDATA;
931  }
932 
933  descriptors_size = ffio_read_leb(pb);
934  if (!descriptors_size || descriptors_size > INT_MAX)
935  return AVERROR_INVALIDDATA;
936 
937  st = c->fc->streams[c->fc->nb_streams - 1];
938  sc = st->priv_data;
939 
940  if (st->codecpar->extradata) {
941  av_log(c->fc, AV_LOG_WARNING, "ignoring iacb\n");
942  return 0;
943  }
944 
945  sc->iamf = av_mallocz(sizeof(*sc->iamf));
946  if (!sc->iamf)
947  return AVERROR(ENOMEM);
948  iamf = &sc->iamf->iamf;
949 
950  ret = ff_alloc_extradata(st->codecpar, descriptors_size);
951  if (ret < 0)
952  return ret;
953 
954  ret = avio_read(pb, st->codecpar->extradata, descriptors_size);
955  if (ret != descriptors_size)
956  return ret < 0 ? ret : AVERROR_INVALIDDATA;
957 
958  ffio_init_read_context(&b, st->codecpar->extradata, descriptors_size);
959  descriptor_pb = &b.pub;
960 
961  ret = ff_iamfdec_read_descriptors(iamf, descriptor_pb, descriptors_size, c->fc);
962  if (ret < 0)
963  return ret;
964 
965  metadata = st->metadata;
966  st->metadata = NULL;
967  start_time = st->start_time;
968  nb_frames = st->nb_frames;
969  duration = st->duration;
970  disposition = st->disposition;
971 
972  for (int i = 0; i < iamf->nb_audio_elements; i++) {
973  IAMFAudioElement *audio_element = iamf->audio_elements[i];
974  const AVIAMFAudioElement *element;
975  AVStreamGroup *stg =
977 
978  if (!stg) {
979  ret = AVERROR(ENOMEM);
980  goto fail;
981  }
982 
984  stg->id = audio_element->audio_element_id;
985  /* Transfer ownership */
986  element = stg->params.iamf_audio_element = audio_element->element;
987  audio_element->element = NULL;
988 
989  for (int j = 0; j < audio_element->nb_substreams; j++) {
990  IAMFSubStream *substream = &audio_element->substreams[j];
991  AVStream *stream;
992 
993  if (!i && !j) {
994  if (audio_element->layers[0].substream_count != 1)
995  disposition &= ~AV_DISPOSITION_DEFAULT;
996  stream = st;
997  } else
998  stream = avformat_new_stream(c->fc, NULL);
999  if (!stream) {
1000  ret = AVERROR(ENOMEM);
1001  goto fail;
1002  }
1003 
1004  stream->start_time = start_time;
1005  stream->nb_frames = nb_frames;
1006  stream->duration = duration;
1007  stream->disposition = disposition;
1008  if (stream != st) {
1009  stream->priv_data = sc;
1010  sc->refcount++;
1011  }
1012 
1015  if (i || j) {
1017  if (audio_element->layers[0].substream_count == 1)
1018  stream->disposition &= ~AV_DISPOSITION_DEFAULT;
1019  }
1020 
1021  ret = avcodec_parameters_copy(stream->codecpar, substream->codecpar);
1022  if (ret < 0)
1023  goto fail;
1024 
1025  stream->id = substream->audio_substream_id;
1026 
1027  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
1028 
1029  ret = avformat_stream_group_add_stream(stg, stream);
1030  if (ret < 0)
1031  goto fail;
1032  }
1033 
1034  ret = av_dict_copy(&stg->metadata, metadata, 0);
1035  if (ret < 0)
1036  goto fail;
1037  }
1038 
1039  for (int i = 0; i < iamf->nb_mix_presentations; i++) {
1040  IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
1041  const AVIAMFMixPresentation *mix = mix_presentation->cmix;
1042  AVStreamGroup *stg =
1044 
1045  if (!stg) {
1046  ret = AVERROR(ENOMEM);
1047  goto fail;
1048  }
1049 
1051  stg->id = mix_presentation->mix_presentation_id;
1052  /* Transfer ownership */
1053  stg->params.iamf_mix_presentation = mix_presentation->mix;
1054  mix_presentation->mix = NULL;
1055 
1056  for (int j = 0; j < mix->nb_submixes; j++) {
1057  const AVIAMFSubmix *submix = mix->submixes[j];
1058 
1059  for (int k = 0; k < submix->nb_elements; k++) {
1060  const AVIAMFSubmixElement *submix_element = submix->elements[k];
1061  const AVStreamGroup *audio_element = NULL;
1062 
1063  for (int l = 0; l < c->fc->nb_stream_groups; l++)
1064  if (c->fc->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
1065  c->fc->stream_groups[l]->id == submix_element->audio_element_id) {
1066  audio_element = c->fc->stream_groups[l];
1067  break;
1068  }
1069  av_assert0(audio_element);
1070 
1071  for (int l = 0; l < audio_element->nb_streams; l++) {
1072  ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
1073  if (ret < 0 && ret != AVERROR(EEXIST))
1074  goto fail;
1075  }
1076  }
1077  }
1078 
1079  ret = av_dict_copy(&stg->metadata, metadata, 0);
1080  if (ret < 0)
1081  goto fail;
1082  }
1083 
1084  ret = 0;
1085 fail:
1087 
1088  return ret;
1089 }
1090 #endif
1091 
1093 {
1094  AVStream *st;
1095  int32_t sample_rate;
1096 
1097  if (atom.size < 8 || c->fc->nb_streams < 1)
1098  return 0;
1099 
1100  st = c->fc->streams[c->fc->nb_streams-1];
1101  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
1102  av_log(c->fc, AV_LOG_WARNING, "'srat' within non-audio sample entry, skip\n");
1103  return 0;
1104  }
1105 
1106  if (!c->isom) {
1107  av_log(c->fc, AV_LOG_WARNING, "'srat' within non-isom, skip\n");
1108  return 0;
1109  }
1110 
1111  avio_skip(pb, 4); // version+flags
1112  sample_rate = avio_rb32(pb);
1113  if (sample_rate > 0) {
1114  av_log(c->fc, AV_LOG_DEBUG,
1115  "overwrite sample rate from %d to %d by 'srat'\n",
1116  st->codecpar->sample_rate, sample_rate);
1117  st->codecpar->sample_rate = sample_rate;
1118  } else {
1119  av_log(c->fc, AV_LOG_WARNING,
1120  "ignore invalid sample rate %d in 'srat'\n", sample_rate);
1121  }
1122 
1123  return 0;
1124 }
1125 
1127 {
1128  AVStream *st;
1129  AVPacketSideData *sd;
1130  enum AVAudioServiceType *ast;
1131  int eac3info, acmod, lfeon, bsmod;
1132  uint64_t mask;
1133 
1134  if (c->fc->nb_streams < 1)
1135  return 0;
1136  st = c->fc->streams[c->fc->nb_streams-1];
1137 
1141  sizeof(*ast), 0);
1142  if (!sd)
1143  return AVERROR(ENOMEM);
1144 
1145  ast = (enum AVAudioServiceType*)sd->data;
1146 
1147  /* No need to parse fields for additional independent substreams and its
1148  * associated dependent substreams since libavcodec's E-AC-3 decoder
1149  * does not support them yet. */
1150  avio_rb16(pb); /* data_rate and num_ind_sub */
1151  eac3info = avio_rb24(pb);
1152  bsmod = (eac3info >> 12) & 0x1f;
1153  acmod = (eac3info >> 9) & 0x7;
1154  lfeon = (eac3info >> 8) & 0x1;
1155 
1157  if (lfeon)
1161 
1162  *ast = bsmod;
1163  if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
1165 
1166  return 0;
1167 }
1168 
1170 {
1171 #define DDTS_SIZE 20
1172  uint8_t buf[DDTS_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
1173  AVStream *st = NULL;
1174  uint32_t frame_duration_code = 0;
1175  uint32_t channel_layout_code = 0;
1176  GetBitContext gb;
1177  int ret;
1178 
1179  if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
1180  return ret;
1181 
1182  init_get_bits(&gb, buf, 8 * DDTS_SIZE);
1183 
1184  if (c->fc->nb_streams < 1) {
1185  return 0;
1186  }
1187  st = c->fc->streams[c->fc->nb_streams-1];
1188 
1189  st->codecpar->sample_rate = get_bits_long(&gb, 32);
1190  if (st->codecpar->sample_rate <= 0) {
1191  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
1192  return AVERROR_INVALIDDATA;
1193  }
1194  skip_bits_long(&gb, 32); /* max bitrate */
1195  st->codecpar->bit_rate = get_bits_long(&gb, 32);
1196  st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
1197  frame_duration_code = get_bits(&gb, 2);
1198  skip_bits(&gb, 30); /* various fields */
1199  channel_layout_code = get_bits(&gb, 16);
1200 
1201  st->codecpar->frame_size =
1202  (frame_duration_code == 0) ? 512 :
1203  (frame_duration_code == 1) ? 1024 :
1204  (frame_duration_code == 2) ? 2048 :
1205  (frame_duration_code == 3) ? 4096 : 0;
1206 
1207  if (channel_layout_code > 0xff) {
1208  av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
1209  }
1212  ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
1213  ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
1214  ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
1215  ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
1216  ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
1217  ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0));
1218 
1219  return 0;
1220 }
1221 
1223 {
1224  AVStream *st;
1225 
1226  if (c->fc->nb_streams < 1)
1227  return 0;
1228  st = c->fc->streams[c->fc->nb_streams-1];
1229 
1230  if (atom.size < 16)
1231  return 0;
1232 
1233  /* skip version and flags */
1234  avio_skip(pb, 4);
1235 
1236  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
1237 
1238  return 0;
1239 }
1240 
1242 {
1243  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
1244  int version, flags;
1245  int ret;
1246  AVStream *st;
1247 
1248  if (c->fc->nb_streams < 1)
1249  return 0;
1250  st = c->fc->streams[c->fc->nb_streams-1];
1251 
1252  version = avio_r8(pb);
1253  flags = avio_rb24(pb);
1254  if (version > 1 || flags != 0) {
1255  av_log(c->fc, AV_LOG_WARNING,
1256  "Unsupported 'chnl' box with version %d, flags: %#x\n",
1257  version, flags);
1258  return 0;
1259  }
1260 
1261  ret = ff_mov_read_chnl(c->fc, pb, st, version);
1262  if (ret < 0) {
1263  avio_seek(pb, end, SEEK_SET);
1264  return 0;
1265  }
1266 
1267  if (avio_tell(pb) != end) {
1268  av_log(c->fc, AV_LOG_WARNING, "skip %" PRId64 " bytes of unknown data inside chnl\n",
1269  end - avio_tell(pb));
1270  avio_seek(pb, end, SEEK_SET);
1271  }
1272  return ret;
1273 }
1274 
1276 {
1277  AVStream *st;
1278  int ret;
1279 
1280  if (c->fc->nb_streams < 1)
1281  return 0;
1282  st = c->fc->streams[c->fc->nb_streams-1];
1283 
1284  if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
1285  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
1286 
1287  return ret;
1288 }
1289 
1291 {
1292  AVStream *st;
1293  HEIFItem *item;
1294  AVPacketSideData *sd;
1295  int width, height, err = 0;
1296  AVRational aperture_width, aperture_height, horiz_off, vert_off;
1297  AVRational pc_x, pc_y;
1298  uint64_t top, bottom, left, right;
1299 
1300  item = get_heif_item(c, c->cur_item_id);
1301  st = get_curr_st(c);
1302  if (!st)
1303  return 0;
1304 
1305  width = st->codecpar->width;
1306  height = st->codecpar->height;
1307  if ((!width || !height) && item) {
1308  width = item->width;
1309  height = item->height;
1310  }
1311  if (!width || !height) {
1312  err = AVERROR_INVALIDDATA;
1313  goto fail;
1314  }
1315 
1316  aperture_width.num = avio_rb32(pb);
1317  aperture_width.den = avio_rb32(pb);
1318  aperture_height.num = avio_rb32(pb);
1319  aperture_height.den = avio_rb32(pb);
1320 
1321  horiz_off.num = avio_rb32(pb);
1322  horiz_off.den = avio_rb32(pb);
1323  vert_off.num = avio_rb32(pb);
1324  vert_off.den = avio_rb32(pb);
1325 
1326  if (aperture_width.num < 0 || aperture_width.den < 0 ||
1327  aperture_height.num < 0 || aperture_height.den < 0 ||
1328  horiz_off.den < 0 || vert_off.den < 0) {
1329  err = AVERROR_INVALIDDATA;
1330  goto fail;
1331  }
1332  if ((av_cmp_q((AVRational) { width, 1 }, aperture_width) < 0) ||
1333  (av_cmp_q((AVRational) { height, 1 }, aperture_height) < 0)) {
1334  err = AVERROR_INVALIDDATA;
1335  goto fail;
1336  }
1337  av_log(c->fc, AV_LOG_TRACE, "clap: apertureWidth %d/%d, apertureHeight %d/%d "
1338  "horizOff %d/%d vertOff %d/%d\n",
1339  aperture_width.num, aperture_width.den, aperture_height.num, aperture_height.den,
1340  horiz_off.num, horiz_off.den, vert_off.num, vert_off.den);
1341 
1342  pc_x = av_mul_q((AVRational) { width - 1, 1 }, (AVRational) { 1, 2 });
1343  pc_x = av_add_q(pc_x, horiz_off);
1344  pc_y = av_mul_q((AVRational) { height - 1, 1 }, (AVRational) { 1, 2 });
1345  pc_y = av_add_q(pc_y, vert_off);
1346 
1347  aperture_width = av_sub_q(aperture_width, (AVRational) { 1, 1 });
1348  aperture_width = av_mul_q(aperture_width, (AVRational) { 1, 2 });
1349  aperture_height = av_sub_q(aperture_height, (AVRational) { 1, 1 });
1350  aperture_height = av_mul_q(aperture_height, (AVRational) { 1, 2 });
1351 
1352  left = av_q2d(av_sub_q(pc_x, aperture_width));
1353  right = av_q2d(av_add_q(pc_x, aperture_width));
1354  top = av_q2d(av_sub_q(pc_y, aperture_height));
1355  bottom = av_q2d(av_add_q(pc_y, aperture_height));
1356 
1357  if (bottom > (height - 1) ||
1358  right > (width - 1)) {
1359  err = AVERROR_INVALIDDATA;
1360  goto fail;
1361  }
1362 
1363  bottom = height - 1 - bottom;
1364  right = width - 1 - right;
1365 
1366  if (!(left | right | top | bottom))
1367  return 0;
1368 
1369  if ((left + right) >= width ||
1370  (top + bottom) >= height) {
1371  err = AVERROR_INVALIDDATA;
1372  goto fail;
1373  }
1374 
1378  sizeof(uint32_t) * 4, 0);
1379  if (!sd)
1380  return AVERROR(ENOMEM);
1381 
1382  AV_WL32A(sd->data, top);
1383  AV_WL32A(sd->data + 4, bottom);
1384  AV_WL32A(sd->data + 8, left);
1385  AV_WL32A(sd->data + 12, right);
1386 
1387 fail:
1388  if (err < 0) {
1389  int explode = !!(c->fc->error_recognition & AV_EF_EXPLODE);
1390  av_log(c->fc, explode ? AV_LOG_ERROR : AV_LOG_WARNING, "Invalid clap box\n");
1391  if (!explode)
1392  err = 0;
1393  }
1394 
1395  return err;
1396 }
1397 
1398 /* This atom overrides any previously set aspect ratio */
1400 {
1401  const int num = avio_rb32(pb);
1402  const int den = avio_rb32(pb);
1403  AVStream *st;
1404  MOVStreamContext *sc;
1405 
1406  if (c->fc->nb_streams < 1)
1407  return 0;
1408  st = c->fc->streams[c->fc->nb_streams-1];
1409  sc = st->priv_data;
1410 
1411  av_log(c->fc, AV_LOG_TRACE, "pasp: hSpacing %d, vSpacing %d\n", num, den);
1412 
1413  if (den != 0) {
1414  sc->h_spacing = num;
1415  sc->v_spacing = den;
1416  }
1417  return 0;
1418 }
1419 
1420 /* this atom contains actual media data */
1422 {
1423  if (atom.size == 0) /* wrong one (MP4) */
1424  return 0;
1425  c->found_mdat=1;
1426  return 0; /* now go for moov */
1427 }
1428 
1429 #define DRM_BLOB_SIZE 56
1430 
1432 {
1433  uint8_t intermediate_key[20];
1434  uint8_t intermediate_iv[20];
1435  uint8_t input[64];
1436  uint8_t output[64];
1437  uint8_t file_checksum[20];
1438  uint8_t calculated_checksum[20];
1439  char checksum_string[2 * sizeof(file_checksum) + 1];
1440  struct AVSHA *sha;
1441  int i;
1442  int ret = 0;
1443  uint8_t *activation_bytes = c->activation_bytes;
1444  uint8_t *fixed_key = c->audible_fixed_key;
1445 
1446  c->aax_mode = 1;
1447 
1448  sha = av_sha_alloc();
1449  if (!sha)
1450  return AVERROR(ENOMEM);
1451  av_free(c->aes_decrypt);
1452  c->aes_decrypt = av_aes_alloc();
1453  if (!c->aes_decrypt) {
1454  ret = AVERROR(ENOMEM);
1455  goto fail;
1456  }
1457 
1458  /* drm blob processing */
1459  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1461  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1462  ret = ffio_read_size(pb, file_checksum, 20);
1463  if (ret < 0)
1464  goto fail;
1465 
1466  // required by external tools
1467  ff_data_to_hex(checksum_string, file_checksum, sizeof(file_checksum), 1);
1468  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == %s\n", checksum_string);
1469 
1470  /* verify activation data */
1471  if (!activation_bytes) {
1472  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1473  ret = 0; /* allow ffprobe to continue working on .aax files */
1474  goto fail;
1475  }
1476  if (c->activation_bytes_size != 4) {
1477  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1478  ret = AVERROR(EINVAL);
1479  goto fail;
1480  }
1481 
1482  /* verify fixed key */
1483  if (c->audible_fixed_key_size != 16) {
1484  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1485  ret = AVERROR(EINVAL);
1486  goto fail;
1487  }
1488 
1489  /* AAX (and AAX+) key derivation */
1490  av_sha_init(sha, 160);
1491  av_sha_update(sha, fixed_key, 16);
1492  av_sha_update(sha, activation_bytes, 4);
1493  av_sha_final(sha, intermediate_key);
1494  av_sha_init(sha, 160);
1495  av_sha_update(sha, fixed_key, 16);
1496  av_sha_update(sha, intermediate_key, 20);
1497  av_sha_update(sha, activation_bytes, 4);
1498  av_sha_final(sha, intermediate_iv);
1499  av_sha_init(sha, 160);
1500  av_sha_update(sha, intermediate_key, 16);
1501  av_sha_update(sha, intermediate_iv, 16);
1502  av_sha_final(sha, calculated_checksum);
1503  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1504  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1506  goto fail;
1507  }
1508  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1509  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1510  for (i = 0; i < 4; i++) {
1511  // file data (in output) is stored in big-endian mode
1512  if (activation_bytes[i] != output[3 - i]) { // critical error
1513  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1515  goto fail;
1516  }
1517  }
1518  memcpy(c->file_key, output + 8, 16);
1519  memcpy(input, output + 26, 16);
1520  av_sha_init(sha, 160);
1521  av_sha_update(sha, input, 16);
1522  av_sha_update(sha, c->file_key, 16);
1523  av_sha_update(sha, fixed_key, 16);
1524  av_sha_final(sha, c->file_iv);
1525 
1526 fail:
1527  av_free(sha);
1528 
1529  return ret;
1530 }
1531 
1533 {
1534  if (c->audible_key_size != 16) {
1535  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
1536  return AVERROR(EINVAL);
1537  }
1538 
1539  if (c->audible_iv_size != 16) {
1540  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
1541  return AVERROR(EINVAL);
1542  }
1543 
1544  c->aes_decrypt = av_aes_alloc();
1545  if (!c->aes_decrypt) {
1546  return AVERROR(ENOMEM);
1547  }
1548 
1549  memcpy(c->file_key, c->audible_key, 16);
1550  memcpy(c->file_iv, c->audible_iv, 16);
1551  c->aax_mode = 1;
1552 
1553  return 0;
1554 }
1555 
1556 // Audible AAX (and AAX+) bytestream decryption
1557 static int aax_filter(uint8_t *input, int size, MOVContext *c)
1558 {
1559  int blocks = 0;
1560  unsigned char iv[16];
1561 
1562  memcpy(iv, c->file_iv, 16); // iv is overwritten
1563  blocks = size >> 4; // trailing bytes are not encrypted!
1564  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1565  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1566 
1567  return 0;
1568 }
1569 
1570 /* read major brand, minor version and compatible brands and store them as metadata */
1572 {
1573  uint32_t minor_ver;
1574  int comp_brand_size;
1575  char* comp_brands_str;
1576  uint8_t type[5] = {0};
1577  int ret = ffio_read_size(pb, type, 4);
1578  if (ret < 0)
1579  return ret;
1580  if (c->fc->nb_streams) {
1581  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT)
1582  return AVERROR_INVALIDDATA;
1583  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate FTYP\n");
1584  return 0;
1585  }
1586 
1587  if (strcmp(type, "qt "))
1588  c->isom = 1;
1589  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1590  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1591  minor_ver = avio_rb32(pb); /* minor version */
1592  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1593 
1594  comp_brand_size = atom.size - 8;
1595  if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
1596  return AVERROR_INVALIDDATA;
1597  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1598  if (!comp_brands_str)
1599  return AVERROR(ENOMEM);
1600 
1601  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1602  if (ret < 0) {
1603  av_freep(&comp_brands_str);
1604  return ret;
1605  }
1606  comp_brands_str[comp_brand_size] = 0;
1607  av_dict_set(&c->fc->metadata, "compatible_brands",
1608  comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
1609 
1610  // Logic for handling Audible's .aaxc files
1611  if (!strcmp(type, "aaxc")) {
1612  mov_aaxc_crypto(c);
1613  }
1614 
1615  return 0;
1616 }
1617 
1618 /* this atom should contain all header atoms */
1620 {
1621  int ret;
1622 
1623  if (c->found_moov) {
1624  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1625  avio_skip(pb, atom.size);
1626  return 0;
1627  }
1628 
1629  if ((ret = mov_read_default(c, pb, atom)) < 0)
1630  return ret;
1631  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1632  /* so we don't parse the whole file if over a network */
1633  c->found_moov=1;
1634  return 0; /* now go for mdat */
1635 }
1636 
1638  MOVFragmentIndex *frag_index,
1639  int index,
1640  int id)
1641 {
1642  int i;
1643  MOVFragmentIndexItem * item;
1644 
1645  if (index < 0 || index >= frag_index->nb_items)
1646  return NULL;
1647  item = &frag_index->item[index];
1648  for (i = 0; i < item->nb_stream_info; i++)
1649  if (item->stream_info[i].id == id)
1650  return &item->stream_info[i];
1651 
1652  // This shouldn't happen
1653  return NULL;
1654 }
1655 
1656 static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
1657 {
1658  int i;
1659  MOVFragmentIndexItem * item;
1660 
1661  if (frag_index->current < 0 ||
1662  frag_index->current >= frag_index->nb_items)
1663  return;
1664 
1665  item = &frag_index->item[frag_index->current];
1666  for (i = 0; i < item->nb_stream_info; i++)
1667  if (item->stream_info[i].id == id) {
1668  item->current = i;
1669  return;
1670  }
1671 
1672  // id not found. This shouldn't happen.
1673  item->current = -1;
1674 }
1675 
1677  MOVFragmentIndex *frag_index)
1678 {
1679  MOVFragmentIndexItem *item;
1680  if (frag_index->current < 0 ||
1681  frag_index->current >= frag_index->nb_items)
1682  return NULL;
1683 
1684  item = &frag_index->item[frag_index->current];
1685  if (item->current >= 0 && item->current < item->nb_stream_info)
1686  return &item->stream_info[item->current];
1687 
1688  // This shouldn't happen
1689  return NULL;
1690 }
1691 
1693 {
1694  int a, b, m;
1695  int64_t moof_offset;
1696 
1697  // Optimize for appending new entries
1698  if (!frag_index->nb_items ||
1699  frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
1700  return frag_index->nb_items;
1701 
1702  a = -1;
1703  b = frag_index->nb_items;
1704 
1705  while (b - a > 1) {
1706  m = (a + b) >> 1;
1707  moof_offset = frag_index->item[m].moof_offset;
1708  if (moof_offset >= offset)
1709  b = m;
1710  if (moof_offset <= offset)
1711  a = m;
1712  }
1713  return b;
1714 }
1715 
1717 {
1718  av_assert0(frag_stream_info);
1719  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1720  return frag_stream_info->sidx_pts;
1721  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1722  return frag_stream_info->first_tfra_pts;
1723  return frag_stream_info->tfdt_dts;
1724 }
1725 
1727  MOVFragmentIndex *frag_index, int index)
1728 {
1729  MOVFragmentStreamInfo * frag_stream_info;
1730  MOVStreamContext *sc = dst_st->priv_data;
1731  int64_t timestamp;
1732  int i, j;
1733 
1734  // If the stream is referenced by any sidx, limit the search
1735  // to fragments that referenced this stream in the sidx
1736  if (sc->has_sidx) {
1737  frag_stream_info = get_frag_stream_info(frag_index, index, sc->id);
1738  if (!frag_stream_info)
1739  return AV_NOPTS_VALUE;
1740  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1741  return frag_stream_info->sidx_pts;
1742  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1743  return frag_stream_info->first_tfra_pts;
1744  return frag_stream_info->sidx_pts;
1745  }
1746 
1747  for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
1748  if (dst_st->id != frag_index->item[index].stream_info[i].id)
1749  continue;
1750  AVStream *frag_stream = NULL;
1751  frag_stream_info = &frag_index->item[index].stream_info[i];
1752  for (j = 0; j < s->nb_streams; j++) {
1753  MOVStreamContext *sc2 = s->streams[j]->priv_data;
1754  if (sc2->id == frag_stream_info->id)
1755  frag_stream = s->streams[j];
1756  }
1757  if (!frag_stream) {
1758  av_log(s, AV_LOG_WARNING, "No stream matching sidx ID found.\n");
1759  continue;
1760  }
1761  timestamp = get_stream_info_time(frag_stream_info);
1762  if (timestamp != AV_NOPTS_VALUE)
1763  return av_rescale_q(timestamp, frag_stream->time_base, dst_st->time_base);
1764  }
1765  return AV_NOPTS_VALUE;
1766 }
1767 
1769  AVStream *st, int64_t timestamp)
1770 {
1771  int a, b, m, m0;
1772  int64_t frag_time;
1773 
1774  a = -1;
1775  b = frag_index->nb_items;
1776 
1777  while (b - a > 1) {
1778  m0 = m = (a + b) >> 1;
1779 
1780  while (m < b &&
1781  (frag_time = get_frag_time(s, st, frag_index, m)) == AV_NOPTS_VALUE)
1782  m++;
1783 
1784  if (m < b && frag_time <= timestamp)
1785  a = m;
1786  else
1787  b = m0;
1788  }
1789 
1790  return a;
1791 }
1792 
1794 {
1795  int index, i;
1796  MOVFragmentIndexItem * item;
1797  MOVFragmentStreamInfo * frag_stream_info;
1798 
1799  // If moof_offset already exists in frag_index, return index to it
1800  index = search_frag_moof_offset(&c->frag_index, offset);
1801  if (index < c->frag_index.nb_items &&
1802  c->frag_index.item[index].moof_offset == offset)
1803  return index;
1804 
1805  // offset is not yet in frag index.
1806  // Insert new item at index (sorted by moof offset)
1807  item = av_fast_realloc(c->frag_index.item,
1808  &c->frag_index.allocated_size,
1809  (c->frag_index.nb_items + 1) *
1810  sizeof(*c->frag_index.item));
1811  if (!item)
1812  return -1;
1813  c->frag_index.item = item;
1814 
1815  frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
1816  sizeof(*item->stream_info));
1817  if (!frag_stream_info)
1818  return -1;
1819 
1820  for (i = 0; i < c->fc->nb_streams; i++) {
1821  // Avoid building frag index if streams lack track id.
1822  MOVStreamContext *sc = c->fc->streams[i]->priv_data;
1823  if (sc->id < 0) {
1824  av_free(frag_stream_info);
1825  return AVERROR_INVALIDDATA;
1826  }
1827 
1828  frag_stream_info[i].id = sc->id;
1829  frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
1830  frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
1831  frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
1832  frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
1833  frag_stream_info[i].index_base = -1;
1834  frag_stream_info[i].index_entry = -1;
1835  frag_stream_info[i].encryption_index = NULL;
1836  frag_stream_info[i].stsd_id = -1;
1837  }
1838 
1839  if (index < c->frag_index.nb_items)
1840  memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
1841  (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
1842 
1843  item = &c->frag_index.item[index];
1844  item->headers_read = 0;
1845  item->current = 0;
1846  item->nb_stream_info = c->fc->nb_streams;
1847  item->moof_offset = offset;
1848  item->stream_info = frag_stream_info;
1849  c->frag_index.nb_items++;
1850 
1851  return index;
1852 }
1853 
1854 static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
1855  int id, int entries)
1856 {
1857  int i;
1858  MOVFragmentStreamInfo * frag_stream_info;
1859 
1860  if (index < 0)
1861  return;
1862  for (i = index; i < frag_index->nb_items; i++) {
1863  frag_stream_info = get_frag_stream_info(frag_index, i, id);
1864  if (frag_stream_info && frag_stream_info->index_entry >= 0)
1865  frag_stream_info->index_entry += entries;
1866  }
1867 }
1868 
1870 {
1871  // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
1872  c->fragment.found_tfhd = 0;
1873 
1874  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1875  c->has_looked_for_mfra = 1;
1876  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1877  int ret;
1878  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1879  "for a mfra\n");
1880  if ((ret = mov_read_mfra(c, pb)) < 0) {
1881  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1882  "read the mfra (may be a live ismv)\n");
1883  }
1884  } else {
1885  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1886  "seekable, can not look for mfra\n");
1887  }
1888  }
1889  c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1890  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1891  c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
1892  return mov_read_default(c, pb, atom);
1893 }
1894 
1896 {
1897  int64_t time;
1898  if (version == 1) {
1899  time = avio_rb64(pb);
1900  avio_rb64(pb);
1901  if (time < 0) {
1902  av_log(c->fc, AV_LOG_DEBUG, "creation_time is negative\n");
1903  return;
1904  }
1905  } else {
1906  time = avio_rb32(pb);
1907  avio_rb32(pb); /* modification time */
1908  if (time > 0 && time < 2082844800) {
1909  av_log(c->fc, AV_LOG_WARNING, "Detected creation time before 1970, parsing as unix timestamp.\n");
1910  time += 2082844800;
1911  }
1912  }
1913  if (time) {
1914  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1915 
1916  if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1917  av_log(c->fc, AV_LOG_DEBUG, "creation_time is not representable\n");
1918  return;
1919  }
1920 
1921  ff_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1922  }
1923 }
1924 
1926 {
1927  AVStream *st;
1928  MOVStreamContext *sc;
1929  int version;
1930  char language[4] = {0};
1931  unsigned lang;
1932 
1933  if (c->fc->nb_streams < 1)
1934  return 0;
1935  st = c->fc->streams[c->fc->nb_streams-1];
1936  sc = st->priv_data;
1937 
1938  if (sc->time_scale) {
1939  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1940  return AVERROR_INVALIDDATA;
1941  }
1942 
1943  version = avio_r8(pb);
1944  if (version > 1) {
1945  avpriv_request_sample(c->fc, "Version %d", version);
1946  return AVERROR_PATCHWELCOME;
1947  }
1948  avio_rb24(pb); /* flags */
1950 
1951  sc->time_scale = avio_rb32(pb);
1952  if (sc->time_scale <= 0) {
1953  av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1954  sc->time_scale = 1;
1955  }
1956  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1957 
1958  if ((version == 1 && st->duration == UINT64_MAX) ||
1959  (version != 1 && st->duration == UINT32_MAX)) {
1960  st->duration = 0;
1961  }
1962 
1963  lang = avio_rb16(pb); /* language */
1964  if (ff_mov_lang_to_iso639(lang, language))
1965  av_dict_set(&st->metadata, "language", language, 0);
1966  avio_rb16(pb); /* quality */
1967 
1968  return 0;
1969 }
1970 
1972 {
1973  int i;
1974  int version = avio_r8(pb); /* version */
1975  avio_rb24(pb); /* flags */
1976 
1977  mov_metadata_creation_time(c, pb, &c->fc->metadata, version);
1978  c->time_scale = avio_rb32(pb); /* time scale */
1979  if (c->time_scale <= 0) {
1980  av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
1981  c->time_scale = 1;
1982  }
1983  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1984 
1985  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1986  avio_rb32(pb); /* preferred scale */
1987 
1988  avio_rb16(pb); /* preferred volume */
1989 
1990  avio_skip(pb, 10); /* reserved */
1991 
1992  /* movie display matrix, store it in main context and use it later on */
1993  for (i = 0; i < 3; i++) {
1994  c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1995  c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1996  c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
1997  }
1998 
1999  avio_rb32(pb); /* preview time */
2000  avio_rb32(pb); /* preview duration */
2001  avio_rb32(pb); /* poster time */
2002  avio_rb32(pb); /* selection time */
2003  avio_rb32(pb); /* selection duration */
2004  avio_rb32(pb); /* current time */
2005  avio_rb32(pb); /* next track ID */
2006 
2007  return 0;
2008 }
2009 
2011 {
2012  AVStream *st;
2013 
2014  if (fc->nb_streams < 1)
2015  return;
2016  st = fc->streams[fc->nb_streams-1];
2017 
2018  switch (st->codecpar->codec_id) {
2019  case AV_CODEC_ID_PCM_S16BE:
2021  break;
2022  case AV_CODEC_ID_PCM_S24BE:
2024  break;
2025  case AV_CODEC_ID_PCM_S32BE:
2027  break;
2028  case AV_CODEC_ID_PCM_F32BE:
2030  break;
2031  case AV_CODEC_ID_PCM_F64BE:
2033  break;
2034  default:
2035  break;
2036  }
2037 }
2038 
2040 {
2041  int little_endian = avio_rb16(pb) & 0xFF;
2042  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
2043  if (little_endian == 1)
2045  return 0;
2046 }
2047 
2049 {
2050  int format_flags;
2051  int version, flags;
2052  int pcm_sample_size;
2053  AVFormatContext *fc = c->fc;
2054  AVStream *st;
2055  MOVStreamContext *sc;
2056 
2057  if (atom.size < 6) {
2058  av_log(c->fc, AV_LOG_ERROR, "Empty pcmC box\n");
2059  return AVERROR_INVALIDDATA;
2060  }
2061 
2062  version = avio_r8(pb);
2063  flags = avio_rb24(pb);
2064 
2065  if (version != 0 || flags != 0) {
2066  av_log(c->fc, AV_LOG_ERROR,
2067  "Unsupported 'pcmC' box with version %d, flags: %x",
2068  version, flags);
2069  return AVERROR_INVALIDDATA;
2070  }
2071 
2072  format_flags = avio_r8(pb);
2073  pcm_sample_size = avio_r8(pb);
2074 
2075  if (fc->nb_streams < 1)
2076  return AVERROR_INVALIDDATA;
2077 
2078  st = fc->streams[fc->nb_streams - 1];
2079  sc = st->priv_data;
2080 
2081  if (sc->format == MOV_MP4_FPCM_TAG) {
2082  switch (pcm_sample_size) {
2083  case 32:
2085  break;
2086  case 64:
2088  break;
2089  default:
2090  av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
2091  pcm_sample_size,
2092  av_fourcc2str(sc->format));
2093  return AVERROR_INVALIDDATA;
2094  }
2095  } else if (sc->format == MOV_MP4_IPCM_TAG) {
2096  switch (pcm_sample_size) {
2097  case 16:
2099  break;
2100  case 24:
2102  break;
2103  case 32:
2105  break;
2106  default:
2107  av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
2108  pcm_sample_size,
2109  av_fourcc2str(sc->format));
2110  return AVERROR_INVALIDDATA;
2111  }
2112  } else {
2113  av_log(fc, AV_LOG_ERROR, "'pcmC' with invalid sample entry '%s'\n",
2114  av_fourcc2str(sc->format));
2115  return AVERROR_INVALIDDATA;
2116  }
2117 
2118  if (format_flags & 1) // indicates little-endian format. If not present, big-endian format is used
2121 
2122  return 0;
2123 }
2124 
2126 {
2127  AVStream *st;
2128  HEIFItem *item = NULL;
2129  char color_parameter_type[5] = { 0 };
2130  uint16_t color_primaries, color_trc, color_matrix;
2131  int ret;
2132 
2133  st = get_curr_st(c);
2134  if (!st) {
2135  item = get_heif_item(c, c->cur_item_id);
2136  if (!item)
2137  return 0;
2138  }
2139 
2140  ret = ffio_read_size(pb, color_parameter_type, 4);
2141  if (ret < 0)
2142  return ret;
2143  if (strncmp(color_parameter_type, "nclx", 4) &&
2144  strncmp(color_parameter_type, "nclc", 4) &&
2145  strncmp(color_parameter_type, "prof", 4)) {
2146  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
2147  color_parameter_type);
2148  return 0;
2149  }
2150 
2151  if (!strncmp(color_parameter_type, "prof", 4)) {
2152  AVPacketSideData *sd;
2153  uint8_t *icc_profile;
2154  if (st) {
2158  atom.size - 4, 0);
2159  if (!sd)
2160  return AVERROR(ENOMEM);
2161  icc_profile = sd->data;
2162  } else {
2163  if (c->heif_icc_profile_items >= c->fc->max_streams) {
2164  av_log(c->fc, AV_LOG_WARNING,
2165  "HEIF ICC profile copies exceed cap %d; ignoring further items\n",
2166  c->fc->max_streams);
2167  return 0;
2168  }
2169  av_freep(&item->icc_profile);
2170  icc_profile = item->icc_profile = av_malloc(atom.size - 4);
2171  if (!icc_profile) {
2172  item->icc_profile_size = 0;
2173  return AVERROR(ENOMEM);
2174  }
2175  item->icc_profile_size = atom.size - 4;
2176  c->heif_icc_profile_items++;
2177  }
2178  ret = ffio_read_size(pb, icc_profile, atom.size - 4);
2179  if (ret < 0)
2180  return ret;
2181  } else if (st) {
2182  color_primaries = avio_rb16(pb);
2183  color_trc = avio_rb16(pb);
2184  color_matrix = avio_rb16(pb);
2185 
2186  av_log(c->fc, AV_LOG_TRACE,
2187  "%s: pri %d trc %d matrix %d",
2188  color_parameter_type, color_primaries, color_trc, color_matrix);
2189 
2190  if (!strncmp(color_parameter_type, "nclx", 4)) {
2191  uint8_t color_range = avio_r8(pb) >> 7;
2192  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
2193  if (color_range)
2195  else
2197  }
2198 
2201  if (!av_color_transfer_name(color_trc))
2202  color_trc = AVCOL_TRC_UNSPECIFIED;
2203  if (!av_color_space_name(color_matrix))
2204  color_matrix = AVCOL_SPC_UNSPECIFIED;
2205 
2207  st->codecpar->color_trc = color_trc;
2208  st->codecpar->color_space = color_matrix;
2209  av_log(c->fc, AV_LOG_TRACE, "\n");
2210  }
2211  return 0;
2212 }
2213 
2215 {
2216  AVStream *st;
2217  unsigned mov_field_order;
2218  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
2219 
2220  if (c->fc->nb_streams < 1) // will happen with jp2 files
2221  return 0;
2222  st = c->fc->streams[c->fc->nb_streams-1];
2223  if (atom.size < 2)
2224  return AVERROR_INVALIDDATA;
2225  mov_field_order = avio_rb16(pb);
2226  if ((mov_field_order & 0xFF00) == 0x0100)
2227  decoded_field_order = AV_FIELD_PROGRESSIVE;
2228  else if ((mov_field_order & 0xFF00) == 0x0200) {
2229  switch (mov_field_order & 0xFF) {
2230  case 0x01: decoded_field_order = AV_FIELD_TT;
2231  break;
2232  case 0x06: decoded_field_order = AV_FIELD_BB;
2233  break;
2234  case 0x09: decoded_field_order = AV_FIELD_TB;
2235  break;
2236  case 0x0E: decoded_field_order = AV_FIELD_BT;
2237  break;
2238  }
2239  }
2240  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
2241  av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
2242  }
2243  st->codecpar->field_order = decoded_field_order;
2244 
2245  return 0;
2246 }
2247 
2249 {
2250  int err = 0;
2251  uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
2252  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
2253  return AVERROR_INVALIDDATA;
2254  if ((err = av_reallocp(&par->extradata, size)) < 0) {
2255  par->extradata_size = 0;
2256  return err;
2257  }
2259  return 0;
2260 }
2261 
2262 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
2264  AVCodecParameters *par, uint8_t *buf)
2265 {
2266  int64_t result = atom.size;
2267  int err;
2268 
2269  AV_WB32(buf , atom.size + 8);
2270  AV_WL32(buf + 4, atom.type);
2271  err = ffio_read_size(pb, buf + 8, atom.size);
2272  if (err < 0) {
2273  par->extradata_size -= atom.size;
2274  return err;
2275  } else if (err < atom.size) {
2276  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
2277  par->extradata_size -= atom.size - err;
2278  result = err;
2279  }
2280  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2281  return result;
2282 }
2283 
2284 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
2286  enum AVCodecID codec_id)
2287 {
2288  AVStream *st;
2289  uint64_t original_size;
2290  int err;
2291 
2292  if (c->fc->nb_streams < 1) // will happen with jp2 files
2293  return 0;
2294  st = c->fc->streams[c->fc->nb_streams-1];
2295 
2296  if (st->codecpar->codec_id != codec_id)
2297  return 0; /* unexpected codec_id - don't mess with extradata */
2298 
2299  original_size = st->codecpar->extradata_size;
2300  err = mov_realloc_extradata(st->codecpar, atom);
2301  if (err)
2302  return err;
2303 
2304  err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
2305  if (err < 0)
2306  return err;
2307  return 0; // Note: this is the original behavior to ignore truncation.
2308 }
2309 
2310 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
2312 {
2313  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
2314 }
2315 
2317 {
2318  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_CAVS);
2319 }
2320 
2322 {
2323  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
2324 }
2325 
2327 {
2328  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
2329 }
2330 
2332 {
2333  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
2334  if (!ret)
2335  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
2336  return ret;
2337 }
2338 
2340 {
2341  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
2342 
2343  if (!ret && c->fc->nb_streams >= 1) {
2344  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
2345  if (par->extradata_size >= 40) {
2346  par->height = AV_RB16(&par->extradata[36]);
2347  par->width = AV_RB16(&par->extradata[38]);
2348  }
2349  }
2350  return ret;
2351 }
2352 
2354 {
2355  if (c->fc->nb_streams >= 1) {
2356  AVStream *const st = c->fc->streams[c->fc->nb_streams - 1];
2357  FFStream *const sti = ffstream(st);
2358  AVCodecParameters *par = st->codecpar;
2359 
2360  if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
2361  par->codec_id == AV_CODEC_ID_H264 &&
2362  atom.size > 11) {
2363  int cid;
2364  avio_skip(pb, 10);
2365  cid = avio_rb16(pb);
2366  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
2367  if (cid == 0xd4d || cid == 0xd4e)
2368  par->width = 1440;
2369  return 0;
2370  } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
2371  par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
2372  par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
2373  atom.size >= 24) {
2374  int num, den;
2375  avio_skip(pb, 12);
2376  num = avio_rb32(pb);
2377  den = avio_rb32(pb);
2378  if (num <= 0 || den <= 0)
2379  return 0;
2380  switch (avio_rb32(pb)) {
2381  case 2:
2382  if (den >= INT_MAX / 2)
2383  return 0;
2384  den *= 2;
2386  case 1:
2387  sti->display_aspect_ratio = (AVRational){ num, den };
2389  default:
2390  return 0;
2391  }
2392  }
2393  }
2394 
2395  return mov_read_avid(c, pb, atom);
2396 }
2397 
2399 {
2400  int ret = 0;
2401  int length = 0;
2402  uint64_t original_size;
2403  if (c->fc->nb_streams >= 1) {
2404  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
2405  if (par->codec_id == AV_CODEC_ID_H264)
2406  return 0;
2407  if (atom.size == 16) {
2408  original_size = par->extradata_size;
2409  ret = mov_realloc_extradata(par, atom);
2410  if (!ret) {
2411  length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
2412  if (length == atom.size) {
2413  const uint8_t range_value = par->extradata[original_size + 19];
2414  switch (range_value) {
2415  case 1:
2417  break;
2418  case 2:
2420  break;
2421  default:
2422  av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
2423  break;
2424  }
2425  ff_dlog(c->fc, "color_range: %d\n", par->color_range);
2426  } else {
2427  /* For some reason the whole atom was not added to the extradata */
2428  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
2429  }
2430  } else {
2431  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
2432  }
2433  } else {
2434  av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
2435  }
2436  }
2437 
2438  return ret;
2439 }
2440 
2442 {
2443  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
2444 }
2445 
2447 {
2448  AVStream *st;
2449  int ret;
2450 
2451  if (c->fc->nb_streams < 1)
2452  return 0;
2453  st = c->fc->streams[c->fc->nb_streams-1];
2454 
2455  if ((uint64_t)atom.size > (1<<30))
2456  return AVERROR_INVALIDDATA;
2457 
2458  if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
2461  // pass all frma atom to codec, needed at least for QDMC and QDM2
2462  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
2463  if (ret < 0)
2464  return ret;
2465  } else if (atom.size > 8) { /* to read frma, esds atoms */
2466  if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
2467  uint64_t buffer;
2468  ret = ffio_ensure_seekback(pb, 8);
2469  if (ret < 0)
2470  return ret;
2471  buffer = avio_rb64(pb);
2472  atom.size -= 8;
2473  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
2474  && buffer >> 32 <= atom.size
2475  && buffer >> 32 >= 8) {
2476  avio_skip(pb, -8);
2477  atom.size += 8;
2478  } else if (!st->codecpar->extradata_size) {
2479 #define ALAC_EXTRADATA_SIZE 36
2481  if (!st->codecpar->extradata)
2482  return AVERROR(ENOMEM);
2485  AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
2486  AV_WB64(st->codecpar->extradata + 12, buffer);
2487  avio_read(pb, st->codecpar->extradata + 20, 16);
2488  avio_skip(pb, atom.size - 24);
2489  return 0;
2490  }
2491  }
2492  if ((ret = mov_read_default(c, pb, atom)) < 0)
2493  return ret;
2494  } else
2495  avio_skip(pb, atom.size);
2496  return 0;
2497 }
2498 
2499 /**
2500  * This function reads atom content and puts data in extradata without tag
2501  * nor size unlike mov_read_extradata.
2502  */
2504 {
2505  AVStream *st;
2506  int ret;
2507 
2508  st = get_curr_st(c);
2509  if (!st)
2510  return 0;
2511 
2512  if ((uint64_t)atom.size > (1<<30))
2513  return AVERROR_INVALIDDATA;
2514 
2515  if (atom.type == MKTAG('v','v','c','C')) {
2516  avio_skip(pb, 4);
2517  atom.size -= 4;
2518  }
2519 
2520  if (atom.size >= 10) {
2521  // Broken files created by legacy versions of libavformat will
2522  // wrap a whole fiel atom inside of a glbl atom.
2523  unsigned size = avio_rb32(pb);
2524  unsigned type = avio_rl32(pb);
2525  if (avio_feof(pb))
2526  return AVERROR_INVALIDDATA;
2527  avio_seek(pb, -8, SEEK_CUR);
2528  if (type == MKTAG('f','i','e','l') && size == atom.size)
2529  return mov_read_default(c, pb, atom);
2530  }
2531  if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
2532  av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
2533  return 0;
2534  }
2535  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
2536  if (ret < 0)
2537  return ret;
2538  if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
2539  /* HEVC-based Dolby Vision derived from hvc1.
2540  Happens to match with an identifier
2541  previously utilized for DV. Thus, if we have
2542  the hvcC extradata box available as specified,
2543  set codec to HEVC */
2545 
2546  return 0;
2547 }
2548 
2550 {
2551  AVStream *st;
2552  uint8_t profile_level;
2553  int ret;
2554 
2555  if (c->fc->nb_streams < 1)
2556  return 0;
2557  st = c->fc->streams[c->fc->nb_streams-1];
2558 
2559  if (atom.size >= (1<<28) || atom.size < 7)
2560  return AVERROR_INVALIDDATA;
2561 
2562  profile_level = avio_r8(pb);
2563  if ((profile_level & 0xf0) != 0xc0)
2564  return 0;
2565 
2566  avio_seek(pb, 6, SEEK_CUR);
2567  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
2568  if (ret < 0)
2569  return ret;
2570 
2571  return 0;
2572 }
2573 
2574 static int mov_find_tref_id(const MovTref *tag, uint32_t id)
2575 {
2576  for (int i = 0; i < tag->nb_id; i++) {
2577  if (tag->id[i] == id)
2578  return 1;
2579  }
2580  return 0;
2581 }
2582 
2583 static int mov_add_tref_id(MovTref *tag, uint32_t id)
2584 {
2585  int ret = mov_find_tref_id(tag, id);
2586 
2587  if (!ret) {
2588  uint32_t *tmp = av_realloc_array(tag->id, tag->nb_id + 1, sizeof(*tag->id));
2589  if (!tmp)
2590  return AVERROR(ENOMEM);
2591  tag->id = tmp;
2592  tag->id[tag->nb_id++] = id;
2593  }
2594 
2595  return 0;
2596 }
2597 
2598 static MovTref *mov_find_tref_tag(const MOVStreamContext *sc, uint32_t name)
2599 {
2600  for (int i = 0; i < sc->nb_tref_tags; i++) {
2601  MovTref *entry = &sc->tref_tags[i];
2602 
2603  if (entry->name == name)
2604  return entry;
2605  }
2606  return NULL;
2607 }
2608 
2610 {
2612 
2613  if (!tag) {
2615  sizeof(*sc->tref_tags));
2616  if (!tmp)
2617  return NULL;
2618  sc->tref_tags = tmp;
2619  tag = &sc->tref_tags[sc->nb_tref_tags++];
2620  *tag = (MovTref){ .name = name };
2621  }
2622 
2623  return tag;
2624 }
2625 
2627 {
2628  AVStream* st;
2629  MOVStreamContext* sc;
2630 
2631  if (c->fc->nb_streams < 1)
2632  return 0;
2633 
2634  /* For SBAS this should be fine - though beware if someone implements a
2635  * tref atom processor that doesn't drop down to default then this may
2636  * be lost. */
2637  if (atom.size > 4) {
2638  av_log(c->fc, AV_LOG_ERROR, "Only a single tref of type sbas is supported\n");
2639  return AVERROR_PATCHWELCOME;
2640  }
2641  if (atom.size < 4)
2642  return AVERROR_INVALIDDATA;
2643 
2644  st = c->fc->streams[c->fc->nb_streams - 1];
2645  sc = st->priv_data;
2646 
2647  MovTref *tag = mov_add_tref_tag(sc, atom.type);
2648  if (!tag)
2649  return AVERROR(ENOMEM);
2650 
2651  int ret = mov_add_tref_id(tag, avio_rb32(pb));
2652  if (ret < 0)
2653  return ret;
2654 
2655  return 0;
2656 }
2657 
2659 {
2660  AVStream* st;
2661  MOVStreamContext* sc;
2662 
2663  if (c->fc->nb_streams < 1)
2664  return 0;
2665 
2666  if (atom.size > 4)
2667  av_log(c->fc, AV_LOG_WARNING, "More than one vdep reference is not supported.\n");
2668  if (atom.size < 4)
2669  return AVERROR_INVALIDDATA;
2670 
2671  st = c->fc->streams[c->fc->nb_streams - 1];
2672  sc = st->priv_data;
2673 
2674  MovTref *tag = mov_add_tref_tag(sc, atom.type);
2675  if (!tag)
2676  return AVERROR(ENOMEM);
2677 
2678  int ret = mov_add_tref_id(tag, avio_rb32(pb));
2679  if (ret < 0)
2680  return ret;
2681 
2682  return 0;
2683 }
2684 
2685 /**
2686  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
2687  * but can have extradata appended at the end after the 40 bytes belonging
2688  * to the struct.
2689  */
2691 {
2692  AVStream *st;
2693  int ret;
2694 
2695  if (c->fc->nb_streams < 1)
2696  return 0;
2697  if (atom.size <= 40)
2698  return 0;
2699  st = c->fc->streams[c->fc->nb_streams-1];
2700 
2701  if ((uint64_t)atom.size > (1<<30))
2702  return AVERROR_INVALIDDATA;
2703 
2704  avio_skip(pb, 40);
2705  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
2706  if (ret < 0)
2707  return ret;
2708 
2709  return 0;
2710 }
2711 
2713 {
2714  AVStream *st;
2715  MOVStreamContext *sc;
2716  unsigned int i, entries;
2717 
2718  if (c->trak_index < 0) {
2719  av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
2720  return 0;
2721  }
2722  if (c->fc->nb_streams < 1)
2723  return 0;
2724  st = c->fc->streams[c->fc->nb_streams-1];
2725  sc = st->priv_data;
2726 
2727  avio_r8(pb); /* version */
2728  avio_rb24(pb); /* flags */
2729 
2730  // Clamp allocation size for `chunk_offsets` -- don't throw an error for an
2731  // invalid count since the EOF path doesn't throw either.
2732  entries = avio_rb32(pb);
2733  entries =
2734  FFMIN(entries,
2735  FFMAX(0, (atom.size - 8) /
2736  (atom.type == MKTAG('s', 't', 'c', 'o') ? 4 : 8)));
2737 
2738  if (!entries)
2739  return 0;
2740 
2741  if (sc->chunk_offsets) {
2742  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STCO atom\n");
2743  return 0;
2744  }
2745 
2746  av_free(sc->chunk_offsets);
2747  sc->chunk_count = 0;
2748  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2749  if (!sc->chunk_offsets)
2750  return AVERROR(ENOMEM);
2751  sc->chunk_count = entries;
2752 
2753  if (atom.type == MKTAG('s','t','c','o'))
2754  for (i = 0; i < entries && !pb->eof_reached; i++)
2755  sc->chunk_offsets[i] = avio_rb32(pb);
2756  else if (atom.type == MKTAG('c','o','6','4'))
2757  for (i = 0; i < entries && !pb->eof_reached; i++) {
2758  sc->chunk_offsets[i] = avio_rb64(pb);
2759  if (sc->chunk_offsets[i] < 0) {
2760  av_log(c->fc, AV_LOG_WARNING, "Impossible chunk_offset\n");
2761  sc->chunk_offsets[i] = 0;
2762  }
2763  }
2764  else
2765  return AVERROR_INVALIDDATA;
2766 
2767  sc->chunk_count = i;
2768 
2769  if (pb->eof_reached) {
2770  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
2771  return AVERROR_EOF;
2772  }
2773 
2774  return 0;
2775 }
2776 
2777 static int mov_codec_id(AVStream *st, uint32_t format)
2778 {
2780 
2781  if (id <= 0 &&
2782  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
2783  (format & 0xFFFF) == 'T' + ('S' << 8)))
2785 
2786  if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
2788  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
2789  /* skip old ASF MPEG-4 tag */
2790  format && format != MKTAG('m','p','4','s')) {
2792  if (id <= 0)
2794  if (id > 0)
2796  else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
2798  st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
2800  if (id <= 0) {
2802  AV_CODEC_ID_TTML : id;
2803  }
2804 
2805  if (id > 0)
2807  else
2809  }
2810  }
2811 
2812  st->codecpar->codec_tag = format;
2813 
2814  return id;
2815 }
2816 
2818  AVStream *st, MOVStreamContext *sc)
2819 {
2820  uint8_t codec_name[32] = { 0 };
2821  int64_t stsd_start;
2822  unsigned int len;
2823  uint32_t id = 0;
2824 
2825  /* The first 16 bytes of the video sample description are already
2826  * read in ff_mov_read_stsd_entries() */
2827  stsd_start = avio_tell(pb) - 16;
2828 
2829  if (c->isom) {
2830  avio_skip(pb, 2); /* pre_defined */
2831  avio_skip(pb, 2); /* reserved */
2832  avio_skip(pb, 12); /* pre_defined */
2833  } else {
2834  avio_rb16(pb); /* version */
2835  avio_rb16(pb); /* revision level */
2836  id = avio_rl32(pb); /* vendor */
2837  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2838  avio_rb32(pb); /* temporal quality */
2839  avio_rb32(pb); /* spatial quality */
2840  }
2841 
2842  st->codecpar->width = avio_rb16(pb); /* width */
2843  st->codecpar->height = avio_rb16(pb); /* height */
2844 
2845  avio_rb32(pb); /* horiz resolution */
2846  avio_rb32(pb); /* vert resolution */
2847  avio_rb32(pb); /* data size, always 0 */
2848  avio_rb16(pb); /* frames per samples */
2849 
2850  len = avio_r8(pb); /* codec name, pascal string */
2851  if (len > 31)
2852  len = 31;
2853  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
2854  if (len < 31)
2855  avio_skip(pb, 31 - len);
2856 
2857  if (codec_name[0])
2858  av_dict_set(&st->metadata, "encoder", codec_name, 0);
2859 
2860  /* codec_tag YV12 triggers an UV swap in rawdec.c */
2861  if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
2862  st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
2863  st->codecpar->width &= ~1;
2864  st->codecpar->height &= ~1;
2865  }
2866  /* Flash Media Server uses tag H.263 with Sorenson Spark */
2867  if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
2868  !strncmp(codec_name, "Sorenson H263", 13))
2870 
2871  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
2872 
2873  avio_seek(pb, stsd_start, SEEK_SET);
2874 
2875  if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2876  st->codecpar->bits_per_coded_sample &= 0x1F;
2877  sc->has_palette = 1;
2878  }
2879 }
2880 
2882  AVStream *st, MOVStreamContext *sc)
2883 {
2884  int bits_per_sample, flags;
2885  uint16_t version = avio_rb16(pb);
2886  uint32_t id = 0;
2887  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
2888  int channel_count;
2889 
2890  if (c->isom)
2891  avio_skip(pb, 6); /* reserved */
2892  else {
2893  avio_rb16(pb); /* revision level */
2894  id = avio_rl32(pb); /* vendor */
2895  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2896  }
2897 
2898  channel_count = avio_rb16(pb);
2899 
2901  st->codecpar->ch_layout.nb_channels = channel_count;
2902  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
2903  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", channel_count);
2904 
2905  sc->audio_cid = avio_rb16(pb);
2906  avio_rb16(pb); /* packet size = 0 */
2907 
2908  st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
2909 
2910  // Read QT version 1 fields. In version 0 these do not exist.
2911  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
2912  if (!c->isom ||
2913  (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
2914  (sc->stsd_version == 0 && version > 0)) {
2915  if (version == 1) {
2916  sc->samples_per_frame = avio_rb32(pb);
2917  avio_rb32(pb); /* bytes per packet */
2918  sc->bytes_per_frame = avio_rb32(pb);
2919  avio_rb32(pb); /* bytes per sample */
2920  } else if (version == 2) {
2921  avio_rb32(pb); /* sizeof struct only */
2923  channel_count = avio_rb32(pb);
2925  st->codecpar->ch_layout.nb_channels = channel_count;
2926  avio_rb32(pb); /* always 0x7F000000 */
2928 
2929  flags = avio_rb32(pb); /* lpcm format specific flag */
2930  sc->bytes_per_frame = avio_rb32(pb);
2931  sc->samples_per_frame = avio_rb32(pb);
2932  if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
2933  st->codecpar->codec_id =
2935  flags);
2936  }
2937  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2938  /* can't correctly handle variable sized packet as audio unit */
2939  switch (st->codecpar->codec_id) {
2940  case AV_CODEC_ID_MP2:
2941  case AV_CODEC_ID_MP3:
2943  break;
2944  }
2945  }
2946  }
2947 
2948  if (sc->format == 0) {
2949  if (st->codecpar->bits_per_coded_sample == 8)
2950  st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
2951  else if (st->codecpar->bits_per_coded_sample == 16)
2952  st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
2953  }
2954 
2955  switch (st->codecpar->codec_id) {
2956  case AV_CODEC_ID_PCM_S8:
2957  case AV_CODEC_ID_PCM_U8:
2958  if (st->codecpar->bits_per_coded_sample == 16)
2960  break;
2961  case AV_CODEC_ID_PCM_S16LE:
2962  case AV_CODEC_ID_PCM_S16BE:
2963  if (st->codecpar->bits_per_coded_sample == 8)
2965  else if (st->codecpar->bits_per_coded_sample == 24)
2966  st->codecpar->codec_id =
2969  else if (st->codecpar->bits_per_coded_sample == 32)
2970  st->codecpar->codec_id =
2973  break;
2974  /* set values for old format before stsd version 1 appeared */
2975  case AV_CODEC_ID_MACE3:
2976  sc->samples_per_frame = 6;
2978  break;
2979  case AV_CODEC_ID_MACE6:
2980  sc->samples_per_frame = 6;
2982  break;
2984  sc->samples_per_frame = 64;
2986  break;
2987  case AV_CODEC_ID_GSM:
2988  sc->samples_per_frame = 160;
2989  sc->bytes_per_frame = 33;
2990  break;
2991  default:
2992  break;
2993  }
2994 
2995  bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2996  if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->ch_layout.nb_channels <= INT_MAX) {
2997  st->codecpar->bits_per_coded_sample = bits_per_sample;
2998  sc->sample_size = (bits_per_sample >> 3) * st->codecpar->ch_layout.nb_channels;
2999  }
3000 }
3001 
3003  AVStream *st, MOVStreamContext *sc,
3004  int64_t size)
3005 {
3006  // ttxt stsd contains display flags, justification, background
3007  // color, fonts, and default styles, so fake an atom to read it
3008  MOVAtom fake_atom = { .size = size };
3009  // mp4s contains a regular esds atom, dfxp ISMV TTML has no content
3010  // in extradata unlike stpp MP4 TTML.
3011  if (st->codecpar->codec_tag != AV_RL32("mp4s") &&
3013  mov_read_glbl(c, pb, fake_atom);
3014  st->codecpar->width = sc->width;
3015  st->codecpar->height = sc->height;
3016 }
3017 
3019  AVStream *st, MOVStreamContext *sc,
3020  int64_t size)
3021 {
3022  int ret;
3023 
3024  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
3025  if ((int)size != size)
3026  return AVERROR(ENOMEM);
3027 
3028  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
3029  if (ret < 0)
3030  return ret;
3031  if (size > 16) {
3032  MOVStreamContext *tmcd_ctx = st->priv_data;
3033  int val;
3034  val = AV_RB32(st->codecpar->extradata + 4);
3035  tmcd_ctx->tmcd_flags = val;
3036  st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
3037  st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
3038  tmcd_ctx->tmcd_nb_frames = st->codecpar->extradata[16]; /* number of frames */
3039  if (size > 30) {
3040  uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
3041  uint32_t format = AV_RB32(st->codecpar->extradata + 22);
3042  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
3043  uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
3044  if (str_size > 0 && size >= (int)str_size + 30 &&
3045  st->codecpar->extradata[30] /* Don't add empty string */) {
3046  char *reel_name = av_malloc(str_size + 1);
3047  if (!reel_name)
3048  return AVERROR(ENOMEM);
3049  memcpy(reel_name, st->codecpar->extradata + 30, str_size);
3050  reel_name[str_size] = 0; /* Add null terminator */
3051  av_dict_set(&st->metadata, "reel_name", reel_name,
3053  }
3054  }
3055  }
3056  }
3057  } else {
3058  /* other codec type, just skip (rtp, mp4s ...) */
3059  avio_skip(pb, size);
3060  }
3061  return 0;
3062 }
3063 
3065  AVStream *st, MOVStreamContext *sc)
3066 {
3067  FFStream *const sti = ffstream(st);
3068 
3069  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3070  !st->codecpar->sample_rate && sc->time_scale > 1)
3071  st->codecpar->sample_rate = sc->time_scale;
3072 
3073  /* special codec parameters handling */
3074  switch (st->codecpar->codec_id) {
3075 #if CONFIG_DV_DEMUXER
3076  case AV_CODEC_ID_DVAUDIO:
3077  if (c->dv_fctx) {
3078  avpriv_request_sample(c->fc, "multiple DV audio streams");
3079  return AVERROR(ENOSYS);
3080  }
3081 
3082  c->dv_fctx = avformat_alloc_context();
3083  if (!c->dv_fctx) {
3084  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
3085  return AVERROR(ENOMEM);
3086  }
3087  c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
3088  if (!c->dv_demux) {
3089  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
3090  return AVERROR(ENOMEM);
3091  }
3092  sc->dv_audio_container = 1;
3094  break;
3095 #endif
3096  /* no ifdef since parameters are always those */
3097  case AV_CODEC_ID_QCELP:
3100  // force sample rate for qcelp when not stored in mov
3101  if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
3102  st->codecpar->sample_rate = 8000;
3103  // FIXME: Why is the following needed for some files?
3104  sc->samples_per_frame = 160;
3105  if (!sc->bytes_per_frame)
3106  sc->bytes_per_frame = 35;
3107  break;
3108  case AV_CODEC_ID_AMR_NB:
3111  /* force sample rate for amr, stsd in 3gp does not store sample rate */
3112  st->codecpar->sample_rate = 8000;
3113  break;
3114  case AV_CODEC_ID_AMR_WB:
3117  st->codecpar->sample_rate = 16000;
3118  break;
3119  case AV_CODEC_ID_MP2:
3120  case AV_CODEC_ID_MP3:
3121  /* force type after stsd for m1a hdlr */
3123  break;
3124  case AV_CODEC_ID_GSM:
3125  case AV_CODEC_ID_ADPCM_MS:
3127  case AV_CODEC_ID_ILBC:
3128  case AV_CODEC_ID_MACE3:
3129  case AV_CODEC_ID_MACE6:
3130  case AV_CODEC_ID_QDM2:
3132  break;
3133  case AV_CODEC_ID_ALAC:
3134  if (st->codecpar->extradata_size == 36) {
3135  int channel_count = AV_RB8(st->codecpar->extradata + 21);
3136  if (st->codecpar->ch_layout.nb_channels != channel_count) {
3139  st->codecpar->ch_layout.nb_channels = channel_count;
3140  }
3141  st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
3142  }
3143  break;
3144  case AV_CODEC_ID_AC3:
3145  case AV_CODEC_ID_EAC3:
3147  case AV_CODEC_ID_VC1:
3148  case AV_CODEC_ID_VP8:
3149  case AV_CODEC_ID_VP9:
3151  break;
3153  case AV_CODEC_ID_PRORES:
3154  case AV_CODEC_ID_APV:
3155  case AV_CODEC_ID_EVC:
3156  case AV_CODEC_ID_LCEVC:
3157  case AV_CODEC_ID_AV1:
3158  /* field_order detection of H264 requires parsing */
3159  case AV_CODEC_ID_H264:
3161  break;
3162  default:
3163  break;
3164  }
3165  return 0;
3166 }
3167 
3169  int codec_tag, int format,
3170  int64_t size)
3171 {
3172  if (codec_tag &&
3173  (codec_tag != format &&
3174  // AVID 1:1 samples with differing data format and codec tag exist
3175  (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
3176  // prores is allowed to have differing data format and codec tag
3177  codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
3178  // so is dv (sigh)
3179  codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
3180  (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
3181  : codec_tag != MKTAG('j','p','e','g')))) {
3182  /* Multiple fourcc, we skip JPEG. This is not correct, we should
3183  * export it as a separate AVStream but this needs a few changes
3184  * in the MOV demuxer, patch welcome. */
3185 
3186  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
3187  avio_skip(pb, size);
3188  return 1;
3189  }
3190 
3191  return 0;
3192 }
3193 
3195 {
3196  int ret;
3197 
3198  /* special codec parameters handling */
3199  switch (st->codecpar->codec_id) {
3200  case AV_CODEC_ID_H264:
3201  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
3202  if (!st->codecpar->extradata_size && TAG_IS_AVCI(st->codecpar->codec_tag)) {
3204  if (ret < 0)
3205  return ret;
3206  }
3207  break;
3208  default:
3209  break;
3210  }
3211 
3212  return 0;
3213 }
3214 
3216 {
3217  AVStream *st;
3218  MOVStreamContext *sc;
3219  int pseudo_stream_id;
3220 
3221  av_assert0 (c->fc->nb_streams >= 1);
3222  st = c->fc->streams[c->fc->nb_streams-1];
3223  sc = st->priv_data;
3224 
3225  for (pseudo_stream_id = 0;
3226  pseudo_stream_id < entries && !pb->eof_reached;
3227  pseudo_stream_id++) {
3228  //Parsing Sample description table
3229  enum AVCodecID id;
3230  int ret, dref_id = 1;
3231  MOVAtom a = { AV_RL32("stsd") };
3232  int64_t start_pos = avio_tell(pb);
3233  int64_t size = avio_rb32(pb); /* size */
3234  uint32_t format = avio_rl32(pb); /* data format */
3235 
3236  if (size >= 16) {
3237  avio_rb32(pb); /* reserved */
3238  avio_rb16(pb); /* reserved */
3239  dref_id = avio_rb16(pb);
3240  } else if (size <= 7) {
3241  av_log(c->fc, AV_LOG_ERROR,
3242  "invalid size %"PRId64" in stsd\n", size);
3243  return AVERROR_INVALIDDATA;
3244  }
3245 
3247  size - (avio_tell(pb) - start_pos))) {
3248  sc->stsd_count++;
3249  continue;
3250  }
3251 
3252  sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
3253  sc->dref_id= dref_id;
3254  sc->format = format;
3255 
3256  id = mov_codec_id(st, format);
3257 
3258  av_log(c->fc, AV_LOG_TRACE,
3259  "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
3261 
3262  st->codecpar->codec_id = id;
3264  mov_parse_stsd_video(c, pb, st, sc);
3265  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
3266  mov_parse_stsd_audio(c, pb, st, sc);
3267  if (st->codecpar->sample_rate < 0) {
3268  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
3269  return AVERROR_INVALIDDATA;
3270  }
3271  if (st->codecpar->ch_layout.nb_channels < 0) {
3272  av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->ch_layout.nb_channels);
3273  return AVERROR_INVALIDDATA;
3274  }
3275  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
3276  mov_parse_stsd_subtitle(c, pb, st, sc,
3277  size - (avio_tell(pb) - start_pos));
3278  } else {
3279  ret = mov_parse_stsd_data(c, pb, st, sc,
3280  size - (avio_tell(pb) - start_pos));
3281  if (ret < 0)
3282  return ret;
3283  }
3284  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
3285  a.size = size - (avio_tell(pb) - start_pos);
3286  if (a.size > 8) {
3287  if ((ret = mov_read_default(c, pb, a)) < 0)
3288  return ret;
3289  } else if (a.size > 0)
3290  avio_skip(pb, a.size);
3291 
3292  ret = mov_finalize_stsd_entry(c, st);
3293  if (ret < 0)
3294  return ret;
3295 
3296  if (sc->extradata && st->codecpar->extradata) {
3297  int extra_size = st->codecpar->extradata_size;
3298 
3299  /* Move the current stream extradata to the stream context one. */
3300  sc->extradata_size[pseudo_stream_id] = extra_size;
3301  sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
3302  st->codecpar->extradata = NULL;
3303  st->codecpar->extradata_size = 0;
3304  }
3305  sc->stsd_count++;
3306  }
3307 
3308  if (pb->eof_reached) {
3309  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
3310  return AVERROR_EOF;
3311  }
3312 
3313  return 0;
3314 }
3315 
3317 {
3318  AVStream *st;
3319  MOVStreamContext *sc;
3320  int ret, entries;
3321 
3322  if (c->fc->nb_streams < 1)
3323  return 0;
3324  st = c->fc->streams[c->fc->nb_streams - 1];
3325  sc = st->priv_data;
3326 
3327  if (sc->extradata) {
3328  av_log(c->fc, AV_LOG_ERROR,
3329  "Duplicate stsd found in this track.\n");
3330  return AVERROR_INVALIDDATA;
3331  }
3332 
3333  sc->stsd_version = avio_r8(pb);
3334  avio_rb24(pb); /* flags */
3335  entries = avio_rb32(pb);
3336 
3337  /* Each entry contains a size (4 bytes) and format (4 bytes). */
3338  if (entries <= 0 || entries > atom.size / 8 || entries > 1024) {
3339  av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
3340  return AVERROR_INVALIDDATA;
3341  }
3342 
3343  /* Prepare space for hosting multiple extradata. */
3344  sc->extradata = av_calloc(entries, sizeof(*sc->extradata));
3345  if (!sc->extradata)
3346  return AVERROR(ENOMEM);
3347 
3348  sc->extradata_size = av_calloc(entries, sizeof(*sc->extradata_size));
3349  if (!sc->extradata_size) {
3350  ret = AVERROR(ENOMEM);
3351  goto fail;
3352  }
3353 
3354  ret = ff_mov_read_stsd_entries(c, pb, entries);
3355  if (ret < 0)
3356  goto fail;
3357 
3358  /* Restore back the primary extradata. */
3359  av_freep(&st->codecpar->extradata);
3360  st->codecpar->extradata_size = sc->extradata_size[0];
3361  if (sc->extradata_size[0]) {
3363  if (!st->codecpar->extradata)
3364  return AVERROR(ENOMEM);
3365  memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
3366  }
3367 
3368  return mov_finalize_stsd_codec(c, pb, st, sc);
3369 fail:
3370  if (sc->extradata) {
3371  int j;
3372  for (j = 0; j < sc->stsd_count; j++)
3373  av_freep(&sc->extradata[j]);
3374  }
3375 
3376  sc->stsd_count = 0;
3377  av_freep(&sc->extradata);
3378  av_freep(&sc->extradata_size);
3379  return ret;
3380 }
3381 
3383 {
3384  AVStream *st;
3385  MOVStreamContext *sc;
3386  unsigned int i, entries;
3387 
3388  if (c->trak_index < 0) {
3389  av_log(c->fc, AV_LOG_WARNING, "STSC outside TRAK\n");
3390  return 0;
3391  }
3392 
3393  if (c->fc->nb_streams < 1)
3394  return 0;
3395  st = c->fc->streams[c->fc->nb_streams-1];
3396  sc = st->priv_data;
3397 
3398  avio_r8(pb); /* version */
3399  avio_rb24(pb); /* flags */
3400 
3401  entries = avio_rb32(pb);
3402  if ((uint64_t)entries * 12 + 4 > atom.size)
3403  return AVERROR_INVALIDDATA;
3404 
3405  av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
3406 
3407  if (!entries)
3408  return 0;
3409  if (sc->stsc_data) {
3410  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STSC atom\n");
3411  return 0;
3412  }
3413  av_free(sc->stsc_data);
3414  sc->stsc_count = 0;
3415  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
3416  if (!sc->stsc_data)
3417  return AVERROR(ENOMEM);
3418 
3419  for (i = 0; i < entries && !pb->eof_reached; i++) {
3420  sc->stsc_data[i].first = avio_rb32(pb);
3421  sc->stsc_data[i].count = avio_rb32(pb);
3422  sc->stsc_data[i].id = avio_rb32(pb);
3423  }
3424 
3425  sc->stsc_count = i;
3426  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
3427  int64_t first_min = i + 1;
3428  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
3429  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
3430  sc->stsc_data[i].first < first_min ||
3431  sc->stsc_data[i].count < 1 ||
3432  sc->stsc_data[i].id < 1) {
3433  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);
3434  if (i+1 >= sc->stsc_count) {
3435  if (sc->stsc_data[i].count == 0 && i > 0) {
3436  sc->stsc_count --;
3437  continue;
3438  }
3439  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
3440  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
3441  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
3442  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
3443  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
3444  continue;
3445  }
3446  av_assert0(sc->stsc_data[i+1].first >= 2);
3447  // We replace this entry by the next valid
3448  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
3449  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
3450  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
3451  }
3452  }
3453 
3454  if (pb->eof_reached) {
3455  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
3456  return AVERROR_EOF;
3457  }
3458 
3459  return 0;
3460 }
3461 
3462 static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
3463 {
3464  return index < count - 1;
3465 }
3466 
3467 /* Compute the samples value for the stsc entry at the given index. */
3468 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
3469 {
3470  int chunk_count;
3471 
3473  chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
3474  else {
3475  // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
3477  chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
3478  }
3479 
3480  return sc->stsc_data[index].count * (int64_t)chunk_count;
3481 }
3482 
3484 {
3485  AVStream *st;
3486  MOVStreamContext *sc;
3487  unsigned i, entries;
3488 
3489  if (c->trak_index < 0) {
3490  av_log(c->fc, AV_LOG_WARNING, "STPS outside TRAK\n");
3491  return 0;
3492  }
3493 
3494  if (c->fc->nb_streams < 1)
3495  return 0;
3496  st = c->fc->streams[c->fc->nb_streams-1];
3497  sc = st->priv_data;
3498 
3499  avio_rb32(pb); // version + flags
3500 
3501  entries = avio_rb32(pb);
3502  if (sc->stps_data)
3503  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
3504  av_free(sc->stps_data);
3505  sc->stps_count = 0;
3506  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
3507  if (!sc->stps_data)
3508  return AVERROR(ENOMEM);
3509 
3510  for (i = 0; i < entries && !pb->eof_reached; i++) {
3511  sc->stps_data[i] = avio_rb32(pb);
3512  }
3513 
3514  sc->stps_count = i;
3515 
3516  if (pb->eof_reached) {
3517  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
3518  return AVERROR_EOF;
3519  }
3520 
3521  return 0;
3522 }
3523 
3525 {
3526  AVStream *st;
3527  FFStream *sti;
3528  MOVStreamContext *sc;
3529  unsigned int i, entries;
3530 
3531  if (c->trak_index < 0) {
3532  av_log(c->fc, AV_LOG_WARNING, "STSS outside TRAK\n");
3533  return 0;
3534  }
3535 
3536  if (c->fc->nb_streams < 1)
3537  return 0;
3538  st = c->fc->streams[c->fc->nb_streams-1];
3539  sti = ffstream(st);
3540  sc = st->priv_data;
3541 
3542  avio_r8(pb); /* version */
3543  avio_rb24(pb); /* flags */
3544 
3545  entries = avio_rb32(pb);
3546 
3547  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
3548 
3549  if (!entries) {
3550  sc->keyframe_absent = 1;
3551  if (!sti->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
3553  return 0;
3554  }
3555  if (sc->keyframes)
3556  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
3557  if (entries >= UINT_MAX / sizeof(int))
3558  return AVERROR_INVALIDDATA;
3559  av_freep(&sc->keyframes);
3560  sc->keyframe_count = 0;
3561  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
3562  if (!sc->keyframes)
3563  return AVERROR(ENOMEM);
3564 
3565  for (i = 0; i < entries && !pb->eof_reached; i++) {
3566  sc->keyframes[i] = avio_rb32(pb);
3567  }
3568 
3569  sc->keyframe_count = i;
3570 
3571  if (pb->eof_reached) {
3572  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
3573  return AVERROR_EOF;
3574  }
3575 
3576  return 0;
3577 }
3578 
3580 {
3581  AVStream *st;
3582  MOVStreamContext *sc;
3583  unsigned int i, entries, sample_size, field_size, num_bytes;
3584  GetBitContext gb;
3585  unsigned char* buf;
3586  int ret;
3587 
3588  if (c->trak_index < 0) {
3589  av_log(c->fc, AV_LOG_WARNING, "STSZ outside TRAK\n");
3590  return 0;
3591  }
3592 
3593  if (c->fc->nb_streams < 1)
3594  return 0;
3595  st = c->fc->streams[c->fc->nb_streams-1];
3596  sc = st->priv_data;
3597 
3598  avio_r8(pb); /* version */
3599  avio_rb24(pb); /* flags */
3600 
3601  if (atom.type == MKTAG('s','t','s','z')) {
3602  sample_size = avio_rb32(pb);
3603  if (!sc->sample_size) /* do not overwrite value computed in stsd */
3604  sc->sample_size = sample_size;
3605  sc->stsz_sample_size = sample_size;
3606  field_size = 32;
3607  } else {
3608  sample_size = 0;
3609  avio_rb24(pb); /* reserved */
3610  field_size = avio_r8(pb);
3611  }
3612  entries = avio_rb32(pb);
3613 
3614  av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
3615 
3616  sc->sample_count = entries;
3617  if (sample_size)
3618  return 0;
3619 
3620  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
3621  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
3622  return AVERROR_INVALIDDATA;
3623  }
3624 
3625  if (!entries)
3626  return 0;
3627  if (entries >= (INT_MAX - 4 - 8 * AV_INPUT_BUFFER_PADDING_SIZE) / field_size)
3628  return AVERROR_INVALIDDATA;
3629  if (sc->sample_sizes)
3630  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
3631  av_free(sc->sample_sizes);
3632  sc->sample_count = 0;
3633  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
3634  if (!sc->sample_sizes)
3635  return AVERROR(ENOMEM);
3636 
3637  num_bytes = (entries*field_size+4)>>3;
3638 
3639  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
3640  if (!buf) {
3641  av_freep(&sc->sample_sizes);
3642  return AVERROR(ENOMEM);
3643  }
3644 
3645  ret = ffio_read_size(pb, buf, num_bytes);
3646  if (ret < 0) {
3647  av_freep(&sc->sample_sizes);
3648  av_free(buf);
3649  av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
3650  return 0;
3651  }
3652 
3653  init_get_bits(&gb, buf, 8*num_bytes);
3654 
3655  for (i = 0; i < entries; i++) {
3656  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
3657  if (sc->sample_sizes[i] > INT64_MAX - sc->data_size) {
3658  av_free(buf);
3659  av_log(c->fc, AV_LOG_ERROR, "Sample size overflow in STSZ\n");
3660  return AVERROR_INVALIDDATA;
3661  }
3662  sc->data_size += sc->sample_sizes[i];
3663  }
3664 
3665  sc->sample_count = i;
3666 
3667  av_free(buf);
3668 
3669  return 0;
3670 }
3671 
3673 {
3674  AVStream *st;
3675  MOVStreamContext *sc;
3676  unsigned int i, entries;
3677  int64_t duration = 0;
3678  int64_t total_sample_count = 0;
3679  int64_t current_dts = 0;
3680  int64_t corrected_dts = 0;
3681 
3682  if (c->trak_index < 0) {
3683  av_log(c->fc, AV_LOG_WARNING, "STTS outside TRAK\n");
3684  return 0;
3685  }
3686 
3687  if (c->fc->nb_streams < 1)
3688  return 0;
3689  st = c->fc->streams[c->fc->nb_streams-1];
3690  sc = st->priv_data;
3691 
3692  avio_r8(pb); /* version */
3693  avio_rb24(pb); /* flags */
3694  entries = avio_rb32(pb);
3695 
3696  av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
3697  c->fc->nb_streams-1, entries);
3698 
3699  if (sc->stts_data)
3700  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
3701  av_freep(&sc->stts_data);
3702  sc->stts_count = 0;
3703  if (entries >= INT_MAX / sizeof(*sc->stts_data))
3704  return AVERROR(ENOMEM);
3705 
3706  for (i = 0; i < entries && !pb->eof_reached; i++) {
3707  unsigned int sample_duration;
3708  unsigned int sample_count;
3709  unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
3710  MOVStts *stts_data = av_fast_realloc(sc->stts_data, &sc->stts_allocated_size,
3711  min_entries * sizeof(*sc->stts_data));
3712  if (!stts_data) {
3713  av_freep(&sc->stts_data);
3714  sc->stts_count = 0;
3715  return AVERROR(ENOMEM);
3716  }
3717  sc->stts_count = min_entries;
3718  sc->stts_data = stts_data;
3719 
3720  sample_count = avio_rb32(pb);
3721  sample_duration = avio_rb32(pb);
3722 
3723  sc->stts_data[i].count= sample_count;
3724  sc->stts_data[i].duration= sample_duration;
3725 
3726  av_log(c->fc, AV_LOG_TRACE, "sample_count=%u, sample_duration=%u\n",
3727  sample_count, sample_duration);
3728 
3729  /* STTS sample offsets are uint32 but some files store it as int32
3730  * with negative values used to correct DTS delays.
3731  There may be abnormally large values as well. */
3732  if (sample_duration > c->max_stts_delta) {
3733  // assume high delta is a correction if negative when cast as int32
3734  int32_t delta_magnitude = (int32_t)sample_duration;
3735  av_log(c->fc, AV_LOG_WARNING, "Too large sample offset %u in stts entry %u with count %u in st:%d. Clipping to 1.\n",
3736  sample_duration, i, sample_count, st->index);
3737  sc->stts_data[i].duration = 1;
3738  corrected_dts = av_sat_add64(corrected_dts, (delta_magnitude < 0 ? (int64_t)delta_magnitude : 1) * sample_count);
3739  } else {
3740  corrected_dts += sample_duration * (uint64_t)sample_count;
3741  }
3742 
3743  current_dts += sc->stts_data[i].duration * (uint64_t)sample_count;
3744 
3745  if (current_dts > corrected_dts) {
3746  int64_t drift = av_sat_sub64(current_dts, corrected_dts) / FFMAX(sample_count, 1);
3747  uint32_t correction = (sc->stts_data[i].duration > drift) ? drift : sc->stts_data[i].duration - 1;
3748  current_dts -= correction * (uint64_t)sample_count;
3749  sc->stts_data[i].duration -= correction;
3750  }
3751 
3752  duration+=(int64_t)sc->stts_data[i].duration*(uint64_t)sc->stts_data[i].count;
3753  total_sample_count+=sc->stts_data[i].count;
3754  }
3755 
3756  sc->stts_count = i;
3757 
3758  if (duration > 0 &&
3759  duration <= INT64_MAX - sc->duration_for_fps &&
3760  total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
3761  sc->duration_for_fps += duration;
3762  sc->nb_frames_for_fps += total_sample_count;
3763  }
3764 
3765  if (pb->eof_reached) {
3766  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
3767  return AVERROR_EOF;
3768  }
3769 
3770  st->nb_frames= total_sample_count;
3771  if (duration)
3772  st->duration= FFMIN(st->duration, duration);
3773 
3774  // All samples have zero duration. They have higher chance be chose by
3775  // mov_find_next_sample, which leads to seek again and again.
3776  //
3777  // It's AVERROR_INVALIDDATA actually, but such files exist in the wild.
3778  // So only mark data stream as discarded for safety.
3779  if (!duration && sc->stts_count &&
3781  av_log(c->fc, AV_LOG_WARNING,
3782  "All samples in data stream index:id [%d:%d] have zero "
3783  "duration, stream set to be discarded by default. Override "
3784  "using AVStream->discard or -discard for ffmpeg command.\n",
3785  st->index, sc->id);
3786  st->discard = AVDISCARD_ALL;
3787  }
3788  sc->track_end = duration;
3789  return 0;
3790 }
3791 
3793 {
3794  AVStream *st;
3795  MOVStreamContext *sc;
3796  unsigned int i;
3797  int64_t entries;
3798 
3799  if (c->fc->nb_streams < 1)
3800  return 0;
3801  st = c->fc->streams[c->fc->nb_streams - 1];
3802  sc = st->priv_data;
3803 
3804  avio_r8(pb); /* version */
3805  avio_rb24(pb); /* flags */
3806  entries = atom.size - 4;
3807 
3808  av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
3809  c->fc->nb_streams - 1, entries);
3810 
3811  if (sc->sdtp_data)
3812  av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
3813  av_freep(&sc->sdtp_data);
3814  sc->sdtp_count = 0;
3815 
3816  if (entries < 0 || entries > UINT_MAX)
3817  return AVERROR(ERANGE);
3818 
3819  sc->sdtp_data = av_malloc(entries);
3820  if (!sc->sdtp_data)
3821  return AVERROR(ENOMEM);
3822 
3823  for (i = 0; i < entries && !pb->eof_reached; i++)
3824  sc->sdtp_data[i] = avio_r8(pb);
3825  sc->sdtp_count = i;
3826 
3827  return 0;
3828 }
3829 
3830 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3831 {
3832  if (duration < 0) {
3833  if (duration == INT_MIN) {
3834  av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
3835  duration++;
3836  }
3837  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3838  }
3839 }
3840 
3842 {
3843  AVStream *st;
3844  MOVStreamContext *sc;
3845  unsigned int i, entries, ctts_count = 0;
3846 
3847  if (c->trak_index < 0) {
3848  av_log(c->fc, AV_LOG_WARNING, "CTTS outside TRAK\n");
3849  return 0;
3850  }
3851 
3852  if (c->fc->nb_streams < 1)
3853  return 0;
3854  st = c->fc->streams[c->fc->nb_streams-1];
3855  sc = st->priv_data;
3856 
3857  avio_r8(pb); /* version */
3858  avio_rb24(pb); /* flags */
3859  entries = avio_rb32(pb);
3860 
3861  av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
3862 
3863  if (!entries)
3864  return 0;
3865  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3866  return AVERROR_INVALIDDATA;
3867  av_freep(&sc->ctts_data);
3868  sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3869  if (!sc->ctts_data)
3870  return AVERROR(ENOMEM);
3871 
3872  for (i = 0; i < entries && !pb->eof_reached; i++) {
3873  MOVCtts *ctts_data;
3874  const size_t min_size_needed = (ctts_count + 1) * sizeof(MOVCtts);
3875  const size_t requested_size =
3876  min_size_needed > sc->ctts_allocated_size ?
3877  FFMAX(min_size_needed, 2 * sc->ctts_allocated_size) :
3878  min_size_needed;
3879  int count = avio_rb32(pb);
3880  int duration = avio_rb32(pb);
3881 
3882  if (count <= 0) {
3883  av_log(c->fc, AV_LOG_TRACE,
3884  "ignoring CTTS entry with count=%d duration=%d\n",
3885  count, duration);
3886  continue;
3887  }
3888 
3889  if (ctts_count >= UINT_MAX / sizeof(MOVCtts) - 1)
3890  return AVERROR(ENOMEM);
3891 
3892  ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size, requested_size);
3893 
3894  if (!ctts_data)
3895  return AVERROR(ENOMEM);
3896 
3897  sc->ctts_data = ctts_data;
3898 
3899  ctts_data[ctts_count].count = count;
3900  ctts_data[ctts_count].offset = duration;
3901  ctts_count++;
3902 
3903  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
3904  count, duration);
3905 
3906  if (i+2<entries)
3907  mov_update_dts_shift(sc, duration, c->fc);
3908  }
3909 
3910  sc->ctts_count = ctts_count;
3911 
3912  if (pb->eof_reached) {
3913  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
3914  return AVERROR_EOF;
3915  }
3916 
3917  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3918 
3919  return 0;
3920 }
3921 
3923 {
3924  AVStream *st;
3925  MOVStreamContext *sc;
3926  uint8_t version;
3927  uint32_t grouping_type;
3928  uint32_t default_length;
3929  av_unused uint32_t default_group_description_index;
3930  uint32_t entry_count;
3931 
3932  if (c->fc->nb_streams < 1)
3933  return 0;
3934  st = c->fc->streams[c->fc->nb_streams - 1];
3935  sc = st->priv_data;
3936 
3937  version = avio_r8(pb); /* version */
3938  avio_rb24(pb); /* flags */
3939  grouping_type = avio_rl32(pb);
3940 
3941  /*
3942  * This function only supports "sync" boxes, but the code is able to parse
3943  * other boxes (such as "tscl", "tsas" and "stsa")
3944  */
3945  if (grouping_type != MKTAG('s','y','n','c'))
3946  return 0;
3947 
3948  default_length = version >= 1 ? avio_rb32(pb) : 0;
3949  default_group_description_index = version >= 2 ? avio_rb32(pb) : 0;
3950  entry_count = avio_rb32(pb);
3951 
3952  av_freep(&sc->sgpd_sync);
3953  sc->sgpd_sync_count = entry_count;
3954  sc->sgpd_sync = av_calloc(entry_count, sizeof(*sc->sgpd_sync));
3955  if (!sc->sgpd_sync)
3956  return AVERROR(ENOMEM);
3957 
3958  for (uint32_t i = 0; i < entry_count && !pb->eof_reached; i++) {
3959  uint32_t description_length = default_length;
3960  if (version >= 1 && default_length == 0)
3961  description_length = avio_rb32(pb);
3962  if (grouping_type == MKTAG('s','y','n','c')) {
3963  const uint8_t nal_unit_type = avio_r8(pb) & 0x3f;
3964  sc->sgpd_sync[i] = nal_unit_type;
3965  description_length -= 1;
3966  }
3967  avio_skip(pb, description_length);
3968  }
3969 
3970  if (pb->eof_reached) {
3971  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SGPD atom\n");
3972  return AVERROR_EOF;
3973  }
3974 
3975  return 0;
3976 }
3977 
3979 {
3980  AVStream *st;
3981  MOVStreamContext *sc;
3982  unsigned int i, entries;
3983  uint8_t version;
3984  uint32_t grouping_type;
3985  MOVSbgp *table, **tablep;
3986  int *table_count;
3987 
3988  if (c->fc->nb_streams < 1)
3989  return 0;
3990  st = c->fc->streams[c->fc->nb_streams-1];
3991  sc = st->priv_data;
3992 
3993  version = avio_r8(pb); /* version */
3994  avio_rb24(pb); /* flags */
3995  grouping_type = avio_rl32(pb);
3996 
3997  if (grouping_type == MKTAG('r','a','p',' ')) {
3998  tablep = &sc->rap_group;
3999  table_count = &sc->rap_group_count;
4000  } else if (grouping_type == MKTAG('s','y','n','c')) {
4001  tablep = &sc->sync_group;
4002  table_count = &sc->sync_group_count;
4003  } else {
4004  return 0;
4005  }
4006 
4007  if (version == 1)
4008  avio_rb32(pb); /* grouping_type_parameter */
4009 
4010  entries = avio_rb32(pb);
4011  if (!entries)
4012  return 0;
4013  if (*tablep)
4014  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP %s atom\n", av_fourcc2str(grouping_type));
4015  av_freep(tablep);
4016  table = av_malloc_array(entries, sizeof(*table));
4017  if (!table)
4018  return AVERROR(ENOMEM);
4019  *tablep = table;
4020 
4021  for (i = 0; i < entries && !pb->eof_reached; i++) {
4022  table[i].count = avio_rb32(pb); /* sample_count */
4023  table[i].index = avio_rb32(pb); /* group_description_index */
4024  }
4025 
4026  *table_count = i;
4027 
4028  if (pb->eof_reached) {
4029  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
4030  return AVERROR_EOF;
4031  }
4032 
4033  return 0;
4034 }
4035 
4036 /**
4037  * Get ith edit list entry (media time, duration).
4038  */
4040  const MOVStreamContext *msc,
4041  unsigned int edit_list_index,
4042  int64_t *edit_list_media_time,
4043  int64_t *edit_list_duration,
4044  int64_t global_timescale)
4045 {
4046  if (edit_list_index == msc->elst_count) {
4047  return 0;
4048  }
4049  *edit_list_media_time = msc->elst_data[edit_list_index].time;
4050  *edit_list_duration = msc->elst_data[edit_list_index].duration;
4051 
4052  /* duration is in global timescale units;convert to msc timescale */
4053  if (global_timescale == 0) {
4054  avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
4055  return 0;
4056  }
4057  *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
4058  global_timescale);
4059 
4060  if (*edit_list_duration + (uint64_t)*edit_list_media_time > INT64_MAX)
4061  *edit_list_duration = 0;
4062 
4063  return 1;
4064 }
4065 
4066 /**
4067  * Find the closest previous frame to the timestamp_pts, in e_old index
4068  * entries. Searching for just any frame / just key frames can be controlled by
4069  * last argument 'flag'.
4070  * Note that if ctts_data is not NULL, we will always search for a key frame
4071  * irrespective of the value of 'flag'. If we don't find any keyframe, we will
4072  * return the first frame of the video.
4073  *
4074  * Here the timestamp_pts is considered to be a presentation timestamp and
4075  * the timestamp of index entries are considered to be decoding timestamps.
4076  *
4077  * Returns 0 if successful in finding a frame, else returns -1.
4078  * Places the found index corresponding output arg.
4079  *
4080  * If ctts_old is not NULL, then refines the searched entry by searching
4081  * backwards from the found timestamp, to find the frame with correct PTS.
4082  *
4083  * Places the found ctts_index and ctts_sample in corresponding output args.
4084  */
4086  AVIndexEntry *e_old,
4087  int nb_old,
4088  MOVTimeToSample *tts_data,
4089  int64_t tts_count,
4090  int64_t timestamp_pts,
4091  int flag,
4092  int64_t* index,
4093  int64_t* tts_index,
4094  int64_t* tts_sample)
4095 {
4096  MOVStreamContext *msc = st->priv_data;
4097  FFStream *const sti = ffstream(st);
4098  AVIndexEntry *e_keep = sti->index_entries;
4099  int nb_keep = sti->nb_index_entries;
4100  int64_t i = 0;
4101 
4102  av_assert0(index);
4103 
4104  // If dts_shift > 0, then all the index timestamps will have to be offset by
4105  // at least dts_shift amount to obtain PTS.
4106  // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
4107  if (msc->dts_shift > 0) {
4108  timestamp_pts -= msc->dts_shift;
4109  }
4110 
4111  sti->index_entries = e_old;
4112  sti->nb_index_entries = nb_old;
4113  *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
4114 
4115  // Keep going backwards in the index entries until the timestamp is the same.
4116  if (*index >= 0) {
4117  for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
4118  i--) {
4119  if ((flag & AVSEEK_FLAG_ANY) ||
4120  (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
4121  *index = i - 1;
4122  }
4123  }
4124  }
4125 
4126  // If we have CTTS then refine the search, by searching backwards over PTS
4127  // computed by adding corresponding CTTS durations to index timestamps.
4128  if (msc->ctts_count && *index >= 0) {
4129  av_assert0(tts_index);
4130  av_assert0(tts_sample);
4131  // Find out the ctts_index for the found frame.
4132  *tts_index = 0;
4133  *tts_sample = 0;
4134  for (int64_t index_tts_count = 0; index_tts_count < *index; index_tts_count++) {
4135  if (*tts_index < tts_count) {
4136  (*tts_sample)++;
4137  if (tts_data[*tts_index].count == *tts_sample) {
4138  (*tts_index)++;
4139  *tts_sample = 0;
4140  }
4141  }
4142  }
4143 
4144  while (*index >= 0 && (*tts_index) >= 0 && (*tts_index) < tts_count) {
4145  // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
4146  // No need to add dts_shift to the timestamp here because timestamp_pts has already been
4147  // compensated by dts_shift above.
4148  if ((e_old[*index].timestamp + tts_data[*tts_index].offset) <= timestamp_pts &&
4149  (e_old[*index].flags & AVINDEX_KEYFRAME)) {
4150  break;
4151  }
4152 
4153  (*index)--;
4154  if (*tts_sample == 0) {
4155  (*tts_index)--;
4156  if (*tts_index >= 0)
4157  *tts_sample = tts_data[*tts_index].count - 1;
4158  } else {
4159  (*tts_sample)--;
4160  }
4161  }
4162  }
4163 
4164  /* restore AVStream state*/
4165  sti->index_entries = e_keep;
4166  sti->nb_index_entries = nb_keep;
4167  return *index >= 0 ? 0 : -1;
4168 }
4169 
4170 /**
4171  * Add index entry with the given values, to the end of ffstream(st)->index_entries.
4172  * Returns the new size ffstream(st)->index_entries if successful, else returns -1.
4173  *
4174  * This function is similar to ff_add_index_entry in libavformat/utils.c
4175  * except that here we are always unconditionally adding an index entry to
4176  * the end, instead of searching the entries list and skipping the add if
4177  * there is an existing entry with the same timestamp.
4178  * This is needed because the mov_fix_index calls this func with the same
4179  * unincremented timestamp for successive discarded frames.
4180  */
4182  int size, int distance, int flags)
4183 {
4184  FFStream *const sti = ffstream(st);
4185  AVIndexEntry *entries, *ie;
4186  int64_t index = -1;
4187  const size_t min_size_needed = (sti->nb_index_entries + 1) * sizeof(AVIndexEntry);
4188 
4189  // Double the allocation each time, to lower memory fragmentation.
4190  // Another difference from ff_add_index_entry function.
4191  const size_t requested_size =
4192  min_size_needed > sti->index_entries_allocated_size ?
4193  FFMAX(min_size_needed, 2 * sti->index_entries_allocated_size) :
4194  min_size_needed;
4195 
4196  if (sti->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
4197  return -1;
4198 
4199  entries = av_fast_realloc(sti->index_entries,
4201  requested_size);
4202  if (!entries)
4203  return -1;
4204 
4205  sti->index_entries = entries;
4206 
4207  index = sti->nb_index_entries++;
4208  ie= &entries[index];
4209 
4210  ie->pos = pos;
4211  ie->timestamp = timestamp;
4212  ie->min_distance= distance;
4213  ie->size= size;
4214  ie->flags = flags;
4215  return index;
4216 }
4217 
4218 /**
4219  * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
4220  * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
4221  */
4222 static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
4223  int64_t* frame_duration_buffer,
4224  int frame_duration_buffer_size) {
4225  FFStream *const sti = ffstream(st);
4226  int i = 0;
4227  av_assert0(end_index >= 0 && end_index <= sti->nb_index_entries);
4228  for (i = 0; i < frame_duration_buffer_size; i++) {
4229  end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
4230  sti->index_entries[end_index - 1 - i].timestamp = end_ts;
4231  }
4232 }
4233 
4234 static int add_tts_entry(MOVTimeToSample **tts_data, unsigned int *tts_count, unsigned int *allocated_size,
4235  int count, int offset, unsigned int duration)
4236 {
4237  MOVTimeToSample *tts_buf_new;
4238  const size_t min_size_needed = (*tts_count + 1) * sizeof(MOVTimeToSample);
4239  const size_t requested_size =
4240  min_size_needed > *allocated_size ?
4241  FFMAX(min_size_needed, 2 * (*allocated_size)) :
4242  min_size_needed;
4243 
4244  if ((unsigned)(*tts_count) >= UINT_MAX / sizeof(MOVTimeToSample) - 1)
4245  return -1;
4246 
4247  tts_buf_new = av_fast_realloc(*tts_data, allocated_size, requested_size);
4248 
4249  if (!tts_buf_new)
4250  return -1;
4251 
4252  *tts_data = tts_buf_new;
4253 
4254  tts_buf_new[*tts_count].count = count;
4255  tts_buf_new[*tts_count].offset = offset;
4256  tts_buf_new[*tts_count].duration = duration;
4257 
4258  *tts_count = (*tts_count) + 1;
4259  return 0;
4260 }
4261 
4262 #define MAX_REORDER_DELAY 16
4264 {
4265  MOVStreamContext *msc = st->priv_data;
4266  FFStream *const sti = ffstream(st);
4267  int ctts_ind = 0;
4268  int ctts_sample = 0;
4269  int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
4270  int buf_start = 0;
4271  int j, r, num_swaps;
4272 
4273  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
4274  pts_buf[j] = INT64_MIN;
4275 
4276  if (st->codecpar->video_delay <= 0 && msc->ctts_count &&
4278  st->codecpar->video_delay = 0;
4279  for (int ind = 0; ind < sti->nb_index_entries && ctts_ind < msc->tts_count; ++ind) {
4280  // Point j to the last elem of the buffer and insert the current pts there.
4281  j = buf_start;
4282  buf_start = (buf_start + 1);
4283  if (buf_start == MAX_REORDER_DELAY + 1)
4284  buf_start = 0;
4285 
4286  pts_buf[j] = sti->index_entries[ind].timestamp + msc->tts_data[ctts_ind].offset;
4287 
4288  // The timestamps that are already in the sorted buffer, and are greater than the
4289  // current pts, are exactly the timestamps that need to be buffered to output PTS
4290  // in correct sorted order.
4291  // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
4292  // can be computed as the maximum no. of swaps any particular timestamp needs to
4293  // go through, to keep this buffer in sorted order.
4294  num_swaps = 0;
4295  while (j != buf_start) {
4296  r = j - 1;
4297  if (r < 0) r = MAX_REORDER_DELAY;
4298  if (pts_buf[j] < pts_buf[r]) {
4299  FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
4300  ++num_swaps;
4301  } else {
4302  break;
4303  }
4304  j = r;
4305  }
4306  st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
4307 
4308  ctts_sample++;
4309  if (ctts_sample == msc->tts_data[ctts_ind].count) {
4310  ctts_ind++;
4311  ctts_sample = 0;
4312  }
4313  }
4314  av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
4315  st->codecpar->video_delay, st->index);
4316  }
4317 }
4318 
4320 {
4321  sc->current_sample++;
4322  sc->current_index++;
4323  if (sc->index_ranges &&
4324  sc->current_index >= sc->current_index_range->end &&
4325  sc->current_index_range->end) {
4326  sc->current_index_range++;
4328  }
4329 }
4330 
4332 {
4333  sc->current_sample--;
4334  sc->current_index--;
4335  if (sc->index_ranges &&
4337  sc->current_index_range > sc->index_ranges) {
4338  sc->current_index_range--;
4339  sc->current_index = sc->current_index_range->end - 1;
4340  }
4341 }
4342 
4343 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
4344 {
4345  int64_t range_size;
4346 
4347  sc->current_sample = current_sample;
4348  sc->current_index = current_sample;
4349  if (!sc->index_ranges) {
4350  return;
4351  }
4352 
4353  for (sc->current_index_range = sc->index_ranges;
4354  sc->current_index_range->end;
4355  sc->current_index_range++) {
4356  range_size = sc->current_index_range->end - sc->current_index_range->start;
4357  if (range_size > current_sample) {
4358  sc->current_index = sc->current_index_range->start + current_sample;
4359  break;
4360  }
4361  current_sample -= range_size;
4362  }
4363 }
4364 
4365 /**
4366  * Fix ffstream(st)->index_entries, so that it contains only the entries (and the entries
4367  * which are needed to decode them) that fall in the edit list time ranges.
4368  * Also fixes the timestamps of the index entries to match the timeline
4369  * specified the edit lists.
4370  */
4371 static void mov_fix_index(MOVContext *mov, AVStream *st)
4372 {
4373  MOVStreamContext *msc = st->priv_data;
4374  FFStream *const sti = ffstream(st);
4375  AVIndexEntry *e_old = sti->index_entries;
4376  int nb_old = sti->nb_index_entries;
4377  const AVIndexEntry *e_old_end = e_old + nb_old;
4378  const AVIndexEntry *current = NULL;
4379  MOVTimeToSample *tts_data_old = msc->tts_data;
4380  int64_t tts_index_old = 0;
4381  int64_t tts_sample_old = 0;
4382  int64_t tts_count_old = msc->tts_count;
4383  int64_t edit_list_media_time = 0;
4384  int64_t edit_list_duration = 0;
4385  int64_t frame_duration = 0;
4386  int64_t edit_list_dts_counter = 0;
4387  int64_t edit_list_dts_entry_end = 0;
4388  int64_t edit_list_start_tts_sample = 0;
4389  int64_t curr_cts;
4390  int64_t curr_ctts = 0;
4391  int64_t empty_edits_sum_duration = 0;
4392  int64_t edit_list_index = 0;
4393  int64_t index;
4394  int flags;
4395  int64_t start_dts = 0;
4396  int64_t edit_list_start_encountered = 0;
4397  int64_t search_timestamp = 0;
4398  int64_t* frame_duration_buffer = NULL;
4399  int num_discarded_begin = 0;
4400  int first_non_zero_audio_edit = -1;
4401  int packet_skip_samples = 0;
4402  MOVIndexRange *current_index_range = NULL;
4403  int found_keyframe_after_edit = 0;
4404  int found_non_empty_edit = 0;
4405 
4406  if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
4407  return;
4408  }
4409 
4410  // allocate the index ranges array
4411  msc->index_ranges = av_malloc_array(msc->elst_count + 1,
4412  sizeof(msc->index_ranges[0]));
4413  if (!msc->index_ranges) {
4414  av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
4415  return;
4416  }
4417  msc->current_index_range = msc->index_ranges;
4418 
4419  // Clean AVStream from traces of old index
4420  sti->index_entries = NULL;
4422  sti->nb_index_entries = 0;
4423 
4424  // Clean time to sample fields of MOVStreamContext
4425  msc->tts_data = NULL;
4426  msc->tts_count = 0;
4427  msc->tts_index = 0;
4428  msc->tts_sample = 0;
4429  msc->tts_allocated_size = 0;
4430 
4431  // Reinitialize min_corrected_pts so that it can be computed again.
4432  msc->min_corrected_pts = -1;
4433 
4434  // If the dts_shift is positive (in case of negative ctts values in mov),
4435  // then negate the DTS by dts_shift
4436  if (msc->dts_shift > 0) {
4437  edit_list_dts_entry_end -= msc->dts_shift;
4438  av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
4439  }
4440 
4441  start_dts = edit_list_dts_entry_end;
4442 
4443  while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
4444  &edit_list_duration, mov->time_scale)) {
4445  av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
4446  st->index, edit_list_index, edit_list_media_time, edit_list_duration);
4447  edit_list_index++;
4448  edit_list_dts_counter = edit_list_dts_entry_end;
4449  edit_list_dts_entry_end = av_sat_add64(edit_list_dts_entry_end, edit_list_duration);
4450  if (edit_list_dts_entry_end == INT64_MAX) {
4451  av_log(mov->fc, AV_LOG_ERROR, "Cannot calculate dts entry length with duration %"PRId64"\n",
4452  edit_list_duration);
4453  break;
4454  }
4455  num_discarded_begin = 0;
4456  if (!found_non_empty_edit && edit_list_media_time == -1) {
4457  empty_edits_sum_duration += edit_list_duration;
4458  continue;
4459  }
4460  found_non_empty_edit = 1;
4461 
4462  // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
4463  // according to the edit list below.
4464  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
4465  if (first_non_zero_audio_edit < 0) {
4466  first_non_zero_audio_edit = 1;
4467  } else {
4468  first_non_zero_audio_edit = 0;
4469  }
4470 
4471  if (first_non_zero_audio_edit > 0)
4472  sti->skip_samples = msc->start_pad = 0;
4473  }
4474 
4475  // While reordering frame index according to edit list we must handle properly
4476  // the scenario when edit list entry starts from none key frame.
4477  // We find closest previous key frame and preserve it and consequent frames in index.
4478  // All frames which are outside edit list entry time boundaries will be dropped after decoding.
4479  search_timestamp = edit_list_media_time;
4480  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
4481  // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
4482  // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
4483  // edit_list_media_time to cover the decoder delay.
4484  search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
4485  }
4486 
4487  if (find_prev_closest_index(st, e_old, nb_old, tts_data_old, tts_count_old, search_timestamp, 0,
4488  &index, &tts_index_old, &tts_sample_old) < 0) {
4489  av_log(mov->fc, AV_LOG_WARNING,
4490  "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
4491  st->index, edit_list_index, search_timestamp);
4492  if (find_prev_closest_index(st, e_old, nb_old, tts_data_old, tts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
4493  &index, &tts_index_old, &tts_sample_old) < 0) {
4494  av_log(mov->fc, AV_LOG_WARNING,
4495  "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
4496  st->index, edit_list_index, search_timestamp);
4497  index = 0;
4498  tts_index_old = 0;
4499  tts_sample_old = 0;
4500  }
4501  }
4502  current = e_old + index;
4503  edit_list_start_tts_sample = tts_sample_old;
4504 
4505  // Iterate over index and arrange it according to edit list
4506  edit_list_start_encountered = 0;
4507  found_keyframe_after_edit = 0;
4508  for (; current < e_old_end; current++, index++) {
4509  // check if frame outside edit list mark it for discard
4510  frame_duration = (current + 1 < e_old_end) ?
4511  ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
4512 
4513  flags = current->flags;
4514 
4515  // frames (pts) before or after edit list
4516  curr_cts = current->timestamp + msc->dts_shift;
4517  curr_ctts = 0;
4518 
4519  if (tts_data_old && tts_index_old < tts_count_old) {
4520  curr_ctts = tts_data_old[tts_index_old].offset;
4521  av_log(mov->fc, AV_LOG_TRACE, "stts: %"PRId64" ctts: %"PRId64", tts_index: %"PRId64", tts_count: %"PRId64"\n",
4522  curr_cts, curr_ctts, tts_index_old, tts_count_old);
4523  curr_cts += curr_ctts;
4524  tts_sample_old++;
4525  if (tts_sample_old == tts_data_old[tts_index_old].count) {
4526  if (add_tts_entry(&msc->tts_data, &msc->tts_count,
4527  &msc->tts_allocated_size,
4528  tts_data_old[tts_index_old].count - edit_list_start_tts_sample,
4529  tts_data_old[tts_index_old].offset, tts_data_old[tts_index_old].duration) == -1) {
4530  av_log(mov->fc, AV_LOG_ERROR, "Cannot add Time To Sample entry %"PRId64" - {%"PRId64", %d}\n",
4531  tts_index_old,
4532  tts_data_old[tts_index_old].count - edit_list_start_tts_sample,
4533  tts_data_old[tts_index_old].offset);
4534  break;
4535  }
4536  tts_index_old++;
4537  tts_sample_old = 0;
4538  edit_list_start_tts_sample = 0;
4539  }
4540  }
4541 
4542  if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
4544  curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
4545  first_non_zero_audio_edit > 0) {
4546  packet_skip_samples = edit_list_media_time - curr_cts;
4547  sti->skip_samples += packet_skip_samples;
4548 
4549  // Shift the index entry timestamp by packet_skip_samples to be correct.
4550  edit_list_dts_counter -= packet_skip_samples;
4551  if (edit_list_start_encountered == 0) {
4552  edit_list_start_encountered = 1;
4553  // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
4554  // discarded packets.
4555  if (frame_duration_buffer) {
4556  fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
4557  frame_duration_buffer, num_discarded_begin);
4558  av_freep(&frame_duration_buffer);
4559  }
4560  }
4561 
4562  av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
4563  } else {
4565  av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
4566 
4567  if (edit_list_start_encountered == 0) {
4568  num_discarded_begin++;
4569  frame_duration_buffer = av_realloc(frame_duration_buffer,
4570  num_discarded_begin * sizeof(int64_t));
4571  if (!frame_duration_buffer) {
4572  av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
4573  break;
4574  }
4575  frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
4576 
4577  // Increment skip_samples for the first non-zero audio edit list
4578  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4579  first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
4580  sti->skip_samples += frame_duration;
4581  }
4582  }
4583  }
4584  } else {
4585  if (msc->min_corrected_pts < 0) {
4586  msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
4587  } else {
4588  msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
4589  }
4590  if (edit_list_start_encountered == 0) {
4591  edit_list_start_encountered = 1;
4592  // Make timestamps strictly monotonically increasing by rewriting timestamps for
4593  // discarded packets.
4594  if (frame_duration_buffer) {
4595  fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
4596  frame_duration_buffer, num_discarded_begin);
4597  av_freep(&frame_duration_buffer);
4598  }
4599  }
4600  }
4601 
4602  if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
4603  current->min_distance, flags) == -1) {
4604  av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
4605  break;
4606  }
4607 
4608  // Update the index ranges array
4609  if (!current_index_range || index != current_index_range->end) {
4610  current_index_range = current_index_range ? current_index_range + 1
4611  : msc->index_ranges;
4612  current_index_range->start = index;
4613  }
4614  current_index_range->end = index + 1;
4615 
4616  // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
4617  if (edit_list_start_encountered > 0) {
4618  edit_list_dts_counter = edit_list_dts_counter + frame_duration;
4619  }
4620 
4621  // Break when found first key frame after edit entry completion
4622  if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
4624  if (msc->ctts_count) {
4625  // If we have CTTS and this is the first keyframe after edit elist,
4626  // wait for one more, because there might be trailing B-frames after this I-frame
4627  // that do belong to the edit.
4628  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
4629  found_keyframe_after_edit = 1;
4630  continue;
4631  }
4632  if (tts_sample_old != 0) {
4633  if (add_tts_entry(&msc->tts_data, &msc->tts_count,
4634  &msc->tts_allocated_size,
4635  tts_sample_old - edit_list_start_tts_sample,
4636  tts_data_old[tts_index_old].offset, tts_data_old[tts_index_old].duration) == -1) {
4637  av_log(mov->fc, AV_LOG_ERROR, "Cannot add Time To Sample entry %"PRId64" - {%"PRId64", %d}\n",
4638  tts_index_old, tts_sample_old - edit_list_start_tts_sample,
4639  tts_data_old[tts_index_old].offset);
4640  break;
4641  }
4642  }
4643  }
4644  break;
4645  }
4646  }
4647  }
4648  // If there are empty edits, then msc->min_corrected_pts might be positive
4649  // intentionally. So we subtract the sum duration of empty edits here.
4650  msc->min_corrected_pts -= empty_edits_sum_duration;
4651 
4652  // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
4653  // dts by that amount to make the first pts zero.
4654  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4655  if (msc->min_corrected_pts > 0) {
4656  av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
4657  for (int i = 0; i < sti->nb_index_entries; ++i)
4659  }
4660  }
4661  // Start time should be equal to zero or the duration of any empty edits.
4662  st->start_time = empty_edits_sum_duration;
4663 
4664  // Update av stream length, if it ends up shorter than the track's media duration
4665  st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
4666  msc->start_pad = sti->skip_samples;
4667 
4668  // Free the old index and the old CTTS structures
4669  av_free(e_old);
4670  av_free(tts_data_old);
4671  av_freep(&frame_duration_buffer);
4672 
4673  // Null terminate the index ranges array
4674  current_index_range = current_index_range ? current_index_range + 1
4675  : msc->index_ranges;
4676  current_index_range->start = 0;
4677  current_index_range->end = 0;
4678  msc->current_index = msc->index_ranges[0].start;
4679 }
4680 
4681 static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
4682 {
4683  for (uint32_t i = 0; i < sc->sgpd_sync_count; i++)
4684  if (sc->sgpd_sync[i] == nal_unit_type)
4685  return i + 1;
4686  return 0;
4687 }
4688 
4690 {
4691  int k;
4692  int sample_id = 0;
4693  uint32_t cra_index;
4694  MOVStreamContext *sc = st->priv_data;
4695 
4696  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !sc->sync_group_count)
4697  return 0;
4698 
4699  /* Build an unrolled index of the samples */
4700  sc->sample_offsets_count = 0;
4701  for (uint32_t i = 0; i < sc->ctts_count; i++) {
4702  if (sc->ctts_data[i].count > INT_MAX - sc->sample_offsets_count)
4703  return AVERROR(ENOMEM);
4704  sc->sample_offsets_count += sc->ctts_data[i].count;
4705  }
4706  av_freep(&sc->sample_offsets);
4708  if (!sc->sample_offsets)
4709  return AVERROR(ENOMEM);
4710  k = 0;
4711  for (uint32_t i = 0; i < sc->ctts_count; i++)
4712  for (int j = 0; j < sc->ctts_data[i].count; j++)
4713  sc->sample_offsets[k++] = sc->ctts_data[i].offset;
4714 
4715  /* The following HEVC NAL type reveal the use of open GOP sync points
4716  * (TODO: BLA types may also be concerned) */
4717  cra_index = get_sgpd_sync_index(sc, HEVC_NAL_CRA_NUT); /* Clean Random Access */
4718  if (!cra_index)
4719  return 0;
4720 
4721  /* Build a list of open-GOP key samples */
4722  sc->open_key_samples_count = 0;
4723  for (uint32_t i = 0; i < sc->sync_group_count; i++)
4724  if (sc->sync_group[i].index == cra_index) {
4725  if (sc->sync_group[i].count > INT_MAX - sc->open_key_samples_count)
4726  return AVERROR(ENOMEM);
4728  }
4729  av_freep(&sc->open_key_samples);
4731  if (!sc->open_key_samples)
4732  return AVERROR(ENOMEM);
4733  k = 0;
4734  for (uint32_t i = 0; i < sc->sync_group_count; i++) {
4735  const MOVSbgp *sg = &sc->sync_group[i];
4736  if (sg->index == cra_index)
4737  for (uint32_t j = 0; j < sg->count; j++)
4738  sc->open_key_samples[k++] = sample_id;
4739  if (sg->count > INT_MAX - sample_id)
4740  return AVERROR_PATCHWELCOME;
4741  sample_id += sg->count;
4742  }
4743 
4744  /* Identify the minimal time step between samples */
4745  sc->min_sample_duration = UINT_MAX;
4746  for (uint32_t i = 0; i < sc->stts_count; i++)
4748 
4749  return 0;
4750 }
4751 
4752 #define MOV_MERGE_CTTS 1
4753 #define MOV_MERGE_STTS 2
4754 /*
4755  * Merge stts and ctts arrays into a new combined array.
4756  * stts_count and ctts_count may be left untouched as they will be
4757  * used to check for the presence of either of them.
4758  */
4759 static int mov_merge_tts_data(MOVContext *mov, AVStream *st, int flags)
4760 {
4761  MOVStreamContext *sc = st->priv_data;
4762  int ctts = sc->ctts_data && (flags & MOV_MERGE_CTTS);
4763  int stts = sc->stts_data && (flags & MOV_MERGE_STTS);
4764  int idx = 0;
4765 
4766  if (!sc->ctts_data && !sc->stts_data)
4767  return 0;
4768  // Expand time to sample entries such that we have a 1-1 mapping with samples
4769  if (!sc->sample_count || sc->sample_count >= UINT_MAX / sizeof(*sc->tts_data))
4770  return -1;
4771 
4772  if (ctts) {
4774  sc->sample_count * sizeof(*sc->tts_data));
4775  if (!sc->tts_data)
4776  return -1;
4777 
4778  memset(sc->tts_data, 0, sc->tts_allocated_size);
4779 
4780  for (int i = 0; i < sc->ctts_count &&
4781  idx < sc->sample_count; i++)
4782  for (int j = 0; j < sc->ctts_data[i].count &&
4783  idx < sc->sample_count; j++) {
4784  sc->tts_data[idx].offset = sc->ctts_data[i].offset;
4785  sc->tts_data[idx++].count = 1;
4786  }
4787 
4788  sc->tts_count = idx;
4789  } else
4790  sc->ctts_count = 0;
4791  av_freep(&sc->ctts_data);
4792  sc->ctts_allocated_size = 0;
4793 
4794  idx = 0;
4795  if (stts) {
4797  sc->sample_count * sizeof(*sc->tts_data));
4798  if (!tts_data)
4799  return -1;
4800 
4801  if (!sc->tts_data)
4802  memset(tts_data, 0, sc->tts_allocated_size);
4803  sc->tts_data = tts_data;
4804 
4805  for (int i = 0; i < sc->stts_count &&
4806  idx < sc->sample_count; i++)
4807  for (int j = 0; j < sc->stts_data[i].count &&
4808  idx < sc->sample_count; j++) {
4809  sc->tts_data[idx].duration = sc->stts_data[i].duration;
4810  sc->tts_data[idx++].count = 1;
4811  }
4812 
4813  sc->tts_count = FFMAX(sc->tts_count, idx);
4814  } else
4815  sc->stts_count = 0;
4816  av_freep(&sc->stts_data);
4817  sc->stts_allocated_size = 0;
4818 
4819  return 0;
4820 }
4821 
4822 static void mov_build_index(MOVContext *mov, AVStream *st)
4823 {
4824  MOVStreamContext *sc = st->priv_data;
4825  FFStream *const sti = ffstream(st);
4826  int64_t current_offset;
4827  int64_t current_dts = 0;
4828  unsigned int stts_index = 0;
4829  unsigned int stsc_index = 0;
4830  unsigned int stss_index = 0;
4831  unsigned int stps_index = 0;
4832  unsigned int i, j;
4833  uint64_t stream_size = 0;
4834 
4835  int ret = build_open_gop_key_points(st);
4836  if (ret < 0)
4837  return;
4838 
4839  if (sc->elst_count) {
4840  int i, edit_start_index = 0, multiple_edits = 0;
4841  int64_t empty_duration = 0; // empty duration of the first edit list entry
4842  int64_t start_time = 0; // start time of the media
4843 
4844  for (i = 0; i < sc->elst_count; i++) {
4845  const MOVElst *e = &sc->elst_data[i];
4846  if (i == 0 && e->time == -1) {
4847  /* if empty, the first entry is the start time of the stream
4848  * relative to the presentation itself */
4849  empty_duration = e->duration;
4850  edit_start_index = 1;
4851  } else if (i == edit_start_index && e->time >= 0) {
4852  start_time = e->time;
4853  } else {
4854  multiple_edits = 1;
4855  }
4856  }
4857 
4858  if (multiple_edits && !mov->advanced_editlist) {
4860  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
4861  "not supported in fragmented MP4 files\n");
4862  else
4863  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
4864  "Use -advanced_editlist to correctly decode otherwise "
4865  "a/v desync might occur\n");
4866  }
4867 
4868  /* adjust first dts according to edit list */
4869  if ((empty_duration || start_time) && mov->time_scale > 0) {
4870  if (empty_duration)
4871  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
4872 
4873  if (av_sat_sub64(start_time, empty_duration) != start_time - (uint64_t)empty_duration)
4874  av_log(mov->fc, AV_LOG_WARNING, "start_time - empty_duration is not representable\n");
4875 
4876  sc->time_offset = start_time - (uint64_t)empty_duration;
4878  if (!mov->advanced_editlist)
4879  current_dts = -sc->time_offset;
4880  }
4881 
4882  if (!multiple_edits && !mov->advanced_editlist &&
4885  (AVRational){1, st->codecpar->sample_rate});
4886  }
4887 
4888  /* only use old uncompressed audio chunk demuxing when stts specifies it */
4889  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4890  sc->stts_count == 1 && sc->stts_data && sc->stts_data[0].duration == 1)) {
4891  unsigned int current_sample = 0;
4892  unsigned int stts_sample = 0;
4893  unsigned int sample_size;
4894  unsigned int distance = 0;
4895  unsigned int rap_group_index = 0;
4896  unsigned int rap_group_sample = 0;
4897  int rap_group_present = sc->rap_group_count && sc->rap_group;
4898  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
4899 
4900  current_dts -= sc->dts_shift;
4901 
4902  if (!sc->sample_count || sti->nb_index_entries || sc->tts_count)
4903  return;
4904  if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
4905  return;
4906  if (av_reallocp_array(&sti->index_entries,
4907  sti->nb_index_entries + sc->sample_count,
4908  sizeof(*sti->index_entries)) < 0) {
4909  sti->nb_index_entries = 0;
4910  return;
4911  }
4912  sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
4913 
4915  if (ret < 0)
4916  return;
4917 
4918  for (i = 0; i < sc->chunk_count; i++) {
4919  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
4920  current_offset = sc->chunk_offsets[i];
4921  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4922  i + 1 == sc->stsc_data[stsc_index + 1].first)
4923  stsc_index++;
4924 
4925  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
4926  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
4927  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
4928  sc->stsz_sample_size = sc->sample_size;
4929  }
4930  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
4931  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
4932  sc->stsz_sample_size = sc->sample_size;
4933  }
4934 
4935  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
4936  int keyframe = 0;
4937  if (current_sample >= sc->sample_count) {
4938  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
4939  return;
4940  }
4941 
4942  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
4943  keyframe = 1;
4944  if (stss_index + 1 < sc->keyframe_count)
4945  stss_index++;
4946  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
4947  keyframe = 1;
4948  if (stps_index + 1 < sc->stps_count)
4949  stps_index++;
4950  }
4951  if (rap_group_present && rap_group_index < sc->rap_group_count) {
4952  if (sc->rap_group[rap_group_index].index > 0)
4953  keyframe = 1;
4954  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
4955  rap_group_sample = 0;
4956  rap_group_index++;
4957  }
4958  }
4959  if (sc->keyframe_absent
4960  && !sc->stps_count
4961  && !rap_group_present
4962  && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
4963  keyframe = 1;
4964  if (keyframe)
4965  distance = 0;
4966  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
4967  if (current_offset > INT64_MAX - sample_size) {
4968  av_log(mov->fc, AV_LOG_ERROR, "Current offset %"PRId64" or sample size %u is too large\n",
4969  current_offset,
4970  sample_size);
4971  return;
4972  }
4973 
4974  if (sc->pseudo_stream_id == -1 ||
4975  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
4976  AVIndexEntry *e;
4977  if (sample_size > 0x3FFFFFFF) {
4978  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
4979  return;
4980  }
4981  e = &sti->index_entries[sti->nb_index_entries++];
4982  e->pos = current_offset;
4983  e->timestamp = current_dts;
4984  e->size = sample_size;
4985  e->min_distance = distance;
4986  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
4987  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
4988  "size %u, distance %u, keyframe %d\n", st->index, current_sample,
4989  current_offset, current_dts, sample_size, distance, keyframe);
4990  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries < 100)
4991  ff_rfps_add_frame(mov->fc, st, current_dts);
4992  }
4993 
4994  current_offset += sample_size;
4995  stream_size += sample_size;
4996 
4997  current_dts += sc->tts_data[stts_index].duration;
4998 
4999  distance++;
5000  stts_sample++;
5001  current_sample++;
5002  if (stts_index + 1 < sc->tts_count && stts_sample == sc->tts_data[stts_index].count) {
5003  stts_sample = 0;
5004  stts_index++;
5005  }
5006  }
5007  }
5008  if (st->duration > 0)
5009  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
5010  } else {
5011  unsigned chunk_samples, total = 0;
5012 
5013  if (!sc->chunk_count || sc->tts_count)
5014  return;
5015 
5016  // compute total chunk count
5017  for (i = 0; i < sc->stsc_count; i++) {
5018  unsigned count, chunk_count;
5019 
5020  chunk_samples = sc->stsc_data[i].count;
5021  if (i != sc->stsc_count - 1 &&
5022  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
5023  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
5024  return;
5025  }
5026 
5027  if (sc->samples_per_frame >= 160) { // gsm
5028  count = chunk_samples / sc->samples_per_frame;
5029  } else if (sc->samples_per_frame > 1) {
5030  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
5031  count = (chunk_samples+samples-1) / samples;
5032  } else {
5033  count = (chunk_samples+1023) / 1024;
5034  }
5035 
5036  if (mov_stsc_index_valid(i, sc->stsc_count))
5037  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
5038  else
5039  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
5040  total += chunk_count * count;
5041  }
5042 
5043  av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
5044  if (total >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
5045  return;
5046  if (av_reallocp_array(&sti->index_entries,
5047  sti->nb_index_entries + total,
5048  sizeof(*sti->index_entries)) < 0) {
5049  sti->nb_index_entries = 0;
5050  return;
5051  }
5052  sti->index_entries_allocated_size = (sti->nb_index_entries + total) * sizeof(*sti->index_entries);
5053 
5054  // populate index
5055  for (i = 0; i < sc->chunk_count; i++) {
5056  current_offset = sc->chunk_offsets[i];
5057  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
5058  i + 1 == sc->stsc_data[stsc_index + 1].first)
5059  stsc_index++;
5060  chunk_samples = sc->stsc_data[stsc_index].count;
5061 
5062  while (chunk_samples > 0) {
5063  AVIndexEntry *e;
5064  unsigned size, samples;
5065 
5066  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
5068  "Zero bytes per frame, but %d samples per frame",
5069  sc->samples_per_frame);
5070  return;
5071  }
5072 
5073  if (sc->samples_per_frame >= 160) { // gsm
5074  samples = sc->samples_per_frame;
5075  size = sc->bytes_per_frame;
5076  } else {
5077  if (sc->samples_per_frame > 1) {
5078  samples = FFMIN((1024 / sc->samples_per_frame)*
5079  sc->samples_per_frame, chunk_samples);
5081  } else {
5082  samples = FFMIN(1024, chunk_samples);
5083  size = samples * sc->sample_size;
5084  }
5085  }
5086 
5087  if (sti->nb_index_entries >= total) {
5088  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
5089  return;
5090  }
5091  if (size > 0x3FFFFFFF) {
5092  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
5093  return;
5094  }
5095  e = &sti->index_entries[sti->nb_index_entries++];
5096  e->pos = current_offset;
5097  e->timestamp = current_dts;
5098  e->size = size;
5099  e->min_distance = 0;
5100  e->flags = AVINDEX_KEYFRAME;
5101  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
5102  "size %u, duration %u\n", st->index, i, current_offset, current_dts,
5103  size, samples);
5104 
5105  current_offset += size;
5106  current_dts += samples;
5107  chunk_samples -= samples;
5108  }
5109  }
5110 
5112  if (ret < 0)
5113  return;
5114  }
5115 
5116  if (!mov->ignore_editlist && mov->advanced_editlist) {
5117  // Fix index according to edit lists.
5118  mov_fix_index(mov, st);
5119  }
5120 
5121  // Update start time of the stream.
5123  st->start_time = sti->index_entries[0].timestamp + sc->dts_shift;
5124  if (sc->tts_data) {
5125  st->start_time += sc->tts_data[0].offset;
5126  }
5127  }
5128 
5129  mov_estimate_video_delay(mov, st);
5130 }
5131 
5132 static int test_same_origin(const char *src, const char *ref) {
5133  char src_proto[64];
5134  char ref_proto[64];
5135  char src_auth[256];
5136  char ref_auth[256];
5137  char src_host[256];
5138  char ref_host[256];
5139  int src_port=-1;
5140  int ref_port=-1;
5141 
5142  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
5143  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
5144 
5145  if (strlen(src) == 0) {
5146  return -1;
5147  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
5148  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
5149  strlen(src_host) + 1 >= sizeof(src_host) ||
5150  strlen(ref_host) + 1 >= sizeof(ref_host)) {
5151  return 0;
5152  } else if (strcmp(src_proto, ref_proto) ||
5153  strcmp(src_auth, ref_auth) ||
5154  strcmp(src_host, ref_host) ||
5155  src_port != ref_port) {
5156  return 0;
5157  } else
5158  return 1;
5159 }
5160 
5161 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
5162 {
5163  /* try relative path, we do not try the absolute because it can leak information about our
5164  system to an attacker */
5165  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
5166  char filename[1025];
5167  const char *src_path;
5168  int i, l;
5169 
5170  /* find a source dir */
5171  src_path = strrchr(src, '/');
5172  if (src_path)
5173  src_path++;
5174  else
5175  src_path = src;
5176 
5177  /* find a next level down to target */
5178  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
5179  if (ref->path[l] == '/') {
5180  if (i == ref->nlvl_to - 1)
5181  break;
5182  else
5183  i++;
5184  }
5185 
5186  /* compose filename if next level down to target was found */
5187  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
5188  memcpy(filename, src, src_path - src);
5189  filename[src_path - src] = 0;
5190 
5191  for (i = 1; i < ref->nlvl_from; i++)
5192  av_strlcat(filename, "../", sizeof(filename));
5193 
5194  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
5195  if (!c->use_absolute_path) {
5196  int same_origin = test_same_origin(src, filename);
5197 
5198  if (!same_origin) {
5199  av_log(c->fc, AV_LOG_ERROR,
5200  "Reference with mismatching origin, %s not tried for security reasons, "
5201  "set demuxer option use_absolute_path to allow it anyway\n",
5202  ref->path);
5203  return AVERROR(ENOENT);
5204  }
5205 
5206  if (strstr(ref->path + l + 1, "..") ||
5207  strstr(ref->path + l + 1, ":") ||
5208  (ref->nlvl_from > 1 && same_origin < 0) ||
5209  (filename[0] == '/' && src_path == src))
5210  return AVERROR(ENOENT);
5211  }
5212 
5213  if (strlen(filename) + 1 == sizeof(filename))
5214  return AVERROR(ENOENT);
5215  if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
5216  return 0;
5217  }
5218  } else if (c->use_absolute_path) {
5219  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
5220  "this is a possible security issue\n");
5221  if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
5222  return 0;
5223  } else {
5224  av_log(c->fc, AV_LOG_ERROR,
5225  "Absolute path %s not tried for security reasons, "
5226  "set demuxer option use_absolute_path to allow absolute paths\n",
5227  ref->path);
5228  }
5229 
5230  return AVERROR(ENOENT);
5231 }
5232 
5234 {
5235  if (sc->time_scale <= 0) {
5236  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
5237  sc->time_scale = c->time_scale;
5238  if (sc->time_scale <= 0)
5239  sc->time_scale = 1;
5240  }
5241 }
5242 
5243 #if CONFIG_IAMFDEC
5244 static int mov_update_iamf_streams(MOVContext *c, const AVStream *st)
5245 {
5246  const MOVStreamContext *sc = st->priv_data;
5247  const IAMFContext *iamf = &sc->iamf->iamf;
5248 
5249  for (int i = 0; i < iamf->nb_audio_elements; i++) {
5250  const AVStreamGroup *stg = NULL;
5251 
5252  for (int j = 0; j < c->fc->nb_stream_groups; j++)
5253  if (c->fc->stream_groups[j]->id == iamf->audio_elements[i]->audio_element_id)
5254  stg = c->fc->stream_groups[j];
5255  av_assert0(stg);
5256 
5257  for (int j = 0; j < stg->nb_streams; j++) {
5258  const FFStream *sti = cffstream(st);
5259  AVStream *out = stg->streams[j];
5260  FFStream *out_sti = ffstream(stg->streams[j]);
5261 
5262  out->codecpar->bit_rate = 0;
5263 
5264  if (out == st)
5265  continue;
5266 
5267  out->time_base = st->time_base;
5268  out->start_time = st->start_time;
5269  out->duration = st->duration;
5270  out->nb_frames = st->nb_frames;
5271  out->discard = st->discard;
5272 
5273  av_assert0(!out_sti->index_entries);
5275  if (!out_sti->index_entries)
5276  return AVERROR(ENOMEM);
5277 
5279  out_sti->nb_index_entries = sti->nb_index_entries;
5280  out_sti->skip_samples = sti->skip_samples;
5281  memcpy(out_sti->index_entries, sti->index_entries, sti->index_entries_allocated_size);
5282  }
5283  }
5284 
5285  return 0;
5286 }
5287 #endif
5288 
5289 static int sanity_checks(void *log_obj, MOVStreamContext *sc, int index)
5290 {
5291  if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
5292  (!sc->sample_size && !sc->sample_count))) ||
5293  (sc->sample_count && (!sc->chunk_count ||
5294  (!sc->sample_size && !sc->sample_sizes)))) {
5295  av_log(log_obj, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
5296  index);
5297  return 1;
5298  }
5299 
5300  if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
5301  av_log(log_obj, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
5302  index);
5303  return 2;
5304  }
5305  return 0;
5306 }
5307 
5309 {
5310  AVStream *st;
5311  MOVStreamContext *sc;
5312  int ret;
5313 
5314  st = avformat_new_stream(c->fc, NULL);
5315  if (!st) return AVERROR(ENOMEM);
5316  st->id = -1;
5317  sc = av_mallocz(sizeof(MOVStreamContext));
5318  if (!sc) return AVERROR(ENOMEM);
5319 
5320  st->priv_data = sc;
5322  sc->ffindex = st->index;
5323  c->trak_index = st->index;
5324  sc->refcount = 1;
5325 
5326  if ((ret = mov_read_default(c, pb, atom)) < 0)
5327  return ret;
5328 
5329  c->trak_index = -1;
5330 
5331  // Here stsc refers to a chunk not described in stco. This is technically invalid,
5332  // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
5333  if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
5334  sc->stsc_count = 0;
5335  av_freep(&sc->stsc_data);
5336  }
5337 
5338  ret = sanity_checks(c->fc, sc, st->index);
5339  if (ret)
5340  return ret > 1 ? AVERROR_INVALIDDATA : 0;
5341 
5342  fix_timescale(c, sc);
5343 
5344  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
5345 
5346  /*
5347  * Advanced edit list support does not work with fragemented MP4s, which
5348  * have stsc, stsz, stco, and stts with zero entries in the moov atom.
5349  * In these files, trun atoms may be streamed in.
5350  */
5351  if (!sc->stts_count && c->advanced_editlist) {
5352 
5353  av_log(c->fc, AV_LOG_VERBOSE, "advanced_editlist does not work with fragmented "
5354  "MP4. disabling.\n");
5355  c->advanced_editlist = 0;
5356  c->advanced_editlist_autodisabled = 1;
5357  }
5358 
5359  mov_build_index(c, st);
5360 
5361 #if CONFIG_IAMFDEC
5362  if (sc->iamf) {
5363  ret = mov_update_iamf_streams(c, st);
5364  if (ret < 0)
5365  return ret;
5366  }
5367 #endif
5368 
5369  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
5370  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
5371  if (c->enable_drefs) {
5372  if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
5373  av_log(c->fc, AV_LOG_ERROR,
5374  "stream %d, error opening alias: path='%s', dir='%s', "
5375  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
5376  st->index, dref->path, dref->dir, dref->filename,
5377  dref->volume, dref->nlvl_from, dref->nlvl_to);
5378  } else {
5379  av_log(c->fc, AV_LOG_WARNING,
5380  "Skipped opening external track: "
5381  "stream %d, alias: path='%s', dir='%s', "
5382  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
5383  "Set enable_drefs to allow this.\n",
5384  st->index, dref->path, dref->dir, dref->filename,
5385  dref->volume, dref->nlvl_from, dref->nlvl_to);
5386  }
5387  } else {
5388  sc->pb = c->fc->pb;
5389  sc->pb_is_copied = 1;
5390  }
5391 
5392  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
5393  int stts_constant = sc->stts_count && sc->tts_count;
5394  if (sc->h_spacing && sc->v_spacing)
5396  sc->h_spacing, sc->v_spacing, INT_MAX);
5397  if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
5398  sc->height && sc->width &&
5399  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
5401  (int64_t)st->codecpar->height * sc->width,
5402  (int64_t)st->codecpar->width * sc->height, INT_MAX);
5403  }
5404 
5405 #if FF_API_R_FRAME_RATE
5406  for (unsigned int i = 1; sc->stts_count && i + 1 < sc->tts_count; i++) {
5407  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5408  continue;
5409  stts_constant = 0;
5410  }
5411  if (stts_constant)
5413  sc->time_scale, sc->tts_data[0].duration, INT_MAX);
5414 #endif
5415  }
5416 
5417 #if CONFIG_H261_DECODER || CONFIG_H263_DECODER || CONFIG_MPEG4_DECODER
5418  switch (st->codecpar->codec_id) {
5419 #if CONFIG_H261_DECODER
5420  case AV_CODEC_ID_H261:
5421 #endif
5422 #if CONFIG_H263_DECODER
5423  case AV_CODEC_ID_H263:
5424 #endif
5425 #if CONFIG_MPEG4_DECODER
5426  case AV_CODEC_ID_MPEG4:
5427 #endif
5428  st->codecpar->width = 0; /* let decoder init width/height */
5429  st->codecpar->height= 0;
5430  break;
5431  }
5432 #endif
5433 
5434  // If the duration of the mp3 packets is not constant, then they could need a parser
5435  if (st->codecpar->codec_id == AV_CODEC_ID_MP3
5436  && sc->time_scale == st->codecpar->sample_rate) {
5437  int stts_constant = 1;
5438  for (int i = 1; sc->stts_count && i < sc->tts_count; i++) {
5439  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5440  continue;
5441  stts_constant = 0;
5442  }
5443  if (!stts_constant)
5445  }
5446  /* Do not need those anymore. */
5447  av_freep(&sc->chunk_offsets);
5448  av_freep(&sc->sample_sizes);
5449  av_freep(&sc->keyframes);
5450  av_freep(&sc->stps_data);
5451  av_freep(&sc->elst_data);
5452  av_freep(&sc->rap_group);
5453  av_freep(&sc->sync_group);
5454  av_freep(&sc->sgpd_sync);
5455 
5456  return 0;
5457 }
5458 
5460 {
5461  int ret;
5462  c->itunes_metadata = 1;
5463  ret = mov_read_default(c, pb, atom);
5464  c->itunes_metadata = 0;
5465  return ret;
5466 }
5467 
5469 {
5470  uint32_t count;
5471  uint32_t i;
5472 
5473  if (atom.size < 8)
5474  return 0;
5475 
5476  avio_skip(pb, 4);
5477  count = avio_rb32(pb);
5478  atom.size -= 8;
5479  if (count >= UINT_MAX / sizeof(*c->meta_keys)) {
5480  av_log(c->fc, AV_LOG_ERROR,
5481  "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
5482  return AVERROR_INVALIDDATA;
5483  }
5484 
5485  c->meta_keys_count = count + 1;
5486  c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
5487  if (!c->meta_keys)
5488  return AVERROR(ENOMEM);
5489 
5490  for (i = 1; i <= count; ++i) {
5491  uint32_t key_size = avio_rb32(pb);
5492  uint32_t type = avio_rl32(pb);
5493  if (key_size < 8 || key_size > atom.size) {
5494  av_log(c->fc, AV_LOG_ERROR,
5495  "The key# %"PRIu32" in meta has invalid size:"
5496  "%"PRIu32"\n", i, key_size);
5497  return AVERROR_INVALIDDATA;
5498  }
5499  atom.size -= key_size;
5500  key_size -= 8;
5501  if (type != MKTAG('m','d','t','a')) {
5502  avio_skip(pb, key_size);
5503  continue;
5504  }
5505  c->meta_keys[i] = av_mallocz(key_size + 1);
5506  if (!c->meta_keys[i])
5507  return AVERROR(ENOMEM);
5508  avio_read(pb, c->meta_keys[i], key_size);
5509  }
5510 
5511  return 0;
5512 }
5513 
5515 {
5516  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
5517  uint8_t *key = NULL, *val = NULL, *mean = NULL;
5518  int i;
5519  int ret = 0;
5520  AVStream *st;
5521  MOVStreamContext *sc;
5522 
5523  if (c->fc->nb_streams < 1)
5524  return 0;
5525  st = c->fc->streams[c->fc->nb_streams-1];
5526  sc = st->priv_data;
5527 
5528  for (i = 0; i < 3; i++) {
5529  uint8_t **p;
5530  uint32_t len, tag;
5531 
5532  if (end - avio_tell(pb) <= 12)
5533  break;
5534 
5535  len = avio_rb32(pb);
5536  tag = avio_rl32(pb);
5537  avio_skip(pb, 4); // flags
5538 
5539  if (len < 12 || len - 12 > end - avio_tell(pb))
5540  break;
5541  len -= 12;
5542 
5543  if (tag == MKTAG('m', 'e', 'a', 'n'))
5544  p = &mean;
5545  else if (tag == MKTAG('n', 'a', 'm', 'e'))
5546  p = &key;
5547  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
5548  avio_skip(pb, 4);
5549  len -= 4;
5550  p = &val;
5551  } else
5552  break;
5553 
5554  if (*p)
5555  break;
5556 
5557  *p = av_malloc(len + 1);
5558  if (!*p) {
5559  ret = AVERROR(ENOMEM);
5560  break;
5561  }
5562  ret = ffio_read_size(pb, *p, len);
5563  if (ret < 0) {
5564  av_freep(p);
5565  break;
5566  }
5567  (*p)[len] = 0;
5568  }
5569 
5570  if (mean && key && val) {
5571  if (strcmp(key, "iTunSMPB") == 0) {
5572  int priming, remainder, samples;
5573  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
5574  if(priming>0 && priming<16384)
5575  sc->start_pad = priming;
5576  }
5577  }
5578  if (strcmp(mean, "com.apple.iTunes") == 0 &&
5579  strcmp(key, "DISCSUBTITLE") == 0) {
5580  av_dict_set(&c->fc->metadata, "disc_subtitle", val,
5582  val = NULL;
5583  }
5584  if (strcmp(key, "cdec") != 0) {
5585  av_dict_set(&c->fc->metadata, key, val,
5587  key = val = NULL;
5588  }
5589  } else {
5590  av_log(c->fc, AV_LOG_VERBOSE,
5591  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
5592  }
5593 
5594  avio_seek(pb, end, SEEK_SET);
5595  av_freep(&key);
5596  av_freep(&val);
5597  av_freep(&mean);
5598  return ret;
5599 }
5600 
5602 {
5603  MOVStreamContext *sc;
5604  AVStream *st;
5605 
5606  st = avformat_new_stream(c->fc, NULL);
5607  if (!st)
5608  return AVERROR(ENOMEM);
5609  sc = av_mallocz(sizeof(MOVStreamContext));
5610  if (!sc)
5611  goto fail;
5612 
5613  item->st = st;
5614  st->id = item->item_id;
5615  st->priv_data = sc;
5617  st->codecpar->codec_id = mov_codec_id(st, item->type);
5618  sc->id = st->id;
5619  sc->ffindex = st->index;
5620  st->avg_frame_rate.num = st->avg_frame_rate.den = 1;
5621  st->time_base.num = st->time_base.den = 1;
5622  st->nb_frames = 1;
5623  sc->time_scale = 1;
5624  sc->pb = c->fc->pb;
5625  sc->pb_is_copied = 1;
5626  sc->refcount = 1;
5627 
5628  if (item->name)
5629  av_dict_set(&st->metadata, "title", item->name, 0);
5630 
5631  // Populate the necessary fields used by mov_build_index.
5632  sc->stsc_data = av_malloc_array(1, sizeof(*sc->stsc_data));
5633  if (!sc->stsc_data)
5634  goto fail;
5635  sc->stsc_count = 1;
5636  sc->stsc_data[0].first = 1;
5637  sc->stsc_data[0].count = 1;
5638  sc->stsc_data[0].id = 1;
5639  sc->chunk_offsets = av_malloc_array(1, sizeof(*sc->chunk_offsets));
5640  if (!sc->chunk_offsets)
5641  goto fail;
5642  sc->chunk_count = 1;
5643  sc->stts_data = av_malloc_array(1, sizeof(*sc->stts_data));
5644  if (!sc->stts_data)
5645  goto fail;
5646  sc->stts_count = 1;
5647  sc->stts_data[0].count = 1;
5648  // Not used for still images. But needed by mov_build_index.
5649  sc->stts_data[0].duration = 0;
5650 
5651  return 0;
5652 fail:
5653  mov_free_stream_context(c->fc, st);
5654  ff_remove_stream(c->fc, st);
5655  item->st = NULL;
5656 
5657  return AVERROR(ENOMEM);
5658 }
5659 
5661 {
5662  while (atom.size > 8) {
5663  uint32_t tag;
5664  if (avio_feof(pb))
5665  return AVERROR_EOF;
5666  tag = avio_rl32(pb);
5667  atom.size -= 4;
5668  if (tag == MKTAG('h','d','l','r')) {
5669  avio_seek(pb, -8, SEEK_CUR);
5670  atom.size += 8;
5671  return mov_read_default(c, pb, atom);
5672  }
5673  }
5674  return 0;
5675 }
5676 
5677 // return 1 when matrix is identity, 0 otherwise
5678 #define IS_MATRIX_IDENT(matrix) \
5679  ( (matrix)[0][0] == (1 << 16) && \
5680  (matrix)[1][1] == (1 << 16) && \
5681  (matrix)[2][2] == (1 << 30) && \
5682  !(matrix)[0][1] && !(matrix)[0][2] && \
5683  !(matrix)[1][0] && !(matrix)[1][2] && \
5684  !(matrix)[2][0] && !(matrix)[2][1])
5685 
5687 {
5688  int i, j, e;
5689  int width;
5690  int height;
5691  int display_matrix[3][3];
5692  int res_display_matrix[3][3] = { { 0 } };
5693  AVStream *st;
5694  MOVStreamContext *sc;
5695  int version;
5696  int flags;
5697 
5698  if (c->fc->nb_streams < 1)
5699  return 0;
5700  st = c->fc->streams[c->fc->nb_streams-1];
5701  sc = st->priv_data;
5702 
5703  // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
5704  // avoids corrupting AVStreams mapped to an earlier tkhd.
5705  if (st->id != -1)
5706  return AVERROR_INVALIDDATA;
5707 
5708  version = avio_r8(pb);
5709  flags = avio_rb24(pb);
5711 
5712  if (version == 1) {
5713  avio_rb64(pb);
5714  avio_rb64(pb);
5715  } else {
5716  avio_rb32(pb); /* creation time */
5717  avio_rb32(pb); /* modification time */
5718  }
5719  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
5720  sc->id = st->id;
5721  avio_rb32(pb); /* reserved */
5722 
5723  /* highlevel (considering edits) duration in movie timebase */
5724  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
5725  avio_rb32(pb); /* reserved */
5726  avio_rb32(pb); /* reserved */
5727 
5728  avio_rb16(pb); /* layer */
5729  avio_rb16(pb); /* alternate group */
5730  avio_rb16(pb); /* volume */
5731  avio_rb16(pb); /* reserved */
5732 
5733  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
5734  // they're kept in fixed point format through all calculations
5735  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
5736  // side data, but the scale factor is not needed to calculate aspect ratio
5737  for (i = 0; i < 3; i++) {
5738  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
5739  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
5740  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
5741  }
5742 
5743  width = avio_rb32(pb); // 16.16 fixed point track width
5744  height = avio_rb32(pb); // 16.16 fixed point track height
5745  sc->width = width >> 16;
5746  sc->height = height >> 16;
5747 
5748  // apply the moov display matrix (after the tkhd one)
5749  for (i = 0; i < 3; i++) {
5750  const int sh[3] = { 16, 16, 30 };
5751  for (j = 0; j < 3; j++) {
5752  for (e = 0; e < 3; e++) {
5753  res_display_matrix[i][j] +=
5754  ((int64_t) display_matrix[i][e] *
5755  c->movie_display_matrix[e][j]) >> sh[e];
5756  }
5757  }
5758  }
5759 
5760  // save the matrix when it is not the default identity
5761  if (!IS_MATRIX_IDENT(res_display_matrix)) {
5762  av_freep(&sc->display_matrix);
5763  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
5764  if (!sc->display_matrix)
5765  return AVERROR(ENOMEM);
5766 
5767  for (i = 0; i < 3; i++)
5768  for (j = 0; j < 3; j++)
5769  sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
5770  }
5771 
5772  // transform the display width/height according to the matrix
5773  // to keep the same scale, use [width height 1<<16]
5774  if (width && height && sc->display_matrix) {
5775  double disp_transform[2];
5776 
5777  for (i = 0; i < 2; i++)
5778  disp_transform[i] = hypot(sc->display_matrix[0 + i],
5779  sc->display_matrix[3 + i]);
5780 
5781  if (disp_transform[0] > 1 && disp_transform[1] > 1 &&
5782  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
5783  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
5785  disp_transform[0] / disp_transform[1],
5786  INT_MAX);
5787  }
5788  return 0;
5789 }
5790 
5792 {
5793  MOVFragment *frag = &c->fragment;
5794  MOVTrackExt *trex = NULL;
5795  int flags, track_id, i;
5796  MOVFragmentStreamInfo * frag_stream_info;
5797 
5798  avio_r8(pb); /* version */
5799  flags = avio_rb24(pb);
5800 
5801  track_id = avio_rb32(pb);
5802  if (!track_id)
5803  return AVERROR_INVALIDDATA;
5804  for (i = 0; i < c->trex_count; i++)
5805  if (c->trex_data[i].track_id == track_id) {
5806  trex = &c->trex_data[i];
5807  break;
5808  }
5809  if (!trex) {
5810  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
5811  return 0;
5812  }
5813  c->fragment.found_tfhd = 1;
5814  frag->track_id = track_id;
5815  set_frag_stream(&c->frag_index, track_id);
5816 
5819  frag->moof_offset : frag->implicit_offset;
5820  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
5821 
5823  avio_rb32(pb) : trex->duration;
5824  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
5825  avio_rb32(pb) : trex->size;
5826  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
5827  avio_rb32(pb) : trex->flags;
5828  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
5829 
5830  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5831  if (frag_stream_info) {
5832  frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
5833  frag_stream_info->stsd_id = frag->stsd_id;
5834  }
5835  return 0;
5836 }
5837 
5839 {
5840  unsigned i, num;
5841  void *new_tracks;
5842 
5843  num = atom.size / 4;
5844  if (!(new_tracks = av_malloc_array(num, sizeof(int))))
5845  return AVERROR(ENOMEM);
5846 
5847  av_free(c->chapter_tracks);
5848  c->chapter_tracks = new_tracks;
5849  c->nb_chapter_tracks = num;
5850 
5851  for (i = 0; i < num && !pb->eof_reached; i++)
5852  c->chapter_tracks[i] = avio_rb32(pb);
5853 
5854  c->nb_chapter_tracks = i;
5855 
5856  return 0;
5857 }
5858 
5860 {
5861  MOVTrackExt *trex;
5862  int err;
5863 
5864  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
5865  return AVERROR_INVALIDDATA;
5866  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
5867  sizeof(*c->trex_data))) < 0) {
5868  c->trex_count = 0;
5869  return err;
5870  }
5871 
5872  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
5873 
5874  trex = &c->trex_data[c->trex_count++];
5875  avio_r8(pb); /* version */
5876  avio_rb24(pb); /* flags */
5877  trex->track_id = avio_rb32(pb);
5878  trex->stsd_id = avio_rb32(pb);
5879  trex->duration = avio_rb32(pb);
5880  trex->size = avio_rb32(pb);
5881  trex->flags = avio_rb32(pb);
5882  return 0;
5883 }
5884 
5886 {
5887  MOVFragment *frag = &c->fragment;
5888  AVStream *st = NULL;
5889  MOVStreamContext *sc;
5890  int version, i;
5891  MOVFragmentStreamInfo * frag_stream_info;
5892  int64_t base_media_decode_time;
5893 
5894  for (i = 0; i < c->fc->nb_streams; i++) {
5895  sc = c->fc->streams[i]->priv_data;
5896  if (sc->id == frag->track_id) {
5897  st = c->fc->streams[i];
5898  break;
5899  }
5900  }
5901  if (!st) {
5902  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5903  return 0;
5904  }
5905  sc = st->priv_data;
5906  if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5907  return 0;
5908  version = avio_r8(pb);
5909  avio_rb24(pb); /* flags */
5910  if (version) {
5911  base_media_decode_time = avio_rb64(pb);
5912  } else {
5913  base_media_decode_time = avio_rb32(pb);
5914  }
5915 
5916  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5917  if (frag_stream_info)
5918  frag_stream_info->tfdt_dts = base_media_decode_time;
5919  sc->track_end = base_media_decode_time;
5920 
5921  return 0;
5922 }
5923 
5925 {
5926  MOVFragment *frag = &c->fragment;
5927  AVStream *st = NULL;
5928  FFStream *sti = NULL;
5929  MOVStreamContext *sc;
5930  MOVTimeToSample *tts_data;
5931  uint64_t offset;
5932  int64_t dts, pts = AV_NOPTS_VALUE;
5933  int data_offset = 0;
5934  unsigned entries, first_sample_flags = frag->flags;
5935  int flags, distance, i;
5936  int64_t prev_dts = AV_NOPTS_VALUE;
5937  int next_frag_index = -1, index_entry_pos;
5938  size_t requested_size;
5939  size_t old_allocated_size;
5940  AVIndexEntry *new_entries;
5941  MOVFragmentStreamInfo * frag_stream_info;
5942 
5943  if (!frag->found_tfhd) {
5944  av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
5945  return AVERROR_INVALIDDATA;
5946  }
5947 
5948  for (i = 0; i < c->fc->nb_streams; i++) {
5949  sc = c->fc->streams[i]->priv_data;
5950  if (sc->id == frag->track_id) {
5951  st = c->fc->streams[i];
5952  sti = ffstream(st);
5953  break;
5954  }
5955  }
5956  if (!st) {
5957  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5958  return 0;
5959  }
5960  sc = st->priv_data;
5961  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5962  return 0;
5963 
5964  // Find the next frag_index index that has a valid index_entry for
5965  // the current track_id.
5966  //
5967  // A valid index_entry means the trun for the fragment was read
5968  // and it's samples are in index_entries at the given position.
5969  // New index entries will be inserted before the index_entry found.
5970  index_entry_pos = sti->nb_index_entries;
5971  for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
5972  frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
5973  if (frag_stream_info && frag_stream_info->index_entry >= 0) {
5974  next_frag_index = i;
5975  index_entry_pos = frag_stream_info->index_entry;
5976  break;
5977  }
5978  }
5979  av_assert0(index_entry_pos <= sti->nb_index_entries);
5980 
5981  avio_r8(pb); /* version */
5982  flags = avio_rb24(pb);
5983  entries = avio_rb32(pb);
5984  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
5985 
5986  if ((uint64_t)entries+sc->tts_count >= UINT_MAX/sizeof(*sc->tts_data))
5987  return AVERROR_INVALIDDATA;
5988  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
5989  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
5990 
5991  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5992  if (frag_stream_info) {
5993  if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
5994  dts = frag_stream_info->next_trun_dts - sc->time_offset;
5995  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
5996  c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
5997  pts = frag_stream_info->first_tfra_pts;
5998  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
5999  ", using it for pts\n", pts);
6000  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
6001  c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
6002  dts = frag_stream_info->first_tfra_pts;
6003  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
6004  ", using it for dts\n", pts);
6005  } else {
6006  int has_tfdt = frag_stream_info->tfdt_dts != AV_NOPTS_VALUE;
6007  int has_sidx = frag_stream_info->sidx_pts != AV_NOPTS_VALUE;
6008  int fallback_tfdt = !c->use_tfdt && !has_sidx && has_tfdt;
6009  int fallback_sidx = c->use_tfdt && !has_tfdt && has_sidx;
6010 
6011  if (fallback_sidx) {
6012  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt set but no tfdt found, using sidx instead\n");
6013  }
6014  if (fallback_tfdt) {
6015  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt not set but no sidx found, using tfdt instead\n");
6016  }
6017 
6018  if (has_tfdt && c->use_tfdt || fallback_tfdt) {
6019  dts = frag_stream_info->tfdt_dts - sc->time_offset;
6020  av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
6021  ", using it for dts\n", dts);
6022  } else if (has_sidx && !c->use_tfdt || fallback_sidx) {
6023  // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
6024  // pts = frag_stream_info->sidx_pts;
6025  dts = frag_stream_info->sidx_pts;
6026  av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
6027  ", using it for dts\n", frag_stream_info->sidx_pts);
6028  } else {
6029  dts = sc->track_end - sc->time_offset;
6030  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
6031  ", using it for dts\n", dts);
6032  }
6033  }
6034  } else {
6035  dts = sc->track_end - sc->time_offset;
6036  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
6037  ", using it for dts\n", dts);
6038  }
6039  offset = frag->base_data_offset + data_offset;
6040  distance = 0;
6041  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
6042 
6043  // realloc space for new index entries
6044  if ((uint64_t)sti->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
6045  entries = UINT_MAX / sizeof(AVIndexEntry) - sti->nb_index_entries;
6046  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
6047  }
6048  if (entries == 0)
6049  return 0;
6050 
6051  requested_size = (sti->nb_index_entries + entries) * sizeof(AVIndexEntry);
6052  new_entries = av_fast_realloc(sti->index_entries,
6054  requested_size);
6055  if (!new_entries)
6056  return AVERROR(ENOMEM);
6057  sti->index_entries= new_entries;
6058 
6059  requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->tts_data);
6060  old_allocated_size = sc->tts_allocated_size;
6061  tts_data = av_fast_realloc(sc->tts_data, &sc->tts_allocated_size,
6062  requested_size);
6063  if (!tts_data)
6064  return AVERROR(ENOMEM);
6065  sc->tts_data = tts_data;
6066 
6067  // In case there were samples without time to sample entries, ensure they get
6068  // zero valued entries. This ensures clips which mix boxes with and
6069  // without time to sample entries don't pickup uninitialized data.
6070  memset((uint8_t*)(sc->tts_data) + old_allocated_size, 0,
6071  sc->tts_allocated_size - old_allocated_size);
6072 
6073  if (index_entry_pos < sti->nb_index_entries) {
6074  // Make hole in index_entries and tts_data for new samples
6075  memmove(sti->index_entries + index_entry_pos + entries,
6076  sti->index_entries + index_entry_pos,
6077  sizeof(*sti->index_entries) *
6078  (sti->nb_index_entries - index_entry_pos));
6079  memmove(sc->tts_data + index_entry_pos + entries,
6080  sc->tts_data + index_entry_pos,
6081  sizeof(*sc->tts_data) * (sc->tts_count - index_entry_pos));
6082  if (index_entry_pos < sc->current_sample) {
6083  sc->current_sample += entries;
6084  }
6085  }
6086 
6087  sti->nb_index_entries += entries;
6088  sc->tts_count = sti->nb_index_entries;
6089  sc->stts_count = sti->nb_index_entries;
6090  if (flags & MOV_TRUN_SAMPLE_CTS)
6091  sc->ctts_count = sti->nb_index_entries;
6092 
6093  // Record the index_entry position in frag_index of this fragment
6094  if (frag_stream_info) {
6095  frag_stream_info->index_entry = index_entry_pos;
6096  if (frag_stream_info->index_base < 0)
6097  frag_stream_info->index_base = index_entry_pos;
6098  }
6099 
6100  if (index_entry_pos > 0)
6101  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
6102 
6103  for (i = 0; i < entries && !pb->eof_reached; i++) {
6104  unsigned sample_size = frag->size;
6105  int sample_flags = i ? frag->flags : first_sample_flags;
6106  unsigned sample_duration = frag->duration;
6107  unsigned ctts_duration = 0;
6108  int keyframe = 0;
6109  int index_entry_flags = 0;
6110 
6111  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
6112  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
6113  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
6114  if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
6115 
6116  mov_update_dts_shift(sc, ctts_duration, c->fc);
6117  if (pts != AV_NOPTS_VALUE) {
6118  dts = pts - sc->dts_shift;
6119  if (flags & MOV_TRUN_SAMPLE_CTS) {
6120  dts -= ctts_duration;
6121  } else {
6122  dts -= sc->time_offset;
6123  }
6124  av_log(c->fc, AV_LOG_DEBUG,
6125  "pts %"PRId64" calculated dts %"PRId64
6126  " sc->dts_shift %d ctts.duration %d"
6127  " sc->time_offset %"PRId64
6128  " flags & MOV_TRUN_SAMPLE_CTS %d\n",
6129  pts, dts,
6130  sc->dts_shift, ctts_duration,
6132  pts = AV_NOPTS_VALUE;
6133  }
6134 
6135  keyframe =
6136  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
6138  if (keyframe) {
6139  distance = 0;
6140  index_entry_flags |= AVINDEX_KEYFRAME;
6141  }
6142  // Fragments can overlap in time. Discard overlapping frames after
6143  // decoding.
6144  if (prev_dts >= dts)
6145  index_entry_flags |= AVINDEX_DISCARD_FRAME;
6146 
6147  sti->index_entries[index_entry_pos].pos = offset;
6148  sti->index_entries[index_entry_pos].timestamp = dts;
6149  sti->index_entries[index_entry_pos].size = sample_size;
6150  sti->index_entries[index_entry_pos].min_distance = distance;
6151  sti->index_entries[index_entry_pos].flags = index_entry_flags;
6152 
6153  sc->tts_data[index_entry_pos].count = 1;
6154  sc->tts_data[index_entry_pos].offset = ctts_duration;
6155  sc->tts_data[index_entry_pos].duration = sample_duration;
6156  index_entry_pos++;
6157 
6158  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
6159  "size %u, distance %d, keyframe %d\n", st->index,
6160  index_entry_pos, offset, dts, sample_size, distance, keyframe);
6161  distance++;
6162  if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
6163  return AVERROR_INVALIDDATA;
6164  if (!sample_size)
6165  return AVERROR_INVALIDDATA;
6166  dts += sample_duration;
6167  offset += sample_size;
6168  sc->data_size += sample_size;
6169 
6170  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
6171  1 <= INT_MAX - sc->nb_frames_for_fps
6172  ) {
6173  sc->duration_for_fps += sample_duration;
6174  sc->nb_frames_for_fps ++;
6175  }
6176  }
6177  if (frag_stream_info)
6178  frag_stream_info->next_trun_dts = dts + sc->time_offset;
6179  if (i < entries) {
6180  // EOF found before reading all entries. Fix the hole this would
6181  // leave in index_entries and tts_data
6182  int gap = entries - i;
6183  memmove(sti->index_entries + index_entry_pos,
6184  sti->index_entries + index_entry_pos + gap,
6185  sizeof(*sti->index_entries) *
6186  (sti->nb_index_entries - (index_entry_pos + gap)));
6187  memmove(sc->tts_data + index_entry_pos,
6188  sc->tts_data + index_entry_pos + gap,
6189  sizeof(*sc->tts_data) *
6190  (sc->tts_count - (index_entry_pos + gap)));
6191 
6192  sti->nb_index_entries -= gap;
6193  sc->tts_count -= gap;
6194  if (index_entry_pos < sc->current_sample) {
6195  sc->current_sample -= gap;
6196  }
6197  entries = i;
6198  }
6199 
6200  // The end of this new fragment may overlap in time with the start
6201  // of the next fragment in index_entries. Mark the samples in the next
6202  // fragment that overlap with AVINDEX_DISCARD_FRAME
6203  prev_dts = AV_NOPTS_VALUE;
6204  if (index_entry_pos > 0)
6205  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
6206  for (int i = index_entry_pos; i < sti->nb_index_entries; i++) {
6207  if (prev_dts < sti->index_entries[i].timestamp)
6208  break;
6210  }
6211 
6212  // If a hole was created to insert the new index_entries into,
6213  // the index_entry recorded for all subsequent moof must
6214  // be incremented by the number of entries inserted.
6215  fix_frag_index_entries(&c->frag_index, next_frag_index,
6216  frag->track_id, entries);
6217 
6218  if (pb->eof_reached) {
6219  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
6220  return AVERROR_EOF;
6221  }
6222 
6223  frag->implicit_offset = offset;
6224 
6225  sc->track_end = dts + sc->time_offset;
6226  if (st->duration < sc->track_end)
6227  st->duration = sc->track_end;
6228 
6229  return 0;
6230 }
6231 
6233 {
6234  int64_t stream_size = avio_size(pb);
6235  int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
6236  uint8_t version, is_complete;
6237  int64_t offadd;
6238  unsigned i, j, track_id, item_count;
6239  AVStream *st = NULL;
6240  AVStream *ref_st = NULL;
6241  MOVStreamContext *sc, *ref_sc = NULL;
6242  AVRational timescale;
6243 
6244  version = avio_r8(pb);
6245  if (version > 1) {
6246  avpriv_request_sample(c->fc, "sidx version %u", version);
6247  return 0;
6248  }
6249 
6250  avio_rb24(pb); // flags
6251 
6252  track_id = avio_rb32(pb); // Reference ID
6253  for (i = 0; i < c->fc->nb_streams; i++) {
6254  sc = c->fc->streams[i]->priv_data;
6255  if (sc->id == track_id) {
6256  st = c->fc->streams[i];
6257  break;
6258  }
6259  }
6260  if (!st) {
6261  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
6262  return 0;
6263  }
6264 
6265  sc = st->priv_data;
6266 
6267  timescale = av_make_q(1, avio_rb32(pb));
6268 
6269  if (timescale.den <= 0) {
6270  av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
6271  return AVERROR_INVALIDDATA;
6272  }
6273 
6274  if (version == 0) {
6275  pts = avio_rb32(pb);
6276  offadd= avio_rb32(pb);
6277  } else {
6278  pts = avio_rb64(pb);
6279  offadd= avio_rb64(pb);
6280  }
6281  if (av_sat_add64(offset, offadd) != offset + (uint64_t)offadd)
6282  return AVERROR_INVALIDDATA;
6283 
6284  offset += (uint64_t)offadd;
6285 
6286  avio_rb16(pb); // reserved
6287 
6288  item_count = avio_rb16(pb);
6289  if (item_count == 0)
6290  return AVERROR_INVALIDDATA;
6291 
6292  for (i = 0; i < item_count; i++) {
6293  int index;
6294  MOVFragmentStreamInfo * frag_stream_info;
6295  uint32_t size = avio_rb32(pb);
6296  uint32_t duration = avio_rb32(pb);
6297  if (size & 0x80000000) {
6298  avpriv_request_sample(c->fc, "sidx reference_type 1");
6299  return AVERROR_PATCHWELCOME;
6300  }
6301  avio_rb32(pb); // sap_flags
6302  timestamp = av_rescale_q(pts, timescale, st->time_base);
6303 
6305  frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
6306  if (frag_stream_info)
6307  frag_stream_info->sidx_pts = timestamp;
6308 
6309  if (av_sat_add64(offset, size) != offset + (uint64_t)size ||
6310  av_sat_add64(pts, duration) != pts + (uint64_t)duration
6311  )
6312  return AVERROR_INVALIDDATA;
6313  offset += size;
6314  pts += duration;
6315  }
6316 
6317  st->duration = sc->track_end = pts;
6318 
6319  sc->has_sidx = 1;
6320 
6321  // See if the remaining bytes are just an mfra which we can ignore.
6322  is_complete = offset == stream_size;
6323  if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL) && stream_size > 0 ) {
6324  int64_t ret;
6325  int64_t original_pos = avio_tell(pb);
6326  if (!c->have_read_mfra_size) {
6327  if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
6328  return ret;
6329  c->mfra_size = avio_rb32(pb);
6330  c->have_read_mfra_size = 1;
6331  if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
6332  return ret;
6333  }
6334  if (offset == stream_size - c->mfra_size)
6335  is_complete = 1;
6336  }
6337 
6338  if (is_complete) {
6339  // Find first entry in fragment index that came from an sidx.
6340  // This will pretty much always be the first entry.
6341  for (i = 0; i < c->frag_index.nb_items; i++) {
6342  MOVFragmentIndexItem * item = &c->frag_index.item[i];
6343  for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
6344  MOVFragmentStreamInfo * si;
6345  si = &item->stream_info[j];
6346  if (si->sidx_pts != AV_NOPTS_VALUE) {
6347  ref_st = c->fc->streams[j];
6348  ref_sc = ref_st->priv_data;
6349  break;
6350  }
6351  }
6352  }
6353  if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
6354  st = c->fc->streams[i];
6355  sc = st->priv_data;
6356  if (!sc->has_sidx) {
6357  st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
6358  }
6359  }
6360 
6361  if (offadd == 0)
6362  c->frag_index.complete = 1;
6363  }
6364 
6365  return 0;
6366 }
6367 
6368 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
6369 /* like the files created with Adobe Premiere 5.0, for samples see */
6370 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
6372 {
6373  int err;
6374 
6375  if (atom.size < 8)
6376  return 0; /* continue */
6377  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
6378  avio_skip(pb, atom.size - 4);
6379  return 0;
6380  }
6381  atom.type = avio_rl32(pb);
6382  atom.size -= 8;
6383  if (atom.type != MKTAG('m','d','a','t')) {
6384  avio_skip(pb, atom.size);
6385  return 0;
6386  }
6387  err = mov_read_mdat(c, pb, atom);
6388  return err;
6389 }
6390 
6392 {
6393 #if CONFIG_ZLIB
6394  FFIOContext ctx;
6395  uint8_t *cmov_data;
6396  uint8_t *moov_data; /* uncompressed data */
6397  long cmov_len, moov_len;
6398  int ret = -1;
6399 
6400  avio_rb32(pb); /* dcom atom */
6401  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
6402  return AVERROR_INVALIDDATA;
6403  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
6404  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
6405  return AVERROR_INVALIDDATA;
6406  }
6407  avio_rb32(pb); /* cmvd atom */
6408  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
6409  return AVERROR_INVALIDDATA;
6410  moov_len = avio_rb32(pb); /* uncompressed size */
6411  cmov_len = atom.size - 6 * 4;
6412 
6413  cmov_data = av_malloc(cmov_len);
6414  if (!cmov_data)
6415  return AVERROR(ENOMEM);
6416  moov_data = av_malloc(moov_len);
6417  if (!moov_data) {
6418  av_free(cmov_data);
6419  return AVERROR(ENOMEM);
6420  }
6421  ret = ffio_read_size(pb, cmov_data, cmov_len);
6422  if (ret < 0)
6423  goto free_and_return;
6424 
6426  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
6427  goto free_and_return;
6428  ffio_init_read_context(&ctx, moov_data, moov_len);
6429  ctx.pub.seekable = AVIO_SEEKABLE_NORMAL;
6430  atom.type = MKTAG('m','o','o','v');
6431  atom.size = moov_len;
6432  ret = mov_read_default(c, &ctx.pub, atom);
6433 free_and_return:
6434  av_free(moov_data);
6435  av_free(cmov_data);
6436  return ret;
6437 #else
6438  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
6439  return AVERROR(ENOSYS);
6440 #endif
6441 }
6442 
6443 /* edit list atom */
6445 {
6446  MOVStreamContext *sc;
6447  int i, edit_count, version;
6448  int64_t elst_entry_size;
6449 
6450  if (c->fc->nb_streams < 1 || c->ignore_editlist)
6451  return 0;
6452  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
6453 
6454  version = avio_r8(pb); /* version */
6455  avio_rb24(pb); /* flags */
6456  edit_count = avio_rb32(pb); /* entries */
6457  atom.size -= 8;
6458 
6459  elst_entry_size = version == 1 ? 20 : 12;
6460  if (atom.size != edit_count * elst_entry_size) {
6461  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6462  av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
6463  edit_count, atom.size + 8);
6464  return AVERROR_INVALIDDATA;
6465  } else {
6466  edit_count = atom.size / elst_entry_size;
6467  if (edit_count * elst_entry_size != atom.size) {
6468  av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
6469  }
6470  }
6471  }
6472 
6473  if (!edit_count)
6474  return 0;
6475  if (sc->elst_data)
6476  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
6477  av_free(sc->elst_data);
6478  sc->elst_count = 0;
6479  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
6480  if (!sc->elst_data)
6481  return AVERROR(ENOMEM);
6482 
6483  av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
6484  for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
6485  MOVElst *e = &sc->elst_data[i];
6486 
6487  if (version == 1) {
6488  e->duration = avio_rb64(pb);
6489  e->time = avio_rb64(pb);
6490  atom.size -= 16;
6491  } else {
6492  e->duration = avio_rb32(pb); /* segment duration */
6493  e->time = (int32_t)avio_rb32(pb); /* media time */
6494  atom.size -= 8;
6495  }
6496  e->rate = avio_rb32(pb) / 65536.0;
6497  atom.size -= 4;
6498  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
6499  e->duration, e->time, e->rate);
6500 
6501  if (e->time < 0 && e->time != -1 &&
6502  c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6503  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
6504  c->fc->nb_streams-1, i, e->time);
6505  return AVERROR_INVALIDDATA;
6506  }
6507  if (e->duration < 0) {
6508  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list duration=%"PRId64"\n",
6509  c->fc->nb_streams-1, i, e->duration);
6510  return AVERROR_INVALIDDATA;
6511  }
6512  }
6513  sc->elst_count = i;
6514 
6515  return 0;
6516 }
6517 
6519 {
6520  MOVStreamContext *sc;
6521  int err;
6522 
6523  if (c->fc->nb_streams < 1)
6524  return AVERROR_INVALIDDATA;
6525  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6526 
6527  if (atom.size % sizeof(uint32_t))
6528  return AVERROR_INVALIDDATA;
6529 
6530  MovTref *tag = mov_add_tref_tag(sc, atom.type);
6531  if (!tag)
6532  return AVERROR(ENOMEM);
6533 
6534  int nb_timecode_track = atom.size >> 2;
6535  for (int i = 0; i < nb_timecode_track; i++) {
6536  if (avio_feof(pb))
6537  return AVERROR_INVALIDDATA;
6538  err = mov_add_tref_id(tag, avio_rb32(pb));
6539  if (err < 0)
6540  return err;
6541  }
6542 
6543  return 0;
6544 }
6545 
6547 {
6548  AVStream *st;
6549  int version, color_range, color_primaries, color_trc, color_space;
6550 
6551  if (c->fc->nb_streams < 1)
6552  return 0;
6553  st = c->fc->streams[c->fc->nb_streams - 1];
6554 
6555  if (atom.size < 5) {
6556  av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
6557  return AVERROR_INVALIDDATA;
6558  }
6559 
6560  version = avio_r8(pb);
6561  if (version != 1) {
6562  av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
6563  return 0;
6564  }
6565  avio_skip(pb, 3); /* flags */
6566 
6567  avio_skip(pb, 2); /* profile + level */
6568  color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
6569  color_primaries = avio_r8(pb);
6570  color_trc = avio_r8(pb);
6571  color_space = avio_r8(pb);
6572  if (avio_rb16(pb)) /* codecIntializationDataSize */
6573  return AVERROR_INVALIDDATA;
6574 
6577  if (!av_color_transfer_name(color_trc))
6578  color_trc = AVCOL_TRC_UNSPECIFIED;
6579  if (!av_color_space_name(color_space))
6580  color_space = AVCOL_SPC_UNSPECIFIED;
6581 
6584  st->codecpar->color_trc = color_trc;
6585  st->codecpar->color_space = color_space;
6586 
6587  return 0;
6588 }
6589 
6591 {
6592  MOVStreamContext *sc;
6593  int i, version;
6594 
6595  if (c->fc->nb_streams < 1)
6596  return AVERROR_INVALIDDATA;
6597 
6598  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6599 
6600  if (atom.size < 5) {
6601  av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
6602  return AVERROR_INVALIDDATA;
6603  }
6604 
6605  version = avio_r8(pb);
6606  if (version) {
6607  av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
6608  return 0;
6609  }
6610  if (sc->mastering) {
6611  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Metadata\n");
6612  return 0;
6613  }
6614 
6615  avio_skip(pb, 3); /* flags */
6616 
6618  if (!sc->mastering)
6619  return AVERROR(ENOMEM);
6620 
6621  for (i = 0; i < 3; i++) {
6622  sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
6623  sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
6624  }
6625  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
6626  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
6627 
6628  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
6629  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
6630 
6631  sc->mastering->has_primaries = 1;
6632  sc->mastering->has_luminance = 1;
6633 
6634  return 0;
6635 }
6636 
6638 {
6639  MOVStreamContext *sc;
6640  const int mapping[3] = {1, 2, 0};
6641  const int chroma_den = 50000;
6642  const int luma_den = 10000;
6643  int i;
6644 
6645  if (c->fc->nb_streams < 1)
6646  return AVERROR_INVALIDDATA;
6647 
6648  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6649 
6650  if (atom.size < 24) {
6651  av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
6652  return AVERROR_INVALIDDATA;
6653  }
6654 
6655  if (sc->mastering) {
6656  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Color Volume\n");
6657  return 0;
6658  }
6659 
6661  if (!sc->mastering)
6662  return AVERROR(ENOMEM);
6663 
6664  for (i = 0; i < 3; i++) {
6665  const int j = mapping[i];
6666  sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
6667  sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
6668  }
6669  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
6670  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
6671 
6672  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
6673  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
6674 
6675  sc->mastering->has_luminance = 1;
6676  sc->mastering->has_primaries = 1;
6677 
6678  return 0;
6679 }
6680 
6682 {
6683  MOVStreamContext *sc;
6684  int version;
6685 
6686  if (c->fc->nb_streams < 1)
6687  return AVERROR_INVALIDDATA;
6688 
6689  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6690 
6691  if (atom.size < 5) {
6692  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
6693  return AVERROR_INVALIDDATA;
6694  }
6695 
6696  version = avio_r8(pb);
6697  if (version) {
6698  av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
6699  return 0;
6700  }
6701  avio_skip(pb, 3); /* flags */
6702 
6703  if (sc->coll){
6704  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate COLL\n");
6705  return 0;
6706  }
6707 
6709  if (!sc->coll)
6710  return AVERROR(ENOMEM);
6711 
6712  sc->coll->MaxCLL = avio_rb16(pb);
6713  sc->coll->MaxFALL = avio_rb16(pb);
6714 
6715  return 0;
6716 }
6717 
6719 {
6720  MOVStreamContext *sc;
6721 
6722  if (c->fc->nb_streams < 1)
6723  return AVERROR_INVALIDDATA;
6724 
6725  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6726 
6727  if (atom.size < 4) {
6728  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
6729  return AVERROR_INVALIDDATA;
6730  }
6731 
6732  if (sc->coll){
6733  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate CLLI/COLL\n");
6734  return 0;
6735  }
6736 
6738  if (!sc->coll)
6739  return AVERROR(ENOMEM);
6740 
6741  sc->coll->MaxCLL = avio_rb16(pb);
6742  sc->coll->MaxFALL = avio_rb16(pb);
6743 
6744  return 0;
6745 }
6746 
6748 {
6749  MOVStreamContext *sc;
6750  const int illuminance_den = 10000;
6751  const int ambient_den = 50000;
6752  if (c->fc->nb_streams < 1)
6753  return AVERROR_INVALIDDATA;
6754  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6755  if (atom.size < 6) {
6756  av_log(c->fc, AV_LOG_ERROR, "Empty Ambient Viewing Environment Info box\n");
6757  return AVERROR_INVALIDDATA;
6758  }
6759  if (sc->ambient){
6760  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate AMVE\n");
6761  return 0;
6762  }
6764  if (!sc->ambient)
6765  return AVERROR(ENOMEM);
6766  sc->ambient->ambient_illuminance = av_make_q(avio_rb32(pb), illuminance_den);
6767  sc->ambient->ambient_light_x = av_make_q(avio_rb16(pb), ambient_den);
6768  sc->ambient->ambient_light_y = av_make_q(avio_rb16(pb), ambient_den);
6769  return 0;
6770 }
6771 
6773 {
6774  AVStream *st;
6775  MOVStreamContext *sc;
6776  enum AVStereo3DType type;
6777  int mode;
6778 
6779  if (c->fc->nb_streams < 1)
6780  return 0;
6781 
6782  st = c->fc->streams[c->fc->nb_streams - 1];
6783  sc = st->priv_data;
6784 
6785  if (atom.size < 5) {
6786  av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
6787  return AVERROR_INVALIDDATA;
6788  }
6789 
6790  if (sc->stereo3d)
6791  return AVERROR_INVALIDDATA;
6792 
6793  avio_skip(pb, 4); /* version + flags */
6794 
6795  mode = avio_r8(pb);
6796  switch (mode) {
6797  case 0:
6798  type = AV_STEREO3D_2D;
6799  break;
6800  case 1:
6802  break;
6803  case 2:
6805  break;
6806  default:
6807  av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
6808  return 0;
6809  }
6810 
6812  if (!sc->stereo3d)
6813  return AVERROR(ENOMEM);
6814 
6815  sc->stereo3d->type = type;
6816  return 0;
6817 }
6818 
6820 {
6821  AVStream *st;
6822  MOVStreamContext *sc;
6823  int size = 0;
6824  int64_t remaining;
6825  uint32_t tag = 0;
6827 
6828  if (c->fc->nb_streams < 1)
6829  return 0;
6830 
6831  st = c->fc->streams[c->fc->nb_streams - 1];
6832  sc = st->priv_data;
6833 
6834  remaining = atom.size;
6835  while (remaining > 0) {
6836  size = avio_rb32(pb);
6837  if (size < 8 || size > remaining ) {
6838  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in pack box\n");
6839  return AVERROR_INVALIDDATA;
6840  }
6841 
6842  tag = avio_rl32(pb);
6843  switch (tag) {
6844  case MKTAG('p','k','i','n'): {
6845  if (size != 16) {
6846  av_log(c->fc, AV_LOG_ERROR, "Invalid size of pkin box: %d\n", size);
6847  return AVERROR_INVALIDDATA;
6848  }
6849  avio_skip(pb, 1); // version
6850  avio_skip(pb, 3); // flags
6851 
6852  tag = avio_rl32(pb);
6853  switch (tag) {
6854  case MKTAG('s','i','d','e'):
6856  break;
6857  case MKTAG('o','v','e','r'):
6859  break;
6860  case 0:
6861  // This means value will be set in another layer
6862  break;
6863  default:
6864  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in pkin: %s\n",
6865  av_fourcc2str(tag));
6866  avio_skip(pb, size - 8);
6867  break;
6868  }
6869 
6870  break;
6871  }
6872  default:
6873  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in pack: %s\n",
6874  av_fourcc2str(tag));
6875  avio_skip(pb, size - 8);
6876  break;
6877  }
6878  remaining -= size;
6879  }
6880 
6881  if (remaining != 0) {
6882  av_log(c->fc, AV_LOG_ERROR, "Broken pack box\n");
6883  return AVERROR_INVALIDDATA;
6884  }
6885 
6886  if (type == AV_STEREO3D_2D)
6887  return 0;
6888 
6889  if (!sc->stereo3d) {
6891  if (!sc->stereo3d)
6892  return AVERROR(ENOMEM);
6893  }
6894 
6895  sc->stereo3d->type = type;
6896 
6897  return 0;
6898 }
6899 
6901 {
6902  AVStream *st;
6903  MOVStreamContext *sc;
6904  int size, version, layout;
6905  int32_t yaw, pitch, roll;
6906  uint32_t l = 0, t = 0, r = 0, b = 0;
6907  uint32_t tag, padding = 0;
6908  enum AVSphericalProjection projection;
6909 
6910  if (c->fc->nb_streams < 1)
6911  return 0;
6912 
6913  st = c->fc->streams[c->fc->nb_streams - 1];
6914  sc = st->priv_data;
6915 
6916  if (atom.size < 8) {
6917  av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
6918  return AVERROR_INVALIDDATA;
6919  }
6920 
6921  size = avio_rb32(pb);
6922  if (size <= 12 || size > atom.size)
6923  return AVERROR_INVALIDDATA;
6924 
6925  tag = avio_rl32(pb);
6926  if (tag != MKTAG('s','v','h','d')) {
6927  av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
6928  return 0;
6929  }
6930  version = avio_r8(pb);
6931  if (version != 0) {
6932  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6933  version);
6934  return 0;
6935  }
6936  avio_skip(pb, 3); /* flags */
6937  avio_skip(pb, size - 12); /* metadata_source */
6938 
6939  size = avio_rb32(pb);
6940  if (size > atom.size)
6941  return AVERROR_INVALIDDATA;
6942 
6943  tag = avio_rl32(pb);
6944  if (tag != MKTAG('p','r','o','j')) {
6945  av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
6946  return 0;
6947  }
6948 
6949  size = avio_rb32(pb);
6950  if (size > atom.size)
6951  return AVERROR_INVALIDDATA;
6952 
6953  tag = avio_rl32(pb);
6954  if (tag != MKTAG('p','r','h','d')) {
6955  av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
6956  return 0;
6957  }
6958  version = avio_r8(pb);
6959  if (version != 0) {
6960  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6961  version);
6962  return 0;
6963  }
6964  avio_skip(pb, 3); /* flags */
6965 
6966  /* 16.16 fixed point */
6967  yaw = avio_rb32(pb);
6968  pitch = avio_rb32(pb);
6969  roll = avio_rb32(pb);
6970 
6971  size = avio_rb32(pb);
6972  if (size > atom.size)
6973  return AVERROR_INVALIDDATA;
6974 
6975  tag = avio_rl32(pb);
6976  version = avio_r8(pb);
6977  if (version != 0) {
6978  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6979  version);
6980  return 0;
6981  }
6982  avio_skip(pb, 3); /* flags */
6983  switch (tag) {
6984  case MKTAG('c','b','m','p'):
6985  layout = avio_rb32(pb);
6986  if (layout) {
6987  av_log(c->fc, AV_LOG_WARNING,
6988  "Unsupported cubemap layout %d\n", layout);
6989  return 0;
6990  }
6991  projection = AV_SPHERICAL_CUBEMAP;
6992  padding = avio_rb32(pb);
6993  break;
6994  case MKTAG('e','q','u','i'):
6995  t = avio_rb32(pb);
6996  b = avio_rb32(pb);
6997  l = avio_rb32(pb);
6998  r = avio_rb32(pb);
6999 
7000  if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
7001  av_log(c->fc, AV_LOG_ERROR,
7002  "Invalid bounding rectangle coordinates "
7003  "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
7004  return AVERROR_INVALIDDATA;
7005  }
7006 
7007  if (l || t || r || b)
7008  projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
7009  else
7010  projection = AV_SPHERICAL_EQUIRECTANGULAR;
7011  break;
7012  default:
7013  av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
7014  return 0;
7015  }
7016 
7018  if (!sc->spherical)
7019  return AVERROR(ENOMEM);
7020 
7021  sc->spherical->projection = projection;
7022 
7023  sc->spherical->yaw = yaw;
7024  sc->spherical->pitch = pitch;
7025  sc->spherical->roll = roll;
7026 
7027  sc->spherical->padding = padding;
7028 
7029  sc->spherical->bound_left = l;
7030  sc->spherical->bound_top = t;
7031  sc->spherical->bound_right = r;
7032  sc->spherical->bound_bottom = b;
7033 
7034  return 0;
7035 }
7036 
7038 {
7039  AVStream *st;
7040  MOVStreamContext *sc;
7041  int size;
7042  uint32_t tag;
7043  enum AVSphericalProjection projection;
7044 
7045  if (c->fc->nb_streams < 1)
7046  return 0;
7047 
7048  st = c->fc->streams[c->fc->nb_streams - 1];
7049  sc = st->priv_data;
7050 
7051  if (atom.size < 16) {
7052  av_log(c->fc, AV_LOG_ERROR, "Invalid size for proj box: %"PRIu64"\n", atom.size);
7053  return AVERROR_INVALIDDATA;
7054  }
7055 
7056  size = avio_rb32(pb);
7057  if (size < 16) {
7058  av_log(c->fc, AV_LOG_ERROR, "Invalid size for prji box: %d\n", size);
7059  return AVERROR_INVALIDDATA;
7060  } else if (size > 16) {
7061  av_log(c->fc, AV_LOG_WARNING, "Box has more bytes (%d) than prji box required (16) \n", size);
7062  }
7063 
7064  tag = avio_rl32(pb);
7065  if (tag != MKTAG('p','r','j','i')) {
7066  av_log(c->fc, AV_LOG_ERROR, "Invalid child box of proj box: %s\n",
7067  av_fourcc2str(tag));
7068  return AVERROR_INVALIDDATA;
7069  }
7070 
7071  // version and flags, only support (0, 0)
7072  unsigned n = avio_rl32(pb);
7073  if (n != 0) {
7074  av_log(c->fc, AV_LOG_ERROR, "prji version %u, flag %u are not supported\n",
7075  n & 0xFF, n >> 8);
7076  return AVERROR_PATCHWELCOME;
7077  }
7078 
7079  tag = avio_rl32(pb);
7080  switch (tag) {
7081  case MKTAG('r','e','c','t'):
7082  projection = AV_SPHERICAL_RECTILINEAR;
7083  break;
7084  case MKTAG('e','q','u','i'):
7085  projection = AV_SPHERICAL_EQUIRECTANGULAR;
7086  break;
7087  case MKTAG('h','e','q','u'):
7088  projection = AV_SPHERICAL_HALF_EQUIRECTANGULAR;
7089  break;
7090  case MKTAG('f','i','s','h'):
7091  projection = AV_SPHERICAL_FISHEYE;
7092  break;
7093  case MKTAG('p','r','i','m'):
7094  projection = AV_SPHERICAL_PARAMETRIC_IMMERSIVE;
7095  break;
7096  default:
7097  av_log(c->fc, AV_LOG_ERROR, "Invalid projection type in prji box: %s\n", av_fourcc2str(tag));
7098  return AVERROR_INVALIDDATA;
7099  }
7100 
7102  if (!sc->spherical)
7103  return AVERROR(ENOMEM);
7104 
7105  sc->spherical->projection = projection;
7106 
7107  return 0;
7108 }
7109 
7111 {
7112  AVStream *st;
7113  MOVStreamContext *sc;
7114  int size, flags = 0;
7115  int64_t remaining;
7116  uint32_t tag, baseline = 0;
7119  enum AVStereo3DPrimaryEye primary_eye = AV_PRIMARY_EYE_NONE;
7120  AVRational horizontal_disparity_adjustment = { 0, 1 };
7121 
7122  if (c->fc->nb_streams < 1)
7123  return 0;
7124 
7125  st = c->fc->streams[c->fc->nb_streams - 1];
7126  sc = st->priv_data;
7127 
7128  remaining = atom.size;
7129  while (remaining > 0) {
7130  size = avio_rb32(pb);
7131  if (size < 8 || size > remaining ) {
7132  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in eyes box\n");
7133  return AVERROR_INVALIDDATA;
7134  }
7135 
7136  tag = avio_rl32(pb);
7137  switch (tag) {
7138  case MKTAG('s','t','r','i'): {
7139  int has_right, has_left;
7140  uint8_t tmp;
7141  if (size != 13) {
7142  av_log(c->fc, AV_LOG_ERROR, "Invalid size of stri box: %d\n", size);
7143  return AVERROR_INVALIDDATA;
7144  }
7145  avio_skip(pb, 1); // version
7146  avio_skip(pb, 3); // flags
7147 
7148  tmp = avio_r8(pb);
7149 
7150  // eye_views_reversed
7151  if (tmp & 8) {
7153  }
7154  // has_additional_views
7155  if (tmp & 4) {
7156  // skip...
7157  }
7158 
7159  has_right = tmp & 2; // has_right_eye_view
7160  has_left = tmp & 1; // has_left_eye_view
7161 
7162  if (has_left && has_right)
7163  view = AV_STEREO3D_VIEW_PACKED;
7164  else if (has_left)
7165  view = AV_STEREO3D_VIEW_LEFT;
7166  else if (has_right)
7167  view = AV_STEREO3D_VIEW_RIGHT;
7168  if (has_left || has_right)
7170 
7171  break;
7172  }
7173  case MKTAG('h','e','r','o'): {
7174  int tmp;
7175  if (size != 13) {
7176  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hero box: %d\n", size);
7177  return AVERROR_INVALIDDATA;
7178  }
7179  avio_skip(pb, 1); // version
7180  avio_skip(pb, 3); // flags
7181 
7182  tmp = avio_r8(pb);
7183  if (tmp == 0)
7184  primary_eye = AV_PRIMARY_EYE_NONE;
7185  else if (tmp == 1)
7186  primary_eye = AV_PRIMARY_EYE_LEFT;
7187  else if (tmp == 2)
7188  primary_eye = AV_PRIMARY_EYE_RIGHT;
7189  else
7190  av_log(c->fc, AV_LOG_WARNING, "Unknown hero eye type: %d\n", tmp);
7191 
7192  break;
7193  }
7194  case MKTAG('c','a','m','s'): {
7195  uint32_t subtag;
7196  int subsize;
7197  if (size != 24) {
7198  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cams box: %d\n", size);
7199  return AVERROR_INVALIDDATA;
7200  }
7201 
7202  subsize = avio_rb32(pb);
7203  if (subsize != 16) {
7204  av_log(c->fc, AV_LOG_ERROR, "Invalid size of blin box: %d\n", size);
7205  return AVERROR_INVALIDDATA;
7206  }
7207 
7208  subtag = avio_rl32(pb);
7209  if (subtag != MKTAG('b','l','i','n')) {
7210  av_log(c->fc, AV_LOG_ERROR, "Expected blin box, got %s\n",
7211  av_fourcc2str(subtag));
7212  return AVERROR_INVALIDDATA;
7213  }
7214 
7215  avio_skip(pb, 1); // version
7216  avio_skip(pb, 3); // flags
7217 
7218  baseline = avio_rb32(pb);
7219 
7220  break;
7221  }
7222  case MKTAG('c','m','f','y'): {
7223  uint32_t subtag;
7224  int subsize;
7225  int32_t adjustment;
7226  if (size != 24) {
7227  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cmfy box: %d\n", size);
7228  return AVERROR_INVALIDDATA;
7229  }
7230 
7231  subsize = avio_rb32(pb);
7232  if (subsize != 16) {
7233  av_log(c->fc, AV_LOG_ERROR, "Invalid size of dadj box: %d\n", size);
7234  return AVERROR_INVALIDDATA;
7235  }
7236 
7237  subtag = avio_rl32(pb);
7238  if (subtag != MKTAG('d','a','d','j')) {
7239  av_log(c->fc, AV_LOG_ERROR, "Expected dadj box, got %s\n",
7240  av_fourcc2str(subtag));
7241  return AVERROR_INVALIDDATA;
7242  }
7243 
7244  avio_skip(pb, 1); // version
7245  avio_skip(pb, 3); // flags
7246 
7247  adjustment = (int32_t) avio_rb32(pb);
7248 
7249  horizontal_disparity_adjustment.num = (int) adjustment;
7250  horizontal_disparity_adjustment.den = 10000;
7251 
7252  break;
7253  }
7254  default:
7255  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in eyes: %s\n",
7256  av_fourcc2str(tag));
7257  avio_skip(pb, size - 8);
7258  break;
7259  }
7260  remaining -= size;
7261  }
7262 
7263  if (remaining != 0) {
7264  av_log(c->fc, AV_LOG_ERROR, "Broken eyes box\n");
7265  return AVERROR_INVALIDDATA;
7266  }
7267 
7268  if (type == AV_STEREO3D_2D)
7269  return 0;
7270 
7271  if (!sc->stereo3d) {
7273  if (!sc->stereo3d)
7274  return AVERROR(ENOMEM);
7275  }
7276 
7277  sc->stereo3d->flags = flags;
7278  sc->stereo3d->type = type;
7279  sc->stereo3d->view = view;
7280  sc->stereo3d->primary_eye = primary_eye;
7281  sc->stereo3d->baseline = baseline;
7282  sc->stereo3d->horizontal_disparity_adjustment = horizontal_disparity_adjustment;
7283 
7284  return 0;
7285 }
7286 
7288 {
7289  int size;
7290  int64_t remaining;
7291  uint32_t tag;
7292 
7293  if (c->fc->nb_streams < 1)
7294  return 0;
7295 
7296  if (atom.size < 8) {
7297  av_log(c->fc, AV_LOG_ERROR, "Empty video extension usage box\n");
7298  return AVERROR_INVALIDDATA;
7299  }
7300 
7301  remaining = atom.size;
7302  while (remaining > 0) {
7303  size = avio_rb32(pb);
7304  if (size < 8 || size > remaining ) {
7305  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in vexu box\n");
7306  return AVERROR_INVALIDDATA;
7307  }
7308 
7309  tag = avio_rl32(pb);
7310  switch (tag) {
7311  case MKTAG('p','r','o','j'): {
7312  MOVAtom proj = { tag, size - 8 };
7313  int ret = mov_read_vexu_proj(c, pb, proj);
7314  if (ret < 0)
7315  return ret;
7316  break;
7317  }
7318  case MKTAG('e','y','e','s'): {
7319  MOVAtom eyes = { tag, size - 8 };
7320  int ret = mov_read_eyes(c, pb, eyes);
7321  if (ret < 0)
7322  return ret;
7323  break;
7324  }
7325  case MKTAG('p','a','c','k'): {
7326  MOVAtom pack = { tag, size - 8 };
7327  int ret = mov_read_pack(c, pb, pack);
7328  if (ret < 0)
7329  return ret;
7330  break;
7331  }
7332  default:
7333  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in vexu: %s\n",
7334  av_fourcc2str(tag));
7335  avio_skip(pb, size - 8);
7336  break;
7337  }
7338  remaining -= size;
7339  }
7340 
7341  if (remaining != 0) {
7342  av_log(c->fc, AV_LOG_ERROR, "Broken vexu box\n");
7343  return AVERROR_INVALIDDATA;
7344  }
7345 
7346  return 0;
7347 }
7348 
7350 {
7351  AVStream *st;
7352  MOVStreamContext *sc;
7353 
7354  if (c->fc->nb_streams < 1)
7355  return 0;
7356 
7357  st = c->fc->streams[c->fc->nb_streams - 1];
7358  sc = st->priv_data;
7359 
7360  if (atom.size != 4) {
7361  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hfov box: %"PRIu64"\n", atom.size);
7362  return AVERROR_INVALIDDATA;
7363  }
7364 
7365 
7366  if (!sc->stereo3d) {
7368  if (!sc->stereo3d)
7369  return AVERROR(ENOMEM);
7370  }
7371 
7373  sc->stereo3d->horizontal_field_of_view.den = 1000; // thousands of a degree
7374 
7375  return 0;
7376 }
7377 
7379 {
7380  int ret = 0;
7381  uint8_t *buffer = av_malloc(len + 1);
7382  const char *val;
7383 
7384  if (!buffer)
7385  return AVERROR(ENOMEM);
7386  buffer[len] = '\0';
7387 
7388  ret = ffio_read_size(pb, buffer, len);
7389  if (ret < 0)
7390  goto out;
7391 
7392  /* Check for mandatory keys and values, try to support XML as best-effort */
7393  if (!sc->spherical &&
7394  av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
7395  (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
7396  av_stristr(val, "true") &&
7397  (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
7398  av_stristr(val, "true") &&
7399  (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
7400  av_stristr(val, "equirectangular")) {
7402  if (!sc->spherical)
7403  goto out;
7404 
7406 
7407  if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
7408  enum AVStereo3DType mode;
7409 
7410  if (av_stristr(buffer, "left-right"))
7412  else if (av_stristr(buffer, "top-bottom"))
7414  else
7415  mode = AV_STEREO3D_2D;
7416 
7418  if (!sc->stereo3d)
7419  goto out;
7420 
7421  sc->stereo3d->type = mode;
7422  }
7423 
7424  /* orientation */
7425  val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
7426  if (val)
7427  sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
7428  val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
7429  if (val)
7430  sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
7431  val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
7432  if (val)
7433  sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
7434  }
7435 
7436 out:
7437  av_free(buffer);
7438  return ret;
7439 }
7440 
7442 {
7443  AVStream *st;
7444  MOVStreamContext *sc;
7445  int64_t ret;
7446  AVUUID uuid;
7447  static const AVUUID uuid_isml_manifest = {
7448  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
7449  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
7450  };
7451  static const AVUUID uuid_xmp = {
7452  0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
7453  0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
7454  };
7455  static const AVUUID uuid_spherical = {
7456  0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
7457  0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
7458  };
7459 
7460  if (atom.size < AV_UUID_LEN || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
7461  return AVERROR_INVALIDDATA;
7462 
7463  if (c->fc->nb_streams < 1)
7464  return 0;
7465  st = c->fc->streams[c->fc->nb_streams - 1];
7466  sc = st->priv_data;
7467 
7468  ret = ffio_read_size(pb, uuid, AV_UUID_LEN);
7469  if (ret < 0)
7470  return ret;
7471  if (av_uuid_equal(uuid, uuid_isml_manifest)) {
7472  uint8_t *buffer, *ptr;
7473  char *endptr;
7474  size_t len = atom.size - AV_UUID_LEN;
7475 
7476  if (len < 4) {
7477  return AVERROR_INVALIDDATA;
7478  }
7479  ret = avio_skip(pb, 4); // zeroes
7480  len -= 4;
7481 
7482  buffer = av_mallocz(len + 1);
7483  if (!buffer) {
7484  return AVERROR(ENOMEM);
7485  }
7486  ret = ffio_read_size(pb, buffer, len);
7487  if (ret < 0) {
7488  av_free(buffer);
7489  return ret;
7490  }
7491 
7492  ptr = buffer;
7493  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
7494  ptr += sizeof("systemBitrate=\"") - 1;
7495  c->bitrates_count++;
7496  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
7497  if (!c->bitrates) {
7498  c->bitrates_count = 0;
7499  av_free(buffer);
7500  return AVERROR(ENOMEM);
7501  }
7502  errno = 0;
7503  ret = strtol(ptr, &endptr, 10);
7504  if (ret < 0 || errno || *endptr != '"') {
7505  c->bitrates[c->bitrates_count - 1] = 0;
7506  } else {
7507  c->bitrates[c->bitrates_count - 1] = ret;
7508  }
7509  }
7510 
7511  av_free(buffer);
7512  } else if (av_uuid_equal(uuid, uuid_xmp)) {
7513  uint8_t *buffer;
7514  size_t len = atom.size - AV_UUID_LEN;
7515  if (c->export_xmp) {
7516  buffer = av_mallocz(len + 1);
7517  if (!buffer) {
7518  return AVERROR(ENOMEM);
7519  }
7520  ret = ffio_read_size(pb, buffer, len);
7521  if (ret < 0) {
7522  av_free(buffer);
7523  return ret;
7524  }
7525  buffer[len] = '\0';
7526  av_dict_set(&c->fc->metadata, "xmp",
7528  } else {
7529  // skip all uuid atom, which makes it fast for long uuid-xmp file
7530  ret = avio_skip(pb, len);
7531  if (ret < 0)
7532  return ret;
7533  }
7534  } else if (av_uuid_equal(uuid, uuid_spherical)) {
7535  size_t len = atom.size - AV_UUID_LEN;
7536  ret = mov_parse_uuid_spherical(sc, pb, len);
7537  if (ret < 0)
7538  return ret;
7539  if (!sc->spherical)
7540  av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
7541  }
7542 
7543  return 0;
7544 }
7545 
7547 {
7548  int ret;
7549  uint8_t content[16];
7550 
7551  if (atom.size < 8)
7552  return 0;
7553 
7554  ret = ffio_read_size(pb, content, FFMIN(sizeof(content), atom.size));
7555  if (ret < 0)
7556  return ret;
7557 
7558  if ( !c->found_moov
7559  && !c->found_mdat
7560  && !memcmp(content, "Anevia\x1A\x1A", 8)
7561  && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
7562  c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
7563  }
7564 
7565  return 0;
7566 }
7567 
7569 {
7570  uint32_t format = avio_rl32(pb);
7571  MOVStreamContext *sc;
7572  enum AVCodecID id;
7573  AVStream *st;
7574 
7575  if (c->fc->nb_streams < 1)
7576  return 0;
7577  st = c->fc->streams[c->fc->nb_streams - 1];
7578  sc = st->priv_data;
7579 
7580  switch (sc->format)
7581  {
7582  case MKTAG('e','n','c','v'): // encrypted video
7583  case MKTAG('e','n','c','a'): // encrypted audio
7584  id = mov_codec_id(st, format);
7585  if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
7586  st->codecpar->codec_id != id) {
7587  av_log(c->fc, AV_LOG_WARNING,
7588  "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
7589  (char*)&format, st->codecpar->codec_id);
7590  break;
7591  }
7592 
7593  st->codecpar->codec_id = id;
7594  sc->format = format;
7595  break;
7596 
7597  default:
7598  if (format != sc->format) {
7599  av_log(c->fc, AV_LOG_WARNING,
7600  "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
7601  (char*)&format, (char*)&sc->format);
7602  }
7603  break;
7604  }
7605 
7606  return 0;
7607 }
7608 
7609 /**
7610  * Gets the current encryption info and associated current stream context. If
7611  * we are parsing a track fragment, this will return the specific encryption
7612  * info for this fragment; otherwise this will return the global encryption
7613  * info for the current stream.
7614  */
7616 {
7617  MOVFragmentStreamInfo *frag_stream_info;
7618  AVStream *st;
7619  int i;
7620 
7621  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
7622  if (frag_stream_info) {
7623  for (i = 0; i < c->fc->nb_streams; i++) {
7624  *sc = c->fc->streams[i]->priv_data;
7625  if ((*sc)->id == frag_stream_info->id) {
7626  st = c->fc->streams[i];
7627  break;
7628  }
7629  }
7630  if (i == c->fc->nb_streams)
7631  return 0;
7632  *sc = st->priv_data;
7633 
7634  if (!frag_stream_info->encryption_index) {
7635  // If this stream isn't encrypted, don't create the index.
7636  if (!(*sc)->cenc.default_encrypted_sample)
7637  return 0;
7638  frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7639  if (!frag_stream_info->encryption_index)
7640  return AVERROR(ENOMEM);
7641  }
7642  *encryption_index = frag_stream_info->encryption_index;
7643  return 1;
7644  } else {
7645  // No current track fragment, using stream level encryption info.
7646 
7647  if (c->fc->nb_streams < 1)
7648  return 0;
7649  st = c->fc->streams[c->fc->nb_streams - 1];
7650  *sc = st->priv_data;
7651 
7652  if (!(*sc)->cenc.encryption_index) {
7653  // If this stream isn't encrypted, don't create the index.
7654  if (!(*sc)->cenc.default_encrypted_sample)
7655  return 0;
7656  (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7657  if (!(*sc)->cenc.encryption_index)
7658  return AVERROR(ENOMEM);
7659  }
7660 
7661  *encryption_index = (*sc)->cenc.encryption_index;
7662  return 1;
7663  }
7664 }
7665 
7667 {
7668  int i, ret;
7669  unsigned int subsample_count;
7670  AVSubsampleEncryptionInfo *subsamples;
7671 
7672  if (!sc->cenc.default_encrypted_sample) {
7673  av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
7674  return AVERROR_INVALIDDATA;
7675  }
7676 
7677  if (sc->cenc.per_sample_iv_size || use_subsamples) {
7679  if (!*sample)
7680  return AVERROR(ENOMEM);
7681  } else
7682  *sample = NULL;
7683 
7684  if (sc->cenc.per_sample_iv_size != 0) {
7685  if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
7686  av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
7688  *sample = NULL;
7689  return ret;
7690  }
7691  }
7692 
7693  if (use_subsamples) {
7694  subsample_count = avio_rb16(pb);
7695  av_free((*sample)->subsamples);
7696  (*sample)->subsamples = av_calloc(subsample_count, sizeof(*subsamples));
7697  if (!(*sample)->subsamples) {
7699  *sample = NULL;
7700  return AVERROR(ENOMEM);
7701  }
7702 
7703  for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
7704  (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
7705  (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
7706  }
7707 
7708  if (pb->eof_reached) {
7709  av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
7711  *sample = NULL;
7712  return AVERROR_INVALIDDATA;
7713  }
7714  (*sample)->subsample_count = subsample_count;
7715  }
7716 
7717  return 0;
7718 }
7719 
7721 {
7722  AVEncryptionInfo **encrypted_samples;
7723  MOVEncryptionIndex *encryption_index;
7724  MOVStreamContext *sc;
7725  int use_subsamples, ret;
7726  unsigned int sample_count, i, alloc_size = 0;
7727 
7728  ret = get_current_encryption_info(c, &encryption_index, &sc);
7729  if (ret != 1)
7730  return ret;
7731 
7732  if (encryption_index->nb_encrypted_samples) {
7733  // This can happen if we have both saio/saiz and senc atoms.
7734  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
7735  return 0;
7736  }
7737 
7738  avio_r8(pb); /* version */
7739  use_subsamples = avio_rb24(pb) & 0x02; /* flags */
7740 
7741  sample_count = avio_rb32(pb);
7742  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7743  return AVERROR(ENOMEM);
7744 
7745  for (i = 0; i < sample_count; i++) {
7746  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7747  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7748  min_samples * sizeof(*encrypted_samples));
7749  if (encrypted_samples) {
7750  encryption_index->encrypted_samples = encrypted_samples;
7751 
7753  c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
7754  } else {
7755  ret = AVERROR(ENOMEM);
7756  }
7757  if (pb->eof_reached) {
7758  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
7759  if (ret >= 0)
7760  av_encryption_info_free(encryption_index->encrypted_samples[i]);
7762  }
7763 
7764  if (ret < 0) {
7765  for (; i > 0; i--)
7766  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7767  av_freep(&encryption_index->encrypted_samples);
7768  return ret;
7769  }
7770  }
7771  encryption_index->nb_encrypted_samples = sample_count;
7772 
7773  return 0;
7774 }
7775 
7777 {
7778  AVEncryptionInfo **sample, **encrypted_samples;
7779  int64_t prev_pos;
7780  size_t sample_count, sample_info_size, i;
7781  int ret = 0;
7782  unsigned int alloc_size = 0;
7783 
7784  if (encryption_index->nb_encrypted_samples)
7785  return 0;
7786  sample_count = encryption_index->auxiliary_info_sample_count;
7787  if (encryption_index->auxiliary_offsets_count != 1) {
7788  av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
7789  return AVERROR_PATCHWELCOME;
7790  }
7791  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7792  return AVERROR(ENOMEM);
7793 
7794  prev_pos = avio_tell(pb);
7795  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
7796  avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
7797  av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
7798  goto finish;
7799  }
7800 
7801  for (i = 0; i < sample_count && !pb->eof_reached; i++) {
7802  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7803  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7804  min_samples * sizeof(*encrypted_samples));
7805  if (!encrypted_samples) {
7806  ret = AVERROR(ENOMEM);
7807  goto finish;
7808  }
7809  encryption_index->encrypted_samples = encrypted_samples;
7810 
7811  sample = &encryption_index->encrypted_samples[i];
7812  sample_info_size = encryption_index->auxiliary_info_default_size
7813  ? encryption_index->auxiliary_info_default_size
7814  : encryption_index->auxiliary_info_sizes[i];
7815 
7816  ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
7817  if (ret < 0)
7818  goto finish;
7819  }
7820  if (pb->eof_reached) {
7821  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
7823  } else {
7824  encryption_index->nb_encrypted_samples = sample_count;
7825  }
7826 
7827 finish:
7828  avio_seek(pb, prev_pos, SEEK_SET);
7829  if (ret < 0) {
7830  for (; i > 0; i--) {
7831  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7832  }
7833  av_freep(&encryption_index->encrypted_samples);
7834  }
7835  return ret;
7836 }
7837 
7839 {
7840  MOVEncryptionIndex *encryption_index;
7841  MOVStreamContext *sc;
7842  int ret;
7843  unsigned int sample_count, aux_info_type, aux_info_param;
7844 
7845  ret = get_current_encryption_info(c, &encryption_index, &sc);
7846  if (ret != 1)
7847  return ret;
7848 
7849  if (encryption_index->nb_encrypted_samples) {
7850  // This can happen if we have both saio/saiz and senc atoms.
7851  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
7852  return 0;
7853  }
7854 
7855  if (encryption_index->auxiliary_info_sample_count) {
7856  av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
7857  return AVERROR_INVALIDDATA;
7858  }
7859 
7860  avio_r8(pb); /* version */
7861  if (avio_rb24(pb) & 0x01) { /* flags */
7862  aux_info_type = avio_rb32(pb);
7863  aux_info_param = avio_rb32(pb);
7864  if (sc->cenc.default_encrypted_sample) {
7865  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7866  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
7867  return 0;
7868  }
7869  if (aux_info_param != 0) {
7870  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
7871  return 0;
7872  }
7873  } else {
7874  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7875  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7876  aux_info_type == MKBETAG('c','e','n','s') ||
7877  aux_info_type == MKBETAG('c','b','c','1') ||
7878  aux_info_type == MKBETAG('c','b','c','s')) &&
7879  aux_info_param == 0) {
7880  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
7881  return AVERROR_INVALIDDATA;
7882  } else {
7883  return 0;
7884  }
7885  }
7886  } else if (!sc->cenc.default_encrypted_sample) {
7887  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7888  return 0;
7889  }
7890 
7891  encryption_index->auxiliary_info_default_size = avio_r8(pb);
7892  sample_count = avio_rb32(pb);
7893 
7894  if (encryption_index->auxiliary_info_default_size == 0) {
7895  if (sample_count == 0)
7896  return AVERROR_INVALIDDATA;
7897 
7898  encryption_index->auxiliary_info_sizes = av_malloc(sample_count);
7899  if (!encryption_index->auxiliary_info_sizes)
7900  return AVERROR(ENOMEM);
7901 
7902  ret = avio_read(pb, encryption_index->auxiliary_info_sizes, sample_count);
7903  if (ret != sample_count) {
7904  av_freep(&encryption_index->auxiliary_info_sizes);
7905 
7906  if (ret >= 0)
7908  av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info, %s\n",
7909  av_err2str(ret));
7910  return ret;
7911  }
7912  }
7913  encryption_index->auxiliary_info_sample_count = sample_count;
7914 
7915  if (encryption_index->auxiliary_offsets_count) {
7916  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
7917  }
7918 
7919  return 0;
7920 }
7921 
7923 {
7924  uint64_t *auxiliary_offsets;
7925  MOVEncryptionIndex *encryption_index;
7926  MOVStreamContext *sc;
7927  int i, ret;
7928  unsigned int version, entry_count, aux_info_type, aux_info_param;
7929  unsigned int alloc_size = 0;
7930 
7931  ret = get_current_encryption_info(c, &encryption_index, &sc);
7932  if (ret != 1)
7933  return ret;
7934 
7935  if (encryption_index->nb_encrypted_samples) {
7936  // This can happen if we have both saio/saiz and senc atoms.
7937  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
7938  return 0;
7939  }
7940 
7941  if (encryption_index->auxiliary_offsets_count) {
7942  av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
7943  return AVERROR_INVALIDDATA;
7944  }
7945 
7946  version = avio_r8(pb); /* version */
7947  if (avio_rb24(pb) & 0x01) { /* flags */
7948  aux_info_type = avio_rb32(pb);
7949  aux_info_param = avio_rb32(pb);
7950  if (sc->cenc.default_encrypted_sample) {
7951  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7952  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
7953  return 0;
7954  }
7955  if (aux_info_param != 0) {
7956  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
7957  return 0;
7958  }
7959  } else {
7960  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7961  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7962  aux_info_type == MKBETAG('c','e','n','s') ||
7963  aux_info_type == MKBETAG('c','b','c','1') ||
7964  aux_info_type == MKBETAG('c','b','c','s')) &&
7965  aux_info_param == 0) {
7966  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
7967  return AVERROR_INVALIDDATA;
7968  } else {
7969  return 0;
7970  }
7971  }
7972  } else if (!sc->cenc.default_encrypted_sample) {
7973  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7974  return 0;
7975  }
7976 
7977  entry_count = avio_rb32(pb);
7978  if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
7979  return AVERROR(ENOMEM);
7980 
7981  for (i = 0; i < entry_count && !pb->eof_reached; i++) {
7982  unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
7983  auxiliary_offsets = av_fast_realloc(
7984  encryption_index->auxiliary_offsets, &alloc_size,
7985  min_offsets * sizeof(*auxiliary_offsets));
7986  if (!auxiliary_offsets) {
7987  av_freep(&encryption_index->auxiliary_offsets);
7988  return AVERROR(ENOMEM);
7989  }
7990  encryption_index->auxiliary_offsets = auxiliary_offsets;
7991 
7992  if (version == 0) {
7993  encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
7994  } else {
7995  encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
7996  }
7997  if (c->frag_index.current >= 0) {
7998  encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
7999  }
8000  }
8001 
8002  if (pb->eof_reached) {
8003  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
8004  av_freep(&encryption_index->auxiliary_offsets);
8005  return AVERROR_INVALIDDATA;
8006  }
8007 
8008  encryption_index->auxiliary_offsets_count = entry_count;
8009 
8010  if (encryption_index->auxiliary_info_sample_count) {
8011  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
8012  }
8013 
8014  return 0;
8015 }
8016 
8018 {
8019  AVEncryptionInitInfo *info, *old_init_info;
8020  uint8_t **key_ids;
8021  AVStream *st;
8022  const AVPacketSideData *old_side_data;
8023  uint8_t *side_data, *extra_data;
8024  size_t side_data_size;
8025  int ret = 0;
8026  unsigned int version, kid_count, extra_data_size, alloc_size = 0;
8027 
8028  if (c->fc->nb_streams < 1)
8029  return 0;
8030  st = c->fc->streams[c->fc->nb_streams-1];
8031 
8032  version = avio_r8(pb); /* version */
8033  avio_rb24(pb); /* flags */
8034 
8035  info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
8036  /* key_id_size */ 16, /* data_size */ 0);
8037  if (!info)
8038  return AVERROR(ENOMEM);
8039 
8040  if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
8041  av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
8042  goto finish;
8043  }
8044 
8045  if (version > 0) {
8046  kid_count = avio_rb32(pb);
8047  if (kid_count >= INT_MAX / sizeof(*key_ids)) {
8048  ret = AVERROR(ENOMEM);
8049  goto finish;
8050  }
8051 
8052  for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
8053  unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
8054  key_ids = av_fast_realloc(info->key_ids, &alloc_size,
8055  min_kid_count * sizeof(*key_ids));
8056  if (!key_ids) {
8057  ret = AVERROR(ENOMEM);
8058  goto finish;
8059  }
8060  info->key_ids = key_ids;
8061 
8062  info->key_ids[i] = av_mallocz(16);
8063  if (!info->key_ids[i]) {
8064  ret = AVERROR(ENOMEM);
8065  goto finish;
8066  }
8067  info->num_key_ids = i + 1;
8068 
8069  if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
8070  av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
8071  goto finish;
8072  }
8073  }
8074 
8075  if (pb->eof_reached) {
8076  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
8078  goto finish;
8079  }
8080  }
8081 
8082  extra_data_size = avio_rb32(pb);
8083  extra_data = av_malloc(extra_data_size);
8084  if (!extra_data) {
8085  ret = AVERROR(ENOMEM);
8086  goto finish;
8087  }
8088  ret = avio_read(pb, extra_data, extra_data_size);
8089  if (ret != extra_data_size) {
8090  av_free(extra_data);
8091 
8092  if (ret >= 0)
8094  goto finish;
8095  }
8096 
8097  av_freep(&info->data); // malloc(0) may still allocate something.
8098  info->data = extra_data;
8099  info->data_size = extra_data_size;
8100 
8101  // If there is existing initialization data, append to the list.
8104  if (old_side_data) {
8105  old_init_info = av_encryption_init_info_get_side_data(old_side_data->data, old_side_data->size);
8106  if (old_init_info) {
8107  // Append to the end of the list.
8108  for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
8109  if (!cur->next) {
8110  cur->next = info;
8111  break;
8112  }
8113  }
8114  info = old_init_info;
8115  } else {
8116  // Assume existing side-data will be valid, so the only error we could get is OOM.
8117  ret = AVERROR(ENOMEM);
8118  goto finish;
8119  }
8120  }
8121 
8122  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
8123  if (!side_data) {
8124  ret = AVERROR(ENOMEM);
8125  goto finish;
8126  }
8130  side_data, side_data_size, 0))
8131  av_free(side_data);
8132 
8133 finish:
8135  return ret;
8136 }
8137 
8139 {
8140  AVStream *st;
8141  MOVStreamContext *sc;
8142 
8143  if (c->fc->nb_streams < 1)
8144  return 0;
8145  st = c->fc->streams[c->fc->nb_streams-1];
8146  sc = st->priv_data;
8147 
8148  if (sc->pseudo_stream_id != 0) {
8149  av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
8150  return AVERROR_PATCHWELCOME;
8151  }
8152 
8153  if (atom.size < 8)
8154  return AVERROR_INVALIDDATA;
8155 
8156  avio_rb32(pb); /* version and flags */
8157 
8158  if (!sc->cenc.default_encrypted_sample) {
8160  if (!sc->cenc.default_encrypted_sample) {
8161  return AVERROR(ENOMEM);
8162  }
8163  }
8164 
8166  return 0;
8167 }
8168 
8170 {
8171  AVStream *st;
8172  MOVStreamContext *sc;
8173  unsigned int version, pattern, is_protected, iv_size;
8174 
8175  if (c->fc->nb_streams < 1)
8176  return 0;
8177  st = c->fc->streams[c->fc->nb_streams-1];
8178  sc = st->priv_data;
8179 
8180  if (sc->pseudo_stream_id != 0) {
8181  av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
8182  return AVERROR_PATCHWELCOME;
8183  }
8184 
8185  if (!sc->cenc.default_encrypted_sample) {
8187  if (!sc->cenc.default_encrypted_sample) {
8188  return AVERROR(ENOMEM);
8189  }
8190  }
8191 
8192  if (atom.size < 20)
8193  return AVERROR_INVALIDDATA;
8194 
8195  version = avio_r8(pb); /* version */
8196  avio_rb24(pb); /* flags */
8197 
8198  avio_r8(pb); /* reserved */
8199  pattern = avio_r8(pb);
8200 
8201  if (version > 0) {
8202  sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
8203  sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
8204  }
8205 
8206  is_protected = avio_r8(pb);
8207  if (is_protected && !sc->cenc.encryption_index) {
8208  // The whole stream should be by-default encrypted.
8210  if (!sc->cenc.encryption_index)
8211  return AVERROR(ENOMEM);
8212  }
8213  sc->cenc.per_sample_iv_size = avio_r8(pb);
8214  if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
8215  sc->cenc.per_sample_iv_size != 16) {
8216  av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
8217  return AVERROR_INVALIDDATA;
8218  }
8219  if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
8220  av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
8221  return AVERROR_INVALIDDATA;
8222  }
8223 
8224  if (is_protected && !sc->cenc.per_sample_iv_size) {
8225  iv_size = avio_r8(pb);
8226  if (iv_size != 8 && iv_size != 16) {
8227  av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
8228  return AVERROR_INVALIDDATA;
8229  }
8230 
8231  if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
8232  av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
8233  return AVERROR_INVALIDDATA;
8234  }
8235  }
8236 
8237  return 0;
8238 }
8239 
8241 {
8242  AVStream *st;
8243  int last, type, size, ret;
8244  uint8_t buf[4];
8245 
8246  if (c->fc->nb_streams < 1)
8247  return 0;
8248  st = c->fc->streams[c->fc->nb_streams-1];
8249 
8250  if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
8251  return AVERROR_INVALIDDATA;
8252 
8253  /* Check FlacSpecificBox version. */
8254  if (avio_r8(pb) != 0)
8255  return AVERROR_INVALIDDATA;
8256 
8257  avio_rb24(pb); /* Flags */
8258 
8259  if (avio_read(pb, buf, sizeof(buf)) != sizeof(buf)) {
8260  av_log(c->fc, AV_LOG_ERROR, "failed to read FLAC metadata block header\n");
8261  return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
8262  }
8263  flac_parse_block_header(buf, &last, &type, &size);
8264 
8266  av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
8267  return AVERROR_INVALIDDATA;
8268  }
8269 
8270  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
8271  if (ret < 0)
8272  return ret;
8273 
8274  if (!last)
8275  av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
8276 
8277  return 0;
8278 }
8279 
8280 static int get_key_from_kid(uint8_t* out, int len, MOVContext *c, AVEncryptionInfo *sample) {
8281  AVDictionaryEntry *key_entry_hex;
8282  char kid_hex[16*2+1];
8283 
8284  if (c->decryption_default_key && c->decryption_default_key_len != len) {
8285  av_log(c->fc, AV_LOG_ERROR, "invalid default decryption key length: got %d, expected %d\n", c->decryption_default_key_len, len);
8286  return -1;
8287  }
8288 
8289  if (!c->decryption_keys) {
8290  av_assert0(c->decryption_default_key);
8291  memcpy(out, c->decryption_default_key, len);
8292  return 0;
8293  }
8294 
8295  if (sample->key_id_size != 16) {
8296  av_log(c->fc, AV_LOG_ERROR, "invalid key ID size: got %u, expected 16\n", sample->key_id_size);
8297  return -1;
8298  }
8299 
8300  ff_data_to_hex(kid_hex, sample->key_id, 16, 1);
8301  key_entry_hex = av_dict_get(c->decryption_keys, kid_hex, NULL, AV_DICT_DONT_STRDUP_KEY|AV_DICT_DONT_STRDUP_VAL);
8302  if (!key_entry_hex) {
8303  if (!c->decryption_default_key) {
8304  av_log(c->fc, AV_LOG_ERROR, "unable to find KID %s\n", kid_hex);
8305  return -1;
8306  }
8307  memcpy(out, c->decryption_default_key, len);
8308  return 0;
8309  }
8310  if (strlen(key_entry_hex->value) != len*2) {
8311  return -1;
8312  }
8313  ff_hex_to_data(out, key_entry_hex->value);
8314  return 0;
8315 }
8316 
8318 {
8319  int i, ret;
8320  int bytes_of_protected_data;
8321  uint8_t decryption_key[AES_CTR_KEY_SIZE];
8322 
8323  if (!sc->cenc.aes_ctr) {
8324  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8325  if (ret < 0) {
8326  return ret;
8327  }
8328 
8329  /* initialize the cipher */
8330  sc->cenc.aes_ctr = av_aes_ctr_alloc();
8331  if (!sc->cenc.aes_ctr) {
8332  return AVERROR(ENOMEM);
8333  }
8334 
8335  ret = av_aes_ctr_init(sc->cenc.aes_ctr, decryption_key);
8336  if (ret < 0) {
8337  return ret;
8338  }
8339  }
8340 
8342 
8343  if (!sample->subsample_count) {
8344  /* decrypt the whole packet */
8346  return 0;
8347  }
8348 
8349  for (i = 0; i < sample->subsample_count; i++) {
8350  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8351  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8352  return AVERROR_INVALIDDATA;
8353  }
8354 
8355  /* skip the clear bytes */
8356  input += sample->subsamples[i].bytes_of_clear_data;
8357  size -= sample->subsamples[i].bytes_of_clear_data;
8358 
8359  /* decrypt the encrypted bytes */
8360 
8361  bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data;
8362  av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data);
8363 
8364  input += bytes_of_protected_data;
8365  size -= bytes_of_protected_data;
8366  }
8367 
8368  if (size > 0) {
8369  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8370  return AVERROR_INVALIDDATA;
8371  }
8372 
8373  return 0;
8374 }
8375 
8377 {
8378  int i, ret;
8379  int num_of_encrypted_blocks;
8380  uint8_t iv[16];
8381  uint8_t decryption_key[16];
8382 
8383  if (!sc->cenc.aes_ctx) {
8384  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8385  if (ret < 0) {
8386  return ret;
8387  }
8388 
8389  /* initialize the cipher */
8390  sc->cenc.aes_ctx = av_aes_alloc();
8391  if (!sc->cenc.aes_ctx) {
8392  return AVERROR(ENOMEM);
8393  }
8394 
8395  ret = av_aes_init(sc->cenc.aes_ctx, decryption_key, 16 * 8, 1);
8396  if (ret < 0) {
8397  return ret;
8398  }
8399  }
8400 
8401  memcpy(iv, sample->iv, 16);
8402 
8403  /* whole-block full sample encryption */
8404  if (!sample->subsample_count) {
8405  /* decrypt the whole packet */
8406  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8407  return 0;
8408  }
8409 
8410  for (i = 0; i < sample->subsample_count; i++) {
8411  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8412  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8413  return AVERROR_INVALIDDATA;
8414  }
8415 
8416  if (sample->subsamples[i].bytes_of_protected_data % 16) {
8417  av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is not a multiple of 16\n");
8418  return AVERROR_INVALIDDATA;
8419  }
8420 
8421  /* skip the clear bytes */
8422  input += sample->subsamples[i].bytes_of_clear_data;
8423  size -= sample->subsamples[i].bytes_of_clear_data;
8424 
8425  /* decrypt the encrypted bytes */
8426  num_of_encrypted_blocks = sample->subsamples[i].bytes_of_protected_data/16;
8427  if (num_of_encrypted_blocks > 0) {
8428  av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1);
8429  }
8430  input += sample->subsamples[i].bytes_of_protected_data;
8431  size -= sample->subsamples[i].bytes_of_protected_data;
8432  }
8433 
8434  if (size > 0) {
8435  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8436  return AVERROR_INVALIDDATA;
8437  }
8438 
8439  return 0;
8440 }
8441 
8443 {
8444  int i, ret, rem_bytes;
8445  uint8_t *data;
8446  uint8_t decryption_key[AES_CTR_KEY_SIZE];
8447 
8448  if (!sc->cenc.aes_ctr) {
8449  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8450  if (ret < 0) {
8451  return ret;
8452  }
8453 
8454  /* initialize the cipher */
8455  sc->cenc.aes_ctr = av_aes_ctr_alloc();
8456  if (!sc->cenc.aes_ctr) {
8457  return AVERROR(ENOMEM);
8458  }
8459 
8460  ret = av_aes_ctr_init(sc->cenc.aes_ctr, decryption_key);
8461  if (ret < 0) {
8462  return ret;
8463  }
8464  }
8465 
8467 
8468  /* whole-block full sample encryption */
8469  if (!sample->subsample_count) {
8470  /* decrypt the whole packet */
8472  return 0;
8473  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8474  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cens' scheme\n");
8475  return AVERROR_INVALIDDATA;
8476  }
8477 
8478  for (i = 0; i < sample->subsample_count; i++) {
8479  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8480  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8481  return AVERROR_INVALIDDATA;
8482  }
8483 
8484  /* skip the clear bytes */
8485  input += sample->subsamples[i].bytes_of_clear_data;
8486  size -= sample->subsamples[i].bytes_of_clear_data;
8487 
8488  /* decrypt the encrypted bytes */
8489  data = input;
8490  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8491  while (rem_bytes > 0) {
8492  if (rem_bytes < 16*sample->crypt_byte_block) {
8493  break;
8494  }
8495  av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block);
8496  data += 16*sample->crypt_byte_block;
8497  rem_bytes -= 16*sample->crypt_byte_block;
8498  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8499  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8500  }
8501  input += sample->subsamples[i].bytes_of_protected_data;
8502  size -= sample->subsamples[i].bytes_of_protected_data;
8503  }
8504 
8505  if (size > 0) {
8506  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8507  return AVERROR_INVALIDDATA;
8508  }
8509 
8510  return 0;
8511 }
8512 
8514 {
8515  int i, ret, rem_bytes;
8516  uint8_t iv[16];
8517  uint8_t *data;
8518  uint8_t decryption_key[16];
8519 
8520  if (!sc->cenc.aes_ctx) {
8521  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8522  if (ret < 0) {
8523  return ret;
8524  }
8525 
8526  /* initialize the cipher */
8527  sc->cenc.aes_ctx = av_aes_alloc();
8528  if (!sc->cenc.aes_ctx) {
8529  return AVERROR(ENOMEM);
8530  }
8531 
8532  ret = av_aes_init(sc->cenc.aes_ctx, decryption_key, 16 * 8, 1);
8533  if (ret < 0) {
8534  return ret;
8535  }
8536  }
8537 
8538  /* whole-block full sample encryption */
8539  if (!sample->subsample_count) {
8540  /* decrypt the whole packet */
8541  memcpy(iv, sample->iv, 16);
8542  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8543  return 0;
8544  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8545  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cbcs' scheme\n");
8546  return AVERROR_INVALIDDATA;
8547  }
8548 
8549  for (i = 0; i < sample->subsample_count; i++) {
8550  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8551  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8552  return AVERROR_INVALIDDATA;
8553  }
8554 
8555  /* skip the clear bytes */
8556  input += sample->subsamples[i].bytes_of_clear_data;
8557  size -= sample->subsamples[i].bytes_of_clear_data;
8558 
8559  /* decrypt the encrypted bytes */
8560  memcpy(iv, sample->iv, 16);
8561  data = input;
8562  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8563  while (rem_bytes > 0) {
8564  if (rem_bytes < 16*sample->crypt_byte_block) {
8565  break;
8566  }
8567  av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1);
8568  data += 16*sample->crypt_byte_block;
8569  rem_bytes -= 16*sample->crypt_byte_block;
8570  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8571  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8572  }
8573  input += sample->subsamples[i].bytes_of_protected_data;
8574  size -= sample->subsamples[i].bytes_of_protected_data;
8575  }
8576 
8577  if (size > 0) {
8578  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8579  return AVERROR_INVALIDDATA;
8580  }
8581 
8582  return 0;
8583 }
8584 
8586 {
8587  if (sample->scheme == MKBETAG('c','e','n','c') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8588  return cenc_scheme_decrypt(c, sc, sample, input, size);
8589  } else if (sample->scheme == MKBETAG('c','b','c','1') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8590  return cbc1_scheme_decrypt(c, sc, sample, input, size);
8591  } else if (sample->scheme == MKBETAG('c','e','n','s')) {
8592  return cens_scheme_decrypt(c, sc, sample, input, size);
8593  } else if (sample->scheme == MKBETAG('c','b','c','s')) {
8594  return cbcs_scheme_decrypt(c, sc, sample, input, size);
8595  } else {
8596  av_log(c->fc, AV_LOG_ERROR, "invalid encryption scheme\n");
8597  return AVERROR_INVALIDDATA;
8598  }
8599 }
8600 
8602 {
8603  int current = frag_index->current;
8604 
8605  if (!frag_index->nb_items)
8606  return NULL;
8607 
8608  // Check frag_index->current is the right one for pkt. It can out of sync.
8609  if (current >= 0 && current < frag_index->nb_items) {
8610  if (frag_index->item[current].moof_offset < pkt->pos &&
8611  (current + 1 == frag_index->nb_items ||
8612  frag_index->item[current + 1].moof_offset > pkt->pos))
8613  return get_frag_stream_info(frag_index, current, id);
8614  }
8615 
8616 
8617  for (int i = 0; i < frag_index->nb_items; i++) {
8618  if (frag_index->item[i].moof_offset > pkt->pos)
8619  break;
8620  current = i;
8621  }
8622  frag_index->current = current;
8623  return get_frag_stream_info(frag_index, current, id);
8624 }
8625 
8626 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
8627 {
8628  MOVFragmentStreamInfo *frag_stream_info;
8629  MOVEncryptionIndex *encryption_index;
8630  AVEncryptionInfo *encrypted_sample;
8631  int encrypted_index, ret;
8632 
8633  frag_stream_info = get_frag_stream_info_from_pkt(&mov->frag_index, pkt, sc->id);
8634  encrypted_index = current_index;
8635  encryption_index = NULL;
8636  if (frag_stream_info) {
8637  // Note this only supports encryption info in the first sample descriptor.
8638  if (frag_stream_info->stsd_id == 1) {
8639  if (frag_stream_info->encryption_index) {
8640  encrypted_index = current_index - frag_stream_info->index_base;
8641  encryption_index = frag_stream_info->encryption_index;
8642  } else {
8643  encryption_index = sc->cenc.encryption_index;
8644  }
8645  }
8646  } else {
8647  encryption_index = sc->cenc.encryption_index;
8648  }
8649 
8650  if (encryption_index) {
8651  if (encryption_index->auxiliary_info_sample_count &&
8652  !encryption_index->nb_encrypted_samples) {
8653  av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
8654  return AVERROR_INVALIDDATA;
8655  }
8656  if (encryption_index->auxiliary_offsets_count &&
8657  !encryption_index->nb_encrypted_samples) {
8658  av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
8659  return AVERROR_INVALIDDATA;
8660  }
8661 
8662  encrypted_sample = NULL;
8663  if (!encryption_index->nb_encrypted_samples) {
8664  // Full-sample encryption with default settings.
8665  encrypted_sample = sc->cenc.default_encrypted_sample;
8666  } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
8667  // Per-sample setting override.
8668  encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
8669  if (!encrypted_sample) {
8670  encrypted_sample = sc->cenc.default_encrypted_sample;
8671  }
8672  }
8673 
8674  if (!encrypted_sample) {
8675  av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
8676  return AVERROR_INVALIDDATA;
8677  }
8678 
8679  if (mov->decryption_keys || mov->decryption_default_key) {
8680  return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
8681  } else {
8682  size_t size;
8683  uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
8684  if (!side_data)
8685  return AVERROR(ENOMEM);
8687  if (ret < 0)
8688  av_free(side_data);
8689  return ret;
8690  }
8691  }
8692 
8693  return 0;
8694 }
8695 
8697 {
8698  const int OPUS_SEEK_PREROLL_MS = 80;
8699  int ret;
8700  AVStream *st;
8701  size_t size;
8702  uint16_t pre_skip;
8703 
8704  if (c->fc->nb_streams < 1)
8705  return 0;
8706  st = c->fc->streams[c->fc->nb_streams-1];
8707 
8708  if ((uint64_t)atom.size > (1<<30) || atom.size < 11 || st->codecpar->extradata)
8709  return AVERROR_INVALIDDATA;
8710 
8711  /* Check OpusSpecificBox version. */
8712  if (avio_r8(pb) != 0) {
8713  av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
8714  return AVERROR_INVALIDDATA;
8715  }
8716 
8717  /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
8718  size = atom.size + 8;
8719 
8720  if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
8721  return ret;
8722 
8723  AV_WL32A(st->codecpar->extradata, MKTAG('O','p','u','s'));
8724  AV_WL32A(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
8725  AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
8726  if ((ret = ffio_read_size(pb, st->codecpar->extradata + 9, size - 9)) < 0) {
8727  av_freep(&st->codecpar->extradata);
8728  st->codecpar->extradata_size = 0;
8729  return ret;
8730  }
8731 
8732  /* OpusSpecificBox is stored in big-endian, but OpusHead is
8733  little-endian; aside from the preceding magic and version they're
8734  otherwise currently identical. Data after output gain at offset 16
8735  doesn't need to be bytewapped. */
8736  pre_skip = AV_RB16A(st->codecpar->extradata + 10);
8737  AV_WL16A(st->codecpar->extradata + 10, pre_skip);
8738  AV_WL32A(st->codecpar->extradata + 12, AV_RB32A(st->codecpar->extradata + 12));
8739  AV_WL16A(st->codecpar->extradata + 16, AV_RB16A(st->codecpar->extradata + 16));
8740 
8741  st->codecpar->initial_padding = pre_skip;
8743  (AVRational){1, 1000},
8744  (AVRational){1, 48000});
8745 
8746  return 0;
8747 }
8748 
8750 {
8751  AVStream *st;
8752  unsigned format_info;
8753  int channel_assignment, channel_assignment1, channel_assignment2;
8754  int ratebits;
8755  uint64_t chmask;
8756 
8757  if (c->fc->nb_streams < 1)
8758  return 0;
8759  st = c->fc->streams[c->fc->nb_streams-1];
8760 
8761  if (atom.size < 10)
8762  return AVERROR_INVALIDDATA;
8763 
8764  format_info = avio_rb32(pb);
8765 
8766  ratebits = (format_info >> 28) & 0xF;
8767  channel_assignment1 = (format_info >> 15) & 0x1F;
8768  channel_assignment2 = format_info & 0x1FFF;
8769  if (channel_assignment2)
8770  channel_assignment = channel_assignment2;
8771  else
8772  channel_assignment = channel_assignment1;
8773 
8774  st->codecpar->frame_size = 40 << (ratebits & 0x7);
8775  st->codecpar->sample_rate = mlp_samplerate(ratebits);
8776 
8778  chmask = truehd_layout(channel_assignment);
8780 
8781  return 0;
8782 }
8783 
8785 {
8786  AVStream *st;
8787  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
8788  int ret;
8789  int64_t read_size = atom.size;
8790 
8791  if (c->fc->nb_streams < 1)
8792  return 0;
8793  st = c->fc->streams[c->fc->nb_streams-1];
8794 
8795  // At most 24 bytes
8796  read_size = FFMIN(read_size, ISOM_DVCC_DVVC_SIZE);
8797 
8798  if ((ret = ffio_read_size(pb, buf, read_size)) < 0)
8799  return ret;
8800 
8801  return ff_isom_parse_dvcc_dvvc(c->fc, st, buf, read_size);
8802 }
8803 
8805 {
8806  AVStream *st;
8807  AVPacketSideData *sd;
8808  int ret;
8809 
8810  if (c->fc->nb_streams < 1)
8811  return 0;
8812  st = c->fc->streams[c->fc->nb_streams - 1];
8813 
8814  if (atom.size < 23 || atom.size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
8815  return AVERROR_INVALIDDATA;
8816 
8820  atom.size, 0);
8821  if (!sd)
8822  return AVERROR(ENOMEM);
8823 
8824  ret = ffio_read_size(pb, sd->data, atom.size);
8825  if (ret < 0)
8826  return ret;
8827 
8828  return 0;
8829 }
8830 
8832 {
8833  AVStream *st;
8834  uint8_t *buf;
8835  int ret, old_size, num_arrays;
8836 
8837  if (c->fc->nb_streams < 1)
8838  return 0;
8839  st = c->fc->streams[c->fc->nb_streams-1];
8840 
8841  if (!st->codecpar->extradata_size)
8842  // TODO: handle lhvC when present before hvcC
8843  return 0;
8844 
8845  if (atom.size < 6 || st->codecpar->extradata_size < 23 ||
8846  atom.size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
8847  return AVERROR_INVALIDDATA;
8848  }
8849 
8851  if (!buf)
8852  return AVERROR(ENOMEM);
8853  memset(buf + atom.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
8854 
8855  ret = ffio_read_size(pb, buf, atom.size);
8856  if (ret < 0) {
8857  av_free(buf);
8858  av_log(c->fc, AV_LOG_WARNING, "lhvC atom truncated\n");
8859  return 0;
8860  }
8861 
8862  num_arrays = buf[5];
8863  old_size = st->codecpar->extradata_size;
8864  atom.size -= 8 /* account for mov_realloc_extradata offsetting */
8865  + 6 /* lhvC bytes before the arrays*/;
8866 
8867  ret = mov_realloc_extradata(st->codecpar, atom);
8868  if (ret < 0) {
8869  av_free(buf);
8870  return ret;
8871  }
8872 
8873  st->codecpar->extradata[22] += num_arrays;
8874  memcpy(st->codecpar->extradata + old_size, buf + 6, atom.size + 8);
8875 
8877 
8878  av_free(buf);
8879  return 0;
8880 }
8881 
8883 {
8884  AVFormatContext *ctx = c->fc;
8885  AVStream *st = NULL;
8886  AVBPrint scheme_buf, value_buf;
8887  int64_t scheme_str_len = 0, value_str_len = 0;
8888  int version, flags, ret = AVERROR_BUG;
8889  int64_t size = atom.size;
8890 
8891  if (atom.size < 6)
8892  // 4 bytes for version + flags, 2x 1 byte for null
8893  return AVERROR_INVALIDDATA;
8894 
8895  if (c->fc->nb_streams < 1)
8896  return 0;
8897  st = c->fc->streams[c->fc->nb_streams-1];
8898 
8899  version = avio_r8(pb);
8900  flags = avio_rb24(pb);
8901  size -= 4;
8902 
8903  if (version != 0 || flags != 0) {
8905  "Unsupported 'kind' box with version %d, flags: %x",
8906  version, flags);
8907  return AVERROR_INVALIDDATA;
8908  }
8909 
8910  av_bprint_init(&scheme_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8911  av_bprint_init(&value_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8912 
8913  if ((scheme_str_len = ff_read_string_to_bprint_overwrite(pb, &scheme_buf,
8914  size)) < 0) {
8915  ret = scheme_str_len;
8916  goto cleanup;
8917  }
8918 
8919  if (scheme_str_len + 1 >= size) {
8920  // we need to have another string, even if nullptr.
8921  // we check with + 1 since we expect that if size was not hit,
8922  // an additional null was read.
8924  goto cleanup;
8925  }
8926 
8927  size -= scheme_str_len + 1;
8928 
8929  if ((value_str_len = ff_read_string_to_bprint_overwrite(pb, &value_buf,
8930  size)) < 0) {
8931  ret = value_str_len;
8932  goto cleanup;
8933  }
8934 
8935  if (value_str_len == size) {
8936  // in case of no trailing null, box is not valid.
8938  goto cleanup;
8939  }
8940 
8942  "%s stream %d KindBox(scheme: %s, value: %s)\n",
8944  st->index,
8945  scheme_buf.str, value_buf.str);
8946 
8947  for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
8949  if (!av_strstart(scheme_buf.str, map.scheme_uri, NULL))
8950  continue;
8951 
8952  for (int j = 0; map.value_maps[j].disposition; j++) {
8953  const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
8954  if (!av_strstart(value_buf.str, value_map.value, NULL))
8955  continue;
8956 
8957  st->disposition |= value_map.disposition;
8958  }
8959  }
8960 
8961  ret = 0;
8962 
8963 cleanup:
8964 
8965  av_bprint_finalize(&scheme_buf, NULL);
8966  av_bprint_finalize(&value_buf, NULL);
8967 
8968  return ret;
8969 }
8970 
8972 {
8973  AVStream *st;
8974  AVChannelLayout ch_layout = { 0 };
8975  int ret, i, version, type;
8976  int ambisonic_order, channel_order, normalization, channel_count;
8977  int ambi_channels, non_diegetic_channels;
8978 
8979  if (c->fc->nb_streams < 1)
8980  return 0;
8981 
8982  st = c->fc->streams[c->fc->nb_streams - 1];
8983 
8984  if (atom.size < 16) {
8985  av_log(c->fc, AV_LOG_ERROR, "SA3D audio box too small\n");
8986  return AVERROR_INVALIDDATA;
8987  }
8988 
8989  version = avio_r8(pb);
8990  if (version) {
8991  av_log(c->fc, AV_LOG_WARNING, "Unsupported SA3D box version %d\n", version);
8992  return 0;
8993  }
8994 
8995  type = avio_r8(pb);
8996  if (type & 0x7f) {
8997  av_log(c->fc, AV_LOG_WARNING,
8998  "Unsupported ambisonic type %d\n", type & 0x7f);
8999  return 0;
9000  }
9001  non_diegetic_channels = (type >> 7) * 2; // head_locked_stereo
9002 
9003  ambisonic_order = avio_rb32(pb);
9004 
9005  channel_order = avio_r8(pb);
9006  if (channel_order) {
9007  av_log(c->fc, AV_LOG_WARNING,
9008  "Unsupported channel_order %d\n", channel_order);
9009  return 0;
9010  }
9011 
9012  normalization = avio_r8(pb);
9013  if (normalization) {
9014  av_log(c->fc, AV_LOG_WARNING,
9015  "Unsupported normalization %d\n", normalization);
9016  return 0;
9017  }
9018 
9019  channel_count = avio_rb32(pb);
9020  if (ambisonic_order < 0 || ambisonic_order > 31 ||
9021  channel_count != ((ambisonic_order + 1LL) * (ambisonic_order + 1LL) +
9022  non_diegetic_channels)) {
9023  av_log(c->fc, AV_LOG_ERROR,
9024  "Invalid number of channels (%d / %d)\n",
9025  channel_count, ambisonic_order);
9026  return 0;
9027  }
9028  ambi_channels = channel_count - non_diegetic_channels;
9029 
9030  ret = av_channel_layout_custom_init(&ch_layout, channel_count);
9031  if (ret < 0)
9032  return 0;
9033 
9034  for (i = 0; i < channel_count; i++) {
9035  unsigned channel = avio_rb32(pb);
9036 
9037  if (channel >= channel_count) {
9038  av_log(c->fc, AV_LOG_ERROR, "Invalid channel index (%d / %d)\n",
9039  channel, ambisonic_order);
9040  av_channel_layout_uninit(&ch_layout);
9041  return 0;
9042  }
9043  if (channel >= ambi_channels)
9044  ch_layout.u.map[i].id = channel - ambi_channels;
9045  else
9046  ch_layout.u.map[i].id = AV_CHAN_AMBISONIC_BASE + channel;
9047  }
9048 
9050  if (ret < 0) {
9051  av_channel_layout_uninit(&ch_layout);
9052  return 0;
9053  }
9054 
9056  st->codecpar->ch_layout = ch_layout;
9057 
9058  return 0;
9059 }
9060 
9062 {
9063  AVStream *st;
9064  int version;
9065 
9066  if (c->fc->nb_streams < 1)
9067  return 0;
9068 
9069  st = c->fc->streams[c->fc->nb_streams - 1];
9070 
9071  if (atom.size < 5) {
9072  av_log(c->fc, AV_LOG_ERROR, "Empty SAND audio box\n");
9073  return AVERROR_INVALIDDATA;
9074  }
9075 
9076  version = avio_r8(pb);
9077  if (version) {
9078  av_log(c->fc, AV_LOG_WARNING, "Unsupported SAND box version %d\n", version);
9079  return 0;
9080  }
9081 
9083 
9084  return 0;
9085 }
9086 
9087 static int rb_size(AVIOContext *pb, int64_t *value, int size)
9088 {
9089  if (size == 0)
9090  *value = 0;
9091  else if (size == 1)
9092  *value = avio_r8(pb);
9093  else if (size == 2)
9094  *value = avio_rb16(pb);
9095  else if (size == 4)
9096  *value = avio_rb32(pb);
9097  else if (size == 8) {
9098  *value = avio_rb64(pb);
9099  if (*value < 0)
9100  return -1;
9101  } else
9102  return -1;
9103  return size;
9104 }
9105 
9107 {
9108  avio_rb32(pb); // version & flags.
9109  c->primary_item_id = avio_rb16(pb);
9110  av_log(c->fc, AV_LOG_TRACE, "pitm: primary_item_id %d\n", c->primary_item_id);
9111  return atom.size;
9112 }
9113 
9115 {
9116  c->idat_offset = avio_tell(pb);
9117  return 0;
9118 }
9119 
9121 {
9122  HEIFItem **heif_item;
9123  int version, offset_size, length_size, base_offset_size, index_size;
9124  int item_count, extent_count;
9125  int64_t base_offset, extent_offset, extent_length;
9126  uint8_t value;
9127 
9128  if (c->found_iloc) {
9129  av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n");
9130  return 0;
9131  }
9132 
9133  version = avio_r8(pb);
9134  avio_rb24(pb); // flags.
9135 
9136  value = avio_r8(pb);
9137  offset_size = (value >> 4) & 0xF;
9138  length_size = value & 0xF;
9139  value = avio_r8(pb);
9140  base_offset_size = (value >> 4) & 0xF;
9141  index_size = !version ? 0 : (value & 0xF);
9142  if (index_size) {
9143  avpriv_report_missing_feature(c->fc, "iloc: index_size != 0");
9144  return AVERROR_PATCHWELCOME;
9145  }
9146  item_count = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
9147 
9148  heif_item = av_realloc_array(c->heif_item, FFMAX(item_count, c->nb_heif_item), sizeof(*c->heif_item));
9149  if (!heif_item)
9150  return AVERROR(ENOMEM);
9151  c->heif_item = heif_item;
9152  if (item_count > c->nb_heif_item)
9153  memset(&c->heif_item[c->nb_heif_item], 0,
9154  sizeof(*c->heif_item) * (item_count - c->nb_heif_item));
9155  c->nb_heif_item = FFMAX(c->nb_heif_item, item_count);
9156 
9157  av_log(c->fc, AV_LOG_TRACE, "iloc: item_count %d\n", item_count);
9158  for (int i = 0; i < item_count; i++) {
9159  HEIFItem *item = NULL;
9160  int item_id = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
9161  int offset_type = (version > 0) ? avio_rb16(pb) & 0xf : 0;
9162  int j;
9163 
9164  if (avio_feof(pb))
9165  return AVERROR_INVALIDDATA;
9166  if (offset_type > 1) {
9167  avpriv_report_missing_feature(c->fc, "iloc offset type %d", offset_type);
9168  return AVERROR_PATCHWELCOME;
9169  }
9170 
9171  avio_rb16(pb); // data_reference_index.
9172  if (rb_size(pb, &base_offset, base_offset_size) < 0)
9173  return AVERROR_INVALIDDATA;
9174  extent_count = avio_rb16(pb);
9175  if (extent_count > 1) {
9176  // For still AVIF images, we only support one extent item.
9177  avpriv_report_missing_feature(c->fc, "iloc: extent_count > 1");
9178  return AVERROR_PATCHWELCOME;
9179  }
9180 
9181  if (rb_size(pb, &extent_offset, offset_size) < 0 ||
9182  rb_size(pb, &extent_length, length_size) < 0 ||
9183  base_offset > INT64_MAX - extent_offset)
9184  return AVERROR_INVALIDDATA;
9185 
9186  for (j = 0; j < c->nb_heif_item; j++) {
9187  item = c->heif_item[j];
9188  if (!item)
9189  item = c->heif_item[j] = av_mallocz(sizeof(*item));
9190  else if (item->item_id != item_id)
9191  continue;
9192  break;
9193  }
9194  if (!item)
9195  return AVERROR(ENOMEM);
9196  if (j == c->nb_heif_item)
9197  return AVERROR_INVALIDDATA;
9198 
9199  item->item_id = item_id;
9200 
9201  if (offset_type == 1)
9202  item->is_idat_relative = 1;
9203  item->extent_length = extent_length;
9204  item->extent_offset = base_offset + extent_offset;
9205  av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, item->item_id %d, offset_type %d, "
9206  "extent_offset %"PRId64", extent_length %"PRId64"\n",
9207  i, item->item_id, offset_type, item->extent_offset, item->extent_length);
9208  }
9209 
9210  c->found_iloc = 1;
9211  return atom.size;
9212 }
9213 
9215 {
9216  HEIFItem *item = NULL;
9217  AVBPrint item_name;
9218  int64_t size = atom.size;
9219  uint32_t item_type;
9220  int item_id;
9221  int i, version, ret;
9222 
9223  version = avio_r8(pb);
9224  avio_rb24(pb); // flags.
9225  size -= 4;
9226  if (size < 0)
9227  return AVERROR_INVALIDDATA;
9228 
9229  if (version < 2) {
9230  avpriv_report_missing_feature(c->fc, "infe version < 2");
9231  avio_skip(pb, size);
9232  return 1;
9233  }
9234 
9235  item_id = version > 2 ? avio_rb32(pb) : avio_rb16(pb);
9236  avio_rb16(pb); // item_protection_index
9237  item_type = avio_rl32(pb);
9238  size -= 8;
9239  if (size < 1)
9240  return AVERROR_INVALIDDATA;
9241 
9244  if (ret < 0) {
9246  return ret;
9247  }
9248 
9249  av_log(c->fc, AV_LOG_TRACE, "infe: item_id %d, item_type %s, item_name %s\n",
9250  item_id, av_fourcc2str(item_type), item_name.str);
9251 
9252  size -= ret + 1;
9253  if (size > 0)
9254  avio_skip(pb, size);
9255 
9256  for (i = 0; i < c->nb_heif_item; i++) {
9257  item = c->heif_item[i];
9258  if (!item)
9259  item = c->heif_item[i] = av_mallocz(sizeof(*item));
9260  else if (item->item_id != item_id)
9261  continue;
9262  break;
9263  }
9264  if (!item) {
9266  return AVERROR(ENOMEM);
9267  }
9268  if (i == c->nb_heif_item) {
9270  return AVERROR_INVALIDDATA;
9271  }
9272 
9273  av_freep(&item->name);
9274  av_bprint_finalize(&item_name, ret ? &item->name : NULL);
9275  item->item_id = item_id;
9276  item->type = item_type;
9277 
9278  switch (item_type) {
9279  case MKTAG('a','v','0','1'):
9280  case MKTAG('j','p','e','g'):
9281  case MKTAG('h','v','c','1'):
9282  ret = heif_add_stream(c, item);
9283  if (ret < 0)
9284  return ret;
9285  break;
9286  }
9287 
9288  return 0;
9289 }
9290 
9292 {
9293  HEIFItem **heif_item;
9294  int entry_count;
9295  int version, got_stream = 0, ret, i;
9296 
9297  if (c->found_iinf) {
9298  av_log(c->fc, AV_LOG_WARNING, "Duplicate iinf box found\n");
9299  return 0;
9300  }
9301 
9302  version = avio_r8(pb);
9303  avio_rb24(pb); // flags.
9304  entry_count = version ? avio_rb32(pb) : avio_rb16(pb);
9305 
9306  heif_item = av_realloc_array(c->heif_item, FFMAX(entry_count, c->nb_heif_item), sizeof(*c->heif_item));
9307  if (!heif_item)
9308  return AVERROR(ENOMEM);
9309  c->heif_item = heif_item;
9310  if (entry_count > c->nb_heif_item)
9311  memset(&c->heif_item[c->nb_heif_item], 0,
9312  sizeof(*c->heif_item) * (entry_count - c->nb_heif_item));
9313  c->nb_heif_item = FFMAX(c->nb_heif_item, entry_count);
9314 
9315  for (i = 0; i < entry_count; i++) {
9316  MOVAtom infe;
9317 
9318  infe.size = avio_rb32(pb) - 8;
9319  infe.type = avio_rl32(pb);
9320  if (avio_feof(pb)) {
9322  goto fail;
9323  }
9324  ret = mov_read_infe(c, pb, infe);
9325  if (ret < 0)
9326  goto fail;
9327  if (!ret)
9328  got_stream = 1;
9329  }
9330 
9331  c->found_iinf = got_stream;
9332  return 0;
9333 fail:
9334  for (; i >= 0; i--) {
9335  HEIFItem *item = c->heif_item[i];
9336 
9337  if (!item)
9338  continue;
9339 
9340  av_freep(&item->name);
9341  }
9342  return ret;
9343 }
9344 
9346 {
9347  HEIFItem *item = NULL;
9348  HEIFGrid *grid;
9349  int entries, i;
9350  int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9351  int ret = 0;
9352 
9353  for (int i = 0; i < c->nb_heif_grid; i++) {
9354  if (c->heif_grid[i].item->item_id == from_item_id) {
9355  av_log(c->fc, AV_LOG_ERROR, "More than one 'dimg' box "
9356  "referencing the same Derived Image item\n");
9357  return AVERROR_INVALIDDATA;
9358  }
9359  }
9360  for (int i = 0; i < c->nb_heif_item; i++) {
9361  if (!c->heif_item[i] || c->heif_item[i]->item_id != from_item_id)
9362  continue;
9363  item = c->heif_item[i];
9364 
9365  switch (item->type) {
9366  case MKTAG('g','r','i','d'):
9367  case MKTAG('i','o','v','l'):
9368  break;
9369  default:
9370  avpriv_report_missing_feature(c->fc, "Derived Image item of type %s",
9371  av_fourcc2str(item->type));
9372  return 0;
9373  }
9374  break;
9375  }
9376  if (!item) {
9377  av_log(c->fc, AV_LOG_ERROR, "Missing grid information\n");
9378  return AVERROR_INVALIDDATA;
9379  }
9380 
9381  entries = avio_rb16(pb);
9382  if (!entries) {
9383  av_log(c->fc, AV_LOG_ERROR,
9384  "Derived image item references no input images\n");
9385  return AVERROR_INVALIDDATA;
9386  }
9387 
9388  grid = av_realloc_array(c->heif_grid, c->nb_heif_grid + 1U,
9389  sizeof(*c->heif_grid));
9390  if (!grid)
9391  return AVERROR(ENOMEM);
9392  c->heif_grid = grid;
9393  grid = &grid[c->nb_heif_grid];
9394 
9395  grid->tile_id_list = av_malloc_array(entries, sizeof(*grid->tile_id_list));
9396  grid->tile_idx_list = av_calloc(entries, sizeof(*grid->tile_idx_list));
9397  grid->tile_item_list = av_calloc(entries, sizeof(*grid->tile_item_list));
9398  if (!grid->tile_id_list || !grid->tile_item_list || !grid->tile_idx_list) {
9399  ret = AVERROR(ENOMEM);
9400  goto fail;
9401  }
9402  /* 'to' item ids */
9403  for (i = 0; i < entries; i++) {
9404  grid->tile_id_list[i] = version ? avio_rb32(pb) : avio_rb16(pb);
9405 
9406  if (avio_feof(pb)) {
9408  goto fail;
9409  }
9410  }
9411 
9412  grid->nb_tiles = entries;
9413  grid->item = item;
9414  ++c->nb_heif_grid;
9415 
9416  av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n",
9417  from_item_id, entries);
9418 
9419  return 0;
9420 fail:
9421  av_freep(&grid->tile_id_list);
9422  av_freep(&grid->tile_idx_list);
9423  av_freep(&grid->tile_item_list);
9424 
9425  return ret;
9426 }
9427 
9428 static int mov_read_iref_cdsc(MOVContext *c, AVIOContext *pb, uint32_t type, int version)
9429 {
9430  HEIFItem *from_item = NULL;
9431  int entries;
9432  int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9433  const HEIFItemRef ref = { type, from_item_id };
9434 
9435  from_item = get_heif_item(c, from_item_id);
9436  if (!from_item) {
9437  av_log(c->fc, AV_LOG_ERROR, "Missing stream referenced by thmb item\n");
9438  return AVERROR_INVALIDDATA;
9439  }
9440 
9441  entries = avio_rb16(pb);
9442  /* 'to' item ids */
9443  for (int i = 0; i < entries; i++) {
9444  HEIFItem *item = get_heif_item(c, version ? avio_rb32(pb) : avio_rb16(pb));
9445 
9446  if (avio_feof(pb))
9447  return AVERROR_INVALIDDATA;
9448 
9449  if (!item) {
9450  av_log(c->fc, AV_LOG_WARNING, "Missing stream referenced by %s item\n",
9451  av_fourcc2str(type));
9452  continue;
9453  }
9454 
9455  if (!av_dynarray2_add((void **)&item->iref_list, &item->nb_iref_list,
9456  sizeof(*item->iref_list), (const uint8_t *)&ref))
9457  return AVERROR(ENOMEM);
9458  }
9459 
9460  av_log(c->fc, AV_LOG_TRACE, "%s: from_item_id %d, entries %d\n",
9461  av_fourcc2str(type), from_item_id, entries);
9462 
9463  return 0;
9464 }
9465 
9467 {
9468  int version = avio_r8(pb);
9469  int ret;
9470 
9471  avio_rb24(pb); // flags
9472  atom.size -= 4;
9473 
9474  if (version > 1) {
9475  av_log(c->fc, AV_LOG_WARNING, "Unknown iref box version %d\n", version);
9476  return 0;
9477  }
9478 
9479  while (atom.size) {
9480  uint32_t type, size = avio_rb32(pb);
9481  int64_t next = avio_tell(pb);
9482 
9483  if (size < 14 || next < 0 || next > INT64_MAX - size)
9484  return AVERROR_INVALIDDATA;
9485 
9486  next += size - 4;
9487  type = avio_rl32(pb);
9488  switch (type) {
9489  case MKTAG('d','i','m','g'):
9490  ret = mov_read_iref_dimg(c, pb, version);
9491  if (ret < 0)
9492  return ret;
9493  break;
9494  case MKTAG('c','d','s','c'):
9495  case MKTAG('t','h','m','b'):
9496  ret = mov_read_iref_cdsc(c, pb, type, version);
9497  if (ret < 0)
9498  return ret;
9499  break;
9500  default:
9501  av_log(c->fc, AV_LOG_DEBUG, "Unknown iref type %s size %"PRIu32"\n",
9502  av_fourcc2str(type), size);
9503  }
9504 
9505  atom.size -= size;
9506  avio_seek(pb, next, SEEK_SET);
9507  }
9508  return 0;
9509 }
9510 
9512 {
9513  HEIFItem *item;
9514  uint32_t width, height;
9515 
9516  avio_r8(pb); /* version */
9517  avio_rb24(pb); /* flags */
9518  width = avio_rb32(pb);
9519  height = avio_rb32(pb);
9520 
9521  av_log(c->fc, AV_LOG_TRACE, "ispe: item_id %d, width %"PRIu32", height %"PRIu32"\n",
9522  c->cur_item_id, width, height);
9523 
9524  if (!width || !height || width > INT_MAX || height > INT_MAX) {
9525  av_log(c->fc, AV_LOG_ERROR, "Invalid ispe dimensions %"PRIu32"x%"PRIu32"\n",
9526  width, height);
9527  return AVERROR_INVALIDDATA;
9528  }
9529 
9530  item = get_heif_item(c, c->cur_item_id);
9531  if (item) {
9532  item->width = width;
9533  item->height = height;
9534  }
9535 
9536  return 0;
9537 }
9538 
9540 {
9541  HEIFItem *item;
9542  int angle;
9543 
9544  angle = avio_r8(pb) & 0x3;
9545 
9546  av_log(c->fc, AV_LOG_TRACE, "irot: item_id %d, angle %u\n",
9547  c->cur_item_id, angle);
9548 
9549  item = get_heif_item(c, c->cur_item_id);
9550  if (item) {
9551  // angle * 90 specifies the angle (in anti-clockwise direction)
9552  // in units of degrees.
9553  item->rotation = angle * 90;
9554  }
9555 
9556  return 0;
9557 }
9558 
9560 {
9561  HEIFItem *item;
9562  int axis;
9563 
9564  axis = avio_r8(pb) & 0x1;
9565 
9566  av_log(c->fc, AV_LOG_TRACE, "imir: item_id %d, axis %u\n",
9567  c->cur_item_id, axis);
9568 
9569  item = get_heif_item(c, c->cur_item_id);
9570  if (item) {
9571  item->hflip = axis;
9572  item->vflip = !axis;
9573  }
9574 
9575  return 0;
9576 }
9577 
9579 {
9580  typedef struct MOVAtoms {
9581  FFIOContext b;
9582  uint32_t type;
9583  int64_t size;
9584  uint8_t *data;
9585  } MOVAtoms;
9586  MOVAtoms *atoms = NULL;
9587  MOVAtom a;
9588  unsigned count;
9589  int nb_atoms = 0;
9590  int version, flags;
9591  int ret;
9592 
9593  a.size = avio_rb32(pb);
9594  a.type = avio_rl32(pb);
9595 
9596  if (a.size < 8 || a.type != MKTAG('i','p','c','o'))
9597  return AVERROR_INVALIDDATA;
9598 
9599  a.size -= 8;
9600  while (a.size >= 8) {
9601  MOVAtoms *ref = av_dynarray2_add((void**)&atoms, &nb_atoms, sizeof(MOVAtoms), NULL);
9602  if (!ref) {
9603  ret = AVERROR(ENOMEM);
9604  goto fail;
9605  }
9606  ref->data = NULL;
9607  ref->size = avio_rb32(pb);
9608  ref->type = avio_rl32(pb);
9609  if (ref->size > a.size || ref->size < 8)
9610  break;
9611  ref->data = av_malloc(ref->size);
9612  if (!ref->data) {
9614  goto fail;
9615  }
9616  av_log(c->fc, AV_LOG_TRACE, "ipco: index %d, box type %s\n", nb_atoms, av_fourcc2str(ref->type));
9617  avio_seek(pb, -8, SEEK_CUR);
9618  if (avio_read(pb, ref->data, ref->size) != ref->size) {
9620  goto fail;
9621  }
9622  ffio_init_read_context(&ref->b, ref->data, ref->size);
9623  a.size -= ref->size;
9624  }
9625 
9626  if (a.size) {
9628  goto fail;
9629  }
9630 
9631  a.size = avio_rb32(pb);
9632  a.type = avio_rl32(pb);
9633 
9634  if (a.size < 8 || a.type != MKTAG('i','p','m','a')) {
9636  goto fail;
9637  }
9638 
9639  version = avio_r8(pb);
9640  flags = avio_rb24(pb);
9641  count = avio_rb32(pb);
9642 
9643  for (int i = 0; i < count; i++) {
9644  int item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9645  int assoc_count = avio_r8(pb);
9646 
9647  if (avio_feof(pb)) {
9649  goto fail;
9650  }
9651 
9652  for (int j = 0; j < assoc_count; j++) {
9653  MOVAtoms *ref;
9654  int index = avio_r8(pb) & 0x7f;
9655  if (flags & 1) {
9656  index <<= 8;
9657  index |= avio_r8(pb);
9658  }
9659  if (index > nb_atoms || index <= 0) {
9661  goto fail;
9662  }
9663  ref = &atoms[--index];
9664 
9665  av_log(c->fc, AV_LOG_TRACE, "ipma: property_index %d, item_id %d, item_type %s\n",
9666  index + 1, item_id, av_fourcc2str(ref->type));
9667 
9668  c->cur_item_id = item_id;
9669 
9670  ret = mov_read_default(c, &ref->b.pub,
9671  (MOVAtom) { .size = ref->size,
9672  .type = MKTAG('i','p','c','o') });
9673  if (ret < 0)
9674  goto fail;
9675  ffio_init_read_context(&ref->b, ref->data, ref->size);
9676  }
9677  }
9678 
9679  ret = 0;
9680 fail:
9681  c->cur_item_id = -1;
9682  for (int i = 0; i < nb_atoms; i++)
9683  av_free(atoms[i].data);
9684  av_free(atoms);
9685 
9686  return ret;
9687 }
9688 
9690 { MKTAG('A','C','L','R'), mov_read_aclr },
9691 { MKTAG('A','P','R','G'), mov_read_avid },
9692 { MKTAG('A','A','L','P'), mov_read_avid },
9693 { MKTAG('A','R','E','S'), mov_read_ares },
9694 { MKTAG('a','v','s','s'), mov_read_avss },
9695 { MKTAG('a','v','1','C'), mov_read_glbl },
9696 { MKTAG('c','h','p','l'), mov_read_chpl },
9697 { MKTAG('c','o','6','4'), mov_read_stco },
9698 { MKTAG('c','o','l','r'), mov_read_colr },
9699 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
9700 { MKTAG('d','i','n','f'), mov_read_default },
9701 { MKTAG('D','p','x','E'), mov_read_dpxe },
9702 { MKTAG('d','r','e','f'), mov_read_dref },
9703 { MKTAG('e','d','t','s'), mov_read_default },
9704 { MKTAG('e','l','s','t'), mov_read_elst },
9705 { MKTAG('e','n','d','a'), mov_read_enda },
9706 { MKTAG('f','i','e','l'), mov_read_fiel },
9707 { MKTAG('a','d','r','m'), mov_read_adrm },
9708 { MKTAG('f','t','y','p'), mov_read_ftyp },
9709 { MKTAG('g','l','b','l'), mov_read_glbl },
9710 { MKTAG('h','d','l','r'), mov_read_hdlr },
9711 { MKTAG('i','l','s','t'), mov_read_ilst },
9712 { MKTAG('j','p','2','h'), mov_read_jp2h },
9713 { MKTAG('m','d','a','t'), mov_read_mdat },
9714 { MKTAG('m','d','h','d'), mov_read_mdhd },
9715 { MKTAG('m','d','i','a'), mov_read_default },
9716 { MKTAG('m','e','t','a'), mov_read_meta },
9717 { MKTAG('m','i','n','f'), mov_read_default },
9718 { MKTAG('m','o','o','f'), mov_read_moof },
9719 { MKTAG('m','o','o','v'), mov_read_moov },
9720 { MKTAG('m','v','e','x'), mov_read_default },
9721 { MKTAG('m','v','h','d'), mov_read_mvhd },
9722 { MKTAG('S','M','I',' '), mov_read_svq3 },
9723 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
9724 { MKTAG('a','v','c','C'), mov_read_glbl },
9725 { MKTAG('p','a','s','p'), mov_read_pasp },
9726 { MKTAG('c','l','a','p'), mov_read_clap },
9727 { MKTAG('s','b','a','s'), mov_read_sbas },
9728 { MKTAG('v','d','e','p'), mov_read_vdep },
9729 { MKTAG('s','i','d','x'), mov_read_sidx },
9730 { MKTAG('s','t','b','l'), mov_read_default },
9731 { MKTAG('s','t','c','o'), mov_read_stco },
9732 { MKTAG('s','t','p','s'), mov_read_stps },
9733 { MKTAG('s','t','r','f'), mov_read_strf },
9734 { MKTAG('s','t','s','c'), mov_read_stsc },
9735 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
9736 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
9737 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
9738 { MKTAG('s','t','t','s'), mov_read_stts },
9739 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
9740 { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
9741 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
9742 { MKTAG('t','f','d','t'), mov_read_tfdt },
9743 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
9744 { MKTAG('t','r','a','k'), mov_read_trak },
9745 { MKTAG('t','r','a','f'), mov_read_default },
9746 { MKTAG('t','r','e','f'), mov_read_default },
9747 { MKTAG('t','m','c','d'), mov_read_tmcd },
9748 { MKTAG('c','h','a','p'), mov_read_chap },
9749 { MKTAG('t','r','e','x'), mov_read_trex },
9750 { MKTAG('t','r','u','n'), mov_read_trun },
9751 { MKTAG('u','d','t','a'), mov_read_default },
9752 { MKTAG('w','a','v','e'), mov_read_wave },
9753 { MKTAG('e','s','d','s'), mov_read_esds },
9754 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
9755 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
9756 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
9757 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
9758 { MKTAG('w','f','e','x'), mov_read_wfex },
9759 { MKTAG('c','m','o','v'), mov_read_cmov },
9760 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout from quicktime */
9761 { MKTAG('c','h','n','l'), mov_read_chnl }, /* channel layout from ISO-14496-12 */
9762 { MKTAG('d','v','c','1'), mov_read_dvc1 },
9763 { MKTAG('s','g','p','d'), mov_read_sgpd },
9764 { MKTAG('s','b','g','p'), mov_read_sbgp },
9765 { MKTAG('h','v','c','C'), mov_read_glbl },
9766 { MKTAG('v','v','c','C'), mov_read_glbl },
9767 { MKTAG('u','u','i','d'), mov_read_uuid },
9768 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
9769 { MKTAG('f','r','e','e'), mov_read_free },
9770 { MKTAG('-','-','-','-'), mov_read_custom },
9771 { MKTAG('s','i','n','f'), mov_read_default },
9772 { MKTAG('f','r','m','a'), mov_read_frma },
9773 { MKTAG('s','e','n','c'), mov_read_senc },
9774 { MKTAG('s','a','i','z'), mov_read_saiz },
9775 { MKTAG('s','a','i','o'), mov_read_saio },
9776 { MKTAG('p','s','s','h'), mov_read_pssh },
9777 { MKTAG('s','c','h','m'), mov_read_schm },
9778 { MKTAG('s','c','h','i'), mov_read_default },
9779 { MKTAG('t','e','n','c'), mov_read_tenc },
9780 { MKTAG('d','f','L','a'), mov_read_dfla },
9781 { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
9782 { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
9783 { MKTAG('v','e','x','u'), mov_read_vexu }, /* video extension usage */
9784 { MKTAG('h','f','o','v'), mov_read_hfov },
9785 { MKTAG('d','O','p','s'), mov_read_dops },
9786 { MKTAG('d','m','l','p'), mov_read_dmlp },
9787 { MKTAG('S','m','D','m'), mov_read_smdm },
9788 { MKTAG('C','o','L','L'), mov_read_coll },
9789 { MKTAG('v','p','c','C'), mov_read_vpcc },
9790 { MKTAG('m','d','c','v'), mov_read_mdcv },
9791 { MKTAG('c','l','l','i'), mov_read_clli },
9792 { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
9793 { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
9794 { MKTAG('d','v','w','C'), mov_read_dvcc_dvvc },
9795 { MKTAG('h','v','c','E'), mov_read_hvce },
9796 { MKTAG('k','i','n','d'), mov_read_kind },
9797 { MKTAG('S','A','3','D'), mov_read_SA3D }, /* ambisonic audio box */
9798 { MKTAG('S','A','N','D'), mov_read_SAND }, /* non diegetic audio box */
9799 { MKTAG('i','l','o','c'), mov_read_iloc },
9800 { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */
9801 { MKTAG('p','i','t','m'), mov_read_pitm },
9802 { MKTAG('e','v','c','C'), mov_read_glbl },
9803 { MKTAG('i','d','a','t'), mov_read_idat },
9804 { MKTAG('i','m','i','r'), mov_read_imir },
9805 { MKTAG('i','r','e','f'), mov_read_iref },
9806 { MKTAG('i','s','p','e'), mov_read_ispe },
9807 { MKTAG('i','r','o','t'), mov_read_irot },
9808 { MKTAG('i','p','r','p'), mov_read_iprp },
9809 { MKTAG('i','i','n','f'), mov_read_iinf },
9810 { MKTAG('a','m','v','e'), mov_read_amve }, /* ambient viewing environment box */
9811 { MKTAG('l','h','v','C'), mov_read_lhvc },
9812 { MKTAG('l','v','c','C'), mov_read_glbl },
9813 { MKTAG('a','p','v','C'), mov_read_glbl },
9814 #if CONFIG_IAMFDEC
9815 { MKTAG('i','a','c','b'), mov_read_iacb },
9816 #endif
9817 { MKTAG('s','r','a','t'), mov_read_srat },
9818 { 0, NULL }
9819 };
9820 
9822 {
9823  int64_t total_size = 0;
9824  MOVAtom a;
9825  int i;
9826 
9827  if (c->atom_depth > 10) {
9828  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
9829  return AVERROR_INVALIDDATA;
9830  }
9831  c->atom_depth ++;
9832 
9833  if (atom.size < 0)
9834  atom.size = INT64_MAX;
9835  while (total_size <= atom.size - 8) {
9836  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
9837  a.size = avio_rb32(pb);
9838  a.type = avio_rl32(pb);
9839  if (avio_feof(pb))
9840  break;
9841  if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
9842  a.type == MKTAG('h','o','o','v')) &&
9843  a.size >= 8 &&
9844  c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
9845  uint32_t type;
9846  avio_skip(pb, 4);
9847  type = avio_rl32(pb);
9848  if (avio_feof(pb))
9849  break;
9850  avio_seek(pb, -8, SEEK_CUR);
9851  if (type == MKTAG('m','v','h','d') ||
9852  type == MKTAG('c','m','o','v')) {
9853  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
9854  a.type = MKTAG('m','o','o','v');
9855  }
9856  }
9857  if (atom.type != MKTAG('r','o','o','t') &&
9858  atom.type != MKTAG('m','o','o','v')) {
9859  if (a.type == MKTAG('t','r','a','k') ||
9860  a.type == MKTAG('m','d','a','t')) {
9861  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
9862  avio_skip(pb, -8);
9863  c->atom_depth --;
9864  return 0;
9865  }
9866  }
9867  total_size += 8;
9868  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
9869  a.size = avio_rb64(pb) - 8;
9870  total_size += 8;
9871  }
9872  av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
9873  av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
9874  if (a.size == 0) {
9875  a.size = atom.size - total_size + 8;
9876  }
9877  if (a.size < 0)
9878  break;
9879  a.size -= 8;
9880  if (a.size < 0)
9881  break;
9882  a.size = FFMIN(a.size, atom.size - total_size);
9883 
9884  for (i = 0; mov_default_parse_table[i].type; i++)
9885  if (mov_default_parse_table[i].type == a.type) {
9887  break;
9888  }
9889 
9890  // container is user data
9891  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
9892  atom.type == MKTAG('i','l','s','t')))
9894 
9895  // Supports parsing the QuickTime Metadata Keys.
9896  // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
9897  if (!parse && c->found_hdlr_mdta &&
9898  atom.type == MKTAG('m','e','t','a') &&
9899  a.type == MKTAG('k','e','y','s') &&
9900  c->meta_keys_count == 0) {
9901  parse = mov_read_keys;
9902  }
9903 
9904  if (!parse) { /* skip leaf atoms data */
9905  avio_skip(pb, a.size);
9906  } else {
9907  int64_t start_pos = avio_tell(pb);
9908  int64_t left;
9909  int err = parse(c, pb, a);
9910  if (err < 0) {
9911  c->atom_depth --;
9912  return err;
9913  }
9914  if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
9915  ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
9916  start_pos + a.size == avio_size(pb))) {
9917  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
9918  c->next_root_atom = start_pos + a.size;
9919  c->atom_depth --;
9920  return 0;
9921  }
9922  left = a.size - avio_tell(pb) + start_pos;
9923  if (left > 0) /* skip garbage at atom end */
9924  avio_skip(pb, left);
9925  else if (left < 0) {
9926  av_log(c->fc, AV_LOG_WARNING,
9927  "overread end of atom '%s' by %"PRId64" bytes\n",
9928  av_fourcc2str(a.type), -left);
9929  avio_seek(pb, left, SEEK_CUR);
9930  }
9931  }
9932 
9933  total_size += a.size;
9934  }
9935 
9936  if (total_size < atom.size && atom.size < 0x7ffff)
9937  avio_skip(pb, atom.size - total_size);
9938 
9939  c->atom_depth --;
9940  return 0;
9941 }
9942 
9943 static int mov_probe(const AVProbeData *p)
9944 {
9945  int64_t offset;
9946  uint32_t tag;
9947  int score = 0;
9948  int moov_offset = -1;
9949 
9950  /* check file header */
9951  offset = 0;
9952  for (;;) {
9953  int64_t size;
9954  int minsize = 8;
9955  /* ignore invalid offset */
9956  if ((offset + 8ULL) > (unsigned int)p->buf_size)
9957  break;
9958  size = AV_RB32(p->buf + offset);
9959  if (size == 1 && offset + 16 <= (unsigned int)p->buf_size) {
9960  size = AV_RB64(p->buf+offset + 8);
9961  minsize = 16;
9962  } else if (size == 0) {
9963  size = p->buf_size - offset;
9964  }
9965  if (size < minsize) {
9966  offset += 4;
9967  continue;
9968  }
9969  tag = AV_RL32(p->buf + offset + 4);
9970  switch(tag) {
9971  /* check for obvious tags */
9972  case MKTAG('m','o','o','v'):
9973  moov_offset = offset + 4;
9975  case MKTAG('m','d','a','t'):
9976  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
9977  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
9978  case MKTAG('f','t','y','p'):
9979  if (tag == MKTAG('f','t','y','p') &&
9980  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
9981  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
9982  || AV_RL32(p->buf + offset + 8) == MKTAG('j','x','l',' ')
9983  )) {
9984  score = FFMAX(score, 5);
9985  } else {
9986  score = AVPROBE_SCORE_MAX;
9987  }
9988  break;
9989  /* those are more common words, so rate then a bit less */
9990  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
9991  case MKTAG('w','i','d','e'):
9992  case MKTAG('f','r','e','e'):
9993  case MKTAG('j','u','n','k'):
9994  case MKTAG('p','i','c','t'):
9995  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
9996  break;
9997  case MKTAG(0x82,0x82,0x7f,0x7d):
9998  score = FFMAX(score, AVPROBE_SCORE_EXTENSION - 5);
9999  break;
10000  case MKTAG('s','k','i','p'):
10001  case MKTAG('u','u','i','d'):
10002  case MKTAG('p','r','f','l'):
10003  /* if we only find those cause probedata is too small at least rate them */
10004  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
10005  break;
10006  }
10007  if (size > INT64_MAX - offset)
10008  break;
10009  offset += size;
10010  }
10011  if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
10012  /* moov atom in the header - we should make sure that this is not a
10013  * MOV-packed MPEG-PS */
10014  offset = moov_offset;
10015 
10016  while (offset < (p->buf_size - 16)) { /* Sufficient space */
10017  /* We found an actual hdlr atom */
10018  if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
10019  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
10020  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
10021  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
10022  /* We found a media handler reference atom describing an
10023  * MPEG-PS-in-MOV, return a
10024  * low score to force expanding the probe window until
10025  * mpegps_probe finds what it needs */
10026  return 5;
10027  } else {
10028  /* Keep looking */
10029  offset += 2;
10030  }
10031  }
10032  }
10033 
10034  return score;
10035 }
10036 
10037 // must be done after parsing all trak because there's no order requirement
10039 {
10040  MOVContext *mov = s->priv_data;
10041  MOVStreamContext *sc;
10042  int64_t cur_pos;
10043  int i, j;
10044  int chapter_track;
10045 
10046  for (j = 0; j < mov->nb_chapter_tracks; j++) {
10047  AVStream *st = NULL;
10048  FFStream *sti = NULL;
10049  chapter_track = mov->chapter_tracks[j];
10050  for (i = 0; i < s->nb_streams; i++) {
10051  sc = mov->fc->streams[i]->priv_data;
10052  if (sc->id == chapter_track) {
10053  st = s->streams[i];
10054  break;
10055  }
10056  }
10057  if (!st) {
10058  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
10059  continue;
10060  }
10061  sti = ffstream(st);
10062 
10063  sc = st->priv_data;
10064  cur_pos = avio_tell(sc->pb);
10065 
10066  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
10068  if (!st->attached_pic.data && sti->nb_index_entries) {
10069  // Retrieve the first frame, if possible
10070  AVIndexEntry *sample = &sti->index_entries[0];
10071  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
10072  av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
10073  goto finish;
10074  }
10075 
10076  if (ff_add_attached_pic(s, st, sc->pb, NULL, sample->size) < 0)
10077  goto finish;
10078  }
10079  } else {
10082  st->discard = AVDISCARD_ALL;
10083  for (int i = 0; i < sti->nb_index_entries; i++) {
10084  AVIndexEntry *sample = &sti->index_entries[i];
10085  int64_t end = i+1 < sti->nb_index_entries ? sti->index_entries[i+1].timestamp : st->duration;
10086  uint8_t *title;
10087  uint16_t ch;
10088  int len, title_len;
10089 
10090  if (end < sample->timestamp) {
10091  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
10092  end = AV_NOPTS_VALUE;
10093  }
10094 
10095  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
10096  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
10097  goto finish;
10098  }
10099 
10100  // the first two bytes are the length of the title
10101  len = avio_rb16(sc->pb);
10102  if (len > sample->size-2)
10103  continue;
10104  title_len = 2*len + 1;
10105  if (!(title = av_mallocz(title_len)))
10106  goto finish;
10107 
10108  // The samples could theoretically be in any encoding if there's an encd
10109  // atom following, but in practice are only utf-8 or utf-16, distinguished
10110  // instead by the presence of a BOM
10111  if (!len) {
10112  title[0] = 0;
10113  } else {
10114  ch = avio_rb16(sc->pb);
10115  if (ch == 0xfeff)
10116  avio_get_str16be(sc->pb, len, title, title_len);
10117  else if (ch == 0xfffe)
10118  avio_get_str16le(sc->pb, len, title, title_len);
10119  else {
10120  AV_WB16(title, ch);
10121  if (len == 1 || len == 2)
10122  title[len] = 0;
10123  else
10124  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
10125  }
10126  }
10127 
10128  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
10129  av_freep(&title);
10130  }
10131  }
10132 finish:
10133  avio_seek(sc->pb, cur_pos, SEEK_SET);
10134  }
10135 }
10136 
10138  int64_t value, int flags)
10139 {
10140  AVTimecode tc;
10141  char buf[AV_TIMECODE_STR_SIZE];
10142  AVRational rate = st->avg_frame_rate;
10143  int ret = av_timecode_init(&tc, rate, flags, 0, s);
10144  if (ret < 0)
10145  return ret;
10146  av_dict_set(&st->metadata, "timecode",
10147  av_timecode_make_string(&tc, buf, value), 0);
10148  return 0;
10149 }
10150 
10152 {
10153  MOVStreamContext *sc = st->priv_data;
10154  FFStream *const sti = ffstream(st);
10155  char buf[AV_TIMECODE_STR_SIZE];
10156  int64_t cur_pos = avio_tell(sc->pb);
10157  int hh, mm, ss, ff, drop;
10158 
10159  if (!sti->nb_index_entries)
10160  return -1;
10161 
10162  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
10163  avio_skip(s->pb, 13);
10164  hh = avio_r8(s->pb);
10165  mm = avio_r8(s->pb);
10166  ss = avio_r8(s->pb);
10167  drop = avio_r8(s->pb);
10168  ff = avio_r8(s->pb);
10169  snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
10170  hh, mm, ss, drop ? ';' : ':', ff);
10171  av_dict_set(&st->metadata, "timecode", buf, 0);
10172 
10173  avio_seek(sc->pb, cur_pos, SEEK_SET);
10174  return 0;
10175 }
10176 
10178 {
10179  MOVStreamContext *sc = st->priv_data;
10180  FFStream *const sti = ffstream(st);
10181  int flags = 0;
10182  int64_t cur_pos = avio_tell(sc->pb);
10183  int64_t value;
10184  AVRational tc_rate = st->avg_frame_rate;
10185  int tmcd_nb_frames = sc->tmcd_nb_frames;
10186  int rounded_tc_rate;
10187 
10188  if (!sti->nb_index_entries)
10189  return -1;
10190 
10191  if (!tc_rate.num || !tc_rate.den || !tmcd_nb_frames)
10192  return -1;
10193 
10194  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
10195  value = avio_rb32(s->pb);
10196 
10197  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
10198  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
10199  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
10200 
10201  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
10202  * not the case) and thus assume "frame number format" instead of QT one.
10203  * No sample with tmcd track can be found with a QT timecode at the moment,
10204  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
10205  * format). */
10206 
10207  /* 60 fps content have tmcd_nb_frames set to 30 but tc_rate set to 60, so
10208  * we multiply the frame number with the quotient.
10209  * See tickets #9492, #9710. */
10210  rounded_tc_rate = (tc_rate.num + tc_rate.den / 2LL) / tc_rate.den;
10211  /* Work around files where tmcd_nb_frames is rounded down from frame rate
10212  * instead of up. See ticket #5978. */
10213  if (tmcd_nb_frames == tc_rate.num / tc_rate.den &&
10214  s->strict_std_compliance < FF_COMPLIANCE_STRICT)
10215  tmcd_nb_frames = rounded_tc_rate;
10216  value = av_rescale(value, rounded_tc_rate, tmcd_nb_frames);
10217 
10219 
10220  avio_seek(sc->pb, cur_pos, SEEK_SET);
10221  return 0;
10222 }
10223 
10225  int i;
10226  if (!index || !*index) return;
10227  for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
10228  av_encryption_info_free((*index)->encrypted_samples[i]);
10229  }
10230  av_freep(&(*index)->encrypted_samples);
10231  av_freep(&(*index)->auxiliary_info_sizes);
10232  av_freep(&(*index)->auxiliary_offsets);
10233  av_freep(index);
10234 }
10235 
10237 {
10238  MOVStreamContext *sc = st->priv_data;
10239 
10240  if (!sc || --sc->refcount) {
10241  st->priv_data = NULL;
10242  return;
10243  }
10244 
10245  av_freep(&sc->tts_data);
10246  for (int i = 0; i < sc->drefs_count; i++) {
10247  av_freep(&sc->drefs[i].path);
10248  av_freep(&sc->drefs[i].dir);
10249  }
10250  av_freep(&sc->drefs);
10251 
10252  sc->drefs_count = 0;
10253 
10254  if (!sc->pb_is_copied)
10255  ff_format_io_close(s, &sc->pb);
10256 
10257  sc->pb = NULL;
10258  av_freep(&sc->chunk_offsets);
10259  av_freep(&sc->stsc_data);
10260  av_freep(&sc->sample_sizes);
10261  av_freep(&sc->keyframes);
10262  av_freep(&sc->ctts_data);
10263  av_freep(&sc->stts_data);
10264  av_freep(&sc->sdtp_data);
10265  av_freep(&sc->stps_data);
10266  av_freep(&sc->elst_data);
10267  av_freep(&sc->rap_group);
10268  av_freep(&sc->sync_group);
10269  av_freep(&sc->sgpd_sync);
10270  av_freep(&sc->sample_offsets);
10271  av_freep(&sc->open_key_samples);
10272  av_freep(&sc->display_matrix);
10273  av_freep(&sc->index_ranges);
10274  for (int i = 0; i < sc->nb_tref_tags; i++)
10275  av_freep(&sc->tref_tags[i].id);
10276  av_freep(&sc->tref_tags);
10277 
10278  if (sc->extradata)
10279  for (int i = 0; i < sc->stsd_count; i++)
10280  av_free(sc->extradata[i]);
10281  av_freep(&sc->extradata);
10282  av_freep(&sc->extradata_size);
10283 
10287 
10288  av_freep(&sc->stereo3d);
10289  av_freep(&sc->spherical);
10290  av_freep(&sc->mastering);
10291  av_freep(&sc->coll);
10292  av_freep(&sc->ambient);
10293 
10294 #if CONFIG_IAMFDEC
10295  if (sc->iamf)
10297 #endif
10298  av_freep(&sc->iamf);
10299 }
10300 
10302 {
10303  MOVContext *mov = s->priv_data;
10304  int i, j;
10305 
10306  for (i = 0; i < s->nb_streams; i++) {
10307  AVStream *st = s->streams[i];
10308 
10310  }
10311 
10312  av_freep(&mov->dv_demux);
10314  mov->dv_fctx = NULL;
10315 
10316  if (mov->meta_keys) {
10317  for (i = 1; i < mov->meta_keys_count; i++) {
10318  av_freep(&mov->meta_keys[i]);
10319  }
10320  av_freep(&mov->meta_keys);
10321  }
10322 
10323  av_freep(&mov->trex_data);
10324  av_freep(&mov->bitrates);
10325 
10326  for (i = 0; i < mov->frag_index.nb_items; i++) {
10328  for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
10329  mov_free_encryption_index(&frag[j].encryption_index);
10330  }
10332  }
10333  av_freep(&mov->frag_index.item);
10334 
10335  av_freep(&mov->aes_decrypt);
10336  av_freep(&mov->chapter_tracks);
10337  for (i = 0; i < mov->nb_heif_item; i++) {
10338  if (!mov->heif_item[i])
10339  continue;
10340  av_freep(&mov->heif_item[i]->name);
10341  av_freep(&mov->heif_item[i]->iref_list);
10342  av_freep(&mov->heif_item[i]->icc_profile);
10343  av_freep(&mov->heif_item[i]);
10344  }
10345  av_freep(&mov->heif_item);
10346  for (i = 0; i < mov->nb_heif_grid; i++) {
10347  av_freep(&mov->heif_grid[i].tile_id_list);
10350  }
10351  av_freep(&mov->heif_grid);
10352 
10353  return 0;
10354 }
10355 
10356 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
10357 {
10358  int i;
10359 
10360  for (i = 0; i < s->nb_streams; i++) {
10361  AVStream *st = s->streams[i];
10362  MOVStreamContext *sc = st->priv_data;
10363  MovTref *tag = mov_find_tref_tag(sc, MKTAG('t','m','c','d'));
10364 
10365  if (!tag)
10366  continue;
10367  if (mov_find_tref_id(tag, tmcd_id))
10368  return 1;
10369  }
10370  return 0;
10371 }
10372 
10373 /* look for a tmcd track not referenced by any video track, and export it globally */
10375 {
10376  int i;
10377 
10378  for (i = 0; i < s->nb_streams; i++) {
10379  AVStream *st = s->streams[i];
10380 
10381  if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
10382  !tmcd_is_referenced(s, st->id)) {
10383  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
10384  if (tcr) {
10385  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
10386  break;
10387  }
10388  }
10389  }
10390 }
10391 
10392 static int read_tfra(MOVContext *mov, AVIOContext *f)
10393 {
10394  int version, fieldlength, i, j;
10395  int64_t pos = avio_tell(f);
10396  uint32_t size = avio_rb32(f);
10397  unsigned track_id, item_count;
10398 
10399  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
10400  return 1;
10401  }
10402  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
10403 
10404  version = avio_r8(f);
10405  avio_rb24(f);
10406  track_id = avio_rb32(f);
10407  fieldlength = avio_rb32(f);
10408  item_count = avio_rb32(f);
10409  for (i = 0; i < item_count; i++) {
10410  int64_t time, offset;
10411  int index;
10412  MOVFragmentStreamInfo * frag_stream_info;
10413 
10414  if (avio_feof(f)) {
10415  return AVERROR_INVALIDDATA;
10416  }
10417 
10418  if (version == 1) {
10419  time = avio_rb64(f);
10420  offset = avio_rb64(f);
10421  } else {
10422  time = avio_rb32(f);
10423  offset = avio_rb32(f);
10424  }
10425 
10426  // The first sample of each stream in a fragment is always a random
10427  // access sample. So it's entry in the tfra can be used as the
10428  // initial PTS of the fragment.
10429  index = update_frag_index(mov, offset);
10430  frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
10431  if (frag_stream_info &&
10432  frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
10433  frag_stream_info->first_tfra_pts = time;
10434 
10435  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
10436  avio_r8(f);
10437  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
10438  avio_r8(f);
10439  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
10440  avio_r8(f);
10441  }
10442 
10443  avio_seek(f, pos + size, SEEK_SET);
10444  return 0;
10445 }
10446 
10448 {
10449  int64_t stream_size = avio_size(f);
10450  int64_t original_pos = avio_tell(f);
10451  int64_t seek_ret;
10452  int ret = -1;
10453  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
10454  ret = seek_ret;
10455  goto fail;
10456  }
10457  c->mfra_size = avio_rb32(f);
10458  c->have_read_mfra_size = 1;
10459  if (!c->mfra_size || c->mfra_size > stream_size) {
10460  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
10461  goto fail;
10462  }
10463  if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
10464  ret = seek_ret;
10465  goto fail;
10466  }
10467  if (avio_rb32(f) != c->mfra_size) {
10468  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
10469  goto fail;
10470  }
10471  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
10472  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
10473  goto fail;
10474  }
10475  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
10476  do {
10477  ret = read_tfra(c, f);
10478  if (ret < 0)
10479  goto fail;
10480  } while (!ret);
10481  ret = 0;
10482  c->frag_index.complete = 1;
10483 fail:
10484  seek_ret = avio_seek(f, original_pos, SEEK_SET);
10485  if (seek_ret < 0) {
10486  av_log(c->fc, AV_LOG_ERROR,
10487  "failed to seek back after looking for mfra\n");
10488  ret = seek_ret;
10489  }
10490  return ret;
10491 }
10492 
10493 static int set_icc_profile_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10494  const HEIFItem *item)
10495 {
10496  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data, nb_coded_side_data,
10498  item->icc_profile_size, 0);
10499  if (!sd)
10500  return AVERROR(ENOMEM);
10501 
10502  memcpy(sd->data, item->icc_profile, item->icc_profile_size);
10503 
10504  return 0;
10505 }
10506 
10507 static int set_display_matrix_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10508  const HEIFItem *item)
10509 {
10510  int32_t *matrix;
10511  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data,
10512  nb_coded_side_data,
10514  9 * sizeof(*matrix), 0);
10515  if (!sd)
10516  return AVERROR(ENOMEM);
10517 
10518  matrix = (int32_t*)sd->data;
10519  /* rotation is in the counter-clockwise direction whereas
10520  * av_display_rotation_set() expects its argument to be
10521  * oriented clockwise, so we need to negate it. */
10523  av_display_matrix_flip(matrix, item->hflip, item->vflip);
10524 
10525  return 0;
10526 }
10527 
10528 static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid,
10529  AVStreamGroupTileGrid *tile_grid)
10530 {
10531  MOVContext *c = s->priv_data;
10532  const HEIFItem *item = grid->item;
10533  int64_t coded_width = 0, coded_height = 0;
10534  int64_t offset = 0, pos = avio_tell(s->pb);
10535  int64_t x = 0, y = 0;
10536  int i = 0;
10537  int tile_rows, tile_cols;
10538  int flags, size;
10539 
10540  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10541  av_log(c->fc, AV_LOG_INFO, "grid box with non seekable input\n");
10542  return AVERROR_PATCHWELCOME;
10543  }
10544  if (item->is_idat_relative) {
10545  if (!c->idat_offset) {
10546  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image grid\n");
10547  return AVERROR_INVALIDDATA;
10548  }
10549  offset = c->idat_offset;
10550  }
10551 
10552  if (offset > INT64_MAX - item->extent_offset)
10553  return AVERROR_INVALIDDATA;
10554 
10555  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10556 
10557  avio_r8(s->pb); /* version */
10558  flags = avio_r8(s->pb);
10559 
10560  tile_rows = avio_r8(s->pb) + 1;
10561  tile_cols = avio_r8(s->pb) + 1;
10562  /* actual width and height of output image */
10563  tile_grid->width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10564  tile_grid->height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10565 
10566  av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width %d output_height %d\n",
10567  tile_rows, tile_cols, tile_grid->width, tile_grid->height);
10568 
10569  avio_seek(s->pb, pos, SEEK_SET);
10570 
10571  size = tile_rows * tile_cols;
10572  tile_grid->nb_tiles = grid->nb_tiles;
10573 
10574  if (tile_grid->nb_tiles != size)
10575  return AVERROR_INVALIDDATA;
10576 
10577  for (int i = 0; i < tile_cols; i++)
10578  coded_width += grid->tile_item_list[i]->width;
10579  for (int i = 0; i < size; i += tile_cols)
10580  coded_height += grid->tile_item_list[i]->height;
10581 
10582  if (coded_width > INT_MAX || coded_height > INT_MAX)
10583  return AVERROR_INVALIDDATA;
10584 
10585  tile_grid->coded_width = coded_width;
10586  tile_grid->coded_height = coded_height;
10587 
10588  tile_grid->offsets = av_calloc(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10589  if (!tile_grid->offsets)
10590  return AVERROR(ENOMEM);
10591 
10592  while (y < tile_grid->coded_height) {
10593  int left_col = i;
10594 
10595  while (x < tile_grid->coded_width) {
10596  if (i == tile_grid->nb_tiles)
10597  return AVERROR_INVALIDDATA;
10598 
10599  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10600  tile_grid->offsets[i].horizontal = x;
10601  tile_grid->offsets[i].vertical = y;
10602 
10603  x += grid->tile_item_list[i++]->width;
10604  }
10605 
10606  if (x > tile_grid->coded_width) {
10607  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10608  return AVERROR_INVALIDDATA;
10609  }
10610 
10611  x = 0;
10612  y += grid->tile_item_list[left_col]->height;
10613  }
10614 
10615  if (y > tile_grid->coded_height || i != tile_grid->nb_tiles) {
10616  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10617  return AVERROR_INVALIDDATA;
10618  }
10619 
10620  return 0;
10621 }
10622 
10623 static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid,
10624  AVStreamGroupTileGrid *tile_grid)
10625 {
10626  MOVContext *c = s->priv_data;
10627  const HEIFItem *item = grid->item;
10628  uint16_t canvas_fill_value[4];
10629  int64_t offset = 0, pos = avio_tell(s->pb);
10630  int ret = 0, flags;
10631 
10632  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10633  av_log(c->fc, AV_LOG_INFO, "iovl box with non seekable input\n");
10634  return AVERROR_PATCHWELCOME;
10635  }
10636  if (item->is_idat_relative) {
10637  if (!c->idat_offset) {
10638  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image overlay\n");
10639  return AVERROR_INVALIDDATA;
10640  }
10641  offset = c->idat_offset;
10642  }
10643 
10644  if (offset > INT64_MAX - item->extent_offset)
10645  return AVERROR_INVALIDDATA;
10646 
10647  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10648 
10649  avio_r8(s->pb); /* version */
10650  flags = avio_r8(s->pb);
10651 
10652  for (int i = 0; i < 4; i++)
10653  canvas_fill_value[i] = avio_rb16(s->pb);
10654  av_log(c->fc, AV_LOG_TRACE, "iovl: canvas_fill_value { %u, %u, %u, %u }\n",
10655  canvas_fill_value[0], canvas_fill_value[1],
10656  canvas_fill_value[2], canvas_fill_value[3]);
10657  for (int i = 0; i < 4; i++)
10658  tile_grid->background[i] = canvas_fill_value[i];
10659 
10660  /* actual width and height of output image */
10661  tile_grid->width =
10662  tile_grid->coded_width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10663  tile_grid->height =
10664  tile_grid->coded_height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10665 
10666  av_log(c->fc, AV_LOG_TRACE, "iovl: output_width %d, output_height %d\n",
10667  tile_grid->width, tile_grid->height);
10668 
10669  tile_grid->nb_tiles = grid->nb_tiles;
10670  tile_grid->offsets = av_malloc_array(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10671  if (!tile_grid->offsets) {
10672  ret = AVERROR(ENOMEM);
10673  goto fail;
10674  }
10675 
10676  for (int i = 0; i < tile_grid->nb_tiles; i++) {
10677  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10678  tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10679  tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10680  av_log(c->fc, AV_LOG_TRACE, "iovl: stream_idx[%d] %u, "
10681  "horizontal_offset[%d] %d, vertical_offset[%d] %d\n",
10682  i, tile_grid->offsets[i].idx,
10683  i, tile_grid->offsets[i].horizontal, i, tile_grid->offsets[i].vertical);
10684  }
10685 
10686 fail:
10687  avio_seek(s->pb, pos, SEEK_SET);
10688 
10689  return ret;
10690 }
10691 
10693  AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10694  const HEIFItem *ref)
10695 {
10696  MOVContext *c = s->priv_data;
10697  AVPacketSideData *sd;
10698  AVExifMetadata ifd = { 0 };
10699  AVBufferRef *buf;
10700  int64_t offset = 0, pos = avio_tell(s->pb);
10701  unsigned orientation_id = av_exif_get_tag_id("Orientation");
10702  int err;
10703 
10704  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10705  av_log(c->fc, AV_LOG_WARNING, "Exif metadata with non seekable input\n");
10706  return AVERROR_PATCHWELCOME;
10707  }
10708  if (ref->is_idat_relative) {
10709  if (!c->idat_offset) {
10710  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the Exif metadata\n");
10711  return AVERROR_INVALIDDATA;
10712  }
10713  offset = c->idat_offset;
10714  }
10715 
10716  buf = av_buffer_alloc(ref->extent_length);
10717  if (!buf)
10718  return AVERROR(ENOMEM);
10719 
10720  if (offset > INT64_MAX - ref->extent_offset) {
10721  err = AVERROR(ENOMEM);
10722  goto fail;
10723  }
10724 
10725  avio_seek(s->pb, ref->extent_offset + offset, SEEK_SET);
10726  err = avio_read(s->pb, buf->data, ref->extent_length);
10727  if (err != ref->extent_length) {
10728  if (err > 0)
10729  err = AVERROR_INVALIDDATA;
10730  goto fail;
10731  }
10732 
10733  // HEIF spec states that Exif metadata is informative. The irot item property is
10734  // the normative source of rotation information. So we remove any Orientation tag
10735  // present in the Exif buffer.
10736  err = av_exif_parse_buffer(s, buf->data, ref->extent_length, &ifd, AV_EXIF_T_OFF);
10737  if (err < 0) {
10738  av_log(s, AV_LOG_ERROR, "Unable to parse Exif metadata\n");
10739  goto fail;
10740  }
10741 
10742  err = av_exif_remove_entry(s, &ifd, orientation_id, 0);
10743  if (err < 0)
10744  goto fail;
10745  else if (!err)
10746  goto finish;
10747 
10748  av_buffer_unref(&buf);
10749  err = av_exif_write(s, &ifd, &buf, AV_EXIF_T_OFF);
10750  if (err < 0)
10751  goto fail;
10752 
10753 finish:
10754  offset = AV_RB32(buf->data) + 4;
10755  if (offset >= buf->size) {
10756  err = AVERROR_INVALIDDATA;
10757  goto fail;
10758  }
10759  sd = av_packet_side_data_new(coded_side_data, nb_coded_side_data,
10760  AV_PKT_DATA_EXIF, buf->size - offset, 0);
10761  if (!sd) {
10762  err = AVERROR(ENOMEM);
10763  goto fail;
10764  }
10765  memcpy(sd->data, buf->data + offset, buf->size - offset);
10766 
10767  err = 0;
10768 fail:
10769  av_buffer_unref(&buf);
10770  av_exif_free(&ifd);
10771  avio_seek(s->pb, pos, SEEK_SET);
10772 
10773  return err;
10774 }
10775 
10777 {
10778  MOVContext *mov = s->priv_data;
10779 
10780  for (int i = 0; i < mov->nb_heif_grid; i++) {
10782  AVStreamGroupTileGrid *tile_grid;
10783  const HEIFGrid *grid = &mov->heif_grid[i];
10784  int err, loop = 1;
10785 
10786  if (!stg)
10787  return AVERROR(ENOMEM);
10788 
10789  stg->id = grid->item->item_id;
10790  tile_grid = stg->params.tile_grid;
10791 
10792  for (int j = 0; j < grid->nb_tiles; j++) {
10793  int tile_id = grid->tile_id_list[j];
10794  int k;
10795 
10796  for (k = 0; k < mov->nb_heif_item; k++) {
10797  HEIFItem *item = mov->heif_item[k];
10798  AVStream *st;
10799 
10800  if (!item || item->item_id != tile_id)
10801  continue;
10802  st = item->st;
10803  if (!st) {
10804  av_log(s, AV_LOG_WARNING, "HEIF item id %d from grid id %d doesn't "
10805  "reference a stream\n",
10806  tile_id, grid->item->item_id);
10807  ff_remove_stream_group(s, stg);
10808  loop = 0;
10809  break;
10810  }
10811 
10812  grid->tile_item_list[j] = item;
10813  grid->tile_idx_list[j] = stg->nb_streams;
10814 
10815  err = avformat_stream_group_add_stream(stg, st);
10816  if (err < 0) {
10817  int l;
10818  if (err != AVERROR(EEXIST))
10819  return err;
10820 
10821  for (l = 0; l < stg->nb_streams; l++)
10822  if (stg->streams[l]->index == st->index)
10823  break;
10824  av_assert0(l < stg->nb_streams);
10825  grid->tile_idx_list[j] = l;
10826  }
10827 
10828  if (item->item_id != mov->primary_item_id)
10830  break;
10831  }
10832 
10833  if (k == mov->nb_heif_item) {
10834  av_assert0(loop);
10835  av_log(s, AV_LOG_WARNING, "HEIF item id %d referenced by grid id %d doesn't "
10836  "exist\n",
10837  tile_id, grid->item->item_id);
10838  ff_remove_stream_group(s, stg);
10839  loop = 0;
10840  }
10841  if (!loop)
10842  break;
10843  }
10844 
10845  if (!loop)
10846  continue;
10847 
10848  switch (grid->item->type) {
10849  case MKTAG('g','r','i','d'):
10850  err = read_image_grid(s, grid, tile_grid);
10851  break;
10852  case MKTAG('i','o','v','l'):
10853  err = read_image_iovl(s, grid, tile_grid);
10854  break;
10855  default:
10856  av_assert0(0);
10857  }
10858  if (err < 0)
10859  return err;
10860 
10861  for (int j = 0; j < grid->item->nb_iref_list; j++) {
10862  HEIFItem *ref = get_heif_item(mov, grid->item->iref_list[j].item_id);
10863 
10864  av_assert0(ref);
10865  switch(ref->type) {
10866  case MKTAG('E','x','i','f'):
10867  err = mov_parse_exif_item(s, &tile_grid->coded_side_data,
10868  &tile_grid->nb_coded_side_data, ref);
10869  if (err < 0 && (s->error_recognition & AV_EF_EXPLODE))
10870  return err;
10871  break;
10872  default:
10873  break;
10874  }
10875  }
10876 
10877  /* rotation */
10878  if (grid->item->rotation || grid->item->hflip || grid->item->vflip) {
10880  &tile_grid->nb_coded_side_data, grid->item);
10881  if (err < 0)
10882  return err;
10883  }
10884 
10885  /* ICC profile */
10886  if (grid->item->icc_profile_size) {
10887  err = set_icc_profile_from_item(&tile_grid->coded_side_data,
10888  &tile_grid->nb_coded_side_data, grid->item);
10889  if (err < 0)
10890  return err;
10891  }
10892 
10893  if (grid->item->name)
10894  av_dict_set(&stg->metadata, "title", grid->item->name, 0);
10895  if (grid->item->item_id == mov->primary_item_id)
10897  }
10898 
10899  return 0;
10900 }
10901 
10903 {
10904  MOVContext *mov = s->priv_data;
10905  int err;
10906 
10907  for (int i = 0; i < mov->nb_heif_item; i++) {
10908  HEIFItem *item = mov->heif_item[i];
10909  MOVStreamContext *sc;
10910  AVStream *st;
10911  int64_t offset = 0;
10912 
10913  if (!item)
10914  continue;
10915  if (!item->st) {
10916  continue;
10917  }
10918  if (item->is_idat_relative) {
10919  if (!mov->idat_offset) {
10920  av_log(s, AV_LOG_ERROR, "Missing idat box for item %d\n", item->item_id);
10921  return AVERROR_INVALIDDATA;
10922  }
10923  offset = mov->idat_offset;
10924  }
10925 
10926  st = item->st;
10927  sc = st->priv_data;
10928  st->codecpar->width = item->width;
10929  st->codecpar->height = item->height;
10930 
10931  sc->sample_size = sc->stsz_sample_size = item->extent_length;
10932  sc->sample_count = 1;
10933 
10934  err = sanity_checks(s, sc, st->index);
10935  if (err)
10936  return AVERROR_INVALIDDATA;
10937 
10938  if (offset > INT64_MAX - item->extent_offset)
10939  return AVERROR_INVALIDDATA;
10940 
10941  sc->chunk_offsets[0] = item->extent_offset + offset;
10942 
10943  if (item->item_id == mov->primary_item_id)
10945 
10946  for (int j = 0; j < item->nb_iref_list; j++) {
10947  HEIFItem *ref = get_heif_item(mov, item->iref_list[j].item_id);
10948 
10949  av_assert0(ref);
10950  switch(ref->type) {
10951  case MKTAG('E','x','i','f'):
10954  if (err < 0 && (s->error_recognition & AV_EF_EXPLODE))
10955  return err;
10956  break;
10957  default:
10958  break;
10959  }
10960  }
10961 
10962  if (item->rotation || item->hflip || item->vflip) {
10964  &st->codecpar->nb_coded_side_data, item);
10965  if (err < 0)
10966  return err;
10967  }
10968 
10969  mov_build_index(mov, st);
10970  }
10971 
10972  if (mov->nb_heif_grid) {
10973  err = mov_parse_tiles(s);
10974  if (err < 0)
10975  return err;
10976  }
10977 
10978  return 0;
10979 }
10980 
10982 {
10983  int err;
10984 
10985  for (int i = 0; i < s->nb_streams; i++) {
10986  AVStreamGroup *stg;
10987  const AVDictionaryEntry *tcr;
10988  AVStream *st = s->streams[i];
10989 
10990  if (st->codecpar->codec_tag != MKTAG('t','m','c','d'))
10991  continue;
10992 
10994  if (!stg)
10995  return AVERROR(ENOMEM);
10996 
10997  stg->id = st->id;
10998  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
10999 
11000  for (int j = 0; j < s->nb_streams; j++) {
11001  AVStream *st2 = s->streams[j];
11002  MOVStreamContext *sc2 = st2->priv_data;
11003  MovTref *tag = mov_find_tref_tag(sc2, MKTAG('t','m','c','d'));
11004 
11005  if (!tag)
11006  continue;
11007 
11008  for (int k = 0; k < tag->nb_id; k++) {
11009  if (tag->id[k] != st->id)
11010  continue;
11011 
11012  err = avformat_stream_group_add_stream(stg, st2);
11013  if (err < 0)
11014  return err;
11015 
11016  if (tcr)
11017  av_dict_set(&st2->metadata, "timecode", tcr->value, AV_DICT_DONT_OVERWRITE);
11018  }
11019  }
11020 
11021  if (!stg->nb_streams) {
11022  ff_remove_stream_group(s, stg);
11023  continue;
11024  }
11025 
11026  err = avformat_stream_group_add_stream(stg, st);
11027  if (err < 0)
11028  return err;
11029 
11030  stg->params.tref->metadata_index = stg->nb_streams - 1;
11031  }
11033 
11034  return 0;
11035 }
11036 
11038  uint32_t *tref_id, int nb_tref_id, int first_index)
11039 {
11040  if (!nb_tref_id)
11041  return NULL;
11042 
11043  for (int i = first_index; i < s->nb_streams; i++)
11044  for (int j = 0; j < nb_tref_id; j++)
11045  if (s->streams[i]->index != st->index &&
11046  s->streams[i]->id == tref_id[j])
11047  return s->streams[i];
11048 
11049  return NULL;
11050 }
11051 
11053 {
11054  int err;
11055 
11056  // Don't try to add a group if there's only one track
11057  if (s->nb_streams <= 1)
11058  return 0;
11059 
11060  for (int i = 0; i < s->nb_streams; i++) {
11061  AVStreamGroup *stg;
11062  AVStream *st = s->streams[i];
11063  AVStream *st_base;
11064  MOVStreamContext *sc = st->priv_data;
11065  MovTref *tag = mov_find_tref_tag(sc, MKTAG('s','b','a','s'));
11066  int j = 0;
11067 
11068  /* Find an enhancement stream. */
11069  if (st->codecpar->codec_id != AV_CODEC_ID_LCEVC || !tag)
11070  continue;
11071 
11073  if (!stg)
11074  return AVERROR(ENOMEM);
11075 
11076  stg->id = st->id;
11077  stg->params.layered_video->width = st->codecpar->width;
11078  stg->params.layered_video->height = st->codecpar->height;
11079 
11080  while (st_base = mov_find_reference_track(s, st, tag->id, tag->nb_id, j)) {
11081  err = avformat_stream_group_add_stream(stg, st_base);
11082  if (err < 0)
11083  return err;
11084 
11085  j = st_base->index + 1;
11086  }
11087  if (!j) {
11088  int loglevel = (s->error_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
11089  av_log(s, loglevel, "Failed to find base stream for LCEVC stream\n");
11090  ff_remove_stream_group(s, stg);
11091  if (s->error_recognition & AV_EF_EXPLODE)
11092  return AVERROR_INVALIDDATA;
11093  continue;
11094  }
11095 
11096  err = avformat_stream_group_add_stream(stg, st);
11097  if (err < 0)
11098  return err;
11099 
11100  stg->params.layered_video->el_index = stg->nb_streams - 1;
11101  }
11102 
11103  return 0;
11104 }
11105 
11107 {
11108  int err;
11109 
11110  if (s->nb_streams <= 1)
11111  return 0;
11112 
11113  /* Identify legacy dual-track Dolby Vision profile 7 carriage per Annex
11114  * C of "Dolby Vision Streams Within the ISO Base Media File Format",
11115  * v2.7.1. The 'vdep' track reference type is shared with depth video
11116  * and multi-view dependencies. The spec-mandated DV-specific sample
11117  * entry combined with the dvcC configuration record disambiguates
11118  * those uses. */
11119  for (int i = 0; i < s->nb_streams; i++) {
11120  AVStreamGroup *stg;
11121  AVStream *st = s->streams[i];
11122  AVStream *st_base;
11123  MOVStreamContext *sc = st->priv_data;
11124  MovTref *tag = mov_find_tref_tag(sc, MKTAG('v','d','e','p'));
11125  const AVPacketSideData *sd;
11127 
11128  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !tag)
11129  continue;
11130 
11131  if (st->codecpar->codec_tag != MKTAG('d','v','h','e') &&
11132  st->codecpar->codec_tag != MKTAG('d','v','h','1'))
11133  continue;
11134 
11138  if (!sd)
11139  continue;
11140  dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
11141 
11142  /* EL track of a dual-track stream: rpu_present_flag = 1,
11143  * el_present_flag = 1, bl_present_flag = 0, dv_profile = 7. */
11144  if (dovi->dv_profile != 7 || !dovi->rpu_present_flag ||
11145  !dovi->el_present_flag || dovi->bl_present_flag)
11146  continue;
11147 
11148  st_base = mov_find_reference_track(s, st, tag->id, tag->nb_id, 0);
11149  if (!st_base) {
11150  int loglevel = (s->error_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
11151  av_log(s, loglevel, "Failed to find base layer for Dolby Vision EL stream\n");
11152  if (s->error_recognition & AV_EF_EXPLODE)
11153  return AVERROR_INVALIDDATA;
11154  continue;
11155  }
11156 
11158  if (!stg)
11159  return AVERROR(ENOMEM);
11160 
11161  stg->id = st->id;
11162 
11163  err = avformat_stream_group_add_stream(stg, st_base);
11164  if (err < 0)
11165  return err;
11166 
11167  err = avformat_stream_group_add_stream(stg, st);
11168  if (err < 0)
11169  return err;
11170 
11171  stg->params.layered_video->el_index = stg->nb_streams - 1;
11172  stg->params.layered_video->width = st_base->codecpar->width;
11173  stg->params.layered_video->height = st_base->codecpar->height;
11174  }
11175 
11176  return 0;
11177 }
11178 
11180 {
11181  int highest_id = 0, lowest_iamf_id = INT_MAX;
11182 
11183  for (int i = 0; i < s->nb_streams; i++) {
11184  const AVStream *st = s->streams[i];
11185  const MOVStreamContext *sc = st->priv_data;
11186  if (!sc->iamf)
11187  highest_id = FFMAX(highest_id, st->id);
11188  }
11189 
11190  for (int i = 0; i < s->nb_stream_groups; i++) {
11191  AVStreamGroup *stg = s->stream_groups[i];
11193  continue;
11194  for (int j = 0; j < stg->nb_streams; j++) {
11195  AVStream *st = stg->streams[j];
11196  lowest_iamf_id = FFMIN(lowest_iamf_id, st->id);
11197  }
11198  }
11199 
11200  if (highest_id < lowest_iamf_id)
11201  return;
11202 
11203  highest_id += !lowest_iamf_id;
11204  for (int i = 0; highest_id > 1 && i < s->nb_stream_groups; i++) {
11205  AVStreamGroup *stg = s->stream_groups[i];
11207  continue;
11208  for (int j = 0; j < stg->nb_streams; j++) {
11209  AVStream *st = stg->streams[j];
11210  MOVStreamContext *sc = st->priv_data;
11211  st->id += highest_id;
11212  sc->iamf_stream_offset = highest_id;
11213  }
11214  }
11215 }
11216 
11218 {
11219  MOVContext *mov = s->priv_data;
11220  AVIOContext *pb = s->pb;
11221  int j, err;
11222  MOVAtom atom = { AV_RL32("root") };
11223  int i;
11224 
11225  mov->fc = s;
11226  mov->trak_index = -1;
11227  mov->primary_item_id = -1;
11228  mov->cur_item_id = -1;
11229  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
11230  if (pb->seekable & AVIO_SEEKABLE_NORMAL)
11231  atom.size = avio_size(pb);
11232  else
11233  atom.size = INT64_MAX;
11234 
11235  /* check MOV header */
11236  do {
11237  if (mov->moov_retry)
11238  avio_seek(pb, 0, SEEK_SET);
11239  if ((err = mov_read_default(mov, pb, atom)) < 0) {
11240  av_log(s, AV_LOG_ERROR, "error reading header\n");
11241  return err;
11242  }
11243  } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
11244  !mov->found_moov && (!mov->found_iloc || !mov->found_iinf) && !mov->moov_retry++);
11245  if (!mov->found_moov && !mov->found_iloc && !mov->found_iinf) {
11246  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
11247  return AVERROR_INVALIDDATA;
11248  }
11249  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
11250 
11251  if (mov->found_iloc && mov->found_iinf) {
11252  err = mov_parse_heif_items(s);
11253  if (err < 0)
11254  return err;
11255  }
11256  // prevent iloc and iinf boxes from being parsed while reading packets.
11257  // this is needed because an iinf box may have been parsed but ignored
11258  // for having old infe boxes which create no streams.
11259  mov->found_iloc = mov->found_iinf = 1;
11260 
11261  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
11262  if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
11264  for (i = 0; i < s->nb_streams; i++)
11265  if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
11266  mov_read_timecode_track(s, s->streams[i]);
11267  } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
11268  mov_read_rtmd_track(s, s->streams[i]);
11269  }
11270  }
11271 
11272  /* copy timecode metadata from tmcd tracks to the related video streams */
11273  err = mov_parse_tmcd_streams(s);
11274  if (err < 0)
11275  return err;
11276 
11277  /* Create LCEVC stream groups. */
11278  err = mov_parse_lcevc_streams(s);
11279  if (err < 0)
11280  return err;
11281 
11282  /* Create Dolby Vision Profile 7 stream groups. */
11283  err = mov_parse_dovi_streams(s);
11284  if (err < 0)
11285  return err;
11286 
11287  for (i = 0; i < s->nb_streams; i++) {
11288  AVStream *st = s->streams[i];
11289  FFStream *const sti = ffstream(st);
11290  MOVStreamContext *sc = st->priv_data;
11291  uint32_t dvdsub_clut[FF_DVDCLUT_CLUT_LEN] = {0};
11292  fix_timescale(mov, sc);
11293 
11294  /* Set the primary extradata based on the first Sample if it doesn't reference the first stsd entry. */
11295  if (sc->stsc_count && sc->extradata_size && !sc->iamf &&
11296  sc->stsc_data[0].id > 1 && sc->stsc_data[0].id <= sc->stsd_count) {
11297  sc->last_stsd_index = sc->stsc_data[0].id - 1;
11298  av_freep(&st->codecpar->extradata);
11300  if (sc->extradata_size[sc->last_stsd_index]) {
11302  if (!st->codecpar->extradata)
11303  return AVERROR(ENOMEM);
11304  memcpy(st->codecpar->extradata, sc->extradata[sc->last_stsd_index], sc->extradata_size[sc->last_stsd_index]);
11305  }
11306  }
11307 
11308  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
11309  st->codecpar->codec_id == AV_CODEC_ID_AAC) {
11310  sti->skip_samples = sc->start_pad;
11311  }
11312  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
11314  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
11316  if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
11317  st->codecpar->width = sc->width;
11318  st->codecpar->height = sc->height;
11319  }
11322 
11323  for (j = 0; j < FF_DVDCLUT_CLUT_LEN; j++)
11324  dvdsub_clut[j] = AV_RB32(st->codecpar->extradata + j * 4);
11325 
11326  err = ff_dvdclut_yuv_to_rgb(dvdsub_clut, FF_DVDCLUT_CLUT_SIZE);
11327  if (err < 0)
11328  return err;
11329 
11330  av_freep(&st->codecpar->extradata);
11331  st->codecpar->extradata_size = 0;
11332 
11334  st->codecpar);
11335  if (err < 0)
11336  return err;
11337  }
11338  }
11339  if (mov->handbrake_version &&
11340  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
11341  st->codecpar->codec_id == AV_CODEC_ID_MP3) {
11342  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
11344  }
11345  }
11346 
11347  if (mov->trex_data || mov->use_mfra_for > 0) {
11348  for (i = 0; i < s->nb_streams; i++) {
11349  AVStream *st = s->streams[i];
11350  MOVStreamContext *sc = st->priv_data;
11351  if (sc->duration_for_fps > 0) {
11352  /* Akin to sc->data_size * 8 * sc->time_scale / sc->duration_for_fps but accounting for overflows. */
11354  if (st->codecpar->bit_rate == INT64_MIN) {
11355  av_log(s, AV_LOG_WARNING, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
11356  sc->data_size, sc->time_scale);
11357  st->codecpar->bit_rate = 0;
11358  if (s->error_recognition & AV_EF_EXPLODE)
11359  return AVERROR_INVALIDDATA;
11360  }
11361  }
11362  }
11363  }
11364 
11365  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
11366  if (mov->bitrates[i]) {
11367  s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
11368  }
11369  }
11370 
11372 
11373  for (i = 0; i < s->nb_streams; i++) {
11374  AVStream *st = s->streams[i];
11375  MOVStreamContext *sc = st->priv_data;
11376 
11377  switch (st->codecpar->codec_type) {
11378  case AVMEDIA_TYPE_AUDIO:
11379  err = ff_replaygain_export(st, s->metadata);
11380  if (err < 0)
11381  return err;
11382  break;
11383  case AVMEDIA_TYPE_VIDEO:
11384  if (sc->display_matrix) {
11387  (uint8_t*)sc->display_matrix, sizeof(int32_t) * 9, 0))
11388  return AVERROR(ENOMEM);
11389 
11390  sc->display_matrix = NULL;
11391  }
11392  if (sc->stereo3d) {
11395  (uint8_t *)sc->stereo3d, sc->stereo3d_size, 0))
11396  return AVERROR(ENOMEM);
11397 
11398  sc->stereo3d = NULL;
11399  }
11400  if (sc->spherical) {
11403  (uint8_t *)sc->spherical, sc->spherical_size, 0))
11404  return AVERROR(ENOMEM);
11405 
11406  sc->spherical = NULL;
11407  }
11408  if (sc->mastering) {
11411  (uint8_t *)sc->mastering, sc->mastering_size, 0))
11412  return AVERROR(ENOMEM);
11413 
11414  sc->mastering = NULL;
11415  }
11416  if (sc->coll) {
11419  (uint8_t *)sc->coll, sc->coll_size, 0))
11420  return AVERROR(ENOMEM);
11421 
11422  sc->coll = NULL;
11423  }
11424  if (sc->ambient) {
11427  (uint8_t *) sc->ambient, sc->ambient_size, 0))
11428  return AVERROR(ENOMEM);
11429 
11430  sc->ambient = NULL;
11431  }
11432  break;
11433  }
11434  }
11435 
11436  fix_stream_ids(s);
11437 
11439 
11440  for (i = 0; i < mov->frag_index.nb_items; i++)
11441  if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
11442  mov->frag_index.item[i].headers_read = 1;
11443 
11444  return 0;
11445 }
11446 
11448 {
11450  int64_t best_dts = INT64_MAX;
11451  int i;
11452  MOVContext *mov = s->priv_data;
11453  int no_interleave = !mov->interleaved_read || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL);
11454  for (i = 0; i < s->nb_streams; i++) {
11455  AVStream *avst = s->streams[i];
11456  FFStream *const avsti = ffstream(avst);
11457  MOVStreamContext *msc = avst->priv_data;
11458  if (msc->pb && msc->current_sample < avsti->nb_index_entries) {
11459  AVIndexEntry *current_sample = &avsti->index_entries[msc->current_sample];
11460  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
11461  uint64_t dtsdiff = best_dts > dts ? best_dts - (uint64_t)dts : ((uint64_t)dts - best_dts);
11462  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
11463  if (!sample || (no_interleave && current_sample->pos < sample->pos) ||
11464  ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
11465  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
11466  ((dtsdiff <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
11467  (dtsdiff > AV_TIME_BASE && dts < best_dts && mov->interleaved_read)))))) {
11468  sample = current_sample;
11469  best_dts = dts;
11470  *st = avst;
11471  }
11472  }
11473  }
11474  return sample;
11475 }
11476 
11477 static int should_retry(AVIOContext *pb, int error_code) {
11478  if (error_code == AVERROR_EOF || avio_feof(pb))
11479  return 0;
11480 
11481  return 1;
11482 }
11483 
11484 static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
11485 {
11486  int ret;
11487  MOVContext *mov = s->priv_data;
11488 
11489  if (index >= 0 && index < mov->frag_index.nb_items)
11490  target = mov->frag_index.item[index].moof_offset;
11491  if (target >= 0 && avio_seek(s->pb, target, SEEK_SET) != target) {
11492  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
11493  return AVERROR_INVALIDDATA;
11494  }
11495 
11496  mov->next_root_atom = 0;
11497  if ((index < 0 && target >= 0) || index >= mov->frag_index.nb_items)
11498  index = search_frag_moof_offset(&mov->frag_index, target);
11499  if (index >= 0 && index < mov->frag_index.nb_items &&
11500  mov->frag_index.item[index].moof_offset == target) {
11501  if (index + 1 < mov->frag_index.nb_items)
11502  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
11503  if (mov->frag_index.item[index].headers_read)
11504  return 0;
11505  mov->frag_index.item[index].headers_read = 1;
11506  }
11507 
11508  mov->found_mdat = 0;
11509 
11510  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
11511  if (ret < 0)
11512  return ret;
11513  if (avio_feof(s->pb))
11514  return AVERROR_EOF;
11515  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
11516 
11517  return 1;
11518 }
11519 
11521 {
11522  MOVStreamContext *sc = st->priv_data;
11523  uint8_t *side, *extradata;
11524  int extradata_size;
11525 
11526  /* Save the current index. */
11527  sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
11528 
11529  /* Notify the decoder that extradata changed. */
11530  extradata_size = sc->extradata_size[sc->last_stsd_index];
11531  extradata = sc->extradata[sc->last_stsd_index];
11532  if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
11535  extradata_size);
11536  if (!side)
11537  return AVERROR(ENOMEM);
11538  memcpy(side, extradata, extradata_size);
11539  }
11540 
11541  return 0;
11542 }
11543 
11544 static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int src_size)
11545 {
11546  /* We can't make assumptions about the structure of the payload,
11547  because it may include multiple cdat and cdt2 samples. */
11548  const uint32_t cdat = AV_RB32("cdat");
11549  const uint32_t cdt2 = AV_RB32("cdt2");
11550  int ret, out_size = 0;
11551 
11552  /* a valid payload must have size, 4cc, and at least 1 byte pair: */
11553  if (src_size < 10)
11554  return AVERROR_INVALIDDATA;
11555 
11556  /* avoid an int overflow: */
11557  if ((src_size - 8) / 2 >= INT_MAX / 3)
11558  return AVERROR_INVALIDDATA;
11559 
11560  ret = av_new_packet(pkt, ((src_size - 8) / 2) * 3);
11561  if (ret < 0)
11562  return ret;
11563 
11564  /* parse and re-format the c608 payload in one pass. */
11565  while (src_size >= 10) {
11566  const uint32_t atom_size = avio_rb32(pb);
11567  const uint32_t atom_type = avio_rb32(pb);
11568  const uint32_t data_size = atom_size - 8;
11569  const uint8_t cc_field =
11570  atom_type == cdat ? 1 :
11571  atom_type == cdt2 ? 2 :
11572  0;
11573 
11574  /* account for bytes consumed for atom size and type. */
11575  src_size -= 8;
11576 
11577  /* make sure the data size stays within the buffer boundaries. */
11578  if (data_size < 2 || data_size > src_size) {
11580  break;
11581  }
11582 
11583  /* make sure the data size is consistent with N byte pairs. */
11584  if (data_size % 2 != 0) {
11586  break;
11587  }
11588 
11589  if (!cc_field) {
11590  /* neither cdat or cdt2 ... skip it */
11591  avio_skip(pb, data_size);
11592  src_size -= data_size;
11593  continue;
11594  }
11595 
11596  for (uint32_t i = 0; i < data_size; i += 2) {
11597  pkt->data[out_size] = (0x1F << 3) | (1 << 2) | (cc_field - 1);
11598  pkt->data[out_size + 1] = avio_r8(pb);
11599  pkt->data[out_size + 2] = avio_r8(pb);
11600  out_size += 3;
11601  src_size -= 2;
11602  }
11603  }
11604 
11605  if (src_size > 0)
11606  /* skip any remaining unread portion of the input payload */
11607  avio_skip(pb, src_size);
11608 
11610  return ret;
11611 }
11612 
11614  int64_t current_index, AVPacket *pkt)
11615 {
11616  MOVStreamContext *sc = st->priv_data;
11617 
11618  pkt->stream_index = sc->ffindex;
11619  pkt->dts = sample->timestamp;
11620  if (sample->flags & AVINDEX_DISCARD_FRAME) {
11622  }
11623  if (sc->stts_count && sc->tts_index < sc->tts_count)
11624  pkt->duration = sc->tts_data[sc->tts_index].duration;
11625  if (sc->ctts_count && sc->tts_index < sc->tts_count) {
11627  } else {
11628  if (pkt->duration == 0) {
11629  int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
11631  if (next_dts >= pkt->dts)
11632  pkt->duration = next_dts - pkt->dts;
11633  }
11634  pkt->pts = pkt->dts;
11635  }
11636 
11637  if (sc->tts_data && sc->tts_index < sc->tts_count) {
11638  /* update tts context */
11639  sc->tts_sample++;
11640  if (sc->tts_index < sc->tts_count &&
11641  sc->tts_data[sc->tts_index].count == sc->tts_sample) {
11642  sc->tts_index++;
11643  sc->tts_sample = 0;
11644  }
11645  }
11646 
11647  if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
11648  uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
11649  uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
11650  pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
11651  }
11652  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
11653  pkt->pos = sample->pos;
11654 
11655  /* Multiple stsd handling. */
11656  if (sc->stsc_data) {
11657  if (sc->stsc_data[sc->stsc_index].id > 0 &&
11658  sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
11659  sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
11660  int ret = mov_change_extradata(st, pkt);
11661  if (ret < 0)
11662  return ret;
11663  }
11664 
11665  /* Update the stsc index for the next sample */
11666  sc->stsc_sample++;
11667  if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
11668  mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
11669  sc->stsc_index++;
11670  sc->stsc_sample = 0;
11671  }
11672  }
11673 
11674  return 0;
11675 }
11676 
11678 {
11679  MOVContext *mov = s->priv_data;
11680  MOVStreamContext *sc;
11682  AVStream *st = NULL;
11683  FFStream *avsti = NULL;
11684  int64_t current_index;
11685  int ret;
11686  int i;
11687  mov->fc = s;
11688  retry:
11689  if (s->pb->pos == 0) {
11690 
11691  // Discard current fragment index
11692  if (mov->frag_index.allocated_size > 0) {
11693  for(int i = 0; i < mov->frag_index.nb_items; i++) {
11695  }
11696  av_freep(&mov->frag_index.item);
11697  mov->frag_index.nb_items = 0;
11698  mov->frag_index.allocated_size = 0;
11699  mov->frag_index.current = -1;
11700  mov->frag_index.complete = 0;
11701  }
11702 
11703  for (i = 0; i < s->nb_streams; i++) {
11704  AVStream *avst = s->streams[i];
11705  MOVStreamContext *msc = avst->priv_data;
11706 
11707  // Clear current sample
11708  mov_current_sample_set(msc, 0);
11709  msc->tts_index = 0;
11710 
11711  // Discard current index entries
11712  avsti = ffstream(avst);
11713  if (avsti->index_entries_allocated_size > 0) {
11714  av_freep(&avsti->index_entries);
11715  avsti->index_entries_allocated_size = 0;
11716  avsti->nb_index_entries = 0;
11717  }
11718  }
11719 
11720  if ((ret = mov_switch_root(s, -1, -1)) < 0)
11721  return ret;
11722  }
11723  sample = mov_find_next_sample(s, &st);
11724  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
11725  if (!mov->next_root_atom)
11726  return AVERROR_EOF;
11727  if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
11728  return ret;
11729  goto retry;
11730  }
11731  sc = st->priv_data;
11732  /* must be done just before reading, to avoid infinite loop on sample */
11733  current_index = sc->current_index;
11735 
11736  if (mov->next_root_atom) {
11737  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
11738  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
11739  }
11740 
11741  if (st->discard != AVDISCARD_ALL || sc->iamf) {
11742  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
11743  if (ret64 != sample->pos) {
11744  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
11745  sc->ffindex, sample->pos);
11746  if (should_retry(sc->pb, ret64)) {
11748  } else if (ret64 < 0) {
11749  return (int)ret64;
11750  }
11751  return AVERROR_INVALIDDATA;
11752  }
11753 
11754  if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
11755  av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
11756  goto retry;
11757  }
11758 
11759  if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
11760  ret = get_eia608_packet(sc->pb, pkt, sample->size);
11761 #if CONFIG_IAMFDEC
11762  else if (sc->iamf) {
11763  int64_t pts, dts, pos, duration;
11764  int flags, size = sample->size;
11765  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
11766  pts = pkt->pts; dts = pkt->dts;
11767  pos = pkt->pos; flags = pkt->flags;
11768  duration = pkt->duration;
11769  while (!ret && size > 0) {
11770  ret = ff_iamf_read_packet(s, sc->iamf, sc->pb, size, sc->iamf_stream_offset, pkt);
11771  if (ret < 0) {
11772  if (should_retry(sc->pb, ret))
11774  return ret;
11775  }
11776  size -= ret;
11777 
11778  if (pkt->flags & AV_PKT_FLAG_DISCARD) {
11780  ret = 0;
11781  continue;
11782  }
11783  pkt->pts = pts; pkt->dts = dts;
11784  pkt->pos = pos; pkt->flags |= flags;
11785  pkt->duration = duration;
11786  ret = ff_buffer_packet(s, pkt);
11787  }
11788  if (!ret)
11789  return FFERROR_REDO;
11790  }
11791 #endif
11792  else if (st->codecpar->codec_id == AV_CODEC_ID_APV && sample->size > 4) {
11793  uint32_t au_size = avio_rb32(sc->pb);
11794  int explode = !!(mov->fc->error_recognition & AV_EF_EXPLODE);
11795  if (au_size > sample->size - 4) {
11796  av_log(s, explode ? AV_LOG_ERROR : AV_LOG_WARNING,
11797  "APV au_size %u exceeds sample body %d\n",
11798  au_size, sample->size - 4);
11799  if (explode)
11800  return AVERROR_INVALIDDATA;
11801  au_size = sample->size - 4;
11802  }
11803  ret = av_get_packet(sc->pb, pkt, au_size);
11804  } else
11805  ret = av_get_packet(sc->pb, pkt, sample->size);
11806  if (ret < 0) {
11807  if (should_retry(sc->pb, ret)) {
11809  }
11810  return ret;
11811  }
11812 #if CONFIG_DV_DEMUXER
11813  if (mov->dv_demux && sc->dv_audio_container) {
11816  if (ret < 0)
11817  return ret;
11819  if (ret < 0)
11820  return ret;
11821  }
11822 #endif
11823  if (sc->has_palette) {
11824  uint8_t *pal;
11825 
11827  if (!pal) {
11828  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
11829  } else {
11830  memcpy(pal, sc->palette, AVPALETTE_SIZE);
11831  sc->has_palette = 0;
11832  }
11833  }
11834  if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !ffstream(st)->need_parsing && pkt->size > 4) {
11835  if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
11837  }
11838  }
11839 
11840  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
11841  if (ret < 0)
11842  return ret;
11843 
11844  if (st->discard == AVDISCARD_ALL)
11845  goto retry;
11846 
11847  if (mov->aax_mode)
11848  aax_filter(pkt->data, pkt->size, mov);
11849 
11850  ret = cenc_filter(mov, st, sc, pkt, current_index);
11851  if (ret < 0) {
11852  return ret;
11853  }
11854 
11855  return 0;
11856 }
11857 
11859 {
11860  MOVContext *mov = s->priv_data;
11861  int index;
11862 
11863  if (!mov->frag_index.complete)
11864  return 0;
11865 
11866  index = search_frag_timestamp(s, &mov->frag_index, st, timestamp);
11867  if (index < 0)
11868  index = 0;
11869  if (!mov->frag_index.item[index].headers_read)
11870  return mov_switch_root(s, -1, index);
11871  if (index + 1 < mov->frag_index.nb_items)
11872  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
11873 
11874  return 0;
11875 }
11876 
11877 static int is_open_key_sample(const MOVStreamContext *sc, int sample)
11878 {
11879  // TODO: a bisect search would scale much better
11880  for (int i = 0; i < sc->open_key_samples_count; i++) {
11881  const int oks = sc->open_key_samples[i];
11882  if (oks == sample)
11883  return 1;
11884  if (oks > sample) /* list is monotically increasing so we can stop early */
11885  break;
11886  }
11887  return 0;
11888 }
11889 
11890 /*
11891  * Some key sample may be key frames but not IDR frames, so a random access to
11892  * them may not be allowed.
11893  */
11894 static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
11895 {
11896  MOVStreamContext *sc = st->priv_data;
11897  FFStream *const sti = ffstream(st);
11898  int64_t key_sample_dts, key_sample_pts;
11899 
11900  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
11901  return 1;
11902 
11903  if (sample >= sc->sample_offsets_count)
11904  return 1;
11905 
11906  av_assert0(sample >= 0);
11907  key_sample_dts = sti->index_entries[sample].timestamp;
11908  key_sample_pts = key_sample_dts + sc->sample_offsets[sample] + sc->dts_shift;
11909 
11910  /*
11911  * If the sample needs to be presented before an open key sample, they may
11912  * not be decodable properly, even though they come after in decoding
11913  * order.
11914  */
11915  if (is_open_key_sample(sc, sample) && key_sample_pts > requested_pts)
11916  return 0;
11917 
11918  return 1;
11919 }
11920 
11921 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
11922 {
11923  MOVStreamContext *sc = st->priv_data;
11924  FFStream *const sti = ffstream(st);
11925  int sample, time_sample, ret, requested_sample;
11926  int64_t next_ts;
11927  unsigned int i;
11928 
11929  // Here we consider timestamp to be PTS, hence try to offset it so that we
11930  // can search over the DTS timeline.
11931  timestamp -= (sc->min_corrected_pts + sc->dts_shift);
11932 
11933  ret = mov_seek_fragment(s, st, timestamp);
11934  if (ret < 0)
11935  return ret;
11936 
11937  for (;;) {
11938  sample = av_index_search_timestamp(st, timestamp, flags);
11939  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
11940  if (sample < 0 && sti->nb_index_entries && timestamp < sti->index_entries[0].timestamp)
11941  sample = 0;
11942  if (sample < 0) /* not sure what to do */
11943  return AVERROR_INVALIDDATA;
11944 
11945  if (!sample || can_seek_to_key_sample(st, sample, timestamp))
11946  break;
11947 
11948  next_ts = timestamp - FFMAX(sc->min_sample_duration, 1);
11949  requested_sample = av_index_search_timestamp(st, next_ts, flags);
11950  if (requested_sample < 0)
11951  return AVERROR_INVALIDDATA;
11952 
11953  // If we've reached a different sample trying to find a good pts to
11954  // seek to, give up searching because we'll end up seeking back to
11955  // sample 0 on every seek.
11956  if (sample != requested_sample && !can_seek_to_key_sample(st, requested_sample, next_ts))
11957  break;
11958 
11959  timestamp = next_ts;
11960  }
11961 
11963  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
11964  /* adjust time to sample index */
11965  if (sc->tts_data) {
11966  time_sample = 0;
11967  for (i = 0; i < sc->tts_count; i++) {
11968  int next = time_sample + sc->tts_data[i].count;
11969  if (next > sc->current_sample) {
11970  sc->tts_index = i;
11971  sc->tts_sample = sc->current_sample - time_sample;
11972  break;
11973  }
11974  time_sample = next;
11975  }
11976  }
11977 
11978  /* adjust stsd index */
11979  if (sc->chunk_count) {
11980  time_sample = 0;
11981  for (i = 0; i < sc->stsc_count; i++) {
11982  int64_t next = time_sample + mov_get_stsc_samples(sc, i);
11983  if (next > sc->current_sample) {
11984  sc->stsc_index = i;
11985  sc->stsc_sample = sc->current_sample - time_sample;
11986  break;
11987  }
11988  av_assert0(next == (int)next);
11989  time_sample = next;
11990  }
11991  }
11992 
11993  return sample;
11994 }
11995 
11997 {
11998  MOVStreamContext *sc = st->priv_data;
11999  FFStream *const sti = ffstream(st);
12000  int64_t first_ts = sti->index_entries[0].timestamp;
12002  int64_t off;
12003 
12005  return 0;
12006 
12007  /* compute skip samples according to stream start_pad, seek ts and first ts */
12008  off = av_rescale_q(ts - first_ts, st->time_base,
12009  (AVRational){1, st->codecpar->sample_rate});
12010  return FFMAX(sc->start_pad - off, 0);
12011 }
12012 
12013 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
12014 {
12015  MOVContext *mc = s->priv_data;
12016  AVStream *st;
12017  FFStream *sti;
12018  int sample;
12019  int i;
12020 
12021  if (stream_index >= s->nb_streams)
12022  return AVERROR_INVALIDDATA;
12023 
12024  st = s->streams[stream_index];
12025  sti = ffstream(st);
12026  sample = mov_seek_stream(s, st, sample_time, flags);
12027  if (sample < 0)
12028  return sample;
12029 
12030  if (mc->seek_individually) {
12031  /* adjust seek timestamp to found sample timestamp */
12032  int64_t seek_timestamp = sti->index_entries[sample].timestamp;
12034 
12035  for (i = 0; i < s->nb_streams; i++) {
12036  AVStream *const st = s->streams[i];
12037  FFStream *const sti = ffstream(st);
12038  int64_t timestamp;
12039 
12040  if (stream_index == i)
12041  continue;
12042 
12043  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
12044  sample = mov_seek_stream(s, st, timestamp, flags);
12045  if (sample >= 0)
12047  }
12048  } else {
12049  for (i = 0; i < s->nb_streams; i++) {
12050  MOVStreamContext *sc;
12051  st = s->streams[i];
12052  sc = st->priv_data;
12053  mov_current_sample_set(sc, 0);
12054  }
12055  while (1) {
12056  MOVStreamContext *sc;
12058  if (!entry)
12059  return AVERROR_INVALIDDATA;
12060  sc = st->priv_data;
12061  if (sc->ffindex == stream_index && sc->current_sample == sample)
12062  break;
12064  }
12065  }
12066  return 0;
12067 }
12068 
12069 #define OFFSET(x) offsetof(MOVContext, x)
12070 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
12071 static const AVOption mov_options[] = {
12072  {"use_absolute_path",
12073  "allow using absolute path when opening alias, this is a possible security issue",
12074  OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
12075  0, 1, FLAGS},
12076  {"seek_streams_individually",
12077  "Seek each stream individually to the closest point",
12078  OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
12079  0, 1, FLAGS},
12080  {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
12081  0, 1, FLAGS},
12082  {"advanced_editlist",
12083  "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
12084  OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
12085  0, 1, FLAGS},
12086  {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
12087  0, 1, FLAGS},
12088  {"use_mfra_for",
12089  "use mfra for fragment timestamps",
12090  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
12092  .unit = "use_mfra_for"},
12093  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
12094  FLAGS, .unit = "use_mfra_for" },
12095  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
12096  FLAGS, .unit = "use_mfra_for" },
12097  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
12098  FLAGS, .unit = "use_mfra_for" },
12099  {"use_tfdt", "use tfdt for fragment timestamps", OFFSET(use_tfdt), AV_OPT_TYPE_BOOL, {.i64 = 1},
12100  0, 1, FLAGS},
12101  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
12102  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
12103  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
12104  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
12105  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
12107  { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
12109  { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
12111  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
12112  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
12113  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
12114  .flags = AV_OPT_FLAG_DECODING_PARAM },
12115  { "decryption_key", "The default media decryption key (hex)", OFFSET(decryption_default_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
12116  { "decryption_keys", "The media decryption keys by KID (hex)", OFFSET(decryption_keys), AV_OPT_TYPE_DICT, .flags = AV_OPT_FLAG_DECODING_PARAM },
12117  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
12118  {.i64 = 0}, 0, 1, FLAGS },
12119  { "max_stts_delta", "treat offsets above this value as invalid", OFFSET(max_stts_delta), AV_OPT_TYPE_INT, {.i64 = UINT_MAX-48000*10 }, 0, UINT_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
12120  { "interleaved_read", "Interleave packets from multiple tracks at demuxer level", OFFSET(interleaved_read), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, .flags = AV_OPT_FLAG_DECODING_PARAM },
12121 
12122  { NULL },
12123 };
12124 
12125 static const AVClass mov_class = {
12126  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
12127  .item_name = av_default_item_name,
12128  .option = mov_options,
12129  .version = LIBAVUTIL_VERSION_INT,
12130 };
12131 
12133  .p.name = "mov,mp4,m4a,3gp,3g2,mj2",
12134  .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
12135  .p.priv_class = &mov_class,
12136  .p.extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4v,m4b,ism,ismv,isma,f4v,avif,heic,heif",
12138  .priv_data_size = sizeof(MOVContext),
12139  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
12140  .read_probe = mov_probe,
12145 };
HEIFItemRef::item_id
int item_id
Definition: isom.h:296
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: demux_utils.c:43
MOVStreamContext::ctts_allocated_size
unsigned int ctts_allocated_size
Definition: isom.h:197
item_name
item_name
Definition: libkvazaar.c:311
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:339
flags
const SwsFlags flags[]
Definition: swscale.c:85
AV_EXIF_T_OFF
@ AV_EXIF_T_OFF
The first four bytes point to the actual start, then it's AV_EXIF_TIFF_HEADER.
Definition: exif.h:68
mov_finalize_stsd_entry
static int mov_finalize_stsd_entry(MOVContext *c, AVStream *st)
Definition: mov.c:3194
mov_read_chpl
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:614
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:7568
mov_read_meta
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5660
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:584
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:105
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:434
MOVContext::found_iloc
int found_iloc
'iloc' atom has been found
Definition: isom.h:332
AV_CODEC_ID_MACE6
@ AV_CODEC_ID_MACE6
Definition: codec_id.h:471
MOVFragmentStreamInfo::first_tfra_pts
int64_t first_tfra_pts
Definition: isom.h:148
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
add frame for rfps calculation.
Definition: demux.c:2336
read_image_iovl
static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid, AVStreamGroupTileGrid *tile_grid)
Definition: mov.c:10623
AV_PRIMARY_EYE_RIGHT
@ AV_PRIMARY_EYE_RIGHT
Right eye.
Definition: stereo3d.h:188
mov_read_irot
static int mov_read_irot(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9539
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:203
AVIAMFSubmix::elements
AVIAMFSubmixElement ** elements
Array of submix elements.
Definition: iamf.h:568
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
MOV_TFHD_DEFAULT_FLAGS
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:413
PUT_UTF8
#define PUT_UTF8(val, tmp, PUT_BYTE)
Definition: common.h:541
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:231
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:359
FFStream::skip_samples
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
Definition: internal.h:208
MOVStreamContext::audio_cid
int16_t audio_cid
stsd audio compression id
Definition: isom.h:227
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:47
AV_CODEC_ID_ADPCM_MS
@ AV_CODEC_ID_ADPCM_MS
Definition: codec_id.h:384
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:71
mov_read_dops
static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8696
can_seek_to_key_sample
static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
Definition: mov.c:11894
name
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 minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:378
entry
#define entry
Definition: aom_film_grain_template.c:66
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:487
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
AVStreamGroup::id
int64_t id
Group type-specific group ID.
Definition: avformat.h:1159
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:464
HEIFItem::name
char * name
Definition: isom.h:303
AV_STEREO3D_VIEW_LEFT
@ AV_STEREO3D_VIEW_LEFT
Frame contains only the left view.
Definition: stereo3d.h:158
MOVStreamContext::sync_group
MOVSbgp * sync_group
Definition: isom.h:248
MOVStreamContext::height
int height
tkhd height
Definition: isom.h:234
MOVContext::moov_retry
int moov_retry
Definition: isom.h:360
MOV_TRUN_SAMPLE_FLAGS
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:421
AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT
@ AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT
Ambient viewing environment metadata, as defined by H.274.
Definition: packet.h:327
MOVContext::nb_chapter_tracks
unsigned int nb_chapter_tracks
Definition: isom.h:348
mix
static int mix(int c0, int c1)
Definition: 4xm.c:717
MOVStreamContext::last_stsd_index
int last_stsd_index
Definition: isom.h:263
ff_ac3_channel_layout_tab
const uint16_t ff_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3_channel_layout_tab.h:31
AVStreamGroup::params
union AVStreamGroup::@450 params
Group type-specific parameters.
r
const char * r
Definition: vf_curves.c:127
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
flag
int flag
Definition: cpu.c:40
get_heif_item
static HEIFItem * get_heif_item(MOVContext *c, unsigned id)
Get the requested item.
Definition: mov.c:195
MOV_TKHD_FLAG_ENABLED
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:434
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:53
AVStreamGroup::tile_grid
struct AVStreamGroupTileGrid * tile_grid
Definition: avformat.h:1175
AVFMT_SHOW_IDS
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition: avformat.h:477
AVSphericalMapping::projection
enum AVSphericalProjection projection
Projection type.
Definition: spherical.h:104
current
static struct @589 current
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
av_exif_parse_buffer
int av_exif_parse_buffer(void *logctx, const uint8_t *buf, size_t size, AVExifMetadata *ifd, enum AVExifHeaderMode header_mode)
Decodes the EXIF data provided in the buffer and writes it into the struct *ifd.
Definition: exif.c:882
AV_STREAM_GROUP_PARAMS_LCEVC
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition: avformat.h:1132
MOVStreamContext::extradata
uint8_t ** extradata
extradata array (and size) for multiple stsd
Definition: isom.h:261
mov_class
static const AVClass mov_class
Definition: mov.c:12125
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:188
MOVStreamContext::open_key_samples
int * open_key_samples
Definition: isom.h:253
AVUUID
uint8_t AVUUID[AV_UUID_LEN]
Definition: uuid.h:60
MovTref
Definition: isom.h:94
mov_parse_tmcd_streams
static int mov_parse_tmcd_streams(AVFormatContext *s)
Definition: mov.c:10981
out
static FILE * out
Definition: movenc.c:55
MOVFragmentStreamInfo
Definition: isom.h:145
AVFieldOrder
AVFieldOrder
Definition: defs.h:211
mov_read_targa_y216
static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2339
mov_read_chnl
static int mov_read_chnl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1241
mov_read_moof
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1869
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
ctype
#define ctype
Definition: afir_template.c:46
AV_PKT_DATA_FRAME_CROPPING
@ AV_PKT_DATA_FRAME_CROPPING
The number of pixels to discard from the top/bottom/left/right border of the decoded frame to obtain ...
Definition: packet.h:340
av_exif_write
int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode)
Allocates a buffer using av_malloc of an appropriate size and writes the EXIF data represented by ifd...
Definition: exif.c:753
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:49
HEIFItem::icc_profile
uint8_t * icc_profile
Definition: isom.h:314
AVFMT_FLAG_IGNIDX
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1467
AVExifMetadata
Definition: exif.h:75
sanity_checks
static int sanity_checks(void *log_obj, MOVStreamContext *sc, int index)
Definition: mov.c:5289
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:58
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:192
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
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:94
tmcd_is_referenced
static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
Definition: mov.c:10356
HEIFItem::hflip
int hflip
Definition: isom.h:310
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:56
IAMFAudioElement::nb_substreams
unsigned int nb_substreams
Definition: iamf.h:99
AVStream::priv_data
void * priv_data
Definition: avformat.h:772
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
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:673
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:818
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:213
mov_options
static const AVOption mov_options[]
Definition: mov.c:12071
mov_codec_id
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:2777
AV_PKT_DATA_MASTERING_DISPLAY_METADATA
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:219
MOVStreamContext::sample_offsets
int32_t * sample_offsets
Definition: isom.h:251
av_int2double
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
mov_read_iloc
static int mov_read_iloc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9120
matrix
Definition: vc1dsp.c:43
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:665
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:424
int64_t
long long int64_t
Definition: coverity.c:34
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:226
mov_read_alac
static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2311
test_same_origin
static int test_same_origin(const char *src, const char *ref)
Definition: mov.c:5132
cbcs_scheme_decrypt
static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8513
MOVFragment::base_data_offset
uint64_t base_data_offset
Definition: isom.h:110
MOVStreamContext
Definition: isom.h:179
AVChannelLayout::map
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
Definition: channel_layout.h:370
MOVStreamContext::stsc_data
MOVStsc * stsc_data
Definition: isom.h:200
ff_buffer_packet
int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
Definition: demux.c:622
IS_MATRIX_IDENT
#define IS_MATRIX_IDENT(matrix)
Definition: mov.c:5678
AVStreamGroup::disposition
int disposition
Stream group disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:1225
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition: avformat.h:620
mov_update_dts_shift
static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
Definition: mov.c:3830
MOVStreamContext::spherical
AVSphericalMapping * spherical
Definition: isom.h:270
mask
int mask
Definition: mediacodecdec_common.c:154
out_size
static int out_size
Definition: movenc.c:56
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:111
avio_get_str16be
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
MOVEncryptionIndex
Definition: isom.h:132
av_encryption_init_info_free
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
Definition: encryption_info.c:216
mov_read_avss
static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2316
AV_RB32A
#define AV_RB32A(p)
Definition: intreadwrite.h:575
MOVContext::primary_item_id
int primary_item_id
Definition: isom.h:385
mode
Definition: swscale.c:71
MOVContext::found_moov
int found_moov
'moov' atom has been found
Definition: isom.h:331
ff_iamf_read_packet
int ff_iamf_read_packet(AVFormatContext *s, IAMFDemuxContext *c, AVIOContext *pb, int max_size, int stream_id_offset, AVPacket *pkt)
Definition: iamf_reader.c:308
mov_read_custom
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5514
pixdesc.h
cleanup
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:130
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
HEIFGrid::nb_tiles
int nb_tiles
Definition: isom.h:323
ID3v1_GENRE_MAX
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
MOVSbgp
Definition: isom.h:127
MOVCtts
Definition: isom.h:68
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:424
mov_read_extradata
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, enum AVCodecID codec_id)
Definition: mov.c:2285
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
mpegaudiodecheader.h
MOVStreamContext::rap_group_count
unsigned int rap_group_count
Definition: isom.h:245
av_display_matrix_flip
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:66
av_sha_init
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:274
HEIFItem::type
int type
Definition: isom.h:312
mov_read_mvhd
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1971
mov_read_idat
static int mov_read_idat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9114
AVPacket::data
uint8_t * data
Definition: packet.h:603
MOVContext::found_mdat
int found_mdat
'mdat' atom has been found
Definition: isom.h:334
MOVStreamContext::drefs_count
unsigned drefs_count
Definition: isom.h:228
AVEncryptionInfo::crypt_byte_block
uint32_t crypt_byte_block
Only used for pattern encryption.
Definition: encryption_info.h:51
AV_PKT_DATA_ENCRYPTION_INIT_INFO
@ AV_PKT_DATA_ENCRYPTION_INIT_INFO
This side data is encryption initialization data.
Definition: packet.h:246
mov_read_avid
static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2331
AVAmbientViewingEnvironment::ambient_light_x
AVRational ambient_light_x
Normalized x chromaticity coordinate of the environmental ambient light in the nominal viewing enviro...
Definition: ambient_viewing_environment.h:47
AVCodecParameters::seek_preroll
int seek_preroll
Number of audio samples to skip after a discontinuity.
Definition: codec_par.h:256
av_dynarray2_add
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:343
AVOption
AVOption.
Definition: opt.h:429
MOVContext::trex_data
MOVTrackExt * trex_data
Definition: isom.h:343
mov_read_stps
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3483
MOVContext::bitrates
int * bitrates
bitrates read before streams creation
Definition: isom.h:358
b
#define b
Definition: input.c:43
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:669
MOVContext::interleaved_read
int interleaved_read
Definition: isom.h:392
mov_read_tkhd
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5686
MOVElst::rate
float rate
Definition: isom.h:82
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:836
table
static const uint16_t table[]
Definition: prosumer.c:203
spherical.h
mov_read_colr
static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2125
data
const char data[16]
Definition: mxf.c:149
set_last_stream_little_endian
static void set_last_stream_little_endian(AVFormatContext *fc)
Definition: mov.c:2010
mov_read_strf
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:2690
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:477
HEIFItem::st
AVStream * st
Definition: isom.h:300
FF_COMPLIANCE_STRICT
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: defs.h:59
AV_DICT_APPEND
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:82
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:239
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:442
av_iamf_mix_presentation_free
void av_iamf_mix_presentation_free(AVIAMFMixPresentation **pmix_presentation)
Free an AVIAMFMixPresentation and all its contents.
Definition: iamf.c:536
AVStreamGroupTREF::metadata_index
unsigned int metadata_index
Index of the metadata stream in the AVStreamGroup.
Definition: avformat.h:1124
mov_parse_auxiliary_info
static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
Definition: mov.c:7776
mov_seek_stream
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:11921
mov_read_saio
static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7922
mov_get_stsc_samples
static int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
Definition: mov.c:3468
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:226
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:530
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:4039
MOVFragmentIndexItem::moof_offset
int64_t moof_offset
Definition: isom.h:159
MOVStreamContext::spherical_size
size_t spherical_size
Definition: isom.h:271
mov_change_extradata
static int mov_change_extradata(AVStream *st, AVPacket *pkt)
Definition: mov.c:11520
nb_streams
static unsigned int nb_streams
Definition: ffprobe.c:352
HEIFItemRef
Definition: isom.h:294
MP4TrackKindMapping::scheme_uri
const char * scheme_uri
Definition: isom.h:494
AVINDEX_DISCARD_FRAME
#define AVINDEX_DISCARD_FRAME
Definition: avformat.h:610
av_display_rotation_set
void av_display_rotation_set(int32_t matrix[9], double angle)
Initialize a transformation matrix describing a pure clockwise rotation by the specified angle (in de...
Definition: display.c:51
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:621
AVStreamGroup::tref
struct AVStreamGroupTREF * tref
Definition: avformat.h:1184
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:61
fixed_key
static const uint8_t fixed_key[]
Definition: aes_ctr.c:45
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:68
AVStereo3D::baseline
uint32_t baseline
The distance between the centres of the lenses of the camera system, in micrometers.
Definition: stereo3d.h:228
MOVDref::dir
char * dir
Definition: isom.h:88
mov_current_sample_set
static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
Definition: mov.c:4343
mathematics.h
AVDictionary
Definition: dict.c:32
ffio_init_read_context
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition: aviobuf.c:99
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:677
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MOVAtom
Definition: isom.h:100
AVStreamGroupTileGrid::vertical
int vertical
Offset in pixels from the top edge of the canvas where the tile should be placed.
Definition: avformat.h:1003
iamf_parse.h
mov_read_moov
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1619
av_sub_q
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition: rational.c:101
cffstream
static const av_always_inline FFStream * cffstream(const AVStream *st)
Definition: internal.h:367
mov_read_esds
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:863
MOVStreamContext::sample_count
unsigned int sample_count
Definition: isom.h:211
MOVFragmentIndex::allocated_size
int allocated_size
Definition: isom.h:167
MOVTrackExt::flags
unsigned flags
Definition: isom.h:124
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
HEIFItem::height
int height
Definition: isom.h:308
AV_SPHERICAL_EQUIRECTANGULAR
@ AV_SPHERICAL_EQUIRECTANGULAR
Video represents a sphere mapped on a flat surface using equirectangular projection.
Definition: spherical.h:52
intfloat.h
id3v1.h
MOVStreamContext::ctts_data
MOVCtts * ctts_data
Definition: isom.h:198
ff_dvdclut_yuv_to_rgb
int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size)
Definition: dvdclut.c:50
AV_CODEC_ID_R10K
@ AV_CODEC_ID_R10K
Definition: codec_id.h:197
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:231
AV_STEREO3D_VIEW_RIGHT
@ AV_STEREO3D_VIEW_RIGHT
Frame contains only the right view.
Definition: stereo3d.h:163
MOVContext::advanced_editlist_autodisabled
int advanced_editlist_autodisabled
Definition: isom.h:352
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:326
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
mov_read_stco
static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2712
AV_STEREO3D_VIEW_UNSPEC
@ AV_STEREO3D_VIEW_UNSPEC
Content is unspecified.
Definition: stereo3d.h:168
mov_read_vexu
static int mov_read_vexu(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7287
FFIOContext
Definition: avio_internal.h:28
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:658
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
MOVStreamContext::aes_ctr
struct AVAESCTR * aes_ctr
Definition: isom.h:283
cenc_filter
static int cenc_filter(MOVContext *mov, AVStream *st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
Definition: mov.c:8626
mov_read_sdtp
static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3792
mov_read_jp2h
static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2321
AV_STEREO3D_UNSPEC
@ AV_STEREO3D_UNSPEC
Video is stereoscopic but the packing is unspecified.
Definition: stereo3d.h:143
AVIndexEntry
Definition: avformat.h:601
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:429
MOVStreamContext::dv_audio_container
int dv_audio_container
Definition: isom.h:225
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:443
mov_read_tfhd
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5791
AV_CODEC_ID_BIN_DATA
@ AV_CODEC_ID_BIN_DATA
Definition: codec_id.h:615
OPUS_SEEK_PREROLL_MS
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:36
AV_CODEC_ID_H261
@ AV_CODEC_ID_H261
Definition: codec_id.h:55
IAMFMixPresentation::cmix
const AVIAMFMixPresentation * cmix
Definition: iamf.h:108
MOVContext::decryption_default_key
uint8_t * decryption_default_key
Definition: isom.h:378
mov_read_wide
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6371
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:609
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:217
MOVStreamContext::stsd_count
int stsd_count
Definition: isom.h:264
ff_mov_lang_to_iso639
int ff_mov_lang_to_iso639(unsigned code, char to[4])
Definition: isom.c:260
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: demux_utils.c:205
ff_get_extradata
int ff_get_extradata(void *logctx, 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: demux_utils.c:340
mov_add_tref_id
static int mov_add_tref_id(MovTref *tag, uint32_t id)
Definition: mov.c:2583
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:280
AVStereo3D::horizontal_field_of_view
AVRational horizontal_field_of_view
Horizontal field of view, in degrees.
Definition: stereo3d.h:239
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:190
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:199
MOVStreamContext::has_palette
int has_palette
Definition: isom.h:239
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:464
AV_STEREO3D_SIDEBYSIDE
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:64
MOVIndexRange::start
int64_t start
Definition: isom.h:175
AVPacketSideData::size
size_t size
Definition: packet.h:426
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
ff_remove_stream
void ff_remove_stream(AVFormatContext *s, AVStream *st)
Remove a stream from its AVFormatContext and free it.
Definition: avformat.c:121
OFFSET
#define OFFSET(x)
Definition: mov.c:12069
HEIFGrid::tile_item_list
HEIFItem ** tile_item_list
Definition: isom.h:320
set_icc_profile_from_item
static int set_icc_profile_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data, const HEIFItem *item)
Definition: mov.c:10493
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:214
AV_STEREO3D_VIEW_PACKED
@ AV_STEREO3D_VIEW_PACKED
Frame contains two packed views.
Definition: stereo3d.h:153
AV_SPHERICAL_PARAMETRIC_IMMERSIVE
@ AV_SPHERICAL_PARAMETRIC_IMMERSIVE
Parametric Immersive projection (Apple).
Definition: spherical.h:90
MOVStreamContext::nb_frames_for_fps
int nb_frames_for_fps
Definition: isom.h:257
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:738
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:902
FF_DVDCLUT_CLUT_LEN
#define FF_DVDCLUT_CLUT_LEN
Definition: dvdclut.h:28
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:374
aax_filter
static int aax_filter(uint8_t *input, int size, MOVContext *c)
Definition: mov.c:1557
AV_OPT_TYPE_BINARY
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition: opt.h:286
av_color_space_name
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:3856
av_packet_add_side_data
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: packet.c:197
MOVStreamContext::mastering
AVMasteringDisplayMetadata * mastering
Definition: isom.h:272
AV_CODEC_ID_SPEEX
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:496
AV_FOURCC_MAX_STRING_SIZE
#define AV_FOURCC_MAX_STRING_SIZE
Definition: avutil.h:345
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:47
mov_find_tref_id
static int mov_find_tref_id(const MovTref *tag, uint32_t id)
Definition: mov.c:2574
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:362
MOVFragmentIndexItem::current
int current
Definition: isom.h:161
mov_add_tref_tag
static MovTref * mov_add_tref_tag(MOVStreamContext *sc, uint32_t name)
Definition: mov.c:2609
AVFMT_SEEK_TO_PTS
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition: avformat.h:501
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:340
mov_read_mdhd
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1925
mov_read_ctts
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3841
MOVTrackExt
Definition: isom.h:119
MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:425
mov_read_aclr
static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2398
AVSEEK_FLAG_ANY
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2575
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:108
AVStreamGroupTileGrid
AVStreamGroupTileGrid holds information on how to combine several independent images on a single canv...
Definition: avformat.h:954
FF_DVDCLUT_CLUT_SIZE
#define FF_DVDCLUT_CLUT_SIZE
Definition: dvdclut.h:29
AV_STEREO3D_2D
@ AV_STEREO3D_2D
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:52
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
HEIFItem::item_id
int item_id
Definition: isom.h:304
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: packet.c:113
timecode.h
get_current_frag_stream_info
static MOVFragmentStreamInfo * get_current_frag_stream_info(MOVFragmentIndex *frag_index)
Definition: mov.c:1676
GetBitContext
Definition: get_bits.h:109
MOVStreamContext::mastering_size
size_t mastering_size
Definition: isom.h:273
AVStreamGroupTileGrid::coded_width
int coded_width
Width of the canvas.
Definition: avformat.h:969
av_iamf_audio_element_free
void av_iamf_audio_element_free(AVIAMFAudioElement **paudio_element)
Free an AVIAMFAudioElement and all its contents.
Definition: iamf.c:338
FFStream::index_entries_allocated_size
unsigned int index_entries_allocated_size
Definition: internal.h:187
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
mov_read_amve
static int mov_read_amve(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6747
mov_read_enda
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2039
mov_read_chap
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5838
MOVParseTableEntry
Definition: mov.c:83
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
av_exif_free
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition: exif.c:659
MOV_TRUN_SAMPLE_DURATION
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:419
val
static double val(void *priv, double ch)
Definition: aeval.c:77
set_display_matrix_from_item
static int set_display_matrix_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data, const HEIFItem *item)
Definition: mov.c:10507
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:326
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:678
AVStreamGroupTileGrid::coded_height
int coded_height
Width of the canvas.
Definition: avformat.h:975
pts
static int64_t pts
Definition: transcode_aac.c:649
MOVStreamContext::v_spacing
int v_spacing
pasp vSpacing
Definition: isom.h:236
AVEncryptionInfo::iv
uint8_t * iv
The initialization vector.
Definition: encryption_info.h:71
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:462
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:806
AVAmbientViewingEnvironment::ambient_illuminance
AVRational ambient_illuminance
Environmental illuminance of the ambient viewing environment in lux.
Definition: ambient_viewing_environment.h:40
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
MOVStreamContext::width
int width
tkhd width
Definition: isom.h:233
MOVContext::meta_keys
char ** meta_keys
Definition: isom.h:337
get_key_from_kid
static int get_key_from_kid(uint8_t *out, int len, MOVContext *c, AVEncryptionInfo *sample)
Definition: mov.c:8280
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
MOVStreamContext::extradata_size
int * extradata_size
Definition: isom.h:262
AVIAMFAudioElement::audio_element_type
enum AVIAMFAudioElementType audio_element_type
Audio element type as defined in section 3.6 of IAMF.
Definition: iamf.h:391
loop
static int loop
Definition: ffplay.c:337
AV_STREAM_GROUP_PARAMS_DOLBY_VISION
@ AV_STREAM_GROUP_PARAMS_DOLBY_VISION
Definition: avformat.h:1134
ff_data_to_hex
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition: utils.c:458
update_frag_index
static int update_frag_index(MOVContext *c, int64_t offset)
Definition: mov.c:1793
AV_CODEC_ID_PRORES_RAW
@ AV_CODEC_ID_PRORES_RAW
Definition: codec_id.h:333
AVRational::num
int num
Numerator.
Definition: rational.h:59
MOVStreamContext::keyframes
int * keyframes
Definition: isom.h:215
av_unused
#define av_unused
Definition: attributes.h:164
MOVEncryptionIndex::auxiliary_info_sample_count
size_t auxiliary_info_sample_count
Definition: isom.h:139
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:216
mov_read_pack
static int mov_read_pack(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6819
AVStream::attached_pic
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:845
MOVStsc::id
int id
Definition: isom.h:76
mov_read_saiz
static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7838
MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:432
mov_merge_tts_data
static int mov_merge_tts_data(MOVContext *mov, AVStream *st, int flags)
Definition: mov.c:4759
MOVContext::idat_offset
int64_t idat_offset
Definition: isom.h:391
MOV_TRUN_DATA_OFFSET
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:417
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:65
av_ambient_viewing_environment_alloc
AVAmbientViewingEnvironment * av_ambient_viewing_environment_alloc(size_t *size)
Allocate an AVAmbientViewingEnvironment structure.
Definition: ambient_viewing_environment.c:31
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:79
IAMFAudioElement::element
AVIAMFAudioElement * element
element backs celement iff the AVIAMFAudioElement is owned by this structure.
Definition: iamf.h:95
MOVStreamContext::elst_data
MOVElst * elst_data
Definition: isom.h:205
mov_read_ares
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2353
mov_read_adrm
static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1431
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:541
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:191
IAMFContext::audio_elements
IAMFAudioElement ** audio_elements
Definition: iamf.h:131
AVChannelLayout::u
union AVChannelLayout::@518 u
Details about which channels are present in this layout.
MOVFragmentIndex::complete
int complete
Definition: isom.h:168
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:343
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:764
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:236
MOVStreamContext::stsc_sample
int stsc_sample
Definition: isom.h:202
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AV_CODEC_ID_MACE3
@ AV_CODEC_ID_MACE3
Definition: codec_id.h:470
MOVTrackExt::track_id
unsigned track_id
Definition: isom.h:120
mov_free_encryption_index
static void mov_free_encryption_index(MOVEncryptionIndex **index)
Definition: mov.c:10224
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:178
MOVEncryptionIndex::auxiliary_offsets
uint64_t * auxiliary_offsets
Absolute seek position.
Definition: isom.h:141
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:212
MOVStreamContext::dts_shift
int dts_shift
dts shift when ctts is negative
Definition: isom.h:237
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:201
AVCodecParameters::frame_size
int frame_size
Audio frame size, if known.
Definition: codec_par.h:227
mov_read_srat
static int mov_read_srat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1092
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:92
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:60
AES_CTR_KEY_SIZE
#define AES_CTR_KEY_SIZE
Definition: aes_ctr.h:35
MOVStreamContext::stsd_version
int stsd_version
Definition: isom.h:265
mov_parse_exif_item
static int mov_parse_exif_item(AVFormatContext *s, AVPacketSideData **coded_side_data, int *nb_coded_side_data, const HEIFItem *ref)
Definition: mov.c:10692
AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
@ AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
Definition: avformat.h:1130
ff_add_attached_pic
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
Definition: demux_utils.c:107
add_tts_entry
static int add_tts_entry(MOVTimeToSample **tts_data, unsigned int *tts_count, unsigned int *allocated_size, int count, int offset, unsigned int duration)
Definition: mov.c:4234
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:497
FF_MOV_FLAG_MFRA_PTS
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:467
MOV_MERGE_STTS
#define MOV_MERGE_STTS
Definition: mov.c:4753
MOVStreamContext::iamf_stream_offset
int iamf_stream_offset
Definition: isom.h:291
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:6681
s
#define s(width, name)
Definition: cbs_vp9.c:198
MOVFragmentStreamInfo::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:154
mov_read_trak
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5308
IAMFSubStream::audio_substream_id
unsigned int audio_substream_id
Definition: iamf.h:83
MOVContext::fc
AVFormatContext * fc
Definition: isom.h:328
MOV_TFHD_DEFAULT_BASE_IS_MOOF
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:415
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: packet.c:98
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:130
AV_CODEC_ID_EVC
@ AV_CODEC_ID_EVC
Definition: codec_id.h:325
IAMFLayer::substream_count
unsigned int substream_count
Definition: iamf.h:78
MOV_TFHD_DEFAULT_DURATION
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:411
ALAC_EXTRADATA_SIZE
#define ALAC_EXTRADATA_SIZE
DRM_BLOB_SIZE
#define DRM_BLOB_SIZE
Definition: mov.c:1429
MOVStreamContext::sample_offsets_count
int sample_offsets_count
Definition: isom.h:252
MOVCtts::count
unsigned int count
Definition: isom.h:69
MOVStreamContext::drefs
MOVDref * drefs
Definition: isom.h:229
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
MOVContext::aes_decrypt
struct AVAES * aes_decrypt
Definition: isom.h:377
AVSphericalMapping::bound_top
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:186
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
MOVFragmentIndex::nb_items
int nb_items
Definition: isom.h:170
AVCodecParameters::width
int width
The width of the video frame in pixels.
Definition: codec_par.h:143
MOVTimeToSample
Definition: isom.h:57
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:461
HEIFGrid::tile_idx_list
unsigned * tile_idx_list
Definition: isom.h:322
MOV_TRUN_FIRST_SAMPLE_FLAGS
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:418
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AVIndexEntry::size
int size
Definition: avformat.h:612
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:253
MOVStreamContext::keyframe_absent
int keyframe_absent
Definition: isom.h:213
info
MIPS optimizations info
Definition: mips.txt:2
MOVStts::duration
unsigned int duration
Definition: isom.h:65
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:494
MOVStreamContext::coll_size
size_t coll_size
Definition: isom.h:275
tile_rows
int tile_rows
Definition: h265_levels.c:217
mov_estimate_video_delay
static void mov_estimate_video_delay(MOVContext *c, AVStream *st)
Definition: mov.c:4263
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:603
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:42
MOVStreamContext::min_corrected_pts
int64_t min_corrected_pts
minimum Composition time shown by the edits excluding empty edits.
Definition: isom.h:218
ffio_read_leb
unsigned int ffio_read_leb(AVIOContext *s)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition: aviobuf.c:930
tile_cols
int tile_cols
Definition: av1_levels.c:73
MOVStreamContext::sdtp_count
unsigned int sdtp_count
Definition: isom.h:194
dvdclut.h
MOVStreamContext::nb_tref_tags
int nb_tref_tags
Definition: isom.h:231
mov_read_lhvc
static int mov_read_lhvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8831
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
AVPacketSideData::data
uint8_t * data
Definition: packet.h:425
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:58
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
hevc.h
get_bits.h
limits.h
mov_read_sidx
static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6232
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:111
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
ff_iamfdec_read_descriptors
int ff_iamfdec_read_descriptors(IAMFContext *c, AVIOContext *pb, int max_size, void *log_ctx)
Definition: iamf_parse.c:1232
FFStream::display_aspect_ratio
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition: internal.h:296
IAMFContext::nb_mix_presentations
int nb_mix_presentations
Definition: iamf.h:134
mov_find_next_sample
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:11447
ff_mov_read_chnl
int ff_mov_read_chnl(AVFormatContext *s, AVIOContext *pb, AVStream *st, int version)
Read 'chnl' tag from the input stream.
Definition: mov_chan.c:768
AV_CODEC_ID_TARGA_Y216
@ AV_CODEC_ID_TARGA_Y216
Definition: codec_id.h:258
AVIndexEntry::min_distance
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:613
mov_read_dvcc_dvvc
static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8784
MOVParseTableEntry::parse
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:85
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
MOVStreamContext::tref_tags
MovTref * tref_tags
Definition: isom.h:232
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:75
key
const char * key
Definition: hwcontext_opencl.c:189
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:88
MOVStreamContext::sdtp_data
uint8_t * sdtp_data
Definition: isom.h:195
color_range
color_range
Definition: vf_selectivecolor.c:43
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:202
mov_read_udta_string
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:346
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
mov_read_stss
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3524
mov_read_ddts
static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1169
mov_read_uuid
static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7441
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:639
MOVTrackExt::duration
unsigned duration
Definition: isom.h:122
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVStreamGroup::layered_video
struct AVStreamGroupLayeredVideo * layered_video
Definition: avformat.h:1176
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:72
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
av_sha_final
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:347
MOVStreamContext::current_sample
int current_sample
Definition: isom.h:219
MOVFragmentStreamInfo::sidx_pts
int64_t sidx_pts
Definition: isom.h:147
MAX_REORDER_DELAY
#define MAX_REORDER_DELAY
Definition: mov.c:4262
MOVFragmentIndex::current
int current
Definition: isom.h:169
MOVEncryptionIndex::encrypted_samples
AVEncryptionInfo ** encrypted_samples
Definition: isom.h:136
mov_read_close
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:10301
ff_hex_to_data
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:479
MOVAtom::size
int64_t size
Definition: isom.h:102
MOVStreamContext::refcount
int refcount
Definition: isom.h:181
AVStereo3D::flags
int flags
Additional information about the frame packing.
Definition: stereo3d.h:212
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:473
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
AV_CODEC_ID_AVUI
@ AV_CODEC_ID_AVUI
Definition: codec_id.h:257
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:6391
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
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:7666
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:314
MOVStreamContext::keyframe_count
unsigned int keyframe_count
Definition: isom.h:214
mov_read_SAND
static int mov_read_SAND(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9061
IAMFAudioElement::audio_element_id
unsigned int audio_element_id
Definition: iamf.h:96
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:232
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
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:3672
MOVStreamContext::index_ranges
MOVIndexRange * index_ranges
Definition: isom.h:221
fail
#define fail
Definition: test.h:478
DDTS_SIZE
#define DDTS_SIZE
internal.h
MOVTrackExt::stsd_id
unsigned stsd_id
Definition: isom.h:121
find_prev_closest_index
static int find_prev_closest_index(AVStream *st, AVIndexEntry *e_old, int nb_old, MOVTimeToSample *tts_data, int64_t tts_count, int64_t timestamp_pts, int flag, int64_t *index, int64_t *tts_index, int64_t *tts_sample)
Find the closest previous frame to the timestamp_pts, in e_old index entries.
Definition: mov.c:4085
set_frag_stream
static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
Definition: mov.c:1656
mov_read_free
static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7546
mov_realloc_extradata
static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
Definition: mov.c:2248
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:770
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
av_exif_get_tag_id
int32_t av_exif_get_tag_id(const char *name)
Retrieves the tag ID associated with the provided tag string name.
Definition: exif.c:244
AV_PKT_DATA_EXIF
@ AV_PKT_DATA_EXIF
Extensible image file format metadata.
Definition: packet.h:369
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2573
aes.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
MOVStreamContext::tts_sample
int tts_sample
Definition: isom.h:208
avpriv_dv_get_packet
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:733
MOVContext::ignore_editlist
int ignore_editlist
Definition: isom.h:350
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
metadata
Stream codec metadata
Definition: ogg-flac-chained-meta.txt:2
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:786
NULL
#define NULL
Definition: coverity.c:32
sha.h
truehd_layout
static uint64_t truehd_layout(int chanmap)
Definition: mlp_parse.h:105
MOVDref
Definition: isom.h:85
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:46
MOVStreamContext::ctts_count
unsigned int ctts_count
Definition: isom.h:196
AVEncryptionInitInfo
This describes info used to initialize an encryption key system.
Definition: encryption_info.h:88
isom.h
MP4TrackKindValueMapping::disposition
int disposition
Definition: isom.h:489
mov_read_ftyp
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1571
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
MOVContext::nb_heif_grid
int nb_heif_grid
Definition: isom.h:390
read_image_grid
static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid, AVStreamGroupTileGrid *tile_grid)
Definition: mov.c:10528
MOVElst
Definition: isom.h:79
av_aes_ctr_alloc
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:41
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:63
AVStreamGroupTileGrid::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the grid.
Definition: avformat.h:1057
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_PRIMARY_EYE_LEFT
@ AV_PRIMARY_EYE_LEFT
Left eye.
Definition: stereo3d.h:183
mov_read_sgpd
static int mov_read_sgpd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3922
AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
The specified retype target order is ignored and the simplest possible (canonical) order is used for ...
Definition: channel_layout.h:721
mov_probe
static int mov_probe(const AVProbeData *p)
Definition: mov.c:9943
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition: opt.h:290
MOVDref::nlvl_to
int16_t nlvl_to
Definition: isom.h:91
get_eia608_packet
static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int src_size)
Definition: mov.c:11544
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:574
AVIndexEntry::flags
int flags
Definition: avformat.h:611
MOVStreamContext::time_offset
int64_t time_offset
time offset of the edit list entries
Definition: isom.h:217
mov_read_smdm
static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6590
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
AVStereo3D::horizontal_disparity_adjustment
AVRational horizontal_disparity_adjustment
Relative shift of the left and right images, which changes the zero parallax plane.
Definition: stereo3d.h:234
avio_rb64
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:911
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:171
MOVStreamContext::current_index_range
MOVIndexRange * current_index_range
Definition: isom.h:222
mov_open_dref
static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
Definition: mov.c:5161
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:186
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
av_aes_alloc
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:37
TAG_IS_AVCI
#define TAG_IS_AVCI(tag)
Definition: isom.h:443
IAMFSubStream
Definition: iamf.h:82
IAMFAudioElement::layers
IAMFLayer * layers
Definition: iamf.h:103
mov_read_schm
static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8138
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:827
FLAC_STREAMINFO_SIZE
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:32
av_color_primaries_name
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition: pixdesc.c:3790
FF_MOV_FLAG_MFRA_DTS
#define FF_MOV_FLAG_MFRA_DTS
Definition: isom.h:466
MOV_SAMPLE_DEPENDENCY_NO
#define MOV_SAMPLE_DEPENDENCY_NO
Definition: isom.h:441
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
ff_codec_movvideo_tags
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom_tags.c:29
attributes.h
MOVStreamContext::tts_index
int tts_index
Definition: isom.h:207
AV_DISPOSITION_MULTILAYER
#define AV_DISPOSITION_MULTILAYER
The video stream contains multiple layers, e.g.
Definition: avformat.h:717
MOVFragmentStreamInfo::index_base
int index_base
Definition: isom.h:152
MOVStreamContext::rap_group
MOVSbgp * rap_group
Definition: isom.h:246
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:480
mov_read_ilst
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5459
MOVTimeToSample::duration
unsigned int duration
Definition: isom.h:59
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:177
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition: codec_par.h:207
get_frag_stream_info_from_pkt
static MOVFragmentStreamInfo * get_frag_stream_info_from_pkt(MOVFragmentIndex *frag_index, AVPacket *pkt, int id)
Definition: mov.c:8601
get_sgpd_sync_index
static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
Definition: mov.c:4681
mov_read_fiel
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2214
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: apv_parser.c:92
MOV_MP4_TTML_TAG
#define MOV_MP4_TTML_TAG
Definition: isom.h:484
AV_PKT_DATA_CONTENT_LIGHT_LEVEL
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition: packet.h:232
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:127
MOV_TFHD_BASE_DATA_OFFSET
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:409
MOVFragmentStreamInfo::stsd_id
int stsd_id
Definition: isom.h:155
av_exif_remove_entry
int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags)
Remove an entry from the provided EXIF metadata struct.
Definition: exif.c:1289
MOVStreamContext::open_key_samples_count
int open_key_samples_count
Definition: isom.h:254
HEIFItem
Definition: isom.h:299
ff_codec_movaudio_tags
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom_tags.c:311
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:1275
av_stereo3d_alloc_size
AVStereo3D * av_stereo3d_alloc_size(size_t *size)
Allocate an AVStereo3D structure and set its fields to default values.
Definition: stereo3d.c:39
index
int index
Definition: gxfenc.c:90
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:462
MOVSbgp::count
unsigned int count
Definition: isom.h:128
AVCodecParameters::sample_rate
int sample_rate
The number of audio samples per second.
Definition: codec_par.h:213
mov_parse_stsd_subtitle
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:3002
cid
uint16_t cid
Definition: mxfenc.c:2335
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:3168
MOVStts
Definition: isom.h:63
AVAudioServiceType
AVAudioServiceType
Definition: defs.h:235
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
AV_CODEC_ID_GSM
@ AV_CODEC_ID_GSM
as in Berlin toast format
Definition: codec_id.h:479
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:808
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av_packet_side_data_get
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition: packet.c:646
AVIAMFSubmixElement::audio_element_id
unsigned int audio_element_id
The id of the Audio Element this submix element references.
Definition: iamf.h:455
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:501
should_retry
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:11477
avformat_stream_group_add_stream
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
Add an already allocated stream to a stream group.
Definition: options.c:560
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:225
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:75
AVIAMFSubmix
Submix layout as defined in section 3.7 of IAMF.
Definition: iamf.h:559
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:415
mov_read_pasp
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1399
MOVContext::dv_demux
DVDemuxContext * dv_demux
Definition: isom.h:339
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:463
AVStereo3D::primary_eye
enum AVStereo3DPrimaryEye primary_eye
Which eye is the primary eye when rendering in 2D.
Definition: stereo3d.h:222
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
color_primaries
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition: csp.c:76
mov_read_SA3D
static int mov_read_SA3D(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8971
mov_read_elst
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6444
MOVEncryptionIndex::auxiliary_info_default_size
uint8_t auxiliary_info_default_size
Definition: isom.h:140
AV_STREAM_GROUP_PARAMS_TILE_GRID
@ AV_STREAM_GROUP_PARAMS_TILE_GRID
Definition: avformat.h:1131
IAMFAudioElement
Definition: iamf.h:89
AV_UUID_LEN
#define AV_UUID_LEN
Definition: uuid.h:57
av_sat_sub64
#define av_sat_sub64
Definition: common.h:142
mov_read_header
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:11217
AV_CODEC_ID_QCELP
@ AV_CODEC_ID_QCELP
Definition: codec_id.h:485
mov_read_vdep
static int mov_read_vdep(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2658
AV_SPHERICAL_HALF_EQUIRECTANGULAR
@ AV_SPHERICAL_HALF_EQUIRECTANGULAR
Video frame displays as a 180 degree equirectangular projection.
Definition: spherical.h:73
cbc1_scheme_decrypt
static int cbc1_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8376
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:733
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:231
MOVFragment::flags
unsigned flags
Definition: isom.h:116
f
f
Definition: af_crystalizer.c:122
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:351
mov_read_wave
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2446
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:61
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:757
ff_mpa_check_header
static int ff_mpa_check_header(uint32_t header)
Definition: mpegaudiodecheader.h:62
MOVStreamContext::aes_ctx
struct AVAES * aes_ctx
Definition: isom.h:284
cens_scheme_decrypt
static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8442
MOVContext::handbrake_version
int handbrake_version
Definition: isom.h:346
mov_free_stream_context
static void mov_free_stream_context(AVFormatContext *s, AVStream *st)
Definition: mov.c:10236
AVPacket::size
int size
Definition: packet.h:604
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:94
AV_STREAM_GROUP_PARAMS_TREF
@ AV_STREAM_GROUP_PARAMS_TREF
Definition: avformat.h:1133
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:143
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:164
MOVFragmentIndexItem
Definition: isom.h:158
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:7615
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
av_aes_ctr_init
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:74
height
#define height
Definition: dsp.h:89
qtpalette.h
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
FFStream
Definition: internal.h:128
mov_read_dref
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:653
mov_current_sample_dec
static void mov_current_sample_dec(MOVStreamContext *sc)
Definition: mov.c:4331
AVSphericalMapping::bound_right
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:187
MOVStsc::first
int first
Definition: isom.h:74
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
av_bswap32
#define av_bswap32
Definition: bswap.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
MOVStreamContext::stsz_sample_size
unsigned int stsz_sample_size
always contains sample size from stsz atom
Definition: isom.h:210
FF_MOV_FLAG_MFRA_AUTO
#define FF_MOV_FLAG_MFRA_AUTO
Definition: isom.h:465
ff_dict_set_timestamp
int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: utils.c:610
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
MOVStreamContext::sgpd_sync
uint8_t * sgpd_sync
Definition: isom.h:249
start_time
static int64_t start_time
Definition: ffplay.c:328
uuid.h
ff_dvdclut_palette_extradata_cat
int ff_dvdclut_palette_extradata_cat(const uint32_t *clut, const size_t clut_size, AVCodecParameters *par)
Definition: dvdclut.c:28
mov_read_trun
static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5924
MOVStreamContext::stereo3d_size
size_t stereo3d_size
Definition: isom.h:269
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:869
mov_read_iprp
static int mov_read_iprp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9578
av_sha_update
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, size_t len)
Update hash value.
Definition: sha.c:315
sample
#define sample
Definition: flacdsp_template.c:44
hypot
static av_const double hypot(double x, double y)
Definition: libm.h:368
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:56
MOV_TFHD_STSD_ID
#define MOV_TFHD_STSD_ID
Definition: isom.h:410
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:1222
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:3382
AV_WL32A
#define AV_WL32A(p, v)
Definition: intreadwrite.h:571
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:188
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:247
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:511
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:75
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
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
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:53
AVStreamGroup::iamf_audio_element
struct AVIAMFAudioElement * iamf_audio_element
Definition: avformat.h:1173
MOVStreamContext::coll
AVContentLightMetadata * coll
Definition: isom.h:274
AVStreamGroupTileGrid::offsets
struct AVStreamGroupTileGrid::@449 * offsets
An nb_tiles sized array of offsets in pixels from the topleft edge of the canvas, indicating where ea...
aes_ctr.h
ff_format_io_close
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: avformat.c:1024
mov_parse_heif_items
static int mov_parse_heif_items(AVFormatContext *s)
Definition: mov.c:10902
HEIFItem::is_idat_relative
int is_idat_relative
Definition: isom.h:313
IAMFContext
Definition: iamf.h:128
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 ffstream(st)->index_entries.
Definition: mov.c:4181
MOVDref::path
char * path
Definition: isom.h:87
mov_current_sample_inc
static void mov_current_sample_inc(MOVStreamContext *sc)
Definition: mov.c:4319
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:825
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:70
dovi_isom.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:602
mov_read_vexu_proj
static int mov_read_vexu_proj(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7037
fix_timescale
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:5233
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:606
IAMFAudioElement::substreams
IAMFSubStream * substreams
Definition: iamf.h:98
AVSphericalMapping::padding
uint32_t padding
Number of pixels to pad from the edge of each cube face.
Definition: spherical.h:200
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
av_channel_layout_retype
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
Definition: channel_layout.c:887
mov_parse_dovi_streams
static int mov_parse_dovi_streams(AVFormatContext *s)
Definition: mov.c:11106
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:225
AVIAMFAudioElement
Information on how to combine one or more audio streams, as defined in section 3.6 of IAMF.
Definition: iamf.h:359
AV_PRIMARY_EYE_NONE
@ AV_PRIMARY_EYE_NONE
Neither eye.
Definition: stereo3d.h:178
mov_read_default
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9821
AV_TIMECODE_FLAG_24HOURSMAX
@ AV_TIMECODE_FLAG_24HOURSMAX
timecode wraps after 24 hours
Definition: timecode.h:37
mov_find_reference_track
static AVStream * mov_find_reference_track(AVFormatContext *s, AVStream *st, uint32_t *tref_id, int nb_tref_id, int first_index)
Definition: mov.c:11037
MOV_MP4_FPCM_TAG
#define MOV_MP4_FPCM_TAG
Definition: isom.h:485
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:1026
AVStreamGroupTileGrid::nb_tiles
unsigned int nb_tiles
Amount of tiles in the grid.
Definition: avformat.h:962
av_packet_side_data_add
AVPacketSideData * av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size, int flags)
Wrap existing data as packet side data.
Definition: packet.c:689
mov_read_packet
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:11677
mov_read_infe
static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9214
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
AV_CODEC_ID_LCEVC
@ AV_CODEC_ID_LCEVC
Definition: codec_id.h:617
MOVEncryptionIndex::auxiliary_offsets_count
size_t auxiliary_offsets_count
Definition: isom.h:142
HEIFItem::vflip
int vflip
Definition: isom.h:311
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:609
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:233
av_encryption_info_free
void av_encryption_info_free(AVEncryptionInfo *info)
Frees the given encryption info object.
Definition: encryption_info.c:82
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:574
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
AVSubsampleEncryptionInfo
This file is part of FFmpeg.
Definition: encryption_info.h:25
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
MOVFragmentIndexItem::stream_info
MOVFragmentStreamInfo * stream_info
Definition: isom.h:163
version
version
Definition: libkvazaar.c:313
AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
@ AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
Definition: avformat.h:1129
AVEncryptionInitInfo::next
struct AVEncryptionInitInfo * next
An optional pointer to the next initialization info in the list.
Definition: encryption_info.h:122
AV_STEREO3D_FLAG_INVERT
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:194
AVStreamGroup::streams
AVStream ** streams
A list of streams in the group.
Definition: avformat.h:1215
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: demux.c:2397
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_MP4_IPCM_TAG
#define MOV_MP4_IPCM_TAG
Definition: isom.h:486
mov_read_clli
static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6718
MOVStreamContext::chunk_offsets
int64_t * chunk_offsets
Definition: isom.h:187
MovTref::id
uint32_t * id
trackID of the referenced track
Definition: isom.h:97
AVStreamGroup::iamf_mix_presentation
struct AVIAMFMixPresentation * iamf_mix_presentation
Definition: avformat.h:1174
AVBufferRef::size
size_t size
Size of data in bytes.
Definition: buffer.h:94
MOVFragmentIndex::item
MOVFragmentIndexItem * item
Definition: isom.h:171
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
MOVStreamContext::iamf
struct IAMFDemuxContext * iamf
Definition: isom.h:290
FFERROR_REDO
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition: demux.h:224
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:178
av_channel_layout_custom_init
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
Initialize a custom channel layout with the specified number of channels.
Definition: channel_layout.c:233
av_aes_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:84
AVFormatContext::error_recognition
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1573
av_mastering_display_metadata_alloc_size
AVMasteringDisplayMetadata * av_mastering_display_metadata_alloc_size(size_t *size)
Allocate an AVMasteringDisplayMetadata structure and set its fields to default values.
Definition: mastering_display_metadata.c:44
mov_read_dfla
static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8240
AV_RB16A
#define AV_RB16A(p)
Definition: intreadwrite.h:561
AVSHA::count
uint64_t count
number of bytes in buffer
Definition: sha.c:37
AV_SPHERICAL_RECTILINEAR
@ AV_SPHERICAL_RECTILINEAR
Video frame displays on a flat, rectangular 2D surface.
Definition: spherical.h:78
mov_default_parse_table
static const MOVParseTableEntry mov_default_parse_table[]
Definition: mov.c:9689
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:91
AVStreamGroupTileGrid::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: avformat.h:1062
mov_metadata_creation_time
static void mov_metadata_creation_time(MOVContext *c, AVIOContext *pb, AVDictionary **metadata, int version)
Definition: mov.c:1895
mov_metadata_hmmt
static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:325
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
MOVFragmentStreamInfo::next_trun_dts
int64_t next_trun_dts
Definition: isom.h:150
MOVStreamContext::stsc_index
unsigned int stsc_index
Definition: isom.h:201
av_sha_alloc
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:46
mov_read_tenc
static int mov_read_tenc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8169
av_uuid_equal
static int av_uuid_equal(const AVUUID uu1, const AVUUID uu2)
Compares two UUIDs for equality.
Definition: uuid.h:119
mov_stsc_index_valid
static int mov_stsc_index_valid(unsigned int index, unsigned int count)
Definition: mov.c:3462
mov_finalize_packet
static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *sample, int64_t current_index, AVPacket *pkt)
Definition: mov.c:11613
MOVIndexRange
Definition: isom.h:174
mov_read_seek
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:12013
bprint.h
MOVContext::advanced_editlist
int advanced_editlist
Definition: isom.h:351
MOVStreamContext::time_scale
int time_scale
Definition: isom.h:216
mlp_parse.h
mac_to_unicode
static const uint32_t mac_to_unicode[128]
Definition: mov.c:151
AVStreamGroupTileGrid::width
int width
Width of the final image for presentation.
Definition: avformat.h:1039
MOVStreamContext::bytes_per_frame
unsigned int bytes_per_frame
Definition: isom.h:223
AVSphericalMapping::roll
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:146
IAMFContext::nb_audio_elements
int nb_audio_elements
Definition: iamf.h:132
MOVIndexRange::end
int64_t end
Definition: isom.h:176
av_malloc
#define av_malloc(s)
Definition: ops_asmgen.c:44
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:596
avio_internal.h
MOVCtts::offset
int offset
Definition: isom.h:70
mov_read_trex
static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5859
search_frag_timestamp
static int search_frag_timestamp(AVFormatContext *s, MOVFragmentIndex *frag_index, AVStream *st, int64_t timestamp)
Definition: mov.c:1768
HEIFItem::width
int width
Definition: isom.h:307
FLAGS
#define FLAGS
Definition: mov.c:12070
MOVStreamContext::stereo3d
AVStereo3D * stereo3d
Definition: isom.h:268
mov_fix_index
static void mov_fix_index(MOVContext *mov, AVStream *st)
Fix ffstream(st)->index_entries, so that it contains only the entries (and the entries which are need...
Definition: mov.c:4371
ff_isom_parse_dvcc_dvvc
int ff_isom_parse_dvcc_dvvc(void *logctx, AVStream *st, const uint8_t *buf_ptr, uint64_t size)
Definition: dovi_isom.c:32
mov_read_pssh
static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8017
MOVDref::volume
char volume[28]
Definition: isom.h:89
mov_read_stsd
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3316
internal.h
AV_SPHERICAL_FISHEYE
@ AV_SPHERICAL_FISHEYE
Fisheye projection (Apple).
Definition: spherical.h:84
AVCodecParameters::height
int height
The height of the video frame in pixels.
Definition: codec_par.h:150
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:253
MOVStreamContext::stps_count
unsigned int stps_count
Definition: isom.h:203
AVCodecParameters::block_align
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition: codec_par.h:221
AV_CODEC_ID_TTML
@ AV_CODEC_ID_TTML
Definition: codec_id.h:598
rb_size
static int rb_size(AVIOContext *pb, int64_t *value, int size)
Definition: mov.c:9087
AVStereo3DPrimaryEye
AVStereo3DPrimaryEye
List of possible primary eyes.
Definition: stereo3d.h:174
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:139
display.h
ff_mov_read_stsd_entries
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:3215
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:215
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:271
MOVStreamContext::cenc
struct MOVStreamContext::@481 cenc
AV_STEREO3D_TOPBOTTOM
@ AV_STEREO3D_TOPBOTTOM
Views are on top of each other.
Definition: stereo3d.h:76
MOVFragment::duration
unsigned duration
Definition: isom.h:114
AVIAMFMixPresentation
Information on how to render and mix one or more AVIAMFAudioElement to generate the final audio outpu...
Definition: iamf.h:616
ff_id3v1_genre_str
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
MOVStreamContext::sample_sizes
unsigned int * sample_sizes
Definition: isom.h:212
MOVContext::frag_index
MOVFragmentIndex frag_index
Definition: isom.h:364
mov_read_vpcc
static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6546
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:361
AVStreamGroupLayeredVideo::width
int width
Width of the final stream for presentation.
Definition: avformat.h:1095
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
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_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:361
MOVStreamContext::dref_id
int dref_id
Definition: isom.h:230
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:1854
mov_finalize_stsd_codec
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:3064
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:175
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:348
mov_read_mdcv
static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6637
AV_TIMECODE_FLAG_ALLOWNEGATIVE
@ AV_TIMECODE_FLAG_ALLOWNEGATIVE
negative time values are allowed
Definition: timecode.h:38
mov_read_mdat
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1421
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
demux.h
AV_DISPOSITION_DEPENDENT
#define AV_DISPOSITION_DEPENDENT
The stream is intended to be mixed with another stream before presentation.
Definition: avformat.h:708
MOVStreamContext::pb
AVIOContext * pb
Definition: isom.h:180
mov_read_keys
static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5468
mov_read_iref_cdsc
static int mov_read_iref_cdsc(MOVContext *c, AVIOContext *pb, uint32_t type, int version)
Definition: mov.c:9428
AVCodecParameters::color_range
enum AVColorRange color_range
Additional colorspace characteristics.
Definition: codec_par.h:189
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:185
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
exif.h
MOVFragment::size
unsigned size
Definition: isom.h:115
MOV_TFHD_DEFAULT_SIZE
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:412
MOVTimeToSample::count
unsigned int count
Definition: isom.h:58
ff_get_wav_header
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:141
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:703
fix_stream_ids
static void fix_stream_ids(AVFormatContext *s)
Definition: mov.c:11179
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:4822
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:83
mov_read_svq3
static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2441
mov_find_tref_tag
static MovTref * mov_find_tref_tag(const MOVStreamContext *sc, uint32_t name)
Definition: mov.c:2598
AVSHA
hash context
Definition: sha.c:35
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
MOVFragmentStreamInfo::tfdt_dts
int64_t tfdt_dts
Definition: isom.h:149
AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
@ AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
Definition: iamf.h:349
AVCodecParameters::field_order
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition: codec_par.h:182
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:98
mov_read_iinf
static int mov_read_iinf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9291
ff_read_string_to_bprint_overwrite
int64_t ff_read_string_to_bprint_overwrite(AVIOContext *s, struct AVBPrint *bp, int64_t max_len)
Read a whole null-terminated string of text from AVIOContext to an AVBPrint buffer overwriting its co...
Definition: aviobuf.c:863
AVStreamGroupTileGrid::horizontal
int horizontal
Offset in pixels from the left edge of the canvas where the tile should be placed.
Definition: avformat.h:998
get_stream_info_time
static int64_t get_stream_info_time(MOVFragmentStreamInfo *frag_stream_info)
Definition: mov.c:1716
MP4TrackKindValueMapping
Definition: isom.h:488
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:4222
AV_WB8
#define AV_WB8(p, d)
Definition: intreadwrite.h:392
MOVStreamContext::chunk_count
unsigned int chunk_count
Definition: isom.h:186
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
MOVStreamContext::data_size
int64_t data_size
Definition: isom.h:240
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
MOVStreamContext::ambient
AVAmbientViewingEnvironment * ambient
Definition: isom.h:276
AVCodecParameters::avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:107
mov_read_tmcd
static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6518
mov_chan.h
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:816
MOVStreamContext::ambient_size
size_t ambient_size
Definition: isom.h:277
tag
uint32_t tag
Definition: movenc.c:2054
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:759
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:747
AV_CODEC_ID_APV
@ AV_CODEC_ID_APV
Definition: codec_id.h:332
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:204
HEIFItem::extent_length
int64_t extent_length
Definition: isom.h:305
MOVEncryptionIndex::nb_encrypted_samples
unsigned int nb_encrypted_samples
Definition: isom.h:135
mov_read_senc
static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7720
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
AVStreamGroupLayeredVideo::height
int height
Height of the final image for presentation.
Definition: avformat.h:1099
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
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:81
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:95
MOVStreamContext::stts_data
MOVStts * stts_data
Definition: isom.h:193
AVSphericalMapping::pitch
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:145
AVStreamGroup::metadata
AVDictionary * metadata
Metadata that applies to the whole group.
Definition: avformat.h:1195
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:749
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:207
MOVSbgp::index
unsigned int index
Definition: isom.h:129
HEIFItem::icc_profile_size
size_t icc_profile_size
Definition: isom.h:315
MOVContext::chapter_tracks
int * chapter_tracks
Definition: isom.h:347
cdt2
static const int16_t cdt2[8]
Definition: truemotion1data.h:43
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:593
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
dovi_meta.h
MOVFragment::implicit_offset
uint64_t implicit_offset
Definition: isom.h:112
dict.h
av_packet_side_data_new
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition: packet.c:696
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:244
mov_read_dmlp
static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8749
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
MOVStreamContext::pseudo_stream_id
int pseudo_stream_id
-1 means demux all ids
Definition: isom.h:226
MOVContext::time_scale
int time_scale
Definition: isom.h:329
id
enum AVCodecID id
Definition: dts2pts.c:578
mov_read_tfdt
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5885
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:139
heif_add_stream
static int heif_add_stream(MOVContext *c, HEIFItem *item)
Definition: mov.c:5601
search_frag_moof_offset
static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
Definition: mov.c:1692
MOVFragment
Definition: isom.h:107
AV_DICT_MATCH_CASE
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:74
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
mov_switch_root
static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
Definition: mov.c:11484
MOVContext::use_mfra_for
int use_mfra_for
Definition: isom.h:361
AVEncryptionInfo
This describes encryption info for a packet.
Definition: encryption_info.h:43
MOVStreamContext::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:287
MIN_DATA_ENTRY_BOX_SIZE
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:652
AVStreamGroup
Definition: avformat.h:1140
av_get_media_type_string
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:28
IAMFMixPresentation
Definition: iamf.h:107
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:753
avpriv_dv_init_demux
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:728
mov_seek_fragment
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
Definition: mov.c:11858
ff_configure_buffers_for_index
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: seek.c:175
mov_parse_stsd_video
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2817
ff_codec_bmp_tags
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:36
MOVStreamContext::h_spacing
int h_spacing
pasp hSpacing
Definition: isom.h:235
mov_read_dec3
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1126
get_frag_time
static int64_t get_frag_time(AVFormatContext *s, AVStream *dst_st, MOVFragmentIndex *frag_index, int index)
Definition: mov.c:1726
MOVStreamContext::sample_size
unsigned int sample_size
may contain value calculated from stsd or value from stsz atom
Definition: isom.h:209
HEVC_NAL_CRA_NUT
@ HEVC_NAL_CRA_NUT
Definition: hevc.h:50
AVStreamGroup::nb_streams
unsigned int nb_streams
Number of elements in AVStreamGroup.streams.
Definition: avformat.h:1202
mlp_samplerate
static int mlp_samplerate(int in)
Definition: mlp_parse.h:87
channel_layout.h
MOVStreamContext::duration_for_fps
int64_t duration_for_fps
Definition: isom.h:258
ISOM_DVCC_DVVC_SIZE
#define ISOM_DVCC_DVVC_SIZE
Definition: dovi_isom.h:29
mov_read_sbgp
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3978
MOVFragment::moof_offset
uint64_t moof_offset
Definition: isom.h:111
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: packet.c:231
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:2503
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:7378
MOVTrackExt::size
unsigned size
Definition: isom.h:123
AVDOVIDecoderConfigurationRecord::bl_present_flag
uint8_t bl_present_flag
Definition: dovi_meta.h:62
ff_remove_stream_group
void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg)
Remove a stream group from its AVFormatContext and free it.
Definition: avformat.c:129
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
MOVContext::dv_fctx
AVFormatContext * dv_fctx
Definition: isom.h:340
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:443
AV_CODEC_ID_DVAUDIO
@ AV_CODEC_ID_DVAUDIO
Definition: codec_id.h:467
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:149
MOVContext::aax_mode
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:366
mov_read_sv3d
static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6900
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:615
mov_aaxc_crypto
static int mov_aaxc_crypto(MOVContext *c)
Definition: mov.c:1532
mov_get_skip_samples
static int64_t mov_get_skip_samples(AVStream *st, int sample)
Definition: mov.c:11996
MOVFragmentIndex
Definition: isom.h:166
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
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:881
MOVStreamContext::tts_count
unsigned int tts_count
Definition: isom.h:188
MOVStreamContext::track_end
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:243
MOVStreamContext::sgpd_sync_count
uint32_t sgpd_sync_count
Definition: isom.h:250
MOVContext::fragment
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:342
AVDOVIDecoderConfigurationRecord::rpu_present_flag
uint8_t rpu_present_flag
Definition: dovi_meta.h:60
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:602
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:238
mov_metadata_int8_bypass_padding
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:112
AVDOVIDecoderConfigurationRecord::el_present_flag
uint8_t el_present_flag
Definition: dovi_meta.h:61
MOVDref::type
uint32_t type
Definition: isom.h:86
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:861
mov_read_covr
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:232
MOVParseTableEntry::type
uint32_t type
Definition: mov.c:84
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
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:285
ff_codec_movsubtitle_tags
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:75
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVPacket::stream_index
int stream_index
Definition: packet.h:605
MOVFragmentIndexItem::nb_stream_info
int nb_stream_info
Definition: isom.h:162
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
export_orphan_timecode
static void export_orphan_timecode(AVFormatContext *s)
Definition: mov.c:10374
mov_read_sbas
static int mov_read_sbas(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2626
MOVStreamContext::has_sidx
int has_sidx
Definition: isom.h:281
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:176
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:102
mov_metadata_gnre
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:135
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:252
av_realloc
#define av_realloc(p, s)
Definition: ops_asmgen.c:46
MOV_MERGE_CTTS
#define MOV_MERGE_CTTS
Definition: mov.c:4752
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:184
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:177
mov_read_dpxe
static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2326
AVIAMFSubmix::nb_elements
unsigned int nb_elements
Number of elements in the submix.
Definition: iamf.h:575
MOVFragmentStreamInfo::id
int id
Definition: isom.h:146
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
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:117
av_spherical_alloc
AVSphericalMapping * av_spherical_alloc(size_t *size)
Allocate a AVSphericalVideo structure and initialize its fields to default values.
Definition: spherical.c:26
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:356
mov_read_rtmd_track
static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:10151
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
MOVStreamContext::pb_is_copied
int pb_is_copied
Definition: isom.h:182
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:347
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:113
mov_parse_tiles
static int mov_parse_tiles(AVFormatContext *s)
Definition: mov.c:10776
mem.h
AVStreamGroup::type
enum AVStreamGroupParamsType type
Group type.
Definition: avformat.h:1167
MOVElst::time
int64_t time
Definition: isom.h:81
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
HEIFGrid
Definition: isom.h:318
mov_read_iref_dimg
static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version)
Definition: mov.c:9345
mov_read_pcmc
static int mov_read_pcmc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2048
build_open_gop_key_points
static int build_open_gop_key_points(AVStream *st)
Definition: mov.c:4689
mov_parse_stsd_audio
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2881
IAMFContext::mix_presentations
IAMFMixPresentation ** mix_presentations
Definition: iamf.h:133
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:344
MOVContext::trak_index
int trak_index
Index of the current 'trak'.
Definition: isom.h:336
mov_read_timecode_track
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:10177
HEIFGrid::item
HEIFItem * item
Definition: isom.h:319
AVSphericalMapping::bound_left
uint32_t bound_left
Distance from the left edge.
Definition: spherical.h:185
mov_read_mac_string
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:170
MOVEncryptionIndex::auxiliary_info_sizes
uint8_t * auxiliary_info_sizes
Definition: isom.h:138
MOVFragment::stsd_id
unsigned stsd_id
Definition: isom.h:113
AVCodecParameters::video_delay
int video_delay
Number of delayed frames.
Definition: codec_par.h:200
HEIFGrid::tile_id_list
int16_t * tile_id_list
Definition: isom.h:321
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
MOVStreamContext::tts_data
MOVTimeToSample * tts_data
Definition: isom.h:190
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
avformat_stream_group_create
AVStreamGroup * avformat_stream_group_create(AVFormatContext *s, enum AVStreamGroupParamsType type, AVDictionary **options)
Add a new empty stream group to a media file.
Definition: options.c:472
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
AV_CODEC_ID_PCM_F64LE
@ AV_CODEC_ID_PCM_F64LE
Definition: codec_id.h:362
AVStreamGroupTileGrid::height
int height
Height of the final image for presentation.
Definition: avformat.h:1049
AVStereo3D::view
enum AVStereo3DView view
Determines which views are packed.
Definition: stereo3d.h:217
read_tfra
static int read_tfra(MOVContext *mov, AVIOContext *f)
Definition: mov.c:10392
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVDictionaryEntry
Definition: dict.h:90
mov_read_hvce
static int mov_read_hvce(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8804
av_add_q
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition: rational.c:93
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:57
AV_PKT_DATA_HEVC_CONF
@ AV_PKT_DATA_HEVC_CONF
Dolby Vision enhancement-layer HEVC decoder configuration.
Definition: packet.h:384
AVStereo3DView
AVStereo3DView
List of possible view types.
Definition: stereo3d.h:149
AVPacket
This structure stores compressed data.
Definition: packet.h:580
AVContentLightMetadata::MaxFALL
unsigned MaxFALL
Max average light level per frame (cd/m^2).
Definition: mastering_display_metadata.h:116
mov_parse_lcevc_streams
static int mov_parse_lcevc_streams(AVFormatContext *s)
Definition: mov.c:11052
mov_read_hfov
static int mov_read_hfov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7349
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
MOVStreamContext::stps_data
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:204
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:379
MOVContext::nb_heif_item
int nb_heif_item
Definition: isom.h:388
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:86
riff.h
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:247
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:41
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:623
FFInputFormat
Definition: demux.h:66
MOVContext::decryption_keys
AVDictionary * decryption_keys
Definition: isom.h:393
mov_metadata_loci
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:275
mov_read_eyes
static int mov_read_eyes(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7110
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:520
AV_CHAN_AMBISONIC_BASE
@ AV_CHAN_AMBISONIC_BASE
Range of channels between AV_CHAN_AMBISONIC_BASE and AV_CHAN_AMBISONIC_END represent Ambisonic compon...
Definition: channel_layout.h:108
MOV_TRUN_SAMPLE_SIZE
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:420
MOVStreamContext::tmcd_flags
uint32_t tmcd_flags
tmcd track flags
Definition: isom.h:241
int32_t
int32_t
Definition: audioconvert.c:56
HEIFItem::rotation
int rotation
Definition: isom.h:309
MOVAtom::type
uint32_t type
Definition: isom.h:101
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:231
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:592
parse_timecode_in_framenum_format
static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, int64_t value, int flags)
Definition: mov.c:10137
MOVStreamContext::tmcd_nb_frames
uint8_t tmcd_nb_frames
tmcd number of frames per tick / second
Definition: isom.h:242
AVIAMFSubmixElement
Submix element as defined in section 3.7 of IAMF.
Definition: iamf.h:449
replaygain.h
MOVFragmentIndexItem::headers_read
int headers_read
Definition: isom.h:160
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
MOVStreamContext::start_pad
int start_pad
amount of samples to skip due to enc-dec delay
Definition: isom.h:244
MP4TrackKindValueMapping::value
const char * value
Definition: isom.h:490
AVStereo3DType
AVStereo3DType
List of possible 3D Types.
Definition: stereo3d.h:48
MOVDref::filename
char filename[64]
Definition: isom.h:90
MOVStsc::count
int count
Definition: isom.h:75
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:360
ff_mov_demuxer
const FFInputFormat ff_mov_demuxer
Definition: mov.c:12132
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:99
MOVStts::count
unsigned int count
Definition: isom.h:64
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MOVStreamContext::display_matrix
int32_t * display_matrix
Definition: isom.h:267
MOVContext::heif_grid
HEIFGrid * heif_grid
Definition: isom.h:389
MOVStreamContext::min_sample_duration
uint32_t min_sample_duration
Definition: isom.h:255
MOVStreamContext::current_index
int64_t current_index
Definition: isom.h:220
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_mov_track_kind_table
const struct MP4TrackKindMapping ff_mov_track_kind_table[]
Definition: isom.c:451
get_curr_st
static AVStream * get_curr_st(MOVContext *c)
Get the current stream in the parsing process.
Definition: mov.c:214
MOVStreamContext::stts_allocated_size
unsigned int stts_allocated_size
Definition: isom.h:192
MOVFragmentStreamInfo::index_entry
int index_entry
Definition: isom.h:153
cenc_decrypt
static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8585
MOVStreamContext::format
uint32_t format
Definition: isom.h:279
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:665
MOVStreamContext::sync_group_count
unsigned int sync_group_count
Definition: isom.h:247
MOVContext::bitrates_count
int bitrates_count
Definition: isom.h:359
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:466
AVDictionaryEntry::value
char * value
Definition: dict.h:92
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:796
MOVStreamContext::id
int id
AVStream id.
Definition: isom.h:183
MOVStreamContext::samples_per_frame
unsigned int samples_per_frame
Definition: isom.h:224
MOVStreamContext::tts_allocated_size
unsigned int tts_allocated_size
Definition: isom.h:189
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
MOVElst::duration
int64_t duration
Definition: isom.h:80
ac3tab.h
avstring.h
mov_read_ispe
static int mov_read_ispe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9511
width
#define width
Definition: dsp.h:89
AVAmbientViewingEnvironment::ambient_light_y
AVRational ambient_light_y
Normalized y chromaticity coordinate of the environmental ambient light in the nominal viewing enviro...
Definition: ambient_viewing_environment.h:54
mov_read_iref
static int mov_read_iref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9466
IAMFMixPresentation::mix
AVIAMFMixPresentation * mix
mix backs cmix iff the AVIAMFMixPresentation is owned by this structure.
Definition: iamf.h:113
mov_read_clap
static int mov_read_clap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1290
mov_read_mfra
static int mov_read_mfra(MOVContext *c, AVIOContext *f)
Definition: mov.c:10447
AV_CODEC_ID_FLV1
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:73
mov_metadata_int8_no_padding
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:126
flac.h
AVStreamGroupTileGrid::idx
unsigned int idx
Index of the stream in the group this tile references.
Definition: avformat.h:993
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:1637
IAMFDemuxContext::iamf
IAMFContext iamf
Definition: iamf_reader.h:33
IAMFSubStream::codecpar
AVCodecParameters * codecpar
Definition: iamf.h:86
mov_read_kind
static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8882
MOVContext::heif_item
HEIFItem ** heif_item
Definition: isom.h:387
MOVStreamContext::stts_count
unsigned int stts_count
Definition: isom.h:191
AVStreamGroupLayeredVideo::el_index
unsigned int el_index
Index of the enhancement layer stream in AVStreamGroup.
Definition: avformat.h:1083
MOV_TRUN_SAMPLE_CTS
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:422
HEIFItem::iref_list
HEIFItemRef * iref_list
Definition: isom.h:301
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
MOV_ISMV_TTML_TAG
#define MOV_ISMV_TTML_TAG
Definition: isom.h:483
snprintf
#define snprintf
Definition: snprintf.h:34
IAMFMixPresentation::mix_presentation_id
unsigned int mix_presentation_id
Definition: iamf.h:114
AVCodecParameters::initial_padding
int initial_padding
Number of padding audio samples at the start.
Definition: codec_par.h:239
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:352
mov_read_st3d
static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6772
mov_read_imir
static int mov_read_imir(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9559
is_open_key_sample
static int is_open_key_sample(const MOVStreamContext *sc, int sample)
Definition: mov.c:11877
mov_read_dvc1
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2549
MOVStreamContext::elst_count
unsigned int elst_count
Definition: isom.h:206
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:3823
AVChannelCustom::id
enum AVChannel id
Definition: channel_layout.h:284
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:2263
AV_WL16A
#define AV_WL16A(p, v)
Definition: intreadwrite.h:557
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:144
src
#define src
Definition: vp8dsp.c:248
mov_read_chapters
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:10038
AV_CODEC_ID_DNXHD
@ AV_CODEC_ID_DNXHD
Definition: codec_id.h:151
channel
channel
Definition: ebur128.h:39
MOVStreamContext::default_encrypted_sample
AVEncryptionInfo * default_encrypted_sample
Definition: isom.h:286
duration
static int64_t duration
Definition: ffplay.c:329
MOVContext::cur_item_id
int cur_item_id
Definition: isom.h:386
av_timecode_make_string
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum_arg)
Load timecode string in buf.
Definition: timecode.c:104
AVFMT_EVENT_FLAG_METADATA_UPDATED
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1693
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:77
MOVContext::next_root_atom
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:355
MOVContext::found_iinf
int found_iinf
'iinf' atom has been found
Definition: isom.h:333
MOVContext::meta_keys_count
unsigned meta_keys_count
Definition: isom.h:338
iamf_reader.h
AVStreamGroupTileGrid::background
uint8_t background[4]
The pixel value per channel in RGBA format used if no pixel of any tile is located at a particular pi...
Definition: avformat.h:1013
MOVStreamContext::palette
uint32_t palette[256]
Definition: isom.h:238
cenc_scheme_decrypt
static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8317
MOVFragment::track_id
unsigned track_id
Definition: isom.h:109
mov_read_hdlr
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:797
mov_parse_stsd_data
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:3018
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:184
ff_iamf_read_deinit
void ff_iamf_read_deinit(IAMFDemuxContext *c)
Definition: iamf_reader.c:371
AV_CODEC_ID_PRORES
@ AV_CODEC_ID_PRORES
Definition: codec_id.h:200
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:292
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:347
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:550
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: seek.c:245
mov_read_pitm
static int mov_read_pitm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9106
MOVContext::ignore_chapters
int ignore_chapters
Definition: isom.h:353
MOVTimeToSample::offset
int offset
Definition: isom.h:60
mov_read_dac3
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:868
MP4TrackKindMapping
Definition: isom.h:493
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:237
AVDOVIDecoderConfigurationRecord
Definition: dovi_meta.h:55
HEIFItem::nb_iref_list
int nb_iref_list
Definition: isom.h:302
AV_DISPOSITION_NON_DIEGETIC
#define AV_DISPOSITION_NON_DIEGETIC
The stream is intended to be mixed with a spatial audio track.
Definition: avformat.h:685
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:349
mc
#define mc
Definition: vf_colormatrix.c:100
MOVStreamContext::ffindex
int ffindex
AVStream index.
Definition: isom.h:184
HEIFItem::extent_offset
int64_t extent_offset
Definition: isom.h:306
mov_read_stsz
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3579