av1_cx_iface.c 62.3 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13
 */
#include <stdlib.h>
#include <string.h>

Yaowu Xu's avatar
Yaowu Xu committed
14 15 16
#include "./aom_config.h"
#include "aom/aom_encoder.h"
#include "aom_ports/aom_once.h"
17
#include "aom_ports/system_state.h"
Yaowu Xu's avatar
Yaowu Xu committed
18 19
#include "aom/internal/aom_codec_internal.h"
#include "./aom_version.h"
20
#include "av1/encoder/encoder.h"
Yaowu Xu's avatar
Yaowu Xu committed
21
#include "aom/aomcx.h"
22
#include "av1/encoder/firstpass.h"
Yaowu Xu's avatar
Yaowu Xu committed
23
#include "av1/av1_iface_common.h"
24 25 26 27
#if CONFIG_OBU
#include "av1/encoder/bitstream.h"
#include "aom_ports/mem_ops.h"
#endif
Jingning Han's avatar
Jingning Han committed
28

29 30 31
#define MAG_SIZE (4)
#define MAX_INDEX_SIZE (256)

Yaowu Xu's avatar
Yaowu Xu committed
32
struct av1_extracfg {
33
  int cpu_used;  // available cpu percentage in 1/16
34
  int dev_sf;
35 36 37 38 39
  unsigned int enable_auto_alt_ref;
  unsigned int enable_auto_bwd_ref;
  unsigned int noise_sensitivity;
  unsigned int sharpness;
  unsigned int static_thresh;
40 41
  unsigned int tile_columns;  // log2 number of tile columns
  unsigned int tile_rows;     // log2 number of tile rows
42 43 44
#if CONFIG_DEPENDENT_HORZTILES
  unsigned int dependent_horz_tiles;
#endif
45
#if CONFIG_LOOPFILTERING_ACROSS_TILES
46 47 48 49
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
  unsigned int loop_filter_across_tiles_v_enabled;
  unsigned int loop_filter_across_tiles_h_enabled;
#else
50
  unsigned int loop_filter_across_tiles_enabled;
51
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
52
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
53 54 55 56
  unsigned int arnr_max_frames;
  unsigned int arnr_strength;
  unsigned int min_gf_interval;
  unsigned int max_gf_interval;
Yaowu Xu's avatar
Yaowu Xu committed
57
  aom_tune_metric tuning;
58 59 60 61 62
  unsigned int cq_level;  // constrained quality level
  unsigned int rc_max_intra_bitrate_pct;
  unsigned int rc_max_inter_bitrate_pct;
  unsigned int gf_cbr_boost_pct;
  unsigned int lossless;
63
  unsigned int enable_cdef;
64 65 66 67
#if CONFIG_AOM_QM
  unsigned int enable_qm;
  unsigned int qm_min;
  unsigned int qm_max;
68 69 70
#endif
#if CONFIG_DIST_8X8
  unsigned int enable_dist_8x8;
71 72 73
#endif
  unsigned int num_tg;
  unsigned int mtu_size;
74 75
#if CONFIG_TEMPMV_SIGNALING
  unsigned int disable_tempmv;
76
#endif
77 78
  unsigned int frame_parallel_decoding_mode;
  AQ_MODE aq_mode;
Fangwen Fu's avatar
Fangwen Fu committed
79 80 81
#if CONFIG_EXT_DELTA_Q
  DELTAQ_MODE deltaq_mode;
#endif
82
  unsigned int frame_periodic_boost;
Yaowu Xu's avatar
Yaowu Xu committed
83 84 85
  aom_bit_depth_t bit_depth;
  aom_tune_content content;
  aom_color_space_t color_space;
86 87
  aom_transfer_function_t transfer_function;
  aom_chroma_sample_position_t chroma_sample_position;
88 89 90
  int color_range;
  int render_width;
  int render_height;
Yaowu Xu's avatar
Yaowu Xu committed
91
  aom_superblock_size_t superblock_size;
92
#if CONFIG_EXT_TILE
93
  unsigned int single_tile_decoding;
94
#endif  // CONFIG_EXT_TILE
Yunqing Wang's avatar
Yunqing Wang committed
95 96

  unsigned int motion_vector_unit_test;
Jingning Han's avatar
Jingning Han committed
97 98
};

