FFmpeg  4.1.11
avidec.c
Go to the documentation of this file.
1 /*
2  * AVI demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <inttypes.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bswap.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "avformat.h"
33 #include "avi.h"
34 #include "dv.h"
35 #include "internal.h"
36 #include "isom.h"
37 #include "riff.h"
38 #include "libavcodec/bytestream.h"
39 #include "libavcodec/exif.h"
40 #include "libavcodec/internal.h"
41 
42 typedef struct AVIStream {
43  int64_t frame_offset; /* current frame (video) or byte (audio) counter
44  * (used to compute the pts) */
45  int remaining;
47 
48  uint32_t handler;
49  uint32_t scale;
50  uint32_t rate;
51  int sample_size; /* size of one sample (or packet)
52  * (in the rate/scale sense) in bytes */
53 
54  int64_t cum_len; /* temporary storage (used during seek) */
55  int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
57  uint32_t pal[256];
58  int has_pal;
59  int dshow_block_align; /* block align variable used to emulate bugs in
60  * the MS dshow demuxer */
61 
65 
66  int64_t seek_pos;
67 } AVIStream;
68 
69 typedef struct AVIContext {
70  const AVClass *class;
71  int64_t riff_end;
72  int64_t movi_end;
73  int64_t fsize;
74  int64_t io_fsize;
75  int64_t movi_list;
76  int64_t last_pkt_pos;
78  int is_odml;
83  int64_t odml_read;
84  int64_t odml_max_pos;
85  int use_odml;
86 #define MAX_ODML_DEPTH 1000
87  int64_t dts_max;
88 } AVIContext;
89 
90 
91 static const AVOption options[] = {
92  { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
93  { NULL },
94 };
95 
96 static const AVClass demuxer_class = {
97  .class_name = "avi",
98  .item_name = av_default_item_name,
99  .option = options,
100  .version = LIBAVUTIL_VERSION_INT,
101  .category = AV_CLASS_CATEGORY_DEMUXER,
102 };
103 
104 
105 static const char avi_headers[][8] = {
106  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
107  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
108  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
109  { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
110  { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
111  { 0 }
112 };
113 
115  { "strn", "title" },
116  { 0 },
117 };
118 
119 static int avi_load_index(AVFormatContext *s);
120 static int guess_ni_flag(AVFormatContext *s);
121 
122 #define print_tag(str, tag, size) \
123  av_log(NULL, AV_LOG_TRACE, "pos:%"PRIX64" %s: tag=%s size=0x%x\n", \
124  avio_tell(pb), str, av_fourcc2str(tag), size) \
125 
126 static inline int get_duration(AVIStream *ast, int len)
127 {
128  if (ast->sample_size)
129  return len;
130  else if (ast->dshow_block_align)
131  return (len + (int64_t)ast->dshow_block_align - 1) / ast->dshow_block_align;
132  else
133  return 1;
134 }
135 
137 {
138  AVIContext *avi = s->priv_data;
139  char header[8] = {0};
140  int i;
141 
142  /* check RIFF header */
143  avio_read(pb, header, 4);
144  avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
145  avi->riff_end += avio_tell(pb); /* RIFF chunk end */
146  avio_read(pb, header + 4, 4);
147 
148  for (i = 0; avi_headers[i][0]; i++)
149  if (!memcmp(header, avi_headers[i], 8))
150  break;
151  if (!avi_headers[i][0])
152  return AVERROR_INVALIDDATA;
153 
154  if (header[7] == 0x19)
155  av_log(s, AV_LOG_INFO,
156  "This file has been generated by a totally broken muxer.\n");
157 
158  return 0;
159 }
160 
161 static int read_odml_index(AVFormatContext *s, int64_t frame_num)
162 {
163  AVIContext *avi = s->priv_data;
164  AVIOContext *pb = s->pb;
165  int longs_per_entry = avio_rl16(pb);
166  int index_sub_type = avio_r8(pb);
167  int index_type = avio_r8(pb);
168  int entries_in_use = avio_rl32(pb);
169  int chunk_id = avio_rl32(pb);
170  int64_t base = avio_rl64(pb);
171  int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
172  ((chunk_id >> 8 & 0xFF) - '0');
173  AVStream *st;
174  AVIStream *ast;
175  int i;
176  int64_t last_pos = -1;
177  int64_t filesize = avi->fsize;
178 
179  av_log(s, AV_LOG_TRACE,
180  "longs_per_entry:%d index_type:%d entries_in_use:%d "
181  "chunk_id:%X base:%16"PRIX64" frame_num:%"PRId64"\n",
182  longs_per_entry,
183  index_type,
184  entries_in_use,
185  chunk_id,
186  base,
187  frame_num);
188 
189  if (stream_id >= s->nb_streams || stream_id < 0)
190  return AVERROR_INVALIDDATA;
191  st = s->streams[stream_id];
192  ast = st->priv_data;
193 
194  if (index_sub_type || entries_in_use < 0)
195  return AVERROR_INVALIDDATA;
196 
197  avio_rl32(pb);
198 
199  if (index_type && longs_per_entry != 2)
200  return AVERROR_INVALIDDATA;
201  if (index_type > 1)
202  return AVERROR_INVALIDDATA;
203 
204  if (filesize > 0 && base >= filesize) {
205  av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
206  if (base >> 32 == (base & 0xFFFFFFFF) &&
207  (base & 0xFFFFFFFF) < filesize &&
208  filesize <= 0xFFFFFFFF)
209  base &= 0xFFFFFFFF;
210  else
211  return AVERROR_INVALIDDATA;
212  }
213 
214  for (i = 0; i < entries_in_use; i++) {
215  avi->odml_max_pos = FFMAX(avi->odml_max_pos, avio_tell(pb));
216 
217  // If we read more than there are bytes then we must have been reading something twice
218  if (avi->odml_read > avi->odml_max_pos)
219  return AVERROR_INVALIDDATA;
220 
221  if (index_type) {
222  int64_t pos = avio_rl32(pb) + base - 8;
223  int len = avio_rl32(pb);
224  int key = len >= 0;
225  len &= 0x7FFFFFFF;
226  avi->odml_read += 8;
227 
228  av_log(s, AV_LOG_TRACE, "pos:%"PRId64", len:%X\n", pos, len);
229 
230  if (avio_feof(pb))
231  return AVERROR_INVALIDDATA;
232 
233  if (last_pos == pos || pos == base - 8)
234  avi->non_interleaved = 1;
235  if (last_pos != pos && len)
236  av_add_index_entry(st, pos, ast->cum_len, len, 0,
237  key ? AVINDEX_KEYFRAME : 0);
238 
239  ast->cum_len += get_duration(ast, len);
240  last_pos = pos;
241  } else {
242  int64_t offset, pos;
243  int duration;
244  int ret;
245  avi->odml_read += 16;
246 
247  offset = avio_rl64(pb);
248  avio_rl32(pb); /* size */
249  duration = avio_rl32(pb);
250 
251  if (avio_feof(pb) || offset > INT64_MAX - 8)
252  return AVERROR_INVALIDDATA;
253 
254  pos = avio_tell(pb);
255 
256  if (avi->odml_depth > MAX_ODML_DEPTH) {
257  av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
258  return AVERROR_INVALIDDATA;
259  }
260 
261  if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
262  return -1;
263  avi->odml_depth++;
264  ret = read_odml_index(s, frame_num);
265  avi->odml_depth--;
266  frame_num += duration;
267 
268  if (avio_seek(pb, pos, SEEK_SET) < 0) {
269  av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
270  return -1;
271  }
272  if (ret < 0)
273  return ret;
274  }
275  }
276  avi->index_loaded = 2;
277  return 0;
278 }
279 
281 {
282  int i;
283  int64_t j;
284 
285  for (i = 0; i < s->nb_streams; i++) {
286  AVStream *st = s->streams[i];
287  AVIStream *ast = st->priv_data;
288  int n = st->nb_index_entries;
289  int max = ast->sample_size;
290  int64_t pos, size, ts;
291 
292  if (n != 1 || ast->sample_size == 0)
293  continue;
294 
295  while (max < 1024)
296  max += max;
297 
298  pos = st->index_entries[0].pos;
299  size = st->index_entries[0].size;
300  ts = st->index_entries[0].timestamp;
301 
302  for (j = 0; j < size; j += max)
303  av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
305  }
306 }
307 
308 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
309  uint32_t size)
310 {
311  AVIOContext *pb = s->pb;
312  char key[5] = { 0 };
313  char *value;
314 
315  size += (size & 1);
316 
317  if (size == UINT_MAX)
318  return AVERROR(EINVAL);
319  value = av_malloc(size + 1);
320  if (!value)
321  return AVERROR(ENOMEM);
322  if (avio_read(pb, value, size) != size)
323  return AVERROR_INVALIDDATA;
324  value[size] = 0;
325 
326  AV_WL32(key, tag);
327 
328  return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
330 }
331 
332 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
333  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
334 
335 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
336 {
337  char month[4], time[9], buffer[64];
338  int i, day, year;
339  /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
340  if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
341  month, &day, time, &year) == 4) {
342  for (i = 0; i < 12; i++)
343  if (!av_strcasecmp(month, months[i])) {
344  snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
345  year, i + 1, day, time);
346  av_dict_set(metadata, "creation_time", buffer, 0);
347  }
348  } else if (date[4] == '/' && date[7] == '/') {
349  date[4] = date[7] = '-';
350  av_dict_set(metadata, "creation_time", date, 0);
351  }
352 }
353 
354 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
355 {
356  while (avio_tell(s->pb) < end && !avio_feof(s->pb)) {
357  uint32_t tag = avio_rl32(s->pb);
358  uint32_t size = avio_rl32(s->pb);
359  switch (tag) {
360  case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
361  {
362  uint64_t tag_end = avio_tell(s->pb) + size;
363  while (avio_tell(s->pb) < tag_end && !avio_feof(s->pb)) {
364  uint16_t tag = avio_rl16(s->pb);
365  uint16_t size = avio_rl16(s->pb);
366  const char *name = NULL;
367  char buffer[64] = { 0 };
368  size = FFMIN(size, tag_end - avio_tell(s->pb));
369  size -= avio_read(s->pb, buffer,
370  FFMIN(size, sizeof(buffer) - 1));
371  switch (tag) {
372  case 0x03:
373  name = "maker";
374  break;
375  case 0x04:
376  name = "model";
377  break;
378  case 0x13:
379  name = "creation_time";
380  if (buffer[4] == ':' && buffer[7] == ':')
381  buffer[4] = buffer[7] = '-';
382  break;
383  }
384  if (name)
385  av_dict_set(&s->metadata, name, buffer, 0);
386  avio_skip(s->pb, size);
387  }
388  break;
389  }
390  default:
391  avio_skip(s->pb, size);
392  break;
393  }
394  }
395 }
396 
398 {
399  GetByteContext gb;
400  uint8_t *data = st->codecpar->extradata;
401  int data_size = st->codecpar->extradata_size;
402  int tag, offset;
403 
404  if (!data || data_size < 8) {
405  return AVERROR_INVALIDDATA;
406  }
407 
408  bytestream2_init(&gb, data, data_size);
409 
410  tag = bytestream2_get_le32(&gb);
411 
412  switch (tag) {
413  case MKTAG('A', 'V', 'I', 'F'):
414  // skip 4 byte padding
415  bytestream2_skip(&gb, 4);
416  offset = bytestream2_tell(&gb);
417 
418  // decode EXIF tags from IFD, AVI is always little-endian
419  return avpriv_exif_decode_ifd(s, data + offset, data_size - offset,
420  1, 0, &st->metadata);
421  break;
422  case MKTAG('C', 'A', 'S', 'I'):
423  avpriv_request_sample(s, "RIFF stream data tag type CASI (%u)", tag);
424  break;
425  case MKTAG('Z', 'o', 'r', 'a'):
426  avpriv_request_sample(s, "RIFF stream data tag type Zora (%u)", tag);
427  break;
428  default:
429  break;
430  }
431 
432  return 0;
433 }
434 
436 {
437  AVIContext *avi = s->priv_data;
438  int i, j;
439  int64_t lensum = 0;
440  int64_t maxpos = 0;
441 
442  for (i = 0; i<s->nb_streams; i++) {
443  int64_t len = 0;
444  AVStream *st = s->streams[i];
445 
446  if (!st->nb_index_entries)
447  continue;
448 
449  for (j = 0; j < st->nb_index_entries; j++)
450  len += st->index_entries[j].size;
451  maxpos = FFMAX(maxpos, st->index_entries[j-1].pos);
452  lensum += len;
453  }
454  if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
455  return 0;
456  if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
457  return 0;
458 
459  for (i = 0; i<s->nb_streams; i++) {
460  int64_t len = 0;
461  AVStream *st = s->streams[i];
462  int64_t duration;
463  int64_t bitrate;
464 
465  for (j = 0; j < st->nb_index_entries; j++)
466  len += st->index_entries[j].size;
467 
468  if (st->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
469  continue;
470  duration = st->index_entries[j-1].timestamp - st->index_entries[0].timestamp;
471  bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
472  if (bitrate <= INT_MAX && bitrate > 0) {
473  st->codecpar->bit_rate = bitrate;
474  }
475  }
476  return 1;
477 }
478 
480 {
481  AVIContext *avi = s->priv_data;
482  AVIOContext *pb = s->pb;
483  unsigned int tag, tag1, handler;
484  int codec_type, stream_index, frame_period;
485  unsigned int size;
486  int i;
487  AVStream *st;
488  AVIStream *ast = NULL;
489  int avih_width = 0, avih_height = 0;
490  int amv_file_format = 0;
491  uint64_t list_end = 0;
492  int64_t pos;
493  int ret;
494  AVDictionaryEntry *dict_entry;
495 
496  avi->stream_index = -1;
497 
498  ret = get_riff(s, pb);
499  if (ret < 0)
500  return ret;
501 
502  av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
503 
504  avi->io_fsize = avi->fsize = avio_size(pb);
505  if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
506  avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
507 
508  /* first list tag */
509  stream_index = -1;
510  codec_type = -1;
511  frame_period = 0;
512  for (;;) {
513  if (avio_feof(pb))
514  goto fail;
515  tag = avio_rl32(pb);
516  size = avio_rl32(pb);
517 
518  print_tag("tag", tag, size);
519 
520  switch (tag) {
521  case MKTAG('L', 'I', 'S', 'T'):
522  list_end = avio_tell(pb) + size;
523  /* Ignored, except at start of video packets. */
524  tag1 = avio_rl32(pb);
525 
526  print_tag("list", tag1, 0);
527 
528  if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
529  avi->movi_list = avio_tell(pb) - 4;
530  if (size)
531  avi->movi_end = avi->movi_list + size + (size & 1);
532  else
533  avi->movi_end = avi->fsize;
534  av_log(NULL, AV_LOG_TRACE, "movi end=%"PRIx64"\n", avi->movi_end);
535  goto end_of_header;
536  } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
537  ff_read_riff_info(s, size - 4);
538  else if (tag1 == MKTAG('n', 'c', 'd', 't'))
539  avi_read_nikon(s, list_end);
540 
541  break;
542  case MKTAG('I', 'D', 'I', 'T'):
543  {
544  unsigned char date[64] = { 0 };
545  size += (size & 1);
546  size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
547  avio_skip(pb, size);
549  break;
550  }
551  case MKTAG('d', 'm', 'l', 'h'):
552  avi->is_odml = 1;
553  avio_skip(pb, size + (size & 1));
554  break;
555  case MKTAG('a', 'm', 'v', 'h'):
556  amv_file_format = 1;
557  case MKTAG('a', 'v', 'i', 'h'):
558  /* AVI header */
559  /* using frame_period is bad idea */
560  frame_period = avio_rl32(pb);
561  avio_rl32(pb); /* max. bytes per second */
562  avio_rl32(pb);
564 
565  avio_skip(pb, 2 * 4);
566  avio_rl32(pb);
567  avio_rl32(pb);
568  avih_width = avio_rl32(pb);
569  avih_height = avio_rl32(pb);
570 
571  avio_skip(pb, size - 10 * 4);
572  break;
573  case MKTAG('s', 't', 'r', 'h'):
574  /* stream header */
575 
576  tag1 = avio_rl32(pb);
577  handler = avio_rl32(pb); /* codec tag */
578 
579  if (tag1 == MKTAG('p', 'a', 'd', 's')) {
580  avio_skip(pb, size - 8);
581  break;
582  } else {
583  stream_index++;
584  st = avformat_new_stream(s, NULL);
585  if (!st)
586  goto fail;
587 
588  st->id = stream_index;
589  ast = av_mallocz(sizeof(AVIStream));
590  if (!ast)
591  goto fail;
592  st->priv_data = ast;
593  }
594  if (amv_file_format)
595  tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
596  : MKTAG('v', 'i', 'd', 's');
597 
598  print_tag("strh", tag1, -1);
599 
600  if (tag1 == MKTAG('i', 'a', 'v', 's') ||
601  tag1 == MKTAG('i', 'v', 'a', 's')) {
602  int64_t dv_dur;
603 
604  /* After some consideration -- I don't think we
605  * have to support anything but DV in type1 AVIs. */
606  if (s->nb_streams != 1)
607  goto fail;
608 
609  if (handler != MKTAG('d', 'v', 's', 'd') &&
610  handler != MKTAG('d', 'v', 'h', 'd') &&
611  handler != MKTAG('d', 'v', 's', 'l'))
612  goto fail;
613 
614  ast = s->streams[0]->priv_data;
616  av_freep(&s->streams[0]->codecpar);
617 #if FF_API_LAVF_AVCTX
619  av_freep(&s->streams[0]->codec);
621 #endif
622  if (s->streams[0]->info)
624  av_freep(&s->streams[0]->info);
625  if (s->streams[0]->internal)
626  av_freep(&s->streams[0]->internal->avctx);
627  av_freep(&s->streams[0]->internal);
628  av_freep(&s->streams[0]);
629  s->nb_streams = 0;
630  if (CONFIG_DV_DEMUXER) {
631  avi->dv_demux = avpriv_dv_init_demux(s);
632  if (!avi->dv_demux)
633  goto fail;
634  } else
635  goto fail;
636  s->streams[0]->priv_data = ast;
637  avio_skip(pb, 3 * 4);
638  ast->scale = avio_rl32(pb);
639  ast->rate = avio_rl32(pb);
640  avio_skip(pb, 4); /* start time */
641 
642  dv_dur = avio_rl32(pb);
643  if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
644  dv_dur *= AV_TIME_BASE;
645  s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
646  }
647  /* else, leave duration alone; timing estimation in utils.c
648  * will make a guess based on bitrate. */
649 
650  stream_index = s->nb_streams - 1;
651  avio_skip(pb, size - 9 * 4);
652  break;
653  }
654 
655  av_assert0(stream_index < s->nb_streams);
656  ast->handler = handler;
657 
658  avio_rl32(pb); /* flags */
659  avio_rl16(pb); /* priority */
660  avio_rl16(pb); /* language */
661  avio_rl32(pb); /* initial frame */
662  ast->scale = avio_rl32(pb);
663  ast->rate = avio_rl32(pb);
664  if (!(ast->scale && ast->rate)) {
666  "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
667  "(This file has been generated by broken software.)\n",
668  ast->scale,
669  ast->rate);
670  if (frame_period) {
671  ast->rate = 1000000;
672  ast->scale = frame_period;
673  } else {
674  ast->rate = 25;
675  ast->scale = 1;
676  }
677  }
678  avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
679 
680  ast->cum_len = avio_rl32(pb); /* start */
681  st->nb_frames = avio_rl32(pb);
682 
683  st->start_time = 0;
684  avio_rl32(pb); /* buffer size */
685  avio_rl32(pb); /* quality */
686  if (ast->cum_len > 3600LL * ast->rate / ast->scale) {
687  av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
688  ast->cum_len = 0;
689  }
690  ast->sample_size = avio_rl32(pb);
691  ast->cum_len *= FFMAX(1, ast->sample_size);
692  av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
693  ast->rate, ast->scale, ast->sample_size);
694 
695  switch (tag1) {
696  case MKTAG('v', 'i', 'd', 's'):
697  codec_type = AVMEDIA_TYPE_VIDEO;
698 
699  ast->sample_size = 0;
700  st->avg_frame_rate = av_inv_q(st->time_base);
701  break;
702  case MKTAG('a', 'u', 'd', 's'):
703  codec_type = AVMEDIA_TYPE_AUDIO;
704  break;
705  case MKTAG('t', 'x', 't', 's'):
706  codec_type = AVMEDIA_TYPE_SUBTITLE;
707  break;
708  case MKTAG('d', 'a', 't', 's'):
709  codec_type = AVMEDIA_TYPE_DATA;
710  break;
711  default:
712  av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
713  }
714 
715  if (ast->sample_size < 0) {
716  if (s->error_recognition & AV_EF_EXPLODE) {
717  av_log(s, AV_LOG_ERROR,
718  "Invalid sample_size %d at stream %d\n",
719  ast->sample_size,
720  stream_index);
721  goto fail;
722  }
724  "Invalid sample_size %d at stream %d "
725  "setting it to 0\n",
726  ast->sample_size,
727  stream_index);
728  ast->sample_size = 0;
729  }
730 
731  if (ast->sample_size == 0) {
732  st->duration = st->nb_frames;
733  if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
734  av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
735  st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
736  }
737  }
738  ast->frame_offset = ast->cum_len;
739  avio_skip(pb, size - 12 * 4);
740  break;
741  case MKTAG('s', 't', 'r', 'f'):
742  /* stream header */
743  if (!size && (codec_type == AVMEDIA_TYPE_AUDIO ||
744  codec_type == AVMEDIA_TYPE_VIDEO))
745  break;
746  if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
747  avio_skip(pb, size);
748  } else {
749  uint64_t cur_pos = avio_tell(pb);
750  unsigned esize;
751  if (cur_pos < list_end)
752  size = FFMIN(size, list_end - cur_pos);
753  st = s->streams[stream_index];
755  avio_skip(pb, size);
756  break;
757  }
758  switch (codec_type) {
759  case AVMEDIA_TYPE_VIDEO:
760  if (amv_file_format) {
761  st->codecpar->width = avih_width;
762  st->codecpar->height = avih_height;
765  avio_skip(pb, size);
766  break;
767  }
768  tag1 = ff_get_bmp_header(pb, st, &esize);
769 
770  if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
771  tag1 == MKTAG('D', 'X', 'S', 'A')) {
773  st->codecpar->codec_tag = tag1;
775  break;
776  }
777 
778  if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
779  if (esize == size-1 && (esize&1)) {
780  st->codecpar->extradata_size = esize - 10 * 4;
781  } else
782  st->codecpar->extradata_size = size - 10 * 4;
783  if (st->codecpar->extradata) {
784  av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
785  av_freep(&st->codecpar->extradata);
786  }
787  if (ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size) < 0)
788  return AVERROR(ENOMEM);
789  }
790 
791  // FIXME: check if the encoder really did this correctly
792  if (st->codecpar->extradata_size & 1)
793  avio_r8(pb);
794 
795  /* Extract palette from extradata if bpp <= 8.
796  * This code assumes that extradata contains only palette.
797  * This is true for all paletted codecs implemented in
798  * FFmpeg. */
799  if (st->codecpar->extradata_size &&
800  (st->codecpar->bits_per_coded_sample <= 8)) {
801  int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
802  const uint8_t *pal_src;
803 
804  pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
805  pal_src = st->codecpar->extradata +
806  st->codecpar->extradata_size - pal_size;
807  /* Exclude the "BottomUp" field from the palette */
808  if (pal_src - st->codecpar->extradata >= 9 &&
809  !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
810  pal_src -= 9;
811  for (i = 0; i < pal_size / 4; i++)
812  ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
813  ast->has_pal = 1;
814  }
815 
816  print_tag("video", tag1, 0);
817 
819  st->codecpar->codec_tag = tag1;
821  tag1);
822  /* If codec is not found yet, try with the mov tags. */
823  if (!st->codecpar->codec_id) {
824  st->codecpar->codec_id =
826  if (st->codecpar->codec_id)
828  "mov tag found in avi (fourcc %s)\n",
829  av_fourcc2str(tag1));
830  }
831  /* This is needed to get the pict type which is necessary
832  * for generating correct pts. */
834 
835  if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
836  ast->handler == MKTAG('X', 'V', 'I', 'D'))
837  st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
838 
839  if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
841  if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
843 
844  if (st->codecpar->codec_tag == 0 && st->codecpar->height > 0 &&
845  st->codecpar->extradata_size < 1U << 30) {
846  st->codecpar->extradata_size += 9;
847  if ((ret = av_reallocp(&st->codecpar->extradata,
848  st->codecpar->extradata_size +
850  st->codecpar->extradata_size = 0;
851  return ret;
852  } else
853  memcpy(st->codecpar->extradata + st->codecpar->extradata_size - 9,
854  "BottomUp", 9);
855  }
856  if (st->codecpar->height == INT_MIN)
857  return AVERROR_INVALIDDATA;
858  st->codecpar->height = FFABS(st->codecpar->height);
859 
860 // avio_skip(pb, size - 5 * 4);
861  break;
862  case AVMEDIA_TYPE_AUDIO:
863  ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
864  if (ret < 0)
865  return ret;
867  if (ast->sample_size && st->codecpar->block_align &&
868  ast->sample_size != st->codecpar->block_align) {
869  av_log(s,
871  "sample size (%d) != block align (%d)\n",
872  ast->sample_size,
873  st->codecpar->block_align);
874  ast->sample_size = st->codecpar->block_align;
875  }
876  /* 2-aligned
877  * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
878  if (size & 1)
879  avio_skip(pb, 1);
880  /* Force parsing as several audio frames can be in
881  * one packet and timestamps refer to packet start. */
883  /* ADTS header is in extradata, AAC without header must be
884  * stored as exact frames. Parser not needed and it will
885  * fail. */
886  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
889  // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
890  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
892  /* AVI files with Xan DPCM audio (wrongly) declare PCM
893  * audio in the header but have Axan as stream_code_tag. */
894  if (ast->handler == AV_RL32("Axan")) {
896  st->codecpar->codec_tag = 0;
897  ast->dshow_block_align = 0;
898  }
899  if (amv_file_format) {
901  ast->dshow_block_align = 0;
902  }
903  if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
905  st->codecpar->codec_id == AV_CODEC_ID_MP2 ) && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
906  av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
907  ast->dshow_block_align = 0;
908  }
909  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
910  st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
911  st->codecpar->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
912  av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
913  ast->sample_size = 0;
914  }
915  break;
918  st->request_probe= 1;
919  avio_skip(pb, size);
920  break;
921  default:
924  st->codecpar->codec_tag = 0;
925  avio_skip(pb, size);
926  break;
927  }
928  }
929  break;
930  case MKTAG('s', 't', 'r', 'd'):
931  if (stream_index >= (unsigned)s->nb_streams
932  || s->streams[stream_index]->codecpar->extradata_size
933  || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
934  avio_skip(pb, size);
935  } else {
936  uint64_t cur_pos = avio_tell(pb);
937  if (cur_pos < list_end)
938  size = FFMIN(size, list_end - cur_pos);
939  st = s->streams[stream_index];
940 
941  if (size<(1<<30)) {
942  if (st->codecpar->extradata) {
943  av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
944  av_freep(&st->codecpar->extradata);
945  }
946  if (ff_get_extradata(s, st->codecpar, pb, size) < 0)
947  return AVERROR(ENOMEM);
948  }
949 
950  if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
951  avio_r8(pb);
952 
953  ret = avi_extract_stream_metadata(s, st);
954  if (ret < 0) {
955  av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
956  }
957  }
958  break;
959  case MKTAG('i', 'n', 'd', 'x'):
960  pos = avio_tell(pb);
961  if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
962  avi->use_odml &&
963  read_odml_index(s, 0) < 0 &&
965  goto fail;
966  avio_seek(pb, pos + size, SEEK_SET);
967  break;
968  case MKTAG('v', 'p', 'r', 'p'):
969  if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
970  AVRational active, active_aspect;
971 
972  st = s->streams[stream_index];
973  avio_rl32(pb);
974  avio_rl32(pb);
975  avio_rl32(pb);
976  avio_rl32(pb);
977  avio_rl32(pb);
978 
979  active_aspect.den = avio_rl16(pb);
980  active_aspect.num = avio_rl16(pb);
981  active.num = avio_rl32(pb);
982  active.den = avio_rl32(pb);
983  avio_rl32(pb); // nbFieldsPerFrame
984 
985  if (active_aspect.num && active_aspect.den &&
986  active.num && active.den) {
987  st->sample_aspect_ratio = av_div_q(active_aspect, active);
988  av_log(s, AV_LOG_TRACE, "vprp %d/%d %d/%d\n",
989  active_aspect.num, active_aspect.den,
990  active.num, active.den);
991  }
992  size -= 9 * 4;
993  }
994  avio_skip(pb, size);
995  break;
996  case MKTAG('s', 't', 'r', 'n'):
997  if (s->nb_streams) {
998  ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
999  if (ret < 0)
1000  return ret;
1001  break;
1002  }
1003  default:
1004  if (size > 1000000) {
1005  av_log(s, AV_LOG_ERROR,
1006  "Something went wrong during header parsing, "
1007  "tag %s has size %u, "
1008  "I will ignore it and try to continue anyway.\n",
1009  av_fourcc2str(tag), size);
1011  goto fail;
1012  avi->movi_list = avio_tell(pb) - 4;
1013  avi->movi_end = avi->fsize;
1014  goto end_of_header;
1015  }
1016  /* Do not fail for very large idx1 tags */
1017  case MKTAG('i', 'd', 'x', '1'):
1018  /* skip tag */
1019  size += (size & 1);
1020  avio_skip(pb, size);
1021  break;
1022  }
1023  }
1024 
1025 end_of_header:
1026  /* check stream number */
1027  if (stream_index != s->nb_streams - 1) {
1028 
1029 fail:
1030  return AVERROR_INVALIDDATA;
1031  }
1032 
1033  if (!avi->index_loaded && (pb->seekable & AVIO_SEEKABLE_NORMAL))
1034  avi_load_index(s);
1035  calculate_bitrate(s);
1036  avi->index_loaded |= 1;
1037 
1038  if ((ret = guess_ni_flag(s)) < 0)
1039  return ret;
1040 
1041  avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1042 
1043  dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1044  if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
1045  for (i = 0; i < s->nb_streams; i++) {
1046  AVStream *st = s->streams[i];
1050  }
1051 
1052  for (i = 0; i < s->nb_streams; i++) {
1053  AVStream *st = s->streams[i];
1054  if (st->nb_index_entries)
1055  break;
1056  }
1057  // DV-in-AVI cannot be non-interleaved, if set this must be
1058  // a mis-detection.
1059  if (avi->dv_demux)
1060  avi->non_interleaved = 0;
1061  if (i == s->nb_streams && avi->non_interleaved) {
1063  "Non-interleaved AVI without index, switching to interleaved\n");
1064  avi->non_interleaved = 0;
1065  }
1066 
1067  if (avi->non_interleaved) {
1068  av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1069  clean_index(s);
1070  }
1071 
1072  ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1074 
1075  return 0;
1076 }
1077 
1079 {
1080  if (pkt->size >= 7 &&
1081  pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1082  !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1083  uint8_t desc[256];
1084  int score = AVPROBE_SCORE_EXTENSION, ret;
1085  AVIStream *ast = st->priv_data;
1086  AVInputFormat *sub_demuxer;
1087  AVRational time_base;
1088  int size;
1089  AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1090  pkt->size - 7,
1091  0, NULL, NULL, NULL, NULL);
1092  AVProbeData pd;
1093  unsigned int desc_len = avio_rl32(pb);
1094 
1095  if (desc_len > pb->buf_end - pb->buf_ptr)
1096  goto error;
1097 
1098  ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1099  avio_skip(pb, desc_len - ret);
1100  if (*desc)
1101  av_dict_set(&st->metadata, "title", desc, 0);
1102 
1103  avio_rl16(pb); /* flags? */
1104  avio_rl32(pb); /* data size */
1105 
1106  size = pb->buf_end - pb->buf_ptr;
1107  pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
1108  .buf_size = size };
1109  if (!pd.buf)
1110  goto error;
1111  memcpy(pd.buf, pb->buf_ptr, size);
1112  sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1113  av_freep(&pd.buf);
1114  if (!sub_demuxer)
1115  goto error;
1116 
1117  if (strcmp(sub_demuxer->name, "srt") && strcmp(sub_demuxer->name, "ass"))
1118  goto error;
1119 
1120  if (!(ast->sub_ctx = avformat_alloc_context()))
1121  goto error;
1122 
1123  ast->sub_ctx->pb = pb;
1124 
1125  if (ff_copy_whiteblacklists(ast->sub_ctx, s) < 0)
1126  goto error;
1127 
1128  if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1129  if (ast->sub_ctx->nb_streams != 1)
1130  goto error;
1131  ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
1133  time_base = ast->sub_ctx->streams[0]->time_base;
1134  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1135  }
1136  ast->sub_buffer = pkt->buf;
1137  pkt->buf = NULL;
1138  av_packet_unref(pkt);
1139  return 1;
1140 
1141 error:
1142  av_freep(&ast->sub_ctx);
1143  avio_context_free(&pb);
1144  }
1145  return 0;
1146 }
1147 
1149  AVPacket *pkt)
1150 {
1151  AVIStream *ast, *next_ast = next_st->priv_data;
1152  int64_t ts, next_ts, ts_min = INT64_MAX;
1153  AVStream *st, *sub_st = NULL;
1154  int i;
1155 
1156  next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1157  AV_TIME_BASE_Q);
1158 
1159  for (i = 0; i < s->nb_streams; i++) {
1160  st = s->streams[i];
1161  ast = st->priv_data;
1162  if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
1163  ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
1164  if (ts <= next_ts && ts < ts_min) {
1165  ts_min = ts;
1166  sub_st = st;
1167  }
1168  }
1169  }
1170 
1171  if (sub_st) {
1172  ast = sub_st->priv_data;
1173  *pkt = ast->sub_pkt;
1174  pkt->stream_index = sub_st->index;
1175 
1176  if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
1177  ast->sub_pkt.data = NULL;
1178  }
1179  return sub_st;
1180 }
1181 
1182 static int get_stream_idx(const unsigned *d)
1183 {
1184  if (d[0] >= '0' && d[0] <= '9' &&
1185  d[1] >= '0' && d[1] <= '9') {
1186  return (d[0] - '0') * 10 + (d[1] - '0');
1187  } else {
1188  return 100; // invalid stream ID
1189  }
1190 }
1191 
1192 /**
1193  *
1194  * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1195  */
1196 static int avi_sync(AVFormatContext *s, int exit_early)
1197 {
1198  AVIContext *avi = s->priv_data;
1199  AVIOContext *pb = s->pb;
1200  int n;
1201  unsigned int d[8];
1202  unsigned int size;
1203  int64_t i, sync;
1204 
1205 start_sync:
1206  memset(d, -1, sizeof(d));
1207  for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1208  int j;
1209 
1210  for (j = 0; j < 7; j++)
1211  d[j] = d[j + 1];
1212  d[7] = avio_r8(pb);
1213 
1214  size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1215 
1216  n = get_stream_idx(d + 2);
1217  ff_tlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1218  d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1219  if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1220  continue;
1221 
1222  // parse ix##
1223  if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1224  // parse JUNK
1225  (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1226  (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1227  (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1228  avio_skip(pb, size);
1229  goto start_sync;
1230  }
1231 
1232  // parse stray LIST
1233  if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1234  avio_skip(pb, 4);
1235  goto start_sync;
1236  }
1237 
1238  n = get_stream_idx(d);
1239 
1240  if (!((i - avi->last_pkt_pos) & 1) &&
1241  get_stream_idx(d + 1) < s->nb_streams)
1242  continue;
1243 
1244  // detect ##ix chunk and skip
1245  if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1246  avio_skip(pb, size);
1247  goto start_sync;
1248  }
1249 
1250  if (d[2] == 'w' && d[3] == 'c' && n < s->nb_streams) {
1251  avio_skip(pb, 16 * 3 + 8);
1252  goto start_sync;
1253  }
1254 
1255  if (avi->dv_demux && n != 0)
1256  continue;
1257 
1258  // parse ##dc/##wb
1259  if (n < s->nb_streams) {
1260  AVStream *st;
1261  AVIStream *ast;
1262  st = s->streams[n];
1263  ast = st->priv_data;
1264 
1265  if (!ast) {
1266  av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1267  continue;
1268  }
1269 
1270  if (s->nb_streams >= 2) {
1271  AVStream *st1 = s->streams[1];
1272  AVIStream *ast1 = st1->priv_data;
1273  // workaround for broken small-file-bug402.avi
1274  if (ast1 && d[2] == 'w' && d[3] == 'b'
1275  && n == 0
1276  && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
1278  && ast->prefix == 'd'*256+'c'
1279  && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1280  ) {
1281  n = 1;
1282  st = st1;
1283  ast = ast1;
1285  "Invalid stream + prefix combination, assuming audio.\n");
1286  }
1287  }
1288 
1289  if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1290  int k = avio_r8(pb);
1291  int last = (k + avio_r8(pb) - 1) & 0xFF;
1292 
1293  avio_rl16(pb); // flags
1294 
1295  // b + (g << 8) + (r << 16);
1296  for (; k <= last; k++)
1297  ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1298 
1299  ast->has_pal = 1;
1300  goto start_sync;
1301  } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1302  d[2] < 128 && d[3] < 128) ||
1303  d[2] * 256 + d[3] == ast->prefix /* ||
1304  (d[2] == 'd' && d[3] == 'c') ||
1305  (d[2] == 'w' && d[3] == 'b') */) {
1306  if (exit_early)
1307  return 0;
1308  if (d[2] * 256 + d[3] == ast->prefix)
1309  ast->prefix_count++;
1310  else {
1311  ast->prefix = d[2] * 256 + d[3];
1312  ast->prefix_count = 0;
1313  }
1314 
1315  if (!avi->dv_demux &&
1316  ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1317  // FIXME: needs a little reordering
1318  (st->discard >= AVDISCARD_NONKEY &&
1319  !(pkt->flags & AV_PKT_FLAG_KEY)) */
1320  || st->discard >= AVDISCARD_ALL)) {
1321 
1322  ast->frame_offset += get_duration(ast, size);
1323  avio_skip(pb, size);
1324  goto start_sync;
1325  }
1326 
1327  avi->stream_index = n;
1328  ast->packet_size = size + 8;
1329  ast->remaining = size;
1330 
1331  if (size) {
1332  uint64_t pos = avio_tell(pb) - 8;
1333  if (!st->index_entries || !st->nb_index_entries ||
1334  st->index_entries[st->nb_index_entries - 1].pos < pos) {
1335  av_add_index_entry(st, pos, ast->frame_offset, size,
1336  0, AVINDEX_KEYFRAME);
1337  }
1338  }
1339  return 0;
1340  }
1341  }
1342  }
1343 
1344  if (pb->error)
1345  return pb->error;
1346  return AVERROR_EOF;
1347 }
1348 
1350 {
1351  AVIContext *avi = s->priv_data;
1352  int best_stream_index = 0;
1353  AVStream *best_st = NULL;
1354  AVIStream *best_ast;
1355  int64_t best_ts = INT64_MAX;
1356  int i;
1357 
1358  for (i = 0; i < s->nb_streams; i++) {
1359  AVStream *st = s->streams[i];
1360  AVIStream *ast = st->priv_data;
1361  int64_t ts = ast->frame_offset;
1362  int64_t last_ts;
1363 
1364  if (!st->nb_index_entries)
1365  continue;
1366 
1367  last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
1368  if (!ast->remaining && ts > last_ts)
1369  continue;
1370 
1371  ts = av_rescale_q(ts, st->time_base,
1372  (AVRational) { FFMAX(1, ast->sample_size),
1373  AV_TIME_BASE });
1374 
1375  av_log(s, AV_LOG_TRACE, "%"PRId64" %d/%d %"PRId64"\n", ts,
1376  st->time_base.num, st->time_base.den, ast->frame_offset);
1377  if (ts < best_ts) {
1378  best_ts = ts;
1379  best_st = st;
1380  best_stream_index = i;
1381  }
1382  }
1383  if (!best_st)
1384  return AVERROR_EOF;
1385 
1386  best_ast = best_st->priv_data;
1387  best_ts = best_ast->frame_offset;
1388  if (best_ast->remaining) {
1389  i = av_index_search_timestamp(best_st,
1390  best_ts,
1391  AVSEEK_FLAG_ANY |
1393  } else {
1394  i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1395  if (i >= 0)
1396  best_ast->frame_offset = best_st->index_entries[i].timestamp;
1397  }
1398 
1399  if (i >= 0) {
1400  int64_t pos = best_st->index_entries[i].pos;
1401  pos += best_ast->packet_size - best_ast->remaining;
1402  if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1403  return AVERROR_EOF;
1404 
1405  av_assert0(best_ast->remaining <= best_ast->packet_size);
1406 
1407  avi->stream_index = best_stream_index;
1408  if (!best_ast->remaining)
1409  best_ast->packet_size =
1410  best_ast->remaining = best_st->index_entries[i].size;
1411  }
1412  else
1413  return AVERROR_EOF;
1414 
1415  return 0;
1416 }
1417 
1419 {
1420  AVIContext *avi = s->priv_data;
1421  AVIOContext *pb = s->pb;
1422  int err;
1423 
1424  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1425  int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1426  if (size >= 0)
1427  return size;
1428  else
1429  goto resync;
1430  }
1431 
1432  if (avi->non_interleaved) {
1433  err = ni_prepare_read(s);
1434  if (err < 0)
1435  return err;
1436  }
1437 
1438 resync:
1439  if (avi->stream_index >= 0) {
1440  AVStream *st = s->streams[avi->stream_index];
1441  AVIStream *ast = st->priv_data;
1442  int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1443  int size, err;
1444 
1445  if (get_subtitle_pkt(s, st, pkt))
1446  return 0;
1447 
1448  // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1449  if (ast->sample_size <= 1)
1450  size = INT_MAX;
1451  else if (ast->sample_size < 32)
1452  // arbitrary multiplier to avoid tiny packets for raw PCM data
1453  size = 1024 * ast->sample_size;
1454  else
1455  size = ast->sample_size;
1456 
1457  if (size > ast->remaining)
1458  size = ast->remaining;
1459  avi->last_pkt_pos = avio_tell(pb);
1460  err = av_get_packet(pb, pkt, size);
1461  if (err < 0)
1462  return err;
1463  size = err;
1464 
1465  if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1466  uint8_t *pal;
1467  pal = av_packet_new_side_data(pkt,
1469  AVPALETTE_SIZE);
1470  if (!pal) {
1471  av_log(s, AV_LOG_ERROR,
1472  "Failed to allocate data for palette\n");
1473  } else {
1474  memcpy(pal, ast->pal, AVPALETTE_SIZE);
1475  ast->has_pal = 0;
1476  }
1477  }
1478 
1479  if (dv_demux) {
1480  AVBufferRef *avbuf = pkt->buf;
1481  size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1482  pkt->data, pkt->size, pkt->pos);
1483  pkt->buf = avbuf;
1484  pkt->flags |= AV_PKT_FLAG_KEY;
1485  if (size < 0)
1486  av_packet_unref(pkt);
1487  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1488  !st->codecpar->codec_tag && read_gab2_sub(s, st, pkt)) {
1489  ast->frame_offset++;
1490  avi->stream_index = -1;
1491  ast->remaining = 0;
1492  goto resync;
1493  } else {
1494  /* XXX: How to handle B-frames in AVI? */
1495  pkt->dts = ast->frame_offset;
1496 // pkt->dts += ast->start;
1497  if (ast->sample_size)
1498  pkt->dts /= ast->sample_size;
1499  pkt->stream_index = avi->stream_index;
1500 
1501  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
1502  AVIndexEntry *e;
1503  int index;
1504 
1506  e = &st->index_entries[index];
1507 
1508  if (index >= 0 && e->timestamp == ast->frame_offset) {
1509  if (index == st->nb_index_entries-1) {
1510  int key=1;
1511  uint32_t state=-1;
1512  if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1513  const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1514  while (ptr < end) {
1515  ptr = avpriv_find_start_code(ptr, end, &state);
1516  if (state == 0x1B6 && ptr < end) {
1517  key = !(*ptr & 0xC0);
1518  break;
1519  }
1520  }
1521  }
1522  if (!key)
1523  e->flags &= ~AVINDEX_KEYFRAME;
1524  }
1525  if (e->flags & AVINDEX_KEYFRAME)
1526  pkt->flags |= AV_PKT_FLAG_KEY;
1527  }
1528  } else {
1529  pkt->flags |= AV_PKT_FLAG_KEY;
1530  }
1531  ast->frame_offset += get_duration(ast, pkt->size);
1532  }
1533  ast->remaining -= err;
1534  if (!ast->remaining) {
1535  avi->stream_index = -1;
1536  ast->packet_size = 0;
1537  }
1538 
1539  if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1540  av_packet_unref(pkt);
1541  goto resync;
1542  }
1543  ast->seek_pos= 0;
1544 
1545  if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
1546  int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1547 
1548  if (avi->dts_max < dts) {
1549  avi->dts_max = dts;
1550  } else if (avi->dts_max - (uint64_t)dts > 2*AV_TIME_BASE) {
1551  avi->non_interleaved= 1;
1552  av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1553  }
1554  }
1555 
1556  return 0;
1557  }
1558 
1559  if ((err = avi_sync(s, 0)) < 0)
1560  return err;
1561  goto resync;
1562 }
1563 
1564 /* XXX: We make the implicit supposition that the positions are sorted
1565  * for each stream. */
1567 {
1568  AVIContext *avi = s->priv_data;
1569  AVIOContext *pb = s->pb;
1570  int nb_index_entries, i;
1571  AVStream *st;
1572  AVIStream *ast;
1573  int64_t pos;
1574  unsigned int index, tag, flags, len, first_packet = 1;
1575  int64_t last_pos = -1;
1576  unsigned last_idx = -1;
1577  int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1578  int anykey = 0;
1579 
1580  nb_index_entries = size / 16;
1581  if (nb_index_entries <= 0)
1582  return AVERROR_INVALIDDATA;
1583 
1584  idx1_pos = avio_tell(pb);
1585  avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1586  if (avi_sync(s, 1) == 0)
1587  first_packet_pos = avio_tell(pb) - 8;
1588  avi->stream_index = -1;
1589  avio_seek(pb, idx1_pos, SEEK_SET);
1590 
1591  if (s->nb_streams == 1 && s->streams[0]->codecpar->codec_tag == AV_RL32("MMES")) {
1592  first_packet_pos = 0;
1593  data_offset = avi->movi_list;
1594  }
1595 
1596  /* Read the entries and sort them in each stream component. */
1597  for (i = 0; i < nb_index_entries; i++) {
1598  if (avio_feof(pb))
1599  return -1;
1600 
1601  tag = avio_rl32(pb);
1602  flags = avio_rl32(pb);
1603  pos = avio_rl32(pb);
1604  len = avio_rl32(pb);
1605  av_log(s, AV_LOG_TRACE, "%d: tag=0x%x flags=0x%x pos=0x%"PRIx64" len=%d/",
1606  i, tag, flags, pos, len);
1607 
1608  index = ((tag & 0xff) - '0') * 10;
1609  index += (tag >> 8 & 0xff) - '0';
1610  if (index >= s->nb_streams)
1611  continue;
1612  st = s->streams[index];
1613  ast = st->priv_data;
1614 
1615  /* Skip 'xxpc' palette change entries in the index until a logic
1616  * to process these is properly implemented. */
1617  if ((tag >> 16 & 0xff) == 'p' && (tag >> 24 & 0xff) == 'c')
1618  continue;
1619 
1620  if (first_packet && first_packet_pos) {
1621  if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
1622  data_offset = first_packet_pos - pos;
1623  first_packet = 0;
1624  }
1625  pos += data_offset;
1626 
1627  av_log(s, AV_LOG_TRACE, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1628 
1629  // even if we have only a single stream, we should
1630  // switch to non-interleaved to get correct timestamps
1631  if (last_pos == pos)
1632  avi->non_interleaved = 1;
1633  if (last_idx != pos && len) {
1634  av_add_index_entry(st, pos, ast->cum_len, len, 0,
1635  (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1636  last_idx= pos;
1637  }
1638  ast->cum_len += get_duration(ast, len);
1639  last_pos = pos;
1640  anykey |= flags&AVIIF_INDEX;
1641  }
1642  if (!anykey) {
1643  for (index = 0; index < s->nb_streams; index++) {
1644  st = s->streams[index];
1645  if (st->nb_index_entries)
1647  }
1648  }
1649  return 0;
1650 }
1651 
1652 /* Scan the index and consider any file with streams more than
1653  * 2 seconds or 64MB apart non-interleaved. */
1655 {
1656  int64_t min_pos, pos;
1657  int i;
1658  int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
1659  if (!idx)
1660  return AVERROR(ENOMEM);
1661  for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1662  int64_t max_dts = INT64_MIN / 2;
1663  int64_t min_dts = INT64_MAX / 2;
1664  int64_t max_buffer = 0;
1665 
1666  min_pos = INT64_MAX;
1667 
1668  for (i = 0; i < s->nb_streams; i++) {
1669  AVStream *st = s->streams[i];
1670  AVIStream *ast = st->priv_data;
1671  int n = st->nb_index_entries;
1672  while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
1673  idx[i]++;
1674  if (idx[i] < n) {
1675  int64_t dts;
1676  dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
1677  FFMAX(ast->sample_size, 1),
1678  st->time_base, AV_TIME_BASE_Q);
1679  min_dts = FFMIN(min_dts, dts);
1680  min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
1681  }
1682  }
1683  for (i = 0; i < s->nb_streams; i++) {
1684  AVStream *st = s->streams[i];
1685  AVIStream *ast = st->priv_data;
1686 
1687  if (idx[i] && min_dts != INT64_MAX / 2) {
1688  int64_t dts;
1689  dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
1690  FFMAX(ast->sample_size, 1),
1691  st->time_base, AV_TIME_BASE_Q);
1692  max_dts = FFMAX(max_dts, dts);
1693  max_buffer = FFMAX(max_buffer,
1694  av_rescale(dts - min_dts,
1695  st->codecpar->bit_rate,
1696  AV_TIME_BASE));
1697  }
1698  }
1699  if (max_dts - min_dts > 2 * AV_TIME_BASE ||
1700  max_buffer > 1024 * 1024 * 8 * 8) {
1701  av_free(idx);
1702  return 1;
1703  }
1704  }
1705  av_free(idx);
1706  return 0;
1707 }
1708 
1710 {
1711  int i;
1712  int64_t last_start = 0;
1713  int64_t first_end = INT64_MAX;
1714  int64_t oldpos = avio_tell(s->pb);
1715 
1716  for (i = 0; i < s->nb_streams; i++) {
1717  AVStream *st = s->streams[i];
1718  int n = st->nb_index_entries;
1719  unsigned int size;
1720 
1721  if (n <= 0)
1722  continue;
1723 
1724  if (n >= 2) {
1725  int64_t pos = st->index_entries[0].pos;
1726  unsigned tag[2];
1727  avio_seek(s->pb, pos, SEEK_SET);
1728  tag[0] = avio_r8(s->pb);
1729  tag[1] = avio_r8(s->pb);
1730  avio_rl16(s->pb);
1731  size = avio_rl32(s->pb);
1732  if (get_stream_idx(tag) == i && pos + size > st->index_entries[1].pos)
1733  last_start = INT64_MAX;
1734  if (get_stream_idx(tag) == i && size == st->index_entries[0].size + 8)
1735  last_start = INT64_MAX;
1736  }
1737 
1738  if (st->index_entries[0].pos > last_start)
1739  last_start = st->index_entries[0].pos;
1740  if (st->index_entries[n - 1].pos < first_end)
1741  first_end = st->index_entries[n - 1].pos;
1742  }
1743  avio_seek(s->pb, oldpos, SEEK_SET);
1744 
1745  if (last_start > first_end)
1746  return 1;
1747 
1748  return check_stream_max_drift(s);
1749 }
1750 
1752 {
1753  AVIContext *avi = s->priv_data;
1754  AVIOContext *pb = s->pb;
1755  uint32_t tag, size;
1756  int64_t pos = avio_tell(pb);
1757  int64_t next;
1758  int ret = -1;
1759 
1760  if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1761  goto the_end; // maybe truncated file
1762  av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1763  for (;;) {
1764  tag = avio_rl32(pb);
1765  size = avio_rl32(pb);
1766  if (avio_feof(pb))
1767  break;
1768  next = avio_tell(pb);
1769  if (next < 0 || next > INT64_MAX - size - (size & 1))
1770  break;
1771  next += size + (size & 1LL);
1772 
1773  if (tag == MKTAG('i', 'd', 'x', '1') &&
1774  avi_read_idx1(s, size) >= 0) {
1775  avi->index_loaded=2;
1776  ret = 0;
1777  }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1778  uint32_t tag1 = avio_rl32(pb);
1779 
1780  if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1781  ff_read_riff_info(s, size - 4);
1782  }else if (!ret)
1783  break;
1784 
1785  if (avio_seek(pb, next, SEEK_SET) < 0)
1786  break; // something is wrong here
1787  }
1788 
1789 the_end:
1790  avio_seek(pb, pos, SEEK_SET);
1791  return ret;
1792 }
1793 
1794 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1795 {
1796  AVIStream *ast2 = st2->priv_data;
1797  int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1798  av_packet_unref(&ast2->sub_pkt);
1799  if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1800  avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1801  ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
1802 }
1803 
1804 static int avi_read_seek(AVFormatContext *s, int stream_index,
1805  int64_t timestamp, int flags)
1806 {
1807  AVIContext *avi = s->priv_data;
1808  AVStream *st;
1809  int i, index;
1810  int64_t pos, pos_min;
1811  AVIStream *ast;
1812 
1813  /* Does not matter which stream is requested dv in avi has the
1814  * stream information in the first video stream.
1815  */
1816  if (avi->dv_demux)
1817  stream_index = 0;
1818 
1819  if (!avi->index_loaded) {
1820  /* we only load the index on demand */
1821  avi_load_index(s);
1822  avi->index_loaded |= 1;
1823  }
1824  av_assert0(stream_index >= 0);
1825 
1826  st = s->streams[stream_index];
1827  ast = st->priv_data;
1828  index = av_index_search_timestamp(st,
1829  timestamp * FFMAX(ast->sample_size, 1),
1830  flags);
1831  if (index < 0) {
1832  if (st->nb_index_entries > 0)
1833  av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1834  timestamp * FFMAX(ast->sample_size, 1),
1835  st->index_entries[0].timestamp,
1837  return AVERROR_INVALIDDATA;
1838  }
1839 
1840  /* find the position */
1841  pos = st->index_entries[index].pos;
1842  timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
1843 
1844  av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1845  timestamp, index, st->index_entries[index].timestamp);
1846 
1847  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1848  /* One and only one real stream for DV in AVI, and it has video */
1849  /* offsets. Calling with other stream indexes should have failed */
1850  /* the av_index_search_timestamp call above. */
1851 
1852  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1853  return -1;
1854 
1855  /* Feed the DV video stream version of the timestamp to the */
1856  /* DV demux so it can synthesize correct timestamps. */
1857  ff_dv_offset_reset(avi->dv_demux, timestamp);
1858 
1859  avi->stream_index = -1;
1860  return 0;
1861  }
1862 
1863  pos_min = pos;
1864  for (i = 0; i < s->nb_streams; i++) {
1865  AVStream *st2 = s->streams[i];
1866  AVIStream *ast2 = st2->priv_data;
1867 
1868  ast2->packet_size =
1869  ast2->remaining = 0;
1870 
1871  if (ast2->sub_ctx) {
1872  seek_subtitle(st, st2, timestamp);
1873  continue;
1874  }
1875 
1876  if (st2->nb_index_entries <= 0)
1877  continue;
1878 
1879 // av_assert1(st2->codecpar->block_align);
1880  index = av_index_search_timestamp(st2,
1881  av_rescale_q(timestamp,
1882  st->time_base,
1883  st2->time_base) *
1884  FFMAX(ast2->sample_size, 1),
1885  flags |
1888  if (index < 0)
1889  index = 0;
1890  ast2->seek_pos = st2->index_entries[index].pos;
1891  pos_min = FFMIN(pos_min,ast2->seek_pos);
1892  }
1893  for (i = 0; i < s->nb_streams; i++) {
1894  AVStream *st2 = s->streams[i];
1895  AVIStream *ast2 = st2->priv_data;
1896 
1897  if (ast2->sub_ctx || st2->nb_index_entries <= 0)
1898  continue;
1899 
1900  index = av_index_search_timestamp(
1901  st2,
1902  av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1904  if (index < 0)
1905  index = 0;
1906  while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
1907  index--;
1908  ast2->frame_offset = st2->index_entries[index].timestamp;
1909  }
1910 
1911  /* do the seek */
1912  if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1913  av_log(s, AV_LOG_ERROR, "Seek failed\n");
1914  return -1;
1915  }
1916  avi->stream_index = -1;
1917  avi->dts_max = INT_MIN;
1918  return 0;
1919 }
1920 
1922 {
1923  int i;
1924  AVIContext *avi = s->priv_data;
1925 
1926  for (i = 0; i < s->nb_streams; i++) {
1927  AVStream *st = s->streams[i];
1928  AVIStream *ast = st->priv_data;
1929  if (ast) {
1930  if (ast->sub_ctx) {
1931  av_freep(&ast->sub_ctx->pb);
1933  }
1934  av_buffer_unref(&ast->sub_buffer);
1935  av_packet_unref(&ast->sub_pkt);
1936  }
1937  }
1938 
1939  av_freep(&avi->dv_demux);
1940 
1941  return 0;
1942 }
1943 
1944 static int avi_probe(AVProbeData *p)
1945 {
1946  int i;
1947 
1948  /* check file header */
1949  for (i = 0; avi_headers[i][0]; i++)
1950  if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
1951  AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
1952  return AVPROBE_SCORE_MAX;
1953 
1954  return 0;
1955 }
1956 
1958  .name = "avi",
1959  .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
1960  .priv_data_size = sizeof(AVIContext),
1961  .extensions = "avi",
1962  .read_probe = avi_probe,
1967  .priv_class = &demuxer_class,
1968 };
const char * name
Definition: avisynth_c.h:775
int ff_read_riff_info(AVFormatContext *s, int64_t size)
Definition: riffdec.c:230
static AVStream * get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, AVPacket *pkt)
Definition: avidec.c:1148
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2504
#define ff_tlog(ctx,...)
Definition: internal.h:75
#define NULL
Definition: coverity.c:32
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:795
uint32_t handler
Definition: avidec.c:48
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
uint32_t pal[256]
Definition: avidec.c:57
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
AVOption.
Definition: opt.h:246
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:2048
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3136
AVFormatContext * sub_ctx
Definition: avidec.c:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition: avio.h:229
#define MAX_ODML_DEPTH
Definition: avidec.c:86
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4899
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:164
const char * desc
Definition: nvenc.c:65
int64_t pos
Definition: avformat.h:803
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2506
int64_t last_pkt_pos
Definition: avidec.c:76
uint32_t rate
Definition: avidec.c:50
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
int dshow_block_align
Definition: avidec.c:59
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:462
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1103
int is_odml
Definition: avidec.c:78
enum AVMediaType codec_type
Definition: rtp.c:37
int64_t movi_end
Definition: avidec.c:72
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
uint32_t scale
Definition: avidec.c:49
#define AV_RL16
Definition: intreadwrite.h:42
static int get_duration(AVIStream *ast, int len)
Definition: avidec.c:126
void * priv_data
Definition: avformat.h:889
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
#define AVIIF_INDEX
Definition: avi.h:38
const char * key
discard all
Definition: avcodec.h:803
static AVPacket pkt
static int ni_prepare_read(AVFormatContext *s)
Definition: avidec.c:1349
EXIF metadata parser.
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.
static int check_stream_max_drift(AVFormatContext *s)
Definition: avidec.c:1654
static const AVMetadataConv avi_metadata_conv[]
Definition: avidec.c:114
Format I/O context.
Definition: avformat.h:1351
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:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1484
Public dictionary API.
int stream_index
Definition: avidec.c:80
static char buffer[20]
Definition: seek.c:32
uint8_t
static int nb_streams
Definition: ffprobe.c:276
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: avcodec.h:3966
AVOptions.
static int avi_read_close(AVFormatContext *s)
Definition: avidec.c:1921
int64_t riff_end
Definition: avidec.c:71
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom.c:75
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:800
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
int use_odml
Definition: avidec.c:85
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
enum AVStreamParseType need_parsing
Definition: avformat.h:1092
int id
Format-specific stream ID.
Definition: avformat.h:881
int64_t movi_list
Definition: avidec.c:75
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4469
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
int64_t duration
Definition: movenc.c:63
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
static void clean_index(AVFormatContext *s)
Definition: avidec.c:280
const char data[16]
Definition: mxf.c:91
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:324
uint8_t * data
Definition: avcodec.h:1445
static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: avidec.c:1078
uint32_t tag
Definition: movenc.c:1478
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:313
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static const uint8_t header[24]
Definition: sdr2.c:67
#define av_log(a,...)
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int packet_size
Definition: avidec.c:46
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define U(x)
Definition: vp56_arith.h:37
#define AVIF_MUSTUSEINDEX
Definition: avi.h:25
#define AVINDEX_KEYFRAME
Definition: avformat.h:810
AVIOContext * avio_alloc_context(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))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:131
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2086
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1158
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2159
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
int remaining
Definition: avidec.c:45
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:804
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:559
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1428
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
Definition: avidec.c:308
simple assert() macros that are a bit more flexible than ISO C assert().
static int get_stream_idx(const unsigned *d)
Definition: avidec.c:1182
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
#define FFMAX(a, b)
Definition: common.h:94
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:205
#define fail()
Definition: checkasm.h:117
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
Only parse headers, do not repack.
Definition: avformat.h:794
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
static struct @303 state
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
int prefix_count
Definition: avidec.c:56
AVBufferRef * sub_buffer
Definition: avidec.c:64
int non_interleaved
Definition: avidec.c:79
int block_align
Audio only.
Definition: avcodec.h:4017
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
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:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define FFMIN(a, b)
Definition: common.h:96
void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
Definition: dv.c:436
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int64_t cum_len
Definition: avidec.c:54
static int avi_read_header(AVFormatContext *s)
Definition: avidec.c:479
static int read_odml_index(AVFormatContext *s, int64_t frame_num)
Definition: avidec.c:161
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static const char months[12][4]
Definition: avidec.c:332
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
Definition: avidec.c:1794
static int avi_extract_stream_metadata(AVFormatContext *s, AVStream *st)
Definition: avidec.c:397
#define AVFMT_FLAG_SORT_DTS
try to interleave outputted packets by dts (using this flag can slow demuxing down) ...
Definition: avformat.h:1503
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2669
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
if(ret< 0)
Definition: vf_mcdeint.c:279
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
static void error(const char *err)
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:839
static int calculate_bitrate(AVFormatContext *s)
Definition: avidec.c:435
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:548
Stream structure.
Definition: avformat.h:874
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static const char avi_headers[][8]
Definition: avidec.c:105
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avidec.c:1418
int prefix
Definition: avidec.c:55
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1231
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:364
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
static int resync(AVFormatContext *s)
Definition: flvdec.c:934
int index_loaded
Definition: avidec.c:77
int avpriv_exif_decode_ifd(void *logctx, const uint8_t *buf, int size, int le, int depth, AVDictionary **metadata)
Recursively decodes all IFD&#39;s and adds included TAGS into the metadata dictionary.
Definition: exif.c:144
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition: avformat.h:1042
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:70
int nb_index_entries
Definition: avformat.h:1105
double value
Definition: eval.c:98
static const AVOption options[]
Definition: avidec.c:91
Describe the class of an AVClass context structure.
Definition: log.h:67
static int avi_read_idx1(AVFormatContext *s, int size)
Definition: avidec.c:1566
int index
Definition: gxfenc.c:89
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:277
byte swapping routines
discard useless packets like 0 size packets in avi
Definition: avcodec.h:798
struct AVStream::@238 * info
Stream information used internally by avformat_find_stream_info()
#define snprintf
Definition: snprintf.h:34
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
static int avi_probe(AVProbeData *p)
Definition: avidec.c:1944
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition: utils.c:2540
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:565
int64_t seek_pos
Definition: avidec.c:66
#define flags(name, subs,...)
Definition: cbs_av1.c:610
static int avi_load_index(AVFormatContext *s)
Definition: avidec.c:1751
AVInputFormat ff_avi_demuxer
Definition: avidec.c:1957
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:923
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
int64_t io_fsize
Definition: avidec.c:74
int64_t bitrate
Definition: h264_levels.c:89
A reference to a data buffer.
Definition: buffer.h:81
static const AVClass demuxer_class
Definition: avidec.c:96
full parsing and repack
Definition: avformat.h:793
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
Main libavformat public API header.
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
Definition: avidec.c:354
static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
Definition: avidec.c:335
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
int64_t odml_max_pos
Definition: avidec.c:84
AVPacket sub_pkt
Definition: avidec.c:63
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:913
#define CONFIG_DV_DEMUXER
Definition: config.h:2027
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1618
static int get_riff(AVFormatContext *s, AVIOContext *pb)
Definition: avidec.c:136
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:148
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3317
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:925
int den
Denominator.
Definition: rational.h:60
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:347
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
Read BITMAPINFOHEADER structure and set AVStream codec width, height and bits_per_encoded_sample fiel...
Definition: riffdec.c:211
int sample_size
Definition: avidec.c:51
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4441
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
#define av_free(p)
char * value
Definition: dict.h:87
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int len
int has_pal
Definition: avidec.c:58
static int guess_ni_flag(AVFormatContext *s)
Definition: avidec.c:1709
void * priv_data
Format private data.
Definition: avformat.h:1379
#define print_tag(str, tag, size)
Definition: avidec.c:122
int64_t frame_offset
Definition: avidec.c:43
int64_t dts_max
Definition: avidec.c:87
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:540
int64_t fsize
Definition: avidec.c:73
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3942
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3914
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:172
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: avidec.c:1804
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1466
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:329
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
#define MKTAG(a, b, c, d)
Definition: common.h:366
int64_t odml_read
Definition: avidec.c:83
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:929
int odml_depth
Definition: avidec.c:82
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: avformat.h:1132
This structure stores compressed data.
Definition: avcodec.h:1422
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:777
static int avi_sync(AVFormatContext *s, int exit_early)
Definition: avidec.c:1196
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:191
DVDemuxContext * dv_demux
Definition: avidec.c:81