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