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