Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/vp9_cx_iface.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include <assert.h>
12
#include <limits.h>
13
#include <math.h>
14
#include <stddef.h>
15
#include <stdint.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
#include "./vpx_config.h"
20
#include "vpx/vpx_encoder.h"
21
#include "vpx/vpx_image.h"
22
#include "vpx/vpx_ext_ratectrl.h"
23
#include "vpx_dsp/psnr.h"
24
#include "vpx_dsp/vpx_dsp_common.h"
25
#include "vpx_ports/static_assert.h"
26
#include "vpx_ports/system_state.h"
27
#include "vpx_scale/yv12config.h"
28
#include "vpx_util/vpx_timestamp.h"
29
#include "vpx/internal/vpx_codec_internal.h"
30
#include "./vpx_version.h"
31
#include "vp9/encoder/vp9_encoder.h"
32
#include "vp9/encoder/vp9_ethread.h"
33
#include "vpx/vp8cx.h"
34
#include "vp9/common/vp9_alloccommon.h"
35
#include "vp9/common/vp9_scale.h"
36
#include "vp9/vp9_cx_iface.h"
37
#include "vp9/encoder/vp9_firstpass.h"
38
#include "vp9/encoder/vp9_lookahead.h"
39
#include "vp9/vp9_cx_iface.h"
40
#include "vp9/vp9_iface_common.h"
41
42
#include "vpx/vpx_tpl.h"
43
44
typedef struct vp9_extracfg {
45
  int cpu_used;  // available cpu percentage in 1/16
46
  unsigned int enable_auto_alt_ref;
47
  unsigned int noise_sensitivity;
48
  unsigned int sharpness;
49
  unsigned int static_thresh;
50
  unsigned int tile_columns;
51
  unsigned int tile_rows;
52
  unsigned int enable_tpl_model;
53
  unsigned int enable_keyframe_filtering;
54
  unsigned int arnr_max_frames;
55
  unsigned int arnr_strength;
56
  unsigned int min_gf_interval;
57
  unsigned int max_gf_interval;
58
  vp8e_tuning tuning;
59
  unsigned int cq_level;  // constrained quality level
60
  unsigned int rc_max_intra_bitrate_pct;
61
  unsigned int rc_max_inter_bitrate_pct;
62
  unsigned int gf_cbr_boost_pct;
63
  unsigned int lossless;
64
  unsigned int target_level;
65
  unsigned int frame_parallel_decoding_mode;
66
  AQ_MODE aq_mode;
67
  int alt_ref_aq;
68
  unsigned int frame_periodic_boost;
69
  vpx_bit_depth_t bit_depth;
70
  vp9e_tune_content content;
71
  vpx_color_space_t color_space;
72
  vpx_color_range_t color_range;
73
  int render_width;
74
  int render_height;
75
  unsigned int row_mt;
76
  unsigned int motion_vector_unit_test;
77
  int delta_q_uv;
78
  unsigned int validate_hbd_input;
79
} vp9_extracfg;
80
81
static struct vp9_extracfg default_extra_cfg = {
82
#if CONFIG_REALTIME_ONLY
83
  5,  // cpu_used
84
#else
85
  0,  // cpu_used
86
#endif
87
  1,                     // enable_auto_alt_ref
88
  0,                     // noise_sensitivity
89
  0,                     // sharpness
90
  0,                     // static_thresh
91
  6,                     // tile_columns
92
  0,                     // tile_rows
93
  1,                     // enable_tpl_model
94
  0,                     // enable_keyframe_filtering
95
  7,                     // arnr_max_frames
96
  5,                     // arnr_strength
97
  0,                     // min_gf_interval; 0 -> default decision
98
  0,                     // max_gf_interval; 0 -> default decision
99
  VP8_TUNE_PSNR,         // tuning
100
  10,                    // cq_level
101
  0,                     // rc_max_intra_bitrate_pct
102
  0,                     // rc_max_inter_bitrate_pct
103
  0,                     // gf_cbr_boost_pct
104
  0,                     // lossless
105
  255,                   // target_level
106
  1,                     // frame_parallel_decoding_mode
107
  NO_AQ,                 // aq_mode
108
  0,                     // alt_ref_aq
109
  0,                     // frame_periodic_delta_q
110
  VPX_BITS_8,            // Bit depth
111
  VP9E_CONTENT_DEFAULT,  // content
112
  VPX_CS_UNKNOWN,        // color space
113
  0,                     // color range
114
  0,                     // render width
115
  0,                     // render height
116
  0,                     // row_mt
117
  0,                     // motion_vector_unit_test
118
  0,                     // delta_q_uv
119
  1,                     // validate_hbd_input
120
};
121
122
struct vpx_codec_alg_priv {
123
  vpx_codec_priv_t base;
124
  vpx_codec_enc_cfg_t cfg;
125
  struct vp9_extracfg extra_cfg;
126
  vpx_codec_pts_t pts_offset;
127
  unsigned char pts_offset_initialized;
128
  VP9EncoderConfig oxcf;
129
  VP9_COMP *cpi;
130
  unsigned char *cx_data;
131
  size_t cx_data_sz;
132
  // pending_cx_data either is a null pointer or points into the cx_data buffer.
133
  unsigned char *pending_cx_data;
134
  size_t pending_cx_data_sz;
135
  int pending_frame_count;
136
  size_t pending_frame_sizes[8];
137
  size_t pending_frame_magnitude;
138
  vpx_image_t preview_img;
139
  vpx_enc_frame_flags_t next_frame_flags;
140
  vp8_postproc_cfg_t preview_ppcfg;
141
  vpx_codec_pkt_list_decl(256) pkt_list;
142
  unsigned int fixed_kf_cntr;
143
  vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
144
  // BufferPool that holds all reference frames.
145
  BufferPool *buffer_pool;
146
  vpx_fixed_buf_t global_headers;
147
  int global_header_subsampling;
148
};
149
150
// Called by encoder_set_config() and encoder_encode() only. Must not be called
151
// by encoder_init() because the `error` paramerer (cpi->common.error) will be
152
// destroyed by vpx_codec_enc_init_ver() after encoder_init() returns an error.
153
// See the "IMPORTANT" comment in vpx_codec_enc_init_ver().
154
static vpx_codec_err_t update_error_state(
155
102
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
156
102
  const vpx_codec_err_t res = error->error_code;
157
158
102
  if (res != VPX_CODEC_OK)
159
102
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;
160
161
102
  return res;
162
102
}
163
164
#undef ERROR
165
#define ERROR(str)                  \
166
1.03k
  do {                              \
167
1.03k
    ctx->base.err_detail = str;     \
168
1.03k
    return VPX_CODEC_INVALID_PARAM; \
169
1.03k
  } while (0)
170
171
#define RANGE_CHECK(p, memb, lo, hi)                                     \
172
1.64M
  do {                                                                   \
173
1.64M
    if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
174
1.64M
      ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
175
1.64M
  } while (0)
176
177
#define RANGE_CHECK_HI(p, memb, hi)                                     \
178
535k
  do {                                                                  \
179
535k
    if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
180
535k
  } while (0)
181
182
#define RANGE_CHECK_LO(p, memb, lo)                                     \
183
  do {                                                                  \
184
    if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
185
  } while (0)
186
187
#define RANGE_CHECK_BOOL(p, memb)                                     \
188
114k
  do {                                                                \
189
114k
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
190
114k
  } while (0)
191
192
static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
193
                                       const vpx_codec_enc_cfg_t *cfg,
194
38.3k
                                       const struct vp9_extracfg *extra_cfg) {
195
38.3k
  RANGE_CHECK(cfg, g_w, 1, 65536);  // 16 bits available
196
38.3k
  RANGE_CHECK(cfg, g_h, 1, 65536);  // 16 bits available
197
38.3k
  RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
198
38.2k
  RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
199
38.2k
  RANGE_CHECK_HI(cfg, g_profile, 3);
200
201
38.2k
  RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
202
38.2k
  RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
203
38.2k
  RANGE_CHECK_BOOL(extra_cfg, lossless);
204
38.2k
  RANGE_CHECK_BOOL(extra_cfg, frame_parallel_decoding_mode);
205
38.2k
  RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 2);
206
38.2k
  RANGE_CHECK(extra_cfg, alt_ref_aq, 0, 1);
207
38.2k
  RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
208
38.2k
  RANGE_CHECK_HI(cfg, g_threads, MAX_NUM_THREADS);
209
38.2k
  RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
210
38.2k
  RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
211
38.2k
  RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
212
38.2k
  RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
213
38.2k
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
214
38.2k
  RANGE_CHECK(cfg, rc_2pass_vbr_corpus_complexity, 0, 10000);
215
38.2k
  RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
216
38.2k
  RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
217
38.2k
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
218
38.2k
  RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
219
38.2k
  RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
220
#if CONFIG_REALTIME_ONLY
221
  RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
222
#else
223
38.2k
  RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
224
38.2k
#endif
225
38.2k
  RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
226
38.2k
  RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
227
38.2k
  if (extra_cfg->max_gf_interval > 0) {
228
0
    RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
229
0
  }
230
38.2k
  if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
231
0
    RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
232
0
                (MAX_LAG_BUFFERS - 1));
233
0
  }
234
235
  // For formation of valid ARF groups lag_in _frames should be 0 or greater
236
  // than the max_gf_interval + 2
237
38.2k
  if (cfg->g_lag_in_frames > 0 && extra_cfg->max_gf_interval > 0 &&
238
0
      cfg->g_lag_in_frames < extra_cfg->max_gf_interval + 2) {
239
0
    ERROR("Set lag in frames to 0 (low delay) or >= (max-gf-interval + 2)");
240
0
  }
241
242
38.2k
  if (cfg->rc_resize_allowed == 1) {
243
0
    RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
244
0
    RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
245
0
  }
246
247
38.2k
  RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
248
38.2k
  RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
249
250
38.2k
  {
251
38.2k
    unsigned int level = extra_cfg->target_level;
252
38.2k
    if (level != LEVEL_1 && level != LEVEL_1_1 && level != LEVEL_2 &&
253
38.2k
        level != LEVEL_2_1 && level != LEVEL_3 && level != LEVEL_3_1 &&
254
38.2k
        level != LEVEL_4 && level != LEVEL_4_1 && level != LEVEL_5 &&
255
38.2k
        level != LEVEL_5_1 && level != LEVEL_5_2 && level != LEVEL_6 &&
256
38.2k
        level != LEVEL_6_1 && level != LEVEL_6_2 && level != LEVEL_UNKNOWN &&
257
38.2k
        level != LEVEL_AUTO && level != LEVEL_MAX)
258
0
      ERROR("target_level is invalid");
259
38.2k
  }
260
261
38.2k
  if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
262
0
    ERROR("ss_number_layers * ts_number_layers is out of range");
263
38.2k
  if ((cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) &&
264
0
      cfg->g_pass != VPX_RC_ONE_PASS)
265
0
    ERROR("SVC is only supported for one-pass encoding.");
266
38.2k
  if (cfg->ts_number_layers > 1) {
267
0
    unsigned int sl, tl;
268
0
    for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
269
0
      for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
270
0
        const int layer = LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
271
0
        if (cfg->layer_target_bitrate[layer] <
272
0
            cfg->layer_target_bitrate[layer - 1])
273
0
          ERROR("ts_target_bitrate entries are not increasing");
274
0
      }
275
0
    }
276
277
0
    RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
278
0
    for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
279
0
      if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
280
0
        ERROR("ts_rate_decimator factors are not powers of 2");
281
0
  }
282
283
  // VP9 does not support a lower bound on the keyframe interval in
284
  // automatic keyframe placement mode.
285
38.2k
  if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
286
33.7k
      cfg->kf_min_dist > 0)
287
0
    ERROR(
288
38.2k
        "kf_min_dist not supported in auto mode, use 0 "
289
38.2k
        "or kf_max_dist instead.");
290
291
38.2k
  RANGE_CHECK(extra_cfg, row_mt, 0, 1);
292
38.2k
  RANGE_CHECK(extra_cfg, motion_vector_unit_test, 0, 2);
293
38.2k
  RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, MAX_ARF_LAYERS);
294
38.2k
  RANGE_CHECK(extra_cfg, cpu_used, -9, 9);
295
38.2k
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
296
38.2k
  RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
297
38.2k
  RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
298
38.2k
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
299
38.2k
  RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
300
38.2k
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
301
38.2k
  RANGE_CHECK(extra_cfg, cq_level, 0, 63);
302
38.2k
  RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
303
38.2k
  RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
304
38.2k
  if (cfg->g_input_bit_depth > (unsigned int)cfg->g_bit_depth)
305
0
    ERROR("Input bit-depth must not exceed codec bit-depth");
306
38.2k
  RANGE_CHECK(extra_cfg, content, VP9E_CONTENT_DEFAULT,
307
38.2k
              VP9E_CONTENT_INVALID - 1);
308
309
38.2k
#if !CONFIG_REALTIME_ONLY
310
38.2k
  if (cfg->g_pass == VPX_RC_LAST_PASS) {
311
0
    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
312
0
    const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
313
0
    const FIRSTPASS_STATS *stats;
314
315
0
    if (cfg->rc_twopass_stats_in.buf == NULL)
316
0
      ERROR("rc_twopass_stats_in.buf not set.");
317
318
0
    if (cfg->rc_twopass_stats_in.sz % packet_sz)
319
0
      ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
320
321
0
    if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
322
0
      ERROR("rc_twopass_stats_in requires at least two packets.");
323
324
0
    stats =
325
0
        (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
326
327
0
    const double count_rounded = stats->count + 0.5;
328
0
    if (!isfinite((float)stats->count) || stats->count < 0.0 ||
329
0
        count_rounded > (double)INT_MAX || (int)count_rounded != n_packets - 1)
330
0
      ERROR("rc_twopass_stats_in missing EOS stats packet");
331
0
  }
332
38.2k
#endif  // !CONFIG_REALTIME_ONLY
333
334
#if !CONFIG_VP9_HIGHBITDEPTH
335
  if (cfg->g_profile > (unsigned int)PROFILE_1) {
336
    ERROR("Profile > 1 not supported in this build configuration");
337
  }
338
#endif
339
38.2k
  if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
340
28.0k
      cfg->g_bit_depth > VPX_BITS_8) {
341
0
    ERROR("Codec high bit-depth not supported in profile < 2");
342
0
  }
343
38.2k
  if (cfg->g_profile > (unsigned int)PROFILE_1 &&
344
10.2k
      cfg->g_bit_depth == VPX_BITS_8) {
345
0
    ERROR("Codec bit-depth 8 not supported in profile > 1");
346
0
  }
347
38.2k
  RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
348
38.2k
  RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
349
350
  // The range below shall be further tuned.
351
38.2k
  RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
352
38.2k
  RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
353
38.2k
  RANGE_CHECK(cfg, err_per_mb_factor.den, 1, 1000);
354
38.2k
  RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
355
38.2k
  RANGE_CHECK(cfg, sr_diff_factor.den, 1, 1000);
356
38.2k
  RANGE_CHECK(cfg, kf_err_per_mb_factor.den, 1, 1000);
357
38.2k
  RANGE_CHECK(cfg, kf_frame_min_boost_factor.den, 1, 1000);
358
38.2k
  RANGE_CHECK(cfg, kf_frame_max_boost_subs_factor.den, 1, 1000);
359
38.2k
  RANGE_CHECK(cfg, kf_max_total_boost_factor.den, 1, 1000);
360
38.2k
  RANGE_CHECK(cfg, gf_max_total_boost_factor.den, 1, 1000);
361
38.2k
  RANGE_CHECK(cfg, gf_frame_max_boost_factor.den, 1, 1000);
362
38.2k
  RANGE_CHECK(cfg, zm_factor.den, 1, 1000);
363
38.2k
  RANGE_CHECK(cfg, rd_mult_inter_qp_fac.den, 1, 1000);
364
38.2k
  RANGE_CHECK(cfg, rd_mult_arf_qp_fac.den, 1, 1000);
365
38.2k
  RANGE_CHECK(cfg, rd_mult_key_qp_fac.den, 1, 1000);
