Coverage Report

Created: 2026-05-23 07:06

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
130
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
156
130
  const vpx_codec_err_t res = error->error_code;
157
158
130
  if (res != VPX_CODEC_OK)
159
130
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;
160
161
130
  return res;
162
130
}
163
164
#undef ERROR
165
#define ERROR(str)                  \
166
45
  do {                              \
167
45
    ctx->base.err_detail = str;     \
168
45
    return VPX_CODEC_INVALID_PARAM; \
169
45
  } while (0)
170
171
#define RANGE_CHECK(p, memb, lo, hi)                                     \
172
1.31M
  do {                                                                   \
173
1.31M
    if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
174
1.31M
      ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
175
1.31M
  } while (0)
176
177
#define RANGE_CHECK_HI(p, memb, hi)                                     \
178
428k
  do {                                                                  \
179
428k
    if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
180
428k
  } 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
91.8k
  do {                                                                \
189
91.8k
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
190
91.8k
  } 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
30.6k
                                       const struct vp9_extracfg *extra_cfg) {
195
30.6k
  RANGE_CHECK(cfg, g_w, 1, 65536);  // 16 bits available
196
30.6k
  RANGE_CHECK(cfg, g_h, 1, 65536);  // 16 bits available
197
30.6k
  RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
198
30.6k
  RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
199
30.6k
  RANGE_CHECK_HI(cfg, g_profile, 3);
200
201
30.6k
  RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
202
30.6k
  RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
203
30.6k
  RANGE_CHECK_BOOL(extra_cfg, lossless);
204
30.6k
  RANGE_CHECK_BOOL(extra_cfg, frame_parallel_decoding_mode);
205
30.6k
  RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 2);
206
30.6k
  RANGE_CHECK(extra_cfg, alt_ref_aq, 0, 1);
207
30.6k
  RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
208
30.6k
  RANGE_CHECK_HI(cfg, g_threads, MAX_NUM_THREADS);
209
30.6k
  RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
210
30.6k
  RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
211
30.6k
  RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
212
30.6k
  RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
213
30.6k
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
214
30.6k
  RANGE_CHECK(cfg, rc_2pass_vbr_corpus_complexity, 0, 10000);
215
30.6k
  RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
216
30.6k
  RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
217
30.6k
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
218
30.6k
  RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
219
30.6k
  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
30.6k
  RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
224
30.6k
#endif
225
30.6k
  RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
226
30.6k
  RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
227
30.6k
  if (extra_cfg->max_gf_interval > 0) {
228
0
    RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
229
0
  }
230
30.6k
  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
30.6k
  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
30.6k
  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
30.6k
  RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
248
30.6k
  RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
249
250
30.6k
  {
251
30.6k
    unsigned int level = extra_cfg->target_level;
252
30.6k
    if (level != LEVEL_1 && level != LEVEL_1_1 && level != LEVEL_2 &&
253
30.6k
        level != LEVEL_2_1 && level != LEVEL_3 && level != LEVEL_3_1 &&
254
30.6k
        level != LEVEL_4 && level != LEVEL_4_1 && level != LEVEL_5 &&
255
30.6k
        level != LEVEL_5_1 && level != LEVEL_5_2 && level != LEVEL_6 &&
256
30.6k
        level != LEVEL_6_1 && level != LEVEL_6_2 && level != LEVEL_UNKNOWN &&
257
30.6k
        level != LEVEL_AUTO && level != LEVEL_MAX)
258
0
      ERROR("target_level is invalid");
259
30.6k
  }
260
261
30.6k
  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
30.6k
  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
30.6k
  if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
283
25.7k
      cfg->kf_min_dist > 0)
284
0
    ERROR(
285
30.6k
        "kf_min_dist not supported in auto mode, use 0 "
286
30.6k
        "or kf_max_dist instead.");
287
288
30.6k
  RANGE_CHECK(extra_cfg, row_mt, 0, 1);
289
30.6k
  RANGE_CHECK(extra_cfg, motion_vector_unit_test, 0, 2);
290
30.6k
  RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, MAX_ARF_LAYERS);
291
30.6k
  RANGE_CHECK(extra_cfg, cpu_used, -9, 9);
292
30.6k
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
293
30.6k
  RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
294
30.6k
  RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
295
30.6k
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
296
30.6k
  RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
297
30.6k
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
298
30.6k
  RANGE_CHECK(extra_cfg, cq_level, 0, 63);
299
30.6k
  RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
300
30.6k
  RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
301
30.6k
  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
30.6k
  RANGE_CHECK(extra_cfg, content, VP9E_CONTENT_DEFAULT,
304
30.6k
              VP9E_CONTENT_INVALID - 1);
305
306
30.6k
#if !CONFIG_REALTIME_ONLY
307
30.6k
  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
30.6k
#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
30.6k
  if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
371
30.6k
      cfg->g_bit_depth > VPX_BITS_8) {
372
0
    ERROR("Codec high bit-depth not supported in profile < 2");
373
0
  }
374
30.6k
  if (cfg->g_profile > (unsigned int)PROFILE_1 &&
375
0
      cfg->g_bit_depth == VPX_BITS_8) {
376
0
    ERROR("Codec bit-depth 8 not supported in profile > 1");
377
0
  }
378
30.6k
  RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
379
30.6k
  RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
380
381
  // The range below shall be further tuned.
382
30.6k
  RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
383
30.6k
  RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
384
30.6k
  RANGE_CHECK(cfg, err_per_mb_factor.den, 1, 1000);
385
30.6k
  RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
386
30.6k
  RANGE_CHECK(cfg, sr_diff_factor.den, 1, 1000);
387
30.6k
  RANGE_CHECK(cfg, kf_err_per_mb_factor.den, 1, 1000);
388
30.6k
  RANGE_CHECK(cfg, kf_frame_min_boost_factor.den, 1, 1000);
389
30.6k
  RANGE_CHECK(cfg, kf_frame_max_boost_subs_factor.den, 1, 1000);
390
30.6k
  RANGE_CHECK(cfg, kf_max_total_boost_factor.den, 1, 1000);
391
30.6k
  RANGE_CHECK(cfg, gf_max_total_boost_factor.den, 1, 1000);
392
30.6k
  RANGE_CHECK(cfg, gf_frame_max_boost_factor.den, 1, 1000);
393
30.6k
  RANGE_CHECK(cfg, zm_factor.den, 1, 1000);
394
30.6k
  RANGE_CHECK(cfg, rd_mult_inter_qp_fac.den, 1, 1000);
395
30.6k
  RANGE_CHECK(cfg, rd_mult_arf_qp_fac.den, 1, 1000);
396
30.6k
  RANGE_CHECK(cfg, rd_mult_key_qp_fac.den, 1, 1000);
397
398
30.6k
  return VPX_CODEC_OK;
