av1_cx_iface.c 60.4 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"
Jingning Han's avatar
Jingning Han committed
24

25 26 27
#define MAG_SIZE (4)
#define MAX_INDEX_SIZE (256)

Yaowu Xu's avatar
Yaowu Xu committed
28
struct av1_extracfg {
29 30
  int cpu_used;  // available cpu percentage in 1/16
  unsigned int enable_auto_alt_ref;
31
#if CONFIG_EXT_REFS
32
  unsigned int enable_auto_bwd_ref;
33
#endif  // CONFIG_EXT_REFS
34 35 36
  unsigned int noise_sensitivity;
  unsigned int sharpness;
  unsigned int static_thresh;
37 38 39 40 41 42
  unsigned int tile_columns;  // log2 number of tile columns
  unsigned int tile_rows;     // log2 number of tile rows
#if CONFIG_MAX_TILE
  unsigned int tile_width;   // tile width in superblocks  (if non zero)
  unsigned int tile_height;  // tile height in superblocks (if non zero)
#endif
43 44 45
#if CONFIG_DEPENDENT_HORZTILES
  unsigned int dependent_horz_tiles;
#endif
46
#if CONFIG_LOOPFILTERING_ACROSS_TILES
47
  unsigned int loop_filter_across_tiles_enabled;
48
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
49 50 51 52
  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
53
  aom_tune_metric tuning;
54 55 56 57 58
  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;
59 60 61 62
#if CONFIG_AOM_QM
  unsigned int enable_qm;
  unsigned int qm_min;
  unsigned int qm_max;
63 64 65
#endif
#if CONFIG_DIST_8X8
  unsigned int enable_dist_8x8;
66 67 68
#endif
  unsigned int num_tg;
  unsigned int mtu_size;
69 70
#if CONFIG_TEMPMV_SIGNALING
  unsigned int disable_tempmv;
71
#endif
72 73
  unsigned int frame_parallel_decoding_mode;
  AQ_MODE aq_mode;
Fangwen Fu's avatar
Fangwen Fu committed
74 75 76
#if CONFIG_EXT_DELTA_Q
  DELTAQ_MODE deltaq_mode;
#endif
77
  unsigned int frame_periodic_boost;
Yaowu Xu's avatar
Yaowu Xu committed
78 79 80
  aom_bit_depth_t bit_depth;
  aom_tune_content content;
  aom_color_space_t color_space;
81 82
  aom_transfer_function_t transfer_function;
  aom_chroma_sample_position_t chroma_sample_position;
83 84 85
  int color_range;
  int render_width;
  int render_height;
Yaowu Xu's avatar
Yaowu Xu committed
86
  aom_superblock_size_t superblock_size;
87 88 89
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  int ans_window_size_log2;
#endif
90
#if CONFIG_EXT_TILE
91
  unsigned int single_tile_decoding;
92
#endif  // CONFIG_EXT_TILE
Yunqing Wang's avatar
Yunqing Wang committed
93 94

  unsigned int motion_vector_unit_test;
Jingning Han's avatar
Jingning Han committed
95 96
};

