Coverage Report

Created: 2026-04-01 07:42

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