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;
755 for (i = 0; i < nb; i++) {
768 subtitle_out_max_size, sub);
769 if (subtitle_out_size < 0) {
776 pkt.
size = subtitle_out_size;
783 pkt.
pts += 90 * sub->start_display_time;
785 pkt.
pts += 90 * sub->end_display_time;
796 int ret, format_video_sync;
800 double sync_ipts,
delta;
811 sync_ipts = in_picture->
pts;
821 switch (format_video_sync) {
826 else if (delta > 1.1)
827 nb_frames =
lrintf(delta);
832 else if (delta > 0.6)
844 if (nb_frames == 0) {
848 }
else if (nb_frames > 1) {
850 av_log(NULL,
AV_LOG_ERROR,
"%d frame duplication too large, skipping\n", nb_frames - 1);
859 for (i = 0; i < nb_frames; i++) {
892 int got_packet, forced_keyframe = 0;
922 av_dlog(NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
940 if (forced_keyframe) {
964 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
969 frame_size = pkt.
size;
995 return -10.0 * log(d) / log(10.0);
1002 double ti1, bitrate, avg_bitrate;
1027 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1028 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1029 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1042 AVFrame *filtered_frame = NULL;
1067 "Error in av_buffersink_get_frame_flags(): %s\n",
av_err2str(ret));
1086 filtered_frame->
pts = frame_pts;
1093 filtered_frame->
pts = frame_pts;
1097 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1122 int frame_number, vid, i;
1124 int64_t pts = INT64_MIN;
1125 static int64_t last_time = -1;
1126 static int qp_histogram[52];
1127 int hours, mins, secs, us;
1129 if (!
print_stats && !is_last_report && !progress_avio)
1132 if (!is_last_report) {
1133 if (last_time == -1) {
1137 if ((cur_time - last_time) < 500000)
1143 oc = output_files[0]->
ctx;
1146 if (total_size <= 0)
1154 ost = output_streams[i];
1159 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1160 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1164 float fps,
t = (cur_time-timer_start) / 1000000.0;
1167 fps = t > 1 ? frame_number / t : 0;
1168 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3.*f q=%3.1f ",
1169 frame_number, fps < 9.95, fps, q);
1170 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1172 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1175 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1181 for (j = 0; j < 32; j++)
1182 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(
log2(qp_histogram[j] + 1)));
1186 double error, error_sum = 0;
1187 double scale, scale_sum = 0;
1189 char type[3] = {
'Y',
'U',
'V' };
1190 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1191 for (j = 0; j < 3; j++) {
1192 if (is_last_report) {
1193 error = enc->
error[j];
1194 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1203 p =
psnr(error / scale);
1204 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j], p);
1205 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1208 p =
psnr(error_sum / scale_sum);
1209 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1210 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1228 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1230 if (total_size < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1232 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1233 "size=%8.0fkB time=", total_size / 1024.0);
1234 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1235 "%02d:%02d:%02d.%02d ", hours, mins, secs,
1237 if (bitrate < 0)
snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1239 else snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1240 "bitrate=%6.1fkbits/s", bitrate);
1241 if (total_size < 0)
av_bprintf(&buf_script,
"total_size=N/A\n");
1242 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1243 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1244 av_bprintf(&buf_script,
"out_time=%02d:%02d:%02d.%06d\n",
1245 hours, mins, secs, us);
1248 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1255 fprintf(stderr,
"%s \r", buf);
1262 if (progress_avio) {
1264 is_last_report ?
"end" :
"continue");
1266 FFMIN(buf_script.len, buf_script.size - 1));
1269 if (is_last_report) {
1271 progress_avio = NULL;
1275 if (is_last_report) {
1278 av_log(NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0f global headers:%1.0fkB muxing overhead %f%%\n",
1282 extra_size / 1024.0,
1283 100.0 * (total_size - raw) / raw
1286 av_log(NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n");
1299 int stop_encoding = 0;
1337 ret = encode(enc, &pkt, NULL, &got_packet);
1435 opkt.
dts -= ost_tb_start_time;
1467 avpicture_fill(&pict, opkt.data, ost->st->codec->pix_fmt, ost->st->codec->width, ost->st->codec->height);
1474 ost->st->codec->frame_number++;
1482 char layout_name[256];
1501 int i,
ret, err = 0, resample_changed;
1519 if (*got_output || ret<0 || pkt->
size)
1522 if (!*got_output || ret < 0) {
1547 if (resample_changed) {
1548 char layout1[64], layout2[64];
1552 "layout for Input Stream #%d.%d\n", ist->
file_index,
1564 "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",
1604 decoded_frame->
pts = pkt->
pts;
1608 decoded_frame->
pts = ist->
dts;
1614 (
AVRational){1, ist->st->codec->sample_rate});
1615 for (i = 0; i < ist->nb_filters; i++) {
1616 if (i < ist->nb_filters - 1) {
1617 f = ist->filter_frame;
1634 return err < 0 ? err :
ret;
1640 void *buffer_to_free = NULL;
1641 int i,
ret = 0, err = 0, resample_changed;
1642 int64_t best_effort_timestamp;
1654 decoded_frame, got_output, pkt);
1657 if (*got_output || ret<0 || pkt->
size)
1660 if (!*got_output || ret < 0) {
1681 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d \n",
1684 best_effort_timestamp,
1697 if (resample_changed) {
1699 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1719 if (!frame_sample_aspect->
num)
1722 if (i < ist->nb_filters - 1) {
1732 }
else if (ret < 0) {
1734 "Failed to inject frame into filter network: %s\n",
av_err2str(ret));
1742 return err < 0 ? err :
ret;
1749 &subtitle, got_output, pkt);
1751 if (*got_output || ret<0 || pkt->
size)
1754 if (ret < 0 || !*got_output) {
1764 if (end < ist->prev_sub.subtitle.end_display_time) {
1766 "Subtitle duration reduced from %d to %d\n",
1778 if (!*got_output || !subtitle.
num_rects)
1842 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
1943 avc[i] = output_files[i]->ctx;
1946 printf(
"SDP:\n%s\n", sdp);
1959 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
1976 "Error while opening decoder for input stream "
2000 int64_t va = *(int64_t *)a, vb = *(int64_t *)
b;
2001 return va < vb ? -1 : va > vb ? +1 : 0;
2011 for (p = kf; *p; p++)
2022 for (i = 0; i <
n; i++) {
2023 char *next = strchr(p,
',');
2028 if (!memcmp(p,
"chapters", 8)) {
2037 "Could not allocate forced key frames array.\n");
2069 InputFile *file = input_files[input_index];
2075 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
2084 int ret = 0, i, j, k;
2102 oc = output_files[i]->
ctx;
2118 ost = output_streams[i];
2143 if (extra_size > INT_MAX) {
2152 unsigned int codec_tag;
2199 if( copy_tb<0 && icodec->time_base.den
2225 av_log(NULL,
AV_LOG_FATAL,
"-acodec copy and -vol are incompatible (frames are not decoded)\n");
2249 "with stream copy may produce invalid files\n");
2273 snprintf(error,
sizeof(error),
"Encoder (codec %s) not found for output stream #%d:%d",
2323 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
2336 if (!strncmp(ost->
enc->
name,
"libx264", 7) &&
2340 "No pixel format specified, %s for H.264 encoding chosen.\n"
2341 "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
2346 codec->
width != icodec->width ||
2347 codec->
height != icodec->height ||
2348 codec->
pix_fmt != icodec->pix_fmt) {
2372 if (!codec->
width) {
2386 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2390 if (!strcmp(ost->
enc->
name,
"libx264")) {
2395 size_t logbuffer_size;
2404 f = fopen(logfilename,
"wb");
2407 logfilename, strerror(errno));
2419 ost = output_streams[i];
2420 if (ost->encoding_needed) {
2425 dec = ist->st->
codec;
2429 if (!ost->st->codec->subtitle_header) {
2441 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2442 ost->file_index, ost->index);
2448 ost->st->codec->frame_size);
2450 if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
2452 " It takes bits/s as argument, not kbits/s\n");
2453 extra_size += ost->st->codec->extradata_size;
2455 if (ost->st->codec->me_threshold)
2466 ost = output_streams[i];
2490 oc = output_files[i]->
ctx;
2496 "Could not write header for output file #%d "
2497 "(incorrect codec parameters ?): %s",
2503 if (strcmp(oc->oformat->name,
"rtp")) {
2512 av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2518 ist = input_streams[i];
2520 for (j = 0; j < ist->nb_filters; j++) {
2521 if (ist->filters[j]->graph->graph_desc) {
2523 ist->file_index, ist->st->index, ist->dec ? ist->dec->name :
"?",
2524 ist->filters[j]->name);
2533 ost = output_streams[i];
2535 if (ost->attachment_filename) {
2538 ost->attachment_filename, ost->file_index, ost->index);
2542 if (ost->filter && ost->filter->graph->graph_desc) {
2549 ost->index, ost->enc ? ost->enc->name :
"?");
2554 input_streams[ost->source_index]->
file_index,
2555 input_streams[ost->source_index]->
st->
index,
2558 if (ost->sync_ist != input_streams[ost->source_index])
2560 ost->sync_ist->file_index,
2561 ost->sync_ist->st->index);
2562 if (ost->stream_copy)
2566 input_streams[ost->source_index]->
dec->
name :
"?",
2567 ost->enc ? ost->enc->name :
"?");
2617 int64_t opts_min = INT64_MAX;
2635 static int64_t last_time;
2658 if (key ==
'c' || key ==
'C'){
2662 fprintf(stderr,
"\nEnter command: <target> <time> <command>[ <argument>]\n");
2664 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
2669 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2671 target, time, command, arg);
2678 fprintf(stderr,
"Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
2686 "Parse error, at least 3 arguments were expected, "
2687 "only %d given in string '%s'\n", n, buf);
2690 if (key ==
'd' || key ==
'D'){
2694 if(!debug) debug = 1;
2698 if(scanf(
"%d", &debug)!=1)
2699 fprintf(stderr,
"error parsing debug value\n");
2708 fprintf(stderr,
"debug=%d\n", debug);
2711 fprintf(stderr,
"key function\n"
2712 "? show this help\n"
2713 "+ increase verbosity\n"
2714 "- decrease verbosity\n"
2715 "c Send command to filtergraph\n"
2716 "D cycle through available debug modes\n"
2717 "h dump packets/hex press to cycle through the 3 states\n"
2719 "s Show QP histogram\n"
2726 static void *input_thread(
void *
arg)
2731 while (!transcoding_finished && ret >= 0) {
2760 if (nb_input_files == 1)
2763 transcoding_finished = 1;
2769 if (!f->fifo || f->joined)
2791 static int init_input_threads(
void)
2795 if (nb_input_files == 1)
2849 if (nb_input_files > 1)
2850 return get_input_packet_mt(f, pkt);
2859 if (output_streams[i]->unavailable)
2868 input_files[i]->eagain = 0;
2870 output_streams[i]->unavailable = 0;
2882 InputFile *ifile = input_files[file_index];
2904 ist = input_streams[ifile->
ist_index + i];
2931 goto discard_packet;
2936 goto discard_packet;
2940 "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",
2951 int64_t stime, stime2;
2958 int64_t new_start_time = INT64_MAX;
3004 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3020 pkt_dts+1<ist->
pts){
3023 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
3053 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",
3089 int nb_requests, nb_requests_max = 0;
3107 for (i = 0; i < graph->
nb_inputs; i++) {
3108 ifilter = graph->
inputs[i];
3114 if (nb_requests > nb_requests_max) {
3115 nb_requests_max = nb_requests;
3180 int64_t timer_start;
3193 if ((ret = init_input_threads()) < 0)
3229 ist = input_streams[i];
3240 os = output_files[i]->
ctx;
3249 ost = output_streams[i];
3258 ist = input_streams[i];
3272 if (output_streams) {
3274 ost = output_streams[i];
3298 struct rusage rusage;
3300 getrusage(RUSAGE_SELF, &rusage);
3301 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3302 #elif HAVE_GETPROCESSTIMES
3304 FILETIME
c, e, k,
u;
3305 proc = GetCurrentProcess();
3306 GetProcessTimes(proc, &c, &e, &k, &u);
3307 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
3315 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
3316 struct rusage rusage;
3317 getrusage(RUSAGE_SELF, &rusage);
3318 return (int64_t)rusage.ru_maxrss * 1024;
3319 #elif HAVE_GETPROCESSMEMORYINFO
3321 PROCESS_MEMORY_COUNTERS memcounters;
3322 proc = GetCurrentProcess();
3323 memcounters.cb =
sizeof(memcounters);
3324 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
3325 return memcounters.PeakPagefileUsage;
3342 setvbuf(stderr,NULL,_IONBF,0);
3347 if(argc>1 && !strcmp(argv[1],
"-d")){
3371 if (nb_output_files <= 0 && nb_input_files == 0) {
3378 if (nb_output_files <= 0) {
3393 printf(
"bench: utime=%0.3fs\n", ti / 1000000.0);
3395 av_log(NULL,
AV_LOG_DEBUG,
"%"PRIu64
" frames successfully decoded, %"PRIu64
" decoding errors\n",