Yaowu Xu's avatar
Yaowu Xu committed
97
static struct av1_extracfg default_extra_cfg = {
98 99
  0,  // cpu_used
  1,  // enable_auto_alt_ref
100
#if CONFIG_EXT_REFS
101
  0,    // enable_auto_bwd_ref
102
#endif  // CONFIG_EXT_REFS
103 104 105
  0,    // noise_sensitivity
  0,    // sharpness
  0,    // static_thresh
106 107
  0,    // tile_columns
  0,    // tile_rows
108 109 110 111
#if CONFIG_MAX_TILE
  0,  // tile_width
  0,  // tile_height
#endif
112
#if CONFIG_DEPENDENT_HORZTILES
113
  0,  // Dependent Horizontal tiles
114
#endif
115
#if CONFIG_LOOPFILTERING_ACROSS_TILES
116
  1,              // loop_filter_across_tiles_enabled
117
#endif            // CONFIG_LOOPFILTERING_ACROSS_TILES
118 119 120 121
  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
122
  AOM_TUNE_PSNR,  // tuning
123 124 125 126 127 128 129 130 131
  10,             // cq_level
  0,              // rc_max_intra_bitrate_pct
  0,              // rc_max_inter_bitrate_pct
  0,              // gf_cbr_boost_pct
  0,              // lossless
#if CONFIG_AOM_QM
  0,                 // enable_qm
  DEFAULT_QM_FIRST,  // qm_min
  DEFAULT_QM_LAST,   // qm_max
132 133 134
#endif
#if CONFIG_DIST_8X8
  0,
135 136 137
#endif
  1,  // max number of tile groups
  0,  // mtu_size
138 139
#if CONFIG_TEMPMV_SIGNALING
  0,  // disable temporal mv prediction
140
#endif
Fangwen Fu's avatar
Fangwen Fu committed
141 142 143 144 145
  1,      // frame_parallel_decoding_mode
  NO_AQ,  // aq_mode
#if CONFIG_EXT_DELTA_Q
  NO_DELTA_Q,  // deltaq_mode
#endif
146 147 148 149 150 151
  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
152 153 154 155 156 157 158
  0,                            // color range
  0,                            // render width
  0,                            // render height
  AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  23,  // ans_window_size_log2
#endif
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
  RANGE_CHECK(cfg, rc_resize_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
264 265
  RANGE_CHECK(cfg, rc_resize_kf_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
266
#if CONFIG_FRAME_SUPERRES
267
  RANGE_CHECK_HI(cfg, rc_superres_mode, SUPERRES_MODES - 1);
268 269
  RANGE_CHECK(cfg, rc_superres_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
270 271
  RANGE_CHECK(cfg, rc_superres_kf_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
272 273
  RANGE_CHECK(cfg, rc_superres_qthresh, 1, 63);
  RANGE_CHECK(cfg, rc_superres_kf_qthresh, 1, 63);
274 275
#endif  // CONFIG_FRAME_SUPERRES

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
  RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
286
#if CONFIG_EXT_REFS
287
  RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
288
#endif  // CONFIG_EXT_REFS
289
  RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
Jingning Han's avatar
Jingning Han committed
290
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
Yaowu Xu's avatar
Yaowu Xu committed
291 292
  RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
              AOM_SUPERBLOCK_SIZE_DYNAMIC);
293
#if CONFIG_EXT_TILE
294 295 296 297
  RANGE_CHECK_HI(cfg, large_scale_tile, 1);
  RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);

  if (cfg->large_scale_tile) {
298 299 300
// 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.
301
#if CONFIG_EXT_PARTITION
302 303 304 305 306 307 308 309 310 311 312
    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
    }
313
#endif  // CONFIG_EXT_PARTITION
314 315
  } else {
#endif  // CONFIG_EXT_TILE
316
#if CONFIG_MAX_TILE
317
    RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
318
    RANGE_CHECK_HI(extra_cfg, tile_rows, 6);
319
    RANGE_CHECK_HI(extra_cfg, tile_width, MAX_TILE_WIDTH_SB);
320 321 322 323
#else   // CONFIG_MAX_TILE
  RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
  RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
#endif  // CONFIG_MAX_TILE
324
#if CONFIG_EXT_TILE
325
  }
326
#endif  // CONFIG_EXT_TILE
327

328 329 330
#if CONFIG_DEPENDENT_HORZTILES
  RANGE_CHECK_HI(extra_cfg, dependent_horz_tiles, 1);
#endif
331
#if CONFIG_LOOPFILTERING_ACROSS_TILES
332
  RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_enabled, 1);
333
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
Jingning Han's avatar
Jingning Han committed
334
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
335
  RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
Jingning Han's avatar
Jingning Han committed
336
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
337
  RANGE_CHECK_HI(extra_cfg, cq_level, 63);
Yaowu Xu's avatar
Yaowu Xu committed
338
  RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
Jingning Han's avatar
Jingning Han committed
339
  RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
Yaowu Xu's avatar
Yaowu Xu committed
340
  RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
Jingning Han's avatar
Jingning Han committed
341

Yaowu Xu's avatar
Yaowu Xu committed
342 343 344
  // 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
345

346 347 348 349 350 351 352 353
// TODO(anybody) : remove this flag when PVQ supports pallete coding tool
#if CONFIG_PVQ
  if (extra_cfg->content == AOM_CONTENT_SCREEN)
    ERROR(
        "Option --tune-content=screen is not currently supported when PVQ is "
        "enabled.");
#endif  // CONFIG_PVQ

Yaowu Xu's avatar
Yaowu Xu committed
354
  if (cfg->g_pass == AOM_RC_LAST_PASS) {
355
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
356 357 358
    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;
359
#endif
Jingning Han's avatar
Jingning Han committed
360 361 362 363

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

364
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
365 366 367
    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
368 369
    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
370

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

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

379
#if !CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
380 381 382 383 384
  if (cfg->g_profile > (unsigned int)PROFILE_1) {
    ERROR("Profile > 1 not supported in this build configuration");
  }
#endif
  if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
Yaowu Xu's avatar
Yaowu Xu committed
385
      cfg->g_bit_depth > AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
386 387
    ERROR("Codec high bit-depth not supported in profile < 2");
  }
388
  if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
Jingning Han's avatar
Jingning Han committed
389 390 391
    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
392
      cfg->g_bit_depth == AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
393 394
    ERROR("Codec bit-depth 8 not supported in profile > 1");
  }
