av1_cx_iface.c 58.5 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
14
 */

#include <stdlib.h>
#include <string.h>

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

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

  unsigned int motion_vector_unit_test;
Jingning Han's avatar
Jingning Han committed
88
89
};

Yaowu Xu's avatar
Yaowu Xu committed
90
static struct av1_extracfg default_extra_cfg = {
91
92
  0,  // cpu_used
  1,  // enable_auto_alt_ref
93
#if CONFIG_EXT_REFS
94
  0,    // enable_auto_bwd_ref
95
#endif  // CONFIG_EXT_REFS
96
97
98
  0,    // noise_sensitivity
  0,    // sharpness
  0,    // static_thresh
99
100
  0,    // tile_columns
  0,    // tile_rows
101
#if CONFIG_DEPENDENT_HORZTILES
102
  0,  // Dependent Horizontal tiles
103
#endif
104
#if CONFIG_LOOPFILTERING_ACROSS_TILES
105
  1,              // loop_filter_across_tiles_enabled
106
#endif            // CONFIG_LOOPFILTERING_ACROSS_TILES
107
108
109
110
  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
111
  AOM_TUNE_PSNR,  // tuning
112
113
114
115
116
117
118
119
120
  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
121
122
123
#endif
  1,  // max number of tile groups
  0,  // mtu_size
124
125
#if CONFIG_TEMPMV_SIGNALING
  0,  // disable temporal mv prediction
126
#endif
Fangwen Fu's avatar
Fangwen Fu committed
127
128
129
130
131
  1,      // frame_parallel_decoding_mode
  NO_AQ,  // aq_mode
#if CONFIG_EXT_DELTA_Q
  NO_DELTA_Q,  // deltaq_mode
#endif
132
133
134
135
136
137
138
139
  CONFIG_XIPHRC,        // frame_periodic_delta_q
  AOM_BITS_8,           // Bit depth
  AOM_CONTENT_DEFAULT,  // content
  AOM_CS_UNKNOWN,       // color space
#if CONFIG_COLORSPACE_HEADERS
  AOM_TF_UNKNOWN,   // transfer function
  AOM_CSP_UNKNOWN,  // chroma sample position
#endif
140
141
142
143
144
145
146
  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
147
#if CONFIG_EXT_TILE
148
  0,    // Single tile decoding is off by default.
149
#endif  // CONFIG_EXT_TILE
Yunqing Wang's avatar
Yunqing Wang committed
150
151

  0,  // motion_vector_unit_test
Jingning Han's avatar
Jingning Han committed
152
153
};

Yaowu Xu's avatar
Yaowu Xu committed
154
155
156
157
158
159
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;
160
161
162
163
164
165
  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
166
167
168
169
  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;
170
  unsigned int fixed_kf_cntr;
Jingning Han's avatar
Jingning Han committed
171
  // BufferPool that holds all reference frames.
172
  BufferPool *buffer_pool;
Jingning Han's avatar
Jingning Han committed
173
174
};

Yaowu Xu's avatar
Yaowu Xu committed
175
176
177
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
178

Yaowu Xu's avatar
Yaowu Xu committed
179
  if (res != AOM_CODEC_OK)
Jingning Han's avatar
Jingning Han committed
180
181
182
183
184
185
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;

  return res;
}

#undef ERROR
186
187
188
#define ERROR(str)                  \
  do {                              \
    ctx->base.err_detail = str;     \
Yaowu Xu's avatar
Yaowu Xu committed
189
    return AOM_CODEC_INVALID_PARAM; \
Jingning Han's avatar
Jingning Han committed
190
191
  } while (0)

192
193
194
195
#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
196
197
  } while (0)

198
199
200
#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
201
202
  } while (0)

203
204
205
#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
206
207
  } while (0)

208
209
210
#define RANGE_CHECK_BOOL(p, memb)                                     \
  do {                                                                \
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
Jingning Han's avatar
Jingning Han committed
211
212
  } while (0)

Yaowu Xu's avatar
Yaowu Xu committed
213
214
215
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) {
216
217
218
219
220
221
222
223
  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
224
  RANGE_CHECK_BOOL(extra_cfg, lossless);
225
  RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 1);
Fangwen Fu's avatar
Fangwen Fu committed
226
227
228
#if CONFIG_EXT_DELTA_Q
  RANGE_CHECK(extra_cfg, deltaq_mode, 0, DELTAQ_MODE_COUNT - 1);
