FFmpeg  2.1.1
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 "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/bswap.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mathematics.h"
30 #include "avformat.h"
31 #include "avi.h"
32 #include "dv.h"
33 #include "internal.h"
34 #include "riff.h"
35 
36 typedef struct AVIStream {
37  int64_t frame_offset; /* current frame (video) or byte (audio) counter
38  * (used to compute the pts) */
39  int remaining;
41 
42  uint32_t scale;
43  uint32_t rate;
44  int sample_size; /* size of one sample (or packet)
45  * (in the rate/scale sense) in bytes */
46 
47  int64_t cum_len; /* temporary storage (used during seek) */
48  int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
50  uint32_t pal[256];
51  int has_pal;
52  int dshow_block_align; /* block align variable used to emulate bugs in
53  * the MS dshow demuxer */
54 
58 
59  int64_t seek_pos;
60 } AVIStream;
61 
62 typedef struct {
63  const AVClass *class;
64  int64_t riff_end;
65  int64_t movi_end;
66  int64_t fsize;
67  int64_t io_fsize;
68  int64_t movi_list;
69  int64_t last_pkt_pos;
71  int is_odml;
76  int use_odml;
77 #define MAX_ODML_DEPTH 1000
78  int64_t dts_max;
79 } AVIContext;
80 
81 
82 static const AVOption options[] = {
83  { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
84  { NULL },
85 };
86 
87 static const AVClass demuxer_class = {
88  .class_name = "avi",
89  .item_name = av_default_item_name,
90  .option = options,
91  .version = LIBAVUTIL_VERSION_INT,
92  .category = AV_CLASS_CATEGORY_DEMUXER,
93 };
94 
95 
96 static const char avi_headers[][8] = {
97  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
98  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
99  { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
100  { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
101  { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
102  { 0 }
103 };
104 
106  { "strn", "title" },
107  { 0 },
108 };
109 
110 static int avi_load_index(AVFormatContext *s);
111 static int guess_ni_flag(AVFormatContext *s);
112 
113 #define print_tag(str, tag, size) \
114  av_dlog(NULL, "%s: tag=%c%c%c%c size=0x%x\n", \
115  str, tag & 0xff, \
116  (tag >> 8) & 0xff, \
117  (tag >> 16) & 0xff, \
118  (tag >> 24) & 0xff, \
119  size)
120 
121 static inline int get_duration(AVIStream *ast, int len)
122 {
123  if (ast->sample_size)
124  return len;
125  else if (ast->dshow_block_align)
126  return (len + ast->dshow_block_align - 1) / ast->dshow_block_align;
127  else
128  return 1;
129 }
130 
132 {
133  AVIContext *avi = s->priv_data;
134  char header[8];
135  int i;
136 
137  /* check RIFF header */
138  avio_read(pb, header, 4);
139  avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
140  avi->riff_end += avio_tell(pb); /* RIFF chunk end */
141  avio_read(pb, header + 4, 4);
142 
143  for (i = 0; avi_headers[i][0]; i++)
144  if (!memcmp(header, avi_headers[i], 8))
145  break;
146  if (!avi_headers[i][0])
147  return AVERROR_INVALIDDATA;
148 
149  if (header[7] == 0x19)
150  av_log(s, AV_LOG_INFO,
151  "This file has been generated by a totally broken muxer.\n");
152 
153  return 0;
154 }
155 
156 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
157 {
158  AVIContext *avi = s->priv_data;
159  AVIOContext *pb = s->pb;
160  int longs_pre_entry = avio_rl16(pb);
161  int index_sub_type = avio_r8(pb);
162  int index_type = avio_r8(pb);
163  int entries_in_use = avio_rl32(pb);
164  int chunk_id = avio_rl32(pb);
165  int64_t base = avio_rl64(pb);
166  int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
167  ((chunk_id >> 8 & 0xFF) - '0');
168  AVStream *st;
169  AVIStream *ast;
170  int i;
171  int64_t last_pos = -1;
172  int64_t filesize = avi->fsize;
173 
174  av_dlog(s,
175  "longs_pre_entry:%d index_type:%d entries_in_use:%d "
176  "chunk_id:%X base:%16"PRIX64"\n",
177  longs_pre_entry,
178  index_type,
179  entries_in_use,
180  chunk_id,
181  base);
182 
183  if (stream_id >= s->nb_streams || stream_id < 0)
184  return AVERROR_INVALIDDATA;
185  st = s->streams[stream_id];
186  ast = st->priv_data;
187 
188  if (index_sub_type)
189  return AVERROR_INVALIDDATA;
190 
191  avio_rl32(pb);
192 
193  if (index_type && longs_pre_entry != 2)
194  return AVERROR_INVALIDDATA;
195  if (index_type > 1)
196  return AVERROR_INVALIDDATA;
197 
198  if (filesize > 0 && base >= filesize) {
199  av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
200  if (base >> 32 == (base & 0xFFFFFFFF) &&
201  (base & 0xFFFFFFFF) < filesize &&
202  filesize <= 0xFFFFFFFF)
203  base &= 0xFFFFFFFF;
204  else
205  return AVERROR_INVALIDDATA;
206  }
207 
208  for (i = 0; i < entries_in_use; i++) {
209  if (index_type) {
210  int64_t pos = avio_rl32(pb) + base - 8;
211  int len = avio_rl32(pb);
212  int key = len >= 0;
213  len &= 0x7FFFFFFF;
214 
215 #ifdef DEBUG_SEEK
216  av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
217 #endif
218  if (url_feof(pb))
219  return AVERROR_INVALIDDATA;
220 
221  if (last_pos == pos || pos == base - 8)
222  avi->non_interleaved = 1;
223  if (last_pos != pos && (len || !ast->sample_size))
224  av_add_index_entry(st, pos, ast->cum_len, len, 0,
225  key ? AVINDEX_KEYFRAME : 0);
226 
227  ast->cum_len += get_duration(ast, len);
228  last_pos = pos;
229  } else {
230  int64_t offset, pos;
231  int duration;
232  offset = avio_rl64(pb);
233  avio_rl32(pb); /* size */
234  duration = avio_rl32(pb);
235 
236  if (url_feof(pb))
237  return AVERROR_INVALIDDATA;
238 
239  pos = avio_tell(pb);
240 
241  if (avi->odml_depth > MAX_ODML_DEPTH) {
242  av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
243  return AVERROR_INVALIDDATA;
244  }
245 
246  if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
247  return -1;
248  avi->odml_depth++;
249  read_braindead_odml_indx(s, frame_num);
250  avi->odml_depth--;
251  frame_num += duration;
252 
253  if (avio_seek(pb, pos, SEEK_SET) < 0) {
254  av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
255  return -1;
256  }
257 
258  }
259  }
260  avi->index_loaded = 2;
261  return 0;
262 }
263 
265 {
266  int i;
267  int64_t j;
268 
269  for (i = 0; i < s->nb_streams; i++) {
270  AVStream *st = s->streams[i];
271  AVIStream *ast = st->priv_data;
272  int n = st->nb_index_entries;
273  int max = ast->sample_size;
274  int64_t pos, size, ts;
275 
276  if (n != 1 || ast->sample_size == 0)
277  continue;
278 
279  while (max < 1024)
280  max += max;
281 
282  pos = st->index_entries[0].pos;
283  size = st->index_entries[0].size;
284  ts = st->index_entries[0].timestamp;
285 
286  for (j = 0; j < size; j += max)
287  av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
289  }
290 }
291 
292 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
293  uint32_t size)
294 {
295  AVIOContext *pb = s->pb;
296  char key[5] = { 0 };
297  char *value;
298 
299  size += (size & 1);
300 
301  if (size == UINT_MAX)
302  return AVERROR(EINVAL);
303  value = av_malloc(size + 1);
304  if (!value)
305  return AVERROR(ENOMEM);
306  avio_read(pb, value, size);
307  value[size] = 0;
308 
309  AV_WL32(key, tag);
310 
311  return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
313 }
314 
315 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
316  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
317 
318 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
319 {
320  char month[4], time[9], buffer[64];
321  int i, day, year;
322  /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
323  if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
324  month, &day, time, &year) == 4) {
325  for (i = 0; i < 12; i++)
326  if (!av_strcasecmp(month, months[i])) {
327  snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
328  year, i + 1, day, time);
329  av_dict_set(metadata, "creation_time", buffer, 0);
330  }
331  } else if (date[4] == '/' && date[7] == '/') {
332  date[4] = date[7] = '-';
333  av_dict_set(metadata, "creation_time", date, 0);
334  }
335 }
336 
337 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
338 {
339  while (avio_tell(s->pb) < end) {
340  uint32_t tag = avio_rl32(s->pb);
341  uint32_t size = avio_rl32(s->pb);
342  switch (tag) {
343  case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
344  {
345  uint64_t tag_end = avio_tell(s->pb) + size;
346  while (avio_tell(s->pb) < tag_end) {
347  uint16_t tag = avio_rl16(s->pb);
348  uint16_t size = avio_rl16(s->pb);
349  const char *name = NULL;
350  char buffer[64] = { 0 };
351  size -= avio_read(s->pb, buffer,
352  FFMIN(size, sizeof(buffer) - 1));
353  switch (tag) {
354  case 0x03:
355  name = "maker";
356  break;
357  case 0x04:
358  name = "model";
359  break;
360  case 0x13:
361  name = "creation_time";
362  if (buffer[4] == ':' && buffer[7] == ':')
363  buffer[4] = buffer[7] = '-';
364  break;
365  }
366  if (name)
367  av_dict_set(&s->metadata, name, buffer, 0);
368  avio_skip(s->pb, size);
369  }
370  break;
371  }
372  default:
373  avio_skip(s->pb, size);
374  break;
375  }
376  }
377 }
378 
380 {
381  AVIContext *avi = s->priv_data;
382  AVIOContext *pb = s->pb;
383  unsigned int tag, tag1, handler;
384  int codec_type, stream_index, frame_period;
385  unsigned int size;
386  int i;
387  AVStream *st;
388  AVIStream *ast = NULL;
389  int avih_width = 0, avih_height = 0;
390  int amv_file_format = 0;
391  uint64_t list_end = 0;
392  int ret;
393  AVDictionaryEntry *dict_entry;
394 
395  avi->stream_index = -1;
396 
397  ret = get_riff(s, pb);
398  if (ret < 0)
399  return ret;
400 
401  av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
402 
403  avi->io_fsize = avi->fsize = avio_size(pb);
404  if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
405  avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
406 
407  /* first list tag */
408  stream_index = -1;
409  codec_type = -1;
410  frame_period = 0;
411  for (;;) {
412  if (url_feof(pb))
413  goto fail;
414  tag = avio_rl32(pb);
415  size = avio_rl32(pb);
416 
417  print_tag("tag", tag, size);
418 
419  switch (tag) {
420  case MKTAG('L', 'I', 'S', 'T'):
421  list_end = avio_tell(pb) + size;
422  /* Ignored, except at start of video packets. */
423  tag1 = avio_rl32(pb);
424 
425  print_tag("list", tag1, 0);
426 
427  if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
428  avi->movi_list = avio_tell(pb) - 4;
429  if (size)
430  avi->movi_end = avi->movi_list + size + (size & 1);
431  else
432  avi->movi_end = avi->fsize;
433  av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
434  goto end_of_header;
435  } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
436  ff_read_riff_info(s, size - 4);
437  else if (tag1 == MKTAG('n', 'c', 'd', 't'))
438  avi_read_nikon(s, list_end);
439 
440  break;
441  case MKTAG('I', 'D', 'I', 'T'):
442  {
443  unsigned char date[64] = { 0 };
444  size += (size & 1);
445  size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
446  avio_skip(pb, size);
448  break;
449  }
450  case MKTAG('d', 'm', 'l', 'h'):
451  avi->is_odml = 1;
452  avio_skip(pb, size + (size & 1));
453  break;
454  case MKTAG('a', 'm', 'v', 'h'):
455  amv_file_format = 1;
456  case MKTAG('a', 'v', 'i', 'h'):
457  /* AVI header */
458  /* using frame_period is bad idea */
459  frame_period = avio_rl32(pb);
460  avio_rl32(pb); /* max. bytes per second */
461  avio_rl32(pb);
463 
464  avio_skip(pb, 2 * 4);
465  avio_rl32(pb);
466  avio_rl32(pb);
467  avih_width = avio_rl32(pb);
468  avih_height = avio_rl32(pb);
469 
470  avio_skip(pb, size - 10 * 4);
471  break;
472  case MKTAG('s', 't', 'r', 'h'):
473  /* stream header */
474 
475  tag1 = avio_rl32(pb);
476  handler = avio_rl32(pb); /* codec tag */
477 
478  if (tag1 == MKTAG('p', 'a', 'd', 's')) {
479  avio_skip(pb, size - 8);
480  break;
481  } else {
482  stream_index++;
483  st = avformat_new_stream(s, NULL);
484  if (!st)
485  goto fail;
486 
487  st->id = stream_index;
488  ast = av_mallocz(sizeof(AVIStream));
489  if (!ast)
490  goto fail;
491  st->priv_data = ast;
492  }
493  if (amv_file_format)
494  tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
495  : MKTAG('v', 'i', 'd', 's');
496 
497  print_tag("strh", tag1, -1);
498 
499  if (tag1 == MKTAG('i', 'a', 'v', 's') ||
500  tag1 == MKTAG('i', 'v', 'a', 's')) {
501  int64_t dv_dur;
502 
503  /* After some consideration -- I don't think we
504  * have to support anything but DV in type1 AVIs. */
505  if (s->nb_streams != 1)
506  goto fail;
507 
508  if (handler != MKTAG('d', 'v', 's', 'd') &&
509  handler != MKTAG('d', 'v', 'h', 'd') &&
510  handler != MKTAG('d', 'v', 's', 'l'))
511  goto fail;
512 
513  ast = s->streams[0]->priv_data;
514  av_freep(&s->streams[0]->codec->extradata);
515  av_freep(&s->streams[0]->codec);
516  if (s->streams[0]->info)
518  av_freep(&s->streams[0]->info);
519  av_freep(&s->streams[0]);
520  s->nb_streams = 0;
521  if (CONFIG_DV_DEMUXER) {
522  avi->dv_demux = avpriv_dv_init_demux(s);
523  if (!avi->dv_demux)
524  goto fail;
525  } else
526  goto fail;
527  s->streams[0]->priv_data = ast;
528  avio_skip(pb, 3 * 4);
529  ast->scale = avio_rl32(pb);
530  ast->rate = avio_rl32(pb);
531  avio_skip(pb, 4); /* start time */
532 
533  dv_dur = avio_rl32(pb);
534  if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
535  dv_dur *= AV_TIME_BASE;
536  s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
537  }
538  /* else, leave duration alone; timing estimation in utils.c
539  * will make a guess based on bitrate. */
540 
541  stream_index = s->nb_streams - 1;
542  avio_skip(pb, size - 9 * 4);
543  break;
544  }
545 
546  av_assert0(stream_index < s->nb_streams);
548 
549  avio_rl32(pb); /* flags */
550  avio_rl16(pb); /* priority */
551  avio_rl16(pb); /* language */
552  avio_rl32(pb); /* initial frame */
553  ast->scale = avio_rl32(pb);
554  ast->rate = avio_rl32(pb);
555  if (!(ast->scale && ast->rate)) {
557  "scale/rate is %u/%u which is invalid. "
558  "(This file has been generated by broken software.)\n",
559  ast->scale,
560  ast->rate);
561  if (frame_period) {
562  ast->rate = 1000000;
563  ast->scale = frame_period;
564  } else {
565  ast->rate = 25;
566  ast->scale = 1;
567  }
568  }
569  avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
570 
571  ast->cum_len = avio_rl32(pb); /* start */
572  st->nb_frames = avio_rl32(pb);
573 
574  st->start_time = 0;
575  avio_rl32(pb); /* buffer size */
576  avio_rl32(pb); /* quality */
577  if (ast->cum_len*ast->scale/ast->rate > 3600) {
578  av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
579  return AVERROR_INVALIDDATA;
580  }
581  ast->sample_size = avio_rl32(pb); /* sample ssize */
582  ast->cum_len *= FFMAX(1, ast->sample_size);
583  av_dlog(s, "%"PRIu32" %"PRIu32" %d\n",
584  ast->rate, ast->scale, ast->sample_size);
585 
586  switch (tag1) {
587  case MKTAG('v', 'i', 'd', 's'):
588  codec_type = AVMEDIA_TYPE_VIDEO;
589 
590  ast->sample_size = 0;
591  break;
592  case MKTAG('a', 'u', 'd', 's'):
593  codec_type = AVMEDIA_TYPE_AUDIO;
594  break;
595  case MKTAG('t', 'x', 't', 's'):
596  codec_type = AVMEDIA_TYPE_SUBTITLE;
597  break;
598  case MKTAG('d', 'a', 't', 's'):
599  codec_type = AVMEDIA_TYPE_DATA;
600  break;
601  default:
602  av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
603  }
604  if (ast->sample_size == 0) {
605  st->duration = st->nb_frames;
606  if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
607  av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
608  st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
609  }
610  }
611  ast->frame_offset = ast->cum_len;
612  avio_skip(pb, size - 12 * 4);
613  break;
614  case MKTAG('s', 't', 'r', 'f'):
615  /* stream header */
616  if (!size)
617  break;
618  if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
619  avio_skip(pb, size);
620  } else {
621  uint64_t cur_pos = avio_tell(pb);
622  unsigned esize;
623  if (cur_pos < list_end)
624  size = FFMIN(size, list_end - cur_pos);
625  st = s->streams[stream_index];
626  switch (codec_type) {
627  case AVMEDIA_TYPE_VIDEO:
628  if (amv_file_format) {
629  st->codec->width = avih_width;
630  st->codec->height = avih_height;
633  avio_skip(pb, size);
634  break;
635  }
636  tag1 = ff_get_bmp_header(pb, st, &esize);
637 
638  if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
639  tag1 == MKTAG('D', 'X', 'S', 'A')) {
641  st->codec->codec_tag = tag1;
643  break;
644  }
645 
646  if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
647  if (esize == size-1 && (esize&1)) {
648  st->codec->extradata_size = esize - 10 * 4;
649  } else
650  st->codec->extradata_size = size - 10 * 4;
652  return AVERROR(ENOMEM);
653  avio_read(pb,
654  st->codec->extradata,
655  st->codec->extradata_size);
656  }
657 
658  // FIXME: check if the encoder really did this correctly
659  if (st->codec->extradata_size & 1)
660  avio_r8(pb);
661 
662  /* Extract palette from extradata if bpp <= 8.
663  * This code assumes that extradata contains only palette.
664  * This is true for all paletted codecs implemented in
665  * FFmpeg. */
666  if (st->codec->extradata_size &&
667  (st->codec->bits_per_coded_sample <= 8)) {
668  int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
669  const uint8_t *pal_src;
670 
671  pal_size = FFMIN(pal_size, st->codec->extradata_size);
672  pal_src = st->codec->extradata +
673  st->codec->extradata_size - pal_size;
674  for (i = 0; i < pal_size / 4; i++)
675  ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
676  ast->has_pal = 1;
677  }
678 
679  print_tag("video", tag1, 0);
680 
682  st->codec->codec_tag = tag1;
684  tag1);
685  /* This is needed to get the pict type which is necessary
686  * for generating correct pts. */
688 
689  if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
690  st->codec->extradata_size < 1U << 30) {
691  st->codec->extradata_size += 9;
692  if ((ret = av_reallocp(&st->codec->extradata,
693  st->codec->extradata_size +
695  st->codec->extradata_size = 0;
696  return ret;
697  } else
698  memcpy(st->codec->extradata + st->codec->extradata_size - 9,
699  "BottomUp", 9);
700  }
701  st->codec->height = FFABS(st->codec->height);
702 
703 // avio_skip(pb, size - 5 * 4);
704  break;
705  case AVMEDIA_TYPE_AUDIO:
706  ret = ff_get_wav_header(pb, st->codec, size);
707  if (ret < 0)
708  return ret;
709  ast->dshow_block_align = st->codec->block_align;
710  if (ast->sample_size && st->codec->block_align &&
711  ast->sample_size != st->codec->block_align) {
712  av_log(s,
714  "sample size (%d) != block align (%d)\n",
715  ast->sample_size,
716  st->codec->block_align);
717  ast->sample_size = st->codec->block_align;
718  }
719  /* 2-aligned
720  * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
721  if (size & 1)
722  avio_skip(pb, 1);
723  /* Force parsing as several audio frames can be in
724  * one packet and timestamps refer to packet start. */
726  /* ADTS header is in extradata, AAC without header must be
727  * stored as exact frames. Parser not needed and it will
728  * fail. */
729  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
730  st->codec->extradata_size)
732  /* AVI files with Xan DPCM audio (wrongly) declare PCM
733  * audio in the header but have Axan as stream_code_tag. */
734  if (st->codec->stream_codec_tag == AV_RL32("Axan")) {
736  st->codec->codec_tag = 0;
737  ast->dshow_block_align = 0;
738  }
739  if (amv_file_format) {
741  ast->dshow_block_align = 0;
742  }
743  if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
744  av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
745  ast->dshow_block_align = 0;
746  }
747  if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
748  st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
749  st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
750  av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
751  ast->sample_size = 0;
752  }
753  break;
756  st->request_probe= 1;
757  avio_skip(pb, size);
758  break;
759  default:
762  st->codec->codec_tag = 0;
763  avio_skip(pb, size);
764  break;
765  }
766  }
767  break;
768  case MKTAG('s', 't', 'r', 'd'):
769  if (stream_index >= (unsigned)s->nb_streams
770  || s->streams[stream_index]->codec->extradata_size
771  || s->streams[stream_index]->codec->codec_tag == MKTAG('H','2','6','4')) {
772  avio_skip(pb, size);
773  } else {
774  uint64_t cur_pos = avio_tell(pb);
775  if (cur_pos < list_end)
776  size = FFMIN(size, list_end - cur_pos);
777  st = s->streams[stream_index];
778 
779  if (size<(1<<30)) {
780  if (ff_alloc_extradata(st->codec, size))
781  return AVERROR(ENOMEM);
782  avio_read(pb, st->codec->extradata, st->codec->extradata_size);
783  }
784 
785  if (st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
786  avio_r8(pb);
787  }
788  break;
789  case MKTAG('i', 'n', 'd', 'x'):
790  i = avio_tell(pb);
791  if (pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
792  avi->use_odml &&
793  read_braindead_odml_indx(s, 0) < 0 &&
795  goto fail;
796  avio_seek(pb, i + size, SEEK_SET);
797  break;
798  case MKTAG('v', 'p', 'r', 'p'):
799  if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
800  AVRational active, active_aspect;
801 
802  st = s->streams[stream_index];
803  avio_rl32(pb);
804  avio_rl32(pb);
805  avio_rl32(pb);
806  avio_rl32(pb);
807  avio_rl32(pb);
808 
809  active_aspect.den = avio_rl16(pb);
810  active_aspect.num = avio_rl16(pb);
811  active.num = avio_rl32(pb);
812  active.den = avio_rl32(pb);
813  avio_rl32(pb); // nbFieldsPerFrame
814 
815  if (active_aspect.num && active_aspect.den &&
816  active.num && active.den) {
817  st->sample_aspect_ratio = av_div_q(active_aspect, active);
818  av_dlog(s, "vprp %d/%d %d/%d\n",
819  active_aspect.num, active_aspect.den,
820  active.num, active.den);
821  }
822  size -= 9 * 4;
823  }
824  avio_skip(pb, size);
825  break;
826  case MKTAG('s', 't', 'r', 'n'):
827  if (s->nb_streams) {
828  ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
829  if (ret < 0)
830  return ret;
831  break;
832  }
833  default:
834  if (size > 1000000) {
835  av_log(s, AV_LOG_ERROR,
836  "Something went wrong during header parsing, "
837  "I will ignore it and try to continue anyway.\n");
839  goto fail;
840  avi->movi_list = avio_tell(pb) - 4;
841  avi->movi_end = avi->fsize;
842  goto end_of_header;
843  }
844  /* skip tag */
845  size += (size & 1);
846  avio_skip(pb, size);
847  break;
848  }
849  }
850 
851 end_of_header:
852  /* check stream number */
853  if (stream_index != s->nb_streams - 1) {
854 
855 fail:
856  return AVERROR_INVALIDDATA;
857  }
858 
859  if (!avi->index_loaded && pb->seekable)
860  avi_load_index(s);
861  avi->index_loaded |= 1;
863 
864  dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
865  if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
866  for (i = 0; i < s->nb_streams; i++) {
867  AVStream *st = s->streams[i];
871  }
872 
873  for (i = 0; i < s->nb_streams; i++) {
874  AVStream *st = s->streams[i];
875  if (st->nb_index_entries)
876  break;
877  }
878  // DV-in-AVI cannot be non-interleaved, if set this must be
879  // a mis-detection.
880  if (avi->dv_demux)
881  avi->non_interleaved = 0;
882  if (i == s->nb_streams && avi->non_interleaved) {
884  "Non-interleaved AVI without index, switching to interleaved\n");
885  avi->non_interleaved = 0;
886  }
887 
888  if (avi->non_interleaved) {
889  av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
890  clean_index(s);
891  }
892 
893  ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
895 
896  return 0;
897 }
898 
900 {
901  if (pkt->size >= 7 &&
902  !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
903  uint8_t desc[256];
904  int score = AVPROBE_SCORE_EXTENSION, ret;
905  AVIStream *ast = st->priv_data;
906  AVInputFormat *sub_demuxer;
907  AVRational time_base;
908  AVIOContext *pb = avio_alloc_context(pkt->data + 7,
909  pkt->size - 7,
910  0, NULL, NULL, NULL, NULL);
911  AVProbeData pd;
912  unsigned int desc_len = avio_rl32(pb);
913 
914  if (desc_len > pb->buf_end - pb->buf_ptr)
915  goto error;
916 
917  ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
918  avio_skip(pb, desc_len - ret);
919  if (*desc)
920  av_dict_set(&st->metadata, "title", desc, 0);
921 
922  avio_rl16(pb); /* flags? */
923  avio_rl32(pb); /* data size */
924 
925  pd = (AVProbeData) { .buf = pb->buf_ptr,
926  .buf_size = pb->buf_end - pb->buf_ptr };
927  if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
928  goto error;
929 
930  if (!(ast->sub_ctx = avformat_alloc_context()))
931  goto error;
932 
933  ast->sub_ctx->pb = pb;
934  if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
935  ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
936  *st->codec = *ast->sub_ctx->streams[0]->codec;
937  ast->sub_ctx->streams[0]->codec->extradata = NULL;
938  time_base = ast->sub_ctx->streams[0]->time_base;
939  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
940  }
941  ast->sub_buffer = pkt->data;
942  memset(pkt, 0, sizeof(*pkt));
943  return 1;
944 
945 error:
946  av_freep(&pb);
947  }
948  return 0;
949 }
950 
952  AVPacket *pkt)
953 {
954  AVIStream *ast, *next_ast = next_st->priv_data;
955  int64_t ts, next_ts, ts_min = INT64_MAX;
956  AVStream *st, *sub_st = NULL;
957  int i;
958 
959  next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
961 
962  for (i = 0; i < s->nb_streams; i++) {
963  st = s->streams[i];
964  ast = st->priv_data;
965  if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
967  if (ts <= next_ts && ts < ts_min) {
968  ts_min = ts;
969  sub_st = st;
970  }
971  }
972  }
973 
974  if (sub_st) {
975  ast = sub_st->priv_data;
976  *pkt = ast->sub_pkt;
977  pkt->stream_index = sub_st->index;
978 
979  if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
980  ast->sub_pkt.data = NULL;
981  }
982  return sub_st;
983 }
984 
985 static int get_stream_idx(unsigned *d)
986 {
987  if (d[0] >= '0' && d[0] <= '9' &&
988  d[1] >= '0' && d[1] <= '9') {
989  return (d[0] - '0') * 10 + (d[1] - '0');
990  } else {
991  return 100; // invalid stream ID
992  }
993 }
994 
995 /**
996  *
997  * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
998  */
999 static int avi_sync(AVFormatContext *s, int exit_early)
1000 {
1001  AVIContext *avi = s->priv_data;
1002  AVIOContext *pb = s->pb;
1003  int n;
1004  unsigned int d[8];
1005  unsigned int size;
1006  int64_t i, sync;
1007 
1008 start_sync:
1009  memset(d, -1, sizeof(d));
1010  for (i = sync = avio_tell(pb); !url_feof(pb); i++) {
1011  int j;
1012 
1013  for (j = 0; j < 7; j++)
1014  d[j] = d[j + 1];
1015  d[7] = avio_r8(pb);
1016 
1017  size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1018 
1019  n = get_stream_idx(d + 2);
1020  av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1021  d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1022  if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1023  continue;
1024 
1025  // parse ix##
1026  if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1027  // parse JUNK
1028  (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1029  (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
1030  avio_skip(pb, size);
1031  goto start_sync;
1032  }
1033 
1034  // parse stray LIST
1035  if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1036  avio_skip(pb, 4);
1037  goto start_sync;
1038  }
1039 
1040  n = avi->dv_demux ? 0 : get_stream_idx(d);
1041 
1042  if (!((i - avi->last_pkt_pos) & 1) &&
1043  get_stream_idx(d + 1) < s->nb_streams)
1044  continue;
1045 
1046  // detect ##ix chunk and skip
1047  if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1048  avio_skip(pb, size);
1049  goto start_sync;
1050  }
1051 
1052  // parse ##dc/##wb
1053  if (n < s->nb_streams) {
1054  AVStream *st;
1055  AVIStream *ast;
1056  st = s->streams[n];
1057  ast = st->priv_data;
1058 
1059  if (!ast) {
1060  av_log(s, AV_LOG_WARNING, "Skiping foreign stream %d packet\n", n);
1061  continue;
1062  }
1063 
1064  if (s->nb_streams >= 2) {
1065  AVStream *st1 = s->streams[1];
1066  AVIStream *ast1 = st1->priv_data;
1067  // workaround for broken small-file-bug402.avi
1068  if ( d[2] == 'w' && d[3] == 'b'
1069  && n == 0
1070  && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
1071  && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
1072  && ast->prefix == 'd'*256+'c'
1073  && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1074  ) {
1075  n = 1;
1076  st = st1;
1077  ast = ast1;
1079  "Invalid stream + prefix combination, assuming audio.\n");
1080  }
1081  }
1082 
1083  if (!avi->dv_demux &&
1084  ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1085  // FIXME: needs a little reordering
1086  (st->discard >= AVDISCARD_NONKEY &&
1087  !(pkt->flags & AV_PKT_FLAG_KEY)) */
1088  || st->discard >= AVDISCARD_ALL)) {
1089  if (!exit_early) {
1090  ast->frame_offset += get_duration(ast, size);
1091  avio_skip(pb, size);
1092  goto start_sync;
1093  }
1094  }
1095 
1096  if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1097  int k = avio_r8(pb);
1098  int last = (k + avio_r8(pb) - 1) & 0xFF;
1099 
1100  avio_rl16(pb); // flags
1101 
1102  // b + (g << 8) + (r << 16);
1103  for (; k <= last; k++)
1104  ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1105 
1106  ast->has_pal = 1;
1107  goto start_sync;
1108  } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1109  d[2] < 128 && d[3] < 128) ||
1110  d[2] * 256 + d[3] == ast->prefix /* ||
1111  (d[2] == 'd' && d[3] == 'c') ||
1112  (d[2] == 'w' && d[3] == 'b') */) {
1113  if (exit_early)
1114  return 0;
1115  if (d[2] * 256 + d[3] == ast->prefix)
1116  ast->prefix_count++;
1117  else {
1118  ast->prefix = d[2] * 256 + d[3];
1119  ast->prefix_count = 0;
1120  }
1121 
1122  avi->stream_index = n;
1123  ast->packet_size = size + 8;
1124  ast->remaining = size;
1125 
1126  if (size || !ast->sample_size) {
1127  uint64_t pos = avio_tell(pb) - 8;
1128  if (!st->index_entries || !st->nb_index_entries ||
1129  st->index_entries[st->nb_index_entries - 1].pos < pos) {
1130  av_add_index_entry(st, pos, ast->frame_offset, size,
1131  0, AVINDEX_KEYFRAME);
1132  }
1133  }
1134  return 0;
1135  }
1136  }
1137  }
1138 
1139  if (pb->error)
1140  return pb->error;
1141  return AVERROR_EOF;
1142 }
1143 
1145 {
1146  AVIContext *avi = s->priv_data;
1147  AVIOContext *pb = s->pb;
1148  int err;
1149 #if FF_API_DESTRUCT_PACKET
1150  void *dstr;
1151 #endif
1152 
1153  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1154  int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1155  if (size >= 0)
1156  return size;
1157  else
1158  goto resync;
1159  }
1160 
1161  if (avi->non_interleaved) {
1162  int best_stream_index = 0;
1163  AVStream *best_st = NULL;
1164  AVIStream *best_ast;
1165  int64_t best_ts = INT64_MAX;
1166  int i;
1167 
1168  for (i = 0; i < s->nb_streams; i++) {
1169  AVStream *st = s->streams[i];
1170  AVIStream *ast = st->priv_data;
1171  int64_t ts = ast->frame_offset;
1172  int64_t last_ts;
1173 
1174  if (!st->nb_index_entries)
1175  continue;
1176 
1177  last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
1178  if (!ast->remaining && ts > last_ts)
1179  continue;
1180 
1181  ts = av_rescale_q(ts, st->time_base,
1182  (AVRational) { FFMAX(1, ast->sample_size),
1183  AV_TIME_BASE });
1184 
1185  av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
1186  st->time_base.num, st->time_base.den, ast->frame_offset);
1187  if (ts < best_ts) {
1188  best_ts = ts;
1189  best_st = st;
1190  best_stream_index = i;
1191  }
1192  }
1193  if (!best_st)
1194  return AVERROR_EOF;
1195 
1196  best_ast = best_st->priv_data;
1197  best_ts = best_ast->frame_offset;
1198  if (best_ast->remaining) {
1199  i = av_index_search_timestamp(best_st,
1200  best_ts,
1201  AVSEEK_FLAG_ANY |
1203  } else {
1204  i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1205  if (i >= 0)
1206  best_ast->frame_offset = best_st->index_entries[i].timestamp;
1207  }
1208 
1209  if (i >= 0) {
1210  int64_t pos = best_st->index_entries[i].pos;
1211  pos += best_ast->packet_size - best_ast->remaining;
1212  if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1213  return AVERROR_EOF;
1214 
1215  av_assert0(best_ast->remaining <= best_ast->packet_size);
1216 
1217  avi->stream_index = best_stream_index;
1218  if (!best_ast->remaining)
1219  best_ast->packet_size =
1220  best_ast->remaining = best_st->index_entries[i].size;
1221  }
1222  else
1223  return AVERROR_EOF;
1224  }
1225 
1226 resync:
1227  if (avi->stream_index >= 0) {
1228  AVStream *st = s->streams[avi->stream_index];
1229  AVIStream *ast = st->priv_data;
1230  int size, err;
1231 
1232  if (get_subtitle_pkt(s, st, pkt))
1233  return 0;
1234 
1235  // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1236  if (ast->sample_size <= 1)
1237  size = INT_MAX;
1238  else if (ast->sample_size < 32)
1239  // arbitrary multiplier to avoid tiny packets for raw PCM data
1240  size = 1024 * ast->sample_size;
1241  else
1242  size = ast->sample_size;
1243 
1244  if (size > ast->remaining)
1245  size = ast->remaining;
1246  avi->last_pkt_pos = avio_tell(pb);
1247  err = av_get_packet(pb, pkt, size);
1248  if (err < 0)
1249  return err;
1250  size = err;
1251 
1252  if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2) {
1253  uint8_t *pal;
1256  AVPALETTE_SIZE);
1257  if (!pal) {
1259  "Failed to allocate data for palette\n");
1260  } else {
1261  memcpy(pal, ast->pal, AVPALETTE_SIZE);
1262  ast->has_pal = 0;
1263  }
1264  }
1265 
1266  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1267  AVBufferRef *avbuf = pkt->buf;
1268 #if FF_API_DESTRUCT_PACKET
1270  dstr = pkt->destruct;
1272 #endif
1273  size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1274  pkt->data, pkt->size, pkt->pos);
1275 #if FF_API_DESTRUCT_PACKET
1277  pkt->destruct = dstr;
1279 #endif
1280  pkt->buf = avbuf;
1282  if (size < 0)
1284  } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1285  !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
1286  ast->frame_offset++;
1287  avi->stream_index = -1;
1288  ast->remaining = 0;
1289  goto resync;
1290  } else {
1291  /* XXX: How to handle B-frames in AVI? */
1292  pkt->dts = ast->frame_offset;
1293 // pkt->dts += ast->start;
1294  if (ast->sample_size)
1295  pkt->dts /= ast->sample_size;
1296  av_dlog(s,
1297  "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
1298  "base:%d st:%d size:%d\n",
1299  pkt->dts,
1300  ast->frame_offset,
1301  ast->scale,
1302  ast->rate,
1303  ast->sample_size,
1304  AV_TIME_BASE,
1305  avi->stream_index,
1306  size);
1307  pkt->stream_index = avi->stream_index;
1308 
1309  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1310  AVIndexEntry *e;
1311  int index;
1313 
1314  index = av_index_search_timestamp(st, ast->frame_offset, 0);
1315  e = &st->index_entries[index];
1316 
1317  if (index >= 0 && e->timestamp == ast->frame_offset) {
1318  if (index == st->nb_index_entries-1) {
1319  int key=1;
1320  int i;
1321  uint32_t state=-1;
1322  for (i=0; i<FFMIN(size,256); i++) {
1323  if (st->codec->codec_id == AV_CODEC_ID_MPEG4) {
1324  if (state == 0x1B6) {
1325  key= !(pkt->data[i]&0xC0);
1326  break;
1327  }
1328  }else
1329  break;
1330  state= (state<<8) + pkt->data[i];
1331  }
1332  if (!key)
1333  e->flags &= ~AVINDEX_KEYFRAME;
1334  }
1335  if (e->flags & AVINDEX_KEYFRAME)
1337  }
1338  } else {
1340  }
1341  ast->frame_offset += get_duration(ast, pkt->size);
1342  }
1343  ast->remaining -= err;
1344  if (!ast->remaining) {
1345  avi->stream_index = -1;
1346  ast->packet_size = 0;
1347  }
1348 
1349  if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1351  goto resync;
1352  }
1353  ast->seek_pos= 0;
1354 
1355  if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
1356  int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1357 
1358  if (avi->dts_max - dts > 2*AV_TIME_BASE) {
1359  avi->non_interleaved= 1;
1360  av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1361  }else if (avi->dts_max < dts)
1362  avi->dts_max = dts;
1363  }
1364 
1365  return 0;
1366  }
1367 
1368  if ((err = avi_sync(s, 0)) < 0)
1369  return err;
1370  goto resync;
1371 }
1372 
1373 /* XXX: We make the implicit supposition that the positions are sorted
1374  * for each stream. */
1375 static int avi_read_idx1(AVFormatContext *s, int size)
1376 {
1377  AVIContext *avi = s->priv_data;
1378  AVIOContext *pb = s->pb;
1379  int nb_index_entries, i;
1380  AVStream *st;
1381  AVIStream *ast;
1382  unsigned int index, tag, flags, pos, len, first_packet = 1;
1383  unsigned last_pos = -1;
1384  unsigned last_idx = -1;
1385  int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1386  int anykey = 0;
1387 
1388  nb_index_entries = size / 16;
1389  if (nb_index_entries <= 0)
1390  return AVERROR_INVALIDDATA;
1391 
1392  idx1_pos = avio_tell(pb);
1393  avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1394  if (avi_sync(s, 1) == 0)
1395  first_packet_pos = avio_tell(pb) - 8;
1396  avi->stream_index = -1;
1397  avio_seek(pb, idx1_pos, SEEK_SET);
1398 
1399  if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")) {
1400  first_packet_pos = 0;
1401  data_offset = avi->movi_list;
1402  }
1403 
1404  /* Read the entries and sort them in each stream component. */
1405  for (i = 0; i < nb_index_entries; i++) {
1406  if (url_feof(pb))
1407  return -1;
1408 
1409  tag = avio_rl32(pb);
1410  flags = avio_rl32(pb);
1411  pos = avio_rl32(pb);
1412  len = avio_rl32(pb);
1413  av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
1414  i, tag, flags, pos, len);
1415 
1416  index = ((tag & 0xff) - '0') * 10;
1417  index += (tag >> 8 & 0xff) - '0';
1418  if (index >= s->nb_streams)
1419  continue;
1420  st = s->streams[index];
1421  ast = st->priv_data;
1422 
1423  if (first_packet && first_packet_pos) {
1424  data_offset = first_packet_pos - pos;
1425  first_packet = 0;
1426  }
1427  pos += data_offset;
1428 
1429  av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1430 
1431  // even if we have only a single stream, we should
1432  // switch to non-interleaved to get correct timestamps
1433  if (last_pos == pos)
1434  avi->non_interleaved = 1;
1435  if (last_idx != pos && len) {
1436  av_add_index_entry(st, pos, ast->cum_len, len, 0,
1437  (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1438  last_idx= pos;
1439  }
1440  ast->cum_len += get_duration(ast, len);
1441  last_pos = pos;
1442  anykey |= flags&AVIIF_INDEX;
1443  }
1444  if (!anykey) {
1445  for (index = 0; index < s->nb_streams; index++) {
1446  st = s->streams[index];
1447  if (st->nb_index_entries)
1449  }
1450  }
1451  return 0;
1452 }
1453 
1455 {
1456  int i;
1457  int64_t last_start = 0;
1458  int64_t first_end = INT64_MAX;
1459  int64_t oldpos = avio_tell(s->pb);
1460  int *idx;
1461  int64_t min_pos, pos;
1462 
1463  for (i = 0; i < s->nb_streams; i++) {
1464  AVStream *st = s->streams[i];
1465  int n = st->nb_index_entries;
1466  unsigned int size;
1467 
1468  if (n <= 0)
1469  continue;
1470 
1471  if (n >= 2) {
1472  int64_t pos = st->index_entries[0].pos;
1473  avio_seek(s->pb, pos + 4, SEEK_SET);
1474  size = avio_rl32(s->pb);
1475  if (pos + size > st->index_entries[1].pos)
1476  last_start = INT64_MAX;
1477  }
1478 
1479  if (st->index_entries[0].pos > last_start)
1480  last_start = st->index_entries[0].pos;
1481  if (st->index_entries[n - 1].pos < first_end)
1482  first_end = st->index_entries[n - 1].pos;
1483  }
1484  avio_seek(s->pb, oldpos, SEEK_SET);
1485  if (last_start > first_end)
1486  return 1;
1487  idx= av_calloc(s->nb_streams, sizeof(*idx));
1488  if (!idx)
1489  return 0;
1490  for (min_pos=pos=0; min_pos!=INT64_MAX; pos= min_pos+1LU) {
1491  int64_t max_dts = INT64_MIN/2, min_dts= INT64_MAX/2;
1492  min_pos = INT64_MAX;
1493 
1494  for (i=0; i<s->nb_streams; i++) {
1495  AVStream *st = s->streams[i];
1496  AVIStream *ast = st->priv_data;
1497  int n= st->nb_index_entries;
1498  while (idx[i]<n && st->index_entries[idx[i]].pos < pos)
1499  idx[i]++;
1500  if (idx[i] < n) {
1501  min_dts = FFMIN(min_dts, av_rescale_q(st->index_entries[idx[i]].timestamp/FFMAX(ast->sample_size, 1), st->time_base, AV_TIME_BASE_Q));
1502  min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
1503  }
1504  if (idx[i])
1505  max_dts = FFMAX(max_dts, av_rescale_q(st->index_entries[idx[i]-1].timestamp/FFMAX(ast->sample_size, 1), st->time_base, AV_TIME_BASE_Q));
1506  }
1507  if (max_dts - min_dts > 2*AV_TIME_BASE) {
1508  av_free(idx);
1509  return 1;
1510  }
1511  }
1512  av_free(idx);
1513  return 0;
1514 }
1515 
1517 {
1518  AVIContext *avi = s->priv_data;
1519  AVIOContext *pb = s->pb;
1520  uint32_t tag, size;
1521  int64_t pos = avio_tell(pb);
1522  int64_t next;
1523  int ret = -1;
1524 
1525  if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1526  goto the_end; // maybe truncated file
1527  av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1528  for (;;) {
1529  tag = avio_rl32(pb);
1530  size = avio_rl32(pb);
1531  if (url_feof(pb))
1532  break;
1533  next = avio_tell(pb) + size + (size & 1);
1534 
1535  av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
1536  tag & 0xff,
1537  (tag >> 8) & 0xff,
1538  (tag >> 16) & 0xff,
1539  (tag >> 24) & 0xff,
1540  size);
1541 
1542  if (tag == MKTAG('i', 'd', 'x', '1') &&
1543  avi_read_idx1(s, size) >= 0) {
1544  avi->index_loaded=2;
1545  ret = 0;
1546  }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1547  uint32_t tag1 = avio_rl32(pb);
1548 
1549  if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1550  ff_read_riff_info(s, size - 4);
1551  }else if (!ret)
1552  break;
1553 
1554  if (avio_seek(pb, next, SEEK_SET) < 0)
1555  break; // something is wrong here
1556  }
1557 
1558 the_end:
1559  avio_seek(pb, pos, SEEK_SET);
1560  return ret;
1561 }
1562 
1563 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1564 {
1565  AVIStream *ast2 = st2->priv_data;
1566  int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1567  av_free_packet(&ast2->sub_pkt);
1568  if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1569  avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1570  ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
1571 }
1572 
1573 static int avi_read_seek(AVFormatContext *s, int stream_index,
1574  int64_t timestamp, int flags)
1575 {
1576  AVIContext *avi = s->priv_data;
1577  AVStream *st;
1578  int i, index;
1579  int64_t pos, pos_min;
1580  AVIStream *ast;
1581 
1582  /* Does not matter which stream is requested dv in avi has the
1583  * stream information in the first video stream.
1584  */
1585  if (avi->dv_demux)
1586  stream_index = 0;
1587 
1588  if (!avi->index_loaded) {
1589  /* we only load the index on demand */
1590  avi_load_index(s);
1591  avi->index_loaded |= 1;
1592  }
1593  av_assert0(stream_index >= 0);
1594 
1595  st = s->streams[stream_index];
1596  ast = st->priv_data;
1597  index = av_index_search_timestamp(st,
1598  timestamp * FFMAX(ast->sample_size, 1),
1599  flags);
1600  if (index < 0) {
1601  if (st->nb_index_entries > 0)
1602  av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1603  timestamp * FFMAX(ast->sample_size, 1),
1604  st->index_entries[0].timestamp,
1606  return AVERROR_INVALIDDATA;
1607  }
1608 
1609  /* find the position */
1610  pos = st->index_entries[index].pos;
1611  timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
1612 
1613  av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
1614  timestamp, index, st->index_entries[index].timestamp);
1615 
1616  if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1617  /* One and only one real stream for DV in AVI, and it has video */
1618  /* offsets. Calling with other stream indexes should have failed */
1619  /* the av_index_search_timestamp call above. */
1620 
1621  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1622  return -1;
1623 
1624  /* Feed the DV video stream version of the timestamp to the */
1625  /* DV demux so it can synthesize correct timestamps. */
1626  ff_dv_offset_reset(avi->dv_demux, timestamp);
1627 
1628  avi->stream_index = -1;
1629  return 0;
1630  }
1631 
1632  pos_min = pos;
1633  for (i = 0; i < s->nb_streams; i++) {
1634  AVStream *st2 = s->streams[i];
1635  AVIStream *ast2 = st2->priv_data;
1636 
1637  ast2->packet_size =
1638  ast2->remaining = 0;
1639 
1640  if (ast2->sub_ctx) {
1641  seek_subtitle(st, st2, timestamp);
1642  continue;
1643  }
1644 
1645  if (st2->nb_index_entries <= 0)
1646  continue;
1647 
1648 // av_assert1(st2->codec->block_align);
1649  av_assert0((int64_t)st2->time_base.num * ast2->rate ==
1650  (int64_t)st2->time_base.den * ast2->scale);
1651  index = av_index_search_timestamp(st2,
1652  av_rescale_q(timestamp,
1653  st->time_base,
1654  st2->time_base) *
1655  FFMAX(ast2->sample_size, 1),
1656  flags |
1659  if (index < 0)
1660  index = 0;
1661  ast2->seek_pos = st2->index_entries[index].pos;
1662  pos_min = FFMIN(pos_min,ast2->seek_pos);
1663  }
1664  for (i = 0; i < s->nb_streams; i++) {
1665  AVStream *st2 = s->streams[i];
1666  AVIStream *ast2 = st2->priv_data;
1667 
1668  if (ast2->sub_ctx || st2->nb_index_entries <= 0)
1669  continue;
1670 
1671  index = av_index_search_timestamp(
1672  st2,
1673  av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1675  if (index < 0)
1676  index = 0;
1677  while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
1678  index--;
1679  ast2->frame_offset = st2->index_entries[index].timestamp;
1680  }
1681 
1682  /* do the seek */
1683  if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1684  av_log(s, AV_LOG_ERROR, "Seek failed\n");
1685  return -1;
1686  }
1687  avi->stream_index = -1;
1688  avi->dts_max = INT_MIN;
1689  return 0;
1690 }
1691 
1693 {
1694  int i;
1695  AVIContext *avi = s->priv_data;
1696 
1697  for (i = 0; i < s->nb_streams; i++) {
1698  AVStream *st = s->streams[i];
1699  AVIStream *ast = st->priv_data;
1700  if (ast) {
1701  if (ast->sub_ctx) {
1702  av_freep(&ast->sub_ctx->pb);
1704  }
1705  av_free(ast->sub_buffer);
1706  av_free_packet(&ast->sub_pkt);
1707  }
1708  }
1709 
1710  av_free(avi->dv_demux);
1711 
1712  return 0;
1713 }
1714 
1715 static int avi_probe(AVProbeData *p)
1716 {
1717  int i;
1718 
1719  /* check file header */
1720  for (i = 0; avi_headers[i][0]; i++)
1721  if (!memcmp(p->buf, avi_headers[i], 4) &&
1722  !memcmp(p->buf + 8, avi_headers[i] + 4, 4))
1723  return AVPROBE_SCORE_MAX;
1724 
1725  return 0;
1726 }
1727 
1729  .name = "avi",
1730  .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
1731  .priv_data_size = sizeof(AVIContext),
1732  .read_probe = avi_probe,
1737  .priv_class = &demuxer_class,
1738 };
const char * name
Definition: avisynth_c.h:675
int ff_read_riff_info(AVFormatContext *s, int64_t size)
Definition: riffdec.c:183
static AVStream * get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, AVPacket *pkt)
Definition: avidec.c:951
unsigned int stream_codec_tag
fourcc from the AVI stream header (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + ...
Definition: avcodec.h:1180
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:249
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
uint32_t pal[256]
Definition: avidec.c:50
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
AVOption.
Definition: opt.h:253
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:1675
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2556
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: utils.c:270
AVFormatContext * sub_ctx
Definition: avidec.c:55
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
#define MAX_ODML_DEPTH
Definition: avidec.c:77
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1092
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:487
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:3922
int64_t pos
Definition: avformat.h:609
int64_t last_pkt_pos
Definition: avidec.c:69
uint32_t rate
Definition: avidec.c:43
#define AV_RL16(x)
Definition: intreadwrite.h:245
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:261
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:733
int dshow_block_align
Definition: avidec.c:52
int num
numerator
Definition: rational.h:44
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:593
int index
stream index in AVFormatContext
Definition: avformat.h:668
int size
Definition: avcodec.h:1064
static int sync(AVFormatContext *s, uint8_t *header)
Read input until we find the next ident.
Definition: lxfdec.c:84
int is_odml
Definition: avidec.c:71
enum AVMediaType codec_type
Definition: rtp.c:37
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int64_t movi_end
Definition: avidec.c:65
uint32_t scale
Definition: avidec.c:42
static int get_duration(AVIStream *ast, int len)
Definition: avidec.c:121
#define AVIIF_INDEX
Definition: avi.h:36
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:624
discard all
Definition: avcodec.h:618
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1910
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:471
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
static const AVMetadataConv avi_metadata_conv[]
Definition: avidec.c:105
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1046
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVDictionary * metadata
Definition: avformat.h:735
Format I/O context.
Definition: avformat.h:968
int stream_index
Definition: avidec.c:73
static int get_stream_idx(unsigned *d)
Definition: avidec.c:985
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
uint8_t
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
static int avi_read_close(AVFormatContext *s)
Definition: avidec.c:1692
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:1088
int64_t riff_end
Definition: avidec.c:64
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
int use_odml
Definition: avidec.c:76
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
enum AVStreamParseType need_parsing
Definition: avformat.h:812
int id
Format-specific stream ID.
Definition: avformat.h:674
int64_t movi_list
Definition: avidec.c:68
static void handler(vbi_event *ev, void *user_data)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
static void clean_index(AVFormatContext *s)
Definition: avidec.c:264
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:325
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1047
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:183
uint32_t tag
Definition: movenc.c:961
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:823
void * priv_data
Definition: avformat.h:687
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
static int64_t duration
Definition: ffplay.c:306
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
const OptionDef options[]
Definition: ffserver.c:4682
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:200
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
int packet_size
Definition: avidec.c:40
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
#define U(x)
Definition: vp56_arith.h:37
#define AVIF_MUSTUSEINDEX
Definition: avi.h:25
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
Read BITMAPINFOHEADER structure and set AVStream codec width, height and bits_per_encoded_sample fiel...
Definition: riffdec.c:165
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1718
void * priv_data
Format private data.
Definition: avformat.h:988
int remaining
Definition: avidec.c:39
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
Definition: riffdec.c:88
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:610
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
AVRational av_div_q(AVRational b, AVRational c) av_const
Divide one rational by another.
Definition: rational.c:87
static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
Definition: avidec.c:156
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:398
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:577
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
Definition: avidec.c:292
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:85
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:298
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
goto fail
Definition: avfilter.c:963
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
int prefix_count
Definition: avidec.c:49
int non_interleaved
Definition: avidec.c:72
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
Opaque data information usually continuous.
Definition: avcodec.h:2233
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avcodec.h:2284
void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
Definition: dv.c:437
full parsing and repack
Definition: avformat.h:599
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:212
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
ret
Definition: avfilter.c:961
int64_t cum_len
Definition: avidec.c:47
int width
picture width / height.
Definition: avcodec.h:1314
static int avi_read_header(AVFormatContext *s)
Definition: avidec.c:379
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:1840
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:341
AVStream ** streams
Definition: avformat.h:1016
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
char * value
Definition: dict.h:82
#define AVPALETTE_SIZE
Definition: avcodec.h:4499
static const char months[12][4]
Definition: avidec.c:315
#define FFMIN(a, b)
Definition: avcodec.h:925
static char buffer[20]
Definition: seek-test.c:31
static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
Definition: avidec.c:1563
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:84
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
int n
Definition: avisynth_c.h:588
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:649
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
static const char avi_headers[][8]
Definition: avidec.c:96
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avidec.c:1144
int prefix
Definition: avidec.c:48
static int read_gab2_sub(AVStream *st, AVPacket *pkt)
Definition: avidec.c:899
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
enum AVMediaType codec_type
Definition: avcodec.h:1154
static int resync(AVIOContext *pb)
Definition: gifdec.c:80
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:365
#define FFMAX(a, b)
Definition: avcodec.h:923
#define AVFMT_FLAG_SORT_DTS
try to interleave outputted packets by dts (using this flag can slow demuxing down) ...
Definition: avformat.h:1057
enum AVCodecID codec_id
Definition: avcodec.h:1157
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avcodec.h:2290
int index_loaded
Definition: avidec.c:70
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.
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2480
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional FF_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2690
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition: avformat.h:767
int extradata_size
Definition: avcodec.h:1255
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
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:109
int nb_index_entries
Definition: avformat.h:825
double value
Definition: eval.c:83
Describe the class of an AVClass context structure.
Definition: log.h:50
#define MKTAG(a, b, c, d)
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1838
static int avi_read_idx1(AVFormatContext *s, int size)
Definition: avidec.c:1375
int index
Definition: gxfenc.c:89
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:601
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data
Definition: avcodec.h:1063
AVDictionary * metadata
Definition: avformat.h:1128
discard useless packets like 0 size packets in avi
Definition: avcodec.h:614
struct AVStream::@29 * info
#define snprintf
Definition: snprintf.h:34
static int avi_probe(AVProbeData *p)
Definition: avidec.c:1715
#define AVINDEX_KEYFRAME
Definition: avformat.h:616
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
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:2083
#define FFABS(a)
Definition: avcodec.h:920
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:429
int64_t seek_pos
Definition: avidec.c:59
static uint32_t state
Definition: trasher.c:27
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
static int avi_load_index(AVFormatContext *s)
Definition: avidec.c:1516
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:342
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:284
AVInputFormat ff_avi_demuxer
Definition: avidec.c:1728
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:720
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int64_t io_fsize
Definition: avidec.c:67
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
A reference to a data buffer.
Definition: buffer.h:81
static const AVClass demuxer_class
Definition: avidec.c:87
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
Definition: avidec.c:337
static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
Definition: avidec.c:318
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
AVPacket sub_pkt
Definition: avidec.c:56
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:713
#define CONFIG_DV_DEMUXER
Definition: config.h:893
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1150
Main libavformat public API header.
static int get_riff(AVFormatContext *s, AVIOContext *pb)
Definition: avidec.c:131
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:106
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:722
int den
denominator
Definition: rational.h:45
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:348
int sample_size
Definition: avidec.c:44
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:3309
#define AVERROR_INVALIDDATA
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
#define AV_RL32(x)
Definition: intreadwrite.h:275
int len
int has_pal
Definition: avidec.c:51
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:601
static int guess_ni_flag(AVFormatContext *s)
Definition: avidec.c:1454
#define print_tag(str, tag, size)
Definition: avidec.c:113
int64_t frame_offset
Definition: avidec.c:37
int64_t dts_max
Definition: avidec.c:78
#define AVERROR(e)
int64_t fsize
Definition: avidec.c:66
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: avidec.c:1573
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1033
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:703
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:726
int odml_depth
Definition: avidec.c:75
int request_probe
stream probing state -1 -&gt; probing finished 0 -&gt; no probing requested rest -&gt; perform probing with re...
Definition: avformat.h:858
This structure stores compressed data.
Definition: avcodec.h:1040
static int avi_sync(AVFormatContext *s, int exit_early)
Definition: avidec.c:999
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
uint8_t * sub_buffer
Definition: avidec.c:57
Only parse headers, do not repack.
Definition: avformat.h:600
DVDemuxContext * dv_demux
Definition: avidec.c:74