66 return FFMPEG_CONFIGURATION;
71 #define LICENSE_PREFIX "libavformat license: "
75 #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
93 timestamp < st->pts_wrap_reference)
109 if(f)
return f->
next;
115 if(f)
return f->
next;
145 ext = strrchr(filename,
'.');
151 while (*p !=
'\0' && *p !=
',' && q-ext1<
sizeof(ext1)-1)
172 namelen = strlen(name);
173 while ((p = strchr(names,
','))) {
174 len =
FFMAX(p - names, namelen);
183 const char *mime_type)
186 int score_max, score;
189 #if CONFIG_IMAGE2_MUXER
190 if (!short_name && filename &&
209 if (score > score_max) {
218 const char *filename,
const char *mime_type,
enum AVMediaType type){
222 #if CONFIG_IMAGE2_MUXER
223 if(!strcmp(fmt->
name,
"image2") || !strcmp(fmt->
name,
"image2pipe")){
252 if(remaining < size){
255 s->
maxsize= newsize - !newsize;
257 remaining=
FFMAX(remaining, 0);
260 if(s->
maxsize>=0 && remaining+1 < size){
271 int orig_size =
size;
286 if (pkt->
size < orig_size)
298 old_size = pkt->
size;
318 int score, nodat = 0, score_max=0;
322 lpd.
buf = zerobuffer;
347 if (score > score_max) {
350 }
else if (score == score_max)
353 *score_ret= score_max;
362 if(score_ret > *score_max){
363 *score_max= score_ret;
376 static const struct {
397 for (i = 0; fmt_id_type[i].name; i++) {
398 if (!strcmp(fmt->
name, fmt_id_type[i].name)) {
428 #define PROBE_BUF_MIN 2048
429 #define PROBE_BUF_MAX (1<<20)
432 const char *filename,
void *logctx,
433 unsigned int offset,
unsigned int max_probe_size)
436 unsigned char *buf =
NULL;
438 int ret = 0, probe_size, buf_offset = 0;
440 if (!max_probe_size) {
448 if (offset >= max_probe_size) {
460 probe_size =
FFMIN(probe_size<<1,
FFMAX(max_probe_size, probe_size+1))) {
464 if (probe_size < offset) {
475 if ((ret =
avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
493 av_log(logctx,
AV_LOG_WARNING,
"Format %s detected only with low score of %d, misdetection possible!\n", (*fmt)->name, score);
495 av_log(logctx,
AV_LOG_DEBUG,
"Format %s probed with size=%d and score=%d\n", (*fmt)->name, probe_size, score);
523 "will be ignored with AVFMT_NOFILE format.\n");
546 (*plast_pktl)->next = pktl;
548 *packet_buffer = pktl;
578 av_log(
NULL,
AV_LOG_ERROR,
"Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
590 if ((ret =
init_input(s, filename, &tmp)) < 0)
627 if (id3v2_extra_meta) {
746 if (!pktl || ret ==
AVERROR(EAGAIN))
761 "Dropped corrupted packet (stream = %d)\n",
795 #if FF_API_READ_PACKET
857 if (st->r_frame_rate.num && !pc) {
858 *pnum = st->r_frame_rate.den;
859 *pden = st->r_frame_rate.num;
881 if (frame_size <= 0 || st->codec->sample_rate <= 0)
912 #if CONFIG_H264_DECODER
949 if (!first_program) {
974 program = first_program;
1029 pts_buffer[0]= pktl->
pkt.
pts;
1030 for(i=0; i<delay && pts_buffer[i] > pts_buffer[i+1]; i++)
1031 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i+1]);
1033 pktl->
pkt.
dts= pts_buffer[0];
1099 int num, den, presentation_delayed, delay, i;
1114 presentation_delayed = 0;
1120 presentation_delayed = 1;
1134 if(strcmp(s->
iformat->
name,
"mov,mp4,m4a,3gp,3g2,mj2"))
1179 presentation_delayed = 1;
1181 av_dlog(
NULL,
"IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%d\n",
1186 if (presentation_delayed) {
1236 av_dlog(
NULL,
"OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s\n",
1250 *pkt_buf = pktl->
next;
1254 *pkt_buf_end =
NULL;
1268 int ret = 0, got_output = 0;
1279 while (size > 0 || (pkt == &
flush_pkt && got_output)) {
1284 &out_pkt.
data, &out_pkt.
size, data, size,
1293 got_output = !!out_pkt.
size;
1367 *pkt_buffer = pktl->
next;
1369 *pkt_buffer_end =
NULL;
1376 int ret = 0, i, got_packet = 0;
1404 cur_pkt.
pts < cur_pkt.
dts) {
1412 av_log(s,
AV_LOG_DEBUG,
"ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1424 "%s, packets or times may be invalid.\n",
1466 av_log(s,
AV_LOG_DEBUG,
"read_frame_internal stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1503 int64_t last_dts = next_pkt->
dts;
1512 last_dts = pktl->
pkt.
dts;
1539 if (pktl && ret !=
AVERROR(EAGAIN)) {
1589 int first_audio_index = -1;
1602 first_audio_index = i;
1604 return first_audio_index >= 0 ? first_audio_index : 0;
1664 int *nb_index_entries,
1665 unsigned int *index_entries_allocated_size,
1671 if((
unsigned)*nb_index_entries + 1 >= UINT_MAX /
sizeof(
AVIndexEntry))
1681 index_entries_allocated_size,
1682 (*nb_index_entries + 1) *
1687 *index_entries= entries;
1692 index= (*nb_index_entries)++;
1693 ie= &entries[
index];
1694 assert(index==0 || ie[-1].timestamp < timestamp);
1696 ie= &entries[
index];
1700 memmove(entries + index + 1, entries + index,
sizeof(
AVIndexEntry)*(*nb_index_entries - index));
1701 (*nb_index_entries)++;
1702 }
else if(ie->
pos == pos && distance < ie->min_distance)
1721 timestamp, size, distance, flags);
1725 int64_t wanted_timestamp,
int flags)
1734 if(b && entries[b-1].timestamp < wanted_timestamp)
1740 if(timestamp >= wanted_timestamp)
1742 if(timestamp <= wanted_timestamp)
1762 wanted_timestamp, flags);
1766 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1775 int64_t ts_min, ts_max, ts;
1780 if (stream_index < 0)
1794 index=
FFMAX(index, 0);
1800 av_dlog(s,
"using cached pos_min=0x%"PRIx64
" dts_min=%s\n",
1807 assert(index < st->nb_index_entries);
1814 av_dlog(s,
"using cached pos_max=0x%"PRIx64
" pos_limit=0x%"PRIx64
" dts_max=%s\n",
1819 pos=
ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->
read_timestamp);
1834 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1835 int64_t ts_min, int64_t ts_max,
int flags, int64_t *ts_ret,
1836 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1839 int64_t start_pos, filesize;
1846 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1851 if(ts_min >= target_ts){
1859 pos_max = filesize - 1;
1862 ts_max =
ff_read_timestamp(s, stream_index, &pos_max, pos_max + step, read_timestamp);
1869 int64_t tmp_pos= pos_max + 1;
1870 int64_t tmp_ts=
ff_read_timestamp(s, stream_index, &tmp_pos, INT64_MAX, read_timestamp);
1875 if(tmp_pos >= filesize)
1881 if(ts_max <= target_ts){
1886 if(ts_min > ts_max){
1888 }
else if(ts_min == ts_max){
1893 while (pos_min < pos_limit) {
1894 av_dlog(s,
"pos_min=0x%"PRIx64
" pos_max=0x%"PRIx64
" dts_min=%s dts_max=%s\n",
1896 assert(pos_limit <= pos_max);
1899 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1901 pos =
av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1902 + pos_min - approximate_keyframe_distance;
1903 }
else if(no_change==1){
1905 pos = (pos_min + pos_limit)>>1;
1913 else if(pos > pos_limit)
1922 av_dlog(s,
"%"PRId64
" %"PRId64
" %"PRId64
" / %s %s %s target:%s limit:%"PRId64
" start:%"PRId64
" noc:%d\n",
1923 pos_min, pos, pos_max,
1925 pos_limit, start_pos, no_change);
1931 if (target_ts <= ts) {
1932 pos_limit = start_pos - 1;
1936 if (target_ts >= ts) {
1946 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1948 ts_max =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1949 av_dlog(s,
"pos=0x%"PRIx64
" %s<=%s<=%s\n",
1957 int64_t pos_min, pos_max;
1962 if (pos < pos_min) pos= pos_min;
1963 else if(pos > pos_max) pos= pos_max;
1971 int stream_index, int64_t timestamp,
int flags)
1978 st = s->
streams[stream_index];
1982 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1985 if(index < 0 || index==st->nb_index_entries-1){
2003 }
while (read_status ==
AVERROR(EAGAIN));
2004 if (read_status < 0)
2011 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);
2035 int64_t timestamp,
int flags)
2047 if(stream_index < 0){
2049 if(stream_index < 0)
2090 if(min_ts > ts || max_ts < ts)
2097 if (stream_index == -1 && s->
nb_streams == 1) {
2104 time_base.
num * (int64_t)AV_TIME_BASE,
2124 if (ret<0 && ts != min_ts && max_ts != ts) {
2125 ret =
av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2165 int64_t
start_time, start_time1, start_time_text, end_time, end_time1;
2166 int64_t
duration, duration1, filesize;
2171 start_time = INT64_MAX;
2172 start_time_text = INT64_MAX;
2173 end_time = INT64_MIN;
2174 duration = INT64_MIN;
2180 if (start_time1 < start_time_text)
2181 start_time_text = start_time1;
2183 start_time =
FFMIN(start_time, start_time1);
2186 end_time1 = start_time1
2188 end_time =
FFMAX(end_time, end_time1);
2199 duration =
FFMAX(duration, duration1);
2202 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text <
AV_TIME_BASE))
2203 start_time = start_time_text;
2204 else if(start_time > start_time_text)
2207 if (start_time != INT64_MAX) {
2209 if (end_time != INT64_MIN) {
2217 duration =
FFMAX(duration, end_time - start_time);
2225 ic->
bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
2279 #define DURATION_MAX_READ_SIZE 250000LL
2280 #define DURATION_MAX_RETRY 4
2287 int read_size, i, ret;
2323 }
while(ret ==
AVERROR(EAGAIN));
2326 read_size += pkt->
size;
2331 duration = end_time = pkt->
pts;
2367 file_size =
FFMAX(0, file_size);
2394 av_dlog(ic,
"%d: start_time: %0.3f duration: %0.3f\n", i,
2398 av_dlog(ic,
"stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2409 #define FAIL(errmsg) do { \
2411 *errmsg_ptr = errmsg; \
2418 FAIL(
"unspecified frame size");
2420 FAIL(
"unspecified sample format");
2422 FAIL(
"unspecified sample rate");
2424 FAIL(
"unspecified number of channels");
2426 FAIL(
"no decodable DTS frames");
2430 FAIL(
"unspecified size");
2432 FAIL(
"unspecified pixel format");
2436 FAIL(
"unspecified size");
2443 FAIL(
"unknown codec");
2451 int got_picture = 1, ret = 0;
2473 av_dict_set(options ? options : &thread_opt,
"threads",
"1", 0);
2490 while ((pkt.
size > 0 || (!pkt.
data && got_picture)) &&
2500 &got_picture, &pkt);
2507 &got_picture, &pkt);
2522 if(!pkt.
data && !got_picture)
2544 if(tag == tags[i].tag)
2565 if (sflags & (1 << (bps - 1))) {
2588 for(i=0; tags && tags[i]; i++){
2598 for(i=0; tags && tags[i]; i++){
2619 if (j != i && next_start > ch->
start && next_start < end)
2622 ch->
end = (end == INT64_MAX) ? ch->
start : end;
2627 if(i<60*12)
return (i+1)*1001;
2628 else return ((
const int[]){24,30,60,12,15,48})[i-60*12]*1000*12;
2652 #if FF_API_FORMAT_PARAMETERS
2661 int i, count, ret, read_size, j;
2694 "%s, packets or times may be invalid.\n",
2703 av_dict_set(options ? &options[i] : &thread_opt,
"threads",
"1", 0);
2722 #if FF_API_R_FRAME_RATE
2740 int fps_analyze_framecount = 20;
2749 fps_analyze_framecount *= 2;
2781 if (!ic->
streams[i]->r_frame_rate.num &&
2784 "Stream #%d: not enough frames to estimate rate; "
2785 "consider increasing probesize\n", i);
2806 goto find_stream_info_err;
2809 read_size += pkt->
size;
2817 "packet %d with DTS %"PRId64
", packet %d with DTS "
2830 "packet %d with DTS %"PRId64
", packet %d with DTS "
2860 #if FF_API_R_FRAME_RATE
2872 double sdts= dts*framerate/(1001*12);
2874 int64_t ticks=
llrint(sdts+j*0.5);
2875 double error= sdts - ticks + j*0.5;
2931 (options && i < orig_nb_streams) ?
2932 &options[i] :
NULL);
2937 "decoding for stream %d failed\n", st->
index);
2945 "Could not find codec parameters for stream %d (%s): %s\n"
2946 "Consider increasing the value for the 'analyzeduration' and 'probesize' options\n",
2971 double best_error = 0.01;
2983 if (error < best_error) {
2985 best_fps = std_fps.
num;
2990 best_fps, 12*1001, INT_MAX);
3001 double best_error= 0.01;
3015 if(error < best_error && best_error> 0.000000001){
3024 if (num && (!st->r_frame_rate.num || (
double)num/(12*1001) < 1.01 *
av_q2d(st->r_frame_rate)))
3025 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
3028 if (!st->r_frame_rate.num){
3062 find_stream_info_err:
3092 int wanted_stream_nb,
3098 int ret =
AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;
3099 unsigned *program =
NULL;
3102 if (related_stream >= 0 && wanted_stream_nb < 0) {
3109 for (i = 0; i < nb_streams; i++) {
3110 int real_stream_index = program ? program[i] : i;
3115 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
3129 multiframe =
FFMIN(5, count);
3130 if ((best_multiframe > multiframe) ||
3131 (best_multiframe == multiframe && best_bitrate > bitrate) ||
3132 (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
3135 best_bitrate = bitrate;
3136 best_multiframe = multiframe;
3137 ret = real_stream_index;
3139 if (program && i == nb_streams - 1 && ret < 0) {
3146 *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);
3366 chapter->
start = start;
3401 uint64_t v=
lrintf(d*100);
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
3466 if(st->r_frame_rate.den && st->r_frame_rate.num)
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)
3578 #if FF_API_AV_GETTIME && CONFIG_SHARED && HAVE_SYMVER
3579 FF_SYMVER(int64_t,
av_gettime, (
void),
"LIBAVFORMAT_54")
3591 const char *path,
int number)
3594 char *q, buf1[20],
c;
3595 int nd,
len, percentd_found;
3607 while (isdigit(*p)) {
3608 nd = nd * 10 + *p++ -
'0';
3611 }
while (isdigit(c));
3620 snprintf(buf1,
sizeof(buf1),
"%0*d", nd, number);
3622 if ((q - buf + len) > buf_size - 1)
3624 memcpy(q, buf1, len);
3632 if ((q - buf) < buf_size - 1)
3636 if (!percentd_found)
3649 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3651 for(i=0;i<
size;i+=16) {
3658 PRINT(
" %02x", buf[i+j]);
3663 for(j=0;j<
len;j++) {
3665 if (c < ' ' || c >
'~')
3686 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3710 void av_pkt_dump(FILE *f,
AVPacket *
pkt,
int dump_payload)
3723 void av_pkt_dump_log(
void *avcl,
int level,
AVPacket *
pkt,
int dump_payload)
3737 char *authorization,
int authorization_size,
3738 char *hostname,
int hostname_size,
3740 char *path,
int path_size,
3743 const char *p, *ls, *ls2, *at, *at2, *col, *brk;
3745 if (port_ptr) *port_ptr = -1;
3746 if (proto_size > 0) proto[0] = 0;
3747 if (authorization_size > 0) authorization[0] = 0;
3748 if (hostname_size > 0) hostname[0] = 0;
3749 if (path_size > 0) path[0] = 0;
3752 if ((p = strchr(url,
':'))) {
3764 ls = strchr(p,
'/');
3765 ls2 = strchr(p,
'?');
3769 ls =
FFMIN(ls, ls2);
3779 while ((at = strchr(p,
'@')) && at < ls) {
3781 FFMIN(authorization_size, at + 1 - at2));
3785 if (*p ==
'[' && (brk = strchr(p,
']')) && brk < ls) {
3788 FFMIN(hostname_size, brk - p));
3789 if (brk[1] ==
':' && port_ptr)
3790 *port_ptr = atoi(brk + 2);
3791 }
else if ((col = strchr(p,
':')) && col < ls) {
3793 FFMIN(col + 1 - p, hostname_size));
3794 if (port_ptr) *port_ptr = atoi(col + 1);
3797 FFMIN(ls + 1 - p, hostname_size));
3804 static const char hex_table_uc[16] = {
'0',
'1',
'2',
'3',
3807 'C',
'D',
'E',
'F' };
3808 static const char hex_table_lc[16] = {
'0',
'1',
'2',
'3',
3811 'c',
'd',
'e',
'f' };
3812 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3814 for(i = 0; i < s; i++) {
3815 buff[i * 2] = hex_table[src[i] >> 4];
3816 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3832 c = toupper((
unsigned char) *p++);
3833 if (c >=
'0' && c <=
'9')
3835 else if (c >=
'A' && c <=
'F')
3850 #if FF_API_SET_PTS_INFO
3851 void av_set_pts_info(
AVStream *s,
int pts_wrap_bits,
3852 unsigned int pts_num,
unsigned int pts_den)
3859 unsigned int pts_num,
unsigned int pts_den)
3863 if(new_tb.
num != pts_num)
3868 if(new_tb.
num <= 0 || new_tb.
den <= 0) {
3878 const char *authorization,
const char *hostname,
3879 int port,
const char *
fmt, ...)
3882 struct addrinfo hints = { 0 }, *ai;
3888 if (authorization && authorization[0])
3890 #if CONFIG_NETWORK && defined(AF_INET6)
3895 if (ai->ai_family == AF_INET6) {
3912 int len = strlen(str);
3915 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
3942 const char *ptr = str;
3947 char *dest =
NULL, *dest_end;
3948 int key_len, dest_len = 0;
3951 while (*ptr && (isspace(*ptr) || *ptr ==
','))
3958 if (!(ptr = strchr(key,
'=')))
3961 key_len = ptr - key;
3963 callback_get_buf(context, key, key_len, &dest, &dest_len);
3964 dest_end = dest + dest_len - 1;
3968 while (*ptr && *ptr !=
'\"') {
3972 if (dest && dest < dest_end)
3976 if (dest && dest < dest_end)
3984 for (; *ptr && !(isspace(*ptr) || *ptr ==
','); ptr++)
3985 if (dest && dest < dest_end)
4006 char *sep, *path_query;
4008 if (base && strstr(base,
"://") && rel[0] ==
'/') {
4011 sep = strstr(buf,
"://");
4014 if (rel[1] ==
'/') {
4019 sep = strchr(sep,
'/');
4028 if (!base || strstr(rel,
"://") || rel[0] ==
'/') {
4036 path_query = strchr(buf,
'?');
4037 if (path_query !=
NULL)
4041 if (rel[0] ==
'?') {
4047 sep = strrchr(buf,
'/');
4055 sep = strrchr(buf,
'/');
4057 if (!strcmp(sep ? &sep[1] : buf,
"..")) {
4074 struct tm time1 = {0}, time2 = {0};
4088 return ofmt->
query_codec(codec_id, std_compliance);
4132 if (channel_layout) {
4140 if (width || height) {
4147 bytestream_put_le32(&data, flags);
4149 bytestream_put_le32(&data, channels);
4151 bytestream_put_le64(&data, channel_layout);
4153 bytestream_put_le32(&data, sample_rate);
4154 if (width || height) {
4155 bytestream_put_le32(&data, width);
4156 bytestream_put_le32(&data, height);
4177 av_reduce(&stream_sample_aspect_ratio.
num, &stream_sample_aspect_ratio.
den,