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