39 for (
unsigned i = 0;
i <
s->nb_streams;
i++) {
52 AVStream *
const st =
s->streams[stream_index];
54 unsigned int max_entries =
s->max_index_size /
sizeof(
AVIndexEntry);
65 int *nb_index_entries,
66 unsigned int *index_entries_allocated_size,
73 if ((
unsigned) *nb_index_entries + 1 >= UINT_MAX /
sizeof(
AVIndexEntry))
86 index_entries_allocated_size,
87 (*nb_index_entries + 1) *
92 *index_entries = entries;
97 index = (*nb_index_entries)++;
101 ie = &entries[
index];
107 (*nb_index_entries)++;
142 if (
b && entries[
b - 1].timestamp < wanted_timestamp)
151 if (m ==
b && entries[m].timestamp >= wanted_timestamp) {
158 if (timestamp >= wanted_timestamp)
160 if (timestamp <= wanted_timestamp)
166 while (m >= 0 && m < nb_entries &&
187 "Protocol name not provided, cannot determine if input is local or "
188 "a network protocol, buffers and access patterns cannot be configured "
189 "optimally without knowing the protocol\n");
192 if (proto && !(strcmp(proto,
"file") && strcmp(proto,
"pipe") && strcmp(proto,
"cache")))
195 for (
unsigned ist1 = 0; ist1 <
s->nb_streams; ist1++) {
198 for (
unsigned ist2 = 0; ist2 <
s->nb_streams; ist2++) {
209 if (e1->
size < (1 << 23))
216 if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance)
219 if (cur_delta < (1 << 23))
220 pos_delta =
FFMAX(pos_delta, cur_delta);
230 if (
s->pb->buffer_size < pos_delta) {
239 ctx->short_seek_threshold =
FFMAX(
ctx->short_seek_threshold, pos_delta/2);
249 wanted_timestamp,
flags);
273 wanted_timestamp,
flags);
285 if (stream_index >= 0)
294 int64_t pos_min = 0, pos_max = 0,
pos, pos_limit;
301 if (stream_index < 0)
310 st =
s->streams[stream_index];
341 " dts_max=%s\n", pos_max, pos_limit,
av_ts2str(ts_max));
369 pos_max =
FFMAX(0, (pos_max) - step);
371 &pos_max,
limit, read_timestamp_func);
380 &tmp_pos, INT64_MAX, read_timestamp_func);
415 ts_min =
read_timestamp(
s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
420 if (ts_min >= target_ts) {
426 if ((ret =
ff_find_last_ts(
s, stream_index, &ts_max, &pos_max, read_timestamp_func)) < 0)
431 if (ts_max <= target_ts) {
439 while (pos_min < pos_limit) {
441 "pos_min=0x%"PRIx64
" pos_max=0x%"PRIx64
" dts_min=%s dts_max=%s\n",
445 if (no_change == 0) {
446 int64_t approximate_keyframe_distance = pos_max - pos_limit;
450 pos_min - approximate_keyframe_distance;
451 }
else if (no_change == 1) {
453 pos = (pos_min + pos_limit) >> 1;
461 else if (
pos > pos_limit)
472 " target:%s limit:%"PRId64
" start:%"PRId64
" noc:%d\n",
473 pos_min,
pos, pos_max,
475 pos_limit, start_pos, no_change);
480 if (target_ts <= ts) {
481 pos_limit = start_pos - 1;
485 if (target_ts >= ts) {
495 ts_min =
read_timestamp(
s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
497 ts_max =
read_timestamp(
s, stream_index, &pos_min, INT64_MAX, read_timestamp_func);
516 else if (
pos > pos_max)
521 s->io_repositioned = 1;
530 AVStream *
const st =
s->streams[stream_index];
539 timestamp < sti->index_entries[0].timestamp)
551 s->io_repositioned = 1;
556 s->io_repositioned = 1;
563 }
while (read_status ==
AVERROR(EAGAIN));
566 if (stream_index ==
pkt->stream_index &&
pkt->dts > timestamp) {
572 av_log(
s,
AV_LOG_ERROR,
"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey);
591 s->io_repositioned = 1;
610 if (stream_index < 0) {
612 if (stream_index < 0)
615 st =
s->streams[stream_index];
647 int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
670 if (min_ts > ts || max_ts < ts)
672 if (stream_index < -1 || stream_index >= (
int)
s->nb_streams)
683 if (stream_index == -1 &&
s->nb_streams == 1) {
707 if (ret < 0 && ts != min_ts && max_ts != ts) {
721 for (
unsigned i = 0;
i <
s->nb_streams;
i++) {
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
int64_t avio_size(AVIOContext *s)
Get the filesize.
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
static int64_t filesize(AVIOContext *pb)
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void av_parser_close(AVCodecParserContext *s)
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.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int avformat_flush(AVFormatContext *s)
Discard all internally buffered data.
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
int avformat_index_get_entries_count(const AVStream *st)
Get the index entry count for the given AVStream.
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.
int avformat_queue_attached_pictures(AVFormatContext *s)
const AVIndexEntry * avformat_index_get_entry(AVStream *st, int idx)
Get the AVIndexEntry corresponding to the given index.
int av_find_default_stream_index(AVFormatContext *s)
const AVIndexEntry * avformat_index_get_entry_from_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the AVIndexEntry corresponding to the given timestamp.
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the index for a specific timestamp.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_DOWN
Round toward -infinity.
@ AV_ROUND_PASS_MINMAX
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
@ AV_ROUND_UP
Round toward +infinity.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_TIME_BASE
Internal time base represented as integer.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
#define MAX_REORDER_DELAY
Memory handling functions.
static float distance(float x, float y, int band)
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and FFInputFormat.read_timestamp().
void ff_rescale_interval(AVRational tb_in, AVRational tb_out, int64_t *min_ts, int64_t *ts, int64_t *max_ts)
Rescales a timestamp and the endpoints of an interval to which the temstamp belongs,...
int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos, int64_t(*read_timestamp_func)(struct AVFormatContext *, int, int64_t *, int64_t))
int ff_add_index_entry(AVIndexEntry **index_entries, int *nb_index_entries, unsigned int *index_entries_allocated_size, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Internal version of av_add_index_entry.
static int seek_frame_generic(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries, int64_t wanted_timestamp, int flags)
Internal version of av_index_search_timestamp.
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
static int seek_frame_internal(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, int64_t pos_min, int64_t pos_max, int64_t pos_limit, int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret, int64_t(*read_timestamp_func)(struct AVFormatContext *, int, int64_t *, int64_t))
Perform a binary search using read_timestamp().
void ff_reduce_index(AVFormatContext *s, int stream_index)
Ensure the index uses less memory than the maximum specified in AVFormatContext.max_index_size by dis...
static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
This structure stores compressed data.
Rational number (pair of numerator and denominator).
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int64_t data_offset
offset of the first packet
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
int probe_packets
Number of packets to buffer for codec probing.
int64_t pts_buffer[MAX_REORDER_DELAY+1]
int64_t last_dts_for_order_check
Internal data to analyze DTS and detect faulty mpeg streams.
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
struct AVCodecParserContext * parser
int64_t first_dts
Timestamp corresponding to the last dts sync point.
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
unsigned int index_entries_allocated_size
static AVFormatContext * ctx
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static double limit(double x)