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