Yaowu Xu's avatar
Yaowu Xu committed
99
static struct av1_extracfg default_extra_cfg = {
100
  0,  // cpu_used
101
  0,  // dev_sf
102
  1,  // enable_auto_alt_ref
103 104 105 106 107 108
  0,  // enable_auto_bwd_ref
  0,  // noise_sensitivity
  0,  // sharpness
  0,  // static_thresh
  0,  // tile_columns
  0,  // tile_rows
109
#if CONFIG_DEPENDENT_HORZTILES
110
  0,  // Dependent Horizontal tiles
111
#endif
112
#if CONFIG_LOOPFILTERING_ACROSS_TILES
113 114 115 116 117 118
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
  1,  // loop_filter_across_tiles_v_enabled
  1,  // loop_filter_across_tiles_h_enabled
#else
  1,  // loop_filter_across_tiles_enabled
#endif            // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
119
#endif            // CONFIG_LOOPFILTERING_ACROSS_TILES
120 121 122 123
  7,              // arnr_max_frames
  5,              // arnr_strength
  0,              // min_gf_interval; 0 -> default decision
  0,              // max_gf_interval; 0 -> default decision
Yaowu Xu's avatar
Yaowu Xu committed
124
  AOM_TUNE_PSNR,  // tuning
125 126 127 128 129
  10,             // cq_level
  0,              // rc_max_intra_bitrate_pct
  0,              // rc_max_inter_bitrate_pct
  0,              // gf_cbr_boost_pct
  0,              // lossless
130
  1,              // enable_cdef
131 132 133 134
#if CONFIG_AOM_QM
  0,                 // enable_qm
  DEFAULT_QM_FIRST,  // qm_min
  DEFAULT_QM_LAST,   // qm_max
135 136 137
#endif
#if CONFIG_DIST_8X8
  0,
138 139 140
#endif
  1,  // max number of tile groups
  0,  // mtu_size
141 142
#if CONFIG_TEMPMV_SIGNALING
  0,  // disable temporal mv prediction
143
#endif
Fangwen Fu's avatar
Fangwen Fu committed
144 145 146 147 148
  1,      // frame_parallel_decoding_mode
  NO_AQ,  // aq_mode
#if CONFIG_EXT_DELTA_Q
  NO_DELTA_Q,  // deltaq_mode
#endif
149 150 151 152 153 154
  CONFIG_XIPHRC,                // frame_periodic_delta_q
  AOM_BITS_8,                   // Bit depth
  AOM_CONTENT_DEFAULT,          // content
  AOM_CS_UNKNOWN,               // color space
  AOM_TF_UNKNOWN,               // transfer function
  AOM_CSP_UNKNOWN,              // chroma sample position
155 156 157 158
  0,                            // color range
  0,                            // render width
  0,                            // render height
  AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
159
#if CONFIG_EXT_TILE
160
  0,    // Single tile decoding is off by default.
161
#endif  // CONFIG_EXT_TILE
Yunqing Wang's avatar
Yunqing Wang committed
162 163

  0,  // motion_vector_unit_test
Jingning Han's avatar
Jingning Han committed
164 165
};

Yaowu Xu's avatar
Yaowu Xu committed
166 167 168 169 170 171
struct aom_codec_alg_priv {
  aom_codec_priv_t base;
  aom_codec_enc_cfg_t cfg;
  struct av1_extracfg extra_cfg;
  AV1EncoderConfig oxcf;
  AV1_COMP *cpi;
172 173 174 175 176 177
  unsigned char *cx_data;
  size_t cx_data_sz;
  unsigned char *pending_cx_data;
  size_t pending_cx_data_sz;
  int pending_frame_count;
  size_t pending_frame_sizes[8];
Yaowu Xu's avatar
Yaowu Xu committed
178 179 180 181
  aom_image_t preview_img;
  aom_enc_frame_flags_t next_frame_flags;
  aom_postproc_cfg_t preview_ppcfg;
  aom_codec_pkt_list_decl(256) pkt_list;
182
  unsigned int fixed_kf_cntr;
Jingning Han's avatar
Jingning Han committed
183
  // BufferPool that holds all reference frames.
184
  BufferPool *buffer_pool;
Jingning Han's avatar
Jingning Han committed
185 186
};

Yaowu Xu's avatar
Yaowu Xu committed
187 188 189
static aom_codec_err_t update_error_state(
    aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
  const aom_codec_err_t res = error->error_code;
Jingning Han's avatar
Jingning Han committed
190

Yaowu Xu's avatar
Yaowu Xu committed
191
  if (res != AOM_CODEC_OK)
Jingning Han's avatar
Jingning Han committed
192 193 194 195 196 197
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;

  return res;
}

#undef ERROR
198 199 200
#define ERROR(str)                  \
  do {                              \
    ctx->base.err_detail = str;     \
Yaowu Xu's avatar
Yaowu Xu committed
201
    return AOM_CODEC_INVALID_PARAM; \
Jingning Han's avatar
Jingning Han committed
202 203
  } while (0)

204 205 206 207
#define RANGE_CHECK(p, memb, lo, hi)                   \
  do {                                                 \
    if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
      ERROR(#memb " out of range [" #lo ".." #hi "]"); \
Jingning Han's avatar
Jingning Han committed
208 209
  } while (0)

210 211 212
#define RANGE_CHECK_HI(p, memb, hi)                                     \
  do {                                                                  \
    if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
Jingning Han's avatar
Jingning Han committed
213 214
  } while (0)

215 216 217
#define RANGE_CHECK_LO(p, memb, lo)                                     \
  do {                                                                  \
    if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
Jingning Han's avatar
Jingning Han committed
218 219
  } while (0)

220 221 222
#define RANGE_CHECK_BOOL(p, memb)                                     \
  do {                                                                \
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
Jingning Han's avatar
Jingning Han committed
223 224
  } while (0)

Yaowu Xu's avatar
Yaowu Xu committed
225 226 227
static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
                                       const aom_codec_enc_cfg_t *cfg,
                                       const struct av1_extracfg *extra_cfg) {
228 229 230 231 232 233 234 235
  RANGE_CHECK(cfg, g_w, 1, 65535);  // 16 bits available
  RANGE_CHECK(cfg, g_h, 1, 65535);  // 16 bits available
  RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
  RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
  RANGE_CHECK_HI(cfg, g_profile, 3);

  RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
  RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
Jingning Han's avatar
Jingning Han committed
236
  RANGE_CHECK_BOOL(extra_cfg, lossless);
237
  RANGE_CHECK_HI(extra_cfg, aq_mode, AQ_MODE_COUNT - 1);
Fangwen Fu's avatar
Fangwen Fu committed
238
#if CONFIG_EXT_DELTA_Q
239
  RANGE_CHECK_HI(extra_cfg, deltaq_mode, DELTAQ_MODE_COUNT - 1);
Fangwen Fu's avatar
Fangwen Fu committed
240
#endif
241
  RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
242 243
  RANGE_CHECK_HI(cfg, g_threads, 64);
  RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
Yaowu Xu's avatar
Yaowu Xu committed
244
  RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
245 246
  RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
  RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
Jingning Han's avatar
Jingning Han committed
247
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
Yaowu Xu's avatar
Yaowu Xu committed
248
  RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
249
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Yaowu Xu's avatar
Yaowu Xu committed
250
  RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
251 252
  RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
  RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
Jingning Han's avatar
Jingning Han committed
253 254 255 256 257
  if (extra_cfg->max_gf_interval > 0) {
    RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
  }
  if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
    RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
258
                (MAX_LAG_BUFFERS - 1));
