12 #include "./vpx_config.h"
23 #include "third_party/libyuv/include/libyuv/scale.h"
33 #include "./tools_common.h"
35 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
38 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
42 #include "vpx/vpx_integer.h"
43 #include "vpx_ports/mem_ops.h"
44 #include "vpx_ports/vpx_timer.h"
45 #include "./rate_hist.h"
46 #include "./vpxstats.h"
47 #include "./warnings.h"
49 #include "./webmenc.h"
51 #include "./y4minput.h"
53 static size_t wrap_fwrite(
const void *ptr,
size_t size,
size_t nmemb,
55 return fwrite(ptr, size, nmemb, stream);
57 #define fwrite wrap_fwrite
59 static const char *exec_name;
61 static VPX_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
66 vfprintf(stderr, s, ap);
69 if (detail) fprintf(stderr,
" %s\n", detail);
71 if (fatal) exit(EXIT_FAILURE);
75 static VPX_TOOLS_FORMAT_PRINTF(2,
81 warn_or_exit_on_errorv(ctx, 1, s, ap);
85 static VPX_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
90 warn_or_exit_on_errorv(ctx, fatal, s, ap);
94 static const arg_def_t help =
95 ARG_DEF(NULL,
"help", 0,
"Show usage options and exit");
96 static const arg_def_t debugmode =
97 ARG_DEF(
"D",
"debug", 0,
"Debug mode (makes output deterministic)");
98 static const arg_def_t outputfile =
99 ARG_DEF(
"o",
"output", 1,
"Output filename");
100 static const arg_def_t use_nv12 =
101 ARG_DEF(NULL,
"nv12", 0,
"Input file is NV12 ");
102 static const arg_def_t use_yv12 =
103 ARG_DEF(NULL,
"yv12", 0,
"Input file is YV12 ");
104 static const arg_def_t use_i420 =
105 ARG_DEF(NULL,
"i420", 0,
"Input file is I420 (default)");
106 static const arg_def_t use_i422 =
107 ARG_DEF(NULL,
"i422", 0,
"Input file is I422");
108 static const arg_def_t use_i444 =
109 ARG_DEF(NULL,
"i444", 0,
"Input file is I444");
110 static const arg_def_t use_i440 =
111 ARG_DEF(NULL,
"i440", 0,
"Input file is I440");
112 static const arg_def_t codecarg = ARG_DEF(NULL,
"codec", 1,
"Codec to use");
113 static const arg_def_t passes =
114 ARG_DEF(
"p",
"passes", 1,
"Number of passes (1/2)");
115 static const arg_def_t pass_arg =
116 ARG_DEF(NULL,
"pass", 1,
"Pass to execute (1/2)");
117 static const arg_def_t fpf_name =
118 ARG_DEF(NULL,
"fpf", 1,
"First pass statistics file name");
119 static const arg_def_t limit =
120 ARG_DEF(NULL,
"limit", 1,
"Stop encoding after n input frames");
121 static const arg_def_t skip =
122 ARG_DEF(NULL,
"skip", 1,
"Skip the first n input frames");
123 static const arg_def_t deadline =
124 ARG_DEF(
"d",
"deadline", 1,
"Deadline per frame (usec)");
125 static const arg_def_t best_dl =
126 ARG_DEF(NULL,
"best", 0,
"Use Best Quality Deadline");
127 static const arg_def_t good_dl =
128 ARG_DEF(NULL,
"good", 0,
"Use Good Quality Deadline");
129 static const arg_def_t rt_dl =
130 ARG_DEF(NULL,
"rt", 0,
"Use Realtime Quality Deadline");
131 static const arg_def_t quietarg =
132 ARG_DEF(
"q",
"quiet", 0,
"Do not print encode progress");
133 static const arg_def_t verbosearg =
134 ARG_DEF(
"v",
"verbose", 0,
"Show encoder parameters");
135 static const arg_def_t psnrarg =
136 ARG_DEF(NULL,
"psnr", 0,
"Show PSNR in status line");
138 static const struct arg_enum_list test_decode_enum[] = {
139 {
"off", TEST_DECODE_OFF },
140 {
"fatal", TEST_DECODE_FATAL },
141 {
"warn", TEST_DECODE_WARN },
144 static const arg_def_t recontest = ARG_DEF_ENUM(
145 NULL,
"test-decode", 1,
"Test encode/decode mismatch", test_decode_enum);
146 static const arg_def_t framerate =
147 ARG_DEF(NULL,
"fps", 1,
"Stream frame rate (rate/scale)");
148 static const arg_def_t use_webm =
149 ARG_DEF(NULL,
"webm", 0,
"Output WebM (default when WebM IO is enabled)");
150 static const arg_def_t use_ivf = ARG_DEF(NULL,
"ivf", 0,
"Output IVF");
151 static const arg_def_t out_part =
152 ARG_DEF(
"P",
"output-partitions", 0,
153 "Makes encoder output partitions. Requires IVF output!");
154 static const arg_def_t q_hist_n =
155 ARG_DEF(NULL,
"q-hist", 1,
"Show quantizer histogram (n-buckets)");
156 static const arg_def_t rate_hist_n =
157 ARG_DEF(NULL,
"rate-hist", 1,
"Show rate histogram (n-buckets)");
158 static const arg_def_t disable_warnings =
159 ARG_DEF(NULL,
"disable-warnings", 0,
160 "Disable warnings about potentially incorrect encode settings.");
161 static const arg_def_t disable_warning_prompt =
162 ARG_DEF(
"y",
"disable-warning-prompt", 0,
163 "Display warnings, but do not prompt user to continue.");
165 #if CONFIG_VP9_HIGHBITDEPTH
166 static const arg_def_t test16bitinternalarg = ARG_DEF(
167 NULL,
"test-16bit-internal", 0,
"Force use of 16 bit internal buffer");
170 static const arg_def_t *main_args[] = { &help,
192 &disable_warning_prompt,
196 static const arg_def_t usage =
197 ARG_DEF(
"u",
"usage", 1,
"Usage profile number to use");
198 static const arg_def_t threads =
199 ARG_DEF(
"t",
"threads", 1,
"Max number of threads to use");
200 static const arg_def_t profile =
201 ARG_DEF(NULL,
"profile", 1,
"Bitstream profile number to use");
202 static const arg_def_t width = ARG_DEF(
"w",
"width", 1,
"Frame width");
203 static const arg_def_t height = ARG_DEF(
"h",
"height", 1,
"Frame height");
205 static const struct arg_enum_list stereo_mode_enum[] = {
206 {
"mono", STEREO_FORMAT_MONO },
207 {
"left-right", STEREO_FORMAT_LEFT_RIGHT },
208 {
"bottom-top", STEREO_FORMAT_BOTTOM_TOP },
209 {
"top-bottom", STEREO_FORMAT_TOP_BOTTOM },
210 {
"right-left", STEREO_FORMAT_RIGHT_LEFT },
213 static const arg_def_t stereo_mode = ARG_DEF_ENUM(
214 NULL,
"stereo-mode", 1,
"Stereo 3D video format", stereo_mode_enum);
216 static const arg_def_t timebase = ARG_DEF(
217 NULL,
"timebase", 1,
"Output timestamp precision (fractional seconds)");
218 static const arg_def_t error_resilient =
219 ARG_DEF(NULL,
"error-resilient", 1,
"Enable error resiliency features");
220 static const arg_def_t lag_in_frames =
221 ARG_DEF(NULL,
"lag-in-frames", 1,
"Max number of frames to lag");
223 static const arg_def_t *global_args[] = { &use_nv12,
240 #if CONFIG_VP9_HIGHBITDEPTH
241 &test16bitinternalarg,
246 static const arg_def_t dropframe_thresh =
247 ARG_DEF(NULL,
"drop-frame", 1,
"Temporal resampling threshold (buf %)");
248 static const arg_def_t resize_allowed =
249 ARG_DEF(NULL,
"resize-allowed", 1,
"Spatial resampling enabled (bool)");
250 static const arg_def_t resize_width =
251 ARG_DEF(NULL,
"resize-width", 1,
"Width of encoded frame");
252 static const arg_def_t resize_height =
253 ARG_DEF(NULL,
"resize-height", 1,
"Height of encoded frame");
254 static const arg_def_t resize_up_thresh =
255 ARG_DEF(NULL,
"resize-up", 1,
"Upscale threshold (buf %)");
256 static const arg_def_t resize_down_thresh =
257 ARG_DEF(NULL,
"resize-down", 1,
"Downscale threshold (buf %)");
258 static const struct arg_enum_list end_usage_enum[] = { {
"vbr",
VPX_VBR },
263 static const arg_def_t end_usage =
264 ARG_DEF_ENUM(NULL,
"end-usage", 1,
"Rate control mode", end_usage_enum);
265 static const arg_def_t target_bitrate =
266 ARG_DEF(NULL,
"target-bitrate", 1,
"Bitrate (kbps)");
267 static const arg_def_t min_quantizer =
268 ARG_DEF(NULL,
"min-q", 1,
"Minimum (best) quantizer");
269 static const arg_def_t max_quantizer =
270 ARG_DEF(NULL,
"max-q", 1,
"Maximum (worst) quantizer");
271 static const arg_def_t undershoot_pct =
272 ARG_DEF(NULL,
"undershoot-pct", 1,
"Datarate undershoot (min) target (%)");
273 static const arg_def_t overshoot_pct =
274 ARG_DEF(NULL,
"overshoot-pct", 1,
"Datarate overshoot (max) target (%)");
275 static const arg_def_t buf_sz =
276 ARG_DEF(NULL,
"buf-sz", 1,
"Client buffer size (ms)");
277 static const arg_def_t buf_initial_sz =
278 ARG_DEF(NULL,
"buf-initial-sz", 1,
"Client initial buffer size (ms)");
279 static const arg_def_t buf_optimal_sz =
280 ARG_DEF(NULL,
"buf-optimal-sz", 1,
"Client optimal buffer size (ms)");
281 static const arg_def_t *rc_args[] = {
282 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
283 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
284 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
285 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
288 #if CONFIG_VP9_ENCODER
289 static const arg_def_t use_vizier_rc_params =
290 ARG_DEF(NULL,
"use-vizier-rc-params", 1,
"Use vizier rc params");
291 static const arg_def_t active_wq_factor =
292 ARG_DEF(NULL,
"active-wq-factor", 1,
"Active worst quality factor");
293 static const arg_def_t err_per_mb_factor =
294 ARG_DEF(NULL,
"err-per-mb-factor", 1,
"Error per macroblock factor");
295 static const arg_def_t sr_default_decay_limit = ARG_DEF(
296 NULL,
"sr-default-decay-limit", 1,
"Second reference default decay limit");
297 static const arg_def_t sr_diff_factor =
298 ARG_DEF(NULL,
"sr-diff-factor", 1,
"Second reference diff factor");
299 static const arg_def_t kf_err_per_mb_factor = ARG_DEF(
300 NULL,
"kf-err-per-mb-factor", 1,
"Keyframe error per macroblock factor");
301 static const arg_def_t kf_frame_min_boost_factor =
302 ARG_DEF(NULL,
"kf-frame-min-boost-factor", 1,
"Keyframe min boost");
303 static const arg_def_t kf_frame_max_boost_first_factor =
304 ARG_DEF(NULL,
"kf-frame-max-boost-first-factor", 1,
305 "Max keyframe boost adjustment factor for first frame");
306 static const arg_def_t kf_frame_max_boost_subs_factor =
307 ARG_DEF(NULL,
"kf-frame-max-boost-subs-factor", 1,
308 "Max boost adjustment factor for subsequent KFs");
309 static const arg_def_t kf_max_total_boost_factor = ARG_DEF(
310 NULL,
"kf-max-total-boost-factor", 1,
"Keyframe max total boost factor");
311 static const arg_def_t gf_max_total_boost_factor =
312 ARG_DEF(NULL,
"gf-max-total-boost-factor", 1,
313 "Golden frame max total boost factor");
314 static const arg_def_t gf_frame_max_boost_factor =
315 ARG_DEF(NULL,
"gf-frame-max-boost-factor", 1,
316 "Golden frame max per frame boost factor");
317 static const arg_def_t zm_factor =
318 ARG_DEF(NULL,
"zm-factor", 1,
"Zero motion power factor");
319 static const arg_def_t rd_mult_inter_qp_fac =
320 ARG_DEF(NULL,
"rd-mult-inter-qp-fac", 1,
321 "RD multiplier adjustment for inter frames");
322 static const arg_def_t rd_mult_arf_qp_fac =
323 ARG_DEF(NULL,
"rd-mult-arf-qp-fac", 1,
324 "RD multiplier adjustment for alt-ref frames");
325 static const arg_def_t rd_mult_key_qp_fac = ARG_DEF(
326 NULL,
"rd-mult-key-qp-fac", 1,
"RD multiplier adjustment for key frames");
327 static const arg_def_t *vizier_rc_args[] = { &use_vizier_rc_params,
330 &sr_default_decay_limit,
332 &kf_err_per_mb_factor,
333 &kf_frame_min_boost_factor,
334 &kf_frame_max_boost_first_factor,
335 &kf_frame_max_boost_subs_factor,
336 &kf_max_total_boost_factor,
337 &gf_max_total_boost_factor,
338 &gf_frame_max_boost_factor,
340 &rd_mult_inter_qp_fac,
346 static const arg_def_t bias_pct =
347 ARG_DEF(NULL,
"bias-pct", 1,
"CBR/VBR bias (0=CBR, 100=VBR)");
348 static const arg_def_t minsection_pct =
349 ARG_DEF(NULL,
"minsection-pct", 1,
"GOP min bitrate (% of target)");
350 static const arg_def_t maxsection_pct =
351 ARG_DEF(NULL,
"maxsection-pct", 1,
"GOP max bitrate (% of target)");
352 static const arg_def_t corpus_complexity =
353 ARG_DEF(NULL,
"corpus-complexity", 1,
"corpus vbr complexity midpoint");
354 static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
356 &corpus_complexity, NULL };
358 static const arg_def_t kf_min_dist =
359 ARG_DEF(NULL,
"kf-min-dist", 1,
"Minimum keyframe interval (frames)");
360 static const arg_def_t kf_max_dist =
361 ARG_DEF(NULL,
"kf-max-dist", 1,
"Maximum keyframe interval (frames)");
362 static const arg_def_t kf_disabled =
363 ARG_DEF(NULL,
"disable-kf", 0,
"Disable keyframe placement");
364 static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
367 static const arg_def_t noise_sens =
368 ARG_DEF(NULL,
"noise-sensitivity", 1,
"Noise sensitivity (frames to blur)");
369 static const arg_def_t sharpness =
370 ARG_DEF(NULL,
"sharpness", 1,
371 "Increase sharpness at the expense of lower PSNR. (0..7)");
372 static const arg_def_t static_thresh =
373 ARG_DEF(NULL,
"static-thresh", 1,
"Motion detection threshold");
374 static const arg_def_t arnr_maxframes =
375 ARG_DEF(NULL,
"arnr-maxframes", 1,
"AltRef max frames (0..15)");
376 static const arg_def_t arnr_strength =
377 ARG_DEF(NULL,
"arnr-strength", 1,
"AltRef filter strength (0..6)");
378 static const arg_def_t arnr_type =
379 ARG_DEF(NULL,
"arnr-type", 1,
"AltRef filter type (1..3)");
380 static const struct arg_enum_list tuning_enum[] = { {
"psnr", VP8_TUNE_PSNR },
381 {
"ssim", VP8_TUNE_SSIM },
383 static const arg_def_t tune_ssim =
384 ARG_DEF_ENUM(NULL,
"tune", 1,
"Material to favor", tuning_enum);
385 static const arg_def_t cq_level =
386 ARG_DEF(NULL,
"cq-level", 1,
"Constant/Constrained Quality level");
387 static const arg_def_t max_intra_rate_pct =
388 ARG_DEF(NULL,
"max-intra-rate", 1,
"Max I-frame bitrate (pct)");
389 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
390 NULL,
"gf-cbr-boost", 1,
"Boost for Golden Frame in CBR mode (pct)");
392 #if CONFIG_VP8_ENCODER
393 static const arg_def_t cpu_used_vp8 =
394 ARG_DEF(NULL,
"cpu-used", 1,
"CPU Used (-16..16)");
395 static const arg_def_t auto_altref_vp8 = ARG_DEF(
396 NULL,
"auto-alt-ref", 1,
"Enable automatic alt reference frames. (0..1)");
397 static const arg_def_t token_parts =
398 ARG_DEF(NULL,
"token-parts", 1,
"Number of token partitions to use, log2");
399 static const arg_def_t screen_content_mode =
400 ARG_DEF(NULL,
"screen-content-mode", 1,
"Screen content mode");
401 static const arg_def_t *vp8_args[] = { &cpu_used_vp8,
414 &screen_content_mode,
433 #if CONFIG_VP9_ENCODER
434 static const arg_def_t cpu_used_vp9 =
435 ARG_DEF(NULL,
"cpu-used", 1,
"CPU Used (-9..9)");
436 static const arg_def_t auto_altref_vp9 = ARG_DEF(
437 NULL,
"auto-alt-ref", 1,
438 "Enable automatic alt reference frames, 2+ enables multi-layer. (0..6)");
439 static const arg_def_t tile_cols =
440 ARG_DEF(NULL,
"tile-columns", 1,
"Number of tile columns to use, log2");
441 static const arg_def_t tile_rows =
442 ARG_DEF(NULL,
"tile-rows", 1,
443 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
445 static const arg_def_t enable_tpl_model =
446 ARG_DEF(NULL,
"enable-tpl", 1,
"Enable temporal dependency model");
448 static const arg_def_t lossless =
449 ARG_DEF(NULL,
"lossless", 1,
"Lossless mode (0: false (default), 1: true)");
450 static const arg_def_t frame_parallel_decoding = ARG_DEF(
451 NULL,
"frame-parallel", 1,
"Enable frame parallel decodability features");
452 static const arg_def_t aq_mode = ARG_DEF(
454 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
455 "3: cyclic refresh, 4: equator360)");
456 static const arg_def_t alt_ref_aq = ARG_DEF(NULL,
"alt-ref-aq", 1,
457 "Special adaptive quantization for "
458 "the alternate reference frames.");
459 static const arg_def_t frame_periodic_boost =
460 ARG_DEF(NULL,
"frame-boost", 1,
461 "Enable frame periodic boost (0: off (default), 1: on)");
462 static const arg_def_t max_inter_rate_pct =
463 ARG_DEF(NULL,
"max-inter-rate", 1,
"Max P-frame bitrate (pct)");
464 static const arg_def_t min_gf_interval = ARG_DEF(
465 NULL,
"min-gf-interval", 1,
466 "min gf/arf frame interval (default 0, indicating in-built behavior)");
467 static const arg_def_t max_gf_interval = ARG_DEF(
468 NULL,
"max-gf-interval", 1,
469 "max gf/arf frame interval (default 0, indicating in-built behavior)");
471 static const struct arg_enum_list color_space_enum[] = {
483 static const arg_def_t input_color_space =
484 ARG_DEF_ENUM(NULL,
"color-space", 1,
485 "The color space of input content:", color_space_enum);
487 #if CONFIG_VP9_HIGHBITDEPTH
488 static const struct arg_enum_list bitdepth_enum[] = {
492 static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
494 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
496 static const arg_def_t inbitdeptharg =
497 ARG_DEF(NULL,
"input-bit-depth", 1,
"Bit depth of input");
500 static const struct arg_enum_list tune_content_enum[] = {
501 {
"default", VP9E_CONTENT_DEFAULT },
502 {
"screen", VP9E_CONTENT_SCREEN },
503 {
"film", VP9E_CONTENT_FILM },
507 static const arg_def_t tune_content = ARG_DEF_ENUM(
508 NULL,
"tune-content", 1,
"Tune content type", tune_content_enum);
510 static const arg_def_t target_level = ARG_DEF(
511 NULL,
"target-level", 1,
513 " 255: off (default)\n"
514 " 0: only keep level stats\n"
515 " 1: adaptively set alt-ref "
516 "distance and column tile limit based on picture size, and keep"
518 " 10: level 1.0 11: level 1.1 "
519 "... 62: level 6.2");
521 static const arg_def_t row_mt =
522 ARG_DEF(NULL,
"row-mt", 1,
523 "Enable row based non-deterministic multi-threading in VP9");
525 static const arg_def_t disable_loopfilter =
526 ARG_DEF(NULL,
"disable-loopfilter", 1,
527 "Control Loopfilter in VP9:\n"
529 "0: Loopfilter on for all frames (default)\n"
531 "1: Loopfilter off for non reference frames\n"
533 "2: Loopfilter off for all frames");
536 #if CONFIG_VP9_ENCODER
537 static const arg_def_t *vp9_args[] = { &cpu_used_vp9,
553 &frame_parallel_decoding,
556 &frame_periodic_boost,
568 #if CONFIG_VP9_HIGHBITDEPTH
571 #endif // CONFIG_VP9_HIGHBITDEPTH
604 static const arg_def_t *no_args[] = { NULL };
606 static void show_help(FILE *fout,
int shorthelp) {
608 const int num_encoder = get_vpx_encoder_count();
610 fprintf(fout,
"Usage: %s <options> -o dst_filename src_filename \n",
614 fprintf(fout,
"Use --help to see the full list of options.\n");
618 fprintf(fout,
"\nOptions:\n");
619 arg_show_usage(fout, main_args);
620 fprintf(fout,
"\nEncoder Global Options:\n");
621 arg_show_usage(fout, global_args);
622 fprintf(fout,
"\nRate Control Options:\n");
623 arg_show_usage(fout, rc_args);
624 fprintf(fout,
"\nTwopass Rate Control Options:\n");
625 arg_show_usage(fout, rc_twopass_args);
626 fprintf(fout,
"\nKeyframe Placement Options:\n");
627 arg_show_usage(fout, kf_args);
628 #if CONFIG_VP8_ENCODER
629 fprintf(fout,
"\nVP8 Specific Options:\n");
630 arg_show_usage(fout, vp8_args);
632 #if CONFIG_VP9_ENCODER
633 fprintf(fout,
"\nVP9 Specific Options:\n");
634 arg_show_usage(fout, vp9_args);
635 fprintf(fout,
"\nVizier Rate Control Options:\n");
636 arg_show_usage(fout, vizier_rc_args);
639 "\nStream timebase (--timebase):\n"
640 " The desired precision of timestamps in the output, expressed\n"
641 " in fractional seconds. Default is 1/1000.\n");
642 fprintf(fout,
"\nIncluded encoders:\n\n");
644 for (i = 0; i < num_encoder; ++i) {
645 const VpxInterface *
const encoder = get_vpx_encoder_by_index(i);
646 const char *defstr = (i == (num_encoder - 1)) ?
"(default)" :
"";
647 fprintf(fout,
" %-6s - %s %s\n", encoder->name,
650 fprintf(fout,
"\n ");
651 fprintf(fout,
"Use --codec to switch to a non-default encoder.\n\n");
654 void usage_exit(
void) {
655 show_help(stderr, 1);
659 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
660 #if CONFIG_VP9_ENCODER
661 #define ARG_CTRL_CNT_MAX NELEMENTS(vp9_arg_ctrl_map)
663 #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
667 typedef int stereo_format_t;
668 struct WebmOutputContext {
674 struct stream_config {
677 const char *stats_fn;
678 stereo_format_t stereo_fmt;
679 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
682 #if CONFIG_VP9_HIGHBITDEPTH
684 int use_16bit_internal;
688 struct stream_state {
690 struct stream_state *next;
691 struct stream_config config;
693 struct rate_hist *rate_hist;
694 struct WebmOutputContext webm_ctx;
695 uint64_t psnr_sse_total;
696 uint64_t psnr_samples_total;
697 double psnr_totals[4];
701 unsigned int frames_out;
710 static void validate_positive_rational(
const char *msg,
717 if (rat->
num < 0) die(
"Error: %s must be positive\n", msg);
719 if (!rat->
den) die(
"Error: %s has zero denominator\n", msg);
722 static void parse_global_config(
struct VpxEncoderConfig *global,
char **argv) {
725 const int num_encoder = get_vpx_encoder_count();
727 if (num_encoder < 1) die(
"Error: no valid encoder available\n");
730 memset(global, 0,
sizeof(*global));
731 global->codec = get_vpx_encoder_by_index(num_encoder - 1);
733 global->color_type = I420;
737 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
740 if (arg_match(&arg, &help, argi)) {
741 show_help(stdout, 0);
743 }
else if (arg_match(&arg, &codecarg, argi)) {
744 global->codec = get_vpx_encoder_by_name(arg.val);
746 die(
"Error: Unrecognized argument (%s) to --codec\n", arg.val);
747 }
else if (arg_match(&arg, &passes, argi)) {
748 global->passes = arg_parse_uint(&arg);
750 if (global->passes < 1 || global->passes > 2)
751 die(
"Error: Invalid number of passes (%d)\n", global->passes);
752 }
else if (arg_match(&arg, &pass_arg, argi)) {
753 global->pass = arg_parse_uint(&arg);
755 if (global->pass < 1 || global->pass > 2)
756 die(
"Error: Invalid pass selected (%d)\n", global->pass);
757 }
else if (arg_match(&arg, &usage, argi))
758 global->usage = arg_parse_uint(&arg);
759 else if (arg_match(&arg, &deadline, argi))
760 global->deadline = arg_parse_uint(&arg);
761 else if (arg_match(&arg, &best_dl, argi))
763 else if (arg_match(&arg, &good_dl, argi))
765 else if (arg_match(&arg, &rt_dl, argi))
767 else if (arg_match(&arg, &use_yv12, argi))
768 global->color_type = YV12;
769 else if (arg_match(&arg, &use_nv12, argi))
770 global->color_type = NV12;
771 else if (arg_match(&arg, &use_i420, argi))
772 global->color_type = I420;
773 else if (arg_match(&arg, &use_i422, argi))
774 global->color_type = I422;
775 else if (arg_match(&arg, &use_i444, argi))
776 global->color_type = I444;
777 else if (arg_match(&arg, &use_i440, argi))
778 global->color_type = I440;
779 else if (arg_match(&arg, &quietarg, argi))
781 else if (arg_match(&arg, &verbosearg, argi))
783 else if (arg_match(&arg, &limit, argi))
784 global->limit = arg_parse_uint(&arg);
785 else if (arg_match(&arg, &skip, argi))
786 global->skip_frames = arg_parse_uint(&arg);
787 else if (arg_match(&arg, &psnrarg, argi))
788 global->show_psnr = 1;
789 else if (arg_match(&arg, &recontest, argi))
790 global->test_decode = arg_parse_enum_or_int(&arg);
791 else if (arg_match(&arg, &framerate, argi)) {
792 global->framerate = arg_parse_rational(&arg);
793 validate_positive_rational(arg.name, &global->framerate);
794 global->have_framerate = 1;
795 }
else if (arg_match(&arg, &out_part, argi))
796 global->out_part = 1;
797 else if (arg_match(&arg, &debugmode, argi))
799 else if (arg_match(&arg, &q_hist_n, argi))
800 global->show_q_hist_buckets = arg_parse_uint(&arg);
801 else if (arg_match(&arg, &rate_hist_n, argi))
802 global->show_rate_hist_buckets = arg_parse_uint(&arg);
803 else if (arg_match(&arg, &disable_warnings, argi))
804 global->disable_warnings = 1;
805 else if (arg_match(&arg, &disable_warning_prompt, argi))
806 global->disable_warning_prompt = 1;
813 if (global->pass > global->passes) {
814 warn(
"Assuming --pass=%d implies --passes=%d\n", global->pass,
816 global->passes = global->pass;
820 if (global->passes == 0) {
821 #if CONFIG_VP9_ENCODER
824 if (global->codec != NULL && global->codec->name != NULL)
825 global->passes = (strcmp(global->codec->name,
"vp9") == 0 &&
835 warn(
"Enforcing one-pass encoding in realtime mode\n");
840 static struct stream_state *new_stream(
struct VpxEncoderConfig *global,
841 struct stream_state *prev) {
842 struct stream_state *stream;
844 stream = calloc(1,
sizeof(*stream));
845 if (stream == NULL) {
846 fatal(
"Failed to allocate new stream.");
850 memcpy(stream, prev,
sizeof(*stream));
858 &stream->config.cfg, global->usage);
864 stream->config.cfg.g_timebase.den = 1000;
869 stream->config.cfg.g_w = 0;
870 stream->config.cfg.g_h = 0;
873 stream->config.write_webm = 1;
875 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
876 stream->webm_ctx.last_pts_ns = -1;
877 stream->webm_ctx.writer = NULL;
878 stream->webm_ctx.segment = NULL;
882 stream->webm_ctx.debug = global->debug;
886 stream->config.cfg.rc_end_usage == 1)
887 stream->config.cfg.g_lag_in_frames = 0;
891 stream->config.out_fn = NULL;
897 static int parse_stream_params(
struct VpxEncoderConfig *global,
898 struct stream_state *stream,
char **argv) {
901 static const arg_def_t **ctrl_args = no_args;
902 static const int *ctrl_args_map = NULL;
903 struct stream_config *config = &stream->config;
904 int eos_mark_found = 0;
905 #if CONFIG_VP9_HIGHBITDEPTH
906 int test_16bit_internal = 0;
911 #if CONFIG_VP8_ENCODER
912 }
else if (strcmp(global->codec->name,
"vp8") == 0) {
913 ctrl_args = vp8_args;
914 ctrl_args_map = vp8_arg_ctrl_map;
916 #if CONFIG_VP9_ENCODER
917 }
else if (strcmp(global->codec->name,
"vp9") == 0) {
918 ctrl_args = vp9_args;
919 ctrl_args_map = vp9_arg_ctrl_map;
923 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
929 if (eos_mark_found) {
932 }
else if (!strcmp(*argj,
"--")) {
937 if (arg_match(&arg, &outputfile, argi)) {
938 config->out_fn = arg.val;
939 }
else if (arg_match(&arg, &fpf_name, argi)) {
940 config->stats_fn = arg.val;
941 }
else if (arg_match(&arg, &use_webm, argi)) {
943 config->write_webm = 1;
945 die(
"Error: --webm specified but webm is disabled.");
947 }
else if (arg_match(&arg, &use_ivf, argi)) {
948 config->write_webm = 0;
949 }
else if (arg_match(&arg, &threads, argi)) {
950 config->cfg.g_threads = arg_parse_uint(&arg);
951 }
else if (arg_match(&arg, &profile, argi)) {
952 config->cfg.g_profile = arg_parse_uint(&arg);
953 }
else if (arg_match(&arg, &width, argi)) {
954 config->cfg.g_w = arg_parse_uint(&arg);
955 }
else if (arg_match(&arg, &height, argi)) {
956 config->cfg.g_h = arg_parse_uint(&arg);
957 #if CONFIG_VP9_HIGHBITDEPTH
958 }
else if (arg_match(&arg, &bitdeptharg, argi)) {
959 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
960 }
else if (arg_match(&arg, &inbitdeptharg, argi)) {
961 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
964 }
else if (arg_match(&arg, &stereo_mode, argi)) {
965 config->stereo_fmt = arg_parse_enum_or_int(&arg);
967 }
else if (arg_match(&arg, &timebase, argi)) {
968 config->cfg.g_timebase = arg_parse_rational(&arg);
969 validate_positive_rational(arg.name, &config->cfg.g_timebase);
970 }
else if (arg_match(&arg, &error_resilient, argi)) {
971 config->cfg.g_error_resilient = arg_parse_uint(&arg);
972 }
else if (arg_match(&arg, &end_usage, argi)) {
973 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
974 }
else if (arg_match(&arg, &lag_in_frames, argi)) {
975 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
977 config->cfg.rc_end_usage ==
VPX_CBR &&
978 config->cfg.g_lag_in_frames != 0) {
979 warn(
"non-zero %s option ignored in realtime CBR mode.\n", arg.name);
980 config->cfg.g_lag_in_frames = 0;
982 }
else if (arg_match(&arg, &dropframe_thresh, argi)) {
983 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
984 }
else if (arg_match(&arg, &resize_allowed, argi)) {
985 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
986 }
else if (arg_match(&arg, &resize_width, argi)) {
987 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
988 }
else if (arg_match(&arg, &resize_height, argi)) {
989 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
990 }
else if (arg_match(&arg, &resize_up_thresh, argi)) {
991 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
992 }
else if (arg_match(&arg, &resize_down_thresh, argi)) {
993 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
994 }
else if (arg_match(&arg, &end_usage, argi)) {
995 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
996 }
else if (arg_match(&arg, &target_bitrate, argi)) {
997 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
998 }
else if (arg_match(&arg, &min_quantizer, argi)) {
999 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1000 }
else if (arg_match(&arg, &max_quantizer, argi)) {
1001 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1002 }
else if (arg_match(&arg, &undershoot_pct, argi)) {
1003 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1004 }
else if (arg_match(&arg, &overshoot_pct, argi)) {
1005 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1006 }
else if (arg_match(&arg, &buf_sz, argi)) {
1007 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1008 }
else if (arg_match(&arg, &buf_initial_sz, argi)) {
1009 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1010 }
else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1011 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1012 }
else if (arg_match(&arg, &bias_pct, argi)) {
1013 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1014 if (global->passes < 2)
1015 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1016 }
else if (arg_match(&arg, &minsection_pct, argi)) {
1017 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1019 if (global->passes < 2)
1020 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1021 }
else if (arg_match(&arg, &maxsection_pct, argi)) {
1022 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1024 if (global->passes < 2)
1025 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1026 }
else if (arg_match(&arg, &corpus_complexity, argi)) {
1027 config->cfg.rc_2pass_vbr_corpus_complexity = arg_parse_uint(&arg);
1029 if (global->passes < 2)
1030 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1031 }
else if (arg_match(&arg, &kf_min_dist, argi)) {
1032 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1033 }
else if (arg_match(&arg, &kf_max_dist, argi)) {
1034 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1035 }
else if (arg_match(&arg, &kf_disabled, argi)) {
1037 #if CONFIG_VP9_ENCODER
1038 }
else if (arg_match(&arg, &use_vizier_rc_params, argi)) {
1039 config->cfg.use_vizier_rc_params = arg_parse_int(&arg);
1040 }
else if (arg_match(&arg, &active_wq_factor, argi)) {
1041 config->cfg.active_wq_factor = arg_parse_rational(&arg);
1042 }
else if (arg_match(&arg, &err_per_mb_factor, argi)) {
1043 config->cfg.err_per_mb_factor = arg_parse_rational(&arg);
1044 }
else if (arg_match(&arg, &sr_default_decay_limit, argi)) {
1045 config->cfg.sr_default_decay_limit = arg_parse_rational(&arg);
1046 }
else if (arg_match(&arg, &sr_diff_factor, argi)) {
1047 config->cfg.sr_diff_factor = arg_parse_rational(&arg);
1048 }
else if (arg_match(&arg, &kf_err_per_mb_factor, argi)) {
1049 config->cfg.kf_err_per_mb_factor = arg_parse_rational(&arg);
1050 }
else if (arg_match(&arg, &kf_frame_min_boost_factor, argi)) {
1051 config->cfg.kf_frame_min_boost_factor = arg_parse_rational(&arg);
1052 }
else if (arg_match(&arg, &kf_frame_max_boost_first_factor, argi)) {
1053 config->cfg.kf_frame_max_boost_first_factor = arg_parse_rational(&arg);
1054 }
else if (arg_match(&arg, &kf_frame_max_boost_subs_factor, argi)) {
1055 config->cfg.kf_frame_max_boost_subs_factor = arg_parse_rational(&arg);
1056 }
else if (arg_match(&arg, &kf_max_total_boost_factor, argi)) {
1057 config->cfg.kf_max_total_boost_factor = arg_parse_rational(&arg);
1058 }
else if (arg_match(&arg, &gf_max_total_boost_factor, argi)) {
1059 config->cfg.gf_max_total_boost_factor = arg_parse_rational(&arg);
1060 }
else if (arg_match(&arg, &gf_frame_max_boost_factor, argi)) {
1061 config->cfg.gf_frame_max_boost_factor = arg_parse_rational(&arg);
1062 }
else if (arg_match(&arg, &zm_factor, argi)) {
1063 config->cfg.zm_factor = arg_parse_rational(&arg);
1064 }
else if (arg_match(&arg, &rd_mult_inter_qp_fac, argi)) {
1065 config->cfg.rd_mult_inter_qp_fac = arg_parse_rational(&arg);
1066 }
else if (arg_match(&arg, &rd_mult_arf_qp_fac, argi)) {
1067 config->cfg.rd_mult_arf_qp_fac = arg_parse_rational(&arg);
1068 }
else if (arg_match(&arg, &rd_mult_key_qp_fac, argi)) {
1069 config->cfg.rd_mult_key_qp_fac = arg_parse_rational(&arg);
1071 #if CONFIG_VP9_HIGHBITDEPTH
1072 }
else if (arg_match(&arg, &test16bitinternalarg, argi)) {
1073 if (strcmp(global->codec->name,
"vp9") == 0) {
1074 test_16bit_internal = 1;
1079 for (i = 0; ctrl_args[i]; i++) {
1080 if (arg_match(&arg, ctrl_args[i], argi)) {
1087 for (j = 0; j < config->arg_ctrl_cnt; j++)
1088 if (ctrl_args_map != NULL &&
1089 config->arg_ctrls[j][0] == ctrl_args_map[i])
1093 assert(j < (
int)ARG_CTRL_CNT_MAX);
1094 if (ctrl_args_map != NULL && j < (
int)ARG_CTRL_CNT_MAX) {
1095 config->arg_ctrls[j][0] = ctrl_args_map[i];
1096 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
1097 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1104 #if CONFIG_VP9_HIGHBITDEPTH
1105 if (strcmp(global->codec->name,
"vp9") == 0) {
1106 config->use_16bit_internal =
1107 test_16bit_internal | (config->cfg.g_profile > 1);
1110 return eos_mark_found;
1113 #define FOREACH_STREAM(func) \
1115 struct stream_state *stream; \
1116 for (stream = streams; stream; stream = stream->next) { \
1121 static void validate_stream_config(
const struct stream_state *stream,
1122 const struct VpxEncoderConfig *global) {
1123 const struct stream_state *streami;
1126 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1128 "Stream %d: Specify stream dimensions with --width (-w) "
1129 " and --height (-h)",
1133 if (stream->config.cfg.g_input_bit_depth >
1134 (
unsigned int)stream->config.cfg.g_bit_depth) {
1135 fatal(
"Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1136 stream->index, (
int)stream->config.cfg.g_bit_depth,
1137 stream->config.cfg.g_input_bit_depth);
1140 for (streami = stream; streami; streami = streami->next) {
1142 if (!streami->config.out_fn)
1143 fatal(
"Stream %d: Output file is required (specify with -o)",
1147 if (streami != stream) {
1148 const char *a = stream->config.out_fn;
1149 const char *b = streami->config.out_fn;
1150 if (!strcmp(a, b) && strcmp(a,
"/dev/null") && strcmp(a,
":nul"))
1151 fatal(
"Stream %d: duplicate output file (from stream %d)",
1152 streami->index, stream->index);
1156 if (streami != stream) {
1157 const char *a = stream->config.stats_fn;
1158 const char *b = streami->config.stats_fn;
1159 if (a && b && !strcmp(a, b))
1160 fatal(
"Stream %d: duplicate stats file (from stream %d)",
1161 streami->index, stream->index);
1166 static void set_stream_dimensions(
struct stream_state *stream,
unsigned int w,
1168 if (!stream->config.cfg.g_w) {
1169 if (!stream->config.cfg.g_h)
1170 stream->config.cfg.g_w = w;
1172 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1174 if (!stream->config.cfg.g_h) {
1175 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1179 static const char *file_type_to_string(
enum VideoFileType t) {
1181 case FILE_TYPE_RAW:
return "RAW";
1182 case FILE_TYPE_Y4M:
return "Y4M";
1183 default:
return "Other";
1187 static const char *image_format_to_string(
vpx_img_fmt_t f) {
1198 default:
return "Other";
1202 static void show_stream_config(
struct stream_state *stream,
1203 struct VpxEncoderConfig *global,
1204 struct VpxInputContext *input) {
1205 #define SHOW(field) \
1206 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1208 if (stream->index == 0) {
1209 fprintf(stderr,
"Codec: %s\n",
1211 fprintf(stderr,
"Source file: %s File Type: %s Format: %s\n",
1212 input->filename, file_type_to_string(input->file_type),
1213 image_format_to_string(input->fmt));
1215 if (stream->next || stream->index)
1216 fprintf(stderr,
"\nStream Index: %d\n", stream->index);
1217 fprintf(stderr,
"Destination file: %s\n", stream->config.out_fn);
1218 fprintf(stderr,
"Encoder parameters:\n");
1226 SHOW(g_input_bit_depth);
1227 SHOW(g_timebase.num);
1228 SHOW(g_timebase.den);
1229 SHOW(g_error_resilient);
1231 SHOW(g_lag_in_frames);
1232 SHOW(rc_dropframe_thresh);
1233 SHOW(rc_resize_allowed);
1234 SHOW(rc_scaled_width);
1235 SHOW(rc_scaled_height);
1236 SHOW(rc_resize_up_thresh);
1237 SHOW(rc_resize_down_thresh);
1239 SHOW(rc_target_bitrate);
1240 SHOW(rc_min_quantizer);
1241 SHOW(rc_max_quantizer);
1242 SHOW(rc_undershoot_pct);
1243 SHOW(rc_overshoot_pct);
1245 SHOW(rc_buf_initial_sz);
1246 SHOW(rc_buf_optimal_sz);
1247 SHOW(rc_2pass_vbr_bias_pct);
1248 SHOW(rc_2pass_vbr_minsection_pct);
1249 SHOW(rc_2pass_vbr_maxsection_pct);
1250 SHOW(rc_2pass_vbr_corpus_complexity);
1255 SHOW(use_vizier_rc_params);
1256 SHOW(active_wq_factor.num);
1257 SHOW(active_wq_factor.den);
1260 static void open_output_file(
struct stream_state *stream,
1261 struct VpxEncoderConfig *global,
1262 const struct VpxRational *pixel_aspect_ratio) {
1263 const char *fn = stream->config.out_fn;
1268 stream->file = strcmp(fn,
"-") ? fopen(fn,
"wb") : set_binary_mode(stdout);
1270 if (!stream->file) fatal(
"Failed to open output file");
1272 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1273 fatal(
"WebM output to pipes not supported.");
1276 if (stream->config.write_webm) {
1277 stream->webm_ctx.stream = stream->file;
1278 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1279 global->codec->fourcc, pixel_aspect_ratio);
1282 (void)pixel_aspect_ratio;
1285 if (!stream->config.write_webm) {
1286 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1290 static void close_output_file(
struct stream_state *stream,
1291 unsigned int fourcc) {
1297 if (stream->config.write_webm) {
1298 write_webm_file_footer(&stream->webm_ctx);
1302 if (!stream->config.write_webm) {
1303 if (!fseek(stream->file, 0, SEEK_SET))
1304 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1305 stream->frames_out);
1308 fclose(stream->file);
1311 static void setup_pass(
struct stream_state *stream,
1312 struct VpxEncoderConfig *global,
int pass) {
1313 if (stream->config.stats_fn) {
1314 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1315 fatal(
"Failed to open statistics store");
1317 if (!stats_open_mem(&stream->stats, pass))
1318 fatal(
"Failed to open statistics store");
1321 stream->config.cfg.g_pass = global->passes == 2
1325 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1328 stream->cx_time = 0;
1330 stream->frames_out = 0;
1333 static void initialize_encoder(
struct stream_state *stream,
1334 struct VpxEncoderConfig *global) {
1340 #if CONFIG_VP9_HIGHBITDEPTH
1346 &stream->config.cfg, flags);
1347 ctx_exit_on_error(&stream->encoder,
"Failed to initialize encoder");
1353 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1354 int ctrl = stream->config.arg_ctrls[i][0];
1355 int value = stream->config.arg_ctrls[i][1];
1357 fprintf(stderr,
"Error: Tried to set control %d = %d\n", ctrl, value);
1359 ctx_exit_on_error(&stream->encoder,
"Failed to control codec");
1363 if (global->test_decode != TEST_DECODE_OFF) {
1364 const VpxInterface *decoder = get_vpx_decoder_by_name(global->codec->name);
1370 static void encode_frame(
struct stream_state *stream,
1371 struct VpxEncoderConfig *global,
struct vpx_image *img,
1372 unsigned int frames_in) {
1375 struct vpx_usec_timer timer;
1378 (cfg->
g_timebase.
den * (int64_t)(frames_in - 1) * global->framerate.den) /
1381 (cfg->
g_timebase.
den * (int64_t)(frames_in)*global->framerate.den) /
1385 #if CONFIG_VP9_HIGHBITDEPTH
1390 fprintf(stderr,
"%s can only scale 4:2:0 inputs\n", exec_name);
1407 stream->img->stride[
VPX_PLANE_V] / 2, stream->img->d_w,
1408 stream->img->d_h, kFilterBox);
1411 stream->encoder.err = 1;
1412 ctx_exit_on_error(&stream->encoder,
1413 "Stream %d: Failed to encode frame.\n"
1414 "Scaling disabled in this configuration. \n"
1415 "To enable, configure with --enable-libyuv\n",
1421 if (img && (img->
d_w != cfg->
g_w || img->
d_h != cfg->
g_h)) {
1423 fprintf(stderr,
"%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1437 stream->img->d_w, stream->img->d_h, kFilterBox);
1440 stream->encoder.err = 1;
1441 ctx_exit_on_error(&stream->encoder,
1442 "Stream %d: Failed to encode frame.\n"
1443 "Scaling disabled in this configuration. \n"
1444 "To enable, configure with --enable-libyuv\n",
1449 vpx_usec_timer_start(&timer);
1451 (
unsigned long)(next_frame_start - frame_start), 0,
1453 vpx_usec_timer_mark(&timer);
1454 stream->cx_time += vpx_usec_timer_elapsed(&timer);
1455 ctx_exit_on_error(&stream->encoder,
"Stream %d: Failed to encode frame",
1459 static void update_quantizer_histogram(
struct stream_state *stream) {
1464 ctx_exit_on_error(&stream->encoder,
"Failed to read quantizer");
1465 stream->counts[q]++;
1469 static void get_cx_data(
struct stream_state *stream,
1470 struct VpxEncoderConfig *global,
int *got_data) {
1477 static size_t fsize = 0;
1478 static FileOffset ivf_header_pos = 0;
1480 switch (pkt->
kind) {
1483 stream->frames_out++;
1486 fprintf(stderr,
" %6luF", (
unsigned long)pkt->
data.
frame.sz);
1488 update_rate_histogram(stream->rate_hist, cfg, pkt);
1490 if (stream->config.write_webm) {
1491 write_webm_block(&stream->webm_ctx, cfg, pkt);
1494 if (!stream->config.write_webm) {
1495 if (pkt->
data.
frame.partition_id <= 0) {
1496 ivf_header_pos = ftello(stream->file);
1499 ivf_write_frame_header(stream->file, pkt->
data.
frame.pts, fsize);
1504 const FileOffset currpos = ftello(stream->file);
1505 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1506 ivf_write_frame_size(stream->file, fsize);
1507 fseeko(stream->file, currpos, SEEK_SET);
1518 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1520 (
unsigned int)pkt->
data.
frame.sz, NULL, 0);
1521 if (stream->decoder.err) {
1522 warn_or_exit_on_error(&stream->decoder,
1523 global->test_decode == TEST_DECODE_FATAL,
1524 "Failed to decode frame %d in stream %d",
1525 stream->frames_out + 1, stream->index);
1526 stream->mismatch_seen = stream->frames_out + 1;
1532 stream->frames_out++;
1539 if (global->show_psnr) {
1542 stream->psnr_sse_total += pkt->
data.
psnr.sse[0];
1543 stream->psnr_samples_total += pkt->
data.
psnr.samples[0];
1544 for (i = 0; i < 4; i++) {
1546 fprintf(stderr,
"%.3f ", pkt->
data.
psnr.psnr[i]);
1547 stream->psnr_totals[i] += pkt->
data.
psnr.psnr[i];
1549 stream->psnr_count++;
1558 static void show_psnr(
struct stream_state *stream,
double peak) {
1562 if (!stream->psnr_count)
return;
1564 fprintf(stderr,
"Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1565 ovpsnr = sse_to_psnr((
double)stream->psnr_samples_total, peak,
1566 (
double)stream->psnr_sse_total);
1567 fprintf(stderr,
" %.3f", ovpsnr);
1569 for (i = 0; i < 4; i++) {
1570 fprintf(stderr,
" %.3f", stream->psnr_totals[i] / stream->psnr_count);
1572 fprintf(stderr,
"\n");
1575 static float usec_to_fps(uint64_t usec,
unsigned int frames) {
1576 return (
float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1579 static void test_decode(
struct stream_state *stream,
1580 enum TestDecodeFatality fatal,
1581 const VpxInterface *codec) {
1584 if (stream->mismatch_seen)
return;
1587 if (strcmp(codec->name,
"vp8") == 0) {
1591 width = (stream->config.cfg.g_w + 15) & ~15;
1592 height = (stream->config.cfg.g_h + 15) & ~15;
1594 enc_img = ref_enc.img;
1596 dec_img = ref_dec.img;
1598 ref_enc.frame_type = VP8_LAST_FRAME;
1599 ref_dec.frame_type = VP8_LAST_FRAME;
1608 enc_img = ref_enc.img;
1610 dec_img = ref_dec.img;
1611 #if CONFIG_VP9_HIGHBITDEPTH
1616 enc_img.
d_w, enc_img.
d_h, 16);
1617 vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
1621 dec_img.
d_w, dec_img.
d_h, 16);
1622 vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
1627 ctx_exit_on_error(&stream->encoder,
"Failed to get encoder reference frame");
1628 ctx_exit_on_error(&stream->decoder,
"Failed to get decoder reference frame");
1630 if (!compare_img(&enc_img, &dec_img)) {
1631 int y[4], u[4], v[4];
1632 #if CONFIG_VP9_HIGHBITDEPTH
1634 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1636 find_mismatch(&enc_img, &dec_img, y, u, v);
1639 find_mismatch(&enc_img, &dec_img, y, u, v);
1641 stream->decoder.err = 1;
1642 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1643 "Stream %d: Encode/decode mismatch on frame %d at"
1644 " Y[%d, %d] {%d/%d},"
1645 " U[%d, %d] {%d/%d},"
1646 " V[%d, %d] {%d/%d}",
1647 stream->index, stream->frames_out, y[0], y[1], y[2],
1648 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1649 stream->mismatch_seen = stream->frames_out;
1656 static void print_time(
const char *label, int64_t etl) {
1663 etl -= hours * 3600;
1668 fprintf(stderr,
"[%3s %2" PRId64
":%02" PRId64
":%02" PRId64
"] ", label,
1671 fprintf(stderr,
"[%3s unknown] ", label);
1675 int main(
int argc,
const char **argv_) {
1678 #if CONFIG_VP9_HIGHBITDEPTH
1680 int allocated_raw_shift = 0;
1681 int use_16bit_internal = 0;
1682 int input_shift = 0;
1684 int frame_avail, got_data;
1686 struct VpxInputContext input;
1687 struct VpxEncoderConfig global;
1688 struct stream_state *streams = NULL;
1689 char **argv, **argi;
1690 uint64_t cx_time = 0;
1694 memset(&input, 0,
sizeof(input));
1695 memset(&raw, 0,
sizeof(raw));
1696 exec_name = argv_[0];
1699 input.framerate.numerator = 30;
1700 input.framerate.denominator = 1;
1701 input.only_i420 = 1;
1702 input.bit_depth = 0;
1708 argv = argv_dup(argc - 1, argv_ + 1);
1710 fprintf(stderr,
"Error allocating argument list\n");
1711 return EXIT_FAILURE;
1713 parse_global_config(&global, argv);
1715 if (argc < 3) usage_exit();
1717 switch (global.color_type) {
1731 struct stream_state *stream = NULL;
1734 stream = new_stream(&global, stream);
1736 if (!streams) streams = stream;
1737 }
while (parse_stream_params(&global, stream, argv));
1741 for (argi = argv; *argi; argi++)
1742 if (argi[0][0] ==
'-' && argi[0][1])
1743 die(
"Error: Unrecognized option %s\n", *argi);
1745 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1746 &stream->config.cfg););
1749 input.filename = argv[0];
1751 if (!input.filename) {
1752 fprintf(stderr,
"No input file specified!\n");
1757 if (global.codec->fourcc == VP9_FOURCC) input.only_i420 = 0;
1759 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1760 int frames_in = 0, seen_frames = 0;
1761 int64_t estimated_time_left = -1;
1762 int64_t average_rate = -1;
1763 int64_t lagged_count = 0;
1765 open_input_file(&input);
1770 if (!input.width || !input.height) {
1772 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1773 input.width = stream->config.cfg.g_w;
1774 input.height = stream->config.cfg.g_h;
1781 if (!input.width || !input.height)
1783 "Specify stream dimensions with --width (-w) "
1784 " and --height (-h)");
1791 if (!input.bit_depth) {
1793 if (stream->config.cfg.g_input_bit_depth)
1794 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1796 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1797 (
int)stream->config.cfg.g_bit_depth;
1802 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
1805 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
1806 FOREACH_STREAM(validate_stream_config(stream, &global));
1811 if (global.pass && global.passes == 2)
1813 if (!stream->config.stats_fn)
1814 die(
"Stream %d: Must specify --fpf when --pass=%d"
1815 " and --passes=2\n",
1816 stream->index, global.pass);
1821 if (stream->config.write_webm) {
1822 stream->config.write_webm = 0;
1824 "vpxenc was compiled without WebM container support."
1825 "Producing IVF output");
1833 if (!global.have_framerate) {
1834 global.framerate.num = input.framerate.numerator;
1835 global.framerate.den = input.framerate.denominator;
1836 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
1837 stream->config.cfg.g_timebase.num = global.framerate.den);
1841 if (global.verbose && pass == 0)
1842 FOREACH_STREAM(show_stream_config(stream, &global, &input));
1844 if (pass == (global.pass ? global.pass - 1 : 0)) {
1846 if (input.file_type != FILE_TYPE_Y4M) {
1847 vpx_img_alloc(&raw, input.fmt, input.width, input.height, 32);
1849 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
1850 &stream->config.cfg, &global.framerate));
1853 FOREACH_STREAM(setup_pass(stream, &global, pass));
1855 open_output_file(stream, &global, &input.pixel_aspect_ratio));
1856 FOREACH_STREAM(initialize_encoder(stream, &global));
1858 #if CONFIG_VP9_HIGHBITDEPTH
1859 if (strcmp(global.codec->name,
"vp9") == 0) {
1864 if (stream->config.use_16bit_internal) {
1865 use_16bit_internal = 1;
1867 if (stream->config.cfg.g_profile == 0) {
1870 input_shift = (int)stream->config.cfg.g_bit_depth -
1871 stream->config.cfg.g_input_bit_depth;
1880 while (frame_avail || got_data) {
1881 struct vpx_usec_timer timer;
1883 if (!global.limit || frames_in < global.limit) {
1884 frame_avail = read_frame(&input, &raw);
1886 if (frame_avail) frames_in++;
1888 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
1890 if (!global.quiet) {
1891 float fps = usec_to_fps(cx_time, seen_frames);
1892 fprintf(stderr,
"\rPass %d/%d ", pass + 1, global.passes);
1894 if (stream_cnt == 1)
1895 fprintf(stderr,
"frame %4d/%-4d %7" PRId64
"B ", frames_in,
1896 streams->frames_out, (int64_t)streams->nbytes);
1898 fprintf(stderr,
"frame %4d ", frames_in);
1900 fprintf(stderr,
"%7" PRId64
" %s %.2f %s ",
1901 cx_time > 9999999 ? cx_time / 1000 : cx_time,
1902 cx_time > 9999999 ?
"ms" :
"us", fps >= 1.0 ? fps : fps * 60,
1903 fps >= 1.0 ?
"fps" :
"fpm");
1904 print_time(
"ETA", estimated_time_left);
1910 if (frames_in > global.skip_frames) {
1911 #if CONFIG_VP9_HIGHBITDEPTH
1913 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
1914 assert(use_16bit_internal);
1917 if (!allocated_raw_shift) {
1919 input.width, input.height, 32);
1920 allocated_raw_shift = 1;
1922 vpx_img_upshift(&raw_shift, &raw, input_shift);
1923 frame_to_encode = &raw_shift;
1925 frame_to_encode = &raw;
1927 vpx_usec_timer_start(&timer);
1928 if (use_16bit_internal) {
1931 if (stream->config.use_16bit_internal)
1932 encode_frame(stream, &global,
1933 frame_avail ? frame_to_encode : NULL, frames_in);
1939 FOREACH_STREAM(encode_frame(stream, &global,
1940 frame_avail ? frame_to_encode : NULL,
1944 vpx_usec_timer_start(&timer);
1945 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
1948 vpx_usec_timer_mark(&timer);
1949 cx_time += vpx_usec_timer_elapsed(&timer);
1951 FOREACH_STREAM(update_quantizer_histogram(stream));
1954 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
1956 if (!got_data && input.length && streams != NULL &&
1957 !streams->frames_out) {
1958 lagged_count = global.limit ? seen_frames : ftello(input.file);
1959 }
else if (input.length) {
1964 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
1966 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
1967 remaining = 1000 * (global.limit - global.skip_frames -
1968 seen_frames + lagged_count);
1970 const int64_t input_pos = ftello(input.file);
1971 const int64_t input_pos_lagged = input_pos - lagged_count;
1972 const int64_t limit = input.length;
1974 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
1975 remaining = limit - input_pos + lagged_count;
1979 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
1980 estimated_time_left = average_rate ? remaining / average_rate : -1;
1983 if (got_data && global.test_decode != TEST_DECODE_OFF)
1984 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
1988 if (!global.quiet) fprintf(stderr,
"\033[K");
1991 if (stream_cnt > 1) fprintf(stderr,
"\n");
1993 if (!global.quiet) {
1994 FOREACH_STREAM(fprintf(
1996 "\rPass %d/%d frame %4d/%-4d %7" PRId64
"B %7" PRId64
"b/f %7" PRId64
1997 "b/s %7" PRId64
" %s (%.2f fps)\033[K\n",
1998 pass + 1, global.passes, frames_in, stream->frames_out,
1999 (int64_t)stream->nbytes,
2000 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2002 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2003 global.framerate.den / seen_frames
2005 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2006 stream->cx_time > 9999999 ?
"ms" :
"us",
2007 usec_to_fps(stream->cx_time, seen_frames)));
2010 if (global.show_psnr) {
2011 if (global.codec->fourcc == VP9_FOURCC) {
2013 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2015 FOREACH_STREAM(show_psnr(stream, 255.0));
2021 if (global.test_decode != TEST_DECODE_OFF) {
2025 close_input_file(&input);
2027 if (global.test_decode == TEST_DECODE_FATAL) {
2028 FOREACH_STREAM(res |= stream->mismatch_seen);
2030 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
2032 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
2034 if (global.pass)
break;
2037 if (global.show_q_hist_buckets)
2039 show_q_histogram(stream->counts, global.show_q_hist_buckets));
2041 if (global.show_rate_hist_buckets)
2042 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2043 global.show_rate_hist_buckets));
2044 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
2046 #if CONFIG_INTERNAL_STATS
2050 if (!(global.pass == 1 && global.passes == 2))
2052 FILE *f = fopen(
"opsnr.stt",
"a");
2053 if (stream->mismatch_seen) {
2054 fprintf(f,
"First mismatch occurred in frame %d\n",
2055 stream->mismatch_seen);
2057 fprintf(f,
"No mismatch detected in recon buffers\n");
2063 #if CONFIG_VP9_HIGHBITDEPTH
2069 return res ? EXIT_FAILURE : EXIT_SUCCESS;