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