xref: /aosp_15_r20/external/libaom/apps/aomenc.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include "apps/aomenc.h"
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
19*77c1e3ccSAndroid Build Coastguard Worker #include <stdarg.h>
20*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
21*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
22*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
25*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
26*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomdx.h"
27*77c1e3ccSAndroid Build Coastguard Worker #endif
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem_ops.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "common/args.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "common/ivfenc.h"
37*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "common/warnings.h"
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
41*77c1e3ccSAndroid Build Coastguard Worker #include "common/webmenc.h"
42*77c1e3ccSAndroid Build Coastguard Worker #endif
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker #include "common/y4minput.h"
45*77c1e3ccSAndroid Build Coastguard Worker #include "examples/encoder_util.h"
46*77c1e3ccSAndroid Build Coastguard Worker #include "stats/aomstats.h"
47*77c1e3ccSAndroid Build Coastguard Worker #include "stats/rate_hist.h"
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_LIBYUV
50*77c1e3ccSAndroid Build Coastguard Worker #include "third_party/libyuv/include/libyuv/scale.h"
51*77c1e3ccSAndroid Build Coastguard Worker #endif
52*77c1e3ccSAndroid Build Coastguard Worker 
53*77c1e3ccSAndroid Build Coastguard Worker /* Swallow warnings about unused results of fread/fwrite */
wrap_fread(void * ptr,size_t size,size_t nmemb,FILE * stream)54*77c1e3ccSAndroid Build Coastguard Worker static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
55*77c1e3ccSAndroid Build Coastguard Worker   return fread(ptr, size, nmemb, stream);
56*77c1e3ccSAndroid Build Coastguard Worker }
57*77c1e3ccSAndroid Build Coastguard Worker #define fread wrap_fread
58*77c1e3ccSAndroid Build Coastguard Worker 
wrap_fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream)59*77c1e3ccSAndroid Build Coastguard Worker static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
60*77c1e3ccSAndroid Build Coastguard Worker                           FILE *stream) {
61*77c1e3ccSAndroid Build Coastguard Worker   return fwrite(ptr, size, nmemb, stream);
62*77c1e3ccSAndroid Build Coastguard Worker }
63*77c1e3ccSAndroid Build Coastguard Worker #define fwrite wrap_fwrite
64*77c1e3ccSAndroid Build Coastguard Worker 
65*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
66*77c1e3ccSAndroid Build Coastguard Worker 
warn_or_exit_on_errorv(aom_codec_ctx_t * ctx,int fatal,const char * s,va_list ap)67*77c1e3ccSAndroid Build Coastguard Worker static AOM_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
68*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
69*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->err) {
70*77c1e3ccSAndroid Build Coastguard Worker     const char *detail = aom_codec_error_detail(ctx);
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker     vfprintf(stderr, s, ap);
73*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, ": %s\n", aom_codec_error(ctx));
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker     if (detail) fprintf(stderr, "    %s\n", detail);
76*77c1e3ccSAndroid Build Coastguard Worker 
77*77c1e3ccSAndroid Build Coastguard Worker     if (fatal) {
78*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_destroy(ctx);
79*77c1e3ccSAndroid Build Coastguard Worker       exit(EXIT_FAILURE);
80*77c1e3ccSAndroid Build Coastguard Worker     }
81*77c1e3ccSAndroid Build Coastguard Worker   }
82*77c1e3ccSAndroid Build Coastguard Worker }
83*77c1e3ccSAndroid Build Coastguard Worker 
84*77c1e3ccSAndroid Build Coastguard Worker static AOM_TOOLS_FORMAT_PRINTF(2,
ctx_exit_on_error(aom_codec_ctx_t * ctx,const char * s,...)85*77c1e3ccSAndroid Build Coastguard Worker                                3) void ctx_exit_on_error(aom_codec_ctx_t *ctx,
86*77c1e3ccSAndroid Build Coastguard Worker                                                          const char *s, ...) {
87*77c1e3ccSAndroid Build Coastguard Worker   va_list ap;
88*77c1e3ccSAndroid Build Coastguard Worker 
89*77c1e3ccSAndroid Build Coastguard Worker   va_start(ap, s);
90*77c1e3ccSAndroid Build Coastguard Worker   warn_or_exit_on_errorv(ctx, 1, s, ap);
91*77c1e3ccSAndroid Build Coastguard Worker   va_end(ap);
92*77c1e3ccSAndroid Build Coastguard Worker }
93*77c1e3ccSAndroid Build Coastguard Worker 
warn_or_exit_on_error(aom_codec_ctx_t * ctx,int fatal,const char * s,...)94*77c1e3ccSAndroid Build Coastguard Worker static AOM_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
95*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_ctx_t *ctx, int fatal, const char *s, ...) {
96*77c1e3ccSAndroid Build Coastguard Worker   va_list ap;
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker   va_start(ap, s);
99*77c1e3ccSAndroid Build Coastguard Worker   warn_or_exit_on_errorv(ctx, fatal, s, ap);
100*77c1e3ccSAndroid Build Coastguard Worker   va_end(ap);
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker 
read_frame(struct AvxInputContext * input_ctx,aom_image_t * img)103*77c1e3ccSAndroid Build Coastguard Worker static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
104*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = input_ctx->file;
105*77c1e3ccSAndroid Build Coastguard Worker   y4m_input *y4m = &input_ctx->y4m;
106*77c1e3ccSAndroid Build Coastguard Worker   int shortread = 0;
107*77c1e3ccSAndroid Build Coastguard Worker 
108*77c1e3ccSAndroid Build Coastguard Worker   if (input_ctx->file_type == FILE_TYPE_Y4M) {
109*77c1e3ccSAndroid Build Coastguard Worker     if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
110*77c1e3ccSAndroid Build Coastguard Worker   } else {
111*77c1e3ccSAndroid Build Coastguard Worker     shortread = read_yuv_frame(input_ctx, img);
112*77c1e3ccSAndroid Build Coastguard Worker   }
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker   return !shortread;
115*77c1e3ccSAndroid Build Coastguard Worker }
116*77c1e3ccSAndroid Build Coastguard Worker 
file_is_y4m(const char detect[4])117*77c1e3ccSAndroid Build Coastguard Worker static int file_is_y4m(const char detect[4]) {
118*77c1e3ccSAndroid Build Coastguard Worker   if (memcmp(detect, "YUV4", 4) == 0) {
119*77c1e3ccSAndroid Build Coastguard Worker     return 1;
120*77c1e3ccSAndroid Build Coastguard Worker   }
121*77c1e3ccSAndroid Build Coastguard Worker   return 0;
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker 
fourcc_is_ivf(const char detect[4])124*77c1e3ccSAndroid Build Coastguard Worker static int fourcc_is_ivf(const char detect[4]) {
125*77c1e3ccSAndroid Build Coastguard Worker   if (memcmp(detect, "DKIF", 4) == 0) {
126*77c1e3ccSAndroid Build Coastguard Worker     return 1;
127*77c1e3ccSAndroid Build Coastguard Worker   }
128*77c1e3ccSAndroid Build Coastguard Worker   return 0;
129*77c1e3ccSAndroid Build Coastguard Worker }
130*77c1e3ccSAndroid Build Coastguard Worker 
131*77c1e3ccSAndroid Build Coastguard Worker static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
132*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_ENABLEAUTOALTREF,
133*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_SHARPNESS,
134*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_STATIC_THRESHOLD,
135*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ROW_MT,
136*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FP_MT,
137*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TILE_COLUMNS,
138*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TILE_ROWS,
139*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_TPL_MODEL,
140*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_KEYFRAME_FILTERING,
141*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_ARNR_MAXFRAMES,
142*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_ARNR_STRENGTH,
143*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_TUNING,
144*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_CQ_LEVEL,
145*77c1e3ccSAndroid Build Coastguard Worker                                         AOME_SET_MAX_INTRA_BITRATE_PCT,
146*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MAX_INTER_BITRATE_PCT,
147*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_GF_CBR_BOOST_PCT,
148*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_LOSSLESS,
149*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_CDEF,
150*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_RESTORATION,
151*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_RECT_PARTITIONS,
152*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_AB_PARTITIONS,
153*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_1TO4_PARTITIONS,
154*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MIN_PARTITION_SIZE,
155*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MAX_PARTITION_SIZE,
156*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DUAL_FILTER,
157*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_CHROMA_DELTAQ,
158*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_INTRA_EDGE_FILTER,
159*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_ORDER_HINT,
160*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_TX64,
161*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_FLIP_IDTX,
162*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_RECT_TX,
163*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DIST_WTD_COMP,
164*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_MASKED_COMP,
165*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_ONESIDED_COMP,
166*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_INTERINTRA_COMP,
167*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_SMOOTH_INTERINTRA,
168*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DIFF_WTD_COMP,
169*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_INTERINTER_WEDGE,
170*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_INTERINTRA_WEDGE,
171*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_GLOBAL_MOTION,
172*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_WARPED_MOTION,
173*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_FILTER_INTRA,
174*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_SMOOTH_INTRA,
175*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_PAETH_INTRA,
176*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_CFL_INTRA,
177*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DIAGONAL_INTRA,
178*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FORCE_VIDEO_MODE,
179*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_OBMC,
180*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_OVERLAY,
181*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_PALETTE,
182*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_INTRABC,
183*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_ANGLE_DELTA,
184*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DISABLE_TRELLIS_QUANT,
185*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_QM,
186*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_QM_MIN,
187*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_QM_MAX,
188*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_REDUCED_TX_TYPE_SET,
189*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_INTRA_DCT_ONLY,
190*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_INTER_DCT_ONLY,
191*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_INTRA_DEFAULT_TX_ONLY,
192*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_QUANT_B_ADAPT,
193*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_COEFF_COST_UPD_FREQ,
194*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MODE_COST_UPD_FREQ,
195*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MV_COST_UPD_FREQ,
196*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FRAME_PARALLEL_DECODING,
197*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ERROR_RESILIENT_MODE,
198*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_AQ_MODE,
199*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DELTAQ_MODE,
200*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DELTAQ_STRENGTH,
201*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DELTALF_MODE,
202*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FRAME_PERIODIC_BOOST,
203*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_NOISE_SENSITIVITY,
204*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TUNE_CONTENT,
205*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_CDF_UPDATE_MODE,
206*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_COLOR_PRIMARIES,
207*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TRANSFER_CHARACTERISTICS,
208*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MATRIX_COEFFICIENTS,
209*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_CHROMA_SAMPLE_POSITION,
210*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MIN_GF_INTERVAL,
211*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MAX_GF_INTERVAL,
212*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_GF_MIN_PYRAMID_HEIGHT,
213*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_GF_MAX_PYRAMID_HEIGHT,
214*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_SUPERBLOCK_SIZE,
215*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_NUM_TG,
216*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MTU,
217*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TIMING_INFO_TYPE,
218*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FILM_GRAIN_TEST_VECTOR,
219*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_FILM_GRAIN_TABLE,
220*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_DENOISE
221*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DENOISE_NOISE_LEVEL,
222*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DENOISE_BLOCK_SIZE,
223*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DNL_DENOISING,
224*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_DENOISE
225*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MAX_REFERENCE_FRAMES,
226*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_REDUCED_REFERENCE_SET,
227*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_REF_FRAME_MVS,
228*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TARGET_SEQ_LEVEL_IDX,
229*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_TIER_MASK,
230*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_MIN_CR,
231*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP,
232*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_CHROMA_SUBSAMPLING_X,
233*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_CHROMA_SUBSAMPLING_Y,
234*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
235*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_VMAF_MODEL_PATH,
236*77c1e3ccSAndroid Build Coastguard Worker #endif
237*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_DV_COST_UPD_FREQ,
238*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_PARTITION_INFO_PATH,
239*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_DIRECTIONAL_INTRA,
240*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_ENABLE_TX_SIZE_SEARCH,
241*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_LOOPFILTER_CONTROL,
242*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_AUTO_INTRA_TOOLS_OFF,
243*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_ENABLE_RATE_GUIDE_DELTAQ,
244*77c1e3ccSAndroid Build Coastguard Worker                                         AV1E_SET_RATE_DISTRIBUTION_INFO,
245*77c1e3ccSAndroid Build Coastguard Worker                                         0 };
246*77c1e3ccSAndroid Build Coastguard Worker 
247*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const main_args[] = {
248*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.help,
249*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_cfg,
250*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.debugmode,
251*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.outputfile,
252*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.codecarg,
253*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.passes,
254*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.pass_arg,
255*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.fpf_name,
256*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.limit,
257*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.skip,
258*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.good_dl,
259*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.rt_dl,
260*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.ai_dl,
261*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.quietarg,
262*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.verbosearg,
263*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.psnrarg,
264*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_webm,
265*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_ivf,
266*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_obu,
267*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.q_hist_n,
268*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.rate_hist_n,
269*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.disable_warnings,
270*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.disable_warning_prompt,
271*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.recontest,
272*77c1e3ccSAndroid Build Coastguard Worker   NULL
273*77c1e3ccSAndroid Build Coastguard Worker };
274*77c1e3ccSAndroid Build Coastguard Worker 
275*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const global_args[] = {
276*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_nv12,
277*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_yv12,
278*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_i420,
279*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_i422,
280*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_i444,
281*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.usage,
282*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.threads,
283*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.profile,
284*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.width,
285*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.height,
286*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.forced_max_frame_width,
287*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.forced_max_frame_height,
288*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
289*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.stereo_mode,
290*77c1e3ccSAndroid Build Coastguard Worker #endif
291*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.timebase,
292*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.framerate,
293*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.global_error_resilient,
294*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.bitdeptharg,
295*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.inbitdeptharg,
296*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.lag_in_frames,
297*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.large_scale_tile,
298*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.monochrome,
299*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.full_still_picture_hdr,
300*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_16bit_internal,
301*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.save_as_annexb,
302*77c1e3ccSAndroid Build Coastguard Worker   NULL
303*77c1e3ccSAndroid Build Coastguard Worker };
304*77c1e3ccSAndroid Build Coastguard Worker 
305*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const rc_args[] = {
306*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.dropframe_thresh,
307*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.resize_mode,
308*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.resize_denominator,
309*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.resize_kf_denominator,
310*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superres_mode,
311*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superres_denominator,
312*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superres_kf_denominator,
313*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superres_qthresh,
314*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superres_kf_qthresh,
315*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.end_usage,
316*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.target_bitrate,
317*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.min_quantizer,
318*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_quantizer,
319*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.undershoot_pct,
320*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.overshoot_pct,
321*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.buf_sz,
322*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.buf_initial_sz,
323*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.buf_optimal_sz,
324*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.bias_pct,
325*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.minsection_pct,
326*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.maxsection_pct,
327*77c1e3ccSAndroid Build Coastguard Worker   NULL
328*77c1e3ccSAndroid Build Coastguard Worker };
329*77c1e3ccSAndroid Build Coastguard Worker 
330*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const kf_args[] = {
331*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.fwd_kf_enabled,
332*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.kf_min_dist,
333*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.kf_max_dist,
334*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.kf_disabled,
335*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.sframe_dist,
336*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.sframe_mode,
337*77c1e3ccSAndroid Build Coastguard Worker   NULL
338*77c1e3ccSAndroid Build Coastguard Worker };
339*77c1e3ccSAndroid Build Coastguard Worker 
340*77c1e3ccSAndroid Build Coastguard Worker // TODO(bohanli): Currently all options are supported by the key & value API.
341*77c1e3ccSAndroid Build Coastguard Worker // Consider removing the control ID usages?
342*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const av1_ctrl_args[] = {
343*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.cpu_used_av1,
344*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.auto_altref,
345*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.sharpness,
346*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.static_thresh,
347*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.rowmtarg,
348*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.fpmtarg,
349*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.tile_cols,
350*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.tile_rows,
351*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_tpl_model,
352*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_keyframe_filtering,
353*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.arnr_maxframes,
354*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.arnr_strength,
355*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.tune_metric,
356*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.cq_level,
357*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_intra_rate_pct,
358*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_inter_rate_pct,
359*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.gf_cbr_boost_pct,
360*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.lossless,
361*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_cdef,
362*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_restoration,
363*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_rect_partitions,
364*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_ab_partitions,
365*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_1to4_partitions,
366*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.min_partition_size,
367*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_partition_size,
368*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_dual_filter,
369*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_chroma_deltaq,
370*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_intra_edge_filter,
371*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_order_hint,
372*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_tx64,
373*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_flip_idtx,
374*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_rect_tx,
375*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_dist_wtd_comp,
376*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_masked_comp,
377*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_onesided_comp,
378*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_interintra_comp,
379*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_smooth_interintra,
380*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_diff_wtd_comp,
381*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_interinter_wedge,
382*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_interintra_wedge,
383*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_global_motion,
384*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_warped_motion,
385*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_filter_intra,
386*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_smooth_intra,
387*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_paeth_intra,
388*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_cfl_intra,
389*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_diagonal_intra,
390*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.force_video_mode,
391*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_obmc,
392*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_overlay,
393*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_palette,
394*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_intrabc,
395*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_angle_delta,
396*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.disable_trellis_quant,
397*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_qm,
398*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.qm_min,
399*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.qm_max,
400*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.reduced_tx_type_set,
401*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_intra_dct_only,
402*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_inter_dct_only,
403*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.use_intra_default_tx_only,
404*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.quant_b_adapt,
405*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.coeff_cost_upd_freq,
406*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.mode_cost_upd_freq,
407*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.mv_cost_upd_freq,
408*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.frame_parallel_decoding,
409*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.error_resilient_mode,
410*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.aq_mode,
411*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.deltaq_mode,
412*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.deltaq_strength,
413*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.deltalf_mode,
414*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.frame_periodic_boost,
415*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.noise_sens,
416*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.tune_content,
417*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.cdf_update_mode,
418*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_color_primaries,
419*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_transfer_characteristics,
420*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_matrix_coefficients,
421*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_chroma_sample_position,
422*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.min_gf_interval,
423*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_gf_interval,
424*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.gf_min_pyr_height,
425*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.gf_max_pyr_height,
426*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.superblock_size,
427*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.num_tg,
428*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.mtu_size,
429*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.timing_info,
430*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.film_grain_test,
431*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.film_grain_table,
432*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_DENOISE
433*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.denoise_noise_level,
434*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.denoise_block_size,
435*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_dnl_denoising,
436*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_DENOISE
437*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.max_reference_frames,
438*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.reduced_reference_set,
439*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_ref_frame_mvs,
440*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.target_seq_level_idx,
441*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.set_tier_mask,
442*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.set_min_cr,
443*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
444*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_chroma_subsampling_x,
445*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.input_chroma_subsampling_y,
446*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
447*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.vmaf_model_path,
448*77c1e3ccSAndroid Build Coastguard Worker #endif
449*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.dv_cost_upd_freq,
450*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.partition_info_path,
451*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_directional_intra,
452*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_tx_size_search,
453*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.loopfilter_control,
454*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.auto_intra_tools_off,
455*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
456*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.rate_distribution_info,
457*77c1e3ccSAndroid Build Coastguard Worker   NULL,
458*77c1e3ccSAndroid Build Coastguard Worker };
459*77c1e3ccSAndroid Build Coastguard Worker 
460*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const av1_key_val_args[] = {
461*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.passes,
462*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.two_pass_output,
463*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.second_pass_log,
464*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.fwd_kf_dist,
465*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.strict_level_conformance,
466*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.sb_qp_sweep,
467*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.dist_metric,
468*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.kf_max_pyr_height,
469*77c1e3ccSAndroid Build Coastguard Worker   &g_av1_codec_arg_defs.auto_tiles,
470*77c1e3ccSAndroid Build Coastguard Worker   NULL,
471*77c1e3ccSAndroid Build Coastguard Worker };
472*77c1e3ccSAndroid Build Coastguard Worker 
473*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *const no_args[] = { NULL };
474*77c1e3ccSAndroid Build Coastguard Worker 
show_help(FILE * fout,int shorthelp)475*77c1e3ccSAndroid Build Coastguard Worker static void show_help(FILE *fout, int shorthelp) {
476*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "Usage: %s <options> -o dst_filename src_filename\n",
477*77c1e3ccSAndroid Build Coastguard Worker           exec_name);
478*77c1e3ccSAndroid Build Coastguard Worker 
479*77c1e3ccSAndroid Build Coastguard Worker   if (shorthelp) {
480*77c1e3ccSAndroid Build Coastguard Worker     fprintf(fout, "Use --help to see the full list of options.\n");
481*77c1e3ccSAndroid Build Coastguard Worker     return;
482*77c1e3ccSAndroid Build Coastguard Worker   }
483*77c1e3ccSAndroid Build Coastguard Worker 
484*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nOptions:\n");
485*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, main_args);
486*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nEncoder Global Options:\n");
487*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, global_args);
488*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nRate Control Options:\n");
489*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, rc_args);
490*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nKeyframe Placement Options:\n");
491*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, kf_args);
492*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER
493*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nAV1 Specific Options:\n");
494*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, av1_ctrl_args);
495*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(fout, av1_key_val_args);
496*77c1e3ccSAndroid Build Coastguard Worker #endif
497*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout,
498*77c1e3ccSAndroid Build Coastguard Worker           "\nStream timebase (--timebase):\n"
499*77c1e3ccSAndroid Build Coastguard Worker           "  The desired precision of timestamps in the output, expressed\n"
500*77c1e3ccSAndroid Build Coastguard Worker           "  in fractional seconds. Default is 1/1000.\n");
501*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\nIncluded encoders:\n\n");
502*77c1e3ccSAndroid Build Coastguard Worker 
503*77c1e3ccSAndroid Build Coastguard Worker   const int num_encoder = get_aom_encoder_count();
504*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < num_encoder; ++i) {
505*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_iface_t *encoder = get_aom_encoder_by_index(i);
506*77c1e3ccSAndroid Build Coastguard Worker     const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
507*77c1e3ccSAndroid Build Coastguard Worker     fprintf(fout, "    %-6s - %s %s\n", get_short_name_by_aom_encoder(encoder),
508*77c1e3ccSAndroid Build Coastguard Worker             aom_codec_iface_name(encoder), defstr);
509*77c1e3ccSAndroid Build Coastguard Worker   }
510*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "\n        ");
511*77c1e3ccSAndroid Build Coastguard Worker   fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
512*77c1e3ccSAndroid Build Coastguard Worker }
513*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)514*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
515*77c1e3ccSAndroid Build Coastguard Worker   show_help(stderr, 1);
516*77c1e3ccSAndroid Build Coastguard Worker   exit(EXIT_FAILURE);
517*77c1e3ccSAndroid Build Coastguard Worker }
518*77c1e3ccSAndroid Build Coastguard Worker 
519*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER
520*77c1e3ccSAndroid Build Coastguard Worker #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
521*77c1e3ccSAndroid Build Coastguard Worker #define ARG_KEY_VAL_CNT_MAX NELEMENTS(av1_key_val_args)
522*77c1e3ccSAndroid Build Coastguard Worker #endif
523*77c1e3ccSAndroid Build Coastguard Worker 
524*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_WEBM_IO
525*77c1e3ccSAndroid Build Coastguard Worker typedef int stereo_format_t;
526*77c1e3ccSAndroid Build Coastguard Worker struct WebmOutputContext {
527*77c1e3ccSAndroid Build Coastguard Worker   int debug;
528*77c1e3ccSAndroid Build Coastguard Worker };
529*77c1e3ccSAndroid Build Coastguard Worker #endif
530*77c1e3ccSAndroid Build Coastguard Worker 
531*77c1e3ccSAndroid Build Coastguard Worker /* Per-stream configuration */
532*77c1e3ccSAndroid Build Coastguard Worker struct stream_config {
533*77c1e3ccSAndroid Build Coastguard Worker   struct aom_codec_enc_cfg cfg;
534*77c1e3ccSAndroid Build Coastguard Worker   const char *out_fn;
535*77c1e3ccSAndroid Build Coastguard Worker   const char *stats_fn;
536*77c1e3ccSAndroid Build Coastguard Worker   stereo_format_t stereo_fmt;
537*77c1e3ccSAndroid Build Coastguard Worker   int arg_ctrls[ARG_CTRL_CNT_MAX][2];
538*77c1e3ccSAndroid Build Coastguard Worker   int arg_ctrl_cnt;
539*77c1e3ccSAndroid Build Coastguard Worker   const char *arg_key_vals[ARG_KEY_VAL_CNT_MAX][2];
540*77c1e3ccSAndroid Build Coastguard Worker   int arg_key_val_cnt;
541*77c1e3ccSAndroid Build Coastguard Worker   int write_webm;
542*77c1e3ccSAndroid Build Coastguard Worker   const char *film_grain_filename;
543*77c1e3ccSAndroid Build Coastguard Worker   int write_ivf;
544*77c1e3ccSAndroid Build Coastguard Worker   // whether to use 16bit internal buffers
545*77c1e3ccSAndroid Build Coastguard Worker   int use_16bit_internal;
546*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
547*77c1e3ccSAndroid Build Coastguard Worker   const char *vmaf_model_path;
548*77c1e3ccSAndroid Build Coastguard Worker #endif
549*77c1e3ccSAndroid Build Coastguard Worker   const char *partition_info_path;
550*77c1e3ccSAndroid Build Coastguard Worker   unsigned int enable_rate_guide_deltaq;
551*77c1e3ccSAndroid Build Coastguard Worker   const char *rate_distribution_info;
552*77c1e3ccSAndroid Build Coastguard Worker   aom_color_range_t color_range;
553*77c1e3ccSAndroid Build Coastguard Worker   const char *two_pass_input;
554*77c1e3ccSAndroid Build Coastguard Worker   const char *two_pass_output;
555*77c1e3ccSAndroid Build Coastguard Worker   int two_pass_width;
556*77c1e3ccSAndroid Build Coastguard Worker   int two_pass_height;
557*77c1e3ccSAndroid Build Coastguard Worker };
558*77c1e3ccSAndroid Build Coastguard Worker 
559*77c1e3ccSAndroid Build Coastguard Worker struct stream_state {
560*77c1e3ccSAndroid Build Coastguard Worker   int index;
561*77c1e3ccSAndroid Build Coastguard Worker   struct stream_state *next;
562*77c1e3ccSAndroid Build Coastguard Worker   struct stream_config config;
563*77c1e3ccSAndroid Build Coastguard Worker   FILE *file;
564*77c1e3ccSAndroid Build Coastguard Worker   struct rate_hist *rate_hist;
565*77c1e3ccSAndroid Build Coastguard Worker   struct WebmOutputContext webm_ctx;
566*77c1e3ccSAndroid Build Coastguard Worker   uint64_t psnr_sse_total[2];
567*77c1e3ccSAndroid Build Coastguard Worker   uint64_t psnr_samples_total[2];
568*77c1e3ccSAndroid Build Coastguard Worker   double psnr_totals[2][4];
569*77c1e3ccSAndroid Build Coastguard Worker   int psnr_count[2];
570*77c1e3ccSAndroid Build Coastguard Worker   int counts[64];
571*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t encoder;
572*77c1e3ccSAndroid Build Coastguard Worker   unsigned int frames_out;
573*77c1e3ccSAndroid Build Coastguard Worker   uint64_t cx_time;
574*77c1e3ccSAndroid Build Coastguard Worker   size_t nbytes;
575*77c1e3ccSAndroid Build Coastguard Worker   stats_io_t stats;
576*77c1e3ccSAndroid Build Coastguard Worker   struct aom_image *img;
577*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t decoder;
578*77c1e3ccSAndroid Build Coastguard Worker   int mismatch_seen;
579*77c1e3ccSAndroid Build Coastguard Worker   unsigned int chroma_subsampling_x;
580*77c1e3ccSAndroid Build Coastguard Worker   unsigned int chroma_subsampling_y;
581*77c1e3ccSAndroid Build Coastguard Worker   const char *orig_out_fn;
582*77c1e3ccSAndroid Build Coastguard Worker   unsigned int orig_width;
583*77c1e3ccSAndroid Build Coastguard Worker   unsigned int orig_height;
584*77c1e3ccSAndroid Build Coastguard Worker   int orig_write_webm;
585*77c1e3ccSAndroid Build Coastguard Worker   int orig_write_ivf;
586*77c1e3ccSAndroid Build Coastguard Worker   char tmp_out_fn[1000];
587*77c1e3ccSAndroid Build Coastguard Worker };
588*77c1e3ccSAndroid Build Coastguard Worker 
validate_positive_rational(const char * msg,struct aom_rational * rat)589*77c1e3ccSAndroid Build Coastguard Worker static void validate_positive_rational(const char *msg,
590*77c1e3ccSAndroid Build Coastguard Worker                                        struct aom_rational *rat) {
591*77c1e3ccSAndroid Build Coastguard Worker   if (rat->den < 0) {
592*77c1e3ccSAndroid Build Coastguard Worker     rat->num *= -1;
593*77c1e3ccSAndroid Build Coastguard Worker     rat->den *= -1;
594*77c1e3ccSAndroid Build Coastguard Worker   }
595*77c1e3ccSAndroid Build Coastguard Worker 
596*77c1e3ccSAndroid Build Coastguard Worker   if (rat->num < 0) die("Error: %s must be positive\n", msg);
597*77c1e3ccSAndroid Build Coastguard Worker 
598*77c1e3ccSAndroid Build Coastguard Worker   if (!rat->den) die("Error: %s has zero denominator\n", msg);
599*77c1e3ccSAndroid Build Coastguard Worker }
600*77c1e3ccSAndroid Build Coastguard Worker 
init_config(cfg_options_t * config)601*77c1e3ccSAndroid Build Coastguard Worker static void init_config(cfg_options_t *config) {
602*77c1e3ccSAndroid Build Coastguard Worker   memset(config, 0, sizeof(cfg_options_t));
603*77c1e3ccSAndroid Build Coastguard Worker   config->super_block_size = 0;  // Dynamic
604*77c1e3ccSAndroid Build Coastguard Worker   config->max_partition_size = 128;
605*77c1e3ccSAndroid Build Coastguard Worker   config->min_partition_size = 4;
606*77c1e3ccSAndroid Build Coastguard Worker   config->disable_trellis_quant = 3;
607*77c1e3ccSAndroid Build Coastguard Worker }
608*77c1e3ccSAndroid Build Coastguard Worker 
609*77c1e3ccSAndroid Build Coastguard Worker /* Parses global config arguments into the AvxEncoderConfig. Note that
610*77c1e3ccSAndroid Build Coastguard Worker  * argv is modified and overwrites all parsed arguments.
611*77c1e3ccSAndroid Build Coastguard Worker  */
parse_global_config(struct AvxEncoderConfig * global,char *** argv)612*77c1e3ccSAndroid Build Coastguard Worker static void parse_global_config(struct AvxEncoderConfig *global, char ***argv) {
613*77c1e3ccSAndroid Build Coastguard Worker   char **argi, **argj;
614*77c1e3ccSAndroid Build Coastguard Worker   struct arg arg;
615*77c1e3ccSAndroid Build Coastguard Worker   const int num_encoder = get_aom_encoder_count();
616*77c1e3ccSAndroid Build Coastguard Worker   char **argv_local = (char **)*argv;
617*77c1e3ccSAndroid Build Coastguard Worker   if (num_encoder < 1) die("Error: no valid encoder available\n");
618*77c1e3ccSAndroid Build Coastguard Worker 
619*77c1e3ccSAndroid Build Coastguard Worker   /* Initialize default parameters */
620*77c1e3ccSAndroid Build Coastguard Worker   memset(global, 0, sizeof(*global));
621*77c1e3ccSAndroid Build Coastguard Worker   global->codec = get_aom_encoder_by_index(num_encoder - 1);
622*77c1e3ccSAndroid Build Coastguard Worker   global->passes = 0;
623*77c1e3ccSAndroid Build Coastguard Worker   global->color_type = I420;
624*77c1e3ccSAndroid Build Coastguard Worker   global->csp = AOM_CSP_UNKNOWN;
625*77c1e3ccSAndroid Build Coastguard Worker   global->show_psnr = 0;
626*77c1e3ccSAndroid Build Coastguard Worker 
627*77c1e3ccSAndroid Build Coastguard Worker   int cfg_included = 0;
628*77c1e3ccSAndroid Build Coastguard Worker   init_config(&global->encoder_config);
629*77c1e3ccSAndroid Build Coastguard Worker 
630*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
631*77c1e3ccSAndroid Build Coastguard Worker     arg.argv_step = 1;
632*77c1e3ccSAndroid Build Coastguard Worker 
633*77c1e3ccSAndroid Build Coastguard Worker     if (arg_match(&arg, &g_av1_codec_arg_defs.use_cfg, argi)) {
634*77c1e3ccSAndroid Build Coastguard Worker       if (!cfg_included) {
635*77c1e3ccSAndroid Build Coastguard Worker         parse_cfg(arg.val, &global->encoder_config);
636*77c1e3ccSAndroid Build Coastguard Worker         cfg_included = 1;
637*77c1e3ccSAndroid Build Coastguard Worker       }
638*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.help, argi)) {
639*77c1e3ccSAndroid Build Coastguard Worker       show_help(stdout, 0);
640*77c1e3ccSAndroid Build Coastguard Worker       exit(EXIT_SUCCESS);
641*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.codecarg, argi)) {
642*77c1e3ccSAndroid Build Coastguard Worker       global->codec = get_aom_encoder_by_short_name(arg.val);
643*77c1e3ccSAndroid Build Coastguard Worker       if (!global->codec)
644*77c1e3ccSAndroid Build Coastguard Worker         die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
645*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.passes, argi)) {
646*77c1e3ccSAndroid Build Coastguard Worker       global->passes = arg_parse_uint(&arg);
647*77c1e3ccSAndroid Build Coastguard Worker 
648*77c1e3ccSAndroid Build Coastguard Worker       if (global->passes < 1 || global->passes > 3)
649*77c1e3ccSAndroid Build Coastguard Worker         die("Error: Invalid number of passes (%d)\n", global->passes);
650*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.pass_arg, argi)) {
651*77c1e3ccSAndroid Build Coastguard Worker       global->pass = arg_parse_uint(&arg);
652*77c1e3ccSAndroid Build Coastguard Worker 
653*77c1e3ccSAndroid Build Coastguard Worker       if (global->pass < 1 || global->pass > 3)
654*77c1e3ccSAndroid Build Coastguard Worker         die("Error: Invalid pass selected (%d)\n", global->pass);
655*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg,
656*77c1e3ccSAndroid Build Coastguard Worker                          &g_av1_codec_arg_defs.input_chroma_sample_position,
657*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
658*77c1e3ccSAndroid Build Coastguard Worker       global->csp = arg_parse_enum(&arg);
659*77c1e3ccSAndroid Build Coastguard Worker       /* Flag is used by later code as well, preserve it. */
660*77c1e3ccSAndroid Build Coastguard Worker       argj++;
661*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.usage, argi)) {
662*77c1e3ccSAndroid Build Coastguard Worker       global->usage = arg_parse_uint(&arg);
663*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.good_dl, argi)) {
664*77c1e3ccSAndroid Build Coastguard Worker       global->usage = AOM_USAGE_GOOD_QUALITY;  // Good quality usage
665*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.rt_dl, argi)) {
666*77c1e3ccSAndroid Build Coastguard Worker       global->usage = AOM_USAGE_REALTIME;  // Real-time usage
667*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.ai_dl, argi)) {
668*77c1e3ccSAndroid Build Coastguard Worker       global->usage = AOM_USAGE_ALL_INTRA;  // All intra usage
669*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_nv12, argi)) {
670*77c1e3ccSAndroid Build Coastguard Worker       global->color_type = NV12;
671*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_yv12, argi)) {
672*77c1e3ccSAndroid Build Coastguard Worker       global->color_type = YV12;
673*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i420, argi)) {
674*77c1e3ccSAndroid Build Coastguard Worker       global->color_type = I420;
675*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i422, argi)) {
676*77c1e3ccSAndroid Build Coastguard Worker       global->color_type = I422;
677*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i444, argi)) {
678*77c1e3ccSAndroid Build Coastguard Worker       global->color_type = I444;
679*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.quietarg, argi)) {
680*77c1e3ccSAndroid Build Coastguard Worker       global->quiet = 1;
681*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.verbosearg, argi)) {
682*77c1e3ccSAndroid Build Coastguard Worker       global->verbose = 1;
683*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.limit, argi)) {
684*77c1e3ccSAndroid Build Coastguard Worker       global->limit = arg_parse_uint(&arg);
685*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.skip, argi)) {
686*77c1e3ccSAndroid Build Coastguard Worker       global->skip_frames = arg_parse_uint(&arg);
687*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.psnrarg, argi)) {
688*77c1e3ccSAndroid Build Coastguard Worker       if (arg.val)
689*77c1e3ccSAndroid Build Coastguard Worker         global->show_psnr = arg_parse_int(&arg);
690*77c1e3ccSAndroid Build Coastguard Worker       else
691*77c1e3ccSAndroid Build Coastguard Worker         global->show_psnr = 1;
692*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.recontest, argi)) {
693*77c1e3ccSAndroid Build Coastguard Worker       global->test_decode = arg_parse_enum_or_int(&arg);
694*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.framerate, argi)) {
695*77c1e3ccSAndroid Build Coastguard Worker       global->framerate = arg_parse_rational(&arg);
696*77c1e3ccSAndroid Build Coastguard Worker       validate_positive_rational(arg.name, &global->framerate);
697*77c1e3ccSAndroid Build Coastguard Worker       global->have_framerate = 1;
698*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.debugmode, argi)) {
699*77c1e3ccSAndroid Build Coastguard Worker       global->debug = 1;
700*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.q_hist_n, argi)) {
701*77c1e3ccSAndroid Build Coastguard Worker       global->show_q_hist_buckets = arg_parse_uint(&arg);
702*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_hist_n, argi)) {
703*77c1e3ccSAndroid Build Coastguard Worker       global->show_rate_hist_buckets = arg_parse_uint(&arg);
704*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warnings, argi)) {
705*77c1e3ccSAndroid Build Coastguard Worker       global->disable_warnings = 1;
706*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warning_prompt,
707*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
708*77c1e3ccSAndroid Build Coastguard Worker       global->disable_warning_prompt = 1;
709*77c1e3ccSAndroid Build Coastguard Worker     } else {
710*77c1e3ccSAndroid Build Coastguard Worker       argj++;
711*77c1e3ccSAndroid Build Coastguard Worker     }
712*77c1e3ccSAndroid Build Coastguard Worker   }
713*77c1e3ccSAndroid Build Coastguard Worker 
714*77c1e3ccSAndroid Build Coastguard Worker   if (global->pass) {
715*77c1e3ccSAndroid Build Coastguard Worker     /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
716*77c1e3ccSAndroid Build Coastguard Worker     if (global->pass > global->passes) {
717*77c1e3ccSAndroid Build Coastguard Worker       aom_tools_warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
718*77c1e3ccSAndroid Build Coastguard Worker                      global->pass);
719*77c1e3ccSAndroid Build Coastguard Worker       global->passes = global->pass;
720*77c1e3ccSAndroid Build Coastguard Worker     }
721*77c1e3ccSAndroid Build Coastguard Worker   }
722*77c1e3ccSAndroid Build Coastguard Worker   /* Validate global config */
723*77c1e3ccSAndroid Build Coastguard Worker   if (global->passes == 0) {
724*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER
725*77c1e3ccSAndroid Build Coastguard Worker     // Make default AV1 passes = 2 until there is a better quality 1-pass
726*77c1e3ccSAndroid Build Coastguard Worker     // encoder
727*77c1e3ccSAndroid Build Coastguard Worker     if (global->codec != NULL)
728*77c1e3ccSAndroid Build Coastguard Worker       global->passes =
729*77c1e3ccSAndroid Build Coastguard Worker           (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0 &&
730*77c1e3ccSAndroid Build Coastguard Worker            global->usage != AOM_USAGE_REALTIME)
731*77c1e3ccSAndroid Build Coastguard Worker               ? 2
732*77c1e3ccSAndroid Build Coastguard Worker               : 1;
733*77c1e3ccSAndroid Build Coastguard Worker #else
734*77c1e3ccSAndroid Build Coastguard Worker     global->passes = 1;
735*77c1e3ccSAndroid Build Coastguard Worker #endif
736*77c1e3ccSAndroid Build Coastguard Worker   }
737*77c1e3ccSAndroid Build Coastguard Worker 
738*77c1e3ccSAndroid Build Coastguard Worker   if (global->usage == AOM_USAGE_REALTIME && global->passes > 1) {
739*77c1e3ccSAndroid Build Coastguard Worker     aom_tools_warn("Enforcing one-pass encoding in realtime mode\n");
740*77c1e3ccSAndroid Build Coastguard Worker     if (global->pass > 1)
741*77c1e3ccSAndroid Build Coastguard Worker       die("Error: Invalid --pass=%d for one-pass encoding\n", global->pass);
742*77c1e3ccSAndroid Build Coastguard Worker     global->passes = 1;
743*77c1e3ccSAndroid Build Coastguard Worker   }
744*77c1e3ccSAndroid Build Coastguard Worker 
745*77c1e3ccSAndroid Build Coastguard Worker   if (global->usage == AOM_USAGE_ALL_INTRA && global->passes > 1) {
746*77c1e3ccSAndroid Build Coastguard Worker     aom_tools_warn("Enforcing one-pass encoding in all intra mode\n");
747*77c1e3ccSAndroid Build Coastguard Worker     global->passes = 1;
748*77c1e3ccSAndroid Build Coastguard Worker   }
749*77c1e3ccSAndroid Build Coastguard Worker }
750*77c1e3ccSAndroid Build Coastguard Worker 
open_input_file(struct AvxInputContext * input,aom_chroma_sample_position_t csp)751*77c1e3ccSAndroid Build Coastguard Worker static void open_input_file(struct AvxInputContext *input,
752*77c1e3ccSAndroid Build Coastguard Worker                             aom_chroma_sample_position_t csp) {
753*77c1e3ccSAndroid Build Coastguard Worker   /* Parse certain options from the input file, if possible */
754*77c1e3ccSAndroid Build Coastguard Worker   input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
755*77c1e3ccSAndroid Build Coastguard Worker                                              : set_binary_mode(stdin);
756*77c1e3ccSAndroid Build Coastguard Worker 
757*77c1e3ccSAndroid Build Coastguard Worker   if (!input->file) fatal("Failed to open input file");
758*77c1e3ccSAndroid Build Coastguard Worker 
759*77c1e3ccSAndroid Build Coastguard Worker   if (!fseeko(input->file, 0, SEEK_END)) {
760*77c1e3ccSAndroid Build Coastguard Worker     /* Input file is seekable. Figure out how long it is, so we can get
761*77c1e3ccSAndroid Build Coastguard Worker      * progress info.
762*77c1e3ccSAndroid Build Coastguard Worker      */
763*77c1e3ccSAndroid Build Coastguard Worker     input->length = ftello(input->file);
764*77c1e3ccSAndroid Build Coastguard Worker     rewind(input->file);
765*77c1e3ccSAndroid Build Coastguard Worker   }
766*77c1e3ccSAndroid Build Coastguard Worker 
767*77c1e3ccSAndroid Build Coastguard Worker   /* Default to 1:1 pixel aspect ratio. */
768*77c1e3ccSAndroid Build Coastguard Worker   input->pixel_aspect_ratio.numerator = 1;
769*77c1e3ccSAndroid Build Coastguard Worker   input->pixel_aspect_ratio.denominator = 1;
770*77c1e3ccSAndroid Build Coastguard Worker 
771*77c1e3ccSAndroid Build Coastguard Worker   /* For RAW input sources, these bytes will applied on the first frame
772*77c1e3ccSAndroid Build Coastguard Worker    *  in read_frame().
773*77c1e3ccSAndroid Build Coastguard Worker    */
774*77c1e3ccSAndroid Build Coastguard Worker   input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
775*77c1e3ccSAndroid Build Coastguard Worker   input->detect.position = 0;
776*77c1e3ccSAndroid Build Coastguard Worker 
777*77c1e3ccSAndroid Build Coastguard Worker   if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
778*77c1e3ccSAndroid Build Coastguard Worker     if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
779*77c1e3ccSAndroid Build Coastguard Worker                        input->only_i420) >= 0) {
780*77c1e3ccSAndroid Build Coastguard Worker       input->file_type = FILE_TYPE_Y4M;
781*77c1e3ccSAndroid Build Coastguard Worker       input->width = input->y4m.pic_w;
782*77c1e3ccSAndroid Build Coastguard Worker       input->height = input->y4m.pic_h;
783*77c1e3ccSAndroid Build Coastguard Worker       input->pixel_aspect_ratio.numerator = input->y4m.par_n;
784*77c1e3ccSAndroid Build Coastguard Worker       input->pixel_aspect_ratio.denominator = input->y4m.par_d;
785*77c1e3ccSAndroid Build Coastguard Worker       input->framerate.numerator = input->y4m.fps_n;
786*77c1e3ccSAndroid Build Coastguard Worker       input->framerate.denominator = input->y4m.fps_d;
787*77c1e3ccSAndroid Build Coastguard Worker       input->fmt = input->y4m.aom_fmt;
788*77c1e3ccSAndroid Build Coastguard Worker       input->bit_depth = input->y4m.bit_depth;
789*77c1e3ccSAndroid Build Coastguard Worker       input->color_range = input->y4m.color_range;
790*77c1e3ccSAndroid Build Coastguard Worker     } else
791*77c1e3ccSAndroid Build Coastguard Worker       fatal("Unsupported Y4M stream.");
792*77c1e3ccSAndroid Build Coastguard Worker   } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
793*77c1e3ccSAndroid Build Coastguard Worker     fatal("IVF is not supported as input.");
794*77c1e3ccSAndroid Build Coastguard Worker   } else {
795*77c1e3ccSAndroid Build Coastguard Worker     input->file_type = FILE_TYPE_RAW;
796*77c1e3ccSAndroid Build Coastguard Worker   }
797*77c1e3ccSAndroid Build Coastguard Worker }
798*77c1e3ccSAndroid Build Coastguard Worker 
close_input_file(struct AvxInputContext * input)799*77c1e3ccSAndroid Build Coastguard Worker static void close_input_file(struct AvxInputContext *input) {
800*77c1e3ccSAndroid Build Coastguard Worker   fclose(input->file);
801*77c1e3ccSAndroid Build Coastguard Worker   if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
802*77c1e3ccSAndroid Build Coastguard Worker }
803*77c1e3ccSAndroid Build Coastguard Worker 
new_stream(struct AvxEncoderConfig * global,struct stream_state * prev)804*77c1e3ccSAndroid Build Coastguard Worker static struct stream_state *new_stream(struct AvxEncoderConfig *global,
805*77c1e3ccSAndroid Build Coastguard Worker                                        struct stream_state *prev) {
806*77c1e3ccSAndroid Build Coastguard Worker   struct stream_state *stream;
807*77c1e3ccSAndroid Build Coastguard Worker 
808*77c1e3ccSAndroid Build Coastguard Worker   stream = calloc(1, sizeof(*stream));
809*77c1e3ccSAndroid Build Coastguard Worker   if (stream == NULL) {
810*77c1e3ccSAndroid Build Coastguard Worker     fatal("Failed to allocate new stream.");
811*77c1e3ccSAndroid Build Coastguard Worker   }
812*77c1e3ccSAndroid Build Coastguard Worker 
813*77c1e3ccSAndroid Build Coastguard Worker   if (prev) {
814*77c1e3ccSAndroid Build Coastguard Worker     memcpy(stream, prev, sizeof(*stream));
815*77c1e3ccSAndroid Build Coastguard Worker     stream->index++;
816*77c1e3ccSAndroid Build Coastguard Worker     prev->next = stream;
817*77c1e3ccSAndroid Build Coastguard Worker   } else {
818*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_err_t res;
819*77c1e3ccSAndroid Build Coastguard Worker 
820*77c1e3ccSAndroid Build Coastguard Worker     /* Populate encoder configuration */
821*77c1e3ccSAndroid Build Coastguard Worker     res = aom_codec_enc_config_default(global->codec, &stream->config.cfg,
822*77c1e3ccSAndroid Build Coastguard Worker                                        global->usage);
823*77c1e3ccSAndroid Build Coastguard Worker     if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
824*77c1e3ccSAndroid Build Coastguard Worker 
825*77c1e3ccSAndroid Build Coastguard Worker     /* Change the default timebase to a high enough value so that the
826*77c1e3ccSAndroid Build Coastguard Worker      * encoder will always create strictly increasing timestamps.
827*77c1e3ccSAndroid Build Coastguard Worker      */
828*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.g_timebase.den = 1000;
829*77c1e3ccSAndroid Build Coastguard Worker 
830*77c1e3ccSAndroid Build Coastguard Worker     /* Never use the library's default resolution, require it be parsed
831*77c1e3ccSAndroid Build Coastguard Worker      * from the file or set on the command line.
832*77c1e3ccSAndroid Build Coastguard Worker      */
833*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.g_w = 0;
834*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.g_h = 0;
835*77c1e3ccSAndroid Build Coastguard Worker 
836*77c1e3ccSAndroid Build Coastguard Worker     /* Initialize remaining stream parameters */
837*77c1e3ccSAndroid Build Coastguard Worker     stream->config.write_webm = 1;
838*77c1e3ccSAndroid Build Coastguard Worker     stream->config.write_ivf = 0;
839*77c1e3ccSAndroid Build Coastguard Worker 
840*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
841*77c1e3ccSAndroid Build Coastguard Worker     stream->config.stereo_fmt = STEREO_FORMAT_MONO;
842*77c1e3ccSAndroid Build Coastguard Worker     stream->webm_ctx.last_pts_ns = -1;
843*77c1e3ccSAndroid Build Coastguard Worker     stream->webm_ctx.writer = NULL;
844*77c1e3ccSAndroid Build Coastguard Worker     stream->webm_ctx.segment = NULL;
845*77c1e3ccSAndroid Build Coastguard Worker #endif
846*77c1e3ccSAndroid Build Coastguard Worker 
847*77c1e3ccSAndroid Build Coastguard Worker     /* Allows removal of the application version from the EBML tags */
848*77c1e3ccSAndroid Build Coastguard Worker     stream->webm_ctx.debug = global->debug;
849*77c1e3ccSAndroid Build Coastguard Worker     memcpy(&stream->config.cfg.encoder_cfg, &global->encoder_config,
850*77c1e3ccSAndroid Build Coastguard Worker            sizeof(stream->config.cfg.encoder_cfg));
851*77c1e3ccSAndroid Build Coastguard Worker   }
852*77c1e3ccSAndroid Build Coastguard Worker 
853*77c1e3ccSAndroid Build Coastguard Worker   /* Output files must be specified for each stream */
854*77c1e3ccSAndroid Build Coastguard Worker   stream->config.out_fn = NULL;
855*77c1e3ccSAndroid Build Coastguard Worker   stream->config.two_pass_input = NULL;
856*77c1e3ccSAndroid Build Coastguard Worker   stream->config.two_pass_output = NULL;
857*77c1e3ccSAndroid Build Coastguard Worker   stream->config.two_pass_width = 0;
858*77c1e3ccSAndroid Build Coastguard Worker   stream->config.two_pass_height = 0;
859*77c1e3ccSAndroid Build Coastguard Worker 
860*77c1e3ccSAndroid Build Coastguard Worker   stream->next = NULL;
861*77c1e3ccSAndroid Build Coastguard Worker   return stream;
862*77c1e3ccSAndroid Build Coastguard Worker }
863*77c1e3ccSAndroid Build Coastguard Worker 
set_config_arg_ctrls(struct stream_config * config,int key,const struct arg * arg)864*77c1e3ccSAndroid Build Coastguard Worker static void set_config_arg_ctrls(struct stream_config *config, int key,
865*77c1e3ccSAndroid Build Coastguard Worker                                  const struct arg *arg) {
866*77c1e3ccSAndroid Build Coastguard Worker   int j;
867*77c1e3ccSAndroid Build Coastguard Worker   if (key == AV1E_SET_FILM_GRAIN_TABLE) {
868*77c1e3ccSAndroid Build Coastguard Worker     config->film_grain_filename = arg->val;
869*77c1e3ccSAndroid Build Coastguard Worker     return;
870*77c1e3ccSAndroid Build Coastguard Worker   }
871*77c1e3ccSAndroid Build Coastguard Worker 
872*77c1e3ccSAndroid Build Coastguard Worker   // For target level, the settings should accumulate rather than overwrite,
873*77c1e3ccSAndroid Build Coastguard Worker   // so we simply append it.
874*77c1e3ccSAndroid Build Coastguard Worker   if (key == AV1E_SET_TARGET_SEQ_LEVEL_IDX) {
875*77c1e3ccSAndroid Build Coastguard Worker     j = config->arg_ctrl_cnt;
876*77c1e3ccSAndroid Build Coastguard Worker     assert(j < ARG_CTRL_CNT_MAX);
877*77c1e3ccSAndroid Build Coastguard Worker     config->arg_ctrls[j][0] = key;
878*77c1e3ccSAndroid Build Coastguard Worker     config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
879*77c1e3ccSAndroid Build Coastguard Worker     ++config->arg_ctrl_cnt;
880*77c1e3ccSAndroid Build Coastguard Worker     return;
881*77c1e3ccSAndroid Build Coastguard Worker   }
882*77c1e3ccSAndroid Build Coastguard Worker 
883*77c1e3ccSAndroid Build Coastguard Worker   /* Point either to the next free element or the first instance of this
884*77c1e3ccSAndroid Build Coastguard Worker    * control.
885*77c1e3ccSAndroid Build Coastguard Worker    */
886*77c1e3ccSAndroid Build Coastguard Worker   for (j = 0; j < config->arg_ctrl_cnt; j++)
887*77c1e3ccSAndroid Build Coastguard Worker     if (config->arg_ctrls[j][0] == key) break;
888*77c1e3ccSAndroid Build Coastguard Worker 
889*77c1e3ccSAndroid Build Coastguard Worker   /* Update/insert */
890*77c1e3ccSAndroid Build Coastguard Worker   assert(j < ARG_CTRL_CNT_MAX);
891*77c1e3ccSAndroid Build Coastguard Worker   config->arg_ctrls[j][0] = key;
892*77c1e3ccSAndroid Build Coastguard Worker   config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
893*77c1e3ccSAndroid Build Coastguard Worker 
894*77c1e3ccSAndroid Build Coastguard Worker   if (key == AOME_SET_ENABLEAUTOALTREF && config->arg_ctrls[j][1] > 1) {
895*77c1e3ccSAndroid Build Coastguard Worker     aom_tools_warn(
896*77c1e3ccSAndroid Build Coastguard Worker         "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
897*77c1e3ccSAndroid Build Coastguard Worker     config->arg_ctrls[j][1] = 1;
898*77c1e3ccSAndroid Build Coastguard Worker   }
899*77c1e3ccSAndroid Build Coastguard Worker 
900*77c1e3ccSAndroid Build Coastguard Worker   if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
901*77c1e3ccSAndroid Build Coastguard Worker }
902*77c1e3ccSAndroid Build Coastguard Worker 
set_config_arg_key_vals(struct stream_config * config,const char * name,const struct arg * arg)903*77c1e3ccSAndroid Build Coastguard Worker static void set_config_arg_key_vals(struct stream_config *config,
904*77c1e3ccSAndroid Build Coastguard Worker                                     const char *name, const struct arg *arg) {
905*77c1e3ccSAndroid Build Coastguard Worker   int j;
906*77c1e3ccSAndroid Build Coastguard Worker   const char *val = arg->val;
907*77c1e3ccSAndroid Build Coastguard Worker   // For target level, the settings should accumulate rather than overwrite,
908*77c1e3ccSAndroid Build Coastguard Worker   // so we simply append it.
909*77c1e3ccSAndroid Build Coastguard Worker   if (strcmp(name, "target-seq-level-idx") == 0) {
910*77c1e3ccSAndroid Build Coastguard Worker     j = config->arg_key_val_cnt;
911*77c1e3ccSAndroid Build Coastguard Worker     assert(j < ARG_KEY_VAL_CNT_MAX);
912*77c1e3ccSAndroid Build Coastguard Worker     config->arg_key_vals[j][0] = name;
913*77c1e3ccSAndroid Build Coastguard Worker     config->arg_key_vals[j][1] = val;
914*77c1e3ccSAndroid Build Coastguard Worker     ++config->arg_key_val_cnt;
915*77c1e3ccSAndroid Build Coastguard Worker     return;
916*77c1e3ccSAndroid Build Coastguard Worker   }
917*77c1e3ccSAndroid Build Coastguard Worker 
918*77c1e3ccSAndroid Build Coastguard Worker   /* Point either to the next free element or the first instance of this
919*77c1e3ccSAndroid Build Coastguard Worker    * option.
920*77c1e3ccSAndroid Build Coastguard Worker    */
921*77c1e3ccSAndroid Build Coastguard Worker   for (j = 0; j < config->arg_key_val_cnt; j++)
922*77c1e3ccSAndroid Build Coastguard Worker     if (strcmp(name, config->arg_key_vals[j][0]) == 0) break;
923*77c1e3ccSAndroid Build Coastguard Worker 
924*77c1e3ccSAndroid Build Coastguard Worker   /* Update/insert */
925*77c1e3ccSAndroid Build Coastguard Worker   assert(j < ARG_KEY_VAL_CNT_MAX);
926*77c1e3ccSAndroid Build Coastguard Worker   config->arg_key_vals[j][0] = name;
927*77c1e3ccSAndroid Build Coastguard Worker   config->arg_key_vals[j][1] = val;
928*77c1e3ccSAndroid Build Coastguard Worker 
929*77c1e3ccSAndroid Build Coastguard Worker   if (strcmp(name, g_av1_codec_arg_defs.auto_altref.long_name) == 0) {
930*77c1e3ccSAndroid Build Coastguard Worker     int auto_altref = arg_parse_int(arg);
931*77c1e3ccSAndroid Build Coastguard Worker     if (auto_altref > 1) {
932*77c1e3ccSAndroid Build Coastguard Worker       aom_tools_warn(
933*77c1e3ccSAndroid Build Coastguard Worker           "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
934*77c1e3ccSAndroid Build Coastguard Worker       config->arg_key_vals[j][1] = "1";
935*77c1e3ccSAndroid Build Coastguard Worker     }
936*77c1e3ccSAndroid Build Coastguard Worker   }
937*77c1e3ccSAndroid Build Coastguard Worker 
938*77c1e3ccSAndroid Build Coastguard Worker   if (j == config->arg_key_val_cnt) config->arg_key_val_cnt++;
939*77c1e3ccSAndroid Build Coastguard Worker }
940*77c1e3ccSAndroid Build Coastguard Worker 
parse_stream_params(struct AvxEncoderConfig * global,struct stream_state * stream,char ** argv)941*77c1e3ccSAndroid Build Coastguard Worker static int parse_stream_params(struct AvxEncoderConfig *global,
942*77c1e3ccSAndroid Build Coastguard Worker                                struct stream_state *stream, char **argv) {
943*77c1e3ccSAndroid Build Coastguard Worker   char **argi, **argj;
944*77c1e3ccSAndroid Build Coastguard Worker   struct arg arg;
945*77c1e3ccSAndroid Build Coastguard Worker   const arg_def_t *const *ctrl_args = no_args;
946*77c1e3ccSAndroid Build Coastguard Worker   const arg_def_t *const *key_val_args = no_args;
947*77c1e3ccSAndroid Build Coastguard Worker   const int *ctrl_args_map = NULL;
948*77c1e3ccSAndroid Build Coastguard Worker   struct stream_config *config = &stream->config;
949*77c1e3ccSAndroid Build Coastguard Worker   int eos_mark_found = 0;
950*77c1e3ccSAndroid Build Coastguard Worker   int webm_forced = 0;
951*77c1e3ccSAndroid Build Coastguard Worker 
952*77c1e3ccSAndroid Build Coastguard Worker   // Handle codec specific options
953*77c1e3ccSAndroid Build Coastguard Worker   if (0) {
954*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_ENCODER
955*77c1e3ccSAndroid Build Coastguard Worker   } else if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
956*77c1e3ccSAndroid Build Coastguard Worker     // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
957*77c1e3ccSAndroid Build Coastguard Worker     // Consider to expand this set for AV1 encoder control.
958*77c1e3ccSAndroid Build Coastguard Worker #if __STDC_VERSION__ >= 201112L
959*77c1e3ccSAndroid Build Coastguard Worker     _Static_assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map),
960*77c1e3ccSAndroid Build Coastguard Worker                    "The av1_ctrl_args and av1_arg_ctrl_map arrays must be of "
961*77c1e3ccSAndroid Build Coastguard Worker                    "the same size.");
962*77c1e3ccSAndroid Build Coastguard Worker #else
963*77c1e3ccSAndroid Build Coastguard Worker     assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map));
964*77c1e3ccSAndroid Build Coastguard Worker #endif
965*77c1e3ccSAndroid Build Coastguard Worker     ctrl_args = av1_ctrl_args;
966*77c1e3ccSAndroid Build Coastguard Worker     ctrl_args_map = av1_arg_ctrl_map;
967*77c1e3ccSAndroid Build Coastguard Worker     key_val_args = av1_key_val_args;
968*77c1e3ccSAndroid Build Coastguard Worker #endif
969*77c1e3ccSAndroid Build Coastguard Worker   }
970*77c1e3ccSAndroid Build Coastguard Worker 
971*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
972*77c1e3ccSAndroid Build Coastguard Worker     arg.argv_step = 1;
973*77c1e3ccSAndroid Build Coastguard Worker 
974*77c1e3ccSAndroid Build Coastguard Worker     /* Once we've found an end-of-stream marker (--) we want to continue
975*77c1e3ccSAndroid Build Coastguard Worker      * shifting arguments but not consuming them.
976*77c1e3ccSAndroid Build Coastguard Worker      */
977*77c1e3ccSAndroid Build Coastguard Worker     if (eos_mark_found) {
978*77c1e3ccSAndroid Build Coastguard Worker       argj++;
979*77c1e3ccSAndroid Build Coastguard Worker       continue;
980*77c1e3ccSAndroid Build Coastguard Worker     } else if (!strcmp(*argj, "--")) {
981*77c1e3ccSAndroid Build Coastguard Worker       eos_mark_found = 1;
982*77c1e3ccSAndroid Build Coastguard Worker       continue;
983*77c1e3ccSAndroid Build Coastguard Worker     }
984*77c1e3ccSAndroid Build Coastguard Worker 
985*77c1e3ccSAndroid Build Coastguard Worker     if (arg_match(&arg, &g_av1_codec_arg_defs.outputfile, argi)) {
986*77c1e3ccSAndroid Build Coastguard Worker       config->out_fn = arg.val;
987*77c1e3ccSAndroid Build Coastguard Worker       if (!webm_forced) {
988*77c1e3ccSAndroid Build Coastguard Worker         const size_t out_fn_len = strlen(config->out_fn);
989*77c1e3ccSAndroid Build Coastguard Worker         if (out_fn_len >= 4 &&
990*77c1e3ccSAndroid Build Coastguard Worker             !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
991*77c1e3ccSAndroid Build Coastguard Worker           config->write_webm = 0;
992*77c1e3ccSAndroid Build Coastguard Worker           config->write_ivf = 1;
993*77c1e3ccSAndroid Build Coastguard Worker         } else if (out_fn_len >= 4 &&
994*77c1e3ccSAndroid Build Coastguard Worker                    !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
995*77c1e3ccSAndroid Build Coastguard Worker           config->write_webm = 0;
996*77c1e3ccSAndroid Build Coastguard Worker           config->write_ivf = 0;
997*77c1e3ccSAndroid Build Coastguard Worker         }
998*77c1e3ccSAndroid Build Coastguard Worker       }
999*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.fpf_name, argi)) {
1000*77c1e3ccSAndroid Build Coastguard Worker       config->stats_fn = arg.val;
1001*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_webm, argi)) {
1002*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
1003*77c1e3ccSAndroid Build Coastguard Worker       config->write_webm = 1;
1004*77c1e3ccSAndroid Build Coastguard Worker       webm_forced = 1;
1005*77c1e3ccSAndroid Build Coastguard Worker #else
1006*77c1e3ccSAndroid Build Coastguard Worker       die("Error: --webm specified but webm is disabled.");
1007*77c1e3ccSAndroid Build Coastguard Worker #endif
1008*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_ivf, argi)) {
1009*77c1e3ccSAndroid Build Coastguard Worker       config->write_webm = 0;
1010*77c1e3ccSAndroid Build Coastguard Worker       config->write_ivf = 1;
1011*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_obu, argi)) {
1012*77c1e3ccSAndroid Build Coastguard Worker       config->write_webm = 0;
1013*77c1e3ccSAndroid Build Coastguard Worker       config->write_ivf = 0;
1014*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.threads, argi)) {
1015*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_threads = arg_parse_uint(&arg);
1016*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.profile, argi)) {
1017*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_profile = arg_parse_uint(&arg);
1018*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.width, argi)) {
1019*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_w = arg_parse_uint(&arg);
1020*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.height, argi)) {
1021*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_h = arg_parse_uint(&arg);
1022*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_width,
1023*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1024*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1025*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_height,
1026*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1027*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
1028*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.bitdeptharg, argi)) {
1029*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1030*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.inbitdeptharg, argi)) {
1031*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1032*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_x,
1033*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1034*77c1e3ccSAndroid Build Coastguard Worker       stream->chroma_subsampling_x = arg_parse_uint(&arg);
1035*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_y,
1036*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1037*77c1e3ccSAndroid Build Coastguard Worker       stream->chroma_subsampling_y = arg_parse_uint(&arg);
1038*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
1039*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.stereo_mode, argi)) {
1040*77c1e3ccSAndroid Build Coastguard Worker       config->stereo_fmt = arg_parse_enum_or_int(&arg);
1041*77c1e3ccSAndroid Build Coastguard Worker #endif
1042*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.timebase, argi)) {
1043*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_timebase = arg_parse_rational(&arg);
1044*77c1e3ccSAndroid Build Coastguard Worker       validate_positive_rational(arg.name, &config->cfg.g_timebase);
1045*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.global_error_resilient,
1046*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1047*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_error_resilient = arg_parse_uint(&arg);
1048*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.lag_in_frames, argi)) {
1049*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1050*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.large_scale_tile, argi)) {
1051*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.large_scale_tile = arg_parse_uint(&arg);
1052*77c1e3ccSAndroid Build Coastguard Worker       if (config->cfg.large_scale_tile) {
1053*77c1e3ccSAndroid Build Coastguard Worker         global->codec = get_aom_encoder_by_short_name("av1");
1054*77c1e3ccSAndroid Build Coastguard Worker       }
1055*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.monochrome, argi)) {
1056*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.monochrome = 1;
1057*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.full_still_picture_hdr,
1058*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1059*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.full_still_picture_hdr = 1;
1060*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_16bit_internal,
1061*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1062*77c1e3ccSAndroid Build Coastguard Worker       config->use_16bit_internal = CONFIG_AV1_HIGHBITDEPTH;
1063*77c1e3ccSAndroid Build Coastguard Worker       if (!config->use_16bit_internal) {
1064*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("%s option ignored with CONFIG_AV1_HIGHBITDEPTH=0.\n",
1065*77c1e3ccSAndroid Build Coastguard Worker                        arg.name);
1066*77c1e3ccSAndroid Build Coastguard Worker       }
1067*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.dropframe_thresh, argi)) {
1068*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1069*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_mode, argi)) {
1070*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1071*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_denominator,
1072*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1073*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1074*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_kf_denominator,
1075*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1076*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1077*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_mode, argi)) {
1078*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1079*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_denominator,
1080*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1081*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1082*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_denominator,
1083*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1084*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1085*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_qthresh, argi)) {
1086*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1087*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_qthresh,
1088*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1089*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1090*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.end_usage, argi)) {
1091*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1092*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.target_bitrate, argi)) {
1093*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1094*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.min_quantizer, argi)) {
1095*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1096*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.max_quantizer, argi)) {
1097*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1098*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.undershoot_pct, argi)) {
1099*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1100*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.overshoot_pct, argi)) {
1101*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1102*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_sz, argi)) {
1103*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1104*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_initial_sz, argi)) {
1105*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1106*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_optimal_sz, argi)) {
1107*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1108*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.bias_pct, argi)) {
1109*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1110*77c1e3ccSAndroid Build Coastguard Worker       if (global->passes < 2)
1111*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1112*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.minsection_pct, argi)) {
1113*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1114*77c1e3ccSAndroid Build Coastguard Worker 
1115*77c1e3ccSAndroid Build Coastguard Worker       if (global->passes < 2)
1116*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1117*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.maxsection_pct, argi)) {
1118*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1119*77c1e3ccSAndroid Build Coastguard Worker 
1120*77c1e3ccSAndroid Build Coastguard Worker       if (global->passes < 2)
1121*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1122*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.fwd_kf_enabled, argi)) {
1123*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.fwd_kf_enabled = arg_parse_uint(&arg);
1124*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_min_dist, argi)) {
1125*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.kf_min_dist = arg_parse_uint(&arg);
1126*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_max_dist, argi)) {
1127*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.kf_max_dist = arg_parse_uint(&arg);
1128*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_disabled, argi)) {
1129*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.kf_mode = AOM_KF_DISABLED;
1130*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_dist, argi)) {
1131*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.sframe_dist = arg_parse_uint(&arg);
1132*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_mode, argi)) {
1133*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.sframe_mode = arg_parse_uint(&arg);
1134*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.save_as_annexb, argi)) {
1135*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.save_as_annexb = arg_parse_uint(&arg);
1136*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_width, argi)) {
1137*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.tile_width_count =
1138*77c1e3ccSAndroid Build Coastguard Worker           arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1139*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_height, argi)) {
1140*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.tile_height_count =
1141*77c1e3ccSAndroid Build Coastguard Worker           arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1142*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
1143*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argi)) {
1144*77c1e3ccSAndroid Build Coastguard Worker       config->vmaf_model_path = arg.val;
1145*77c1e3ccSAndroid Build Coastguard Worker #endif
1146*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.partition_info_path,
1147*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1148*77c1e3ccSAndroid Build Coastguard Worker       config->partition_info_path = arg.val;
1149*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
1150*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1151*77c1e3ccSAndroid Build Coastguard Worker       config->enable_rate_guide_deltaq = arg_parse_uint(&arg);
1152*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_distribution_info,
1153*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1154*77c1e3ccSAndroid Build Coastguard Worker       config->rate_distribution_info = arg.val;
1155*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_fixed_qp_offsets,
1156*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1157*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.use_fixed_qp_offsets = arg_parse_uint(&arg);
1158*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.fixed_qp_offsets, argi)) {
1159*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.use_fixed_qp_offsets = 1;
1160*77c1e3ccSAndroid Build Coastguard Worker     } else if (global->usage == AOM_USAGE_REALTIME &&
1161*77c1e3ccSAndroid Build Coastguard Worker                arg_match(&arg, &g_av1_codec_arg_defs.enable_restoration,
1162*77c1e3ccSAndroid Build Coastguard Worker                          argi)) {
1163*77c1e3ccSAndroid Build Coastguard Worker       if (arg_parse_uint(&arg) == 1) {
1164*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("non-zero %s option ignored in realtime mode.\n",
1165*77c1e3ccSAndroid Build Coastguard Worker                        arg.name);
1166*77c1e3ccSAndroid Build Coastguard Worker       }
1167*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_input, argi)) {
1168*77c1e3ccSAndroid Build Coastguard Worker       config->two_pass_input = arg.val;
1169*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_output, argi)) {
1170*77c1e3ccSAndroid Build Coastguard Worker       config->two_pass_output = arg.val;
1171*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_width, argi)) {
1172*77c1e3ccSAndroid Build Coastguard Worker       config->two_pass_width = arg_parse_int(&arg);
1173*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_height, argi)) {
1174*77c1e3ccSAndroid Build Coastguard Worker       config->two_pass_height = arg_parse_int(&arg);
1175*77c1e3ccSAndroid Build Coastguard Worker     } else {
1176*77c1e3ccSAndroid Build Coastguard Worker       int i, match = 0;
1177*77c1e3ccSAndroid Build Coastguard Worker       // check if the control ID API supports this arg
1178*77c1e3ccSAndroid Build Coastguard Worker       if (ctrl_args_map) {
1179*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; ctrl_args[i]; i++) {
1180*77c1e3ccSAndroid Build Coastguard Worker           if (arg_match(&arg, ctrl_args[i], argi)) {
1181*77c1e3ccSAndroid Build Coastguard Worker             match = 1;
1182*77c1e3ccSAndroid Build Coastguard Worker             set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1183*77c1e3ccSAndroid Build Coastguard Worker             break;
1184*77c1e3ccSAndroid Build Coastguard Worker           }
1185*77c1e3ccSAndroid Build Coastguard Worker         }
1186*77c1e3ccSAndroid Build Coastguard Worker       }
1187*77c1e3ccSAndroid Build Coastguard Worker       if (!match) {
1188*77c1e3ccSAndroid Build Coastguard Worker         // check if the key & value API supports this arg
1189*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; key_val_args[i]; i++) {
1190*77c1e3ccSAndroid Build Coastguard Worker           if (arg_match(&arg, key_val_args[i], argi)) {
1191*77c1e3ccSAndroid Build Coastguard Worker             match = 1;
1192*77c1e3ccSAndroid Build Coastguard Worker             set_config_arg_key_vals(config, key_val_args[i]->long_name, &arg);
1193*77c1e3ccSAndroid Build Coastguard Worker             break;
1194*77c1e3ccSAndroid Build Coastguard Worker           }
1195*77c1e3ccSAndroid Build Coastguard Worker         }
1196*77c1e3ccSAndroid Build Coastguard Worker       }
1197*77c1e3ccSAndroid Build Coastguard Worker       if (!match) argj++;
1198*77c1e3ccSAndroid Build Coastguard Worker     }
1199*77c1e3ccSAndroid Build Coastguard Worker   }
1200*77c1e3ccSAndroid Build Coastguard Worker   config->use_16bit_internal |= config->cfg.g_bit_depth > AOM_BITS_8;
1201*77c1e3ccSAndroid Build Coastguard Worker 
1202*77c1e3ccSAndroid Build Coastguard Worker   if (global->usage == AOM_USAGE_REALTIME && config->cfg.g_lag_in_frames != 0) {
1203*77c1e3ccSAndroid Build Coastguard Worker     aom_tools_warn("non-zero lag-in-frames option ignored in realtime mode.\n");
1204*77c1e3ccSAndroid Build Coastguard Worker     config->cfg.g_lag_in_frames = 0;
1205*77c1e3ccSAndroid Build Coastguard Worker   }
1206*77c1e3ccSAndroid Build Coastguard Worker 
1207*77c1e3ccSAndroid Build Coastguard Worker   if (global->usage == AOM_USAGE_ALL_INTRA) {
1208*77c1e3ccSAndroid Build Coastguard Worker     if (config->cfg.g_lag_in_frames != 0) {
1209*77c1e3ccSAndroid Build Coastguard Worker       aom_tools_warn(
1210*77c1e3ccSAndroid Build Coastguard Worker           "non-zero lag-in-frames option ignored in all intra mode.\n");
1211*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.g_lag_in_frames = 0;
1212*77c1e3ccSAndroid Build Coastguard Worker     }
1213*77c1e3ccSAndroid Build Coastguard Worker     if (config->cfg.kf_max_dist != 0) {
1214*77c1e3ccSAndroid Build Coastguard Worker       aom_tools_warn(
1215*77c1e3ccSAndroid Build Coastguard Worker           "non-zero max key frame distance option ignored in all intra "
1216*77c1e3ccSAndroid Build Coastguard Worker           "mode.\n");
1217*77c1e3ccSAndroid Build Coastguard Worker       config->cfg.kf_max_dist = 0;
1218*77c1e3ccSAndroid Build Coastguard Worker     }
1219*77c1e3ccSAndroid Build Coastguard Worker   }
1220*77c1e3ccSAndroid Build Coastguard Worker 
1221*77c1e3ccSAndroid Build Coastguard Worker   // set the passes field using key & val API
1222*77c1e3ccSAndroid Build Coastguard Worker   if (config->arg_key_val_cnt >= ARG_KEY_VAL_CNT_MAX) {
1223*77c1e3ccSAndroid Build Coastguard Worker     die("Not enough buffer for the key & value API.");
1224*77c1e3ccSAndroid Build Coastguard Worker   }
1225*77c1e3ccSAndroid Build Coastguard Worker   config->arg_key_vals[config->arg_key_val_cnt][0] = "passes";
1226*77c1e3ccSAndroid Build Coastguard Worker   switch (global->passes) {
1227*77c1e3ccSAndroid Build Coastguard Worker     case 0: config->arg_key_vals[config->arg_key_val_cnt][1] = "0"; break;
1228*77c1e3ccSAndroid Build Coastguard Worker     case 1: config->arg_key_vals[config->arg_key_val_cnt][1] = "1"; break;
1229*77c1e3ccSAndroid Build Coastguard Worker     case 2: config->arg_key_vals[config->arg_key_val_cnt][1] = "2"; break;
1230*77c1e3ccSAndroid Build Coastguard Worker     case 3: config->arg_key_vals[config->arg_key_val_cnt][1] = "3"; break;
1231*77c1e3ccSAndroid Build Coastguard Worker     default: die("Invalid value of --passes.");
1232*77c1e3ccSAndroid Build Coastguard Worker   }
1233*77c1e3ccSAndroid Build Coastguard Worker   config->arg_key_val_cnt++;
1234*77c1e3ccSAndroid Build Coastguard Worker 
1235*77c1e3ccSAndroid Build Coastguard Worker   // set the two_pass_output field
1236*77c1e3ccSAndroid Build Coastguard Worker   if (!config->two_pass_output && global->passes == 3) {
1237*77c1e3ccSAndroid Build Coastguard Worker     // If not specified, set the name of two_pass_output file here.
1238*77c1e3ccSAndroid Build Coastguard Worker     snprintf(stream->tmp_out_fn, sizeof(stream->tmp_out_fn),
1239*77c1e3ccSAndroid Build Coastguard Worker              "%.980s_pass2_%d.ivf", stream->config.out_fn, stream->index);
1240*77c1e3ccSAndroid Build Coastguard Worker     stream->config.two_pass_output = stream->tmp_out_fn;
1241*77c1e3ccSAndroid Build Coastguard Worker   }
1242*77c1e3ccSAndroid Build Coastguard Worker   if (config->two_pass_output) {
1243*77c1e3ccSAndroid Build Coastguard Worker     config->arg_key_vals[config->arg_key_val_cnt][0] = "two-pass-output";
1244*77c1e3ccSAndroid Build Coastguard Worker     config->arg_key_vals[config->arg_key_val_cnt][1] = config->two_pass_output;
1245*77c1e3ccSAndroid Build Coastguard Worker     config->arg_key_val_cnt++;
1246*77c1e3ccSAndroid Build Coastguard Worker   }
1247*77c1e3ccSAndroid Build Coastguard Worker 
1248*77c1e3ccSAndroid Build Coastguard Worker   return eos_mark_found;
1249*77c1e3ccSAndroid Build Coastguard Worker }
1250*77c1e3ccSAndroid Build Coastguard Worker 
1251*77c1e3ccSAndroid Build Coastguard Worker #define FOREACH_STREAM(iterator, list)                 \
1252*77c1e3ccSAndroid Build Coastguard Worker   for (struct stream_state *iterator = list; iterator; \
1253*77c1e3ccSAndroid Build Coastguard Worker        iterator = iterator->next)
1254*77c1e3ccSAndroid Build Coastguard Worker 
validate_stream_config(const struct stream_state * stream,const struct AvxEncoderConfig * global)1255*77c1e3ccSAndroid Build Coastguard Worker static void validate_stream_config(const struct stream_state *stream,
1256*77c1e3ccSAndroid Build Coastguard Worker                                    const struct AvxEncoderConfig *global) {
1257*77c1e3ccSAndroid Build Coastguard Worker   const struct stream_state *streami;
1258*77c1e3ccSAndroid Build Coastguard Worker   (void)global;
1259*77c1e3ccSAndroid Build Coastguard Worker 
1260*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1261*77c1e3ccSAndroid Build Coastguard Worker     fatal(
1262*77c1e3ccSAndroid Build Coastguard Worker         "Stream %d: Specify stream dimensions with --width (-w) "
1263*77c1e3ccSAndroid Build Coastguard Worker         " and --height (-h)",
1264*77c1e3ccSAndroid Build Coastguard Worker         stream->index);
1265*77c1e3ccSAndroid Build Coastguard Worker 
1266*77c1e3ccSAndroid Build Coastguard Worker   /* Even if bit depth is set on the command line flag to be lower,
1267*77c1e3ccSAndroid Build Coastguard Worker    * it is upgraded to at least match the input bit depth.
1268*77c1e3ccSAndroid Build Coastguard Worker    */
1269*77c1e3ccSAndroid Build Coastguard Worker   assert(stream->config.cfg.g_input_bit_depth <=
1270*77c1e3ccSAndroid Build Coastguard Worker          (unsigned int)stream->config.cfg.g_bit_depth);
1271*77c1e3ccSAndroid Build Coastguard Worker 
1272*77c1e3ccSAndroid Build Coastguard Worker   for (streami = stream; streami; streami = streami->next) {
1273*77c1e3ccSAndroid Build Coastguard Worker     /* All streams require output files */
1274*77c1e3ccSAndroid Build Coastguard Worker     if (!streami->config.out_fn)
1275*77c1e3ccSAndroid Build Coastguard Worker       fatal("Stream %d: Output file is required (specify with -o)",
1276*77c1e3ccSAndroid Build Coastguard Worker             streami->index);
1277*77c1e3ccSAndroid Build Coastguard Worker 
1278*77c1e3ccSAndroid Build Coastguard Worker     /* Check for two streams outputting to the same file */
1279*77c1e3ccSAndroid Build Coastguard Worker     if (streami != stream) {
1280*77c1e3ccSAndroid Build Coastguard Worker       const char *a = stream->config.out_fn;
1281*77c1e3ccSAndroid Build Coastguard Worker       const char *b = streami->config.out_fn;
1282*77c1e3ccSAndroid Build Coastguard Worker       if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1283*77c1e3ccSAndroid Build Coastguard Worker         fatal("Stream %d: duplicate output file (from stream %d)",
1284*77c1e3ccSAndroid Build Coastguard Worker               streami->index, stream->index);
1285*77c1e3ccSAndroid Build Coastguard Worker     }
1286*77c1e3ccSAndroid Build Coastguard Worker 
1287*77c1e3ccSAndroid Build Coastguard Worker     /* Check for two streams sharing a stats file. */
1288*77c1e3ccSAndroid Build Coastguard Worker     if (streami != stream) {
1289*77c1e3ccSAndroid Build Coastguard Worker       const char *a = stream->config.stats_fn;
1290*77c1e3ccSAndroid Build Coastguard Worker       const char *b = streami->config.stats_fn;
1291*77c1e3ccSAndroid Build Coastguard Worker       if (a && b && !strcmp(a, b))
1292*77c1e3ccSAndroid Build Coastguard Worker         fatal("Stream %d: duplicate stats file (from stream %d)",
1293*77c1e3ccSAndroid Build Coastguard Worker               streami->index, stream->index);
1294*77c1e3ccSAndroid Build Coastguard Worker     }
1295*77c1e3ccSAndroid Build Coastguard Worker   }
1296*77c1e3ccSAndroid Build Coastguard Worker }
1297*77c1e3ccSAndroid Build Coastguard Worker 
set_stream_dimensions(struct stream_state * stream,unsigned int w,unsigned int h)1298*77c1e3ccSAndroid Build Coastguard Worker static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1299*77c1e3ccSAndroid Build Coastguard Worker                                   unsigned int h) {
1300*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->config.cfg.g_w) {
1301*77c1e3ccSAndroid Build Coastguard Worker     if (!stream->config.cfg.g_h)
1302*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_w = w;
1303*77c1e3ccSAndroid Build Coastguard Worker     else
1304*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1305*77c1e3ccSAndroid Build Coastguard Worker   }
1306*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->config.cfg.g_h) {
1307*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1308*77c1e3ccSAndroid Build Coastguard Worker   }
1309*77c1e3ccSAndroid Build Coastguard Worker }
1310*77c1e3ccSAndroid Build Coastguard Worker 
file_type_to_string(enum VideoFileType t)1311*77c1e3ccSAndroid Build Coastguard Worker static const char *file_type_to_string(enum VideoFileType t) {
1312*77c1e3ccSAndroid Build Coastguard Worker   switch (t) {
1313*77c1e3ccSAndroid Build Coastguard Worker     case FILE_TYPE_RAW: return "RAW";
1314*77c1e3ccSAndroid Build Coastguard Worker     case FILE_TYPE_Y4M: return "Y4M";
1315*77c1e3ccSAndroid Build Coastguard Worker     default: return "Other";
1316*77c1e3ccSAndroid Build Coastguard Worker   }
1317*77c1e3ccSAndroid Build Coastguard Worker }
1318*77c1e3ccSAndroid Build Coastguard Worker 
show_stream_config(struct stream_state * stream,struct AvxEncoderConfig * global,struct AvxInputContext * input)1319*77c1e3ccSAndroid Build Coastguard Worker static void show_stream_config(struct stream_state *stream,
1320*77c1e3ccSAndroid Build Coastguard Worker                                struct AvxEncoderConfig *global,
1321*77c1e3ccSAndroid Build Coastguard Worker                                struct AvxInputContext *input) {
1322*77c1e3ccSAndroid Build Coastguard Worker #define SHOW(field) \
1323*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "    %-28s = %d\n", #field, stream->config.cfg.field)
1324*77c1e3ccSAndroid Build Coastguard Worker 
1325*77c1e3ccSAndroid Build Coastguard Worker   if (stream->index == 0) {
1326*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "Codec: %s\n", aom_codec_iface_name(global->codec));
1327*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1328*77c1e3ccSAndroid Build Coastguard Worker             input->filename, file_type_to_string(input->file_type),
1329*77c1e3ccSAndroid Build Coastguard Worker             image_format_to_string(input->fmt));
1330*77c1e3ccSAndroid Build Coastguard Worker   }
1331*77c1e3ccSAndroid Build Coastguard Worker   if (stream->next || stream->index)
1332*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "\nStream Index: %d\n", stream->index);
1333*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1334*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Coding path: %s\n",
1335*77c1e3ccSAndroid Build Coastguard Worker           stream->config.use_16bit_internal ? "HBD" : "LBD");
1336*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Encoder parameters:\n");
1337*77c1e3ccSAndroid Build Coastguard Worker 
1338*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_usage);
1339*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_threads);
1340*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_profile);
1341*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_w);
1342*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_h);
1343*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_bit_depth);
1344*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_input_bit_depth);
1345*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_timebase.num);
1346*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_timebase.den);
1347*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_error_resilient);
1348*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_pass);
1349*77c1e3ccSAndroid Build Coastguard Worker   SHOW(g_lag_in_frames);
1350*77c1e3ccSAndroid Build Coastguard Worker   SHOW(large_scale_tile);
1351*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_dropframe_thresh);
1352*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_resize_mode);
1353*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_resize_denominator);
1354*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_resize_kf_denominator);
1355*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_superres_mode);
1356*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_superres_denominator);
1357*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_superres_kf_denominator);
1358*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_superres_qthresh);
1359*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_superres_kf_qthresh);
1360*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_end_usage);
1361*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_target_bitrate);
1362*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_min_quantizer);
1363*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_max_quantizer);
1364*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_undershoot_pct);
1365*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_overshoot_pct);
1366*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_buf_sz);
1367*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_buf_initial_sz);
1368*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_buf_optimal_sz);
1369*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_2pass_vbr_bias_pct);
1370*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_2pass_vbr_minsection_pct);
1371*77c1e3ccSAndroid Build Coastguard Worker   SHOW(rc_2pass_vbr_maxsection_pct);
1372*77c1e3ccSAndroid Build Coastguard Worker   SHOW(fwd_kf_enabled);
1373*77c1e3ccSAndroid Build Coastguard Worker   SHOW(kf_mode);
1374*77c1e3ccSAndroid Build Coastguard Worker   SHOW(kf_min_dist);
1375*77c1e3ccSAndroid Build Coastguard Worker   SHOW(kf_max_dist);
1376*77c1e3ccSAndroid Build Coastguard Worker 
1377*77c1e3ccSAndroid Build Coastguard Worker #define SHOW_PARAMS(field)                    \
1378*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "    %-28s = %d\n", #field, \
1379*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.encoder_cfg.field)
1380*77c1e3ccSAndroid Build Coastguard Worker   if (global->encoder_config.init_by_cfg_file) {
1381*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(super_block_size);
1382*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(max_partition_size);
1383*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(min_partition_size);
1384*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_ab_partition_type);
1385*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_rect_partition_type);
1386*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_1to4_partition_type);
1387*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_flip_idtx);
1388*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_cdef);
1389*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_lr);
1390*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_obmc);
1391*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_warp_motion);
1392*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_global_motion);
1393*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_dist_wtd_comp);
1394*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_diff_wtd_comp);
1395*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_inter_intra_comp);
1396*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_masked_comp);
1397*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_one_sided_comp);
1398*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_palette);
1399*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_intrabc);
1400*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_cfl);
1401*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_smooth_intra);
1402*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_filter_intra);
1403*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_dual_filter);
1404*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_intra_angle_delta);
1405*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_intra_edge_filter);
1406*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_tx_64x64);
1407*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_smooth_inter_intra);
1408*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_inter_inter_wedge);
1409*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_inter_intra_wedge);
1410*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_paeth_intra);
1411*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_trellis_quant);
1412*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(disable_ref_frame_mv);
1413*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(reduced_reference_set);
1414*77c1e3ccSAndroid Build Coastguard Worker     SHOW_PARAMS(reduced_tx_type_set);
1415*77c1e3ccSAndroid Build Coastguard Worker   }
1416*77c1e3ccSAndroid Build Coastguard Worker }
1417*77c1e3ccSAndroid Build Coastguard Worker 
open_output_file(struct stream_state * stream,struct AvxEncoderConfig * global,const struct AvxRational * pixel_aspect_ratio,const char * encoder_settings)1418*77c1e3ccSAndroid Build Coastguard Worker static void open_output_file(struct stream_state *stream,
1419*77c1e3ccSAndroid Build Coastguard Worker                              struct AvxEncoderConfig *global,
1420*77c1e3ccSAndroid Build Coastguard Worker                              const struct AvxRational *pixel_aspect_ratio,
1421*77c1e3ccSAndroid Build Coastguard Worker                              const char *encoder_settings) {
1422*77c1e3ccSAndroid Build Coastguard Worker   const char *fn = stream->config.out_fn;
1423*77c1e3ccSAndroid Build Coastguard Worker   const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1424*77c1e3ccSAndroid Build Coastguard Worker 
1425*77c1e3ccSAndroid Build Coastguard Worker   if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1426*77c1e3ccSAndroid Build Coastguard Worker 
1427*77c1e3ccSAndroid Build Coastguard Worker   stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1428*77c1e3ccSAndroid Build Coastguard Worker 
1429*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->file) fatal("Failed to open output file");
1430*77c1e3ccSAndroid Build Coastguard Worker 
1431*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1432*77c1e3ccSAndroid Build Coastguard Worker     fatal("WebM output to pipes not supported.");
1433*77c1e3ccSAndroid Build Coastguard Worker 
1434*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
1435*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.write_webm) {
1436*77c1e3ccSAndroid Build Coastguard Worker     stream->webm_ctx.stream = stream->file;
1437*77c1e3ccSAndroid Build Coastguard Worker     if (write_webm_file_header(&stream->webm_ctx, &stream->encoder, cfg,
1438*77c1e3ccSAndroid Build Coastguard Worker                                stream->config.stereo_fmt,
1439*77c1e3ccSAndroid Build Coastguard Worker                                get_fourcc_by_aom_encoder(global->codec),
1440*77c1e3ccSAndroid Build Coastguard Worker                                pixel_aspect_ratio, encoder_settings) != 0) {
1441*77c1e3ccSAndroid Build Coastguard Worker       fatal("WebM writer initialization failed.");
1442*77c1e3ccSAndroid Build Coastguard Worker     }
1443*77c1e3ccSAndroid Build Coastguard Worker   }
1444*77c1e3ccSAndroid Build Coastguard Worker #else
1445*77c1e3ccSAndroid Build Coastguard Worker   (void)pixel_aspect_ratio;
1446*77c1e3ccSAndroid Build Coastguard Worker   (void)encoder_settings;
1447*77c1e3ccSAndroid Build Coastguard Worker #endif
1448*77c1e3ccSAndroid Build Coastguard Worker 
1449*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->config.write_webm && stream->config.write_ivf) {
1450*77c1e3ccSAndroid Build Coastguard Worker     ivf_write_file_header(stream->file, cfg,
1451*77c1e3ccSAndroid Build Coastguard Worker                           get_fourcc_by_aom_encoder(global->codec), 0);
1452*77c1e3ccSAndroid Build Coastguard Worker   }
1453*77c1e3ccSAndroid Build Coastguard Worker }
1454*77c1e3ccSAndroid Build Coastguard Worker 
close_output_file(struct stream_state * stream,unsigned int fourcc)1455*77c1e3ccSAndroid Build Coastguard Worker static void close_output_file(struct stream_state *stream,
1456*77c1e3ccSAndroid Build Coastguard Worker                               unsigned int fourcc) {
1457*77c1e3ccSAndroid Build Coastguard Worker   const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1458*77c1e3ccSAndroid Build Coastguard Worker 
1459*77c1e3ccSAndroid Build Coastguard Worker   if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1460*77c1e3ccSAndroid Build Coastguard Worker 
1461*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
1462*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.write_webm) {
1463*77c1e3ccSAndroid Build Coastguard Worker     if (write_webm_file_footer(&stream->webm_ctx) != 0) {
1464*77c1e3ccSAndroid Build Coastguard Worker       fatal("WebM writer finalization failed.");
1465*77c1e3ccSAndroid Build Coastguard Worker     }
1466*77c1e3ccSAndroid Build Coastguard Worker   }
1467*77c1e3ccSAndroid Build Coastguard Worker #endif
1468*77c1e3ccSAndroid Build Coastguard Worker 
1469*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->config.write_webm && stream->config.write_ivf) {
1470*77c1e3ccSAndroid Build Coastguard Worker     if (!fseek(stream->file, 0, SEEK_SET))
1471*77c1e3ccSAndroid Build Coastguard Worker       ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1472*77c1e3ccSAndroid Build Coastguard Worker                             stream->frames_out);
1473*77c1e3ccSAndroid Build Coastguard Worker   }
1474*77c1e3ccSAndroid Build Coastguard Worker 
1475*77c1e3ccSAndroid Build Coastguard Worker   fclose(stream->file);
1476*77c1e3ccSAndroid Build Coastguard Worker }
1477*77c1e3ccSAndroid Build Coastguard Worker 
setup_pass(struct stream_state * stream,struct AvxEncoderConfig * global,int pass)1478*77c1e3ccSAndroid Build Coastguard Worker static void setup_pass(struct stream_state *stream,
1479*77c1e3ccSAndroid Build Coastguard Worker                        struct AvxEncoderConfig *global, int pass) {
1480*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.stats_fn) {
1481*77c1e3ccSAndroid Build Coastguard Worker     if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1482*77c1e3ccSAndroid Build Coastguard Worker       fatal("Failed to open statistics store");
1483*77c1e3ccSAndroid Build Coastguard Worker   } else {
1484*77c1e3ccSAndroid Build Coastguard Worker     if (!stats_open_mem(&stream->stats, pass))
1485*77c1e3ccSAndroid Build Coastguard Worker       fatal("Failed to open statistics store");
1486*77c1e3ccSAndroid Build Coastguard Worker   }
1487*77c1e3ccSAndroid Build Coastguard Worker 
1488*77c1e3ccSAndroid Build Coastguard Worker   if (global->passes == 1) {
1489*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.g_pass = AOM_RC_ONE_PASS;
1490*77c1e3ccSAndroid Build Coastguard Worker   } else {
1491*77c1e3ccSAndroid Build Coastguard Worker     switch (pass) {
1492*77c1e3ccSAndroid Build Coastguard Worker       case 0: stream->config.cfg.g_pass = AOM_RC_FIRST_PASS; break;
1493*77c1e3ccSAndroid Build Coastguard Worker       case 1: stream->config.cfg.g_pass = AOM_RC_SECOND_PASS; break;
1494*77c1e3ccSAndroid Build Coastguard Worker       case 2: stream->config.cfg.g_pass = AOM_RC_THIRD_PASS; break;
1495*77c1e3ccSAndroid Build Coastguard Worker       default: fatal("Failed to set pass");
1496*77c1e3ccSAndroid Build Coastguard Worker     }
1497*77c1e3ccSAndroid Build Coastguard Worker   }
1498*77c1e3ccSAndroid Build Coastguard Worker 
1499*77c1e3ccSAndroid Build Coastguard Worker   if (pass) {
1500*77c1e3ccSAndroid Build Coastguard Worker     stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1501*77c1e3ccSAndroid Build Coastguard Worker   }
1502*77c1e3ccSAndroid Build Coastguard Worker 
1503*77c1e3ccSAndroid Build Coastguard Worker   stream->cx_time = 0;
1504*77c1e3ccSAndroid Build Coastguard Worker   stream->nbytes = 0;
1505*77c1e3ccSAndroid Build Coastguard Worker   stream->frames_out = 0;
1506*77c1e3ccSAndroid Build Coastguard Worker }
1507*77c1e3ccSAndroid Build Coastguard Worker 
initialize_encoder(struct stream_state * stream,struct AvxEncoderConfig * global)1508*77c1e3ccSAndroid Build Coastguard Worker static void initialize_encoder(struct stream_state *stream,
1509*77c1e3ccSAndroid Build Coastguard Worker                                struct AvxEncoderConfig *global) {
1510*77c1e3ccSAndroid Build Coastguard Worker   int i;
1511*77c1e3ccSAndroid Build Coastguard Worker   int flags = 0;
1512*77c1e3ccSAndroid Build Coastguard Worker 
1513*77c1e3ccSAndroid Build Coastguard Worker   flags |= (global->show_psnr >= 1) ? AOM_CODEC_USE_PSNR : 0;
1514*77c1e3ccSAndroid Build Coastguard Worker   flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1515*77c1e3ccSAndroid Build Coastguard Worker 
1516*77c1e3ccSAndroid Build Coastguard Worker   /* Construct Encoder Context */
1517*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_enc_init(&stream->encoder, global->codec, &stream->config.cfg,
1518*77c1e3ccSAndroid Build Coastguard Worker                      flags);
1519*77c1e3ccSAndroid Build Coastguard Worker   ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1520*77c1e3ccSAndroid Build Coastguard Worker 
1521*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1522*77c1e3ccSAndroid Build Coastguard Worker     int ctrl = stream->config.arg_ctrls[i][0];
1523*77c1e3ccSAndroid Build Coastguard Worker     int value = stream->config.arg_ctrls[i][1];
1524*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_control(&stream->encoder, ctrl, value))
1525*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1526*77c1e3ccSAndroid Build Coastguard Worker 
1527*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1528*77c1e3ccSAndroid Build Coastguard Worker   }
1529*77c1e3ccSAndroid Build Coastguard Worker 
1530*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < stream->config.arg_key_val_cnt; i++) {
1531*77c1e3ccSAndroid Build Coastguard Worker     const char *name = stream->config.arg_key_vals[i][0];
1532*77c1e3ccSAndroid Build Coastguard Worker     const char *val = stream->config.arg_key_vals[i][1];
1533*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_set_option(&stream->encoder, name, val))
1534*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Error: Tried to set option %s = %s\n", name, val);
1535*77c1e3ccSAndroid Build Coastguard Worker 
1536*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to set codec option");
1537*77c1e3ccSAndroid Build Coastguard Worker   }
1538*77c1e3ccSAndroid Build Coastguard Worker 
1539*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_TUNE_VMAF
1540*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.vmaf_model_path) {
1541*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_VMAF_MODEL_PATH,
1542*77c1e3ccSAndroid Build Coastguard Worker                                   stream->config.vmaf_model_path);
1543*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to set vmaf model path");
1544*77c1e3ccSAndroid Build Coastguard Worker   }
1545*77c1e3ccSAndroid Build Coastguard Worker #endif
1546*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.partition_info_path) {
1547*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1548*77c1e3ccSAndroid Build Coastguard Worker                                   AV1E_SET_PARTITION_INFO_PATH,
1549*77c1e3ccSAndroid Build Coastguard Worker                                   stream->config.partition_info_path);
1550*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to set partition info path");
1551*77c1e3ccSAndroid Build Coastguard Worker   }
1552*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.enable_rate_guide_deltaq) {
1553*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1554*77c1e3ccSAndroid Build Coastguard Worker                                   AV1E_ENABLE_RATE_GUIDE_DELTAQ,
1555*77c1e3ccSAndroid Build Coastguard Worker                                   stream->config.enable_rate_guide_deltaq);
1556*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to enable rate guide deltaq");
1557*77c1e3ccSAndroid Build Coastguard Worker   }
1558*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.rate_distribution_info) {
1559*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1560*77c1e3ccSAndroid Build Coastguard Worker                                   AV1E_SET_RATE_DISTRIBUTION_INFO,
1561*77c1e3ccSAndroid Build Coastguard Worker                                   stream->config.rate_distribution_info);
1562*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to set rate distribution info");
1563*77c1e3ccSAndroid Build Coastguard Worker   }
1564*77c1e3ccSAndroid Build Coastguard Worker 
1565*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.film_grain_filename) {
1566*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_FILM_GRAIN_TABLE,
1567*77c1e3ccSAndroid Build Coastguard Worker                                   stream->config.film_grain_filename);
1568*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to set film grain table");
1569*77c1e3ccSAndroid Build Coastguard Worker   }
1570*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1E_SET_COLOR_RANGE,
1571*77c1e3ccSAndroid Build Coastguard Worker                                 stream->config.color_range);
1572*77c1e3ccSAndroid Build Coastguard Worker   ctx_exit_on_error(&stream->encoder, "Failed to set color range");
1573*77c1e3ccSAndroid Build Coastguard Worker 
1574*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
1575*77c1e3ccSAndroid Build Coastguard Worker   if (global->test_decode != TEST_DECODE_OFF) {
1576*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(
1577*77c1e3ccSAndroid Build Coastguard Worker         get_short_name_by_aom_encoder(global->codec));
1578*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_dec_cfg_t cfg = { 0, 0, 0, !stream->config.use_16bit_internal };
1579*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_dec_init(&stream->decoder, decoder, &cfg, 0);
1580*77c1e3ccSAndroid Build Coastguard Worker 
1581*77c1e3ccSAndroid Build Coastguard Worker     if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
1582*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_TILE_MODE,
1583*77c1e3ccSAndroid Build Coastguard Worker                                     stream->config.cfg.large_scale_tile);
1584*77c1e3ccSAndroid Build Coastguard Worker       ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1585*77c1e3ccSAndroid Build Coastguard Worker 
1586*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1D_SET_IS_ANNEXB,
1587*77c1e3ccSAndroid Build Coastguard Worker                                     stream->config.cfg.save_as_annexb);
1588*77c1e3ccSAndroid Build Coastguard Worker       ctx_exit_on_error(&stream->decoder, "Failed to set is_annexb");
1589*77c1e3ccSAndroid Build Coastguard Worker 
1590*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_ROW,
1591*77c1e3ccSAndroid Build Coastguard Worker                                     -1);
1592*77c1e3ccSAndroid Build Coastguard Worker       ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1593*77c1e3ccSAndroid Build Coastguard Worker 
1594*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_COL,
1595*77c1e3ccSAndroid Build Coastguard Worker                                     -1);
1596*77c1e3ccSAndroid Build Coastguard Worker       ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1597*77c1e3ccSAndroid Build Coastguard Worker     }
1598*77c1e3ccSAndroid Build Coastguard Worker   }
1599*77c1e3ccSAndroid Build Coastguard Worker #endif
1600*77c1e3ccSAndroid Build Coastguard Worker }
1601*77c1e3ccSAndroid Build Coastguard Worker 
1602*77c1e3ccSAndroid Build Coastguard Worker // Convert the input image 'img' to a monochrome image. The Y plane of the
1603*77c1e3ccSAndroid Build Coastguard Worker // output image is a shallow copy of the Y plane of the input image, therefore
1604*77c1e3ccSAndroid Build Coastguard Worker // the input image must remain valid for the lifetime of the output image. The U
1605*77c1e3ccSAndroid Build Coastguard Worker // and V planes of the output image are set to null pointers. The output image
1606*77c1e3ccSAndroid Build Coastguard Worker // format is AOM_IMG_FMT_I420 because libaom does not have AOM_IMG_FMT_I400.
convert_image_to_monochrome(const struct aom_image * img,struct aom_image * monochrome_img)1607*77c1e3ccSAndroid Build Coastguard Worker static void convert_image_to_monochrome(const struct aom_image *img,
1608*77c1e3ccSAndroid Build Coastguard Worker                                         struct aom_image *monochrome_img) {
1609*77c1e3ccSAndroid Build Coastguard Worker   *monochrome_img = *img;
1610*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->fmt = AOM_IMG_FMT_I420;
1611*77c1e3ccSAndroid Build Coastguard Worker   if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1612*77c1e3ccSAndroid Build Coastguard Worker     monochrome_img->fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1613*77c1e3ccSAndroid Build Coastguard Worker   }
1614*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->monochrome = 1;
1615*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->csp = AOM_CSP_UNKNOWN;
1616*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->x_chroma_shift = 1;
1617*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->y_chroma_shift = 1;
1618*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->planes[AOM_PLANE_U] = NULL;
1619*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->planes[AOM_PLANE_V] = NULL;
1620*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->stride[AOM_PLANE_U] = 0;
1621*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->stride[AOM_PLANE_V] = 0;
1622*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->sz = 0;
1623*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->bps = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
1624*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->img_data = NULL;
1625*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->img_data_owner = 0;
1626*77c1e3ccSAndroid Build Coastguard Worker   monochrome_img->self_allocd = 0;
1627*77c1e3ccSAndroid Build Coastguard Worker }
1628*77c1e3ccSAndroid Build Coastguard Worker 
encode_frame(struct stream_state * stream,struct AvxEncoderConfig * global,struct aom_image * img,unsigned int frames_in)1629*77c1e3ccSAndroid Build Coastguard Worker static void encode_frame(struct stream_state *stream,
1630*77c1e3ccSAndroid Build Coastguard Worker                          struct AvxEncoderConfig *global, struct aom_image *img,
1631*77c1e3ccSAndroid Build Coastguard Worker                          unsigned int frames_in) {
1632*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_pts_t frame_start, next_frame_start;
1633*77c1e3ccSAndroid Build Coastguard Worker   struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1634*77c1e3ccSAndroid Build Coastguard Worker   struct aom_usec_timer timer;
1635*77c1e3ccSAndroid Build Coastguard Worker 
1636*77c1e3ccSAndroid Build Coastguard Worker   frame_start =
1637*77c1e3ccSAndroid Build Coastguard Worker       (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1638*77c1e3ccSAndroid Build Coastguard Worker       cfg->g_timebase.num / global->framerate.num;
1639*77c1e3ccSAndroid Build Coastguard Worker   next_frame_start =
1640*77c1e3ccSAndroid Build Coastguard Worker       (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1641*77c1e3ccSAndroid Build Coastguard Worker       cfg->g_timebase.num / global->framerate.num;
1642*77c1e3ccSAndroid Build Coastguard Worker 
1643*77c1e3ccSAndroid Build Coastguard Worker   /* Scale if necessary */
1644*77c1e3ccSAndroid Build Coastguard Worker   if (img) {
1645*77c1e3ccSAndroid Build Coastguard Worker     if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1646*77c1e3ccSAndroid Build Coastguard Worker         (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1647*77c1e3ccSAndroid Build Coastguard Worker       if (img->fmt != AOM_IMG_FMT_I42016) {
1648*77c1e3ccSAndroid Build Coastguard Worker         fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1649*77c1e3ccSAndroid Build Coastguard Worker         exit(EXIT_FAILURE);
1650*77c1e3ccSAndroid Build Coastguard Worker       }
1651*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_LIBYUV
1652*77c1e3ccSAndroid Build Coastguard Worker       if (!stream->img) {
1653*77c1e3ccSAndroid Build Coastguard Worker         stream->img =
1654*77c1e3ccSAndroid Build Coastguard Worker             aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1655*77c1e3ccSAndroid Build Coastguard Worker       }
1656*77c1e3ccSAndroid Build Coastguard Worker       I420Scale_16(
1657*77c1e3ccSAndroid Build Coastguard Worker           (uint16_t *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1658*77c1e3ccSAndroid Build Coastguard Worker           (uint16_t *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1659*77c1e3ccSAndroid Build Coastguard Worker           (uint16_t *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1660*77c1e3ccSAndroid Build Coastguard Worker           img->d_w, img->d_h, (uint16_t *)stream->img->planes[AOM_PLANE_Y],
1661*77c1e3ccSAndroid Build Coastguard Worker           stream->img->stride[AOM_PLANE_Y] / 2,
1662*77c1e3ccSAndroid Build Coastguard Worker           (uint16_t *)stream->img->planes[AOM_PLANE_U],
1663*77c1e3ccSAndroid Build Coastguard Worker           stream->img->stride[AOM_PLANE_U] / 2,
1664*77c1e3ccSAndroid Build Coastguard Worker           (uint16_t *)stream->img->planes[AOM_PLANE_V],
1665*77c1e3ccSAndroid Build Coastguard Worker           stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1666*77c1e3ccSAndroid Build Coastguard Worker           stream->img->d_h, kFilterBox);
1667*77c1e3ccSAndroid Build Coastguard Worker       img = stream->img;
1668*77c1e3ccSAndroid Build Coastguard Worker #else
1669*77c1e3ccSAndroid Build Coastguard Worker       stream->encoder.err = 1;
1670*77c1e3ccSAndroid Build Coastguard Worker       ctx_exit_on_error(&stream->encoder,
1671*77c1e3ccSAndroid Build Coastguard Worker                         "Stream %d: Failed to encode frame.\n"
1672*77c1e3ccSAndroid Build Coastguard Worker                         "libyuv is required for scaling but is currently "
1673*77c1e3ccSAndroid Build Coastguard Worker                         "disabled.\n"
1674*77c1e3ccSAndroid Build Coastguard Worker                         "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1675*77c1e3ccSAndroid Build Coastguard Worker                         "cmake.\n",
1676*77c1e3ccSAndroid Build Coastguard Worker                         stream->index);
1677*77c1e3ccSAndroid Build Coastguard Worker #endif
1678*77c1e3ccSAndroid Build Coastguard Worker     }
1679*77c1e3ccSAndroid Build Coastguard Worker   }
1680*77c1e3ccSAndroid Build Coastguard Worker   if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1681*77c1e3ccSAndroid Build Coastguard Worker     if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1682*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1683*77c1e3ccSAndroid Build Coastguard Worker       exit(EXIT_FAILURE);
1684*77c1e3ccSAndroid Build Coastguard Worker     }
1685*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_LIBYUV
1686*77c1e3ccSAndroid Build Coastguard Worker     if (!stream->img)
1687*77c1e3ccSAndroid Build Coastguard Worker       stream->img =
1688*77c1e3ccSAndroid Build Coastguard Worker           aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1689*77c1e3ccSAndroid Build Coastguard Worker     I420Scale(
1690*77c1e3ccSAndroid Build Coastguard Worker         img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1691*77c1e3ccSAndroid Build Coastguard Worker         img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1692*77c1e3ccSAndroid Build Coastguard Worker         img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1693*77c1e3ccSAndroid Build Coastguard Worker         stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1694*77c1e3ccSAndroid Build Coastguard Worker         stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1695*77c1e3ccSAndroid Build Coastguard Worker         stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1696*77c1e3ccSAndroid Build Coastguard Worker         stream->img->d_w, stream->img->d_h, kFilterBox);
1697*77c1e3ccSAndroid Build Coastguard Worker     img = stream->img;
1698*77c1e3ccSAndroid Build Coastguard Worker #else
1699*77c1e3ccSAndroid Build Coastguard Worker     stream->encoder.err = 1;
1700*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder,
1701*77c1e3ccSAndroid Build Coastguard Worker                       "Stream %d: Failed to encode frame.\n"
1702*77c1e3ccSAndroid Build Coastguard Worker                       "Scaling disabled in this configuration. \n"
1703*77c1e3ccSAndroid Build Coastguard Worker                       "To enable, configure with --enable-libyuv\n",
1704*77c1e3ccSAndroid Build Coastguard Worker                       stream->index);
1705*77c1e3ccSAndroid Build Coastguard Worker #endif
1706*77c1e3ccSAndroid Build Coastguard Worker   }
1707*77c1e3ccSAndroid Build Coastguard Worker 
1708*77c1e3ccSAndroid Build Coastguard Worker   struct aom_image monochrome_img;
1709*77c1e3ccSAndroid Build Coastguard Worker   if (img && cfg->monochrome) {
1710*77c1e3ccSAndroid Build Coastguard Worker     convert_image_to_monochrome(img, &monochrome_img);
1711*77c1e3ccSAndroid Build Coastguard Worker     img = &monochrome_img;
1712*77c1e3ccSAndroid Build Coastguard Worker   }
1713*77c1e3ccSAndroid Build Coastguard Worker 
1714*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_start(&timer);
1715*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_encode(&stream->encoder, img, frame_start,
1716*77c1e3ccSAndroid Build Coastguard Worker                    (uint32_t)(next_frame_start - frame_start), 0);
1717*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_mark(&timer);
1718*77c1e3ccSAndroid Build Coastguard Worker   stream->cx_time += aom_usec_timer_elapsed(&timer);
1719*77c1e3ccSAndroid Build Coastguard Worker   ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1720*77c1e3ccSAndroid Build Coastguard Worker                     stream->index);
1721*77c1e3ccSAndroid Build Coastguard Worker }
1722*77c1e3ccSAndroid Build Coastguard Worker 
update_quantizer_histogram(struct stream_state * stream)1723*77c1e3ccSAndroid Build Coastguard Worker static void update_quantizer_histogram(struct stream_state *stream) {
1724*77c1e3ccSAndroid Build Coastguard Worker   if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1725*77c1e3ccSAndroid Build Coastguard Worker     int q;
1726*77c1e3ccSAndroid Build Coastguard Worker 
1727*77c1e3ccSAndroid Build Coastguard Worker     AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AOME_GET_LAST_QUANTIZER_64,
1728*77c1e3ccSAndroid Build Coastguard Worker                                   &q);
1729*77c1e3ccSAndroid Build Coastguard Worker     ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1730*77c1e3ccSAndroid Build Coastguard Worker     stream->counts[q]++;
1731*77c1e3ccSAndroid Build Coastguard Worker   }
1732*77c1e3ccSAndroid Build Coastguard Worker }
1733*77c1e3ccSAndroid Build Coastguard Worker 
get_cx_data(struct stream_state * stream,struct AvxEncoderConfig * global,int * got_data)1734*77c1e3ccSAndroid Build Coastguard Worker static void get_cx_data(struct stream_state *stream,
1735*77c1e3ccSAndroid Build Coastguard Worker                         struct AvxEncoderConfig *global, int *got_data) {
1736*77c1e3ccSAndroid Build Coastguard Worker   const aom_codec_cx_pkt_t *pkt;
1737*77c1e3ccSAndroid Build Coastguard Worker   const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1738*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iter_t iter = NULL;
1739*77c1e3ccSAndroid Build Coastguard Worker 
1740*77c1e3ccSAndroid Build Coastguard Worker   *got_data = 0;
1741*77c1e3ccSAndroid Build Coastguard Worker   while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1742*77c1e3ccSAndroid Build Coastguard Worker     static size_t fsize = 0;
1743*77c1e3ccSAndroid Build Coastguard Worker     static FileOffset ivf_header_pos = 0;
1744*77c1e3ccSAndroid Build Coastguard Worker 
1745*77c1e3ccSAndroid Build Coastguard Worker     switch (pkt->kind) {
1746*77c1e3ccSAndroid Build Coastguard Worker       case AOM_CODEC_CX_FRAME_PKT:
1747*77c1e3ccSAndroid Build Coastguard Worker         ++stream->frames_out;
1748*77c1e3ccSAndroid Build Coastguard Worker         if (!global->quiet)
1749*77c1e3ccSAndroid Build Coastguard Worker           fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1750*77c1e3ccSAndroid Build Coastguard Worker 
1751*77c1e3ccSAndroid Build Coastguard Worker         update_rate_histogram(stream->rate_hist, cfg, pkt);
1752*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
1753*77c1e3ccSAndroid Build Coastguard Worker         if (stream->config.write_webm) {
1754*77c1e3ccSAndroid Build Coastguard Worker           if (write_webm_block(&stream->webm_ctx, cfg, pkt) != 0) {
1755*77c1e3ccSAndroid Build Coastguard Worker             fatal("WebM writer failed.");
1756*77c1e3ccSAndroid Build Coastguard Worker           }
1757*77c1e3ccSAndroid Build Coastguard Worker         }
1758*77c1e3ccSAndroid Build Coastguard Worker #endif
1759*77c1e3ccSAndroid Build Coastguard Worker         if (!stream->config.write_webm) {
1760*77c1e3ccSAndroid Build Coastguard Worker           if (stream->config.write_ivf) {
1761*77c1e3ccSAndroid Build Coastguard Worker             if (pkt->data.frame.partition_id <= 0) {
1762*77c1e3ccSAndroid Build Coastguard Worker               ivf_header_pos = ftello(stream->file);
1763*77c1e3ccSAndroid Build Coastguard Worker               fsize = pkt->data.frame.sz;
1764*77c1e3ccSAndroid Build Coastguard Worker 
1765*77c1e3ccSAndroid Build Coastguard Worker               ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1766*77c1e3ccSAndroid Build Coastguard Worker             } else {
1767*77c1e3ccSAndroid Build Coastguard Worker               fsize += pkt->data.frame.sz;
1768*77c1e3ccSAndroid Build Coastguard Worker 
1769*77c1e3ccSAndroid Build Coastguard Worker               const FileOffset currpos = ftello(stream->file);
1770*77c1e3ccSAndroid Build Coastguard Worker               fseeko(stream->file, ivf_header_pos, SEEK_SET);
1771*77c1e3ccSAndroid Build Coastguard Worker               ivf_write_frame_size(stream->file, fsize);
1772*77c1e3ccSAndroid Build Coastguard Worker               fseeko(stream->file, currpos, SEEK_SET);
1773*77c1e3ccSAndroid Build Coastguard Worker             }
1774*77c1e3ccSAndroid Build Coastguard Worker           }
1775*77c1e3ccSAndroid Build Coastguard Worker 
1776*77c1e3ccSAndroid Build Coastguard Worker           (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1777*77c1e3ccSAndroid Build Coastguard Worker                        stream->file);
1778*77c1e3ccSAndroid Build Coastguard Worker         }
1779*77c1e3ccSAndroid Build Coastguard Worker         stream->nbytes += pkt->data.raw.sz;
1780*77c1e3ccSAndroid Build Coastguard Worker 
1781*77c1e3ccSAndroid Build Coastguard Worker         *got_data = 1;
1782*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
1783*77c1e3ccSAndroid Build Coastguard Worker         if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1784*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1785*77c1e3ccSAndroid Build Coastguard Worker                            pkt->data.frame.sz, NULL);
1786*77c1e3ccSAndroid Build Coastguard Worker           if (stream->decoder.err) {
1787*77c1e3ccSAndroid Build Coastguard Worker             warn_or_exit_on_error(&stream->decoder,
1788*77c1e3ccSAndroid Build Coastguard Worker                                   global->test_decode == TEST_DECODE_FATAL,
1789*77c1e3ccSAndroid Build Coastguard Worker                                   "Failed to decode frame %d in stream %d",
1790*77c1e3ccSAndroid Build Coastguard Worker                                   stream->frames_out + 1, stream->index);
1791*77c1e3ccSAndroid Build Coastguard Worker             stream->mismatch_seen = stream->frames_out + 1;
1792*77c1e3ccSAndroid Build Coastguard Worker           }
1793*77c1e3ccSAndroid Build Coastguard Worker         }
1794*77c1e3ccSAndroid Build Coastguard Worker #endif
1795*77c1e3ccSAndroid Build Coastguard Worker         break;
1796*77c1e3ccSAndroid Build Coastguard Worker       case AOM_CODEC_STATS_PKT:
1797*77c1e3ccSAndroid Build Coastguard Worker         stream->frames_out++;
1798*77c1e3ccSAndroid Build Coastguard Worker         stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1799*77c1e3ccSAndroid Build Coastguard Worker                     pkt->data.twopass_stats.sz);
1800*77c1e3ccSAndroid Build Coastguard Worker         stream->nbytes += pkt->data.raw.sz;
1801*77c1e3ccSAndroid Build Coastguard Worker         break;
1802*77c1e3ccSAndroid Build Coastguard Worker       case AOM_CODEC_PSNR_PKT:
1803*77c1e3ccSAndroid Build Coastguard Worker 
1804*77c1e3ccSAndroid Build Coastguard Worker         if (global->show_psnr >= 1) {
1805*77c1e3ccSAndroid Build Coastguard Worker           int i;
1806*77c1e3ccSAndroid Build Coastguard Worker 
1807*77c1e3ccSAndroid Build Coastguard Worker           stream->psnr_sse_total[0] += pkt->data.psnr.sse[0];
1808*77c1e3ccSAndroid Build Coastguard Worker           stream->psnr_samples_total[0] += pkt->data.psnr.samples[0];
1809*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < 4; i++) {
1810*77c1e3ccSAndroid Build Coastguard Worker             if (!global->quiet)
1811*77c1e3ccSAndroid Build Coastguard Worker               fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1812*77c1e3ccSAndroid Build Coastguard Worker             stream->psnr_totals[0][i] += pkt->data.psnr.psnr[i];
1813*77c1e3ccSAndroid Build Coastguard Worker           }
1814*77c1e3ccSAndroid Build Coastguard Worker           stream->psnr_count[0]++;
1815*77c1e3ccSAndroid Build Coastguard Worker 
1816*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1817*77c1e3ccSAndroid Build Coastguard Worker           if (stream->config.cfg.g_input_bit_depth <
1818*77c1e3ccSAndroid Build Coastguard Worker               (unsigned int)stream->config.cfg.g_bit_depth) {
1819*77c1e3ccSAndroid Build Coastguard Worker             stream->psnr_sse_total[1] += pkt->data.psnr.sse_hbd[0];
1820*77c1e3ccSAndroid Build Coastguard Worker             stream->psnr_samples_total[1] += pkt->data.psnr.samples_hbd[0];
1821*77c1e3ccSAndroid Build Coastguard Worker             for (i = 0; i < 4; i++) {
1822*77c1e3ccSAndroid Build Coastguard Worker               if (!global->quiet)
1823*77c1e3ccSAndroid Build Coastguard Worker                 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr_hbd[i]);
1824*77c1e3ccSAndroid Build Coastguard Worker               stream->psnr_totals[1][i] += pkt->data.psnr.psnr_hbd[i];
1825*77c1e3ccSAndroid Build Coastguard Worker             }
1826*77c1e3ccSAndroid Build Coastguard Worker             stream->psnr_count[1]++;
1827*77c1e3ccSAndroid Build Coastguard Worker           }
1828*77c1e3ccSAndroid Build Coastguard Worker #endif
1829*77c1e3ccSAndroid Build Coastguard Worker         }
1830*77c1e3ccSAndroid Build Coastguard Worker 
1831*77c1e3ccSAndroid Build Coastguard Worker         break;
1832*77c1e3ccSAndroid Build Coastguard Worker       default: break;
1833*77c1e3ccSAndroid Build Coastguard Worker     }
1834*77c1e3ccSAndroid Build Coastguard Worker   }
1835*77c1e3ccSAndroid Build Coastguard Worker }
1836*77c1e3ccSAndroid Build Coastguard Worker 
show_psnr(struct stream_state * stream,double peak,int64_t bps)1837*77c1e3ccSAndroid Build Coastguard Worker static void show_psnr(struct stream_state *stream, double peak, int64_t bps) {
1838*77c1e3ccSAndroid Build Coastguard Worker   int i;
1839*77c1e3ccSAndroid Build Coastguard Worker   double ovpsnr;
1840*77c1e3ccSAndroid Build Coastguard Worker 
1841*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->psnr_count[0]) return;
1842*77c1e3ccSAndroid Build Coastguard Worker 
1843*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1844*77c1e3ccSAndroid Build Coastguard Worker   ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[0], peak,
1845*77c1e3ccSAndroid Build Coastguard Worker                        (double)stream->psnr_sse_total[0]);
1846*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, " %.3f", ovpsnr);
1847*77c1e3ccSAndroid Build Coastguard Worker 
1848*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 4; i++) {
1849*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, " %.3f", stream->psnr_totals[0][i] / stream->psnr_count[0]);
1850*77c1e3ccSAndroid Build Coastguard Worker   }
1851*77c1e3ccSAndroid Build Coastguard Worker   if (bps > 0) {
1852*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, " %7" PRId64 " bps", bps);
1853*77c1e3ccSAndroid Build Coastguard Worker   }
1854*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1855*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\n");
1856*77c1e3ccSAndroid Build Coastguard Worker }
1857*77c1e3ccSAndroid Build Coastguard Worker 
1858*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
show_psnr_hbd(struct stream_state * stream,double peak,int64_t bps)1859*77c1e3ccSAndroid Build Coastguard Worker static void show_psnr_hbd(struct stream_state *stream, double peak,
1860*77c1e3ccSAndroid Build Coastguard Worker                           int64_t bps) {
1861*77c1e3ccSAndroid Build Coastguard Worker   int i;
1862*77c1e3ccSAndroid Build Coastguard Worker   double ovpsnr;
1863*77c1e3ccSAndroid Build Coastguard Worker   // Compute PSNR based on stream bit depth
1864*77c1e3ccSAndroid Build Coastguard Worker   if (!stream->psnr_count[1]) return;
1865*77c1e3ccSAndroid Build Coastguard Worker 
1866*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1867*77c1e3ccSAndroid Build Coastguard Worker   ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[1], peak,
1868*77c1e3ccSAndroid Build Coastguard Worker                        (double)stream->psnr_sse_total[1]);
1869*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, " %.3f", ovpsnr);
1870*77c1e3ccSAndroid Build Coastguard Worker 
1871*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 4; i++) {
1872*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, " %.3f", stream->psnr_totals[1][i] / stream->psnr_count[1]);
1873*77c1e3ccSAndroid Build Coastguard Worker   }
1874*77c1e3ccSAndroid Build Coastguard Worker   if (bps > 0) {
1875*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, " %7" PRId64 " bps", bps);
1876*77c1e3ccSAndroid Build Coastguard Worker   }
1877*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1878*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\n");
1879*77c1e3ccSAndroid Build Coastguard Worker }
1880*77c1e3ccSAndroid Build Coastguard Worker #endif
1881*77c1e3ccSAndroid Build Coastguard Worker 
usec_to_fps(uint64_t usec,unsigned int frames)1882*77c1e3ccSAndroid Build Coastguard Worker static float usec_to_fps(uint64_t usec, unsigned int frames) {
1883*77c1e3ccSAndroid Build Coastguard Worker   return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1884*77c1e3ccSAndroid Build Coastguard Worker }
1885*77c1e3ccSAndroid Build Coastguard Worker 
test_decode(struct stream_state * stream,enum TestDecodeFatality fatal)1886*77c1e3ccSAndroid Build Coastguard Worker static void test_decode(struct stream_state *stream,
1887*77c1e3ccSAndroid Build Coastguard Worker                         enum TestDecodeFatality fatal) {
1888*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t enc_img, dec_img;
1889*77c1e3ccSAndroid Build Coastguard Worker 
1890*77c1e3ccSAndroid Build Coastguard Worker   if (stream->mismatch_seen) return;
1891*77c1e3ccSAndroid Build Coastguard Worker 
1892*77c1e3ccSAndroid Build Coastguard Worker   /* Get the internal reference frame */
1893*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE,
1894*77c1e3ccSAndroid Build Coastguard Worker                                 &enc_img);
1895*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE,
1896*77c1e3ccSAndroid Build Coastguard Worker                                 &dec_img);
1897*77c1e3ccSAndroid Build Coastguard Worker 
1898*77c1e3ccSAndroid Build Coastguard Worker   if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1899*77c1e3ccSAndroid Build Coastguard Worker       (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1900*77c1e3ccSAndroid Build Coastguard Worker     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1901*77c1e3ccSAndroid Build Coastguard Worker       aom_image_t enc_hbd_img;
1902*77c1e3ccSAndroid Build Coastguard Worker       aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1903*77c1e3ccSAndroid Build Coastguard Worker                     enc_img.d_w, enc_img.d_h, 16);
1904*77c1e3ccSAndroid Build Coastguard Worker       aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1905*77c1e3ccSAndroid Build Coastguard Worker       enc_img = enc_hbd_img;
1906*77c1e3ccSAndroid Build Coastguard Worker     }
1907*77c1e3ccSAndroid Build Coastguard Worker     if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1908*77c1e3ccSAndroid Build Coastguard Worker       aom_image_t dec_hbd_img;
1909*77c1e3ccSAndroid Build Coastguard Worker       aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1910*77c1e3ccSAndroid Build Coastguard Worker                     dec_img.d_w, dec_img.d_h, 16);
1911*77c1e3ccSAndroid Build Coastguard Worker       aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1912*77c1e3ccSAndroid Build Coastguard Worker       dec_img = dec_hbd_img;
1913*77c1e3ccSAndroid Build Coastguard Worker     }
1914*77c1e3ccSAndroid Build Coastguard Worker   }
1915*77c1e3ccSAndroid Build Coastguard Worker 
1916*77c1e3ccSAndroid Build Coastguard Worker   ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1917*77c1e3ccSAndroid Build Coastguard Worker   ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1918*77c1e3ccSAndroid Build Coastguard Worker 
1919*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_compare_img(&enc_img, &dec_img)) {
1920*77c1e3ccSAndroid Build Coastguard Worker     int y[4], u[4], v[4];
1921*77c1e3ccSAndroid Build Coastguard Worker     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1922*77c1e3ccSAndroid Build Coastguard Worker       aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1923*77c1e3ccSAndroid Build Coastguard Worker     } else {
1924*77c1e3ccSAndroid Build Coastguard Worker       aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1925*77c1e3ccSAndroid Build Coastguard Worker     }
1926*77c1e3ccSAndroid Build Coastguard Worker     stream->decoder.err = 1;
1927*77c1e3ccSAndroid Build Coastguard Worker     warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1928*77c1e3ccSAndroid Build Coastguard Worker                           "Stream %d: Encode/decode mismatch on frame %d at"
1929*77c1e3ccSAndroid Build Coastguard Worker                           " Y[%d, %d] {%d/%d},"
1930*77c1e3ccSAndroid Build Coastguard Worker                           " U[%d, %d] {%d/%d},"
1931*77c1e3ccSAndroid Build Coastguard Worker                           " V[%d, %d] {%d/%d}",
1932*77c1e3ccSAndroid Build Coastguard Worker                           stream->index, stream->frames_out, y[0], y[1], y[2],
1933*77c1e3ccSAndroid Build Coastguard Worker                           y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1934*77c1e3ccSAndroid Build Coastguard Worker     stream->mismatch_seen = stream->frames_out;
1935*77c1e3ccSAndroid Build Coastguard Worker   }
1936*77c1e3ccSAndroid Build Coastguard Worker 
1937*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&enc_img);
1938*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&dec_img);
1939*77c1e3ccSAndroid Build Coastguard Worker }
1940*77c1e3ccSAndroid Build Coastguard Worker 
print_time(const char * label,int64_t etl)1941*77c1e3ccSAndroid Build Coastguard Worker static void print_time(const char *label, int64_t etl) {
1942*77c1e3ccSAndroid Build Coastguard Worker   int64_t hours;
1943*77c1e3ccSAndroid Build Coastguard Worker   int64_t mins;
1944*77c1e3ccSAndroid Build Coastguard Worker   int64_t secs;
1945*77c1e3ccSAndroid Build Coastguard Worker 
1946*77c1e3ccSAndroid Build Coastguard Worker   if (etl >= 0) {
1947*77c1e3ccSAndroid Build Coastguard Worker     hours = etl / 3600;
1948*77c1e3ccSAndroid Build Coastguard Worker     etl -= hours * 3600;
1949*77c1e3ccSAndroid Build Coastguard Worker     mins = etl / 60;
1950*77c1e3ccSAndroid Build Coastguard Worker     etl -= mins * 60;
1951*77c1e3ccSAndroid Build Coastguard Worker     secs = etl;
1952*77c1e3ccSAndroid Build Coastguard Worker 
1953*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1954*77c1e3ccSAndroid Build Coastguard Worker             hours, mins, secs);
1955*77c1e3ccSAndroid Build Coastguard Worker   } else {
1956*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "[%3s  unknown] ", label);
1957*77c1e3ccSAndroid Build Coastguard Worker   }
1958*77c1e3ccSAndroid Build Coastguard Worker }
1959*77c1e3ccSAndroid Build Coastguard Worker 
clear_stream_count_state(struct stream_state * stream)1960*77c1e3ccSAndroid Build Coastguard Worker static void clear_stream_count_state(struct stream_state *stream) {
1961*77c1e3ccSAndroid Build Coastguard Worker   // PSNR counters
1962*77c1e3ccSAndroid Build Coastguard Worker   for (int k = 0; k < 2; k++) {
1963*77c1e3ccSAndroid Build Coastguard Worker     stream->psnr_sse_total[k] = 0;
1964*77c1e3ccSAndroid Build Coastguard Worker     stream->psnr_samples_total[k] = 0;
1965*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 4; i++) {
1966*77c1e3ccSAndroid Build Coastguard Worker       stream->psnr_totals[k][i] = 0;
1967*77c1e3ccSAndroid Build Coastguard Worker     }
1968*77c1e3ccSAndroid Build Coastguard Worker     stream->psnr_count[k] = 0;
1969*77c1e3ccSAndroid Build Coastguard Worker   }
1970*77c1e3ccSAndroid Build Coastguard Worker   // q hist
1971*77c1e3ccSAndroid Build Coastguard Worker   memset(stream->counts, 0, sizeof(stream->counts));
1972*77c1e3ccSAndroid Build Coastguard Worker }
1973*77c1e3ccSAndroid Build Coastguard Worker 
1974*77c1e3ccSAndroid Build Coastguard Worker // aomenc will downscale the second pass if:
1975*77c1e3ccSAndroid Build Coastguard Worker // 1. the specific pass is not given by commandline (aomenc will perform all
1976*77c1e3ccSAndroid Build Coastguard Worker //    passes)
1977*77c1e3ccSAndroid Build Coastguard Worker // 2. there are more than 2 passes in total
1978*77c1e3ccSAndroid Build Coastguard Worker // 3. current pass is the second pass (the parameter pass starts with 0 so
1979*77c1e3ccSAndroid Build Coastguard Worker //    pass == 1)
pass_need_downscale(int global_pass,int global_passes,int pass)1980*77c1e3ccSAndroid Build Coastguard Worker static int pass_need_downscale(int global_pass, int global_passes, int pass) {
1981*77c1e3ccSAndroid Build Coastguard Worker   return !global_pass && global_passes > 2 && pass == 1;
1982*77c1e3ccSAndroid Build Coastguard Worker }
1983*77c1e3ccSAndroid Build Coastguard Worker 
main(int argc,const char ** argv_)1984*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, const char **argv_) {
1985*77c1e3ccSAndroid Build Coastguard Worker   int pass;
1986*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t raw;
1987*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t raw_shift;
1988*77c1e3ccSAndroid Build Coastguard Worker   int allocated_raw_shift = 0;
1989*77c1e3ccSAndroid Build Coastguard Worker   int do_16bit_internal = 0;
1990*77c1e3ccSAndroid Build Coastguard Worker   int input_shift = 0;
1991*77c1e3ccSAndroid Build Coastguard Worker   int frame_avail, got_data;
1992*77c1e3ccSAndroid Build Coastguard Worker 
1993*77c1e3ccSAndroid Build Coastguard Worker   struct AvxInputContext input;
1994*77c1e3ccSAndroid Build Coastguard Worker   struct AvxEncoderConfig global;
1995*77c1e3ccSAndroid Build Coastguard Worker   struct stream_state *streams = NULL;
1996*77c1e3ccSAndroid Build Coastguard Worker   char **argv, **argi;
1997*77c1e3ccSAndroid Build Coastguard Worker   uint64_t cx_time = 0;
1998*77c1e3ccSAndroid Build Coastguard Worker   int stream_cnt = 0;
1999*77c1e3ccSAndroid Build Coastguard Worker   int res = 0;
2000*77c1e3ccSAndroid Build Coastguard Worker   int profile_updated = 0;
2001*77c1e3ccSAndroid Build Coastguard Worker 
2002*77c1e3ccSAndroid Build Coastguard Worker   memset(&input, 0, sizeof(input));
2003*77c1e3ccSAndroid Build Coastguard Worker   memset(&raw, 0, sizeof(raw));
2004*77c1e3ccSAndroid Build Coastguard Worker   exec_name = argv_[0];
2005*77c1e3ccSAndroid Build Coastguard Worker 
2006*77c1e3ccSAndroid Build Coastguard Worker   /* Setup default input stream settings */
2007*77c1e3ccSAndroid Build Coastguard Worker   input.framerate.numerator = 30;
2008*77c1e3ccSAndroid Build Coastguard Worker   input.framerate.denominator = 1;
2009*77c1e3ccSAndroid Build Coastguard Worker   input.only_i420 = 1;
2010*77c1e3ccSAndroid Build Coastguard Worker   input.bit_depth = 0;
2011*77c1e3ccSAndroid Build Coastguard Worker 
2012*77c1e3ccSAndroid Build Coastguard Worker   /* First parse the global configuration values, because we want to apply
2013*77c1e3ccSAndroid Build Coastguard Worker    * other parameters on top of the default configuration provided by the
2014*77c1e3ccSAndroid Build Coastguard Worker    * codec.
2015*77c1e3ccSAndroid Build Coastguard Worker    */
2016*77c1e3ccSAndroid Build Coastguard Worker   argv = argv_dup(argc - 1, argv_ + 1);
2017*77c1e3ccSAndroid Build Coastguard Worker   if (!argv) {
2018*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "Error allocating argument list\n");
2019*77c1e3ccSAndroid Build Coastguard Worker     return EXIT_FAILURE;
2020*77c1e3ccSAndroid Build Coastguard Worker   }
2021*77c1e3ccSAndroid Build Coastguard Worker   parse_global_config(&global, &argv);
2022*77c1e3ccSAndroid Build Coastguard Worker 
2023*77c1e3ccSAndroid Build Coastguard Worker   if (argc < 2) usage_exit();
2024*77c1e3ccSAndroid Build Coastguard Worker 
2025*77c1e3ccSAndroid Build Coastguard Worker   switch (global.color_type) {
2026*77c1e3ccSAndroid Build Coastguard Worker     case I420: input.fmt = AOM_IMG_FMT_I420; break;
2027*77c1e3ccSAndroid Build Coastguard Worker     case I422: input.fmt = AOM_IMG_FMT_I422; break;
2028*77c1e3ccSAndroid Build Coastguard Worker     case I444: input.fmt = AOM_IMG_FMT_I444; break;
2029*77c1e3ccSAndroid Build Coastguard Worker     case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
2030*77c1e3ccSAndroid Build Coastguard Worker     case NV12: input.fmt = AOM_IMG_FMT_NV12; break;
2031*77c1e3ccSAndroid Build Coastguard Worker   }
2032*77c1e3ccSAndroid Build Coastguard Worker 
2033*77c1e3ccSAndroid Build Coastguard Worker   {
2034*77c1e3ccSAndroid Build Coastguard Worker     /* Now parse each stream's parameters. Using a local scope here
2035*77c1e3ccSAndroid Build Coastguard Worker      * due to the use of 'stream' as loop variable in FOREACH_STREAM
2036*77c1e3ccSAndroid Build Coastguard Worker      * loops
2037*77c1e3ccSAndroid Build Coastguard Worker      */
2038*77c1e3ccSAndroid Build Coastguard Worker     struct stream_state *stream = NULL;
2039*77c1e3ccSAndroid Build Coastguard Worker 
2040*77c1e3ccSAndroid Build Coastguard Worker     do {
2041*77c1e3ccSAndroid Build Coastguard Worker       stream = new_stream(&global, stream);
2042*77c1e3ccSAndroid Build Coastguard Worker       stream_cnt++;
2043*77c1e3ccSAndroid Build Coastguard Worker       if (!streams) streams = stream;
2044*77c1e3ccSAndroid Build Coastguard Worker     } while (parse_stream_params(&global, stream, argv));
2045*77c1e3ccSAndroid Build Coastguard Worker   }
2046*77c1e3ccSAndroid Build Coastguard Worker 
2047*77c1e3ccSAndroid Build Coastguard Worker   /* Check for unrecognized options */
2048*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argv; *argi; argi++)
2049*77c1e3ccSAndroid Build Coastguard Worker     if (argi[0][0] == '-' && argi[0][1])
2050*77c1e3ccSAndroid Build Coastguard Worker       die("Error: Unrecognized option %s\n", *argi);
2051*77c1e3ccSAndroid Build Coastguard Worker 
2052*77c1e3ccSAndroid Build Coastguard Worker   FOREACH_STREAM(stream, streams) {
2053*77c1e3ccSAndroid Build Coastguard Worker     check_encoder_config(global.disable_warning_prompt, &global,
2054*77c1e3ccSAndroid Build Coastguard Worker                          &stream->config.cfg);
2055*77c1e3ccSAndroid Build Coastguard Worker 
2056*77c1e3ccSAndroid Build Coastguard Worker     // If large_scale_tile = 1, only support to output to ivf format.
2057*77c1e3ccSAndroid Build Coastguard Worker     if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
2058*77c1e3ccSAndroid Build Coastguard Worker       die("only support ivf output format while large-scale-tile=1\n");
2059*77c1e3ccSAndroid Build Coastguard Worker   }
2060*77c1e3ccSAndroid Build Coastguard Worker 
2061*77c1e3ccSAndroid Build Coastguard Worker   /* Handle non-option arguments */
2062*77c1e3ccSAndroid Build Coastguard Worker   input.filename = argv[0];
2063*77c1e3ccSAndroid Build Coastguard Worker   const char *orig_input_filename = input.filename;
2064*77c1e3ccSAndroid Build Coastguard Worker   FOREACH_STREAM(stream, streams) {
2065*77c1e3ccSAndroid Build Coastguard Worker     stream->orig_out_fn = stream->config.out_fn;
2066*77c1e3ccSAndroid Build Coastguard Worker     stream->orig_width = stream->config.cfg.g_w;
2067*77c1e3ccSAndroid Build Coastguard Worker     stream->orig_height = stream->config.cfg.g_h;
2068*77c1e3ccSAndroid Build Coastguard Worker     stream->orig_write_ivf = stream->config.write_ivf;
2069*77c1e3ccSAndroid Build Coastguard Worker     stream->orig_write_webm = stream->config.write_webm;
2070*77c1e3ccSAndroid Build Coastguard Worker   }
2071*77c1e3ccSAndroid Build Coastguard Worker 
2072*77c1e3ccSAndroid Build Coastguard Worker   if (!input.filename) {
2073*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "No input file specified!\n");
2074*77c1e3ccSAndroid Build Coastguard Worker     usage_exit();
2075*77c1e3ccSAndroid Build Coastguard Worker   }
2076*77c1e3ccSAndroid Build Coastguard Worker 
2077*77c1e3ccSAndroid Build Coastguard Worker   /* Decide if other chroma subsamplings than 4:2:0 are supported */
2078*77c1e3ccSAndroid Build Coastguard Worker   if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC)
2079*77c1e3ccSAndroid Build Coastguard Worker     input.only_i420 = 0;
2080*77c1e3ccSAndroid Build Coastguard Worker 
2081*77c1e3ccSAndroid Build Coastguard Worker   for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
2082*77c1e3ccSAndroid Build Coastguard Worker     if (pass > 1) {
2083*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) { clear_stream_count_state(stream); }
2084*77c1e3ccSAndroid Build Coastguard Worker     }
2085*77c1e3ccSAndroid Build Coastguard Worker 
2086*77c1e3ccSAndroid Build Coastguard Worker     int frames_in = 0, seen_frames = 0;
2087*77c1e3ccSAndroid Build Coastguard Worker     int64_t estimated_time_left = -1;
2088*77c1e3ccSAndroid Build Coastguard Worker     int64_t average_rate = -1;
2089*77c1e3ccSAndroid Build Coastguard Worker     int64_t lagged_count = 0;
2090*77c1e3ccSAndroid Build Coastguard Worker     const int need_downscale =
2091*77c1e3ccSAndroid Build Coastguard Worker         pass_need_downscale(global.pass, global.passes, pass);
2092*77c1e3ccSAndroid Build Coastguard Worker 
2093*77c1e3ccSAndroid Build Coastguard Worker     // Set the output to the specified two-pass output file, and
2094*77c1e3ccSAndroid Build Coastguard Worker     // restore the width and height to the original values.
2095*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2096*77c1e3ccSAndroid Build Coastguard Worker       if (need_downscale) {
2097*77c1e3ccSAndroid Build Coastguard Worker         stream->config.out_fn = stream->config.two_pass_output;
2098*77c1e3ccSAndroid Build Coastguard Worker         // Libaom currently only supports the ivf format for the third pass.
2099*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_ivf = 1;
2100*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_webm = 0;
2101*77c1e3ccSAndroid Build Coastguard Worker       } else {
2102*77c1e3ccSAndroid Build Coastguard Worker         stream->config.out_fn = stream->orig_out_fn;
2103*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_ivf = stream->orig_write_ivf;
2104*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_webm = stream->orig_write_webm;
2105*77c1e3ccSAndroid Build Coastguard Worker       }
2106*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_w = stream->orig_width;
2107*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_h = stream->orig_height;
2108*77c1e3ccSAndroid Build Coastguard Worker     }
2109*77c1e3ccSAndroid Build Coastguard Worker 
2110*77c1e3ccSAndroid Build Coastguard Worker     // For second pass in three-pass encoding, set the input to
2111*77c1e3ccSAndroid Build Coastguard Worker     // the given two-pass-input file if available. If the scaled input is not
2112*77c1e3ccSAndroid Build Coastguard Worker     // given, we will attempt to re-scale the original input.
2113*77c1e3ccSAndroid Build Coastguard Worker     input.filename = orig_input_filename;
2114*77c1e3ccSAndroid Build Coastguard Worker     const char *two_pass_input = NULL;
2115*77c1e3ccSAndroid Build Coastguard Worker     if (need_downscale) {
2116*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2117*77c1e3ccSAndroid Build Coastguard Worker         if (stream->config.two_pass_input) {
2118*77c1e3ccSAndroid Build Coastguard Worker           two_pass_input = stream->config.two_pass_input;
2119*77c1e3ccSAndroid Build Coastguard Worker           input.filename = two_pass_input;
2120*77c1e3ccSAndroid Build Coastguard Worker           break;
2121*77c1e3ccSAndroid Build Coastguard Worker         }
2122*77c1e3ccSAndroid Build Coastguard Worker       }
2123*77c1e3ccSAndroid Build Coastguard Worker     }
2124*77c1e3ccSAndroid Build Coastguard Worker 
2125*77c1e3ccSAndroid Build Coastguard Worker     open_input_file(&input, global.csp);
2126*77c1e3ccSAndroid Build Coastguard Worker 
2127*77c1e3ccSAndroid Build Coastguard Worker     /* If the input file doesn't specify its w/h (raw files), try to get
2128*77c1e3ccSAndroid Build Coastguard Worker      * the data from the first stream's configuration.
2129*77c1e3ccSAndroid Build Coastguard Worker      */
2130*77c1e3ccSAndroid Build Coastguard Worker     if (!input.width || !input.height) {
2131*77c1e3ccSAndroid Build Coastguard Worker       if (two_pass_input) {
2132*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) {
2133*77c1e3ccSAndroid Build Coastguard Worker           if (stream->config.two_pass_width && stream->config.two_pass_height) {
2134*77c1e3ccSAndroid Build Coastguard Worker             input.width = stream->config.two_pass_width;
2135*77c1e3ccSAndroid Build Coastguard Worker             input.height = stream->config.two_pass_height;
2136*77c1e3ccSAndroid Build Coastguard Worker             break;
2137*77c1e3ccSAndroid Build Coastguard Worker           }
2138*77c1e3ccSAndroid Build Coastguard Worker         }
2139*77c1e3ccSAndroid Build Coastguard Worker       } else {
2140*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) {
2141*77c1e3ccSAndroid Build Coastguard Worker           if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2142*77c1e3ccSAndroid Build Coastguard Worker             input.width = stream->config.cfg.g_w;
2143*77c1e3ccSAndroid Build Coastguard Worker             input.height = stream->config.cfg.g_h;
2144*77c1e3ccSAndroid Build Coastguard Worker             break;
2145*77c1e3ccSAndroid Build Coastguard Worker           }
2146*77c1e3ccSAndroid Build Coastguard Worker         }
2147*77c1e3ccSAndroid Build Coastguard Worker       }
2148*77c1e3ccSAndroid Build Coastguard Worker     }
2149*77c1e3ccSAndroid Build Coastguard Worker 
2150*77c1e3ccSAndroid Build Coastguard Worker     /* Update stream configurations from the input file's parameters */
2151*77c1e3ccSAndroid Build Coastguard Worker     if (!input.width || !input.height) {
2152*77c1e3ccSAndroid Build Coastguard Worker       if (two_pass_input) {
2153*77c1e3ccSAndroid Build Coastguard Worker         fatal(
2154*77c1e3ccSAndroid Build Coastguard Worker             "Specify downscaled stream dimensions with --two-pass-width "
2155*77c1e3ccSAndroid Build Coastguard Worker             " and --two-pass-height");
2156*77c1e3ccSAndroid Build Coastguard Worker       } else {
2157*77c1e3ccSAndroid Build Coastguard Worker         fatal(
2158*77c1e3ccSAndroid Build Coastguard Worker             "Specify stream dimensions with --width (-w) "
2159*77c1e3ccSAndroid Build Coastguard Worker             " and --height (-h)");
2160*77c1e3ccSAndroid Build Coastguard Worker       }
2161*77c1e3ccSAndroid Build Coastguard Worker     }
2162*77c1e3ccSAndroid Build Coastguard Worker 
2163*77c1e3ccSAndroid Build Coastguard Worker     if (need_downscale) {
2164*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2165*77c1e3ccSAndroid Build Coastguard Worker         if (stream->config.two_pass_width && stream->config.two_pass_height) {
2166*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = stream->config.two_pass_width;
2167*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = stream->config.two_pass_height;
2168*77c1e3ccSAndroid Build Coastguard Worker         } else if (two_pass_input) {
2169*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = input.width;
2170*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = input.height;
2171*77c1e3ccSAndroid Build Coastguard Worker         } else if (stream->orig_width && stream->orig_height) {
2172*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2173*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = stream->orig_width;
2174*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = stream->orig_height;
2175*77c1e3ccSAndroid Build Coastguard Worker #else   // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2176*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = (stream->orig_width + 1) / 2;
2177*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = (stream->orig_height + 1) / 2;
2178*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2179*77c1e3ccSAndroid Build Coastguard Worker         } else {
2180*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2181*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = input.width;
2182*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = input.height;
2183*77c1e3ccSAndroid Build Coastguard Worker #else   // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2184*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_w = (input.width + 1) / 2;
2185*77c1e3ccSAndroid Build Coastguard Worker           stream->config.cfg.g_h = (input.height + 1) / 2;
2186*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2187*77c1e3ccSAndroid Build Coastguard Worker         }
2188*77c1e3ccSAndroid Build Coastguard Worker       }
2189*77c1e3ccSAndroid Build Coastguard Worker     }
2190*77c1e3ccSAndroid Build Coastguard Worker 
2191*77c1e3ccSAndroid Build Coastguard Worker     /* If input file does not specify bit-depth but input-bit-depth parameter
2192*77c1e3ccSAndroid Build Coastguard Worker      * exists, assume that to be the input bit-depth. However, if the
2193*77c1e3ccSAndroid Build Coastguard Worker      * input-bit-depth paramter does not exist, assume the input bit-depth
2194*77c1e3ccSAndroid Build Coastguard Worker      * to be the same as the codec bit-depth.
2195*77c1e3ccSAndroid Build Coastguard Worker      */
2196*77c1e3ccSAndroid Build Coastguard Worker     if (!input.bit_depth) {
2197*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2198*77c1e3ccSAndroid Build Coastguard Worker         if (stream->config.cfg.g_input_bit_depth)
2199*77c1e3ccSAndroid Build Coastguard Worker           input.bit_depth = stream->config.cfg.g_input_bit_depth;
2200*77c1e3ccSAndroid Build Coastguard Worker         else
2201*77c1e3ccSAndroid Build Coastguard Worker           input.bit_depth = stream->config.cfg.g_input_bit_depth =
2202*77c1e3ccSAndroid Build Coastguard Worker               (int)stream->config.cfg.g_bit_depth;
2203*77c1e3ccSAndroid Build Coastguard Worker       }
2204*77c1e3ccSAndroid Build Coastguard Worker       if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
2205*77c1e3ccSAndroid Build Coastguard Worker     } else {
2206*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2207*77c1e3ccSAndroid Build Coastguard Worker         stream->config.cfg.g_input_bit_depth = input.bit_depth;
2208*77c1e3ccSAndroid Build Coastguard Worker       }
2209*77c1e3ccSAndroid Build Coastguard Worker     }
2210*77c1e3ccSAndroid Build Coastguard Worker 
2211*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2212*77c1e3ccSAndroid Build Coastguard Worker       if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016 &&
2213*77c1e3ccSAndroid Build Coastguard Worker           input.fmt != AOM_IMG_FMT_NV12) {
2214*77c1e3ccSAndroid Build Coastguard Worker         /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2215*77c1e3ccSAndroid Build Coastguard Worker            was selected. */
2216*77c1e3ccSAndroid Build Coastguard Worker         switch (stream->config.cfg.g_profile) {
2217*77c1e3ccSAndroid Build Coastguard Worker           case 0:
2218*77c1e3ccSAndroid Build Coastguard Worker             if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2219*77c1e3ccSAndroid Build Coastguard Worker                                          input.fmt == AOM_IMG_FMT_I44416)) {
2220*77c1e3ccSAndroid Build Coastguard Worker               if (!stream->config.cfg.monochrome) {
2221*77c1e3ccSAndroid Build Coastguard Worker                 stream->config.cfg.g_profile = 1;
2222*77c1e3ccSAndroid Build Coastguard Worker                 profile_updated = 1;
2223*77c1e3ccSAndroid Build Coastguard Worker               }
2224*77c1e3ccSAndroid Build Coastguard Worker             } else if (input.bit_depth == 12 ||
2225*77c1e3ccSAndroid Build Coastguard Worker                        ((input.fmt == AOM_IMG_FMT_I422 ||
2226*77c1e3ccSAndroid Build Coastguard Worker                          input.fmt == AOM_IMG_FMT_I42216) &&
2227*77c1e3ccSAndroid Build Coastguard Worker                         !stream->config.cfg.monochrome)) {
2228*77c1e3ccSAndroid Build Coastguard Worker               stream->config.cfg.g_profile = 2;
2229*77c1e3ccSAndroid Build Coastguard Worker               profile_updated = 1;
2230*77c1e3ccSAndroid Build Coastguard Worker             }
2231*77c1e3ccSAndroid Build Coastguard Worker             break;
2232*77c1e3ccSAndroid Build Coastguard Worker           case 1:
2233*77c1e3ccSAndroid Build Coastguard Worker             if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2234*77c1e3ccSAndroid Build Coastguard Worker                 input.fmt == AOM_IMG_FMT_I42216) {
2235*77c1e3ccSAndroid Build Coastguard Worker               stream->config.cfg.g_profile = 2;
2236*77c1e3ccSAndroid Build Coastguard Worker               profile_updated = 1;
2237*77c1e3ccSAndroid Build Coastguard Worker             } else if (input.bit_depth < 12 &&
2238*77c1e3ccSAndroid Build Coastguard Worker                        (input.fmt == AOM_IMG_FMT_I420 ||
2239*77c1e3ccSAndroid Build Coastguard Worker                         input.fmt == AOM_IMG_FMT_I42016)) {
2240*77c1e3ccSAndroid Build Coastguard Worker               stream->config.cfg.g_profile = 0;
2241*77c1e3ccSAndroid Build Coastguard Worker               profile_updated = 1;
2242*77c1e3ccSAndroid Build Coastguard Worker             }
2243*77c1e3ccSAndroid Build Coastguard Worker             break;
2244*77c1e3ccSAndroid Build Coastguard Worker           case 2:
2245*77c1e3ccSAndroid Build Coastguard Worker             if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2246*77c1e3ccSAndroid Build Coastguard Worker                                          input.fmt == AOM_IMG_FMT_I44416)) {
2247*77c1e3ccSAndroid Build Coastguard Worker               stream->config.cfg.g_profile = 1;
2248*77c1e3ccSAndroid Build Coastguard Worker               profile_updated = 1;
2249*77c1e3ccSAndroid Build Coastguard Worker             } else if (input.bit_depth < 12 &&
2250*77c1e3ccSAndroid Build Coastguard Worker                        (input.fmt == AOM_IMG_FMT_I420 ||
2251*77c1e3ccSAndroid Build Coastguard Worker                         input.fmt == AOM_IMG_FMT_I42016)) {
2252*77c1e3ccSAndroid Build Coastguard Worker               stream->config.cfg.g_profile = 0;
2253*77c1e3ccSAndroid Build Coastguard Worker               profile_updated = 1;
2254*77c1e3ccSAndroid Build Coastguard Worker             } else if (input.bit_depth == 12 &&
2255*77c1e3ccSAndroid Build Coastguard Worker                        input.file_type == FILE_TYPE_Y4M) {
2256*77c1e3ccSAndroid Build Coastguard Worker               // Note that here the input file values for chroma subsampling
2257*77c1e3ccSAndroid Build Coastguard Worker               // are used instead of those from the command line.
2258*77c1e3ccSAndroid Build Coastguard Worker               AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2259*77c1e3ccSAndroid Build Coastguard Worker                                             AV1E_SET_CHROMA_SUBSAMPLING_X,
2260*77c1e3ccSAndroid Build Coastguard Worker                                             input.y4m.dst_c_dec_h >> 1);
2261*77c1e3ccSAndroid Build Coastguard Worker               ctx_exit_on_error(&stream->encoder,
2262*77c1e3ccSAndroid Build Coastguard Worker                                 "Failed to set chroma subsampling x");
2263*77c1e3ccSAndroid Build Coastguard Worker               AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2264*77c1e3ccSAndroid Build Coastguard Worker                                             AV1E_SET_CHROMA_SUBSAMPLING_Y,
2265*77c1e3ccSAndroid Build Coastguard Worker                                             input.y4m.dst_c_dec_v >> 1);
2266*77c1e3ccSAndroid Build Coastguard Worker               ctx_exit_on_error(&stream->encoder,
2267*77c1e3ccSAndroid Build Coastguard Worker                                 "Failed to set chroma subsampling y");
2268*77c1e3ccSAndroid Build Coastguard Worker             } else if (input.bit_depth == 12 &&
2269*77c1e3ccSAndroid Build Coastguard Worker                        input.file_type == FILE_TYPE_RAW) {
2270*77c1e3ccSAndroid Build Coastguard Worker               AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2271*77c1e3ccSAndroid Build Coastguard Worker                                             AV1E_SET_CHROMA_SUBSAMPLING_X,
2272*77c1e3ccSAndroid Build Coastguard Worker                                             stream->chroma_subsampling_x);
2273*77c1e3ccSAndroid Build Coastguard Worker               ctx_exit_on_error(&stream->encoder,
2274*77c1e3ccSAndroid Build Coastguard Worker                                 "Failed to set chroma subsampling x");
2275*77c1e3ccSAndroid Build Coastguard Worker               AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2276*77c1e3ccSAndroid Build Coastguard Worker                                             AV1E_SET_CHROMA_SUBSAMPLING_Y,
2277*77c1e3ccSAndroid Build Coastguard Worker                                             stream->chroma_subsampling_y);
2278*77c1e3ccSAndroid Build Coastguard Worker               ctx_exit_on_error(&stream->encoder,
2279*77c1e3ccSAndroid Build Coastguard Worker                                 "Failed to set chroma subsampling y");
2280*77c1e3ccSAndroid Build Coastguard Worker             }
2281*77c1e3ccSAndroid Build Coastguard Worker             break;
2282*77c1e3ccSAndroid Build Coastguard Worker           default: break;
2283*77c1e3ccSAndroid Build Coastguard Worker         }
2284*77c1e3ccSAndroid Build Coastguard Worker       }
2285*77c1e3ccSAndroid Build Coastguard Worker       /* Automatically set the codec bit depth to match the input bit depth.
2286*77c1e3ccSAndroid Build Coastguard Worker        * Upgrade the profile if required. */
2287*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.cfg.g_input_bit_depth >
2288*77c1e3ccSAndroid Build Coastguard Worker           (unsigned int)stream->config.cfg.g_bit_depth) {
2289*77c1e3ccSAndroid Build Coastguard Worker         stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2290*77c1e3ccSAndroid Build Coastguard Worker         if (!global.quiet) {
2291*77c1e3ccSAndroid Build Coastguard Worker           fprintf(stderr,
2292*77c1e3ccSAndroid Build Coastguard Worker                   "Warning: automatically updating bit depth to %d to "
2293*77c1e3ccSAndroid Build Coastguard Worker                   "match input format.\n",
2294*77c1e3ccSAndroid Build Coastguard Worker                   stream->config.cfg.g_input_bit_depth);
2295*77c1e3ccSAndroid Build Coastguard Worker         }
2296*77c1e3ccSAndroid Build Coastguard Worker       }
2297*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_AV1_HIGHBITDEPTH
2298*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.cfg.g_bit_depth > 8) {
2299*77c1e3ccSAndroid Build Coastguard Worker         fatal("Unsupported bit-depth with CONFIG_AV1_HIGHBITDEPTH=0\n");
2300*77c1e3ccSAndroid Build Coastguard Worker       }
2301*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_HIGHBITDEPTH
2302*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.cfg.g_bit_depth > 10) {
2303*77c1e3ccSAndroid Build Coastguard Worker         switch (stream->config.cfg.g_profile) {
2304*77c1e3ccSAndroid Build Coastguard Worker           case 0:
2305*77c1e3ccSAndroid Build Coastguard Worker           case 1:
2306*77c1e3ccSAndroid Build Coastguard Worker             stream->config.cfg.g_profile = 2;
2307*77c1e3ccSAndroid Build Coastguard Worker             profile_updated = 1;
2308*77c1e3ccSAndroid Build Coastguard Worker             break;
2309*77c1e3ccSAndroid Build Coastguard Worker           default: break;
2310*77c1e3ccSAndroid Build Coastguard Worker         }
2311*77c1e3ccSAndroid Build Coastguard Worker       }
2312*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.cfg.g_bit_depth > 8) {
2313*77c1e3ccSAndroid Build Coastguard Worker         stream->config.use_16bit_internal = 1;
2314*77c1e3ccSAndroid Build Coastguard Worker       }
2315*77c1e3ccSAndroid Build Coastguard Worker       if (profile_updated && !global.quiet) {
2316*77c1e3ccSAndroid Build Coastguard Worker         fprintf(stderr,
2317*77c1e3ccSAndroid Build Coastguard Worker                 "Warning: automatically updating to profile %d to "
2318*77c1e3ccSAndroid Build Coastguard Worker                 "match input format.\n",
2319*77c1e3ccSAndroid Build Coastguard Worker                 stream->config.cfg.g_profile);
2320*77c1e3ccSAndroid Build Coastguard Worker       }
2321*77c1e3ccSAndroid Build Coastguard Worker       if ((global.show_psnr == 2) && (stream->config.cfg.g_input_bit_depth ==
2322*77c1e3ccSAndroid Build Coastguard Worker                                       stream->config.cfg.g_bit_depth)) {
2323*77c1e3ccSAndroid Build Coastguard Worker         fprintf(stderr,
2324*77c1e3ccSAndroid Build Coastguard Worker                 "Warning: --psnr==2 and --psnr==1 will provide same "
2325*77c1e3ccSAndroid Build Coastguard Worker                 "results when input bit-depth == stream bit-depth, "
2326*77c1e3ccSAndroid Build Coastguard Worker                 "falling back to default psnr value\n");
2327*77c1e3ccSAndroid Build Coastguard Worker         global.show_psnr = 1;
2328*77c1e3ccSAndroid Build Coastguard Worker       }
2329*77c1e3ccSAndroid Build Coastguard Worker       if (global.show_psnr < 0 || global.show_psnr > 2) {
2330*77c1e3ccSAndroid Build Coastguard Worker         fprintf(stderr,
2331*77c1e3ccSAndroid Build Coastguard Worker                 "Warning: --psnr can take only 0,1,2 as values,"
2332*77c1e3ccSAndroid Build Coastguard Worker                 "falling back to default psnr value\n");
2333*77c1e3ccSAndroid Build Coastguard Worker         global.show_psnr = 1;
2334*77c1e3ccSAndroid Build Coastguard Worker       }
2335*77c1e3ccSAndroid Build Coastguard Worker       /* Set limit */
2336*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_limit = global.limit;
2337*77c1e3ccSAndroid Build Coastguard Worker     }
2338*77c1e3ccSAndroid Build Coastguard Worker 
2339*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2340*77c1e3ccSAndroid Build Coastguard Worker       set_stream_dimensions(stream, input.width, input.height);
2341*77c1e3ccSAndroid Build Coastguard Worker       stream->config.color_range = input.color_range;
2342*77c1e3ccSAndroid Build Coastguard Worker     }
2343*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2344*77c1e3ccSAndroid Build Coastguard Worker 
2345*77c1e3ccSAndroid Build Coastguard Worker     /* Ensure that --passes and --pass are consistent. If --pass is set and
2346*77c1e3ccSAndroid Build Coastguard Worker      * --passes >= 2, ensure --fpf was set.
2347*77c1e3ccSAndroid Build Coastguard Worker      */
2348*77c1e3ccSAndroid Build Coastguard Worker     if (global.pass > 0 && global.pass <= 3 && global.passes >= 2) {
2349*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2350*77c1e3ccSAndroid Build Coastguard Worker         if (!stream->config.stats_fn)
2351*77c1e3ccSAndroid Build Coastguard Worker           die("Stream %d: Must specify --fpf when --pass=%d"
2352*77c1e3ccSAndroid Build Coastguard Worker               " and --passes=%d\n",
2353*77c1e3ccSAndroid Build Coastguard Worker               stream->index, global.pass, global.passes);
2354*77c1e3ccSAndroid Build Coastguard Worker       }
2355*77c1e3ccSAndroid Build Coastguard Worker     }
2356*77c1e3ccSAndroid Build Coastguard Worker 
2357*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_WEBM_IO
2358*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2359*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.write_webm) {
2360*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_webm = 0;
2361*77c1e3ccSAndroid Build Coastguard Worker         stream->config.write_ivf = 0;
2362*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("aomenc compiled w/o WebM support. Writing OBU stream.");
2363*77c1e3ccSAndroid Build Coastguard Worker       }
2364*77c1e3ccSAndroid Build Coastguard Worker     }
2365*77c1e3ccSAndroid Build Coastguard Worker #endif
2366*77c1e3ccSAndroid Build Coastguard Worker 
2367*77c1e3ccSAndroid Build Coastguard Worker     /* Use the frame rate from the file only if none was specified
2368*77c1e3ccSAndroid Build Coastguard Worker      * on the command-line.
2369*77c1e3ccSAndroid Build Coastguard Worker      */
2370*77c1e3ccSAndroid Build Coastguard Worker     if (!global.have_framerate) {
2371*77c1e3ccSAndroid Build Coastguard Worker       global.framerate.num = input.framerate.numerator;
2372*77c1e3ccSAndroid Build Coastguard Worker       global.framerate.den = input.framerate.denominator;
2373*77c1e3ccSAndroid Build Coastguard Worker     }
2374*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2375*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_timebase.den = global.framerate.num;
2376*77c1e3ccSAndroid Build Coastguard Worker       stream->config.cfg.g_timebase.num = global.framerate.den;
2377*77c1e3ccSAndroid Build Coastguard Worker     }
2378*77c1e3ccSAndroid Build Coastguard Worker     /* Show configuration */
2379*77c1e3ccSAndroid Build Coastguard Worker     if (global.verbose && pass == 0) {
2380*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2381*77c1e3ccSAndroid Build Coastguard Worker         show_stream_config(stream, &global, &input);
2382*77c1e3ccSAndroid Build Coastguard Worker       }
2383*77c1e3ccSAndroid Build Coastguard Worker     }
2384*77c1e3ccSAndroid Build Coastguard Worker 
2385*77c1e3ccSAndroid Build Coastguard Worker     if (pass == (global.pass ? global.pass - 1 : 0)) {
2386*77c1e3ccSAndroid Build Coastguard Worker       // The Y4M reader does its own allocation.
2387*77c1e3ccSAndroid Build Coastguard Worker       if (input.file_type != FILE_TYPE_Y4M) {
2388*77c1e3ccSAndroid Build Coastguard Worker         aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2389*77c1e3ccSAndroid Build Coastguard Worker       }
2390*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2391*77c1e3ccSAndroid Build Coastguard Worker         stream->rate_hist =
2392*77c1e3ccSAndroid Build Coastguard Worker             init_rate_histogram(&stream->config.cfg, &global.framerate);
2393*77c1e3ccSAndroid Build Coastguard Worker       }
2394*77c1e3ccSAndroid Build Coastguard Worker     }
2395*77c1e3ccSAndroid Build Coastguard Worker 
2396*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2397*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2398*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2399*77c1e3ccSAndroid Build Coastguard Worker       char *encoder_settings = NULL;
2400*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_WEBM_IO
2401*77c1e3ccSAndroid Build Coastguard Worker       // Test frameworks may compare outputs from different versions, but only
2402*77c1e3ccSAndroid Build Coastguard Worker       // wish to check for bitstream changes. The encoder-settings tag, however,
2403*77c1e3ccSAndroid Build Coastguard Worker       // can vary if the version is updated, even if no encoder algorithm
2404*77c1e3ccSAndroid Build Coastguard Worker       // changes were made. To work around this issue, do not output
2405*77c1e3ccSAndroid Build Coastguard Worker       // the encoder-settings tag when --debug is enabled (which is the flag
2406*77c1e3ccSAndroid Build Coastguard Worker       // that test frameworks should use, when they want deterministic output
2407*77c1e3ccSAndroid Build Coastguard Worker       // from the container format).
2408*77c1e3ccSAndroid Build Coastguard Worker       if (stream->config.write_webm && !stream->webm_ctx.debug) {
2409*77c1e3ccSAndroid Build Coastguard Worker         encoder_settings = extract_encoder_settings(
2410*77c1e3ccSAndroid Build Coastguard Worker             aom_codec_version_str(), argv_, argc, input.filename);
2411*77c1e3ccSAndroid Build Coastguard Worker         if (encoder_settings == NULL) {
2412*77c1e3ccSAndroid Build Coastguard Worker           fprintf(
2413*77c1e3ccSAndroid Build Coastguard Worker               stderr,
2414*77c1e3ccSAndroid Build Coastguard Worker               "Warning: unable to extract encoder settings. Continuing...\n");
2415*77c1e3ccSAndroid Build Coastguard Worker         }
2416*77c1e3ccSAndroid Build Coastguard Worker       }
2417*77c1e3ccSAndroid Build Coastguard Worker #endif
2418*77c1e3ccSAndroid Build Coastguard Worker       open_output_file(stream, &global, &input.pixel_aspect_ratio,
2419*77c1e3ccSAndroid Build Coastguard Worker                        encoder_settings);
2420*77c1e3ccSAndroid Build Coastguard Worker       free(encoder_settings);
2421*77c1e3ccSAndroid Build Coastguard Worker     }
2422*77c1e3ccSAndroid Build Coastguard Worker 
2423*77c1e3ccSAndroid Build Coastguard Worker     if (strcmp(get_short_name_by_aom_encoder(global.codec), "av1") == 0) {
2424*77c1e3ccSAndroid Build Coastguard Worker       // Check to see if at least one stream uses 16 bit internal.
2425*77c1e3ccSAndroid Build Coastguard Worker       // Currently assume that the bit_depths for all streams using
2426*77c1e3ccSAndroid Build Coastguard Worker       // highbitdepth are the same.
2427*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2428*77c1e3ccSAndroid Build Coastguard Worker         if (stream->config.use_16bit_internal) {
2429*77c1e3ccSAndroid Build Coastguard Worker           do_16bit_internal = 1;
2430*77c1e3ccSAndroid Build Coastguard Worker         }
2431*77c1e3ccSAndroid Build Coastguard Worker         input_shift = (int)stream->config.cfg.g_bit_depth -
2432*77c1e3ccSAndroid Build Coastguard Worker                       stream->config.cfg.g_input_bit_depth;
2433*77c1e3ccSAndroid Build Coastguard Worker       }
2434*77c1e3ccSAndroid Build Coastguard Worker     }
2435*77c1e3ccSAndroid Build Coastguard Worker 
2436*77c1e3ccSAndroid Build Coastguard Worker     frame_avail = 1;
2437*77c1e3ccSAndroid Build Coastguard Worker     got_data = 0;
2438*77c1e3ccSAndroid Build Coastguard Worker 
2439*77c1e3ccSAndroid Build Coastguard Worker     while (frame_avail || got_data) {
2440*77c1e3ccSAndroid Build Coastguard Worker       struct aom_usec_timer timer;
2441*77c1e3ccSAndroid Build Coastguard Worker 
2442*77c1e3ccSAndroid Build Coastguard Worker       if (!global.limit || frames_in < global.limit) {
2443*77c1e3ccSAndroid Build Coastguard Worker         frame_avail = read_frame(&input, &raw);
2444*77c1e3ccSAndroid Build Coastguard Worker 
2445*77c1e3ccSAndroid Build Coastguard Worker         if (frame_avail) frames_in++;
2446*77c1e3ccSAndroid Build Coastguard Worker         seen_frames =
2447*77c1e3ccSAndroid Build Coastguard Worker             frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2448*77c1e3ccSAndroid Build Coastguard Worker 
2449*77c1e3ccSAndroid Build Coastguard Worker         if (!global.quiet) {
2450*77c1e3ccSAndroid Build Coastguard Worker           float fps = usec_to_fps(cx_time, seen_frames);
2451*77c1e3ccSAndroid Build Coastguard Worker           fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2452*77c1e3ccSAndroid Build Coastguard Worker 
2453*77c1e3ccSAndroid Build Coastguard Worker           if (stream_cnt == 1)
2454*77c1e3ccSAndroid Build Coastguard Worker             fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2455*77c1e3ccSAndroid Build Coastguard Worker                     streams->frames_out, (int64_t)streams->nbytes);
2456*77c1e3ccSAndroid Build Coastguard Worker           else
2457*77c1e3ccSAndroid Build Coastguard Worker             fprintf(stderr, "frame %4d ", frames_in);
2458*77c1e3ccSAndroid Build Coastguard Worker 
2459*77c1e3ccSAndroid Build Coastguard Worker           fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2460*77c1e3ccSAndroid Build Coastguard Worker                   cx_time > 9999999 ? cx_time / 1000 : cx_time,
2461*77c1e3ccSAndroid Build Coastguard Worker                   cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2462*77c1e3ccSAndroid Build Coastguard Worker                   fps >= 1.0 ? "fps" : "fpm");
2463*77c1e3ccSAndroid Build Coastguard Worker           print_time("ETA", estimated_time_left);
2464*77c1e3ccSAndroid Build Coastguard Worker           // mingw-w64 gcc does not match msvc for stderr buffering behavior
2465*77c1e3ccSAndroid Build Coastguard Worker           // and uses line buffering, thus the progress output is not
2466*77c1e3ccSAndroid Build Coastguard Worker           // real-time. The fflush() is here to make sure the progress output
2467*77c1e3ccSAndroid Build Coastguard Worker           // is sent out while the clip is being processed.
2468*77c1e3ccSAndroid Build Coastguard Worker           fflush(stderr);
2469*77c1e3ccSAndroid Build Coastguard Worker         }
2470*77c1e3ccSAndroid Build Coastguard Worker 
2471*77c1e3ccSAndroid Build Coastguard Worker       } else {
2472*77c1e3ccSAndroid Build Coastguard Worker         frame_avail = 0;
2473*77c1e3ccSAndroid Build Coastguard Worker       }
2474*77c1e3ccSAndroid Build Coastguard Worker 
2475*77c1e3ccSAndroid Build Coastguard Worker       if (frames_in > global.skip_frames) {
2476*77c1e3ccSAndroid Build Coastguard Worker         aom_image_t *frame_to_encode;
2477*77c1e3ccSAndroid Build Coastguard Worker         if (input_shift || (do_16bit_internal && input.bit_depth == 8)) {
2478*77c1e3ccSAndroid Build Coastguard Worker           assert(do_16bit_internal);
2479*77c1e3ccSAndroid Build Coastguard Worker           // Input bit depth and stream bit depth do not match, so up
2480*77c1e3ccSAndroid Build Coastguard Worker           // shift frame to stream bit depth
2481*77c1e3ccSAndroid Build Coastguard Worker           if (!allocated_raw_shift) {
2482*77c1e3ccSAndroid Build Coastguard Worker             aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2483*77c1e3ccSAndroid Build Coastguard Worker                           input.width, input.height, 32);
2484*77c1e3ccSAndroid Build Coastguard Worker             allocated_raw_shift = 1;
2485*77c1e3ccSAndroid Build Coastguard Worker           }
2486*77c1e3ccSAndroid Build Coastguard Worker           aom_img_upshift(&raw_shift, &raw, input_shift);
2487*77c1e3ccSAndroid Build Coastguard Worker           frame_to_encode = &raw_shift;
2488*77c1e3ccSAndroid Build Coastguard Worker         } else {
2489*77c1e3ccSAndroid Build Coastguard Worker           frame_to_encode = &raw;
2490*77c1e3ccSAndroid Build Coastguard Worker         }
2491*77c1e3ccSAndroid Build Coastguard Worker         aom_usec_timer_start(&timer);
2492*77c1e3ccSAndroid Build Coastguard Worker         if (do_16bit_internal) {
2493*77c1e3ccSAndroid Build Coastguard Worker           assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2494*77c1e3ccSAndroid Build Coastguard Worker           FOREACH_STREAM(stream, streams) {
2495*77c1e3ccSAndroid Build Coastguard Worker             if (stream->config.use_16bit_internal)
2496*77c1e3ccSAndroid Build Coastguard Worker               encode_frame(stream, &global,
2497*77c1e3ccSAndroid Build Coastguard Worker                            frame_avail ? frame_to_encode : NULL, frames_in);
2498*77c1e3ccSAndroid Build Coastguard Worker             else
2499*77c1e3ccSAndroid Build Coastguard Worker               assert(0);
2500*77c1e3ccSAndroid Build Coastguard Worker           }
2501*77c1e3ccSAndroid Build Coastguard Worker         } else {
2502*77c1e3ccSAndroid Build Coastguard Worker           assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2503*77c1e3ccSAndroid Build Coastguard Worker           FOREACH_STREAM(stream, streams) {
2504*77c1e3ccSAndroid Build Coastguard Worker             encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2505*77c1e3ccSAndroid Build Coastguard Worker                          frames_in);
2506*77c1e3ccSAndroid Build Coastguard Worker           }
2507*77c1e3ccSAndroid Build Coastguard Worker         }
2508*77c1e3ccSAndroid Build Coastguard Worker         aom_usec_timer_mark(&timer);
2509*77c1e3ccSAndroid Build Coastguard Worker         cx_time += aom_usec_timer_elapsed(&timer);
2510*77c1e3ccSAndroid Build Coastguard Worker 
2511*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2512*77c1e3ccSAndroid Build Coastguard Worker 
2513*77c1e3ccSAndroid Build Coastguard Worker         got_data = 0;
2514*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) {
2515*77c1e3ccSAndroid Build Coastguard Worker           get_cx_data(stream, &global, &got_data);
2516*77c1e3ccSAndroid Build Coastguard Worker         }
2517*77c1e3ccSAndroid Build Coastguard Worker 
2518*77c1e3ccSAndroid Build Coastguard Worker         if (!got_data && input.length && streams != NULL &&
2519*77c1e3ccSAndroid Build Coastguard Worker             !streams->frames_out) {
2520*77c1e3ccSAndroid Build Coastguard Worker           lagged_count = global.limit ? seen_frames : ftello(input.file);
2521*77c1e3ccSAndroid Build Coastguard Worker         } else if (input.length) {
2522*77c1e3ccSAndroid Build Coastguard Worker           int64_t remaining;
2523*77c1e3ccSAndroid Build Coastguard Worker           int64_t rate;
2524*77c1e3ccSAndroid Build Coastguard Worker 
2525*77c1e3ccSAndroid Build Coastguard Worker           if (global.limit) {
2526*77c1e3ccSAndroid Build Coastguard Worker             const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2527*77c1e3ccSAndroid Build Coastguard Worker 
2528*77c1e3ccSAndroid Build Coastguard Worker             rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2529*77c1e3ccSAndroid Build Coastguard Worker             remaining = 1000 * (global.limit - global.skip_frames -
2530*77c1e3ccSAndroid Build Coastguard Worker                                 seen_frames + lagged_count);
2531*77c1e3ccSAndroid Build Coastguard Worker           } else {
2532*77c1e3ccSAndroid Build Coastguard Worker             const int64_t input_pos = ftello(input.file);
2533*77c1e3ccSAndroid Build Coastguard Worker             const int64_t input_pos_lagged = input_pos - lagged_count;
2534*77c1e3ccSAndroid Build Coastguard Worker             const int64_t input_limit = input.length;
2535*77c1e3ccSAndroid Build Coastguard Worker 
2536*77c1e3ccSAndroid Build Coastguard Worker             rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2537*77c1e3ccSAndroid Build Coastguard Worker             remaining = input_limit - input_pos + lagged_count;
2538*77c1e3ccSAndroid Build Coastguard Worker           }
2539*77c1e3ccSAndroid Build Coastguard Worker 
2540*77c1e3ccSAndroid Build Coastguard Worker           average_rate =
2541*77c1e3ccSAndroid Build Coastguard Worker               (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2542*77c1e3ccSAndroid Build Coastguard Worker           estimated_time_left = average_rate ? remaining / average_rate : -1;
2543*77c1e3ccSAndroid Build Coastguard Worker         }
2544*77c1e3ccSAndroid Build Coastguard Worker 
2545*77c1e3ccSAndroid Build Coastguard Worker         if (got_data && global.test_decode != TEST_DECODE_OFF) {
2546*77c1e3ccSAndroid Build Coastguard Worker           FOREACH_STREAM(stream, streams) {
2547*77c1e3ccSAndroid Build Coastguard Worker             test_decode(stream, global.test_decode);
2548*77c1e3ccSAndroid Build Coastguard Worker           }
2549*77c1e3ccSAndroid Build Coastguard Worker         }
2550*77c1e3ccSAndroid Build Coastguard Worker       }
2551*77c1e3ccSAndroid Build Coastguard Worker 
2552*77c1e3ccSAndroid Build Coastguard Worker       fflush(stdout);
2553*77c1e3ccSAndroid Build Coastguard Worker       if (!global.quiet) fprintf(stderr, "\033[K");
2554*77c1e3ccSAndroid Build Coastguard Worker     }
2555*77c1e3ccSAndroid Build Coastguard Worker 
2556*77c1e3ccSAndroid Build Coastguard Worker     if (stream_cnt > 1) fprintf(stderr, "\n");
2557*77c1e3ccSAndroid Build Coastguard Worker 
2558*77c1e3ccSAndroid Build Coastguard Worker     if (!global.quiet) {
2559*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2560*77c1e3ccSAndroid Build Coastguard Worker         const int64_t bpf =
2561*77c1e3ccSAndroid Build Coastguard Worker             seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
2562*77c1e3ccSAndroid Build Coastguard Worker         const int64_t bps = bpf * global.framerate.num / global.framerate.den;
2563*77c1e3ccSAndroid Build Coastguard Worker         fprintf(stderr,
2564*77c1e3ccSAndroid Build Coastguard Worker                 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2565*77c1e3ccSAndroid Build Coastguard Worker                 "b/f %7" PRId64
2566*77c1e3ccSAndroid Build Coastguard Worker                 "b/s"
2567*77c1e3ccSAndroid Build Coastguard Worker                 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2568*77c1e3ccSAndroid Build Coastguard Worker                 pass + 1, global.passes, frames_in, stream->frames_out,
2569*77c1e3ccSAndroid Build Coastguard Worker                 (int64_t)stream->nbytes, bpf, bps,
2570*77c1e3ccSAndroid Build Coastguard Worker                 stream->cx_time > 9999999 ? stream->cx_time / 1000
2571*77c1e3ccSAndroid Build Coastguard Worker                                           : stream->cx_time,
2572*77c1e3ccSAndroid Build Coastguard Worker                 stream->cx_time > 9999999 ? "ms" : "us",
2573*77c1e3ccSAndroid Build Coastguard Worker                 usec_to_fps(stream->cx_time, seen_frames));
2574*77c1e3ccSAndroid Build Coastguard Worker         // This instance of cr does not need fflush as it is followed by a
2575*77c1e3ccSAndroid Build Coastguard Worker         // newline in the same string.
2576*77c1e3ccSAndroid Build Coastguard Worker       }
2577*77c1e3ccSAndroid Build Coastguard Worker     }
2578*77c1e3ccSAndroid Build Coastguard Worker 
2579*77c1e3ccSAndroid Build Coastguard Worker     if (global.show_psnr >= 1) {
2580*77c1e3ccSAndroid Build Coastguard Worker       if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC) {
2581*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) {
2582*77c1e3ccSAndroid Build Coastguard Worker           int64_t bps = 0;
2583*77c1e3ccSAndroid Build Coastguard Worker           if (global.show_psnr == 1) {
2584*77c1e3ccSAndroid Build Coastguard Worker             if (stream->psnr_count[0] && seen_frames && global.framerate.den) {
2585*77c1e3ccSAndroid Build Coastguard Worker               bps = (int64_t)stream->nbytes * 8 *
2586*77c1e3ccSAndroid Build Coastguard Worker                     (int64_t)global.framerate.num / global.framerate.den /
2587*77c1e3ccSAndroid Build Coastguard Worker                     seen_frames;
2588*77c1e3ccSAndroid Build Coastguard Worker             }
2589*77c1e3ccSAndroid Build Coastguard Worker             show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
2590*77c1e3ccSAndroid Build Coastguard Worker                       bps);
2591*77c1e3ccSAndroid Build Coastguard Worker           }
2592*77c1e3ccSAndroid Build Coastguard Worker           if (global.show_psnr == 2) {
2593*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
2594*77c1e3ccSAndroid Build Coastguard Worker             if (stream->config.cfg.g_input_bit_depth <
2595*77c1e3ccSAndroid Build Coastguard Worker                 (unsigned int)stream->config.cfg.g_bit_depth)
2596*77c1e3ccSAndroid Build Coastguard Worker               show_psnr_hbd(stream, (1 << stream->config.cfg.g_bit_depth) - 1,
2597*77c1e3ccSAndroid Build Coastguard Worker                             bps);
2598*77c1e3ccSAndroid Build Coastguard Worker #endif
2599*77c1e3ccSAndroid Build Coastguard Worker           }
2600*77c1e3ccSAndroid Build Coastguard Worker         }
2601*77c1e3ccSAndroid Build Coastguard Worker       } else {
2602*77c1e3ccSAndroid Build Coastguard Worker         FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
2603*77c1e3ccSAndroid Build Coastguard Worker       }
2604*77c1e3ccSAndroid Build Coastguard Worker     }
2605*77c1e3ccSAndroid Build Coastguard Worker 
2606*77c1e3ccSAndroid Build Coastguard Worker     if (pass == global.passes - 1) {
2607*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) {
2608*77c1e3ccSAndroid Build Coastguard Worker         int num_operating_points;
2609*77c1e3ccSAndroid Build Coastguard Worker         int levels[32];
2610*77c1e3ccSAndroid Build Coastguard Worker         int target_levels[32];
2611*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&stream->encoder, AV1E_GET_NUM_OPERATING_POINTS,
2612*77c1e3ccSAndroid Build Coastguard Worker                           &num_operating_points);
2613*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&stream->encoder, AV1E_GET_SEQ_LEVEL_IDX, levels);
2614*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&stream->encoder, AV1E_GET_TARGET_SEQ_LEVEL_IDX,
2615*77c1e3ccSAndroid Build Coastguard Worker                           target_levels);
2616*77c1e3ccSAndroid Build Coastguard Worker 
2617*77c1e3ccSAndroid Build Coastguard Worker         for (int i = 0; i < num_operating_points; i++) {
2618*77c1e3ccSAndroid Build Coastguard Worker           if (levels[i] > target_levels[i]) {
2619*77c1e3ccSAndroid Build Coastguard Worker             if (levels[i] == 31) {
2620*77c1e3ccSAndroid Build Coastguard Worker               aom_tools_warn(
2621*77c1e3ccSAndroid Build Coastguard Worker                   "Failed to encode to target level %d.%d for operating point "
2622*77c1e3ccSAndroid Build Coastguard Worker                   "%d. The output level is SEQ_LEVEL_MAX",
2623*77c1e3ccSAndroid Build Coastguard Worker                   2 + (target_levels[i] >> 2), target_levels[i] & 3, i);
2624*77c1e3ccSAndroid Build Coastguard Worker             } else {
2625*77c1e3ccSAndroid Build Coastguard Worker               aom_tools_warn(
2626*77c1e3ccSAndroid Build Coastguard Worker                   "Failed to encode to target level %d.%d for operating point "
2627*77c1e3ccSAndroid Build Coastguard Worker                   "%d. The output level is %d.%d",
2628*77c1e3ccSAndroid Build Coastguard Worker                   2 + (target_levels[i] >> 2), target_levels[i] & 3, i,
2629*77c1e3ccSAndroid Build Coastguard Worker                   2 + (levels[i] >> 2), levels[i] & 3);
2630*77c1e3ccSAndroid Build Coastguard Worker             }
2631*77c1e3ccSAndroid Build Coastguard Worker           }
2632*77c1e3ccSAndroid Build Coastguard Worker         }
2633*77c1e3ccSAndroid Build Coastguard Worker       }
2634*77c1e3ccSAndroid Build Coastguard Worker     }
2635*77c1e3ccSAndroid Build Coastguard Worker 
2636*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
2637*77c1e3ccSAndroid Build Coastguard Worker 
2638*77c1e3ccSAndroid Build Coastguard Worker     if (global.test_decode != TEST_DECODE_OFF) {
2639*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
2640*77c1e3ccSAndroid Build Coastguard Worker     }
2641*77c1e3ccSAndroid Build Coastguard Worker 
2642*77c1e3ccSAndroid Build Coastguard Worker     close_input_file(&input);
2643*77c1e3ccSAndroid Build Coastguard Worker 
2644*77c1e3ccSAndroid Build Coastguard Worker     if (global.test_decode == TEST_DECODE_FATAL) {
2645*77c1e3ccSAndroid Build Coastguard Worker       FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2646*77c1e3ccSAndroid Build Coastguard Worker     }
2647*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2648*77c1e3ccSAndroid Build Coastguard Worker       close_output_file(stream, get_fourcc_by_aom_encoder(global.codec));
2649*77c1e3ccSAndroid Build Coastguard Worker     }
2650*77c1e3ccSAndroid Build Coastguard Worker 
2651*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2652*77c1e3ccSAndroid Build Coastguard Worker       stats_close(&stream->stats, global.passes - 1);
2653*77c1e3ccSAndroid Build Coastguard Worker     }
2654*77c1e3ccSAndroid Build Coastguard Worker 
2655*77c1e3ccSAndroid Build Coastguard Worker     if (global.pass) break;
2656*77c1e3ccSAndroid Build Coastguard Worker   }
2657*77c1e3ccSAndroid Build Coastguard Worker 
2658*77c1e3ccSAndroid Build Coastguard Worker   if (global.show_q_hist_buckets) {
2659*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2660*77c1e3ccSAndroid Build Coastguard Worker       show_q_histogram(stream->counts, global.show_q_hist_buckets);
2661*77c1e3ccSAndroid Build Coastguard Worker     }
2662*77c1e3ccSAndroid Build Coastguard Worker   }
2663*77c1e3ccSAndroid Build Coastguard Worker 
2664*77c1e3ccSAndroid Build Coastguard Worker   if (global.show_rate_hist_buckets) {
2665*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2666*77c1e3ccSAndroid Build Coastguard Worker       show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2667*77c1e3ccSAndroid Build Coastguard Worker                           global.show_rate_hist_buckets);
2668*77c1e3ccSAndroid Build Coastguard Worker     }
2669*77c1e3ccSAndroid Build Coastguard Worker   }
2670*77c1e3ccSAndroid Build Coastguard Worker   FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2671*77c1e3ccSAndroid Build Coastguard Worker 
2672*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
2673*77c1e3ccSAndroid Build Coastguard Worker   /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2674*77c1e3ccSAndroid Build Coastguard Worker    * to match some existing utilities.
2675*77c1e3ccSAndroid Build Coastguard Worker    */
2676*77c1e3ccSAndroid Build Coastguard Worker   if (!(global.pass == 1 && global.passes == 2)) {
2677*77c1e3ccSAndroid Build Coastguard Worker     FOREACH_STREAM(stream, streams) {
2678*77c1e3ccSAndroid Build Coastguard Worker       FILE *f = fopen("opsnr.stt", "a");
2679*77c1e3ccSAndroid Build Coastguard Worker       if (stream->mismatch_seen) {
2680*77c1e3ccSAndroid Build Coastguard Worker         fprintf(f, "First mismatch occurred in frame %d\n",
2681*77c1e3ccSAndroid Build Coastguard Worker                 stream->mismatch_seen);
2682*77c1e3ccSAndroid Build Coastguard Worker       } else {
2683*77c1e3ccSAndroid Build Coastguard Worker         fprintf(f, "No mismatch detected in recon buffers\n");
2684*77c1e3ccSAndroid Build Coastguard Worker       }
2685*77c1e3ccSAndroid Build Coastguard Worker       fclose(f);
2686*77c1e3ccSAndroid Build Coastguard Worker     }
2687*77c1e3ccSAndroid Build Coastguard Worker   }
2688*77c1e3ccSAndroid Build Coastguard Worker #endif
2689*77c1e3ccSAndroid Build Coastguard Worker 
2690*77c1e3ccSAndroid Build Coastguard Worker   if (allocated_raw_shift) aom_img_free(&raw_shift);
2691*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&raw);
2692*77c1e3ccSAndroid Build Coastguard Worker   free(argv);
2693*77c1e3ccSAndroid Build Coastguard Worker   free(streams);
2694*77c1e3ccSAndroid Build Coastguard Worker   return res ? EXIT_FAILURE : EXIT_SUCCESS;
2695*77c1e3ccSAndroid Build Coastguard Worker }
2696