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