70 #if HAVE_SYS_RESOURCE_H
72 #include <sys/types.h>
73 #include <sys/resource.h>
74 #elif HAVE_GETPROCESSTIMES
77 #if HAVE_GETPROCESSMEMORYINFO
83 #include <sys/select.h>
88 #include <sys/ioctl.h>
140 static int transcoding_finished;
143 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
161 static struct termios oldtty;
162 static int restore_tty;
191 uint32_t *pal, *dst2;
199 if (r->
x < 0 || r->
x + r->
w > w || r->
y < 0 || r->
y + r->
h > h) {
204 dst += r->
y * dst_linesize + r->
x * 4;
207 for (y = 0; y < r->
h; y++) {
208 dst2 = (uint32_t *)dst;
210 for (x = 0; x < r->
w; x++)
211 *(dst2++) = pal[*(src2++)];
237 int64_t pts, end_pts;
254 "Impossible to get a blank canvas.\n");
257 dst = frame->
data [0];
259 for (i = 0; i < num_rects; i++)
283 if (pts2 <= ist2->sub2video.last_pts)
287 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
309 tcsetattr (0, TCSANOW, &oldtty);
333 istty = isatty(0) && isatty(2);
335 if (istty && tcgetattr (0, &tty) == 0) {
340 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
341 |INLCR|IGNCR|ICRNL|IXON);
342 tty.c_oflag |= OPOST;
343 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
344 tty.c_cflag &= ~(CSIZE|PARENB);
349 tcsetattr (0, TCSANOW, &tty);
376 n = select(1, &rfds,
NULL,
NULL, &tv);
385 # if HAVE_PEEKNAMEDPIPE
387 static HANDLE input_handle;
390 input_handle = GetStdHandle(STD_INPUT_HANDLE);
391 is_pipe = !GetConsoleMode(input_handle, &dw);
394 if (stdin->_cnt > 0) {
400 if (!PeekNamedPipe(input_handle,
NULL, 0,
NULL, &nchars,
NULL)) {
432 printf(
"bench: maxrss=%ikB\n", maxrss);
437 for (j = 0; j < filtergraphs[i]->
nb_inputs; j++) {
442 for (j = 0; j < filtergraphs[i]->
nb_outputs; j++) {
447 av_freep(&filtergraphs[i]->graph_desc);
473 av_freep(&output_streams[i]->forced_keyframes);
474 av_expr_free(output_streams[i]->forced_keyframes_pexpr);
475 av_freep(&output_streams[i]->avfilter);
476 av_freep(&output_streams[i]->logfile_prefix);
540 printf(
"bench: %8"PRIu64
" %s \n", t -
current_time, buf);
577 if(a == 0 && new_pkt.
data != pkt->
data && new_pkt.destruct) {
580 memcpy(t, new_pkt.
data, new_pkt.
size);
612 if (pkt->
dts < max) {
614 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
615 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
621 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
622 "in incorrect timestamps in the output file.\n",
635 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
710 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
727 int subtitle_out_max_size = 1024 * 1024;
728 int subtitle_out_size, nb, i;
756 for (i = 0; i < nb; i++) {
769 subtitle_out_max_size, sub);
770 if (subtitle_out_size < 0) {
777 pkt.
size = subtitle_out_size;
784 pkt.
pts += 90 * sub->start_display_time;
786 pkt.
pts += 90 * sub->end_display_time;
797 int ret, format_video_sync;
801 double sync_ipts,
delta;
812 sync_ipts = in_picture->
pts;
822 switch (format_video_sync) {
827 else if (delta > 1.1)
828 nb_frames =
lrintf(delta);
833 else if (delta > 0.6)
845 if (nb_frames == 0) {
849 }
else if (nb_frames > 1) {
860 for (i = 0; i < nb_frames; i++) {
893 int got_packet, forced_keyframe = 0;
923 av_dlog(
NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
941 if (forced_keyframe) {
965 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
970 frame_size = pkt.
size;
996 return -10.0 * log(d) / log(10.0);
1003 double ti1, bitrate, avg_bitrate;
1028 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1029 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1030 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1068 "Error in av_buffersink_get_frame_flags(): %s\n",
av_err2str(ret));
1087 filtered_frame->
pts = frame_pts;
1094 filtered_frame->
pts = frame_pts;
1098 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1123 int frame_number, vid, i;
1125 int64_t pts = INT64_MIN;
1126 static int64_t last_time = -1;
1127 static int qp_histogram[52];
1128 int hours, mins, secs, us;
1130 if (!
print_stats && !is_last_report && !progress_avio)
1133 if (!is_last_report) {
1134 if (last_time == -1) {
1138 if ((cur_time - last_time) < 500000)
1144 oc = output_files[0]->
ctx;
1147 if (total_size <= 0)
1155 ost = output_streams[i];
1160 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1161 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1165 float fps,
t = (cur_time-timer_start) / 1000000.0;
1168 fps = t > 1 ? frame_number / t : 0;
1169 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1170 frame_number, fps < 9.95, fps, q);
1171 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1173 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1176 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1182 for (j = 0; j < 32; j++)
1183 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1187 double error, error_sum = 0;
1188 double scale, scale_sum = 0;
1190 char type[3] = {
'Y',
'U',
'V' };
1191 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1192 for (j = 0; j < 3; j++) {
1193 if (is_last_report) {
1194 error = enc->
error[j];
1195 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1204 p =
psnr(error / scale);
1205 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1206 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1209 p =
psnr(error_sum / scale_sum);
1210 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1211 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1229 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1231 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1233 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1234 "size=%8.0fkB time=", total_size / 1024.0);
1235 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1236 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1238 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1240 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1241 "bitrate=%6.1fkbits/s", bitrate);
1242 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1243 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1244 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1245 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1246 hours, mins, secs, us);
1249 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1256 fprintf(stderr,
"%s \r", buf);
1263 if (progress_avio) {
1265 is_last_report ?
"end" :
"continue");
1267 FFMIN(buf_script.len, buf_script.size - 1));
1270 if (is_last_report) {
1272 progress_avio =
NULL;
1276 if (is_last_report) {
1279 av_log(
NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1283 extra_size / 1024.0,
1284 100.0 * (total_size - raw) / raw
1287 av_log(
NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1300 int stop_encoding = 0;
1338 ret = encode(enc, &pkt,
NULL, &got_packet);
1429 opkt.
dts -= ost_tb_start_time;
1461 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1468 ost->st->codec->frame_number++;
1476 char layout_name[256];
1495 int i,
ret, err = 0, resample_changed;
1513 if (*got_output || ret<0 || pkt->
size)
1516 if (!*got_output || ret < 0) {
1541 if (resample_changed) {
1542 char layout1[64], layout2[64];
1546 "layout for Input Stream #%d.%d\n", ist->
file_index,
1558 "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",
1598 decoded_frame->
pts = pkt->
pts;
1602 decoded_frame->
pts = ist->
dts;
1608 (
AVRational){1, ist->st->codec->sample_rate});
1609 for (i = 0; i < ist->nb_filters; i++) {
1610 if (i < ist->nb_filters - 1) {
1611 f = ist->filter_frame;
1626 return err < 0 ? err :
ret;
1632 void *buffer_to_free =
NULL;
1633 int i,
ret = 0, err = 0, resample_changed;
1634 int64_t best_effort_timestamp;
1646 decoded_frame, got_output, pkt);
1649 if (*got_output || ret<0 || pkt->
size)
1652 if (!*got_output || ret < 0) {
1673 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
1676 best_effort_timestamp,
1689 if (resample_changed) {
1691 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1711 if (!frame_sample_aspect->
num)
1714 if (i < ist->nb_filters - 1) {
1724 "Failed to inject frame into filter network: %s\n",
av_err2str(ret));
1732 return err < 0 ? err :
ret;
1739 &subtitle, got_output, pkt);
1741 if (*got_output || ret<0 || pkt->
size)
1744 if (ret < 0 || !*got_output) {
1754 if (end < ist->prev_sub.subtitle.end_display_time) {
1756 "Subtitle duration reduced from %d to %d\n",
1768 if (!*got_output || !subtitle.
num_rects)
1832 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
1933 avc[i] = output_files[i]->ctx;
1936 printf(
"SDP:\n%s\n", sdp);
1949 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
1961 snprintf(error, error_len,
"Error while opening decoder for input stream #%d:%d",
1984 int64_t va = *(int64_t *)a, vb = *(int64_t *)
b;
1985 return va < vb ? -1 : va > vb ? +1 : 0;
1995 for (p = kf; *p; p++)
2006 for (i = 0; i <
n; i++) {
2007 char *next = strchr(p,
',');
2012 if (!memcmp(p,
"chapters", 8)) {
2021 "Could not allocate forced key frames array.\n");
2053 InputFile *file = input_files[input_index];
2059 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
2068 int ret = 0, i, j, k;
2086 oc = output_files[i]->
ctx;
2102 ost = output_streams[i];
2126 if (extra_size > INT_MAX) {
2135 unsigned int codec_tag;
2182 if( copy_tb<0 && icodec->time_base.den
2233 "with stream copy may produce invalid files\n");
2258 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2308 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2321 if (!strncmp(ost->
enc->
name,
"libx264", 7) &&
2325 "No pixel format specified, %s for H.264 encoding chosen.\n"
2326 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2331 codec->
width != icodec->width ||
2332 codec->
height != icodec->height ||
2333 codec->
pix_fmt != icodec->pix_fmt) {
2357 if (!codec->
width) {
2371 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2375 if (!strcmp(ost->
enc->
name,
"libx264")) {
2380 size_t logbuffer_size;
2389 f = fopen(logfilename,
"wb");
2392 logfilename, strerror(errno));
2404 ost = output_streams[i];
2405 if (ost->encoding_needed) {
2410 dec = ist->st->
codec;
2414 if (!ost->st->codec->subtitle_header) {
2426 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2427 ost->file_index, ost->index);
2433 ost->st->codec->frame_size);
2435 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
2437 " It takes bits/s as argument, not kbits/s\n");
2438 extra_size += ost->st->codec->extradata_size;
2440 if (ost->st->codec->me_threshold)
2451 ost = output_streams[i];
2475 oc = output_files[i]->
ctx;
2479 const char *errbuf_ptr = errbuf;
2482 snprintf(error,
sizeof(error),
"Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
2487 if (strcmp(oc->oformat->name,
"rtp")) {
2496 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2502 ist = input_streams[i];
2504 for (j = 0; j < ist->nb_filters; j++) {
2505 if (ist->filters[j]->graph->graph_desc) {
2507 ist->file_index, ist->st->index, ist->dec ? ist->dec->name :
"?",
2508 ist->filters[j]->name);
2517 ost = output_streams[i];
2519 if (ost->attachment_filename) {
2522 ost->attachment_filename, ost->file_index, ost->index);
2526 if (ost->filter && ost->filter->graph->graph_desc) {
2533 ost->index, ost->enc ? ost->enc->name :
"?");
2538 input_streams[ost->source_index]->
file_index,
2539 input_streams[ost->source_index]->
st->
index,
2542 if (ost->sync_ist != input_streams[ost->source_index])
2544 ost->sync_ist->file_index,
2545 ost->sync_ist->st->index);
2546 if (ost->stream_copy)
2550 input_streams[ost->source_index]->
dec->
name :
"?",
2551 ost->enc ? ost->enc->name :
"?");
2601 int64_t opts_min = INT64_MAX;
2619 static int64_t last_time;
2642 if (key ==
'c' || key ==
'C'){
2646 fprintf(stderr,
"\nEnter command: <target> <time> <command>[ <argument>]\n");
2648 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2653 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2655 target, time, command, arg);
2662 fprintf(stderr,
"Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
2670 "Parse error, at least 3 arguments were expected, "
2671 "only %d given in string '%s'\n", n, buf);
2674 if (key ==
'd' || key ==
'D'){
2678 if(!debug) debug = 1;
2682 if(scanf(
"%d", &debug)!=1)
2683 fprintf(stderr,
"error parsing debug value\n");
2692 fprintf(stderr,
"debug=%d\n", debug);
2695 fprintf(stderr,
"key function\n"
2696 "? show this help\n"
2697 "+ increase verbosity\n"
2698 "- decrease verbosity\n"
2699 "c Send command to filtergraph\n"
2700 "D cycle through available debug modes\n"
2701 "h dump packets/hex press to cycle through the 3 states\n"
2703 "s Show QP histogram\n"
2710 static void *input_thread(
void *
arg)
2715 while (!transcoding_finished && ret >= 0) {
2744 if (nb_input_files == 1)
2747 transcoding_finished = 1;
2753 if (!f->fifo || f->joined)
2775 static int init_input_threads(
void)
2779 if (nb_input_files == 1)
2833 if (nb_input_files > 1)
2834 return get_input_packet_mt(f, pkt);
2843 if (output_streams[i]->unavailable)
2852 input_files[i]->eagain = 0;
2854 output_streams[i]->unavailable = 0;
2866 InputFile *ifile = input_files[file_index];
2888 ist = input_streams[ifile->
ist_index + i];
2915 goto discard_packet;
2920 goto discard_packet;
2924 "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",
2935 int64_t stime, stime2;
2942 int64_t new_start_time = INT64_MAX;
2988 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3004 pkt_dts+1<ist->
pts){
3007 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3037 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",
3073 int nb_requests, nb_requests_max = 0;
3091 for (i = 0; i < graph->
nb_inputs; i++) {
3092 ifilter = graph->
inputs[i];
3098 if (nb_requests > nb_requests_max) {
3099 nb_requests_max = nb_requests;
3164 int64_t timer_start;
3177 if ((ret = init_input_threads()) < 0)
3213 ist = input_streams[i];
3224 os = output_files[i]->
ctx;
3233 ost = output_streams[i];
3242 ist = input_streams[i];
3256 if (output_streams) {
3258 ost = output_streams[i];
3282 struct rusage rusage;
3284 getrusage(RUSAGE_SELF, &rusage);
3285 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3286 #elif HAVE_GETPROCESSTIMES
3288 FILETIME
c, e, k,
u;
3289 proc = GetCurrentProcess();
3290 GetProcessTimes(proc, &c, &e, &k, &u);
3291 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3299 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3300 struct rusage rusage;
3301 getrusage(RUSAGE_SELF, &rusage);
3302 return (int64_t)rusage.ru_maxrss * 1024;
3303 #elif HAVE_GETPROCESSMEMORYINFO
3305 PROCESS_MEMORY_COUNTERS memcounters;
3306 proc = GetCurrentProcess();
3307 memcounters.cb =
sizeof(memcounters);
3308 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3309 return memcounters.PeakPagefileUsage;
3326 setvbuf(stderr,
NULL,_IONBF,0);
3331 if(argc>1 && !strcmp(argv[1],
"-d")){
3355 if (nb_output_files <= 0 && nb_input_files == 0) {
3362 if (nb_output_files <= 0) {
3377 printf(
"bench: utime=%0.3fs\n", ti / 1000000.0);