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