#endif
229
  RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
230
231
  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
232
  RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
233
234
  RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
  RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
Jingning Han's avatar
Jingning Han committed
235
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
Yaowu Xu's avatar
Yaowu Xu committed
236
  RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
237
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
Yaowu Xu's avatar
Yaowu Xu committed
238
  RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
239
240
  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
241
242
243
244
245
  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,
246
                (MAX_LAG_BUFFERS - 1));
Jingning Han's avatar
Jingning Han committed
247
248
  }

249
  RANGE_CHECK_HI(cfg, rc_resize_mode, RESIZE_DYNAMIC);
250
251
  RANGE_CHECK(cfg, rc_resize_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
252
253
  RANGE_CHECK(cfg, rc_resize_kf_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
254
255
#if CONFIG_FRAME_SUPERRES
  RANGE_CHECK_HI(cfg, rc_superres_mode, SUPERRES_DYNAMIC);
256
257
  RANGE_CHECK(cfg, rc_superres_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
258
259
  RANGE_CHECK(cfg, rc_superres_kf_numerator, SCALE_DENOMINATOR / 2,
              SCALE_DENOMINATOR);
260
261
#endif  // CONFIG_FRAME_SUPERRES

Yaowu Xu's avatar
Yaowu Xu committed
262
  // AV1 does not support a lower bound on the keyframe interval in
Jingning Han's avatar
Jingning Han committed
263
  // automatic keyframe placement mode.
Yaowu Xu's avatar
Yaowu Xu committed
264
  if (cfg->kf_mode != AOM_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
Jingning Han's avatar
Jingning Han committed
265
      cfg->kf_min_dist > 0)
266
267
268
    ERROR(
        "kf_min_dist not supported in auto mode, use 0 "
        "or kf_max_dist instead.");
Jingning Han's avatar
Jingning Han committed
269

Yunqing Wang's avatar
Yunqing Wang committed
270
  RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
271
  RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
272
#if CONFIG_EXT_REFS
273
  RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
274
#endif  // CONFIG_EXT_REFS
275
  RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
Jingning Han's avatar
Jingning Han committed
276
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
Yaowu Xu's avatar
Yaowu Xu committed
277
278
  RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
              AOM_SUPERBLOCK_SIZE_DYNAMIC);
279
#if CONFIG_EXT_TILE
280
281
282
283
  RANGE_CHECK_HI(cfg, large_scale_tile, 1);
  RANGE_CHECK_HI(extra_cfg, single_tile_decoding, 1);

  if (cfg->large_scale_tile) {
284
285
286
// 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.
287
#if CONFIG_EXT_PARTITION
288
289
290
291
292
293
294
295
296
297
298
    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
    }
299
#endif  // CONFIG_EXT_PARTITION
300
301
302
303
304
  } else {
#endif  // CONFIG_EXT_TILE
    RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
    RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
#if CONFIG_EXT_TILE
305
  }
306
#endif  // CONFIG_EXT_TILE
307

308
309
310
#if CONFIG_DEPENDENT_HORZTILES
  RANGE_CHECK_HI(extra_cfg, dependent_horz_tiles, 1);
#endif
311
#if CONFIG_LOOPFILTERING_ACROSS_TILES
312
  RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_enabled, 1);
313
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
Jingning Han's avatar
Jingning Han committed
314
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
315
  RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
Jingning Han's avatar
Jingning Han committed
316
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
317
  RANGE_CHECK_HI(extra_cfg, cq_level, 63);
Yaowu Xu's avatar
Yaowu Xu committed
318
  RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
Jingning Han's avatar
Jingning Han committed
319
  RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
Yaowu Xu's avatar
Yaowu Xu committed
320
  RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
Jingning Han's avatar
Jingning Han committed
321

Yaowu Xu's avatar
Yaowu Xu committed
322
323
324
  // 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
325

Yaowu Xu's avatar
Yaowu Xu committed
326
  if (cfg->g_pass == AOM_RC_LAST_PASS) {
327
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
328
329
330
    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;
331
#endif
Jingning Han's avatar
Jingning Han committed
332
333
334
335

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

336
#if !CONFIG_XIPHRC
Jingning Han's avatar
Jingning Han committed
337
338
339
    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
340
341
    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
342

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

Yunqing Wang's avatar
Yunqing Wang committed
346
347
    if ((int)(stats->count + 0.5) != n_packets - 1)
      ERROR("rc_twopass_stats_in missing EOS stats packet");
348
#endif
Jingning Han's avatar
Jingning Han committed
349
350
  }

