2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "./vpx_config.h"
23 #include "third_party/libyuv/include/libyuv/scale.h"
26 #include "vpx/vpx_encoder.h"
28 #include "vpx/vpx_decoder.h"
33 #include "./tools_common.h"
35 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
36 #include "vpx/vp8cx.h"
38 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
39 #include "vpx/vp8dx.h"
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(
62 vpx_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
64 const char *detail = vpx_codec_error_detail(ctx);
66 vfprintf(stderr, s, ap);
67 fprintf(stderr, ": %s\n", vpx_codec_error(ctx));
69 if (detail) fprintf(stderr, " %s\n", detail);
71 if (fatal) exit(EXIT_FAILURE);
75 static VPX_TOOLS_FORMAT_PRINTF(2,
76 3) void ctx_exit_on_error(vpx_codec_ctx_t *ctx,
81 warn_or_exit_on_errorv(ctx, 1, s, ap);
85 static VPX_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
86 vpx_codec_ctx_t *ctx, int fatal, const char *s, ...) {
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,
416 static const int vp8_arg_ctrl_map[] = { VP8E_SET_CPUUSED,
417 VP8E_SET_ENABLEAUTOALTREF,
418 VP8E_SET_NOISE_SENSITIVITY,
420 VP8E_SET_STATIC_THRESHOLD,
421 VP8E_SET_TOKEN_PARTITIONS,
422 VP8E_SET_ARNR_MAXFRAMES,
423 VP8E_SET_ARNR_STRENGTH,
427 VP8E_SET_MAX_INTRA_BITRATE_PCT,
428 VP8E_SET_GF_CBR_BOOST_PCT,
429 VP8E_SET_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[] = {
472 { "unknown", VPX_CS_UNKNOWN },
473 { "bt601", VPX_CS_BT_601 },
474 { "bt709", VPX_CS_BT_709 },
475 { "smpte170", VPX_CS_SMPTE_170 },
476 { "smpte240", VPX_CS_SMPTE_240 },
477 { "bt2020", VPX_CS_BT_2020 },
478 { "reserved", VPX_CS_RESERVED },
479 { "sRGB", VPX_CS_SRGB },
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[] = {
489 { "8", VPX_BITS_8 }, { "10", VPX_BITS_10 }, { "12", VPX_BITS_12 }, { NULL, 0 }
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"
528 "0: Loopfilter on for all frames (default)\n"
529 "1: Loopfilter off for non reference frames\n"
530 "2: Loopfilter off for all frames");
533 #if CONFIG_VP9_ENCODER
534 static const arg_def_t *vp9_args[] = { &cpu_used_vp9,
550 &frame_parallel_decoding,
553 &frame_periodic_boost,
562 // NOTE: The entries above have a corresponding entry in vp9_arg_ctrl_map. The
563 // entries below do not have a corresponding entry in vp9_arg_ctrl_map. They
564 // must be listed at the end of vp9_args.
565 #if CONFIG_VP9_HIGHBITDEPTH
568 #endif // CONFIG_VP9_HIGHBITDEPTH
570 static const int vp9_arg_ctrl_map[] = { VP8E_SET_CPUUSED,
571 VP8E_SET_ENABLEAUTOALTREF,
573 VP8E_SET_STATIC_THRESHOLD,
574 VP9E_SET_TILE_COLUMNS,
577 VP8E_SET_ARNR_MAXFRAMES,
578 VP8E_SET_ARNR_STRENGTH,
582 VP8E_SET_MAX_INTRA_BITRATE_PCT,
583 VP9E_SET_MAX_INTER_BITRATE_PCT,
584 VP9E_SET_GF_CBR_BOOST_PCT,
586 VP9E_SET_FRAME_PARALLEL_DECODING,
589 VP9E_SET_FRAME_PERIODIC_BOOST,
590 VP9E_SET_NOISE_SENSITIVITY,
591 VP9E_SET_TUNE_CONTENT,
592 VP9E_SET_COLOR_SPACE,
593 VP9E_SET_MIN_GF_INTERVAL,
594 VP9E_SET_MAX_GF_INTERVAL,
595 VP9E_SET_TARGET_LEVEL,
597 VP9E_SET_DISABLE_LOOPFILTER,
601 static const arg_def_t *no_args[] = { NULL };
603 static void show_help(FILE *fout, int shorthelp) {
605 const int num_encoder = get_vpx_encoder_count();
607 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
611 fprintf(fout, "Use --help to see the full list of options.\n");
615 fprintf(fout, "\nOptions:\n");
616 arg_show_usage(fout, main_args);
617 fprintf(fout, "\nEncoder Global Options:\n");
618 arg_show_usage(fout, global_args);
619 fprintf(fout, "\nRate Control Options:\n");
620 arg_show_usage(fout, rc_args);
621 fprintf(fout, "\nTwopass Rate Control Options:\n");
622 arg_show_usage(fout, rc_twopass_args);
623 fprintf(fout, "\nKeyframe Placement Options:\n");
624 arg_show_usage(fout, kf_args);
625 #if CONFIG_VP8_ENCODER
626 fprintf(fout, "\nVP8 Specific Options:\n");
627 arg_show_usage(fout, vp8_args);
629 #if CONFIG_VP9_ENCODER
630 fprintf(fout, "\nVP9 Specific Options:\n");
631 arg_show_usage(fout, vp9_args);
632 fprintf(fout, "\nVizier Rate Control Options:\n");
633 arg_show_usage(fout, vizier_rc_args);
636 "\nStream timebase (--timebase):\n"
637 " The desired precision of timestamps in the output, expressed\n"
638 " in fractional seconds. Default is 1/1000.\n");
639 fprintf(fout, "\nIncluded encoders:\n\n");
641 for (i = 0; i < num_encoder; ++i) {
642 const VpxInterface *const encoder = get_vpx_encoder_by_index(i);
643 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
644 fprintf(fout, " %-6s - %s %s\n", encoder->name,
645 vpx_codec_iface_name(encoder->codec_interface()), defstr);
647 fprintf(fout, "\n ");
648 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
651 void usage_exit(void) {
652 show_help(stderr, 1);
656 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
657 #if CONFIG_VP9_ENCODER
658 #define ARG_CTRL_CNT_MAX NELEMENTS(vp9_arg_ctrl_map)
660 #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
664 typedef int stereo_format_t;
665 struct WebmOutputContext {
670 /* Per-stream configuration */
671 struct stream_config {
672 struct vpx_codec_enc_cfg cfg;
674 const char *stats_fn;
675 stereo_format_t stereo_fmt;
676 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
679 #if CONFIG_VP9_HIGHBITDEPTH
680 // whether to use 16bit internal buffers
681 int use_16bit_internal;
685 struct stream_state {
687 struct stream_state *next;
688 struct stream_config config;
690 struct rate_hist *rate_hist;
691 struct WebmOutputContext webm_ctx;
692 uint64_t psnr_sse_total;
693 uint64_t psnr_samples_total;
694 double psnr_totals[4];
697 vpx_codec_ctx_t encoder;
698 unsigned int frames_out;
702 struct vpx_image *img;
703 vpx_codec_ctx_t decoder;
707 static void validate_positive_rational(const char *msg,
708 struct vpx_rational *rat) {
714 if (rat->num < 0) die("Error: %s must be positive\n", msg);
716 if (!rat->den) die("Error: %s has zero denominator\n", msg);
719 static void parse_global_config(struct VpxEncoderConfig *global, char **argv) {
722 const int num_encoder = get_vpx_encoder_count();
724 if (num_encoder < 1) die("Error: no valid encoder available\n");
726 /* Initialize default parameters */
727 memset(global, 0, sizeof(*global));
728 global->codec = get_vpx_encoder_by_index(num_encoder - 1);
730 global->color_type = I420;
731 /* Assign default deadline to good quality */
732 global->deadline = VPX_DL_GOOD_QUALITY;
734 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
737 if (arg_match(&arg, &help, argi)) {
738 show_help(stdout, 0);
740 } else if (arg_match(&arg, &codecarg, argi)) {
741 global->codec = get_vpx_encoder_by_name(arg.val);
743 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
744 } else if (arg_match(&arg, &passes, argi)) {
745 global->passes = arg_parse_uint(&arg);
747 if (global->passes < 1 || global->passes > 2)
748 die("Error: Invalid number of passes (%d)\n", global->passes);
749 } else if (arg_match(&arg, &pass_arg, argi)) {
750 global->pass = arg_parse_uint(&arg);
752 if (global->pass < 1 || global->pass > 2)
753 die("Error: Invalid pass selected (%d)\n", global->pass);
754 } else if (arg_match(&arg, &usage, argi))
755 global->usage = arg_parse_uint(&arg);
756 else if (arg_match(&arg, &deadline, argi))
757 global->deadline = arg_parse_uint(&arg);
758 else if (arg_match(&arg, &best_dl, argi))
759 global->deadline = VPX_DL_BEST_QUALITY;
760 else if (arg_match(&arg, &good_dl, argi))
761 global->deadline = VPX_DL_GOOD_QUALITY;
762 else if (arg_match(&arg, &rt_dl, argi))
763 global->deadline = VPX_DL_REALTIME;
764 else if (arg_match(&arg, &use_yv12, argi))
765 global->color_type = YV12;
766 else if (arg_match(&arg, &use_nv12, argi))
767 global->color_type = NV12;
768 else if (arg_match(&arg, &use_i420, argi))
769 global->color_type = I420;
770 else if (arg_match(&arg, &use_i422, argi))
771 global->color_type = I422;
772 else if (arg_match(&arg, &use_i444, argi))
773 global->color_type = I444;
774 else if (arg_match(&arg, &use_i440, argi))
775 global->color_type = I440;
776 else if (arg_match(&arg, &quietarg, argi))
778 else if (arg_match(&arg, &verbosearg, argi))
780 else if (arg_match(&arg, &limit, argi))
781 global->limit = arg_parse_uint(&arg);
782 else if (arg_match(&arg, &skip, argi))
783 global->skip_frames = arg_parse_uint(&arg);
784 else if (arg_match(&arg, &psnrarg, argi))
785 global->show_psnr = 1;
786 else if (arg_match(&arg, &recontest, argi))
787 global->test_decode = arg_parse_enum_or_int(&arg);
788 else if (arg_match(&arg, &framerate, argi)) {
789 global->framerate = arg_parse_rational(&arg);
790 validate_positive_rational(arg.name, &global->framerate);
791 global->have_framerate = 1;
792 } else if (arg_match(&arg, &out_part, argi))
793 global->out_part = 1;
794 else if (arg_match(&arg, &debugmode, argi))
796 else if (arg_match(&arg, &q_hist_n, argi))
797 global->show_q_hist_buckets = arg_parse_uint(&arg);
798 else if (arg_match(&arg, &rate_hist_n, argi))
799 global->show_rate_hist_buckets = arg_parse_uint(&arg);
800 else if (arg_match(&arg, &disable_warnings, argi))
801 global->disable_warnings = 1;
802 else if (arg_match(&arg, &disable_warning_prompt, argi))
803 global->disable_warning_prompt = 1;
809 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
810 if (global->pass > global->passes) {
811 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
813 global->passes = global->pass;
816 /* Validate global config */
817 if (global->passes == 0) {
818 #if CONFIG_VP9_ENCODER
819 // Make default VP9 passes = 2 until there is a better quality 1-pass
821 if (global->codec != NULL && global->codec->name != NULL)
822 global->passes = (strcmp(global->codec->name, "vp9") == 0 &&
823 global->deadline != VPX_DL_REALTIME)
831 if (global->deadline == VPX_DL_REALTIME && global->passes > 1) {
832 warn("Enforcing one-pass encoding in realtime mode\n");
837 static struct stream_state *new_stream(struct VpxEncoderConfig *global,
838 struct stream_state *prev) {
839 struct stream_state *stream;
841 stream = calloc(1, sizeof(*stream));
842 if (stream == NULL) {
843 fatal("Failed to allocate new stream.");
847 memcpy(stream, prev, sizeof(*stream));
853 /* Populate encoder configuration */
854 res = vpx_codec_enc_config_default(global->codec->codec_interface(),
855 &stream->config.cfg, global->usage);
856 if (res) fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res));
858 /* Change the default timebase to a high enough value so that the
859 * encoder will always create strictly increasing timestamps.
861 stream->config.cfg.g_timebase.den = 1000;
863 /* Never use the library's default resolution, require it be parsed
864 * from the file or set on the command line.
866 stream->config.cfg.g_w = 0;
867 stream->config.cfg.g_h = 0;
869 /* Initialize remaining stream parameters */
870 stream->config.write_webm = 1;
872 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
873 stream->webm_ctx.last_pts_ns = -1;
874 stream->webm_ctx.writer = NULL;
875 stream->webm_ctx.segment = NULL;
878 /* Allows removal of the application version from the EBML tags */
879 stream->webm_ctx.debug = global->debug;
881 /* Default lag_in_frames is 0 in realtime mode CBR mode*/
882 if (global->deadline == VPX_DL_REALTIME &&
883 stream->config.cfg.rc_end_usage == 1)
884 stream->config.cfg.g_lag_in_frames = 0;
887 /* Output files must be specified for each stream */
888 stream->config.out_fn = NULL;
894 static int parse_stream_params(struct VpxEncoderConfig *global,
895 struct stream_state *stream, char **argv) {
898 static const arg_def_t **ctrl_args = no_args;
899 static const int *ctrl_args_map = NULL;
900 struct stream_config *config = &stream->config;
901 int eos_mark_found = 0;
902 #if CONFIG_VP9_HIGHBITDEPTH
903 int test_16bit_internal = 0;
906 // Handle codec specific options
908 #if CONFIG_VP8_ENCODER
909 } else if (strcmp(global->codec->name, "vp8") == 0) {
910 ctrl_args = vp8_args;
911 ctrl_args_map = vp8_arg_ctrl_map;
913 #if CONFIG_VP9_ENCODER
914 } else if (strcmp(global->codec->name, "vp9") == 0) {
915 ctrl_args = vp9_args;
916 ctrl_args_map = vp9_arg_ctrl_map;
920 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
923 /* Once we've found an end-of-stream marker (--) we want to continue
924 * shifting arguments but not consuming them.
926 if (eos_mark_found) {
929 } else if (!strcmp(*argj, "--")) {
934 if (arg_match(&arg, &outputfile, argi)) {
935 config->out_fn = arg.val;
936 } else if (arg_match(&arg, &fpf_name, argi)) {
937 config->stats_fn = arg.val;
938 } else if (arg_match(&arg, &use_webm, argi)) {
940 config->write_webm = 1;
942 die("Error: --webm specified but webm is disabled.");
944 } else if (arg_match(&arg, &use_ivf, argi)) {
945 config->write_webm = 0;
946 } else if (arg_match(&arg, &threads, argi)) {
947 config->cfg.g_threads = arg_parse_uint(&arg);
948 } else if (arg_match(&arg, &profile, argi)) {
949 config->cfg.g_profile = arg_parse_uint(&arg);
950 } else if (arg_match(&arg, &width, argi)) {
951 config->cfg.g_w = arg_parse_uint(&arg);
952 } else if (arg_match(&arg, &height, argi)) {
953 config->cfg.g_h = arg_parse_uint(&arg);
954 #if CONFIG_VP9_HIGHBITDEPTH
955 } else if (arg_match(&arg, &bitdeptharg, argi)) {
956 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
957 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
958 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
961 } else if (arg_match(&arg, &stereo_mode, argi)) {
962 config->stereo_fmt = arg_parse_enum_or_int(&arg);
964 } else if (arg_match(&arg, &timebase, argi)) {
965 config->cfg.g_timebase = arg_parse_rational(&arg);
966 validate_positive_rational(arg.name, &config->cfg.g_timebase);
967 } else if (arg_match(&arg, &error_resilient, argi)) {
968 config->cfg.g_error_resilient = arg_parse_uint(&arg);
969 } else if (arg_match(&arg, &end_usage, argi)) {
970 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
971 } else if (arg_match(&arg, &lag_in_frames, argi)) {
972 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
973 if (global->deadline == VPX_DL_REALTIME &&
974 config->cfg.rc_end_usage == VPX_CBR &&
975 config->cfg.g_lag_in_frames != 0) {
976 warn("non-zero %s option ignored in realtime CBR mode.\n", arg.name);
977 config->cfg.g_lag_in_frames = 0;
979 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
980 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
981 } else if (arg_match(&arg, &resize_allowed, argi)) {
982 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
983 } else if (arg_match(&arg, &resize_width, argi)) {
984 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
985 } else if (arg_match(&arg, &resize_height, argi)) {
986 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
987 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
988 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
989 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
990 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
991 } else if (arg_match(&arg, &end_usage, argi)) {
992 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
993 } else if (arg_match(&arg, &target_bitrate, argi)) {
994 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
995 } else if (arg_match(&arg, &min_quantizer, argi)) {
996 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
997 } else if (arg_match(&arg, &max_quantizer, argi)) {
998 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
999 } else if (arg_match(&arg, &undershoot_pct, argi)) {
1000 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1001 } else if (arg_match(&arg, &overshoot_pct, argi)) {
1002 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1003 } else if (arg_match(&arg, &buf_sz, argi)) {
1004 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1005 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
1006 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1007 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1008 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1009 } else if (arg_match(&arg, &bias_pct, argi)) {
1010 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1011 if (global->passes < 2)
1012 warn("option %s ignored in one-pass mode.\n", arg.name);
1013 } else if (arg_match(&arg, &minsection_pct, argi)) {
1014 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1016 if (global->passes < 2)
1017 warn("option %s ignored in one-pass mode.\n", arg.name);
1018 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1019 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1021 if (global->passes < 2)
1022 warn("option %s ignored in one-pass mode.\n", arg.name);
1023 } else if (arg_match(&arg, &corpus_complexity, argi)) {
1024 config->cfg.rc_2pass_vbr_corpus_complexity = arg_parse_uint(&arg);
1026 if (global->passes < 2)
1027 warn("option %s ignored in one-pass mode.\n", arg.name);
1028 } else if (arg_match(&arg, &kf_min_dist, argi)) {
1029 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1030 } else if (arg_match(&arg, &kf_max_dist, argi)) {
1031 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1032 } else if (arg_match(&arg, &kf_disabled, argi)) {
1033 config->cfg.kf_mode = VPX_KF_DISABLED;
1034 #if CONFIG_VP9_ENCODER
1035 } else if (arg_match(&arg, &use_vizier_rc_params, argi)) {
1036 config->cfg.use_vizier_rc_params = arg_parse_int(&arg);
1037 } else if (arg_match(&arg, &active_wq_factor, argi)) {
1038 config->cfg.active_wq_factor = arg_parse_rational(&arg);
1039 } else if (arg_match(&arg, &err_per_mb_factor, argi)) {
1040 config->cfg.err_per_mb_factor = arg_parse_rational(&arg);
1041 } else if (arg_match(&arg, &sr_default_decay_limit, argi)) {
1042 config->cfg.sr_default_decay_limit = arg_parse_rational(&arg);
1043 } else if (arg_match(&arg, &sr_diff_factor, argi)) {
1044 config->cfg.sr_diff_factor = arg_parse_rational(&arg);
1045 } else if (arg_match(&arg, &kf_err_per_mb_factor, argi)) {
1046 config->cfg.kf_err_per_mb_factor = arg_parse_rational(&arg);
1047 } else if (arg_match(&arg, &kf_frame_min_boost_factor, argi)) {
1048 config->cfg.kf_frame_min_boost_factor = arg_parse_rational(&arg);
1049 } else if (arg_match(&arg, &kf_frame_max_boost_first_factor, argi)) {
1050 config->cfg.kf_frame_max_boost_first_factor = arg_parse_rational(&arg);
1051 } else if (arg_match(&arg, &kf_frame_max_boost_subs_factor, argi)) {
1052 config->cfg.kf_frame_max_boost_subs_factor = arg_parse_rational(&arg);
1053 } else if (arg_match(&arg, &kf_max_total_boost_factor, argi)) {
1054 config->cfg.kf_max_total_boost_factor = arg_parse_rational(&arg);
1055 } else if (arg_match(&arg, &gf_max_total_boost_factor, argi)) {
1056 config->cfg.gf_max_total_boost_factor = arg_parse_rational(&arg);
1057 } else if (arg_match(&arg, &gf_frame_max_boost_factor, argi)) {
1058 config->cfg.gf_frame_max_boost_factor = arg_parse_rational(&arg);
1059 } else if (arg_match(&arg, &zm_factor, argi)) {
1060 config->cfg.zm_factor = arg_parse_rational(&arg);
1061 } else if (arg_match(&arg, &rd_mult_inter_qp_fac, argi)) {
1062 config->cfg.rd_mult_inter_qp_fac = arg_parse_rational(&arg);
1063 } else if (arg_match(&arg, &rd_mult_arf_qp_fac, argi)) {
1064 config->cfg.rd_mult_arf_qp_fac = arg_parse_rational(&arg);
1065 } else if (arg_match(&arg, &rd_mult_key_qp_fac, argi)) {
1066 config->cfg.rd_mult_key_qp_fac = arg_parse_rational(&arg);
1068 #if CONFIG_VP9_HIGHBITDEPTH
1069 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
1070 if (strcmp(global->codec->name, "vp9") == 0) {
1071 test_16bit_internal = 1;
1076 for (i = 0; ctrl_args[i]; i++) {
1077 if (arg_match(&arg, ctrl_args[i], argi)) {
1081 /* Point either to the next free element or the first
1082 * instance of this control.
1084 for (j = 0; j < config->arg_ctrl_cnt; j++)
1085 if (ctrl_args_map != NULL &&
1086 config->arg_ctrls[j][0] == ctrl_args_map[i])
1090 assert(j < (int)ARG_CTRL_CNT_MAX);
1091 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
1092 config->arg_ctrls[j][0] = ctrl_args_map[i];
1093 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
1094 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1101 #if CONFIG_VP9_HIGHBITDEPTH
1102 if (strcmp(global->codec->name, "vp9") == 0) {
1103 config->use_16bit_internal =
1104 test_16bit_internal | (config->cfg.g_profile > 1);
1107 return eos_mark_found;
1110 #define FOREACH_STREAM(func) \
1112 struct stream_state *stream; \
1113 for (stream = streams; stream; stream = stream->next) { \
1118 static void validate_stream_config(const struct stream_state *stream,
1119 const struct VpxEncoderConfig *global) {
1120 const struct stream_state *streami;
1123 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1125 "Stream %d: Specify stream dimensions with --width (-w) "
1126 " and --height (-h)",
1129 // Check that the codec bit depth is greater than the input bit depth.
1130 if (stream->config.cfg.g_input_bit_depth >
1131 (unsigned int)stream->config.cfg.g_bit_depth) {
1132 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1133 stream->index, (int)stream->config.cfg.g_bit_depth,
1134 stream->config.cfg.g_input_bit_depth);
1137 for (streami = stream; streami; streami = streami->next) {
1138 /* All streams require output files */
1139 if (!streami->config.out_fn)
1140 fatal("Stream %d: Output file is required (specify with -o)",
1143 /* Check for two streams outputting to the same file */
1144 if (streami != stream) {
1145 const char *a = stream->config.out_fn;
1146 const char *b = streami->config.out_fn;
1147 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1148 fatal("Stream %d: duplicate output file (from stream %d)",
1149 streami->index, stream->index);
1152 /* Check for two streams sharing a stats file. */
1153 if (streami != stream) {
1154 const char *a = stream->config.stats_fn;
1155 const char *b = streami->config.stats_fn;
1156 if (a && b && !strcmp(a, b))
1157 fatal("Stream %d: duplicate stats file (from stream %d)",
1158 streami->index, stream->index);
1163 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1165 if (!stream->config.cfg.g_w) {
1166 if (!stream->config.cfg.g_h)
1167 stream->config.cfg.g_w = w;
1169 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1171 if (!stream->config.cfg.g_h) {
1172 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1176 static const char *file_type_to_string(enum VideoFileType t) {
1178 case FILE_TYPE_RAW: return "RAW";
1179 case FILE_TYPE_Y4M: return "Y4M";
1180 default: return "Other";
1184 static const char *image_format_to_string(vpx_img_fmt_t f) {
1186 case VPX_IMG_FMT_I420: return "I420";
1187 case VPX_IMG_FMT_I422: return "I422";
1188 case VPX_IMG_FMT_I444: return "I444";
1189 case VPX_IMG_FMT_I440: return "I440";
1190 case VPX_IMG_FMT_YV12: return "YV12";
1191 case VPX_IMG_FMT_I42016: return "I42016";
1192 case VPX_IMG_FMT_I42216: return "I42216";
1193 case VPX_IMG_FMT_I44416: return "I44416";
1194 case VPX_IMG_FMT_I44016: return "I44016";
1195 default: return "Other";
1199 static void show_stream_config(struct stream_state *stream,
1200 struct VpxEncoderConfig *global,
1201 struct VpxInputContext *input) {
1202 #define SHOW(field) \
1203 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1205 if (stream->index == 0) {
1206 fprintf(stderr, "Codec: %s\n",
1207 vpx_codec_iface_name(global->codec->codec_interface()));
1208 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1209 input->filename, file_type_to_string(input->file_type),
1210 image_format_to_string(input->fmt));
1212 if (stream->next || stream->index)
1213 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1214 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1215 fprintf(stderr, "Encoder parameters:\n");
1223 SHOW(g_input_bit_depth);
1224 SHOW(g_timebase.num);
1225 SHOW(g_timebase.den);
1226 SHOW(g_error_resilient);
1228 SHOW(g_lag_in_frames);
1229 SHOW(rc_dropframe_thresh);
1230 SHOW(rc_resize_allowed);
1231 SHOW(rc_scaled_width);
1232 SHOW(rc_scaled_height);
1233 SHOW(rc_resize_up_thresh);
1234 SHOW(rc_resize_down_thresh);
1236 SHOW(rc_target_bitrate);
1237 SHOW(rc_min_quantizer);
1238 SHOW(rc_max_quantizer);
1239 SHOW(rc_undershoot_pct);
1240 SHOW(rc_overshoot_pct);
1242 SHOW(rc_buf_initial_sz);
1243 SHOW(rc_buf_optimal_sz);
1244 SHOW(rc_2pass_vbr_bias_pct);
1245 SHOW(rc_2pass_vbr_minsection_pct);
1246 SHOW(rc_2pass_vbr_maxsection_pct);
1247 SHOW(rc_2pass_vbr_corpus_complexity);
1251 // Temporary use for debug
1252 SHOW(use_vizier_rc_params);
1253 SHOW(active_wq_factor.num);
1254 SHOW(active_wq_factor.den);
1257 static void open_output_file(struct stream_state *stream,
1258 struct VpxEncoderConfig *global,
1259 const struct VpxRational *pixel_aspect_ratio) {
1260 const char *fn = stream->config.out_fn;
1261 const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg;
1263 if (cfg->g_pass == VPX_RC_FIRST_PASS) return;
1265 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1267 if (!stream->file) fatal("Failed to open output file");
1269 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1270 fatal("WebM output to pipes not supported.");
1273 if (stream->config.write_webm) {
1274 stream->webm_ctx.stream = stream->file;
1275 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1276 global->codec->fourcc, pixel_aspect_ratio);
1279 (void)pixel_aspect_ratio;
1282 if (!stream->config.write_webm) {
1283 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1287 static void close_output_file(struct stream_state *stream,
1288 unsigned int fourcc) {
1289 const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg;
1291 if (cfg->g_pass == VPX_RC_FIRST_PASS) return;
1294 if (stream->config.write_webm) {
1295 write_webm_file_footer(&stream->webm_ctx);
1299 if (!stream->config.write_webm) {
1300 if (!fseek(stream->file, 0, SEEK_SET))
1301 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1302 stream->frames_out);
1305 fclose(stream->file);
1308 static void setup_pass(struct stream_state *stream,
1309 struct VpxEncoderConfig *global, int pass) {
1310 if (stream->config.stats_fn) {
1311 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1312 fatal("Failed to open statistics store");
1314 if (!stats_open_mem(&stream->stats, pass))
1315 fatal("Failed to open statistics store");
1318 stream->config.cfg.g_pass = global->passes == 2
1319 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
1322 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1325 stream->cx_time = 0;
1327 stream->frames_out = 0;
1330 static void initialize_encoder(struct stream_state *stream,
1331 struct VpxEncoderConfig *global) {
1335 flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0;
1336 flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0;
1337 #if CONFIG_VP9_HIGHBITDEPTH
1338 flags |= stream->config.use_16bit_internal ? VPX_CODEC_USE_HIGHBITDEPTH : 0;
1341 /* Construct Encoder Context */
1342 vpx_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
1343 &stream->config.cfg, flags);
1344 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1346 /* Note that we bypass the vpx_codec_control wrapper macro because
1347 * we're being clever to store the control IDs in an array. Real
1348 * applications will want to make use of the enumerations directly
1350 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1351 int ctrl = stream->config.arg_ctrls[i][0];
1352 int value = stream->config.arg_ctrls[i][1];
1353 if (vpx_codec_control_(&stream->encoder, ctrl, value))
1354 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1356 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1360 if (global->test_decode != TEST_DECODE_OFF) {
1361 const VpxInterface *decoder = get_vpx_decoder_by_name(global->codec->name);
1362 vpx_codec_dec_init(&stream->decoder, decoder->codec_interface(), NULL, 0);
1367 static void encode_frame(struct stream_state *stream,
1368 struct VpxEncoderConfig *global, struct vpx_image *img,
1369 unsigned int frames_in) {
1370 vpx_codec_pts_t frame_start, next_frame_start;
1371 struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
1372 struct vpx_usec_timer timer;
1375 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1376 cfg->g_timebase.num / global->framerate.num;
1378 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1379 cfg->g_timebase.num / global->framerate.num;
1381 /* Scale if necessary */
1382 #if CONFIG_VP9_HIGHBITDEPTH
1384 if ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) &&
1385 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1386 if (img->fmt != VPX_IMG_FMT_I42016) {
1387 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1393 vpx_img_alloc(NULL, VPX_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1396 (uint16_t *)img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y] / 2,
1397 (uint16_t *)img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U] / 2,
1398 (uint16_t *)img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V] / 2,
1399 img->d_w, img->d_h, (uint16_t *)stream->img->planes[VPX_PLANE_Y],
1400 stream->img->stride[VPX_PLANE_Y] / 2,
1401 (uint16_t *)stream->img->planes[VPX_PLANE_U],
1402 stream->img->stride[VPX_PLANE_U] / 2,
1403 (uint16_t *)stream->img->planes[VPX_PLANE_V],
1404 stream->img->stride[VPX_PLANE_V] / 2, stream->img->d_w,
1405 stream->img->d_h, kFilterBox);
1408 stream->encoder.err = 1;
1409 ctx_exit_on_error(&stream->encoder,
1410 "Stream %d: Failed to encode frame.\n"
1411 "Scaling disabled in this configuration. \n"
1412 "To enable, configure with --enable-libyuv\n",
1418 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1419 if (img->fmt != VPX_IMG_FMT_I420 && img->fmt != VPX_IMG_FMT_YV12) {
1420 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1426 vpx_img_alloc(NULL, VPX_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1428 img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y],
1429 img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U],
1430 img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V], img->d_w, img->d_h,
1431 stream->img->planes[VPX_PLANE_Y], stream->img->stride[VPX_PLANE_Y],
1432 stream->img->planes[VPX_PLANE_U], stream->img->stride[VPX_PLANE_U],
1433 stream->img->planes[VPX_PLANE_V], stream->img->stride[VPX_PLANE_V],
1434 stream->img->d_w, stream->img->d_h, kFilterBox);
1437 stream->encoder.err = 1;
1438 ctx_exit_on_error(&stream->encoder,
1439 "Stream %d: Failed to encode frame.\n"
1440 "Scaling disabled in this configuration. \n"
1441 "To enable, configure with --enable-libyuv\n",
1446 vpx_usec_timer_start(&timer);
1447 vpx_codec_encode(&stream->encoder, img, frame_start,
1448 (unsigned long)(next_frame_start - frame_start), 0,
1450 vpx_usec_timer_mark(&timer);
1451 stream->cx_time += vpx_usec_timer_elapsed(&timer);
1452 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1456 static void update_quantizer_histogram(struct stream_state *stream) {
1457 if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) {
1460 vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
1461 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1462 stream->counts[q]++;
1466 static void get_cx_data(struct stream_state *stream,
1467 struct VpxEncoderConfig *global, int *got_data) {
1468 const vpx_codec_cx_pkt_t *pkt;
1469 const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
1470 vpx_codec_iter_t iter = NULL;
1473 while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter))) {
1474 static size_t fsize = 0;
1475 static FileOffset ivf_header_pos = 0;
1477 switch (pkt->kind) {
1478 case VPX_CODEC_CX_FRAME_PKT:
1479 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
1480 stream->frames_out++;
1483 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1485 update_rate_histogram(stream->rate_hist, cfg, pkt);
1487 if (stream->config.write_webm) {
1488 write_webm_block(&stream->webm_ctx, cfg, pkt);
1491 if (!stream->config.write_webm) {
1492 if (pkt->data.frame.partition_id <= 0) {
1493 ivf_header_pos = ftello(stream->file);
1494 fsize = pkt->data.frame.sz;
1496 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1498 fsize += pkt->data.frame.sz;
1500 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
1501 const FileOffset currpos = ftello(stream->file);
1502 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1503 ivf_write_frame_size(stream->file, fsize);
1504 fseeko(stream->file, currpos, SEEK_SET);
1508 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1511 stream->nbytes += pkt->data.raw.sz;
1515 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1516 vpx_codec_decode(&stream->decoder, pkt->data.frame.buf,
1517 (unsigned int)pkt->data.frame.sz, NULL, 0);
1518 if (stream->decoder.err) {
1519 warn_or_exit_on_error(&stream->decoder,
1520 global->test_decode == TEST_DECODE_FATAL,
1521 "Failed to decode frame %d in stream %d",
1522 stream->frames_out + 1, stream->index);
1523 stream->mismatch_seen = stream->frames_out + 1;
1528 case VPX_CODEC_STATS_PKT:
1529 stream->frames_out++;
1530 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1531 pkt->data.twopass_stats.sz);
1532 stream->nbytes += pkt->data.raw.sz;
1534 case VPX_CODEC_PSNR_PKT:
1536 if (global->show_psnr) {
1539 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1540 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1541 for (i = 0; i < 4; i++) {
1543 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1544 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1546 stream->psnr_count++;
1555 static void show_psnr(struct stream_state *stream, double peak) {
1559 if (!stream->psnr_count) return;
1561 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1562 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
1563 (double)stream->psnr_sse_total);
1564 fprintf(stderr, " %.3f", ovpsnr);
1566 for (i = 0; i < 4; i++) {
1567 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1569 fprintf(stderr, "\n");
1572 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1573 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1576 static void test_decode(struct stream_state *stream,
1577 enum TestDecodeFatality fatal,
1578 const VpxInterface *codec) {
1579 vpx_image_t enc_img, dec_img;
1581 if (stream->mismatch_seen) return;
1583 /* Get the internal reference frame */
1584 if (strcmp(codec->name, "vp8") == 0) {
1585 struct vpx_ref_frame ref_enc, ref_dec;
1588 width = (stream->config.cfg.g_w + 15) & ~15;
1589 height = (stream->config.cfg.g_h + 15) & ~15;
1590 vpx_img_alloc(&ref_enc.img, VPX_IMG_FMT_I420, width, height, 1);
1591 enc_img = ref_enc.img;
1592 vpx_img_alloc(&ref_dec.img, VPX_IMG_FMT_I420, width, height, 1);
1593 dec_img = ref_dec.img;
1595 ref_enc.frame_type = VP8_LAST_FRAME;
1596 ref_dec.frame_type = VP8_LAST_FRAME;
1597 vpx_codec_control(&stream->encoder, VP8_COPY_REFERENCE, &ref_enc);
1598 vpx_codec_control(&stream->decoder, VP8_COPY_REFERENCE, &ref_dec);
1600 struct vp9_ref_frame ref_enc, ref_dec;
1604 vpx_codec_control(&stream->encoder, VP9_GET_REFERENCE, &ref_enc);
1605 enc_img = ref_enc.img;
1606 vpx_codec_control(&stream->decoder, VP9_GET_REFERENCE, &ref_dec);
1607 dec_img = ref_dec.img;
1608 #if CONFIG_VP9_HIGHBITDEPTH
1609 if ((enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) !=
1610 (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
1611 if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1612 vpx_img_alloc(&enc_img, enc_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
1613 enc_img.d_w, enc_img.d_h, 16);
1614 vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
1616 if (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1617 vpx_img_alloc(&dec_img, dec_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
1618 dec_img.d_w, dec_img.d_h, 16);
1619 vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
1624 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1625 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1627 if (!compare_img(&enc_img, &dec_img)) {
1628 int y[4], u[4], v[4];
1629 #if CONFIG_VP9_HIGHBITDEPTH
1630 if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1631 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1633 find_mismatch(&enc_img, &dec_img, y, u, v);
1636 find_mismatch(&enc_img, &dec_img, y, u, v);
1638 stream->decoder.err = 1;
1639 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1640 "Stream %d: Encode/decode mismatch on frame %d at"
1641 " Y[%d, %d] {%d/%d},"
1642 " U[%d, %d] {%d/%d},"
1643 " V[%d, %d] {%d/%d}",
1644 stream->index, stream->frames_out, y[0], y[1], y[2],
1645 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1646 stream->mismatch_seen = stream->frames_out;
1649 vpx_img_free(&enc_img);
1650 vpx_img_free(&dec_img);
1653 static void print_time(const char *label, int64_t etl) {
1660 etl -= hours * 3600;
1665 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1668 fprintf(stderr, "[%3s unknown] ", label);
1672 int main(int argc, const char **argv_) {
1675 #if CONFIG_VP9_HIGHBITDEPTH
1676 vpx_image_t raw_shift;
1677 int allocated_raw_shift = 0;
1678 int use_16bit_internal = 0;
1679 int input_shift = 0;
1681 int frame_avail, got_data;
1683 struct VpxInputContext input;
1684 struct VpxEncoderConfig global;
1685 struct stream_state *streams = NULL;
1686 char **argv, **argi;
1687 uint64_t cx_time = 0;
1691 memset(&input, 0, sizeof(input));
1692 memset(&raw, 0, sizeof(raw));
1693 exec_name = argv_[0];
1695 /* Setup default input stream settings */
1696 input.framerate.numerator = 30;
1697 input.framerate.denominator = 1;
1698 input.only_i420 = 1;
1699 input.bit_depth = 0;
1701 /* First parse the global configuration values, because we want to apply
1702 * other parameters on top of the default configuration provided by the
1705 argv = argv_dup(argc - 1, argv_ + 1);
1706 parse_global_config(&global, argv);
1708 if (argc < 3) usage_exit();
1710 switch (global.color_type) {
1711 case I420: input.fmt = VPX_IMG_FMT_I420; break;
1712 case I422: input.fmt = VPX_IMG_FMT_I422; break;
1713 case I444: input.fmt = VPX_IMG_FMT_I444; break;
1714 case I440: input.fmt = VPX_IMG_FMT_I440; break;
1715 case YV12: input.fmt = VPX_IMG_FMT_YV12; break;
1716 case NV12: input.fmt = VPX_IMG_FMT_NV12; break;
1720 /* Now parse each stream's parameters. Using a local scope here
1721 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1724 struct stream_state *stream = NULL;
1727 stream = new_stream(&global, stream);
1729 if (!streams) streams = stream;
1730 } while (parse_stream_params(&global, stream, argv));
1733 /* Check for unrecognized options */
1734 for (argi = argv; *argi; argi++)
1735 if (argi[0][0] == '-' && argi[0][1])
1736 die("Error: Unrecognized option %s\n", *argi);
1738 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1739 &stream->config.cfg););
1741 /* Handle non-option arguments */
1742 input.filename = argv[0];
1744 if (!input.filename) {
1745 fprintf(stderr, "No input file specified!\n");
1749 /* Decide if other chroma subsamplings than 4:2:0 are supported */
1750 if (global.codec->fourcc == VP9_FOURCC) input.only_i420 = 0;
1752 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1753 int frames_in = 0, seen_frames = 0;
1754 int64_t estimated_time_left = -1;
1755 int64_t average_rate = -1;
1756 int64_t lagged_count = 0;
1758 open_input_file(&input);
1760 /* If the input file doesn't specify its w/h (raw files), try to get
1761 * the data from the first stream's configuration.
1763 if (!input.width || !input.height) {
1765 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1766 input.width = stream->config.cfg.g_w;
1767 input.height = stream->config.cfg.g_h;
1773 /* Update stream configurations from the input file's parameters */
1774 if (!input.width || !input.height)
1776 "Specify stream dimensions with --width (-w) "
1777 " and --height (-h)");
1779 /* If input file does not specify bit-depth but input-bit-depth parameter
1780 * exists, assume that to be the input bit-depth. However, if the
1781 * input-bit-depth paramter does not exist, assume the input bit-depth
1782 * to be the same as the codec bit-depth.
1784 if (!input.bit_depth) {
1786 if (stream->config.cfg.g_input_bit_depth)
1787 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1789 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1790 (int)stream->config.cfg.g_bit_depth;
1792 if (input.bit_depth > 8) input.fmt |= VPX_IMG_FMT_HIGHBITDEPTH;
1795 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
1798 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
1799 FOREACH_STREAM(validate_stream_config(stream, &global));
1801 /* Ensure that --passes and --pass are consistent. If --pass is set and
1802 * --passes=2, ensure --fpf was set.
1804 if (global.pass && global.passes == 2)
1806 if (!stream->config.stats_fn)
1807 die("Stream %d: Must specify --fpf when --pass=%d"
1808 " and --passes=2\n",
1809 stream->index, global.pass);
1814 if (stream->config.write_webm) {
1815 stream->config.write_webm = 0;
1817 "vpxenc was compiled without WebM container support."
1818 "Producing IVF output");
1823 /* Use the frame rate from the file only if none was specified
1824 * on the command-line.
1826 if (!global.have_framerate) {
1827 global.framerate.num = input.framerate.numerator;
1828 global.framerate.den = input.framerate.denominator;
1829 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
1830 stream->config.cfg.g_timebase.num = global.framerate.den);
1833 /* Show configuration */
1834 if (global.verbose && pass == 0)
1835 FOREACH_STREAM(show_stream_config(stream, &global, &input));
1837 if (pass == (global.pass ? global.pass - 1 : 0)) {
1838 // The Y4M reader does its own allocation.
1839 if (input.file_type != FILE_TYPE_Y4M) {
1840 vpx_img_alloc(&raw, input.fmt, input.width, input.height, 32);
1842 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
1843 &stream->config.cfg, &global.framerate));
1846 FOREACH_STREAM(setup_pass(stream, &global, pass));
1848 open_output_file(stream, &global, &input.pixel_aspect_ratio));
1849 FOREACH_STREAM(initialize_encoder(stream, &global));
1851 #if CONFIG_VP9_HIGHBITDEPTH
1852 if (strcmp(global.codec->name, "vp9") == 0) {
1853 // Check to see if at least one stream uses 16 bit internal.
1854 // Currently assume that the bit_depths for all streams using
1855 // highbitdepth are the same.
1857 if (stream->config.use_16bit_internal) {
1858 use_16bit_internal = 1;
1860 if (stream->config.cfg.g_profile == 0) {
1863 input_shift = (int)stream->config.cfg.g_bit_depth -
1864 stream->config.cfg.g_input_bit_depth;
1873 while (frame_avail || got_data) {
1874 struct vpx_usec_timer timer;
1876 if (!global.limit || frames_in < global.limit) {
1877 frame_avail = read_frame(&input, &raw);
1879 if (frame_avail) frames_in++;
1881 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
1883 if (!global.quiet) {
1884 float fps = usec_to_fps(cx_time, seen_frames);
1885 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
1887 if (stream_cnt == 1)
1888 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
1889 streams->frames_out, (int64_t)streams->nbytes);
1891 fprintf(stderr, "frame %4d ", frames_in);
1893 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
1894 cx_time > 9999999 ? cx_time / 1000 : cx_time,
1895 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
1896 fps >= 1.0 ? "fps" : "fpm");
1897 print_time("ETA", estimated_time_left);
1903 if (frames_in > global.skip_frames) {
1904 #if CONFIG_VP9_HIGHBITDEPTH
1905 vpx_image_t *frame_to_encode;
1906 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
1907 assert(use_16bit_internal);
1908 // Input bit depth and stream bit depth do not match, so up
1909 // shift frame to stream bit depth
1910 if (!allocated_raw_shift) {
1911 vpx_img_alloc(&raw_shift, raw.fmt | VPX_IMG_FMT_HIGHBITDEPTH,
1912 input.width, input.height, 32);
1913 allocated_raw_shift = 1;
1915 vpx_img_upshift(&raw_shift, &raw, input_shift);
1916 frame_to_encode = &raw_shift;
1918 frame_to_encode = &raw;
1920 vpx_usec_timer_start(&timer);
1921 if (use_16bit_internal) {
1922 assert(frame_to_encode->fmt & VPX_IMG_FMT_HIGHBITDEPTH);
1924 if (stream->config.use_16bit_internal)
1925 encode_frame(stream, &global,
1926 frame_avail ? frame_to_encode : NULL, frames_in);
1931 assert((frame_to_encode->fmt & VPX_IMG_FMT_HIGHBITDEPTH) == 0);
1932 FOREACH_STREAM(encode_frame(stream, &global,
1933 frame_avail ? frame_to_encode : NULL,
1937 vpx_usec_timer_start(&timer);
1938 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
1941 vpx_usec_timer_mark(&timer);
1942 cx_time += vpx_usec_timer_elapsed(&timer);
1944 FOREACH_STREAM(update_quantizer_histogram(stream));
1947 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
1949 if (!got_data && input.length && streams != NULL &&
1950 !streams->frames_out) {
1951 lagged_count = global.limit ? seen_frames : ftello(input.file);
1952 } else if (input.length) {
1957 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
1959 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
1960 remaining = 1000 * (global.limit - global.skip_frames -
1961 seen_frames + lagged_count);
1963 const int64_t input_pos = ftello(input.file);
1964 const int64_t input_pos_lagged = input_pos - lagged_count;
1965 const int64_t limit = input.length;
1967 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
1968 remaining = limit - input_pos + lagged_count;
1972 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
1973 estimated_time_left = average_rate ? remaining / average_rate : -1;
1976 if (got_data && global.test_decode != TEST_DECODE_OFF)
1977 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
1981 if (!global.quiet) fprintf(stderr, "\033[K");
1984 if (stream_cnt > 1) fprintf(stderr, "\n");
1986 if (!global.quiet) {
1987 FOREACH_STREAM(fprintf(
1989 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64 "b/f %7" PRId64
1990 "b/s %7" PRId64 " %s (%.2f fps)\033[K\n",
1991 pass + 1, global.passes, frames_in, stream->frames_out,
1992 (int64_t)stream->nbytes,
1993 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
1995 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
1996 global.framerate.den / seen_frames
1998 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
1999 stream->cx_time > 9999999 ? "ms" : "us",
2000 usec_to_fps(stream->cx_time, seen_frames)));
2003 if (global.show_psnr) {
2004 if (global.codec->fourcc == VP9_FOURCC) {
2006 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2008 FOREACH_STREAM(show_psnr(stream, 255.0));
2012 FOREACH_STREAM(vpx_codec_destroy(&stream->encoder));
2014 if (global.test_decode != TEST_DECODE_OFF) {
2015 FOREACH_STREAM(vpx_codec_destroy(&stream->decoder));
2018 close_input_file(&input);
2020 if (global.test_decode == TEST_DECODE_FATAL) {
2021 FOREACH_STREAM(res |= stream->mismatch_seen);
2023 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
2025 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
2027 if (global.pass) break;
2030 if (global.show_q_hist_buckets)
2032 show_q_histogram(stream->counts, global.show_q_hist_buckets));
2034 if (global.show_rate_hist_buckets)
2035 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2036 global.show_rate_hist_buckets));
2037 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
2039 #if CONFIG_INTERNAL_STATS
2040 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2041 * to match some existing utilities.
2043 if (!(global.pass == 1 && global.passes == 2))
2045 FILE *f = fopen("opsnr.stt", "a");
2046 if (stream->mismatch_seen) {
2047 fprintf(f, "First mismatch occurred in frame %d\n",
2048 stream->mismatch_seen);
2050 fprintf(f, "No mismatch detected in recon buffers\n");
2056 #if CONFIG_VP9_HIGHBITDEPTH
2057 if (allocated_raw_shift) vpx_img_free(&raw_shift);
2062 return res ? EXIT_FAILURE : EXIT_SUCCESS;