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