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>
140 static int transcoding_finished;
143 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
161 static struct termios oldtty;
162 static int restore_tty;
178 uint32_t *pal, *dst2;
186 if (r->
x < 0 || r->
x + r->
w > w || r->
y < 0 || r->
y + r->
h > h) {
191 dst += r->
y * dst_linesize + r->
x * 4;
194 for (y = 0; y < r->
h; y++) {
195 dst2 = (uint32_t *)dst;
197 for (x = 0; x < r->
w; x++)
198 *(dst2++) = pal[*(src2++)];
225 int64_t pts, end_pts;
242 memset(dst, 0, h * dst_linesize);
243 for (i = 0; i < num_rects; i++)
267 if (pts2 <= ist2->sub2video.last_pts)
271 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
293 tcsetattr (0, TCSANOW, &oldtty);
317 istty = isatty(0) && isatty(2);
319 if (istty && tcgetattr (0, &tty) == 0) {
324 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
325 |INLCR|IGNCR|ICRNL|IXON);
326 tty.c_oflag |= OPOST;
327 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
328 tty.c_cflag &= ~(CSIZE|PARENB);
333 tcsetattr (0, TCSANOW, &tty);
360 n = select(1, &rfds,
NULL,
NULL, &tv);
369 # if HAVE_PEEKNAMEDPIPE
371 static HANDLE input_handle;
374 input_handle = GetStdHandle(STD_INPUT_HANDLE);
375 is_pipe = !GetConsoleMode(input_handle, &dw);
378 if (stdin->_cnt > 0) {
384 if (!PeekNamedPipe(input_handle,
NULL, 0,
NULL, &nchars,
NULL)) {
416 printf(
"bench: maxrss=%ikB\n", maxrss);
421 for (j = 0; j < filtergraphs[i]->
nb_inputs; j++) {
426 for (j = 0; j < filtergraphs[i]->
nb_outputs; j++) {
456 av_freep(&output_streams[i]->forced_keyframes);
457 av_expr_free(output_streams[i]->forced_keyframes_pexpr);
458 av_freep(&output_streams[i]->avfilter);
459 av_freep(&output_streams[i]->logfile_prefix);
524 printf(
"bench: %8"PRIu64
" %s \n", t -
current_time, buf);
544 "st:%d PTS: %"PRId64
" DTS: %"PRId64
" < %"PRId64
" invalid, clipping\n", pkt->
stream_index, pkt->
pts, pkt->
dts, max);
575 memcpy(t, new_pkt.
data, new_pkt.
size);
602 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
677 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
689 #if FF_API_DEINTERLACE
711 picture2 = &picture_tmp;
714 if (avpicture_deinterlace(picture2, picture,
726 if (picture != picture2)
727 *picture = *picture2;
737 int subtitle_out_max_size = 1024 * 1024;
738 int subtitle_out_size, nb, i;
766 for (i = 0; i < nb; i++) {
779 subtitle_out_max_size, sub);
780 if (subtitle_out_size < 0) {
787 pkt.
size = subtitle_out_size;
794 pkt.
pts += 90 * sub->start_display_time;
796 pkt.
pts += 90 * sub->end_display_time;
807 int ret, format_video_sync;
811 double sync_ipts,
delta;
822 sync_ipts = in_picture->
pts;
832 switch (format_video_sync) {
837 else if (delta > 1.1)
838 nb_frames =
lrintf(delta);
843 else if (delta > 0.6)
855 if (nb_frames == 0) {
859 }
else if (nb_frames > 1) {
870 for (i = 0; i < nb_frames; i++) {
899 int got_packet, forced_keyframe = 0;
903 big_picture = *in_picture;
937 av_dlog(
NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
955 if (forced_keyframe) {
979 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
984 frame_size = pkt.
size;
1010 return -10.0 * log(d) / log(10.0);
1017 double ti1, bitrate, avg_bitrate;
1042 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1043 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1044 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1085 "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
1110 filtered_frame->
pts = frame_pts;
1118 filtered_frame->
pts = frame_pts;
1122 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1147 int frame_number, vid, i;
1149 int64_t pts = INT64_MIN;
1150 static int64_t last_time = -1;
1151 static int qp_histogram[52];
1152 int hours, mins, secs, us;
1154 if (!
print_stats && !is_last_report && !progress_avio)
1157 if (!is_last_report) {
1158 if (last_time == -1) {
1162 if ((cur_time - last_time) < 500000)
1168 oc = output_files[0]->
ctx;
1171 if (total_size <= 0)
1179 ost = output_streams[i];
1184 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1185 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1189 float fps,
t = (cur_time-timer_start) / 1000000.0;
1192 fps = t > 1 ? frame_number / t : 0;
1193 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1194 frame_number, fps < 9.95, fps, q);
1195 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1197 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1200 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1206 for (j = 0; j < 32; j++)
1207 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1211 double error, error_sum = 0;
1212 double scale, scale_sum = 0;
1214 char type[3] = {
'Y',
'U',
'V' };
1215 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1216 for (j = 0; j < 3; j++) {
1217 if (is_last_report) {
1218 error = enc->
error[j];
1219 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1228 p =
psnr(error / scale);
1229 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1230 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1233 p =
psnr(error_sum / scale_sum);
1234 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1235 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1253 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1255 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1257 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1258 "size=%8.0fkB time=", total_size / 1024.0);
1259 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1260 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1262 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1264 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1265 "bitrate=%6.1fkbits/s", bitrate);
1266 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1267 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1268 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1269 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1270 hours, mins, secs, us);
1273 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1280 fprintf(stderr,
"%s \r", buf);
1287 if (progress_avio) {
1289 is_last_report ?
"end" :
"continue");
1291 FFMIN(buf_script.len, buf_script.size - 1));
1294 if (is_last_report) {
1296 progress_avio =
NULL;
1300 if (is_last_report) {
1303 av_log(
NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1307 extra_size / 1024.0,
1308 100.0 * (total_size - raw) / raw
1311 av_log(
NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1324 int stop_encoding = 0;
1362 ret = encode(enc, &pkt,
NULL, &got_packet);
1453 opkt.
dts -= ost_tb_start_time;
1482 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1489 ost->st->codec->frame_number++;
1507 char layout_name[256];
1526 int i, ret, resample_changed;
1542 if (!*got_output || ret < 0) {
1565 if (resample_changed) {
1566 char layout1[64], layout2[64];
1570 "layout for Input Stream #%d.%d\n", ist->
file_index,
1582 "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",
1622 decoded_frame->
pts = pkt->
pts;
1626 decoded_frame->
pts = ist->
dts;
1645 void *buffer_to_free =
NULL;
1646 int i, ret = 0, resample_changed;
1647 int64_t best_effort_timestamp;
1657 decoded_frame, got_output, pkt);
1659 if (!*got_output || ret < 0) {
1676 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
1679 best_effort_timestamp,
1685 #if FF_API_DEINTERLACE
1686 pre_process_video_frame(ist, (
AVPicture *)decoded_frame, &buffer_to_free);
1697 if (resample_changed) {
1699 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1723 if (!frame_sample_aspect->
num)
1759 &subtitle, got_output, pkt);
1760 if (ret < 0 || !*got_output) {
1770 if (end < ist->prev_sub.subtitle.end_display_time) {
1772 "Subtitle duration reduced from %d to %d\n",
1784 if (!*got_output || !subtitle.
num_rects)
1850 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
1947 avc[i] = output_files[i]->ctx;
1950 printf(
"SDP:\n%s\n", sdp);
1963 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
1980 snprintf(error, error_len,
"Error while opening decoder for input stream #%d:%d",
2003 int64_t va = *(int64_t *)a, vb = *(int64_t *)
b;
2004 return va < vb ? -1 : va > vb ? +1 : 0;
2014 for (p = kf; *p; p++)
2025 for (i = 0; i < n; i++) {
2026 char *next = strchr(p,
',');
2031 if (!memcmp(p,
"chapters", 8)) {
2040 "Could not allocate forced key frames array.\n");
2072 InputFile *file = input_files[input_index];
2078 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
2087 int ret = 0, i, j, k;
2105 oc = output_files[i]->
ctx;
2121 ost = output_streams[i];
2145 if (extra_size > INT_MAX) {
2154 unsigned int codec_tag;
2201 if( copy_tb<0 && icodec->time_base.den
2268 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2318 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2360 if (!codec->
width) {
2374 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2378 if (!strcmp(ost->
enc->
name,
"libx264")) {
2383 size_t logbuffer_size;
2392 f = fopen(logfilename,
"wb");
2395 logfilename, strerror(errno));
2407 ost = output_streams[i];
2429 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2440 " It takes bits/s as argument, not kbits/s\n");
2454 ost = output_streams[i];
2478 oc = output_files[i]->
ctx;
2482 const char *errbuf_ptr = errbuf;
2485 snprintf(error,
sizeof(error),
"Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
2499 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2505 ist = input_streams[i];
2512 if (nb_filtergraphs > 1)
2520 ost = output_streams[i];
2532 if (nb_filtergraphs > 1)
2604 int64_t opts_min = INT64_MAX;
2622 static int64_t last_time;
2645 if (key ==
'c' || key ==
'C'){
2646 char buf[4096], target[64],
command[256],
arg[256] = {0};
2649 fprintf(stderr,
"\nEnter command: <target> <time> <command>[ <argument>]\n");
2651 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2656 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2658 target, time, command, arg);
2665 fprintf(stderr,
"Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
2673 "Parse error, at least 3 arguments were expected, "
2674 "only %d given in string '%s'\n", n, buf);
2677 if (key ==
'd' || key ==
'D'){
2681 if(!debug) debug = 1;
2685 if(scanf(
"%d", &debug)!=1)
2686 fprintf(stderr,
"error parsing debug value\n");
2695 fprintf(stderr,
"debug=%d\n", debug);
2698 fprintf(stderr,
"key function\n"
2699 "? show this help\n"
2700 "+ increase verbosity\n"
2701 "- decrease verbosity\n"
2702 "c Send command to filtergraph\n"
2703 "D cycle through available debug modes\n"
2704 "h dump packets/hex press to cycle through the 3 states\n"
2706 "s Show QP histogram\n"
2713 static void *input_thread(
void *
arg)
2718 while (!transcoding_finished && ret >= 0) {
2750 transcoding_finished = 1;
2756 if (!f->fifo || f->joined)
2778 static int init_input_threads(
void)
2782 if (nb_input_files == 1)
2825 if (nb_input_files > 1)
2826 return get_input_packet_mt(f, pkt);
2835 if (output_streams[i]->unavailable)
2844 input_files[i]->eagain = 0;
2846 output_streams[i]->unavailable = 0;
2858 InputFile *ifile = input_files[file_index];
2880 ist = input_streams[ifile->
ist_index + i];
2907 goto discard_packet;
2912 goto discard_packet;
2916 "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",
2927 int64_t stime, stime2;
2934 int64_t new_start_time = INT64_MAX;
2979 pkt_dts+1<ist->
pts){
2982 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3009 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",
3045 int nb_requests, nb_requests_max = 0;
3063 for (i = 0; i < graph->
nb_inputs; i++) {
3064 ifilter = graph->
inputs[i];
3070 if (nb_requests > nb_requests_max) {
3071 nb_requests_max = nb_requests;
3136 int64_t timer_start;
3149 if ((ret = init_input_threads()) < 0)
3185 ist = input_streams[i];
3196 os = output_files[i]->
ctx;
3205 ost = output_streams[i];
3214 ist = input_streams[i];
3228 if (output_streams) {
3230 ost = output_streams[i];
3253 struct rusage rusage;
3255 getrusage(RUSAGE_SELF, &rusage);
3256 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3257 #elif HAVE_GETPROCESSTIMES
3259 FILETIME
c, e, k,
u;
3260 proc = GetCurrentProcess();
3261 GetProcessTimes(proc, &c, &e, &k, &u);
3262 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3270 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3271 struct rusage rusage;
3272 getrusage(RUSAGE_SELF, &rusage);
3273 return (int64_t)rusage.ru_maxrss * 1024;
3274 #elif HAVE_GETPROCESSMEMORYINFO
3276 PROCESS_MEMORY_COUNTERS memcounters;
3277 proc = GetCurrentProcess();
3278 memcounters.cb =
sizeof(memcounters);
3279 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3280 return memcounters.PeakPagefileUsage;
3297 setvbuf(stderr,
NULL,_IONBF,0);
3302 if(argc>1 && !strcmp(argv[1],
"-d")){
3348 printf(
"bench: utime=%0.3fs\n", ti / 1000000.0);