Coverage Report

Created: 2025-08-28 07:12

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