12 #include "apps/aomenc.h"
14 #include "config/aom_config.h"
24 #if CONFIG_AV1_DECODER
30 #include "aom/aom_integer.h"
32 #include "aom_dsp/aom_dsp_common.h"
33 #include "aom_ports/aom_timer.h"
34 #include "aom_ports/mem_ops.h"
35 #include "common/args.h"
36 #include "common/ivfenc.h"
37 #include "common/tools_common.h"
38 #include "common/warnings.h"
41 #include "common/webmenc.h"
44 #include "common/y4minput.h"
45 #include "examples/encoder_util.h"
46 #include "stats/aomstats.h"
47 #include "stats/rate_hist.h"
50 #include "third_party/libyuv/include/libyuv/scale.h"
54 static size_t wrap_fread(
void *ptr,
size_t size,
size_t nmemb, FILE *stream) {
55 return fread(ptr, size, nmemb, stream);
57 #define fread wrap_fread
59 static size_t wrap_fwrite(
const void *ptr,
size_t size,
size_t nmemb,
61 return fwrite(ptr, size, nmemb, stream);
63 #define fwrite wrap_fwrite
65 static const char *exec_name;
68 const char *s, va_list ap) {
72 vfprintf(stderr, s, ap);
75 if (detail) fprintf(stderr,
" %s\n", detail);
77 if (fatal) exit(EXIT_FAILURE);
81 static void ctx_exit_on_error(
aom_codec_ctx_t *ctx,
const char *s, ...) {
85 warn_or_exit_on_errorv(ctx, 1, s, ap);
94 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 static int read_frame(
struct AvxInputContext *input_ctx,
aom_image_t *img) {
99 FILE *f = input_ctx->file;
100 y4m_input *y4m = &input_ctx->y4m;
103 if (input_ctx->file_type == FILE_TYPE_Y4M) {
104 if (y4m_input_fetch_frame(y4m, f, img) < 1)
return 0;
106 shortread = read_yuv_frame(input_ctx, img);
112 static int file_is_y4m(
const char detect[4]) {
113 if (memcmp(detect,
"YUV4", 4) == 0) {
119 static int fourcc_is_ivf(
const char detect[4]) {
120 if (memcmp(detect,
"DKIF", 4) == 0) {
126 static const arg_def_t help =
127 ARG_DEF(NULL,
"help", 0,
"Show usage options and exit");
128 static const arg_def_t debugmode =
129 ARG_DEF(
"D",
"debug", 0,
"Debug mode (makes output deterministic)");
130 static const arg_def_t outputfile =
131 ARG_DEF(
"o",
"output", 1,
"Output filename");
132 static const arg_def_t use_yv12 =
133 ARG_DEF(NULL,
"yv12", 0,
"Input file is YV12 ");
134 static const arg_def_t use_i420 =
135 ARG_DEF(NULL,
"i420", 0,
"Input file is I420 (default)");
136 static const arg_def_t use_i422 =
137 ARG_DEF(NULL,
"i422", 0,
"Input file is I422");
138 static const arg_def_t use_i444 =
139 ARG_DEF(NULL,
"i444", 0,
"Input file is I444");
140 static const arg_def_t codecarg = ARG_DEF(NULL,
"codec", 1,
"Codec to use");
141 static const arg_def_t passes =
142 ARG_DEF(
"p",
"passes", 1,
"Number of passes (1/2)");
143 static const arg_def_t pass_arg =
144 ARG_DEF(NULL,
"pass", 1,
"Pass to execute (1/2)");
145 static const arg_def_t fpf_name =
146 ARG_DEF(NULL,
"fpf", 1,
"First pass statistics file name");
147 static const arg_def_t limit =
148 ARG_DEF(NULL,
"limit", 1,
"Stop encoding after n input frames");
149 static const arg_def_t skip =
150 ARG_DEF(NULL,
"skip", 1,
"Skip the first n input frames");
151 static const arg_def_t good_dl =
152 ARG_DEF(NULL,
"good", 0,
"Use Good Quality Deadline");
153 static const arg_def_t rt_dl =
154 ARG_DEF(NULL,
"rt", 0,
"Use Realtime Quality Deadline");
155 static const arg_def_t quietarg =
156 ARG_DEF(
"q",
"quiet", 0,
"Do not print encode progress");
157 static const arg_def_t verbosearg =
158 ARG_DEF(
"v",
"verbose", 0,
"Show encoder parameters");
159 static const arg_def_t psnrarg =
160 ARG_DEF(NULL,
"psnr", 0,
"Show PSNR in status line");
161 #if CONFIG_FILEOPTIONS
162 static const arg_def_t use_cfg = ARG_DEF(
"c",
"cfg", 1,
"Config file to use");
163 static const arg_def_t ext_partition =
164 ARG_DEF(NULL,
"ext-partition", 1,
"corresponds to extended partitions");
167 static const struct arg_enum_list test_decode_enum[] = {
168 {
"off", TEST_DECODE_OFF },
169 {
"fatal", TEST_DECODE_FATAL },
170 {
"warn", TEST_DECODE_WARN },
173 static const arg_def_t recontest = ARG_DEF_ENUM(
174 NULL,
"test-decode", 1,
"Test encode/decode mismatch", test_decode_enum);
175 static const arg_def_t framerate =
176 ARG_DEF(NULL,
"fps", 1,
"Stream frame rate (rate/scale)");
177 static const arg_def_t use_webm =
178 ARG_DEF(NULL,
"webm", 0,
"Output WebM (default when WebM IO is enabled)");
179 static const arg_def_t use_ivf = ARG_DEF(NULL,
"ivf", 0,
"Output IVF");
180 static const arg_def_t use_obu = ARG_DEF(NULL,
"obu", 0,
"Output OBU");
181 static const arg_def_t q_hist_n =
182 ARG_DEF(NULL,
"q-hist", 1,
"Show quantizer histogram (n-buckets)");
183 static const arg_def_t rate_hist_n =
184 ARG_DEF(NULL,
"rate-hist", 1,
"Show rate histogram (n-buckets)");
185 static const arg_def_t disable_warnings =
186 ARG_DEF(NULL,
"disable-warnings", 0,
187 "Disable warnings about potentially incorrect encode settings.");
188 static const arg_def_t disable_warning_prompt =
189 ARG_DEF(
"y",
"disable-warning-prompt", 0,
190 "Display warnings, but do not prompt user to continue.");
191 static const struct arg_enum_list bitdepth_enum[] = {
195 static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
197 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
199 static const arg_def_t inbitdeptharg =
200 ARG_DEF(NULL,
"input-bit-depth", 1,
"Bit depth of input");
202 static const arg_def_t input_chroma_subsampling_x = ARG_DEF(
203 NULL,
"input-chroma-subsampling-x", 1,
"chroma subsampling x value.");
204 static const arg_def_t input_chroma_subsampling_y = ARG_DEF(
205 NULL,
"input-chroma-subsampling-y", 1,
"chroma subsampling y value.");
207 static const arg_def_t *main_args[] = { &help,
208 #if CONFIG_FILEOPTIONS
230 &disable_warning_prompt,
234 static const arg_def_t usage =
235 ARG_DEF(
"u",
"usage", 1,
"Usage profile number to use");
236 static const arg_def_t threads =
237 ARG_DEF(
"t",
"threads", 1,
"Max number of threads to use");
238 static const arg_def_t profile =
239 ARG_DEF(NULL,
"profile", 1,
"Bitstream profile number to use");
240 static const arg_def_t width = ARG_DEF(
"w",
"width", 1,
"Frame width");
241 static const arg_def_t height = ARG_DEF(
"h",
"height", 1,
"Frame height");
242 static const arg_def_t forced_max_frame_width = ARG_DEF(
243 NULL,
"forced_max_frame_width", 0,
"Maximum frame width value to force");
244 static const arg_def_t forced_max_frame_height = ARG_DEF(
245 NULL,
"forced_max_frame_height", 0,
"Maximum frame height value to force");
247 static const struct arg_enum_list stereo_mode_enum[] = {
248 {
"mono", STEREO_FORMAT_MONO },
249 {
"left-right", STEREO_FORMAT_LEFT_RIGHT },
250 {
"bottom-top", STEREO_FORMAT_BOTTOM_TOP },
251 {
"top-bottom", STEREO_FORMAT_TOP_BOTTOM },
252 {
"right-left", STEREO_FORMAT_RIGHT_LEFT },
255 static const arg_def_t stereo_mode = ARG_DEF_ENUM(
256 NULL,
"stereo-mode", 1,
"Stereo 3D video format", stereo_mode_enum);
258 static const arg_def_t timebase = ARG_DEF(
259 NULL,
"timebase", 1,
"Output timestamp precision (fractional seconds)");
260 static const arg_def_t global_error_resilient =
261 ARG_DEF(NULL,
"global-error-resilient", 1,
262 "Enable global error resiliency features");
263 static const arg_def_t lag_in_frames =
264 ARG_DEF(NULL,
"lag-in-frames", 1,
"Max number of frames to lag");
265 static const arg_def_t large_scale_tile = ARG_DEF(
266 NULL,
"large-scale-tile", 1,
267 "Large scale tile coding (0: off (default), 1: on (ivf output only))");
268 static const arg_def_t monochrome =
269 ARG_DEF(NULL,
"monochrome", 0,
"Monochrome video (no chroma planes)");
270 static const arg_def_t full_still_picture_hdr = ARG_DEF(
271 NULL,
"full-still-picture-hdr", 0,
"Use full header for still picture");
273 static const arg_def_t *global_args[] = { &use_yv12,
282 &forced_max_frame_width,
283 &forced_max_frame_height,
289 &global_error_resilient,
294 &full_still_picture_hdr,
297 static const arg_def_t dropframe_thresh =
298 ARG_DEF(NULL,
"drop-frame", 1,
"Temporal resampling threshold (buf %)");
299 static const arg_def_t resize_mode =
300 ARG_DEF(NULL,
"resize-mode", 1,
"Frame resize mode");
301 static const arg_def_t resize_denominator =
302 ARG_DEF(NULL,
"resize-denominator", 1,
"Frame resize denominator");
303 static const arg_def_t resize_kf_denominator = ARG_DEF(
304 NULL,
"resize-kf-denominator", 1,
"Frame resize keyframe denominator");
305 static const arg_def_t superres_mode =
306 ARG_DEF(NULL,
"superres-mode", 1,
"Frame super-resolution mode");
307 static const arg_def_t superres_denominator = ARG_DEF(
308 NULL,
"superres-denominator", 1,
"Frame super-resolution denominator");
309 static const arg_def_t superres_kf_denominator =
310 ARG_DEF(NULL,
"superres-kf-denominator", 1,
311 "Frame super-resolution keyframe denominator");
312 static const arg_def_t superres_qthresh = ARG_DEF(
313 NULL,
"superres-qthresh", 1,
"Frame super-resolution qindex threshold");
314 static const arg_def_t superres_kf_qthresh =
315 ARG_DEF(NULL,
"superres-kf-qthresh", 1,
316 "Frame super-resolution keyframe qindex threshold");
317 static const struct arg_enum_list end_usage_enum[] = { {
"vbr",
AOM_VBR },
322 static const arg_def_t end_usage =
323 ARG_DEF_ENUM(NULL,
"end-usage", 1,
"Rate control mode", end_usage_enum);
324 static const arg_def_t target_bitrate =
325 ARG_DEF(NULL,
"target-bitrate", 1,
"Bitrate (kbps)");
326 static const arg_def_t min_quantizer =
327 ARG_DEF(NULL,
"min-q", 1,
"Minimum (best) quantizer");
328 static const arg_def_t max_quantizer =
329 ARG_DEF(NULL,
"max-q", 1,
"Maximum (worst) quantizer");
330 static const arg_def_t undershoot_pct =
331 ARG_DEF(NULL,
"undershoot-pct", 1,
"Datarate undershoot (min) target (%)");
332 static const arg_def_t overshoot_pct =
333 ARG_DEF(NULL,
"overshoot-pct", 1,
"Datarate overshoot (max) target (%)");
334 static const arg_def_t buf_sz =
335 ARG_DEF(NULL,
"buf-sz", 1,
"Client buffer size (ms)");
336 static const arg_def_t buf_initial_sz =
337 ARG_DEF(NULL,
"buf-initial-sz", 1,
"Client initial buffer size (ms)");
338 static const arg_def_t buf_optimal_sz =
339 ARG_DEF(NULL,
"buf-optimal-sz", 1,
"Client optimal buffer size (ms)");
340 static const arg_def_t *rc_args[] = { &dropframe_thresh,
343 &resize_kf_denominator,
345 &superres_denominator,
346 &superres_kf_denominator,
348 &superres_kf_qthresh,
360 static const arg_def_t bias_pct =
361 ARG_DEF(NULL,
"bias-pct", 1,
"CBR/VBR bias (0=CBR, 100=VBR)");
362 static const arg_def_t minsection_pct =
363 ARG_DEF(NULL,
"minsection-pct", 1,
"GOP min bitrate (% of target)");
364 static const arg_def_t maxsection_pct =
365 ARG_DEF(NULL,
"maxsection-pct", 1,
"GOP max bitrate (% of target)");
366 static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
367 &maxsection_pct, NULL };
368 static const arg_def_t fwd_kf_enabled =
369 ARG_DEF(NULL,
"enable-fwd-kf", 1,
"Enable forward reference keyframes");
370 static const arg_def_t kf_min_dist =
371 ARG_DEF(NULL,
"kf-min-dist", 1,
"Minimum keyframe interval (frames)");
372 static const arg_def_t kf_max_dist =
373 ARG_DEF(NULL,
"kf-max-dist", 1,
"Maximum keyframe interval (frames)");
374 static const arg_def_t kf_disabled =
375 ARG_DEF(NULL,
"disable-kf", 0,
"Disable keyframe placement");
376 static const arg_def_t *kf_args[] = { &fwd_kf_enabled, &kf_min_dist,
377 &kf_max_dist, &kf_disabled, NULL };
378 static const arg_def_t sframe_dist =
379 ARG_DEF(NULL,
"sframe-dist", 1,
"S-Frame interval (frames)");
380 static const arg_def_t sframe_mode =
381 ARG_DEF(NULL,
"sframe-mode", 1,
"S-Frame insertion mode (1..2)");
382 static const arg_def_t save_as_annexb =
383 ARG_DEF(NULL,
"annexb", 1,
"Save as Annex-B");
384 static const arg_def_t noise_sens =
385 ARG_DEF(NULL,
"noise-sensitivity", 1,
"Noise sensitivity (frames to blur)");
386 static const arg_def_t sharpness =
387 ARG_DEF(NULL,
"sharpness", 1,
"Loop filter sharpness (0..7)");
388 static const arg_def_t static_thresh =
389 ARG_DEF(NULL,
"static-thresh", 1,
"Motion detection threshold");
390 static const arg_def_t auto_altref =
391 ARG_DEF(NULL,
"auto-alt-ref", 1,
"Enable automatic alt reference frames");
392 static const arg_def_t arnr_maxframes =
393 ARG_DEF(NULL,
"arnr-maxframes", 1,
"AltRef max frames (0..15)");
394 static const arg_def_t arnr_strength =
395 ARG_DEF(NULL,
"arnr-strength", 1,
"AltRef filter strength (0..6)");
396 static const struct arg_enum_list tuning_enum[] = {
397 {
"psnr", AOM_TUNE_PSNR },
398 {
"ssim", AOM_TUNE_SSIM },
400 {
"cdef-dist", AOM_TUNE_CDEF_DIST },
401 {
"daala-dist", AOM_TUNE_DAALA_DIST },
405 static const arg_def_t tune_metric =
406 ARG_DEF_ENUM(NULL,
"tune", 1,
"Distortion metric tuned with", tuning_enum);
407 static const arg_def_t cq_level =
408 ARG_DEF(NULL,
"cq-level", 1,
"Constant/Constrained Quality level");
409 static const arg_def_t max_intra_rate_pct =
410 ARG_DEF(NULL,
"max-intra-rate", 1,
"Max I-frame bitrate (pct)");
412 #if CONFIG_AV1_ENCODER
413 static const arg_def_t cpu_used_av1 =
414 ARG_DEF(NULL,
"cpu-used", 1,
"CPU Used (0..8)");
415 static const arg_def_t rowmtarg =
416 ARG_DEF(NULL,
"row-mt", 1,
417 "Enable row based multi-threading (0: off, 1: on (default))");
418 static const arg_def_t tile_cols =
419 ARG_DEF(NULL,
"tile-columns", 1,
"Number of tile columns to use, log2");
420 static const arg_def_t tile_rows =
421 ARG_DEF(NULL,
"tile-rows", 1,
"Number of tile rows to use, log2");
422 static const arg_def_t enable_tpl_model =
423 ARG_DEF(NULL,
"enable-tpl-model", 1,
424 "RDO based on frame temporal dependency "
425 "(0: off, 1: backward source based, 2: forward 2-pass");
426 static const arg_def_t tile_width =
427 ARG_DEF(NULL,
"tile-width", 1,
"Tile widths (comma separated)");
428 static const arg_def_t tile_height =
429 ARG_DEF(NULL,
"tile-height", 1,
"Tile heights (command separated)");
430 static const arg_def_t lossless =
431 ARG_DEF(NULL,
"lossless", 1,
"Lossless mode (0: false (default), 1: true)");
432 static const arg_def_t enable_cdef =
433 ARG_DEF(NULL,
"enable-cdef", 1,
434 "Enable the constrained directional enhancement filter (0: false, "
435 "1: true (default))");
436 static const arg_def_t enable_restoration = ARG_DEF(
437 NULL,
"enable-restoration", 1,
438 "Enable the loop restoration filter (0: false (default in Realtime mode), "
439 "1: true (default in Non-realtime mode))");
440 static const arg_def_t enable_rect_partitions =
441 ARG_DEF(NULL,
"enable-rect-partitions", 1,
442 "Enable rectangular partitions "
443 "(0: false, 1: true (default))");
444 static const arg_def_t enable_ab_partitions =
445 ARG_DEF(NULL,
"enable-ab-partitions", 1,
446 "Enable ab partitions (0: false, 1: true (default))");
447 static const arg_def_t enable_1to4_partitions =
448 ARG_DEF(NULL,
"enable-1to4-partitions", 1,
449 "Enable 1:4 and 4:1 partitions "
450 "(0: false, 1: true (default))");
451 static const arg_def_t min_partition_size =
452 ARG_DEF(NULL,
"min-partition-size", 4,
453 "Set min partition size "
454 "(4:4x4, 8:8x8, 16:16x16, 32:32x32, 64:64x64, 128:128x128)."
455 "On frame with 4k+ resolutions or higher speed settings, the min "
456 "partition size will have a minimum of 8.");
457 static const arg_def_t max_partition_size =
458 ARG_DEF(NULL,
"max-partition-size", 128,
459 "Set max partition size "
460 "(4:4x4, 8:8x8, 16:16x16, 32:32x32, 64:64x64, 128:128x128)");
461 static const arg_def_t enable_dual_filter =
462 ARG_DEF(NULL,
"enable-dual-filter", 1,
463 "Enable dual filter "
464 "(0: false, 1: true (default))");
465 static const arg_def_t enable_chroma_deltaq =
466 ARG_DEF(NULL,
"enable-chroma-deltaq", 1,
467 "Enable chroma delta quant "
468 "(0: false (default), 1: true)");
469 static const arg_def_t enable_intra_edge_filter =
470 ARG_DEF(NULL,
"enable-intra-edge-filter", 1,
471 "Enable intra edge filtering "
472 "(0: false, 1: true (default))");
473 static const arg_def_t enable_order_hint =
474 ARG_DEF(NULL,
"enable-order-hint", 1,
476 "(0: false, 1: true (default))");
477 static const arg_def_t enable_tx64 =
478 ARG_DEF(NULL,
"enable-tx64", 1,
479 "Enable 64-pt transform (0: false, 1: true (default))");
480 static const arg_def_t tx_size_search_method =
481 ARG_DEF(NULL,
"tx-size-search-method", 0,
482 "Set transform block size search method "
483 "(0: Full RD (default), 1: Fast RD, 2: use largest allowed)");
484 static const arg_def_t enable_flip_idtx =
485 ARG_DEF(NULL,
"enable-flip-idtx", 1,
486 "Enable extended transform type (0: false, 1: true (default)) "
487 "including FLIPADST_DCT, DCT_FLIPADST, FLIPADST_FLIPADST, "
488 "ADST_FLIPADST, FLIPADST_ADST, IDTX, V_DCT, H_DCT, V_ADST, "
489 "H_ADST, V_FLIPADST, H_FLIPADST");
490 static const arg_def_t enable_dist_wtd_comp =
491 ARG_DEF(NULL,
"enable-dist-wtd-comp", 1,
492 "Enable distance-weighted compound "
493 "(0: false, 1: true (default))");
494 static const arg_def_t enable_masked_comp =
495 ARG_DEF(NULL,
"enable-masked-comp", 1,
496 "Enable masked (wedge/diff-wtd) compound "
497 "(0: false, 1: true (default))");
498 static const arg_def_t enable_onesided_comp =
499 ARG_DEF(NULL,
"enable-onesided-comp", 1,
500 "Enable one sided compound "
501 "(0: false, 1: true (default))");
502 static const arg_def_t enable_interintra_comp =
503 ARG_DEF(NULL,
"enable-interintra-comp", 1,
504 "Enable interintra compound "
505 "(0: false, 1: true (default))");
506 static const arg_def_t enable_smooth_interintra =
507 ARG_DEF(NULL,
"enable-smooth-interintra", 1,
508 "Enable smooth interintra mode "
509 "(0: false, 1: true (default))");
510 static const arg_def_t enable_diff_wtd_comp =
511 ARG_DEF(NULL,
"enable-diff-wtd-comp", 1,
512 "Enable difference-weighted compound "
513 "(0: false, 1: true (default))");
514 static const arg_def_t enable_interinter_wedge =
515 ARG_DEF(NULL,
"enable-interinter-wedge", 1,
516 "Enable interinter wedge compound "
517 "(0: false, 1: true (default))");
518 static const arg_def_t enable_interintra_wedge =
519 ARG_DEF(NULL,
"enable-interintra-wedge", 1,
520 "Enable interintra wedge compound "
521 "(0: false, 1: true (default))");
522 static const arg_def_t enable_global_motion =
523 ARG_DEF(NULL,
"enable-global-motion", 1,
524 "Enable global motion "
525 "(0: false, 1: true (default))");
526 static const arg_def_t enable_warped_motion =
527 ARG_DEF(NULL,
"enable-warped-motion", 1,
528 "Enable local warped motion "
529 "(0: false, 1: true (default))");
530 static const arg_def_t enable_filter_intra =
531 ARG_DEF(NULL,
"enable-filter-intra", 1,
532 "Enable filter intra prediction mode "
533 "(0: false, 1: true (default))");
534 static const arg_def_t enable_smooth_intra =
535 ARG_DEF(NULL,
"enable-smooth-intra", 1,
536 "Enable smooth intra prediction modes "
537 "(0: false, 1: true (default))");
538 static const arg_def_t enable_paeth_intra =
539 ARG_DEF(NULL,
"enable-paeth-intra", 1,
540 "Enable Paeth intra prediction mode (0: false, 1: true (default))");
541 static const arg_def_t enable_cfl_intra =
542 ARG_DEF(NULL,
"enable-cfl-intra", 1,
543 "Enable chroma from luma intra prediction mode "
544 "(0: false, 1: true (default))");
545 static const arg_def_t force_video_mode =
546 ARG_DEF(NULL,
"force-video-mode", 1,
547 "Force video mode (0: false, 1: true (default))");
548 static const arg_def_t enable_obmc = ARG_DEF(
549 NULL,
"enable-obmc", 1,
"Enable OBMC (0: false, 1: true (default))");
550 static const arg_def_t enable_palette =
551 ARG_DEF(NULL,
"enable-palette", 1,
552 "Enable palette prediction mode (0: false, 1: true (default))");
553 static const arg_def_t enable_intrabc =
554 ARG_DEF(NULL,
"enable-intrabc", 1,
555 "Enable intra block copy prediction mode "
556 "(0: false, 1: true (default))");
557 static const arg_def_t enable_angle_delta =
558 ARG_DEF(NULL,
"enable-angle-delta", 1,
559 "Enable intra angle delta (0: false, 1: true (default))");
560 static const arg_def_t disable_trellis_quant =
561 ARG_DEF(NULL,
"disable-trellis-quant", 1,
562 "Disable trellis optimization of quantized coefficients (0: false "
563 "1: true 2: true for rd search 3: true for estimate yrd serch "
565 static const arg_def_t enable_qm =
566 ARG_DEF(NULL,
"enable-qm", 1,
567 "Enable quantisation matrices (0: false (default), 1: true)");
568 static const arg_def_t qm_min = ARG_DEF(
569 NULL,
"qm-min", 1,
"Min quant matrix flatness (0..15), default is 8");
570 static const arg_def_t qm_max = ARG_DEF(
571 NULL,
"qm-max", 1,
"Max quant matrix flatness (0..15), default is 15");
572 static const arg_def_t reduced_tx_type_set = ARG_DEF(
573 NULL,
"reduced-tx-type-set", 1,
"Use reduced set of transform types");
574 static const arg_def_t use_intra_dct_only =
575 ARG_DEF(NULL,
"use-intra-dct-only", 1,
"Use DCT only for INTRA modes");
576 static const arg_def_t use_inter_dct_only =
577 ARG_DEF(NULL,
"use-inter-dct-only", 1,
"Use DCT only for INTER modes");
578 static const arg_def_t use_intra_default_tx_only =
579 ARG_DEF(NULL,
"use-intra-default-tx-only", 1,
580 "Use Default-transform only for INTRA modes");
581 static const arg_def_t quant_b_adapt =
582 ARG_DEF(NULL,
"quant-b-adapt", 1,
"Use adaptive quantize_b");
583 static const arg_def_t coeff_cost_upd_freq =
584 ARG_DEF(NULL,
"coeff-cost-upd-freq", 1,
585 "Update freq for coeff costs"
586 "0: SB, 1: SB Row per Tile, 2: Tile");
587 static const arg_def_t mode_cost_upd_freq =
588 ARG_DEF(NULL,
"mode-cost-upd-freq", 1,
589 "Update freq for mode costs"
590 "0: SB, 1: SB Row per Tile, 2: Tile");
592 static const arg_def_t enable_dist_8x8 =
593 ARG_DEF(NULL,
"enable-dist-8x8", 1,
594 "Enable dist-8x8 (0: false (default), 1: true)");
595 #endif // CONFIG_DIST_8X8
596 static const arg_def_t num_tg = ARG_DEF(
597 NULL,
"num-tile-groups", 1,
"Maximum number of tile groups, default is 1");
598 static const arg_def_t mtu_size =
599 ARG_DEF(NULL,
"mtu-size", 1,
600 "MTU size for a tile group, default is 0 (no MTU targeting), "
601 "overrides maximum number of tile groups");
602 static const struct arg_enum_list timing_info_enum[] = {
603 {
"unspecified", AOM_TIMING_UNSPECIFIED },
604 {
"constant", AOM_TIMING_EQUAL },
605 {
"model", AOM_TIMING_DEC_MODEL },
608 static const arg_def_t timing_info =
609 ARG_DEF_ENUM(NULL,
"timing-info", 1,
610 "Signal timing info in the bitstream (model unly works for no "
611 "hidden frames, no super-res yet):",
613 static const arg_def_t film_grain_test =
614 ARG_DEF(NULL,
"film-grain-test", 1,
615 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
617 static const arg_def_t film_grain_table =
618 ARG_DEF(NULL,
"film-grain-table", 1,
619 "Path to file containing film grain parameters");
621 static const arg_def_t denoise_noise_level =
622 ARG_DEF(NULL,
"denoise-noise-level", 1,
623 "Amount of noise (from 0 = don't denoise, to 50)");
624 static const arg_def_t denoise_block_size =
625 ARG_DEF(NULL,
"denoise-block-size", 1,
"Denoise block size (default = 32)");
627 static const arg_def_t enable_ref_frame_mvs =
628 ARG_DEF(NULL,
"enable-ref-frame-mvs", 1,
629 "Enable temporal mv prediction (default is 1)");
630 static const arg_def_t frame_parallel_decoding =
631 ARG_DEF(NULL,
"frame-parallel", 1,
632 "Enable frame parallel decodability features "
633 "(0: false (default), 1: true)");
634 static const arg_def_t error_resilient_mode =
635 ARG_DEF(NULL,
"error-resilient", 1,
636 "Enable error resilient features "
637 "(0: false (default), 1: true)");
638 static const arg_def_t aq_mode = ARG_DEF(
640 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
641 "3: cyclic refresh)");
642 static const arg_def_t deltaq_mode =
643 ARG_DEF(NULL,
"deltaq-mode", 1,
644 "Delta qindex mode (0: off, 1: deltaq pred efficiency (default), "
645 "2: deltaq perceptual)");
646 static const arg_def_t deltalf_mode = ARG_DEF(
647 NULL,
"delta-lf-mode", 1,
"Enable delta-lf-mode (0: off (default), 1: on)");
648 static const arg_def_t frame_periodic_boost =
649 ARG_DEF(NULL,
"frame-boost", 1,
650 "Enable frame periodic boost (0: off (default), 1: on)");
651 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
652 NULL,
"gf-cbr-boost", 1,
"Boost for Golden Frame in CBR mode (pct)");
653 static const arg_def_t max_inter_rate_pct =
654 ARG_DEF(NULL,
"max-inter-rate", 1,
"Max P-frame bitrate (pct)");
655 static const arg_def_t min_gf_interval = ARG_DEF(
656 NULL,
"min-gf-interval", 1,
657 "min gf/arf frame interval (default 0, indicating in-built behavior)");
658 static const arg_def_t max_gf_interval = ARG_DEF(
659 NULL,
"max-gf-interval", 1,
660 "max gf/arf frame interval (default 0, indicating in-built behavior)");
661 static const arg_def_t gf_max_pyr_height =
662 ARG_DEF(NULL,
"gf-max-pyr-height", 1,
663 "maximum height for GF group pyramid structure (0 to 4 (default))");
664 static const arg_def_t max_reference_frames = ARG_DEF(
665 NULL,
"max-reference-frames", 1,
666 "maximum number of reference frames allowed per frame (3 to 7 (default))");
667 static const arg_def_t reduced_reference_set =
668 ARG_DEF(NULL,
"reduced-reference-set", 1,
669 "Use reduced set of single and compound references (0: off "
670 "(default), 1: on)");
671 static const arg_def_t target_seq_level_idx =
672 ARG_DEF(NULL,
"target-seq-level-idx", 1,
673 "Target sequence level index. "
674 "Possible values are in the form of \"ABxy\"(pad leading zeros if "
675 "less than 4 digits). "
676 "AB: Operating point(OP) index; "
677 "xy: Target level index for the OP. "
678 "E.g. \"0\" means target level index 0 for the 0th OP; "
679 "\"1021\" means target level index 21 for the 10th OP.");
680 static const arg_def_t set_min_cr =
681 ARG_DEF(NULL,
"min-cr", 1,
682 "Set minimum compression ratio. Take integer values. Default is 0. "
683 "If non-zero, encoder will try to keep the compression ratio of "
684 "each frame to be higher than the given value divided by 100.");
686 static const struct arg_enum_list color_primaries_enum[] = {
702 static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
703 NULL,
"color-primaries", 1,
704 "Color primaries (CICP) of input content:", color_primaries_enum);
706 static const struct arg_enum_list transfer_characteristics_enum[] = {
727 static const arg_def_t input_transfer_characteristics =
728 ARG_DEF_ENUM(NULL,
"transfer-characteristics", 1,
729 "Transfer characteristics (CICP) of input content:",
730 transfer_characteristics_enum);
732 static const struct arg_enum_list matrix_coefficients_enum[] = {
750 static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
751 NULL,
"matrix-coefficients", 1,
752 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
754 static const struct arg_enum_list chroma_sample_position_enum[] = {
761 static const arg_def_t input_chroma_sample_position =
762 ARG_DEF_ENUM(NULL,
"chroma-sample-position", 1,
763 "The chroma sample position when chroma 4:2:0 is signaled:",
764 chroma_sample_position_enum);
766 static const struct arg_enum_list tune_content_enum[] = {
767 {
"default", AOM_CONTENT_DEFAULT },
768 {
"screen", AOM_CONTENT_SCREEN },
772 static const arg_def_t tune_content = ARG_DEF_ENUM(
773 NULL,
"tune-content", 1,
"Tune content type", tune_content_enum);
775 static const arg_def_t cdf_update_mode =
776 ARG_DEF(NULL,
"cdf-update-mode", 1,
777 "CDF update mode for entropy coding "
778 "(0: no CDF update; 1: update CDF on all frames(default); "
779 "2: selectively update CDF on some frames");
781 static const struct arg_enum_list superblock_size_enum[] = {
787 static const arg_def_t superblock_size = ARG_DEF_ENUM(
788 NULL,
"sb-size", 1,
"Superblock size to use", superblock_size_enum);
790 static const arg_def_t set_tier_mask =
791 ARG_DEF(NULL,
"set-tier-mask", 1,
792 "Set bit mask to specify which tier each of the 32 possible "
793 "operating points conforms to. "
794 "Bit value 0(defualt): Main Tier; 1: High Tier.");
796 static const arg_def_t *av1_args[] = { &cpu_used_av1,
814 &enable_rect_partitions,
815 &enable_ab_partitions,
816 &enable_1to4_partitions,
820 &enable_chroma_deltaq,
821 &enable_intra_edge_filter,
824 &tx_size_search_method,
826 &enable_dist_wtd_comp,
828 &enable_onesided_comp,
829 &enable_interintra_comp,
830 &enable_smooth_interintra,
831 &enable_diff_wtd_comp,
832 &enable_interinter_wedge,
833 &enable_interintra_wedge,
834 &enable_global_motion,
835 &enable_warped_motion,
836 &enable_filter_intra,
837 &enable_smooth_intra,
845 &disable_trellis_quant,
849 &reduced_tx_type_set,
852 &use_intra_default_tx_only,
854 &coeff_cost_upd_freq,
859 &frame_parallel_decoding,
860 &error_resilient_mode,
864 &frame_periodic_boost,
868 &input_color_primaries,
869 &input_transfer_characteristics,
870 &input_matrix_coefficients,
871 &input_chroma_sample_position,
882 &denoise_noise_level,
884 #endif // CONFIG_DENOISE
885 &max_reference_frames,
886 &reduced_reference_set,
887 &enable_ref_frame_mvs,
888 &target_seq_level_idx,
893 &input_chroma_subsampling_x,
894 &input_chroma_subsampling_y,
987 #endif // CONFIG_DENOISE
995 #endif // CONFIG_AV1_ENCODER
997 static const arg_def_t *no_args[] = { NULL };
999 static void show_help(FILE *fout,
int shorthelp) {
1000 fprintf(fout,
"Usage: %s <options> -o dst_filename src_filename \n",
1004 fprintf(fout,
"Use --help to see the full list of options.\n");
1008 fprintf(fout,
"\nOptions:\n");
1009 arg_show_usage(fout, main_args);
1010 fprintf(fout,
"\nEncoder Global Options:\n");
1011 arg_show_usage(fout, global_args);
1012 fprintf(fout,
"\nRate Control Options:\n");
1013 arg_show_usage(fout, rc_args);
1014 fprintf(fout,
"\nTwopass Rate Control Options:\n");
1015 arg_show_usage(fout, rc_twopass_args);
1016 fprintf(fout,
"\nKeyframe Placement Options:\n");
1017 arg_show_usage(fout, kf_args);
1018 #if CONFIG_AV1_ENCODER
1019 fprintf(fout,
"\nAV1 Specific Options:\n");
1020 arg_show_usage(fout, av1_args);
1023 "\nStream timebase (--timebase):\n"
1024 " The desired precision of timestamps in the output, expressed\n"
1025 " in fractional seconds. Default is 1/1000.\n");
1026 fprintf(fout,
"\nIncluded encoders:\n\n");
1028 const int num_encoder = get_aom_encoder_count();
1029 for (
int i = 0; i < num_encoder; ++i) {
1030 const AvxInterface *
const encoder = get_aom_encoder_by_index(i);
1031 const char *defstr = (i == (num_encoder - 1)) ?
"(default)" :
"";
1032 fprintf(fout,
" %-6s - %s %s\n", encoder->name,
1035 fprintf(fout,
"\n ");
1036 fprintf(fout,
"Use --codec to switch to a non-default encoder.\n\n");
1039 void usage_exit(
void) {
1040 show_help(stderr, 1);
1044 #if CONFIG_AV1_ENCODER
1045 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
1049 typedef int stereo_format_t;
1050 struct WebmOutputContext {
1056 struct stream_config {
1059 const char *stats_fn;
1060 stereo_format_t stereo_fmt;
1061 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
1064 const char *film_grain_filename;
1067 int use_16bit_internal;
1070 struct stream_state {
1072 struct stream_state *next;
1073 struct stream_config config;
1075 struct rate_hist *rate_hist;
1076 struct WebmOutputContext webm_ctx;
1077 uint64_t psnr_sse_total;
1078 uint64_t psnr_samples_total;
1079 double psnr_totals[4];
1083 unsigned int frames_out;
1090 unsigned int chroma_subsampling_x;
1091 unsigned int chroma_subsampling_y;
1094 static void validate_positive_rational(
const char *msg,
1101 if (rat->
num < 0) die(
"Error: %s must be positive\n", msg);
1103 if (!rat->
den) die(
"Error: %s has zero denominator\n", msg);
1109 static void parse_global_config(
struct AvxEncoderConfig *global,
int argc,
1111 char **argi, **argj;
1113 const int num_encoder = get_aom_encoder_count();
1114 char **argv_local = (
char **)*argv;
1115 #if CONFIG_FILEOPTIONS
1116 int argc_local = argc;
1118 if (num_encoder < 1) die(
"Error: no valid encoder available\n");
1121 memset(global, 0,
sizeof(*global));
1122 global->codec = get_aom_encoder_by_index(num_encoder - 1);
1124 global->color_type = I420;
1127 #if CONFIG_FILEOPTIONS
1128 const char *cfg = NULL;
1129 int cfg_included = 0;
1131 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
1134 #if CONFIG_FILEOPTIONS
1135 if (arg_match(&arg, &use_cfg, argi)) {
1136 if (cfg_included)
continue;
1139 arg_cfg(&argc_local, &argv_local, cfg);
1141 *argj = *argi = *argv_local;
1142 argj = argi = argv_local;
1148 if (arg_match(&arg, &help, argi)) {
1149 show_help(stdout, 0);
1151 }
else if (arg_match(&arg, &codecarg, argi)) {
1152 global->codec = get_aom_encoder_by_name(arg.val);
1154 die(
"Error: Unrecognized argument (%s) to --codec\n", arg.val);
1155 }
else if (arg_match(&arg, &passes, argi)) {
1156 global->passes = arg_parse_uint(&arg);
1158 if (global->passes < 1 || global->passes > 2)
1159 die(
"Error: Invalid number of passes (%d)\n", global->passes);
1160 }
else if (arg_match(&arg, &pass_arg, argi)) {
1161 global->pass = arg_parse_uint(&arg);
1163 if (global->pass < 1 || global->pass > 2)
1164 die(
"Error: Invalid pass selected (%d)\n", global->pass);
1165 }
else if (arg_match(&arg, &input_chroma_sample_position, argi)) {
1166 global->csp = arg_parse_enum(&arg);
1169 }
else if (arg_match(&arg, &usage, argi))
1170 global->usage = arg_parse_uint(&arg);
1171 else if (arg_match(&arg, &good_dl, argi))
1173 else if (arg_match(&arg, &rt_dl, argi))
1175 else if (arg_match(&arg, &use_yv12, argi))
1176 global->color_type = YV12;
1177 else if (arg_match(&arg, &use_i420, argi))
1178 global->color_type = I420;
1179 else if (arg_match(&arg, &use_i422, argi))
1180 global->color_type = I422;
1181 else if (arg_match(&arg, &use_i444, argi))
1182 global->color_type = I444;
1183 else if (arg_match(&arg, &quietarg, argi))
1185 else if (arg_match(&arg, &verbosearg, argi))
1186 global->verbose = 1;
1187 else if (arg_match(&arg, &limit, argi))
1188 global->limit = arg_parse_uint(&arg);
1189 else if (arg_match(&arg, &skip, argi))
1190 global->skip_frames = arg_parse_uint(&arg);
1191 else if (arg_match(&arg, &psnrarg, argi))
1192 global->show_psnr = 1;
1193 else if (arg_match(&arg, &recontest, argi))
1194 global->test_decode = arg_parse_enum_or_int(&arg);
1195 else if (arg_match(&arg, &framerate, argi)) {
1196 global->framerate = arg_parse_rational(&arg);
1197 validate_positive_rational(arg.name, &global->framerate);
1198 global->have_framerate = 1;
1199 }
else if (arg_match(&arg, &debugmode, argi))
1201 else if (arg_match(&arg, &q_hist_n, argi))
1202 global->show_q_hist_buckets = arg_parse_uint(&arg);
1203 else if (arg_match(&arg, &rate_hist_n, argi))
1204 global->show_rate_hist_buckets = arg_parse_uint(&arg);
1205 else if (arg_match(&arg, &disable_warnings, argi))
1206 global->disable_warnings = 1;
1207 else if (arg_match(&arg, &disable_warning_prompt, argi))
1208 global->disable_warning_prompt = 1;
1215 if (global->pass > global->passes) {
1216 warn(
"Assuming --pass=%d implies --passes=%d\n", global->pass,
1218 global->passes = global->pass;
1222 if (global->passes == 0) {
1223 #if CONFIG_AV1_ENCODER
1226 if (global->codec != NULL && global->codec->name != NULL)
1227 global->passes = (strcmp(global->codec->name,
"av1") == 0 &&
1237 warn(
"Enforcing one-pass encoding in realtime mode\n");
1242 static void open_input_file(
struct AvxInputContext *input,
1245 input->file = strcmp(input->filename,
"-") ? fopen(input->filename,
"rb")
1246 : set_binary_mode(stdin);
1248 if (!input->file) fatal(
"Failed to open input file");
1250 if (!fseeko(input->file, 0, SEEK_END)) {
1254 input->length = ftello(input->file);
1255 rewind(input->file);
1259 input->pixel_aspect_ratio.numerator = 1;
1260 input->pixel_aspect_ratio.denominator = 1;
1265 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1266 input->detect.position = 0;
1268 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
1269 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
1270 input->only_i420) >= 0) {
1271 input->file_type = FILE_TYPE_Y4M;
1272 input->width = input->y4m.pic_w;
1273 input->height = input->y4m.pic_h;
1274 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1275 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
1276 input->framerate.numerator = input->y4m.fps_n;
1277 input->framerate.denominator = input->y4m.fps_d;
1278 input->fmt = input->y4m.aom_fmt;
1279 input->bit_depth = input->y4m.bit_depth;
1281 fatal(
"Unsupported Y4M stream.");
1282 }
else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
1283 fatal(
"IVF is not supported as input.");
1285 input->file_type = FILE_TYPE_RAW;
1289 static void close_input_file(
struct AvxInputContext *input) {
1290 fclose(input->file);
1291 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
1294 static struct stream_state *new_stream(
struct AvxEncoderConfig *global,
1295 struct stream_state *prev) {
1296 struct stream_state *stream;
1298 stream = calloc(1,
sizeof(*stream));
1299 if (stream == NULL) {
1300 fatal(
"Failed to allocate new stream.");
1304 memcpy(stream, prev,
sizeof(*stream));
1306 prev->next = stream;
1312 &stream->config.cfg, global->usage);
1318 stream->config.cfg.g_timebase.den = 1000;
1323 stream->config.cfg.g_w = 0;
1324 stream->config.cfg.g_h = 0;
1327 stream->config.write_webm = 1;
1328 stream->config.write_ivf = 0;
1331 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1332 stream->webm_ctx.last_pts_ns = -1;
1333 stream->webm_ctx.writer = NULL;
1334 stream->webm_ctx.segment = NULL;
1338 stream->webm_ctx.debug = global->debug;
1342 stream->config.out_fn = NULL;
1344 stream->next = NULL;
1348 static void set_config_arg_ctrls(
struct stream_config *config,
int key,
1349 const struct arg *arg) {
1352 config->film_grain_filename = arg->val;
1359 j = config->arg_ctrl_cnt;
1360 assert(j < (
int)ARG_CTRL_CNT_MAX);
1361 config->arg_ctrls[j][0] = key;
1362 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1363 ++config->arg_ctrl_cnt;
1370 for (j = 0; j < config->arg_ctrl_cnt; j++)
1371 if (config->arg_ctrls[j][0] == key)
break;
1374 assert(j < (
int)ARG_CTRL_CNT_MAX);
1375 config->arg_ctrls[j][0] = key;
1376 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1379 warn(
"auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
1380 config->arg_ctrls[j][1] = 1;
1382 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1385 static int parse_stream_params(
struct AvxEncoderConfig *global,
1386 struct stream_state *stream,
char **argv) {
1387 char **argi, **argj;
1389 static const arg_def_t **ctrl_args = no_args;
1390 static const int *ctrl_args_map = NULL;
1391 struct stream_config *config = &stream->config;
1392 int eos_mark_found = 0;
1393 int webm_forced = 0;
1397 #if CONFIG_AV1_ENCODER
1398 }
else if (strcmp(global->codec->name,
"av1") == 0) {
1401 ctrl_args = av1_args;
1402 ctrl_args_map = av1_arg_ctrl_map;
1406 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1412 if (eos_mark_found) {
1415 }
else if (!strcmp(*argj,
"--")) {
1420 if (arg_match(&arg, &outputfile, argi)) {
1421 config->out_fn = arg.val;
1423 const size_t out_fn_len = strlen(config->out_fn);
1424 if (out_fn_len >= 4 &&
1425 !strcmp(config->out_fn + out_fn_len - 4,
".ivf")) {
1426 config->write_webm = 0;
1427 config->write_ivf = 1;
1428 }
else if (out_fn_len >= 4 &&
1429 !strcmp(config->out_fn + out_fn_len - 4,
".obu")) {
1430 config->write_webm = 0;
1431 config->write_ivf = 0;
1434 }
else if (arg_match(&arg, &fpf_name, argi)) {
1435 config->stats_fn = arg.val;
1436 }
else if (arg_match(&arg, &use_webm, argi)) {
1438 config->write_webm = 1;
1441 die(
"Error: --webm specified but webm is disabled.");
1443 }
else if (arg_match(&arg, &use_ivf, argi)) {
1444 config->write_webm = 0;
1445 config->write_ivf = 1;
1446 }
else if (arg_match(&arg, &use_obu, argi)) {
1447 config->write_webm = 0;
1448 config->write_ivf = 0;
1449 }
else if (arg_match(&arg, &threads, argi)) {
1450 config->cfg.g_threads = arg_parse_uint(&arg);
1451 }
else if (arg_match(&arg, &profile, argi)) {
1452 config->cfg.g_profile = arg_parse_uint(&arg);
1453 }
else if (arg_match(&arg, &width, argi)) {
1454 config->cfg.g_w = arg_parse_uint(&arg);
1455 }
else if (arg_match(&arg, &height, argi)) {
1456 config->cfg.g_h = arg_parse_uint(&arg);
1457 }
else if (arg_match(&arg, &forced_max_frame_width, argi)) {
1458 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1459 }
else if (arg_match(&arg, &forced_max_frame_height, argi)) {
1460 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
1461 }
else if (arg_match(&arg, &bitdeptharg, argi)) {
1462 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1463 }
else if (arg_match(&arg, &inbitdeptharg, argi)) {
1464 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1465 }
else if (arg_match(&arg, &input_chroma_subsampling_x, argi)) {
1466 stream->chroma_subsampling_x = arg_parse_uint(&arg);
1467 }
else if (arg_match(&arg, &input_chroma_subsampling_y, argi)) {
1468 stream->chroma_subsampling_y = arg_parse_uint(&arg);
1470 }
else if (arg_match(&arg, &stereo_mode, argi)) {
1471 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1473 }
else if (arg_match(&arg, &timebase, argi)) {
1474 config->cfg.g_timebase = arg_parse_rational(&arg);
1475 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1476 }
else if (arg_match(&arg, &global_error_resilient, argi)) {
1477 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1478 }
else if (arg_match(&arg, &lag_in_frames, argi)) {
1479 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1481 config->cfg.rc_end_usage ==
AOM_CBR &&
1482 config->cfg.g_lag_in_frames != 0) {
1483 warn(
"non-zero %s option ignored in realtime CBR mode.\n", arg.name);
1484 config->cfg.g_lag_in_frames = 0;
1486 }
else if (arg_match(&arg, &large_scale_tile, argi)) {
1487 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1488 if (config->cfg.large_scale_tile) global->codec = get_aom_lst_encoder();
1489 }
else if (arg_match(&arg, &monochrome, argi)) {
1490 config->cfg.monochrome = 1;
1491 }
else if (arg_match(&arg, &full_still_picture_hdr, argi)) {
1492 config->cfg.full_still_picture_hdr = 1;
1493 }
else if (arg_match(&arg, &dropframe_thresh, argi)) {
1494 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1495 }
else if (arg_match(&arg, &resize_mode, argi)) {
1496 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1497 }
else if (arg_match(&arg, &resize_denominator, argi)) {
1498 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1499 }
else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1500 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1501 }
else if (arg_match(&arg, &superres_mode, argi)) {
1502 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1503 }
else if (arg_match(&arg, &superres_denominator, argi)) {
1504 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1505 }
else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1506 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1507 }
else if (arg_match(&arg, &superres_qthresh, argi)) {
1508 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1509 }
else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1510 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1511 }
else if (arg_match(&arg, &end_usage, argi)) {
1512 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1513 }
else if (arg_match(&arg, &target_bitrate, argi)) {
1514 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1515 }
else if (arg_match(&arg, &min_quantizer, argi)) {
1516 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1517 }
else if (arg_match(&arg, &max_quantizer, argi)) {
1518 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1519 }
else if (arg_match(&arg, &undershoot_pct, argi)) {
1520 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1521 }
else if (arg_match(&arg, &overshoot_pct, argi)) {
1522 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1523 }
else if (arg_match(&arg, &buf_sz, argi)) {
1524 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1525 }
else if (arg_match(&arg, &buf_initial_sz, argi)) {
1526 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1527 }
else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1528 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1529 }
else if (arg_match(&arg, &bias_pct, argi)) {
1530 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1531 if (global->passes < 2)
1532 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1533 }
else if (arg_match(&arg, &minsection_pct, argi)) {
1534 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1536 if (global->passes < 2)
1537 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1538 }
else if (arg_match(&arg, &maxsection_pct, argi)) {
1539 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1541 if (global->passes < 2)
1542 warn(
"option %s ignored in one-pass mode.\n", arg.name);
1543 }
else if (arg_match(&arg, &fwd_kf_enabled, argi)) {
1544 config->cfg.fwd_kf_enabled = arg_parse_uint(&arg);
1545 }
else if (arg_match(&arg, &kf_min_dist, argi)) {
1546 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1547 }
else if (arg_match(&arg, &kf_max_dist, argi)) {
1548 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1549 }
else if (arg_match(&arg, &kf_disabled, argi)) {
1551 }
else if (arg_match(&arg, &sframe_dist, argi)) {
1552 config->cfg.sframe_dist = arg_parse_uint(&arg);
1553 }
else if (arg_match(&arg, &sframe_mode, argi)) {
1554 config->cfg.sframe_mode = arg_parse_uint(&arg);
1555 }
else if (arg_match(&arg, &save_as_annexb, argi)) {
1556 config->cfg.save_as_annexb = arg_parse_uint(&arg);
1557 }
else if (arg_match(&arg, &tile_width, argi)) {
1558 config->cfg.tile_width_count =
1560 }
else if (arg_match(&arg, &tile_height, argi)) {
1561 config->cfg.tile_height_count =
1563 #if CONFIG_FILEOPTIONS
1564 }
else if (arg_match(&arg, &ext_partition, argi)) {
1565 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1568 arg_match(&arg, &enable_restoration, argi)) {
1569 if (arg_parse_uint(&arg) == 1) {
1570 warn(
"non-zero %s option ignored in realtime mode.\n", arg.name);
1574 for (i = 0; ctrl_args[i]; i++) {
1575 if (arg_match(&arg, ctrl_args[i], argi)) {
1577 if (ctrl_args_map) {
1578 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1585 config->use_16bit_internal =
1586 config->cfg.g_bit_depth >
AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
1587 return eos_mark_found;
1590 #define FOREACH_STREAM(iterator, list) \
1591 for (struct stream_state *iterator = list; iterator; \
1592 iterator = iterator->next)
1594 static void validate_stream_config(
const struct stream_state *stream,
1595 const struct AvxEncoderConfig *global) {
1596 const struct stream_state *streami;
1599 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1601 "Stream %d: Specify stream dimensions with --width (-w) "
1602 " and --height (-h)",
1608 assert(stream->config.cfg.g_input_bit_depth <=
1609 (
unsigned int)stream->config.cfg.g_bit_depth);
1611 for (streami = stream; streami; streami = streami->next) {
1613 if (!streami->config.out_fn)
1614 fatal(
"Stream %d: Output file is required (specify with -o)",
1618 if (streami != stream) {
1619 const char *a = stream->config.out_fn;
1620 const char *b = streami->config.out_fn;
1621 if (!strcmp(a, b) && strcmp(a,
"/dev/null") && strcmp(a,
":nul"))
1622 fatal(
"Stream %d: duplicate output file (from stream %d)",
1623 streami->index, stream->index);
1627 if (streami != stream) {
1628 const char *a = stream->config.stats_fn;
1629 const char *b = streami->config.stats_fn;
1630 if (a && b && !strcmp(a, b))
1631 fatal(
"Stream %d: duplicate stats file (from stream %d)",
1632 streami->index, stream->index);
1637 static void set_stream_dimensions(
struct stream_state *stream,
unsigned int w,
1639 if (!stream->config.cfg.g_w) {
1640 if (!stream->config.cfg.g_h)
1641 stream->config.cfg.g_w = w;
1643 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1645 if (!stream->config.cfg.g_h) {
1646 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1650 static const char *file_type_to_string(
enum VideoFileType t) {
1652 case FILE_TYPE_RAW:
return "RAW";
1653 case FILE_TYPE_Y4M:
return "Y4M";
1654 default:
return "Other";
1658 static const char *image_format_to_string(
aom_img_fmt_t f) {
1668 default:
return "Other";
1672 static void show_stream_config(
struct stream_state *stream,
1673 struct AvxEncoderConfig *global,
1674 struct AvxInputContext *input) {
1675 #define SHOW(field) \
1676 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1678 if (stream->index == 0) {
1679 fprintf(stderr,
"Codec: %s\n",
1681 fprintf(stderr,
"Source file: %s File Type: %s Format: %s\n",
1682 input->filename, file_type_to_string(input->file_type),
1683 image_format_to_string(input->fmt));
1685 if (stream->next || stream->index)
1686 fprintf(stderr,
"\nStream Index: %d\n", stream->index);
1687 fprintf(stderr,
"Destination file: %s\n", stream->config.out_fn);
1688 fprintf(stderr,
"Coding path: %s\n",
1689 stream->config.use_16bit_internal ?
"HBD" :
"LBD");
1690 fprintf(stderr,
"Encoder parameters:\n");
1698 SHOW(g_input_bit_depth);
1699 SHOW(g_timebase.num);
1700 SHOW(g_timebase.den);
1701 SHOW(g_error_resilient);
1703 SHOW(g_lag_in_frames);
1704 SHOW(large_scale_tile);
1705 SHOW(rc_dropframe_thresh);
1706 SHOW(rc_resize_mode);
1707 SHOW(rc_resize_denominator);
1708 SHOW(rc_resize_kf_denominator);
1709 SHOW(rc_superres_mode);
1710 SHOW(rc_superres_denominator);
1711 SHOW(rc_superres_kf_denominator);
1712 SHOW(rc_superres_qthresh);
1713 SHOW(rc_superres_kf_qthresh);
1715 SHOW(rc_target_bitrate);
1716 SHOW(rc_min_quantizer);
1717 SHOW(rc_max_quantizer);
1718 SHOW(rc_undershoot_pct);
1719 SHOW(rc_overshoot_pct);
1721 SHOW(rc_buf_initial_sz);
1722 SHOW(rc_buf_optimal_sz);
1723 SHOW(rc_2pass_vbr_bias_pct);
1724 SHOW(rc_2pass_vbr_minsection_pct);
1725 SHOW(rc_2pass_vbr_maxsection_pct);
1726 SHOW(fwd_kf_enabled);
1732 static void open_output_file(
struct stream_state *stream,
1733 struct AvxEncoderConfig *global,
1734 const struct AvxRational *pixel_aspect_ratio) {
1735 const char *fn = stream->config.out_fn;
1740 stream->file = strcmp(fn,
"-") ? fopen(fn,
"wb") : set_binary_mode(stdout);
1742 if (!stream->file) fatal(
"Failed to open output file");
1744 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1745 fatal(
"WebM output to pipes not supported.");
1748 if (stream->config.write_webm) {
1749 stream->webm_ctx.stream = stream->file;
1750 if (write_webm_file_header(&stream->webm_ctx, &stream->encoder,
cfg,
1751 stream->config.stereo_fmt, global->codec->fourcc,
1752 pixel_aspect_ratio) != 0) {
1753 fatal(
"WebM writer initialization failed.");
1757 (void)pixel_aspect_ratio;
1760 if (!stream->config.write_webm && stream->config.write_ivf) {
1761 ivf_write_file_header(stream->file,
cfg, global->codec->fourcc, 0);
1765 static void close_output_file(
struct stream_state *stream,
1766 unsigned int fourcc) {
1772 if (stream->config.write_webm) {
1773 if (write_webm_file_footer(&stream->webm_ctx) != 0) {
1774 fatal(
"WebM writer finalization failed.");
1779 if (!stream->config.write_webm && stream->config.write_ivf) {
1780 if (!fseek(stream->file, 0, SEEK_SET))
1781 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1782 stream->frames_out);
1785 fclose(stream->file);
1788 static void setup_pass(
struct stream_state *stream,
1789 struct AvxEncoderConfig *global,
int pass) {
1790 if (stream->config.stats_fn) {
1791 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1792 fatal(
"Failed to open statistics store");
1794 if (!stats_open_mem(&stream->stats, pass))
1795 fatal(
"Failed to open statistics store");
1798 stream->config.cfg.g_pass = global->passes == 2
1802 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1805 stream->cx_time = 0;
1807 stream->frames_out = 0;
1810 static void initialize_encoder(
struct stream_state *stream,
1811 struct AvxEncoderConfig *global) {
1820 &stream->config.cfg, flags);
1821 ctx_exit_on_error(&stream->encoder,
"Failed to initialize encoder");
1827 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1828 int ctrl = stream->config.arg_ctrls[i][0];
1829 int value = stream->config.arg_ctrls[i][1];
1831 fprintf(stderr,
"Error: Tried to set control %d = %d\n", ctrl, value);
1833 ctx_exit_on_error(&stream->encoder,
"Failed to control codec");
1835 if (stream->config.film_grain_filename) {
1837 stream->config.film_grain_filename);
1840 #if CONFIG_AV1_DECODER
1841 if (global->test_decode != TEST_DECODE_OFF) {
1842 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1846 if (strcmp(global->codec->name,
"av1") == 0) {
1848 stream->config.cfg.large_scale_tile);
1849 ctx_exit_on_error(&stream->decoder,
"Failed to set decode_tile_mode");
1852 stream->config.cfg.save_as_annexb);
1853 ctx_exit_on_error(&stream->decoder,
"Failed to set is_annexb");
1856 ctx_exit_on_error(&stream->decoder,
"Failed to set decode_tile_row");
1859 ctx_exit_on_error(&stream->decoder,
"Failed to set decode_tile_col");
1865 static void encode_frame(
struct stream_state *stream,
1866 struct AvxEncoderConfig *global,
struct aom_image *img,
1867 unsigned int frames_in) {
1870 struct aom_usec_timer timer;
1873 (cfg->
g_timebase.
den * (int64_t)(frames_in - 1) * global->framerate.den) /
1876 (cfg->
g_timebase.
den * (int64_t)(frames_in)*global->framerate.den) /
1884 fprintf(stderr,
"%s can only scale 4:2:0 inputs\n", exec_name);
1901 stream->img->stride[
AOM_PLANE_V] / 2, stream->img->d_w,
1902 stream->img->d_h, kFilterBox);
1905 stream->encoder.err = 1;
1906 ctx_exit_on_error(&stream->encoder,
1907 "Stream %d: Failed to encode frame.\n"
1908 "libyuv is required for scaling but is currently "
1910 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1916 if (img && (img->
d_w != cfg->
g_w || img->
d_h != cfg->
g_h)) {
1918 fprintf(stderr,
"%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1932 stream->img->d_w, stream->img->d_h, kFilterBox);
1935 stream->encoder.err = 1;
1936 ctx_exit_on_error(&stream->encoder,
1937 "Stream %d: Failed to encode frame.\n"
1938 "Scaling disabled in this configuration. \n"
1939 "To enable, configure with --enable-libyuv\n",
1944 aom_usec_timer_start(&timer);
1946 (uint32_t)(next_frame_start - frame_start), 0);
1947 aom_usec_timer_mark(&timer);
1948 stream->cx_time += aom_usec_timer_elapsed(&timer);
1949 ctx_exit_on_error(&stream->encoder,
"Stream %d: Failed to encode frame",
1953 static void update_quantizer_histogram(
struct stream_state *stream) {
1958 ctx_exit_on_error(&stream->encoder,
"Failed to read quantizer");
1959 stream->counts[q]++;
1963 static void get_cx_data(
struct stream_state *stream,
1964 struct AvxEncoderConfig *global,
int *got_data) {
1971 static size_t fsize = 0;
1972 static FileOffset ivf_header_pos = 0;
1974 switch (pkt->
kind) {
1976 ++stream->frames_out;
1978 fprintf(stderr,
" %6luF", (
unsigned long)pkt->
data.
frame.sz);
1980 update_rate_histogram(stream->rate_hist,
cfg, pkt);
1982 if (stream->config.write_webm) {
1983 if (write_webm_block(&stream->webm_ctx,
cfg, pkt) != 0) {
1984 fatal(
"WebM writer failed.");
1988 if (!stream->config.write_webm) {
1989 if (stream->config.write_ivf) {
1990 if (pkt->
data.
frame.partition_id <= 0) {
1991 ivf_header_pos = ftello(stream->file);
1994 ivf_write_frame_header(stream->file, pkt->
data.
frame.pts, fsize);
1998 const FileOffset currpos = ftello(stream->file);
1999 fseeko(stream->file, ivf_header_pos, SEEK_SET);
2000 ivf_write_frame_size(stream->file, fsize);
2001 fseeko(stream->file, currpos, SEEK_SET);
2011 #if CONFIG_AV1_DECODER
2012 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
2015 if (stream->decoder.err) {
2016 warn_or_exit_on_error(&stream->decoder,
2017 global->test_decode == TEST_DECODE_FATAL,
2018 "Failed to decode frame %d in stream %d",
2019 stream->frames_out + 1, stream->index);
2020 stream->mismatch_seen = stream->frames_out + 1;
2026 stream->frames_out++;
2033 if (global->show_psnr) {
2036 stream->psnr_sse_total += pkt->
data.
psnr.sse[0];
2037 stream->psnr_samples_total += pkt->
data.
psnr.samples[0];
2038 for (i = 0; i < 4; i++) {
2040 fprintf(stderr,
"%.3f ", pkt->
data.
psnr.psnr[i]);
2041 stream->psnr_totals[i] += pkt->
data.
psnr.psnr[i];
2043 stream->psnr_count++;
2052 static void show_psnr(
struct stream_state *stream,
double peak, int64_t bps) {
2056 if (!stream->psnr_count)
return;
2058 fprintf(stderr,
"Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
2059 ovpsnr = sse_to_psnr((
double)stream->psnr_samples_total, peak,
2060 (
double)stream->psnr_sse_total);
2061 fprintf(stderr,
" %.3f", ovpsnr);
2063 for (i = 0; i < 4; i++) {
2064 fprintf(stderr,
" %.3f", stream->psnr_totals[i] / stream->psnr_count);
2067 fprintf(stderr,
" %7" PRId64
" bps", bps);
2069 fprintf(stderr,
" %7" PRId64
" ms", stream->cx_time / 1000);
2070 fprintf(stderr,
"\n");
2073 static float usec_to_fps(uint64_t usec,
unsigned int frames) {
2074 return (
float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
2077 static void test_decode(
struct stream_state *stream,
2078 enum TestDecodeFatality fatal) {
2081 if (stream->mismatch_seen)
return;
2092 enc_img.
d_w, enc_img.
d_h, 16);
2093 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
2094 enc_img = enc_hbd_img;
2099 dec_img.
d_w, dec_img.
d_h, 16);
2100 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
2101 dec_img = dec_hbd_img;
2105 ctx_exit_on_error(&stream->encoder,
"Failed to get encoder reference frame");
2106 ctx_exit_on_error(&stream->decoder,
"Failed to get decoder reference frame");
2108 if (!aom_compare_img(&enc_img, &dec_img)) {
2109 int y[4], u[4], v[4];
2111 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
2113 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
2115 stream->decoder.err = 1;
2116 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
2117 "Stream %d: Encode/decode mismatch on frame %d at"
2118 " Y[%d, %d] {%d/%d},"
2119 " U[%d, %d] {%d/%d},"
2120 " V[%d, %d] {%d/%d}",
2121 stream->index, stream->frames_out, y[0], y[1], y[2],
2122 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
2123 stream->mismatch_seen = stream->frames_out;
2130 static void print_time(
const char *label, int64_t etl) {
2137 etl -= hours * 3600;
2142 fprintf(stderr,
"[%3s %2" PRId64
":%02" PRId64
":%02" PRId64
"] ", label,
2145 fprintf(stderr,
"[%3s unknown] ", label);
2149 int main(
int argc,
const char **argv_) {
2153 int allocated_raw_shift = 0;
2154 int use_16bit_internal = 0;
2155 int input_shift = 0;
2156 int frame_avail, got_data;
2158 struct AvxInputContext input;
2159 struct AvxEncoderConfig global;
2160 struct stream_state *streams = NULL;
2161 char **argv, **argi;
2162 uint64_t cx_time = 0;
2165 int profile_updated = 0;
2167 memset(&input, 0,
sizeof(input));
2168 exec_name = argv_[0];
2171 input.framerate.numerator = 30;
2172 input.framerate.denominator = 1;
2173 input.only_i420 = 1;
2174 input.bit_depth = 0;
2180 argv = argv_dup(argc - 1, argv_ + 1);
2181 parse_global_config(&global, argc, &argv);
2183 #if CONFIG_FILEOPTIONS
2184 if (argc < 2) usage_exit();
2186 if (argc < 3) usage_exit();
2189 switch (global.color_type) {
2201 struct stream_state *stream = NULL;
2204 stream = new_stream(&global, stream);
2206 if (!streams) streams = stream;
2207 }
while (parse_stream_params(&global, stream, argv));
2211 for (argi = argv; *argi; argi++)
2212 if (argi[0][0] ==
'-' && argi[0][1])
2213 die(
"Error: Unrecognized option %s\n", *argi);
2215 FOREACH_STREAM(stream, streams) {
2216 check_encoder_config(global.disable_warning_prompt, &global,
2217 &stream->config.cfg);
2220 if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
2221 die(
"only support ivf output format while large-scale-tile=1\n");
2225 input.filename = argv[0];
2227 if (!input.filename) {
2228 fprintf(stderr,
"No input file specified!\n");
2233 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
2235 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
2236 int frames_in = 0, seen_frames = 0;
2237 int64_t estimated_time_left = -1;
2238 int64_t average_rate = -1;
2239 int64_t lagged_count = 0;
2241 open_input_file(&input, global.csp);
2246 if (!input.width || !input.height) {
2247 FOREACH_STREAM(stream, streams) {
2248 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2249 input.width = stream->config.cfg.g_w;
2250 input.height = stream->config.cfg.g_h;
2257 if (!input.width || !input.height)
2259 "Specify stream dimensions with --width (-w) "
2260 " and --height (-h)");
2267 if (!input.bit_depth) {
2268 FOREACH_STREAM(stream, streams) {
2269 if (stream->config.cfg.g_input_bit_depth)
2270 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2272 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2273 (int)stream->config.cfg.g_bit_depth;
2277 FOREACH_STREAM(stream, streams) {
2278 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2282 FOREACH_STREAM(stream, streams) {
2286 switch (stream->config.cfg.g_profile) {
2290 if (!stream->config.cfg.monochrome) {
2291 stream->config.cfg.g_profile = 1;
2292 profile_updated = 1;
2296 stream->config.cfg.g_profile = 2;
2297 profile_updated = 1;
2303 stream->config.cfg.g_profile = 2;
2304 profile_updated = 1;
2305 }
else if (input.bit_depth < 12 &&
2308 stream->config.cfg.g_profile = 0;
2309 profile_updated = 1;
2315 stream->config.cfg.g_profile = 1;
2316 profile_updated = 1;
2317 }
else if (input.bit_depth < 12 &&
2320 stream->config.cfg.g_profile = 0;
2321 profile_updated = 1;
2322 }
else if (input.bit_depth == 12 &&
2323 input.file_type == FILE_TYPE_Y4M) {
2327 input.y4m.dst_c_dec_h >> 1);
2329 input.y4m.dst_c_dec_v >> 1);
2330 }
else if (input.bit_depth == 12 &&
2331 input.file_type == FILE_TYPE_RAW) {
2333 stream->chroma_subsampling_x);
2335 stream->chroma_subsampling_y);
2343 if (stream->config.cfg.g_input_bit_depth >
2344 (
unsigned int)stream->config.cfg.g_bit_depth) {
2345 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2346 if (!global.quiet) {
2348 "Warning: automatically updating bit depth to %d to "
2349 "match input format.\n",
2350 stream->config.cfg.g_input_bit_depth);
2353 if (stream->config.cfg.g_bit_depth > 10) {
2354 switch (stream->config.cfg.g_profile) {
2357 stream->config.cfg.g_profile = 2;
2358 profile_updated = 1;
2363 if (stream->config.cfg.g_bit_depth > 8) {
2364 stream->config.use_16bit_internal = 1;
2366 if (profile_updated && !global.quiet) {
2368 "Warning: automatically updating to profile %d to "
2369 "match input format.\n",
2370 stream->config.cfg.g_profile);
2373 stream->config.cfg.g_limit = global.limit;
2376 FOREACH_STREAM(stream, streams) {
2377 set_stream_dimensions(stream, input.width, input.height);
2379 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2384 if (global.pass && global.passes == 2) {
2385 FOREACH_STREAM(stream, streams) {
2386 if (!stream->config.stats_fn)
2387 die(
"Stream %d: Must specify --fpf when --pass=%d"
2388 " and --passes=2\n",
2389 stream->index, global.pass);
2394 FOREACH_STREAM(stream, streams) {
2395 if (stream->config.write_webm) {
2396 stream->config.write_webm = 0;
2397 stream->config.write_ivf = 0;
2398 warn(
"aomenc compiled w/o WebM support. Writing OBU stream.");
2406 if (!global.have_framerate) {
2407 global.framerate.num = input.framerate.numerator;
2408 global.framerate.den = input.framerate.denominator;
2410 FOREACH_STREAM(stream, streams) {
2411 stream->config.cfg.g_timebase.den = global.framerate.num;
2412 stream->config.cfg.g_timebase.num = global.framerate.den;
2415 if (global.verbose && pass == 0) {
2416 FOREACH_STREAM(stream, streams) {
2417 show_stream_config(stream, &global, &input);
2421 if (pass == (global.pass ? global.pass - 1 : 0)) {
2422 if (input.file_type == FILE_TYPE_Y4M)
2426 memset(&raw, 0,
sizeof(raw));
2428 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2430 FOREACH_STREAM(stream, streams) {
2432 init_rate_histogram(&stream->config.cfg, &global.framerate);
2436 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2437 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2438 FOREACH_STREAM(stream, streams) {
2439 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2442 if (strcmp(global.codec->name,
"av1") == 0 ||
2443 strcmp(global.codec->name,
"av1") == 0) {
2447 FOREACH_STREAM(stream, streams) {
2448 if (stream->config.use_16bit_internal) {
2449 use_16bit_internal = 1;
2451 input_shift = (int)stream->config.cfg.g_bit_depth -
2452 stream->config.cfg.g_input_bit_depth;
2459 while (frame_avail || got_data) {
2460 struct aom_usec_timer timer;
2462 if (!global.limit || frames_in < global.limit) {
2463 frame_avail = read_frame(&input, &raw);
2465 if (frame_avail) frames_in++;
2467 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2469 if (!global.quiet) {
2470 float fps = usec_to_fps(cx_time, seen_frames);
2471 fprintf(stderr,
"\rPass %d/%d ", pass + 1, global.passes);
2473 if (stream_cnt == 1)
2474 fprintf(stderr,
"frame %4d/%-4d %7" PRId64
"B ", frames_in,
2475 streams->frames_out, (int64_t)streams->nbytes);
2477 fprintf(stderr,
"frame %4d ", frames_in);
2479 fprintf(stderr,
"%7" PRId64
" %s %.2f %s ",
2480 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2481 cx_time > 9999999 ?
"ms" :
"us", fps >= 1.0 ? fps : fps * 60,
2482 fps >= 1.0 ?
"fps" :
"fpm");
2483 print_time(
"ETA", estimated_time_left);
2490 if (frames_in > global.skip_frames) {
2492 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2493 assert(use_16bit_internal);
2496 if (!allocated_raw_shift) {
2498 input.width, input.height, 32);
2499 allocated_raw_shift = 1;
2501 aom_img_upshift(&raw_shift, &raw, input_shift);
2502 frame_to_encode = &raw_shift;
2504 frame_to_encode = &raw;
2506 aom_usec_timer_start(&timer);
2507 if (use_16bit_internal) {
2509 FOREACH_STREAM(stream, streams) {
2510 if (stream->config.use_16bit_internal)
2511 encode_frame(stream, &global,
2512 frame_avail ? frame_to_encode : NULL, frames_in);
2518 FOREACH_STREAM(stream, streams) {
2519 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2523 aom_usec_timer_mark(&timer);
2524 cx_time += aom_usec_timer_elapsed(&timer);
2526 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2529 FOREACH_STREAM(stream, streams) {
2530 get_cx_data(stream, &global, &got_data);
2533 if (!got_data && input.length && streams != NULL &&
2534 !streams->frames_out) {
2535 lagged_count = global.limit ? seen_frames : ftello(input.file);
2536 }
else if (input.length) {
2541 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2543 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2544 remaining = 1000 * (global.limit - global.skip_frames -
2545 seen_frames + lagged_count);
2547 const int64_t input_pos = ftello(input.file);
2548 const int64_t input_pos_lagged = input_pos - lagged_count;
2549 const int64_t input_limit = input.length;
2551 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2552 remaining = input_limit - input_pos + lagged_count;
2556 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2557 estimated_time_left = average_rate ? remaining / average_rate : -1;
2560 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2561 FOREACH_STREAM(stream, streams) {
2562 test_decode(stream, global.test_decode);
2568 if (!global.quiet) fprintf(stderr,
"\033[K");
2571 if (stream_cnt > 1) fprintf(stderr,
"\n");
2573 if (!global.quiet) {
2574 FOREACH_STREAM(stream, streams) {
2576 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
2577 const int64_t bps = bpf * global.framerate.num / global.framerate.den;
2579 "\rPass %d/%d frame %4d/%-4d %7" PRId64
"B %7" PRId64
2582 " %7" PRId64
" %s (%.2f fps)\033[K\n",
2583 pass + 1, global.passes, frames_in, stream->frames_out,
2584 (int64_t)stream->nbytes, bpf, bps,
2585 stream->cx_time > 9999999 ? stream->cx_time / 1000
2587 stream->cx_time > 9999999 ?
"ms" :
"us",
2588 usec_to_fps(stream->cx_time, seen_frames));
2592 if (global.show_psnr) {
2593 if (global.codec->fourcc == AV1_FOURCC) {
2594 FOREACH_STREAM(stream, streams) {
2596 if (stream->psnr_count && seen_frames && global.framerate.den) {
2597 bps = (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2598 global.framerate.den / seen_frames;
2600 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
2604 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
2610 if (global.test_decode != TEST_DECODE_OFF) {
2614 close_input_file(&input);
2616 if (global.test_decode == TEST_DECODE_FATAL) {
2617 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2619 FOREACH_STREAM(stream, streams) {
2620 close_output_file(stream, global.codec->fourcc);
2623 FOREACH_STREAM(stream, streams) {
2624 stats_close(&stream->stats, global.passes - 1);
2627 if (global.pass)
break;
2630 if (global.show_q_hist_buckets) {
2631 FOREACH_STREAM(stream, streams) {
2632 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2636 if (global.show_rate_hist_buckets) {
2637 FOREACH_STREAM(stream, streams) {
2638 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2639 global.show_rate_hist_buckets);
2642 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2644 #if CONFIG_INTERNAL_STATS
2648 if (!(global.pass == 1 && global.passes == 2)) {
2649 FOREACH_STREAM(stream, streams) {
2650 FILE *f = fopen(
"opsnr.stt",
"a");
2651 if (stream->mismatch_seen) {
2652 fprintf(f,
"First mismatch occurred in frame %d\n",
2653 stream->mismatch_seen);
2655 fprintf(f,
"No mismatch detected in recon buffers\n");
2666 return res ? EXIT_FAILURE : EXIT_SUCCESS;