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; }
113 if(f)
return f->
next;
119 if(f)
return f->
next;
149 ext = strrchr(filename,
'.');
155 while (*p !=
'\0' && *p !=
',' && q-ext1<
sizeof(ext1)-1)
176 namelen = strlen(name);
177 while ((p = strchr(names,
','))) {
178 len =
FFMAX(p - names, namelen);
187 const char *mime_type)
190 int score_max, score;
193 #if CONFIG_IMAGE2_MUXER
194 if (!short_name && filename &&
213 if (score > score_max) {
222 const char *filename,
const char *mime_type,
enum AVMediaType type){
223 if (!strcmp(fmt->
name,
"segment") || !strcmp(fmt->
name,
"ssegment")) {
230 #if CONFIG_IMAGE2_MUXER
231 if(!strcmp(fmt->
name,
"image2") || !strcmp(fmt->
name,
"image2pipe")){
257 #define SANE_CHUNK_SIZE (50000000)
263 if(remaining < size){
266 s->
maxsize= newsize - !newsize;
268 remaining=
FFMAX(remaining, 0);
271 if(s->
maxsize>=0 && remaining+1 < size){
285 int64_t orig_pos = pkt->
pos;
286 int orig_size = pkt->
size;
290 int prev_size = pkt->
size;
310 if (ret != read_size) {
323 return pkt->
size > orig_size ? pkt->
size - orig_size :
ret;
354 int score, nodat = 0, score_max=0;
358 lpd.
buf = zerobuffer;
383 if (score > score_max) {
386 }
else if (score == score_max)
391 *score_ret= score_max;
400 if(score_ret > *score_max){
401 *score_max= score_ret;
414 static const struct {
435 for (i = 0; fmt_id_type[i].name; i++) {
436 if (!strcmp(fmt->
name, fmt_id_type[i].name)) {
466 #define PROBE_BUF_MIN 2048
467 #define PROBE_BUF_MAX (1<<20)
470 const char *filename,
void *logctx,
471 unsigned int offset,
unsigned int max_probe_size)
476 int ret = 0, probe_size, buf_offset = 0;
478 if (!max_probe_size) {
484 "Specified probe size value %u cannot be < %u\n", max_probe_size,
PROBE_BUF_MIN);
488 if (offset >= max_probe_size) {
500 probe_size =
FFMIN(probe_size<<1,
FFMAX(max_probe_size, probe_size+1))) {
504 if (probe_size < offset) {
515 if ((ret =
avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
533 av_log(logctx,
AV_LOG_WARNING,
"Format %s detected only with low score of %d, misdetection possible!\n", (*fmt)->name, score);
535 av_log(logctx,
AV_LOG_DEBUG,
"Format %s probed with size=%d and score=%d\n", (*fmt)->name, probe_size, score);
563 "will be ignored with AVFMT_NOFILE format.\n");
586 (*plast_pktl)->next = pktl;
588 *packet_buffer = pktl;
622 av_log(
NULL,
AV_LOG_ERROR,
"Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
634 if ((ret =
init_input(s, filename, &tmp)) < 0)
671 if (id3v2_extra_meta) {
793 if (!pktl || ret ==
AVERROR(EAGAIN))
808 "Dropped corrupted packet (stream = %d)\n",
842 #if FF_API_READ_PACKET
928 if (frame_size <= 0 || st->codec->sample_rate <= 0)
959 #if CONFIG_H264_DECODER
996 if (!first_program) {
1021 program = first_program;
1076 pts_buffer[0]= pktl->
pkt.
pts;
1077 for(i=0; i<delay && pts_buffer[i] > pts_buffer[i+1]; i++)
1078 FFSWAP(int64_t, pts_buffer[i], pts_buffer[i+1]);
1080 pktl->
pkt.
dts= pts_buffer[0];
1146 int num, den, presentation_delayed, delay, i;
1161 presentation_delayed = 0;
1167 presentation_delayed = 1;
1181 if(strcmp(s->
iformat->
name,
"mov,mp4,m4a,3gp,3g2,mj2"))
1226 presentation_delayed = 1;
1228 av_dlog(
NULL,
"IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%d\n",
1233 if (presentation_delayed) {
1283 av_dlog(
NULL,
"OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s\n",
1297 *pkt_buf = pktl->
next;
1301 *pkt_buf_end =
NULL;
1315 int ret = 0, got_output = 0;
1326 while (size > 0 || (pkt == &
flush_pkt && got_output)) {
1331 &out_pkt.
data, &out_pkt.
size, data, size,
1340 got_output = !!out_pkt.
size;
1383 #if FF_API_DESTRUCT_PACKET
1384 out_pkt.destruct = pkt->destruct;
1385 pkt->destruct =
NULL;
1418 *pkt_buffer = pktl->
next;
1420 *pkt_buffer_end =
NULL;
1427 int ret = 0, i, got_packet = 0;
1455 cur_pkt.
pts < cur_pkt.
dts) {
1463 av_log(s,
AV_LOG_DEBUG,
"ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1475 "%s, packets or times may be invalid.\n",
1517 av_log(s,
AV_LOG_DEBUG,
"read_frame_internal stream=%d, pts=%s, dts=%s, size=%d, duration=%d, flags=%d\n",
1554 int64_t last_dts = next_pkt->
dts;
1563 last_dts = pktl->
pkt.
dts;
1590 if (pktl && ret !=
AVERROR(EAGAIN)) {
1640 int first_audio_index = -1;
1653 first_audio_index = i;
1655 return first_audio_index >= 0 ? first_audio_index : 0;
1715 int *nb_index_entries,
1716 unsigned int *index_entries_allocated_size,
1722 if((
unsigned)*nb_index_entries + 1 >= UINT_MAX /
sizeof(
AVIndexEntry))
1732 index_entries_allocated_size,
1733 (*nb_index_entries + 1) *
1738 *index_entries= entries;
1743 index= (*nb_index_entries)++;
1744 ie= &entries[
index];
1745 av_assert0(index==0 || ie[-1].timestamp < timestamp);
1747 ie= &entries[
index];
1751 memmove(entries + index + 1, entries + index,
sizeof(
AVIndexEntry)*(*nb_index_entries - index));
1752 (*nb_index_entries)++;
1753 }
else if(ie->
pos == pos && distance < ie->min_distance)
1772 timestamp, size, distance, flags);
1776 int64_t wanted_timestamp,
int flags)
1785 if(b && entries[b-1].timestamp < wanted_timestamp)
1791 if(timestamp >= wanted_timestamp)
1793 if(timestamp <= wanted_timestamp)
1813 wanted_timestamp, flags);
1817 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1819 int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
1820 if (stream_index >= 0)
1829 int64_t ts_min, ts_max, ts;
1834 if (stream_index < 0)
1848 index=
FFMAX(index, 0);
1854 av_dlog(s,
"using cached pos_min=0x%"PRIx64
" dts_min=%s\n",
1868 av_dlog(s,
"using cached pos_max=0x%"PRIx64
" pos_limit=0x%"PRIx64
" dts_max=%s\n",
1873 pos=
ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->
read_timestamp);
1888 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1889 int64_t ts_min, int64_t ts_max,
int flags, int64_t *ts_ret,
1890 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1893 int64_t start_pos, filesize;
1900 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
1905 if(ts_min >= target_ts){
1913 pos_max = filesize - 1;
1915 pos_max =
FFMAX(0, pos_max - step);
1916 ts_max =
ff_read_timestamp(s, stream_index, &pos_max, pos_max + step, read_timestamp);
1923 int64_t tmp_pos= pos_max + 1;
1924 int64_t tmp_ts=
ff_read_timestamp(s, stream_index, &tmp_pos, INT64_MAX, read_timestamp);
1929 if(tmp_pos >= filesize)
1935 if(ts_max <= target_ts){
1940 if(ts_min > ts_max){
1942 }
else if(ts_min == ts_max){
1947 while (pos_min < pos_limit) {
1948 av_dlog(s,
"pos_min=0x%"PRIx64
" pos_max=0x%"PRIx64
" dts_min=%s dts_max=%s\n",
1950 assert(pos_limit <= pos_max);
1953 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1955 pos =
av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1956 + pos_min - approximate_keyframe_distance;
1957 }
else if(no_change==1){
1959 pos = (pos_min + pos_limit)>>1;
1967 else if(pos > pos_limit)
1976 av_dlog(s,
"%"PRId64
" %"PRId64
" %"PRId64
" / %s %s %s target:%s limit:%"PRId64
" start:%"PRId64
" noc:%d\n",
1977 pos_min, pos, pos_max,
1979 pos_limit, start_pos, no_change);
1985 if (target_ts <= ts) {
1986 pos_limit = start_pos - 1;
1990 if (target_ts >= ts) {
2000 ts_min =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2002 ts_max =
ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2003 av_dlog(s,
"pos=0x%"PRIx64
" %s<=%s<=%s\n",
2011 int64_t pos_min, pos_max;
2016 if (pos < pos_min) pos= pos_min;
2017 else if(pos > pos_max) pos= pos_max;
2027 int stream_index, int64_t timestamp,
int flags)
2034 st = s->
streams[stream_index];
2038 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
2041 if(index < 0 || index==st->nb_index_entries-1){
2059 }
while (read_status ==
AVERROR(EAGAIN));
2060 if (read_status < 0)
2067 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);
2091 int64_t timestamp,
int flags)
2103 if(stream_index < 0){
2105 if(stream_index < 0)
2139 int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
2145 flags & ~AVSEEK_FLAG_BACKWARD);
2158 if(min_ts > ts || max_ts < ts)
2160 if (stream_index < -1 || stream_index >= (
int)s->
nb_streams)
2171 if (stream_index == -1 && s->
nb_streams == 1) {
2178 time_base.
num * (int64_t)AV_TIME_BASE,
2198 if (ret<0 && ts != min_ts && max_ts != ts) {
2199 ret =
av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2239 int64_t
start_time, start_time1, start_time_text, end_time, end_time1;
2240 int64_t
duration, duration1, filesize;
2245 start_time = INT64_MAX;
2246 start_time_text = INT64_MAX;
2247 end_time = INT64_MIN;
2248 duration = INT64_MIN;
2254 if (start_time1 < start_time_text)
2255 start_time_text = start_time1;
2257 start_time =
FFMIN(start_time, start_time1);
2260 end_time1 = start_time1
2262 end_time =
FFMAX(end_time, end_time1);
2273 duration =
FFMAX(duration, duration1);
2276 if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text <
AV_TIME_BASE))
2277 start_time = start_time_text;
2278 else if(start_time > start_time_text)
2281 if (start_time != INT64_MAX) {
2283 if (end_time != INT64_MIN) {
2291 duration =
FFMAX(duration, end_time - start_time);
2299 double bitrate = (double)filesize * 8.0 * AV_TIME_BASE /
2301 if (bitrate >= 0 && bitrate <= INT_MAX)
2326 int bit_rate, i, show_warning = 0;
2360 #define DURATION_MAX_READ_SIZE 250000LL
2361 #define DURATION_MAX_RETRY 4
2368 int read_size, i,
ret;
2404 }
while(ret ==
AVERROR(EAGAIN));
2407 read_size += pkt->
size;
2412 duration = end_time = pkt->
pts;
2450 file_size =
FFMAX(0, file_size);
2476 av_dlog(ic,
"%d: start_time: %0.3f duration: %0.3f\n", i,
2480 av_dlog(ic,
"stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2491 #define FAIL(errmsg) do { \
2493 *errmsg_ptr = errmsg; \
2500 FAIL(
"unspecified frame size");
2502 FAIL(
"unspecified sample format");
2504 FAIL(
"unspecified sample rate");
2506 FAIL(
"unspecified number of channels");
2508 FAIL(
"no decodable DTS frames");
2512 FAIL(
"unspecified size");
2514 FAIL(
"unspecified pixel format");
2517 FAIL(
"no frame in rv30/40 and no sar");
2521 FAIL(
"unspecified size");
2528 FAIL(
"unknown codec");
2536 int got_picture = 1,
ret = 0;
2558 av_dict_set(options ? options : &thread_opt,
"threads",
"1", 0);
2575 while ((pkt.
size > 0 || (!pkt.
data && got_picture)) &&
2585 &got_picture, &pkt);
2592 &got_picture, &pkt);
2607 if(!pkt.
data && !got_picture)
2629 if(tag == tags[i].tag)
2650 if (sflags & (1 << (bps - 1))) {
2682 for(i=0; tags && tags[i]; i++){
2685 if (codec_tags->
id ==
id) {
2686 *tag = codec_tags->
tag;
2698 for(i=0; tags && tags[i]; i++){
2719 if (j != i && next_start > ch->
start && next_start < end)
2722 ch->
end = (end == INT64_MAX) ? ch->
start : end;
2727 if(i<60*12)
return (i+1)*1001;
2728 else return ((
const int[]){24,30,60,12,15,48})[i-60*12]*1000*12;
2752 #if FF_API_FORMAT_PARAMETERS
2795 "%s, packets or times may be invalid.\n",
2804 av_dict_set(options ? &options[i] : &thread_opt,
"threads",
"1", 0);
2823 #if FF_API_R_FRAME_RATE
2841 int fps_analyze_framecount = 20;
2850 fps_analyze_framecount *= 2;
2854 fps_analyze_framecount = 0;
2887 "Stream #%d: not enough frames to estimate rate; "
2888 "consider increasing probesize\n", i);
2909 goto find_stream_info_err;
2912 read_size += pkt->
size;
2920 "packet %d with DTS %"PRId64
", packet %d with DTS "
2933 "packet %d with DTS %"PRId64
", packet %d with DTS "
2963 #if FF_API_R_FRAME_RATE
2968 && pkt->
dts - (uint64_t)last < INT64_MAX){
2979 double sdts= dts*framerate/(1001*12);
2981 int64_t ticks=
llrint(sdts+j*0.5);
2982 double error= sdts - ticks + j*0.5;
3038 (options && i < orig_nb_streams) ?
3039 &options[i] :
NULL);
3044 "decoding for stream %d failed\n", st->
index);
3052 "Could not find codec parameters for stream %d (%s): %s\n"
3053 "Consider increasing the value for the 'analyzeduration' and 'probesize' options\n",
3078 double best_error = 0.01;
3090 if (error < best_error) {
3092 best_fps = std_fps.
num;
3097 best_fps, 12*1001, INT_MAX);
3108 double best_error= 0.01;
3122 if(error < best_error && best_error> 0.000000001){
3169 find_stream_info_err:
3202 int wanted_stream_nb,
3209 unsigned *program =
NULL;
3212 if (related_stream >= 0 && wanted_stream_nb < 0) {
3219 for (i = 0; i < nb_streams; i++) {
3220 int real_stream_index = program ? program[i] : i;
3225 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
3240 if ((best_multiframe > multiframe) ||
3241 (best_multiframe == multiframe && best_bitrate > bitrate) ||
3242 (best_multiframe == multiframe && best_bitrate == bitrate && best_count >=
count))
3245 best_bitrate = bitrate;
3246 best_multiframe = multiframe;
3247 ret = real_stream_index;
3249 if (program && i == nb_streams - 1 && ret < 0) {
3256 *decoder_ret = best_decoder;
3333 #if FF_API_CLOSE_INPUT_FILE
3363 #if FF_API_NEW_STREAM
3379 if (s->
nb_streams >= INT_MAX/
sizeof(*streams))
3422 #if FF_API_R_FRAME_RATE
3437 av_dlog(ac,
"new_program: id=0x%04x\n",
id);
3526 if(strcmp(
"language", tag->
key)){
3527 const char *p = tag->
value;
3531 size_t len = strcspn(p,
"\x8\xa\xb\xc\xd");
3572 display_aspect_ratio.
num, display_aspect_ratio.
den);
3577 #if FF_API_R_FRAME_RATE
3621 is_output ?
"Output" :
"Input",
3624 is_output ?
"to" :
"from", url);
3629 int hours, mins, secs, us;
3667 int j, k, total = 0;
3672 name ? name->
value :
"");
3680 if (total < ic->nb_streams)
3696 const char *path,
int number)
3699 char *q, buf1[20],
c;
3700 int nd,
len, percentd_found;
3713 nd = nd * 10 + *p++ -
'0';
3725 snprintf(buf1,
sizeof(buf1),
"%0*d", nd, number);
3727 if ((q - buf + len) > buf_size - 1)
3729 memcpy(q, buf1, len);
3737 if ((q - buf) < buf_size - 1)
3741 if (!percentd_found)
3754 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3756 for(i=0;i<
size;i+=16) {
3763 PRINT(
" %02x", buf[i+j]);
3768 for(j=0;j<
len;j++) {
3770 if (c < ' ' || c >
'~')
3791 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3815 void av_pkt_dump(FILE *f,
AVPacket *
pkt,
int dump_payload)
3828 void av_pkt_dump_log(
void *avcl,
int level,
AVPacket *
pkt,
int dump_payload)
3842 char *authorization,
int authorization_size,
3843 char *hostname,
int hostname_size,
3845 char *path,
int path_size,
3848 const char *p, *ls, *ls2, *at, *at2, *col, *brk;
3850 if (port_ptr) *port_ptr = -1;
3851 if (proto_size > 0) proto[0] = 0;
3852 if (authorization_size > 0) authorization[0] = 0;
3853 if (hostname_size > 0) hostname[0] = 0;
3854 if (path_size > 0) path[0] = 0;
3857 if ((p = strchr(url,
':'))) {
3869 ls = strchr(p,
'/');
3870 ls2 = strchr(p,
'?');
3874 ls =
FFMIN(ls, ls2);
3884 while ((at = strchr(p,
'@')) && at < ls) {
3886 FFMIN(authorization_size, at + 1 - at2));
3890 if (*p ==
'[' && (brk = strchr(p,
']')) && brk < ls) {
3893 FFMIN(hostname_size, brk - p));
3894 if (brk[1] ==
':' && port_ptr)
3895 *port_ptr = atoi(brk + 2);
3896 }
else if ((col = strchr(p,
':')) && col < ls) {
3898 FFMIN(col + 1 - p, hostname_size));
3899 if (port_ptr) *port_ptr = atoi(col + 1);
3902 FFMIN(ls + 1 - p, hostname_size));
3909 static const char hex_table_uc[16] = {
'0',
'1',
'2',
'3',
3912 'C',
'D',
'E',
'F' };
3913 static const char hex_table_lc[16] = {
'0',
'1',
'2',
'3',
3916 'c',
'd',
'e',
'f' };
3917 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3919 for(i = 0; i <
s; i++) {
3920 buff[i * 2] = hex_table[src[i] >> 4];
3921 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3938 if (c >=
'0' && c <=
'9')
3940 else if (c >=
'A' && c <=
'F')
3955 #if FF_API_SET_PTS_INFO
3956 void av_set_pts_info(
AVStream *s,
int pts_wrap_bits,
3957 unsigned int pts_num,
unsigned int pts_den)
3964 unsigned int pts_num,
unsigned int pts_den)
3968 if(new_tb.
num != pts_num)
3973 if(new_tb.
num <= 0 || new_tb.
den <= 0) {
3983 const char *authorization,
const char *hostname,
3984 int port,
const char *
fmt, ...)
3987 struct addrinfo hints = { 0 }, *ai;
3993 if (authorization && authorization[0])
3995 #if CONFIG_NETWORK && defined(AF_INET6)
4000 if (ai->ai_family == AF_INET6) {
4017 int len = strlen(str);
4020 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);