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