399
30.6k
}
400
401
static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
402
75.9k
                                    const vpx_image_t *img) {
403
75.9k
  switch (img->fmt) {
404
0
    case VPX_IMG_FMT_YV12:
405
75.9k
    case VPX_IMG_FMT_I420:
406
75.9k
    case VPX_IMG_FMT_I42016:
407
75.9k
    case VPX_IMG_FMT_NV12: break;
408
0
    case VPX_IMG_FMT_I422:
409
0
    case VPX_IMG_FMT_I444:
410
0
    case VPX_IMG_FMT_I440:
411
0
      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
0
      break;
417
0
    case VPX_IMG_FMT_I42216:
418
0
    case VPX_IMG_FMT_I44416:
419
0
    case VPX_IMG_FMT_I44016:
420
0
      if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
421
0
          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
0
      break;
427
0
    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
75.9k
  }
433
434
75.9k
  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
75.9k
  assert(img->fmt & VPX_IMG_FMT_PLANAR);
437
75.9k
  if (img->stride[VPX_PLANE_U] != img->stride[VPX_PLANE_V])
438
0
    ERROR("Image U/V strides must match");
439
440
75.9k
#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
75.9k
  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
75.9k
  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
75.9k
  if (ctx->extra_cfg.validate_hbd_input &&
457
75.9k
      (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
458
0
    const unsigned int h = img->d_h;
459
0
    const unsigned int w = img->d_w;
460
0
    const unsigned int bit_depth = ctx->cfg.g_bit_depth;
461
0
    const int max_val = 1 << bit_depth;
462
0
    for (int plane = 0; plane < 3; ++plane) {
463
0
      const unsigned short *src = (const unsigned short *)img->planes[plane];
464
0
      if (!src) return VPX_CODEC_INVALID_PARAM;
465
0
      const unsigned int stride = img->stride[plane] / 2;
466
0
      const unsigned int ph =
467
0
          (plane == 0) ? h : (h + img->y_chroma_shift) >> img->y_chroma_shift;
468
0
      const unsigned int pw =
469
0
          (plane == 0) ? w : (w + img->x_chroma_shift) >> img->x_chroma_shift;
470
0
      for (unsigned int i = 0; i < ph; ++i) {
471
0
        for (unsigned int j = 0; j < pw; ++j) {
472
0
          if (src[j] >= max_val) {
473
0
            ERROR("Input pixel value out of range for encoder bit depth");
474
0
          }
475
0
        }
476
0
        src += stride;
477
0
      }
478
0
    }
479
0
  }
480
75.9k
#endif  // CONFIG_VP9_HIGHBITDEPTH
481
482
75.9k
  return VPX_CODEC_OK;
483
75.9k
}
484
485
75.9k
static int get_image_bps(const vpx_image_t *img) {
486
75.9k
  switch (img->fmt) {
487
0
    case VPX_IMG_FMT_YV12:
488
0
    case VPX_IMG_FMT_NV12:
489
75.9k
    case VPX_IMG_FMT_I420: return 12;
490
0
    case VPX_IMG_FMT_I422: return 16;
491
0
    case VPX_IMG_FMT_I444: return 24;
492
0
    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
75.9k
  }
499
0
  return 0;
500
75.9k
}
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
30.6k
static vpx_rational64_t get_g_timebase_in_ts(vpx_rational_t g_timebase) {
559
30.6k
  vpx_rational64_t g_timebase_in_ts;
560
30.6k
  g_timebase_in_ts.den = g_timebase.den;
561
30.6k
  g_timebase_in_ts.num = g_timebase.num;
562
30.6k
  g_timebase_in_ts.num *= TICKS_PER_SEC;
563
30.6k
  reduce_ratio(&g_timebase_in_ts);
564
30.6k
  return g_timebase_in_ts;
565
30.6k
}
566
567
static vpx_codec_err_t set_encoder_config(
568
    VP9EncoderConfig *oxcf, vpx_codec_enc_cfg_t *cfg,
569
30.6k
    const struct vp9_extracfg *extra_cfg) {
570
30.6k
  int sl, tl;
571
30.6k
  unsigned int raw_target_rate;
572
30.6k
  oxcf->profile = cfg->g_profile;
573
30.6k
  oxcf->max_threads = (int)cfg->g_threads;
574
30.6k
  oxcf->width = cfg->g_w;
575
30.6k
  oxcf->height = cfg->g_h;
576
30.6k
  oxcf->bit_depth = cfg->g_bit_depth;
577
30.6k
  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
30.6k
  oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
582
30.6k
  if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
583
30.6k
  oxcf->g_timebase = cfg->g_timebase;
584
30.6k
  oxcf->g_timebase_in_ts = get_g_timebase_in_ts(oxcf->g_timebase);
585
586
30.6k
  oxcf->mode = GOOD;
587
588
30.6k
  switch (cfg->g_pass) {
589
30.6k
    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
30.6k
  }
593
594
30.6k
  oxcf->lag_in_frames =
595
30.6k
      cfg->g_pass == VPX_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
596
30.6k
  oxcf->rc_mode = cfg->rc_end_usage;
597
598
30.6k
  raw_target_rate =
599
30.6k
      (unsigned int)((int64_t)oxcf->width * oxcf->height * oxcf->bit_depth * 3 *
600
30.6k
                     oxcf->init_framerate / 1000);
601
  // Cap target bitrate to raw rate or 1000Mbps, whichever is less
602
30.6k
  cfg->rc_target_bitrate =
603
30.6k
      VPXMIN(VPXMIN(raw_target_rate, cfg->rc_target_bitrate), 1000000);
604
605
  // Convert target bandwidth from Kbit/s to Bit/s
606
30.6k
  oxcf->target_bandwidth = 1000 * (int64_t)cfg->rc_target_bitrate;
607
30.6k
  oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
608
30.6k
  oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
609
30.6k
  oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
610
611
30.6k
  oxcf->best_allowed_q =
612
30.6k
      extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
613
30.6k
  oxcf->worst_allowed_q =
614
30.6k
      extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
615
30.6k
  oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
616
30.6k
  oxcf->fixed_q = -1;
617
618
30.6k
  oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
619
30.6k
  oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
620
621
30.6k
  oxcf->scaled_frame_width = cfg->rc_scaled_width;
622
30.6k
  oxcf->scaled_frame_height = cfg->rc_scaled_height;
623
30.6k
  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
30.6k
  } else {
629
30.6k
    oxcf->resize_mode = RESIZE_NONE;
630
30.6k
  }
631
632
30.6k
  oxcf->maximum_buffer_size_ms = cfg->rc_buf_sz;
633
30.6k
  oxcf->starting_buffer_level_ms = cfg->rc_buf_initial_sz;
634
30.6k
  oxcf->optimal_buffer_level_ms = cfg->rc_buf_optimal_sz;
635
636
30.6k
  oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
637
638
30.6k
  oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
639
30.6k
  oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
640
30.6k
  oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
641
30.6k
  oxcf->vbr_corpus_complexity = cfg->rc_2pass_vbr_corpus_complexity;
642
643
30.6k
  oxcf->auto_key =
644
30.6k
      cfg->kf_mode == VPX_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
645
646
30.6k
  oxcf->key_freq = cfg->kf_max_dist;
647
648
30.6k
  oxcf->speed = abs(extra_cfg->cpu_used);
649
30.6k
  oxcf->encode_breakout = extra_cfg->static_thresh;
650
30.6k
  oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
651
30.6k
  if (oxcf->bit_depth == VPX_BITS_8) {
652
30.6k
    oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
653
30.6k
  } else {
654
    // Disable denoiser for high bitdepth since vp9_denoiser_filter only works
655
    // for 8 bits.
656
0
    oxcf->noise_sensitivity = 0;
657
0
  }
658
30.6k
  oxcf->sharpness = extra_cfg->sharpness;
659
660
30.6k
  vp9_set_first_pass_stats(oxcf, &cfg->rc_twopass_stats_in);
661
662
30.6k
  oxcf->color_space = extra_cfg->color_space;
663
30.6k
  oxcf->color_range = extra_cfg->color_range;
664
30.6k
  oxcf->render_width = extra_cfg->render_width;
665
30.6k
  oxcf->render_height = extra_cfg->render_height;
666
30.6k
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
667
30.6k
  oxcf->arnr_strength = extra_cfg->arnr_strength;
668
30.6k
  oxcf->min_gf_interval = extra_cfg->min_gf_interval;
669
30.6k
  oxcf->max_gf_interval = extra_cfg->max_gf_interval;
670
671
30.6k
  oxcf->tuning = extra_cfg->tuning;
672
30.6k
  oxcf->content = extra_cfg->content;
673
674
30.6k
  oxcf->tile_columns = extra_cfg->tile_columns;
675
676
30.6k
  oxcf->enable_tpl_model = extra_cfg->enable_tpl_model;
677
678
30.6k
  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
30.6k
  if (oxcf->max_threads > 1 && oxcf->tile_columns > 0)
686
0
    oxcf->tile_rows = 0;
687
30.6k
  else
688
30.6k
    oxcf->tile_rows = extra_cfg->tile_rows;
689
690
30.6k
  oxcf->error_resilient_mode = cfg->g_error_resilient;
691
30.6k
  oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
692
693
30.6k
  oxcf->aq_mode = extra_cfg->aq_mode;
694
30.6k
  oxcf->alt_ref_aq = extra_cfg->alt_ref_aq;
695
696
30.6k
  oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
697
698
30.6k
  oxcf->ss_number_layers = cfg->ss_number_layers;
699
30.6k
  oxcf->ts_number_layers = cfg->ts_number_layers;
700
30.6k
  oxcf->temporal_layering_mode =
701
30.6k
      (enum vp9e_temporal_layering_mode)cfg->temporal_layering_mode;
702
30.6k
  if (oxcf->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
703
30.6k
    if (oxcf->ts_number_layers == 2)
704
0
      oxcf->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
705
30.6k
    else if (oxcf->ts_number_layers == 3)
706
0
      oxcf->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
707
30.6k
  }
708
709
30.6k
  oxcf->target_level = extra_cfg->target_level;
710
711
30.6k
  oxcf->row_mt = extra_cfg->row_mt;
712
30.6k
  oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
713
714
30.6k
  oxcf->delta_q_uv = extra_cfg->delta_q_uv;
715
716
61.2k
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
717
61.2k
    for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
718
30.6k
      const int layer = sl * oxcf->ts_number_layers + tl;
719
30.6k
      if (cfg->layer_target_bitrate[layer] > INT_MAX / 1000)
720
0
        oxcf->layer_target_bitrate[layer] = INT_MAX;
721
30.6k
      else
722
30.6k
        oxcf->layer_target_bitrate[layer] =
723
30.6k
            1000 * cfg->layer_target_bitrate[layer];
724
30.6k
    }