Jingning Han's avatar
Jingning Han committed
259 260
  }

261
  RANGE_CHECK_HI(cfg, rc_resize_mode, RESIZE_MODES - 1);
262 263 264 265
  RANGE_CHECK(cfg, rc_resize_denominator, SCALE_NUMERATOR,
              SCALE_NUMERATOR << 1);
  RANGE_CHECK(cfg, rc_resize_kf_denominator, SCALE_NUMERATOR,
              SCALE_NUMERATOR << 1);
266
#if CONFIG_HORZONLY_FRAME_SUPERRES
267
  RANGE_CHECK_HI(cfg, rc_superres_mode, SUPERRES_MODES - 1);
268 269 270 271
  RANGE_CHECK(cfg, rc_superres_denominator, SCALE_NUMERATOR,
              SCALE_NUMERATOR << 1);
  RANGE_CHECK(cfg, rc_superres_kf_denominator, SCALE_NUMERATOR,
              SCALE_NUMERATOR << 1);
272 273
  RANGE_CHECK(cfg, rc_superres_qthresh, 1, 63);
  RANGE_CHECK(cfg, rc_superres_kf_qthresh, 1, 63);
274
#endif  // CONFIG_HORZONLY_FRAME_SUPERRES
275

Yaowu Xu's avatar
Yaowu Xu committed
276
  // AV1 does not support a lower bound on the keyframe interval in
Jingning Han's avatar
Jingning Han committed
277
  // automatic keyframe placement mode.
Yaowu Xu's avatar
Yaowu Xu committed
278
  if (cfg->kf_mode != AOM_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
Jingning Han's avatar
Jingning Han committed
279
      cfg->kf_min_dist > 0)
280 281 282
    ERROR(
        "kf_min_dist not supported in auto mode, use 0 "
        "or kf_max_dist instead.");
Jingning Han's avatar
Jingning Han committed
283

Yunqing Wang's avatar
Yunqing Wang committed
284
  RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
285 286
  RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
  RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
287
  RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
288
  RANGE_CHECK(extra_cfg, dev_sf, 0, UINT8_MAX);
Jingning Han's avatar
Jingning Han committed
289
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
Yaowu Xu's avatar
Yaowu Xu committed
290 291
  RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
              AOM_SUPERBLOCK_SIZE_DYNAMIC);
292
#if CONFIG_EXT_TILE
293 294 295 296
  RANGE_CHECK_HI(cfg, large_scale_tile, 1);
  RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);

  if (cfg->large_scale_tile) {
297 298 299
// TODO(any): Waring. If CONFIG_EXT_TILE is true, tile_columns really
// means tile_width, and tile_rows really means tile_hight. The interface
// should be sanitized.
300
#if CONFIG_EXT_PARTITION
301 302 303 304 305 306 307 308 309 310 311
    if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
      if (extra_cfg->tile_columns != 0)
        RANGE_CHECK(extra_cfg, tile_columns, 1, 32);
      if (extra_cfg->tile_rows != 0) RANGE_CHECK(extra_cfg, tile_rows, 1, 32);
    } else {
#endif  // CONFIG_EXT_PARTITION
      if (extra_cfg->tile_columns != 0)
        RANGE_CHECK(extra_cfg, tile_columns, 1, 64);
      if (extra_cfg->tile_rows != 0) RANGE_CHECK(extra_cfg, tile_rows, 1, 64);
#if CONFIG_EXT_PARTITION
    }
312
#endif  // CONFIG_EXT_PARTITION
313 314
  } else {
#endif  // CONFIG_EXT_TILE
315
#if CONFIG_MAX_TILE
316
    RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
317 318 319 320 321
    RANGE_CHECK_HI(extra_cfg, tile_rows, 6);
#else   // CONFIG_MAX_TILE
  RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
  RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
#endif  // CONFIG_MAX_TILE
322
#if CONFIG_EXT_TILE
323
  }
324
#endif  // CONFIG_EXT_TILE
325

326 327 328 329 330 331 332
#if CONFIG_EXT_TILE
  if (cfg->large_scale_tile && extra_cfg->aq_mode)
    ERROR(
        "Adaptive quantization are not supported in large scale tile "
        "coding.");
#endif  // CONFIG_EXT_TILE

333 334 335
#if CONFIG_DEPENDENT_HORZTILES
  RANGE_CHECK_HI(extra_cfg, dependent_horz_tiles, 1);
#endif
336
#if CONFIG_LOOPFILTERING_ACROSS_TILES
337 338 339 340
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
  RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_v_enabled, 1);
  RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_h_enabled, 1);
#else
341
  RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_enabled, 1);
342
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
343
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
Jingning Han's avatar
Jingning Han committed
344
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
345
  RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