366
367
38.2k
  return VPX_CODEC_OK;
368
38.2k
}
369
370
static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
371
75.5k
                                    const vpx_image_t *img) {
372
75.5k
  switch (img->fmt) {
373
0
    case VPX_IMG_FMT_YV12:
374
63.7k
    case VPX_IMG_FMT_I420:
375
67.0k
    case VPX_IMG_FMT_I42016:
376
67.0k
    case VPX_IMG_FMT_NV12: break;
377
2.17k
    case VPX_IMG_FMT_I422:
378
3.56k
    case VPX_IMG_FMT_I444:
379
4.84k
    case VPX_IMG_FMT_I440:
380
4.84k
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
381
0
        ERROR(
382
0
            "Invalid image format. I422, I444, I440 images are not supported "
383
0
            "in profile.");
384
0
      }
385
4.84k
      break;
386
4.84k
    case VPX_IMG_FMT_I42216:
387
3.27k
    case VPX_IMG_FMT_I44416:
388
3.64k
    case VPX_IMG_FMT_I44016:
389
3.64k
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
390
3.64k
          ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
391
0
        ERROR(
392
0
            "Invalid image format. 16-bit I422, I444, I440 images are "
393
0
            "not supported in profile.");
394
0
      }
395
3.64k
      break;
396
3.64k
    default:
397
0
      ERROR(
398
0
          "Invalid image format. Only YV12, I420, I422, I444, I440, NV12 "
399
0
          "images are supported.");
400
0
      break;
401
75.5k
  }
402
403
75.5k
  if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
404
0
    ERROR("Image size must match encoder init configuration size");
405
75.5k
  assert(img->fmt & VPX_IMG_FMT_PLANAR);
406
75.5k
  if (img->stride[VPX_PLANE_U] != img->stride[VPX_PLANE_V])
407
0
    ERROR("Image U/V strides must match");
408
409
75.5k
#if CONFIG_VP9_HIGHBITDEPTH
410
  // The following checks ensure that the image format matches the encoder's
411
  // high bit-depth setting. Currently, libvpx overwrites function pointers if
412
  // use_highbitdepth is set to 1, but there is no other way around if the
413
  // internal flag gets reset to 0.
414
75.5k
  if (ctx->oxcf.use_highbitdepth && !(img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
415
0
    ERROR(
416
0
        "Encoder initialized with VPX_CODEC_USE_HIGHBITDEPTH requires a "
417
0
        "high-bit-depth image format (e.g. VPX_IMG_FMT_I42016)");
418
0
  }
419
75.5k
  if (!ctx->oxcf.use_highbitdepth && (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
420
0
    ERROR(
421
0
        "Non-high-bit-depth encoder cannot accept a high-bit-depth image "
422
0
        "format");
423
0
  }
424
425
75.5k
  if (ctx->extra_cfg.validate_hbd_input &&
426
75.5k
      (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
427
6.97k
    const unsigned int h = img->d_h;
428
6.97k
    const unsigned int w = img->d_w;
429
6.97k
    const unsigned int bit_depth = ctx->cfg.g_bit_depth;
430
6.97k
    const int max_val = 1 << bit_depth;
431
25.4k
    for (int plane = 0; plane < 3; ++plane) {
432
19.5k
      const unsigned short *src = (const unsigned short *)img->planes[plane];
433
19.5k
      if (!src) return VPX_CODEC_INVALID_PARAM;
434
19.5k
      const int stride = img->stride[plane] / 2;
435
19.5k
      const unsigned int ph =
436
19.5k
          (plane == 0) ? h : (h + img->y_chroma_shift) >> img->y_chroma_shift;
437
19.5k
      const unsigned int pw =
438
19.5k
          (plane == 0) ? w : (w + img->x_chroma_shift) >> img->x_chroma_shift;
439
452k
      for (unsigned int i = 0; i < ph; ++i) {
440
15.9M
        for (unsigned int j = 0; j < pw; ++j) {
441
15.5M
          if (src[j] >= max_val) {
442
1.00k
            ERROR("Input pixel value out of range for encoder bit depth");
443
1.00k
          }
444
15.5M
        }
445
432k
        src += stride;
446
432k
      }
447
19.5k
    }
448
6.97k
  }
449
74.5k
#endif  // CONFIG_VP9_HIGHBITDEPTH
450
451
74.5k
  return VPX_CODEC_OK;
452
75.5k
}
453
454
74.5k
static int get_image_bps(const vpx_image_t *img) {
455
74.5k
  switch (img->fmt) {
456
0
    case VPX_IMG_FMT_YV12:
457
0
    case VPX_IMG_FMT_NV12:
458
63.7k
    case VPX_IMG_FMT_I420: return 12;
459
2.17k
    case VPX_IMG_FMT_I422: return 16;
460
1.38k
    case VPX_IMG_FMT_I444: return 24;
461
1.28k
    case VPX_IMG_FMT_I440: return 16;
462
2.89k
    case VPX_IMG_FMT_I42016: return 24;
463
1.47k
    case VPX_IMG_FMT_I42216: return 32;
464
1.28k
    case VPX_IMG_FMT_I44416: return 48;
465
320
    case VPX_IMG_FMT_I44016: return 32;
466
0
    default: assert(0 && "Invalid image format"); break;
467
74.5k
  }
468
0
  return 0;
469
74.5k
}
470
471
// Modify the encoder config for the target level.
472
0
static void config_target_level(VP9EncoderConfig *oxcf) {
473
0
  double max_average_bitrate;  // in bits per second
474
0
  int max_over_shoot_pct;
475
0
  const int target_level_index = get_level_index(oxcf->target_level);
476
477
0
  vpx_clear_system_state();
478
0
  assert(target_level_index >= 0);
479
0
  assert(target_level_index < VP9_LEVELS);
480
481
  // Maximum target bit-rate is level_limit * 80%.
482
0
  max_average_bitrate =
483
0
      vp9_level_defs[target_level_index].average_bitrate * 800.0;
484
0
  if ((double)oxcf->target_bandwidth > max_average_bitrate)
485
0
    oxcf->target_bandwidth = (int64_t)(max_average_bitrate);
486
0
  if (oxcf->ss_number_layers == 1 && oxcf->pass != 0)
487
0
    oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
488
489
  // Adjust max over-shoot percentage (only when target_bandwidth > 0).
490
0
  if (oxcf->target_bandwidth > 0) {
491
0
    max_over_shoot_pct =
492
0
        (int)((max_average_bitrate * 1.10 - (double)oxcf->target_bandwidth) *
493
0
              100 / (double)(oxcf->target_bandwidth));
494
0
    if (oxcf->over_shoot_pct > max_over_shoot_pct)
495
0
      oxcf->over_shoot_pct = max_over_shoot_pct;
496
0
  }
497
498
  // Adjust worst allowed quantizer.
499
0
  oxcf->worst_allowed_q = vp9_quantizer_to_qindex(63);
500
501
  // Adjust minimum art-ref distance.
502
  // min_gf_interval should be no less than min_altref_distance + 1,
503
  // as the encoder may produce bitstream with alt-ref distance being
504
  // min_gf_interval - 1.
505
0
  if (oxcf->min_gf_interval <=
506
0
      (int)vp9_level_defs[target_level_index].min_altref_distance) {
507
0
    oxcf->min_gf_interval =
508
0
        (int)vp9_level_defs[target_level_index].min_altref_distance + 1;
509
    // If oxcf->max_gf_interval == 0, it will be assigned with a default value
510
    // in vp9_rc_set_gf_interval_range().
511
0
    if (oxcf->max_gf_interval != 0) {
512
0
      oxcf->max_gf_interval =
513
0
          VPXMAX(oxcf->max_gf_interval, oxcf->min_gf_interval);
514
0
    }
515
0
  }
516
517
  // Adjust maximum column tiles.
518
0
  if (vp9_level_defs[target_level_index].max_col_tiles <
519
0
      (1 << oxcf->tile_columns)) {
520
0
    while (oxcf->tile_columns > 0 &&
521
0
           vp9_level_defs[target_level_index].max_col_tiles <
522
0
               (1 << oxcf->tile_columns))
523
0
      --oxcf->tile_columns;
524
0
  }
525
0
}
526
527
38.2k
static vpx_rational64_t get_g_timebase_in_ts(vpx_rational_t g_timebase) {
528
38.2k
  vpx_rational64_t g_timebase_in_ts;
529
38.2k
  g_timebase_in_ts.den = g_timebase.den;
530
38.2k
  g_timebase_in_ts.num = g_timebase.num;
531
38.2k
  g_timebase_in_ts.num *= TICKS_PER_SEC;
532
38.2k
  reduce_ratio(&g_timebase_in_ts);
533
38.2k
  return g_timebase_in_ts;
534
38.2k
}
535
536
static vpx_codec_err_t set_encoder_config(
537
    VP9EncoderConfig *oxcf, vpx_codec_enc_cfg_t *cfg,
538
38.2k
    const struct vp9_extracfg *extra_cfg) {
539
38.2k
  int sl, tl;
540
38.2k
  unsigned int raw_target_rate;
541
38.2k
  oxcf->profile = cfg->g_profile;
542
38.2k
  oxcf->max_threads = (int)cfg->g_threads;
543
38.2k
  oxcf->width = cfg->g_w;
544
38.2k
  oxcf->height = cfg->g_h;
545
38.2k
  oxcf->bit_depth = cfg->g_bit_depth;
546
38.2k
  oxcf->input_bit_depth = cfg->g_input_bit_depth;
547
  // TODO(angiebird): Figure out if we can just use g_timebase to indicate the
548
  // inverse of framerate
549
  // guess a frame rate if out of whack, use 30
550
38.2k
  oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
551
38.2k
  if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
552
38.2k
  oxcf->g_timebase = cfg->g_timebase;
553
38.2k
  oxcf->g_timebase_in_ts = get_g_timebase_in_ts(oxcf->g_timebase);
554
555
38.2k
  oxcf->mode = GOOD;
556
557
38.2k
  switch (cfg->g_pass) {
558
38.2k
    case VPX_RC_ONE_PASS: oxcf->pass = 0; break;
559
0
    case VPX_RC_FIRST_PASS: oxcf->pass = 1; break;
560
0
    case VPX_RC_LAST_PASS: oxcf->pass = 2; break;
561
38.2k
  }
562
563
38.2k
  oxcf->lag_in_frames =
564
38.2k
      cfg->g_pass == VPX_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
565
38.2k
  oxcf->rc_mode = cfg->rc_end_usage;
566
567
38.2k
  raw_target_rate =
568
38.2k
      (unsigned int)((int64_t)oxcf->width * oxcf->height * oxcf->bit_depth * 3 *
569
38.2k
                     oxcf->init_framerate / 1000);
570
  // Cap target bitrate to raw rate or 1000Mbps, whichever is less
571
38.2k
  cfg->rc_target_bitrate =
572
38.2k
      VPXMIN(VPXMIN(raw_target_rate, cfg->rc_target_bitrate), 1000000);
573
574
  // Convert target bandwidth from Kbit/s to Bit/s
575
38.2k
  oxcf->target_bandwidth = 1000 * (int64_t)cfg->rc_target_bitrate;
576
38.2k
  oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
577
38.2k
  oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
578
38.2k
  oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
579
580
38.2k
  oxcf->best_allowed_q =
581
38.2k
      extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
582
38.2k
  oxcf->worst_allowed_q =
583
38.2k
      extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
584
38.2k
  oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
585
38.2k
  oxcf->fixed_q = -1;
586
587
38.2k
  oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
588
38.2k
  oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
589
590
38.2k
  oxcf->scaled_frame_width = cfg->rc_scaled_width;
591
38.2k
  oxcf->scaled_frame_height = cfg->rc_scaled_height;
592
38.2k
  if (cfg->rc_resize_allowed == 1) {
593
0
    oxcf->resize_mode =
594
0
        (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
595
0
            ? RESIZE_DYNAMIC
596
0
            : RESIZE_FIXED;
597
38.2k
  } else {
598
38.2k
    oxcf->resize_mode = RESIZE_NONE;
599
38.2k
  }
600
601
38.2k
  oxcf->maximum_buffer_size_ms = cfg->rc_buf_sz;
602
38.2k
  oxcf->starting_buffer_level_ms = cfg->rc_buf_initial_sz;
603
38.2k
  oxcf->optimal_buffer_level_ms = cfg->rc_buf_optimal_sz;
604
605
38.2k
  oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
606
607
38.2k
  oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
608
38.2k
  oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
609
38.2k
  oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
610
38.2k
  oxcf->vbr_corpus_complexity = cfg->rc_2pass_vbr_corpus_complexity;
611
612
38.2k
  oxcf->auto_key =
613
38.2k
      cfg->kf_mode == VPX_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
614
615
38.2k
  oxcf->key_freq = cfg->kf_max_dist;
616
617
38.2k
  oxcf->speed = abs(extra_cfg->cpu_used);
618
38.2k
  oxcf->encode_breakout = extra_cfg->static_thresh;
619
38.2k
  oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
620
38.2k
  if (oxcf->bit_depth == VPX_BITS_8) {
621
28.0k
    oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
622
28.0k
  } else {
623
    // Disable denoiser for high bitdepth since vp9_denoiser_filter only works
624
    // for 8 bits.
625
10.2k
    oxcf->noise_sensitivity = 0;
626
10.2k
  }
627
38.2k
  oxcf->sharpness = extra_cfg->sharpness;
628
629
38.2k
  vp9_set_first_pass_stats(oxcf, &cfg->rc_twopass_stats_in);
630
631
38.2k
  oxcf->color_space = extra_cfg->color_space;
632
38.2k
  oxcf->color_range = extra_cfg->color_range;
633
38.2k
  oxcf->render_width = extra_cfg->render_width;
634
38.2k
  oxcf->render_height = extra_cfg->render_height;
635
38.2k
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
636
38.2k
  oxcf->arnr_strength = extra_cfg->arnr_strength;
637
38.2k
  oxcf->min_gf_interval = extra_cfg->min_gf_interval;
638
38.2k
  oxcf->max_gf_interval = extra_cfg->max_gf_interval;
639
640
38.2k
  oxcf->tuning = extra_cfg->tuning;
641
38.2k
  oxcf->content = extra_cfg->content;
642
643
38.2k
  oxcf->tile_columns = extra_cfg->tile_columns;
644
645
38.2k
  oxcf->enable_tpl_model = extra_cfg->enable_tpl_model;
646
647
38.2k
  oxcf->enable_keyframe_filtering = extra_cfg->enable_keyframe_filtering;
648
649
  // TODO(yunqing): The dependencies between row tiles cause error in multi-
650
  // threaded encoding. For now, tile_rows is forced to be 0 in this case.
651
  // The further fix can be done by adding synchronizations after a tile row
652
  // is encoded. But this will hurt multi-threaded encoder performance. So,
653
  // it is recommended to use tile-rows=0 while encoding with threads > 1.
654
38.2k
  if (oxcf->max_threads > 1 && oxcf->tile_columns > 0)
655
0
    oxcf->tile_rows = 0;
656
38.2k
  else
657
38.2k
    oxcf->tile_rows = extra_cfg->tile_rows;
658
659
38.2k
  oxcf->error_resilient_mode =
660
38.2k
      cfg->g_error_resilient & VPX_ERROR_RESILIENT_DEFAULT;
661
38.2k
  oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
662
663
38.2k
  oxcf->aq_mode = extra_cfg->aq_mode;
664
38.2k
  oxcf->alt_ref_aq = extra_cfg->alt_ref_aq;
665
666
38.2k
  oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
667
668
38.2k
  oxcf->ss_number_layers = cfg->ss_number_layers;
669
38.2k
  oxcf->ts_number_layers = cfg->ts_number_layers;
670
38.2k
  oxcf->temporal_layering_mode =
671
38.2k
      (enum vp9e_temporal_layering_mode)cfg->temporal_layering_mode;
672
38.2k
  if (oxcf->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
673
38.2k
    if (oxcf->ts_number_layers == 2)
674
0
      oxcf->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
675
38.2k
    else if (oxcf->ts_number_layers == 3)
676
0
      oxcf->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
677
38.2k
  }
678
679
38.2k
  oxcf->target_level = extra_cfg->target_level;
680
681
38.2k
  oxcf->row_mt = extra_cfg->row_mt;
682
38.2k
  oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
683
684
38.2k
  oxcf->delta_q_uv = extra_cfg->delta_q_uv;
685
686
76.5k
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
687
76.5k
    for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
688
38.2k
      const int layer = sl * oxcf->ts_number_layers + tl;
689
38.2k
      if (cfg->layer_target_bitrate[layer] > INT_MAX / 1000)
690
0
        oxcf->layer_target_bitrate[layer] = INT_MAX;
691
38.2k
      else
692
38.2k
        oxcf->layer_target_bitrate[layer] =
693
38.2k
            1000 * cfg->layer_target_bitrate[layer];
694
38.2k
    }
695
38.2k
  }
696
38.2k
  if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
697
0
    oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
698
0
  }
699
38.2k
  if (oxcf->ts_number_layers > 1) {
700
0
    for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
701
0
      oxcf->ts_rate_decimator[tl] =
702
0
          cfg->ts_rate_decimator[tl] ? cfg->ts_rate_decimator[tl] : 1;
703
0
    }
704
38.2k
  } else if (oxcf->ts_number_layers == 1) {
705
38.2k
    oxcf->ts_rate_decimator[0] = 1;
706
38.2k
  }
