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