Jingning Han's avatar
Jingning Han committed
346
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
347
  RANGE_CHECK_HI(extra_cfg, cq_level, 63);
Yaowu Xu's avatar
Yaowu Xu committed
348
  RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
Jingning Han's avatar
Jingning Han committed
349
  RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
Yaowu Xu's avatar
Yaowu Xu committed
350
  RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
Jingning Han's avatar
Jingning Han committed
351

Yaowu Xu's avatar
Yaowu Xu committed
352 353 354
  // TODO(yaowu): remove this when ssim tuning is implemented for av1
  if (extra_cfg->tuning == AOM_TUNE_SSIM)
    ERROR("Option --tune=ssim is not currently supported in AV1.");
Jingning Han's avatar
Jingning Han committed
355

Yaowu Xu's avatar
Yaowu Xu committed
356
  if (cfg->g_pass == AOM_RC_LAST_PASS) {
357
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
358 359 360
    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
    const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
    const FIRSTPASS_STATS *stats;
361
#endif
Jingning Han's avatar
Jingning Han committed
362 363 364 365

    if (cfg->rc_twopass_stats_in.buf == NULL)
      ERROR("rc_twopass_stats_in.buf not set.");

366
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
367 368 369
    if (cfg->rc_twopass_stats_in.sz % packet_sz)
      ERROR("rc_twopass_stats_in.sz indicates truncated packet.");

Yunqing Wang's avatar
Yunqing Wang committed
370 371
    if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
      ERROR("rc_twopass_stats_in requires at least two packets.");
Jingning Han's avatar
Jingning Han committed
372

Yunqing Wang's avatar
Yunqing Wang committed
373 374
    stats =
        (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
Jingning Han's avatar
Jingning Han committed
375

Yunqing Wang's avatar
Yunqing Wang committed
376 377
    if ((int)(stats->count + 0.5) != n_packets - 1)
      ERROR("rc_twopass_stats_in missing EOS stats packet");
378
#endif
Jingning Han's avatar
Jingning Han committed
379 380 381
  }

  if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
Yaowu Xu's avatar
Yaowu Xu committed
382
      cfg->g_bit_depth > AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
383 384
    ERROR("Codec high bit-depth not supported in profile < 2");
  }
385
  if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
Jingning Han's avatar
Jingning Han committed
386 387 388
    ERROR("Source high bit-depth not supported in profile < 2");
  }
  if (cfg->g_profile > (unsigned int)PROFILE_1 &&
Yaowu Xu's avatar
Yaowu Xu committed
389
      cfg->g_bit_depth == AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
390 391
    ERROR("Codec bit-depth 8 not supported in profile > 1");
  }
392
#if CONFIG_COLORSPACE_HEADERS
393 394 395
#if CONFIG_MONO_VIDEO
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_MONOCHROME);
#else
396
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_ICTCP);
397
#endif  // CONFIG_MONO_VIDEO
398 399 400
  RANGE_CHECK(extra_cfg, transfer_function, AOM_TF_UNKNOWN, AOM_TF_HLG);
  RANGE_CHECK(extra_cfg, chroma_sample_position, AOM_CSP_UNKNOWN,
              AOM_CSP_COLOCATED);
401 402 403
#else
#if CONFIG_MONO_VIDEO
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_MONOCHROME);
404
#else
Yaowu Xu's avatar
Yaowu Xu committed
405
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_SRGB);
406
#endif
407
#endif
408
  RANGE_CHECK(extra_cfg, color_range, 0, 1);
Tom Finegan's avatar
Tom Finegan committed
409 410 411 412 413 414 415

#if CONFIG_DIST_8X8
  RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_DAALA_DIST);
#else
  RANGE_CHECK(extra_cfg, tuning, AOM_TUNE_PSNR, AOM_TUNE_SSIM);
#endif

416 417 418 419 420 421 422 423 424
  if (extra_cfg->lossless) {
    if (extra_cfg->aq_mode != 0)
      ERROR("Only --aq_mode=0 can be used with --lossless=1.");
#if CONFIG_DIST_8X8
    if (extra_cfg->enable_dist_8x8)
      ERROR("dist-8x8 cannot be used with lossless compression.");
#endif
  }

Yaowu Xu's avatar
Yaowu Xu committed
425
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
426 427
}

Yaowu Xu's avatar
Yaowu Xu committed
428 429
static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
                                    const aom_image_t *img) {
Jingning Han's avatar
Jingning Han committed
430
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
431 432 433 434 435 436
    case AOM_IMG_FMT_YV12:
    case AOM_IMG_FMT_I420:
    case AOM_IMG_FMT_I42016: break;
    case AOM_IMG_FMT_I422:
    case AOM_IMG_FMT_I444:
    case AOM_IMG_FMT_I440:
Jingning Han's avatar
Jingning Han committed
437
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
438 439 440
        ERROR(
            "Invalid image format. I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
441 442
      }
      break;
Yaowu Xu's avatar
Yaowu Xu committed
443 444 445
    case AOM_IMG_FMT_I42216:
    case AOM_IMG_FMT_I44416:
    case AOM_IMG_FMT_I44016:
Jingning Han's avatar
Jingning Han committed
446 447
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
          ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
448 449 450
        ERROR(
            "Invalid image format. 16-bit I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
451 452 453
      }
      break;
    default:
454 455 456
      ERROR(
          "Invalid image format. Only YV12, I420, I422, I444 images are "
          "supported.");
Jingning Han's avatar
Jingning Han committed
457 458 459 460 461 462
      break;
  }

  if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
    ERROR("Image size must match encoder init configuration size");