707
708
38.2k
  if (get_level_index(oxcf->target_level) >= 0) config_target_level(oxcf);
709
  // vp9_dump_encoder_config(oxcf, stderr);
710
38.2k
  return VPX_CODEC_OK;
711
38.2k
}
712
713
static vpx_codec_err_t set_twopass_params_from_config(
714
38.2k
    const vpx_codec_enc_cfg_t *const cfg, struct VP9_COMP *cpi) {
715
38.2k
  if (!cfg->use_vizier_rc_params) return VPX_CODEC_OK;
716
0
  if (cpi == NULL) return VPX_CODEC_ERROR;
717
718
0
  cpi->twopass.use_vizier_rc_params = cfg->use_vizier_rc_params;
719
720
  // The values set here are factors that will be applied to default values
721
  // to get the final value used in the two pass code. Hence 1.0 will
722
  // match the default behaviour when not using passed in values.
723
  // We also apply limits here to prevent the user from applying settings
724
  // that make no sense.
725
0
  cpi->twopass.active_wq_factor =
726
0
      (double)cfg->active_wq_factor.num / (double)cfg->active_wq_factor.den;
727
0
  if (cpi->twopass.active_wq_factor < 0.25)
728
0
    cpi->twopass.active_wq_factor = 0.25;
729
0
  else if (cpi->twopass.active_wq_factor > 16.0)
730
0
    cpi->twopass.active_wq_factor = 16.0;
731
732
0
  cpi->twopass.err_per_mb =
733
0
      (double)cfg->err_per_mb_factor.num / (double)cfg->err_per_mb_factor.den;
734
0
  if (cpi->twopass.err_per_mb < 0.25)
735
0
    cpi->twopass.err_per_mb = 0.25;
736
0
  else if (cpi->twopass.err_per_mb > 4.0)
737
0
    cpi->twopass.err_per_mb = 4.0;
738
739
0
  cpi->twopass.sr_default_decay_limit =
740
0
      (double)cfg->sr_default_decay_limit.num /
741
0
      (double)cfg->sr_default_decay_limit.den;
742
0
  if (cpi->twopass.sr_default_decay_limit < 0.25)
743
0
    cpi->twopass.sr_default_decay_limit = 0.25;
744
  // If the default changes this will need to change.
745
0
  else if (cpi->twopass.sr_default_decay_limit > 1.33)
746
0
    cpi->twopass.sr_default_decay_limit = 1.33;
747
748
0
  cpi->twopass.sr_diff_factor =
749
0
      (double)cfg->sr_diff_factor.num / (double)cfg->sr_diff_factor.den;
750
0
  if (cpi->twopass.sr_diff_factor < 0.25)
751
0
    cpi->twopass.sr_diff_factor = 0.25;
752
0
  else if (cpi->twopass.sr_diff_factor > 4.0)
753
0
    cpi->twopass.sr_diff_factor = 4.0;
754
755
0
  cpi->twopass.kf_err_per_mb = (double)cfg->kf_err_per_mb_factor.num /
756
0
                               (double)cfg->kf_err_per_mb_factor.den;
757
0
  if (cpi->twopass.kf_err_per_mb < 0.25)
758
0
    cpi->twopass.kf_err_per_mb = 0.25;
759
0
  else if (cpi->twopass.kf_err_per_mb > 4.0)
760
0
    cpi->twopass.kf_err_per_mb = 4.0;
761
762
0
  cpi->twopass.kf_frame_min_boost = (double)cfg->kf_frame_min_boost_factor.num /
763
0
                                    (double)cfg->kf_frame_min_boost_factor.den;
764
0
  if (cpi->twopass.kf_frame_min_boost < 0.25)
765
0
    cpi->twopass.kf_frame_min_boost = 0.25;
766
0
  else if (cpi->twopass.kf_frame_min_boost > 4.0)
767
0
    cpi->twopass.kf_frame_min_boost = 4.0;
768
769
0
  cpi->twopass.kf_frame_max_boost_first =
770
0
      (double)cfg->kf_frame_max_boost_first_factor.num /
771
0
      (double)cfg->kf_frame_max_boost_first_factor.den;
772
0
  if (cpi->twopass.kf_frame_max_boost_first < 0.25)
773
0
    cpi->twopass.kf_frame_max_boost_first = 0.25;
774
0
  else if (cpi->twopass.kf_frame_max_boost_first > 4.0)
775
0
    cpi->twopass.kf_frame_max_boost_first = 4.0;
776
777
0
  cpi->twopass.kf_frame_max_boost_subs =
778
0
      (double)cfg->kf_frame_max_boost_subs_factor.num /
779
0
      (double)cfg->kf_frame_max_boost_subs_factor.den;
780
0
  if (cpi->twopass.kf_frame_max_boost_subs < 0.25)
781
0
    cpi->twopass.kf_frame_max_boost_subs = 0.25;
782
0
  else if (cpi->twopass.kf_frame_max_boost_subs > 4.0)
783
0
    cpi->twopass.kf_frame_max_boost_subs = 4.0;
784
785
0
  cpi->twopass.kf_max_total_boost = (double)cfg->kf_max_total_boost_factor.num /
786
0
                                    (double)cfg->kf_max_total_boost_factor.den;
787
0
  if (cpi->twopass.kf_max_total_boost < 0.25)
788
0
    cpi->twopass.kf_max_total_boost = 0.25;
789
0
  else if (cpi->twopass.kf_max_total_boost > 4.0)
790
0
    cpi->twopass.kf_max_total_boost = 4.0;
791
792
0
  cpi->twopass.gf_max_total_boost = (double)cfg->gf_max_total_boost_factor.num /
793
0
                                    (double)cfg->gf_max_total_boost_factor.den;
794
0
  if (cpi->twopass.gf_max_total_boost < 0.25)
795
0
    cpi->twopass.gf_max_total_boost = 0.25;
796
0
  else if (cpi->twopass.gf_max_total_boost > 4.0)
797
0
    cpi->twopass.gf_max_total_boost = 4.0;
798
799
0
  cpi->twopass.gf_frame_max_boost = (double)cfg->gf_frame_max_boost_factor.num /
800
0
                                    (double)cfg->gf_frame_max_boost_factor.den;
801
0
  if (cpi->twopass.gf_frame_max_boost < 0.25)
802
0
    cpi->twopass.gf_frame_max_boost = 0.25;
803
0
  else if (cpi->twopass.gf_frame_max_boost > 4.0)
804
0
    cpi->twopass.gf_frame_max_boost = 4.0;
805
806
0
  cpi->twopass.zm_factor =
807
0
      (double)cfg->zm_factor.num / (double)cfg->zm_factor.den;
808
0
  if (cpi->twopass.zm_factor < 0.25)
809
0
    cpi->twopass.zm_factor = 0.25;
810
0
  else if (cpi->twopass.zm_factor > 2.0)
811
0
    cpi->twopass.zm_factor = 2.0;
812
813
0
  cpi->rd_ctrl.rd_mult_inter_qp_fac = (double)cfg->rd_mult_inter_qp_fac.num /
814
0
                                      (double)cfg->rd_mult_inter_qp_fac.den;
815
0
  if (cpi->rd_ctrl.rd_mult_inter_qp_fac < 0.25)
816
0
    cpi->rd_ctrl.rd_mult_inter_qp_fac = 0.25;
817
0
  else if (cpi->rd_ctrl.rd_mult_inter_qp_fac > 4.0)
818
0
    cpi->rd_ctrl.rd_mult_inter_qp_fac = 4.0;
819
820
0
  cpi->rd_ctrl.rd_mult_arf_qp_fac =
821
0
      (double)cfg->rd_mult_arf_qp_fac.num / (double)cfg->rd_mult_arf_qp_fac.den;
822
0
  if (cpi->rd_ctrl.rd_mult_arf_qp_fac < 0.25)
823
0
    cpi->rd_ctrl.rd_mult_arf_qp_fac = 0.25;
824
0
  else if (cpi->rd_ctrl.rd_mult_arf_qp_fac > 4.0)
825
0
    cpi->rd_ctrl.rd_mult_arf_qp_fac = 4.0;
826
827
0
  cpi->rd_ctrl.rd_mult_key_qp_fac =
828
0
      (double)cfg->rd_mult_key_qp_fac.num / (double)cfg->rd_mult_key_qp_fac.den;
829
0
  if (cpi->rd_ctrl.rd_mult_key_qp_fac < 0.25)
830
0
    cpi->rd_ctrl.rd_mult_key_qp_fac = 0.25;
831
0
  else if (cpi->rd_ctrl.rd_mult_key_qp_fac > 4.0)
832
0
    cpi->rd_ctrl.rd_mult_key_qp_fac = 4.0;
833
834
0
  return VPX_CODEC_OK;
835
0
}
836
837
static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
838
0
                                          const vpx_codec_enc_cfg_t *cfg) {
839
0
  vpx_codec_err_t res;
840
0
  volatile int force_key = 0;
841
842
0
  if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
843
0
    if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
844
0
      ERROR("Cannot change width or height after initialization");
845
    // Note: function encoder_set_config() is allowed to be called multiple
846
    // times. However, when the original frame width or height is less than two
847
    // times of the new frame width or height, a forced key frame should be
848
    // used (for the case of single spatial layer, since otherwise a previous
849
    //  encoded frame at a lower layer may be the desired reference). To make
850
    //  sure the correct detection of a forced key frame, we need
851
    // to update the frame width and height only when the actual encoding is
852
    // performed. cpi->last_coded_width and cpi->last_coded_height are used to
853
    // track the actual coded frame size.
854
0
    if ((ctx->cpi->last_coded_width && ctx->cpi->last_coded_height &&
855
0
         (!valid_ref_frame_size(ctx->cpi->last_coded_width,
856
0
                                ctx->cpi->last_coded_height, cfg->g_w,
857
0
                                cfg->g_h) &&
858
0
          ctx->cpi->svc.number_spatial_layers == 1)) ||
859
0
        (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
860
0
        (ctx->cpi->initial_height &&
861
0
         (int)cfg->g_h > ctx->cpi->initial_height)) {
862
0
      force_key = 1;
863
0
    }
864
0
  }
865
866
  // Prevent increasing lag_in_frames. This check is stricter than it needs
867
  // to be -- the limit is not increasing past the first lag_in_frames
868
  // value, but we don't track the initial config, only the last successful
869
  // config.
870
0
  if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
871
0
    ERROR("Cannot increase lag_in_frames");
872
873
0
  res = validate_config(ctx, cfg, &ctx->extra_cfg);
874
0
  if (res != VPX_CODEC_OK) return res;
875
876
0
  if (setjmp(ctx->cpi->common.error.jmp)) {
877
0
    const vpx_codec_err_t codec_err =
878
0
        update_error_state(ctx, &ctx->cpi->common.error);
879
0
    ctx->cpi->common.error.setjmp = 0;
880
0
    vpx_clear_system_state();
881
0
    assert(codec_err != VPX_CODEC_OK);
882
0
    return codec_err;
883
0
  }
884
0
  ctx->cpi->common.error.setjmp = 1;
885
886
0
  ctx->cfg = *cfg;
887
0
  set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
888
0
  set_twopass_params_from_config(&ctx->cfg, ctx->cpi);
889
  // On profile change, request a key frame
890
0
  force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
891
0
  vp9_change_config(ctx->cpi, &ctx->oxcf);
892
893
0
  if (force_key) ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
894
895
0
  ctx->cpi->common.error.setjmp = 0;
896
0
  return VPX_CODEC_OK;
897
0
}
898
899
static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
900
0
                                          va_list args) {
901
0
  int *const arg = va_arg(args, int *);
902
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
903
0
  *arg = vp9_get_quantizer(ctx->cpi);
904
0
  return VPX_CODEC_OK;
905
0
}
906
907
static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
908
68.4k
                                            va_list args) {
909
68.4k
  int *const arg = va_arg(args, int *);
910
68.4k
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
911
68.4k
  *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
912
68.4k
  return VPX_CODEC_OK;
913
68.4k
}
914
915
static vpx_codec_err_t ctrl_get_quantizer_svc_layers(vpx_codec_alg_priv_t *ctx,
916
0
                                                     va_list args) {
917
0
  int *const arg = va_arg(args, int *);
918
0
  int i;
919
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
920
0
  for (i = 0; i < VPX_SS_MAX_LAYERS; i++) {
921
0
    arg[i] = ctx->cpi->svc.base_qindex[i];
922
0
  }
923
0
  return VPX_CODEC_OK;
924
0
}
925
926
static vpx_codec_err_t ctrl_get_loopfilter_level(vpx_codec_alg_priv_t *ctx,
927
0
                                                 va_list args) {
928
0
  int *const arg = va_arg(args, int *);
929
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
930
0
  *arg = ctx->cpi->common.lf.filter_level;
931
0
  return VPX_CODEC_OK;
932
0
}
933
934
static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
935
33.4k
                                        const struct vp9_extracfg *extra_cfg) {
936
33.4k
  const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
937
33.4k
  if (res == VPX_CODEC_OK) {
938
33.4k
    ctx->extra_cfg = *extra_cfg;
939
33.4k
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
940
33.4k
    set_twopass_params_from_config(&ctx->cfg, ctx->cpi);
941
33.4k
    vp9_change_config(ctx->cpi, &ctx->oxcf);
942
33.4k
  }
943
33.4k
  return res;
944
33.4k
}
945
946
static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
947
4.79k
                                        va_list args) {
948
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
949
  // Use fastest speed setting (speed 9 or -9) if it's set beyond the range.
950
4.79k
  extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
951
4.79k
  extra_cfg.cpu_used = clamp(extra_cfg.cpu_used, -9, 9);
952
#if CONFIG_REALTIME_ONLY
953
  if (extra_cfg.cpu_used > -5 && extra_cfg.cpu_used < 5)
954
    extra_cfg.cpu_used = (extra_cfg.cpu_used > 0) ? 5 : -5;
955
#endif
956
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
957
4.79k
}
958
959
static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
960
0
                                                    va_list args) {
961
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
962
0
  extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
963
0
  return update_extra_cfg(ctx, &extra_cfg);
964
0
}
965
966
static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
967
0
                                                  va_list args) {
968
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
969
0
  extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
970
0
  return update_extra_cfg(ctx, &extra_cfg);
971
0
}
972
973
static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
974
0
                                          va_list args) {
975
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
976
0
  extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
977
0
  return update_extra_cfg(ctx, &extra_cfg);
978
0
}
979
980
static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
981
4.79k
                                              va_list args) {
982
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
983
4.79k
  extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
984
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
985
4.79k
}
986
987
static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
988
0
                                             va_list args) {
989
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
990
0
  extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
991
0
  return update_extra_cfg(ctx, &extra_cfg);
992
0
}
993
994
static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
995
0
                                          va_list args) {
996
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
997
0
  extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
998
0
  return update_extra_cfg(ctx, &extra_cfg);
999
0
}
1000
1001
static vpx_codec_err_t ctrl_set_tpl_model(vpx_codec_alg_priv_t *ctx,
1002
0
                                          va_list args) {
1003
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1004
0
  extra_cfg.enable_tpl_model = CAST(VP9E_SET_TPL, args);
1005
0
  return update_extra_cfg(ctx, &extra_cfg);
1006
0
}
1007
1008
static vpx_codec_err_t ctrl_set_keyframe_filtering(vpx_codec_alg_priv_t *ctx,
1009
0
                                                   va_list args) {
1010
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1011
0
  extra_cfg.enable_keyframe_filtering =
1012
0
      CAST(VP9E_SET_KEY_FRAME_FILTERING, args);
1013
0
  return update_extra_cfg(ctx, &extra_cfg);
1014
0
}
1015
1016
static vpx_codec_err_t ctrl_set_validate_hbd_input(vpx_codec_alg_priv_t *ctx,
1017
0
                                                   va_list args) {
1018
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1019
0
  extra_cfg.validate_hbd_input = CAST(VP9E_SET_VALIDATE_HBD_INPUT, args);
1020
0
  return update_extra_cfg(ctx, &extra_cfg);
1021
0
}
1022
static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
1023
4.79k
                                                va_list args) {
1024
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1025
4.79k
  extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
1026
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
1027
4.79k
}
1028
1029
static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
1030
4.79k
                                              va_list args) {
1031
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1032
4.79k
  extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
1033
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
1034
4.79k
}
1035
1036
static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
1037
4.79k
                                          va_list args) {
1038
4.79k
  (void)ctx;
1039
4.79k
  (void)args;
1040
4.79k
  return VPX_CODEC_OK;
1041
4.79k
}
1042
1043
static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
1044
0
                                       va_list args) {
1045
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1046
0
  extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
1047
0
  return update_extra_cfg(ctx, &extra_cfg);
1048
0
}
1049
1050
static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
1051
482
                                         va_list args) {
1052
482
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1053
482
  extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
1054
482
  return update_extra_cfg(ctx, &extra_cfg);
1055
482
}
1056
1057
static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
1058
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
1059
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1060
0
  extra_cfg.rc_max_intra_bitrate_pct =
