67 #define LICENSE_PREFIX "libavformat license: "
114 ext = strrchr(filename,
'.');
120 while (*p !=
'\0' && *p !=
',' && q-ext1<
sizeof(ext1)-1)
141 namelen = strlen(name);
142 while ((p = strchr(names,
','))) {
143 len =
FFMAX(p - names, namelen);
152 const char *mime_type)
155 int score_max, score;
158 #if CONFIG_IMAGE2_MUXER
159 if (!short_name && filename &&
178 if (score > score_max) {
187 const char *filename,
const char *mime_type,
enum AVMediaType type){
191 #if CONFIG_IMAGE2_MUXER
192 if(!strcmp(fmt->
name,
"image2") || !strcmp(fmt->
name,
"image2pipe")){
242 old_size = pkt->
size;
285 if (score > *score_max) {
288 }
else if (score == *score_max)
303 if (fmt->extensions &&
av_match_ext(
"mp3", fmt->extensions)) {
319 static const struct {
338 for (i = 0; fmt_id_type[i].name; i++) {
339 if (!strcmp(fmt->
name, fmt_id_type[i].name)) {
353 #define PROBE_BUF_MIN 2048
354 #define PROBE_BUF_MAX (1<<20)
357 const char *filename,
void *logctx,
358 unsigned int offset,
unsigned int max_probe_size)
361 unsigned char *buf =
NULL;
362 int ret = 0, probe_size;
364 if (!max_probe_size) {
372 if (offset >= max_probe_size) {
376 for(probe_size=
PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
377 probe_size =
FFMIN(probe_size<<1,
FFMAX(max_probe_size, probe_size+1))) {
379 int buf_offset = (probe_size ==
PROBE_BUF_MIN) ? 0 : probe_size>>1;
381 if (probe_size < offset) {
387 if ((ret =
avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
397 pd.
buf = &buf[offset];
405 av_log(logctx,
AV_LOG_WARNING,
"Format detected only with low score of %d, misdetection possible!\n", score);
407 av_log(logctx,
AV_LOG_DEBUG,
"Probed with size=%d and score=%d\n", probe_size, score);
457 (*plast_pktl)->next = pktl;
459 *packet_buffer = pktl;
497 if ((ret =
init_input(s, filename, &tmp)) < 0)
533 if (id3v2_extra_meta &&
629 if (!pktl || ret ==
AVERROR(EAGAIN))
643 "Dropped corrupted packet (stream = %d)\n",
674 #if FF_API_READ_PACKET
742 if (frame_size <= 0 || st->codec->sample_rate <= 0)
763 int64_t dts, int64_t pts)
774 for(; pktl; pktl= pktl->
next){
799 for(; pktl; pktl= pktl->
next){
811 for(; pktl; pktl= pktl->
next){
832 int num, den, presentation_delayed, delay, i;
843 presentation_delayed = 0;
849 presentation_delayed = 1;
906 presentation_delayed = 1;
909 "IN delayed:%d pts:%"PRId64
", dts:%"PRId64
" cur_dts:%"PRId64
" st:%d pc:%p\n",
915 if (presentation_delayed) {
982 "OUTdelayed:%d/%d pts:%"PRId64
", dts:%"PRId64
" cur_dts:%"PRId64
"\n",
983 presentation_delayed, delay, pkt->
pts, pkt->
dts, st->
cur_dts);
996 *pkt_buf = pktl->
next;
1000 *pkt_buf_end =
NULL;
1014 int ret = 0, got_output = 0;
1022 while (size > 0 || (pkt == &
flush_pkt && got_output)) {
1027 &out_pkt.
data, &out_pkt.
size, data, size,
1035 got_output = !!out_pkt.
size;
1110 *pkt_buffer = pktl->
next;
1112 *pkt_buffer_end =
NULL;
1119 int ret = 0, i, got_packet = 0;
1147 cur_pkt.
pts < cur_pkt.
dts) {
1148 av_log(s,
AV_LOG_WARNING,
"Invalid timestamps stream=%d, pts=%"PRId64
", dts=%"PRId64
", size=%d\n",
1155 av_log(s,
AV_LOG_DEBUG,
"ff_read_packet stream=%d, pts=%"PRId64
", dts=%"PRId64
", size=%d, duration=%d, flags=%d\n",
1198 av_log(s,
AV_LOG_DEBUG,
"read_frame_internal stream=%d, pts=%"PRId64
", dts=%"PRId64
", size=%d, duration=%d, flags=%d\n",
1249 if (pktl && ret !=
AVERROR(EAGAIN)) {
1277 int first_audio_index = -1;
1290 first_audio_index = i;
1292 return first_audio_index >= 0 ? first_audio_index : 0;
1351 int *nb_index_entries,
1352 unsigned int *index_entries_allocated_size,
1358 if((
unsigned)*nb_index_entries + 1 >= UINT_MAX /
sizeof(
AVIndexEntry))
1362 index_entries_allocated_size,
1363 (*nb_index_entries + 1) *
1368 *index_entries= entries;
1373 index= (*nb_index_entries)++;
1374 ie= &entries[
index];
1375 assert(index==0 || ie[-1].timestamp < timestamp);
1377 ie= &entries[
index];
1381 memmove(entries + index + 1, entries + index,
sizeof(
AVIndexEntry)*(*nb_index_entries - index));
1382 (*nb_index_entries)++;
1383 }
else if(ie->
pos == pos && distance < ie->min_distance)
1401 timestamp, size, distance, flags);
1405 int64_t wanted_timestamp,
int flags)
1414 if(b && entries[b-1].timestamp < wanted_timestamp)
1420 if(timestamp >= wanted_timestamp)
1422 if(timestamp <= wanted_timestamp)
1442 wanted_timestamp, flags);
1449 int64_t ts_min, ts_max, ts;
1454 if (stream_index < 0)
1457 av_dlog(s,
"read_seek: %d %"PRId64
"\n", stream_index, target_ts);
1468 index=
FFMAX(index, 0);
1474 av_dlog(s,
"using cached pos_min=0x%"PRIx64
" dts_min=%"PRId64
"\n",
1481 assert(index < st->nb_index_entries);
1488 av_dlog(s,
"using cached pos_max=0x%"PRIx64
" pos_limit=0x%"PRIx64
" dts_max=%"PRId64
"\n",
1489 pos_max,pos_limit, ts_max);
1493 pos=
ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->
read_timestamp);
1507 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1508 int64_t ts_min, int64_t ts_max,
int flags, int64_t *ts_ret,
1509 int64_t (*read_timestamp)(
struct AVFormatContext *,
int , int64_t *, int64_t ))
1512 int64_t start_pos, filesize;
1515 av_dlog(s,
"gen_seek: %d %"PRId64
"\n", stream_index, target_ts);
1519 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1527 pos_max = filesize - 1;
1530 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
1537 int64_t tmp_pos= pos_max + 1;
1538 int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
1543 if(tmp_pos >= filesize)
1549 if(ts_min > ts_max){
1551 }
else if(ts_min == ts_max){
1556 while (pos_min < pos_limit) {
1557 av_dlog(s,
"pos_min=0x%"PRIx64
" pos_max=0x%"PRIx64
" dts_min=%"PRId64
" dts_max=%"PRId64
"\n",
1558 pos_min, pos_max, ts_min, ts_max);
1559 assert(pos_limit <= pos_max);
1562 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1564 pos =
av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1565 + pos_min - approximate_keyframe_distance;
1566 }
else if(no_change==1){
1568 pos = (pos_min + pos_limit)>>1;
1576 else if(pos > pos_limit)
1580 ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
1585 av_dlog(s,
"%"PRId64
" %"PRId64
" %"PRId64
" / %"PRId64
" %"PRId64
" %"PRId64
" target:%"PRId64
" limit:%"PRId64
" start:%"PRId64
" noc:%d\n",
1586 pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1587 pos_limit, start_pos, no_change);
1593 if (target_ts <= ts) {
1594 pos_limit = start_pos - 1;
1598 if (target_ts >= ts) {
1607 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1609 ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1610 av_dlog(s,
"pos=0x%"PRIx64
" %"PRId64
"<=%"PRId64
"<=%"PRId64
"\n",
1611 pos, ts_min, target_ts, ts_max);
1617 int64_t pos_min, pos_max;
1622 if (pos < pos_min) pos= pos_min;
1623 else if(pos > pos_max) pos= pos_max;
1631 int stream_index, int64_t timestamp,
int flags)
1638 st = s->
streams[stream_index];
1642 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1645 if(index < 0 || index==st->nb_index_entries-1){
1662 }
while (read_status ==
AVERROR(EAGAIN));
1663 if (read_status < 0)
1690 int64_t timestamp,
int flags)
1702 if(stream_index < 0){
1704 if(stream_index < 0)
1745 if(min_ts > ts || max_ts < ts)
1799 int64_t
start_time, start_time1, end_time, end_time1;
1800 int64_t
duration, duration1, filesize;
1804 start_time = INT64_MAX;
1805 end_time = INT64_MIN;
1806 duration = INT64_MIN;
1811 start_time =
FFMIN(start_time, start_time1);
1813 end_time1 = start_time1
1815 end_time =
FFMAX(end_time, end_time1);
1820 duration =
FFMAX(duration, duration1);
1823 if (start_time != INT64_MAX) {
1825 if (end_time != INT64_MIN)
1826 duration =
FFMAX(duration, end_time - start_time);
1828 if (duration != INT64_MIN) {
1887 #define DURATION_MAX_READ_SIZE 250000
1888 #define DURATION_MAX_RETRY 3
1895 int read_size, i, ret;
1897 int64_t filesize, offset,
duration;
1931 }
while(ret ==
AVERROR(EAGAIN));
1934 read_size += pkt->
size;
1939 duration = end_time = pkt->
pts;
1977 file_size =
FFMAX(0, file_size);
2001 av_dlog(ic,
"%d: start_time: %0.3f duration: %0.3f\n", i,
2005 av_dlog(ic,
"stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2044 int got_picture = 1, ret = 0;
2065 av_dict_set(options ? options : &thread_opt,
"threads",
"1", 0);
2082 while ((pkt.
size > 0 || (!pkt.
data && got_picture)) &&
2092 &got_picture, &pkt);
2128 if(tag == tags[i].tag)
2148 if (sflags & (1 << (bps - 1))) {
2171 for(i=0; tags && tags[i]; i++){
2181 for(i=0; tags && tags[i]; i++){
2202 if (j != i && next_start > ch->
start && next_start < end)
2205 ch->
end = (end == INT64_MAX) ? ch->
start : end;
2210 if(i<60*12)
return i*1001;
2211 else return ((
const int[]){24,30,60,12,15})[i-60*12]*1000*12;
2236 int i, count, ret, read_size, j;
2259 av_dict_set(options ? &options[i] : &thread_opt,
"threads",
"1", 0);
2278 #if FF_API_R_FRAME_RATE
2296 int fps_analyze_framecount = 20;
2305 fps_analyze_framecount *= 2;
2358 (options && i < orig_nb_streams) ?
2359 &options[i] :
NULL);
2365 "decoding for stream %d failed\n", st->
index);
2370 "Could not find codec parameters (%s)\n", buf);
2384 goto find_stream_info_err;
2387 read_size += pkt->
size;
2395 "packet %d with DTS %"PRId64
", packet %d with DTS "
2408 "packet %d with DTS %"PRId64
", packet %d with DTS "
2429 #if FF_API_R_FRAME_RATE
2431 int64_t last = st->
info->last_dts;
2437 if (st->
info->duration_count < 2)
2438 memset(st->
info->duration_error, 0,
sizeof(st->
info->duration_error));
2441 int ticks=
lrintf(dur*framerate/(1001*12));
2442 double error = dur - (double)ticks*1001*12 / framerate;
2443 st->
info->duration_error[i] += error*error;
2445 st->
info->duration_count++;
2447 if (st->
info->duration_count > 3)
2448 st->
info->duration_gcd =
av_gcd(st->
info->duration_gcd, duration);
2451 st->
info->last_dts = pkt->
dts;
2494 double best_error = 0.01;
2506 if (error < best_error) {
2508 best_fps = std_fps.
num;
2513 best_fps, 12*1001, INT_MAX);
2516 #if FF_API_R_FRAME_RATE
2522 if (st->
info->duration_count && !st->r_frame_rate.num
2526 best_error = best_error*best_error*st->
info->duration_count*1000*12*30;
2530 if(error < best_error){
2536 if (num && (!st->r_frame_rate.num || (
double)num/(12*1001) < 1.01 *
av_q2d(st->r_frame_rate)))
2537 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2563 find_stream_info_err:
2585 int wanted_stream_nb,
2592 unsigned *program =
NULL;
2595 if (related_stream >= 0 && wanted_stream_nb < 0) {
2602 for (i = 0; i < nb_streams; i++) {
2603 int real_stream_index = program ? program[i] : i;
2608 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2623 ret = real_stream_index;
2625 if (program && i == nb_streams - 1 && ret < 0) {
2632 *decoder_ret = best_decoder;
2700 #if FF_API_CLOSE_INPUT_FILE
2736 if (s->
nb_streams >= INT_MAX/
sizeof(*streams))
2776 #if FF_API_R_FRAME_RATE
2791 av_dlog(ac,
"new_program: id=0x%04x\n",
id);
2827 chapter->
start = start;
2862 uint64_t v=
lrintf(d*100);
2875 if(strcmp(
"language", tag->
key))
2908 display_aspect_ratio.
num, display_aspect_ratio.
den);
2913 #if FF_API_R_FRAME_RATE
2914 if(st->r_frame_rate.den && st->r_frame_rate.num)
2957 is_output ?
"Output" :
"Input",
2960 is_output ?
"to" :
"from", url);
2965 int hours, mins, secs, us;
3002 int j, k, total = 0;
3007 name ? name->
value :
"");
3015 if (total < ic->nb_streams)
3025 #if FF_API_AV_GETTIME && CONFIG_SHARED && HAVE_SYMVER
3026 FF_SYMVER(int64_t,
av_gettime, (
void),
"LIBAVFORMAT_54")
3038 const char *path,
int number)
3041 char *q, buf1[20], c;
3042 int nd,
len, percentd_found;
3054 while (isdigit(*p)) {
3055 nd = nd * 10 + *p++ -
'0';
3058 }
while (isdigit(c));
3067 snprintf(buf1,
sizeof(buf1),
"%0*d", nd, number);
3069 if ((q - buf + len) > buf_size - 1)
3071 memcpy(q, buf1, len);
3079 if ((q - buf) < buf_size - 1)
3083 if (!percentd_found)
3096 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3098 for(i=0;i<
size;i+=16) {
3105 PRINT(
" %02x", buf[i+j]);
3110 for(j=0;j<
len;j++) {
3112 if (c < ' ' || c >
'~')
3133 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3168 char *authorization,
int authorization_size,
3169 char *hostname,
int hostname_size,
3171 char *path,
int path_size,
3174 const char *p, *ls, *at, *col, *brk;
3176 if (port_ptr) *port_ptr = -1;
3177 if (proto_size > 0) proto[0] = 0;
3178 if (authorization_size > 0) authorization[0] = 0;
3179 if (hostname_size > 0) hostname[0] = 0;
3180 if (path_size > 0) path[0] = 0;
3183 if ((p = strchr(url,
':'))) {
3195 ls = strchr(p,
'/');
3197 ls = strchr(p,
'?');
3206 if ((at = strchr(p,
'@')) && at < ls) {
3208 FFMIN(authorization_size, at + 1 - p));
3212 if (*p ==
'[' && (brk = strchr(p,
']')) && brk < ls) {
3215 FFMIN(hostname_size, brk - p));
3216 if (brk[1] ==
':' && port_ptr)
3217 *port_ptr = atoi(brk + 2);
3218 }
else if ((col = strchr(p,
':')) && col < ls) {
3220 FFMIN(col + 1 - p, hostname_size));
3221 if (port_ptr) *port_ptr = atoi(col + 1);
3224 FFMIN(ls + 1 - p, hostname_size));
3231 static const char hex_table_uc[16] = {
'0',
'1',
'2',
'3',
3234 'C',
'D',
'E',
'F' };
3235 static const char hex_table_lc[16] = {
'0',
'1',
'2',
'3',
3238 'c',
'd',
'e',
'f' };
3239 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3241 for(i = 0; i < s; i++) {
3242 buff[i * 2] = hex_table[src[i] >> 4];
3243 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3259 c = toupper((
unsigned char) *p++);
3260 if (c >=
'0' && c <=
'9')
3262 else if (c >=
'A' && c <=
'F')
3278 unsigned int pts_num,
unsigned int pts_den)
3282 if(new_tb.
num != pts_num)
3287 if(new_tb.
num <= 0 || new_tb.
den <= 0) {
3296 const char *authorization,
const char *hostname,
3297 int port,
const char *fmt, ...)
3300 struct addrinfo hints = { 0 }, *ai;
3306 if (authorization && authorization[0])
3308 #if CONFIG_NETWORK && defined(AF_INET6)
3313 if (ai->ai_family == AF_INET6) {
3330 int len = strlen(str);
3333 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
3360 const char *ptr = str;
3365 char *dest =
NULL, *dest_end;
3366 int key_len, dest_len = 0;
3369 while (*ptr && (isspace(*ptr) || *ptr ==
','))
3376 if (!(ptr = strchr(key,
'=')))
3379 key_len = ptr - key;
3381 callback_get_buf(context, key, key_len, &dest, &dest_len);
3382 dest_end = dest + dest_len - 1;
3386 while (*ptr && *ptr !=
'\"') {
3390 if (dest && dest < dest_end)
3394 if (dest && dest < dest_end)
3402 for (; *ptr && !(isspace(*ptr) || *ptr ==
','); ptr++)
3403 if (dest && dest < dest_end)
3424 char *sep, *path_query;
3426 if (base && strstr(base,
"://") && rel[0] ==
'/') {
3429 sep = strstr(buf,
"://");
3432 if (rel[1] ==
'/') {
3437 sep = strchr(sep,
'/');
3446 if (!base || strstr(rel,
"://") || rel[0] ==
'/') {
3454 path_query = strchr(buf,
'?');
3455 if (path_query !=
NULL)
3459 if (rel[0] ==
'?') {
3465 sep = strrchr(buf,
'/');
3473 sep = strrchr(buf,
'/');
3475 if (!strcmp(sep ? &sep[1] : buf,
"..")) {
3493 struct tm time1 = {0}, time2 = {0};
3495 ret1 = strptime(datestr,
"%Y - %m - %d %T", &time1);
3496 ret2 = strptime(datestr,
"%Y - %m - %dT%T", &time2);
3503 "the date string.\n");
3512 return ofmt->
query_codec(codec_id, std_compliance);
3544 uint64_t channel_layout,
int32_t sample_rate,
3556 if (channel_layout) {
3564 if (width || height) {
3571 bytestream_put_le32(&data, flags);
3573 bytestream_put_le32(&data, channels);
3575 bytestream_put_le64(&data, channel_layout);
3577 bytestream_put_le32(&data, sample_rate);
3578 if (width || height) {
3579 bytestream_put_le32(&data, width);
3580 bytestream_put_le32(&data, height);