Yaowu Xu's avatar
Yaowu Xu committed
463
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
464 465
}

Yaowu Xu's avatar
Yaowu Xu committed
466
static int get_image_bps(const aom_image_t *img) {
Jingning Han's avatar
Jingning Han committed
467
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
468 469 470 471 472 473 474 475 476
    case AOM_IMG_FMT_YV12:
    case AOM_IMG_FMT_I420: return 12;
    case AOM_IMG_FMT_I422: return 16;
    case AOM_IMG_FMT_I444: return 24;
    case AOM_IMG_FMT_I440: return 16;
    case AOM_IMG_FMT_I42016: return 24;
    case AOM_IMG_FMT_I42216: return 32;
    case AOM_IMG_FMT_I44416: return 48;
    case AOM_IMG_FMT_I44016: return 32;
Jingning Han's avatar
Jingning Han committed
477 478 479 480 481
    default: assert(0 && "Invalid image format"); break;
  }
  return 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
482 483 484 485
static aom_codec_err_t set_encoder_config(
    AV1EncoderConfig *oxcf, const aom_codec_enc_cfg_t *cfg,
    const struct av1_extracfg *extra_cfg) {
  const int is_vbr = cfg->rc_end_usage == AOM_VBR;
Jingning Han's avatar
Jingning Han committed
486 487
  oxcf->profile = cfg->g_profile;
  oxcf->max_threads = (int)cfg->g_threads;
488 489
  oxcf->width = cfg->g_w;
  oxcf->height = cfg->g_h;
Jingning Han's avatar
Jingning Han committed
490 491 492 493
  oxcf->bit_depth = cfg->g_bit_depth;
  oxcf->input_bit_depth = cfg->g_input_bit_depth;
  // guess a frame rate if out of whack, use 30
  oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
494
  if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
Jingning Han's avatar
Jingning Han committed
495 496 497 498

  oxcf->mode = GOOD;

  switch (cfg->g_pass) {
Yaowu Xu's avatar
Yaowu Xu committed
499 500 501
    case AOM_RC_ONE_PASS: oxcf->pass = 0; break;
    case AOM_RC_FIRST_PASS: oxcf->pass = 1; break;
    case AOM_RC_LAST_PASS: oxcf->pass = 2; break;
Jingning Han's avatar
Jingning Han committed
502 503
  }

504
  oxcf->lag_in_frames =
Yaowu Xu's avatar
Yaowu Xu committed
505
      cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
Jingning Han's avatar
Jingning Han committed
506 507 508 509 510 511 512 513 514
  oxcf->rc_mode = cfg->rc_end_usage;

  // Convert target bandwidth from Kbit/s to Bit/s
  oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
  oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
  oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
  oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;

  oxcf->best_allowed_q =
Yaowu Xu's avatar
Yaowu Xu committed
515
      extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
Jingning Han's avatar
Jingning Han committed
516
  oxcf->worst_allowed_q =
Yaowu Xu's avatar
Yaowu Xu committed
517 518
      extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
  oxcf->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
Jingning Han's avatar
Jingning Han committed
519 520
  oxcf->fixed_q = -1;

521
  oxcf->using_cdef = extra_cfg->enable_cdef;
522 523 524 525 526
#if CONFIG_AOM_QM
  oxcf->using_qm = extra_cfg->enable_qm;
  oxcf->qm_minlevel = extra_cfg->qm_min;
  oxcf->qm_maxlevel = extra_cfg->qm_max;
#endif
527 528 529 530 531 532
#if CONFIG_DIST_8X8
  oxcf->using_dist_8x8 = extra_cfg->enable_dist_8x8;
  if (extra_cfg->tuning == AOM_TUNE_CDEF_DIST ||
      extra_cfg->tuning == AOM_TUNE_DAALA_DIST)
    oxcf->using_dist_8x8 = 1;
#endif
533
  oxcf->num_tile_groups = extra_cfg->num_tg;
534 535 536 537
#if CONFIG_EXT_TILE
  // In large-scale tile encoding mode, num_tile_groups is always 1.
  if (cfg->large_scale_tile) oxcf->num_tile_groups = 1;
#endif  // CONFIG_EXT_TILE
538 539
  oxcf->mtu = extra_cfg->mtu_size;

540 541 542
#if CONFIG_TEMPMV_SIGNALING
  oxcf->disable_tempmv = extra_cfg->disable_tempmv;
#endif
543 544
  oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
  oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
Jingning Han's avatar
Jingning Han committed
545

546
  oxcf->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
547 548
  oxcf->resize_scale_denominator = (uint8_t)cfg->rc_resize_denominator;
  oxcf->resize_kf_scale_denominator = (uint8_t)cfg->rc_resize_kf_denominator;
549
  if (oxcf->resize_mode == RESIZE_FIXED &&
550 551
      oxcf->resize_scale_denominator == SCALE_NUMERATOR &&
      oxcf->resize_kf_scale_denominator == SCALE_NUMERATOR)
Jingning Han's avatar
Jingning Han committed
552
    oxcf->resize_mode = RESIZE_NONE;
553

554
#if CONFIG_HORZONLY_FRAME_SUPERRES
555
  oxcf->superres_mode = (SUPERRES_MODE)cfg->rc_superres_mode;
556 557 558
  oxcf->superres_scale_denominator = (uint8_t)cfg->rc_superres_denominator;
  oxcf->superres_kf_scale_denominator =
      (uint8_t)cfg->rc_superres_kf_denominator;
559 560 561 562 563 564 565
  oxcf->superres_qthresh =
      extra_cfg->lossless ? 255
                          : av1_quantizer_to_qindex(cfg->rc_superres_qthresh);
  oxcf->superres_kf_qthresh =
      extra_cfg->lossless
          ? 255
          : av1_quantizer_to_qindex(cfg->rc_superres_kf_qthresh);
566
  if (oxcf->superres_mode == SUPERRES_FIXED &&
567 568
      oxcf->superres_scale_denominator == SCALE_NUMERATOR &&
      oxcf->superres_kf_scale_denominator == SCALE_NUMERATOR)
569
    oxcf->superres_mode = SUPERRES_NONE;
570 571 572
  if (oxcf->superres_mode == SUPERRES_QTHRESH &&
      oxcf->superres_qthresh == 255 && oxcf->superres_kf_qthresh == 255)
    oxcf->superres_mode = SUPERRES_NONE;
573
#endif  // CONFIG_HORZONLY_FRAME_SUPERRES
574

575
  oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
Jingning Han's avatar
Jingning Han committed
576
  oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
577
  oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
Jingning Han's avatar
Jingning Han committed
578

579
  oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
Jingning Han's avatar
Jingning Han committed
580

581 582 583
  oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
  oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
  oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
Jingning Han's avatar
Jingning Han committed
584

585
  oxcf->auto_key =
Yaowu Xu's avatar
Yaowu Xu committed
586
      cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
587

588
  oxcf->key_freq = cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
589

590
  oxcf->speed = extra_cfg->cpu_used;
591
  oxcf->dev_sf = extra_cfg->dev_sf;
592 593 594 595
  oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
  oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
  oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
  oxcf->sharpness = extra_cfg->sharpness;
Jingning Han's avatar
Jingning Han committed
596

597
  oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
Jingning Han's avatar
Jingning Han committed
598 599

#if CONFIG_FP_MB_STATS
600
  oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
Jingning Han's avatar
Jingning Han committed
601 602 603
#endif

  oxcf->color_space = extra_cfg->color_space;
604

605 606 607
#if CONFIG_COLORSPACE_HEADERS
  oxcf->transfer_function = extra_cfg->transfer_function;
  oxcf->chroma_sample_position = extra_cfg->chroma_sample_position;
608 609 610 611 612
#else
  if (extra_cfg->transfer_function != AOM_TF_UNKNOWN)
    return AOM_CODEC_UNSUP_FEATURE;
  if (extra_cfg->chroma_sample_position != AOM_CSP_UNKNOWN)
    return AOM_CODEC_UNSUP_FEATURE;
613
#endif
614

615
  oxcf->color_range = extra_cfg->color_range;
616
  oxcf->render_width = extra_cfg->render_width;
617
  oxcf->render_height = extra_cfg->render_height;
Jingning Han's avatar
Jingning Han committed
618
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
619 620 621
  // Adjust g_lag_in_frames down if not needed
  oxcf->lag_in_frames =
      AOMMIN(MAX_GF_INTERVAL + oxcf->arnr_max_frames / 2, oxcf->lag_in_frames);
622
  oxcf->arnr_strength = extra_cfg->arnr_strength;
Jingning Han's avatar
Jingning Han committed
623 624 625 626 627 628
  oxcf->min_gf_interval = extra_cfg->min_gf_interval;
  oxcf->max_gf_interval = extra_cfg->max_gf_interval;

  oxcf->tuning = extra_cfg->tuning;
  oxcf->content = extra_cfg->content;

629 630 631 632 633
#if CONFIG_EXT_PARTITION
  oxcf->superblock_size = extra_cfg->superblock_size;
#endif  // CONFIG_EXT_PARTITION

#if CONFIG_EXT_TILE
634 635 636 637
  oxcf->large_scale_tile = cfg->large_scale_tile;
  oxcf->single_tile_decoding =
      (oxcf->large_scale_tile) ? extra_cfg->single_tile_decoding : 0;
  if (oxcf->large_scale_tile) {
638 639
#if CONFIG_EXT_PARTITION
    const unsigned int max =
Yaowu Xu's avatar
Yaowu Xu committed
640
        extra_cfg->superblock_size == AOM_SUPERBLOCK_SIZE_64X64 ? 64 : 32;
641 642 643
#else
    const unsigned int max = 64;
#endif  // CONFIG_EXT_PARTITION
644 645 646 647 648 649 650 651 652 653 654 655 656
    // If tile size is not set, set it to the default value.
    const unsigned int tc =
        (!extra_cfg->tile_columns) ? UINT_MAX : extra_cfg->tile_columns;
    const unsigned int tr =
        (!extra_cfg->tile_rows) ? UINT_MAX : extra_cfg->tile_rows;

    oxcf->tile_columns = AOMMIN(tc, max);
    oxcf->tile_rows = AOMMIN(tr, max);
  } else {
#endif  // CONFIG_EXT_TILE
    oxcf->tile_columns = extra_cfg->tile_columns;
    oxcf->tile_rows = extra_cfg->tile_rows;
#if CONFIG_EXT_TILE
657 658
  }
#endif  // CONFIG_EXT_TILE
659

660
#if CONFIG_MAX_TILE
661 662 663 664 665 666 667 668
  oxcf->tile_width_count = AOMMIN(cfg->tile_width_count, MAX_TILE_COLS);
  oxcf->tile_height_count = AOMMIN(cfg->tile_height_count, MAX_TILE_ROWS);
  for (int i = 0; i < oxcf->tile_width_count; i++) {
    oxcf->tile_widths[i] = AOMMAX(cfg->tile_widths[i], 1);
  }
  for (int i = 0; i < oxcf->tile_height_count; i++) {
    oxcf->tile_heights[i] = AOMMAX(cfg->tile_heights[i], 1);
  }
669
#endif
670
#if CONFIG_DEPENDENT_HORZTILES
671 672 673 674 675
  oxcf->dependent_horz_tiles =
#if CONFIG_EXT_TILE
      (cfg->large_scale_tile) ? 0 :
#endif  // CONFIG_EXT_TILE
                              extra_cfg->dependent_horz_tiles;
676
#endif
677
#if CONFIG_LOOPFILTERING_ACROSS_TILES
678 679 680 681 682 683
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
  oxcf->loop_filter_across_tiles_v_enabled =
      extra_cfg->loop_filter_across_tiles_v_enabled;
  oxcf->loop_filter_across_tiles_h_enabled =
      extra_cfg->loop_filter_across_tiles_h_enabled;
#else
684 685
  oxcf->loop_filter_across_tiles_enabled =
      extra_cfg->loop_filter_across_tiles_enabled;
686
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
687
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
688
  oxcf->error_resilient_mode = cfg->g_error_resilient;
Jingning Han's avatar
Jingning Han committed
689 690 691
  oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;

  oxcf->aq_mode = extra_cfg->aq_mode;
Fangwen Fu's avatar
Fangwen Fu committed
692 693 694
#if CONFIG_EXT_DELTA_Q
  oxcf->deltaq_mode = extra_cfg->deltaq_mode;
#endif
Jingning Han's avatar
Jingning Han committed
695

696
  oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
Yunqing Wang's avatar
Yunqing Wang committed
697
  oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
Yaowu Xu's avatar
Yaowu Xu committed
698
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
699 700
}

