FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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/avstring.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/display.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/aes.h"
42 #include "libavutil/sha.h"
43 #include "libavutil/timecode.h"
44 #include "libavcodec/ac3tab.h"
45 #include "avformat.h"
46 #include "internal.h"
47 #include "avio_internal.h"
48 #include "riff.h"
49 #include "isom.h"
50 #include "libavcodec/get_bits.h"
51 #include "id3v1.h"
52 #include "mov_chan.h"
53 #include "replaygain.h"
54 
55 #if CONFIG_ZLIB
56 #include <zlib.h>
57 #endif
58 
59 #include "qtpalette.h"
60 
61 /* those functions parse an atom */
62 /* links atom IDs to parse functions */
63 typedef struct MOVParseTableEntry {
64  uint32_t type;
65  int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
67 
68 static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
69 static int mov_read_mfra(MOVContext *c, AVIOContext *f);
70 
72  unsigned len, const char *key)
73 {
74  char buf[16];
75 
76  short current, total = 0;
77  avio_rb16(pb); // unknown
78  current = avio_rb16(pb);
79  if (len >= 6)
80  total = avio_rb16(pb);
81  if (!total)
82  snprintf(buf, sizeof(buf), "%d", current);
83  else
84  snprintf(buf, sizeof(buf), "%d/%d", current, total);
86  av_dict_set(&c->fc->metadata, key, buf, 0);
87 
88  return 0;
89 }
90 
92  unsigned len, const char *key)
93 {
94  /* bypass padding bytes */
95  avio_r8(pb);
96  avio_r8(pb);
97  avio_r8(pb);
98 
100  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
101 
102  return 0;
103 }
104 
106  unsigned len, const char *key)
107 {
109  av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0);
110 
111  return 0;
112 }
113 
115  unsigned len, const char *key)
116 {
117  short genre;
118 
119  avio_r8(pb); // unknown
120 
121  genre = avio_r8(pb);
122  if (genre < 1 || genre > ID3v1_GENRE_MAX)
123  return 0;
125  av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0);
126 
127  return 0;
128 }
129 
130 static const uint32_t mac_to_unicode[128] = {
131  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
132  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
133  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
134  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
135  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
136  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
137  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
138  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
139  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
140  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
141  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
142  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
143  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
144  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
145  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
146  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
147 };
148 
150  char *dst, int dstlen)
151 {
152  char *p = dst;
153  char *end = dst+dstlen-1;
154  int i;
155 
156  for (i = 0; i < len; i++) {
157  uint8_t t, c = avio_r8(pb);
158  if (c < 0x80 && p < end)
159  *p++ = c;
160  else if (p < end)
161  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
162  }
163  *p = 0;
164  return p - dst;
165 }
166 
167 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
168 {
169  AVPacket pkt;
170  AVStream *st;
171  MOVStreamContext *sc;
172  enum AVCodecID id;
173  int ret;
174 
175  switch (type) {
176  case 0xd: id = AV_CODEC_ID_MJPEG; break;
177  case 0xe: id = AV_CODEC_ID_PNG; break;
178  case 0x1b: id = AV_CODEC_ID_BMP; break;
179  default:
180  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
181  avio_skip(pb, len);
182  return 0;
183  }
184 
185  st = avformat_new_stream(c->fc, NULL);
186  if (!st)
187  return AVERROR(ENOMEM);
188  sc = av_mallocz(sizeof(*sc));
189  if (!sc)
190  return AVERROR(ENOMEM);
191  st->priv_data = sc;
192 
193  ret = av_get_packet(pb, &pkt, len);
194  if (ret < 0)
195  return ret;
196 
198 
199  st->attached_pic = pkt;
200  st->attached_pic.stream_index = st->index;
202 
204  st->codec->codec_id = id;
205 
206  return 0;
207 }
208 
209 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
210 {
211  char language[4] = { 0 };
212  char buf[200], place[100];
213  uint16_t langcode = 0;
214  double longitude, latitude, altitude;
215  const char *key = "location";
216 
217  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
218  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
219  return AVERROR_INVALIDDATA;
220  }
221 
222  avio_skip(pb, 4); // version+flags
223  langcode = avio_rb16(pb);
224  ff_mov_lang_to_iso639(langcode, language);
225  len -= 6;
226 
227  len -= avio_get_str(pb, len, place, sizeof(place));
228  if (len < 1) {
229  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
230  return AVERROR_INVALIDDATA;
231  }
232  avio_skip(pb, 1); // role
233  len -= 1;
234 
235  if (len < 12) {
236  av_log(c->fc, AV_LOG_ERROR, "no space for coordinates left (%d)\n", len);
237  return AVERROR_INVALIDDATA;
238  }
239  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
240  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
241  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
242 
243  // Try to output in the same format as the ?xyz field
244  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
245  if (altitude)
246  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
247  av_strlcatf(buf, sizeof(buf), "/%s", place);
248 
249  if (*language && strcmp(language, "und")) {
250  char key2[16];
251  snprintf(key2, sizeof(key2), "%s-%s", key, language);
252  av_dict_set(&c->fc->metadata, key2, buf, 0);
253  }
255  return av_dict_set(&c->fc->metadata, key, buf, 0);
256 }
257 
259 {
260  char tmp_key[5];
261  char key2[32], language[4] = {0};
262  char *str = NULL;
263  const char *key = NULL;
264  uint16_t langcode = 0;
265  uint32_t data_type = 0, str_size, str_size_alloc;
266  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
267  int raw = 0;
268 
269  switch (atom.type) {
270  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
271  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
272  case MKTAG( 'X','M','P','_'):
273  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
274  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
275  case MKTAG( 'a','k','I','D'): key = "account_type";
277  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
278  case MKTAG( 'c','a','t','g'): key = "category"; break;
279  case MKTAG( 'c','p','i','l'): key = "compilation";
281  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
282  case MKTAG( 'd','e','s','c'): key = "description"; break;
283  case MKTAG( 'd','i','s','k'): key = "disc";
285  case MKTAG( 'e','g','i','d'): key = "episode_uid";
287  case MKTAG( 'g','n','r','e'): key = "genre";
288  parse = mov_metadata_gnre; break;
289  case MKTAG( 'h','d','v','d'): key = "hd_video";
291  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
292  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
293  case MKTAG( 'l','o','c','i'):
294  return mov_metadata_loci(c, pb, atom.size);
295  case MKTAG( 'p','c','s','t'): key = "podcast";
297  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
299  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
300  case MKTAG( 'r','t','n','g'): key = "rating";
302  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
303  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
304  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
305  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
306  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
307  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
308  case MKTAG( 's','t','i','k'): key = "media_type";
310  case MKTAG( 't','r','k','n'): key = "track";
312  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
313  case MKTAG( 't','v','e','s'): key = "episode_sort";
315  case MKTAG( 't','v','n','n'): key = "network"; break;
316  case MKTAG( 't','v','s','h'): key = "show"; break;
317  case MKTAG( 't','v','s','n'): key = "season_number";
319  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
320  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
321  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
322  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
323  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
324  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
325  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
326  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
327  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
328  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
329  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
330  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
331  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
332  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
333  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
334  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
335  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
336  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
337  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
338  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
339  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
340  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
341  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
342  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
343  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
344  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
345  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
346  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
347  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
348  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
349  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
350  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
351  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
352  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
353  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
354  }
355 retry:
356  if (c->itunes_metadata && atom.size > 8) {
357  int data_size = avio_rb32(pb);
358  int tag = avio_rl32(pb);
359  if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) {
360  data_type = avio_rb32(pb); // type
361  avio_rb32(pb); // unknown
362  str_size = data_size - 16;
363  atom.size -= 16;
364 
365  if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
366  int ret = mov_read_covr(c, pb, data_type, str_size);
367  if (ret < 0) {
368  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
369  }
370  return ret;
371  }
372  } else return 0;
373  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
374  str_size = avio_rb16(pb); // string length
375  if (str_size > atom.size) {
376  raw = 1;
377  avio_seek(pb, -2, SEEK_CUR);
378  av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n");
379  goto retry;
380  }
381  langcode = avio_rb16(pb);
382  ff_mov_lang_to_iso639(langcode, language);
383  atom.size -= 4;
384  } else
385  str_size = atom.size;
386 
387  if (c->export_all && !key) {
388  snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
389  key = tmp_key;
390  }
391 
392  if (!key)
393  return 0;
394  if (atom.size < 0 || str_size >= INT_MAX/2)
395  return AVERROR_INVALIDDATA;
396 
397  // worst-case requirement for output string in case of utf8 coded input
398  str_size_alloc = (raw ? str_size : str_size * 2) + 1;
399  str = av_mallocz(str_size_alloc);
400  if (!str)
401  return AVERROR(ENOMEM);
402 
403  if (parse)
404  parse(c, pb, str_size, key);
405  else {
406  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
407  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
408  } else {
409  int ret = ffio_read_size(pb, str, str_size);
410  if (ret < 0) {
411  av_free(str);
412  return ret;
413  }
414  str[str_size] = 0;
415  }
417  av_dict_set(&c->fc->metadata, key, str, 0);
418  if (*language && strcmp(language, "und")) {
419  snprintf(key2, sizeof(key2), "%s-%s", key, language);
420  av_dict_set(&c->fc->metadata, key2, str, 0);
421  }
422  if (!strcmp(key, "encoder")) {
423  int major, minor, micro;
424  if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, &micro) == 3) {
425  c->handbrake_version = 1000000*major + 1000*minor + micro;
426  }
427  }
428  }
429  av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
430  av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
431  key, str, (char*)&atom.type, str_size_alloc, atom.size);
432 
433  av_freep(&str);
434  return 0;
435 }
436 
438 {
439  int64_t start;
440  int i, nb_chapters, str_len, version;
441  char str[256+1];
442  int ret;
443 
444  if ((atom.size -= 5) < 0)
445  return 0;
446 
447  version = avio_r8(pb);
448  avio_rb24(pb);
449  if (version)
450  avio_rb32(pb); // ???
451  nb_chapters = avio_r8(pb);
452 
453  for (i = 0; i < nb_chapters; i++) {
454  if (atom.size < 9)
455  return 0;
456 
457  start = avio_rb64(pb);
458  str_len = avio_r8(pb);
459 
460  if ((atom.size -= 9+str_len) < 0)
461  return 0;
462 
463  ret = ffio_read_size(pb, str, str_len);
464  if (ret < 0)
465  return ret;
466  str[str_len] = 0;
467  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
468  }
469  return 0;
470 }
471 
472 #define MIN_DATA_ENTRY_BOX_SIZE 12
474 {
475  AVStream *st;
476  MOVStreamContext *sc;
477  int entries, i, j;
478 
479  if (c->fc->nb_streams < 1)
480  return 0;
481  st = c->fc->streams[c->fc->nb_streams-1];
482  sc = st->priv_data;
483 
484  avio_rb32(pb); // version + flags
485  entries = avio_rb32(pb);
486  if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
487  entries >= UINT_MAX / sizeof(*sc->drefs))
488  return AVERROR_INVALIDDATA;
489  av_free(sc->drefs);
490  sc->drefs_count = 0;
491  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
492  if (!sc->drefs)
493  return AVERROR(ENOMEM);
494  sc->drefs_count = entries;
495 
496  for (i = 0; i < sc->drefs_count; i++) {
497  MOVDref *dref = &sc->drefs[i];
498  uint32_t size = avio_rb32(pb);
499  int64_t next = avio_tell(pb) + size - 4;
500 
501  if (size < 12)
502  return AVERROR_INVALIDDATA;
503 
504  dref->type = avio_rl32(pb);
505  avio_rb32(pb); // version + flags
506  av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
507 
508  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
509  /* macintosh alias record */
510  uint16_t volume_len, len;
511  int16_t type;
512  int ret;
513 
514  avio_skip(pb, 10);
515 
516  volume_len = avio_r8(pb);
517  volume_len = FFMIN(volume_len, 27);
518  ret = ffio_read_size(pb, dref->volume, 27);
519  if (ret < 0)
520  return ret;
521  dref->volume[volume_len] = 0;
522  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
523 
524  avio_skip(pb, 12);
525 
526  len = avio_r8(pb);
527  len = FFMIN(len, 63);
528  ret = ffio_read_size(pb, dref->filename, 63);
529  if (ret < 0)
530  return ret;
531  dref->filename[len] = 0;
532  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
533 
534  avio_skip(pb, 16);
535 
536  /* read next level up_from_alias/down_to_target */
537  dref->nlvl_from = avio_rb16(pb);
538  dref->nlvl_to = avio_rb16(pb);
539  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
540  dref->nlvl_from, dref->nlvl_to);
541 
542  avio_skip(pb, 16);
543 
544  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
545  if(avio_feof(pb))
546  return AVERROR_EOF;
547  type = avio_rb16(pb);
548  len = avio_rb16(pb);
549  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
550  if (len&1)
551  len += 1;
552  if (type == 2 || type == 18) { // absolute path
553  av_free(dref->path);
554  dref->path = av_mallocz(len+1);
555  if (!dref->path)
556  return AVERROR(ENOMEM);
557 
558  ret = ffio_read_size(pb, dref->path, len);
559  if (ret < 0) {
560  av_freep(&dref->path);
561  return ret;
562  }
563  if (type == 18) // no additional processing needed
564  continue;
565  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
566  len -= volume_len;
567  memmove(dref->path, dref->path+volume_len, len);
568  dref->path[len] = 0;
569  }
570  for (j = 0; j < len; j++)
571  if (dref->path[j] == ':')
572  dref->path[j] = '/';
573  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
574  } else if (type == 0) { // directory name
575  av_free(dref->dir);
576  dref->dir = av_malloc(len+1);
577  if (!dref->dir)
578  return AVERROR(ENOMEM);
579 
580  ret = ffio_read_size(pb, dref->dir, len);
581  if (ret < 0) {
582  av_freep(&dref->dir);
583  return ret;
584  }
585  dref->dir[len] = 0;
586  for (j = 0; j < len; j++)
587  if (dref->dir[j] == ':')
588  dref->dir[j] = '/';
589  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
590  } else
591  avio_skip(pb, len);
592  }
593  }
594  avio_seek(pb, next, SEEK_SET);
595  }
596  return 0;
597 }
598 
600 {
601  AVStream *st;
602  uint32_t type;
603  uint32_t av_unused ctype;
604  int64_t title_size;
605  char *title_str;
606  int ret;
607 
608  if (c->fc->nb_streams < 1) // meta before first trak
609  return 0;
610 
611  st = c->fc->streams[c->fc->nb_streams-1];
612 
613  avio_r8(pb); /* version */
614  avio_rb24(pb); /* flags */
615 
616  /* component type */
617  ctype = avio_rl32(pb);
618  type = avio_rl32(pb); /* component subtype */
619 
620  av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
621  av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
622 
623  if (type == MKTAG('v','i','d','e'))
625  else if (type == MKTAG('s','o','u','n'))
627  else if (type == MKTAG('m','1','a',' '))
629  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
631 
632  avio_rb32(pb); /* component manufacture */
633  avio_rb32(pb); /* component flags */
634  avio_rb32(pb); /* component flags mask */
635 
636  title_size = atom.size - 24;
637  if (title_size > 0) {
638  if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
639  return AVERROR_INVALIDDATA;
640  title_str = av_malloc(title_size + 1); /* Add null terminator */
641  if (!title_str)
642  return AVERROR(ENOMEM);
643 
644  ret = ffio_read_size(pb, title_str, title_size);
645  if (ret < 0) {
646  av_freep(&title_str);
647  return ret;
648  }
649  title_str[title_size] = 0;
650  if (title_str[0]) {
651  int off = (!c->isom && title_str[0] == title_size - 1);
652  av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
653  }
654  av_freep(&title_str);
655  }
656 
657  return 0;
658 }
659 
661 {
662  AVStream *st;
663  int tag;
664 
665  if (fc->nb_streams < 1)
666  return 0;
667  st = fc->streams[fc->nb_streams-1];
668 
669  avio_rb32(pb); /* version + flags */
670  ff_mp4_read_descr(fc, pb, &tag);
671  if (tag == MP4ESDescrTag) {
673  } else
674  avio_rb16(pb); /* ID */
675 
676  ff_mp4_read_descr(fc, pb, &tag);
677  if (tag == MP4DecConfigDescrTag)
678  ff_mp4_read_dec_config_descr(fc, st, pb);
679  return 0;
680 }
681 
683 {
684  return ff_mov_read_esds(c->fc, pb);
685 }
686 
688 {
689  AVStream *st;
690  enum AVAudioServiceType *ast;
691  int ac3info, acmod, lfeon, bsmod;
692 
693  if (c->fc->nb_streams < 1)
694  return 0;
695  st = c->fc->streams[c->fc->nb_streams-1];
696 
698  sizeof(*ast));
699  if (!ast)
700  return AVERROR(ENOMEM);
701 
702  ac3info = avio_rb24(pb);
703  bsmod = (ac3info >> 14) & 0x7;
704  acmod = (ac3info >> 11) & 0x7;
705  lfeon = (ac3info >> 10) & 0x1;
706  st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
708  if (lfeon)
710  *ast = bsmod;
711  if (st->codec->channels > 1 && bsmod == 0x7)
713 
714  st->codec->audio_service_type = *ast;
715 
716  return 0;
717 }
718 
720 {
721  AVStream *st;
722  enum AVAudioServiceType *ast;
723  int eac3info, acmod, lfeon, bsmod;
724 
725  if (c->fc->nb_streams < 1)
726  return 0;
727  st = c->fc->streams[c->fc->nb_streams-1];
728 
730  sizeof(*ast));
731  if (!ast)
732  return AVERROR(ENOMEM);
733 
734  /* No need to parse fields for additional independent substreams and its
735  * associated dependent substreams since libavcodec's E-AC-3 decoder
736  * does not support them yet. */
737  avio_rb16(pb); /* data_rate and num_ind_sub */
738  eac3info = avio_rb24(pb);
739  bsmod = (eac3info >> 12) & 0x1f;
740  acmod = (eac3info >> 9) & 0x7;
741  lfeon = (eac3info >> 8) & 0x1;
743  if (lfeon)
746  *ast = bsmod;
747  if (st->codec->channels > 1 && bsmod == 0x7)
749 
750  st->codec->audio_service_type = *ast;
751 
752  return 0;
753 }
754 
756 {
757  AVStream *st;
758 
759  if (c->fc->nb_streams < 1)
760  return 0;
761  st = c->fc->streams[c->fc->nb_streams-1];
762 
763  if (atom.size < 16)
764  return 0;
765 
766  /* skip version and flags */
767  avio_skip(pb, 4);
768 
769  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
770 
771  return 0;
772 }
773 
775 {
776  AVStream *st;
777  int ret;
778 
779  if (c->fc->nb_streams < 1)
780  return 0;
781  st = c->fc->streams[c->fc->nb_streams-1];
782 
783  if ((ret = ff_get_wav_header(c->fc, pb, st->codec, atom.size, 0)) < 0)
784  av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n");
785 
786  return ret;
787 }
788 
790 {
791  const int num = avio_rb32(pb);
792  const int den = avio_rb32(pb);
793  AVStream *st;
794 
795  if (c->fc->nb_streams < 1)
796  return 0;
797  st = c->fc->streams[c->fc->nb_streams-1];
798 
799  if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
800  (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
802  "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
804  num, den);
805  } else if (den != 0) {
807  num, den, 32767);
808  }
809  return 0;
810 }
811 
812 /* this atom contains actual media data */
814 {
815  if (atom.size == 0) /* wrong one (MP4) */
816  return 0;
817  c->found_mdat=1;
818  return 0; /* now go for moov */
819 }
820 
821 #define DRM_BLOB_SIZE 56
822 
824 {
825  uint8_t intermediate_key[20];
826  uint8_t intermediate_iv[20];
827  uint8_t input[64];
828  uint8_t output[64];
829  uint8_t file_checksum[20];
830  uint8_t calculated_checksum[20];
831  struct AVSHA *sha;
832  int i;
833  int ret = 0;
834  uint8_t *activation_bytes = c->activation_bytes;
835  uint8_t *fixed_key = c->audible_fixed_key;
836 
837  c->aax_mode = 1;
838 
839  sha = av_sha_alloc();
840  if (!sha)
841  return AVERROR(ENOMEM);
842  c->aes_decrypt = av_aes_alloc();
843  if (!c->aes_decrypt) {
844  ret = AVERROR(ENOMEM);
845  goto fail;
846  }
847 
848  /* drm blob processing */
849  avio_read(pb, output, 8); // go to offset 8, absolute position 0x251
850  avio_read(pb, input, DRM_BLOB_SIZE);
851  avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d
852  avio_read(pb, file_checksum, 20);
853 
854  av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools
855  for (i = 0; i < 20; i++)
856  av_log(sha, AV_LOG_INFO, "%02x", file_checksum[i]);
857  av_log(c->fc, AV_LOG_INFO, "\n");
858 
859  /* verify activation data */
860  if (!activation_bytes) {
861  av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n");
862  ret = 0; /* allow ffprobe to continue working on .aax files */
863  goto fail;
864  }
865  if (c->activation_bytes_size != 4) {
866  av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n");
867  ret = AVERROR(EINVAL);
868  goto fail;
869  }
870 
871  /* verify fixed key */
872  if (c->audible_fixed_key_size != 16) {
873  av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n");
874  ret = AVERROR(EINVAL);
875  goto fail;
876  }
877 
878  /* AAX (and AAX+) key derivation */
879  av_sha_init(sha, 160);
880  av_sha_update(sha, fixed_key, 16);
881  av_sha_update(sha, activation_bytes, 4);
882  av_sha_final(sha, intermediate_key);
883  av_sha_init(sha, 160);
884  av_sha_update(sha, fixed_key, 16);
885  av_sha_update(sha, intermediate_key, 20);
886  av_sha_update(sha, activation_bytes, 4);
887  av_sha_final(sha, intermediate_iv);
888  av_sha_init(sha, 160);
889  av_sha_update(sha, intermediate_key, 16);
890  av_sha_update(sha, intermediate_iv, 16);
891  av_sha_final(sha, calculated_checksum);
892  if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error
893  av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n");
894  ret = AVERROR_INVALIDDATA;
895  goto fail;
896  }
897  av_aes_init(c->aes_decrypt, intermediate_key, 128, 1);
898  av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1);
899  for (i = 0; i < 4; i++) {
900  // file data (in output) is stored in big-endian mode
901  if (activation_bytes[i] != output[3 - i]) { // critical error
902  av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n");
903  ret = AVERROR_INVALIDDATA;
904  goto fail;
905  }
906  }
907  memcpy(c->file_key, output + 8, 16);
908  memcpy(input, output + 26, 16);
909  av_sha_init(sha, 160);
910  av_sha_update(sha, input, 16);
911  av_sha_update(sha, c->file_key, 16);
912  av_sha_update(sha, fixed_key, 16);
913  av_sha_final(sha, c->file_iv);
914 
915 fail:
916  av_free(sha);
917 
918  return ret;
919 }
920 
921 // Audible AAX (and AAX+) bytestream decryption
922 static int aax_filter(uint8_t *input, int size, MOVContext *c)
923 {
924  int blocks = 0;
925  unsigned char iv[16];
926 
927  memcpy(iv, c->file_iv, 16); // iv is overwritten
928  blocks = size >> 4; // trailing bytes are not encrypted!
929  av_aes_init(c->aes_decrypt, c->file_key, 128, 1);
930  av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1);
931 
932  return 0;
933 }
934 
935 /* read major brand, minor version and compatible brands and store them as metadata */
937 {
938  uint32_t minor_ver;
939  int comp_brand_size;
940  char* comp_brands_str;
941  uint8_t type[5] = {0};
942  int ret = ffio_read_size(pb, type, 4);
943  if (ret < 0)
944  return ret;
945 
946  if (strcmp(type, "qt "))
947  c->isom = 1;
948  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
949  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
950  minor_ver = avio_rb32(pb); /* minor version */
951  av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0);
952 
953  comp_brand_size = atom.size - 8;
954  if (comp_brand_size < 0)
955  return AVERROR_INVALIDDATA;
956  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
957  if (!comp_brands_str)
958  return AVERROR(ENOMEM);
959 
960  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
961  if (ret < 0) {
962  av_freep(&comp_brands_str);
963  return ret;
964  }
965  comp_brands_str[comp_brand_size] = 0;
966  av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
967  av_freep(&comp_brands_str);
968 
969  return 0;
970 }
971 
972 /* this atom should contain all header atoms */
974 {
975  int ret;
976 
977  if (c->found_moov) {
978  av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n");
979  avio_skip(pb, atom.size);
980  return 0;
981  }
982 
983  if ((ret = mov_read_default(c, pb, atom)) < 0)
984  return ret;
985  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
986  /* so we don't parse the whole file if over a network */
987  c->found_moov=1;
988  return 0; /* now go for mdat */
989 }
990 
992 {
993  if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
994  c->has_looked_for_mfra = 1;
995  if (pb->seekable) {
996  int ret;
997  av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look "
998  "for a mfra\n");
999  if ((ret = mov_read_mfra(c, pb)) < 0) {
1000  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to "
1001  "read the mfra (may be a live ismv)\n");
1002  }
1003  } else {
1004  av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not "
1005  "seekable, can not look for mfra\n");
1006  }
1007  }
1009  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
1010  return mov_read_default(c, pb, atom);
1011 }
1012 
1013 static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
1014 {
1015  char buffer[32];
1016  if (time) {
1017  struct tm *ptm, tmbuf;
1018  time_t timet;
1019  if(time >= 2082844800)
1020  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
1021  timet = time;
1022  ptm = gmtime_r(&timet, &tmbuf);
1023  if (!ptm) return;
1024  if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
1025  av_dict_set(metadata, "creation_time", buffer, 0);
1026  }
1027 }
1028 
1030 {
1031  AVStream *st;
1032  MOVStreamContext *sc;
1033  int version;
1034  char language[4] = {0};
1035  unsigned lang;
1036  int64_t creation_time;
1037 
1038  if (c->fc->nb_streams < 1)
1039  return 0;
1040  st = c->fc->streams[c->fc->nb_streams-1];
1041  sc = st->priv_data;
1042 
1043  if (sc->time_scale) {
1044  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
1045  return AVERROR_INVALIDDATA;
1046  }
1047 
1048  version = avio_r8(pb);
1049  if (version > 1) {
1050  avpriv_request_sample(c->fc, "Version %d", version);
1051  return AVERROR_PATCHWELCOME;
1052  }
1053  avio_rb24(pb); /* flags */
1054  if (version == 1) {
1055  creation_time = avio_rb64(pb);
1056  avio_rb64(pb);
1057  } else {
1058  creation_time = avio_rb32(pb);
1059  avio_rb32(pb); /* modification time */
1060  }
1061  mov_metadata_creation_time(&st->metadata, creation_time);
1062 
1063  sc->time_scale = avio_rb32(pb);
1064  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1065 
1066  lang = avio_rb16(pb); /* language */
1067  if (ff_mov_lang_to_iso639(lang, language))
1068  av_dict_set(&st->metadata, "language", language, 0);
1069  avio_rb16(pb); /* quality */
1070 
1071  return 0;
1072 }
1073 
1075 {
1076  int64_t creation_time;
1077  int version = avio_r8(pb); /* version */
1078  avio_rb24(pb); /* flags */
1079 
1080  if (version == 1) {
1081  creation_time = avio_rb64(pb);
1082  avio_rb64(pb);
1083  } else {
1084  creation_time = avio_rb32(pb);
1085  avio_rb32(pb); /* modification time */
1086  }
1087  mov_metadata_creation_time(&c->fc->metadata, creation_time);
1088  c->time_scale = avio_rb32(pb); /* time scale */
1089 
1090  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
1091 
1092  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
1093  // set the AVCodecContext duration because the duration of individual tracks
1094  // may be inaccurate
1095  if (c->time_scale > 0 && !c->trex_data)
1097  avio_rb32(pb); /* preferred scale */
1098 
1099  avio_rb16(pb); /* preferred volume */
1100 
1101  avio_skip(pb, 10); /* reserved */
1102 
1103  avio_skip(pb, 36); /* display matrix */
1104 
1105  avio_rb32(pb); /* preview time */
1106  avio_rb32(pb); /* preview duration */
1107  avio_rb32(pb); /* poster time */
1108  avio_rb32(pb); /* selection time */
1109  avio_rb32(pb); /* selection duration */
1110  avio_rb32(pb); /* current time */
1111  avio_rb32(pb); /* next track ID */
1112 
1113  return 0;
1114 }
1115 
1117 {
1118  AVStream *st;
1119  int little_endian;
1120 
1121  if (c->fc->nb_streams < 1)
1122  return 0;
1123  st = c->fc->streams[c->fc->nb_streams-1];
1124 
1125  little_endian = avio_rb16(pb) & 0xFF;
1126  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1127  if (little_endian == 1) {
1128  switch (st->codec->codec_id) {
1129  case AV_CODEC_ID_PCM_S24BE:
1131  break;
1132  case AV_CODEC_ID_PCM_S32BE:
1134  break;
1135  case AV_CODEC_ID_PCM_F32BE:
1137  break;
1138  case AV_CODEC_ID_PCM_F64BE:
1140  break;
1141  default:
1142  break;
1143  }
1144  }
1145  return 0;
1146 }
1147 
1149 {
1150  AVStream *st;
1151  char color_parameter_type[5] = { 0 };
1152  uint16_t color_primaries, color_trc, color_matrix;
1153  int ret;
1154 
1155  if (c->fc->nb_streams < 1)
1156  return 0;
1157  st = c->fc->streams[c->fc->nb_streams - 1];
1158 
1159  ret = ffio_read_size(pb, color_parameter_type, 4);
1160  if (ret < 0)
1161  return ret;
1162  if (strncmp(color_parameter_type, "nclx", 4) &&
1163  strncmp(color_parameter_type, "nclc", 4)) {
1164  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1165  color_parameter_type);
1166  return 0;
1167  }
1168 
1169  color_primaries = avio_rb16(pb);
1170  color_trc = avio_rb16(pb);
1171  color_matrix = avio_rb16(pb);
1172 
1173  av_log(c->fc, AV_LOG_TRACE,
1174  "%s: pri %d trc %d matrix %d",
1175  color_parameter_type, color_primaries, color_trc, color_matrix);
1176 
1177  if (!strncmp(color_parameter_type, "nclx", 4)) {
1178  uint8_t color_range = avio_r8(pb) >> 7;
1179  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1180  if (color_range)
1182  else
1184  /* 14496-12 references JPEG XR specs (rather than the more complete
1185  * 23001-8) so some adjusting is required */
1186  if (color_primaries >= AVCOL_PRI_FILM)
1187  color_primaries = AVCOL_PRI_UNSPECIFIED;
1188  if ((color_trc >= AVCOL_TRC_LINEAR &&
1189  color_trc <= AVCOL_TRC_LOG_SQRT) ||
1190  color_trc >= AVCOL_TRC_BT2020_10)
1191  color_trc = AVCOL_TRC_UNSPECIFIED;
1192  if (color_matrix >= AVCOL_SPC_BT2020_NCL)
1193  color_matrix = AVCOL_SPC_UNSPECIFIED;
1195  st->codec->color_trc = color_trc;
1196  st->codec->colorspace = color_matrix;
1197  } else if (!strncmp(color_parameter_type, "nclc", 4)) {
1198  /* color primaries, Table 4-4 */
1199  switch (color_primaries) {
1200  case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break;
1201  case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break;
1202  case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break;
1203  }
1204  /* color transfer, Table 4-5 */
1205  switch (color_trc) {
1206  case 1: st->codec->color_trc = AVCOL_TRC_BT709; break;
1207  case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break;
1208  }
1209  /* color matrix, Table 4-6 */
1210  switch (color_matrix) {
1211  case 1: st->codec->colorspace = AVCOL_SPC_BT709; break;
1212  case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break;
1213  case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break;
1214  }
1215  }
1216  av_log(c->fc, AV_LOG_TRACE, "\n");
1217 
1218  return 0;
1219 }
1220 
1222 {
1223  AVStream *st;
1224  unsigned mov_field_order;
1225  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1226 
1227  if (c->fc->nb_streams < 1) // will happen with jp2 files
1228  return 0;
1229  st = c->fc->streams[c->fc->nb_streams-1];
1230  if (atom.size < 2)
1231  return AVERROR_INVALIDDATA;
1232  mov_field_order = avio_rb16(pb);
1233  if ((mov_field_order & 0xFF00) == 0x0100)
1234  decoded_field_order = AV_FIELD_PROGRESSIVE;
1235  else if ((mov_field_order & 0xFF00) == 0x0200) {
1236  switch (mov_field_order & 0xFF) {
1237  case 0x01: decoded_field_order = AV_FIELD_TT;
1238  break;
1239  case 0x06: decoded_field_order = AV_FIELD_BB;
1240  break;
1241  case 0x09: decoded_field_order = AV_FIELD_TB;
1242  break;
1243  case 0x0E: decoded_field_order = AV_FIELD_BT;
1244  break;
1245  }
1246  }
1247  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1248  av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1249  }
1250  st->codec->field_order = decoded_field_order;
1251 
1252  return 0;
1253 }
1254 
1256 {
1257  int err = 0;
1258  uint64_t size = (uint64_t)codec->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1259  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1260  return AVERROR_INVALIDDATA;
1261  if ((err = av_reallocp(&codec->extradata, size)) < 0) {
1262  codec->extradata_size = 0;
1263  return err;
1264  }
1266  return 0;
1267 }
1268 
1269 /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */
1271  AVCodecContext *codec, uint8_t *buf)
1272 {
1273  int64_t result = atom.size;
1274  int err;
1275 
1276  AV_WB32(buf , atom.size + 8);
1277  AV_WL32(buf + 4, atom.type);
1278  err = ffio_read_size(pb, buf + 8, atom.size);
1279  if (err < 0) {
1280  codec->extradata_size -= atom.size;
1281  return err;
1282  } else if (err < atom.size) {
1283  av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
1284  codec->extradata_size -= atom.size - err;
1285  result = err;
1286  }
1287  memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1288  return result;
1289 }
1290 
1291 /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
1293  enum AVCodecID codec_id)
1294 {
1295  AVStream *st;
1296  uint64_t original_size;
1297  int err;
1298 
1299  if (c->fc->nb_streams < 1) // will happen with jp2 files
1300  return 0;
1301  st = c->fc->streams[c->fc->nb_streams-1];
1302 
1303  if (st->codec->codec_id != codec_id)
1304  return 0; /* unexpected codec_id - don't mess with extradata */
1305 
1306  original_size = st->codec->extradata_size;
1307  err = mov_realloc_extradata(st->codec, atom);
1308  if (err)
1309  return err;
1310 
1311  err = mov_read_atom_into_extradata(c, pb, atom, st->codec, st->codec->extradata + original_size);
1312  if (err < 0)
1313  return err;
1314  return 0; // Note: this is the original behavior to ignore truncation.
1315 }
1316 
1317 /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
1319 {
1320  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
1321 }
1322 
1324 {
1325  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
1326 }
1327 
1329 {
1330  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
1331 }
1332 
1334 {
1335  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K);
1336 }
1337 
1339 {
1340  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
1341  if(ret == 0)
1342  ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD);
1343  return ret;
1344 }
1345 
1347 {
1348  int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216);
1349 
1350  if (!ret && c->fc->nb_streams >= 1) {
1351  AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec;
1352  if (avctx->extradata_size >= 40) {
1353  avctx->height = AV_RB16(&avctx->extradata[36]);
1354  avctx->width = AV_RB16(&avctx->extradata[38]);
1355  }
1356  }
1357  return ret;
1358 }
1359 
1361 {
1362  if (c->fc->nb_streams >= 1) {
1363  AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1364  if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
1365  codec->codec_id == AV_CODEC_ID_H264 &&
1366  atom.size > 11) {
1367  avio_skip(pb, 10);
1368  /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
1369  if (avio_rb16(pb) == 0xd4d)
1370  codec->width = 1440;
1371  return 0;
1372  }
1373  }
1374 
1375  return mov_read_avid(c, pb, atom);
1376 }
1377 
1379 {
1380  int ret = 0;
1381  int length = 0;
1382  uint64_t original_size;
1383  if (c->fc->nb_streams >= 1) {
1384  AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec;
1385  if (codec->codec_id == AV_CODEC_ID_H264)
1386  return 0;
1387  if (atom.size == 16) {
1388  original_size = codec->extradata_size;
1389  ret = mov_realloc_extradata(codec, atom);
1390  if (!ret) {
1391  length = mov_read_atom_into_extradata(c, pb, atom, codec, codec->extradata + original_size);
1392  if (length == atom.size) {
1393  const uint8_t range_value = codec->extradata[original_size + 19];
1394  switch (range_value) {
1395  case 1:
1396  codec->color_range = AVCOL_RANGE_MPEG;
1397  break;
1398  case 2:
1399  codec->color_range = AVCOL_RANGE_JPEG;
1400  break;
1401  default:
1402  av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value);
1403  break;
1404  }
1405  ff_dlog(c, "color_range: %d\n", codec->color_range);
1406  } else {
1407  /* For some reason the whole atom was not added to the extradata */
1408  av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n");
1409  }
1410  } else {
1411  av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n");
1412  }
1413  } else {
1414  av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size);
1415  }
1416  }
1417 
1418  return ret;
1419 }
1420 
1422 {
1423  return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
1424 }
1425 
1427 {
1428  AVStream *st;
1429  int ret;
1430 
1431  if (c->fc->nb_streams < 1)
1432  return 0;
1433  st = c->fc->streams[c->fc->nb_streams-1];
1434 
1435  if ((uint64_t)atom.size > (1<<30))
1436  return AVERROR_INVALIDDATA;
1437 
1438  if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
1439  st->codec->codec_id == AV_CODEC_ID_QDMC ||
1440  st->codec->codec_id == AV_CODEC_ID_SPEEX) {
1441  // pass all frma atom to codec, needed at least for QDMC and QDM2
1442  av_freep(&st->codec->extradata);
1443  ret = ff_get_extradata(st->codec, pb, atom.size);
1444  if (ret < 0)
1445  return ret;
1446  } else if (atom.size > 8) { /* to read frma, esds atoms */
1447  if (st->codec->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) {
1448  uint64_t buffer;
1449  ret = ffio_ensure_seekback(pb, 8);
1450  if (ret < 0)
1451  return ret;
1452  buffer = avio_rb64(pb);
1453  atom.size -= 8;
1454  if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a')
1455  && buffer >> 32 <= atom.size
1456  && buffer >> 32 >= 8) {
1457  avio_skip(pb, -8);
1458  atom.size += 8;
1459  } else if (!st->codec->extradata_size) {
1460 #define ALAC_EXTRADATA_SIZE 36
1462  if (!st->codec->extradata)
1463  return AVERROR(ENOMEM);
1466  AV_WB32(st->codec->extradata + 4, MKTAG('a','l','a','c'));
1467  AV_WB64(st->codec->extradata + 12, buffer);
1468  avio_read(pb, st->codec->extradata + 20, 16);
1469  avio_skip(pb, atom.size - 24);
1470  return 0;
1471  }
1472  }
1473  if ((ret = mov_read_default(c, pb, atom)) < 0)
1474  return ret;
1475  } else
1476  avio_skip(pb, atom.size);
1477  return 0;
1478 }
1479 
1480 /**
1481  * This function reads atom content and puts data in extradata without tag
1482  * nor size unlike mov_read_extradata.
1483  */
1485 {
1486  AVStream *st;
1487  int ret;
1488 
1489  if (c->fc->nb_streams < 1)
1490  return 0;
1491  st = c->fc->streams[c->fc->nb_streams-1];
1492 
1493  if ((uint64_t)atom.size > (1<<30))
1494  return AVERROR_INVALIDDATA;
1495 
1496  if (atom.size >= 10) {
1497  // Broken files created by legacy versions of libavformat will
1498  // wrap a whole fiel atom inside of a glbl atom.
1499  unsigned size = avio_rb32(pb);
1500  unsigned type = avio_rl32(pb);
1501  avio_seek(pb, -8, SEEK_CUR);
1502  if (type == MKTAG('f','i','e','l') && size == atom.size)
1503  return mov_read_default(c, pb, atom);
1504  }
1505  if (st->codec->extradata_size > 1 && st->codec->extradata) {
1506  av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
1507  return 0;
1508  }
1509  av_freep(&st->codec->extradata);
1510  ret = ff_get_extradata(st->codec, pb, atom.size);
1511  if (ret < 0)
1512  return ret;
1513 
1514  return 0;
1515 }
1516 
1518 {
1519  AVStream *st;
1520  uint8_t profile_level;
1521  int ret;
1522 
1523  if (c->fc->nb_streams < 1)
1524  return 0;
1525  st = c->fc->streams[c->fc->nb_streams-1];
1526 
1527  if (atom.size >= (1<<28) || atom.size < 7)
1528  return AVERROR_INVALIDDATA;
1529 
1530  profile_level = avio_r8(pb);
1531  if ((profile_level & 0xf0) != 0xc0)
1532  return 0;
1533 
1534  avio_seek(pb, 6, SEEK_CUR);
1535  av_freep(&st->codec->extradata);
1536  ret = ff_get_extradata(st->codec, pb, atom.size - 7);
1537  if (ret < 0)
1538  return ret;
1539 
1540  return 0;
1541 }
1542 
1543 /**
1544  * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
1545  * but can have extradata appended at the end after the 40 bytes belonging
1546  * to the struct.
1547  */
1549 {
1550  AVStream *st;
1551  int ret;
1552 
1553  if (c->fc->nb_streams < 1)
1554  return 0;
1555  if (atom.size <= 40)
1556  return 0;
1557  st = c->fc->streams[c->fc->nb_streams-1];
1558 
1559  if ((uint64_t)atom.size > (1<<30))
1560  return AVERROR_INVALIDDATA;
1561 
1562  avio_skip(pb, 40);
1563  av_freep(&st->codec->extradata);
1564  ret = ff_get_extradata(st->codec, pb, atom.size - 40);
1565  if (ret < 0)
1566  return ret;
1567 
1568  return 0;
1569 }
1570 
1572 {
1573  AVStream *st;
1574  MOVStreamContext *sc;
1575  unsigned int i, entries;
1576 
1577  if (c->fc->nb_streams < 1)
1578  return 0;
1579  st = c->fc->streams[c->fc->nb_streams-1];
1580  sc = st->priv_data;
1581 
1582  avio_r8(pb); /* version */
1583  avio_rb24(pb); /* flags */
1584 
1585  entries = avio_rb32(pb);
1586 
1587  if (!entries)
1588  return 0;
1589 
1590  if (sc->chunk_offsets)
1591  av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n");
1592  av_free(sc->chunk_offsets);
1593  sc->chunk_count = 0;
1594  sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
1595  if (!sc->chunk_offsets)
1596  return AVERROR(ENOMEM);
1597  sc->chunk_count = entries;
1598 
1599  if (atom.type == MKTAG('s','t','c','o'))
1600  for (i = 0; i < entries && !pb->eof_reached; i++)
1601  sc->chunk_offsets[i] = avio_rb32(pb);
1602  else if (atom.type == MKTAG('c','o','6','4'))
1603  for (i = 0; i < entries && !pb->eof_reached; i++)
1604  sc->chunk_offsets[i] = avio_rb64(pb);
1605  else
1606  return AVERROR_INVALIDDATA;
1607 
1608  sc->chunk_count = i;
1609 
1610  if (pb->eof_reached)
1611  return AVERROR_EOF;
1612 
1613  return 0;
1614 }
1615 
1616 /**
1617  * Compute codec id for 'lpcm' tag.
1618  * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
1619  */
1621 {
1622  /* lpcm flags:
1623  * 0x1 = float
1624  * 0x2 = big-endian
1625  * 0x4 = signed
1626  */
1627  return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1628 }
1629 
1630 static int mov_codec_id(AVStream *st, uint32_t format)
1631 {
1632  int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1633 
1634  if (id <= 0 &&
1635  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1636  (format & 0xFFFF) == 'T' + ('S' << 8)))
1637  id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1638 
1639  if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1641  } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO &&
1642  /* skip old asf mpeg4 tag */
1643  format && format != MKTAG('m','p','4','s')) {
1645  if (id <= 0)
1646  id = ff_codec_get_id(ff_codec_bmp_tags, format);
1647  if (id > 0)
1649  else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
1651  st->codec->codec_id == AV_CODEC_ID_NONE)) {
1653  if (id > 0)
1655  }
1656  }
1657 
1658  st->codec->codec_tag = format;
1659 
1660  return id;
1661 }
1662 
1664  AVStream *st, MOVStreamContext *sc)
1665 {
1666  uint8_t codec_name[32];
1667  unsigned int color_depth, len, j;
1668  int color_greyscale;
1669  int color_table_id;
1670 
1671  avio_rb16(pb); /* version */
1672  avio_rb16(pb); /* revision level */
1673  avio_rb32(pb); /* vendor */
1674  avio_rb32(pb); /* temporal quality */
1675  avio_rb32(pb); /* spatial quality */
1676 
1677  st->codec->width = avio_rb16(pb); /* width */
1678  st->codec->height = avio_rb16(pb); /* height */
1679 
1680  avio_rb32(pb); /* horiz resolution */
1681  avio_rb32(pb); /* vert resolution */
1682  avio_rb32(pb); /* data size, always 0 */
1683  avio_rb16(pb); /* frames per samples */
1684 
1685  len = avio_r8(pb); /* codec name, pascal string */
1686  if (len > 31)
1687  len = 31;
1688  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1689  if (len < 31)
1690  avio_skip(pb, 31 - len);
1691 
1692  if (codec_name[0])
1693  av_dict_set(&st->metadata, "encoder", codec_name, 0);
1694 
1695  /* codec_tag YV12 triggers an UV swap in rawdec.c */
1696  if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
1697  st->codec->codec_tag = MKTAG('I', '4', '2', '0');
1698  st->codec->width &= ~1;
1699  st->codec->height &= ~1;
1700  }
1701  /* Flash Media Server uses tag H263 with Sorenson Spark */
1702  if (st->codec->codec_tag == MKTAG('H','2','6','3') &&
1703  !memcmp(codec_name, "Sorenson H263", 13))
1705 
1706  st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
1707  color_table_id = avio_rb16(pb); /* colortable id */
1708  av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
1709  st->codec->bits_per_coded_sample, color_table_id);
1710  /* figure out the palette situation */
1711  color_depth = st->codec->bits_per_coded_sample & 0x1F;
1712  color_greyscale = st->codec->bits_per_coded_sample & 0x20;
1713  /* Do not create a greyscale palette for cinepak */
1714  if (color_greyscale && st->codec->codec_id == AV_CODEC_ID_CINEPAK)
1715  return;
1716 
1717  /* if the depth is 2, 4, or 8 bpp, file is palettized */
1718  if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1719  /* for palette traversal */
1720  unsigned int color_start, color_count, color_end;
1721  unsigned int a, r, g, b;
1722 
1723  if (color_greyscale) {
1724  int color_index, color_dec;
1725  /* compute the greyscale palette */
1726  st->codec->bits_per_coded_sample = color_depth;
1727  color_count = 1 << color_depth;
1728  color_index = 255;
1729  color_dec = 256 / (color_count - 1);
1730  for (j = 0; j < color_count; j++) {
1731  r = g = b = color_index;
1732  sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1733  color_index -= color_dec;
1734  if (color_index < 0)
1735  color_index = 0;
1736  }
1737  } else if (color_table_id) {
1738  const uint8_t *color_table;
1739  /* if flag bit 3 is set, use the default palette */
1740  color_count = 1 << color_depth;
1741  if (color_depth == 2)
1742  color_table = ff_qt_default_palette_4;
1743  else if (color_depth == 4)
1744  color_table = ff_qt_default_palette_16;
1745  else
1746  color_table = ff_qt_default_palette_256;
1747 
1748  for (j = 0; j < color_count; j++) {
1749  r = color_table[j * 3 + 0];
1750  g = color_table[j * 3 + 1];
1751  b = color_table[j * 3 + 2];
1752  sc->palette[j] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
1753  }
1754  } else {
1755  /* load the palette from the file */
1756  color_start = avio_rb32(pb);
1757  color_count = avio_rb16(pb);
1758  color_end = avio_rb16(pb);
1759  if ((color_start <= 255) && (color_end <= 255)) {
1760  for (j = color_start; j <= color_end; j++) {
1761  /* each A, R, G, or B component is 16 bits;
1762  * only use the top 8 bits */
1763  a = avio_r8(pb);
1764  avio_r8(pb);
1765  r = avio_r8(pb);
1766  avio_r8(pb);
1767  g = avio_r8(pb);
1768  avio_r8(pb);
1769  b = avio_r8(pb);
1770  avio_r8(pb);
1771  sc->palette[j] = (a << 24 ) | (r << 16) | (g << 8) | (b);
1772  }
1773  }
1774  }
1775  sc->has_palette = 1;
1776  }
1777 }
1778 
1780  AVStream *st, MOVStreamContext *sc)
1781 {
1782  int bits_per_sample, flags;
1783  uint16_t version = avio_rb16(pb);
1784  AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE);
1785 
1786  avio_rb16(pb); /* revision level */
1787  avio_rb32(pb); /* vendor */
1788 
1789  st->codec->channels = avio_rb16(pb); /* channel count */
1790  st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1791  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels);
1792 
1793  sc->audio_cid = avio_rb16(pb);
1794  avio_rb16(pb); /* packet size = 0 */
1795 
1796  st->codec->sample_rate = ((avio_rb32(pb) >> 16));
1797 
1798  // Read QT version 1 fields. In version 0 these do not exist.
1799  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
1800  if (!c->isom ||
1801  (compatible_brands && strstr(compatible_brands->value, "qt "))) {
1802 
1803  if (version == 1) {
1804  sc->samples_per_frame = avio_rb32(pb);
1805  avio_rb32(pb); /* bytes per packet */
1806  sc->bytes_per_frame = avio_rb32(pb);
1807  avio_rb32(pb); /* bytes per sample */
1808  } else if (version == 2) {
1809  avio_rb32(pb); /* sizeof struct only */
1811  st->codec->channels = avio_rb32(pb);
1812  avio_rb32(pb); /* always 0x7F000000 */
1814 
1815  flags = avio_rb32(pb); /* lpcm format specific flag */
1816  sc->bytes_per_frame = avio_rb32(pb);
1817  sc->samples_per_frame = avio_rb32(pb);
1818  if (st->codec->codec_tag == MKTAG('l','p','c','m'))
1819  st->codec->codec_id =
1821  flags);
1822  }
1823  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1824  /* can't correctly handle variable sized packet as audio unit */
1825  switch (st->codec->codec_id) {
1826  case AV_CODEC_ID_MP2:
1827  case AV_CODEC_ID_MP3:
1829  break;
1830  }
1831  }
1832  }
1833 
1834  switch (st->codec->codec_id) {
1835  case AV_CODEC_ID_PCM_S8:
1836  case AV_CODEC_ID_PCM_U8:
1837  if (st->codec->bits_per_coded_sample == 16)
1839  break;
1840  case AV_CODEC_ID_PCM_S16LE:
1841  case AV_CODEC_ID_PCM_S16BE:
1842  if (st->codec->bits_per_coded_sample == 8)
1844  else if (st->codec->bits_per_coded_sample == 24)
1845  st->codec->codec_id =
1848  else if (st->codec->bits_per_coded_sample == 32)
1849  st->codec->codec_id =
1852  break;
1853  /* set values for old format before stsd version 1 appeared */
1854  case AV_CODEC_ID_MACE3:
1855  sc->samples_per_frame = 6;
1856  sc->bytes_per_frame = 2 * st->codec->channels;
1857  break;
1858  case AV_CODEC_ID_MACE6:
1859  sc->samples_per_frame = 6;
1860  sc->bytes_per_frame = 1 * st->codec->channels;
1861  break;
1863  sc->samples_per_frame = 64;
1864  sc->bytes_per_frame = 34 * st->codec->channels;
1865  break;
1866  case AV_CODEC_ID_GSM:
1867  sc->samples_per_frame = 160;
1868  sc->bytes_per_frame = 33;
1869  break;
1870  default:
1871  break;
1872  }
1873 
1874  bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
1875  if (bits_per_sample) {
1876  st->codec->bits_per_coded_sample = bits_per_sample;
1877  sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
1878  }
1879 }
1880 
1882  AVStream *st, MOVStreamContext *sc,
1883  int64_t size)
1884 {
1885  // ttxt stsd contains display flags, justification, background
1886  // color, fonts, and default styles, so fake an atom to read it
1887  MOVAtom fake_atom = { .size = size };
1888  // mp4s contains a regular esds atom
1889  if (st->codec->codec_tag != AV_RL32("mp4s"))
1890  mov_read_glbl(c, pb, fake_atom);
1891  st->codec->width = sc->width;
1892  st->codec->height = sc->height;
1893 }
1894 
1895 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1896 {
1897  uint8_t r, g, b;
1898  int y, cb, cr;
1899 
1900  y = (ycbcr >> 16) & 0xFF;
1901  cr = (ycbcr >> 8) & 0xFF;
1902  cb = ycbcr & 0xFF;
1903 
1904  b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000);
1905  g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000);
1906  r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000);
1907 
1908  return (r << 16) | (g << 8) | b;
1909 }
1910 
1912 {
1913  char buf[256] = {0};
1914  uint8_t *src = st->codec->extradata;
1915  int i;
1916 
1917  if (st->codec->extradata_size != 64)
1918  return 0;
1919 
1920  if (st->codec->width > 0 && st->codec->height > 0)
1921  snprintf(buf, sizeof(buf), "size: %dx%d\n",
1922  st->codec->width, st->codec->height);
1923  av_strlcat(buf, "palette: ", sizeof(buf));
1924 
1925  for (i = 0; i < 16; i++) {
1926  uint32_t yuv = AV_RB32(src + i * 4);
1927  uint32_t rgba = yuv_to_rgba(yuv);
1928 
1929  av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1930  }
1931 
1932  if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1933  return 0;
1934 
1935  av_freep(&st->codec->extradata);
1936  st->codec->extradata_size = 0;
1938  if (!st->codec->extradata)
1939  return AVERROR(ENOMEM);
1940  st->codec->extradata_size = strlen(buf);
1941  memcpy(st->codec->extradata, buf, st->codec->extradata_size);
1942 
1943  return 0;
1944 }
1945 
1947  AVStream *st, MOVStreamContext *sc,
1948  int64_t size)
1949 {
1950  int ret;
1951 
1952  if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
1953  if ((int)size != size)
1954  return AVERROR(ENOMEM);
1955 
1956  ret = ff_get_extradata(st->codec, pb, size);
1957  if (ret < 0)
1958  return ret;
1959  if (size > 16) {
1960  MOVStreamContext *tmcd_ctx = st->priv_data;
1961  int val;
1962  val = AV_RB32(st->codec->extradata + 4);
1963  tmcd_ctx->tmcd_flags = val;
1964  if (val & 1)
1966  st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */
1967  st->codec->time_base.num = 1;
1968  /* adjust for per frame dur in counter mode */
1969  if (tmcd_ctx->tmcd_flags & 0x0008) {
1970  int timescale = AV_RB32(st->codec->extradata + 8);
1971  int framedur = AV_RB32(st->codec->extradata + 12);
1972  st->codec->time_base.den *= timescale;
1973  st->codec->time_base.num *= framedur;
1974  }
1975  if (size > 30) {
1976  uint32_t len = AV_RB32(st->codec->extradata + 18); /* name atom length */
1977  uint32_t format = AV_RB32(st->codec->extradata + 22);
1978  if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
1979  uint16_t str_size = AV_RB16(st->codec->extradata + 26); /* string length */
1980  if (str_size > 0 && size >= (int)str_size + 26) {
1981  char *reel_name = av_malloc(str_size + 1);
1982  if (!reel_name)
1983  return AVERROR(ENOMEM);
1984  memcpy(reel_name, st->codec->extradata + 30, str_size);
1985  reel_name[str_size] = 0; /* Add null terminator */
1986  /* don't add reel_name if emtpy string */
1987  if (*reel_name == 0) {
1988  av_free(reel_name);
1989  } else {
1990  av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL);
1991  }
1992  }
1993  }
1994  }
1995  }
1996  } else {
1997  /* other codec type, just skip (rtp, mp4s ...) */
1998  avio_skip(pb, size);
1999  }
2000  return 0;
2001 }
2002 
2004  AVStream *st, MOVStreamContext *sc)
2005 {
2006  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2007  !st->codec->sample_rate && sc->time_scale > 1)
2008  st->codec->sample_rate = sc->time_scale;
2009 
2010  /* special codec parameters handling */
2011  switch (st->codec->codec_id) {
2012 #if CONFIG_DV_DEMUXER
2013  case AV_CODEC_ID_DVAUDIO:
2015  if (!c->dv_fctx) {
2016  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
2017  return AVERROR(ENOMEM);
2018  }
2020  if (!c->dv_demux) {
2021  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
2022  return AVERROR(ENOMEM);
2023  }
2024  sc->dv_audio_container = 1;
2026  break;
2027 #endif
2028  /* no ifdef since parameters are always those */
2029  case AV_CODEC_ID_QCELP:
2030  st->codec->channels = 1;
2031  // force sample rate for qcelp when not stored in mov
2032  if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
2033  st->codec->sample_rate = 8000;
2034  // FIXME: Why is the following needed for some files?
2035  sc->samples_per_frame = 160;
2036  if (!sc->bytes_per_frame)
2037  sc->bytes_per_frame = 35;
2038  break;
2039  case AV_CODEC_ID_AMR_NB:
2040  st->codec->channels = 1;
2041  /* force sample rate for amr, stsd in 3gp does not store sample rate */
2042  st->codec->sample_rate = 8000;
2043  break;
2044  case AV_CODEC_ID_AMR_WB:
2045  st->codec->channels = 1;
2046  st->codec->sample_rate = 16000;
2047  break;
2048  case AV_CODEC_ID_MP2:
2049  case AV_CODEC_ID_MP3:
2050  /* force type after stsd for m1a hdlr */
2052  break;
2053  case AV_CODEC_ID_GSM:
2054  case AV_CODEC_ID_ADPCM_MS:
2056  case AV_CODEC_ID_ILBC:
2057  case AV_CODEC_ID_MACE3:
2058  case AV_CODEC_ID_MACE6:
2059  case AV_CODEC_ID_QDM2:
2060  st->codec->block_align = sc->bytes_per_frame;
2061  break;
2062  case AV_CODEC_ID_ALAC:
2063  if (st->codec->extradata_size == 36) {
2064  st->codec->channels = AV_RB8 (st->codec->extradata + 21);
2065  st->codec->sample_rate = AV_RB32(st->codec->extradata + 32);
2066  }
2067  break;
2068  case AV_CODEC_ID_AC3:
2069  case AV_CODEC_ID_EAC3:
2071  case AV_CODEC_ID_VC1:
2073  break;
2074  default:
2075  break;
2076  }
2077  return 0;
2078 }
2079 
2081  int codec_tag, int format,
2082  int64_t size)
2083 {
2084  int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
2085 
2086  if (codec_tag &&
2087  (codec_tag != format &&
2088  (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
2089  : codec_tag != MKTAG('j','p','e','g')))) {
2090  /* Multiple fourcc, we skip JPEG. This is not correct, we should
2091  * export it as a separate AVStream but this needs a few changes
2092  * in the MOV demuxer, patch welcome. */
2093 
2094  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
2095  avio_skip(pb, size);
2096  return 1;
2097  }
2098  if ( codec_tag == AV_RL32("avc1") ||
2099  codec_tag == AV_RL32("hvc1") ||
2100  codec_tag == AV_RL32("hev1")
2101  )
2102  av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n");
2103 
2104  return 0;
2105 }
2106 
2108 {
2109  AVStream *st;
2110  MOVStreamContext *sc;
2111  int pseudo_stream_id;
2112 
2113  if (c->fc->nb_streams < 1)
2114  return 0;
2115  st = c->fc->streams[c->fc->nb_streams-1];
2116  sc = st->priv_data;
2117 
2118  for (pseudo_stream_id = 0;
2119  pseudo_stream_id < entries && !pb->eof_reached;
2120  pseudo_stream_id++) {
2121  //Parsing Sample description table
2122  enum AVCodecID id;
2123  int ret, dref_id = 1;
2124  MOVAtom a = { AV_RL32("stsd") };
2125  int64_t start_pos = avio_tell(pb);
2126  int64_t size = avio_rb32(pb); /* size */
2127  uint32_t format = avio_rl32(pb); /* data format */
2128 
2129  if (size >= 16) {
2130  avio_rb32(pb); /* reserved */
2131  avio_rb16(pb); /* reserved */
2132  dref_id = avio_rb16(pb);
2133  }else if (size <= 7){
2134  av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
2135  return AVERROR_INVALIDDATA;
2136  }
2137 
2138  if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format,
2139  size - (avio_tell(pb) - start_pos)))
2140  continue;
2141 
2142  sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
2143  sc->dref_id= dref_id;
2144 
2145  id = mov_codec_id(st, format);
2146 
2147  av_log(c->fc, AV_LOG_TRACE,
2148  "size=%"PRId64" 4CC= %c%c%c%c/0x%08x codec_type=%d\n", size,
2149  (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
2150  (format >> 24) & 0xff, format, st->codec->codec_type);
2151 
2152  if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
2153  st->codec->codec_id = id;
2154  mov_parse_stsd_video(c, pb, st, sc);
2155  } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
2156  st->codec->codec_id = id;
2157  mov_parse_stsd_audio(c, pb, st, sc);
2158  } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
2159  st->codec->codec_id = id;
2160  mov_parse_stsd_subtitle(c, pb, st, sc,
2161  size - (avio_tell(pb) - start_pos));
2162  } else {
2163  ret = mov_parse_stsd_data(c, pb, st, sc,
2164  size - (avio_tell(pb) - start_pos));
2165  if (ret < 0)
2166  return ret;
2167  }
2168  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
2169  a.size = size - (avio_tell(pb) - start_pos);
2170  if (a.size > 8) {
2171  if ((ret = mov_read_default(c, pb, a)) < 0)
2172  return ret;
2173  } else if (a.size > 0)
2174  avio_skip(pb, a.size);
2175  }
2176 
2177  if (pb->eof_reached)
2178  return AVERROR_EOF;
2179 
2180  return mov_finalize_stsd_codec(c, pb, st, sc);
2181 }
2182 
2184 {
2185  int entries;
2186 
2187  avio_r8(pb); /* version */
2188  avio_rb24(pb); /* flags */
2189  entries = avio_rb32(pb);
2190 
2191  return ff_mov_read_stsd_entries(c, pb, entries);
2192 }
2193 
2195 {
2196  AVStream *st;
2197  MOVStreamContext *sc;
2198  unsigned int i, entries;
2199 
2200  if (c->fc->nb_streams < 1)
2201  return 0;
2202  st = c->fc->streams[c->fc->nb_streams-1];
2203  sc = st->priv_data;
2204 
2205  avio_r8(pb); /* version */
2206  avio_rb24(pb); /* flags */
2207 
2208  entries = avio_rb32(pb);
2209  if ((uint64_t)entries * 12 + 4 > atom.size)
2210  return AVERROR_INVALIDDATA;
2211 
2212  av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
2213 
2214  if (!entries)
2215  return 0;
2216  if (sc->stsc_data)
2217  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n");
2218  av_free(sc->stsc_data);
2219  sc->stsc_count = 0;
2220  sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2221  if (!sc->stsc_data)
2222  return AVERROR(ENOMEM);
2223 
2224  for (i = 0; i < entries && !pb->eof_reached; i++) {
2225  sc->stsc_data[i].first = avio_rb32(pb);
2226  sc->stsc_data[i].count = avio_rb32(pb);
2227  sc->stsc_data[i].id = avio_rb32(pb);
2228  }
2229 
2230  sc->stsc_count = i;
2231  for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2232  int64_t first_min = i + 1;
2233  if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2234  (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2235  sc->stsc_data[i].first < first_min ||
2236  sc->stsc_data[i].count < 1 ||
2237  sc->stsc_data[i].id < 1) {
2238  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);
2239  if (i+1 >= sc->stsc_count) {
2240  sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2241  if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2242  sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2243  sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2244  sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
2245  continue;
2246  }
2247  av_assert0(sc->stsc_data[i+1].first >= 2);
2248  // We replace this entry by the next valid
2249  sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2250  sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2251  sc->stsc_data[i].id = sc->stsc_data[i+1].id;
2252  }
2253  }
2254 
2255  if (pb->eof_reached)
2256  return AVERROR_EOF;
2257 
2258  return 0;
2259 }
2260 
2262 {
2263  AVStream *st;
2264  MOVStreamContext *sc;
2265  unsigned i, entries;
2266 
2267  if (c->fc->nb_streams < 1)
2268  return 0;
2269  st = c->fc->streams[c->fc->nb_streams-1];
2270  sc = st->priv_data;
2271 
2272  avio_rb32(pb); // version + flags
2273 
2274  entries = avio_rb32(pb);
2275  if (sc->stps_data)
2276  av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n");
2277  av_free(sc->stps_data);
2278  sc->stps_count = 0;
2279  sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2280  if (!sc->stps_data)
2281  return AVERROR(ENOMEM);
2282 
2283  for (i = 0; i < entries && !pb->eof_reached; i++) {
2284  sc->stps_data[i] = avio_rb32(pb);
2285  //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
2286  }
2287 
2288  sc->stps_count = i;
2289 
2290  if (pb->eof_reached)
2291  return AVERROR_EOF;
2292 
2293  return 0;
2294 }
2295 
2297 {
2298  AVStream *st;
2299  MOVStreamContext *sc;
2300  unsigned int i, entries;
2301 
2302  if (c->fc->nb_streams < 1)
2303  return 0;
2304  st = c->fc->streams[c->fc->nb_streams-1];
2305  sc = st->priv_data;
2306 
2307  avio_r8(pb); /* version */
2308  avio_rb24(pb); /* flags */
2309 
2310  entries = avio_rb32(pb);
2311 
2312  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
2313 
2314  if (!entries)
2315  {
2316  sc->keyframe_absent = 1;
2317  if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2319  return 0;
2320  }
2321  if (sc->keyframes)
2322  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n");
2323  if (entries >= UINT_MAX / sizeof(int))
2324  return AVERROR_INVALIDDATA;
2325  av_freep(&sc->keyframes);
2326  sc->keyframe_count = 0;
2327  sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
2328  if (!sc->keyframes)
2329  return AVERROR(ENOMEM);
2330 
2331  for (i = 0; i < entries && !pb->eof_reached; i++) {
2332  sc->keyframes[i] = avio_rb32(pb);
2333  //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
2334  }
2335 
2336  sc->keyframe_count = i;
2337 
2338  if (pb->eof_reached)
2339  return AVERROR_EOF;
2340 
2341  return 0;
2342 }
2343 
2345 {
2346  AVStream *st;
2347  MOVStreamContext *sc;
2348  unsigned int i, entries, sample_size, field_size, num_bytes;
2349  GetBitContext gb;
2350  unsigned char* buf;
2351  int ret;
2352 
2353  if (c->fc->nb_streams < 1)
2354  return 0;
2355  st = c->fc->streams[c->fc->nb_streams-1];
2356  sc = st->priv_data;
2357 
2358  avio_r8(pb); /* version */
2359  avio_rb24(pb); /* flags */
2360 
2361  if (atom.type == MKTAG('s','t','s','z')) {
2362  sample_size = avio_rb32(pb);
2363  if (!sc->sample_size) /* do not overwrite value computed in stsd */
2364  sc->sample_size = sample_size;
2365  sc->stsz_sample_size = sample_size;
2366  field_size = 32;
2367  } else {
2368  sample_size = 0;
2369  avio_rb24(pb); /* reserved */
2370  field_size = avio_r8(pb);
2371  }
2372  entries = avio_rb32(pb);
2373 
2374  av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2375 
2376  sc->sample_count = entries;
2377  if (sample_size)
2378  return 0;
2379 
2380  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2381  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2382  return AVERROR_INVALIDDATA;
2383  }
2384 
2385  if (!entries)
2386  return 0;
2387  if (entries >= (UINT_MAX - 4) / field_size)
2388  return AVERROR_INVALIDDATA;
2389  if (sc->sample_sizes)
2390  av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n");
2391  av_free(sc->sample_sizes);
2392  sc->sample_count = 0;
2393  sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
2394  if (!sc->sample_sizes)
2395  return AVERROR(ENOMEM);
2396 
2397  num_bytes = (entries*field_size+4)>>3;
2398 
2399  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2400  if (!buf) {
2401  av_freep(&sc->sample_sizes);
2402  return AVERROR(ENOMEM);
2403  }
2404 
2405  ret = ffio_read_size(pb, buf, num_bytes);
2406  if (ret < 0) {
2407  av_freep(&sc->sample_sizes);
2408  av_free(buf);
2409  return ret;
2410  }
2411 
2412  init_get_bits(&gb, buf, 8*num_bytes);
2413 
2414  for (i = 0; i < entries && !pb->eof_reached; i++) {
2415  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2416  sc->data_size += sc->sample_sizes[i];
2417  }
2418 
2419  sc->sample_count = i;
2420 
2421  av_free(buf);
2422 
2423  if (pb->eof_reached)
2424  return AVERROR_EOF;
2425 
2426  return 0;
2427 }
2428 
2430 {
2431  AVStream *st;
2432  MOVStreamContext *sc;
2433  unsigned int i, entries;
2434  int64_t duration=0;
2435  int64_t total_sample_count=0;
2436 
2437  if (c->fc->nb_streams < 1)
2438  return 0;
2439  st = c->fc->streams[c->fc->nb_streams-1];
2440  sc = st->priv_data;
2441 
2442  avio_r8(pb); /* version */
2443  avio_rb24(pb); /* flags */
2444  entries = avio_rb32(pb);
2445 
2446  av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
2447  c->fc->nb_streams-1, entries);
2448 
2449  if (sc->stts_data)
2450  av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
2451  av_free(sc->stts_data);
2452  sc->stts_count = 0;
2453  sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data));
2454  if (!sc->stts_data)
2455  return AVERROR(ENOMEM);
2456 
2457  for (i = 0; i < entries && !pb->eof_reached; i++) {
2458  int sample_duration;
2459  int sample_count;
2460 
2461  sample_count=avio_rb32(pb);
2462  sample_duration = avio_rb32(pb);
2463 
2464  if (sample_count < 0) {
2465  av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2466  return AVERROR_INVALIDDATA;
2467  }
2468  sc->stts_data[i].count= sample_count;
2469  sc->stts_data[i].duration= sample_duration;
2470 
2471  av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2472  sample_count, sample_duration);
2473 
2474  if ( i+1 == entries
2475  && i
2476  && sample_count == 1
2477  && total_sample_count > 100
2478  && sample_duration/10 > duration / total_sample_count)
2479  sample_duration = duration / total_sample_count;
2480  duration+=(int64_t)sample_duration*(uint64_t)sample_count;
2481  total_sample_count+=sample_count;
2482  }
2483 
2484  sc->stts_count = i;
2485 
2486  if (duration > 0 &&
2487  duration <= INT64_MAX - sc->duration_for_fps &&
2488  total_sample_count <= INT64_MAX - sc->nb_frames_for_fps
2489  ) {
2490  sc->duration_for_fps += duration;
2491  sc->nb_frames_for_fps += total_sample_count;
2492  }
2493 
2494  if (pb->eof_reached)
2495  return AVERROR_EOF;
2496 
2497  st->nb_frames= total_sample_count;
2498  if (duration)
2499  st->duration= duration;
2500  sc->track_end = duration;
2501  return 0;
2502 }
2503 
2505 {
2506  if (duration < 0) {
2507  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2508  }
2509 }
2510 
2512 {
2513  AVStream *st;
2514  MOVStreamContext *sc;
2515  unsigned int i, entries;
2516 
2517  if (c->fc->nb_streams < 1)
2518  return 0;
2519  st = c->fc->streams[c->fc->nb_streams-1];
2520  sc = st->priv_data;
2521 
2522  avio_r8(pb); /* version */
2523  avio_rb24(pb); /* flags */
2524  entries = avio_rb32(pb);
2525 
2526  av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2527 
2528  if (!entries)
2529  return 0;
2530  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2531  return AVERROR_INVALIDDATA;
2532  av_freep(&sc->ctts_data);
2533  sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
2534  if (!sc->ctts_data)
2535  return AVERROR(ENOMEM);
2536 
2537  for (i = 0; i < entries && !pb->eof_reached; i++) {
2538  int count =avio_rb32(pb);
2539  int duration =avio_rb32(pb);
2540 
2541  sc->ctts_data[i].count = count;
2542  sc->ctts_data[i].duration= duration;
2543 
2544  av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
2545  count, duration);
2546 
2547  if (FFNABS(duration) < -(1<<28) && i+2<entries) {
2548  av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
2549  av_freep(&sc->ctts_data);
2550  sc->ctts_count = 0;
2551  return 0;
2552  }
2553 
2554  if (i+2<entries)
2555  mov_update_dts_shift(sc, duration);
2556  }
2557 
2558  sc->ctts_count = i;
2559 
2560  if (pb->eof_reached)
2561  return AVERROR_EOF;
2562 
2563  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
2564 
2565  return 0;
2566 }
2567 
2569 {
2570  AVStream *st;
2571  MOVStreamContext *sc;
2572  unsigned int i, entries;
2573  uint8_t version;
2574  uint32_t grouping_type;
2575 
2576  if (c->fc->nb_streams < 1)
2577  return 0;
2578  st = c->fc->streams[c->fc->nb_streams-1];
2579  sc = st->priv_data;
2580 
2581  version = avio_r8(pb); /* version */
2582  avio_rb24(pb); /* flags */
2583  grouping_type = avio_rl32(pb);
2584  if (grouping_type != MKTAG( 'r','a','p',' '))
2585  return 0; /* only support 'rap ' grouping */
2586  if (version == 1)
2587  avio_rb32(pb); /* grouping_type_parameter */
2588 
2589  entries = avio_rb32(pb);
2590  if (!entries)
2591  return 0;
2592  if (sc->rap_group)
2593  av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
2594  av_free(sc->rap_group);
2595  sc->rap_group_count = 0;
2596  sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
2597  if (!sc->rap_group)
2598  return AVERROR(ENOMEM);
2599 
2600  for (i = 0; i < entries && !pb->eof_reached; i++) {
2601  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2602  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2603  }
2604 
2605  sc->rap_group_count = i;
2606 
2607  return pb->eof_reached ? AVERROR_EOF : 0;
2608 }
2609 
2610 static void mov_build_index(MOVContext *mov, AVStream *st)
2611 {
2612  MOVStreamContext *sc = st->priv_data;
2613  int64_t current_offset;
2614  int64_t current_dts = 0;
2615  unsigned int stts_index = 0;
2616  unsigned int stsc_index = 0;
2617  unsigned int stss_index = 0;
2618  unsigned int stps_index = 0;
2619  unsigned int i, j;
2620  uint64_t stream_size = 0;
2621 
2622  if (sc->elst_count) {
2623  int i, edit_start_index = 0, unsupported = 0;
2624  int64_t empty_duration = 0; // empty duration of the first edit list entry
2625  int64_t start_time = 0; // start time of the media
2626 
2627  for (i = 0; i < sc->elst_count; i++) {
2628  const MOVElst *e = &sc->elst_data[i];
2629  if (i == 0 && e->time == -1) {
2630  /* if empty, the first entry is the start time of the stream
2631  * relative to the presentation itself */
2632  empty_duration = e->duration;
2633  edit_start_index = 1;
2634  } else if (i == edit_start_index && e->time >= 0) {
2635  start_time = e->time;
2636  } else
2637  unsupported = 1;
2638  }
2639  if (unsupported)
2640  av_log(mov->fc, AV_LOG_WARNING, "multiple edit list entries, "
2641  "a/v desync might occur, patch welcome\n");
2642 
2643  /* adjust first dts according to edit list */
2644  if ((empty_duration || start_time) && mov->time_scale > 0) {
2645  if (empty_duration)
2646  empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
2647  sc->time_offset = start_time - empty_duration;
2648  current_dts = -sc->time_offset;
2649  if (sc->ctts_count>0 && sc->stts_count>0 &&
2650  sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
2651  /* more than 16 frames delay, dts are likely wrong
2652  this happens with files created by iMovie */
2653  sc->wrong_dts = 1;
2654  st->codec->has_b_frames = 1;
2655  }
2656  }
2657  }
2658 
2659  /* only use old uncompressed audio chunk demuxing when stts specifies it */
2660  if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
2661  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2662  unsigned int current_sample = 0;
2663  unsigned int stts_sample = 0;
2664  unsigned int sample_size;
2665  unsigned int distance = 0;
2666  unsigned int rap_group_index = 0;
2667  unsigned int rap_group_sample = 0;
2668  int64_t last_dts = 0;
2669  int64_t dts_correction = 0;
2670  int rap_group_present = sc->rap_group_count && sc->rap_group;
2671  int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
2672 
2673  current_dts -= sc->dts_shift;
2674  last_dts = current_dts;
2675 
2676  if (!sc->sample_count || st->nb_index_entries)
2677  return;
2678  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2679  return;
2681  st->nb_index_entries + sc->sample_count,
2682  sizeof(*st->index_entries)) < 0) {
2683  st->nb_index_entries = 0;
2684  return;
2685  }
2687 
2688  for (i = 0; i < sc->chunk_count; i++) {
2689  int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
2690  current_offset = sc->chunk_offsets[i];
2691  while (stsc_index + 1 < sc->stsc_count &&
2692  i + 1 == sc->stsc_data[stsc_index + 1].first)
2693  stsc_index++;
2694 
2695  if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
2696  sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
2697  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
2698  sc->stsz_sample_size = sc->sample_size;
2699  }
2700  if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
2701  av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
2702  sc->stsz_sample_size = sc->sample_size;
2703  }
2704 
2705  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2706  int keyframe = 0;
2707  if (current_sample >= sc->sample_count) {
2708  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2709  return;
2710  }
2711 
2712  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2713  keyframe = 1;
2714  if (stss_index + 1 < sc->keyframe_count)
2715  stss_index++;
2716  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2717  keyframe = 1;
2718  if (stps_index + 1 < sc->stps_count)
2719  stps_index++;
2720  }
2721  if (rap_group_present && rap_group_index < sc->rap_group_count) {
2722  if (sc->rap_group[rap_group_index].index > 0)
2723  keyframe = 1;
2724  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2725  rap_group_sample = 0;
2726  rap_group_index++;
2727  }
2728  }
2729  if (sc->keyframe_absent
2730  && !sc->stps_count
2731  && !rap_group_present
2732  && (st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (i==0 && j==0)))
2733  keyframe = 1;
2734  if (keyframe)
2735  distance = 0;
2736  sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
2737  if (sc->pseudo_stream_id == -1 ||
2738  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2739  AVIndexEntry *e;
2740  if (sample_size > 0x3FFFFFFF) {
2741  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", sample_size);
2742  return;
2743  }
2744  e = &st->index_entries[st->nb_index_entries++];
2745  e->pos = current_offset;
2746  e->timestamp = current_dts;
2747  e->size = sample_size;
2748  e->min_distance = distance;
2749  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2750  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2751  "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2752  current_offset, current_dts, sample_size, distance, keyframe);
2753  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->nb_index_entries < 100)
2754  ff_rfps_add_frame(mov->fc, st, current_dts);
2755  }
2756 
2757  current_offset += sample_size;
2758  stream_size += sample_size;
2759 
2760  /* A negative sample duration is invalid based on the spec,
2761  * but some samples need it to correct the DTS. */
2762  if (sc->stts_data[stts_index].duration < 0) {
2763  av_log(mov->fc, AV_LOG_WARNING,
2764  "Invalid SampleDelta %d in STTS, at %d st:%d\n",
2765  sc->stts_data[stts_index].duration, stts_index,
2766  st->index);
2767  dts_correction += sc->stts_data[stts_index].duration - 1;
2768  sc->stts_data[stts_index].duration = 1;
2769  }
2770  current_dts += sc->stts_data[stts_index].duration;
2771  if (!dts_correction || current_dts + dts_correction > last_dts) {
2772  current_dts += dts_correction;
2773  dts_correction = 0;
2774  } else {
2775  /* Avoid creating non-monotonous DTS */
2776  dts_correction += current_dts - last_dts - 1;
2777  current_dts = last_dts + 1;
2778  }
2779  last_dts = current_dts;
2780  distance++;
2781  stts_sample++;
2782  current_sample++;
2783  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2784  stts_sample = 0;
2785  stts_index++;
2786  }
2787  }
2788  }
2789  if (st->duration > 0)
2790  st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2791  } else {
2792  unsigned chunk_samples, total = 0;
2793 
2794  if (!sc->chunk_count)
2795  return;
2796 
2797  // compute total chunk count
2798  for (i = 0; i < sc->stsc_count; i++) {
2799  unsigned count, chunk_count;
2800 
2801  chunk_samples = sc->stsc_data[i].count;
2802  if (i != sc->stsc_count - 1 &&
2803  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2804  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2805  return;
2806  }
2807 
2808  if (sc->samples_per_frame >= 160) { // gsm
2809  count = chunk_samples / sc->samples_per_frame;
2810  } else if (sc->samples_per_frame > 1) {
2811  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2812  count = (chunk_samples+samples-1) / samples;
2813  } else {
2814  count = (chunk_samples+1023) / 1024;
2815  }
2816 
2817  if (i < sc->stsc_count - 1)
2818  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2819  else
2820  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2821  total += chunk_count * count;
2822  }
2823 
2824  av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
2825  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2826  return;
2828  st->nb_index_entries + total,
2829  sizeof(*st->index_entries)) < 0) {
2830  st->nb_index_entries = 0;
2831  return;
2832  }
2833  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2834 
2835  // populate index
2836  for (i = 0; i < sc->chunk_count; i++) {
2837  current_offset = sc->chunk_offsets[i];
2838  if (stsc_index + 1 < sc->stsc_count &&
2839  i + 1 == sc->stsc_data[stsc_index + 1].first)
2840  stsc_index++;
2841  chunk_samples = sc->stsc_data[stsc_index].count;
2842 
2843  while (chunk_samples > 0) {
2844  AVIndexEntry *e;
2845  unsigned size, samples;
2846 
2847  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
2849  "Zero bytes per frame, but %d samples per frame",
2850  sc->samples_per_frame);
2851  return;
2852  }
2853 
2854  if (sc->samples_per_frame >= 160) { // gsm
2855  samples = sc->samples_per_frame;
2856  size = sc->bytes_per_frame;
2857  } else {
2858  if (sc->samples_per_frame > 1) {
2859  samples = FFMIN((1024 / sc->samples_per_frame)*
2860  sc->samples_per_frame, chunk_samples);
2861  size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2862  } else {
2863  samples = FFMIN(1024, chunk_samples);
2864  size = samples * sc->sample_size;
2865  }
2866  }
2867 
2868  if (st->nb_index_entries >= total) {
2869  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2870  return;
2871  }
2872  if (size > 0x3FFFFFFF) {
2873  av_log(mov->fc, AV_LOG_ERROR, "Sample size %u is too large\n", size);
2874  return;
2875  }
2876  e = &st->index_entries[st->nb_index_entries++];
2877  e->pos = current_offset;
2878  e->timestamp = current_dts;
2879  e->size = size;
2880  e->min_distance = 0;
2881  e->flags = AVINDEX_KEYFRAME;
2882  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2883  "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2884  size, samples);
2885 
2886  current_offset += size;
2887  current_dts += samples;
2888  chunk_samples -= samples;
2889  }
2890  }
2891  }
2892 }
2893 
2894 static int test_same_origin(const char *src, const char *ref) {
2895  char src_proto[64];
2896  char ref_proto[64];
2897  char src_auth[256];
2898  char ref_auth[256];
2899  char src_host[256];
2900  char ref_host[256];
2901  int src_port=-1;
2902  int ref_port=-1;
2903 
2904  av_url_split(src_proto, sizeof(src_proto), src_auth, sizeof(src_auth), src_host, sizeof(src_host), &src_port, NULL, 0, src);
2905  av_url_split(ref_proto, sizeof(ref_proto), ref_auth, sizeof(ref_auth), ref_host, sizeof(ref_host), &ref_port, NULL, 0, ref);
2906 
2907  if (strlen(src) == 0) {
2908  return -1;
2909  } else if (strlen(src_auth) + 1 >= sizeof(src_auth) ||
2910  strlen(ref_auth) + 1 >= sizeof(ref_auth) ||
2911  strlen(src_host) + 1 >= sizeof(src_host) ||
2912  strlen(ref_host) + 1 >= sizeof(ref_host)) {
2913  return 0;
2914  } else if (strcmp(src_proto, ref_proto) ||
2915  strcmp(src_auth, ref_auth) ||
2916  strcmp(src_host, ref_host) ||
2917  src_port != ref_port) {
2918  return 0;
2919  } else
2920  return 1;
2921 }
2922 
2923 static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref,
2925 {
2926  AVOpenCallback open_func = c->fc->open_cb;
2927 
2928  if (!open_func)
2929  open_func = ffio_open2_wrapper;
2930 
2931  /* try relative path, we do not try the absolute because it can leak information about our
2932  system to an attacker */
2933  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2934  char filename[1025];
2935  const char *src_path;
2936  int i, l;
2937 
2938  /* find a source dir */
2939  src_path = strrchr(src, '/');
2940  if (src_path)
2941  src_path++;
2942  else
2943  src_path = src;
2944 
2945  /* find a next level down to target */
2946  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2947  if (ref->path[l] == '/') {
2948  if (i == ref->nlvl_to - 1)
2949  break;
2950  else
2951  i++;
2952  }
2953 
2954  /* compose filename if next level down to target was found */
2955  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2956  memcpy(filename, src, src_path - src);
2957  filename[src_path - src] = 0;
2958 
2959  for (i = 1; i < ref->nlvl_from; i++)
2960  av_strlcat(filename, "../", sizeof(filename));
2961 
2962  av_strlcat(filename, ref->path + l + 1, sizeof(filename));
2963  if (!c->use_absolute_path && !c->fc->open_cb) {
2964  int same_origin = test_same_origin(src, filename);
2965 
2966  if (!same_origin) {
2967  av_log(c->fc, AV_LOG_ERROR,
2968  "Reference with mismatching origin, %s not tried for security reasons, "
2969  "set demuxer option use_absolute_path to allow it anyway\n",
2970  ref->path);
2971  return AVERROR(ENOENT);
2972  }
2973 
2974  if(strstr(ref->path + l + 1, "..") ||
2975  strstr(ref->path + l + 1, ":") ||
2976  (ref->nlvl_from > 1 && same_origin < 0) ||
2977  (filename[0] == '/' && src_path == src))
2978  return AVERROR(ENOENT);
2979  }
2980 
2981  if (strlen(filename) + 1 == sizeof(filename))
2982  return AVERROR(ENOENT);
2983  if (!open_func(c->fc, pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2984  return 0;
2985  }
2986  } else if (c->use_absolute_path) {
2987  av_log(c->fc, AV_LOG_WARNING, "Using absolute path on user request, "
2988  "this is a possible security issue\n");
2989  if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2990  return 0;
2991  } else if (c->fc->open_cb) {
2992  if (!open_func(c->fc, pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
2993  return 0;
2994  } else {
2995  av_log(c->fc, AV_LOG_ERROR,
2996  "Absolute path %s not tried for security reasons, "
2997  "set demuxer option use_absolute_path to allow absolute paths\n",
2998  ref->path);
2999  }
3000 
3001  return AVERROR(ENOENT);
3002 }
3003 
3005 {
3006  if (sc->time_scale <= 0) {
3007  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
3008  sc->time_scale = c->time_scale;
3009  if (sc->time_scale <= 0)
3010  sc->time_scale = 1;
3011  }
3012 }
3013 
3015 {
3016  AVStream *st;
3017  MOVStreamContext *sc;
3018  int ret;
3019 
3020  st = avformat_new_stream(c->fc, NULL);
3021  if (!st) return AVERROR(ENOMEM);
3022  st->id = c->fc->nb_streams;
3023  sc = av_mallocz(sizeof(MOVStreamContext));
3024  if (!sc) return AVERROR(ENOMEM);
3025 
3026  st->priv_data = sc;
3028  sc->ffindex = st->index;
3029 
3030  if ((ret = mov_read_default(c, pb, atom)) < 0)
3031  return ret;
3032 
3033  /* sanity checks */
3034  if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
3035  (!sc->sample_size && !sc->sample_count))) {
3036  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
3037  st->index);
3038  return 0;
3039  }
3040  if (sc->chunk_count && sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
3041  av_log(c->fc, AV_LOG_ERROR, "stream %d, contradictionary STSC and STCO\n",
3042  st->index);
3043  return AVERROR_INVALIDDATA;
3044  }
3045 
3046  fix_timescale(c, sc);
3047 
3048  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
3049 
3050  mov_build_index(c, st);
3051 
3052  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
3053  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
3054  if (c->enable_drefs) {
3055  if (mov_open_dref(c, &sc->pb, c->fc->filename, dref,
3056  &c->fc->interrupt_callback) < 0)
3057  av_log(c->fc, AV_LOG_ERROR,
3058  "stream %d, error opening alias: path='%s', dir='%s', "
3059  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
3060  st->index, dref->path, dref->dir, dref->filename,
3061  dref->volume, dref->nlvl_from, dref->nlvl_to);
3062  } else {
3063  av_log(c->fc, AV_LOG_WARNING,
3064  "Skipped opening external track: "
3065  "stream %d, alias: path='%s', dir='%s', "
3066  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
3067  "Set enable_drefs to allow this.\n",
3068  st->index, dref->path, dref->dir, dref->filename,
3069  dref->volume, dref->nlvl_from, dref->nlvl_to);
3070  }
3071  } else {
3072  sc->pb = c->fc->pb;
3073  sc->pb_is_copied = 1;
3074  }
3075 
3076  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
3077  if (!st->sample_aspect_ratio.num && st->codec->width && st->codec->height &&
3078  sc->height && sc->width &&
3079  (st->codec->width != sc->width || st->codec->height != sc->height)) {
3080  st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
3081  ((double)st->codec->width * sc->height), INT_MAX);
3082  }
3083 
3084 #if FF_API_R_FRAME_RATE
3085  if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
3087  sc->time_scale, sc->stts_data[0].duration, INT_MAX);
3088 #endif
3089  }
3090 
3091  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
3092  if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
3093  TAG_IS_AVCI(st->codec->codec_tag)) {
3094  ret = ff_generate_avci_extradata(st);
3095  if (ret < 0)
3096  return ret;
3097  }
3098 
3099  switch (st->codec->codec_id) {
3100 #if CONFIG_H261_DECODER
3101  case AV_CODEC_ID_H261:
3102 #endif
3103 #if CONFIG_H263_DECODER
3104  case AV_CODEC_ID_H263:
3105 #endif
3106 #if CONFIG_MPEG4_DECODER
3107  case AV_CODEC_ID_MPEG4:
3108 #endif
3109  st->codec->width = 0; /* let decoder init width/height */
3110  st->codec->height= 0;
3111  break;
3112  }
3113 
3114  /* Do not need those anymore. */
3115  av_freep(&sc->chunk_offsets);
3116  av_freep(&sc->stsc_data);
3117  av_freep(&sc->sample_sizes);
3118  av_freep(&sc->keyframes);
3119  av_freep(&sc->stts_data);
3120  av_freep(&sc->stps_data);
3121  av_freep(&sc->elst_data);
3122  av_freep(&sc->rap_group);
3123 
3124  return 0;
3125 }
3126 
3128 {
3129  int ret;
3130  c->itunes_metadata = 1;
3131  ret = mov_read_default(c, pb, atom);
3132  c->itunes_metadata = 0;
3133  return ret;
3134 }
3135 
3137 {
3138  int64_t end = avio_tell(pb) + size;
3139  uint8_t *key = NULL, *val = NULL;
3140  int i;
3141  AVStream *st;
3142  MOVStreamContext *sc;
3143 
3144  if (c->fc->nb_streams < 1)
3145  return 0;
3146  st = c->fc->streams[c->fc->nb_streams-1];
3147  sc = st->priv_data;
3148 
3149  for (i = 0; i < 2; i++) {
3150  uint8_t **p;
3151  uint32_t len, tag;
3152  int ret;
3153 
3154  if (end - avio_tell(pb) <= 12)
3155  break;
3156 
3157  len = avio_rb32(pb);
3158  tag = avio_rl32(pb);
3159  avio_skip(pb, 4); // flags
3160 
3161  if (len < 12 || len - 12 > end - avio_tell(pb))
3162  break;
3163  len -= 12;
3164 
3165  if (tag == MKTAG('n', 'a', 'm', 'e'))
3166  p = &key;
3167  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
3168  avio_skip(pb, 4);
3169  len -= 4;
3170  p = &val;
3171  } else
3172  break;
3173 
3174  *p = av_malloc(len + 1);
3175  if (!*p)
3176  break;
3177  ret = ffio_read_size(pb, *p, len);
3178  if (ret < 0) {
3179  av_freep(p);
3180  return ret;
3181  }
3182  (*p)[len] = 0;
3183  }
3184 
3185  if (key && val) {
3186  if (strcmp(key, "iTunSMPB") == 0) {
3187  int priming, remainder, samples;
3188  if(sscanf(val, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
3189  if(priming>0 && priming<16384)
3190  sc->start_pad = priming;
3191  }
3192  }
3193  if (strcmp(key, "cdec") != 0) {
3194  av_dict_set(&c->fc->metadata, key, val,
3196  key = val = NULL;
3197  }
3198  }
3199 
3200  avio_seek(pb, end, SEEK_SET);
3201  av_freep(&key);
3202  av_freep(&val);
3203  return 0;
3204 }
3205 
3207 {
3208  int64_t end = avio_tell(pb) + atom.size;
3209  uint32_t tag, len;
3210 
3211  if (atom.size < 8)
3212  goto fail;
3213 
3214  len = avio_rb32(pb);
3215  tag = avio_rl32(pb);
3216 
3217  if (len > atom.size)
3218  goto fail;
3219 
3220  if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
3221  uint8_t domain[128];
3222  int domain_len;
3223 
3224  avio_skip(pb, 4); // flags
3225  len -= 12;
3226 
3227  domain_len = avio_get_str(pb, len, domain, sizeof(domain));
3228  avio_skip(pb, len - domain_len);
3229  return mov_read_custom_2plus(c, pb, end - avio_tell(pb));
3230  }
3231 
3232 fail:
3233  av_log(c->fc, AV_LOG_VERBOSE,
3234  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
3235  return 0;
3236 }
3237 
3239 {
3240  while (atom.size > 8) {
3241  uint32_t tag = avio_rl32(pb);
3242  atom.size -= 4;
3243  if (tag == MKTAG('h','d','l','r')) {
3244  avio_seek(pb, -8, SEEK_CUR);
3245  atom.size += 8;
3246  return mov_read_default(c, pb, atom);
3247  }
3248  }
3249  return 0;
3250 }
3251 
3253 {
3254  int i;
3255  int width;
3256  int height;
3257  int display_matrix[3][3];
3258  AVStream *st;
3259  MOVStreamContext *sc;
3260  int version;
3261  int flags;
3262 
3263  if (c->fc->nb_streams < 1)
3264  return 0;
3265  st = c->fc->streams[c->fc->nb_streams-1];
3266  sc = st->priv_data;
3267 
3268  version = avio_r8(pb);
3269  flags = avio_rb24(pb);
3271 
3272  if (version == 1) {
3273  avio_rb64(pb);
3274  avio_rb64(pb);
3275  } else {
3276  avio_rb32(pb); /* creation time */
3277  avio_rb32(pb); /* modification time */
3278  }
3279  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
3280  avio_rb32(pb); /* reserved */
3281 
3282  /* highlevel (considering edits) duration in movie timebase */
3283  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
3284  avio_rb32(pb); /* reserved */
3285  avio_rb32(pb); /* reserved */
3286 
3287  avio_rb16(pb); /* layer */
3288  avio_rb16(pb); /* alternate group */
3289  avio_rb16(pb); /* volume */
3290  avio_rb16(pb); /* reserved */
3291 
3292  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
3293  // they're kept in fixed point format through all calculations
3294  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
3295  // side data, but the scale factor is not needed to calculate aspect ratio
3296  for (i = 0; i < 3; i++) {
3297  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
3298  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
3299  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
3300  }
3301 
3302  width = avio_rb32(pb); // 16.16 fixed point track width
3303  height = avio_rb32(pb); // 16.16 fixed point track height
3304  sc->width = width >> 16;
3305  sc->height = height >> 16;
3306 
3307  // save the matrix and add rotate metadata when it is not the default
3308  // identity
3309  if (display_matrix[0][0] != (1 << 16) ||
3310  display_matrix[1][1] != (1 << 16) ||
3311  display_matrix[2][2] != (1 << 30) ||
3312  display_matrix[0][1] || display_matrix[0][2] ||
3313  display_matrix[1][0] || display_matrix[1][2] ||
3314  display_matrix[2][0] || display_matrix[2][1]) {
3315  int i, j;
3316  double rotate;
3317 
3318  av_freep(&sc->display_matrix);
3319  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
3320  if (!sc->display_matrix)
3321  return AVERROR(ENOMEM);
3322 
3323  for (i = 0; i < 3; i++)
3324  for (j = 0; j < 3; j++)
3325  sc->display_matrix[i * 3 + j] = display_matrix[i][j];
3326 
3328  if (!isnan(rotate)) {
3329  char rotate_buf[64];
3330  rotate = -rotate;
3331  if (rotate < 0) // for backward compatibility
3332  rotate += 360;
3333  snprintf(rotate_buf, sizeof(rotate_buf), "%g", rotate);
3334  av_dict_set(&st->metadata, "rotate", rotate_buf, 0);
3335  }
3336  }
3337 
3338  // transform the display width/height according to the matrix
3339  // to keep the same scale, use [width height 1<<16]
3340  if (width && height && sc->display_matrix) {
3341  double disp_transform[2];
3342 
3343 #define SQR(a) ((a)*(double)(a))
3344  for (i = 0; i < 2; i++)
3345  disp_transform[i] = sqrt(SQR(display_matrix[i][0]) + SQR(display_matrix[i][1]));
3346 
3347  if (disp_transform[0] > 0 && disp_transform[1] > 0 &&
3348  disp_transform[0] < (1<<24) && disp_transform[1] < (1<<24) &&
3349  fabs((disp_transform[0] / disp_transform[1]) - 1.0) > 0.01)
3351  disp_transform[0] / disp_transform[1],
3352  INT_MAX);
3353  }
3354  return 0;
3355 }
3356 
3358 {
3359  MOVFragment *frag = &c->fragment;
3360  MOVTrackExt *trex = NULL;
3362  int flags, track_id, i, found = 0;
3363 
3364  avio_r8(pb); /* version */
3365  flags = avio_rb24(pb);
3366 
3367  track_id = avio_rb32(pb);
3368  if (!track_id)
3369  return AVERROR_INVALIDDATA;
3370  frag->track_id = track_id;
3371  for (i = 0; i < c->trex_count; i++)
3372  if (c->trex_data[i].track_id == frag->track_id) {
3373  trex = &c->trex_data[i];
3374  break;
3375  }
3376  if (!trex) {
3377  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
3378  return AVERROR_INVALIDDATA;
3379  }
3380 
3383  frag->moof_offset : frag->implicit_offset;
3384  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
3385 
3386  frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
3387  avio_rb32(pb) : trex->duration;
3388  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
3389  avio_rb32(pb) : trex->size;
3390  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
3391  avio_rb32(pb) : trex->flags;
3392  frag->time = AV_NOPTS_VALUE;
3393  for (i = 0; i < c->fragment_index_count; i++) {
3394  int j;
3395  MOVFragmentIndex* candidate = c->fragment_index_data[i];
3396  if (candidate->track_id == frag->track_id) {
3397  av_log(c->fc, AV_LOG_DEBUG,
3398  "found fragment index for track %u\n", frag->track_id);
3399  index = candidate;
3400  for (j = index->current_item; j < index->item_count; j++) {
3401  if (frag->implicit_offset == index->items[j].moof_offset) {
3402  av_log(c->fc, AV_LOG_DEBUG, "found fragment index entry "
3403  "for track %u and moof_offset %"PRId64"\n",
3404  frag->track_id, index->items[j].moof_offset);
3405  frag->time = index->items[j].time;
3406  index->current_item = j + 1;
3407  found = 1;
3408  break;
3409  }
3410  }
3411  if (found)
3412  break;
3413  }
3414  }
3415  if (index && !found) {
3416  av_log(c->fc, AV_LOG_DEBUG, "track %u has a fragment index but "
3417  "it doesn't have an (in-order) entry for moof_offset "
3418  "%"PRId64"\n", frag->track_id, frag->implicit_offset);
3419  }
3420  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
3421  return 0;
3422 }
3423 
3425 {
3426  c->chapter_track = avio_rb32(pb);
3427  return 0;
3428 }
3429 
3431 {
3432  MOVTrackExt *trex;
3433  int err;
3434 
3435  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
3436  return AVERROR_INVALIDDATA;
3437  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
3438  sizeof(*c->trex_data))) < 0) {
3439  c->trex_count = 0;
3440  return err;
3441  }
3442 
3443  c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
3444 
3445  trex = &c->trex_data[c->trex_count++];
3446  avio_r8(pb); /* version */
3447  avio_rb24(pb); /* flags */
3448  trex->track_id = avio_rb32(pb);
3449  trex->stsd_id = avio_rb32(pb);
3450  trex->duration = avio_rb32(pb);
3451  trex->size = avio_rb32(pb);
3452  trex->flags = avio_rb32(pb);
3453  return 0;
3454 }
3455 
3457 {
3458  MOVFragment *frag = &c->fragment;
3459  AVStream *st = NULL;
3460  MOVStreamContext *sc;
3461  int version, i;
3462 
3463  for (i = 0; i < c->fc->nb_streams; i++) {
3464  if (c->fc->streams[i]->id == frag->track_id) {
3465  st = c->fc->streams[i];
3466  break;
3467  }
3468  }
3469  if (!st) {
3470  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3471  return AVERROR_INVALIDDATA;
3472  }
3473  sc = st->priv_data;
3474  if (sc->pseudo_stream_id + 1 != frag->stsd_id)
3475  return 0;
3476  version = avio_r8(pb);
3477  avio_rb24(pb); /* flags */
3478  if (version) {
3479  sc->track_end = avio_rb64(pb);
3480  } else {
3481  sc->track_end = avio_rb32(pb);
3482  }
3483  return 0;
3484 }
3485 
3487 {
3488  MOVFragment *frag = &c->fragment;
3489  AVStream *st = NULL;
3490  MOVStreamContext *sc;
3491  MOVStts *ctts_data;
3492  uint64_t offset;
3493  int64_t dts;
3494  int data_offset = 0;
3495  unsigned entries, first_sample_flags = frag->flags;
3496  int flags, distance, i, found_keyframe = 0, err;
3497 
3498  for (i = 0; i < c->fc->nb_streams; i++) {
3499  if (c->fc->streams[i]->id == frag->track_id) {
3500  st = c->fc->streams[i];
3501  break;
3502  }
3503  }
3504  if (!st) {
3505  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
3506  return AVERROR_INVALIDDATA;
3507  }
3508  sc = st->priv_data;
3509  if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
3510  return 0;
3511  avio_r8(pb); /* version */
3512  flags = avio_rb24(pb);
3513  entries = avio_rb32(pb);
3514  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
3515 
3516  /* Always assume the presence of composition time offsets.
3517  * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
3518  * 1) in the initial movie, there are no samples.
3519  * 2) in the first movie fragment, there is only one sample without composition time offset.
3520  * 3) in the subsequent movie fragments, there are samples with composition time offset. */
3521  if (!sc->ctts_count && sc->sample_count)
3522  {
3523  /* Complement ctts table if moov atom doesn't have ctts atom. */
3524  ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
3525  if (!ctts_data)
3526  return AVERROR(ENOMEM);
3527  sc->ctts_data = ctts_data;
3528  sc->ctts_data[sc->ctts_count].count = sc->sample_count;
3529  sc->ctts_data[sc->ctts_count].duration = 0;
3530  sc->ctts_count++;
3531  }
3532  if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
3533  return AVERROR_INVALIDDATA;
3534  if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
3535  sizeof(*sc->ctts_data))) < 0) {
3536  sc->ctts_count = 0;
3537  return err;
3538  }
3539  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
3540  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
3541  dts = sc->track_end - sc->time_offset;
3542  offset = frag->base_data_offset + data_offset;
3543  distance = 0;
3544  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
3545  for (i = 0; i < entries && !pb->eof_reached; i++) {
3546  unsigned sample_size = frag->size;
3547  int sample_flags = i ? frag->flags : first_sample_flags;
3548  unsigned sample_duration = frag->duration;
3549  int keyframe = 0;
3550 
3551  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
3552  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
3553  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
3554  sc->ctts_data[sc->ctts_count].count = 1;
3555  sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
3556  avio_rb32(pb) : 0;
3558  if (frag->time != AV_NOPTS_VALUE) {
3559  if (c->use_mfra_for == FF_MOV_FLAG_MFRA_PTS) {
3560  int64_t pts = frag->time;
3561  av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3562  " sc->dts_shift %d ctts.duration %d"
3563  " sc->time_offset %"PRId64" flags & MOV_TRUN_SAMPLE_CTS %d\n", pts,
3564  sc->dts_shift, sc->ctts_data[sc->ctts_count].duration,
3565  sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
3566  dts = pts - sc->dts_shift;
3567  if (flags & MOV_TRUN_SAMPLE_CTS) {
3568  dts -= sc->ctts_data[sc->ctts_count].duration;
3569  } else {
3570  dts -= sc->time_offset;
3571  }
3572  av_log(c->fc, AV_LOG_DEBUG, "calculated into dts %"PRId64"\n", dts);
3573  } else {
3574  dts = frag->time - sc->time_offset;
3575  av_log(c->fc, AV_LOG_DEBUG, "found frag time %"PRId64
3576  ", using it for dts\n", dts);
3577  }
3578  frag->time = AV_NOPTS_VALUE;
3579  }
3580  sc->ctts_count++;
3581  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
3582  keyframe = 1;
3583  else if (!found_keyframe)
3584  keyframe = found_keyframe =
3585  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
3587  if (keyframe)
3588  distance = 0;
3589  err = av_add_index_entry(st, offset, dts, sample_size, distance,
3590  keyframe ? AVINDEX_KEYFRAME : 0);
3591  if (err < 0) {
3592  av_log(c->fc, AV_LOG_ERROR, "Failed to add index entry\n");
3593  }
3594  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
3595  "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
3596  offset, dts, sample_size, distance, keyframe);
3597  distance++;
3598  dts += sample_duration;
3599  offset += sample_size;
3600  sc->data_size += sample_size;
3601 
3602  if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
3603  1 <= INT64_MAX - sc->nb_frames_for_fps
3604  ) {
3605  sc->duration_for_fps += sample_duration;
3606  sc->nb_frames_for_fps ++;
3607  }
3608  }
3609 
3610  if (pb->eof_reached)
3611  return AVERROR_EOF;
3612 
3613  frag->implicit_offset = offset;
3614 
3615  sc->track_end = dts + sc->time_offset;
3616  if (st->duration < sc->track_end)
3617  st->duration = sc->track_end;
3618 
3619  return 0;
3620 }
3621 
3623 {
3624  int64_t offset = avio_tell(pb) + atom.size, pts;
3625  uint8_t version;
3626  unsigned i, track_id;
3627  AVStream *st = NULL;
3628  MOVStreamContext *sc;
3630  MOVFragmentIndex **tmp;
3631  AVRational timescale;
3632 
3633  version = avio_r8(pb);
3634  if (version > 1) {
3635  avpriv_request_sample(c->fc, "sidx version %u", version);
3636  return 0;
3637  }
3638 
3639  avio_rb24(pb); // flags
3640 
3641  track_id = avio_rb32(pb); // Reference ID
3642  for (i = 0; i < c->fc->nb_streams; i++) {
3643  if (c->fc->streams[i]->id == track_id) {
3644  st = c->fc->streams[i];
3645  break;
3646  }
3647  }
3648  if (!st) {
3649  av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);
3650  return 0;
3651  }
3652 
3653  sc = st->priv_data;
3654 
3655  timescale = av_make_q(1, avio_rb32(pb));
3656 
3657  if (version == 0) {
3658  pts = avio_rb32(pb);
3659  offset += avio_rb32(pb);
3660  } else {
3661  pts = avio_rb64(pb);
3662  offset += avio_rb64(pb);
3663  }
3664 
3665  avio_rb16(pb); // reserved
3666 
3667  index = av_mallocz(sizeof(MOVFragmentIndex));
3668  if (!index)
3669  return AVERROR(ENOMEM);
3670 
3671  index->track_id = track_id;
3672 
3673  index->item_count = avio_rb16(pb);
3674  index->items = av_mallocz_array(index->item_count, sizeof(MOVFragmentIndexItem));
3675 
3676  if (!index->items) {
3677  av_freep(&index);
3678  return AVERROR(ENOMEM);
3679  }
3680 
3681  for (i = 0; i < index->item_count; i++) {
3682  uint32_t size = avio_rb32(pb);
3683  uint32_t duration = avio_rb32(pb);
3684  if (size & 0x80000000) {
3685  avpriv_request_sample(c->fc, "sidx reference_type 1");
3686  av_freep(&index->items);
3687  av_freep(&index);
3688  return AVERROR_PATCHWELCOME;
3689  }
3690  avio_rb32(pb); // sap_flags
3691  index->items[i].moof_offset = offset;
3692  index->items[i].time = av_rescale_q(pts, st->time_base, timescale);
3693  offset += size;
3694  pts += duration;
3695  }
3696 
3697  st->duration = sc->track_end = pts;
3698 
3700  c->fragment_index_count + 1,
3701  sizeof(MOVFragmentIndex*));
3702  if (!tmp) {
3703  av_freep(&index->items);
3704  av_freep(&index);
3705  return AVERROR(ENOMEM);
3706  }
3707 
3708  c->fragment_index_data = tmp;
3710 
3711  if (offset == avio_size(pb))
3712  c->fragment_index_complete = 1;
3713 
3714  return 0;
3715 }
3716 
3717 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
3718 /* like the files created with Adobe Premiere 5.0, for samples see */
3719 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
3721 {
3722  int err;
3723 
3724  if (atom.size < 8)
3725  return 0; /* continue */
3726  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3727  avio_skip(pb, atom.size - 4);
3728  return 0;
3729  }
3730  atom.type = avio_rl32(pb);
3731  atom.size -= 8;
3732  if (atom.type != MKTAG('m','d','a','t')) {
3733  avio_skip(pb, atom.size);
3734  return 0;
3735  }
3736  err = mov_read_mdat(c, pb, atom);
3737  return err;
3738 }
3739 
3741 {
3742 #if CONFIG_ZLIB
3743  AVIOContext ctx;
3744  uint8_t *cmov_data;
3745  uint8_t *moov_data; /* uncompressed data */
3746  long cmov_len, moov_len;
3747  int ret = -1;
3748 
3749  avio_rb32(pb); /* dcom atom */
3750  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3751  return AVERROR_INVALIDDATA;
3752  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3753  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
3754  return AVERROR_INVALIDDATA;
3755  }
3756  avio_rb32(pb); /* cmvd atom */
3757  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3758  return AVERROR_INVALIDDATA;
3759  moov_len = avio_rb32(pb); /* uncompressed size */
3760  cmov_len = atom.size - 6 * 4;
3761 
3762  cmov_data = av_malloc(cmov_len);
3763  if (!cmov_data)
3764  return AVERROR(ENOMEM);
3765  moov_data = av_malloc(moov_len);
3766  if (!moov_data) {
3767  av_free(cmov_data);
3768  return AVERROR(ENOMEM);
3769  }
3770  ret = ffio_read_size(pb, cmov_data, cmov_len);
3771  if (ret < 0)
3772  goto free_and_return;
3773 
3774  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3775  goto free_and_return;
3776  if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3777  goto free_and_return;
3779  atom.type = MKTAG('m','o','o','v');
3780  atom.size = moov_len;
3781  ret = mov_read_default(c, &ctx, atom);
3782 free_and_return:
3783  av_free(moov_data);
3784  av_free(cmov_data);
3785  return ret;
3786 #else
3787  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3788  return AVERROR(ENOSYS);
3789 #endif
3790 }
3791 
3792 /* edit list atom */
3794 {
3795  MOVStreamContext *sc;
3796  int i, edit_count, version;
3797 
3798  if (c->fc->nb_streams < 1 || c->ignore_editlist)
3799  return 0;
3800  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3801 
3802  version = avio_r8(pb); /* version */
3803  avio_rb24(pb); /* flags */
3804  edit_count = avio_rb32(pb); /* entries */
3805 
3806  if (!edit_count)
3807  return 0;
3808  if (sc->elst_data)
3809  av_log(c->fc, AV_LOG_WARNING, "Duplicated ELST atom\n");
3810  av_free(sc->elst_data);
3811  sc->elst_count = 0;
3812  sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
3813  if (!sc->elst_data)
3814  return AVERROR(ENOMEM);
3815 
3816  av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3817  for (i = 0; i < edit_count && !pb->eof_reached; i++) {
3818  MOVElst *e = &sc->elst_data[i];
3819 
3820  if (version == 1) {
3821  e->duration = avio_rb64(pb);
3822  e->time = avio_rb64(pb);
3823  } else {
3824  e->duration = avio_rb32(pb); /* segment duration */
3825  e->time = (int32_t)avio_rb32(pb); /* media time */
3826  }
3827  e->rate = avio_rb32(pb) / 65536.0;
3828  av_log(c->fc, AV_LOG_TRACE, "duration=%"PRId64" time=%"PRId64" rate=%f\n",
3829  e->duration, e->time, e->rate);
3830  }
3831  sc->elst_count = i;
3832 
3833  return 0;
3834 }
3835 
3837 {
3838  MOVStreamContext *sc;
3839 
3840  if (c->fc->nb_streams < 1)
3841  return AVERROR_INVALIDDATA;
3842  sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
3843  sc->timecode_track = avio_rb32(pb);
3844  return 0;
3845 }
3846 
3848 {
3849  int ret;
3850  uint8_t uuid[16];
3851  static const uint8_t uuid_isml_manifest[] = {
3852  0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
3853  0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
3854  };
3855 
3856  if (atom.size < sizeof(uuid) || atom.size >= FFMIN(INT_MAX, SIZE_MAX))
3857  return AVERROR_INVALIDDATA;
3858 
3859  ret = avio_read(pb, uuid, sizeof(uuid));
3860  if (ret < 0) {
3861  return ret;
3862  } else if (ret != sizeof(uuid)) {
3863  return AVERROR_INVALIDDATA;
3864  }
3865  if (!memcmp(uuid, uuid_isml_manifest, sizeof(uuid))) {
3866  uint8_t *buffer, *ptr;
3867  char *endptr;
3868  size_t len = atom.size - sizeof(uuid);
3869 
3870  if (len < 4) {
3871  return AVERROR_INVALIDDATA;
3872  }
3873  ret = avio_skip(pb, 4); // zeroes
3874  len -= 4;
3875 
3876  buffer = av_mallocz(len + 1);
3877  if (!buffer) {
3878  return AVERROR(ENOMEM);
3879  }
3880  ret = avio_read(pb, buffer, len);
3881  if (ret < 0) {
3882  av_free(buffer);
3883  return ret;
3884  } else if (ret != len) {
3885  av_free(buffer);
3886  return AVERROR_INVALIDDATA;
3887  }
3888 
3889  ptr = buffer;
3890  while ((ptr = av_stristr(ptr, "systemBitrate=\""))) {
3891  ptr += sizeof("systemBitrate=\"") - 1;
3892  c->bitrates_count++;
3893  c->bitrates = av_realloc_f(c->bitrates, c->bitrates_count, sizeof(*c->bitrates));
3894  if (!c->bitrates) {
3895  c->bitrates_count = 0;
3896  av_free(buffer);
3897  return AVERROR(ENOMEM);
3898  }
3899  errno = 0;
3900  ret = strtol(ptr, &endptr, 10);
3901  if (ret < 0 || errno || *endptr != '"') {
3902  c->bitrates[c->bitrates_count - 1] = 0;
3903  } else {
3904  c->bitrates[c->bitrates_count - 1] = ret;
3905  }
3906  }
3907 
3908  av_free(buffer);
3909  }
3910  return 0;
3911 }
3912 
3914 {
3915  int ret;
3916  uint8_t content[16];
3917 
3918  if (atom.size < 8)
3919  return 0;
3920 
3921  ret = avio_read(pb, content, FFMIN(sizeof(content), atom.size));
3922  if (ret < 0)
3923  return ret;
3924 
3925  if ( !c->found_moov
3926  && !c->found_mdat
3927  && !memcmp(content, "Anevia\x1A\x1A", 8)
3930  }
3931 
3932  return 0;
3933 }
3934 
3936 { MKTAG('A','C','L','R'), mov_read_aclr },
3937 { MKTAG('A','P','R','G'), mov_read_avid },
3938 { MKTAG('A','A','L','P'), mov_read_avid },
3939 { MKTAG('A','R','E','S'), mov_read_ares },
3940 { MKTAG('a','v','s','s'), mov_read_avss },
3941 { MKTAG('c','h','p','l'), mov_read_chpl },
3942 { MKTAG('c','o','6','4'), mov_read_stco },
3943 { MKTAG('c','o','l','r'), mov_read_colr },
3944 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3945 { MKTAG('d','i','n','f'), mov_read_default },
3946 { MKTAG('D','p','x','E'), mov_read_dpxe },
3947 { MKTAG('d','r','e','f'), mov_read_dref },
3948 { MKTAG('e','d','t','s'), mov_read_default },
3949 { MKTAG('e','l','s','t'), mov_read_elst },
3950 { MKTAG('e','n','d','a'), mov_read_enda },
3951 { MKTAG('f','i','e','l'), mov_read_fiel },
3952 { MKTAG('a','d','r','m'), mov_read_adrm },
3953 { MKTAG('f','t','y','p'), mov_read_ftyp },
3954 { MKTAG('g','l','b','l'), mov_read_glbl },
3955 { MKTAG('h','d','l','r'), mov_read_hdlr },
3956 { MKTAG('i','l','s','t'), mov_read_ilst },
3957 { MKTAG('j','p','2','h'), mov_read_jp2h },
3958 { MKTAG('m','d','a','t'), mov_read_mdat },
3959 { MKTAG('m','d','h','d'), mov_read_mdhd },
3960 { MKTAG('m','d','i','a'), mov_read_default },
3961 { MKTAG('m','e','t','a'), mov_read_meta },
3962 { MKTAG('m','i','n','f'), mov_read_default },
3963 { MKTAG('m','o','o','f'), mov_read_moof },
3964 { MKTAG('m','o','o','v'), mov_read_moov },
3965 { MKTAG('m','v','e','x'), mov_read_default },
3966 { MKTAG('m','v','h','d'), mov_read_mvhd },
3967 { MKTAG('S','M','I',' '), mov_read_svq3 },
3968 { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
3969 { MKTAG('a','v','c','C'), mov_read_glbl },
3970 { MKTAG('p','a','s','p'), mov_read_pasp },
3971 { MKTAG('s','i','d','x'), mov_read_sidx },
3972 { MKTAG('s','t','b','l'), mov_read_default },
3973 { MKTAG('s','t','c','o'), mov_read_stco },
3974 { MKTAG('s','t','p','s'), mov_read_stps },
3975 { MKTAG('s','t','r','f'), mov_read_strf },
3976 { MKTAG('s','t','s','c'), mov_read_stsc },
3977 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3978 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3979 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3980 { MKTAG('s','t','t','s'), mov_read_stts },
3981 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3982 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3983 { MKTAG('t','f','d','t'), mov_read_tfdt },
3984 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3985 { MKTAG('t','r','a','k'), mov_read_trak },
3986 { MKTAG('t','r','a','f'), mov_read_default },
3987 { MKTAG('t','r','e','f'), mov_read_default },
3988 { MKTAG('t','m','c','d'), mov_read_tmcd },
3989 { MKTAG('c','h','a','p'), mov_read_chap },
3990 { MKTAG('t','r','e','x'), mov_read_trex },
3991 { MKTAG('t','r','u','n'), mov_read_trun },
3992 { MKTAG('u','d','t','a'), mov_read_default },
3993 { MKTAG('w','a','v','e'), mov_read_wave },
3994 { MKTAG('e','s','d','s'), mov_read_esds },
3995 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3996 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3997 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3998 { MKTAG('w','f','e','x'), mov_read_wfex },
3999 { MKTAG('c','m','o','v'), mov_read_cmov },
4000 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
4001 { MKTAG('d','v','c','1'), mov_read_dvc1 },
4002 { MKTAG('s','b','g','p'), mov_read_sbgp },
4003 { MKTAG('h','v','c','C'), mov_read_glbl },
4004 { MKTAG('u','u','i','d'), mov_read_uuid },
4005 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
4006 { MKTAG('f','r','e','e'), mov_read_free },
4007 { MKTAG('-','-','-','-'), mov_read_custom },
4008 { 0, NULL }
4009 };
4010 
4012 {
4013  int64_t total_size = 0;
4014  MOVAtom a;
4015  int i;
4016 
4017  if (c->atom_depth > 10) {
4018  av_log(c->fc, AV_LOG_ERROR, "Atoms too deeply nested\n");
4019  return AVERROR_INVALIDDATA;
4020  }
4021  c->atom_depth ++;
4022 
4023  if (atom.size < 0)
4024  atom.size = INT64_MAX;
4025  while (total_size <= atom.size - 8 && !avio_feof(pb)) {
4026  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
4027  a.size = atom.size;
4028  a.type=0;
4029  if (atom.size >= 8) {
4030  a.size = avio_rb32(pb);
4031  a.type = avio_rl32(pb);
4032  if (a.type == MKTAG('f','r','e','e') &&
4033  a.size >= 8 &&
4034  c->moov_retry) {
4035  uint8_t buf[8];
4036  uint32_t *type = (uint32_t *)buf + 1;
4037  if (avio_read(pb, buf, 8) != 8)
4038  return AVERROR_INVALIDDATA;
4039  avio_seek(pb, -8, SEEK_CUR);
4040  if (*type == MKTAG('m','v','h','d') ||
4041  *type == MKTAG('c','m','o','v')) {
4042  av_log(c->fc, AV_LOG_ERROR, "Detected moov in a free atom.\n");
4043  a.type = MKTAG('m','o','o','v');
4044  }
4045  }
4046  if (atom.type != MKTAG('r','o','o','t') &&
4047  atom.type != MKTAG('m','o','o','v'))
4048  {
4049  if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
4050  {
4051  av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
4052  avio_skip(pb, -8);
4053  c->atom_depth --;
4054  return 0;
4055  }
4056  }
4057  total_size += 8;
4058  if (a.size == 1 && total_size + 8 <= atom.size) { /* 64 bit extended size */
4059  a.size = avio_rb64(pb) - 8;
4060  total_size += 8;
4061  }
4062  }
4063  av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
4064  a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
4065  if (a.size == 0) {
4066  a.size = atom.size - total_size + 8;
4067  }
4068  a.size -= 8;
4069  if (a.size < 0)
4070  break;
4071  a.size = FFMIN(a.size, atom.size - total_size);
4072 
4073  for (i = 0; mov_default_parse_table[i].type; i++)
4074  if (mov_default_parse_table[i].type == a.type) {
4075  parse = mov_default_parse_table[i].parse;
4076  break;
4077  }
4078 
4079  // container is user data
4080  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
4081  atom.type == MKTAG('i','l','s','t')))
4083 
4084  if (!parse) { /* skip leaf atoms data */
4085  avio_skip(pb, a.size);
4086  } else {
4087  int64_t start_pos = avio_tell(pb);
4088  int64_t left;
4089  int err = parse(c, pb, a);
4090  if (err < 0) {
4091  c->atom_depth --;
4092  return err;
4093  }
4094  if (c->found_moov && c->found_mdat &&
4095  ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete) ||
4096  start_pos + a.size == avio_size(pb))) {
4097  if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX || c->fragment_index_complete)
4098  c->next_root_atom = start_pos + a.size;
4099  c->atom_depth --;
4100  return 0;
4101  }
4102  left = a.size - avio_tell(pb) + start_pos;
4103  if (left > 0) /* skip garbage at atom end */
4104  avio_skip(pb, left);
4105  else if (left < 0) {
4106  av_log(c->fc, AV_LOG_WARNING,
4107  "overread end of atom '%.4s' by %"PRId64" bytes\n",
4108  (char*)&a.type, -left);
4109  avio_seek(pb, left, SEEK_CUR);
4110  }
4111  }
4112 
4113  total_size += a.size;
4114  }
4115 
4116  if (total_size < atom.size && atom.size < 0x7ffff)
4117  avio_skip(pb, atom.size - total_size);
4118 
4119  c->atom_depth --;
4120  return 0;
4121 }
4122 
4123 static int mov_probe(AVProbeData *p)
4124 {
4125  int64_t offset;
4126  uint32_t tag;
4127  int score = 0;
4128  int moov_offset = -1;
4129 
4130  /* check file header */
4131  offset = 0;
4132  for (;;) {
4133  /* ignore invalid offset */
4134  if ((offset + 8) > (unsigned int)p->buf_size)
4135  break;
4136  tag = AV_RL32(p->buf + offset + 4);
4137  switch(tag) {
4138  /* check for obvious tags */
4139  case MKTAG('m','o','o','v'):
4140  moov_offset = offset + 4;
4141  case MKTAG('m','d','a','t'):
4142  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
4143  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
4144  case MKTAG('f','t','y','p'):
4145  if (AV_RB32(p->buf+offset) < 8 &&
4146  (AV_RB32(p->buf+offset) != 1 ||
4147  offset + 12 > (unsigned int)p->buf_size ||
4148  AV_RB64(p->buf+offset + 8) == 0)) {
4149  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4150  } else if (tag == MKTAG('f','t','y','p') &&
4151  ( AV_RL32(p->buf + offset + 8) == MKTAG('j','p','2',' ')
4152  || AV_RL32(p->buf + offset + 8) == MKTAG('j','p','x',' ')
4153  )) {
4154  score = FFMAX(score, 5);
4155  } else {
4156  score = AVPROBE_SCORE_MAX;
4157  }
4158  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4159  break;
4160  /* those are more common words, so rate then a bit less */
4161  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
4162  case MKTAG('w','i','d','e'):
4163  case MKTAG('f','r','e','e'):
4164  case MKTAG('j','u','n','k'):
4165  case MKTAG('p','i','c','t'):
4166  score = FFMAX(score, AVPROBE_SCORE_MAX - 5);
4167  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4168  break;
4169  case MKTAG(0x82,0x82,0x7f,0x7d):
4170  case MKTAG('s','k','i','p'):
4171  case MKTAG('u','u','i','d'):
4172  case MKTAG('p','r','f','l'):
4173  /* if we only find those cause probedata is too small at least rate them */
4174  score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
4175  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4176  break;
4177  default:
4178  offset = FFMAX(4, AV_RB32(p->buf+offset)) + offset;
4179  }
4180  }
4181  if(score > AVPROBE_SCORE_MAX - 50 && moov_offset != -1) {
4182  /* moov atom in the header - we should make sure that this is not a
4183  * MOV-packed MPEG-PS */
4184  offset = moov_offset;
4185 
4186  while(offset < (p->buf_size - 16)){ /* Sufficient space */
4187  /* We found an actual hdlr atom */
4188  if(AV_RL32(p->buf + offset ) == MKTAG('h','d','l','r') &&
4189  AV_RL32(p->buf + offset + 8) == MKTAG('m','h','l','r') &&
4190  AV_RL32(p->buf + offset + 12) == MKTAG('M','P','E','G')){
4191  av_log(NULL, AV_LOG_WARNING, "Found media data tag MPEG indicating this is a MOV-packed MPEG-PS.\n");
4192  /* We found a media handler reference atom describing an
4193  * MPEG-PS-in-MOV, return a
4194  * low score to force expanding the probe window until
4195  * mpegps_probe finds what it needs */
4196  return 5;
4197  }else
4198  /* Keep looking */
4199  offset+=2;
4200  }
4201  }
4202 
4203  return score;
4204 }
4205 
4206 // must be done after parsing all trak because there's no order requirement
4208 {
4209  MOVContext *mov = s->priv_data;
4210  AVStream *st = NULL;
4211  MOVStreamContext *sc;
4212  int64_t cur_pos;
4213  int i;
4214 
4215  for (i = 0; i < s->nb_streams; i++)
4216  if (s->streams[i]->id == mov->chapter_track) {
4217  st = s->streams[i];
4218  break;
4219  }
4220  if (!st) {
4221  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
4222  return;
4223  }
4224 
4225  st->discard = AVDISCARD_ALL;
4226  sc = st->priv_data;
4227  cur_pos = avio_tell(sc->pb);
4228 
4229  for (i = 0; i < st->nb_index_entries; i++) {
4230  AVIndexEntry *sample = &st->index_entries[i];
4231  int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
4232  uint8_t *title;
4233  uint16_t ch;
4234  int len, title_len;
4235 
4236  if (end < sample->timestamp) {
4237  av_log(s, AV_LOG_WARNING, "ignoring stream duration which is shorter than chapters\n");
4238  end = AV_NOPTS_VALUE;
4239  }
4240 
4241  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
4242  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
4243  goto finish;
4244  }
4245 
4246  // the first two bytes are the length of the title
4247  len = avio_rb16(sc->pb);
4248  if (len > sample->size-2)
4249  continue;
4250  title_len = 2*len + 1;
4251  if (!(title = av_mallocz(title_len)))
4252  goto finish;
4253 
4254  // The samples could theoretically be in any encoding if there's an encd
4255  // atom following, but in practice are only utf-8 or utf-16, distinguished
4256  // instead by the presence of a BOM
4257  if (!len) {
4258  title[0] = 0;
4259  } else {
4260  ch = avio_rb16(sc->pb);
4261  if (ch == 0xfeff)
4262  avio_get_str16be(sc->pb, len, title, title_len);
4263  else if (ch == 0xfffe)
4264  avio_get_str16le(sc->pb, len, title, title_len);
4265  else {
4266  AV_WB16(title, ch);
4267  if (len == 1 || len == 2)
4268  title[len] = 0;
4269  else
4270  avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
4271  }
4272  }
4273 
4274  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
4275  av_freep(&title);
4276  }
4277 finish:
4278  avio_seek(sc->pb, cur_pos, SEEK_SET);
4279 }
4280 
4282  uint32_t value, int flags)
4283 {
4284  AVTimecode tc;
4285  char buf[AV_TIMECODE_STR_SIZE];
4286  AVRational rate = {st->codec->time_base.den,
4287  st->codec->time_base.num};
4288  int ret = av_timecode_init(&tc, rate, flags, 0, s);
4289  if (ret < 0)
4290  return ret;
4291  av_dict_set(&st->metadata, "timecode",
4292  av_timecode_make_string(&tc, buf, value), 0);
4293  return 0;
4294 }
4295 
4297 {
4298  MOVStreamContext *sc = st->priv_data;
4299  int flags = 0;
4300  int64_t cur_pos = avio_tell(sc->pb);
4301  uint32_t value;
4302 
4303  if (!st->nb_index_entries)
4304  return -1;
4305 
4306  avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
4307  value = avio_rb32(s->pb);
4308 
4309  if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
4310  if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
4311  if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
4312 
4313  /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
4314  * not the case) and thus assume "frame number format" instead of QT one.
4315  * No sample with tmcd track can be found with a QT timecode at the moment,
4316  * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
4317  * format). */
4318  parse_timecode_in_framenum_format(s, st, value, flags);
4319 
4320  avio_seek(sc->pb, cur_pos, SEEK_SET);
4321  return 0;
4322 }
4323 
4325 {
4326  MOVContext *mov = s->priv_data;
4327  int i, j;
4328 
4329  for (i = 0; i < s->nb_streams; i++) {
4330  AVStream *st = s->streams[i];
4331  MOVStreamContext *sc = st->priv_data;
4332 
4333  if (!sc)
4334  continue;
4335 
4336  av_freep(&sc->ctts_data);
4337  for (j = 0; j < sc->drefs_count; j++) {
4338  av_freep(&sc->drefs[j].path);
4339  av_freep(&sc->drefs[j].dir);
4340  }
4341  av_freep(&sc->drefs);
4342 
4343  sc->drefs_count = 0;
4344 
4345  if (!sc->pb_is_copied)
4346  avio_closep(&sc->pb);
4347 
4348  sc->pb = NULL;
4349  av_freep(&sc->chunk_offsets);
4350  av_freep(&sc->stsc_data);
4351  av_freep(&sc->sample_sizes);
4352  av_freep(&sc->keyframes);
4353  av_freep(&sc->stts_data);
4354  av_freep(&sc->stps_data);
4355  av_freep(&sc->elst_data);
4356  av_freep(&sc->rap_group);
4357  av_freep(&sc->display_matrix);
4358  }
4359 
4360  if (mov->dv_demux) {
4362  mov->dv_fctx = NULL;
4363  }
4364 
4365  av_freep(&mov->trex_data);
4366  av_freep(&mov->bitrates);
4367 
4368  for (i = 0; i < mov->fragment_index_count; i++) {
4370  av_freep(&index->items);
4371  av_freep(&mov->fragment_index_data[i]);
4372  }
4374 
4375  av_freep(&mov->aes_decrypt);
4376 
4377  return 0;
4378 }
4379 
4380 static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
4381 {
4382  int i;
4383 
4384  for (i = 0; i < s->nb_streams; i++) {
4385  AVStream *st = s->streams[i];
4386  MOVStreamContext *sc = st->priv_data;
4387 
4388  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
4389  sc->timecode_track == tmcd_id)
4390  return 1;
4391  }
4392  return 0;
4393 }
4394 
4395 /* look for a tmcd track not referenced by any video track, and export it globally */
4397 {
4398  int i;
4399 
4400  for (i = 0; i < s->nb_streams; i++) {
4401  AVStream *st = s->streams[i];
4402 
4403  if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
4404  !tmcd_is_referenced(s, i + 1)) {
4405  AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
4406  if (tcr) {
4407  av_dict_set(&s->metadata, "timecode", tcr->value, 0);
4408  break;
4409  }
4410  }
4411  }
4412 }
4413 
4414 static int read_tfra(MOVContext *mov, AVIOContext *f)
4415 {
4417  int version, fieldlength, i, j;
4418  int64_t pos = avio_tell(f);
4419  uint32_t size = avio_rb32(f);
4420  void *tmp;
4421 
4422  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a')) {
4423  return 1;
4424  }
4425  av_log(mov->fc, AV_LOG_VERBOSE, "found tfra\n");
4426  index = av_mallocz(sizeof(MOVFragmentIndex));
4427  if (!index) {
4428  return AVERROR(ENOMEM);
4429  }
4430 
4432  mov->fragment_index_count + 1,
4433  sizeof(MOVFragmentIndex*));
4434  if (!tmp) {
4435  av_freep(&index);
4436  return AVERROR(ENOMEM);
4437  }
4438  mov->fragment_index_data = tmp;
4440 
4441  version = avio_r8(f);
4442  avio_rb24(f);
4443  index->track_id = avio_rb32(f);
4444  fieldlength = avio_rb32(f);
4445  index->item_count = avio_rb32(f);
4446  index->items = av_mallocz_array(
4447  index->item_count, sizeof(MOVFragmentIndexItem));
4448  if (!index->items) {
4449  index->item_count = 0;
4450  return AVERROR(ENOMEM);
4451  }
4452  for (i = 0; i < index->item_count; i++) {
4453  int64_t time, offset;
4454 
4455  if (avio_feof(f)) {
4456  index->item_count = 0;
4457  av_freep(&index->items);
4458  return AVERROR_INVALIDDATA;
4459  }
4460 
4461  if (version == 1) {
4462  time = avio_rb64(f);
4463  offset = avio_rb64(f);
4464  } else {
4465  time = avio_rb32(f);
4466  offset = avio_rb32(f);
4467  }
4468  index->items[i].time = time;
4469  index->items[i].moof_offset = offset;
4470  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
4471  avio_r8(f);
4472  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
4473  avio_r8(f);
4474  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
4475  avio_r8(f);
4476  }
4477 
4478  avio_seek(f, pos + size, SEEK_SET);
4479  return 0;
4480 }
4481 
4483 {
4484  int64_t stream_size = avio_size(f);
4485  int64_t original_pos = avio_tell(f);
4486  int64_t seek_ret;
4487  int32_t mfra_size;
4488  int ret = -1;
4489  if ((seek_ret = avio_seek(f, stream_size - 4, SEEK_SET)) < 0) {
4490  ret = seek_ret;
4491  goto fail;
4492  }
4493  mfra_size = avio_rb32(f);
4494  if (mfra_size < 0 || mfra_size > stream_size) {
4495  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (unreasonable size)\n");
4496  goto fail;
4497  }
4498  if ((seek_ret = avio_seek(f, -mfra_size, SEEK_CUR)) < 0) {
4499  ret = seek_ret;
4500  goto fail;
4501  }
4502  if (avio_rb32(f) != mfra_size) {
4503  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (size mismatch)\n");
4504  goto fail;
4505  }
4506  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
4507  av_log(c->fc, AV_LOG_DEBUG, "doesn't look like mfra (tag mismatch)\n");
4508  goto fail;
4509  }
4510  av_log(c->fc, AV_LOG_VERBOSE, "stream has mfra\n");
4511  do {
4512  ret = read_tfra(c, f);
4513  if (ret < 0)
4514  goto fail;
4515  } while (!ret);
4516  ret = 0;
4517 fail:
4518  seek_ret = avio_seek(f, original_pos, SEEK_SET);
4519  if (seek_ret < 0) {
4520  av_log(c->fc, AV_LOG_ERROR,
4521  "failed to seek back after looking for mfra\n");
4522  ret = seek_ret;
4523  }
4524  return ret;
4525 }
4526 
4528 {
4529  MOVContext *mov = s->priv_data;
4530  AVIOContext *pb = s->pb;
4531  int j, err;
4532  MOVAtom atom = { AV_RL32("root") };
4533  int i;
4534 
4535  mov->fc = s;
4536  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
4537  if (pb->seekable)
4538  atom.size = avio_size(pb);
4539  else
4540  atom.size = INT64_MAX;
4541 
4542  /* check MOV header */
4543  do {
4544  if (mov->moov_retry)
4545  avio_seek(pb, 0, SEEK_SET);
4546  if ((err = mov_read_default(mov, pb, atom)) < 0) {
4547  av_log(s, AV_LOG_ERROR, "error reading header\n");
4548  mov_read_close(s);
4549  return err;
4550  }
4551  } while (pb->seekable && !mov->found_moov && !mov->moov_retry++);
4552  if (!mov->found_moov) {
4553  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
4554  mov_read_close(s);
4555  return AVERROR_INVALIDDATA;
4556  }
4557  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
4558 
4559  if (pb->seekable) {
4560  if (mov->chapter_track > 0)
4561  mov_read_chapters(s);
4562  for (i = 0; i < s->nb_streams; i++)
4563  if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
4564  mov_read_timecode_track(s, s->streams[i]);
4565  }
4566 
4567  /* copy timecode metadata from tmcd tracks to the related video streams */
4568  for (i = 0; i < s->nb_streams; i++) {
4569  AVStream *st = s->streams[i];
4570  MOVStreamContext *sc = st->priv_data;
4571  if (sc->timecode_track > 0) {
4572  AVDictionaryEntry *tcr;
4573  int tmcd_st_id = -1;
4574 
4575  for (j = 0; j < s->nb_streams; j++)
4576  if (s->streams[j]->id == sc->timecode_track)
4577  tmcd_st_id = j;
4578 
4579  if (tmcd_st_id < 0 || tmcd_st_id == i)
4580  continue;
4581  tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
4582  if (tcr)
4583  av_dict_set(&st->metadata, "timecode", tcr->value, 0);
4584  }
4585  }
4587 
4588  for (i = 0; i < s->nb_streams; i++) {
4589  AVStream *st = s->streams[i];
4590  MOVStreamContext *sc = st->priv_data;
4591  fix_timescale(mov, sc);
4593  st->skip_samples = sc->start_pad;
4594  }
4595  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
4597  sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
4598  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4599  if (st->codec->width <= 0 || st->codec->height <= 0) {
4600  st->codec->width = sc->width;
4601  st->codec->height = sc->height;
4602  }
4603  if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
4604  if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
4605  return err;
4606  }
4607  }
4608  if (mov->handbrake_version &&
4609  mov->handbrake_version <= 1000000*0 + 1000*10 + 2 && // 0.10.2
4611  ) {
4612  av_log(s, AV_LOG_VERBOSE, "Forcing full parsing for mp3 stream\n");
4614  }
4615  }
4616 
4617  if (mov->trex_data) {
4618  for (i = 0; i < s->nb_streams; i++) {
4619  AVStream *st = s->streams[i];
4620  MOVStreamContext *sc = st->priv_data;
4621  if (st->duration > 0)
4622  st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
4623  }
4624  }
4625 
4626  if (mov->use_mfra_for > 0) {
4627  for (i = 0; i < s->nb_streams; i++) {
4628  AVStream *st = s->streams[i];
4629  MOVStreamContext *sc = st->priv_data;
4630  if (sc->duration_for_fps > 0) {
4631  st->codec->bit_rate = sc->data_size * 8 * sc->time_scale /
4632  sc->duration_for_fps;
4633  }
4634  }
4635  }
4636 
4637  for (i = 0; i < mov->bitrates_count && i < s->nb_streams; i++) {
4638  if (mov->bitrates[i]) {
4639  s->streams[i]->codec->bit_rate = mov->bitrates[i];
4640  }
4641  }
4642 
4643  ff_rfps_calculate(s);
4644 
4645  for (i = 0; i < s->nb_streams; i++) {
4646  AVStream *st = s->streams[i];
4647  MOVStreamContext *sc = st->priv_data;
4648 
4649  switch (st->codec->codec_type) {
4650  case AVMEDIA_TYPE_AUDIO:
4651  err = ff_replaygain_export(st, s->metadata);
4652  if (err < 0) {
4653  mov_read_close(s);
4654  return err;
4655  }
4656  break;
4657  case AVMEDIA_TYPE_VIDEO:
4658  if (sc->display_matrix) {
4659  AVPacketSideData *sd, *tmp;
4660 
4661  tmp = av_realloc_array(st->side_data,
4662  st->nb_side_data + 1, sizeof(*tmp));
4663  if (!tmp)
4664  return AVERROR(ENOMEM);
4665 
4666  st->side_data = tmp;
4667  st->nb_side_data++;
4668 
4669  sd = &st->side_data[st->nb_side_data - 1];
4671  sd->size = sizeof(int32_t) * 9;
4672  sd->data = (uint8_t*)sc->display_matrix;
4673  sc->display_matrix = NULL;
4674  }
4675  break;
4676  }
4677  }
4679 
4680  return 0;
4681 }
4682 
4684 {
4686  int64_t best_dts = INT64_MAX;
4687  int i;
4688  for (i = 0; i < s->nb_streams; i++) {
4689  AVStream *avst = s->streams[i];
4690  MOVStreamContext *msc = avst->priv_data;
4691  if (msc->pb && msc->current_sample < avst->nb_index_entries) {
4692  AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
4693  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
4694  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
4695  if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
4696  (s->pb->seekable &&
4697  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
4698  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
4699  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
4700  sample = current_sample;
4701  best_dts = dts;
4702  *st = avst;
4703  }
4704  }
4705  }
4706  return sample;
4707 }
4708 
4709 static int should_retry(AVIOContext *pb, int error_code) {
4710  if (error_code == AVERROR_EOF || avio_feof(pb))
4711  return 0;
4712 
4713  return 1;
4714 }
4715 
4716 static int mov_switch_root(AVFormatContext *s, int64_t target)
4717 {
4718  int ret;
4719  MOVContext *mov = s->priv_data;
4720  int i, j;
4721  int already_read = 0;
4722 
4723  if (avio_seek(s->pb, target, SEEK_SET) != target) {
4724  av_log(mov->fc, AV_LOG_ERROR, "root atom offset 0x%"PRIx64": partial file\n", target);
4725  return AVERROR_INVALIDDATA;
4726  }
4727 
4728  mov->next_root_atom = 0;
4729 
4730  for (i = 0; i < mov->fragment_index_count; i++) {
4732  int found = 0;
4733  for (j = 0; j < index->item_count; j++) {
4734  MOVFragmentIndexItem *item = &index->items[j];
4735  if (found) {
4736  mov->next_root_atom = item->moof_offset;
4737  break; // Advance to next index in outer loop
4738  } else if (item->moof_offset == target) {
4739  index->current_item = FFMIN(j, index->current_item);
4740  if (item->headers_read)
4741  already_read = 1;
4742  item->headers_read = 1;
4743  found = 1;
4744  }
4745  }
4746  if (!found)
4747  index->current_item = 0;
4748  }
4749 
4750  if (already_read)
4751  return 0;
4752 
4753  mov->found_mdat = 0;
4754 
4755  ret = mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX });
4756  if (ret < 0)
4757  return ret;
4758  if (avio_feof(s->pb))
4759  return AVERROR_EOF;
4760  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
4761 
4762  return 1;
4763 }
4764 
4766 {
4767  MOVContext *mov = s->priv_data;
4768  MOVStreamContext *sc;
4770  AVStream *st = NULL;
4771  int ret;
4772  mov->fc = s;
4773  retry:
4774  sample = mov_find_next_sample(s, &st);
4775  if (!sample || (mov->next_root_atom && sample->pos > mov->next_root_atom)) {
4776  if (!mov->next_root_atom)
4777  return AVERROR_EOF;
4778  if ((ret = mov_switch_root(s, mov->next_root_atom)) < 0)
4779  return ret;
4780  goto retry;
4781  }
4782  sc = st->priv_data;
4783  /* must be done just before reading, to avoid infinite loop on sample */
4784  sc->current_sample++;
4785 
4786  if (mov->next_root_atom) {
4787  sample->pos = FFMIN(sample->pos, mov->next_root_atom);
4788  sample->size = FFMIN(sample->size, (mov->next_root_atom - sample->pos));
4789  }
4790 
4791  if (st->discard != AVDISCARD_ALL) {
4792  int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
4793  if (ret64 != sample->pos) {
4794  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
4795  sc->ffindex, sample->pos);
4796  sc->current_sample -= should_retry(sc->pb, ret64);
4797  return AVERROR_INVALIDDATA;
4798  }
4799  ret = av_get_packet(sc->pb, pkt, sample->size);
4800  if (ret < 0) {
4801  sc->current_sample -= should_retry(sc->pb, ret);
4802  return ret;
4803  }
4804  if (sc->has_palette) {
4805  uint8_t *pal;
4806 
4808  if (!pal) {
4809  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
4810  } else {
4811  memcpy(pal, sc->palette, AVPALETTE_SIZE);
4812  sc->has_palette = 0;
4813  }
4814  }
4815 #if CONFIG_DV_DEMUXER
4816  if (mov->dv_demux && sc->dv_audio_container) {
4817  avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
4818  av_freep(&pkt->data);
4819  pkt->size = 0;
4820  ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
4821  if (ret < 0)
4822  return ret;
4823  }
4824 #endif
4825  }
4826 
4827  pkt->stream_index = sc->ffindex;
4828  pkt->dts = sample->timestamp;
4829  if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
4830  pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
4831  /* update ctts context */
4832  sc->ctts_sample++;
4833  if (sc->ctts_index < sc->ctts_count &&
4834  sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
4835  sc->ctts_index++;
4836  sc->ctts_sample = 0;
4837  }
4838  if (sc->wrong_dts)
4839  pkt->dts = AV_NOPTS_VALUE;
4840  } else {
4841  int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
4843 
4844  if (next_dts >= pkt->dts)
4845  pkt->duration = next_dts - pkt->dts;
4846  pkt->pts = pkt->dts;
4847  }
4848  if (st->discard == AVDISCARD_ALL)
4849  goto retry;
4850  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
4851  pkt->pos = sample->pos;
4852 
4853  if (mov->aax_mode)
4854  aax_filter(pkt->data, pkt->size, mov);
4855 
4856  return 0;
4857 }
4858 
4859 static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
4860 {
4861  MOVContext *mov = s->priv_data;
4862  int i, j;
4863 
4864  if (!mov->fragment_index_complete)
4865  return 0;
4866 
4867  for (i = 0; i < mov->fragment_index_count; i++) {
4868  if (mov->fragment_index_data[i]->track_id == st->id) {
4869  MOVFragmentIndex *index = index = mov->fragment_index_data[i];
4870  for (j = index->item_count - 1; j >= 0; j--) {
4871  if (index->items[j].time <= timestamp) {
4872  if (index->items[j].headers_read)
4873  return 0;
4874 
4875  return mov_switch_root(s, index->items[j].moof_offset);
4876  }
4877  }
4878  }
4879  }
4880 
4881  return 0;
4882 }
4883 
4884 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
4885 {
4886  MOVStreamContext *sc = st->priv_data;
4887  int sample, time_sample;
4888  int i;
4889 
4890  int ret = mov_seek_fragment(s, st, timestamp);
4891  if (ret < 0)
4892  return ret;
4893 
4894  sample = av_index_search_timestamp(st, timestamp, flags);
4895  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
4896  if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
4897  sample = 0;
4898  if (sample < 0) /* not sure what to do */
4899  return AVERROR_INVALIDDATA;
4900  sc->current_sample = sample;
4901  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
4902  /* adjust ctts index */
4903  if (sc->ctts_data) {
4904  time_sample = 0;
4905  for (i = 0; i < sc->ctts_count; i++) {
4906  int next = time_sample + sc->ctts_data[i].count;
4907  if (next > sc->current_sample) {
4908  sc->ctts_index = i;
4909  sc->ctts_sample = sc->current_sample - time_sample;
4910  break;
4911  }
4912  time_sample = next;
4913  }
4914  }
4915  return sample;
4916 }
4917 
4918 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
4919 {
4920  MOVContext *mc = s->priv_data;
4921  AVStream *st;
4922  int sample;
4923  int i;
4924 
4925  if (stream_index >= s->nb_streams)
4926  return AVERROR_INVALIDDATA;
4927 
4928  st = s->streams[stream_index];
4929  sample = mov_seek_stream(s, st, sample_time, flags);
4930  if (sample < 0)
4931  return sample;
4932 
4933  if (mc->seek_individually) {
4934  /* adjust seek timestamp to found sample timestamp */
4935  int64_t seek_timestamp = st->index_entries[sample].timestamp;
4936 
4937  for (i = 0; i < s->nb_streams; i++) {
4938  int64_t timestamp;
4939  MOVStreamContext *sc = s->streams[i]->priv_data;
4940  st = s->streams[i];
4941  st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
4942 
4943  if (stream_index == i)
4944  continue;
4945 
4946  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
4947  mov_seek_stream(s, st, timestamp, flags);
4948  }
4949  } else {
4950  for (i = 0; i < s->nb_streams; i++) {
4951  MOVStreamContext *sc;
4952  st = s->streams[i];
4953  sc = st->priv_data;
4954  sc->current_sample = 0;
4955  }
4956  while (1) {
4957  MOVStreamContext *sc;
4958  AVIndexEntry *entry = mov_find_next_sample(s, &st);
4959  if (!entry)
4960  return AVERROR_INVALIDDATA;
4961  sc = st->priv_data;
4962  if (sc->ffindex == stream_index && sc->current_sample == sample)
4963  break;
4964  sc->current_sample++;
4965  }
4966  }
4967  return 0;
4968 }
4969 
4970 #define OFFSET(x) offsetof(MOVContext, x)
4971 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
4972 static const AVOption mov_options[] = {
4973  {"use_absolute_path",
4974  "allow using absolute path when opening alias, this is a possible security issue",
4975  OFFSET(use_absolute_path), AV_OPT_TYPE_INT, {.i64 = 0},
4976  0, 1, FLAGS},
4977  {"seek_streams_individually",
4978  "Seek each stream individually to the to the closest point",
4979  OFFSET(seek_individually), AV_OPT_TYPE_INT, { .i64 = 1 },
4980  0, 1, FLAGS},
4981  {"ignore_editlist", "", OFFSET(ignore_editlist), AV_OPT_TYPE_INT, {.i64 = 0},
4982  0, 1, FLAGS},
4983  {"use_mfra_for",
4984  "use mfra for fragment timestamps",
4985  OFFSET(use_mfra_for), AV_OPT_TYPE_INT, {.i64 = FF_MOV_FLAG_MFRA_AUTO},
4987  "use_mfra_for"},
4988  {"auto", "auto", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_AUTO}, 0, 0,
4989  FLAGS, "use_mfra_for" },
4990  {"dts", "dts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_DTS}, 0, 0,
4991  FLAGS, "use_mfra_for" },
4992  {"pts", "pts", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_MFRA_PTS}, 0, 0,
4993  FLAGS, "use_mfra_for" },
4994  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
4995  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
4996  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
4997  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
4998  { "activation_bytes", "Secret bytes for Audible AAX files", OFFSET(activation_bytes),
5000  { "audible_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
5001  "Fixed key used for handling Audible AAX files", OFFSET(audible_fixed_key),
5002  AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd20a51d67"},
5003  .flags = AV_OPT_FLAG_DECODING_PARAM },
5004  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_INT,
5005  {.i64 = 0}, 0, 1, FLAGS },
5006  { NULL },
5007 };
5008 
5009 static const AVClass mov_class = {
5010  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
5011  .item_name = av_default_item_name,
5012  .option = mov_options,
5013  .version = LIBAVUTIL_VERSION_INT,
5014 };
5015 
5017  .name = "mov,mp4,m4a,3gp,3g2,mj2",
5018  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
5019  .priv_class = &mov_class,
5020  .priv_data_size = sizeof(MOVContext),
5021  .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
5022  .read_probe = mov_probe,
5028 };
int chapter_track
Definition: isom.h:188
static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1346
int itunes_metadata
metadata are itunes style
Definition: isom.h:186
int * bitrates
bitrates read before streams creation
Definition: isom.h:195
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:519
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:991
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:3973
int64_t moof_offset
Definition: isom.h:104
#define AV_RB8(x)
Definition: intreadwrite.h:395
#define NULL
Definition: coverity.c:32
#define DRM_BLOB_SIZE
Definition: mov.c:821
const char const char void * val
Definition: avisynth_c.h:634
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:682
const char * s
Definition: avisynth_c.h:631
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1116
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:284
#define MP4ESDescrTag
Definition: isom.h:222
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:719
#define av_realloc_f(p, o, n)
uint32_t tmcd_flags
tmcd track flags
Definition: isom.h:161
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:314
unsigned int rap_group_count
Definition: isom.h:164
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition: isom.c:426
unsigned int elst_count
Definition: isom.h:132
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1535
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3720
AVOption.
Definition: opt.h:255
hash context
Definition: sha.c:34
MOVTrackExt * trex_data
Definition: isom.h:184
int export_all
Definition: isom.h:193
"Linear transfer characteristics"
Definition: pixfmt.h:503
unsigned track_id
Definition: isom.h:79
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1748
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:229
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:4683
enum AVCodecID id
Definition: mxfenc.c:102
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:437
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2780
unsigned int samples_per_frame
Definition: isom.h:146
static void mov_build_index(MOVContext *mov, AVStream *st)
Definition: mov.c:2610
int dv_audio_container
Definition: isom.h:147
Definition: isom.h:45
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:273
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
float rate
Definition: isom.h:59
uint64_t base_data_offset
Definition: isom.h:80
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int64_t duration
Definition: isom.h:57
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1458
#define AV_RB64
Definition: intreadwrite.h:164
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4093
const char * g
Definition: vf_curves.c:108
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:590
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
int64_t pos
Definition: avformat.h:796
int ff_get_extradata(AVCodecContext *avctx, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:2959
unsigned int stsc_count
Definition: isom.h:127
int has_palette
Definition: isom.h:159
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:523
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:114
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2247
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3014
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:926
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:855
unsigned item_count
Definition: isom.h:111
int size
Definition: avcodec.h:1434
const char * b
Definition: vf_curves.c:109
#define MOV_TRUN_SAMPLE_CTS
Definition: isom.h:240
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AVIO_FLAG_READ
read-only
Definition: avio.h:485
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: aes.c:143
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1057
static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
Definition: mov.c:4380
void * activation_bytes
Definition: isom.h:207
#define tc
Definition: regdef.h:69
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1572
static const uint8_t ff_qt_default_palette_256[256 *3]
Definition: qtpalette.h:54
void * priv_data
Definition: avformat.h:874
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:279
static int mov_read_custom_2plus(MOVContext *c, AVIOContext *pb, int size)
Definition: mov.c:3136
int version
Definition: avisynth_c.h:629
int16_t audio_cid
stsd audio compression id
Definition: isom.h:149
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition: common.h:413
discard all
Definition: avcodec.h:689
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:966
static AVPacket pkt
int height
tkhd height
Definition: isom.h:156
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:679
static AVRational av_make_q(int num, int den)
Create a rational.
Definition: rational.h:53
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:258
#define sample
uint32_t type
Definition: isom.h:72
static int mov_read_uuid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3847
MOVElst * elst_data
Definition: isom.h:131
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2309
MOVStsc * stsc_data
Definition: isom.h:128
enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
Compute codec id for 'lpcm' tag.
Definition: mov.c:1620
unsigned fragment_index_count
Definition: isom.h:201
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1074
int ctts_index
Definition: isom.h:133
unsigned stsd_id
Definition: isom.h:92
int(* open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Called to open further IO contexts when needed for demuxing.
Definition: avformat.h:1833
int found_moov
'moov' atom has been found
Definition: isom.h:178
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2261
static void export_orphan_timecode(AVFormatContext *s)
Definition: mov.c:4396
static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1378
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:233
Macro definitions for various function/variable attributes.
static int should_retry(AVIOContext *pb, int error_code)
Definition: mov.c:4709
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1641
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:75
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
unsigned flags
Definition: isom.h:95
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:252
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:3855
int isom
1 if file is ISO Media (mp4/3gp)
Definition: isom.h:182
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:2347
int found_mdat
'mdat' atom has been found
Definition: isom.h:179
int width
tkhd width
Definition: isom.h:155
unsigned drefs_count
Definition: isom.h:150
Format I/O context.
Definition: avformat.h:1285
static int mov_probe(AVProbeData *p)
Definition: mov.c:4123
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:4527
char filename[64]
Definition: isom.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1398
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: vp3_parser.c:23
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1946
Public dictionary API.
MOVDref * drefs
Definition: isom.h:151
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:97
int fragment_index_complete
Definition: isom.h:202
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
enum AVColorTransferCharacteristic color_trc
Definition: dirac.c:103
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:973
static const AVOption mov_options[]
Definition: mov.c:4972
static int64_t start_time
Definition: ffplay.c:325
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
int first
Definition: isom.h:51
uint8_t
#define FF_MOV_FLAG_MFRA_DTS
Definition: isom.h:281
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:195
AVOptions.
static av_always_inline av_const int isnan(float x)
Definition: libm.h:96
unsigned int sample_count
Definition: isom.h:137
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:71
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:694
int count
Definition: isom.h:52
int dts_shift
dts shift when ctts is negative
Definition: isom.h:157
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
timecode is drop frame
Definition: timecode.h:36
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1279
int id
Format-specific stream ID.
Definition: avformat.h:861
enum AVStreamParseType need_parsing
Definition: avformat.h:1046
AVInputFormat ff_mov_demuxer
Definition: mov.c:5016
enum AVColorRange color_range
Definition: dirac.c:86
unsigned int count
Definition: isom.h:99
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3740
int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
Definition: mov.c:660
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:970
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3756
int16_t nlvl_to
Definition: isom.h:68
#define AV_CH_LOW_FREQUENCY
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
char volume[28]
Definition: isom.h:66
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:105
static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:774
uint8_t * ff_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, int size)
Add new side data to a stream.
Definition: utils.c:4584
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, int codec_tag, int format, int64_t size)
Definition: mov.c:2080
int activation_bytes_size
Definition: isom.h:208
MOVFragmentIndex ** fragment_index_data
Definition: isom.h:200
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
#define ALAC_EXTRADATA_SIZE
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1396
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:328
uint8_t * data
Definition: avcodec.h:1433
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:238
uint32_t tag
Definition: movenc.c:1339
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: utils.c:1790
int nb_frames_for_fps
Definition: isom.h:167
#define ff_dlog(a,...)
#define AVERROR_EOF
End of file.
Definition: error.h:55
bitstream reader API header.
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
uint8_t * data
Definition: avcodec.h:1383
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:244
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:599
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:761
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3006
static int64_t duration
Definition: ffplay.c:326
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
static int test_same_origin(const char *src, const char *ref)
Definition: mov.c:2894
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1443
#define av_log(a,...)
int current_sample
Definition: isom.h:144
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:230
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
unsigned track_id
Definition: isom.h:91
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
void ff_rfps_calculate(AVFormatContext *ic)
Definition: utils.c:3033
int64_t time_offset
time offset of the edit list entries
Definition: isom.h:143
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3206
unsigned int keyframe_count
Definition: isom.h:140
static const uint8_t ff_qt_default_palette_4[4 *3]
Definition: qtpalette.h:28
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition: timecode.c:182
const uint16_t avpriv_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3tab.c:89
static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1221
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2183
#define U(x)
Definition: vp56_arith.h:37
Callback for checking whether to abort blocking functions.
Definition: avio.h:50
static int mov_rewrite_dvd_sub_extradata(AVStream *st)
Definition: mov.c:1911
#define AVINDEX_KEYFRAME
Definition: avformat.h:803
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb)
Definition: isom.c:451
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1881
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:102
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:71
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1822
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:3404
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1497
unsigned int ctts_count
Definition: isom.h:125
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1847
const AVIOInterruptCB int_cb
Definition: ffmpeg.c:464
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:789
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3793
AVAudioServiceType
Definition: avcodec.h:692
av_default_item_name
#define AV_RB16
Definition: intreadwrite.h:53
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:663
#define AVERROR(e)
Definition: error.h:43
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2511
int atom_depth
Definition: isom.h:203
static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1421
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1426
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:797
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, AVCodecContext *codec, uint8_t *buf)
Definition: mov.c:1270
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int * keyframes
Definition: isom.h:141
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:425
AVFormatContext * fc
Definition: isom.h:175
enum AVColorPrimaries color_primaries
Definition: dirac.c:101
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:478
static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3486
enum AVPacketSideDataType type
Definition: avcodec.h:1385
#define FFNABS(a)
Negative Absolute value.
Definition: common.h:77
GLsizei GLsizei * length
Definition: opengl_enc.c:115
int handbrake_version
Definition: isom.h:187
int ctts_sample
Definition: isom.h:134
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:45
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet. ...
Definition: avformat.h:1102
enum AVCodecID codec_id
Definition: mov_chan.c:433
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
int keyframe_absent
Definition: isom.h:139
static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3622
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:937
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:378
#define FFMAX(a, b)
Definition: common.h:90
int16_t nlvl_from
Definition: isom.h:68
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:250
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:806
#define fail()
Definition: checkasm.h:57
static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
Definition: mov.c:1013
void * audible_fixed_key
Definition: isom.h:209
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:322
static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:823
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:472
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2333
Only parse headers, do not repack.
Definition: avformat.h:787
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:2107
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:533
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
static float distance(float x, float y, int band)
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3252
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:4011
static int aax_filter(uint8_t *input, int size, MOVContext *c)
Definition: mov.c:922
static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1148
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int64_t time
Definition: isom.h:105
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1341
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1029
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:4433
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:4884
uint8_t file_key[20]
Definition: isom.h:205
static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:71
struct AVAES * aes_decrypt
Definition: isom.h:211
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2429
static const uint16_t fc[]
Definition: dcaenc.h:41
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
int bit_rate
the average bitrate
Definition: avcodec.h:1577
audio channel layout utility functions
static int mov_realloc_extradata(AVCodecContext *codec, MOVAtom atom)
Definition: mov.c:1255
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:687
char filename[1024]
input or output filename
Definition: avformat.h:1361
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3424
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:134
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
#define FFMIN(a, b)
Definition: common.h:92
float y
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:936
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:78
static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3836
colour filters using Illuminant C
Definition: pixfmt.h:486
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
char * dir
Definition: isom.h:65
int width
picture width / height.
Definition: avcodec.h:1691
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int id
Definition: isom.h:53
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:527
static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1328
#define FLAGS
Definition: mov.c:4971
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
Definition: mov.c:4296
int audible_fixed_key_size
Definition: isom.h:210
int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: aviobuf.c:939
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3357
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1360
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2226
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3456
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1573
char * path
Definition: isom.h:64
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int time_scale
Definition: isom.h:142
int64_t time
Definition: isom.h:87
MOVFragmentIndexItem * items
Definition: isom.h:113
#define AV_RL32
Definition: intreadwrite.h:146
uint64_t moof_offset
Definition: isom.h:81
MOVStts * ctts_data
Definition: isom.h:126
#define OFFSET(x)
Definition: mov.c:4970
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:237
AVDictionary * metadata
Definition: avformat.h:928
unsigned size
Definition: isom.h:94
int ignore_editlist
Definition: isom.h:190
int64_t * chunk_offsets
Definition: isom.h:122
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:4765
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:4324
unsigned int index
Definition: isom.h:100
static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
This function reads atom content and puts data in extradata without tag nor size unlike mov_read_extr...
Definition: mov.c:1484
static int mov_open_dref(MOVContext *c, AVIOContext **pb, const char *src, MOVDref *ref, AVIOInterruptCB *int_cb)
Definition: mov.c:2923
int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size)
Read 'chan' tag from the input stream.
Definition: mov_chan.c:547
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:831
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:229
#define FF_MOV_FLAG_MFRA_PTS
Definition: isom.h:282
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:540
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
#define mc
unsigned current_item
Definition: isom.h:112
int64_t duration
duration of the longest track
Definition: isom.h:177
int ff_mov_lang_to_iso639(unsigned code, char to[4])
Definition: isom.c:382
Stream structure.
Definition: avformat.h:854
static const ColorEntry color_table[]
Definition: parseutils.c:198
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:167
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:194
unsigned duration
Definition: isom.h:93
DVDemuxContext * dv_demux
Definition: isom.h:180
also ITU-R BT1361
Definition: pixfmt.h:496
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:37
int timecode_track
Definition: isom.h:153
int * sample_sizes
Definition: isom.h:138
#define AV_CODEC_FLAG2_DROP_FRAME_TIMECODE
timecode is in drop frame format.
Definition: avcodec.h:816
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:4918
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts)
add frame for rfps calculation.
Definition: utils.c:2975
static const uint32_t mac_to_unicode[128]
Definition: mov.c:130
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:809
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:473
#define av_bswap32
Definition: bswap.h:33
AVS_Value src
Definition: avisynth_c.h:482
#define SQR(a)
enum AVMediaType codec_type
Definition: avcodec.h:1520
unsigned duration
Definition: isom.h:84
#define MP4DecConfigDescrTag
Definition: isom.h:223
MOVSbgp * rap_group
Definition: isom.h:165
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:368
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:268
functionally identical to above
Definition: pixfmt.h:485
int duration
Definition: isom.h:47
enum AVCodecID codec_id
Definition: avcodec.h:1529
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:4207
Timecode helpers header.
int sample_rate
samples per second
Definition: avcodec.h:2272
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1663
unsigned int stsz_sample_size
always contains sample size from stsz atom
Definition: isom.h:136
static int read_tfra(MOVContext *mov, AVIOContext *f)
Definition: mov.c:4414
main external API structure.
Definition: avcodec.h:1512
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:687
unsigned trex_count
Definition: isom.h:185
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:236
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3127
timecode wraps after 24 hours
Definition: timecode.h:37
int enable_drefs
Definition: isom.h:212
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1544
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
int64_t data_size
Definition: isom.h:160
static int mov_read_free(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3913
int has_looked_for_mfra
Definition: isom.h:199
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1628
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:69
uint32_t type
Definition: isom.h:63
int nb_index_entries
Definition: avformat.h:1059
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
MOVStts * stts_data
Definition: isom.h:124
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int count
Definition: isom.h:46
int index
Definition: gxfenc.c:89
enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
Select a PCM codec based on the given parameters.
Definition: utils.c:2792
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2240
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2233
unsigned int aax_mode
'aax' file has been detected
Definition: isom.h:204
static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1333
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:286
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
Definition: pixfmt.h:505
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:183
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:415
AVFieldOrder
Definition: avcodec.h:1494
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:162
Definition: isom.h:71
static const uint8_t ff_qt_default_palette_16[16 *3]
Definition: qtpalette.h:35
int use_mfra_for
Definition: isom.h:198
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1779
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:149
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:231
#define snprintf
Definition: snprintf.h:34
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3693
#define FF_MOV_FLAG_MFRA_AUTO
Definition: isom.h:280
int64_t time
Definition: isom.h:58
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int size, int big_endian)
Definition: riffdec.c:86
int pb_is_copied
Definition: isom.h:118
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:239
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:338
#define TAG_IS_AVCI(tag)
Definition: isom.h:257
#define MOV_TFHD_STSD_ID
Definition: isom.h:228
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2296
AVFormatContext * dv_fctx
Definition: isom.h:181
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
uint64_t implicit_offset
Definition: isom.h:82
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
static void mov_update_dts_shift(MOVStreamContext *sc, int duration)
Definition: mov.c:2504
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3238
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:84
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:130
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:539
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:65
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:813
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:91
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:814
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:913
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1338
int start_pad
amount of samples to skip due to enc-dec delay
Definition: isom.h:163
negative time values are allowed
Definition: timecode.h:38
full parsing and repack
Definition: avformat.h:786
Main libavformat public API header.
int32_t * display_matrix
Definition: isom.h:170
int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag)
Definition: isom.c:417
AVIOContext * pb
Definition: isom.h:117
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:1548
unsigned int bytes_per_frame
Definition: isom.h:145
static void finish(void)
Definition: ffhash.c:62
unsigned flags
Definition: isom.h:86
int bitrates_count
Definition: isom.h:196
int wrong_dts
dts are wrong due to huge ctts offset (iMovie files)
Definition: isom.h:154
if(ret< 0)
Definition: vf_mcdeint.c:280
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:227
int64_t size
Definition: isom.h:73
int export_xmp
Definition: isom.h:194
int seek_individually
Definition: isom.h:191
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:72
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
static const MOVParseTableEntry mov_default_parse_table[]
Definition: mov.c:3935
static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st, uint32_t value, int flags)
Definition: mov.c:4281
static double c[64]
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:143
static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:755
unsigned int stps_count
Definition: isom.h:129
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:917
unsigned int chunk_count
Definition: isom.h:121
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:208
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:1630
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:915
int den
denominator
Definition: rational.h:45
unsigned bps
Definition: movenc.c:1340
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:351
double av_display_rotation_get(const int32_t matrix[9])
The display transformation matrix specifies an affine transformation that should be applied to video ...
Definition: display.c:34
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:209
#define MKBETAG(a, b, c, d)
Definition: common.h:342
static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1571
uint32_t palette[256]
Definition: isom.h:158
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
unsigned stsd_id
Definition: isom.h:83
static int mov_seek_fragment(AVFormatContext *s, AVStream *st, int64_t timestamp)
Definition: mov.c:4859
unsigned int index_entries_allocated_size
Definition: avformat.h:1060
#define av_free(p)
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:492
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:2003
char * value
Definition: dict.h:88
int eof_reached
true if eof reached
Definition: avio.h:139
static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1323
int(* AVOpenCallback)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: avformat.h:1262
as in Berlin toast format
Definition: avcodec.h:442
int len
int channels
number of audio channels
Definition: avcodec.h:2273
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, enum AVCodecID codec_id)
Definition: mov.c:1292
unsigned int stts_count
Definition: isom.h:123
unsigned int sample_size
may contain value calculated from stsd or value from stsz atom
Definition: isom.h:135
uint32_t type
Definition: mov.c:64
void * priv_data
Format private data.
Definition: avformat.h:1313
static int mov_read_mfra(MOVContext *c, AVIOContext *f)
Definition: mov.c:4482
uint8_t file_iv[20]
Definition: isom.h:206
static const AVClass mov_class
Definition: mov.c:5009
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1614
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:235
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:192
ITU-R BT2020 for 10 bit system.
Definition: pixfmt.h:509
int time_scale
Definition: isom.h:176
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1517
Definition: isom.h:56
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:484
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
int pseudo_stream_id
-1 means demux all ids
Definition: isom.h:148
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1380
int ffindex
AVStream index.
Definition: isom.h:119
int use_absolute_path
Definition: isom.h:189
#define av_freep(p)
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2269
static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3430
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2194
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
unsigned size
Definition: isom.h:85
#define av_malloc_array(a, b)
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:719
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:303
static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1318
int64_t duration_for_fps
Definition: isom.h:168
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2344
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:303
int stream_index
Definition: avcodec.h:1435
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:896
static int unsupported(AVCodecContext *avctx)
Definition: iff.c:663
unsigned track_id
Definition: isom.h:110
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:98
#define MKTAG(a, b, c, d)
Definition: common.h:341
int moov_retry
Definition: isom.h:197
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:919
static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
Definition: mov.c:3004
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1073
static int mov_switch_root(AVFormatContext *s, int64_t target)
Definition: mov.c:4716
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:27
This structure stores compressed data.
Definition: avcodec.h:1410
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:963
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2568
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:946
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:118
Definition: isom.h:62
static uint32_t yuv_to_rgba(uint32_t ycbcr)
Definition: mov.c:1895
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:243
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: avcodec.h:1291
static int width