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 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "config/aom_config.h"
15 #include "config/aom_version.h"
16 
17 #include "aom_ports/aom_once.h"
18 #include "aom_ports/mem_ops.h"
19 #include "aom_ports/system_state.h"
20 
21 #include "aom/aom_encoder.h"
22 #include "aom/internal/aom_codec_internal.h"
23 
24 #include "av1/av1_iface_common.h"
25 #include "av1/encoder/bitstream.h"
26 #include "av1/encoder/encoder.h"
27 #include "av1/encoder/firstpass.h"
28 
29 #define MAG_SIZE (4)
30 #define MAX_NUM_ENHANCEMENT_LAYERS 3
31 
32 struct av1_extracfg {
33   int cpu_used;  // available cpu percentage in 1/16
34   unsigned int enable_auto_alt_ref;
35   unsigned int enable_auto_bwd_ref;
36   unsigned int noise_sensitivity;
37   unsigned int sharpness;
38   unsigned int static_thresh;
39   unsigned int row_mt;
40   unsigned int tile_columns;  // log2 number of tile columns
41   unsigned int tile_rows;     // log2 number of tile rows
42   unsigned int arnr_max_frames;
43   unsigned int arnr_strength;
44   unsigned int min_gf_interval;
45   unsigned int max_gf_interval;
46   aom_tune_metric tuning;
47   unsigned int cq_level;  // constrained quality level
48   unsigned int rc_max_intra_bitrate_pct;
49   unsigned int rc_max_inter_bitrate_pct;
50   unsigned int gf_cbr_boost_pct;
51   unsigned int lossless;
52   unsigned int enable_cdef;
53   unsigned int enable_restoration;
54   unsigned int disable_trellis_quant;
55   unsigned int enable_qm;
56   unsigned int qm_y;
57   unsigned int qm_u;
58   unsigned int qm_v;
59   unsigned int qm_min;
60   unsigned int qm_max;
61 #if CONFIG_DIST_8X8
62   unsigned int enable_dist_8x8;
63 #endif
64   unsigned int num_tg;
65   unsigned int mtu_size;
66 
67   aom_timing_info_type_t timing_info_type;
68   unsigned int frame_parallel_decoding_mode;
69   int use_dual_filter;
70   AQ_MODE aq_mode;
71   DELTAQ_MODE deltaq_mode;
72   unsigned int frame_periodic_boost;
73   aom_bit_depth_t bit_depth;
74   aom_tune_content content;
75   aom_color_primaries_t color_primaries;
76   aom_transfer_characteristics_t transfer_characteristics;
77   aom_matrix_coefficients_t matrix_coefficients;
78   aom_chroma_sample_position_t chroma_sample_position;
79   int color_range;
80   int render_width;
81   int render_height;
82   aom_superblock_size_t superblock_size;
83   unsigned int single_tile_decoding;
84   int error_resilient_mode;
85   int s_frame_mode;
86 
87   int film_grain_test_vector;
88   const char *film_grain_table_filename;
89   unsigned int motion_vector_unit_test;
90   unsigned int cdf_update_mode;
91   int enable_order_hint;
92   int enable_jnt_comp;
93   int enable_ref_frame_mvs;  // sequence level
94   int allow_ref_frame_mvs;   // frame level
95   int enable_warped_motion;  // sequence level
96   int allow_warped_motion;   // frame level
97   int enable_superres;
98 #if CONFIG_DENOISE
99   float noise_level;
100   int noise_block_size;
101 #endif
102 
103   unsigned int chroma_subsampling_x;
104   unsigned int chroma_subsampling_y;
105 };
106 
107 static struct av1_extracfg default_extra_cfg = {
108   0,                       // cpu_used
109   1,                       // enable_auto_alt_ref
110   0,                       // enable_auto_bwd_ref
111   0,                       // noise_sensitivity
112   CONFIG_SHARP_SETTINGS,   // sharpness
113   0,                       // static_thresh
114   0,                       // row_mt
115   0,                       // tile_columns
116   0,                       // tile_rows
117   7,                       // arnr_max_frames
118   5,                       // arnr_strength
119   0,                       // min_gf_interval; 0 -> default decision
120   0,                       // max_gf_interval; 0 -> default decision
121   AOM_TUNE_PSNR,           // tuning
122   10,                      // cq_level
123   0,                       // rc_max_intra_bitrate_pct
124   0,                       // rc_max_inter_bitrate_pct
125   0,                       // gf_cbr_boost_pct
126   0,                       // lossless
127   !CONFIG_SHARP_SETTINGS,  // enable_cdef
128   1,                       // enable_restoration
129   0,                       // disable_trellis_quant
130   0,                       // enable_qm
131   DEFAULT_QM_Y,            // qm_y
132   DEFAULT_QM_U,            // qm_u
133   DEFAULT_QM_V,            // qm_v
134   DEFAULT_QM_FIRST,        // qm_min
135   DEFAULT_QM_LAST,         // qm_max
136 #if CONFIG_DIST_8X8
137   0,
138 #endif
139   1,                            // max number of tile groups
140   0,                            // mtu_size
141   AOM_TIMING_UNSPECIFIED,       // No picture timing signaling in bitstream
142   1,                            // frame_parallel_decoding_mode
143   1,                            // enable dual filter
144   NO_AQ,                        // aq_mode
145   NO_DELTA_Q,                   // deltaq_mode
146   0,                            // frame_periodic_delta_q
147   AOM_BITS_8,                   // Bit depth
148   AOM_CONTENT_DEFAULT,          // content
149   AOM_CICP_CP_UNSPECIFIED,      // CICP color space
150   AOM_CICP_TC_UNSPECIFIED,      // CICP transfer characteristics
151   AOM_CICP_MC_UNSPECIFIED,      // CICP matrix coefficients
152   AOM_CSP_UNKNOWN,              // chroma sample position
153   0,                            // color range
154   0,                            // render width
155   0,                            // render height
156   AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
157   1,                            // this depends on large_scale_tile.
158   0,                            // error_resilient_mode off by default.
159   0,                            // s_frame_mode off by default.
160   0,                            // film_grain_test_vector
161   0,                            // film_grain_table_filename
162   0,                            // motion_vector_unit_test
163   1,                            // CDF update mode
164   1,                            // frame order hint
165   1,                            // jnt_comp
166   1,                            // enable_ref_frame_mvs sequence level
167   1,                            // allow ref_frame_mvs frame level
168   1,                            // enable_warped_motion at sequence level
169   1,                            // allow_warped_motion at frame level
170   1,                            // superres
171 #if CONFIG_DENOISE
172   0,   // noise_level
173   32,  // noise_block_size
174 #endif
175   0,  // chroma_subsampling_x
176   0,  // chroma_subsampling_y
177 };
178 
179 struct aom_codec_alg_priv {
180   aom_codec_priv_t base;
181   aom_codec_enc_cfg_t cfg;
182   struct av1_extracfg extra_cfg;
183   AV1EncoderConfig oxcf;
184   AV1_COMP *cpi;
185   unsigned char *cx_data;
186   size_t cx_data_sz;
187   unsigned char *pending_cx_data;
188   size_t pending_cx_data_sz;
189   int pending_frame_count;
190   size_t pending_frame_sizes[8];
191   aom_image_t preview_img;
192   aom_enc_frame_flags_t next_frame_flags;
193   aom_postproc_cfg_t preview_ppcfg;
194   aom_codec_pkt_list_decl(256) pkt_list;
195   unsigned int fixed_kf_cntr;
196   // BufferPool that holds all reference frames.
197   BufferPool *buffer_pool;
198 };
199 
update_error_state(aom_codec_alg_priv_t * ctx,const struct aom_internal_error_info * error)200 static aom_codec_err_t update_error_state(
201     aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
202   const aom_codec_err_t res = error->error_code;
203 
204   if (res != AOM_CODEC_OK)
205     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
206 
207   return res;
208 }
209 
210 #undef ERROR
211 #define ERROR(str)                  \
212   do {                              \
213     ctx->base.err_detail = str;     \
214     return AOM_CODEC_INVALID_PARAM; \
215   } while (0)
216 
217 #define RANGE_CHECK(p, memb, lo, hi)                   \
218   do {                                                 \
219     if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
220       ERROR(#memb " out of range [" #lo ".." #hi "]"); \
221   } while (0)
222 
223 #define RANGE_CHECK_HI(p, memb, hi)                                     \
224   do {                                                                  \
225     if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
226   } while (0)
227 
228 #define RANGE_CHECK_BOOL(p, memb)                                     \
229   do {                                                                \
230     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
231   } while (0)
232 
validate_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg,const struct av1_extracfg * extra_cfg)233 static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
234                                        const aom_codec_enc_cfg_t *cfg,
235                                        const struct av1_extracfg *extra_cfg) {
236   RANGE_CHECK(cfg, g_w, 1, 65535);  // 16 bits available
237   RANGE_CHECK(cfg, g_h, 1, 65535);  // 16 bits available
238   RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
239   RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
240   RANGE_CHECK_HI(cfg, g_profile, MAX_PROFILES - 1);
241 
242   RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
243   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
244   RANGE_CHECK_BOOL(extra_cfg, lossless);
245   RANGE_CHECK_HI(extra_cfg, aq_mode, AQ_MODE_COUNT - 1);
246   RANGE_CHECK_HI(extra_cfg, deltaq_mode, DELTAQ_MODE_COUNT - 1);
247   RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
248   RANGE_CHECK_HI(cfg, g_threads, 64);
249   RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
250   RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
251   RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
252   RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
253   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
254   RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
255   RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
256   RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
257   RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
258   RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
259   if (extra_cfg->max_gf_interval > 0) {
260     RANGE_CHECK(extra_cfg, max_gf_interval, MAX(2, extra_cfg->min_gf_interval),
261                 (MAX_LAG_BUFFERS - 1));
262   }
263 
264   RANGE_CHECK_HI(cfg, rc_resize_mode, RESIZE_MODES - 1);
265   RANGE_CHECK(cfg, rc_resize_denominator, SCALE_NUMERATOR,
266               SCALE_NUMERATOR << 1);
267   RANGE_CHECK(cfg, rc_resize_kf_denominator, SCALE_NUMERATOR,
268               SCALE_NUMERATOR << 1);
269   RANGE_CHECK_HI(cfg, rc_superres_mode, SUPERRES_MODES - 1);
270   RANGE_CHECK(cfg, rc_superres_denominator, SCALE_NUMERATOR,
271               SCALE_NUMERATOR << 1);
272   RANGE_CHECK(cfg, rc_superres_kf_denominator, SCALE_NUMERATOR,
273               SCALE_NUMERATOR << 1);
274   RANGE_CHECK(cfg, rc_superres_qthresh, 1, 63);
275   RANGE_CHECK(cfg, rc_superres_kf_qthresh, 1, 63);
276   RANGE_CHECK_HI(extra_cfg, cdf_update_mode, 2);
277 
278   // AV1 does not support a lower bound on the keyframe interval in
279   // automatic keyframe placement mode.
280   if (cfg->kf_mode != AOM_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
281       cfg->kf_min_dist > 0)
282     ERROR(
283         "kf_min_dist not supported in auto mode, use 0 "
284         "or kf_max_dist instead.");
285 
286   RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
287   RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
288   RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
289   RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
290   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
291   RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
292               AOM_SUPERBLOCK_SIZE_DYNAMIC);
293   RANGE_CHECK_HI(cfg, large_scale_tile, 1);
294   RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);
295 
296   RANGE_CHECK_HI(extra_cfg, row_mt, 1);
297 
298   RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
299   RANGE_CHECK_HI(extra_cfg, tile_rows, 6);
300 
301   RANGE_CHECK_HI(cfg, monochrome, 1);
302 
303   if (cfg->large_scale_tile && extra_cfg->aq_mode)
304     ERROR(
305         "Adaptive quantization are not supported in large scale tile "
306         "coding.");
307 
308   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
309   RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
310   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
311   RANGE_CHECK_HI(extra_cfg, cq_level, 63);
312   RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
313   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
314   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
315 
316   // TODO(yaowu): remove this when ssim tuning is implemented for av1
317   if (extra_cfg->tuning == AOM_TUNE_SSIM)
318     ERROR("Option --tune=ssim is not currently supported in AV1.");
319 
320   if (cfg->g_pass == AOM_RC_LAST_PASS) {
321     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
322     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
323     const FIRSTPASS_STATS *stats;
324 
325     if (cfg->rc_twopass_stats_in.buf == NULL)
326       ERROR("rc_twopass_stats_in.buf not set.");
327 
328     if (cfg->rc_twopass_stats_in.sz % packet_sz)
329       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
330 
331     if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
332       ERROR("rc_twopass_stats_in requires at least two packets.");
333 
334     stats =
335         (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
336 
337     if ((int)(stats->count + 0.5) != n_packets - 1)
338       ERROR("rc_twopass_stats_in missing EOS stats packet");
339   }
340 
341   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
342       cfg->g_bit_depth > AOM_BITS_10) {
343     ERROR("Codec bit-depth 12 not supported in profile < 2");
344   }
345   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
346       cfg->g_input_bit_depth > 10) {
347     ERROR("Source bit-depth 12 not supported in profile < 2");
348   }
349 
350   RANGE_CHECK(extra_cfg, color_primaries, AOM_CICP_CP_BT_709,
351               AOM_CICP_CP_EBU_3213);  // Need to check range more precisely to
352                                       // check for reserved values?
353   RANGE_CHECK(extra_cfg, transfer_characteristics, AOM_CICP_TC_BT_709,
354               AOM_CICP_TC_HLG);
355   RANGE_CHECK(extra_cfg, matrix_coefficients, AOM_CICP_MC_IDENTITY,
356               AOM_CICP_MC_ICTCP);
357   RANGE_CHECK(extra_cfg, color_range, 0, 1);
358 
359 #if CONFIG_DIST_8X8
360   RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_DAALA_DIST);
361 #else
362   RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_SSIM);
363 #endif
364 
365   RANGE_CHECK(extra_cfg, timing_info_type, AOM_TIMING_UNSPECIFIED,
366               AOM_TIMING_DEC_MODEL);
367 
368   RANGE_CHECK(extra_cfg, film_grain_test_vector, 0, 16);
369 
370   if (extra_cfg->lossless) {
371     if (extra_cfg->aq_mode != 0)
372       ERROR("Only --aq_mode=0 can be used with --lossless=1.");
373 #if CONFIG_DIST_8X8
374     if (extra_cfg->enable_dist_8x8)
375       ERROR("dist-8x8 cannot be used with lossless compression.");
376 #endif
377   }
378 
379   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_x, 1);
380   RANGE_CHECK_HI(extra_cfg, chroma_subsampling_y, 1);
381 
382   return AOM_CODEC_OK;
383 }
384 
validate_img(aom_codec_alg_priv_t * ctx,const aom_image_t * img)385 static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
386                                     const aom_image_t *img) {
387   switch (img->fmt) {
388     case AOM_IMG_FMT_YV12:
389     case AOM_IMG_FMT_I420:
390     case AOM_IMG_FMT_I42016: break;
391     case AOM_IMG_FMT_I444:
392     case AOM_IMG_FMT_I44416:
393       if (ctx->cfg.g_profile == (unsigned int)PROFILE_0 &&
394           !ctx->cfg.monochrome) {
395         ERROR("Invalid image format. I444 images not supported in profile.");
396       }
397       break;
398     case AOM_IMG_FMT_I422:
399     case AOM_IMG_FMT_I42216:
400       if (ctx->cfg.g_profile != (unsigned int)PROFILE_2) {
401         ERROR("Invalid image format. I422 images not supported in profile.");
402       }
403       break;
404     default:
405       ERROR(
406           "Invalid image format. Only YV12, I420, I422, I444 images are "
407           "supported.");
408       break;
409   }
410 
411   if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
412     ERROR("Image size must match encoder init configuration size");
413 
414   return AOM_CODEC_OK;
415 }
416 
get_image_bps(const aom_image_t * img)417 static int get_image_bps(const aom_image_t *img) {
418   switch (img->fmt) {
419     case AOM_IMG_FMT_YV12:
420     case AOM_IMG_FMT_I420: return 12;
421     case AOM_IMG_FMT_I422: return 16;
422     case AOM_IMG_FMT_I444: return 24;
423     case AOM_IMG_FMT_I42016: return 24;
424     case AOM_IMG_FMT_I42216: return 32;
425     case AOM_IMG_FMT_I44416: return 48;
426     default: assert(0 && "Invalid image format"); break;
427   }
428   return 0;
429 }
430 
431 // Set appropriate options to disable frame super-resolution.
disable_superres(AV1EncoderConfig * const oxcf)432 static void disable_superres(AV1EncoderConfig *const oxcf) {
433   oxcf->superres_mode = SUPERRES_NONE;
434   oxcf->superres_scale_denominator = SCALE_NUMERATOR;
435   oxcf->superres_kf_scale_denominator = SCALE_NUMERATOR;
436   oxcf->superres_qthresh = 255;
437   oxcf->superres_kf_qthresh = 255;
438 }
439 
set_encoder_config(AV1EncoderConfig * oxcf,const aom_codec_enc_cfg_t * cfg,const struct av1_extracfg * extra_cfg)440 static aom_codec_err_t set_encoder_config(
441     AV1EncoderConfig *oxcf, const aom_codec_enc_cfg_t *cfg,
442     const struct av1_extracfg *extra_cfg) {
443   const int is_vbr = cfg->rc_end_usage == AOM_VBR;
444   oxcf->profile = cfg->g_profile;
445   oxcf->fwd_kf_enabled = cfg->fwd_kf_enabled;
446   oxcf->max_threads = (int)cfg->g_threads;
447   oxcf->width = cfg->g_w;
448   oxcf->height = cfg->g_h;
449   oxcf->forced_max_frame_width = cfg->g_forced_max_frame_width;
450   oxcf->forced_max_frame_height = cfg->g_forced_max_frame_height;
451   oxcf->bit_depth = cfg->g_bit_depth;
452   oxcf->input_bit_depth = cfg->g_input_bit_depth;
453   // guess a frame rate if out of whack, use 30
454   oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
455   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL ||
456       extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
457     oxcf->timing_info_present = 1;
458     oxcf->timing_info.num_units_in_display_tick = cfg->g_timebase.num;
459     oxcf->timing_info.time_scale = cfg->g_timebase.den;
460     oxcf->timing_info.num_ticks_per_picture = 1;
461   } else {
462     oxcf->timing_info_present = 0;
463   }
464   if (extra_cfg->timing_info_type == AOM_TIMING_EQUAL) {
465     oxcf->timing_info.equal_picture_interval = 1;
466     oxcf->decoder_model_info_present_flag = 0;
467     oxcf->display_model_info_present_flag = 1;
468   } else if (extra_cfg->timing_info_type == AOM_TIMING_DEC_MODEL) {
469     //    if( extra_cfg->arnr_strength > 0 )
470     //    {
471     //      printf("Only --arnr-strength=0 can currently be used with
472     //      --timing-info=model."); return AOM_CODEC_INVALID_PARAM;
473     //    }
474     //    if( extra_cfg->enable_superres)
475     //    {
476     //      printf("Only --superres-mode=0 can currently be used with
477     //      --timing-info=model."); return AOM_CODEC_INVALID_PARAM;
478     //    }
479     oxcf->buffer_model.num_units_in_decoding_tick = cfg->g_timebase.num;
480     oxcf->timing_info.equal_picture_interval = 0;
481     oxcf->decoder_model_info_present_flag = 1;
482     oxcf->buffer_removal_time_present = 1;
483     oxcf->display_model_info_present_flag = 1;
484   }
485   if (oxcf->init_framerate > 180) {
486     oxcf->init_framerate = 30;
487     oxcf->timing_info_present = 0;
488   }
489   oxcf->mode = GOOD;
490   oxcf->cfg = &cfg->cfg;
491 
492   switch (cfg->g_pass) {
493     case AOM_RC_ONE_PASS: oxcf->pass = 0; break;
494     case AOM_RC_FIRST_PASS: oxcf->pass = 1; break;
495     case AOM_RC_LAST_PASS: oxcf->pass = 2; break;
496   }
497 
498   oxcf->lag_in_frames =
499       cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
500   oxcf->rc_mode = cfg->rc_end_usage;
501 
502   // Convert target bandwidth from Kbit/s to Bit/s
503   oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
504   oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
505   oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
506   oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
507 
508   oxcf->best_allowed_q =
509       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
510   oxcf->worst_allowed_q =
511       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
512   oxcf->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
513   oxcf->fixed_q = -1;
514 
515   oxcf->enable_cdef = extra_cfg->enable_cdef;
516   oxcf->enable_restoration = extra_cfg->enable_restoration;
517   oxcf->disable_trellis_quant = extra_cfg->disable_trellis_quant;
518   oxcf->using_qm = extra_cfg->enable_qm;
519   oxcf->qm_y = extra_cfg->qm_y;
520   oxcf->qm_u = extra_cfg->qm_u;
521   oxcf->qm_v = extra_cfg->qm_v;
522   oxcf->qm_minlevel = extra_cfg->qm_min;
523   oxcf->qm_maxlevel = extra_cfg->qm_max;
524 #if CONFIG_DIST_8X8
525   oxcf->using_dist_8x8 = extra_cfg->enable_dist_8x8;
526   if (extra_cfg->tuning == AOM_TUNE_CDEF_DIST ||
527       extra_cfg->tuning == AOM_TUNE_DAALA_DIST)
528     oxcf->using_dist_8x8 = 1;
529 #endif
530   oxcf->num_tile_groups = extra_cfg->num_tg;
531   // In large-scale tile encoding mode, num_tile_groups is always 1.
532   if (cfg->large_scale_tile) oxcf->num_tile_groups = 1;
533   oxcf->mtu = extra_cfg->mtu_size;
534 
535   // FIXME(debargha): Should this be:
536   // oxcf->allow_ref_frame_mvs = extra_cfg->allow_ref_frame_mvs &
537   //                             extra_cfg->enable_order_hint ?
538   // Disallow using temporal MVs while large_scale_tile = 1.
539   oxcf->allow_ref_frame_mvs =
540       extra_cfg->allow_ref_frame_mvs && !cfg->large_scale_tile;
541   oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
542   oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
543 
544   oxcf->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
545   oxcf->resize_scale_denominator = (uint8_t)cfg->rc_resize_denominator;
546   oxcf->resize_kf_scale_denominator = (uint8_t)cfg->rc_resize_kf_denominator;
547   if (oxcf->resize_mode == RESIZE_FIXED &&
548       oxcf->resize_scale_denominator == SCALE_NUMERATOR &&
549       oxcf->resize_kf_scale_denominator == SCALE_NUMERATOR)
550     oxcf->resize_mode = RESIZE_NONE;
551 
552   if (extra_cfg->lossless || cfg->large_scale_tile) {
553     disable_superres(oxcf);
554   } else {
555     oxcf->superres_mode = (SUPERRES_MODE)cfg->rc_superres_mode;
556     oxcf->superres_scale_denominator = (uint8_t)cfg->rc_superres_denominator;
557     oxcf->superres_kf_scale_denominator =
558         (uint8_t)cfg->rc_superres_kf_denominator;
559     oxcf->superres_qthresh = av1_quantizer_to_qindex(cfg->rc_superres_qthresh);
560     oxcf->superres_kf_qthresh =
561         av1_quantizer_to_qindex(cfg->rc_superres_kf_qthresh);
562     if (oxcf->superres_mode == SUPERRES_FIXED &&
563         oxcf->superres_scale_denominator == SCALE_NUMERATOR &&
564         oxcf->superres_kf_scale_denominator == SCALE_NUMERATOR) {
565       disable_superres(oxcf);
566     }
567     if (oxcf->superres_mode == SUPERRES_QTHRESH &&
568         oxcf->superres_qthresh == 255 && oxcf->superres_kf_qthresh == 255) {
569       disable_superres(oxcf);
570     }
571   }
572 
573   oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
574   oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
575   oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
576 
577   oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
578 
579   oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
580   oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
581   oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
582 
583   oxcf->auto_key =
584       cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
585 
586   oxcf->key_freq = cfg->kf_max_dist;
587   oxcf->sframe_dist = cfg->sframe_dist;
588   oxcf->sframe_mode = cfg->sframe_mode;
589   oxcf->sframe_enabled = cfg->sframe_dist != 0;
590   oxcf->speed = extra_cfg->cpu_used;
591   oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
592   oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
593   oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
594   oxcf->sharpness = extra_cfg->sharpness;
595 
596   oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
597 
598 #if CONFIG_FP_MB_STATS
599   oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
600 #endif
601 
602   oxcf->color_primaries = extra_cfg->color_primaries;
603   oxcf->transfer_characteristics = extra_cfg->transfer_characteristics;
604   oxcf->matrix_coefficients = extra_cfg->matrix_coefficients;
605   oxcf->chroma_sample_position = extra_cfg->chroma_sample_position;
606 
607   oxcf->color_range = extra_cfg->color_range;
608   oxcf->render_width = extra_cfg->render_width;
609   oxcf->render_height = extra_cfg->render_height;
610   oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
611   // Adjust g_lag_in_frames down if not needed
612   oxcf->lag_in_frames =
613       AOMMIN(MAX_GF_INTERVAL + oxcf->arnr_max_frames / 2, oxcf->lag_in_frames);
614   oxcf->arnr_strength = extra_cfg->arnr_strength;
615   oxcf->min_gf_interval = extra_cfg->min_gf_interval;
616   oxcf->max_gf_interval = extra_cfg->max_gf_interval;
617 
618   oxcf->tuning = extra_cfg->tuning;
619   oxcf->content = extra_cfg->content;
620   oxcf->cdf_update_mode = (uint8_t)extra_cfg->cdf_update_mode;
621   oxcf->superblock_size = extra_cfg->superblock_size;
622   if (cfg->large_scale_tile) {
623     oxcf->film_grain_test_vector = 0;
624     oxcf->film_grain_table_filename = NULL;
625   } else {
626     oxcf->film_grain_test_vector = extra_cfg->film_grain_test_vector;
627     oxcf->film_grain_table_filename = extra_cfg->film_grain_table_filename;
628   }
629 #if CONFIG_DENOISE
630   oxcf->noise_level = extra_cfg->noise_level;
631   oxcf->noise_block_size = extra_cfg->noise_block_size;
632 #endif
633   oxcf->large_scale_tile = cfg->large_scale_tile;
634   oxcf->single_tile_decoding =
635       (oxcf->large_scale_tile) ? extra_cfg->single_tile_decoding : 0;
636   if (oxcf->large_scale_tile) {
637     // The superblock_size can only be AOM_SUPERBLOCK_SIZE_64X64 or
638     // AOM_SUPERBLOCK_SIZE_128X128 while oxcf->large_scale_tile = 1. If
639     // superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC, hard set it to
640     // AOM_SUPERBLOCK_SIZE_64X64(default value in large_scale_tile).
641     if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64 &&
642         extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_128X128)
643       oxcf->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
644   }
645 
646   oxcf->row_mt = extra_cfg->row_mt;
647 
648   oxcf->tile_columns = extra_cfg->tile_columns;
649   oxcf->tile_rows = extra_cfg->tile_rows;
650 
651   oxcf->monochrome = cfg->monochrome;
652   oxcf->full_still_picture_hdr = cfg->full_still_picture_hdr;
653   oxcf->enable_dual_filter = extra_cfg->use_dual_filter;
654   oxcf->enable_order_hint = extra_cfg->enable_order_hint;
655   oxcf->enable_jnt_comp =
656       extra_cfg->enable_jnt_comp & extra_cfg->enable_order_hint;
657   oxcf->enable_ref_frame_mvs =
658       extra_cfg->enable_ref_frame_mvs & extra_cfg->enable_order_hint;
659 
660   oxcf->enable_warped_motion = extra_cfg->enable_warped_motion;
661   oxcf->allow_warped_motion =
662       extra_cfg->allow_warped_motion & extra_cfg->enable_warped_motion;
663 
664   oxcf->enable_superres =
665       (oxcf->superres_mode != SUPERRES_NONE) && extra_cfg->enable_superres;
666   if (!oxcf->enable_superres) {
667     disable_superres(oxcf);
668   }
669 
670   oxcf->tile_width_count = AOMMIN(cfg->tile_width_count, MAX_TILE_COLS);
671   oxcf->tile_height_count = AOMMIN(cfg->tile_height_count, MAX_TILE_ROWS);
672   for (int i = 0; i < oxcf->tile_width_count; i++) {
673     oxcf->tile_widths[i] = AOMMAX(cfg->tile_widths[i], 1);
674   }
675   for (int i = 0; i < oxcf->tile_height_count; i++) {
676     oxcf->tile_heights[i] = AOMMAX(cfg->tile_heights[i], 1);
677   }
678   oxcf->error_resilient_mode =
679       cfg->g_error_resilient | extra_cfg->error_resilient_mode;
680   oxcf->s_frame_mode = extra_cfg->s_frame_mode;
681   oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
682   if (cfg->g_pass == AOM_RC_LAST_PASS) {
683     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
684     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
685     oxcf->limit = n_packets - 1;
686   } else {
687     oxcf->limit = cfg->g_limit;
688   }
689 
690   if (oxcf->limit == 1) {
691     // still picture mode, display model and timing is meaningless
692     oxcf->display_model_info_present_flag = 0;
693     oxcf->timing_info_present = 0;
694   }
695 
696   oxcf->aq_mode = extra_cfg->aq_mode;
697   oxcf->deltaq_mode = extra_cfg->deltaq_mode;
698 
699   oxcf->save_as_annexb = cfg->save_as_annexb;
700 
701   oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
702   oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
703 
704 #if CONFIG_REDUCED_ENCODER_BORDER
705   if (oxcf->superres_mode != SUPERRES_NONE ||
706       oxcf->resize_mode != RESIZE_NONE) {
707     warn(
708         "Superres / resize cannot be used with CONFIG_REDUCED_ENCODER_BORDER. "
709         "Disabling superres/resize.\n");
710     // return AOM_CODEC_INVALID_PARAM;
711     disable_superres(oxcf);
712     oxcf->resize_mode = RESIZE_NONE;
713     oxcf->resize_scale_denominator = SCALE_NUMERATOR;
714     oxcf->resize_kf_scale_denominator = SCALE_NUMERATOR;
715   }
716 #endif  // CONFIG_REDUCED_ENCODER_BORDER
717 
718   oxcf->chroma_subsampling_x = extra_cfg->chroma_subsampling_x;
719   oxcf->chroma_subsampling_y = extra_cfg->chroma_subsampling_y;
720 
721   return AOM_CODEC_OK;
722 }
723 
encoder_set_config(aom_codec_alg_priv_t * ctx,const aom_codec_enc_cfg_t * cfg)724 static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
725                                           const aom_codec_enc_cfg_t *cfg) {
726   aom_codec_err_t res;
727   int force_key = 0;
728 
729   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
730     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
731       ERROR("Cannot change width or height after initialization");
732     if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
733         (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
734         (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
735       force_key = 1;
736   }
737 
738   // Prevent increasing lag_in_frames. This check is stricter than it needs
739   // to be -- the limit is not increasing past the first lag_in_frames
740   // value, but we don't track the initial config, only the last successful
741   // config.
742   if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
743     ERROR("Cannot increase lag_in_frames");
744 
745   res = validate_config(ctx, cfg, &ctx->extra_cfg);
746 
747   if (res == AOM_CODEC_OK) {
748     ctx->cfg = *cfg;
749     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
750     // On profile change, request a key frame
751     force_key |= ctx->cpi->common.seq_params.profile != ctx->oxcf.profile;
752     av1_change_config(ctx->cpi, &ctx->oxcf);
753   }
754 
755   if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
756 
757   return res;
758 }
759 
encoder_get_global_headers(aom_codec_alg_priv_t * ctx)760 static aom_fixed_buf_t *encoder_get_global_headers(aom_codec_alg_priv_t *ctx) {
761   return av1_get_global_headers(ctx->cpi);
762 }
763 
ctrl_get_quantizer(aom_codec_alg_priv_t * ctx,va_list args)764 static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
765                                           va_list args) {
766   int *const arg = va_arg(args, int *);
767   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
768   *arg = av1_get_quantizer(ctx->cpi);
769   return AOM_CODEC_OK;
770 }
771 
ctrl_get_quantizer64(aom_codec_alg_priv_t * ctx,va_list args)772 static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
773                                             va_list args) {
774   int *const arg = va_arg(args, int *);
775   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
776   *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->cpi));
777   return AOM_CODEC_OK;
778 }
779 
update_extra_cfg(aom_codec_alg_priv_t * ctx,const struct av1_extracfg * extra_cfg)780 static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
781                                         const struct av1_extracfg *extra_cfg) {
782   const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
783   if (res == AOM_CODEC_OK) {
784     ctx->extra_cfg = *extra_cfg;
785     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
786     av1_change_config(ctx->cpi, &ctx->oxcf);
787   }
788   return res;
789 }
790 
ctrl_set_cpuused(aom_codec_alg_priv_t * ctx,va_list args)791 static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
792                                         va_list args) {
793   struct av1_extracfg extra_cfg = ctx->extra_cfg;
794   extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
795   return update_extra_cfg(ctx, &extra_cfg);
796 }
797 
ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t * ctx,va_list args)798 static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
799                                                     va_list args) {
800   struct av1_extracfg extra_cfg = ctx->extra_cfg;
801   extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
802   return update_extra_cfg(ctx, &extra_cfg);
803 }
804 
ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t * ctx,va_list args)805 static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
806                                                     va_list args) {
807   struct av1_extracfg extra_cfg = ctx->extra_cfg;
808   extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
809   return update_extra_cfg(ctx, &extra_cfg);
810 }
811 
ctrl_set_noise_sensitivity(aom_codec_alg_priv_t * ctx,va_list args)812 static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
813                                                   va_list args) {
814   struct av1_extracfg extra_cfg = ctx->extra_cfg;
815   extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
816   return update_extra_cfg(ctx, &extra_cfg);
817 }
818 
ctrl_set_sharpness(aom_codec_alg_priv_t * ctx,va_list args)819 static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
820                                           va_list args) {
821   struct av1_extracfg extra_cfg = ctx->extra_cfg;
822   extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
823   return update_extra_cfg(ctx, &extra_cfg);
824 }
825 
ctrl_set_static_thresh(aom_codec_alg_priv_t * ctx,va_list args)826 static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
827                                               va_list args) {
828   struct av1_extracfg extra_cfg = ctx->extra_cfg;
829   extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
830   return update_extra_cfg(ctx, &extra_cfg);
831 }
832 
ctrl_set_row_mt(aom_codec_alg_priv_t * ctx,va_list args)833 static aom_codec_err_t ctrl_set_row_mt(aom_codec_alg_priv_t *ctx,
834                                        va_list args) {
835   struct av1_extracfg extra_cfg = ctx->extra_cfg;
836   extra_cfg.row_mt = CAST(AV1E_SET_ROW_MT, args);
837   return update_extra_cfg(ctx, &extra_cfg);
838 }
839 
ctrl_set_tile_columns(aom_codec_alg_priv_t * ctx,va_list args)840 static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
841                                              va_list args) {
842   struct av1_extracfg extra_cfg = ctx->extra_cfg;
843   extra_cfg.tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
844   return update_extra_cfg(ctx, &extra_cfg);
845 }
846 
ctrl_set_tile_rows(aom_codec_alg_priv_t * ctx,va_list args)847 static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
848                                           va_list args) {
849   struct av1_extracfg extra_cfg = ctx->extra_cfg;
850   extra_cfg.tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
851   return update_extra_cfg(ctx, &extra_cfg);
852 }
853 
ctrl_set_arnr_max_frames(aom_codec_alg_priv_t * ctx,va_list args)854 static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
855                                                 va_list args) {
856   struct av1_extracfg extra_cfg = ctx->extra_cfg;
857   extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
858   return update_extra_cfg(ctx, &extra_cfg);
859 }
860 
ctrl_set_arnr_strength(aom_codec_alg_priv_t * ctx,va_list args)861 static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
862                                               va_list args) {
863   struct av1_extracfg extra_cfg = ctx->extra_cfg;
864   extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
865   return update_extra_cfg(ctx, &extra_cfg);
866 }
867 
ctrl_set_tuning(aom_codec_alg_priv_t * ctx,va_list args)868 static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
869                                        va_list args) {
870   struct av1_extracfg extra_cfg = ctx->extra_cfg;
871   extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
872   return update_extra_cfg(ctx, &extra_cfg);
873 }
874 
ctrl_set_cq_level(aom_codec_alg_priv_t * ctx,va_list args)875 static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
876                                          va_list args) {
877   struct av1_extracfg extra_cfg = ctx->extra_cfg;
878   extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
879   return update_extra_cfg(ctx, &extra_cfg);
880 }
881 
ctrl_set_rc_max_intra_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)882 static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
883     aom_codec_alg_priv_t *ctx, va_list args) {
884   struct av1_extracfg extra_cfg = ctx->extra_cfg;
885   extra_cfg.rc_max_intra_bitrate_pct =
886       CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
887   return update_extra_cfg(ctx, &extra_cfg);
888 }
889 
ctrl_set_rc_max_inter_bitrate_pct(aom_codec_alg_priv_t * ctx,va_list args)890 static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
891     aom_codec_alg_priv_t *ctx, va_list args) {
892   struct av1_extracfg extra_cfg = ctx->extra_cfg;
893   extra_cfg.rc_max_inter_bitrate_pct =
894       CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
895   return update_extra_cfg(ctx, &extra_cfg);
896 }
897 
ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t * ctx,va_list args)898 static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
899                                                     va_list args) {
900   struct av1_extracfg extra_cfg = ctx->extra_cfg;
901   extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
902   return update_extra_cfg(ctx, &extra_cfg);
903 }
904 
ctrl_set_lossless(aom_codec_alg_priv_t * ctx,va_list args)905 static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
906                                          va_list args) {
907   struct av1_extracfg extra_cfg = ctx->extra_cfg;
908   extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
909   return update_extra_cfg(ctx, &extra_cfg);
910 }
911 
ctrl_set_enable_cdef(aom_codec_alg_priv_t * ctx,va_list args)912 static aom_codec_err_t ctrl_set_enable_cdef(aom_codec_alg_priv_t *ctx,
913                                             va_list args) {
914   struct av1_extracfg extra_cfg = ctx->extra_cfg;
915   extra_cfg.enable_cdef = CAST(AV1E_SET_ENABLE_CDEF, args);
916   return update_extra_cfg(ctx, &extra_cfg);
917 }
918 
ctrl_set_enable_restoration(aom_codec_alg_priv_t * ctx,va_list args)919 static aom_codec_err_t ctrl_set_enable_restoration(aom_codec_alg_priv_t *ctx,
920                                                    va_list args) {
921   struct av1_extracfg extra_cfg = ctx->extra_cfg;
922   extra_cfg.enable_restoration = CAST(AV1E_SET_ENABLE_RESTORATION, args);
923   return update_extra_cfg(ctx, &extra_cfg);
924 }
925 
ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t * ctx,va_list args)926 static aom_codec_err_t ctrl_set_disable_trellis_quant(aom_codec_alg_priv_t *ctx,
927                                                       va_list args) {
928   struct av1_extracfg extra_cfg = ctx->extra_cfg;
929   extra_cfg.disable_trellis_quant = CAST(AV1E_SET_DISABLE_TRELLIS_QUANT, args);
930   return update_extra_cfg(ctx, &extra_cfg);
931 }
932 
ctrl_set_enable_qm(aom_codec_alg_priv_t * ctx,va_list args)933 static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
934                                           va_list args) {
935   struct av1_extracfg extra_cfg = ctx->extra_cfg;
936   extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
937   return update_extra_cfg(ctx, &extra_cfg);
938 }
ctrl_set_qm_y(aom_codec_alg_priv_t * ctx,va_list args)939 static aom_codec_err_t ctrl_set_qm_y(aom_codec_alg_priv_t *ctx, va_list args) {
940   struct av1_extracfg extra_cfg = ctx->extra_cfg;
941   extra_cfg.qm_y = CAST(AV1E_SET_QM_Y, args);
942   return update_extra_cfg(ctx, &extra_cfg);
943 }
ctrl_set_qm_u(aom_codec_alg_priv_t * ctx,va_list args)944 static aom_codec_err_t ctrl_set_qm_u(aom_codec_alg_priv_t *ctx, va_list args) {
945   struct av1_extracfg extra_cfg = ctx->extra_cfg;
946   extra_cfg.qm_u = CAST(AV1E_SET_QM_U, args);
947   return update_extra_cfg(ctx, &extra_cfg);
948 }
ctrl_set_qm_v(aom_codec_alg_priv_t * ctx,va_list args)949 static aom_codec_err_t ctrl_set_qm_v(aom_codec_alg_priv_t *ctx, va_list args) {
950   struct av1_extracfg extra_cfg = ctx->extra_cfg;
951   extra_cfg.qm_v = CAST(AV1E_SET_QM_V, args);
952   return update_extra_cfg(ctx, &extra_cfg);
953 }
ctrl_set_qm_min(aom_codec_alg_priv_t * ctx,va_list args)954 static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
955                                        va_list args) {
956   struct av1_extracfg extra_cfg = ctx->extra_cfg;
957   extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
958   return update_extra_cfg(ctx, &extra_cfg);
959 }
960 
ctrl_set_qm_max(aom_codec_alg_priv_t * ctx,va_list args)961 static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
962                                        va_list args) {
963   struct av1_extracfg extra_cfg = ctx->extra_cfg;
964   extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
965   return update_extra_cfg(ctx, &extra_cfg);
966 }
967 #if CONFIG_DIST_8X8
ctrl_set_enable_dist_8x8(aom_codec_alg_priv_t * ctx,va_list args)968 static aom_codec_err_t ctrl_set_enable_dist_8x8(aom_codec_alg_priv_t *ctx,
969                                                 va_list args) {
970   struct av1_extracfg extra_cfg = ctx->extra_cfg;
971   extra_cfg.enable_dist_8x8 = CAST(AV1E_SET_ENABLE_DIST_8X8, args);
972   return update_extra_cfg(ctx, &extra_cfg);
973 }
974 #endif
ctrl_set_num_tg(aom_codec_alg_priv_t * ctx,va_list args)975 static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
976                                        va_list args) {
977   struct av1_extracfg extra_cfg = ctx->extra_cfg;
978   extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
979   return update_extra_cfg(ctx, &extra_cfg);
980 }
981 
ctrl_set_mtu(aom_codec_alg_priv_t * ctx,va_list args)982 static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
983   struct av1_extracfg extra_cfg = ctx->extra_cfg;
984   extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
985   return update_extra_cfg(ctx, &extra_cfg);
986 }
ctrl_set_timing_info_type(aom_codec_alg_priv_t * ctx,va_list args)987 static aom_codec_err_t ctrl_set_timing_info_type(aom_codec_alg_priv_t *ctx,
988                                                  va_list args) {
989   struct av1_extracfg extra_cfg = ctx->extra_cfg;
990   extra_cfg.timing_info_type = CAST(AV1E_SET_TIMING_INFO_TYPE, args);
991   return update_extra_cfg(ctx, &extra_cfg);
992 }
993 
ctrl_set_enable_df(aom_codec_alg_priv_t * ctx,va_list args)994 static aom_codec_err_t ctrl_set_enable_df(aom_codec_alg_priv_t *ctx,
995                                           va_list args) {
996   struct av1_extracfg extra_cfg = ctx->extra_cfg;
997   extra_cfg.use_dual_filter = CAST(AV1E_SET_ENABLE_DF, args);
998   return update_extra_cfg(ctx, &extra_cfg);
999 }
1000 
ctrl_set_enable_order_hint(aom_codec_alg_priv_t * ctx,va_list args)1001 static aom_codec_err_t ctrl_set_enable_order_hint(aom_codec_alg_priv_t *ctx,
1002                                                   va_list args) {
1003   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1004   extra_cfg.enable_order_hint = CAST(AV1E_SET_ENABLE_ORDER_HINT, args);
1005   return update_extra_cfg(ctx, &extra_cfg);
1006 }
1007 
ctrl_set_enable_jnt_comp(aom_codec_alg_priv_t * ctx,va_list args)1008 static aom_codec_err_t ctrl_set_enable_jnt_comp(aom_codec_alg_priv_t *ctx,
1009                                                 va_list args) {
1010   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1011   extra_cfg.enable_jnt_comp = CAST(AV1E_SET_ENABLE_JNT_COMP, args);
1012   return update_extra_cfg(ctx, &extra_cfg);
1013 }
1014 
ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)1015 static aom_codec_err_t ctrl_set_enable_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
1016                                                      va_list args) {
1017   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1018   extra_cfg.enable_ref_frame_mvs = CAST(AV1E_SET_ENABLE_REF_FRAME_MVS, args);
1019   return update_extra_cfg(ctx, &extra_cfg);
1020 }
1021 
ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t * ctx,va_list args)1022 static aom_codec_err_t ctrl_set_allow_ref_frame_mvs(aom_codec_alg_priv_t *ctx,
1023                                                     va_list args) {
1024   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1025   extra_cfg.allow_ref_frame_mvs = CAST(AV1E_SET_ALLOW_REF_FRAME_MVS, args);
1026   return update_extra_cfg(ctx, &extra_cfg);
1027 }
1028 
ctrl_set_enable_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)1029 static aom_codec_err_t ctrl_set_enable_warped_motion(aom_codec_alg_priv_t *ctx,
1030                                                      va_list args) {
1031   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1032   extra_cfg.enable_warped_motion = CAST(AV1E_SET_ENABLE_WARPED_MOTION, args);
1033   return update_extra_cfg(ctx, &extra_cfg);
1034 }
1035 
ctrl_set_allow_warped_motion(aom_codec_alg_priv_t * ctx,va_list args)1036 static aom_codec_err_t ctrl_set_allow_warped_motion(aom_codec_alg_priv_t *ctx,
1037                                                     va_list args) {
1038   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1039   extra_cfg.allow_warped_motion = CAST(AV1E_SET_ALLOW_WARPED_MOTION, args);
1040   return update_extra_cfg(ctx, &extra_cfg);
1041 }
1042 
ctrl_set_enable_superres(aom_codec_alg_priv_t * ctx,va_list args)1043 static aom_codec_err_t ctrl_set_enable_superres(aom_codec_alg_priv_t *ctx,
1044                                                 va_list args) {
1045   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1046   extra_cfg.enable_superres = CAST(AV1E_SET_ENABLE_SUPERRES, args);
1047   return update_extra_cfg(ctx, &extra_cfg);
1048 }
1049 
ctrl_set_error_resilient_mode(aom_codec_alg_priv_t * ctx,va_list args)1050 static aom_codec_err_t ctrl_set_error_resilient_mode(aom_codec_alg_priv_t *ctx,
1051                                                      va_list args) {
1052   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1053   extra_cfg.error_resilient_mode = CAST(AV1E_SET_ERROR_RESILIENT_MODE, args);
1054   return update_extra_cfg(ctx, &extra_cfg);
1055 }
1056 
ctrl_set_s_frame_mode(aom_codec_alg_priv_t * ctx,va_list args)1057 static aom_codec_err_t ctrl_set_s_frame_mode(aom_codec_alg_priv_t *ctx,
1058                                              va_list args) {
1059   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1060   extra_cfg.s_frame_mode = CAST(AV1E_SET_S_FRAME_MODE, args);
1061   return update_extra_cfg(ctx, &extra_cfg);
1062 }
1063 
ctrl_set_frame_parallel_decoding_mode(aom_codec_alg_priv_t * ctx,va_list args)1064 static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
1065     aom_codec_alg_priv_t *ctx, va_list args) {
1066   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1067   extra_cfg.frame_parallel_decoding_mode =
1068       CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
1069   return update_extra_cfg(ctx, &extra_cfg);
1070 }
1071 
ctrl_set_single_tile_decoding(aom_codec_alg_priv_t * ctx,va_list args)1072 static aom_codec_err_t ctrl_set_single_tile_decoding(aom_codec_alg_priv_t *ctx,
1073                                                      va_list args) {
1074   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1075   extra_cfg.single_tile_decoding = CAST(AV1E_SET_SINGLE_TILE_DECODING, args);
1076   return update_extra_cfg(ctx, &extra_cfg);
1077 }
1078 
ctrl_set_aq_mode(aom_codec_alg_priv_t * ctx,va_list args)1079 static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
1080                                         va_list args) {
1081   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1082   extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
1083   return update_extra_cfg(ctx, &extra_cfg);
1084 }
1085 
ctrl_set_film_grain_test_vector(aom_codec_alg_priv_t * ctx,va_list args)1086 static aom_codec_err_t ctrl_set_film_grain_test_vector(
1087     aom_codec_alg_priv_t *ctx, va_list args) {
1088   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1089   extra_cfg.film_grain_test_vector =
1090       CAST(AV1E_SET_FILM_GRAIN_TEST_VECTOR, args);
1091   return update_extra_cfg(ctx, &extra_cfg);
1092 }
1093 
ctrl_set_film_grain_table(aom_codec_alg_priv_t * ctx,va_list args)1094 static aom_codec_err_t ctrl_set_film_grain_table(aom_codec_alg_priv_t *ctx,
1095                                                  va_list args) {
1096   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1097   extra_cfg.film_grain_table_filename = CAST(AV1E_SET_FILM_GRAIN_TABLE, args);
1098   return update_extra_cfg(ctx, &extra_cfg);
1099 }
1100 
1101 #if CONFIG_DENOISE
ctrl_set_denoise_noise_level(aom_codec_alg_priv_t * ctx,va_list args)1102 static aom_codec_err_t ctrl_set_denoise_noise_level(aom_codec_alg_priv_t *ctx,
1103                                                     va_list args) {
1104   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1105   extra_cfg.noise_level =
1106       ((float)CAST(AV1E_SET_DENOISE_NOISE_LEVEL, args)) / 10.0f;
1107   return update_extra_cfg(ctx, &extra_cfg);
1108 }
1109 
ctrl_set_denoise_block_size(aom_codec_alg_priv_t * ctx,va_list args)1110 static aom_codec_err_t ctrl_set_denoise_block_size(aom_codec_alg_priv_t *ctx,
1111                                                    va_list args) {
1112   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1113   extra_cfg.noise_block_size = CAST(AV1E_SET_DENOISE_BLOCK_SIZE, args);
1114   return update_extra_cfg(ctx, &extra_cfg);
1115 }
1116 #endif
1117 
ctrl_set_deltaq_mode(aom_codec_alg_priv_t * ctx,va_list args)1118 static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
1119                                             va_list args) {
1120   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1121   extra_cfg.deltaq_mode = CAST(AV1E_SET_DELTAQ_MODE, args);
1122   return update_extra_cfg(ctx, &extra_cfg);
1123 }
1124 
ctrl_set_min_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1125 static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
1126                                                 va_list args) {
1127   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1128   extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
1129   return update_extra_cfg(ctx, &extra_cfg);
1130 }
1131 
ctrl_set_max_gf_interval(aom_codec_alg_priv_t * ctx,va_list args)1132 static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
1133                                                 va_list args) {
1134   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1135   extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
1136   return update_extra_cfg(ctx, &extra_cfg);
1137 }
1138 
ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t * ctx,va_list args)1139 static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
1140                                                      va_list args) {
1141   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1142   extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
1143   return update_extra_cfg(ctx, &extra_cfg);
1144 }
1145 
ctrl_enable_motion_vector_unit_test(aom_codec_alg_priv_t * ctx,va_list args)1146 static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
1147     aom_codec_alg_priv_t *ctx, va_list args) {
1148   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1149   extra_cfg.motion_vector_unit_test =
1150       CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
1151   return update_extra_cfg(ctx, &extra_cfg);
1152 }
1153 
encoder_init(aom_codec_ctx_t * ctx,aom_codec_priv_enc_mr_cfg_t * data)1154 static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx,
1155                                     aom_codec_priv_enc_mr_cfg_t *data) {
1156   aom_codec_err_t res = AOM_CODEC_OK;
1157   (void)data;
1158 
1159   if (ctx->priv == NULL) {
1160     aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
1161     if (priv == NULL) return AOM_CODEC_MEM_ERROR;
1162 
1163     ctx->priv = (aom_codec_priv_t *)priv;
1164     ctx->priv->init_flags = ctx->init_flags;
1165     ctx->priv->enc.total_encoders = 1;
1166     priv->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
1167     if (priv->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
1168 
1169 #if CONFIG_MULTITHREAD
1170     if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
1171       return AOM_CODEC_MEM_ERROR;
1172     }
1173 #endif
1174 
1175     if (ctx->config.enc) {
1176       // Update the reference to the config structure to an internal copy.
1177       priv->cfg = *ctx->config.enc;
1178       ctx->config.enc = &priv->cfg;
1179     }
1180 
1181     priv->extra_cfg = default_extra_cfg;
1182     aom_once(av1_initialize_enc);
1183 
1184     res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
1185 
1186     if (res == AOM_CODEC_OK) {
1187       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
1188       priv->oxcf.use_highbitdepth =
1189           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
1190       priv->cpi = av1_create_compressor(&priv->oxcf, priv->buffer_pool);
1191       if (priv->cpi == NULL)
1192         res = AOM_CODEC_MEM_ERROR;
1193       else
1194         priv->cpi->output_pkt_list = &priv->pkt_list.head;
1195     }
1196   }
1197 
1198   return res;
1199 }
1200 
encoder_destroy(aom_codec_alg_priv_t * ctx)1201 static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
1202   free(ctx->cx_data);
1203   av1_remove_compressor(ctx->cpi);
1204 #if CONFIG_MULTITHREAD
1205   pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
1206 #endif
1207   aom_free(ctx->buffer_pool);
1208   aom_free(ctx);
1209   return AOM_CODEC_OK;
1210 }
1211 
get_frame_pkt_flags(const AV1_COMP * cpi,unsigned int lib_flags)1212 static aom_codec_frame_flags_t get_frame_pkt_flags(const AV1_COMP *cpi,
1213                                                    unsigned int lib_flags) {
1214   aom_codec_frame_flags_t flags = lib_flags << 16;
1215 
1216   if (lib_flags & FRAMEFLAGS_KEY) flags |= AOM_FRAME_IS_KEY;
1217 
1218   if (cpi->droppable) flags |= AOM_FRAME_IS_DROPPABLE;
1219 
1220   return flags;
1221 }
1222 
encoder_encode(aom_codec_alg_priv_t * ctx,const aom_image_t * img,aom_codec_pts_t pts,unsigned long duration,aom_enc_frame_flags_t enc_flags)1223 static aom_codec_err_t encoder_encode(aom_codec_alg_priv_t *ctx,
1224                                       const aom_image_t *img,
1225                                       aom_codec_pts_t pts,
1226                                       unsigned long duration,
1227                                       aom_enc_frame_flags_t enc_flags) {
1228   const size_t kMinCompressedSize = 8192;
1229   volatile aom_codec_err_t res = AOM_CODEC_OK;
1230   AV1_COMP *const cpi = ctx->cpi;
1231   const aom_rational_t *const timebase = &ctx->cfg.g_timebase;
1232 
1233   if (cpi == NULL) return AOM_CODEC_INVALID_PARAM;
1234 
1235   if (img != NULL) {
1236     res = validate_img(ctx, img);
1237     // TODO(jzern) the checks related to cpi's validity should be treated as a
1238     // failure condition, encoder setup is done fully in init() currently.
1239     if (res == AOM_CODEC_OK) {
1240       size_t data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
1241                        ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img);
1242       if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
1243       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1244         ctx->cx_data_sz = data_sz;
1245         free(ctx->cx_data);
1246         ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1247         if (ctx->cx_data == NULL) {
1248           return AOM_CODEC_MEM_ERROR;
1249         }
1250       }
1251     }
1252   }
1253 
1254   if (ctx->oxcf.mode != GOOD) {
1255     ctx->oxcf.mode = GOOD;
1256     av1_change_config(ctx->cpi, &ctx->oxcf);
1257   }
1258 
1259   aom_codec_pkt_list_init(&ctx->pkt_list);
1260 
1261   volatile aom_enc_frame_flags_t flags = enc_flags;
1262 
1263   // The jmp_buf is valid only for the duration of the function that calls
1264   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1265   // before it returns.
1266   if (setjmp(cpi->common.error.jmp)) {
1267     cpi->common.error.setjmp = 0;
1268     res = update_error_state(ctx, &cpi->common.error);
1269     aom_clear_system_state();
1270     return res;
1271   }
1272   cpi->common.error.setjmp = 1;
1273 
1274   // Note(yunqing): While applying encoding flags, always start from enabling
1275   // all, and then modifying according to the flags. Previous frame's flags are
1276   // overwritten.
1277   av1_apply_encoding_flags(cpi, flags);
1278 
1279   // Handle fixed keyframe intervals
1280   if (ctx->cfg.kf_mode == AOM_KF_AUTO &&
1281       ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1282     if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1283       flags |= AOM_EFLAG_FORCE_KF;
1284       ctx->fixed_kf_cntr = 1;
1285     }
1286   }
1287 
1288   if (res == AOM_CODEC_OK) {
1289     int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
1290     int64_t dst_end_time_stamp =
1291         timebase_units_to_ticks(timebase, pts + duration);
1292 
1293     // Set up internal flags
1294     if (ctx->base.init_flags & AOM_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
1295 
1296     if (img != NULL) {
1297       YV12_BUFFER_CONFIG sd;
1298       res = image2yuvconfig(img, &sd);
1299 
1300       // Store the original flags in to the frame buffer. Will extract the
1301       // key frame flag when we actually encode this frame.
1302       if (av1_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1303                                 dst_time_stamp, dst_end_time_stamp)) {
1304         res = update_error_state(ctx, &cpi->common.error);
1305       }
1306       ctx->next_frame_flags = 0;
1307     }
1308 
1309     unsigned char *cx_data = ctx->cx_data;
1310     size_t cx_data_sz = ctx->cx_data_sz;
1311 
1312     /* Any pending invisible frames? */
1313     if (ctx->pending_cx_data) {
1314       memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1315       ctx->pending_cx_data = cx_data;
1316       cx_data += ctx->pending_cx_data_sz;
1317       cx_data_sz -= ctx->pending_cx_data_sz;
1318 
1319       /* TODO: this is a minimal check, the underlying codec doesn't respect
1320        * the buffer size anyway.
1321        */
1322       if (cx_data_sz < ctx->cx_data_sz / 2) {
1323         aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
1324                            "Compressed data buffer too small");
1325       }
1326     }
1327 
1328     size_t frame_size = 0;
1329     unsigned int lib_flags = 0;
1330     int is_frame_visible = 0;
1331     int index_size = 0;
1332     // invisible frames get packed with the next visible frame
1333     while (cx_data_sz - index_size >= ctx->cx_data_sz / 2 &&
1334            !is_frame_visible &&
1335            -1 != av1_get_compressed_data(cpi, &lib_flags, &frame_size, cx_data,
1336                                          &dst_time_stamp, &dst_end_time_stamp,
1337                                          !img, timebase)) {
1338       if (cpi->common.seq_params.frame_id_numbers_present_flag) {
1339         if (cpi->common.invalid_delta_frame_id_minus_1) {
1340           aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
1341                              "Invalid delta_frame_id_minus_1");
1342         }
1343       }
1344       cpi->seq_params_locked = 1;
1345       if (frame_size) {
1346         if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
1347 
1348         const int write_temporal_delimiter =
1349             !cpi->common.spatial_layer_id && !ctx->pending_frame_count;
1350 
1351         if (write_temporal_delimiter) {
1352           uint32_t obu_header_size = 1;
1353           const uint32_t obu_payload_size = 0;
1354           const size_t length_field_size =
1355               aom_uleb_size_in_bytes(obu_payload_size);
1356 
1357           if (ctx->pending_cx_data) {
1358             const size_t move_offset = length_field_size + 1;
1359             memmove(ctx->pending_cx_data + move_offset, ctx->pending_cx_data,
1360                     frame_size);
1361           }
1362           const uint32_t obu_header_offset = 0;
1363           obu_header_size = write_obu_header(
1364               OBU_TEMPORAL_DELIMITER, 0,
1365               (uint8_t *)(ctx->pending_cx_data + obu_header_offset));
1366 
1367           // OBUs are preceded/succeeded by an unsigned leb128 coded integer.
1368           if (write_uleb_obu_size(obu_header_size, obu_payload_size,
1369                                   ctx->pending_cx_data) != AOM_CODEC_OK) {
1370             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
1371           }
1372 
1373           frame_size += obu_header_size + obu_payload_size + length_field_size;
1374         }
1375 
1376         if (ctx->oxcf.save_as_annexb) {
1377           size_t curr_frame_size = frame_size;
1378           if (av1_convert_sect5obus_to_annexb(cx_data, &curr_frame_size) !=
1379               AOM_CODEC_OK) {
1380             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
1381           }
1382           frame_size = curr_frame_size;
1383 
1384           // B_PRIME (add frame size)
1385           const size_t length_field_size = aom_uleb_size_in_bytes(frame_size);
1386           if (ctx->pending_cx_data) {
1387             const size_t move_offset = length_field_size;
1388             memmove(cx_data + move_offset, cx_data, frame_size);
1389           }
1390           if (write_uleb_obu_size(0, (uint32_t)frame_size, cx_data) !=
1391               AOM_CODEC_OK) {
1392             aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
1393           }
1394           frame_size += length_field_size;
1395         }
1396 
1397         ctx->pending_frame_sizes[ctx->pending_frame_count++] = frame_size;
1398         ctx->pending_cx_data_sz += frame_size;
1399 
1400         cx_data += frame_size;
1401         cx_data_sz -= frame_size;
1402 
1403         index_size = MAG_SIZE * (ctx->pending_frame_count - 1) + 2;
1404 
1405         is_frame_visible = cpi->common.show_frame;
1406       }
1407     }
1408     if (is_frame_visible) {
1409       // Add the frame packet to the list of returned packets.
1410       aom_codec_cx_pkt_t pkt;
1411 
1412       if (ctx->oxcf.save_as_annexb) {
1413         //  B_PRIME (add TU size)
1414         size_t tu_size = ctx->pending_cx_data_sz;
1415         const size_t length_field_size = aom_uleb_size_in_bytes(tu_size);
1416         if (ctx->pending_cx_data) {
1417           const size_t move_offset = length_field_size;
1418           memmove(ctx->pending_cx_data + move_offset, ctx->pending_cx_data,
1419                   tu_size);
1420         }
1421         if (write_uleb_obu_size(0, (uint32_t)tu_size, ctx->pending_cx_data) !=
1422             AOM_CODEC_OK) {
1423           aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR, NULL);
1424         }
1425         ctx->pending_cx_data_sz += length_field_size;
1426       }
1427 
1428       pkt.kind = AOM_CODEC_CX_FRAME_PKT;
1429 
1430       pkt.data.frame.buf = ctx->pending_cx_data;
1431       pkt.data.frame.sz = ctx->pending_cx_data_sz;
1432       pkt.data.frame.partition_id = -1;
1433       pkt.data.frame.vis_frame_size = frame_size;
1434 
1435       pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
1436       pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1437       pkt.data.frame.duration = (uint32_t)ticks_to_timebase_units(
1438           timebase, dst_end_time_stamp - dst_time_stamp);
1439 
1440       aom_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1441 
1442       ctx->pending_cx_data = NULL;
1443       ctx->pending_cx_data_sz = 0;
1444       ctx->pending_frame_count = 0;
1445     }
1446   }
1447 
1448   cpi->common.error.setjmp = 0;
1449   return res;
1450 }
1451 
encoder_get_cxdata(aom_codec_alg_priv_t * ctx,aom_codec_iter_t * iter)1452 static const aom_codec_cx_pkt_t *encoder_get_cxdata(aom_codec_alg_priv_t *ctx,
1453                                                     aom_codec_iter_t *iter) {
1454   return aom_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1455 }
1456 
ctrl_set_reference(aom_codec_alg_priv_t * ctx,va_list args)1457 static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
1458                                           va_list args) {
1459   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
1460 
1461   if (frame != NULL) {
1462     YV12_BUFFER_CONFIG sd;
1463 
1464     image2yuvconfig(&frame->img, &sd);
1465     av1_set_reference_enc(ctx->cpi, frame->idx, &sd);
1466     return AOM_CODEC_OK;
1467   } else {
1468     return AOM_CODEC_INVALID_PARAM;
1469   }
1470 }
1471 
ctrl_copy_reference(aom_codec_alg_priv_t * ctx,va_list args)1472 static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
1473                                            va_list args) {
1474   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
1475 
1476   if (frame != NULL) {
1477     YV12_BUFFER_CONFIG sd;
1478 
1479     image2yuvconfig(&frame->img, &sd);
1480     av1_copy_reference_enc(ctx->cpi, frame->idx, &sd);
1481     return AOM_CODEC_OK;
1482   } else {
1483     return AOM_CODEC_INVALID_PARAM;
1484   }
1485 }
1486 
ctrl_get_reference(aom_codec_alg_priv_t * ctx,va_list args)1487 static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
1488                                           va_list args) {
1489   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
1490 
1491   if (frame != NULL) {
1492     YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
1493     if (fb == NULL) return AOM_CODEC_ERROR;
1494 
1495     yuvconfig2image(&frame->img, fb, NULL);
1496     return AOM_CODEC_OK;
1497   } else {
1498     return AOM_CODEC_INVALID_PARAM;
1499   }
1500 }
1501 
ctrl_get_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)1502 static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
1503                                                 va_list args) {
1504   aom_image_t *const new_img = va_arg(args, aom_image_t *);
1505 
1506   if (new_img != NULL) {
1507     YV12_BUFFER_CONFIG new_frame;
1508 
1509     if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
1510       yuvconfig2image(new_img, &new_frame, NULL);
1511       return AOM_CODEC_OK;
1512     } else {
1513       return AOM_CODEC_ERROR;
1514     }
1515   } else {
1516     return AOM_CODEC_INVALID_PARAM;
1517   }
1518 }
1519 
ctrl_copy_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)1520 static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
1521                                                  va_list args) {
1522   aom_image_t *const new_img = va_arg(args, aom_image_t *);
1523 
1524   if (new_img != NULL) {
1525     YV12_BUFFER_CONFIG new_frame;
1526 
1527     if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
1528       YV12_BUFFER_CONFIG sd;
1529       image2yuvconfig(new_img, &sd);
1530       return av1_copy_new_frame_enc(&ctx->cpi->common, &new_frame, &sd);
1531     } else {
1532       return AOM_CODEC_ERROR;
1533     }
1534   } else {
1535     return AOM_CODEC_INVALID_PARAM;
1536   }
1537 }
1538 
ctrl_set_previewpp(aom_codec_alg_priv_t * ctx,va_list args)1539 static aom_codec_err_t ctrl_set_previewpp(aom_codec_alg_priv_t *ctx,
1540                                           va_list args) {
1541   (void)ctx;
1542   (void)args;
1543   return AOM_CODEC_INCAPABLE;
1544 }
1545 
encoder_get_preview(aom_codec_alg_priv_t * ctx)1546 static aom_image_t *encoder_get_preview(aom_codec_alg_priv_t *ctx) {
1547   YV12_BUFFER_CONFIG sd;
1548 
1549   if (av1_get_preview_raw_frame(ctx->cpi, &sd) == 0) {
1550     yuvconfig2image(&ctx->preview_img, &sd, NULL);
1551     return &ctx->preview_img;
1552   } else {
1553     return NULL;
1554   }
1555 }
1556 
ctrl_use_reference(aom_codec_alg_priv_t * ctx,va_list args)1557 static aom_codec_err_t ctrl_use_reference(aom_codec_alg_priv_t *ctx,
1558                                           va_list args) {
1559   const int reference_flag = va_arg(args, int);
1560 
1561   av1_use_as_reference(ctx->cpi, reference_flag);
1562   return AOM_CODEC_OK;
1563 }
1564 
ctrl_set_roi_map(aom_codec_alg_priv_t * ctx,va_list args)1565 static aom_codec_err_t ctrl_set_roi_map(aom_codec_alg_priv_t *ctx,
1566                                         va_list args) {
1567   (void)ctx;
1568   (void)args;
1569 
1570   // TODO(yaowu): Need to re-implement and test for AV1.
1571   return AOM_CODEC_INVALID_PARAM;
1572 }
1573 
ctrl_set_active_map(aom_codec_alg_priv_t * ctx,va_list args)1574 static aom_codec_err_t ctrl_set_active_map(aom_codec_alg_priv_t *ctx,
1575                                            va_list args) {
1576   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
1577 
1578   if (map) {
1579     if (!av1_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
1580                             (int)map->cols))
1581       return AOM_CODEC_OK;
1582     else
1583       return AOM_CODEC_INVALID_PARAM;
1584   } else {
1585     return AOM_CODEC_INVALID_PARAM;
1586   }
1587 }
1588 
ctrl_get_active_map(aom_codec_alg_priv_t * ctx,va_list args)1589 static aom_codec_err_t ctrl_get_active_map(aom_codec_alg_priv_t *ctx,
1590                                            va_list args) {
1591   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
1592 
1593   if (map) {
1594     if (!av1_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
1595                             (int)map->cols))
1596       return AOM_CODEC_OK;
1597     else
1598       return AOM_CODEC_INVALID_PARAM;
1599   } else {
1600     return AOM_CODEC_INVALID_PARAM;
1601   }
1602 }
1603 
ctrl_set_scale_mode(aom_codec_alg_priv_t * ctx,va_list args)1604 static aom_codec_err_t ctrl_set_scale_mode(aom_codec_alg_priv_t *ctx,
1605                                            va_list args) {
1606   aom_scaling_mode_t *const mode = va_arg(args, aom_scaling_mode_t *);
1607 
1608   if (mode) {
1609     const int res =
1610         av1_set_internal_size(ctx->cpi, (AOM_SCALING)mode->h_scaling_mode,
1611                               (AOM_SCALING)mode->v_scaling_mode);
1612     return (res == 0) ? AOM_CODEC_OK : AOM_CODEC_INVALID_PARAM;
1613   } else {
1614     return AOM_CODEC_INVALID_PARAM;
1615   }
1616 }
1617 
ctrl_set_spatial_layer_id(aom_codec_alg_priv_t * ctx,va_list args)1618 static aom_codec_err_t ctrl_set_spatial_layer_id(aom_codec_alg_priv_t *ctx,
1619                                                  va_list args) {
1620   const int spatial_layer_id = va_arg(args, int);
1621   if (spatial_layer_id > MAX_NUM_ENHANCEMENT_LAYERS)
1622     return AOM_CODEC_INVALID_PARAM;
1623   ctx->cpi->common.spatial_layer_id = spatial_layer_id;
1624   return AOM_CODEC_OK;
1625 }
1626 
ctrl_set_number_spatial_layers(aom_codec_alg_priv_t * ctx,va_list args)1627 static aom_codec_err_t ctrl_set_number_spatial_layers(aom_codec_alg_priv_t *ctx,
1628                                                       va_list args) {
1629   const int number_spatial_layers = va_arg(args, int);
1630   if (number_spatial_layers > MAX_NUM_ENHANCEMENT_LAYERS)
1631     return AOM_CODEC_INVALID_PARAM;
1632   ctx->cpi->common.number_spatial_layers = number_spatial_layers;
1633   return AOM_CODEC_OK;
1634 }
1635 
ctrl_set_tune_content(aom_codec_alg_priv_t * ctx,va_list args)1636 static aom_codec_err_t ctrl_set_tune_content(aom_codec_alg_priv_t *ctx,
1637                                              va_list args) {
1638   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1639   extra_cfg.content = CAST(AV1E_SET_TUNE_CONTENT, args);
1640   return update_extra_cfg(ctx, &extra_cfg);
1641 }
1642 
ctrl_set_cdf_update_mode(aom_codec_alg_priv_t * ctx,va_list args)1643 static aom_codec_err_t ctrl_set_cdf_update_mode(aom_codec_alg_priv_t *ctx,
1644                                                 va_list args) {
1645   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1646   extra_cfg.cdf_update_mode = CAST(AV1E_SET_CDF_UPDATE_MODE, args);
1647   return update_extra_cfg(ctx, &extra_cfg);
1648 }
1649 
ctrl_set_color_primaries(aom_codec_alg_priv_t * ctx,va_list args)1650 static aom_codec_err_t ctrl_set_color_primaries(aom_codec_alg_priv_t *ctx,
1651                                                 va_list args) {
1652   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1653   extra_cfg.color_primaries = CAST(AV1E_SET_COLOR_PRIMARIES, args);
1654   return update_extra_cfg(ctx, &extra_cfg);
1655 }
1656 
ctrl_set_transfer_characteristics(aom_codec_alg_priv_t * ctx,va_list args)1657 static aom_codec_err_t ctrl_set_transfer_characteristics(
1658     aom_codec_alg_priv_t *ctx, va_list args) {
1659   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1660   extra_cfg.transfer_characteristics =
1661       CAST(AV1E_SET_TRANSFER_CHARACTERISTICS, args);
1662   return update_extra_cfg(ctx, &extra_cfg);
1663 }
1664 
ctrl_set_matrix_coefficients(aom_codec_alg_priv_t * ctx,va_list args)1665 static aom_codec_err_t ctrl_set_matrix_coefficients(aom_codec_alg_priv_t *ctx,
1666                                                     va_list args) {
1667   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1668   extra_cfg.matrix_coefficients = CAST(AV1E_SET_MATRIX_COEFFICIENTS, args);
1669   return update_extra_cfg(ctx, &extra_cfg);
1670 }
1671 
ctrl_set_chroma_sample_position(aom_codec_alg_priv_t * ctx,va_list args)1672 static aom_codec_err_t ctrl_set_chroma_sample_position(
1673     aom_codec_alg_priv_t *ctx, va_list args) {
1674   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1675   extra_cfg.chroma_sample_position =
1676       CAST(AV1E_SET_CHROMA_SAMPLE_POSITION, args);
1677   return update_extra_cfg(ctx, &extra_cfg);
1678 }
1679 
ctrl_set_color_range(aom_codec_alg_priv_t * ctx,va_list args)1680 static aom_codec_err_t ctrl_set_color_range(aom_codec_alg_priv_t *ctx,
1681                                             va_list args) {
1682   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1683   extra_cfg.color_range = CAST(AV1E_SET_COLOR_RANGE, args);
1684   return update_extra_cfg(ctx, &extra_cfg);
1685 }
1686 
ctrl_set_render_size(aom_codec_alg_priv_t * ctx,va_list args)1687 static aom_codec_err_t ctrl_set_render_size(aom_codec_alg_priv_t *ctx,
1688                                             va_list args) {
1689   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1690   int *const render_size = va_arg(args, int *);
1691   extra_cfg.render_width = render_size[0];
1692   extra_cfg.render_height = render_size[1];
1693   return update_extra_cfg(ctx, &extra_cfg);
1694 }
1695 
ctrl_set_superblock_size(aom_codec_alg_priv_t * ctx,va_list args)1696 static aom_codec_err_t ctrl_set_superblock_size(aom_codec_alg_priv_t *ctx,
1697                                                 va_list args) {
1698   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1699   extra_cfg.superblock_size = CAST(AV1E_SET_SUPERBLOCK_SIZE, args);
1700   return update_extra_cfg(ctx, &extra_cfg);
1701 }
1702 
ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t * ctx,va_list args)1703 static aom_codec_err_t ctrl_set_chroma_subsampling_x(aom_codec_alg_priv_t *ctx,
1704                                                      va_list args) {
1705   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1706   extra_cfg.chroma_subsampling_x = CAST(AV1E_SET_CHROMA_SUBSAMPLING_X, args);
1707   return update_extra_cfg(ctx, &extra_cfg);
1708 }
1709 
ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t * ctx,va_list args)1710 static aom_codec_err_t ctrl_set_chroma_subsampling_y(aom_codec_alg_priv_t *ctx,
1711                                                      va_list args) {
1712   struct av1_extracfg extra_cfg = ctx->extra_cfg;
1713   extra_cfg.chroma_subsampling_y = CAST(AV1E_SET_CHROMA_SUBSAMPLING_Y, args);
1714   return update_extra_cfg(ctx, &extra_cfg);
1715 }
1716 
1717 static aom_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1718   { AV1_COPY_REFERENCE, ctrl_copy_reference },
1719   { AOME_USE_REFERENCE, ctrl_use_reference },
1720 
1721   // Setters
1722   { AV1_SET_REFERENCE, ctrl_set_reference },
1723   { AOM_SET_POSTPROC, ctrl_set_previewpp },
1724   { AOME_SET_ROI_MAP, ctrl_set_roi_map },
1725   { AOME_SET_ACTIVEMAP, ctrl_set_active_map },
1726   { AOME_SET_SCALEMODE, ctrl_set_scale_mode },
1727   { AOME_SET_SPATIAL_LAYER_ID, ctrl_set_spatial_layer_id },
1728   { AOME_SET_CPUUSED, ctrl_set_cpuused },
1729   { AOME_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
1730   { AOME_SET_ENABLEAUTOBWDREF, ctrl_set_enable_auto_bwd_ref },
1731   { AOME_SET_SHARPNESS, ctrl_set_sharpness },
1732   { AOME_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
1733   { AV1E_SET_ROW_MT, ctrl_set_row_mt },
1734   { AV1E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
1735   { AV1E_SET_TILE_ROWS, ctrl_set_tile_rows },
1736   { AOME_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
1737   { AOME_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
1738   { AOME_SET_TUNING, ctrl_set_tuning },
1739   { AOME_SET_CQ_LEVEL, ctrl_set_cq_level },
1740   { AOME_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
1741   { AOME_SET_NUMBER_SPATIAL_LAYERS, ctrl_set_number_spatial_layers },
1742   { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
1743   { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1744   { AV1E_SET_LOSSLESS, ctrl_set_lossless },
1745   { AV1E_SET_ENABLE_CDEF, ctrl_set_enable_cdef },
1746   { AV1E_SET_ENABLE_RESTORATION, ctrl_set_enable_restoration },
1747   { AV1E_SET_DISABLE_TRELLIS_QUANT, ctrl_set_disable_trellis_quant },
1748   { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
1749   { AV1E_SET_QM_Y, ctrl_set_qm_y },
1750   { AV1E_SET_QM_U, ctrl_set_qm_u },
1751   { AV1E_SET_QM_V, ctrl_set_qm_v },
1752   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
1753   { AV1E_SET_QM_MAX, ctrl_set_qm_max },
1754 #if CONFIG_DIST_8X8
1755   { AV1E_SET_ENABLE_DIST_8X8, ctrl_set_enable_dist_8x8 },
1756 #endif
1757   { AV1E_SET_NUM_TG, ctrl_set_num_tg },
1758   { AV1E_SET_MTU, ctrl_set_mtu },
1759   { AV1E_SET_TIMING_INFO_TYPE, ctrl_set_timing_info_type },
1760   { AV1E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
1761   { AV1E_SET_ERROR_RESILIENT_MODE, ctrl_set_error_resilient_mode },
1762   { AV1E_SET_S_FRAME_MODE, ctrl_set_s_frame_mode },
1763   { AV1E_SET_ENABLE_DF, ctrl_set_enable_df },
1764   { AV1E_SET_ENABLE_ORDER_HINT, ctrl_set_enable_order_hint },
1765   { AV1E_SET_ENABLE_JNT_COMP, ctrl_set_enable_jnt_comp },
1766   { AV1E_SET_ENABLE_REF_FRAME_MVS, ctrl_set_enable_ref_frame_mvs },
1767   { AV1E_SET_ALLOW_REF_FRAME_MVS, ctrl_set_allow_ref_frame_mvs },
1768   { AV1E_SET_ENABLE_WARPED_MOTION, ctrl_set_enable_warped_motion },
1769   { AV1E_SET_ALLOW_WARPED_MOTION, ctrl_set_allow_warped_motion },
1770   { AV1E_SET_ENABLE_SUPERRES, ctrl_set_enable_superres },
1771   { AV1E_SET_AQ_MODE, ctrl_set_aq_mode },
1772   { AV1E_SET_DELTAQ_MODE, ctrl_set_deltaq_mode },
1773   { AV1E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
1774   { AV1E_SET_TUNE_CONTENT, ctrl_set_tune_content },
1775   { AV1E_SET_CDF_UPDATE_MODE, ctrl_set_cdf_update_mode },
1776   { AV1E_SET_COLOR_PRIMARIES, ctrl_set_color_primaries },
1777   { AV1E_SET_TRANSFER_CHARACTERISTICS, ctrl_set_transfer_characteristics },
1778   { AV1E_SET_MATRIX_COEFFICIENTS, ctrl_set_matrix_coefficients },
1779   { AV1E_SET_CHROMA_SAMPLE_POSITION, ctrl_set_chroma_sample_position },
1780   { AV1E_SET_COLOR_RANGE, ctrl_set_color_range },
1781   { AV1E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
1782   { AV1E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
1783   { AV1E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
1784   { AV1E_SET_RENDER_SIZE, ctrl_set_render_size },
1785   { AV1E_SET_SUPERBLOCK_SIZE, ctrl_set_superblock_size },
1786   { AV1E_SET_SINGLE_TILE_DECODING, ctrl_set_single_tile_decoding },
1787   { AV1E_SET_FILM_GRAIN_TEST_VECTOR, ctrl_set_film_grain_test_vector },
1788   { AV1E_SET_FILM_GRAIN_TABLE, ctrl_set_film_grain_table },
1789 #if CONFIG_DENOISE
1790   { AV1E_SET_DENOISE_NOISE_LEVEL, ctrl_set_denoise_noise_level },
1791   { AV1E_SET_DENOISE_BLOCK_SIZE, ctrl_set_denoise_block_size },
1792 #endif  // CONFIG_FILM_GRAIN
1793   { AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
1794 
1795   // Getters
1796   { AOME_GET_LAST_QUANTIZER, ctrl_get_quantizer },
1797   { AOME_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
1798   { AV1_GET_REFERENCE, ctrl_get_reference },
1799   { AV1E_GET_ACTIVEMAP, ctrl_get_active_map },
1800   { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
1801   { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
1802   { AV1E_SET_CHROMA_SUBSAMPLING_X, ctrl_set_chroma_subsampling_x },
1803   { AV1E_SET_CHROMA_SUBSAMPLING_Y, ctrl_set_chroma_subsampling_y },
1804   { -1, NULL },
1805 };
1806 
1807 static aom_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
1808   { 0,
1809     {
1810         // NOLINT
1811         0,  // g_usage
1812         0,  // g_threads
1813         0,  // g_profile
1814 
1815         320,         // g_width
1816         240,         // g_height
1817         0,           // g_limit
1818         0,           // g_forced_max_frame_width
1819         0,           // g_forced_max_frame_height
1820         AOM_BITS_8,  // g_bit_depth
1821         8,           // g_input_bit_depth
1822 
1823         { 1, 30 },  // g_timebase
1824 
1825         0,  // g_error_resilient
1826 
1827         AOM_RC_ONE_PASS,  // g_pass
1828 
1829         19,  // g_lag_in_frames
1830 
1831         0,                // rc_dropframe_thresh
1832         RESIZE_NONE,      // rc_resize_mode
1833         SCALE_NUMERATOR,  // rc_resize_denominator
1834         SCALE_NUMERATOR,  // rc_resize_kf_denominator
1835 
1836         0,                // rc_superres_mode
1837         SCALE_NUMERATOR,  // rc_superres_denominator
1838         SCALE_NUMERATOR,  // rc_superres_kf_denominator
1839         63,               // rc_superres_qthresh
1840         63,               // rc_superres_kf_qthresh
1841 
1842         AOM_VBR,      // rc_end_usage
1843         { NULL, 0 },  // rc_twopass_stats_in
1844         { NULL, 0 },  // rc_firstpass_mb_stats_in
1845         256,          // rc_target_bandwidth
1846         0,            // rc_min_quantizer
1847         63,           // rc_max_quantizer
1848         25,           // rc_undershoot_pct
1849         25,           // rc_overshoot_pct
1850 
1851         6000,  // rc_max_buffer_size
1852         4000,  // rc_buffer_initial_size
1853         5000,  // rc_buffer_optimal_size
1854 
1855         50,    // rc_two_pass_vbrbias
1856         0,     // rc_two_pass_vbrmin_section
1857         2000,  // rc_two_pass_vbrmax_section
1858 
1859         // keyframing settings (kf)
1860         0,            // fwd_kf_enabled
1861         AOM_KF_AUTO,  // g_kfmode
1862         0,            // kf_min_dist
1863         9999,         // kf_max_dist
1864         0,            // sframe_dist
1865         1,            // sframe_mode
1866         0,            // large_scale_tile
1867         0,            // monochrome
1868         0,            // full_still_picture_hdr
1869         0,            // save_as_annexb
1870         0,            // tile_width_count
1871         0,            // tile_height_count
1872         { 0 },        // tile_widths
1873         { 0 },        // tile_heights
1874         { 1 },        // config file
1875     } },
1876 };
1877 
1878 #ifndef VERSION_STRING
1879 #define VERSION_STRING
1880 #endif
1881 CODEC_INTERFACE(aom_codec_av1_cx) = {
1882   "AOMedia Project AV1 Encoder" VERSION_STRING,
1883   AOM_CODEC_INTERNAL_ABI_VERSION,
1884   AOM_CODEC_CAP_HIGHBITDEPTH | AOM_CODEC_CAP_ENCODER |
1885       AOM_CODEC_CAP_PSNR,  // aom_codec_caps_t
1886   encoder_init,            // aom_codec_init_fn_t
1887   encoder_destroy,         // aom_codec_destroy_fn_t
1888   encoder_ctrl_maps,       // aom_codec_ctrl_fn_map_t
1889   {
1890       // NOLINT
1891       NULL,  // aom_codec_peek_si_fn_t
1892       NULL,  // aom_codec_get_si_fn_t
1893       NULL,  // aom_codec_decode_fn_t
1894       NULL,  // aom_codec_get_frame_fn_t
1895       NULL   // aom_codec_set_fb_fn_t
1896   },
1897   {
1898       // NOLINT
1899       1,                           // 1 cfg map
1900       encoder_usage_cfg_map,       // aom_codec_enc_cfg_map_t
1901       encoder_encode,              // aom_codec_encode_fn_t
1902       encoder_get_cxdata,          // aom_codec_get_cx_data_fn_t
1903       encoder_set_config,          // aom_codec_enc_config_set_fn_t
1904       encoder_get_global_headers,  // aom_codec_get_global_headers_fn_t
1905       encoder_get_preview,         // aom_codec_get_preview_frame_fn_t
1906       NULL                         // aom_codec_enc_mr_get_mem_loc_fn_t
1907   }
1908 };
1909