Yaowu Xu's avatar
Yaowu Xu committed
701 702 703
static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
                                          const aom_codec_enc_cfg_t *cfg) {
  aom_codec_err_t res;
Jingning Han's avatar
Jingning Han committed
704 705 706
  int force_key = 0;

  if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
Yaowu Xu's avatar
Yaowu Xu committed
707
    if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
Jingning Han's avatar
Jingning Han committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
      ERROR("Cannot change width or height after initialization");
    if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
        (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
        (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
      force_key = 1;
  }

  // Prevent increasing lag_in_frames. This check is stricter than it needs
  // to be -- the limit is not increasing past the first lag_in_frames
  // value, but we don't track the initial config, only the last successful
  // config.
  if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
    ERROR("Cannot increase lag_in_frames");

  res = validate_config(ctx, cfg, &ctx->extra_cfg);

Yaowu Xu's avatar
Yaowu Xu committed
724
  if (res == AOM_CODEC_OK) {
Jingning Han's avatar
Jingning Han committed
725 726 727 728
    ctx->cfg = *cfg;
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
    // On profile change, request a key frame
    force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
Yaowu Xu's avatar
Yaowu Xu committed
729
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
730 731
  }

Yaowu Xu's avatar
Yaowu Xu committed
732
  if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
Jingning Han's avatar
Jingning Han committed
733 734 735 736

  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
737
static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
738 739
                                          va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
740 741 742
  if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
  *arg = av1_get_quantizer(ctx->cpi);
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
743 744
}

Yaowu Xu's avatar
Yaowu Xu committed
745
static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
746 747
                                            va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
748 749 750
  if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
  *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->cpi));
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
751 752
}

