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