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"
371 #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
374 for (i = 0; i < o->nb_ ## name; i++) {\
375 char *spec = o->name[i].specifier;\
376 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
377 outvar = o->name[i].u.type;\
389 void *dst = (uint8_t*)o + po->
u.
off;
393 int i, *count = (
int*)(so + 1);
394 for (i = 0; i < *count; i++) {
410 memset(o, 0,
sizeof(*o));
427 int h_chroma_shift, v_chroma_shift;
450 memset(buf->
base[0], 128, ret);
454 const int h_shift = i==0 ? 0 : h_chroma_shift;
455 const int v_shift = i==0 ? 0 : v_chroma_shift;
458 else if (buf->
base[i])
461 (pixel_size*edge >> h_shift), 32);
566 snprintf(args, 255,
"%d:%d:%d:%d:%d:%d:%d", ist->
st->
codec->
width,
568 sample_aspect_ratio.
num, sample_aspect_ratio.
den);
571 "src", args,
NULL, ost->graph);
575 "out",
NULL, &avsink_ctx, ost->graph);
578 last_filter = ost->input_video_filter;
581 snprintf(args, 255,
"%d:%d:flags=0x%X",
593 snprintf(args,
sizeof(args),
"flags=0x%X", (
unsigned)ost->
sws_flags);
594 ost->graph->scale_sws_opts =
av_strdup(args);
606 inputs->
filter_ctx = ost->output_video_filter;
613 if ((ret =
avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0)
620 codec->
width = ost->output_video_filter->inputs[0]->w;
621 codec->
height = ost->output_video_filter->inputs[0]->h;
625 ost->output_video_filter->inputs[0]->sample_aspect_ratio;
642 received_sigterm = sig;
643 received_nb_signals++;
658 return received_nb_signals > 1;
684 if (output_streams[i].output_frame) {
691 av_freep(&output_streams[i].forced_keyframes);
693 av_freep(&output_streams[i].avfilter);
700 av_freep(&input_streams[i].decoded_frame);
701 av_freep(&input_streams[i].filtered_frame);
724 if (received_sigterm) {
726 (
int) received_sigterm);
744 const char *codec_string = encoder ?
"encoder" :
"decoder";
749 "results.\nAdd '-strict experimental' if you want to use it.\n",
754 codec_string, codec->
name);
763 for (; *p != -1; p++) {
769 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
799 int min_dec = -1, min_inc = -1;
823 int best_dist = INT_MAX;
826 if (dist < best_dist) {
857 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
924 int fill_char = 0x00;
927 memset(buf, fill_char, size);
931 const uint8_t *buf,
int buf_size)
958 buf, buf_size, 1)) < 0) {
992 int64_t audio_buf_size;
994 int size_out, frame_bytes, resample_changed;
999 uint8_t *buf = decoded_frame->
data[0];
1001 int64_t allocated_for_size =
size;
1004 audio_buf_size = (allocated_for_size + isize * dec->
channels - 1) / (isize * dec->
channels);
1006 audio_buf_size = audio_buf_size * 2 + 10000;
1008 audio_buf_size *= osize * enc->
channels;
1010 if (audio_buf_size > INT_MAX) {
1029 if (resample_changed) {
1030 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",
1063 #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
1083 int byte_delta = idelta * isize * dec->
channels;
1086 if (fabs(delta) > 50) {
1088 if (byte_delta < 0) {
1089 byte_delta =
FFMAX(byte_delta, -size);
1093 -byte_delta / (isize * dec->
channels));
1098 static uint8_t *input_tmp =
NULL;
1099 input_tmp =
av_realloc(input_tmp, byte_delta + size);
1101 if (byte_delta > allocated_for_size - size) {
1102 allocated_for_size = byte_delta + (int64_t)size;
1108 memcpy(input_tmp + byte_delta, buf, size);
1129 (
short *)buftmp, (
short *)buf,
1131 size_out = size_out * enc->
channels * osize;
1138 const void *ibuf[6] = { buftmp };
1140 int istride[6] = { isize };
1141 int ostride[6] = { osize };
1142 int len = size_out / istride[0];
1144 printf(
"av_audio_convert() failed\n");
1150 size_out = len * osize;
1192 picture2 = &picture_tmp;
1207 if (picture != picture2)
1208 *picture = *picture2;
1218 static uint8_t *subtitle_out =
NULL;
1219 int subtitle_out_max_size = 1024 * 1024;
1220 int subtitle_out_size, nb, i;
1233 if (!subtitle_out) {
1234 subtitle_out =
av_malloc(subtitle_out_max_size);
1245 for (i = 0; i < nb; i++) {
1252 subtitle_out_max_size, sub);
1253 if (subtitle_out_size < 0) {
1260 pkt.
data = subtitle_out;
1261 pkt.
size = subtitle_out_size;
1278 #if !CONFIG_AVFILTER
1284 int resample_changed = 0;
1285 *out_picture = in_picture;
1291 if (resample_changed) {
1293 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1303 if (resample_changed) {
1320 0, ost->
resample_height, (*out_picture)->data, (*out_picture)->linesize);
1322 if (resample_changed) {
1335 int *frame_size,
float quality)
1337 int nb_frames, i, ret, format_video_sync;
1357 double vdelta = sync_ipts - ost->
sync_opts;
1361 else if (format_video_sync ==
VSYNC_VFR) {
1362 if (vdelta <= -0.6) {
1364 }
else if (vdelta > 0.6)
1366 }
else if (vdelta > 1.1)
1367 nb_frames =
lrintf(vdelta);
1369 if (nb_frames == 0) {
1372 }
else if (nb_frames > 1) {
1383 #if !CONFIG_AVFILTER
1386 final_picture = in_picture;
1390 for (i = 0; i < nb_frames; i++) {
1402 pkt.
data = (uint8_t *)final_picture;
1411 big_picture = *final_picture;
1424 big_picture.
quality = quality;
1437 bit_buffer, bit_buffer_size,
1478 return -10.0 * log(d) / log(10.0);
1486 double ti1, bitrate, avg_bitrate;
1511 avg_bitrate = (double)(
video_size * 8) / ti1 / 1000.0;
1512 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1513 (
double)
video_size / 1024, ti1, bitrate, avg_bitrate);
1527 int frame_number, vid, i;
1528 double bitrate, ti1, pts;
1529 static int64_t last_time = -1;
1530 static int qp_histogram[52];
1535 if (!is_last_report) {
1539 if (last_time == -1) {
1543 if ((cur_time - last_time) < 500000)
1549 oc = output_files[0].
ctx;
1558 for (i = 0; i < nb_ostreams; i++) {
1560 ost = &ost_table[i];
1565 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"q=%2.1f ", q);
1571 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"frame=%5d fps=%3d q=%3.1f ",
1572 frame_number, (t > 1) ? (
int)(frame_number / t + 0.5) : 0, q);
1574 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"L");
1580 for (j = 0; j < 32; j++)
1581 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%X", (
int)
lrintf(log(qp_histogram[j] + 1) / log(2)));
1585 double error, error_sum = 0;
1586 double scale, scale_sum = 0;
1587 char type[3] = {
'Y',
'U',
'V' };
1588 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"PSNR=");
1589 for (j = 0; j < 3; j++) {
1590 if (is_last_report) {
1591 error = enc->
error[j];
1592 scale = enc->
width * enc->
height * 255.0 * 255.0 * frame_number;
1601 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%c:%2.2f ", type[j],
psnr(error / scale));
1603 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"*:%2.2f ",
psnr(error_sum / scale_sum));
1609 if ((pts < ti1) && (pts > 0))
1615 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1617 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
1618 "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
1619 (
double)total_size / 1024, ti1, bitrate);
1622 snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
" dup=%d drop=%d",
1629 if (is_last_report) {
1632 av_log(
NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1635 extra_size / 1024.0,
1636 100.0 * (total_size - raw) / raw
1645 for (i = 0; i < nb_ostreams; i++) {
1649 int stop_encoding = 0;
1669 if (fifo_bytes > 0) {
1671 int frame_bytes = fifo_bytes;
1779 opkt.
dts -= ost_tb_start_time;
1852 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1854 *volp++ = av_clip_uint8(v);
1861 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1863 *volp++ = av_clip_int16(v);
1870 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1872 *volp++ = av_clipl_int32(v);
1880 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1889 for (i = 0; i < (decoded_data_size /
sizeof(*volp)); i++) {
1896 "Audio volume adjustment on sample format %s is not supported.\n",
1918 void *buffer_to_free =
NULL;
1922 int frame_available = 1;
1930 pkt->
pts = *pkt_pts;
1935 decoded_frame, got_output, pkt);
1962 int frame_size, resample_changed;
1971 if (resample_changed) {
1973 "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
1979 if (configure_video_filters(ist, ost)) {
2018 while (frame_available) {
2020 if (ost->output_video_filter)
2027 filtered_frame = decoded_frame;
2035 frame_available = ost->output_video_filter &&
avfilter_poll_frame(ost->output_video_filter->inputs[0]);
2050 &subtitle, got_output, pkt);
2108 "Multiple frames in a packet from stream %d\n", pkt->
stream_index);
2157 for (i = 0; pkt && i < nb_ostreams; i++) {
2177 for (i = 0; i < n; i++)
2178 avc[i] = output_files[i].ctx;
2181 printf(
"SDP:\n%s\n", sdp);
2187 char *error,
int error_len)
2194 snprintf(error, error_len,
"Decoder (codec id %d) not found for input stream #%d:%d",
2218 snprintf(error, error_len,
"Error while opening decoder for input stream #%d:%d",
2241 for (p = kf; *p; p++)
2252 for (i = 0; i < n; i++) {
2253 char *next = strchr(p,
',');
2270 int ret = 0, i, j, k;
2288 oc = output_files[i].
ctx;
2298 ost = &output_streams[i];
2315 if (extra_size > INT_MAX) {
2415 icodec->request_channels = codec-> channels;
2439 #if !CONFIG_AVFILTER
2475 if (configure_video_filters(ist, ost)) {
2495 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
2498 if (!strcmp(ost->
enc->
name,
"libx264")) {
2502 f = fopen(logfilename,
"wb");
2505 logfilename, strerror(errno));
2511 size_t logbuffer_size;
2538 ost = &output_streams[i];
2543 snprintf(error,
sizeof(error),
"Encoder (codec id %d) not found for output stream #%d:%d",
2560 snprintf(error,
sizeof(error),
"Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2569 "It takes bits/s as argument, not kbits/s\n");
2579 if ((ret =
init_input_stream(i, output_streams, nb_output_streams, error,
sizeof(error))) < 0)
2600 oc = output_files[i].
ctx;
2603 snprintf(error,
sizeof(error),
"Could not write header for output file #%d (incorrect codec parameters ?)", i);
2617 av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
2623 ost = &output_streams[i];
2655 print_sdp(output_files, nb_output_files);
2674 int no_packet_count = 0;
2677 if (!(no_packet =
av_mallocz(nb_input_files)))
2680 ret =
transcode_init(output_files, nb_output_files, input_files, nb_input_files);
2690 int file_index, ist_index;
2705 ost = &output_streams[i];
2717 if (ipts < ipts_min) {
2722 if (opts < opts_min) {
2735 if (file_index < 0) {
2736 if (no_packet_count) {
2737 no_packet_count = 0;
2738 memset(no_packet, 0, nb_input_files);
2746 is = input_files[file_index].
ctx;
2749 no_packet[file_index] = 1;
2761 no_packet_count = 0;
2762 memset(no_packet, 0, nb_input_files);
2771 goto discard_packet;
2773 ist = &input_streams[ist_index];
2775 goto discard_packet;
2798 "timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
2807 if (
output_packet(ist, output_streams, nb_output_streams, &pkt) < 0) {
2821 print_report(output_files, output_streams, nb_output_streams, 0, timer_start);
2826 ist = &input_streams[i];
2837 os = output_files[i].
ctx;
2846 ost = &output_streams[i];
2858 ist = &input_streams[i];
2871 if (output_streams) {
2873 ost = &output_streams[i];
2906 p = strchr(arg,
':');
2908 x = strtol(arg, &end, 10);
2910 y = strtol(end + 1, &end, 10);
2912 ar = (double)x / (
double)y;
2914 ar = strtod(arg,
NULL);
2946 int i, negative = 0, file_idx;
2947 int sync_file_idx = -1, sync_stream_idx;
2958 if (sync = strchr(map,
',')) {
2960 sync_file_idx = strtol(sync + 1, &sync, 0);
2967 for (i = 0; i < input_files[sync_file_idx].
nb_streams; i++)
2969 input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
2970 sync_stream_idx = i;
2973 if (i == input_files[sync_file_idx].nb_streams) {
2975 "match any streams.\n", arg);
2981 file_idx = strtol(map, &p, 0);
2993 *p ==
':' ? p + 1 : p) > 0)
2997 for (i = 0; i < input_files[file_idx].
nb_streams; i++) {
2999 *p ==
':' ? p + 1 : p) <= 0)
3008 if (sync_file_idx >= 0) {
3048 if (*(++arg) && *arg !=
':') {
3052 *stream_spec = *arg ==
':' ? arg + 1 :
"";
3056 if (*(++arg) ==
':')
3057 *index = strtol(++arg,
NULL, 0);
3072 char type_in, type_out;
3073 const char *istream_spec =
NULL, *ostream_spec =
NULL;
3074 int idx_in = 0, idx_out = 0;
3079 if (type_in ==
'g' || type_out ==
'g')
3081 if (type_in ==
's' || type_out ==
's')
3083 if (type_in ==
'c' || type_out ==
'c')
3086 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
3087 if ((index) < 0 || (index) >= (nb_elems)) {\
3088 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
3093 #define SET_DICT(type, meta, context, index)\
3096 meta = &context->metadata;\
3099 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
3100 meta = &context->chapters[index]->metadata;\
3103 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
3104 meta = &context->programs[index]->metadata;\
3110 SET_DICT(type_in, meta_in, ic, idx_in);
3111 SET_DICT(type_out, meta_out, oc, idx_out);
3114 if (type_in ==
's') {
3128 if (type_out ==
's') {
3144 const char *codec_string = encoder ?
"encoder" :
"decoder";
3154 if (codec->
type != type) {
3163 char *codec_name =
NULL;
3234 (strchr(filename,
':') ==
NULL || filename[1] ==
':' ||
3238 fprintf(stderr,
"File '%s' already exists. Overwrite ? [y/N] ", filename);
3241 fprintf(stderr,
"Not overwriting - exiting\n");
3246 fprintf(stderr,
"File '%s' already exists. Exiting.\n", filename);
3265 filename = e->
value;
3293 int orig_nb_streams;
3302 if (!strcmp(filename,
"-"))
3306 !strcmp(filename,
"/dev/stdin");
3397 for (i = 0; i < orig_nb_streams; i++)
3416 while ((c =
avio_r8(s)) && c !=
'\n')
3427 char filename[1000];
3428 const char *base[3] = { getenv(
"AVCONV_DATADIR"),
3437 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
3438 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
3442 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
3443 i != 1 ?
"" :
"/.avconv", preset_name);
3452 char *codec_name =
NULL;
3459 }
else if (!strcmp(codec_name,
"copy"))
3472 char *bsf =
NULL, *next, *codec_tag =
NULL;
3502 if (!buf[0] || buf[0] ==
'#') {
3506 if (!(arg = strchr(buf,
'='))) {
3518 "Preset %s specified for stream %d:%d, but could not be opened.\n",
3532 if (next = strchr(bsf,
','))
3539 bsfc_prev->
next = bsfc;
3549 uint32_t
tag = strtol(codec_tag, &next, 0);
3571 const char *p = str;
3593 video_enc = st->
codec;
3596 const char *p =
NULL;
3615 if (frame_aspect_ratio)
3643 for (i = 0; p; i++) {
3645 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
3708 audio_enc = st->
codec;
3712 char *sample_fmt =
NULL;
3757 subtitle_enc = st->
codec;
3772 p = strchr(idx_str,
':');
3775 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
3800 if (in_ch->
end < ts_off)
3809 out_ch->id = in_ch->
id;
3811 out_ch->start =
FFMAX(0, in_ch->
start - ts_off);
3812 out_ch->end =
FFMIN(rt, in_ch->
end - ts_off);
3835 if (!strcmp(filename,
"-"))
3846 if (!file_oformat) {
3852 if (!file_oformat) {
3865 #define NEW_STREAM(type, index)\
3867 ost = new_ ## type ## _stream(o, oc);\
3868 ost->source_index = index;\
3869 ost->sync_ist = &input_streams[index];\
3870 input_streams[index].discard = 0;\
3875 int area = 0, idx = -1;
3877 ist = &input_streams[i];
3889 int channels = 0, idx = -1;
3891 ist = &input_streams[i];
3940 uint8_t *attachment;
4015 if (in_file_index < 0)
4018 av_log(
NULL,
AV_LOG_FATAL,
"Invalid input file index %d while processing metadata maps\n", in_file_index);
4030 if (input_files[i].ctx->nb_chapters) {
4051 if (output_streams[i].source_index < 0)
4061 const char *stream_spec;
4062 int index = 0, j, ret;
4080 printf(
"ret %d, stream_spec %s\n", ret, stream_spec);
4115 struct rusage rusage;
4117 getrusage(RUSAGE_SELF, &rusage);
4118 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
4119 #elif HAVE_GETPROCESSTIMES
4121 FILETIME c, e, k, u;
4122 proc = GetCurrentProcess();
4123 GetProcessTimes(proc, &c, &e, &k, &u);
4124 return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
4132 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
4133 struct rusage rusage;
4134 getrusage(RUSAGE_SELF, &rusage);
4135 return (int64_t)rusage.ru_maxrss * 1024;
4136 #elif HAVE_GETPROCESSMEMORYINFO
4138 PROCESS_MEMORY_COUNTERS memcounters;
4139 proc = GetCurrentProcess();
4140 memcounters.cb =
sizeof(memcounters);
4141 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
4142 return memcounters.PeakPagefileUsage;
4155 printf(
"Hyper fast Audio and Video encoder\n");
4156 printf(
"usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n",
program_name);
4196 enum {
PAL,
NTSC, FILM, UNKNOWN } norm = UNKNOWN;
4197 static const char *
const frame_rates[] = {
"25",
"30000/1001",
"24000/1001" };
4199 if (!strncmp(arg,
"pal-", 4)) {
4202 }
else if (!strncmp(arg,
"ntsc-", 5)) {
4205 }
else if (!strncmp(arg,
"film-", 5)) {
4213 for (i = 0; i < input_files[j].
nb_streams; i++) {
4222 }
else if ((fr == 29970) || (fr == 23976)) {
4227 if (norm != UNKNOWN)
4231 if (norm != UNKNOWN)
4235 if (norm == UNKNOWN) {
4242 if (!strcmp(arg,
"vcd")) {
4269 }
else if (!strcmp(arg,
"svcd")) {
4291 }
else if (!strcmp(arg,
"dvd")) {
4312 }
else if (!strncmp(arg,
"dv", 2)) {
4317 parse_option(o,
"pix_fmt", !strncmp(arg,
"dv50", 4) ?
"yuv422p" :
4318 norm ==
PAL ?
"yuv420p" :
"yuv411p", options);
4342 struct tm *today = localtime(&today2);
4344 snprintf(filename,
sizeof(filename),
"vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
4395 #define OFFSET(x) offsetof(OptionsContext, x)
4405 {
"map",
HAS_ARG |
OPT_EXPERT |
OPT_FUNC2, {(
void*)
opt_map},
"set input stream mapping",
"[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
4407 "outfile[,metadata]:infile[,metadata]" },
4417 "add timings for benchmarking" },
4420 "dump each input packet" },
4422 "when dumping packets, also dump the payload" },
4424 {
"target",
HAS_ARG |
OPT_FUNC2, {(
void*)
opt_target},
"specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)",
"type" },
4456 "use same quantizer as source (implies VBR)" },
4460 "deinterlace pictures" },
4541 fprintf(stderr,
"At least one output file must be specified\n");
4556 printf(
"bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);