Libav
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 Libav.
10  *
11  * Libav 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  * Libav 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 Libav; 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/opt.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavcodec/ac3tab.h"
41 #include "avformat.h"
42 #include "internal.h"
43 #include "avio_internal.h"
44 #include "riff.h"
45 #include "isom.h"
46 #include "libavcodec/get_bits.h"
47 #include "id3v1.h"
48 #include "mov_chan.h"
49 #include "replaygain.h"
50 
51 #if CONFIG_ZLIB
52 #include <zlib.h>
53 #endif
54 
55 #include "qtpalette.h"
56 
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 
68  unsigned len, const char *key)
69 {
70  char buf[16];
71 
72  short current, total = 0;
73  avio_rb16(pb); // unknown
74  current = avio_rb16(pb);
75  if (len >= 6)
76  total = avio_rb16(pb);
77  if (!total)
78  snprintf(buf, sizeof(buf), "%d", current);
79  else
80  snprintf(buf, sizeof(buf), "%d/%d", current, total);
82  av_dict_set(&c->fc->metadata, key, buf, 0);
83 
84  return 0;
85 }
86 
88  unsigned len, const char *key)
89 {
90  char buf[16];
91 
92  /* bypass padding bytes */
93  avio_r8(pb);
94  avio_r8(pb);
95  avio_r8(pb);
96 
97  snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
99  av_dict_set(&c->fc->metadata, key, buf, 0);
100 
101  return 0;
102 }
103 
105  unsigned len, const char *key)
106 {
107  char buf[16];
108 
109  snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
111  av_dict_set(&c->fc->metadata, key, buf, 0);
112 
113  return 0;
114 }
115 
117  unsigned len, const char *key)
118 {
119  short genre;
120  char buf[20];
121 
122  avio_r8(pb); // unknown
123 
124  genre = avio_r8(pb);
125  if (genre < 1 || genre > ID3v1_GENRE_MAX)
126  return 0;
127  snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
129  av_dict_set(&c->fc->metadata, key, buf, 0);
130 
131  return 0;
132 }
133 
134 static const uint32_t mac_to_unicode[128] = {
135  0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
136  0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
137  0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
138  0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
139  0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
140  0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
141  0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
142  0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
143  0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
144  0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
145  0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
146  0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
147  0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
148  0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
149  0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
150  0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
151 };
152 
154  char *dst, int dstlen)
155 {
156  char *p = dst;
157  char *end = dst+dstlen-1;
158  int i;
159 
160  for (i = 0; i < len; i++) {
161  uint8_t t, c = avio_r8(pb);
162 
163  if (p >= end)
164  continue;
165 
166  if (c < 0x80)
167  *p++ = c;
168  else
169  PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
170  }
171  *p = 0;
172  return p - dst;
173 }
174 
175 static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
176 {
177  AVPacket pkt;
178  AVStream *st;
179  MOVStreamContext *sc;
180  enum AVCodecID id;
181  int ret;
182 
183  switch (type) {
184  case 0xd: id = AV_CODEC_ID_MJPEG; break;
185  case 0xe: id = AV_CODEC_ID_PNG; break;
186  case 0x1b: id = AV_CODEC_ID_BMP; break;
187  default:
188  av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
189  avio_skip(pb, len);
190  return 0;
191  }
192 
193  st = avformat_new_stream(c->fc, NULL);
194  if (!st)
195  return AVERROR(ENOMEM);
196  sc = av_mallocz(sizeof(*sc));
197  if (!sc)
198  return AVERROR(ENOMEM);
199  st->priv_data = sc;
200 
201  ret = av_get_packet(pb, &pkt, len);
202  if (ret < 0)
203  return ret;
204 
205  if (pkt.size >= 8 && id != AV_CODEC_ID_BMP) {
206  if (AV_RB64(pkt.data) == 0x89504e470d0a1a0a) {
207  id = AV_CODEC_ID_PNG;
208  } else {
209  id = AV_CODEC_ID_MJPEG;
210  }
211  }
212 
214 
215  st->attached_pic = pkt;
216  st->attached_pic.stream_index = st->index;
218 
220  st->codecpar->codec_id = id;
221 
222  return 0;
223 }
224 
225 // 3GPP TS 26.244
226 static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
227 {
228  char language[4] = { 0 };
229  char buf[200], place[100];
230  uint16_t langcode = 0;
231  double longitude, latitude, altitude;
232  const char *key = "location";
233 
234  if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) {
235  av_log(c->fc, AV_LOG_ERROR, "loci too short\n");
236  return AVERROR_INVALIDDATA;
237  }
238 
239  avio_skip(pb, 4); // version+flags
240  langcode = avio_rb16(pb);
241  ff_mov_lang_to_iso639(langcode, language);
242  len -= 6;
243 
244  len -= avio_get_str(pb, len, place, sizeof(place));
245  if (len < 1) {
246  av_log(c->fc, AV_LOG_ERROR, "place name too long\n");
247  return AVERROR_INVALIDDATA;
248  }
249  avio_skip(pb, 1); // role
250  len -= 1;
251 
252  if (len < 12) {
253  av_log(c->fc, AV_LOG_ERROR,
254  "loci too short (%u bytes left, need at least %d)\n", len, 12);
255  return AVERROR_INVALIDDATA;
256  }
257  longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
258  latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
259  altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16);
260 
261  // Try to output in the same format as the ?xyz field
262  snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude);
263  if (altitude)
264  av_strlcatf(buf, sizeof(buf), "%+f", altitude);
265  av_strlcatf(buf, sizeof(buf), "/%s", place);
266 
267  if (*language && strcmp(language, "und")) {
268  char key2[16];
269  snprintf(key2, sizeof(key2), "%s-%s", key, language);
270  av_dict_set(&c->fc->metadata, key2, buf, 0);
271  }
273  return av_dict_set(&c->fc->metadata, key, buf, 0);
274 }
275 
277 {
278  char tmp_key[5];
279  char *str, key2[32], language[4] = {0};
280  const char *key = NULL;
281  uint16_t langcode = 0;
282  uint32_t data_type = 0, str_size, str_size_alloc;
283  int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
284  int raw = 0;
285 
286  switch (atom.type) {
287  case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break;
288  case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break;
289  case MKTAG( 'X','M','P','_'):
290  if (c->export_xmp) { key = "xmp"; raw = 1; } break;
291  case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
292  case MKTAG( 'a','k','I','D'): key = "account_type";
294  case MKTAG( 'a','p','I','D'): key = "account_id"; break;
295  case MKTAG( 'c','a','t','g'): key = "category"; break;
296  case MKTAG( 'c','p','i','l'): key = "compilation";
298  case MKTAG( 'c','p','r','t'): key = "copyright"; break;
299  case MKTAG( 'd','e','s','c'): key = "description"; break;
300  case MKTAG( 'd','i','s','k'): key = "disc";
302  case MKTAG( 'e','g','i','d'): key = "episode_uid";
304  case MKTAG( 'g','n','r','e'): key = "genre";
305  parse = mov_metadata_gnre; break;
306  case MKTAG( 'h','d','v','d'): key = "hd_video";
308  case MKTAG( 'k','e','y','w'): key = "keywords"; break;
309  case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
310  case MKTAG( 'l','o','c','i'):
311  return mov_metadata_loci(c, pb, atom.size);
312  case MKTAG( 'p','c','s','t'): key = "podcast";
314  case MKTAG( 'p','g','a','p'): key = "gapless_playback";
316  case MKTAG( 'p','u','r','d'): key = "purchase_date"; break;
317  case MKTAG( 'r','t','n','g'): key = "rating";
319  case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break;
320  case MKTAG( 's','o','a','l'): key = "sort_album"; break;
321  case MKTAG( 's','o','a','r'): key = "sort_artist"; break;
322  case MKTAG( 's','o','c','o'): key = "sort_composer"; break;
323  case MKTAG( 's','o','n','m'): key = "sort_name"; break;
324  case MKTAG( 's','o','s','n'): key = "sort_show"; break;
325  case MKTAG( 's','t','i','k'): key = "media_type";
327  case MKTAG( 't','r','k','n'): key = "track";
329  case MKTAG( 't','v','e','n'): key = "episode_id"; break;
330  case MKTAG( 't','v','e','s'): key = "episode_sort";
332  case MKTAG( 't','v','n','n'): key = "network"; break;
333  case MKTAG( 't','v','s','h'): key = "show"; break;
334  case MKTAG( 't','v','s','n'): key = "season_number";
336  case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
337  case MKTAG(0xa9,'P','R','D'): key = "producer"; break;
338  case MKTAG(0xa9,'a','l','b'): key = "album"; break;
339  case MKTAG(0xa9,'a','u','t'): key = "artist"; break;
340  case MKTAG(0xa9,'c','h','p'): key = "chapter"; break;
341  case MKTAG(0xa9,'c','m','t'): key = "comment"; break;
342  case MKTAG(0xa9,'c','o','m'): key = "composer"; break;
343  case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
344  case MKTAG(0xa9,'d','a','y'): key = "date"; break;
345  case MKTAG(0xa9,'d','i','r'): key = "director"; break;
346  case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break;
347  case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break;
348  case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
349  case MKTAG(0xa9,'f','m','t'): key = "original_format"; break;
350  case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
351  case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
352  case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break;
353  case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
354  case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
355  case MKTAG(0xa9,'m','a','k'): key = "make"; break;
356  case MKTAG(0xa9,'m','o','d'): key = "model"; break;
357  case MKTAG(0xa9,'n','a','m'): key = "title"; break;
358  case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break;
359  case MKTAG(0xa9,'p','r','d'): key = "producer"; break;
360  case MKTAG(0xa9,'p','r','f'): key = "performers"; break;
361  case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break;
362  case MKTAG(0xa9,'s','r','c'): key = "original_source"; break;
363  case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break;
364  case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
365  case MKTAG(0xa9,'t','o','o'): key = "encoder"; break;
366  case MKTAG(0xa9,'t','r','k'): key = "track"; break;
367  case MKTAG(0xa9,'u','r','l'): key = "URL"; break;
368  case MKTAG(0xa9,'w','r','n'): key = "warning"; break;
369  case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
370  case MKTAG(0xa9,'x','y','z'): key = "location"; break;
371  }
372 
373  if (c->itunes_metadata && atom.size > 8) {
374  int data_size = avio_rb32(pb);
375  int tag = avio_rl32(pb);
376  if (tag == MKTAG('d','a','t','a')) {
377  data_type = avio_rb32(pb); // type
378  avio_rb32(pb); // unknown
379  str_size = data_size - 16;
380  atom.size -= 16;
381 
382  if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
383  int ret = mov_read_covr(c, pb, data_type, str_size);
384  if (ret < 0) {
385  av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
386  return ret;
387  }
388  }
389  } else return 0;
390  } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) {
391  str_size = avio_rb16(pb); // string length
392  langcode = avio_rb16(pb);
393  ff_mov_lang_to_iso639(langcode, language);
394  atom.size -= 4;
395  } else
396  str_size = atom.size;
397 
398  if (c->export_all && !key) {
399  snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
400  key = tmp_key;
401  }
402 
403  if (!key)
404  return 0;
405  if (atom.size < 0)
406  return AVERROR_INVALIDDATA;
407 
408  // allocate twice as much as worst-case
409  str_size_alloc = (raw ? str_size : str_size * 2) + 1;
410  str = av_malloc(str_size_alloc);
411  if (!str)
412  return AVERROR(ENOMEM);
413 
414  if (parse)
415  parse(c, pb, str_size, key);
416  else {
417  if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded
418  mov_read_mac_string(c, pb, str_size, str, str_size_alloc);
419  } else {
420  int ret = ffio_read_size(pb, str, str_size);
421  if (ret < 0) {
422  av_free(str);
423  return ret;
424  }
425  str[str_size] = 0;
426  }
428  av_dict_set(&c->fc->metadata, key, str, 0);
429  if (*language && strcmp(language, "und")) {
430  snprintf(key2, sizeof(key2), "%s-%s", key, language);
431  av_dict_set(&c->fc->metadata, key2, str, 0);
432  }
433  }
434  av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language);
435  av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
436  key, str, (char*)&atom.type, str_size_alloc, atom.size);
437 
438  av_freep(&str);
439  return 0;
440 }
441 
442 static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
443 {
444  int64_t start;
445  int i, nb_chapters, str_len, version;
446  char str[256+1];
447  int ret;
448 
449  if ((atom.size -= 5) < 0)
450  return 0;
451 
452  version = avio_r8(pb);
453  avio_rb24(pb);
454  if (version)
455  avio_rb32(pb); // ???
456  nb_chapters = avio_r8(pb);
457 
458  for (i = 0; i < nb_chapters; i++) {
459  if (atom.size < 9)
460  return 0;
461 
462  start = avio_rb64(pb);
463  str_len = avio_r8(pb);
464 
465  if ((atom.size -= 9+str_len) < 0)
466  return 0;
467 
468  ret = ffio_read_size(pb, str, str_len);
469  if (ret < 0)
470  return ret;
471  str[str_len] = 0;
472  avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
473  }
474  return 0;
475 }
476 
477 #define MIN_DATA_ENTRY_BOX_SIZE 12
478 static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
479 {
480  AVStream *st;
481  MOVStreamContext *sc;
482  int entries, i, j;
483 
484  if (c->fc->nb_streams < 1)
485  return 0;
486  st = c->fc->streams[c->fc->nb_streams-1];
487  sc = st->priv_data;
488 
489  avio_rb32(pb); // version + flags
490  entries = avio_rb32(pb);
491  if (!entries ||
492  entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
493  entries >= UINT_MAX / sizeof(*sc->drefs))
494  return AVERROR_INVALIDDATA;
495  sc->drefs_count = 0;
496  av_free(sc->drefs);
497  sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
498  if (!sc->drefs)
499  return AVERROR(ENOMEM);
500  sc->drefs_count = entries;
501 
502  for (i = 0; i < entries; i++) {
503  MOVDref *dref = &sc->drefs[i];
504  uint32_t size = avio_rb32(pb);
505  int64_t next = avio_tell(pb) + size - 4;
506 
507  if (size < 12)
508  return AVERROR_INVALIDDATA;
509 
510  dref->type = avio_rl32(pb);
511  avio_rb32(pb); // version + flags
512  av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size);
513 
514  if (dref->type == MKTAG('a','l','i','s') && size > 150) {
515  /* macintosh alias record */
516  uint16_t volume_len, len;
517  int16_t type;
518  int ret;
519 
520  avio_skip(pb, 10);
521 
522  volume_len = avio_r8(pb);
523  volume_len = FFMIN(volume_len, 27);
524  ret = ffio_read_size(pb, dref->volume, 27);
525  if (ret < 0)
526  return ret;
527  dref->volume[volume_len] = 0;
528  av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
529 
530  avio_skip(pb, 12);
531 
532  len = avio_r8(pb);
533  len = FFMIN(len, 63);
534  ret = ffio_read_size(pb, dref->filename, 63);
535  if (ret < 0)
536  return ret;
537  dref->filename[len] = 0;
538  av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
539 
540  avio_skip(pb, 16);
541 
542  /* read next level up_from_alias/down_to_target */
543  dref->nlvl_from = avio_rb16(pb);
544  dref->nlvl_to = avio_rb16(pb);
545  av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
546  dref->nlvl_from, dref->nlvl_to);
547 
548  avio_skip(pb, 16);
549 
550  for (type = 0; type != -1 && avio_tell(pb) < next; ) {
551  if (pb->eof_reached)
552  return AVERROR_EOF;
553  type = avio_rb16(pb);
554  len = avio_rb16(pb);
555  av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
556  if (len&1)
557  len += 1;
558  if (type == 2) { // absolute path
559  av_free(dref->path);
560  dref->path = av_mallocz(len+1);
561  if (!dref->path)
562  return AVERROR(ENOMEM);
563 
564  ret = ffio_read_size(pb, dref->path, len);
565  if (ret < 0) {
566  av_freep(&dref->path);
567  return ret;
568  }
569  if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
570  len -= volume_len;
571  memmove(dref->path, dref->path+volume_len, len);
572  dref->path[len] = 0;
573  }
574  // trim string of any ending zeros
575  for (j = len - 1; j >= 0; j--) {
576  if (dref->path[j] == 0)
577  len--;
578  else
579  break;
580  }
581  for (j = 0; j < len; j++)
582  if (dref->path[j] == ':' || dref->path[j] == 0)
583  dref->path[j] = '/';
584  av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
585  } else if (type == 0) { // directory name
586  av_free(dref->dir);
587  dref->dir = av_malloc(len+1);
588  if (!dref->dir)
589  return AVERROR(ENOMEM);
590 
591  ret = ffio_read_size(pb, dref->dir, len);
592  if (ret < 0) {
593  av_freep(&dref->dir);
594  return ret;
595  }
596  dref->dir[len] = 0;
597  for (j = 0; j < len; j++)
598  if (dref->dir[j] == ':')
599  dref->dir[j] = '/';
600  av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
601  } else
602  avio_skip(pb, len);
603  }
604  } else {
605  av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x08%x size %d\n",
606  dref->type, size);
607  entries--;
608  i--;
609  }
610  avio_seek(pb, next, SEEK_SET);
611  }
612  return 0;
613 }
614 
615 static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
616 {
617  AVStream *st;
618  uint32_t type;
619  uint32_t av_unused ctype;
620  int64_t title_size;
621  char *title_str;
622  int ret;
623 
624  if (c->fc->nb_streams < 1) // meta before first trak
625  return 0;
626 
627  st = c->fc->streams[c->fc->nb_streams-1];
628 
629  avio_r8(pb); /* version */
630  avio_rb24(pb); /* flags */
631 
632  /* component type */
633  ctype = avio_rl32(pb);
634  type = avio_rl32(pb); /* component subtype */
635 
636  av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
637  av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type);
638 
639  if (type == MKTAG('v','i','d','e'))
641  else if (type == MKTAG('s','o','u','n'))
643  else if (type == MKTAG('m','1','a',' '))
645  else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
647 
648  avio_rb32(pb); /* component manufacture */
649  avio_rb32(pb); /* component flags */
650  avio_rb32(pb); /* component flags mask */
651 
652  title_size = atom.size - 24;
653  if (title_size > 0) {
654  title_str = av_malloc(title_size + 1); /* Add null terminator */
655  if (!title_str)
656  return AVERROR(ENOMEM);
657 
658  ret = ffio_read_size(pb, title_str, title_size);
659  if (ret < 0) {
660  av_freep(&title_str);
661  return ret;
662  }
663  title_str[title_size] = 0;
664  if (title_str[0]) {
665  int off = (!c->isom && title_str[0] == title_size - 1);
666  av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
667  }
668  av_freep(&title_str);
669  }
670 
671  return 0;
672 }
673 
675 {
676  AVStream *st;
677  int tag;
678 
679  if (fc->nb_streams < 1)
680  return 0;
681  st = fc->streams[fc->nb_streams-1];
682 
683  avio_rb32(pb); /* version + flags */
684  ff_mp4_read_descr(fc, pb, &tag);
685  if (tag == MP4ESDescrTag) {
687  } else
688  avio_rb16(pb); /* ID */
689 
690  ff_mp4_read_descr(fc, pb, &tag);
691  if (tag == MP4DecConfigDescrTag)
692  ff_mp4_read_dec_config_descr(fc, st, pb);
693  return 0;
694 }
695 
696 static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
697 {
698  return ff_mov_read_esds(c->fc, pb);
699 }
700 
701 static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
702 {
703  AVStream *st;
704  enum AVAudioServiceType *ast;
705  int ac3info, acmod, lfeon, bsmod;
706 
707  if (c->fc->nb_streams < 1)
708  return 0;
709  st = c->fc->streams[c->fc->nb_streams-1];
710 
712  sizeof(*ast));
713  if (!ast)
714  return AVERROR(ENOMEM);
715 
716  ac3info = avio_rb24(pb);
717  bsmod = (ac3info >> 14) & 0x7;
718  acmod = (ac3info >> 11) & 0x7;
719  lfeon = (ac3info >> 10) & 0x1;
720  st->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
722  if (lfeon)
724  *ast = bsmod;
725  if (st->codecpar->channels > 1 && bsmod == 0x7)
727 
728 #if FF_API_LAVF_AVCTX
730  st->codec->audio_service_type = *ast;
732 #endif
733 
734  return 0;
735 }
736 
737 static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
738 {
739  AVStream *st;
740  enum AVAudioServiceType *ast;
741  int eac3info, acmod, lfeon, bsmod;
742 
743  if (c->fc->nb_streams < 1)
744  return 0;
745  st = c->fc->streams[c->fc->nb_streams-1];
746 
748  sizeof(*ast));
749  if (!ast)
750  return AVERROR(ENOMEM);
751 
752  /* No need to parse fields for additional independent substreams and its
753  * associated dependent substreams since libavcodec's E-AC-3 decoder
754  * does not support them yet. */
755  avio_rb16(pb); /* data_rate and num_ind_sub */
756  eac3info = avio_rb24(pb);
757  bsmod = (eac3info >> 12) & 0x1f;
758  acmod = (eac3info >> 9) & 0x7;
759  lfeon = (eac3info >> 8) & 0x1;
761  if (lfeon)
764  *ast = bsmod;
765  if (st->codecpar->channels > 1 && bsmod == 0x7)
767 
768 #if FF_API_LAVF_AVCTX
770  st->codec->audio_service_type = *ast;
772 #endif
773 
774  return 0;
775 }
776 
777 static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
778 {
779  AVStream *st;
780 
781  if (c->fc->nb_streams < 1)
782  return 0;
783  st = c->fc->streams[c->fc->nb_streams-1];
784 
785  if (atom.size < 16)
786  return 0;
787 
788  /* skip version and flags */
789  avio_skip(pb, 4);
790 
791  ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
792 
793  return 0;
794 }
795 
796 static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
797 {
798  AVStream *st;
799 
800  if (c->fc->nb_streams < 1)
801  return 0;
802  st = c->fc->streams[c->fc->nb_streams-1];
803 
804  return ff_get_wav_header(c->fc, pb, st->codecpar, atom.size);
805 }
806 
807 static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
808 {
809  const int num = avio_rb32(pb);
810  const int den = avio_rb32(pb);
811  AVStream *st;
812 
813  if (c->fc->nb_streams < 1)
814  return 0;
815  st = c->fc->streams[c->fc->nb_streams-1];
816 
817  if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
818  (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
820  "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
822  num, den);
823  } else if (den != 0) {
824  st->sample_aspect_ratio.num = num;
825  st->sample_aspect_ratio.den = den;
826  }
827  return 0;
828 }
829 
830 /* this atom contains actual media data */
831 static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
832 {
833  if (atom.size == 0) /* wrong one (MP4) */
834  return 0;
835  c->found_mdat=1;
836  return 0; /* now go for moov */
837 }
838 
839 /* read major brand, minor version and compatible brands and store them as metadata */
840 static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
841 {
842  uint32_t minor_ver;
843  int comp_brand_size;
844  char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
845  char* comp_brands_str;
846  uint8_t type[5] = {0};
847  int ret = ffio_read_size(pb, type, 4);
848  if (ret < 0)
849  return ret;
850 
851  if (strcmp(type, "qt "))
852  c->isom = 1;
853  av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
854  av_dict_set(&c->fc->metadata, "major_brand", type, 0);
855  minor_ver = avio_rb32(pb); /* minor version */
856  snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver);
857  av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
858 
859  comp_brand_size = atom.size - 8;
860  if (comp_brand_size < 0)
861  return AVERROR_INVALIDDATA;
862  comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
863  if (!comp_brands_str)
864  return AVERROR(ENOMEM);
865 
866  ret = ffio_read_size(pb, comp_brands_str, comp_brand_size);
867  if (ret < 0) {
868  av_freep(&comp_brands_str);
869  return ret;
870  }
871  comp_brands_str[comp_brand_size] = 0;
872  av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
873  av_freep(&comp_brands_str);
874 
875  return 0;
876 }
877 
878 /* this atom should contain all header atoms */
879 static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
880 {
881  int ret;
882 
883  if ((ret = mov_read_default(c, pb, atom)) < 0)
884  return ret;
885  /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
886  /* so we don't parse the whole file if over a network */
887  c->found_moov=1;
888  return 0; /* now go for mdat */
889 }
890 
891 static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
892 {
894  av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
895  return mov_read_default(c, pb, atom);
896 }
897 
898 static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
899 {
900  char buffer[32];
901  if (time) {
902  struct tm *ptm, tmbuf;
903  time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
904  ptm = gmtime_r(&time, &tmbuf);
905  if (!ptm) return;
906  if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm))
907  av_dict_set(metadata, "creation_time", buffer, 0);
908  }
909 }
910 
911 static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
912 {
913  AVStream *st;
914  MOVStreamContext *sc;
915  int version;
916  char language[4] = {0};
917  unsigned lang;
918  time_t creation_time;
919 
920  if (c->fc->nb_streams < 1)
921  return 0;
922  st = c->fc->streams[c->fc->nb_streams-1];
923  sc = st->priv_data;
924 
925  if (sc->time_scale) {
926  av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n");
927  return AVERROR_INVALIDDATA;
928  }
929 
930  version = avio_r8(pb);
931  if (version > 1) {
932  avpriv_request_sample(c->fc, "Version %d", version);
933  return AVERROR_PATCHWELCOME;
934  }
935  avio_rb24(pb); /* flags */
936  if (version == 1) {
937  creation_time = avio_rb64(pb);
938  avio_rb64(pb);
939  } else {
940  creation_time = avio_rb32(pb);
941  avio_rb32(pb); /* modification time */
942  }
943  mov_metadata_creation_time(&st->metadata, creation_time);
944 
945  sc->time_scale = avio_rb32(pb);
946  st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
947 
948  lang = avio_rb16(pb); /* language */
949  if (ff_mov_lang_to_iso639(lang, language))
950  av_dict_set(&st->metadata, "language", language, 0);
951  avio_rb16(pb); /* quality */
952 
953  return 0;
954 }
955 
956 static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
957 {
958  time_t creation_time;
959  int version = avio_r8(pb); /* version */
960  avio_rb24(pb); /* flags */
961 
962  if (version == 1) {
963  creation_time = avio_rb64(pb);
964  avio_rb64(pb);
965  } else {
966  creation_time = avio_rb32(pb);
967  avio_rb32(pb); /* modification time */
968  }
969  mov_metadata_creation_time(&c->fc->metadata, creation_time);
970  c->time_scale = avio_rb32(pb); /* time scale */
971 
972  av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale);
973 
974  c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
975  avio_rb32(pb); /* preferred scale */
976 
977  avio_rb16(pb); /* preferred volume */
978 
979  avio_skip(pb, 10); /* reserved */
980 
981  avio_skip(pb, 36); /* display matrix */
982 
983  avio_rb32(pb); /* preview time */
984  avio_rb32(pb); /* preview duration */
985  avio_rb32(pb); /* poster time */
986  avio_rb32(pb); /* selection time */
987  avio_rb32(pb); /* selection duration */
988  avio_rb32(pb); /* current time */
989  avio_rb32(pb); /* next track ID */
990 
991  return 0;
992 }
993 
994 static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
995 {
996  AVStream *st;
997  int ret;
998 
999  if (c->fc->nb_streams < 1)
1000  return 0;
1001  st = c->fc->streams[c->fc->nb_streams-1];
1002 
1003  if ((uint64_t)atom.size > (1<<30))
1004  return AVERROR_INVALIDDATA;
1005 
1006  // currently SVQ3 decoder expect full STSD header - so let's fake it
1007  // this should be fixed and just SMI header should be passed
1008  av_free(st->codecpar->extradata);
1010  if (!st->codecpar->extradata)
1011  return AVERROR(ENOMEM);
1012  st->codecpar->extradata_size = 0x5a + atom.size;
1013  memcpy(st->codecpar->extradata, "SVQ3", 4); // fake
1014 
1015  ret = ffio_read_size(pb, st->codecpar->extradata + 0x5a, atom.size);
1016  if (ret < 0)
1017  return ret;
1018 
1019  av_log(c->fc, AV_LOG_TRACE, "Reading SMI %"PRId64" %s\n", atom.size, st->codecpar->extradata + 0x5a);
1020  return 0;
1021 }
1022 
1023 static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1024 {
1025  AVStream *st;
1026  int little_endian;
1027 
1028  if (c->fc->nb_streams < 1)
1029  return 0;
1030  st = c->fc->streams[c->fc->nb_streams-1];
1031 
1032  little_endian = !!avio_rb16(pb);
1033  av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian);
1034  if (little_endian == 1) {
1035  switch (st->codecpar->codec_id) {
1036  case AV_CODEC_ID_PCM_S24BE:
1038  break;
1039  case AV_CODEC_ID_PCM_S32BE:
1041  break;
1042  case AV_CODEC_ID_PCM_F32BE:
1044  break;
1045  case AV_CODEC_ID_PCM_F64BE:
1047  break;
1048  default:
1049  break;
1050  }
1051  }
1052  return 0;
1053 }
1054 
1055 static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1056 {
1057  AVStream *st;
1058  char color_parameter_type[5] = { 0 };
1059  uint16_t color_primaries, color_trc, color_matrix;
1060  int ret;
1061 
1062  if (c->fc->nb_streams < 1)
1063  return 0;
1064  st = c->fc->streams[c->fc->nb_streams - 1];
1065 
1066  ret = ffio_read_size(pb, color_parameter_type, 4);
1067  if (ret < 0)
1068  return ret;
1069  if (strncmp(color_parameter_type, "nclx", 4) &&
1070  strncmp(color_parameter_type, "nclc", 4)) {
1071  av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n",
1072  color_parameter_type);
1073  return 0;
1074  }
1075 
1076  color_primaries = avio_rb16(pb);
1077  color_trc = avio_rb16(pb);
1078  color_matrix = avio_rb16(pb);
1079 
1080  av_log(c->fc, AV_LOG_TRACE,
1081  "%s: pri %"PRIu16" trc %"PRIu16" matrix %"PRIu16"",
1082  color_parameter_type, color_primaries, color_trc, color_matrix);
1083 
1084  if (!strncmp(color_parameter_type, "nclx", 4)) {
1085  uint8_t color_range = avio_r8(pb) >> 7;
1086  av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range);
1087  if (color_range)
1089  else
1091 
1092  if (!av_color_primaries_name(color_primaries))
1093  color_primaries = AVCOL_PRI_UNSPECIFIED;
1094  if (!av_color_transfer_name(color_trc))
1095  color_trc = AVCOL_TRC_UNSPECIFIED;
1096  if (!av_color_space_name(color_matrix))
1097  color_matrix = AVCOL_SPC_UNSPECIFIED;
1098 
1099  st->codecpar->color_primaries = color_primaries;
1100  st->codecpar->color_trc = color_trc;
1101  st->codecpar->color_space = color_matrix;
1102  } else if (!strncmp(color_parameter_type, "nclc", 4)) {
1103  /* color primaries, Table 4-4 */
1104  switch (color_primaries) {
1105  case 1: st->codecpar->color_primaries = AVCOL_PRI_BT709; break;
1106  case 5: st->codecpar->color_primaries = AVCOL_PRI_SMPTE170M; break;
1107  case 6: st->codecpar->color_primaries = AVCOL_PRI_SMPTE240M; break;
1108  case 9: st->codecpar->color_primaries = AVCOL_PRI_BT2020; break;
1109  case 10: st->codecpar->color_primaries = AVCOL_PRI_SMPTE431; break;
1110  case 11: st->codecpar->color_primaries = AVCOL_PRI_SMPTE432; break;
1111  }
1112  /* color transfer, Table 4-5 */
1113  switch (color_trc) {
1114  case 1: st->codecpar->color_trc = AVCOL_TRC_BT709; break;
1115  case 7: st->codecpar->color_trc = AVCOL_TRC_SMPTE240M; break;
1116  case 17: st->codecpar->color_trc = AVCOL_TRC_SMPTE428; break;
1117  }
1118  /* color matrix, Table 4-6 */
1119  switch (color_matrix) {
1120  case 1: st->codecpar->color_space = AVCOL_SPC_BT709; break;
1121  case 6: st->codecpar->color_space = AVCOL_SPC_BT470BG; break;
1122  case 7: st->codecpar->color_space = AVCOL_SPC_SMPTE240M; break;
1123  case 9: st->codecpar->color_space = AVCOL_SPC_BT2020_NCL; break;
1124  }
1125  }
1126  av_log(c->fc, AV_LOG_TRACE, "\n");
1127 
1128  return 0;
1129 }
1130 
1131 static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1132 {
1133  AVStream *st;
1134  unsigned mov_field_order;
1135  enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
1136 
1137  if (c->fc->nb_streams < 1) // will happen with jp2 files
1138  return 0;
1139  st = c->fc->streams[c->fc->nb_streams-1];
1140  if (atom.size < 2)
1141  return AVERROR_INVALIDDATA;
1142  mov_field_order = avio_rb16(pb);
1143  if ((mov_field_order & 0xFF00) == 0x0100)
1144  decoded_field_order = AV_FIELD_PROGRESSIVE;
1145  else if ((mov_field_order & 0xFF00) == 0x0200) {
1146  switch (mov_field_order & 0xFF) {
1147  case 0x01: decoded_field_order = AV_FIELD_TT;
1148  break;
1149  case 0x06: decoded_field_order = AV_FIELD_BB;
1150  break;
1151  case 0x09: decoded_field_order = AV_FIELD_TB;
1152  break;
1153  case 0x0E: decoded_field_order = AV_FIELD_BT;
1154  break;
1155  }
1156  }
1157  if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
1158  av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
1159  }
1160  st->codecpar->field_order = decoded_field_order;
1161 
1162  return 0;
1163 }
1164 
1165 /* FIXME modify QDM2/SVQ3/H.264 decoders to take full atom as extradata */
1167 {
1168  AVStream *st;
1169  uint64_t size;
1170  uint8_t *buf;
1171  int err;
1172 
1173  if (c->fc->nb_streams < 1) // will happen with jp2 files
1174  return 0;
1175  st= c->fc->streams[c->fc->nb_streams-1];
1176  size= (uint64_t)st->codecpar->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE;
1177  if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
1178  return AVERROR_INVALIDDATA;
1179  if ((err = av_reallocp(&st->codecpar->extradata, size)) < 0) {
1180  st->codecpar->extradata_size = 0;
1181  return err;
1182  }
1183  buf = st->codecpar->extradata + st->codecpar->extradata_size;
1185  AV_WB32( buf , atom.size + 8);
1186  AV_WL32( buf + 4, atom.type);
1187 
1188  err = ffio_read_size(pb, buf + 8, atom.size);
1189  if (err < 0)
1190  return err;
1191 
1192  return 0;
1193 }
1194 
1195 static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1196 {
1197  AVStream *st;
1198  int ret;
1199 
1200  if (c->fc->nb_streams < 1)
1201  return 0;
1202  st = c->fc->streams[c->fc->nb_streams-1];
1203 
1204  if ((uint64_t)atom.size > (1<<30))
1205  return AVERROR_INVALIDDATA;
1206 
1208  // pass all frma atom to codec, needed at least for QDMC and QDM2
1209  av_free(st->codecpar->extradata);
1211  if (!st->codecpar->extradata)
1212  return AVERROR(ENOMEM);
1213  st->codecpar->extradata_size = atom.size;
1214 
1215  ret = ffio_read_size(pb, st->codecpar->extradata, atom.size);
1216  if (ret < 0)
1217  return ret;
1218  } else if (atom.size > 8) { /* to read frma, esds atoms */
1219  if ((ret = mov_read_default(c, pb, atom)) < 0)
1220  return ret;
1221  } else
1222  avio_skip(pb, atom.size);
1223  return 0;
1224 }
1225 
1230 static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1231 {
1232  AVStream *st;
1233  int ret;
1234 
1235  if (c->fc->nb_streams < 1)
1236  return 0;
1237  st = c->fc->streams[c->fc->nb_streams-1];
1238 
1239  if ((uint64_t)atom.size > (1<<30))
1240  return AVERROR_INVALIDDATA;
1241 
1242  if (atom.size >= 10) {
1243  // Broken files created by legacy versions of libavformat will
1244  // wrap a whole fiel atom inside of a glbl atom.
1245  unsigned size = avio_rb32(pb);
1246  unsigned type = avio_rl32(pb);
1247  avio_seek(pb, -8, SEEK_CUR);
1248  if (type == MKTAG('f','i','e','l') && size == atom.size)
1249  return mov_read_default(c, pb, atom);
1250  }
1251  av_free(st->codecpar->extradata);
1253  if (!st->codecpar->extradata)
1254  return AVERROR(ENOMEM);
1255  st->codecpar->extradata_size = atom.size;
1256 
1257  ret = ffio_read_size(pb, st->codecpar->extradata, atom.size);
1258  if (ret < 0)
1259  return ret;
1260 
1261  return 0;
1262 }
1263 
1264 static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1265 {
1266  AVStream *st;
1267  uint8_t profile_level;
1268  int ret;
1269 
1270  if (c->fc->nb_streams < 1)
1271  return 0;
1272  st = c->fc->streams[c->fc->nb_streams-1];
1273 
1274  if (atom.size >= (1<<28) || atom.size < 7)
1275  return AVERROR_INVALIDDATA;
1276 
1277  profile_level = avio_r8(pb);
1278  if ((profile_level & 0xf0) != 0xc0)
1279  return 0;
1280 
1281  av_free(st->codecpar->extradata);
1283  if (!st->codecpar->extradata)
1284  return AVERROR(ENOMEM);
1285  st->codecpar->extradata_size = atom.size - 7;
1286  avio_seek(pb, 6, SEEK_CUR);
1287 
1289  if (ret < 0)
1290  return ret;
1291 
1292  return 0;
1293 }
1294 
1300 static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1301 {
1302  AVStream *st;
1303  int ret;
1304 
1305  if (c->fc->nb_streams < 1)
1306  return 0;
1307  if (atom.size <= 40)
1308  return 0;
1309  st = c->fc->streams[c->fc->nb_streams-1];
1310 
1311  if ((uint64_t)atom.size > (1<<30))
1312  return AVERROR_INVALIDDATA;
1313 
1314  av_free(st->codecpar->extradata);
1316  if (!st->codecpar->extradata)
1317  return AVERROR(ENOMEM);
1318  st->codecpar->extradata_size = atom.size - 40;
1319  avio_skip(pb, 40);
1320 
1321  ret = ffio_read_size(pb, st->codecpar->extradata, atom.size - 40);
1322  if (ret < 0)
1323  return ret;
1324 
1325  return 0;
1326 }
1327 
1328 static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1329 {
1330  AVStream *st;
1331  MOVStreamContext *sc;
1332  unsigned int i, entries;
1333 
1334  if (c->fc->nb_streams < 1)
1335  return 0;
1336  st = c->fc->streams[c->fc->nb_streams-1];
1337  sc = st->priv_data;
1338 
1339  avio_r8(pb); /* version */
1340  avio_rb24(pb); /* flags */
1341 
1342  entries = avio_rb32(pb);
1343 
1344  if (!entries)
1345  return 0;
1346  if (entries >= UINT_MAX/sizeof(int64_t))
1347  return AVERROR_INVALIDDATA;
1348 
1349  sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
1350  if (!sc->chunk_offsets)
1351  return AVERROR(ENOMEM);
1352  sc->chunk_count = entries;
1353 
1354  if (atom.type == MKTAG('s','t','c','o'))
1355  for (i = 0; i < entries && !pb->eof_reached; i++)
1356  sc->chunk_offsets[i] = avio_rb32(pb);
1357  else if (atom.type == MKTAG('c','o','6','4'))
1358  for (i = 0; i < entries && !pb->eof_reached; i++)
1359  sc->chunk_offsets[i] = avio_rb64(pb);
1360  else
1361  return AVERROR_INVALIDDATA;
1362 
1363  sc->chunk_count = i;
1364 
1365  if (pb->eof_reached)
1366  return AVERROR_EOF;
1367 
1368  return 0;
1369 }
1370 
1376 {
1377  /* lpcm flags:
1378  * 0x1 = float
1379  * 0x2 = big-endian
1380  * 0x4 = signed
1381  */
1382  return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0);
1383 }
1384 
1385 static int mov_codec_id(AVStream *st, uint32_t format)
1386 {
1387  int id = ff_codec_get_id(ff_codec_movaudio_tags, format);
1388 
1389  if (id <= 0 &&
1390  ((format & 0xFFFF) == 'm' + ('s' << 8) ||
1391  (format & 0xFFFF) == 'T' + ('S' << 8)))
1392  id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF);
1393 
1394  if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
1396  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
1397  /* skip old ASF MPEG-4 tag */
1398  format && format != MKTAG('m','p','4','s')) {
1400  if (id <= 0)
1401  id = ff_codec_get_id(ff_codec_bmp_tags, format);
1402  if (id > 0)
1404  else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
1406  if (id > 0)
1408  }
1409  }
1410 
1411  st->codecpar->codec_tag = format;
1412 
1413  return id;
1414 }
1415 
1417  AVStream *st, MOVStreamContext *sc)
1418 {
1419  uint8_t codec_name[32];
1420  unsigned int color_depth, len, j;
1421  int color_greyscale;
1422  int color_table_id;
1423 
1424  avio_rb16(pb); /* version */
1425  avio_rb16(pb); /* revision level */
1426  avio_rb32(pb); /* vendor */
1427  avio_rb32(pb); /* temporal quality */
1428  avio_rb32(pb); /* spatial quality */
1429 
1430  st->codecpar->width = avio_rb16(pb); /* width */
1431  st->codecpar->height = avio_rb16(pb); /* height */
1432 
1433  avio_rb32(pb); /* horiz resolution */
1434  avio_rb32(pb); /* vert resolution */
1435  avio_rb32(pb); /* data size, always 0 */
1436  avio_rb16(pb); /* frames per samples */
1437 
1438  len = avio_r8(pb); /* codec name, pascal string */
1439  if (len > 31)
1440  len = 31;
1441  mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name));
1442  if (len < 31)
1443  avio_skip(pb, 31 - len);
1444 
1445  if (codec_name[0])
1446  av_dict_set(&st->metadata, "encoder", codec_name, 0);
1447 
1448  /* codec_tag YV12 triggers an UV swap in rawdec.c */
1449  if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
1450  st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
1451  /* Flash Media Server uses tag H.263 with Sorenson Spark */
1452  if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
1453  !memcmp(codec_name, "Sorenson H263", 13))
1455 
1456  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
1457  color_table_id = avio_rb16(pb); /* colortable id */
1458  av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n",
1459  st->codecpar->bits_per_coded_sample, color_table_id);
1460  /* figure out the palette situation */
1461  color_depth = st->codecpar->bits_per_coded_sample & 0x1F;
1462  color_greyscale = st->codecpar->bits_per_coded_sample & 0x20;
1463 
1464  /* if the depth is 2, 4, or 8 bpp, file is palettized */
1465  if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) {
1466  /* for palette traversal */
1467  unsigned int color_start, color_count, color_end;
1468  unsigned char r, g, b;
1469 
1470  if (color_greyscale) {
1471  int color_index, color_dec;
1472  /* compute the greyscale palette */
1473  st->codecpar->bits_per_coded_sample = color_depth;
1474  color_count = 1 << color_depth;
1475  color_index = 255;
1476  color_dec = 256 / (color_count - 1);
1477  for (j = 0; j < color_count; j++) {
1478  r = g = b = color_index;
1479  sc->palette[j] = (r << 16) | (g << 8) | (b);
1480  color_index -= color_dec;
1481  if (color_index < 0)
1482  color_index = 0;
1483  }
1484  } else if (color_table_id) {
1485  const uint8_t *color_table;
1486  /* if flag bit 3 is set, use the default palette */
1487  color_count = 1 << color_depth;
1488  if (color_depth == 2)
1489  color_table = ff_qt_default_palette_4;
1490  else if (color_depth == 4)
1491  color_table = ff_qt_default_palette_16;
1492  else
1493  color_table = ff_qt_default_palette_256;
1494 
1495  for (j = 0; j < color_count; j++) {
1496  r = color_table[j * 3 + 0];
1497  g = color_table[j * 3 + 1];
1498  b = color_table[j * 3 + 2];
1499  sc->palette[j] = (r << 16) | (g << 8) | (b);
1500  }
1501  } else {
1502  /* load the palette from the file */
1503  color_start = avio_rb32(pb);
1504  color_count = avio_rb16(pb);
1505  color_end = avio_rb16(pb);
1506  if ((color_start <= 255) && (color_end <= 255)) {
1507  for (j = color_start; j <= color_end; j++) {
1508  /* each R, G, or B component is 16 bits;
1509  * only use the top 8 bits; skip alpha bytes
1510  * up front */
1511  avio_r8(pb);
1512  avio_r8(pb);
1513  r = avio_r8(pb);
1514  avio_r8(pb);
1515  g = avio_r8(pb);
1516  avio_r8(pb);
1517  b = avio_r8(pb);
1518  avio_r8(pb);
1519  sc->palette[j] = (r << 16) | (g << 8) | (b);
1520  }
1521  }
1522  }
1523  sc->has_palette = 1;
1524  }
1525 }
1526 
1528  AVStream *st, MOVStreamContext *sc)
1529 {
1530  int bits_per_sample, flags;
1531  uint16_t version = avio_rb16(pb);
1532 
1533  avio_rb16(pb); /* revision level */
1534  avio_rb32(pb); /* vendor */
1535 
1536  st->codecpar->channels = avio_rb16(pb); /* channel count */
1537  st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
1538  av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
1539 
1540  sc->audio_cid = avio_rb16(pb);
1541  avio_rb16(pb); /* packet size = 0 */
1542 
1543  st->codecpar->sample_rate = ((avio_rb32(pb) >> 16));
1544 
1545  // Read QT version 1 fields. In version 0 these do not exist.
1546  av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom);
1547  if (!c->isom) {
1548  if (version == 1) {
1549  sc->samples_per_frame = avio_rb32(pb);
1550  avio_rb32(pb); /* bytes per packet */
1551  sc->bytes_per_frame = avio_rb32(pb);
1552  avio_rb32(pb); /* bytes per sample */
1553  } else if (version == 2) {
1554  avio_rb32(pb); /* sizeof struct only */
1556  st->codecpar->channels = avio_rb32(pb);
1557  avio_rb32(pb); /* always 0x7F000000 */
1559 
1560  flags = avio_rb32(pb); /* lpcm format specific flag */
1561  sc->bytes_per_frame = avio_rb32(pb);
1562  sc->samples_per_frame = avio_rb32(pb);
1563  if (st->codecpar->codec_tag == MKTAG('l','p','c','m'))
1564  st->codecpar->codec_id =
1566  flags);
1567  }
1568  if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
1569  /* can't correctly handle variable sized packet as audio unit */
1570  switch (st->codecpar->codec_id) {
1571  case AV_CODEC_ID_MP2:
1572  case AV_CODEC_ID_MP3:
1574  break;
1575  }
1576  }
1577  }
1578 
1579  switch (st->codecpar->codec_id) {
1580  case AV_CODEC_ID_PCM_S8:
1581  case AV_CODEC_ID_PCM_U8:
1582  if (st->codecpar->bits_per_coded_sample == 16)
1584  break;
1585  case AV_CODEC_ID_PCM_S16LE:
1586  case AV_CODEC_ID_PCM_S16BE:
1587  if (st->codecpar->bits_per_coded_sample == 8)
1589  else if (st->codecpar->bits_per_coded_sample == 24)
1590  st->codecpar->codec_id =
1593  break;
1594  /* set values for old format before stsd version 1 appeared */
1595  case AV_CODEC_ID_MACE3:
1596  sc->samples_per_frame = 6;
1597  sc->bytes_per_frame = 2 * st->codecpar->channels;
1598  break;
1599  case AV_CODEC_ID_MACE6:
1600  sc->samples_per_frame = 6;
1601  sc->bytes_per_frame = 1 * st->codecpar->channels;
1602  break;
1604  sc->samples_per_frame = 64;
1605  sc->bytes_per_frame = 34 * st->codecpar->channels;
1606  break;
1607  case AV_CODEC_ID_GSM:
1608  sc->samples_per_frame = 160;
1609  sc->bytes_per_frame = 33;
1610  break;
1611  default:
1612  break;
1613  }
1614 
1615  bits_per_sample = av_get_bits_per_sample(st->codecpar->codec_id);
1616  if (bits_per_sample) {
1617  st->codecpar->bits_per_coded_sample = bits_per_sample;
1618  sc->sample_size = (bits_per_sample >> 3) * st->codecpar->channels;
1619  }
1620 }
1621 
1623  AVStream *st, MOVStreamContext *sc,
1624  int64_t size)
1625 {
1626  // ttxt stsd contains display flags, justification, background
1627  // color, fonts, and default styles, so fake an atom to read it
1628  MOVAtom fake_atom = { .size = size };
1629  // mp4s contains a regular esds atom
1630  if (st->codecpar->codec_tag != AV_RL32("mp4s"))
1631  mov_read_glbl(c, pb, fake_atom);
1632  st->codecpar->width = sc->width;
1633  st->codecpar->height = sc->height;
1634 }
1635 
1636 static uint32_t yuv_to_rgba(uint32_t ycbcr)
1637 {
1638  uint8_t r, g, b;
1639  int y, cb, cr;
1640 
1641  y = (ycbcr >> 16) & 0xFF;
1642  cr = (ycbcr >> 8) & 0xFF;
1643  cb = ycbcr & 0xFF;
1644 
1645  b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128));
1646  g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128));
1647  r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128));
1648 
1649  return (r << 16) | (g << 8) | b;
1650 }
1651 
1653 {
1654  char buf[256] = {0};
1655  uint8_t *src = st->codecpar->extradata;
1656  int i;
1657 
1658  if (st->codecpar->extradata_size != 64)
1659  return 0;
1660 
1661  if (st->codecpar->width > 0 && st->codecpar->height > 0)
1662  snprintf(buf, sizeof(buf), "size: %dx%d\n",
1663  st->codecpar->width, st->codecpar->height);
1664  av_strlcat(buf, "palette: ", sizeof(buf));
1665 
1666  for (i = 0; i < 16; i++) {
1667  uint32_t yuv = AV_RB32(src + i * 4);
1668  uint32_t rgba = yuv_to_rgba(yuv);
1669 
1670  av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : "");
1671  }
1672 
1673  if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf))
1674  return 0;
1675 
1676  av_freep(&st->codecpar->extradata);
1677  st->codecpar->extradata_size = 0;
1679  if (!st->codecpar->extradata)
1680  return AVERROR(ENOMEM);
1681  st->codecpar->extradata_size = strlen(buf);
1682  memcpy(st->codecpar->extradata, buf, st->codecpar->extradata_size);
1683 
1684  return 0;
1685 }
1686 
1688  AVStream *st, MOVStreamContext *sc,
1689  int64_t size)
1690 {
1691  int ret;
1692 
1693  if (st->codecpar->codec_tag == MKTAG('t','m','c','d')) {
1694  st->codecpar->extradata_size = size;
1696  if (!st->codecpar->extradata)
1697  return AVERROR(ENOMEM);
1698  ret = ffio_read_size(pb, st->codecpar->extradata, size);
1699  if (ret < 0)
1700  return ret;
1701  } else {
1702  /* other codec type, just skip (rtp, mp4s ...) */
1703  avio_skip(pb, size);
1704  }
1705  return 0;
1706 }
1707 
1709  AVStream *st, MOVStreamContext *sc)
1710 {
1711  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1712  !st->codecpar->sample_rate && sc->time_scale > 1)
1713  st->codecpar->sample_rate = sc->time_scale;
1714 
1715  /* special codec parameters handling */
1716  switch (st->codecpar->codec_id) {
1717 #if CONFIG_DV_DEMUXER
1718  case AV_CODEC_ID_DVAUDIO:
1720  if (!c->dv_fctx) {
1721  av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n");
1722  return AVERROR(ENOMEM);
1723  }
1725  if (!c->dv_demux) {
1726  av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
1727  return AVERROR(ENOMEM);
1728  }
1729  sc->dv_audio_container = 1;
1731  break;
1732 #endif
1733  /* no ifdef since parameters are always those */
1734  case AV_CODEC_ID_QCELP:
1735  st->codecpar->channels = 1;
1736  // force sample rate for qcelp when not stored in mov
1737  if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
1738  st->codecpar->sample_rate = 8000;
1739  break;
1740  case AV_CODEC_ID_AMR_NB:
1741  st->codecpar->channels = 1;
1742  /* force sample rate for amr, stsd in 3gp does not store sample rate */
1743  st->codecpar->sample_rate = 8000;
1744  break;
1745  case AV_CODEC_ID_AMR_WB:
1746  st->codecpar->channels = 1;
1747  st->codecpar->sample_rate = 16000;
1748  break;
1749  case AV_CODEC_ID_MP2:
1750  case AV_CODEC_ID_MP3:
1751  /* force type after stsd for m1a hdlr */
1753  break;
1754  case AV_CODEC_ID_GSM:
1755  case AV_CODEC_ID_ADPCM_MS:
1757  case AV_CODEC_ID_ILBC:
1759  break;
1760  case AV_CODEC_ID_ALAC:
1761  if (st->codecpar->extradata_size == 36) {
1762  st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
1763  st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
1764  }
1765  break;
1766  case AV_CODEC_ID_VC1:
1768  break;
1769  default:
1770  break;
1771  }
1772  return 0;
1773 }
1774 
1776  int codec_tag, int format,
1777  int64_t size)
1778 {
1779  int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format);
1780 
1781  if (codec_tag &&
1782  (codec_tag == AV_RL32("hvc1") ||
1783  codec_tag == AV_RL32("hev1") ||
1784  (codec_tag != format &&
1785  // prores is allowed to have differing data format and codec tag
1786  codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
1787  (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id
1788  : codec_tag != MKTAG('j','p','e','g'))))) {
1789  /* Multiple fourcc, we skip JPEG. This is not correct, we should
1790  * export it as a separate AVStream but this needs a few changes
1791  * in the MOV demuxer, patch welcome. */
1792 
1793  av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
1794  avio_skip(pb, size);
1795  return 1;
1796  }
1797 
1798  return 0;
1799 }
1800 
1802 {
1803  AVStream *st;
1804  MOVStreamContext *sc;
1805  int pseudo_stream_id;
1806 
1807  if (c->fc->nb_streams < 1)
1808  return 0;
1809  st = c->fc->streams[c->fc->nb_streams-1];
1810  sc = st->priv_data;
1811 
1812  for (pseudo_stream_id = 0;
1813  pseudo_stream_id < entries && !pb->eof_reached;
1814  pseudo_stream_id++) {
1815  //Parsing Sample description table
1816  enum AVCodecID id;
1817  int ret, dref_id = 1;
1818  MOVAtom a = { AV_RL32("stsd") };
1819  int64_t start_pos = avio_tell(pb);
1820  int64_t size = avio_rb32(pb); /* size */
1821  uint32_t format = avio_rl32(pb); /* data format */
1822 
1823  if (size >= 16) {
1824  avio_rb32(pb); /* reserved */
1825  avio_rb16(pb); /* reserved */
1826  dref_id = avio_rb16(pb);
1827  } else {
1828  av_log(c->fc, AV_LOG_ERROR,
1829  "invalid size %"PRId64" in stsd\n", size);
1830  return AVERROR_INVALIDDATA;
1831  }
1832 
1833  if (mov_skip_multiple_stsd(c, pb, st->codecpar->codec_tag, format,
1834  size - (avio_tell(pb) - start_pos)))
1835  continue;
1836 
1837  sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
1838  sc->dref_id= dref_id;
1839 
1840  id = mov_codec_id(st, format);
1841 
1842  av_log(c->fc, AV_LOG_TRACE,
1843  "size=%"PRId64" format=0x%08x codec_type=%d\n",
1844  size, format, st->codecpar->codec_type);
1845 
1847  st->codecpar->codec_id = id;
1848  mov_parse_stsd_video(c, pb, st, sc);
1849  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
1850  st->codecpar->codec_id = id;
1851  mov_parse_stsd_audio(c, pb, st, sc);
1852  } else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
1853  st->codecpar->codec_id = id;
1854  mov_parse_stsd_subtitle(c, pb, st, sc,
1855  size - (avio_tell(pb) - start_pos));
1856  } else {
1857  ret = mov_parse_stsd_data(c, pb, st, sc,
1858  size - (avio_tell(pb) - start_pos));
1859  if (ret < 0)
1860  return ret;
1861  }
1862  /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */
1863  a.size = size - (avio_tell(pb) - start_pos);
1864  if (a.size > 8) {
1865  if ((ret = mov_read_default(c, pb, a)) < 0)
1866  return ret;
1867  } else if (a.size > 0)
1868  avio_skip(pb, a.size);
1869 
1870  if (sc->extradata) {
1871  int extra_size = st->codecpar->extradata_size;
1872 
1873  /* Move the current stream extradata to the stream context one. */
1874  sc->extradata_size[pseudo_stream_id] = extra_size;
1875  sc->extradata[pseudo_stream_id] = av_malloc(extra_size + AV_INPUT_BUFFER_PADDING_SIZE);
1876  if (!sc->extradata[pseudo_stream_id])
1877  return AVERROR(ENOMEM);
1878  memcpy(sc->extradata[pseudo_stream_id], st->codecpar->extradata, extra_size);
1879  av_freep(&st->codecpar->extradata);
1880  st->codecpar->extradata_size = 0;
1881  }
1882  }
1883 
1884  if (pb->eof_reached)
1885  return AVERROR_EOF;
1886 
1887  return 0;
1888 }
1889 
1890 static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1891 {
1892  AVStream *st;
1893  MOVStreamContext *sc;
1894  int ret, entries;
1895 
1896  if (c->fc->nb_streams < 1)
1897  return 0;
1898  st = c->fc->streams[c->fc->nb_streams - 1];
1899  sc = st->priv_data;
1900 
1901  avio_r8(pb); /* version */
1902  avio_rb24(pb); /* flags */
1903  entries = avio_rb32(pb);
1904 
1905  if (entries <= 0) {
1906  av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
1907  return AVERROR_INVALIDDATA;
1908  }
1909 
1910  if (sc->extradata) {
1911  av_log(c->fc, AV_LOG_ERROR,
1912  "Duplicate stsd found in this track.\n");
1913  return AVERROR_INVALIDDATA;
1914  }
1915 
1916  /* Prepare space for hosting multiple extradata. */
1917  sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
1918  if (!sc->extradata)
1919  return AVERROR(ENOMEM);
1920 
1921  sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
1922  if (!sc->extradata_size) {
1923  ret = AVERROR(ENOMEM);
1924  goto fail;
1925  }
1926 
1927  ret = ff_mov_read_stsd_entries(c, pb, entries);
1928  if (ret < 0)
1929  goto fail;
1930 
1931  sc->stsd_count = entries;
1932 
1933  /* Restore back the primary extradata. */
1934  av_free(st->codecpar->extradata);
1935  st->codecpar->extradata_size = sc->extradata_size[0];
1937  if (!st->codecpar->extradata) {
1938  ret = AVERROR(ENOMEM);
1939  goto fail;
1940  }
1941  memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
1942 
1943  return mov_finalize_stsd_codec(c, pb, st, sc);
1944 fail:
1945  av_freep(&sc->extradata);
1946  av_freep(&sc->extradata_size);
1947  return ret;
1948 }
1949 
1950 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1951 {
1952  AVStream *st;
1953  MOVStreamContext *sc;
1954  unsigned int i, entries;
1955 
1956  if (c->fc->nb_streams < 1)
1957  return 0;
1958  st = c->fc->streams[c->fc->nb_streams-1];
1959  sc = st->priv_data;
1960 
1961  avio_r8(pb); /* version */
1962  avio_rb24(pb); /* flags */
1963 
1964  entries = avio_rb32(pb);
1965 
1966  av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1967 
1968  if (!entries)
1969  return 0;
1970  if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1971  return AVERROR_INVALIDDATA;
1972  sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1973  if (!sc->stsc_data)
1974  return AVERROR(ENOMEM);
1975 
1976  for (i = 0; i < entries && !pb->eof_reached; i++) {
1977  sc->stsc_data[i].first = avio_rb32(pb);
1978  sc->stsc_data[i].count = avio_rb32(pb);
1979  sc->stsc_data[i].id = avio_rb32(pb);
1980  if (sc->stsc_data[i].id <= 0 || sc->stsc_data[i].id > sc->stsd_count) {
1981  sc->stsc_data[i].id = 1;
1982  if (c->fc->error_recognition & AV_EF_EXPLODE) {
1983  av_log(c->fc, AV_LOG_ERROR, "Invalid stsc index.\n");
1984  return AVERROR_INVALIDDATA;
1985  }
1986  }
1987  }
1988 
1989  sc->stsc_count = i;
1990 
1991  if (pb->eof_reached)
1992  return AVERROR_EOF;
1993 
1994  return 0;
1995 }
1996 
1997 static inline int mov_stsc_index_valid(int index, int count)
1998 {
1999  return index + 1 < count;
2000 }
2001 
2002 /* Compute the samples value for the stsc entry at the given index. */
2003 static inline int mov_get_stsc_samples(MOVStreamContext *sc, int index)
2004 {
2005  int chunk_count;
2006 
2007  if (mov_stsc_index_valid(index, sc->stsc_count))
2008  chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2009  else
2010  chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2011 
2012  return sc->stsc_data[index].count * chunk_count;
2013 }
2014 
2015 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2016 {
2017  AVStream *st;
2018  MOVStreamContext *sc;
2019  unsigned i, entries;
2020 
2021  if (c->fc->nb_streams < 1)
2022  return 0;
2023  st = c->fc->streams[c->fc->nb_streams-1];
2024  sc = st->priv_data;
2025 
2026  avio_rb32(pb); // version + flags
2027 
2028  entries = avio_rb32(pb);
2029  if (entries >= UINT_MAX / sizeof(*sc->stps_data))
2030  return AVERROR_INVALIDDATA;
2031  sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
2032  if (!sc->stps_data)
2033  return AVERROR(ENOMEM);
2034 
2035  for (i = 0; i < entries && !pb->eof_reached; i++) {
2036  sc->stps_data[i] = avio_rb32(pb);
2037  //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]);
2038  }
2039 
2040  sc->stps_count = i;
2041 
2042  if (pb->eof_reached)
2043  return AVERROR_EOF;
2044 
2045  return 0;
2046 }
2047 
2048 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2049 {
2050  AVStream *st;
2051  MOVStreamContext *sc;
2052  unsigned int i, entries;
2053 
2054  if (c->fc->nb_streams < 1)
2055  return 0;
2056  st = c->fc->streams[c->fc->nb_streams-1];
2057  sc = st->priv_data;
2058 
2059  avio_r8(pb); /* version */
2060  avio_rb24(pb); /* flags */
2061 
2062  entries = avio_rb32(pb);
2063 
2064  av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries);
2065 
2066  if (!entries)
2067  {
2068  sc->keyframe_absent = 1;
2069  return 0;
2070  }
2071  if (entries >= UINT_MAX / sizeof(int))
2072  return AVERROR_INVALIDDATA;
2073  av_freep(&sc->keyframes);
2074  sc->keyframes = av_malloc(entries * sizeof(int));
2075  if (!sc->keyframes)
2076  return AVERROR(ENOMEM);
2077 
2078  for (i = 0; i < entries && !pb->eof_reached; i++) {
2079  sc->keyframes[i] = avio_rb32(pb);
2080  //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]);
2081  }
2082 
2083  sc->keyframe_count = i;
2084 
2085  if (pb->eof_reached)
2086  return AVERROR_EOF;
2087 
2088  return 0;
2089 }
2090 
2091 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2092 {
2093  AVStream *st;
2094  MOVStreamContext *sc;
2095  unsigned int i, entries, sample_size, field_size, num_bytes;
2096  GetBitContext gb;
2097  unsigned char* buf;
2098  int ret;
2099 
2100  if (c->fc->nb_streams < 1)
2101  return 0;
2102  st = c->fc->streams[c->fc->nb_streams-1];
2103  sc = st->priv_data;
2104 
2105  avio_r8(pb); /* version */
2106  avio_rb24(pb); /* flags */
2107 
2108  if (atom.type == MKTAG('s','t','s','z')) {
2109  sample_size = avio_rb32(pb);
2110  if (!sc->sample_size) /* do not overwrite value computed in stsd */
2111  sc->sample_size = sample_size;
2112  field_size = 32;
2113  } else {
2114  sample_size = 0;
2115  avio_rb24(pb); /* reserved */
2116  field_size = avio_r8(pb);
2117  }
2118  entries = avio_rb32(pb);
2119 
2120  av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
2121 
2122  sc->sample_count = entries;
2123  if (sample_size)
2124  return 0;
2125 
2126  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
2127  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
2128  return AVERROR_INVALIDDATA;
2129  }
2130 
2131  if (!entries)
2132  return 0;
2133  if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
2134  return AVERROR_INVALIDDATA;
2135  sc->sample_sizes = av_malloc(entries * sizeof(int));
2136  if (!sc->sample_sizes)
2137  return AVERROR(ENOMEM);
2138 
2139  num_bytes = (entries*field_size+4)>>3;
2140 
2141  buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE);
2142  if (!buf) {
2143  av_freep(&sc->sample_sizes);
2144  return AVERROR(ENOMEM);
2145  }
2146 
2147  ret = ffio_read_size(pb, buf, num_bytes);
2148  if (ret < 0) {
2149  av_freep(&sc->sample_sizes);
2150  av_free(buf);
2151  return ret;
2152  }
2153 
2154  init_get_bits(&gb, buf, 8*num_bytes);
2155 
2156  for (i = 0; i < entries && !pb->eof_reached; i++) {
2157  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
2158  sc->data_size += sc->sample_sizes[i];
2159  }
2160 
2161  sc->sample_count = i;
2162 
2163  av_free(buf);
2164 
2165  if (pb->eof_reached)
2166  return AVERROR_EOF;
2167 
2168  return 0;
2169 }
2170 
2171 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2172 {
2173  AVStream *st;
2174  MOVStreamContext *sc;
2175  unsigned int i, entries;
2176  int64_t duration=0;
2177  int64_t total_sample_count=0;
2178 
2179  if (c->fc->nb_streams < 1)
2180  return 0;
2181  st = c->fc->streams[c->fc->nb_streams-1];
2182  sc = st->priv_data;
2183 
2184  avio_r8(pb); /* version */
2185  avio_rb24(pb); /* flags */
2186  entries = avio_rb32(pb);
2187 
2188  av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n",
2189  c->fc->nb_streams-1, entries);
2190 
2191  if (!entries)
2192  return 0;
2193  if (entries >= UINT_MAX / sizeof(*sc->stts_data))
2194  return AVERROR(EINVAL);
2195 
2196  av_free(sc->stts_data);
2197  sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
2198  if (!sc->stts_data)
2199  return AVERROR(ENOMEM);
2200 
2201  for (i = 0; i < entries && !pb->eof_reached; i++) {
2202  int sample_duration;
2203  int sample_count;
2204 
2205  sample_count=avio_rb32(pb);
2206  sample_duration = avio_rb32(pb);
2207  if (sample_count < 0) {
2208  av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
2209  return AVERROR_INVALIDDATA;
2210  }
2211  sc->stts_data[i].count= sample_count;
2212  sc->stts_data[i].duration= sample_duration;
2213 
2214  av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n",
2215  sample_count, sample_duration);
2216 
2217  duration+=(int64_t)sample_duration*sample_count;
2218  total_sample_count+=sample_count;
2219  }
2220 
2221  sc->stts_count = i;
2222 
2223  if (pb->eof_reached)
2224  return AVERROR_EOF;
2225 
2226  st->nb_frames= total_sample_count;
2227  if (duration)
2228  st->duration= duration;
2229  sc->track_end = duration;
2230  return 0;
2231 }
2232 
2233 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2234 {
2235  AVStream *st;
2236  MOVStreamContext *sc;
2237  unsigned int i, entries;
2238 
2239  if (c->fc->nb_streams < 1)
2240  return 0;
2241  st = c->fc->streams[c->fc->nb_streams-1];
2242  sc = st->priv_data;
2243 
2244  avio_r8(pb); /* version */
2245  avio_rb24(pb); /* flags */
2246  entries = avio_rb32(pb);
2247 
2248  av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
2249 
2250  av_freep(&sc->ctts_data);
2251 
2252  if (!entries)
2253  return 0;
2254  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
2255  return AVERROR_INVALIDDATA;
2256  sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
2257  if (!sc->ctts_data)
2258  return AVERROR(ENOMEM);
2259 
2260  for (i = 0; i < entries && !pb->eof_reached; i++) {
2261  int count =avio_rb32(pb);
2262  int duration =avio_rb32(pb);
2263 
2264  sc->ctts_data[i].count = count;
2265  sc->ctts_data[i].duration= duration;
2266  if (duration < 0)
2267  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
2268  }
2269 
2270  sc->ctts_count = i;
2271 
2272  if (pb->eof_reached)
2273  return AVERROR_EOF;
2274 
2275  av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
2276 
2277  return 0;
2278 }
2279 
2280 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2281 {
2282  AVStream *st;
2283  MOVStreamContext *sc;
2284  unsigned int i, entries;
2285  uint8_t version;
2286  uint32_t grouping_type;
2287 
2288  if (c->fc->nb_streams < 1)
2289  return 0;
2290  st = c->fc->streams[c->fc->nb_streams-1];
2291  sc = st->priv_data;
2292 
2293  version = avio_r8(pb); /* version */
2294  avio_rb24(pb); /* flags */
2295  grouping_type = avio_rl32(pb);
2296  if (grouping_type != MKTAG( 'r','a','p',' '))
2297  return 0; /* only support 'rap ' grouping */
2298  if (version == 1)
2299  avio_rb32(pb); /* grouping_type_parameter */
2300 
2301  entries = avio_rb32(pb);
2302  if (!entries)
2303  return 0;
2304  if (entries >= UINT_MAX / sizeof(*sc->rap_group))
2305  return AVERROR_INVALIDDATA;
2306  sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
2307  if (!sc->rap_group)
2308  return AVERROR(ENOMEM);
2309 
2310  for (i = 0; i < entries && !pb->eof_reached; i++) {
2311  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
2312  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
2313  }
2314 
2315  sc->rap_group_count = i;
2316 
2317  return pb->eof_reached ? AVERROR_EOF : 0;
2318 }
2319 
2320 static void mov_build_index(MOVContext *mov, AVStream *st)
2321 {
2322  MOVStreamContext *sc = st->priv_data;
2323  int64_t current_offset;
2324  int64_t current_dts = 0;
2325  unsigned int stts_index = 0;
2326  unsigned int stsc_index = 0;
2327  unsigned int stss_index = 0;
2328  unsigned int stps_index = 0;
2329  unsigned int i, j;
2330  uint64_t stream_size = 0;
2331 
2332  /* adjust first dts according to edit list */
2333  if (sc->time_offset && mov->time_scale > 0) {
2334  if (sc->time_offset < 0)
2335  sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
2336  current_dts = -sc->time_offset;
2337  }
2338 
2339  /* only use old uncompressed audio chunk demuxing when stts specifies it */
2340  if (!(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2341  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
2342  unsigned int current_sample = 0;
2343  unsigned int stts_sample = 0;
2344  unsigned int sample_size;
2345  unsigned int distance = 0;
2346  unsigned int rap_group_index = 0;
2347  unsigned int rap_group_sample = 0;
2348  int rap_group_present = sc->rap_group_count && sc->rap_group;
2349  int key_off = (sc->keyframes && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
2350 
2351  current_dts -= sc->dts_shift;
2352 
2353  if (!sc->sample_count)
2354  return;
2355  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2356  return;
2358  st->nb_index_entries + sc->sample_count,
2359  sizeof(*st->index_entries)) < 0) {
2360  st->nb_index_entries = 0;
2361  return;
2362  }
2364 
2365  for (i = 0; i < sc->chunk_count; i++) {
2366  current_offset = sc->chunk_offsets[i];
2367  while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
2368  i + 1 == sc->stsc_data[stsc_index + 1].first)
2369  stsc_index++;
2370  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2371  int keyframe = 0;
2372  if (current_sample >= sc->sample_count) {
2373  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2374  return;
2375  }
2376 
2377  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2378  keyframe = 1;
2379  if (stss_index + 1 < sc->keyframe_count)
2380  stss_index++;
2381  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2382  keyframe = 1;
2383  if (stps_index + 1 < sc->stps_count)
2384  stps_index++;
2385  }
2386  if (rap_group_present && rap_group_index < sc->rap_group_count) {
2387  if (sc->rap_group[rap_group_index].index > 0)
2388  keyframe = 1;
2389  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2390  rap_group_sample = 0;
2391  rap_group_index++;
2392  }
2393  }
2394  if (keyframe)
2395  distance = 0;
2396  sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
2397  if (sc->pseudo_stream_id == -1 ||
2398  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2399  AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2400  e->pos = current_offset;
2401  e->timestamp = current_dts;
2402  e->size = sample_size;
2403  e->min_distance = distance;
2404  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2405  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2406  "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2407  current_offset, current_dts, sample_size, distance, keyframe);
2408  }
2409 
2410  current_offset += sample_size;
2411  stream_size += sample_size;
2412  current_dts += sc->stts_data[stts_index].duration;
2413  distance++;
2414  stts_sample++;
2415  current_sample++;
2416  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2417  stts_sample = 0;
2418  stts_index++;
2419  }
2420  }
2421  }
2422  if (st->duration > 0)
2423  st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
2424  } else {
2425  unsigned chunk_samples, total = 0;
2426 
2427  // compute total chunk count
2428  for (i = 0; i < sc->stsc_count; i++) {
2429  unsigned count, chunk_count;
2430 
2431  chunk_samples = sc->stsc_data[i].count;
2432  if (i != sc->stsc_count - 1 &&
2433  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2434  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2435  return;
2436  }
2437 
2438  if (sc->samples_per_frame >= 160) { // gsm
2439  count = chunk_samples / sc->samples_per_frame;
2440  } else if (sc->samples_per_frame > 1) {
2441  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2442  count = (chunk_samples+samples-1) / samples;
2443  } else {
2444  count = (chunk_samples+1023) / 1024;
2445  }
2446 
2447  if (mov_stsc_index_valid(i, sc->stsc_count))
2448  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2449  else
2450  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2451  total += chunk_count * count;
2452  }
2453 
2454  av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total);
2455  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2456  return;
2458  st->nb_index_entries + total,
2459  sizeof(*st->index_entries)) < 0) {
2460  st->nb_index_entries = 0;
2461  return;
2462  }
2463  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2464 
2465  // populate index
2466  for (i = 0; i < sc->chunk_count; i++) {
2467  current_offset = sc->chunk_offsets[i];
2468  if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
2469  i + 1 == sc->stsc_data[stsc_index + 1].first)
2470  stsc_index++;
2471  chunk_samples = sc->stsc_data[stsc_index].count;
2472 
2473  while (chunk_samples > 0) {
2474  AVIndexEntry *e;
2475  unsigned size, samples;
2476 
2477  if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
2479  "Zero bytes per frame, but %d samples per frame",
2480  sc->samples_per_frame);
2481  return;
2482  }
2483 
2484  if (sc->samples_per_frame >= 160) { // gsm
2485  samples = sc->samples_per_frame;
2486  size = sc->bytes_per_frame;
2487  } else {
2488  if (sc->samples_per_frame > 1) {
2489  samples = FFMIN((1024 / sc->samples_per_frame)*
2490  sc->samples_per_frame, chunk_samples);
2491  size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2492  } else {
2493  samples = FFMIN(1024, chunk_samples);
2494  size = samples * sc->sample_size;
2495  }
2496  }
2497 
2498  if (st->nb_index_entries >= total) {
2499  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2500  return;
2501  }
2502  e = &st->index_entries[st->nb_index_entries++];
2503  e->pos = current_offset;
2504  e->timestamp = current_dts;
2505  e->size = size;
2506  e->min_distance = 0;
2507  e->flags = AVINDEX_KEYFRAME;
2508  av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2509  "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2510  size, samples);
2511 
2512  current_offset += size;
2513  current_dts += samples;
2514  chunk_samples -= samples;
2515  }
2516  }
2517  }
2518 }
2519 
2520 static int mov_open_dref(AVFormatContext *s, AVIOContext **pb, char *src,
2521  MOVDref *ref)
2522 {
2523  /* try relative path, we do not try the absolute because it can leak information about our
2524  system to an attacker */
2525  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2526  char filename[1024];
2527  char *src_path;
2528  int i, l;
2529 
2530  /* find a source dir */
2531  src_path = strrchr(src, '/');
2532  if (src_path)
2533  src_path++;
2534  else
2535  src_path = src;
2536 
2537  /* find a next level down to target */
2538  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2539  if (ref->path[l] == '/') {
2540  if (i == ref->nlvl_to - 1)
2541  break;
2542  else
2543  i++;
2544  }
2545 
2546  /* compose filename if next level down to target was found */
2547  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2548  memcpy(filename, src, src_path - src);
2549  filename[src_path - src] = 0;
2550 
2551  for (i = 1; i < ref->nlvl_from; i++)
2552  av_strlcat(filename, "../", 1024);
2553 
2554  av_strlcat(filename, ref->path + l + 1, 1024);
2555 
2556  if (!s->io_open(s, pb, filename, AVIO_FLAG_READ, NULL))
2557  return 0;
2558  }
2559  }
2560 
2561  return AVERROR(ENOENT);
2562 }
2563 
2564 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2565 {
2566  AVStream *st;
2567  MOVStreamContext *sc;
2568  int ret;
2569 
2570  st = avformat_new_stream(c->fc, NULL);
2571  if (!st) return AVERROR(ENOMEM);
2572  st->id = c->fc->nb_streams;
2573  sc = av_mallocz(sizeof(MOVStreamContext));
2574  if (!sc) return AVERROR(ENOMEM);
2575 
2576  st->priv_data = sc;
2578  sc->ffindex = st->index;
2579 
2580  if ((ret = mov_read_default(c, pb, atom)) < 0)
2581  return ret;
2582 
2583  /* sanity checks */
2584  if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2585  (!sc->sample_size && !sc->sample_count))) {
2586  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2587  st->index);
2588  return 0;
2589  }
2590 
2591  if (sc->time_scale <= 0) {
2592  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
2593  sc->time_scale = c->time_scale;
2594  if (sc->time_scale <= 0)
2595  sc->time_scale = 1;
2596  }
2597 
2598  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2599 
2600  mov_build_index(c, st);
2601 
2602  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2603  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2604  if (c->enable_drefs) {
2605  if (mov_open_dref(c->fc, &sc->pb, c->fc->filename, dref) < 0)
2606  av_log(c->fc, AV_LOG_ERROR,
2607  "stream %d, error opening alias: path='%s', dir='%s', "
2608  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2609  st->index, dref->path, dref->dir, dref->filename,
2610  dref->volume, dref->nlvl_from, dref->nlvl_to);
2611  } else {
2612  av_log(c->fc, AV_LOG_WARNING,
2613  "Skipped opening external track: "
2614  "stream %d, alias: path='%s', dir='%s', "
2615  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d."
2616  "Set enable_drefs to allow this.\n",
2617  st->index, dref->path, dref->dir, dref->filename,
2618  dref->volume, dref->nlvl_from, dref->nlvl_to);
2619  }
2620  } else
2621  sc->pb = c->fc->pb;
2622 
2623  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2624  if (!st->sample_aspect_ratio.num &&
2625  (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
2626  st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
2627  ((double)st->codecpar->width * sc->height), INT_MAX);
2628  }
2629  }
2630 
2631  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2632  if (!st->codecpar->extradata_size && st->codecpar->codec_id == AV_CODEC_ID_H264 &&
2633  TAG_IS_AVCI(st->codecpar->codec_tag)) {
2634  ret = ff_generate_avci_extradata(st);
2635  if (ret < 0)
2636  return ret;
2637  }
2638 
2639  switch (st->codecpar->codec_id) {
2640 #if CONFIG_H261_DECODER
2641  case AV_CODEC_ID_H261:
2642 #endif
2643 #if CONFIG_H263_DECODER
2644  case AV_CODEC_ID_H263:
2645 #endif
2646 #if CONFIG_MPEG4_DECODER
2647  case AV_CODEC_ID_MPEG4:
2648 #endif
2649  st->codecpar->width = 0; /* let decoder init width/height */
2650  st->codecpar->height= 0;
2651  break;
2652  case AV_CODEC_ID_MP3:
2654  break;
2655  }
2656 
2657  /* Do not need those anymore. */
2658  av_freep(&sc->chunk_offsets);
2659  av_freep(&sc->sample_sizes);
2660  av_freep(&sc->keyframes);
2661  av_freep(&sc->stts_data);
2662  av_freep(&sc->stps_data);
2663  av_freep(&sc->rap_group);
2664 
2665  return 0;
2666 }
2667 
2668 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2669 {
2670  int ret;
2671  c->itunes_metadata = 1;
2672  ret = mov_read_default(c, pb, atom);
2673  c->itunes_metadata = 0;
2674  return ret;
2675 }
2676 
2677 static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int64_t size)
2678 {
2679  int64_t end = avio_tell(pb) + size;
2680  uint8_t *key = NULL, *val = NULL;
2681  int i;
2682 
2683  for (i = 0; i < 2; i++) {
2684  uint8_t **p;
2685  uint32_t len, tag;
2686  int ret;
2687 
2688  if (end - avio_tell(pb) <= 12)
2689  break;
2690 
2691  len = avio_rb32(pb);
2692  tag = avio_rl32(pb);
2693  avio_skip(pb, 4); // flags
2694 
2695  if (len < 12 || len - 12 > end - avio_tell(pb))
2696  break;
2697  len -= 12;
2698 
2699  if (tag == MKTAG('n', 'a', 'm', 'e'))
2700  p = &key;
2701  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2702  avio_skip(pb, 4);
2703  len -= 4;
2704  p = &val;
2705  } else
2706  break;
2707 
2708  *p = av_malloc(len + 1);
2709  if (!*p)
2710  break;
2711  ret = ffio_read_size(pb, *p, len);
2712  if (ret < 0) {
2713  av_freep(p);
2714  return ret;
2715  }
2716  (*p)[len] = 0;
2717  }
2718 
2719  if (key && val) {
2720  av_dict_set(&c->fc->metadata, key, val,
2722  key = val = NULL;
2723  }
2724 
2725  avio_seek(pb, end, SEEK_SET);
2726  av_freep(&key);
2727  av_freep(&val);
2728  return 0;
2729 }
2730 
2732 {
2733  int64_t end = avio_tell(pb) + atom.size;
2734  uint32_t tag, len;
2735 
2736  if (atom.size < 8)
2737  goto fail;
2738 
2739  len = avio_rb32(pb);
2740  tag = avio_rl32(pb);
2741 
2742  if (len > atom.size)
2743  goto fail;
2744 
2745  if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2746  uint8_t domain[128];
2747  int domain_len;
2748 
2749  avio_skip(pb, 4); // flags
2750  len -= 12;
2751 
2752  domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2753  avio_skip(pb, len - domain_len);
2754  if (!strcmp(domain, "org.hydrogenaudio.replaygain"))
2755  return mov_read_replaygain(c, pb, end - avio_tell(pb));
2756  }
2757 
2758 fail:
2759  av_log(c->fc, AV_LOG_VERBOSE,
2760  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2761  return 0;
2762 }
2763 
2764 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2765 {
2766  while (atom.size > 8) {
2767  uint32_t tag = avio_rl32(pb);
2768  atom.size -= 4;
2769  if (tag == MKTAG('h','d','l','r')) {
2770  avio_seek(pb, -8, SEEK_CUR);
2771  atom.size += 8;
2772  return mov_read_default(c, pb, atom);
2773  }
2774  }
2775  return 0;
2776 }
2777 
2778 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2779 {
2780  int i;
2781  int width;
2782  int height;
2783  int64_t disp_transform[2];
2784  int display_matrix[3][3];
2785  AVStream *st;
2786  MOVStreamContext *sc;
2787  int version;
2788  int flags;
2789 
2790  if (c->fc->nb_streams < 1)
2791  return 0;
2792  st = c->fc->streams[c->fc->nb_streams-1];
2793  sc = st->priv_data;
2794 
2795  version = avio_r8(pb);
2796  flags = avio_rb24(pb);
2798 
2799  if (version == 1) {
2800  avio_rb64(pb);
2801  avio_rb64(pb);
2802  } else {
2803  avio_rb32(pb); /* creation time */
2804  avio_rb32(pb); /* modification time */
2805  }
2806  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2807  avio_rb32(pb); /* reserved */
2808 
2809  /* highlevel (considering edits) duration in movie timebase */
2810  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2811  avio_rb32(pb); /* reserved */
2812  avio_rb32(pb); /* reserved */
2813 
2814  avio_rb16(pb); /* layer */
2815  avio_rb16(pb); /* alternate group */
2816  avio_rb16(pb); /* volume */
2817  avio_rb16(pb); /* reserved */
2818 
2819  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2820  // they're kept in fixed point format through all calculations
2821  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2822  // side data, but the scale factor is not needed to calculate aspect ratio
2823  for (i = 0; i < 3; i++) {
2824  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2825  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2826  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
2827  }
2828 
2829  width = avio_rb32(pb); // 16.16 fixed point track width
2830  height = avio_rb32(pb); // 16.16 fixed point track height
2831  sc->width = width >> 16;
2832  sc->height = height >> 16;
2833 
2834  // save the matrix when it is not the default identity
2835  if (display_matrix[0][0] != (1 << 16) ||
2836  display_matrix[1][1] != (1 << 16) ||
2837  display_matrix[2][2] != (1 << 30) ||
2838  display_matrix[0][1] || display_matrix[0][2] ||
2839  display_matrix[1][0] || display_matrix[1][2] ||
2840  display_matrix[2][0] || display_matrix[2][1]) {
2841  int i, j;
2842 
2843  av_freep(&sc->display_matrix);
2844  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2845  if (!sc->display_matrix)
2846  return AVERROR(ENOMEM);
2847 
2848  for (i = 0; i < 3; i++)
2849  for (j = 0; j < 3; j++)
2850  sc->display_matrix[i * 3 + j] = display_matrix[i][j];
2851  }
2852 
2853  // transform the display width/height according to the matrix
2854  // skip this when the display matrix is the identity one
2855  // to keep the same scale, use [width height 1<<16]
2856  if (width && height && sc->display_matrix) {
2857  for (i = 0; i < 2; i++)
2858  disp_transform[i] =
2859  (int64_t) width * display_matrix[0][i] +
2860  (int64_t) height * display_matrix[1][i] +
2861  ((int64_t) display_matrix[2][i] << 16);
2862 
2863  //sample aspect ratio is new width/height divided by old width/height
2864  if (disp_transform[0] > 0 && disp_transform[1] > 0)
2866  ((double) disp_transform[0] * height) /
2867  ((double) disp_transform[1] * width), INT_MAX);
2868  }
2869  return 0;
2870 }
2871 
2872 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2873 {
2874  MOVFragment *frag = &c->fragment;
2875  MOVTrackExt *trex = NULL;
2876  int flags, track_id, i;
2877 
2878  avio_r8(pb); /* version */
2879  flags = avio_rb24(pb);
2880 
2881  track_id = avio_rb32(pb);
2882  if (!track_id)
2883  return AVERROR_INVALIDDATA;
2884  frag->track_id = track_id;
2885  for (i = 0; i < c->trex_count; i++)
2886  if (c->trex_data[i].track_id == frag->track_id) {
2887  trex = &c->trex_data[i];
2888  break;
2889  }
2890  if (!trex) {
2891  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2892  return AVERROR_INVALIDDATA;
2893  }
2894 
2897  frag->moof_offset : frag->implicit_offset;
2898  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2899 
2900  frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2901  avio_rb32(pb) : trex->duration;
2902  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2903  avio_rb32(pb) : trex->size;
2904  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2905  avio_rb32(pb) : trex->flags;
2906  av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags);
2907  return 0;
2908 }
2909 
2910 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2911 {
2912  c->chapter_track = avio_rb32(pb);
2913  return 0;
2914 }
2915 
2916 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2917 {
2918  MOVTrackExt *trex;
2919  int err;
2920 
2921  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2922  return AVERROR_INVALIDDATA;
2923  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2924  sizeof(*c->trex_data))) < 0) {
2925  c->trex_count = 0;
2926  return err;
2927  }
2928  trex = &c->trex_data[c->trex_count++];
2929  avio_r8(pb); /* version */
2930  avio_rb24(pb); /* flags */
2931  trex->track_id = avio_rb32(pb);
2932  trex->stsd_id = avio_rb32(pb);
2933  trex->duration = avio_rb32(pb);
2934  trex->size = avio_rb32(pb);
2935  trex->flags = avio_rb32(pb);
2936  return 0;
2937 }
2938 
2939 static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2940 {
2941  MOVFragment *frag = &c->fragment;
2942  AVStream *st = NULL;
2943  MOVStreamContext *sc;
2944  int version, i;
2945 
2946  for (i = 0; i < c->fc->nb_streams; i++) {
2947  if (c->fc->streams[i]->id == frag->track_id) {
2948  st = c->fc->streams[i];
2949  break;
2950  }
2951  }
2952  if (!st) {
2953  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2954  return AVERROR_INVALIDDATA;
2955  }
2956  sc = st->priv_data;
2957  if (sc->pseudo_stream_id + 1 != frag->stsd_id)
2958  return 0;
2959  version = avio_r8(pb);
2960  avio_rb24(pb); /* flags */
2961  if (version) {
2962  sc->track_end = avio_rb64(pb);
2963  } else {
2964  sc->track_end = avio_rb32(pb);
2965  }
2966  return 0;
2967 }
2968 
2969 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2970 {
2971  MOVFragment *frag = &c->fragment;
2972  AVStream *st = NULL;
2973  MOVStreamContext *sc;
2974  MOVStts *ctts_data;
2975  uint64_t offset;
2976  int64_t dts;
2977  int data_offset = 0;
2978  unsigned entries, first_sample_flags = frag->flags;
2979  int flags, distance, i, err;
2980 
2981  for (i = 0; i < c->fc->nb_streams; i++) {
2982  if (c->fc->streams[i]->id == frag->track_id) {
2983  st = c->fc->streams[i];
2984  break;
2985  }
2986  }
2987  if (!st) {
2988  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2989  return AVERROR_INVALIDDATA;
2990  }
2991  sc = st->priv_data;
2992  if (sc->pseudo_stream_id+1 != frag->stsd_id)
2993  return 0;
2994  avio_r8(pb); /* version */
2995  flags = avio_rb24(pb);
2996  entries = avio_rb32(pb);
2997  av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries);
2998 
2999  /* Always assume the presence of composition time offsets.
3000  * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
3001  * 1) in the initial movie, there are no samples.
3002  * 2) in the first movie fragment, there is only one sample without composition time offset.
3003  * 3) in the subsequent movie fragments, there are samples with composition time offset. */
3004  if (!sc->ctts_count && sc->sample_count)
3005  {
3006  /* Complement ctts table if moov atom doesn't have ctts atom. */
3007  ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
3008  if (!ctts_data)
3009  return AVERROR(ENOMEM);
3010  sc->ctts_data = ctts_data;
3011  sc->ctts_data[sc->ctts_count].count = sc->sample_count;
3012  sc->ctts_data[sc->ctts_count].duration = 0;
3013  sc->ctts_count++;
3014  }
3015  if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
3016  return AVERROR_INVALIDDATA;
3017  if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
3018  sizeof(*sc->ctts_data))) < 0) {
3019  sc->ctts_count = 0;
3020  return err;
3021  }
3022  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
3023  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
3024  dts = sc->track_end - sc->time_offset;
3025  offset = frag->base_data_offset + data_offset;
3026  distance = 0;
3027  av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags);
3028  for (i = 0; i < entries && !pb->eof_reached; i++) {
3029  unsigned sample_size = frag->size;
3030  int sample_flags = i ? frag->flags : first_sample_flags;
3031  unsigned sample_duration = frag->duration;
3032  int keyframe = 0;
3033 
3034  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
3035  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
3036  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
3037  sc->ctts_data[sc->ctts_count].count = 1;
3038  sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
3039  avio_rb32(pb) : 0;
3040  sc->ctts_count++;
3042  keyframe = 1;
3043  else
3044  keyframe =
3045  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
3047  if (keyframe)
3048  distance = 0;
3049  av_add_index_entry(st, offset, dts, sample_size, distance,
3050  keyframe ? AVINDEX_KEYFRAME : 0);
3051  av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
3052  "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
3053  offset, dts, sample_size, distance, keyframe);
3054  distance++;
3055  dts += sample_duration;
3056  offset += sample_size;
3057  sc->data_size += sample_size;
3058  }
3059 
3060  if (pb->eof_reached)
3061  return AVERROR_EOF;
3062 
3063  frag->implicit_offset = offset;
3064  st->duration = sc->track_end = dts + sc->time_offset;
3065  return 0;
3066 }
3067 
3068 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
3069 /* like the files created with Adobe Premiere 5.0, for samples see */
3070 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
3071 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3072 {
3073  int err;
3074 
3075  if (atom.size < 8)
3076  return 0; /* continue */
3077  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
3078  avio_skip(pb, atom.size - 4);
3079  return 0;
3080  }
3081  atom.type = avio_rl32(pb);
3082  atom.size -= 8;
3083  if (atom.type != MKTAG('m','d','a','t')) {
3084  avio_skip(pb, atom.size);
3085  return 0;
3086  }
3087  err = mov_read_mdat(c, pb, atom);
3088  return err;
3089 }
3090 
3091 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3092 {
3093 #if CONFIG_ZLIB
3094  AVIOContext ctx;
3095  uint8_t *cmov_data;
3096  uint8_t *moov_data; /* uncompressed data */
3097  long cmov_len, moov_len;
3098  int ret = -1;
3099 
3100  avio_rb32(pb); /* dcom atom */
3101  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
3102  return AVERROR_INVALIDDATA;
3103  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
3104  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
3105  return AVERROR_INVALIDDATA;
3106  }
3107  avio_rb32(pb); /* cmvd atom */
3108  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
3109  return AVERROR_INVALIDDATA;
3110  moov_len = avio_rb32(pb); /* uncompressed size */
3111  cmov_len = atom.size - 6 * 4;
3112 
3113  cmov_data = av_malloc(cmov_len);
3114  if (!cmov_data)
3115  return AVERROR(ENOMEM);
3116  moov_data = av_malloc(moov_len);
3117  if (!moov_data) {
3118  av_free(cmov_data);
3119  return AVERROR(ENOMEM);
3120  }
3121  ret = ffio_read_size(pb, cmov_data, cmov_len);
3122  if (ret < 0)
3123  goto free_and_return;
3124 
3125  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
3126  goto free_and_return;
3127  if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
3128  goto free_and_return;
3129  atom.type = MKTAG('m','o','o','v');
3130  atom.size = moov_len;
3131  ret = mov_read_default(c, &ctx, atom);
3132 free_and_return:
3133  av_free(moov_data);
3134  av_free(cmov_data);
3135  return ret;
3136 #else
3137  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
3138  return AVERROR(ENOSYS);
3139 #endif
3140 }
3141 
3142 /* edit list atom */
3143 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
3144 {
3145  MOVStreamContext *sc;
3146  int i, edit_count, version;
3147 
3148  if (c->fc->nb_streams < 1)
3149  return 0;
3150  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
3151 
3152  version = avio_r8(pb); /* version */
3153  avio_rb24(pb); /* flags */
3154  edit_count = avio_rb32(pb); /* entries */
3155 
3156  if ((uint64_t)edit_count*12+8 > atom.size)
3157  return AVERROR_INVALIDDATA;
3158 
3159  for (i=0; i<edit_count; i++){
3160  int64_t time;
3161  int64_t duration;
3162  if (version == 1) {
3163  duration = avio_rb64(pb);
3164  time = avio_rb64(pb);
3165  } else {
3166  duration = avio_rb32(pb); /* segment duration */
3167  time = (int32_t)avio_rb32(pb); /* media time */
3168  }
3169  avio_rb32(pb); /* Media rate */
3170  if (i == 0 && time >= -1) {
3171  sc->time_offset = time != -1 ? time : -duration;
3172  }
3173  }
3174 
3175  if (edit_count > 1)
3176  av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
3177  "a/v desync might occur, patch welcome\n");
3178 
3179  av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
3180  return 0;
3181 }
3182 
3184 { MKTAG('a','v','s','s'), mov_read_extradata },
3185 { MKTAG('c','h','p','l'), mov_read_chpl },
3186 { MKTAG('c','o','6','4'), mov_read_stco },
3187 { MKTAG('c','o','l','r'), mov_read_colr },
3188 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
3189 { MKTAG('d','i','n','f'), mov_read_default },
3190 { MKTAG('d','r','e','f'), mov_read_dref },
3191 { MKTAG('e','d','t','s'), mov_read_default },
3192 { MKTAG('e','l','s','t'), mov_read_elst },
3193 { MKTAG('e','n','d','a'), mov_read_enda },
3194 { MKTAG('f','i','e','l'), mov_read_fiel },
3195 { MKTAG('f','t','y','p'), mov_read_ftyp },
3196 { MKTAG('g','l','b','l'), mov_read_glbl },
3197 { MKTAG('h','d','l','r'), mov_read_hdlr },
3198 { MKTAG('i','l','s','t'), mov_read_ilst },
3199 { MKTAG('j','p','2','h'), mov_read_extradata },
3200 { MKTAG('m','d','a','t'), mov_read_mdat },
3201 { MKTAG('m','d','h','d'), mov_read_mdhd },
3202 { MKTAG('m','d','i','a'), mov_read_default },
3203 { MKTAG('m','e','t','a'), mov_read_meta },
3204 { MKTAG('m','i','n','f'), mov_read_default },
3205 { MKTAG('m','o','o','f'), mov_read_moof },
3206 { MKTAG('m','o','o','v'), mov_read_moov },
3207 { MKTAG('m','v','e','x'), mov_read_default },
3208 { MKTAG('m','v','h','d'), mov_read_mvhd },
3209 { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
3210 { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
3211 { MKTAG('a','v','c','C'), mov_read_glbl },
3212 { MKTAG('p','a','s','p'), mov_read_pasp },
3213 { MKTAG('s','t','b','l'), mov_read_default },
3214 { MKTAG('s','t','c','o'), mov_read_stco },
3215 { MKTAG('s','t','p','s'), mov_read_stps },
3216 { MKTAG('s','t','r','f'), mov_read_strf },
3217 { MKTAG('s','t','s','c'), mov_read_stsc },
3218 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
3219 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
3220 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
3221 { MKTAG('s','t','t','s'), mov_read_stts },
3222 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
3223 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
3224 { MKTAG('t','f','d','t'), mov_read_tfdt },
3225 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
3226 { MKTAG('t','r','a','k'), mov_read_trak },
3227 { MKTAG('t','r','a','f'), mov_read_default },
3228 { MKTAG('t','r','e','f'), mov_read_default },
3229 { MKTAG('c','h','a','p'), mov_read_chap },
3230 { MKTAG('t','r','e','x'), mov_read_trex },
3231 { MKTAG('t','r','u','n'), mov_read_trun },
3232 { MKTAG('u','d','t','a'), mov_read_default },
3233 { MKTAG('w','a','v','e'), mov_read_wave },
3234 { MKTAG('e','s','d','s'), mov_read_esds },
3235 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
3236 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
3237 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
3238 { MKTAG('w','f','e','x'), mov_read_wfex },
3239 { MKTAG('c','m','o','v'), mov_read_cmov },
3240 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
3241 { MKTAG('d','v','c','1'), mov_read_dvc1 },
3242 { MKTAG('s','b','g','p'), mov_read_sbgp },
3243 { MKTAG('h','v','c','C'), mov_read_glbl },
3244 { MKTAG('-','-','-','-'), mov_read_custom },
3245 { 0, NULL }
3246 };
3247 
3249 {
3250  int64_t total_size = 0;
3251  MOVAtom a;
3252  int i;
3253 
3254  if (atom.size < 0)
3255  atom.size = INT64_MAX;
3256  while (total_size + 8 < atom.size && !pb->eof_reached) {
3257  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
3258  a.size = atom.size;
3259  a.type=0;
3260  if (atom.size >= 8) {
3261  a.size = avio_rb32(pb);
3262  a.type = avio_rl32(pb);
3263  }
3264  av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
3265  a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
3266  total_size += 8;
3267  if (a.size == 1) { /* 64 bit extended size */
3268  a.size = avio_rb64(pb) - 8;
3269  total_size += 8;
3270  }
3271  if (a.size == 0) {
3272  a.size = atom.size - total_size;
3273  if (a.size <= 8)
3274  break;
3275  }
3276  a.size -= 8;
3277  if (a.size < 0)
3278  break;
3279  a.size = FFMIN(a.size, atom.size - total_size);
3280 
3281  for (i = 0; mov_default_parse_table[i].type; i++)
3282  if (mov_default_parse_table[i].type == a.type) {
3283  parse = mov_default_parse_table[i].parse;
3284  break;
3285  }
3286 
3287  // container is user data
3288  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
3289  atom.type == MKTAG('i','l','s','t')))
3291 
3292  if (!parse) { /* skip leaf atoms data */
3293  avio_skip(pb, a.size);
3294  } else {
3295  int64_t start_pos = avio_tell(pb);
3296  int64_t left;
3297  int err = parse(c, pb, a);
3298  if (err < 0)
3299  return err;
3300  if (c->found_moov && c->found_mdat &&
3301  ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
3302  start_pos + a.size == avio_size(pb))) {
3303  if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
3304  c->next_root_atom = start_pos + a.size;
3305  return 0;
3306  }
3307  left = a.size - avio_tell(pb) + start_pos;
3308  if (left > 0) /* skip garbage at atom end */
3309  avio_skip(pb, left);
3310  else if (left < 0) {
3311  av_log(c->fc, AV_LOG_WARNING,
3312  "overread end of atom '%.4s' by %"PRId64" bytes\n",
3313  (char*)&a.type, -left);
3314  avio_seek(pb, left, SEEK_CUR);
3315  }
3316  }
3317 
3318  total_size += a.size;
3319  }
3320 
3321  if (total_size < atom.size && atom.size < 0x7ffff)
3322  avio_skip(pb, atom.size - total_size);
3323 
3324  return 0;
3325 }
3326 
3327 static int mov_probe(AVProbeData *p)
3328 {
3329  unsigned int offset;
3330  uint32_t tag;
3331  int score = 0;
3332 
3333  /* check file header */
3334  offset = 0;
3335  for (;;) {
3336  /* ignore invalid offset */
3337  if ((offset + 8) > (unsigned int)p->buf_size)
3338  return score;
3339  tag = AV_RL32(p->buf + offset + 4);
3340  switch(tag) {
3341  /* check for obvious tags */
3342  case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
3343  case MKTAG('m','o','o','v'):
3344  case MKTAG('m','d','a','t'):
3345  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
3346  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
3347  case MKTAG('f','t','y','p'):
3348  return AVPROBE_SCORE_MAX;
3349  /* those are more common words, so rate then a bit less */
3350  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
3351  case MKTAG('w','i','d','e'):
3352  case MKTAG('f','r','e','e'):
3353  case MKTAG('j','u','n','k'):
3354  case MKTAG('p','i','c','t'):
3355  return AVPROBE_SCORE_MAX - 5;
3356  case MKTAG(0x82,0x82,0x7f,0x7d):
3357  case MKTAG('s','k','i','p'):
3358  case MKTAG('u','u','i','d'):
3359  case MKTAG('p','r','f','l'):
3360  offset = AV_RB32(p->buf+offset) + offset;
3361  /* if we only find those cause probedata is too small at least rate them */
3362  score = AVPROBE_SCORE_EXTENSION;
3363  break;
3364  default:
3365  /* unrecognized tag */
3366  return score;
3367  }
3368  }
3369 }
3370 
3371 // must be done after parsing all trak because there's no order requirement
3373 {
3374  MOVContext *mov = s->priv_data;
3375  AVStream *st = NULL;
3376  MOVStreamContext *sc;
3377  int64_t cur_pos;
3378  int i;
3379 
3380  for (i = 0; i < s->nb_streams; i++)
3381  if (s->streams[i]->id == mov->chapter_track) {
3382  st = s->streams[i];
3383  break;
3384  }
3385  if (!st) {
3386  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
3387  return;
3388  }
3389 
3390  st->discard = AVDISCARD_ALL;
3391  sc = st->priv_data;
3392  cur_pos = avio_tell(sc->pb);
3393 
3394  for (i = 0; i < st->nb_index_entries; i++) {
3395  AVIndexEntry *sample = &st->index_entries[i];
3396  int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
3397  uint8_t *title;
3398  uint16_t ch;
3399  int len, title_len;
3400 
3401  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3402  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
3403  goto finish;
3404  }
3405 
3406  // the first two bytes are the length of the title
3407  len = avio_rb16(sc->pb);
3408  if (len > sample->size-2)
3409  continue;
3410  title_len = 2*len + 1;
3411  if (!(title = av_mallocz(title_len)))
3412  goto finish;
3413 
3414  // The samples could theoretically be in any encoding if there's an encd
3415  // atom following, but in practice are only utf-8 or utf-16, distinguished
3416  // instead by the presence of a BOM
3417  if (!len) {
3418  title[0] = 0;
3419  } else {
3420  ch = avio_rb16(sc->pb);
3421  if (ch == 0xfeff)
3422  avio_get_str16be(sc->pb, len, title, title_len);
3423  else if (ch == 0xfffe)
3424  avio_get_str16le(sc->pb, len, title, title_len);
3425  else {
3426  AV_WB16(title, ch);
3427  if (len == 1 || len == 2)
3428  title[len] = 0;
3429  else
3430  avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
3431  }
3432  }
3433 
3434  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3435  av_freep(&title);
3436  }
3437 finish:
3438  avio_seek(sc->pb, cur_pos, SEEK_SET);
3439 }
3440 
3442 {
3443  MOVContext *mov = s->priv_data;
3444  int i, j;
3445 
3446  for (i = 0; i < s->nb_streams; i++) {
3447  AVStream *st = s->streams[i];
3448  MOVStreamContext *sc = st->priv_data;
3449 
3450  av_freep(&sc->ctts_data);
3451  for (j = 0; j < sc->drefs_count; j++) {
3452  av_freep(&sc->drefs[j].path);
3453  av_freep(&sc->drefs[j].dir);
3454  }
3455  av_freep(&sc->drefs);
3456  if (sc->pb && sc->pb != s->pb)
3457  ff_format_io_close(s, &sc->pb);
3458 
3459  av_freep(&sc->chunk_offsets);
3460  av_freep(&sc->stsc_data);
3461  av_freep(&sc->sample_sizes);
3462  av_freep(&sc->keyframes);
3463  av_freep(&sc->stts_data);
3464  av_freep(&sc->stps_data);
3465  av_freep(&sc->rap_group);
3466  av_freep(&sc->display_matrix);
3467 
3468  for (j = 0; j < sc->stsd_count; j++)
3469  av_free(sc->extradata[j]);
3470  av_freep(&sc->extradata);
3471  av_freep(&sc->extradata_size);
3472  }
3473 
3474  if (mov->dv_demux) {
3476  mov->dv_fctx = NULL;
3477  }
3478 
3479  av_freep(&mov->trex_data);
3480 
3481  return 0;
3482 }
3483 
3485 {
3486  MOVContext *mov = s->priv_data;
3487  AVIOContext *pb = s->pb;
3488  int err;
3489  MOVAtom atom = { AV_RL32("root") };
3490  int i;
3491 
3492  mov->fc = s;
3493  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3494  if (pb->seekable)
3495  atom.size = avio_size(pb);
3496  else
3497  atom.size = INT64_MAX;
3498 
3499  /* check MOV header */
3500  if ((err = mov_read_default(mov, pb, atom)) < 0) {
3501  av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3502  mov_read_close(s);
3503  return err;
3504  }
3505  if (!mov->found_moov) {
3506  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3507  mov_read_close(s);
3508  return AVERROR_INVALIDDATA;
3509  }
3510  av_log(mov->fc, AV_LOG_TRACE, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3511 
3512  if (pb->seekable && mov->chapter_track > 0)
3513  mov_read_chapters(s);
3514 
3515  for (i = 0; i < s->nb_streams; i++) {
3516  AVStream *st = s->streams[i];
3517  MOVStreamContext *sc = st->priv_data;
3518 
3520  if (st->codecpar->width <= 0 || st->codecpar->height <= 0) {
3521  st->codecpar->width = sc->width;
3522  st->codecpar->height = sc->height;
3523  }
3525  if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
3526  return err;
3527  }
3528  }
3529  }
3530 
3531  if (mov->trex_data) {
3532  for (i = 0; i < s->nb_streams; i++) {
3533  AVStream *st = s->streams[i];
3534  MOVStreamContext *sc = st->priv_data;
3535  if (st->duration > 0)
3536  st->codecpar->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3537  }
3538  }
3539 
3540  for (i = 0; i < s->nb_streams; i++) {
3541  AVStream *st = s->streams[i];
3542  MOVStreamContext *sc = st->priv_data;
3543 
3544  switch (st->codecpar->codec_type) {
3545  case AVMEDIA_TYPE_AUDIO:
3546  err = ff_replaygain_export(st, s->metadata);
3547  if (err < 0) {
3548  mov_read_close(s);
3549  return err;
3550  }
3551  break;
3552  case AVMEDIA_TYPE_VIDEO:
3553  if (sc->display_matrix) {
3554  AVPacketSideData *sd, *tmp;
3555 
3556  tmp = av_realloc_array(st->side_data,
3557  st->nb_side_data + 1, sizeof(*tmp));
3558  if (!tmp)
3559  return AVERROR(ENOMEM);
3560 
3561  st->side_data = tmp;
3562  st->nb_side_data++;
3563 
3564  sd = &st->side_data[st->nb_side_data - 1];
3566  sd->size = sizeof(int32_t) * 9;
3567  sd->data = (uint8_t*)sc->display_matrix;
3568  sc->display_matrix = NULL;
3569  }
3570  break;
3571  }
3572  }
3573 
3574  return 0;
3575 }
3576 
3578 {
3580  int64_t best_dts = INT64_MAX;
3581  int i;
3582  for (i = 0; i < s->nb_streams; i++) {
3583  AVStream *avst = s->streams[i];
3584  MOVStreamContext *msc = avst->priv_data;
3585  if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3586  AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3587  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3588  av_log(s, AV_LOG_TRACE, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3589  if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3590  (s->pb->seekable &&
3591  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3592  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3593  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3594  sample = current_sample;
3595  best_dts = dts;
3596  *st = avst;
3597  }
3598  }
3599  }
3600  return sample;
3601 }
3602 
3604 {
3605  uint8_t *side, *extradata;
3606  int extradata_size;
3607 
3608  /* Save the current index. */
3609  sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
3610 
3611  /* Notify the decoder that extradata changed. */
3612  extradata_size = sc->extradata_size[sc->last_stsd_index];
3613  extradata = sc->extradata[sc->last_stsd_index];
3614  if (extradata_size > 0 && extradata) {
3615  side = av_packet_new_side_data(pkt,
3617  extradata_size);
3618  if (!side)
3619  return AVERROR(ENOMEM);
3620  memcpy(side, extradata, extradata_size);
3621  }
3622 
3623  return 0;
3624 }
3625 
3627 {
3628  MOVContext *mov = s->priv_data;
3629  MOVStreamContext *sc;
3631  AVStream *st = NULL;
3632  int ret;
3633  retry:
3634  sample = mov_find_next_sample(s, &st);
3635  if (!sample) {
3636  mov->found_mdat = 0;
3637  if (!mov->next_root_atom)
3638  return AVERROR_EOF;
3639  avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3640  mov->next_root_atom = 0;
3641  if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3642  s->pb->eof_reached)
3643  return AVERROR_EOF;
3644  av_log(s, AV_LOG_TRACE, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3645  goto retry;
3646  }
3647  sc = st->priv_data;
3648  /* must be done just before reading, to avoid infinite loop on sample */
3649  sc->current_sample++;
3650 
3651  if (st->discard != AVDISCARD_ALL) {
3652  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3653  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3654  sc->ffindex, sample->pos);
3655  return AVERROR_INVALIDDATA;
3656  }
3657  ret = av_get_packet(sc->pb, pkt, sample->size);
3658  if (ret < 0)
3659  return ret;
3660  if (sc->has_palette) {
3661  uint8_t *pal;
3662 
3664  if (!pal) {
3665  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3666  } else {
3667  memcpy(pal, sc->palette, AVPALETTE_SIZE);
3668  sc->has_palette = 0;
3669  }
3670  }
3671 #if CONFIG_DV_DEMUXER
3672  if (mov->dv_demux && sc->dv_audio_container) {
3673  avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
3674  av_free(pkt->data);
3675  pkt->size = 0;
3676  ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3677  if (ret < 0)
3678  return ret;
3679  }
3680 #endif
3681  }
3682 
3683  pkt->stream_index = sc->ffindex;
3684  pkt->dts = sample->timestamp;
3685  if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3686  pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3687  /* update ctts context */
3688  sc->ctts_sample++;
3689  if (sc->ctts_index < sc->ctts_count &&
3690  sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3691  sc->ctts_index++;
3692  sc->ctts_sample = 0;
3693  }
3694  } else {
3695  int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3697  pkt->duration = next_dts - pkt->dts;
3698  pkt->pts = pkt->dts;
3699  }
3700  if (st->discard == AVDISCARD_ALL)
3701  goto retry;
3702  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3703  pkt->pos = sample->pos;
3704  av_log(s, AV_LOG_TRACE, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %"PRId64"\n",
3705  pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3706 
3707  /* Multiple stsd handling. */
3708  if (sc->stsc_data) {
3709  /* Keep track of the stsc index for the given sample, then check
3710  * if the stsd index is different from the last used one. */
3711  sc->stsc_sample++;
3712  if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
3713  mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
3714  sc->stsc_index++;
3715  sc->stsc_sample = 0;
3716  /* Do not check indexes after a switch. */
3717  } else if (sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
3718  ret = mov_change_extradata(sc, pkt);
3719  if (ret < 0)
3720  return ret;
3721  }
3722  }
3723 
3724  return 0;
3725 }
3726 
3727 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3728 {
3729  MOVStreamContext *sc = st->priv_data;
3730  int sample, time_sample;
3731  int i;
3732 
3733  sample = av_index_search_timestamp(st, timestamp, flags);
3734  av_log(s, AV_LOG_TRACE, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3735  if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3736  sample = 0;
3737  if (sample < 0) /* not sure what to do */
3738  return AVERROR_INVALIDDATA;
3739  sc->current_sample = sample;
3740  av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
3741  /* adjust ctts index */
3742  if (sc->ctts_data) {
3743  time_sample = 0;
3744  for (i = 0; i < sc->ctts_count; i++) {
3745  int next = time_sample + sc->ctts_data[i].count;
3746  if (next > sc->current_sample) {
3747  sc->ctts_index = i;
3748  sc->ctts_sample = sc->current_sample - time_sample;
3749  break;
3750  }
3751  time_sample = next;
3752  }
3753  }
3754 
3755  /* adjust stsd index */
3756  time_sample = 0;
3757  for (i = 0; i < sc->stsc_count; i++) {
3758  int next = time_sample + mov_get_stsc_samples(sc, i);
3759  if (next > sc->current_sample) {
3760  sc->stsc_index = i;
3761  sc->stsc_sample = sc->current_sample - time_sample;
3762  break;
3763  }
3764  time_sample = next;
3765  }
3766 
3767  return sample;
3768 }
3769 
3770 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3771 {
3772  MOVContext *mc = s->priv_data;
3773  AVStream *st;
3774  int sample;
3775  int i;
3776 
3777  if (stream_index >= s->nb_streams)
3778  return AVERROR_INVALIDDATA;
3779  if (sample_time < 0)
3780  sample_time = 0;
3781 
3782  st = s->streams[stream_index];
3783  sample = mov_seek_stream(s, st, sample_time, flags);
3784  if (sample < 0)
3785  return sample;
3786 
3787  if (mc->seek_individually) {
3788  /* adjust seek timestamp to found sample timestamp */
3789  int64_t seek_timestamp = st->index_entries[sample].timestamp;
3790 
3791  for (i = 0; i < s->nb_streams; i++) {
3792  int64_t timestamp;
3793  st = s->streams[i];
3794  if (stream_index == i)
3795  continue;
3796 
3797  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
3798  mov_seek_stream(s, st, timestamp, flags);
3799  }
3800  } else {
3801  for (i = 0; i < s->nb_streams; i++) {
3802  MOVStreamContext *sc;
3803  st = s->streams[i];
3804  sc = st->priv_data;
3805  sc->current_sample = 0;
3806  }
3807  while (1) {
3808  MOVStreamContext *sc;
3809  AVIndexEntry *entry = mov_find_next_sample(s, &st);
3810  if (!entry)
3811  return AVERROR_INVALIDDATA;
3812  sc = st->priv_data;
3813  if (sc->ffindex == stream_index && sc->current_sample == sample)
3814  break;
3815  sc->current_sample++;
3816  }
3817  }
3818  return 0;
3819 }
3820 
3821 #define OFFSET(x) offsetof(MOVContext, x)
3822 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
3823 static const AVOption mov_options[] = {
3824  {"seek_streams_individually",
3825  "Seek each stream individually to the to the closest point",
3826  OFFSET(seek_individually), AV_OPT_TYPE_INT, { .i64 = 1 },
3827  0, 1, FLAGS},
3828  { "export_all", "Export unrecognized metadata entries", OFFSET(export_all),
3829  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
3830  { "export_xmp", "Export full XMP metadata", OFFSET(export_xmp),
3831  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
3832  { "enable_drefs", "Enable external track support.", OFFSET(enable_drefs),
3833  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS },
3834  { NULL },
3835 };
3836 
3837 static const AVClass mov_class = {
3838  .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
3839  .item_name = av_default_item_name,
3840  .option = mov_options,
3841  .version = LIBAVUTIL_VERSION_INT,
3842 };
3843 
3845  .name = "mov,mp4,m4a,3gp,3g2,mj2",
3846  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3847  .priv_class = &mov_class,
3848  .priv_data_size = sizeof(MOVContext),
3849  .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
3850  .read_probe = mov_probe,
3855 };
int chapter_track
Definition: isom.h:164
int itunes_metadata
metadata are itunes style
Definition: isom.h:163
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:345
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:891
#define AVPALETTE_SIZE
Definition: avcodec.h:3402
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
#define AV_RB8(x)
Definition: intreadwrite.h:368
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3540
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:696
static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1023
Bytestream IO Context.
Definition: avio.h:104
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:3547
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
#define MP4ESDescrTag
Definition: isom.h:179
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:737
unsigned int rap_group_count
Definition: isom.h:138
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition: isom.c:426
static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3071
AVOption.
Definition: opt.h:234
MOVTrackExt * trex_data
Definition: isom.h:161
int export_all
Definition: isom.h:167
static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
Definition: mov.c:3603
unsigned track_id
Definition: isom.h:73
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:1214
#define MOV_TFHD_DEFAULT_DURATION
Definition: isom.h:186
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:3577
enum AVCodecID id
Definition: mxfenc.c:85
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:442
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
unsigned int samples_per_frame
Definition: isom.h:124
static void mov_build_index(MOVContext *mov, AVStream *st)
Definition: mov.c:2320
int dv_audio_container
Definition: isom.h:125
static ColorEntry color_table[]
Definition: parseutils.c:155
Definition: isom.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
uint64_t base_data_offset
Definition: isom.h:74
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
#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:2986
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:592
int64_t pos
Definition: avformat.h:664
unsigned int stsc_count
Definition: isom.h:106
int has_palette
Definition: isom.h:135
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:349
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:116
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
SMPTE ST 432-1 D65 (2010)
Definition: pixfmt.h:308
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2564
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, 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 MOV_TRUN_SAMPLE_CTS
Definition: isom.h:197
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#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:890
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1218
SMPTE ST 431-2 (2011)
Definition: pixfmt.h:307
static const uint8_t ff_qt_default_palette_256[256 *3]
Definition: qtpalette.h:54
void * priv_data
Definition: avformat.h:720
int16_t audio_cid
stsd audio compression id
Definition: isom.h:127
#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:323
discard all
Definition: avcodec.h:689
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:808
int height
tkhd height
Definition: isom.h:132
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:681
int(* parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:62
static int mov_get_stsc_samples(MOVStreamContext *sc, int index)
Definition: mov.c:2003
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:276
#define sample
uint32_t type
Definition: isom.h:66
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.
MOVStsc * stsc_data
Definition: isom.h:107
enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
Compute codec id for &#39;lpcm&#39; tag.
Definition: mov.c:1375
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:956
int ctts_index
Definition: isom.h:112
unsigned stsd_id
Definition: isom.h:85
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
functionally identical to above
Definition: pixfmt.h:351
int found_moov
&#39;moov&#39; atom has been found
Definition: isom.h:155
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2015
enum AVColorSpace color_space
Definition: avcodec.h:3548
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:190
const char * av_color_space_name(enum AVColorSpace space)
Definition: pixdesc.c:1890
Macro definitions for various function/variable attributes.
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:61
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
unsigned flags
Definition: isom.h:88
#define MOV_TKHD_FLAG_ENABLED
Definition: isom.h:209
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:2758
int isom
1 if file is ISO Media (mp4/3gp)
Definition: isom.h:159
int found_mdat
&#39;mdat&#39; atom has been found
Definition: isom.h:156
int width
tkhd width
Definition: isom.h:131
unsigned drefs_count
Definition: isom.h:128
static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:994
Format I/O context.
Definition: avformat.h:940
static int mov_probe(AVProbeData *p)
Definition: mov.c:3327
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:3484
char filename[64]
Definition: isom.h:61
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:39
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1053
static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1687
Public dictionary API.
MOVDref * drefs
Definition: isom.h:129
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:879
static char buffer[20]
Definition: seek.c:32
static const AVOption mov_options[]
Definition: mov.c:3823
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
Opaque data information usually continuous.
Definition: avutil.h:196
int width
Video only.
Definition: avcodec.h:3525
AVOptions.
unsigned int sample_count
Definition: isom.h:115
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:70
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:696
int count
Definition: isom.h:52
int dts_shift
dts shift when ctts is negative
Definition: isom.h:133
#define AV_RB32
Definition: intreadwrite.h:130
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: avcodec.h:1254
int id
Format-specific stream ID.
Definition: avformat.h:712
enum AVStreamParseType need_parsing
Definition: avformat.h:879
#define b
Definition: input.c:52
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
AVInputFormat ff_mov_demuxer
Definition: mov.c:3844
unsigned int count
Definition: isom.h:92
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3091
int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb)
Definition: mov.c:674
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:812
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
int16_t nlvl_to
Definition: isom.h:62
#define AV_CH_LOW_FREQUENCY
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
char volume[28]
Definition: isom.h:60
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:104
static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:796
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, int codec_tag, int format, int64_t size)
Definition: mov.c:1775
int stsd_count
Definition: isom.h:145
static int mov_open_dref(AVFormatContext *s, AVIOContext **pb, char *src, MOVDref *ref)
Definition: mov.c:2520
static void finish(void)
Definition: movenc.c:338
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:302
uint8_t * data
Definition: avcodec.h:1346
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int flags
Definition: log.c:50
#define MOV_TRUN_SAMPLE_SIZE
Definition: isom.h:195
uint32_t tag
Definition: movenc.c:854
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:140
uint8_t * data
Definition: avcodec.h:1290
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:117
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:615
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:761
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size)
Definition: dv.c:342
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1094
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int current_sample
Definition: isom.h:122
#define MOV_TFHD_DEFAULT_SIZE
Definition: isom.h:187
unsigned track_id
Definition: isom.h:84
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int64_t time_offset
time offset of the first edit list entry
Definition: isom.h:121
static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2731
unsigned int keyframe_count
Definition: isom.h:118
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:99
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:1131
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1890
#define src
Definition: vp8dsp.c:254
static int mov_rewrite_dvd_sub_extradata(AVStream *st)
Definition: mov.c:1652
#define AVINDEX_KEYFRAME
Definition: avformat.h:666
int * extradata_size
Definition: isom.h:143
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb)
Definition: isom.c:451
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:168
static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc, int64_t size)
Definition: mov.c:1622
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2348
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
unsigned int ctts_count
Definition: isom.h:104
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1255
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:807
static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3143
static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int64_t size)
Definition: mov.c:2677
AVAudioServiceType
Definition: avcodec.h:692
int stsc_sample
Definition: isom.h:109
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2233
static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1195
int64_t timestamp
Definition: avformat.h:665
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVColorPrimaries color_primaries
Definition: avcodec.h:3546
g
Definition: yuv2rgb.c:546
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
int * keyframes
Definition: isom.h:119
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVFormatContext * fc
Definition: isom.h:152
static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
Definition: mov.c:898
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:295
static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2969
enum AVPacketSideDataType type
Definition: avcodec.h:1292
int ctts_sample
Definition: isom.h:113
int keyframe_absent
Definition: isom.h:117
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:374
#define FFMAX(a, b)
Definition: common.h:64
int16_t nlvl_from
Definition: isom.h:62
#define MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES
Definition: isom.h:207
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:669
#define fail()
Definition: checkasm.h:80
const AVCodecTag ff_codec_movsubtitle_tags[]
Definition: isom.c:323
#define MIN_DATA_ENTRY_BOX_SIZE
Definition: mov.c:477
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:1352
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition: pixdesc.c:1878
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
Definition: mov.c:1801
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
static float distance(float x, float y, int band)
static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2778
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:401
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:3248
static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1055
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:911
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:3163
int stsc_index
Definition: isom.h:108
static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
Definition: mov.c:3727
static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:67
int block_align
Audio only.
Definition: avcodec.h:3571
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2171
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
audio channel layout utility functions
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:105
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:689
char filename[1024]
input or output filename
Definition: avformat.h:1016
static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2910
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:86
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
#define FFMIN(a, b)
Definition: common.h:66
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:840
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:64
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
char * dir
Definition: isom.h:59
int id
Definition: isom.h:53
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:353
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define FLAGS
Definition: mov.c:3822
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2872
static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2939
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1219
char * path
Definition: isom.h:58
#define FFABS(a)
Definition: common.h:61
int time_scale
Definition: isom.h:120
#define AV_RL32
Definition: intreadwrite.h:146
uint64_t moof_offset
Definition: isom.h:75
MOVStts * ctts_data
Definition: isom.h:105
#define OFFSET(x)
Definition: mov.c:3821
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
#define MOV_TRUN_SAMPLE_DURATION
Definition: isom.h:194
AVDictionary * metadata
Definition: avformat.h:772
uint8_t * av_stream_new_side_data(AVStream *stream, enum AVPacketSideDataType type, int size)
Allocate new information from stream.
Definition: utils.c:3280
unsigned size
Definition: isom.h:87
enum AVColorRange color_range
Video only.
Definition: avcodec.h:3545
int64_t * chunk_offsets
Definition: isom.h:101
static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mov.c:3626
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:3441
unsigned int index
Definition: isom.h:93
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:1230
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size)
Read &#39;chan&#39; tag from the input stream.
Definition: mov_chan.c:546
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:694
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
if(ac->has_optimized_func)
int64_t duration
duration of the longest track
Definition: isom.h:154
int ff_mov_lang_to_iso639(unsigned code, char to[4])
Definition: isom.c:382
Stream structure.
Definition: avformat.h:705
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
Definition: mov.c:175
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
unsigned duration
Definition: isom.h:86
DVDemuxContext * dv_demux
Definition: isom.h:157
also ITU-R BT1361
Definition: pixfmt.h:317
NULL
Definition: eval.c:55
int * sample_sizes
Definition: isom.h:116
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1201
static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mov.c:3770
static const uint32_t mac_to_unicode[128]
Definition: mov.c:134
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:672
static int width
Definition: utils.c:156
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:478
#define av_bswap32
Definition: bswap.h:33
unsigned duration
Definition: isom.h:78
#define MP4DecConfigDescrTag
Definition: isom.h:180
MOVSbgp * rap_group
Definition: isom.h:139
version
Definition: ffv1enc.c:1091
const AVCodecTag ff_codec_movaudio_tags[]
Definition: isom.c:271
functionally identical to above
Definition: pixfmt.h:302
int duration
Definition: isom.h:47
static void mov_read_chapters(AVFormatContext *s)
Definition: mov.c:3372
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1416
av_default_item_name
Definition: dnxhdenc.c:55
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:701
unsigned trex_count
Definition: isom.h:162
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:193
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2668
int enable_drefs
Definition: isom.h:169
int64_t data_size
Definition: isom.h:136
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
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:68
uint32_t type
Definition: isom.h:57
int nb_index_entries
Definition: avformat.h:892
MOVStts * stts_data
Definition: isom.h:103
const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:34
int count
Definition: isom.h:46
int index
Definition: gxfenc.c:72
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:1995
rational number numerator/denominator
Definition: rational.h:43
MOVFragment fragment
current fragment in moof atom
Definition: isom.h:160
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
AVFieldOrder
Definition: avcodec.h:1393
int64_t track_end
used for dts generation in fragmented movie files
Definition: isom.h:137
Definition: isom.h:65
static const uint8_t ff_qt_default_palette_16[16 *3]
Definition: qtpalette.h:35
static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1527
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, char *dst, int dstlen)
Definition: mov.c:153
SMPTE ST 428-1.
Definition: pixfmt.h:334
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:188
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:405
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
#define MOV_TRUN_SAMPLE_FLAGS
Definition: isom.h:196
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
#define TAG_IS_AVCI(tag)
Definition: isom.h:214
#define MOV_TFHD_STSD_ID
Definition: isom.h:185
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2048
AVFormatContext * dv_fctx
Definition: isom.h:158
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:91
uint64_t implicit_offset
Definition: isom.h:76
static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2764
unsigned * stps_data
partial sync sample for mpeg-2 open gop
Definition: isom.h:111
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:364
int height
Definition: gxfenc.c:72
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:831
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, unsigned len, const char *key)
Definition: mov.c:87
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
full parsing and repack
Definition: avformat.h:657
Main libavformat public API header.
int32_t * display_matrix
Definition: isom.h:147
int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag)
Definition: isom.c:417
AVIOContext * pb
Definition: isom.h:97
static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
An strf atom is a BITMAPINFOHEADER struct.
Definition: mov.c:1300
unsigned int bytes_per_frame
Definition: isom.h:123
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
unsigned flags
Definition: isom.h:80
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:184
int64_t size
Definition: isom.h:67
int export_xmp
Definition: isom.h:168
int seek_individually
Definition: isom.h:165
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:109
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
static const MOVParseTableEntry mov_default_parse_table[]
Definition: mov.c:3183
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition: pixdesc.c:1884
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1170
static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:777
unsigned int stps_count
Definition: isom.h:110
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
unsigned int chunk_count
Definition: isom.h:100
static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1166
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:161
static int mov_codec_id(AVStream *st, uint32_t format)
Definition: mov.c:1385
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:759
int den
denominator
Definition: rational.h:45
unsigned bps
Definition: movenc.c:855
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:325
static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len)
Definition: mov.c:226
static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1328
uint32_t palette[256]
Definition: isom.h:134
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
unsigned stsd_id
Definition: isom.h:77
unsigned int index_entries_allocated_size
Definition: avformat.h:893
static int mov_stsc_index_valid(int index, int count)
Definition: mov.c:1997
static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, AVStream *st, MOVStreamContext *sc)
Definition: mov.c:1708
int eof_reached
true if eof reached
Definition: avio.h:132
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
as in Berlin toast format
Definition: avcodec.h:496
int len
unsigned int stts_count
Definition: isom.h:102
unsigned int sample_size
Definition: isom.h:114
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size)
Definition: riffdec.c:82
static uint8_t tmp[8]
Definition: des.c:38
uint32_t type
Definition: mov.c:61
void * priv_data
Format private data.
Definition: avformat.h:968
static const AVClass mov_class
Definition: mov.c:3837
int bits_per_coded_sample
Definition: avcodec.h:3514
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
#define MOV_TRUN_DATA_OFFSET
Definition: isom.h:192
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:166
int last_stsd_index
Definition: isom.h:144
int channels
Audio only.
Definition: avcodec.h:3560
#define AV_WL32(p, val)
Definition: intreadwrite.h:263
int time_scale
Definition: isom.h:153
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1264
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:301
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
int pseudo_stream_id
-1 means demux all ids
Definition: isom.h:126
ITU-R BT2020.
Definition: pixfmt.h:304
int ffindex
AVStream index.
Definition: isom.h:98
uint8_t ** extradata
extradata array (and size) for multiple stsd
Definition: isom.h:142
static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2916
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1950
AVCodecParameters * codecpar
Definition: avformat.h:831
unsigned size
Definition: isom.h:79
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:719
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3487
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2091
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:263
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
#define AV_WB16(p, val)
Definition: intreadwrite.h:218
#define MKTAG(a, b, c, d)
Definition: common.h:256
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:763
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
This structure stores compressed data.
Definition: avcodec.h:1323
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2280
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:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:790
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
#define av_unused
Definition: attributes.h:86
Definition: isom.h:56
static uint32_t yuv_to_rgba(uint32_t ycbcr)
Definition: mov.c:1636
#define MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC
Definition: isom.h:200
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: avcodec.h:1266