725
30.6k
  }
726
30.6k
  if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
727
0
    oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
728
0
  }
729
30.6k
  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
30.6k
  } else if (oxcf->ts_number_layers == 1) {
735
30.6k
    oxcf->ts_rate_decimator[0] = 1;
736
30.6k
  }
737
738
30.6k
  if (get_level_index(oxcf->target_level) >= 0) config_target_level(oxcf);
739
  // vp9_dump_encoder_config(oxcf, stderr);
740
30.6k
  return VPX_CODEC_OK;
741
30.6k
}
742
743
static vpx_codec_err_t set_twopass_params_from_config(
744
30.6k
    const vpx_codec_enc_cfg_t *const cfg, struct VP9_COMP *cpi) {
745
30.6k
  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
74.7k
                                            va_list args) {
939
74.7k
  int *const arg = va_arg(args, int *);
940
74.7k
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
941
74.7k
  *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
942
74.7k
  return VPX_CODEC_OK;
943
74.7k
}
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
26.7k
                                        const struct vp9_extracfg *extra_cfg) {
966
26.7k
  const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
967
26.7k
  if (res == VPX_CODEC_OK) {
968
26.7k
    ctx->extra_cfg = *extra_cfg;
969
26.7k
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
970
26.7k
    set_twopass_params_from_config(&ctx->cfg, ctx->cpi);
971
26.7k
    vp9_change_config(ctx->cpi, &ctx->oxcf);
972
26.7k
  }
973
26.7k
  return res;
974
26.7k
}
975
976
static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
977
3.80k
                                        va_list args) {
978
3.80k
  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.80k
  extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
981
3.80k
  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.80k
  return update_extra_cfg(ctx, &extra_cfg);
987
3.80k
}
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.80k
                                              va_list args) {
1012
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1013
3.80k
  extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
1014
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
1015
3.80k
}
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.80k
                                                va_list args) {
1054
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1055
3.80k
  extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
1056
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
1057
3.80k
}
1058
1059
static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
1060
3.80k
                                              va_list args) {
1061
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1062
3.80k
  extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
1063
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
1064
3.80k
}
1065
1066
static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
1067
3.80k
                                          va_list args) {
1068
3.80k
  (void)ctx;
1069
3.80k
  (void)args;
1070
3.80k
  return VPX_CODEC_OK;
1071
3.80k
}
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
164
                                         va_list args) {
1082
164
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1083
164
  extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
1084
164
  return update_extra_cfg(ctx, &extra_cfg);
1085
164
}
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.80k
                                             va_list args) {
1163
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1164
3.80k
  extra_cfg.target_level = CAST(VP9E_SET_TARGET_LEVEL, args);
1165
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
1166
3.80k
}
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.84k
                                    vpx_codec_priv_enc_mr_cfg_t *data) {
1205
3.84k
  vpx_codec_err_t res = VPX_CODEC_OK;
1206
3.84k
  (void)data;
1207
1208
3.84k
  if (ctx->priv == NULL) {
1209
3.84k
    vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
1210
3.84k
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
1211
1212
3.84k
    ctx->priv = (vpx_codec_priv_t *)priv;
1213
3.84k
    ctx->priv->init_flags = ctx->init_flags;
1214
3.84k
    ctx->priv->enc.total_encoders = 1;
1215
3.84k
    priv->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
1216
3.84k
    if (priv->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
1217
1218
3.84k
    if (ctx->config.enc) {
1219
      // Update the reference to the config structure to an internal copy.
1220
3.84k
      priv->cfg = *ctx->config.enc;
1221
3.84k
      ctx->config.enc = &priv->cfg;
1222
3.84k
    }
1223
1224
3.84k
    priv->extra_cfg = default_extra_cfg;
1225
3.84k
    vp9_initialize_enc();
1226
1227
3.84k
    res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
1228
1229
3.84k
    if (res == VPX_CODEC_OK) {
1230
3.80k
      priv->pts_offset_initialized = 0;
1231
3.80k
      priv->global_header_subsampling = -1;
1232
3.80k
      set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
1233
3.80k
#if CONFIG_VP9_HIGHBITDEPTH
1234
3.80k
      priv->oxcf.use_highbitdepth =
1235
3.80k
          (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
1236
3.80k
#endif
1237
3.80k
      priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
1238
3.80k
      if (priv->cpi == NULL) res = VPX_CODEC_MEM_ERROR;
1239
3.80k
      set_twopass_params_from_config(&priv->cfg, priv->cpi);
1240
3.80k
    }
1241
3.84k
  }
1242
1243
3.84k
  return res;
1244
3.84k
}
1245
1246
3.84k
static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
1247
3.84k
  free(ctx->cx_data);
1248
3.84k
  free(ctx->global_headers.buf);
1249
3.84k
  vp9_remove_compressor(ctx->cpi);
1250
3.84k
  vpx_free(ctx->buffer_pool);
1251
3.84k
  vpx_free(ctx);
1252
3.84k
  return VPX_CODEC_OK;
1253
3.84k
}
1254
1255
static vpx_codec_err_t pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
1256
                                               unsigned long duration,
1257
108k
                                               vpx_enc_deadline_t deadline) {
1258
108k
  MODE new_mode = BEST;
1259
1260
#if CONFIG_REALTIME_ONLY
1261
  (void)duration;
1262
  deadline = VPX_DL_REALTIME;
1263
#else
1264
108k
  switch (ctx->cfg.g_pass) {
1265
108k
    case VPX_RC_ONE_PASS:
1266
108k
      if (deadline > 0) {
1267
        // Convert duration parameter from stream timebase to microseconds.
1268
108k
        VPX_STATIC_ASSERT(TICKS_PER_SEC > 1000000 &&
1269
108k
                          (TICKS_PER_SEC % 1000000) == 0);
1270
1271
108k
        if (duration > UINT64_MAX / (uint64_t)ctx->oxcf.g_timebase_in_ts.num) {
1272
6
          ERROR("duration is too big");
1273
6
        }
1274
108k
        uint64_t duration_us = duration *
1275
108k
                               (uint64_t)ctx->oxcf.g_timebase_in_ts.num /
1276
108k
                               ((uint64_t)ctx->oxcf.g_timebase_in_ts.den *
1277
108k
                                (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
108k
        new_mode = (deadline > duration_us) ? GOOD : REALTIME;
1282
108k
      } else {
1283
0
        new_mode = BEST;
1284
0
      }
1285
108k
      break;
1286
108k
    case VPX_RC_FIRST_PASS: break;
1287
0
    case VPX_RC_LAST_PASS: new_mode = deadline > 0 ? GOOD : BEST; break;
1288
108k
  }
1289
108k
#endif  // CONFIG_REALTIME_ONLY
1290
1291
108k
  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
108k
  if (ctx->oxcf.mode != new_mode) {
1298
2.23k
    ctx->oxcf.mode = new_mode;
1299
2.23k
    vp9_change_config(ctx->cpi, &ctx->oxcf);
1300
2.23k
  }
1301
108k
  return VPX_CODEC_OK;
1302
108k
}
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
75.1k
                                                   unsigned int lib_flags) {
1370
75.1k
  vpx_codec_frame_flags_t flags = lib_flags << 16;
1371
1372
75.1k
  if (lib_flags & FRAMEFLAGS_KEY ||
1373
59.9k
      (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
15.2k
    flags |= VPX_FRAME_IS_KEY;
1379
1380
75.1k
  if (!cpi->common.show_frame) {
1381
0
    flags |= VPX_FRAME_IS_INVISIBLE;
1382
0
  }
1383
1384
75.1k
  if (cpi->droppable) flags |= VPX_FRAME_IS_DROPPABLE;
1385
1386
75.1k
  return flags;
1387
75.1k
}
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
108k
                                      vpx_enc_deadline_t deadline) {
1417
108k
  volatile vpx_codec_err_t res;
1418
108k
  volatile vpx_enc_frame_flags_t flags = enc_flags;
1419
108k
  volatile vpx_codec_pts_t pts = pts_val;
1420
108k
  VP9_COMP *const cpi = ctx->cpi;
1421
108k
  const vpx_rational64_t *const timebase_in_ts = &ctx->oxcf.g_timebase_in_ts;
1422
108k
  size_t data_sz;
1423
108k
  vpx_codec_cx_pkt_t pkt;
1424
108k
  memset(&pkt, 0, sizeof(pkt));
1425
1426
108k
  if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
1427
1428
108k
  cpi->last_coded_width = ctx->oxcf.width;
1429
108k
  cpi->last_coded_height = ctx->oxcf.height;
1430
1431
108k
  if (img != NULL) {
1432
75.9k
    res = validate_img(ctx, img);
1433
75.9k
    if (res != VPX_CODEC_OK) {
1434
0
      return res;
1435
0
    }
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
75.9k
    data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
1439
75.9k
              (cpi->multi_layer_arf ? 8 : 2);
1440
75.9k
    if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
1441
75.9k
    if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1442
3.72k
      ctx->cx_data_sz = data_sz;
1443
3.72k
      free(ctx->cx_data);
1444
3.72k
      ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1445
3.72k
      if (ctx->cx_data == NULL) {
1446
0
        return VPX_CODEC_MEM_ERROR;
1447
0
      }
1448
3.72k
    }
1449
1450
75.9k
    int chroma_subsampling = -1;
1451
75.9k
    if ((img->fmt & VPX_IMG_FMT_I420) == VPX_IMG_FMT_I420 ||
1452
0
        (img->fmt & VPX_IMG_FMT_NV12) == VPX_IMG_FMT_NV12 ||
1453
75.9k
        (img->fmt & VPX_IMG_FMT_YV12) == VPX_IMG_FMT_YV12) {
1454
75.9k
      chroma_subsampling = 1;  // matches default for Codec Parameter String
1455
75.9k
    } else if ((img->fmt & VPX_IMG_FMT_I422) == VPX_IMG_FMT_I422) {
1456
0
      chroma_subsampling = 2;
1457
0
    } else if ((img->fmt & VPX_IMG_FMT_I444) == VPX_IMG_FMT_I444) {
1458
0
      chroma_subsampling = 3;
1459
0
    }
1460
75.9k
    if (chroma_subsampling > ctx->global_header_subsampling) {
1461
3.72k
      ctx->global_header_subsampling = chroma_subsampling;
1462
3.72k
    }
1463
75.9k
  }
1464
1465
108k
  res = pick_quickcompress_mode(ctx, duration, deadline);
1466
108k
  if (res != VPX_CODEC_OK) {
1467
6
    return res;
1468
6
  }
1469
108k
  vpx_codec_pkt_list_init(&ctx->pkt_list);
1470
1471
  // Handle Flags
1472
108k
  if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
1473
108k
      ((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
108k
  if (setjmp(cpi->common.error.jmp)) {
1479
130
    cpi->common.error.setjmp = 0;
1480
130
    res = update_error_state(ctx, &cpi->common.error);
1481
130
    vpx_clear_system_state();
1482
130
    return res;
1483
130
  }
1484
108k
  cpi->common.error.setjmp = 1;
1485
1486
108k
  vp9_apply_encoding_flags(cpi, flags);
1487
1488
  // Handle fixed keyframe intervals
1489
108k
  if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
1490
108k
      ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1491
11.8k
    if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1492
11.8k
      flags |= VPX_EFLAG_FORCE_KF;
1493
11.8k
      ctx->fixed_kf_cntr = 1;
1494
11.8k
    }
1495
11.8k
  }
1496
1497
108k
  unsigned int lib_flags = 0;
1498
108k
  size_t size, cx_data_sz;
1499
108k
  unsigned char *cx_data;
1500
1501
  // Per-frame PSNR is not supported when g_lag_in_frames is greater than 0.
1502
108k
  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
108k
  cpi->b_calculate_psnr = (ctx->base.init_flags & VPX_CODEC_USE_PSNR) ||
1512
108k
                          (flags & VPX_EFLAG_CALCULATE_PSNR);
1513
108k
#endif
1514
1515
108k
  if (img != NULL) {
1516
75.9k
    YV12_BUFFER_CONFIG sd;
1517
1518
75.9k
    if (!ctx->pts_offset_initialized) {
1519
3.72k
      ctx->pts_offset = pts;
1520
3.72k
      ctx->pts_offset_initialized = 1;
1521
3.72k
    }
1522
75.9k
    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
75.9k
    pts -= ctx->pts_offset;
1527
75.9k
    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
75.9k
    const int64_t dst_time_stamp = timebase_units_to_ticks(timebase_in_ts, pts);
1532
1533
75.9k
    cpi->svc.timebase_fac = timebase_units_to_ticks(timebase_in_ts, 1);
1534
75.9k
    cpi->svc.time_stamp_superframe = dst_time_stamp;
1535
1536
75.9k
#if ULONG_MAX > INT64_MAX
1537
75.9k
    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
75.9k
#endif
1542
75.9k
    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
75.9k
    vpx_codec_pts_t pts_end = pts + (int64_t)duration;
1547
75.9k
    if (pts_end > INT64_MAX / timebase_in_ts->num) {
1548
7
      vpx_internal_error(
1549
7
          &cpi->common.error, VPX_CODEC_INVALID_PARAM,
1550
7
          "conversion of relative pts + duration to ticks would overflow");
1551
7
    }
1552
75.9k
    const int64_t dst_end_time_stamp =
1553
75.9k
        timebase_units_to_ticks(timebase_in_ts, pts_end);
1554
75.9k
    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
75.9k
    if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1559
75.9k
                              dst_time_stamp, dst_end_time_stamp)) {
1560
0
      res = update_error_state(ctx, &cpi->common.error);
1561
0
    }
1562
75.9k
    ctx->next_frame_flags = 0;
1563
75.9k
  }
