Coverage Report

Created: 2026-02-14 06:59

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