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