1061
0
      CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
1062
0
  return update_extra_cfg(ctx, &extra_cfg);
1063
0
}
1064
1065
static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
1066
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
1067
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1068
0
  extra_cfg.rc_max_inter_bitrate_pct =
1069
0
      CAST(VP9E_SET_MAX_INTER_BITRATE_PCT, args);
1070
0
  return update_extra_cfg(ctx, &extra_cfg);
1071
0
}
1072
1073
static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
1074
0
                                                    va_list args) {
1075
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1076
0
  extra_cfg.gf_cbr_boost_pct = CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
1077
0
  return update_extra_cfg(ctx, &extra_cfg);
1078
0
}
1079
1080
static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
1081
0
                                         va_list args) {
1082
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1083
0
  extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
1084
0
  return update_extra_cfg(ctx, &extra_cfg);
1085
0
}
1086
1087
static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
1088
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
1089
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1090
0
  extra_cfg.frame_parallel_decoding_mode =
1091
0
      CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
1092
0
  return update_extra_cfg(ctx, &extra_cfg);
1093
0
}
1094
1095
static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
1096
0
                                        va_list args) {
1097
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1098
0
  extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
1099
0
  if (ctx->cpi->fixed_qp_onepass) extra_cfg.aq_mode = 0;
1100
0
  return update_extra_cfg(ctx, &extra_cfg);
1101
0
}
1102
1103
static vpx_codec_err_t ctrl_set_alt_ref_aq(vpx_codec_alg_priv_t *ctx,
1104
0
                                           va_list args) {
1105
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1106
0
  extra_cfg.alt_ref_aq = CAST(VP9E_SET_ALT_REF_AQ, args);
1107
0
  return update_extra_cfg(ctx, &extra_cfg);
1108
0
}
1109
1110
static vpx_codec_err_t ctrl_set_min_gf_interval(vpx_codec_alg_priv_t *ctx,
1111
0
                                                va_list args) {
1112
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1113
0
  extra_cfg.min_gf_interval = CAST(VP9E_SET_MIN_GF_INTERVAL, args);
1114
0
  return update_extra_cfg(ctx, &extra_cfg);
1115
0
}
1116
1117
static vpx_codec_err_t ctrl_set_max_gf_interval(vpx_codec_alg_priv_t *ctx,
1118
0
                                                va_list args) {
1119
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1120
0
  extra_cfg.max_gf_interval = CAST(VP9E_SET_MAX_GF_INTERVAL, args);
1121
0
  return update_extra_cfg(ctx, &extra_cfg);
1122
0
}
1123
1124
static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
1125
0
                                                     va_list args) {
1126
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1127
0
  extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
1128
0
  return update_extra_cfg(ctx, &extra_cfg);
1129
0
}
1130
1131
static vpx_codec_err_t ctrl_set_target_level(vpx_codec_alg_priv_t *ctx,
1132
4.79k
                                             va_list args) {
1133
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1134
4.79k
  extra_cfg.target_level = CAST(VP9E_SET_TARGET_LEVEL, args);
1135
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
1136
4.79k
}
1137
1138
static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
1139
0
                                       va_list args) {
1140
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1141
0
  extra_cfg.row_mt = CAST(VP9E_SET_ROW_MT, args);
1142
0
  return update_extra_cfg(ctx, &extra_cfg);
1143
0
}
1144
1145
static vpx_codec_err_t ctrl_set_rtc_external_ratectrl(vpx_codec_alg_priv_t *ctx,
1146
0
                                                      va_list args) {
1147
0
  VP9_COMP *const cpi = ctx->cpi;
1148
0
  const unsigned int data = va_arg(args, unsigned int);
1149
0
  if (data) {
1150
0
    cpi->compute_frame_low_motion_onepass = 0;
1151
0
    cpi->rc.constrain_gf_key_freq_onepass_vbr = 0;
1152
0
    cpi->cyclic_refresh->content_mode = 0;
1153
0
    cpi->disable_scene_detection_rtc_ratectrl = 1;
1154
0
  }
1155
0
  return VPX_CODEC_OK;
1156
0
}
1157
1158
static vpx_codec_err_t ctrl_enable_motion_vector_unit_test(
1159
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
1160
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1161
0
  extra_cfg.motion_vector_unit_test =
1162
0
      CAST(VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
1163
0
  return update_extra_cfg(ctx, &extra_cfg);
1164
0
}
1165
1166
0
static vpx_codec_err_t ctrl_get_level(vpx_codec_alg_priv_t *ctx, va_list args) {
1167
0
  int *const arg = va_arg(args, int *);
1168
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
1169
0
  *arg = (int)vp9_get_level(&ctx->cpi->level_info.level_spec);
1170
0
  return VPX_CODEC_OK;
1171
0
}
1172
1173
static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
1174
4.82k
                                    vpx_codec_priv_enc_mr_cfg_t *data) {
1175
4.82k
  vpx_codec_err_t res = VPX_CODEC_OK;
1176
4.82k
  (void)data;
1177
1178
4.82k
  if (ctx->priv == NULL) {
1179
4.82k
    vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
1180
4.82k
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
1181
1182
4.82k
    ctx->priv = (vpx_codec_priv_t *)priv;
1183
4.82k
    ctx->priv->init_flags = ctx->init_flags;
1184
4.82k
    ctx->priv->enc.total_encoders = 1;
1185
4.82k
    priv->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
1186
4.82k
    if (priv->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
1187
1188
4.82k
    if (ctx->config.enc) {
1189
      // Update the reference to the config structure to an internal copy.
1190
4.82k
      priv->cfg = *ctx->config.enc;
1191
4.82k
      ctx->config.enc = &priv->cfg;
1192
4.82k
    }
1193
1194
4.82k
    priv->extra_cfg = default_extra_cfg;
1195
4.82k
    vp9_initialize_enc();
1196
1197
4.82k
    res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
1198
1199
4.82k
    if (res == VPX_CODEC_OK) {
1200
4.79k
      priv->pts_offset_initialized = 0;
1201
4.79k
      priv->global_header_subsampling = -1;
1202
4.79k
      set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
1203
4.79k
#if CONFIG_VP9_HIGHBITDEPTH
1204
4.79k
      priv->oxcf.use_highbitdepth =
1205
4.79k
          (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
1206
4.79k
#endif
1207
4.79k
      priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
1208
4.79k
      if (priv->cpi == NULL) res = VPX_CODEC_MEM_ERROR;
1209
4.79k
      set_twopass_params_from_config(&priv->cfg, priv->cpi);
1210
4.79k
    }
1211
4.82k
  }
1212
1213
4.82k
  return res;
1214
4.82k
}
1215
1216
4.82k
static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
1217
4.82k
  free(ctx->cx_data);
1218
4.82k
  free(ctx->global_headers.buf);
1219
4.82k
  vp9_remove_compressor(ctx->cpi);
1220
4.82k
  vpx_free(ctx->buffer_pool);
1221
4.82k
  vpx_free(ctx);
1222
4.82k
  return VPX_CODEC_OK;
1223
4.82k
}
1224
1225
static vpx_codec_err_t pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
1226
                                               unsigned long duration,
1227
111k
                                               vpx_enc_deadline_t deadline) {
1228
111k
  MODE new_mode = BEST;
1229
1230
#if CONFIG_REALTIME_ONLY
1231
  (void)duration;
1232
  deadline = VPX_DL_REALTIME;
1233
#else
1234
111k
  switch (ctx->cfg.g_pass) {
1235
111k
    case VPX_RC_ONE_PASS:
1236
111k
      if (deadline > 0) {
1237
        // Convert duration parameter from stream timebase to microseconds.
1238
111k
        VPX_STATIC_ASSERT(TICKS_PER_SEC > 1000000 &&
1239
111k
                          (TICKS_PER_SEC % 1000000) == 0);
1240
1241
111k
        if (duration > UINT64_MAX / (uint64_t)ctx->oxcf.g_timebase_in_ts.num) {
1242
7
          ERROR("duration is too big");
1243
7
        }
1244
111k
        uint64_t duration_us = duration *
1245
111k
                               (uint64_t)ctx->oxcf.g_timebase_in_ts.num /
1246
111k
                               ((uint64_t)ctx->oxcf.g_timebase_in_ts.den *
1247
111k
                                (TICKS_PER_SEC / 1000000));
1248
1249
        // If the deadline is more that the duration this frame is to be shown,
1250
        // use good quality mode. Otherwise use realtime mode.
1251
111k
        new_mode = (deadline > duration_us) ? GOOD : REALTIME;
1252
111k
      } else {
1253
0
        new_mode = BEST;
1254
0
      }
1255
111k
      break;
1256
111k
    case VPX_RC_FIRST_PASS: break;
1257
0
    case VPX_RC_LAST_PASS: new_mode = deadline > 0 ? GOOD : BEST; break;
1258
111k
  }
1259
111k
#endif  // CONFIG_REALTIME_ONLY
1260
1261
111k
  if (deadline == VPX_DL_REALTIME) {
1262
0
    ctx->oxcf.pass = 0;
1263
0
    new_mode = REALTIME;
1264
0
    ctx->oxcf.speed = VPXMAX(ctx->oxcf.speed, 5);
1265
0
  }
1266
1267
111k
  if (ctx->oxcf.mode != new_mode) {
1268
2.79k
    ctx->oxcf.mode = new_mode;
1269
2.79k
    vp9_change_config(ctx->cpi, &ctx->oxcf);
1270
2.79k
  }
1271
111k
  return VPX_CODEC_OK;
1272
111k
}
1273
1274
// Turn on to test if supplemental superframe data breaks decoding
1275
// #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1276
0
static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
1277
0
  uint8_t marker = 0xc0;
1278
0
  unsigned int mask;
1279
0
  int mag, index_sz;
1280
1281
0
  assert(ctx->pending_frame_count <= 8);
1282
1283
  // Add the number of frames to the marker byte
1284
0
  marker |= ctx->pending_frame_count - 1;
1285
1286
  // Choose the magnitude
1287
0
  for (mag = 0, mask = 0xff; mag < 4; mag++) {
1288
0
    if (ctx->pending_frame_magnitude < mask) break;
1289
0
    mask <<= 8;
1290
0
    mask |= 0xff;
1291
0
  }
1292
0
  marker |= mag << 3;
1293
1294
  // Write the index
1295
0
  index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
1296
0
  unsigned char *cx_data_end = ctx->cx_data + ctx->cx_data_sz;
1297
0
  unsigned char *pending_cx_data_end =
1298
0
      ctx->pending_cx_data + ctx->pending_cx_data_sz;
1299
0
  ptrdiff_t space_remaining = cx_data_end - pending_cx_data_end;
1300
0
  if (index_sz <= space_remaining) {
1301
0
    uint8_t *x = pending_cx_data_end;
1302
0
    int i, j;
1303
#ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1304
    uint8_t marker_test = 0xc0;
1305
    int mag_test = 2;     // 1 - 4
1306
    int frames_test = 4;  // 1 - 8
1307
    int index_sz_test = 2 + mag_test * frames_test;
1308
    marker_test |= frames_test - 1;
1309
    marker_test |= (mag_test - 1) << 3;
1310
    *x++ = marker_test;
1311
    for (i = 0; i < mag_test * frames_test; ++i)
1312
      *x++ = 0;  // fill up with arbitrary data
1313
    *x++ = marker_test;
1314
    ctx->pending_cx_data_sz += index_sz_test;
1315
    printf("Added supplemental superframe data\n");
1316
#endif
1317
1318
0
    *x++ = marker;
1319
0
    for (i = 0; i < ctx->pending_frame_count; i++) {
1320
0
      unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
1321
1322
0
      for (j = 0; j <= mag; j++) {
1323
0
        *x++ = this_sz & 0xff;
1324
0
        this_sz >>= 8;
1325
0
      }
1326
0
    }
1327
0
    *x++ = marker;
1328
0
    ctx->pending_cx_data_sz += index_sz;
1329
#ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1330
    index_sz += index_sz_test;
1331
#endif
1332
0
  } else {
1333
0
    index_sz = 0;
1334
0
  }
1335
0
  return index_sz;
1336
0
}
1337
1338
static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
1339
73.9k
                                                   unsigned int lib_flags) {
1340
73.9k
  vpx_codec_frame_flags_t flags = lib_flags << 16;
1341
1342
73.9k
  if (lib_flags & FRAMEFLAGS_KEY ||
1343
58.2k
      (cpi->use_svc && cpi->svc
1344
0
                           .layer_context[cpi->svc.spatial_layer_id *
1345
0
                                              cpi->svc.number_temporal_layers +
1346
0
                                          cpi->svc.temporal_layer_id]
1347
0
                           .is_key_frame))
1348
15.6k
    flags |= VPX_FRAME_IS_KEY;
1349
1350
73.9k
  if (!cpi->common.show_frame) {
1351
0
    flags |= VPX_FRAME_IS_INVISIBLE;
1352
0
  }
1353
1354
73.9k
  if (cpi->droppable) flags |= VPX_FRAME_IS_DROPPABLE;
1355
1356
73.9k
  return flags;
1357
73.9k
}
1358
1359
0
static INLINE vpx_codec_cx_pkt_t get_psnr_pkt(const PSNR_STATS *psnr) {
1360
0
  vpx_codec_cx_pkt_t pkt;
1361
0
  pkt.kind = VPX_CODEC_PSNR_PKT;
1362
0
  pkt.data.psnr = *psnr;
1363
0
  return pkt;
1364
0
}
1365
1366
#if !CONFIG_REALTIME_ONLY
1367
static INLINE vpx_codec_cx_pkt_t
1368
0
get_first_pass_stats_pkt(FIRSTPASS_STATS *stats) {
1369
  // WARNNING: This function assumes that stats will
1370
  // exist and not be changed until the packet is processed
1371
  // TODO(angiebird): Refactor the code to avoid using the assumption.
1372
0
  vpx_codec_cx_pkt_t pkt;
1373
0
  pkt.kind = VPX_CODEC_STATS_PKT;
1374
0
  pkt.data.twopass_stats.buf = stats;
1375
0
  pkt.data.twopass_stats.sz = sizeof(*stats);
1376
0
  return pkt;
1377
0
}
1378
#endif
1379
1380
const size_t kMinCompressedSize = 8192;
1381
static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t *ctx,
1382
                                      const vpx_image_t *img,