351
#if !CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
352
353
354
355
356
  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
357
      cfg->g_bit_depth > AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
358
359
    ERROR("Codec high bit-depth not supported in profile < 2");
  }
360
  if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
Jingning Han's avatar
Jingning Han committed
361
362
363
    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
364
      cfg->g_bit_depth == AOM_BITS_8) {
Jingning Han's avatar
Jingning Han committed
365
366
    ERROR("Codec bit-depth 8 not supported in profile > 1");
  }
367
368
369
370
371
372
#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
373
  RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_SRGB);
374
#endif
375
  RANGE_CHECK(extra_cfg, color_range, 0, 1);
376
377
378
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  RANGE_CHECK(extra_cfg, ans_window_size_log2, 8, 23);
#endif
Yaowu Xu's avatar
Yaowu Xu committed
379
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
380
381
}

Yaowu Xu's avatar
Yaowu Xu committed
382
383
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
384
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
385
386
387
388
389
390
    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
391
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
392
393
394
        ERROR(
            "Invalid image format. I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
395
396
      }
      break;
Yaowu Xu's avatar
Yaowu Xu committed
397
398
399
    case AOM_IMG_FMT_I42216:
    case AOM_IMG_FMT_I44416:
    case AOM_IMG_FMT_I44016:
Jingning Han's avatar
Jingning Han committed
400
401
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
          ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
402
403
404
        ERROR(
            "Invalid image format. 16-bit I422, I444, I440 images are "
            "not supported in profile.");
Jingning Han's avatar
Jingning Han committed
405
406
407
      }
      break;
    default:
408
409
410
      ERROR(
          "Invalid image format. Only YV12, I420, I422, I444 images are "
          "supported.");
Jingning Han's avatar
Jingning Han committed
411
412
413
414
415
416
      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
417
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
418
419
}