1564
1565
108k
  cx_data = ctx->cx_data;
1566
108k
  cx_data_sz = ctx->cx_data_sz;
1567
1568
  /* Any pending invisible frames? */
1569
108k
  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
108k
  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
108k
  } else {
1616
108k
    ENCODE_FRAME_RESULT encode_frame_result;
1617
108k
    int64_t dst_time_stamp;
1618
108k
    int64_t dst_end_time_stamp;
1619
108k
    vp9_init_encode_frame_result(&encode_frame_result);
1620
183k
    while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1621
178k
           -1 != vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1622
178k
                                         cx_data_sz, &dst_time_stamp,
1623
178k
                                         &dst_end_time_stamp, !img,
1624
178k
                                         &encode_frame_result)) {
1625
      // Pack psnr pkt.
1626
75.1k
      if (size > 0) {
1627
75.1k
        PSNR_STATS psnr;
1628
75.1k
        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
75.1k
      }
1633
1634
75.1k
      if (size || (cpi->use_svc && cpi->svc.skip_enhancement_layer)) {
1635
        // Pack invisible frames with the next visible frame
1636
75.1k
        if (!cpi->common.show_frame ||
1637
75.1k
            (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
75.1k
        pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1672
75.1k
        pkt.data.frame.pts =
1673
75.1k
            ticks_to_timebase_units(timebase_in_ts, dst_time_stamp) +
1674
75.1k
            ctx->pts_offset;
1675
75.1k
        pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1676
75.1k
            timebase_in_ts, dst_end_time_stamp - dst_time_stamp);
1677
75.1k
        pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1678
75.1k
        pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
1679
75.1k
        pkt.data.frame.height[cpi->svc.spatial_layer_id] = cpi->common.height;
1680
75.1k
        pkt.data.frame.spatial_layer_encoded[cpi->svc.spatial_layer_id] =
1681
75.1k
            1 - cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id];
1682
1683
75.1k
        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
75.1k
        } else {
1701
75.1k
          pkt.data.frame.buf = cx_data;
1702
75.1k
          pkt.data.frame.sz = size;
1703
75.1k
        }
1704
75.1k
        pkt.data.frame.partition_id = -1;
1705
1706
75.1k
        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
75.1k
        else
1710
75.1k
          vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1711
1712
75.1k
        cx_data += size;
1713
75.1k
        cx_data_sz -= size;
1714
75.1k
        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
75.1k
      }
