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