1383
                                      vpx_codec_pts_t pts_val,
1384
                                      unsigned long duration,
1385
                                      vpx_enc_frame_flags_t enc_flags,
1386
112k
                                      vpx_enc_deadline_t deadline) {
1387
112k
  volatile vpx_codec_err_t res;
1388
112k
  volatile vpx_enc_frame_flags_t flags = enc_flags;
1389
112k
  volatile vpx_codec_pts_t pts = pts_val;
1390
112k
  VP9_COMP *const cpi = ctx->cpi;
1391
112k
  const vpx_rational64_t *const timebase_in_ts = &ctx->oxcf.g_timebase_in_ts;
1392
112k
  size_t data_sz;
1393
112k
  vpx_codec_cx_pkt_t pkt;
1394
112k
  memset(&pkt, 0, sizeof(pkt));
1395
1396
112k
  if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
1397
1398
112k
  cpi->last_coded_width = ctx->oxcf.width;
1399
112k
  cpi->last_coded_height = ctx->oxcf.height;
1400
1401
112k
  if (img != NULL) {
1402
75.5k
    res = validate_img(ctx, img);
1403
75.5k
    if (res != VPX_CODEC_OK) {
1404
1.00k
      return res;
1405
1.00k
    }
1406
    // There's no codec control for multiple alt-refs so check the encoder
1407
    // instance for its status to determine the compressed data size.
1408
74.5k
    data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
1409
74.5k
              (cpi->multi_layer_arf ? 8 : 2);
1410
74.5k
    if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
1411
74.5k
    if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1412
4.55k
      ctx->cx_data_sz = data_sz;
1413
4.55k
      free(ctx->cx_data);
1414
4.55k
      ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1415
4.55k
      if (ctx->cx_data == NULL) {
1416
0
        return VPX_CODEC_MEM_ERROR;
1417
0
      }
1418
4.55k
    }
1419
1420
74.5k
    int chroma_subsampling = -1;
1421
74.5k
    if ((img->fmt & VPX_IMG_FMT_I420) == VPX_IMG_FMT_I420 ||
1422
3.64k
        (img->fmt & VPX_IMG_FMT_NV12) == VPX_IMG_FMT_NV12 ||
1423
70.8k
        (img->fmt & VPX_IMG_FMT_YV12) == VPX_IMG_FMT_YV12) {
1424
70.8k
      chroma_subsampling = 1;  // matches default for Codec Parameter String
1425
70.8k
    } else if ((img->fmt & VPX_IMG_FMT_I422) == VPX_IMG_FMT_I422) {
1426
3.64k
      chroma_subsampling = 2;
1427
3.64k
    } else if ((img->fmt & VPX_IMG_FMT_I444) == VPX_IMG_FMT_I444) {
1428
0
      chroma_subsampling = 3;
1429
0
    }
1430
74.5k
    if (chroma_subsampling > ctx->global_header_subsampling) {
1431
4.55k
      ctx->global_header_subsampling = chroma_subsampling;
1432
4.55k
    }
1433
74.5k
  }
1434
1435
111k
  res = pick_quickcompress_mode(ctx, duration, deadline);
1436
111k
  if (res != VPX_CODEC_OK) {
1437
7
    return res;
1438
7
  }
1439
111k
  vpx_codec_pkt_list_init(&ctx->pkt_list);
1440
1441
  // Handle Flags
1442
111k
  if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
1443
111k
      ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
1444
0
    ctx->base.err_detail = "Conflicting flags.";
1445
0
    return VPX_CODEC_INVALID_PARAM;
1446
0
  }
1447
1448
111k
  if (setjmp(cpi->common.error.jmp)) {
1449
102
    cpi->common.error.setjmp = 0;
1450
102
    res = update_error_state(ctx, &cpi->common.error);
1451
102
    vpx_clear_system_state();
1452
102
    return res;
1453
102
  }
1454
111k
  cpi->common.error.setjmp = 1;
1455
1456
111k
  vp9_apply_encoding_flags(cpi, flags);
1457
1458
  // Handle fixed keyframe intervals
1459
111k
  if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
1460
111k
      ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1461
11.7k
    if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1462
11.7k
      flags |= VPX_EFLAG_FORCE_KF;
1463
11.7k
      ctx->fixed_kf_cntr = 1;
1464
11.7k
    }
1465
11.7k
  }
1466
1467
111k
  unsigned int lib_flags = 0;
1468
111k
  size_t size, cx_data_sz;
1469
111k
  unsigned char *cx_data;
1470
1471
  // Per-frame PSNR is not supported when g_lag_in_frames is greater than 0.
1472
111k
  if ((flags & VPX_EFLAG_CALCULATE_PSNR) && ctx->cfg.g_lag_in_frames != 0) {
1473
0
    vpx_internal_error(
1474
0
        &ctx->cpi->common.error, VPX_CODEC_INCAPABLE,
1475
0
        "Cannot calculate per-frame PSNR when g_lag_in_frames is nonzero");
1476
0
  }
1477
  // Set up internal flags
1478
#if CONFIG_INTERNAL_STATS
1479
  assert(cpi->b_calculate_psnr == 1);
1480
#else
1481
111k
  cpi->b_calculate_psnr = (ctx->base.init_flags & VPX_CODEC_USE_PSNR) ||
1482
111k
                          (flags & VPX_EFLAG_CALCULATE_PSNR);
1483
111k
#endif
1484
1485
111k
  if (img != NULL) {
1486
74.5k
    YV12_BUFFER_CONFIG sd;
1487
1488
74.5k
    if (!ctx->pts_offset_initialized) {
1489
4.55k
      ctx->pts_offset = pts;
1490
4.55k
      ctx->pts_offset_initialized = 1;
1491
4.55k
    }
1492
74.5k
    if (pts < ctx->pts_offset) {
1493
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_INVALID_PARAM,
1494
0
                         "pts is smaller than initial pts");
1495
0
    }
1496
74.5k
    pts -= ctx->pts_offset;
1497
74.5k
    if (pts > INT64_MAX / timebase_in_ts->num) {
1498
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_INVALID_PARAM,
1499
0
                         "conversion of relative pts to ticks would overflow");
1500
0
    }
1501
74.5k
    const int64_t dst_time_stamp = timebase_units_to_ticks(timebase_in_ts, pts);
1502
1503
74.5k
    cpi->svc.timebase_fac = timebase_units_to_ticks(timebase_in_ts, 1);
1504
74.5k
    cpi->svc.time_stamp_superframe = dst_time_stamp;
1505
1506
74.5k
#if ULONG_MAX > INT64_MAX
1507
74.5k
    if (duration > INT64_MAX) {
1508
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_INVALID_PARAM,
1509
0
                         "duration is too big");
1510
0
    }
1511
74.5k
#endif
1512
74.5k
    if (pts > INT64_MAX - (int64_t)duration) {
1513
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_INVALID_PARAM,
1514
0
                         "relative pts + duration is too big");
1515
0
    }
1516
74.5k
    vpx_codec_pts_t pts_end = pts + (int64_t)duration;
1517
74.5k
    if (pts_end > INT64_MAX / timebase_in_ts->num) {
1518
3
      vpx_internal_error(
1519
3
          &cpi->common.error, VPX_CODEC_INVALID_PARAM,
1520
3
          "conversion of relative pts + duration to ticks would overflow");
1521
3
    }
1522
74.5k
    const int64_t dst_end_time_stamp =
1523
74.5k
        timebase_units_to_ticks(timebase_in_ts, pts_end);
1524
74.5k
    res = image2yuvconfig(img, &sd);
1525
1526
    // Store the original flags in to the frame buffer. Will extract the
1527
    // key frame flag when we actually encode this frame.
1528
74.5k
    if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1529
74.5k
                              dst_time_stamp, dst_end_time_stamp)) {
1530
0
      res = update_error_state(ctx, &cpi->common.error);
1531
0
    }
1532
74.5k
    ctx->next_frame_flags = 0;
1533
74.5k
  }
1534
1535
111k
  cx_data = ctx->cx_data;
1536
111k
  cx_data_sz = ctx->cx_data_sz;
1537
1538
  /* Any pending invisible frames? */
1539
111k
  if (ctx->pending_cx_data) {
1540
0
    assert(cx_data_sz >= ctx->pending_cx_data_sz);
1541
0
    memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1542
0
    ctx->pending_cx_data = cx_data;
1543
0
    cx_data += ctx->pending_cx_data_sz;
1544
0
    cx_data_sz -= ctx->pending_cx_data_sz;
1545
1546
    /* TODO(webm:1844): this is a minimal check, the underlying codec doesn't
1547
     * respect the buffer size anyway.
1548
     */
1549
0
    if (cx_data_sz < ctx->cx_data_sz / 2) {
1550
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_ERROR,
1551
0
                         "Compressed data buffer too small");
1552
0
    }
1553
0
  }
1554
1555
111k
  if (cpi->oxcf.pass == 1 && !cpi->use_svc) {
1556
0
#if !CONFIG_REALTIME_ONLY
1557
    // compute first pass stats
1558
0
    if (img) {
1559
0
      int ret;
1560
0
      int64_t dst_time_stamp;
1561
0
      int64_t dst_end_time_stamp;
1562
0
      vpx_codec_cx_pkt_t fps_pkt;
1563
0
      ENCODE_FRAME_RESULT encode_frame_result;
1564
0
      vp9_init_encode_frame_result(&encode_frame_result);
1565
      // TODO(angiebird): Call vp9_first_pass directly
1566
0
      ret = vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data, cx_data_sz,
1567
0
                                    &dst_time_stamp, &dst_end_time_stamp, !img,
1568
0
                                    &encode_frame_result);
1569
0
      assert(size == 0);  // There is no compressed data in the first pass
1570
0
      (void)ret;
1571
0
      assert(ret == 0);
1572
0
      fps_pkt = get_first_pass_stats_pkt(&cpi->twopass.this_frame_stats);
1573
0
      vpx_codec_pkt_list_add(&ctx->pkt_list.head, &fps_pkt);
1574
0
    } else {
1575
0
      if (!cpi->twopass.first_pass_done) {
1576
0
        vpx_codec_cx_pkt_t fps_pkt;
1577
0
        vp9_end_first_pass(cpi);
1578
0
        fps_pkt = get_first_pass_stats_pkt(&cpi->twopass.total_stats);
1579
0
        vpx_codec_pkt_list_add(&ctx->pkt_list.head, &fps_pkt);
1580
0
      }
1581
0
    }
1582
#else   // !CONFIG_REALTIME_ONLY
1583
    assert(0);
1584
#endif  // !CONFIG_REALTIME_ONLY
1585
111k
  } else {
1586
111k
    ENCODE_FRAME_RESULT encode_frame_result;
1587
111k
    int64_t dst_time_stamp;
1588
111k
    int64_t dst_end_time_stamp;
1589
111k
    vp9_init_encode_frame_result(&encode_frame_result);
1590
184k
    while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1591
181k
           -1 != vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1592
181k
                                         cx_data_sz, &dst_time_stamp,
1593
181k
                                         &dst_end_time_stamp, !img,
1594
181k
                                         &encode_frame_result)) {
1595
      // Pack psnr pkt.
1596
73.9k
      if (size > 0) {
1597
73.9k
        PSNR_STATS psnr;
1598
73.9k
        if (vp9_get_psnr(cpi, &psnr)) {
1599
0
          vpx_codec_cx_pkt_t psnr_pkt = get_psnr_pkt(&psnr);
1600
0
          vpx_codec_pkt_list_add(&ctx->pkt_list.head, &psnr_pkt);
1601
0
        }
1602
73.9k
      }
1603
1604
73.9k
      if (size || (cpi->use_svc && cpi->svc.skip_enhancement_layer)) {
1605
        // Pack invisible frames with the next visible frame
1606
73.9k
        if (!cpi->common.show_frame ||
1607
73.9k
            (cpi->use_svc &&
1608
0
             cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)) {
1609
0
          if (ctx->pending_cx_data == NULL) ctx->pending_cx_data = cx_data;
1610
0
          ctx->pending_cx_data_sz += size;
1611
0
          if (size) ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1612
0
          ctx->pending_frame_magnitude |= size;
1613
0
          cx_data += size;
1614
0
          cx_data_sz -= size;
1615
0
          pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
1616
0
          pkt.data.frame.height[cpi->svc.spatial_layer_id] = cpi->common.height;
1617
0
          pkt.data.frame.spatial_layer_encoded[cpi->svc.spatial_layer_id] =
1618
0
              1 - cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id];
1619
1620
0
          if (ctx->output_cx_pkt_cb.output_cx_pkt) {
1621
0
            pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1622
0
            pkt.data.frame.pts =
1623
0
                ticks_to_timebase_units(timebase_in_ts, dst_time_stamp) +
1624
0
                ctx->pts_offset;
1625
0
            pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1626
0
                timebase_in_ts, dst_end_time_stamp - dst_time_stamp);
1627
0
            pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1628
0
            pkt.data.frame.buf = ctx->pending_cx_data;
1629
0
            pkt.data.frame.sz = size;
1630
0
            ctx->pending_cx_data = NULL;
1631
0
            ctx->pending_cx_data_sz = 0;
1632
0
            ctx->pending_frame_count = 0;
1633
0
            ctx->pending_frame_magnitude = 0;
1634
0
            ctx->output_cx_pkt_cb.output_cx_pkt(
1635
0
                &pkt, ctx->output_cx_pkt_cb.user_priv);
1636
0
          }
1637
0
          continue;
1638
0
        }
1639
1640
        // Add the frame packet to the list of returned packets.
1641
73.9k
        pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1642
73.9k
        pkt.data.frame.pts =
1643
73.9k
            ticks_to_timebase_units(timebase_in_ts, dst_time_stamp) +
1644
73.9k
            ctx->pts_offset;
1645
73.9k
        pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1646
73.9k
            timebase_in_ts, dst_end_time_stamp - dst_time_stamp);
1647
73.9k
        pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1648
73.9k
        pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
1649
73.9k
        pkt.data.frame.height[cpi->svc.spatial_layer_id] = cpi->common.height;
1650
73.9k
        pkt.data.frame.spatial_layer_encoded[cpi->svc.spatial_layer_id] =
1651
73.9k
            1 - cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id];
1652
1653
73.9k
        if (ctx->pending_cx_data) {
1654
0
          if (size) ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1655
0
          ctx->pending_frame_magnitude |= size;
1656
0
          ctx->pending_cx_data_sz += size;
1657
          // write the superframe only for the case when the callback function
1658
          // for getting per-layer packets is not registered.
1659
0
          if (!ctx->output_cx_pkt_cb.output_cx_pkt &&
1660
0
              ctx->pending_frame_count > 0) {
1661
0
            size += write_superframe_index(ctx);
1662
0
            assert(size <= cx_data_sz);
1663
0
          }
1664
0
          pkt.data.frame.buf = ctx->pending_cx_data;
1665
0
          pkt.data.frame.sz = ctx->pending_cx_data_sz;
1666
0
          ctx->pending_cx_data = NULL;
1667
0
          ctx->pending_cx_data_sz = 0;
1668
0
          ctx->pending_frame_count = 0;
1669
0
          ctx->pending_frame_magnitude = 0;
1670
73.9k
        } else {
1671
73.9k
          pkt.data.frame.buf = cx_data;
1672
73.9k
          pkt.data.frame.sz = size;
1673
73.9k
        }
