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"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mathematics.h"
77 #define MAX_ODML_DEPTH 1000
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',
' ' },
113 #define print_tag(str, tag, size) \
114 av_dlog(NULL, "%s: tag=%c%c%c%c size=0x%x\n", \
117 (tag >> 16) & 0xff, \
118 (tag >> 24) & 0xff, \
149 if (header[7] == 0x19)
151 "This file has been generated by a totally broken muxer.\n");
161 int index_sub_type =
avio_r8(pb);
166 int stream_id = ((chunk_id & 0xFF) -
'0') * 10 +
167 ((chunk_id >> 8 & 0xFF) -
'0');
171 int64_t last_pos = -1;
172 int64_t filesize = avi->
fsize;
175 "longs_pre_entry:%d index_type:%d entries_in_use:%d "
176 "chunk_id:%X base:%16"PRIX64
"\n",
183 if (stream_id >= s->
nb_streams || stream_id < 0)
193 if (index_type && longs_pre_entry != 2)
198 if (filesize > 0 && base >= filesize) {
200 if (base >> 32 == (base & 0xFFFFFFFF) &&
201 (base & 0xFFFFFFFF) < filesize &&
202 filesize <= 0xFFFFFFFF)
208 for (i = 0; i < entries_in_use; i++) {
221 if (last_pos == pos || pos == base - 8)
246 if (
avio_seek(pb, offset + 8, SEEK_SET) < 0)
274 int64_t pos,
size, ts;
286 for (j = 0; j <
size; j += max)
301 if (size == UINT_MAX)
315 static const char months[12][4] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
316 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
320 char month[4], time[9],
buffer[64];
323 if (sscanf(date,
"%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
324 month, &day, time, &year) == 4) {
325 for (i = 0; i < 12; i++)
327 snprintf(buffer,
sizeof(buffer),
"%.4d-%.2d-%.2d %s",
328 year, i + 1, day, time);
331 }
else if (date[4] ==
'/' && date[7] ==
'/') {
332 date[4] = date[7] =
'-';
343 case MKTAG(
'n',
'c',
't',
'g'):
349 const char *
name = NULL;
352 FFMIN(size,
sizeof(buffer) - 1));
361 name =
"creation_time";
362 if (buffer[4] ==
':' && buffer[7] ==
':')
363 buffer[4] = buffer[7] =
'-';
389 int avih_width = 0, avih_height = 0;
390 int amv_file_format = 0;
391 uint64_t list_end = 0;
420 case MKTAG(
'L',
'I',
'S',
'T'):
427 if (tag1 ==
MKTAG(
'm',
'o',
'v',
'i')) {
435 }
else if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
437 else if (tag1 ==
MKTAG(
'n',
'c',
'd',
't'))
441 case MKTAG(
'I',
'D',
'I',
'T'):
443 unsigned char date[64] = { 0 };
450 case MKTAG(
'd',
'm',
'l',
'h'):
454 case MKTAG(
'a',
'm',
'v',
'h'):
456 case MKTAG(
'a',
'v',
'i',
'h'):
472 case MKTAG(
's',
't',
'r',
'h'):
478 if (tag1 ==
MKTAG(
'p',
'a',
'd',
's')) {
487 st->
id = stream_index;
494 tag1 = stream_index ?
MKTAG(
'a',
'u',
'd',
's')
495 :
MKTAG(
'v',
'i',
'd',
's');
499 if (tag1 ==
MKTAG(
'i',
'a',
'v',
's') ||
500 tag1 ==
MKTAG(
'i',
'v',
'a',
's')) {
508 if (handler !=
MKTAG(
'd',
'v',
's',
'd') &&
509 handler !=
MKTAG(
'd',
'v',
'h',
'd') &&
510 handler !=
MKTAG(
'd',
'v',
's',
'l'))
534 if (ast->
scale > 0 && ast->
rate > 0 && dv_dur > 0) {
557 "scale/rate is %u/%u which is invalid. "
558 "(This file has been generated by broken software.)\n",
563 ast->
scale = frame_period;
583 av_dlog(s,
"%"PRIu32
" %"PRIu32
" %d\n",
587 case MKTAG(
'v',
'i',
'd',
's'):
592 case MKTAG(
'a',
'u',
'd',
's'):
595 case MKTAG(
't',
'x',
't',
's'):
598 case MKTAG(
'd',
'a',
't',
's'):
614 case MKTAG(
's',
't',
'r',
'f'):
623 if (cur_pos < list_end)
624 size =
FFMIN(size, list_end - cur_pos);
626 switch (codec_type) {
628 if (amv_file_format) {
638 if (tag1 ==
MKTAG(
'D',
'X',
'S',
'B') ||
639 tag1 ==
MKTAG(
'D',
'X',
'S',
'A')) {
646 if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
647 if (esize == size-1 && (esize&1)) {
674 for (i = 0; i < pal_size / 4; i++)
675 ast->
pal[i] = 0xFFU<<24 |
AV_RL32(pal_src+4*i);
714 "sample size (%d) != block align (%d)\n",
739 if (amv_file_format) {
768 case MKTAG(
's',
't',
'r',
'd'):
775 if (cur_pos < list_end)
776 size =
FFMIN(size, list_end - cur_pos);
789 case MKTAG(
'i',
'n',
'd',
'x'):
798 case MKTAG(
'v',
'p',
'r',
'p'):
799 if (stream_index < (
unsigned)s->
nb_streams && size > 9 * 4) {
815 if (active_aspect.
num && active_aspect.
den &&
816 active.
num && active.
den) {
818 av_dlog(s,
"vprp %d/%d %d/%d\n",
819 active_aspect.
num, active_aspect.
den,
826 case MKTAG(
's',
't',
'r',
'n'):
834 if (size > 1000000) {
836 "Something went wrong during header parsing, "
837 "I will ignore it and try to continue anyway.\n");
865 if (dict_entry && !strcmp(dict_entry->
value,
"PotEncoder"))
884 "Non-interleaved AVI without index, switching to interleaved\n");
901 if (pkt->
size >= 7 &&
910 0, NULL, NULL, NULL, NULL);
942 memset(pkt, 0,
sizeof(*pkt));
955 int64_t ts, next_ts, ts_min = INT64_MAX;
967 if (ts <= next_ts && ts < ts_min) {
987 if (d[0] >=
'0' && d[0] <=
'9' &&
988 d[1] >=
'0' && d[1] <=
'9') {
989 return (d[0] -
'0') * 10 + (d[1] -
'0');
1009 memset(d, -1,
sizeof(d));
1013 for (j = 0; j < 7; j++)
1017 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
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)
1026 if ((d[0] ==
'i' && d[1] ==
'x' && n < s->nb_streams) ||
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')) {
1035 if (d[0] ==
'L' && d[1] ==
'I' && d[2] ==
'S' && d[3] ==
'T') {
1047 if (d[2] ==
'i' && d[3] ==
'x' && n < s->nb_streams) {
1053 if (n < s->nb_streams) {
1068 if ( d[2] ==
'w' && d[3] ==
'b'
1072 && ast->
prefix ==
'd'*256+
'c'
1079 "Invalid stream + prefix combination, assuming audio.\n");
1096 if (d[2] ==
'p' && d[3] ==
'c' && size <= 4 * 256 + 4) {
1098 int last = (k +
avio_r8(pb) - 1) & 0xFF;
1103 for (; k <= last; k++)
1109 d[2] < 128 && d[3] < 128) ||
1110 d[2] * 256 + d[3] == ast->
prefix
1115 if (d[2] * 256 + d[3] == ast->
prefix)
1118 ast->
prefix = d[2] * 256 + d[3];
1149 #if FF_API_DESTRUCT_PACKET
1162 int best_stream_index = 0;
1165 int64_t best_ts = INT64_MAX;
1185 av_dlog(s,
"%"PRId64
" %d/%d %"PRId64
"\n", ts,
1186 st->time_base.num, st->time_base.den, ast->frame_offset);
1190 best_stream_index = i;
1196 best_ast = best_st->priv_data;
1197 best_ts = best_ast->frame_offset;
1198 if (best_ast->remaining) {
1206 best_ast->frame_offset = best_st->index_entries[i].timestamp;
1210 int64_t pos = best_st->index_entries[i].pos;
1211 pos += best_ast->packet_size - best_ast->remaining;
1215 av_assert0(best_ast->remaining <= best_ast->packet_size);
1218 if (!best_ast->remaining)
1219 best_ast->packet_size =
1220 best_ast->remaining = best_st->index_entries[i].size;
1227 if (avi->stream_index >= 0) {
1228 AVStream *st =
s->streams[avi->stream_index];
1259 "Failed to allocate data for palette\n");
1268 #if FF_API_DESTRUCT_PACKET
1275 #if FF_API_DESTRUCT_PACKET
1287 avi->stream_index = -1;
1297 "dts:%"PRId64
" offset:%"PRId64
" %d/%d smpl_siz:%d "
1298 "base:%d st:%d size:%d\n",
1322 for (i=0; i<
FFMIN(size,256); i++) {
1324 if (state == 0x1B6) {
1330 state= (state<<8) +
pkt->
data[i];
1345 avi->stream_index = -1;
1355 if (!avi->non_interleaved && st->
nb_index_entries>1 && avi->index_loaded>1) {
1359 avi->non_interleaved= 1;
1361 }
else if (avi->dts_max < dts)
1379 int nb_index_entries, i;
1383 unsigned last_pos = -1;
1384 unsigned last_idx = -1;
1385 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1388 nb_index_entries = size / 16;
1389 if (nb_index_entries <= 0)
1400 first_packet_pos = 0;
1405 for (i = 0; i < nb_index_entries; i++) {
1413 av_dlog(s,
"%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
1414 i, tag, flags, pos, len);
1416 index = ((tag & 0xff) -
'0') * 10;
1417 index += (tag >> 8 & 0xff) -
'0';
1423 if (first_packet && first_packet_pos) {
1424 data_offset = first_packet_pos - pos;
1433 if (last_pos == pos)
1435 if (last_idx != pos && len) {
1445 for (index = 0; index < s->
nb_streams; index++) {
1457 int64_t last_start = 0;
1458 int64_t first_end = INT64_MAX;
1461 int64_t min_pos, pos;
1476 last_start = INT64_MAX;
1485 if (last_start > first_end)
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;
1533 next =
avio_tell(pb) + size + (size & 1);
1535 av_dlog(s,
"tag=%c%c%c%c size=0x%x\n",
1542 if (tag ==
MKTAG(
'i',
'd',
'x',
'1') &&
1546 }
else if (tag ==
MKTAG(
'L',
'I',
'S',
'T')) {
1549 if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
1574 int64_t timestamp,
int flags)
1579 int64_t pos, pos_min;
1595 st = s->
streams[stream_index];
1602 av_log(s,
AV_LOG_DEBUG,
"Failed to find timestamp %"PRId64
" in index %"PRId64
" .. %"PRId64
"\n",
1613 av_dlog(s,
"XX %"PRId64
" %d %"PRId64
"\n",
1737 .priv_class = &demuxer_class,
int ff_read_riff_info(AVFormatContext *s, int64_t size)
static AVStream * get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, AVPacket *pkt)
unsigned int stream_codec_tag
fourcc from the AVI stream header (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + ...
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...
void av_free_packet(AVPacket *pkt)
Free a packet.
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.
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
AVFormatContext * sub_ctx
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
int64_t pos
byte position in stream, -1 if unknown
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
AVCodecContext * codec
Codec context associated with this stream.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int index
stream index in AVFormatContext
static int sync(AVFormatContext *s, uint8_t *header)
Read input until we find the next ident.
enum AVMediaType codec_type
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...
static int get_duration(AVIStream *ast, int len)
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)
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static const AVMetadataConv avi_metadata_conv[]
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int get_stream_idx(unsigned *d)
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int avi_read_close(AVFormatContext *s)
attribute_deprecated void(* destruct)(struct AVPacket *)
static const uint8_t offset[511][2]
static av_cold int end(AVCodecContext *avctx)
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
enum AVStreamParseType need_parsing
int id
Format-specific stream ID.
static void handler(vbi_event *ev, void *user_data)
static void clean_index(AVFormatContext *s)
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
static av_cold int read_close(AVFormatContext *ctx)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
const OptionDef options[]
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
#define AVIF_MUSTUSEINDEX
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...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
void * priv_data
Format private data.
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVRational av_div_q(AVRational b, AVRational c) av_const
Divide one rational by another.
static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
int flags
A combination of AV_PKT_FLAG values.
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...
unsigned int nb_streams
A list of all streams in the file.
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Opaque data information usually continuous.
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
#define AV_TIME_BASE
Internal time base represented as integer.
const AVCodecTag ff_codec_bmp_tags[]
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
int width
picture width / height.
static int avi_read_header(AVFormatContext *s)
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 ...
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static const char months[12][4]
static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
unsigned char * buf_ptr
Current position in the buffer.
preferred ID for MPEG-1/2 video decoding
static int read_header(FFV1Context *f)
static const char avi_headers[][8]
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
static int read_gab2_sub(AVStream *st, AVPacket *pkt)
#define AV_LOG_INFO
Standard information.
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
#define AV_EF_EXPLODE
abort decoding on minor error detection
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
AVIOContext * pb
I/O context.
double(* duration_error)[2][MAX_STD_TIMEBASES]
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
#define MKTAG(a, b, c, d)
static int avi_read_idx1(AVFormatContext *s, int size)
rational number numerator/denominator
discard useless packets like 0 size packets in avi
struct AVStream::@29 * info
static int avi_probe(AVProbeData *p)
int error
contains the error code or 0 if no error happened
This structure contains the data a format has to probe a file.
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.
const AVMetadataConv ff_riff_info_conv[]
static int avi_load_index(AVFormatContext *s)
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
AVInputFormat ff_avi_demuxer
int64_t duration
Decoding: duration of the stream, in stream time base.
A reference to a data buffer.
static const AVClass demuxer_class
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
#define FF_DISABLE_DEPRECATION_WARNINGS
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
#define CONFIG_DV_DEMUXER
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
static int get_riff(AVFormatContext *s, AVIOContext *pb)
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
int64_t nb_frames
number of frames in this stream if known or 0
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
#define AVERROR_INVALIDDATA
#define FF_ENABLE_DEPRECATION_WARNINGS
static int guess_ni_flag(AVFormatContext *s)
#define print_tag(str, tag, size)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
This structure stores compressed data.
static int avi_sync(AVFormatContext *s, int exit_early)
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 ...
DVDemuxContext * dv_demux