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