1720
75.1k
    }
1721
108k
  }
1722
1723
108k
  cpi->common.error.setjmp = 0;
1724
108k
  return res;
1725
108k
}
1726
1727
static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1728
183k
                                                    vpx_codec_iter_t *iter) {
1729
183k
  return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1730
183k
}
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
3.80k
                                            va_list args) {
2092
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2093
3.80k
  extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
2094
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
2095
3.80k
}
2096
2097
static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
2098
3.80k
                                            va_list args) {
2099
3.80k
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
2100
3.80k
  extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
2101
3.80k
  return update_extra_cfg(ctx, &extra_cfg);
2102
3.80k
}
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 (qp < 0 || qp > 63) return VPX_CODEC_INVALID_PARAM;
2190
2191
0
  cfg->rc_min_quantizer = cfg->rc_max_quantizer = qp;
2192
0
  extra_cfg.aq_mode = 0;
2193
0
  cpi->fixed_qp_onepass = 1;
2194
2195
0
  res = update_extra_cfg(ctx, &extra_cfg);
2196
0
  return res;
2197
0
}
2198
2199
static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
2200
  { VP8_COPY_REFERENCE, ctrl_copy_reference },
2201
2202
  // Setters
2203
  { VP8_SET_REFERENCE, ctrl_set_reference },
2204
  { VP8_SET_POSTPROC, ctrl_set_previewpp },
2205
  { VP9E_SET_ROI_MAP, ctrl_set_roi_map },
2206
  { VP8E_SET_ACTIVEMAP, ctrl_set_active_map },
2207
  { VP8E_SET_SCALEMODE, ctrl_set_scale_mode },
2208
  { VP8E_SET_CPUUSED, ctrl_set_cpuused },
