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)
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  return ff_mov_read_stsd_entries(c, pb, entries);
1611 }
1612 
1613 static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1614 {
1615  AVStream *st;
1616  MOVStreamContext *sc;
1617  unsigned int i, entries;
1618 
1619  if (c->fc->nb_streams < 1)
1620  return 0;
1621  st = c->fc->streams[c->fc->nb_streams-1];
1622  sc = st->priv_data;
1623 
1624  avio_r8(pb); /* version */
1625  avio_rb24(pb); /* flags */
1626 
1627  entries = avio_rb32(pb);
1628 
1629  av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
1630 
1631  if (!entries)
1632  return 0;
1633  if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
1634  return AVERROR_INVALIDDATA;
1635  sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
1636  if (!sc->stsc_data)
1637  return AVERROR(ENOMEM);
1638 
1639  for (i = 0; i < entries && !pb->eof_reached; i++) {
1640  sc->stsc_data[i].first = avio_rb32(pb);
1641  sc->stsc_data[i].count = avio_rb32(pb);
1642  sc->stsc_data[i].id = avio_rb32(pb);
1643  }
1644 
1645  sc->stsc_count = i;
1646 
1647  if (pb->eof_reached)
1648  return AVERROR_EOF;
1649 
1650  return 0;
1651 }
1652 
1653 static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1654 {
1655  AVStream *st;
1656  MOVStreamContext *sc;
1657  unsigned i, entries;
1658 
1659  if (c->fc->nb_streams < 1)
1660  return 0;
1661  st = c->fc->streams[c->fc->nb_streams-1];
1662  sc = st->priv_data;
1663 
1664  avio_rb32(pb); // version + flags
1665 
1666  entries = avio_rb32(pb);
1667  if (entries >= UINT_MAX / sizeof(*sc->stps_data))
1668  return AVERROR_INVALIDDATA;
1669  sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
1670  if (!sc->stps_data)
1671  return AVERROR(ENOMEM);
1672 
1673  for (i = 0; i < entries && !pb->eof_reached; i++) {
1674  sc->stps_data[i] = avio_rb32(pb);
1675  //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
1676  }
1677 
1678  sc->stps_count = i;
1679 
1680  if (pb->eof_reached)
1681  return AVERROR_EOF;
1682 
1683  return 0;
1684 }
1685 
1686 static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1687 {
1688  AVStream *st;
1689  MOVStreamContext *sc;
1690  unsigned int i, entries;
1691 
1692  if (c->fc->nb_streams < 1)
1693  return 0;
1694  st = c->fc->streams[c->fc->nb_streams-1];
1695  sc = st->priv_data;
1696 
1697  avio_r8(pb); /* version */
1698  avio_rb24(pb); /* flags */
1699 
1700  entries = avio_rb32(pb);
1701 
1702  av_dlog(c->fc, "keyframe_count = %d\n", entries);
1703 
1704  if (!entries)
1705  {
1706  sc->keyframe_absent = 1;
1707  return 0;
1708  }
1709  if (entries >= UINT_MAX / sizeof(int))
1710  return AVERROR_INVALIDDATA;
1711  av_freep(&sc->keyframes);
1712  sc->keyframes = av_malloc(entries * sizeof(int));
1713  if (!sc->keyframes)
1714  return AVERROR(ENOMEM);
1715 
1716  for (i = 0; i < entries && !pb->eof_reached; i++) {
1717  sc->keyframes[i] = avio_rb32(pb);
1718  //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
1719  }
1720 
1721  sc->keyframe_count = i;
1722 
1723  if (pb->eof_reached)
1724  return AVERROR_EOF;
1725 
1726  return 0;
1727 }
1728 
1729 static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1730 {
1731  AVStream *st;
1732  MOVStreamContext *sc;
1733  unsigned int i, entries, sample_size, field_size, num_bytes;
1734  GetBitContext gb;
1735  unsigned char* buf;
1736 
1737  if (c->fc->nb_streams < 1)
1738  return 0;
1739  st = c->fc->streams[c->fc->nb_streams-1];
1740  sc = st->priv_data;
1741 
1742  avio_r8(pb); /* version */
1743  avio_rb24(pb); /* flags */
1744 
1745  if (atom.type == MKTAG('s','t','s','z')) {
1746  sample_size = avio_rb32(pb);
1747  if (!sc->sample_size) /* do not overwrite value computed in stsd */
1748  sc->sample_size = sample_size;
1749  field_size = 32;
1750  } else {
1751  sample_size = 0;
1752  avio_rb24(pb); /* reserved */
1753  field_size = avio_r8(pb);
1754  }
1755  entries = avio_rb32(pb);
1756 
1757  av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
1758 
1759  sc->sample_count = entries;
1760  if (sample_size)
1761  return 0;
1762 
1763  if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
1764  av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
1765  return AVERROR_INVALIDDATA;
1766  }
1767 
1768  if (!entries)
1769  return 0;
1770  if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
1771  return AVERROR_INVALIDDATA;
1772  sc->sample_sizes = av_malloc(entries * sizeof(int));
1773  if (!sc->sample_sizes)
1774  return AVERROR(ENOMEM);
1775 
1776  num_bytes = (entries*field_size+4)>>3;
1777 
1778  buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
1779  if (!buf) {
1780  av_freep(&sc->sample_sizes);
1781  return AVERROR(ENOMEM);
1782  }
1783 
1784  if (avio_read(pb, buf, num_bytes) < num_bytes) {
1785  av_freep(&sc->sample_sizes);
1786  av_free(buf);
1787  return AVERROR_INVALIDDATA;
1788  }
1789 
1790  init_get_bits(&gb, buf, 8*num_bytes);
1791 
1792  for (i = 0; i < entries && !pb->eof_reached; i++) {
1793  sc->sample_sizes[i] = get_bits_long(&gb, field_size);
1794  sc->data_size += sc->sample_sizes[i];
1795  }
1796 
1797  sc->sample_count = i;
1798 
1799  av_free(buf);
1800 
1801  if (pb->eof_reached)
1802  return AVERROR_EOF;
1803 
1804  return 0;
1805 }
1806 
1807 static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1808 {
1809  AVStream *st;
1810  MOVStreamContext *sc;
1811  unsigned int i, entries;
1812  int64_t duration=0;
1813  int64_t total_sample_count=0;
1814 
1815  if (c->fc->nb_streams < 1)
1816  return 0;
1817  st = c->fc->streams[c->fc->nb_streams-1];
1818  sc = st->priv_data;
1819 
1820  avio_r8(pb); /* version */
1821  avio_rb24(pb); /* flags */
1822  entries = avio_rb32(pb);
1823 
1824  av_dlog(c->fc, "track[%i].stts.entries = %i\n",
1825  c->fc->nb_streams-1, entries);
1826 
1827  if (!entries)
1828  return 0;
1829  if (entries >= UINT_MAX / sizeof(*sc->stts_data))
1830  return AVERROR(EINVAL);
1831 
1832  av_free(sc->stts_data);
1833  sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
1834  if (!sc->stts_data)
1835  return AVERROR(ENOMEM);
1836 
1837  for (i = 0; i < entries && !pb->eof_reached; i++) {
1838  int sample_duration;
1839  int sample_count;
1840 
1841  sample_count=avio_rb32(pb);
1842  sample_duration = avio_rb32(pb);
1843  if (sample_count < 0) {
1844  av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count);
1845  return AVERROR_INVALIDDATA;
1846  }
1847  sc->stts_data[i].count= sample_count;
1848  sc->stts_data[i].duration= sample_duration;
1849 
1850  av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
1851  sample_count, sample_duration);
1852 
1853  duration+=(int64_t)sample_duration*sample_count;
1854  total_sample_count+=sample_count;
1855  }
1856 
1857  sc->stts_count = i;
1858 
1859  if (pb->eof_reached)
1860  return AVERROR_EOF;
1861 
1862  st->nb_frames= total_sample_count;
1863  if (duration)
1864  st->duration= duration;
1865  sc->track_end = duration;
1866  return 0;
1867 }
1868 
1869 static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1870 {
1871  AVStream *st;
1872  MOVStreamContext *sc;
1873  unsigned int i, entries;
1874 
1875  if (c->fc->nb_streams < 1)
1876  return 0;
1877  st = c->fc->streams[c->fc->nb_streams-1];
1878  sc = st->priv_data;
1879 
1880  avio_r8(pb); /* version */
1881  avio_rb24(pb); /* flags */
1882  entries = avio_rb32(pb);
1883 
1884  av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
1885 
1886  av_freep(&sc->ctts_data);
1887 
1888  if (!entries)
1889  return 0;
1890  if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
1891  return AVERROR_INVALIDDATA;
1892  sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data));
1893  if (!sc->ctts_data)
1894  return AVERROR(ENOMEM);
1895 
1896  for (i = 0; i < entries && !pb->eof_reached; i++) {
1897  int count =avio_rb32(pb);
1898  int duration =avio_rb32(pb);
1899 
1900  sc->ctts_data[i].count = count;
1901  sc->ctts_data[i].duration= duration;
1902  if (duration < 0)
1903  sc->dts_shift = FFMAX(sc->dts_shift, -duration);
1904  }
1905 
1906  sc->ctts_count = i;
1907 
1908  if (pb->eof_reached)
1909  return AVERROR_EOF;
1910 
1911  av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
1912 
1913  return 0;
1914 }
1915 
1916 static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
1917 {
1918  AVStream *st;
1919  MOVStreamContext *sc;
1920  unsigned int i, entries;
1921  uint8_t version;
1922  uint32_t grouping_type;
1923 
1924  if (c->fc->nb_streams < 1)
1925  return 0;
1926  st = c->fc->streams[c->fc->nb_streams-1];
1927  sc = st->priv_data;
1928 
1929  version = avio_r8(pb); /* version */
1930  avio_rb24(pb); /* flags */
1931  grouping_type = avio_rl32(pb);
1932  if (grouping_type != MKTAG( 'r','a','p',' '))
1933  return 0; /* only support 'rap ' grouping */
1934  if (version == 1)
1935  avio_rb32(pb); /* grouping_type_parameter */
1936 
1937  entries = avio_rb32(pb);
1938  if (!entries)
1939  return 0;
1940  if (entries >= UINT_MAX / sizeof(*sc->rap_group))
1941  return AVERROR_INVALIDDATA;
1942  sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
1943  if (!sc->rap_group)
1944  return AVERROR(ENOMEM);
1945 
1946  for (i = 0; i < entries && !pb->eof_reached; i++) {
1947  sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
1948  sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
1949  }
1950 
1951  sc->rap_group_count = i;
1952 
1953  return pb->eof_reached ? AVERROR_EOF : 0;
1954 }
1955 
1956 static void mov_build_index(MOVContext *mov, AVStream *st)
1957 {
1958  MOVStreamContext *sc = st->priv_data;
1959  int64_t current_offset;
1960  int64_t current_dts = 0;
1961  unsigned int stts_index = 0;
1962  unsigned int stsc_index = 0;
1963  unsigned int stss_index = 0;
1964  unsigned int stps_index = 0;
1965  unsigned int i, j;
1966  uint64_t stream_size = 0;
1967 
1968  /* adjust first dts according to edit list */
1969  if (sc->time_offset && mov->time_scale > 0) {
1970  if (sc->time_offset < 0)
1971  sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale);
1972  current_dts = -sc->time_offset;
1973  if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration &&
1974  sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
1975  /* more than 16 frames delay, dts are likely wrong
1976  this happens with files created by iMovie */
1977  sc->wrong_dts = 1;
1978  st->codec->has_b_frames = 1;
1979  }
1980  }
1981 
1982  /* only use old uncompressed audio chunk demuxing when stts specifies it */
1983  if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1984  sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
1985  unsigned int current_sample = 0;
1986  unsigned int stts_sample = 0;
1987  unsigned int sample_size;
1988  unsigned int distance = 0;
1989  unsigned int rap_group_index = 0;
1990  unsigned int rap_group_sample = 0;
1991  int rap_group_present = sc->rap_group_count && sc->rap_group;
1992  int key_off = (sc->keyframes && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
1993 
1994  current_dts -= sc->dts_shift;
1995 
1996  if (!sc->sample_count)
1997  return;
1998  if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
1999  return;
2001  st->nb_index_entries + sc->sample_count,
2002  sizeof(*st->index_entries)) < 0) {
2003  st->nb_index_entries = 0;
2004  return;
2005  }
2007 
2008  for (i = 0; i < sc->chunk_count; i++) {
2009  current_offset = sc->chunk_offsets[i];
2010  while (stsc_index + 1 < sc->stsc_count &&
2011  i + 1 == sc->stsc_data[stsc_index + 1].first)
2012  stsc_index++;
2013  for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
2014  int keyframe = 0;
2015  if (current_sample >= sc->sample_count) {
2016  av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
2017  return;
2018  }
2019 
2020  if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
2021  keyframe = 1;
2022  if (stss_index + 1 < sc->keyframe_count)
2023  stss_index++;
2024  } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
2025  keyframe = 1;
2026  if (stps_index + 1 < sc->stps_count)
2027  stps_index++;
2028  }
2029  if (rap_group_present && rap_group_index < sc->rap_group_count) {
2030  if (sc->rap_group[rap_group_index].index > 0)
2031  keyframe = 1;
2032  if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
2033  rap_group_sample = 0;
2034  rap_group_index++;
2035  }
2036  }
2037  if (keyframe)
2038  distance = 0;
2039  sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
2040  if (sc->pseudo_stream_id == -1 ||
2041  sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
2042  AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
2043  e->pos = current_offset;
2044  e->timestamp = current_dts;
2045  e->size = sample_size;
2046  e->min_distance = distance;
2047  e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
2048  av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2049  "size %d, distance %d, keyframe %d\n", st->index, current_sample,
2050  current_offset, current_dts, sample_size, distance, keyframe);
2051  }
2052 
2053  current_offset += sample_size;
2054  stream_size += sample_size;
2055  current_dts += sc->stts_data[stts_index].duration;
2056  distance++;
2057  stts_sample++;
2058  current_sample++;
2059  if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
2060  stts_sample = 0;
2061  stts_index++;
2062  }
2063  }
2064  }
2065  if (st->duration > 0)
2066  st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
2067  } else {
2068  unsigned chunk_samples, total = 0;
2069 
2070  // compute total chunk count
2071  for (i = 0; i < sc->stsc_count; i++) {
2072  unsigned count, chunk_count;
2073 
2074  chunk_samples = sc->stsc_data[i].count;
2075  if (i != sc->stsc_count - 1 &&
2076  sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
2077  av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
2078  return;
2079  }
2080 
2081  if (sc->samples_per_frame >= 160) { // gsm
2082  count = chunk_samples / sc->samples_per_frame;
2083  } else if (sc->samples_per_frame > 1) {
2084  unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
2085  count = (chunk_samples+samples-1) / samples;
2086  } else {
2087  count = (chunk_samples+1023) / 1024;
2088  }
2089 
2090  if (i < sc->stsc_count - 1)
2091  chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
2092  else
2093  chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
2094  total += chunk_count * count;
2095  }
2096 
2097  av_dlog(mov->fc, "chunk count %d\n", total);
2098  if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
2099  return;
2101  st->nb_index_entries + total,
2102  sizeof(*st->index_entries)) < 0) {
2103  st->nb_index_entries = 0;
2104  return;
2105  }
2106  st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
2107 
2108  // populate index
2109  for (i = 0; i < sc->chunk_count; i++) {
2110  current_offset = sc->chunk_offsets[i];
2111  if (stsc_index + 1 < sc->stsc_count &&
2112  i + 1 == sc->stsc_data[stsc_index + 1].first)
2113  stsc_index++;
2114  chunk_samples = sc->stsc_data[stsc_index].count;
2115 
2116  while (chunk_samples > 0) {
2117  AVIndexEntry *e;
2118  unsigned size, samples;
2119 
2120  if (sc->samples_per_frame >= 160) { // gsm
2121  samples = sc->samples_per_frame;
2122  size = sc->bytes_per_frame;
2123  } else {
2124  if (sc->samples_per_frame > 1) {
2125  samples = FFMIN((1024 / sc->samples_per_frame)*
2126  sc->samples_per_frame, chunk_samples);
2127  size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
2128  } else {
2129  samples = FFMIN(1024, chunk_samples);
2130  size = samples * sc->sample_size;
2131  }
2132  }
2133 
2134  if (st->nb_index_entries >= total) {
2135  av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
2136  return;
2137  }
2138  e = &st->index_entries[st->nb_index_entries++];
2139  e->pos = current_offset;
2140  e->timestamp = current_dts;
2141  e->size = size;
2142  e->min_distance = 0;
2143  e->flags = AVINDEX_KEYFRAME;
2144  av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
2145  "size %d, duration %d\n", st->index, i, current_offset, current_dts,
2146  size, samples);
2147 
2148  current_offset += size;
2149  current_dts += samples;
2150  chunk_samples -= samples;
2151  }
2152  }
2153  }
2154 }
2155 
2156 static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
2158 {
2159  /* try relative path, we do not try the absolute because it can leak information about our
2160  system to an attacker */
2161  if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
2162  char filename[1024];
2163  char *src_path;
2164  int i, l;
2165 
2166  /* find a source dir */
2167  src_path = strrchr(src, '/');
2168  if (src_path)
2169  src_path++;
2170  else
2171  src_path = src;
2172 
2173  /* find a next level down to target */
2174  for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
2175  if (ref->path[l] == '/') {
2176  if (i == ref->nlvl_to - 1)
2177  break;
2178  else
2179  i++;
2180  }
2181 
2182  /* compose filename if next level down to target was found */
2183  if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
2184  memcpy(filename, src, src_path - src);
2185  filename[src_path - src] = 0;
2186 
2187  for (i = 1; i < ref->nlvl_from; i++)
2188  av_strlcat(filename, "../", 1024);
2189 
2190  av_strlcat(filename, ref->path + l + 1, 1024);
2191 
2192  if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
2193  return 0;
2194  }
2195  }
2196 
2197  return AVERROR(ENOENT);
2198 }
2199 
2200 static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2201 {
2202  AVStream *st;
2203  MOVStreamContext *sc;
2204  int ret;
2205 
2206  st = avformat_new_stream(c->fc, NULL);
2207  if (!st) return AVERROR(ENOMEM);
2208  st->id = c->fc->nb_streams;
2209  sc = av_mallocz(sizeof(MOVStreamContext));
2210  if (!sc) return AVERROR(ENOMEM);
2211 
2212  st->priv_data = sc;
2214  sc->ffindex = st->index;
2215 
2216  if ((ret = mov_read_default(c, pb, atom)) < 0)
2217  return ret;
2218 
2219  /* sanity checks */
2220  if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
2221  (!sc->sample_size && !sc->sample_count))) {
2222  av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
2223  st->index);
2224  return 0;
2225  }
2226 
2227  if (sc->time_scale <= 0) {
2228  av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
2229  sc->time_scale = c->time_scale;
2230  if (sc->time_scale <= 0)
2231  sc->time_scale = 1;
2232  }
2233 
2234  avpriv_set_pts_info(st, 64, 1, sc->time_scale);
2235 
2236  mov_build_index(c, st);
2237 
2238  if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
2239  MOVDref *dref = &sc->drefs[sc->dref_id - 1];
2240  if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0)
2241  av_log(c->fc, AV_LOG_ERROR,
2242  "stream %d, error opening alias: path='%s', dir='%s', "
2243  "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
2244  st->index, dref->path, dref->dir, dref->filename,
2245  dref->volume, dref->nlvl_from, dref->nlvl_to);
2246  } else
2247  sc->pb = c->fc->pb;
2248 
2249  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2250  if (!st->sample_aspect_ratio.num &&
2251  (st->codec->width != sc->width || st->codec->height != sc->height)) {
2252  st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
2253  ((double)st->codec->width * sc->height), INT_MAX);
2254  }
2255  }
2256 
2257  // done for ai5q, ai52, ai55, ai1q, ai12 and ai15.
2258  if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 &&
2259  TAG_IS_AVCI(st->codec->codec_tag)) {
2260  ret = ff_generate_avci_extradata(st);
2261  if (ret < 0)
2262  return ret;
2263  }
2264 
2265  switch (st->codec->codec_id) {
2266 #if CONFIG_H261_DECODER
2267  case AV_CODEC_ID_H261:
2268 #endif
2269 #if CONFIG_H263_DECODER
2270  case AV_CODEC_ID_H263:
2271 #endif
2272 #if CONFIG_MPEG4_DECODER
2273  case AV_CODEC_ID_MPEG4:
2274 #endif
2275  st->codec->width = 0; /* let decoder init width/height */
2276  st->codec->height= 0;
2277  break;
2278  case AV_CODEC_ID_MP3:
2280  break;
2281  }
2282 
2283  /* Do not need those anymore. */
2284  av_freep(&sc->chunk_offsets);
2285  av_freep(&sc->stsc_data);
2286  av_freep(&sc->sample_sizes);
2287  av_freep(&sc->keyframes);
2288  av_freep(&sc->stts_data);
2289  av_freep(&sc->stps_data);
2290  av_freep(&sc->rap_group);
2291 
2292  return 0;
2293 }
2294 
2295 static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2296 {
2297  int ret;
2298  c->itunes_metadata = 1;
2299  ret = mov_read_default(c, pb, atom);
2300  c->itunes_metadata = 0;
2301  return ret;
2302 }
2303 
2304 static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int64_t size)
2305 {
2306  int64_t end = avio_tell(pb) + size;
2307  uint8_t *key = NULL, *val = NULL;
2308  int i;
2309 
2310  for (i = 0; i < 2; i++) {
2311  uint8_t **p;
2312  uint32_t len, tag;
2313 
2314  if (end - avio_tell(pb) <= 12)
2315  break;
2316 
2317  len = avio_rb32(pb);
2318  tag = avio_rl32(pb);
2319  avio_skip(pb, 4); // flags
2320 
2321  if (len < 12 || len - 12 > end - avio_tell(pb))
2322  break;
2323  len -= 12;
2324 
2325  if (tag == MKTAG('n', 'a', 'm', 'e'))
2326  p = &key;
2327  else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) {
2328  avio_skip(pb, 4);
2329  len -= 4;
2330  p = &val;
2331  } else
2332  break;
2333 
2334  *p = av_malloc(len + 1);
2335  if (!*p)
2336  break;
2337  avio_read(pb, *p, len);
2338  (*p)[len] = 0;
2339  }
2340 
2341  if (key && val) {
2342  av_dict_set(&c->fc->metadata, key, val,
2344  key = val = NULL;
2345  }
2346 
2347  avio_seek(pb, end, SEEK_SET);
2348  av_freep(&key);
2349  av_freep(&val);
2350  return 0;
2351 }
2352 
2354 {
2355  int64_t end = avio_tell(pb) + atom.size;
2356  uint32_t tag, len;
2357 
2358  if (atom.size < 8)
2359  goto fail;
2360 
2361  len = avio_rb32(pb);
2362  tag = avio_rl32(pb);
2363 
2364  if (len > atom.size)
2365  goto fail;
2366 
2367  if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) {
2368  uint8_t domain[128];
2369  int domain_len;
2370 
2371  avio_skip(pb, 4); // flags
2372  len -= 12;
2373 
2374  domain_len = avio_get_str(pb, len, domain, sizeof(domain));
2375  avio_skip(pb, len - domain_len);
2376  if (!strcmp(domain, "org.hydrogenaudio.replaygain"))
2377  return mov_read_replaygain(c, pb, end - avio_tell(pb));
2378  }
2379 
2380 fail:
2381  av_log(c->fc, AV_LOG_VERBOSE,
2382  "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size);
2383  return 0;
2384 }
2385 
2386 static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2387 {
2388  while (atom.size > 8) {
2389  uint32_t tag = avio_rl32(pb);
2390  atom.size -= 4;
2391  if (tag == MKTAG('h','d','l','r')) {
2392  avio_seek(pb, -8, SEEK_CUR);
2393  atom.size += 8;
2394  return mov_read_default(c, pb, atom);
2395  }
2396  }
2397  return 0;
2398 }
2399 
2400 static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2401 {
2402  int i;
2403  int width;
2404  int height;
2405  int64_t disp_transform[2];
2406  int display_matrix[3][3];
2407  AVStream *st;
2408  MOVStreamContext *sc;
2409  int version;
2410  int flags;
2411 
2412  if (c->fc->nb_streams < 1)
2413  return 0;
2414  st = c->fc->streams[c->fc->nb_streams-1];
2415  sc = st->priv_data;
2416 
2417  version = avio_r8(pb);
2418  flags = avio_rb24(pb);
2420 
2421  if (version == 1) {
2422  avio_rb64(pb);
2423  avio_rb64(pb);
2424  } else {
2425  avio_rb32(pb); /* creation time */
2426  avio_rb32(pb); /* modification time */
2427  }
2428  st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
2429  avio_rb32(pb); /* reserved */
2430 
2431  /* highlevel (considering edits) duration in movie timebase */
2432  (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
2433  avio_rb32(pb); /* reserved */
2434  avio_rb32(pb); /* reserved */
2435 
2436  avio_rb16(pb); /* layer */
2437  avio_rb16(pb); /* alternate group */
2438  avio_rb16(pb); /* volume */
2439  avio_rb16(pb); /* reserved */
2440 
2441  //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
2442  // they're kept in fixed point format through all calculations
2443  // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX
2444  // side data, but the scale factor is not needed to calculate aspect ratio
2445  for (i = 0; i < 3; i++) {
2446  display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
2447  display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
2448  display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
2449  }
2450 
2451  width = avio_rb32(pb); // 16.16 fixed point track width
2452  height = avio_rb32(pb); // 16.16 fixed point track height
2453  sc->width = width >> 16;
2454  sc->height = height >> 16;
2455 
2456  // save the matrix when it is not the default identity
2457  if (display_matrix[0][0] != (1 << 16) ||
2458  display_matrix[1][1] != (1 << 16) ||
2459  display_matrix[2][2] != (1 << 30) ||
2460  display_matrix[0][1] || display_matrix[0][2] ||
2461  display_matrix[1][0] || display_matrix[1][2] ||
2462  display_matrix[2][0] || display_matrix[2][1]) {
2463  int i, j;
2464 
2465  av_freep(&sc->display_matrix);
2466  sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
2467  if (!sc->display_matrix)
2468  return AVERROR(ENOMEM);
2469 
2470  for (i = 0; i < 3; i++)
2471  for (j = 0; j < 3; j++)
2472  sc->display_matrix[i * 3 + j] = display_matrix[j][i];
2473  }
2474 
2475  // transform the display width/height according to the matrix
2476  // skip this if the display matrix is the default identity matrix
2477  // or if it is rotating the picture, ex iPhone 3GS
2478  // to keep the same scale, use [width height 1<<16]
2479  if (width && height &&
2480  ((display_matrix[0][0] != 65536 ||
2481  display_matrix[1][1] != 65536) &&
2482  !display_matrix[0][1] &&
2483  !display_matrix[1][0] &&
2484  !display_matrix[2][0] && !display_matrix[2][1])) {
2485  for (i = 0; i < 2; i++)
2486  disp_transform[i] =
2487  (int64_t) width * display_matrix[0][i] +
2488  (int64_t) height * display_matrix[1][i] +
2489  ((int64_t) display_matrix[2][i] << 16);
2490 
2491  //sample aspect ratio is new width/height divided by old width/height
2493  ((double) disp_transform[0] * height) /
2494  ((double) disp_transform[1] * width), INT_MAX);
2495  }
2496  return 0;
2497 }
2498 
2499 static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2500 {
2501  MOVFragment *frag = &c->fragment;
2502  MOVTrackExt *trex = NULL;
2503  int flags, track_id, i;
2504 
2505  avio_r8(pb); /* version */
2506  flags = avio_rb24(pb);
2507 
2508  track_id = avio_rb32(pb);
2509  if (!track_id)
2510  return AVERROR_INVALIDDATA;
2511  frag->track_id = track_id;
2512  for (i = 0; i < c->trex_count; i++)
2513  if (c->trex_data[i].track_id == frag->track_id) {
2514  trex = &c->trex_data[i];
2515  break;
2516  }
2517  if (!trex) {
2518  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
2519  return AVERROR_INVALIDDATA;
2520  }
2521 
2524  frag->moof_offset : frag->implicit_offset;
2525  frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
2526 
2527  frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
2528  avio_rb32(pb) : trex->duration;
2529  frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
2530  avio_rb32(pb) : trex->size;
2531  frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
2532  avio_rb32(pb) : trex->flags;
2533  av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
2534  return 0;
2535 }
2536 
2537 static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2538 {
2539  c->chapter_track = avio_rb32(pb);
2540  return 0;
2541 }
2542 
2543 static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2544 {
2545  MOVTrackExt *trex;
2546  int err;
2547 
2548  if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
2549  return AVERROR_INVALIDDATA;
2550  if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1,
2551  sizeof(*c->trex_data))) < 0) {
2552  c->trex_count = 0;
2553  return err;
2554  }
2555  trex = &c->trex_data[c->trex_count++];
2556  avio_r8(pb); /* version */
2557  avio_rb24(pb); /* flags */
2558  trex->track_id = avio_rb32(pb);
2559  trex->stsd_id = avio_rb32(pb);
2560  trex->duration = avio_rb32(pb);
2561  trex->size = avio_rb32(pb);
2562  trex->flags = avio_rb32(pb);
2563  return 0;
2564 }
2565 
2566 static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2567 {
2568  MOVFragment *frag = &c->fragment;
2569  AVStream *st = NULL;
2570  MOVStreamContext *sc;
2571  MOVStts *ctts_data;
2572  uint64_t offset;
2573  int64_t dts;
2574  int data_offset = 0;
2575  unsigned entries, first_sample_flags = frag->flags;
2576  int flags, distance, i, found_keyframe = 0, err;
2577 
2578  for (i = 0; i < c->fc->nb_streams; i++) {
2579  if (c->fc->streams[i]->id == frag->track_id) {
2580  st = c->fc->streams[i];
2581  break;
2582  }
2583  }
2584  if (!st) {
2585  av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
2586  return AVERROR_INVALIDDATA;
2587  }
2588  sc = st->priv_data;
2589  if (sc->pseudo_stream_id+1 != frag->stsd_id)
2590  return 0;
2591  avio_r8(pb); /* version */
2592  flags = avio_rb24(pb);
2593  entries = avio_rb32(pb);
2594  av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
2595 
2596  /* Always assume the presence of composition time offsets.
2597  * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
2598  * 1) in the initial movie, there are no samples.
2599  * 2) in the first movie fragment, there is only one sample without composition time offset.
2600  * 3) in the subsequent movie fragments, there are samples with composition time offset. */
2601  if (!sc->ctts_count && sc->sample_count)
2602  {
2603  /* Complement ctts table if moov atom doesn't have ctts atom. */
2604  ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data));
2605  if (!ctts_data)
2606  return AVERROR(ENOMEM);
2607  sc->ctts_data = ctts_data;
2608  sc->ctts_data[sc->ctts_count].count = sc->sample_count;
2609  sc->ctts_data[sc->ctts_count].duration = 0;
2610  sc->ctts_count++;
2611  }
2612  if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
2613  return AVERROR_INVALIDDATA;
2614  if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count,
2615  sizeof(*sc->ctts_data))) < 0) {
2616  sc->ctts_count = 0;
2617  return err;
2618  }
2619  if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
2620  if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
2621  dts = sc->track_end - sc->time_offset;
2622  offset = frag->base_data_offset + data_offset;
2623  distance = 0;
2624  av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
2625  for (i = 0; i < entries && !pb->eof_reached; i++) {
2626  unsigned sample_size = frag->size;
2627  int sample_flags = i ? frag->flags : first_sample_flags;
2628  unsigned sample_duration = frag->duration;
2629  int keyframe = 0;
2630 
2631  if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
2632  if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
2633  if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
2634  sc->ctts_data[sc->ctts_count].count = 1;
2635  sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
2636  avio_rb32(pb) : 0;
2637  sc->ctts_count++;
2638  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2639  keyframe = 1;
2640  else if (!found_keyframe)
2641  keyframe = found_keyframe =
2642  !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
2644  if (keyframe)
2645  distance = 0;
2646  av_add_index_entry(st, offset, dts, sample_size, distance,
2647  keyframe ? AVINDEX_KEYFRAME : 0);
2648  av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
2649  "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
2650  offset, dts, sample_size, distance, keyframe);
2651  distance++;
2652  dts += sample_duration;
2653  offset += sample_size;
2654  sc->data_size += sample_size;
2655  }
2656 
2657  if (pb->eof_reached)
2658  return AVERROR_EOF;
2659 
2660  frag->implicit_offset = offset;
2661  st->duration = sc->track_end = dts + sc->time_offset;
2662  return 0;
2663 }
2664 
2665 /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
2666 /* like the files created with Adobe Premiere 5.0, for samples see */
2667 /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
2668 static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2669 {
2670  int err;
2671 
2672  if (atom.size < 8)
2673  return 0; /* continue */
2674  if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
2675  avio_skip(pb, atom.size - 4);
2676  return 0;
2677  }
2678  atom.type = avio_rl32(pb);
2679  atom.size -= 8;
2680  if (atom.type != MKTAG('m','d','a','t')) {
2681  avio_skip(pb, atom.size);
2682  return 0;
2683  }
2684  err = mov_read_mdat(c, pb, atom);
2685  return err;
2686 }
2687 
2688 static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2689 {
2690 #if CONFIG_ZLIB
2691  AVIOContext ctx;
2692  uint8_t *cmov_data;
2693  uint8_t *moov_data; /* uncompressed data */
2694  long cmov_len, moov_len;
2695  int ret = -1;
2696 
2697  avio_rb32(pb); /* dcom atom */
2698  if (avio_rl32(pb) != MKTAG('d','c','o','m'))
2699  return AVERROR_INVALIDDATA;
2700  if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
2701  av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
2702  return AVERROR_INVALIDDATA;
2703  }
2704  avio_rb32(pb); /* cmvd atom */
2705  if (avio_rl32(pb) != MKTAG('c','m','v','d'))
2706  return AVERROR_INVALIDDATA;
2707  moov_len = avio_rb32(pb); /* uncompressed size */
2708  cmov_len = atom.size - 6 * 4;
2709 
2710  cmov_data = av_malloc(cmov_len);
2711  if (!cmov_data)
2712  return AVERROR(ENOMEM);
2713  moov_data = av_malloc(moov_len);
2714  if (!moov_data) {
2715  av_free(cmov_data);
2716  return AVERROR(ENOMEM);
2717  }
2718  avio_read(pb, cmov_data, cmov_len);
2719  if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
2720  goto free_and_return;
2721  if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
2722  goto free_and_return;
2723  atom.type = MKTAG('m','o','o','v');
2724  atom.size = moov_len;
2725  ret = mov_read_default(c, &ctx, atom);
2726 free_and_return:
2727  av_free(moov_data);
2728  av_free(cmov_data);
2729  return ret;
2730 #else
2731  av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
2732  return AVERROR(ENOSYS);
2733 #endif
2734 }
2735 
2736 /* edit list atom */
2737 static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
2738 {
2739  MOVStreamContext *sc;
2740  int i, edit_count, version;
2741 
2742  if (c->fc->nb_streams < 1)
2743  return 0;
2744  sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
2745 
2746  version = avio_r8(pb); /* version */
2747  avio_rb24(pb); /* flags */
2748  edit_count = avio_rb32(pb); /* entries */
2749 
2750  if ((uint64_t)edit_count*12+8 > atom.size)
2751  return AVERROR_INVALIDDATA;
2752 
2753  for (i=0; i<edit_count; i++){
2754  int64_t time;
2755  int64_t duration;
2756  if (version == 1) {
2757  duration = avio_rb64(pb);
2758  time = avio_rb64(pb);
2759  } else {
2760  duration = avio_rb32(pb); /* segment duration */
2761  time = (int32_t)avio_rb32(pb); /* media time */
2762  }
2763  avio_rb32(pb); /* Media rate */
2764  if (i == 0 && time >= -1) {
2765  sc->time_offset = time != -1 ? time : -duration;
2766  }
2767  }
2768 
2769  if (edit_count > 1)
2770  av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
2771  "a/v desync might occur, patch welcome\n");
2772 
2773  av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
2774  return 0;
2775 }
2776 
2778 { MKTAG('a','v','s','s'), mov_read_extradata },
2779 { MKTAG('c','h','p','l'), mov_read_chpl },
2780 { MKTAG('c','o','6','4'), mov_read_stco },
2781 { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
2782 { MKTAG('d','i','n','f'), mov_read_default },
2783 { MKTAG('d','r','e','f'), mov_read_dref },
2784 { MKTAG('e','d','t','s'), mov_read_default },
2785 { MKTAG('e','l','s','t'), mov_read_elst },
2786 { MKTAG('e','n','d','a'), mov_read_enda },
2787 { MKTAG('f','i','e','l'), mov_read_fiel },
2788 { MKTAG('f','t','y','p'), mov_read_ftyp },
2789 { MKTAG('g','l','b','l'), mov_read_glbl },
2790 { MKTAG('h','d','l','r'), mov_read_hdlr },
2791 { MKTAG('i','l','s','t'), mov_read_ilst },
2792 { MKTAG('j','p','2','h'), mov_read_extradata },
2793 { MKTAG('m','d','a','t'), mov_read_mdat },
2794 { MKTAG('m','d','h','d'), mov_read_mdhd },
2795 { MKTAG('m','d','i','a'), mov_read_default },
2796 { MKTAG('m','e','t','a'), mov_read_meta },
2797 { MKTAG('m','i','n','f'), mov_read_default },
2798 { MKTAG('m','o','o','f'), mov_read_moof },
2799 { MKTAG('m','o','o','v'), mov_read_moov },
2800 { MKTAG('m','v','e','x'), mov_read_default },
2801 { MKTAG('m','v','h','d'), mov_read_mvhd },
2802 { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
2803 { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */
2804 { MKTAG('a','v','c','C'), mov_read_glbl },
2805 { MKTAG('p','a','s','p'), mov_read_pasp },
2806 { MKTAG('s','t','b','l'), mov_read_default },
2807 { MKTAG('s','t','c','o'), mov_read_stco },
2808 { MKTAG('s','t','p','s'), mov_read_stps },
2809 { MKTAG('s','t','r','f'), mov_read_strf },
2810 { MKTAG('s','t','s','c'), mov_read_stsc },
2811 { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
2812 { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
2813 { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
2814 { MKTAG('s','t','t','s'), mov_read_stts },
2815 { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
2816 { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
2817 { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
2818 { MKTAG('t','r','a','k'), mov_read_trak },
2819 { MKTAG('t','r','a','f'), mov_read_default },
2820 { MKTAG('t','r','e','f'), mov_read_default },
2821 { MKTAG('c','h','a','p'), mov_read_chap },
2822 { MKTAG('t','r','e','x'), mov_read_trex },
2823 { MKTAG('t','r','u','n'), mov_read_trun },
2824 { MKTAG('u','d','t','a'), mov_read_default },
2825 { MKTAG('w','a','v','e'), mov_read_wave },
2826 { MKTAG('e','s','d','s'), mov_read_esds },
2827 { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
2828 { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
2829 { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
2830 { MKTAG('w','f','e','x'), mov_read_wfex },
2831 { MKTAG('c','m','o','v'), mov_read_cmov },
2832 { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
2833 { MKTAG('d','v','c','1'), mov_read_dvc1 },
2834 { MKTAG('s','b','g','p'), mov_read_sbgp },
2835 { MKTAG('h','v','c','C'), mov_read_glbl },
2836 { MKTAG('-','-','-','-'), mov_read_custom },
2837 { 0, NULL }
2838 };
2839 
2841 {
2842  int64_t total_size = 0;
2843  MOVAtom a;
2844  int i;
2845 
2846  if (atom.size < 0)
2847  atom.size = INT64_MAX;
2848  while (total_size + 8 < atom.size && !pb->eof_reached) {
2849  int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
2850  a.size = atom.size;
2851  a.type=0;
2852  if (atom.size >= 8) {
2853  a.size = avio_rb32(pb);
2854  a.type = avio_rl32(pb);
2855  }
2856  av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
2857  a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
2858  total_size += 8;
2859  if (a.size == 1) { /* 64 bit extended size */
2860  a.size = avio_rb64(pb) - 8;
2861  total_size += 8;
2862  }
2863  if (a.size == 0) {
2864  a.size = atom.size - total_size;
2865  if (a.size <= 8)
2866  break;
2867  }
2868  a.size -= 8;
2869  if (a.size < 0)
2870  break;
2871  a.size = FFMIN(a.size, atom.size - total_size);
2872 
2873  for (i = 0; mov_default_parse_table[i].type; i++)
2874  if (mov_default_parse_table[i].type == a.type) {
2875  parse = mov_default_parse_table[i].parse;
2876  break;
2877  }
2878 
2879  // container is user data
2880  if (!parse && (atom.type == MKTAG('u','d','t','a') ||
2881  atom.type == MKTAG('i','l','s','t')))
2883 
2884  if (!parse) { /* skip leaf atoms data */
2885  avio_skip(pb, a.size);
2886  } else {
2887  int64_t start_pos = avio_tell(pb);
2888  int64_t left;
2889  int err = parse(c, pb, a);
2890  if (err < 0)
2891  return err;
2892  if (c->found_moov && c->found_mdat &&
2893  ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
2894  start_pos + a.size == avio_size(pb))) {
2895  if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
2896  c->next_root_atom = start_pos + a.size;
2897  return 0;
2898  }
2899  left = a.size - avio_tell(pb) + start_pos;
2900  if (left > 0) /* skip garbage at atom end */
2901  avio_skip(pb, left);
2902  else if (left < 0) {
2903  av_log(c->fc, AV_LOG_WARNING,
2904  "overread end of atom '%.4s' by %"PRId64" bytes\n",
2905  (char*)&a.type, -left);
2906  avio_seek(pb, left, SEEK_CUR);
2907  }
2908  }
2909 
2910  total_size += a.size;
2911  }
2912 
2913  if (total_size < atom.size && atom.size < 0x7ffff)
2914  avio_skip(pb, atom.size - total_size);
2915 
2916  return 0;
2917 }
2918 
2919 static int mov_probe(AVProbeData *p)
2920 {
2921  unsigned int offset;
2922  uint32_t tag;
2923  int score = 0;
2924 
2925  /* check file header */
2926  offset = 0;
2927  for (;;) {
2928  /* ignore invalid offset */
2929  if ((offset + 8) > (unsigned int)p->buf_size)
2930  return score;
2931  tag = AV_RL32(p->buf + offset + 4);
2932  switch(tag) {
2933  /* check for obvious tags */
2934  case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
2935  case MKTAG('m','o','o','v'):
2936  case MKTAG('m','d','a','t'):
2937  case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
2938  case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
2939  case MKTAG('f','t','y','p'):
2940  return AVPROBE_SCORE_MAX;
2941  /* those are more common words, so rate then a bit less */
2942  case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
2943  case MKTAG('w','i','d','e'):
2944  case MKTAG('f','r','e','e'):
2945  case MKTAG('j','u','n','k'):
2946  case MKTAG('p','i','c','t'):
2947  return AVPROBE_SCORE_MAX - 5;
2948  case MKTAG(0x82,0x82,0x7f,0x7d):
2949  case MKTAG('s','k','i','p'):
2950  case MKTAG('u','u','i','d'):
2951  case MKTAG('p','r','f','l'):
2952  offset = AV_RB32(p->buf+offset) + offset;
2953  /* if we only find those cause probedata is too small at least rate them */
2954  score = AVPROBE_SCORE_EXTENSION;
2955  break;
2956  default:
2957  /* unrecognized tag */
2958  return score;
2959  }
2960  }
2961 }
2962 
2963 // must be done after parsing all trak because there's no order requirement
2965 {
2966  MOVContext *mov = s->priv_data;
2967  AVStream *st = NULL;
2968  MOVStreamContext *sc;
2969  int64_t cur_pos;
2970  int i;
2971 
2972  for (i = 0; i < s->nb_streams; i++)
2973  if (s->streams[i]->id == mov->chapter_track) {
2974  st = s->streams[i];
2975  break;
2976  }
2977  if (!st) {
2978  av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
2979  return;
2980  }
2981 
2982  st->discard = AVDISCARD_ALL;
2983  sc = st->priv_data;
2984  cur_pos = avio_tell(sc->pb);
2985 
2986  for (i = 0; i < st->nb_index_entries; i++) {
2987  AVIndexEntry *sample = &st->index_entries[i];
2988  int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
2989  uint8_t *title;
2990  uint16_t ch;
2991  int len, title_len;
2992 
2993  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
2994  av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
2995  goto finish;
2996  }
2997 
2998  // the first two bytes are the length of the title
2999  len = avio_rb16(sc->pb);
3000  if (len > sample->size-2)
3001  continue;
3002  title_len = 2*len + 1;
3003  if (!(title = av_mallocz(title_len)))
3004  goto finish;
3005 
3006  // The samples could theoretically be in any encoding if there's an encd
3007  // atom following, but in practice are only utf-8 or utf-16, distinguished
3008  // instead by the presence of a BOM
3009  if (!len) {
3010  title[0] = 0;
3011  } else {
3012  ch = avio_rb16(sc->pb);
3013  if (ch == 0xfeff)
3014  avio_get_str16be(sc->pb, len, title, title_len);
3015  else if (ch == 0xfffe)
3016  avio_get_str16le(sc->pb, len, title, title_len);
3017  else {
3018  AV_WB16(title, ch);
3019  if (len == 1 || len == 2)
3020  title[len] = 0;
3021  else
3022  avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
3023  }
3024  }
3025 
3026  avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
3027  av_freep(&title);
3028  }
3029 finish:
3030  avio_seek(sc->pb, cur_pos, SEEK_SET);
3031 }
3032 
3034 {
3035  MOVContext *mov = s->priv_data;
3036  int i, j;
3037 
3038  for (i = 0; i < s->nb_streams; i++) {
3039  AVStream *st = s->streams[i];
3040  MOVStreamContext *sc = st->priv_data;
3041 
3042  av_freep(&sc->ctts_data);
3043  for (j = 0; j < sc->drefs_count; j++) {
3044  av_freep(&sc->drefs[j].path);
3045  av_freep(&sc->drefs[j].dir);
3046  }
3047  av_freep(&sc->drefs);
3048  if (sc->pb && sc->pb != s->pb)
3049  avio_close(sc->pb);
3050 
3051  av_freep(&sc->chunk_offsets);
3052  av_freep(&sc->stsc_data);
3053  av_freep(&sc->sample_sizes);
3054  av_freep(&sc->keyframes);
3055  av_freep(&sc->stts_data);
3056  av_freep(&sc->stps_data);
3057  av_freep(&sc->rap_group);
3058  av_freep(&sc->display_matrix);
3059  }
3060 
3061  if (mov->dv_demux) {
3063  mov->dv_fctx = NULL;
3064  }
3065 
3066  av_freep(&mov->trex_data);
3067 
3068  return 0;
3069 }
3070 
3072 {
3073  MOVContext *mov = s->priv_data;
3074  AVIOContext *pb = s->pb;
3075  int err;
3076  MOVAtom atom = { AV_RL32("root") };
3077  int i;
3078 
3079  mov->fc = s;
3080  /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
3081  if (pb->seekable)
3082  atom.size = avio_size(pb);
3083  else
3084  atom.size = INT64_MAX;
3085 
3086  /* check MOV header */
3087  if ((err = mov_read_default(mov, pb, atom)) < 0) {
3088  av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
3089  mov_read_close(s);
3090  return err;
3091  }
3092  if (!mov->found_moov) {
3093  av_log(s, AV_LOG_ERROR, "moov atom not found\n");
3094  mov_read_close(s);
3095  return AVERROR_INVALIDDATA;
3096  }
3097  av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
3098 
3099  if (pb->seekable && mov->chapter_track > 0)
3100  mov_read_chapters(s);
3101 
3102  for (i = 0; i < s->nb_streams; i++) {
3103  AVStream *st = s->streams[i];
3104  MOVStreamContext *sc = st->priv_data;
3105 
3106  if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3107  if (st->codec->width <= 0 || st->codec->height <= 0) {
3108  st->codec->width = sc->width;
3109  st->codec->height = sc->height;
3110  }
3111  if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
3112  if ((err = mov_rewrite_dvd_sub_extradata(st)) < 0)
3113  return err;
3114  }
3115  }
3116  }
3117 
3118  if (mov->trex_data) {
3119  for (i = 0; i < s->nb_streams; i++) {
3120  AVStream *st = s->streams[i];
3121  MOVStreamContext *sc = st->priv_data;
3122  if (st->duration > 0)
3123  st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
3124  }
3125  }
3126 
3127  for (i = 0; i < s->nb_streams; i++) {
3128  AVStream *st = s->streams[i];
3129  MOVStreamContext *sc = st->priv_data;
3130 
3131  switch (st->codec->codec_type) {
3132  case AVMEDIA_TYPE_AUDIO:
3133  err = ff_replaygain_export(st, s->metadata);
3134  if (err < 0) {
3135  mov_read_close(s);
3136  return err;
3137  }
3138  break;
3139  case AVMEDIA_TYPE_VIDEO:
3140  if (sc->display_matrix) {
3141  AVPacketSideData *sd, *tmp;
3142 
3143  tmp = av_realloc_array(st->side_data,
3144  st->nb_side_data + 1, sizeof(*tmp));
3145  if (!tmp)
3146  return AVERROR(ENOMEM);
3147 
3148  st->side_data = tmp;
3149  st->nb_side_data++;
3150 
3151  sd = &st->side_data[st->nb_side_data - 1];
3153  sd->size = sizeof(int32_t) * 9;
3154  sd->data = (uint8_t*)sc->display_matrix;
3155  sc->display_matrix = NULL;
3156  }
3157  break;
3158  }
3159  }
3160 
3161  return 0;
3162 }
3163 
3165 {
3167  int64_t best_dts = INT64_MAX;
3168  int i;
3169  for (i = 0; i < s->nb_streams; i++) {
3170  AVStream *avst = s->streams[i];
3171  MOVStreamContext *msc = avst->priv_data;
3172  if (msc->pb && msc->current_sample < avst->nb_index_entries) {
3173  AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
3174  int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
3175  av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
3176  if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
3177  (s->pb->seekable &&
3178  ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
3179  ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
3180  (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
3181  sample = current_sample;
3182  best_dts = dts;
3183  *st = avst;
3184  }
3185  }
3186  }
3187  return sample;
3188 }
3189 
3191 {
3192  MOVContext *mov = s->priv_data;
3193  MOVStreamContext *sc;
3195  AVStream *st = NULL;
3196  int ret;
3197  retry:
3198  sample = mov_find_next_sample(s, &st);
3199  if (!sample) {
3200  mov->found_mdat = 0;
3201  if (!mov->next_root_atom)
3202  return AVERROR_EOF;
3203  avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
3204  mov->next_root_atom = 0;
3205  if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
3206  s->pb->eof_reached)
3207  return AVERROR_EOF;
3208  av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
3209  goto retry;
3210  }
3211  sc = st->priv_data;
3212  /* must be done just before reading, to avoid infinite loop on sample */
3213  sc->current_sample++;
3214 
3215  if (st->discard != AVDISCARD_ALL) {
3216  if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
3217  av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
3218  sc->ffindex, sample->pos);
3219  return AVERROR_INVALIDDATA;
3220  }
3221  ret = av_get_packet(sc->pb, pkt, sample->size);
3222  if (ret < 0)
3223  return ret;
3224  if (sc->has_palette) {
3225  uint8_t *pal;
3226 
3228  if (!pal) {
3229  av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
3230  } else {
3231  memcpy(pal, sc->palette, AVPALETTE_SIZE);
3232  sc->has_palette = 0;
3233  }
3234  }
3235 #if CONFIG_DV_DEMUXER
3236  if (mov->dv_demux && sc->dv_audio_container) {
3237  avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size);
3238  av_free(pkt->data);
3239  pkt->size = 0;
3240  ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
3241  if (ret < 0)
3242  return ret;
3243  }
3244 #endif
3245  }
3246 
3247  pkt->stream_index = sc->ffindex;
3248  pkt->dts = sample->timestamp;
3249  if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
3250  pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
3251  /* update ctts context */
3252  sc->ctts_sample++;
3253  if (sc->ctts_index < sc->ctts_count &&
3254  sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
3255  sc->ctts_index++;
3256  sc->ctts_sample = 0;
3257  }
3258  if (sc->wrong_dts)
3259  pkt->dts = AV_NOPTS_VALUE;
3260  } else {
3261  int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
3263  pkt->duration = next_dts - pkt->dts;
3264  pkt->pts = pkt->dts;
3265  }
3266  if (st->discard == AVDISCARD_ALL)
3267  goto retry;
3268  pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
3269  pkt->pos = sample->pos;
3270  av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
3271  pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
3272  return 0;
3273 }
3274 
3275 static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
3276 {
3277  MOVStreamContext *sc = st->priv_data;
3278  int sample, time_sample;
3279  int i;
3280 
3281  sample = av_index_search_timestamp(st, timestamp, flags);
3282  av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
3283  if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
3284  sample = 0;
3285  if (sample < 0) /* not sure what to do */
3286  return AVERROR_INVALIDDATA;
3287  sc->current_sample = sample;
3288  av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
3289  /* adjust ctts index */
3290  if (sc->ctts_data) {
3291  time_sample = 0;
3292  for (i = 0; i < sc->ctts_count; i++) {
3293  int next = time_sample + sc->ctts_data[i].count;
3294  if (next > sc->current_sample) {
3295  sc->ctts_index = i;
3296  sc->ctts_sample = sc->current_sample - time_sample;
3297  break;
3298  }
3299  time_sample = next;
3300  }
3301  }
3302  return sample;
3303 }
3304 
3305 static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3306 {
3307  AVStream *st;
3308  int64_t seek_timestamp, timestamp;
3309  int sample;
3310  int i;
3311 
3312  if (stream_index >= s->nb_streams)
3313  return AVERROR_INVALIDDATA;
3314  if (sample_time < 0)
3315  sample_time = 0;
3316 
3317  st = s->streams[stream_index];
3318  sample = mov_seek_stream(s, st, sample_time, flags);
3319  if (sample < 0)
3320  return sample;
3321 
3322  /* adjust seek timestamp to found sample timestamp */
3323  seek_timestamp = st->index_entries[sample].timestamp;
3324 
3325  for (i = 0; i < s->nb_streams; i++) {
3326  st = s->streams[i];
3327  if (stream_index == i)
3328  continue;
3329 
3330  timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
3331  mov_seek_stream(s, st, timestamp, flags);
3332  }
3333  return 0;
3334 }
3335 
3337  .name = "mov,mp4,m4a,3gp,3g2,mj2",
3338  .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
3339  .priv_data_size = sizeof(MOVContext),
3340  .extensions = "mov,mp4,m4a,3gp,3g2,mj2",
3341  .read_probe = mov_probe,
3346 };
int chapter_track
Definition: isom.h:156
int itunes_metadata
metadata are itunes style
Definition: isom.h:155
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:167
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:599
unsigned int rap_group_count
Definition: isom.h:137
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:2668
MOVTrackExt * trex_data
Definition: isom.h:153
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:174
static AVIndexEntry * mov_find_next_sample(AVFormatContext *s, AVStream **st)
Definition: mov.c:3164
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:1956
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:134
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:2200
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:185
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:292
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:131
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:580
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:147
static int64_t duration
Definition: avplay.c:246
static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1653
#define MOV_TFHD_DEFAULT_BASE_IS_MOOF
Definition: isom.h:178
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:197
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:151
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:148
int width
tkhd width
Definition: isom.h:130
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:2919
static int mov_read_header(AVFormatContext *s)
Definition: mov.c:3071
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:595
int count
Definition: isom.h:52
int dts_shift
dts shift when ctts is negative
Definition: isom.h:132
#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:3336
unsigned int count
Definition: isom.h:92
static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2688
#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:183
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:660
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:175
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
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:2353
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 has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1355
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2043
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:2737
static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int64_t size)
Definition: mov.c:2304
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1869
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
Definition: riffdec.c:78
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:800
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:144
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:2566
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:195
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:443
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:2400
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:2840
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:3275
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:1807
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:588
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:2537
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:2156
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:2499
#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:182
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:3190
static int mov_read_close(AVFormatContext *s)
Definition: mov.c:3033
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:146
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:149
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:3305
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:168
MOVSbgp * rap_group
Definition: isom.h:138
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:2964
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:154
#define MOV_TRUN_FIRST_SAMPLE_FLAGS
Definition: isom.h:181
static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:2295
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:135
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:152
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:136
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:783
#define MOV_TFHD_DEFAULT_FLAGS
Definition: isom.h:176
#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:184
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:202
#define MOV_TFHD_STSD_ID
Definition: isom.h:173
static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1686
AVFormatContext * dv_fctx
Definition: isom.h:150
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:2386
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:140
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
int wrong_dts
dts are wrong due to huge ctts offset (iMovie files)
Definition: isom.h:129
#define MOV_TFHD_BASE_DATA_OFFSET
Definition: isom.h:172
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:2777
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:133
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:180
int64_t next_root_atom
offset of the next root atom
Definition: isom.h:157
int time_scale
Definition: isom.h:145
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:2543
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:1613
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:618
static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Definition: mov.c:1729
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:1916
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:188