71 #if HAVE_SYS_RESOURCE_H
73 #include <sys/types.h>
74 #include <sys/resource.h>
75 #elif HAVE_GETPROCESSTIMES
78 #if HAVE_GETPROCESSMEMORYINFO
84 #include <sys/select.h>
89 #include <sys/ioctl.h>
142 static int transcoding_finished;
145 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
163 static struct termios oldtty;
164 static int restore_tty;
193 uint32_t *pal, *dst2;
201 if (r->
x < 0 || r->
x + r->
w > w || r->
y < 0 || r->
y + r->
h > h) {
206 dst += r->
y * dst_linesize + r->
x * 4;
209 for (y = 0; y < r->
h; y++) {
210 dst2 = (uint32_t *)dst;
212 for (x = 0; x < r->
w; x++)
213 *(dst2++) = pal[*(src2++)];
239 int64_t pts, end_pts;
256 "Impossible to get a blank canvas.\n");
259 dst = frame->
data [0];
261 for (i = 0; i < num_rects; i++)
285 if (pts2 <= ist2->sub2video.last_pts)
289 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
311 tcsetattr (0, TCSANOW, &oldtty);
336 istty = isatty(0) && isatty(2);
338 if (istty && tcgetattr (0, &tty) == 0) {
342 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
343 |INLCR|IGNCR|ICRNL|IXON);
344 tty.c_oflag |= OPOST;
345 tty.c_lflag &= ~(
ECHO|ECHONL|ICANON|IEXTEN);
346 tty.c_cflag &= ~(CSIZE|PARENB);
351 tcsetattr (0, TCSANOW, &tty);
378 n = select(1, &rfds, NULL, NULL, &tv);
387 # if HAVE_PEEKNAMEDPIPE
389 static HANDLE input_handle;
392 input_handle = GetStdHandle(STD_INPUT_HANDLE);
393 is_pipe = !GetConsoleMode(input_handle, &dw);
396 if (stdin->_cnt > 0) {
402 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
434 printf(
"bench: maxrss=%ikB\n", maxrss);
439 for (j = 0; j < filtergraphs[i]->
nb_inputs; j++) {
444 for (j = 0; j < filtergraphs[i]->
nb_outputs; j++) {
449 av_freep(&filtergraphs[i]->graph_desc);
477 av_freep(&output_streams[i]->forced_keyframes);
478 av_expr_free(output_streams[i]->forced_keyframes_pexpr);
479 av_freep(&output_streams[i]->avfilter);
480 av_freep(&output_streams[i]->logfile_prefix);
497 av_freep(&input_streams[i]->hwaccel_device);
546 printf(
"bench: %8"PRIu64
" %s \n", t -
current_time, buf);
557 ost2->
finished |= ost == ost2 ? this_stream : others;
595 memcpy(t, new_pkt.
data, new_pkt.
size);
610 av_log(NULL,
AV_LOG_ERROR,
"Failed to open bitstream filter %s for stream %d with codec %s",
627 if (pkt->
dts < max) {
629 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
630 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
636 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
637 "in incorrect timestamps in the output file.\n",
650 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
712 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
732 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
749 int subtitle_out_max_size = 1024 * 1024;
750 int subtitle_out_size, nb, i;
780 for (i = 0; i < nb; i++) {
793 subtitle_out_max_size, sub);
794 if (subtitle_out_size < 0) {
801 pkt.
size = subtitle_out_size;
808 pkt.
pts += 90 * sub->start_display_time;
810 pkt.
pts += 90 * sub->end_display_time;
821 int ret, format_video_sync;
825 double sync_ipts,
delta;
836 sync_ipts = in_picture->
pts;
859 switch (format_video_sync) {
861 if (ost->
frame_number == 0 && delta - duration >= 0.5) {
870 else if (delta > 1.1)
871 nb_frames =
lrintf(delta);
876 else if (delta > 0.6)
888 if (nb_frames == 0) {
892 }
else if (nb_frames > 1) {
894 av_log(NULL,
AV_LOG_ERROR,
"%d frame duplication too large, skipping\n", nb_frames - 1);
903 for (i = 0; i < nb_frames; i++) {
936 int got_packet, forced_keyframe = 0;
966 av_dlog(NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
984 if (forced_keyframe) {
992 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1007 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1022 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1027 frame_size = pkt.
size;
1053 return -10.0 * log(d) / log(10.0);
1060 double ti1, bitrate, avg_bitrate;
1085 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1086 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1087 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1100 AVFrame *filtered_frame = NULL;
1127 "Error in av_buffersink_get_frame_flags(): %s\n",
av_err2str(ret));
1138 filtered_frame->
pts = frame_pts =
1147 filtered_frame->
pts = frame_pts;
1160 filtered_frame->
pts = frame_pts;
1164 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1189 int frame_number, vid, i;
1191 int64_t pts = INT64_MIN;
1192 static int64_t last_time = -1;
1193 static int qp_histogram[52];
1194 int hours, mins, secs, us;
1196 if (!
print_stats && !is_last_report && !progress_avio)
1199 if (!is_last_report) {
1200 if (last_time == -1) {
1204 if ((cur_time - last_time) < 500000)
1210 oc = output_files[0]->
ctx;
1213 if (total_size <= 0)
1221 ost = output_streams[i];
1226 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1227 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1231 float fps,
t = (cur_time-timer_start) / 1000000.0;
1234 fps = t > 1 ? frame_number / t : 0;
1235 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1236 frame_number, fps < 9.95, fps, q);
1237 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1239 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1242 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1248 for (j = 0; j < 32; j++)
1249 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1253 double error, error_sum = 0;
1254 double scale, scale_sum = 0;
1256 char type[3] = {
'Y',
'U',
'V' };
1257 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1258 for (j = 0; j < 3; j++) {
1259 if (is_last_report) {
1260 error = enc->
error[j];
1261 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1270 p =
psnr(error / scale);
1271 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1272 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1275 p =
psnr(error_sum / scale_sum);
1276 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1277 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1295 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1297 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1299 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1300 "size=%8.0fkB time=", total_size / 1024.0);
1301 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1302 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1304 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1306 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1307 "bitrate=%6.1fkbits/s", bitrate);
1308 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1309 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1310 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1311 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1312 hours, mins, secs, us);
1315 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1322 fprintf(stderr,
"%s \r", buf);
1329 if (progress_avio) {
1331 is_last_report ?
"end" :
"continue");
1333 FFMIN(buf_script.len, buf_script.size - 1));
1336 if (is_last_report) {
1338 progress_avio = NULL;
1342 if (is_last_report) {
1344 float percent = 0.0;
1347 percent = 100.0 * (total_size - raw) / raw;
1350 av_log(NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f data:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1355 extra_size / 1024.0,
1358 av_log(NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1371 int stop_encoding = 0;
1410 ret = encode(enc, &pkt, NULL, &got_packet);
1434 pkt_size = pkt.
size;
1530 opkt.
dts -= ost_tb_start_time;
1551 &opkt.data, &opkt.size,
1566 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1573 ost->st->codec->frame_number++;
1581 char layout_name[256];
1600 int i,
ret, err = 0, resample_changed;
1618 if (*got_output || ret<0 || pkt->
size)
1621 if (!*got_output || ret < 0) {
1646 if (resample_changed) {
1647 char layout1[64], layout2[64];
1651 "layout for Input Stream #%d.%d\n", ist->
file_index,
1663 "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n",
1703 decoded_frame->
pts = pkt->
pts;
1707 decoded_frame->
pts = ist->
dts;
1713 (
AVRational){1, ist->st->codec->sample_rate});
1714 for (i = 0; i < ist->nb_filters; i++) {
1715 if (i < ist->nb_filters - 1) {
1716 f = ist->filter_frame;
1733 return err < 0 ? err :
ret;
1739 int i,
ret = 0, err = 0, resample_changed;
1740 int64_t best_effort_timestamp;
1752 decoded_frame, got_output, pkt);
1755 if (*got_output || ret<0 || pkt->
size)
1758 if (!*got_output || ret < 0) {
1786 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d time_base:%d/%d\n",
1789 best_effort_timestamp,
1803 if (resample_changed) {
1805 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1825 if (!frame_sample_aspect->
num)
1828 if (i < ist->nb_filters - 1) {
1838 }
else if (ret < 0) {
1840 "Failed to inject frame into filter network: %s\n",
av_err2str(ret));
1848 return err < 0 ? err :
ret;
1855 &subtitle, got_output, pkt);
1857 if (*got_output || ret<0 || pkt->
size)
1860 if (ret < 0 || !*got_output) {
1871 if (end < ist->prev_sub.subtitle.end_display_time) {
1873 "Subtitle duration reduced from %d to %d%s\n",
1875 end <= 0 ?
", dropping it" :
"");
1956 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
2057 avc[i] = output_files[i]->ctx;
2060 printf(
"SDP:\n%s\n", sdp);
2069 if (
hwaccels[i].pix_fmt == pix_fmt)
2080 for (p = pix_fmts; *p != -1; p++) {
2093 ret = hwaccel->
init(s);
2097 "%s hwaccel requested for input stream #%d:%d, "
2098 "but cannot be initialized.\n", hwaccel->
name,
2130 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
2149 "Error while opening decoder for input stream "
2172 int64_t va = *(int64_t *)a, vb = *(int64_t *)
b;
2173 return va < vb ? -1 : va > vb ? +1 : 0;
2183 for (p = kf; *p; p++)
2194 for (i = 0; i <
n; i++) {
2195 char *next = strchr(p,
',');
2200 if (!memcmp(p,
"chapters", 8)) {
2209 "Could not allocate forced key frames array.\n");
2241 InputFile *file = input_files[input_index];
2247 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
2256 int ret = 0, i, j, k;
2273 if (fg->
inputs[0]->
ist == input_streams[k])
2289 oc = output_files[i]->
ctx;
2305 ost = output_streams[i];
2339 if (extra_size > INT_MAX) {
2348 unsigned int codec_tag;
2395 if( copy_tb<0 && icodec->time_base.den
2423 av_log(NULL,
AV_LOG_FATAL,
"-acodec copy and -vol are incompatible (frames are not decoded)\n");
2448 "with stream copy may produce invalid files\n");
2472 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2504 "about the input framerate is available. Falling "
2505 "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
2506 "if you want a different framerate.\n",
2535 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2548 if (!strncmp(ost->
enc->
name,
"libx264", 7) &&
2552 "No pixel format specified, %s for H.264 encoding chosen.\n"
2553 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2555 if (!strncmp(ost->
enc->
name,
"mpeg2video", 10) &&
2559 "No pixel format specified, %s for MPEG-2 encoding chosen.\n"
2560 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2565 codec->
width != icodec->width ||
2566 codec->
height != icodec->height ||
2567 codec->
pix_fmt != icodec->pix_fmt) {
2591 if (!codec->
width) {
2605 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2609 if (!strcmp(ost->
enc->
name,
"libx264")) {
2614 size_t logbuffer_size;
2626 logfilename, strerror(errno));
2638 ost = output_streams[i];
2639 if (ost->encoding_needed) {
2644 dec = ist->st->
codec;
2648 if (!ost->st->codec->subtitle_header) {
2660 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2661 ost->file_index, ost->index);
2667 ost->st->codec->frame_size);
2669 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
2671 " It takes bits/s as argument, not kbits/s\n");
2672 extra_size += ost->st->codec->extradata_size;
2682 ost = output_streams[i];
2706 oc = output_files[i]->
ctx;
2710 "Could not write header for output file #%d "
2711 "(incorrect codec parameters ?): %s",
2717 if (strcmp(oc->oformat->name,
"rtp")) {
2726 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2732 ist = input_streams[i];
2734 for (j = 0; j < ist->nb_filters; j++) {
2735 if (ist->filters[j]->graph->graph_desc) {
2737 ist->file_index, ist->st->index, ist->dec ? ist->dec->name :
"?",
2738 ist->filters[j]->name);
2747 ost = output_streams[i];
2749 if (ost->attachment_filename) {
2752 ost->attachment_filename, ost->file_index, ost->index);
2756 if (ost->filter && ost->filter->graph->graph_desc) {
2763 ost->index, ost->enc ? ost->enc->name :
"?");
2768 input_streams[ost->source_index]->
file_index,
2769 input_streams[ost->source_index]->
st->
index,
2772 if (ost->sync_ist != input_streams[ost->source_index])
2774 ost->sync_ist->file_index,
2775 ost->sync_ist->st->index);
2776 if (ost->stream_copy)
2780 input_streams[ost->source_index]->
dec->
name :
"?",
2781 ost->enc ? ost->enc->name :
"?");
2831 int64_t opts_min = INT64_MAX;
2849 static int64_t last_time;
2872 if (key ==
'c' || key ==
'C'){
2876 fprintf(stderr,
"\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
2878 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2883 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2885 target, time, command, arg);
2892 fprintf(stderr,
"Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
2893 }
else if (key ==
'c') {
2894 fprintf(stderr,
"Queing commands only on filters supporting the specific command is unsupported\n");
2903 "Parse error, at least 3 arguments were expected, "
2904 "only %d given in string '%s'\n", n, buf);
2907 if (key ==
'd' || key ==
'D'){
2911 if(!debug) debug = 1;
2915 if(scanf(
"%d", &debug)!=1)
2916 fprintf(stderr,
"error parsing debug value\n");
2925 fprintf(stderr,
"debug=%d\n", debug);
2928 fprintf(stderr,
"key function\n"
2929 "? show this help\n"
2930 "+ increase verbosity\n"
2931 "- decrease verbosity\n"
2932 "c Send command to first matching filter supporting it\n"
2933 "C Send/Que command to all matching filters\n"
2934 "D cycle through available debug modes\n"
2935 "h dump packets/hex press to cycle through the 3 states\n"
2937 "s Show QP histogram\n"
2944 static void *input_thread(
void *
arg)
2949 while (!transcoding_finished && ret >= 0) {
2982 if (nb_input_files == 1)
2985 transcoding_finished = 1;
2991 if (!f->fifo || f->joined)
3013 static int init_input_threads(
void)
3017 if (nb_input_files == 1)
3028 f->non_blocking = 1;
3055 if (f->non_blocking) {
3083 if (nb_input_files > 1)
3084 return get_input_packet_mt(f, pkt);
3093 if (output_streams[i]->unavailable)
3102 input_files[i]->eagain = 0;
3104 output_streams[i]->unavailable = 0;
3116 InputFile *ifile = input_files[file_index];
3138 ist = input_streams[ifile->
ist_index + i];
3165 goto discard_packet;
3170 goto discard_packet;
3174 "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
3185 int64_t stime, stime2;
3192 int64_t new_start_time = INT64_MAX;
3238 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3257 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3285 av_log(NULL,
AV_LOG_INFO,
"demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
3319 int nb_requests, nb_requests_max = 0;
3337 for (i = 0; i < graph->
nb_inputs; i++) {
3338 ifilter = graph->
inputs[i];
3344 if (nb_requests > nb_requests_max) {
3345 nb_requests_max = nb_requests;
3410 int64_t timer_start;
3423 if ((ret = init_input_threads()) < 0)
3459 ist = input_streams[i];
3470 os = output_files[i]->
ctx;
3479 ost = output_streams[i];
3488 ist = input_streams[i];
3504 if (output_streams) {
3506 ost = output_streams[i];
3530 struct rusage rusage;
3532 getrusage(RUSAGE_SELF, &rusage);
3533 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3534 #elif HAVE_GETPROCESSTIMES
3536 FILETIME
c, e, k,
u;
3537 proc = GetCurrentProcess();
3538 GetProcessTimes(proc, &c, &e, &k, &u);
3539 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3547 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3548 struct rusage rusage;
3549 getrusage(RUSAGE_SELF, &rusage);
3550 return (int64_t)rusage.ru_maxrss * 1024;
3551 #elif HAVE_GETPROCESSMEMORYINFO
3553 PROCESS_MEMORY_COUNTERS memcounters;
3554 proc = GetCurrentProcess();
3555 memcounters.cb =
sizeof(memcounters);
3556 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3557 return memcounters.PeakPagefileUsage;
3574 setvbuf(stderr,NULL,_IONBF,0);
3579 if(argc>1 && !strcmp(argv[1],
"-d")){
3603 if (nb_output_files <= 0 && nb_input_files == 0) {
3610 if (nb_output_files <= 0) {
3625 printf(
"bench: utime=%0.3fs\n", ti / 1000000.0);
3627 av_log(NULL,
AV_LOG_DEBUG,
"%"PRIu64
" frames successfully decoded, %"PRIu64
" decoding errors\n",