Yaowu Xu's avatar
Yaowu Xu committed
753 754 755 756
static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
                                        const struct av1_extracfg *extra_cfg) {
  const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
  if (res == AOM_CODEC_OK) {
Jingning Han's avatar
Jingning Han committed
757 758
    ctx->extra_cfg = *extra_cfg;
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
Yaowu Xu's avatar
Yaowu Xu committed
759
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
760 761 762 763
  }
  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
764
static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
765
                                        va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
766 767
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
Jingning Han's avatar
Jingning Han committed
768 769 770
  return update_extra_cfg(ctx, &extra_cfg);
}

771 772 773 774 775 776
static aom_codec_err_t ctrl_set_devsf(aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.dev_sf = CAST(AOME_SET_DEVSF, args);
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
777
static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
778
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
779 780
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
Jingning Han's avatar
Jingning Han committed
781 782 783
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
784
static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
785
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
786 787
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
788 789 790
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
791
static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
792
                                                  va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
793 794
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
Jingning Han's avatar
Jingning Han committed
795 796 797
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
798
static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
799
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
800 801
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
Jingning Han's avatar
Jingning Han committed
802 803 804
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
805
static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
806
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
807 808
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
Jingning Han's avatar
Jingning Han committed
809 810 811
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
812
static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
813
                                             va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
814 815
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
Jingning Han's avatar
Jingning Han committed
816 817 818
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
819
static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
820
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
821 822
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
Jingning Han's avatar
Jingning Han committed
823 824
  return update_extra_cfg(ctx, &extra_cfg);
}
825

