Coverage Report

Created: 2024-09-06 07:53

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