Coverage Report

Created: 2026-05-16 07:49

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