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