Improved aom_smooth_predictor_16x 32,16,8
[aom.git] / aomenc.c
blob2bd9de6601b5d6b6afc51763817994974d097c8b
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #include "./aomenc.h"
13 #include "./aom_config.h"
15 #include <assert.h>
16 #include <limits.h>
17 #include <math.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #if CONFIG_LIBYUV
24 #include "third_party/libyuv/include/libyuv/scale.h"
25 #endif
27 #include "aom/aom_encoder.h"
28 #if CONFIG_AV1_DECODER
29 #include "aom/aom_decoder.h"
30 #endif
32 #include "./args.h"
33 #include "./ivfenc.h"
34 #include "./tools_common.h"
35 #include "examples/encoder_util.h"
37 #if CONFIG_AV1_ENCODER
38 #include "aom/aomcx.h"
39 #endif
40 #if CONFIG_AV1_DECODER
41 #include "aom/aomdx.h"
42 #endif
44 #include "./aomstats.h"
45 #include "./rate_hist.h"
46 #include "./warnings.h"
47 #include "aom/aom_integer.h"
48 #include "aom_dsp/aom_dsp_common.h"
49 #include "aom_ports/aom_timer.h"
50 #include "aom_ports/mem_ops.h"
51 #if CONFIG_WEBM_IO
52 #include "./webmenc.h"
53 #endif
54 #include "./y4minput.h"
56 /* Swallow warnings about unused results of fread/fwrite */
57 static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
58 return fread(ptr, size, nmemb, stream);
60 #define fread wrap_fread
62 static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
63 FILE *stream) {
64 return fwrite(ptr, size, nmemb, stream);
66 #define fwrite wrap_fwrite
68 static const char *exec_name;
70 static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
71 const char *s, va_list ap) {
72 if (ctx->err) {
73 const char *detail = aom_codec_error_detail(ctx);
75 vfprintf(stderr, s, ap);
76 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
78 if (detail) fprintf(stderr, " %s\n", detail);
80 if (fatal) exit(EXIT_FAILURE);
84 static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
85 va_list ap;
87 va_start(ap, s);
88 warn_or_exit_on_errorv(ctx, 1, s, ap);
89 va_end(ap);
92 static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
93 const char *s, ...) {
94 va_list ap;
96 va_start(ap, s);
97 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 va_end(ap);
101 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
102 FILE *f = input_ctx->file;
103 y4m_input *y4m = &input_ctx->y4m;
104 int shortread = 0;
106 if (input_ctx->file_type == FILE_TYPE_Y4M) {
107 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
108 } else {
109 shortread = read_yuv_frame(input_ctx, img);
112 return !shortread;
115 static int file_is_y4m(const char detect[4]) {
116 if (memcmp(detect, "YUV4", 4) == 0) {
117 return 1;
119 return 0;
122 static int fourcc_is_ivf(const char detect[4]) {
123 if (memcmp(detect, "DKIF", 4) == 0) {
124 return 1;
126 return 0;
129 static const arg_def_t help =
130 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
131 static const arg_def_t debugmode =
132 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
133 static const arg_def_t outputfile =
134 ARG_DEF("o", "output", 1, "Output filename");
135 static const arg_def_t use_yv12 =
136 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
137 static const arg_def_t use_i420 =
138 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
139 static const arg_def_t use_i422 =
140 ARG_DEF(NULL, "i422", 0, "Input file is I422");
141 static const arg_def_t use_i444 =
142 ARG_DEF(NULL, "i444", 0, "Input file is I444");
143 static const arg_def_t use_i440 =
144 ARG_DEF(NULL, "i440", 0, "Input file is I440");
145 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
146 static const arg_def_t passes =
147 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
148 static const arg_def_t pass_arg =
149 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
150 static const arg_def_t fpf_name =
151 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
152 #if CONFIG_FP_MB_STATS
153 static const arg_def_t fpmbf_name =
154 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
155 #endif
156 static const arg_def_t limit =
157 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
158 static const arg_def_t skip =
159 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
160 static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
162 static const arg_def_t quietarg =
163 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
164 static const arg_def_t verbosearg =
165 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
166 static const arg_def_t psnrarg =
167 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
168 #if CONFIG_FILEOPTIONS
169 static const arg_def_t use_cfg = ARG_DEF("c", "cfg", 1, "Config file to use");
170 static const arg_def_t ext_partition =
171 ARG_DEF(NULL, "ext-partition", 1, "corresponds to extended partitions");
172 #endif
174 static const struct arg_enum_list test_decode_enum[] = {
175 { "off", TEST_DECODE_OFF },
176 { "fatal", TEST_DECODE_FATAL },
177 { "warn", TEST_DECODE_WARN },
178 { NULL, 0 }
180 static const arg_def_t recontest = ARG_DEF_ENUM(
181 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
182 static const arg_def_t framerate =
183 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
184 static const arg_def_t use_webm =
185 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
186 static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
187 #if CONFIG_OBU_NO_IVF
188 static const arg_def_t use_obu = ARG_DEF(NULL, "obu", 0, "Output OBU");
189 #endif
190 static const arg_def_t out_part =
191 ARG_DEF("P", "output-partitions", 0,
192 "Makes encoder output partitions. Requires IVF output!");
193 static const arg_def_t q_hist_n =
194 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
195 static const arg_def_t rate_hist_n =
196 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
197 static const arg_def_t disable_warnings =
198 ARG_DEF(NULL, "disable-warnings", 0,
199 "Disable warnings about potentially incorrect encode settings.");
200 static const arg_def_t disable_warning_prompt =
201 ARG_DEF("y", "disable-warning-prompt", 0,
202 "Display warnings, but do not prompt user to continue.");
203 static const struct arg_enum_list bitdepth_enum[] = {
204 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
207 static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
208 "b", "bit-depth", 1,
209 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
210 bitdepth_enum);
211 static const arg_def_t inbitdeptharg =
212 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
213 static const arg_def_t *main_args[] = { &help,
214 #if CONFIG_FILEOPTIONS
215 &use_cfg,
216 #endif
217 &debugmode,
218 &outputfile,
219 &codecarg,
220 &passes,
221 &pass_arg,
222 &fpf_name,
223 &limit,
224 &skip,
225 &good_dl,
226 &quietarg,
227 &verbosearg,
228 &psnrarg,
229 &use_webm,
230 &use_ivf,
231 #if CONFIG_OBU_NO_IVF
232 &use_obu,
233 #endif
234 &out_part,
235 &q_hist_n,
236 &rate_hist_n,
237 &disable_warnings,
238 &disable_warning_prompt,
239 &recontest,
240 NULL };
242 static const arg_def_t usage =
243 ARG_DEF("u", "usage", 1, "Usage profile number to use");
244 static const arg_def_t threads =
245 ARG_DEF("t", "threads", 1, "Max number of threads to use");
246 static const arg_def_t profile =
247 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
248 static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
249 static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
250 static const arg_def_t forced_max_frame_width = ARG_DEF(
251 NULL, "forced_max_frame_width", 0, "Maximum frame width value to force");
252 static const arg_def_t forced_max_frame_height = ARG_DEF(
253 NULL, "forced_max_frame_height", 0, "Maximum frame height value to force");
254 #if CONFIG_WEBM_IO
255 static const struct arg_enum_list stereo_mode_enum[] = {
256 { "mono", STEREO_FORMAT_MONO },
257 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
258 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
259 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
260 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
261 { NULL, 0 }
263 static const arg_def_t stereo_mode = ARG_DEF_ENUM(
264 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
265 #endif
266 static const arg_def_t timebase = ARG_DEF(
267 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
268 static const arg_def_t error_resilient =
269 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
270 static const arg_def_t lag_in_frames =
271 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
272 static const arg_def_t large_scale_tile =
273 ARG_DEF(NULL, "large-scale-tile", 1,
274 "Large scale tile coding (0: off (default), 1: on)");
275 #if CONFIG_MONO_VIDEO
276 static const arg_def_t monochrome =
277 ARG_DEF(NULL, "monochrome", 0, "Monochrome video (no chroma planes)");
278 #endif // CONFIG_MONO_VIDEO
280 static const arg_def_t *global_args[] = { &use_yv12,
281 &use_i420,
282 &use_i422,
283 &use_i444,
284 &use_i440,
285 &usage,
286 &threads,
287 &profile,
288 &width,
289 &height,
290 &forced_max_frame_width,
291 &forced_max_frame_height,
292 #if CONFIG_WEBM_IO
293 &stereo_mode,
294 #endif
295 &timebase,
296 &framerate,
297 &error_resilient,
298 &bitdeptharg,
299 &lag_in_frames,
300 &large_scale_tile,
301 #if CONFIG_MONO_VIDEO
302 &monochrome,
303 #endif // CONFIG_MONO_VIDEO
304 NULL };
306 static const arg_def_t dropframe_thresh =
307 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
308 static const arg_def_t resize_mode =
309 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
310 static const arg_def_t resize_denominator =
311 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
312 static const arg_def_t resize_kf_denominator = ARG_DEF(
313 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
314 static const arg_def_t superres_mode =
315 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
316 static const arg_def_t superres_denominator = ARG_DEF(
317 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
318 static const arg_def_t superres_kf_denominator =
319 ARG_DEF(NULL, "superres-kf-denominator", 1,
320 "Frame super-resolution keyframe denominator");
321 static const arg_def_t superres_qthresh = ARG_DEF(
322 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
323 static const arg_def_t superres_kf_qthresh =
324 ARG_DEF(NULL, "superres-kf-qthresh", 1,
325 "Frame super-resolution keyframe qindex threshold");
326 static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
327 { "cbr", AOM_CBR },
328 { "cq", AOM_CQ },
329 { "q", AOM_Q },
330 { NULL, 0 } };
331 static const arg_def_t end_usage =
332 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
333 static const arg_def_t target_bitrate =
334 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
335 static const arg_def_t min_quantizer =
336 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
337 static const arg_def_t max_quantizer =
338 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
339 static const arg_def_t undershoot_pct =
340 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
341 static const arg_def_t overshoot_pct =
342 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
343 static const arg_def_t buf_sz =
344 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
345 static const arg_def_t buf_initial_sz =
346 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
347 static const arg_def_t buf_optimal_sz =
348 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
349 static const arg_def_t *rc_args[] = { &dropframe_thresh,
350 &resize_mode,
351 &resize_denominator,
352 &resize_kf_denominator,
353 &superres_mode,
354 &superres_denominator,
355 &superres_kf_denominator,
356 &superres_qthresh,
357 &superres_kf_qthresh,
358 &end_usage,
359 &target_bitrate,
360 &min_quantizer,
361 &max_quantizer,
362 &undershoot_pct,
363 &overshoot_pct,
364 &buf_sz,
365 &buf_initial_sz,
366 &buf_optimal_sz,
367 NULL };
369 static const arg_def_t bias_pct =
370 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
371 static const arg_def_t minsection_pct =
372 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
373 static const arg_def_t maxsection_pct =
374 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
375 static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
376 &maxsection_pct, NULL };
378 static const arg_def_t kf_min_dist =
379 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
380 static const arg_def_t kf_max_dist =
381 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
382 static const arg_def_t kf_disabled =
383 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
384 static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
385 NULL };
386 static const arg_def_t sframe_dist =
387 ARG_DEF(NULL, "sframe-dist", 1, "S-Frame interval (frames)");
388 static const arg_def_t sframe_mode =
389 ARG_DEF(NULL, "sframe-mode", 1, "S-Frame insertion mode (1..2)");
390 static const arg_def_t noise_sens =
391 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
392 static const arg_def_t sharpness =
393 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
394 static const arg_def_t static_thresh =
395 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
396 static const arg_def_t auto_altref =
397 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
398 static const arg_def_t arnr_maxframes =
399 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
400 static const arg_def_t arnr_strength =
401 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
402 static const struct arg_enum_list tuning_enum[] = {
403 { "psnr", AOM_TUNE_PSNR },
404 { "ssim", AOM_TUNE_SSIM },
405 #ifdef CONFIG_DIST_8X8
406 { "cdef-dist", AOM_TUNE_CDEF_DIST },
407 { "daala-dist", AOM_TUNE_DAALA_DIST },
408 #endif
409 { NULL, 0 }
411 static const arg_def_t tune_metric =
412 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
413 static const arg_def_t cq_level =
414 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
415 static const arg_def_t max_intra_rate_pct =
416 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
418 #if CONFIG_AV1_ENCODER
419 static const arg_def_t cpu_used_av1 =
420 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
421 static const arg_def_t dev_sf_av1 =
422 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
423 static const arg_def_t single_tile_decoding =
424 ARG_DEF(NULL, "single-tile-decoding", 1,
425 "Single tile decoding (0: off (default), 1: on)");
426 static const arg_def_t tile_cols =
427 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
428 static const arg_def_t tile_rows =
429 ARG_DEF(NULL, "tile-rows", 1,
430 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
431 static const arg_def_t tile_width =
432 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
433 static const arg_def_t tile_height =
434 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
435 static const arg_def_t lossless =
436 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
437 static const arg_def_t enable_cdef =
438 ARG_DEF(NULL, "enable-cdef", 1,
439 "Enable the constrained directional enhancement filter (0: false, "
440 "1: true (default))");
441 static const arg_def_t enable_restoration =
442 ARG_DEF(NULL, "enable-restoration", 1,
443 "Enable the loop restoration filter (0: false, "
444 "1: true (default))");
445 static const arg_def_t enable_qm =
446 ARG_DEF(NULL, "enable-qm", 1,
447 "Enable quantisation matrices (0: false (default), 1: true)");
448 static const arg_def_t qm_min = ARG_DEF(
449 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
450 static const arg_def_t qm_max = ARG_DEF(
451 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 15");
452 #if CONFIG_DIST_8X8
453 static const arg_def_t enable_dist_8x8 =
454 ARG_DEF(NULL, "enable-dist-8x8", 1,
455 "Enable dist-8x8 (0: false (default), 1: true)");
456 #endif // CONFIG_DIST_8X8
457 static const arg_def_t num_tg = ARG_DEF(
458 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
459 static const arg_def_t mtu_size =
460 ARG_DEF(NULL, "mtu-size", 1,
461 "MTU size for a tile group, default is 0 (no MTU targeting), "
462 "overrides maximum number of tile groups");
463 static const struct arg_enum_list timing_info_enum[] = {
464 { "unspecified", AOM_TIMING_UNSPECIFIED },
465 { "constant", AOM_TIMING_EQUAL },
466 { NULL, 0 }
468 static const arg_def_t timing_info =
469 ARG_DEF_ENUM(NULL, "timing-info", 1,
470 "Signal timing info in the bitstream:", timing_info_enum);
471 #if CONFIG_FILM_GRAIN
472 static const arg_def_t film_grain_test =
473 ARG_DEF(NULL, "film-grain-test", 1,
474 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
475 "... 16: test-16)");
476 #endif
477 static const arg_def_t disable_tempmv = ARG_DEF(
478 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
479 static const arg_def_t frame_parallel_decoding =
480 ARG_DEF(NULL, "frame-parallel", 1,
481 "Enable frame parallel decodability features "
482 "(0: false (default), 1: true)");
483 #if !CONFIG_EXT_DELTA_Q
484 static const arg_def_t aq_mode = ARG_DEF(
485 NULL, "aq-mode", 1,
486 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
487 "3: cyclic refresh, 4: delta quant)");
488 #else
489 static const arg_def_t aq_mode = ARG_DEF(
490 NULL, "aq-mode", 1,
491 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
492 "3: cyclic refresh)");
493 #endif
494 #if CONFIG_EXT_DELTA_Q
495 static const arg_def_t deltaq_mode = ARG_DEF(
496 NULL, "deltaq-mode", 1,
497 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
498 #endif
499 static const arg_def_t frame_periodic_boost =
500 ARG_DEF(NULL, "frame-boost", 1,
501 "Enable frame periodic boost (0: off (default), 1: on)");
502 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
503 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
504 static const arg_def_t max_inter_rate_pct =
505 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
506 static const arg_def_t min_gf_interval = ARG_DEF(
507 NULL, "min-gf-interval", 1,
508 "min gf/arf frame interval (default 0, indicating in-built behavior)");
509 static const arg_def_t max_gf_interval = ARG_DEF(
510 NULL, "max-gf-interval", 1,
511 "max gf/arf frame interval (default 0, indicating in-built behavior)");
513 static const struct arg_enum_list color_primaries_enum[] = {
514 { "bt709", AOM_CICP_CP_BT_709 },
515 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
516 { "bt601", AOM_CICP_CP_BT_601 },
517 { "bt470m", AOM_CICP_CP_BT_470_M },
518 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
519 { "smpte240", AOM_CICP_CP_SMPTE_240 },
520 { "film", AOM_CICP_CP_GENERIC_FILM },
521 { "bt2020", AOM_CICP_CP_BT_2020 },
522 { "xyz", AOM_CICP_CP_XYZ },
523 { "smpte431", AOM_CICP_CP_SMPTE_431 },
524 { "smpte432", AOM_CICP_CP_SMPTE_432 },
525 { "ebu3213", AOM_CICP_CP_EBU_3213 },
526 { NULL, 0 }
529 static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
530 NULL, "color-primaries", 1,
531 "Color primaries (CICP) of input content:", color_primaries_enum);
533 static const struct arg_enum_list transfer_characteristics_enum[] = {
534 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
535 { "bt709", AOM_CICP_TC_BT_709 },
536 { "bt470m", AOM_CICP_TC_BT_470_M },
537 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
538 { "bt601", AOM_CICP_TC_BT_601 },
539 { "smpte240", AOM_CICP_TC_SMPTE_240 },
540 { "lin", AOM_CICP_TC_LINEAR },
541 { "log100", AOM_CICP_TC_LOG_100 },
542 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
543 { "iec61966", AOM_CICP_TC_IEC_61966 },
544 { "bt1361", AOM_CICP_TC_BT_1361 },
545 { "srgb", AOM_CICP_TC_SRGB },
546 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
547 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
548 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
549 { "hlg", AOM_CICP_TC_HLG },
550 { "smpte428", AOM_CICP_TC_SMPTE_428 },
551 { NULL, 0 }
554 static const arg_def_t input_transfer_characteristics =
555 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
556 "Transfer characteristics (CICP) of input content:",
557 transfer_characteristics_enum);
559 static const struct arg_enum_list matrix_coefficients_enum[] = {
560 { "identity", AOM_CICP_MC_IDENTITY },
561 { "bt709", AOM_CICP_MC_BT_709 },
562 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
563 { "fcc73", AOM_CICP_MC_FCC },
564 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
565 { "bt601", AOM_CICP_MC_BT_601 },
566 { "smpte240", AOM_CICP_CP_SMPTE_240 },
567 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
568 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
569 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
570 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
571 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
572 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
573 { "ictcp", AOM_CICP_MC_ICTCP },
574 { NULL, 0 }
577 static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
578 NULL, "matrix-coefficients", 1,
579 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
581 static const struct arg_enum_list chroma_sample_position_enum[] = {
582 { "unknown", AOM_CSP_UNKNOWN },
583 { "vertical", AOM_CSP_VERTICAL },
584 { "colocated", AOM_CSP_COLOCATED },
585 { NULL, 0 }
588 static const arg_def_t input_chroma_sample_position =
589 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
590 "The chroma sample position when chroma 4:2:0 is signaled:",
591 chroma_sample_position_enum);
593 static const struct arg_enum_list tune_content_enum[] = {
594 { "default", AOM_CONTENT_DEFAULT },
595 { "screen", AOM_CONTENT_SCREEN },
596 { NULL, 0 }
599 static const arg_def_t tune_content = ARG_DEF_ENUM(
600 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
602 #if CONFIG_CDF_UPDATE_MODE
603 static const arg_def_t cdf_update_mode =
604 ARG_DEF(NULL, "cdf-update-mode", 1,
605 "CDF update mode for entropy coding "
606 "(0: no CDF update; 1: update CDF on all frames(default); "
607 "2: selectively update CDF on some frames");
608 #endif // CONFIG_CDF_UPDATE_MODE
610 static const struct arg_enum_list superblock_size_enum[] = {
611 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
612 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
613 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
614 { NULL, 0 }
616 static const arg_def_t superblock_size = ARG_DEF_ENUM(
617 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
619 static const arg_def_t *av1_args[] = { &cpu_used_av1,
620 &dev_sf_av1,
621 &auto_altref,
622 &sharpness,
623 &static_thresh,
624 &single_tile_decoding,
625 &tile_cols,
626 &tile_rows,
627 &arnr_maxframes,
628 &arnr_strength,
629 &tune_metric,
630 &cq_level,
631 &max_intra_rate_pct,
632 &max_inter_rate_pct,
633 &gf_cbr_boost_pct,
634 &lossless,
635 &enable_cdef,
636 &enable_restoration,
637 &enable_qm,
638 &qm_min,
639 &qm_max,
640 #if CONFIG_DIST_8X8
641 &enable_dist_8x8,
642 #endif
643 &frame_parallel_decoding,
644 &aq_mode,
645 #if CONFIG_EXT_DELTA_Q
646 &deltaq_mode,
647 #endif
648 &frame_periodic_boost,
649 &noise_sens,
650 &tune_content,
651 #if CONFIG_CDF_UPDATE_MODE
652 &cdf_update_mode,
653 #endif // CONFIG_CDF_UPDATE_MODE
654 &input_color_primaries,
655 &input_transfer_characteristics,
656 &input_matrix_coefficients,
657 &input_chroma_sample_position,
658 &min_gf_interval,
659 &max_gf_interval,
660 &superblock_size,
661 &num_tg,
662 &mtu_size,
663 &timing_info,
664 #if CONFIG_FILM_GRAIN
665 &film_grain_test,
666 #endif
667 &disable_tempmv,
668 &bitdeptharg,
669 &inbitdeptharg,
670 &sframe_dist,
671 &sframe_mode,
672 NULL };
673 static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
674 AOME_SET_DEVSF,
675 AOME_SET_ENABLEAUTOALTREF,
676 AOME_SET_SHARPNESS,
677 AOME_SET_STATIC_THRESHOLD,
678 AV1E_SET_SINGLE_TILE_DECODING,
679 AV1E_SET_TILE_COLUMNS,
680 AV1E_SET_TILE_ROWS,
681 AOME_SET_ARNR_MAXFRAMES,
682 AOME_SET_ARNR_STRENGTH,
683 AOME_SET_TUNING,
684 AOME_SET_CQ_LEVEL,
685 AOME_SET_MAX_INTRA_BITRATE_PCT,
686 AV1E_SET_MAX_INTER_BITRATE_PCT,
687 AV1E_SET_GF_CBR_BOOST_PCT,
688 AV1E_SET_LOSSLESS,
689 AV1E_SET_ENABLE_CDEF,
690 AV1E_SET_ENABLE_RESTORATION,
691 AV1E_SET_ENABLE_QM,
692 AV1E_SET_QM_MIN,
693 AV1E_SET_QM_MAX,
694 #if CONFIG_DIST_8X8
695 AV1E_SET_ENABLE_DIST_8X8,
696 #endif
697 AV1E_SET_FRAME_PARALLEL_DECODING,
698 AV1E_SET_AQ_MODE,
699 #if CONFIG_EXT_DELTA_Q
700 AV1E_SET_DELTAQ_MODE,
701 #endif
702 AV1E_SET_FRAME_PERIODIC_BOOST,
703 AV1E_SET_NOISE_SENSITIVITY,
704 AV1E_SET_TUNE_CONTENT,
705 #if CONFIG_CDF_UPDATE_MODE
706 AV1E_SET_CDF_UPDATE_MODE,
707 #endif // CONFIG_CDF_UPDATE_MODE
708 AV1E_SET_COLOR_PRIMARIES,
709 AV1E_SET_TRANSFER_CHARACTERISTICS,
710 AV1E_SET_MATRIX_COEFFICIENTS,
711 AV1E_SET_CHROMA_SAMPLE_POSITION,
712 AV1E_SET_MIN_GF_INTERVAL,
713 AV1E_SET_MAX_GF_INTERVAL,
714 AV1E_SET_SUPERBLOCK_SIZE,
715 AV1E_SET_NUM_TG,
716 AV1E_SET_MTU,
717 AV1E_SET_TIMING_INFO,
718 #if CONFIG_FILM_GRAIN
719 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
720 #endif
721 AV1E_SET_DISABLE_TEMPMV,
722 AV1E_SET_ENABLE_DF,
723 AV1E_SET_ENABLE_ORDER_HINT,
724 AV1E_SET_ENABLE_JNT_COMP,
725 AV1E_SET_ENABLE_SUPERRES,
726 0 };
727 #endif // CONFIG_AV1_ENCODER
729 static const arg_def_t *no_args[] = { NULL };
731 void show_help(FILE *fout, int shorthelp) {
732 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
733 exec_name);
735 if (shorthelp) {
736 fprintf(fout, "Use --help to see the full list of options.\n");
737 return;
740 fprintf(fout, "\nOptions:\n");
741 arg_show_usage(fout, main_args);
742 fprintf(fout, "\nEncoder Global Options:\n");
743 arg_show_usage(fout, global_args);
744 fprintf(fout, "\nRate Control Options:\n");
745 arg_show_usage(fout, rc_args);
746 fprintf(fout, "\nTwopass Rate Control Options:\n");
747 arg_show_usage(fout, rc_twopass_args);
748 fprintf(fout, "\nKeyframe Placement Options:\n");
749 arg_show_usage(fout, kf_args);
750 #if CONFIG_AV1_ENCODER
751 fprintf(fout, "\nAV1 Specific Options:\n");
752 arg_show_usage(fout, av1_args);
753 #endif
754 fprintf(fout,
755 "\nStream timebase (--timebase):\n"
756 " The desired precision of timestamps in the output, expressed\n"
757 " in fractional seconds. Default is 1/1000.\n");
758 fprintf(fout, "\nIncluded encoders:\n\n");
760 const int num_encoder = get_aom_encoder_count();
761 for (int i = 0; i < num_encoder; ++i) {
762 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
763 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
764 fprintf(fout, " %-6s - %s %s\n", encoder->name,
765 aom_codec_iface_name(encoder->codec_interface()), defstr);
767 fprintf(fout, "\n ");
768 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
771 void usage_exit(void) {
772 show_help(stderr, 1);
773 exit(EXIT_FAILURE);
776 #if CONFIG_AV1_ENCODER
777 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
778 #endif
780 #if !CONFIG_WEBM_IO
781 typedef int stereo_format_t;
782 struct WebmOutputContext {
783 int debug;
785 #endif
787 /* Per-stream configuration */
788 struct stream_config {
789 struct aom_codec_enc_cfg cfg;
790 const char *out_fn;
791 const char *stats_fn;
792 #if CONFIG_FP_MB_STATS
793 const char *fpmb_stats_fn;
794 #endif
795 stereo_format_t stereo_fmt;
796 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
797 int arg_ctrl_cnt;
798 int write_webm;
799 #if CONFIG_OBU_NO_IVF
800 int write_ivf;
801 #endif
802 // whether to use 16bit internal buffers
803 int use_16bit_internal;
806 struct stream_state {
807 int index;
808 struct stream_state *next;
809 struct stream_config config;
810 FILE *file;
811 struct rate_hist *rate_hist;
812 struct WebmOutputContext webm_ctx;
813 uint64_t psnr_sse_total;
814 uint64_t psnr_samples_total;
815 double psnr_totals[4];
816 int psnr_count;
817 int counts[64];
818 aom_codec_ctx_t encoder;
819 unsigned int frames_out;
820 uint64_t cx_time;
821 size_t nbytes;
822 stats_io_t stats;
823 #if CONFIG_FP_MB_STATS
824 stats_io_t fpmb_stats;
825 #endif
826 struct aom_image *img;
827 aom_codec_ctx_t decoder;
828 int mismatch_seen;
831 static void validate_positive_rational(const char *msg,
832 struct aom_rational *rat) {
833 if (rat->den < 0) {
834 rat->num *= -1;
835 rat->den *= -1;
838 if (rat->num < 0) die("Error: %s must be positive\n", msg);
840 if (!rat->den) die("Error: %s has zero denominator\n", msg);
843 static void parse_global_config(struct AvxEncoderConfig *global, int *argc,
844 char ***argv) {
845 char **argi, **argj;
846 struct arg arg;
847 const int num_encoder = get_aom_encoder_count();
848 char **argv_local = (char **)*argv;
849 #if CONFIG_FILEOPTIONS
850 int argc_local = *argc;
851 #endif
852 if (num_encoder < 1) die("Error: no valid encoder available\n");
854 /* Initialize default parameters */
855 memset(global, 0, sizeof(*global));
856 global->codec = get_aom_encoder_by_index(num_encoder - 1);
857 global->passes = 0;
858 global->color_type = I420;
860 #if CONFIG_FILEOPTIONS
861 const char *cfg = NULL;
862 int cfg_included = 0;
863 #endif
864 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
865 arg.argv_step = 1;
867 #if CONFIG_FILEOPTIONS
868 if (arg_match(&arg, &use_cfg, argi)) {
869 if (cfg_included) continue;
870 cfg = arg.val;
872 arg_cfg(&argc_local, &argv_local, cfg);
874 *argj = *argi = *argv_local;
875 argj = argi = argv_local;
876 *argv = argv_local;
877 cfg_included = 1;
878 continue;
880 #endif
881 if (arg_match(&arg, &help, argi)) {
882 show_help(stdout, 0);
883 exit(EXIT_SUCCESS);
884 } else if (arg_match(&arg, &codecarg, argi)) {
885 global->codec = get_aom_encoder_by_name(arg.val);
886 if (!global->codec)
887 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
888 } else if (arg_match(&arg, &passes, argi)) {
889 global->passes = arg_parse_uint(&arg);
891 if (global->passes < 1 || global->passes > 2)
892 die("Error: Invalid number of passes (%d)\n", global->passes);
893 } else if (arg_match(&arg, &pass_arg, argi)) {
894 global->pass = arg_parse_uint(&arg);
896 if (global->pass < 1 || global->pass > 2)
897 die("Error: Invalid pass selected (%d)\n", global->pass);
898 } else if (arg_match(&arg, &usage, argi))
899 global->usage = arg_parse_uint(&arg);
900 else if (arg_match(&arg, &good_dl, argi))
901 warn("Deprecated --good option! Ignoring\n");
902 else if (arg_match(&arg, &use_yv12, argi))
903 global->color_type = YV12;
904 else if (arg_match(&arg, &use_i420, argi))
905 global->color_type = I420;
906 else if (arg_match(&arg, &use_i422, argi))
907 global->color_type = I422;
908 else if (arg_match(&arg, &use_i444, argi))
909 global->color_type = I444;
910 else if (arg_match(&arg, &use_i440, argi))
911 global->color_type = I440;
912 else if (arg_match(&arg, &quietarg, argi))
913 global->quiet = 1;
914 else if (arg_match(&arg, &verbosearg, argi))
915 global->verbose = 1;
916 else if (arg_match(&arg, &limit, argi))
917 global->limit = arg_parse_uint(&arg);
918 else if (arg_match(&arg, &skip, argi))
919 global->skip_frames = arg_parse_uint(&arg);
920 else if (arg_match(&arg, &psnrarg, argi))
921 global->show_psnr = 1;
922 else if (arg_match(&arg, &recontest, argi))
923 global->test_decode = arg_parse_enum_or_int(&arg);
924 else if (arg_match(&arg, &framerate, argi)) {
925 global->framerate = arg_parse_rational(&arg);
926 validate_positive_rational(arg.name, &global->framerate);
927 global->have_framerate = 1;
928 } else if (arg_match(&arg, &out_part, argi))
929 global->out_part = 1;
930 else if (arg_match(&arg, &debugmode, argi))
931 global->debug = 1;
932 else if (arg_match(&arg, &q_hist_n, argi))
933 global->show_q_hist_buckets = arg_parse_uint(&arg);
934 else if (arg_match(&arg, &rate_hist_n, argi))
935 global->show_rate_hist_buckets = arg_parse_uint(&arg);
936 else if (arg_match(&arg, &disable_warnings, argi))
937 global->disable_warnings = 1;
938 else if (arg_match(&arg, &disable_warning_prompt, argi))
939 global->disable_warning_prompt = 1;
940 else
941 argj++;
944 if (global->pass) {
945 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
946 if (global->pass > global->passes) {
947 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
948 global->pass);
949 global->passes = global->pass;
952 /* Validate global config */
953 if (global->passes == 0) {
954 #if CONFIG_AV1_ENCODER
955 // Make default AV1 passes = 2 until there is a better quality 1-pass
956 // encoder
957 if (global->codec != NULL && global->codec->name != NULL)
958 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
959 #else
960 global->passes = 1;
961 #endif
965 static void open_input_file(struct AvxInputContext *input) {
966 /* Parse certain options from the input file, if possible */
967 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
968 : set_binary_mode(stdin);
970 if (!input->file) fatal("Failed to open input file");
972 if (!fseeko(input->file, 0, SEEK_END)) {
973 /* Input file is seekable. Figure out how long it is, so we can get
974 * progress info.
976 input->length = ftello(input->file);
977 rewind(input->file);
980 /* Default to 1:1 pixel aspect ratio. */
981 input->pixel_aspect_ratio.numerator = 1;
982 input->pixel_aspect_ratio.denominator = 1;
984 /* For RAW input sources, these bytes will applied on the first frame
985 * in read_frame().
987 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
988 input->detect.position = 0;
990 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
991 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
992 input->only_i420) >= 0) {
993 input->file_type = FILE_TYPE_Y4M;
994 input->width = input->y4m.pic_w;
995 input->height = input->y4m.pic_h;
996 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
997 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
998 input->framerate.numerator = input->y4m.fps_n;
999 input->framerate.denominator = input->y4m.fps_d;
1000 input->fmt = input->y4m.aom_fmt;
1001 input->bit_depth = input->y4m.bit_depth;
1002 } else
1003 fatal("Unsupported Y4M stream.");
1004 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
1005 fatal("IVF is not supported as input.");
1006 } else {
1007 input->file_type = FILE_TYPE_RAW;
1011 static void close_input_file(struct AvxInputContext *input) {
1012 fclose(input->file);
1013 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
1016 static struct stream_state *new_stream(struct AvxEncoderConfig *global,
1017 struct stream_state *prev) {
1018 struct stream_state *stream;
1020 stream = calloc(1, sizeof(*stream));
1021 if (stream == NULL) {
1022 fatal("Failed to allocate new stream.");
1025 if (prev) {
1026 memcpy(stream, prev, sizeof(*stream));
1027 stream->index++;
1028 prev->next = stream;
1029 } else {
1030 aom_codec_err_t res;
1032 /* Populate encoder configuration */
1033 res = aom_codec_enc_config_default(global->codec->codec_interface(),
1034 &stream->config.cfg, global->usage);
1035 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
1037 /* Change the default timebase to a high enough value so that the
1038 * encoder will always create strictly increasing timestamps.
1040 stream->config.cfg.g_timebase.den = 1000;
1042 /* Never use the library's default resolution, require it be parsed
1043 * from the file or set on the command line.
1045 stream->config.cfg.g_w = 0;
1046 stream->config.cfg.g_h = 0;
1048 /* Initialize remaining stream parameters */
1049 stream->config.write_webm = 1;
1050 #if CONFIG_OBU_NO_IVF
1051 stream->config.write_ivf = 0;
1052 #endif
1053 #if CONFIG_WEBM_IO
1054 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1055 stream->webm_ctx.last_pts_ns = -1;
1056 stream->webm_ctx.writer = NULL;
1057 stream->webm_ctx.segment = NULL;
1058 #endif
1060 /* Allows removal of the application version from the EBML tags */
1061 stream->webm_ctx.debug = global->debug;
1064 /* Output files must be specified for each stream */
1065 stream->config.out_fn = NULL;
1067 stream->next = NULL;
1068 return stream;
1071 static void set_config_arg_ctrls(struct stream_config *config, int key,
1072 const struct arg *arg) {
1073 int j;
1074 /* Point either to the next free element or the first instance of this
1075 * control.
1077 for (j = 0; j < config->arg_ctrl_cnt; j++)
1078 if (config->arg_ctrls[j][0] == key) break;
1080 /* Update/insert */
1081 assert(j < (int)ARG_CTRL_CNT_MAX);
1082 config->arg_ctrls[j][0] = key;
1083 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1084 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1087 static int parse_stream_params(struct AvxEncoderConfig *global,
1088 struct stream_state *stream, char **argv) {
1089 char **argi, **argj;
1090 struct arg arg;
1091 static const arg_def_t **ctrl_args = no_args;
1092 static const int *ctrl_args_map = NULL;
1093 struct stream_config *config = &stream->config;
1094 int eos_mark_found = 0;
1095 int webm_forced = 0;
1097 // Handle codec specific options
1098 if (0) {
1099 #if CONFIG_AV1_ENCODER
1100 } else if (strcmp(global->codec->name, "av1") == 0) {
1101 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1102 // Consider to expand this set for AV1 encoder control.
1103 ctrl_args = av1_args;
1104 ctrl_args_map = av1_arg_ctrl_map;
1105 #endif
1108 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1109 arg.argv_step = 1;
1111 /* Once we've found an end-of-stream marker (--) we want to continue
1112 * shifting arguments but not consuming them.
1114 if (eos_mark_found) {
1115 argj++;
1116 continue;
1117 } else if (!strcmp(*argj, "--")) {
1118 eos_mark_found = 1;
1119 continue;
1122 if (arg_match(&arg, &outputfile, argi)) {
1123 config->out_fn = arg.val;
1124 if (!webm_forced) {
1125 const size_t out_fn_len = strlen(config->out_fn);
1126 if (out_fn_len >= 4 &&
1127 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1128 config->write_webm = 0;
1129 #if CONFIG_OBU_NO_IVF
1130 config->write_ivf = 1;
1131 #endif
1133 #if CONFIG_OBU_NO_IVF
1134 else if (out_fn_len >= 4 &&
1135 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1136 config->write_webm = 0;
1137 config->write_ivf = 0;
1139 #endif
1141 } else if (arg_match(&arg, &fpf_name, argi)) {
1142 config->stats_fn = arg.val;
1143 #if CONFIG_FP_MB_STATS
1144 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1145 config->fpmb_stats_fn = arg.val;
1146 #endif
1147 } else if (arg_match(&arg, &use_webm, argi)) {
1148 #if CONFIG_WEBM_IO
1149 config->write_webm = 1;
1150 webm_forced = 1;
1151 #else
1152 die("Error: --webm specified but webm is disabled.");
1153 #endif
1154 } else if (arg_match(&arg, &use_ivf, argi)) {
1155 config->write_webm = 0;
1156 #if CONFIG_OBU_NO_IVF
1157 config->write_ivf = 1;
1158 #endif
1159 #if CONFIG_OBU_NO_IVF
1160 } else if (arg_match(&arg, &use_obu, argi)) {
1161 config->write_webm = 0;
1162 config->write_ivf = 0;
1163 #endif
1164 } else if (arg_match(&arg, &threads, argi)) {
1165 config->cfg.g_threads = arg_parse_uint(&arg);
1166 } else if (arg_match(&arg, &profile, argi)) {
1167 config->cfg.g_profile = arg_parse_uint(&arg);
1168 } else if (arg_match(&arg, &width, argi)) {
1169 config->cfg.g_w = arg_parse_uint(&arg);
1170 } else if (arg_match(&arg, &height, argi)) {
1171 config->cfg.g_h = arg_parse_uint(&arg);
1172 } else if (arg_match(&arg, &forced_max_frame_width, argi)) {
1173 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1174 } else if (arg_match(&arg, &forced_max_frame_height, argi)) {
1175 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
1176 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1177 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1178 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1179 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1180 #if CONFIG_WEBM_IO
1181 } else if (arg_match(&arg, &stereo_mode, argi)) {
1182 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1183 #endif
1184 } else if (arg_match(&arg, &timebase, argi)) {
1185 config->cfg.g_timebase = arg_parse_rational(&arg);
1186 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1187 } else if (arg_match(&arg, &error_resilient, argi)) {
1188 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1189 } else if (arg_match(&arg, &lag_in_frames, argi)) {
1190 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1191 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1192 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1193 #if CONFIG_MONO_VIDEO
1194 } else if (arg_match(&arg, &monochrome, argi)) {
1195 config->cfg.monochrome = 1;
1196 #endif // CONFIG_MONO_VIDEO
1197 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
1198 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1199 } else if (arg_match(&arg, &resize_mode, argi)) {
1200 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1201 } else if (arg_match(&arg, &resize_denominator, argi)) {
1202 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1203 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1204 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1205 } else if (arg_match(&arg, &superres_mode, argi)) {
1206 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1207 } else if (arg_match(&arg, &superres_denominator, argi)) {
1208 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1209 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1210 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1211 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1212 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1213 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1214 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1215 } else if (arg_match(&arg, &end_usage, argi)) {
1216 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1217 } else if (arg_match(&arg, &target_bitrate, argi)) {
1218 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1219 } else if (arg_match(&arg, &min_quantizer, argi)) {
1220 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1221 } else if (arg_match(&arg, &max_quantizer, argi)) {
1222 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1223 } else if (arg_match(&arg, &undershoot_pct, argi)) {
1224 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1225 } else if (arg_match(&arg, &overshoot_pct, argi)) {
1226 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1227 } else if (arg_match(&arg, &buf_sz, argi)) {
1228 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1229 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
1230 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1231 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1232 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1233 } else if (arg_match(&arg, &bias_pct, argi)) {
1234 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1235 if (global->passes < 2)
1236 warn("option %s ignored in one-pass mode.\n", arg.name);
1237 } else if (arg_match(&arg, &minsection_pct, argi)) {
1238 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1240 if (global->passes < 2)
1241 warn("option %s ignored in one-pass mode.\n", arg.name);
1242 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1243 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1245 if (global->passes < 2)
1246 warn("option %s ignored in one-pass mode.\n", arg.name);
1247 } else if (arg_match(&arg, &kf_min_dist, argi)) {
1248 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1249 } else if (arg_match(&arg, &kf_max_dist, argi)) {
1250 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1251 } else if (arg_match(&arg, &kf_disabled, argi)) {
1252 config->cfg.kf_mode = AOM_KF_DISABLED;
1253 } else if (arg_match(&arg, &sframe_dist, argi)) {
1254 config->cfg.sframe_dist = arg_parse_uint(&arg);
1255 } else if (arg_match(&arg, &sframe_mode, argi)) {
1256 config->cfg.sframe_mode = arg_parse_uint(&arg);
1257 } else if (arg_match(&arg, &tile_width, argi)) {
1258 config->cfg.tile_width_count =
1259 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1260 } else if (arg_match(&arg, &tile_height, argi)) {
1261 config->cfg.tile_height_count =
1262 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1263 #if CONFIG_FILEOPTIONS
1264 } else if (arg_match(&arg, &ext_partition, argi)) {
1265 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1266 #endif
1267 } else {
1268 int i, match = 0;
1269 for (i = 0; ctrl_args[i]; i++) {
1270 if (arg_match(&arg, ctrl_args[i], argi)) {
1271 match = 1;
1272 if (ctrl_args_map) {
1273 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1277 if (!match) argj++;
1280 config->use_16bit_internal =
1281 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
1282 return eos_mark_found;
1285 #define FOREACH_STREAM(iterator, list) \
1286 for (struct stream_state *iterator = list; iterator; \
1287 iterator = iterator->next)
1289 static void validate_stream_config(const struct stream_state *stream,
1290 const struct AvxEncoderConfig *global) {
1291 const struct stream_state *streami;
1292 (void)global;
1294 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1295 fatal(
1296 "Stream %d: Specify stream dimensions with --width (-w) "
1297 " and --height (-h)",
1298 stream->index);
1300 // Check that the codec bit depth is greater than the input bit depth.
1301 if (stream->config.cfg.g_input_bit_depth >
1302 (unsigned int)stream->config.cfg.g_bit_depth) {
1303 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1304 stream->index, (int)stream->config.cfg.g_bit_depth,
1305 stream->config.cfg.g_input_bit_depth);
1308 for (streami = stream; streami; streami = streami->next) {
1309 /* All streams require output files */
1310 if (!streami->config.out_fn)
1311 fatal("Stream %d: Output file is required (specify with -o)",
1312 streami->index);
1314 /* Check for two streams outputting to the same file */
1315 if (streami != stream) {
1316 const char *a = stream->config.out_fn;
1317 const char *b = streami->config.out_fn;
1318 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1319 fatal("Stream %d: duplicate output file (from stream %d)",
1320 streami->index, stream->index);
1323 /* Check for two streams sharing a stats file. */
1324 if (streami != stream) {
1325 const char *a = stream->config.stats_fn;
1326 const char *b = streami->config.stats_fn;
1327 if (a && b && !strcmp(a, b))
1328 fatal("Stream %d: duplicate stats file (from stream %d)",
1329 streami->index, stream->index);
1332 #if CONFIG_FP_MB_STATS
1333 /* Check for two streams sharing a mb stats file. */
1334 if (streami != stream) {
1335 const char *a = stream->config.fpmb_stats_fn;
1336 const char *b = streami->config.fpmb_stats_fn;
1337 if (a && b && !strcmp(a, b))
1338 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1339 streami->index, stream->index);
1341 #endif
1345 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1346 unsigned int h) {
1347 if (!stream->config.cfg.g_w) {
1348 if (!stream->config.cfg.g_h)
1349 stream->config.cfg.g_w = w;
1350 else
1351 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1353 if (!stream->config.cfg.g_h) {
1354 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1358 static const char *file_type_to_string(enum VideoFileType t) {
1359 switch (t) {
1360 case FILE_TYPE_RAW: return "RAW";
1361 case FILE_TYPE_Y4M: return "Y4M";
1362 default: return "Other";
1366 static const char *image_format_to_string(aom_img_fmt_t f) {
1367 switch (f) {
1368 case AOM_IMG_FMT_I420: return "I420";
1369 case AOM_IMG_FMT_I422: return "I422";
1370 case AOM_IMG_FMT_I444: return "I444";
1371 case AOM_IMG_FMT_I440: return "I440";
1372 case AOM_IMG_FMT_YV12: return "YV12";
1373 case AOM_IMG_FMT_I42016: return "I42016";
1374 case AOM_IMG_FMT_I42216: return "I42216";
1375 case AOM_IMG_FMT_I44416: return "I44416";
1376 case AOM_IMG_FMT_I44016: return "I44016";
1377 default: return "Other";
1381 static void show_stream_config(struct stream_state *stream,
1382 struct AvxEncoderConfig *global,
1383 struct AvxInputContext *input) {
1384 #define SHOW(field) \
1385 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1387 if (stream->index == 0) {
1388 fprintf(stderr, "Codec: %s\n",
1389 aom_codec_iface_name(global->codec->codec_interface()));
1390 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1391 input->filename, file_type_to_string(input->file_type),
1392 image_format_to_string(input->fmt));
1394 if (stream->next || stream->index)
1395 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1396 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1397 fprintf(stderr, "Coding path: %s\n",
1398 stream->config.use_16bit_internal ? "HBD" : "LBD");
1399 fprintf(stderr, "Encoder parameters:\n");
1401 SHOW(g_usage);
1402 SHOW(g_threads);
1403 SHOW(g_profile);
1404 SHOW(g_w);
1405 SHOW(g_h);
1406 SHOW(g_bit_depth);
1407 SHOW(g_input_bit_depth);
1408 SHOW(g_timebase.num);
1409 SHOW(g_timebase.den);
1410 SHOW(g_error_resilient);
1411 SHOW(g_pass);
1412 SHOW(g_lag_in_frames);
1413 SHOW(large_scale_tile);
1414 SHOW(rc_dropframe_thresh);
1415 SHOW(rc_resize_mode);
1416 SHOW(rc_resize_denominator);
1417 SHOW(rc_resize_kf_denominator);
1418 SHOW(rc_superres_mode);
1419 SHOW(rc_superres_denominator);
1420 SHOW(rc_superres_kf_denominator);
1421 SHOW(rc_superres_qthresh);
1422 SHOW(rc_superres_kf_qthresh);
1423 SHOW(rc_end_usage);
1424 SHOW(rc_target_bitrate);
1425 SHOW(rc_min_quantizer);
1426 SHOW(rc_max_quantizer);
1427 SHOW(rc_undershoot_pct);
1428 SHOW(rc_overshoot_pct);
1429 SHOW(rc_buf_sz);
1430 SHOW(rc_buf_initial_sz);
1431 SHOW(rc_buf_optimal_sz);
1432 SHOW(rc_2pass_vbr_bias_pct);
1433 SHOW(rc_2pass_vbr_minsection_pct);
1434 SHOW(rc_2pass_vbr_maxsection_pct);
1435 SHOW(kf_mode);
1436 SHOW(kf_min_dist);
1437 SHOW(kf_max_dist);
1440 static void open_output_file(struct stream_state *stream,
1441 struct AvxEncoderConfig *global,
1442 const struct AvxRational *pixel_aspect_ratio) {
1443 const char *fn = stream->config.out_fn;
1444 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1446 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1448 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1450 if (!stream->file) fatal("Failed to open output file");
1452 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1453 fatal("WebM output to pipes not supported.");
1455 #if CONFIG_WEBM_IO
1456 if (stream->config.write_webm) {
1457 stream->webm_ctx.stream = stream->file;
1458 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1459 global->codec->fourcc, pixel_aspect_ratio);
1461 #else
1462 (void)pixel_aspect_ratio;
1463 #endif
1465 if (!stream->config.write_webm
1466 #if CONFIG_OBU_NO_IVF
1467 && stream->config.write_ivf
1468 #endif
1470 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1474 static void close_output_file(struct stream_state *stream,
1475 unsigned int fourcc) {
1476 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1478 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1480 #if CONFIG_WEBM_IO
1481 if (stream->config.write_webm) {
1482 write_webm_file_footer(&stream->webm_ctx);
1484 #endif
1486 if (!stream->config.write_webm
1487 #if CONFIG_OBU_NO_IVF
1488 && stream->config.write_ivf
1489 #endif
1491 if (!fseek(stream->file, 0, SEEK_SET))
1492 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1493 stream->frames_out);
1496 fclose(stream->file);
1499 static void setup_pass(struct stream_state *stream,
1500 struct AvxEncoderConfig *global, int pass) {
1501 if (stream->config.stats_fn) {
1502 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1503 fatal("Failed to open statistics store");
1504 } else {
1505 if (!stats_open_mem(&stream->stats, pass))
1506 fatal("Failed to open statistics store");
1509 #if CONFIG_FP_MB_STATS
1510 if (stream->config.fpmb_stats_fn) {
1511 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1512 pass))
1513 fatal("Failed to open mb statistics store");
1514 } else {
1515 if (!stats_open_mem(&stream->fpmb_stats, pass))
1516 fatal("Failed to open mb statistics store");
1518 #endif
1520 stream->config.cfg.g_pass = global->passes == 2
1521 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1522 : AOM_RC_ONE_PASS;
1523 if (pass) {
1524 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1525 #if CONFIG_FP_MB_STATS
1526 stream->config.cfg.rc_firstpass_mb_stats_in =
1527 stats_get(&stream->fpmb_stats);
1528 #endif
1531 stream->cx_time = 0;
1532 stream->nbytes = 0;
1533 stream->frames_out = 0;
1536 static void initialize_encoder(struct stream_state *stream,
1537 struct AvxEncoderConfig *global) {
1538 int i;
1539 int flags = 0;
1541 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1542 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1543 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1545 /* Construct Encoder Context */
1546 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
1547 &stream->config.cfg, flags);
1548 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1550 /* Note that we bypass the aom_codec_control wrapper macro because
1551 * we're being clever to store the control IDs in an array. Real
1552 * applications will want to make use of the enumerations directly
1554 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1555 int ctrl = stream->config.arg_ctrls[i][0];
1556 int value = stream->config.arg_ctrls[i][1];
1557 if (aom_codec_control_(&stream->encoder, ctrl, value))
1558 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1560 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1563 #if CONFIG_AV1_DECODER
1564 if (global->test_decode != TEST_DECODE_OFF) {
1565 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1566 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
1567 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
1569 if (strcmp(global->codec->name, "av1") == 0) {
1570 aom_codec_control(&stream->decoder, AV1_SET_TILE_MODE,
1571 stream->config.cfg.large_scale_tile);
1572 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1574 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
1575 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1577 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
1578 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1581 #endif
1584 static void encode_frame(struct stream_state *stream,
1585 struct AvxEncoderConfig *global, struct aom_image *img,
1586 unsigned int frames_in) {
1587 aom_codec_pts_t frame_start, next_frame_start;
1588 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1589 struct aom_usec_timer timer;
1591 frame_start =
1592 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1593 cfg->g_timebase.num / global->framerate.num;
1594 next_frame_start =
1595 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1596 cfg->g_timebase.num / global->framerate.num;
1598 /* Scale if necessary */
1599 if (img) {
1600 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1601 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1602 if (img->fmt != AOM_IMG_FMT_I42016) {
1603 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1604 exit(EXIT_FAILURE);
1606 #if CONFIG_LIBYUV
1607 if (!stream->img) {
1608 stream->img =
1609 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1611 I420Scale_16(
1612 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1613 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1614 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1615 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1616 stream->img->stride[AOM_PLANE_Y] / 2,
1617 (uint16 *)stream->img->planes[AOM_PLANE_U],
1618 stream->img->stride[AOM_PLANE_U] / 2,
1619 (uint16 *)stream->img->planes[AOM_PLANE_V],
1620 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1621 stream->img->d_h, kFilterBox);
1622 img = stream->img;
1623 #else
1624 stream->encoder.err = 1;
1625 ctx_exit_on_error(&stream->encoder,
1626 "Stream %d: Failed to encode frame.\n"
1627 "libyuv is required for scaling but is currently "
1628 "disabled.\n"
1629 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1630 "cmake.\n",
1631 stream->index);
1632 #endif
1635 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1636 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1637 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1638 exit(EXIT_FAILURE);
1640 #if CONFIG_LIBYUV
1641 if (!stream->img)
1642 stream->img =
1643 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1644 I420Scale(
1645 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1646 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1647 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1648 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1649 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1650 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1651 stream->img->d_w, stream->img->d_h, kFilterBox);
1652 img = stream->img;
1653 #else
1654 stream->encoder.err = 1;
1655 ctx_exit_on_error(&stream->encoder,
1656 "Stream %d: Failed to encode frame.\n"
1657 "Scaling disabled in this configuration. \n"
1658 "To enable, configure with --enable-libyuv\n",
1659 stream->index);
1660 #endif
1663 aom_usec_timer_start(&timer);
1664 aom_codec_encode(&stream->encoder, img, frame_start,
1665 (uint32_t)(next_frame_start - frame_start), 0);
1666 aom_usec_timer_mark(&timer);
1667 stream->cx_time += aom_usec_timer_elapsed(&timer);
1668 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1669 stream->index);
1672 static void update_quantizer_histogram(struct stream_state *stream) {
1673 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1674 int q;
1676 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
1677 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1678 stream->counts[q]++;
1682 static void get_cx_data(struct stream_state *stream,
1683 struct AvxEncoderConfig *global, int *got_data) {
1684 const aom_codec_cx_pkt_t *pkt;
1685 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1686 aom_codec_iter_t iter = NULL;
1688 *got_data = 0;
1689 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1690 static size_t fsize = 0;
1691 static FileOffset ivf_header_pos = 0;
1693 switch (pkt->kind) {
1694 case AOM_CODEC_CX_FRAME_PKT:
1695 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1696 stream->frames_out++;
1698 if (!global->quiet)
1699 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1701 update_rate_histogram(stream->rate_hist, cfg, pkt);
1702 #if CONFIG_WEBM_IO
1703 if (stream->config.write_webm) {
1704 write_webm_block(&stream->webm_ctx, cfg, pkt);
1706 #endif
1707 if (!stream->config.write_webm) {
1708 #if CONFIG_OBU_NO_IVF
1709 if (stream->config.write_ivf) {
1710 #endif
1711 if (pkt->data.frame.partition_id <= 0) {
1712 ivf_header_pos = ftello(stream->file);
1713 fsize = pkt->data.frame.sz;
1715 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1716 } else {
1717 fsize += pkt->data.frame.sz;
1719 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1720 const FileOffset currpos = ftello(stream->file);
1721 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1722 ivf_write_frame_size(stream->file, fsize);
1723 fseeko(stream->file, currpos, SEEK_SET);
1726 #if CONFIG_OBU_NO_IVF
1728 #endif
1730 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1731 stream->file);
1733 stream->nbytes += pkt->data.raw.sz;
1735 *got_data = 1;
1736 #if CONFIG_AV1_DECODER
1737 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1738 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1739 (unsigned int)pkt->data.frame.sz, NULL);
1740 if (stream->decoder.err) {
1741 warn_or_exit_on_error(&stream->decoder,
1742 global->test_decode == TEST_DECODE_FATAL,
1743 "Failed to decode frame %d in stream %d",
1744 stream->frames_out + 1, stream->index);
1745 stream->mismatch_seen = stream->frames_out + 1;
1748 #endif
1749 break;
1750 case AOM_CODEC_STATS_PKT:
1751 stream->frames_out++;
1752 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1753 pkt->data.twopass_stats.sz);
1754 stream->nbytes += pkt->data.raw.sz;
1755 break;
1756 #if CONFIG_FP_MB_STATS
1757 case AOM_CODEC_FPMB_STATS_PKT:
1758 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
1759 pkt->data.firstpass_mb_stats.sz);
1760 stream->nbytes += pkt->data.raw.sz;
1761 break;
1762 #endif
1763 case AOM_CODEC_PSNR_PKT:
1765 if (global->show_psnr) {
1766 int i;
1768 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1769 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1770 for (i = 0; i < 4; i++) {
1771 if (!global->quiet)
1772 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1773 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1775 stream->psnr_count++;
1778 break;
1779 default: break;
1784 static void show_psnr(struct stream_state *stream, double peak) {
1785 int i;
1786 double ovpsnr;
1788 if (!stream->psnr_count) return;
1790 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1791 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
1792 (double)stream->psnr_sse_total);
1793 fprintf(stderr, " %.3f", ovpsnr);
1795 for (i = 0; i < 4; i++) {
1796 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1798 fprintf(stderr, "\n");
1801 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1802 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1805 static void test_decode(struct stream_state *stream,
1806 enum TestDecodeFatality fatal) {
1807 aom_image_t enc_img, dec_img;
1809 if (stream->mismatch_seen) return;
1811 /* Get the internal reference frame */
1812 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1813 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
1815 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1816 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1817 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1818 aom_image_t enc_hbd_img;
1819 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1820 enc_img.d_w, enc_img.d_h, 16);
1821 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1822 enc_img = enc_hbd_img;
1824 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1825 aom_image_t dec_hbd_img;
1826 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1827 dec_img.d_w, dec_img.d_h, 16);
1828 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1829 dec_img = dec_hbd_img;
1833 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1834 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1836 if (!aom_compare_img(&enc_img, &dec_img)) {
1837 int y[4], u[4], v[4];
1838 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1839 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1840 } else {
1841 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1843 stream->decoder.err = 1;
1844 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1845 "Stream %d: Encode/decode mismatch on frame %d at"
1846 " Y[%d, %d] {%d/%d},"
1847 " U[%d, %d] {%d/%d},"
1848 " V[%d, %d] {%d/%d}",
1849 stream->index, stream->frames_out, y[0], y[1], y[2],
1850 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1851 stream->mismatch_seen = stream->frames_out;
1854 aom_img_free(&enc_img);
1855 aom_img_free(&dec_img);
1858 static void print_time(const char *label, int64_t etl) {
1859 int64_t hours;
1860 int64_t mins;
1861 int64_t secs;
1863 if (etl >= 0) {
1864 hours = etl / 3600;
1865 etl -= hours * 3600;
1866 mins = etl / 60;
1867 etl -= mins * 60;
1868 secs = etl;
1870 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1871 hours, mins, secs);
1872 } else {
1873 fprintf(stderr, "[%3s unknown] ", label);
1877 int main(int argc, const char **argv_) {
1878 int pass;
1879 aom_image_t raw;
1880 aom_image_t raw_shift;
1881 int allocated_raw_shift = 0;
1882 int use_16bit_internal = 0;
1883 int input_shift = 0;
1884 int frame_avail, got_data;
1886 struct AvxInputContext input;
1887 struct AvxEncoderConfig global;
1888 struct stream_state *streams = NULL;
1889 char **argv, **argi;
1890 uint64_t cx_time = 0;
1891 int stream_cnt = 0;
1892 int res = 0;
1893 int profile_updated = 0;
1895 memset(&input, 0, sizeof(input));
1896 exec_name = argv_[0];
1898 /* Setup default input stream settings */
1899 input.framerate.numerator = 30;
1900 input.framerate.denominator = 1;
1901 input.only_i420 = 1;
1902 input.bit_depth = 0;
1904 /* First parse the global configuration values, because we want to apply
1905 * other parameters on top of the default configuration provided by the
1906 * codec.
1908 argv = argv_dup(argc - 1, argv_ + 1);
1909 parse_global_config(&global, &argc, &argv);
1911 #if CONFIG_FILEOPTIONS
1912 if (argc < 2) usage_exit();
1913 #else
1914 if (argc < 3) usage_exit();
1915 #endif
1917 switch (global.color_type) {
1918 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1919 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1920 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1921 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1922 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
1926 /* Now parse each stream's parameters. Using a local scope here
1927 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1928 * loops
1930 struct stream_state *stream = NULL;
1932 do {
1933 stream = new_stream(&global, stream);
1934 stream_cnt++;
1935 if (!streams) streams = stream;
1936 } while (parse_stream_params(&global, stream, argv));
1939 /* Check for unrecognized options */
1940 for (argi = argv; *argi; argi++)
1941 if (argi[0][0] == '-' && argi[0][1])
1942 die("Error: Unrecognized option %s\n", *argi);
1944 FOREACH_STREAM(stream, streams) {
1945 check_encoder_config(global.disable_warning_prompt, &global,
1946 &stream->config.cfg);
1949 /* Handle non-option arguments */
1950 input.filename = argv[0];
1952 if (!input.filename) {
1953 fprintf(stderr, "No input file specified!\n");
1954 usage_exit();
1957 /* Decide if other chroma subsamplings than 4:2:0 are supported */
1958 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
1960 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1961 int frames_in = 0, seen_frames = 0;
1962 int64_t estimated_time_left = -1;
1963 int64_t average_rate = -1;
1964 int64_t lagged_count = 0;
1966 open_input_file(&input);
1968 /* If the input file doesn't specify its w/h (raw files), try to get
1969 * the data from the first stream's configuration.
1971 if (!input.width || !input.height) {
1972 FOREACH_STREAM(stream, streams) {
1973 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1974 input.width = stream->config.cfg.g_w;
1975 input.height = stream->config.cfg.g_h;
1976 break;
1981 /* Update stream configurations from the input file's parameters */
1982 if (!input.width || !input.height)
1983 fatal(
1984 "Specify stream dimensions with --width (-w) "
1985 " and --height (-h)");
1987 /* If input file does not specify bit-depth but input-bit-depth parameter
1988 * exists, assume that to be the input bit-depth. However, if the
1989 * input-bit-depth paramter does not exist, assume the input bit-depth
1990 * to be the same as the codec bit-depth.
1992 if (!input.bit_depth) {
1993 FOREACH_STREAM(stream, streams) {
1994 if (stream->config.cfg.g_input_bit_depth)
1995 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1996 else
1997 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1998 (int)stream->config.cfg.g_bit_depth;
2000 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
2001 } else {
2002 FOREACH_STREAM(stream, streams) {
2003 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2007 FOREACH_STREAM(stream, streams) {
2008 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2009 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2010 was selected. */
2011 switch (stream->config.cfg.g_profile) {
2012 case 0:
2013 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2014 input.fmt == AOM_IMG_FMT_I44416)) {
2015 if (!stream->config.cfg.monochrome) {
2016 stream->config.cfg.g_profile = 1;
2017 profile_updated = 1;
2019 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2020 input.fmt == AOM_IMG_FMT_I42216) {
2021 stream->config.cfg.g_profile = 2;
2022 profile_updated = 1;
2024 break;
2025 case 1:
2026 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2027 input.fmt == AOM_IMG_FMT_I42216) {
2028 stream->config.cfg.g_profile = 2;
2029 profile_updated = 1;
2030 } else if (input.bit_depth < 12 &&
2031 (input.fmt == AOM_IMG_FMT_I420 ||
2032 input.fmt == AOM_IMG_FMT_I42016)) {
2033 stream->config.cfg.g_profile = 0;
2034 profile_updated = 1;
2036 break;
2037 case 2:
2038 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2039 input.fmt == AOM_IMG_FMT_I44416)) {
2040 stream->config.cfg.g_profile = 1;
2041 profile_updated = 1;
2042 } else if (input.bit_depth < 12 &&
2043 (input.fmt == AOM_IMG_FMT_I420 ||
2044 input.fmt == AOM_IMG_FMT_I42016)) {
2045 stream->config.cfg.g_profile = 0;
2046 profile_updated = 1;
2048 break;
2049 default: break;
2052 /* Automatically set the codec bit depth to match the input bit depth.
2053 * Upgrade the profile if required. */
2054 if (stream->config.cfg.g_input_bit_depth >
2055 (unsigned int)stream->config.cfg.g_bit_depth) {
2056 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2058 if (stream->config.cfg.g_bit_depth > 10) {
2059 switch (stream->config.cfg.g_profile) {
2060 case 0:
2061 case 1:
2062 stream->config.cfg.g_profile = 2;
2063 profile_updated = 1;
2064 break;
2065 default: break;
2068 if (stream->config.cfg.g_bit_depth > 8) {
2069 stream->config.use_16bit_internal = 1;
2071 if (profile_updated && !global.quiet) {
2072 fprintf(stderr,
2073 "Warning: automatically updating to profile %d to "
2074 "match input format.\n",
2075 stream->config.cfg.g_profile);
2079 FOREACH_STREAM(stream, streams) {
2080 set_stream_dimensions(stream, input.width, input.height);
2082 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2084 /* Ensure that --passes and --pass are consistent. If --pass is set and
2085 * --passes=2, ensure --fpf was set.
2087 if (global.pass && global.passes == 2) {
2088 FOREACH_STREAM(stream, streams) {
2089 if (!stream->config.stats_fn)
2090 die("Stream %d: Must specify --fpf when --pass=%d"
2091 " and --passes=2\n",
2092 stream->index, global.pass);
2096 #if !CONFIG_WEBM_IO
2097 FOREACH_STREAM(stream, streams) {
2098 if (stream->config.write_webm) {
2099 stream->config.write_webm = 0;
2100 #if CONFIG_OBU_NO_IVF
2101 stream->config.write_ivf = 0;
2102 #endif
2103 warn(
2104 "aomenc was compiled without WebM container support."
2105 #if CONFIG_OBU_NO_IVF
2106 "Producing OBU output");
2107 #else
2108 "Producing IVF output");
2109 #endif
2112 #endif
2114 /* Use the frame rate from the file only if none was specified
2115 * on the command-line.
2117 if (!global.have_framerate) {
2118 global.framerate.num = input.framerate.numerator;
2119 global.framerate.den = input.framerate.denominator;
2121 FOREACH_STREAM(stream, streams) {
2122 stream->config.cfg.g_timebase.den = global.framerate.num;
2123 stream->config.cfg.g_timebase.num = global.framerate.den;
2125 /* Show configuration */
2126 if (global.verbose && pass == 0) {
2127 FOREACH_STREAM(stream, streams) {
2128 show_stream_config(stream, &global, &input);
2132 if (pass == (global.pass ? global.pass - 1 : 0)) {
2133 if (input.file_type == FILE_TYPE_Y4M)
2134 /*The Y4M reader does its own allocation.
2135 Just initialize this here to avoid problems if we never read any
2136 frames.*/
2137 memset(&raw, 0, sizeof(raw));
2138 else
2139 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2141 FOREACH_STREAM(stream, streams) {
2142 stream->rate_hist =
2143 init_rate_histogram(&stream->config.cfg, &global.framerate);
2147 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2148 FOREACH_STREAM(stream, streams) {
2149 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2151 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2152 if (strcmp(global.codec->name, "av1") == 0 ||
2153 strcmp(global.codec->name, "av1") == 0) {
2154 // Check to see if at least one stream uses 16 bit internal.
2155 // Currently assume that the bit_depths for all streams using
2156 // highbitdepth are the same.
2157 FOREACH_STREAM(stream, streams) {
2158 if (stream->config.use_16bit_internal) {
2159 use_16bit_internal = 1;
2161 input_shift = (int)stream->config.cfg.g_bit_depth -
2162 stream->config.cfg.g_input_bit_depth;
2166 frame_avail = 1;
2167 got_data = 0;
2169 while (frame_avail || got_data) {
2170 struct aom_usec_timer timer;
2172 if (!global.limit || frames_in < global.limit) {
2173 frame_avail = read_frame(&input, &raw);
2175 if (frame_avail) frames_in++;
2176 seen_frames =
2177 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2179 if (!global.quiet) {
2180 float fps = usec_to_fps(cx_time, seen_frames);
2181 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2183 if (stream_cnt == 1)
2184 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2185 streams->frames_out, (int64_t)streams->nbytes);
2186 else
2187 fprintf(stderr, "frame %4d ", frames_in);
2189 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2190 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2191 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2192 fps >= 1.0 ? "fps" : "fpm");
2193 print_time("ETA", estimated_time_left);
2196 } else {
2197 frame_avail = 0;
2200 if (frames_in > global.skip_frames) {
2201 aom_image_t *frame_to_encode;
2202 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2203 assert(use_16bit_internal);
2204 // Input bit depth and stream bit depth do not match, so up
2205 // shift frame to stream bit depth
2206 if (!allocated_raw_shift) {
2207 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2208 input.width, input.height, 32);
2209 allocated_raw_shift = 1;
2211 aom_img_upshift(&raw_shift, &raw, input_shift);
2212 frame_to_encode = &raw_shift;
2213 } else {
2214 frame_to_encode = &raw;
2216 aom_usec_timer_start(&timer);
2217 if (use_16bit_internal) {
2218 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2219 FOREACH_STREAM(stream, streams) {
2220 if (stream->config.use_16bit_internal)
2221 encode_frame(stream, &global,
2222 frame_avail ? frame_to_encode : NULL, frames_in);
2223 else
2224 assert(0);
2226 } else {
2227 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2228 FOREACH_STREAM(stream, streams) {
2229 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2230 frames_in);
2233 aom_usec_timer_mark(&timer);
2234 cx_time += aom_usec_timer_elapsed(&timer);
2236 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2238 got_data = 0;
2239 FOREACH_STREAM(stream, streams) {
2240 get_cx_data(stream, &global, &got_data);
2243 if (!got_data && input.length && streams != NULL &&
2244 !streams->frames_out) {
2245 lagged_count = global.limit ? seen_frames : ftello(input.file);
2246 } else if (input.length) {
2247 int64_t remaining;
2248 int64_t rate;
2250 if (global.limit) {
2251 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2253 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2254 remaining = 1000 * (global.limit - global.skip_frames -
2255 seen_frames + lagged_count);
2256 } else {
2257 const int64_t input_pos = ftello(input.file);
2258 const int64_t input_pos_lagged = input_pos - lagged_count;
2259 const int64_t input_limit = input.length;
2261 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2262 remaining = input_limit - input_pos + lagged_count;
2265 average_rate =
2266 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2267 estimated_time_left = average_rate ? remaining / average_rate : -1;
2270 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2271 FOREACH_STREAM(stream, streams) {
2272 test_decode(stream, global.test_decode);
2277 fflush(stdout);
2278 if (!global.quiet) fprintf(stderr, "\033[K");
2281 if (stream_cnt > 1) fprintf(stderr, "\n");
2283 if (!global.quiet) {
2284 FOREACH_STREAM(stream, streams) {
2285 fprintf(stderr,
2286 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2287 "b/f %7" PRId64
2288 "b/s"
2289 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2290 pass + 1, global.passes, frames_in, stream->frames_out,
2291 (int64_t)stream->nbytes,
2292 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2293 seen_frames ? (int64_t)stream->nbytes * 8 *
2294 (int64_t)global.framerate.num /
2295 global.framerate.den / seen_frames
2296 : 0,
2297 stream->cx_time > 9999999 ? stream->cx_time / 1000
2298 : stream->cx_time,
2299 stream->cx_time > 9999999 ? "ms" : "us",
2300 usec_to_fps(stream->cx_time, seen_frames));
2304 if (global.show_psnr) {
2305 if (global.codec->fourcc == AV1_FOURCC) {
2306 FOREACH_STREAM(stream, streams) {
2307 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2309 } else {
2310 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
2314 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
2316 if (global.test_decode != TEST_DECODE_OFF) {
2317 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
2320 close_input_file(&input);
2322 if (global.test_decode == TEST_DECODE_FATAL) {
2323 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2325 FOREACH_STREAM(stream, streams) {
2326 close_output_file(stream, global.codec->fourcc);
2329 FOREACH_STREAM(stream, streams) {
2330 stats_close(&stream->stats, global.passes - 1);
2333 #if CONFIG_FP_MB_STATS
2334 FOREACH_STREAM(stream, streams) {
2335 stats_close(&stream->fpmb_stats, global.passes - 1);
2337 #endif
2339 if (global.pass) break;
2342 if (global.show_q_hist_buckets) {
2343 FOREACH_STREAM(stream, streams) {
2344 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2348 if (global.show_rate_hist_buckets) {
2349 FOREACH_STREAM(stream, streams) {
2350 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2351 global.show_rate_hist_buckets);
2354 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2356 #if CONFIG_INTERNAL_STATS
2357 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2358 * to match some existing utilities.
2360 if (!(global.pass == 1 && global.passes == 2)) {
2361 FOREACH_STREAM(stream, streams) {
2362 FILE *f = fopen("opsnr.stt", "a");
2363 if (stream->mismatch_seen) {
2364 fprintf(f, "First mismatch occurred in frame %d\n",
2365 stream->mismatch_seen);
2366 } else {
2367 fprintf(f, "No mismatch detected in recon buffers\n");
2369 fclose(f);
2372 #endif
2374 if (allocated_raw_shift) aom_img_free(&raw_shift);
2375 aom_img_free(&raw);
2376 free(argv);
2377 free(streams);
2378 return res ? EXIT_FAILURE : EXIT_SUCCESS;