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>
130 static int transcoding_finished;
133 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
151 static struct termios oldtty;
152 static int restore_tty;
168 uint32_t *pal, *dst2;
176 if (r->
x < 0 || r->
x + r->
w > w || r->
y < 0 || r->
y + r->
h > h) {
181 dst += r->
y * dst_linesize + r->
x * 4;
184 for (y = 0; y < r->
h; y++) {
185 dst2 = (uint32_t *)dst;
187 for (x = 0; x < r->
w; x++)
188 *(dst2++) = pal[*(src2++)];
215 int64_t pts, end_pts;
232 memset(dst, 0, h * dst_linesize);
233 for (i = 0; i < num_rects; i++)
257 if (pts2 <= ist2->sub2video.last_pts)
261 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
283 tcsetattr (0, TCSANOW, &oldtty);
307 istty = isatty(0) && isatty(2);
309 if (istty && tcgetattr (0, &tty) == 0) {
314 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
315 |INLCR|IGNCR|ICRNL|IXON);
316 tty.c_oflag |= OPOST;
317 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
318 tty.c_cflag &= ~(CSIZE|PARENB);
323 tcsetattr (0, TCSANOW, &tty);
350 n = select(1, &rfds,
NULL,
NULL, &tv);
359 # if HAVE_PEEKNAMEDPIPE
361 static HANDLE input_handle;
364 input_handle = GetStdHandle(STD_INPUT_HANDLE);
365 is_pipe = !GetConsoleMode(input_handle, &dw);
368 if (stdin->_cnt > 0) {
374 if (!PeekNamedPipe(input_handle,
NULL, 0,
NULL, &nchars,
NULL)) {
406 for (j = 0; j < filtergraphs[i]->
nb_inputs; j++) {
411 for (j = 0; j < filtergraphs[i]->
nb_outputs; j++) {
441 av_freep(&output_streams[i]->forced_keyframes);
442 av_freep(&output_streams[i]->avfilter);
443 av_freep(&output_streams[i]->logfile_prefix);
507 printf(
"bench: %8"PRIu64
" %s \n", t -
current_time, buf);
527 "st:%d PTS: %"PRId64
" DTS: %"PRId64
" < %"PRId64
" invalid, clipping\n", pkt->
stream_index, pkt->
pts, pkt->
dts, max);
558 memcpy(t, new_pkt.
data, new_pkt.
size);
585 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
660 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
693 picture2 = &picture_tmp;
708 if (picture != picture2)
709 *picture = *picture2;
718 int subtitle_out_max_size = 1024 * 1024;
719 int subtitle_out_size, nb, i;
747 for (i = 0; i < nb; i++) {
760 subtitle_out_max_size, sub);
761 if (subtitle_out_size < 0) {
768 pkt.
size = subtitle_out_size;
775 pkt.
pts += 90 * sub->start_display_time;
777 pkt.
pts += 90 * sub->end_display_time;
788 int ret, format_video_sync;
792 double sync_ipts,
delta;
803 sync_ipts = in_picture->
pts;
813 switch (format_video_sync) {
818 else if (delta > 1.1)
819 nb_frames =
lrintf(delta);
824 else if (delta > 0.6)
836 if (nb_frames == 0) {
840 }
else if (nb_frames > 1) {
851 for (i = 0; i < nb_frames; i++) {
883 big_picture = *in_picture;
929 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
934 frame_size = pkt.
size;
960 return -10.0 * log(d) / log(10.0);
967 double ti1, bitrate, avg_bitrate;
992 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
993 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
994 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1035 "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
1060 filtered_frame->
pts = frame_pts;
1068 filtered_frame->
pts = frame_pts;
1091 int frame_number, vid, i;
1093 int64_t pts = INT64_MIN;
1094 static int64_t last_time = -1;
1095 static int qp_histogram[52];
1096 int hours, mins, secs, us;
1098 if (!
print_stats && !is_last_report && !progress_avio)
1101 if (!is_last_report) {
1102 if (last_time == -1) {
1106 if ((cur_time - last_time) < 500000)
1112 oc = output_files[0]->
ctx;
1115 if (total_size <= 0)
1123 ost = output_streams[i];
1128 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1129 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1133 float fps,
t = (cur_time-timer_start) / 1000000.0;
1136 fps = t > 1 ? frame_number / t : 0;
1137 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1138 frame_number, fps < 9.95, fps, q);
1139 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1141 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1144 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1150 for (j = 0; j < 32; j++)
1151 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1155 double error, error_sum = 0;
1156 double scale, scale_sum = 0;
1158 char type[3] = {
'Y',
'U',
'V' };
1159 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1160 for (j = 0; j < 3; j++) {
1161 if (is_last_report) {
1162 error = enc->
error[j];
1163 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1172 p =
psnr(error / scale);
1173 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1174 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1177 p =
psnr(error_sum / scale_sum);
1178 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1179 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1197 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1199 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1201 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1202 "size=%8.0fkB time=", total_size / 1024.0);
1203 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1204 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1206 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1208 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1209 "bitrate=%6.1fkbits/s", bitrate);
1210 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1211 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1212 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1213 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1214 hours, mins, secs, us);
1217 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1228 if (progress_avio) {
1230 is_last_report ?
"end" :
"continue");
1232 FFMIN(buf_script.len, buf_script.size - 1));
1235 if (is_last_report) {
1237 progress_avio =
NULL;
1241 if (is_last_report) {
1244 av_log(
NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1248 extra_size / 1024.0,
1249 100.0 * (total_size - raw) / raw
1252 av_log(
NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1265 int stop_encoding = 0;
1303 ret = encode(enc, &pkt,
NULL, &got_packet);
1394 opkt.
dts -= ost_tb_start_time;
1423 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1430 ost->st->codec->frame_number++;
1448 char layout_name[256];
1465 int i, ret, resample_changed;
1481 if (!*got_output || ret < 0) {
1504 if (resample_changed) {
1505 char layout1[64], layout2[64];
1509 "layout for Input Stream #%d.%d\n", ist->
file_index,
1521 "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",
1561 decoded_frame->
pts = pkt->
pts;
1565 decoded_frame->
pts = ist->
dts;
1571 (
AVRational){1, ist->st->codec->sample_rate});
1572 for (i = 0; i < ist->nb_filters; i++)
1584 void *buffer_to_free =
NULL;
1585 int i, ret = 0, resample_changed;
1586 int64_t best_effort_timestamp;
1596 decoded_frame, got_output, pkt);
1598 if (!*got_output || ret < 0) {
1615 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
1618 best_effort_timestamp,
1634 if (resample_changed) {
1636 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1660 if (!frame_sample_aspect->
num)
1696 &subtitle, got_output, pkt);
1697 if (ret < 0 || !*got_output) {
1707 if (end < ist->prev_sub.subtitle.end_display_time) {
1709 "Subtitle duration reduced from %d to %d\n",
1721 if (!*got_output || !subtitle.
num_rects)
1787 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
1884 avc[i] = output_files[i]->ctx;
1887 printf(
"SDP:\n%s\n", sdp);
1900 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
1917 snprintf(error, error_len,
"Error while opening decoder for input stream #%d:%d",
1945 for (p = kf; *p; p++)
1956 for (i = 0; i < n; i++) {
1957 char *next = strchr(p,
',');
1971 InputFile *file = input_files[input_index];
1977 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
1986 int ret = 0, i, j, k;
2004 oc = output_files[i]->
ctx;
2020 ost = output_streams[i];
2044 if (extra_size > INT_MAX) {
2099 if( copy_tb<0 && icodec->time_base.den
2166 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2216 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2244 if (!codec->
width) {
2258 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2262 if (!strcmp(ost->
enc->
name,
"libx264")) {
2267 size_t logbuffer_size;
2276 f = fopen(logfilename,
"wb");
2279 logfilename, strerror(errno));
2291 ost = output_streams[i];
2313 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2324 " It takes bits/s as argument, not kbits/s\n");
2355 oc = output_files[i]->
ctx;
2359 const char *errbuf_ptr = errbuf;
2362 snprintf(error,
sizeof(error),
"Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
2376 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2382 ist = input_streams[i];
2389 if (nb_filtergraphs > 1)
2397 ost = output_streams[i];
2409 if (nb_filtergraphs > 1)
2481 int64_t opts_min = INT64_MAX;
2499 static int64_t last_time;
2522 if (key ==
'c' || key ==
'C'){
2523 char buf[4096], target[64],
command[256],
arg[256] = {0};
2526 fprintf(stderr,
"\nEnter command: <target> <time> <command>[ <argument>]\n");
2528 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2533 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2535 target, time, command, arg);
2542 fprintf(stderr,
"Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
2550 "Parse error, at least 3 arguments were expected, "
2551 "only %d given in string '%s'\n", n, buf);
2554 if (key ==
'd' || key ==
'D'){
2558 if(!debug) debug = 1;
2562 if(scanf(
"%d", &debug)!=1)
2563 fprintf(stderr,
"error parsing debug value\n");
2572 fprintf(stderr,
"debug=%d\n", debug);
2575 fprintf(stderr,
"key function\n"
2576 "? show this help\n"
2577 "+ increase verbosity\n"
2578 "- decrease verbosity\n"
2579 "c Send command to filtergraph\n"
2580 "D cycle through available debug modes\n"
2581 "h dump packets/hex press to cycle through the 3 states\n"
2583 "s Show QP histogram\n"
2590 static void *input_thread(
void *
arg)
2595 while (!transcoding_finished && ret >= 0) {
2627 transcoding_finished = 1;
2633 if (!f->fifo || f->joined)
2655 static int init_input_threads(
void)
2659 if (nb_input_files == 1)
2702 if (nb_input_files > 1)
2703 return get_input_packet_mt(f, pkt);
2712 if (output_streams[i]->unavailable)
2721 input_files[i]->eagain = 0;
2723 output_streams[i]->unavailable = 0;
2735 InputFile *ifile = input_files[file_index];
2757 ist = input_streams[ifile->
ist_index + i];
2784 goto discard_packet;
2789 goto discard_packet;
2793 "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",
2804 int64_t stime, stime2;
2811 int64_t new_start_time = INT64_MAX;
2856 pkt_dts+1<ist->
pts){
2859 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
2886 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",
2922 int nb_requests, nb_requests_max = 0;
2940 for (i = 0; i < graph->
nb_inputs; i++) {
2941 ifilter = graph->
inputs[i];
2947 if (nb_requests > nb_requests_max) {
2948 nb_requests_max = nb_requests;
3013 int64_t timer_start;
3026 if ((ret = init_input_threads()) < 0)
3062 ist = input_streams[i];
3073 os = output_files[i]->
ctx;
3082 ost = output_streams[i];
3091 ist = input_streams[i];
3105 if (output_streams) {
3107 ost = output_streams[i];
3128 struct rusage rusage;
3130 getrusage(RUSAGE_SELF, &rusage);
3131 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3132 #elif HAVE_GETPROCESSTIMES
3134 FILETIME
c, e, k, u;
3135 proc = GetCurrentProcess();
3136 GetProcessTimes(proc, &c, &e, &k, &u);
3137 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3145 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3146 struct rusage rusage;
3147 getrusage(RUSAGE_SELF, &rusage);
3148 return (int64_t)rusage.ru_maxrss * 1024;
3149 #elif HAVE_GETPROCESSMEMORYINFO
3151 PROCESS_MEMORY_COUNTERS memcounters;
3152 proc = GetCurrentProcess();
3153 memcounters.cb =
sizeof(memcounters);
3154 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3155 return memcounters.PeakPagefileUsage;
3172 setvbuf(stderr,
NULL,_IONBF,0);
3177 if(argc>1 && !strcmp(argv[1],
"-d")){
3224 printf(
"bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);