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