826 827 828 829 830 831 832 833
#if CONFIG_DEPENDENT_HORZTILES
static aom_codec_err_t ctrl_set_tile_dependent_rows(aom_codec_alg_priv_t *ctx,
                                                    va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.dependent_horz_tiles = CAST(AV1E_SET_TILE_DEPENDENT_ROWS, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
834
#if CONFIG_LOOPFILTERING_ACROSS_TILES
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
static aom_codec_err_t ctrl_set_tile_loopfilter_v(aom_codec_alg_priv_t *ctx,
                                                  va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.loop_filter_across_tiles_v_enabled =
      CAST(AV1E_SET_TILE_LOOPFILTER_V, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
static aom_codec_err_t ctrl_set_tile_loopfilter_h(aom_codec_alg_priv_t *ctx,
                                                  va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.loop_filter_across_tiles_h_enabled =
      CAST(AV1E_SET_TILE_LOOPFILTER_H, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#else
851 852 853 854 855 856 857
static aom_codec_err_t ctrl_set_tile_loopfilter(aom_codec_alg_priv_t *ctx,
                                                va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.loop_filter_across_tiles_enabled =
      CAST(AV1E_SET_TILE_LOOPFILTER, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
858
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
859
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
860

Yaowu Xu's avatar
Yaowu Xu committed
861
static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
862
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
863 864
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
Jingning Han's avatar
Jingning Han committed
865 866 867
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
868
static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
869
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
870 871
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
Jingning Han's avatar
Jingning Han committed
872 873 874
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
875
static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
876
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
877 878
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
Jingning Han's avatar
Jingning Han committed
879 880 881
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
882
static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
883
                                         va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
884 885
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
Jingning Han's avatar
Jingning Han committed
886 887 888
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
889 890 891
static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
    aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
Jingning Han's avatar
Jingning Han committed
892
  extra_cfg.rc_max_intra_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
893
      CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
894 895 896
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
897 898 899
static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
    aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
Jingning Han's avatar
Jingning Han committed
900
  extra_cfg.rc_max_inter_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
901
      CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
902 903 904
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
905
static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
906
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
907 908
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
Jingning Han's avatar
Jingning Han committed
909 910 911
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
912
static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
913
                                         va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
914 915
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
Jingning Han's avatar
Jingning Han committed
916 917 918
  return update_extra_cfg(ctx, &extra_cfg);
}

919 920 921 922 923 924 925
static aom_codec_err_t ctrl_set_enable_cdef(aom_codec_alg_priv_t *ctx,
                                            va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_cdef = CAST(AV1E_SET_ENABLE_CDEF, args);
  return update_extra_cfg(ctx, &extra_cfg);
}

926
#if CONFIG_AOM_QM
Yaowu Xu's avatar
Yaowu Xu committed
927
static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
928
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
929 930
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
931 932 933
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
934
static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
935
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
936 937
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
938 939 940
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
941
static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
942
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
943 944
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
945 946 947
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
948 949 950 951 952 953 954 955
#if CONFIG_DIST_8X8
static aom_codec_err_t ctrl_set_enable_dist_8x8(aom_codec_alg_priv_t *ctx,
                                                va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_dist_8x8 = CAST(AV1E_SET_ENABLE_DIST_8X8, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
956 957 958 959 960 961 962 963 964 965 966 967
static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
                                       va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
  return update_extra_cfg(ctx, &extra_cfg);
}

static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
968 969 970 971 972 973 974 975
#if CONFIG_TEMPMV_SIGNALING
static aom_codec_err_t ctrl_set_disable_tempmv(aom_codec_alg_priv_t *ctx,
                                               va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.disable_tempmv = CAST(AV1E_SET_DISABLE_TEMPMV, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
Yaowu Xu's avatar
Yaowu Xu committed
976 977 978
static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
    aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
Jingning Han's avatar
Jingning Han committed
979
  extra_cfg.frame_parallel_decoding_mode =
Yaowu Xu's avatar
Yaowu Xu committed
980
      CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
Jingning Han's avatar
Jingning Han committed
981 982 983
  return update_extra_cfg(ctx, &extra_cfg);
}

984
#if CONFIG_EXT_TILE
985 986
static aom_codec_err_t ctrl_set_single_tile_decoding(aom_codec_alg_priv_t *ctx,
                                                     va_list args) {
987
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
988
  extra_cfg.single_tile_decoding = CAST(AV1E_SET_SINGLE_TILE_DECODING, args);
989 990 991 992
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif  // CONFIG_EXT_TILE

Yaowu Xu's avatar
Yaowu Xu committed
993
static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
994
                                        va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
995 996
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
Jingning Han's avatar
Jingning Han committed
997 998 999
  return update_extra_cfg(ctx, &extra_cfg);
}

Fangwen Fu's avatar
Fangwen Fu committed
1000 1001 1002 1003 1004 1005 1006 1007
#if CONFIG_EXT_DELTA_Q
static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
                                            va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.deltaq_mode = CAST(AV1E_SET_DELTAQ_MODE, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
Yaowu Xu's avatar
Yaowu Xu committed
1008
static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
1009
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
1010 1011
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
Jingning Han's avatar
Jingning Han committed
1012 1013 1014
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
1015
static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
1016
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
1017 1018
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
Jingning Han's avatar
Jingning Han committed
1019 1020 1021
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
1022
static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
1023
                                                     va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
1024 1025
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
Jingning Han's avatar
Jingning Han committed
1026 1027 1028
  return update_extra_cfg(ctx, &extra_cfg);
}

Yunqing Wang's avatar
Yunqing Wang committed
1029 1030 1031 1032 1033 1034 1035 1036
static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
    aom_codec_alg_priv_t *ctx, va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.motion_vector_unit_test =
      CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
1037 1038 1039
static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx,
                                    aom_codec_priv_enc_mr_cfg_t *data) {
  aom_codec_err_t res = AOM_CODEC_OK;