1674
73.9k
        pkt.data.frame.partition_id = -1;
1675
1676
73.9k
        if (ctx->output_cx_pkt_cb.output_cx_pkt)
1677
0
          ctx->output_cx_pkt_cb.output_cx_pkt(&pkt,
1678
0
                                              ctx->output_cx_pkt_cb.user_priv);
1679
73.9k
        else
1680
73.9k
          vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1681
1682
73.9k
        cx_data += size;
1683
73.9k
        cx_data_sz -= size;
1684
73.9k
        if (is_one_pass_svc(cpi) &&
1685
0
            (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
1686
          // Encoded all spatial layers; exit loop.
1687
0
          break;
1688
0
        }
1689
73.9k
      }
1690
73.9k
    }
1691
111k
  }
1692
1693
111k
  cpi->common.error.setjmp = 0;
1694
111k
  return res;
1695
111k
}
1696
1697
static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1698
184k
                                                    vpx_codec_iter_t *iter) {
1699
184k
  return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1700
184k
}
1701
1702
static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
1703
0
                                          va_list args) {
1704
0
  vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1705
1706
0
  if (frame != NULL) {
1707
0
    YV12_BUFFER_CONFIG sd;
1708
1709
0
    if (frame->img.planes[VPX_PLANE_Y] == NULL ||
1710
0
        frame->img.planes[VPX_PLANE_U] == NULL ||
1711
0
        frame->img.planes[VPX_PLANE_V] == NULL) {
1712
0
      return VPX_CODEC_INVALID_PARAM;
1713
0
    }
1714
1715
0
    image2yuvconfig(&frame->img, &sd);
1716
0
    if (vp9_set_reference_enc(
1717
0
            ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type), &sd)) {
1718
0
      return VPX_CODEC_ERROR;
1719
0
    }
1720
0
    return VPX_CODEC_OK;
1721
0
  }
1722
0
  return VPX_CODEC_INVALID_PARAM;
1723
0
}
1724
1725
static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
1726
0
                                           va_list args) {
1727
0
  vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1728
1729
0
  if (frame != NULL) {
1730
0
    YV12_BUFFER_CONFIG sd;
1731
1732
0
    if (frame->img.planes[VPX_PLANE_Y] == NULL ||
1733
0
        frame->img.planes[VPX_PLANE_U] == NULL ||
1734
0
        frame->img.planes[VPX_PLANE_V] == NULL) {
1735
0
      return VPX_CODEC_INVALID_PARAM;
1736
0
    }
1737
1738
0
    image2yuvconfig(&frame->img, &sd);
1739
0
    if (vp9_copy_reference_enc(
1740
0
            ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type), &sd)) {
1741
0
      return VPX_CODEC_ERROR;
1742
0
    }
1743
0
    return VPX_CODEC_OK;
1744
0
  }
1745
0
  return VPX_CODEC_INVALID_PARAM;
1746
0
}
1747
1748
static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
1749
0
                                          va_list args) {
1750
0
  vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
1751
1752
0
  if (frame != NULL) {
1753
0
    const int fb_idx = ctx->cpi->common.cur_show_frame_fb_idx;
1754
0
    YV12_BUFFER_CONFIG *fb = get_buf_frame(&ctx->cpi->common, fb_idx);
1755
0
    if (fb == NULL) return VPX_CODEC_ERROR;
1756
0
    yuvconfig2image(&frame->img, fb, NULL);
1757
0
    return VPX_CODEC_OK;
1758
0
  }
1759
0
  return VPX_CODEC_INVALID_PARAM;
1760
0
}
1761
1762
static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
1763
0
                                          va_list args) {
1764
#if CONFIG_VP9_POSTPROC
1765
  vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
1766
  if (config != NULL) {
1767
    ctx->preview_ppcfg = *config;
1768
    return VPX_CODEC_OK;
1769
  }
1770
  return VPX_CODEC_INVALID_PARAM;
1771
#else
1772
0
  (void)ctx;
1773
0
  (void)args;
1774
0
  return VPX_CODEC_INCAPABLE;
1775
0
#endif
1776
0
}
1777
1778
// Returns the contents of CodecPrivate described in:
1779
// https://www.webmproject.org/docs/container/#vp9-codec-feature-metadata-codecprivate
1780
// This includes Profile, Level, Bit depth and Chroma subsampling. Each entry
1781
// is 3 bytes. 1 byte ID, 1 byte length (= 1) and 1 byte value.
1782
0
static vpx_fixed_buf_t *encoder_get_global_headers(vpx_codec_alg_priv_t *ctx) {
1783
0
  if (!ctx->cpi) return NULL;
1784
1785
0
  const unsigned int profile = ctx->cfg.g_profile;
1786
0
  const VP9_LEVEL level = vp9_get_level(&ctx->cpi->level_info.level_spec);
1787
0
  const vpx_bit_depth_t bit_depth = ctx->cfg.g_bit_depth;
1788
0
  const int subsampling = ctx->global_header_subsampling;
1789
0
  const uint8_t buf[12] = {
1790
0
    1, 1, (uint8_t)profile,   2, 1, (uint8_t)level,
1791
0
    3, 1, (uint8_t)bit_depth, 4, 1, (uint8_t)subsampling
1792
0
  };
1793
1794
0
  if (ctx->global_headers.buf) free(ctx->global_headers.buf);
1795
0
  ctx->global_headers.buf = malloc(sizeof(buf));
1796
0
  if (!ctx->global_headers.buf) return NULL;
1797
1798
0
  ctx->global_headers.sz = sizeof(buf);
1799
  // No data or I440, which isn't mapped.
1800
0
  if (ctx->global_header_subsampling == -1) ctx->global_headers.sz -= 3;
1801
0
  memcpy(ctx->global_headers.buf, buf, ctx->global_headers.sz);
1802
1803
0
  return &ctx->global_headers;
1804
0
}
1805
1806
0
static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
1807
0
  YV12_BUFFER_CONFIG sd;
1808
0
  vp9_ppflags_t flags;
1809
0
  vp9_zero(flags);
1810
1811
0
  if (ctx->preview_ppcfg.post_proc_flag) {
1812
0
    flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1813
0
    flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1814
0
    flags.noise_level = ctx->preview_ppcfg.noise_level;
1815
0
  }
1816
1817
0
  if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
1818
0
    yuvconfig2image(&ctx->preview_img, &sd, NULL);
1819
0
    return &ctx->preview_img;
1820
0
  }
1821
0
  return NULL;
1822
0
}
1823
1824
static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
1825
0
                                        va_list args) {
1826
0
  vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1827
1828
0
  if (data) {
1829
0
    vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1830
0
    return vp9_set_roi_map(ctx->cpi, roi->roi_map, roi->rows, roi->cols,
1831
0
                           roi->delta_q, roi->delta_lf, roi->skip,
1832
0
                           roi->ref_frame);
1833
0
  }
1834
0
  return VPX_CODEC_INVALID_PARAM;
1835
0
}
1836
1837
static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
1838
0
                                           va_list args) {
1839
0
  vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1840
1841
0
  if (map) {
1842
0
    if (!vp9_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
1843
0
                            (int)map->cols))
1844
0
      return VPX_CODEC_OK;
1845
1846
0
    return VPX_CODEC_INVALID_PARAM;
1847
0
  }
1848
0
  return VPX_CODEC_INVALID_PARAM;
1849
0
}
1850
1851
static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
1852
0
                                           va_list args) {
1853
0
  vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1854
1855
0
  if (map) {
1856
0
    if (!vp9_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
1857
0
                            (int)map->cols))
1858
0
      return VPX_CODEC_OK;
1859
1860
0
    return VPX_CODEC_INVALID_PARAM;
1861
0
  }
1862
0
  return VPX_CODEC_INVALID_PARAM;
1863
0
}
1864
1865
static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
1866
0
                                           va_list args) {
1867
0
  vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
1868
1869
0
  if (mode) {
1870
0
    const int res = vp9_set_internal_size(ctx->cpi, mode->h_scaling_mode,
1871
0
                                          mode->v_scaling_mode);
1872
0
    return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
1873
0
  }
1874
0
  return VPX_CODEC_INVALID_PARAM;
1875
0
}
1876
1877
0
static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
1878
0
  int data = va_arg(args, int);
1879
0
  const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
1880
  // SVC is only supported for one-pass.
1881
0
  if (data == 1 &&
1882
0
      (cfg->g_pass == VPX_RC_FIRST_PASS || cfg->g_pass == VPX_RC_LAST_PASS)) {
1883
0
    return VPX_CODEC_INVALID_PARAM;
1884
0
  }
1885
0
  vp9_set_svc(ctx->cpi, data);
1886
0
  vp9_set_row_mt(ctx->cpi);
1887
0
  return VPX_CODEC_OK;
1888
0
}
1889
1890
static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1891
0
                                             va_list args) {
1892
0
  vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
1893
0
  VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1894
0
  SVC *const svc = &cpi->svc;
1895
0
  int sl;
1896
  // Checks on valid spatial_layer_id input.
1897
0
  if (data->spatial_layer_id < 0 ||
1898
0
      data->spatial_layer_id >= (int)ctx->cfg.ss_number_layers) {
1899
0
    return VPX_CODEC_INVALID_PARAM;
1900
0
  }
1901
  // Validate temporal_layer_id and every temporal_layer_id_per_spatial[]
1902
  // entry before storing any of them. Both are later used to index
1903
  // svc.layer_context[] (VPX_MAX_LAYERS entries) in
1904
  // vp9_one_pass_svc_start_layer(); an out-of-range value causes an
1905
  // out-of-bounds access during encoding. Validating up front also leaves
1906
  // svc unchanged when the input is rejected.
1907
0
  if (data->temporal_layer_id < 0 ||
1908
0
      data->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
1909
0
    return VPX_CODEC_INVALID_PARAM;
1910
0
  }
1911
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1912
0
    if (data->temporal_layer_id_per_spatial[sl] < 0 ||
1913
0
        data->temporal_layer_id_per_spatial[sl] >=
1914
0
            (int)ctx->cfg.ts_number_layers) {
1915
0
      return VPX_CODEC_INVALID_PARAM;
1916
0
    }
1917
0
  }
1918
1919
0
  svc->spatial_layer_to_encode = data->spatial_layer_id;
1920
0
  svc->first_spatial_layer_to_encode = data->spatial_layer_id;
1921
  // TODO(jianj): Deprecated to be removed.
1922
0
  svc->temporal_layer_id = data->temporal_layer_id;
1923
  // Allow for setting temporal layer per spatial layer for superframe.
1924
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1925
0
    svc->temporal_layer_id_per_spatial[sl] =
1926
0
        data->temporal_layer_id_per_spatial[sl];
1927
0
  }
1928
1929
0
  return VPX_CODEC_OK;
1930
0
}
1931
1932
static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1933
0
                                             va_list args) {
1934
0
  vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
1935
0
  VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1936
0
  SVC *const svc = &cpi->svc;
1937
1938
0
  data->spatial_layer_id = svc->spatial_layer_id;
1939
0
  data->temporal_layer_id = svc->temporal_layer_id;
1940
1941
0
  return VPX_CODEC_OK;
1942
0
}
1943
1944
static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1945
0
                                               va_list args) {
1946
0
  VP9_COMP *const cpi = ctx->cpi;
1947
0
  vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
1948
0
  int sl, tl;
1949
1950
  // Number of temporal layers and number of spatial layers have to be set
1951
  // properly before calling this control function.
1952
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1953
0
    for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
1954
0
      const int layer =
1955
0
          LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
1956
0
      LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1957
0
      if (params->max_quantizers[layer] < 0 ||
1958
0
          params->max_quantizers[layer] > 63 ||
1959
0
          params->min_quantizers[layer] < 0 ||
1960
0
          params->min_quantizers[layer] > params->max_quantizers[layer]) {
1961
0
        return VPX_CODEC_INVALID_PARAM;
1962
0
      }
1963
0
      lc->max_q = params->max_quantizers[layer];
1964
0
      lc->min_q = params->min_quantizers[layer];
1965
      // Checks on valid scale factors.
1966
0
      if (params->scaling_factor_num[sl] < 1 ||
1967
0
          params->scaling_factor_den[sl] < 1 ||
1968
0
          (params->scaling_factor_num[sl] > params->scaling_factor_den[sl])) {
1969
0
        return VPX_CODEC_INVALID_PARAM;
1970
0
      }
1971
0
      lc->scaling_factor_num = params->scaling_factor_num[sl];
1972
0
      lc->scaling_factor_den = params->scaling_factor_den[sl];
1973
0
      lc->speed = params->speed_per_layer[sl];
1974
0
      lc->loopfilter_ctrl = params->loopfilter_ctrl[sl];
1975
0
    }
1976
0
  }
1977
1978
0
  return VPX_CODEC_OK;
1979
0
}
1980
1981
static vpx_codec_err_t ctrl_get_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
1982
0
                                                     va_list args) {
1983
0
  VP9_COMP *const cpi = ctx->cpi;
1984
0
  vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
1985
0
  int sl;
1986
0
  for (sl = 0; sl <= cpi->svc.spatial_layer_id; sl++) {
1987
0
    data->update_buffer_slot[sl] = cpi->svc.update_buffer_slot[sl];
1988
0
    data->reference_last[sl] = cpi->svc.reference_last[sl];
1989
0
    data->reference_golden[sl] = cpi->svc.reference_golden[sl];
1990
0
    data->reference_alt_ref[sl] = cpi->svc.reference_altref[sl];
1991
0
    data->lst_fb_idx[sl] = cpi->svc.lst_fb_idx[sl];
1992
0
    data->gld_fb_idx[sl] = cpi->svc.gld_fb_idx[sl];
1993
0
    data->alt_fb_idx[sl] = cpi->svc.alt_fb_idx[sl];
1994
    // TODO(jianj): Remove these 3, deprecated.
1995
0
    data->update_last[sl] = cpi->svc.update_last[sl];
1996
0
    data->update_golden[sl] = cpi->svc.update_golden[sl];
1997
0
    data->update_alt_ref[sl] = cpi->svc.update_altref[sl];
1998
0
  }
1999
0
  return VPX_CODEC_OK;
2000
0
}
2001
2002
static vpx_codec_err_t ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
2003
0
                                                     va_list args) {
2004
0
  VP9_COMP *const cpi = ctx->cpi;
2005
0
  vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
2006
0
  int sl;
2007
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
2008
0
    if (data->lst_fb_idx[sl] < 0 || data->lst_fb_idx[sl] >= REF_FRAMES ||
2009
0
        data->gld_fb_idx[sl] < 0 || data->gld_fb_idx[sl] >= REF_FRAMES ||
2010
0
        data->alt_fb_idx[sl] < 0 || data->alt_fb_idx[sl] >= REF_FRAMES) {
2011
0
      return VPX_CODEC_INVALID_PARAM;
2012
0
    }
2013
0
  }
2014
0
  cpi->svc.use_set_ref_frame_config = 1;
2015
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
2016
0
    cpi->svc.update_buffer_slot[sl] = data->update_buffer_slot[sl];
2017
0
    cpi->svc.reference_last[sl] = data->reference_last[sl];
2018
0
    cpi->svc.reference_golden[sl] = data->reference_golden[sl];
2019
0
    cpi->svc.reference_altref[sl] = data->reference_alt_ref[sl];
2020
0
    cpi->svc.lst_fb_idx[sl] = data->lst_fb_idx[sl];
2021
0
    cpi->svc.gld_fb_idx[sl] = data->gld_fb_idx[sl];
2022
0
    cpi->svc.alt_fb_idx[sl] = data->alt_fb_idx[sl];
2023
0
    cpi->svc.duration[sl] = data->duration[sl];
2024
0
  }
2025
0
  cpi->svc.temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
2026
0
  return VPX_CODEC_OK;