Yaowu Xu's avatar
Yaowu Xu committed
420
static int get_image_bps(const aom_image_t *img) {
Jingning Han's avatar
Jingning Han committed
421
  switch (img->fmt) {
Yaowu Xu's avatar
Yaowu Xu committed
422
423
424
425
426
427
428
429
430
    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
431
432
433
434
435
    default: assert(0 && "Invalid image format"); break;
  }
  return 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
436
437
438
439
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
440
441
  oxcf->profile = cfg->g_profile;
  oxcf->max_threads = (int)cfg->g_threads;
442
443
  oxcf->width = cfg->g_w;
  oxcf->height = cfg->g_h;
Jingning Han's avatar
Jingning Han committed
444
445
446
447
  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;
448
  if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
Jingning Han's avatar
Jingning Han committed
449
450
451
452

  oxcf->mode = GOOD;

  switch (cfg->g_pass) {
Yaowu Xu's avatar
Yaowu Xu committed
453
454
455
    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
456
457
  }

458
  oxcf->lag_in_frames =
Yaowu Xu's avatar
Yaowu Xu committed
459
      cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
Jingning Han's avatar
Jingning Han committed
460
461
462
463
464
465
466
467
468
  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
469
      extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
Jingning Han's avatar
Jingning Han committed
470
  oxcf->worst_allowed_q =
Yaowu Xu's avatar
Yaowu Xu committed
471
472
      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
473
474
  oxcf->fixed_q = -1;

475
476
477
478
479
480
#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

481
  oxcf->num_tile_groups = extra_cfg->num_tg;
482
483
484
485
#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
486
487
  oxcf->mtu = extra_cfg->mtu_size;

488
489
490
#if CONFIG_TEMPMV_SIGNALING
  oxcf->disable_tempmv = extra_cfg->disable_tempmv;
#endif
491
492
  oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
  oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
Jingning Han's avatar
Jingning Han committed
493

494
495
  oxcf->resize_mode = (RESIZE_MODE)cfg->rc_resize_mode;
  oxcf->resize_scale_numerator = (uint8_t)cfg->rc_resize_numerator;
496
  oxcf->resize_kf_scale_numerator = (uint8_t)cfg->rc_resize_kf_numerator;
497
  if (oxcf->resize_mode == RESIZE_FIXED &&
498
499
      oxcf->resize_scale_numerator == SCALE_DENOMINATOR &&
      oxcf->resize_kf_scale_numerator == SCALE_DENOMINATOR)
Jingning Han's avatar
Jingning Han committed
500
    oxcf->resize_mode = RESIZE_NONE;
501
502

#if CONFIG_FRAME_SUPERRES
503
504
  oxcf->superres_mode = (SUPERRES_MODE)cfg->rc_superres_mode;
  oxcf->superres_scale_numerator = (uint8_t)cfg->rc_superres_numerator;
505
  oxcf->superres_kf_scale_numerator = (uint8_t)cfg->rc_superres_kf_numerator;
506
  if (oxcf->superres_mode == SUPERRES_FIXED &&
507
508
      oxcf->superres_scale_numerator == SCALE_DENOMINATOR &&
      oxcf->superres_kf_scale_numerator == SCALE_DENOMINATOR)
509
510
    oxcf->superres_mode = SUPERRES_NONE;
#endif  // CONFIG_FRAME_SUPERRES
511

512
  oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
Jingning Han's avatar
Jingning Han committed
513
  oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
514
  oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
Jingning Han's avatar
Jingning Han committed
515

516
  oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
Jingning Han's avatar
Jingning Han committed
517

518
519
520
  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
521

522
  oxcf->auto_key =
Yaowu Xu's avatar
Yaowu Xu committed
523
      cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
524

525
  oxcf->key_freq = cfg->kf_max_dist;
Jingning Han's avatar
Jingning Han committed
526

527
  oxcf->speed = extra_cfg->cpu_used;
528
  oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
529
#if CONFIG_EXT_REFS
530
  oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
531
#endif  // CONFIG_EXT_REFS
532
533
  oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
  oxcf->sharpness = extra_cfg->sharpness;
Jingning Han's avatar
Jingning Han committed
534

535
  oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
Jingning Han's avatar
Jingning Han committed
536
537

#if CONFIG_FP_MB_STATS
538
  oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
Jingning Han's avatar
Jingning Han committed
539
540
541
#endif

  oxcf->color_space = extra_cfg->color_space;
542
543
544
545
#if CONFIG_COLORSPACE_HEADERS
  oxcf->transfer_function = extra_cfg->transfer_function;
  oxcf->chroma_sample_position = extra_cfg->chroma_sample_position;
#endif
546
  oxcf->color_range = extra_cfg->color_range;
547
  oxcf->render_width = extra_cfg->render_width;
548
  oxcf->render_height = extra_cfg->render_height;
Jingning Han's avatar
Jingning Han committed
549
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
550
  oxcf->arnr_strength = extra_cfg->arnr_strength;
Jingning Han's avatar
Jingning Han committed
551
552
553
554
555
556
  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;

557
558
559
#if CONFIG_EXT_PARTITION
  oxcf->superblock_size = extra_cfg->superblock_size;
#endif  // CONFIG_EXT_PARTITION
560
561
562
#if CONFIG_ANS && ANS_MAX_SYMBOLS
  oxcf->ans_window_size_log2 = extra_cfg->ans_window_size_log2;
#endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
563
564

#if CONFIG_EXT_TILE
565
566
567
568
  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) {
569
570
#if CONFIG_EXT_PARTITION
    const unsigned int max =
Yaowu Xu's avatar
Yaowu Xu committed
571
        extra_cfg->superblock_size == AOM_SUPERBLOCK_SIZE_64X64 ? 64 : 32;
572
573
574
#else
    const unsigned int max = 64;
#endif  // CONFIG_EXT_PARTITION
575
576
577
578
579
580
581
582
583
584
585
586
587
    // 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
588
589
  }
#endif  // CONFIG_EXT_TILE
590

591
#if CONFIG_DEPENDENT_HORZTILES
592
593
594
595
596
  oxcf->dependent_horz_tiles =
#if CONFIG_EXT_TILE
      (cfg->large_scale_tile) ? 0 :
#endif  // CONFIG_EXT_TILE
                              extra_cfg->dependent_horz_tiles;
597
#endif
598
#if CONFIG_LOOPFILTERING_ACROSS_TILES
599
600
  oxcf->loop_filter_across_tiles_enabled =
      extra_cfg->loop_filter_across_tiles_enabled;
601
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
602
  oxcf->error_resilient_mode = cfg->g_error_resilient;
Jingning Han's avatar
Jingning Han committed
603
604
605
  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
606
607
608
#if CONFIG_EXT_DELTA_Q
  oxcf->deltaq_mode = extra_cfg->deltaq_mode;
