Coverage Report

Created: 2026-07-25 07:52

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