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