#endif
Jingning Han's avatar
Jingning Han committed
609

610
  oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
Jingning Han's avatar
Jingning Han committed
611

Yunqing Wang's avatar
Yunqing Wang committed
612
  oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
Jingning Han's avatar
Jingning Han committed
613
  /*
Yaowu Xu's avatar
Yaowu Xu committed
614
  printf("Current AV1 Settings: \n");
Jingning Han's avatar
Jingning Han committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
  printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
  printf("sharpness: %d\n",    oxcf->sharpness);
  printf("cpu_used: %d\n",  oxcf->cpu_used);
  printf("Mode: %d\n",     oxcf->mode);
  printf("auto_key: %d\n",  oxcf->auto_key);
  printf("key_freq: %d\n", oxcf->key_freq);
  printf("end_usage: %d\n", oxcf->end_usage);
  printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
  printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
  printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
  printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
  printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
  printf("fixed_q: %d\n",  oxcf->fixed_q);
  printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
  printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
  printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
  printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
  printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
  printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
  printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
  printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
  printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
  printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
  printf("Version: %d\n", oxcf->Version);
  printf("error resilient: %d\n", oxcf->error_resilient_mode);
  printf("frame parallel detokenization: %d\n",
         oxcf->frame_parallel_decoding_mode);
  */
Yaowu Xu's avatar
Yaowu Xu committed
644
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
645
646
}

Yaowu Xu's avatar
Yaowu Xu committed
647
648
649
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
650
651
652
  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
653
    if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
Jingning Han's avatar
Jingning Han committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
      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
670
  if (res == AOM_CODEC_OK) {
Jingning Han's avatar
Jingning Han committed
671
672
673
674
    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
675
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
676
677
  }

Yaowu Xu's avatar
Yaowu Xu committed
678
  if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
Jingning Han's avatar
Jingning Han committed
679
680
681
682

  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
683
static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
684
685
                                          va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
686
687
688
  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
689
690
}

Yaowu Xu's avatar
Yaowu Xu committed
691
static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
692
693
                                            va_list args) {
  int *const arg = va_arg(args, int *);
Yaowu Xu's avatar
Yaowu Xu committed
694
695
696
  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
697
698
}

Yaowu Xu's avatar
Yaowu Xu committed
699
700
701
702
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
703
704
    ctx->extra_cfg = *extra_cfg;
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
Yaowu Xu's avatar
Yaowu Xu committed
705
    av1_change_config(ctx->cpi, &ctx->oxcf);
Jingning Han's avatar
Jingning Han committed
706
707
708
709
  }
  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
710
static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
711
                                        va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
712
713
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
Jingning Han's avatar
Jingning Han committed
714
715
716
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
717
static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
718
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
719
720
  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
721
722
723
  return update_extra_cfg(ctx, &extra_cfg);
}

724
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
725
static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
726
                                                    va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
727
728
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
729
730
  return update_extra_cfg(ctx, &extra_cfg);
}
731
#endif  // CONFIG_EXT_REFS
732

Yaowu Xu's avatar
Yaowu Xu committed
733
static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
734
                                                  va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
735
736
  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
737
738
739
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
740
static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
741
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
742
743
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
Jingning Han's avatar
Jingning Han committed
744
745
746
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
747
static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
748
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
749
750
  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
751
752
753
  return update_extra_cfg(ctx, &extra_cfg);
}

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

Yaowu Xu's avatar
Yaowu Xu committed
761
static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
762
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
763
764
  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
765
766
  return update_extra_cfg(ctx, &extra_cfg);
}
767
768
769
770
771
772
773
774
#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
775
#if CONFIG_LOOPFILTERING_ACROSS_TILES
776
777
778
779
780
781
782
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);
}
783
#endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
784

Yaowu Xu's avatar
Yaowu Xu committed
785
static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
786
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
787
788
  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
789
790
791
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
792
static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
793
                                              va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
794
795
  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
796
797
798
  return update_extra_cfg(ctx, &extra_cfg);
}

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

Yaowu Xu's avatar
Yaowu Xu committed
806
static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
807
                                         va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
808
809
  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
810
811
812
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
813
814
815
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
816
  extra_cfg.rc_max_intra_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
817
      CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
818
819
820
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
821
822
823
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
824
  extra_cfg.rc_max_inter_bitrate_pct =
Yaowu Xu's avatar
Yaowu Xu committed
825
      CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
