69 #if HAVE_SYS_RESOURCE_H
71 #include <sys/types.h>
72 #include <sys/resource.h>
73 #elif HAVE_GETPROCESSTIMES
76 #if HAVE_GETPROCESSMEMORYINFO
82 #include <sys/select.h>
87 #include <sys/ioctl.h>
139 static int transcoding_finished;
142 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
160 static struct termios oldtty;
161 static int restore_tty;
190 uint32_t *pal, *dst2;
198 if (r->
x < 0 || r->
x + r->
w > w || r->
y < 0 || r->
y + r->
h > h) {
203 dst += r->
y * dst_linesize + r->
x * 4;
206 for (y = 0; y < r->
h; y++) {
207 dst2 = (uint32_t *)dst;
209 for (x = 0; x < r->
w; x++)
210 *(dst2++) = pal[*(src2++)];
236 int64_t pts, end_pts;
253 "Impossible to get a blank canvas.\n");
256 dst = frame->
data [0];
258 for (i = 0; i < num_rects; i++)
282 if (pts2 <= ist2->sub2video.last_pts)
286 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
308 tcsetattr (0, TCSANOW, &oldtty);
332 istty = isatty(0) && isatty(2);
334 if (istty && tcgetattr (0, &tty) == 0) {
338 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
339 |INLCR|IGNCR|ICRNL|IXON);
340 tty.c_oflag |= OPOST;
341 tty.c_lflag &= ~(
ECHO|ECHONL|ICANON|IEXTEN);
342 tty.c_cflag &= ~(CSIZE|PARENB);
347 tcsetattr (0, TCSANOW, &tty);
374 n = select(1, &rfds, NULL, NULL, &tv);
383 # if HAVE_PEEKNAMEDPIPE
385 static HANDLE input_handle;
388 input_handle = GetStdHandle(STD_INPUT_HANDLE);
389 is_pipe = !GetConsoleMode(input_handle, &dw);
392 if (stdin->_cnt > 0) {
398 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
430 printf(
"bench: maxrss=%ikB\n", maxrss);
435 for (j = 0; j < filtergraphs[i]->
nb_inputs; j++) {
440 for (j = 0; j < filtergraphs[i]->
nb_outputs; j++) {
445 av_freep(&filtergraphs[i]->graph_desc);
471 av_freep(&output_streams[i]->forced_keyframes);
472 av_expr_free(output_streams[i]->forced_keyframes_pexpr);
473 av_freep(&output_streams[i]->avfilter);
474 av_freep(&output_streams[i]->logfile_prefix);
539 printf(
"bench: %8"PRIu64
" %s \n", t -
current_time, buf);
576 if(a == 0 && new_pkt.
data != pkt->
data && new_pkt.destruct) {
579 memcpy(t, new_pkt.
data, new_pkt.
size);
594 av_log(NULL,
AV_LOG_ERROR,
"Failed to open bitstream filter %s for stream %d with codec %s",
611 if (pkt->
dts < max) {
613 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
614 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
620 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
621 "in incorrect timestamps in the output file.\n",
634 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
709 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
726 int subtitle_out_max_size = 1024 * 1024;
727 int subtitle_out_size, nb, i;
757 for (i = 0; i < nb; i++) {
770 subtitle_out_max_size, sub);
771 if (subtitle_out_size < 0) {
778 pkt.
size = subtitle_out_size;
785 pkt.
pts += 90 * sub->start_display_time;
787 pkt.
pts += 90 * sub->end_display_time;
798 int ret, format_video_sync;
802 double sync_ipts,
delta;
813 sync_ipts = in_picture->
pts;
833 switch (format_video_sync) {
835 if (ost->
frame_number == 0 && delta - duration >= 0.5) {
844 else if (delta > 1.1)
845 nb_frames =
lrintf(delta);
850 else if (delta > 0.6)
862 if (nb_frames == 0) {
866 }
else if (nb_frames > 1) {
868 av_log(NULL,
AV_LOG_ERROR,
"%d frame duplication too large, skipping\n", nb_frames - 1);
877 for (i = 0; i < nb_frames; i++) {
910 int got_packet, forced_keyframe = 0;
940 av_dlog(NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
958 if (forced_keyframe) {
982 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
987 frame_size = pkt.
size;
1013 return -10.0 * log(d) / log(10.0);
1020 double ti1, bitrate, avg_bitrate;
1045 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1046 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1047 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1060 AVFrame *filtered_frame = NULL;
1085 "Error in av_buffersink_get_frame_flags(): %s\n",
av_err2str(ret));
1105 filtered_frame->
pts = frame_pts;
1112 filtered_frame->
pts = frame_pts;
1116 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1141 int frame_number, vid, i;
1143 int64_t pts = INT64_MIN;
1144 static int64_t last_time = -1;
1145 static int qp_histogram[52];
1146 int hours, mins, secs, us;
1148 if (!
print_stats && !is_last_report && !progress_avio)
1151 if (!is_last_report) {
1152 if (last_time == -1) {
1156 if ((cur_time - last_time) < 500000)
1162 oc = output_files[0]->
ctx;
1165 if (total_size <= 0)
1173 ost = output_streams[i];
1178 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1179 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1183 float fps,
t = (cur_time-timer_start) / 1000000.0;
1186 fps = t > 1 ? frame_number / t : 0;
1187 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1188 frame_number, fps < 9.95, fps, q);
1189 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1191 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1194 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1200 for (j = 0; j < 32; j++)
1201 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1205 double error, error_sum = 0;
1206 double scale, scale_sum = 0;
1208 char type[3] = {
'Y',
'U',
'V' };
1209 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1210 for (j = 0; j < 3; j++) {
1211 if (is_last_report) {
1212 error = enc->
error[j];
1213 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1222 p =
psnr(error / scale);
1223 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1224 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1227 p =
psnr(error_sum / scale_sum);
1228 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1229 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1247 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1249 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1251 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1252 "size=%8.0fkB time=", total_size / 1024.0);
1253 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1254 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1256 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1258 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1259 "bitrate=%6.1fkbits/s", bitrate);
1260 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1261 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1262 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1263 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1264 hours, mins, secs, us);
1267 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1274 fprintf(stderr,
"%s \r", buf);
1281 if (progress_avio) {
1283 is_last_report ?
"end" :
"continue");
1285 FFMIN(buf_script.len, buf_script.size - 1));
1288 if (is_last_report) {
1290 progress_avio = NULL;
1294 if (is_last_report) {
1297 av_log(NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1301 extra_size / 1024.0,
1302 100.0 * (total_size - raw) / raw
1305 av_log(NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1318 int stop_encoding = 0;
1356 ret = encode(enc, &pkt, NULL, &got_packet);
1466 opkt.
dts -= ost_tb_start_time;
1498 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1505 ost->st->codec->frame_number++;
1513 char layout_name[256];
1532 int i,
ret, err = 0, resample_changed;
1550 if (*got_output || ret<0 || pkt->
size)
1553 if (!*got_output || ret < 0) {
1578 if (resample_changed) {
1579 char layout1[64], layout2[64];
1583 "layout for Input Stream #%d.%d\n", ist->
file_index,
1595 "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",
1635 decoded_frame->
pts = pkt->
pts;
1639 decoded_frame->
pts = ist->
dts;
1645 (
AVRational){1, ist->st->codec->sample_rate});
1646 for (i = 0; i < ist->nb_filters; i++) {
1647 if (i < ist->nb_filters - 1) {
1648 f = ist->filter_frame;
1665 return err < 0 ? err :
ret;
1671 void *buffer_to_free = NULL;
1672 int i,
ret = 0, err = 0, resample_changed;
1673 int64_t best_effort_timestamp;
1685 decoded_frame, got_output, pkt);
1688 if (*got_output || ret<0 || pkt->
size)
1691 if (!*got_output || ret < 0) {
1712 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
1715 best_effort_timestamp,
1728 if (resample_changed) {
1730 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1750 if (!frame_sample_aspect->
num)
1753 if (i < ist->nb_filters - 1) {
1763 }
else if (ret < 0) {
1765 "Failed to inject frame into filter network: %s\n",
av_err2str(ret));
1773 return err < 0 ? err :
ret;
1780 &subtitle, got_output, pkt);
1782 if (*got_output || ret<0 || pkt->
size)
1785 if (ret < 0 || !*got_output) {
1795 if (end < ist->prev_sub.subtitle.end_display_time) {
1797 "Subtitle duration reduced from %d to %d\n",
1809 if (!*got_output || !subtitle.
num_rects)
1873 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
1974 avc[i] = output_files[i]->ctx;
1977 printf(
"SDP:\n%s\n", sdp);
1990 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
2007 "Error while opening decoder for input stream "
2031 int64_t va = *(int64_t *)a, vb = *(int64_t *)
b;
2032 return va < vb ? -1 : va > vb ? +1 : 0;
2042 for (p = kf; *p; p++)
2053 for (i = 0; i <
n; i++) {
2054 char *next = strchr(p,
',');
2059 if (!memcmp(p,
"chapters", 8)) {
2068 "Could not allocate forced key frames array.\n");
2100 InputFile *file = input_files[input_index];
2106 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
2115 int ret = 0, i, j, k;
2132 if (fg->
inputs[0]->
ist == input_streams[k])
2148 oc = output_files[i]->
ctx;
2164 ost = output_streams[i];
2198 if (extra_size > INT_MAX) {
2207 unsigned int codec_tag;
2254 if( copy_tb<0 && icodec->time_base.den
2280 av_log(NULL,
AV_LOG_FATAL,
"-acodec copy and -vol are incompatible (frames are not decoded)\n");
2304 "with stream copy may produce invalid files\n");
2328 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2378 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2391 if (!strncmp(ost->
enc->
name,
"libx264", 7) &&
2395 "No pixel format specified, %s for H.264 encoding chosen.\n"
2396 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2398 if (!strncmp(ost->
enc->
name,
"mpeg2video", 10) &&
2402 "No pixel format specified, %s for MPEG-2 encoding chosen.\n"
2403 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2408 codec->
width != icodec->width ||
2409 codec->
height != icodec->height ||
2410 codec->
pix_fmt != icodec->pix_fmt) {
2434 if (!codec->
width) {
2448 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2452 if (!strcmp(ost->
enc->
name,
"libx264")) {
2457 size_t logbuffer_size;
2466 f = fopen(logfilename,
"wb");
2469 logfilename, strerror(errno));
2481 ost = output_streams[i];
2482 if (ost->encoding_needed) {
2487 dec = ist->st->
codec;
2491 if (!ost->st->codec->subtitle_header) {
2503 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2504 ost->file_index, ost->index);
2510 ost->st->codec->frame_size);
2512 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
2514 " It takes bits/s as argument, not kbits/s\n");
2515 extra_size += ost->st->codec->extradata_size;
2517 if (ost->st->codec->me_threshold)
2528 ost = output_streams[i];
2552 oc = output_files[i]->
ctx;
2558 "Could not write header for output file #%d "
2559 "(incorrect codec parameters ?): %s",
2565 if (strcmp(oc->oformat->name,
"rtp")) {
2574 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2580 ist = input_streams[i];
2582 for (j = 0; j < ist->nb_filters; j++) {
2583 if (ist->filters[j]->graph->graph_desc) {
2585 ist->file_index, ist->st->index, ist->dec ? ist->dec->name :
"?",
2586 ist->filters[j]->name);
2595 ost = output_streams[i];
2597 if (ost->attachment_filename) {
2600 ost->attachment_filename, ost->file_index, ost->index);
2604 if (ost->filter && ost->filter->graph->graph_desc) {
2611 ost->index, ost->enc ? ost->enc->name :
"?");
2616 input_streams[ost->source_index]->
file_index,
2617 input_streams[ost->source_index]->
st->
index,
2620 if (ost->sync_ist != input_streams[ost->source_index])
2622 ost->sync_ist->file_index,
2623 ost->sync_ist->st->index);
2624 if (ost->stream_copy)
2628 input_streams[ost->source_index]->
dec->
name :
"?",
2629 ost->enc ? ost->enc->name :
"?");
2679 int64_t opts_min = INT64_MAX;
2697 static int64_t last_time;
2720 if (key ==
'c' || key ==
'C'){
2724 fprintf(stderr,
"\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
2726 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2731 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2733 target, time, command, arg);
2740 fprintf(stderr,
"Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
2741 }
else if (key ==
'c') {
2742 fprintf(stderr,
"Queing commands only on filters supporting the specific command is unsupported\n");
2751 "Parse error, at least 3 arguments were expected, "
2752 "only %d given in string '%s'\n", n, buf);
2755 if (key ==
'd' || key ==
'D'){
2759 if(!debug) debug = 1;
2763 if(scanf(
"%d", &debug)!=1)
2764 fprintf(stderr,
"error parsing debug value\n");
2773 fprintf(stderr,
"debug=%d\n", debug);
2776 fprintf(stderr,
"key function\n"
2777 "? show this help\n"
2778 "+ increase verbosity\n"
2779 "- decrease verbosity\n"
2780 "c Send command to first matching filter supporting it\n"
2781 "C Send/Que command to all matching filters\n"
2782 "D cycle through available debug modes\n"
2783 "h dump packets/hex press to cycle through the 3 states\n"
2785 "s Show QP histogram\n"
2792 static void *input_thread(
void *
arg)
2797 while (!transcoding_finished && ret >= 0) {
2826 if (nb_input_files == 1)
2829 transcoding_finished = 1;
2835 if (!f->fifo || f->joined)
2857 static int init_input_threads(
void)
2861 if (nb_input_files == 1)
2915 if (nb_input_files > 1)
2916 return get_input_packet_mt(f, pkt);
2925 if (output_streams[i]->unavailable)
2934 input_files[i]->eagain = 0;
2936 output_streams[i]->unavailable = 0;
2948 InputFile *ifile = input_files[file_index];
2970 ist = input_streams[ifile->
ist_index + i];
2997 goto discard_packet;
3002 goto discard_packet;
3006 "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",
3017 int64_t stime, stime2;
3024 int64_t new_start_time = INT64_MAX;
3070 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3089 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3119 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",
3155 int nb_requests, nb_requests_max = 0;
3173 for (i = 0; i < graph->
nb_inputs; i++) {
3174 ifilter = graph->
inputs[i];
3180 if (nb_requests > nb_requests_max) {
3181 nb_requests_max = nb_requests;
3246 int64_t timer_start;
3259 if ((ret = init_input_threads()) < 0)
3295 ist = input_streams[i];
3306 os = output_files[i]->
ctx;
3315 ost = output_streams[i];
3324 ist = input_streams[i];
3338 if (output_streams) {
3340 ost = output_streams[i];
3364 struct rusage rusage;
3366 getrusage(RUSAGE_SELF, &rusage);
3367 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3368 #elif HAVE_GETPROCESSTIMES
3370 FILETIME
c, e, k,
u;
3371 proc = GetCurrentProcess();
3372 GetProcessTimes(proc, &c, &e, &k, &u);
3373 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3381 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3382 struct rusage rusage;
3383 getrusage(RUSAGE_SELF, &rusage);
3384 return (int64_t)rusage.ru_maxrss * 1024;
3385 #elif HAVE_GETPROCESSMEMORYINFO
3387 PROCESS_MEMORY_COUNTERS memcounters;
3388 proc = GetCurrentProcess();
3389 memcounters.cb =
sizeof(memcounters);
3390 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3391 return memcounters.PeakPagefileUsage;
3408 setvbuf(stderr,NULL,_IONBF,0);
3413 if(argc>1 && !strcmp(argv[1],
"-d")){
3437 if (nb_output_files <= 0 && nb_input_files == 0) {
3444 if (nb_output_files <= 0) {
3459 printf(
"bench: utime=%0.3fs\n", ti / 1000000.0);
3461 av_log(NULL,
AV_LOG_DEBUG,
"%"PRIu64
" frames successfully decoded, %"PRIu64
" decoding errors\n",