57 #if HAVE_SYS_RESOURCE_H
58 #include <sys/types.h>
60 #include <sys/resource.h>
61 #elif HAVE_GETPROCESSTIMES
64 #if HAVE_GETPROCESSMEMORYINFO
70 #include <sys/select.h>
80 #define VSYNC_PASSTHROUGH 0
146 #define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
370 #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
373 for (i = 0; i < o->nb_ ## name; i++) {\
374 char *spec = o->name[i].specifier;\
375 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
376 outvar = o->name[i].u.type;\
388 void *dst = (uint8_t*)o + po->
u.
off;
392 int i, *count = (
int*)(so + 1);
393 for (i = 0; i < *count; i++) {
409 memset(o, 0,
sizeof(*o));
426 int h_chroma_shift, v_chroma_shift;
449 memset(buf->
base[0], 128, ret);
453 const int h_shift = i==0 ? 0 : h_chroma_shift;
454 const int v_shift = i==0 ? 0 : v_chroma_shift;
460 (pixel_size*edge >> h_shift), 32);
565 snprintf(args, 255,
"%d:%d:%d:%d:%d:%d:%d", ist->
st->
codec->
width,
567 sample_aspect_ratio.
num, sample_aspect_ratio.
den);
570 "src", args,
NULL, ost->graph);
574 "out",
NULL, &avsink_ctx, ost->graph);
577 last_filter = ost->input_video_filter;
580 snprintf(args, 255,
"%d:%d:flags=0x%X",
592 snprintf(args,
sizeof(args),
"flags=0x%X", (
unsigned)ost->
sws_flags);
593 ost->graph->scale_sws_opts =
av_strdup(args);
605 inputs->
filter_ctx = ost->output_video_filter;
612 if ((ret =
avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0)
619 codec->
width = ost->output_video_filter->inputs[0]->w;
620 codec->
height = ost->output_video_filter->inputs[0]->h;
624 ost->output_video_filter->inputs[0]->sample_aspect_ratio;
641 received_sigterm = sig;
642 received_nb_signals++;
657 return received_nb_signals > 1;
683 if (output_streams[i].output_frame) {
691 av_freep(&output_streams[i].avfilter);
698 av_freep(&input_streams[i].decoded_frame);
699 av_freep(&input_streams[i].filtered_frame);
722 if (received_sigterm) {
724 (
int) received_sigterm);
742 const char *codec_string = encoder ?
"encoder" :
"decoder";
747 "results.\nAdd '-strict experimental' if you want to use it.\n",
752 codec_string, codec->
name);
761 for (; *p != -1; p++) {
767 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
797 int min_dec = -1, min_inc = -1;
821 int best_dist = INT_MAX;
824 if (dist < best_dist) {
855 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
922 int fill_char = 0x00;
925 memset(buf, fill_char, size);
929 const uint8_t *buf,
int buf_size)
956 buf, buf_size, 1)) < 0) {
990 int64_t audio_buf_size;
992 int size_out, frame_bytes, resample_changed;
997 uint8_t *buf = decoded_frame->
data[0];
999 int64_t allocated_for_size =
size;
1002 audio_buf_size = (allocated_for_size + isize * dec->
channels - 1) / (isize * dec->
channels);
1004 audio_buf_size = audio_buf_size * 2 + 10000;
1006 audio_buf_size *= osize * enc->
channels;
1008 if (audio_buf_size > INT_MAX) {
1027 if (resample_changed) {
1028 av_log(
NULL,
AV_LOG_INFO,
"Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
1061 #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
1081 int byte_delta = idelta * isize * dec->
channels;
1084 if (fabs(delta) > 50) {
1086 if (byte_delta < 0) {
1087 byte_delta =
FFMAX(byte_delta, -size);
1091 -byte_delta / (isize * dec->
channels));
1096 static uint8_t *input_tmp =
NULL;
1097 input_tmp =
av_realloc(input_tmp, byte_delta + size);
1099 if (byte_delta > allocated_for_size - size) {
1100 allocated_for_size = byte_delta + (int64_t)size;
1106 memcpy(input_tmp + byte_delta, buf, size);
1127 (
short *)buftmp, (
short *)buf,
1129 size_out = size_out * enc->
channels * osize;
1136 const void *ibuf[6] = { buftmp };
1138 int istride[6] = { isize };
1139 int ostride[6] = { osize };
1140 int len = size_out / istride[0];
1142 printf(
"av_audio_convert() failed\n");
1148 size_out = len * osize;
1190 picture2 = &picture_tmp;
1205 if (picture != picture2)
1206 *picture = *picture2;
1216 static uint8_t *subtitle_out =
NULL;
1217 int subtitle_out_max_size = 1024 * 1024;
1218 int subtitle_out_size, nb, i;
1231 if (!subtitle_out) {
1232 subtitle_out =
av_malloc(subtitle_out_max_size);
1243 for (i = 0; i < nb; i++) {
1250 subtitle_out_max_size, sub);
1251 if (subtitle_out_size < 0) {
1258 pkt.
data = subtitle_out;
1259 pkt.
size = subtitle_out_size;
1276 #if !CONFIG_AVFILTER
1282 int resample_changed = 0;
1283 *out_picture = in_picture;
1289 if (resample_changed) {
1291 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1301 if (resample_changed) {
1318 0, ost->
resample_height, (*out_picture)->data, (*out_picture)->linesize);
1320 if (resample_changed) {
1333 int *frame_size,
float quality)
1335 int nb_frames, i, ret, format_video_sync;
1355 double vdelta = sync_ipts - ost->
sync_opts;
1359 else if (format_video_sync ==
VSYNC_VFR) {
1360 if (vdelta <= -0.6) {
1362 }
else if (vdelta > 0.6)
1364 }
else if (vdelta > 1.1)
1365 nb_frames =
lrintf(vdelta);
1367 if (nb_frames == 0) {
1370 }
else if (nb_frames > 1) {
1381 #if !CONFIG_AVFILTER
1384 final_picture = in_picture;
1388 for (i = 0; i < nb_frames; i++) {
1400 pkt.
data = (uint8_t *)final_picture;
1409 big_picture = *final_picture;
1422 big_picture.
quality = quality;
1435 bit_buffer, bit_buffer_size,
1476 return -10.0 * log(d) / log(10.0);
1484 double ti1, bitrate, avg_bitrate;
1509 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1510 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1511 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1525 int frame_number, vid, i;
1526 double bitrate, ti1, pts;
1527 static int64_t last_time = -1;
1528 static int qp_histogram[52];
1533 if (!is_last_report) {
1537 if (last_time == -1) {
1541 if ((cur_time - last_time) < 500000)
1547 oc = output_files[0].
ctx;
1556 for (i = 0; i < nb_ostreams; i++) {
1558 ost = &ost_table[i];
1563 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1569 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3d q=%3.1f ",
1570 frame_number, (t > 1) ? (
int)(frame_number / t + 0.5) : 0, q);
1572 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1578 for (j = 0; j < 32; j++)
1579 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(log(qp_histogram[j] + 1) / log(2)));
1583 double error, error_sum = 0;
1584 double scale, scale_sum = 0;
1585 char type[3] = {
'Y',
'U',
'V' };
1586 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1587 for (j = 0; j < 3; j++) {
1588 if (is_last_report) {
1589 error = enc->
error[j];
1590 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1599 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j],
psnr(error / scale));
1601 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1607 if ((pts < ti1) && (pts > 0))
1613 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1615 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1616 "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
1617 (
double)total_size / 1024, ti1, bitrate);
1620 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1627 if (is_last_report) {
1630 av_log(
NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1633 extra_size / 1024.0,
1634 100.0 * (total_size - raw) / raw
1643 for (i = 0; i < nb_ostreams; i++) {
1647 int stop_encoding = 0;
1667 if (fifo_bytes > 0) {
1669 int frame_bytes = fifo_bytes;
1777 opkt.
dts -= ost_tb_start_time;
1850 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1852 *volp++ = av_clip_uint8(v);
1859 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1861 *volp++ = av_clip_int16(v);
1868 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1870 *volp++ = av_clipl_int32(v);
1878 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1887 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1894 "Audio volume adjustment on sample format %s is not supported.\n",
1916 void *buffer_to_free =
NULL;
1920 int frame_available = 1;
1928 pkt->
pts = *pkt_pts;
1933 decoded_frame, got_output, pkt);
1960 int frame_size, resample_changed;
1969 if (resample_changed) {
1971 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1977 if (configure_video_filters(ist, ost)) {
2016 while (frame_available) {
2018 if (ost->output_video_filter)
2025 filtered_frame = decoded_frame;
2033 frame_available = ost->output_video_filter &&
avfilter_poll_frame(ost->output_video_filter->inputs[0]);
2048 &subtitle, got_output, pkt);
2106 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
2155 for (i = 0; pkt && i < nb_ostreams; i++) {
2175 for (i = 0; i < n; i++)
2176 avc[i] = output_files[i].ctx;
2179 printf(
"SDP:\n%s\n", sdp);
2185 char *error,
int error_len)
2192 snprintf(error, error_len,
"Decoder (codec id %d) not found for input stream #%d:%d",
2216 snprintf(error, error_len,
"Error while opening decoder for input stream #%d:%d",
2237 int ret = 0, i, j, k;
2255 oc = output_files[i].
ctx;
2265 ost = &output_streams[i];
2282 if (extra_size > INT_MAX) {
2382 icodec->request_channels = codec-> channels;
2406 #if !CONFIG_AVFILTER
2442 if (configure_video_filters(ist, ost)) {
2459 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2462 if (!strcmp(ost->
enc->
name,
"libx264")) {
2466 f = fopen(logfilename,
"wb");
2469 logfilename, strerror(errno));
2475 size_t logbuffer_size;
2502 ost = &output_streams[i];
2507 snprintf(error,
sizeof(error),
"Encoder (codec id %d) not found for output stream #%d:%d",
2524 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2533 "It takes bits/s as argument, not kbits/s\n");
2543 if ((ret =
init_input_stream(i, output_streams, nb_output_streams, error,
sizeof(error))) < 0)
2564 oc = output_files[i].
ctx;
2567 snprintf(error,
sizeof(error),
"Could not write header for output file #%d (incorrect codec parameters ?)", i);
2581 av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
2587 ost = &output_streams[i];
2619 print_sdp(output_files, nb_output_files);
2638 int no_packet_count = 0;
2641 if (!(no_packet =
av_mallocz(nb_input_files)))
2644 ret =
transcode_init(output_files, nb_output_files, input_files, nb_input_files);
2654 int file_index, ist_index;
2669 ost = &output_streams[i];
2679 if (ipts < ipts_min) {
2684 if (opts < opts_min) {
2697 if (file_index < 0) {
2698 if (no_packet_count) {
2699 no_packet_count = 0;
2700 memset(no_packet, 0, nb_input_files);
2708 is = input_files[file_index].
ctx;
2711 no_packet[file_index] = 1;
2723 no_packet_count = 0;
2724 memset(no_packet, 0, nb_input_files);
2733 goto discard_packet;
2735 ist = &input_streams[ist_index];
2737 goto discard_packet;
2760 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
2769 if (
output_packet(ist, output_streams, nb_output_streams, &pkt) < 0) {
2783 print_report(output_files, output_streams, nb_output_streams, 0, timer_start);
2788 ist = &input_streams[i];
2799 os = output_files[i].
ctx;
2808 ost = &output_streams[i];
2820 ist = &input_streams[i];
2833 if (output_streams) {
2835 ost = &output_streams[i];
2868 p = strchr(arg,
':');
2870 x = strtol(arg, &end, 10);
2872 y = strtol(end + 1, &end, 10);
2874 ar = (double)x / (
double)y;
2876 ar = strtod(arg,
NULL);
2908 int i, negative = 0, file_idx;
2909 int sync_file_idx = -1, sync_stream_idx;
2920 if (sync = strchr(map,
',')) {
2922 sync_file_idx = strtol(sync + 1, &sync, 0);
2929 for (i = 0; i < input_files[sync_file_idx].
nb_streams; i++)
2931 input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
2932 sync_stream_idx = i;
2935 if (i == input_files[sync_file_idx].nb_streams) {
2937 "match any streams.\n", arg);
2943 file_idx = strtol(map, &p, 0);
2955 *p ==
':' ? p + 1 : p) > 0)
2959 for (i = 0; i < input_files[file_idx].
nb_streams; i++) {
2961 *p ==
':' ? p + 1 : p) <= 0)
2970 if (sync_file_idx >= 0) {
3010 if (*(++arg) && *arg !=
':') {
3014 *stream_spec = *arg ==
':' ? arg + 1 :
"";
3018 if (*(++arg) ==
':')
3019 *index = strtol(++arg,
NULL, 0);
3034 char type_in, type_out;
3035 const char *istream_spec =
NULL, *ostream_spec =
NULL;
3036 int idx_in = 0, idx_out = 0;
3041 if (type_in ==
'g' || type_out ==
'g')
3043 if (type_in ==
's' || type_out ==
's')
3045 if (type_in ==
'c' || type_out ==
'c')
3048 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
3049 if ((index) < 0 || (index) >= (nb_elems)) {\
3050 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
3055 #define SET_DICT(type, meta, context, index)\
3058 meta = &context->metadata;\
3061 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
3062 meta = &context->chapters[index]->metadata;\
3065 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
3066 meta = &context->programs[index]->metadata;\
3070 SET_DICT(type_in, meta_in, ic, idx_in);
3071 SET_DICT(type_out, meta_out, oc, idx_out);
3074 if (type_in ==
's') {
3088 if (type_out ==
's') {
3104 const char *codec_string = encoder ?
"encoder" :
"decoder";
3114 if (codec->
type != type) {
3123 char *codec_name =
NULL;
3194 (strchr(filename,
':') ==
NULL || filename[1] ==
':' ||
3198 fprintf(stderr,
"File '%s' already exists. Overwrite ? [y/N] ", filename);
3201 fprintf(stderr,
"Not overwriting - exiting\n");
3206 fprintf(stderr,
"File '%s' already exists. Exiting.\n", filename);
3225 filename = e->
value;
3253 int orig_nb_streams;
3262 if (!strcmp(filename,
"-"))
3266 !strcmp(filename,
"/dev/stdin");
3357 for (i = 0; i < orig_nb_streams; i++)
3372 for (p = kf; *p; p++)
3381 for (i = 0; i < n; i++) {
3382 p = i ? strchr(p,
',') + 1 : kf;
3399 while ((c =
avio_r8(s)) && c !=
'\n')
3410 char filename[1000];
3411 const char *base[3] = { getenv(
"AVCONV_DATADIR"),
3420 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
3421 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
3425 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
3426 i != 1 ?
"" :
"/.avconv", preset_name);
3435 char *codec_name =
NULL;
3442 }
else if (!strcmp(codec_name,
"copy"))
3455 char *bsf =
NULL, *next, *codec_tag =
NULL;
3488 if (!buf[0] || buf[0] ==
'#') {
3492 if (!(arg = strchr(buf,
'='))) {
3504 "Preset %s specified for stream %d:%d, but could not be opened.\n",
3514 if (next = strchr(bsf,
','))
3521 bsfc_prev->
next = bsfc;
3531 uint32_t
tag = strtol(codec_tag, &next, 0);
3553 const char *p = str;
3575 video_enc = st->
codec;
3578 const char *p =
NULL;
3597 if (frame_aspect_ratio)
3625 for (i = 0; p; i++) {
3627 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
3663 if (forced_key_frames)
3692 audio_enc = st->
codec;
3696 char *sample_fmt =
NULL;
3741 subtitle_enc = st->
codec;
3756 p = strchr(idx_str,
':');
3759 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
3784 if (in_ch->
end < ts_off)
3793 out_ch->id = in_ch->
id;
3795 out_ch->start =
FFMAX(0, in_ch->
start - ts_off);
3796 out_ch->end =
FFMIN(rt, in_ch->
end - ts_off);
3819 if (!strcmp(filename,
"-"))
3830 if (!file_oformat) {
3836 if (!file_oformat) {
3849 #define NEW_STREAM(type, index)\
3851 ost = new_ ## type ## _stream(o, oc);\
3852 ost->source_index = index;\
3853 ost->sync_ist = &input_streams[index];\
3854 input_streams[index].discard = 0;\
3859 int area = 0, idx = -1;
3861 ist = &input_streams[i];
3873 int channels = 0, idx = -1;
3875 ist = &input_streams[i];
3924 uint8_t *attachment;
3999 if (in_file_index < 0)
4002 av_log(
NULL,
AV_LOG_FATAL,
"Invalid input file index %d while processing metadata maps\n", in_file_index);
4014 if (input_files[i].ctx->nb_chapters) {
4035 if (output_streams[i].source_index < 0)
4045 const char *stream_spec;
4046 int index = 0, j, ret;
4064 printf(
"ret %d, stream_spec %s\n", ret, stream_spec);
4099 struct rusage rusage;
4101 getrusage(RUSAGE_SELF, &rusage);
4102 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
4103 #elif HAVE_GETPROCESSTIMES
4105 FILETIME c, e, k, u;
4106 proc = GetCurrentProcess();
4107 GetProcessTimes(proc, &c, &e, &k, &u);
4108 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
4116 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
4117 struct rusage rusage;
4118 getrusage(RUSAGE_SELF, &rusage);
4119 return (int64_t)rusage.ru_maxrss * 1024;
4120 #elif HAVE_GETPROCESSMEMORYINFO
4122 PROCESS_MEMORY_COUNTERS memcounters;
4123 proc = GetCurrentProcess();
4124 memcounters.cb =
sizeof(memcounters);
4125 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
4126 return memcounters.PeakPagefileUsage;
4139 printf(
"Hyper fast Audio and Video encoder\n");
4140 printf(
"usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n",
program_name);
4180 enum {
PAL,
NTSC, FILM, UNKNOWN } norm = UNKNOWN;
4181 static const char *
const frame_rates[] = {
"25",
"30000/1001",
"24000/1001" };
4183 if (!strncmp(arg,
"pal-", 4)) {
4186 }
else if (!strncmp(arg,
"ntsc-", 5)) {
4189 }
else if (!strncmp(arg,
"film-", 5)) {
4197 for (i = 0; i < input_files[j].
nb_streams; i++) {
4205 }
else if ((fr == 29970) || (fr == 23976)) {
4210 if (norm != UNKNOWN)
4214 if (norm != UNKNOWN)
4218 if (norm == UNKNOWN) {
4225 if (!strcmp(arg,
"vcd")) {
4252 }
else if (!strcmp(arg,
"svcd")) {
4274 }
else if (!strcmp(arg,
"dvd")) {
4295 }
else if (!strncmp(arg,
"dv", 2)) {
4300 parse_option(o,
"pix_fmt", !strncmp(arg,
"dv50", 4) ?
"yuv422p" :
4301 norm ==
PAL ?
"yuv420p" :
"yuv411p", options);
4325 struct tm *today = localtime(&today2);
4327 snprintf(filename,
sizeof(filename),
"vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
4378 #define OFFSET(x) offsetof(OptionsContext, x)
4388 {
"map",
HAS_ARG |
OPT_EXPERT |
OPT_FUNC2, {(
void*)
opt_map},
"set input stream mapping",
"[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
4390 "outfile[,metadata]:infile[,metadata]" },
4400 "add timings for benchmarking" },
4403 "dump each input packet" },
4405 "when dumping packets, also dump the payload" },
4407 {
"target",
HAS_ARG |
OPT_FUNC2, {(
void*)
opt_target},
"specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)",
"type" },
4439 "use same quantizer as source (implies VBR)" },
4443 "deinterlace pictures" },
4524 fprintf(stderr,
"At least one output file must be specified\n");
4539 printf(
"bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);