395 396 397 398 399 400
#if CONFIG_COLORSPACE_HEADERS
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_ICTCP);
  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);
#else
Yaowu Xu's avatar
Yaowu Xu committed
401
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_SRGB);
402
#endif
403
  RANGE_CHECK(extra_cfg, color_range, 0, 1);
404 405 406
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  RANGE_CHECK(extra_cfg, ans_window_size_log2, 8, 23);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
407
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
408 409
}

Yaowu Xu's avatar
Yaowu Xu committed
410 411
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
412
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
413 414 415 416 417 418
    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
419
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
420 421 422
        ERROR(
            "Invalid image format. I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
423 424
      }
      break;
Yaowu Xu's avatar
Yaowu Xu committed
425 426 427
    case AOM_IMG_FMT_I42216:
    case AOM_IMG_FMT_I44416:
    case AOM_IMG_FMT_I44016:
Jingning Han's avatar
Jingning Han committed
428 429
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
          ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
430 431 432
        ERROR(
            "Invalid image format. 16-bit I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
433 434 435
      }
      break;
    default:
436 437 438
      ERROR(
          "Invalid image format. Only YV12, I420, I422, I444 images are "
          "supported.");
Jingning Han's avatar
Jingning Han committed
439 440 441 442 443 444
      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
445
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
446 447
}

Yaowu Xu's avatar
Yaowu Xu committed
448
static int get_image_bps(const aom_image_t *img) {
Jingning Han's avatar
Jingning Han committed
449
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
450 451 452 453 454 455 456 457 458
    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
459 460 461 462 463
    default: assert(0 && "Invalid image format"); break;
  }
  return 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
464 465 466 467
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
468 469
  oxcf->profile = cfg->g_profile;
  oxcf->max_threads = (int)cfg->g_threads;
470 471
  oxcf->width = cfg->g_w;
  oxcf->height = cfg->g_h;
Jingning Han's avatar
Jingning Han committed
472 473 474 475
  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;
476
  if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
Jingning Han's avatar
Jingning Han committed
477 478 479 480

  oxcf->mode = GOOD;

  switch (cfg->g_pass) {
Yaowu Xu's avatar
Yaowu Xu committed
481 482 483
    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
484 485
  }

486
  oxcf->lag_in_frames =
Yaowu Xu's avatar
Yaowu Xu committed
487
      cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
Jingning Han's avatar
Jingning Han committed
488 489 490 491 492 493 494 495 496
  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
497
      extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
Jingning Han's avatar
Jingning Han committed
498
  oxcf->worst_allowed_q =
Yaowu Xu's avatar
Yaowu Xu committed
499 500
      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
501 502
  oxcf->fixed_q = -1;

503 504 505 506 507
#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
508 509 510 511 512 513
#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
514
  oxcf->num_tile_groups = extra_cfg->num_tg;
515 516 517 518
#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
519 520
  oxcf->mtu = extra_cfg->mtu_size;

521 522 523
#if CONFIG_TEMPMV_SIGNALING
  oxcf->disable_tempmv = extra_cfg->disable_tempmv;
#endif
524 525
  oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
  oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
Jingning Han's avatar
Jingning Han committed
526

527 528
  oxcf->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
  oxcf->resize_scale_numerator = (uint8_t)cfg->rc_resize_numerator;
529
  oxcf->resize_kf_scale_numerator = (uint8_t)cfg->rc_resize_kf_numerator;
530
  if (oxcf->resize_mode == RESIZE_FIXED &&
531 532
      oxcf->resize_scale_numerator == SCALE_DENOMINATOR &&
      oxcf->resize_kf_scale_numerator == SCALE_DENOMINATOR)
