FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mov.c
Go to the documentation of this file.
1 /*
2  * MOV demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5  *
6  * first version by Francois Revol <revol@free.fr>
7  * seek function by Gael Chardon <gael.dev@4now.net>
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "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"
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/flac.h"
56 #include "libavcodec/hevc/hevc.h"
58 #include "libavcodec/mlp_parse.h"
59 #include "avformat.h"
60 #include "internal.h"
61 #include "avio_internal.h"
62 #include "demux.h"
63 #include "dvdclut.h"
64 #include "iamf_parse.h"
65 #include "iamf_reader.h"
66 #include "dovi_isom.h"
67 #include "riff.h"
68 #include "isom.h"
69 #include "libavcodec/get_bits.h"
70 #include "id3v1.h"
71 #include "mov_chan.h"
72 #include "replaygain.h"
73 
74 #if CONFIG_ZLIB
75 #include <zlib.h>
76 #endif
77 
78 #include "qtpalette.h"
79 
80 /* those functions parse an atom */
81 /* links atom IDs to parse functions */
82 typedef struct MOVParseTableEntry {
83  uint32_t type;
84  int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
86 
87 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
88 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
90 
92  unsigned len, const char *key)
93 {
94  char buf[16];
95 
96  short current, total = 0;
97  avio_rb16(pb); // unknown
98  current = avio_rb16(pb);
99  if (len >= 6)
100  total = avio_rb16(pb);
101  if (!total)
102  snprintf(buf, sizeof(buf), "%d", current);
103  else
104  snprintf(buf, sizeof(buf), "%d/%d", current, total);
105  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
106  av_dict_set(&c->fc->metadata, key, buf, 0);
107 
108  return 0;
109 }
110 
112  unsigned len, const char *key)
113 {
114  /* bypass padding bytes */
115  avio_r8(pb);
116  avio_r8(pb);
117  avio_r8(pb);
118 
119  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
120  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
121 
122  return 0;
123 }
124 
126  unsigned len, const char *key)
127 {
128  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
129  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
130 
131  return 0;
132 }
133 
135  unsigned len, const char *key)
136 {
137  short genre;
138 
139  avio_r8(pb); // unknown
140 
141  genre = avio_r8(pb);
142  if (genre < 1 || genre > ID3v1_GENRE_MAX)
143  return 0;
144  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
145  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
146 
147  return 0;
148 }
149 
150 static const uint32_t mac_to_unicode[128] = {
151  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
152  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
153  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
154  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
155  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
156  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
157  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
158  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
159  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
160  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
161  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
162  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
163  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
164  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
165  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
166  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
167 };
168 
170  char *dst, int dstlen)
171 {
172  char *p = dst;
173  char *end = dst+dstlen-1;
174  int i;
175 
176  for (i = 0; i < len; i++) {
177  uint8_t t, c = avio_r8(pb);
178 
179  if (p >= end)
180  continue;
181 
182  if (c < 0x80)
183  *p++ = c;
184  else if (p < end)
185  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
186  }
187  *p = 0;
188  return p - dst;
189 }
190 
191 /**
192  * Get the current item in the parsing process.
193  */
195 {
196  HEIFItem *item = NULL;
197 
198  for (int i = 0; i < c->nb_heif_item; i++) {
199  if (!c->heif_item[i] || c->heif_item[i]->item_id != c->cur_item_id)
200  continue;
201 
202  item = c->heif_item[i];
203  break;
204  }
205 
206  return item;
207 }
208 
209 /**
210  * Get the current stream in the parsing process. This can either be the
211  * latest stream added to the context, or the stream referenced by an item.
212  */
214 {
215  AVStream *st = NULL;
216  HEIFItem *item;
217 
218  if (c->fc->nb_streams < 1)
219  return NULL;
220 
221  if (c->cur_item_id == -1)
222  return c->fc->streams[c->fc->nb_streams-1];
223 
224  item = heif_cur_item(c);
225  if (item)
226  st = item->st;
227 
228  return st;
229 }
230 
231 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
232 {
233  AVStream *st;
234  MOVStreamContext *sc;
235  enum AVCodecID id;
236  int ret;
237 
238  switch (type) {
239  case 0xd: id = AV_CODEC_ID_MJPEG; break;
240  case 0xe: id = AV_CODEC_ID_PNG; break;
241  case 0x1b: id = AV_CODEC_ID_BMP; break;
242  default:
243  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
244  avio_skip(pb, len);
245  return 0;
246  }
247 
248  sc = av_mallocz(sizeof(*sc));
249  if (!sc)
250  return AVERROR(ENOMEM);
251  ret = ff_add_attached_pic(c->fc, NULL, pb, NULL, len);
252  if (ret < 0) {
253  av_free(sc);
254  return ret;
255  }
256  st = c->fc->streams[c->fc->nb_streams - 1];
257  st->priv_data = sc;
258  sc->id = st->id;
259  sc->refcount = 1;
260 
261  if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
262  if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
263  id = AV_CODEC_ID_PNG;
264  } else {
265  id = AV_CODEC_ID_MJPEG;
266  }
267  }
268  st->codecpar->codec_id = id;
269 
270  return 0;
271 }
272 
273 // 3GPP TS 26.244
274 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
275 {
276  char language[4] = { 0 };
277  char buf[200], place[100];
278  uint16_t langcode = 0;
279  double longitude, latitude, altitude;
280  const char *key = "location";
281 
282  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
283  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
284  return AVERROR_INVALIDDATA;
285  }
286 
287  avio_skip(pb, 4); // version+flags
288  langcode = avio_rb16(pb);
289  ff_mov_lang_to_iso639(langcode, language);
290  len -= 6;
291 
292  len -= avio_get_str(pb, len, place, sizeof(place));
293  if (len < 1) {
294  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
295  return AVERROR_INVALIDDATA;
296  }
297  avio_skip(pb, 1); // role
298  len -= 1;
299 
300  if (len < 12) {
301  av_log(c->fc, AV_LOG_ERROR,
302  "loci too short (%u bytes left, need at least %d)\n", len, 12);
303  return AVERROR_INVALIDDATA;
304  }
305  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
306  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
307  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
308 
309  // Try to output in the same format as the ?xyz field
310  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
311  if (altitude)
312  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
313  av_strlcatf(buf, sizeof(buf), "/%s", place);
314 
315  if (*language && strcmp(language, "und")) {
316  char key2[16];
317  snprintf(key2, sizeof(key2), "%s-%s", key, language);
318  av_dict_set(&c->fc->metadata, key2, buf, 0);
319  }
320  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
321  return av_dict_set(&c->fc->metadata, key, buf, 0);
322 }
323 
324 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
325 {
326  int i, n_hmmt;
327 
328  if (len < 2)
329  return 0;
330  if (c->ignore_chapters)
331  return 0;
332 
333  n_hmmt = avio_rb32(pb);
334  if (n_hmmt > len / 4)
335  return AVERROR_INVALIDDATA;
336  for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
337  int moment_time = avio_rb32(pb);
338  avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
339  }
340  if (avio_feof(pb))
341  return AVERROR_INVALIDDATA;
342  return 0;
343 }
344 
346 {
347  char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
348  char key2[32], language[4] = {0};
349  char *str = NULL;
350  const char *key = NULL;
351  uint16_t langcode = 0;
352  uint32_t data_type = 0, str_size_alloc;
353  uint64_t str_size;
354  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
355  int raw = 0;
356  int num = 0;
357  AVDictionary **metadata;
358 
359  if (c->trak_index >= 0 && c->trak_index < c->fc->nb_streams)
360  metadata = &c->fc->streams[c->trak_index]->metadata;
361  else
362  metadata = &c->fc->metadata;
363 
364  switch (atom.type) {
365  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
366  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
367  case MKTAG( 'X','M','P','_'):
368  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
369  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
370  case MKTAG( 'a','k','I','D'): key = "account_type";
372  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
373  case MKTAG( 'c','a','t','g'): key = "category"; break;
374  case MKTAG( 'c','p','i','l'): key = "compilation";
376  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
377  case MKTAG( 'd','e','s','c'): key = "description"; break;
378  case MKTAG( 'd','i','s','k'): key = "disc";
380  case MKTAG( 'e','g','i','d'): key = "episode_uid";
382  case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
383  case MKTAG( 'g','n','r','e'): key = "genre";
384  parse = mov_metadata_gnre; break;
385  case MKTAG( 'h','d','v','d'): key = "hd_video";
387  case MKTAG( 'H','M','M','T'):
388  return mov_metadata_hmmt(c, pb, atom.size);
389  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
390  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
391  case MKTAG( 'l','o','c','i'):
392  return mov_metadata_loci(c, pb, atom.size);
393  case MKTAG( 'm','a','n','u'): key = "make"; break;
394  case MKTAG( 'm','o','d','l'): key = "model"; break;
395  case MKTAG( 'p','c','s','t'): key = "podcast";
397  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
399  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
400  case MKTAG( 'r','t','n','g'): key = "rating";
402  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
403  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
404  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
405  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
406  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
407  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
408  case MKTAG( 's','t','i','k'): key = "media_type";
410  case MKTAG( 't','r','k','n'): key = "track";
412  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
413  case MKTAG( 't','v','e','s'): key = "episode_sort";
415  case MKTAG( 't','v','n','n'): key = "network"; break;
416  case MKTAG( 't','v','s','h'): key = "show"; break;
417  case MKTAG( 't','v','s','n'): key = "season_number";
419  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
420  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
421  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
422  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
423  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
424  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
425  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
426  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
427  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
428  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
429  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
430  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
431  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
432  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
433  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
434  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
435  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
436  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
437  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
438  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
439  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
440  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
441  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
442  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
443  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
444  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
445  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
446  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
447  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
448  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
449  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
450  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
451  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
452  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
453  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
454  }
455 retry:
456  if (c->itunes_metadata && atom.size > 8) {
457  int data_size = avio_rb32(pb);
458  int tag = avio_rl32(pb);
459  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size && data_size >= 16) {
460  data_type = avio_rb32(pb); // type
461  avio_rb32(pb); // unknown
462  str_size = data_size - 16;
463  atom.size -= 16;
464 
465  if (!key && c->found_hdlr_mdta && c->meta_keys) {
466  uint32_t index = av_bswap32(atom.type); // BE number has been read as LE
467  if (index < c->meta_keys_count && index > 0) {
468  key = c->meta_keys[index];
469  } else if (atom.type != MKTAG('c', 'o', 'v', 'r')) {
470  av_log(c->fc, AV_LOG_WARNING,
471  "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
472  index, c->meta_keys_count);
473  }
474  }
475  if (atom.type == MKTAG('c', 'o', 'v', 'r') ||
476  (key && !strcmp(key, "com.apple.quicktime.artwork"))) {
477  int ret = mov_read_covr(c, pb, data_type, str_size);
478  if (ret < 0) {
479  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
480  return ret;
481  }
482  atom.size -= str_size;
483  if (atom.size > 8)
484  goto retry;
485  return ret;
486  }
487  } else return 0;
488  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
489  str_size = avio_rb16(pb); // string length
490  if (str_size > atom.size) {
491  raw = 1;
492  avio_seek(pb, -2, SEEK_CUR);
493  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
494  goto retry;
495  }
496  langcode = avio_rb16(pb);
497  ff_mov_lang_to_iso639(langcode, language);
498  atom.size -= 4;
499  } else
500  str_size = atom.size;
501 
502  if (c->export_all && !key) {
503  key = av_fourcc_make_string(tmp_key, atom.type);
504  }
505 
506  if (!key)
507  return 0;
508  if (atom.size < 0 || str_size >= INT_MAX/2)
509  return AVERROR_INVALIDDATA;
510 
511  // Allocates enough space if data_type is a int32 or float32 number, otherwise
512  // worst-case requirement for output string in case of utf8 coded input
513  num = (data_type >= 21 && data_type <= 23);
514  str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
515  str = av_mallocz(str_size_alloc);
516  if (!str)
517  return AVERROR(ENOMEM);
518 
519  if (parse)
520  parse(c, pb, str_size, key);
521  else {
522  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
523  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
524  } else if (data_type == 21) { // BE signed integer, variable size
525  int val = 0;
526  if (str_size == 1)
527  val = (int8_t)avio_r8(pb);
528  else if (str_size == 2)
529  val = (int16_t)avio_rb16(pb);
530  else if (str_size == 3)
531  val = ((int32_t)(avio_rb24(pb)<<8))>>8;
532  else if (str_size == 4)
533  val = (int32_t)avio_rb32(pb);
534  if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
535  av_log(c->fc, AV_LOG_ERROR,
536  "Failed to store the number (%d) in string.\n", val);
537  av_free(str);
538  return AVERROR_INVALIDDATA;
539  }
540  } else if (data_type == 22) { // BE unsigned integer, variable size
541  unsigned int val = 0;
542  if (str_size == 1)
543  val = avio_r8(pb);
544  else if (str_size == 2)
545  val = avio_rb16(pb);
546  else if (str_size == 3)
547  val = avio_rb24(pb);
548  else if (str_size == 4)
549  val = avio_rb32(pb);
550  if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
551  av_log(c->fc, AV_LOG_ERROR,
552  "Failed to store the number (%u) in string.\n", val);
553  av_free(str);
554  return AVERROR_INVALIDDATA;
555  }
556  } else if (data_type == 23 && str_size >= 4) { // BE float32
557  float val = av_int2float(avio_rb32(pb));
558  if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
559  av_log(c->fc, AV_LOG_ERROR,
560  "Failed to store the float32 number (%f) in string.\n", val);
561  av_free(str);
562  return AVERROR_INVALIDDATA;
563  }
564  } else if (data_type > 1 && data_type != 4) {
565  // data_type can be 0 if not set at all above. data_type 1 means
566  // UTF8 and 4 means "UTF8 sort". For any other type (UTF16 or e.g.
567  // a picture), don't return it blindly in a string that is supposed
568  // to be UTF8 text.
569  av_log(c->fc, AV_LOG_WARNING, "Skipping unhandled metadata %s of type %d\n", key, data_type);
570  av_free(str);
571  return 0;
572  } else {
573  int ret = ffio_read_size(pb, str, str_size);
574  if (ret < 0) {
575  av_free(str);
576  return ret;
577  }
578  str[str_size] = 0;
579  }
580  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
581  av_dict_set(metadata, key, str, 0);
582  if (*language && strcmp(language, "und")) {
583  snprintf(key2, sizeof(key2), "%s-%s", key, language);
584  av_dict_set(metadata, key2, str, 0);
585  }
586  if (!strcmp(key, "encoder")) {
587  int major, minor, micro;
588  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
589  c->handbrake_version = 1000000*major + 1000*minor + micro;
590  }
591  }
592  }
593 
594  av_freep(&str);
595  return 0;
596 }
597 
599 {
600  int64_t start;
601  int i, nb_chapters, str_len, version;
602  char str[256+1];
603  int ret;
604 
605  if (c->ignore_chapters)
606  return 0;
607 
608  if ((atom.size -= 5) < 0)
609  return 0;
610 
611  version = avio_r8(pb);
612  avio_rb24(pb);
613  if (version)
614  avio_rb32(pb); // ???
615  nb_chapters = avio_r8(pb);
616 
617  for (i = 0; i < nb_chapters; i++) {
618  if (atom.size < 9)
619  return 0;
620 
621  start = avio_rb64(pb);
622  str_len = avio_r8(pb);
623 
624  if ((atom.size -= 9+str_len) < 0)
625  return 0;
626 
627  ret = ffio_read_size(pb, str, str_len);
628  if (ret < 0)
629  return ret;
630  str[str_len] = 0;
631  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
632  }
633  return 0;
634 }
635 
636 #define MIN_DATA_ENTRY_BOX_SIZE 12
638 {
639  AVStream *st;
640  MOVStreamContext *sc;
641  int entries, i, j;
642 
643  if (c->fc->nb_streams < 1)
644  return 0;
645  st = c->fc->streams[c->fc->nb_streams-1];
646  sc = st->priv_data;
647 
648  avio_rb32(pb); // version + flags
649  entries = avio_rb32(pb);
650  if (!entries ||
651  entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
652  entries >= UINT_MAX / sizeof(*sc->drefs))
653  return AVERROR_INVALIDDATA;
654 
655  for (i = 0; i < sc->drefs_count; i++) {
656  MOVDref *dref = &sc->drefs[i];
657  av_freep(&dref->path);
658  av_freep(&dref->dir);
659  }
660  av_free(sc->drefs);
661  sc->drefs_count = 0;
662  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
663  if (!sc->drefs)
664  return AVERROR(ENOMEM);
665  sc->drefs_count = entries;
666 
667  for (i = 0; i < entries; i++) {
668  MOVDref *dref = &sc->drefs[i];
669  uint32_t size = avio_rb32(pb);
670  int64_t next = avio_tell(pb);
671 
672  if (size < 12 || next < 0 || next > INT64_MAX - size)
673  return AVERROR_INVALIDDATA;
674 
675  next += size - 4;
676 
677  dref->type = avio_rl32(pb);
678  avio_rb32(pb); // version + flags
679 
680  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
681  /* macintosh alias record */
682  uint16_t volume_len, len;
683  int16_t type;
684  int ret;
685 
686  avio_skip(pb, 10);
687 
688  volume_len = avio_r8(pb);
689  volume_len = FFMIN(volume_len, 27);
690  ret = ffio_read_size(pb, dref->volume, 27);
691  if (ret < 0)
692  return ret;
693  dref->volume[volume_len] = 0;
694  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
695 
696  avio_skip(pb, 12);
697 
698  len = avio_r8(pb);
699  len = FFMIN(len, 63);
700  ret = ffio_read_size(pb, dref->filename, 63);
701  if (ret < 0)
702  return ret;
703  dref->filename[len] = 0;
704  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
705 
706  avio_skip(pb, 16);
707 
708  /* read next level up_from_alias/down_to_target */
709  dref->nlvl_from = avio_rb16(pb);
710  dref->nlvl_to = avio_rb16(pb);
711  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
712  dref->nlvl_from, dref->nlvl_to);
713 
714  avio_skip(pb, 16);
715 
716  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
717  if (avio_feof(pb))
718  return AVERROR_EOF;
719  type = avio_rb16(pb);
720  len = avio_rb16(pb);
721  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
722  if (len&1)
723  len += 1;
724  if (type == 2) { // absolute path
725  av_free(dref->path);
726  dref->path = av_mallocz(len+1);
727  if (!dref->path)
728  return AVERROR(ENOMEM);
729 
730  ret = ffio_read_size(pb, dref->path, len);
731  if (ret < 0) {
732  av_freep(&dref->path);
733  return ret;
734  }
735  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
736  len -= volume_len;
737  memmove(dref->path, dref->path+volume_len, len);
738  dref->path[len] = 0;
739  }
740  // trim string of any ending zeros
741  for (j = len - 1; j >= 0; j--) {
742  if (dref->path[j] == 0)
743  len--;
744  else
745  break;
746  }
747  for (j = 0; j < len; j++)
748  if (dref->path[j] == ':' || dref->path[j] == 0)
749  dref->path[j] = '/';
750  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
751  } else if (type == 0) { // directory name
752  av_free(dref->dir);
753  dref->dir = av_malloc(len+1);
754  if (!dref->dir)
755  return AVERROR(ENOMEM);
756 
757  ret = ffio_read_size(pb, dref->dir, len);
758  if (ret < 0) {
759  av_freep(&dref->dir);
760  return ret;
761  }
762  dref->dir[len] = 0;
763  for (j = 0; j < len; j++)
764  if (dref->dir[j] == ':')
765  dref->dir[j] = '/';
766  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
767  } else
768  avio_skip(pb, len);
769  }
770  } else {
771  av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
772  dref->type, size);
773  entries--;
774  i--;
775  }
776  avio_seek(pb, next, SEEK_SET);
777  }
778  return 0;
779 }
780 
782 {
783  AVStream *st;
784  uint32_t type;
785  uint32_t ctype;
786  int64_t title_size;
787  char *title_str;
788  int ret;
789 
790  avio_r8(pb); /* version */
791  avio_rb24(pb); /* flags */
792 
793  /* component type */
794  ctype = avio_rl32(pb);
795  type = avio_rl32(pb); /* component subtype */
796 
797  av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
798  av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
799 
800  if (c->trak_index < 0) { // meta not inside a trak
801  if (type == MKTAG('m','d','t','a')) {
802  c->found_hdlr_mdta = 1;
803  }
804  return 0;
805  }
806 
807  st = c->fc->streams[c->fc->nb_streams-1];
808 
809  if (type == MKTAG('v','i','d','e'))
811  else if (type == MKTAG('s','o','u','n'))
813  else if (type == MKTAG('m','1','a',' '))
815  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
817 
818  avio_rb32(pb); /* component manufacture */
819  avio_rb32(pb); /* component flags */
820  avio_rb32(pb); /* component flags mask */
821 
822  title_size = atom.size - 24;
823  if (title_size > 0) {
824  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
825  return AVERROR_INVALIDDATA;
826  title_str = av_malloc(title_size + 1); /* Add null terminator */
827  if (!title_str)
828  return AVERROR(ENOMEM);
829 
830  ret = ffio_read_size(pb, title_str, title_size);
831  if (ret < 0) {
832  av_freep(&title_str);
833  return ret;
834  }
835  title_str[title_size] = 0;
836  if (title_str[0]) {
837  int off = (!c->isom && title_str[0] == title_size - 1);
838  // flag added so as to not set stream handler name if already set from mdia->hdlr
839  av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
840  }
841  av_freep(&title_str);
842  }
843 
844  return 0;
845 }
846 
848 {
849  return ff_mov_read_esds(c->fc, pb);
850 }
851 
853 {
854  AVStream *st;
855  AVPacketSideData *sd;
856  enum AVAudioServiceType *ast;
857  int ac3info, acmod, lfeon, bsmod;
858  uint64_t mask;
859 
860  if (c->fc->nb_streams < 1)
861  return 0;
862  st = c->fc->streams[c->fc->nb_streams-1];
863 
867  sizeof(*ast), 0);
868  if (!sd)
869  return AVERROR(ENOMEM);
870 
871  ast = (enum AVAudioServiceType*)sd->data;
872  ac3info = avio_rb24(pb);
873  bsmod = (ac3info >> 14) & 0x7;
874  acmod = (ac3info >> 11) & 0x7;
875  lfeon = (ac3info >> 10) & 0x1;
876 
878  if (lfeon)
882 
883  *ast = bsmod;
884  if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
886 
887  return 0;
888 }
889 
890 #if CONFIG_IAMFDEC
891 static int mov_read_iacb(MOVContext *c, AVIOContext *pb, MOVAtom atom)
892 {
893  AVStream *st;
894  MOVStreamContext *sc;
895  FFIOContext b;
896  AVIOContext *descriptor_pb;
897  AVDictionary *metadata;
898  IAMFContext *iamf;
900  unsigned descriptors_size;
901  int nb_frames, disposition;
902  int version, ret;
903 
904  if (atom.size < 5)
905  return AVERROR_INVALIDDATA;
906 
907  if (c->fc->nb_streams < 1)
908  return 0;
909 
910  version = avio_r8(pb);
911  if (version != 1) {
912  av_log(c->fc, AV_LOG_ERROR, "%s configurationVersion %d",
913  version < 1 ? "invalid" : "unsupported", version);
914  return AVERROR_INVALIDDATA;
915  }
916 
917  descriptors_size = ffio_read_leb(pb);
918  if (!descriptors_size || descriptors_size > INT_MAX)
919  return AVERROR_INVALIDDATA;
920 
921  st = c->fc->streams[c->fc->nb_streams - 1];
922  sc = st->priv_data;
923 
924  if (st->codecpar->extradata) {
925  av_log(c->fc, AV_LOG_WARNING, "ignoring iacb\n");
926  return 0;
927  }
928 
929  sc->iamf = av_mallocz(sizeof(*sc->iamf));
930  if (!sc->iamf)
931  return AVERROR(ENOMEM);
932  iamf = &sc->iamf->iamf;
933 
934  st->codecpar->extradata = av_malloc(descriptors_size);
935  if (!st->codecpar->extradata)
936  return AVERROR(ENOMEM);
937  st->codecpar->extradata_size = descriptors_size;
938 
939  ret = avio_read(pb, st->codecpar->extradata, descriptors_size);
940  if (ret != descriptors_size)
941  return ret < 0 ? ret : AVERROR_INVALIDDATA;
942 
943  ffio_init_read_context(&b, st->codecpar->extradata, descriptors_size);
944  descriptor_pb = &b.pub;
945 
946  ret = ff_iamfdec_read_descriptors(iamf, descriptor_pb, descriptors_size, c->fc);
947  if (ret < 0)
948  return ret;
949 
950  metadata = st->metadata;
951  st->metadata = NULL;
952  start_time = st->start_time;
953  nb_frames = st->nb_frames;
954  duration = st->duration;
955  disposition = st->disposition;
956 
957  for (int i = 0; i < iamf->nb_audio_elements; i++) {
958  IAMFAudioElement *audio_element = iamf->audio_elements[i];
959  const AVIAMFAudioElement *element;
960  AVStreamGroup *stg =
962 
963  if (!stg) {
964  ret = AVERROR(ENOMEM);
965  goto fail;
966  }
967 
969  stg->id = audio_element->audio_element_id;
970  /* Transfer ownership */
971  element = stg->params.iamf_audio_element = audio_element->element;
972  audio_element->element = NULL;
973 
974  for (int j = 0; j < audio_element->nb_substreams; j++) {
975  IAMFSubStream *substream = &audio_element->substreams[j];
976  AVStream *stream;
977 
978  if (!i && !j) {
979  if (audio_element->layers[0].substream_count != 1)
980  disposition &= ~AV_DISPOSITION_DEFAULT;
981  stream = st;
982  } else
983  stream = avformat_new_stream(c->fc, NULL);
984  if (!stream) {
985  ret = AVERROR(ENOMEM);
986  goto fail;
987  }
988 
989  stream->start_time = start_time;
990  stream->nb_frames = nb_frames;
991  stream->duration = duration;
992  stream->disposition = disposition;
993  if (stream != st) {
994  stream->priv_data = sc;
995  sc->refcount++;
996  }
997 
1000  if (i || j) {
1002  if (audio_element->layers[0].substream_count == 1)
1003  stream->disposition &= ~AV_DISPOSITION_DEFAULT;
1004  }
1005 
1006  ret = avcodec_parameters_copy(stream->codecpar, substream->codecpar);
1007  if (ret < 0)
1008  goto fail;
1009 
1010  stream->id = substream->audio_substream_id;
1011 
1012  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
1013 
1014  ret = avformat_stream_group_add_stream(stg, stream);
1015  if (ret < 0)
1016  goto fail;
1017  }
1018 
1019  ret = av_dict_copy(&stg->metadata, metadata, 0);
1020  if (ret < 0)
1021  goto fail;
1022  }
1023 
1024  for (int i = 0; i < iamf->nb_mix_presentations; i++) {
1025  IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
1026  const AVIAMFMixPresentation *mix = mix_presentation->cmix;
1027  AVStreamGroup *stg =
1029 
1030  if (!stg) {
1031  ret = AVERROR(ENOMEM);
1032  goto fail;
1033  }
1034 
1036  stg->id = mix_presentation->mix_presentation_id;
1037  /* Transfer ownership */
1038  stg->params.iamf_mix_presentation = mix_presentation->mix;
1039  mix_presentation->mix = NULL;
1040 
1041  for (int j = 0; j < mix->nb_submixes; j++) {
1042  const AVIAMFSubmix *submix = mix->submixes[j];
1043 
1044  for (int k = 0; k < submix->nb_elements; k++) {
1045  const AVIAMFSubmixElement *submix_element = submix->elements[k];
1046  const AVStreamGroup *audio_element = NULL;
1047 
1048  for (int l = 0; l < c->fc->nb_stream_groups; l++)
1049  if (c->fc->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
1050  c->fc->stream_groups[l]->id == submix_element->audio_element_id) {
1051  audio_element = c->fc->stream_groups[l];
1052  break;
1053  }
1054  av_assert0(audio_element);
1055 
1056  for (int l = 0; l < audio_element->nb_streams; l++) {
1057  ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
1058  if (ret < 0 && ret != AVERROR(EEXIST))
1059  goto fail;
1060  }
1061  }
1062  }
1063 
1064  ret = av_dict_copy(&stg->metadata, metadata, 0);
1065  if (ret < 0)
1066  goto fail;
1067  }
1068 
1069  ret = 0;
1070 fail:
1071  av_dict_free(&metadata);
1072 
1073  return ret;
1074 }
1075 #endif
1076 
1078 {
1079  AVStream *st;
1080  AVPacketSideData *sd;
1081  enum AVAudioServiceType *ast;
1082  int eac3info, acmod, lfeon, bsmod;
1083  uint64_t mask;
1084 
1085  if (c->fc->nb_streams < 1)
1086  return 0;
1087  st = c->fc->streams[c->fc->nb_streams-1];
1088 
1092  sizeof(*ast), 0);
1093  if (!sd)
1094  return AVERROR(ENOMEM);
1095 
1096  ast = (enum AVAudioServiceType*)sd->data;
1097 
1098  /* No need to parse fields for additional independent substreams and its
1099  * associated dependent substreams since libavcodec's E-AC-3 decoder
1100  * does not support them yet. */
1101  avio_rb16(pb); /* data_rate and num_ind_sub */
1102  eac3info = avio_rb24(pb);
1103  bsmod = (eac3info >> 12) & 0x1f;
1104  acmod = (eac3info >> 9) & 0x7;
1105  lfeon = (eac3info >> 8) & 0x1;
1106 
1108  if (lfeon)
1112 
1113  *ast = bsmod;
1114  if (st->codecpar->ch_layout.nb_channels > 1 && bsmod == 0x7)
1116 
1117  return 0;
1118 }
1119 
1121 {
1122 #define DDTS_SIZE 20
1123  uint8_t buf[DDTS_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
1124  AVStream *st = NULL;
1125  uint32_t frame_duration_code = 0;
1126  uint32_t channel_layout_code = 0;
1127  GetBitContext gb;
1128  int ret;
1129 
1130  if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
1131  return ret;
1132 
1133  init_get_bits(&gb, buf, 8 * DDTS_SIZE);
1134 
1135  if (c->fc->nb_streams < 1) {
1136  return 0;
1137  }
1138  st = c->fc->streams[c->fc->nb_streams-1];
1139 
1140  st->codecpar->sample_rate = get_bits_long(&gb, 32);
1141  if (st->codecpar->sample_rate <= 0) {
1142  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
1143  return AVERROR_INVALIDDATA;
1144  }
1145  skip_bits_long(&gb, 32); /* max bitrate */
1146  st->codecpar->bit_rate = get_bits_long(&gb, 32);
1147  st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
1148  frame_duration_code = get_bits(&gb, 2);
1149  skip_bits(&gb, 30); /* various fields */
1150  channel_layout_code = get_bits(&gb, 16);
1151 
1152  st->codecpar->frame_size =
1153  (frame_duration_code == 0) ? 512 :
1154  (frame_duration_code == 1) ? 1024 :
1155  (frame_duration_code == 2) ? 2048 :
1156  (frame_duration_code == 3) ? 4096 : 0;
1157 
1158  if (channel_layout_code > 0xff) {
1159  av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
1160  }
1163  ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
1164  ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
1165  ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
1166  ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
1167  ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
1168  ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0));
1169 
1170  return 0;
1171 }
1172 
1174 {
1175  AVStream *st;
1176 
1177  if (c->fc->nb_streams < 1)
1178  return 0;
1179  st = c->fc->streams[c->fc->nb_streams-1];
1180 
1181  if (atom.size < 16)
1182  return 0;
1183 
1184  /* skip version and flags */
1185  avio_skip(pb, 4);
1186 
1187  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
1188 
1189  return 0;
1190 }
1191 
1193 {
1194  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
1195  int version, flags;
1196  int ret;
1197  AVStream *st;
1198 
1199  if (c->fc->nb_streams < 1)
1200  return 0;
1201  st = c->fc->streams[c->fc->nb_streams-1];
1202 
1203  version = avio_r8(pb);
1204  flags = avio_rb24(pb);
1205  if (version != 0 || flags != 0) {
1206  av_log(c->fc, AV_LOG_ERROR,
1207  "Unsupported 'chnl' box with version %d, flags: %#x",
1208  version, flags);
1209  return AVERROR_INVALIDDATA;
1210  }
1211 
1212  ret = ff_mov_read_chnl(c->fc, pb, st);
1213  if (ret < 0)
1214  return ret;
1215 
1216  if (avio_tell(pb) != end) {
1217  av_log(c->fc, AV_LOG_WARNING, "skip %" PRId64 " bytes of unknown data inside chnl\n",
1218  end - avio_tell(pb));
1219  avio_seek(pb, end, SEEK_SET);
1220  }
1221  return ret;
1222 }
1223 
1225 {
1226  AVStream *st;
1227  int ret;
1228 
1229  if (c->fc->nb_streams < 1)
1230  return 0;
1231  st = c->fc->streams[c->fc->nb_streams-1];
1232 
1233  if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
1234  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
1235 
1236  return ret;
1237 }
1238 
1240 {
1241  AVStream *st;
1242  HEIFItem *item;
1243  AVPacketSideData *sd;
1244  int width, height, err = 0;
1245  AVRational aperture_width, aperture_height, horiz_off, vert_off;
1246  AVRational pc_x, pc_y;
1247  uint64_t top, bottom, left, right;
1248 
1249  item = heif_cur_item(c);
1250  st = get_curr_st(c);
1251  if (!st)
1252  return 0;
1253 
1254  width = st->codecpar->width;
1255  height = st->codecpar->height;
1256  if ((!width || !height) && item) {
1257  width = item->width;
1258  height = item->height;
1259  }
1260  if (!width || !height) {
1261  err = AVERROR_INVALIDDATA;
1262  goto fail;
1263  }
1264 
1265  aperture_width.num = avio_rb32(pb);
1266  aperture_width.den = avio_rb32(pb);
1267  aperture_height.num = avio_rb32(pb);
1268  aperture_height.den = avio_rb32(pb);
1269 
1270  horiz_off.num = avio_rb32(pb);
1271  horiz_off.den = avio_rb32(pb);
1272  vert_off.num = avio_rb32(pb);
1273  vert_off.den = avio_rb32(pb);
1274 
1275  if (aperture_width.num < 0 || aperture_width.den < 0 ||
1276  aperture_height.num < 0 || aperture_height.den < 0 ||
1277  horiz_off.den < 0 || vert_off.den < 0) {
1278  err = AVERROR_INVALIDDATA;
1279  goto fail;
1280  }
1281  av_log(c->fc, AV_LOG_TRACE, "clap: apertureWidth %d/%d, apertureHeight %d/%d "
1282  "horizOff %d/%d vertOff %d/%d\n",
1283  aperture_width.num, aperture_width.den, aperture_height.num, aperture_height.den,
1284  horiz_off.num, horiz_off.den, vert_off.num, vert_off.den);
1285 
1286  pc_x = av_mul_q((AVRational) { width - 1, 1 }, (AVRational) { 1, 2 });
1287  pc_x = av_add_q(pc_x, horiz_off);
1288  pc_y = av_mul_q((AVRational) { height - 1, 1 }, (AVRational) { 1, 2 });
1289  pc_y = av_add_q(pc_y, vert_off);
1290 
1291  aperture_width = av_sub_q(aperture_width, (AVRational) { 1, 1 });
1292  aperture_width = av_mul_q(aperture_width, (AVRational) { 1, 2 });
1293  aperture_height = av_sub_q(aperture_height, (AVRational) { 1, 1 });
1294  aperture_height = av_mul_q(aperture_height, (AVRational) { 1, 2 });
1295 
1296  left = av_q2d(av_sub_q(pc_x, aperture_width));
1297  right = av_q2d(av_add_q(pc_x, aperture_width));
1298  top = av_q2d(av_sub_q(pc_y, aperture_height));
1299  bottom = av_q2d(av_add_q(pc_y, aperture_height));
1300 
1301  if (bottom > (height - 1) ||
1302  right > (width - 1)) {
1303  err = AVERROR_INVALIDDATA;
1304  goto fail;
1305  }
1306 
1307  bottom = height - 1 - bottom;
1308  right = width - 1 - right;
1309 
1310  if (!(left | right | top | bottom))
1311  return 0;
1312 
1313  if ((left + right) >= width ||
1314  (top + bottom) >= height) {
1315  err = AVERROR_INVALIDDATA;
1316  goto fail;
1317  }
1318 
1322  sizeof(uint32_t) * 4, 0);
1323  if (!sd)
1324  return AVERROR(ENOMEM);
1325 
1326  AV_WL32A(sd->data, top);
1327  AV_WL32A(sd->data + 4, bottom);
1328  AV_WL32A(sd->data + 8, left);
1329  AV_WL32A(sd->data + 12, right);
1330 
1331 fail:
1332  if (err < 0) {
1333  int explode = !!(c->fc->error_recognition & AV_EF_EXPLODE);
1334  av_log(c->fc, explode ? AV_LOG_ERROR : AV_LOG_WARNING, "Invalid clap box\n");
1335  if (!explode)
1336  err = 0;
1337  }
1338 
1339  return err;
1340 }
1341 
1342 /* This atom overrides any previously set aspect ratio */
1344 {
1345  const int num = avio_rb32(pb);
1346  const int den = avio_rb32(pb);
1347  AVStream *st;
1348  MOVStreamContext *sc;
1349 
1350  if (c->fc->nb_streams < 1)
1351  return 0;
1352  st = c->fc->streams[c->fc->nb_streams-1];
1353  sc = st->priv_data;
1354 
1355  av_log(c->fc, AV_LOG_TRACE, "pasp: hSpacing %d, vSpacing %d\n", num, den);
1356 
1357  if (den != 0) {
1358  sc->h_spacing = num;
1359  sc->v_spacing = den;
1360  }
1361  return 0;
1362 }
1363 
1364 /* this atom contains actual media data */
1366 {
1367  if (atom.size == 0) /* wrong one (MP4) */
1368  return 0;
1369  c->found_mdat=1;
1370  return 0; /* now go for moov */
1371 }
1372 
1373 #define DRM_BLOB_SIZE 56
1374 
1376 {
1377  uint8_t intermediate_key[20];
1378  uint8_t intermediate_iv[20];
1379  uint8_t input[64];
1380  uint8_t output[64];
1381  uint8_t file_checksum[20];
1382  uint8_t calculated_checksum[20];
1383  char checksum_string[2 * sizeof(file_checksum) + 1];
1384  struct AVSHA *sha;
1385  int i;
1386  int ret = 0;
1387  uint8_t *activation_bytes = c->activation_bytes;
1388  uint8_t *fixed_key = c->audible_fixed_key;
1389 
1390  c->aax_mode = 1;
1391 
1392  sha = av_sha_alloc();
1393  if (!sha)
1394  return AVERROR(ENOMEM);
1395  av_free(c->aes_decrypt);
1396  c->aes_decrypt = av_aes_alloc();
1397  if (!c->aes_decrypt) {
1398  ret = AVERROR(ENOMEM);
1399  goto fail;
1400  }
1401 
1402  /* drm blob processing */
1403  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1405  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1406  avio_read(pb, file_checksum, 20);
1407 
1408  // required by external tools
1409  ff_data_to_hex(checksum_string, file_checksum, sizeof(file_checksum), 1);
1410  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == %s\n", checksum_string);
1411 
1412  /* verify activation data */
1413  if (!activation_bytes) {
1414  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1415  ret = 0; /* allow ffprobe to continue working on .aax files */
1416  goto fail;
1417  }
1418  if (c->activation_bytes_size != 4) {
1419  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1420  ret = AVERROR(EINVAL);
1421  goto fail;
1422  }
1423 
1424  /* verify fixed key */
1425  if (c->audible_fixed_key_size != 16) {
1426  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1427  ret = AVERROR(EINVAL);
1428  goto fail;
1429  }
1430 
1431  /* AAX (and AAX+) key derivation */
1432  av_sha_init(sha, 160);
1433  av_sha_update(sha, fixed_key, 16);
1434  av_sha_update(sha, activation_bytes, 4);
1435  av_sha_final(sha, intermediate_key);
1436  av_sha_init(sha, 160);
1437  av_sha_update(sha, fixed_key, 16);
1438  av_sha_update(sha, intermediate_key, 20);
1439  av_sha_update(sha, activation_bytes, 4);
1440  av_sha_final(sha, intermediate_iv);
1441  av_sha_init(sha, 160);
1442  av_sha_update(sha, intermediate_key, 16);
1443  av_sha_update(sha, intermediate_iv, 16);
1444  av_sha_final(sha, calculated_checksum);
1445  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1446  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1448  goto fail;
1449  }
1450  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1451  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1452  for (i = 0; i < 4; i++) {
1453  // file data (in output) is stored in big-endian mode
1454  if (activation_bytes[i] != output[3 - i]) { // critical error
1455  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1457  goto fail;
1458  }
1459  }
1460  memcpy(c->file_key, output + 8, 16);
1461  memcpy(input, output + 26, 16);
1462  av_sha_init(sha, 160);
1463  av_sha_update(sha, input, 16);
1464  av_sha_update(sha, c->file_key, 16);
1465  av_sha_update(sha, fixed_key, 16);
1466  av_sha_final(sha, c->file_iv);
1467 
1468 fail:
1469  av_free(sha);
1470 
1471  return ret;
1472 }
1473 
1475 {
1476  if (c->audible_key_size != 16) {
1477  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
1478  return AVERROR(EINVAL);
1479  }
1480 
1481  if (c->audible_iv_size != 16) {
1482  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
1483  return AVERROR(EINVAL);
1484  }
1485 
1486  c->aes_decrypt = av_aes_alloc();
1487  if (!c->aes_decrypt) {
1488  return AVERROR(ENOMEM);
1489  }
1490 
1491  memcpy(c->file_key, c->audible_key, 16);
1492  memcpy(c->file_iv, c->audible_iv, 16);
1493  c->aax_mode = 1;
1494 
1495  return 0;
1496 }
1497 
1498 // Audible AAX (and AAX+) bytestream decryption
1499 static int aax_filter(uint8_t *input, int size, MOVContext *c)
1500 {
1501  int blocks = 0;
1502  unsigned char iv[16];
1503 
1504  memcpy(iv, c->file_iv, 16); // iv is overwritten
1505  blocks = size >> 4; // trailing bytes are not encrypted!
1506  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1507  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1508 
1509  return 0;
1510 }
1511 
1512 /* read major brand, minor version and compatible brands and store them as metadata */
1514 {
1515  uint32_t minor_ver;
1516  int comp_brand_size;
1517  char* comp_brands_str;
1518  uint8_t type[5] = {0};
1519  int ret = ffio_read_size(pb, type, 4);
1520  if (ret < 0)
1521  return ret;
1522  if (c->fc->nb_streams) {
1523  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT)
1524  return AVERROR_INVALIDDATA;
1525  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate FTYP\n");
1526  return 0;
1527  }
1528 
1529  if (strcmp(type, "qt "))
1530  c->isom = 1;
1531  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1532  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1533  minor_ver = avio_rb32(pb); /* minor version */
1534  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1535 
1536  comp_brand_size = atom.size - 8;
1537  if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
1538  return AVERROR_INVALIDDATA;
1539  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1540  if (!comp_brands_str)
1541  return AVERROR(ENOMEM);
1542 
1543  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1544  if (ret < 0) {
1545  av_freep(&comp_brands_str);
1546  return ret;
1547  }
1548  comp_brands_str[comp_brand_size] = 0;
1549  av_dict_set(&c->fc->metadata, "compatible_brands",
1550  comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
1551 
1552  // Logic for handling Audible's .aaxc files
1553  if (!strcmp(type, "aaxc")) {
1554  mov_aaxc_crypto(c);
1555  }
1556 
1557  return 0;
1558 }
1559 
1560 /* this atom should contain all header atoms */
1562 {
1563  int ret;
1564 
1565  if (c->found_moov) {
1566  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1567  avio_skip(pb, atom.size);
1568  return 0;
1569  }
1570 
1571  if ((ret = mov_read_default(c, pb, atom)) < 0)
1572  return ret;
1573  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1574  /* so we don't parse the whole file if over a network */
1575  c->found_moov=1;
1576  return 0; /* now go for mdat */
1577 }
1578 
1580  MOVFragmentIndex *frag_index,
1581  int index,
1582  int id)
1583 {
1584  int i;
1585  MOVFragmentIndexItem * item;
1586 
1587  if (index < 0 || index >= frag_index->nb_items)
1588  return NULL;
1589  item = &frag_index->item[index];
1590  for (i = 0; i < item->nb_stream_info; i++)
1591  if (item->stream_info[i].id == id)
1592  return &item->stream_info[i];
1593 
1594  // This shouldn't happen
1595  return NULL;
1596 }
1597 
1598 static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
1599 {
1600  int i;
1601  MOVFragmentIndexItem * item;
1602 
1603  if (frag_index->current < 0 ||
1604  frag_index->current >= frag_index->nb_items)
1605  return;
1606 
1607  item = &frag_index->item[frag_index->current];
1608  for (i = 0; i < item->nb_stream_info; i++)
1609  if (item->stream_info[i].id == id) {
1610  item->current = i;
1611  return;
1612  }
1613 
1614  // id not found. This shouldn't happen.
1615  item->current = -1;
1616 }
1617 
1619  MOVFragmentIndex *frag_index)
1620 {
1621  MOVFragmentIndexItem *item;
1622  if (frag_index->current < 0 ||
1623  frag_index->current >= frag_index->nb_items)
1624  return NULL;
1625 
1626  item = &frag_index->item[frag_index->current];
1627  if (item->current >= 0 && item->current < item->nb_stream_info)
1628  return &item->stream_info[item->current];
1629 
1630  // This shouldn't happen
1631  return NULL;
1632 }
1633 
1635 {
1636  int a, b, m;
1637  int64_t moof_offset;
1638 
1639  // Optimize for appending new entries
1640  if (!frag_index->nb_items ||
1641  frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
1642  return frag_index->nb_items;
1643 
1644  a = -1;
1645  b = frag_index->nb_items;
1646 
1647  while (b - a > 1) {
1648  m = (a + b) >> 1;
1649  moof_offset = frag_index->item[m].moof_offset;
1650  if (moof_offset >= offset)
1651  b = m;
1652  if (moof_offset <= offset)
1653  a = m;
1654  }
1655  return b;
1656 }
1657 
1659 {
1660  av_assert0(frag_stream_info);
1661  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1662  return frag_stream_info->sidx_pts;
1663  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1664  return frag_stream_info->first_tfra_pts;
1665  return frag_stream_info->tfdt_dts;
1666 }
1667 
1669  MOVFragmentIndex *frag_index, int index)
1670 {
1671  MOVFragmentStreamInfo * frag_stream_info;
1672  MOVStreamContext *sc = dst_st->priv_data;
1673  int64_t timestamp;
1674  int i, j;
1675 
1676  // If the stream is referenced by any sidx, limit the search
1677  // to fragments that referenced this stream in the sidx
1678  if (sc->has_sidx) {
1679  frag_stream_info = get_frag_stream_info(frag_index, index, sc->id);
1680  if (!frag_stream_info)
1681  return AV_NOPTS_VALUE;
1682  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1683  return frag_stream_info->sidx_pts;
1684  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1685  return frag_stream_info->first_tfra_pts;
1686  return frag_stream_info->sidx_pts;
1687  }
1688 
1689  for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
1690  AVStream *frag_stream = NULL;
1691  frag_stream_info = &frag_index->item[index].stream_info[i];
1692  for (j = 0; j < s->nb_streams; j++) {
1693  MOVStreamContext *sc2 = s->streams[j]->priv_data;
1694  if (sc2->id == frag_stream_info->id)
1695  frag_stream = s->streams[j];
1696  }
1697  if (!frag_stream) {
1698  av_log(s, AV_LOG_WARNING, "No stream matching sidx ID found.\n");
1699  continue;
1700  }
1701  timestamp = get_stream_info_time(frag_stream_info);
1702  if (timestamp != AV_NOPTS_VALUE)
1703  return av_rescale_q(timestamp, frag_stream->time_base, dst_st->time_base);
1704  }
1705  return AV_NOPTS_VALUE;
1706 }
1707 
1709  AVStream *st, int64_t timestamp)
1710 {
1711  int a, b, m, m0;
1712  int64_t frag_time;
1713 
1714  a = -1;
1715  b = frag_index->nb_items;
1716 
1717  while (b - a > 1) {
1718  m0 = m = (a + b) >> 1;
1719 
1720  while (m < b &&
1721  (frag_time = get_frag_time(s, st, frag_index, m)) == AV_NOPTS_VALUE)
1722  m++;
1723 
1724  if (m < b && frag_time <= timestamp)
1725  a = m;
1726  else
1727  b = m0;
1728  }
1729 
1730  return a;
1731 }
1732 
1734 {
1735  int index, i;
1736  MOVFragmentIndexItem * item;
1737  MOVFragmentStreamInfo * frag_stream_info;
1738 
1739  // If moof_offset already exists in frag_index, return index to it
1740  index = search_frag_moof_offset(&c->frag_index, offset);
1741  if (index < c->frag_index.nb_items &&
1742  c->frag_index.item[index].moof_offset == offset)
1743  return index;
1744 
1745  // offset is not yet in frag index.
1746  // Insert new item at index (sorted by moof offset)
1747  item = av_fast_realloc(c->frag_index.item,
1748  &c->frag_index.allocated_size,
1749  (c->frag_index.nb_items + 1) *
1750  sizeof(*c->frag_index.item));
1751  if (!item)
1752  return -1;
1753  c->frag_index.item = item;
1754 
1755  frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
1756  sizeof(*item->stream_info));
1757  if (!frag_stream_info)
1758  return -1;
1759 
1760  for (i = 0; i < c->fc->nb_streams; i++) {
1761  // Avoid building frag index if streams lack track id.
1762  MOVStreamContext *sc = c->fc->streams[i]->priv_data;
1763  if (sc->id < 0) {
1764  av_free(frag_stream_info);
1765  return AVERROR_INVALIDDATA;
1766  }
1767 
1768  frag_stream_info[i].id = sc->id;
1769  frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
1770  frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
1771  frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
1772  frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
1773  frag_stream_info[i].index_base = -1;
1774  frag_stream_info[i].index_entry = -1;
1775  frag_stream_info[i].encryption_index = NULL;
1776  frag_stream_info[i].stsd_id = -1;
1777  }
1778 
1779  if (index < c->frag_index.nb_items)
1780  memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
1781  (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
1782 
1783  item = &c->frag_index.item[index];
1784  item->headers_read = 0;
1785  item->current = 0;
1786  item->nb_stream_info = c->fc->nb_streams;
1787  item->moof_offset = offset;
1788  item->stream_info = frag_stream_info;
1789  c->frag_index.nb_items++;
1790 
1791  return index;
1792 }
1793 
1794 static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
1795  int id, int entries)
1796 {
1797  int i;
1798  MOVFragmentStreamInfo * frag_stream_info;
1799 
1800  if (index < 0)
1801  return;
1802  for (i = index; i < frag_index->nb_items; i++) {
1803  frag_stream_info = get_frag_stream_info(frag_index, i, id);
1804  if (frag_stream_info && frag_stream_info->index_entry >= 0)
1805  frag_stream_info->index_entry += entries;
1806  }
1807 }
1808 
1810 {
1811  // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
1812  c->fragment.found_tfhd = 0;
1813 
1814  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1815  c->has_looked_for_mfra = 1;
1816  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1817  int ret;
1818  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1819  "for a mfra\n");
1820  if ((ret = mov_read_mfra(c, pb)) < 0) {
1821  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1822  "read the mfra (may be a live ismv)\n");
1823  }
1824  } else {
1825  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1826  "seekable, can not look for mfra\n");
1827  }
1828  }
1829  c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1830  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1831  c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
1832  return mov_read_default(c, pb, atom);
1833 }
1834 
1836 {
1837  int64_t time;
1838  if (version == 1) {
1839  time = avio_rb64(pb);
1840  avio_rb64(pb);
1841  if (time < 0) {
1842  av_log(c->fc, AV_LOG_DEBUG, "creation_time is negative\n");
1843  return;
1844  }
1845  } else {
1846  time = avio_rb32(pb);
1847  avio_rb32(pb); /* modification time */
1848  if (time > 0 && time < 2082844800) {
1849  av_log(c->fc, AV_LOG_WARNING, "Detected creation time before 1970, parsing as unix timestamp.\n");
1850  time += 2082844800;
1851  }
1852  }
1853  if (time) {
1854  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1855 
1856  if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1857  av_log(c->fc, AV_LOG_DEBUG, "creation_time is not representable\n");
1858  return;
1859  }
1860 
1861  avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1862  }
1863 }
1864 
1866 {
1867  AVStream *st;
1868  MOVStreamContext *sc;
1869  int version;
1870  char language[4] = {0};
1871  unsigned lang;
1872 
1873  if (c->fc->nb_streams < 1)
1874  return 0;
1875  st = c->fc->streams[c->fc->nb_streams-1];
1876  sc = st->priv_data;
1877 
1878  if (sc->time_scale) {
1879  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1880  return AVERROR_INVALIDDATA;
1881  }
1882 
1883  version = avio_r8(pb);
1884  if (version > 1) {
1885  avpriv_request_sample(c->fc, "Version %d", version);
1886  return AVERROR_PATCHWELCOME;
1887  }
1888  avio_rb24(pb); /* flags */
1890 
1891  sc->time_scale = avio_rb32(pb);
1892  if (sc->time_scale <= 0) {
1893  av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1894  sc->time_scale = 1;
1895  }
1896  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1897 
1898  if ((version == 1 && st->duration == UINT64_MAX) ||
1899  (version != 1 && st->duration == UINT32_MAX)) {
1900  st->duration = 0;
1901  }
1902 
1903  lang = avio_rb16(pb); /* language */
1904  if (ff_mov_lang_to_iso639(lang, language))
1905  av_dict_set(&st->metadata, "language", language, 0);
1906  avio_rb16(pb); /* quality */
1907 
1908  return 0;
1909 }
1910 
1912 {
1913  int i;
1914  int version = avio_r8(pb); /* version */
1915  avio_rb24(pb); /* flags */
1916 
1917  mov_metadata_creation_time(c, pb, &c->fc->metadata, version);
1918  c->time_scale = avio_rb32(pb); /* time scale */
1919  if (c->time_scale <= 0) {
1920  av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
1921  c->time_scale = 1;
1922  }
1923  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1924 
1925  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1926  avio_rb32(pb); /* preferred scale */
1927 
1928  avio_rb16(pb); /* preferred volume */
1929 
1930  avio_skip(pb, 10); /* reserved */
1931 
1932  /* movie display matrix, store it in main context and use it later on */
1933  for (i = 0; i < 3; i++) {
1934  c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1935  c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1936  c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
1937  }
1938 
1939  avio_rb32(pb); /* preview time */
1940  avio_rb32(pb); /* preview duration */
1941  avio_rb32(pb); /* poster time */
1942  avio_rb32(pb); /* selection time */
1943  avio_rb32(pb); /* selection duration */
1944  avio_rb32(pb); /* current time */
1945  avio_rb32(pb); /* next track ID */
1946 
1947  return 0;
1948 }
1949 
1951 {
1952  AVStream *st;
1953 
1954  if (fc->nb_streams < 1)
1955  return;
1956  st = fc->streams[fc->nb_streams-1];
1957 
1958  switch (st->codecpar->codec_id) {
1959  case AV_CODEC_ID_PCM_S16BE:
1961  break;
1962  case AV_CODEC_ID_PCM_S24BE:
1964  break;
1965  case AV_CODEC_ID_PCM_S32BE:
1967  break;
1968  case AV_CODEC_ID_PCM_F32BE:
1970  break;
1971  case AV_CODEC_ID_PCM_F64BE:
1973  break;
1974  default:
1975  break;
1976  }
1977 }
1978 
1980 {
1981  int little_endian = avio_rb16(pb) & 0xFF;
1982  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1983  if (little_endian == 1)
1985  return 0;
1986 }
1987 
1989 {
1990  int format_flags;
1991  int version, flags;
1992  int pcm_sample_size;
1993  AVFormatContext *fc = c->fc;
1994  AVStream *st;
1995  MOVStreamContext *sc;
1996 
1997  if (atom.size < 6) {
1998  av_log(c->fc, AV_LOG_ERROR, "Empty pcmC box\n");
1999  return AVERROR_INVALIDDATA;
2000  }
2001 
2002  version = avio_r8(pb);
2003  flags = avio_rb24(pb);
2004 
2005  if (version != 0 || flags != 0) {
2006  av_log(c->fc, AV_LOG_ERROR,
2007  "Unsupported 'pcmC' box with version %d, flags: %x",
2008  version, flags);
2009  return AVERROR_INVALIDDATA;
2010  }
2011 
2012  format_flags = avio_r8(pb);
2013  pcm_sample_size = avio_r8(pb);
2014 
2015  if (fc->nb_streams < 1)
2016  return AVERROR_INVALIDDATA;
2017 
2018  st = fc->streams[fc->nb_streams - 1];
2019  sc = st->priv_data;
2020 
2021  if (sc->format == MOV_MP4_FPCM_TAG) {
2022  switch (pcm_sample_size) {
2023  case 32:
2025  break;
2026  case 64:
2028  break;
2029  default:
2030  av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
2031  pcm_sample_size,
2032  av_fourcc2str(sc->format));
2033  return AVERROR_INVALIDDATA;
2034  }
2035  } else if (sc->format == MOV_MP4_IPCM_TAG) {
2036  switch (pcm_sample_size) {
2037  case 16:
2039  break;
2040  case 24:
2042  break;
2043  case 32:
2045  break;
2046  default:
2047  av_log(fc, AV_LOG_ERROR, "invalid pcm_sample_size %d for %s\n",
2048  pcm_sample_size,
2049  av_fourcc2str(sc->format));
2050  return AVERROR_INVALIDDATA;
2051  }
2052  } else {
2053  av_log(fc, AV_LOG_ERROR, "'pcmC' with invalid sample entry '%s'\n",
2054  av_fourcc2str(sc->format));
2055  return AVERROR_INVALIDDATA;
2056  }
2057 
2058  if (format_flags & 1) // indicates little-endian format. If not present, big-endian format is used
2061 
2062  return 0;
2063 }
2064 
2066 {
2067  AVStream *st;
2068  HEIFItem *item = NULL;
2069  char color_parameter_type[5] = { 0 };
2070  uint16_t color_primaries, color_trc, color_matrix;
2071  int ret;
2072 
2073  st = get_curr_st(c);
2074  if (!st) {
2075  item = heif_cur_item(c);
2076  if (!item)
2077  return 0;
2078  }
2079 
2080  ret = ffio_read_size(pb, color_parameter_type, 4);
2081  if (ret < 0)
2082  return ret;
2083  if (strncmp(color_parameter_type, "nclx", 4) &&
2084  strncmp(color_parameter_type, "nclc", 4) &&
2085  strncmp(color_parameter_type, "prof", 4)) {
2086  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
2087  color_parameter_type);
2088  return 0;
2089  }
2090 
2091  if (!strncmp(color_parameter_type, "prof", 4)) {
2092  AVPacketSideData *sd;
2093  uint8_t *icc_profile;
2094  if (st) {
2098  atom.size - 4, 0);
2099  if (!sd)
2100  return AVERROR(ENOMEM);
2101  icc_profile = sd->data;
2102  } else {
2103  av_freep(&item->icc_profile);
2104  icc_profile = item->icc_profile = av_malloc(atom.size - 4);
2105  if (!icc_profile) {
2106  item->icc_profile_size = 0;
2107  return AVERROR(ENOMEM);
2108  }
2109  item->icc_profile_size = atom.size - 4;
2110  }
2111  ret = ffio_read_size(pb, icc_profile, atom.size - 4);
2112  if (ret < 0)
2113  return ret;
2114  } else if (st) {
2115  color_primaries = avio_rb16(pb);
2116  color_trc = avio_rb16(pb);
2117  color_matrix = avio_rb16(pb);
2118 
2119  av_log(c->fc, AV_LOG_TRACE,
2120  "%s: pri %d trc %d matrix %d",
2121  color_parameter_type, color_primaries, color_trc, color_matrix);
2122 
2123  if (!strncmp(color_parameter_type, "nclx", 4)) {
2124  uint8_t color_range = avio_r8(pb) >> 7;
2125  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
2126  if (color_range)
2128  else
2130  }
2131 
2134  if (!av_color_transfer_name(color_trc))
2135  color_trc = AVCOL_TRC_UNSPECIFIED;
2136  if (!av_color_space_name(color_matrix))
2137  color_matrix = AVCOL_SPC_UNSPECIFIED;
2138 
2140  st->codecpar->color_trc = color_trc;
2141  st->codecpar->color_space = color_matrix;
2142  av_log(c->fc, AV_LOG_TRACE, "\n");
2143  }
2144  return 0;
2145 }
2146 
2148 {
2149  AVStream *st;
2150  unsigned mov_field_order;
2151  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
2152 
2153  if (c->fc->nb_streams < 1) // will happen with jp2 files
2154  return 0;
2155  st = c->fc->streams[c->fc->nb_streams-1];
2156  if (atom.size < 2)
2157  return AVERROR_INVALIDDATA;
2158  mov_field_order = avio_rb16(pb);
2159  if ((mov_field_order & 0xFF00) == 0x0100)
2160  decoded_field_order = AV_FIELD_PROGRESSIVE;
2161  else if ((mov_field_order & 0xFF00) == 0x0200) {
2162  switch (mov_field_order & 0xFF) {
2163  case 0x01: decoded_field_order = AV_FIELD_TT;
2164  break;
2165  case 0x06: decoded_field_order = AV_FIELD_BB;
2166  break;
2167  case 0x09: decoded_field_order = AV_FIELD_TB;
2168  break;
2169  case 0x0E: decoded_field_order = AV_FIELD_BT;
2170  break;
2171  }
2172  }
2173  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
2174  av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
2175  }
2176  st->codecpar->field_order = decoded_field_order;
2177 
2178  return 0;
2179 }
2180 
2182 {
2183  int err = 0;
2184  uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
2185  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
2186  return AVERROR_INVALIDDATA;
2187  if ((err = av_reallocp(&par->extradata, size)) < 0) {
2188  par->extradata_size = 0;
2189  return err;
2190  }
2192  return 0;
2193 }
2194 
2195 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
2197  AVCodecParameters *par, uint8_t *buf)
2198 {
2199  int64_t result = atom.size;
2200  int err;
2201 
2202  AV_WB32(buf , atom.size + 8);
2203  AV_WL32(buf + 4, atom.type);
2204  err = ffio_read_size(pb, buf + 8, atom.size);
2205  if (err < 0) {
2206  par->extradata_size -= atom.size;
2207  return err;
2208  } else if (err < atom.size) {
2209  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
2210  par->extradata_size -= atom.size - err;
2211  result = err;
2212  }
2213  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2214  return result;
2215 }
2216 
2217 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
2219  enum AVCodecID codec_id)
2220 {
2221  AVStream *st;
2222  uint64_t original_size;
2223  int err;
2224 
2225  if (c->fc->nb_streams < 1) // will happen with jp2 files
2226  return 0;
2227  st = c->fc->streams[c->fc->nb_streams-1];
2228 
2229  if (st->codecpar->codec_id != codec_id)
2230  return 0; /* unexpected codec_id - don't mess with extradata */
2231 
2232  original_size = st->codecpar->extradata_size;
2233  err = mov_realloc_extradata(st->codecpar, atom);
2234  if (err)
2235  return err;
2236 
2237  err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
2238  if (err < 0)
2239  return err;
2240  return 0; // Note: this is the original behavior to ignore truncation.
2241 }
2242 
2243 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
2245 {
2246  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
2247 }
2248 
2250 {
2251  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_CAVS);
2252 }
2253 
2255 {
2256  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
2257 }
2258 
2260 {
2261  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
2262 }
2263 
2265 {
2266  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
2267  if (!ret)
2268  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
2269  return ret;
2270 }
2271 
2273 {
2274  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
2275 
2276  if (!ret && c->fc->nb_streams >= 1) {
2277  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
2278  if (par->extradata_size >= 40) {
2279  par->height = AV_RB16(&par->extradata[36]);
2280  par->width = AV_RB16(&par->extradata[38]);
2281  }
2282  }
2283  return ret;
2284 }
2285 
2287 {
2288  if (c->fc->nb_streams >= 1) {
2289  AVStream *const st = c->fc->streams[c->fc->nb_streams - 1];
2290  FFStream *const sti = ffstream(st);
2291  AVCodecParameters *par = st->codecpar;
2292 
2293  if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
2294  par->codec_id == AV_CODEC_ID_H264 &&
2295  atom.size > 11) {
2296  int cid;
2297  avio_skip(pb, 10);
2298  cid = avio_rb16(pb);
2299  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
2300  if (cid == 0xd4d || cid == 0xd4e)
2301  par->width = 1440;
2302  return 0;
2303  } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
2304  par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
2305  par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
2306  atom.size >= 24) {
2307  int num, den;
2308  avio_skip(pb, 12);
2309  num = avio_rb32(pb);
2310  den = avio_rb32(pb);
2311  if (num <= 0 || den <= 0)
2312  return 0;
2313  switch (avio_rb32(pb)) {
2314  case 2:
2315  if (den >= INT_MAX / 2)
2316  return 0;
2317  den *= 2;
2318  case 1:
2319  sti->display_aspect_ratio = (AVRational){ num, den };
2320  default:
2321  return 0;
2322  }
2323  }
2324  }
2325 
2326  return mov_read_avid(c, pb, atom);
2327 }
2328 
2330 {
2331  int ret = 0;
2332  int length = 0;
2333  uint64_t original_size;
2334  if (c->fc->nb_streams >= 1) {
2335  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
2336  if (par->codec_id == AV_CODEC_ID_H264)
2337  return 0;
2338  if (atom.size == 16) {
2339  original_size = par->extradata_size;
2340  ret = mov_realloc_extradata(par, atom);
2341  if (!ret) {
2342  length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
2343  if (length == atom.size) {
2344  const uint8_t range_value = par->extradata[original_size + 19];
2345  switch (range_value) {
2346  case 1:
2348  break;
2349  case 2:
2351  break;
2352  default:
2353  av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
2354  break;
2355  }
2356  ff_dlog(c->fc, "color_range: %d\n", par->color_range);
2357  } else {
2358  /* For some reason the whole atom was not added to the extradata */
2359  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
2360  }
2361  } else {
2362  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
2363  }
2364  } else {
2365  av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
2366  }
2367  }
2368 
2369  return ret;
2370 }
2371 
2373 {
2374  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
2375 }
2376 
2378 {
2379  AVStream *st;
2380  int ret;
2381 
2382  if (c->fc->nb_streams < 1)
2383  return 0;
2384  st = c->fc->streams[c->fc->nb_streams-1];
2385 
2386  if ((uint64_t)atom.size > (1<<30))
2387  return AVERROR_INVALIDDATA;
2388 
2389  if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
2392  // pass all frma atom to codec, needed at least for QDMC and QDM2
2393  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
2394  if (ret < 0)
2395  return ret;
2396  } else if (atom.size > 8) { /* to read frma, esds atoms */
2397  if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
2398  uint64_t buffer;
2399  ret = ffio_ensure_seekback(pb, 8);
2400  if (ret < 0)
2401  return ret;
2402  buffer = avio_rb64(pb);
2403  atom.size -= 8;
2404  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
2405  && buffer >> 32 <= atom.size
2406  && buffer >> 32 >= 8) {
2407  avio_skip(pb, -8);
2408  atom.size += 8;
2409  } else if (!st->codecpar->extradata_size) {
2410 #define ALAC_EXTRADATA_SIZE 36
2412  if (!st->codecpar->extradata)
2413  return AVERROR(ENOMEM);
2416  AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
2417  AV_WB64(st->codecpar->extradata + 12, buffer);
2418  avio_read(pb, st->codecpar->extradata + 20, 16);
2419  avio_skip(pb, atom.size - 24);
2420  return 0;
2421  }
2422  }
2423  if ((ret = mov_read_default(c, pb, atom)) < 0)
2424  return ret;
2425  } else
2426  avio_skip(pb, atom.size);
2427  return 0;
2428 }
2429 
2430 /**
2431  * This function reads atom content and puts data in extradata without tag
2432  * nor size unlike mov_read_extradata.
2433  */
2435 {
2436  AVStream *st;
2437  int ret;
2438 
2439  st = get_curr_st(c);
2440  if (!st)
2441  return 0;
2442 
2443  if ((uint64_t)atom.size > (1<<30))
2444  return AVERROR_INVALIDDATA;
2445 
2446  if (atom.type == MKTAG('v','v','c','C')) {
2447  avio_skip(pb, 4);
2448  atom.size -= 4;
2449  }
2450 
2451  if (atom.size >= 10) {
2452  // Broken files created by legacy versions of libavformat will
2453  // wrap a whole fiel atom inside of a glbl atom.
2454  unsigned size = avio_rb32(pb);
2455  unsigned type = avio_rl32(pb);
2456  if (avio_feof(pb))
2457  return AVERROR_INVALIDDATA;
2458  avio_seek(pb, -8, SEEK_CUR);
2459  if (type == MKTAG('f','i','e','l') && size == atom.size)
2460  return mov_read_default(c, pb, atom);
2461  }
2462  if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
2463  av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
2464  return 0;
2465  }
2466  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
2467  if (ret < 0)
2468  return ret;
2469  if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
2470  /* HEVC-based Dolby Vision derived from hvc1.
2471  Happens to match with an identifier
2472  previously utilized for DV. Thus, if we have
2473  the hvcC extradata box available as specified,
2474  set codec to HEVC */
2476 
2477  return 0;
2478 }
2479 
2481 {
2482  AVStream *st;
2483  uint8_t profile_level;
2484  int ret;
2485 
2486  if (c->fc->nb_streams < 1)
2487  return 0;
2488  st = c->fc->streams[c->fc->nb_streams-1];
2489 
2490  if (atom.size >= (1<<28) || atom.size < 7)
2491  return AVERROR_INVALIDDATA;
2492 
2493  profile_level = avio_r8(pb);
2494  if ((profile_level & 0xf0) != 0xc0)
2495  return 0;
2496 
2497  avio_seek(pb, 6, SEEK_CUR);
2498  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
2499  if (ret < 0)
2500  return ret;
2501 
2502  return 0;
2503 }
2504 
2506 {
2507  AVStream* st;
2508  MOVStreamContext* sc;
2509 
2510  if (c->fc->nb_streams < 1)
2511  return 0;
2512 
2513  /* For SBAS this should be fine - though beware if someone implements a
2514  * tref atom processor that doesn't drop down to default then this may
2515  * be lost. */
2516  if (atom.size > 4) {
2517  av_log(c->fc, AV_LOG_ERROR, "Only a single tref of type sbas is supported\n");
2518  return AVERROR_PATCHWELCOME;
2519  }
2520 
2521  st = c->fc->streams[c->fc->nb_streams - 1];
2522  sc = st->priv_data;
2523  sc->tref_id = avio_rb32(pb);
2525 
2526  return 0;
2527 }
2528 
2529 /**
2530  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
2531  * but can have extradata appended at the end after the 40 bytes belonging
2532  * to the struct.
2533  */
2535 {
2536  AVStream *st;
2537  int ret;
2538 
2539  if (c->fc->nb_streams < 1)
2540  return 0;
2541  if (atom.size <= 40)
2542  return 0;
2543  st = c->fc->streams[c->fc->nb_streams-1];
2544 
2545  if ((uint64_t)atom.size > (1<<30))
2546  return AVERROR_INVALIDDATA;
2547 
2548  avio_skip(pb, 40);
2549  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
2550  if (ret < 0)
2551  return ret;
2552 
2553  return 0;
2554 }
2555 
2557 {
2558  AVStream *st;
2559  MOVStreamContext *sc;
2560  unsigned int i, entries;
2561 
2562  if (c->trak_index < 0) {
2563  av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
2564  return 0;
2565  }
2566  if (c->fc->nb_streams < 1)
2567  return 0;
2568  st = c->fc->streams[c->fc->nb_streams-1];
2569  sc = st->priv_data;
2570 
2571  avio_r8(pb); /* version */
2572  avio_rb24(pb); /* flags */
2573 
2574  // Clamp allocation size for `chunk_offsets` -- don't throw an error for an
2575  // invalid count since the EOF path doesn't throw either.
2576  entries = avio_rb32(pb);
2577  entries =
2578  FFMIN(entries,
2579  FFMAX(0, (atom.size - 8) /
2580  (atom.type == MKTAG('s', 't', 'c', 'o') ? 4 : 8)));
2581 
2582  if (!entries)
2583  return 0;
2584 
2585  if (sc->chunk_offsets) {
2586  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STCO atom\n");
2587  return 0;
2588  }
2589 
2590  av_free(sc->chunk_offsets);
2591  sc->chunk_count = 0;
2592  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2593  if (!sc->chunk_offsets)
2594  return AVERROR(ENOMEM);
2595  sc->chunk_count = entries;
2596 
2597  if (atom.type == MKTAG('s','t','c','o'))
2598  for (i = 0; i < entries && !pb->eof_reached; i++)
2599  sc->chunk_offsets[i] = avio_rb32(pb);
2600  else if (atom.type == MKTAG('c','o','6','4'))
2601  for (i = 0; i < entries && !pb->eof_reached; i++) {
2602  sc->chunk_offsets[i] = avio_rb64(pb);
2603  if (sc->chunk_offsets[i] < 0) {
2604  av_log(c->fc, AV_LOG_WARNING, "Impossible chunk_offset\n");
2605  sc->chunk_offsets[i] = 0;
2606  }
2607  }
2608  else
2609  return AVERROR_INVALIDDATA;
2610 
2611  sc->chunk_count = i;
2612 
2613  if (pb->eof_reached) {
2614  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
2615  return AVERROR_EOF;
2616  }
2617 
2618  return 0;
2619 }
2620 
2621 static int mov_codec_id(AVStream *st, uint32_t format)
2622 {
2624 
2625  if (id <= 0 &&
2626  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
2627  (format & 0xFFFF) == 'T' + ('S' << 8)))
2629 
2630  if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
2632  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
2633  /* skip old ASF MPEG-4 tag */
2634  format && format != MKTAG('m','p','4','s')) {
2636  if (id <= 0)
2638  if (id > 0)
2640  else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
2642  st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
2644  if (id <= 0) {
2646  AV_CODEC_ID_TTML : id;
2647  }
2648 
2649  if (id > 0)
2651  else
2653  }
2654  }
2655 
2656  st->codecpar->codec_tag = format;
2657 
2658  return id;
2659 }
2660 
2662  AVStream *st, MOVStreamContext *sc)
2663 {
2664  uint8_t codec_name[32] = { 0 };
2665  int64_t stsd_start;
2666  unsigned int len;
2667  uint32_t id = 0;
2668 
2669  /* The first 16 bytes of the video sample description are already
2670  * read in ff_mov_read_stsd_entries() */
2671  stsd_start = avio_tell(pb) - 16;
2672 
2673  avio_rb16(pb); /* version */
2674  avio_rb16(pb); /* revision level */
2675  id = avio_rl32(pb); /* vendor */
2676  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2677  avio_rb32(pb); /* temporal quality */
2678  avio_rb32(pb); /* spatial quality */
2679 
2680  st->codecpar->width = avio_rb16(pb); /* width */
2681  st->codecpar->height = avio_rb16(pb); /* height */
2682 
2683  avio_rb32(pb); /* horiz resolution */
2684  avio_rb32(pb); /* vert resolution */
2685  avio_rb32(pb); /* data size, always 0 */
2686  avio_rb16(pb); /* frames per samples */
2687 
2688  len = avio_r8(pb); /* codec name, pascal string */
2689  if (len > 31)
2690  len = 31;
2691  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
2692  if (len < 31)
2693  avio_skip(pb, 31 - len);
2694 
2695  if (codec_name[0])
2696  av_dict_set(&st->metadata, "encoder", codec_name, 0);
2697 
2698  /* codec_tag YV12 triggers an UV swap in rawdec.c */
2699  if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
2700  st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
2701  st->codecpar->width &= ~1;
2702  st->codecpar->height &= ~1;
2703  }
2704  /* Flash Media Server uses tag H.263 with Sorenson Spark */
2705  if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
2706  !strncmp(codec_name, "Sorenson H263", 13))
2708 
2709  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
2710 
2711  avio_seek(pb, stsd_start, SEEK_SET);
2712 
2713  if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2714  st->codecpar->bits_per_coded_sample &= 0x1F;
2715  sc->has_palette = 1;
2716  }
2717 }
2718 
2720  AVStream *st, MOVStreamContext *sc)
2721 {
2722  int bits_per_sample, flags;
2723  uint16_t version = avio_rb16(pb);
2724  uint32_t id = 0;
2725  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
2726  int channel_count;
2727 
2728  avio_rb16(pb); /* revision level */
2729  id = avio_rl32(pb); /* vendor */
2730  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2731 
2732  channel_count = avio_rb16(pb);
2733 
2735  st->codecpar->ch_layout.nb_channels = channel_count;
2736  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
2737  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", channel_count);
2738 
2739  sc->audio_cid = avio_rb16(pb);
2740  avio_rb16(pb); /* packet size = 0 */
2741 
2742  st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
2743 
2744  // Read QT version 1 fields. In version 0 these do not exist.
2745  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
2746  if (!c->isom ||
2747  (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
2748  (sc->stsd_version == 0 && version > 0)) {
2749  if (version == 1) {
2750  sc->samples_per_frame = avio_rb32(pb);
2751  avio_rb32(pb); /* bytes per packet */
2752  sc->bytes_per_frame = avio_rb32(pb);
2753  avio_rb32(pb); /* bytes per sample */
2754  } else if (version == 2) {
2755  avio_rb32(pb); /* sizeof struct only */
2757  channel_count = avio_rb32(pb);
2759  st->codecpar->ch_layout.nb_channels = channel_count;
2760  avio_rb32(pb); /* always 0x7F000000 */
2762 
2763  flags = avio_rb32(pb); /* lpcm format specific flag */
2764  sc->bytes_per_frame = avio_rb32(pb);
2765  sc->samples_per_frame = avio_rb32(pb);
2766  if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
2767  st->codecpar->codec_id =
2769  flags);
2770  }
2771  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2772  /* can't correctly handle variable sized packet as audio unit */
2773  switch (st->codecpar->codec_id) {
2774  case AV_CODEC_ID_MP2:
2775  case AV_CODEC_ID_MP3:
2777  break;
2778  }
2779  }
2780  }
2781 
2782  if (sc->format == 0) {
2783  if (st->codecpar->bits_per_coded_sample == 8)
2784  st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
2785  else if (st->codecpar->bits_per_coded_sample == 16)
2786  st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
2787  }
2788 
2789  switch (st->codecpar->codec_id) {
2790  case AV_CODEC_ID_PCM_S8:
2791  case AV_CODEC_ID_PCM_U8:
2792  if (st->codecpar->bits_per_coded_sample == 16)
2794  break;
2795  case AV_CODEC_ID_PCM_S16LE:
2796  case AV_CODEC_ID_PCM_S16BE:
2797  if (st->codecpar->bits_per_coded_sample == 8)
2799  else if (st->codecpar->bits_per_coded_sample == 24)
2800  st->codecpar->codec_id =
2803  else if (st->codecpar->bits_per_coded_sample == 32)
2804  st->codecpar->codec_id =
2807  break;
2808  /* set values for old format before stsd version 1 appeared */
2809  case AV_CODEC_ID_MACE3:
2810  sc->samples_per_frame = 6;
2812  break;
2813  case AV_CODEC_ID_MACE6:
2814  sc->samples_per_frame = 6;
2816  break;
2818  sc->samples_per_frame = 64;
2820  break;
2821  case AV_CODEC_ID_GSM:
2822  sc->samples_per_frame = 160;
2823  sc->bytes_per_frame = 33;
2824  break;
2825  default:
2826  break;
2827  }
2828 
2829  bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2830  if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->ch_layout.nb_channels <= INT_MAX) {
2831  st->codecpar->bits_per_coded_sample = bits_per_sample;
2832  sc->sample_size = (bits_per_sample >> 3) * st->codecpar->ch_layout.nb_channels;
2833  }
2834 }
2835 
2837  AVStream *st, MOVStreamContext *sc,
2838  int64_t size)
2839 {
2840  // ttxt stsd contains display flags, justification, background
2841  // color, fonts, and default styles, so fake an atom to read it
2842  MOVAtom fake_atom = { .size = size };
2843  // mp4s contains a regular esds atom, dfxp ISMV TTML has no content
2844  // in extradata unlike stpp MP4 TTML.
2845  if (st->codecpar->codec_tag != AV_RL32("mp4s") &&
2847  mov_read_glbl(c, pb, fake_atom);
2848  st->codecpar->width = sc->width;
2849  st->codecpar->height = sc->height;
2850 }
2851 
2853  AVStream *st, MOVStreamContext *sc,
2854  int64_t size)
2855 {
2856  int ret;
2857 
2858  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2859  if ((int)size != size)
2860  return AVERROR(ENOMEM);
2861 
2862  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2863  if (ret < 0)
2864  return ret;
2865  if (size > 16) {
2866  MOVStreamContext *tmcd_ctx = st->priv_data;
2867  int val;
2868  val = AV_RB32(st->codecpar->extradata + 4);
2869  tmcd_ctx->tmcd_flags = val;
2870  st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
2871  st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
2872  tmcd_ctx->tmcd_nb_frames = st->codecpar->extradata[16]; /* number of frames */
2873  if (size > 30) {
2874  uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2875  uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2876  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2877  uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2878  if (str_size > 0 && size >= (int)str_size + 30 &&
2879  st->codecpar->extradata[30] /* Don't add empty string */) {
2880  char *reel_name = av_malloc(str_size + 1);
2881  if (!reel_name)
2882  return AVERROR(ENOMEM);
2883  memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2884  reel_name[str_size] = 0; /* Add null terminator */
2885  av_dict_set(&st->metadata, "reel_name", reel_name,
2887  }
2888  }
2889  }
2890  }
2891  } else {
2892  /* other codec type, just skip (rtp, mp4s ...) */
2893  avio_skip(pb, size);
2894  }
2895  return 0;
2896 }
2897 
2899  AVStream *st, MOVStreamContext *sc)
2900 {
2901  FFStream *const sti = ffstream(st);
2902 
2903  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2904  !st->codecpar->sample_rate && sc->time_scale > 1)
2905  st->codecpar->sample_rate = sc->time_scale;
2906 
2907  /* special codec parameters handling */
2908  switch (st->codecpar->codec_id) {
2909 #if CONFIG_DV_DEMUXER
2910  case AV_CODEC_ID_DVAUDIO:
2911  if (c->dv_fctx) {
2912  avpriv_request_sample(c->fc, "multiple DV audio streams");
2913  return AVERROR(ENOSYS);
2914  }
2915 
2916  c->dv_fctx = avformat_alloc_context();
2917  if (!c->dv_fctx) {
2918  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2919  return AVERROR(ENOMEM);
2920  }
2921  c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2922  if (!c->dv_demux) {
2923  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2924  return AVERROR(ENOMEM);
2925  }
2926  sc->dv_audio_container = 1;
2928  break;
2929 #endif
2930  /* no ifdef since parameters are always those */
2931  case AV_CODEC_ID_QCELP:
2934  // force sample rate for qcelp when not stored in mov
2935  if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2936  st->codecpar->sample_rate = 8000;
2937  // FIXME: Why is the following needed for some files?
2938  sc->samples_per_frame = 160;
2939  if (!sc->bytes_per_frame)
2940  sc->bytes_per_frame = 35;
2941  break;
2942  case AV_CODEC_ID_AMR_NB:
2945  /* force sample rate for amr, stsd in 3gp does not store sample rate */
2946  st->codecpar->sample_rate = 8000;
2947  break;
2948  case AV_CODEC_ID_AMR_WB:
2951  st->codecpar->sample_rate = 16000;
2952  break;
2953  case AV_CODEC_ID_MP2:
2954  case AV_CODEC_ID_MP3:
2955  /* force type after stsd for m1a hdlr */
2957  break;
2958  case AV_CODEC_ID_GSM:
2959  case AV_CODEC_ID_ADPCM_MS:
2961  case AV_CODEC_ID_ILBC:
2962  case AV_CODEC_ID_MACE3:
2963  case AV_CODEC_ID_MACE6:
2964  case AV_CODEC_ID_QDM2:
2966  break;
2967  case AV_CODEC_ID_ALAC:
2968  if (st->codecpar->extradata_size == 36) {
2969  int channel_count = AV_RB8(st->codecpar->extradata + 21);
2970  if (st->codecpar->ch_layout.nb_channels != channel_count) {
2973  st->codecpar->ch_layout.nb_channels = channel_count;
2974  }
2975  st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2976  }
2977  break;
2978  case AV_CODEC_ID_AC3:
2979  case AV_CODEC_ID_EAC3:
2981  case AV_CODEC_ID_VC1:
2982  case AV_CODEC_ID_VP8:
2983  case AV_CODEC_ID_VP9:
2985  break;
2986  case AV_CODEC_ID_EVC:
2987  case AV_CODEC_ID_AV1:
2988  /* field_order detection of H264 requires parsing */
2989  case AV_CODEC_ID_H264:
2991  break;
2992  default:
2993  break;
2994  }
2995  return 0;
2996 }
2997 
2999  int codec_tag, int format,
3000  int64_t size)
3001 {
3002  if (codec_tag &&
3003  (codec_tag != format &&
3004  // AVID 1:1 samples with differing data format and codec tag exist
3005  (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
3006  // prores is allowed to have differing data format and codec tag
3007  codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
3008  // so is dv (sigh)
3009  codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
3010  (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
3011  : codec_tag != MKTAG('j','p','e','g')))) {
3012  /* Multiple fourcc, we skip JPEG. This is not correct, we should
3013  * export it as a separate AVStream but this needs a few changes
3014  * in the MOV demuxer, patch welcome. */
3015 
3016  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
3017  avio_skip(pb, size);
3018  return 1;
3019  }
3020 
3021  return 0;
3022 }
3023 
3025 {
3026  AVStream *st;
3027  MOVStreamContext *sc;
3028  int pseudo_stream_id;
3029 
3030  av_assert0 (c->fc->nb_streams >= 1);
3031  st = c->fc->streams[c->fc->nb_streams-1];
3032  sc = st->priv_data;
3033 
3034  for (pseudo_stream_id = 0;
3035  pseudo_stream_id < entries && !pb->eof_reached;
3036  pseudo_stream_id++) {
3037  //Parsing Sample description table
3038  enum AVCodecID id;
3039  int ret, dref_id = 1;
3040  MOVAtom a = { AV_RL32("stsd") };
3041  int64_t start_pos = avio_tell(pb);
3042  int64_t size = avio_rb32(pb); /* size */
3043  uint32_t format = avio_rl32(pb); /* data format */
3044 
3045  if (size >= 16) {
3046  avio_rb32(pb); /* reserved */
3047  avio_rb16(pb); /* reserved */
3048  dref_id = avio_rb16(pb);
3049  } else if (size <= 7) {
3050  av_log(c->fc, AV_LOG_ERROR,
3051  "invalid size %"PRId64" in stsd\n", size);
3052  return AVERROR_INVALIDDATA;
3053  }
3054 
3056  size - (avio_tell(pb) - start_pos))) {
3057  sc->stsd_count++;
3058  continue;
3059  }
3060 
3061  sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
3062  sc->dref_id= dref_id;
3063  sc->format = format;
3064 
3065  id = mov_codec_id(st, format);
3066 
3067  av_log(c->fc, AV_LOG_TRACE,
3068  "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
3070 
3071  st->codecpar->codec_id = id;
3073  mov_parse_stsd_video(c, pb, st, sc);
3074  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
3075  mov_parse_stsd_audio(c, pb, st, sc);
3076  if (st->codecpar->sample_rate < 0) {
3077  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
3078  return AVERROR_INVALIDDATA;
3079  }
3080  if (st->codecpar->ch_layout.nb_channels < 0) {
3081  av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->ch_layout.nb_channels);
3082  return AVERROR_INVALIDDATA;
3083  }
3084  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
3085  mov_parse_stsd_subtitle(c, pb, st, sc,
3086  size - (avio_tell(pb) - start_pos));
3087  } else {
3088  ret = mov_parse_stsd_data(c, pb, st, sc,
3089  size - (avio_tell(pb) - start_pos));
3090  if (ret < 0)
3091  return ret;
3092  }
3093  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
3094  a.size = size - (avio_tell(pb) - start_pos);
3095  if (a.size > 8) {
3096  if ((ret = mov_read_default(c, pb, a)) < 0)
3097  return ret;
3098  } else if (a.size > 0)
3099  avio_skip(pb, a.size);
3100 
3101  if (sc->extradata && st->codecpar->extradata) {
3102  int extra_size = st->codecpar->extradata_size;
3103 
3104  /* Move the current stream extradata to the stream context one. */
3105  sc->extradata_size[pseudo_stream_id] = extra_size;
3106  sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
3107  st->codecpar->extradata = NULL;
3108  st->codecpar->extradata_size = 0;
3109  }
3110  sc->stsd_count++;
3111  }
3112 
3113  if (pb->eof_reached) {
3114  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
3115  return AVERROR_EOF;
3116  }
3117 
3118  return 0;
3119 }
3120 
3122 {
3123  AVStream *st;
3124  MOVStreamContext *sc;
3125  int ret, entries;
3126 
3127  if (c->fc->nb_streams < 1)
3128  return 0;
3129  st = c->fc->streams[c->fc->nb_streams - 1];
3130  sc = st->priv_data;
3131 
3132  sc->stsd_version = avio_r8(pb);
3133  avio_rb24(pb); /* flags */
3134  entries = avio_rb32(pb);
3135 
3136  /* Each entry contains a size (4 bytes) and format (4 bytes). */
3137  if (entries <= 0 || entries > atom.size / 8 || entries > 1024) {
3138  av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
3139  return AVERROR_INVALIDDATA;
3140  }
3141 
3142  if (sc->extradata) {
3143  av_log(c->fc, AV_LOG_ERROR,
3144  "Duplicate stsd found in this track.\n");
3145  return AVERROR_INVALIDDATA;
3146  }
3147 
3148  /* Prepare space for hosting multiple extradata. */
3149  sc->extradata = av_calloc(entries, sizeof(*sc->extradata));
3150  if (!sc->extradata)
3151  return AVERROR(ENOMEM);
3152 
3153  sc->extradata_size = av_calloc(entries, sizeof(*sc->extradata_size));
3154  if (!sc->extradata_size) {
3155  ret = AVERROR(ENOMEM);
3156  goto fail;
3157  }
3158 
3159  ret = ff_mov_read_stsd_entries(c, pb, entries);
3160  if (ret < 0)
3161  goto fail;
3162 
3163  /* Restore back the primary extradata. */
3164  av_freep(&st->codecpar->extradata);
3165  st->codecpar->extradata_size = sc->extradata_size[0];
3166  if (sc->extradata_size[0]) {
3168  if (!st->codecpar->extradata)
3169  return AVERROR(ENOMEM);
3170  memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
3171  }
3172 
3173  return mov_finalize_stsd_codec(c, pb, st, sc);
3174 fail:
3175  if (sc->extradata) {
3176  int j;
3177  for (j = 0; j < sc->stsd_count; j++)
3178  av_freep(&sc->extradata[j]);
3179  }
3180 
3181  av_freep(&sc->extradata);
3182  av_freep(&sc->extradata_size);
3183  return ret;
3184 }
3185 
3187 {
3188  AVStream *st;
3189  MOVStreamContext *sc;
3190  unsigned int i, entries;
3191 
3192  if (c->trak_index < 0) {
3193  av_log(c->fc, AV_LOG_WARNING, "STSC outside TRAK\n");
3194  return 0;
3195  }
3196 
3197  if (c->fc->nb_streams < 1)
3198  return 0;
3199  st = c->fc->streams[c->fc->nb_streams-1];
3200  sc = st->priv_data;
3201 
3202  avio_r8(pb); /* version */
3203  avio_rb24(pb); /* flags */
3204 
3205  entries = avio_rb32(pb);
3206  if ((uint64_t)entries * 12 + 4 > atom.size)
3207  return AVERROR_INVALIDDATA;
3208 
3209  av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
3210 
3211  if (!entries)
3212  return 0;
3213  if (sc->stsc_data) {
3214  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STSC atom\n");
3215  return 0;
3216  }
3217  av_free(sc->stsc_data);
3218  sc->stsc_count = 0;
3219  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
3220  if (!sc->stsc_data)
3221  return AVERROR(ENOMEM);
3222 
3223  for (i = 0; i < entries && !pb->eof_reached; i++) {
3224  sc->stsc_data[i].first = avio_rb32(pb);
3225  sc->stsc_data[i].count = avio_rb32(pb);
3226  sc->stsc_data[i].id = avio_rb32(pb);
3227  }
3228 
3229  sc->stsc_count = i;
3230  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
3231  int64_t first_min = i + 1;
3232  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
3233  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
3234  sc->stsc_data[i].first < first_min ||
3235  sc->stsc_data[i].count < 1 ||
3236  sc->stsc_data[i].id < 1) {
3237  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);
3238  if (i+1 >= sc->stsc_count) {
3239  if (sc->stsc_data[i].count == 0 && i > 0) {
3240  sc->stsc_count --;
3241  continue;
3242  }
3243  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
3244  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
3245  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
3246  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
3247  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
3248  continue;
3249  }
3250  av_assert0(sc->stsc_data[i+1].first >= 2);
3251  // We replace this entry by the next valid
3252  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
3253  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
3254  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
3255  }
3256  }
3257 
3258  if (pb->eof_reached) {
3259  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
3260  return AVERROR_EOF;
3261  }
3262 
3263  return 0;
3264 }
3265 
3266 static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
3267 {
3268  return index < count - 1;
3269 }
3270 
3271 /* Compute the samples value for the stsc entry at the given index. */
3272 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
3273 {
3274  int chunk_count;
3275 
3277  chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
3278  else {
3279  // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
3281  chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
3282  }
3283 
3284  return sc->stsc_data[index].count * (int64_t)chunk_count;
3285 }
3286 
3288 {
3289  AVStream *st;
3290  MOVStreamContext *sc;
3291  unsigned i, entries;
3292 
3293  if (c->trak_index < 0) {
3294  av_log(c->fc, AV_LOG_WARNING, "STPS outside TRAK\n");
3295  return 0;
3296  }
3297 
3298  if (c->fc->nb_streams < 1)
3299  return 0;
3300  st = c->fc->streams[c->fc->nb_streams-1];
3301  sc = st->priv_data;
3302 
3303  avio_rb32(pb); // version + flags
3304 
3305  entries = avio_rb32(pb);
3306  if (sc->stps_data)
3307  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
3308  av_free(sc->stps_data);
3309  sc->stps_count = 0;
3310  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
3311  if (!sc->stps_data)
3312  return AVERROR(ENOMEM);
3313 
3314  for (i = 0; i < entries && !pb->eof_reached; i++) {
3315  sc->stps_data[i] = avio_rb32(pb);
3316  }
3317 
3318  sc->stps_count = i;
3319 
3320  if (pb->eof_reached) {
3321  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
3322  return AVERROR_EOF;
3323  }
3324 
3325  return 0;
3326 }
3327 
3329 {
3330  AVStream *st;
3331  FFStream *sti;
3332  MOVStreamContext *sc;
3333  unsigned int i, entries;
3334 
3335  if (c->trak_index < 0) {
3336  av_log(c->fc, AV_LOG_WARNING, "STSS outside TRAK\n");
3337  return 0;
3338  }
3339 
3340  if (c->fc->nb_streams < 1)
3341  return 0;
3342  st = c->fc->streams[c->fc->nb_streams-1];
3343  sti = ffstream(st);
3344  sc = st->priv_data;
3345 
3346  avio_r8(pb); /* version */
3347  avio_rb24(pb); /* flags */
3348 
3349  entries = avio_rb32(pb);
3350 
3351  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
3352 
3353  if (!entries) {
3354  sc->keyframe_absent = 1;
3355  if (!sti->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
3357  return 0;
3358  }
3359  if (sc->keyframes)
3360  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
3361  if (entries >= UINT_MAX / sizeof(int))
3362  return AVERROR_INVALIDDATA;
3363  av_freep(&sc->keyframes);
3364  sc->keyframe_count = 0;
3365  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
3366  if (!sc->keyframes)
3367  return AVERROR(ENOMEM);
3368 
3369  for (i = 0; i < entries && !pb->eof_reached; i++) {
3370  sc->keyframes[i] = avio_rb32(pb);
3371  }
3372 
3373  sc->keyframe_count = i;
3374 
3375  if (pb->eof_reached) {
3376  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
3377  return AVERROR_EOF;
3378  }
3379 
3380  return 0;
3381 }
3382 
3384 {
3385  AVStream *st;
3386  MOVStreamContext *sc;
3387  unsigned int i, entries, sample_size, field_size, num_bytes;
3388  GetBitContext gb;
3389  unsigned char* buf;
3390  int ret;
3391 
3392  if (c->trak_index < 0) {
3393  av_log(c->fc, AV_LOG_WARNING, "STSZ outside TRAK\n");
3394  return 0;
3395  }
3396 
3397  if (c->fc->nb_streams < 1)
3398  return 0;
3399  st = c->fc->streams[c->fc->nb_streams-1];
3400  sc = st->priv_data;
3401 
3402  avio_r8(pb); /* version */
3403  avio_rb24(pb); /* flags */
3404 
3405  if (atom.type == MKTAG('s','t','s','z')) {
3406  sample_size = avio_rb32(pb);
3407  if (!sc->sample_size) /* do not overwrite value computed in stsd */
3408  sc->sample_size = sample_size;
3409  sc->stsz_sample_size = sample_size;
3410  field_size = 32;
3411  } else {
3412  sample_size = 0;
3413  avio_rb24(pb); /* reserved */
3414  field_size = avio_r8(pb);
3415  }
3416  entries = avio_rb32(pb);
3417 
3418  av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
3419 
3420  sc->sample_count = entries;
3421  if (sample_size)
3422  return 0;
3423 
3424  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
3425  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
3426  return AVERROR_INVALIDDATA;
3427  }
3428 
3429  if (!entries)
3430  return 0;
3431  if (entries >= (INT_MAX - 4 - 8 * AV_INPUT_BUFFER_PADDING_SIZE) / field_size)
3432  return AVERROR_INVALIDDATA;
3433  if (sc->sample_sizes)
3434  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
3435  av_free(sc->sample_sizes);
3436  sc->sample_count = 0;
3437  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
3438  if (!sc->sample_sizes)
3439  return AVERROR(ENOMEM);
3440 
3441  num_bytes = (entries*field_size+4)>>3;
3442 
3443  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
3444  if (!buf) {
3445  av_freep(&sc->sample_sizes);
3446  return AVERROR(ENOMEM);
3447  }
3448 
3449  ret = ffio_read_size(pb, buf, num_bytes);
3450  if (ret < 0) {
3451  av_freep(&sc->sample_sizes);
3452  av_free(buf);
3453  av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
3454  return 0;
3455  }
3456 
3457  init_get_bits(&gb, buf, 8*num_bytes);
3458 
3459  for (i = 0; i < entries; i++) {
3460  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
3461  if (sc->sample_sizes[i] > INT64_MAX - sc->data_size) {
3462  av_free(buf);
3463  av_log(c->fc, AV_LOG_ERROR, "Sample size overflow in STSZ\n");
3464  return AVERROR_INVALIDDATA;
3465  }
3466  sc->data_size += sc->sample_sizes[i];
3467  }
3468 
3469  sc->sample_count = i;
3470 
3471  av_free(buf);
3472 
3473  return 0;
3474 }
3475 
3477 {
3478  AVStream *st;
3479  MOVStreamContext *sc;
3480  unsigned int i, entries;
3481  int64_t duration = 0;
3482  int64_t total_sample_count = 0;
3483  int64_t current_dts = 0;
3484  int64_t corrected_dts = 0;
3485 
3486  if (c->trak_index < 0) {
3487  av_log(c->fc, AV_LOG_WARNING, "STTS outside TRAK\n");
3488  return 0;
3489  }
3490 
3491  if (c->fc->nb_streams < 1)
3492  return 0;
3493  st = c->fc->streams[c->fc->nb_streams-1];
3494  sc = st->priv_data;
3495 
3496  avio_r8(pb); /* version */
3497  avio_rb24(pb); /* flags */
3498  entries = avio_rb32(pb);
3499 
3500  av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
3501  c->fc->nb_streams-1, entries);
3502 
3503  if (sc->stts_data)
3504  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
3505  av_freep(&sc->stts_data);
3506  sc->stts_count = 0;
3507  if (entries >= INT_MAX / sizeof(*sc->stts_data))
3508  return AVERROR(ENOMEM);
3509 
3510  for (i = 0; i < entries && !pb->eof_reached; i++) {
3511  unsigned int sample_duration;
3512  unsigned int sample_count;
3513  unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
3514  MOVStts *stts_data = av_fast_realloc(sc->stts_data, &sc->stts_allocated_size,
3515  min_entries * sizeof(*sc->stts_data));
3516  if (!stts_data) {
3517  av_freep(&sc->stts_data);
3518  sc->stts_count = 0;
3519  return AVERROR(ENOMEM);
3520  }
3521  sc->stts_count = min_entries;
3522  sc->stts_data = stts_data;
3523 
3524  sample_count = avio_rb32(pb);
3525  sample_duration = avio_rb32(pb);
3526 
3527  sc->stts_data[i].count= sample_count;
3528  sc->stts_data[i].duration= sample_duration;
3529 
3530  av_log(c->fc, AV_LOG_TRACE, "sample_count=%u, sample_duration=%u\n",
3531  sample_count, sample_duration);
3532 
3533  /* STTS sample offsets are uint32 but some files store it as int32
3534  * with negative values used to correct DTS delays.
3535  There may be abnormally large values as well. */
3536  if (sample_duration > c->max_stts_delta) {
3537  // assume high delta is a correction if negative when cast as int32
3538  int32_t delta_magnitude = (int32_t)sample_duration;
3539  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",
3540  sample_duration, i, sample_count, st->index);
3541  sc->stts_data[i].duration = 1;
3542  corrected_dts += (delta_magnitude < 0 ? (int64_t)delta_magnitude : 1) * sample_count;
3543  } else {
3544  corrected_dts += sample_duration * (uint64_t)sample_count;
3545  }
3546 
3547  current_dts += sc->stts_data[i].duration * (uint64_t)sample_count;
3548 
3549  if (current_dts > corrected_dts) {
3550  int64_t drift = av_sat_sub64(current_dts, corrected_dts) / FFMAX(sample_count, 1);
3551  uint32_t correction = (sc->stts_data[i].duration > drift) ? drift : sc->stts_data[i].duration - 1;
3552  current_dts -= correction * (uint64_t)sample_count;
3553  sc->stts_data[i].duration -= correction;
3554  }
3555 
3556  duration+=(int64_t)sc->stts_data[i].duration*(uint64_t)sc->stts_data[i].count;
3557  total_sample_count+=sc->stts_data[i].count;
3558  }
3559 
3560  sc->stts_count = i;
3561 
3562  if (duration > 0 &&
3563  duration <= INT64_MAX - sc->duration_for_fps &&
3564  total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
3565  sc->duration_for_fps += duration;
3566  sc->nb_frames_for_fps += total_sample_count;
3567  }
3568 
3569  if (pb->eof_reached) {
3570  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
3571  return AVERROR_EOF;
3572  }
3573 
3574  st->nb_frames= total_sample_count;
3575  if (duration)
3576  st->duration= FFMIN(st->duration, duration);
3577 
3578  // All samples have zero duration. They have higher chance be chose by
3579  // mov_find_next_sample, which leads to seek again and again.
3580  //
3581  // It's AVERROR_INVALIDDATA actually, but such files exist in the wild.
3582  // So only mark data stream as discarded for safety.
3583  if (!duration && sc->stts_count &&
3585  av_log(c->fc, AV_LOG_WARNING,
3586  "All samples in data stream index:id [%d:%d] have zero "
3587  "duration, stream set to be discarded by default. Override "
3588  "using AVStream->discard or -discard for ffmpeg command.\n",
3589  st->index, sc->id);
3590  st->discard = AVDISCARD_ALL;
3591  }
3592  sc->track_end = duration;
3593  return 0;
3594 }
3595 
3597 {
3598  AVStream *st;
3599  MOVStreamContext *sc;
3600  int64_t i, entries;
3601 
3602  if (c->fc->nb_streams < 1)
3603  return 0;
3604  st = c->fc->streams[c->fc->nb_streams - 1];
3605  sc = st->priv_data;
3606 
3607  avio_r8(pb); /* version */
3608  avio_rb24(pb); /* flags */
3609  entries = atom.size - 4;
3610 
3611  av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
3612  c->fc->nb_streams - 1, entries);
3613 
3614  if (sc->sdtp_data)
3615  av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
3616  av_freep(&sc->sdtp_data);
3617  sc->sdtp_count = 0;
3618 
3619  sc->sdtp_data = av_malloc(entries);
3620  if (!sc->sdtp_data)
3621  return AVERROR(ENOMEM);
3622 
3623  for (i = 0; i < entries && !pb->eof_reached; i++)
3624  sc->sdtp_data[i] = avio_r8(pb);
3625  sc->sdtp_count = i;
3626 
3627  return 0;
3628 }
3629 
3630 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3631 {
3632  if (duration < 0) {
3633  if (duration == INT_MIN) {
3634  av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
3635  duration++;
3636  }
3637  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3638  }
3639 }
3640 
3642 {
3643  AVStream *st;
3644  MOVStreamContext *sc;
3645  unsigned int i, entries, ctts_count = 0;
3646 
3647  if (c->trak_index < 0) {
3648  av_log(c->fc, AV_LOG_WARNING, "CTTS outside TRAK\n");
3649  return 0;
3650  }
3651 
3652  if (c->fc->nb_streams < 1)
3653  return 0;
3654  st = c->fc->streams[c->fc->nb_streams-1];
3655  sc = st->priv_data;
3656 
3657  avio_r8(pb); /* version */
3658  avio_rb24(pb); /* flags */
3659  entries = avio_rb32(pb);
3660 
3661  av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
3662 
3663  if (!entries)
3664  return 0;
3665  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3666  return AVERROR_INVALIDDATA;
3667  av_freep(&sc->ctts_data);
3668  sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3669  if (!sc->ctts_data)
3670  return AVERROR(ENOMEM);
3671 
3672  for (i = 0; i < entries && !pb->eof_reached; i++) {
3673  MOVCtts *ctts_data;
3674  const size_t min_size_needed = (ctts_count + 1) * sizeof(MOVCtts);
3675  const size_t requested_size =
3676  min_size_needed > sc->ctts_allocated_size ?
3677  FFMAX(min_size_needed, 2 * sc->ctts_allocated_size) :
3678  min_size_needed;
3679  int count = avio_rb32(pb);
3680  int duration = avio_rb32(pb);
3681 
3682  if (count <= 0) {
3683  av_log(c->fc, AV_LOG_TRACE,
3684  "ignoring CTTS entry with count=%d duration=%d\n",
3685  count, duration);
3686  continue;
3687  }
3688 
3689  if (ctts_count >= UINT_MAX / sizeof(MOVCtts) - 1)
3690  return AVERROR(ENOMEM);
3691 
3692  ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size, requested_size);
3693 
3694  if (!ctts_data)
3695  return AVERROR(ENOMEM);
3696 
3697  sc->ctts_data = ctts_data;
3698 
3699  ctts_data[ctts_count].count = count;
3700  ctts_data[ctts_count].offset = duration;
3701  ctts_count++;
3702 
3703  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
3704  count, duration);
3705 
3706  if (FFNABS(duration) < -(1<<28) && i+2<entries) {
3707  av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
3708  av_freep(&sc->ctts_data);
3709  sc->ctts_count = 0;
3710  return 0;
3711  }
3712 
3713  if (i+2<entries)
3714  mov_update_dts_shift(sc, duration, c->fc);
3715  }
3716 
3717  sc->ctts_count = ctts_count;
3718 
3719  if (pb->eof_reached) {
3720  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
3721  return AVERROR_EOF;
3722  }
3723 
3724  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3725 
3726  return 0;
3727 }
3728 
3730 {
3731  AVStream *st;
3732  MOVStreamContext *sc;
3733  uint8_t version;
3734  uint32_t grouping_type;
3735  uint32_t default_length;
3736  av_unused uint32_t default_group_description_index;
3737  uint32_t entry_count;
3738 
3739  if (c->fc->nb_streams < 1)
3740  return 0;
3741  st = c->fc->streams[c->fc->nb_streams - 1];
3742  sc = st->priv_data;
3743 
3744  version = avio_r8(pb); /* version */
3745  avio_rb24(pb); /* flags */
3746  grouping_type = avio_rl32(pb);
3747 
3748  /*
3749  * This function only supports "sync" boxes, but the code is able to parse
3750  * other boxes (such as "tscl", "tsas" and "stsa")
3751  */
3752  if (grouping_type != MKTAG('s','y','n','c'))
3753  return 0;
3754 
3755  default_length = version >= 1 ? avio_rb32(pb) : 0;
3756  default_group_description_index = version >= 2 ? avio_rb32(pb) : 0;
3757  entry_count = avio_rb32(pb);
3758 
3759  av_freep(&sc->sgpd_sync);
3760  sc->sgpd_sync_count = entry_count;
3761  sc->sgpd_sync = av_calloc(entry_count, sizeof(*sc->sgpd_sync));
3762  if (!sc->sgpd_sync)
3763  return AVERROR(ENOMEM);
3764 
3765  for (uint32_t i = 0; i < entry_count && !pb->eof_reached; i++) {
3766  uint32_t description_length = default_length;
3767  if (version >= 1 && default_length == 0)
3768  description_length = avio_rb32(pb);
3769  if (grouping_type == MKTAG('s','y','n','c')) {
3770  const uint8_t nal_unit_type = avio_r8(pb) & 0x3f;
3771  sc->sgpd_sync[i] = nal_unit_type;
3772  description_length -= 1;
3773  }
3774  avio_skip(pb, description_length);
3775  }
3776 
3777  if (pb->eof_reached) {
3778  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SGPD atom\n");
3779  return AVERROR_EOF;
3780  }
3781 
3782  return 0;
3783 }
3784 
3786 {
3787  AVStream *st;
3788  MOVStreamContext *sc;
3789  unsigned int i, entries;
3790  uint8_t version;
3791  uint32_t grouping_type;
3792  MOVSbgp *table, **tablep;
3793  int *table_count;
3794 
3795  if (c->fc->nb_streams < 1)
3796  return 0;
3797  st = c->fc->streams[c->fc->nb_streams-1];
3798  sc = st->priv_data;
3799 
3800  version = avio_r8(pb); /* version */
3801  avio_rb24(pb); /* flags */
3802  grouping_type = avio_rl32(pb);
3803 
3804  if (grouping_type == MKTAG('r','a','p',' ')) {
3805  tablep = &sc->rap_group;
3806  table_count = &sc->rap_group_count;
3807  } else if (grouping_type == MKTAG('s','y','n','c')) {
3808  tablep = &sc->sync_group;
3809  table_count = &sc->sync_group_count;
3810  } else {
3811  return 0;
3812  }
3813 
3814  if (version == 1)
3815  avio_rb32(pb); /* grouping_type_parameter */
3816 
3817  entries = avio_rb32(pb);
3818  if (!entries)
3819  return 0;
3820  if (*tablep)
3821  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP %s atom\n", av_fourcc2str(grouping_type));
3822  av_freep(tablep);
3823  table = av_malloc_array(entries, sizeof(*table));
3824  if (!table)
3825  return AVERROR(ENOMEM);
3826  *tablep = table;
3827 
3828  for (i = 0; i < entries && !pb->eof_reached; i++) {
3829  table[i].count = avio_rb32(pb); /* sample_count */
3830  table[i].index = avio_rb32(pb); /* group_description_index */
3831  }
3832 
3833  *table_count = i;
3834 
3835  if (pb->eof_reached) {
3836  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
3837  return AVERROR_EOF;
3838  }
3839 
3840  return 0;
3841 }
3842 
3843 /**
3844  * Get ith edit list entry (media time, duration).
3845  */
3847  const MOVStreamContext *msc,
3848  unsigned int edit_list_index,
3849  int64_t *edit_list_media_time,
3850  int64_t *edit_list_duration,
3851  int64_t global_timescale)
3852 {
3853  if (edit_list_index == msc->elst_count) {
3854  return 0;
3855  }
3856  *edit_list_media_time = msc->elst_data[edit_list_index].time;
3857  *edit_list_duration = msc->elst_data[edit_list_index].duration;
3858 
3859  /* duration is in global timescale units;convert to msc timescale */
3860  if (global_timescale == 0) {
3861  avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
3862  return 0;
3863  }
3864  *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
3865  global_timescale);
3866 
3867  if (*edit_list_duration + (uint64_t)*edit_list_media_time > INT64_MAX)
3868  *edit_list_duration = 0;
3869 
3870  return 1;
3871 }
3872 
3873 /**
3874  * Find the closest previous frame to the timestamp_pts, in e_old index
3875  * entries. Searching for just any frame / just key frames can be controlled by
3876  * last argument 'flag'.
3877  * Note that if ctts_data is not NULL, we will always search for a key frame
3878  * irrespective of the value of 'flag'. If we don't find any keyframe, we will
3879  * return the first frame of the video.
3880  *
3881  * Here the timestamp_pts is considered to be a presentation timestamp and
3882  * the timestamp of index entries are considered to be decoding timestamps.
3883  *
3884  * Returns 0 if successful in finding a frame, else returns -1.
3885  * Places the found index corresponding output arg.
3886  *
3887  * If ctts_old is not NULL, then refines the searched entry by searching
3888  * backwards from the found timestamp, to find the frame with correct PTS.
3889  *
3890  * Places the found ctts_index and ctts_sample in corresponding output args.
3891  */
3893  AVIndexEntry *e_old,
3894  int nb_old,
3895  MOVTimeToSample *tts_data,
3896  int64_t tts_count,
3897  int64_t timestamp_pts,
3898  int flag,
3899  int64_t* index,
3900  int64_t* tts_index,
3901  int64_t* tts_sample)
3902 {
3903  MOVStreamContext *msc = st->priv_data;
3904  FFStream *const sti = ffstream(st);
3905  AVIndexEntry *e_keep = sti->index_entries;
3906  int nb_keep = sti->nb_index_entries;
3907  int64_t i = 0;
3908 
3909  av_assert0(index);
3910 
3911  // If dts_shift > 0, then all the index timestamps will have to be offset by
3912  // at least dts_shift amount to obtain PTS.
3913  // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
3914  if (msc->dts_shift > 0) {
3915  timestamp_pts -= msc->dts_shift;
3916  }
3917 
3918  sti->index_entries = e_old;
3919  sti->nb_index_entries = nb_old;
3920  *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
3921 
3922  // Keep going backwards in the index entries until the timestamp is the same.
3923  if (*index >= 0) {
3924  for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
3925  i--) {
3926  if ((flag & AVSEEK_FLAG_ANY) ||
3927  (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
3928  *index = i - 1;
3929  }
3930  }
3931  }
3932 
3933  // If we have CTTS then refine the search, by searching backwards over PTS
3934  // computed by adding corresponding CTTS durations to index timestamps.
3935  if (msc->ctts_count && *index >= 0) {
3936  av_assert0(tts_index);
3937  av_assert0(tts_sample);
3938  // Find out the ctts_index for the found frame.
3939  *tts_index = 0;
3940  *tts_sample = 0;
3941  for (int64_t index_tts_count = 0; index_tts_count < *index; index_tts_count++) {
3942  if (*tts_index < tts_count) {
3943  (*tts_sample)++;
3944  if (tts_data[*tts_index].count == *tts_sample) {
3945  (*tts_index)++;
3946  *tts_sample = 0;
3947  }
3948  }
3949  }
3950 
3951  while (*index >= 0 && (*tts_index) >= 0 && (*tts_index) < tts_count) {
3952  // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
3953  // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
3954  // compensated by dts_shift above.
3955  if ((e_old[*index].timestamp + tts_data[*tts_index].offset) <= timestamp_pts &&
3956  (e_old[*index].flags & AVINDEX_KEYFRAME)) {
3957  break;
3958  }
3959 
3960  (*index)--;
3961  if (*tts_sample == 0) {
3962  (*tts_index)--;
3963  if (*tts_index >= 0)
3964  *tts_sample = tts_data[*tts_index].count - 1;
3965  } else {
3966  (*tts_sample)--;
3967  }
3968  }
3969  }
3970 
3971  /* restore AVStream state*/
3972  sti->index_entries = e_keep;
3973  sti->nb_index_entries = nb_keep;
3974  return *index >= 0 ? 0 : -1;
3975 }
3976 
3977 /**
3978  * Add index entry with the given values, to the end of ffstream(st)->index_entries.
3979  * Returns the new size ffstream(st)->index_entries if successful, else returns -1.
3980  *
3981  * This function is similar to ff_add_index_entry in libavformat/utils.c
3982  * except that here we are always unconditionally adding an index entry to
3983  * the end, instead of searching the entries list and skipping the add if
3984  * there is an existing entry with the same timestamp.
3985  * This is needed because the mov_fix_index calls this func with the same
3986  * unincremented timestamp for successive discarded frames.
3987  */
3989  int size, int distance, int flags)
3990 {
3991  FFStream *const sti = ffstream(st);
3992  AVIndexEntry *entries, *ie;
3993  int64_t index = -1;
3994  const size_t min_size_needed = (sti->nb_index_entries + 1) * sizeof(AVIndexEntry);
3995 
3996  // Double the allocation each time, to lower memory fragmentation.
3997  // Another difference from ff_add_index_entry function.
3998  const size_t requested_size =
3999  min_size_needed > sti->index_entries_allocated_size ?
4000  FFMAX(min_size_needed, 2 * sti->index_entries_allocated_size) :
4001  min_size_needed;
4002 
4003  if (sti->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
4004  return -1;
4005 
4006  entries = av_fast_realloc(sti->index_entries,
4008  requested_size);
4009  if (!entries)
4010  return -1;
4011 
4012  sti->index_entries = entries;
4013 
4014  index = sti->nb_index_entries++;
4015  ie= &entries[index];
4016 
4017  ie->pos = pos;
4018  ie->timestamp = timestamp;
4019  ie->min_distance= distance;
4020  ie->size= size;
4021  ie->flags = flags;
4022  return index;
4023 }
4024 
4025 /**
4026  * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
4027  * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
4028  */
4029 static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
4030  int64_t* frame_duration_buffer,
4031  int frame_duration_buffer_size) {
4032  FFStream *const sti = ffstream(st);
4033  int i = 0;
4034  av_assert0(end_index >= 0 && end_index <= sti->nb_index_entries);
4035  for (i = 0; i < frame_duration_buffer_size; i++) {
4036  end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
4037  sti->index_entries[end_index - 1 - i].timestamp = end_ts;
4038  }
4039 }
4040 
4041 static int add_tts_entry(MOVTimeToSample **tts_data, unsigned int *tts_count, unsigned int *allocated_size,
4042  int count, int offset, unsigned int duration)
4043 {
4044  MOVTimeToSample *tts_buf_new;
4045  const size_t min_size_needed = (*tts_count + 1) * sizeof(MOVTimeToSample);
4046  const size_t requested_size =
4047  min_size_needed > *allocated_size ?
4048  FFMAX(min_size_needed, 2 * (*allocated_size)) :
4049  min_size_needed;
4050 
4051  if ((unsigned)(*tts_count) >= UINT_MAX / sizeof(MOVTimeToSample) - 1)
4052  return -1;
4053 
4054  tts_buf_new = av_fast_realloc(*tts_data, allocated_size, requested_size);
4055 
4056  if (!tts_buf_new)
4057  return -1;
4058 
4059  *tts_data = tts_buf_new;
4060 
4061  tts_buf_new[*tts_count].count = count;
4062  tts_buf_new[*tts_count].offset = offset;
4063  tts_buf_new[*tts_count].duration = duration;
4064 
4065  *tts_count = (*tts_count) + 1;
4066  return 0;
4067 }
4068 
4069 #define MAX_REORDER_DELAY 16
4071 {
4072  MOVStreamContext *msc = st->priv_data;
4073  FFStream *const sti = ffstream(st);
4074  int ctts_ind = 0;
4075  int ctts_sample = 0;
4076  int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
4077  int buf_start = 0;
4078  int j, r, num_swaps;
4079 
4080  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
4081  pts_buf[j] = INT64_MIN;
4082 
4083  if (st->codecpar->video_delay <= 0 && msc->ctts_count &&
4085  st->codecpar->video_delay = 0;
4086  for (int ind = 0; ind < sti->nb_index_entries && ctts_ind < msc->tts_count; ++ind) {
4087  // Point j to the last elem of the buffer and insert the current pts there.
4088  j = buf_start;
4089  buf_start = (buf_start + 1);
4090  if (buf_start == MAX_REORDER_DELAY + 1)
4091  buf_start = 0;
4092 
4093  pts_buf[j] = sti->index_entries[ind].timestamp + msc->tts_data[ctts_ind].offset;
4094 
4095  // The timestamps that are already in the sorted buffer, and are greater than the
4096  // current pts, are exactly the timestamps that need to be buffered to output PTS
4097  // in correct sorted order.
4098  // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
4099  // can be computed as the maximum no. of swaps any particular timestamp needs to
4100  // go through, to keep this buffer in sorted order.
4101  num_swaps = 0;
4102  while (j != buf_start) {
4103  r = j - 1;
4104  if (r < 0) r = MAX_REORDER_DELAY;
4105  if (pts_buf[j] < pts_buf[r]) {
4106  FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
4107  ++num_swaps;
4108  } else {
4109  break;
4110  }
4111  j = r;
4112  }
4113  st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
4114 
4115  ctts_sample++;
4116  if (ctts_sample == msc->tts_data[ctts_ind].count) {
4117  ctts_ind++;
4118  ctts_sample = 0;
4119  }
4120  }
4121  av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
4122  st->codecpar->video_delay, st->index);
4123  }
4124 }
4125 
4127 {
4128  sc->current_sample++;
4129  sc->current_index++;
4130  if (sc->index_ranges &&
4131  sc->current_index >= sc->current_index_range->end &&
4132  sc->current_index_range->end) {
4133  sc->current_index_range++;
4135  }
4136 }
4137 
4139 {
4140  sc->current_sample--;
4141  sc->current_index--;
4142  if (sc->index_ranges &&
4144  sc->current_index_range > sc->index_ranges) {
4145  sc->current_index_range--;
4146  sc->current_index = sc->current_index_range->end - 1;
4147  }
4148 }
4149 
4150 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
4151 {
4152  int64_t range_size;
4153 
4154  sc->current_sample = current_sample;
4155  sc->current_index = current_sample;
4156  if (!sc->index_ranges) {
4157  return;
4158  }
4159 
4160  for (sc->current_index_range = sc->index_ranges;
4161  sc->current_index_range->end;
4162  sc->current_index_range++) {
4163  range_size = sc->current_index_range->end - sc->current_index_range->start;
4164  if (range_size > current_sample) {
4165  sc->current_index = sc->current_index_range->start + current_sample;
4166  break;
4167  }
4168  current_sample -= range_size;
4169  }
4170 }
4171 
4172 /**
4173  * Fix ffstream(st)->index_entries, so that it contains only the entries (and the entries
4174  * which are needed to decode them) that fall in the edit list time ranges.
4175  * Also fixes the timestamps of the index entries to match the timeline
4176  * specified the edit lists.
4177  */
4178 static void mov_fix_index(MOVContext *mov, AVStream *st)
4179 {
4180  MOVStreamContext *msc = st->priv_data;
4181  FFStream *const sti = ffstream(st);
4182  AVIndexEntry *e_old = sti->index_entries;
4183  int nb_old = sti->nb_index_entries;
4184  const AVIndexEntry *e_old_end = e_old + nb_old;
4185  const AVIndexEntry *current = NULL;
4186  MOVTimeToSample *tts_data_old = msc->tts_data;
4187  int64_t tts_index_old = 0;
4188  int64_t tts_sample_old = 0;
4189  int64_t tts_count_old = msc->tts_count;
4190  int64_t edit_list_media_time = 0;
4191  int64_t edit_list_duration = 0;
4192  int64_t frame_duration = 0;
4193  int64_t edit_list_dts_counter = 0;
4194  int64_t edit_list_dts_entry_end = 0;
4195  int64_t edit_list_start_tts_sample = 0;
4196  int64_t curr_cts;
4197  int64_t curr_ctts = 0;
4198  int64_t empty_edits_sum_duration = 0;
4199  int64_t edit_list_index = 0;
4200  int64_t index;
4201  int flags;
4202  int64_t start_dts = 0;
4203  int64_t edit_list_start_encountered = 0;
4204  int64_t search_timestamp = 0;
4205  int64_t* frame_duration_buffer = NULL;
4206  int num_discarded_begin = 0;
4207  int first_non_zero_audio_edit = -1;
4208  int packet_skip_samples = 0;
4209  MOVIndexRange *current_index_range = NULL;
4210  int found_keyframe_after_edit = 0;
4211  int found_non_empty_edit = 0;
4212 
4213  if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
4214  return;
4215  }
4216 
4217  // allocate the index ranges array
4218  msc->index_ranges = av_malloc_array(msc->elst_count + 1,
4219  sizeof(msc->index_ranges[0]));
4220  if (!msc->index_ranges) {
4221  av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
4222  return;
4223  }
4224  msc->current_index_range = msc->index_ranges;
4225 
4226  // Clean AVStream from traces of old index
4227  sti->index_entries = NULL;
4229  sti->nb_index_entries = 0;
4230 
4231  // Clean time to sample fields of MOVStreamContext
4232  msc->tts_data = NULL;
4233  msc->tts_count = 0;
4234  msc->tts_index = 0;
4235  msc->tts_sample = 0;
4236  msc->tts_allocated_size = 0;
4237 
4238  // Reinitialize min_corrected_pts so that it can be computed again.
4239  msc->min_corrected_pts = -1;
4240 
4241  // If the dts_shift is positive (in case of negative ctts values in mov),
4242  // then negate the DTS by dts_shift
4243  if (msc->dts_shift > 0) {
4244  edit_list_dts_entry_end -= msc->dts_shift;
4245  av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
4246  }
4247 
4248  start_dts = edit_list_dts_entry_end;
4249 
4250  while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
4251  &edit_list_duration, mov->time_scale)) {
4252  av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
4253  st->index, edit_list_index, edit_list_media_time, edit_list_duration);
4254  edit_list_index++;
4255  edit_list_dts_counter = edit_list_dts_entry_end;
4256  edit_list_dts_entry_end += edit_list_duration;
4257  num_discarded_begin = 0;
4258  if (!found_non_empty_edit && edit_list_media_time == -1) {
4259  empty_edits_sum_duration += edit_list_duration;
4260  continue;
4261  }
4262  found_non_empty_edit = 1;
4263 
4264  // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
4265  // according to the edit list below.
4266  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
4267  if (first_non_zero_audio_edit < 0) {
4268  first_non_zero_audio_edit = 1;
4269  } else {
4270  first_non_zero_audio_edit = 0;
4271  }
4272 
4273  if (first_non_zero_audio_edit > 0)
4274  sti->skip_samples = msc->start_pad = 0;
4275  }
4276 
4277  // While reordering frame index according to edit list we must handle properly
4278  // the scenario when edit list entry starts from none key frame.
4279  // We find closest previous key frame and preserve it and consequent frames in index.
4280  // All frames which are outside edit list entry time boundaries will be dropped after decoding.
4281  search_timestamp = edit_list_media_time;
4282  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
4283  // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
4284  // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
4285  // edit_list_media_time to cover the decoder delay.
4286  search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
4287  }
4288 
4289  if (find_prev_closest_index(st, e_old, nb_old, tts_data_old, tts_count_old, search_timestamp, 0,
4290  &index, &tts_index_old, &tts_sample_old) < 0) {
4291  av_log(mov->fc, AV_LOG_WARNING,
4292  "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
4293  st->index, edit_list_index, search_timestamp);
4294  if (find_prev_closest_index(st, e_old, nb_old, tts_data_old, tts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
4295  &index, &tts_index_old, &tts_sample_old) < 0) {
4296  av_log(mov->fc, AV_LOG_WARNING,
4297  "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
4298  st->index, edit_list_index, search_timestamp);
4299  index = 0;
4300  tts_index_old = 0;
4301  tts_sample_old = 0;
4302  }
4303  }
4304  current = e_old + index;
4305  edit_list_start_tts_sample = tts_sample_old;
4306 
4307  // Iterate over index and arrange it according to edit list
4308  edit_list_start_encountered = 0;
4309  found_keyframe_after_edit = 0;
4310  for (; current < e_old_end; current++, index++) {
4311  // check if frame outside edit list mark it for discard
4312  frame_duration = (current + 1 < e_old_end) ?
4313  ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
4314 
4315  flags = current->flags;
4316 
4317  // frames (pts) before or after edit list
4318  curr_cts = current->timestamp + msc->dts_shift;
4319  curr_ctts = 0;
4320 
4321  if (tts_data_old && tts_index_old < tts_count_old) {
4322  curr_ctts = tts_data_old[tts_index_old].offset;
4323  av_log(mov->fc, AV_LOG_TRACE, "stts: %"PRId64" ctts: %"PRId64", tts_index: %"PRId64", tts_count: %"PRId64"\n",
4324  curr_cts, curr_ctts, tts_index_old, tts_count_old);
4325  curr_cts += curr_ctts;
4326  tts_sample_old++;
4327  if (tts_sample_old == tts_data_old[tts_index_old].count) {
4328  if (add_tts_entry(&msc->tts_data, &msc->tts_count,
4329  &msc->tts_allocated_size,
4330  tts_data_old[tts_index_old].count - edit_list_start_tts_sample,
4331  tts_data_old[tts_index_old].offset, tts_data_old[tts_index_old].duration) == -1) {
4332  av_log(mov->fc, AV_LOG_ERROR, "Cannot add Time To Sample entry %"PRId64" - {%"PRId64", %d}\n",
4333  tts_index_old,
4334  tts_data_old[tts_index_old].count - edit_list_start_tts_sample,
4335  tts_data_old[tts_index_old].offset);
4336  break;
4337  }
4338  tts_index_old++;
4339  tts_sample_old = 0;
4340  edit_list_start_tts_sample = 0;
4341  }
4342  }
4343 
4344  if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
4346  curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
4347  first_non_zero_audio_edit > 0) {
4348  packet_skip_samples = edit_list_media_time - curr_cts;
4349  sti->skip_samples += packet_skip_samples;
4350 
4351  // Shift the index entry timestamp by packet_skip_samples to be correct.
4352  edit_list_dts_counter -= packet_skip_samples;
4353  if (edit_list_start_encountered == 0) {
4354  edit_list_start_encountered = 1;
4355  // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
4356  // discarded packets.
4357  if (frame_duration_buffer) {
4358  fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
4359  frame_duration_buffer, num_discarded_begin);
4360  av_freep(&frame_duration_buffer);
4361  }
4362  }
4363 
4364  av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
4365  } else {
4367  av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
4368 
4369  if (edit_list_start_encountered == 0) {
4370  num_discarded_begin++;
4371  frame_duration_buffer = av_realloc(frame_duration_buffer,
4372  num_discarded_begin * sizeof(int64_t));
4373  if (!frame_duration_buffer) {
4374  av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
4375  break;
4376  }
4377  frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
4378 
4379  // Increment skip_samples for the first non-zero audio edit list
4380  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4381  first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
4382  sti->skip_samples += frame_duration;
4383  }
4384  }
4385  }
4386  } else {
4387  if (msc->min_corrected_pts < 0) {
4388  msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
4389  } else {
4390  msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
4391  }
4392  if (edit_list_start_encountered == 0) {
4393  edit_list_start_encountered = 1;
4394  // Make timestamps strictly monotonically increasing by rewriting timestamps for
4395  // discarded packets.
4396  if (frame_duration_buffer) {
4397  fix_index_entry_timestamps(st, sti->nb_index_entries, edit_list_dts_counter,
4398  frame_duration_buffer, num_discarded_begin);
4399  av_freep(&frame_duration_buffer);
4400  }
4401  }
4402  }
4403 
4404  if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
4405  current->min_distance, flags) == -1) {
4406  av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
4407  break;
4408  }
4409 
4410  // Update the index ranges array
4411  if (!current_index_range || index != current_index_range->end) {
4412  current_index_range = current_index_range ? current_index_range + 1
4413  : msc->index_ranges;
4414  current_index_range->start = index;
4415  }
4416  current_index_range->end = index + 1;
4417 
4418  // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
4419  if (edit_list_start_encountered > 0) {
4420  edit_list_dts_counter = edit_list_dts_counter + frame_duration;
4421  }
4422 
4423  // Break when found first key frame after edit entry completion
4424  if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
4426  if (msc->ctts_count) {
4427  // If we have CTTS and this is the first keyframe after edit elist,
4428  // wait for one more, because there might be trailing B-frames after this I-frame
4429  // that do belong to the edit.
4430  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
4431  found_keyframe_after_edit = 1;
4432  continue;
4433  }
4434  if (tts_sample_old != 0) {
4435  if (add_tts_entry(&msc->tts_data, &msc->tts_count,
4436  &msc->tts_allocated_size,
4437  tts_sample_old - edit_list_start_tts_sample,
4438  tts_data_old[tts_index_old].offset, tts_data_old[tts_index_old].duration) == -1) {
4439  av_log(mov->fc, AV_LOG_ERROR, "Cannot add Time To Sample entry %"PRId64" - {%"PRId64", %d}\n",
4440  tts_index_old, tts_sample_old - edit_list_start_tts_sample,
4441  tts_data_old[tts_index_old].offset);
4442  break;
4443  }
4444  }
4445  }
4446  break;
4447  }
4448  }
4449  }
4450  // If there are empty edits, then msc->min_corrected_pts might be positive
4451  // intentionally. So we subtract the sum duration of emtpy edits here.
4452  msc->min_corrected_pts -= empty_edits_sum_duration;
4453 
4454  // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
4455  // dts by that amount to make the first pts zero.
4456  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4457  if (msc->min_corrected_pts > 0) {
4458  av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
4459  for (int i = 0; i < sti->nb_index_entries; ++i)
4461  }
4462  }
4463  // Start time should be equal to zero or the duration of any empty edits.
4464  st->start_time = empty_edits_sum_duration;
4465 
4466  // Update av stream length, if it ends up shorter than the track's media duration
4467  st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
4468  msc->start_pad = sti->skip_samples;
4469 
4470  // Free the old index and the old CTTS structures
4471  av_free(e_old);
4472  av_free(tts_data_old);
4473  av_freep(&frame_duration_buffer);
4474 
4475  // Null terminate the index ranges array
4476  current_index_range = current_index_range ? current_index_range + 1
4477  : msc->index_ranges;
4478  current_index_range->start = 0;
4479  current_index_range->end = 0;
4480  msc->current_index = msc->index_ranges[0].start;
4481 }
4482 
4483 static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
4484 {
4485  for (uint32_t i = 0; i < sc->sgpd_sync_count; i++)
4486  if (sc->sgpd_sync[i] == HEVC_NAL_CRA_NUT)
4487  return i + 1;
4488  return 0;
4489 }
4490 
4492 {
4493  int k;
4494  int sample_id = 0;
4495  uint32_t cra_index;
4496  MOVStreamContext *sc = st->priv_data;
4497 
4498  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !sc->sync_group_count)
4499  return 0;
4500 
4501  /* Build an unrolled index of the samples */
4502  sc->sample_offsets_count = 0;
4503  for (uint32_t i = 0; i < sc->ctts_count; i++) {
4504  if (sc->ctts_data[i].count > INT_MAX - sc->sample_offsets_count)
4505  return AVERROR(ENOMEM);
4506  sc->sample_offsets_count += sc->ctts_data[i].count;
4507  }
4508  av_freep(&sc->sample_offsets);
4510  if (!sc->sample_offsets)
4511  return AVERROR(ENOMEM);
4512  k = 0;
4513  for (uint32_t i = 0; i < sc->ctts_count; i++)
4514  for (int j = 0; j < sc->ctts_data[i].count; j++)
4515  sc->sample_offsets[k++] = sc->ctts_data[i].offset;
4516 
4517  /* The following HEVC NAL type reveal the use of open GOP sync points
4518  * (TODO: BLA types may also be concerned) */
4519  cra_index = get_sgpd_sync_index(sc, HEVC_NAL_CRA_NUT); /* Clean Random Access */
4520  if (!cra_index)
4521  return 0;
4522 
4523  /* Build a list of open-GOP key samples */
4524  sc->open_key_samples_count = 0;
4525  for (uint32_t i = 0; i < sc->sync_group_count; i++)
4526  if (sc->sync_group[i].index == cra_index) {
4527  if (sc->sync_group[i].count > INT_MAX - sc->open_key_samples_count)
4528  return AVERROR(ENOMEM);
4530  }
4531  av_freep(&sc->open_key_samples);
4533  if (!sc->open_key_samples)
4534  return AVERROR(ENOMEM);
4535  k = 0;
4536  for (uint32_t i = 0; i < sc->sync_group_count; i++) {
4537  const MOVSbgp *sg = &sc->sync_group[i];
4538  if (sg->index == cra_index)
4539  for (uint32_t j = 0; j < sg->count; j++)
4540  sc->open_key_samples[k++] = sample_id;
4541  if (sg->count > INT_MAX - sample_id)
4542  return AVERROR_PATCHWELCOME;
4543  sample_id += sg->count;
4544  }
4545 
4546  /* Identify the minimal time step between samples */
4547  sc->min_sample_duration = UINT_MAX;
4548  for (uint32_t i = 0; i < sc->stts_count; i++)
4550 
4551  return 0;
4552 }
4553 
4554 #define MOV_MERGE_CTTS 1
4555 #define MOV_MERGE_STTS 2
4556 /*
4557  * Merge stts and ctts arrays into a new combined array.
4558  * stts_count and ctts_count may be left untouched as they will be
4559  * used to check for the presence of either of them.
4560  */
4561 static int mov_merge_tts_data(MOVContext *mov, AVStream *st, int flags)
4562 {
4563  MOVStreamContext *sc = st->priv_data;
4564  int ctts = sc->ctts_data && (flags & MOV_MERGE_CTTS);
4565  int stts = sc->stts_data && (flags & MOV_MERGE_STTS);
4566  int idx = 0;
4567 
4568  if (!sc->ctts_data && !sc->stts_data)
4569  return 0;
4570  // Expand time to sample entries such that we have a 1-1 mapping with samples
4571  if (!sc->sample_count || sc->sample_count >= UINT_MAX / sizeof(*sc->tts_data))
4572  return -1;
4573 
4574  if (ctts) {
4576  sc->sample_count * sizeof(*sc->tts_data));
4577  if (!sc->tts_data)
4578  return -1;
4579 
4580  memset(sc->tts_data, 0, sc->tts_allocated_size);
4581 
4582  for (int i = 0; i < sc->ctts_count &&
4583  idx < sc->sample_count; i++)
4584  for (int j = 0; j < sc->ctts_data[i].count &&
4585  idx < sc->sample_count; j++) {
4586  sc->tts_data[idx].offset = sc->ctts_data[i].offset;
4587  sc->tts_data[idx++].count = 1;
4588  }
4589 
4590  sc->tts_count = idx;
4591  } else
4592  sc->ctts_count = 0;
4593  av_freep(&sc->ctts_data);
4594  sc->ctts_allocated_size = 0;
4595 
4596  idx = 0;
4597  if (stts) {
4599  sc->sample_count * sizeof(*sc->tts_data));
4600  if (!tts_data)
4601  return -1;
4602 
4603  if (!sc->tts_data)
4604  memset(tts_data, 0, sc->tts_allocated_size);
4605  sc->tts_data = tts_data;
4606 
4607  for (int i = 0; i < sc->stts_count &&
4608  idx < sc->sample_count; i++)
4609  for (int j = 0; j < sc->stts_data[i].count &&
4610  idx < sc->sample_count; j++) {
4611  sc->tts_data[idx].duration = sc->stts_data[i].duration;
4612  sc->tts_data[idx++].count = 1;
4613  }
4614 
4615  sc->tts_count = FFMAX(sc->tts_count, idx);
4616  } else
4617  sc->stts_count = 0;
4618  av_freep(&sc->stts_data);
4619  sc->stts_allocated_size = 0;
4620 
4621  return 0;
4622 }
4623 
4624 static void mov_build_index(MOVContext *mov, AVStream *st)
4625 {
4626  MOVStreamContext *sc = st->priv_data;
4627  FFStream *const sti = ffstream(st);
4628  int64_t current_offset;
4629  int64_t current_dts = 0;
4630  unsigned int stts_index = 0;
4631  unsigned int stsc_index = 0;
4632  unsigned int stss_index = 0;
4633  unsigned int stps_index = 0;
4634  unsigned int i, j;
4635  uint64_t stream_size = 0;
4636 
4637  int ret = build_open_gop_key_points(st);
4638  if (ret < 0)
4639  return;
4640 
4641  if (sc->elst_count) {
4642  int i, edit_start_index = 0, multiple_edits = 0;
4643  int64_t empty_duration = 0; // empty duration of the first edit list entry
4644  int64_t start_time = 0; // start time of the media
4645 
4646  for (i = 0; i < sc->elst_count; i++) {
4647  const MOVElst *e = &sc->elst_data[i];
4648  if (i == 0 && e->time == -1) {
4649  /* if empty, the first entry is the start time of the stream
4650  * relative to the presentation itself */
4651  empty_duration = e->duration;
4652  edit_start_index = 1;
4653  } else if (i == edit_start_index && e->time >= 0) {
4654  start_time = e->time;
4655  } else {
4656  multiple_edits = 1;
4657  }
4658  }
4659 
4660  if (multiple_edits && !mov->advanced_editlist) {
4662  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
4663  "not supported in fragmented MP4 files\n");
4664  else
4665  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
4666  "Use -advanced_editlist to correctly decode otherwise "
4667  "a/v desync might occur\n");
4668  }
4669 
4670  /* adjust first dts according to edit list */
4671  if ((empty_duration || start_time) && mov->time_scale > 0) {
4672  if (empty_duration)
4673  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
4674 
4675  if (av_sat_sub64(start_time, empty_duration) != start_time - (uint64_t)empty_duration)
4676  av_log(mov->fc, AV_LOG_WARNING, "start_time - empty_duration is not representable\n");
4677 
4678  sc->time_offset = start_time - (uint64_t)empty_duration;
4680  if (!mov->advanced_editlist)
4681  current_dts = -sc->time_offset;
4682  }
4683 
4684  if (!multiple_edits && !mov->advanced_editlist &&
4686  sc->start_pad = start_time;
4687  }
4688 
4689  /* only use old uncompressed audio chunk demuxing when stts specifies it */
4690  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4691  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
4692  unsigned int current_sample = 0;
4693  unsigned int stts_sample = 0;
4694  unsigned int sample_size;
4695  unsigned int distance = 0;
4696  unsigned int rap_group_index = 0;
4697  unsigned int rap_group_sample = 0;
4698  int rap_group_present = sc->rap_group_count && sc->rap_group;
4699  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
4700 
4701  current_dts -= sc->dts_shift;
4702 
4703  if (!sc->sample_count || sti->nb_index_entries)
4704  return;
4705  if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
4706  return;
4707  if (av_reallocp_array(&sti->index_entries,
4708  sti->nb_index_entries + sc->sample_count,
4709  sizeof(*sti->index_entries)) < 0) {
4710  sti->nb_index_entries = 0;
4711  return;
4712  }
4713  sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
4714 
4716  if (ret < 0)
4717  return;
4718 
4719  for (i = 0; i < sc->chunk_count; i++) {
4720  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
4721  current_offset = sc->chunk_offsets[i];
4722  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4723  i + 1 == sc->stsc_data[stsc_index + 1].first)
4724  stsc_index++;
4725 
4726  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
4727  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
4728  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
4729  sc->stsz_sample_size = sc->sample_size;
4730  }
4731  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
4732  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
4733  sc->stsz_sample_size = sc->sample_size;
4734  }
4735 
4736  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
4737  int keyframe = 0;
4738  if (current_sample >= sc->sample_count) {
4739  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
4740  return;
4741  }
4742 
4743  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
4744  keyframe = 1;
4745  if (stss_index + 1 < sc->keyframe_count)
4746  stss_index++;
4747  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
4748  keyframe = 1;
4749  if (stps_index + 1 < sc->stps_count)
4750  stps_index++;
4751  }
4752  if (rap_group_present && rap_group_index < sc->rap_group_count) {
4753  if (sc->rap_group[rap_group_index].index > 0)
4754  keyframe = 1;
4755  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
4756  rap_group_sample = 0;
4757  rap_group_index++;
4758  }
4759  }
4760  if (sc->keyframe_absent
4761  && !sc->stps_count
4762  && !rap_group_present
4763  && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
4764  keyframe = 1;
4765  if (keyframe)
4766  distance = 0;
4767  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
4768  if (current_offset > INT64_MAX - sample_size) {
4769  av_log(mov->fc, AV_LOG_ERROR, "Current offset %"PRId64" or sample size %u is too large\n",
4770  current_offset,
4771  sample_size);
4772  return;
4773  }
4774 
4775  if (sc->pseudo_stream_id == -1 ||
4776  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
4777  AVIndexEntry *e;
4778  if (sample_size > 0x3FFFFFFF) {
4779  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
4780  return;
4781  }
4782  e = &sti->index_entries[sti->nb_index_entries++];
4783  e->pos = current_offset;
4784  e->timestamp = current_dts;
4785  e->size = sample_size;
4786  e->min_distance = distance;
4787  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
4788  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
4789  "size %u, distance %u, keyframe %d\n", st->index, current_sample,
4790  current_offset, current_dts, sample_size, distance, keyframe);
4791  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->nb_index_entries < 100)
4792  ff_rfps_add_frame(mov->fc, st, current_dts);
4793  }
4794 
4795  current_offset += sample_size;
4796  stream_size += sample_size;
4797 
4798  current_dts += sc->tts_data[stts_index].duration;
4799 
4800  distance++;
4801  stts_sample++;
4802  current_sample++;
4803  if (stts_index + 1 < sc->tts_count && stts_sample == sc->tts_data[stts_index].count) {
4804  stts_sample = 0;
4805  stts_index++;
4806  }
4807  }
4808  }
4809  if (st->duration > 0)
4810  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
4811  } else {
4812  unsigned chunk_samples, total = 0;
4813 
4815  if (ret < 0)
4816  return;
4817 
4818  if (!sc->chunk_count)
4819  return;
4820 
4821  // compute total chunk count
4822  for (i = 0; i < sc->stsc_count; i++) {
4823  unsigned count, chunk_count;
4824 
4825  chunk_samples = sc->stsc_data[i].count;
4826  if (i != sc->stsc_count - 1 &&
4827  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
4828  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
4829  return;
4830  }
4831 
4832  if (sc->samples_per_frame >= 160) { // gsm
4833  count = chunk_samples / sc->samples_per_frame;
4834  } else if (sc->samples_per_frame > 1) {
4835  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
4836  count = (chunk_samples+samples-1) / samples;
4837  } else {
4838  count = (chunk_samples+1023) / 1024;
4839  }
4840 
4841  if (mov_stsc_index_valid(i, sc->stsc_count))
4842  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
4843  else
4844  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
4845  total += chunk_count * count;
4846  }
4847 
4848  av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
4849  if (total >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
4850  return;
4851  if (av_reallocp_array(&sti->index_entries,
4852  sti->nb_index_entries + total,
4853  sizeof(*sti->index_entries)) < 0) {
4854  sti->nb_index_entries = 0;
4855  return;
4856  }
4857  sti->index_entries_allocated_size = (sti->nb_index_entries + total) * sizeof(*sti->index_entries);
4858 
4859  // populate index
4860  for (i = 0; i < sc->chunk_count; i++) {
4861  current_offset = sc->chunk_offsets[i];
4862  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4863  i + 1 == sc->stsc_data[stsc_index + 1].first)
4864  stsc_index++;
4865  chunk_samples = sc->stsc_data[stsc_index].count;
4866 
4867  while (chunk_samples > 0) {
4868  AVIndexEntry *e;
4869  unsigned size, samples;
4870 
4871  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
4873  "Zero bytes per frame, but %d samples per frame",
4874  sc->samples_per_frame);
4875  return;
4876  }
4877 
4878  if (sc->samples_per_frame >= 160) { // gsm
4879  samples = sc->samples_per_frame;
4880  size = sc->bytes_per_frame;
4881  } else {
4882  if (sc->samples_per_frame > 1) {
4883  samples = FFMIN((1024 / sc->samples_per_frame)*
4884  sc->samples_per_frame, chunk_samples);
4886  } else {
4887  samples = FFMIN(1024, chunk_samples);
4888  size = samples * sc->sample_size;
4889  }
4890  }
4891 
4892  if (sti->nb_index_entries >= total) {
4893  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
4894  return;
4895  }
4896  if (size > 0x3FFFFFFF) {
4897  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
4898  return;
4899  }
4900  e = &sti->index_entries[sti->nb_index_entries++];
4901  e->pos = current_offset;
4902  e->timestamp = current_dts;
4903  e->size = size;
4904  e->min_distance = 0;
4905  e->flags = AVINDEX_KEYFRAME;
4906  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
4907  "size %u, duration %u\n", st->index, i, current_offset, current_dts,
4908  size, samples);
4909 
4910  current_offset += size;
4911  current_dts += samples;
4912  chunk_samples -= samples;
4913  }
4914  }
4915  }
4916 
4917  if (!mov->ignore_editlist && mov->advanced_editlist) {
4918  // Fix index according to edit lists.
4919  mov_fix_index(mov, st);
4920  }
4921 
4922  // Update start time of the stream.
4924  st->start_time = sti->index_entries[0].timestamp + sc->dts_shift;
4925  if (sc->tts_data) {
4926  st->start_time += sc->tts_data[0].offset;
4927  }
4928  }
4929 
4930  mov_estimate_video_delay(mov, st);
4931 }
4932 
4933 static int test_same_origin(const char *src, const char *ref) {
4934  char src_proto[64];
4935  char ref_proto[64];
4936  char src_auth[256];
4937  char ref_auth[256];
4938  char src_host[256];
4939  char ref_host[256];
4940  int src_port=-1;
4941  int ref_port=-1;
4942 
4943  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
4944  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
4945 
4946  if (strlen(src) == 0) {
4947  return -1;
4948  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
4949  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
4950  strlen(src_host) + 1 >= sizeof(src_host) ||
4951  strlen(ref_host) + 1 >= sizeof(ref_host)) {
4952  return 0;
4953  } else if (strcmp(src_proto, ref_proto) ||
4954  strcmp(src_auth, ref_auth) ||
4955  strcmp(src_host, ref_host) ||
4956  src_port != ref_port) {
4957  return 0;
4958  } else
4959  return 1;
4960 }
4961 
4962 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
4963 {
4964  /* try relative path, we do not try the absolute because it can leak information about our
4965  system to an attacker */
4966  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
4967  char filename[1025];
4968  const char *src_path;
4969  int i, l;
4970 
4971  /* find a source dir */
4972  src_path = strrchr(src, '/');
4973  if (src_path)
4974  src_path++;
4975  else
4976  src_path = src;
4977 
4978  /* find a next level down to target */
4979  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
4980  if (ref->path[l] == '/') {
4981  if (i == ref->nlvl_to - 1)
4982  break;
4983  else
4984  i++;
4985  }
4986 
4987  /* compose filename if next level down to target was found */
4988  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
4989  memcpy(filename, src, src_path - src);
4990  filename[src_path - src] = 0;
4991 
4992  for (i = 1; i < ref->nlvl_from; i++)
4993  av_strlcat(filename, "../", sizeof(filename));
4994 
4995  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
4996  if (!c->use_absolute_path) {
4997  int same_origin = test_same_origin(src, filename);
4998 
4999  if (!same_origin) {
5000  av_log(c->fc, AV_LOG_ERROR,
5001  "Reference with mismatching origin, %s not tried for security reasons, "
5002  "set demuxer option use_absolute_path to allow it anyway\n",
5003  ref->path);
5004  return AVERROR(ENOENT);
5005  }
5006 
5007  if (strstr(ref->path + l + 1, "..") ||
5008  strstr(ref->path + l + 1, ":") ||
5009  (ref->nlvl_from > 1 && same_origin < 0) ||
5010  (filename[0] == '/' && src_path == src))
5011  return AVERROR(ENOENT);
5012  }
5013 
5014  if (strlen(filename) + 1 == sizeof(filename))
5015  return AVERROR(ENOENT);
5016  if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
5017  return 0;
5018  }
5019  } else if (c->use_absolute_path) {
5020  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
5021  "this is a possible security issue\n");
5022  if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
5023  return 0;
5024  } else {
5025  av_log(c->fc, AV_LOG_ERROR,
5026  "Absolute path %s not tried for security reasons, "
5027  "set demuxer option use_absolute_path to allow absolute paths\n",
5028  ref->path);
5029  }
5030 
5031  return AVERROR(ENOENT);
5032 }
5033 
5035 {
5036  if (sc->time_scale <= 0) {
5037  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
5038  sc->time_scale = c->time_scale;
5039  if (sc->time_scale <= 0)
5040  sc->time_scale = 1;
5041  }
5042 }
5043 
5044 #if CONFIG_IAMFDEC
5045 static int mov_update_iamf_streams(MOVContext *c, const AVStream *st)
5046 {
5047  const MOVStreamContext *sc = st->priv_data;
5048  const IAMFContext *iamf = &sc->iamf->iamf;
5049 
5050  for (int i = 0; i < iamf->nb_audio_elements; i++) {
5051  const AVStreamGroup *stg = NULL;
5052 
5053  for (int j = 0; j < c->fc->nb_stream_groups; j++)
5054  if (c->fc->stream_groups[j]->id == iamf->audio_elements[i]->audio_element_id)
5055  stg = c->fc->stream_groups[j];
5056  av_assert0(stg);
5057 
5058  for (int j = 0; j < stg->nb_streams; j++) {
5059  const FFStream *sti = cffstream(st);
5060  AVStream *out = stg->streams[j];
5061  FFStream *out_sti = ffstream(stg->streams[j]);
5062 
5063  out->codecpar->bit_rate = 0;
5064 
5065  if (out == st)
5066  continue;
5067 
5068  out->time_base = st->time_base;
5069  out->start_time = st->start_time;
5070  out->duration = st->duration;
5071  out->nb_frames = st->nb_frames;
5072  out->discard = st->discard;
5073 
5074  av_assert0(!out_sti->index_entries);
5076  if (!out_sti->index_entries)
5077  return AVERROR(ENOMEM);
5078 
5080  out_sti->nb_index_entries = sti->nb_index_entries;
5081  out_sti->skip_samples = sti->skip_samples;
5082  memcpy(out_sti->index_entries, sti->index_entries, sti->index_entries_allocated_size);
5083  }
5084  }
5085 
5086  return 0;
5087 }
5088 #endif
5089 
5090 static int sanity_checks(void *log_obj, MOVStreamContext *sc, int index)
5091 {
5092  if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
5093  (!sc->sample_size && !sc->sample_count))) ||
5094  (!sc->chunk_count && sc->sample_count)) {
5095  av_log(log_obj, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
5096  index);
5097  return 1;
5098  }
5099 
5100  if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
5101  av_log(log_obj, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
5102  index);
5103  return 2;
5104  }
5105  return 0;
5106 }
5107 
5109 {
5110  AVStream *st;
5111  MOVStreamContext *sc;
5112  int ret;
5113 
5114  st = avformat_new_stream(c->fc, NULL);
5115  if (!st) return AVERROR(ENOMEM);
5116  st->id = -1;
5117  sc = av_mallocz(sizeof(MOVStreamContext));
5118  if (!sc) return AVERROR(ENOMEM);
5119 
5120  st->priv_data = sc;
5122  sc->ffindex = st->index;
5123  c->trak_index = st->index;
5124  sc->tref_flags = 0;
5125  sc->tref_id = -1;
5126  sc->refcount = 1;
5127 
5128  if ((ret = mov_read_default(c, pb, atom)) < 0)
5129  return ret;
5130 
5131  c->trak_index = -1;
5132 
5133  // Here stsc refers to a chunk not described in stco. This is technically invalid,
5134  // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
5135  if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
5136  sc->stsc_count = 0;
5137  av_freep(&sc->stsc_data);
5138  }
5139 
5140  ret = sanity_checks(c->fc, sc, st->index);
5141  if (ret)
5142  return ret > 1 ? AVERROR_INVALIDDATA : 0;
5143 
5144  fix_timescale(c, sc);
5145 
5146  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
5147 
5148  /*
5149  * Advanced edit list support does not work with fragemented MP4s, which
5150  * have stsc, stsz, stco, and stts with zero entries in the moov atom.
5151  * In these files, trun atoms may be streamed in.
5152  */
5153  if (!sc->stts_count && c->advanced_editlist) {
5154 
5155  av_log(c->fc, AV_LOG_VERBOSE, "advanced_editlist does not work with fragmented "
5156  "MP4. disabling.\n");
5157  c->advanced_editlist = 0;
5158  c->advanced_editlist_autodisabled = 1;
5159  }
5160 
5161  mov_build_index(c, st);
5162 
5163 #if CONFIG_IAMFDEC
5164  if (sc->iamf) {
5165  ret = mov_update_iamf_streams(c, st);
5166  if (ret < 0)
5167  return ret;
5168  }
5169 #endif
5170 
5171  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
5172  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
5173  if (c->enable_drefs) {
5174  if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
5175  av_log(c->fc, AV_LOG_ERROR,
5176  "stream %d, error opening alias: path='%s', dir='%s', "
5177  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
5178  st->index, dref->path, dref->dir, dref->filename,
5179  dref->volume, dref->nlvl_from, dref->nlvl_to);
5180  } else {
5181  av_log(c->fc, AV_LOG_WARNING,
5182  "Skipped opening external track: "
5183  "stream %d, alias: path='%s', dir='%s', "
5184  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
5185  "Set enable_drefs to allow this.\n",
5186  st->index, dref->path, dref->dir, dref->filename,
5187  dref->volume, dref->nlvl_from, dref->nlvl_to);
5188  }
5189  } else {
5190  sc->pb = c->fc->pb;
5191  sc->pb_is_copied = 1;
5192  }
5193 
5194  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
5195  int stts_constant = sc->stts_count && sc->tts_count;
5196  if (sc->h_spacing && sc->v_spacing)
5198  sc->h_spacing, sc->v_spacing, INT_MAX);
5199  if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
5200  sc->height && sc->width &&
5201  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
5203  (int64_t)st->codecpar->height * sc->width,
5204  (int64_t)st->codecpar->width * sc->height, INT_MAX);
5205  }
5206 
5207 #if FF_API_R_FRAME_RATE
5208  for (unsigned int i = 1; sc->stts_count && i + 1 < sc->tts_count; i++) {
5209  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5210  continue;
5211  stts_constant = 0;
5212  }
5213  if (stts_constant)
5215  sc->time_scale, sc->tts_data[0].duration, INT_MAX);
5216 #endif
5217  }
5218 
5219  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
5220  if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
5221  TAG_IS_AVCI(st->codecpar->codec_tag)) {
5223  if (ret < 0)
5224  return ret;
5225  }
5226 
5227  switch (st->codecpar->codec_id) {
5228 #if CONFIG_H261_DECODER
5229  case AV_CODEC_ID_H261:
5230 #endif
5231 #if CONFIG_H263_DECODER
5232  case AV_CODEC_ID_H263:
5233 #endif
5234 #if CONFIG_MPEG4_DECODER
5235  case AV_CODEC_ID_MPEG4:
5236 #endif
5237  st->codecpar->width = 0; /* let decoder init width/height */
5238  st->codecpar->height= 0;
5239  break;
5240  }
5241 
5242  // If the duration of the mp3 packets is not constant, then they could need a parser
5243  if (st->codecpar->codec_id == AV_CODEC_ID_MP3
5244  && sc->time_scale == st->codecpar->sample_rate) {
5245  int stts_constant = 1;
5246  for (int i = 1; sc->stts_count && i < sc->tts_count; i++) {
5247  if (sc->tts_data[i].duration == sc->tts_data[0].duration)
5248  continue;
5249  stts_constant = 0;
5250  }
5251  if (!stts_constant)
5253  }
5254  /* Do not need those anymore. */
5255  av_freep(&sc->chunk_offsets);
5256  av_freep(&sc->sample_sizes);
5257  av_freep(&sc->keyframes);
5258  av_freep(&sc->stps_data);
5259  av_freep(&sc->elst_data);
5260  av_freep(&sc->rap_group);
5261  av_freep(&sc->sync_group);
5262  av_freep(&sc->sgpd_sync);
5263 
5264  return 0;
5265 }
5266 
5268 {
5269  int ret;
5270  c->itunes_metadata = 1;
5271  ret = mov_read_default(c, pb, atom);
5272  c->itunes_metadata = 0;
5273  return ret;
5274 }
5275 
5277 {
5278  uint32_t count;
5279  uint32_t i;
5280 
5281  if (atom.size < 8)
5282  return 0;
5283 
5284  avio_skip(pb, 4);
5285  count = avio_rb32(pb);
5286  atom.size -= 8;
5287  if (count >= UINT_MAX / sizeof(*c->meta_keys)) {
5288  av_log(c->fc, AV_LOG_ERROR,
5289  "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
5290  return AVERROR_INVALIDDATA;
5291  }
5292 
5293  c->meta_keys_count = count + 1;
5294  c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
5295  if (!c->meta_keys)
5296  return AVERROR(ENOMEM);
5297 
5298  for (i = 1; i <= count; ++i) {
5299  uint32_t key_size = avio_rb32(pb);
5300  uint32_t type = avio_rl32(pb);
5301  if (key_size < 8 || key_size > atom.size) {
5302  av_log(c->fc, AV_LOG_ERROR,
5303  "The key# %"PRIu32" in meta has invalid size:"
5304  "%"PRIu32"\n", i, key_size);
5305  return AVERROR_INVALIDDATA;
5306  }
5307  atom.size -= key_size;
5308  key_size -= 8;
5309  if (type != MKTAG('m','d','t','a')) {
5310  avio_skip(pb, key_size);
5311  continue;
5312  }
5313  c->meta_keys[i] = av_mallocz(key_size + 1);
5314  if (!c->meta_keys[i])
5315  return AVERROR(ENOMEM);
5316  avio_read(pb, c->meta_keys[i], key_size);
5317  }
5318 
5319  return 0;
5320 }
5321 
5323 {
5324  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
5325  uint8_t *key = NULL, *val = NULL, *mean = NULL;
5326  int i;
5327  int ret = 0;
5328  AVStream *st;
5329  MOVStreamContext *sc;
5330 
5331  if (c->fc->nb_streams < 1)
5332  return 0;
5333  st = c->fc->streams[c->fc->nb_streams-1];
5334  sc = st->priv_data;
5335 
5336  for (i = 0; i < 3; i++) {
5337  uint8_t **p;
5338  uint32_t len, tag;
5339 
5340  if (end - avio_tell(pb) <= 12)
5341  break;
5342 
5343  len = avio_rb32(pb);
5344  tag = avio_rl32(pb);
5345  avio_skip(pb, 4); // flags
5346 
5347  if (len < 12 || len - 12 > end - avio_tell(pb))
5348  break;
5349  len -= 12;
5350 
5351  if (tag == MKTAG('m', 'e', 'a', 'n'))
5352  p = &mean;
5353  else if (tag == MKTAG('n', 'a', 'm', 'e'))
5354  p = &key;
5355  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
5356  avio_skip(pb, 4);
5357  len -= 4;
5358  p = &val;
5359  } else
5360  break;
5361 
5362  if (*p)
5363  break;
5364 
5365  *p = av_malloc(len + 1);
5366  if (!*p) {
5367  ret = AVERROR(ENOMEM);
5368  break;
5369  }
5370  ret = ffio_read_size(pb, *p, len);
5371  if (ret < 0) {
5372  av_freep(p);
5373  break;
5374  }
5375  (*p)[len] = 0;
5376  }
5377 
5378  if (mean && key && val) {
5379  if (strcmp(key, "iTunSMPB") == 0) {
5380  int priming, remainder, samples;
5381  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
5382  if(priming>0 && priming<16384)
5383  sc->start_pad = priming;
5384  }
5385  }
5386  if (strcmp(key, "cdec") != 0) {
5387  av_dict_set(&c->fc->metadata, key, val,
5389  key = val = NULL;
5390  }
5391  } else {
5392  av_log(c->fc, AV_LOG_VERBOSE,
5393  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
5394  }
5395 
5396  avio_seek(pb, end, SEEK_SET);
5397  av_freep(&key);
5398  av_freep(&val);
5399  av_freep(&mean);
5400  return ret;
5401 }
5402 
5404 {
5405  MOVStreamContext *sc;
5406  AVStream *st;
5407 
5408  st = avformat_new_stream(c->fc, NULL);
5409  if (!st)
5410  return AVERROR(ENOMEM);
5411  sc = av_mallocz(sizeof(MOVStreamContext));
5412  if (!sc)
5413  return AVERROR(ENOMEM);
5414 
5415  item->st = st;
5416  st->id = item->item_id;
5417  st->priv_data = sc;
5419  st->codecpar->codec_id = mov_codec_id(st, item->type);
5420  sc->id = st->id;
5421  sc->ffindex = st->index;
5422  st->avg_frame_rate.num = st->avg_frame_rate.den = 1;
5423  st->time_base.num = st->time_base.den = 1;
5424  st->nb_frames = 1;
5425  sc->time_scale = 1;
5426  sc->pb = c->fc->pb;
5427  sc->pb_is_copied = 1;
5428  sc->refcount = 1;
5429 
5430  if (item->name)
5431  av_dict_set(&st->metadata, "title", item->name, 0);
5432 
5433  // Populate the necessary fields used by mov_build_index.
5434  sc->stsc_count = 1;
5435  sc->stsc_data = av_malloc_array(1, sizeof(*sc->stsc_data));
5436  if (!sc->stsc_data)
5437  return AVERROR(ENOMEM);
5438  sc->stsc_data[0].first = 1;
5439  sc->stsc_data[0].count = 1;
5440  sc->stsc_data[0].id = 1;
5441  sc->chunk_count = 1;
5442  sc->chunk_offsets = av_malloc_array(1, sizeof(*sc->chunk_offsets));
5443  if (!sc->chunk_offsets)
5444  return AVERROR(ENOMEM);
5445  sc->sample_count = 1;
5446  sc->sample_sizes = av_malloc_array(1, sizeof(*sc->sample_sizes));
5447  if (!sc->sample_sizes)
5448  return AVERROR(ENOMEM);
5449  sc->stts_count = 1;
5450  sc->stts_data = av_malloc_array(1, sizeof(*sc->stts_data));
5451  if (!sc->stts_data)
5452  return AVERROR(ENOMEM);
5453  sc->stts_data[0].count = 1;
5454  // Not used for still images. But needed by mov_build_index.
5455  sc->stts_data[0].duration = 0;
5456 
5457  return 0;
5458 }
5459 
5461 {
5462  while (atom.size > 8) {
5463  uint32_t tag;
5464  if (avio_feof(pb))
5465  return AVERROR_EOF;
5466  tag = avio_rl32(pb);
5467  atom.size -= 4;
5468  if (tag == MKTAG('h','d','l','r')) {
5469  avio_seek(pb, -8, SEEK_CUR);
5470  atom.size += 8;
5471  return mov_read_default(c, pb, atom);
5472  }
5473  }
5474  return 0;
5475 }
5476 
5477 // return 1 when matrix is identity, 0 otherwise
5478 #define IS_MATRIX_IDENT(matrix) \
5479  ( (matrix)[0][0] == (1 << 16) && \
5480  (matrix)[1][1] == (1 << 16) && \
5481  (matrix)[2][2] == (1 << 30) && \
5482  !(matrix)[0][1] && !(matrix)[0][2] && \
5483  !(matrix)[1][0] && !(matrix)[1][2] && \
5484  !(matrix)[2][0] && !(matrix)[2][1])
5485 
5487 {
5488  int i, j, e;
5489  int width;
5490  int height;
5491  int display_matrix[3][3];
5492  int res_display_matrix[3][3] = { { 0 } };
5493  AVStream *st;
5494  MOVStreamContext *sc;
5495  int version;
5496  int flags;
5497 
5498  if (c->fc->nb_streams < 1)
5499  return 0;
5500  st = c->fc->streams[c->fc->nb_streams-1];
5501  sc = st->priv_data;
5502 
5503  // Each stream (trak) should have exactly 1 tkhd. This catches bad files and
5504  // avoids corrupting AVStreams mapped to an earlier tkhd.
5505  if (st->id != -1)
5506  return AVERROR_INVALIDDATA;
5507 
5508  version = avio_r8(pb);
5509  flags = avio_rb24(pb);
5511 
5512  if (version == 1) {
5513  avio_rb64(pb);
5514  avio_rb64(pb);
5515  } else {
5516  avio_rb32(pb); /* creation time */
5517  avio_rb32(pb); /* modification time */
5518  }
5519  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
5520  sc->id = st->id;
5521  avio_rb32(pb); /* reserved */
5522 
5523  /* highlevel (considering edits) duration in movie timebase */
5524  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
5525  avio_rb32(pb); /* reserved */
5526  avio_rb32(pb); /* reserved */
5527 
5528  avio_rb16(pb); /* layer */
5529  avio_rb16(pb); /* alternate group */
5530  avio_rb16(pb); /* volume */
5531  avio_rb16(pb); /* reserved */
5532 
5533  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
5534  // they're kept in fixed point format through all calculations
5535  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
5536  // side data, but the scale factor is not needed to calculate aspect ratio
5537  for (i = 0; i < 3; i++) {
5538  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
5539  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
5540  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
5541  }
5542 
5543  width = avio_rb32(pb); // 16.16 fixed point track width
5544  height = avio_rb32(pb); // 16.16 fixed point track height
5545  sc->width = width >> 16;
5546  sc->height = height >> 16;
5547 
5548  // apply the moov display matrix (after the tkhd one)
5549  for (i = 0; i < 3; i++) {
5550  const int sh[3] = { 16, 16, 30 };
5551  for (j = 0; j < 3; j++) {
5552  for (e = 0; e < 3; e++) {
5553  res_display_matrix[i][j] +=
5554  ((int64_t) display_matrix[i][e] *
5555  c->movie_display_matrix[e][j]) >> sh[e];
5556  }
5557  }
5558  }
5559 
5560  // save the matrix when it is not the default identity
5561  if (!IS_MATRIX_IDENT(res_display_matrix)) {
5562  av_freep(&sc->display_matrix);
5563  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
5564  if (!sc->display_matrix)
5565  return AVERROR(ENOMEM);
5566 
5567  for (i = 0; i < 3; i++)
5568  for (j = 0; j < 3; j++)
5569  sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
5570  }
5571 
5572  // transform the display width/height according to the matrix
5573  // to keep the same scale, use [width height 1<<16]
5574  if (width && height && sc->display_matrix) {
5575  double disp_transform[2];
5576 
5577  for (i = 0; i < 2; i++)
5578  disp_transform[i] = hypot(sc->display_matrix[0 + i],
5579  sc->display_matrix[3 + i]);
5580 
5581  if (disp_transform[0] > 1 && disp_transform[1] > 1 &&
5582  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
5583  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
5585  disp_transform[0] / disp_transform[1],
5586  INT_MAX);
5587  }
5588  return 0;
5589 }
5590 
5592 {
5593  MOVFragment *frag = &c->fragment;
5594  MOVTrackExt *trex = NULL;
5595  int flags, track_id, i;
5596  MOVFragmentStreamInfo * frag_stream_info;
5597 
5598  avio_r8(pb); /* version */
5599  flags = avio_rb24(pb);
5600 
5601  track_id = avio_rb32(pb);
5602  if (!track_id)
5603  return AVERROR_INVALIDDATA;
5604  for (i = 0; i < c->trex_count; i++)
5605  if (c->trex_data[i].track_id == track_id) {
5606  trex = &c->trex_data[i];
5607  break;
5608  }
5609  if (!trex) {
5610  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding trex (id %u)\n", track_id);
5611  return 0;
5612  }
5613  c->fragment.found_tfhd = 1;
5614  frag->track_id = track_id;
5615  set_frag_stream(&c->frag_index, track_id);
5616 
5619  frag->moof_offset : frag->implicit_offset;
5620  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
5621 
5623  avio_rb32(pb) : trex->duration;
5624  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
5625  avio_rb32(pb) : trex->size;
5626  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
5627  avio_rb32(pb) : trex->flags;
5628  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
5629 
5630  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5631  if (frag_stream_info) {
5632  frag_stream_info->next_trun_dts = AV_NOPTS_VALUE;
5633  frag_stream_info->stsd_id = frag->stsd_id;
5634  }
5635  return 0;
5636 }
5637 
5639 {
5640  unsigned i, num;
5641  void *new_tracks;
5642 
5643  num = atom.size / 4;
5644  if (!(new_tracks = av_malloc_array(num, sizeof(int))))
5645  return AVERROR(ENOMEM);
5646 
5647  av_free(c->chapter_tracks);
5648  c->chapter_tracks = new_tracks;
5649  c->nb_chapter_tracks = num;
5650 
5651  for (i = 0; i < num && !pb->eof_reached; i++)
5652  c->chapter_tracks[i] = avio_rb32(pb);
5653 
5654  c->nb_chapter_tracks = i;
5655 
5656  return 0;
5657 }
5658 
5660 {
5661  MOVTrackExt *trex;
5662  int err;
5663 
5664  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
5665  return AVERROR_INVALIDDATA;
5666  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
5667  sizeof(*c->trex_data))) < 0) {
5668  c->trex_count = 0;
5669  return err;
5670  }
5671 
5672  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
5673 
5674  trex = &c->trex_data[c->trex_count++];
5675  avio_r8(pb); /* version */
5676  avio_rb24(pb); /* flags */
5677  trex->track_id = avio_rb32(pb);
5678  trex->stsd_id = avio_rb32(pb);
5679  trex->duration = avio_rb32(pb);
5680  trex->size = avio_rb32(pb);
5681  trex->flags = avio_rb32(pb);
5682  return 0;
5683 }
5684 
5686 {
5687  MOVFragment *frag = &c->fragment;
5688  AVStream *st = NULL;
5689  MOVStreamContext *sc;
5690  int version, i;
5691  MOVFragmentStreamInfo * frag_stream_info;
5692  int64_t base_media_decode_time;
5693 
5694  for (i = 0; i < c->fc->nb_streams; i++) {
5695  sc = c->fc->streams[i]->priv_data;
5696  if (sc->id == frag->track_id) {
5697  st = c->fc->streams[i];
5698  break;
5699  }
5700  }
5701  if (!st) {
5702  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5703  return 0;
5704  }
5705  sc = st->priv_data;
5706  if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5707  return 0;
5708  version = avio_r8(pb);
5709  avio_rb24(pb); /* flags */
5710  if (version) {
5711  base_media_decode_time = avio_rb64(pb);
5712  } else {
5713  base_media_decode_time = avio_rb32(pb);
5714  }
5715 
5716  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5717  if (frag_stream_info)
5718  frag_stream_info->tfdt_dts = base_media_decode_time;
5719  sc->track_end = base_media_decode_time;
5720 
5721  return 0;
5722 }
5723 
5725 {
5726  MOVFragment *frag = &c->fragment;
5727  AVStream *st = NULL;
5728  FFStream *sti = NULL;
5729  MOVStreamContext *sc;
5730  MOVTimeToSample *tts_data;
5731  uint64_t offset;
5732  int64_t dts, pts = AV_NOPTS_VALUE;
5733  int data_offset = 0;
5734  unsigned entries, first_sample_flags = frag->flags;
5735  int flags, distance, i;
5736  int64_t prev_dts = AV_NOPTS_VALUE;
5737  int next_frag_index = -1, index_entry_pos;
5738  size_t requested_size;
5739  size_t old_allocated_size;
5740  AVIndexEntry *new_entries;
5741  MOVFragmentStreamInfo * frag_stream_info;
5742 
5743  if (!frag->found_tfhd) {
5744  av_log(c->fc, AV_LOG_ERROR, "trun track id unknown, no tfhd was found\n");
5745  return AVERROR_INVALIDDATA;
5746  }
5747 
5748  for (i = 0; i < c->fc->nb_streams; i++) {
5749  sc = c->fc->streams[i]->priv_data;
5750  if (sc->id == frag->track_id) {
5751  st = c->fc->streams[i];
5752  sti = ffstream(st);
5753  break;
5754  }
5755  }
5756  if (!st) {
5757  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %u\n", frag->track_id);
5758  return 0;
5759  }
5760  sc = st->priv_data;
5761  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5762  return 0;
5763 
5764  // Find the next frag_index index that has a valid index_entry for
5765  // the current track_id.
5766  //
5767  // A valid index_entry means the trun for the fragment was read
5768  // and it's samples are in index_entries at the given position.
5769  // New index entries will be inserted before the index_entry found.
5770  index_entry_pos = sti->nb_index_entries;
5771  for (i = c->frag_index.current + 1; i < c->frag_index.nb_items; i++) {
5772  frag_stream_info = get_frag_stream_info(&c->frag_index, i, frag->track_id);
5773  if (frag_stream_info && frag_stream_info->index_entry >= 0) {
5774  next_frag_index = i;
5775  index_entry_pos = frag_stream_info->index_entry;
5776  break;
5777  }
5778  }
5779  av_assert0(index_entry_pos <= sti->nb_index_entries);
5780 
5781  avio_r8(pb); /* version */
5782  flags = avio_rb24(pb);
5783  entries = avio_rb32(pb);
5784  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %u\n", flags, entries);
5785 
5786  if ((uint64_t)entries+sc->tts_count >= UINT_MAX/sizeof(*sc->tts_data))
5787  return AVERROR_INVALIDDATA;
5788  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
5789  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
5790 
5791  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
5792  if (frag_stream_info) {
5793  if (frag_stream_info->next_trun_dts != AV_NOPTS_VALUE) {
5794  dts = frag_stream_info->next_trun_dts - sc->time_offset;
5795  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
5796  c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
5797  pts = frag_stream_info->first_tfra_pts;
5798  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
5799  ", using it for pts\n", pts);
5800  } else if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE &&
5801  c->use_mfra_for == FF_MOV_FLAG_MFRA_DTS) {
5802  dts = frag_stream_info->first_tfra_pts;
5803  av_log(c->fc, AV_LOG_DEBUG, "found mfra time %"PRId64
5804  ", using it for dts\n", pts);
5805  } else {
5806  int has_tfdt = frag_stream_info->tfdt_dts != AV_NOPTS_VALUE;
5807  int has_sidx = frag_stream_info->sidx_pts != AV_NOPTS_VALUE;
5808  int fallback_tfdt = !c->use_tfdt && !has_sidx && has_tfdt;
5809  int fallback_sidx = c->use_tfdt && !has_tfdt && has_sidx;
5810 
5811  if (fallback_sidx) {
5812  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt set but no tfdt found, using sidx instead\n");
5813  }
5814  if (fallback_tfdt) {
5815  av_log(c->fc, AV_LOG_DEBUG, "use_tfdt not set but no sidx found, using tfdt instead\n");
5816  }
5817 
5818  if (has_tfdt && c->use_tfdt || fallback_tfdt) {
5819  dts = frag_stream_info->tfdt_dts - sc->time_offset;
5820  av_log(c->fc, AV_LOG_DEBUG, "found tfdt time %"PRId64
5821  ", using it for dts\n", dts);
5822  } else if (has_sidx && !c->use_tfdt || fallback_sidx) {
5823  // FIXME: sidx earliest_presentation_time is *PTS*, s.b.
5824  // pts = frag_stream_info->sidx_pts;
5825  dts = frag_stream_info->sidx_pts - sc->time_offset;
5826  av_log(c->fc, AV_LOG_DEBUG, "found sidx time %"PRId64
5827  ", using it for dts\n", frag_stream_info->sidx_pts);
5828  } else {
5829  dts = sc->track_end - sc->time_offset;
5830  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
5831  ", using it for dts\n", dts);
5832  }
5833  }
5834  } else {
5835  dts = sc->track_end - sc->time_offset;
5836  av_log(c->fc, AV_LOG_DEBUG, "found track end time %"PRId64
5837  ", using it for dts\n", dts);
5838  }
5839  offset = frag->base_data_offset + data_offset;
5840  distance = 0;
5841  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
5842 
5843  // realloc space for new index entries
5844  if ((uint64_t)sti->nb_index_entries + entries >= UINT_MAX / sizeof(AVIndexEntry)) {
5845  entries = UINT_MAX / sizeof(AVIndexEntry) - sti->nb_index_entries;
5846  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
5847  }
5848  if (entries == 0)
5849  return 0;
5850 
5851  requested_size = (sti->nb_index_entries + entries) * sizeof(AVIndexEntry);
5852  new_entries = av_fast_realloc(sti->index_entries,
5854  requested_size);
5855  if (!new_entries)
5856  return AVERROR(ENOMEM);
5857  sti->index_entries= new_entries;
5858 
5859  requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->tts_data);
5860  old_allocated_size = sc->tts_allocated_size;
5861  tts_data = av_fast_realloc(sc->tts_data, &sc->tts_allocated_size,
5862  requested_size);
5863  if (!tts_data)
5864  return AVERROR(ENOMEM);
5865  sc->tts_data = tts_data;
5866 
5867  // In case there were samples without time to sample entries, ensure they get
5868  // zero valued entries. This ensures clips which mix boxes with and
5869  // without time to sample entries don't pickup uninitialized data.
5870  memset((uint8_t*)(sc->tts_data) + old_allocated_size, 0,
5871  sc->tts_allocated_size - old_allocated_size);
5872 
5873  if (index_entry_pos < sti->nb_index_entries) {
5874  // Make hole in index_entries and tts_data for new samples
5875  memmove(sti->index_entries + index_entry_pos + entries,
5876  sti->index_entries + index_entry_pos,
5877  sizeof(*sti->index_entries) *
5878  (sti->nb_index_entries - index_entry_pos));
5879  memmove(sc->tts_data + index_entry_pos + entries,
5880  sc->tts_data + index_entry_pos,
5881  sizeof(*sc->tts_data) * (sc->tts_count - index_entry_pos));
5882  if (index_entry_pos < sc->current_sample) {
5883  sc->current_sample += entries;
5884  }
5885  }
5886 
5887  sti->nb_index_entries += entries;
5888  sc->tts_count = sti->nb_index_entries;
5889  sc->stts_count = sti->nb_index_entries;
5890  if (flags & MOV_TRUN_SAMPLE_CTS)
5891  sc->ctts_count = sti->nb_index_entries;
5892 
5893  // Record the index_entry position in frag_index of this fragment
5894  if (frag_stream_info) {
5895  frag_stream_info->index_entry = index_entry_pos;
5896  if (frag_stream_info->index_base < 0)
5897  frag_stream_info->index_base = index_entry_pos;
5898  }
5899 
5900  if (index_entry_pos > 0)
5901  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
5902 
5903  for (i = 0; i < entries && !pb->eof_reached; i++) {
5904  unsigned sample_size = frag->size;
5905  int sample_flags = i ? frag->flags : first_sample_flags;
5906  unsigned sample_duration = frag->duration;
5907  unsigned ctts_duration = 0;
5908  int keyframe = 0;
5909  int index_entry_flags = 0;
5910 
5911  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
5912  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
5913  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
5914  if (flags & MOV_TRUN_SAMPLE_CTS) ctts_duration = avio_rb32(pb);
5915 
5916  mov_update_dts_shift(sc, ctts_duration, c->fc);
5917  if (pts != AV_NOPTS_VALUE) {
5918  dts = pts - sc->dts_shift;
5919  if (flags & MOV_TRUN_SAMPLE_CTS) {
5920  dts -= ctts_duration;
5921  } else {
5922  dts -= sc->time_offset;
5923  }
5924  av_log(c->fc, AV_LOG_DEBUG,
5925  "pts %"PRId64" calculated dts %"PRId64
5926  " sc->dts_shift %d ctts.duration %d"
5927  " sc->time_offset %"PRId64
5928  " flags & MOV_TRUN_SAMPLE_CTS %d\n",
5929  pts, dts,
5930  sc->dts_shift, ctts_duration,
5932  pts = AV_NOPTS_VALUE;
5933  }
5934 
5935  keyframe =
5936  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
5938  if (keyframe) {
5939  distance = 0;
5940  index_entry_flags |= AVINDEX_KEYFRAME;
5941  }
5942  // Fragments can overlap in time. Discard overlapping frames after
5943  // decoding.
5944  if (prev_dts >= dts)
5945  index_entry_flags |= AVINDEX_DISCARD_FRAME;
5946 
5947  sti->index_entries[index_entry_pos].pos = offset;
5948  sti->index_entries[index_entry_pos].timestamp = dts;
5949  sti->index_entries[index_entry_pos].size = sample_size;
5950  sti->index_entries[index_entry_pos].min_distance = distance;
5951  sti->index_entries[index_entry_pos].flags = index_entry_flags;
5952 
5953  sc->tts_data[index_entry_pos].count = 1;
5954  sc->tts_data[index_entry_pos].offset = ctts_duration;
5955  sc->tts_data[index_entry_pos].duration = sample_duration;
5956  index_entry_pos++;
5957 
5958  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
5959  "size %u, distance %d, keyframe %d\n", st->index,
5960  index_entry_pos, offset, dts, sample_size, distance, keyframe);
5961  distance++;
5962  if (av_sat_add64(dts, sample_duration) != dts + (uint64_t)sample_duration)
5963  return AVERROR_INVALIDDATA;
5964  if (!sample_size)
5965  return AVERROR_INVALIDDATA;
5966  dts += sample_duration;
5967  offset += sample_size;
5968  sc->data_size += sample_size;
5969 
5970  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
5971  1 <= INT_MAX - sc->nb_frames_for_fps
5972  ) {
5973  sc->duration_for_fps += sample_duration;
5974  sc->nb_frames_for_fps ++;
5975  }
5976  }
5977  if (frag_stream_info)
5978  frag_stream_info->next_trun_dts = dts + sc->time_offset;
5979  if (i < entries) {
5980  // EOF found before reading all entries. Fix the hole this would
5981  // leave in index_entries and tts_data
5982  int gap = entries - i;
5983  memmove(sti->index_entries + index_entry_pos,
5984  sti->index_entries + index_entry_pos + gap,
5985  sizeof(*sti->index_entries) *
5986  (sti->nb_index_entries - (index_entry_pos + gap)));
5987  memmove(sc->tts_data + index_entry_pos,
5988  sc->tts_data + index_entry_pos + gap,
5989  sizeof(*sc->tts_data) *
5990  (sc->tts_count - (index_entry_pos + gap)));
5991 
5992  sti->nb_index_entries -= gap;
5993  sc->tts_count -= gap;
5994  if (index_entry_pos < sc->current_sample) {
5995  sc->current_sample -= gap;
5996  }
5997  entries = i;
5998  }
5999 
6000  // The end of this new fragment may overlap in time with the start
6001  // of the next fragment in index_entries. Mark the samples in the next
6002  // fragment that overlap with AVINDEX_DISCARD_FRAME
6003  prev_dts = AV_NOPTS_VALUE;
6004  if (index_entry_pos > 0)
6005  prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
6006  for (int i = index_entry_pos; i < sti->nb_index_entries; i++) {
6007  if (prev_dts < sti->index_entries[i].timestamp)
6008  break;
6010  }
6011 
6012  // If a hole was created to insert the new index_entries into,
6013  // the index_entry recorded for all subsequent moof must
6014  // be incremented by the number of entries inserted.
6015  fix_frag_index_entries(&c->frag_index, next_frag_index,
6016  frag->track_id, entries);
6017 
6018  if (pb->eof_reached) {
6019  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted TRUN atom\n");
6020  return AVERROR_EOF;
6021  }
6022 
6023  frag->implicit_offset = offset;
6024 
6025  sc->track_end = dts + sc->time_offset;
6026  if (st->duration < sc->track_end)
6027  st->duration = sc->track_end;
6028 
6029  return 0;
6030 }
6031 
6033 {
6034  int64_t stream_size = avio_size(pb);
6035  int64_t offset = av_sat_add64(avio_tell(pb), atom.size), pts, timestamp;
6036  uint8_t version, is_complete;
6037  int64_t offadd;
6038  unsigned i, j, track_id, item_count;
6039  AVStream *st = NULL;
6040  AVStream *ref_st = NULL;
6041  MOVStreamContext *sc, *ref_sc = NULL;
6042  AVRational timescale;
6043 
6044  version = avio_r8(pb);
6045  if (version > 1) {
6046  avpriv_request_sample(c->fc, "sidx version %u", version);
6047  return 0;
6048  }
6049 
6050  avio_rb24(pb); // flags
6051 
6052  track_id = avio_rb32(pb); // Reference ID
6053  for (i = 0; i < c->fc->nb_streams; i++) {
6054  sc = c->fc->streams[i]->priv_data;
6055  if (sc->id == track_id) {
6056  st = c->fc->streams[i];
6057  break;
6058  }
6059  }
6060  if (!st) {
6061  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
6062  return 0;
6063  }
6064 
6065  sc = st->priv_data;
6066 
6067  timescale = av_make_q(1, avio_rb32(pb));
6068 
6069  if (timescale.den <= 0) {
6070  av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);
6071  return AVERROR_INVALIDDATA;
6072  }
6073 
6074  if (version == 0) {
6075  pts = avio_rb32(pb);
6076  offadd= avio_rb32(pb);
6077  } else {
6078  pts = avio_rb64(pb);
6079  offadd= avio_rb64(pb);
6080  }
6081  if (av_sat_add64(offset, offadd) != offset + (uint64_t)offadd)
6082  return AVERROR_INVALIDDATA;
6083 
6084  offset += (uint64_t)offadd;
6085 
6086  avio_rb16(pb); // reserved
6087 
6088  item_count = avio_rb16(pb);
6089  if (item_count == 0)
6090  return AVERROR_INVALIDDATA;
6091 
6092  for (i = 0; i < item_count; i++) {
6093  int index;
6094  MOVFragmentStreamInfo * frag_stream_info;
6095  uint32_t size = avio_rb32(pb);
6096  uint32_t duration = avio_rb32(pb);
6097  if (size & 0x80000000) {
6098  avpriv_request_sample(c->fc, "sidx reference_type 1");
6099  return AVERROR_PATCHWELCOME;
6100  }
6101  avio_rb32(pb); // sap_flags
6102  timestamp = av_rescale_q(pts, timescale, st->time_base);
6103 
6105  frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);
6106  if (frag_stream_info)
6107  frag_stream_info->sidx_pts = timestamp;
6108 
6109  if (av_sat_add64(offset, size) != offset + (uint64_t)size ||
6110  av_sat_add64(pts, duration) != pts + (uint64_t)duration
6111  )
6112  return AVERROR_INVALIDDATA;
6113  offset += size;
6114  pts += duration;
6115  }
6116 
6117  st->duration = sc->track_end = pts;
6118 
6119  sc->has_sidx = 1;
6120 
6121  // See if the remaining bytes are just an mfra which we can ignore.
6122  is_complete = offset == stream_size;
6123  if (!is_complete && (pb->seekable & AVIO_SEEKABLE_NORMAL) && stream_size > 0 ) {
6124  int64_t ret;
6125  int64_t original_pos = avio_tell(pb);
6126  if (!c->have_read_mfra_size) {
6127  if ((ret = avio_seek(pb, stream_size - 4, SEEK_SET)) < 0)
6128  return ret;
6129  c->mfra_size = avio_rb32(pb);
6130  c->have_read_mfra_size = 1;
6131  if ((ret = avio_seek(pb, original_pos, SEEK_SET)) < 0)
6132  return ret;
6133  }
6134  if (offset == stream_size - c->mfra_size)
6135  is_complete = 1;
6136  }
6137 
6138  if (is_complete) {
6139  // Find first entry in fragment index that came from an sidx.
6140  // This will pretty much always be the first entry.
6141  for (i = 0; i < c->frag_index.nb_items; i++) {
6142  MOVFragmentIndexItem * item = &c->frag_index.item[i];
6143  for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {
6144  MOVFragmentStreamInfo * si;
6145  si = &item->stream_info[j];
6146  if (si->sidx_pts != AV_NOPTS_VALUE) {
6147  ref_st = c->fc->streams[j];
6148  ref_sc = ref_st->priv_data;
6149  break;
6150  }
6151  }
6152  }
6153  if (ref_st) for (i = 0; i < c->fc->nb_streams; i++) {
6154  st = c->fc->streams[i];
6155  sc = st->priv_data;
6156  if (!sc->has_sidx) {
6157  st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
6158  }
6159  }
6160 
6161  c->frag_index.complete = 1;
6162  }
6163 
6164  return 0;
6165 }
6166 
6167 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
6168 /* like the files created with Adobe Premiere 5.0, for samples see */
6169 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
6171 {
6172  int err;
6173 
6174  if (atom.size < 8)
6175  return 0; /* continue */
6176  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
6177  avio_skip(pb, atom.size - 4);
6178  return 0;
6179  }
6180  atom.type = avio_rl32(pb);
6181  atom.size -= 8;
6182  if (atom.type != MKTAG('m','d','a','t')) {
6183  avio_skip(pb, atom.size);
6184  return 0;
6185  }
6186  err = mov_read_mdat(c, pb, atom);
6187  return err;
6188 }
6189 
6191 {
6192 #if CONFIG_ZLIB
6193  FFIOContext ctx;
6194  uint8_t *cmov_data;
6195  uint8_t *moov_data; /* uncompressed data */
6196  long cmov_len, moov_len;
6197  int ret = -1;
6198 
6199  avio_rb32(pb); /* dcom atom */
6200  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
6201  return AVERROR_INVALIDDATA;
6202  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
6203  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
6204  return AVERROR_INVALIDDATA;
6205  }
6206  avio_rb32(pb); /* cmvd atom */
6207  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
6208  return AVERROR_INVALIDDATA;
6209  moov_len = avio_rb32(pb); /* uncompressed size */
6210  cmov_len = atom.size - 6 * 4;
6211 
6212  cmov_data = av_malloc(cmov_len);
6213  if (!cmov_data)
6214  return AVERROR(ENOMEM);
6215  moov_data = av_malloc(moov_len);
6216  if (!moov_data) {
6217  av_free(cmov_data);
6218  return AVERROR(ENOMEM);
6219  }
6220  ret = ffio_read_size(pb, cmov_data, cmov_len);
6221  if (ret < 0)
6222  goto free_and_return;
6223 
6225  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
6226  goto free_and_return;
6227  ffio_init_read_context(&ctx, moov_data, moov_len);
6228  ctx.pub.seekable = AVIO_SEEKABLE_NORMAL;
6229  atom.type = MKTAG('m','o','o','v');
6230  atom.size = moov_len;
6231  ret = mov_read_default(c, &ctx.pub, atom);
6232 free_and_return:
6233  av_free(moov_data);
6234  av_free(cmov_data);
6235  return ret;
6236 #else
6237  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
6238  return AVERROR(ENOSYS);
6239 #endif
6240 }
6241 
6242 /* edit list atom */
6244 {
6245  MOVStreamContext *sc;
6246  int i, edit_count, version;
6247  int64_t elst_entry_size;
6248 
6249  if (c->fc->nb_streams < 1 || c->ignore_editlist)
6250  return 0;
6251  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
6252 
6253  version = avio_r8(pb); /* version */
6254  avio_rb24(pb); /* flags */
6255  edit_count = avio_rb32(pb); /* entries */
6256  atom.size -= 8;
6257 
6258  elst_entry_size = version == 1 ? 20 : 12;
6259  if (atom.size != edit_count * elst_entry_size) {
6260  if (c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6261  av_log(c->fc, AV_LOG_ERROR, "Invalid edit list entry_count: %d for elst atom of size: %"PRId64" bytes.\n",
6262  edit_count, atom.size + 8);
6263  return AVERROR_INVALIDDATA;
6264  } else {
6265  edit_count = atom.size / elst_entry_size;
6266  if (edit_count * elst_entry_size != atom.size) {
6267  av_log(c->fc, AV_LOG_WARNING, "ELST atom of %"PRId64" bytes, bigger than %d entries.\n", atom.size, edit_count);
6268  }
6269  }
6270  }
6271 
6272  if (!edit_count)
6273  return 0;
6274  if (sc->elst_data)
6275  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
6276  av_free(sc->elst_data);
6277  sc->elst_count = 0;
6278  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
6279  if (!sc->elst_data)
6280  return AVERROR(ENOMEM);
6281 
6282  av_log(c->fc, AV_LOG_TRACE, "track[%u].edit_count = %i\n", c->fc->nb_streams - 1, edit_count);
6283  for (i = 0; i < edit_count && atom.size > 0 && !pb->eof_reached; i++) {
6284  MOVElst *e = &sc->elst_data[i];
6285 
6286  if (version == 1) {
6287  e->duration = avio_rb64(pb);
6288  e->time = avio_rb64(pb);
6289  atom.size -= 16;
6290  } else {
6291  e->duration = avio_rb32(pb); /* segment duration */
6292  e->time = (int32_t)avio_rb32(pb); /* media time */
6293  atom.size -= 8;
6294  }
6295  e->rate = avio_rb32(pb) / 65536.0;
6296  atom.size -= 4;
6297  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
6298  e->duration, e->time, e->rate);
6299 
6300  if (e->time < 0 && e->time != -1 &&
6301  c->fc->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
6302  av_log(c->fc, AV_LOG_ERROR, "Track %d, edit %d: Invalid edit list media time=%"PRId64"\n",
6303  c->fc->nb_streams-1, i, e->time);
6304  return AVERROR_INVALIDDATA;
6305  }
6306  }
6307  sc->elst_count = i;
6308 
6309  return 0;
6310 }
6311 
6313 {
6314  MOVStreamContext *sc;
6315 
6316  if (c->fc->nb_streams < 1)
6317  return AVERROR_INVALIDDATA;
6318  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6319  sc->timecode_track = avio_rb32(pb);
6320  return 0;
6321 }
6322 
6324 {
6325  AVStream *st;
6326  int version, color_range, color_primaries, color_trc, color_space;
6327 
6328  if (c->fc->nb_streams < 1)
6329  return 0;
6330  st = c->fc->streams[c->fc->nb_streams - 1];
6331 
6332  if (atom.size < 5) {
6333  av_log(c->fc, AV_LOG_ERROR, "Empty VP Codec Configuration box\n");
6334  return AVERROR_INVALIDDATA;
6335  }
6336 
6337  version = avio_r8(pb);
6338  if (version != 1) {
6339  av_log(c->fc, AV_LOG_WARNING, "Unsupported VP Codec Configuration box version %d\n", version);
6340  return 0;
6341  }
6342  avio_skip(pb, 3); /* flags */
6343 
6344  avio_skip(pb, 2); /* profile + level */
6345  color_range = avio_r8(pb); /* bitDepth, chromaSubsampling, videoFullRangeFlag */
6346  color_primaries = avio_r8(pb);
6347  color_trc = avio_r8(pb);
6348  color_space = avio_r8(pb);
6349  if (avio_rb16(pb)) /* codecIntializationDataSize */
6350  return AVERROR_INVALIDDATA;
6351 
6354  if (!av_color_transfer_name(color_trc))
6355  color_trc = AVCOL_TRC_UNSPECIFIED;
6356  if (!av_color_space_name(color_space))
6357  color_space = AVCOL_SPC_UNSPECIFIED;
6358 
6361  st->codecpar->color_trc = color_trc;
6362  st->codecpar->color_space = color_space;
6363 
6364  return 0;
6365 }
6366 
6368 {
6369  MOVStreamContext *sc;
6370  int i, version;
6371 
6372  if (c->fc->nb_streams < 1)
6373  return AVERROR_INVALIDDATA;
6374 
6375  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6376 
6377  if (atom.size < 5) {
6378  av_log(c->fc, AV_LOG_ERROR, "Empty Mastering Display Metadata box\n");
6379  return AVERROR_INVALIDDATA;
6380  }
6381 
6382  version = avio_r8(pb);
6383  if (version) {
6384  av_log(c->fc, AV_LOG_WARNING, "Unsupported Mastering Display Metadata box version %d\n", version);
6385  return 0;
6386  }
6387  if (sc->mastering) {
6388  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Metadata\n");
6389  return 0;
6390  }
6391 
6392  avio_skip(pb, 3); /* flags */
6393 
6395  if (!sc->mastering)
6396  return AVERROR(ENOMEM);
6397 
6398  for (i = 0; i < 3; i++) {
6399  sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
6400  sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
6401  }
6402  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
6403  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
6404 
6405  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
6406  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
6407 
6408  sc->mastering->has_primaries = 1;
6409  sc->mastering->has_luminance = 1;
6410 
6411  return 0;
6412 }
6413 
6415 {
6416  MOVStreamContext *sc;
6417  const int mapping[3] = {1, 2, 0};
6418  const int chroma_den = 50000;
6419  const int luma_den = 10000;
6420  int i;
6421 
6422  if (c->fc->nb_streams < 1)
6423  return AVERROR_INVALIDDATA;
6424 
6425  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6426 
6427  if (atom.size < 24) {
6428  av_log(c->fc, AV_LOG_ERROR, "Invalid Mastering Display Color Volume box\n");
6429  return AVERROR_INVALIDDATA;
6430  }
6431 
6432  if (sc->mastering) {
6433  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate Mastering Display Color Volume\n");
6434  return 0;
6435  }
6436 
6438  if (!sc->mastering)
6439  return AVERROR(ENOMEM);
6440 
6441  for (i = 0; i < 3; i++) {
6442  const int j = mapping[i];
6443  sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
6444  sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
6445  }
6446  sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
6447  sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
6448 
6449  sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
6450  sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
6451 
6452  sc->mastering->has_luminance = 1;
6453  sc->mastering->has_primaries = 1;
6454 
6455  return 0;
6456 }
6457 
6459 {
6460  MOVStreamContext *sc;
6461  int version;
6462 
6463  if (c->fc->nb_streams < 1)
6464  return AVERROR_INVALIDDATA;
6465 
6466  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6467 
6468  if (atom.size < 5) {
6469  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level box\n");
6470  return AVERROR_INVALIDDATA;
6471  }
6472 
6473  version = avio_r8(pb);
6474  if (version) {
6475  av_log(c->fc, AV_LOG_WARNING, "Unsupported Content Light Level box version %d\n", version);
6476  return 0;
6477  }
6478  avio_skip(pb, 3); /* flags */
6479 
6480  if (sc->coll){
6481  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate COLL\n");
6482  return 0;
6483  }
6484 
6486  if (!sc->coll)
6487  return AVERROR(ENOMEM);
6488 
6489  sc->coll->MaxCLL = avio_rb16(pb);
6490  sc->coll->MaxFALL = avio_rb16(pb);
6491 
6492  return 0;
6493 }
6494 
6496 {
6497  MOVStreamContext *sc;
6498 
6499  if (c->fc->nb_streams < 1)
6500  return AVERROR_INVALIDDATA;
6501 
6502  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6503 
6504  if (atom.size < 4) {
6505  av_log(c->fc, AV_LOG_ERROR, "Empty Content Light Level Info box\n");
6506  return AVERROR_INVALIDDATA;
6507  }
6508 
6509  if (sc->coll){
6510  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate CLLI/COLL\n");
6511  return 0;
6512  }
6513 
6515  if (!sc->coll)
6516  return AVERROR(ENOMEM);
6517 
6518  sc->coll->MaxCLL = avio_rb16(pb);
6519  sc->coll->MaxFALL = avio_rb16(pb);
6520 
6521  return 0;
6522 }
6523 
6525 {
6526  MOVStreamContext *sc;
6527  const int illuminance_den = 10000;
6528  const int ambient_den = 50000;
6529  if (c->fc->nb_streams < 1)
6530  return AVERROR_INVALIDDATA;
6531  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
6532  if (atom.size < 6) {
6533  av_log(c->fc, AV_LOG_ERROR, "Empty Ambient Viewing Environment Info box\n");
6534  return AVERROR_INVALIDDATA;
6535  }
6536  if (sc->ambient){
6537  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicate AMVE\n");
6538  return 0;
6539  }
6541  if (!sc->ambient)
6542  return AVERROR(ENOMEM);
6543  sc->ambient->ambient_illuminance = av_make_q(avio_rb32(pb), illuminance_den);
6544  sc->ambient->ambient_light_x = av_make_q(avio_rb16(pb), ambient_den);
6545  sc->ambient->ambient_light_y = av_make_q(avio_rb16(pb), ambient_den);
6546  return 0;
6547 }
6548 
6550 {
6551  AVStream *st;
6552  MOVStreamContext *sc;
6553  enum AVStereo3DType type;
6554  int mode;
6555 
6556  if (c->fc->nb_streams < 1)
6557  return 0;
6558 
6559  st = c->fc->streams[c->fc->nb_streams - 1];
6560  sc = st->priv_data;
6561 
6562  if (atom.size < 5) {
6563  av_log(c->fc, AV_LOG_ERROR, "Empty stereoscopic video box\n");
6564  return AVERROR_INVALIDDATA;
6565  }
6566 
6567  if (sc->stereo3d)
6568  return AVERROR_INVALIDDATA;
6569 
6570  avio_skip(pb, 4); /* version + flags */
6571 
6572  mode = avio_r8(pb);
6573  switch (mode) {
6574  case 0:
6575  type = AV_STEREO3D_2D;
6576  break;
6577  case 1:
6579  break;
6580  case 2:
6582  break;
6583  default:
6584  av_log(c->fc, AV_LOG_WARNING, "Unknown st3d mode value %d\n", mode);
6585  return 0;
6586  }
6587 
6589  if (!sc->stereo3d)
6590  return AVERROR(ENOMEM);
6591 
6592  sc->stereo3d->type = type;
6593  return 0;
6594 }
6595 
6597 {
6598  AVStream *st;
6599  MOVStreamContext *sc;
6600  int size, version, layout;
6601  int32_t yaw, pitch, roll;
6602  uint32_t l = 0, t = 0, r = 0, b = 0;
6603  uint32_t tag, padding = 0;
6604  enum AVSphericalProjection projection;
6605 
6606  if (c->fc->nb_streams < 1)
6607  return 0;
6608 
6609  st = c->fc->streams[c->fc->nb_streams - 1];
6610  sc = st->priv_data;
6611 
6612  if (atom.size < 8) {
6613  av_log(c->fc, AV_LOG_ERROR, "Empty spherical video box\n");
6614  return AVERROR_INVALIDDATA;
6615  }
6616 
6617  size = avio_rb32(pb);
6618  if (size <= 12 || size > atom.size)
6619  return AVERROR_INVALIDDATA;
6620 
6621  tag = avio_rl32(pb);
6622  if (tag != MKTAG('s','v','h','d')) {
6623  av_log(c->fc, AV_LOG_ERROR, "Missing spherical video header\n");
6624  return 0;
6625  }
6626  version = avio_r8(pb);
6627  if (version != 0) {
6628  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6629  version);
6630  return 0;
6631  }
6632  avio_skip(pb, 3); /* flags */
6633  avio_skip(pb, size - 12); /* metadata_source */
6634 
6635  size = avio_rb32(pb);
6636  if (size > atom.size)
6637  return AVERROR_INVALIDDATA;
6638 
6639  tag = avio_rl32(pb);
6640  if (tag != MKTAG('p','r','o','j')) {
6641  av_log(c->fc, AV_LOG_ERROR, "Missing projection box\n");
6642  return 0;
6643  }
6644 
6645  size = avio_rb32(pb);
6646  if (size > atom.size)
6647  return AVERROR_INVALIDDATA;
6648 
6649  tag = avio_rl32(pb);
6650  if (tag != MKTAG('p','r','h','d')) {
6651  av_log(c->fc, AV_LOG_ERROR, "Missing projection header box\n");
6652  return 0;
6653  }
6654  version = avio_r8(pb);
6655  if (version != 0) {
6656  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6657  version);
6658  return 0;
6659  }
6660  avio_skip(pb, 3); /* flags */
6661 
6662  /* 16.16 fixed point */
6663  yaw = avio_rb32(pb);
6664  pitch = avio_rb32(pb);
6665  roll = avio_rb32(pb);
6666 
6667  size = avio_rb32(pb);
6668  if (size > atom.size)
6669  return AVERROR_INVALIDDATA;
6670 
6671  tag = avio_rl32(pb);
6672  version = avio_r8(pb);
6673  if (version != 0) {
6674  av_log(c->fc, AV_LOG_WARNING, "Unknown spherical version %d\n",
6675  version);
6676  return 0;
6677  }
6678  avio_skip(pb, 3); /* flags */
6679  switch (tag) {
6680  case MKTAG('c','b','m','p'):
6681  layout = avio_rb32(pb);
6682  if (layout) {
6683  av_log(c->fc, AV_LOG_WARNING,
6684  "Unsupported cubemap layout %d\n", layout);
6685  return 0;
6686  }
6687  projection = AV_SPHERICAL_CUBEMAP;
6688  padding = avio_rb32(pb);
6689  break;
6690  case MKTAG('e','q','u','i'):
6691  t = avio_rb32(pb);
6692  b = avio_rb32(pb);
6693  l = avio_rb32(pb);
6694  r = avio_rb32(pb);
6695 
6696  if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
6697  av_log(c->fc, AV_LOG_ERROR,
6698  "Invalid bounding rectangle coordinates "
6699  "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", l, t, r, b);
6700  return AVERROR_INVALIDDATA;
6701  }
6702 
6703  if (l || t || r || b)
6704  projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
6705  else
6706  projection = AV_SPHERICAL_EQUIRECTANGULAR;
6707  break;
6708  default:
6709  av_log(c->fc, AV_LOG_ERROR, "Unknown projection type: %s\n", av_fourcc2str(tag));
6710  return 0;
6711  }
6712 
6714  if (!sc->spherical)
6715  return AVERROR(ENOMEM);
6716 
6717  sc->spherical->projection = projection;
6718 
6719  sc->spherical->yaw = yaw;
6720  sc->spherical->pitch = pitch;
6721  sc->spherical->roll = roll;
6722 
6723  sc->spherical->padding = padding;
6724 
6725  sc->spherical->bound_left = l;
6726  sc->spherical->bound_top = t;
6727  sc->spherical->bound_right = r;
6728  sc->spherical->bound_bottom = b;
6729 
6730  return 0;
6731 }
6732 
6734 {
6735  AVStream *st;
6736  MOVStreamContext *sc;
6737  int size;
6738  uint32_t tag;
6739  enum AVSphericalProjection projection;
6740 
6741  if (c->fc->nb_streams < 1)
6742  return 0;
6743 
6744  st = c->fc->streams[c->fc->nb_streams - 1];
6745  sc = st->priv_data;
6746 
6747  if (atom.size != 16) {
6748  av_log(c->fc, AV_LOG_ERROR, "Invalid size for proj box: %"PRIu64"\n", atom.size);
6749  return AVERROR_INVALIDDATA;
6750  }
6751 
6752  size = avio_rb32(pb);
6753  if (size != 16) {
6754  av_log(c->fc, AV_LOG_ERROR, "Invalid size for prji box: %d\n", size);
6755  return AVERROR_INVALIDDATA;
6756  }
6757 
6758  tag = avio_rl32(pb);
6759  if (tag != MKTAG('p','r','j','i')) {
6760  av_log(c->fc, AV_LOG_ERROR, "Invalid child box of proj box: 0x%08X\n", tag);
6761  return AVERROR_INVALIDDATA;
6762  }
6763 
6764  avio_skip(pb, 1); // version
6765  avio_skip(pb, 3); // flags
6766 
6767  tag = avio_rl32(pb);
6768  switch (tag) {
6769  case MKTAG('r','e','c','t'):
6770  projection = AV_SPHERICAL_RECTILINEAR;
6771  break;
6772  case MKTAG('e','q','u','i'):
6773  projection = AV_SPHERICAL_EQUIRECTANGULAR;
6774  break;
6775  case MKTAG('h','e','q','u'):
6776  projection = AV_SPHERICAL_HALF_EQUIRECTANGULAR;
6777  break;
6778  case MKTAG('f','i','s','h'):
6779  projection = AV_SPHERICAL_FISHEYE;
6780  break;
6781  default:
6782  av_log(c->fc, AV_LOG_ERROR, "Invalid projection type in prji box: 0x%08X\n", tag);
6783  return AVERROR_INVALIDDATA;
6784  }
6785 
6787  if (!sc->spherical)
6788  return AVERROR(ENOMEM);
6789 
6790  sc->spherical->projection = projection;
6791 
6792  return 0;
6793 }
6794 
6796 {
6797  AVStream *st;
6798  MOVStreamContext *sc;
6799  int size, flags = 0;
6800  int64_t remaining;
6801  uint32_t tag, baseline = 0;
6804  enum AVStereo3DPrimaryEye primary_eye = AV_PRIMARY_EYE_NONE;
6805  AVRational horizontal_disparity_adjustment = { 0, 1 };
6806 
6807  if (c->fc->nb_streams < 1)
6808  return 0;
6809 
6810  st = c->fc->streams[c->fc->nb_streams - 1];
6811  sc = st->priv_data;
6812 
6813  remaining = atom.size;
6814  while (remaining > 0) {
6815  size = avio_rb32(pb);
6816  if (size < 8 || size > remaining ) {
6817  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in eyes box\n");
6818  return AVERROR_INVALIDDATA;
6819  }
6820 
6821  tag = avio_rl32(pb);
6822  switch (tag) {
6823  case MKTAG('s','t','r','i'): {
6824  int has_right, has_left;
6825  uint8_t tmp;
6826  if (size != 13) {
6827  av_log(c->fc, AV_LOG_ERROR, "Invalid size of stri box: %d\n", size);
6828  return AVERROR_INVALIDDATA;
6829  }
6830  avio_skip(pb, 1); // version
6831  avio_skip(pb, 3); // flags
6832 
6833  tmp = avio_r8(pb);
6834 
6835  // eye_views_reversed
6836  if (tmp & 8) {
6838  }
6839  // has_additional_views
6840  if (tmp & 4) {
6841  // skip...
6842  }
6843 
6844  has_right = tmp & 2; // has_right_eye_view
6845  has_left = tmp & 1; // has_left_eye_view
6846 
6847  if (has_left && has_right)
6848  view = AV_STEREO3D_VIEW_PACKED;
6849  else if (has_left)
6850  view = AV_STEREO3D_VIEW_LEFT;
6851  else if (has_right)
6852  view = AV_STEREO3D_VIEW_RIGHT;
6853  if (has_left || has_right)
6855 
6856  break;
6857  }
6858  case MKTAG('h','e','r','o'): {
6859  int tmp;
6860  if (size != 13) {
6861  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hero box: %d\n", size);
6862  return AVERROR_INVALIDDATA;
6863  }
6864  avio_skip(pb, 1); // version
6865  avio_skip(pb, 3); // flags
6866 
6867  tmp = avio_r8(pb);
6868  if (tmp == 0)
6869  primary_eye = AV_PRIMARY_EYE_NONE;
6870  else if (tmp == 1)
6871  primary_eye = AV_PRIMARY_EYE_LEFT;
6872  else if (tmp == 2)
6873  primary_eye = AV_PRIMARY_EYE_RIGHT;
6874  else
6875  av_log(c->fc, AV_LOG_WARNING, "Unknown hero eye type: %d\n", tmp);
6876 
6877  break;
6878  }
6879  case MKTAG('c','a','m','s'): {
6880  uint32_t subtag;
6881  int subsize;
6882  if (size != 24) {
6883  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cams box: %d\n", size);
6884  return AVERROR_INVALIDDATA;
6885  }
6886 
6887  subsize = avio_rb32(pb);
6888  if (subsize != 16) {
6889  av_log(c->fc, AV_LOG_ERROR, "Invalid size of blin box: %d\n", size);
6890  return AVERROR_INVALIDDATA;
6891  }
6892 
6893  subtag = avio_rl32(pb);
6894  if (subtag != MKTAG('b','l','i','n')) {
6895  av_log(c->fc, AV_LOG_ERROR, "Expected blin box, got 0x%08X\n", subtag);
6896  return AVERROR_INVALIDDATA;
6897  }
6898 
6899  avio_skip(pb, 1); // version
6900  avio_skip(pb, 3); // flags
6901 
6902  baseline = avio_rb32(pb);
6903 
6904  break;
6905  }
6906  case MKTAG('c','m','f','y'): {
6907  uint32_t subtag;
6908  int subsize;
6909  int32_t adjustment;
6910  if (size != 24) {
6911  av_log(c->fc, AV_LOG_ERROR, "Invalid size of cmfy box: %d\n", size);
6912  return AVERROR_INVALIDDATA;
6913  }
6914 
6915  subsize = avio_rb32(pb);
6916  if (subsize != 16) {
6917  av_log(c->fc, AV_LOG_ERROR, "Invalid size of dadj box: %d\n", size);
6918  return AVERROR_INVALIDDATA;
6919  }
6920 
6921  subtag = avio_rl32(pb);
6922  if (subtag != MKTAG('d','a','d','j')) {
6923  av_log(c->fc, AV_LOG_ERROR, "Expected dadj box, got 0x%08X\n", subtag);
6924  return AVERROR_INVALIDDATA;
6925  }
6926 
6927  avio_skip(pb, 1); // version
6928  avio_skip(pb, 3); // flags
6929 
6930  adjustment = (int32_t) avio_rb32(pb);
6931 
6932  horizontal_disparity_adjustment.num = (int) adjustment;
6933  horizontal_disparity_adjustment.den = 10000;
6934 
6935  break;
6936  }
6937  default:
6938  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in eyes: 0x%08X\n", tag);
6939  avio_skip(pb, size - 8);
6940  break;
6941  }
6942  remaining -= size;
6943  }
6944 
6945  if (remaining != 0) {
6946  av_log(c->fc, AV_LOG_ERROR, "Broken eyes box\n");
6947  return AVERROR_INVALIDDATA;
6948  }
6949 
6950  if (type == AV_STEREO3D_2D)
6951  return 0;
6952 
6953  if (!sc->stereo3d) {
6955  if (!sc->stereo3d)
6956  return AVERROR(ENOMEM);
6957  }
6958 
6959  sc->stereo3d->flags = flags;
6960  sc->stereo3d->type = type;
6961  sc->stereo3d->view = view;
6962  sc->stereo3d->primary_eye = primary_eye;
6963  sc->stereo3d->baseline = baseline;
6964  sc->stereo3d->horizontal_disparity_adjustment = horizontal_disparity_adjustment;
6965 
6966  return 0;
6967 }
6968 
6970 {
6971  int size;
6972  int64_t remaining;
6973  uint32_t tag;
6974 
6975  if (c->fc->nb_streams < 1)
6976  return 0;
6977 
6978  if (atom.size < 8) {
6979  av_log(c->fc, AV_LOG_ERROR, "Empty video extension usage box\n");
6980  return AVERROR_INVALIDDATA;
6981  }
6982 
6983  remaining = atom.size;
6984  while (remaining > 0) {
6985  size = avio_rb32(pb);
6986  if (size < 8 || size > remaining ) {
6987  av_log(c->fc, AV_LOG_ERROR, "Invalid child size in vexu box\n");
6988  return AVERROR_INVALIDDATA;
6989  }
6990 
6991  tag = avio_rl32(pb);
6992  switch (tag) {
6993  case MKTAG('p','r','o','j'): {
6994  MOVAtom proj = { tag, size - 8 };
6995  int ret = mov_read_vexu_proj(c, pb, proj);
6996  if (ret < 0)
6997  return ret;
6998  break;
6999  }
7000  case MKTAG('e','y','e','s'): {
7001  MOVAtom eyes = { tag, size - 8 };
7002  int ret = mov_read_eyes(c, pb, eyes);
7003  if (ret < 0)
7004  return ret;
7005  break;
7006  }
7007  default:
7008  av_log(c->fc, AV_LOG_WARNING, "Unknown tag in vexu: 0x%08X\n", tag);
7009  avio_skip(pb, size - 8);
7010  break;
7011  }
7012  remaining -= size;
7013  }
7014 
7015  if (remaining != 0) {
7016  av_log(c->fc, AV_LOG_ERROR, "Broken vexu box\n");
7017  return AVERROR_INVALIDDATA;
7018  }
7019 
7020  return 0;
7021 }
7022 
7024 {
7025  AVStream *st;
7026  MOVStreamContext *sc;
7027 
7028  if (c->fc->nb_streams < 1)
7029  return 0;
7030 
7031  st = c->fc->streams[c->fc->nb_streams - 1];
7032  sc = st->priv_data;
7033 
7034  if (atom.size != 4) {
7035  av_log(c->fc, AV_LOG_ERROR, "Invalid size of hfov box: %"PRIu64"\n", atom.size);
7036  return AVERROR_INVALIDDATA;
7037  }
7038 
7039 
7040  if (!sc->stereo3d) {
7042  if (!sc->stereo3d)
7043  return AVERROR(ENOMEM);
7044  }
7045 
7047  sc->stereo3d->horizontal_field_of_view.den = 1000; // thousands of a degree
7048 
7049  return 0;
7050 }
7051 
7053 {
7054  int ret = 0;
7055  uint8_t *buffer = av_malloc(len + 1);
7056  const char *val;
7057 
7058  if (!buffer)
7059  return AVERROR(ENOMEM);
7060  buffer[len] = '\0';
7061 
7062  ret = ffio_read_size(pb, buffer, len);
7063  if (ret < 0)
7064  goto out;
7065 
7066  /* Check for mandatory keys and values, try to support XML as best-effort */
7067  if (!sc->spherical &&
7068  av_stristr(buffer, "<GSpherical:StitchingSoftware>") &&
7069  (val = av_stristr(buffer, "<GSpherical:Spherical>")) &&
7070  av_stristr(val, "true") &&
7071  (val = av_stristr(buffer, "<GSpherical:Stitched>")) &&
7072  av_stristr(val, "true") &&
7073  (val = av_stristr(buffer, "<GSpherical:ProjectionType>")) &&
7074  av_stristr(val, "equirectangular")) {
7076  if (!sc->spherical)
7077  goto out;
7078 
7080 
7081  if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
7082  enum AVStereo3DType mode;
7083 
7084  if (av_stristr(buffer, "left-right"))
7086  else if (av_stristr(buffer, "top-bottom"))
7088  else
7089  mode = AV_STEREO3D_2D;
7090 
7092  if (!sc->stereo3d)
7093  goto out;
7094 
7095  sc->stereo3d->type = mode;
7096  }
7097 
7098  /* orientation */
7099  val = av_stristr(buffer, "<GSpherical:InitialViewHeadingDegrees>");
7100  if (val)
7101  sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
7102  val = av_stristr(buffer, "<GSpherical:InitialViewPitchDegrees>");
7103  if (val)
7104  sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
7105  val = av_stristr(buffer, "<GSpherical:InitialViewRollDegrees>");
7106  if (val)
7107  sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
7108  }
7109 
7110 out:
7111  av_free(buffer);
7112  return ret;
7113 }
7114 
7116 {
7117  AVStream *st;
7118  MOVStreamContext *sc;
7119  int64_t ret;
7120  AVUUID uuid;
7121  static const AVUUID uuid_isml_manifest = {
7122  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
7123  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
7124  };
7125  static const AVUUID uuid_xmp = {
7126  0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8,
7127  0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac
7128  };
7129  static const AVUUID uuid_spherical = {
7130  0xff, 0xcc, 0x82, 0x63, 0xf8, 0x55, 0x4a, 0x93,
7131  0x88, 0x14, 0x58, 0x7a, 0x02, 0x52, 0x1f, 0xdd,
7132  };
7133 
7134  if (atom.size < AV_UUID_LEN || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
7135  return AVERROR_INVALIDDATA;
7136 
7137  if (c->fc->nb_streams < 1)
7138  return 0;
7139  st = c->fc->streams[c->fc->nb_streams - 1];
7140  sc = st->priv_data;
7141 
7142  ret = ffio_read_size(pb, uuid, AV_UUID_LEN);
7143  if (ret < 0)
7144  return ret;
7145  if (av_uuid_equal(uuid, uuid_isml_manifest)) {
7146  uint8_t *buffer, *ptr;
7147  char *endptr;
7148  size_t len = atom.size - AV_UUID_LEN;
7149 
7150  if (len < 4) {
7151  return AVERROR_INVALIDDATA;
7152  }
7153  ret = avio_skip(pb, 4); // zeroes
7154  len -= 4;
7155 
7156  buffer = av_mallocz(len + 1);
7157  if (!buffer) {
7158  return AVERROR(ENOMEM);
7159  }
7160  ret = ffio_read_size(pb, buffer, len);
7161  if (ret < 0) {
7162  av_free(buffer);
7163  return ret;
7164  }
7165 
7166  ptr = buffer;
7167  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
7168  ptr += sizeof("systemBitrate=\"") - 1;
7169  c->bitrates_count++;
7170  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
7171  if (!c->bitrates) {
7172  c->bitrates_count = 0;
7173  av_free(buffer);
7174  return AVERROR(ENOMEM);
7175  }
7176  errno = 0;
7177  ret = strtol(ptr, &endptr, 10);
7178  if (ret < 0 || errno || *endptr != '"') {
7179  c->bitrates[c->bitrates_count - 1] = 0;
7180  } else {
7181  c->bitrates[c->bitrates_count - 1] = ret;
7182  }
7183  }
7184 
7185  av_free(buffer);
7186  } else if (av_uuid_equal(uuid, uuid_xmp)) {
7187  uint8_t *buffer;
7188  size_t len = atom.size - AV_UUID_LEN;
7189  if (c->export_xmp) {
7190  buffer = av_mallocz(len + 1);
7191  if (!buffer) {
7192  return AVERROR(ENOMEM);
7193  }
7194  ret = ffio_read_size(pb, buffer, len);
7195  if (ret < 0) {
7196  av_free(buffer);
7197  return ret;
7198  }
7199  buffer[len] = '\0';
7200  av_dict_set(&c->fc->metadata, "xmp",
7202  } else {
7203  // skip all uuid atom, which makes it fast for long uuid-xmp file
7204  ret = avio_skip(pb, len);
7205  if (ret < 0)
7206  return ret;
7207  }
7208  } else if (av_uuid_equal(uuid, uuid_spherical)) {
7209  size_t len = atom.size - AV_UUID_LEN;
7210  ret = mov_parse_uuid_spherical(sc, pb, len);
7211  if (ret < 0)
7212  return ret;
7213  if (!sc->spherical)
7214  av_log(c->fc, AV_LOG_WARNING, "Invalid spherical metadata found\n");
7215  }
7216 
7217  return 0;
7218 }
7219 
7221 {
7222  int ret;
7223  uint8_t content[16];
7224 
7225  if (atom.size < 8)
7226  return 0;
7227 
7228  ret = ffio_read_size(pb, content, FFMIN(sizeof(content), atom.size));
7229  if (ret < 0)
7230  return ret;
7231 
7232  if ( !c->found_moov
7233  && !c->found_mdat
7234  && !memcmp(content, "Anevia\x1A\x1A", 8)
7235  && c->use_mfra_for == FF_MOV_FLAG_MFRA_AUTO) {
7236  c->use_mfra_for = FF_MOV_FLAG_MFRA_PTS;
7237  }
7238 
7239  return 0;
7240 }
7241 
7243 {
7244  uint32_t format = avio_rl32(pb);
7245  MOVStreamContext *sc;
7246  enum AVCodecID id;
7247  AVStream *st;
7248 
7249  if (c->fc->nb_streams < 1)
7250  return 0;
7251  st = c->fc->streams[c->fc->nb_streams - 1];
7252  sc = st->priv_data;
7253 
7254  switch (sc->format)
7255  {
7256  case MKTAG('e','n','c','v'): // encrypted video
7257  case MKTAG('e','n','c','a'): // encrypted audio
7258  id = mov_codec_id(st, format);
7259  if (st->codecpar->codec_id != AV_CODEC_ID_NONE &&
7260  st->codecpar->codec_id != id) {
7261  av_log(c->fc, AV_LOG_WARNING,
7262  "ignoring 'frma' atom of '%.4s', stream has codec id %d\n",
7263  (char*)&format, st->codecpar->codec_id);
7264  break;
7265  }
7266 
7267  st->codecpar->codec_id = id;
7268  sc->format = format;
7269  break;
7270 
7271  default:
7272  if (format != sc->format) {
7273  av_log(c->fc, AV_LOG_WARNING,
7274  "ignoring 'frma' atom of '%.4s', stream format is '%.4s'\n",
7275  (char*)&format, (char*)&sc->format);
7276  }
7277  break;
7278  }
7279 
7280  return 0;
7281 }
7282 
7283 /**
7284  * Gets the current encryption info and associated current stream context. If
7285  * we are parsing a track fragment, this will return the specific encryption
7286  * info for this fragment; otherwise this will return the global encryption
7287  * info for the current stream.
7288  */
7290 {
7291  MOVFragmentStreamInfo *frag_stream_info;
7292  AVStream *st;
7293  int i;
7294 
7295  frag_stream_info = get_current_frag_stream_info(&c->frag_index);
7296  if (frag_stream_info) {
7297  for (i = 0; i < c->fc->nb_streams; i++) {
7298  *sc = c->fc->streams[i]->priv_data;
7299  if ((*sc)->id == frag_stream_info->id) {
7300  st = c->fc->streams[i];
7301  break;
7302  }
7303  }
7304  if (i == c->fc->nb_streams)
7305  return 0;
7306  *sc = st->priv_data;
7307 
7308  if (!frag_stream_info->encryption_index) {
7309  // If this stream isn't encrypted, don't create the index.
7310  if (!(*sc)->cenc.default_encrypted_sample)
7311  return 0;
7312  frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7313  if (!frag_stream_info->encryption_index)
7314  return AVERROR(ENOMEM);
7315  }
7316  *encryption_index = frag_stream_info->encryption_index;
7317  return 1;
7318  } else {
7319  // No current track fragment, using stream level encryption info.
7320 
7321  if (c->fc->nb_streams < 1)
7322  return 0;
7323  st = c->fc->streams[c->fc->nb_streams - 1];
7324  *sc = st->priv_data;
7325 
7326  if (!(*sc)->cenc.encryption_index) {
7327  // If this stream isn't encrypted, don't create the index.
7328  if (!(*sc)->cenc.default_encrypted_sample)
7329  return 0;
7330  (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
7331  if (!(*sc)->cenc.encryption_index)
7332  return AVERROR(ENOMEM);
7333  }
7334 
7335  *encryption_index = (*sc)->cenc.encryption_index;
7336  return 1;
7337  }
7338 }
7339 
7341 {
7342  int i, ret;
7343  unsigned int subsample_count;
7344  AVSubsampleEncryptionInfo *subsamples;
7345 
7346  if (!sc->cenc.default_encrypted_sample) {
7347  av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
7348  return AVERROR_INVALIDDATA;
7349  }
7350 
7351  if (sc->cenc.per_sample_iv_size || use_subsamples) {
7353  if (!*sample)
7354  return AVERROR(ENOMEM);
7355  } else
7356  *sample = NULL;
7357 
7358  if (sc->cenc.per_sample_iv_size != 0) {
7359  if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
7360  av_log(c->fc, AV_LOG_ERROR, "failed to read the initialization vector\n");
7362  *sample = NULL;
7363  return ret;
7364  }
7365  }
7366 
7367  if (use_subsamples) {
7368  subsample_count = avio_rb16(pb);
7369  av_free((*sample)->subsamples);
7370  (*sample)->subsamples = av_calloc(subsample_count, sizeof(*subsamples));
7371  if (!(*sample)->subsamples) {
7373  *sample = NULL;
7374  return AVERROR(ENOMEM);
7375  }
7376 
7377  for (i = 0; i < subsample_count && !pb->eof_reached; i++) {
7378  (*sample)->subsamples[i].bytes_of_clear_data = avio_rb16(pb);
7379  (*sample)->subsamples[i].bytes_of_protected_data = avio_rb32(pb);
7380  }
7381 
7382  if (pb->eof_reached) {
7383  av_log(c->fc, AV_LOG_ERROR, "hit EOF while reading sub-sample encryption info\n");
7385  *sample = NULL;
7386  return AVERROR_INVALIDDATA;
7387  }
7388  (*sample)->subsample_count = subsample_count;
7389  }
7390 
7391  return 0;
7392 }
7393 
7395 {
7396  AVEncryptionInfo **encrypted_samples;
7397  MOVEncryptionIndex *encryption_index;
7398  MOVStreamContext *sc;
7399  int use_subsamples, ret;
7400  unsigned int sample_count, i, alloc_size = 0;
7401 
7402  ret = get_current_encryption_info(c, &encryption_index, &sc);
7403  if (ret != 1)
7404  return ret;
7405 
7406  if (encryption_index->nb_encrypted_samples) {
7407  // This can happen if we have both saio/saiz and senc atoms.
7408  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in senc\n");
7409  return 0;
7410  }
7411 
7412  avio_r8(pb); /* version */
7413  use_subsamples = avio_rb24(pb) & 0x02; /* flags */
7414 
7415  sample_count = avio_rb32(pb);
7416  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7417  return AVERROR(ENOMEM);
7418 
7419  for (i = 0; i < sample_count; i++) {
7420  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7421  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7422  min_samples * sizeof(*encrypted_samples));
7423  if (encrypted_samples) {
7424  encryption_index->encrypted_samples = encrypted_samples;
7425 
7427  c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
7428  } else {
7429  ret = AVERROR(ENOMEM);
7430  }
7431  if (pb->eof_reached) {
7432  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading senc\n");
7433  if (ret >= 0)
7434  av_encryption_info_free(encryption_index->encrypted_samples[i]);
7436  }
7437 
7438  if (ret < 0) {
7439  for (; i > 0; i--)
7440  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7441  av_freep(&encryption_index->encrypted_samples);
7442  return ret;
7443  }
7444  }
7445  encryption_index->nb_encrypted_samples = sample_count;
7446 
7447  return 0;
7448 }
7449 
7451 {
7452  AVEncryptionInfo **sample, **encrypted_samples;
7453  int64_t prev_pos;
7454  size_t sample_count, sample_info_size, i;
7455  int ret = 0;
7456  unsigned int alloc_size = 0;
7457 
7458  if (encryption_index->nb_encrypted_samples)
7459  return 0;
7460  sample_count = encryption_index->auxiliary_info_sample_count;
7461  if (encryption_index->auxiliary_offsets_count != 1) {
7462  av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n");
7463  return AVERROR_PATCHWELCOME;
7464  }
7465  if (sample_count >= INT_MAX / sizeof(*encrypted_samples))
7466  return AVERROR(ENOMEM);
7467 
7468  prev_pos = avio_tell(pb);
7469  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
7470  avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[0]) {
7471  av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n");
7472  goto finish;
7473  }
7474 
7475  for (i = 0; i < sample_count && !pb->eof_reached; i++) {
7476  unsigned int min_samples = FFMIN(FFMAX(i + 1, 1024 * 1024), sample_count);
7477  encrypted_samples = av_fast_realloc(encryption_index->encrypted_samples, &alloc_size,
7478  min_samples * sizeof(*encrypted_samples));
7479  if (!encrypted_samples) {
7480  ret = AVERROR(ENOMEM);
7481  goto finish;
7482  }
7483  encryption_index->encrypted_samples = encrypted_samples;
7484 
7485  sample = &encryption_index->encrypted_samples[i];
7486  sample_info_size = encryption_index->auxiliary_info_default_size
7487  ? encryption_index->auxiliary_info_default_size
7488  : encryption_index->auxiliary_info_sizes[i];
7489 
7490  ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
7491  if (ret < 0)
7492  goto finish;
7493  }
7494  if (pb->eof_reached) {
7495  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading auxiliary info\n");
7497  } else {
7498  encryption_index->nb_encrypted_samples = sample_count;
7499  }
7500 
7501 finish:
7502  avio_seek(pb, prev_pos, SEEK_SET);
7503  if (ret < 0) {
7504  for (; i > 0; i--) {
7505  av_encryption_info_free(encryption_index->encrypted_samples[i - 1]);
7506  }
7507  av_freep(&encryption_index->encrypted_samples);
7508  }
7509  return ret;
7510 }
7511 
7513 {
7514  MOVEncryptionIndex *encryption_index;
7515  MOVStreamContext *sc;
7516  int ret;
7517  unsigned int sample_count, aux_info_type, aux_info_param;
7518 
7519  ret = get_current_encryption_info(c, &encryption_index, &sc);
7520  if (ret != 1)
7521  return ret;
7522 
7523  if (encryption_index->nb_encrypted_samples) {
7524  // This can happen if we have both saio/saiz and senc atoms.
7525  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saiz\n");
7526  return 0;
7527  }
7528 
7529  if (encryption_index->auxiliary_info_sample_count) {
7530  av_log(c->fc, AV_LOG_ERROR, "Duplicate saiz atom\n");
7531  return AVERROR_INVALIDDATA;
7532  }
7533 
7534  avio_r8(pb); /* version */
7535  if (avio_rb24(pb) & 0x01) { /* flags */
7536  aux_info_type = avio_rb32(pb);
7537  aux_info_param = avio_rb32(pb);
7538  if (sc->cenc.default_encrypted_sample) {
7539  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7540  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type\n");
7541  return 0;
7542  }
7543  if (aux_info_param != 0) {
7544  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saiz box with non-zero aux_info_type_parameter\n");
7545  return 0;
7546  }
7547  } else {
7548  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7549  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7550  aux_info_type == MKBETAG('c','e','n','s') ||
7551  aux_info_type == MKBETAG('c','b','c','1') ||
7552  aux_info_type == MKBETAG('c','b','c','s')) &&
7553  aux_info_param == 0) {
7554  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saiz without schm/tenc\n");
7555  return AVERROR_INVALIDDATA;
7556  } else {
7557  return 0;
7558  }
7559  }
7560  } else if (!sc->cenc.default_encrypted_sample) {
7561  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7562  return 0;
7563  }
7564 
7565  encryption_index->auxiliary_info_default_size = avio_r8(pb);
7566  sample_count = avio_rb32(pb);
7567 
7568  if (encryption_index->auxiliary_info_default_size == 0) {
7569  if (sample_count == 0)
7570  return AVERROR_INVALIDDATA;
7571 
7572  encryption_index->auxiliary_info_sizes = av_malloc(sample_count);
7573  if (!encryption_index->auxiliary_info_sizes)
7574  return AVERROR(ENOMEM);
7575 
7576  ret = avio_read(pb, encryption_index->auxiliary_info_sizes, sample_count);
7577  if (ret != sample_count) {
7578  av_freep(&encryption_index->auxiliary_info_sizes);
7579 
7580  if (ret >= 0)
7582  av_log(c->fc, AV_LOG_ERROR, "Failed to read the auxiliary info, %s\n",
7583  av_err2str(ret));
7584  return ret;
7585  }
7586  }
7587  encryption_index->auxiliary_info_sample_count = sample_count;
7588 
7589  if (encryption_index->auxiliary_offsets_count) {
7590  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
7591  }
7592 
7593  return 0;
7594 }
7595 
7597 {
7598  uint64_t *auxiliary_offsets;
7599  MOVEncryptionIndex *encryption_index;
7600  MOVStreamContext *sc;
7601  int i, ret;
7602  unsigned int version, entry_count, aux_info_type, aux_info_param;
7603  unsigned int alloc_size = 0;
7604 
7605  ret = get_current_encryption_info(c, &encryption_index, &sc);
7606  if (ret != 1)
7607  return ret;
7608 
7609  if (encryption_index->nb_encrypted_samples) {
7610  // This can happen if we have both saio/saiz and senc atoms.
7611  av_log(c->fc, AV_LOG_DEBUG, "Ignoring duplicate encryption info in saio\n");
7612  return 0;
7613  }
7614 
7615  if (encryption_index->auxiliary_offsets_count) {
7616  av_log(c->fc, AV_LOG_ERROR, "Duplicate saio atom\n");
7617  return AVERROR_INVALIDDATA;
7618  }
7619 
7620  version = avio_r8(pb); /* version */
7621  if (avio_rb24(pb) & 0x01) { /* flags */
7622  aux_info_type = avio_rb32(pb);
7623  aux_info_param = avio_rb32(pb);
7624  if (sc->cenc.default_encrypted_sample) {
7625  if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
7626  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type\n");
7627  return 0;
7628  }
7629  if (aux_info_param != 0) {
7630  av_log(c->fc, AV_LOG_DEBUG, "Ignoring saio box with non-zero aux_info_type_parameter\n");
7631  return 0;
7632  }
7633  } else {
7634  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7635  if ((aux_info_type == MKBETAG('c','e','n','c') ||
7636  aux_info_type == MKBETAG('c','e','n','s') ||
7637  aux_info_type == MKBETAG('c','b','c','1') ||
7638  aux_info_type == MKBETAG('c','b','c','s')) &&
7639  aux_info_param == 0) {
7640  av_log(c->fc, AV_LOG_ERROR, "Saw encrypted saio without schm/tenc\n");
7641  return AVERROR_INVALIDDATA;
7642  } else {
7643  return 0;
7644  }
7645  }
7646  } else if (!sc->cenc.default_encrypted_sample) {
7647  // Didn't see 'schm' or 'tenc', so this isn't encrypted.
7648  return 0;
7649  }
7650 
7651  entry_count = avio_rb32(pb);
7652  if (entry_count >= INT_MAX / sizeof(*auxiliary_offsets))
7653  return AVERROR(ENOMEM);
7654 
7655  for (i = 0; i < entry_count && !pb->eof_reached; i++) {
7656  unsigned int min_offsets = FFMIN(FFMAX(i + 1, 1024), entry_count);
7657  auxiliary_offsets = av_fast_realloc(
7658  encryption_index->auxiliary_offsets, &alloc_size,
7659  min_offsets * sizeof(*auxiliary_offsets));
7660  if (!auxiliary_offsets) {
7661  av_freep(&encryption_index->auxiliary_offsets);
7662  return AVERROR(ENOMEM);
7663  }
7664  encryption_index->auxiliary_offsets = auxiliary_offsets;
7665 
7666  if (version == 0) {
7667  encryption_index->auxiliary_offsets[i] = avio_rb32(pb);
7668  } else {
7669  encryption_index->auxiliary_offsets[i] = avio_rb64(pb);
7670  }
7671  if (c->frag_index.current >= 0) {
7672  encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset;
7673  }
7674  }
7675 
7676  if (pb->eof_reached) {
7677  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading saio\n");
7678  av_freep(&encryption_index->auxiliary_offsets);
7679  return AVERROR_INVALIDDATA;
7680  }
7681 
7682  encryption_index->auxiliary_offsets_count = entry_count;
7683 
7684  if (encryption_index->auxiliary_info_sample_count) {
7685  return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
7686  }
7687 
7688  return 0;
7689 }
7690 
7692 {
7693  AVEncryptionInitInfo *info, *old_init_info;
7694  uint8_t **key_ids;
7695  AVStream *st;
7696  const AVPacketSideData *old_side_data;
7697  uint8_t *side_data, *extra_data;
7698  size_t side_data_size;
7699  int ret = 0;
7700  unsigned int version, kid_count, extra_data_size, alloc_size = 0;
7701 
7702  if (c->fc->nb_streams < 1)
7703  return 0;
7704  st = c->fc->streams[c->fc->nb_streams-1];
7705 
7706  version = avio_r8(pb); /* version */
7707  avio_rb24(pb); /* flags */
7708 
7709  info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
7710  /* key_id_size */ 16, /* data_size */ 0);
7711  if (!info)
7712  return AVERROR(ENOMEM);
7713 
7714  if ((ret = ffio_read_size(pb, info->system_id, 16)) < 0) {
7715  av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
7716  goto finish;
7717  }
7718 
7719  if (version > 0) {
7720  kid_count = avio_rb32(pb);
7721  if (kid_count >= INT_MAX / sizeof(*key_ids)) {
7722  ret = AVERROR(ENOMEM);
7723  goto finish;
7724  }
7725 
7726  for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
7727  unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
7728  key_ids = av_fast_realloc(info->key_ids, &alloc_size,
7729  min_kid_count * sizeof(*key_ids));
7730  if (!key_ids) {
7731  ret = AVERROR(ENOMEM);
7732  goto finish;
7733  }
7734  info->key_ids = key_ids;
7735 
7736  info->key_ids[i] = av_mallocz(16);
7737  if (!info->key_ids[i]) {
7738  ret = AVERROR(ENOMEM);
7739  goto finish;
7740  }
7741  info->num_key_ids = i + 1;
7742 
7743  if ((ret = ffio_read_size(pb, info->key_ids[i], 16)) < 0) {
7744  av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
7745  goto finish;
7746  }
7747  }
7748 
7749  if (pb->eof_reached) {
7750  av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
7752  goto finish;
7753  }
7754  }
7755 
7756  extra_data_size = avio_rb32(pb);
7757  extra_data = av_malloc(extra_data_size);
7758  if (!extra_data) {
7759  ret = AVERROR(ENOMEM);
7760  goto finish;
7761  }
7762  ret = avio_read(pb, extra_data, extra_data_size);
7763  if (ret != extra_data_size) {
7764  av_free(extra_data);
7765 
7766  if (ret >= 0)
7768  goto finish;
7769  }
7770 
7771  av_freep(&info->data); // malloc(0) may still allocate something.
7772  info->data = extra_data;
7773  info->data_size = extra_data_size;
7774 
7775  // If there is existing initialization data, append to the list.
7778  if (old_side_data) {
7779  old_init_info = av_encryption_init_info_get_side_data(old_side_data->data, old_side_data->size);
7780  if (old_init_info) {
7781  // Append to the end of the list.
7782  for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
7783  if (!cur->next) {
7784  cur->next = info;
7785  break;
7786  }
7787  }
7788  info = old_init_info;
7789  } else {
7790  // Assume existing side-data will be valid, so the only error we could get is OOM.
7791  ret = AVERROR(ENOMEM);
7792  goto finish;
7793  }
7794  }
7795 
7796  side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
7797  if (!side_data) {
7798  ret = AVERROR(ENOMEM);
7799  goto finish;
7800  }
7804  side_data, side_data_size, 0))
7805  av_free(side_data);
7806 
7807 finish:
7809  return ret;
7810 }
7811 
7813 {
7814  AVStream *st;
7815  MOVStreamContext *sc;
7816 
7817  if (c->fc->nb_streams < 1)
7818  return 0;
7819  st = c->fc->streams[c->fc->nb_streams-1];
7820  sc = st->priv_data;
7821 
7822  if (sc->pseudo_stream_id != 0) {
7823  av_log(c->fc, AV_LOG_ERROR, "schm boxes are only supported in first sample descriptor\n");
7824  return AVERROR_PATCHWELCOME;
7825  }
7826 
7827  if (atom.size < 8)
7828  return AVERROR_INVALIDDATA;
7829 
7830  avio_rb32(pb); /* version and flags */
7831 
7832  if (!sc->cenc.default_encrypted_sample) {
7834  if (!sc->cenc.default_encrypted_sample) {
7835  return AVERROR(ENOMEM);
7836  }
7837  }
7838 
7840  return 0;
7841 }
7842 
7844 {
7845  AVStream *st;
7846  MOVStreamContext *sc;
7847  unsigned int version, pattern, is_protected, iv_size;
7848 
7849  if (c->fc->nb_streams < 1)
7850  return 0;
7851  st = c->fc->streams[c->fc->nb_streams-1];
7852  sc = st->priv_data;
7853 
7854  if (sc->pseudo_stream_id != 0) {
7855  av_log(c->fc, AV_LOG_ERROR, "tenc atom are only supported in first sample descriptor\n");
7856  return AVERROR_PATCHWELCOME;
7857  }
7858 
7859  if (!sc->cenc.default_encrypted_sample) {
7861  if (!sc->cenc.default_encrypted_sample) {
7862  return AVERROR(ENOMEM);
7863  }
7864  }
7865 
7866  if (atom.size < 20)
7867  return AVERROR_INVALIDDATA;
7868 
7869  version = avio_r8(pb); /* version */
7870  avio_rb24(pb); /* flags */
7871 
7872  avio_r8(pb); /* reserved */
7873  pattern = avio_r8(pb);
7874 
7875  if (version > 0) {
7876  sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
7877  sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
7878  }
7879 
7880  is_protected = avio_r8(pb);
7881  if (is_protected && !sc->cenc.encryption_index) {
7882  // The whole stream should be by-default encrypted.
7884  if (!sc->cenc.encryption_index)
7885  return AVERROR(ENOMEM);
7886  }
7887  sc->cenc.per_sample_iv_size = avio_r8(pb);
7888  if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
7889  sc->cenc.per_sample_iv_size != 16) {
7890  av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
7891  return AVERROR_INVALIDDATA;
7892  }
7893  if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
7894  av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
7895  return AVERROR_INVALIDDATA;
7896  }
7897 
7898  if (is_protected && !sc->cenc.per_sample_iv_size) {
7899  iv_size = avio_r8(pb);
7900  if (iv_size != 8 && iv_size != 16) {
7901  av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in tenc atom\n");
7902  return AVERROR_INVALIDDATA;
7903  }
7904 
7905  if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
7906  av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
7907  return AVERROR_INVALIDDATA;
7908  }
7909  }
7910 
7911  return 0;
7912 }
7913 
7915 {
7916  AVStream *st;
7917  int last, type, size, ret;
7918  uint8_t buf[4];
7919 
7920  if (c->fc->nb_streams < 1)
7921  return 0;
7922  st = c->fc->streams[c->fc->nb_streams-1];
7923 
7924  if ((uint64_t)atom.size > (1<<30) || atom.size < 42)
7925  return AVERROR_INVALIDDATA;
7926 
7927  /* Check FlacSpecificBox version. */
7928  if (avio_r8(pb) != 0)
7929  return AVERROR_INVALIDDATA;
7930 
7931  avio_rb24(pb); /* Flags */
7932 
7933  if (avio_read(pb, buf, sizeof(buf)) != sizeof(buf)) {
7934  av_log(c->fc, AV_LOG_ERROR, "failed to read FLAC metadata block header\n");
7935  return pb->error < 0 ? pb->error : AVERROR_INVALIDDATA;
7936  }
7937  flac_parse_block_header(buf, &last, &type, &size);
7938 
7940  av_log(c->fc, AV_LOG_ERROR, "STREAMINFO must be first FLACMetadataBlock\n");
7941  return AVERROR_INVALIDDATA;
7942  }
7943 
7944  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
7945  if (ret < 0)
7946  return ret;
7947 
7948  if (!last)
7949  av_log(c->fc, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n");
7950 
7951  return 0;
7952 }
7953 
7955 {
7956  int i, ret;
7957  int bytes_of_protected_data;
7958 
7959  if (!sc->cenc.aes_ctr) {
7960  /* initialize the cipher */
7961  sc->cenc.aes_ctr = av_aes_ctr_alloc();
7962  if (!sc->cenc.aes_ctr) {
7963  return AVERROR(ENOMEM);
7964  }
7965 
7966  ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
7967  if (ret < 0) {
7968  return ret;
7969  }
7970  }
7971 
7973 
7974  if (!sample->subsample_count) {
7975  /* decrypt the whole packet */
7977  return 0;
7978  }
7979 
7980  for (i = 0; i < sample->subsample_count; i++) {
7981  if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
7982  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
7983  return AVERROR_INVALIDDATA;
7984  }
7985 
7986  /* skip the clear bytes */
7987  input += sample->subsamples[i].bytes_of_clear_data;
7988  size -= sample->subsamples[i].bytes_of_clear_data;
7989 
7990  /* decrypt the encrypted bytes */
7991 
7992  bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data;
7993  av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data);
7994 
7995  input += bytes_of_protected_data;
7996  size -= bytes_of_protected_data;
7997  }
7998 
7999  if (size > 0) {
8000  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8001  return AVERROR_INVALIDDATA;
8002  }
8003 
8004  return 0;
8005 }
8006 
8008 {
8009  int i, ret;
8010  int num_of_encrypted_blocks;
8011  uint8_t iv[16];
8012 
8013  if (!sc->cenc.aes_ctx) {
8014  /* initialize the cipher */
8015  sc->cenc.aes_ctx = av_aes_alloc();
8016  if (!sc->cenc.aes_ctx) {
8017  return AVERROR(ENOMEM);
8018  }
8019 
8020  ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
8021  if (ret < 0) {
8022  return ret;
8023  }
8024  }
8025 
8026  memcpy(iv, sample->iv, 16);
8027 
8028  /* whole-block full sample encryption */
8029  if (!sample->subsample_count) {
8030  /* decrypt the whole packet */
8031  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8032  return 0;
8033  }
8034 
8035  for (i = 0; i < sample->subsample_count; i++) {
8036  if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
8037  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8038  return AVERROR_INVALIDDATA;
8039  }
8040 
8041  if (sample->subsamples[i].bytes_of_protected_data % 16) {
8042  av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is not a multiple of 16\n");
8043  return AVERROR_INVALIDDATA;
8044  }
8045 
8046  /* skip the clear bytes */
8047  input += sample->subsamples[i].bytes_of_clear_data;
8048  size -= sample->subsamples[i].bytes_of_clear_data;
8049 
8050  /* decrypt the encrypted bytes */
8051  num_of_encrypted_blocks = sample->subsamples[i].bytes_of_protected_data/16;
8052  if (num_of_encrypted_blocks > 0) {
8053  av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1);
8054  }
8055  input += sample->subsamples[i].bytes_of_protected_data;
8056  size -= sample->subsamples[i].bytes_of_protected_data;
8057  }
8058 
8059  if (size > 0) {
8060  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8061  return AVERROR_INVALIDDATA;
8062  }
8063 
8064  return 0;
8065 }
8066 
8068 {
8069  int i, ret, rem_bytes;
8070  uint8_t *data;
8071 
8072  if (!sc->cenc.aes_ctr) {
8073  /* initialize the cipher */
8074  sc->cenc.aes_ctr = av_aes_ctr_alloc();
8075  if (!sc->cenc.aes_ctr) {
8076  return AVERROR(ENOMEM);
8077  }
8078 
8079  ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
8080  if (ret < 0) {
8081  return ret;
8082  }
8083  }
8084 
8086 
8087  /* whole-block full sample encryption */
8088  if (!sample->subsample_count) {
8089  /* decrypt the whole packet */
8091  return 0;
8092  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8093  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cens' scheme\n");
8094  return AVERROR_INVALIDDATA;
8095  }
8096 
8097  for (i = 0; i < sample->subsample_count; i++) {
8098  if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
8099  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8100  return AVERROR_INVALIDDATA;
8101  }
8102 
8103  /* skip the clear bytes */
8104  input += sample->subsamples[i].bytes_of_clear_data;
8105  size -= sample->subsamples[i].bytes_of_clear_data;
8106 
8107  /* decrypt the encrypted bytes */
8108  data = input;
8109  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8110  while (rem_bytes > 0) {
8111  if (rem_bytes < 16*sample->crypt_byte_block) {
8112  break;
8113  }
8114  av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block);
8115  data += 16*sample->crypt_byte_block;
8116  rem_bytes -= 16*sample->crypt_byte_block;
8117  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8118  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8119  }
8120  input += sample->subsamples[i].bytes_of_protected_data;
8121  size -= sample->subsamples[i].bytes_of_protected_data;
8122  }
8123 
8124  if (size > 0) {
8125  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8126  return AVERROR_INVALIDDATA;
8127  }
8128 
8129  return 0;
8130 }
8131 
8133 {
8134  int i, ret, rem_bytes;
8135  uint8_t iv[16];
8136  uint8_t *data;
8137 
8138  if (!sc->cenc.aes_ctx) {
8139  /* initialize the cipher */
8140  sc->cenc.aes_ctx = av_aes_alloc();
8141  if (!sc->cenc.aes_ctx) {
8142  return AVERROR(ENOMEM);
8143  }
8144 
8145  ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
8146  if (ret < 0) {
8147  return ret;
8148  }
8149  }
8150 
8151  /* whole-block full sample encryption */
8152  if (!sample->subsample_count) {
8153  /* decrypt the whole packet */
8154  memcpy(iv, sample->iv, 16);
8155  av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
8156  return 0;
8157  } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
8158  av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cbcs' scheme\n");
8159  return AVERROR_INVALIDDATA;
8160  }
8161 
8162  for (i = 0; i < sample->subsample_count; i++) {
8163  if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) {
8164  av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n");
8165  return AVERROR_INVALIDDATA;
8166  }
8167 
8168  /* skip the clear bytes */
8169  input += sample->subsamples[i].bytes_of_clear_data;
8170  size -= sample->subsamples[i].bytes_of_clear_data;
8171 
8172  /* decrypt the encrypted bytes */
8173  memcpy(iv, sample->iv, 16);
8174  data = input;
8175  rem_bytes = sample->subsamples[i].bytes_of_protected_data;
8176  while (rem_bytes > 0) {
8177  if (rem_bytes < 16*sample->crypt_byte_block) {
8178  break;
8179  }
8180  av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1);
8181  data += 16*sample->crypt_byte_block;
8182  rem_bytes -= 16*sample->crypt_byte_block;
8183  data += FFMIN(16*sample->skip_byte_block, rem_bytes);
8184  rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
8185  }
8186  input += sample->subsamples[i].bytes_of_protected_data;
8187  size -= sample->subsamples[i].bytes_of_protected_data;
8188  }
8189 
8190  if (size > 0) {
8191  av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n");
8192  return AVERROR_INVALIDDATA;
8193  }
8194 
8195  return 0;
8196 }
8197 
8199 {
8200  if (sample->scheme == MKBETAG('c','e','n','c') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8201  return cenc_scheme_decrypt(c, sc, sample, input, size);
8202  } else if (sample->scheme == MKBETAG('c','b','c','1') && !sample->crypt_byte_block && !sample->skip_byte_block) {
8203  return cbc1_scheme_decrypt(c, sc, sample, input, size);
8204  } else if (sample->scheme == MKBETAG('c','e','n','s')) {
8205  return cens_scheme_decrypt(c, sc, sample, input, size);
8206  } else if (sample->scheme == MKBETAG('c','b','c','s')) {
8207  return cbcs_scheme_decrypt(c, sc, sample, input, size);
8208  } else {
8209  av_log(c->fc, AV_LOG_ERROR, "invalid encryption scheme\n");
8210  return AVERROR_INVALIDDATA;
8211  }
8212 }
8213 
8215 {
8216  int current = frag_index->current;
8217 
8218  if (!frag_index->nb_items)
8219  return NULL;
8220 
8221  // Check frag_index->current is the right one for pkt. It can out of sync.
8222  if (current >= 0 && current < frag_index->nb_items) {
8223  if (frag_index->item[current].moof_offset < pkt->pos &&
8224  (current + 1 == frag_index->nb_items ||
8225  frag_index->item[current + 1].moof_offset > pkt->pos))
8226  return get_frag_stream_info(frag_index, current, id);
8227  }
8228 
8229 
8230  for (int i = 0; i < frag_index->nb_items; i++) {
8231  if (frag_index->item[i].moof_offset > pkt->pos)
8232  break;
8233  current = i;
8234  }
8235  frag_index->current = current;
8236  return get_frag_stream_info(frag_index, current, id);
8237 }
8238 
8239 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
8240 {
8241  MOVFragmentStreamInfo *frag_stream_info;
8242  MOVEncryptionIndex *encryption_index;
8243  AVEncryptionInfo *encrypted_sample;
8244  int encrypted_index, ret;
8245 
8246  frag_stream_info = get_frag_stream_info_from_pkt(&mov->frag_index, pkt, sc->id);
8247  encrypted_index = current_index;
8248  encryption_index = NULL;
8249  if (frag_stream_info) {
8250  // Note this only supports encryption info in the first sample descriptor.
8251  if (frag_stream_info->stsd_id == 1) {
8252  if (frag_stream_info->encryption_index) {
8253  encrypted_index = current_index - frag_stream_info->index_base;
8254  encryption_index = frag_stream_info->encryption_index;
8255  } else {
8256  encryption_index = sc->cenc.encryption_index;
8257  }
8258  }
8259  } else {
8260  encryption_index = sc->cenc.encryption_index;
8261  }
8262 
8263  if (encryption_index) {
8264  if (encryption_index->auxiliary_info_sample_count &&
8265  !encryption_index->nb_encrypted_samples) {
8266  av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n");
8267  return AVERROR_INVALIDDATA;
8268  }
8269  if (encryption_index->auxiliary_offsets_count &&
8270  !encryption_index->nb_encrypted_samples) {
8271  av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n");
8272  return AVERROR_INVALIDDATA;
8273  }
8274 
8275  encrypted_sample = NULL;
8276  if (!encryption_index->nb_encrypted_samples) {
8277  // Full-sample encryption with default settings.
8278  encrypted_sample = sc->cenc.default_encrypted_sample;
8279  } else if (encrypted_index >= 0 && encrypted_index < encryption_index->nb_encrypted_samples) {
8280  // Per-sample setting override.
8281  encrypted_sample = encryption_index->encrypted_samples[encrypted_index];
8282  if (!encrypted_sample) {
8283  encrypted_sample = sc->cenc.default_encrypted_sample;
8284  }
8285  }
8286 
8287  if (!encrypted_sample) {
8288  av_log(mov->fc, AV_LOG_ERROR, "Incorrect number of samples in encryption info\n");
8289  return AVERROR_INVALIDDATA;
8290  }
8291 
8292  if (mov->decryption_key) {
8293  return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
8294  } else {
8295  size_t size;
8296  uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
8297  if (!side_data)
8298  return AVERROR(ENOMEM);
8300  if (ret < 0)
8301  av_free(side_data);
8302  return ret;
8303  }
8304  }
8305 
8306  return 0;
8307 }
8308 
8310 {
8311  const int OPUS_SEEK_PREROLL_MS = 80;
8312  int ret;
8313  AVStream *st;
8314  size_t size;
8315  uint16_t pre_skip;
8316 
8317  if (c->fc->nb_streams < 1)
8318  return 0;
8319  st = c->fc->streams[c->fc->nb_streams-1];
8320 
8321  if ((uint64_t)atom.size > (1<<30) || atom.size < 11)
8322  return AVERROR_INVALIDDATA;
8323 
8324  /* Check OpusSpecificBox version. */
8325  if (avio_r8(pb) != 0) {
8326  av_log(c->fc, AV_LOG_ERROR, "unsupported OpusSpecificBox version\n");
8327  return AVERROR_INVALIDDATA;
8328  }
8329 
8330  /* OpusSpecificBox size plus magic for Ogg OpusHead header. */
8331  size = atom.size + 8;
8332 
8333  if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
8334  return ret;
8335 
8336  AV_WL32A(st->codecpar->extradata, MKTAG('O','p','u','s'));
8337  AV_WL32A(st->codecpar->extradata + 4, MKTAG('H','e','a','d'));
8338  AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */
8339  avio_read(pb, st->codecpar->extradata + 9, size - 9);
8340 
8341  /* OpusSpecificBox is stored in big-endian, but OpusHead is
8342  little-endian; aside from the preceeding magic and version they're
8343  otherwise currently identical. Data after output gain at offset 16
8344  doesn't need to be bytewapped. */
8345  pre_skip = AV_RB16A(st->codecpar->extradata + 10);
8346  AV_WL16A(st->codecpar->extradata + 10, pre_skip);
8347  AV_WL32A(st->codecpar->extradata + 12, AV_RB32A(st->codecpar->extradata + 12));
8348  AV_WL16A(st->codecpar->extradata + 16, AV_RB16A(st->codecpar->extradata + 16));
8349 
8350  st->codecpar->initial_padding = pre_skip;
8352  (AVRational){1, 1000},
8353  (AVRational){1, 48000});
8354 
8355  return 0;
8356 }
8357 
8359 {
8360  AVStream *st;
8361  unsigned format_info;
8362  int channel_assignment, channel_assignment1, channel_assignment2;
8363  int ratebits;
8364  uint64_t chmask;
8365 
8366  if (c->fc->nb_streams < 1)
8367  return 0;
8368  st = c->fc->streams[c->fc->nb_streams-1];
8369 
8370  if (atom.size < 10)
8371  return AVERROR_INVALIDDATA;
8372 
8373  format_info = avio_rb32(pb);
8374 
8375  ratebits = (format_info >> 28) & 0xF;
8376  channel_assignment1 = (format_info >> 15) & 0x1F;
8377  channel_assignment2 = format_info & 0x1FFF;
8378  if (channel_assignment2)
8379  channel_assignment = channel_assignment2;
8380  else
8381  channel_assignment = channel_assignment1;
8382 
8383  st->codecpar->frame_size = 40 << (ratebits & 0x7);
8384  st->codecpar->sample_rate = mlp_samplerate(ratebits);
8385 
8387  chmask = truehd_layout(channel_assignment);
8389 
8390  return 0;
8391 }
8392 
8394 {
8395  AVStream *st;
8396  uint8_t buf[ISOM_DVCC_DVVC_SIZE];
8397  int ret;
8398  int64_t read_size = atom.size;
8399 
8400  if (c->fc->nb_streams < 1)
8401  return 0;
8402  st = c->fc->streams[c->fc->nb_streams-1];
8403 
8404  // At most 24 bytes
8405  read_size = FFMIN(read_size, ISOM_DVCC_DVVC_SIZE);
8406 
8407  if ((ret = ffio_read_size(pb, buf, read_size)) < 0)
8408  return ret;
8409 
8410  return ff_isom_parse_dvcc_dvvc(c->fc, st, buf, read_size);
8411 }
8412 
8414 {
8415  AVStream *st;
8416  uint8_t *buf;
8417  int ret, old_size, num_arrays;
8418 
8419  if (c->fc->nb_streams < 1)
8420  return 0;
8421  st = c->fc->streams[c->fc->nb_streams-1];
8422 
8423  if (!st->codecpar->extradata_size)
8424  // TODO: handle lhvC when present before hvcC
8425  return 0;
8426 
8427  if (atom.size < 6 || st->codecpar->extradata_size < 23)
8428  return AVERROR_INVALIDDATA;
8429 
8431  if (!buf)
8432  return AVERROR(ENOMEM);
8433  memset(buf + atom.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
8434 
8435  ret = ffio_read_size(pb, buf, atom.size);
8436  if (ret < 0) {
8437  av_free(buf);
8438  av_log(c->fc, AV_LOG_WARNING, "lhvC atom truncated\n");
8439  return 0;
8440  }
8441 
8442  num_arrays = buf[5];
8443  old_size = st->codecpar->extradata_size;
8444  atom.size -= 8 /* account for mov_realloc_extradata offseting */
8445  + 6 /* lhvC bytes before the arrays*/;
8446 
8447  ret = mov_realloc_extradata(st->codecpar, atom);
8448  if (ret < 0) {
8449  av_free(buf);
8450  return ret;
8451  }
8452 
8453  st->codecpar->extradata[22] += num_arrays;
8454  memcpy(st->codecpar->extradata + old_size, buf + 6, atom.size + 8);
8455 
8457 
8458  av_free(buf);
8459  return 0;
8460 }
8461 
8463 {
8464  AVFormatContext *ctx = c->fc;
8465  AVStream *st = NULL;
8466  AVBPrint scheme_buf, value_buf;
8467  int64_t scheme_str_len = 0, value_str_len = 0;
8468  int version, flags, ret = AVERROR_BUG;
8469  int64_t size = atom.size;
8470 
8471  if (atom.size < 6)
8472  // 4 bytes for version + flags, 2x 1 byte for null
8473  return AVERROR_INVALIDDATA;
8474 
8475  if (c->fc->nb_streams < 1)
8476  return 0;
8477  st = c->fc->streams[c->fc->nb_streams-1];
8478 
8479  version = avio_r8(pb);
8480  flags = avio_rb24(pb);
8481  size -= 4;
8482 
8483  if (version != 0 || flags != 0) {
8485  "Unsupported 'kind' box with version %d, flags: %x",
8486  version, flags);
8487  return AVERROR_INVALIDDATA;
8488  }
8489 
8490  av_bprint_init(&scheme_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8491  av_bprint_init(&value_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
8492 
8493  if ((scheme_str_len = ff_read_string_to_bprint_overwrite(pb, &scheme_buf,
8494  size)) < 0) {
8495  ret = scheme_str_len;
8496  goto cleanup;
8497  }
8498 
8499  if (scheme_str_len + 1 >= size) {
8500  // we need to have another string, even if nullptr.
8501  // we check with + 1 since we expect that if size was not hit,
8502  // an additional null was read.
8504  goto cleanup;
8505  }
8506 
8507  size -= scheme_str_len + 1;
8508 
8509  if ((value_str_len = ff_read_string_to_bprint_overwrite(pb, &value_buf,
8510  size)) < 0) {
8511  ret = value_str_len;
8512  goto cleanup;
8513  }
8514 
8515  if (value_str_len == size) {
8516  // in case of no trailing null, box is not valid.
8518  goto cleanup;
8519  }
8520 
8522  "%s stream %d KindBox(scheme: %s, value: %s)\n",
8524  st->index,
8525  scheme_buf.str, value_buf.str);
8526 
8527  for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
8529  if (!av_strstart(scheme_buf.str, map.scheme_uri, NULL))
8530  continue;
8531 
8532  for (int j = 0; map.value_maps[j].disposition; j++) {
8533  const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
8534  if (!av_strstart(value_buf.str, value_map.value, NULL))
8535  continue;
8536 
8537  st->disposition |= value_map.disposition;
8538  }
8539  }
8540 
8541  ret = 0;
8542 
8543 cleanup:
8544 
8545  av_bprint_finalize(&scheme_buf, NULL);
8546  av_bprint_finalize(&value_buf, NULL);
8547 
8548  return ret;
8549 }
8550 
8552 {
8553  AVStream *st;
8554  AVChannelLayout ch_layout = { 0 };
8555  int ret, i, version, type;
8556  int ambisonic_order, channel_order, normalization, channel_count;
8557  int ambi_channels, non_diegetic_channels;
8558 
8559  if (c->fc->nb_streams < 1)
8560  return 0;
8561 
8562  st = c->fc->streams[c->fc->nb_streams - 1];
8563 
8564  if (atom.size < 16) {
8565  av_log(c->fc, AV_LOG_ERROR, "SA3D audio box too small\n");
8566  return AVERROR_INVALIDDATA;
8567  }
8568 
8569  version = avio_r8(pb);
8570  if (version) {
8571  av_log(c->fc, AV_LOG_WARNING, "Unsupported SA3D box version %d\n", version);
8572  return 0;
8573  }
8574 
8575  type = avio_r8(pb);
8576  if (type & 0x7f) {
8577  av_log(c->fc, AV_LOG_WARNING,
8578  "Unsupported ambisonic type %d\n", type & 0x7f);
8579  return 0;
8580  }
8581  non_diegetic_channels = (type >> 7) * 2; // head_locked_stereo
8582 
8583  ambisonic_order = avio_rb32(pb);
8584 
8585  channel_order = avio_r8(pb);
8586  if (channel_order) {
8587  av_log(c->fc, AV_LOG_WARNING,
8588  "Unsupported channel_order %d\n", channel_order);
8589  return 0;
8590  }
8591 
8592  normalization = avio_r8(pb);
8593  if (normalization) {
8594  av_log(c->fc, AV_LOG_WARNING,
8595  "Unsupported normalization %d\n", normalization);
8596  return 0;
8597  }
8598 
8599  channel_count = avio_rb32(pb);
8600  if (ambisonic_order < 0 || ambisonic_order > 31 ||
8601  channel_count != ((ambisonic_order + 1LL) * (ambisonic_order + 1LL) +
8602  non_diegetic_channels)) {
8603  av_log(c->fc, AV_LOG_ERROR,
8604  "Invalid number of channels (%d / %d)\n",
8605  channel_count, ambisonic_order);
8606  return 0;
8607  }
8608  ambi_channels = channel_count - non_diegetic_channels;
8609 
8610  ret = av_channel_layout_custom_init(&ch_layout, channel_count);
8611  if (ret < 0)
8612  return 0;
8613 
8614  for (i = 0; i < channel_count; i++) {
8615  unsigned channel = avio_rb32(pb);
8616 
8617  if (channel >= channel_count) {
8618  av_log(c->fc, AV_LOG_ERROR, "Invalid channel index (%d / %d)\n",
8619  channel, ambisonic_order);
8620  av_channel_layout_uninit(&ch_layout);
8621  return 0;
8622  }
8623  if (channel >= ambi_channels)
8624  ch_layout.u.map[i].id = channel - ambi_channels;
8625  else
8626  ch_layout.u.map[i].id = AV_CHAN_AMBISONIC_BASE + channel;
8627  }
8628 
8630  if (ret < 0) {
8631  av_channel_layout_uninit(&ch_layout);
8632  return 0;
8633  }
8634 
8636  st->codecpar->ch_layout = ch_layout;
8637 
8638  return 0;
8639 }
8640 
8642 {
8643  AVStream *st;
8644  int version;
8645 
8646  if (c->fc->nb_streams < 1)
8647  return 0;
8648 
8649  st = c->fc->streams[c->fc->nb_streams - 1];
8650 
8651  if (atom.size < 5) {
8652  av_log(c->fc, AV_LOG_ERROR, "Empty SAND audio box\n");
8653  return AVERROR_INVALIDDATA;
8654  }
8655 
8656  version = avio_r8(pb);
8657  if (version) {
8658  av_log(c->fc, AV_LOG_WARNING, "Unsupported SAND box version %d\n", version);
8659  return 0;
8660  }
8661 
8663 
8664  return 0;
8665 }
8666 
8667 static int rb_size(AVIOContext *pb, int64_t *value, int size)
8668 {
8669  if (size == 0)
8670  *value = 0;
8671  else if (size == 1)
8672  *value = avio_r8(pb);
8673  else if (size == 2)
8674  *value = avio_rb16(pb);
8675  else if (size == 4)
8676  *value = avio_rb32(pb);
8677  else if (size == 8) {
8678  *value = avio_rb64(pb);
8679  if (*value < 0)
8680  return -1;
8681  } else
8682  return -1;
8683  return size;
8684 }
8685 
8687 {
8688  avio_rb32(pb); // version & flags.
8689  c->primary_item_id = avio_rb16(pb);
8690  av_log(c->fc, AV_LOG_TRACE, "pitm: primary_item_id %d\n", c->primary_item_id);
8691  return atom.size;
8692 }
8693 
8695 {
8696  c->idat_offset = avio_tell(pb);
8697  return 0;
8698 }
8699 
8701 {
8702  HEIFItem **heif_item;
8703  int version, offset_size, length_size, base_offset_size, index_size;
8704  int item_count, extent_count;
8705  int64_t base_offset, extent_offset, extent_length;
8706  uint8_t value;
8707 
8708  if (c->found_iloc) {
8709  av_log(c->fc, AV_LOG_INFO, "Duplicate iloc box found\n");
8710  return 0;
8711  }
8712 
8713  version = avio_r8(pb);
8714  avio_rb24(pb); // flags.
8715 
8716  value = avio_r8(pb);
8717  offset_size = (value >> 4) & 0xF;
8718  length_size = value & 0xF;
8719  value = avio_r8(pb);
8720  base_offset_size = (value >> 4) & 0xF;
8721  index_size = !version ? 0 : (value & 0xF);
8722  if (index_size) {
8723  avpriv_report_missing_feature(c->fc, "iloc: index_size != 0");
8724  return AVERROR_PATCHWELCOME;
8725  }
8726  item_count = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
8727 
8728  heif_item = av_realloc_array(c->heif_item, FFMAX(item_count, c->nb_heif_item), sizeof(*c->heif_item));
8729  if (!heif_item)
8730  return AVERROR(ENOMEM);
8731  c->heif_item = heif_item;
8732  if (item_count > c->nb_heif_item)
8733  memset(&c->heif_item[c->nb_heif_item], 0,
8734  sizeof(*c->heif_item) * (item_count - c->nb_heif_item));
8735  c->nb_heif_item = FFMAX(c->nb_heif_item, item_count);
8736 
8737  av_log(c->fc, AV_LOG_TRACE, "iloc: item_count %d\n", item_count);
8738  for (int i = 0; i < item_count; i++) {
8739  HEIFItem *item = c->heif_item[i];
8740  int item_id = (version < 2) ? avio_rb16(pb) : avio_rb32(pb);
8741  int offset_type = (version > 0) ? avio_rb16(pb) & 0xf : 0;
8742 
8743  if (avio_feof(pb))
8744  return AVERROR_INVALIDDATA;
8745  if (offset_type > 1) {
8746  avpriv_report_missing_feature(c->fc, "iloc offset type %d", offset_type);
8747  return AVERROR_PATCHWELCOME;
8748  }
8749 
8750  avio_rb16(pb); // data_reference_index.
8751  if (rb_size(pb, &base_offset, base_offset_size) < 0)
8752  return AVERROR_INVALIDDATA;
8753  extent_count = avio_rb16(pb);
8754  if (extent_count > 1) {
8755  // For still AVIF images, we only support one extent item.
8756  avpriv_report_missing_feature(c->fc, "iloc: extent_count > 1");
8757  return AVERROR_PATCHWELCOME;
8758  }
8759 
8760  if (rb_size(pb, &extent_offset, offset_size) < 0 ||
8761  rb_size(pb, &extent_length, length_size) < 0 ||
8762  base_offset > INT64_MAX - extent_offset)
8763  return AVERROR_INVALIDDATA;
8764 
8765  if (!item)
8766  item = c->heif_item[i] = av_mallocz(sizeof(*item));
8767  if (!item)
8768  return AVERROR(ENOMEM);
8769 
8770  item->item_id = item_id;
8771 
8772  if (offset_type == 1)
8773  item->is_idat_relative = 1;
8774  item->extent_length = extent_length;
8775  item->extent_offset = base_offset + extent_offset;
8776  av_log(c->fc, AV_LOG_TRACE, "iloc: item_idx %d, offset_type %d, "
8777  "extent_offset %"PRId64", extent_length %"PRId64"\n",
8778  i, offset_type, item->extent_offset, item->extent_length);
8779  }
8780 
8781  c->found_iloc = 1;
8782  return atom.size;
8783 }
8784 
8785 static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom, int idx)
8786 {
8787  HEIFItem *item;
8788  AVBPrint item_name;
8789  int64_t size = atom.size;
8790  uint32_t item_type;
8791  int item_id;
8792  int version, ret;
8793 
8794  version = avio_r8(pb);
8795  avio_rb24(pb); // flags.
8796  size -= 4;
8797  if (size < 0)
8798  return AVERROR_INVALIDDATA;
8799 
8800  if (version < 2) {
8801  avpriv_report_missing_feature(c->fc, "infe version < 2");
8802  avio_skip(pb, size);
8803  return 1;
8804  }
8805 
8806  item_id = version > 2 ? avio_rb32(pb) : avio_rb16(pb);
8807  avio_rb16(pb); // item_protection_index
8808  item_type = avio_rl32(pb);
8809  size -= 8;
8810  if (size < 1)
8811  return AVERROR_INVALIDDATA;
8812 
8815  if (ret < 0) {
8817  return ret;
8818  }
8819 
8820  av_log(c->fc, AV_LOG_TRACE, "infe: item_id %d, item_type %s, item_name %s\n",
8821  item_id, av_fourcc2str(item_type), item_name.str);
8822 
8823  size -= ret + 1;
8824  if (size > 0)
8825  avio_skip(pb, size);
8826 
8827  item = c->heif_item[idx];
8828  if (!item)
8829  item = c->heif_item[idx] = av_mallocz(sizeof(*item));
8830  if (!item)
8831  return AVERROR(ENOMEM);
8832 
8833  if (ret)
8834  av_bprint_finalize(&item_name, &c->heif_item[idx]->name);
8835  c->heif_item[idx]->item_id = item_id;
8836  c->heif_item[idx]->type = item_type;
8837 
8838  switch (item_type) {
8839  case MKTAG('a','v','0','1'):
8840  case MKTAG('h','v','c','1'):
8841  ret = heif_add_stream(c, c->heif_item[idx]);
8842  if (ret < 0)
8843  return ret;
8844  break;
8845  }
8846 
8847  return 0;
8848 }
8849 
8851 {
8852  HEIFItem **heif_item;
8853  int entry_count;
8854  int version, got_stream = 0, ret, i;
8855 
8856  if (c->found_iinf) {
8857  av_log(c->fc, AV_LOG_WARNING, "Duplicate iinf box found\n");
8858  return 0;
8859  }
8860 
8861  version = avio_r8(pb);
8862  avio_rb24(pb); // flags.
8863  entry_count = version ? avio_rb32(pb) : avio_rb16(pb);
8864 
8865  heif_item = av_realloc_array(c->heif_item, FFMAX(entry_count, c->nb_heif_item), sizeof(*c->heif_item));
8866  if (!heif_item)
8867  return AVERROR(ENOMEM);
8868  c->heif_item = heif_item;
8869  if (entry_count > c->nb_heif_item)
8870  memset(&c->heif_item[c->nb_heif_item], 0,
8871  sizeof(*c->heif_item) * (entry_count - c->nb_heif_item));
8872  c->nb_heif_item = FFMAX(c->nb_heif_item, entry_count);
8873 
8874  for (i = 0; i < entry_count; i++) {
8875  MOVAtom infe;
8876 
8877  infe.size = avio_rb32(pb) - 8;
8878  infe.type = avio_rl32(pb);
8879  if (avio_feof(pb)) {
8881  goto fail;
8882  }
8883  ret = mov_read_infe(c, pb, infe, i);
8884  if (ret < 0)
8885  goto fail;
8886  if (!ret)
8887  got_stream = 1;
8888  }
8889 
8890  c->found_iinf = got_stream;
8891  return 0;
8892 fail:
8893  for (; i >= 0; i--) {
8894  HEIFItem *item = c->heif_item[i];
8895 
8896  if (!item)
8897  continue;
8898 
8899  av_freep(&item->name);
8900  if (!item->st)
8901  continue;
8902 
8903  mov_free_stream_context(c->fc, item->st);
8904  ff_remove_stream(c->fc, item->st);
8905  item->st = NULL;
8906  }
8907  return ret;
8908 }
8909 
8911 {
8912  HEIFItem *item = NULL;
8913  HEIFGrid *grid;
8914  int entries, i;
8915  int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
8916 
8917  for (int i = 0; i < c->nb_heif_grid; i++) {
8918  if (c->heif_grid[i].item->item_id == from_item_id) {
8919  av_log(c->fc, AV_LOG_ERROR, "More than one 'dimg' box "
8920  "referencing the same Derived Image item\n");
8921  return AVERROR_INVALIDDATA;
8922  }
8923  }
8924  for (int i = 0; i < c->nb_heif_item; i++) {
8925  if (!c->heif_item[i] || c->heif_item[i]->item_id != from_item_id)
8926  continue;
8927  item = c->heif_item[i];
8928 
8929  switch (item->type) {
8930  case MKTAG('g','r','i','d'):
8931  case MKTAG('i','o','v','l'):
8932  break;
8933  default:
8934  avpriv_report_missing_feature(c->fc, "Derived Image item of type %s",
8935  av_fourcc2str(item->type));
8936  return 0;
8937  }
8938  break;
8939  }
8940  if (!item) {
8941  av_log(c->fc, AV_LOG_ERROR, "Missing grid information\n");
8942  return AVERROR_INVALIDDATA;
8943  }
8944 
8945  grid = av_realloc_array(c->heif_grid, c->nb_heif_grid + 1U,
8946  sizeof(*c->heif_grid));
8947  if (!grid)
8948  return AVERROR(ENOMEM);
8949  c->heif_grid = grid;
8950  grid = &grid[c->nb_heif_grid++];
8951 
8952  entries = avio_rb16(pb);
8953  grid->tile_id_list = av_malloc_array(entries, sizeof(*grid->tile_id_list));
8954  grid->tile_idx_list = av_calloc(entries, sizeof(*grid->tile_idx_list));
8955  grid->tile_item_list = av_calloc(entries, sizeof(*grid->tile_item_list));
8956  if (!grid->tile_id_list || !grid->tile_item_list || !grid->tile_idx_list)
8957  return AVERROR(ENOMEM);
8958  /* 'to' item ids */
8959  for (i = 0; i < entries; i++)
8960  grid->tile_id_list[i] = version ? avio_rb32(pb) : avio_rb16(pb);
8961  grid->nb_tiles = entries;
8962  grid->item = item;
8963 
8964  av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n",
8965  from_item_id, entries);
8966 
8967  return 0;
8968 }
8969 
8971 {
8972  int entries;
8973  int to_item_id, from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
8974 
8975  entries = avio_rb16(pb);
8976  if (entries > 1) {
8977  avpriv_request_sample(c->fc, "thmb in iref referencing several items");
8978  return AVERROR_PATCHWELCOME;
8979  }
8980  /* 'to' item ids */
8981  to_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
8982 
8983  if (to_item_id != c->primary_item_id)
8984  return 0;
8985 
8986  c->thmb_item_id = from_item_id;
8987 
8988  av_log(c->fc, AV_LOG_TRACE, "thmb: from_item_id %d, entries %d\n",
8989  from_item_id, entries);
8990 
8991  return 0;
8992 }
8993 
8995 {
8996  int version = avio_r8(pb);
8997  avio_rb24(pb); // flags
8998  atom.size -= 4;
8999 
9000  if (version > 1) {
9001  av_log(c->fc, AV_LOG_WARNING, "Unknown iref box version %d\n", version);
9002  return 0;
9003  }
9004 
9005  while (atom.size) {
9006  uint32_t type, size = avio_rb32(pb);
9007  int64_t next = avio_tell(pb);
9008 
9009  if (size < 14 || next < 0 || next > INT64_MAX - size)
9010  return AVERROR_INVALIDDATA;
9011 
9012  next += size - 4;
9013  type = avio_rl32(pb);
9014  switch (type) {
9015  case MKTAG('d','i','m','g'):
9017  break;
9018  case MKTAG('t','h','m','b'):
9020  break;
9021  default:
9022  av_log(c->fc, AV_LOG_DEBUG, "Unknown iref type %s size %"PRIu32"\n",
9023  av_fourcc2str(type), size);
9024  }
9025 
9026  atom.size -= size;
9027  avio_seek(pb, next, SEEK_SET);
9028  }
9029  return 0;
9030 }
9031 
9033 {
9034  HEIFItem *item;
9035  uint32_t width, height;
9036 
9037  avio_r8(pb); /* version */
9038  avio_rb24(pb); /* flags */
9039  width = avio_rb32(pb);
9040  height = avio_rb32(pb);
9041 
9042  av_log(c->fc, AV_LOG_TRACE, "ispe: item_id %d, width %u, height %u\n",
9043  c->cur_item_id, width, height);
9044 
9045  item = heif_cur_item(c);
9046  if (item) {
9047  item->width = width;
9048  item->height = height;
9049  }
9050 
9051  return 0;
9052 }
9053 
9055 {
9056  HEIFItem *item;
9057  int angle;
9058 
9059  angle = avio_r8(pb) & 0x3;
9060 
9061  av_log(c->fc, AV_LOG_TRACE, "irot: item_id %d, angle %u\n",
9062  c->cur_item_id, angle);
9063 
9064  item = heif_cur_item(c);
9065  if (item) {
9066  // angle * 90 specifies the angle (in anti-clockwise direction)
9067  // in units of degrees.
9068  item->rotation = angle * 90;
9069  }
9070 
9071  return 0;
9072 }
9073 
9075 {
9076  HEIFItem *item;
9077  int axis;
9078 
9079  axis = avio_r8(pb) & 0x1;
9080 
9081  av_log(c->fc, AV_LOG_TRACE, "imir: item_id %d, axis %u\n",
9082  c->cur_item_id, axis);
9083 
9084  item = heif_cur_item(c);
9085  if (item) {
9086  item->hflip = axis;
9087  item->vflip = !axis;
9088  }
9089 
9090  return 0;
9091 }
9092 
9094 {
9095  typedef struct MOVAtoms {
9096  FFIOContext b;
9097  uint32_t type;
9098  int64_t size;
9099  uint8_t *data;
9100  } MOVAtoms;
9101  MOVAtoms *atoms = NULL;
9102  MOVAtom a;
9103  unsigned count;
9104  int nb_atoms = 0;
9105  int version, flags;
9106  int ret;
9107 
9108  a.size = avio_rb32(pb);
9109  a.type = avio_rl32(pb);
9110 
9111  if (a.size < 8 || a.type != MKTAG('i','p','c','o'))
9112  return AVERROR_INVALIDDATA;
9113 
9114  a.size -= 8;
9115  while (a.size >= 8) {
9116  MOVAtoms *ref = av_dynarray2_add((void**)&atoms, &nb_atoms, sizeof(MOVAtoms), NULL);
9117  if (!ref) {
9118  ret = AVERROR(ENOMEM);
9119  goto fail;
9120  }
9121  ref->data = NULL;
9122  ref->size = avio_rb32(pb);
9123  ref->type = avio_rl32(pb);
9124  if (ref->size > a.size || ref->size < 8)
9125  break;
9126  ref->data = av_malloc(ref->size);
9127  if (!ref->data) {
9129  goto fail;
9130  }
9131  av_log(c->fc, AV_LOG_TRACE, "ipco: index %d, box type %s\n", nb_atoms, av_fourcc2str(ref->type));
9132  avio_seek(pb, -8, SEEK_CUR);
9133  if (avio_read(pb, ref->data, ref->size) != ref->size) {
9135  goto fail;
9136  }
9137  ffio_init_read_context(&ref->b, ref->data, ref->size);
9138  a.size -= ref->size;
9139  }
9140 
9141  if (a.size) {
9143  goto fail;
9144  }
9145 
9146  a.size = avio_rb32(pb);
9147  a.type = avio_rl32(pb);
9148 
9149  if (a.size < 8 || a.type != MKTAG('i','p','m','a')) {
9151  goto fail;
9152  }
9153 
9154  version = avio_r8(pb);
9155  flags = avio_rb24(pb);
9156  count = avio_rb32(pb);
9157 
9158  for (int i = 0; i < count; i++) {
9159  int item_id = version ? avio_rb32(pb) : avio_rb16(pb);
9160  int assoc_count = avio_r8(pb);
9161 
9162  if (avio_feof(pb)) {
9164  goto fail;
9165  }
9166 
9167  for (int j = 0; j < assoc_count; j++) {
9168  MOVAtoms *ref;
9169  int index = avio_r8(pb) & 0x7f;
9170  if (flags & 1) {
9171  index <<= 8;
9172  index |= avio_r8(pb);
9173  }
9174  if (index > nb_atoms || index <= 0) {
9176  goto fail;
9177  }
9178  ref = &atoms[--index];
9179 
9180  av_log(c->fc, AV_LOG_TRACE, "ipma: property_index %d, item_id %d, item_type %s\n",
9181  index + 1, item_id, av_fourcc2str(ref->type));
9182 
9183  c->cur_item_id = item_id;
9184 
9185  ret = mov_read_default(c, &ref->b.pub,
9186  (MOVAtom) { .size = ref->size,
9187  .type = MKTAG('i','p','c','o') });
9188  if (ret < 0)
9189  goto fail;
9190  ffio_init_read_context(&ref->b, ref->data, ref->size);
9191  }
9192  }
9193 
9194  ret = 0;
9195 fail:
9196  c->cur_item_id = -1;
9197  for (int i = 0; i < nb_atoms; i++)
9198  av_free(atoms[i].data);
9199  av_free(atoms);
9200 
9201  return ret;
9202 }
9203 
9205 { MKTAG('A','C','L','R'), mov_read_aclr },
9206 { MKTAG('A','P','R','G'), mov_read_avid },
9207 { MKTAG('A','A','L','P'), mov_read_avid },
9208 { MKTAG('A','R','E','S'), mov_read_ares },
9209 { MKTAG('a','v','s','s'), mov_read_avss },
9210 { MKTAG('a','v','1','C'), mov_read_glbl },
9211 { MKTAG('c','h','p','l'), mov_read_chpl },
9212 { MKTAG('c','o','6','4'), mov_read_stco },
9213 { MKTAG('c','o','l','r'), mov_read_colr },
9214 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
9215 { MKTAG('d','i','n','f'), mov_read_default },
9216 { MKTAG('D','p','x','E'), mov_read_dpxe },
9217 { MKTAG('d','r','e','f'), mov_read_dref },
9218 { MKTAG('e','d','t','s'), mov_read_default },
9219 { MKTAG('e','l','s','t'), mov_read_elst },
9220 { MKTAG('e','n','d','a'), mov_read_enda },
9221 { MKTAG('f','i','e','l'), mov_read_fiel },
9222 { MKTAG('a','d','r','m'), mov_read_adrm },
9223 { MKTAG('f','t','y','p'), mov_read_ftyp },
9224 { MKTAG('g','l','b','l'), mov_read_glbl },
9225 { MKTAG('h','d','l','r'), mov_read_hdlr },
9226 { MKTAG('i','l','s','t'), mov_read_ilst },
9227 { MKTAG('j','p','2','h'), mov_read_jp2h },
9228 { MKTAG('m','d','a','t'), mov_read_mdat },
9229 { MKTAG('m','d','h','d'), mov_read_mdhd },
9230 { MKTAG('m','d','i','a'), mov_read_default },
9231 { MKTAG('m','e','t','a'), mov_read_meta },
9232 { MKTAG('m','i','n','f'), mov_read_default },
9233 { MKTAG('m','o','o','f'), mov_read_moof },
9234 { MKTAG('m','o','o','v'), mov_read_moov },
9235 { MKTAG('m','v','e','x'), mov_read_default },
9236 { MKTAG('m','v','h','d'), mov_read_mvhd },
9237 { MKTAG('S','M','I',' '), mov_read_svq3 },
9238 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
9239 { MKTAG('a','v','c','C'), mov_read_glbl },
9240 { MKTAG('p','a','s','p'), mov_read_pasp },
9241 { MKTAG('c','l','a','p'), mov_read_clap },
9242 { MKTAG('s','b','a','s'), mov_read_sbas },
9243 { MKTAG('s','i','d','x'), mov_read_sidx },
9244 { MKTAG('s','t','b','l'), mov_read_default },
9245 { MKTAG('s','t','c','o'), mov_read_stco },
9246 { MKTAG('s','t','p','s'), mov_read_stps },
9247 { MKTAG('s','t','r','f'), mov_read_strf },
9248 { MKTAG('s','t','s','c'), mov_read_stsc },
9249 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
9250 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
9251 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
9252 { MKTAG('s','t','t','s'), mov_read_stts },
9253 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
9254 { MKTAG('s','d','t','p'), mov_read_sdtp }, /* independent and disposable samples */
9255 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
9256 { MKTAG('t','f','d','t'), mov_read_tfdt },
9257 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
9258 { MKTAG('t','r','a','k'), mov_read_trak },
9259 { MKTAG('t','r','a','f'), mov_read_default },
9260 { MKTAG('t','r','e','f'), mov_read_default },
9261 { MKTAG('t','m','c','d'), mov_read_tmcd },
9262 { MKTAG('c','h','a','p'), mov_read_chap },
9263 { MKTAG('t','r','e','x'), mov_read_trex },
9264 { MKTAG('t','r','u','n'), mov_read_trun },
9265 { MKTAG('u','d','t','a'), mov_read_default },
9266 { MKTAG('w','a','v','e'), mov_read_wave },
9267 { MKTAG('e','s','d','s'), mov_read_esds },
9268 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
9269 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
9270 { MKTAG('d','d','t','s'), mov_read_ddts }, /* DTS audio descriptor */
9271 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
9272 { MKTAG('w','f','e','x'), mov_read_wfex },
9273 { MKTAG('c','m','o','v'), mov_read_cmov },
9274 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout from quicktime */
9275 { MKTAG('c','h','n','l'), mov_read_chnl }, /* channel layout from ISO-14496-12 */
9276 { MKTAG('d','v','c','1'), mov_read_dvc1 },
9277 { MKTAG('s','g','p','d'), mov_read_sgpd },
9278 { MKTAG('s','b','g','p'), mov_read_sbgp },
9279 { MKTAG('h','v','c','C'), mov_read_glbl },
9280 { MKTAG('v','v','c','C'), mov_read_glbl },
9281 { MKTAG('u','u','i','d'), mov_read_uuid },
9282 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
9283 { MKTAG('f','r','e','e'), mov_read_free },
9284 { MKTAG('-','-','-','-'), mov_read_custom },
9285 { MKTAG('s','i','n','f'), mov_read_default },
9286 { MKTAG('f','r','m','a'), mov_read_frma },
9287 { MKTAG('s','e','n','c'), mov_read_senc },
9288 { MKTAG('s','a','i','z'), mov_read_saiz },
9289 { MKTAG('s','a','i','o'), mov_read_saio },
9290 { MKTAG('p','s','s','h'), mov_read_pssh },
9291 { MKTAG('s','c','h','m'), mov_read_schm },
9292 { MKTAG('s','c','h','i'), mov_read_default },
9293 { MKTAG('t','e','n','c'), mov_read_tenc },
9294 { MKTAG('d','f','L','a'), mov_read_dfla },
9295 { MKTAG('s','t','3','d'), mov_read_st3d }, /* stereoscopic 3D video box */
9296 { MKTAG('s','v','3','d'), mov_read_sv3d }, /* spherical video box */
9297 { MKTAG('v','e','x','u'), mov_read_vexu }, /* video extension usage */
9298 { MKTAG('h','f','o','v'), mov_read_hfov },
9299 { MKTAG('d','O','p','s'), mov_read_dops },
9300 { MKTAG('d','m','l','p'), mov_read_dmlp },
9301 { MKTAG('S','m','D','m'), mov_read_smdm },
9302 { MKTAG('C','o','L','L'), mov_read_coll },
9303 { MKTAG('v','p','c','C'), mov_read_vpcc },
9304 { MKTAG('m','d','c','v'), mov_read_mdcv },
9305 { MKTAG('c','l','l','i'), mov_read_clli },
9306 { MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
9307 { MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
9308 { MKTAG('d','v','w','C'), mov_read_dvcc_dvvc },
9309 { MKTAG('k','i','n','d'), mov_read_kind },
9310 { MKTAG('S','A','3','D'), mov_read_SA3D }, /* ambisonic audio box */
9311 { MKTAG('S','A','N','D'), mov_read_SAND }, /* non diegetic audio box */
9312 { MKTAG('i','l','o','c'), mov_read_iloc },
9313 { MKTAG('p','c','m','C'), mov_read_pcmc }, /* PCM configuration box */
9314 { MKTAG('p','i','t','m'), mov_read_pitm },
9315 { MKTAG('e','v','c','C'), mov_read_glbl },
9316 { MKTAG('i','d','a','t'), mov_read_idat },
9317 { MKTAG('i','m','i','r'), mov_read_imir },
9318 { MKTAG('i','r','e','f'), mov_read_iref },
9319 { MKTAG('i','s','p','e'), mov_read_ispe },
9320 { MKTAG('i','r','o','t'), mov_read_irot },
9321 { MKTAG('i','p','r','p'), mov_read_iprp },
9322 { MKTAG('i','i','n','f'), mov_read_iinf },
9323 { MKTAG('a','m','v','e'), mov_read_amve }, /* ambient viewing environment box */
9324 { MKTAG('l','h','v','C'), mov_read_lhvc },
9325 { MKTAG('l','v','c','C'), mov_read_glbl },
9326 #if CONFIG_IAMFDEC
9327 { MKTAG('i','a','c','b'), mov_read_iacb },
9328 #endif
9329 { 0, NULL }
9330 };
9331 
9333 {
9334  int64_t total_size = 0;
9335  MOVAtom a;
9336  int i;
9337 
9338  if (c->atom_depth > 10) {
9339  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
9340  return AVERROR_INVALIDDATA;
9341  }
9342  c->atom_depth ++;
9343 
9344  if (atom.size < 0)
9345  atom.size = INT64_MAX;
9346  while (total_size <= atom.size - 8) {
9347  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
9348  a.size = avio_rb32(pb);
9349  a.type = avio_rl32(pb);
9350  if (avio_feof(pb))
9351  break;
9352  if (((a.type == MKTAG('f','r','e','e') && c->moov_retry) ||
9353  a.type == MKTAG('h','o','o','v')) &&
9354  a.size >= 8 &&
9355  c->fc->strict_std_compliance < FF_COMPLIANCE_STRICT) {
9356  uint32_t type;
9357  avio_skip(pb, 4);
9358  type = avio_rl32(pb);
9359  if (avio_feof(pb))
9360  break;
9361  avio_seek(pb, -8, SEEK_CUR);
9362  if (type == MKTAG('m','v','h','d') ||
9363  type == MKTAG('c','m','o','v')) {
9364  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free or hoov atom.\n");
9365  a.type = MKTAG('m','o','o','v');
9366  }
9367  }
9368  if (atom.type != MKTAG('r','o','o','t') &&
9369  atom.type != MKTAG('m','o','o','v')) {
9370  if (a.type == MKTAG('t','r','a','k') ||
9371  a.type == MKTAG('m','d','a','t')) {
9372  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
9373  avio_skip(pb, -8);
9374  c->atom_depth --;
9375  return 0;
9376  }
9377  }
9378  total_size += 8;
9379  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
9380  a.size = avio_rb64(pb) - 8;
9381  total_size += 8;
9382  }
9383  av_log(c->fc, AV_LOG_TRACE, "type:'%s' parent:'%s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
9384  av_fourcc2str(a.type), av_fourcc2str(atom.type), a.size, total_size, atom.size);
9385  if (a.size == 0) {
9386  a.size = atom.size - total_size + 8;
9387  }
9388  if (a.size < 0)
9389  break;
9390  a.size -= 8;
9391  if (a.size < 0)
9392  break;
9393  a.size = FFMIN(a.size, atom.size - total_size);
9394 
9395  for (i = 0; mov_default_parse_table[i].type; i++)
9396  if (mov_default_parse_table[i].type == a.type) {
9398  break;
9399  }
9400 
9401  // container is user data
9402  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
9403  atom.type == MKTAG('i','l','s','t')))
9405 
9406  // Supports parsing the QuickTime Metadata Keys.
9407  // https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
9408  if (!parse && c->found_hdlr_mdta &&
9409  atom.type == MKTAG('m','e','t','a') &&
9410  a.type == MKTAG('k','e','y','s') &&
9411  c->meta_keys_count == 0) {
9412  parse = mov_read_keys;
9413  }
9414 
9415  if (!parse) { /* skip leaf atoms data */
9416  avio_skip(pb, a.size);
9417  } else {
9418  int64_t start_pos = avio_tell(pb);
9419  int64_t left;
9420  int err = parse(c, pb, a);
9421  if (err < 0) {
9422  c->atom_depth --;
9423  return err;
9424  }
9425  if (c->found_moov && c->found_mdat && a.size <= INT64_MAX - start_pos &&
9426  ((!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete) ||
9427  start_pos + a.size == avio_size(pb))) {
9428  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || c->fc->flags & AVFMT_FLAG_IGNIDX || c->frag_index.complete)
9429  c->next_root_atom = start_pos + a.size;
9430  c->atom_depth --;
9431  return 0;
9432  }
9433  left = a.size - avio_tell(pb) + start_pos;
9434  if (left > 0) /* skip garbage at atom end */
9435  avio_skip(pb, left);
9436  else if (left < 0) {
9437  av_log(c->fc, AV_LOG_WARNING,
9438  "overread end of atom '%s' by %"PRId64" bytes\n",
9439  av_fourcc2str(a.type), -left);
9440  avio_seek(pb, left, SEEK_CUR);
9441  }
9442  }
9443 
9444  total_size += a.size;
9445  }
9446 
9447  if (total_size < atom.size && atom.size < 0x7ffff)
9448  avio_skip(pb, atom.size - total_size);
9449 
9450  c->atom_depth --;
9451  return 0;
9452 }
9453 
9454 static int mov_probe(const AVProbeData *p)
9455 {
9456  int64_t offset;
9457  uint32_t tag;
9458  int score = 0;
9459  int moov_offset = -1;
9460 
9461  /* check file header */
9462  offset = 0;
9463  for (;;) {
9464  int64_t size;
9465  int minsize = 8;
9466  /* ignore invalid offset */
9467  if ((offset + 8ULL) > (unsigned int)p->buf_size)
9468  break;
9469  size = AV_RB32(p->buf + offset);
9470  if (size == 1 && offset + 16 <= (unsigned int)p->buf_size) {
9471  size = AV_RB64(p->buf+offset + 8);
9472  minsize = 16;
9473  } else if (size == 0) {
9474  size = p->buf_size - offset;
9475  }
9476  if (size < minsize) {
9477  offset += 4;
9478  continue;
9479  }
9480  tag = AV_RL32(p->buf + offset + 4);
9481  switch(tag) {
9482  /* check for obvious tags */
9483  case MKTAG('m','o','o','v'):
9484  moov_offset = offset + 4;
9485  case MKTAG('m','d','a','t'):
9486  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
9487  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
9488  case MKTAG('f','t','y','p'):
9489  if (tag == MKTAG('f','t','y','p') &&
9490  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
9491  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
9492  || AV_RL32(p->buf + offset + 8) == MKTAG('j','x','l',' ')
9493  )) {
9494  score = FFMAX(score, 5);
9495  } else {
9496  score = AVPROBE_SCORE_MAX;
9497  }
9498  break;
9499  /* those are more common words, so rate then a bit less */
9500  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
9501  case MKTAG('w','i','d','e'):
9502  case MKTAG('f','r','e','e'):
9503  case MKTAG('j','u','n','k'):
9504  case MKTAG('p','i','c','t'):
9505  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
9506  break;
9507  case MKTAG(0x82,0x82,0x7f,0x7d):
9508  score = FFMAX(score, AVPROBE_SCORE_EXTENSION - 5);
9509  break;
9510  case MKTAG('s','k','i','p'):
9511  case MKTAG('u','u','i','d'):
9512  case MKTAG('p','r','f','l'):
9513  /* if we only find those cause probedata is too small at least rate them */
9514  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
9515  break;
9516  }
9517  if (size > INT64_MAX - offset)
9518  break;
9519  offset += size;
9520  }
9521  if (score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
9522  /* moov atom in the header - we should make sure that this is not a
9523  * MOV-packed MPEG-PS */
9524  offset = moov_offset;
9525 
9526  while (offset < (p->buf_size - 16)) { /* Sufficient space */
9527  /* We found an actual hdlr atom */
9528  if (AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
9529  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
9530  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')) {
9531  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
9532  /* We found a media handler reference atom describing an
9533  * MPEG-PS-in-MOV, return a
9534  * low score to force expanding the probe window until
9535  * mpegps_probe finds what it needs */
9536  return 5;
9537  } else {
9538  /* Keep looking */
9539  offset += 2;
9540  }
9541  }
9542  }
9543 
9544  return score;
9545 }
9546 
9547 // must be done after parsing all trak because there's no order requirement
9549 {
9550  MOVContext *mov = s->priv_data;
9551  MOVStreamContext *sc;
9552  int64_t cur_pos;
9553  int i, j;
9554  int chapter_track;
9555 
9556  for (j = 0; j < mov->nb_chapter_tracks; j++) {
9557  AVStream *st = NULL;
9558  FFStream *sti = NULL;
9559  chapter_track = mov->chapter_tracks[j];
9560  for (i = 0; i < s->nb_streams; i++) {
9561  sc = mov->fc->streams[i]->priv_data;
9562  if (sc->id == chapter_track) {
9563  st = s->streams[i];
9564  break;
9565  }
9566  }
9567  if (!st) {
9568  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
9569  continue;
9570  }
9571  sti = ffstream(st);
9572 
9573  sc = st->priv_data;
9574  cur_pos = avio_tell(sc->pb);
9575 
9576  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
9578  if (!st->attached_pic.data && sti->nb_index_entries) {
9579  // Retrieve the first frame, if possible
9580  AVIndexEntry *sample = &sti->index_entries[0];
9581  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
9582  av_log(s, AV_LOG_ERROR, "Failed to retrieve first frame\n");
9583  goto finish;
9584  }
9585 
9586  if (ff_add_attached_pic(s, st, sc->pb, NULL, sample->size) < 0)
9587  goto finish;
9588  }
9589  } else {
9592  st->discard = AVDISCARD_ALL;
9593  for (int i = 0; i < sti->nb_index_entries; i++) {
9594  AVIndexEntry *sample = &sti->index_entries[i];
9595  int64_t end = i+1 < sti->nb_index_entries ? sti->index_entries[i+1].timestamp : st->duration;
9596  uint8_t *title;
9597  uint16_t ch;
9598  int len, title_len;
9599 
9600  if (end < sample->timestamp) {
9601  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
9602  end = AV_NOPTS_VALUE;
9603  }
9604 
9605  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
9606  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
9607  goto finish;
9608  }
9609 
9610  // the first two bytes are the length of the title
9611  len = avio_rb16(sc->pb);
9612  if (len > sample->size-2)
9613  continue;
9614  title_len = 2*len + 1;
9615  if (!(title = av_mallocz(title_len)))
9616  goto finish;
9617 
9618  // The samples could theoretically be in any encoding if there's an encd
9619  // atom following, but in practice are only utf-8 or utf-16, distinguished
9620  // instead by the presence of a BOM
9621  if (!len) {
9622  title[0] = 0;
9623  } else {
9624  ch = avio_rb16(sc->pb);
9625  if (ch == 0xfeff)
9626  avio_get_str16be(sc->pb, len, title, title_len);
9627  else if (ch == 0xfffe)
9628  avio_get_str16le(sc->pb, len, title, title_len);
9629  else {
9630  AV_WB16(title, ch);
9631  if (len == 1 || len == 2)
9632  title[len] = 0;
9633  else
9634  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
9635  }
9636  }
9637 
9638  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
9639  av_freep(&title);
9640  }
9641  }
9642 finish:
9643  avio_seek(sc->pb, cur_pos, SEEK_SET);
9644  }
9645 }
9646 
9648  int64_t value, int flags)
9649 {
9650  AVTimecode tc;
9651  char buf[AV_TIMECODE_STR_SIZE];
9652  AVRational rate = st->avg_frame_rate;
9653  int ret = av_timecode_init(&tc, rate, flags, 0, s);
9654  if (ret < 0)
9655  return ret;
9656  av_dict_set(&st->metadata, "timecode",
9657  av_timecode_make_string(&tc, buf, value), 0);
9658  return 0;
9659 }
9660 
9662 {
9663  MOVStreamContext *sc = st->priv_data;
9664  FFStream *const sti = ffstream(st);
9665  char buf[AV_TIMECODE_STR_SIZE];
9666  int64_t cur_pos = avio_tell(sc->pb);
9667  int hh, mm, ss, ff, drop;
9668 
9669  if (!sti->nb_index_entries)
9670  return -1;
9671 
9672  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
9673  avio_skip(s->pb, 13);
9674  hh = avio_r8(s->pb);
9675  mm = avio_r8(s->pb);
9676  ss = avio_r8(s->pb);
9677  drop = avio_r8(s->pb);
9678  ff = avio_r8(s->pb);
9679  snprintf(buf, AV_TIMECODE_STR_SIZE, "%02d:%02d:%02d%c%02d",
9680  hh, mm, ss, drop ? ';' : ':', ff);
9681  av_dict_set(&st->metadata, "timecode", buf, 0);
9682 
9683  avio_seek(sc->pb, cur_pos, SEEK_SET);
9684  return 0;
9685 }
9686 
9688 {
9689  MOVStreamContext *sc = st->priv_data;
9690  FFStream *const sti = ffstream(st);
9691  int flags = 0;
9692  int64_t cur_pos = avio_tell(sc->pb);
9693  int64_t value;
9694  AVRational tc_rate = st->avg_frame_rate;
9695  int tmcd_nb_frames = sc->tmcd_nb_frames;
9696  int rounded_tc_rate;
9697 
9698  if (!sti->nb_index_entries)
9699  return -1;
9700 
9701  if (!tc_rate.num || !tc_rate.den || !tmcd_nb_frames)
9702  return -1;
9703 
9704  avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
9705  value = avio_rb32(s->pb);
9706 
9707  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
9708  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
9709  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
9710 
9711  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
9712  * not the case) and thus assume "frame number format" instead of QT one.
9713  * No sample with tmcd track can be found with a QT timecode at the moment,
9714  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
9715  * format). */
9716 
9717  /* 60 fps content have tmcd_nb_frames set to 30 but tc_rate set to 60, so
9718  * we multiply the frame number with the quotient.
9719  * See tickets #9492, #9710. */
9720  rounded_tc_rate = (tc_rate.num + tc_rate.den / 2LL) / tc_rate.den;
9721  /* Work around files where tmcd_nb_frames is rounded down from frame rate
9722  * instead of up. See ticket #5978. */
9723  if (tmcd_nb_frames == tc_rate.num / tc_rate.den &&
9724  s->strict_std_compliance < FF_COMPLIANCE_STRICT)
9725  tmcd_nb_frames = rounded_tc_rate;
9726  value = av_rescale(value, rounded_tc_rate, tmcd_nb_frames);
9727 
9729 
9730  avio_seek(sc->pb, cur_pos, SEEK_SET);
9731  return 0;
9732 }
9733 
9735  int i;
9736  if (!index || !*index) return;
9737  for (i = 0; i < (*index)->nb_encrypted_samples; i++) {
9738  av_encryption_info_free((*index)->encrypted_samples[i]);
9739  }
9740  av_freep(&(*index)->encrypted_samples);
9741  av_freep(&(*index)->auxiliary_info_sizes);
9742  av_freep(&(*index)->auxiliary_offsets);
9743  av_freep(index);
9744 }
9745 
9747 {
9748  MOVStreamContext *sc = st->priv_data;
9749 
9750  if (!sc || --sc->refcount) {
9751  st->priv_data = NULL;
9752  return;
9753  }
9754 
9755  av_freep(&sc->tts_data);
9756  for (int i = 0; i < sc->drefs_count; i++) {
9757  av_freep(&sc->drefs[i].path);
9758  av_freep(&sc->drefs[i].dir);
9759  }
9760  av_freep(&sc->drefs);
9761 
9762  sc->drefs_count = 0;
9763 
9764  if (!sc->pb_is_copied)
9765  ff_format_io_close(s, &sc->pb);
9766 
9767  sc->pb = NULL;
9768  av_freep(&sc->chunk_offsets);
9769  av_freep(&sc->stsc_data);
9770  av_freep(&sc->sample_sizes);
9771  av_freep(&sc->keyframes);
9772  av_freep(&sc->ctts_data);
9773  av_freep(&sc->stts_data);
9774  av_freep(&sc->sdtp_data);
9775  av_freep(&sc->stps_data);
9776  av_freep(&sc->elst_data);
9777  av_freep(&sc->rap_group);
9778  av_freep(&sc->sync_group);
9779  av_freep(&sc->sgpd_sync);
9780  av_freep(&sc->sample_offsets);
9781  av_freep(&sc->open_key_samples);
9782  av_freep(&sc->display_matrix);
9783  av_freep(&sc->index_ranges);
9784 
9785  if (sc->extradata)
9786  for (int i = 0; i < sc->stsd_count; i++)
9787  av_free(sc->extradata[i]);
9788  av_freep(&sc->extradata);
9789  av_freep(&sc->extradata_size);
9790 
9794 
9795  av_freep(&sc->stereo3d);
9796  av_freep(&sc->spherical);
9797  av_freep(&sc->mastering);
9798  av_freep(&sc->coll);
9799  av_freep(&sc->ambient);
9800 
9801 #if CONFIG_IAMFDEC
9802  if (sc->iamf)
9804 #endif
9805  av_freep(&sc->iamf);
9806 }
9807 
9809 {
9810  MOVContext *mov = s->priv_data;
9811  int i, j;
9812 
9813  for (i = 0; i < s->nb_streams; i++) {
9814  AVStream *st = s->streams[i];
9815 
9817  }
9818 
9819  av_freep(&mov->dv_demux);
9821  mov->dv_fctx = NULL;
9822 
9823  if (mov->meta_keys) {
9824  for (i = 1; i < mov->meta_keys_count; i++) {
9825  av_freep(&mov->meta_keys[i]);
9826  }
9827  av_freep(&mov->meta_keys);
9828  }
9829 
9830  av_freep(&mov->trex_data);
9831  av_freep(&mov->bitrates);
9832 
9833  for (i = 0; i < mov->frag_index.nb_items; i++) {
9835  for (j = 0; j < mov->frag_index.item[i].nb_stream_info; j++) {
9836  mov_free_encryption_index(&frag[j].encryption_index);
9837  }
9839  }
9840  av_freep(&mov->frag_index.item);
9841 
9842  av_freep(&mov->aes_decrypt);
9843  av_freep(&mov->chapter_tracks);
9844  for (i = 0; i < mov->nb_heif_item; i++) {
9845  if (!mov->heif_item[i])
9846  continue;
9847  av_freep(&mov->heif_item[i]->name);
9848  av_freep(&mov->heif_item[i]->icc_profile);
9849  av_freep(&mov->heif_item[i]);
9850  }
9851  av_freep(&mov->heif_item);
9852  for (i = 0; i < mov->nb_heif_grid; i++) {
9853  av_freep(&mov->heif_grid[i].tile_id_list);
9856  }
9857  av_freep(&mov->heif_grid);
9858 
9859  return 0;
9860 }
9861 
9862 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
9863 {
9864  int i;
9865 
9866  for (i = 0; i < s->nb_streams; i++) {
9867  AVStream *st = s->streams[i];
9868  MOVStreamContext *sc = st->priv_data;
9869 
9870  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
9871  sc->timecode_track == tmcd_id)
9872  return 1;
9873  }
9874  return 0;
9875 }
9876 
9877 /* look for a tmcd track not referenced by any video track, and export it globally */
9879 {
9880  int i;
9881 
9882  for (i = 0; i < s->nb_streams; i++) {
9883  AVStream *st = s->streams[i];
9884 
9885  if (st->codecpar->codec_tag == MKTAG('t','m','c','d') &&
9886  !tmcd_is_referenced(s, i + 1)) {
9887  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
9888  if (tcr) {
9889  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
9890  break;
9891  }
9892  }
9893  }
9894 }
9895 
9896 static int read_tfra(MOVContext *mov, AVIOContext *f)
9897 {
9898  int version, fieldlength, i, j;
9899  int64_t pos = avio_tell(f);
9900  uint32_t size = avio_rb32(f);
9901  unsigned track_id, item_count;
9902 
9903  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
9904  return 1;
9905  }
9906  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
9907 
9908  version = avio_r8(f);
9909  avio_rb24(f);
9910  track_id = avio_rb32(f);
9911  fieldlength = avio_rb32(f);
9912  item_count = avio_rb32(f);
9913  for (i = 0; i < item_count; i++) {
9914  int64_t time, offset;
9915  int index;
9916  MOVFragmentStreamInfo * frag_stream_info;
9917 
9918  if (avio_feof(f)) {
9919  return AVERROR_INVALIDDATA;
9920  }
9921 
9922  if (version == 1) {
9923  time = avio_rb64(f);
9924  offset = avio_rb64(f);
9925  } else {
9926  time = avio_rb32(f);
9927  offset = avio_rb32(f);
9928  }
9929 
9930  // The first sample of each stream in a fragment is always a random
9931  // access sample. So it's entry in the tfra can be used as the
9932  // initial PTS of the fragment.
9933  index = update_frag_index(mov, offset);
9934  frag_stream_info = get_frag_stream_info(&mov->frag_index, index, track_id);
9935  if (frag_stream_info &&
9936  frag_stream_info->first_tfra_pts == AV_NOPTS_VALUE)
9937  frag_stream_info->first_tfra_pts = time;
9938 
9939  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
9940  avio_r8(f);
9941  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
9942  avio_r8(f);
9943  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
9944  avio_r8(f);
9945  }
9946 
9947  avio_seek(f, pos + size, SEEK_SET);
9948  return 0;
9949 }
9950 
9952 {
9953  int64_t stream_size = avio_size(f);
9954  int64_t original_pos = avio_tell(f);
9955  int64_t seek_ret;
9956  int ret = -1;
9957  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
9958  ret = seek_ret;
9959  goto fail;
9960  }
9961  c->mfra_size = avio_rb32(f);
9962  c->have_read_mfra_size = 1;
9963  if (!c->mfra_size || c->mfra_size > stream_size) {
9964  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
9965  goto fail;
9966  }
9967  if ((seek_ret = avio_seek(f, -((int64_t) c->mfra_size), SEEK_CUR)) < 0) {
9968  ret = seek_ret;
9969  goto fail;
9970  }
9971  if (avio_rb32(f) != c->mfra_size) {
9972  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
9973  goto fail;
9974  }
9975  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
9976  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
9977  goto fail;
9978  }
9979  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
9980  do {
9981  ret = read_tfra(c, f);
9982  if (ret < 0)
9983  goto fail;
9984  } while (!ret);
9985  ret = 0;
9986  c->frag_index.complete = 1;
9987 fail:
9988  seek_ret = avio_seek(f, original_pos, SEEK_SET);
9989  if (seek_ret < 0) {
9990  av_log(c->fc, AV_LOG_ERROR,
9991  "failed to seek back after looking for mfra\n");
9992  ret = seek_ret;
9993  }
9994  return ret;
9995 }
9996 
9997 static int set_icc_profile_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
9998  const HEIFItem *item)
9999 {
10000  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data, nb_coded_side_data,
10002  item->icc_profile_size, 0);
10003  if (!sd)
10004  return AVERROR(ENOMEM);
10005 
10006  memcpy(sd->data, item->icc_profile, item->icc_profile_size);
10007 
10008  return 0;
10009 }
10010 
10011 static int set_display_matrix_from_item(AVPacketSideData **coded_side_data, int *nb_coded_side_data,
10012  const HEIFItem *item)
10013 {
10014  int32_t *matrix;
10015  AVPacketSideData *sd = av_packet_side_data_new(coded_side_data,
10016  nb_coded_side_data,
10018  9 * sizeof(*matrix), 0);
10019  if (!sd)
10020  return AVERROR(ENOMEM);
10021 
10022  matrix = (int32_t*)sd->data;
10023  /* rotation is in the counter-clockwise direction whereas
10024  * av_display_rotation_set() expects its argument to be
10025  * oriented clockwise, so we need to negate it. */
10027  av_display_matrix_flip(matrix, item->hflip, item->vflip);
10028 
10029  return 0;
10030 }
10031 
10032 static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid,
10033  AVStreamGroupTileGrid *tile_grid)
10034 {
10035  MOVContext *c = s->priv_data;
10036  const HEIFItem *item = grid->item;
10037  int64_t offset = 0, pos = avio_tell(s->pb);
10038  int x = 0, y = 0, i = 0;
10039  int tile_rows, tile_cols;
10040  int flags, size;
10041 
10042  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10043  av_log(c->fc, AV_LOG_INFO, "grid box with non seekable input\n");
10044  return AVERROR_PATCHWELCOME;
10045  }
10046  if (item->is_idat_relative) {
10047  if (!c->idat_offset) {
10048  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image grid\n");
10049  return AVERROR_INVALIDDATA;
10050  }
10051  offset = c->idat_offset;
10052  }
10053 
10054  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10055 
10056  avio_r8(s->pb); /* version */
10057  flags = avio_r8(s->pb);
10058 
10059  tile_rows = avio_r8(s->pb) + 1;
10060  tile_cols = avio_r8(s->pb) + 1;
10061  /* actual width and height of output image */
10062  tile_grid->width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10063  tile_grid->height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10064 
10065  /* ICC profile */
10066  if (item->icc_profile_size) {
10067  int ret = set_icc_profile_from_item(&tile_grid->coded_side_data,
10068  &tile_grid->nb_coded_side_data, item);
10069  if (ret < 0)
10070  return ret;
10071  }
10072  /* rotation */
10073  if (item->rotation || item->hflip || item->vflip) {
10075  &tile_grid->nb_coded_side_data, item);
10076  if (ret < 0)
10077  return ret;
10078  }
10079 
10080  av_log(c->fc, AV_LOG_TRACE, "grid: grid_rows %d grid_cols %d output_width %d output_height %d\n",
10081  tile_rows, tile_cols, tile_grid->width, tile_grid->height);
10082 
10083  avio_seek(s->pb, pos, SEEK_SET);
10084 
10085  size = tile_rows * tile_cols;
10086  tile_grid->nb_tiles = grid->nb_tiles;
10087 
10088  if (tile_grid->nb_tiles != size)
10089  return AVERROR_INVALIDDATA;
10090 
10091  for (int i = 0; i < tile_cols; i++)
10092  tile_grid->coded_width += grid->tile_item_list[i]->width;
10093  for (int i = 0; i < size; i += tile_cols)
10094  tile_grid->coded_height += grid->tile_item_list[i]->height;
10095 
10096  tile_grid->offsets = av_calloc(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10097  if (!tile_grid->offsets)
10098  return AVERROR(ENOMEM);
10099 
10100  while (y < tile_grid->coded_height) {
10101  int left_col = i;
10102 
10103  while (x < tile_grid->coded_width) {
10104  if (i == tile_grid->nb_tiles)
10105  return AVERROR_INVALIDDATA;
10106 
10107  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10108  tile_grid->offsets[i].horizontal = x;
10109  tile_grid->offsets[i].vertical = y;
10110 
10111  x += grid->tile_item_list[i++]->width;
10112  }
10113 
10114  if (x > tile_grid->coded_width) {
10115  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10116  return AVERROR_INVALIDDATA;
10117  }
10118 
10119  x = 0;
10120  y += grid->tile_item_list[left_col]->height;
10121  }
10122 
10123  if (y > tile_grid->coded_height || i != tile_grid->nb_tiles) {
10124  av_log(c->fc, AV_LOG_ERROR, "Non uniform HEIF tiles\n");
10125  return AVERROR_INVALIDDATA;
10126  }
10127 
10128  return 0;
10129 }
10130 
10131 static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid,
10132  AVStreamGroupTileGrid *tile_grid)
10133 {
10134  MOVContext *c = s->priv_data;
10135  const HEIFItem *item = grid->item;
10136  uint16_t canvas_fill_value[4];
10137  int64_t offset = 0, pos = avio_tell(s->pb);
10138  int ret = 0, flags;
10139 
10140  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
10141  av_log(c->fc, AV_LOG_INFO, "iovl box with non seekable input\n");
10142  return AVERROR_PATCHWELCOME;
10143  }
10144  if (item->is_idat_relative) {
10145  if (!c->idat_offset) {
10146  av_log(c->fc, AV_LOG_ERROR, "missing idat box required by the image overlay\n");
10147  return AVERROR_INVALIDDATA;
10148  }
10149  offset = c->idat_offset;
10150  }
10151 
10152  avio_seek(s->pb, item->extent_offset + offset, SEEK_SET);
10153 
10154  avio_r8(s->pb); /* version */
10155  flags = avio_r8(s->pb);
10156 
10157  for (int i = 0; i < 4; i++)
10158  canvas_fill_value[i] = avio_rb16(s->pb);
10159  av_log(c->fc, AV_LOG_TRACE, "iovl: canvas_fill_value { %u, %u, %u, %u }\n",
10160  canvas_fill_value[0], canvas_fill_value[1],
10161  canvas_fill_value[2], canvas_fill_value[3]);
10162  for (int i = 0; i < 4; i++)
10163  tile_grid->background[i] = canvas_fill_value[i];
10164 
10165  /* actual width and height of output image */
10166  tile_grid->width =
10167  tile_grid->coded_width = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10168  tile_grid->height =
10169  tile_grid->coded_height = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10170 
10171  /* rotation */
10172  if (item->rotation || item->hflip || item->vflip) {
10174  &tile_grid->nb_coded_side_data, item);
10175  if (ret < 0)
10176  return ret;
10177  }
10178 
10179  /* ICC profile */
10180  if (item->icc_profile_size) {
10181  int ret = set_icc_profile_from_item(&tile_grid->coded_side_data,
10182  &tile_grid->nb_coded_side_data, item);
10183  if (ret < 0)
10184  return ret;
10185  }
10186 
10187  av_log(c->fc, AV_LOG_TRACE, "iovl: output_width %d, output_height %d\n",
10188  tile_grid->width, tile_grid->height);
10189 
10190  tile_grid->nb_tiles = grid->nb_tiles;
10191  tile_grid->offsets = av_malloc_array(tile_grid->nb_tiles, sizeof(*tile_grid->offsets));
10192  if (!tile_grid->offsets) {
10193  ret = AVERROR(ENOMEM);
10194  goto fail;
10195  }
10196 
10197  for (int i = 0; i < tile_grid->nb_tiles; i++) {
10198  tile_grid->offsets[i].idx = grid->tile_idx_list[i];
10199  tile_grid->offsets[i].horizontal = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10200  tile_grid->offsets[i].vertical = (flags & 1) ? avio_rb32(s->pb) : avio_rb16(s->pb);
10201  av_log(c->fc, AV_LOG_TRACE, "iovl: stream_idx[%d] %u, "
10202  "horizontal_offset[%d] %d, vertical_offset[%d] %d\n",
10203  i, tile_grid->offsets[i].idx,
10204  i, tile_grid->offsets[i].horizontal, i, tile_grid->offsets[i].vertical);
10205  }
10206 
10207 fail:
10208  avio_seek(s->pb, pos, SEEK_SET);
10209 
10210  return ret;
10211 }
10212 
10214 {
10215  MOVContext *mov = s->priv_data;
10216 
10217  for (int i = 0; i < mov->nb_heif_grid; i++) {
10219  AVStreamGroupTileGrid *tile_grid;
10220  const HEIFGrid *grid = &mov->heif_grid[i];
10221  int err, loop = 1;
10222 
10223  if (!stg)
10224  return AVERROR(ENOMEM);
10225 
10226  stg->id = grid->item->item_id;
10227  tile_grid = stg->params.tile_grid;
10228 
10229  for (int j = 0; j < grid->nb_tiles; j++) {
10230  int tile_id = grid->tile_id_list[j];
10231  int k;
10232 
10233  for (k = 0; k < mov->nb_heif_item; k++) {
10234  HEIFItem *item = mov->heif_item[k];
10235  AVStream *st;
10236 
10237  if (!item || item->item_id != tile_id)
10238  continue;
10239  st = item->st;
10240  if (!st) {
10241  av_log(s, AV_LOG_WARNING, "HEIF item id %d from grid id %d doesn't "
10242  "reference a stream\n",
10243  tile_id, grid->item->item_id);
10244  ff_remove_stream_group(s, stg);
10245  loop = 0;
10246  break;
10247  }
10248 
10249  grid->tile_item_list[j] = item;
10250  grid->tile_idx_list[j] = stg->nb_streams;
10251 
10252  err = avformat_stream_group_add_stream(stg, st);
10253  if (err < 0) {
10254  int l;
10255  if (err != AVERROR(EEXIST))
10256  return err;
10257 
10258  for (l = 0; l < stg->nb_streams; l++)
10259  if (stg->streams[l]->index == st->index)
10260  break;
10261  av_assert0(l < stg->nb_streams);
10262  grid->tile_idx_list[j] = l;
10263  }
10264 
10265  if (item->item_id != mov->primary_item_id)
10267  break;
10268  }
10269 
10270  if (k == mov->nb_heif_item) {
10271  av_assert0(loop);
10272  av_log(s, AV_LOG_WARNING, "HEIF item id %d referenced by grid id %d doesn't "
10273  "exist\n",
10274  tile_id, grid->item->item_id);
10275  ff_remove_stream_group(s, stg);
10276  loop = 0;
10277  }
10278  if (!loop)
10279  break;
10280  }
10281 
10282  if (!loop)
10283  continue;
10284 
10285  switch (grid->item->type) {
10286  case MKTAG('g','r','i','d'):
10287  err = read_image_grid(s, grid, tile_grid);
10288  break;
10289  case MKTAG('i','o','v','l'):
10290  err = read_image_iovl(s, grid, tile_grid);
10291  break;
10292  default:
10293  av_assert0(0);
10294  }
10295  if (err < 0)
10296  return err;
10297 
10298 
10299  if (grid->item->name)
10300  av_dict_set(&stg->metadata, "title", grid->item->name, 0);
10301  if (grid->item->item_id == mov->primary_item_id)
10303  }
10304 
10305  return 0;
10306 }
10307 
10309 {
10310  MOVContext *mov = s->priv_data;
10311  int err;
10312 
10313  for (int i = 0; i < mov->nb_heif_item; i++) {
10314  HEIFItem *item = mov->heif_item[i];
10315  MOVStreamContext *sc;
10316  AVStream *st;
10317  int64_t offset = 0;
10318 
10319  if (!item)
10320  continue;
10321  if (!item->st) {
10322  if (item->item_id == mov->thmb_item_id) {
10323  av_log(s, AV_LOG_ERROR, "HEIF thumbnail doesn't reference a stream\n");
10324  return AVERROR_INVALIDDATA;
10325  }
10326  continue;
10327  }
10328  if (item->is_idat_relative) {
10329  if (!mov->idat_offset) {
10330  av_log(s, AV_LOG_ERROR, "Missing idat box for item %d\n", item->item_id);
10331  return AVERROR_INVALIDDATA;
10332  }
10333  offset = mov->idat_offset;
10334  }
10335 
10336  st = item->st;
10337  sc = st->priv_data;
10338  st->codecpar->width = item->width;
10339  st->codecpar->height = item->height;
10340 
10341  err = sanity_checks(s, sc, item->item_id);
10342  if (err)
10343  return AVERROR_INVALIDDATA;
10344 
10345  sc->sample_sizes[0] = item->extent_length;
10346  sc->chunk_offsets[0] = item->extent_offset + offset;
10347 
10348  if (item->item_id == mov->primary_item_id)
10350 
10351  if (item->rotation || item->hflip || item->vflip) {
10353  &st->codecpar->nb_coded_side_data, item);
10354  if (err < 0)
10355  return err;
10356  }
10357 
10358  mov_build_index(mov, st);
10359  }
10360 
10361  if (mov->nb_heif_grid) {
10362  err = mov_parse_tiles(s);
10363  if (err < 0)
10364  return err;
10365  }
10366 
10367  return 0;
10368 }
10369 
10371  int first_index)
10372 {
10373  MOVStreamContext *sc = st->priv_data;
10374 
10375  if (sc->tref_id < 0)
10376  return NULL;
10377 
10378  for (int i = first_index; i < s->nb_streams; i++)
10379  if (s->streams[i]->id == sc->tref_id)
10380  return s->streams[i];
10381 
10382  return NULL;
10383 }
10384 
10386 {
10387  int err;
10388 
10389  for (int i = 0; i < s->nb_streams; i++) {
10390  AVStreamGroup *stg;
10391  AVStream *st = s->streams[i];
10392  AVStream *st_base;
10393  MOVStreamContext *sc = st->priv_data;
10394  int j = 0;
10395 
10396  /* Find an enhancement stream. */
10397  if (st->codecpar->codec_id != AV_CODEC_ID_LCEVC ||
10399  continue;
10400 
10402 
10404  if (!stg)
10405  return AVERROR(ENOMEM);
10406 
10407  stg->id = st->id;
10408  stg->params.lcevc->width = st->codecpar->width;
10409  stg->params.lcevc->height = st->codecpar->height;
10410  st->codecpar->width = 0;
10411  st->codecpar->height = 0;
10412 
10413  while (st_base = mov_find_reference_track(s, st, j)) {
10414  err = avformat_stream_group_add_stream(stg, st_base);
10415  if (err < 0)
10416  return err;
10417 
10418  j = st_base->index + 1;
10419  }
10420  if (!j) {
10421  av_log(s, AV_LOG_ERROR, "Failed to find base stream for enhancement stream\n");
10422  return AVERROR_INVALIDDATA;
10423  }
10424 
10425  err = avformat_stream_group_add_stream(stg, st);
10426  if (err < 0)
10427  return err;
10428 
10429  stg->params.lcevc->lcevc_index = stg->nb_streams - 1;
10430  }
10431 
10432  return 0;
10433 }
10434 
10436 {
10437  int highest_id = 0;
10438 
10439  for (int i = 0; i < s->nb_streams; i++) {
10440  const AVStream *st = s->streams[i];
10441  const MOVStreamContext *sc = st->priv_data;
10442  if (!sc->iamf)
10443  highest_id = FFMAX(highest_id, st->id);
10444  }
10445  highest_id += !highest_id;
10446  for (int i = 0; highest_id > 1 && i < s->nb_stream_groups; i++) {
10447  AVStreamGroup *stg = s->stream_groups[i];
10449  continue;
10450  for (int j = 0; j < stg->nb_streams; j++) {
10451  AVStream *st = stg->streams[j];
10452  MOVStreamContext *sc = st->priv_data;
10453  st->id += highest_id;
10454  sc->iamf_stream_offset = highest_id;
10455  }
10456  }
10457 }
10458 
10460 {
10461  MOVContext *mov = s->priv_data;
10462  AVIOContext *pb = s->pb;
10463  int j, err;
10464  MOVAtom atom = { AV_RL32("root") };
10465  int i;
10466 
10467  if (mov->decryption_key_len != 0 && mov->decryption_key_len != AES_CTR_KEY_SIZE) {
10468  av_log(s, AV_LOG_ERROR, "Invalid decryption key len %d expected %d\n",
10470  return AVERROR(EINVAL);
10471  }
10472 
10473  mov->fc = s;
10474  mov->trak_index = -1;
10475  mov->thmb_item_id = -1;
10476  mov->primary_item_id = -1;
10477  mov->cur_item_id = -1;
10478  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
10479  if (pb->seekable & AVIO_SEEKABLE_NORMAL)
10480  atom.size = avio_size(pb);
10481  else
10482  atom.size = INT64_MAX;
10483 
10484  /* check MOV header */
10485  do {
10486  if (mov->moov_retry)
10487  avio_seek(pb, 0, SEEK_SET);
10488  if ((err = mov_read_default(mov, pb, atom)) < 0) {
10489  av_log(s, AV_LOG_ERROR, "error reading header\n");
10490  return err;
10491  }
10492  } while ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
10493  !mov->found_moov && (!mov->found_iloc || !mov->found_iinf) && !mov->moov_retry++);
10494  if (!mov->found_moov && !mov->found_iloc && !mov->found_iinf) {
10495  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
10496  return AVERROR_INVALIDDATA;
10497  }
10498  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
10499 
10500  if (mov->found_iloc && mov->found_iinf) {
10501  err = mov_parse_heif_items(s);
10502  if (err < 0)
10503  return err;
10504  }
10505  // prevent iloc and iinf boxes from being parsed while reading packets.
10506  // this is needed because an iinf box may have been parsed but ignored
10507  // for having old infe boxes which create no streams.
10508  mov->found_iloc = mov->found_iinf = 1;
10509 
10510  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
10511  if (mov->nb_chapter_tracks > 0 && !mov->ignore_chapters)
10513  for (i = 0; i < s->nb_streams; i++)
10514  if (s->streams[i]->codecpar->codec_tag == AV_RL32("tmcd")) {
10515  mov_read_timecode_track(s, s->streams[i]);
10516  } else if (s->streams[i]->codecpar->codec_tag == AV_RL32("rtmd")) {
10517  mov_read_rtmd_track(s, s->streams[i]);
10518  }
10519  }
10520 
10521  /* copy timecode metadata from tmcd tracks to the related video streams */
10522  for (i = 0; i < s->nb_streams; i++) {
10523  AVStream *st = s->streams[i];
10524  MOVStreamContext *sc = st->priv_data;
10525  if (sc->timecode_track > 0) {
10526  AVDictionaryEntry *tcr;
10527  int tmcd_st_id = -1;
10528 
10529  for (j = 0; j < s->nb_streams; j++) {
10530  MOVStreamContext *sc2 = s->streams[j]->priv_data;
10531  if (sc2->id == sc->timecode_track)
10532  tmcd_st_id = j;
10533  }
10534 
10535  if (tmcd_st_id < 0 || tmcd_st_id == i)
10536  continue;
10537  tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
10538  if (tcr)
10539  av_dict_set(&st->metadata, "timecode", tcr->value, 0);
10540  }
10541  }
10543 
10544  /* Create LCEVC stream groups. */
10545  err = mov_parse_lcevc_streams(s);
10546  if (err < 0)
10547  return err;
10548 
10549  for (i = 0; i < s->nb_streams; i++) {
10550  AVStream *st = s->streams[i];
10551  FFStream *const sti = ffstream(st);
10552  MOVStreamContext *sc = st->priv_data;
10553  uint32_t dvdsub_clut[FF_DVDCLUT_CLUT_LEN] = {0};
10554  fix_timescale(mov, sc);
10555  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
10556  st->codecpar->codec_id == AV_CODEC_ID_AAC) {
10557  sti->skip_samples = sc->start_pad;
10558  }
10559  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
10561  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
10563  if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
10564  st->codecpar->width = sc->width;
10565  st->codecpar->height = sc->height;
10566  }
10569 
10570  for (j = 0; j < FF_DVDCLUT_CLUT_LEN; j++)
10571  dvdsub_clut[j] = AV_RB32(st->codecpar->extradata + j * 4);
10572 
10573  err = ff_dvdclut_yuv_to_rgb(dvdsub_clut, FF_DVDCLUT_CLUT_SIZE);
10574  if (err < 0)
10575  return err;
10576 
10577  av_freep(&st->codecpar->extradata);
10578  st->codecpar->extradata_size = 0;
10579 
10581  st->codecpar);
10582  if (err < 0)
10583  return err;
10584  }
10585  }
10586  if (mov->handbrake_version &&
10587  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
10588  st->codecpar->codec_id == AV_CODEC_ID_MP3) {
10589  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
10591  }
10592  }
10593 
10594  if (mov->trex_data || mov->use_mfra_for > 0) {
10595  for (i = 0; i < s->nb_streams; i++) {
10596  AVStream *st = s->streams[i];
10597  MOVStreamContext *sc = st->priv_data;
10598  if (sc->duration_for_fps > 0) {
10599  /* Akin to sc->data_size * 8 * sc->time_scale / sc->duration_for_fps but accounting for overflows. */
10601  if (st->codecpar->bit_rate == INT64_MIN) {
10602  av_log(s, AV_LOG_WARNING, "Overflow during bit rate calculation %"PRId64" * 8 * %d\n",
10603  sc->data_size, sc->time_scale);
10604  st->codecpar->bit_rate = 0;
10605  if (s->error_recognition & AV_EF_EXPLODE)
10606  return AVERROR_INVALIDDATA;
10607  }
10608  }
10609  }
10610  }
10611 
10612  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
10613  if (mov->bitrates[i]) {
10614  s->streams[i]->codecpar->bit_rate = mov->bitrates[i];
10615  }
10616  }
10617 
10619 
10620  for (i = 0; i < s->nb_streams; i++) {
10621  AVStream *st = s->streams[i];
10622  MOVStreamContext *sc = st->priv_data;
10623 
10624  switch (st->codecpar->codec_type) {
10625  case AVMEDIA_TYPE_AUDIO:
10626  err = ff_replaygain_export(st, s->metadata);
10627  if (err < 0)
10628  return err;
10629  break;
10630  case AVMEDIA_TYPE_VIDEO:
10631  if (sc->display_matrix) {
10634  (uint8_t*)sc->display_matrix, sizeof(int32_t) * 9, 0))
10635  return AVERROR(ENOMEM);
10636 
10637  sc->display_matrix = NULL;
10638  }
10639  if (sc->stereo3d) {
10642  (uint8_t *)sc->stereo3d, sc->stereo3d_size, 0))
10643  return AVERROR(ENOMEM);
10644 
10645  sc->stereo3d = NULL;
10646  }
10647  if (sc->spherical) {
10650  (uint8_t *)sc->spherical, sc->spherical_size, 0))
10651  return AVERROR(ENOMEM);
10652 
10653  sc->spherical = NULL;
10654  }
10655  if (sc->mastering) {
10658  (uint8_t *)sc->mastering, sc->mastering_size, 0))
10659  return AVERROR(ENOMEM);
10660 
10661  sc->mastering = NULL;
10662  }
10663  if (sc->coll) {
10666  (uint8_t *)sc->coll, sc->coll_size, 0))
10667  return AVERROR(ENOMEM);
10668 
10669  sc->coll = NULL;
10670  }
10671  if (sc->ambient) {
10674  (uint8_t *) sc->ambient, sc->ambient_size, 0))
10675  return AVERROR(ENOMEM);
10676 
10677  sc->ambient = NULL;
10678  }
10679  break;
10680  }
10681  }
10682 
10683  fix_stream_ids(s);
10684 
10686 
10687  for (i = 0; i < mov->frag_index.nb_items; i++)
10688  if (mov->frag_index.item[i].moof_offset <= mov->fragment.moof_offset)
10689  mov->frag_index.item[i].headers_read = 1;
10690 
10691  return 0;
10692 }
10693 
10695 {
10697  int64_t best_dts = INT64_MAX;
10698  int i;
10699  MOVContext *mov = s->priv_data;
10700  int no_interleave = !mov->interleaved_read || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL);
10701  for (i = 0; i < s->nb_streams; i++) {
10702  AVStream *avst = s->streams[i];
10703  FFStream *const avsti = ffstream(avst);
10704  MOVStreamContext *msc = avst->priv_data;
10705  if (msc->pb && msc->current_sample < avsti->nb_index_entries) {
10706  AVIndexEntry *current_sample = &avsti->index_entries[msc->current_sample];
10707  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
10708  uint64_t dtsdiff = best_dts > dts ? best_dts - (uint64_t)dts : ((uint64_t)dts - best_dts);
10709  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
10710  if (!sample || (no_interleave && current_sample->pos < sample->pos) ||
10711  ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
10712  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && dts != AV_NOPTS_VALUE &&
10713  ((dtsdiff <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
10714  (dtsdiff > AV_TIME_BASE && dts < best_dts)))))) {
10715  sample = current_sample;
10716  best_dts = dts;
10717  *st = avst;
10718  }
10719  }
10720  }
10721  return sample;
10722 }
10723 
10724 static int should_retry(AVIOContext *pb, int error_code) {
10725  if (error_code == AVERROR_EOF || avio_feof(pb))
10726  return 0;
10727 
10728  return 1;
10729 }
10730 
10731 static int mov_switch_root(AVFormatContext *s, int64_t target, int index)
10732 {
10733  int ret;
10734  MOVContext *mov = s->priv_data;
10735 
10736  if (index >= 0 && index < mov->frag_index.nb_items)
10737  target = mov->frag_index.item[index].moof_offset;
10738  if (target >= 0 && avio_seek(s->pb, target, SEEK_SET) != target) {
10739  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
10740  return AVERROR_INVALIDDATA;
10741  }
10742 
10743  mov->next_root_atom = 0;
10744  if ((index < 0 && target >= 0) || index >= mov->frag_index.nb_items)
10745  index = search_frag_moof_offset(&mov->frag_index, target);
10746  if (index >= 0 && index < mov->frag_index.nb_items &&
10747  mov->frag_index.item[index].moof_offset == target) {
10748  if (index + 1 < mov->frag_index.nb_items)
10749  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
10750  if (mov->frag_index.item[index].headers_read)
10751  return 0;
10752  mov->frag_index.item[index].headers_read = 1;
10753  }
10754 
10755  mov->found_mdat = 0;
10756 
10757  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
10758  if (ret < 0)
10759  return ret;
10760  if (avio_feof(s->pb))
10761  return AVERROR_EOF;
10762  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
10763 
10764  return 1;
10765 }
10766 
10768 {
10769  MOVStreamContext *sc = st->priv_data;
10770  uint8_t *side, *extradata;
10771  int extradata_size;
10772 
10773  /* Save the current index. */
10774  sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
10775 
10776  /* Notify the decoder that extradata changed. */
10777  extradata_size = sc->extradata_size[sc->last_stsd_index];
10778  extradata = sc->extradata[sc->last_stsd_index];
10779  if (st->discard != AVDISCARD_ALL && extradata_size > 0 && extradata) {
10782  extradata_size);
10783  if (!side)
10784  return AVERROR(ENOMEM);
10785  memcpy(side, extradata, extradata_size);
10786  }
10787 
10788  return 0;
10789 }
10790 
10792 {
10793  int new_size, ret;
10794 
10795  if (size <= 8)
10796  return AVERROR_INVALIDDATA;
10797  new_size = ((size - 8) / 2) * 3;
10798  ret = av_new_packet(pkt, new_size);
10799  if (ret < 0)
10800  return ret;
10801 
10802  avio_skip(pb, 8);
10803  for (int j = 0; j < new_size; j += 3) {
10804  pkt->data[j] = 0xFC;
10805  pkt->data[j+1] = avio_r8(pb);
10806  pkt->data[j+2] = avio_r8(pb);
10807  }
10808 
10809  return 0;
10810 }
10811 
10813  int64_t current_index, AVPacket *pkt)
10814 {
10815  MOVStreamContext *sc = st->priv_data;
10816 
10817  pkt->stream_index = sc->ffindex;
10818  pkt->dts = sample->timestamp;
10819  if (sample->flags & AVINDEX_DISCARD_FRAME) {
10821  }
10822  if (sc->stts_count && sc->tts_index < sc->tts_count)
10823  pkt->duration = sc->tts_data[sc->tts_index].duration;
10824  if (sc->ctts_count && sc->tts_index < sc->tts_count) {
10826  } else {
10827  if (pkt->duration == 0) {
10828  int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
10830  if (next_dts >= pkt->dts)
10831  pkt->duration = next_dts - pkt->dts;
10832  }
10833  pkt->pts = pkt->dts;
10834  }
10835 
10836  if (sc->tts_data && sc->tts_index < sc->tts_count) {
10837  /* update tts context */
10838  sc->tts_sample++;
10839  if (sc->tts_index < sc->tts_count &&
10840  sc->tts_data[sc->tts_index].count == sc->tts_sample) {
10841  sc->tts_index++;
10842  sc->tts_sample = 0;
10843  }
10844  }
10845 
10846  if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
10847  uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
10848  uint8_t sample_is_depended_on = (sample_flags >> 2) & 0x3;
10849  pkt->flags |= sample_is_depended_on == MOV_SAMPLE_DEPENDENCY_NO ? AV_PKT_FLAG_DISPOSABLE : 0;
10850  }
10851  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
10852  pkt->pos = sample->pos;
10853 
10854  /* Multiple stsd handling. */
10855  if (sc->stsc_data) {
10856  if (sc->stsc_data[sc->stsc_index].id > 0 &&
10857  sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
10858  sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
10859  int ret = mov_change_extradata(st, pkt);
10860  if (ret < 0)
10861  return ret;
10862  }
10863 
10864  /* Update the stsc index for the next sample */
10865  sc->stsc_sample++;
10866  if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
10867  mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
10868  sc->stsc_index++;
10869  sc->stsc_sample = 0;
10870  }
10871  }
10872 
10873  return 0;
10874 }
10875 
10877 {
10878  MOVContext *mov = s->priv_data;
10879  MOVStreamContext *sc;
10881  AVStream *st = NULL;
10882  FFStream *avsti = NULL;
10883  int64_t current_index;
10884  int ret;
10885  int i;
10886  mov->fc = s;
10887  retry:
10888  if (s->pb->pos == 0) {
10889 
10890  // Discard current fragment index
10891  if (mov->frag_index.allocated_size > 0) {
10892  for(int i = 0; i < mov->frag_index.nb_items; i++) {
10894  }
10895  av_freep(&mov->frag_index.item);
10896  mov->frag_index.nb_items = 0;
10897  mov->frag_index.allocated_size = 0;
10898  mov->frag_index.current = -1;
10899  mov->frag_index.complete = 0;
10900  }
10901 
10902  for (i = 0; i < s->nb_streams; i++) {
10903  AVStream *avst = s->streams[i];
10904  MOVStreamContext *msc = avst->priv_data;
10905 
10906  // Clear current sample
10907  mov_current_sample_set(msc, 0);
10908  msc->tts_index = 0;
10909 
10910  // Discard current index entries
10911  avsti = ffstream(avst);
10912  if (avsti->index_entries_allocated_size > 0) {
10913  av_freep(&avsti->index_entries);
10914  avsti->index_entries_allocated_size = 0;
10915  avsti->nb_index_entries = 0;
10916  }
10917  }
10918 
10919  if ((ret = mov_switch_root(s, -1, -1)) < 0)
10920  return ret;
10921  }
10922  sample = mov_find_next_sample(s, &st);
10923  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
10924  if (!mov->next_root_atom)
10925  return AVERROR_EOF;
10926  if ((ret = mov_switch_root(s, mov->next_root_atom, -1)) < 0)
10927  return ret;
10928  goto retry;
10929  }
10930  sc = st->priv_data;
10931  /* must be done just before reading, to avoid infinite loop on sample */
10932  current_index = sc->current_index;
10934 
10935  if (mov->next_root_atom) {
10936  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
10937  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
10938  }
10939 
10940  if (st->discard != AVDISCARD_ALL) {
10941  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
10942  if (ret64 != sample->pos) {
10943  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
10944  sc->ffindex, sample->pos);
10945  if (should_retry(sc->pb, ret64)) {
10947  } else if (ret64 < 0) {
10948  return (int)ret64;
10949  }
10950  return AVERROR_INVALIDDATA;
10951  }
10952 
10953  if (st->discard == AVDISCARD_NONKEY && !(sample->flags & AVINDEX_KEYFRAME)) {
10954  av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
10955  goto retry;
10956  }
10957 
10958  if (st->codecpar->codec_id == AV_CODEC_ID_EIA_608 && sample->size > 8)
10959  ret = get_eia608_packet(sc->pb, pkt, sample->size);
10960 #if CONFIG_IAMFDEC
10961  else if (sc->iamf) {
10962  int64_t pts, dts, pos, duration;
10963  int flags, size = sample->size;
10964  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
10965  pts = pkt->pts; dts = pkt->dts;
10966  pos = pkt->pos; flags = pkt->flags;
10967  duration = pkt->duration;
10968  while (!ret && size > 0) {
10969  ret = ff_iamf_read_packet(s, sc->iamf, sc->pb, size, sc->iamf_stream_offset, pkt);
10970  if (ret < 0) {
10971  if (should_retry(sc->pb, ret))
10973  return ret;
10974  }
10975  size -= ret;
10976  pkt->pts = pts; pkt->dts = dts;
10977  pkt->pos = pos; pkt->flags |= flags;
10978  pkt->duration = duration;
10979  ret = ff_buffer_packet(s, pkt);
10980  }
10981  if (!ret)
10982  return FFERROR_REDO;
10983  }
10984 #endif
10985  else
10986  ret = av_get_packet(sc->pb, pkt, sample->size);
10987  if (ret < 0) {
10988  if (should_retry(sc->pb, ret)) {
10990  }
10991  return ret;
10992  }
10993 #if CONFIG_DV_DEMUXER
10994  if (mov->dv_demux && sc->dv_audio_container) {
10997  if (ret < 0)
10998  return ret;
11000  if (ret < 0)
11001  return ret;
11002  }
11003 #endif
11004  if (sc->has_palette) {
11005  uint8_t *pal;
11006 
11008  if (!pal) {
11009  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
11010  } else {
11011  memcpy(pal, sc->palette, AVPALETTE_SIZE);
11012  sc->has_palette = 0;
11013  }
11014  }
11015  if (st->codecpar->codec_id == AV_CODEC_ID_MP3 && !ffstream(st)->need_parsing && pkt->size > 4) {
11016  if (ff_mpa_check_header(AV_RB32(pkt->data)) < 0)
11018  }
11019  }
11020 
11021  ret = mov_finalize_packet(s, st, sample, current_index, pkt);
11022  if (ret < 0)
11023  return ret;
11024 
11025  if (st->discard == AVDISCARD_ALL)
11026  goto retry;
11027 
11028  if (mov->aax_mode)
11029  aax_filter(pkt->data, pkt->size, mov);
11030 
11031  ret = cenc_filter(mov, st, sc, pkt, current_index);
11032  if (ret < 0) {
11033  return ret;
11034  }
11035 
11036  return 0;
11037 }
11038 
11040 {
11041  MOVContext *mov = s->priv_data;
11042  int index;
11043 
11044  if (!mov->frag_index.complete)
11045  return 0;
11046 
11047  index = search_frag_timestamp(s, &mov->frag_index, st, timestamp);
11048  if (index < 0)
11049  index = 0;
11050  if (!mov->frag_index.item[index].headers_read)
11051  return mov_switch_root(s, -1, index);
11052  if (index + 1 < mov->frag_index.nb_items)
11053  mov->next_root_atom = mov->frag_index.item[index + 1].moof_offset;
11054 
11055  return 0;
11056 }
11057 
11058 static int is_open_key_sample(const MOVStreamContext *sc, int sample)
11059 {
11060  // TODO: a bisect search would scale much better
11061  for (int i = 0; i < sc->open_key_samples_count; i++) {
11062  const int oks = sc->open_key_samples[i];
11063  if (oks == sample)
11064  return 1;
11065  if (oks > sample) /* list is monotically increasing so we can stop early */
11066  break;
11067  }
11068  return 0;
11069 }
11070 
11071 /*
11072  * Some key sample may be key frames but not IDR frames, so a random access to
11073  * them may not be allowed.
11074  */
11075 static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
11076 {
11077  MOVStreamContext *sc = st->priv_data;
11078  FFStream *const sti = ffstream(st);
11079  int64_t key_sample_dts, key_sample_pts;
11080 
11081  if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
11082  return 1;
11083 
11084  if (sample >= sc->sample_offsets_count)
11085  return 1;
11086 
11087  key_sample_dts = sti->index_entries[sample].timestamp;
11088  key_sample_pts = key_sample_dts + sc->sample_offsets[sample] + sc->dts_shift;
11089 
11090  /*
11091  * If the sample needs to be presented before an open key sample, they may
11092  * not be decodable properly, even though they come after in decoding
11093  * order.
11094  */
11095  if (is_open_key_sample(sc, sample) && key_sample_pts > requested_pts)
11096  return 0;
11097 
11098  return 1;
11099 }
11100 
11101 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
11102 {
11103  MOVStreamContext *sc = st->priv_data;
11104  FFStream *const sti = ffstream(st);
11105  int sample, time_sample, ret, next_ts, requested_sample;
11106  unsigned int i;
11107 
11108  // Here we consider timestamp to be PTS, hence try to offset it so that we
11109  // can search over the DTS timeline.
11110  timestamp -= (sc->min_corrected_pts + sc->dts_shift);
11111 
11112  ret = mov_seek_fragment(s, st, timestamp);
11113  if (ret < 0)
11114  return ret;
11115 
11116  for (;;) {
11117  sample = av_index_search_timestamp(st, timestamp, flags);
11118  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
11119  if (sample < 0 && sti->nb_index_entries && timestamp < sti->index_entries[0].timestamp)
11120  sample = 0;
11121  if (sample < 0) /* not sure what to do */
11122  return AVERROR_INVALIDDATA;
11123 
11124  if (!sample || can_seek_to_key_sample(st, sample, timestamp))
11125  break;
11126 
11127  next_ts = timestamp - FFMAX(sc->min_sample_duration, 1);
11128  requested_sample = av_index_search_timestamp(st, next_ts, flags);
11129 
11130  // If we've reached a different sample trying to find a good pts to
11131  // seek to, give up searching because we'll end up seeking back to
11132  // sample 0 on every seek.
11133  if (sample != requested_sample && !can_seek_to_key_sample(st, requested_sample, next_ts))
11134  break;
11135 
11136  timestamp = next_ts;
11137  }
11138 
11140  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
11141  /* adjust time to sample index */
11142  if (sc->tts_data) {
11143  time_sample = 0;
11144  for (i = 0; i < sc->tts_count; i++) {
11145  int next = time_sample + sc->tts_data[i].count;
11146  if (next > sc->current_sample) {
11147  sc->tts_index = i;
11148  sc->tts_sample = sc->current_sample - time_sample;
11149  break;
11150  }
11151  time_sample = next;
11152  }
11153  }
11154 
11155  /* adjust stsd index */
11156  if (sc->chunk_count) {
11157  time_sample = 0;
11158  for (i = 0; i < sc->stsc_count; i++) {
11159  int64_t next = time_sample + mov_get_stsc_samples(sc, i);
11160  if (next > sc->current_sample) {
11161  sc->stsc_index = i;
11162  sc->stsc_sample = sc->current_sample - time_sample;
11163  break;
11164  }
11165  av_assert0(next == (int)next);
11166  time_sample = next;
11167  }
11168  }
11169 
11170  return sample;
11171 }
11172 
11174 {
11175  MOVStreamContext *sc = st->priv_data;
11176  FFStream *const sti = ffstream(st);
11177  int64_t first_ts = sti->index_entries[0].timestamp;
11179  int64_t off;
11180 
11182  return 0;
11183 
11184  /* compute skip samples according to stream start_pad, seek ts and first ts */
11185  off = av_rescale_q(ts - first_ts, st->time_base,
11186  (AVRational){1, st->codecpar->sample_rate});
11187  return FFMAX(sc->start_pad - off, 0);
11188 }
11189 
11190 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
11191 {
11192  MOVContext *mc = s->priv_data;
11193  AVStream *st;
11194  FFStream *sti;
11195  int sample;
11196  int i;
11197 
11198  if (stream_index >= s->nb_streams)
11199  return AVERROR_INVALIDDATA;
11200 
11201  st = s->streams[stream_index];
11202  sti = ffstream(st);
11203  sample = mov_seek_stream(s, st, sample_time, flags);
11204  if (sample < 0)
11205  return sample;
11206 
11207  if (mc->seek_individually) {
11208  /* adjust seek timestamp to found sample timestamp */
11209  int64_t seek_timestamp = sti->index_entries[sample].timestamp;
11211 
11212  for (i = 0; i < s->nb_streams; i++) {
11213  AVStream *const st = s->streams[i];
11214  FFStream *const sti = ffstream(st);
11215  int64_t timestamp;
11216 
11217  if (stream_index == i)
11218  continue;
11219 
11220  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
11221  sample = mov_seek_stream(s, st, timestamp, flags);
11222  if (sample >= 0)
11224  }
11225  } else {
11226  for (i = 0; i < s->nb_streams; i++) {
11227  MOVStreamContext *sc;
11228  st = s->streams[i];
11229  sc = st->priv_data;
11230  mov_current_sample_set(sc, 0);
11231  }
11232  while (1) {
11233  MOVStreamContext *sc;
11235  if (!entry)
11236  return AVERROR_INVALIDDATA;
11237  sc = st->priv_data;
11238  if (sc->ffindex == stream_index && sc->current_sample == sample)
11239  break;
11241  }
11242  }
11243  return 0;
11244 }
11245 
11246 #define OFFSET(x) offsetof(MOVContext, x)
11247 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
11248 static const AVOption mov_options[] = {
11249  {"use_absolute_path",
11250  "allow using absolute path when opening alias, this is a possible security issue",
11251  OFFSET(use_absolute_path), AV_OPT_TYPE_BOOL, {.i64 = 0},
11252  0, 1, FLAGS},
11253  {"seek_streams_individually",
11254  "Seek each stream individually to the closest point",
11255  OFFSET(seek_individually), AV_OPT_TYPE_BOOL, { .i64 = 1 },
11256  0, 1, FLAGS},
11257  {"ignore_editlist", "Ignore the edit list atom.", OFFSET(ignore_editlist), AV_OPT_TYPE_BOOL, {.i64 = 0},
11258  0, 1, FLAGS},
11259  {"advanced_editlist",
11260  "Modify the AVIndex according to the editlists. Use this option to decode in the order specified by the edits.",
11261  OFFSET(advanced_editlist), AV_OPT_TYPE_BOOL, {.i64 = 1},
11262  0, 1, FLAGS},
11263  {"ignore_chapters", "", OFFSET(ignore_chapters), AV_OPT_TYPE_BOOL, {.i64 = 0},
11264  0, 1, FLAGS},
11265  {"use_mfra_for",
11266  "use mfra for fragment timestamps",
11267  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
11269  .unit = "use_mfra_for"},
11270  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
11271  FLAGS, .unit = "use_mfra_for" },
11272  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
11273  FLAGS, .unit = "use_mfra_for" },
11274  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
11275  FLAGS, .unit = "use_mfra_for" },
11276  {"use_tfdt", "use tfdt for fragment timestamps", OFFSET(use_tfdt), AV_OPT_TYPE_BOOL, {.i64 = 1},
11277  0, 1, FLAGS},
11278  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
11279  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
11280  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
11281  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
11282  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
11284  { "audible_key", "AES-128 Key for Audible AAXC files", OFFSET(audible_key),
11286  { "audible_iv", "AES-128 IV for Audible AAXC files", OFFSET(audible_iv),
11288  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
11289  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
11290  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
11291  .flags = AV_OPT_FLAG_DECODING_PARAM },
11292  { "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
11293  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
11294  {.i64 = 0}, 0, 1, FLAGS },
11295  { "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 },
11296  { "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 },
11297 
11298  { NULL },
11299 };
11300 
11301 static const AVClass mov_class = {
11302  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
11303  .item_name = av_default_item_name,
11304  .option = mov_options,
11305  .version = LIBAVUTIL_VERSION_INT,
11306 };
11307 
11309  .p.name = "mov,mp4,m4a,3gp,3g2,mj2",
11310  .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
11311  .p.priv_class = &mov_class,
11312  .p.extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v,avif,heic,heif",
11314  .priv_data_size = sizeof(MOVContext),
11315  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
11316  .read_probe = mov_probe,
11321 };
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:191
item_name
item_name
Definition: libkvazaar.c:319
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:335
mov_read_chpl
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:598
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:7242
mov_read_meta
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5460
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:568
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:430
MOVContext::found_iloc
int found_iloc
'iloc' atom has been found
Definition: isom.h:320
AV_CODEC_ID_MACE6
@ AV_CODEC_ID_MACE6
Definition: codec_id.h:458
MOVFragmentStreamInfo::first_tfra_pts
int64_t first_tfra_pts
Definition: isom.h:142
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
add frame for rfps calculation.
Definition: demux.c:2295
read_image_iovl
static int read_image_iovl(AVFormatContext *s, const HEIFGrid *grid, AVStreamGroupTileGrid *tile_grid)
Definition: mov.c:10131
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:9054
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AVIAMFSubmix::elements
AVIAMFSubmixElement ** elements
Array of submix elements.
Definition: iamf.h:565
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
MOV_TFHD_DEFAULT_FLAGS
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:400
PUT_UTF8
#define PUT_UTF8(val, tmp, PUT_BYTE)
Definition: common.h:541
AV_TIMECODE_STR_SIZE
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
av_aes_init
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:201
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:355
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:212
MOVStreamContext::audio_cid
int16_t audio_cid
stsd audio compression id
Definition: isom.h:221
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:380
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
mov_read_dops
static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8309
can_seek_to_key_sample
static int can_seek_to_key_sample(AVStream *st, int sample, int64_t requested_pts)
Definition: mov.c:11075
AV_CODEC_ID_ADPCM_IMA_QT
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition: codec_id.h:374
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:1153
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:451
HEIFItem::name
char * name
Definition: isom.h:291
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:243
MOVStreamContext::height
int height
tkhd height
Definition: isom.h:229
MOVContext::moov_retry
int moov_retry
Definition: isom.h:348
MOV_TRUN_SAMPLE_FLAGS
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:408
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:336
mix
static int mix(int c0, int c1)
Definition: 4xm.c:716
MOVStreamContext::last_stsd_index
int last_stsd_index
Definition: isom.h:258
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
MOV_TKHD_FLAG_ENABLED
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:421
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVStreamGroup::tile_grid
struct AVStreamGroupTileGrid * tile_grid
Definition: avformat.h:1169
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:98
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
AV_STREAM_GROUP_PARAMS_LCEVC
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition: avformat.h:1128
MOVStreamContext::extradata
uint8_t ** extradata
extradata array (and size) for multiple stsd
Definition: isom.h:256
mov_class
static const AVClass mov_class
Definition: mov.c:11301
AVSphericalMapping::bound_bottom
uint32_t bound_bottom
Distance from the bottom edge.
Definition: spherical.h:182
MOVStreamContext::open_key_samples
int * open_key_samples
Definition: isom.h:248
AVUUID
uint8_t AVUUID[AV_UUID_LEN]
Definition: uuid.h:60
out
FILE * out
Definition: movenc.c:55
MOVFragmentStreamInfo
Definition: isom.h:139
AVFieldOrder
AVFieldOrder
Definition: defs.h:200
mov_read_targa_y216
static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2272
mov_read_chnl
static int mov_read_chnl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1192
mov_read_moof
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1809
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
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
HEIFItem::icc_profile
uint8_t * icc_profile
Definition: isom.h:302
AVFMT_FLAG_IGNIDX
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1453
sanity_checks
static int sanity_checks(void *log_obj, MOVStreamContext *sc, int index)
Definition: mov.c:5090
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:169
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:9862
HEIFItem::hflip
int hflip
Definition: isom.h:298
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:773
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
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:674
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:819
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:202
mov_options
static const AVOption mov_options[]
Definition: mov.c:11248
mov_codec_id
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:2621
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:246
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:8700
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:601
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:421
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:225
mov_read_alac
static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2244
test_same_origin
static int test_same_origin(const char *src, const char *ref)
Definition: mov.c:4933
cbcs_scheme_decrypt
static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8132
MOVFragment::base_data_offset
uint64_t base_data_offset
Definition: isom.h:104
MOVStreamContext
Definition: isom.h:173
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:194
ff_buffer_packet
int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
Definition: demux.c:612
IS_MATRIX_IDENT
#define IS_MATRIX_IDENT(matrix)
Definition: mov.c:5478
AVStreamGroup::disposition
int disposition
Stream group disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:1211
dict_internal.h
av_unused
#define av_unused
Definition: attributes.h:131
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:621
mov_update_dts_shift
static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
Definition: mov.c:3630
MOVStreamContext::spherical
AVSphericalMapping * spherical
Definition: isom.h:265
mask
int mask
Definition: mediacodecdec_common.c:154
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
AVContentLightMetadata::MaxCLL
unsigned MaxCLL
Max content light level (cd/m^2).
Definition: mastering_display_metadata.h:111
avio_get_str16be
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
MOVEncryptionIndex
Definition: isom.h:126
av_encryption_init_info_free
void av_encryption_init_info_free(AVEncryptionInitInfo *info)
Frees the given encryption init info object.
Definition: encryption_info.c:216
mov_read_avss
static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2249
AV_RB32A
#define AV_RB32A(p)
Definition: intreadwrite.h:575
MOVContext::primary_item_id
int primary_item_id
Definition: isom.h:373
mode
Definition: swscale.c:52
MOVContext::found_moov
int found_moov
'moov' atom has been found
Definition: isom.h:319
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:279
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
mov_read_custom
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5322
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:1368
HEIFGrid::nb_tiles
int nb_tiles
Definition: isom.h:311
ID3v1_GENRE_MAX
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
MOVSbgp
Definition: isom.h:121
MOVCtts
Definition: isom.h:68
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:390
mov_read_extradata
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, enum AVCodecID codec_id)
Definition: mov.c:2218
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:733
mpegaudiodecheader.h
MOVStreamContext::rap_group_count
unsigned int rap_group_count
Definition: isom.h:240
av_display_matrix_flip
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:66
av_sha_init
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:274
HEIFItem::type
int type
Definition: isom.h:300
mov_read_mvhd
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1911
mov_read_idat
static int mov_read_idat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8694
AVPacket::data
uint8_t * data
Definition: packet.h:539
MOVContext::found_mdat
int found_mdat
'mdat' atom has been found
Definition: isom.h:322
MOVStreamContext::drefs_count
unsigned drefs_count
Definition: isom.h:222
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:2264
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
Audio only.
Definition: codec_par.h:214
av_dynarray2_add
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:343
AVOption
AVOption.
Definition: opt.h:429
MOVContext::trex_data
MOVTrackExt * trex_data
Definition: isom.h:331
mov_read_stps
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3287
MOVContext::bitrates
int * bitrates
bitrates read before streams creation
Definition: isom.h:346
b
#define b
Definition: input.c:41
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:630
MOVContext::interleaved_read
int interleaved_read
Definition: isom.h:381
mov_read_tkhd
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5486
MOVElst::rate
float rate
Definition: isom.h:82
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:837
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:2065
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:1950
mov_read_strf
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:2534
AV_CODEC_ID_ALAC
@ AV_CODEC_ID_ALAC
Definition: codec_id.h:464
HEIFItem::st
AVStream * st
Definition: isom.h:290
FF_COMPLIANCE_STRICT
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition: defs.h:59
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:429
av_iamf_mix_presentation_free
void av_iamf_mix_presentation_free(AVIAMFMixPresentation **pmix_presentation)
Free an AVIAMFMixPresentation and all its contents.
Definition: iamf.c:534
mov_parse_auxiliary_info
static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
Definition: mov.c:7450
mov_seek_stream
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:11101
mov_read_saio
static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7596
mov_get_stsc_samples
static int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
Definition: mov.c:3272
ff_get_wav_header
int ff_get_wav_header(void *logctx, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:95
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
ff_codec_wav_tags
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:530
get_edit_list_entry
static int get_edit_list_entry(MOVContext *mov, const MOVStreamContext *msc, unsigned int edit_list_index, int64_t *edit_list_media_time, int64_t *edit_list_duration, int64_t global_timescale)
Get ith edit list entry (media time, duration).
Definition: mov.c:3846
MOVFragmentIndexItem::moof_offset
int64_t moof_offset
Definition: isom.h:153
MOVStreamContext::spherical_size
size_t spherical_size
Definition: isom.h:266
mov_change_extradata
static int mov_change_extradata(AVStream *st, AVPacket *pkt)
Definition: mov.c:10767
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:472
MOVStreamContext::tref_id
int tref_id
Definition: isom.h:226
MP4TrackKindMapping::scheme_uri
const char * scheme_uri
Definition: isom.h:483
AVINDEX_DISCARD_FRAME
#define AVINDEX_DISCARD_FRAME
Definition: avformat.h:611
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:557
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
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:4150
mathematics.h
AVDictionary
Definition: dict.c:34
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:613
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
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:94
AVStreamGroupTileGrid::vertical
int vertical
Offset in pixels from the top edge of the canvas where the tile should be placed.
Definition: avformat.h:1036
iamf_parse.h
mov_read_moov
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1561
FFNABS
#define FFNABS(a)
Negative Absolute value.
Definition: common.h:83
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:847
MOVStreamContext::sample_count
unsigned int sample_count
Definition: isom.h:205
MOVFragmentIndex::allocated_size
int allocated_size
Definition: isom.h:161
MOVTrackExt::flags
unsigned flags
Definition: isom.h:118
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
HEIFItem::height
int height
Definition: isom.h:296
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:192
ff_dvdclut_yuv_to_rgb
int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size)
Definition: dvdclut.c:50
AV_CODEC_ID_R10K
@ AV_CODEC_ID_R10K
Definition: codec_id.h:197
heif_cur_item
static HEIFItem * heif_cur_item(MOVContext *c)
Get the current item in the parsing process.
Definition: mov.c:194
av_encryption_init_info_get_side_data
AVEncryptionInitInfo * av_encryption_init_info_get_side_data(const uint8_t *side_data, size_t side_data_size)
Creates a copy of the AVEncryptionInitInfo that is contained in the given side data.
Definition: encryption_info.c: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:340
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:323
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:2556
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:6969
FFIOContext
Definition: avio_internal.h:28
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVStreamGroup::params
union AVStreamGroup::@368 params
Group type-specific parameters.
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
MOVStreamContext::aes_ctr
struct AVAESCTR * aes_ctr
Definition: isom.h:278
cenc_filter
static int cenc_filter(MOVContext *mov, AVStream *st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
Definition: mov.c:8239
mov_read_sdtp
static int mov_read_sdtp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3596
mov_read_jp2h
static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2254
AV_STEREO3D_UNSPEC
@ AV_STEREO3D_UNSPEC
Video is stereoscopic but the packing is unspecified.
Definition: stereo3d.h:143
AVIndexEntry
Definition: avformat.h:602
AV_WB64
#define AV_WB64(p, v)
Definition: intreadwrite.h:429
MOVStreamContext::dv_audio_container
int dv_audio_container
Definition: isom.h:219
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:430
mov_read_tfhd
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5591
AV_CODEC_ID_BIN_DATA
@ AV_CODEC_ID_BIN_DATA
Definition: codec_id.h:599
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
OPUS_SEEK_PREROLL_MS
#define OPUS_SEEK_PREROLL_MS
Definition: oggparseopus.c:36
AV_CODEC_ID_H261
@ AV_CODEC_ID_H261
Definition: codec_id.h:55
IAMFMixPresentation::cmix
const AVIAMFMixPresentation * cmix
Definition: iamf.h:108
mov_read_wide
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6170
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:610
AV_FIELD_BT
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition: defs.h:206
MOVStreamContext::stsd_count
int stsd_count
Definition: isom.h:259
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:203
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:338
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:381
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:167
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:193
MOVStreamContext::has_palette
int has_palette
Definition: isom.h:234
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
AV_STEREO3D_SIDEBYSIDE
@ AV_STEREO3D_SIDEBYSIDE
Views are next to each other.
Definition: stereo3d.h:64
MOVIndexRange::start
int64_t start
Definition: isom.h:169
AVPacketSideData::size
size_t size
Definition: packet.h:392
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
ff_remove_stream
void ff_remove_stream(AVFormatContext *s, AVStream *st)
Remove a stream from its AVFormatContext and free it.
Definition: avformat.c:121
OFFSET
#define OFFSET(x)
Definition: mov.c:11246
HEIFGrid::tile_item_list
HEIFItem ** tile_item_list
Definition: isom.h:308
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:9997
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:203
AV_STEREO3D_VIEW_PACKED
@ AV_STEREO3D_VIEW_PACKED
Frame contains two packed views.
Definition: stereo3d.h:153
MOVStreamContext::nb_frames_for_fps
int nb_frames_for_fps
Definition: isom.h:252
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:736
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:868
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:1499
AV_OPT_TYPE_BINARY
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
Definition: opt.h:286
av_color_space_name
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:3624
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:198
MOVStreamContext::mastering
AVMasteringDisplayMetadata * mastering
Definition: isom.h:267
AV_CODEC_ID_SPEEX
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:483
AV_FOURCC_MAX_STRING_SIZE
#define AV_FOURCC_MAX_STRING_SIZE
Definition: avutil.h:346
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
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
MOVFragmentIndexItem::current
int current
Definition: isom.h:155
AVFMT_SEEK_TO_PTS
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition: avformat.h:503
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:336
mov_read_mdhd
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1865
mov_read_ctts
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3641
MOVTrackExt
Definition: isom.h:113
MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:412
fail
#define fail()
Definition: checkasm.h:193
mov_read_aclr
static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2329
AVSEEK_FLAG_ANY
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2501
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:102
AVStreamGroupTileGrid
AVStreamGroupTileGrid holds information on how to combine several independent images on a single canv...
Definition: avformat.h:987
MOVContext::decryption_key
uint8_t * decryption_key
Definition: isom.h:366
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:292
timecode.h
get_current_frag_stream_info
static MOVFragmentStreamInfo * get_current_frag_stream_info(MOVFragmentIndex *frag_index)
Definition: mov.c:1618
GetBitContext
Definition: get_bits.h:108
MOVStreamContext::mastering_size
size_t mastering_size
Definition: isom.h:268
AVStreamGroupTileGrid::coded_width
int coded_width
Width of the canvas.
Definition: avformat.h:1002
av_iamf_audio_element_free
void av_iamf_audio_element_free(AVIAMFAudioElement **paudio_element)
Free an AVIAMFAudioElement and all its contents.
Definition: iamf.c:336
FFStream::index_entries_allocated_size
unsigned int index_entries_allocated_size
Definition: internal.h:191
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:6524
mov_read_enda
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1979
mov_read_chap
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5638
MOVParseTableEntry
Definition: mov.c:82
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
MOV_TRUN_SAMPLE_DURATION
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:406
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:10011
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:314
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:679
AVStreamGroupTileGrid::coded_height
int coded_height
Width of the canvas.
Definition: avformat.h:1008
pts
static int64_t pts
Definition: transcode_aac.c:644
MOVStreamContext::v_spacing
int v_spacing
pasp vSpacing
Definition: isom.h:231
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:449
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
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:228
MOVContext::meta_keys
char ** meta_keys
Definition: isom.h:325
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:257
AVIAMFAudioElement::audio_element_type
enum AVIAMFAudioElementType audio_element_type
Audio element type as defined in section 3.6 of IAMF.
Definition: iamf.h:388
loop
static int loop
Definition: ffplay.c:335
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:448
update_frag_index
static int update_frag_index(MOVContext *c, int64_t offset)
Definition: mov.c:1733
AVRational::num
int num
Numerator.
Definition: rational.h:59
MOVStreamContext::keyframes
int * keyframes
Definition: isom.h:209
MOVEncryptionIndex::auxiliary_info_sample_count
size_t auxiliary_info_sample_count
Definition: isom.h:133
AV_FIELD_TB
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition: defs.h:205
AVStream::attached_pic
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:846
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:7512
MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:419
mov_merge_tts_data
static int mov_merge_tts_data(MOVContext *mov, AVStream *st, int flags)
Definition: mov.c:4561
MOVContext::idat_offset
int64_t idat_offset
Definition: isom.h:380
MOV_TRUN_DATA_OFFSET
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:404
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:199
AVStreamGroupTileGrid::offsets
struct AVStreamGroupTileGrid::@367 * offsets
An nb_tiles sized array of offsets in pixels from the topleft edge of the canvas, indicating where ea...
mov_read_ares
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2286
mov_read_adrm
static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1375
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:550
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:168
IAMFContext::audio_elements
IAMFAudioElement ** audio_elements
Definition: iamf.h:131
MOVFragmentIndex::complete
int complete
Definition: isom.h:162
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:339
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:235
MOVStreamContext::stsc_sample
int stsc_sample
Definition: isom.h:196
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
AV_CODEC_ID_MACE3
@ AV_CODEC_ID_MACE3
Definition: codec_id.h:457
MOVTrackExt::track_id
unsigned track_id
Definition: isom.h:114
mov_free_encryption_index
static void mov_free_encryption_index(MOVEncryptionIndex **index)
Definition: mov.c:9734
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:178
duration
int64_t duration
Definition: movenc.c:65
MOVEncryptionIndex::auxiliary_offsets
uint64_t * auxiliary_offsets
Absolute seek position.
Definition: isom.h:135
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:201
MOVStreamContext::dts_shift
int dts_shift
dts shift when ctts is negative
Definition: isom.h:232
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:222
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:195
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:91
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:62
AES_CTR_KEY_SIZE
#define AES_CTR_KEY_SIZE
Definition: aes_ctr.h:35
MOVStreamContext::stsd_version
int stsd_version
Definition: isom.h:260
AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
@ AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
Definition: avformat.h:1126
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:119
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:4041
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
FF_MOV_FLAG_MFRA_PTS
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:456
MOV_MERGE_STTS
#define MOV_MERGE_STTS
Definition: mov.c:4555
MOVStreamContext::iamf_stream_offset
int iamf_stream_offset
Definition: isom.h:286
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:6458
s
#define s(width, name)
Definition: cbs_vp9.c:198
MOVFragmentStreamInfo::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:148
mov_read_trak
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5108
IAMFSubStream::audio_substream_id
unsigned int audio_substream_id
Definition: iamf.h:83
MOVContext::fc
AVFormatContext * fc
Definition: isom.h:316
MOV_TFHD_DEFAULT_BASE_IS_MOOF
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:402
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:99
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:130
AV_CODEC_ID_EVC
@ AV_CODEC_ID_EVC
Definition: codec_id.h:325
IAMFLayer::substream_count
unsigned int substream_count
Definition: iamf.h:78
MOV_TFHD_DEFAULT_DURATION
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:398
ALAC_EXTRADATA_SIZE
#define ALAC_EXTRADATA_SIZE
DRM_BLOB_SIZE
#define DRM_BLOB_SIZE
Definition: mov.c:1373
MOVStreamContext::sample_offsets_count
int sample_offsets_count
Definition: isom.h:247
MOVCtts::count
unsigned int count
Definition: isom.h:69
MOVStreamContext::drefs
MOVDref * drefs
Definition: isom.h:223
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
MOVContext::aes_decrypt
struct AVAES * aes_decrypt
Definition: isom.h:365
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVSphericalMapping::bound_top
uint32_t bound_top
Distance from the top edge.
Definition: spherical.h:180
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:222
MOVFragmentIndex::nb_items
int nb_items
Definition: isom.h:164
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
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:448
HEIFGrid::tile_idx_list
unsigned * tile_idx_list
Definition: isom.h:310
MOV_TRUN_FIRST_SAMPLE_FLAGS
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:405
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:613
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:252
MOVStreamContext::keyframe_absent
int keyframe_absent
Definition: isom.h:207
info
MIPS optimizations info
Definition: mips.txt:2
MOVStts::duration
unsigned int duration
Definition: isom.h:65
MOVStreamContext::coll_size
size_t coll_size
Definition: isom.h:270
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:4070
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:604
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
MOVStreamContext::min_corrected_pts
int64_t min_corrected_pts
minimum Composition time shown by the edits excluding empty edits.
Definition: isom.h:212
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:927
tile_cols
int tile_cols
Definition: av1_levels.c:73
MOVStreamContext::sdtp_count
unsigned int sdtp_count
Definition: isom.h:188
dvdclut.h
mov_read_lhvc
static int mov_read_lhvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8413
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
AVPacketSideData::data
uint8_t * data
Definition: packet.h:391
ctx
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:6032
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
nb_streams
static int nb_streams
Definition: ffprobe.c:385
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:1076
FFStream::display_aspect_ratio
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition: internal.h:307
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:10694
AV_CODEC_ID_TARGA_Y216
@ AV_CODEC_ID_TARGA_Y216
Definition: codec_id.h:258
AVIndexEntry::min_distance
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:614
mov_read_dvcc_dvvc
static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8393
MOVParseTableEntry::parse
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:84
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:75
key
const char * key
Definition: hwcontext_opencl.c:189
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:86
MOVStreamContext::sdtp_data
uint8_t * sdtp_data
Definition: isom.h:189
color_range
color_range
Definition: vf_selectivecolor.c:43
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
mov_read_udta_string
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:345
mov_read_stss
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3328
mov_read_ddts
static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1120
mov_read_uuid
static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7115
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:605
MOVTrackExt::duration
unsigned duration
Definition: isom.h:116
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
av_content_light_metadata_alloc
AVContentLightMetadata * av_content_light_metadata_alloc(size_t *size)
Allocate an AVContentLightMetadata structure and set its fields to default values.
Definition: mastering_display_metadata.c:72
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:213
MOVFragmentStreamInfo::sidx_pts
int64_t sidx_pts
Definition: isom.h:141
MAX_REORDER_DELAY
#define MAX_REORDER_DELAY
Definition: mov.c:4069
MOVFragmentIndex::current
int current
Definition: isom.h:163
MOVEncryptionIndex::encrypted_samples
AVEncryptionInfo ** encrypted_samples
Definition: isom.h:130
mov_read_close
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:9808
MOVAtom::size
int64_t size
Definition: isom.h:96
MOVStreamContext::refcount
int refcount
Definition: isom.h:175
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:462
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
AV_CODEC_ID_AVUI
@ AV_CODEC_ID_AVUI
Definition: codec_id.h:257
if
if(ret)
Definition: filter_design.txt:179
mov_read_cmov
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6190
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:7340
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:325
MOVStreamContext::keyframe_count
unsigned int keyframe_count
Definition: isom.h:208
mov_read_SAND
static int mov_read_SAND(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8641
IAMFAudioElement::audio_element_id
unsigned int audio_element_id
Definition: iamf.h:96
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:221
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:32
mov_read_stts
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3476
MOVStreamContext::index_ranges
MOVIndexRange * index_ranges
Definition: isom.h:215
DDTS_SIZE
#define DDTS_SIZE
internal.h
MOVTrackExt::stsd_id
unsigned stsd_id
Definition: isom.h:115
AVStreamGroupLCEVC::height
int height
Height of the final image for presentation.
Definition: avformat.h:1120
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:3892
set_frag_stream
static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
Definition: mov.c:1598
mov_read_free
static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7220
mov_realloc_extradata
static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
Definition: mov.c:2181
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2499
aes.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
MOVStreamContext::tts_sample
int tts_sample
Definition: isom.h:202
avpriv_dv_get_packet
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:731
MOVContext::ignore_editlist
int ignore_editlist
Definition: isom.h:338
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:787
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
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
MOVStreamContext::ctts_count
unsigned int ctts_count
Definition: isom.h:190
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:478
mov_read_ftyp
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1513
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
MOVContext::nb_heif_grid
int nb_heif_grid
Definition: isom.h:378
read_image_grid
static int read_image_grid(AVFormatContext *s, const HEIFGrid *grid, AVStreamGroupTileGrid *tile_grid)
Definition: mov.c:10032
MOVElst
Definition: isom.h:79
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: ffv1_parser.c:28
av_aes_ctr_alloc
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:40
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:1090
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:3729
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:9454
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:46
MOVDref::nlvl_to
int16_t nlvl_to
Definition: isom.h:91
AV_CODEC_ID_DVD_SUBTITLE
@ AV_CODEC_ID_DVD_SUBTITLE
Definition: codec_id.h:558
AVIndexEntry::flags
int flags
Definition: avformat.h:612
MOVStreamContext::time_offset
int64_t time_offset
time offset of the edit list entries
Definition: isom.h:211
mov_read_smdm
static int mov_read_smdm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6367
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:239
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:908
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:169
MOVStreamContext::current_index_range
MOVIndexRange * current_index_range
Definition: isom.h:216
mov_open_dref
static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
Definition: mov.c:4962
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:190
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
av_aes_alloc
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:35
TAG_IS_AVCI
#define TAG_IS_AVCI(tag)
Definition: isom.h:432
IAMFSubStream
Definition: iamf.h:82
MOVStreamContext::timecode_track
int timecode_track
Definition: isom.h:227
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:7812
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:828
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:3582
FF_MOV_FLAG_MFRA_DTS
#define FF_MOV_FLAG_MFRA_DTS
Definition: isom.h:455
MOV_SAMPLE_DEPENDENCY_NO
#define MOV_SAMPLE_DEPENDENCY_NO
Definition: isom.h:428
mov_read_iref_thmb
static int mov_read_iref_thmb(MOVContext *c, AVIOContext *pb, int version)
Definition: mov.c:8970
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
MOVStreamContext::tts_index
int tts_index
Definition: isom.h:201
AV_DISPOSITION_MULTILAYER
#define AV_DISPOSITION_MULTILAYER
The video stream contains multiple layers, e.g.
Definition: avformat.h:718
MOVFragmentStreamInfo::index_base
int index_base
Definition: isom.h:146
MOVStreamContext::rap_group
MOVSbgp * rap_group
Definition: isom.h:241
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:467
mov_read_ilst
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5267
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
Audio only.
Definition: codec_par.h:180
get_frag_stream_info_from_pkt
static MOVFragmentStreamInfo * get_frag_stream_info_from_pkt(MOVFragmentIndex *frag_index, AVPacket *pkt, int id)
Definition: mov.c:8214
get_sgpd_sync_index
static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
Definition: mov.c:4483
mov_read_fiel
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2147
MOV_MP4_TTML_TAG
#define MOV_MP4_TTML_TAG
Definition: isom.h:473
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:396
MOVFragmentStreamInfo::stsd_id
int stsd_id
Definition: isom.h:149
MOVStreamContext::open_key_samples_count
int open_key_samples_count
Definition: isom.h:249
HEIFItem
Definition: isom.h:289
ff_codec_movaudio_tags
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom_tags.c:303
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:1224
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:40
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:461
MOVSbgp::count
unsigned int count
Definition: isom.h:122
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
mov_parse_stsd_subtitle
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:2836
cid
uint16_t cid
Definition: mxfenc.c:2285
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:2998
MOVStts
Definition: isom.h:63
AVAudioServiceType
AVAudioServiceType
Definition: defs.h:224
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition: codec_id.h:53
AV_CODEC_ID_GSM
@ AV_CODEC_ID_GSM
as in Berlin toast format
Definition: codec_id.h:466
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:809
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av_packet_side_data_get
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition: packet.c:657
AVIAMFSubmixElement::audio_element_id
unsigned int audio_element_id
The id of the Audio Element this submix element references.
Definition: iamf.h:452
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:488
should_retry
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:10724
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:529
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:73
AVIAMFSubmix
Submix layout as defined in section 3.7 of IAMF.
Definition: iamf.h:556
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:1343
MOVContext::dv_demux
DVDemuxContext * dv_demux
Definition: isom.h:327
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:450
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:8551
mov_read_elst
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6243
MOVEncryptionIndex::auxiliary_info_default_size
uint8_t auxiliary_info_default_size
Definition: isom.h:134
AV_STREAM_GROUP_PARAMS_TILE_GRID
@ AV_STREAM_GROUP_PARAMS_TILE_GRID
Definition: avformat.h:1127
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:10459
AV_CODEC_ID_QCELP
@ AV_CODEC_ID_QCELP
Definition: codec_id.h:472
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:8007
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
MOVStreamContext::tref_flags
unsigned tref_flags
Definition: isom.h:225
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:220
MOVFragment::flags
unsigned flags
Definition: isom.h:110
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:347
mov_read_wave
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2377
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:754
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:279
MOV_TREF_FLAG_ENHANCEMENT
#define MOV_TREF_FLAG_ENHANCEMENT
Definition: isom.h:430
cens_scheme_decrypt
static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8067
MOVContext::handbrake_version
int handbrake_version
Definition: isom.h:334
mov_free_stream_context
static void mov_free_stream_context(AVFormatContext *s, AVStream *st)
Definition: mov.c:9746
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:133
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:162
MOVFragmentIndexItem
Definition: isom.h:152
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:7289
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:73
height
#define height
Definition: dsp.h:85
qtpalette.h
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
FFStream
Definition: internal.h:132
mov_read_dref
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:637
mov_current_sample_dec
static void mov_current_sample_dec(MOVStreamContext *sc)
Definition: mov.c:4138
AVSphericalMapping::bound_right
uint32_t bound_right
Distance from the right edge.
Definition: spherical.h:181
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:83
av_bswap32
#define av_bswap32
Definition: bswap.h:47
MOVStreamContext::stsz_sample_size
unsigned int stsz_sample_size
always contains sample size from stsz atom
Definition: isom.h:204
FF_MOV_FLAG_MFRA_AUTO
#define FF_MOV_FLAG_MFRA_AUTO
Definition: isom.h:454
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:424
MOVStreamContext::sgpd_sync
uint8_t * sgpd_sync
Definition: isom.h:244
start_time
static int64_t start_time
Definition: ffplay.c:326
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:5724
MOVStreamContext::stereo3d_size
size_t stereo3d_size
Definition: isom.h:264
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:866
mov_read_iprp
static int mov_read_iprp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9093
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:366
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:56
MOV_TFHD_STSD_ID
#define MOV_TFHD_STSD_ID
Definition: isom.h:397
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:1173
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:3186
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:248
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:498
av_fourcc_make_string
char * av_fourcc_make_string(char *buf, uint32_t fourcc)
Fill the provided buffer with a string containing a FourCC (four-character code) representation.
Definition: utils.c:75
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
av_aes_ctr_set_full_iv
void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the "full" 16-byte iv, including the counter.
Definition: aes_ctr.c:52
AVStreamGroup::iamf_audio_element
struct AVIAMFAudioElement * iamf_audio_element
Definition: avformat.h:1167
MOVStreamContext::coll
AVContentLightMetadata * coll
Definition: isom.h:269
aes_ctr.h
ff_format_io_close
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: avformat.c:959
mov_parse_heif_items
static int mov_parse_heif_items(AVFormatContext *s)
Definition: mov.c:10308
HEIFItem::is_idat_relative
int is_idat_relative
Definition: isom.h:301
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:3988
MOVDref::path
char * path
Definition: isom.h:87
mov_current_sample_inc
static void mov_current_sample_inc(MOVStreamContext *sc)
Definition: mov.c:4126
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:826
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
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:538
mov_read_vexu_proj
static int mov_read_vexu_proj(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6733
fix_timescale
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:5034
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
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:194
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:885
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:356
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:9332
AV_TIMECODE_FLAG_24HOURSMAX
@ AV_TIMECODE_FLAG_24HOURSMAX
timecode wraps after 24 hours
Definition: timecode.h:37
MOV_MP4_FPCM_TAG
#define MOV_MP4_FPCM_TAG
Definition: isom.h:474
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:1023
AVStreamGroupTileGrid::nb_tiles
unsigned int nb_tiles
Amount of tiles in the grid.
Definition: avformat.h:995
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:700
mov_read_packet
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:10876
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
AVStreamGroupLCEVC::lcevc_index
unsigned int lcevc_index
Index of the LCEVC data stream in AVStreamGroup.
Definition: avformat.h:1112
AVStreamGroup::lcevc
struct AVStreamGroupLCEVC * lcevc
Definition: avformat.h:1170
attributes.h
AV_CODEC_ID_LCEVC
@ AV_CODEC_ID_LCEVC
Definition: codec_id.h:601
MOVEncryptionIndex::auxiliary_offsets_count
size_t auxiliary_offsets_count
Definition: isom.h:136
HEIFItem::vflip
int vflip
Definition: isom.h:299
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
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:223
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:348
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
mov_find_reference_track
static AVStream * mov_find_reference_track(AVFormatContext *s, AVStream *st, int first_index)
Definition: mov.c:10370
AVSubsampleEncryptionInfo
This file is part of FFmpeg.
Definition: encryption_info.h:25
MOVFragmentIndexItem::stream_info
MOVFragmentStreamInfo * stream_info
Definition: isom.h:157
version
version
Definition: libkvazaar.c:321
AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
@ AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
Definition: avformat.h:1125
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:1201
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: demux.c:2356
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:475
mov_read_clli
static int mov_read_clli(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6495
MOVStreamContext::chunk_offsets
int64_t * chunk_offsets
Definition: isom.h:181
AVStreamGroup::iamf_mix_presentation
struct AVIAMFMixPresentation * iamf_mix_presentation
Definition: avformat.h:1168
MOVFragmentIndex::item
MOVFragmentIndexItem * item
Definition: isom.h:165
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
MOVStreamContext::iamf
struct IAMFDemuxContext * iamf
Definition: isom.h:285
FFERROR_REDO
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition: demux.h:176
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:232
MOVContext::decryption_key_len
int decryption_key_len
Definition: isom.h:367
av_aes_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:83
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:7914
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:9204
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
flag
#define flag(name)
Definition: cbs_av1.c:474
AVStreamGroupTileGrid::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: avformat.h:1095
mov_metadata_creation_time
static void mov_metadata_creation_time(MOVContext *c, AVIOContext *pb, AVDictionary **metadata, int version)
Definition: mov.c:1835
mov_metadata_hmmt
static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:324
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
MOVFragmentStreamInfo::next_trun_dts
int64_t next_trun_dts
Definition: isom.h:144
MOVStreamContext::stsc_index
unsigned int stsc_index
Definition: isom.h:195
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:7843
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:3266
mov_finalize_packet
static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry *sample, int64_t current_index, AVPacket *pkt)
Definition: mov.c:10812
MOVIndexRange
Definition: isom.h:168
mov_read_seek
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:11190
bprint.h
MOVContext::advanced_editlist
int advanced_editlist
Definition: isom.h:339
MOVStreamContext::time_scale
int time_scale
Definition: isom.h:210
mlp_parse.h
mac_to_unicode
static const uint32_t mac_to_unicode[128]
Definition: mov.c:150
AVStreamGroupTileGrid::width
int width
Width of the final image for presentation.
Definition: avformat.h:1072
MOVStreamContext::bytes_per_frame
unsigned int bytes_per_frame
Definition: isom.h:217
AVSphericalMapping::roll
int32_t roll
Rotation around the forward vector [-180, 180].
Definition: spherical.h:140
IAMFContext::nb_audio_elements
int nb_audio_elements
Definition: iamf.h:132
MOVIndexRange::end
int64_t end
Definition: isom.h:170
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
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:5659
search_frag_timestamp
static int search_frag_timestamp(AVFormatContext *s, MOVFragmentIndex *frag_index, AVStream *st, int64_t timestamp)
Definition: mov.c:1708
HEIFItem::width
int width
Definition: isom.h:295
FLAGS
#define FLAGS
Definition: mov.c:11247
MOVStreamContext::stereo3d
AVStereo3D * stereo3d
Definition: isom.h:263
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:4178
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:7691
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:3121
internal.h
AV_SPHERICAL_FISHEYE
@ AV_SPHERICAL_FISHEYE
Fisheye projection (Apple).
Definition: spherical.h:84
AVCodecParameters::height
int height
Definition: codec_par.h:135
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
MOVStreamContext::stps_count
unsigned int stps_count
Definition: isom.h:197
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
AV_CODEC_ID_TTML
@ AV_CODEC_ID_TTML
Definition: codec_id.h:582
rb_size
static int rb_size(AVIOContext *pb, int64_t *value, int size)
Definition: mov.c:8667
AVStereo3DPrimaryEye
AVStereo3DPrimaryEye
List of possible primary eyes.
Definition: stereo3d.h:174
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:139
display.h
ff_mov_read_stsd_entries
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:3024
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:204
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:108
AVIAMFMixPresentation
Information on how to render and mix one or more AVIAMFAudioElement to generate the final audio outpu...
Definition: iamf.h:613
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:206
MOVContext::frag_index
MOVFragmentIndex frag_index
Definition: isom.h:352
mov_read_vpcc
static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6323
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:357
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:228
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
av_url_split
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:351
MOVStreamContext::dref_id
int dref_id
Definition: isom.h:224
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
fix_frag_index_entries
static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index, int id, int entries)
Definition: mov.c:1794
mov_finalize_stsd_codec
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2898
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:344
mov_read_mdcv
static int mov_read_mdcv(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6414
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
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:1365
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
demux.h
AV_DISPOSITION_DEPENDENT
#define AV_DISPOSITION_DEPENDENT
The stream is intended to be mixed with another stream before presentation.
Definition: avformat.h:709
MOVStreamContext::pb
AVIOContext * pb
Definition: isom.h:174
mov_read_keys
static int mov_read_keys(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5276
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:166
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:185
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
MOVFragment::size
unsigned size
Definition: isom.h:109
MOV_TFHD_DEFAULT_SIZE
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:399
MOVTimeToSample::count
unsigned int count
Definition: isom.h:58
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:659
fix_stream_ids
static void fix_stream_ids(AVFormatContext *s)
Definition: mov.c:10435
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:4624
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:81
mov_read_svq3
static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2372
AVSHA
hash context
Definition: sha.c:35
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:716
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:143
AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
@ AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
Definition: iamf.h:346
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:161
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:91
mov_read_iinf
static int mov_read_iinf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8850
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:860
AVStreamGroupTileGrid::horizontal
int horizontal
Offset in pixels from the left edge of the canvas where the tile should be placed.
Definition: avformat.h:1031
get_stream_info_time
static int64_t get_stream_info_time(MOVFragmentStreamInfo *frag_stream_info)
Definition: mov.c:1658
MP4TrackKindValueMapping
Definition: isom.h:477
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:4029
AV_WB8
#define AV_WB8(p, d)
Definition: intreadwrite.h:392
MOVStreamContext::chunk_count
unsigned int chunk_count
Definition: isom.h:180
MOVStreamContext::data_size
int64_t data_size
Definition: isom.h:235
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:271
mov_read_tmcd
static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6312
mov_chan.h
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:817
MOVStreamContext::ambient_size
size_t ambient_size
Definition: isom.h:272
tag
uint32_t tag
Definition: movenc.c:1879
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:760
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:203
HEIFItem::extent_length
int64_t extent_length
Definition: isom.h:293
MOVEncryptionIndex::nb_encrypted_samples
unsigned int nb_encrypted_samples
Definition: isom.h:129
mov_read_senc
static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7394
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
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:80
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:187
AVSphericalMapping::pitch
int32_t pitch
Rotation around the right vector [-90, 90].
Definition: spherical.h:139
AVStreamGroup::metadata
AVDictionary * metadata
Metadata that applies to the whole group.
Definition: avformat.h:1181
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:746
AVStereo3D::type
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:207
MOVSbgp::index
unsigned int index
Definition: isom.h:123
HEIFItem::icc_profile_size
size_t icc_profile_size
Definition: isom.h:303
MOVContext::chapter_tracks
int * chapter_tracks
Definition: isom.h:335
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:594
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
MOVFragment::implicit_offset
uint64_t implicit_offset
Definition: isom.h:106
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:707
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:233
mov_read_dmlp
static int mov_read_dmlp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:8358
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:220
MOVContext::time_scale
int time_scale
Definition: isom.h:317
id
enum AVCodecID id
Definition: dts2pts.c:367
mov_read_tfdt
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:5685
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:5403
search_frag_moof_offset
static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
Definition: mov.c:1634
MOVFragment
Definition: isom.h:101
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:10731
MOVContext::use_mfra_for
int use_mfra_for
Definition: isom.h:349
AVEncryptionInfo
This describes encryption info for a packet.
Definition: encryption_info.h:43
MOVStreamContext::encryption_index
MOVEncryptionIndex * encryption_index
Definition: isom.h:282
MIN_DATA_ENTRY_BOX_SIZE
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:636
AVStreamGroup
Definition: avformat.h:1134
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:754
avpriv_dv_init_demux
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:726
mov_seek_fragment
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
Definition: mov.c:11039
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:2661
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:230
mov_read_dec3
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1077
get_frag_time
static int64_t get_frag_time(AVFormatContext *s, AVStream *dst_st, MOVFragmentIndex *frag_index, int index)
Definition: mov.c:1668
MOVStreamContext::sample_size
unsigned int sample_size
may contain value calculated from stsd or value from stsz atom
Definition: isom.h:203
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:1188
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:253
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:3785
MOVFragment::moof_offset
uint64_t moof_offset
Definition: isom.h:105
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:232
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:2434
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:7052
MOVTrackExt::size
unsigned size
Definition: isom.h:117
ff_remove_stream_group
void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg)
Remove a stream group from its AVFormatContext and free it.
Definition: avformat.c:129
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
MOVContext::dv_fctx
AVFormatContext * dv_fctx
Definition: isom.h:328
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:442
AV_CODEC_ID_DVAUDIO
@ AV_CODEC_ID_DVAUDIO
Definition: codec_id.h:454
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:149
MOVContext::aax_mode
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:354
mov_read_sv3d
static int mov_read_sv3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6596
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
mov_aaxc_crypto
static int mov_aaxc_crypto(MOVContext *c)
Definition: mov.c:1474
mov_get_skip_samples
static int64_t mov_get_skip_samples(AVStream *st, int sample)
Definition: mov.c:11173
MOVFragmentIndex
Definition: isom.h:160
mov_read_infe
static int mov_read_infe(MOVContext *c, AVIOContext *pb, MOVAtom atom, int idx)
Definition: mov.c:8785
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:914
MOVStreamContext::tts_count
unsigned int tts_count
Definition: isom.h:182
MOVStreamContext::track_end
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:238
MOVStreamContext::sgpd_sync_count
uint32_t sgpd_sync_count
Definition: isom.h:245
MOVContext::fragment
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:330
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:603
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:111
AVStreamGroupLCEVC::width
int width
Width of the final stream for presentation.
Definition: avformat.h:1116
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:866
mov_read_covr
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:231
MOVParseTableEntry::type
uint32_t type
Definition: mov.c:83
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:280
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:541
MOVFragmentIndexItem::nb_stream_info
int nb_stream_info
Definition: isom.h:156
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
export_orphan_timecode
static void export_orphan_timecode(AVFormatContext *s)
Definition: mov.c:9878
mov_read_sbas
static int mov_read_sbas(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2505
MOVStreamContext::has_sidx
int has_sidx
Definition: isom.h:276
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:107
mov_metadata_gnre
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:134
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
MOV_MERGE_CTTS
#define MOV_MERGE_CTTS
Definition: mov.c:4554
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:188
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:167
mov_read_dpxe
static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2259
AVIAMFSubmix::nb_elements
unsigned int nb_elements
Number of elements in the submix.
Definition: iamf.h:572
MOVFragmentStreamInfo::id
int id
Definition: isom.h:140
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:617
AV_PKT_DATA_AUDIO_SERVICE_TYPE
@ AV_PKT_DATA_AUDIO_SERVICE_TYPE
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: packet.h:117
av_spherical_alloc
AVSphericalMapping * av_spherical_alloc(size_t *size)
Allocate a AVSphericalVideo structure and initialize its fields to default values.
Definition: spherical.c:26
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition: opt.h:356
MOVContext::thmb_item_id
int thmb_item_id
Definition: isom.h:379
mov_read_rtmd_track
static int mov_read_rtmd_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:9661
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
MOVStreamContext::pb_is_copied
int pb_is_copied
Definition: isom.h:176
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:343
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mov_parse_tiles
static int mov_parse_tiles(AVFormatContext *s)
Definition: mov.c:10213
MOVStreamContext::cenc
struct MOVStreamContext::@399 cenc
mem.h
AVStreamGroup::type
enum AVStreamGroupParamsType type
Group type.
Definition: avformat.h:1161
MOVElst::time
int64_t time
Definition: isom.h:81
HEIFGrid
Definition: isom.h:306
mov_read_iref_dimg
static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version)
Definition: mov.c:8910
mov_read_pcmc
static int mov_read_pcmc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1988
build_open_gop_key_points
static int build_open_gop_key_points(AVStream *st)
Definition: mov.c:4491
mov_parse_stsd_audio
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2719
IAMFContext::mix_presentations
IAMFMixPresentation ** mix_presentations
Definition: iamf.h:133
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:340
MOVContext::trak_index
int trak_index
Index of the current 'trak'.
Definition: isom.h:324
mov_read_timecode_track
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:9687
HEIFGrid::item
HEIFItem * item
Definition: isom.h:307
AVSphericalMapping::bound_left
uint32_t bound_left
Distance from the left edge.
Definition: spherical.h:179
mov_read_mac_string
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:169
MOVEncryptionIndex::auxiliary_info_sizes
uint8_t * auxiliary_info_sizes
Definition: isom.h:132
MOVFragment::stsd_id
unsigned stsd_id
Definition: isom.h:107
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:175
HEIFGrid::tile_id_list
int16_t * tile_id_list
Definition: isom.h:309
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
MOVStreamContext::tts_data
MOVTimeToSample * tts_data
Definition: isom.h:184
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:449
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:358
AVStreamGroupTileGrid::height
int height
Height of the final image for presentation.
Definition: avformat.h:1082
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:9896
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:89
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:55
AVStereo3DView
AVStereo3DView
List of possible view types.
Definition: stereo3d.h:149
AVPacket
This structure stores compressed data.
Definition: packet.h:516
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:10385
mov_read_hfov
static int mov_read_hfov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:7023
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
MOVStreamContext::stps_data
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:198
AV_CODEC_ID_ADPCM_IMA_WAV
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition: codec_id.h:375
MOVContext::nb_heif_item
int nb_heif_item
Definition: isom.h:376
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:88
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:237
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:559
FFInputFormat
Definition: demux.h:42
mov_metadata_loci
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:274
mov_read_eyes
static int mov_read_eyes(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6795
AV_CODEC_ID_ILBC
@ AV_CODEC_ID_ILBC
Definition: codec_id.h:507
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:407
MOVStreamContext::tmcd_flags
uint32_t tmcd_flags
tmcd track flags
Definition: isom.h:236
int32_t
int32_t
Definition: audioconvert.c:56
HEIFItem::rotation
int rotation
Definition: isom.h:297
MOVAtom::type
uint32_t type
Definition: isom.h:95
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:593
AVChannelLayout::u
union AVChannelLayout::@434 u
Details about which channels are present in this layout.
parse_timecode_in_framenum_format
static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, int64_t value, int flags)
Definition: mov.c:9647
MOVStreamContext::tmcd_nb_frames
uint8_t tmcd_nb_frames
tmcd number of frames per tick / second
Definition: isom.h:237
AVIAMFSubmixElement
Submix element as defined in section 3.7 of IAMF.
Definition: iamf.h:446
replaygain.h
MOVFragmentIndexItem::headers_read
int headers_read
Definition: isom.h:154
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
MOVStreamContext::start_pad
int start_pad
amount of samples to skip due to enc-dec delay
Definition: isom.h:239
MP4TrackKindValueMapping::value
const char * value
Definition: isom.h:479
AVStereo3DType
AVStereo3DType
List of possible 3D Types.
Definition: stereo3d.h:48
MOVDref::filename
char filename[64]
Definition: isom.h:90
ff_mov_read_chnl
int ff_mov_read_chnl(AVFormatContext *s, AVIOContext *pb, AVStream *st)
Read 'chnl' tag from the input stream.
Definition: mov_chan.c:729
MOVStsc::count
int count
Definition: isom.h:75
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:356
ff_mov_demuxer
const FFInputFormat ff_mov_demuxer
Definition: mov.c:11308
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
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:262
MOVContext::heif_grid
HEIFGrid * heif_grid
Definition: isom.h:377
MOVStreamContext::min_sample_duration
uint32_t min_sample_duration
Definition: isom.h:250
MOVStreamContext::current_index
int64_t current_index
Definition: isom.h:214
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_mov_track_kind_table
const struct MP4TrackKindMapping ff_mov_track_kind_table[]
Definition: isom.c:451
get_curr_st
static AVStream * get_curr_st(MOVContext *c)
Get the current stream in the parsing process.
Definition: mov.c:213
MOVStreamContext::stts_allocated_size
unsigned int stts_allocated_size
Definition: isom.h:186
MOVFragmentStreamInfo::index_entry
int index_entry
Definition: isom.h:147
cenc_decrypt
static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:8198
MOVStreamContext::format
uint32_t format
Definition: isom.h:274
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:662
MOVStreamContext::sync_group_count
unsigned int sync_group_count
Definition: isom.h:242
MOVContext::bitrates_count
int bitrates_count
Definition: isom.h:347
AV_CODEC_ID_VORBIS
@ AV_CODEC_ID_VORBIS
Definition: codec_id.h:453
AVDictionaryEntry::value
char * value
Definition: dict.h:91
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:797
MOVStreamContext::id
int id
AVStream id.
Definition: isom.h:177
MOVStreamContext::samples_per_frame
unsigned int samples_per_frame
Definition: isom.h:218
MOVStreamContext::tts_allocated_size
unsigned int tts_allocated_size
Definition: isom.h:183
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:9032
width
#define width
Definition: dsp.h:85
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:8994
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:1239
mov_read_mfra
static int mov_read_mfra(MOVContext *c, AVIOContext *f)
Definition: mov.c:9951
AV_CODEC_ID_FLV1
@ AV_CODEC_ID_FLV1
Definition: codec_id.h:73
mov_metadata_int8_no_padding
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:125
flac.h
AVStreamGroupTileGrid::idx
unsigned int idx
Index of the stream in the group this tile references.
Definition: avformat.h:1026
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:1579
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:8462
MOVContext::heif_item
HEIFItem ** heif_item
Definition: isom.h:375
MOVStreamContext::stts_count
unsigned int stts_count
Definition: isom.h:185
MOV_TRUN_SAMPLE_CTS
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:409
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
MOV_ISMV_TTML_TAG
#define MOV_ISMV_TTML_TAG
Definition: isom.h:472
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
Audio only.
Definition: codec_par.h:203
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:348
get_eia608_packet
static int get_eia608_packet(AVIOContext *pb, AVPacket *pkt, int size)
Definition: mov.c:10791
mov_read_st3d
static int mov_read_st3d(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:6549
mov_read_imir
static int mov_read_imir(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:9074
is_open_key_sample
static int is_open_key_sample(const MOVStreamContext *sc, int sample)
Definition: mov.c:11058
mov_read_dvc1
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2480
MOVStreamContext::elst_count
unsigned int elst_count
Definition: isom.h:200
av_color_transfer_name
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:3603
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:278
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:2196
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:138
src
#define src
Definition: vp8dsp.c:248
mov_read_chapters
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:9548
AV_CODEC_ID_DNXHD
@ AV_CODEC_ID_DNXHD
Definition: codec_id.h:151
channel
channel
Definition: ebur128.h:39
MOVStreamContext::default_encrypted_sample
AVEncryptionInfo * default_encrypted_sample
Definition: isom.h:281
MOVContext::cur_item_id
int cur_item_id
Definition: isom.h:374
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:103
AVFMT_EVENT_FLAG_METADATA_UPDATED
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1677
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:343
MOVContext::found_iinf
int found_iinf
'iinf' atom has been found
Definition: isom.h:321
MOVContext::meta_keys_count
unsigned meta_keys_count
Definition: isom.h:326
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
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:1046
MOVStreamContext::palette
uint32_t palette[256]
Definition: isom.h:233
cenc_scheme_decrypt
static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
Definition: mov.c:7954
MOVFragment::track_id
unsigned track_id
Definition: isom.h:103
mov_read_hdlr
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:781
mov_parse_stsd_data
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:2852
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:342
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_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155
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:348
ff_mov_read_chan
int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size)
Read 'chan' tag from the input stream.
Definition: mov_chan.c:524
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:8686
MOVContext::ignore_chapters
int ignore_chapters
Definition: isom.h:341
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:852
MP4TrackKindMapping
Definition: isom.h:482
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:227
AV_DISPOSITION_NON_DIEGETIC
#define AV_DISPOSITION_NON_DIEGETIC
The stream is intended to be mixed with a spatial audio track.
Definition: avformat.h:686
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346
mc
#define mc
Definition: vf_colormatrix.c:100
MOVStreamContext::ffindex
int ffindex
AVStream index.
Definition: isom.h:178
HEIFItem::extent_offset
int64_t extent_offset
Definition: isom.h:294
mov_read_stsz
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3383