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