Jingning Han's avatar
Jingning Han committed
826
827
828
  return update_extra_cfg(ctx, &extra_cfg);
}

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

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

843
#if CONFIG_AOM_QM
Yaowu Xu's avatar
Yaowu Xu committed
844
static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
845
                                          va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
846
847
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
848
849
850
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
851
static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
852
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
853
854
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
855
856
857
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
858
static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
859
                                       va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
860
861
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
  extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
862
863
864
865
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif

866
867
868
869
870
871
872
873
874
875
876
877
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);
}
878
879
880
881
882
883
884
885
#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
886
887
888
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
889
  extra_cfg.frame_parallel_decoding_mode =
Yaowu Xu's avatar
Yaowu Xu committed
890
      CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
Jingning Han's avatar
Jingning Han committed
891
892
893
  return update_extra_cfg(ctx, &extra_cfg);
}

894
#if CONFIG_EXT_TILE
895
896
static aom_codec_err_t ctrl_set_single_tile_decoding(aom_codec_alg_priv_t *ctx,
                                                     va_list args) {
897
  struct av1_extracfg extra_cfg = ctx->extra_cfg;
898
  extra_cfg.single_tile_decoding = CAST(AV1E_SET_SINGLE_TILE_DECODING, args);
899
900
901
902
  return update_extra_cfg(ctx, &extra_cfg);
}
#endif  // CONFIG_EXT_TILE

Yaowu Xu's avatar
Yaowu Xu committed
903
static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
904
                                        va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
905
906
  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
907
908
909
  return update_extra_cfg(ctx, &extra_cfg);
}

Fangwen Fu's avatar
Fangwen Fu committed
910
911
912
913
914
915
916
917
#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
918
static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
919
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
920
921
  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
922
923
924
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
925
static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
926
                                                va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
927
928
  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
929
930
931
  return update_extra_cfg(ctx, &extra_cfg);
}

Yaowu Xu's avatar
Yaowu Xu committed
932
static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
933
                                                     va_list args) {
Yaowu Xu's avatar
Yaowu Xu committed
934
935
  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
936
937
938
  return update_extra_cfg(ctx, &extra_cfg);
}

Yunqing Wang's avatar
Yunqing Wang committed
939
940
941
942
943
944
945
946
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
947
948
949
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;
Jingning Han's avatar
Jingning Han committed
950
951
952
  (void)data;

  if (ctx->priv == NULL) {
Yaowu Xu's avatar
Yaowu Xu committed
953
954
    aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
    if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
955

Yaowu Xu's avatar
Yaowu Xu committed
956
    ctx->priv = (aom_codec_priv_t *)priv;
Jingning Han's avatar
Jingning Han committed
957
958
    ctx->priv->init_flags = ctx->init_flags;
    ctx->priv->enc.total_encoders = 1;
Yaowu Xu's avatar
Yaowu Xu committed
959
960
    priv->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
    if (priv->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
961
962
963

#if CONFIG_MULTITHREAD
    if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
Yaowu Xu's avatar
Yaowu Xu committed
964
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
965
966
967
968
969
970
971
972
973
974
    }
#endif

    if (ctx->config.enc) {
      // Update the reference to the config structure to an internal copy.
      priv->cfg = *ctx->config.enc;
      ctx->config.enc = &priv->cfg;
    }

    priv->extra_cfg = default_extra_cfg;
Yaowu Xu's avatar
Yaowu Xu committed
975
    once(av1_initialize_enc);
Jingning Han's avatar
Jingning Han committed
976
977
978

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

Yaowu Xu's avatar
Yaowu Xu committed
979
    if (res == AOM_CODEC_OK) {
Jingning Han's avatar
Jingning Han committed
980
      set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
981
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
982
      priv->oxcf.use_highbitdepth =
Yaowu Xu's avatar
Yaowu Xu committed
983
          (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
Jingning Han's avatar
Jingning Han committed
984
#endif
Yaowu Xu's avatar
Yaowu Xu committed
985
      priv->cpi = av1_create_compressor(&priv->oxcf, priv->buffer_pool);
Jingning Han's avatar
Jingning Han committed
986
      if (priv->cpi == NULL)
Yaowu Xu's avatar
Yaowu Xu committed
987
        res = AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
988
989
990
991
992
993
994
995
      else
        priv->cpi->output_pkt_list = &priv->pkt_list.head;
    }
  }

  return res;
}

Yaowu Xu's avatar
Yaowu Xu committed
996
static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {