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