Jingning Han's avatar
Jingning Han committed
533
    oxcf->resize_mode = RESIZE_NONE;
534 535

#if CONFIG_FRAME_SUPERRES
536 537
  oxcf->superres_mode = (SUPERRES_MODE)cfg->rc_superres_mode;
  oxcf->superres_scale_numerator = (uint8_t)cfg->rc_superres_numerator;
538
  oxcf->superres_kf_scale_numerator = (uint8_t)cfg->rc_superres_kf_numerator;
539 540 541 542 543 544 545
  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);
546
  if (oxcf->superres_mode == SUPERRES_FIXED &&
547 548
      oxcf->superres_scale_numerator == SCALE_DENOMINATOR &&
      oxcf->superres_kf_scale_numerator == SCALE_DENOMINATOR)
549
    oxcf->superres_mode = SUPERRES_NONE;
550 551 552
  if (oxcf->superres_mode == SUPERRES_QTHRESH &&
      oxcf->superres_qthresh == 255 && oxcf->superres_kf_qthresh == 255)
    oxcf->superres_mode = SUPERRES_NONE;
553
#endif  // CONFIG_FRAME_SUPERRES
554

555
  oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
Jingning Han's avatar
Jingning Han committed
556
  oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
557
  oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
Jingning Han's avatar
Jingning Han committed
558

559
  oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
Jingning Han's avatar
Jingning Han committed
560

561 562 563
  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
564

565
  oxcf->auto_key =
Yaowu Xu's avatar
Yaowu Xu committed
566
      cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
567

568
  oxcf->key_freq = cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
569

570
  oxcf->speed = extra_cfg->cpu_used;
571
  oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
572
#if CONFIG_EXT_REFS
573
  oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
574
#endif  // CONFIG_EXT_REFS
575 576
  oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
  oxcf->sharpness = extra_cfg->sharpness;
Jingning Han's avatar
Jingning Han committed
577

578
  oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
Jingning Han's avatar
Jingning Han committed
579 580

#if CONFIG_FP_MB_STATS
581
  oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
Jingning Han's avatar
Jingning Han committed
582 583 584
#endif

  oxcf->color_space = extra_cfg->color_space;
585

586 587 588
#if CONFIG_COLORSPACE_HEADERS
  oxcf->transfer_function = extra_cfg->transfer_function;
  oxcf->chroma_sample_position = extra_cfg->chroma_sample_position;
589 590 591 592 593
#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;
594
#endif
595

596
  oxcf->color_range = extra_cfg->color_range;
597
  oxcf->render_width = extra_cfg->render_width;
598
  oxcf->render_height = extra_cfg->render_height;
Jingning Han's avatar
Jingning Han committed
599
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
600
  oxcf->arnr_strength = extra_cfg->arnr_strength;
Jingning Han's avatar
Jingning Han committed
601 602 603 604 605 606
  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;

607 608 609
#if CONFIG_EXT_PARTITION
  oxcf->superblock_size = extra_cfg->superblock_size;
#endif  // CONFIG_EXT_PARTITION
610 611 612
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  oxcf->ans_window_size_log2 = extra_cfg->ans_window_size_log2;
#endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
613 614

#if CONFIG_EXT_TILE
615 616 617 618
  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) {
619 620
#if CONFIG_EXT_PARTITION
    const unsigned int max =
Yaowu Xu's avatar
Yaowu Xu committed
621
        extra_cfg->superblock_size == AOM_SUPERBLOCK_SIZE_64X64 ? 64 : 32;
622 623 624
#else
    const unsigned int max = 64;
#endif  // CONFIG_EXT_PARTITION
625 626 627 628 629 630 631 632 633 634 635 636 637
    // 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
638 639
  }
#endif  // CONFIG_EXT_TILE
640

641 642 643 644
#if CONFIG_MAX_TILE
  oxcf->tile_width = extra_cfg->tile_width;
  oxcf->tile_height = extra_cfg->tile_height;
#endif
645
#if CONFIG_DEPENDENT_HORZTILES
646 647 648 649 650
  oxcf->dependent_horz_tiles =
#if CONFIG_EXT_TILE
      (cfg->large_scale_tile) ? 0 :
#endif  // CONFIG_EXT_TILE
                              extra_cfg->dependent_horz_tiles;
