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