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