651
#endif
652
#if CONFIG_LOOPFILTERING_ACROSS_TILES
653 654
  oxcf->loop_filter_across_tiles_enabled =
      extra_cfg->loop_filter_across_tiles_enabled;
655
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
656
  oxcf->error_resilient_mode = cfg->g_error_resilient;
Jingning Han's avatar
Jingning Han committed
657 658 659
  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
660 661 662
#if CONFIG_EXT_DELTA_Q
  oxcf->deltaq_mode = extra_cfg->deltaq_mode;
#endif
Jingning Han's avatar
Jingning Han committed
663

664
  oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
Yunqing Wang's avatar
Yunqing Wang committed
665
  oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
Yaowu Xu's avatar
Yaowu Xu committed
666
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
667 668
}

Yaowu Xu's avatar
Yaowu Xu committed
669 670 671
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
672 673 674
  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
675
    if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
Jingning Han's avatar
Jingning Han committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
      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
692
  if (res == AOM_CODEC_OK) {
Jingning Han's avatar
Jingning Han committed
693 694 695 696
    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
697
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
698 699
  }

Yaowu Xu's avatar
Yaowu Xu committed
700
  if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
Jingning Han's avatar
Jingning Han committed
701 702 703 704

  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
705
static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
706 707
                                          va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
708 709 710
  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
711 712
}

Yaowu Xu's avatar
Yaowu Xu committed
713
static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
714 715
                                            va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
716 717 718
  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
719 720
}

Yaowu Xu's avatar
Yaowu Xu committed
721 722 723 724
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
725 726
    ctx->extra_cfg = *extra_cfg;
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
Yaowu Xu's avatar
Yaowu Xu committed
727
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
728 729 730 731
  }
  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
732
static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
733
                                        va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
734 735
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
Jingning Han's avatar
Jingning Han committed
736 737 738
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
739
static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
740
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
741 742
  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
743 744 745
  return update_extra_cfg(ctx, &extra_cfg);
}

746
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
747
static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
748
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
749 750
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
751 752
  return update_extra_cfg(ctx, &extra_cfg);
}
753
#endif  // CONFIG_EXT_REFS
754

Yaowu Xu's avatar
Yaowu Xu committed
755
static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
756
                                                  va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
757 758
  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
759 760 761
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
762
static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
763
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
764 765
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
Jingning Han's avatar
Jingning Han committed
766 767 768
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
769
static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
770
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
771 772
  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
773 774 775
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
776
static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
777
                                             va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
778 779
  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
780 781 782
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
783
static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
784
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
785 786
  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
787 788
  return update_extra_cfg(ctx, &extra_cfg);
}
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

#if CONFIG_MAX_TILE
static aom_codec_err_t ctrl_set_tile_width(aom_codec_alg_priv_t *ctx,
                                           va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tile_width = CAST(AV1E_SET_TILE_WIDTH, args);
  return update_extra_cfg(ctx, &extra_cfg);
}

static aom_codec_err_t ctrl_set_tile_height(aom_codec_alg_priv_t *ctx,
                                            va_list args) {
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tile_height = CAST(AV1E_SET_TILE_HEIGHT, args);
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif
805 806 807 808 809 810 811 812
#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
813
#if CONFIG_LOOPFILTERING_ACROSS_TILES
814 815 816 817 818 819 820
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);
}
821
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
822

Yaowu Xu's avatar
Yaowu Xu committed
823
static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
824
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
825 826
  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
827 828 829
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
830
static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
831
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
832 833
  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
834 835 836
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
837
static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
838
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
839 840
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
Jingning Han's avatar
Jingning Han committed
841 842 843
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
844
static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
845
                                         va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
846 847
  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
848 849 850
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
851 852 853
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
854
  extra_cfg.rc_max_intra_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
855
      CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
856 857 858
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
859 860 861
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
862
  extra_cfg.rc_max_inter_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
863
      CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
864 865 866
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
867
static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
868
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
869 870
  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
871 872 873
  return update_extra_cfg(ctx, &extra_cfg);
}

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

881
#if CONFIG_AOM_QM
Yaowu Xu's avatar
Yaowu Xu committed
882
static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
883
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
884 885
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
886 887 888
  return update_extra_cfg(ctx, &extra_cfg);
}

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

Yaowu Xu's avatar
Yaowu Xu committed
896
static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
897
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
898 899
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
900 901 902
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif