blob: 555c6d88397b2d0bdee472759e4b04f889b2017b [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
James Zernb7c05bd2024-06-11 19:15:10 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Tom Fineganc65bf062018-05-21 15:50:57 -070012#include "apps/aomenc.h"
13
Tom Finegan60e653d2018-05-22 11:34:58 -070014#include "config/aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040015
Tom Finegan00a35aa2013-11-14 12:37:42 -080016#include <assert.h>
17#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080018#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080019#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040020#include <stdio.h>
21#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040022#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080023
Tom Fineganba02c242017-05-16 15:01:54 -070024#if CONFIG_AV1_DECODER
Yaowu Xuf883b422016-08-30 14:01:10 -070025#include "aom/aom_decoder.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070026#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080027#endif
28
Tom Finegan77902132018-05-21 10:19:15 -070029#include "aom/aom_encoder.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080030#include "aom/aom_integer.h"
Tom Finegan77902132018-05-21 10:19:15 -070031#include "aom/aomcx.h"
hui suefdad1f2017-05-16 18:01:25 -070032#include "aom_dsp/aom_dsp_common.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080033#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem_ops.h"
Tom Finegan77902132018-05-21 10:19:15 -070035#include "common/args.h"
36#include "common/ivfenc.h"
37#include "common/tools_common.h"
38#include "common/warnings.h"
39
James Zerndba73762014-05-10 17:44:12 -070040#if CONFIG_WEBM_IO
Tom Finegan77902132018-05-21 10:19:15 -070041#include "common/webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070042#endif
Tom Finegan77902132018-05-21 10:19:15 -070043
44#include "common/y4minput.h"
45#include "examples/encoder_util.h"
46#include "stats/aomstats.h"
47#include "stats/rate_hist.h"
48
49#if CONFIG_LIBYUV
50#include "third_party/libyuv/include/libyuv/scale.h"
51#endif
Tom Finegan03848f52013-11-05 10:02:18 -080052
John Koleszar6291dd42012-06-19 21:05:36 -070053/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070054static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080055 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070056}
57#define fread wrap_fread
58
59static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080060 FILE *stream) {
61 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070062}
63#define fwrite wrap_fwrite
64
John Koleszar0ea50ce2010-05-18 11:58:33 -040065static const char *exec_name;
66
James Zern20660e52021-11-02 17:19:10 -070067static AOM_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
68 aom_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070069 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070070 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040071
John Koleszar6ad3b742012-11-06 12:02:42 -080072 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070073 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
clang-format6c4d83e2016-08-08 19:03:30 -070075 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040076
Mudassir Galaganath6062c092023-07-18 14:01:21 +053077 if (fatal) {
78 aom_codec_destroy(ctx);
79 exit(EXIT_FAILURE);
80 }
John Koleszarc6b90392012-07-13 15:21:29 -070081 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
James Zern20660e52021-11-02 17:19:10 -070084static AOM_TOOLS_FORMAT_PRINTF(2,
85 3) void ctx_exit_on_error(aom_codec_ctx_t *ctx,
86 const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080087 va_list ap;
88
89 va_start(ap, s);
90 warn_or_exit_on_errorv(ctx, 1, s, ap);
91 va_end(ap);
92}
93
James Zern20660e52021-11-02 17:19:10 -070094static AOM_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
95 aom_codec_ctx_t *ctx, int fatal, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080096 va_list ap;
97
98 va_start(ap, s);
99 warn_or_exit_on_errorv(ctx, fatal, s, ap);
100 va_end(ap);
101}
102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800104 FILE *f = input_ctx->file;
105 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700106 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400107
Tom Finegan00a35aa2013-11-14 12:37:42 -0800108 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700109 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700110 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800111 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700112 }
113
114 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400115}
116
James Zern5c337fd2015-05-09 10:42:58 -0700117static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700118 if (memcmp(detect, "YUV4", 4) == 0) {
119 return 1;
120 }
121 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400122}
123
James Zern5c337fd2015-05-09 10:42:58 -0700124static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800125 if (memcmp(detect, "DKIF", 4) == 0) {
126 return 1;
127 }
128 return 0;
129}
John Koleszardc666302010-10-20 12:05:48 -0400130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
132 AOME_SET_ENABLEAUTOALTREF,
133 AOME_SET_SHARPNESS,
134 AOME_SET_STATIC_THRESHOLD,
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +0530135 AV1E_SET_ROW_MT,
Remya Prakasan93c1c372021-10-25 14:36:07 +0530136 AV1E_SET_FP_MT,
Yaowu Xuf883b422016-08-30 14:01:10 -0700137 AV1E_SET_TILE_COLUMNS,
138 AV1E_SET_TILE_ROWS,
Yue Chen7cae98f2018-08-24 10:43:16 -0700139 AV1E_SET_ENABLE_TPL_MODEL,
Cheng Chen0fcf6f82019-10-11 11:41:19 -0700140 AV1E_SET_ENABLE_KEYFRAME_FILTERING,
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 AOME_SET_ARNR_MAXFRAMES,
142 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700143 AOME_SET_TUNING,
144 AOME_SET_CQ_LEVEL,
145 AOME_SET_MAX_INTRA_BITRATE_PCT,
146 AV1E_SET_MAX_INTER_BITRATE_PCT,
147 AV1E_SET_GF_CBR_BOOST_PCT,
148 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100149 AV1E_SET_ENABLE_CDEF,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100150 AV1E_SET_ENABLE_RESTORATION,
Debargha Mukherjee9d9cf192018-12-17 11:24:21 -0800151 AV1E_SET_ENABLE_RECT_PARTITIONS,
Ryan Lei9543c702019-03-11 11:06:14 -0700152 AV1E_SET_ENABLE_AB_PARTITIONS,
153 AV1E_SET_ENABLE_1TO4_PARTITIONS,
154 AV1E_SET_MIN_PARTITION_SIZE,
155 AV1E_SET_MAX_PARTITION_SIZE,
Debargha Mukherjee4ad01132018-12-10 17:37:47 -0800156 AV1E_SET_ENABLE_DUAL_FILTER,
Yue Chenc5806c22019-07-19 17:32:57 -0700157 AV1E_SET_ENABLE_CHROMA_DELTAQ,
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -0800158 AV1E_SET_ENABLE_INTRA_EDGE_FILTER,
Debargha Mukherjee4ad01132018-12-10 17:37:47 -0800159 AV1E_SET_ENABLE_ORDER_HINT,
Debargha Mukherjee8c917b02018-12-17 13:45:50 -0800160 AV1E_SET_ENABLE_TX64,
Ryan Lei9543c702019-03-11 11:06:14 -0700161 AV1E_SET_ENABLE_FLIP_IDTX,
Josh Verdejob7897b42020-08-13 18:17:30 +0000162 AV1E_SET_ENABLE_RECT_TX,
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -0800163 AV1E_SET_ENABLE_DIST_WTD_COMP,
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -0800164 AV1E_SET_ENABLE_MASKED_COMP,
Ryan Lei9543c702019-03-11 11:06:14 -0700165 AV1E_SET_ENABLE_ONESIDED_COMP,
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -0800166 AV1E_SET_ENABLE_INTERINTRA_COMP,
Yue Chen8cbee6c2018-12-14 11:54:39 -0800167 AV1E_SET_ENABLE_SMOOTH_INTERINTRA,
Debargha Mukherjee78e6b2c2018-12-11 07:50:57 -0800168 AV1E_SET_ENABLE_DIFF_WTD_COMP,
169 AV1E_SET_ENABLE_INTERINTER_WEDGE,
170 AV1E_SET_ENABLE_INTERINTRA_WEDGE,
Debargha Mukherjeed2e53ca2018-12-10 11:34:59 -0800171 AV1E_SET_ENABLE_GLOBAL_MOTION,
172 AV1E_SET_ENABLE_WARPED_MOTION,
Yue Chen8f9ca582018-12-12 15:11:47 -0800173 AV1E_SET_ENABLE_FILTER_INTRA,
Debargha Mukherjee078ae862018-12-18 11:05:43 -0800174 AV1E_SET_ENABLE_SMOOTH_INTRA,
175 AV1E_SET_ENABLE_PAETH_INTRA,
Debargha Mukherjee8e256782018-12-18 13:44:13 -0800176 AV1E_SET_ENABLE_CFL_INTRA,
Fyodor Kyslovc491c662021-03-09 11:11:20 -0800177 AV1E_SET_ENABLE_DIAGONAL_INTRA,
Yaowu Xu2a9ac432019-08-06 14:21:17 -0700178 AV1E_SET_FORCE_VIDEO_MODE,
Yue Chen5e12951e2018-12-10 10:48:54 -0800179 AV1E_SET_ENABLE_OBMC,
Debargha Mukherjeea2074dd2019-09-04 10:03:44 -0700180 AV1E_SET_ENABLE_OVERLAY,
Hui Sua9e3d142018-12-14 15:21:21 -0800181 AV1E_SET_ENABLE_PALETTE,
Hui Su440e6d42018-12-17 11:47:07 -0800182 AV1E_SET_ENABLE_INTRABC,
Hui Su94b38172018-12-19 10:40:29 -0800183 AV1E_SET_ENABLE_ANGLE_DELTA,
Yue Chen4835dc02018-05-11 15:57:43 -0700184 AV1E_SET_DISABLE_TRELLIS_QUANT,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800185 AV1E_SET_ENABLE_QM,
186 AV1E_SET_QM_MIN,
187 AV1E_SET_QM_MAX,
Sarah Parker0b7e5902018-12-10 13:35:25 -0800188 AV1E_SET_REDUCED_TX_TYPE_SET,
Debargha Mukherjeedb458152019-01-25 11:04:09 -0800189 AV1E_SET_INTRA_DCT_ONLY,
190 AV1E_SET_INTER_DCT_ONLY,
Debargha Mukherjee042af922019-02-07 05:58:26 -0800191 AV1E_SET_INTRA_DEFAULT_TX_ONLY,
Sarah Parker740e8392019-01-23 15:47:53 -0800192 AV1E_SET_QUANT_B_ADAPT,
Debargha Mukherjee03ad12d2019-02-07 11:17:59 -0800193 AV1E_SET_COEFF_COST_UPD_FREQ,
194 AV1E_SET_MODE_COST_UPD_FREQ,
Debargha Mukherjee8d273412019-09-11 10:36:50 -0700195 AV1E_SET_MV_COST_UPD_FREQ,
Yaowu Xuf883b422016-08-30 14:01:10 -0700196 AV1E_SET_FRAME_PARALLEL_DECODING,
Debargha Mukherjee921cc4c2018-03-24 12:01:12 -0700197 AV1E_SET_ERROR_RESILIENT_MODE,
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700199 AV1E_SET_DELTAQ_MODE,
Sai Denga62022d2021-10-01 10:27:57 -0700200 AV1E_SET_DELTAQ_STRENGTH,
Debargha Mukherjee4fef7a52019-04-04 01:02:06 -0700201 AV1E_SET_DELTALF_MODE,
Yaowu Xuf883b422016-08-30 14:01:10 -0700202 AV1E_SET_FRAME_PERIODIC_BOOST,
203 AV1E_SET_NOISE_SENSITIVITY,
204 AV1E_SET_TUNE_CONTENT,
Hui Su1cb1c002018-02-05 18:21:20 -0800205 AV1E_SET_CDF_UPDATE_MODE,
Andrey Norkin9e694632017-12-21 18:50:57 -0800206 AV1E_SET_COLOR_PRIMARIES,
207 AV1E_SET_TRANSFER_CHARACTERISTICS,
208 AV1E_SET_MATRIX_COEFFICIENTS,
anorkin76fb1262017-03-22 15:12:12 -0700209 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 AV1E_SET_MIN_GF_INTERVAL,
211 AV1E_SET_MAX_GF_INTERVAL,
Urvang Joshib44f48f2020-01-27 11:09:48 -0800212 AV1E_SET_GF_MIN_PYRAMID_HEIGHT,
Urvang Joshif4e775c2018-12-14 11:33:17 -0800213 AV1E_SET_GF_MAX_PYRAMID_HEIGHT,
Yaowu Xuf883b422016-08-30 14:01:10 -0700214 AV1E_SET_SUPERBLOCK_SIZE,
Thomas Daviesaf6df172016-11-09 14:04:18 +0000215 AV1E_SET_NUM_TG,
216 AV1E_SET_MTU,
Andrey Norkin795ba872018-03-06 13:24:14 -0800217 AV1E_SET_TIMING_INFO_TYPE,
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800218 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700219 AV1E_SET_FILM_GRAIN_TABLE,
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700220#if CONFIG_DENOISE
221 AV1E_SET_DENOISE_NOISE_LEVEL,
222 AV1E_SET_DENOISE_BLOCK_SIZE,
n9Mtq408eb1d42021-02-18 00:13:10 -0500223 AV1E_SET_ENABLE_DNL_DENOISING,
Urvang Joshi9ad9f082019-01-22 12:31:33 -0800224#endif // CONFIG_DENOISE
225 AV1E_SET_MAX_REFERENCE_FRAMES,
Urvang Joshi1a9e27e2019-02-19 11:17:23 -0800226 AV1E_SET_REDUCED_REFERENCE_SET,
Debargha Mukherjee0d8368a2018-03-25 12:23:02 -0700227 AV1E_SET_ENABLE_REF_FRAME_MVS,
Hui Sufad38562019-03-04 14:12:23 -0800228 AV1E_SET_TARGET_SEQ_LEVEL_IDX,
Hui Sud909c2c2019-03-08 11:51:14 -0800229 AV1E_SET_TIER_MASK,
Hui Suef139e12019-05-20 15:51:22 -0700230 AV1E_SET_MIN_CR,
Hamsalekha S3de77b42020-04-23 17:19:49 +0530231 AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP,
Bohan Li368b9182021-02-04 12:04:43 -0800232 AV1E_SET_CHROMA_SUBSAMPLING_X,
233 AV1E_SET_CHROMA_SUBSAMPLING_Y,
sdeng29c62152019-12-09 12:44:18 -0800234#if CONFIG_TUNE_VMAF
235 AV1E_SET_VMAF_MODEL_PATH,
236#endif
chiyotsaicb037d72021-04-15 17:17:08 -0700237 AV1E_SET_DV_COST_UPD_FREQ,
Cheng Chen9164dee2021-04-19 23:11:38 -0700238 AV1E_SET_PARTITION_INFO_PATH,
Maryla4bb52812021-06-18 08:36:58 +0200239 AV1E_SET_ENABLE_DIRECTIONAL_INTRA,
Maryla36c2b992021-06-21 11:48:34 +0200240 AV1E_SET_ENABLE_TX_SIZE_SEARCH,
Fyodor Kyslov2372d3e2021-11-01 16:11:13 -0700241 AV1E_SET_LOOPFILTER_CONTROL,
Cheng Chenf15d9ea2021-11-15 16:52:13 -0800242 AV1E_SET_AUTO_INTRA_TOOLS_OFF,
Cheng Chen567038a2022-12-22 16:45:02 -0800243 AV1E_ENABLE_RATE_GUIDE_DELTAQ,
244 AV1E_SET_RATE_DISTRIBUTION_INFO,
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 0 };
Bohan Lic1d42fe2020-12-21 13:27:47 -0800246
247const arg_def_t *main_args[] = { &g_av1_codec_arg_defs.help,
248 &g_av1_codec_arg_defs.use_cfg,
249 &g_av1_codec_arg_defs.debugmode,
250 &g_av1_codec_arg_defs.outputfile,
251 &g_av1_codec_arg_defs.codecarg,
252 &g_av1_codec_arg_defs.passes,
253 &g_av1_codec_arg_defs.pass_arg,
254 &g_av1_codec_arg_defs.fpf_name,
255 &g_av1_codec_arg_defs.limit,
256 &g_av1_codec_arg_defs.skip,
257 &g_av1_codec_arg_defs.good_dl,
258 &g_av1_codec_arg_defs.rt_dl,
Jingning Hanc5d38e02021-02-11 14:40:27 -0800259 &g_av1_codec_arg_defs.ai_dl,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800260 &g_av1_codec_arg_defs.quietarg,
261 &g_av1_codec_arg_defs.verbosearg,
262 &g_av1_codec_arg_defs.psnrarg,
263 &g_av1_codec_arg_defs.use_webm,
264 &g_av1_codec_arg_defs.use_ivf,
265 &g_av1_codec_arg_defs.use_obu,
266 &g_av1_codec_arg_defs.q_hist_n,
267 &g_av1_codec_arg_defs.rate_hist_n,
268 &g_av1_codec_arg_defs.disable_warnings,
269 &g_av1_codec_arg_defs.disable_warning_prompt,
270 &g_av1_codec_arg_defs.recontest,
271 NULL };
272
273const arg_def_t *global_args[] = {
Jerome Jiangd116cab2022-01-21 13:17:02 -0800274 &g_av1_codec_arg_defs.use_nv12,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800275 &g_av1_codec_arg_defs.use_yv12,
276 &g_av1_codec_arg_defs.use_i420,
277 &g_av1_codec_arg_defs.use_i422,
278 &g_av1_codec_arg_defs.use_i444,
279 &g_av1_codec_arg_defs.usage,
280 &g_av1_codec_arg_defs.threads,
281 &g_av1_codec_arg_defs.profile,
282 &g_av1_codec_arg_defs.width,
283 &g_av1_codec_arg_defs.height,
284 &g_av1_codec_arg_defs.forced_max_frame_width,
285 &g_av1_codec_arg_defs.forced_max_frame_height,
286#if CONFIG_WEBM_IO
287 &g_av1_codec_arg_defs.stereo_mode,
288#endif
289 &g_av1_codec_arg_defs.timebase,
290 &g_av1_codec_arg_defs.framerate,
291 &g_av1_codec_arg_defs.global_error_resilient,
292 &g_av1_codec_arg_defs.bitdeptharg,
Bohan Li368b9182021-02-04 12:04:43 -0800293 &g_av1_codec_arg_defs.inbitdeptharg,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800294 &g_av1_codec_arg_defs.lag_in_frames,
295 &g_av1_codec_arg_defs.large_scale_tile,
296 &g_av1_codec_arg_defs.monochrome,
297 &g_av1_codec_arg_defs.full_still_picture_hdr,
298 &g_av1_codec_arg_defs.use_16bit_internal,
Bohan Li368b9182021-02-04 12:04:43 -0800299 &g_av1_codec_arg_defs.save_as_annexb,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800300 NULL
301};
302
303const arg_def_t *rc_args[] = { &g_av1_codec_arg_defs.dropframe_thresh,
304 &g_av1_codec_arg_defs.resize_mode,
305 &g_av1_codec_arg_defs.resize_denominator,
306 &g_av1_codec_arg_defs.resize_kf_denominator,
307 &g_av1_codec_arg_defs.superres_mode,
308 &g_av1_codec_arg_defs.superres_denominator,
309 &g_av1_codec_arg_defs.superres_kf_denominator,
310 &g_av1_codec_arg_defs.superres_qthresh,
311 &g_av1_codec_arg_defs.superres_kf_qthresh,
312 &g_av1_codec_arg_defs.end_usage,
313 &g_av1_codec_arg_defs.target_bitrate,
314 &g_av1_codec_arg_defs.min_quantizer,
315 &g_av1_codec_arg_defs.max_quantizer,
316 &g_av1_codec_arg_defs.undershoot_pct,
317 &g_av1_codec_arg_defs.overshoot_pct,
318 &g_av1_codec_arg_defs.buf_sz,
319 &g_av1_codec_arg_defs.buf_initial_sz,
320 &g_av1_codec_arg_defs.buf_optimal_sz,
321 &g_av1_codec_arg_defs.bias_pct,
322 &g_av1_codec_arg_defs.minsection_pct,
323 &g_av1_codec_arg_defs.maxsection_pct,
324 NULL };
325
326const arg_def_t *kf_args[] = { &g_av1_codec_arg_defs.fwd_kf_enabled,
327 &g_av1_codec_arg_defs.kf_min_dist,
328 &g_av1_codec_arg_defs.kf_max_dist,
Bohan Li368b9182021-02-04 12:04:43 -0800329 &g_av1_codec_arg_defs.kf_disabled,
330 &g_av1_codec_arg_defs.sframe_dist,
331 &g_av1_codec_arg_defs.sframe_mode,
332 NULL };
Bohan Lic1d42fe2020-12-21 13:27:47 -0800333
Bohan Li77edd8c2021-02-04 16:07:34 -0800334// TODO(bohanli): Currently all options are supported by the key & value API.
335// Consider removing the control ID usages?
Bohan Li67c3ce02021-02-25 12:10:29 -0800336const arg_def_t *av1_ctrl_args[] = {
Bohan Lic1d42fe2020-12-21 13:27:47 -0800337 &g_av1_codec_arg_defs.cpu_used_av1,
338 &g_av1_codec_arg_defs.auto_altref,
339 &g_av1_codec_arg_defs.sharpness,
340 &g_av1_codec_arg_defs.static_thresh,
341 &g_av1_codec_arg_defs.rowmtarg,
Remya Prakasan93c1c372021-10-25 14:36:07 +0530342 &g_av1_codec_arg_defs.fpmtarg,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800343 &g_av1_codec_arg_defs.tile_cols,
344 &g_av1_codec_arg_defs.tile_rows,
345 &g_av1_codec_arg_defs.enable_tpl_model,
346 &g_av1_codec_arg_defs.enable_keyframe_filtering,
347 &g_av1_codec_arg_defs.arnr_maxframes,
348 &g_av1_codec_arg_defs.arnr_strength,
349 &g_av1_codec_arg_defs.tune_metric,
350 &g_av1_codec_arg_defs.cq_level,
351 &g_av1_codec_arg_defs.max_intra_rate_pct,
352 &g_av1_codec_arg_defs.max_inter_rate_pct,
353 &g_av1_codec_arg_defs.gf_cbr_boost_pct,
354 &g_av1_codec_arg_defs.lossless,
355 &g_av1_codec_arg_defs.enable_cdef,
356 &g_av1_codec_arg_defs.enable_restoration,
357 &g_av1_codec_arg_defs.enable_rect_partitions,
358 &g_av1_codec_arg_defs.enable_ab_partitions,
359 &g_av1_codec_arg_defs.enable_1to4_partitions,
360 &g_av1_codec_arg_defs.min_partition_size,
361 &g_av1_codec_arg_defs.max_partition_size,
362 &g_av1_codec_arg_defs.enable_dual_filter,
363 &g_av1_codec_arg_defs.enable_chroma_deltaq,
364 &g_av1_codec_arg_defs.enable_intra_edge_filter,
365 &g_av1_codec_arg_defs.enable_order_hint,
366 &g_av1_codec_arg_defs.enable_tx64,
367 &g_av1_codec_arg_defs.enable_flip_idtx,
368 &g_av1_codec_arg_defs.enable_rect_tx,
369 &g_av1_codec_arg_defs.enable_dist_wtd_comp,
370 &g_av1_codec_arg_defs.enable_masked_comp,
371 &g_av1_codec_arg_defs.enable_onesided_comp,
372 &g_av1_codec_arg_defs.enable_interintra_comp,
373 &g_av1_codec_arg_defs.enable_smooth_interintra,
374 &g_av1_codec_arg_defs.enable_diff_wtd_comp,
375 &g_av1_codec_arg_defs.enable_interinter_wedge,
376 &g_av1_codec_arg_defs.enable_interintra_wedge,
377 &g_av1_codec_arg_defs.enable_global_motion,
378 &g_av1_codec_arg_defs.enable_warped_motion,
379 &g_av1_codec_arg_defs.enable_filter_intra,
380 &g_av1_codec_arg_defs.enable_smooth_intra,
381 &g_av1_codec_arg_defs.enable_paeth_intra,
382 &g_av1_codec_arg_defs.enable_cfl_intra,
Fyodor Kyslovc491c662021-03-09 11:11:20 -0800383 &g_av1_codec_arg_defs.enable_diagonal_intra,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800384 &g_av1_codec_arg_defs.force_video_mode,
385 &g_av1_codec_arg_defs.enable_obmc,
386 &g_av1_codec_arg_defs.enable_overlay,
387 &g_av1_codec_arg_defs.enable_palette,
388 &g_av1_codec_arg_defs.enable_intrabc,
389 &g_av1_codec_arg_defs.enable_angle_delta,
390 &g_av1_codec_arg_defs.disable_trellis_quant,
391 &g_av1_codec_arg_defs.enable_qm,
392 &g_av1_codec_arg_defs.qm_min,
393 &g_av1_codec_arg_defs.qm_max,
394 &g_av1_codec_arg_defs.reduced_tx_type_set,
395 &g_av1_codec_arg_defs.use_intra_dct_only,
396 &g_av1_codec_arg_defs.use_inter_dct_only,
397 &g_av1_codec_arg_defs.use_intra_default_tx_only,
398 &g_av1_codec_arg_defs.quant_b_adapt,
399 &g_av1_codec_arg_defs.coeff_cost_upd_freq,
400 &g_av1_codec_arg_defs.mode_cost_upd_freq,
401 &g_av1_codec_arg_defs.mv_cost_upd_freq,
402 &g_av1_codec_arg_defs.frame_parallel_decoding,
403 &g_av1_codec_arg_defs.error_resilient_mode,
404 &g_av1_codec_arg_defs.aq_mode,
405 &g_av1_codec_arg_defs.deltaq_mode,
Sai Denga62022d2021-10-01 10:27:57 -0700406 &g_av1_codec_arg_defs.deltaq_strength,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800407 &g_av1_codec_arg_defs.deltalf_mode,
408 &g_av1_codec_arg_defs.frame_periodic_boost,
409 &g_av1_codec_arg_defs.noise_sens,
410 &g_av1_codec_arg_defs.tune_content,
411 &g_av1_codec_arg_defs.cdf_update_mode,
412 &g_av1_codec_arg_defs.input_color_primaries,
413 &g_av1_codec_arg_defs.input_transfer_characteristics,
414 &g_av1_codec_arg_defs.input_matrix_coefficients,
415 &g_av1_codec_arg_defs.input_chroma_sample_position,
416 &g_av1_codec_arg_defs.min_gf_interval,
417 &g_av1_codec_arg_defs.max_gf_interval,
418 &g_av1_codec_arg_defs.gf_min_pyr_height,
419 &g_av1_codec_arg_defs.gf_max_pyr_height,
420 &g_av1_codec_arg_defs.superblock_size,
421 &g_av1_codec_arg_defs.num_tg,
422 &g_av1_codec_arg_defs.mtu_size,
423 &g_av1_codec_arg_defs.timing_info,
424 &g_av1_codec_arg_defs.film_grain_test,
425 &g_av1_codec_arg_defs.film_grain_table,
426#if CONFIG_DENOISE
427 &g_av1_codec_arg_defs.denoise_noise_level,
428 &g_av1_codec_arg_defs.denoise_block_size,
n9Mtq408eb1d42021-02-18 00:13:10 -0500429 &g_av1_codec_arg_defs.enable_dnl_denoising,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800430#endif // CONFIG_DENOISE
431 &g_av1_codec_arg_defs.max_reference_frames,
432 &g_av1_codec_arg_defs.reduced_reference_set,
433 &g_av1_codec_arg_defs.enable_ref_frame_mvs,
434 &g_av1_codec_arg_defs.target_seq_level_idx,
435 &g_av1_codec_arg_defs.set_tier_mask,
436 &g_av1_codec_arg_defs.set_min_cr,
437 &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800438 &g_av1_codec_arg_defs.input_chroma_subsampling_x,
439 &g_av1_codec_arg_defs.input_chroma_subsampling_y,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800440#if CONFIG_TUNE_VMAF
441 &g_av1_codec_arg_defs.vmaf_model_path,
Bohan Li67c3ce02021-02-25 12:10:29 -0800442#endif
chiyotsaicb037d72021-04-15 17:17:08 -0700443 &g_av1_codec_arg_defs.dv_cost_upd_freq,
Cheng Chen9164dee2021-04-19 23:11:38 -0700444 &g_av1_codec_arg_defs.partition_info_path,
Maryla4bb52812021-06-18 08:36:58 +0200445 &g_av1_codec_arg_defs.enable_directional_intra,
Maryla36c2b992021-06-21 11:48:34 +0200446 &g_av1_codec_arg_defs.enable_tx_size_search,
Fyodor Kyslov2372d3e2021-11-01 16:11:13 -0700447 &g_av1_codec_arg_defs.loopfilter_control,
Cheng Chenf15d9ea2021-11-15 16:52:13 -0800448 &g_av1_codec_arg_defs.auto_intra_tools_off,
Cheng Chen97e6d482024-03-07 17:10:41 -0800449 &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
450 &g_av1_codec_arg_defs.rate_distribution_info,
Bohan Li67c3ce02021-02-25 12:10:29 -0800451 NULL,
452};
453
454const arg_def_t *av1_key_val_args[] = {
Bohan Li445fdf62021-06-03 16:16:00 -0700455 &g_av1_codec_arg_defs.passes,
Bohan Li86117812021-07-14 14:08:59 -0700456 &g_av1_codec_arg_defs.two_pass_output,
Bohan Liedec7cd2021-08-26 09:17:47 -0700457 &g_av1_codec_arg_defs.second_pass_log,
Jingning Han07eda7a2021-06-22 13:56:57 -0700458 &g_av1_codec_arg_defs.fwd_kf_dist,
Bohan Li239351d2022-01-21 11:42:17 -0800459 &g_av1_codec_arg_defs.strict_level_conformance,
linzhen49073062022-10-31 00:42:59 +0000460 &g_av1_codec_arg_defs.sb_qp_sweep,
Luca Versari6807a7f2021-12-20 21:41:20 +0100461 &g_av1_codec_arg_defs.dist_metric,
Bohan Li4e025702022-08-18 12:05:43 -0700462 &g_av1_codec_arg_defs.kf_max_pyr_height,
Bohan Li67c3ce02021-02-25 12:10:29 -0800463 NULL,
Bohan Lic1d42fe2020-12-21 13:27:47 -0800464};
hui su82331e02015-08-16 18:21:56 -0700465
John Koleszar0ea50ce2010-05-18 11:58:33 -0400466static const arg_def_t *no_args[] = { NULL };
467
Wan-Teh Changdc6e2c82018-09-21 13:49:49 -0700468static void show_help(FILE *fout, int shorthelp) {
Wan-Teh Changca5281c2021-03-02 11:14:10 -0800469 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename\n",
John Koleszarc6b90392012-07-13 15:21:29 -0700470 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400471
James Zernfd8c78c2017-11-27 16:37:33 -0800472 if (shorthelp) {
473 fprintf(fout, "Use --help to see the full list of options.\n");
474 return;
475 }
476
477 fprintf(fout, "\nOptions:\n");
478 arg_show_usage(fout, main_args);
479 fprintf(fout, "\nEncoder Global Options:\n");
480 arg_show_usage(fout, global_args);
481 fprintf(fout, "\nRate Control Options:\n");
482 arg_show_usage(fout, rc_args);
James Zernfd8c78c2017-11-27 16:37:33 -0800483 fprintf(fout, "\nKeyframe Placement Options:\n");
484 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700485#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800486 fprintf(fout, "\nAV1 Specific Options:\n");
Bohan Li67c3ce02021-02-25 12:10:29 -0800487 arg_show_usage(fout, av1_ctrl_args);
488 arg_show_usage(fout, av1_key_val_args);
hui su82331e02015-08-16 18:21:56 -0700489#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800490 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700491 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700492 " The desired precision of timestamps in the output, expressed\n"
493 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800494 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400495
James Zernfd8c78c2017-11-27 16:37:33 -0800496 const int num_encoder = get_aom_encoder_count();
497 for (int i = 0; i < num_encoder; ++i) {
Elliott Karpilovsky67e84d42020-04-26 16:03:39 -0700498 aom_codec_iface_t *encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700499 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700500 fprintf(fout, " %-6s - %s %s\n", get_short_name_by_aom_encoder(encoder),
501 aom_codec_iface_name(encoder), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800502 }
James Zernfd8c78c2017-11-27 16:37:33 -0800503 fprintf(fout, "\n ");
504 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
505}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400506
James Zernfd8c78c2017-11-27 16:37:33 -0800507void usage_exit(void) {
508 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700509 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400510}
511
Yaowu Xuf883b422016-08-30 14:01:10 -0700512#if CONFIG_AV1_ENCODER
513#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
Bohan Li67c3ce02021-02-25 12:10:29 -0800514#define ARG_KEY_VAL_CNT_MAX NELEMENTS(av1_key_val_args)
John Koleszara9c75972012-11-08 17:09:30 -0800515#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800516
James Zerndba73762014-05-10 17:44:12 -0700517#if !CONFIG_WEBM_IO
518typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700519struct WebmOutputContext {
520 int debug;
521};
James Zerndba73762014-05-10 17:44:12 -0700522#endif
523
John Koleszar9e50ed72012-02-15 12:39:38 -0800524/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800525struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700526 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700527 const char *out_fn;
528 const char *stats_fn;
clang-format6c4d83e2016-08-08 19:03:30 -0700529 stereo_format_t stereo_fmt;
530 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
531 int arg_ctrl_cnt;
Bohan Li67c3ce02021-02-25 12:10:29 -0800532 const char *arg_key_vals[ARG_KEY_VAL_CNT_MAX][2];
Bohan Li77edd8c2021-02-04 16:07:34 -0800533 int arg_key_val_cnt;
clang-format6c4d83e2016-08-08 19:03:30 -0700534 int write_webm;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700535 const char *film_grain_filename;
Cyril Concolato6c788832017-10-30 16:30:35 -0700536 int write_ivf;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700537 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700538 int use_16bit_internal;
sdeng29c62152019-12-09 12:44:18 -0800539#if CONFIG_TUNE_VMAF
540 const char *vmaf_model_path;
541#endif
Cheng Chen9164dee2021-04-19 23:11:38 -0700542 const char *partition_info_path;
Cheng Chen567038a2022-12-22 16:45:02 -0800543 unsigned int enable_rate_guide_deltaq;
544 const char *rate_distribution_info;
Elliott Karpilovskyb2d82582021-02-02 22:49:56 -0800545 aom_color_range_t color_range;
Bohan Li86117812021-07-14 14:08:59 -0700546 const char *two_pass_input;
547 const char *two_pass_output;
548 int two_pass_width;
549 int two_pass_height;
John Koleszar9e50ed72012-02-15 12:39:38 -0800550};
551
John Koleszar6ad3b742012-11-06 12:02:42 -0800552struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700553 int index;
554 struct stream_state *next;
555 struct stream_config config;
556 FILE *file;
557 struct rate_hist *rate_hist;
558 struct WebmOutputContext webm_ctx;
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +0530559 uint64_t psnr_sse_total[2];
560 uint64_t psnr_samples_total[2];
561 double psnr_totals[2][4];
562 int psnr_count[2];
clang-format6c4d83e2016-08-08 19:03:30 -0700563 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700565 unsigned int frames_out;
566 uint64_t cx_time;
567 size_t nbytes;
568 stats_io_t stats;
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 struct aom_image *img;
570 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700571 int mismatch_seen;
Tom Finegan02b2a842018-08-24 13:50:00 -0700572 unsigned int chroma_subsampling_x;
573 unsigned int chroma_subsampling_y;
Bohan Li86117812021-07-14 14:08:59 -0700574 const char *orig_out_fn;
575 unsigned int orig_width;
576 unsigned int orig_height;
Bohan Li15b3c6d2021-07-27 17:27:20 -0700577 int orig_write_webm;
578 int orig_write_ivf;
Bohan Lib5c7e342021-11-03 13:32:03 -0700579 char tmp_out_fn[1000];
John Koleszar9e50ed72012-02-15 12:39:38 -0800580};
581
clang-format6c4d83e2016-08-08 19:03:30 -0700582static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700583 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800584 if (rat->den < 0) {
585 rat->num *= -1;
586 rat->den *= -1;
587 }
John Koleszar1b27e932012-04-25 17:08:56 -0700588
clang-format6c4d83e2016-08-08 19:03:30 -0700589 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700590
clang-format6c4d83e2016-08-08 19:03:30 -0700591 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700592}
593
Ryan44276df2019-10-29 17:11:45 -0700594static void init_config(cfg_options_t *config) {
595 memset(config, 0, sizeof(cfg_options_t));
596 config->super_block_size = 0; // Dynamic
597 config->max_partition_size = 128;
598 config->min_partition_size = 4;
599 config->disable_trellis_quant = 3;
600}
Ryan44276df2019-10-29 17:11:45 -0700601
elliottkb63b5712018-09-17 11:20:20 -0700602/* Parses global config arguments into the AvxEncoderConfig. Note that
603 * argv is modified and overwrites all parsed arguments.
604 */
Ryan Leic8b6dd62019-10-23 20:01:26 -0700605static void parse_global_config(struct AvxEncoderConfig *global, char ***argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700606 char **argi, **argj;
607 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 const int num_encoder = get_aom_encoder_count();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100609 char **argv_local = (char **)*argv;
clang-format6c4d83e2016-08-08 19:03:30 -0700610 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800611
John Koleszar6ad3b742012-11-06 12:02:42 -0800612 /* Initialize default parameters */
613 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700614 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700615 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700616 global->color_type = I420;
elliottkb63b5712018-09-17 11:20:20 -0700617 global->csp = AOM_CSP_UNKNOWN;
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +0530618 global->show_psnr = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400619
Ryan44276df2019-10-29 17:11:45 -0700620 int cfg_included = 0;
621 init_config(&global->encoder_config);
Ryan44276df2019-10-29 17:11:45 -0700622
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100623 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700624 arg.argv_step = 1;
Ryan44276df2019-10-29 17:11:45 -0700625
Bohan Lic1d42fe2020-12-21 13:27:47 -0800626 if (arg_match(&arg, &g_av1_codec_arg_defs.use_cfg, argi)) {
Wan-Teh Changb8804bb2020-09-17 12:32:40 -0700627 if (!cfg_included) {
628 parse_cfg(arg.val, &global->encoder_config);
629 cfg_included = 1;
630 }
Bohan Lic1d42fe2020-12-21 13:27:47 -0800631 } else if (arg_match(&arg, &g_av1_codec_arg_defs.help, argi)) {
James Zernfd8c78c2017-11-27 16:37:33 -0800632 show_help(stdout, 0);
633 exit(EXIT_SUCCESS);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800634 } else if (arg_match(&arg, &g_av1_codec_arg_defs.codecarg, argi)) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700635 global->codec = get_aom_encoder_by_short_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800636 if (!global->codec)
637 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800638 } else if (arg_match(&arg, &g_av1_codec_arg_defs.passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800639 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400640
Bohan Li445fdf62021-06-03 16:16:00 -0700641 if (global->passes < 1 || global->passes > 3)
John Koleszar6ad3b742012-11-06 12:02:42 -0800642 die("Error: Invalid number of passes (%d)\n", global->passes);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800643 } else if (arg_match(&arg, &g_av1_codec_arg_defs.pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800644 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400645
Bohan Li445fdf62021-06-03 16:16:00 -0700646 if (global->pass < 1 || global->pass > 3)
clang-format6c4d83e2016-08-08 19:03:30 -0700647 die("Error: Invalid pass selected (%d)\n", global->pass);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800648 } else if (arg_match(&arg,
649 &g_av1_codec_arg_defs.input_chroma_sample_position,
650 argi)) {
elliottkb63b5712018-09-17 11:20:20 -0700651 global->csp = arg_parse_enum(&arg);
652 /* Flag is used by later code as well, preserve it. */
653 argj++;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800654 } else if (arg_match(&arg, &g_av1_codec_arg_defs.usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800655 global->usage = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800656 } else if (arg_match(&arg, &g_av1_codec_arg_defs.good_dl, argi)) {
kyslov7b9d0d62018-12-21 11:12:26 -0800657 global->usage = AOM_USAGE_GOOD_QUALITY; // Good quality usage
Bohan Lic1d42fe2020-12-21 13:27:47 -0800658 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rt_dl, argi)) {
kyslov7b9d0d62018-12-21 11:12:26 -0800659 global->usage = AOM_USAGE_REALTIME; // Real-time usage
Jingning Hanc5d38e02021-02-11 14:40:27 -0800660 } else if (arg_match(&arg, &g_av1_codec_arg_defs.ai_dl, argi)) {
661 global->usage = AOM_USAGE_ALL_INTRA; // All intra usage
Jerome Jiangd116cab2022-01-21 13:17:02 -0800662 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_nv12, argi)) {
663 global->color_type = NV12;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800664 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_yv12, argi)) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700665 global->color_type = YV12;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800666 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i420, argi)) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700667 global->color_type = I420;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800668 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i422, argi)) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700669 global->color_type = I422;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800670 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i444, argi)) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700671 global->color_type = I444;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800672 } else if (arg_match(&arg, &g_av1_codec_arg_defs.quietarg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800673 global->quiet = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800674 } else if (arg_match(&arg, &g_av1_codec_arg_defs.verbosearg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800675 global->verbose = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800676 } else if (arg_match(&arg, &g_av1_codec_arg_defs.limit, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800677 global->limit = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800678 } else if (arg_match(&arg, &g_av1_codec_arg_defs.skip, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800679 global->skip_frames = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800680 } else if (arg_match(&arg, &g_av1_codec_arg_defs.psnrarg, argi)) {
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +0530681 if (arg.val)
682 global->show_psnr = arg_parse_int(&arg);
683 else
684 global->show_psnr = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800685 } else if (arg_match(&arg, &g_av1_codec_arg_defs.recontest, argi)) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800686 global->test_decode = arg_parse_enum_or_int(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800687 } else if (arg_match(&arg, &g_av1_codec_arg_defs.framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800688 global->framerate = arg_parse_rational(&arg);
689 validate_positive_rational(arg.name, &global->framerate);
690 global->have_framerate = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800691 } else if (arg_match(&arg, &g_av1_codec_arg_defs.debugmode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800692 global->debug = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800693 } else if (arg_match(&arg, &g_av1_codec_arg_defs.q_hist_n, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800694 global->show_q_hist_buckets = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800695 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_hist_n, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800696 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -0800697 } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warnings, argi)) {
Tom Finegan249366b2013-11-25 12:05:19 -0800698 global->disable_warnings = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800699 } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warning_prompt,
700 argi)) {
Tom Finegan249366b2013-11-25 12:05:19 -0800701 global->disable_warning_prompt = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800702 } else {
John Koleszarc6b90392012-07-13 15:21:29 -0700703 argj++;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800704 }
John Koleszarc6b90392012-07-13 15:21:29 -0700705 }
706
John Koleszar6ad3b742012-11-06 12:02:42 -0800707 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700708 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800709 if (global->pass > global->passes) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -0700710 aom_tools_warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
711 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800712 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800713 }
John Koleszarc6b90392012-07-13 15:21:29 -0700714 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800715 /* Validate global config */
716 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700717#if CONFIG_AV1_ENCODER
718 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800719 // encoder
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700720 if (global->codec != NULL)
721 global->passes =
722 (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0 &&
723 global->usage != AOM_USAGE_REALTIME)
724 ? 2
725 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800726#else
727 global->passes = 1;
728#endif
729 }
kyslov7b9d0d62018-12-21 11:12:26 -0800730
731 if (global->usage == AOM_USAGE_REALTIME && global->passes > 1) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -0700732 aom_tools_warn("Enforcing one-pass encoding in realtime mode\n");
Wan-Teh Chang7a20d102021-04-13 12:18:34 -0700733 if (global->pass > 1)
734 die("Error: Invalid --pass=%d for one-pass encoding\n", global->pass);
kyslov7b9d0d62018-12-21 11:12:26 -0800735 global->passes = 1;
736 }
Jingning Han14bfd942021-02-11 15:18:29 -0800737
738 if (global->usage == AOM_USAGE_ALL_INTRA && global->passes > 1) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -0700739 aom_tools_warn("Enforcing one-pass encoding in all intra mode\n");
Jingning Han14bfd942021-02-11 15:18:29 -0800740 global->passes = 1;
741 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800742}
743
elliottkb63b5712018-09-17 11:20:20 -0700744static void open_input_file(struct AvxInputContext *input,
745 aom_chroma_sample_position_t csp) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800746 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700747 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
748 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800749
clang-format6c4d83e2016-08-08 19:03:30 -0700750 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800751
John Koleszar25b6e9f2013-02-12 21:17:56 -0800752 if (!fseeko(input->file, 0, SEEK_END)) {
753 /* Input file is seekable. Figure out how long it is, so we can get
754 * progress info.
755 */
756 input->length = ftello(input->file);
757 rewind(input->file);
758 }
759
Frank Galligan09acd262015-06-01 10:20:58 -0700760 /* Default to 1:1 pixel aspect ratio. */
761 input->pixel_aspect_ratio.numerator = 1;
762 input->pixel_aspect_ratio.denominator = 1;
763
John Koleszar6ad3b742012-11-06 12:02:42 -0800764 /* For RAW input sources, these bytes will applied on the first frame
765 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700766 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800767 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
768 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400769
clang-format6c4d83e2016-08-08 19:03:30 -0700770 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
elliottkb63b5712018-09-17 11:20:20 -0700771 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
John Koleszar8dd82872013-05-06 11:01:35 -0700772 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800773 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800774 input->width = input->y4m.pic_w;
775 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700776 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
777 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800778 input->framerate.numerator = input->y4m.fps_n;
779 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700781 input->bit_depth = input->y4m.bit_depth;
Elliott Karpilovskyb2d82582021-02-02 22:49:56 -0800782 input->color_range = input->y4m.color_range;
John Koleszar6ad3b742012-11-06 12:02:42 -0800783 } else
784 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800785 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800786 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800787 } else {
788 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700789 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800790}
791
Yaowu Xuf883b422016-08-30 14:01:10 -0700792static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800793 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700794 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800795}
796
Yaowu Xuf883b422016-08-30 14:01:10 -0700797static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800798 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800799 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800800
John Koleszar6ad3b742012-11-06 12:02:42 -0800801 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700802 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800803 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700804 }
805
John Koleszar6ad3b742012-11-06 12:02:42 -0800806 if (prev) {
807 memcpy(stream, prev, sizeof(*stream));
808 stream->index++;
809 prev->next = stream;
810 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800812
John Koleszar6ad3b742012-11-06 12:02:42 -0800813 /* Populate encoder configuration */
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700814 res = aom_codec_enc_config_default(global->codec, &stream->config.cfg,
815 global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800817
John Koleszar6ad3b742012-11-06 12:02:42 -0800818 /* Change the default timebase to a high enough value so that the
819 * encoder will always create strictly increasing timestamps.
820 */
821 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800822
John Koleszar6ad3b742012-11-06 12:02:42 -0800823 /* Never use the library's default resolution, require it be parsed
824 * from the file or set on the command line.
825 */
826 stream->config.cfg.g_w = 0;
827 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800828
John Koleszar6ad3b742012-11-06 12:02:42 -0800829 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800830 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -0700831 stream->config.write_ivf = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700832
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700833#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700834 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700835 stream->webm_ctx.last_pts_ns = -1;
836 stream->webm_ctx.writer = NULL;
837 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700838#endif
Johann87c40b32012-03-01 16:12:53 -0800839
John Koleszar6ad3b742012-11-06 12:02:42 -0800840 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700841 stream->webm_ctx.debug = global->debug;
Ryan44276df2019-10-29 17:11:45 -0700842 memcpy(&stream->config.cfg.encoder_cfg, &global->encoder_config,
843 sizeof(stream->config.cfg.encoder_cfg));
John Koleszar6ad3b742012-11-06 12:02:42 -0800844 }
John Koleszar9e50ed72012-02-15 12:39:38 -0800845
John Koleszar6ad3b742012-11-06 12:02:42 -0800846 /* Output files must be specified for each stream */
847 stream->config.out_fn = NULL;
Bohan Li86117812021-07-14 14:08:59 -0700848 stream->config.two_pass_input = NULL;
849 stream->config.two_pass_output = NULL;
850 stream->config.two_pass_width = 0;
851 stream->config.two_pass_height = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800852
John Koleszar6ad3b742012-11-06 12:02:42 -0800853 stream->next = NULL;
854 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800855}
856
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +0000857static void set_config_arg_ctrls(struct stream_config *config, int key,
858 const struct arg *arg) {
859 int j;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700860 if (key == AV1E_SET_FILM_GRAIN_TABLE) {
861 config->film_grain_filename = arg->val;
862 return;
863 }
864
Hui Su2aa492c2019-03-12 15:18:18 -0700865 // For target level, the settings should accumulate rather than overwrite,
866 // so we simply append it.
867 if (key == AV1E_SET_TARGET_SEQ_LEVEL_IDX) {
868 j = config->arg_ctrl_cnt;
Bohan Li67c3ce02021-02-25 12:10:29 -0800869 assert(j < ARG_CTRL_CNT_MAX);
Hui Su2aa492c2019-03-12 15:18:18 -0700870 config->arg_ctrls[j][0] = key;
871 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
872 ++config->arg_ctrl_cnt;
873 return;
874 }
875
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +0000876 /* Point either to the next free element or the first instance of this
877 * control.
878 */
879 for (j = 0; j < config->arg_ctrl_cnt; j++)
880 if (config->arg_ctrls[j][0] == key) break;
881
882 /* Update/insert */
Bohan Li67c3ce02021-02-25 12:10:29 -0800883 assert(j < ARG_CTRL_CNT_MAX);
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +0000884 config->arg_ctrls[j][0] = key;
885 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
Wei-Ting Line6961e72018-08-15 17:43:33 -0700886
887 if (key == AOME_SET_ENABLEAUTOALTREF && config->arg_ctrls[j][1] > 1) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -0700888 aom_tools_warn(
889 "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
Wei-Ting Line6961e72018-08-15 17:43:33 -0700890 config->arg_ctrls[j][1] = 1;
891 }
Bohan Li67c3ce02021-02-25 12:10:29 -0800892
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +0000893 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
894}
895
Bohan Li77edd8c2021-02-04 16:07:34 -0800896static void set_config_arg_key_vals(struct stream_config *config,
Bohan Li67c3ce02021-02-25 12:10:29 -0800897 const char *name, const struct arg *arg) {
Bohan Li77edd8c2021-02-04 16:07:34 -0800898 int j;
Bohan Li67c3ce02021-02-25 12:10:29 -0800899 const char *val = arg->val;
Bohan Li77edd8c2021-02-04 16:07:34 -0800900 // For target level, the settings should accumulate rather than overwrite,
901 // so we simply append it.
902 if (strcmp(name, "target-seq-level-idx") == 0) {
903 j = config->arg_key_val_cnt;
Bohan Li67c3ce02021-02-25 12:10:29 -0800904 assert(j < ARG_KEY_VAL_CNT_MAX);
Bohan Li77edd8c2021-02-04 16:07:34 -0800905 config->arg_key_vals[j][0] = name;
906 config->arg_key_vals[j][1] = val;
907 ++config->arg_key_val_cnt;
908 return;
909 }
910
911 /* Point either to the next free element or the first instance of this
Bohan Li0b822be2021-04-13 13:30:05 -0700912 * option.
Bohan Li77edd8c2021-02-04 16:07:34 -0800913 */
Bohan Li0b822be2021-04-13 13:30:05 -0700914 for (j = 0; j < config->arg_key_val_cnt; j++)
Bohan Li77edd8c2021-02-04 16:07:34 -0800915 if (strcmp(name, config->arg_key_vals[j][0]) == 0) break;
916
917 /* Update/insert */
Bohan Li67c3ce02021-02-25 12:10:29 -0800918 assert(j < ARG_KEY_VAL_CNT_MAX);
Bohan Li77edd8c2021-02-04 16:07:34 -0800919 config->arg_key_vals[j][0] = name;
920 config->arg_key_vals[j][1] = val;
921
Bohan Li67c3ce02021-02-25 12:10:29 -0800922 if (strcmp(name, g_av1_codec_arg_defs.auto_altref.long_name) == 0) {
923 int auto_altref = arg_parse_int(arg);
924 if (auto_altref > 1) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -0700925 aom_tools_warn(
926 "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
Bohan Li67c3ce02021-02-25 12:10:29 -0800927 config->arg_key_vals[j][1] = "1";
928 }
929 }
930
Bohan Li77edd8c2021-02-04 16:07:34 -0800931 if (j == config->arg_key_val_cnt) config->arg_key_val_cnt++;
932}
933
Yaowu Xuf883b422016-08-30 14:01:10 -0700934static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -0700935 struct stream_state *stream, char **argv) {
936 char **argi, **argj;
937 struct arg arg;
Bohan Li67c3ce02021-02-25 12:10:29 -0800938 static const arg_def_t **ctrl_args = no_args;
939 static const arg_def_t **key_val_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -0700940 static const int *ctrl_args_map = NULL;
941 struct stream_config *config = &stream->config;
942 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400943 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800944
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800945 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -0800946 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700947#if CONFIG_AV1_ENCODER
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700948 } else if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700949 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
950 // Consider to expand this set for AV1 encoder control.
Wan-Teh Chang491889e2022-01-10 15:25:20 -0800951#if __STDC_VERSION__ >= 201112L
952 _Static_assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map),
953 "The av1_ctrl_args and av1_arg_ctrl_map arrays must be of "
954 "the same size.");
955#else
956 assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map));
957#endif
Bohan Li67c3ce02021-02-25 12:10:29 -0800958 ctrl_args = av1_ctrl_args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700959 ctrl_args_map = av1_arg_ctrl_map;
Bohan Li67c3ce02021-02-25 12:10:29 -0800960 key_val_args = av1_key_val_args;
Jingning Han3ee6db62015-08-05 19:00:31 -0700961#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700962 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400963
John Koleszarc6b90392012-07-13 15:21:29 -0700964 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700965 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400966
John Koleszar6ad3b742012-11-06 12:02:42 -0800967 /* Once we've found an end-of-stream marker (--) we want to continue
968 * shifting arguments but not consuming them.
969 */
970 if (eos_mark_found) {
971 argj++;
972 continue;
973 } else if (!strcmp(*argj, "--")) {
974 eos_mark_found = 1;
975 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -0800976 }
977
Bohan Lic1d42fe2020-12-21 13:27:47 -0800978 if (arg_match(&arg, &g_av1_codec_arg_defs.outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800979 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400980 if (!webm_forced) {
981 const size_t out_fn_len = strlen(config->out_fn);
982 if (out_fn_len >= 4 &&
983 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
984 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -0700985 config->write_ivf = 1;
Tom Finegan61b71ab2018-03-22 15:58:38 -0700986 } else if (out_fn_len >= 4 &&
987 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
Cyril Concolato6c788832017-10-30 16:30:35 -0700988 config->write_webm = 0;
989 config->write_ivf = 0;
990 }
Tristan Matthewsed858d62017-07-24 15:33:44 -0400991 }
Bohan Lic1d42fe2020-12-21 13:27:47 -0800992 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800993 config->stats_fn = arg.val;
Bohan Lic1d42fe2020-12-21 13:27:47 -0800994 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_webm, argi)) {
Johannb50e5182015-01-30 15:05:14 -0800995#if CONFIG_WEBM_IO
996 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400997 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -0800998#else
999 die("Error: --webm specified but webm is disabled.");
1000#endif
Bohan Lic1d42fe2020-12-21 13:27:47 -08001001 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001002 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001003 config->write_ivf = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001004 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_obu, argi)) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001005 config->write_webm = 0;
1006 config->write_ivf = 0;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001007 } else if (arg_match(&arg, &g_av1_codec_arg_defs.threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001008 config->cfg.g_threads = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001009 } else if (arg_match(&arg, &g_av1_codec_arg_defs.profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001010 config->cfg.g_profile = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001011 } else if (arg_match(&arg, &g_av1_codec_arg_defs.width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001012 config->cfg.g_w = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001013 } else if (arg_match(&arg, &g_av1_codec_arg_defs.height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001014 config->cfg.g_h = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001015 } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_width,
1016 argi)) {
Imdad Sardharwalla102c8652018-02-23 16:35:13 +00001017 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001018 } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_height,
1019 argi)) {
Imdad Sardharwalla102c8652018-02-23 16:35:13 +00001020 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001021 } else if (arg_match(&arg, &g_av1_codec_arg_defs.bitdeptharg, argi)) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001022 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001023 } else if (arg_match(&arg, &g_av1_codec_arg_defs.inbitdeptharg, argi)) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001024 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001025 } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_x,
1026 argi)) {
Tom Finegan02b2a842018-08-24 13:50:00 -07001027 stream->chroma_subsampling_x = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001028 } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_y,
1029 argi)) {
Tom Finegan02b2a842018-08-24 13:50:00 -07001030 stream->chroma_subsampling_y = arg_parse_uint(&arg);
James Zerndba73762014-05-10 17:44:12 -07001031#if CONFIG_WEBM_IO
Bohan Lic1d42fe2020-12-21 13:27:47 -08001032 } else if (arg_match(&arg, &g_av1_codec_arg_defs.stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001033 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001034#endif
Bohan Lic1d42fe2020-12-21 13:27:47 -08001035 } else if (arg_match(&arg, &g_av1_codec_arg_defs.timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001036 config->cfg.g_timebase = arg_parse_rational(&arg);
1037 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001038 } else if (arg_match(&arg, &g_av1_codec_arg_defs.global_error_resilient,
1039 argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001040 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001041 } else if (arg_match(&arg, &g_av1_codec_arg_defs.lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001042 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001043 } else if (arg_match(&arg, &g_av1_codec_arg_defs.large_scale_tile, argi)) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001044 config->cfg.large_scale_tile = arg_parse_uint(&arg);
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001045 if (config->cfg.large_scale_tile) {
1046 global->codec = get_aom_encoder_by_short_name("av1");
1047 }
Bohan Lic1d42fe2020-12-21 13:27:47 -08001048 } else if (arg_match(&arg, &g_av1_codec_arg_defs.monochrome, argi)) {
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001049 config->cfg.monochrome = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001050 } else if (arg_match(&arg, &g_av1_codec_arg_defs.full_still_picture_hdr,
1051 argi)) {
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001052 config->cfg.full_still_picture_hdr = 1;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001053 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_16bit_internal,
1054 argi)) {
Debargha Mukherjee48215712020-05-14 15:51:10 -07001055 config->use_16bit_internal = CONFIG_AV1_HIGHBITDEPTH;
1056 if (!config->use_16bit_internal) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001057 aom_tools_warn("%s option ignored with CONFIG_AV1_HIGHBITDEPTH=0.\n",
1058 arg.name);
Debargha Mukherjee48215712020-05-14 15:51:10 -07001059 }
Bohan Lic1d42fe2020-12-21 13:27:47 -08001060 } else if (arg_match(&arg, &g_av1_codec_arg_defs.dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001061 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001062 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_mode, argi)) {
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001063 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001064 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_denominator,
1065 argi)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001066 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001067 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_kf_denominator,
1068 argi)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001069 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001070 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_mode, argi)) {
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001071 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001072 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_denominator,
1073 argi)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001074 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001075 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_denominator,
1076 argi)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001077 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001078 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_qthresh, argi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001079 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001080 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_qthresh,
1081 argi)) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001082 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001083 } else if (arg_match(&arg, &g_av1_codec_arg_defs.end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001084 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001085 } else if (arg_match(&arg, &g_av1_codec_arg_defs.target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001086 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001087 } else if (arg_match(&arg, &g_av1_codec_arg_defs.min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001088 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001089 } else if (arg_match(&arg, &g_av1_codec_arg_defs.max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001091 } else if (arg_match(&arg, &g_av1_codec_arg_defs.undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001093 } else if (arg_match(&arg, &g_av1_codec_arg_defs.overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001094 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001095 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001096 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001097 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001098 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001099 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001100 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001101 } else if (arg_match(&arg, &g_av1_codec_arg_defs.bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001102 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001103 if (global->passes < 2)
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001104 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001105 } else if (arg_match(&arg, &g_av1_codec_arg_defs.minsection_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001106 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1107
1108 if (global->passes < 2)
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001109 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001110 } else if (arg_match(&arg, &g_av1_codec_arg_defs.maxsection_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001111 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1112
1113 if (global->passes < 2)
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001114 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001115 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fwd_kf_enabled, argi)) {
Sarah Parker93c03142018-05-22 13:35:45 -07001116 config->cfg.fwd_kf_enabled = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001117 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001118 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001119 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001120 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001121 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001122 config->cfg.kf_mode = AOM_KF_DISABLED;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001123 } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_dist, argi)) {
Tarek AMARAc9813852018-03-05 18:40:18 -05001124 config->cfg.sframe_dist = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001125 } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_mode, argi)) {
Tarek AMARAc9813852018-03-05 18:40:18 -05001126 config->cfg.sframe_mode = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001127 } else if (arg_match(&arg, &g_av1_codec_arg_defs.save_as_annexb, argi)) {
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001128 config->cfg.save_as_annexb = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001129 } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_width, argi)) {
Dominic Symes26ad0b22017-10-01 16:35:13 +02001130 config->cfg.tile_width_count =
1131 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001132 } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_height, argi)) {
Dominic Symes26ad0b22017-10-01 16:35:13 +02001133 config->cfg.tile_height_count =
1134 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
sdeng29c62152019-12-09 12:44:18 -08001135#if CONFIG_TUNE_VMAF
Sai Dengd2f082e2021-02-09 15:19:52 -08001136 } else if (arg_match(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argi)) {
sdeng29c62152019-12-09 12:44:18 -08001137 config->vmaf_model_path = arg.val;
1138#endif
Cheng Chen9164dee2021-04-19 23:11:38 -07001139 } else if (arg_match(&arg, &g_av1_codec_arg_defs.partition_info_path,
1140 argi)) {
1141 config->partition_info_path = arg.val;
Cheng Chen567038a2022-12-22 16:45:02 -08001142 } else if (arg_match(&arg, &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
1143 argi)) {
1144 config->enable_rate_guide_deltaq = arg_parse_uint(&arg);
1145 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_distribution_info,
1146 argi)) {
1147 config->rate_distribution_info = arg.val;
Bohan Lic1d42fe2020-12-21 13:27:47 -08001148 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_fixed_qp_offsets,
1149 argi)) {
Urvang Joshid6a3d662020-01-30 12:27:38 -08001150 config->cfg.use_fixed_qp_offsets = arg_parse_uint(&arg);
Bohan Lic1d42fe2020-12-21 13:27:47 -08001151 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fixed_qp_offsets, argi)) {
Urvang Joshid6a3d662020-01-30 12:27:38 -08001152 config->cfg.use_fixed_qp_offsets = 1;
kyslov067fdf92019-05-08 12:17:53 -07001153 } else if (global->usage == AOM_USAGE_REALTIME &&
Bohan Lic1d42fe2020-12-21 13:27:47 -08001154 arg_match(&arg, &g_av1_codec_arg_defs.enable_restoration,
1155 argi)) {
kyslov067fdf92019-05-08 12:17:53 -07001156 if (arg_parse_uint(&arg) == 1) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001157 aom_tools_warn("non-zero %s option ignored in realtime mode.\n",
1158 arg.name);
kyslov067fdf92019-05-08 12:17:53 -07001159 }
Bohan Li86117812021-07-14 14:08:59 -07001160 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_input, argi)) {
1161 config->two_pass_input = arg.val;
1162 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_output, argi)) {
1163 config->two_pass_output = arg.val;
1164 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_width, argi)) {
1165 config->two_pass_width = arg_parse_int(&arg);
1166 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_height, argi)) {
1167 config->two_pass_height = arg_parse_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001168 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001169 int i, match = 0;
Bohan Li67c3ce02021-02-25 12:10:29 -08001170 // check if the control ID API supports this arg
1171 if (ctrl_args_map) {
1172 for (i = 0; ctrl_args[i]; i++) {
1173 if (arg_match(&arg, ctrl_args[i], argi)) {
1174 match = 1;
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001175 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
Bohan Li67c3ce02021-02-25 12:10:29 -08001176 break;
John Koleszar6ad3b742012-11-06 12:02:42 -08001177 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001178 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001179 }
Bohan Li67c3ce02021-02-25 12:10:29 -08001180 if (!match) {
1181 // check if the key & value API supports this arg
1182 for (i = 0; key_val_args[i]; i++) {
1183 if (arg_match(&arg, key_val_args[i], argi)) {
1184 match = 1;
1185 set_config_arg_key_vals(config, key_val_args[i]->long_name, &arg);
1186 break;
1187 }
Bohan Li77edd8c2021-02-04 16:07:34 -08001188 }
1189 }
clang-format6c4d83e2016-08-08 19:03:30 -07001190 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001191 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001192 }
Debargha Mukherjee48215712020-05-14 15:51:10 -07001193 config->use_16bit_internal |= config->cfg.g_bit_depth > AOM_BITS_8;
1194
Fyodor Kyslov8fbe1b92020-07-29 15:54:14 -07001195 if (global->usage == AOM_USAGE_REALTIME && config->cfg.g_lag_in_frames != 0) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001196 aom_tools_warn("non-zero lag-in-frames option ignored in realtime mode.\n");
Fyodor Kyslov8fbe1b92020-07-29 15:54:14 -07001197 config->cfg.g_lag_in_frames = 0;
1198 }
Jingning Han14bfd942021-02-11 15:18:29 -08001199
1200 if (global->usage == AOM_USAGE_ALL_INTRA) {
1201 if (config->cfg.g_lag_in_frames != 0) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001202 aom_tools_warn(
1203 "non-zero lag-in-frames option ignored in all intra mode.\n");
Jingning Han14bfd942021-02-11 15:18:29 -08001204 config->cfg.g_lag_in_frames = 0;
1205 }
1206 if (config->cfg.kf_max_dist != 0) {
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07001207 aom_tools_warn(
Jingning Han14bfd942021-02-11 15:18:29 -08001208 "non-zero max key frame distance option ignored in all intra "
1209 "mode.\n");
1210 config->cfg.kf_max_dist = 0;
1211 }
1212 }
Bohan Li445fdf62021-06-03 16:16:00 -07001213
1214 // set the passes field using key & val API
1215 if (config->arg_key_val_cnt >= ARG_KEY_VAL_CNT_MAX) {
1216 die("Not enough buffer for the key & value API.");
1217 }
1218 config->arg_key_vals[config->arg_key_val_cnt][0] = "passes";
1219 switch (global->passes) {
1220 case 0: config->arg_key_vals[config->arg_key_val_cnt][1] = "0"; break;
1221 case 1: config->arg_key_vals[config->arg_key_val_cnt][1] = "1"; break;
1222 case 2: config->arg_key_vals[config->arg_key_val_cnt][1] = "2"; break;
1223 case 3: config->arg_key_vals[config->arg_key_val_cnt][1] = "3"; break;
1224 default: die("Invalid value of --passes.");
1225 }
1226 config->arg_key_val_cnt++;
1227
Bohan Li86117812021-07-14 14:08:59 -07001228 // set the two_pass_output field
1229 if (!config->two_pass_output && global->passes == 3) {
Bohan Lib5c7e342021-11-03 13:32:03 -07001230 // If not specified, set the name of two_pass_output file here.
Bohan Li86117812021-07-14 14:08:59 -07001231 snprintf(stream->tmp_out_fn, sizeof(stream->tmp_out_fn),
Bohan Lib5c7e342021-11-03 13:32:03 -07001232 "%.980s_pass2_%d.ivf", stream->config.out_fn, stream->index);
Bohan Li86117812021-07-14 14:08:59 -07001233 stream->config.two_pass_output = stream->tmp_out_fn;
1234 }
1235 if (config->two_pass_output) {
1236 config->arg_key_vals[config->arg_key_val_cnt][0] = "two-pass-output";
1237 config->arg_key_vals[config->arg_key_val_cnt][1] = config->two_pass_output;
1238 config->arg_key_val_cnt++;
1239 }
1240
John Koleszar6ad3b742012-11-06 12:02:42 -08001241 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001242}
1243
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001244#define FOREACH_STREAM(iterator, list) \
1245 for (struct stream_state *iterator = list; iterator; \
1246 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001247
Alex Conversed66bd222014-02-21 10:52:09 -08001248static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001249 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001250 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001251 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001252
John Koleszar6ad3b742012-11-06 12:02:42 -08001253 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001254 fatal(
1255 "Stream %d: Specify stream dimensions with --width (-w) "
1256 " and --height (-h)",
1257 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001258
elliottk7a162472018-09-25 15:06:24 -07001259 /* Even if bit depth is set on the command line flag to be lower,
1260 * it is upgraded to at least match the input bit depth.
1261 */
1262 assert(stream->config.cfg.g_input_bit_depth <=
1263 (unsigned int)stream->config.cfg.g_bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001264
John Koleszar6ad3b742012-11-06 12:02:42 -08001265 for (streami = stream; streami; streami = streami->next) {
1266 /* All streams require output files */
1267 if (!streami->config.out_fn)
1268 fatal("Stream %d: Output file is required (specify with -o)",
1269 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001270
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 /* Check for two streams outputting to the same file */
1272 if (streami != stream) {
1273 const char *a = stream->config.out_fn;
1274 const char *b = streami->config.out_fn;
1275 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1276 fatal("Stream %d: duplicate output file (from stream %d)",
1277 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001278 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001279
1280 /* Check for two streams sharing a stats file. */
1281 if (streami != stream) {
1282 const char *a = stream->config.stats_fn;
1283 const char *b = streami->config.stats_fn;
1284 if (a && b && !strcmp(a, b))
1285 fatal("Stream %d: duplicate stats file (from stream %d)",
1286 streami->index, stream->index);
1287 }
1288 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001289}
1290
clang-format6c4d83e2016-08-08 19:03:30 -07001291static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001292 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001293 if (!stream->config.cfg.g_w) {
1294 if (!stream->config.cfg.g_h)
1295 stream->config.cfg.g_w = w;
1296 else
1297 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1298 }
1299 if (!stream->config.cfg.g_h) {
1300 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1301 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001302}
1303
clang-format6c4d83e2016-08-08 19:03:30 -07001304static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001305 switch (t) {
1306 case FILE_TYPE_RAW: return "RAW";
1307 case FILE_TYPE_Y4M: return "Y4M";
1308 default: return "Other";
1309 }
1310}
1311
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001312static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001313 struct AvxEncoderConfig *global,
1314 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001315#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001316 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001317
John Koleszar6ad3b742012-11-06 12:02:42 -08001318 if (stream->index == 0) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001319 fprintf(stderr, "Codec: %s\n", aom_codec_iface_name(global->codec));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001320 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001321 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001322 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001323 }
1324 if (stream->next || stream->index)
1325 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1326 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001327 fprintf(stderr, "Coding path: %s\n",
1328 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001329 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001330
John Koleszar6ad3b742012-11-06 12:02:42 -08001331 SHOW(g_usage);
1332 SHOW(g_threads);
1333 SHOW(g_profile);
1334 SHOW(g_w);
1335 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001336 SHOW(g_bit_depth);
1337 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001338 SHOW(g_timebase.num);
1339 SHOW(g_timebase.den);
1340 SHOW(g_error_resilient);
1341 SHOW(g_pass);
1342 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001343 SHOW(large_scale_tile);
John Koleszar6ad3b742012-11-06 12:02:42 -08001344 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001345 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001346 SHOW(rc_resize_denominator);
1347 SHOW(rc_resize_kf_denominator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001348 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001349 SHOW(rc_superres_denominator);
1350 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001351 SHOW(rc_superres_qthresh);
1352 SHOW(rc_superres_kf_qthresh);
John Koleszar6ad3b742012-11-06 12:02:42 -08001353 SHOW(rc_end_usage);
1354 SHOW(rc_target_bitrate);
1355 SHOW(rc_min_quantizer);
1356 SHOW(rc_max_quantizer);
1357 SHOW(rc_undershoot_pct);
1358 SHOW(rc_overshoot_pct);
1359 SHOW(rc_buf_sz);
1360 SHOW(rc_buf_initial_sz);
1361 SHOW(rc_buf_optimal_sz);
1362 SHOW(rc_2pass_vbr_bias_pct);
1363 SHOW(rc_2pass_vbr_minsection_pct);
1364 SHOW(rc_2pass_vbr_maxsection_pct);
Sarah Parker93c03142018-05-22 13:35:45 -07001365 SHOW(fwd_kf_enabled);
John Koleszar6ad3b742012-11-06 12:02:42 -08001366 SHOW(kf_mode);
1367 SHOW(kf_min_dist);
1368 SHOW(kf_max_dist);
Ryan44276df2019-10-29 17:11:45 -07001369
Ryan44276df2019-10-29 17:11:45 -07001370#define SHOW_PARAMS(field) \
1371 fprintf(stderr, " %-28s = %d\n", #field, \
1372 stream->config.cfg.encoder_cfg.field)
Yaowu Xuc851d282020-06-15 15:28:22 -07001373 if (global->encoder_config.init_by_cfg_file) {
1374 SHOW_PARAMS(super_block_size);
1375 SHOW_PARAMS(max_partition_size);
1376 SHOW_PARAMS(min_partition_size);
1377 SHOW_PARAMS(disable_ab_partition_type);
1378 SHOW_PARAMS(disable_rect_partition_type);
1379 SHOW_PARAMS(disable_1to4_partition_type);
1380 SHOW_PARAMS(disable_flip_idtx);
1381 SHOW_PARAMS(disable_cdef);
1382 SHOW_PARAMS(disable_lr);
1383 SHOW_PARAMS(disable_obmc);
1384 SHOW_PARAMS(disable_warp_motion);
1385 SHOW_PARAMS(disable_global_motion);
1386 SHOW_PARAMS(disable_dist_wtd_comp);
1387 SHOW_PARAMS(disable_diff_wtd_comp);
1388 SHOW_PARAMS(disable_inter_intra_comp);
1389 SHOW_PARAMS(disable_masked_comp);
1390 SHOW_PARAMS(disable_one_sided_comp);
1391 SHOW_PARAMS(disable_palette);
1392 SHOW_PARAMS(disable_intrabc);
1393 SHOW_PARAMS(disable_cfl);
1394 SHOW_PARAMS(disable_smooth_intra);
1395 SHOW_PARAMS(disable_filter_intra);
1396 SHOW_PARAMS(disable_dual_filter);
1397 SHOW_PARAMS(disable_intra_angle_delta);
1398 SHOW_PARAMS(disable_intra_edge_filter);
1399 SHOW_PARAMS(disable_tx_64x64);
1400 SHOW_PARAMS(disable_smooth_inter_intra);
1401 SHOW_PARAMS(disable_inter_inter_wedge);
1402 SHOW_PARAMS(disable_inter_intra_wedge);
1403 SHOW_PARAMS(disable_paeth_intra);
1404 SHOW_PARAMS(disable_trellis_quant);
1405 SHOW_PARAMS(disable_ref_frame_mv);
1406 SHOW_PARAMS(reduced_reference_set);
1407 SHOW_PARAMS(reduced_tx_type_set);
1408 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001409}
1410
John Koleszar9e50ed72012-02-15 12:39:38 -08001411static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001412 struct AvxEncoderConfig *global,
Elliott Karpilovsky4e55eb72020-05-14 14:16:24 -07001413 const struct AvxRational *pixel_aspect_ratio,
1414 const char *encoder_settings) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001415 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001417
Yaowu Xuf883b422016-08-30 14:01:10 -07001418 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001419
John Koleszar6ad3b742012-11-06 12:02:42 -08001420 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001421
clang-format6c4d83e2016-08-08 19:03:30 -07001422 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001423
John Koleszar6ad3b742012-11-06 12:02:42 -08001424 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1425 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001426
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001427#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001428 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001429 stream->webm_ctx.stream = stream->file;
Tom Fineganbb8157b2018-09-20 15:33:11 -07001430 if (write_webm_file_header(&stream->webm_ctx, &stream->encoder, cfg,
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001431 stream->config.stereo_fmt,
1432 get_fourcc_by_aom_encoder(global->codec),
Elliott Karpilovsky4e55eb72020-05-14 14:16:24 -07001433 pixel_aspect_ratio, encoder_settings) != 0) {
Tom Fineganbb8157b2018-09-20 15:33:11 -07001434 fatal("WebM writer initialization failed.");
1435 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001436 }
James Zernc8e5a772016-02-11 18:53:50 -08001437#else
1438 (void)pixel_aspect_ratio;
Elliott Karpilovsky4e55eb72020-05-14 14:16:24 -07001439 (void)encoder_settings;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001440#endif
1441
Tom Finegan61b71ab2018-03-22 15:58:38 -07001442 if (!stream->config.write_webm && stream->config.write_ivf) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001443 ivf_write_file_header(stream->file, cfg,
1444 get_fourcc_by_aom_encoder(global->codec), 0);
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001445 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001446}
1447
John Koleszar9e50ed72012-02-15 12:39:38 -08001448static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001449 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001450 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001451
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001453
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001454#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001455 if (stream->config.write_webm) {
Tom Fineganbb8157b2018-09-20 15:33:11 -07001456 if (write_webm_file_footer(&stream->webm_ctx) != 0) {
1457 fatal("WebM writer finalization failed.");
1458 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001459 }
1460#endif
1461
Tom Finegan61b71ab2018-03-22 15:58:38 -07001462 if (!stream->config.write_webm && stream->config.write_ivf) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001463 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001464 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001465 stream->frames_out);
1466 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001467
John Koleszar6ad3b742012-11-06 12:02:42 -08001468 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001469}
1470
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001471static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001472 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001473 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001474 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001475 fatal("Failed to open statistics store");
1476 } else {
1477 if (!stats_open_mem(&stream->stats, pass))
1478 fatal("Failed to open statistics store");
1479 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001480
Bohan Li445fdf62021-06-03 16:16:00 -07001481 if (global->passes == 1) {
1482 stream->config.cfg.g_pass = AOM_RC_ONE_PASS;
1483 } else {
1484 switch (pass) {
1485 case 0: stream->config.cfg.g_pass = AOM_RC_FIRST_PASS; break;
1486 case 1: stream->config.cfg.g_pass = AOM_RC_SECOND_PASS; break;
1487 case 2: stream->config.cfg.g_pass = AOM_RC_THIRD_PASS; break;
1488 default: fatal("Failed to set pass");
1489 }
1490 }
1491
Pengchong Jinf349b072014-07-14 09:13:38 -07001492 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001493 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001494 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001495
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 stream->cx_time = 0;
1497 stream->nbytes = 0;
1498 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001499}
1500
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001501static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001503 int i;
1504 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001505
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301506 flags |= (global->show_psnr >= 1) ? AOM_CODEC_USE_PSNR : 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001508
John Koleszar6ad3b742012-11-06 12:02:42 -08001509 /* Construct Encoder Context */
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001510 aom_codec_enc_init(&stream->encoder, global->codec, &stream->config.cfg,
1511 flags);
John Koleszar6ad3b742012-11-06 12:02:42 -08001512 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001513
John Koleszar6ad3b742012-11-06 12:02:42 -08001514 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1515 int ctrl = stream->config.arg_ctrls[i][0];
1516 int value = stream->config.arg_ctrls[i][1];
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001517 if (aom_codec_control(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001518 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001519
John Koleszar6ad3b742012-11-06 12:02:42 -08001520 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1521 }
sdeng29c62152019-12-09 12:44:18 -08001522
Bohan Li77edd8c2021-02-04 16:07:34 -08001523 for (i = 0; i < stream->config.arg_key_val_cnt; i++) {
1524 const char *name = stream->config.arg_key_vals[i][0];
1525 const char *val = stream->config.arg_key_vals[i][1];
1526 if (aom_codec_set_option(&stream->encoder, name, val))
1527 fprintf(stderr, "Error: Tried to set option %s = %s\n", name, val);
1528
1529 ctx_exit_on_error(&stream->encoder, "Failed to set codec option");
1530 }
1531
sdeng29c62152019-12-09 12:44:18 -08001532#if CONFIG_TUNE_VMAF
1533 if (stream->config.vmaf_model_path) {
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001534 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_VMAF_MODEL_PATH,
1535 stream->config.vmaf_model_path);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301536 ctx_exit_on_error(&stream->encoder, "Failed to set vmaf model path");
sdeng29c62152019-12-09 12:44:18 -08001537 }
1538#endif
Cheng Chen9164dee2021-04-19 23:11:38 -07001539 if (stream->config.partition_info_path) {
1540 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1541 AV1E_SET_PARTITION_INFO_PATH,
1542 stream->config.partition_info_path);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301543 ctx_exit_on_error(&stream->encoder, "Failed to set partition info path");
Cheng Chen9164dee2021-04-19 23:11:38 -07001544 }
Cheng Chen567038a2022-12-22 16:45:02 -08001545 if (stream->config.enable_rate_guide_deltaq) {
1546 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1547 AV1E_ENABLE_RATE_GUIDE_DELTAQ,
1548 stream->config.enable_rate_guide_deltaq);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301549 ctx_exit_on_error(&stream->encoder, "Failed to enable rate guide deltaq");
Cheng Chen567038a2022-12-22 16:45:02 -08001550 }
1551 if (stream->config.rate_distribution_info) {
1552 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1553 AV1E_SET_RATE_DISTRIBUTION_INFO,
1554 stream->config.rate_distribution_info);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301555 ctx_exit_on_error(&stream->encoder, "Failed to set rate distribution info");
Cheng Chen567038a2022-12-22 16:45:02 -08001556 }
sdeng29c62152019-12-09 12:44:18 -08001557
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07001558 if (stream->config.film_grain_filename) {
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001559 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_FILM_GRAIN_TABLE,
1560 stream->config.film_grain_filename);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301561 ctx_exit_on_error(&stream->encoder, "Failed to set film grain table");
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07001562 }
Elliott Karpilovskyb2d82582021-02-02 22:49:56 -08001563 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_COLOR_RANGE,
1564 stream->config.color_range);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05301565 ctx_exit_on_error(&stream->encoder, "Failed to set color range");
John Koleszar6ad3b742012-11-06 12:02:42 -08001566
Tom Fineganba02c242017-05-16 15:01:54 -07001567#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001568 if (global->test_decode != TEST_DECODE_OFF) {
Elliott Karpilovsky67e84d42020-04-26 16:03:39 -07001569 aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001570 get_short_name_by_aom_encoder(global->codec));
Debargha Mukherjee48215712020-05-14 15:51:10 -07001571 aom_codec_dec_cfg_t cfg = { 0, 0, 0, !stream->config.use_16bit_internal };
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001572 aom_codec_dec_init(&stream->decoder, decoder, &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001573
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07001574 if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001575 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_TILE_MODE,
1576 stream->config.cfg.large_scale_tile);
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001577 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1578
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001579 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1D_SET_IS_ANNEXB,
1580 stream->config.cfg.save_as_annexb);
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001581 ctx_exit_on_error(&stream->decoder, "Failed to set is_annexb");
1582
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001583 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_ROW,
1584 -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001585 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1586
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001587 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_COL,
1588 -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001589 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1590 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001591 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001592#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001593}
1594
Wan-Teh Chang5c9bc412021-04-13 10:12:20 -07001595// Convert the input image 'img' to a monochrome image. The Y plane of the
1596// output image is a shallow copy of the Y plane of the input image, therefore
1597// the input image must remain valid for the lifetime of the output image. The U
1598// and V planes of the output image are set to null pointers. The output image
1599// format is AOM_IMG_FMT_I420 because libaom does not have AOM_IMG_FMT_I400.
1600static void convert_image_to_monochrome(const struct aom_image *img,
1601 struct aom_image *monochrome_img) {
1602 *monochrome_img = *img;
1603 monochrome_img->fmt = AOM_IMG_FMT_I420;
1604 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1605 monochrome_img->fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1606 }
1607 monochrome_img->monochrome = 1;
1608 monochrome_img->csp = AOM_CSP_UNKNOWN;
1609 monochrome_img->x_chroma_shift = 1;
1610 monochrome_img->y_chroma_shift = 1;
1611 monochrome_img->planes[AOM_PLANE_U] = NULL;
1612 monochrome_img->planes[AOM_PLANE_V] = NULL;
1613 monochrome_img->stride[AOM_PLANE_U] = 0;
1614 monochrome_img->stride[AOM_PLANE_V] = 0;
1615 monochrome_img->sz = 0;
1616 monochrome_img->bps = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
1617 monochrome_img->img_data = NULL;
1618 monochrome_img->img_data_owner = 0;
1619 monochrome_img->self_allocd = 0;
1620}
1621
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001622static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001624 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001625 aom_codec_pts_t frame_start, next_frame_start;
1626 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1627 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001628
clang-format6c4d83e2016-08-08 19:03:30 -07001629 frame_start =
1630 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1631 cfg->g_timebase.num / global->framerate.num;
1632 next_frame_start =
1633 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1634 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001635
Yaowu Xud3e7c682017-12-21 14:08:25 -08001636 /* Scale if necessary */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001637 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001639 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001640 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001641 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1642 exit(EXIT_FAILURE);
1643 }
1644#if CONFIG_LIBYUV
1645 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001646 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001647 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001648 }
clang-format6c4d83e2016-08-08 19:03:30 -07001649 I420Scale_16(
Mirko Bonadei8a634182018-08-03 23:09:09 -07001650 (uint16_t *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1651 (uint16_t *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1652 (uint16_t *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1653 img->d_w, img->d_h, (uint16_t *)stream->img->planes[AOM_PLANE_Y],
Yaowu Xuf883b422016-08-30 14:01:10 -07001654 stream->img->stride[AOM_PLANE_Y] / 2,
Mirko Bonadei8a634182018-08-03 23:09:09 -07001655 (uint16_t *)stream->img->planes[AOM_PLANE_U],
Yaowu Xuf883b422016-08-30 14:01:10 -07001656 stream->img->stride[AOM_PLANE_U] / 2,
Mirko Bonadei8a634182018-08-03 23:09:09 -07001657 (uint16_t *)stream->img->planes[AOM_PLANE_V],
Yaowu Xuf883b422016-08-30 14:01:10 -07001658 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001659 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001660 img = stream->img;
1661#else
clang-format6c4d83e2016-08-08 19:03:30 -07001662 stream->encoder.err = 1;
1663 ctx_exit_on_error(&stream->encoder,
1664 "Stream %d: Failed to encode frame.\n"
Johanne07a6752018-01-10 12:47:44 -08001665 "libyuv is required for scaling but is currently "
1666 "disabled.\n"
1667 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1668 "cmake.\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001669 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001670#endif
1671 }
1672 }
John Koleszar34882b92012-03-01 12:50:40 -08001673 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001674 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001675 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1676 exit(EXIT_FAILURE);
1677 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001678#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001679 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001680 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001681 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001682 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001683 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1684 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1685 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1686 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1687 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1688 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001689 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001690 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001691#else
1692 stream->encoder.err = 1;
1693 ctx_exit_on_error(&stream->encoder,
1694 "Stream %d: Failed to encode frame.\n"
1695 "Scaling disabled in this configuration. \n"
1696 "To enable, configure with --enable-libyuv\n",
1697 stream->index);
1698#endif
John Koleszar34882b92012-03-01 12:50:40 -08001699 }
1700
Wan-Teh Chang5c9bc412021-04-13 10:12:20 -07001701 struct aom_image monochrome_img;
1702 if (img && cfg->monochrome) {
1703 convert_image_to_monochrome(img, &monochrome_img);
1704 img = &monochrome_img;
1705 }
1706
Yaowu Xuf883b422016-08-30 14:01:10 -07001707 aom_usec_timer_start(&timer);
1708 aom_codec_encode(&stream->encoder, img, frame_start,
Sean DuBois47cc2552018-01-23 07:44:16 +00001709 (uint32_t)(next_frame_start - frame_start), 0);
Yaowu Xuf883b422016-08-30 14:01:10 -07001710 aom_usec_timer_mark(&timer);
1711 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001712 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1713 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001714}
1715
John Koleszar6ad3b742012-11-06 12:02:42 -08001716static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001717 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001718 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001719
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001720 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AOME_GET_LAST_QUANTIZER_64,
1721 &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001722 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1723 stream->counts[q]++;
1724 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001725}
1726
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001727static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001728 struct AvxEncoderConfig *global, int *got_data) {
1729 const aom_codec_cx_pkt_t *pkt;
1730 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1731 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001732
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001733 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001734 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001735 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001736 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001737
John Koleszar6ad3b742012-11-06 12:02:42 -08001738 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001739 case AOM_CODEC_CX_FRAME_PKT:
Urvang Joshi22043ef2018-09-27 12:41:17 -07001740 ++stream->frames_out;
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001741 if (!global->quiet)
1742 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001743
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001744 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001745#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001746 if (stream->config.write_webm) {
Tom Fineganbb8157b2018-09-20 15:33:11 -07001747 if (write_webm_block(&stream->webm_ctx, cfg, pkt) != 0) {
1748 fatal("WebM writer failed.");
1749 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001750 }
1751#endif
1752 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001753 if (stream->config.write_ivf) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001754 if (pkt->data.frame.partition_id <= 0) {
1755 ivf_header_pos = ftello(stream->file);
1756 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001757
Cyril Concolato6c788832017-10-30 16:30:35 -07001758 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1759 } else {
1760 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001761
Urvang Joshi22043ef2018-09-27 12:41:17 -07001762 const FileOffset currpos = ftello(stream->file);
1763 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1764 ivf_write_frame_size(stream->file, fsize);
1765 fseeko(stream->file, currpos, SEEK_SET);
John Koleszar6ad3b742012-11-06 12:02:42 -08001766 }
1767 }
1768
clang-format6c4d83e2016-08-08 19:03:30 -07001769 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1770 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001771 }
1772 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001773
John Koleszar9e50ed72012-02-15 12:39:38 -08001774 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001775#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001776 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001777 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Wan-Teh Chang84b22b82018-05-16 14:56:28 -07001778 pkt->data.frame.sz, NULL);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001779 if (stream->decoder.err) {
1780 warn_or_exit_on_error(&stream->decoder,
1781 global->test_decode == TEST_DECODE_FATAL,
1782 "Failed to decode frame %d in stream %d",
1783 stream->frames_out + 1, stream->index);
1784 stream->mismatch_seen = stream->frames_out + 1;
1785 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001786 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001787#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001788 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001789 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001790 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001791 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001792 pkt->data.twopass_stats.sz);
1793 stream->nbytes += pkt->data.raw.sz;
1794 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001795 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001796
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301797 if (global->show_psnr >= 1) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001798 int i;
1799
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301800 stream->psnr_sse_total[0] += pkt->data.psnr.sse[0];
1801 stream->psnr_samples_total[0] += pkt->data.psnr.samples[0];
John Koleszar6ad3b742012-11-06 12:02:42 -08001802 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001803 if (!global->quiet)
1804 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301805 stream->psnr_totals[0][i] += pkt->data.psnr.psnr[i];
John Koleszar6ad3b742012-11-06 12:02:42 -08001806 }
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301807 stream->psnr_count[0]++;
1808
1809#if CONFIG_AV1_HIGHBITDEPTH
1810 if (stream->config.cfg.g_input_bit_depth <
Aasaipriya Chandran48d580b2020-08-18 14:56:25 +05301811 (unsigned int)stream->config.cfg.g_bit_depth) {
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301812 stream->psnr_sse_total[1] += pkt->data.psnr.sse_hbd[0];
1813 stream->psnr_samples_total[1] += pkt->data.psnr.samples_hbd[0];
1814 for (i = 0; i < 4; i++) {
1815 if (!global->quiet)
1816 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr_hbd[i]);
1817 stream->psnr_totals[1][i] += pkt->data.psnr.psnr_hbd[i];
1818 }
1819 stream->psnr_count[1]++;
1820 }
1821#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001822 }
1823
1824 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001825 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001826 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001827 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001828}
1829
Peng Bineede8352018-04-25 15:52:34 +08001830static void show_psnr(struct stream_state *stream, double peak, int64_t bps) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001831 int i;
1832 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001833
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301834 if (!stream->psnr_count[0]) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001835
John Koleszar6ad3b742012-11-06 12:02:42 -08001836 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301837 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[0], peak,
1838 (double)stream->psnr_sse_total[0]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001839 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001840
John Koleszar6ad3b742012-11-06 12:02:42 -08001841 for (i = 0; i < 4; i++) {
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301842 fprintf(stderr, " %.3f", stream->psnr_totals[0][i] / stream->psnr_count[0]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001843 }
Peng Bineede8352018-04-25 15:52:34 +08001844 if (bps > 0) {
1845 fprintf(stderr, " %7" PRId64 " bps", bps);
1846 }
1847 fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
John Koleszar6ad3b742012-11-06 12:02:42 -08001848 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001849}
1850
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05301851#if CONFIG_AV1_HIGHBITDEPTH
1852static void show_psnr_hbd(struct stream_state *stream, double peak,
1853 int64_t bps) {
1854 int i;
1855 double ovpsnr;
1856 // Compute PSNR based on stream bit depth
1857 if (!stream->psnr_count[1]) return;
1858
1859 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1860 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[1], peak,
1861 (double)stream->psnr_sse_total[1]);
1862 fprintf(stderr, " %.3f", ovpsnr);
1863
1864 for (i = 0; i < 4; i++) {
1865 fprintf(stderr, " %.3f", stream->psnr_totals[1][i] / stream->psnr_count[1]);
1866 }
1867 if (bps > 0) {
1868 fprintf(stderr, " %7" PRId64 " bps", bps);
1869 }
1870 fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1871 fprintf(stderr, "\n");
1872}
1873#endif
1874
John Koleszar25b6e9f2013-02-12 21:17:56 -08001875static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001876 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001877}
1878
clang-format6c4d83e2016-08-08 19:03:30 -07001879static void test_decode(struct stream_state *stream,
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001880 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001881 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001882
clang-format6c4d83e2016-08-08 19:03:30 -07001883 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001884
John Koleszarb3c350a2013-03-13 12:15:43 -07001885 /* Get the internal reference frame */
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07001886 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE,
1887 &enc_img);
1888 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE,
1889 &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001890
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001891 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1892 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1893 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1894 aom_image_t enc_hbd_img;
1895 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1896 enc_img.d_w, enc_img.d_h, 16);
1897 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1898 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001899 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001900 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1901 aom_image_t dec_hbd_img;
1902 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1903 dec_img.d_w, dec_img.d_h, 16);
1904 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1905 dec_img = dec_hbd_img;
1906 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001907 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001908
John Koleszar6ad3b742012-11-06 12:02:42 -08001909 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001910 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001911
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001912 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001913 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001914 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001915 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001916 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001917 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001918 }
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001919 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001920 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001921 "Stream %d: Encode/decode mismatch on frame %d at"
1922 " Y[%d, %d] {%d/%d},"
1923 " U[%d, %d] {%d/%d},"
1924 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001925 stream->index, stream->frames_out, y[0], y[1], y[2],
1926 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
Ronald S. Bultje195172c2012-11-10 16:22:49 -08001927 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001928 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001929
Yaowu Xuf883b422016-08-30 14:01:10 -07001930 aom_img_free(&enc_img);
1931 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001932}
John Koleszarefd54f82012-02-13 16:52:18 -08001933
John Koleszar25b6e9f2013-02-12 21:17:56 -08001934static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001935 int64_t hours;
1936 int64_t mins;
1937 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001938
1939 if (etl >= 0) {
1940 hours = etl / 3600;
1941 etl -= hours * 3600;
1942 mins = etl / 60;
1943 etl -= mins * 60;
1944 secs = etl;
1945
clang-format6c4d83e2016-08-08 19:03:30 -07001946 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1947 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001948 } else {
1949 fprintf(stderr, "[%3s unknown] ", label);
1950 }
1951}
1952
Bohan Li86117812021-07-14 14:08:59 -07001953static void clear_stream_count_state(struct stream_state *stream) {
1954 // PSNR counters
1955 for (int k = 0; k < 2; k++) {
1956 stream->psnr_sse_total[k] = 0;
1957 stream->psnr_samples_total[k] = 0;
1958 for (int i = 0; i < 4; i++) {
1959 stream->psnr_totals[k][i] = 0;
1960 }
1961 stream->psnr_count[k] = 0;
1962 }
1963 // q hist
1964 memset(stream->counts, 0, sizeof(stream->counts));
1965}
1966
1967// aomenc will downscale the second pass if:
1968// 1. the specific pass is not given by commandline (aomenc will perform all
1969// passes)
1970// 2. there are more than 2 passes in total
1971// 3. current pass is the second pass (the parameter pass starts with 0 so
1972// pass == 1)
1973static int pass_need_downscale(int global_pass, int global_passes, int pass) {
1974 return !global_pass && global_passes > 2 && pass == 1;
1975}
1976
Tom Finegan44dd3272013-11-20 17:18:28 -08001977int main(int argc, const char **argv_) {
1978 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001979 aom_image_t raw;
Yaowu Xuf883b422016-08-30 14:01:10 -07001980 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001981 int allocated_raw_shift = 0;
Debargha Mukherjee48215712020-05-14 15:51:10 -07001982 int do_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001983 int input_shift = 0;
Tom Finegan44dd3272013-11-20 17:18:28 -08001984 int frame_avail, got_data;
1985
Yaowu Xuf883b422016-08-30 14:01:10 -07001986 struct AvxInputContext input;
1987 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001988 struct stream_state *streams = NULL;
1989 char **argv, **argi;
1990 uint64_t cx_time = 0;
1991 int stream_cnt = 0;
1992 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001993 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001994
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001995 memset(&input, 0, sizeof(input));
Hui Su94bcbfe2021-01-12 11:50:01 -08001996 memset(&raw, 0, sizeof(raw));
John Koleszar6ad3b742012-11-06 12:02:42 -08001997 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001998
John Koleszar6ad3b742012-11-06 12:02:42 -08001999 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002000 input.framerate.numerator = 30;
2001 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07002002 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07002003 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002004
2005 /* First parse the global configuration values, because we want to apply
2006 * other parameters on top of the default configuration provided by the
2007 * codec.
2008 */
2009 argv = argv_dup(argc - 1, argv_ + 1);
James Zernb79f7052022-04-28 16:26:22 -07002010 if (!argv) {
2011 fprintf(stderr, "Error allocating argument list\n");
2012 return EXIT_FAILURE;
2013 }
Ryan Leic8b6dd62019-10-23 20:01:26 -07002014 parse_global_config(&global, &argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002015
Ryan44276df2019-10-29 17:11:45 -07002016 if (argc < 2) usage_exit();
James Zernfd8c78c2017-11-27 16:37:33 -08002017
Deb Mukherjee090f4d42014-07-16 09:37:13 -07002018 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002019 case I420: input.fmt = AOM_IMG_FMT_I420; break;
2020 case I422: input.fmt = AOM_IMG_FMT_I422; break;
2021 case I444: input.fmt = AOM_IMG_FMT_I444; break;
Yaowu Xuf883b422016-08-30 14:01:10 -07002022 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Jerome Jiangd116cab2022-01-21 13:17:02 -08002023 case NV12: input.fmt = AOM_IMG_FMT_NV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07002024 }
Tom Finegan44dd3272013-11-20 17:18:28 -08002025
John Koleszar6ad3b742012-11-06 12:02:42 -08002026 {
2027 /* Now parse each stream's parameters. Using a local scope here
2028 * due to the use of 'stream' as loop variable in FOREACH_STREAM
2029 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08002030 */
John Koleszar6ad3b742012-11-06 12:02:42 -08002031 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002032
John Koleszar6ad3b742012-11-06 12:02:42 -08002033 do {
2034 stream = new_stream(&global, stream);
2035 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07002036 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08002037 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07002038 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002039
John Koleszarc6b90392012-07-13 15:21:29 -07002040 /* Check for unrecognized options */
2041 for (argi = argv; *argi; argi++)
2042 if (argi[0][0] == '-' && argi[0][1])
2043 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002044
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002045 FOREACH_STREAM(stream, streams) {
2046 check_encoder_config(global.disable_warning_prompt, &global,
2047 &stream->config.cfg);
Yunqing Wanga879ee92019-01-30 10:27:20 -08002048
2049 // If large_scale_tile = 1, only support to output to ivf format.
2050 if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
2051 die("only support ivf output format while large-scale-tile=1\n");
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002052 }
Tom Finegan249366b2013-11-25 12:05:19 -08002053
John Koleszarc6b90392012-07-13 15:21:29 -07002054 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002055 input.filename = argv[0];
Bohan Li86117812021-07-14 14:08:59 -07002056 const char *orig_input_filename = input.filename;
2057 FOREACH_STREAM(stream, streams) {
2058 stream->orig_out_fn = stream->config.out_fn;
2059 stream->orig_width = stream->config.cfg.g_w;
2060 stream->orig_height = stream->config.cfg.g_h;
Bohan Li15b3c6d2021-07-27 17:27:20 -07002061 stream->orig_write_ivf = stream->config.write_ivf;
2062 stream->orig_write_webm = stream->config.write_webm;
Bohan Li86117812021-07-14 14:08:59 -07002063 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002064
James Zernfd8c78c2017-11-27 16:37:33 -08002065 if (!input.filename) {
2066 fprintf(stderr, "No input file specified!\n");
2067 usage_exit();
2068 }
John Koleszar53291892010-10-22 14:48:21 -04002069
John Koleszar8dd82872013-05-06 11:01:35 -07002070 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07002071 if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC)
2072 input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07002073
John Koleszar6ad3b742012-11-06 12:02:42 -08002074 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
Bohan Li86117812021-07-14 14:08:59 -07002075 if (pass > 1) {
2076 FOREACH_STREAM(stream, streams) { clear_stream_count_state(stream); }
2077 }
2078
John Koleszar2bf563c2013-03-11 15:03:00 -07002079 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002080 int64_t estimated_time_left = -1;
2081 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07002082 int64_t lagged_count = 0;
Bohan Li86117812021-07-14 14:08:59 -07002083 const int need_downscale =
2084 pass_need_downscale(global.pass, global.passes, pass);
2085
2086 // Set the output to the specified two-pass output file, and
2087 // restore the width and height to the original values.
2088 FOREACH_STREAM(stream, streams) {
2089 if (need_downscale) {
2090 stream->config.out_fn = stream->config.two_pass_output;
Bohan Li15b3c6d2021-07-27 17:27:20 -07002091 // Libaom currently only supports the ivf format for the third pass.
2092 stream->config.write_ivf = 1;
2093 stream->config.write_webm = 0;
Bohan Li86117812021-07-14 14:08:59 -07002094 } else {
2095 stream->config.out_fn = stream->orig_out_fn;
Bohan Li15b3c6d2021-07-27 17:27:20 -07002096 stream->config.write_ivf = stream->orig_write_ivf;
2097 stream->config.write_webm = stream->orig_write_webm;
Bohan Li86117812021-07-14 14:08:59 -07002098 }
2099 stream->config.cfg.g_w = stream->orig_width;
2100 stream->config.cfg.g_h = stream->orig_height;
2101 }
2102
2103 // For second pass in three-pass encoding, set the input to
2104 // the given two-pass-input file if available. If the scaled input is not
2105 // given, we will attempt to re-scale the original input.
2106 input.filename = orig_input_filename;
2107 const char *two_pass_input = NULL;
2108 if (need_downscale) {
2109 FOREACH_STREAM(stream, streams) {
2110 if (stream->config.two_pass_input) {
2111 two_pass_input = stream->config.two_pass_input;
2112 input.filename = two_pass_input;
2113 break;
2114 }
2115 }
2116 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002117
elliottkb63b5712018-09-17 11:20:20 -07002118 open_input_file(&input, global.csp);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002119
John Koleszar6ad3b742012-11-06 12:02:42 -08002120 /* If the input file doesn't specify its w/h (raw files), try to get
2121 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07002122 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002123 if (!input.width || !input.height) {
Bohan Li86117812021-07-14 14:08:59 -07002124 if (two_pass_input) {
2125 FOREACH_STREAM(stream, streams) {
2126 if (stream->config.two_pass_width && stream->config.two_pass_height) {
2127 input.width = stream->config.two_pass_width;
2128 input.height = stream->config.two_pass_height;
2129 break;
2130 }
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002131 }
Bohan Li86117812021-07-14 14:08:59 -07002132 } else {
2133 FOREACH_STREAM(stream, streams) {
2134 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2135 input.width = stream->config.cfg.g_w;
2136 input.height = stream->config.cfg.g_h;
2137 break;
2138 }
2139 }
2140 }
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002141 }
John Koleszarc6b90392012-07-13 15:21:29 -07002142
John Koleszar6ad3b742012-11-06 12:02:42 -08002143 /* Update stream configurations from the input file's parameters */
Bohan Li86117812021-07-14 14:08:59 -07002144 if (!input.width || !input.height) {
2145 if (two_pass_input) {
2146 fatal(
2147 "Specify downscaled stream dimensions with --two-pass-width "
2148 " and --two-pass-height");
2149 } else {
2150 fatal(
2151 "Specify stream dimensions with --width (-w) "
2152 " and --height (-h)");
2153 }
2154 }
2155
2156 if (need_downscale) {
2157 FOREACH_STREAM(stream, streams) {
2158 if (stream->config.two_pass_width && stream->config.two_pass_height) {
2159 stream->config.cfg.g_w = stream->config.two_pass_width;
2160 stream->config.cfg.g_h = stream->config.two_pass_height;
2161 } else if (two_pass_input) {
2162 stream->config.cfg.g_w = input.width;
2163 stream->config.cfg.g_h = input.height;
2164 } else if (stream->orig_width && stream->orig_height) {
Angie Chiang958912e2021-12-10 17:25:57 -08002165#if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2166 stream->config.cfg.g_w = stream->orig_width;
2167 stream->config.cfg.g_h = stream->orig_height;
2168#else // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
Bohan Li86117812021-07-14 14:08:59 -07002169 stream->config.cfg.g_w = (stream->orig_width + 1) / 2;
2170 stream->config.cfg.g_h = (stream->orig_height + 1) / 2;
Angie Chiang958912e2021-12-10 17:25:57 -08002171#endif // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
Bohan Li86117812021-07-14 14:08:59 -07002172 } else {
Angie Chiang958912e2021-12-10 17:25:57 -08002173#if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2174 stream->config.cfg.g_w = input.width;
2175 stream->config.cfg.g_h = input.height;
2176#else // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
Bohan Li86117812021-07-14 14:08:59 -07002177 stream->config.cfg.g_w = (input.width + 1) / 2;
2178 stream->config.cfg.g_h = (input.height + 1) / 2;
Angie Chiang958912e2021-12-10 17:25:57 -08002179#endif // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
Bohan Li86117812021-07-14 14:08:59 -07002180 }
2181 }
2182 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002183
2184 /* If input file does not specify bit-depth but input-bit-depth parameter
2185 * exists, assume that to be the input bit-depth. However, if the
2186 * input-bit-depth paramter does not exist, assume the input bit-depth
2187 * to be the same as the codec bit-depth.
2188 */
2189 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002190 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002191 if (stream->config.cfg.g_input_bit_depth)
2192 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2193 else
2194 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2195 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002196 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002197 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002198 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002199 FOREACH_STREAM(stream, streams) {
2200 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2201 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002202 }
2203
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002204 FOREACH_STREAM(stream, streams) {
Jerome Jiangd116cab2022-01-21 13:17:02 -08002205 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016 &&
2206 input.fmt != AOM_IMG_FMT_NV12) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07002207 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2208 was selected. */
2209 switch (stream->config.cfg.g_profile) {
2210 case 0:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002211 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2212 input.fmt == AOM_IMG_FMT_I44416)) {
Yaowu Xuc1fdb4b2018-03-16 08:44:13 -07002213 if (!stream->config.cfg.monochrome) {
2214 stream->config.cfg.g_profile = 1;
2215 profile_updated = 1;
2216 }
Wan-Teh Chang5c9bc412021-04-13 10:12:20 -07002217 } else if (input.bit_depth == 12 ||
2218 ((input.fmt == AOM_IMG_FMT_I422 ||
2219 input.fmt == AOM_IMG_FMT_I42216) &&
2220 !stream->config.cfg.monochrome)) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002221 stream->config.cfg.g_profile = 2;
2222 profile_updated = 1;
2223 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002224 break;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002225 case 1:
2226 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2227 input.fmt == AOM_IMG_FMT_I42216) {
2228 stream->config.cfg.g_profile = 2;
2229 profile_updated = 1;
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002230 } else if (input.bit_depth < 12 &&
2231 (input.fmt == AOM_IMG_FMT_I420 ||
2232 input.fmt == AOM_IMG_FMT_I42016)) {
2233 stream->config.cfg.g_profile = 0;
2234 profile_updated = 1;
2235 }
2236 break;
2237 case 2:
2238 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2239 input.fmt == AOM_IMG_FMT_I44416)) {
2240 stream->config.cfg.g_profile = 1;
2241 profile_updated = 1;
2242 } else if (input.bit_depth < 12 &&
2243 (input.fmt == AOM_IMG_FMT_I420 ||
2244 input.fmt == AOM_IMG_FMT_I42016)) {
2245 stream->config.cfg.g_profile = 0;
2246 profile_updated = 1;
Tom Finegan02b2a842018-08-24 13:50:00 -07002247 } else if (input.bit_depth == 12 &&
2248 input.file_type == FILE_TYPE_Y4M) {
2249 // Note that here the input file values for chroma subsampling
2250 // are used instead of those from the command line.
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07002251 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2252 AV1E_SET_CHROMA_SUBSAMPLING_X,
2253 input.y4m.dst_c_dec_h >> 1);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05302254 ctx_exit_on_error(&stream->encoder,
2255 "Failed to set chroma subsampling x");
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07002256 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2257 AV1E_SET_CHROMA_SUBSAMPLING_Y,
2258 input.y4m.dst_c_dec_v >> 1);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05302259 ctx_exit_on_error(&stream->encoder,
2260 "Failed to set chroma subsampling y");
Tom Finegan02b2a842018-08-24 13:50:00 -07002261 } else if (input.bit_depth == 12 &&
2262 input.file_type == FILE_TYPE_RAW) {
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07002263 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2264 AV1E_SET_CHROMA_SUBSAMPLING_X,
2265 stream->chroma_subsampling_x);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05302266 ctx_exit_on_error(&stream->encoder,
2267 "Failed to set chroma subsampling x");
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -07002268 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2269 AV1E_SET_CHROMA_SUBSAMPLING_Y,
2270 stream->chroma_subsampling_y);
Satheesh Kumare46f3f22023-12-05 19:34:33 +05302271 ctx_exit_on_error(&stream->encoder,
2272 "Failed to set chroma subsampling y");
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002273 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002274 break;
2275 default: break;
2276 }
2277 }
elliottk7a162472018-09-25 15:06:24 -07002278 /* Automatically set the codec bit depth to match the input bit depth.
2279 * Upgrade the profile if required. */
2280 if (stream->config.cfg.g_input_bit_depth >
2281 (unsigned int)stream->config.cfg.g_bit_depth) {
2282 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2283 if (!global.quiet) {
2284 fprintf(stderr,
2285 "Warning: automatically updating bit depth to %d to "
2286 "match input format.\n",
2287 stream->config.cfg.g_input_bit_depth);
2288 }
2289 }
Debargha Mukherjee48215712020-05-14 15:51:10 -07002290#if !CONFIG_AV1_HIGHBITDEPTH
2291 if (stream->config.cfg.g_bit_depth > 8) {
2292 fatal("Unsupported bit-depth with CONFIG_AV1_HIGHBITDEPTH=0\n");
2293 }
2294#endif // CONFIG_AV1_HIGHBITDEPTH
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002295 if (stream->config.cfg.g_bit_depth > 10) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002296 switch (stream->config.cfg.g_profile) {
2297 case 0:
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002298 case 1:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002299 stream->config.cfg.g_profile = 2;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002300 profile_updated = 1;
2301 break;
2302 default: break;
2303 }
2304 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002305 if (stream->config.cfg.g_bit_depth > 8) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002306 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002307 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002308 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002309 fprintf(stderr,
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002310 "Warning: automatically updating to profile %d to "
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002311 "match input format.\n",
2312 stream->config.cfg.g_profile);
2313 }
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05302314 if ((global.show_psnr == 2) && (stream->config.cfg.g_input_bit_depth ==
2315 stream->config.cfg.g_bit_depth)) {
2316 fprintf(stderr,
2317 "Warning: --psnr==2 and --psnr==1 will provide same "
2318 "results when input bit-depth == stream bit-depth, "
2319 "falling back to default psnr value\n");
2320 global.show_psnr = 1;
2321 }
2322 if (global.show_psnr < 0 || global.show_psnr > 2) {
2323 fprintf(stderr,
2324 "Warning: --psnr can take only 0,1,2 as values,"
2325 "falling back to default psnr value\n");
2326 global.show_psnr = 1;
2327 }
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07002328 /* Set limit */
2329 stream->config.cfg.g_limit = global.limit;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002330 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002331
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002332 FOREACH_STREAM(stream, streams) {
2333 set_stream_dimensions(stream, input.width, input.height);
Elliott Karpilovskyb2d82582021-02-02 22:49:56 -08002334 stream->config.color_range = input.color_range;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002335 }
2336 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002337
John Koleszar6ad3b742012-11-06 12:02:42 -08002338 /* Ensure that --passes and --pass are consistent. If --pass is set and
Bohan Li86117812021-07-14 14:08:59 -07002339 * --passes >= 2, ensure --fpf was set.
John Koleszar6ad3b742012-11-06 12:02:42 -08002340 */
Bohan Li86117812021-07-14 14:08:59 -07002341 if (global.pass > 0 && global.pass <= 3 && global.passes >= 2) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002342 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002343 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002344 die("Stream %d: Must specify --fpf when --pass=%d"
Bohan Li86117812021-07-14 14:08:59 -07002345 " and --passes=%d\n",
2346 stream->index, global.pass, global.passes);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002347 }
2348 }
John Koleszar3245d462010-06-10 17:10:12 -04002349
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002350#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002351 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002352 if (stream->config.write_webm) {
2353 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002354 stream->config.write_ivf = 0;
Wan-Teh Chang3668fed2021-08-30 15:57:37 -07002355 aom_tools_warn("aomenc compiled w/o WebM support. Writing OBU stream.");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002356 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002357 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002358#endif
2359
Debargha Mukherjee4a707a82018-03-02 11:19:43 -08002360 /* Use the frame rate from the file only if none was specified
2361 * on the command-line.
2362 */
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002363 if (!global.have_framerate) {
2364 global.framerate.num = input.framerate.numerator;
2365 global.framerate.den = input.framerate.denominator;
2366 }
2367 FOREACH_STREAM(stream, streams) {
2368 stream->config.cfg.g_timebase.den = global.framerate.num;
2369 stream->config.cfg.g_timebase.num = global.framerate.den;
2370 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002371 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002372 if (global.verbose && pass == 0) {
2373 FOREACH_STREAM(stream, streams) {
2374 show_stream_config(stream, &global, &input);
2375 }
2376 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002377
2378 if (pass == (global.pass ? global.pass - 1 : 0)) {
Hui Su94bcbfe2021-01-12 11:50:01 -08002379 // The Y4M reader does its own allocation.
2380 if (input.file_type != FILE_TYPE_Y4M) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002381 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
Hui Su94bcbfe2021-01-12 11:50:01 -08002382 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002383 FOREACH_STREAM(stream, streams) {
2384 stream->rate_hist =
2385 init_rate_histogram(&stream->config.cfg, &global.framerate);
2386 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002387 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002388
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002389 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
Tom Fineganec0833f2018-09-19 14:28:20 -07002390 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002391 FOREACH_STREAM(stream, streams) {
Elliott Karpilovsky4e55eb72020-05-14 14:16:24 -07002392 char *encoder_settings = NULL;
2393#if CONFIG_WEBM_IO
Elliott Karpilovskye4d1bba2020-05-19 16:51:41 -07002394 // Test frameworks may compare outputs from different versions, but only
2395 // wish to check for bitstream changes. The encoder-settings tag, however,
2396 // can vary if the version is updated, even if no encoder algorithm
2397 // changes were made. To work around this issue, do not output
2398 // the encoder-settings tag when --debug is enabled (which is the flag
2399 // that test frameworks should use, when they want deterministic output
2400 // from the container format).
2401 if (stream->config.write_webm && !stream->webm_ctx.debug) {
Elliott Karpilovsky4e55eb72020-05-14 14:16:24 -07002402 encoder_settings = extract_encoder_settings(
2403 aom_codec_version_str(), argv_, argc, input.filename);
2404 if (encoder_settings == NULL) {
2405 fprintf(
2406 stderr,
2407 "Warning: unable to extract encoder settings. Continuing...\n");
2408 }
2409 }
2410#endif
2411 open_output_file(stream, &global, &input.pixel_aspect_ratio,
2412 encoder_settings);
2413 free(encoder_settings);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002414 }
Tom Fineganec0833f2018-09-19 14:28:20 -07002415
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07002416 if (strcmp(get_short_name_by_aom_encoder(global.codec), "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002417 // Check to see if at least one stream uses 16 bit internal.
2418 // Currently assume that the bit_depths for all streams using
2419 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002420 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002421 if (stream->config.use_16bit_internal) {
Debargha Mukherjee48215712020-05-14 15:51:10 -07002422 do_16bit_internal = 1;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002423 }
Yaowu Xu913867b2018-01-23 18:27:59 -08002424 input_shift = (int)stream->config.cfg.g_bit_depth -
2425 stream->config.cfg.g_input_bit_depth;
James Zernf2658a32022-02-09 10:18:38 -08002426 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002427 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002428
John Koleszarc6b90392012-07-13 15:21:29 -07002429 frame_avail = 1;
2430 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002431
John Koleszarc6b90392012-07-13 15:21:29 -07002432 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002433 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002434
John Koleszar6ad3b742012-11-06 12:02:42 -08002435 if (!global.limit || frames_in < global.limit) {
2436 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002437
clang-format6c4d83e2016-08-08 19:03:30 -07002438 if (frame_avail) frames_in++;
2439 seen_frames =
2440 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002441
John Koleszar6ad3b742012-11-06 12:02:42 -08002442 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002443 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002444 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2445
John Koleszar6ad3b742012-11-06 12:02:42 -08002446 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002447 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2448 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002449 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002450 fprintf(stderr, "frame %4d ", frames_in);
2451
clang-format6c4d83e2016-08-08 19:03:30 -07002452 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002453 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002454 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002455 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002456 print_time("ETA", estimated_time_left);
Christopher Degawaeda9c452021-07-23 11:04:48 -05002457 // mingw-w64 gcc does not match msvc for stderr buffering behavior
2458 // and uses line buffering, thus the progress output is not
2459 // real-time. The fflush() is here to make sure the progress output
2460 // is sent out while the clip is being processed.
Christopher Degawa697613f2021-07-15 10:41:44 -05002461 fflush(stderr);
John Koleszar3245d462010-06-10 17:10:12 -04002462 }
2463
hui su251e1512016-10-03 15:48:43 -07002464 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002465 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002466 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002467
John Koleszar6ad3b742012-11-06 12:02:42 -08002468 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002469 aom_image_t *frame_to_encode;
Debargha Mukherjee48215712020-05-14 15:51:10 -07002470 if (input_shift || (do_16bit_internal && input.bit_depth == 8)) {
2471 assert(do_16bit_internal);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002472 // Input bit depth and stream bit depth do not match, so up
2473 // shift frame to stream bit depth
2474 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002475 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002476 input.width, input.height, 32);
2477 allocated_raw_shift = 1;
2478 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002479 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002480 frame_to_encode = &raw_shift;
2481 } else {
2482 frame_to_encode = &raw;
2483 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002484 aom_usec_timer_start(&timer);
Debargha Mukherjee48215712020-05-14 15:51:10 -07002485 if (do_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002486 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002487 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002488 if (stream->config.use_16bit_internal)
2489 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002490 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002491 else
2492 assert(0);
James Zernf2658a32022-02-09 10:18:38 -08002493 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002494 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002495 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002496 FOREACH_STREAM(stream, streams) {
2497 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2498 frames_in);
2499 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002500 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002501 aom_usec_timer_mark(&timer);
2502 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002503
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002504 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002505
John Koleszar0ea50ce2010-05-18 11:58:33 -04002506 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002507 FOREACH_STREAM(stream, streams) {
2508 get_cx_data(stream, &global, &got_data);
2509 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002510
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002511 if (!got_data && input.length && streams != NULL &&
2512 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002513 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002514 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002515 int64_t remaining;
2516 int64_t rate;
2517
2518 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002519 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002520
2521 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002522 remaining = 1000 * (global.limit - global.skip_frames -
2523 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002524 } else {
James Zerne3578af2014-04-17 10:47:08 -07002525 const int64_t input_pos = ftello(input.file);
2526 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002527 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002528
2529 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002530 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002531 }
2532
clang-format6c4d83e2016-08-08 19:03:30 -07002533 average_rate =
2534 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002535 estimated_time_left = average_rate ? remaining / average_rate : -1;
2536 }
2537
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002538 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2539 FOREACH_STREAM(stream, streams) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +00002540 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002541 }
2542 }
John Koleszarc6b90392012-07-13 15:21:29 -07002543 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002544
John Koleszarc6b90392012-07-13 15:21:29 -07002545 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002546 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002547 }
2548
clang-format6c4d83e2016-08-08 19:03:30 -07002549 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002550
Deb Mukherjeea160d722014-09-30 21:56:33 -07002551 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002552 FOREACH_STREAM(stream, streams) {
Peng Bineede8352018-04-25 15:52:34 +08002553 const int64_t bpf =
2554 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
2555 const int64_t bps = bpf * global.framerate.num / global.framerate.den;
Johann3c30fb42018-02-08 14:33:20 -08002556 fprintf(stderr,
2557 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2558 "b/f %7" PRId64
2559 "b/s"
2560 " %7" PRId64 " %s (%.2f fps)\033[K\n",
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002561 pass + 1, global.passes, frames_in, stream->frames_out,
Peng Bineede8352018-04-25 15:52:34 +08002562 (int64_t)stream->nbytes, bpf, bps,
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002563 stream->cx_time > 9999999 ? stream->cx_time / 1000
2564 : stream->cx_time,
2565 stream->cx_time > 9999999 ? "ms" : "us",
2566 usec_to_fps(stream->cx_time, seen_frames));
Christopher Degawaeda9c452021-07-23 11:04:48 -05002567 // This instance of cr does not need fflush as it is followed by a
2568 // newline in the same string.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002569 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002570 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002571
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05302572 if (global.show_psnr >= 1) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07002573 if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002574 FOREACH_STREAM(stream, streams) {
Peng Bineede8352018-04-25 15:52:34 +08002575 int64_t bps = 0;
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05302576 if (global.show_psnr == 1) {
2577 if (stream->psnr_count[0] && seen_frames && global.framerate.den) {
2578 bps = (int64_t)stream->nbytes * 8 *
2579 (int64_t)global.framerate.num / global.framerate.den /
2580 seen_frames;
2581 }
2582 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
2583 bps);
Peng Bineede8352018-04-25 15:52:34 +08002584 }
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05302585 if (global.show_psnr == 2) {
2586#if CONFIG_AV1_HIGHBITDEPTH
2587 if (stream->config.cfg.g_input_bit_depth <
Aasaipriya Chandran48d580b2020-08-18 14:56:25 +05302588 (unsigned int)stream->config.cfg.g_bit_depth)
Aasaipriya Chandran4c2b30e2020-08-07 12:13:49 +05302589 show_psnr_hbd(stream, (1 << stream->config.cfg.g_bit_depth) - 1,
2590 bps);
2591#endif
2592 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002593 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002594 } else {
Peng Bineede8352018-04-25 15:52:34 +08002595 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002596 }
2597 }
John Koleszarc6b90392012-07-13 15:21:29 -07002598
Bohan Lie8033432022-01-21 13:46:49 -08002599 if (pass == global.passes - 1) {
2600 FOREACH_STREAM(stream, streams) {
Wan-Teh Chang977be332022-06-06 17:35:19 -07002601 int num_operating_points;
2602 int levels[32];
2603 int target_levels[32];
2604 aom_codec_control(&stream->encoder, AV1E_GET_NUM_OPERATING_POINTS,
2605 &num_operating_points);
Bohan Lie8033432022-01-21 13:46:49 -08002606 aom_codec_control(&stream->encoder, AV1E_GET_SEQ_LEVEL_IDX, levels);
2607 aom_codec_control(&stream->encoder, AV1E_GET_TARGET_SEQ_LEVEL_IDX,
2608 target_levels);
2609
Wan-Teh Chang977be332022-06-06 17:35:19 -07002610 for (int i = 0; i < num_operating_points; i++) {
Bohan Lie8033432022-01-21 13:46:49 -08002611 if (levels[i] > target_levels[i]) {
Bohan Lidebf0862022-11-21 09:55:53 -08002612 if (levels[i] == 31) {
2613 aom_tools_warn(
2614 "Failed to encode to target level %d.%d for operating point "
2615 "%d. The output level is SEQ_LEVEL_MAX",
2616 2 + (target_levels[i] >> 2), target_levels[i] & 3, i);
2617 } else {
2618 aom_tools_warn(
2619 "Failed to encode to target level %d.%d for operating point "
2620 "%d. The output level is %d.%d",
2621 2 + (target_levels[i] >> 2), target_levels[i] & 3, i,
2622 2 + (levels[i] >> 2), levels[i] & 3);
2623 }
Bohan Lie8033432022-01-21 13:46:49 -08002624 }
2625 }
2626 }
2627 }
2628
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002629 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002630
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002631 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002632 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002633 }
2634
John Koleszar6ad3b742012-11-06 12:02:42 -08002635 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002636
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002637 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002638 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002639 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002640 FOREACH_STREAM(stream, streams) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -07002641 close_output_file(stream, get_fourcc_by_aom_encoder(global.codec));
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002642 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002643
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002644 FOREACH_STREAM(stream, streams) {
2645 stats_close(&stream->stats, global.passes - 1);
2646 }
John Koleszarc6b90392012-07-13 15:21:29 -07002647
clang-format6c4d83e2016-08-08 19:03:30 -07002648 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002649 }
2650
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002651 if (global.show_q_hist_buckets) {
2652 FOREACH_STREAM(stream, streams) {
2653 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2654 }
2655 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002656
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002657 if (global.show_rate_hist_buckets) {
2658 FOREACH_STREAM(stream, streams) {
2659 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2660 global.show_rate_hist_buckets);
2661 }
2662 }
2663 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002664
Ronald S. Bultje31214972012-10-11 10:13:48 -07002665#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002666 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2667 * to match some existing utilities.
2668 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002669 if (!(global.pass == 1 && global.passes == 2)) {
2670 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002671 FILE *f = fopen("opsnr.stt", "a");
2672 if (stream->mismatch_seen) {
2673 fprintf(f, "First mismatch occurred in frame %d\n",
2674 stream->mismatch_seen);
2675 } else {
2676 fprintf(f, "No mismatch detected in recon buffers\n");
2677 }
2678 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002679 }
2680 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002681#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002682
Yaowu Xuf883b422016-08-30 14:01:10 -07002683 if (allocated_raw_shift) aom_img_free(&raw_shift);
Yaowu Xuf883b422016-08-30 14:01:10 -07002684 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002685 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002686 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002687 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002688}