2027
0
}
2028
2029
static vpx_codec_err_t ctrl_set_svc_inter_layer_pred(vpx_codec_alg_priv_t *ctx,
2030
0
                                                     va_list args) {
2031
0
  const int data = va_arg(args, int);
2032
0
  VP9_COMP *const cpi = ctx->cpi;
2033
0
  cpi->svc.disable_inter_layer_pred = data;
2034
0
  return VPX_CODEC_OK;
2035
0
}
2036
2037
static vpx_codec_err_t ctrl_set_svc_frame_drop_layer(vpx_codec_alg_priv_t *ctx,
2038
0
                                                     va_list args) {
2039
0
  VP9_COMP *const cpi = ctx->cpi;
2040
0
  vpx_svc_frame_drop_t *data = va_arg(args, vpx_svc_frame_drop_t *);
2041
0
  int sl;
2042
0
  cpi->svc.framedrop_mode = data->framedrop_mode;
2043
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl)
2044
0
    cpi->svc.framedrop_thresh[sl] = data->framedrop_thresh[sl];
2045
  // Don't allow max_consec_drop values below 1.
2046
0
  cpi->svc.max_consec_drop = VPXMAX(1, data->max_consec_drop);
2047
0
  return VPX_CODEC_OK;
2048
0
}
2049
2050
static vpx_codec_err_t ctrl_set_svc_gf_temporal_ref(vpx_codec_alg_priv_t *ctx,
2051
0
                                                    va_list args) {
2052
0
  VP9_COMP *const cpi = ctx->cpi;
2053
0
  const unsigned int data = va_arg(args, unsigned int);
2054
0
  cpi->svc.use_gf_temporal_ref = data;
2055
0
  return VPX_CODEC_OK;
2056
0
}
2057
2058
static vpx_codec_err_t ctrl_set_svc_spatial_layer_sync(
2059
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
2060
0
  VP9_COMP *const cpi = ctx->cpi;
2061
0
  vpx_svc_spatial_layer_sync_t *data =
2062
0
      va_arg(args, vpx_svc_spatial_layer_sync_t *);
2063
0
  int sl;
2064
0
  for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl)
2065
0
    cpi->svc.spatial_layer_sync[sl] = data->spatial_layer_sync[sl];
2066
0
  cpi->svc.set_intra_only_frame = data->base_layer_intra_only;
2067
0
  return VPX_CODEC_OK;
2068
0
}
2069
2070
static vpx_codec_err_t ctrl_set_delta_q_uv(vpx_codec_alg_priv_t *ctx,
2071
0
                                           va_list args) {
2072
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2073
0
  int data = va_arg(args, int);
2074
0
  data = clamp(data, -15, 15);
2075
0
  extra_cfg.delta_q_uv = data;
2076
0
  return update_extra_cfg(ctx, &extra_cfg);
2077
0
}
2078
2079
static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
2080
0
                                                 va_list args) {
2081
0
  vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
2082
0
      (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
2083
0
  ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
2084
0
  ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
2085
2086
0
  return VPX_CODEC_OK;
2087
0
}
2088
2089
static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
2090
0
                                             va_list args) {
2091
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2092
0
  extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
2093
0
  return update_extra_cfg(ctx, &extra_cfg);
2094
0
}
2095
2096
static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
2097
4.25k
                                            va_list args) {
2098
4.25k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2099
4.25k
  extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
2100
4.25k
  return update_extra_cfg(ctx, &extra_cfg);
2101
4.25k
}
2102
2103
static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
2104
4.79k
                                            va_list args) {
2105
4.79k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2106
4.79k
  extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
2107
4.79k
  return update_extra_cfg(ctx, &extra_cfg);
2108
4.79k
}
2109
2110
static vpx_codec_err_t ctrl_set_render_size(vpx_codec_alg_priv_t *ctx,
2111
0
                                            va_list args) {
2112
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2113
0
  int *const render_size = va_arg(args, int *);
2114
0
  extra_cfg.render_width = render_size[0];
2115
0
  extra_cfg.render_height = render_size[1];
2116
0
  return update_extra_cfg(ctx, &extra_cfg);
2117
0
}
2118
2119
static vpx_codec_err_t ctrl_set_postencode_drop(vpx_codec_alg_priv_t *ctx,
2120
0
                                                va_list args) {
2121
0
  VP9_COMP *const cpi = ctx->cpi;
2122
0
  const unsigned int data = va_arg(args, unsigned int);
2123
0
  cpi->rc.ext_use_post_encode_drop = data;
2124
0
  return VPX_CODEC_OK;
2125
0
}
2126
2127
static vpx_codec_err_t ctrl_set_disable_overshoot_maxq_cbr(
2128
0
    vpx_codec_alg_priv_t *ctx, va_list args) {
2129
0
  VP9_COMP *const cpi = ctx->cpi;
2130
0
  const unsigned int data = va_arg(args, unsigned int);
2131
0
  cpi->rc.disable_overshoot_maxq_cbr = data;
2132
0
  return VPX_CODEC_OK;
2133
0
}
2134
2135
static vpx_codec_err_t ctrl_set_disable_loopfilter(vpx_codec_alg_priv_t *ctx,
2136
0
                                                   va_list args) {
2137
0
  VP9_COMP *const cpi = ctx->cpi;
2138
0
  const unsigned int data = va_arg(args, unsigned int);
2139
0
  cpi->loopfilter_ctrl = data;
2140
0
  return VPX_CODEC_OK;
2141
0
}
2142
2143
static vpx_codec_err_t ctrl_set_external_rate_control(vpx_codec_alg_priv_t *ctx,
2144
0
                                                      va_list args) {
2145
0
  vpx_rc_funcs_t funcs = *CAST(VP9E_SET_EXTERNAL_RATE_CONTROL, args);
2146
0
  VP9_COMP *cpi = ctx->cpi;
2147
0
  EXT_RATECTRL *ext_ratectrl = &cpi->ext_ratectrl;
2148
0
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
2149
0
  if (oxcf->pass == 2) {
2150
0
    const FRAME_INFO *frame_info = &cpi->frame_info;
2151
0
    vpx_rc_config_t ratectrl_config;
2152
0
    vpx_codec_err_t codec_status;
2153
0
    memset(&ratectrl_config, 0, sizeof(ratectrl_config));
2154
2155
0
    ratectrl_config.frame_width = frame_info->frame_width;
2156
0
    ratectrl_config.frame_height = frame_info->frame_height;
2157
0
    ratectrl_config.show_frame_count = cpi->twopass.first_pass_info.num_frames;
2158
0
    ratectrl_config.max_gf_interval = oxcf->max_gf_interval;
2159
0
    ratectrl_config.min_gf_interval = oxcf->min_gf_interval;
2160
    // TODO(angiebird): Double check whether this is the proper way to set up
2161
    // target_bitrate and frame_rate.
2162
0
    ratectrl_config.target_bitrate_kbps = (int)(oxcf->target_bandwidth / 1000);
2163
0
    ratectrl_config.frame_rate_num = oxcf->g_timebase.den;
2164
0
    ratectrl_config.frame_rate_den = oxcf->g_timebase.num;
2165
0
    ratectrl_config.overshoot_percent = oxcf->over_shoot_pct;
2166
0
    ratectrl_config.undershoot_percent = oxcf->under_shoot_pct;
2167
0
    ratectrl_config.min_base_q_index = oxcf->best_allowed_q;
2168
0
    ratectrl_config.max_base_q_index = oxcf->worst_allowed_q;
2169
0
    ratectrl_config.base_qp = oxcf->cq_level;
2170
2171
0
    if (oxcf->rc_mode == VPX_VBR) {
2172
0
      ratectrl_config.rc_mode = VPX_RC_VBR;
2173
0
    } else if (oxcf->rc_mode == VPX_Q) {
2174
0
      ratectrl_config.rc_mode = VPX_RC_QMODE;
2175
0
    } else if (oxcf->rc_mode == VPX_CQ) {
2176
0
      ratectrl_config.rc_mode = VPX_RC_CQ;
2177
0
    }
2178
2179
0
    codec_status = vp9_extrc_create(funcs, ratectrl_config, ext_ratectrl);
2180
0
    if (codec_status != VPX_CODEC_OK) {
2181
0
      return codec_status;
2182
0
    }
2183
0
  }
2184
0
  return VPX_CODEC_OK;
2185
0
}
2186
2187
static vpx_codec_err_t ctrl_set_quantizer_one_pass(vpx_codec_alg_priv_t *ctx,
2188
0
                                                   va_list args) {
2189
0
  VP9_COMP *const cpi = ctx->cpi;
2190
0
  const int qp = va_arg(args, int);
2191
0
  vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
2192
0
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2193
0
  vpx_codec_err_t res;
2194
2195
0
  if (cfg->g_pass != VPX_RC_ONE_PASS) return VPX_CODEC_INVALID_PARAM;
2196
0
  if (qp < 0 || qp > 63) return VPX_CODEC_INVALID_PARAM;
2197
2198
0
  cfg->rc_min_quantizer = cfg->rc_max_quantizer = qp;
2199
0
  extra_cfg.aq_mode = 0;
2200
0
  cpi->fixed_qp_onepass = 1;
2201
2202
0
  res = update_extra_cfg(ctx, &extra_cfg);
2203
0
  return res;
2204
0
}
2205
2206
static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
2207
  { VP8_COPY_REFERENCE, ctrl_copy_reference },
2208
2209
  // Setters
2210
  { VP8_SET_REFERENCE, ctrl_set_reference },
2211
  { VP8_SET_POSTPROC, ctrl_set_previewpp },
2212
  { VP9E_SET_ROI_MAP, ctrl_set_roi_map },
2213
  { VP8E_SET_ACTIVEMAP, ctrl_set_active_map },
2214
  { VP8E_SET_SCALEMODE, ctrl_set_scale_mode },
2215
  { VP8E_SET_CPUUSED, ctrl_set_cpuused },
