FFmpeg  4.3.7
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/opt.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mathematics.h"
31 #include "avformat.h"
32 #include "avi.h"
33 #include "dv.h"
34 #include "internal.h"
35 #include "isom.h"
36 #include "riff.h"
37 #include "libavcodec/bytestream.h"
38 #include "libavcodec/exif.h"
39 #include "libavcodec/internal.h"
40 
41 typedef struct AVIStream {
42  int64_t frame_offset; /* current frame (video) or byte (audio) counter
43  * (used to compute the pts) */
44  int remaining;
46 
47  uint32_t handler;
48  uint32_t scale;
49  uint32_t rate;
50  int sample_size; /* size of one sample (or packet)
51  * (in the rate/scale sense) in bytes */
52 
53  int64_t cum_len; /* temporary storage (used during seek) */
54  int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
56  uint32_t pal[256];
57  int has_pal;
58  int dshow_block_align; /* block align variable used to emulate bugs in
59  * the MS dshow demuxer */
60 
64 
65  int64_t seek_pos;
66 } AVIStream;
67 
68 typedef struct AVIContext {
69  const AVClass *class;
70  int64_t riff_end;
71  int64_t movi_end;
72  int64_t fsize;
73  int64_t io_fsize;
74  int64_t movi_list;
75  int64_t last_pkt_pos;
77  int is_odml;
82  int64_t odml_read;
83  int64_t odml_max_pos;
84  int use_odml;
85 #define MAX_ODML_DEPTH 1000
86  int64_t dts_max;
87 } AVIContext;
88 
89 
90 static const AVOption options[] = {
91  { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
92  { NULL },
93 };
94 
95 static const AVClass demuxer_class = {
96  .class_name = "avi",
97  .item_name = av_default_item_name,
98  .option = options,
99  .version = LIBAVUTIL_VERSION_INT,
100  .category = AV_CLASS_CATEGORY_DEMUXER,
101 };
102 
103 
104 static const char avi_headers[][8] = {
105  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
106  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
107  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
108  { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
109  { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
110  { 0 }
111 };
112 
114  { "strn", "title" },
115  { 0 },
116 };
117 
118 static int avi_read_close(AVFormatContext *s);
119 static int avi_load_index(AVFormatContext *s);
120 static int guess_ni_flag(AVFormatContext *s);
121 
122 #define print_tag(s, str, tag, size) \
123  av_log(s, 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  av_freep(&value);
324  return AVERROR_INVALIDDATA;
325  }
326  value[size] = 0;
327 
328  AV_WL32(key, tag);
329 
330  return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
332 }
333 
334 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
335  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
336 
337 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
338 {
339  char month[4], time[9], buffer[64];
340  int i, day, year;
341  /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
342  if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
343  month, &day, time, &year) == 4) {
344  for (i = 0; i < 12; i++)
345  if (!av_strcasecmp(month, months[i])) {
346  snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
347  year, i + 1, day, time);
348  av_dict_set(metadata, "creation_time", buffer, 0);
349  }
350  } else if (date[4] == '/' && date[7] == '/') {
351  date[4] = date[7] = '-';
352  av_dict_set(metadata, "creation_time", date, 0);
353  }
354 }
355 
356 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
357 {
358  while (avio_tell(s->pb) < end && !avio_feof(s->pb)) {
359  uint32_t tag = avio_rl32(s->pb);
360  uint32_t size = avio_rl32(s->pb);
361  switch (tag) {
362  case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
363  {
364  uint64_t tag_end = avio_tell(s->pb) + size;
365  while (avio_tell(s->pb) < tag_end && !avio_feof(s->pb)) {
366  uint16_t tag = avio_rl16(s->pb);
367  uint16_t size = avio_rl16(s->pb);
368  const char *name = NULL;
369  char buffer[64] = { 0 };
370  size = FFMIN(size, tag_end - avio_tell(s->pb));
371  size -= avio_read(s->pb, buffer,
372  FFMIN(size, sizeof(buffer) - 1));
373  switch (tag) {
374  case 0x03:
375  name = "maker";
376  break;
377  case 0x04:
378  name = "model";
379  break;
380  case 0x13:
381  name = "creation_time";
382  if (buffer[4] == ':' && buffer[7] == ':')
383  buffer[4] = buffer[7] = '-';
384  break;
385  }
386  if (name)
387  av_dict_set(&s->metadata, name, buffer, 0);
388  avio_skip(s->pb, size);
389  }
390  break;
391  }
392  default:
393  avio_skip(s->pb, size);
394  break;
395  }
396  }
397 }
398 
400 {
401  GetByteContext gb;
402  uint8_t *data = st->codecpar->extradata;
403  int data_size = st->codecpar->extradata_size;
404  int tag, offset;
405 
406  if (!data || data_size < 8) {
407  return AVERROR_INVALIDDATA;
408  }
409 
410  bytestream2_init(&gb, data, data_size);
411 
412  tag = bytestream2_get_le32(&gb);
413 
414  switch (tag) {
415  case MKTAG('A', 'V', 'I', 'F'):
416  // skip 4 byte padding
417  bytestream2_skip(&gb, 4);
418  offset = bytestream2_tell(&gb);
419 
420  // decode EXIF tags from IFD, AVI is always little-endian
421  return avpriv_exif_decode_ifd(s, data + offset, data_size - offset,
422  1, 0, &st->metadata);
423  break;
424  case MKTAG('C', 'A', 'S', 'I'):
425  avpriv_request_sample(s, "RIFF stream data tag type CASI (%u)", tag);
426  break;
427  case MKTAG('Z', 'o', 'r', 'a'):
428  avpriv_request_sample(s, "RIFF stream data tag type Zora (%u)", tag);
429  break;
430  default:
431  break;
432  }
433 
434  return 0;
435 }
436 
438 {
439  AVIContext *avi = s->priv_data;
440  int i, j;
441  int64_t lensum = 0;
442  int64_t maxpos = 0;
443 
444  for (i = 0; i<s->nb_streams; i++) {
445  int64_t len = 0;
446  AVStream *st = s->streams[i];
447 
448  if (!st->nb_index_entries)
449  continue;
450 
451  for (j = 0; j < st->nb_index_entries; j++)
452  len += st->index_entries[j].size;
453  maxpos = FFMAX(maxpos, st->index_entries[j-1].pos);
454  lensum += len;
455  }
456  if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
457  return 0;
458  if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
459  return 0;
460 
461  for (i = 0; i<s->nb_streams; i++) {
462  int64_t len = 0;
463  AVStream *st = s->streams[i];
464  int64_t duration;
465  int64_t bitrate;
466 
467  for (j = 0; j < st->nb_index_entries; j++)
468  len += st->index_entries[j].size;
469 
470  if (st->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
471  continue;
472  duration = st->index_entries[j-1].timestamp - st->index_entries[0].timestamp;
473  bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
474  if (bitrate > 0) {
475  st->codecpar->bit_rate = bitrate;
476  }
477  }
478  return 1;
479 }
480 
481 #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
483 {
484  AVIContext *avi = s->priv_data;
485  AVIOContext *pb = s->pb;
486  unsigned int tag, tag1, handler;
487  int codec_type, stream_index, frame_period;
488  unsigned int size;
489  int i;
490  AVStream *st;
491  AVIStream *ast = NULL;
492  int avih_width = 0, avih_height = 0;
493  int amv_file_format = 0;
494  uint64_t list_end = 0;
495  int64_t pos;
496  int ret;
497  AVDictionaryEntry *dict_entry;
498 
499  avi->stream_index = -1;
500 
501  ret = get_riff(s, pb);
502  if (ret < 0)
503  return ret;
504 
505  av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
506 
507  avi->io_fsize = avi->fsize = avio_size(pb);
508  if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
509  avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
510 
511  /* first list tag */
512  stream_index = -1;
513  codec_type = -1;
514  frame_period = 0;
515  for (;;) {
516  if (avio_feof(pb))
518  tag = avio_rl32(pb);
519  size = avio_rl32(pb);
520 
521  print_tag(s, "tag", tag, size);
522 
523  switch (tag) {
524  case MKTAG('L', 'I', 'S', 'T'):
525  list_end = avio_tell(pb) + size;
526  /* Ignored, except at start of video packets. */
527  tag1 = avio_rl32(pb);
528 
529  print_tag(s, "list", tag1, 0);
530 
531  if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
532  avi->movi_list = avio_tell(pb) - 4;
533  if (size)
534  avi->movi_end = avi->movi_list + size + (size & 1);
535  else
536  avi->movi_end = avi->fsize;
537  av_log(s, AV_LOG_TRACE, "movi end=%"PRIx64"\n", avi->movi_end);
538  goto end_of_header;
539  } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
540  ff_read_riff_info(s, size - 4);
541  else if (tag1 == MKTAG('n', 'c', 'd', 't'))
542  avi_read_nikon(s, list_end);
543 
544  break;
545  case MKTAG('I', 'D', 'I', 'T'):
546  {
547  unsigned char date[64] = { 0 };
548  size += (size & 1);
549  size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
550  avio_skip(pb, size);
552  break;
553  }
554  case MKTAG('d', 'm', 'l', 'h'):
555  avi->is_odml = 1;
556  avio_skip(pb, size + (size & 1));
557  break;
558  case MKTAG('a', 'm', 'v', 'h'):
559  amv_file_format = 1;
560  case MKTAG('a', 'v', 'i', 'h'):
561  /* AVI header */
562  /* using frame_period is bad idea */
563  frame_period = avio_rl32(pb);
564  avio_rl32(pb); /* max. bytes per second */
565  avio_rl32(pb);
567 
568  avio_skip(pb, 2 * 4);
569  avio_rl32(pb);
570  avio_rl32(pb);
571  avih_width = avio_rl32(pb);
572  avih_height = avio_rl32(pb);
573 
574  avio_skip(pb, size - 10 * 4);
575  break;
576  case MKTAG('s', 't', 'r', 'h'):
577  /* stream header */
578 
579  tag1 = avio_rl32(pb);
580  handler = avio_rl32(pb); /* codec tag */
581 
582  if (tag1 == MKTAG('p', 'a', 'd', 's')) {
583  avio_skip(pb, size - 8);
584  break;
585  } else {
586  stream_index++;
587  st = avformat_new_stream(s, NULL);
588  if (!st)
589  RETURN_ERROR(AVERROR(ENOMEM));
590 
591  st->id = stream_index;
592  ast = av_mallocz(sizeof(AVIStream));
593  if (!ast)
594  RETURN_ERROR(AVERROR(ENOMEM));
595  st->priv_data = ast;
596  }
597  if (amv_file_format)
598  tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
599  : MKTAG('v', 'i', 'd', 's');
600 
601  print_tag(s, "strh", tag1, -1);
602 
603  if (tag1 == MKTAG('i', 'a', 'v', 's') ||
604  tag1 == MKTAG('i', 'v', 'a', 's')) {
605  int64_t dv_dur;
606 
607  /* After some consideration -- I don't think we
608  * have to support anything but DV in type1 AVIs. */
609  if (s->nb_streams != 1)
611 
612  if (handler != MKTAG('d', 'v', 's', 'd') &&
613  handler != MKTAG('d', 'v', 'h', 'd') &&
614  handler != MKTAG('d', 'v', 's', 'l'))
615  return AVERROR_INVALIDDATA;
616 
617  if (!CONFIG_DV_DEMUXER)
619 
620  ast = s->streams[0]->priv_data;
621  st->priv_data = NULL;
622  ff_free_stream(s, st);
623 
624  avi->dv_demux = avpriv_dv_init_demux(s);
625  if (!avi->dv_demux) {
626  av_free(ast);
627  return AVERROR(ENOMEM);
628  }
629 
630  s->streams[0]->priv_data = ast;
631  avio_skip(pb, 3 * 4);
632  ast->scale = avio_rl32(pb);
633  ast->rate = avio_rl32(pb);
634  avio_skip(pb, 4); /* start time */
635 
636  dv_dur = avio_rl32(pb);
637  if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
638  dv_dur *= AV_TIME_BASE;
639  s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
640  }
641  /* else, leave duration alone; timing estimation in utils.c
642  * will make a guess based on bitrate. */
643 
644  stream_index = s->nb_streams - 1;
645  avio_skip(pb, size - 9 * 4);
646  break;
647  }
648 
649  av_assert0(stream_index < s->nb_streams);
650  ast->handler = handler;
651 
652  avio_rl32(pb); /* flags */
653  avio_rl16(pb); /* priority */
654  avio_rl16(pb); /* language */
655  avio_rl32(pb); /* initial frame */
656  ast->scale = avio_rl32(pb);
657  ast->rate = avio_rl32(pb);
658  if (!(ast->scale && ast->rate)) {
660  "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
661  "(This file has been generated by broken software.)\n",
662  ast->scale,
663  ast->rate);
664  if (frame_period) {
665  ast->rate = 1000000;
666  ast->scale = frame_period;
667  } else {
668  ast->rate = 25;
669  ast->scale = 1;
670  }
671  }
672  avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
673 
674  ast->cum_len = avio_rl32(pb); /* start */
675  st->nb_frames = avio_rl32(pb);
676 
677  st->start_time = 0;
678  avio_rl32(pb); /* buffer size */
679  avio_rl32(pb); /* quality */
680  if (ast->cum_len > 3600LL * ast->rate / ast->scale) {
681  av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
682  ast->cum_len = 0;
683  }
684  ast->sample_size = avio_rl32(pb);
685  ast->cum_len *= FFMAX(1, ast->sample_size);
686  av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
687  ast->rate, ast->scale, ast->sample_size);
688 
689  switch (tag1) {
690  case MKTAG('v', 'i', 'd', 's'):
691  codec_type = AVMEDIA_TYPE_VIDEO;
692 
693  ast->sample_size = 0;
694  st->avg_frame_rate = av_inv_q(st->time_base);
695  break;
696  case MKTAG('a', 'u', 'd', 's'):
697  codec_type = AVMEDIA_TYPE_AUDIO;
698  break;
699  case MKTAG('t', 'x', 't', 's'):
700  codec_type = AVMEDIA_TYPE_SUBTITLE;
701  break;
702  case MKTAG('d', 'a', 't', 's'):
703  codec_type = AVMEDIA_TYPE_DATA;
704  break;
705  default:
706  av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
707  }
708 
709  if (ast->sample_size < 0) {
710  if (s->error_recognition & AV_EF_EXPLODE) {
711  av_log(s, AV_LOG_ERROR,
712  "Invalid sample_size %d at stream %d\n",
713  ast->sample_size,
714  stream_index);
716  }
718  "Invalid sample_size %d at stream %d "
719  "setting it to 0\n",
720  ast->sample_size,
721  stream_index);
722  ast->sample_size = 0;
723  }
724 
725  if (ast->sample_size == 0) {
726  st->duration = st->nb_frames;
727  if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
728  av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
729  st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
730  }
731  }
732  ast->frame_offset = ast->cum_len;
733  avio_skip(pb, size - 12 * 4);
734  break;
735  case MKTAG('s', 't', 'r', 'f'):
736  /* stream header */
737  if (!size && (codec_type == AVMEDIA_TYPE_AUDIO ||
738  codec_type == AVMEDIA_TYPE_VIDEO))
739  break;
740  if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
741  avio_skip(pb, size);
742  } else {
743  uint64_t cur_pos = avio_tell(pb);
744  unsigned esize;
745  if (cur_pos < list_end)
746  size = FFMIN(size, list_end - cur_pos);
747  st = s->streams[stream_index];
749  avio_skip(pb, size);
750  break;
751  }
752  switch (codec_type) {
753  case AVMEDIA_TYPE_VIDEO:
754  if (amv_file_format) {
755  st->codecpar->width = avih_width;
756  st->codecpar->height = avih_height;
759  avio_skip(pb, size);
760  break;
761  }
762  tag1 = ff_get_bmp_header(pb, st, &esize);
763 
764  if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
765  tag1 == MKTAG('D', 'X', 'S', 'A')) {
767  st->codecpar->codec_tag = tag1;
769  break;
770  }
771 
772  if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
773  if (esize == size-1 && (esize&1)) {
774  st->codecpar->extradata_size = esize - 10 * 4;
775  } else
776  st->codecpar->extradata_size = size - 10 * 4;
777  if (st->codecpar->extradata) {
778  av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
779  }
780  ret = ff_get_extradata(s, st->codecpar, pb,
781  st->codecpar->extradata_size);
782  if (ret < 0)
783  return ret;
784  }
785 
786  // FIXME: check if the encoder really did this correctly
787  if (st->codecpar->extradata_size & 1)
788  avio_r8(pb);
789 
790  /* Extract palette from extradata if bpp <= 8.
791  * This code assumes that extradata contains only palette.
792  * This is true for all paletted codecs implemented in
793  * FFmpeg. */
794  if (st->codecpar->extradata_size &&
795  (st->codecpar->bits_per_coded_sample <= 8)) {
796  int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
797  const uint8_t *pal_src;
798 
799  pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
800  pal_src = st->codecpar->extradata +
801  st->codecpar->extradata_size - pal_size;
802  /* Exclude the "BottomUp" field from the palette */
803  if (pal_src - st->codecpar->extradata >= 9 &&
804  !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
805  pal_src -= 9;
806  for (i = 0; i < pal_size / 4; i++)
807  ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
808  ast->has_pal = 1;
809  }
810 
811  print_tag(s, "video", tag1, 0);
812 
814  st->codecpar->codec_tag = tag1;
816  tag1);
817  /* If codec is not found yet, try with the mov tags. */
818  if (!st->codecpar->codec_id) {
819  st->codecpar->codec_id =
821  if (st->codecpar->codec_id)
823  "mov tag found in avi (fourcc %s)\n",
824  av_fourcc2str(tag1));
825  }
826  if (!st->codecpar->codec_id)
828 
829  /* This is needed to get the pict type which is necessary
830  * for generating correct pts. */
832 
833  if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
834  ast->handler == MKTAG('X', 'V', 'I', 'D'))
835  st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
836 
837  if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
839  if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
841  if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
842  st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
844 
845  if (st->codecpar->codec_tag == 0 && st->codecpar->height > 0 &&
846  st->codecpar->extradata_size < 1U << 30) {
847  st->codecpar->extradata_size += 9;
848  if ((ret = av_reallocp(&st->codecpar->extradata,
849  st->codecpar->extradata_size +
851  st->codecpar->extradata_size = 0;
852  return ret;
853  } else
854  memcpy(st->codecpar->extradata + st->codecpar->extradata_size - 9,
855  "BottomUp", 9);
856  }
857  if (st->codecpar->height == INT_MIN)
858  return AVERROR_INVALIDDATA;
859  st->codecpar->height = FFABS(st->codecpar->height);
860 
861 // avio_skip(pb, size - 5 * 4);
862  break;
863  case AVMEDIA_TYPE_AUDIO:
864  ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
865  if (ret < 0)
866  return ret;
868  if (ast->sample_size && st->codecpar->block_align &&
869  ast->sample_size != st->codecpar->block_align) {
870  av_log(s,
872  "sample size (%d) != block align (%d)\n",
873  ast->sample_size,
874  st->codecpar->block_align);
875  ast->sample_size = st->codecpar->block_align;
876  }
877  /* 2-aligned
878  * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
879  if (size & 1)
880  avio_skip(pb, 1);
881  /* Force parsing as several audio frames can be in
882  * one packet and timestamps refer to packet start. */
884  /* ADTS header is in extradata, AAC without header must be
885  * stored as exact frames. Parser not needed and it will
886  * fail. */
887  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
890  // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
891  if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
893  /* AVI files with Xan DPCM audio (wrongly) declare PCM
894  * audio in the header but have Axan as stream_code_tag. */
895  if (ast->handler == AV_RL32("Axan")) {
897  st->codecpar->codec_tag = 0;
898  ast->dshow_block_align = 0;
899  }
900  if (amv_file_format) {
902  ast->dshow_block_align = 0;
903  }
904  if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
906  st->codecpar->codec_id == AV_CODEC_ID_MP2 ) && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
907  av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
908  ast->dshow_block_align = 0;
909  }
910  if (st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
911  st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
912  st->codecpar->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
913  av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
914  ast->sample_size = 0;
915  }
916  break;
919  st->request_probe= 1;
920  avio_skip(pb, size);
921  break;
922  default:
925  st->codecpar->codec_tag = 0;
926  avio_skip(pb, size);
927  break;
928  }
929  }
930  break;
931  case MKTAG('s', 't', 'r', 'd'):
932  if (stream_index >= (unsigned)s->nb_streams
933  || s->streams[stream_index]->codecpar->extradata_size
934  || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
935  avio_skip(pb, size);
936  } else {
937  uint64_t cur_pos = avio_tell(pb);
938  if (cur_pos < list_end)
939  size = FFMIN(size, list_end - cur_pos);
940  st = s->streams[stream_index];
941 
942  if (size<(1<<30)) {
943  if (st->codecpar->extradata) {
944  av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
945  }
946  if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
947  goto fail;
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 &&
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  goto fail;
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);
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) {
1029  }
1030 
1031  if (!avi->index_loaded && (pb->seekable & AVIO_SEEKABLE_NORMAL))
1032  avi_load_index(s);
1033  calculate_bitrate(s);
1034  avi->index_loaded |= 1;
1035 
1036  if ((ret = guess_ni_flag(s)) < 0)
1037  goto fail;
1038 
1039  avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1040 
1041  dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1042  if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
1043  for (i = 0; i < s->nb_streams; i++) {
1044  AVStream *st = s->streams[i];
1048  }
1049 
1050  for (i = 0; i < s->nb_streams; i++) {
1051  AVStream *st = s->streams[i];
1052  if (st->nb_index_entries)
1053  break;
1054  }
1055  // DV-in-AVI cannot be non-interleaved, if set this must be
1056  // a mis-detection.
1057  if (avi->dv_demux)
1058  avi->non_interleaved = 0;
1059  if (i == s->nb_streams && avi->non_interleaved) {
1061  "Non-interleaved AVI without index, switching to interleaved\n");
1062  avi->non_interleaved = 0;
1063  }
1064 
1065  if (avi->non_interleaved) {
1066  av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1067  clean_index(s);
1068  }
1069 
1070  ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1072 
1073  return 0;
1074 fail:
1075  avi_read_close(s);
1076  return ret;
1077 }
1078 
1080 {
1081  if (pkt->size >= 7 &&
1082  pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1083  !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1084  uint8_t desc[256];
1085  int score = AVPROBE_SCORE_EXTENSION, ret;
1086  AVIStream *ast = st->priv_data;
1087  ff_const59 AVInputFormat *sub_demuxer;
1088  AVRational time_base;
1089  int size;
1090  AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1091  pkt->size - 7,
1092  0, NULL, NULL, NULL, NULL);
1093  AVProbeData pd;
1094  unsigned int desc_len = avio_rl32(pb);
1095 
1096  if (desc_len > pb->buf_end - pb->buf_ptr)
1097  goto error;
1098 
1099  ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1100  avio_skip(pb, desc_len - ret);
1101  if (*desc)
1102  av_dict_set(&st->metadata, "title", desc, 0);
1103 
1104  avio_rl16(pb); /* flags? */
1105  avio_rl32(pb); /* data size */
1106 
1107  size = pb->buf_end - pb->buf_ptr;
1108  pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
1109  .buf_size = size };
1110  if (!pd.buf)
1111  goto error;
1112  memcpy(pd.buf, pb->buf_ptr, size);
1113  sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1114  av_freep(&pd.buf);
1115  if (!sub_demuxer)
1116  goto error;
1117 
1118  if (strcmp(sub_demuxer->name, "srt") && strcmp(sub_demuxer->name, "ass"))
1119  goto error;
1120 
1121  if (!(ast->sub_ctx = avformat_alloc_context()))
1122  goto error;
1123 
1124  ast->sub_ctx->pb = pb;
1125 
1126  if (ff_copy_whiteblacklists(ast->sub_ctx, s) < 0)
1127  goto error;
1128 
1129  if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1130  if (ast->sub_ctx->nb_streams != 1)
1131  goto error;
1132  ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
1134  time_base = ast->sub_ctx->streams[0]->time_base;
1135  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1136  }
1137  ast->sub_buffer = pkt->buf;
1138  pkt->buf = NULL;
1139  av_packet_unref(pkt);
1140  return 1;
1141 
1142 error:
1143  av_freep(&ast->sub_ctx);
1144  avio_context_free(&pb);
1145  }
1146  return 0;
1147 }
1148 
1150  AVPacket *pkt)
1151 {
1152  AVIStream *ast, *next_ast = next_st->priv_data;
1153  int64_t ts, next_ts, ts_min = INT64_MAX;
1154  AVStream *st, *sub_st = NULL;
1155  int i;
1156 
1157  next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1158  AV_TIME_BASE_Q);
1159 
1160  for (i = 0; i < s->nb_streams; i++) {
1161  st = s->streams[i];
1162  ast = st->priv_data;
1163  if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
1164  ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
1165  if (ts <= next_ts && ts < ts_min) {
1166  ts_min = ts;
1167  sub_st = st;
1168  }
1169  }
1170  }
1171 
1172  if (sub_st) {
1173  ast = sub_st->priv_data;
1174  *pkt = ast->sub_pkt;
1175  pkt->stream_index = sub_st->index;
1176 
1177  if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
1178  ast->sub_pkt.data = NULL;
1179  }
1180  return sub_st;
1181 }
1182 
1183 static int get_stream_idx(const unsigned *d)
1184 {
1185  if (d[0] >= '0' && d[0] <= '9' &&
1186  d[1] >= '0' && d[1] <= '9') {
1187  return (d[0] - '0') * 10 + (d[1] - '0');
1188  } else {
1189  return 100; // invalid stream ID
1190  }
1191 }
1192 
1193 /**
1194  *
1195  * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1196  */
1197 static int avi_sync(AVFormatContext *s, int exit_early)
1198 {
1199  AVIContext *avi = s->priv_data;
1200  AVIOContext *pb = s->pb;
1201  int n;
1202  unsigned int d[8];
1203  unsigned int size;
1204  int64_t i, sync;
1205 
1206 start_sync:
1207  memset(d, -1, sizeof(d));
1208  for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1209  int j;
1210 
1211  for (j = 0; j < 7; j++)
1212  d[j] = d[j + 1];
1213  d[7] = avio_r8(pb);
1214 
1215  size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1216 
1217  n = get_stream_idx(d + 2);
1218  ff_tlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1219  d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1220  if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1221  continue;
1222 
1223  // parse ix##
1224  if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1225  // parse JUNK
1226  (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1227  (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1228  (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1229  avio_skip(pb, size);
1230  goto start_sync;
1231  }
1232 
1233  // parse stray LIST
1234  if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1235  avio_skip(pb, 4);
1236  goto start_sync;
1237  }
1238 
1239  n = get_stream_idx(d);
1240 
1241  if (!((i - avi->last_pkt_pos) & 1) &&
1242  get_stream_idx(d + 1) < s->nb_streams)
1243  continue;
1244 
1245  // detect ##ix chunk and skip
1246  if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1247  avio_skip(pb, size);
1248  goto start_sync;
1249  }
1250 
1251  if (d[2] == 'w' && d[3] == 'c' && n < s->nb_streams) {
1252  avio_skip(pb, 16 * 3 + 8);
1253  goto start_sync;
1254  }
1255 
1256  if (avi->dv_demux && n != 0)
1257  continue;
1258 
1259  // parse ##dc/##wb
1260  if (n < s->nb_streams) {
1261  AVStream *st;
1262  AVIStream *ast;
1263  st = s->streams[n];
1264  ast = st->priv_data;
1265 
1266  if (!ast) {
1267  av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1268  continue;
1269  }
1270 
1271  if (s->nb_streams >= 2) {
1272  AVStream *st1 = s->streams[1];
1273  AVIStream *ast1 = st1->priv_data;
1274  // workaround for broken small-file-bug402.avi
1275  if (ast1 && d[2] == 'w' && d[3] == 'b'
1276  && n == 0
1277  && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
1279  && ast->prefix == 'd'*256+'c'
1280  && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1281  ) {
1282  n = 1;
1283  st = st1;
1284  ast = ast1;
1286  "Invalid stream + prefix combination, assuming audio.\n");
1287  }
1288  }
1289 
1290  if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1291  int k = avio_r8(pb);
1292  int last = (k + avio_r8(pb) - 1) & 0xFF;
1293 
1294  avio_rl16(pb); // flags
1295 
1296  // b + (g << 8) + (r << 16);
1297  for (; k <= last; k++)
1298  ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1299 
1300  ast->has_pal = 1;
1301  goto start_sync;
1302  } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1303  d[2] < 128 && d[3] < 128) ||
1304  d[2] * 256 + d[3] == ast->prefix /* ||
1305  (d[2] == 'd' && d[3] == 'c') ||
1306  (d[2] == 'w' && d[3] == 'b') */) {
1307  if (exit_early)
1308  return 0;
1309  if (d[2] * 256 + d[3] == ast->prefix)
1310  ast->prefix_count++;
1311  else {
1312  ast->prefix = d[2] * 256 + d[3];
1313  ast->prefix_count = 0;
1314  }
1315 
1316  if (!avi->dv_demux &&
1317  ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1318  // FIXME: needs a little reordering
1319  (st->discard >= AVDISCARD_NONKEY &&
1320  !(pkt->flags & AV_PKT_FLAG_KEY)) */
1321  || st->discard >= AVDISCARD_ALL)) {
1322 
1323  ast->frame_offset += get_duration(ast, size);
1324  avio_skip(pb, size);
1325  goto start_sync;
1326  }
1327 
1328  avi->stream_index = n;
1329  ast->packet_size = size + 8;
1330  ast->remaining = size;
1331 
1332  if (size) {
1333  uint64_t pos = avio_tell(pb) - 8;
1334  if (!st->index_entries || !st->nb_index_entries ||
1335  st->index_entries[st->nb_index_entries - 1].pos < pos) {
1336  av_add_index_entry(st, pos, ast->frame_offset, size,
1337  0, AVINDEX_KEYFRAME);
1338  }
1339  }
1340  return 0;
1341  }
1342  }
1343  }
1344 
1345  if (pb->error)
1346  return pb->error;
1347  return AVERROR_EOF;
1348 }
1349 
1351 {
1352  AVIContext *avi = s->priv_data;
1353  int best_stream_index = 0;
1354  AVStream *best_st = NULL;
1355  AVIStream *best_ast;
1356  int64_t best_ts = INT64_MAX;
1357  int i;
1358 
1359  for (i = 0; i < s->nb_streams; i++) {
1360  AVStream *st = s->streams[i];
1361  AVIStream *ast = st->priv_data;
1362  int64_t ts = ast->frame_offset;
1363  int64_t last_ts;
1364 
1365  if (!st->nb_index_entries)
1366  continue;
1367 
1368  last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
1369  if (!ast->remaining && ts > last_ts)
1370  continue;
1371 
1372  ts = av_rescale_q(ts, st->time_base,
1373  (AVRational) { FFMAX(1, ast->sample_size),
1374  AV_TIME_BASE });
1375 
1376  av_log(s, AV_LOG_TRACE, "%"PRId64" %d/%d %"PRId64"\n", ts,
1377  st->time_base.num, st->time_base.den, ast->frame_offset);
1378  if (ts < best_ts) {
1379  best_ts = ts;
1380  best_st = st;
1381  best_stream_index = i;
1382  }
1383  }
1384  if (!best_st)
1385  return AVERROR_EOF;
1386 
1387  best_ast = best_st->priv_data;
1388  best_ts = best_ast->frame_offset;
1389  if (best_ast->remaining) {
1390  i = av_index_search_timestamp(best_st,
1391  best_ts,
1392  AVSEEK_FLAG_ANY |
1394  } else {
1395  i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1396  if (i >= 0)
1397  best_ast->frame_offset = best_st->index_entries[i].timestamp;
1398  }
1399 
1400  if (i >= 0) {
1401  int64_t pos = best_st->index_entries[i].pos;
1402  pos += best_ast->packet_size - best_ast->remaining;
1403  if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1404  return AVERROR_EOF;
1405 
1406  av_assert0(best_ast->remaining <= best_ast->packet_size);
1407 
1408  avi->stream_index = best_stream_index;
1409  if (!best_ast->remaining)
1410  best_ast->packet_size =
1411  best_ast->remaining = best_st->index_entries[i].size;
1412  }
1413  else
1414  return AVERROR_EOF;
1415 
1416  return 0;
1417 }
1418 
1420 {
1421  AVIContext *avi = s->priv_data;
1422  AVIOContext *pb = s->pb;
1423  int err;
1424 
1425  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1426  int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1427  if (size >= 0)
1428  return size;
1429  else
1430  goto resync;
1431  }
1432 
1433  if (avi->non_interleaved) {
1434  err = ni_prepare_read(s);
1435  if (err < 0)
1436  return err;
1437  }
1438 
1439 resync:
1440  if (avi->stream_index >= 0) {
1441  AVStream *st = s->streams[avi->stream_index];
1442  AVIStream *ast = st->priv_data;
1443  int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1444  int size, err;
1445 
1446  if (get_subtitle_pkt(s, st, pkt))
1447  return 0;
1448 
1449  // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1450  if (ast->sample_size <= 1)
1451  size = INT_MAX;
1452  else if (ast->sample_size < 32)
1453  // arbitrary multiplier to avoid tiny packets for raw PCM data
1454  size = 1024 * ast->sample_size;
1455  else
1456  size = ast->sample_size;
1457 
1458  if (size > ast->remaining)
1459  size = ast->remaining;
1460  avi->last_pkt_pos = avio_tell(pb);
1461  err = av_get_packet(pb, pkt, size);
1462  if (err < 0)
1463  return err;
1464  size = err;
1465 
1466  if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1467  uint8_t *pal;
1468  pal = av_packet_new_side_data(pkt,
1470  AVPALETTE_SIZE);
1471  if (!pal) {
1472  av_log(s, AV_LOG_ERROR,
1473  "Failed to allocate data for palette\n");
1474  } else {
1475  memcpy(pal, ast->pal, AVPALETTE_SIZE);
1476  ast->has_pal = 0;
1477  }
1478  }
1479 
1480  if (dv_demux) {
1481  AVBufferRef *avbuf = pkt->buf;
1482  size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1483  pkt->data, pkt->size, pkt->pos);
1484  pkt->buf = avbuf;
1485  pkt->flags |= AV_PKT_FLAG_KEY;
1486  if (size < 0)
1487  av_packet_unref(pkt);
1488  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1489  !st->codecpar->codec_tag && read_gab2_sub(s, st, pkt)) {
1490  ast->frame_offset++;
1491  avi->stream_index = -1;
1492  ast->remaining = 0;
1493  goto resync;
1494  } else {
1495  /* XXX: How to handle B-frames in AVI? */
1496  pkt->dts = ast->frame_offset;
1497 // pkt->dts += ast->start;
1498  if (ast->sample_size)
1499  pkt->dts /= ast->sample_size;
1500  pkt->stream_index = avi->stream_index;
1501 
1502  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
1503  AVIndexEntry *e;
1504  int index;
1505 
1507  e = &st->index_entries[index];
1508 
1509  if (index >= 0 && e->timestamp == ast->frame_offset) {
1510  if (index == st->nb_index_entries-1) {
1511  int key=1;
1512  uint32_t state=-1;
1513  if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1514  const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1515  while (ptr < end) {
1516  ptr = avpriv_find_start_code(ptr, end, &state);
1517  if (state == 0x1B6 && ptr < end) {
1518  key = !(*ptr & 0xC0);
1519  break;
1520  }
1521  }
1522  }
1523  if (!key)
1524  e->flags &= ~AVINDEX_KEYFRAME;
1525  }
1526  if (e->flags & AVINDEX_KEYFRAME)
1527  pkt->flags |= AV_PKT_FLAG_KEY;
1528  }
1529  } else {
1530  pkt->flags |= AV_PKT_FLAG_KEY;
1531  }
1532  ast->frame_offset += get_duration(ast, pkt->size);
1533  }
1534  ast->remaining -= err;
1535  if (!ast->remaining) {
1536  avi->stream_index = -1;
1537  ast->packet_size = 0;
1538  }
1539 
1540  if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1541  av_packet_unref(pkt);
1542  goto resync;
1543  }
1544  ast->seek_pos= 0;
1545 
1546  if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
1547  int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1548 
1549  if (avi->dts_max < dts) {
1550  avi->dts_max = dts;
1551  } else if (avi->dts_max - (uint64_t)dts > 2*AV_TIME_BASE) {
1552  avi->non_interleaved= 1;
1553  av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1554  }
1555  }
1556 
1557  return 0;
1558  }
1559 
1560  if ((err = avi_sync(s, 0)) < 0)
1561  return err;
1562  goto resync;
1563 }
1564 
1565 /* XXX: We make the implicit supposition that the positions are sorted
1566  * for each stream. */
1568 {
1569  AVIContext *avi = s->priv_data;
1570  AVIOContext *pb = s->pb;
1571  int nb_index_entries, i;
1572  AVStream *st;
1573  AVIStream *ast;
1574  int64_t pos;
1575  unsigned int index, tag, flags, len, first_packet = 1;
1576  int64_t last_pos = -1;
1577  unsigned last_idx = -1;
1578  int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1579  int anykey = 0;
1580 
1581  nb_index_entries = size / 16;
1582  if (nb_index_entries <= 0)
1583  return AVERROR_INVALIDDATA;
1584 
1585  idx1_pos = avio_tell(pb);
1586  avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1587  if (avi_sync(s, 1) == 0)
1588  first_packet_pos = avio_tell(pb) - 8;
1589  avi->stream_index = -1;
1590  avio_seek(pb, idx1_pos, SEEK_SET);
1591 
1592  if (s->nb_streams == 1 && s->streams[0]->codecpar->codec_tag == AV_RL32("MMES")) {
1593  first_packet_pos = 0;
1594  data_offset = avi->movi_list;
1595  }
1596 
1597  /* Read the entries and sort them in each stream component. */
1598  for (i = 0; i < nb_index_entries; i++) {
1599  if (avio_feof(pb))
1600  return -1;
1601 
1602  tag = avio_rl32(pb);
1603  flags = avio_rl32(pb);
1604  pos = avio_rl32(pb);
1605  len = avio_rl32(pb);
1606  av_log(s, AV_LOG_TRACE, "%d: tag=0x%x flags=0x%x pos=0x%"PRIx64" len=%d/",
1607  i, tag, flags, pos, len);
1608 
1609  index = ((tag & 0xff) - '0') * 10;
1610  index += (tag >> 8 & 0xff) - '0';
1611  if (index >= s->nb_streams)
1612  continue;
1613  st = s->streams[index];
1614  ast = st->priv_data;
1615 
1616  /* Skip 'xxpc' palette change entries in the index until a logic
1617  * to process these is properly implemented. */
1618  if ((tag >> 16 & 0xff) == 'p' && (tag >> 24 & 0xff) == 'c')
1619  continue;
1620 
1621  if (first_packet && first_packet_pos) {
1622  if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
1623  data_offset = first_packet_pos - pos;
1624  first_packet = 0;
1625  }
1626  pos += data_offset;
1627 
1628  av_log(s, AV_LOG_TRACE, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1629 
1630  // even if we have only a single stream, we should
1631  // switch to non-interleaved to get correct timestamps
1632  if (last_pos == pos)
1633  avi->non_interleaved = 1;
1634  if (last_idx != pos && len) {
1635  av_add_index_entry(st, pos, ast->cum_len, len, 0,
1636  (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1637  last_idx= pos;
1638  }
1639  ast->cum_len += get_duration(ast, len);
1640  last_pos = pos;
1641  anykey |= flags&AVIIF_INDEX;
1642  }
1643  if (!anykey) {
1644  for (index = 0; index < s->nb_streams; index++) {
1645  st = s->streams[index];
1646  if (st->nb_index_entries)
1648  }
1649  }
1650  return 0;
1651 }
1652 
1653 /* Scan the index and consider any file with streams more than
1654  * 2 seconds or 64MB apart non-interleaved. */
1656 {
1657  int64_t min_pos, pos;
1658  int i;
1659  int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
1660  if (!idx)
1661  return AVERROR(ENOMEM);
1662  for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1663  int64_t max_dts = INT64_MIN / 2;
1664  int64_t min_dts = INT64_MAX / 2;
1665  int64_t max_buffer = 0;
1666 
1667  min_pos = INT64_MAX;
1668 
1669  for (i = 0; i < s->nb_streams; i++) {
1670  AVStream *st = s->streams[i];
1671  AVIStream *ast = st->priv_data;
1672  int n = st->nb_index_entries;
1673  while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
1674  idx[i]++;
1675  if (idx[i] < n) {
1676  int64_t dts;
1677  dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
1678  FFMAX(ast->sample_size, 1),
1679  st->time_base, AV_TIME_BASE_Q);
1680  min_dts = FFMIN(min_dts, dts);
1681  min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
1682  }
1683  }
1684  for (i = 0; i < s->nb_streams; i++) {
1685  AVStream *st = s->streams[i];
1686  AVIStream *ast = st->priv_data;
1687 
1688  if (idx[i] && min_dts != INT64_MAX / 2) {
1689  int64_t dts;
1690  dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
1691  FFMAX(ast->sample_size, 1),
1692  st->time_base, AV_TIME_BASE_Q);
1693  max_dts = FFMAX(max_dts, dts);
1694  max_buffer = FFMAX(max_buffer,
1695  av_rescale(dts - min_dts,
1696  st->codecpar->bit_rate,
1697  AV_TIME_BASE));
1698  }
1699  }
1700  if (max_dts - min_dts > 2 * AV_TIME_BASE ||
1701  max_buffer > 1024 * 1024 * 8 * 8) {
1702  av_free(idx);
1703  return 1;
1704  }
1705  }
1706  av_free(idx);
1707  return 0;
1708 }
1709 
1711 {
1712  int i;
1713  int64_t last_start = 0;
1714  int64_t first_end = INT64_MAX;
1715  int64_t oldpos = avio_tell(s->pb);
1716 
1717  for (i = 0; i < s->nb_streams; i++) {
1718  AVStream *st = s->streams[i];
1719  int n = st->nb_index_entries;
1720  unsigned int size;
1721 
1722  if (n <= 0)
1723  continue;
1724 
1725  if (n >= 2) {
1726  int64_t pos = st->index_entries[0].pos;
1727  unsigned tag[2];
1728  avio_seek(s->pb, pos, SEEK_SET);
1729  tag[0] = avio_r8(s->pb);
1730  tag[1] = avio_r8(s->pb);
1731  avio_rl16(s->pb);
1732  size = avio_rl32(s->pb);
1733  if (get_stream_idx(tag) == i && pos + size > st->index_entries[1].pos)
1734  last_start = INT64_MAX;
1735  if (get_stream_idx(tag) == i && size == st->index_entries[0].size + 8)
1736  last_start = INT64_MAX;
1737  }
1738 
1739  if (st->index_entries[0].pos > last_start)
1740  last_start = st->index_entries[0].pos;
1741  if (st->index_entries[n - 1].pos < first_end)
1742  first_end = st->index_entries[n - 1].pos;
1743  }
1744  avio_seek(s->pb, oldpos, SEEK_SET);
1745 
1746  if (last_start > first_end)
1747  return 1;
1748 
1749  return check_stream_max_drift(s);
1750 }
1751 
1753 {
1754  AVIContext *avi = s->priv_data;
1755  AVIOContext *pb = s->pb;
1756  uint32_t tag, size;
1757  int64_t pos = avio_tell(pb);
1758  int64_t next;
1759  int ret = -1;
1760 
1761  if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1762  goto the_end; // maybe truncated file
1763  av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1764  for (;;) {
1765  tag = avio_rl32(pb);
1766  size = avio_rl32(pb);
1767  if (avio_feof(pb))
1768  break;
1769  next = avio_tell(pb);
1770  if (next < 0 || next > INT64_MAX - size - (size & 1))
1771  break;
1772  next += size + (size & 1LL);
1773 
1774  if (tag == MKTAG('i', 'd', 'x', '1') &&
1775  avi_read_idx1(s, size) >= 0) {
1776  avi->index_loaded=2;
1777  ret = 0;
1778  }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1779  uint32_t tag1 = avio_rl32(pb);
1780 
1781  if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1782  ff_read_riff_info(s, size - 4);
1783  }else if (!ret)
1784  break;
1785 
1786  if (avio_seek(pb, next, SEEK_SET) < 0)
1787  break; // something is wrong here
1788  }
1789 
1790 the_end:
1791  avio_seek(pb, pos, SEEK_SET);
1792  return ret;
1793 }
1794 
1795 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1796 {
1797  AVIStream *ast2 = st2->priv_data;
1798  int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1799  av_packet_unref(&ast2->sub_pkt);
1800  if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1801  avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1802  ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
1803 }
1804 
1805 static int avi_read_seek(AVFormatContext *s, int stream_index,
1806  int64_t timestamp, int flags)
1807 {
1808  AVIContext *avi = s->priv_data;
1809  AVStream *st;
1810  int i, index;
1811  int64_t pos, pos_min;
1812  AVIStream *ast;
1813 
1814  /* Does not matter which stream is requested dv in avi has the
1815  * stream information in the first video stream.
1816  */
1817  if (avi->dv_demux)
1818  stream_index = 0;
1819 
1820  if (!avi->index_loaded) {
1821  /* we only load the index on demand */
1822  avi_load_index(s);
1823  avi->index_loaded |= 1;
1824  }
1825  av_assert0(stream_index >= 0);
1826 
1827  st = s->streams[stream_index];
1828  ast = st->priv_data;
1829  index = av_index_search_timestamp(st,
1830  timestamp * FFMAX(ast->sample_size, 1),
1831  flags);
1832  if (index < 0) {
1833  if (st->nb_index_entries > 0)
1834  av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1835  timestamp * FFMAX(ast->sample_size, 1),
1836  st->index_entries[0].timestamp,
1838  return AVERROR_INVALIDDATA;
1839  }
1840 
1841  /* find the position */
1842  pos = st->index_entries[index].pos;
1843  timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
1844 
1845  av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1846  timestamp, index, st->index_entries[index].timestamp);
1847 
1848  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1849  /* One and only one real stream for DV in AVI, and it has video */
1850  /* offsets. Calling with other stream indexes should have failed */
1851  /* the av_index_search_timestamp call above. */
1852 
1853  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1854  return -1;
1855 
1856  /* Feed the DV video stream version of the timestamp to the */
1857  /* DV demux so it can synthesize correct timestamps. */
1858  ff_dv_offset_reset(avi->dv_demux, timestamp);
1859 
1860  avi->stream_index = -1;
1861  return 0;
1862  }
1863 
1864  pos_min = pos;
1865  for (i = 0; i < s->nb_streams; i++) {
1866  AVStream *st2 = s->streams[i];
1867  AVIStream *ast2 = st2->priv_data;
1868 
1869  ast2->packet_size =
1870  ast2->remaining = 0;
1871 
1872  if (ast2->sub_ctx) {
1873  seek_subtitle(st, st2, timestamp);
1874  continue;
1875  }
1876 
1877  if (st2->nb_index_entries <= 0)
1878  continue;
1879 
1880 // av_assert1(st2->codecpar->block_align);
1881  index = av_index_search_timestamp(st2,
1882  av_rescale_q(timestamp,
1883  st->time_base,
1884  st2->time_base) *
1885  FFMAX(ast2->sample_size, 1),
1886  flags |
1889  if (index < 0)
1890  index = 0;
1891  ast2->seek_pos = st2->index_entries[index].pos;
1892  pos_min = FFMIN(pos_min,ast2->seek_pos);
1893  }
1894  for (i = 0; i < s->nb_streams; i++) {
1895  AVStream *st2 = s->streams[i];
1896  AVIStream *ast2 = st2->priv_data;
1897 
1898  if (ast2->sub_ctx || st2->nb_index_entries <= 0)
1899  continue;
1900 
1901  index = av_index_search_timestamp(
1902  st2,
1903  av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1905  if (index < 0)
1906  index = 0;
1907  while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
1908  index--;
1909  ast2->frame_offset = st2->index_entries[index].timestamp;
1910  }
1911 
1912  /* do the seek */
1913  if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1914  av_log(s, AV_LOG_ERROR, "Seek failed\n");
1915  return -1;
1916  }
1917  avi->stream_index = -1;
1918  avi->dts_max = INT_MIN;
1919  return 0;
1920 }
1921 
1923 {
1924  int i;
1925  AVIContext *avi = s->priv_data;
1926 
1927  for (i = 0; i < s->nb_streams; i++) {
1928  AVStream *st = s->streams[i];
1929  AVIStream *ast = st->priv_data;
1930  if (ast) {
1931  if (ast->sub_ctx) {
1932  av_freep(&ast->sub_ctx->pb);
1934  }
1935  av_buffer_unref(&ast->sub_buffer);
1936  av_packet_unref(&ast->sub_pkt);
1937  }
1938  }
1939 
1940  av_freep(&avi->dv_demux);
1941 
1942  return 0;
1943 }
1944 
1945 static int avi_probe(const AVProbeData *p)
1946 {
1947  int i;
1948 
1949  /* check file header */
1950  for (i = 0; avi_headers[i][0]; i++)
1951  if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
1952  AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
1953  return AVPROBE_SCORE_MAX;
1954 
1955  return 0;
1956 }
1957 
1959  .name = "avi",
1960  .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
1961  .priv_data_size = sizeof(AVIContext),
1962  .extensions = "avi",
1963  .read_probe = avi_probe,
1968  .priv_class = &demuxer_class,
1969 };
int ff_read_riff_info(AVFormatContext *s, int64_t size)
Definition: riffdec.c:228
static AVStream * get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, AVPacket *pkt)
Definition: avidec.c:1149
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2512
#define ff_tlog(ctx,...)
Definition: internal.h:86
#define NULL
Definition: coverity.c:32
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:797
uint32_t handler
Definition: avidec.c:47
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:334
uint32_t pal[256]
Definition: avidec.c:56
static struct @314 state
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:2052
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3165
AVFormatContext * sub_ctx
Definition: avidec.c:61
#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:85
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:375
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:4948
#define print_tag(s, str, tag, size)
Definition: avidec.c:122
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:159
const char * desc
Definition: nvenc.c:79
int64_t pos
Definition: avformat.h:805
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2514
int64_t last_pkt_pos
Definition: avidec.c:75
uint32_t rate
Definition: avidec.c:49
#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: codec_par.h:60
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:938
int dshow_block_align
Definition: avidec.c:58
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:877
int size
Definition: packet.h:356
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:455
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1105
int is_odml
Definition: avidec.c:77
enum AVMediaType codec_type
Definition: rtp.c:37
int64_t movi_end
Definition: avidec.c:71
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:48
#define AV_RL16
Definition: intreadwrite.h:42
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:536
static int get_duration(AVIStream *ast, int len)
Definition: avidec.c:126
void * priv_data
Definition: avformat.h:891
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
#define AVIIF_INDEX
Definition: avi.h:38
const char * key
discard all
Definition: avcodec.h:236
static AVPacket pkt
static void error(const char *err)
static int ni_prepare_read(AVFormatContext *s)
Definition: avidec.c:1350
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:1655
static const AVMetadataConv avi_metadata_conv[]
Definition: avidec.c:113
uint8_t base
Definition: vp3data.h:202
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:79
static char buffer[20]
Definition: seek.c:32
uint8_t
static int nb_streams
Definition: ffprobe.c:282
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: codec_par.h:126
AVOptions.
static int avi_read_close(AVFormatContext *s)
Definition: avidec.c:1922
int64_t riff_end
Definition: avidec.c:70
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:778
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
int use_odml
Definition: avidec.c:84
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
enum AVStreamParseType need_parsing
Definition: avformat.h:1094
int id
Format-specific stream ID.
Definition: avformat.h:883
int64_t movi_list
Definition: avidec.c:74
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
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: packet.h:355
static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition: avidec.c:1079
uint32_t tag
Definition: movenc.c:1532
#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:307
const AVCodecTag ff_codec_bmp_tags_unofficial[]
Definition: riff.c:500
#define max(a, b)
Definition: cuda_runtime.h:33
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:625
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: codec_par.h:89
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:388
int packet_size
Definition: avidec.c:45
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 ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning...
Definition: avformat.h:544
#define AVIF_MUSTUSEINDEX
Definition: avi.h:25
#define AVINDEX_KEYFRAME
Definition: avformat.h:812
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:126
ff_const59 AVInputFormat * av_probe_input_format2(ff_const59 AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:205
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#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: packet.h:46
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2169
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
#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:44
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:806
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
unsigned int pos
Definition: spdifenc.c:412
#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: codec_id.h:411
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:338
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:1183
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static const uint8_t offset[127][2]
Definition: vf_spp.c:93
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:949
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:123
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
Only parse headers, do not repack.
Definition: avformat.h:796
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
int prefix_count
Definition: avidec.c:55
AVBufferRef * sub_buffer
Definition: avidec.c:63
int non_interleaved
Definition: avidec.c:78
int block_align
Audio only.
Definition: codec_par.h:177
const char * name
Definition: qsvenc.c:46
static int avi_probe(const AVProbeData *p)
Definition: avidec.c:1945
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
void ff_free_stream(AVFormatContext *s, AVStream *st)
Definition: utils.c:4447
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
int64_t cum_len
Definition: avidec.c:53
static int avi_read_header(AVFormatContext *s)
Definition: avidec.c:482
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:334
#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:1795
static int avi_extract_stream_metadata(AVFormatContext *s, AVStream *st)
Definition: avidec.c:399
#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:1666
AVDictionary * metadata
Definition: avformat.h:940
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:828
static int calculate_bitrate(AVFormatContext *s)
Definition: avidec.c:437
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
Stream structure.
Definition: avformat.h:876
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:104
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:1419
int prefix
Definition: avidec.c:54
#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:161
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:976
int index_loaded
Definition: avidec.c:76
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:605
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:1107
double value
Definition: eval.c:98
static const AVOption options[]
Definition: avidec.c:90
Describe the class of an AVClass context structure.
Definition: log.h:67
static int avi_read_idx1(AVFormatContext *s, int size)
Definition: avidec.c:1567
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
discard useless packets like 0 size packets in avi
Definition: avcodec.h:231
#define snprintf
Definition: snprintf.h:34
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
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:441
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:2550
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:591
int64_t seek_pos
Definition: avidec.c:65
#define flags(name, subs,...)
Definition: cbs_av1.c:565
static int avi_load_index(AVFormatContext *s)
Definition: avidec.c:1752
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
#define AVERROR_DEMUXER_NOT_FOUND
Demuxer not found.
Definition: error.h:53
AVInputFormat ff_avi_demuxer
Definition: avidec.c:1958
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:925
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:453
int64_t io_fsize
Definition: avidec.c:73
int64_t bitrate
Definition: h264_levels.c:131
A reference to a data buffer.
Definition: buffer.h:81
static const AVClass demuxer_class
Definition: avidec.c:95
full parsing and repack
Definition: avformat.h:795
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:731
Main libavformat public API header.
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
Definition: avidec.c:356
static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
Definition: avidec.c:337
common internal api header.
int64_t odml_max_pos
Definition: avidec.c:83
AVPacket sub_pkt
Definition: avidec.c:62
if(ret< 0)
Definition: vf_mcdeint.c:279
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:915
#define CONFIG_DV_DEMUXER
Definition: config.h:2155
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:143
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:3346
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:927
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:209
int sample_size
Definition: avidec.c:50
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4498
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
#define av_free(p)
char * value
Definition: dict.h:87
int len
int has_pal
Definition: avidec.c:57
static int guess_ni_flag(AVFormatContext *s)
Definition: avidec.c:1710
void * priv_data
Format private data.
Definition: avformat.h:1379
int64_t frame_offset
Definition: avidec.c:42
int64_t dts_max
Definition: avidec.c:86
int64_t fsize
Definition: avidec.c:72
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:354
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: avidec.c:1805
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:650
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
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:332
int stream_index
Definition: packet.h:357
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
#define MKTAG(a, b, c, d)
Definition: common.h:406
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2115
int64_t odml_read
Definition: avidec.c:82
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:931
int odml_depth
Definition: avidec.c:81
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: avformat.h:1133
This structure stores compressed data.
Definition: packet.h:332
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:755
static int avi_sync(AVFormatContext *s, int exit_early)
Definition: avidec.c:1197
#define RETURN_ERROR(code)
Definition: avidec.c:481
#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:190
DVDemuxContext * dv_demux
Definition: avidec.c:80