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