2209
  { VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
2210
  { VP8E_SET_SHARPNESS, ctrl_set_sharpness },
2211
  { VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
2212
  { VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
2213
  { VP9E_SET_TILE_ROWS, ctrl_set_tile_rows },
2214
  { VP9E_SET_TPL, ctrl_set_tpl_model },
2215
  { VP9E_SET_KEY_FRAME_FILTERING, ctrl_set_keyframe_filtering },
2216
  { VP9E_SET_VALIDATE_HBD_INPUT, ctrl_set_validate_hbd_input },
2217
  { VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
2218
  { VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
2219
  { VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type },
2220
  { VP8E_SET_TUNING, ctrl_set_tuning },
2221
  { VP8E_SET_CQ_LEVEL, ctrl_set_cq_level },
2222
  { VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
2223
  { VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
2224
  { VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
2225
  { VP9E_SET_LOSSLESS, ctrl_set_lossless },
2226
  { VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
2227
  { VP9E_SET_AQ_MODE, ctrl_set_aq_mode },
2228
  { VP9E_SET_ALT_REF_AQ, ctrl_set_alt_ref_aq },
2229
  { VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
2230
  { VP9E_SET_SVC, ctrl_set_svc },
2231
  { VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters },
2232
  { VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback },
2233
  { VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id },
2234
  { VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content },
2235
  { VP9E_SET_COLOR_SPACE, ctrl_set_color_space },
2236
  { VP9E_SET_COLOR_RANGE, ctrl_set_color_range },
2237
  { VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
2238
  { VP9E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
2239
  { VP9E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
2240
  { VP9E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
2241
  { VP9E_SET_RENDER_SIZE, ctrl_set_render_size },
2242
  { VP9E_SET_TARGET_LEVEL, ctrl_set_target_level },
2243
  { VP9E_SET_ROW_MT, ctrl_set_row_mt },
2244
  { VP9E_SET_POSTENCODE_DROP, ctrl_set_postencode_drop },
2245
  { VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR, ctrl_set_disable_overshoot_maxq_cbr },
2246
  { VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
2247
  { VP9E_SET_SVC_INTER_LAYER_PRED, ctrl_set_svc_inter_layer_pred },
2248
  { VP9E_SET_SVC_FRAME_DROP_LAYER, ctrl_set_svc_frame_drop_layer },
2249
  { VP9E_SET_SVC_GF_TEMPORAL_REF, ctrl_set_svc_gf_temporal_ref },
2250
  { VP9E_SET_SVC_SPATIAL_LAYER_SYNC, ctrl_set_svc_spatial_layer_sync },
2251
  { VP9E_SET_DELTA_Q_UV, ctrl_set_delta_q_uv },
2252
  { VP9E_SET_DISABLE_LOOPFILTER, ctrl_set_disable_loopfilter },
2253
  { VP9E_SET_RTC_EXTERNAL_RATECTRL, ctrl_set_rtc_external_ratectrl },
2254
  { VP9E_SET_EXTERNAL_RATE_CONTROL, ctrl_set_external_rate_control },
2255
  { VP9E_SET_QUANTIZER_ONE_PASS, ctrl_set_quantizer_one_pass },
2256
2257
  // Getters
2258
  { VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer },
2259
  { VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
2260
  { VP9E_GET_LAST_QUANTIZER_SVC_LAYERS, ctrl_get_quantizer_svc_layers },
2261
  { VP9E_GET_LOOPFILTER_LEVEL, ctrl_get_loopfilter_level },
2262
  { VP9_GET_REFERENCE, ctrl_get_reference },
2263
  { VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id },
2264
  { VP9E_GET_ACTIVEMAP, ctrl_get_active_map },
2265
  { VP9E_GET_LEVEL, ctrl_get_level },
2266
  { VP9E_GET_SVC_REF_FRAME_CONFIG, ctrl_get_svc_ref_frame_config },
2267
2268
  { -1, NULL },
2269
};
2270
2271
static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
2272
  { 0,
2273
    {
2274
        // NOLINT
2275
        0,  // g_usage (unused)
2276
        8,  // g_threads
2277
        0,  // g_profile
2278
2279
        320,         // g_width
2280
        240,         // g_height
2281
        VPX_BITS_8,  // g_bit_depth
2282
        8,           // g_input_bit_depth
2283
2284
        { 1, 30 },  // g_timebase
2285
2286
        0,  // g_error_resilient
2287
2288
        VPX_RC_ONE_PASS,  // g_pass
2289
2290
        25,  // g_lag_in_frames
2291
2292
        0,   // rc_dropframe_thresh
2293
        0,   // rc_resize_allowed
2294
        0,   // rc_scaled_width
2295
        0,   // rc_scaled_height
2296
        60,  // rc_resize_down_thresh
2297
        30,  // rc_resize_up_thresh
2298
2299
        VPX_VBR,      // rc_end_usage
2300
        { NULL, 0 },  // rc_twopass_stats_in
2301
        { NULL, 0 },  // rc_firstpass_mb_stats_in
2302
        256,          // rc_target_bitrate
2303
        0,            // rc_min_quantizer
2304
        63,           // rc_max_quantizer
2305
        25,           // rc_undershoot_pct
2306
        25,           // rc_overshoot_pct
2307
2308
        6000,  // rc_max_buffer_size
2309
        4000,  // rc_buffer_initial_size
2310
        5000,  // rc_buffer_optimal_size
2311
2312
        50,    // rc_two_pass_vbrbias
2313
        0,     // rc_two_pass_vbrmin_section
2314
        2000,  // rc_two_pass_vbrmax_section
2315
        0,     // rc_2pass_vbr_corpus_complexity (non 0 for corpus vbr)
2316
2317
        // keyframing settings (kf)
2318
        VPX_KF_AUTO,  // g_kfmode
2319
        0,            // kf_min_dist
2320
        128,          // kf_max_dist
2321
2322
        VPX_SS_DEFAULT_LAYERS,  // ss_number_layers
2323
        { 0 },
2324
        { 0 },     // ss_target_bitrate
2325
        1,         // ts_number_layers
2326
        { 0 },     // ts_target_bitrate
2327
        { 0 },     // ts_rate_decimator
2328
        0,         // ts_periodicity
2329
        { 0 },     // ts_layer_id
2330
        { 0 },     // layer_target_bitrate
2331
        0,         // temporal_layering_mode
2332
        0,         // use_vizier_rc_params
2333
        { 1, 1 },  // active_wq_factor
2334
        { 1, 1 },  // err_per_mb_factor
2335
        { 1, 1 },  // sr_default_decay_limit
2336
        { 1, 1 },  // sr_diff_factor
2337
        { 1, 1 },  // kf_err_per_mb_factor
2338
        { 1, 1 },  // kf_frame_min_boost_factor
2339
        { 1, 1 },  // kf_frame_max_boost_first_factor
2340
        { 1, 1 },  // kf_frame_max_boost_subs_factor
2341
        { 1, 1 },  // kf_max_total_boost_factor
2342
        { 1, 1 },  // gf_max_total_boost_factor
2343
        { 1, 1 },  // gf_frame_max_boost_factor
2344
        { 1, 1 },  // zm_factor
2345
        { 1, 1 },  // rd_mult_inter_qp_fac
2346
        { 1, 1 },  // rd_mult_arf_qp_fac
2347
        { 1, 1 },  // rd_mult_key_qp_fac
2348
    } },
2349
};
2350
2351
#ifndef VERSION_STRING
2352
#define VERSION_STRING
2353
#endif
2354
CODEC_INTERFACE(vpx_codec_vp9_cx) = {
2355
  "WebM Project VP9 Encoder" VERSION_STRING,
2356
  VPX_CODEC_INTERNAL_ABI_VERSION,
2357
#if CONFIG_VP9_HIGHBITDEPTH
2358
  VPX_CODEC_CAP_HIGHBITDEPTH |
2359
#endif
2360
      VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,  // vpx_codec_caps_t
2361
  encoder_init,                                    // vpx_codec_init_fn_t
2362
  encoder_destroy,                                 // vpx_codec_destroy_fn_t
2363
  encoder_ctrl_maps,                               // vpx_codec_ctrl_fn_map_t
2364
  {
2365
      // NOLINT
2366
      NULL,  // vpx_codec_peek_si_fn_t
2367
      NULL,  // vpx_codec_get_si_fn_t
2368
      NULL,  // vpx_codec_decode_fn_t
2369
      NULL,  // vpx_codec_frame_get_fn_t
2370
      NULL   // vpx_codec_set_fb_fn_t
2371
  },
2372
  {
2373
      // NOLINT
2374
      1,                           // 1 cfg map
2375
      encoder_usage_cfg_map,       // vpx_codec_enc_cfg_map_t
2376
      encoder_encode,              // vpx_codec_encode_fn_t
2377
      encoder_get_cxdata,          // vpx_codec_get_cx_data_fn_t
2378
      encoder_set_config,          // vpx_codec_enc_config_set_fn_t
2379
      encoder_get_global_headers,  // vpx_codec_get_global_headers_fn_t
2380
      encoder_get_preview,         // vpx_codec_get_preview_frame_fn_t
2381
      NULL,                        // vpx_codec_enc_mr_get_mem_loc_fn_t
2382
      NULL                         // vpx_codec_enc_mr_free_mem_loc_fn_t
2383
  }
2384
};
2385
2386
static vpx_codec_enc_cfg_t get_enc_cfg(int frame_width, int frame_height,
2387
                                       vpx_rational_t frame_rate,
2388
                                       int target_bitrate,
2389
0
                                       vpx_enc_pass enc_pass) {
2390
0
  vpx_codec_enc_cfg_t enc_cfg = encoder_usage_cfg_map[0].cfg;
2391
0
  enc_cfg.g_w = frame_width;
2392
0
  enc_cfg.g_h = frame_height;
2393
0
  enc_cfg.rc_target_bitrate = target_bitrate;
2394
0
  enc_cfg.g_pass = enc_pass;
2395
  // g_timebase is the inverse of frame_rate
2396
0
  enc_cfg.g_timebase.num = frame_rate.den;
2397
0
  enc_cfg.g_timebase.den = frame_rate.num;
2398
0
  return enc_cfg;
2399
0
}
2400
2401
0
static vp9_extracfg get_extra_cfg(void) {
2402
0
  vp9_extracfg extra_cfg = default_extra_cfg;
2403
0
  return extra_cfg;
2404
0
}
2405
2406
VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
2407
                                        vpx_rational_t frame_rate,
2408
                                        int target_bitrate, int encode_speed,
2409
                                        int target_level,
2410
0
                                        vpx_enc_pass enc_pass) {
2411
  /* This function will generate the same VP9EncoderConfig used by the
2412
   * vpxenc command given below.
2413
   * The configs in the vpxenc command corresponds to parameters of
2414
   * vp9_get_encoder_config() as follows.
2415
   *
2416
   * WIDTH:   frame_width
2417
   * HEIGHT:  frame_height
2418
   * FPS:     frame_rate
2419
   * BITRATE: target_bitrate
2420
   * CPU_USED:encode_speed
2421
   * TARGET_LEVEL: target_level
2422
   *
2423
   * INPUT, OUTPUT, LIMIT will not affect VP9EncoderConfig
2424
   *
2425
   * vpxenc command:
2426
   * INPUT=bus_cif.y4m
2427
   * OUTPUT=output.webm
2428
   * WIDTH=352
2429
   * HEIGHT=288
2430
   * BITRATE=600
2431
   * FPS=30/1
2432
   * LIMIT=150
2433
   * CPU_USED=0
2434
   * TARGET_LEVEL=0
2435
   * ./vpxenc --limit=$LIMIT --width=$WIDTH --height=$HEIGHT --fps=$FPS
2436
   * --lag-in-frames=25 \
2437
   *  --codec=vp9 --good --cpu-used=CPU_USED --threads=0 --profile=0 \
2438
   *  --min-q=0 --max-q=63 --auto-alt-ref=1 --passes=2 --kf-max-dist=150 \
2439
   *  --kf-min-dist=0 --drop-frame=0 --static-thresh=0 --bias-pct=50 \
2440
   *  --minsection-pct=0 --maxsection-pct=150 --arnr-maxframes=7 --psnr \
2441
   *  --arnr-strength=5 --sharpness=0 --undershoot-pct=100 --overshoot-pct=100 \
2442
   *  --frame-parallel=0 --tile-columns=0 --cpu-used=0 --end-usage=vbr \
2443
   *  --target-bitrate=$BITRATE --target-level=0 -o $OUTPUT $INPUT
2444
   */
2445
2446
0
  VP9EncoderConfig oxcf;
2447
0
  vp9_extracfg extra_cfg = get_extra_cfg();
2448
0
  vpx_codec_enc_cfg_t enc_cfg = get_enc_cfg(
2449
0
      frame_width, frame_height, frame_rate, target_bitrate, enc_pass);
2450
0
  set_encoder_config(&oxcf, &enc_cfg, &extra_cfg);
2451
2452
  // These settings are made to match the settings of the vpxenc command.
2453
0
  oxcf.key_freq = 150;
2454
0
  oxcf.under_shoot_pct = 100;
2455
0
  oxcf.over_shoot_pct = 100;
2456
0
  oxcf.max_threads = 0;
2457
0
  oxcf.tile_columns = 0;
2458
0
  oxcf.frame_parallel_decoding_mode = 0;
2459
0
  oxcf.two_pass_vbrmax_section = 150;
2460
0
  oxcf.speed = abs(encode_speed);
2461
0
  oxcf.target_level = target_level;
2462
0
  return oxcf;
2463
0
}
2464
2465
#define DUMP_STRUCT_VALUE(fp, structure, value) \
2466
0
  fprintf(fp, #value " %" PRId64 "\n", (int64_t)(structure)->value)
2467
2468
0
void vp9_dump_encoder_config(const VP9EncoderConfig *oxcf, FILE *fp) {
2469
0
  DUMP_STRUCT_VALUE(fp, oxcf, profile);
2470
0
  DUMP_STRUCT_VALUE(fp, oxcf, bit_depth);
2471
0
  DUMP_STRUCT_VALUE(fp, oxcf, width);
2472
0
  DUMP_STRUCT_VALUE(fp, oxcf, height);
2473
0
  DUMP_STRUCT_VALUE(fp, oxcf, input_bit_depth);
2474
0
  DUMP_STRUCT_VALUE(fp, oxcf, init_framerate);
2475
  // TODO(angiebird): dump g_timebase
2476
  // TODO(angiebird): dump g_timebase_in_ts
2477
2478
0
  DUMP_STRUCT_VALUE(fp, oxcf, target_bandwidth);
2479
2480
0
  DUMP_STRUCT_VALUE(fp, oxcf, noise_sensitivity);
2481
0
  DUMP_STRUCT_VALUE(fp, oxcf, sharpness);
2482
0
  DUMP_STRUCT_VALUE(fp, oxcf, speed);
2483
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_max_intra_bitrate_pct);
2484
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_max_inter_bitrate_pct);
2485
0
  DUMP_STRUCT_VALUE(fp, oxcf, gf_cbr_boost_pct);
2486
2487
0
  DUMP_STRUCT_VALUE(fp, oxcf, mode);
2488
0
  DUMP_STRUCT_VALUE(fp, oxcf, pass);
2489
2490
  // Key Framing Operations
2491
0
  DUMP_STRUCT_VALUE(fp, oxcf, auto_key);
2492
0
  DUMP_STRUCT_VALUE(fp, oxcf, key_freq);
2493
2494
0
  DUMP_STRUCT_VALUE(fp, oxcf, lag_in_frames);
2495
2496
  // ----------------------------------------------------------------
2497
  // DATARATE CONTROL OPTIONS
2498
2499
  // vbr, cbr, constrained quality or constant quality
2500
0
  DUMP_STRUCT_VALUE(fp, oxcf, rc_mode);
2501
2502
  // buffer targeting aggressiveness
2503
0
  DUMP_STRUCT_VALUE(fp, oxcf, under_shoot_pct);
2504
0
  DUMP_STRUCT_VALUE(fp, oxcf, over_shoot_pct);
2505
2506
  // buffering parameters
2507
  // TODO(angiebird): dump tarting_buffer_level_ms
2508
  // TODO(angiebird): dump ptimal_buffer_level_ms
2509
  // TODO(angiebird): dump maximum_buffer_size_ms
2510
2511
  // Frame drop threshold.
2512
0
  DUMP_STRUCT_VALUE(fp, oxcf, drop_frames_water_mark);
2513
2514
  // controlling quality
2515
0
  DUMP_STRUCT_VALUE(fp, oxcf, fixed_q);
2516
0
  DUMP_STRUCT_VALUE(fp, oxcf, worst_allowed_q);
2517
0
  DUMP_STRUCT_VALUE(fp, oxcf, best_allowed_q);
2518
0
  DUMP_STRUCT_VALUE(fp, oxcf, cq_level);
2519
0
  DUMP_STRUCT_VALUE(fp, oxcf, aq_mode);
2520
2521
  // Special handling of Adaptive Quantization for AltRef frames
2522
0
  DUMP_STRUCT_VALUE(fp, oxcf, alt_ref_aq);
2523
2524
  // Internal frame size scaling.
2525
0
  DUMP_STRUCT_VALUE(fp, oxcf, resize_mode);
2526
0
  DUMP_STRUCT_VALUE(fp, oxcf, scaled_frame_width);
2527
0
  DUMP_STRUCT_VALUE(fp, oxcf, scaled_frame_height);
2528
2529
  // Enable feature to reduce the frame quantization every x frames.
2530
0
  DUMP_STRUCT_VALUE(fp, oxcf, frame_periodic_boost);
2531
2532
  // two pass datarate control
2533
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrbias);
2534
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrmin_section);
2535
0
  DUMP_STRUCT_VALUE(fp, oxcf, two_pass_vbrmax_section);
2536
0
  DUMP_STRUCT_VALUE(fp, oxcf, vbr_corpus_complexity);
2537
  // END DATARATE CONTROL OPTIONS
2538
  // ----------------------------------------------------------------
2539
2540
  // Spatial and temporal scalability.
2541
0
  DUMP_STRUCT_VALUE(fp, oxcf, ss_number_layers);
2542
0
  DUMP_STRUCT_VALUE(fp, oxcf, ts_number_layers);
2543
2544
  // Bitrate allocation for spatial layers.
2545
  // TODO(angiebird): dump layer_target_bitrate[VPX_MAX_LAYERS]
2546
  // TODO(angiebird): dump ss_target_bitrate[VPX_SS_MAX_LAYERS]
2547
  // TODO(angiebird): dump ss_enable_auto_arf[VPX_SS_MAX_LAYERS]
2548
  // TODO(angiebird): dump ts_rate_decimator[VPX_TS_MAX_LAYERS]
2549
2550
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_auto_arf);
2551
0
  DUMP_STRUCT_VALUE(fp, oxcf, encode_breakout);
2552
0
  DUMP_STRUCT_VALUE(fp, oxcf, error_resilient_mode);
2553
0
  DUMP_STRUCT_VALUE(fp, oxcf, frame_parallel_decoding_mode);
2554
2555
0
  DUMP_STRUCT_VALUE(fp, oxcf, arnr_max_frames);
2556
0
  DUMP_STRUCT_VALUE(fp, oxcf, arnr_strength);
2557
2558
0
  DUMP_STRUCT_VALUE(fp, oxcf, min_gf_interval);
2559
0
  DUMP_STRUCT_VALUE(fp, oxcf, max_gf_interval);
2560
2561
0
  DUMP_STRUCT_VALUE(fp, oxcf, tile_columns);
2562
0
  DUMP_STRUCT_VALUE(fp, oxcf, tile_rows);
2563
2564
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_tpl_model);
2565
2566
0
  DUMP_STRUCT_VALUE(fp, oxcf, enable_keyframe_filtering);
2567
2568
0
  DUMP_STRUCT_VALUE(fp, oxcf, max_threads);
2569
2570
0
  DUMP_STRUCT_VALUE(fp, oxcf, target_level);
2571
2572
  // TODO(angiebird): dump two_pass_stats_in
2573
0
  DUMP_STRUCT_VALUE(fp, oxcf, tuning);
2574
0
  DUMP_STRUCT_VALUE(fp, oxcf, content);
2575
0
#if CONFIG_VP9_HIGHBITDEPTH
2576
0
  DUMP_STRUCT_VALUE(fp, oxcf, use_highbitdepth);
2577
0
#endif
2578
0
  DUMP_STRUCT_VALUE(fp, oxcf, color_space);
2579
0
  DUMP_STRUCT_VALUE(fp, oxcf, color_range);
2580
0
  DUMP_STRUCT_VALUE(fp, oxcf, render_width);
2581
0
  DUMP_STRUCT_VALUE(fp, oxcf, render_height);
2582
0
  DUMP_STRUCT_VALUE(fp, oxcf, temporal_layering_mode);
2583
2584
0
  DUMP_STRUCT_VALUE(fp, oxcf, row_mt);
2585
0
  DUMP_STRUCT_VALUE(fp, oxcf, motion_vector_unit_test);
2586
0
  DUMP_STRUCT_VALUE(fp, oxcf, delta_q_uv);
2587
0
}
2588
2589
3.80k
FRAME_INFO vp9_get_frame_info(const VP9EncoderConfig *oxcf) {
2590
3.80k
  FRAME_INFO frame_info;
2591
3.80k
  int dummy;
2592
3.80k
  frame_info.frame_width = oxcf->width;
2593
3.80k
  frame_info.frame_height = oxcf->height;
2594
3.80k
  frame_info.render_frame_width = oxcf->width;
2595
3.80k
  frame_info.render_frame_height = oxcf->height;
2596
3.80k
  frame_info.bit_depth = oxcf->bit_depth;
2597
3.80k
  vp9_set_mi_size(&frame_info.mi_rows, &frame_info.mi_cols, &dummy,
2598
3.80k
                  frame_info.frame_width, frame_info.frame_height);
2599
3.80k
  vp9_set_mb_size(&frame_info.mb_rows, &frame_info.mb_cols, &frame_info.num_mbs,
2600
3.80k
                  frame_info.mi_rows, frame_info.mi_cols);
2601
  // TODO(angiebird): Figure out how to get subsampling_x/y here
2602
3.80k
  return frame_info;
2603
3.80k
}
2604
2605
void vp9_set_first_pass_stats(VP9EncoderConfig *oxcf,
2606
30.6k
                              const vpx_fixed_buf_t *stats) {
2607
30.6k
  oxcf->two_pass_stats_in = *stats;
2608
30.6k
}