FFmpeg
mov.c
Go to the documentation of this file.
1 /*
2  * MOV demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5  *
6  * first version by Francois Revol <revol@free.fr>
7  * seek function by Gael Chardon <gael.dev@4now.net>
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <stdint.h>
29 
30 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/intfloat.h"
35 #include "libavutil/mathematics.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/display.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/aes.h"
43 #include "libavutil/aes_ctr.h"
44 #include "libavutil/pixdesc.h"
45 #include "libavutil/sha.h"
46 #include "libavutil/spherical.h"
47 #include "libavutil/stereo3d.h"
48 #include "libavutil/timecode.h"
49 #include "libavutil/dovi_meta.h"
50 #include "libavcodec/ac3tab.h"
51 #include "libavcodec/flac.h"
53 #include "libavcodec/mlp_parse.h"
54 #include "avformat.h"
55 #include "internal.h"
56 #include "avio_internal.h"
57 #include "riff.h"
58 #include "isom.h"
59 #include "libavcodec/get_bits.h"
60 #include "id3v1.h"
61 #include "mov_chan.h"
62 #include "replaygain.h"
63 
64 #if CONFIG_ZLIB
65 #include <zlib.h>
66 #endif
67 
68 #include "qtpalette.h"
69 
70 /* those functions parse an atom */
71 /* links atom IDs to parse functions */
72 typedef struct MOVParseTableEntry {
73  uint32_t type;
76 
77 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
78 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
79 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
80  int count, int duration);
81 
83  unsigned len, const char *key)
84 {
85  char buf[16];
86 
87  short current, total = 0;
88  avio_rb16(pb); // unknown
89  current = avio_rb16(pb);
90  if (len >= 6)
91  total = avio_rb16(pb);
92  if (!total)
93  snprintf(buf, sizeof(buf), "%d", current);
94  else
95  snprintf(buf, sizeof(buf), "%d/%d", current, total);
96  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
97  av_dict_set(&c->fc->metadata, key, buf, 0);
98 
99  return 0;
100 }
101 
103  unsigned len, const char *key)
104 {
105  /* bypass padding bytes */
106  avio_r8(pb);
107  avio_r8(pb);
108  avio_r8(pb);
109 
110  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
111  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
112 
113  return 0;
114 }
115 
117  unsigned len, const char *key)
118 {
119  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
120  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
121 
122  return 0;
123 }
124 
126  unsigned len, const char *key)
127 {
128  short genre;
129 
130  avio_r8(pb); // unknown
131 
132  genre = avio_r8(pb);
133  if (genre < 1 || genre > ID3v1_GENRE_MAX)
134  return 0;
135  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
136  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
137 
138  return 0;
139 }
140 
141 static const uint32_t mac_to_unicode[128] = {
142  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
143  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
144  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
145  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
146  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
147  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
148  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
149  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
150  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
151  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
152  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
153  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
154  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
155  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
156  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
157  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
158 };
159 
161  char *dst, int dstlen)
162 {
163  char *p = dst;
164  char *end = dst+dstlen-1;
165  int i;
166 
167  for (i = 0; i < len; i++) {
168  uint8_t t, c = avio_r8(pb);
169 
170  if (p >= end)
171  continue;
172 
173  if (c < 0x80)
174  *p++ = c;
175  else if (p < end)
176  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
177  }
178  *p = 0;
179  return p - dst;
180 }
181 
182 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
183 {
184  AVStream *st;
185  MOVStreamContext *sc;
186  enum AVCodecID id;
187  int ret;
188 
189  switch (type) {
190  case 0xd: id = AV_CODEC_ID_MJPEG; break;
191  case 0xe: id = AV_CODEC_ID_PNG; break;
192  case 0x1b: id = AV_CODEC_ID_BMP; break;
193  default:
194  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
195  avio_skip(pb, len);
196  return 0;
197  }
198 
199  st = avformat_new_stream(c->fc, NULL);
200  if (!st)
201  return AVERROR(ENOMEM);
202  sc = av_mallocz(sizeof(*sc));
203  if (!sc)
204  return AVERROR(ENOMEM);
205  st->priv_data = sc;
206 
207  ret = av_get_packet(pb, &st->attached_pic, len);
208  if (ret < 0)
209  return ret;
210 
211  if (st->attached_pic.size >= 8 && id != AV_CODEC_ID_BMP) {
212  if (AV_RB64(st->attached_pic.data) == 0x89504e470d0a1a0a) {
213  id = AV_CODEC_ID_PNG;
214  } else {
215  id = AV_CODEC_ID_MJPEG;
216  }
217  }
218 
220 
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  }
277  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
278  return av_dict_set(&c->fc->metadata, key, buf, 0);
279 }
280 
281 static int mov_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
282 {
283  int i, n_hmmt;
284 
285  if (len < 2)
286  return 0;
287  if (c->ignore_chapters)
288  return 0;
289 
290  n_hmmt = avio_rb32(pb);
291  if (n_hmmt > len / 4)
292  return AVERROR_INVALIDDATA;
293  for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
294  int moment_time = avio_rb32(pb);
295  avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
296  }
297  if (avio_feof(pb))
298  return AVERROR_INVALIDDATA;
299  return 0;
300 }
301 
303 {
304  char tmp_key[AV_FOURCC_MAX_STRING_SIZE] = {0};
305  char key2[32], language[4] = {0};
306  char *str = NULL;
307  const char *key = NULL;
308  uint16_t langcode = 0;
309  uint32_t data_type = 0, str_size, str_size_alloc;
310  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
311  int raw = 0;
312  int num = 0;
313 
314  switch (atom.type) {
315  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
316  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
317  case MKTAG( 'X','M','P','_'):
318  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
319  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
320  case MKTAG( 'a','k','I','D'): key = "account_type";
322  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
323  case MKTAG( 'c','a','t','g'): key = "category"; break;
324  case MKTAG( 'c','p','i','l'): key = "compilation";
326  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
327  case MKTAG( 'd','e','s','c'): key = "description"; break;
328  case MKTAG( 'd','i','s','k'): key = "disc";
330  case MKTAG( 'e','g','i','d'): key = "episode_uid";
332  case MKTAG( 'F','I','R','M'): key = "firmware"; raw = 1; break;
333  case MKTAG( 'g','n','r','e'): key = "genre";
334  parse = mov_metadata_gnre; break;
335  case MKTAG( 'h','d','v','d'): key = "hd_video";
337  case MKTAG( 'H','M','M','T'):
338  return mov_metadata_hmmt(c, pb, atom.size);
339  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
340  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
341  case MKTAG( 'l','o','c','i'):
342  return mov_metadata_loci(c, pb, atom.size);
343  case MKTAG( 'm','a','n','u'): key = "make"; break;
344  case MKTAG( 'm','o','d','l'): key = "model"; break;
345  case MKTAG( 'p','c','s','t'): key = "podcast";
347  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
349  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
350  case MKTAG( 'r','t','n','g'): key = "rating";
352  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
353  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
354  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
355  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
356  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
357  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
358  case MKTAG( 's','t','i','k'): key = "media_type";
360  case MKTAG( 't','r','k','n'): key = "track";
362  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
363  case MKTAG( 't','v','e','s'): key = "episode_sort";
365  case MKTAG( 't','v','n','n'): key = "network"; break;
366  case MKTAG( 't','v','s','h'): key = "show"; break;
367  case MKTAG( 't','v','s','n'): key = "season_number";
369  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
370  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
371  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
372  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
373  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
374  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
375  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
376  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
377  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
378  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
379  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
380  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
381  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
382  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
383  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
384  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
385  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
386  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
387  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
388  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
389  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
390  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
391  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
392  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
393  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
394  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
395  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
396  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
397  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
398  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
399  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
400  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
401  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
402  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
403  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
404  }
405 retry:
406  if (c->itunes_metadata && atom.size > 8) {
407  int data_size = avio_rb32(pb);
408  int tag = avio_rl32(pb);
409  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size && data_size >= 16) {
410  data_type = avio_rb32(pb); // type
411  avio_rb32(pb); // unknown
412  str_size = data_size - 16;
413  atom.size -= 16;
414 
415  if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
416  int ret = mov_read_covr(c, pb, data_type, str_size);
417  if (ret < 0) {
418  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
419  return ret;
420  }
421  atom.size -= str_size;
422  if (atom.size > 8)
423  goto retry;
424  return ret;
425  } else if (!key && c->found_hdlr_mdta && c->meta_keys) {
426  uint32_t index = AV_RB32(&atom.type);
427  if (index < c->meta_keys_count && index > 0) {
428  key = c->meta_keys[index];
429  } else {
430  av_log(c->fc, AV_LOG_WARNING,
431  "The index of 'data' is out of range: %"PRId32" < 1 or >= %d.\n",
432  index, c->meta_keys_count);
433  }
434  }
435  } else return 0;
436  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
437  str_size = avio_rb16(pb); // string length
438  if (str_size > atom.size) {
439  raw = 1;
440  avio_seek(pb, -2, SEEK_CUR);
441  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
442  goto retry;
443  }
444  langcode = avio_rb16(pb);
445  ff_mov_lang_to_iso639(langcode, language);
446  atom.size -= 4;
447  } else
448  str_size = atom.size;
449 
450  if (c->export_all && !key) {
451  key = av_fourcc_make_string(tmp_key, atom.type);
452  }
453 
454  if (!key)
455  return 0;
456  if (atom.size < 0 || str_size >= INT_MAX/2)
457  return AVERROR_INVALIDDATA;
458 
459  // Allocates enough space if data_type is a int32 or float32 number, otherwise
460  // worst-case requirement for output string in case of utf8 coded input
461  num = (data_type >= 21 && data_type <= 23);
462  str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1;
463  str = av_mallocz(str_size_alloc);
464  if (!str)
465  return AVERROR(ENOMEM);
466 
467  if (parse)
468  parse(c, pb, str_size, key);
469  else {
470  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
471  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
472  } else if (data_type == 21) { // BE signed integer, variable size
473  int val = 0;
474  if (str_size == 1)
475  val = (int8_t)avio_r8(pb);
476  else if (str_size == 2)
477  val = (int16_t)avio_rb16(pb);
478  else if (str_size == 3)
479  val = ((int32_t)(avio_rb24(pb)<<8))>>8;
480  else if (str_size == 4)
481  val = (int32_t)avio_rb32(pb);
482  if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
483  av_log(c->fc, AV_LOG_ERROR,
484  "Failed to store the number (%d) in string.\n", val);
485  av_free(str);
486  return AVERROR_INVALIDDATA;
487  }
488  } else if (data_type == 22) { // BE unsigned integer, variable size
489  unsigned int val = 0;
490  if (str_size == 1)
491  val = avio_r8(pb);
492  else if (str_size == 2)
493  val = avio_rb16(pb);
494  else if (str_size == 3)
495  val = avio_rb24(pb);
496  else if (str_size == 4)
497  val = avio_rb32(pb);
498  if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
499  av_log(c->fc, AV_LOG_ERROR,
500  "Failed to store the number (%u) in string.\n", val);
501  av_free(str);
502  return AVERROR_INVALIDDATA;
503  }
504  } else if (data_type == 23 && str_size >= 4) { // BE float32
505  float val = av_int2float(avio_rb32(pb));
506  if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) {
507  av_log(c->fc, AV_LOG_ERROR,
508  "Failed to store the float32 number (%f) in string.\n", val);
509  av_free(str);
510  return AVERROR_INVALIDDATA;
511  }
512  } else {
513  int ret = ffio_read_size(pb, str, str_size);
514  if (ret < 0) {
515  av_free(str);
516  return ret;
517  }
518  str[str_size] = 0;
519  }
520  c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
521  av_dict_set(&c->fc->metadata, key, str, 0);
522  if (*language && strcmp(language, "und")) {
523  snprintf(key2, sizeof(key2), "%s-%s", key, language);
524  av_dict_set(&c->fc->metadata, key2, str, 0);
525  }
526  if (!strcmp(key, "encoder")) {
527  int major, minor, micro;
528  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
529  c->handbrake_version = 1000000*major + 1000*minor + micro;
530  }
531  }
532  }
533 
534  av_freep(&str);
535  return 0;
536 }
537 
539 {
540  int64_t start;
541  int i, nb_chapters, str_len, version;
542  char str[256+1];
543  int ret;
544 
545  if (c->ignore_chapters)
546  return 0;
547 
548  if ((atom.size -= 5) < 0)
549  return 0;
550 
551  version = avio_r8(pb);
552  avio_rb24(pb);
553  if (version)
554  avio_rb32(pb); // ???
555  nb_chapters = avio_r8(pb);
556 
557  for (i = 0; i < nb_chapters; i++) {
558  if (atom.size < 9)
559  return 0;
560 
561  start = avio_rb64(pb);
562  str_len = avio_r8(pb);
563 
564  if ((atom.size -= 9+str_len) < 0)
565  return 0;
566 
567  ret = ffio_read_size(pb, str, str_len);
568  if (ret < 0)
569  return ret;
570  str[str_len] = 0;
571  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
572  }
573  return 0;
574 }
575 
576 #define MIN_DATA_ENTRY_BOX_SIZE 12
578 {
579  AVStream *st;
580  MOVStreamContext *sc;
581  int entries, i, j;
582 
583  if (c->fc->nb_streams < 1)
584  return 0;
585  st = c->fc->streams[c->fc->nb_streams-1];
586  sc = st->priv_data;
587 
588  avio_rb32(pb); // version + flags
589  entries = avio_rb32(pb);
590  if (!entries ||
591  entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
592  entries >= UINT_MAX / sizeof(*sc->drefs))
593  return AVERROR_INVALIDDATA;
594 
595  for (i = 0; i < sc->drefs_count; i++) {
596  MOVDref *dref = &sc->drefs[i];
597  av_freep(&dref->path);
598  av_freep(&dref->dir);
599  }
600  av_free(sc->drefs);
601  sc->drefs_count = 0;
602  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
603  if (!sc->drefs)
604  return AVERROR(ENOMEM);
605  sc->drefs_count = entries;
606 
607  for (i = 0; i < entries; i++) {
608  MOVDref *dref = &sc->drefs[i];
609  uint32_t size = avio_rb32(pb);
610  int64_t next = avio_tell(pb);
611 
612  if (size < 12 || next < 0 || next > INT64_MAX - size)
613  return AVERROR_INVALIDDATA;
614 
615  next += size - 4;
616 
617  dref->type = avio_rl32(pb);
618  avio_rb32(pb); // version + flags
619 
620  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
621  /* macintosh alias record */
622  uint16_t volume_len, len;
623  int16_t type;
624  int ret;
625 
626  avio_skip(pb, 10);
627 
628  volume_len = avio_r8(pb);
629  volume_len = FFMIN(volume_len, 27);
630  ret = ffio_read_size(pb, dref->volume, 27);
631  if (ret < 0)
632  return ret;
633  dref->volume[volume_len] = 0;
634  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
635 
636  avio_skip(pb, 12);
637 
638  len = avio_r8(pb);
639  len = FFMIN(len, 63);
640  ret = ffio_read_size(pb, dref->filename, 63);
641  if (ret < 0)
642  return ret;
643  dref->filename[len] = 0;
644  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
645 
646  avio_skip(pb, 16);
647 
648  /* read next level up_from_alias/down_to_target */
649  dref->nlvl_from = avio_rb16(pb);
650  dref->nlvl_to = avio_rb16(pb);
651  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
652  dref->nlvl_from, dref->nlvl_to);
653 
654  avio_skip(pb, 16);
655 
656  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
657  if (avio_feof(pb))
658  return AVERROR_EOF;
659  type = avio_rb16(pb);
660  len = avio_rb16(pb);
661  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
662  if (len&1)
663  len += 1;
664  if (type == 2) { // absolute path
665  av_free(dref->path);
666  dref->path = av_mallocz(len+1);
667  if (!dref->path)
668  return AVERROR(ENOMEM);
669 
670  ret = ffio_read_size(pb, dref->path, len);
671  if (ret < 0) {
672  av_freep(&dref->path);
673  return ret;
674  }
675  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
676  len -= volume_len;
677  memmove(dref->path, dref->path+volume_len, len);
678  dref->path[len] = 0;
679  }
680  // trim string of any ending zeros
681  for (j = len - 1; j >= 0; j--) {
682  if (dref->path[j] == 0)
683  len--;
684  else
685  break;
686  }
687  for (j = 0; j < len; j++)
688  if (dref->path[j] == ':' || dref->path[j] == 0)
689  dref->path[j] = '/';
690  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
691  } else if (type == 0) { // directory name
692  av_free(dref->dir);
693  dref->dir = av_malloc(len+1);
694  if (!dref->dir)
695  return AVERROR(ENOMEM);
696 
697  ret = ffio_read_size(pb, dref->dir, len);
698  if (ret < 0) {
699  av_freep(&dref->dir);
700  return ret;
701  }
702  dref->dir[len] = 0;
703  for (j = 0; j < len; j++)
704  if (dref->dir[j] == ':')
705  dref->dir[j] = '/';
706  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
707  } else
708  avio_skip(pb, len);
709  }
710  } else {
711  av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x%08"PRIx32" size %"PRIu32"\n",
712  dref->type, size);
713  entries--;
714  i--;
715  }
716  avio_seek(pb, next, SEEK_SET);
717  }
718  return 0;
719 }
720 
722 {
723  AVStream *st;
724  uint32_t type;
725  uint32_t ctype;
726  int64_t title_size;
727  char *title_str;
728  int ret;
729 
730  avio_r8(pb); /* version */
731  avio_rb24(pb); /* flags */
732 
733  /* component type */
734  ctype = avio_rl32(pb);
735  type = avio_rl32(pb); /* component subtype */
736 
737  av_log(c->fc, AV_LOG_TRACE, "ctype=%s\n", av_fourcc2str(ctype));
738  av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(type));
739 
740  if (c->trak_index < 0) { // meta not inside a trak
741  if (type == MKTAG('m','d','t','a')) {
742  c->found_hdlr_mdta = 1;
743  }
744  return 0;
745  }
746 
747  st = c->fc->streams[c->fc->nb_streams-1];
748 
749  if (type == MKTAG('v','i','d','e'))
751  else if (type == MKTAG('s','o','u','n'))
753  else if (type == MKTAG('m','1','a',' '))
755  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
757 
758  avio_rb32(pb); /* component manufacture */
759  avio_rb32(pb); /* component flags */
760  avio_rb32(pb); /* component flags mask */
761 
762  title_size = atom.size - 24;
763  if (title_size > 0) {
764  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
765  return AVERROR_INVALIDDATA;
766  title_str = av_malloc(title_size + 1); /* Add null terminator */
767  if (!title_str)
768  return AVERROR(ENOMEM);
769 
770  ret = ffio_read_size(pb, title_str, title_size);
771  if (ret < 0) {
772  av_freep(&title_str);
773  return ret;
774  }
775  title_str[title_size] = 0;
776  if (title_str[0]) {
777  int off = (!c->isom && title_str[0] == title_size - 1);
778  // flag added so as to not set stream handler name if already set from mdia->hdlr
779  av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
780  }
781  av_freep(&title_str);
782  }
783 
784  return 0;
785 }
786 
788 {
789  return ff_mov_read_esds(c->fc, pb);
790 }
791 
793 {
794  AVStream *st;
795  enum AVAudioServiceType *ast;
796  int ac3info, acmod, lfeon, bsmod;
797 
798  if (c->fc->nb_streams < 1)
799  return 0;
800  st = c->fc->streams[c->fc->nb_streams-1];
801 
803  sizeof(*ast));
804  if (!ast)
805  return AVERROR(ENOMEM);
806 
807  ac3info = avio_rb24(pb);
808  bsmod = (ac3info >> 14) & 0x7;
809  acmod = (ac3info >> 11) & 0x7;
810  lfeon = (ac3info >> 10) & 0x1;
811  st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
813  if (lfeon)
815  *ast = bsmod;
816  if (st->codecpar->channels > 1 && bsmod == 0x7)
818 
819 #if FF_API_LAVF_AVCTX
821  st->codec->audio_service_type = *ast;
823 #endif
824 
825  return 0;
826 }
827 
829 {
830  AVStream *st;
831  enum AVAudioServiceType *ast;
832  int eac3info, acmod, lfeon, bsmod;
833 
834  if (c->fc->nb_streams < 1)
835  return 0;
836  st = c->fc->streams[c->fc->nb_streams-1];
837 
839  sizeof(*ast));
840  if (!ast)
841  return AVERROR(ENOMEM);
842 
843  /* No need to parse fields for additional independent substreams and its
844  * associated dependent substreams since libavcodec's E-AC-3 decoder
845  * does not support them yet. */
846  avio_rb16(pb); /* data_rate and num_ind_sub */
847  eac3info = avio_rb24(pb);
848  bsmod = (eac3info >> 12) & 0x1f;
849  acmod = (eac3info >> 9) & 0x7;
850  lfeon = (eac3info >> 8) & 0x1;
852  if (lfeon)
855  *ast = bsmod;
856  if (st->codecpar->channels > 1 && bsmod == 0x7)
858 
859 #if FF_API_LAVF_AVCTX
861  st->codec->audio_service_type = *ast;
863 #endif
864 
865  return 0;
866 }
867 
869 {
870 #define DDTS_SIZE 20
872  AVStream *st = NULL;
873  uint32_t frame_duration_code = 0;
874  uint32_t channel_layout_code = 0;
875  GetBitContext gb;
876  int ret;
877 
878  if ((ret = ffio_read_size(pb, buf, DDTS_SIZE)) < 0)
879  return ret;
880 
881  init_get_bits(&gb, buf, 8 * DDTS_SIZE);
882 
883  if (c->fc->nb_streams < 1) {
884  return 0;
885  }
886  st = c->fc->streams[c->fc->nb_streams-1];
887 
888  st->codecpar->sample_rate = get_bits_long(&gb, 32);
889  if (st->codecpar->sample_rate <= 0) {
890  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
891  return AVERROR_INVALIDDATA;
892  }
893  skip_bits_long(&gb, 32); /* max bitrate */
894  st->codecpar->bit_rate = get_bits_long(&gb, 32);
895  st->codecpar->bits_per_coded_sample = get_bits(&gb, 8);
896  frame_duration_code = get_bits(&gb, 2);
897  skip_bits(&gb, 30); /* various fields */
898  channel_layout_code = get_bits(&gb, 16);
899 
900  st->codecpar->frame_size =
901  (frame_duration_code == 0) ? 512 :
902  (frame_duration_code == 1) ? 1024 :
903  (frame_duration_code == 2) ? 2048 :
904  (frame_duration_code == 3) ? 4096 : 0;
905 
906  if (channel_layout_code > 0xff) {
907  av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout\n");
908  }
909  st->codecpar->channel_layout =
910  ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) |
911  ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) |
912  ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) |
913  ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) |
914  ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) |
915  ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0);
916 
918 
919  return 0;
920 }
921 
923 {
924  AVStream *st;
925 
926  if (c->fc->nb_streams < 1)
927  return 0;
928  st = c->fc->streams[c->fc->nb_streams-1];
929 
930  if (atom.size < 16)
931  return 0;
932 
933  /* skip version and flags */
934  avio_skip(pb, 4);
935 
936  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
937 
938  return 0;
939 }
940 
942 {
943  AVStream *st;
944  int ret;
945 
946  if (c->fc->nb_streams < 1)
947  return 0;
948  st = c->fc->streams[c->fc->nb_streams-1];
949 
950  if ((ret = ff_get_wav_header(c->fc, pb, st->codecpar, atom.size, 0)) < 0)
951  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
952 
953  return ret;
954 }
955 
957 {
958  const int num = avio_rb32(pb);
959  const int den = avio_rb32(pb);
960  AVStream *st;
961 
962  if (c->fc->nb_streams < 1)
963  return 0;
964  st = c->fc->streams[c->fc->nb_streams-1];
965 
966  if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
967  (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
968  av_log(c->fc, AV_LOG_WARNING,
969  "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
971  num, den);
972  } else if (den != 0) {
974  num, den, 32767);
975  }
976  return 0;
977 }
978 
979 /* this atom contains actual media data */
981 {
982  if (atom.size == 0) /* wrong one (MP4) */
983  return 0;
984  c->found_mdat=1;
985  return 0; /* now go for moov */
986 }
987 
988 #define DRM_BLOB_SIZE 56
989 
991 {
992  uint8_t intermediate_key[20];
993  uint8_t intermediate_iv[20];
994  uint8_t input[64];
995  uint8_t output[64];
996  uint8_t file_checksum[20];
997  uint8_t calculated_checksum[20];
998  struct AVSHA *sha;
999  int i;
1000  int ret = 0;
1001  uint8_t *activation_bytes = c->activation_bytes;
1002  uint8_t *fixed_key = c->audible_fixed_key;
1003 
1004  c->aax_mode = 1;
1005 
1006  sha = av_sha_alloc();
1007  if (!sha)
1008  return AVERROR(ENOMEM);
1009  av_free(c->aes_decrypt);
1010  c->aes_decrypt = av_aes_alloc();
1011  if (!c->aes_decrypt) {
1012  ret = AVERROR(ENOMEM);
1013  goto fail;
1014  }
1015 
1016  /* drm blob processing */
1017  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
1019  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
1020  avio_read(pb, file_checksum, 20);
1021 
1022  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
1023  for (i = 0; i < 20; i++)
1024  av_log(c->fc, AV_LOG_INFO, "%02x", file_checksum[i]);
1025  av_log(c->fc, AV_LOG_INFO, "\n");
1026 
1027  /* verify activation data */
1028  if (!activation_bytes) {
1029  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
1030  ret = 0; /* allow ffprobe to continue working on .aax files */
1031  goto fail;
1032  }
1033  if (c->activation_bytes_size != 4) {
1034  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
1035  ret = AVERROR(EINVAL);
1036  goto fail;
1037  }
1038 
1039  /* verify fixed key */
1040  if (c->audible_fixed_key_size != 16) {
1041  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
1042  ret = AVERROR(EINVAL);
1043  goto fail;
1044  }
1045 
1046  /* AAX (and AAX+) key derivation */
1047  av_sha_init(sha, 160);
1048  av_sha_update(sha, fixed_key, 16);
1049  av_sha_update(sha, activation_bytes, 4);
1050  av_sha_final(sha, intermediate_key);
1051  av_sha_init(sha, 160);
1052  av_sha_update(sha, fixed_key, 16);
1053  av_sha_update(sha, intermediate_key, 20);
1054  av_sha_update(sha, activation_bytes, 4);
1055  av_sha_final(sha, intermediate_iv);
1056  av_sha_init(sha, 160);
1057  av_sha_update(sha, intermediate_key, 16);
1058  av_sha_update(sha, intermediate_iv, 16);
1059  av_sha_final(sha, calculated_checksum);
1060  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
1061  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
1063  goto fail;
1064  }
1065  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
1066  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
1067  for (i = 0; i < 4; i++) {
1068  // file data (in output) is stored in big-endian mode
1069  if (activation_bytes[i] != output[3 - i]) { // critical error
1070  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
1072  goto fail;
1073  }
1074  }
1075  memcpy(c->file_key, output + 8, 16);
1076  memcpy(input, output + 26, 16);
1077  av_sha_init(sha, 160);
1078  av_sha_update(sha, input, 16);
1079  av_sha_update(sha, c->file_key, 16);
1080  av_sha_update(sha, fixed_key, 16);
1081  av_sha_final(sha, c->file_iv);
1082 
1083 fail:
1084  av_free(sha);
1085 
1086  return ret;
1087 }
1088 
1090 {
1091  if (c->audible_key_size != 16) {
1092  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_key value needs to be 16 bytes!\n");
1093  return AVERROR(EINVAL);
1094  }
1095 
1096  if (c->audible_iv_size != 16) {
1097  av_log(c->fc, AV_LOG_FATAL, "[aaxc] audible_iv value needs to be 16 bytes!\n");
1098  return AVERROR(EINVAL);
1099  }
1100 
1101  c->aes_decrypt = av_aes_alloc();
1102  if (!c->aes_decrypt) {
1103  return AVERROR(ENOMEM);
1104  }
1105 
1106  memcpy(c->file_key, c->audible_key, 16);
1107  memcpy(c->file_iv, c->audible_iv, 16);
1108  c->aax_mode = 1;
1109 
1110  return 0;
1111 }
1112 
1113 // Audible AAX (and AAX+) bytestream decryption
1115 {
1116  int blocks = 0;
1117  unsigned char iv[16];
1118 
1119  memcpy(iv, c->file_iv, 16); // iv is overwritten
1120  blocks = size >> 4; // trailing bytes are not encrypted!
1121  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
1122  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
1123 
1124  return 0;
1125 }
1126 
1127 /* read major brand, minor version and compatible brands and store them as metadata */
1129 {
1130  uint32_t minor_ver;
1131  int comp_brand_size;
1132  char* comp_brands_str;
1133  uint8_t type[5] = {0};
1134  int ret = ffio_read_size(pb, type, 4);
1135  if (ret < 0)
1136  return ret;
1137 
1138  if (strcmp(type, "qt "))
1139  c->isom = 1;
1140  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
1141  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
1142  minor_ver = avio_rb32(pb); /* minor version */
1143  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
1144 
1145  comp_brand_size = atom.size - 8;
1146  if (comp_brand_size < 0 || comp_brand_size == INT_MAX)
1147  return AVERROR_INVALIDDATA;
1148  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
1149  if (!comp_brands_str)
1150  return AVERROR(ENOMEM);
1151 
1152  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
1153  if (ret < 0) {
1154  av_freep(&comp_brands_str);
1155  return ret;
1156  }
1157  comp_brands_str[comp_brand_size] = 0;
1158  av_dict_set(&c->fc->metadata, "compatible_brands",
1159  comp_brands_str, AV_DICT_DONT_STRDUP_VAL);
1160 
1161  // Logic for handling Audible's .aaxc files
1162  if (!strcmp(type, "aaxc")) {
1163  mov_aaxc_crypto(c);
1164  }
1165 
1166  return 0;
1167 }
1168 
1169 /* this atom should contain all header atoms */
1171 {
1172  int ret;
1173 
1174  if (c->found_moov) {
1175  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
1176  avio_skip(pb, atom.size);
1177  return 0;
1178  }
1179 
1180  if ((ret = mov_read_default(c, pb, atom)) < 0)
1181  return ret;
1182  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
1183  /* so we don't parse the whole file if over a network */
1184  c->found_moov=1;
1185  return 0; /* now go for mdat */
1186 }
1187 
1189  MOVFragmentIndex *frag_index,
1190  int index,
1191  int id)
1192 {
1193  int i;
1194  MOVFragmentIndexItem * item;
1195 
1196  if (index < 0 || index >= frag_index->nb_items)
1197  return NULL;
1198  item = &frag_index->item[index];
1199  for (i = 0; i < item->nb_stream_info; i++)
1200  if (item->stream_info[i].id == id)
1201  return &item->stream_info[i];
1202 
1203  // This shouldn't happen
1204  return NULL;
1205 }
1206 
1207 static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
1208 {
1209  int i;
1210  MOVFragmentIndexItem * item;
1211 
1212  if (frag_index->current < 0 ||
1213  frag_index->current >= frag_index->nb_items)
1214  return;
1215 
1216  item = &frag_index->item[frag_index->current];
1217  for (i = 0; i < item->nb_stream_info; i++)
1218  if (item->stream_info[i].id == id) {
1219  item->current = i;
1220  return;
1221  }
1222 
1223  // id not found. This shouldn't happen.
1224  item->current = -1;
1225 }
1226 
1228  MOVFragmentIndex *frag_index)
1229 {
1230  MOVFragmentIndexItem *item;
1231  if (frag_index->current < 0 ||
1232  frag_index->current >= frag_index->nb_items)
1233  return NULL;
1234 
1235  item = &frag_index->item[frag_index->current];
1236  if (item->current >= 0 && item->current < item->nb_stream_info)
1237  return &item->stream_info[item->current];
1238 
1239  // This shouldn't happen
1240  return NULL;
1241 }
1242 
1243 static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
1244 {
1245  int a, b, m;
1246  int64_t moof_offset;
1247 
1248  // Optimize for appending new entries
1249  if (!frag_index->nb_items ||
1250  frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
1251  return frag_index->nb_items;
1252 
1253  a = -1;
1254  b = frag_index->nb_items;
1255 
1256  while (b - a > 1) {
1257  m = (a + b) >> 1;
1258  moof_offset = frag_index->item[m].moof_offset;
1259  if (moof_offset >= offset)
1260  b = m;
1261  if (moof_offset <= offset)
1262  a = m;
1263  }
1264  return b;
1265 }
1266 
1267 static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
1268 {
1269  av_assert0(frag_stream_info);
1270  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1271  return frag_stream_info->sidx_pts;
1272  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1273  return frag_stream_info->first_tfra_pts;
1274  return frag_stream_info->tfdt_dts;
1275 }
1276 
1277 static int64_t get_frag_time(MOVFragmentIndex *frag_index,
1278  int index, int track_id)
1279 {
1280  MOVFragmentStreamInfo * frag_stream_info;
1281  int64_t timestamp;
1282  int i;
1283 
1284  if (track_id >= 0) {
1285  frag_stream_info = get_frag_stream_info(frag_index, index, track_id);
1286  if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
1287  return frag_stream_info->sidx_pts;
1288  if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
1289  return frag_stream_info->first_tfra_pts;
1290  return frag_stream_info->sidx_pts;
1291  }
1292 
1293  for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
1294  frag_stream_info = &frag_index->item[index].stream_info[i];
1295  timestamp = get_stream_info_time(frag_stream_info);
1296  if (timestamp != AV_NOPTS_VALUE)
1297  return timestamp;
1298  }
1299  return AV_NOPTS_VALUE;
1300 }
1301 
1303  AVStream *st, int64_t timestamp)
1304 {
1305  int a, b, m, m0;
1306  int64_t frag_time;
1307  int id = -1;
1308 
1309  if (st) {
1310  // If the stream is referenced by any sidx, limit the search
1311  // to fragments that referenced this stream in the sidx
1312  MOVStreamContext *sc = st->priv_data;
1313  if (sc->has_sidx)
1314  id = st->id;
1315  }
1316 
1317  a = -1;
1318  b = frag_index->nb_items;
1319 
1320  while (b - a > 1) {
1321  m0 = m = (a + b) >> 1;
1322 
1323  while (m < b &&
1324  (frag_time = get_frag_time(frag_index, m, id)) == AV_NOPTS_VALUE)
1325  m++;
1326 
1327  if (m < b && frag_time <= timestamp)
1328  a = m;
1329  else
1330  b = m0;
1331  }
1332 
1333  return a;
1334 }
1335 
1336 static int update_frag_index(MOVContext *c, int64_t offset)
1337 {
1338  int index, i;
1339  MOVFragmentIndexItem * item;
1340  MOVFragmentStreamInfo * frag_stream_info;
1341 
1342  // If moof_offset already exists in frag_index, return index to it
1343  index = search_frag_moof_offset(&c->frag_index, offset);
1344  if (index < c->frag_index.nb_items &&
1345  c->frag_index.item[index].moof_offset == offset)
1346  return index;
1347 
1348  // offset is not yet in frag index.
1349  // Insert new item at index (sorted by moof offset)
1350  item = av_fast_realloc(c->frag_index.item,
1351  &c->frag_index.allocated_size,
1352  (c->frag_index.nb_items + 1) *
1353  sizeof(*c->frag_index.item));
1354  if (!item)
1355  return -1;
1356  c->frag_index.item = item;
1357 
1358  frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
1359  sizeof(*item->stream_info));
1360  if (!frag_stream_info)
1361  return -1;
1362 
1363  for (i = 0; i < c->fc->nb_streams; i++) {
1364  // Avoid building frag index if streams lack track id.
1365  if (c->fc->streams[i]->id < 0) {
1366  av_free(frag_stream_info);
1367  return AVERROR_INVALIDDATA;
1368  }
1369 
1370  frag_stream_info[i].id = c->fc->streams[i]->id;
1371  frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
1372  frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
1373  frag_stream_info[i].next_trun_dts = AV_NOPTS_VALUE;
1374  frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
1375  frag_stream_info[i].index_entry = -1;
1376  frag_stream_info[i].encryption_index = NULL;
1377  }
1378 
1379  if (index < c->frag_index.nb_items)
1380  memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
1381  (c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
1382 
1383  item = &c->frag_index.item[index];
1384  item->headers_read = 0;
1385  item->current = 0;
1386  item->nb_stream_info = c->fc->nb_streams;
1387  item->moof_offset = offset;
1388  item->stream_info = frag_stream_info;
1389  c->frag_index.nb_items++;
1390 
1391  return index;
1392 }
1393 
1394 static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
1395  int id, int entries)
1396 {
1397  int i;
1398  MOVFragmentStreamInfo * frag_stream_info;
1399 
1400  if (index < 0)
1401  return;
1402  for (i = index; i < frag_index->nb_items; i++) {
1403  frag_stream_info = get_frag_stream_info(frag_index, i, id);
1404  if (frag_stream_info && frag_stream_info->index_entry >= 0)
1405  frag_stream_info->index_entry += entries;
1406  }
1407 }
1408 
1410 {
1411  // Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
1412  c->fragment.found_tfhd = 0;
1413 
1414  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
1415  c->has_looked_for_mfra = 1;
1416  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
1417  int ret;
1418  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
1419  "for a mfra\n");
1420  if ((ret = mov_read_mfra(c, pb)) < 0) {
1421  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1422  "read the mfra (may be a live ismv)\n");
1423  }
1424  } else {
1425  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1426  "seekable, can not look for mfra\n");
1427  }
1428  }
1429  c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8;
1430  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1431  c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
1432  return mov_read_default(c, pb, atom);
1433 }
1434 
1435 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time, void *logctx)
1436 {
1437  if (time) {
1438  if (time >= 2082844800)
1439  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1440 
1441  if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
1442  av_log(logctx, AV_LOG_DEBUG, "creation_time is not representable\n");
1443  return;
1444  }
1445 
1446  avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
1447  }
1448 }
1449 
1451 {
1452  AVStream *st;
1453  MOVStreamContext *sc;
1454  int version;
1455  char language[4] = {0};
1456  unsigned lang;
1457  int64_t creation_time;
1458 
1459  if (c->fc->nb_streams < 1)
1460  return 0;
1461  st = c->fc->streams[c->fc->nb_streams-1];
1462  sc = st->priv_data;
1463 
1464  if (sc->time_scale) {
1465  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1466  return AVERROR_INVALIDDATA;
1467  }
1468 
1469  version = avio_r8(pb);
1470  if (version > 1) {
1471  avpriv_request_sample(c->fc, "Version %d", version);
1472  return AVERROR_PATCHWELCOME;
1473  }
1474  avio_rb24(pb); /* flags */
1475  if (version == 1) {
1476  creation_time = avio_rb64(pb);
1477  avio_rb64(pb);
1478  } else {
1479  creation_time = avio_rb32(pb);
1480  avio_rb32(pb); /* modification time */
1481  }
1482  mov_metadata_creation_time(&st->metadata, creation_time, c->fc);
1483 
1484  sc->time_scale = avio_rb32(pb);
1485  if (sc->time_scale <= 0) {
1486  av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1487  sc->time_scale = 1;
1488  }
1489  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1490 
1491  lang = avio_rb16(pb); /* language */
1492  if (ff_mov_lang_to_iso639(lang, language))
1493  av_dict_set(&st->metadata, "language", language, 0);
1494  avio_rb16(pb); /* quality */
1495 
1496  return 0;
1497 }
1498 
1500 {
1501  int i;
1502  int64_t creation_time;
1503  int version = avio_r8(pb); /* version */
1504  avio_rb24(pb); /* flags */
1505 
1506  if (version == 1) {
1507  creation_time = avio_rb64(pb);
1508  avio_rb64(pb);
1509  } else {
1510  creation_time = avio_rb32(pb);
1511  avio_rb32(pb); /* modification time */
1512  }
1513  mov_metadata_creation_time(&c->fc->metadata, creation_time, c->fc);
1514  c->time_scale = avio_rb32(pb); /* time scale */
1515  if (c->time_scale <= 0) {
1516  av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
1517  c->time_scale = 1;
1518  }
1519  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1520 
1521  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1522  // set the AVFormatContext duration because the duration of individual tracks
1523  // may be inaccurate
1524  if (!c->trex_data)
1525  c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
1526  avio_rb32(pb); /* preferred scale */
1527 
1528  avio_rb16(pb); /* preferred volume */
1529 
1530  avio_skip(pb, 10); /* reserved */
1531 
1532  /* movie display matrix, store it in main context and use it later on */
1533  for (i = 0; i < 3; i++) {
1534  c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
1535  c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
1536  c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
1537  }
1538 
1539  avio_rb32(pb); /* preview time */
1540  avio_rb32(pb); /* preview duration */
1541  avio_rb32(pb); /* poster time */
1542  avio_rb32(pb); /* selection time */
1543  avio_rb32(pb); /* selection duration */
1544  avio_rb32(pb); /* current time */
1545  avio_rb32(pb); /* next track ID */
1546 
1547  return 0;
1548 }
1549 
1551 {
1552  AVStream *st;
1553  int little_endian;
1554 
1555  if (c->fc->nb_streams < 1)
1556  return 0;
1557  st = c->fc->streams[c->fc->nb_streams-1];
1558 
1559  little_endian = avio_rb16(pb) & 0xFF;
1560  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1561  if (little_endian == 1) {
1562  switch (st->codecpar->codec_id) {
1563  case AV_CODEC_ID_PCM_S24BE:
1565  break;
1566  case AV_CODEC_ID_PCM_S32BE:
1568  break;
1569  case AV_CODEC_ID_PCM_F32BE:
1571  break;
1572  case AV_CODEC_ID_PCM_F64BE:
1574  break;
1575  default:
1576  break;
1577  }
1578  }
1579  return 0;
1580 }
1581 
1583 {
1584  AVStream *st;
1585  uint8_t *icc_profile;
1586  char color_parameter_type[5] = { 0 };
1587  uint16_t color_primaries, color_trc, color_matrix;
1588  int ret;
1589 
1590  if (c->fc->nb_streams < 1)
1591  return 0;
1592  st = c->fc->streams[c->fc->nb_streams - 1];
1593 
1594  ret = ffio_read_size(pb, color_parameter_type, 4);
1595  if (ret < 0)
1596  return ret;
1597  if (strncmp(color_parameter_type, "nclx", 4) &&
1598  strncmp(color_parameter_type, "nclc", 4) &&
1599  strncmp(color_parameter_type, "prof", 4)) {
1600  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1601  color_parameter_type);
1602  return 0;
1603  }
1604 
1605  if (!strncmp(color_parameter_type, "prof", 4)) {
1606  icc_profile = av_stream_new_side_data(st, AV_PKT_DATA_ICC_PROFILE, atom.size - 4);
1607  if (!icc_profile)
1608  return AVERROR(ENOMEM);
1609  ret = ffio_read_size(pb, icc_profile, atom.size - 4);
1610  if (ret < 0)
1611  return ret;
1612  } else {
1613  color_primaries = avio_rb16(pb);
1614  color_trc = avio_rb16(pb);
1615  color_matrix = avio_rb16(pb);
1616 
1617  av_log(c->fc, AV_LOG_TRACE,
1618  "%s: pri %d trc %d matrix %d",
1619  color_parameter_type, color_primaries, color_trc, color_matrix);
1620 
1621  if (!strncmp(color_parameter_type, "nclx", 4)) {
1622  uint8_t color_range = avio_r8(pb) >> 7;
1623  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1624  if (color_range)
1626  else
1628  }
1629 
1632  if (!av_color_transfer_name(color_trc))
1633  color_trc = AVCOL_TRC_UNSPECIFIED;
1634  if (!av_color_space_name(color_matrix))
1635  color_matrix = AVCOL_SPC_UNSPECIFIED;
1636 
1638  st->codecpar->color_trc = color_trc;
1639  st->codecpar->color_space = color_matrix;
1640  av_log(c->fc, AV_LOG_TRACE, "\n");
1641  }
1642  return 0;
1643 }
1644 
1646 {
1647  AVStream *st;
1648  unsigned mov_field_order;
1649  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1650 
1651  if (c->fc->nb_streams < 1) // will happen with jp2 files
1652  return 0;
1653  st = c->fc->streams[c->fc->nb_streams-1];
1654  if (atom.size < 2)
1655  return AVERROR_INVALIDDATA;
1656  mov_field_order = avio_rb16(pb);
1657  if ((mov_field_order & 0xFF00) == 0x0100)
1658  decoded_field_order = AV_FIELD_PROGRESSIVE;
1659  else if ((mov_field_order & 0xFF00) == 0x0200) {
1660  switch (mov_field_order & 0xFF) {
1661  case 0x01: decoded_field_order = AV_FIELD_TT;
1662  break;
1663  case 0x06: decoded_field_order = AV_FIELD_BB;
1664  break;
1665  case 0x09: decoded_field_order = AV_FIELD_TB;
1666  break;
1667  case 0x0E: decoded_field_order = AV_FIELD_BT;
1668  break;
1669  }
1670  }
1671  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1672  av_log(c->fc, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1673  }
1674  st->codecpar->field_order = decoded_field_order;
1675 
1676  return 0;
1677 }
1678 
1680 {
1681  int err = 0;
1682  uint64_t size = (uint64_t)par->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1683  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1684  return AVERROR_INVALIDDATA;
1685  if ((err = av_reallocp(&par->extradata, size)) < 0) {
1686  par->extradata_size = 0;
1687  return err;
1688  }
1690  return 0;
1691 }
1692 
1693 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1695  AVCodecParameters *par, uint8_t *buf)
1696 {
1697  int64_t result = atom.size;
1698  int err;
1699 
1700  AV_WB32(buf , atom.size + 8);
1701  AV_WL32(buf + 4, atom.type);
1702  err = ffio_read_size(pb, buf + 8, atom.size);
1703  if (err < 0) {
1704  par->extradata_size -= atom.size;
1705  return err;
1706  } else if (err < atom.size) {
1707  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1708  par->extradata_size -= atom.size - err;
1709  result = err;
1710  }
1711  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1712  return result;
1713 }
1714 
1715 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1717  enum AVCodecID codec_id)
1718 {
1719  AVStream *st;
1720  uint64_t original_size;
1721  int err;
1722 
1723  if (c->fc->nb_streams < 1) // will happen with jp2 files
1724  return 0;
1725  st = c->fc->streams[c->fc->nb_streams-1];
1726 
1727  if (st->codecpar->codec_id != codec_id)
1728  return 0; /* unexpected codec_id - don't mess with extradata */
1729 
1730  original_size = st->codecpar->extradata_size;
1731  err = mov_realloc_extradata(st->codecpar, atom);
1732  if (err)
1733  return err;
1734 
1735  err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->extradata + original_size);
1736  if (err < 0)
1737  return err;
1738  return 0; // Note: this is the original behavior to ignore truncation.
1739 }
1740 
1741 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1743 {
1744  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1745 }
1746 
1748 {
1749  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1750 }
1751 
1753 {
1754  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1755 }
1756 
1758 {
1759  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1760 }
1761 
1763 {
1764  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1765  if (!ret)
1766  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1767  return ret;
1768 }
1769 
1771 {
1772  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1773 
1774  if (!ret && c->fc->nb_streams >= 1) {
1775  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1776  if (par->extradata_size >= 40) {
1777  par->height = AV_RB16(&par->extradata[36]);
1778  par->width = AV_RB16(&par->extradata[38]);
1779  }
1780  }
1781  return ret;
1782 }
1783 
1785 {
1786  if (c->fc->nb_streams >= 1) {
1787  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1788  if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1789  par->codec_id == AV_CODEC_ID_H264 &&
1790  atom.size > 11) {
1791  int cid;
1792  avio_skip(pb, 10);
1793  cid = avio_rb16(pb);
1794  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1795  if (cid == 0xd4d || cid == 0xd4e)
1796  par->width = 1440;
1797  return 0;
1798  } else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
1799  par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
1800  par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
1801  atom.size >= 24) {
1802  int num, den;
1803  avio_skip(pb, 12);
1804  num = avio_rb32(pb);
1805  den = avio_rb32(pb);
1806  if (num <= 0 || den <= 0)
1807  return 0;
1808  switch (avio_rb32(pb)) {
1809  case 2:
1810  if (den >= INT_MAX / 2)
1811  return 0;
1812  den *= 2;
1813  case 1:
1814  c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.num = num;
1815  c->fc->streams[c->fc->nb_streams-1]->internal->display_aspect_ratio.den = den;
1816  default:
1817  return 0;
1818  }
1819  }
1820  }
1821 
1822  return mov_read_avid(c, pb, atom);
1823 }
1824 
1826 {
1827  int ret = 0;
1828  int length = 0;
1829  uint64_t original_size;
1830  if (c->fc->nb_streams >= 1) {
1831  AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
1832  if (par->codec_id == AV_CODEC_ID_H264)
1833  return 0;
1834  if (atom.size == 16) {
1835  original_size = par->extradata_size;
1836  ret = mov_realloc_extradata(par, atom);
1837  if (!ret) {
1838  length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
1839  if (length == atom.size) {
1840  const uint8_t range_value = par->extradata[original_size + 19];
1841  switch (range_value) {
1842  case 1:
1844  break;
1845  case 2:
1847  break;
1848  default:
1849  av_log(c->fc, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1850  break;
1851  }
1852  ff_dlog(c->fc, "color_range: %d\n", par->color_range);
1853  } else {
1854  /* For some reason the whole atom was not added to the extradata */
1855  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1856  }
1857  } else {
1858  av_log(c->fc, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1859  }
1860  } else {
1861  av_log(c->fc, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1862  }
1863  }
1864 
1865  return ret;
1866 }
1867 
1869 {
1870  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1871 }
1872 
1874 {
1875  AVStream *st;
1876  int ret;
1877 
1878  if (c->fc->nb_streams < 1)
1879  return 0;
1880  st = c->fc->streams[c->fc->nb_streams-1];
1881 
1882  if ((uint64_t)atom.size > (1<<30))
1883  return AVERROR_INVALIDDATA;
1884 
1885  if (st->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
1888  // pass all frma atom to codec, needed at least for QDMC and QDM2
1889  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1890  if (ret < 0)
1891  return ret;
1892  } else if (atom.size > 8) { /* to read frma, esds atoms */
1893  if (st->codecpar->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1894  uint64_t buffer;
1895  ret = ffio_ensure_seekback(pb, 8);
1896  if (ret < 0)
1897  return ret;
1898  buffer = avio_rb64(pb);
1899  atom.size -= 8;
1900  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1901  && buffer >> 32 <= atom.size
1902  && buffer >> 32 >= 8) {
1903  avio_skip(pb, -8);
1904  atom.size += 8;
1905  } else if (!st->codecpar->extradata_size) {
1906 #define ALAC_EXTRADATA_SIZE 36
1908  if (!st->codecpar->extradata)
1909  return AVERROR(ENOMEM);
1912  AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
1913  AV_WB64(st->codecpar->extradata + 12, buffer);
1914  avio_read(pb, st->codecpar->extradata + 20, 16);
1915  avio_skip(pb, atom.size - 24);
1916  return 0;
1917  }
1918  }
1919  if ((ret = mov_read_default(c, pb, atom)) < 0)
1920  return ret;
1921  } else
1922  avio_skip(pb, atom.size);
1923  return 0;
1924 }
1925 
1926 /**
1927  * This function reads atom content and puts data in extradata without tag
1928  * nor size unlike mov_read_extradata.
1929  */
1931 {
1932  AVStream *st;
1933  int ret;
1934 
1935  if (c->fc->nb_streams < 1)
1936  return 0;
1937  st = c->fc->streams[c->fc->nb_streams-1];
1938 
1939  if ((uint64_t)atom.size > (1<<30))
1940  return AVERROR_INVALIDDATA;
1941 
1942  if (atom.size >= 10) {
1943  // Broken files created by legacy versions of libavformat will
1944  // wrap a whole fiel atom inside of a glbl atom.
1945  unsigned size = avio_rb32(pb);
1946  unsigned type = avio_rl32(pb);
1947  if (avio_feof(pb))
1948  return AVERROR_INVALIDDATA;
1949  avio_seek(pb, -8, SEEK_CUR);
1950  if (type == MKTAG('f','i','e','l') && size == atom.size)
1951  return mov_read_default(c, pb, atom);
1952  }
1953  if (st->codecpar->extradata_size > 1 && st->codecpar->extradata) {
1954  av_log(c->fc, AV_LOG_WARNING, "ignoring multiple glbl\n");
1955  return 0;
1956  }
1957  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
1958  if (ret < 0)
1959  return ret;
1960  if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
1961  /* HEVC-based Dolby Vision derived from hvc1.
1962  Happens to match with an identifier
1963  previously utilized for DV. Thus, if we have
1964  the hvcC extradata box available as specified,
1965  set codec to HEVC */
1967 
1968  return 0;
1969 }
1970 
1972 {
1973  AVStream *st;
1974  uint8_t profile_level;
1975  int ret;
1976 
1977  if (c->fc->nb_streams < 1)
1978  return 0;
1979  st = c->fc->streams[c->fc->nb_streams-1];
1980 
1981  if (atom.size >= (1<<28) || atom.size < 7)
1982  return AVERROR_INVALIDDATA;
1983 
1984  profile_level = avio_r8(pb);
1985  if ((profile_level & 0xf0) != 0xc0)
1986  return 0;
1987 
1988  avio_seek(pb, 6, SEEK_CUR);
1989  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 7);
1990  if (ret < 0)
1991  return ret;
1992 
1993  return 0;
1994 }
1995 
1996 /**
1997  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1998  * but can have extradata appended at the end after the 40 bytes belonging
1999  * to the struct.
2000  */
2002 {
2003  AVStream *st;
2004  int ret;
2005 
2006  if (c->fc->nb_streams < 1)
2007  return 0;
2008  if (atom.size <= 40)
2009  return 0;
2010  st = c->fc->streams[c->fc->nb_streams-1];
2011 
2012  if ((uint64_t)atom.size > (1<<30))
2013  return AVERROR_INVALIDDATA;
2014 
2015  avio_skip(pb, 40);
2016  ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 40);
2017  if (ret < 0)
2018  return ret;
2019 
2020  return 0;
2021 }
2022 
2024 {
2025  AVStream *st;
2026  MOVStreamContext *sc;
2027  unsigned int i, entries;
2028 
2029  if (c->trak_index < 0) {
2030  av_log(c->fc, AV_LOG_WARNING, "STCO outside TRAK\n");
2031  return 0;
2032  }
2033  if (c->fc->nb_streams < 1)
2034  return 0;
2035  st = c->fc->streams[c->fc->nb_streams-1];
2036  sc = st->priv_data;
2037 
2038  avio_r8(pb); /* version */
2039  avio_rb24(pb); /* flags */
2040 
2041  entries = avio_rb32(pb);
2042 
2043  if (!entries)
2044  return 0;
2045 
2046  if (sc->chunk_offsets) {
2047  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STCO atom\n");
2048  return 0;
2049  }
2050  av_free(sc->chunk_offsets);
2051  sc->chunk_count = 0;
2052  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2053  if (!sc->chunk_offsets)
2054  return AVERROR(ENOMEM);
2055  sc->chunk_count = entries;
2056 
2057  if (atom.type == MKTAG('s','t','c','o'))
2058  for (i = 0; i < entries && !pb->eof_reached; i++)
2059  sc->chunk_offsets[i] = avio_rb32(pb);
2060  else if (atom.type == MKTAG('c','o','6','4'))
2061  for (i = 0; i < entries && !pb->eof_reached; i++)
2062  sc->chunk_offsets[i] = avio_rb64(pb);
2063  else
2064  return AVERROR_INVALIDDATA;
2065 
2066  sc->chunk_count = i;
2067 
2068  if (pb->eof_reached) {
2069  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
2070  return AVERROR_EOF;
2071  }
2072 
2073  return 0;
2074 }
2075 
2076 static int mov_codec_id(AVStream *st, uint32_t format)
2077 {
2079 
2080  if (id <= 0 &&
2081  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
2082  (format & 0xFFFF) == 'T' + ('S' << 8)))
2084 
2085  if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
2087  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
2088  /* skip old ASF MPEG-4 tag */
2089  format && format != MKTAG('m','p','4','s')) {
2091  if (id <= 0)
2093  if (id > 0)
2095  else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
2097  st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
2099  if (id > 0)
2101  else
2103  }
2104  }
2105 
2106  st->codecpar->codec_tag = format;
2107 
2108  return id;
2109 }
2110 
2112  AVStream *st, MOVStreamContext *sc)
2113 {
2114  uint8_t codec_name[32] = { 0 };
2115  int64_t stsd_start;
2116  unsigned int len;
2117  uint32_t id = 0;
2118 
2119  /* The first 16 bytes of the video sample description are already
2120  * read in ff_mov_read_stsd_entries() */
2121  stsd_start = avio_tell(pb) - 16;
2122 
2123  avio_rb16(pb); /* version */
2124  avio_rb16(pb); /* revision level */
2125  id = avio_rl32(pb); /* vendor */
2126  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2127  avio_rb32(pb); /* temporal quality */
2128  avio_rb32(pb); /* spatial quality */
2129 
2130  st->codecpar->width = avio_rb16(pb); /* width */
2131  st->codecpar->height = avio_rb16(pb); /* height */
2132 
2133  avio_rb32(pb); /* horiz resolution */
2134  avio_rb32(pb); /* vert resolution */
2135  avio_rb32(pb); /* data size, always 0 */
2136  avio_rb16(pb); /* frames per samples */
2137 
2138  len = avio_r8(pb); /* codec name, pascal string */
2139  if (len > 31)
2140  len = 31;
2141  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
2142  if (len < 31)
2143  avio_skip(pb, 31 - len);
2144 
2145  if (codec_name[0])
2146  av_dict_set(&st->metadata, "encoder", codec_name, 0);
2147 
2148  /* codec_tag YV12 triggers an UV swap in rawdec.c */
2149  if (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
2150  st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
2151  st->codecpar->width &= ~1;
2152  st->codecpar->height &= ~1;
2153  }
2154  /* Flash Media Server uses tag H.263 with Sorenson Spark */
2155  if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
2156  !strncmp(codec_name, "Sorenson H263", 13))
2158 
2159  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
2160 
2161  avio_seek(pb, stsd_start, SEEK_SET);
2162 
2163  if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2164  st->codecpar->bits_per_coded_sample &= 0x1F;
2165  sc->has_palette = 1;
2166  }
2167 }
2168 
2170  AVStream *st, MOVStreamContext *sc)
2171 {
2172  int bits_per_sample, flags;
2173  uint16_t version = avio_rb16(pb);
2174  uint32_t id = 0;
2175  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
2176 
2177  avio_rb16(pb); /* revision level */
2178  id = avio_rl32(pb); /* vendor */
2179  av_dict_set(&st->metadata, "vendor_id", av_fourcc2str(id), 0);
2180 
2181  st->codecpar->channels = avio_rb16(pb); /* channel count */
2182  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
2183  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
2184 
2185  sc->audio_cid = avio_rb16(pb);
2186  avio_rb16(pb); /* packet size = 0 */
2187 
2188  st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
2189 
2190  // Read QT version 1 fields. In version 0 these do not exist.
2191  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
2192  if (!c->isom ||
2193  (compatible_brands && strstr(compatible_brands->value, "qt ")) ||
2194  (sc->stsd_version == 0 && version > 0)) {
2195  if (version == 1) {
2196  sc->samples_per_frame = avio_rb32(pb);
2197  avio_rb32(pb); /* bytes per packet */
2198  sc->bytes_per_frame = avio_rb32(pb);
2199  avio_rb32(pb); /* bytes per sample */
2200  } else if (version == 2) {
2201  avio_rb32(pb); /* sizeof struct only */
2203  st->codecpar->channels = avio_rb32(pb);
2204  avio_rb32(pb); /* always 0x7F000000 */
2206 
2207  flags = avio_rb32(pb); /* lpcm format specific flag */
2208  sc->bytes_per_frame = avio_rb32(pb);
2209  sc->samples_per_frame = avio_rb32(pb);
2210  if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
2211  st->codecpar->codec_id =
2213  flags);
2214  }
2215  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2216  /* can't correctly handle variable sized packet as audio unit */
2217  switch (st->codecpar->codec_id) {
2218  case AV_CODEC_ID_MP2:
2219  case AV_CODEC_ID_MP3:
2221  break;
2222  }
2223  }
2224  }
2225 
2226  if (sc->format == 0) {
2227  if (st->codecpar->bits_per_coded_sample == 8)
2228  st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
2229  else if (st->codecpar->bits_per_coded_sample == 16)
2230  st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
2231  }
2232 
2233  switch (st->codecpar->codec_id) {
2234  case AV_CODEC_ID_PCM_S8:
2235  case AV_CODEC_ID_PCM_U8:
2236  if (st->codecpar->bits_per_coded_sample == 16)
2238  break;
2239  case AV_CODEC_ID_PCM_S16LE:
2240  case AV_CODEC_ID_PCM_S16BE:
2241  if (st->codecpar->bits_per_coded_sample == 8)
2243  else if (st->codecpar->bits_per_coded_sample == 24)
2244  st->codecpar->codec_id =
2247  else if (st->codecpar->bits_per_coded_sample == 32)
2248  st->codecpar->codec_id =
2251  break;
2252  /* set values for old format before stsd version 1 appeared */
2253  case AV_CODEC_ID_MACE3:
2254  sc->samples_per_frame = 6;
2255  sc->bytes_per_frame = 2 * st->codecpar->channels;
2256  break;
2257  case AV_CODEC_ID_MACE6:
2258  sc->samples_per_frame = 6;
2259  sc->bytes_per_frame = 1 * st->codecpar->channels;
2260  break;
2262  sc->samples_per_frame = 64;
2263  sc->bytes_per_frame = 34 * st->codecpar->channels;
2264  break;
2265  case AV_CODEC_ID_GSM:
2266  sc->samples_per_frame = 160;
2267  sc->bytes_per_frame = 33;
2268  break;
2269  default:
2270  break;
2271  }
2272 
2273  bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
2274  if (bits_per_sample && (bits_per_sample >> 3) * (uint64_t)st->codecpar->channels <= INT_MAX) {
2275  st->codecpar->bits_per_coded_sample = bits_per_sample;
2276  sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
2277  }
2278 }
2279 
2281  AVStream *st, MOVStreamContext *sc,
2282  int64_t size)
2283 {
2284  // ttxt stsd contains display flags, justification, background
2285  // color, fonts, and default styles, so fake an atom to read it
2286  MOVAtom fake_atom = { .size = size };
2287  // mp4s contains a regular esds atom
2288  if (st->codecpar->codec_tag != AV_RL32("mp4s"))
2289  mov_read_glbl(c, pb, fake_atom);
2290  st->codecpar->width = sc->width;
2291  st->codecpar->height = sc->height;
2292 }
2293 
2294 static uint32_t yuv_to_rgba(uint32_t ycbcr)
2295 {
2296  uint8_t r, g, b;
2297  int y, cb, cr;
2298 
2299  y = (ycbcr >> 16) & 0xFF;
2300  cr = (ycbcr >> 8) & 0xFF;
2301  cb = ycbcr & 0xFF;
2302 
2303  b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
2304  g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
2305  r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
2306 
2307  return (r << 16) | (g << 8) | b;
2308 }
2309 
2311 {
2312  char buf[256] = {0};
2313  uint8_t *src = st->codecpar->extradata;
2314  int i, ret;
2315 
2316  if (st->codecpar->extradata_size != 64)
2317  return 0;
2318 
2319  if (st->codecpar->width > 0 && st->codecpar->height > 0)
2320  snprintf(buf, sizeof(buf), "size: %dx%d\n",
2321  st->codecpar->width, st->codecpar->height);
2322  av_strlcat(buf, "palette: ", sizeof(buf));
2323 
2324  for (i = 0; i < 16; i++) {
2325  uint32_t yuv = AV_RB32(src + i * 4);
2326  uint32_t rgba = yuv_to_rgba(yuv);
2327 
2328  av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
2329  }
2330 
2331  if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
2332  return 0;
2333 
2334  ret = ff_alloc_extradata(st->codecpar, strlen(buf));
2335  if (ret < 0)
2336  return ret;
2337  memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
2338 
2339  return 0;
2340 }
2341 
2343  AVStream *st, MOVStreamContext *sc,
2344  int64_t size)
2345 {
2346  int ret;
2347 
2348  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
2349  if ((int)size != size)
2350  return AVERROR(ENOMEM);
2351 
2352  ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
2353  if (ret < 0)
2354  return ret;
2355  if (size > 16) {
2356  MOVStreamContext *tmcd_ctx = st->priv_data;
2357  int val;
2358  val = AV_RB32(st->codecpar->extradata + 4);
2359  tmcd_ctx->tmcd_flags = val;
2360  st->avg_frame_rate.num = AV_RB32(st->codecpar->extradata + 8); /* timescale */
2361  st->avg_frame_rate.den = AV_RB32(st->codecpar->extradata + 12); /* frameDuration */
2362 #if FF_API_LAVF_AVCTX
2364  st->codec->time_base = av_inv_q(st->avg_frame_rate);
2366 #endif
2367  if (size > 30) {
2368  uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
2369  uint32_t format = AV_RB32(st->codecpar->extradata + 22);
2370  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
2371  uint16_t str_size = AV_RB16(st->codecpar->extradata + 26); /* string length */
2372  if (str_size > 0 && size >= (int)str_size + 30 &&
2373  st->codecpar->extradata[30] /* Don't add empty string */) {
2374  char *reel_name = av_malloc(str_size + 1);
2375  if (!reel_name)
2376  return AVERROR(ENOMEM);
2377  memcpy(reel_name, st->codecpar->extradata + 30, str_size);
2378  reel_name[str_size] = 0; /* Add null terminator */
2379  av_dict_set(&st->metadata, "reel_name", reel_name,
2381  }
2382  }
2383  }
2384  }
2385  } else {
2386  /* other codec type, just skip (rtp, mp4s ...) */
2387  avio_skip(pb, size);
2388  }
2389  return 0;
2390 }
2391 
2393  AVStream *st, MOVStreamContext *sc)
2394 {
2395  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2396  !st->codecpar->sample_rate && sc->time_scale > 1)
2397  st->codecpar->sample_rate = sc->time_scale;
2398 
2399  /* special codec parameters handling */
2400  switch (st->codecpar->codec_id) {
2401 #if CONFIG_DV_DEMUXER
2402  case AV_CODEC_ID_DVAUDIO:
2403  c->dv_fctx = avformat_alloc_context();
2404  if (!c->dv_fctx) {
2405  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2406  return AVERROR(ENOMEM);
2407  }
2408  c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
2409  if (!c->dv_demux) {
2410  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2411  return AVERROR(ENOMEM);
2412  }
2413  sc->dv_audio_container = 1;
2415  break;
2416 #endif
2417  /* no ifdef since parameters are always those */
2418  case AV_CODEC_ID_QCELP:
2419  st->codecpar->channels = 1;
2420  // force sample rate for qcelp when not stored in mov
2421  if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
2422  st->codecpar->sample_rate = 8000;
2423  // FIXME: Why is the following needed for some files?
2424  sc->samples_per_frame = 160;
2425  if (!sc->bytes_per_frame)
2426  sc->bytes_per_frame = 35;
2427  break;
2428  case AV_CODEC_ID_AMR_NB:
2429  st->codecpar->channels = 1;
2430  /* force sample rate for amr, stsd in 3gp does not store sample rate */
2431  st->codecpar->sample_rate = 8000;
2432  break;
2433  case AV_CODEC_ID_AMR_WB:
2434  st->codecpar->channels = 1;
2435  st->codecpar->sample_rate = 16000;
2436  break;
2437  case AV_CODEC_ID_MP2:
2438  case AV_CODEC_ID_MP3:
2439  /* force type after stsd for m1a hdlr */
2441  break;
2442  case AV_CODEC_ID_GSM:
2443  case AV_CODEC_ID_ADPCM_MS:
2445  case AV_CODEC_ID_ILBC:
2446  case AV_CODEC_ID_MACE3:
2447  case AV_CODEC_ID_MACE6:
2448  case AV_CODEC_ID_QDM2:
2450  break;
2451  case AV_CODEC_ID_ALAC:
2452  if (st->codecpar->extradata_size == 36) {
2453  st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
2454  st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
2455  }
2456  break;
2457  case AV_CODEC_ID_AC3:
2458  case AV_CODEC_ID_EAC3:
2460  case AV_CODEC_ID_VC1:
2461  case AV_CODEC_ID_VP8:
2462  case AV_CODEC_ID_VP9:
2464  break;
2465  case AV_CODEC_ID_AV1:
2467  break;
2468  default:
2469  break;
2470  }
2471  return 0;
2472 }
2473 
2475  int codec_tag, int format,
2476  int64_t size)
2477 {
2478  int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2479 
2480  if (codec_tag &&
2481  (codec_tag != format &&
2482  // AVID 1:1 samples with differing data format and codec tag exist
2483  (codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
2484  // prores is allowed to have differing data format and codec tag
2485  codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
2486  // so is dv (sigh)
2487  codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
2488  (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2489  : codec_tag != MKTAG('j','p','e','g')))) {
2490  /* Multiple fourcc, we skip JPEG. This is not correct, we should
2491  * export it as a separate AVStream but this needs a few changes
2492  * in the MOV demuxer, patch welcome. */
2493 
2494  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2495  avio_skip(pb, size);
2496  return 1;
2497  }
2498 
2499  return 0;
2500 }
2501 
2503 {
2504  AVStream *st;
2505  MOVStreamContext *sc;
2506  int pseudo_stream_id;
2507 
2508  av_assert0 (c->fc->nb_streams >= 1);
2509  st = c->fc->streams[c->fc->nb_streams-1];
2510  sc = st->priv_data;
2511 
2512  for (pseudo_stream_id = 0;
2513  pseudo_stream_id < entries && !pb->eof_reached;
2514  pseudo_stream_id++) {
2515  //Parsing Sample description table
2516  enum AVCodecID id;
2517  int ret, dref_id = 1;
2518  MOVAtom a = { AV_RL32("stsd") };
2519  int64_t start_pos = avio_tell(pb);
2520  int64_t size = avio_rb32(pb); /* size */
2521  uint32_t format = avio_rl32(pb); /* data format */
2522 
2523  if (size >= 16) {
2524  avio_rb32(pb); /* reserved */
2525  avio_rb16(pb); /* reserved */
2526  dref_id = avio_rb16(pb);
2527  } else if (size <= 7) {
2528  av_log(c->fc, AV_LOG_ERROR,
2529  "invalid size %"PRId64" in stsd\n", size);
2530  return AVERROR_INVALIDDATA;
2531  }
2532 
2534  size - (avio_tell(pb) - start_pos))) {
2535  sc->stsd_count++;
2536  continue;
2537  }
2538 
2539  sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2540  sc->dref_id= dref_id;
2541  sc->format = format;
2542 
2543  id = mov_codec_id(st, format);
2544 
2545  av_log(c->fc, AV_LOG_TRACE,
2546  "size=%"PRId64" 4CC=%s codec_type=%d\n", size,
2548 
2549  st->codecpar->codec_id = id;
2551  mov_parse_stsd_video(c, pb, st, sc);
2552  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
2553  mov_parse_stsd_audio(c, pb, st, sc);
2554  if (st->codecpar->sample_rate < 0) {
2555  av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
2556  return AVERROR_INVALIDDATA;
2557  }
2558  if (st->codecpar->channels < 0) {
2559  av_log(c->fc, AV_LOG_ERROR, "Invalid channels %d\n", st->codecpar->channels);
2560  return AVERROR_INVALIDDATA;
2561  }
2562  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
2563  mov_parse_stsd_subtitle(c, pb, st, sc,
2564  size - (avio_tell(pb) - start_pos));
2565  } else {
2566  ret = mov_parse_stsd_data(c, pb, st, sc,
2567  size - (avio_tell(pb) - start_pos));
2568  if (ret < 0)
2569  return ret;
2570  }
2571  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2572  a.size = size - (avio_tell(pb) - start_pos);
2573  if (a.size > 8) {
2574  if ((ret = mov_read_default(c, pb, a)) < 0)
2575  return ret;
2576  } else if (a.size > 0)
2577  avio_skip(pb, a.size);
2578 
2579  if (sc->extradata && st->codecpar->extradata) {
2580  int extra_size = st->codecpar->extradata_size;
2581 
2582  /* Move the current stream extradata to the stream context one. */
2583  sc->extradata_size[pseudo_stream_id] = extra_size;
2584  sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
2585  st->codecpar->extradata = NULL;
2586  st->codecpar->extradata_size = 0;
2587  }
2588  sc->stsd_count++;
2589  }
2590 
2591  if (pb->eof_reached) {
2592  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
2593  return AVERROR_EOF;
2594  }
2595 
2596  return 0;
2597 }
2598 
2600 {
2601  AVStream *st;
2602  MOVStreamContext *sc;
2603  int ret, entries;
2604 
2605  if (c->fc->nb_streams < 1)
2606  return 0;
2607  st = c->fc->streams[c->fc->nb_streams - 1];
2608  sc = st->priv_data;
2609 
2610  sc->stsd_version = avio_r8(pb);
2611  avio_rb24(pb); /* flags */
2612  entries = avio_rb32(pb);
2613 
2614  /* Each entry contains a size (4 bytes) and format (4 bytes). */
2615  if (entries <= 0 || entries > atom.size / 8 || entries > 1024) {
2616  av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
2617  return AVERROR_INVALIDDATA;
2618  }
2619 
2620  if (sc->extradata) {
2621  av_log(c->fc, AV_LOG_ERROR,
2622  "Duplicate stsd found in this track.\n");
2623  return AVERROR_INVALIDDATA;
2624  }
2625 
2626  /* Prepare space for hosting multiple extradata. */
2627  sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
2628  if (!sc->extradata)
2629  return AVERROR(ENOMEM);
2630 
2631  sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
2632  if (!sc->extradata_size) {
2633  ret = AVERROR(ENOMEM);
2634  goto fail;
2635  }
2636 
2637  ret = ff_mov_read_stsd_entries(c, pb, entries);
2638  if (ret < 0)
2639  goto fail;
2640 
2641  /* Restore back the primary extradata. */
2642  av_freep(&st->codecpar->extradata);
2643  st->codecpar->extradata_size = sc->extradata_size[0];
2644  if (sc->extradata_size[0]) {
2646  if (!st->codecpar->extradata)
2647  return AVERROR(ENOMEM);
2648  memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
2649  }
2650 
2651  return mov_finalize_stsd_codec(c, pb, st, sc);
2652 fail:
2653  if (sc->extradata) {
2654  int j;
2655  for (j = 0; j < sc->stsd_count; j++)
2656  av_freep(&sc->extradata[j]);
2657  }
2658 
2659  av_freep(&sc->extradata);
2660  av_freep(&sc->extradata_size);
2661  return ret;
2662 }
2663 
2665 {
2666  AVStream *st;
2667  MOVStreamContext *sc;
2668  unsigned int i, entries;
2669 
2670  if (c->fc->nb_streams < 1)
2671  return 0;
2672  st = c->fc->streams[c->fc->nb_streams-1];
2673  sc = st->priv_data;
2674 
2675  avio_r8(pb); /* version */
2676  avio_rb24(pb); /* flags */
2677 
2678  entries = avio_rb32(pb);
2679  if ((uint64_t)entries * 12 + 4 > atom.size)
2680  return AVERROR_INVALIDDATA;
2681 
2682  av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\n", c->fc->nb_streams - 1, entries);
2683 
2684  if (!entries)
2685  return 0;
2686  if (sc->stsc_data) {
2687  av_log(c->fc, AV_LOG_WARNING, "Ignoring duplicated STSC atom\n");
2688  return 0;
2689  }
2690  av_free(sc->stsc_data);
2691  sc->stsc_count = 0;
2692  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2693  if (!sc->stsc_data)
2694  return AVERROR(ENOMEM);
2695 
2696  for (i = 0; i < entries && !pb->eof_reached; i++) {
2697  sc->stsc_data[i].first = avio_rb32(pb);
2698  sc->stsc_data[i].count = avio_rb32(pb);
2699  sc->stsc_data[i].id = avio_rb32(pb);
2700  }
2701 
2702  sc->stsc_count = i;
2703  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2704  int64_t first_min = i + 1;
2705  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2706  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2707  sc->stsc_data[i].first < first_min ||
2708  sc->stsc_data[i].count < 1 ||
2709  sc->stsc_data[i].id < 1) {
2710  av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
2711  if (i+1 >= sc->stsc_count) {
2712  if (sc->stsc_data[i].count == 0 && i > 0) {
2713  sc->stsc_count --;
2714  continue;
2715  }
2716  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2717  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2718  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2719  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2720  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
2721  continue;
2722  }
2723  av_assert0(sc->stsc_data[i+1].first >= 2);
2724  // We replace this entry by the next valid
2725  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2726  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2727  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
2728  }
2729  }
2730 
2731  if (pb->eof_reached) {
2732  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
2733  return AVERROR_EOF;
2734  }
2735 
2736  return 0;
2737 }
2738 
2739 static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
2740 {
2741  return index < count - 1;
2742 }
2743 
2744 /* Compute the samples value for the stsc entry at the given index. */
2745 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
2746 {
2747  int chunk_count;
2748 
2750  chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2751  else {
2752  // Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
2754  chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2755  }
2756 
2757  return sc->stsc_data[index].count * (int64_t)chunk_count;
2758 }
2759 
2761 {
2762  AVStream *st;
2763  MOVStreamContext *sc;
2764  unsigned i, entries;
2765 
2766  if (c->fc->nb_streams < 1)
2767  return 0;
2768  st = c->fc->streams[c->fc->nb_streams-1];
2769  sc = st->priv_data;
2770 
2771  avio_rb32(pb); // version + flags
2772 
2773  entries = avio_rb32(pb);
2774  if (sc->stps_data)
2775  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2776  av_free(sc->stps_data);
2777  sc->stps_count = 0;
2778  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2779  if (!sc->stps_data)
2780  return AVERROR(ENOMEM);
2781 
2782  for (i = 0; i < entries && !pb->eof_reached; i++) {
2783  sc->stps_data[i] = avio_rb32(pb);
2784  }
2785 
2786  sc->stps_count = i;
2787 
2788  if (pb->eof_reached) {
2789  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
2790  return AVERROR_EOF;
2791  }
2792 
2793  return 0;
2794 }
2795 
2797 {
2798  AVStream *st;
2799  MOVStreamContext *sc;
2800  unsigned int i, entries;
2801 
2802  if (c->fc->nb_streams < 1)
2803  return 0;
2804  st = c->fc->streams[c->fc->nb_streams-1];
2805  sc = st->priv_data;
2806 
2807  avio_r8(pb); /* version */
2808  avio_rb24(pb); /* flags */
2809 
2810  entries = avio_rb32(pb);
2811 
2812  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %u\n", entries);
2813 
2814  if (!entries) {
2815  sc->keyframe_absent = 1;
2816  if (!st->need_parsing && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2818  return 0;
2819  }
2820  if (sc->keyframes)
2821  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2822  if (entries >= UINT_MAX / sizeof(int))
2823  return AVERROR_INVALIDDATA;
2824  av_freep(&sc->keyframes);
2825  sc->keyframe_count = 0;
2826  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2827  if (!sc->keyframes)
2828  return AVERROR(ENOMEM);
2829 
2830  for (i = 0; i < entries && !pb->eof_reached; i++) {
2831  sc->keyframes[i] = avio_rb32(pb);
2832  }
2833 
2834  sc->keyframe_count = i;
2835 
2836  if (pb->eof_reached) {
2837  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
2838  return AVERROR_EOF;
2839  }
2840 
2841  return 0;
2842 }
2843 
2845 {
2846  AVStream *st;
2847  MOVStreamContext *sc;
2848  unsigned int i, entries, sample_size, field_size, num_bytes;
2849  GetBitContext gb;
2850  unsigned char* buf;
2851  int ret;
2852 
2853  if (c->fc->nb_streams < 1)
2854  return 0;
2855  st = c->fc->streams[c->fc->nb_streams-1];
2856  sc = st->priv_data;
2857 
2858  avio_r8(pb); /* version */
2859  avio_rb24(pb); /* flags */
2860 
2861  if (atom.type == MKTAG('s','t','s','z')) {
2862  sample_size = avio_rb32(pb);
2863  if (!sc->sample_size) /* do not overwrite value computed in stsd */
2864  sc->sample_size = sample_size;
2865  sc->stsz_sample_size = sample_size;
2866  field_size = 32;
2867  } else {
2868  sample_size = 0;
2869  avio_rb24(pb); /* reserved */
2870  field_size = avio_r8(pb);
2871  }
2872  entries = avio_rb32(pb);
2873 
2874  av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
2875 
2876  sc->sample_count = entries;
2877  if (sample_size)
2878  return 0;
2879 
2880  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2881  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %u\n", field_size);
2882  return AVERROR_INVALIDDATA;
2883  }
2884 
2885  if (!entries)
2886  return 0;
2887  if (entries >= (UINT_MAX - 4) / field_size)
2888  return AVERROR_INVALIDDATA;
2889  if (sc->sample_sizes)
2890  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2891  av_free(sc->sample_sizes);
2892  sc->sample_count = 0;
2893  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2894  if (!sc->sample_sizes)
2895  return AVERROR(ENOMEM);
2896 
2897  num_bytes = (entries*field_size+4)>>3;
2898 
2899  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2900  if (!buf) {
2901  av_freep(&sc->sample_sizes);
2902  return AVERROR(ENOMEM);
2903  }
2904 
2905  ret = ffio_read_size(pb, buf, num_bytes);
2906  if (ret < 0) {
2907  av_freep(&sc->sample_sizes);
2908  av_free(buf);
2909  av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
2910  return 0;
2911  }
2912 
2913  init_get_bits(&gb, buf, 8*num_bytes);
2914 
2915  for (i = 0; i < entries && !pb->eof_reached; i++) {
2916  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2917  if (sc->sample_sizes[i] < 0) {
2918  av_free(buf);
2919  av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
2920  return AVERROR_INVALIDDATA;
2921  }
2922  sc->data_size += sc->sample_sizes[i];
2923  }
2924 
2925  sc->sample_count = i;
2926 
2927  av_free(buf);
2928 
2929  if (pb->eof_reached) {
2930  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSZ atom\n");
2931  return AVERROR_EOF;
2932  }
2933 
2934  return 0;
2935 }
2936 
2938 {
2939  AVStream *st;
2940  MOVStreamContext *sc;
2941  unsigned int i, entries, alloc_size = 0;
2942  int64_t duration = 0;
2943  int64_t total_sample_count = 0;
2944 
2945  if (c->fc->nb_streams < 1)
2946  return 0;
2947  st = c->fc->streams[c->fc->nb_streams-1];
2948  sc = st->priv_data;
2949 
2950  avio_r8(pb); /* version */
2951  avio_rb24(pb); /* flags */
2952  entries = avio_rb32(pb);
2953 
2954  av_log(c->fc, AV_LOG_TRACE, "track[%u].stts.entries = %u\n",
2955  c->fc->nb_streams-1, entries);
2956 
2957  if (sc->stts_data)
2958  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2959  av_freep(&sc->stts_data);
2960  sc->stts_count = 0;
2961  if (entries >= INT_MAX / sizeof(*sc->stts_data))
2962  return AVERROR(ENOMEM);
2963 
2964  for (i = 0; i < entries && !pb->eof_reached; i++) {
2965  int sample_duration;
2966  unsigned int sample_count;
2967  unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
2968  MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
2969  min_entries * sizeof(*sc->stts_data));
2970  if (!stts_data) {
2971  av_freep(&sc->stts_data);
2972  sc->stts_count = 0;
2973  return AVERROR(ENOMEM);
2974  }
2975  sc->stts_count = min_entries;
2976  sc->stts_data = stts_data;
2977 
2978  sample_count = avio_rb32(pb);
2979  sample_duration = avio_rb32(pb);
2980 
2981  sc->stts_data[i].count= sample_count;
2982  sc->stts_data[i].duration= sample_duration;
2983 
2984  av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2985  sample_count, sample_duration);
2986 
2987  duration+=(int64_t)sample_duration*(uint64_t)sample_count;
2988  total_sample_count+=sample_count;
2989  }
2990 
2991  sc->stts_count = i;
2992 
2993  if (duration > 0 &&
2994  duration <= INT64_MAX - sc->duration_for_fps &&
2995  total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
2996  sc->duration_for_fps += duration;
2997  sc->nb_frames_for_fps += total_sample_count;
2998  }
2999 
3000  if (pb->eof_reached) {
3001  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
3002  return AVERROR_EOF;
3003  }
3004 
3005  st->nb_frames= total_sample_count;
3006  if (duration)
3007  st->duration= FFMIN(st->duration, duration);
3008  sc->track_end = duration;
3009  return 0;
3010 }
3011 
3013 {
3014  AVStream *st;
3015  MOVStreamContext *sc;
3016  int64_t i, entries;
3017 
3018  if (c->fc->nb_streams < 1)
3019  return 0;
3020  st = c->fc->streams[c->fc->nb_streams - 1];
3021  sc = st->priv_data;
3022 
3023  avio_r8(pb); /* version */
3024  avio_rb24(pb); /* flags */
3025  entries = atom.size - 4;
3026 
3027  av_log(c->fc, AV_LOG_TRACE, "track[%u].sdtp.entries = %" PRId64 "\n",
3028  c->fc->nb_streams - 1, entries);
3029 
3030  if (sc->sdtp_data)
3031  av_log(c->fc, AV_LOG_WARNING, "Duplicated SDTP atom\n");
3032  av_freep(&sc->sdtp_data);
3033  sc->sdtp_count = 0;
3034 
3035  sc->sdtp_data = av_malloc(entries);
3036  if (!sc->sdtp_data)
3037  return AVERROR(ENOMEM);
3038 
3039  for (i = 0; i < entries && !pb->eof_reached; i++)
3040  sc->sdtp_data[i] = avio_r8(pb);
3041  sc->sdtp_count = i;
3042 
3043  return 0;
3044 }
3045 
3046 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3047 {
3048  if (duration < 0) {
3049  if (duration == INT_MIN) {
3050  av_log(logctx, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX);
3051  duration++;
3052  }
3053  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3054  }
3055 }
3056 
3058 {
3059  AVStream *st;
3060  MOVStreamContext *sc;
3061  unsigned int i, entries, ctts_count = 0;
3062 
3063  if (c->fc->nb_streams < 1)
3064  return 0;
3065  st = c->fc->streams[c->fc->nb_streams-1];
3066  sc = st->priv_data;
3067 
3068  avio_r8(pb); /* version */
3069  avio_rb24(pb); /* flags */
3070  entries = avio_rb32(pb);
3071 
3072  av_log(c->fc, AV_LOG_TRACE, "track[%u].ctts.entries = %u\n", c->fc->nb_streams - 1, entries);
3073 
3074  if (!entries)
3075  return 0;
3076  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3077  return AVERROR_INVALIDDATA;
3078  av_freep(&sc->ctts_data);
3079  sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3080  if (!sc->ctts_data)
3081  return AVERROR(ENOMEM);
3082 
3083  for (i = 0; i < entries && !pb->eof_reached; i++) {
3084  int count = avio_rb32(pb);
3085  int duration = avio_rb32(pb);
3086 
3087  if (count <= 0) {
3088  av_log(c->fc, AV_LOG_TRACE,
3089  "ignoring CTTS entry with count=%d duration=%d\n",
3090  count, duration);
3091  continue;
3092  }
3093 
3094  add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
3095  count, duration);
3096 
3097  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
3098  count, duration);
3099 
3100  if (FFNABS(duration) < -(1<<28) && i+2<entries) {
3101  av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
3102  av_freep(&sc->ctts_data);
3103  sc->ctts_count = 0;
3104  return 0;
3105  }
3106 
3107  if (i+2<entries)
3108  mov_update_dts_shift(sc, duration, c->fc);
3109  }
3110 
3111  sc->ctts_count = ctts_count;
3112 
3113  if (pb->eof_reached) {
3114  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
3115  return AVERROR_EOF;
3116  }
3117 
3118  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3119 
3120  return 0;
3121 }
3122 
3124 {
3125  AVStream *st;
3126  MOVStreamContext *sc;
3127  unsigned int i, entries;
3128  uint8_t version;
3129  uint32_t grouping_type;
3130 
3131  if (c->fc->nb_streams < 1)
3132  return 0;
3133  st = c->fc->streams[c->fc->nb_streams-1];
3134  sc = st->priv_data;
3135 
3136  version = avio_r8(pb); /* version */
3137  avio_rb24(pb); /* flags */
3138  grouping_type = avio_rl32(pb);
3139  if (grouping_type != MKTAG( 'r','a','p',' '))
3140  return 0; /* only support 'rap ' grouping */
3141  if (version == 1)
3142  avio_rb32(pb); /* grouping_type_parameter */
3143 
3144  entries = avio_rb32(pb);
3145  if (!entries)
3146  return 0;
3147  if (sc->rap_group)
3148  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
3149  av_free(sc->rap_group);
3150  sc->rap_group_count = 0;
3151  sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
3152  if (!sc->rap_group)
3153  return AVERROR(ENOMEM);
3154 
3155  for (i = 0; i < entries && !pb->eof_reached; i++) {
3156  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
3157  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
3158  }
3159 
3160  sc->rap_group_count = i;
3161 
3162  if (pb->eof_reached) {
3163  av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
3164  return AVERROR_EOF;
3165  }
3166 
3167  return 0;
3168 }
3169 
3170 /**
3171  * Get ith edit list entry (media time, duration).
3172  */
3174  const MOVStreamContext *msc,
3175  unsigned int edit_list_index,
3176  int64_t *edit_list_media_time,
3177  int64_t *edit_list_duration,
3178  int64_t global_timescale)
3179 {
3180  if (edit_list_index == msc->elst_count) {
3181  return 0;
3182  }
3183  *edit_list_media_time = msc->elst_data[edit_list_index].time;
3184  *edit_list_duration = msc->elst_data[edit_list_index].duration;
3185 
3186  /* duration is in global timescale units;convert to msc timescale */
3187  if (global_timescale == 0) {
3188  avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
3189  return 0;
3190  }
3191  *edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
3192  global_timescale);
3193  return 1;
3194 }
3195 
3196 /**
3197  * Find the closest previous frame to the timestamp_pts, in e_old index
3198  * entries. Searching for just any frame / just key frames can be controlled by
3199  * last argument 'flag'.
3200  * Note that if ctts_data is not NULL, we will always search for a key frame
3201  * irrespective of the value of 'flag'. If we don't find any keyframe, we will
3202  * return the first frame of the video.
3203  *
3204  * Here the timestamp_pts is considered to be a presentation timestamp and
3205  * the timestamp of index entries are considered to be decoding timestamps.
3206  *
3207  * Returns 0 if successful in finding a frame, else returns -1.
3208  * Places the found index corresponding output arg.
3209  *
3210  * If ctts_old is not NULL, then refines the searched entry by searching
3211  * backwards from the found timestamp, to find the frame with correct PTS.
3212  *
3213  * Places the found ctts_index and ctts_sample in corresponding output args.
3214  */
3216  AVIndexEntry *e_old,
3217  int nb_old,
3218  MOVStts* ctts_data,
3219  int64_t ctts_count,
3220  int64_t timestamp_pts,
3221  int flag,
3222  int64_t* index,
3223  int64_t* ctts_index,
3224  int64_t* ctts_sample)
3225 {
3226  MOVStreamContext *msc = st->priv_data;
3227  AVIndexEntry *e_keep = st->index_entries;
3228  int nb_keep = st->nb_index_entries;
3229  int64_t i = 0;
3230  int64_t index_ctts_count;
3231 
3232  av_assert0(index);
3233 
3234  // If dts_shift > 0, then all the index timestamps will have to be offset by
3235  // at least dts_shift amount to obtain PTS.
3236  // Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
3237  if (msc->dts_shift > 0) {
3238  timestamp_pts -= msc->dts_shift;
3239  }
3240 
3241  st->index_entries = e_old;
3242  st->nb_index_entries = nb_old;
3243  *index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
3244 
3245  // Keep going backwards in the index entries until the timestamp is the same.
3246  if (*index >= 0) {
3247  for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
3248  i--) {
3249  if ((flag & AVSEEK_FLAG_ANY) ||
3250  (e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
3251  *index = i - 1;
3252  }
3253  }
3254  }
3255 
3256  // If we have CTTS then refine the search, by searching backwards over PTS
3257  // computed by adding corresponding CTTS durations to index timestamps.
3258  if (ctts_data && *index >= 0) {
3259  av_assert0(ctts_index);
3260  av_assert0(ctts_sample);
3261  // Find out the ctts_index for the found frame.
3262  *ctts_index = 0;
3263  *ctts_sample = 0;
3264  for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
3265  if (*ctts_index < ctts_count) {
3266  (*ctts_sample)++;
3267  if (ctts_data[*ctts_index].count == *ctts_sample) {
3268  (*ctts_index)++;
3269  *ctts_sample = 0;
3270  }
3271  }
3272  }
3273 
3274  while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
3275  // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
3276  // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
3277  // compensated by dts_shift above.
3278  if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
3279  (e_old[*index].flags & AVINDEX_KEYFRAME)) {
3280  break;
3281  }
3282 
3283  (*index)--;
3284  if (*ctts_sample == 0) {
3285  (*ctts_index)--;
3286  if (*ctts_index >= 0)
3287  *ctts_sample = ctts_data[*ctts_index].count - 1;
3288  } else {
3289  (*ctts_sample)--;
3290  }
3291  }
3292  }
3293 
3294  /* restore AVStream state*/
3295  st->index_entries = e_keep;
3296  st->nb_index_entries = nb_keep;
3297  return *index >= 0 ? 0 : -1;
3298 }
3299 
3300 /**
3301  * Add index entry with the given values, to the end of st->index_entries.
3302  * Returns the new size st->index_entries if successful, else returns -1.
3303  *
3304  * This function is similar to ff_add_index_entry in libavformat/utils.c
3305  * except that here we are always unconditionally adding an index entry to
3306  * the end, instead of searching the entries list and skipping the add if
3307  * there is an existing entry with the same timestamp.
3308  * This is needed because the mov_fix_index calls this func with the same
3309  * unincremented timestamp for successive discarded frames.
3310  */
3311 static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
3312  int size, int distance, int flags)
3313 {
3314  AVIndexEntry *entries, *ie;
3315  int64_t index = -1;
3316  const size_t min_size_needed = (st->nb_index_entries + 1) * sizeof(AVIndexEntry);
3317 
3318  // Double the allocation each time, to lower memory fragmentation.
3319  // Another difference from ff_add_index_entry function.
3320  const size_t requested_size =
3321  min_size_needed > st->index_entries_allocated_size ?
3322  FFMAX(min_size_needed, 2 * st->index_entries_allocated_size) :
3323  min_size_needed;
3324 
3325  if (st->nb_index_entries + 1U >= UINT_MAX / sizeof(AVIndexEntry))
3326  return -1;
3327 
3328  entries = av_fast_realloc(st->index_entries,
3330  requested_size);
3331  if (!entries)
3332  return -1;
3333 
3334  st->index_entries= entries;
3335 
3336  index= st->nb_index_entries++;
3337  ie= &entries[index];
3338 
3339  ie->pos = pos;
3340  ie->timestamp = timestamp;
3341  ie->min_distance= distance;
3342  ie->size= size;
3343  ie->flags = flags;
3344  return index;
3345 }
3346 
3347 /**
3348  * Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
3349  * by subtracting end_ts successively by the amounts given in frame_duration_buffer.
3350  */
3351 static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
3352  int64_t* frame_duration_buffer,
3353  int frame_duration_buffer_size) {
3354  int i = 0;
3355  av_assert0(end_index >= 0 && end_index <= st->nb_index_entries);
3356  for (i = 0; i < frame_duration_buffer_size; i++) {
3357  end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
3358  st->index_entries[end_index - 1 - i].timestamp = end_ts;
3359  }
3360 }
3361 
3362 /**
3363  * Append a new ctts entry to ctts_data.
3364  * Returns the new ctts_count if successful, else returns -1.
3365  */
3366 static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
3367  int count, int duration)
3368 {
3369  MOVStts *ctts_buf_new;
3370  const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
3371  const size_t requested_size =
3372  min_size_needed > *allocated_size ?
3373  FFMAX(min_size_needed, 2 * (*allocated_size)) :
3374  min_size_needed;
3375 
3376  if ((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1)
3377  return -1;
3378 
3379  ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
3380 
3381  if (!ctts_buf_new)
3382  return -1;
3383 
3384  *ctts_data = ctts_buf_new;
3385 
3386  ctts_buf_new[*ctts_count].count = count;
3387  ctts_buf_new[*ctts_count].duration = duration;
3388 
3389  *ctts_count = (*ctts_count) + 1;
3390  return *ctts_count;
3391 }
3392 
3393 #define MAX_REORDER_DELAY 16
3395 {
3396  MOVStreamContext *msc = st->priv_data;
3397  int ind;
3398  int ctts_ind = 0;
3399  int ctts_sample = 0;
3400  int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
3401  int buf_start = 0;
3402  int j, r, num_swaps;
3403 
3404  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
3405  pts_buf[j] = INT64_MIN;
3406 
3407  if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
3409  st->codecpar->video_delay = 0;
3410  for (ind = 0; ind < st->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
3411  // Point j to the last elem of the buffer and insert the current pts there.
3412  j = buf_start;
3413  buf_start = (buf_start + 1);
3414  if (buf_start == MAX_REORDER_DELAY + 1)
3415  buf_start = 0;
3416 
3417  pts_buf[j] = st->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
3418 
3419  // The timestamps that are already in the sorted buffer, and are greater than the
3420  // current pts, are exactly the timestamps that need to be buffered to output PTS
3421  // in correct sorted order.
3422  // Hence the video delay (which is the buffer size used to sort DTS and output PTS),
3423  // can be computed as the maximum no. of swaps any particular timestamp needs to
3424  // go through, to keep this buffer in sorted order.
3425  num_swaps = 0;
3426  while (j != buf_start) {
3427  r = j - 1;
3428  if (r < 0) r = MAX_REORDER_DELAY;
3429  if (pts_buf[j] < pts_buf[r]) {
3430  FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
3431  ++num_swaps;
3432  } else {
3433  break;
3434  }
3435  j = r;
3436  }
3437  st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
3438 
3439  ctts_sample++;
3440  if (ctts_sample == msc->ctts_data[ctts_ind].count) {
3441  ctts_ind++;
3442  ctts_sample = 0;
3443  }
3444  }
3445  av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
3446  st->codecpar->video_delay, st->index);
3447  }
3448 }
3449 
3451 {
3452  sc->current_sample++;
3453  sc->current_index++;
3454  if (sc->index_ranges &&
3455  sc->current_index >= sc->current_index_range->end &&
3456  sc->current_index_range->end) {
3457  sc->current_index_range++;
3459  }
3460 }
3461 
3463 {
3464  sc->current_sample--;
3465  sc->current_index--;
3466  if (sc->index_ranges &&
3468  sc->current_index_range > sc->index_ranges) {
3469  sc->current_index_range--;
3470  sc->current_index = sc->current_index_range->end - 1;
3471  }
3472 }
3473 
3474 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
3475 {
3476  int64_t range_size;
3477 
3478  sc->current_sample = current_sample;
3479  sc->current_index = current_sample;
3480  if (!sc->index_ranges) {
3481  return;
3482  }
3483 
3484  for (sc->current_index_range = sc->index_ranges;
3485  sc->current_index_range->end;
3486  sc->current_index_range++) {
3487  range_size = sc->current_index_range->end - sc->current_index_range->start;
3488  if (range_size > current_sample) {
3489  sc->current_index = sc->current_index_range->start + current_sample;
3490  break;
3491  }
3492  current_sample -= range_size;
3493  }
3494 }
3495 
3496 /**
3497  * Fix st->index_entries, so that it contains only the entries (and the entries
3498  * which are needed to decode them) that fall in the edit list time ranges.
3499  * Also fixes the timestamps of the index entries to match the timeline
3500  * specified the edit lists.
3501  */
3502 static void mov_fix_index(MOVContext *mov, AVStream *st)
3503 {
3504  MOVStreamContext *msc = st->priv_data;
3505  AVIndexEntry *e_old = st->index_entries;
3506  int nb_old = st->nb_index_entries;
3507  const AVIndexEntry *e_old_end = e_old + nb_old;
3508  const AVIndexEntry *current = NULL;
3509  MOVStts *ctts_data_old = msc->ctts_data;
3510  int64_t ctts_index_old = 0;
3511  int64_t ctts_sample_old = 0;
3512  int64_t ctts_count_old = msc->ctts_count;
3513  int64_t edit_list_media_time = 0;
3514  int64_t edit_list_duration = 0;
3515  int64_t frame_duration = 0;
3516  int64_t edit_list_dts_counter = 0;
3517  int64_t edit_list_dts_entry_end = 0;
3518  int64_t edit_list_start_ctts_sample = 0;
3519  int64_t curr_cts;
3520  int64_t curr_ctts = 0;
3521  int64_t empty_edits_sum_duration = 0;
3522  int64_t edit_list_index = 0;
3523  int64_t index;
3524  int flags;
3525  int64_t start_dts = 0;
3526  int64_t edit_list_start_encountered = 0;
3527  int64_t search_timestamp = 0;
3528  int64_t* frame_duration_buffer = NULL;
3529  int num_discarded_begin = 0;
3530  int first_non_zero_audio_edit = -1;
3531  int packet_skip_samples = 0;
3532  MOVIndexRange *current_index_range;
3533  int i;
3534  int found_keyframe_after_edit = 0;
3535  int found_non_empty_edit = 0;
3536 
3537  if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
3538  return;
3539  }
3540 
3541  // allocate the index ranges array
3542  msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
3543  if (!msc->index_ranges) {
3544  av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
3545  return;
3546  }
3547  msc->current_index_range = msc->index_ranges;
3548  current_index_range = msc->index_ranges - 1;
3549 
3550  // Clean AVStream from traces of old index
3551  st->index_entries = NULL;
3553  st->nb_index_entries = 0;
3554 
3555  // Clean ctts fields of MOVStreamContext
3556  msc->ctts_data = NULL;
3557  msc->ctts_count = 0;
3558  msc->ctts_index = 0;
3559  msc->ctts_sample = 0;
3560  msc->ctts_allocated_size = 0;
3561 
3562  // Reinitialize min_corrected_pts so that it can be computed again.
3563  msc->min_corrected_pts = -1;
3564 
3565  // If the dts_shift is positive (in case of negative ctts values in mov),
3566  // then negate the DTS by dts_shift
3567  if (msc->dts_shift > 0) {
3568  edit_list_dts_entry_end -= msc->dts_shift;
3569  av_log(mov->fc, AV_LOG_DEBUG, "Shifting DTS by %d because of negative CTTS.\n", msc->dts_shift);
3570  }
3571 
3572  start_dts = edit_list_dts_entry_end;
3573 
3574  while (get_edit_list_entry(mov, msc, edit_list_index, &edit_list_media_time,
3575  &edit_list_duration, mov->time_scale)) {
3576  av_log(mov->fc, AV_LOG_DEBUG, "Processing st: %d, edit list %"PRId64" - media time: %"PRId64", duration: %"PRId64"\n",
3577  st->index, edit_list_index, edit_list_media_time, edit_list_duration);
3578  edit_list_index++;
3579  edit_list_dts_counter = edit_list_dts_entry_end;
3580  edit_list_dts_entry_end += edit_list_duration;
3581  num_discarded_begin = 0;
3582  if (!found_non_empty_edit && edit_list_media_time == -1) {
3583  empty_edits_sum_duration += edit_list_duration;
3584  continue;
3585  }
3586  found_non_empty_edit = 1;
3587 
3588  // If we encounter a non-negative edit list reset the skip_samples/start_pad fields and set them
3589  // according to the edit list below.
3590  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3591  if (first_non_zero_audio_edit < 0) {
3592  first_non_zero_audio_edit = 1;
3593  } else {
3594  first_non_zero_audio_edit = 0;
3595  }
3596 
3597  if (first_non_zero_audio_edit > 0)
3598  st->internal->skip_samples = msc->start_pad = 0;
3599  }
3600 
3601  // While reordering frame index according to edit list we must handle properly
3602  // the scenario when edit list entry starts from none key frame.
3603  // We find closest previous key frame and preserve it and consequent frames in index.
3604  // All frames which are outside edit list entry time boundaries will be dropped after decoding.
3605  search_timestamp = edit_list_media_time;
3606  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3607  // Audio decoders like AAC need need a decoder delay samples previous to the current sample,
3608  // to correctly decode this frame. Hence for audio we seek to a frame 1 sec. before the
3609  // edit_list_media_time to cover the decoder delay.
3610  search_timestamp = FFMAX(search_timestamp - msc->time_scale, e_old[0].timestamp);
3611  }
3612 
3613  if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, 0,
3614  &index, &ctts_index_old, &ctts_sample_old) < 0) {
3615  av_log(mov->fc, AV_LOG_WARNING,
3616  "st: %d edit list: %"PRId64" Missing key frame while searching for timestamp: %"PRId64"\n",
3617  st->index, edit_list_index, search_timestamp);
3618  if (find_prev_closest_index(st, e_old, nb_old, ctts_data_old, ctts_count_old, search_timestamp, AVSEEK_FLAG_ANY,
3619  &index, &ctts_index_old, &ctts_sample_old) < 0) {
3620  av_log(mov->fc, AV_LOG_WARNING,
3621  "st: %d edit list %"PRId64" Cannot find an index entry before timestamp: %"PRId64".\n",
3622  st->index, edit_list_index, search_timestamp);
3623  index = 0;
3624  ctts_index_old = 0;
3625  ctts_sample_old = 0;
3626  }
3627  }
3628  current = e_old + index;
3629  edit_list_start_ctts_sample = ctts_sample_old;
3630 
3631  // Iterate over index and arrange it according to edit list
3632  edit_list_start_encountered = 0;
3633  found_keyframe_after_edit = 0;
3634  for (; current < e_old_end; current++, index++) {
3635  // check if frame outside edit list mark it for discard
3636  frame_duration = (current + 1 < e_old_end) ?
3637  ((current + 1)->timestamp - current->timestamp) : edit_list_duration;
3638 
3639  flags = current->flags;
3640 
3641  // frames (pts) before or after edit list
3642  curr_cts = current->timestamp + msc->dts_shift;
3643  curr_ctts = 0;
3644 
3645  if (ctts_data_old && ctts_index_old < ctts_count_old) {
3646  curr_ctts = ctts_data_old[ctts_index_old].duration;
3647  av_log(mov->fc, AV_LOG_DEBUG, "stts: %"PRId64" ctts: %"PRId64", ctts_index: %"PRId64", ctts_count: %"PRId64"\n",
3648  curr_cts, curr_ctts, ctts_index_old, ctts_count_old);
3649  curr_cts += curr_ctts;
3650  ctts_sample_old++;
3651  if (ctts_sample_old == ctts_data_old[ctts_index_old].count) {
3652  if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3653  &msc->ctts_allocated_size,
3654  ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3655  ctts_data_old[ctts_index_old].duration) == -1) {
3656  av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3657  ctts_index_old,
3658  ctts_data_old[ctts_index_old].count - edit_list_start_ctts_sample,
3659  ctts_data_old[ctts_index_old].duration);
3660  break;
3661  }
3662  ctts_index_old++;
3663  ctts_sample_old = 0;
3664  edit_list_start_ctts_sample = 0;
3665  }
3666  }
3667 
3668  if (curr_cts < edit_list_media_time || curr_cts >= (edit_list_duration + edit_list_media_time)) {
3670  curr_cts < edit_list_media_time && curr_cts + frame_duration > edit_list_media_time &&
3671  first_non_zero_audio_edit > 0) {
3672  packet_skip_samples = edit_list_media_time - curr_cts;
3673  st->internal->skip_samples += packet_skip_samples;
3674 
3675  // Shift the index entry timestamp by packet_skip_samples to be correct.
3676  edit_list_dts_counter -= packet_skip_samples;
3677  if (edit_list_start_encountered == 0) {
3678  edit_list_start_encountered = 1;
3679  // Make timestamps strictly monotonically increasing for audio, by rewriting timestamps for
3680  // discarded packets.
3681  if (frame_duration_buffer) {
3682  fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3683  frame_duration_buffer, num_discarded_begin);
3684  av_freep(&frame_duration_buffer);
3685  }
3686  }
3687 
3688  av_log(mov->fc, AV_LOG_DEBUG, "skip %d audio samples from curr_cts: %"PRId64"\n", packet_skip_samples, curr_cts);
3689  } else {
3691  av_log(mov->fc, AV_LOG_DEBUG, "drop a frame at curr_cts: %"PRId64" @ %"PRId64"\n", curr_cts, index);
3692 
3693  if (edit_list_start_encountered == 0) {
3694  num_discarded_begin++;
3695  frame_duration_buffer = av_realloc(frame_duration_buffer,
3696  num_discarded_begin * sizeof(int64_t));
3697  if (!frame_duration_buffer) {
3698  av_log(mov->fc, AV_LOG_ERROR, "Cannot reallocate frame duration buffer\n");
3699  break;
3700  }
3701  frame_duration_buffer[num_discarded_begin - 1] = frame_duration;
3702 
3703  // Increment skip_samples for the first non-zero audio edit list
3704  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3705  first_non_zero_audio_edit > 0 && st->codecpar->codec_id != AV_CODEC_ID_VORBIS) {
3706  st->internal->skip_samples += frame_duration;
3707  }
3708  }
3709  }
3710  } else {
3711  if (msc->min_corrected_pts < 0) {
3712  msc->min_corrected_pts = edit_list_dts_counter + curr_ctts + msc->dts_shift;
3713  } else {
3714  msc->min_corrected_pts = FFMIN(msc->min_corrected_pts, edit_list_dts_counter + curr_ctts + msc->dts_shift);
3715  }
3716  if (edit_list_start_encountered == 0) {
3717  edit_list_start_encountered = 1;
3718  // Make timestamps strictly monotonically increasing by rewriting timestamps for
3719  // discarded packets.
3720  if (frame_duration_buffer) {
3721  fix_index_entry_timestamps(st, st->nb_index_entries, edit_list_dts_counter,
3722  frame_duration_buffer, num_discarded_begin);
3723  av_freep(&frame_duration_buffer);
3724  }
3725  }
3726  }
3727 
3728  if (add_index_entry(st, current->pos, edit_list_dts_counter, current->size,
3729  current->min_distance, flags) == -1) {
3730  av_log(mov->fc, AV_LOG_ERROR, "Cannot add index entry\n");
3731  break;
3732  }
3733 
3734  // Update the index ranges array
3735  if (current_index_range < msc->index_ranges || index != current_index_range->end) {
3736  current_index_range++;
3737  current_index_range->start = index;
3738  }
3739  current_index_range->end = index + 1;
3740 
3741  // Only start incrementing DTS in frame_duration amounts, when we encounter a frame in edit list.
3742  if (edit_list_start_encountered > 0) {
3743  edit_list_dts_counter = edit_list_dts_counter + frame_duration;
3744  }
3745 
3746  // Break when found first key frame after edit entry completion
3747  if ((curr_cts + frame_duration >= (edit_list_duration + edit_list_media_time)) &&
3749  if (ctts_data_old) {
3750  // If we have CTTS and this is the first keyframe after edit elist,
3751  // wait for one more, because there might be trailing B-frames after this I-frame
3752  // that do belong to the edit.
3753  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO && found_keyframe_after_edit == 0) {
3754  found_keyframe_after_edit = 1;
3755  continue;
3756  }
3757  if (ctts_sample_old != 0) {
3758  if (add_ctts_entry(&msc->ctts_data, &msc->ctts_count,
3759  &msc->ctts_allocated_size,
3760  ctts_sample_old - edit_list_start_ctts_sample,
3761  ctts_data_old[ctts_index_old].duration) == -1) {
3762  av_log(mov->fc, AV_LOG_ERROR, "Cannot add CTTS entry %"PRId64" - {%"PRId64", %d}\n",
3763  ctts_index_old, ctts_sample_old - edit_list_start_ctts_sample,
3764  ctts_data_old[ctts_index_old].duration);
3765  break;
3766  }
3767  }
3768  }
3769  break;
3770  }
3771  }
3772  }
3773  // If there are empty edits, then msc->min_corrected_pts might be positive
3774  // intentionally. So we subtract the sum duration of emtpy edits here.
3775  msc->min_corrected_pts -= empty_edits_sum_duration;
3776 
3777  // If the minimum pts turns out to be greater than zero after fixing the index, then we subtract the
3778  // dts by that amount to make the first pts zero.
3779  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3780  if (msc->min_corrected_pts > 0) {
3781  av_log(mov->fc, AV_LOG_DEBUG, "Offset DTS by %"PRId64" to make first pts zero.\n", msc->min_corrected_pts);
3782  for (i = 0; i < st->nb_index_entries; ++i) {
3784  }
3785  }
3786  }
3787  // Start time should be equal to zero or the duration of any empty edits.
3788  st->start_time = empty_edits_sum_duration;
3789 
3790  // Update av stream length, if it ends up shorter than the track's media duration
3791  st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
3792  msc->start_pad = st->internal->skip_samples;
3793 
3794  // Free the old index and the old CTTS structures
3795  av_free(e_old);
3796  av_free(ctts_data_old);
3797  av_freep(&frame_duration_buffer);
3798 
3799  // Null terminate the index ranges array
3800  current_index_range++;
3801  current_index_range->start = 0;
3802  current_index_range->end = 0;
3803  msc->current_index = msc->index_ranges[0].start;
3804 }
3805 
3806 static void mov_build_index(MOVContext *mov, AVStream *st)
3807 {
3808  MOVStreamContext *sc = st->priv_data;
3809  int64_t current_offset;
3810  int64_t current_dts = 0;
3811  unsigned int stts_index = 0;
3812  unsigned int stsc_index = 0;
3813  unsigned int stss_index = 0;
3814  unsigned int stps_index = 0;
3815  unsigned int i, j;
3816  uint64_t stream_size = 0;
3817  MOVStts *ctts_data_old = sc->ctts_data;
3818  unsigned int ctts_count_old = sc->ctts_count;
3819 
3820  if (sc->elst_count) {
3821  int i, edit_start_index = 0, multiple_edits = 0;
3822  int64_t empty_duration = 0; // empty duration of the first edit list entry
3823  int64_t start_time = 0; // start time of the media
3824 
3825  for (i = 0; i < sc->elst_count; i++) {
3826  const MOVElst *e = &sc->elst_data[i];
3827  if (i == 0 && e->time == -1) {
3828  /* if empty, the first entry is the start time of the stream
3829  * relative to the presentation itself */
3830  empty_duration = e->duration;
3831  edit_start_index = 1;
3832  } else if (i == edit_start_index && e->time >= 0) {
3833  start_time = e->time;
3834  } else {
3835  multiple_edits = 1;
3836  }
3837  }
3838 
3839  if (multiple_edits && !mov->advanced_editlist)
3840  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
3841  "Use -advanced_editlist to correctly decode otherwise "
3842  "a/v desync might occur\n");
3843 
3844  /* adjust first dts according to edit list */
3845  if ((empty_duration || start_time) && mov->time_scale > 0) {
3846  if (empty_duration)
3847  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
3848 
3849  if (av_sat_sub64(start_time, empty_duration) != start_time - (uint64_t)empty_duration)
3850  av_log(mov->fc, AV_LOG_WARNING, "start_time - empty_duration is not representable\n");
3851 
3852  sc->time_offset = start_time - (uint64_t)empty_duration;
3854  if (!mov->advanced_editlist)
3855  current_dts = -sc->time_offset;
3856  }
3857 
3858  if (!multiple_edits && !mov->advanced_editlist &&
3860  sc->start_pad = start_time;
3861  }
3862 
3863  /* only use old uncompressed audio chunk demuxing when stts specifies it */
3864  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
3865  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
3866  unsigned int current_sample = 0;
3867  unsigned int stts_sample = 0;
3868  unsigned int sample_size;
3869  unsigned int distance = 0;
3870  unsigned int rap_group_index = 0;
3871  unsigned int rap_group_sample = 0;
3872  int64_t last_dts = 0;
3873  int64_t dts_correction = 0;
3874  int rap_group_present = sc->rap_group_count && sc->rap_group;
3875  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
3876 
3877  current_dts -= sc->dts_shift;
3878  last_dts = current_dts;
3879 
3880  if (!sc->sample_count || st->nb_index_entries)
3881  return;
3882  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
3883  return;
3885  st->nb_index_entries + sc->sample_count,
3886  sizeof(*st->index_entries)) < 0) {
3887  st->nb_index_entries = 0;
3888  return;
3889  }
3891 
3892  if (ctts_data_old) {
3893  // Expand ctts entries such that we have a 1-1 mapping with samples
3894  if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
3895  return;
3896  sc->ctts_count = 0;
3897  sc->ctts_allocated_size = 0;
3899  sc->sample_count * sizeof(*sc->ctts_data));
3900  if (!sc->ctts_data) {
3901  av_free(ctts_data_old);
3902  return;
3903  }
3904 
3905  memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
3906 
3907  for (i = 0; i < ctts_count_old &&
3908  sc->ctts_count < sc->sample_count; i++)
3909  for (j = 0; j < ctts_data_old[i].count &&
3910  sc->ctts_count < sc->sample_count; j++)
3911  add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
3912  &sc->ctts_allocated_size, 1,
3913  ctts_data_old[i].duration);
3914  av_free(ctts_data_old);
3915  }
3916 
3917  for (i = 0; i < sc->chunk_count; i++) {
3918  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
3919  current_offset = sc->chunk_offsets[i];
3920  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
3921  i + 1 == sc->stsc_data[stsc_index + 1].first)
3922  stsc_index++;
3923 
3924  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
3925  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
3926  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
3927  sc->stsz_sample_size = sc->sample_size;
3928  }
3929  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
3930  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
3931  sc->stsz_sample_size = sc->sample_size;
3932  }
3933 
3934  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
3935  int keyframe = 0;
3936  if (current_sample >= sc->sample_count) {
3937  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
3938  return;
3939  }
3940 
3941  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
3942  keyframe = 1;
3943  if (stss_index + 1 < sc->keyframe_count)
3944  stss_index++;
3945  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
3946  keyframe = 1;
3947  if (stps_index + 1 < sc->stps_count)
3948  stps_index++;
3949  }
3950  if (rap_group_present && rap_group_index < sc->rap_group_count) {
3951  if (sc->rap_group[rap_group_index].index > 0)
3952  keyframe = 1;
3953  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
3954  rap_group_sample = 0;
3955  rap_group_index++;
3956  }
3957  }
3958  if (sc->keyframe_absent
3959  && !sc->stps_count
3960  && !rap_group_present
3961  && (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
3962  keyframe = 1;
3963  if (keyframe)
3964  distance = 0;
3965  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
3966  if (sc->pseudo_stream_id == -1 ||
3967  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
3968  AVIndexEntry *e;
3969  if (sample_size > 0x3FFFFFFF) {
3970  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
3971  return;
3972  }
3973  e = &st->index_entries[st->nb_index_entries++];
3974  e->pos = current_offset;
3975  e->timestamp = current_dts;
3976  e->size = sample_size;
3977  e->min_distance = distance;
3978  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
3979  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %u, offset %"PRIx64", dts %"PRId64", "
3980  "size %u, distance %u, keyframe %d\n", st->index, current_sample,
3981  current_offset, current_dts, sample_size, distance, keyframe);
3982  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
3983  ff_rfps_add_frame(mov->fc, st, current_dts);
3984  }
3985 
3986  current_offset += sample_size;
3987  stream_size += sample_size;
3988 
3989  /* A negative sample duration is invalid based on the spec,
3990  * but some samples need it to correct the DTS. */
3991  if (sc->stts_data[stts_index].duration < 0) {
3992  av_log(mov->fc, AV_LOG_WARNING,
3993  "Invalid SampleDelta %d in STTS, at %d st:%d\n",
3994  sc->stts_data[stts_index].duration, stts_index,
3995  st->index);
3996  dts_correction += sc->stts_data[stts_index].duration - 1;
3997  sc->stts_data[stts_index].duration = 1;
3998  }
3999  current_dts += sc->stts_data[stts_index].duration;
4000  if (!dts_correction || current_dts + dts_correction > last_dts) {
4001  current_dts += dts_correction;
4002  dts_correction = 0;
4003  } else {
4004  /* Avoid creating non-monotonous DTS */
4005  dts_correction += current_dts - last_dts - 1;
4006  current_dts = last_dts + 1;
4007  }
4008  last_dts = current_dts;
4009  distance++;
4010  stts_sample++;
4011  current_sample++;
4012  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
4013  stts_sample = 0;
4014  stts_index++;
4015  }
4016  }
4017  }
4018  if (st->duration > 0)
4019  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
4020  } else {
4021  unsigned chunk_samples, total = 0;
4022 
4023  if (!sc->chunk_count)
4024  return;
4025 
4026  // compute total chunk count
4027  for (i = 0; i < sc->stsc_count; i++) {
4028  unsigned count, chunk_count;
4029 
4030  chunk_samples = sc->stsc_data[i].count;
4031  if (i != sc->stsc_count - 1 &&
4032  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
4033  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
4034  return;
4035  }
4036 
4037  if (sc->samples_per_frame >= 160) { // gsm
4038  count = chunk_samples / sc->samples_per_frame;
4039  } else if (sc->samples_per_frame > 1) {
4040  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
4041  count = (chunk_samples+samples-1) / samples;
4042  } else {
4043  count = (chunk_samples+1023) / 1024;
4044  }
4045 
4046  if (mov_stsc_index_valid(i, sc->stsc_count))
4047  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
4048  else
4049  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
4050  total += chunk_count * count;
4051  }
4052 
4053  av_log(mov->fc, AV_LOG_TRACE, "chunk count %u\n", total);
4054  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
4055  return;
4057  st->nb_index_entries + total,
4058  sizeof(*st->index_entries)) < 0) {
4059  st->nb_index_entries = 0;
4060  return;
4061  }
4062  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
4063 
4064  // populate index
4065  for (i = 0; i < sc->chunk_count; i++) {
4066  current_offset = sc->chunk_offsets[i];
4067  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4068  i + 1 == sc->stsc_data[stsc_index + 1].first)
4069  stsc_index++;
4070  chunk_samples = sc->stsc_data[stsc_index].count;
4071 
4072  while (chunk_samples > 0) {
4073  AVIndexEntry *e;
4074  unsigned size, samples;
4075 
4076  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
4078  "Zero bytes per frame, but %d samples per frame",
4079  sc->samples_per_frame);
4080  return;
4081  }
4082 
4083  if (sc->samples_per_frame >= 160) { // gsm
4084  samples = sc->samples_per_frame;
4085  size = sc->bytes_per_frame;
4086  } else {
4087  if (sc->samples_per_frame > 1) {
4088  samples = FFMIN((1024 / sc->samples_per_frame)*
4089  sc->samples_per_frame, chunk_samples);
4091  } else {
4092  samples = FFMIN(1024, chunk_samples);
4093  size = samples * sc->sample_size;
4094  }
4095  }
4096 
4097  if (st->nb_index_entries >= total) {
4098  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %u\n", total);
4099  return;
4100  }
4101  if (size > 0x3FFFFFFF) {
4102  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
4103  return;
4104  }
4105  e = &st->index_entries[st->nb_index_entries++];
4106  e->pos = current_offset;
4107  e->timestamp = current_dts;
4108  e->size = size;
4109  e->min_distance = 0;
4110  e->flags = AVINDEX_KEYFRAME;
4111  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %u, offset %"PRIx64", dts %"PRId64", "
4112  "size %u, duration %u\n", st->index, i, current_offset, current_dts,
4113  size, samples);
4114 
4115  current_offset += size;
4116  current_dts += samples;
4117  chunk_samples -= samples;
4118  }
4119  }
4120  }
4121 
4122  if (!mov->ignore_editlist && mov->advanced_editlist) {
4123  // Fix index according to edit lists.
4124  mov_fix_index(mov, st);
4125  }
4126 
4127  // Update start time of the stream.
4129  st->start_time = st->index_entries[0].timestamp + sc->dts_shift;
4130  if (sc->ctts_data) {
4131  st->start_time += sc->ctts_data[0].duration;
4132  }
4133  }
4134 
4135  mov_estimate_video_delay(mov, st);
4136 }
4137 
4138 static int test_same_origin(const char *src, const char *ref) {
4139  char src_proto[64];
4140  char ref_proto[64];
4141  char src_auth[256];
4142  char ref_auth[256];
4143  char src_host[256];
4144  char ref_host[256];
4145  int src_port=-1;
4146  int ref_port=-1;
4147 
4148  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
4149  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
4150 
4151  if (strlen(src) == 0) {
4152  return -1;
4153  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
4154  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
4155  strlen(src_host) + 1 >= sizeof(src_host) ||
4156  strlen(ref_host) + 1 >= sizeof(ref_host)) {
4157  return 0;
4158  } else if (strcmp(src_proto, ref_proto) ||
4159  strcmp(src_auth, ref_auth) ||
4160  strcmp(src_host, ref_host) ||
4161  src_port != ref_port) {
4162  return 0;
4163  } else
4164  return 1;
4165 }
4166 
4167 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref)
4168 {
4169  /* try relative path, we do not try the absolute because it can leak information about our
4170  system to an attacker */
4171  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
4172  char filename[1025];
4173  const char *src_path;
4174  int i, l;
4175 
4176  /* find a source dir */
4177  src_path = strrchr(src, '/');
4178  if (src_path)
4179  src_path++;
4180  else
4181  src_path = src;
4182 
4183  /* find a next level down to target */
4184  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
4185  if (ref->path[l] == '/') {
4186  if (i == ref->nlvl_to - 1)
4187  break;
4188  else
4189  i++;
4190  }
4191 
4192  /* compose filename if next level down to target was found */
4193  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
4194  memcpy(filename, src, src_path - src);
4195  filename[src_path - src] = 0;
4196 
4197  for (i = 1; i < ref->nlvl_from; i++)
4198  av_strlcat(filename, "../", sizeof(filename));
4199 
4200  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
4201  if (!c->use_absolute_path) {
4202  int same_origin = test_same_origin(src, filename);
4203 
4204  if (!same_origin) {
4205  av_log(c->fc, AV_LOG_ERROR,
4206  "Reference with mismatching origin, %s not tried for security reasons, "
4207  "set demuxer option use_absolute_path to allow it anyway\n",
4208  ref->path);
4209  return AVERROR(ENOENT);
4210  }
4211 
4212  if (strstr(ref->path + l + 1, "..") ||
4213  strstr(ref->path + l + 1, ":") ||
4214  (ref->nlvl_from > 1 && same_origin < 0) ||
4215  (filename[0] == '/' && src_path == src))
4216  return AVERROR(ENOENT);
4217  }
4218 
4219  if (strlen(filename) + 1 == sizeof(filename))
4220  return AVERROR(ENOENT);
4221  if (!c->fc->io_open(c->fc, pb, filename, AVIO_FLAG_READ, NULL))
4222  return 0;
4223  }
4224  } else if (c->use_absolute_path) {
4225  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
4226  "this is a possible security issue\n");
4227  if (!c->fc->io_open(c->fc, pb, ref->path, AVIO_FLAG_READ, NULL))
4228  return 0;
4229  } else {
4230  av_log(c->fc, AV_LOG_ERROR,
4231  "Absolute path %s not tried for security reasons, "
4232  "set demuxer option use_absolute_path to allow absolute paths\n",
4233  ref->path);
4234  }
4235 
4236  return AVERROR(ENOENT);
4237 }
4238 
4240 {
4241  if (sc->time_scale <= 0) {
4242  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
4243  sc->time_scale = c->time_scale;
4244  if (sc->time_scale <= 0)
4245  sc->time_scale = 1;
4246  }
4247 }
4248 
4250 {
4251  AVStream *st;
4252  MOVStreamContext *sc;
4253  int ret;
4254 
4255  st = avformat_new_stream(c->fc, NULL);
4256  if (!st) return AVERROR(ENOMEM);
4257  st->id = -1;
4258  sc = av_mallocz(sizeof(MOVStreamContext));
4259  if (!sc) return AVERROR(ENOMEM);
4260 
4261  st->priv_data = sc;
4263  sc->ffindex = st->index;
4264  c->trak_index = st->index;
4265 
4266  if ((ret = mov_read_default(c, pb, atom)) < 0)
4267  return ret;
4268 
4269  c->trak_index = -1;
4270 
4271  // Here stsc refers to a chunk not described in stco. This is technically invalid,
4272  // but we can overlook it (clearing stsc) whenever stts_count == 0 (indicating no samples).
4273  if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
4274  sc->stsc_count = 0;
4275  av_freep(&sc->stsc_data);
4276  }
4277 
4278  /* sanity checks */
4279  if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
4280  (!sc->sample_size && !sc->sample_count))) ||
4281  (!sc->chunk_count && sc->sample_count)) {
4282  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
4283  st->index);
4284  return 0;
4285  }
4286  if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
4287  av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
4288  st->index);
4289  return AVERROR_INVALIDDATA;
4290  }
4291 
4292  fix_timescale(c, sc);
4293 
4294  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
4295 
4296  mov_build_index(c, st);
4297 
4298  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
4299  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
4300  if (c->enable_drefs) {
4301  if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
4302  av_log(c->fc, AV_LOG_ERROR,
4303  "stream %d, error opening alias: path='%s', dir='%s', "
4304  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
4305  st->index, dref->path, dref->dir, dref->filename,
4306  dref->volume, dref->nlvl_from, dref->nlvl_to);
4307  } else {
4308  av_log(c->fc, AV_LOG_WARNING,
4309  "Skipped opening external track: "
4310  "stream %d, alias: path='%s', dir='%s', "
4311  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
4312  "Set enable_drefs to allow this.\n",
4313  st->index, dref->path, dref->dir, dref->filename,
4314  dref->volume, dref->nlvl_from, dref->nlvl_to);
4315  }
4316  } else {
4317  sc->pb = c->fc->pb;
4318  sc->pb_is_copied = 1;
4319  }
4320 
4321  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
4322  if (!st->sample_aspect_ratio.num && st->codecpar->width && st->codecpar->height &&
4323  sc->height && sc->width &&
4324  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
4325  st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
4326  ((double)st->codecpar->width * sc->height), INT_MAX);
4327  }
4328 
4329 #if FF_API_R_FRAME_RATE
4330  if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
4332  sc->time_scale, sc->stts_data[0].duration, INT_MAX);
4333 #endif
4334  }
4335 
4336  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
4337  if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
4338  TAG_IS_AVCI(st->codecpar->codec_tag)) {
4340  if (ret < 0)
4341  return ret;
4342  }
4343 
4344  switch (st->codecpar->codec_id) {
4345 #if CONFIG_H261_DECODER
4346  case AV_CODEC_ID_H261:
4347 #endif
4348 #if CONFIG_H263_DECODER
4349  case AV_CODEC_ID_H263:
4350 #endif
4351 #if CONFIG_MPEG4_DECODER
4352  case AV_CODEC_ID_MPEG4:
4353 #endif
4354  st->codecpar->width = 0; /* let decoder init width/height */
4355  st->codecpar->height= 0;
4356  break;
4357  }
4358 
4359  // If the duration of the mp3 packets is not constant, then they could need a parser
4360  if (st->codecpar->codec_id == AV_CODEC_ID_MP3
4361  && sc->stts_count > 3
4362  && sc->stts_count*10 > st->nb_frames
4363  && sc->time_scale == st->codecpar->sample_rate) {
4365  }
4366  /* Do not need those anymore. */
4367  av_freep(&sc->chunk_offsets);
4368  av_freep(&sc->sample_sizes);
4369  av_freep(&sc->keyframes);
4370  av_freep(&sc->stts_data);
4371  av_freep(&sc->stps_data);
4372  av_freep(&sc->elst_data);
4373  av_freep(&sc->rap_group);
4374 
4375  return 0;
4376 }
4377 
4379 {
4380  int ret;
4381  c->itunes_metadata = 1;
4382  ret = mov_read_default(c, pb, atom);
4383  c->itunes_metadata = 0;
4384  return ret;
4385 }
4386 
4388 {
4389  uint32_t count;
4390  uint32_t i;
4391 
4392  if (atom.size < 8)
4393  return 0;
4394 
4395  avio_skip(pb, 4);
4396  count = avio_rb32(pb);
4397  if (count > UINT_MAX / sizeof(*c->meta_keys) - 1) {
4398  av_log(c->fc, AV_LOG_ERROR,
4399  "The 'keys' atom with the invalid key count: %"PRIu32"\n", count);
4400  return AVERROR_INVALIDDATA;
4401  }
4402 
4403  c->meta_keys_count = count + 1;
4404  c->meta_keys = av_mallocz(c->meta_keys_count * sizeof(*c->meta_keys));
4405  if (!c->meta_keys)
4406  return AVERROR(ENOMEM);
4407 
4408  for (i = 1; i <= count; ++i) {
4409  uint32_t key_size = avio_rb32(pb);
4410  uint32_t type = avio_rl32(pb);
4411  if (key_size < 8) {
4412  av_log(c->fc, AV_LOG_ERROR,
4413  "The key# %"PRIu32" in meta has invalid size:"
4414  "%"PRIu32"\n", i, key_size);
4415  return AVERROR_INVALIDDATA;
4416  }
4417  key_size -= 8;
4418  if (type != MKTAG('m','d','t','a')) {
4419  avio_skip(pb, key_size);
4420  }
4421  c->meta_keys[i] = av_mallocz(key_size + 1);
4422  if (!c->meta_keys[i])
4423  return AVERROR(ENOMEM);
4424  avio_read(pb, c->meta_keys[i], key_size);
4425  }
4426 
4427  return 0;
4428 }
4429 
4431 {
4432  int64_t end = av_sat_add64(avio_tell(pb), atom.size);
4433  uint8_t *key = NULL, *val = NULL, *mean = NULL;
4434  int i;
4435  int ret = 0;
4436  AVStream *st;
4437  MOVStreamContext *sc;
4438 
4439  if (c->fc->nb_streams < 1)
4440  return 0;
4441  st = c->fc->streams[c->fc->nb_streams-1];