2216
  { VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
2217
  { VP8E_SET_SHARPNESS, ctrl_set_sharpness },
2218
  { VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
2219
  { VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
2220
  { VP9E_SET_TILE_ROWS, ctrl_set_tile_rows },
2221
  { VP9E_SET_TPL, ctrl_set_tpl_model },
2222
  { VP9E_SET_KEY_FRAME_FILTERING, ctrl_set_keyframe_filtering },
2223
  { VP9E_SET_VALIDATE_HBD_INPUT, ctrl_set_validate_hbd_input },
2224
  { VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
2225
  { VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
2226
  { VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type },
2227
  { VP8E_SET_TUNING, ctrl_set_tuning },
2228
  { VP8E_SET_CQ_LEVEL, ctrl_set_cq_level },
2229
  { VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
2230
  { VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
2231
  { VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
2232
  { VP9E_SET_LOSSLESS, ctrl_set_lossless },
2233
  { VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
2234
  { VP9E_SET_AQ_MODE, ctrl_set_aq_mode },
2235
  { VP9E_SET_ALT_REF_AQ, ctrl_set_alt_ref_aq },
2236
  { VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
2237
  { VP9E_SET_SVC, ctrl_set_svc },
2238
  { VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters },
2239
  { VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback },
2240
  { VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id },
2241
  { VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content },
2242
  { VP9E_SET_COLOR_SPACE, ctrl_set_color_space },
2243
  { VP9E_SET_COLOR_RANGE, ctrl_set_color_range },
2244
  { VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
2245
  { VP9E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
2246
  { VP9E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
2247
  { VP9E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
2248
  { VP9E_SET_RENDER_SIZE, ctrl_set_render_size },
2249
  { VP9E_SET_TARGET_LEVEL, ctrl_set_target_level },
2250
  { VP9E_SET_ROW_MT, ctrl_set_row_mt },
2251
  { VP9E_SET_POSTENCODE_DROP, ctrl_set_postencode_drop },
2252
  { VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR, ctrl_set_disable_overshoot_maxq_cbr },
2253
  { VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
2254
  { VP9E_SET_SVC_INTER_LAYER_PRED, ctrl_set_svc_inter_layer_pred },
2255
  { VP9E_SET_SVC_FRAME_DROP_LAYER, ctrl_set_svc_frame_drop_layer },
2256
  { VP9E_SET_SVC_GF_TEMPORAL_REF, ctrl_set_svc_gf_temporal_ref },
2257
  { VP9E_SET_SVC_SPATIAL_LAYER_SYNC, ctrl_set_svc_spatial_layer_sync },
2258
  { VP9E_SET_DELTA_Q_UV, ctrl_set_delta_q_uv },
2259
  { VP9E_SET_DISABLE_LOOPFILTER, ctrl_set_disable_loopfilter },
2260
  { VP9E_SET_RTC_EXTERNAL_RATECTRL, ctrl_set_rtc_external_ratectrl },
2261
  { VP9E_SET_EXTERNAL_RATE_CONTROL, ctrl_set_external_rate_control },
2262
  { VP9E_SET_QUANTIZER_ONE_PASS, ctrl_set_quantizer_one_pass },
2263
2264
  // Getters
2265
  { VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer },
2266
  { VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
2267
  { VP9E_GET_LAST_QUANTIZER_SVC_LAYERS, ctrl_get_quantizer_svc_layers },
2268
  { VP9E_GET_LOOPFILTER_LEVEL, ctrl_get_loopfilter_level },
2269
  { VP9_GET_REFERENCE, ctrl_get_reference },
2270
  { VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id },
2271
  { VP9E_GET_ACTIVEMAP, ctrl_get_active_map },
2272
  { VP9E_GET_LEVEL, ctrl_get_level },
2273
  { VP9E_GET_SVC_REF_FRAME_CONFIG, ctrl_get_svc_ref_frame_config },
2274
2275
  { -1, NULL },
2276
};
2277
2278
static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
2279
  { 0,
2280
    {
2281
        // NOLINT
2282
        0,  // g_usage (unused)
2283
        8,  // g_threads
2284
        0,  // g_profile
2285
2286
        320,         // g_width
2287
        240,         // g_height
2288
        VPX_BITS_8,  // g_bit_depth
2289
        8,           // g_input_bit_depth
2290
2291
        { 1, 30 },  // g_timebase
2292
2293
        0,  // g_error_resilient
2294
2295
        VPX_RC_ONE_PASS,  // g_pass
2296
2297
        25,  // g_lag_in_frames
2298
2299
        0,   // rc_dropframe_thresh
2300
        0,   // rc_resize_allowed
2301
        0,   // rc_scaled_width
2302
        0,   // rc_scaled_height
2303
        60,  // rc_resize_down_thresh
2304
        30,  // rc_resize_up_thresh
2305
2306
        VPX_VBR,      // rc_end_usage
2307
        { NULL, 0 },  // rc_twopass_stats_in
2308
        { NULL, 0 },  // rc_firstpass_mb_stats_in
2309
        256,          // rc_target_bitrate
2310
        0,            // rc_min_quantizer
2311
        63,           // rc_max_quantizer
2312
        25,           // rc_undershoot_pct
2313
        25,           // rc_overshoot_pct
2314
2315
        6000,  // rc_max_buffer_size
2316
        4000,  // rc_buffer_initial_size
2317
        5000,  // rc_buffer_optimal_size
2318
2319
        50,    // rc_two_pass_vbrbias
2320
        0,     // rc_two_pass_vbrmin_section
2321
        2000,  // rc_two_pass_vbrmax_section
2322
        0,     // rc_2pass_vbr_corpus_complexity (non 0 for corpus vbr)
2323
2324
        // keyframing settings (kf)
2325
        VPX_KF_AUTO,  // g_kfmode
2326
        0,            // kf_min_dist
2327
        128,          // kf_max_dist
2328
2329
        VPX_SS_DEFAULT_LAYERS,  // ss_number_layers
2330
        { 0 },
2331
        { 0 },     // ss_target_bitrate
2332
        1,         // ts_number_layers
2333
        { 0 },     // ts_target_bitrate
2334
        { 0 },     // ts_rate_decimator
2335
        0,         // ts_periodicity
2336
        { 0 },     // ts_layer_id
2337
        { 0 },     // layer_target_bitrate
2338
        0,         // temporal_layering_mode
2339
        0,         // use_vizier_rc_params
2340
        { 1, 1 },  // active_wq_factor
2341
        { 1, 1 },  // err_per_mb_factor
2342
        { 1, 1 },  // sr_default_decay_limit
2343
        { 1, 1 },  // sr_diff_factor
2344
        { 1, 1 },  // kf_err_per_mb_factor
2345
        { 1, 1 },  // kf_frame_min_boost_factor
2346
        { 1, 1 },  // kf_frame_max_boost_first_factor
2347
        { 1, 1 },  // kf_frame_max_boost_subs_factor
2348
        { 1, 1 },  // kf_max_total_boost_factor
2349
        { 1, 1 },  // gf_max_total_boost_factor
2350
        { 1, 1 },  // gf_frame_max_boost_factor
2351
        { 1, 1 },  // zm_factor
2352
        { 1, 1 },  // rd_mult_inter_qp_fac
2353
        { 1, 1 },  // rd_mult_arf_qp_fac
2354
        { 1, 1 },  // rd_mult_key_qp_fac
2355
    } },
2356
};
2357
2358
#ifndef VERSION_STRING
2359
#define VERSION_STRING
2360
#endif
2361
CODEC_INTERFACE(vpx_codec_vp9_cx) = {
2362
  "WebM Project VP9 Encoder" VERSION_STRING,
2363
  VPX_CODEC_INTERNAL_ABI_VERSION,
2364
#if CONFIG_VP9_HIGHBITDEPTH
2365
  VPX_CODEC_CAP_HIGHBITDEPTH |
2366
#endif
2367
      VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,  // vpx_codec_caps_t
2368
  encoder_init,                                    // vpx_codec_init_fn_t
2369
  encoder_destroy,                                 // vpx_codec_destroy_fn_t
2370
  encoder_ctrl_maps,                               // vpx_codec_ctrl_fn_map_t
2371
  {
2372
      // NOLINT
2373
      NULL,  // vpx_codec_peek_si_fn_t
2374
      NULL,  // vpx_codec_get_si_fn_t
2375
      NULL,  // vpx_codec_decode_fn_t
2376
      NULL,  // vpx_codec_frame_get_fn_t
2377
      NULL   // vpx_codec_set_fb_fn_t
2378
  },
2379
  {
2380
      // NOLINT
2381
      1,                           // 1 cfg map
2382
      encoder_usage_cfg_map,       // vpx_codec_enc_cfg_map_t
2383
      encoder_encode,              // vpx_codec_encode_fn_t
2384
      encoder_get_cxdata,          // vpx_codec_get_cx_data_fn_t
2385
      encoder_set_config,          // vpx_codec_enc_config_set_fn_t
2386
      encoder_get_global_headers,  // vpx_codec_get_global_headers_fn_t
2387
      encoder_get_preview,         // vpx_codec_get_preview_frame_fn_t
2388
      NULL,                        // vpx_codec_enc_mr_get_mem_loc_fn_t
2389
      NULL                         // vpx_codec_enc_mr_free_mem_loc_fn_t
2390
  }
2391
};
2392
2393
static vpx_codec_enc_cfg_t get_enc_cfg(int frame_width, int frame_height,
2394
                                       vpx_rational_t frame_rate,
2395
                                       int target_bitrate,
2396
0
                                       vpx_enc_pass enc_pass) {
2397
0
  vpx_codec_enc_cfg_t enc_cfg = encoder_usage_cfg_map[0].cfg;
2398
0
  enc_cfg.g_w = frame_width;
2399
0
  enc_cfg.g_h = frame_height;
2400
0
  enc_cfg.rc_target_bitrate = target_bitrate;
2401
0
  enc_cfg.g_pass = enc_pass;
2402
  // g_timebase is the inverse of frame_rate
2403
0
  enc_cfg.g_timebase.num = frame_rate.den;
2404
0
  enc_cfg.g_timebase.den = frame_rate.num;
2405
0
  return enc_cfg;
2406
0
}
2407
2408
0
static vp9_extracfg get_extra_cfg(void) {
2409
0
  vp9_extracfg extra_cfg = default_extra_cfg;
2410
0
  return extra_cfg;
2411
0
}
2412
2413
VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
2414
                                        vpx_rational_t frame_rate,
2415
                                        int target_bitrate, int encode_speed,
2416
                                        int target_level,
2417
0
                                        vpx_enc_pass enc_pass) {
2418
  /* This function will generate the same VP9EncoderConfig used by the
2419
   * vpxenc command given below.
2420
   * The configs in the vpxenc command corresponds to parameters of
2421
   * vp9_get_encoder_config() as follows.
2422
   *
2423
   * WIDTH:   frame_width
2424
   * HEIGHT:  frame_height
2425
   * FPS:     frame_rate
2426
   * BITRATE: target_bitrate
2427
   * CPU_USED:encode_speed
2428
   * TARGET_LEVEL: target_level
2429
   *
2430
   * INPUT, OUTPUT, LIMIT will not affect VP9EncoderConfig
2431
   *
2432
   * vpxenc command:
2433
   * INPUT=bus_cif.y4m
2434
   * OUTPUT=output.webm
2435
   * WIDTH=352
2436
   * HEIGHT=288
2437
   * BITRATE=600
2438
   * FPS=30/1
2439
   * LIMIT=150
2440
   * CPU_USED=0
2441
   * TARGET_LEVEL=0
2442
   * ./vpxenc --limit=$LIMIT --width=$WIDTH --height=$HEIGHT --fps=$FPS
2443
   * --lag-in-frames=25 \
2444
   *  --codec=vp9 --good --cpu-used=CPU_USED --threads=0 --profile=0 \
2445
   *  --min-q=0 --max-q=63 --auto-alt-ref=1 --passes=2 --kf-max-dist=150 \
2446
   *  --kf-min-dist=0 --drop-frame=0 --static-thresh=0 --bias-pct=50 \
2447
   *  --minsection-pct=0 --maxsection-pct=150 --arnr-maxframes=7 --psnr \
2448
   *  --arnr-strength=5 --sharpness=0 --undershoot-pct=100 --overshoot-pct=100 \
2449
   *  --frame-parallel=0 --tile-columns=0 --cpu-used=0 --end-usage=vbr \
2450
   *  --target-bitrate=$BITRATE --target-level=0 -o $OUTPUT $INPUT
2451
   */
2452
2453
0
  VP9EncoderConfig oxcf;
2454
0
  vp9_extracfg extra_cfg = get_extra_cfg();
2455
0
  vpx_codec_enc_cfg_t enc_cfg = get_enc_cfg(
2456
0
      frame_width, frame_height, frame_rate, target_bitrate, enc_pass);
2457
0
  set_encoder_config(&oxcf, &enc_cfg, &extra_cfg);
2458
2459
  // These settings are made to match the settings of the vpxenc command.
2460
0
  oxcf.key_freq = 150;
2461
0
  oxcf.under_shoot_pct = 100;
2462
0
  oxcf.over_shoot_pct = 100;
2463
0
  oxcf.max_threads = 0;
2464
0
  oxcf.tile_columns = 0;
2465
0
  oxcf.frame_parallel_decoding_mode = 0;
2466
0
  oxcf.two_pass_vbrmax_section = 150;
2467
0
  oxcf.speed = abs(encode_speed);
2468
0
  oxcf.target_level = target_level;
2469
0
  return oxcf;
2470
0
}
2471
2472
#define DUMP_STRUCT_VALUE(fp, structure, value) \
2473
0
  fprintf(fp, #value " %" PRId64 "\n", (int64_t)(structure)->value)
2474
2475
0
void vp9_dump_encoder_config(const VP9EncoderConfig *oxcf, FILE *fp) {
2476
0
  DUMP_STRUCT_VALUE(fp, oxcf, profile);
2477
0
  DUMP_STRUCT_VALUE(fp, oxcf, bit_depth);
2478
0
  DUMP_STRUCT_VALUE(fp, oxcf, width);
2479
0
  DUMP_STRUCT_VALUE(fp, oxcf, height);
2480
0
  DUMP_STRUCT_VALUE(fp, oxcf, input_bit_depth);
2481
0
  DUMP_STRUCT_VALUE(fp, oxcf, init_framerate);
2482
  // TODO(angiebird): dump g_timebase
2483
  // TODO(angiebird): dump g_timebase_in_ts
2484
2485
0
  DUMP_STRUCT_VALUE(fp, oxcf, target_bandwidth);
2486
2487
0
  DUMP_STRUCT_VALUE(fp, oxcf, noise_sensitivity);
2488
0
  DUMP_STRUCT_VALUE(fp, oxcf, sharpness);
2489
0
  DUMP_STRUCT_VALUE(fp, oxcf, speed);
2490
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_max_intra_bitrate_pct);
2491
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_max_inter_bitrate_pct);
2492
0
  DUMP_STRUCT_VALUE(fp, oxcf, gf_cbr_boost_pct);
2493
2494
0
  DUMP_STRUCT_VALUE(fp, oxcf, mode);
2495
0
  DUMP_STRUCT_VALUE(fp, oxcf, pass);
2496
2497
  // Key Framing Operations
2498
0
  DUMP_STRUCT_VALUE(fp, oxcf, auto_key);
2499
0
  DUMP_STRUCT_VALUE(fp, oxcf, key_freq);
2500
2501
0
  DUMP_STRUCT_VALUE(fp, oxcf, lag_in_frames);
2502
2503
  // ----------------------------------------------------------------
2504
  // DATARATE CONTROL OPTIONS
2505
2506
  // vbr, cbr, constrained quality or constant quality
2507
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_mode);
2508
2509
  // buffer targeting aggressiveness
2510
0
  DUMP_STRUCT_VALUE(fp, oxcf, under_shoot_pct);
2511
0
  DUMP_STRUCT_VALUE(fp, oxcf, over_shoot_pct);
2512
2513
  // buffering parameters
2514
  // TODO(angiebird): dump tarting_buffer_level_ms
2515
  // TODO(angiebird): dump ptimal_buffer_level_ms
2516
  // TODO(angiebird): dump maximum_buffer_size_ms
2517
2518
  // Frame drop threshold.
2519
0
  DUMP_STRUCT_VALUE(fp, oxcf, drop_frames_water_mark);
2520
2521
  // controlling quality
2522
0
  DUMP_STRUCT_VALUE(fp, oxcf, fixed_q);
2523
0
  DUMP_STRUCT_VALUE(fp, oxcf, worst_allowed_q);
2524
0
  DUMP_STRUCT_VALUE(fp, oxcf, best_allowed_q);
2525
0
  DUMP_STRUCT_VALUE(fp, oxcf, cq_level);
2526
0
  DUMP_STRUCT_VALUE(fp, oxcf, aq_mode);
2527
2528
  // Special handling of Adaptive Quantization for AltRef frames
2529
0
  DUMP_STRUCT_VALUE(fp, oxcf, alt_ref_aq);
2530
2531
  // Internal frame size scaling.
2532
0
  DUMP_STRUCT_VALUE(fp, oxcf, resize_mode);
2533
0
  DUMP_STRUCT_VALUE(fp, oxcf, scaled_frame_width);
2534
0
  DUMP_STRUCT_VALUE(fp, oxcf, scaled_frame_height);
2535
2536
  // Enable feature to reduce the frame quantization every x frames.
2537
0
  DUMP_STRUCT_VALUE(fp, oxcf, frame_periodic_boost);
2538
2539
  // two pass datarate control
2540
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrbias);
2541
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrmin_section);
2542
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrmax_section);
2543
0
  DUMP_STRUCT_VALUE(fp, oxcf, vbr_corpus_complexity);
2544
  // END DATARATE CONTROL OPTIONS
2545
  // ----------------------------------------------------------------
2546
2547
  // Spatial and temporal scalability.
2548
0
  DUMP_STRUCT_VALUE(fp, oxcf, ss_number_layers);
2549
0
  DUMP_STRUCT_VALUE(fp, oxcf, ts_number_layers);
2550
2551
  // Bitrate allocation for spatial layers.
2552
  // TODO(angiebird): dump layer_target_bitrate[VPX_MAX_LAYERS]
2553
  // TODO(angiebird): dump ss_target_bitrate[VPX_SS_MAX_LAYERS]
2554
  // TODO(angiebird): dump ss_enable_auto_arf[VPX_SS_MAX_LAYERS]
2555
  // TODO(angiebird): dump ts_rate_decimator[VPX_TS_MAX_LAYERS]
2556
2557
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_auto_arf);
2558
0
  DUMP_STRUCT_VALUE(fp, oxcf, encode_breakout);
2559
0
  DUMP_STRUCT_VALUE(fp, oxcf, error_resilient_mode);
2560
0
  DUMP_STRUCT_VALUE(fp, oxcf, frame_parallel_decoding_mode);
2561
2562
0
  DUMP_STRUCT_VALUE(fp, oxcf, arnr_max_frames);
2563
0
  DUMP_STRUCT_VALUE(fp, oxcf, arnr_strength);
2564
2565
0
  DUMP_STRUCT_VALUE(fp, oxcf, min_gf_interval);
2566
0
  DUMP_STRUCT_VALUE(fp, oxcf, max_gf_interval);
2567
2568
0
  DUMP_STRUCT_VALUE(fp, oxcf, tile_columns);
2569
0
  DUMP_STRUCT_VALUE(fp, oxcf, tile_rows);
2570
2571
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_tpl_model);
2572
2573
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_keyframe_filtering);
2574
2575
0
  DUMP_STRUCT_VALUE(fp, oxcf, max_threads);
2576
2577
0
  DUMP_STRUCT_VALUE(fp, oxcf, target_level);
2578
2579
  // TODO(angiebird): dump two_pass_stats_in
2580
0
  DUMP_STRUCT_VALUE(fp, oxcf, tuning);
2581
0
  DUMP_STRUCT_VALUE(fp, oxcf, content);
2582
0
#if CONFIG_VP9_HIGHBITDEPTH
2583
0
  DUMP_STRUCT_VALUE(fp, oxcf, use_highbitdepth);
2584
0
#endif
2585
0
  DUMP_STRUCT_VALUE(fp, oxcf, color_space);
2586
0
  DUMP_STRUCT_VALUE(fp, oxcf, color_range);
2587
0
  DUMP_STRUCT_VALUE(fp, oxcf, render_width);
2588
0
  DUMP_STRUCT_VALUE(fp, oxcf, render_height);
2589
0
  DUMP_STRUCT_VALUE(fp, oxcf, temporal_layering_mode);
2590
2591
0
  DUMP_STRUCT_VALUE(fp, oxcf, row_mt);
2592
0
  DUMP_STRUCT_VALUE(fp, oxcf, motion_vector_unit_test);
2593
0
  DUMP_STRUCT_VALUE(fp, oxcf, delta_q_uv);
2594
0
}
2595
2596
4.79k
FRAME_INFO vp9_get_frame_info(const VP9EncoderConfig *oxcf) {
2597
4.79k
  FRAME_INFO frame_info;
2598
4.79k
  int dummy;
2599
4.79k
  frame_info.frame_width = oxcf->width;
2600
4.79k
  frame_info.frame_height = oxcf->height;
2601
4.79k
  frame_info.render_frame_width = oxcf->width;
2602
4.79k
  frame_info.render_frame_height = oxcf->height;
2603
4.79k
  frame_info.bit_depth = oxcf->bit_depth;
2604
4.79k
  vp9_set_mi_size(&frame_info.mi_rows, &frame_info.mi_cols, &dummy,
2605
4.79k
                  frame_info.frame_width, frame_info.frame_height);
2606
4.79k
  vp9_set_mb_size(&frame_info.mb_rows, &frame_info.mb_cols, &frame_info.num_mbs,
2607
4.79k
                  frame_info.mi_rows, frame_info.mi_cols);
2608
  // TODO(angiebird): Figure out how to get subsampling_x/y here
2609
4.79k
  return frame_info;
2610
4.79k
}
2611
2612
void vp9_set_first_pass_stats(VP9EncoderConfig *oxcf,
2613
38.2k
                              const vpx_fixed_buf_t *stats) {
2614
38.2k
  oxcf->two_pass_stats_in = *stats;
2615
38.2k
}