Add sse2/avx2 version of aom_dc_predictor_wxh()
[aom.git] / aomenc.c
blobb4a39a60f102426ff39e1d1b7aedca2873bfb1e2
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 exended 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 };
387 static const arg_def_t noise_sens =
388 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
389 static const arg_def_t sharpness =
390 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
391 static const arg_def_t static_thresh =
392 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
393 static const arg_def_t auto_altref =
394 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
395 static const arg_def_t arnr_maxframes =
396 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
397 static const arg_def_t arnr_strength =
398 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
399 static const struct arg_enum_list tuning_enum[] = {
400 { "psnr", AOM_TUNE_PSNR },
401 { "ssim", AOM_TUNE_SSIM },
402 #ifdef CONFIG_DIST_8X8
403 { "cdef-dist", AOM_TUNE_CDEF_DIST },
404 { "daala-dist", AOM_TUNE_DAALA_DIST },
405 #endif
406 { NULL, 0 }
408 static const arg_def_t tune_metric =
409 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
410 static const arg_def_t cq_level =
411 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
412 static const arg_def_t max_intra_rate_pct =
413 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
415 #if CONFIG_AV1_ENCODER
416 static const arg_def_t cpu_used_av1 =
417 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
418 static const arg_def_t dev_sf_av1 =
419 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
420 static const arg_def_t single_tile_decoding =
421 ARG_DEF(NULL, "single-tile-decoding", 1,
422 "Single tile decoding (0: off (default), 1: on)");
423 static const arg_def_t tile_cols =
424 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
425 static const arg_def_t tile_rows =
426 ARG_DEF(NULL, "tile-rows", 1,
427 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
428 #if CONFIG_MAX_TILE
429 static const arg_def_t tile_width =
430 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
431 static const arg_def_t tile_height =
432 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
433 #endif
434 #if CONFIG_DEPENDENT_HORZTILES
435 static const arg_def_t tile_dependent_rows =
436 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
437 #endif
438 #if CONFIG_LOOPFILTERING_ACROSS_TILES
439 #if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
440 static const arg_def_t tile_loopfilter_v =
441 ARG_DEF(NULL, "tile-loopfilter-v", 1,
442 "Enable loop filter across vertical tile boundary");
443 static const arg_def_t tile_loopfilter_h =
444 ARG_DEF(NULL, "tile-loopfilter-h", 1,
445 "Enable loop filter across horizontal tile boundary");
446 #else
447 static const arg_def_t tile_loopfilter = ARG_DEF(
448 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
449 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
450 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES
451 static const arg_def_t lossless =
452 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
453 static const arg_def_t enable_cdef =
454 ARG_DEF(NULL, "enable-cdef", 1,
455 "Enable the constrained directional enhancement filter (0: false, "
456 "1: true (default))");
457 static const arg_def_t enable_restoration =
458 ARG_DEF(NULL, "enable-restoration", 1,
459 "Enable the loop restoration filter (0: false, "
460 "1: true (default))");
461 static const arg_def_t enable_qm =
462 ARG_DEF(NULL, "enable-qm", 1,
463 "Enable quantisation matrices (0: false (default), 1: true)");
464 static const arg_def_t qm_min = ARG_DEF(
465 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
466 static const arg_def_t qm_max = ARG_DEF(
467 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 15");
468 #if CONFIG_DIST_8X8
469 static const arg_def_t enable_dist_8x8 =
470 ARG_DEF(NULL, "enable-dist-8x8", 1,
471 "Enable dist-8x8 (0: false (default), 1: true)");
472 #endif // CONFIG_DIST_8X8
473 static const arg_def_t num_tg = ARG_DEF(
474 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
475 static const arg_def_t mtu_size =
476 ARG_DEF(NULL, "mtu-size", 1,
477 "MTU size for a tile group, default is 0 (no MTU targeting), "
478 "overrides maximum number of tile groups");
479 static const struct arg_enum_list timing_info_enum[] = {
480 { "unspecified", AOM_TIMING_UNSPECIFIED },
481 { "constant", AOM_TIMING_EQUAL },
482 { NULL, 0 }
484 static const arg_def_t timing_info =
485 ARG_DEF_ENUM(NULL, "timing-info", 1,
486 "Signal timing info in the bitstream:", timing_info_enum);
487 #if CONFIG_FILM_GRAIN
488 static const arg_def_t film_grain_test =
489 ARG_DEF(NULL, "film-grain-test", 1,
490 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
491 "... 16: test-16)");
492 #endif
493 static const arg_def_t disable_tempmv = ARG_DEF(
494 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
495 static const arg_def_t frame_parallel_decoding =
496 ARG_DEF(NULL, "frame-parallel", 1,
497 "Enable frame parallel decodability features "
498 "(0: false (default), 1: true)");
499 #if !CONFIG_EXT_DELTA_Q
500 static const arg_def_t aq_mode = ARG_DEF(
501 NULL, "aq-mode", 1,
502 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
503 "3: cyclic refresh, 4: delta quant)");
504 #else
505 static const arg_def_t aq_mode = ARG_DEF(
506 NULL, "aq-mode", 1,
507 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
508 "3: cyclic refresh)");
509 #endif
510 #if CONFIG_EXT_DELTA_Q
511 static const arg_def_t deltaq_mode = ARG_DEF(
512 NULL, "deltaq-mode", 1,
513 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
514 #endif
515 static const arg_def_t frame_periodic_boost =
516 ARG_DEF(NULL, "frame-boost", 1,
517 "Enable frame periodic boost (0: off (default), 1: on)");
518 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
519 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
520 static const arg_def_t max_inter_rate_pct =
521 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
522 static const arg_def_t min_gf_interval = ARG_DEF(
523 NULL, "min-gf-interval", 1,
524 "min gf/arf frame interval (default 0, indicating in-built behavior)");
525 static const arg_def_t max_gf_interval = ARG_DEF(
526 NULL, "max-gf-interval", 1,
527 "max gf/arf frame interval (default 0, indicating in-built behavior)");
529 static const struct arg_enum_list color_primaries_enum[] = {
530 { "bt709", AOM_CICP_CP_BT_709 },
531 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
532 { "bt601", AOM_CICP_CP_BT_601 },
533 { "bt470m", AOM_CICP_CP_BT_470_M },
534 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
535 { "smpte240", AOM_CICP_CP_SMPTE_240 },
536 { "film", AOM_CICP_CP_GENERIC_FILM },
537 { "bt2020", AOM_CICP_CP_BT_2020 },
538 { "xyz", AOM_CICP_CP_XYZ },
539 { "smpte431", AOM_CICP_CP_SMPTE_431 },
540 { "smpte432", AOM_CICP_CP_SMPTE_432 },
541 { "ebu3213", AOM_CICP_CP_EBU_3213 },
542 { NULL, 0 }
545 static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
546 NULL, "color-primaries", 1,
547 "Color primaries (CICP) of input content:", color_primaries_enum);
549 static const struct arg_enum_list transfer_characteristics_enum[] = {
550 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
551 { "bt709", AOM_CICP_TC_BT_709 },
552 { "bt470m", AOM_CICP_TC_BT_470_M },
553 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
554 { "bt601", AOM_CICP_TC_BT_601 },
555 { "smpte240", AOM_CICP_TC_SMPTE_240 },
556 { "lin", AOM_CICP_TC_LINEAR },
557 { "log100", AOM_CICP_TC_LOG_100 },
558 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
559 { "iec61966", AOM_CICP_TC_IEC_61966 },
560 { "bt1361", AOM_CICP_TC_BT_1361 },
561 { "srgb", AOM_CICP_TC_SRGB },
562 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
563 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
564 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
565 { "hlg", AOM_CICP_TC_HLG },
566 { "smpte428", AOM_CICP_TC_SMPTE_428 },
567 { NULL, 0 }
570 static const arg_def_t input_transfer_characteristics =
571 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
572 "Transfer characteristics (CICP) of input content:",
573 transfer_characteristics_enum);
575 static const struct arg_enum_list matrix_coefficients_enum[] = {
576 { "identity", AOM_CICP_MC_IDENTITY },
577 { "bt709", AOM_CICP_MC_BT_709 },
578 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
579 { "fcc73", AOM_CICP_MC_FCC },
580 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
581 { "bt601", AOM_CICP_MC_BT_601 },
582 { "smpte240", AOM_CICP_CP_SMPTE_240 },
583 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
584 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
585 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
586 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
587 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
588 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
589 { "ictcp", AOM_CICP_MC_ICTCP },
590 { NULL, 0 }
593 static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
594 NULL, "matrix-coefficients", 1,
595 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
597 static const struct arg_enum_list chroma_sample_position_enum[] = {
598 { "unknown", AOM_CSP_UNKNOWN },
599 { "vertical", AOM_CSP_VERTICAL },
600 { "colocated", AOM_CSP_COLOCATED },
601 { NULL, 0 }
604 static const arg_def_t input_chroma_sample_position =
605 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
606 "The chroma sample position when chroma 4:2:0 is signaled:",
607 chroma_sample_position_enum);
609 static const struct arg_enum_list tune_content_enum[] = {
610 { "default", AOM_CONTENT_DEFAULT },
611 { "screen", AOM_CONTENT_SCREEN },
612 { NULL, 0 }
615 static const arg_def_t tune_content = ARG_DEF_ENUM(
616 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
618 #if CONFIG_CDF_UPDATE_MODE
619 static const arg_def_t cdf_update_mode =
620 ARG_DEF(NULL, "cdf-update-mode", 1,
621 "CDF update mode for entropy coding "
622 "(0: no CDF update; 1: update CDF on all frames(default); "
623 "2: selectively update CDF on some frames");
624 #endif // CONFIG_CDF_UPDATE_MODE
626 static const struct arg_enum_list superblock_size_enum[] = {
627 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
628 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
629 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
630 { NULL, 0 }
632 static const arg_def_t superblock_size = ARG_DEF_ENUM(
633 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
635 static const arg_def_t *av1_args[] = { &cpu_used_av1,
636 &dev_sf_av1,
637 &auto_altref,
638 &sharpness,
639 &static_thresh,
640 &single_tile_decoding,
641 &tile_cols,
642 &tile_rows,
643 #if CONFIG_DEPENDENT_HORZTILES
644 &tile_dependent_rows,
645 #endif
646 #if CONFIG_LOOPFILTERING_ACROSS_TILES
647 #if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
648 &tile_loopfilter_v,
649 &tile_loopfilter_h,
650 #else
651 &tile_loopfilter,
652 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
653 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES
654 &arnr_maxframes,
655 &arnr_strength,
656 &tune_metric,
657 &cq_level,
658 &max_intra_rate_pct,
659 &max_inter_rate_pct,
660 &gf_cbr_boost_pct,
661 &lossless,
662 &enable_cdef,
663 &enable_restoration,
664 &enable_qm,
665 &qm_min,
666 &qm_max,
667 #if CONFIG_DIST_8X8
668 &enable_dist_8x8,
669 #endif
670 &frame_parallel_decoding,
671 &aq_mode,
672 #if CONFIG_EXT_DELTA_Q
673 &deltaq_mode,
674 #endif
675 &frame_periodic_boost,
676 &noise_sens,
677 &tune_content,
678 #if CONFIG_CDF_UPDATE_MODE
679 &cdf_update_mode,
680 #endif // CONFIG_CDF_UPDATE_MODE
681 &input_color_primaries,
682 &input_transfer_characteristics,
683 &input_matrix_coefficients,
684 &input_chroma_sample_position,
685 &min_gf_interval,
686 &max_gf_interval,
687 &superblock_size,
688 &num_tg,
689 &mtu_size,
690 &timing_info,
691 #if CONFIG_FILM_GRAIN
692 &film_grain_test,
693 #endif
694 &disable_tempmv,
695 &bitdeptharg,
696 &inbitdeptharg,
697 NULL };
698 static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
699 AOME_SET_DEVSF,
700 AOME_SET_ENABLEAUTOALTREF,
701 AOME_SET_SHARPNESS,
702 AOME_SET_STATIC_THRESHOLD,
703 AV1E_SET_SINGLE_TILE_DECODING,
704 AV1E_SET_TILE_COLUMNS,
705 AV1E_SET_TILE_ROWS,
706 #if CONFIG_DEPENDENT_HORZTILES
707 AV1E_SET_TILE_DEPENDENT_ROWS,
708 #endif
709 #if CONFIG_LOOPFILTERING_ACROSS_TILES
710 #if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
711 AV1E_SET_TILE_LOOPFILTER_V,
712 AV1E_SET_TILE_LOOPFILTER_H,
713 #else
714 AV1E_SET_TILE_LOOPFILTER,
715 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
716 #endif // CONFIG_LOOPFILTERING_ACROSS_TILES
717 AOME_SET_ARNR_MAXFRAMES,
718 AOME_SET_ARNR_STRENGTH,
719 AOME_SET_TUNING,
720 AOME_SET_CQ_LEVEL,
721 AOME_SET_MAX_INTRA_BITRATE_PCT,
722 AV1E_SET_MAX_INTER_BITRATE_PCT,
723 AV1E_SET_GF_CBR_BOOST_PCT,
724 AV1E_SET_LOSSLESS,
725 AV1E_SET_ENABLE_CDEF,
726 AV1E_SET_ENABLE_RESTORATION,
727 AV1E_SET_ENABLE_QM,
728 AV1E_SET_QM_MIN,
729 AV1E_SET_QM_MAX,
730 #if CONFIG_DIST_8X8
731 AV1E_SET_ENABLE_DIST_8X8,
732 #endif
733 AV1E_SET_FRAME_PARALLEL_DECODING,
734 AV1E_SET_AQ_MODE,
735 #if CONFIG_EXT_DELTA_Q
736 AV1E_SET_DELTAQ_MODE,
737 #endif
738 AV1E_SET_FRAME_PERIODIC_BOOST,
739 AV1E_SET_NOISE_SENSITIVITY,
740 AV1E_SET_TUNE_CONTENT,
741 #if CONFIG_CDF_UPDATE_MODE
742 AV1E_SET_CDF_UPDATE_MODE,
743 #endif // CONFIG_CDF_UPDATE_MODE
744 AV1E_SET_COLOR_PRIMARIES,
745 AV1E_SET_TRANSFER_CHARACTERISTICS,
746 AV1E_SET_MATRIX_COEFFICIENTS,
747 AV1E_SET_CHROMA_SAMPLE_POSITION,
748 AV1E_SET_MIN_GF_INTERVAL,
749 AV1E_SET_MAX_GF_INTERVAL,
750 AV1E_SET_SUPERBLOCK_SIZE,
751 AV1E_SET_NUM_TG,
752 AV1E_SET_MTU,
753 AV1E_SET_TIMING_INFO,
754 #if CONFIG_FILM_GRAIN
755 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
756 #endif
757 AV1E_SET_DISABLE_TEMPMV,
758 AV1E_SET_ENABLE_DF,
759 #if CONFIG_JNT_COMP
760 AV1E_SET_ENABLE_JNT_COMP,
761 #endif
762 0 };
763 #endif // CONFIG_AV1_ENCODER
765 static const arg_def_t *no_args[] = { NULL };
767 void show_help(FILE *fout, int shorthelp) {
768 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
769 exec_name);
771 if (shorthelp) {
772 fprintf(fout, "Use --help to see the full list of options.\n");
773 return;
776 fprintf(fout, "\nOptions:\n");
777 arg_show_usage(fout, main_args);
778 fprintf(fout, "\nEncoder Global Options:\n");
779 arg_show_usage(fout, global_args);
780 fprintf(fout, "\nRate Control Options:\n");
781 arg_show_usage(fout, rc_args);
782 fprintf(fout, "\nTwopass Rate Control Options:\n");
783 arg_show_usage(fout, rc_twopass_args);
784 fprintf(fout, "\nKeyframe Placement Options:\n");
785 arg_show_usage(fout, kf_args);
786 #if CONFIG_AV1_ENCODER
787 fprintf(fout, "\nAV1 Specific Options:\n");
788 arg_show_usage(fout, av1_args);
789 #endif
790 fprintf(fout,
791 "\nStream timebase (--timebase):\n"
792 " The desired precision of timestamps in the output, expressed\n"
793 " in fractional seconds. Default is 1/1000.\n");
794 fprintf(fout, "\nIncluded encoders:\n\n");
796 const int num_encoder = get_aom_encoder_count();
797 for (int i = 0; i < num_encoder; ++i) {
798 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
799 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
800 fprintf(fout, " %-6s - %s %s\n", encoder->name,
801 aom_codec_iface_name(encoder->codec_interface()), defstr);
803 fprintf(fout, "\n ");
804 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
807 void usage_exit(void) {
808 show_help(stderr, 1);
809 exit(EXIT_FAILURE);
812 #if CONFIG_AV1_ENCODER
813 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
814 #endif
816 #if !CONFIG_WEBM_IO
817 typedef int stereo_format_t;
818 struct WebmOutputContext {
819 int debug;
821 #endif
823 /* Per-stream configuration */
824 struct stream_config {
825 struct aom_codec_enc_cfg cfg;
826 const char *out_fn;
827 const char *stats_fn;
828 #if CONFIG_FP_MB_STATS
829 const char *fpmb_stats_fn;
830 #endif
831 stereo_format_t stereo_fmt;
832 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
833 int arg_ctrl_cnt;
834 int write_webm;
835 #if CONFIG_OBU_NO_IVF
836 int write_ivf;
837 #endif
838 // whether to use 16bit internal buffers
839 int use_16bit_internal;
842 struct stream_state {
843 int index;
844 struct stream_state *next;
845 struct stream_config config;
846 FILE *file;
847 struct rate_hist *rate_hist;
848 struct WebmOutputContext webm_ctx;
849 uint64_t psnr_sse_total;
850 uint64_t psnr_samples_total;
851 double psnr_totals[4];
852 int psnr_count;
853 int counts[64];
854 aom_codec_ctx_t encoder;
855 unsigned int frames_out;
856 uint64_t cx_time;
857 size_t nbytes;
858 stats_io_t stats;
859 #if CONFIG_FP_MB_STATS
860 stats_io_t fpmb_stats;
861 #endif
862 struct aom_image *img;
863 aom_codec_ctx_t decoder;
864 int mismatch_seen;
867 static void validate_positive_rational(const char *msg,
868 struct aom_rational *rat) {
869 if (rat->den < 0) {
870 rat->num *= -1;
871 rat->den *= -1;
874 if (rat->num < 0) die("Error: %s must be positive\n", msg);
876 if (!rat->den) die("Error: %s has zero denominator\n", msg);
879 static void parse_global_config(struct AvxEncoderConfig *global, int *argc,
880 char ***argv) {
881 char **argi, **argj;
882 struct arg arg;
883 const int num_encoder = get_aom_encoder_count();
884 char **argv_local = (char **)*argv;
885 #if CONFIG_FILEOPTIONS
886 int argc_local = *argc;
887 #endif
888 if (num_encoder < 1) die("Error: no valid encoder available\n");
890 /* Initialize default parameters */
891 memset(global, 0, sizeof(*global));
892 global->codec = get_aom_encoder_by_index(num_encoder - 1);
893 global->passes = 0;
894 global->color_type = I420;
896 #if CONFIG_FILEOPTIONS
897 const char *cfg = NULL;
898 int cfg_included = 0;
899 #endif
900 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
901 arg.argv_step = 1;
903 #if CONFIG_FILEOPTIONS
904 if (arg_match(&arg, &use_cfg, argi)) {
905 if (cfg_included) continue;
906 cfg = arg.val;
908 arg_cfg(&argc_local, &argv_local, cfg);
910 *argj = *argi = *argv_local;
911 argj = argi = argv_local;
912 *argv = argv_local;
913 cfg_included = 1;
914 continue;
916 #endif
917 if (arg_match(&arg, &help, argi)) {
918 show_help(stdout, 0);
919 exit(EXIT_SUCCESS);
920 } else if (arg_match(&arg, &codecarg, argi)) {
921 global->codec = get_aom_encoder_by_name(arg.val);
922 if (!global->codec)
923 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
924 } else if (arg_match(&arg, &passes, argi)) {
925 global->passes = arg_parse_uint(&arg);
927 if (global->passes < 1 || global->passes > 2)
928 die("Error: Invalid number of passes (%d)\n", global->passes);
929 } else if (arg_match(&arg, &pass_arg, argi)) {
930 global->pass = arg_parse_uint(&arg);
932 if (global->pass < 1 || global->pass > 2)
933 die("Error: Invalid pass selected (%d)\n", global->pass);
934 } else if (arg_match(&arg, &usage, argi))
935 global->usage = arg_parse_uint(&arg);
936 else if (arg_match(&arg, &good_dl, argi))
937 warn("Deprecated --good option! Ignoring\n");
938 else if (arg_match(&arg, &use_yv12, argi))
939 global->color_type = YV12;
940 else if (arg_match(&arg, &use_i420, argi))
941 global->color_type = I420;
942 else if (arg_match(&arg, &use_i422, argi))
943 global->color_type = I422;
944 else if (arg_match(&arg, &use_i444, argi))
945 global->color_type = I444;
946 else if (arg_match(&arg, &use_i440, argi))
947 global->color_type = I440;
948 else if (arg_match(&arg, &quietarg, argi))
949 global->quiet = 1;
950 else if (arg_match(&arg, &verbosearg, argi))
951 global->verbose = 1;
952 else if (arg_match(&arg, &limit, argi))
953 global->limit = arg_parse_uint(&arg);
954 else if (arg_match(&arg, &skip, argi))
955 global->skip_frames = arg_parse_uint(&arg);
956 else if (arg_match(&arg, &psnrarg, argi))
957 global->show_psnr = 1;
958 else if (arg_match(&arg, &recontest, argi))
959 global->test_decode = arg_parse_enum_or_int(&arg);
960 else if (arg_match(&arg, &framerate, argi)) {
961 global->framerate = arg_parse_rational(&arg);
962 validate_positive_rational(arg.name, &global->framerate);
963 global->have_framerate = 1;
964 } else if (arg_match(&arg, &out_part, argi))
965 global->out_part = 1;
966 else if (arg_match(&arg, &debugmode, argi))
967 global->debug = 1;
968 else if (arg_match(&arg, &q_hist_n, argi))
969 global->show_q_hist_buckets = arg_parse_uint(&arg);
970 else if (arg_match(&arg, &rate_hist_n, argi))
971 global->show_rate_hist_buckets = arg_parse_uint(&arg);
972 else if (arg_match(&arg, &disable_warnings, argi))
973 global->disable_warnings = 1;
974 else if (arg_match(&arg, &disable_warning_prompt, argi))
975 global->disable_warning_prompt = 1;
976 else
977 argj++;
980 if (global->pass) {
981 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
982 if (global->pass > global->passes) {
983 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
984 global->pass);
985 global->passes = global->pass;
988 /* Validate global config */
989 if (global->passes == 0) {
990 #if CONFIG_AV1_ENCODER
991 // Make default AV1 passes = 2 until there is a better quality 1-pass
992 // encoder
993 if (global->codec != NULL && global->codec->name != NULL)
994 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
995 #else
996 global->passes = 1;
997 #endif
1001 static void open_input_file(struct AvxInputContext *input) {
1002 /* Parse certain options from the input file, if possible */
1003 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1004 : set_binary_mode(stdin);
1006 if (!input->file) fatal("Failed to open input file");
1008 if (!fseeko(input->file, 0, SEEK_END)) {
1009 /* Input file is seekable. Figure out how long it is, so we can get
1010 * progress info.
1012 input->length = ftello(input->file);
1013 rewind(input->file);
1016 /* Default to 1:1 pixel aspect ratio. */
1017 input->pixel_aspect_ratio.numerator = 1;
1018 input->pixel_aspect_ratio.denominator = 1;
1020 /* For RAW input sources, these bytes will applied on the first frame
1021 * in read_frame().
1023 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1024 input->detect.position = 0;
1026 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
1027 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1028 input->only_i420) >= 0) {
1029 input->file_type = FILE_TYPE_Y4M;
1030 input->width = input->y4m.pic_w;
1031 input->height = input->y4m.pic_h;
1032 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1033 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
1034 input->framerate.numerator = input->y4m.fps_n;
1035 input->framerate.denominator = input->y4m.fps_d;
1036 input->fmt = input->y4m.aom_fmt;
1037 input->bit_depth = input->y4m.bit_depth;
1038 } else
1039 fatal("Unsupported Y4M stream.");
1040 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
1041 fatal("IVF is not supported as input.");
1042 } else {
1043 input->file_type = FILE_TYPE_RAW;
1047 static void close_input_file(struct AvxInputContext *input) {
1048 fclose(input->file);
1049 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
1052 static struct stream_state *new_stream(struct AvxEncoderConfig *global,
1053 struct stream_state *prev) {
1054 struct stream_state *stream;
1056 stream = calloc(1, sizeof(*stream));
1057 if (stream == NULL) {
1058 fatal("Failed to allocate new stream.");
1061 if (prev) {
1062 memcpy(stream, prev, sizeof(*stream));
1063 stream->index++;
1064 prev->next = stream;
1065 } else {
1066 aom_codec_err_t res;
1068 /* Populate encoder configuration */
1069 res = aom_codec_enc_config_default(global->codec->codec_interface(),
1070 &stream->config.cfg, global->usage);
1071 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
1073 /* Change the default timebase to a high enough value so that the
1074 * encoder will always create strictly increasing timestamps.
1076 stream->config.cfg.g_timebase.den = 1000;
1078 /* Never use the library's default resolution, require it be parsed
1079 * from the file or set on the command line.
1081 stream->config.cfg.g_w = 0;
1082 stream->config.cfg.g_h = 0;
1084 /* Initialize remaining stream parameters */
1085 stream->config.write_webm = 1;
1086 #if CONFIG_OBU_NO_IVF
1087 stream->config.write_ivf = 0;
1088 #endif
1089 #if CONFIG_WEBM_IO
1090 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1091 stream->webm_ctx.last_pts_ns = -1;
1092 stream->webm_ctx.writer = NULL;
1093 stream->webm_ctx.segment = NULL;
1094 #endif
1096 /* Allows removal of the application version from the EBML tags */
1097 stream->webm_ctx.debug = global->debug;
1100 /* Output files must be specified for each stream */
1101 stream->config.out_fn = NULL;
1103 stream->next = NULL;
1104 return stream;
1107 static void set_config_arg_ctrls(struct stream_config *config, int key,
1108 const struct arg *arg) {
1109 int j;
1110 /* Point either to the next free element or the first instance of this
1111 * control.
1113 for (j = 0; j < config->arg_ctrl_cnt; j++)
1114 if (config->arg_ctrls[j][0] == key) break;
1116 /* Update/insert */
1117 assert(j < (int)ARG_CTRL_CNT_MAX);
1118 config->arg_ctrls[j][0] = key;
1119 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1120 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1123 static int parse_stream_params(struct AvxEncoderConfig *global,
1124 struct stream_state *stream, char **argv) {
1125 char **argi, **argj;
1126 struct arg arg;
1127 static const arg_def_t **ctrl_args = no_args;
1128 static const int *ctrl_args_map = NULL;
1129 struct stream_config *config = &stream->config;
1130 int eos_mark_found = 0;
1131 int webm_forced = 0;
1133 // Handle codec specific options
1134 if (0) {
1135 #if CONFIG_AV1_ENCODER
1136 } else if (strcmp(global->codec->name, "av1") == 0) {
1137 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1138 // Consider to expand this set for AV1 encoder control.
1139 ctrl_args = av1_args;
1140 ctrl_args_map = av1_arg_ctrl_map;
1141 #endif
1144 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1145 arg.argv_step = 1;
1147 /* Once we've found an end-of-stream marker (--) we want to continue
1148 * shifting arguments but not consuming them.
1150 if (eos_mark_found) {
1151 argj++;
1152 continue;
1153 } else if (!strcmp(*argj, "--")) {
1154 eos_mark_found = 1;
1155 continue;
1158 if (arg_match(&arg, &outputfile, argi)) {
1159 config->out_fn = arg.val;
1160 if (!webm_forced) {
1161 const size_t out_fn_len = strlen(config->out_fn);
1162 if (out_fn_len >= 4 &&
1163 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1164 config->write_webm = 0;
1165 #if CONFIG_OBU_NO_IVF
1166 config->write_ivf = 1;
1167 #endif
1169 #if CONFIG_OBU_NO_IVF
1170 else if (out_fn_len >= 4 &&
1171 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1172 config->write_webm = 0;
1173 config->write_ivf = 0;
1175 #endif
1177 } else if (arg_match(&arg, &fpf_name, argi)) {
1178 config->stats_fn = arg.val;
1179 #if CONFIG_FP_MB_STATS
1180 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1181 config->fpmb_stats_fn = arg.val;
1182 #endif
1183 } else if (arg_match(&arg, &use_webm, argi)) {
1184 #if CONFIG_WEBM_IO
1185 config->write_webm = 1;
1186 webm_forced = 1;
1187 #else
1188 die("Error: --webm specified but webm is disabled.");
1189 #endif
1190 } else if (arg_match(&arg, &use_ivf, argi)) {
1191 config->write_webm = 0;
1192 #if CONFIG_OBU_NO_IVF
1193 config->write_ivf = 1;
1194 #endif
1195 #if CONFIG_OBU_NO_IVF
1196 } else if (arg_match(&arg, &use_obu, argi)) {
1197 config->write_webm = 0;
1198 config->write_ivf = 0;
1199 #endif
1200 } else if (arg_match(&arg, &threads, argi)) {
1201 config->cfg.g_threads = arg_parse_uint(&arg);
1202 } else if (arg_match(&arg, &profile, argi)) {
1203 config->cfg.g_profile = arg_parse_uint(&arg);
1204 } else if (arg_match(&arg, &width, argi)) {
1205 config->cfg.g_w = arg_parse_uint(&arg);
1206 } else if (arg_match(&arg, &height, argi)) {
1207 config->cfg.g_h = arg_parse_uint(&arg);
1208 } else if (arg_match(&arg, &forced_max_frame_width, argi)) {
1209 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1210 } else if (arg_match(&arg, &forced_max_frame_height, argi)) {
1211 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
1212 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1213 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1214 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1215 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1216 #if CONFIG_WEBM_IO
1217 } else if (arg_match(&arg, &stereo_mode, argi)) {
1218 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1219 #endif
1220 } else if (arg_match(&arg, &timebase, argi)) {
1221 config->cfg.g_timebase = arg_parse_rational(&arg);
1222 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1223 } else if (arg_match(&arg, &error_resilient, argi)) {
1224 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1225 } else if (arg_match(&arg, &lag_in_frames, argi)) {
1226 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1227 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1228 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1229 #if CONFIG_MONO_VIDEO
1230 } else if (arg_match(&arg, &monochrome, argi)) {
1231 config->cfg.monochrome = 1;
1232 #endif // CONFIG_MONO_VIDEO
1233 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
1234 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1235 } else if (arg_match(&arg, &resize_mode, argi)) {
1236 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1237 } else if (arg_match(&arg, &resize_denominator, argi)) {
1238 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1239 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1240 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1241 } else if (arg_match(&arg, &superres_mode, argi)) {
1242 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1243 } else if (arg_match(&arg, &superres_denominator, argi)) {
1244 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1245 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1246 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1247 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1248 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1249 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1250 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1251 } else if (arg_match(&arg, &end_usage, argi)) {
1252 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1253 } else if (arg_match(&arg, &target_bitrate, argi)) {
1254 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1255 } else if (arg_match(&arg, &min_quantizer, argi)) {
1256 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1257 } else if (arg_match(&arg, &max_quantizer, argi)) {
1258 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1259 } else if (arg_match(&arg, &undershoot_pct, argi)) {
1260 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1261 } else if (arg_match(&arg, &overshoot_pct, argi)) {
1262 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1263 } else if (arg_match(&arg, &buf_sz, argi)) {
1264 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1265 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
1266 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1267 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1268 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1269 } else if (arg_match(&arg, &bias_pct, argi)) {
1270 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1271 if (global->passes < 2)
1272 warn("option %s ignored in one-pass mode.\n", arg.name);
1273 } else if (arg_match(&arg, &minsection_pct, argi)) {
1274 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1276 if (global->passes < 2)
1277 warn("option %s ignored in one-pass mode.\n", arg.name);
1278 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1279 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1281 if (global->passes < 2)
1282 warn("option %s ignored in one-pass mode.\n", arg.name);
1283 } else if (arg_match(&arg, &kf_min_dist, argi)) {
1284 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1285 } else if (arg_match(&arg, &kf_max_dist, argi)) {
1286 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1287 } else if (arg_match(&arg, &kf_disabled, argi)) {
1288 config->cfg.kf_mode = AOM_KF_DISABLED;
1289 #if CONFIG_MAX_TILE
1290 } else if (arg_match(&arg, &tile_width, argi)) {
1291 config->cfg.tile_width_count =
1292 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1293 } else if (arg_match(&arg, &tile_height, argi)) {
1294 config->cfg.tile_height_count =
1295 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1296 #endif
1297 #if CONFIG_FILEOPTIONS
1298 } else if (arg_match(&arg, &ext_partition, argi)) {
1299 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1300 #endif
1301 } else {
1302 int i, match = 0;
1303 for (i = 0; ctrl_args[i]; i++) {
1304 if (arg_match(&arg, ctrl_args[i], argi)) {
1305 match = 1;
1306 if (ctrl_args_map) {
1307 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1311 if (!match) argj++;
1314 config->use_16bit_internal =
1315 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
1316 return eos_mark_found;
1319 #define FOREACH_STREAM(iterator, list) \
1320 for (struct stream_state *iterator = list; iterator; \
1321 iterator = iterator->next)
1323 static void validate_stream_config(const struct stream_state *stream,
1324 const struct AvxEncoderConfig *global) {
1325 const struct stream_state *streami;
1326 (void)global;
1328 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1329 fatal(
1330 "Stream %d: Specify stream dimensions with --width (-w) "
1331 " and --height (-h)",
1332 stream->index);
1334 // Check that the codec bit depth is greater than the input bit depth.
1335 if (stream->config.cfg.g_input_bit_depth >
1336 (unsigned int)stream->config.cfg.g_bit_depth) {
1337 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1338 stream->index, (int)stream->config.cfg.g_bit_depth,
1339 stream->config.cfg.g_input_bit_depth);
1342 for (streami = stream; streami; streami = streami->next) {
1343 /* All streams require output files */
1344 if (!streami->config.out_fn)
1345 fatal("Stream %d: Output file is required (specify with -o)",
1346 streami->index);
1348 /* Check for two streams outputting to the same file */
1349 if (streami != stream) {
1350 const char *a = stream->config.out_fn;
1351 const char *b = streami->config.out_fn;
1352 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1353 fatal("Stream %d: duplicate output file (from stream %d)",
1354 streami->index, stream->index);
1357 /* Check for two streams sharing a stats file. */
1358 if (streami != stream) {
1359 const char *a = stream->config.stats_fn;
1360 const char *b = streami->config.stats_fn;
1361 if (a && b && !strcmp(a, b))
1362 fatal("Stream %d: duplicate stats file (from stream %d)",
1363 streami->index, stream->index);
1366 #if CONFIG_FP_MB_STATS
1367 /* Check for two streams sharing a mb stats file. */
1368 if (streami != stream) {
1369 const char *a = stream->config.fpmb_stats_fn;
1370 const char *b = streami->config.fpmb_stats_fn;
1371 if (a && b && !strcmp(a, b))
1372 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1373 streami->index, stream->index);
1375 #endif
1379 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1380 unsigned int h) {
1381 if (!stream->config.cfg.g_w) {
1382 if (!stream->config.cfg.g_h)
1383 stream->config.cfg.g_w = w;
1384 else
1385 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1387 if (!stream->config.cfg.g_h) {
1388 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1392 static const char *file_type_to_string(enum VideoFileType t) {
1393 switch (t) {
1394 case FILE_TYPE_RAW: return "RAW";
1395 case FILE_TYPE_Y4M: return "Y4M";
1396 default: return "Other";
1400 static const char *image_format_to_string(aom_img_fmt_t f) {
1401 switch (f) {
1402 case AOM_IMG_FMT_I420: return "I420";
1403 case AOM_IMG_FMT_I422: return "I422";
1404 case AOM_IMG_FMT_I444: return "I444";
1405 case AOM_IMG_FMT_I440: return "I440";
1406 case AOM_IMG_FMT_YV12: return "YV12";
1407 case AOM_IMG_FMT_I42016: return "I42016";
1408 case AOM_IMG_FMT_I42216: return "I42216";
1409 case AOM_IMG_FMT_I44416: return "I44416";
1410 case AOM_IMG_FMT_I44016: return "I44016";
1411 default: return "Other";
1415 static void show_stream_config(struct stream_state *stream,
1416 struct AvxEncoderConfig *global,
1417 struct AvxInputContext *input) {
1418 #define SHOW(field) \
1419 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1421 if (stream->index == 0) {
1422 fprintf(stderr, "Codec: %s\n",
1423 aom_codec_iface_name(global->codec->codec_interface()));
1424 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1425 input->filename, file_type_to_string(input->file_type),
1426 image_format_to_string(input->fmt));
1428 if (stream->next || stream->index)
1429 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1430 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1431 fprintf(stderr, "Coding path: %s\n",
1432 stream->config.use_16bit_internal ? "HBD" : "LBD");
1433 fprintf(stderr, "Encoder parameters:\n");
1435 SHOW(g_usage);
1436 SHOW(g_threads);
1437 SHOW(g_profile);
1438 SHOW(g_w);
1439 SHOW(g_h);
1440 SHOW(g_bit_depth);
1441 SHOW(g_input_bit_depth);
1442 SHOW(g_timebase.num);
1443 SHOW(g_timebase.den);
1444 SHOW(g_error_resilient);
1445 SHOW(g_pass);
1446 SHOW(g_lag_in_frames);
1447 SHOW(large_scale_tile);
1448 SHOW(rc_dropframe_thresh);
1449 SHOW(rc_resize_mode);
1450 SHOW(rc_resize_denominator);
1451 SHOW(rc_resize_kf_denominator);
1452 SHOW(rc_superres_mode);
1453 SHOW(rc_superres_denominator);
1454 SHOW(rc_superres_kf_denominator);
1455 SHOW(rc_superres_qthresh);
1456 SHOW(rc_superres_kf_qthresh);
1457 SHOW(rc_end_usage);
1458 SHOW(rc_target_bitrate);
1459 SHOW(rc_min_quantizer);
1460 SHOW(rc_max_quantizer);
1461 SHOW(rc_undershoot_pct);
1462 SHOW(rc_overshoot_pct);
1463 SHOW(rc_buf_sz);
1464 SHOW(rc_buf_initial_sz);
1465 SHOW(rc_buf_optimal_sz);
1466 SHOW(rc_2pass_vbr_bias_pct);
1467 SHOW(rc_2pass_vbr_minsection_pct);
1468 SHOW(rc_2pass_vbr_maxsection_pct);
1469 SHOW(kf_mode);
1470 SHOW(kf_min_dist);
1471 SHOW(kf_max_dist);
1474 static void open_output_file(struct stream_state *stream,
1475 struct AvxEncoderConfig *global,
1476 const struct AvxRational *pixel_aspect_ratio) {
1477 const char *fn = stream->config.out_fn;
1478 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1480 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1482 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1484 if (!stream->file) fatal("Failed to open output file");
1486 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1487 fatal("WebM output to pipes not supported.");
1489 #if CONFIG_WEBM_IO
1490 if (stream->config.write_webm) {
1491 stream->webm_ctx.stream = stream->file;
1492 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1493 global->codec->fourcc, pixel_aspect_ratio);
1495 #else
1496 (void)pixel_aspect_ratio;
1497 #endif
1499 if (!stream->config.write_webm
1500 #if CONFIG_OBU_NO_IVF
1501 && stream->config.write_ivf
1502 #endif
1504 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1508 static void close_output_file(struct stream_state *stream,
1509 unsigned int fourcc) {
1510 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1512 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1514 #if CONFIG_WEBM_IO
1515 if (stream->config.write_webm) {
1516 write_webm_file_footer(&stream->webm_ctx);
1518 #endif
1520 if (!stream->config.write_webm
1521 #if CONFIG_OBU_NO_IVF
1522 && stream->config.write_ivf
1523 #endif
1525 if (!fseek(stream->file, 0, SEEK_SET))
1526 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1527 stream->frames_out);
1530 fclose(stream->file);
1533 static void setup_pass(struct stream_state *stream,
1534 struct AvxEncoderConfig *global, int pass) {
1535 if (stream->config.stats_fn) {
1536 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1537 fatal("Failed to open statistics store");
1538 } else {
1539 if (!stats_open_mem(&stream->stats, pass))
1540 fatal("Failed to open statistics store");
1543 #if CONFIG_FP_MB_STATS
1544 if (stream->config.fpmb_stats_fn) {
1545 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1546 pass))
1547 fatal("Failed to open mb statistics store");
1548 } else {
1549 if (!stats_open_mem(&stream->fpmb_stats, pass))
1550 fatal("Failed to open mb statistics store");
1552 #endif
1554 stream->config.cfg.g_pass = global->passes == 2
1555 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1556 : AOM_RC_ONE_PASS;
1557 if (pass) {
1558 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1559 #if CONFIG_FP_MB_STATS
1560 stream->config.cfg.rc_firstpass_mb_stats_in =
1561 stats_get(&stream->fpmb_stats);
1562 #endif
1565 stream->cx_time = 0;
1566 stream->nbytes = 0;
1567 stream->frames_out = 0;
1570 static void initialize_encoder(struct stream_state *stream,
1571 struct AvxEncoderConfig *global) {
1572 int i;
1573 int flags = 0;
1575 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1576 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1577 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1579 /* Construct Encoder Context */
1580 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
1581 &stream->config.cfg, flags);
1582 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1584 /* Note that we bypass the aom_codec_control wrapper macro because
1585 * we're being clever to store the control IDs in an array. Real
1586 * applications will want to make use of the enumerations directly
1588 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1589 int ctrl = stream->config.arg_ctrls[i][0];
1590 int value = stream->config.arg_ctrls[i][1];
1591 if (aom_codec_control_(&stream->encoder, ctrl, value))
1592 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1594 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1597 #if CONFIG_AV1_DECODER
1598 if (global->test_decode != TEST_DECODE_OFF) {
1599 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1600 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
1601 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
1603 if (strcmp(global->codec->name, "av1") == 0) {
1604 aom_codec_control(&stream->decoder, AV1_SET_TILE_MODE,
1605 stream->config.cfg.large_scale_tile);
1606 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1608 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
1609 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1611 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
1612 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1615 #endif
1618 static void encode_frame(struct stream_state *stream,
1619 struct AvxEncoderConfig *global, struct aom_image *img,
1620 unsigned int frames_in) {
1621 aom_codec_pts_t frame_start, next_frame_start;
1622 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1623 struct aom_usec_timer timer;
1625 frame_start =
1626 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1627 cfg->g_timebase.num / global->framerate.num;
1628 next_frame_start =
1629 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1630 cfg->g_timebase.num / global->framerate.num;
1632 /* Scale if necessary */
1633 if (img) {
1634 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1635 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1636 if (img->fmt != AOM_IMG_FMT_I42016) {
1637 fprintf(stderr, "%s can only scale 4:2:0 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_I42016, cfg->g_w, cfg->g_h, 16);
1645 I420Scale_16(
1646 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1647 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1648 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1649 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1650 stream->img->stride[AOM_PLANE_Y] / 2,
1651 (uint16 *)stream->img->planes[AOM_PLANE_U],
1652 stream->img->stride[AOM_PLANE_U] / 2,
1653 (uint16 *)stream->img->planes[AOM_PLANE_V],
1654 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1655 stream->img->d_h, kFilterBox);
1656 img = stream->img;
1657 #else
1658 stream->encoder.err = 1;
1659 ctx_exit_on_error(&stream->encoder,
1660 "Stream %d: Failed to encode frame.\n"
1661 "libyuv is required for scaling but is currently "
1662 "disabled.\n"
1663 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1664 "cmake.\n",
1665 stream->index);
1666 #endif
1669 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1670 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1671 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1672 exit(EXIT_FAILURE);
1674 #if CONFIG_LIBYUV
1675 if (!stream->img)
1676 stream->img =
1677 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1678 I420Scale(
1679 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1680 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1681 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1682 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1683 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1684 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1685 stream->img->d_w, stream->img->d_h, kFilterBox);
1686 img = stream->img;
1687 #else
1688 stream->encoder.err = 1;
1689 ctx_exit_on_error(&stream->encoder,
1690 "Stream %d: Failed to encode frame.\n"
1691 "Scaling disabled in this configuration. \n"
1692 "To enable, configure with --enable-libyuv\n",
1693 stream->index);
1694 #endif
1697 aom_usec_timer_start(&timer);
1698 aom_codec_encode(&stream->encoder, img, frame_start,
1699 (uint32_t)(next_frame_start - frame_start), 0);
1700 aom_usec_timer_mark(&timer);
1701 stream->cx_time += aom_usec_timer_elapsed(&timer);
1702 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1703 stream->index);
1706 static void update_quantizer_histogram(struct stream_state *stream) {
1707 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1708 int q;
1710 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
1711 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1712 stream->counts[q]++;
1716 static void get_cx_data(struct stream_state *stream,
1717 struct AvxEncoderConfig *global, int *got_data) {
1718 const aom_codec_cx_pkt_t *pkt;
1719 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1720 aom_codec_iter_t iter = NULL;
1722 *got_data = 0;
1723 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1724 static size_t fsize = 0;
1725 static FileOffset ivf_header_pos = 0;
1727 switch (pkt->kind) {
1728 case AOM_CODEC_CX_FRAME_PKT:
1729 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1730 stream->frames_out++;
1732 if (!global->quiet)
1733 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1735 update_rate_histogram(stream->rate_hist, cfg, pkt);
1736 #if CONFIG_WEBM_IO
1737 if (stream->config.write_webm) {
1738 write_webm_block(&stream->webm_ctx, cfg, pkt);
1740 #endif
1741 if (!stream->config.write_webm) {
1742 #if CONFIG_OBU_NO_IVF
1743 if (stream->config.write_ivf) {
1744 #endif
1745 if (pkt->data.frame.partition_id <= 0) {
1746 ivf_header_pos = ftello(stream->file);
1747 fsize = pkt->data.frame.sz;
1749 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1750 } else {
1751 fsize += pkt->data.frame.sz;
1753 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1754 const FileOffset currpos = ftello(stream->file);
1755 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1756 ivf_write_frame_size(stream->file, fsize);
1757 fseeko(stream->file, currpos, SEEK_SET);
1760 #if CONFIG_OBU_NO_IVF
1762 #endif
1764 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1765 stream->file);
1767 stream->nbytes += pkt->data.raw.sz;
1769 *got_data = 1;
1770 #if CONFIG_AV1_DECODER
1771 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1772 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1773 (unsigned int)pkt->data.frame.sz, NULL);
1774 if (stream->decoder.err) {
1775 warn_or_exit_on_error(&stream->decoder,
1776 global->test_decode == TEST_DECODE_FATAL,
1777 "Failed to decode frame %d in stream %d",
1778 stream->frames_out + 1, stream->index);
1779 stream->mismatch_seen = stream->frames_out + 1;
1782 #endif
1783 break;
1784 case AOM_CODEC_STATS_PKT:
1785 stream->frames_out++;
1786 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1787 pkt->data.twopass_stats.sz);
1788 stream->nbytes += pkt->data.raw.sz;
1789 break;
1790 #if CONFIG_FP_MB_STATS
1791 case AOM_CODEC_FPMB_STATS_PKT:
1792 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
1793 pkt->data.firstpass_mb_stats.sz);
1794 stream->nbytes += pkt->data.raw.sz;
1795 break;
1796 #endif
1797 case AOM_CODEC_PSNR_PKT:
1799 if (global->show_psnr) {
1800 int i;
1802 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1803 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1804 for (i = 0; i < 4; i++) {
1805 if (!global->quiet)
1806 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1807 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1809 stream->psnr_count++;
1812 break;
1813 default: break;
1818 static void show_psnr(struct stream_state *stream, double peak) {
1819 int i;
1820 double ovpsnr;
1822 if (!stream->psnr_count) return;
1824 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1825 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
1826 (double)stream->psnr_sse_total);
1827 fprintf(stderr, " %.3f", ovpsnr);
1829 for (i = 0; i < 4; i++) {
1830 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1832 fprintf(stderr, "\n");
1835 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1836 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1839 static void test_decode(struct stream_state *stream,
1840 enum TestDecodeFatality fatal) {
1841 aom_image_t enc_img, dec_img;
1843 if (stream->mismatch_seen) return;
1845 /* Get the internal reference frame */
1846 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1847 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
1849 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1850 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1851 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1852 aom_image_t enc_hbd_img;
1853 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1854 enc_img.d_w, enc_img.d_h, 16);
1855 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1856 enc_img = enc_hbd_img;
1858 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1859 aom_image_t dec_hbd_img;
1860 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1861 dec_img.d_w, dec_img.d_h, 16);
1862 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1863 dec_img = dec_hbd_img;
1867 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1868 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1870 if (!aom_compare_img(&enc_img, &dec_img)) {
1871 int y[4], u[4], v[4];
1872 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1873 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1874 } else {
1875 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1877 stream->decoder.err = 1;
1878 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1879 "Stream %d: Encode/decode mismatch on frame %d at"
1880 " Y[%d, %d] {%d/%d},"
1881 " U[%d, %d] {%d/%d},"
1882 " V[%d, %d] {%d/%d}",
1883 stream->index, stream->frames_out, y[0], y[1], y[2],
1884 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1885 stream->mismatch_seen = stream->frames_out;
1888 aom_img_free(&enc_img);
1889 aom_img_free(&dec_img);
1892 static void print_time(const char *label, int64_t etl) {
1893 int64_t hours;
1894 int64_t mins;
1895 int64_t secs;
1897 if (etl >= 0) {
1898 hours = etl / 3600;
1899 etl -= hours * 3600;
1900 mins = etl / 60;
1901 etl -= mins * 60;
1902 secs = etl;
1904 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1905 hours, mins, secs);
1906 } else {
1907 fprintf(stderr, "[%3s unknown] ", label);
1911 int main(int argc, const char **argv_) {
1912 int pass;
1913 aom_image_t raw;
1914 aom_image_t raw_shift;
1915 int allocated_raw_shift = 0;
1916 int use_16bit_internal = 0;
1917 int input_shift = 0;
1918 int frame_avail, got_data;
1920 struct AvxInputContext input;
1921 struct AvxEncoderConfig global;
1922 struct stream_state *streams = NULL;
1923 char **argv, **argi;
1924 uint64_t cx_time = 0;
1925 int stream_cnt = 0;
1926 int res = 0;
1927 int profile_updated = 0;
1929 memset(&input, 0, sizeof(input));
1930 exec_name = argv_[0];
1932 /* Setup default input stream settings */
1933 input.framerate.numerator = 30;
1934 input.framerate.denominator = 1;
1935 input.only_i420 = 1;
1936 input.bit_depth = 0;
1938 /* First parse the global configuration values, because we want to apply
1939 * other parameters on top of the default configuration provided by the
1940 * codec.
1942 argv = argv_dup(argc - 1, argv_ + 1);
1943 parse_global_config(&global, &argc, &argv);
1945 #if CONFIG_FILEOPTIONS
1946 if (argc < 2) usage_exit();
1947 #else
1948 if (argc < 3) usage_exit();
1949 #endif
1951 switch (global.color_type) {
1952 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1953 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1954 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1955 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1956 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
1960 /* Now parse each stream's parameters. Using a local scope here
1961 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1962 * loops
1964 struct stream_state *stream = NULL;
1966 do {
1967 stream = new_stream(&global, stream);
1968 stream_cnt++;
1969 if (!streams) streams = stream;
1970 } while (parse_stream_params(&global, stream, argv));
1973 /* Check for unrecognized options */
1974 for (argi = argv; *argi; argi++)
1975 if (argi[0][0] == '-' && argi[0][1])
1976 die("Error: Unrecognized option %s\n", *argi);
1978 FOREACH_STREAM(stream, streams) {
1979 check_encoder_config(global.disable_warning_prompt, &global,
1980 &stream->config.cfg);
1983 /* Handle non-option arguments */
1984 input.filename = argv[0];
1986 if (!input.filename) {
1987 fprintf(stderr, "No input file specified!\n");
1988 usage_exit();
1991 /* Decide if other chroma subsamplings than 4:2:0 are supported */
1992 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
1994 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1995 int frames_in = 0, seen_frames = 0;
1996 int64_t estimated_time_left = -1;
1997 int64_t average_rate = -1;
1998 int64_t lagged_count = 0;
2000 open_input_file(&input);
2002 /* If the input file doesn't specify its w/h (raw files), try to get
2003 * the data from the first stream's configuration.
2005 if (!input.width || !input.height) {
2006 FOREACH_STREAM(stream, streams) {
2007 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2008 input.width = stream->config.cfg.g_w;
2009 input.height = stream->config.cfg.g_h;
2010 break;
2015 /* Update stream configurations from the input file's parameters */
2016 if (!input.width || !input.height)
2017 fatal(
2018 "Specify stream dimensions with --width (-w) "
2019 " and --height (-h)");
2021 /* If input file does not specify bit-depth but input-bit-depth parameter
2022 * exists, assume that to be the input bit-depth. However, if the
2023 * input-bit-depth paramter does not exist, assume the input bit-depth
2024 * to be the same as the codec bit-depth.
2026 if (!input.bit_depth) {
2027 FOREACH_STREAM(stream, streams) {
2028 if (stream->config.cfg.g_input_bit_depth)
2029 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2030 else
2031 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2032 (int)stream->config.cfg.g_bit_depth;
2034 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
2035 } else {
2036 FOREACH_STREAM(stream, streams) {
2037 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2041 FOREACH_STREAM(stream, streams) {
2042 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2043 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2044 was selected. */
2045 switch (stream->config.cfg.g_profile) {
2046 case 0:
2047 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2048 input.fmt == AOM_IMG_FMT_I44416)) {
2049 stream->config.cfg.g_profile = 1;
2050 profile_updated = 1;
2051 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2052 input.fmt == AOM_IMG_FMT_I42216) {
2053 stream->config.cfg.g_profile = 2;
2054 profile_updated = 1;
2056 break;
2057 case 1:
2058 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2059 input.fmt == AOM_IMG_FMT_I42216) {
2060 stream->config.cfg.g_profile = 2;
2061 profile_updated = 1;
2062 } else if (input.bit_depth < 12 &&
2063 (input.fmt == AOM_IMG_FMT_I420 ||
2064 input.fmt == AOM_IMG_FMT_I42016)) {
2065 stream->config.cfg.g_profile = 0;
2066 profile_updated = 1;
2068 break;
2069 case 2:
2070 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2071 input.fmt == AOM_IMG_FMT_I44416)) {
2072 stream->config.cfg.g_profile = 1;
2073 profile_updated = 1;
2074 } else if (input.bit_depth < 12 &&
2075 (input.fmt == AOM_IMG_FMT_I420 ||
2076 input.fmt == AOM_IMG_FMT_I42016)) {
2077 stream->config.cfg.g_profile = 0;
2078 profile_updated = 1;
2080 break;
2081 default: break;
2084 /* Automatically set the codec bit depth to match the input bit depth.
2085 * Upgrade the profile if required. */
2086 if (stream->config.cfg.g_input_bit_depth >
2087 (unsigned int)stream->config.cfg.g_bit_depth) {
2088 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2090 if (stream->config.cfg.g_bit_depth > 10) {
2091 switch (stream->config.cfg.g_profile) {
2092 case 0:
2093 case 1:
2094 stream->config.cfg.g_profile = 2;
2095 profile_updated = 1;
2096 break;
2097 default: break;
2100 if (stream->config.cfg.g_bit_depth > 8) {
2101 stream->config.use_16bit_internal = 1;
2103 if (profile_updated && !global.quiet) {
2104 fprintf(stderr,
2105 "Warning: automatically updating to profile %d to "
2106 "match input format.\n",
2107 stream->config.cfg.g_profile);
2111 FOREACH_STREAM(stream, streams) {
2112 set_stream_dimensions(stream, input.width, input.height);
2114 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2116 /* Ensure that --passes and --pass are consistent. If --pass is set and
2117 * --passes=2, ensure --fpf was set.
2119 if (global.pass && global.passes == 2) {
2120 FOREACH_STREAM(stream, streams) {
2121 if (!stream->config.stats_fn)
2122 die("Stream %d: Must specify --fpf when --pass=%d"
2123 " and --passes=2\n",
2124 stream->index, global.pass);
2128 #if !CONFIG_WEBM_IO
2129 FOREACH_STREAM(stream, streams) {
2130 if (stream->config.write_webm) {
2131 stream->config.write_webm = 0;
2132 #if CONFIG_OBU_NO_IVF
2133 stream->config.write_ivf = 0;
2134 #endif
2135 warn(
2136 "aomenc was compiled without WebM container support."
2137 #if CONFIG_OBU_NO_IVF
2138 "Producing OBU output");
2139 #else
2140 "Producing IVF output");
2141 #endif
2144 #endif
2146 /* Use the frame rate from the file only if none was specified
2147 * on the command-line.
2149 if (!global.have_framerate) {
2150 global.framerate.num = input.framerate.numerator;
2151 global.framerate.den = input.framerate.denominator;
2153 FOREACH_STREAM(stream, streams) {
2154 stream->config.cfg.g_timebase.den = global.framerate.num;
2155 stream->config.cfg.g_timebase.num = global.framerate.den;
2157 /* Show configuration */
2158 if (global.verbose && pass == 0) {
2159 FOREACH_STREAM(stream, streams) {
2160 show_stream_config(stream, &global, &input);
2164 if (pass == (global.pass ? global.pass - 1 : 0)) {
2165 if (input.file_type == FILE_TYPE_Y4M)
2166 /*The Y4M reader does its own allocation.
2167 Just initialize this here to avoid problems if we never read any
2168 frames.*/
2169 memset(&raw, 0, sizeof(raw));
2170 else
2171 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2173 FOREACH_STREAM(stream, streams) {
2174 stream->rate_hist =
2175 init_rate_histogram(&stream->config.cfg, &global.framerate);
2179 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2180 FOREACH_STREAM(stream, streams) {
2181 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2183 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2184 if (strcmp(global.codec->name, "av1") == 0 ||
2185 strcmp(global.codec->name, "av1") == 0) {
2186 // Check to see if at least one stream uses 16 bit internal.
2187 // Currently assume that the bit_depths for all streams using
2188 // highbitdepth are the same.
2189 FOREACH_STREAM(stream, streams) {
2190 if (stream->config.use_16bit_internal) {
2191 use_16bit_internal = 1;
2193 input_shift = (int)stream->config.cfg.g_bit_depth -
2194 stream->config.cfg.g_input_bit_depth;
2198 frame_avail = 1;
2199 got_data = 0;
2201 while (frame_avail || got_data) {
2202 struct aom_usec_timer timer;
2204 if (!global.limit || frames_in < global.limit) {
2205 frame_avail = read_frame(&input, &raw);
2207 if (frame_avail) frames_in++;
2208 seen_frames =
2209 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2211 if (!global.quiet) {
2212 float fps = usec_to_fps(cx_time, seen_frames);
2213 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2215 if (stream_cnt == 1)
2216 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2217 streams->frames_out, (int64_t)streams->nbytes);
2218 else
2219 fprintf(stderr, "frame %4d ", frames_in);
2221 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2222 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2223 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2224 fps >= 1.0 ? "fps" : "fpm");
2225 print_time("ETA", estimated_time_left);
2228 } else {
2229 frame_avail = 0;
2232 if (frames_in > global.skip_frames) {
2233 aom_image_t *frame_to_encode;
2234 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2235 assert(use_16bit_internal);
2236 // Input bit depth and stream bit depth do not match, so up
2237 // shift frame to stream bit depth
2238 if (!allocated_raw_shift) {
2239 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2240 input.width, input.height, 32);
2241 allocated_raw_shift = 1;
2243 aom_img_upshift(&raw_shift, &raw, input_shift);
2244 frame_to_encode = &raw_shift;
2245 } else {
2246 frame_to_encode = &raw;
2248 aom_usec_timer_start(&timer);
2249 if (use_16bit_internal) {
2250 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2251 FOREACH_STREAM(stream, streams) {
2252 if (stream->config.use_16bit_internal)
2253 encode_frame(stream, &global,
2254 frame_avail ? frame_to_encode : NULL, frames_in);
2255 else
2256 assert(0);
2258 } else {
2259 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2260 FOREACH_STREAM(stream, streams) {
2261 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2262 frames_in);
2265 aom_usec_timer_mark(&timer);
2266 cx_time += aom_usec_timer_elapsed(&timer);
2268 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2270 got_data = 0;
2271 FOREACH_STREAM(stream, streams) {
2272 get_cx_data(stream, &global, &got_data);
2275 if (!got_data && input.length && streams != NULL &&
2276 !streams->frames_out) {
2277 lagged_count = global.limit ? seen_frames : ftello(input.file);
2278 } else if (input.length) {
2279 int64_t remaining;
2280 int64_t rate;
2282 if (global.limit) {
2283 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2285 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2286 remaining = 1000 * (global.limit - global.skip_frames -
2287 seen_frames + lagged_count);
2288 } else {
2289 const int64_t input_pos = ftello(input.file);
2290 const int64_t input_pos_lagged = input_pos - lagged_count;
2291 const int64_t input_limit = input.length;
2293 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2294 remaining = input_limit - input_pos + lagged_count;
2297 average_rate =
2298 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2299 estimated_time_left = average_rate ? remaining / average_rate : -1;
2302 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2303 FOREACH_STREAM(stream, streams) {
2304 test_decode(stream, global.test_decode);
2309 fflush(stdout);
2310 if (!global.quiet) fprintf(stderr, "\033[K");
2313 if (stream_cnt > 1) fprintf(stderr, "\n");
2315 if (!global.quiet) {
2316 FOREACH_STREAM(stream, streams) {
2317 fprintf(stderr,
2318 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2319 "b/f %7" PRId64
2320 "b/s"
2321 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2322 pass + 1, global.passes, frames_in, stream->frames_out,
2323 (int64_t)stream->nbytes,
2324 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2325 seen_frames ? (int64_t)stream->nbytes * 8 *
2326 (int64_t)global.framerate.num /
2327 global.framerate.den / seen_frames
2328 : 0,
2329 stream->cx_time > 9999999 ? stream->cx_time / 1000
2330 : stream->cx_time,
2331 stream->cx_time > 9999999 ? "ms" : "us",
2332 usec_to_fps(stream->cx_time, seen_frames));
2336 if (global.show_psnr) {
2337 if (global.codec->fourcc == AV1_FOURCC) {
2338 FOREACH_STREAM(stream, streams) {
2339 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2341 } else {
2342 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
2346 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
2348 if (global.test_decode != TEST_DECODE_OFF) {
2349 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
2352 close_input_file(&input);
2354 if (global.test_decode == TEST_DECODE_FATAL) {
2355 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2357 FOREACH_STREAM(stream, streams) {
2358 close_output_file(stream, global.codec->fourcc);
2361 FOREACH_STREAM(stream, streams) {
2362 stats_close(&stream->stats, global.passes - 1);
2365 #if CONFIG_FP_MB_STATS
2366 FOREACH_STREAM(stream, streams) {
2367 stats_close(&stream->fpmb_stats, global.passes - 1);
2369 #endif
2371 if (global.pass) break;
2374 if (global.show_q_hist_buckets) {
2375 FOREACH_STREAM(stream, streams) {
2376 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2380 if (global.show_rate_hist_buckets) {
2381 FOREACH_STREAM(stream, streams) {
2382 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2383 global.show_rate_hist_buckets);
2386 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2388 #if CONFIG_INTERNAL_STATS
2389 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2390 * to match some existing utilities.
2392 if (!(global.pass == 1 && global.passes == 2)) {
2393 FOREACH_STREAM(stream, streams) {
2394 FILE *f = fopen("opsnr.stt", "a");
2395 if (stream->mismatch_seen) {
2396 fprintf(f, "First mismatch occurred in frame %d\n",
2397 stream->mismatch_seen);
2398 } else {
2399 fprintf(f, "No mismatch detected in recon buffers\n");
2401 fclose(f);
2404 #endif
2406 if (allocated_raw_shift) aom_img_free(&raw_shift);
2407 aom_img_free(&raw);
2408 free(argv);
2409 free(streams);
2410 return res ? EXIT_FAILURE : EXIT_SUCCESS;