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