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