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