64 return FFMPEG_CONFIGURATION;
69 #define LICENSE_PREFIX "libavformat license: "
73 #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
91 timestamp < st->pts_wrap_reference)
100 #define MAKE_ACCESSORS(str, name, type, field) \
101 type av_##name##_get_##field(const str *s) { return s->field; } \
102 void av_##name##_set_##field(str *s, type v) { s->field = v; }
107 #define SANE_CHUNK_SIZE (50000000)
113 if(remaining < size){
116 s->
maxsize= newsize - !newsize;
118 remaining=
FFMAX(remaining, 0);
121 if(s->
maxsize>=0 && remaining+1 < size){
135 int64_t orig_pos = pkt->
pos;
136 int orig_size = pkt->
size;
140 int prev_size = pkt->
size;
160 if (ret != read_size) {
173 return pkt->
size > orig_size ? pkt->
size - orig_size :
ret;
204 int score, nodat = 0, score_max=0;
208 lpd.
buf = zerobuffer;
233 if (score > score_max) {
236 }
else if (score == score_max)
241 *score_ret= score_max;
250 if(score_ret > *score_max){
251 *score_max= score_ret;
264 static const struct {
285 for (i = 0; fmt_id_type[i].name; i++) {
286 if (!strcmp(fmt->
name, fmt_id_type[i].name)) {
316 #define PROBE_BUF_MIN 2048
317 #define PROBE_BUF_MAX (1<<20)
320 const char *filename,
void *logctx,
321 unsigned int offset,
unsigned int max_probe_size)
323 AVProbeData pd = { filename ? filename :
"", NULL, -offset };
324 unsigned char *
buf = NULL;
326 int ret = 0, probe_size, buf_offset = 0;
328 if (!max_probe_size) {
334 "Specified probe size value %u cannot be < %u\n", max_probe_size,
PROBE_BUF_MIN);
338 if (offset >= max_probe_size) {
350 probe_size =
FFMIN(probe_size<<1,
FFMAX(max_probe_size, probe_size+1))) {
354 if (probe_size < offset) {
365 if ((ret =
avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
383 av_log(logctx,
AV_LOG_WARNING,
"Format %s detected only with low score of %d, misdetection possible!\n", (*fmt)->name, score);
385 av_log(logctx,
AV_LOG_DEBUG,
"Format %s probed with size=%d and score=%d\n", (*fmt)->name, probe_size, score);
413 "will be ignored with AVFMT_NOFILE format.\n");
436 (*plast_pktl)->next = pktl;
438 *packet_buffer = pktl;
472 av_log(NULL,
AV_LOG_ERROR,
"Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
484 if ((ret =
init_input(s, filename, &tmp)) < 0)
521 if (id3v2_extra_meta) {
644 if (!pktl || ret ==
AVERROR(EAGAIN))
659 "Dropped corrupted packet (stream = %d)\n",
693 #if FF_API_READ_PACKET
779 if (frame_size <= 0 || st->codec->sample_rate <= 0)
810 #if CONFIG_H264_DECODER
847 if (!first_program) {
872 program = first_program;
927 pts_buffer[0]= pktl->
pkt.
pts;
928 for(i=0; i<delay && pts_buffer[i] > pts_buffer[i+1]; i++)
929 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i+1]);
931 pktl->
pkt.
dts= pts_buffer[0];
997 int num, den, presentation_delayed, delay, i;
1012 presentation_delayed = 0;
1018 presentation_delayed = 1;
1032 if(strcmp(s->
iformat->
name,
"mov,mp4,m4a,3gp,3g2,mj2"))
1077 presentation_delayed = 1;
1079 av_dlog(NULL,
"IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%d\n",
1084 if (presentation_delayed) {
1134 av_dlog(NULL,
"OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s\n",
1148 *pkt_buf = pktl->
next;
1152 *pkt_buf_end = NULL;
1166 int ret = 0, got_output = 0;
1177 while (size > 0 || (pkt == &
flush_pkt && got_output)) {
1182 &out_pkt.
data, &out_pkt.
size, data, size,
1191 got_output = !!out_pkt.
size;
1241 #if FF_API_DESTRUCT_PACKET
1242 out_pkt.destruct = pkt->destruct;
1243 pkt->destruct = NULL;
1276 *pkt_buffer = pktl->
next;
1278 *pkt_buffer_end = NULL;
1285 int ret = 0, i, got_packet = 0;
1313 cur_pkt.
pts < cur_pkt.
dts) {
1321 av_log(s,
AV_LOG_DEBUG,
"ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1333 "%s, packets or times may be invalid.\n",
1378 av_log(s,
AV_LOG_DEBUG,
"read_frame_internal stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1415 int64_t last_dts = next_pkt->
dts;
1424 last_dts = pktl->
pkt.
dts;
1451 if (pktl && ret !=
AVERROR(EAGAIN)) {
1501 int first_audio_index = -1;
1514 first_audio_index = i;
1516 return first_audio_index >= 0 ? first_audio_index : 0;
1576 int *nb_index_entries,
1577 unsigned int *index_entries_allocated_size,
1583 if((
unsigned)*nb_index_entries + 1 >= UINT_MAX /
sizeof(
AVIndexEntry))
1593 index_entries_allocated_size,
1594 (*nb_index_entries + 1) *
1599 *index_entries= entries;
1604 index= (*nb_index_entries)++;
1605 ie= &entries[
index];
1606 av_assert0(index==0 || ie[-1].timestamp < timestamp);
1608 ie= &entries[
index];
1612 memmove(entries + index + 1, entries + index,
sizeof(
AVIndexEntry)*(*nb_index_entries - index));
1613 (*nb_index_entries)++;
1614 }
else if(ie->
pos == pos && distance < ie->min_distance)
1633 timestamp, size, distance, flags);
1637 int64_t wanted_timestamp,
int flags)
1646 if(b && entries[b-1].timestamp < wanted_timestamp)
1652 if(timestamp >= wanted_timestamp)
1654 if(timestamp <= wanted_timestamp)
1674 wanted_timestamp, flags);
1678 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1680 int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
1681 if (stream_index >= 0)
1690 int64_t ts_min, ts_max, ts;
1695 if (stream_index < 0)
1709 index=
FFMAX(index, 0);
1715 av_dlog(s,
"using cached pos_min=0x%"PRIx64
" dts_min=%s\n",
1729 av_dlog(s,
"using cached pos_max=0x%"PRIx64
" pos_limit=0x%"PRIx64
" dts_max=%s\n",
1734 pos=
ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->
read_timestamp);
1749 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1752 int64_t limit, ts_max;
1754 int64_t pos_max = filesize - 1;
1757 pos_max =
FFMAX(0, (pos_max) - step);
1765 int64_t tmp_pos = pos_max + 1;
1766 int64_t tmp_ts =
ff_read_timestamp(s, stream_index, &tmp_pos, INT64_MAX, read_timestamp);
1771 if(tmp_pos >= filesize)
1784 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1785 int64_t ts_min, int64_t ts_max,
int flags, int64_t *ts_ret,
1786 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1797 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1802 if(ts_min >= target_ts){
1808 if ((ret =
ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp)) < 0)
1813 if(ts_max <= target_ts){
1818 if(ts_min > ts_max){
1820 }
else if(ts_min == ts_max){
1825 while (pos_min < pos_limit) {
1826 av_dlog(s,
"pos_min=0x%"PRIx64
" pos_max=0x%"PRIx64
" dts_min=%s dts_max=%s\n",
1828 assert(pos_limit <= pos_max);
1831 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1833 pos =
av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1834 + pos_min - approximate_keyframe_distance;
1835 }
else if(no_change==1){
1837 pos = (pos_min + pos_limit)>>1;
1845 else if(pos > pos_limit)
1854 av_dlog(s,
"%"PRId64
" %"PRId64
" %"PRId64
" / %s %s %s target:%s limit:%"PRId64
" start:%"PRId64
" noc:%d\n",
1855 pos_min, pos, pos_max,
1857 pos_limit, start_pos, no_change);
1863 if (target_ts <= ts) {
1864 pos_limit = start_pos - 1;
1868 if (target_ts >= ts) {
1878 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1880 ts_max =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1881 av_dlog(s,
"pos=0x%"PRIx64
" %s<=%s<=%s\n",
1889 int64_t pos_min, pos_max;
1894 if (pos < pos_min) pos= pos_min;
1895 else if(pos > pos_max) pos= pos_max;
1905 int stream_index, int64_t timestamp,
int flags)
1912 st = s->
streams[stream_index];
1916 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1919 if(index < 0 || index==st->nb_index_entries-1){
1937 }
while (read_status ==
AVERROR(EAGAIN));
1938 if (read_status < 0)
1945 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);
1969 int64_t timestamp,
int flags)
1981 if(stream_index < 0){
1983 if(stream_index < 0)
2017 int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
2023 flags & ~AVSEEK_FLAG_BACKWARD);
2036 if(min_ts > ts || max_ts < ts)
2038 if (stream_index < -1 || stream_index >= (
int)s->
nb_streams)
2049 if (stream_index == -1 && s->
nb_streams == 1) {
2056 time_base.
num * (int64_t)AV_TIME_BASE,
2076 if (ret<0 && ts != min_ts && max_ts != ts) {
2077 ret =
av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2117 int64_t
start_time, start_time1, start_time_text, end_time, end_time1;
2118 int64_t
duration, duration1, filesize;
2123 start_time = INT64_MAX;
2124 start_time_text = INT64_MAX;
2125 end_time = INT64_MIN;
2126 duration = INT64_MIN;
2132 if (start_time1 < start_time_text)
2133 start_time_text = start_time1;
2135 start_time =
FFMIN(start_time, start_time1);
2138 end_time1 = start_time1
2140 end_time =
FFMAX(end_time, end_time1);
2151 duration =
FFMAX(duration, duration1);
2154 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text <
AV_TIME_BASE))
2155 start_time = start_time_text;
2156 else if(start_time > start_time_text)
2159 if (start_time != INT64_MAX) {
2161 if (end_time != INT64_MIN) {
2169 duration =
FFMAX(duration, end_time - start_time);
2177 double bitrate = (double)filesize * 8.0 * AV_TIME_BASE /
2179 if (bitrate >= 0 && bitrate <= INT_MAX)
2204 int bit_rate, i, show_warning = 0;
2238 #define DURATION_MAX_READ_SIZE 250000LL
2239 #define DURATION_MAX_RETRY 4
2246 int read_size, i,
ret;
2282 }
while(ret ==
AVERROR(EAGAIN));
2285 read_size += pkt->
size;
2290 duration = end_time = pkt->
pts;
2328 file_size =
FFMAX(0, file_size);
2354 av_dlog(ic,
"%d: start_time: %0.3f duration: %0.3f\n", i,
2358 av_dlog(ic,
"stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2369 #define FAIL(errmsg) do { \
2371 *errmsg_ptr = errmsg; \
2378 FAIL(
"unspecified frame size");
2380 FAIL(
"unspecified sample format");
2382 FAIL(
"unspecified sample rate");
2384 FAIL(
"unspecified number of channels");
2386 FAIL(
"no decodable DTS frames");
2390 FAIL(
"unspecified size");
2392 FAIL(
"unspecified pixel format");
2395 FAIL(
"no frame in rv30/40 and no sar");
2399 FAIL(
"unspecified size");
2406 FAIL(
"unknown codec");
2414 int got_picture = 1,
ret = 0;
2436 av_dict_set(options ? options : &thread_opt,
"threads",
"1", 0);
2453 while ((pkt.
size > 0 || (!pkt.
data && got_picture)) &&
2463 &got_picture, &pkt);
2470 &got_picture, &pkt);
2485 if(!pkt.
data && !got_picture)
2507 if(tag == tags[i].tag)
2528 if (sflags & (1 << (bps - 1))) {
2560 for(i=0; tags && tags[i]; i++){
2563 if (codec_tags->
id ==
id) {
2564 *tag = codec_tags->
tag;
2576 for(i=0; tags && tags[i]; i++){
2597 if (j != i && next_start > ch->
start && next_start < end)
2600 ch->
end = (end == INT64_MAX) ? ch->
start : end;
2605 if(i<60*12)
return (i+1)*1001;
2606 else return ((
const int[]){24,30,60,12,15,48})[i-60*12]*1000*12;
2630 #if FF_API_FORMAT_PARAMETERS
2673 "%s, packets or times may be invalid.\n",
2682 av_dict_set(options ? &options[i] : &thread_opt,
"threads",
"1", 0);
2701 #if FF_API_R_FRAME_RATE
2719 int fps_analyze_framecount = 20;
2728 fps_analyze_framecount *= 2;
2732 fps_analyze_framecount = 0;
2765 "Stream #%d: not enough frames to estimate rate; "
2766 "consider increasing probesize\n", i);
2787 goto find_stream_info_err;
2790 read_size += pkt->
size;
2798 "packet %d with DTS %"PRId64
", packet %d with DTS "
2811 "packet %d with DTS %"PRId64
", packet %d with DTS "
2847 #if FF_API_R_FRAME_RATE
2852 && pkt->
dts - (uint64_t)last < INT64_MAX){
2863 double sdts= dts*framerate/(1001*12);
2865 int64_t ticks=
llrint(sdts+j*0.5);
2866 double error= sdts - ticks + j*0.5;
2901 try_decode_frame(st, pkt, (options && i < orig_nb_streams ) ? &options[i] : NULL);
2920 (options && i < orig_nb_streams) ?
2921 &options[i] : NULL);
2926 "decoding for stream %d failed\n", st->
index);
2949 double best_error = 0.01;
2961 if (error < best_error) {
2963 best_fps = std_fps.
num;
2968 best_fps, 12*1001, INT_MAX);
2979 double best_error= 0.01;
2993 if(error < best_error && best_error> 0.000000001){
3047 "Could not find codec parameters for stream %d (%s): %s\n"
3048 "Consider increasing the value for the 'analyzeduration' and 'probesize' options\n",
3057 find_stream_info_err:
3090 int wanted_stream_nb,
3097 unsigned *program = NULL;
3100 if (related_stream >= 0 && wanted_stream_nb < 0) {
3107 for (i = 0; i < nb_streams; i++) {
3108 int real_stream_index = program ? program[i] : i;
3113 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
3128 if ((best_multiframe > multiframe) ||
3129 (best_multiframe == multiframe && best_bitrate > bitrate) ||
3130 (best_multiframe == multiframe && best_bitrate == bitrate && best_count >=
count))
3133 best_bitrate = bitrate;
3134 best_multiframe = multiframe;
3135 ret = real_stream_index;
3137 if (program && i == nb_streams - 1 && ret < 0) {
3144 *decoder_ret = best_decoder;
3221 #if FF_API_CLOSE_INPUT_FILE
3251 #if FF_API_NEW_STREAM
3267 if (s->
nb_streams >= INT_MAX/
sizeof(*streams))
3310 #if FF_API_R_FRAME_RATE
3325 av_dlog(ac,
"new_program: id=0x%04x\n",
id);
3414 if(strcmp(
"language", tag->
key)){
3415 const char *p = tag->
value;
3419 size_t len = strcspn(p,
"\x8\xa\xb\xc\xd");
3460 display_aspect_ratio.
num, display_aspect_ratio.
den);
3465 #if FF_API_R_FRAME_RATE
3509 is_output ?
"Output" :
"Input",
3512 is_output ?
"to" :
"from", url);
3517 int hours, mins, secs, us;
3555 int j, k, total = 0;
3560 name ? name->
value :
"");
3568 if (total < ic->nb_streams)
3584 const char *path,
int number)
3587 char *q, buf1[20],
c;
3588 int nd,
len, percentd_found;
3601 nd = nd * 10 + *p++ -
'0';
3613 snprintf(buf1,
sizeof(buf1),
"%0*d", nd, number);
3615 if ((q - buf + len) > buf_size - 1)
3617 memcpy(q, buf1, len);
3625 if ((q - buf) < buf_size - 1)
3629 if (!percentd_found)
3642 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3644 for(i=0;i<
size;i+=16) {
3651 PRINT(
" %02x", buf[i+j]);
3656 for(j=0;j<
len;j++) {
3658 if (c < ' ' || c >
'~')
3679 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3703 void av_pkt_dump(FILE *f,
AVPacket *
pkt,
int dump_payload)
3716 void av_pkt_dump_log(
void *avcl,
int level,
AVPacket *
pkt,
int dump_payload)
3730 char *authorization,
int authorization_size,
3731 char *hostname,
int hostname_size,
3733 char *path,
int path_size,
3736 const char *p, *ls, *ls2, *at, *at2, *col, *brk;
3738 if (port_ptr) *port_ptr = -1;
3739 if (proto_size > 0) proto[0] = 0;
3740 if (authorization_size > 0) authorization[0] = 0;
3741 if (hostname_size > 0) hostname[0] = 0;
3742 if (path_size > 0) path[0] = 0;
3745 if ((p = strchr(url,
':'))) {
3757 ls = strchr(p,
'/');
3758 ls2 = strchr(p,
'?');
3762 ls =
FFMIN(ls, ls2);
3772 while ((at = strchr(p,
'@')) && at < ls) {
3774 FFMIN(authorization_size, at + 1 - at2));
3778 if (*p ==
'[' && (brk = strchr(p,
']')) && brk < ls) {
3781 FFMIN(hostname_size, brk - p));
3782 if (brk[1] ==
':' && port_ptr)
3783 *port_ptr = atoi(brk + 2);
3784 }
else if ((col = strchr(p,
':')) && col < ls) {
3786 FFMIN(col + 1 - p, hostname_size));
3787 if (port_ptr) *port_ptr = atoi(col + 1);
3790 FFMIN(ls + 1 - p, hostname_size));
3797 static const char hex_table_uc[16] = {
'0',
'1',
'2',
'3',
3800 'C',
'D',
'E',
'F' };
3801 static const char hex_table_lc[16] = {
'0',
'1',
'2',
'3',
3804 'c',
'd',
'e',
'f' };
3805 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3807 for(i = 0; i <
s; i++) {
3808 buff[i * 2] = hex_table[src[i] >> 4];
3809 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3826 if (c >=
'0' && c <=
'9')
3828 else if (c >=
'A' && c <=
'F')
3843 #if FF_API_SET_PTS_INFO
3844 void av_set_pts_info(
AVStream *s,
int pts_wrap_bits,
3845 unsigned int pts_num,
unsigned int pts_den)
3852 unsigned int pts_num,
unsigned int pts_den)
3856 if(new_tb.
num != pts_num)
3861 if(new_tb.
num <= 0 || new_tb.
den <= 0) {
3873 const char *ptr = str;
3878 char *dest = NULL, *dest_end;
3879 int key_len, dest_len = 0;
3882 while (*ptr && (
av_isspace(*ptr) || *ptr ==
','))
3889 if (!(ptr = strchr(key,
'=')))
3892 key_len = ptr - key;
3894 callback_get_buf(context, key, key_len, &dest, &dest_len);
3895 dest_end = dest + dest_len - 1;
3899 while (*ptr && *ptr !=
'\"') {
3903 if (dest && dest < dest_end)
3907 if (dest && dest < dest_end)
3915 for (; *ptr && !(
av_isspace(*ptr) || *ptr ==
','); ptr++)
3916 if (dest && dest < dest_end)
3936 struct tm time1 = {0}, time2 = {0};
3950 return ofmt->
query_codec(codec_id, std_compliance);
3994 if (channel_layout) {
4002 if (width || height) {
4009 bytestream_put_le32(&data, flags);
4011 bytestream_put_le32(&data, channels);
4013 bytestream_put_le64(&data, channel_layout);
4015 bytestream_put_le32(&data, sample_rate);
4016 if (width || height) {
4017 bytestream_put_le32(&data, width);
4018 bytestream_put_le32(&data, height);
4030 av_reduce(&stream_sample_aspect_ratio.
num, &stream_sample_aspect_ratio.
den,
4031 stream_sample_aspect_ratio.
num, stream_sample_aspect_ratio.
den, INT_MAX);
4032 if (stream_sample_aspect_ratio.
num <= 0 || stream_sample_aspect_ratio.
den <= 0)
4033 stream_sample_aspect_ratio = undef;
4035 av_reduce(&frame_sample_aspect_ratio.
num, &frame_sample_aspect_ratio.
den,
4036 frame_sample_aspect_ratio.
num, frame_sample_aspect_ratio.
den, INT_MAX);
4037 if (frame_sample_aspect_ratio.
num <= 0 || frame_sample_aspect_ratio.
den <= 0)
4038 frame_sample_aspect_ratio = undef;
4040 if (stream_sample_aspect_ratio.
num)
4041 return stream_sample_aspect_ratio;
4043 return frame_sample_aspect_ratio;
4065 if (*spec <= '9' && *spec >=
'0')
4066 return strtol(spec, NULL, 0) == st->
index;
4067 else if (*spec ==
'v' || *spec ==
'a' || *spec ==
's' || *spec ==
'd' ||
4081 if (*spec++ ==
':') {
4082 int i,
index = strtol(spec, NULL, 0);
4085 return i == st->
index;
4089 }
else if (*spec ==
'p' && *(spec + 1) ==
':') {
4093 prog_id = strtol(spec, &endptr, 0);