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/initial_padding 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 = st->codecpar->initial_padding = 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);
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 = -av_clip64(sc->time_offset, -INT64_MAX, INT64_MAX);
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  av_assert0(sc->dts_shift >= 0);
4901  if (current_dts < INT64_MIN + sc->dts_shift)
4902  return;
4903  current_dts -= sc->dts_shift;
4904  if (!sc->sample_count || sti->nb_index_entries || sc->tts_count)
4905  return;
4906  if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
4907  return;
4908  if (av_reallocp_array(&sti->index_entries,
4909  sti->nb_index_entries + sc->sample_count,
4910  sizeof(*sti->index_entries)) < 0) {
4911  sti->nb_index_entries = 0;
4912  return;
4913  }
4914  sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
4915 
4917  if (ret < 0)
4918  return;
4919 
4920  for (i = 0; i < sc->chunk_count; i++) {
4921  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
4922  current_offset = sc->chunk_offsets[i];
4923  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4924  i + 1 == sc->stsc_data[stsc_index + 1].first)
4925  stsc_index++;
4926 
4927  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
4928  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
4929  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
4930  sc->stsz_sample_size = sc->sample_size;
4931  }
4932  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
4933  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
4934  sc->stsz_sample_size = sc->sample_size;
4935  }
4936 
4937  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
4938  int keyframe = 0;
4939  if (current_sample >= sc->sample_count) {
4940  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
4941  return;
4942  }
4943 
4944  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
4945  keyframe = 1;
4946  if (stss_index + 1 < sc->keyframe_count)
4947  stss_index++;
4948  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
4949  keyframe = 1;
4950  if (stps_index + 1 < sc->stps_count)
4951  stps_index++;
4952  }
4953  if (rap_group_present && rap_group_index < sc->rap_group_count) {
4954  if (sc->rap_group[rap_group_index].index > 0)
4955  keyframe = 1;
4956  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
4957  rap_group_sample = 0;
4958  rap_group_index++;
4959  }
4960  }
4961  if (sc->keyframe_absent
4962  && !sc->stps_count
4963  && !rap_group_present
4964  && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
4965  keyframe = 1;
4966  if (keyframe)
4967  distance = 0;
4968  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
4969  if (current_offset > INT64_MAX - sample_size) {
4970  av_log(mov->fc, AV_LOG_ERROR, "Current offset %"PRId64" or sample size %u is too large\n",
4971  current_offset,
4972  sample_size);
4973  return;
4974  }
4975 
4976  if (sc->pseudo_stream_id == -1 ||
4977  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
4978  AVIndexEntry *e;
4979  if (sample_size > 0x3FFFFFFF) {
4980  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
4981  return;
4982  }
4983  e = &sti->index_entries[sti->nb_index_entries++];
4984  e->pos = current_offset;
4985  e->timestamp = current_dts;
4986  e->size = sample_size;
4987  e->min_distance = distance;
4988  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
4989  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
4990  "size %u, distance %u, keyframe %d\n", st->index, current_sample,
4991  current_offset, current_dts, sample_size, distance, keyframe);
4992  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries < 100)
4993  ff_rfps_add_frame(mov->fc, st, current_dts);
4994  }
4995 
4996  current_offset += sample_size;
4997  stream_size += sample_size;
4998 
4999  if (current_dts > INT64_MAX - sc->tts_data[stts_index].duration)
5000  return;
5001  current_dts += sc->tts_data[stts_index].duration;
5002 
5003  distance++;
5004  stts_sample++;
5005  current_sample++;
5006  if (stts_index + 1 < sc->tts_count && stts_sample == sc->tts_data[stts_index].count) {
5007  stts_sample = 0;
5008  stts_index++;
5009  }
5010  }
5011  }
5012  if (st->duration > 0)
5013  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
5014  } else {
5015  unsigned chunk_samples, total = 0;
5016 
5017  if (!sc->chunk_count || sc->tts_count)
5018  return;
5019 
5020  // compute total chunk count
5021  for (i = 0; i < sc->stsc_count; i++) {
5022  unsigned count, chunk_count;
5023 
5024  chunk_samples = sc->stsc_data[i].count;
5025  if (i != sc->stsc_count - 1 &&
5026  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
5027  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
5028  return;
5029  }
5030 
5031  if (sc->samples_per_frame >= 160) { // gsm
5032  count = chunk_samples / sc->samples_per_frame;
5033  } else if (sc->samples_per_frame > 1) {
5034  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
5035  count = (chunk_samples+samples-1) / samples;
5036  } else {
5037  count = (chunk_samples+1023) / 1024;
5038  }
5039 
5040  if (mov_stsc_index_valid(i, sc->stsc_count))
5041  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
5042  else
5043  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
5044  total += chunk_count * count;
5045  }
5046 
5047  av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
5048  if (total >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
5049  return;
5050  if (av_reallocp_array(&sti->index_entries,
5051  sti->nb_index_entries + total,
5052  sizeof(*sti->index_entries)) < 0) {
5053  sti->nb_index_entries = 0;
5054  return;
5055  }
5056  sti->index_entries_allocated_size = (sti->nb_index_entries + total) * sizeof(*sti->index_entries);
5057 
5058  // populate index
5059  for (i = 0; i < sc->chunk_count; i++) {
5060  current_offset = sc->chunk_offsets[i];
5061  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
5062  i + 1 == sc->stsc_data[stsc_index + 1].first)
5063  stsc_index++;
5064  chunk_samples = sc->stsc_data[stsc_index].count;
5065 
5066  while (chunk_samples > 0) {
5067  AVIndexEntry *e;
5068  unsigned size, samples;
5069 
5070  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
5072  "Zero bytes per frame, but %d samples per frame",
5073  sc->samples_per_frame);
5074  return;
5075  }
5076 
5077  if (sc->samples_per_frame >= 160) { // gsm
5078  samples = sc->samples_per_frame;
5079  size = sc->bytes_per_frame;
5080  } else {
5081  if (sc->samples_per_frame > 1) {
5082  samples = FFMIN((1024 / sc->samples_per_frame)*
5083  sc->samples_per_frame, chunk_samples);
5085  } else {
5086  samples = FFMIN(1024, chunk_samples);
5087  size = samples * sc->sample_size;
5088  }
5089  }
5090 
5091  if (sti->nb_index_entries >= total) {
5092  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
5093  return;
5094  }
5095  if (size > 0x3FFFFFFF) {
5096  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
5097  return;
5098  }
5099  e = &sti->index_entries[sti->nb_index_entries++];
5100  e->pos = current_offset;
5101  e->timestamp = current_dts;
5102  e->size = size;
5103  e->min_distance = 0;
5104  e->flags = AVINDEX_KEYFRAME;
5105  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
5106  "size %u, duration %u\n", st->index, i, current_offset, current_dts,
5107  size, samples);
5108 
5109  current_offset += size;
5110  if (current_dts > INT64_MAX - samples)
5111  return;
5112  current_dts += samples;
5113  chunk_samples -= samples;
5114  }
5115  }
5116 
5118  if (ret < 0)
5119  return;
5120  }
5121 
5122  if (!mov->ignore_editlist && mov->advanced_editlist) {
5123  // Fix index according to edit lists.
5124  mov_fix_index(mov, st);
5125  }
5126 
5127  // Update start time of the stream.
5130  if (sc->tts_data) {
5131  st->start_time = av_sat_add64(st->start_time, sc->tts_data[0].offset);
5132  }
5133  }
5134 
5135  mov_estimate_video_delay(mov, st);
5136 }
5137 
5138 static int test_same_origin(const char *src, const char *ref) {
5139  char src_proto[64];
5140  char ref_proto[64];
5141  char src_auth[256];
5142  char ref_auth[256];
5143  char src_host[256];
5144  char ref_host[256];
5145  int src_port=-1;
5146  int ref_port=-1;
5147 
5148  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
5149  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
5150 
5151  if (strlen(src) == 0) {
5152  return -1;
5153  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
5154  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
5155  strlen(src_host) + 1 >= sizeof(src_host) ||
5156  strlen(ref_host) + 1 >= sizeof(ref_host)) {
5157  return 0;
5158  } else if (strcmp(src_proto, ref_proto) ||
5159  strcmp(src_auth, ref_auth) ||
5160  strcmp(src_host, ref_host) ||
5161  src_port != ref_port) {
5162  return 0;
5163  } else
5164  return 1;
5165 }
5166 
5167 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
5168 {
5169  /* try relative path, we do not try the absolute because it can leak information about our
5170  system to an attacker */
5171  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
5172  char filename[1025];
5173  const char *src_path;
5174  int i, l;
5175 
5176  /* find a source dir */
5177  src_path = strrchr(src, '/');
5178  if (src_path)
5179  src_path++;
5180  else
5181  src_path = src;
5182 
5183  /* find a next level down to target */
5184  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
5185  if (ref->path[l] == '/') {
5186  if (i == ref->nlvl_to - 1)
5187  break;
5188  else
5189  i++;
5190  }
5191 
5192  /* compose filename if next level down to target was found */
5193  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
5194  memcpy(filename, src, src_path - src);
5195  filename[src_path - src] = 0;
5196 
5197  for (i = 1; i < ref->nlvl_from; i++)
5198  av_strlcat(filename, "../", sizeof(filename));
5199 
5200  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
5201  if (!c->use_absolute_path) {
5202  int same_origin = test_same_origin(src, filename);
5203 
5204  if (!same_origin) {
5205  av_log(c->fc, AV_LOG_ERROR,
5206  "Reference with mismatching origin, %s not tried for security reasons, "
5207  "set demuxer option use_absolute_path to allow it anyway\n",
5208  ref->path);
5209  return AVERROR(ENOENT);
5210  }
5211 
5212  if (strstr(ref->path + l + 1, "..") ||
5213  strstr(ref->path + l + 1, ":") ||
5214  (ref->nlvl_from > 1 && same_origin < 0) ||
5215  (filename[0] == '/' && src_path == src))
5216  return AVERROR(ENOENT);
5217  }
5218 
5219  if (strlen(filename) + 1 == sizeof(filename))
5220  return AVERROR(ENOENT);
5221  if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
5222  return 0;
5223  }
5224  } else if (c->use_absolute_path) {
5225  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
5226  "this is a possible security issue\n");
5227  if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
5228  return 0;
5229  } else {
5230  av_log(c->fc, AV_LOG_ERROR,
5231  "Absolute path %s not tried for security reasons, "
5232  "set demuxer option use_absolute_path to allow absolute paths\n",
5233  ref->path);
5234  }
5235 
5236  return AVERROR(ENOENT);
5237 }
5238 
5240 {
5241  if (sc->time_scale <= 0) {
5242  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
5243  sc->time_scale = c->time_scale;
5244  if (sc->time_scale <= 0)
5245  sc->time_scale = 1;
5246  }
5247 }
5248 
5249 #if CONFIG_IAMFDEC
5250 static int mov_update_iamf_streams(MOVContext *c, const AVStream *st)
5251 {
5252  const MOVStreamContext *sc = st->priv_data;
5253  const IAMFContext *iamf = &sc->iamf->iamf;
5254 
5255  for (int i = 0; i < iamf->nb_audio_elements; i++) {
5256  const AVStreamGroup *stg = NULL;
5257 
5258  for (int j = 0; j < c->fc->nb_stream_groups; j++)
5259  if (c->fc->stream_groups[j]->id == iamf->audio_elements[i]->audio_element_id)
5260  stg = c->fc->stream_groups[j];
5261  av_assert0(stg);
5262 
5263  for (int j = 0; j < stg->nb_streams; j++) {
5264  const FFStream *sti = cffstream(st);
5265  AVStream *out = stg->streams[j];
5266  FFStream *out_sti = ffstream(stg->streams[j]);
5267 
5268  out->codecpar->bit_rate = 0;
5269 
5270  if (out == st)
5271  continue;
5272 
5273  out->time_base = st->time_base;
5274  out->start_time = st->start_time;
5275  out->duration = st->duration;
5276  out->nb_frames = st->nb_frames;
5277  out->discard = st->discard;
5278 
5279  av_assert0(!out_sti->index_entries);
5281  if (!out_sti->index_entries)
5282  return AVERROR(ENOMEM);
5283 
5285  out_sti->nb_index_entries = sti->nb_index_entries;
5286  out_sti->skip_samples = sti->skip_samples;
5287  memcpy(out_sti->index_entries, sti->index_entries, sti->index_entries_allocated_size);
5288  }
5289  }
5290 
5291  return 0;
5292 }
5293 #endif
5294 
5295 static int sanity_checks(void *log_obj, MOVStreamContext *sc, int index)
5296 {
5297  if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
5298  (!sc->sample_size && !sc->sample_count))) ||
5299  (sc->sample_count && (!sc->chunk_count ||
5300  (!sc->sample_size && !sc->sample_sizes)))) {
5301  av_log(log_obj, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
5302  index);
5303  return 1;
5304  }
5305 
5306  if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
5307  av_log(log_obj, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
5308  index);
5309  return 2;
5310  }
5311  return 0;
5312 }
5313 
5315 {
5316  AVStream *st;
5317  MOVStreamContext *sc;
5318  int ret;
5319 
5320  st = avformat_new_stream(c->fc, NULL);
5321  if (!st) return AVERROR(ENOMEM);
5322  st->id = -1;
5323  sc = av_mallocz(sizeof(MOVStreamContext));
5324  if (!sc) return AVERROR(ENOMEM);
5325 
5326  st->priv_data = sc;
5328  sc->ffindex = st->index;
5329  c->trak_index = st->index;
5330  sc->refcount = 1;
5331 
5332  if ((ret = mov_read_default(c, pb, atom)) < 0)
5333  return ret;
5334 
5335  c->trak_index = -1;
5336 
5337  // Here stsc refers to a chunk not described in stco. This is technically invalid,
5338  // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
5339  if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
5340  sc->stsc_count = 0;
5341  av_freep(&sc->stsc_data);
5342  }
5343 
5344  ret = sanity_checks(c->fc, sc, st->index);
5345  if (ret)
5346  return ret > 1 ? AVERROR_INVALIDDATA : 0;
5347 
5348  fix_timescale(c, sc);
5349 
5350  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
5351 
5352  /*
5353  * Advanced edit list support does not work with fragemented MP4s, which
5354  * have stsc, stsz, stco, and stts with zero entries in the moov atom.
5355  * In these files, trun atoms may be streamed in.
5356  */
5357  if (!sc->stts_count && c->advanced_editlist) {
5358 
5359  av_log(c->fc, AV_LOG_VERBOSE, "advanced_editlist does not work with fragmented "
5360  "MP4. disabling.\n");
5361  c->advanced_editlist = 0;
5362  c->advanced_editlist_autodisabled = 1;
5363  }
5364 
5365  mov_build_index(c, st);
5366 
5367 #if CONFIG_IAMFDEC
5368  if (sc->iamf) {
5369  ret = mov_update_iamf_streams(c, st);
5370  if (ret < 0)
5371  return ret;
5372  }
5373 #endif
5374 
5375  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
5376  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
5377  if (c->enable_drefs) {
5378  if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
5379  av_log(c->fc, AV_LOG_ERROR,
5380  "stream %d, error opening alias: path='%s', dir='%s', "
5381  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
5382  st->index, dref->path, dref->dir, dref->filename,
5383  dref->volume, dref->nlvl_from, dref->nlvl_to);
5384  } else {
5385  av_log(c->fc, AV_LOG_WARNING,
5386  "Skipped opening external track: "
5387  "stream %d, alias: path='%s', dir='%s', "
5388  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
5389  "Set enable_drefs to allow this.\n",
5390  st->index, dref->path, dref->dir, dref->filename,
5391  dref->volume, dref->nlvl_from, dref->nlvl_to);
5392  }
5393  } else {
5394  sc->pb = c->fc->pb;
5395  sc->pb_is_copied = 1;
5396  }
5397 
5398  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
5399  int stts_constant = sc->stts_count && sc->tts_count;
5400  if (sc->h_spacing && sc->v_spacing)
5402  sc->h_spacing, sc->v_spacing, INT_MAX);
5403  if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
5404  sc->height && sc->width &&
5405  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
5407  (int64_t)st->codecpar->height * sc->width,
5408  (int64_t)st->codecpar->width * sc->height, INT_MAX);
5409  }
5410 
5411 #if FF_API_R_FRAME_RATE
5412  for (unsigned int i = 1; sc->stts_count && i + 1 < sc->tts_count; i++) {
5413  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5414  continue;
5415  stts_constant = 0;
5416  }
5417  if (stts_constant)
5419  sc->time_scale, sc->tts_data[0].duration, INT_MAX);
5420 #endif
5421  }
5422 
5423 #if CONFIG_H261_DECODER || CONFIG_H263_DECODER || CONFIG_MPEG4_DECODER
5424  switch (st->codecpar->codec_id) {
5425 #if CONFIG_H261_DECODER
5426  case AV_CODEC_ID_H261:
5427 #endif
5428 #if CONFIG_H263_DECODER
5429  case AV_CODEC_ID_H263:
5430 #endif
5431 #if CONFIG_MPEG4_DECODER
5432  case AV_CODEC_ID_MPEG4:
5433 #endif
5434  st->codecpar->width = 0; /* let decoder init width/height */
5435  st->codecpar->height= 0;
5436  break;
5437  }
5438 #endif
5439 
5440  // If the duration of the mp3 packets is not constant, then they could need a parser
5441  if (st->codecpar->codec_id == AV_CODEC_ID_MP3
5442  && sc->time_scale == st->codecpar->sample_rate) {
5443  int stts_constant = 1;
5444  for (int i = 1; sc->stts_count && i < sc->tts_count; i++) {
5445  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5446  continue;
5447  stts_constant = 0;
5448  }
5449  if (!stts_constant)
5451  }
5452  /* Do not need those anymore. */
5453  av_freep(&sc->chunk_offsets);
5454  av_freep(&sc->sample_sizes);
5455  av_freep(&sc->keyframes);
5456  av_freep(&sc->stps_data);
5457  av_freep(&sc->elst_data);
5458  av_freep(&sc->rap_group);
5459  av_freep(&sc->sync_group);
5460  av_freep(&sc->sgpd_sync);
5461 
5462  return 0;
5463 }
5464 
5466 {
5467  int ret;
5468  c->itunes_metadata = 1;
5469  ret = mov_read_default(c, pb, atom);
5470  c->itunes_metadata = 0;
5471  return ret;
5472 }
5473 
5475 {
5476  uint32_t count;
5477  uint32_t i;
5478 
5479  if (atom.size < 8)
5480  return 0;
5481 
5482  avio_skip(pb, 4);
5483  count = avio_rb32(pb);
5484  atom.size -= 8;
5485  if (count >= UINT_MAX / sizeof(*c->meta_keys)) {
5486  av_log(c->fc, AV_LOG_ERROR,
5487  "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
5488  return AVERROR_INVALIDDATA;
5489  }
5490 
5491  c->meta_keys_count = count + 1;
5492  c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
5493  if (!c->meta_keys)
5494  return AVERROR(ENOMEM);
5495 
5496  for (i = 1; i <= count; ++i) {
5497  uint32_t key_size = avio_rb32(pb);
5498  uint32_t type = avio_rl32(pb);
5499  if (key_size < 8 || key_size > atom.size) {
5500  av_log(c->fc, AV_LOG_ERROR,
5501  "The key# %"PRIu32" in meta has invalid size:"
5502  "%"PRIu32"\n", i, key_size);
5503  return AVERROR_INVALIDDATA;
5504  }
5505  atom.size -= key_size;
5506  key_size -= 8;
5507  if (type != MKTAG('m','d','t','a')) {
5508  avio_skip(pb, key_size);
5509  continue;
5510  }
5511  c->meta_keys[i] = av_mallocz(key_size + 1);
5512  if (!c->meta_keys[i])
5513  return AVERROR(ENOMEM);
5514  avio_read(pb, c->meta_keys[i], key_size);
5515  }
5516 
5517  return 0;
5518 }
5519 
5521 {
5522  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
5523  uint8_t *key = NULL, *val = NULL, *mean = NULL;
5524  int i;
5525  int ret = 0;
5526  AVStream *st;
5527 
5528  if (c->fc->nb_streams < 1)
5529  return 0;
5530  st = c->fc->streams[c->fc->nb_streams-1];
5531 
5532  for (i = 0; i < 3; i++) {
5533  uint8_t **p;
5534  uint32_t len, tag;
5535 
5536  if (end - avio_tell(pb) <= 12)
5537  break;
5538 
5539  len = avio_rb32(pb);
5540  tag = avio_rl32(pb);
5541  avio_skip(pb, 4); // flags
5542 
5543  if (len < 12 || len - 12 > end - avio_tell(pb))
5544  break;
5545  len -= 12;
5546 
5547  if (tag == MKTAG('m', 'e', 'a', 'n'))
5548  p = &mean;
5549  else if (tag == MKTAG('n', 'a', 'm', 'e'))
5550  p = &key;
5551  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
5552  avio_skip(pb, 4);
5553  len -= 4;
5554  p = &val;
5555  } else
5556  break;
5557 
5558  if (*p)
5559  break;
5560 
5561  *p = av_malloc(len + 1);
5562  if (!*p) {
5563  ret = AVERROR(ENOMEM);
5564  break;
5565  }
5566  ret = ffio_read_size(pb, *p, len);
5567  if (ret < 0) {
5568  av_freep(p);
5569  break;
5570  }
5571  (*p)[len] = 0;
5572  }
5573 
5574  if (mean && key && val) {
5575  if (strcmp(key, "iTunSMPB") == 0) {
5576  int priming, remainder, samples;
5577  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
5578  if(priming>0 && priming<16384)
5579  st->codecpar->initial_padding = priming = av_rescale_q(priming, st->time_base,
5580  (AVRational){ 1, st->codecpar->sample_rate });
5581  if (remainder > 0) {
5583  (AVRational){ 1, st->codecpar->sample_rate });
5584  remainder = av_rescale_q(remainder, st->time_base,
5585  (AVRational){ 1, st->codecpar->sample_rate });
5587  (AVRational){ 1, st->codecpar->sample_rate });
5588  if (duration > remainder && duration > samples) {
5589  ffstream(st)->first_discard_sample = duration - remainder;
5591  }
5592  }
5593  av_log(c->fc, AV_LOG_DEBUG, "Parsed iTunSMPB: priming %d, remainder %d samples %d\n",
5594  priming, remainder, samples);
5595  }
5596  }
5597  if (strcmp(mean, "com.apple.iTunes") == 0 &&
5598  strcmp(key, "DISCSUBTITLE") == 0) {
5599  av_dict_set(&c->fc->metadata, "disc_subtitle", val,
5601  val = NULL;
5602  }
5603  if (strcmp(key, "cdec") != 0) {
5604  av_dict_set(&c->fc->metadata, key, val,
5606  key = val = NULL;
5607  }
5608  } else {
5609  av_log(c->fc, AV_LOG_VERBOSE,
5610  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
5611  }
5612 
5613  avio_seek(pb, end, SEEK_SET);
5614  av_freep(&key);
5615  av_freep(&val);
5616  av_freep(&mean);
5617  return ret;
5618 }
5619 
5621 {
5622  MOVStreamContext *sc;
5623  AVStream *st;
5624 
5625  st = avformat_new_stream(c->fc, NULL);
5626  if (!st)
5627  return AVERROR(ENOMEM);
5628  sc = av_mallocz(sizeof(MOVStreamContext));
5629  if (!sc)
5630  goto fail;
5631 
5632  item->st = st;
5633  st->id = item->item_id;
5634  st->priv_data = sc;
5636  st->codecpar->codec_id = mov_codec_id(st, item->type);
5637  sc->id = st->id;
5638  sc->ffindex = st->index;
5639  st->avg_frame_rate.num = st->avg_frame_rate.den = 1;
5640  st->time_base.num = st->time_base.den = 1;
5641  st->nb_frames = 1;
5642  sc->time_scale = 1;
5643  sc->pb = c->fc->pb;
5644  sc->pb_is_copied = 1;
5645  sc->refcount = 1;
5646 
5647  if (item->name)
5648  av_dict_set(&st->metadata, "title", item->name, 0);
5649 
5650  // Populate the necessary fields used by mov_build_index.
5651  sc->stsc_data = av_malloc_array(1, sizeof(*sc->stsc_data));
5652  if (!sc->stsc_data)
5653  goto fail;
5654  sc->stsc_count = 1;
5655  sc->stsc_data[0].first = 1;
5656  sc->stsc_data[0].count = 1;
5657  sc->stsc_data[0].id = 1;
5658  sc->chunk_offsets = av_malloc_array(1, sizeof(*sc->chunk_offsets));
5659  if (!sc->chunk_offsets)
5660  goto fail;
5661  sc->chunk_count = 1;
5662  sc->stts_data = av_malloc_array(1, sizeof(*sc->stts_data));
5663  if (!sc->stts_data)
5664  goto fail;
5665  sc->stts_count = 1;
5666  sc->stts_data[0].count = 1;
5667  // Not used for still images. But needed by mov_build_index.
5668  sc->stts_data[0].duration = 0;
5669 
5670  return 0;
5671 fail:
5672  mov_free_stream_context(c->fc, st);
5673  ff_remove_stream(c->fc, st);
5674  item->st = NULL;
5675 
5676  return AVERROR(ENOMEM);
5677 }
5678 
5680 {
5681  while (atom.size > 8) {
5682  uint32_t tag;
5683  if (avio_feof(pb))
5684  return AVERROR_EOF;
5685  tag = avio_rl32(pb);
5686  atom.size -= 4;
5687  if (tag == MKTAG('h','d','l','r')) {
5688  avio_seek(pb, -8, SEEK_CUR);
5689  atom.size += 8;
5690  return mov_read_default(c, pb, atom);
5691  }
5692  }
5693  return 0;
5694 }
5695 
5696 // return 1 when matrix is identity, 0 otherwise
5697 #define IS_MATRIX_IDENT(matrix) \
5698  ( (matrix)[0][0] == (1 << 16) && \
5699  (matrix)[1][1] == (1 << 16) && \
5700  (matrix)[2][2] == (1 << 30) && \
5701  !(matrix)[0][1] && !(matrix)[0][2] && \
5702  !(matrix)[1][0] && !(matrix)[1][2] && \
5703  !(matrix)[2][0] && !(matrix)[2][1])
5704 
5706 {
5707  int i, j, e;
5708  int width;
5709  int height;
5710  int display_matrix[3][3];
5711  int res_display_matrix[3][3] = { { 0 } };
5712  AVStream *st;
5713  MOVStreamContext *sc;
5714  int version;
5715  int flags;
5716 
5717  if (c->fc->nb_streams < 1)
5718  return 0;
5719  st = c->fc->streams[c->fc->nb_streams-1];
5720  sc = st->priv_data;
5721 
5722  // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
5723  // avoids corrupting AVStreams mapped to an earlier tkhd.
5724  if (st->id != -1)
5725  return AVERROR_INVALIDDATA;
5726 
5727  version = avio_r8(pb);
5728  flags = avio_rb24(pb);
5730 
5731  if (version == 1) {
5732  avio_rb64(pb);
5733  avio_rb64(pb);
5734  } else {
5735  avio_rb32(pb); /* creation time */
5736  avio_rb32(pb); /* modification time */
5737  }
5738  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
5739  sc->id = st->id;
5740  avio_rb32(pb); /* reserved */
5741 
5742  /* highlevel (considering edits) duration in movie timebase */
5743  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
5744  avio_rb32(pb); /* reserved */
5745  avio_rb32(pb); /* reserved */
5746 
5747  avio_rb16(pb); /* layer */
5748  avio_rb16(pb); /* alternate group */
5749  avio_rb16(pb); /* volume */
5750  avio_rb16(pb); /* reserved */
5751 
5752  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
5753  // they're kept in fixed point format through all calculations
5754  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
5755  // side data, but the scale factor is not needed to calculate aspect ratio
5756  for (i = 0; i < 3; i++) {
5757  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
5758  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
5759  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
5760  }
5761 
5762  width = avio_rb32(pb); // 16.16 fixed point track width
5763  height = avio_rb32(pb); // 16.16 fixed point track height
5764  sc->width = width >> 16;
5765  sc->height = height >> 16;
5766 
5767  // apply the moov display matrix (after the tkhd one)
5768  for (i = 0; i < 3; i++) {
5769  const int sh[3] = { 16, 16, 30 };
5770  for (j = 0; j < 3; j++) {
5771  for (e = 0; e < 3; e++) {
5772  res_display_matrix[i][j] +=
5773  ((int64_t) display_matrix[i][e] *
5774  c->movie_display_matrix[e][j]) >> sh[e];
5775  }
5776  }
5777  }
5778 
5779  // save the matrix when it is not the default identity
5780  if (!IS_MATRIX_IDENT(res_display_matrix)) {
5781  av_freep(&sc->display_matrix);
5782  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
5783  if (!sc->display_matrix)
5784  return AVERROR(ENOMEM);
5785 
5786  for (i = 0; i < 3; i++)
5787  for (j = 0; j < 3; j++)
5788  sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
5789  }
5790 
5791  // transform the display width/height according to the matrix
5792  // to keep the same scale, use [width height 1<<16]
5793  if (width && height && sc->display_matrix) {
5794  double disp_transform[2];
5795 
5796  for (i = 0; i < 2; i++)
5797  disp_transform[i] = hypot(sc->display_matrix[0 + i],
5798  sc->display_matrix[3 + i]);
5799 
5800  if (disp_transform[0] > 1 && disp_transform[1] > 1 &&
5801  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
5802  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
5804  disp_transform[0] / disp_transform[1],
5805  INT_MAX);
5806  }
5807  return 0;
5808 }
5809 
5811 {
5812  MOVFragment *frag = &c->fragment;
5813  MOVTrackExt *trex = NULL;
5814  int flags, track_id, i;
5815  MOVFragmentStreamInfo * frag_stream_info;
5816 
5817  avio_r8(pb); /* version */
5818  flags = avio_rb24(pb);
5819 
5820  track_id = avio_rb32(pb);
5821  if (!track_id)
5822  return AVERROR_INVALIDDATA;
5823  for (i = 0; i < c->trex_count; i++)
5824  if (c->trex_data[i].track_id == track_id) {
5825  trex = &c->trex_data[i];
5826  break;
5827  }
5828  if (!trex) {
5829  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
5830  return 0;
5831  }
5832  c->fragment.found_tfhd = 1;
5833  frag->track_id = track_id;
5834  set_frag_stream(&c->frag_index, track_id);
5835 
5838  frag->moof_offset : frag->implicit_offset;
5839  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
5840 
5842  avio_rb32(pb) : trex->duration;
5843  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
5844  avio_rb32(pb) : trex->size;
5845  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
5846  avio_rb32(pb) : trex->flags;
5847  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
5848 
5849  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5850  if (frag_stream_info) {
5851  frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
5852  frag_stream_info->stsd_id = frag->stsd_id;
5853  }
5854  return 0;
5855 }
5856 
5858 {
5859  unsigned i, num;
5860  void *new_tracks;
5861 
5862  num = atom.size / 4;
5863  if (!(new_tracks = av_malloc_array(num, sizeof(int))))
5864  return AVERROR(ENOMEM);
5865 
5866  av_free(c->chapter_tracks);
5867  c->chapter_tracks = new_tracks;
5868  c->nb_chapter_tracks = num;
5869 
5870  for (i = 0; i < num && !pb->eof_reached; i++)
5871  c->chapter_tracks[i] = avio_rb32(pb);
5872 
5873  c->nb_chapter_tracks = i;
5874 
5875  return 0;
5876 }
5877 
5879 {
5880  MOVTrackExt *trex;
5881  int err;
5882 
5883  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
5884  return AVERROR_INVALIDDATA;
5885  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
5886  sizeof(*c->trex_data))) < 0) {
5887  c->trex_count = 0;
5888  return err;
5889  }
5890 
5891  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
5892 
5893  trex = &c->trex_data[c->trex_count++];
5894  avio_r8(pb); /* version */
5895  avio_rb24(pb); /* flags */
5896  trex->track_id = avio_rb32(pb);
5897  trex->stsd_id = avio_rb32(pb);
5898  trex->duration = avio_rb32(pb);
5899  trex->size = avio_rb32(pb);
5900  trex->flags = avio_rb32(pb);
5901  return 0;
5902 }
5903 
5905 {
5906  MOVFragment *frag = &c->fragment;
5907  AVStream *st = NULL;
5908  MOVStreamContext *sc;
5909  int version, i;
5910  MOVFragmentStreamInfo * frag_stream_info;
5911  int64_t base_media_decode_time;
5912 
5913  for (i = 0; i < c->fc->nb_streams; i++) {
5914  sc = c->fc->streams[i]->priv_data;
5915  if (sc->id == frag->track_id) {
5916  st = c->fc->streams[i];
5917  break;
5918  }
5919  }
5920  if (!st) {
5921  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5922  return 0;
5923  }
5924  sc = st->priv_data;
5925  if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5926  return 0;
5927  version = avio_r8(pb);
5928  avio_rb24(pb); /* flags */
5929  if (version) {
5930  base_media_decode_time = avio_rb64(pb);
5931  } else {
5932  base_media_decode_time = avio_rb32(pb);
5933  }
5934 
5935  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5936  if (frag_stream_info)
5937  frag_stream_info->tfdt_dts = base_media_decode_time;
5938  sc->track_end = base_media_decode_time;
5939 
5940  return 0;
5941 }
5942 
5944 {
5945  MOVFragment *frag = &c->fragment;
5946  AVStream *st = NULL;
5947  FFStream *sti = NULL;
5948  MOVStreamContext *sc;
5949  MOVTimeToSample *tts_data;
5950  uint64_t offset;
5951  int64_t dts, pts = AV_NOPTS_VALUE;
5952  int data_offset = 0;
5953  unsigned entries, first_sample_flags = frag->flags;
5954  int flags, distance, i;
5955  int64_t prev_dts = AV_NOPTS_VALUE;
5956  int next_frag_index = -1, index_entry_pos;
5957  size_t requested_size;
5958  size_t old_allocated_size;
5959  AVIndexEntry *new_entries;
5960  MOVFragmentStreamInfo * frag_stream_info;
5961 
5962  if (!frag->found_tfhd) {
5963  av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
5964  return AVERROR_INVALIDDATA;
5965  }
5966 
5967  for (i = 0; i < c->fc->nb_streams; i++) {
5968  sc = c->fc->streams[i]->priv_data;
5969  if (sc->id == frag->track_id) {
5970  st = c->fc->streams[i];
5971  sti = ffstream(st);
5972  break;
5973  }
5974  }
5975  if (!st) {
5976  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5977  return 0;
5978  }
5979  sc = st->priv_data;
5980  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5981  return 0;
5982 
5983  // Find the next frag_index index that has a valid index_entry for
5984  // the current track_id.
5985  //
5986  // A valid index_entry means the trun for the fragment was read
5987  // and it's samples are in index_entries at the given position.
5988  // New index entries will be inserted before the index_entry found.
5989  index_entry_pos = sti->nb_index_entries;
5990  for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
5991  frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
5992  if (frag_stream_info && frag_stream_info->index_entry >= 0) {
5993  next_frag_index = i;
5994  index_entry_pos = frag_stream_info->index_entry;
5995  break;
5996  }
5997  }
5998  av_assert0(index_entry_pos <= sti->nb_index_entries);
5999 
6000  avio_r8(pb); /* version */
6001  flags = avio_rb24(pb);
6002  entries = avio_rb32(pb);
6003  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
6004 
6005  if ((uint64_t)entries+sc->tts_count >= UINT_MAX/sizeof(*sc->tts_data))
6006  return AVERROR_INVALIDDATA;
6007  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
6008  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
6009 
6010  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
6011  if (frag_stream_info) {
6012  if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
6013  dts = frag_stream_info->next_trun_dts - sc->time_offset;
6014  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
6015  c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
6016  pts = frag_stream_info->first_tfra_pts;
6017  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
6018  ", using it for pts\n", pts);
6019  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
6020  c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
6021  dts = frag_stream_info->first_tfra_pts;
6022  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
6023  ", using it for dts\n", pts);
6024  } else {
6025  int has_tfdt = frag_stream_info->tfdt_dts != AV_NOPTS_VALUE;
6026  int has_sidx = frag_stream_info->sidx_pts != AV_NOPTS_VALUE;
6027  int fallback_tfdt = !c->use_tfdt && !has_sidx && has_tfdt;
6028  int fallback_sidx = c->use_tfdt && !has_tfdt && has_sidx;
6029 
6030  if (fallback_sidx) {
6031  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt set but no tfdt found, using sidx instead\n");
6032  }
6033  if (fallback_tfdt) {
6034  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt not set but no sidx found, using tfdt instead\n");
6035  }
6036 
6037  if (has_tfdt && c->use_tfdt || fallback_tfdt) {
6038  dts = frag_stream_info->tfdt_dts - sc->time_offset;
6039  av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
6040  ", using it for dts\n", dts);
6041  } else if (has_sidx && !c->use_tfdt || fallback_sidx) {
6042  // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
6043  // pts = frag_stream_info->sidx_pts;
6044  dts = frag_stream_info->sidx_pts;
6045  av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
6046  ", using it for dts\n", frag_stream_info->sidx_pts);
6047  } else {
6048  dts = sc->track_end - sc->time_offset;
6049  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
6050  ", using it for dts\n", dts);
6051  }
6052  }
6053  } else {
6054  dts = sc->track_end - sc->time_offset;
6055  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
6056  ", using it for dts\n", dts);
6057  }
6058  offset = frag->base_data_offset + data_offset;
6059  distance = 0;
6060  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
6061 
6062  // realloc space for new index entries
6063  if ((uint64_t)sti->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
6064  entries = UINT_MAX / sizeof(AVIndexEntry) - sti->nb_index_entries;
6065  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
6066  }
6067  if (entries == 0)
6068  return 0;
6069 
6070  requested_size = (sti->nb_index_entries + entries) * sizeof(AVIndexEntry);
6071  new_entries = av_fast_realloc(sti->index_entries,
6073  requested_size);
6074  if (!new_entries)
6075  return AVERROR(ENOMEM);
6076  sti->index_entries= new_entries;
6077 
6078  requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->tts_data);
6079  old_allocated_size = sc->tts_allocated_size;
6080  tts_data = av_fast_realloc(sc->tts_data, &sc->tts_allocated_size,
6081  requested_size);
6082  if (!tts_data)
6083  return AVERROR(ENOMEM);
6084  sc->tts_data = tts_data;
6085 
6086  // In case there were samples without time to sample entries, ensure they get
6087  // zero valued entries. This ensures clips which mix boxes with and
6088  // without time to sample entries don't pickup uninitialized data.
6089  memset((uint8_t*)(sc->tts_data) + old_allocated_size, 0,
6090  sc->tts_allocated_size - old_allocated_size);
6091 
6092  if (index_entry_pos < sti->nb_index_entries) {
6093  // Make hole in index_entries and tts_data for new samples
6094  memmove(sti->index_entries + index_entry_pos + entries,
6095  sti->index_entries + index_entry_pos,
6096  sizeof(*sti->index_entries) *
6097  (sti->nb_index_entries - index_entry_pos));
6098  memmove(sc->tts_data + index_entry_pos + entries,
6099  sc->tts_data + index_entry_pos,
6100  sizeof(*sc->tts_data) * (sc->tts_count - index_entry_pos));
6101  if (index_entry_pos < sc->current_sample) {
6102  sc->current_sample += entries;
6103  }
6104  }
6105 
6106  sti->nb_index_entries += entries;
6107  sc->tts_count = sti->nb_index_entries;
6108  sc->stts_count = sti->nb_index_entries;
6109  if (flags & MOV_TRUN_SAMPLE_CTS)
6110  sc->ctts_count = sti->nb_index_entries;
6111 
6112  // Record the index_entry position in frag_index of this fragment
6113  if (frag_stream_info) {
6114  frag_stream_info->index_entry = index_entry_pos;
6115  if (frag_stream_info->index_base < 0)
6116  frag_stream_info->index_base = index_entry_pos;
6117  }
6118 
6119  if (index_entry_pos > 0)
6120  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
6121 
6122  for (i = 0; i < entries && !pb->eof_reached; i++) {
6123  unsigned sample_size = frag->size;
6124  int sample_flags = i ? frag->flags : first_sample_flags;
6125  unsigned sample_duration = frag->duration;
6126  unsigned ctts_duration = 0;
6127  int keyframe = 0;
6128  int index_entry_flags = 0;
6129 
6130  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
6131  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
6132  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
6133  if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
6134 
6135  mov_update_dts_shift(sc, ctts_duration, c->fc);
6136  if (pts != AV_NOPTS_VALUE) {
6137  dts = pts - sc->dts_shift;
6138  if (flags & MOV_TRUN_SAMPLE_CTS) {
6139  dts -= ctts_duration;
6140  } else {
6141  dts -= sc->time_offset;
6142  }
6143  av_log(c->fc, AV_LOG_DEBUG,
6144  "pts %"PRId64" calculated dts %"PRId64
6145  " sc->dts_shift %d ctts.duration %d"
6146  " sc->time_offset %"PRId64
6147  " flags & MOV_TRUN_SAMPLE_CTS %d\n",
6148  pts, dts,
6149  sc->dts_shift, ctts_duration,
6151  pts = AV_NOPTS_VALUE;
6152  }
6153 
6154  keyframe =
6155  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
6157  if (keyframe) {
6158  distance = 0;
6159  index_entry_flags |= AVINDEX_KEYFRAME;
6160  }
6161  // Fragments can overlap in time. Discard overlapping frames after
6162  // decoding.
6163  if (prev_dts >= dts)
6164  index_entry_flags |= AVINDEX_DISCARD_FRAME;
6165 
6166  sti->index_entries[index_entry_pos].pos = offset;
6167  sti->index_entries[index_entry_pos].timestamp = dts;
6168  sti->index_entries[index_entry_pos].size = sample_size;
6169  sti->index_entries[index_entry_pos].min_distance = distance;
6170  sti->index_entries[index_entry_pos].flags = index_entry_flags;
6171 
6172  sc->tts_data[index_entry_pos].count = 1;
6173  sc->tts_data[index_entry_pos].offset = ctts_duration;
6174  sc->tts_data[index_entry_pos].duration = sample_duration;
6175  index_entry_pos++;
6176 
6177  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
6178  "size %u, distance %d, keyframe %d\n", st->index,
6179  index_entry_pos, offset, dts, sample_size, distance, keyframe);
6180  distance++;
6181  if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
6182  return AVERROR_INVALIDDATA;
6183  if (!sample_size)
6184  return AVERROR_INVALIDDATA;
6185  dts += sample_duration;
6186  offset += sample_size;
6187  sc->data_size += sample_size;
6188 
6189  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
6190  1 <= INT_MAX - sc->nb_frames_for_fps
6191  ) {
6192  sc->duration_for_fps += sample_duration;
6193  sc->nb_frames_for_fps ++;
6194  }
6195  }
6196  if (frag_stream_info)
6197  frag_stream_info->next_trun_dts = dts + sc->time_offset;
6198  if (i < entries) {
6199  // EOF found before reading all entries. Fix the hole this would
6200  // leave in index_entries and tts_data
6201  int gap = entries - i;
6202  memmove(sti->index_entries + index_entry_pos,
6203  sti->index_entries + index_entry_pos + gap,
6204  sizeof(*sti->index_entries) *
6205  (sti->nb_index_entries - (index_entry_pos + gap)));
6206  memmove(sc->tts_data + index_entry_pos,
6207  sc->tts_data + index_entry_pos + gap,
6208  sizeof(*sc->tts_data) *
6209  (sc->tts_count - (index_entry_pos + gap)));
6210 
6211  sti->nb_index_entries -= gap;
6212  sc->tts_count -= gap;
6213  if (index_entry_pos < sc->current_sample) {
6214  sc->current_sample -= gap;
6215  }
6216  entries = i;
6217  }
6218 
6219  // The end of this new fragment may overlap in time with the start
6220  // of the next fragment in index_entries. Mark the samples in the next
6221  // fragment that overlap with AVINDEX_DISCARD_FRAME
6222  prev_dts = AV_NOPTS_VALUE;
6223  if (index_entry_pos > 0)
6224  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
6225  for (int i = index_entry_pos; i < sti->nb_index_entries; i++) {
6226  if (prev_dts < sti->index_entries[i].timestamp)
6227  break;
6229  }
6230 
6231  // If a hole was created to insert the new index_entries into,
6232  // the index_entry recorded for all subsequent moof must
6233  // be incremented by the number of entries inserted.
6234  fix_frag_index_entries(&c->frag_index, next_frag_index,
6235  frag->track_id, entries);
6236 
6237  if (pb->eof_reached) {
6238  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
6239  return AVERROR_EOF;
6240  }
6241 
6242  frag->implicit_offset = offset;
6243 
6244  sc->track_end = dts + sc->time_offset;
6245  if (st->duration < sc->track_end)
6246  st->duration = sc->track_end;
6247 
6248  return 0;
6249 }
6250 
6252 {
6253  int64_t stream_size = avio_size(pb);
6254  int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
6255  uint8_t version, is_complete;
6256  int64_t offadd;
6257  unsigned i, j, track_id, item_count;
6258  AVStream *st = NULL;
6259  AVStream *ref_st = NULL;
6260  MOVStreamContext *sc, *ref_sc = NULL;
6261  AVRational timescale;
6262 
6263  version = avio_r8(pb);
6264  if (version > 1) {
6265  avpriv_request_sample(c->fc, "sidx version %u", version);
6266  return 0;
6267  }
6268 
6269  avio_rb24(pb); // flags
6270 
6271  track_id = avio_rb32(pb); // Reference ID
6272  for (i = 0; i < c->fc->nb_streams; i++) {
6273  sc = c->fc->streams[i]->priv_data;
6274  if (sc->id == track_id) {
6275  st = c->fc->streams[i];
6276  break;
6277  }
6278  }
6279  if (!st) {
6280  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
6281  return 0;
6282  }
6283 
6284  sc = st->priv_data;
6285 
6286  timescale = av_make_q(1, avio_rb32(pb));
6287 
6288  if (timescale.den <= 0) {
6289  av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
6290  return AVERROR_INVALIDDATA;
6291  }
6292 
6293  if (version == 0) {
6294  pts = avio_rb32(pb);
6295  offadd= avio_rb32(pb);
6296  } else {
6297  pts = avio_rb64(pb);
6298  offadd= avio_rb64(pb);
6299  }
6300  if (av_sat_add64(offset, offadd) != offset + (uint64_t)offadd)
6301  return AVERROR_INVALIDDATA;
6302 
6303  offset += (uint64_t)offadd;
6304 
6305  avio_rb16(pb); // reserved
6306 
6307  item_count = avio_rb16(pb);
6308  if (item_count == 0)
6309  return AVERROR_INVALIDDATA;
6310 
6311  for (i = 0; i < item_count; i++) {
6312  int index;
6313  MOVFragmentStreamInfo * frag_stream_info;
6314  uint32_t size = avio_rb32(pb);
6315  uint32_t duration = avio_rb32(pb);
6316  if (size & 0x80000000) {
6317  avpriv_request_sample(c->fc, "sidx reference_type 1");
6318  return AVERROR_PATCHWELCOME;
6319  }
6320  avio_rb32(pb); // sap_flags
6321  timestamp = av_rescale_q(pts, timescale, st->time_base);
6322 
6324  frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
6325  if (frag_stream_info)
6326  frag_stream_info->sidx_pts = timestamp;
6327 
6328  if (av_sat_add64(offset, size) != offset + (uint64_t)size ||
6329  av_sat_add64(pts, duration) != pts + (uint64_t)duration
6330  )
6331  return AVERROR_INVALIDDATA;
6332  offset += size;
6333  pts += duration;
6334  }
6335 
6336  st->duration = sc->track_end = pts;
6337 
6338  sc->has_sidx = 1;
6339 
6340  // See if the remaining bytes are just an mfra which we can ignore.
6341  is_complete = offset == stream_size;
6342  if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL) && stream_size > 0 ) {
6343  int64_t ret;
6344  int64_t original_pos = avio_tell(pb);
6345  if (!c->have_read_mfra_size) {
6346  if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
6347  return ret;
6348  c->mfra_size = avio_rb32(pb);
6349  c->have_read_mfra_size = 1;
6350  if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
6351  return ret;
6352  }
6353  if (offset == stream_size - c->mfra_size)
6354  is_complete = 1;
6355  }
6356 
6357  if (is_complete) {
6358  // Find first entry in fragment index that came from an sidx.
6359  // This will pretty much always be the first entry.
6360  for (i = 0; i < c->frag_index.nb_items; i++) {
6361  MOVFragmentIndexItem * item = &c->frag_index.item[i];
6362  for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
6363  MOVFragmentStreamInfo * si;
6364  si = &item->stream_info[j];
6365  if (si->sidx_pts != AV_NOPTS_VALUE) {
6366  ref_st = c->fc->streams[j];
6367  ref_sc = ref_st->priv_data;
6368  break;
6369  }
6370  }
6371  }
6372  if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
6373  st = c->fc->streams[i];
6374  sc = st->priv_data;
6375  if (!sc->has_sidx) {
6376  st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
6377  }
6378  }
6379 
6380  if (offadd == 0)
6381  c->frag_index.complete = 1;
6382  }
6383 
6384  return 0;
6385 }
6386 
6387 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
6388 /* like the files created with Adobe Premiere 5.0, for samples see */
6389 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
6391 {
6392  int err;
6393 
6394  if (atom.size < 8)
6395  return 0; /* continue */
6396  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
6397  avio_skip(pb, atom.size - 4);
6398  return 0;
6399  }
6400  atom.type = avio_rl32(pb);
6401  atom.size -= 8;
6402  if (atom.type != MKTAG('m','d','a','t')) {
6403  avio_skip(pb, atom.size);
6404  return 0;
6405  }
6406  err = mov_read_mdat(c, pb, atom);
6407  return err;
6408 }
6409 
6411 {
6412 #if CONFIG_ZLIB
6413  FFIOContext ctx;
6414  uint8_t *cmov_data;
6415  uint8_t *moov_data; /* uncompressed data */
6416  long cmov_len, moov_len;
6417  int ret = -1;
6418 
6419  avio_rb32(pb); /* dcom atom */
6420  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
6421  return AVERROR_INVALIDDATA;
6422  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
6423  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
6424  return AVERROR_INVALIDDATA;
6425  }
6426  avio_rb32(pb); /* cmvd atom */
6427  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
6428  return AVERROR_INVALIDDATA;
6429  moov_len = avio_rb32(pb); /* uncompressed size */
6430  cmov_len = atom.size - 6 * 4;
6431 
6432  cmov_data = av_malloc(cmov_len);
6433  if (!cmov_data)
6434  return AVERROR(ENOMEM);
6435  moov_data = av_malloc(moov_len);
6436  if (!moov_data) {
6437  av_free(cmov_data);
6438  return AVERROR(ENOMEM);
6439  }
6440  ret = ffio_read_size(pb, cmov_data, cmov_len);
6441  if (ret < 0)
6442  goto free_and_return;
6443 
6445  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
6446  goto free_and_return;
6447  ffio_init_read_context(&ctx, moov_data, moov_len);
6448  ctx.pub.seekable = AVIO_SEEKABLE_NORMAL;
6449  atom.type = MKTAG('m','o','o','v');
6450  atom.size = moov_len;
6451  ret = mov_read_default(c, &ctx.pub, atom);
6452 free_and_return:
6453  av_free(moov_data);
6454  av_free(cmov_data);
6455  return ret;
6456 #else
6457  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
6458  return AVERROR(ENOSYS);
6459 #endif
6460 }
6461 
6462 /* edit list atom */
6464 {
6465  MOVStreamContext *sc;
6466  int i, edit_count, version;
6467  int64_t elst_entry_size;
6468 
6469  if (c->fc->nb_streams < 1 || c->ignore_editlist)
6470  return 0;
6471  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
6472 
6473  version = avio_r8(pb); /* version */
6474  avio_rb24(pb); /* flags */
6475  edit_count = avio_rb32(pb); /* entries */
6476  atom.size -= 8;
6477 
6478  elst_entry_size = version == 1 ? 20 : 12;
6479  if (atom.size != edit_count * elst_entry_size) {
6480  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6481  av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
6482  edit_count, atom.size + 8);
6483  return AVERROR_INVALIDDATA;
6484  } else {
6485  edit_count = atom.size / elst_entry_size;
6486  if (edit_count * elst_entry_size != atom.size) {
6487  av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
6488  }
6489  }
6490  }
6491 
6492  if (!edit_count)
6493  return 0;
6494  if (sc->elst_data)
6495  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
6496  av_free(sc->elst_data);
6497  sc->elst_count = 0;
6498  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
6499  if (!sc->elst_data)
6500  return AVERROR(ENOMEM);
6501 
6502  av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
6503  for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
6504  MOVElst *e = &sc->elst_data[i];
6505 
6506  if (version == 1) {
6507  e->duration = avio_rb64(pb);
6508  e->time = avio_rb64(pb);
6509  atom.size -= 16;
6510  } else {
6511  e->duration = avio_rb32(pb); /* segment duration */
6512  e->time = (int32_t)avio_rb32(pb); /* media time */
6513  atom.size -= 8;
6514  }
6515  e->rate = avio_rb32(pb) / 65536.0;
6516  atom.size -= 4;
6517  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
6518  e->duration, e->time, e->rate);
6519 
6520  if (e->time < 0 && e->time != -1 &&
6521  c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6522  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
6523  c->fc->nb_streams-1, i, e->time);
6524  return AVERROR_INVALIDDATA;
6525  }
6526  if (e->duration < 0) {
6527  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list duration=%"PRId64"\n",
6528  c->fc->nb_streams-1, i, e->duration);
6529  return AVERROR_INVALIDDATA;
6530  }
6531  }
6532  sc->elst_count = i;
6533 
6534  return 0;
6535 }
6536 
6538 {
6539  MOVStreamContext *sc;
6540  int err;
6541 
6542  if (c->fc->nb_streams < 1)
6543  return AVERROR_INVALIDDATA;
6544  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6545 
6546  if (atom.size % sizeof(uint32_t))
6547  return AVERROR_INVALIDDATA;
6548 
6549  MovTref *tag = mov_add_tref_tag(sc, atom.type);
6550  if (!tag)
6551  return AVERROR(ENOMEM);
6552 
6553  int nb_timecode_track = atom.size >> 2;
6554  for (int i = 0; i < nb_timecode_track; i++) {
6555  if (avio_feof(pb))
6556  return AVERROR_INVALIDDATA;
6557  err = mov_add_tref_id(tag, avio_rb32(pb));
6558  if (err < 0)
6559  return err;
6560  }
6561 
6562  return 0;
6563 }
6564 
6566 {
6567  AVStream *st;
6568  int version, color_range, color_primaries, color_trc, color_space;
6569 
6570  if (c->fc->nb_streams < 1)
6571  return 0;
6572  st = c->fc->streams[c->fc->nb_streams - 1];
6573 
6574  if (atom.size < 5) {
6575  av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
6576  return AVERROR_INVALIDDATA;
6577  }
6578 
6579  version = avio_r8(pb);
6580  if (version != 1) {
6581  av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
6582  return 0;
6583  }
6584  avio_skip(pb, 3); /* flags */
6585 
6586  avio_skip(pb, 2); /* profile + level */
6587  color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
6588  color_primaries = avio_r8(pb);
6589  color_trc = avio_r8(pb);
6590  color_space = avio_r8(pb);
6591  if (avio_rb16(pb)) /* codecIntializationDataSize */
6592  return AVERROR_INVALIDDATA;
6593 
6596  if (!av_color_transfer_name(color_trc))
6597  color_trc = AVCOL_TRC_UNSPECIFIED;
6598  if (!av_color_space_name(color_space))
6599  color_space = AVCOL_SPC_UNSPECIFIED;
6600 
6603  st->codecpar->color_trc = color_trc;
6604  st->codecpar->color_space = color_space;
6605 
6606  return 0;
6607 }
6608 
6610 {
6611  MOVStreamContext *sc;
6612  int i, version;
6613 
6614  if (c->fc->nb_streams < 1)
6615  return AVERROR_INVALIDDATA;
6616 
6617  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6618 
6619  if (atom.size < 5) {
6620  av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
6621  return AVERROR_INVALIDDATA;
6622  }
6623 
6624  version = avio_r8(pb);
6625  if (version) {
6626  av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
6627  return 0;
6628  }
6629  if (sc->mastering) {
6630  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Metadata\n");
6631  return 0;
6632  }
6633 
6634  avio_skip(pb, 3); /* flags */
6635 
6637  if (!sc->mastering)
6638  return AVERROR(ENOMEM);
6639 
6640  for (i = 0; i < 3; i++) {
6641  sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
6642  sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
6643  }
6644  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
6645  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
6646 
6647  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
6648  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
6649 
6650  sc->mastering->has_primaries = 1;
6651  sc->mastering->has_luminance = 1;
6652 
6653  return 0;
6654 }
6655 
6657 {
6658  MOVStreamContext *sc;
6659  const int mapping[3] = {1, 2, 0};
6660  const int chroma_den = 50000;
6661  const int luma_den = 10000;
6662  int i;
6663 
6664  if (c->fc->nb_streams < 1)
6665  return AVERROR_INVALIDDATA;
6666 
6667  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6668 
6669  if (atom.size < 24) {
6670  av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
6671  return AVERROR_INVALIDDATA;
6672  }
6673 
6674  if (sc->mastering) {
6675  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Color Volume\n");
6676  return 0;
6677  }
6678 
6680  if (!sc->mastering)
6681  return AVERROR(ENOMEM);
6682 
6683  for (i = 0; i < 3; i++) {
6684  const int j = mapping[i];
6685  sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
6686  sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
6687  }
6688  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
6689  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
6690 
6691  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
6692  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
6693 
6694  sc->mastering->has_luminance = 1;
6695  sc->mastering->has_primaries = 1;
6696 
6697  return 0;
6698 }
6699 
6701 {
6702  MOVStreamContext *sc;
6703  int version;
6704 
6705  if (c->fc->nb_streams < 1)
6706  return AVERROR_INVALIDDATA;
6707 
6708  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6709 
6710  if (atom.size < 5) {
6711  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
6712  return AVERROR_INVALIDDATA;
6713  }
6714 
6715  version = avio_r8(pb);
6716  if (version) {
6717  av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
6718  return 0;
6719  }
6720  avio_skip(pb, 3); /* flags */
6721 
6722  if (sc->coll){
6723  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate COLL\n");
6724  return 0;
6725  }
6726 
6728  if (!sc->coll)
6729  return AVERROR(ENOMEM);
6730 
6731  sc->coll->MaxCLL = avio_rb16(pb);
6732  sc->coll->MaxFALL = avio_rb16(pb);
6733 
6734  return 0;
6735 }
6736 
6738 {
6739  MOVStreamContext *sc;
6740 
6741  if (c->fc->nb_streams < 1)
6742  return AVERROR_INVALIDDATA;
6743 
6744  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6745 
6746  if (atom.size < 4) {
6747  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
6748  return AVERROR_INVALIDDATA;
6749  }
6750 
6751  if (sc->coll){
6752  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate CLLI/COLL\n");
6753  return 0;
6754  }
6755 
6757  if (!sc->coll)
6758  return AVERROR(ENOMEM);
6759 
6760  sc->coll->MaxCLL = avio_rb16(pb);
6761  sc->coll->MaxFALL = avio_rb16(pb);
6762 
6763  return 0;
6764 }
6765 
6767 {
6768  MOVStreamContext *sc;
6769  const int illuminance_den = 10000;
6770  const int ambient_den = 50000;
6771  if (c->fc->nb_streams < 1)
6772  return AVERROR_INVALIDDATA;
6773  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6774  if (atom.size < 6) {
6775  av_log(c->fc, AV_LOG_ERROR, "Empty Ambient Viewing Environment Info box\n");
6776  return AVERROR_INVALIDDATA;
6777  }
6778  if (sc->ambient){
6779  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate AMVE\n");
6780  return 0;
6781  }
6783  if (!sc->ambient)
6784  return AVERROR(ENOMEM);
6785  sc->ambient->ambient_illuminance = av_make_q(avio_rb32(pb), illuminance_den);
6786  sc->ambient->ambient_light_x = av_make_q(avio_rb16(pb), ambient_den);
6787  sc->ambient->ambient_light_y = av_make_q(avio_rb16(pb), ambient_den);
6788  return 0;
6789 }
6790 
6792 {
6793  AVStream *st;
6794  MOVStreamContext *sc;
6795  enum AVStereo3DType type;
6796  int mode;
6797 
6798  if (c->fc->nb_streams < 1)
6799  return 0;
6800 
6801  st = c->fc->streams[c->fc->nb_streams - 1];
6802  sc = st->priv_data;
6803 
6804  if (atom.size < 5) {
6805  av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
6806  return AVERROR_INVALIDDATA;
6807  }
6808 
6809  if (sc->stereo3d)
6810  return AVERROR_INVALIDDATA;
6811 
6812  avio_skip(pb, 4); /* version + flags */
6813 
6814  mode = avio_r8(pb);
6815  switch (mode) {
6816  case 0:
6817  type = AV_STEREO3D_2D;
6818  break;
6819  case 1:
6821  break;
6822  case 2:
6824  break;
6825  default:
6826  av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
6827  return 0;
6828  }
6829 
6831  if (!sc->stereo3d)
6832  return AVERROR(ENOMEM);
6833 
6834  sc->stereo3d->type = type;
6835  return 0;
6836 }
6837 
6839 {
6840  AVStream *st;
6841  MOVStreamContext *sc;
6842  int size = 0;
6843  int64_t remaining;
6844  uint32_t tag = 0;
6846 
6847  if (c->fc->nb_streams < 1)
6848  return 0;
6849 
6850  st = c->fc->streams[c->fc->nb_streams - 1];
6851  sc = st->priv_data;
6852 
6853  remaining = atom.size;
6854  while (remaining > 0) {
6855  size = avio_rb32(pb);
6856  if (size < 8 || size > remaining ) {
6857  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in pack box\n");
6858  return AVERROR_INVALIDDATA;
6859  }
6860 
6861  tag = avio_rl32(pb);
6862  switch (tag) {
6863  case MKTAG('p','k','i','n'): {
6864  if (size != 16) {
6865  av_log(c->fc, AV_LOG_ERROR, "Invalid size of pkin box: %d\n", size);
6866  return AVERROR_INVALIDDATA;
6867  }
6868  avio_skip(pb, 1); // version
6869  avio_skip(pb, 3); // flags
6870 
6871  tag = avio_rl32(pb);
6872  switch (tag) {
6873  case MKTAG('s','i','d','e'):
6875  break;
6876  case MKTAG('o','v','e','r'):
6878  break;
6879  case 0:
6880  // This means value will be set in another layer
6881  break;
6882  default:
6883  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in pkin: %s\n",
6884  av_fourcc2str(tag));
6885  avio_skip(pb, size - 8);
6886  break;
6887  }
6888 
6889  break;
6890  }
6891  default:
6892  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in pack: %s\n",
6893  av_fourcc2str(tag));
6894  avio_skip(pb, size - 8);
6895  break;
6896  }
6897  remaining -= size;
6898  }
6899 
6900  if (remaining != 0) {
6901  av_log(c->fc, AV_LOG_ERROR, "Broken pack box\n");
6902  return AVERROR_INVALIDDATA;
6903  }
6904 
6905  if (type == AV_STEREO3D_2D)
6906  return 0;
6907 
6908  if (!sc->stereo3d) {
6910  if (!sc->stereo3d)
6911  return AVERROR(ENOMEM);
6912  }
6913 
6914  sc->stereo3d->type = type;
6915 
6916  return 0;
6917 }
6918 
6920 {
6921  AVStream *st;
6922  MOVStreamContext *sc;
6923  int size, version, layout;
6924  int32_t yaw, pitch, roll;
6925  uint32_t l = 0, t = 0, r = 0, b = 0;
6926  uint32_t tag, padding = 0;
6927  enum AVSphericalProjection projection;
6928 
6929  if (c->fc->nb_streams < 1)
6930  return 0;
6931 
6932  st = c->fc->streams[c->fc->nb_streams - 1];
6933  sc = st->priv_data;
6934 
6935  if (atom.size < 8) {
6936  av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
6937  return AVERROR_INVALIDDATA;
6938  }
6939 
6940  size = avio_rb32(pb);
6941  if (size <= 12 || size > atom.size)
6942  return AVERROR_INVALIDDATA;
6943 
6944  tag = avio_rl32(pb);
6945  if (tag != MKTAG('s','v','h','d')) {
6946  av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
6947  return 0;
6948  }
6949  version = avio_r8(pb);
6950  if (version != 0) {
6951  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6952  version);
6953  return 0;
6954  }
6955  avio_skip(pb, 3); /* flags */
6956  avio_skip(pb, size - 12); /* metadata_source */
6957 
6958  size = avio_rb32(pb);
6959  if (size > atom.size)
6960  return AVERROR_INVALIDDATA;
6961 
6962  tag = avio_rl32(pb);
6963  if (tag != MKTAG('p','r','o','j')) {
6964  av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
6965  return 0;
6966  }
6967 
6968  size = avio_rb32(pb);
6969  if (size > atom.size)
6970  return AVERROR_INVALIDDATA;
6971 
6972  tag = avio_rl32(pb);
6973  if (tag != MKTAG('p','r','h','d')) {
6974  av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
6975  return 0;
6976  }
6977  version = avio_r8(pb);
6978  if (version != 0) {
6979  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6980  version);
6981  return 0;
6982  }
6983  avio_skip(pb, 3); /* flags */
6984 
6985  /* 16.16 fixed point */
6986  yaw = avio_rb32(pb);
6987  pitch = avio_rb32(pb);
6988  roll = avio_rb32(pb);
6989 
6990  size = avio_rb32(pb);
6991  if (size > atom.size)
6992  return AVERROR_INVALIDDATA;
6993 
6994  tag = avio_rl32(pb);
6995  version = avio_r8(pb);
6996  if (version != 0) {
6997  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6998  version);
6999  return 0;
7000  }
7001  avio_skip(pb, 3); /* flags */
7002  switch (tag) {
7003  case MKTAG('c','b','m','p'):
7004  layout = avio_rb32(pb);
7005  if (layout) {
7006  av_log(c->fc, AV_LOG_WARNING,
7007  "Unsupported cubemap layout %d\n", layout);
7008  return 0;
7009  }
7010  projection = AV_SPHERICAL_CUBEMAP;
7011  padding = avio_rb32(pb);
7012  break;
7013  case MKTAG('e','q','u','i'):
7014  t = avio_rb32(pb);
7015  b = avio_rb32(pb);
7016  l = avio_rb32(pb);
7017  r = avio_rb32(pb);
7018 
7019  if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
7020  av_log(c->fc, AV_LOG_ERROR,
7021  "Invalid bounding rectangle coordinates "
7022  "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
7023  return AVERROR_INVALIDDATA;
7024  }
7025 
7026  if (l || t || r || b)
7027  projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
7028  else
7029  projection = AV_SPHERICAL_EQUIRECTANGULAR;
7030  break;
7031  default:
7032  av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
7033  return 0;
7034  }
7035 
7037  if (!sc->spherical)
7038  return AVERROR(ENOMEM);
7039 
7040  sc->spherical->projection = projection;
7041 
7042  sc->spherical->yaw = yaw;
7043  sc->spherical->pitch = pitch;
7044  sc->spherical->roll = roll;
7045 
7046  sc->spherical->padding = padding;
7047 
7048  sc->spherical->bound_left = l;
7049  sc->spherical->bound_top = t;
7050  sc->spherical->bound_right = r;
7051  sc->spherical->bound_bottom = b;
7052 
7053  return 0;
7054 }
7055 
7057 {
7058  AVStream *st;
7059  MOVStreamContext *sc;
7060  int size;
7061  uint32_t tag;
7062  enum AVSphericalProjection projection;
7063 
7064  if (c->fc->nb_streams < 1)
7065  return 0;
7066 
7067  st = c->fc->streams[c->fc->nb_streams - 1];
7068  sc = st->priv_data;
7069 
7070  if (atom.size < 16) {
7071  av_log(c->fc, AV_LOG_ERROR, "Invalid size for proj box: %"PRIu64"\n", atom.size);
7072  return AVERROR_INVALIDDATA;
7073  }
7074 
7075  size = avio_rb32(pb);
7076  if (size < 16) {
7077  av_log(c->fc, AV_LOG_ERROR, "Invalid size for prji box: %d\n", size);
7078  return AVERROR_INVALIDDATA;
7079  } else if (size > 16) {
7080  av_log(c->fc, AV_LOG_WARNING, "Box has more bytes (%d) than prji box required (16) \n", size);
7081  }
7082 
7083  tag = avio_rl32(pb);
7084  if (tag != MKTAG('p','r','j','i')) {
7085  av_log(c->fc, AV_LOG_ERROR, "Invalid child box of proj box: %s\n",
7086  av_fourcc2str(tag));
7087  return AVERROR_INVALIDDATA;
7088  }
7089 
7090  // version and flags, only support (0, 0)
7091  unsigned n = avio_rl32(pb);
7092  if (n != 0) {
7093  av_log(c->fc, AV_LOG_ERROR, "prji version %u, flag %u are not supported\n",
7094  n & 0xFF, n >> 8);
7095  return AVERROR_PATCHWELCOME;
7096  }
7097 
7098  tag = avio_rl32(pb);
7099  switch (tag) {
7100  case MKTAG('r','e','c','t'):
7101  projection = AV_SPHERICAL_RECTILINEAR;
7102  break;
7103  case MKTAG('e','q','u','i'):
7104  projection = AV_SPHERICAL_EQUIRECTANGULAR;
7105  break;
7106  case MKTAG('h','e','q','u'):
7107  projection = AV_SPHERICAL_HALF_EQUIRECTANGULAR;
7108  break;
7109  case MKTAG('f','i','s','h'):
7110  projection = AV_SPHERICAL_FISHEYE;
7111  break;
7112  case MKTAG('p','r','i','m'):
7113  projection = AV_SPHERICAL_PARAMETRIC_IMMERSIVE;
7114  break;
7115  default:
7116  av_log(c->fc, AV_LOG_ERROR, "Invalid projection type in prji box: %s\n", av_fourcc2str(tag));
7117  return AVERROR_INVALIDDATA;
7118  }
7119 
7121  if (!sc->spherical)
7122  return AVERROR(ENOMEM);
7123 
7124  sc->spherical->projection = projection;
7125 
7126  return 0;
7127 }
7128 
7130 {
7131  AVStream *st;
7132  MOVStreamContext *sc;
7133  int size, flags = 0;
7134  int64_t remaining;
7135  uint32_t tag, baseline = 0;
7138  enum AVStereo3DPrimaryEye primary_eye = AV_PRIMARY_EYE_NONE;
7139  AVRational horizontal_disparity_adjustment = { 0, 1 };
7140 
7141  if (c->fc->nb_streams < 1)
7142  return 0;
7143 
7144  st = c->fc->streams[c->fc->nb_streams - 1];
7145  sc = st->priv_data;
7146 
7147  remaining = atom.size;
7148  while (remaining > 0) {
7149  size = avio_rb32(pb);
7150  if (size < 8 || size > remaining ) {
7151  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in eyes box\n");
7152  return AVERROR_INVALIDDATA;
7153  }
7154 
7155  tag = avio_rl32(pb);
7156  switch (tag) {
7157  case MKTAG('s','t','r','i'): {
7158  int has_right, has_left;
7159  uint8_t tmp;
7160  if (size != 13) {
7161  av_log(c->fc, AV_LOG_ERROR, "Invalid size of stri box: %d\n", size);
7162  return AVERROR_INVALIDDATA;
7163  }
7164  avio_skip(pb, 1); // version
7165  avio_skip(pb, 3); // flags
7166 
7167  tmp = avio_r8(pb);
7168 
7169  // eye_views_reversed
7170  if (tmp & 8) {
7172  }
7173  // has_additional_views
7174  if (tmp & 4) {
7175  // skip...
7176  }
7177 
7178  has_right = tmp & 2; // has_right_eye_view
7179  has_left = tmp & 1; // has_left_eye_view
7180 
7181  if (has_left && has_right)
7182  view = AV_STEREO3D_VIEW_PACKED;
7183  else if (has_left)
7184  view = AV_STEREO3D_VIEW_LEFT;
7185  else if (has_right)
7186  view = AV_STEREO3D_VIEW_RIGHT;
7187  if (has_left || has_right)
7189 
7190  break;
7191  }
7192  case MKTAG('h','e','r','o'): {
7193  int tmp;
7194  if (size != 13) {
7195  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hero box: %d\n", size);
7196  return AVERROR_INVALIDDATA;
7197  }
7198  avio_skip(pb, 1); // version
7199  avio_skip(pb, 3); // flags
7200 
7201  tmp = avio_r8(pb);
7202  if (tmp == 0)
7203  primary_eye = AV_PRIMARY_EYE_NONE;
7204  else if (tmp == 1)
7205  primary_eye = AV_PRIMARY_EYE_LEFT;
7206  else if (tmp == 2)
7207  primary_eye = AV_PRIMARY_EYE_RIGHT;
7208  else
7209  av_log(c->fc, AV_LOG_WARNING, "Unknown hero eye type: %d\n", tmp);
7210 
7211  break;
7212  }
7213  case MKTAG('c','a','m','s'): {
7214  uint32_t subtag;
7215  int subsize;
7216  if (size != 24) {
7217  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cams box: %d\n", size);
7218  return AVERROR_INVALIDDATA;
7219  }
7220 
7221  subsize = avio_rb32(pb);
7222  if (subsize != 16) {
7223  av_log(c->fc, AV_LOG_ERROR, "Invalid size of blin box: %d\n", size);
7224  return AVERROR_INVALIDDATA;
7225  }
7226 
7227  subtag = avio_rl32(pb);
7228  if (subtag != MKTAG('b','l','i','n')) {
7229  av_log(c->fc, AV_LOG_ERROR, "Expected blin box, got %s\n",
7230  av_fourcc2str(subtag));
7231  return AVERROR_INVALIDDATA;
7232  }
7233 
7234  avio_skip(pb, 1); // version
7235  avio_skip(pb, 3); // flags
7236 
7237  baseline = avio_rb32(pb);
7238 
7239  break;
7240  }
7241  case MKTAG('c','m','f','y'): {
7242  uint32_t subtag;
7243  int subsize;
7244  int32_t adjustment;
7245  if (size != 24) {
7246  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cmfy box: %d\n", size);
7247  return AVERROR_INVALIDDATA;
7248  }
7249 
7250  subsize = avio_rb32(pb);
7251  if (subsize != 16) {
7252  av_log(c->fc, AV_LOG_ERROR, "Invalid size of dadj box: %d\n", size);
7253  return AVERROR_INVALIDDATA;
7254  }
7255 
7256  subtag = avio_rl32(pb);
7257  if (subtag != MKTAG('d','a','d','j')) {
7258  av_log(c->fc, AV_LOG_ERROR, "Expected dadj box, got %s\n",
7259  av_fourcc2str(subtag));
7260  return AVERROR_INVALIDDATA;
7261  }
7262 
7263  avio_skip(pb, 1); // version
7264  avio_skip(pb, 3); // flags
7265 
7266  adjustment = (int32_t) avio_rb32(pb);
7267 
7268  horizontal_disparity_adjustment.num = (int) adjustment;
7269  horizontal_disparity_adjustment.den = 10000;
7270 
7271  break;
7272  }
7273  default:
7274  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in eyes: %s\n",
7275  av_fourcc2str(tag));
7276  avio_skip(pb, size - 8);
7277  break;
7278  }
7279  remaining -= size;
7280  }
7281 
7282  if (remaining != 0) {
7283  av_log(c->fc, AV_LOG_ERROR, "Broken eyes box\n");
7284  return AVERROR_INVALIDDATA;
7285  }
7286 
7287  if (type == AV_STEREO3D_2D)
7288  return 0;
7289 
7290  if (!sc->stereo3d) {
7292  if (!sc->stereo3d)
7293  return AVERROR(ENOMEM);
7294  }
7295 
7296  sc->stereo3d->flags = flags;
7297  sc->stereo3d->type = type;
7298  sc->stereo3d->view = view;
7299  sc->stereo3d->primary_eye = primary_eye;
7300  sc->stereo3d->baseline = baseline;
7301  sc->stereo3d->horizontal_disparity_adjustment = horizontal_disparity_adjustment;
7302 
7303  return 0;
7304 }
7305 
7307 {
7308  int size;
7309  int64_t remaining;
7310  uint32_t tag;
7311 
7312  if (c->fc->nb_streams < 1)
7313  return 0;
7314 
7315  if (atom.size < 8) {
7316  av_log(c->fc, AV_LOG_ERROR, "Empty video extension usage box\n");
7317  return AVERROR_INVALIDDATA;
7318  }
7319 
7320  remaining = atom.size;
7321  while (remaining > 0) {
7322  size = avio_rb32(pb);
7323  if (size < 8 || size > remaining ) {
7324  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in vexu box\n");
7325  return AVERROR_INVALIDDATA;
7326  }
7327 
7328  tag = avio_rl32(pb);
7329  switch (tag) {
7330  case MKTAG('p','r','o','j'): {
7331  MOVAtom proj = { tag, size - 8 };
7332  int ret = mov_read_vexu_proj(c, pb, proj);
7333  if (ret < 0)
7334  return ret;
7335  break;
7336  }
7337  case MKTAG('e','y','e','s'): {
7338  MOVAtom eyes = { tag, size - 8 };
7339  int ret = mov_read_eyes(c, pb, eyes);
7340  if (ret < 0)
7341  return ret;
7342  break;
7343  }
7344  case MKTAG('p','a','c','k'): {
7345  MOVAtom pack = { tag, size - 8 };
7346  int ret = mov_read_pack(c, pb, pack);
7347  if (ret < 0)
7348  return ret;
7349  break;
7350  }
7351  default:
7352  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in vexu: %s\n",
7353  av_fourcc2str(tag));
7354  avio_skip(pb, size - 8);
7355  break;
7356  }
7357  remaining -= size;
7358  }
7359 
7360  if (remaining != 0) {
7361  av_log(c->fc, AV_LOG_ERROR, "Broken vexu box\n");
7362  return AVERROR_INVALIDDATA;
7363  }
7364 
7365  return 0;
7366 }
7367 
7369 {
7370  AVStream *st;
7371  MOVStreamContext *sc;
7372 
7373  if (c->fc->nb_streams < 1)
7374  return 0;
7375 
7376  st = c->fc->streams[c->fc->nb_streams - 1];
7377  sc = st->priv_data;
7378 
7379  if (atom.size != 4) {
7380  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hfov box: %"PRIu64"\n", atom.size);
7381  return AVERROR_INVALIDDATA;
7382  }
7383 
7384 
7385  if (!sc->stereo3d) {
7387  if (!sc->stereo3d)
7388  return AVERROR(ENOMEM);
7389  }
7390 
7392  sc->stereo3d->horizontal_field_of_view.den = 1000; // thousands of a degree
7393 
7394  return 0;
7395 }
7396 
7398 {
7399  int ret = 0;
7400  uint8_t *buffer = av_malloc(len + 1);
7401  const char *val;
7402 
7403  if (!buffer)
7404  return AVERROR(ENOMEM);
7405  buffer[len] = '\0';
7406 
7407  ret = ffio_read_size(pb, buffer, len);
7408  if (ret < 0)
7409  goto out;
7410 
7411  /* Check for mandatory keys and values, try to support XML as best-effort */
7412  if (!sc->spherical &&
7413  av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
7414  (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
7415  av_stristr(val, "true") &&
7416  (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
7417  av_stristr(val, "true") &&
7418  (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
7419  av_stristr(val, "equirectangular")) {
7421  if (!sc->spherical)
7422  goto out;
7423 
7425 
7426  if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
7427  enum AVStereo3DType mode;
7428 
7429  if (av_stristr(buffer, "left-right"))
7431  else if (av_stristr(buffer, "top-bottom"))
7433  else
7434  mode = AV_STEREO3D_2D;
7435 
7437  if (!sc->stereo3d)
7438  goto out;
7439 
7440  sc->stereo3d->type = mode;
7441  }
7442 
7443  /* orientation */
7444  val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
7445  if (val)
7446  sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
7447  val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
7448  if (val)
7449  sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
7450  val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
7451  if (val)
7452  sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
7453  }
7454 
7455 out:
7456  av_free(buffer);
7457  return ret;
7458 }
7459 
7461 {
7462  AVStream *st;
7463  MOVStreamContext *sc;
7464  int64_t ret;
7465  AVUUID uuid;
7466  static const AVUUID uuid_isml_manifest = {
7467  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
7468  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
7469  };
7470  static const AVUUID uuid_xmp = {
7471  0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
7472  0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
7473  };
7474  static const AVUUID uuid_spherical = {
7475  0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
7476  0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
7477  };
7478 
7479  if (atom.size < AV_UUID_LEN || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
7480  return AVERROR_INVALIDDATA;
7481 
7482  if (c->fc->nb_streams < 1)
7483  return 0;
7484  st = c->fc->streams[c->fc->nb_streams - 1];
7485  sc = st->priv_data;
7486 
7487  ret = ffio_read_size(pb, uuid, AV_UUID_LEN);
7488  if (ret < 0)
7489  return ret;
7490  if (av_uuid_equal(uuid, uuid_isml_manifest)) {
7491  uint8_t *buffer, *ptr;
7492  char *endptr;
7493  size_t len = atom.size - AV_UUID_LEN;
7494 
7495  if (len < 4) {
7496  return AVERROR_INVALIDDATA;
7497  }
7498  ret = avio_skip(pb, 4); // zeroes
7499  len -= 4;
7500 
7501  buffer = av_mallocz(len + 1);
7502  if (!buffer) {
7503  return AVERROR(ENOMEM);
7504  }
7505  ret = ffio_read_size(pb, buffer, len);
7506  if (ret < 0) {
7507  av_free(buffer);
7508  return ret;
7509  }
7510 
7511  ptr = buffer;
7512  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
7513  ptr += sizeof("systemBitrate=\"") - 1;
7514  c->bitrates_count++;
7515  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
7516  if (!c->bitrates) {
7517  c->bitrates_count = 0;
7518  av_free(buffer);
7519  return AVERROR(ENOMEM);
7520  }
7521  errno = 0;
7522  ret = strtol(ptr, &endptr, 10);
7523  if (ret < 0 || errno || *endptr != '"') {
7524  c->bitrates[c->bitrates_count - 1] = 0;
7525  } else {
7526  c->bitrates[c->bitrates_count - 1] = ret;
7527  }
7528  }
7529 
7530  av_free(buffer);
7531  } else if (av_uuid_equal(uuid, uuid_xmp)) {
7532  uint8_t *buffer;
7533  size_t len = atom.size - AV_UUID_LEN;
7534  if (c->export_xmp) {
7535  buffer = av_mallocz(len + 1);
7536  if (!buffer) {
7537  return AVERROR(ENOMEM);
7538  }
7539  ret = ffio_read_size(pb, buffer, len);
7540  if (ret < 0) {
7541  av_free(buffer);
7542  return ret;
7543  }
7544  buffer[len] = '\0';
7545  av_dict_set(&c->fc->metadata, "xmp",
7547  } else {
7548  // skip all uuid atom, which makes it fast for long uuid-xmp file
7549  ret = avio_skip(pb, len);
7550  if (ret < 0)
7551  return ret;
7552  }
7553  } else if (av_uuid_equal(uuid, uuid_spherical)) {
7554  size_t len = atom.size - AV_UUID_LEN;
7555  ret = mov_parse_uuid_spherical(sc, pb, len);
7556  if (ret < 0)
7557  return ret;
7558  if (!sc->spherical)
7559  av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
7560  }
7561 
7562  return 0;
7563 }
7564 
7566 {
7567  int ret;
7568  uint8_t content[16];
7569 
7570  if (atom.size < 8)
7571  return 0;
7572 
7573  ret = ffio_read_size(pb, content, FFMIN(sizeof(content), atom.size));
7574  if (ret < 0)
7575  return ret;
7576 
7577  if ( !c->found_moov
7578  && !c->found_mdat
7579  && !memcmp(content, "Anevia\x1A\x1A", 8)
7580  && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
7581  c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
7582  }
7583 
7584  return 0;
7585 }
7586 
7588 {
7589  uint32_t format = avio_rl32(pb);
7590  MOVStreamContext *sc;
7591  enum AVCodecID id;
7592  AVStream *st;
7593 
7594  if (c->fc->nb_streams < 1)
7595  return 0;
7596  st = c->fc->streams[c->fc->nb_streams - 1];
7597  sc = st->priv_data;
7598 
7599  switch (sc->format)
7600  {
7601  case MKTAG('e','n','c','v'): // encrypted video
7602  case MKTAG('e','n','c','a'): // encrypted audio
7603  id = mov_codec_id(st, format);
7604  if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
7605  st->codecpar->codec_id != id) {
7606  av_log(c->fc, AV_LOG_WARNING,
7607  "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
7608  (char*)&format, st->codecpar->codec_id);
7609  break;
7610  }
7611 
7612  st->codecpar->codec_id = id;
7613  sc->format = format;
7614  break;
7615 
7616  default:
7617  if (format != sc->format) {
7618  av_log(c->fc, AV_LOG_WARNING,
7619  "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
7620  (char*)&format, (char*)&sc->format);
7621  }
7622  break;
7623  }
7624 
7625  return 0;
7626 }
7627 
7628 /**
7629  * Gets the current encryption info and associated current stream context. If
7630  * we are parsing a track fragment, this will return the specific encryption
7631  * info for this fragment; otherwise this will return the global encryption
7632  * info for the current stream.
7633  */
7635 {
7636  MOVFragmentStreamInfo *frag_stream_info;
7637  AVStream *st;
7638  int i;
7639 
7640  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
7641  if (frag_stream_info) {
7642  for (i = 0; i < c->fc->nb_streams; i++) {
7643  *sc = c->fc->streams[i]->priv_data;
7644  if ((*sc)->id == frag_stream_info->id) {
7645  st = c->fc->streams[i];
7646  break;
7647  }
7648  }
7649  if (i == c->fc->nb_streams)
7650  return 0;
7651  *sc = st->priv_data;
7652 
7653  if (!frag_stream_info->encryption_index) {
7654  // If this stream isn't encrypted, don't create the index.
7655  if (!(*sc)->cenc.default_encrypted_sample)
7656  return 0;
7657  frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7658  if (!frag_stream_info->encryption_index)
7659  return AVERROR(ENOMEM);
7660  }
7661  *encryption_index = frag_stream_info->encryption_index;
7662  return 1;
7663  } else {
7664  // No current track fragment, using stream level encryption info.
7665 
7666  if (c->fc->nb_streams < 1)
7667  return 0;
7668  st = c->fc->streams[c->fc->nb_streams - 1];
7669  *sc = st->priv_data;
7670 
7671  if (!(*sc)->cenc.encryption_index) {
7672  // If this stream isn't encrypted, don't create the index.
7673  if (!(*sc)->cenc.default_encrypted_sample)
7674  return 0;
7675  (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7676  if (!(*sc)->cenc.encryption_index)
7677  return AVERROR(ENOMEM);
7678  }
7679 
7680  *encryption_index = (*sc)->cenc.encryption_index;
7681  return 1;
7682  }
7683 }
7684 
7686 {
7687  int i, ret;
7688  unsigned int subsample_count;
7689  AVSubsampleEncryptionInfo *subsamples;
7690 
7691  if (!sc->cenc.default_encrypted_sample) {
7692  av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
7693  return AVERROR_INVALIDDATA;
7694  }
7695 
7696  if (sc->cenc.per_sample_iv_size || use_subsamples) {
7698  if (!*sample)
7699  return AVERROR(ENOMEM);
7700  } else
7701  *sample = NULL;
7702 
7703  if (sc->cenc.per_sample_iv_size != 0) {
7704  if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
7705  av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
7707  *sample = NULL;
7708  return ret;
7709  }
7710  }
7711 
7712  if (use_subsamples) {
7713  subsample_count = avio_rb16(pb);
7714  av_free((*sample)->subsamples);
7715  (*sample)->subsamples = av_calloc(subsample_count, sizeof(*subsamples));
7716  if (!(*sample)->subsamples) {
7718  *sample = NULL;
7719  return AVERROR(ENOMEM);
7720  }
7721 
7722  for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
7723  (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
7724  (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
7725  }
7726 
7727  if (pb->eof_reached) {
7728  av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
7730  *sample = NULL;
7731  return AVERROR_INVALIDDATA;
7732  }
7733  (*sample)->subsample_count = subsample_count;
7734  }
7735 
7736  return 0;
7737 }
7738 
7740 {
7741  AVEncryptionInfo **encrypted_samples;
7742  MOVEncryptionIndex *encryption_index;
7743  MOVStreamContext *sc;
7744  int use_subsamples, ret;
7745  unsigned int sample_count, i, alloc_size = 0;
7746 
7747  ret = get_current_encryption_info(c, &encryption_index, &sc);
7748  if (ret != 1)
7749  return ret;
7750 
7751  if (encryption_index->nb_encrypted_samples) {
7752  // This can happen if we have both saio/saiz and senc atoms.
7753  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
7754  return 0;
7755  }
7756 
7757  avio_r8(pb); /* version */
7758  use_subsamples = avio_rb24(pb) & 0x02; /* flags */
7759 
7760  sample_count = avio_rb32(pb);
7761  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7762  return AVERROR(ENOMEM);
7763 
7764  for (i = 0; i < sample_count; i++) {
7765  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7766  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7767  min_samples * sizeof(*encrypted_samples));
7768  if (encrypted_samples) {
7769  encryption_index->encrypted_samples = encrypted_samples;
7770 
7772  c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
7773  } else {
7774  ret = AVERROR(ENOMEM);
7775  }
7776  if (pb->eof_reached) {
7777  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
7778  if (ret >= 0)
7779  av_encryption_info_free(encryption_index->encrypted_samples[i]);
7781  }
7782 
7783  if (ret < 0) {
7784  for (; i > 0; i--)
7785  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7786  av_freep(&encryption_index->encrypted_samples);
7787  return ret;
7788  }
7789  }
7790  encryption_index->nb_encrypted_samples = sample_count;
7791 
7792  return 0;
7793 }
7794 
7796 {
7797  AVEncryptionInfo **sample, **encrypted_samples;
7798  int64_t prev_pos;
7799  size_t sample_count, sample_info_size, i;
7800  int ret = 0;
7801  unsigned int alloc_size = 0;
7802 
7803  if (encryption_index->nb_encrypted_samples)
7804  return 0;
7805  sample_count = encryption_index->auxiliary_info_sample_count;
7806  if (encryption_index->auxiliary_offsets_count != 1) {
7807  av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
7808  return AVERROR_PATCHWELCOME;
7809  }
7810  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7811  return AVERROR(ENOMEM);
7812 
7813  prev_pos = avio_tell(pb);
7814  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
7815  avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
7816  av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
7817  goto finish;
7818  }
7819 
7820  for (i = 0; i < sample_count && !pb->eof_reached; i++) {
7821  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7822  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7823  min_samples * sizeof(*encrypted_samples));
7824  if (!encrypted_samples) {
7825  ret = AVERROR(ENOMEM);
7826  goto finish;
7827  }
7828  encryption_index->encrypted_samples = encrypted_samples;
7829 
7830  sample = &encryption_index->encrypted_samples[i];
7831  sample_info_size = encryption_index->auxiliary_info_default_size
7832  ? encryption_index->auxiliary_info_default_size
7833  : encryption_index->auxiliary_info_sizes[i];
7834 
7835  ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
7836  if (ret < 0)
7837  goto finish;
7838  }
7839  if (pb->eof_reached) {
7840  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
7842  } else {
7843  encryption_index->nb_encrypted_samples = sample_count;
7844  }
7845 
7846 finish:
7847  avio_seek(pb, prev_pos, SEEK_SET);
7848  if (ret < 0) {
7849  for (; i > 0; i--) {
7850  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7851  }
7852  av_freep(&encryption_index->encrypted_samples);
7853  }
7854  return ret;
7855 }
7856 
7858 {
7859  MOVEncryptionIndex *encryption_index;
7860  MOVStreamContext *sc;
7861  int ret;
7862  unsigned int sample_count, aux_info_type, aux_info_param;
7863 
7864  ret = get_current_encryption_info(c, &encryption_index, &sc);
7865  if (ret != 1)
7866  return ret;
7867 
7868  if (encryption_index->nb_encrypted_samples) {
7869  // This can happen if we have both saio/saiz and senc atoms.
7870  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
7871  return 0;
7872  }
7873 
7874  if (encryption_index->auxiliary_info_sample_count) {
7875  av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
7876  return AVERROR_INVALIDDATA;
7877  }
7878 
7879  avio_r8(pb); /* version */
7880  if (avio_rb24(pb) & 0x01) { /* flags */
7881  aux_info_type = avio_rb32(pb);
7882  aux_info_param = avio_rb32(pb);
7883  if (sc->cenc.default_encrypted_sample) {
7884  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7885  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
7886  return 0;
7887  }
7888  if (aux_info_param != 0) {
7889  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
7890  return 0;
7891  }
7892  } else {
7893  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7894  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7895  aux_info_type == MKBETAG('c','e','n','s') ||
7896  aux_info_type == MKBETAG('c','b','c','1') ||
7897  aux_info_type == MKBETAG('c','b','c','s')) &&
7898  aux_info_param == 0) {
7899  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
7900  return AVERROR_INVALIDDATA;
7901  } else {
7902  return 0;
7903  }
7904  }
7905  } else if (!sc->cenc.default_encrypted_sample) {
7906  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7907  return 0;
7908  }
7909 
7910  encryption_index->auxiliary_info_default_size = avio_r8(pb);
7911  sample_count = avio_rb32(pb);
7912 
7913  if (encryption_index->auxiliary_info_default_size == 0) {
7914  if (sample_count == 0)
7915  return AVERROR_INVALIDDATA;
7916 
7917  encryption_index->auxiliary_info_sizes = av_malloc(sample_count);
7918  if (!encryption_index->auxiliary_info_sizes)
7919  return AVERROR(ENOMEM);
7920 
7921  ret = avio_read(pb, encryption_index->auxiliary_info_sizes, sample_count);
7922  if (ret != sample_count) {
7923  av_freep(&encryption_index->auxiliary_info_sizes);
7924 
7925  if (ret >= 0)
7927  av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info, %s\n",
7928  av_err2str(ret));
7929  return ret;
7930  }
7931  }
7932  encryption_index->auxiliary_info_sample_count = sample_count;
7933 
7934  if (encryption_index->auxiliary_offsets_count) {
7935  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
7936  }
7937 
7938  return 0;
7939 }
7940 
7942 {
7943  uint64_t *auxiliary_offsets;
7944  MOVEncryptionIndex *encryption_index;
7945  MOVStreamContext *sc;
7946  int i, ret;
7947  unsigned int version, entry_count, aux_info_type, aux_info_param;
7948  unsigned int alloc_size = 0;
7949 
7950  ret = get_current_encryption_info(c, &encryption_index, &sc);
7951  if (ret != 1)
7952  return ret;
7953 
7954  if (encryption_index->nb_encrypted_samples) {
7955  // This can happen if we have both saio/saiz and senc atoms.
7956  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
7957  return 0;
7958  }
7959 
7960  if (encryption_index->auxiliary_offsets_count) {
7961  av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
7962  return AVERROR_INVALIDDATA;
7963  }
7964 
7965  version = avio_r8(pb); /* version */
7966  if (avio_rb24(pb) & 0x01) { /* flags */
7967  aux_info_type = avio_rb32(pb);
7968  aux_info_param = avio_rb32(pb);
7969  if (sc->cenc.default_encrypted_sample) {
7970  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7971  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
7972  return 0;
7973  }
7974  if (aux_info_param != 0) {
7975  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
7976  return 0;
7977  }
7978  } else {
7979  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7980  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7981  aux_info_type == MKBETAG('c','e','n','s') ||
7982  aux_info_type == MKBETAG('c','b','c','1') ||
7983  aux_info_type == MKBETAG('c','b','c','s')) &&
7984  aux_info_param == 0) {
7985  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
7986  return AVERROR_INVALIDDATA;
7987  } else {
7988  return 0;
7989  }
7990  }
7991  } else if (!sc->cenc.default_encrypted_sample) {
7992  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7993  return 0;
7994  }
7995 
7996  entry_count = avio_rb32(pb);
7997  if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
7998  return AVERROR(ENOMEM);
7999 
8000  for (i = 0; i < entry_count && !pb->eof_reached; i++) {
8001  unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
8002  auxiliary_offsets = av_fast_realloc(
8003  encryption_index->auxiliary_offsets, &alloc_size,
8004  min_offsets * sizeof(*auxiliary_offsets));
8005  if (!auxiliary_offsets) {
8006  av_freep(&encryption_index->auxiliary_offsets);
8007  return AVERROR(ENOMEM);
8008  }
8009  encryption_index->auxiliary_offsets = auxiliary_offsets;
8010 
8011  if (version == 0) {
8012  encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
8013  } else {
8014  encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
8015  }
8016  if (c->frag_index.current >= 0) {
8017  encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
8018  }
8019  }
8020 
8021  if (pb->eof_reached) {
8022  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
8023  av_freep(&encryption_index->auxiliary_offsets);
8024  return AVERROR_INVALIDDATA;
8025  }
8026 
8027  encryption_index->auxiliary_offsets_count = entry_count;
8028 
8029  if (encryption_index->auxiliary_info_sample_count) {
8030  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
8031  }
8032 
8033  return 0;
8034 }
8035 
8037 {
8038  AVEncryptionInitInfo *info, *old_init_info;
8039  uint8_t **key_ids;
8040  AVStream *st;
8041  const AVPacketSideData *old_side_data;
8042  uint8_t *side_data, *extra_data;
8043  size_t side_data_size;
8044  int ret = 0;
8045  unsigned int version, kid_count, extra_data_size, alloc_size = 0;
8046 
8047  if (c->fc->nb_streams < 1)
8048  return 0;
8049  st = c->fc->streams[c->fc->nb_streams-1];
8050 
8051  version = avio_r8(pb); /* version */
8052  avio_rb24(pb); /* flags */
8053 
8054  info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
8055  /* key_id_size */ 16, /* data_size */ 0);
8056  if (!info)
8057  return AVERROR(ENOMEM);
8058 
8059  if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
8060  av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
8061  goto finish;
8062  }
8063 
8064  if (version > 0) {
8065  kid_count = avio_rb32(pb);
8066  if (kid_count >= INT_MAX / sizeof(*key_ids)) {
8067  ret = AVERROR(ENOMEM);
8068  goto finish;
8069  }
8070 
8071  for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
8072  unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
8073  key_ids = av_fast_realloc(info->key_ids, &alloc_size,
8074  min_kid_count * sizeof(*key_ids));
8075  if (!key_ids) {
8076  ret = AVERROR(ENOMEM);
8077  goto finish;
8078  }
8079  info->key_ids = key_ids;
8080 
8081  info->key_ids[i] = av_mallocz(16);
8082  if (!info->key_ids[i]) {
8083  ret = AVERROR(ENOMEM);
8084  goto finish;
8085  }
8086  info->num_key_ids = i + 1;
8087 
8088  if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
8089  av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
8090  goto finish;
8091  }
8092  }
8093 
8094  if (pb->eof_reached) {
8095  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
8097  goto finish;
8098  }
8099  }
8100 
8101  extra_data_size = avio_rb32(pb);
8102  extra_data = av_malloc(extra_data_size);
8103  if (!extra_data) {
8104  ret = AVERROR(ENOMEM);
8105  goto finish;
8106  }
8107  ret = avio_read(pb, extra_data, extra_data_size);
8108  if (ret != extra_data_size) {
8109  av_free(extra_data);
8110 
8111  if (ret >= 0)
8113  goto finish;
8114  }
8115 
8116  av_freep(&info->data); // malloc(0) may still allocate something.
8117  info->data = extra_data;
8118  info->data_size = extra_data_size;
8119 
8120  // If there is existing initialization data, append to the list.
8123  if (old_side_data) {
8124  old_init_info = av_encryption_init_info_get_side_data(old_side_data->data, old_side_data->size);
8125  if (old_init_info) {
8126  // Append to the end of the list.
8127  for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
8128  if (!cur->next) {
8129  cur->next = info;
8130  break;
8131  }
8132  }
8133  info = old_init_info;
8134  } else {
8135  // Assume existing side-data will be valid, so the only error we could get is OOM.
8136  ret = AVERROR(ENOMEM);
8137  goto finish;
8138  }
8139  }
8140 
8141  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
8142  if (!side_data) {
8143  ret = AVERROR(ENOMEM);
8144  goto finish;
8145  }
8149  side_data, side_data_size, 0))
8150  av_free(side_data);
8151 
8152 finish:
8154  return ret;
8155 }
8156 
8158 {
8159  AVStream *st;
8160  MOVStreamContext *sc;
8161 
8162  if (c->fc->nb_streams < 1)
8163  return 0;
8164  st = c->fc->streams[c->fc->nb_streams-1];
8165  sc = st->priv_data;
8166 
8167  if (sc->pseudo_stream_id != 0) {
8168  av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
8169  return AVERROR_PATCHWELCOME;
8170  }
8171 
8172  if (atom.size < 8)
8173  return AVERROR_INVALIDDATA;
8174 
8175  avio_rb32(pb); /* version and flags */
8176 
8177  if (!sc->cenc.default_encrypted_sample) {
8179  if (!sc->cenc.default_encrypted_sample) {
8180  return AVERROR(ENOMEM);
8181  }
8182  }
8183 
8185  return 0;
8186 }
8187 
8189 {
8190  AVStream *st;
8191  MOVStreamContext *sc;
8192  unsigned int version, pattern, is_protected, iv_size;
8193 
8194  if (c->fc->nb_streams < 1)
8195  return 0;
8196  st = c->fc->streams[c->fc->nb_streams-1];
8197  sc = st->priv_data;
8198 
8199  if (sc->pseudo_stream_id != 0) {
8200  av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
8201  return AVERROR_PATCHWELCOME;
8202  }
8203 
8204  if (!sc->cenc.default_encrypted_sample) {
8206  if (!sc->cenc.default_encrypted_sample) {
8207  return AVERROR(ENOMEM);
8208  }
8209  }
8210 
8211  if (atom.size < 20)
8212  return AVERROR_INVALIDDATA;
8213 
8214  version = avio_r8(pb); /* version */
8215  avio_rb24(pb); /* flags */
8216 
8217  avio_r8(pb); /* reserved */
8218  pattern = avio_r8(pb);
8219 
8220  if (version > 0) {
8221  sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
8222  sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
8223  }
8224 
8225  is_protected = avio_r8(pb);
8226  if (is_protected && !sc->cenc.encryption_index) {
8227  // The whole stream should be by-default encrypted.
8229  if (!sc->cenc.encryption_index)
8230  return AVERROR(ENOMEM);
8231  }
8232  sc->cenc.per_sample_iv_size = avio_r8(pb);
8233  if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
8234  sc->cenc.per_sample_iv_size != 16) {
8235  av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
8236  return AVERROR_INVALIDDATA;
8237  }
8238  if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
8239  av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
8240  return AVERROR_INVALIDDATA;
8241  }
8242 
8243  if (is_protected && !sc->cenc.per_sample_iv_size) {
8244  iv_size = avio_r8(pb);
8245  if (iv_size != 8 && iv_size != 16) {
8246  av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
8247  return AVERROR_INVALIDDATA;
8248  }
8249 
8250  if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
8251  av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
8252  return AVERROR_INVALIDDATA;
8253  }
8254  }
8255 
8256  return 0;
8257 }
8258 
8260 {
8261  AVStream *st;
8262  int last, type, size, ret;
8263  uint8_t buf[4];
8264 
8265  if (c->fc->nb_streams < 1)
8266  return 0;
8267  st = c->fc->streams[c->fc->nb_streams-1];
8268 
8269  if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
8270  return AVERROR_INVALIDDATA;
8271 
8272  /* Check FlacSpecificBox version. */
8273  if (avio_r8(pb) != 0)
8274  return AVERROR_INVALIDDATA;
8275 
8276  avio_rb24(pb); /* Flags */
8277 
8278  if (avio_read(pb, buf, sizeof(buf)) != sizeof(buf)) {
8279  av_log(c->fc, AV_LOG_ERROR, "failed to read FLAC metadata block header\n");
8280  return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
8281  }
8282  flac_parse_block_header(buf, &last, &type, &size);
8283 
8285  av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
8286  return AVERROR_INVALIDDATA;
8287  }
8288 
8289  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
8290  if (ret < 0)
8291  return ret;
8292 
8293  if (!last)
8294  av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
8295 
8296  return 0;
8297 }
8298 
8299 static int get_key_from_kid(uint8_t* out, int len, MOVContext *c, AVEncryptionInfo *sample) {
8300  AVDictionaryEntry *key_entry_hex;
8301  char kid_hex[16*2+1];
8302 
8303  if (c->decryption_default_key && c->decryption_default_key_len != len) {
8304  av_log(c->fc, AV_LOG_ERROR, "invalid default decryption key length: got %d, expected %d\n", c->decryption_default_key_len, len);
8305  return -1;
8306  }
8307 
8308  if (!c->decryption_keys) {
8309  av_assert0(c->decryption_default_key);
8310  memcpy(out, c->decryption_default_key, len);
8311  return 0;
8312  }
8313 
8314  if (sample->key_id_size != 16) {
8315  av_log(c->fc, AV_LOG_ERROR, "invalid key ID size: got %u, expected 16\n", sample->key_id_size);
8316  return -1;
8317  }
8318 
8319  ff_data_to_hex(kid_hex, sample->key_id, 16, 1);
8320  key_entry_hex = av_dict_get(c->decryption_keys, kid_hex, NULL, AV_DICT_DONT_STRDUP_KEY|AV_DICT_DONT_STRDUP_VAL);
8321  if (!key_entry_hex) {
8322  if (!c->decryption_default_key) {
8323  av_log(c->fc, AV_LOG_ERROR, "unable to find KID %s\n", kid_hex);
8324  return -1;
8325  }
8326  memcpy(out, c->decryption_default_key, len);
8327  return 0;
8328  }
8329  if (strlen(key_entry_hex->value) != len*2) {
8330  return -1;
8331  }
8332  ff_hex_to_data(out, key_entry_hex->value);
8333  return 0;
8334 }
8335 
8337 {
8338  int i, ret;
8339  int bytes_of_protected_data;
8340  uint8_t decryption_key[AES_CTR_KEY_SIZE];
8341 
8342  if (!sc->cenc.aes_ctr) {
8343  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8344  if (ret < 0) {
8345  return ret;
8346  }
8347 
8348  /* initialize the cipher */
8349  sc->cenc.aes_ctr = av_aes_ctr_alloc();
8350  if (!sc->cenc.aes_ctr) {
8351  return AVERROR(ENOMEM);
8352  }
8353 
8354  ret = av_aes_ctr_init(sc->cenc.aes_ctr, decryption_key);
8355  if (ret < 0) {
8356  return ret;
8357  }
8358  }
8359 
8361 
8362  if (!sample->subsample_count) {
8363  /* decrypt the whole packet */
8365  return 0;
8366  }
8367 
8368  for (i = 0; i < sample->subsample_count; i++) {
8369  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8370  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8371  return AVERROR_INVALIDDATA;
8372  }
8373 
8374  /* skip the clear bytes */
8375  input += sample->subsamples[i].bytes_of_clear_data;
8376  size -= sample->subsamples[i].bytes_of_clear_data;
8377 
8378  /* decrypt the encrypted bytes */
8379 
8380  bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data;
8381  av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data);
8382 
8383  input += bytes_of_protected_data;
8384  size -= bytes_of_protected_data;
8385  }
8386 
8387  if (size > 0) {
8388  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8389  return AVERROR_INVALIDDATA;
8390  }
8391 
8392  return 0;
8393 }
8394 
8396 {
8397  int i, ret;
8398  int num_of_encrypted_blocks;
8399  uint8_t iv[16];
8400  uint8_t decryption_key[16];
8401 
8402  if (!sc->cenc.aes_ctx) {
8403  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8404  if (ret < 0) {
8405  return ret;
8406  }
8407 
8408  /* initialize the cipher */
8409  sc->cenc.aes_ctx = av_aes_alloc();
8410  if (!sc->cenc.aes_ctx) {
8411  return AVERROR(ENOMEM);
8412  }
8413 
8414  ret = av_aes_init(sc->cenc.aes_ctx, decryption_key, 16 * 8, 1);
8415  if (ret < 0) {
8416  return ret;
8417  }
8418  }
8419 
8420  memcpy(iv, sample->iv, 16);
8421 
8422  /* whole-block full sample encryption */
8423  if (!sample->subsample_count) {
8424  /* decrypt the whole packet */
8425  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8426  return 0;
8427  }
8428 
8429  for (i = 0; i < sample->subsample_count; i++) {
8430  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8431  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8432  return AVERROR_INVALIDDATA;
8433  }
8434 
8435  if (sample->subsamples[i].bytes_of_protected_data % 16) {
8436  av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is not a multiple of 16\n");
8437  return AVERROR_INVALIDDATA;
8438  }
8439 
8440  /* skip the clear bytes */
8441  input += sample->subsamples[i].bytes_of_clear_data;
8442  size -= sample->subsamples[i].bytes_of_clear_data;
8443 
8444  /* decrypt the encrypted bytes */
8445  num_of_encrypted_blocks = sample->subsamples[i].bytes_of_protected_data/16;
8446  if (num_of_encrypted_blocks > 0) {
8447  av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1);
8448  }
8449  input += sample->subsamples[i].bytes_of_protected_data;
8450  size -= sample->subsamples[i].bytes_of_protected_data;
8451  }
8452 
8453  if (size > 0) {
8454  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8455  return AVERROR_INVALIDDATA;
8456  }
8457 
8458  return 0;
8459 }
8460 
8462 {
8463  int i, ret, rem_bytes;
8464  uint8_t *data;
8465  uint8_t decryption_key[AES_CTR_KEY_SIZE];
8466 
8467  if (!sc->cenc.aes_ctr) {
8468  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8469  if (ret < 0) {
8470  return ret;
8471  }
8472 
8473  /* initialize the cipher */
8474  sc->cenc.aes_ctr = av_aes_ctr_alloc();
8475  if (!sc->cenc.aes_ctr) {
8476  return AVERROR(ENOMEM);
8477  }
8478 
8479  ret = av_aes_ctr_init(sc->cenc.aes_ctr, decryption_key);
8480  if (ret < 0) {
8481  return ret;
8482  }
8483  }
8484 
8486 
8487  /* whole-block full sample encryption */
8488  if (!sample->subsample_count) {
8489  /* decrypt the whole packet */
8491  return 0;
8492  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8493  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cens' scheme\n");
8494  return AVERROR_INVALIDDATA;
8495  }
8496 
8497  for (i = 0; i < sample->subsample_count; i++) {
8498  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8499  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8500  return AVERROR_INVALIDDATA;
8501  }
8502 
8503  /* skip the clear bytes */
8504  input += sample->subsamples[i].bytes_of_clear_data;
8505  size -= sample->subsamples[i].bytes_of_clear_data;
8506 
8507  /* decrypt the encrypted bytes */
8508  data = input;
8509  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8510  while (rem_bytes > 0) {
8511  if (rem_bytes < 16*sample->crypt_byte_block) {
8512  break;
8513  }
8514  av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block);
8515  data += 16*sample->crypt_byte_block;
8516  rem_bytes -= 16*sample->crypt_byte_block;
8517  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8518  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8519  }
8520  input += sample->subsamples[i].bytes_of_protected_data;
8521  size -= sample->subsamples[i].bytes_of_protected_data;
8522  }
8523 
8524  if (size > 0) {
8525  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8526  return AVERROR_INVALIDDATA;
8527  }
8528 
8529  return 0;
8530 }
8531 
8533 {
8534  int i, ret, rem_bytes;
8535  uint8_t iv[16];
8536  uint8_t *data;
8537  uint8_t decryption_key[16];
8538 
8539  if (!sc->cenc.aes_ctx) {
8540  ret = get_key_from_kid(decryption_key, sizeof(decryption_key), c, sample);
8541  if (ret < 0) {
8542  return ret;
8543  }
8544 
8545  /* initialize the cipher */
8546  sc->cenc.aes_ctx = av_aes_alloc();
8547  if (!sc->cenc.aes_ctx) {
8548  return AVERROR(ENOMEM);
8549  }
8550 
8551  ret = av_aes_init(sc->cenc.aes_ctx, decryption_key, 16 * 8, 1);
8552  if (ret < 0) {
8553  return ret;
8554  }
8555  }
8556 
8557  /* whole-block full sample encryption */
8558  if (!sample->subsample_count) {
8559  /* decrypt the whole packet */
8560  memcpy(iv, sample->iv, 16);
8561  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8562  return 0;
8563  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8564  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cbcs' scheme\n");
8565  return AVERROR_INVALIDDATA;
8566  }
8567 
8568  for (i = 0; i < sample->subsample_count; i++) {
8569  if (sample->subsamples[i].bytes_of_clear_data + (int64_t)sample->subsamples[i].bytes_of_protected_data > size) {
8570  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8571  return AVERROR_INVALIDDATA;
8572  }
8573 
8574  /* skip the clear bytes */
8575  input += sample->subsamples[i].bytes_of_clear_data;
8576  size -= sample->subsamples[i].bytes_of_clear_data;
8577 
8578  /* decrypt the encrypted bytes */
8579  memcpy(iv, sample->iv, 16);
8580  data = input;
8581  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8582  while (rem_bytes > 0) {
8583  if (rem_bytes < 16*sample->crypt_byte_block) {
8584  break;
8585  }
8586  av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1);
8587  data += 16*sample->crypt_byte_block;
8588  rem_bytes -= 16*sample->crypt_byte_block;
8589  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8590  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8591  }
8592  input += sample->subsamples[i].bytes_of_protected_data;
8593  size -= sample->subsamples[i].bytes_of_protected_data;
8594  }
8595 
8596  if (size > 0) {
8597  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8598  return AVERROR_INVALIDDATA;
8599  }
8600 
8601  return 0;
8602 }
8603 
8605 {
8606  if (sample->scheme == MKBETAG('c','e','n','c') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8607  return cenc_scheme_decrypt(c, sc, sample, input, size);
8608  } else if (sample->scheme == MKBETAG('c','b','c','1') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8609  return cbc1_scheme_decrypt(c, sc, sample, input, size);
8610  } else if (sample->scheme == MKBETAG('c','e','n','s')) {
8611  return cens_scheme_decrypt(c, sc, sample, input, size);
8612  } else if (sample->scheme == MKBETAG('c','b','c','s')) {
8613  return cbcs_scheme_decrypt(c, sc, sample, input, size);
8614  } else {
8615  av_log(c->fc, AV_LOG_ERROR, "invalid encryption scheme\n");
8616  return AVERROR_INVALIDDATA;
8617  }
8618 }
8619 
8621 {
8622  int current = frag_index->current;
8623 
8624  if (!frag_index->nb_items)
8625  return NULL;
8626 
8627  // Check frag_index->current is the right one for pkt. It can out of sync.
8628  if (current >= 0 && current < frag_index->nb_items) {
8629  if (frag_index->item[current].moof_offset < pkt->pos &&
8630  (current + 1 == frag_index->nb_items ||
8631  frag_index->item[current + 1].moof_offset > pkt->pos))
8632  return get_frag_stream_info(frag_index, current, id);
8633  }
8634 
8635 
8636  for (int i = 0; i < frag_index->nb_items; i++) {
8637  if (frag_index->item[i].moof_offset > pkt->pos)
8638  break;
8639  current = i;
8640  }
8641  frag_index->current = current;
8642  return get_frag_stream_info(frag_index, current, id);
8643 }
8644 
8645 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
8646 {
8647  MOVFragmentStreamInfo *frag_stream_info;
8648  MOVEncryptionIndex *encryption_index;
8649  AVEncryptionInfo *encrypted_sample;
8650  int encrypted_index, ret;
8651 
8652  frag_stream_info = get_frag_stream_info_from_pkt(&mov->frag_index, pkt, sc->id);
8653  encrypted_index = current_index;
8654  encryption_index = NULL;
8655  if (frag_stream_info) {
8656  // Note this only supports encryption info in the first sample descriptor.
8657  if (frag_stream_info->stsd_id == 1) {
8658  if (frag_stream_info->encryption_index) {
8659  encrypted_index = current_index - frag_stream_info->index_base;
8660  encryption_index = frag_stream_info->encryption_index;
8661  } else {
8662  encryption_index = sc->cenc.encryption_index;
8663  }
8664  }
8665  } else {
8666  encryption_index = sc->cenc.encryption_index;
8667  }
8668 
8669  if (encryption_index) {
8670  if (encryption_index->auxiliary_info_sample_count &&
8671  !encryption_index->nb_encrypted_samples) {
8672  av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
8673  return AVERROR_INVALIDDATA;
8674  }
8675  if (encryption_index->auxiliary_offsets_count &&
8676  !encryption_index->nb_encrypted_samples) {
8677  av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
8678  return AVERROR_INVALIDDATA;
8679  }
8680 
8681  encrypted_sample = NULL;
8682  if (!encryption_index->nb_encrypted_samples) {
8683  // Full-sample encryption with default settings.
8684  encrypted_sample = sc->cenc.default_encrypted_sample;
8685  } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
8686  // Per-sample setting override.
8687  encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
8688  if (!encrypted_sample) {
8689  encrypted_sample = sc->cenc.default_encrypted_sample;
8690  }
8691  }
8692 
8693  if (!encrypted_sample) {
8694  av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
8695  return AVERROR_INVALIDDATA;
8696  }
8697 
8698  if (mov->decryption_keys || mov->decryption_default_key) {
8699  return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
8700  } else {
8701  size_t size;
8702  uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
8703  if (!side_data)
8704  return AVERROR(ENOMEM);
8706  if (ret < 0)
8707  av_free(side_data);
8708  return ret;
8709  }
8710  }
8711 
8712  return 0;
8713 }
8714 
8716 {
8717  const int OPUS_SEEK_PREROLL_MS = 80;
8718  int ret;
8719  AVStream *st;
8720  size_t size;
8721  uint16_t pre_skip;
8722 
8723  if (c->fc->nb_streams < 1)
8724  return 0;
8725  st = c->fc->streams[c->fc->nb_streams-1];
8726 
8727  if ((uint64_t)atom.size > (1<<30) || atom.size < 11 || st->codecpar->extradata)
8728  return AVERROR_INVALIDDATA;
8729 
8730  /* Check OpusSpecificBox version. */
8731  if (avio_r8(pb) != 0) {
8732  av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
8733  return AVERROR_INVALIDDATA;
8734  }
8735 
8736  /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
8737  size = atom.size + 8;
8738 
8739  if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
8740  return ret;
8741 
8742  AV_WL32A(st->codecpar->extradata, MKTAG('O','p','u','s'));
8743  AV_WL32A(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
8744  AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
8745  if ((ret = ffio_read_size(pb, st->codecpar->extradata + 9, size - 9)) < 0) {
8746  av_freep(&st->codecpar->extradata);
8747  st->codecpar->extradata_size = 0;
8748  return ret;
8749  }
8750 
8751  /* OpusSpecificBox is stored in big-endian, but OpusHead is
8752  little-endian; aside from the preceding magic and version they're
8753  otherwise currently identical. Data after output gain at offset 16
8754  doesn't need to be bytewapped. */
8755  pre_skip = AV_RB16A(st->codecpar->extradata + 10);
8756  AV_WL16A(st->codecpar->extradata + 10, pre_skip);
8757  AV_WL32A(st->codecpar->extradata + 12, AV_RB32A(st->codecpar->extradata + 12));
8758  AV_WL16A(st->codecpar->extradata + 16, AV_RB16A(st->codecpar->extradata + 16));
8759 
8760  st->codecpar->initial_padding = pre_skip;
8762  (AVRational){1, 1000},
8763  (AVRational){1, 48000});
8764 
8765  return 0;
8766 }
8767 
8769 {
8770  AVStream *st;
8771  unsigned format_info;
8772  int channel_assignment, channel_assignment1, channel_assignment2;
8773  int ratebits;
8774  uint64_t chmask;
8775 
8776  if (c->fc->nb_streams < 1)
8777  return 0;
8778  st = c->fc->streams[c->fc->nb_streams-1];
8779 
8780  if (atom.size < 10)
8781  return AVERROR_INVALIDDATA;
8782 
8783  format_info = avio_rb32(pb);
8784 
8785  ratebits = (format_info >> 28) & 0xF;
8786  channel_assignment1 = (format_info >> 15) & 0x1F;
8787  channel_assignment2 = format_info & 0x1FFF;
8788  if (channel_assignment2)
8789  channel_assignment = channel_assignment2;
8790  else
8791  channel_assignment = channel_assignment1;
8792 
8793  st->codecpar->frame_size = 40 << (ratebits & 0x7);
8794  st->codecpar->sample_rate = mlp_samplerate(ratebits);
8795 
8797  chmask = truehd_layout(channel_assignment);
8799 
8800  return 0;
8801 }
8802 
8804 {
8805  AVStream *st;
8806  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
8807  int ret;
8808  int64_t read_size = atom.size;
8809 
8810  if (c->fc->nb_streams < 1)
8811  return 0;
8812  st = c->fc->streams[c->fc->nb_streams-1];
8813 
8814  // At most 24 bytes
8815  read_size = FFMIN(read_size, ISOM_DVCC_DVVC_SIZE);
8816 
8817  if ((ret = ffio_read_size(pb, buf, read_size)) < 0)
8818  return ret;
8819 
8820  return ff_isom_parse_dvcc_dvvc(c->fc, st, buf, read_size);
8821 }
8822 
8824 {
8825  AVStream *st;
8826  AVPacketSideData *sd;
8827  int ret;
8828 
8829  if (c->fc->nb_streams < 1)
8830  return 0;
8831  st = c->fc->streams[c->fc->nb_streams - 1];
8832 
8833  if (atom.size < 23 || atom.size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
8834  return AVERROR_INVALIDDATA;
8835 
8839  atom.size, 0);
8840  if (!sd)
8841  return AVERROR(ENOMEM);
8842 
8843  ret = ffio_read_size(pb, sd->data, atom.size);
8844  if (ret < 0)
8845  return ret;
8846 
8847  return 0;
8848 }
8849 
8851 {
8852  AVStream *st;
8853  uint8_t *buf;
8854  int ret, old_size, num_arrays;
8855 
8856  if (c->fc->nb_streams < 1)
8857  return 0;
8858  st = c->fc->streams[c->fc->nb_streams-1];
8859 
8860  if (!st->codecpar->extradata_size)
8861  // TODO: handle lhvC when present before hvcC
8862  return 0;
8863 
8864  if (atom.size < 6 || st->codecpar->extradata_size < 23 ||
8865  atom.size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
8866  return AVERROR_INVALIDDATA;
8867  }
8868 
8870  if (!buf)
8871  return AVERROR(ENOMEM);
8872  memset(buf + atom.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
8873 
8874  ret = ffio_read_size(pb, buf, atom.size);
8875  if (ret < 0) {
8876  av_free(buf);
8877  av_log(c->fc, AV_LOG_WARNING, "lhvC atom truncated\n");
8878  return 0;
8879  }
8880 
8881  num_arrays = buf[5];
8882  old_size = st->codecpar->extradata_size;
8883  atom.size -= 8 /* account for mov_realloc_extradata offsetting */
8884  + 6 /* lhvC bytes before the arrays*/;
8885 
8886  ret = mov_realloc_extradata(st->codecpar, atom);
8887  if (ret < 0) {
8888  av_free(buf);
8889  return ret;
8890  }
8891 
8892  st->codecpar->extradata[22] += num_arrays;
8893  memcpy(st->codecpar->extradata + old_size, buf + 6, atom.size + 8);
8894 
8896 
8897  av_free(buf);
8898  return 0;
8899 }
8900 
8902 {
8903  AVFormatContext *ctx = c->fc;
8904  AVStream *st = NULL;
8905  AVBPrint scheme_buf, value_buf;
8906  int64_t scheme_str_len = 0, value_str_len = 0;
8907  int version, flags, ret = AVERROR_BUG;
8908  int64_t size = atom.size;
8909 
8910  if (atom.size < 6)
8911  // 4 bytes for version + flags, 2x 1 byte for null
8912  return AVERROR_INVALIDDATA;
8913 
8914  if (c->fc->nb_streams < 1)
8915  return 0;
8916  st = c->fc->streams[c->fc->nb_streams-1];
8917 
8918  version = avio_r8(pb);
8919  flags = avio_rb24(pb);
8920  size -= 4;
8921 
8922  if (version != 0 || flags != 0) {
8924  "Unsupported 'kind' box with version %d, flags: %x",
8925  version, flags);
8926  return AVERROR_INVALIDDATA;
8927  }
8928 
8929  av_bprint_init(&scheme_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8930  av_bprint_init(&value_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8931 
8932  if ((scheme_str_len = ff_read_string_to_bprint_overwrite(pb, &scheme_buf,
8933  size)) < 0) {
8934  ret = scheme_str_len;
8935  goto cleanup;
8936  }
8937 
8938  if (scheme_str_len + 1 >= size) {
8939  // we need to have another string, even if nullptr.
8940  // we check with + 1 since we expect that if size was not hit,
8941  // an additional null was read.
8943  goto cleanup;
8944  }
8945 
8946  size -= scheme_str_len + 1;
8947 
8948  if ((value_str_len = ff_read_string_to_bprint_overwrite(pb, &value_buf,
8949  size)) < 0) {
8950  ret = value_str_len;
8951  goto cleanup;
8952  }
8953 
8954  if (value_str_len == size) {
8955  // in case of no trailing null, box is not valid.
8957  goto cleanup;
8958  }
8959 
8961  "%s stream %d KindBox(scheme: %s, value: %s)\n",
8963  st->index,
8964  scheme_buf.str, value_buf.str);
8965 
8966  for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
8968  if (!av_strstart(scheme_buf.str, map.scheme_uri, NULL))
8969  continue;
8970 
8971  for (int j = 0; map.value_maps[j].disposition; j++) {
8972  const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
8973  if (!av_strstart(value_buf.str, value_map.value, NULL))
8974  continue;
8975 
8976  st->disposition |= value_map.disposition;
8977  }
8978  }
8979 
8980  ret = 0;
8981 
8982 cleanup:
8983 
8984  av_bprint_finalize(&scheme_buf, NULL);
8985  av_bprint_finalize(&value_buf, NULL);
8986 
8987  return ret;
8988 }
8989 
8991 {
8992  AVStream *st;
8993  AVChannelLayout ch_layout = { 0 };
8994  int ret, i, version, type;
8995  int ambisonic_order, channel_order, normalization, channel_count;
8996  int ambi_channels, non_diegetic_channels;
8997 
8998  if (c->fc->nb_streams < 1)
8999  return 0;
9000 
9001  st = c->fc->streams[c->fc->nb_streams - 1];
9002 
9003  if (atom.size < 16) {
9004  av_log(c->fc, AV_LOG_ERROR, "SA3D audio box too small\n");
9005  return AVERROR_INVALIDDATA;
9006  }
9007 
9008  version = avio_r8(pb);
9009  if (version) {
9010  av_log(c->fc, AV_LOG_WARNING, "Unsupported SA3D box version %d\n", version);
9011  return 0;
9012  }
9013 
9014  type = avio_r8(pb);
9015  if (type & 0x7f) {
9016  av_log(c->fc, AV_LOG_WARNING,
9017  "Unsupported ambisonic type %d\n", type & 0x7f);
9018  return 0;
9019  }
9020  non_diegetic_channels = (type >> 7) * 2; // head_locked_stereo
9021 
9022  ambisonic_order = avio_rb32(pb);
9023 
9024  channel_order = avio_r8(pb);
9025  if (channel_order) {
9026  av_log(c->fc, AV_LOG_WARNING,
9027  "Unsupported channel_order %d\n", channel_order);
9028  return 0;
9029  }
9030 
9031  normalization = avio_r8(pb);
9032  if (normalization) {
9033  av_log(c->fc, AV_LOG_WARNING,
9034  "Unsupported normalization %d\n", normalization);
9035  return 0;
9036  }
9037 
9038  channel_count = avio_rb32(pb);
9039  if (ambisonic_order < 0 || ambisonic_order > 31 ||
9040  channel_count != ((ambisonic_order + 1LL) * (ambisonic_order + 1LL) +
9041  non_diegetic_channels)) {
9042  av_log(c->fc, AV_LOG_ERROR,
9043  "Invalid number of channels (%d / %d)\n",
9044  channel_count, ambisonic_order);
9045  return 0;
9046  }
9047  ambi_channels = channel_count - non_diegetic_channels;
9048 
9049  ret = av_channel_layout_custom_init(&ch_layout, channel_count);
9050  if (ret < 0)
9051  return 0;
9052 
9053  for (i = 0; i < channel_count; i++) {
9054  unsigned channel = avio_rb32(pb);
9055 
9056  if (channel >= channel_count) {
9057  av_log(c->fc, AV_LOG_ERROR, "Invalid channel index (%d / %d)\n",
9058  channel, ambisonic_order);
9059  av_channel_layout_uninit(&ch_layout);
9060  return 0;
9061  }
9062  if (channel >= ambi_channels)
9063  ch_layout.u.map[i].id = channel - ambi_channels;
9064  else
9065  ch_layout.u.map[i].id = AV_CHAN_AMBISONIC_BASE + channel;
9066  }
9067 
9069  if (ret < 0) {
9070  av_channel_layout_uninit(&ch_layout);
9071  return 0;
9072  }
9073 
9075  st->codecpar->ch_layout = ch_layout;
9076 
9077  return 0;
9078 }
9079 
9081 {
9082  AVStream *st;
9083  int version;
9084 
9085  if (c->fc->nb_streams < 1)
9086  return 0;
9087 
9088  st = c->fc->streams[c->fc->nb_streams - 1];
9089 
9090  if (atom.size < 5) {
9091  av_log(c->fc, AV_LOG_ERROR, "Empty SAND audio box\n");
9092  return AVERROR_INVALIDDATA;
9093  }
9094 
9095  version = avio_r8(pb);
9096  if (version) {
9097  av_log(c->fc, AV_LOG_WARNING, "Unsupported SAND box version %d\n", version);
9098  return 0;
9099  }
9100 
9102 
9103  return 0;
9104 }
9105 
9106 static int rb_size(AVIOContext *pb, int64_t *value, int size)
9107 {
9108  if (size == 0)
9109  *value = 0;
9110  else if (size == 1)
9111  *value = avio_r8(pb);
9112  else if (size == 2)
9113  *value = avio_rb16(pb);
9114  else if (size == 4)
9115  *value = avio_rb32(pb);
9116  else if (size == 8) {
9117  *value = avio_rb64(pb);
9118  if (*value < 0)
9119  return -1;
9120  } else
9121  return -1;
9122  return size;
9123 }
9124 
9126 {
9127  avio_rb32(pb); // version & flags.
9128  c->primary_item_id = avio_rb16(pb);
9129  av_log(c->fc, AV_LOG_TRACE, "pitm: primary_item_id %d\n", c->primary_item_id);
9130  return atom.size;
9131 }
9132 
9134 {
9135  c->idat_offset = avio_tell(pb);
9136  return 0;
9137 }
9138 
9140 {
9141  HEIFItem **heif_item;
9142  int version, offset_size, length_size, base_offset_size, index_size;
9143  int item_count, extent_count;
9144  int64_t base_offset, extent_offset, extent_length;
9145  uint8_t value;
9146 
9147  if (c->found_iloc) {
9148  av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n");
9149  return 0;
9150  }
9151 
9152  version = avio_r8(pb);
9153  avio_rb24(pb); // flags.
9154 
9155  value = avio_r8(pb);
9156  offset_size = (value >> 4) & 0xF;
9157  length_size = value & 0xF;
9158  value = avio_r8(pb);
9159  base_offset_size = (value >> 4) & 0xF;
9160  index_size = !version ? 0 : (value & 0xF);
9161  if (index_size) {
9162  avpriv_report_missing_feature(c->fc, "iloc: index_size != 0");
9163  return AVERROR_PATCHWELCOME;
9164  }
9165  item_count = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
9166 
9167  heif_item = av_realloc_array(c->heif_item, FFMAX(item_count, c->nb_heif_item), sizeof(*c->heif_item));
9168  if (!heif_item)
9169  return AVERROR(ENOMEM);
9170  c->heif_item = heif_item;
9171  if (item_count > c->nb_heif_item)
9172  memset(&c->heif_item[c->nb_heif_item], 0,
9173  sizeof(*c->heif_item) * (item_count - c->nb_heif_item));
9174  c->nb_heif_item = FFMAX(c->nb_heif_item, item_count);
9175 
9176  av_log(c->fc, AV_LOG_TRACE, "iloc: item_count %d\n", item_count);
9177  for (int i = 0; i < item_count; i++) {
9178  HEIFItem *item = NULL;
9179  int item_id = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
9180  int offset_type = (version > 0) ? avio_rb16(pb) & 0xf : 0;
9181  int j;
9182 
9183  if (avio_feof(pb))
9184  return AVERROR_INVALIDDATA;
9185  if (offset_type > 1) {
9186  avpriv_report_missing_feature(c->fc, "iloc offset type %d", offset_type);
9187  return AVERROR_PATCHWELCOME;
9188  }
9189 
9190  avio_rb16(pb); // data_reference_index.
9191  if (rb_size(pb, &base_offset, base_offset_size) < 0)
9192  return AVERROR_INVALIDDATA;
9193  extent_count = avio_rb16(pb);
9194  if (extent_count > 1) {
9195  // For still AVIF images, we only support one extent item.
9196  avpriv_report_missing_feature(c->fc, "iloc: extent_count > 1");
9197  return AVERROR_PATCHWELCOME;
9198  }
9199 
9200  if (rb_size(pb, &extent_offset, offset_size) < 0 ||
9201  rb_size(pb, &extent_length, length_size) < 0 ||
9202  base_offset > INT64_MAX - extent_offset)
9203  return AVERROR_INVALIDDATA;
9204 
9205  for (j = 0; j < c->nb_heif_item; j++) {
9206  item = c->heif_item[j];
9207  if (!item)
9208  item = c->heif_item[j] = av_mallocz(sizeof(*item));
9209  else if (item->item_id != item_id)
9210  continue;
9211  break;
9212  }
9213  if (!item)
9214  return AVERROR(ENOMEM);
9215  if (j == c->nb_heif_item)
9216  return AVERROR_INVALIDDATA;
9217 
9218  item->item_id = item_id;
9219 
9220  if (offset_type == 1)
9221  item->is_idat_relative = 1;
9222  item->extent_length = extent_length;
9223  item->extent_offset = base_offset + extent_offset;
9224  av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, item->item_id %d, offset_type %d, "
9225  "extent_offset %"PRId64", extent_length %"PRId64"\n",
9226  i, item->item_id, offset_type, item->extent_offset, item->extent_length);
9227  }
9228 
9229  c->found_iloc = 1;
9230  return atom.size;
9231 }
9232 
9234 {
9235  HEIFItem *item = NULL;
9236  AVBPrint item_name;
9237  int64_t size = atom.size;
9238  uint32_t item_type;
9239  int item_id;
9240  int i, version, ret;
9241 
9242  version = avio_r8(pb);
9243  avio_rb24(pb); // flags.
9244  size -= 4;
9245  if (size < 0)
9246  return AVERROR_INVALIDDATA;
9247 
9248  if (version < 2) {
9249  avpriv_report_missing_feature(c->fc, "infe version < 2");
9250  avio_skip(pb, size);
9251  return 1;
9252  }
9253 
9254  item_id = version > 2 ? avio_rb32(pb) : avio_rb16(pb);
9255  avio_rb16(pb); // item_protection_index
9256  item_type = avio_rl32(pb);
9257  size -= 8;
9258  if (size < 1)
9259  return AVERROR_INVALIDDATA;
9260 
9263  if (ret < 0) {
9265  return ret;
9266  }
9267 
9268  av_log(c->fc, AV_LOG_TRACE, "infe: item_id %d, item_type %s, item_name %s\n",
9269  item_id, av_fourcc2str(item_type), item_name.str);
9270 
9271  size -= ret + 1;
9272  if (size > 0)
9273  avio_skip(pb, size);
9274 
9275  for (i = 0; i < c->nb_heif_item; i++) {
9276  item = c->heif_item[i];
9277  if (!item)
9278  item = c->heif_item[i] = av_mallocz(sizeof(*item));
9279  else if (item->item_id != item_id)
9280  continue;
9281  break;
9282  }
9283  if (!item) {
9285  return AVERROR(ENOMEM);
9286  }
9287  if (i == c->nb_heif_item) {
9289  return AVERROR_INVALIDDATA;
9290  }
9291 
9292  av_freep(&item->name);
9293  av_bprint_finalize(&item_name, ret ? &item->name : NULL);
9294  item->item_id = item_id;
9295  item->type = item_type;
9296 
9297  switch (item_type) {
9298  case MKTAG('a','v','0','1'):
9299  case MKTAG('j','p','e','g'):
9300  case MKTAG('h','v','c','1'):
9301  ret = heif_add_stream(c, item);
9302  if (ret < 0)
9303  return ret;
9304  break;
9305  }
9306 
9307  return 0;
9308 }
9309 
9311 {
9312  HEIFItem **heif_item;
9313  int entry_count;
9314  int version, got_stream = 0, ret, i;
9315 
9316  if (c->found_iinf) {
9317  av_log(c->fc, AV_LOG_WARNING, "Duplicate iinf box found\n");
9318  return 0;
9319  }
9320 
9321  version = avio_r8(pb);
9322  avio_rb24(pb); // flags.
9323  entry_count = version ? avio_rb32(pb) : avio_rb16(pb);
9324 
9325  heif_item = av_realloc_array(c->heif_item, FFMAX(entry_count, c->nb_heif_item), sizeof(*c->heif_item));
9326  if (!heif_item)
9327  return AVERROR(ENOMEM);
9328  c->heif_item = heif_item;
9329  if (entry_count > c->nb_heif_item)
9330  memset(&c->heif_item[c->nb_heif_item], 0,
9331  sizeof(*c->heif_item) * (entry_count - c->nb_heif_item));
9332  c->nb_heif_item = FFMAX(c->nb_heif_item, entry_count);
9333 
9334  for (i = 0; i < entry_count; i++) {
9335  MOVAtom infe;
9336 
9337  infe.size = avio_rb32(pb) - 8;
9338  infe.type = avio_rl32(pb);
9339  if (avio_feof(pb)) {
9341  goto fail;
9342  }
9343  ret = mov_read_infe(c, pb, infe);
9344  if (ret < 0)
9345  goto fail;
9346  if (!ret)
9347  got_stream = 1;
9348  }
9349 
9350  c->found_iinf = got_stream;
9351  return 0;
9352 fail:
9353  for (; i >= 0; i--) {
9354  HEIFItem *item = c->heif_item[i];
9355 
9356  if (!item)
9357  continue;
9358 
9359  av_freep(&item->name);
9360  }
9361  return ret;
9362 }
9363 
9365 {
9366  HEIFItem *item = NULL;
9367  HEIFGrid *grid;
9368  int entries, i;
9369  int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9370  int ret = 0;
9371 
9372  for (int i = 0; i < c->nb_heif_grid; i++) {
9373  if (c->heif_grid[i].item->item_id == from_item_id) {
9374  av_log(c->fc, AV_LOG_ERROR, "More than one 'dimg' box "
9375  "referencing the same Derived Image item\n");
9376  return AVERROR_INVALIDDATA;
9377  }
9378  }
9379  for (int i = 0; i < c->nb_heif_item; i++) {
9380  if (!c->heif_item[i] || c->heif_item[i]->item_id != from_item_id)
9381  continue;
9382  item = c->heif_item[i];
9383 
9384  switch (item->type) {
9385  case MKTAG('g','r','i','d'):
9386  case MKTAG('i','o','v','l'):
9387  break;
9388  default:
9389  avpriv_report_missing_feature(c->fc, "Derived Image item of type %s",
9390  av_fourcc2str(item->type));
9391  return 0;
9392  }
9393  break;
9394  }
9395  if (!item) {
9396  av_log(c->fc, AV_LOG_ERROR, "Missing grid information\n");
9397  return AVERROR_INVALIDDATA;
9398  }
9399 
9400  entries = avio_rb16(pb);
9401  if (!entries) {
9402  av_log(c->fc, AV_LOG_ERROR,
9403  "Derived image item references no input images\n");
9404  return AVERROR_INVALIDDATA;
9405  }
9406 
9407  grid = av_realloc_array(c->heif_grid, c->nb_heif_grid + 1U,
9408  sizeof(*c->heif_grid));
9409  if (!grid)
9410  return AVERROR(ENOMEM);
9411  c->heif_grid = grid;
9412  grid = &grid[c->nb_heif_grid];
9413 
9414  grid->tile_id_list = av_malloc_array(entries, sizeof(*grid->tile_id_list));
9415  grid->tile_idx_list = av_calloc(entries, sizeof(*grid->tile_idx_list));
9416  grid->tile_item_list = av_calloc(entries, sizeof(*grid->tile_item_list));
9417  if (!grid->tile_id_list || !grid->tile_item_list || !grid->tile_idx_list) {
9418  ret = AVERROR(ENOMEM);
9419  goto fail;
9420  }
9421  /* 'to' item ids */
9422  for (i = 0; i < entries; i++) {
9423  grid->tile_id_list[i] = version ? avio_rb32(pb) : avio_rb16(pb);
9424 
9425  if (avio_feof(pb)) {
9427  goto fail;
9428  }
9429  }
9430 
9431  grid->nb_tiles = entries;
9432  grid->item = item;
9433  ++c->nb_heif_grid;
9434 
9435  av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n",
9436  from_item_id, entries);
9437 
9438  return 0;
9439 fail:
9440  av_freep(&grid->tile_id_list);
9441  av_freep(&grid->tile_idx_list);
9442  av_freep(&grid->tile_item_list);
9443 
9444  return ret;
9445 }
9446 
9447 static int mov_read_iref_cdsc(MOVContext *c, AVIOContext *pb, uint32_t type, int version, uint32_t size)
9448 {
9449  HEIFItem *from_item = NULL;
9450  int entries;
9451  int item_id_size = version ? 4 : 2;
9452  int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9453  const HEIFItemRef ref = { type, from_item_id };
9454 
9455  from_item = get_heif_item(c, from_item_id);
9456  if (!from_item) {
9457  av_log(c->fc, AV_LOG_ERROR, "Missing stream referenced by thmb item\n");
9458  return AVERROR_INVALIDDATA;
9459  }
9460 
9461  entries = avio_rb16(pb);
9462  if ((int64_t)entries * item_id_size > (int64_t)size - item_id_size - 2) {
9463  av_log(c->fc, AV_LOG_ERROR, "iref %s entry count %d exceeds the sub-box size\n",
9464  av_fourcc2str(type), entries);
9465  return AVERROR_INVALIDDATA;
9466  }
9467  /* 'to' item ids */
9468  for (int i = 0; i < entries; i++) {
9469  HEIFItem *item = get_heif_item(c, version ? avio_rb32(pb) : avio_rb16(pb));
9470 
9471  if (avio_feof(pb))
9472  return AVERROR_INVALIDDATA;
9473 
9474  if (!item) {
9475  av_log(c->fc, AV_LOG_WARNING, "Missing stream referenced by %s item\n",
9476  av_fourcc2str(type));
9477  continue;
9478  }
9479 
9480  if (!av_dynarray2_add((void **)&item->iref_list, &item->nb_iref_list,
9481  sizeof(*item->iref_list), (const uint8_t *)&ref))
9482  return AVERROR(ENOMEM);
9483  }
9484 
9485  av_log(c->fc, AV_LOG_TRACE, "%s: from_item_id %d, entries %d\n",
9486  av_fourcc2str(type), from_item_id, entries);
9487 
9488  return 0;
9489 }
9490 
9492 {
9493  int version = avio_r8(pb);
9494  int ret;
9495 
9496  avio_rb24(pb); // flags
9497  atom.size -= 4;
9498 
9499  if (version > 1) {
9500  av_log(c->fc, AV_LOG_WARNING, "Unknown iref box version %d\n", version);
9501  return 0;
9502  }
9503 
9504  while (atom.size) {
9505  int64_t next = avio_tell(pb);
9506  uint32_t type, size = avio_rb32(pb);
9507 
9508  if (size < 14 || size > atom.size || next > INT64_MAX - size)
9509  return AVERROR_INVALIDDATA;
9510 
9511  next += size;
9512  type = avio_rl32(pb);
9513  switch (type) {
9514  case MKTAG('d','i','m','g'):
9515  ret = mov_read_iref_dimg(c, pb, version);
9516  if (ret < 0)
9517  return ret;
9518  break;
9519  case MKTAG('c','d','s','c'):
9520  case MKTAG('t','h','m','b'):
9521  ret = mov_read_iref_cdsc(c, pb, type, version, size - 8);
9522  if (ret < 0)
9523  return ret;
9524  break;
9525  default:
9526  av_log(c->fc, AV_LOG_DEBUG, "Unknown iref type %s size %"PRIu32"\n",
9527  av_fourcc2str(type), size);
9528  }
9529 
9530  atom.size -= size;
9531  avio_seek(pb, next, SEEK_SET);
9532  }
9533  return 0;
9534 }
9535 
9537 {
9538  HEIFItem *item;
9539  uint32_t width, height;
9540 
9541  avio_r8(pb); /* version */
9542  avio_rb24(pb); /* flags */
9543  width = avio_rb32(pb);
9544  height = avio_rb32(pb);
9545 
9546  av_log(c->fc, AV_LOG_TRACE, "ispe: item_id %d, width %"PRIu32", height %"PRIu32"\n",
9547  c->cur_item_id, width, height);
9548 
9549  if (!width || !height || width > INT_MAX || height > INT_MAX) {
9550  av_log(c->fc, AV_LOG_ERROR, "Invalid ispe dimensions %"PRIu32"x%"PRIu32"\n",
9551  width, height);
9552  return AVERROR_INVALIDDATA;
9553  }
9554 
9555  item = get_heif_item(c, c->cur_item_id);
9556  if (item) {
9557  item->width = width;
9558  item->height = height;
9559  }
9560 
9561  return 0;
9562 }
9563 
9565 {
9566  HEIFItem *item;
9567  int angle;
9568 
9569  angle = avio_r8(pb) & 0x3;
9570 
9571  av_log(c->fc, AV_LOG_TRACE, "irot: item_id %d, angle %u\n",
9572  c->cur_item_id, angle);
9573 
9574  item = get_heif_item(c, c->cur_item_id);
9575  if (item) {
9576  // angle * 90 specifies the angle (in anti-clockwise direction)
9577  // in units of degrees.
9578  item->rotation = angle * 90;
9579  }
9580 
9581  return 0;
9582 }
9583 
9585 {
9586  HEIFItem *item;
9587  int axis;
9588 
9589  axis = avio_r8(pb) & 0x1;
9590 
9591  av_log(c->fc, AV_LOG_TRACE, "imir: item_id %d, axis %u\n",
9592  c->cur_item_id, axis);
9593 
9594  item = get_heif_item(c, c->cur_item_id);
9595  if (item) {
9596  item->hflip = axis;
9597  item->vflip = !axis;
9598  }
9599 
9600  return 0;
9601 }
9602 
9604 {
9605  typedef struct MOVAtoms {
9606  FFIOContext b;
9607  uint32_t type;
9608  int64_t size;
9609  uint8_t *data;
9610  } MOVAtoms;
9611  MOVAtoms *atoms = NULL;
9612  MOVAtom a;
9613  unsigned count;
9614  int nb_atoms = 0;
9615  int version, flags;
9616  int ret;
9617 
9618  a.size = avio_rb32(pb);
9619  a.type = avio_rl32(pb);
9620 
9621  if (a.size < 8 || a.type != MKTAG('i','p','c','o'))
9622  return AVERROR_INVALIDDATA;
9623 
9624  a.size -= 8;
9625  while (a.size >= 8) {
9626  MOVAtoms *ref = av_dynarray2_add((void**)&atoms, &nb_atoms, sizeof(MOVAtoms), NULL);
9627  if (!ref) {
9628  ret = AVERROR(ENOMEM);
9629  goto fail;
9630  }
9631  ref->data = NULL;
9632  ref->size = avio_rb32(pb);
9633  ref->type = avio_rl32(pb);
9634  if (ref->size > a.size || ref->size < 8)
9635  break;
9636  ref->data = av_malloc(ref->size);
9637  if (!ref->data) {
9639  goto fail;
9640  }
9641  av_log(c->fc, AV_LOG_TRACE, "ipco: index %d, box type %s\n", nb_atoms, av_fourcc2str(ref->type));
9642  avio_seek(pb, -8, SEEK_CUR);
9643  if (avio_read(pb, ref->data, ref->size) != ref->size) {
9645  goto fail;
9646  }
9647  ffio_init_read_context(&ref->b, ref->data, ref->size);
9648  a.size -= ref->size;
9649  }
9650 
9651  if (a.size) {
9653  goto fail;
9654  }
9655 
9656  a.size = avio_rb32(pb);
9657  a.type = avio_rl32(pb);
9658 
9659  if (a.size < 8 || a.type != MKTAG('i','p','m','a')) {
9661  goto fail;
9662  }
9663 
9664  version = avio_r8(pb);
9665  flags = avio_rb24(pb);
9666  count = avio_rb32(pb);
9667 
9668  for (int i = 0; i < count; i++) {
9669  int item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9670  int assoc_count = avio_r8(pb);
9671 
9672  if (avio_feof(pb)) {
9674  goto fail;
9675  }
9676 
9677  for (int j = 0; j < assoc_count; j++) {
9678  MOVAtoms *ref;
9679  int index = avio_r8(pb) & 0x7f;
9680  if (flags & 1) {
9681  index <<= 8;
9682  index |= avio_r8(pb);
9683  }
9684  if (index > nb_atoms || index <= 0) {
9686  goto fail;
9687  }
9688  ref = &atoms[--index];
9689 
9690  av_log(c->fc, AV_LOG_TRACE, "ipma: property_index %d, item_id %d, item_type %s\n",
9691  index + 1, item_id, av_fourcc2str(ref->type));
9692 
9693  c->cur_item_id = item_id;
9694 
9695  ret = mov_read_default(c, &ref->b.pub,
9696  (MOVAtom) { .size = ref->size,
9697  .type = MKTAG('i','p','c','o') });
9698  if (ret < 0)
9699  goto fail;
9700  ffio_init_read_context(&ref->b, ref->data, ref->size);
9701  }
9702  }
9703 
9704  ret = 0;
9705 fail:
9706  c->cur_item_id = -1;
9707  for (int i = 0; i < nb_atoms; i++)
9708  av_free(atoms[i].data);
9709  av_free(atoms);
9710 
9711  return ret;
9712 }
9713 
9715 { MKTAG('A','C','L','R'), mov_read_aclr },
9716 { MKTAG('A','P','R','G'), mov_read_avid },
9717 { MKTAG('A','A','L','P'), mov_read_avid },
9718 { MKTAG('A','R','E','S'), mov_read_ares },
9719 { MKTAG('a','v','s','s'), mov_read_avss },
9720 { MKTAG('a','v','1','C'), mov_read_glbl },
9721 { MKTAG('c','h','p','l'), mov_read_chpl },
9722 { MKTAG('c','o','6','4'), mov_read_stco },
9723 { MKTAG('c','o','l','r'), mov_read_colr },
9724 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
9725 { MKTAG('d','i','n','f'), mov_read_default },
9726 { MKTAG('D','p','x','E'), mov_read_dpxe },
9727 { MKTAG('d','r','e','f'), mov_read_dref },
9728 { MKTAG('e','d','t','s'), mov_read_default },
9729 { MKTAG('e','l','s','t'), mov_read_elst },
9730 { MKTAG('e','n','d','a'), mov_read_enda },
9731 { MKTAG('f','i','e','l'), mov_read_fiel },
9732 { MKTAG('a','d','r','m'), mov_read_adrm },
9733 { MKTAG('f','t','y','p'), mov_read_ftyp },
9734 { MKTAG('g','l','b','l'), mov_read_glbl },
9735 { MKTAG('h','d','l','r'), mov_read_hdlr },
9736 { MKTAG('i','l','s','t'), mov_read_ilst },
9737 { MKTAG('j','p','2','h'), mov_read_jp2h },
9738 { MKTAG('m','d','a','t'), mov_read_mdat },
9739 { MKTAG('m','d','h','d'), mov_read_mdhd },
9740 { MKTAG('m','d','i','a'), mov_read_default },
9741 { MKTAG('m','e','t','a'), mov_read_meta },
9742 { MKTAG('m','i','n','f'), mov_read_default },
9743 { MKTAG('m','o','o','f'), mov_read_moof },
9744 { MKTAG('m','o','o','v'), mov_read_moov },
9745 { MKTAG('m','v','e','x'), mov_read_default },
9746 { MKTAG('m','v','h','d'), mov_read_mvhd },
9747 { MKTAG('S','M','I',' '), mov_read_svq3 },
9748 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
9749 { MKTAG('a','v','c','C'), mov_read_glbl },
9750 { MKTAG('p','a','s','p'), mov_read_pasp },
9751 { MKTAG('c','l','a','p'), mov_read_clap },
9752 { MKTAG('s','b','a','s'), mov_read_sbas },
9753 { MKTAG('v','d','e','p'), mov_read_vdep },
9754 { MKTAG('s','i','d','x'), mov_read_sidx },
9755 { MKTAG('s','t','b','l'), mov_read_default },
9756 { MKTAG('s','t','c','o'), mov_read_stco },
9757 { MKTAG('s','t','p','s'), mov_read_stps },
9758 { MKTAG('s','t','r','f'), mov_read_strf },
9759 { MKTAG('s','t','s','c'), mov_read_stsc },
9760 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
9761 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
9762 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
9763 { MKTAG('s','t','t','s'), mov_read_stts },
9764 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
9765 { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
9766 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
9767 { MKTAG('t','f','d','t'), mov_read_tfdt },
9768 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
9769 { MKTAG('t','r','a','k'), mov_read_trak },
9770 { MKTAG('t','r','a','f'), mov_read_default },
9771 { MKTAG('t','r','e','f'), mov_read_default },
9772 { MKTAG('t','m','c','d'), mov_read_tmcd },
9773 { MKTAG('c','h','a','p'), mov_read_chap },
9774 { MKTAG('t','r','e','x'), mov_read_trex },
9775 { MKTAG('t','r','u','n'), mov_read_trun },
9776 { MKTAG('u','d','t','a'), mov_read_default },
9777 { MKTAG('w','a','v','e'), mov_read_wave },
9778 { MKTAG('e','s','d','s'), mov_read_esds },
9779 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
9780 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
9781 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
9782 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
9783 { MKTAG('w','f','e','x'), mov_read_wfex },
9784 { MKTAG('c','m','o','v'), mov_read_cmov },
9785 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout from quicktime */
9786 { MKTAG('c','h','n','l'), mov_read_chnl }, /* channel layout from ISO-14496-12 */
9787 { MKTAG('d','v','c','1'), mov_read_dvc1 },
9788 { MKTAG('s','g','p','d'), mov_read_sgpd },
9789 { MKTAG('s','b','g','p'), mov_read_sbgp },
9790 { MKTAG('h','v','c','C'), mov_read_glbl },
9791 { MKTAG('v','v','c','C'), mov_read_glbl },
9792 { MKTAG('u','u','i','d'), mov_read_uuid },
9793 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
9794 { MKTAG('f','r','e','e'), mov_read_free },
9795 { MKTAG('-','-','-','-'), mov_read_custom },
9796 { MKTAG('s','i','n','f'), mov_read_default },
9797 { MKTAG('f','r','m','a'), mov_read_frma },
9798 { MKTAG('s','e','n','c'), mov_read_senc },
9799 { MKTAG('s','a','i','z'), mov_read_saiz },
9800 { MKTAG('s','a','i','o'), mov_read_saio },
9801 { MKTAG('p','s','s','h'), mov_read_pssh },
9802 { MKTAG('s','c','h','m'), mov_read_schm },
9803 { MKTAG('s','c','h','i'), mov_read_default },
9804 { MKTAG('t','e','n','c'), mov_read_tenc },
9805 { MKTAG('d','f','L','a'), mov_read_dfla },
9806 { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
9807 { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
9808 { MKTAG('v','e','x','u'), mov_read_vexu }, /* video extension usage */
9809 { MKTAG('h','f','o','v'), mov_read_hfov },
9810 { MKTAG('d','O','p','s'), mov_read_dops },
9811 { MKTAG('d','m','l','p'), mov_read_dmlp },
9812 { MKTAG('S','m','D','m'), mov_read_smdm },
9813 { MKTAG('C','o','L','L'), mov_read_coll },
9814 { MKTAG('v','p','c','C'), mov_read_vpcc },
9815 { MKTAG('m','d','c','v'), mov_read_mdcv },
9816 { MKTAG('c','l','l','i'), mov_read_clli },
9817 { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
9818 { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
9819 { MKTAG('d','v','w','C'), mov_read_dvcc_dvvc },
9820 { MKTAG('h','v','c','E'), mov_read_hvce },
9821 { MKTAG('k','i','n','d'), mov_read_kind },
9822 { MKTAG('S','A','3','D'), mov_read_SA3D }, /* ambisonic audio box */
9823 { MKTAG('S','A','N','D'), mov_read_SAND }, /* non diegetic audio box */
9824 { MKTAG('i','l','o','c'), mov_read_iloc },
9825 { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */
9826 { MKTAG('p','i','t','m'), mov_read_pitm },
9827 { MKTAG('e','v','c','C'), mov_read_glbl },
9828 { MKTAG('i','d','a','t'), mov_read_idat },
9829 { MKTAG('i','m','i','r'), mov_read_imir },
9830 { MKTAG('i','r','e','f'), mov_read_iref },
9831 { MKTAG('i','s','p','e'), mov_read_ispe },
9832 { MKTAG('i','r','o','t'), mov_read_irot },
9833 { MKTAG('i','p','r','p'), mov_read_iprp },
9834 { MKTAG('i','i','n','f'), mov_read_iinf },
9835 { MKTAG('a','m','v','e'), mov_read_amve }, /* ambient viewing environment box */
9836 { MKTAG('l','h','v','C'), mov_read_lhvc },
9837 { MKTAG('l','v','c','C'), mov_read_glbl },
9838 { MKTAG('a','p','v','C'), mov_read_glbl },
9839 #if CONFIG_IAMFDEC
9840 { MKTAG('i','a','c','b'), mov_read_iacb },
9841 #endif
9842 { MKTAG('s','r','a','t'), mov_read_srat },
9843 { 0, NULL }
9844 };
9845 
9847 {
9848  int64_t total_size = 0;
9849  MOVAtom a;
9850  int i;
9851 
9852  if (c->atom_depth > 10) {
9853  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
9854  return AVERROR_INVALIDDATA;
9855  }
9856  c->atom_depth ++;
9857 
9858  if (atom.size < 0)
9859  atom.size = INT64_MAX;
9860  while (total_size <= atom.size - 8) {
9861  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
9862  a.size = avio_rb32(pb);
9863  a.type = avio_rl32(pb);
9864  if (avio_feof(pb))
9865  break;
9866  if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
9867  a.type == MKTAG('h','o','o','v')) &&
9868  a.size >= 8 &&
9869  c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
9870  uint32_t type;
9871  avio_skip(pb, 4);
9872  type = avio_rl32(pb);
9873  if (avio_feof(pb))
9874  break;
9875  avio_seek(pb, -8, SEEK_CUR);
9876  if (type == MKTAG('m','v','h','d') ||
9877  type == MKTAG('c','m','o','v')) {
9878  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
9879  a.type = MKTAG('m','o','o','v');
9880  }
9881  }
9882  if (atom.type != MKTAG('r','o','o','t') &&
9883  atom.type != MKTAG('m','o','o','v')) {
9884  if (a.type == MKTAG('t','r','a','k') ||
9885  a.type == MKTAG('m','d','a','t')) {
9886  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
9887  avio_skip(pb, -8);
9888  c->atom_depth --;
9889  return 0;
9890  }
9891  }
9892  total_size += 8;
9893  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
9894  a.size = avio_rb64(pb) - 8;
9895  total_size += 8;
9896  }
9897  av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
9898  av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
9899  if (a.size == 0) {
9900  a.size = atom.size - total_size + 8;
9901  }
9902  if (a.size < 0)
9903  break;
9904  a.size -= 8;
9905  if (a.size < 0)
9906  break;
9907  a.size = FFMIN(a.size, atom.size - total_size);
9908 
9909  for (i = 0; mov_default_parse_table[i].type; i++)
9910  if (mov_default_parse_table[i].type == a.type) {
9912  break;
9913  }
9914 
9915  // container is user data
9916  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
9917  atom.type == MKTAG('i','l','s','t')))
9919 
9920  // Supports parsing the QuickTime Metadata Keys.
9921  // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
9922  if (!parse && c->found_hdlr_mdta &&
9923  atom.type == MKTAG('m','e','t','a') &&
9924  a.type == MKTAG('k','e','y','s') &&
9925  c->meta_keys_count == 0) {
9926  parse = mov_read_keys;
9927  }
9928 
9929  if (!parse) { /* skip leaf atoms data */
9930  avio_skip(pb, a.size);
9931  } else {
9932  int64_t start_pos = avio_tell(pb);
9933  int64_t left;
9934  int err = parse(c, pb, a);
9935  if (err < 0) {
9936  c->atom_depth --;
9937  return err;
9938  }
9939  if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
9940  ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
9941  start_pos + a.size == avio_size(pb))) {
9942  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
9943  c->next_root_atom = start_pos + a.size;
9944  c->atom_depth --;
9945  return 0;
9946  }
9947  left = a.size - avio_tell(pb) + start_pos;
9948  if (left > 0) /* skip garbage at atom end */
9949  avio_skip(pb, left);
9950  else if (left < 0) {
9951  av_log(c->fc, AV_LOG_WARNING,
9952  "overread end of atom '%s' by %"PRId64" bytes\n",
9953  av_fourcc2str(a.type), -left);
9954  avio_seek(pb, left, SEEK_CUR);
9955  }
9956  }
9957 
9958  total_size += a.size;
9959  }
9960 
9961  if (total_size < atom.size && atom.size < 0x7ffff)
9962  avio_skip(pb, atom.size - total_size);
9963 
9964  c->atom_depth --;
9965  return 0;
9966 }
9967 
9968 static int mov_probe(const AVProbeData *p)
9969 {
9970  int64_t offset;
9971  uint32_t tag;
9972  int score = 0;
9973  int moov_offset = -1;
9974 
9975  /* check file header */
9976  offset = 0;
9977  for (;;) {
9978  int64_t size;
9979  int minsize = 8;
9980  /* ignore invalid offset */
9981  if ((offset + 8ULL) > (unsigned int)p->buf_size)
9982  break;
9983  size = AV_RB32(p->buf + offset);
9984  if (size == 1 && offset + 16 <= (unsigned int)p->buf_size) {
9985  size = AV_RB64(p->buf+offset + 8);
9986  minsize = 16;
9987  } else if (size == 0) {
9988  size = p->buf_size - offset;
9989  }
9990  if (size < minsize) {
9991  offset += 4;
9992  continue;
9993  }
9994  tag = AV_RL32(p->buf + offset + 4);
9995  switch(tag) {
9996  /* check for obvious tags */
9997  case MKTAG('m','o','o','v'):
9998  moov_offset = offset + 4;
10000  case MKTAG('m','d','a','t'):
10001  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
10002  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
10003  case MKTAG('f','t','y','p'):
10004  if (tag == MKTAG('f','t','y','p') &&
10005  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
10006  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
10007  || AV_RL32(p->buf + offset + 8) == MKTAG('j','x','l',' ')
10008  )) {
10009  score = FFMAX(score, 5);
10010  } else {
10011  score = AVPROBE_SCORE_MAX;
10012  }
10013  break;
10014  /* those are more common words, so rate then a bit less */
10015  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
10016  case MKTAG('w','i','d','e'):
10017  case MKTAG('f','r','e','e'):
10018  case MKTAG('j','u','n','k'):
10019  case MKTAG('p','i','c','t'):
10020  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
10021  break;
10022  case MKTAG(0x82,0x82,0x7f,0x7d):
10023  score = FFMAX(score, AVPROBE_SCORE_EXTENSION - 5);
10024  break;
10025  case MKTAG('s','k','i','p'):
10026  case MKTAG('u','u','i','d'):
10027  case MKTAG('p','r','f','l'):
10028  /* if we only find those cause probedata is too small at least rate them */
10029  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
10030  break;
10031  }
10032  if (size > INT64_MAX - offset)
10033  break;
10034  offset += size;
10035  }
10036  if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
10037  /* moov atom in the header - we should make sure that this is not a
10038  * MOV-packed MPEG-PS */
10039  offset = moov_offset;
10040 
10041  while (offset < (p->buf_size - 16)) { /* Sufficient space */
10042  /* We found an actual hdlr atom */
10043  if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
10044  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
10045  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
10046  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
10047  /* We found a media handler reference atom describing an
10048  * MPEG-PS-in-MOV, return a
10049  * low score to force expanding the probe window until
10050  * mpegps_probe finds what it needs */
10051  return 5;
10052  } else {
10053  /* Keep looking */
10054  offset += 2;
10055  }
10056  }
10057  }
10058 
10059  return score;
10060 }
10061 
10062 // must be done after parsing all trak because there's no order requirement
10064 {
10065  MOVContext *mov = s->priv_data;
10066  MOVStreamContext *sc;
10067  int64_t cur_pos;
10068  int i, j;
10069  int chapter_track;
10070 
10071  for (j = 0; j < mov->nb_chapter_tracks; j++) {
10072  AVStream *st = NULL;
10073  FFStream *sti = NULL;
10074  chapter_track = mov->chapter_tracks[j];
10075  for (i = 0; i < s->nb_streams; i++) {
10076  sc = mov->fc->streams[i]->priv_data;
10077  if (sc->id == chapter_track) {
10078  st = s->streams[i];
10079  break;
10080  }
10081  }
10082  if (!st) {
10083  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
10084  continue;
10085  }
10086  sti = ffstream(st);
10087 
10088  sc = st->priv_data;
10089  cur_pos = avio_tell(sc->pb);
10090 
10091  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
10093  if (!st->attached_pic.data && sti->nb_index_entries) {
10094  // Retrieve the first frame, if possible
10095  AVIndexEntry *sample = &sti->index_entries[0];
10096  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
10097  av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
10098  goto finish;
10099  }
10100 
10101  if (ff_add_attached_pic(s, st, sc->pb, NULL, sample->size) < 0)
10102  goto finish;
10103  }
10104  } else {
10107  st->discard = AVDISCARD_ALL;
10108  for (int i = 0; i < sti->nb_index_entries; i++) {
10109  AVIndexEntry *sample = &sti->index_entries[i];
10110  int64_t end = i+1 < sti->nb_index_entries ? sti->index_entries[i+1].timestamp : st->duration;
10111  uint8_t *title;
10112  uint16_t ch;
10113  int len, title_len;
10114 
10115  if (end < sample->timestamp) {
10116  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
10117  end = AV_NOPTS_VALUE;
10118  }
10119 
10120  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
10121  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
10122  goto finish;
10123  }
10124 
10125  // the first two bytes are the length of the title
10126  len = avio_rb16(sc->pb);
10127  if (len > sample->size-2)
10128  continue;
10129  title_len = 2*len + 1;
10130  if (!(title = av_mallocz(title_len)))
10131  goto finish;
10132 
10133  // The samples could theoretically be in any encoding if there's an encd
10134  // atom following, but in practice are only utf-8 or utf-16, distinguished
10135  // instead by the presence of a BOM
10136  if (!len) {
10137  title[0] = 0;
10138  } else {
10139  ch = avio_rb16(sc->pb);
10140  if (ch == 0xfeff)
10141  avio_get_str16be(sc->pb, len, title, title_len);
10142  else if (ch == 0xfffe)
10143  avio_get_str16le(sc->pb, len, title, title_len);
10144  else {
10145  AV_WB16(title, ch);
10146  if (len == 1 || len == 2)
10147  title[len] = 0;
10148  else
10149  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
10150  }
10151  }
10152 
10153  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
10154  av_freep(&title);
10155  }
10156  }
10157 finish:
10158  avio_seek(sc->pb, cur_pos, SEEK_SET);
10159  }
10160 }
10161 
10163  int64_t value, int flags)
10164 {
10165  AVTimecode tc;
10166  char buf[AV_TIMECODE_STR_SIZE];
10167  AVRational rate = st->avg_frame_rate;
10168  int ret = av_timecode_init(&tc, rate, flags, 0, s);
10169  if (ret < 0)
10170  return ret;
10171  av_dict_set(&st->metadata, "timecode",
10172  av_timecode_make_string(&tc, buf, value), 0);
10173  return 0;
10174 }
10175 
10177 {
10178  MOVStreamContext *sc = st->priv_data;
10179  FFStream *const sti = ffstream(st);
10180  char buf[AV_TIMECODE_STR_SIZE];
10181  int64_t cur_pos = avio_tell(sc->pb);
10182  int hh, mm, ss, ff, drop;
10183 
10184  if (!sti->nb_index_entries)
10185  return -1;
10186 
10187  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
10188  avio_skip(s->pb, 13);
10189  hh = avio_r8(s->pb);
10190  mm = avio_r8(s->pb);
10191  ss = avio_r8(s->pb);
10192  drop = avio_r8(s->pb);
10193  ff = avio_r8(s->pb);
10194  snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
10195  hh, mm, ss, drop ? ';' : ':', ff);
10196  av_dict_set(&st->metadata, "timecode", buf, 0);
10197 
10198  avio_seek(sc->pb, cur_pos, SEEK_SET);
10199  return 0;
10200 }
10201 
10203 {
10204  MOVStreamContext *sc = st->priv_data;
10205  FFStream *const sti = ffstream(st);
10206  int flags = 0;
10207  int64_t cur_pos = avio_tell(sc->pb);
10208  int64_t value;
10209  AVRational tc_rate = st->avg_frame_rate;
10210  int tmcd_nb_frames = sc->tmcd_nb_frames;
10211  int rounded_tc_rate;
10212 
10213  if (!sti->nb_index_entries)
10214  return -1;
10215 
10216  if (!tc_rate.num || !tc_rate.den || !tmcd_nb_frames)
10217  return -1;
10218 
10219  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
10220  value = avio_rb32(s->pb);
10221 
10222  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
10223  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
10224  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
10225 
10226  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
10227  * not the case) and thus assume "frame number format" instead of QT one.
10228  * No sample with tmcd track can be found with a QT timecode at the moment,
10229  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
10230  * format). */
10231 
10232  /* 60 fps content have tmcd_nb_frames set to 30 but tc_rate set to 60, so
10233  * we multiply the frame number with the quotient.
10234  * See tickets #9492, #9710. */
10235  rounded_tc_rate = (tc_rate.num + tc_rate.den / 2LL) / tc_rate.den;
10236  /* Work around files where tmcd_nb_frames is rounded down from frame rate
10237  * instead of up. See ticket #5978. */
10238  if (tmcd_nb_frames == tc_rate.num / tc_rate.den &&
10239  s->strict_std_compliance < FF_COMPLIANCE_STRICT)
10240  tmcd_nb_frames = rounded_tc_rate;
10241  value = av_rescale(value, rounded_tc_rate, tmcd_nb_frames);
10242 
10244 
10245  avio_seek(sc->pb, cur_pos, SEEK_SET);
10246  return 0;
10247 }
10248 
10250  int i;
10251  if (!index || !*index) return;
10252  for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
10253  av_encryption_info_free((*index)->encrypted_samples[i]);
10254  }
10255  av_freep(&(*index)->encrypted_samples);
10256  av_freep(&(*index)->auxiliary_info_sizes);
10257  av_freep(&(*index)->auxiliary_offsets);
10258  av_freep(index);
10259 }
10260 
10262 {
10263  MOVStreamContext *sc = st->priv_data;
10264 
10265  if (!sc || --sc->refcount) {
10266  st->priv_data = NULL;
10267  return;
10268  }
10269 
10270  av_freep(&sc->tts_data);
10271  for (int i = 0; i < sc->drefs_count; i++) {
10272  av_freep(&sc->drefs[i].path);
10273  av_freep(&sc->drefs[i].dir);
10274  }
10275  av_freep(&sc->drefs);
10276 
10277  sc->drefs_count = 0;
10278 
10279  if (!sc->pb_is_copied)
10280  ff_format_io_close(s, &sc->pb);
10281 
10282  sc->pb = NULL;
10283  av_freep(&sc->chunk_offsets);
10284  av_freep(&sc->stsc_data);
10285  av_freep(&sc->sample_sizes);
10286  av_freep(&sc->keyframes);
10287  av_freep(&sc->ctts_data);
10288  av_freep(&sc->stts_data);
10289  av_freep(&sc->sdtp_data);
10290  av_freep(&sc->stps_data);
10291  av_freep(&sc->elst_data);
10292  av_freep(&sc->rap_group);
10293  av_freep(&sc->sync_group);
10294  av_freep(&sc->sgpd_sync);
10295  av_freep(&sc->sample_offsets);
10296  av_freep(&sc->open_key_samples);
10297  av_freep(&sc->display_matrix);
10298  av_freep(&sc->index_ranges);
10299  for (int i = 0; i < sc->nb_tref_tags; i++)
10300  av_freep(&sc->tref_tags[i].id);
10301  av_freep(&sc->tref_tags);
10302 
10303  if (sc->extradata)
10304  for (int i = 0; i < sc->stsd_count; i++)
10305  av_free(sc->extradata[i]);
10306  av_freep(&sc->extradata);
10307  av_freep(&sc->extradata_size);
10308 
10312 
10313  av_freep(&sc->stereo3d);
10314  av_freep(&sc->spherical);
10315  av_freep(&sc->mastering);
10316  av_freep(&sc->coll);
10317  av_freep(&sc->ambient);
10318 
10319 #if CONFIG_IAMFDEC
10320  if (sc->iamf)
10322 #endif
10323  av_freep(&sc->iamf);
10324 }
10325 
10327 {
10328  MOVContext *mov = s->priv_data;
10329  int i, j;
10330 
10331  for (i = 0; i < s->nb_streams; i++) {
10332  AVStream *st = s->streams[i];
10333 
10335  }
10336 
10337  av_freep(&mov->dv_demux);
10339  mov->dv_fctx = NULL;
10340 
10341  if (mov->meta_keys) {
10342  for (i = 1; i < mov->meta_keys_count; i++) {
10343  av_freep(&mov->meta_keys[i]);
10344  }
10345  av_freep(&mov->meta_keys);
10346  }
10347 
10348  av_freep(&mov->trex_data);
10349  av_freep(&mov->bitrates);
10350 
10351  for (i = 0; i < mov->frag_index.nb_items; i++) {
10353  for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
10354  mov_free_encryption_index(&frag[j].encryption_index);
10355  }
10357  }
10358  av_freep(&mov->frag_index.item);
10359 
10360  av_freep(&mov->aes_decrypt);
10361  av_freep(&mov->chapter_tracks);
10362  for (i = 0; i < mov->nb_heif_item; i++) {
10363  if (!mov->heif_item[i])
10364  continue;
10365  av_freep(&mov->heif_item[i]->name);
10366  av_freep(&mov->heif_item[i]->iref_list);
10367  av_freep(&mov->heif_item[i]->icc_profile);
10368  av_freep(&mov->heif_item[i]);
10369  }
10370  av_freep(&mov->heif_item);
10371  for (i = 0; i < mov->nb_heif_grid; i++) {
10372  av_freep(&mov->heif_grid[i].tile_id_list);
10375  }
10376  av_freep(&mov->heif_grid);
10377 
10378  return 0;
10379 }
10380 
10381 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
10382 {
10383  int i;
10384 
10385  for (i = 0; i < s->nb_streams; i++) {
10386  AVStream *st = s->streams[i];
10387  MOVStreamContext *sc = st->priv_data;
10388  MovTref *tag = mov_find_tref_tag(sc, MKTAG('t','m','c','d'));
10389 
10390  if (!tag)
10391  continue;
10392  if (mov_find_tref_id(tag, tmcd_id))
10393  return 1;
10394  }
10395  return 0;
10396 }
10397 
10398 /* look for a tmcd track not referenced by any video track, and export it globally */
10400 {
10401  int i;
10402 
10403  for (i = 0; i < s->nb_streams; i++) {
10404  AVStream *st = s->streams[i];
10405 
10406  if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
10407  !tmcd_is_referenced(s, st->id)) {
10408  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
10409  if (tcr) {
10410  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
10411  break;
10412  }
10413  }
10414  }
10415 }
10416 
10417 static int read_tfra(MOVContext *mov, AVIOContext *f)
10418 {
10419  int version, fieldlength, i, j;
10420  int64_t pos = avio_tell(f);
10421  uint32_t size = avio_rb32(f);
10422  unsigned track_id, item_count;
10423 
10424  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
10425  return 1;
10426  }
10427  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
10428 
10429  version = avio_r8(f);
10430  avio_rb24(f);
10431  track_id = avio_rb32(f);
10432  fieldlength = avio_rb32(f);
10433  item_count = avio_rb32(f);
10434  for (i = 0; i < item_count; i++) {
10435  int64_t time, offset;
10436  int index;
10437  MOVFragmentStreamInfo * frag_stream_info;
10438 
10439  if (avio_feof(f)) {
10440  return AVERROR_INVALIDDATA;
10441  }
10442 
10443  if (version == 1) {
10444  time = avio_rb64(f);
10445  offset = avio_rb64(f);
10446  } else {
10447  time = avio_rb32(f);
10448  offset = avio_rb32(f);
10449  }
10450 
10451  // The first sample of each stream in a fragment is always a random
10452  // access sample. So it's entry in the tfra can be used as the
10453  // initial PTS of the fragment.
10454  index = update_frag_index(mov, offset);
10455  frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
10456  if (frag_stream_info &&
10457  frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
10458  frag_stream_info->first_tfra_pts = time;
10459 
10460  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
10461  avio_r8(f);
10462  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
10463  avio_r8(f);
10464  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
10465  avio_r8(f);
10466  }
10467 
10468  avio_seek(f, pos + size, SEEK_SET);
10469  return 0;
10470 }
10471 
10473 {
10474  int64_t stream_size = avio_size(f);
10475  int64_t original_pos = avio_tell(f);
10476  int64_t seek_ret;
10477  int ret = -1;
10478  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
10479  ret = seek_ret;
10480  goto fail;
10481  }
10482  c->mfra_size = avio_rb32(f);
10483  c->have_read_mfra_size = 1;
10484  if (!c->mfra_size || c->mfra_size > stream_size) {
10485  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
10486  goto fail;
10487  }
10488  if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
10489  ret = seek_ret;
10490  goto fail;
10491  }
10492  if (avio_rb32(f) != c->mfra_size) {
10493  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
10494  goto fail;
10495  }
10496  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
10497  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
10498  goto fail;
10499  }
10500  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
10501  do {
10502  ret = read_tfra(c, f);
10503  if (ret < 0)
10504  goto fail;
10505  } while (!ret);
10506  ret = 0;
10507  c->frag_index.complete = 1;
10508 fail:
10509  seek_ret = avio_seek(f, original_pos, SEEK_SET);
10510  if (seek_ret < 0) {
10511  av_log(c->fc, AV_LOG_ERROR,
10512  "failed to seek back after looking for mfra\n");
10513  ret = seek_ret;
10514  }
10515  return ret;
10516 }
10517 
10518 static int set_icc_profile_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10519  const HEIFItem *item)
10520 {
10521  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data, nb_coded_side_data,
10523  item->icc_profile_size, 0);
10524  if (!sd)
10525  return AVERROR(ENOMEM);
10526 
10527  memcpy(sd->data, item->icc_profile, item->icc_profile_size);
10528 
10529  return 0;
10530 }
10531 
10532 static int set_display_matrix_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10533  const HEIFItem *item)
10534 {
10535  int32_t *matrix;
10536  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data,
10537  nb_coded_side_data,
10539  9 * sizeof(*matrix), 0);
10540  if (!sd)
10541  return AVERROR(ENOMEM);
10542 
10543  matrix = (int32_t*)sd->data;
10544  /* rotation is in the counter-clockwise direction whereas
10545  * av_display_rotation_set() expects its argument to be
10546  * oriented clockwise, so we need to negate it. */
10548  av_display_matrix_flip(matrix, item->hflip, item->vflip);
10549 
10550  return 0;
10551 }
10552 
10553 static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid,
10554  AVStreamGroupTileGrid *tile_grid)
10555 {
10556  MOVContext *c = s->priv_data;
10557  const HEIFItem *item = grid->item;
10558  int64_t coded_width = 0, coded_height = 0;
10559  int64_t offset = 0, pos = avio_tell(s->pb);
10560  int64_t x = 0, y = 0;
10561  int i = 0;
10562  int tile_rows, tile_cols;
10563  int flags, size;
10564 
10565  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10566  av_log(c->fc, AV_LOG_INFO, "grid box with non seekable input\n");
10567  return AVERROR_PATCHWELCOME;
10568  }
10569  if (item->is_idat_relative) {
10570  if (!c->idat_offset) {
10571  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image grid\n");
10572  return AVERROR_INVALIDDATA;
10573  }
10574  offset = c->idat_offset;
10575  }
10576 
10577  if (offset > INT64_MAX - item->extent_offset)
10578  return AVERROR_INVALIDDATA;
10579 
10580  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10581 
10582  avio_r8(s->pb); /* version */
10583  flags = avio_r8(s->pb);
10584 
10585  tile_rows = avio_r8(s->pb) + 1;
10586  tile_cols = avio_r8(s->pb) + 1;
10587  /* actual width and height of output image */
10588  tile_grid->width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10589  tile_grid->height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10590 
10591  av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width %d output_height %d\n",
10592  tile_rows, tile_cols, tile_grid->width, tile_grid->height);
10593 
10594  avio_seek(s->pb, pos, SEEK_SET);
10595 
10596  size = tile_rows * tile_cols;
10597  tile_grid->nb_tiles = grid->nb_tiles;
10598 
10599  if (tile_grid->nb_tiles != size)
10600  return AVERROR_INVALIDDATA;
10601 
10602  for (int i = 0; i < tile_cols; i++)
10603  coded_width += grid->tile_item_list[i]->width;
10604  for (int i = 0; i < size; i += tile_cols)
10605  coded_height += grid->tile_item_list[i]->height;
10606 
10607  if (coded_width > INT_MAX || coded_height > INT_MAX)
10608  return AVERROR_INVALIDDATA;
10609 
10610  tile_grid->coded_width = coded_width;
10611  tile_grid->coded_height = coded_height;
10612 
10613  tile_grid->offsets = av_calloc(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10614  if (!tile_grid->offsets)
10615  return AVERROR(ENOMEM);
10616 
10617  while (y < tile_grid->coded_height) {
10618  int left_col = i;
10619 
10620  while (x < tile_grid->coded_width) {
10621  if (i == tile_grid->nb_tiles)
10622  return AVERROR_INVALIDDATA;
10623 
10624  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10625  tile_grid->offsets[i].horizontal = x;
10626  tile_grid->offsets[i].vertical = y;
10627 
10628  x += grid->tile_item_list[i++]->width;
10629  }
10630 
10631  if (x > tile_grid->coded_width) {
10632  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10633  return AVERROR_INVALIDDATA;
10634  }
10635 
10636  x = 0;
10637  y += grid->tile_item_list[left_col]->height;
10638  }
10639 
10640  if (y > tile_grid->coded_height || i != tile_grid->nb_tiles) {
10641  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10642  return AVERROR_INVALIDDATA;
10643  }
10644 
10645  return 0;
10646 }
10647 
10648 static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid,
10649  AVStreamGroupTileGrid *tile_grid)
10650 {
10651  MOVContext *c = s->priv_data;
10652  const HEIFItem *item = grid->item;
10653  uint16_t canvas_fill_value[4];
10654  int64_t offset = 0, pos = avio_tell(s->pb);
10655  int ret = 0, flags;
10656 
10657  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10658  av_log(c->fc, AV_LOG_INFO, "iovl box with non seekable input\n");
10659  return AVERROR_PATCHWELCOME;
10660  }
10661  if (item->is_idat_relative) {
10662  if (!c->idat_offset) {
10663  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image overlay\n");
10664  return AVERROR_INVALIDDATA;
10665  }
10666  offset = c->idat_offset;
10667  }
10668 
10669  if (offset > INT64_MAX - item->extent_offset)
10670  return AVERROR_INVALIDDATA;
10671 
10672  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10673 
10674  avio_r8(s->pb); /* version */
10675  flags = avio_r8(s->pb);
10676 
10677  for (int i = 0; i < 4; i++)
10678  canvas_fill_value[i] = avio_rb16(s->pb);
10679  av_log(c->fc, AV_LOG_TRACE, "iovl: canvas_fill_value { %u, %u, %u, %u }\n",
10680  canvas_fill_value[0], canvas_fill_value[1],
10681  canvas_fill_value[2], canvas_fill_value[3]);
10682  for (int i = 0; i < 4; i++)
10683  tile_grid->background[i] = canvas_fill_value[i];
10684 
10685  /* actual width and height of output image */
10686  tile_grid->width =
10687  tile_grid->coded_width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10688  tile_grid->height =
10689  tile_grid->coded_height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10690 
10691  av_log(c->fc, AV_LOG_TRACE, "iovl: output_width %d, output_height %d\n",
10692  tile_grid->width, tile_grid->height);
10693 
10694  tile_grid->nb_tiles = grid->nb_tiles;
10695  tile_grid->offsets = av_malloc_array(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10696  if (!tile_grid->offsets) {
10697  ret = AVERROR(ENOMEM);
10698  goto fail;
10699  }
10700 
10701  for (int i = 0; i < tile_grid->nb_tiles; i++) {
10702  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10703  tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10704  tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10705  av_log(c->fc, AV_LOG_TRACE, "iovl: stream_idx[%d] %u, "
10706  "horizontal_offset[%d] %d, vertical_offset[%d] %d\n",
10707  i, tile_grid->offsets[i].idx,
10708  i, tile_grid->offsets[i].horizontal, i, tile_grid->offsets[i].vertical);
10709  }
10710 
10711 fail:
10712  avio_seek(s->pb, pos, SEEK_SET);
10713 
10714  return ret;
10715 }
10716 
10718  AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10719  const HEIFItem *ref)
10720 {
10721  MOVContext *c = s->priv_data;
10722  AVPacketSideData *sd;
10723  AVExifMetadata ifd = { 0 };
10724  AVBufferRef *buf;
10725  int64_t offset = 0, pos = avio_tell(s->pb);
10726  unsigned orientation_id = av_exif_get_tag_id("Orientation");
10727  int err;
10728 
10729  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10730  av_log(c->fc, AV_LOG_WARNING, "Exif metadata with non seekable input\n");
10731  return AVERROR_PATCHWELCOME;
10732  }
10733  if (ref->is_idat_relative) {
10734  if (!c->idat_offset) {
10735  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the Exif metadata\n");
10736  return AVERROR_INVALIDDATA;
10737  }
10738  offset = c->idat_offset;
10739  }
10740 
10741  buf = av_buffer_alloc(ref->extent_length);
10742  if (!buf)
10743  return AVERROR(ENOMEM);
10744 
10745  if (offset > INT64_MAX - ref->extent_offset) {
10746  err = AVERROR(ENOMEM);
10747  goto fail;
10748  }
10749 
10750  avio_seek(s->pb, ref->extent_offset + offset, SEEK_SET);
10751  err = avio_read(s->pb, buf->data, ref->extent_length);
10752  if (err != ref->extent_length) {
10753  if (err > 0)
10754  err = AVERROR_INVALIDDATA;
10755  goto fail;
10756  }
10757 
10758  // HEIF spec states that Exif metadata is informative. The irot item property is
10759  // the normative source of rotation information. So we remove any Orientation tag
10760  // present in the Exif buffer.
10761  err = av_exif_parse_buffer(s, buf->data, ref->extent_length, &ifd, AV_EXIF_T_OFF);
10762  if (err < 0) {
10763  av_log(s, AV_LOG_ERROR, "Unable to parse Exif metadata\n");
10764  goto fail;
10765  }
10766 
10767  err = av_exif_remove_entry(s, &ifd, orientation_id, 0);
10768  if (err < 0)
10769  goto fail;
10770  else if (!err)
10771  goto finish;
10772 
10773  av_buffer_unref(&buf);
10774  err = av_exif_write(s, &ifd, &buf, AV_EXIF_T_OFF);
10775  if (err < 0)
10776  goto fail;
10777 
10778 finish:
10779  offset = AV_RB32(buf->data) + 4;
10780  if (offset >= buf->size) {
10781  err = AVERROR_INVALIDDATA;
10782  goto fail;
10783  }
10784  sd = av_packet_side_data_new(coded_side_data, nb_coded_side_data,
10785  AV_PKT_DATA_EXIF, buf->size - offset, 0);
10786  if (!sd) {
10787  err = AVERROR(ENOMEM);
10788  goto fail;
10789  }
10790  memcpy(sd->data, buf->data + offset, buf->size - offset);
10791 
10792  err = 0;
10793 fail:
10794  av_buffer_unref(&buf);
10795  av_exif_free(&ifd);
10796  avio_seek(s->pb, pos, SEEK_SET);
10797 
10798  return err;
10799 }
10800 
10802 {
10803  MOVContext *mov = s->priv_data;
10804 
10805  for (int i = 0; i < mov->nb_heif_grid; i++) {
10807  AVStreamGroupTileGrid *tile_grid;
10808  const HEIFGrid *grid = &mov->heif_grid[i];
10809  int err, loop = 1;
10810 
10811  if (!stg)
10812  return AVERROR(ENOMEM);
10813 
10814  stg->id = grid->item->item_id;
10815  tile_grid = stg->params.tile_grid;
10816 
10817  for (int j = 0; j < grid->nb_tiles; j++) {
10818  int tile_id = grid->tile_id_list[j];
10819  int k;
10820 
10821  for (k = 0; k < mov->nb_heif_item; k++) {
10822  HEIFItem *item = mov->heif_item[k];
10823  AVStream *st;
10824 
10825  if (!item || item->item_id != tile_id)
10826  continue;
10827  st = item->st;
10828  if (!st) {
10829  av_log(s, AV_LOG_WARNING, "HEIF item id %d from grid id %d doesn't "
10830  "reference a stream\n",
10831  tile_id, grid->item->item_id);
10832  ff_remove_stream_group(s, stg);
10833  loop = 0;
10834  break;
10835  }
10836 
10837  grid->tile_item_list[j] = item;
10838  grid->tile_idx_list[j] = stg->nb_streams;
10839 
10840  err = avformat_stream_group_add_stream(stg, st);
10841  if (err < 0) {
10842  int l;
10843  if (err != AVERROR(EEXIST))
10844  return err;
10845 
10846  for (l = 0; l < stg->nb_streams; l++)
10847  if (stg->streams[l]->index == st->index)
10848  break;
10849  av_assert0(l < stg->nb_streams);
10850  grid->tile_idx_list[j] = l;
10851  }
10852 
10853  if (item->item_id != mov->primary_item_id)
10855  break;
10856  }
10857 
10858  if (k == mov->nb_heif_item) {
10859  av_assert0(loop);
10860  av_log(s, AV_LOG_WARNING, "HEIF item id %d referenced by grid id %d doesn't "
10861  "exist\n",
10862  tile_id, grid->item->item_id);
10863  ff_remove_stream_group(s, stg);
10864  loop = 0;
10865  }
10866  if (!loop)
10867  break;
10868  }
10869 
10870  if (!loop)
10871  continue;
10872 
10873  switch (grid->item->type) {
10874  case MKTAG('g','r','i','d'):
10875  err = read_image_grid(s, grid, tile_grid);
10876  break;
10877  case MKTAG('i','o','v','l'):
10878  err = read_image_iovl(s, grid, tile_grid);
10879  break;
10880  default:
10881  av_assert0(0);
10882  }
10883  if (err < 0)
10884  return err;
10885 
10886  for (int j = 0; j < grid->item->nb_iref_list; j++) {
10887  HEIFItem *ref = get_heif_item(mov, grid->item->iref_list[j].item_id);
10888 
10889  av_assert0(ref);
10890  switch(ref->type) {
10891  case MKTAG('E','x','i','f'):
10892  err = mov_parse_exif_item(s, &tile_grid->coded_side_data,
10893  &tile_grid->nb_coded_side_data, ref);
10894  if (err < 0 && (s->error_recognition & AV_EF_EXPLODE))
10895  return err;
10896  break;
10897  default:
10898  break;
10899  }
10900  }
10901 
10902  /* rotation */
10903  if (grid->item->rotation || grid->item->hflip || grid->item->vflip) {
10905  &tile_grid->nb_coded_side_data, grid->item);
10906  if (err < 0)
10907  return err;
10908  }
10909 
10910  /* ICC profile */
10911  if (grid->item->icc_profile_size) {
10912  err = set_icc_profile_from_item(&tile_grid->coded_side_data,
10913  &tile_grid->nb_coded_side_data, grid->item);
10914  if (err < 0)
10915  return err;
10916  }
10917 
10918  if (grid->item->name)
10919  av_dict_set(&stg->metadata, "title", grid->item->name, 0);
10920  if (grid->item->item_id == mov->primary_item_id)
10922  }
10923 
10924  return 0;
10925 }
10926 
10928 {
10929  MOVContext *mov = s->priv_data;
10930  int err;
10931 
10932  for (int i = 0; i < mov->nb_heif_item; i++) {
10933  HEIFItem *item = mov->heif_item[i];
10934  MOVStreamContext *sc;
10935  AVStream *st;
10936  int64_t offset = 0;
10937 
10938  if (!item)
10939  continue;
10940  if (!item->st) {
10941  continue;
10942  }
10943  if (item->is_idat_relative) {
10944  if (!mov->idat_offset) {
10945  av_log(s, AV_LOG_ERROR, "Missing idat box for item %d\n", item->item_id);
10946  return AVERROR_INVALIDDATA;
10947  }
10948  offset = mov->idat_offset;
10949  }
10950 
10951  st = item->st;
10952  sc = st->priv_data;
10953  st->codecpar->width = item->width;
10954  st->codecpar->height = item->height;
10955 
10956  sc->sample_size = sc->stsz_sample_size = item->extent_length;
10957  sc->sample_count = 1;
10958 
10959  err = sanity_checks(s, sc, st->index);
10960  if (err)
10961  return AVERROR_INVALIDDATA;
10962 
10963  if (offset > INT64_MAX - item->extent_offset)
10964  return AVERROR_INVALIDDATA;
10965 
10966  sc->chunk_offsets[0] = item->extent_offset + offset;
10967 
10968  if (item->item_id == mov->primary_item_id)
10970 
10971  for (int j = 0; j < item->nb_iref_list; j++) {
10972  HEIFItem *ref = get_heif_item(mov, item->iref_list[j].item_id);
10973 
10974  av_assert0(ref);
10975  switch(ref->type) {
10976  case MKTAG('E','x','i','f'):
10979  if (err < 0 && (s->error_recognition & AV_EF_EXPLODE))
10980  return err;
10981  break;
10982  default:
10983  break;
10984  }
10985  }
10986 
10987  if (item->rotation || item->hflip || item->vflip) {
10989  &st->codecpar->nb_coded_side_data, item);
10990  if (err < 0)
10991  return err;
10992  }
10993 
10994  mov_build_index(mov, st);
10995  }
10996 
10997  if (mov->nb_heif_grid) {
10998  err = mov_parse_tiles(s);
10999  if (err < 0)
11000  return err;
11001  }
11002 
11003  return 0;
11004 }
11005 
11007 {
11008  int err;
11009 
11010  for (int i = 0; i < s->nb_streams; i++) {
11011  AVStreamGroup *stg;
11012  const AVDictionaryEntry *tcr;
11013  AVStream *st = s->streams[i];
11014 
11015  if (st->codecpar->codec_tag != MKTAG('t','m','c','d'))
11016  continue;
11017 
11019  if (!stg)
11020  return AVERROR(ENOMEM);
11021 
11022  stg->id = st->id;
11023  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
11024 
11025  for (int j = 0; j < s->nb_streams; j++) {
11026  AVStream *st2 = s->streams[j];
11027  MOVStreamContext *sc2 = st2->priv_data;
11028  MovTref *tag = mov_find_tref_tag(sc2, MKTAG('t','m','c','d'));
11029 
11030  if (!tag)
11031  continue;
11032 
11033  for (int k = 0; k < tag->nb_id; k++) {
11034  if (tag->id[k] != st->id)
11035  continue;
11036 
11037  err = avformat_stream_group_add_stream(stg, st2);
11038  if (err < 0)
11039  return err;
11040 
11041  if (tcr)
11042  av_dict_set(&st2->metadata, "timecode", tcr->value, AV_DICT_DONT_OVERWRITE);
11043  }
11044  }
11045 
11046  if (!stg->nb_streams) {
11047  ff_remove_stream_group(s, stg);
11048  continue;
11049  }
11050 
11051  err = avformat_stream_group_add_stream(stg, st);
11052  if (err < 0)
11053  return err;
11054 
11055  stg->params.tref->metadata_index = stg->nb_streams - 1;
11056  }
11058 
11059  return 0;
11060 }
11061 
11063  uint32_t *tref_id, int nb_tref_id, int first_index)
11064 {
11065  if (!nb_tref_id)
11066  return NULL;
11067 
11068  for (int i = first_index; i < s->nb_streams; i++)
11069  for (int j = 0; j < nb_tref_id; j++)
11070  if (s->streams[i]->index != st->index &&
11071  s->streams[i]->id == tref_id[j])
11072  return s->streams[i];
11073 
11074  return NULL;
11075 }
11076 
11078 {
11079  int err;
11080 
11081  // Don't try to add a group if there's only one track
11082  if (s->nb_streams <= 1)
11083  return 0;
11084 
11085  for (int i = 0; i < s->nb_streams; i++) {
11086  AVStreamGroup *stg;
11087  AVStream *st = s->streams[i];
11088  AVStream *st_base;
11089  MOVStreamContext *sc = st->priv_data;
11090  MovTref *tag = mov_find_tref_tag(sc, MKTAG('s','b','a','s'));
11091  int j = 0;
11092 
11093  /* Find an enhancement stream. */
11094  if (st->codecpar->codec_id != AV_CODEC_ID_LCEVC || !tag)
11095  continue;
11096 
11098  if (!stg)
11099  return AVERROR(ENOMEM);
11100 
11101  stg->id = st->id;
11102  stg->params.layered_video->width = st->codecpar->width;
11103  stg->params.layered_video->height = st->codecpar->height;
11104 
11105  while (st_base = mov_find_reference_track(s, st, tag->id, tag->nb_id, j)) {
11106  err = avformat_stream_group_add_stream(stg, st_base);
11107  if (err < 0)
11108  return err;
11109 
11110  j = st_base->index + 1;
11111  }
11112  if (!j) {
11113  int loglevel = (s->error_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
11114  av_log(s, loglevel, "Failed to find base stream for LCEVC stream\n");
11115  ff_remove_stream_group(s, stg);
11116  if (s->error_recognition & AV_EF_EXPLODE)
11117  return AVERROR_INVALIDDATA;
11118  continue;
11119  }
11120 
11121  err = avformat_stream_group_add_stream(stg, st);
11122  if (err < 0)
11123  return err;
11124 
11125  stg->params.layered_video->el_index = stg->nb_streams - 1;
11126  }
11127 
11128  return 0;
11129 }
11130 
11132 {
11133  int err;
11134 
11135  if (s->nb_streams <= 1)
11136  return 0;
11137 
11138  /* Identify legacy dual-track Dolby Vision profile 7 carriage per Annex
11139  * C of "Dolby Vision Streams Within the ISO Base Media File Format",
11140  * v2.7.1. The 'vdep' track reference type is shared with depth video
11141  * and multi-view dependencies. The spec-mandated DV-specific sample
11142  * entry combined with the dvcC configuration record disambiguates
11143  * those uses. */
11144  for (int i = 0; i < s->nb_streams; i++) {
11145  AVStreamGroup *stg;
11146  AVStream *st = s->streams[i];
11147  AVStream *st_base;
11148  MOVStreamContext *sc = st->priv_data;
11149  MovTref *tag = mov_find_tref_tag(sc, MKTAG('v','d','e','p'));
11150  const AVPacketSideData *sd;
11152 
11153  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !tag)
11154  continue;
11155 
11156  if (st->codecpar->codec_tag != MKTAG('d','v','h','e') &&
11157  st->codecpar->codec_tag != MKTAG('d','v','h','1'))
11158  continue;
11159 
11163  if (!sd)
11164  continue;
11165  dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
11166 
11167  /* EL track of a dual-track stream: rpu_present_flag = 1,
11168  * el_present_flag = 1, bl_present_flag = 0, dv_profile = 7. */
11169  if (dovi->dv_profile != 7 || !dovi->rpu_present_flag ||
11170  !dovi->el_present_flag || dovi->bl_present_flag)
11171  continue;
11172 
11173  st_base = mov_find_reference_track(s, st, tag->id, tag->nb_id, 0);
11174  if (!st_base) {
11175  int loglevel = (s->error_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
11176  av_log(s, loglevel, "Failed to find base layer for Dolby Vision EL stream\n");
11177  if (s->error_recognition & AV_EF_EXPLODE)
11178  return AVERROR_INVALIDDATA;
11179  continue;
11180  }
11181 
11183  if (!stg)
11184  return AVERROR(ENOMEM);
11185 
11186  stg->id = st->id;
11187 
11188  err = avformat_stream_group_add_stream(stg, st_base);
11189  if (err < 0)
11190  return err;
11191 
11192  err = avformat_stream_group_add_stream(stg, st);
11193  if (err < 0)
11194  return err;
11195 
11196  stg->params.layered_video->el_index = stg->nb_streams - 1;
11197  stg->params.layered_video->width = st_base->codecpar->width;
11198  stg->params.layered_video->height = st_base->codecpar->height;
11199  }
11200 
11201  return 0;
11202 }
11203 
11205 {
11206  int highest_id = 0, lowest_iamf_id = INT_MAX;
11207 
11208  for (int i = 0; i < s->nb_streams; i++) {
11209  const AVStream *st = s->streams[i];
11210  const MOVStreamContext *sc = st->priv_data;
11211  if (!sc->iamf)
11212  highest_id = FFMAX(highest_id, st->id);
11213  }
11214 
11215  for (int i = 0; i < s->nb_stream_groups; i++) {
11216  AVStreamGroup *stg = s->stream_groups[i];
11218  continue;
11219  for (int j = 0; j < stg->nb_streams; j++) {
11220  AVStream *st = stg->streams[j];
11221  lowest_iamf_id = FFMIN(lowest_iamf_id, st->id);
11222  }
11223  }
11224 
11225  if (highest_id < lowest_iamf_id)
11226  return;
11227 
11228  highest_id += !lowest_iamf_id;
11229  for (int i = 0; highest_id > 1 && i < s->nb_stream_groups; i++) {
11230  AVStreamGroup *stg = s->stream_groups[i];
11232  continue;
11233  for (int j = 0; j < stg->nb_streams; j++) {
11234  AVStream *st = stg->streams[j];
11235  MOVStreamContext *sc = st->priv_data;
11236  st->id += highest_id;
11237  sc->iamf_stream_offset = highest_id;
11238  }
11239  }
11240 }
11241 
11243 {
11244  MOVContext *mov = s->priv_data;
11245  AVIOContext *pb = s->pb;
11246  int j, err;
11247  MOVAtom atom = { AV_RL32("root") };
11248  int i;
11249 
11250  mov->fc = s;
11251  mov->trak_index = -1;
11252  mov->primary_item_id = -1;
11253  mov->cur_item_id = -1;
11254  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
11255  if (pb->seekable & AVIO_SEEKABLE_NORMAL)
11256  atom.size = avio_size(pb);
11257  else
11258  atom.size = INT64_MAX;
11259 
11260  /* check MOV header */
11261  do {
11262  if (mov->moov_retry)
11263  avio_seek(pb, 0, SEEK_SET);
11264  if ((err = mov_read_default(mov, pb, atom)) < 0) {
11265  av_log(s, AV_LOG_ERROR, "error reading header\n");
11266  return err;
11267  }
11268  } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
11269  !mov->found_moov && (!mov->found_iloc || !mov->found_iinf) && !mov->moov_retry++);
11270  if (!mov->found_moov && !mov->found_iloc && !mov->found_iinf) {
11271  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
11272  return AVERROR_INVALIDDATA;
11273  }
11274  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
11275 
11276  if (mov->found_iloc && mov->found_iinf) {
11277  err = mov_parse_heif_items(s);
11278  if (err < 0)
11279  return err;
11280  }
11281  // prevent iloc and iinf boxes from being parsed while reading packets.
11282  // this is needed because an iinf box may have been parsed but ignored
11283  // for having old infe boxes which create no streams.
11284  mov->found_iloc = mov->found_iinf = 1;
11285 
11286  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
11287  if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
11289  for (i = 0; i < s->nb_streams; i++)
11290  if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
11291  mov_read_timecode_track(s, s->streams[i]);
11292  } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
11293  mov_read_rtmd_track(s, s->streams[i]);
11294  }
11295  }
11296 
11297  /* copy timecode metadata from tmcd tracks to the related video streams */
11298  err = mov_parse_tmcd_streams(s);
11299  if (err < 0)
11300  return err;
11301 
11302  /* Create LCEVC stream groups. */
11303  err = mov_parse_lcevc_streams(s);
11304  if (err < 0)
11305  return err;
11306 
11307  /* Create Dolby Vision Profile 7 stream groups. */
11308  err = mov_parse_dovi_streams(s);
11309  if (err < 0)
11310  return err;
11311 
11312  for (i = 0; i < s->nb_streams; i++) {
11313  AVStream *st = s->streams[i];
11314  FFStream *const sti = ffstream(st);
11315  MOVStreamContext *sc = st->priv_data;
11316  uint32_t dvdsub_clut[FF_DVDCLUT_CLUT_LEN] = {0};
11317  fix_timescale(mov, sc);
11318 
11319  /* Set the primary extradata based on the first Sample if it doesn't reference the first stsd entry. */
11320  if (sc->stsc_count && sc->extradata_size && !sc->iamf &&
11321  sc->stsc_data[0].id > 1 && sc->stsc_data[0].id <= sc->stsd_count) {
11322  sc->last_stsd_index = sc->stsc_data[0].id - 1;
11323  av_freep(&st->codecpar->extradata);
11325  if (sc->extradata_size[sc->last_stsd_index]) {
11327  if (!st->codecpar->extradata)
11328  return AVERROR(ENOMEM);
11329  memcpy(st->codecpar->extradata, sc->extradata[sc->last_stsd_index], sc->extradata_size[sc->last_stsd_index]);
11330  }
11331  }
11332 
11333  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
11334  st->codecpar->codec_id == AV_CODEC_ID_AAC) {
11336  }
11337  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
11339  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
11341  if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
11342  st->codecpar->width = sc->width;
11343  st->codecpar->height = sc->height;
11344  }
11347 
11348  for (j = 0; j < FF_DVDCLUT_CLUT_LEN; j++)
11349  dvdsub_clut[j] = AV_RB32(st->codecpar->extradata + j * 4);
11350 
11351  err = ff_dvdclut_yuv_to_rgb(dvdsub_clut, FF_DVDCLUT_CLUT_SIZE);
11352  if (err < 0)
11353  return err;
11354 
11355  av_freep(&st->codecpar->extradata);
11356  st->codecpar->extradata_size = 0;
11357 
11359  st->codecpar);
11360  if (err < 0)
11361  return err;
11362  }
11363  }
11364  if (mov->handbrake_version &&
11365  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
11366  st->codecpar->codec_id == AV_CODEC_ID_MP3) {
11367  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
11369  }
11370  }
11371 
11372  if (mov->trex_data || mov->use_mfra_for > 0) {
11373  for (i = 0; i < s->nb_streams; i++) {
11374  AVStream *st = s->streams[i];
11375  MOVStreamContext *sc = st->priv_data;
11376  if (sc->duration_for_fps > 0) {
11377  /* Akin to sc->data_size * 8 * sc->time_scale / sc->duration_for_fps but accounting for overflows. */
11379  if (st->codecpar->bit_rate == INT64_MIN) {
11380  av_log(s, AV_LOG_WARNING, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
11381  sc->data_size, sc->time_scale);
11382  st->codecpar->bit_rate = 0;
11383  if (s->error_recognition & AV_EF_EXPLODE)
11384  return AVERROR_INVALIDDATA;
11385  }
11386  }
11387  }
11388  }
11389 
11390  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
11391  if (mov->bitrates[i]) {
11392  s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
11393  }
11394  }
11395 
11397 
11398  for (i = 0; i < s->nb_streams; i++) {
11399  AVStream *st = s->streams[i];
11400  MOVStreamContext *sc = st->priv_data;
11401 
11402  switch (st->codecpar->codec_type) {
11403  case AVMEDIA_TYPE_AUDIO:
11404  err = ff_replaygain_export(st, s->metadata);
11405  if (err < 0)
11406  return err;
11407  break;
11408  case AVMEDIA_TYPE_VIDEO:
11409  if (sc->display_matrix) {
11412  (uint8_t*)sc->display_matrix, sizeof(int32_t) * 9, 0))
11413  return AVERROR(ENOMEM);
11414 
11415  sc->display_matrix = NULL;
11416  }
11417  if (sc->stereo3d) {
11420  (uint8_t *)sc->stereo3d, sc->stereo3d_size, 0))
11421  return AVERROR(ENOMEM);
11422 
11423  sc->stereo3d = NULL;
11424  }
11425  if (sc->spherical) {
11428  (uint8_t *)sc->spherical, sc->spherical_size, 0))
11429  return AVERROR(ENOMEM);
11430 
11431  sc->spherical = NULL;
11432  }
11433  if (sc->mastering) {
11436  (uint8_t *)sc->mastering, sc->mastering_size, 0))
11437  return AVERROR(ENOMEM);
11438 
11439  sc->mastering = NULL;
11440  }
11441  if (sc->coll) {
11444  (uint8_t *)sc->coll, sc->coll_size, 0))
11445  return AVERROR(ENOMEM);
11446 
11447  sc->coll = NULL;
11448  }
11449  if (sc->ambient) {
11452  (uint8_t *) sc->ambient, sc->ambient_size, 0))
11453  return AVERROR(ENOMEM);
11454 
11455  sc->ambient = NULL;
11456  }
11457  break;
11458  }
11459  }
11460 
11461  fix_stream_ids(s);
11462 
11464 
11465  for (i = 0; i < mov->frag_index.nb_items; i++)
11466  if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
11467  mov->frag_index.item[i].headers_read = 1;
11468 
11469  return 0;
11470 }
11471 
11473 {
11475  int64_t best_dts = INT64_MAX;
11476  int i;
11477  MOVContext *mov = s->priv_data;
11478  int no_interleave = !mov->interleaved_read || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL);
11479  for (i = 0; i < s->nb_streams; i++) {
11480  AVStream *avst = s->streams[i];
11481  FFStream *const avsti = ffstream(avst);
11482  MOVStreamContext *msc = avst->priv_data;
11483  if (msc->pb && msc->current_sample < avsti->nb_index_entries) {
11484  AVIndexEntry *current_sample = &avsti->index_entries[msc->current_sample];
11485  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
11486  uint64_t dtsdiff = best_dts > dts ? best_dts - (uint64_t)dts : ((uint64_t)dts - best_dts);
11487  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
11488  if (!sample || (no_interleave && current_sample->pos < sample->pos) ||
11489  ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
11490  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
11491  ((dtsdiff <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
11492  (dtsdiff > AV_TIME_BASE && dts < best_dts && mov->interleaved_read)))))) {
11493  sample = current_sample;
11494  best_dts = dts;
11495  *st = avst;
11496  }
11497  }
11498  }
11499  return sample;
11500 }
11501 
11502 static int should_retry(AVIOContext *pb, int error_code) {
11503  if (error_code == AVERROR_EOF || avio_feof(pb))
11504  return 0;
11505 
11506  return 1;
11507 }
11508 
11509 static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
11510 {
11511  int ret;
11512  MOVContext *mov = s->priv_data;
11513 
11514  if (index >= 0 && index < mov->frag_index.nb_items)
11515  target = mov->frag_index.item[index].moof_offset;
11516  if (target >= 0 && avio_seek(s->pb, target, SEEK_SET) != target) {
11517  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
11518  return AVERROR_INVALIDDATA;
11519  }
11520 
11521  mov->next_root_atom = 0;
11522  if ((index < 0 && target >= 0) || index >= mov->frag_index.nb_items)
11523  index = search_frag_moof_offset(&mov->frag_index, target);
11524  if (index >= 0 && index < mov->frag_index.nb_items &&
11525  mov->frag_index.item[index].moof_offset == target) {
11526  if (index + 1 < mov->frag_index.nb_items)
11527  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
11528  if (mov->frag_index.item[index].headers_read)
11529  return 0;
11530  mov->frag_index.item[index].headers_read = 1;
11531  }
11532 
11533  mov->found_mdat = 0;
11534 
11535  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
11536  if (ret < 0)
11537  return ret;
11538  if (avio_feof(s->pb))
11539  return AVERROR_EOF;
11540  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
11541 
11542  return 1;
11543 }
11544 
11546 {
11547  MOVStreamContext *sc = st->priv_data;
11548  uint8_t *side, *extradata;
11549  int extradata_size;
11550 
11551  /* Save the current index. */
11552  sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
11553 
11554  /* Notify the decoder that extradata changed. */
11555  extradata_size = sc->extradata_size[sc->last_stsd_index];
11556  extradata = sc->extradata[sc->last_stsd_index];
11557  if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
11560  extradata_size);
11561  if (!side)
11562  return AVERROR(ENOMEM);
11563  memcpy(side, extradata, extradata_size);
11564  }
11565 
11566  return 0;
11567 }
11568 
11569 static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int src_size)
11570 {
11571  /* We can't make assumptions about the structure of the payload,
11572  because it may include multiple cdat and cdt2 samples. */
11573  const uint32_t cdat = AV_RB32("cdat");
11574  const uint32_t cdt2 = AV_RB32("cdt2");
11575  int ret, out_size = 0;
11576 
11577  /* a valid payload must have size, 4cc, and at least 1 byte pair: */
11578  if (src_size < 10)
11579  return AVERROR_INVALIDDATA;
11580 
11581  /* avoid an int overflow: */
11582  if ((src_size - 8) / 2 >= INT_MAX / 3)
11583  return AVERROR_INVALIDDATA;
11584 
11585  ret = av_new_packet(pkt, ((src_size - 8) / 2) * 3);
11586  if (ret < 0)
11587  return ret;
11588 
11589  /* parse and re-format the c608 payload in one pass. */
11590  while (src_size >= 10) {
11591  const uint32_t atom_size = avio_rb32(pb);
11592  const uint32_t atom_type = avio_rb32(pb);
11593  const uint32_t data_size = atom_size - 8;
11594  const uint8_t cc_field =
11595  atom_type == cdat ? 1 :
11596  atom_type == cdt2 ? 2 :
11597  0;
11598 
11599  /* account for bytes consumed for atom size and type. */
11600  src_size -= 8;
11601 
11602  /* make sure the data size stays within the buffer boundaries. */
11603  if (data_size < 2 || data_size > src_size) {
11605  break;
11606  }
11607 
11608  /* make sure the data size is consistent with N byte pairs. */
11609  if (data_size % 2 != 0) {
11611  break;
11612  }
11613 
11614  if (!cc_field) {
11615  /* neither cdat or cdt2 ... skip it */
11616  avio_skip(pb, data_size);
11617  src_size -= data_size;
11618  continue;
11619  }
11620 
11621  for (uint32_t i = 0; i < data_size; i += 2) {
11622  pkt->data[out_size] = (0x1F << 3) | (1 << 2) | (cc_field - 1);
11623  pkt->data[out_size + 1] = avio_r8(pb);
11624  pkt->data[out_size + 2] = avio_r8(pb);
11625  out_size += 3;
11626  src_size -= 2;
11627  }
11628  }
11629 
11630  if (src_size > 0)
11631  /* skip any remaining unread portion of the input payload */
11632  avio_skip(pb, src_size);
11633 
11635  return ret;
11636 }
11637 
11639  int64_t current_index, AVPacket *pkt)
11640 {
11641  MOVContext *mov = s->priv_data;
11642  MOVStreamContext *sc = st->priv_data;
11643 
11644  pkt->stream_index = sc->ffindex;
11645  pkt->dts = sample->timestamp;
11646  if (sample->flags & AVINDEX_DISCARD_FRAME) {
11648  }
11649  if (sc->stts_count && sc->tts_index < sc->tts_count)
11650  pkt->duration = sc->tts_data[sc->tts_index].duration;
11651  if (sc->ctts_count && sc->tts_index < sc->tts_count) {
11653  } else {
11654  if (pkt->duration == 0) {
11655  int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
11657  if (next_dts >= pkt->dts)
11658  pkt->duration = next_dts - pkt->dts;
11659  }
11660  pkt->pts = pkt->dts;
11661  }
11662 
11663  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && !mov->fragment.found_tfhd &&
11665  int64_t pts = av_rescale_q(pkt->pts, st->time_base, (AVRational){ 1, st->codecpar->sample_rate });
11666  int64_t total = av_rescale_q(st->duration, st->time_base, (AVRational){ 1, st->codecpar->sample_rate });
11668 
11669  if (av_sat_add64(pkt->pts, pkt->duration) > st->duration)
11670  duration = st->duration - pkt->pts;
11671  duration = av_rescale_q(duration, st->time_base, (AVRational){ 1, st->codecpar->sample_rate });
11672 
11673  if (!ffstream(st)->first_discard_sample)
11675  if (!ffstream(st)->last_discard_sample)
11676  ffstream(st)->last_discard_sample = total;
11677  }
11678 
11679  if (sc->tts_data && sc->tts_index < sc->tts_count) {
11680  /* update tts context */
11681  sc->tts_sample++;
11682  if (sc->tts_index < sc->tts_count &&
11683  sc->tts_data[sc->tts_index].count == sc->tts_sample) {
11684  sc->tts_index++;
11685  sc->tts_sample = 0;
11686  }
11687  }
11688 
11689  if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
11690  uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
11691  uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
11692  pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
11693  }
11694  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
11695  pkt->pos = sample->pos;
11696 
11697  /* Multiple stsd handling. */
11698  if (sc->stsc_data) {
11699  if (sc->stsc_data[sc->stsc_index].id > 0 &&
11700  sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
11701  sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
11702  int ret = mov_change_extradata(st, pkt);
11703  if (ret < 0)
11704  return ret;
11705  }
11706 
11707  /* Update the stsc index for the next sample */
11708  sc->stsc_sample++;
11709  if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
11710  mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
11711  sc->stsc_index++;
11712  sc->stsc_sample = 0;
11713  }
11714  }
11715 
11716  return 0;
11717 }
11718 
11720 {
11721  MOVContext *mov = s->priv_data;
11722  MOVStreamContext *sc;
11724  AVStream *st = NULL;
11725  FFStream *avsti = NULL;
11726  int64_t current_index;
11727  int ret;
11728  int i;
11729  mov->fc = s;
11730  retry:
11731  if (s->pb->pos == 0) {
11732 
11733  // Discard current fragment index
11734  if (mov->frag_index.allocated_size > 0) {
11735  for(int i = 0; i < mov->frag_index.nb_items; i++) {
11737  }
11738  av_freep(&mov->frag_index.item);
11739  mov->frag_index.nb_items = 0;
11740  mov->frag_index.allocated_size = 0;
11741  mov->frag_index.current = -1;
11742  mov->frag_index.complete = 0;
11743  }
11744 
11745  for (i = 0; i < s->nb_streams; i++) {
11746  AVStream *avst = s->streams[i];
11747  MOVStreamContext *msc = avst->priv_data;
11748 
11749  // Clear current sample
11750  mov_current_sample_set(msc, 0);
11751  msc->tts_index = 0;
11752 
11753  // Discard current index entries
11754  avsti = ffstream(avst);
11755  if (avsti->index_entries_allocated_size > 0) {
11756  av_freep(&avsti->index_entries);
11757  avsti->index_entries_allocated_size = 0;
11758  avsti->nb_index_entries = 0;
11759  }
11760  }
11761 
11762  if ((ret = mov_switch_root(s, -1, -1)) < 0)
11763  return ret;
11764  }
11765  sample = mov_find_next_sample(s, &st);
11766  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
11767  if (!mov->next_root_atom)
11768  return AVERROR_EOF;
11769  if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
11770  return ret;
11771  goto retry;
11772  }
11773  sc = st->priv_data;
11774  /* must be done just before reading, to avoid infinite loop on sample */
11775  current_index = sc->current_index;
11777 
11778  if (mov->next_root_atom) {
11779  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
11780  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
11781  }
11782 
11783  if (st->discard != AVDISCARD_ALL || sc->iamf) {
11784  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
11785  if (ret64 != sample->pos) {
11786  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
11787  sc->ffindex, sample->pos);
11788  if (should_retry(sc->pb, ret64)) {
11790  } else if (ret64 < 0) {
11791  return (int)ret64;
11792  }
11793  return AVERROR_INVALIDDATA;
11794  }
11795 
11796  if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
11797  av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
11798  goto retry;
11799  }
11800 
11801  if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
11802  ret = get_eia608_packet(sc->pb, pkt, sample->size);
11803 #if CONFIG_IAMFDEC
11804  else if (sc->iamf) {
11805  int64_t pts, dts, pos, duration;
11806  int flags, size = sample->size;
11807  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
11808  pts = pkt->pts; dts = pkt->dts;
11809  pos = pkt->pos; flags = pkt->flags;
11810  duration = pkt->duration;
11811  while (!ret && size > 0) {
11812  ret = ff_iamf_read_packet(s, sc->iamf, sc->pb, size, sc->iamf_stream_offset, pkt);
11813  if (ret < 0) {
11814  if (should_retry(sc->pb, ret))
11816  return ret;
11817  }
11818  size -= ret;
11819 
11820  if (pkt->flags & AV_PKT_FLAG_DISCARD) {
11822  ret = 0;
11823  continue;
11824  }
11825  pkt->pts = pts; pkt->dts = dts;
11826  pkt->pos = pos; pkt->flags |= flags;
11827  pkt->duration = duration;
11828  ret = ff_buffer_packet(s, pkt);
11829  }
11830  if (!ret)
11831  return FFERROR_REDO;
11832  }
11833 #endif
11834  else if (st->codecpar->codec_id == AV_CODEC_ID_APV && sample->size > 4) {
11835  uint32_t au_size = avio_rb32(sc->pb);
11836  int explode = !!(mov->fc->error_recognition & AV_EF_EXPLODE);
11837  if (au_size > sample->size - 4) {
11838  av_log(s, explode ? AV_LOG_ERROR : AV_LOG_WARNING,
11839  "APV au_size %u exceeds sample body %d\n",
11840  au_size, sample->size - 4);
11841  if (explode)
11842  return AVERROR_INVALIDDATA;
11843  au_size = sample->size - 4;
11844  }
11845  ret = av_get_packet(sc->pb, pkt, au_size);
11846  } else
11847  ret = av_get_packet(sc->pb, pkt, sample->size);
11848  if (ret < 0) {
11849  if (should_retry(sc->pb, ret)) {
11851  }
11852  return ret;
11853  }
11854 #if CONFIG_DV_DEMUXER
11855  if (mov->dv_demux && sc->dv_audio_container) {
11858  if (ret < 0)
11859  return ret;
11861  if (ret < 0)
11862  return ret;
11863  }
11864 #endif
11865  if (sc->has_palette) {
11866  uint8_t *pal;
11867 
11869  if (!pal) {
11870  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
11871  } else {
11872  memcpy(pal, sc->palette, AVPALETTE_SIZE);
11873  sc->has_palette = 0;
11874  }
11875  }
11876  if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !ffstream(st)->need_parsing && pkt->size > 4) {
11877  if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
11879  }
11880  }
11881 
11882  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
11883  if (ret < 0)
11884  return ret;
11885 
11886  if (st->discard == AVDISCARD_ALL)
11887  goto retry;
11888 
11889  if (mov->aax_mode)
11890  aax_filter(pkt->data, pkt->size, mov);
11891 
11892  ret = cenc_filter(mov, st, sc, pkt, current_index);
11893  if (ret < 0) {
11894  return ret;
11895  }
11896 
11897  return 0;
11898 }
11899 
11901 {
11902  MOVContext *mov = s->priv_data;
11903  int index;
11904 
11905  if (!mov->frag_index.complete)
11906  return 0;
11907 
11908  index = search_frag_timestamp(s, &mov->frag_index, st, timestamp);
11909  if (index < 0)
11910  index = 0;
11911  if (!mov->frag_index.item[index].headers_read)
11912  return mov_switch_root(s, -1, index);
11913  if (index + 1 < mov->frag_index.nb_items)
11914  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
11915 
11916  return 0;
11917 }
11918 
11919 static int is_open_key_sample(const MOVStreamContext *sc, int sample)
11920 {
11921  // TODO: a bisect search would scale much better
11922  for (int i = 0; i < sc->open_key_samples_count; i++) {
11923  const int oks = sc->open_key_samples[i];
11924  if (oks == sample)
11925  return 1;
11926  if (oks > sample) /* list is monotically increasing so we can stop early */
11927  break;
11928  }
11929  return 0;
11930 }
11931 
11932 /*
11933  * Some key sample may be key frames but not IDR frames, so a random access to
11934  * them may not be allowed.
11935  */
11936 static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
11937 {
11938  MOVStreamContext *sc = st->priv_data;
11939  FFStream *const sti = ffstream(st);
11940  int64_t key_sample_dts, key_sample_pts;
11941 
11942  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
11943  return 1;
11944 
11945  if (sample >= sc->sample_offsets_count)
11946  return 1;
11947 
11948  av_assert0(sample >= 0);
11949  key_sample_dts = sti->index_entries[sample].timestamp;
11950  key_sample_pts = key_sample_dts + sc->sample_offsets[sample] + sc->dts_shift;
11951 
11952  /*
11953  * If the sample needs to be presented before an open key sample, they may
11954  * not be decodable properly, even though they come after in decoding
11955  * order.
11956  */
11957  if (is_open_key_sample(sc, sample) && key_sample_pts > requested_pts)
11958  return 0;
11959 
11960  return 1;
11961 }
11962 
11963 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
11964 {
11965  MOVStreamContext *sc = st->priv_data;
11966  FFStream *const sti = ffstream(st);
11967  int sample, time_sample, ret, requested_sample;
11968  int64_t next_ts;
11969  unsigned int i;
11970 
11971  // Here we consider timestamp to be PTS, hence try to offset it so that we
11972  // can search over the DTS timeline.
11973  timestamp -= (sc->min_corrected_pts + sc->dts_shift);
11974 
11975  ret = mov_seek_fragment(s, st, timestamp);
11976  if (ret < 0)
11977  return ret;
11978 
11979  for (;;) {
11980  sample = av_index_search_timestamp(st, timestamp, flags);
11981  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
11982  if (sample < 0 && sti->nb_index_entries && timestamp < sti->index_entries[0].timestamp)
11983  sample = 0;
11984  if (sample < 0) /* not sure what to do */
11985  return AVERROR_INVALIDDATA;
11986 
11987  if (!sample || can_seek_to_key_sample(st, sample, timestamp))
11988  break;
11989 
11990  next_ts = timestamp - FFMAX(sc->min_sample_duration, 1);
11991  requested_sample = av_index_search_timestamp(st, next_ts, flags);
11992  if (requested_sample < 0)
11993  return AVERROR_INVALIDDATA;
11994 
11995  // If we've reached a different sample trying to find a good pts to
11996  // seek to, give up searching because we'll end up seeking back to
11997  // sample 0 on every seek.
11998  if (sample != requested_sample && !can_seek_to_key_sample(st, requested_sample, next_ts))
11999  break;
12000 
12001  timestamp = next_ts;
12002  }
12003 
12005  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
12006  /* adjust time to sample index */
12007  if (sc->tts_data) {
12008  time_sample = 0;
12009  for (i = 0; i < sc->tts_count; i++) {
12010  int next = time_sample + sc->tts_data[i].count;
12011  if (next > sc->current_sample) {
12012  sc->tts_index = i;
12013  sc->tts_sample = sc->current_sample - time_sample;
12014  break;
12015  }
12016  time_sample = next;
12017  }
12018  }
12019 
12020  /* adjust stsd index */
12021  if (sc->chunk_count) {
12022  time_sample = 0;
12023  for (i = 0; i < sc->stsc_count; i++) {
12024  int64_t next = time_sample + mov_get_stsc_samples(sc, i);
12025  if (next > sc->current_sample) {
12026  sc->stsc_index = i;
12027  sc->stsc_sample = sc->current_sample - time_sample;
12028  break;
12029  }
12030  av_assert0(next == (int)next);
12031  time_sample = next;
12032  }
12033  }
12034 
12035  return sample;
12036 }
12037 
12039 {
12040  FFStream *const sti = ffstream(st);
12041  int64_t first_ts = sti->index_entries[0].timestamp;
12043  int64_t off;
12044 
12046  return 0;
12047 
12048  /* compute skip samples according to stream initial_padding, seek ts and first ts */
12049  off = av_rescale_q(ts - first_ts, st->time_base,
12050  (AVRational){1, st->codecpar->sample_rate});
12051  return FFMAX(st->codecpar->initial_padding - off, 0);
12052 }
12053 
12054 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
12055 {
12056  MOVContext *mc = s->priv_data;
12057  AVStream *st;
12058  FFStream *sti;
12059  int sample;
12060  int i;
12061 
12062  if (stream_index >= s->nb_streams)
12063  return AVERROR_INVALIDDATA;
12064 
12065  st = s->streams[stream_index];
12066  sti = ffstream(st);
12067  sample = mov_seek_stream(s, st, sample_time, flags);
12068  if (sample < 0)
12069  return sample;
12070 
12071  if (mc->seek_individually) {
12072  /* adjust seek timestamp to found sample timestamp */
12073  int64_t seek_timestamp = sti->index_entries[sample].timestamp;
12075 
12076  for (i = 0; i < s->nb_streams; i++) {
12077  AVStream *const st = s->streams[i];
12078  FFStream *const sti = ffstream(st);
12079  int64_t timestamp;
12080 
12081  if (stream_index == i)
12082  continue;
12083 
12084  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
12085  sample = mov_seek_stream(s, st, timestamp, flags);
12086  if (sample >= 0)
12088  }
12089  } else {
12090  for (i = 0; i < s->nb_streams; i++) {
12091  MOVStreamContext *sc;
12092  st = s->streams[i];
12093  sc = st->priv_data;
12094  mov_current_sample_set(sc, 0);
12095  }
12096  while (1) {
12097  MOVStreamContext *sc;
12099  if (!entry)
12100  return AVERROR_INVALIDDATA;
12101  sc = st->priv_data;
12102  if (sc->ffindex == stream_index && sc->current_sample == sample)
12103  break;
12105  }
12106  }
12107  return 0;
12108 }
12109 
12110 #define OFFSET(x) offsetof(MOVContext, x)
12111 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
12112 static const AVOption mov_options[] = {
12113  {"use_absolute_path",
12114  "allow using absolute path when opening alias, this is a possible security issue",
12115  OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
12116  0, 1, FLAGS},
12117  {"seek_streams_individually",
12118  "Seek each stream individually to the closest point",
12119  OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
12120  0, 1, FLAGS},
12121  {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
12122  0, 1, FLAGS},
12123  {"advanced_editlist",
12124  "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
12125  OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
12126  0, 1, FLAGS},
12127  {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
12128  0, 1, FLAGS},
12129  {"use_mfra_for",
12130  "use mfra for fragment timestamps",
12131  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
12133  .unit = "use_mfra_for"},
12134  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
12135  FLAGS, .unit = "use_mfra_for" },
12136  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
12137  FLAGS, .unit = "use_mfra_for" },
12138  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
12139  FLAGS, .unit = "use_mfra_for" },
12140  {"use_tfdt", "use tfdt for fragment timestamps", OFFSET(use_tfdt), AV_OPT_TYPE_BOOL, {.i64 = 1},
12141  0, 1, FLAGS},
12142  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
12143  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
12144  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
12145  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
12146  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
12148  { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
12150  { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
12152  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
12153  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
12154  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
12155  .flags = AV_OPT_FLAG_DECODING_PARAM },
12156  { "decryption_key", "The default media decryption key (hex)", OFFSET(decryption_default_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
12157  { "decryption_keys", "The media decryption keys by KID (hex)", OFFSET(decryption_keys), AV_OPT_TYPE_DICT, .flags = AV_OPT_FLAG_DECODING_PARAM },
12158  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
12159  {.i64 = 0}, 0, 1, FLAGS },
12160  { "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 },
12161  { "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 },
12162 
12163  { NULL },
12164 };
12165 
12166 static const AVClass mov_class = {
12167  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
12168  .item_name = av_default_item_name,
12169  .option = mov_options,
12170  .version = LIBAVUTIL_VERSION_INT,
12171 };
12172 
12174  .p.name = "mov,mp4,m4a,3gp,3g2,mj2",
12175  .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
12176  .p.priv_class = &mov_class,
12177  .p.extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4v,m4b,ism,ismv,isma,f4v,avif,heic,heif",
12179  .priv_data_size = sizeof(MOVContext),
12180  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
12181  .read_probe = mov_probe,
12186 };
HEIFItemRef::item_id
int item_id
Definition: isom.h:295
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:330
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:7587
mov_read_meta
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5679
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:575
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:331
AV_CODEC_ID_MACE6
@ AV_CODEC_ID_MACE6
Definition: codec_id.h:462
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:10648
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:9564
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:412
PUT_UTF8
#define PUT_UTF8(val, tmp, PUT_BYTE)
Definition: common.h:530
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:350
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:375
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:8715
can_seek_to_key_sample
static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
Definition: mov.c:11936
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:369
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:455
HEIFItem::name
char * name
Definition: isom.h:302
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:247
MOVStreamContext::height
int height
tkhd height
Definition: isom.h:234
MOVContext::moov_retry
int moov_retry
Definition: isom.h:359
MOV_TRUN_SAMPLE_FLAGS
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:420
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:347
mix
static int mix(int c0, int c1)
Definition: 4xm.c:717
MOVStreamContext::last_stsd_index
int last_stsd_index
Definition: isom.h:262
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
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:433
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
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:260
mov_class
static const AVClass mov_class
Definition: mov.c:12166
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:252
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:11006
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:313
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:5295
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:10381
HEIFItem::hflip
int hflip
Definition: isom.h:309
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:12112
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:250
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:9139
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:657
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:5138
cbcs_scheme_decrypt
static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8532
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:5697
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:269
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:62
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
FFStream::first_discard_sample
int64_t first_discard_sample
If not 0, the first audio sample that should be discarded from the stream.
Definition: internal.h:225
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:384
mode
Definition: swscale.c:71
MOVContext::found_moov
int found_moov
'moov' atom has been found
Definition: isom.h:330
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:5520
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:322
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:244
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
mov_read_iref_cdsc
static int mov_read_iref_cdsc(MOVContext *c, AVIOContext *pb, uint32_t type, int version, uint32_t size)
Definition: mov.c:9447
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:311
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:9133
AVPacket::data
uint8_t * data
Definition: packet.h:603
MOVContext::found_mdat
int found_mdat
'mdat' atom has been found
Definition: isom.h:333
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:341
AVOption
AVOption.
Definition: opt.h:428
MOVContext::trex_data
MOVTrackExt * trex_data
Definition: isom.h:342
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:357
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:391
mov_read_tkhd
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5705
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:468
HEIFItem::st
AVStream * st
Definition: isom.h:299
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:433
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:7795
mov_seek_stream
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:11963
mov_read_saio
static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7941
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:525
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:270
mov_change_extradata
static int mov_change_extradata(AVStream *st, AVPacket *pkt)
Definition: mov.c:11545
nb_streams
static unsigned int nb_streams
Definition: ffprobe.c:352
HEIFItemRef
Definition: isom.h:293
MP4TrackKindMapping::scheme_uri
const char * scheme_uri
Definition: isom.h:493
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:669
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:363
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:307
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:195
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:351
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:7306
FFIOContext
Definition: avio_internal.h:28
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:650
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:282
cenc_filter
static int cenc_filter(MOVContext *mov, AVStream *st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
Definition: mov.c:8645
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:434
mov_read_tfhd
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5810
AV_CODEC_ID_BIN_DATA
@ AV_CODEC_ID_BIN_DATA
Definition: codec_id.h:606
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:53
IAMFMixPresentation::cmix
const AVIAMFMixPresentation * cmix
Definition: iamf.h:108
MOVContext::decryption_default_key
uint8_t * decryption_default_key
Definition: isom.h:377
mov_read_wide
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6390
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:263
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:120
OFFSET
#define OFFSET(x)
Definition: mov.c:12110
HEIFGrid::tile_item_list
HEIFItem ** tile_item_list
Definition: isom.h:319
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:10518
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:256
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:829
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:285
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:271
AV_CODEC_ID_SPEEX
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:487
AV_FOURCC_MAX_STRING_SIZE
#define AV_FOURCC_MAX_STRING_SIZE
Definition: avutil.h:321
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:358
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:331
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:424
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:303
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:272
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:6766
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:5857
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:418
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:10532
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:325
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:453
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:336
get_key_from_kid
static int get_key_from_kid(uint8_t *out, int len, MOVContext *c, AVEncryptionInfo *sample)
Definition: mov.c:8299
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:261
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:324
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_clip64
#define av_clip64
Definition: common.h:103
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:6838
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:7857
MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:431
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:390
MOV_TRUN_DATA_OFFSET
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:416
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:556
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:191
AVChannelLayout::u
union AVChannelLayout::@527 u
Details about which channels are present in this layout.
IAMFContext::audio_elements
IAMFAudioElement ** audio_elements
Definition: iamf.h:131
MOVFragmentIndex::complete
int complete
Definition: isom.h:168
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:334
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:461
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:10249
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:264
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:10717
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:495
FF_MOV_FLAG_MFRA_PTS
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:466
MOV_MERGE_STTS
#define MOV_MERGE_STTS
Definition: mov.c:4753
MOVStreamContext::iamf_stream_offset
int iamf_stream_offset
Definition: isom.h:290
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:6700
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:5314
IAMFSubStream::audio_substream_id
unsigned int audio_substream_id
Definition: iamf.h:83
MOVContext::fc
AVFormatContext * fc
Definition: isom.h:327
MOV_TFHD_DEFAULT_BASE_IS_MOOF
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:414
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:128
AV_CODEC_ID_EVC
@ AV_CODEC_ID_EVC
Definition: codec_id.h:316
IAMFLayer::substream_count
unsigned int substream_count
Definition: iamf.h:78
MOV_TFHD_DEFAULT_DURATION
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:410
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:251
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:376
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:217
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:452
HEIFGrid::tile_idx_list
unsigned * tile_idx_list
Definition: isom.h:321
MOV_TRUN_FIRST_SAMPLE_FLAGS
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:417
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:274
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:8850
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:6251
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:1237
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:11472
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:253
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:8803
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:73
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:7460
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:77
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:73
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:10326
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:472
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:111
AV_CODEC_ID_AVUI
@ AV_CODEC_ID_AVUI
Definition: codec_id.h:252
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:6410
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:7685
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:9080
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:7565
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:349
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:275
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:488
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:389
read_image_grid
static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid, AVStreamGroupTileGrid *tile_grid)
Definition: mov.c:10553
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:9968
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:289
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:11569
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:565
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:6609
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:5167
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:442
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:8157
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:465
MOV_SAMPLE_DEPENDENCY_NO
#define MOV_SAMPLE_DEPENDENCY_NO
Definition: isom.h:440
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:245
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:471
mov_read_ilst
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5465
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:8620
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:483
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:408
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:1272
MOVStreamContext::open_key_samples_count
int open_key_samples_count
Definition: isom.h:253
HEIFItem
Definition: isom.h:298
ff_codec_movaudio_tags
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom_tags.c:306
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:51
AV_CODEC_ID_GSM
@ AV_CODEC_ID_GSM
as in Berlin toast format
Definition: codec_id.h:470
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:47
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:570
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:492
should_retry
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:11502
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:557
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:338
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:454
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:8990
mov_read_elst
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6463
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:11242
AV_CODEC_ID_QCELP
@ AV_CODEC_ID_QCELP
Definition: codec_id.h:476
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:8395
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
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:46
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:342
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:283
cens_scheme_decrypt
static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8461
MOVContext::handbrake_version
int handbrake_version
Definition: isom.h:345
mov_free_stream_context
static void mov_free_stream_context(AVFormatContext *s, AVStream *st)
Definition: mov.c:10261
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:88
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:7634
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
FFStream::last_discard_sample
int64_t last_discard_sample
The sample after last sample that is intended to be discarded after first_discard_sample.
Definition: internal.h:232
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:464
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
current
static struct @598 current
MOVStreamContext::sgpd_sync
uint8_t * sgpd_sync
Definition: isom.h:248
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:5943
MOVStreamContext::stereo3d_size
size_t stereo3d_size
Definition: isom.h:268
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:9603
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:54
MOV_TFHD_STSD_ID
#define MOV_TFHD_STSD_ID
Definition: isom.h:409
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:502
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:54
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:273
aes_ctr.h
ff_format_io_close
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: avformat.c:951
mov_parse_heif_items
static int mov_parse_heif_items(AVFormatContext *s)
Definition: mov.c:10927
HEIFItem::is_idat_relative
int is_idat_relative
Definition: isom.h:312
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:7056
fix_timescale
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:5239
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:11131
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:9846
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:11062
MOV_MP4_FPCM_TAG
#define MOV_MP4_FPCM_TAG
Definition: isom.h:484
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:613
mov_read_packet
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:11719
mov_read_infe
static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9233
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:608
MOVEncryptionIndex::auxiliary_offsets_count
size_t auxiliary_offsets_count
Definition: isom.h:142
HEIFItem::vflip
int vflip
Definition: isom.h:310
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:485
mov_read_clli
static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6737
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:289
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:8259
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:9714
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:57
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:8188
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:11638
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:12054
bprint.h
MOVContext::advanced_editlist
int advanced_editlist
Definition: isom.h:350
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:48
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:5878
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:306
FLAGS
#define FLAGS
Definition: mov.c:12111
MOVStreamContext::stereo3d
AVStereo3D * stereo3d
Definition: isom.h:267
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:8036
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:589
rb_size
static int rb_size(AVIOContext *pb, int64_t *value, int size)
Definition: mov.c:9106
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:137
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
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
s
uint8_t s
Definition: llvidencdsp.c:39
MOVContext::frag_index
MOVFragmentIndex frag_index
Definition: isom.h:363
mov_read_vpcc
static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6565
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:352
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:223
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
av_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:110
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:339
mov_read_mdcv
static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6656
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:120
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:5474
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:138
exif.h
MOVFragment::size
unsigned size
Definition: isom.h:115
MOV_TFHD_DEFAULT_SIZE
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:411
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:11204
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:9310
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:487
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:275
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:6537
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:276
tag
uint32_t tag
Definition: movenc.c:2073
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:323
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:304
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:7739
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:314
MOVContext::chapter_tracks
int * chapter_tracks
Definition: isom.h:346
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:620
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:8768
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:328
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:5904
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:5620
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:11509
MOVContext::use_mfra_for
int use_mfra_for
Definition: isom.h:360
AVEncryptionInfo
This describes encryption info for a packet.
Definition: encryption_info.h:43
MOVStreamContext::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:286
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:11900
AVStreamGroup::params
union AVStreamGroup::@459 params
Group type-specific parameters.
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:257
MOVStreamContext::cenc
struct MOVStreamContext::@490 cenc
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:7397
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:128
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
MOVContext::dv_fctx
AVFormatContext * dv_fctx
Definition: isom.h:339
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:458
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:148
MOVContext::aax_mode
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:365
mov_read_sv3d
static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6919
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:12038
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:249
MOVContext::fragment
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:341
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:284
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:10399
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:280
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:355
mov_read_rtmd_track
static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:10176
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:338
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:10801
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:317
mov_read_iref_dimg
static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version)
Definition: mov.c:9364
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:335
MOVContext::trak_index
int trak_index
Index of the current 'trak'.
Definition: isom.h:335
mov_read_timecode_track
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:10202
HEIFGrid::item
HEIFItem * item
Definition: isom.h:318
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:320
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:469
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:353
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:10417
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:8823
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:11077
mov_read_hfov
static int mov_read_hfov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7368
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:326
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:370
MOVContext::nb_heif_item
int nb_heif_item
Definition: isom.h:387
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:392
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:7129
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:511
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:419
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:308
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:10162
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:190
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
MP4TrackKindValueMapping::value
const char * value
Definition: isom.h:489
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:351
ff_mov_demuxer
const FFInputFormat ff_mov_demuxer
Definition: mov.c:12173
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:266
MOVContext::heif_grid
HEIFGrid * heif_grid
Definition: isom.h:388
MOVStreamContext::min_sample_duration
uint32_t min_sample_duration
Definition: isom.h:254
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:464
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:8604
MOVStreamContext::format
uint32_t format
Definition: isom.h:278
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStreamGroupTileGrid::offsets
struct AVStreamGroupTileGrid::@458 * offsets
An nb_tiles sized array of offsets in pixels from the topleft edge of the canvas, indicating where ea...
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:246
MOVContext::bitrates_count
int bitrates_count
Definition: isom.h:358
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:457
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:9536
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:9491
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:10472
AV_CODEC_ID_FLV1
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:71
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:8901
MOVContext::heif_item
HEIFItem ** heif_item
Definition: isom.h:386
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:421
HEIFItem::iref_list
HEIFItemRef * iref_list
Definition: isom.h:300
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:298
MOV_ISMV_TTML_TAG
#define MOV_ISMV_TTML_TAG
Definition: isom.h:482
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:343
mov_read_st3d
static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6791
mov_read_imir
static int mov_read_imir(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9584
is_open_key_sample
static int is_open_key_sample(const MOVStreamContext *sc, int sample)
Definition: mov.c:11919
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:10063
AV_CODEC_ID_DNXHD
@ AV_CODEC_ID_DNXHD
Definition: codec_id.h:149
channel
channel
Definition: ebur128.h:39
MOVStreamContext::default_encrypted_sample
AVEncryptionInfo * default_encrypted_sample
Definition: isom.h:285
duration
static int64_t duration
Definition: ffplay.c:329
MOVContext::cur_item_id
int cur_item_id
Definition: isom.h:385
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:354
MOVContext::found_iinf
int found_iinf
'iinf' atom has been found
Definition: isom.h:332
MOVContext::meta_keys_count
unsigned meta_keys_count
Definition: isom.h:337
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:8336
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:198
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:323
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:9125
MOVContext::ignore_chapters
int ignore_chapters
Definition: isom.h:352
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:492
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:301
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:305
mov_read_stsz
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3579