Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp8/vp8_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 <stdint.h>
14
#include <stdlib.h>
15
#include <string.h>
16
17
#include "./vpx_config.h"
18
#include "./vp8_rtcd.h"
19
#include "./vpx_dsp_rtcd.h"
20
#include "./vpx_scale_rtcd.h"
21
#include "vpx/vpx_encoder.h"
22
#include "vpx/internal/vpx_codec_internal.h"
23
#include "vpx_version.h"
24
#include "vpx_mem/vpx_mem.h"
25
#include "vpx_ports/static_assert.h"
26
#include "vpx_ports/system_state.h"
27
#include "vpx_util/vpx_timestamp.h"
28
#if CONFIG_MULTITHREAD
29
#include "vp8/encoder/ethreading.h"
30
#endif
31
#include "vp8/encoder/onyx_int.h"
32
#include "vpx/vp8cx.h"
33
#include "vp8/encoder/firstpass.h"
34
#include "vp8/common/onyx.h"
35
#include "vp8/common/common.h"
36
37
struct vp8_extracfg {
38
  struct vpx_codec_pkt_list *pkt_list;
39
  int cpu_used; /** available cpu percentage in 1/16*/
40
  /** if encoder decides to uses alternate reference frame */
41
  unsigned int enable_auto_alt_ref;
42
  unsigned int noise_sensitivity;
43
  unsigned int Sharpness;
44
  unsigned int static_thresh;
45
  unsigned int token_partitions;
46
  unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
47
  unsigned int arnr_strength;   /* alt_ref Noise Reduction Strength */
48
  unsigned int arnr_type;       /* alt_ref filter type */
49
  vp8e_tuning tuning;
50
  unsigned int cq_level; /* constrained quality level */
51
  unsigned int rc_max_intra_bitrate_pct;
52
  unsigned int gf_cbr_boost_pct;
53
  unsigned int screen_content_mode;
54
};
55
56
static struct vp8_extracfg default_extracfg = {
57
  NULL,
58
#if !(CONFIG_REALTIME_ONLY)
59
  0, /* cpu_used      */
60
#else
61
  4, /* cpu_used      */
62
#endif
63
  0, /* enable_auto_alt_ref */
64
  0, /* noise_sensitivity */
65
  0, /* Sharpness */
66
  0, /* static_thresh */
67
#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
68
  VP8_EIGHT_TOKENPARTITION,
69
#else
70
  VP8_ONE_TOKENPARTITION, /* token_partitions */
71
#endif
72
  0,  /* arnr_max_frames */
73
  3,  /* arnr_strength */
74
  3,  /* arnr_type*/
75
  0,  /* tuning*/
76
  10, /* cq_level */
77
  0,  /* rc_max_intra_bitrate_pct */
78
  0,  /* gf_cbr_boost_pct */
79
  0,  /* screen_content_mode */
80
};
81
82
struct vpx_codec_alg_priv {
83
  vpx_codec_priv_t base;
84
  vpx_codec_enc_cfg_t cfg;
85
  struct vp8_extracfg vp8_cfg;
86
  vpx_rational64_t timestamp_ratio;
87
  vpx_codec_pts_t pts_offset;
88
  unsigned char pts_offset_initialized;
89
  VP8_CONFIG oxcf;
90
  struct VP8_COMP *cpi;
91
  unsigned char *cx_data;
92
  unsigned int cx_data_sz;
93
  vpx_image_t preview_img;
94
  unsigned int next_frame_flag;
95
  vp8_postproc_cfg_t preview_ppcfg;
96
  /* pkt_list size depends on the maximum number of lagged frames allowed. */
97
  vpx_codec_pkt_list_decl(64) pkt_list;
98
  unsigned int fixed_kf_cntr;
99
  vpx_enc_frame_flags_t control_frame_flags;
100
};
101
102
// Called by vp8e_set_config() and vp8e_encode() only. Must not be called
103
// by vp8e_init() because the `error` paramerer (cpi->common.error) will be
104
// destroyed by vpx_codec_enc_init_ver() after vp8e_init() returns an error.
105
// See the "IMPORTANT" comment in vpx_codec_enc_init_ver().
106
static vpx_codec_err_t update_error_state(
107
27
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
108
27
  const vpx_codec_err_t res = error->error_code;
109
110
27
  if (res != VPX_CODEC_OK)
111
27
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;
112
113
27
  return res;
114
27
}
115
116
#undef ERROR
117
#define ERROR(str)                  \
118
71
  do {                              \
119
71
    ctx->base.err_detail = str;     \
120
71
    return VPX_CODEC_INVALID_PARAM; \
121
71
  } while (0)
122
123
#define RANGE_CHECK(p, memb, lo, hi)                                     \
124
4.99M
  do {                                                                   \
125
4.99M
    if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
126
4.99M
      ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
127
4.99M
  } while (0)
128
129
#define RANGE_CHECK_HI(p, memb, hi)                                     \
130
2.66M
  do {                                                                  \
131
2.66M
    if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
132
2.66M
  } while (0)
133
134
#define RANGE_CHECK_LO(p, memb, lo)                                     \
135
  do {                                                                  \
136
    if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
137
  } while (0)
138
139
#define RANGE_CHECK_BOOL(p, memb)                                     \
140
355k
  do {                                                                \
141
355k
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
142
355k
  } while (0)
143
144
static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
145
                                       const vpx_codec_enc_cfg_t *cfg,
146
                                       const struct vp8_extracfg *vp8_cfg,
147
177k
                                       int finalize) {
148
177k
  RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
149
177k
  RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
150
177k
  RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
151
177k
  RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
152
177k
  RANGE_CHECK_HI(cfg, g_profile, 3);
153
177k
  RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
154
177k
  RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
155
177k
  RANGE_CHECK_HI(cfg, g_threads, 64);
156
#if CONFIG_REALTIME_ONLY
157
  RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
158
#elif CONFIG_MULTI_RES_ENCODING
159
  if (ctx->base.enc.total_encoders > 1) RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
160
#else
161
177k
  RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
162
177k
#endif
163
177k
  RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
164
177k
  RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
165
177k
  RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
166
177k
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
167
177k
  RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
168
169
/* TODO: add spatial re-sampling support and frame dropping in
170
 * multi-res-encoder.*/
171
#if CONFIG_MULTI_RES_ENCODING
172
  if (ctx->base.enc.total_encoders > 1)
173
    RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
174
#else
175
177k
  RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
176
177k
#endif
177
177k
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
178
177k
  RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
179
177k
  RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
180
181
#if CONFIG_REALTIME_ONLY
182
  RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
183
#elif CONFIG_MULTI_RES_ENCODING
184
  if (ctx->base.enc.total_encoders > 1)
185
    RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
186
#else
187
177k
  RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
188
177k
#endif
189
190
  /* VP8 does not support a lower bound on the keyframe interval in
191
   * automatic keyframe placement mode.
192
   */
193
177k
  if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
194
166k
      cfg->kf_min_dist > 0)
195
0
    ERROR(
196
177k
        "kf_min_dist not supported in auto mode, use 0 "
197
177k
        "or kf_max_dist instead.");
198
199
177k
  RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
200
177k
  RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
201
202
#if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
203
  RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
204
#else
205
177k
  RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
206
177k
#endif
207
208
177k
  RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
209
177k
              VP8_EIGHT_TOKENPARTITION);
210
177k
  RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
211
177k
  RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
212
177k
  RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
213
177k
  RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
214
177k
  RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
215
177k
  RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2);
216
177k
  if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
217
21.5k
    RANGE_CHECK(vp8_cfg, cq_level, cfg->rc_min_quantizer,
218
177k
                cfg->rc_max_quantizer);
219
220
177k
#if !(CONFIG_REALTIME_ONLY)
221
177k
  if (cfg->g_pass == VPX_RC_LAST_PASS) {
222
0
    size_t packet_sz = sizeof(FIRSTPASS_STATS);
223
0
    int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
224
0
    FIRSTPASS_STATS *stats;
225
226
0
    if (!cfg->rc_twopass_stats_in.buf)
227
0
      ERROR("rc_twopass_stats_in.buf not set.");
228
229
0
    if (cfg->rc_twopass_stats_in.sz % packet_sz)
230
0
      ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
231
232
0
    if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
233
0
      ERROR("rc_twopass_stats_in requires at least two packets.");
234
235
0
    stats = (void *)((char *)cfg->rc_twopass_stats_in.buf +
236
0
                     (n_packets - 1) * packet_sz);
237
238
0
    if ((int)(stats->count + 0.5) != n_packets - 1)
239
0
      ERROR("rc_twopass_stats_in missing EOS stats packet");
240
0
  }
241
177k
#endif
242
243
177k
  RANGE_CHECK(cfg, ts_number_layers, 1, 5);
244
245
177k
  if (cfg->ts_number_layers > 1) {
246
0
    unsigned int i;
247
0
    RANGE_CHECK_HI(cfg, ts_periodicity, 16);
248
249
0
    for (i = 1; i < cfg->ts_number_layers; ++i) {
250
0
      if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i - 1] &&
251
0
          cfg->rc_target_bitrate > 0)
252
0
        ERROR("ts_target_bitrate entries are not strictly increasing");
253
0
    }
254
255
0
    RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
256
0
    for (i = cfg->ts_number_layers - 2; i > 0; i--) {
257
0
      if (cfg->ts_rate_decimator[i - 1] != 2 * cfg->ts_rate_decimator[i])
258
0
        ERROR("ts_rate_decimator factors are not powers of 2");
259
0
    }
260
261
0
    RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers - 1);
262
0
  }
263
264
#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
265
  if (cfg->g_threads > (1 << vp8_cfg->token_partitions))
266
    ERROR("g_threads cannot be bigger than number of token partitions");
267
#endif
268
269
  // The range below shall be further tuned.
270
177k
  RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
271
177k
  RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
272
177k
  RANGE_CHECK(cfg, err_per_mb_factor.den, 1, 1000);
273
177k
  RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
274
177k
  RANGE_CHECK(cfg, sr_diff_factor.den, 1, 1000);
275
177k
  RANGE_CHECK(cfg, kf_err_per_mb_factor.den, 1, 1000);
276
177k
  RANGE_CHECK(cfg, kf_frame_min_boost_factor.den, 1, 1000);
277
177k
  RANGE_CHECK(cfg, kf_frame_max_boost_subs_factor.den, 1, 1000);
278
177k
  RANGE_CHECK(cfg, kf_max_total_boost_factor.den, 1, 1000);
279
177k
  RANGE_CHECK(cfg, gf_max_total_boost_factor.den, 1, 1000);
280
177k
  RANGE_CHECK(cfg, gf_frame_max_boost_factor.den, 1, 1000);
281
177k
  RANGE_CHECK(cfg, zm_factor.den, 1, 1000);
282
177k
  RANGE_CHECK(cfg, rd_mult_inter_qp_fac.den, 1, 1000);
283
177k
  RANGE_CHECK(cfg, rd_mult_arf_qp_fac.den, 1, 1000);
284
177k
  RANGE_CHECK(cfg, rd_mult_key_qp_fac.den, 1, 1000);
285
286
177k
  return VPX_CODEC_OK;
287
177k
}
288
289
static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
290
116k
                                    const vpx_image_t *img) {
291
116k
  switch (img->fmt) {
292
0
    case VPX_IMG_FMT_YV12:
293
116k
    case VPX_IMG_FMT_I420:
294
116k
    case VPX_IMG_FMT_NV12: break;
295
0
    default:
296
0
      ERROR(
297
116k
          "Invalid image format. Only YV12, I420 and NV12 images are "
298
116k
          "supported");
299
116k
  }
300
301
116k
  if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
302
0
    ERROR("Image size must match encoder init configuration size");
303
116k
  assert(img->fmt & VPX_IMG_FMT_PLANAR);
304
116k
  if (img->stride[VPX_PLANE_U] != img->stride[VPX_PLANE_V])
305
0
    ERROR("Image U/V strides must match");
306
307
116k
  return VPX_CODEC_OK;
308
116k
}
309
310
static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
311
                                       vpx_codec_enc_cfg_t cfg,
312
                                       struct vp8_extracfg vp8_cfg,
313
54.2k
                                       vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
314
54.2k
  oxcf->multi_threaded = cfg.g_threads;
315
54.2k
  oxcf->Version = cfg.g_profile;
316
317
54.2k
  oxcf->Width = cfg.g_w;
318
54.2k
  oxcf->Height = cfg.g_h;
319
54.2k
  oxcf->timebase = cfg.g_timebase;
320
321
54.2k
  oxcf->error_resilient_mode = cfg.g_error_resilient;
322
323
54.2k
  switch (cfg.g_pass) {
324
54.2k
    case VPX_RC_ONE_PASS: oxcf->Mode = MODE_BESTQUALITY; break;
325
0
    case VPX_RC_FIRST_PASS: oxcf->Mode = MODE_FIRSTPASS; break;
326
0
    case VPX_RC_LAST_PASS: oxcf->Mode = MODE_SECONDPASS_BEST; break;
327
54.2k
  }
328
329
54.2k
  if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS) {
330
54.2k
    oxcf->allow_lag = 0;
331
54.2k
    oxcf->lag_in_frames = 0;
332
54.2k
  } else {
333
0
    oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
334
0
    oxcf->lag_in_frames = cfg.g_lag_in_frames;
335
0
  }
336
337
54.2k
  oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
338
54.2k
  oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
339
340
54.2k
  oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
341
54.2k
  oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
342
54.2k
  oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
343
344
54.2k
  if (cfg.rc_end_usage == VPX_VBR) {
345
51.2k
    oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
346
51.2k
  } else if (cfg.rc_end_usage == VPX_CBR) {
347
0
    oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
348
3.07k
  } else if (cfg.rc_end_usage == VPX_CQ) {
349
3.07k
    oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
350
3.07k
  } else if (cfg.rc_end_usage == VPX_Q) {
351
0
    oxcf->end_usage = USAGE_CONSTANT_QUALITY;
352
0
  }
353
354
  // Cap the target rate to 1000 Mbps to avoid some integer overflows in
355
  // target bandwidth calculations.
356
54.2k
  oxcf->target_bandwidth = VPXMIN(cfg.rc_target_bitrate, 1000000);
357
54.2k
  oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
358
54.2k
  oxcf->gf_cbr_boost_pct = vp8_cfg.gf_cbr_boost_pct;
359
360
54.2k
  oxcf->best_allowed_q = cfg.rc_min_quantizer;
361
54.2k
  oxcf->worst_allowed_q = cfg.rc_max_quantizer;
362
54.2k
  oxcf->cq_level = vp8_cfg.cq_level;
363
54.2k
  oxcf->fixed_q = -1;
364
365
54.2k
  oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
366
54.2k
  oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
367
368
54.2k
  oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
369
54.2k
  oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
370
54.2k
  oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
371
372
54.2k
  oxcf->maximum_buffer_size = cfg.rc_buf_sz;
373
54.2k
  oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
374
54.2k
  oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
375
376
54.2k
  oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
377
54.2k
  oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
378
54.2k
  oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
379
380
54.2k
  oxcf->auto_key =
381
54.2k
      cfg.kf_mode == VPX_KF_AUTO && cfg.kf_min_dist != cfg.kf_max_dist;
382
54.2k
  oxcf->key_freq = cfg.kf_max_dist;
383
384
54.2k
  oxcf->number_of_layers = cfg.ts_number_layers;
385
54.2k
  oxcf->periodicity = cfg.ts_periodicity;
386
387
54.2k
  if (oxcf->number_of_layers > 1) {
388
0
    memcpy(oxcf->target_bitrate, cfg.ts_target_bitrate,
389
0
           sizeof(cfg.ts_target_bitrate));
390
0
    memcpy(oxcf->rate_decimator, cfg.ts_rate_decimator,
391
0
           sizeof(cfg.ts_rate_decimator));
392
0
    memcpy(oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
393
0
  }
394
395
#if CONFIG_MULTI_RES_ENCODING
396
  /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
397
   * are both memset to 0, which ensures the correct logic under this
398
   * situation.
399
   */
400
  if (mr_cfg) {
401
    oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
402
    oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
403
    oxcf->mr_down_sampling_factor = mr_cfg->mr_down_sampling_factor;
404
    oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
405
  }
406
#else
407
54.2k
  (void)mr_cfg;
408
54.2k
#endif
409
410
54.2k
  oxcf->cpu_used = vp8_cfg.cpu_used;
411
54.2k
  if (cfg.g_pass == VPX_RC_FIRST_PASS) {
412
0
    oxcf->cpu_used = VPXMAX(4, oxcf->cpu_used);
413
0
  }
414
54.2k
  oxcf->encode_breakout = vp8_cfg.static_thresh;
415
54.2k
  oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
416
54.2k
  oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
417
54.2k
  oxcf->Sharpness = vp8_cfg.Sharpness;
418
54.2k
  oxcf->token_partitions = vp8_cfg.token_partitions;
419
420
54.2k
  oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
421
54.2k
  oxcf->output_pkt_list = vp8_cfg.pkt_list;
422
423
54.2k
  oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
424
54.2k
  oxcf->arnr_strength = vp8_cfg.arnr_strength;
425
54.2k
  oxcf->arnr_type = vp8_cfg.arnr_type;
426
427
54.2k
  oxcf->tuning = vp8_cfg.tuning;
428
429
54.2k
  oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
430
431
  /*
432
      printf("Current VP8 Settings: \n");
433
      printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
434
      printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
435
      printf("Sharpness: %d\n",    oxcf->Sharpness);
436
      printf("cpu_used: %d\n",  oxcf->cpu_used);
437
      printf("Mode: %d\n",     oxcf->Mode);
438
      printf("auto_key: %d\n",  oxcf->auto_key);
439
      printf("key_freq: %d\n", oxcf->key_freq);
440
      printf("end_usage: %d\n", oxcf->end_usage);
441
      printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
442
      printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
443
      printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
444
      printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
445
      printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
446
      printf("fixed_q: %d\n",  oxcf->fixed_q);
447
      printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
448
      printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
449
      printf("allow_spatial_resampling: %d\n",  oxcf->allow_spatial_resampling);
450
      printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
451
      printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
452
      printf("allow_df: %d\n", oxcf->allow_df);
453
      printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
454
      printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
455
      printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
456
      printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
457
      printf("allow_lag: %d\n", oxcf->allow_lag);
458
      printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
459
      printf("play_alternate: %d\n", oxcf->play_alternate);
460
      printf("Version: %d\n", oxcf->Version);
461
      printf("multi_threaded: %d\n",   oxcf->multi_threaded);
462
      printf("encode_breakout: %d\n", oxcf->encode_breakout);
463
  */
464
54.2k
  return VPX_CODEC_OK;
465
54.2k
}
466
467
static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
468
0
                                       const vpx_codec_enc_cfg_t *cfg) {
469
0
  vpx_codec_err_t res;
470
471
0
  if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
472
0
    if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
473
0
      ERROR("Cannot change width or height after initialization");
474
0
    if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
475
0
        (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
476
0
      ERROR("Cannot increase width or height larger than their initial values");
477
0
  }
478
479
  /* Prevent increasing lag_in_frames. This check is stricter than it needs
480
   * to be -- the limit is not increasing past the first lag_in_frames
481
   * value, but we don't track the initial config, only the last successful
482
   * config.
483
   */
484
0
  if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
485
0
    ERROR("Cannot increase lag_in_frames");
486
487
0
  res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
488
0
  if (res != VPX_CODEC_OK) return res;
489
490
0
  if (setjmp(ctx->cpi->common.error.jmp)) {
491
0
    const vpx_codec_err_t codec_err =
492
0
        update_error_state(ctx, &ctx->cpi->common.error);
493
0
    ctx->cpi->common.error.setjmp = 0;
494
0
    vpx_clear_system_state();
495
0
    assert(codec_err != VPX_CODEC_OK);
496
0
    return codec_err;
497
0
  }
498
499
0
  ctx->cpi->common.error.setjmp = 1;
500
0
  ctx->cfg = *cfg;
501
0
  set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
502
0
  vp8_change_config(ctx->cpi, &ctx->oxcf);
503
0
#if CONFIG_MULTITHREAD
504
0
  if (vp8cx_create_encoder_threads(ctx->cpi)) {
505
0
    ctx->cpi->common.error.setjmp = 0;
506
0
    return VPX_CODEC_ERROR;
507
0
  }
508
0
#endif
509
0
  ctx->cpi->common.error.setjmp = 0;
510
0
  return VPX_CODEC_OK;
511
0
}
512
513
0
static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args) {
514
0
  int *const arg = va_arg(args, int *);
515
0
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
516
0
  *arg = vp8_get_quantizer(ctx->cpi);
517
0
  return VPX_CODEC_OK;
518
0
}
519
520
static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx,
521
116k
                                       va_list args) {
522
116k
  int *const arg = va_arg(args, int *);
523
116k
  if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
524
116k
  *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
525
116k
  return VPX_CODEC_OK;
526
116k
}
527
528
static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
529
47.5k
                                       const struct vp8_extracfg *extra_cfg) {
530
47.5k
  const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
531
47.5k
  if (res == VPX_CODEC_OK) {
532
47.5k
    ctx->vp8_cfg = *extra_cfg;
533
47.5k
    set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
534
47.5k
    vp8_change_config(ctx->cpi, &ctx->oxcf);
535
47.5k
  }
536
47.5k
  return res;
537
47.5k
}
538
539
6.75k
static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args) {
540
6.75k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
541
6.75k
  extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
542
  // Use fastest speed setting (speed 16 or -16) if it's set beyond the range.
543
6.75k
  extra_cfg.cpu_used = VPXMIN(16, extra_cfg.cpu_used);
544
6.75k
  extra_cfg.cpu_used = VPXMAX(-16, extra_cfg.cpu_used);
545
6.75k
  return update_extracfg(ctx, &extra_cfg);
546
6.75k
}
547
548
static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
549
0
                                               va_list args) {
550
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
551
0
  extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
552
0
  return update_extracfg(ctx, &extra_cfg);
553
0
}
554
555
static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
556
6.72k
                                             va_list args) {
557
6.72k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
558
6.72k
  extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
559
6.72k
  return update_extracfg(ctx, &extra_cfg);
560
6.72k
}
561
562
0
static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args) {
563
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
564
0
  extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
565
0
  return update_extracfg(ctx, &extra_cfg);
566
0
}
567
568
static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
569
6.72k
                                         va_list args) {
570
6.72k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
571
6.72k
  extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
572
6.72k
  return update_extracfg(ctx, &extra_cfg);
573
6.72k
}
574
575
static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
576
6.72k
                                            va_list args) {
577
6.72k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
578
6.72k
  extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
579
6.72k
  return update_extracfg(ctx, &extra_cfg);
580
6.72k
}
581
582
static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
583
6.75k
                                           va_list args) {
584
6.75k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
585
6.75k
  extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
586
6.75k
  return update_extracfg(ctx, &extra_cfg);
587
6.75k
}
588
589
static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
590
6.75k
                                         va_list args) {
591
6.75k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
592
6.75k
  extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
593
6.75k
  return update_extracfg(ctx, &extra_cfg);
594
6.75k
}
595
596
6.75k
static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args) {
597
6.75k
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
598
6.75k
  extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
599
6.75k
  return update_extracfg(ctx, &extra_cfg);
600
6.75k
}
601
602
0
static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args) {
603
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
604
0
  extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
605
0
  return update_extracfg(ctx, &extra_cfg);
606
0
}
607
608
342
static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args) {
609
342
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
610
342
  extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
611
342
  return update_extracfg(ctx, &extra_cfg);
612
342
}
613
614
static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
615
0
                                                    va_list args) {
616
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
617
0
  extra_cfg.rc_max_intra_bitrate_pct =
618
0
      CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
619
0
  return update_extracfg(ctx, &extra_cfg);
620
0
}
621
622
static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
623
0
                                                    va_list args) {
624
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
625
0
  extra_cfg.gf_cbr_boost_pct = CAST(VP8E_SET_GF_CBR_BOOST_PCT, args);
626
0
  return update_extracfg(ctx, &extra_cfg);
627
0
}
628
629
static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
630
0
                                               va_list args) {
631
0
  struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
632
0
  extra_cfg.screen_content_mode = CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
633
0
  return update_extracfg(ctx, &extra_cfg);
634
0
}
635
636
static vpx_codec_err_t ctrl_set_rtc_external_ratectrl(vpx_codec_alg_priv_t *ctx,
637
0
                                                      va_list args) {
638
0
  VP8_COMP *cpi = ctx->cpi;
639
0
  const unsigned int data = CAST(VP8E_SET_RTC_EXTERNAL_RATECTRL, args);
640
0
  if (data) {
641
0
    cpi->cyclic_refresh_mode_enabled = 0;
642
0
    cpi->rt_always_update_correction_factor = 1;
643
0
    cpi->rt_drop_recode_on_overshoot = 0;
644
0
  }
645
0
  return VPX_CODEC_OK;
646
0
}
647
648
static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
649
0
                                         void **mem_loc) {
650
0
  vpx_codec_err_t res = VPX_CODEC_OK;
651
652
#if CONFIG_MULTI_RES_ENCODING
653
  LOWER_RES_FRAME_INFO *shared_mem_loc;
654
  int mb_rows = ((cfg->g_w + 15) >> 4);
655
  int mb_cols = ((cfg->g_h + 15) >> 4);
656
657
  shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
658
  if (!shared_mem_loc) {
659
    return VPX_CODEC_MEM_ERROR;
660
  }
661
662
  shared_mem_loc->mb_info =
663
      calloc(mb_rows * mb_cols, sizeof(LOWER_RES_MB_INFO));
664
  if (!(shared_mem_loc->mb_info)) {
665
    free(shared_mem_loc);
666
    res = VPX_CODEC_MEM_ERROR;
667
  } else {
668
    *mem_loc = (void *)shared_mem_loc;
669
    res = VPX_CODEC_OK;
670
  }
671
#else
672
0
  (void)cfg;
673
0
  *mem_loc = NULL;
674
0
#endif
675
0
  return res;
676
0
}
677
678
0
static void vp8e_mr_free_mem(void *mem_loc) {
679
#if CONFIG_MULTI_RES_ENCODING
680
  LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)mem_loc;
681
  free(shared_mem_loc->mb_info);
682
  free(mem_loc);
683
#else
684
0
  (void)mem_loc;
685
0
  assert(!mem_loc);
686
0
#endif
687
0
}
688
689
static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
690
6.82k
                                 vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
691
6.82k
  vpx_codec_err_t res = VPX_CODEC_OK;
692
693
6.82k
  vp8_rtcd();
694
6.82k
  vpx_dsp_rtcd();
695
6.82k
  vpx_scale_rtcd();
696
697
6.82k
  if (!ctx->priv) {
698
6.82k
    struct vpx_codec_alg_priv *priv =
699
6.82k
        (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
700
701
6.82k
    if (!priv) {
702
0
      return VPX_CODEC_MEM_ERROR;
703
0
    }
704
705
6.82k
    ctx->priv = (vpx_codec_priv_t *)priv;
706
6.82k
    ctx->priv->init_flags = ctx->init_flags;
707
708
6.82k
    if (ctx->config.enc) {
709
      /* Update the reference to the config structure to an
710
       * internal copy.
711
       */
712
6.82k
      priv->cfg = *ctx->config.enc;
713
6.82k
      ctx->config.enc = &priv->cfg;
714
6.82k
    }
715
716
6.82k
    priv->vp8_cfg = default_extracfg;
717
6.82k
    priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
718
719
6.82k
    priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
720
721
6.82k
    if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
722
723
6.82k
    priv->cx_data = malloc(priv->cx_data_sz);
724
725
6.82k
    if (!priv->cx_data) {
726
0
      priv->cx_data_sz = 0;
727
0
      return VPX_CODEC_MEM_ERROR;
728
0
    }
729
730
6.82k
    if (mr_cfg) {
731
0
      ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
732
6.82k
    } else {
733
6.82k
      ctx->priv->enc.total_encoders = 1;
734
6.82k
    }
735
736
6.82k
    vp8_initialize_enc();
737
738
6.82k
    res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
739
740
6.82k
    if (!res) {
741
6.75k
      priv->pts_offset_initialized = 0;
742
6.75k
      priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
743
6.75k
      priv->timestamp_ratio.num = (int64_t)priv->cfg.g_timebase.num;
744
6.75k
      priv->timestamp_ratio.num *= TICKS_PER_SEC;
745
6.75k
      reduce_ratio(&priv->timestamp_ratio);
746
747
6.75k
      set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
748
6.75k
      priv->cpi = vp8_create_compressor(&priv->oxcf);
749
6.75k
      if (!priv->cpi) {
750
#if CONFIG_MULTI_RES_ENCODING
751
        // Release ownership of mr_cfg->mr_low_res_mode_info on failure. This
752
        // prevents ownership confusion with the caller and avoids a double
753
        // free when vpx_codec_destroy() is called on this instance.
754
        priv->oxcf.mr_total_resolutions = 0;
755
        priv->oxcf.mr_encoder_id = 0;
756
        priv->oxcf.mr_low_res_mode_info = NULL;
757
#endif
758
0
        res = VPX_CODEC_MEM_ERROR;
759
0
      }
760
6.75k
    }
761
6.82k
  }
762
763
6.82k
  return res;
764
6.82k
}
765
766
6.82k
static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx) {
767
#if CONFIG_MULTI_RES_ENCODING
768
  /* Free multi-encoder shared memory */
769
  if (ctx->oxcf.mr_total_resolutions > 0 &&
770
      (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions - 1)) {
771
    vp8e_mr_free_mem(ctx->oxcf.mr_low_res_mode_info);
772
  }
773
#endif
774
775
6.82k
  free(ctx->cx_data);
776
6.82k
  vp8_remove_compressor(&ctx->cpi);
777
6.82k
  vpx_free(ctx);
778
6.82k
  return VPX_CODEC_OK;
779
6.82k
}
780
781
static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
782
116k
                                       YV12_BUFFER_CONFIG *yv12) {
783
116k
  const int y_w = img->d_w;
784
116k
  const int y_h = img->d_h;
785
116k
  const int uv_w = (img->d_w + 1) / 2;
786
116k
  const int uv_h = (img->d_h + 1) / 2;
787
116k
  vpx_codec_err_t res = VPX_CODEC_OK;
788
116k
  yv12->y_buffer = img->planes[VPX_PLANE_Y];
789
116k
  yv12->u_buffer = img->planes[VPX_PLANE_U];
790
116k
  yv12->v_buffer = img->planes[VPX_PLANE_V];
791
792
116k
  yv12->y_crop_width = y_w;
793
116k
  yv12->y_crop_height = y_h;
794
116k
  yv12->y_width = y_w;
795
116k
  yv12->y_height = y_h;
796
116k
  yv12->uv_crop_width = uv_w;
797
116k
  yv12->uv_crop_height = uv_h;
798
116k
  yv12->uv_width = uv_w;
799
116k
  yv12->uv_height = uv_h;
800
801
116k
  yv12->y_stride = img->stride[VPX_PLANE_Y];
802
116k
  assert(img->stride[VPX_PLANE_U] == img->stride[VPX_PLANE_V]);
803
116k
  yv12->uv_stride = img->stride[VPX_PLANE_U];
804
805
116k
  yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
806
116k
  return res;
807
116k
}
808
809
static vpx_codec_err_t pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
810
                                               unsigned long duration,
811
123k
                                               vpx_enc_deadline_t deadline) {
812
123k
  int new_qc;
813
814
123k
#if !(CONFIG_REALTIME_ONLY)
815
  /* Use best quality mode if no deadline is given. */
816
123k
  new_qc = MODE_BESTQUALITY;
817
818
123k
  if (deadline) {
819
    /* Convert duration parameter from stream timebase to microseconds */
820
123k
    VPX_STATIC_ASSERT(TICKS_PER_SEC > 1000000 &&
821
123k
                      (TICKS_PER_SEC % 1000000) == 0);
822
823
123k
    if (duration > UINT64_MAX / (uint64_t)ctx->timestamp_ratio.num) {
824
8
      ERROR("duration is too big");
825
8
    }
826
123k
    uint64_t duration_us =
827
123k
        duration * (uint64_t)ctx->timestamp_ratio.num /
828
123k
        ((uint64_t)ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
829
830
    /* If the deadline is more that the duration this frame is to be shown,
831
     * use good quality mode. Otherwise use realtime mode.
832
     */
833
123k
    new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
834
123k
  }
835
836
#else
837
  (void)duration;
838
  new_qc = MODE_REALTIME;
839
#endif
840
841
123k
  if (deadline == VPX_DL_REALTIME) {
842
0
    new_qc = MODE_REALTIME;
843
123k
  } else if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS) {
844
0
    new_qc = MODE_FIRSTPASS;
845
123k
  } else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS) {
846
0
    new_qc =
847
0
        (new_qc == MODE_BESTQUALITY) ? MODE_SECONDPASS_BEST : MODE_SECONDPASS;
848
0
  }
849
850
123k
  if (ctx->oxcf.Mode != new_qc) {
851
6.64k
    ctx->oxcf.Mode = new_qc;
852
6.64k
    vp8_change_config(ctx->cpi, &ctx->oxcf);
853
6.64k
  }
854
123k
  return VPX_CODEC_OK;
855
123k
}
856
857
static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
858
123k
                                                vpx_enc_frame_flags_t flags) {
859
  /* Handle Flags */
860
123k
  if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
861
123k
      ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
862
0
    ctx->base.err_detail = "Conflicting flags.";
863
0
    return VPX_CODEC_INVALID_PARAM;
864
0
  }
865
866
123k
  if (flags &
867
123k
      (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
868
0
    int ref = 7;
869
870
0
    if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP8_LAST_FRAME;
871
872
0
    if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP8_GOLD_FRAME;
873
874
0
    if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP8_ALTR_FRAME;
875
876
0
    vp8_use_as_reference(ctx->cpi, ref);
877
0
  }
878
879
123k
  if (flags &
880
123k
      (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
881
123k
       VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
882
0
    int upd = 7;
883
884
0
    if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP8_LAST_FRAME;
885
886
0
    if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP8_GOLD_FRAME;
887
888
0
    if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP8_ALTR_FRAME;
889
890
0
    vp8_update_reference(ctx->cpi, upd);
891
0
  }
892
893
123k
  if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
894
0
    vp8_update_entropy(ctx->cpi, 0);
895
0
  }
896
897
123k
  return VPX_CODEC_OK;
898
123k
}
899
900
static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
901
                                   const vpx_image_t *img, vpx_codec_pts_t pts,
902
                                   unsigned long duration,
903
                                   vpx_enc_frame_flags_t enc_flags,
904
126k
                                   vpx_enc_deadline_t deadline) {
905
126k
  volatile vpx_codec_err_t res = VPX_CODEC_OK;
906
  // Make a copy as volatile to avoid -Wclobbered with longjmp.
907
126k
  volatile vpx_enc_frame_flags_t flags = enc_flags;
908
126k
  volatile vpx_codec_pts_t pts_val = pts;
909
910
126k
  if (!ctx->cfg.rc_target_bitrate) {
911
#if CONFIG_MULTI_RES_ENCODING
912
    if (!ctx->cpi) return VPX_CODEC_ERROR;
913
    if (ctx->cpi->oxcf.mr_total_resolutions > 1) {
914
      LOWER_RES_FRAME_INFO *low_res_frame_info =
915
          (LOWER_RES_FRAME_INFO *)ctx->cpi->oxcf.mr_low_res_mode_info;
916
      if (!low_res_frame_info) return VPX_CODEC_ERROR;
917
      low_res_frame_info->skip_encoding_prev_stream = 1;
918
      if (ctx->cpi->oxcf.mr_encoder_id == 0)
919
        low_res_frame_info->skip_encoding_base_stream = 1;
920
    }
921
#endif
922
3.30k
    return res;
923
3.30k
  }
924
925
123k
  if (img) res = validate_img(ctx, img);
926
927
123k
  if (!res) res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
928
929
123k
  if (!res) res = pick_quickcompress_mode(ctx, duration, deadline);
930
123k
  vpx_codec_pkt_list_init(&ctx->pkt_list);
931
932
  // If no flags are set in the encode call, then use the frame flags as
933
  // defined via the control function: vp8e_set_frame_flags.
934
123k
  if (!flags) {
935
123k
    flags = ctx->control_frame_flags;
936
123k
  }
937
123k
  ctx->control_frame_flags = 0;
938
939
123k
  if (!res) res = set_reference_and_update(ctx, flags);
940
941
  /* Handle fixed keyframe intervals */
942
123k
  if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
943
123k
      ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
944
8.58k
    if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
945
8.58k
      flags |= VPX_EFLAG_FORCE_KF;
946
8.58k
      ctx->fixed_kf_cntr = 1;
947
8.58k
    }
948
8.58k
  }
949
950
  /* Initialize the encoder instance on the first frame */
951
123k
  if (!res && ctx->cpi) {
952
123k
    unsigned int lib_flags;
953
123k
    int64_t dst_time_stamp, dst_end_time_stamp;
954
123k
    size_t size, cx_data_sz;
955
123k
    unsigned char *cx_data;
956
123k
    unsigned char *cx_data_end;
957
123k
    int comp_data_state = 0;
958
959
123k
    if (setjmp(ctx->cpi->common.error.jmp)) {
960
27
      ctx->cpi->common.error.setjmp = 0;
961
27
      res = update_error_state(ctx, &ctx->cpi->common.error);
962
27
      vpx_clear_system_state();
963
27
      return res;
964
27
    }
965
123k
    ctx->cpi->common.error.setjmp = 1;
966
967
    // Per-frame PSNR is not supported when g_lag_in_frames is greater than 0.
968
123k
    if ((flags & VPX_EFLAG_CALCULATE_PSNR) && ctx->cfg.g_lag_in_frames != 0) {
969
0
      vpx_internal_error(
970
0
          &ctx->cpi->common.error, VPX_CODEC_INCAPABLE,
971
0
          "Cannot calculate per-frame PSNR when g_lag_in_frames is nonzero");
972
0
    }
973
    /* Set up internal flags */
974
#if CONFIG_INTERNAL_STATS
975
    assert(((VP8_COMP *)ctx->cpi)->b_calculate_psnr == 1);
976
#else
977
123k
    ((VP8_COMP *)ctx->cpi)->b_calculate_psnr =
978
123k
        (ctx->base.init_flags & VPX_CODEC_USE_PSNR) ||
979
123k
        (flags & VPX_EFLAG_CALCULATE_PSNR);
980
123k
#endif
981
982
123k
    if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION) {
983
0
      ((VP8_COMP *)ctx->cpi)->output_partition = 1;
984
0
    }
985
986
    /* Convert API flags to internal codec lib flags */
987
123k
    lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
988
989
123k
    if (img != NULL) {
990
116k
      YV12_BUFFER_CONFIG sd;
991
992
116k
      if (!ctx->pts_offset_initialized) {
993
6.54k
        ctx->pts_offset = pts_val;
994
6.54k
        ctx->pts_offset_initialized = 1;
995
6.54k
      }
996
116k
      if (pts_val < ctx->pts_offset) {
997
0
        vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
998
0
                           "pts is smaller than initial pts");
999
0
      }
1000
116k
      pts_val -= ctx->pts_offset;
1001
116k
      if (pts_val > INT64_MAX / ctx->timestamp_ratio.num) {
1002
0
        vpx_internal_error(
1003
0
            &ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
1004
0
            "conversion of relative pts to ticks would overflow");
1005
0
      }
1006
116k
      dst_time_stamp =
1007
116k
          pts_val * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
1008
116k
#if ULONG_MAX > INT64_MAX
1009
116k
      if (duration > INT64_MAX) {
1010
0
        vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
1011
0
                           "duration is too big");
1012
0
      }
1013
116k
#endif
1014
116k
      if (pts_val > INT64_MAX - (int64_t)duration) {
1015
0
        vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
1016
0
                           "relative pts + duration is too big");
1017
0
      }
1018
116k
      vpx_codec_pts_t pts_end = pts_val + (int64_t)duration;
1019
116k
      if (pts_end > INT64_MAX / ctx->timestamp_ratio.num) {
1020
24
        vpx_internal_error(
1021
24
            &ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
1022
24
            "conversion of relative pts + duration to ticks would overflow");
1023
24
      }
1024
116k
      dst_end_time_stamp =
1025
116k
          pts_end * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
1026
1027
116k
      res = image2yuvconfig(img, &sd);
1028
1029
116k
      if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags, &sd,
1030
116k
                                dst_time_stamp, dst_end_time_stamp)) {
1031
0
        VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
1032
0
        res = update_error_state(ctx, &cpi->common.error);
1033
0
      }
1034
1035
      /* reset for next frame */
1036
116k
      ctx->next_frame_flag = 0;
1037
116k
    }
1038
1039
123k
    cx_data = ctx->cx_data;
1040
123k
    cx_data_sz = ctx->cx_data_sz;
1041
123k
    cx_data_end = ctx->cx_data + cx_data_sz;
1042
123k
    lib_flags = 0;
1043
1044
239k
    while (cx_data_sz >= ctx->cx_data_sz / 2) {
1045
239k
      comp_data_state = vp8_get_compressed_data(
1046
239k
          ctx->cpi, &lib_flags, &size, cx_data, cx_data_end, &dst_time_stamp,
1047
239k
          &dst_end_time_stamp, !img);
1048
1049
239k
      if (comp_data_state == VPX_CODEC_CORRUPT_FRAME) {
1050
0
        ctx->cpi->common.error.setjmp = 0;
1051
0
        return VPX_CODEC_CORRUPT_FRAME;
1052
239k
      } else if (comp_data_state == -1) {
1053
123k
        break;
1054
123k
      }
1055
1056
116k
      if (size) {
1057
116k
        vpx_codec_pts_t round, delta;
1058
116k
        vpx_codec_cx_pkt_t pkt;
1059
116k
        VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
1060
1061
        /* Add the frame packet to the list of returned packets. */
1062
116k
        round = (vpx_codec_pts_t)ctx->timestamp_ratio.num / 2;
1063
116k
        if (round > 0) --round;
1064
116k
        delta = (dst_end_time_stamp - dst_time_stamp);
1065
116k
        pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1066
116k
        pkt.data.frame.pts =
1067
116k
            (dst_time_stamp * ctx->timestamp_ratio.den + round) /
1068
116k
                ctx->timestamp_ratio.num +
1069
116k
            ctx->pts_offset;
1070
116k
        pkt.data.frame.duration =
1071
116k
            (unsigned long)((delta * ctx->timestamp_ratio.den + round) /
1072
116k
                            ctx->timestamp_ratio.num);
1073
116k
        pkt.data.frame.flags = lib_flags << 16;
1074
116k
        pkt.data.frame.width[0] = cpi->common.Width;
1075
116k
        pkt.data.frame.height[0] = cpi->common.Height;
1076
116k
        pkt.data.frame.spatial_layer_encoded[0] = 1;
1077
1078
116k
        if (lib_flags & FRAMEFLAGS_KEY) {
1079
21.8k
          pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
1080
21.8k
        }
1081
1082
116k
        if (!cpi->common.show_frame) {
1083
0
          pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
1084
1085
          /* This timestamp should be as close as possible to the
1086
           * prior PTS so that if a decoder uses pts to schedule when
1087
           * to do this, we start right after last frame was decoded.
1088
           * Invisible frames have no duration.
1089
           */
1090
0
          pkt.data.frame.pts =
1091
0
              ((cpi->last_time_stamp_seen * ctx->timestamp_ratio.den + round) /
1092
0
               ctx->timestamp_ratio.num) +
1093
0
              ctx->pts_offset + 1;
1094
0
          pkt.data.frame.duration = 0;
1095
0
        }
1096
1097
116k
        if (cpi->droppable) pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
1098
1099
116k
        if (cpi->output_partition) {
1100
0
          int i;
1101
0
          const int num_partitions =
1102
0
              (1 << cpi->common.multi_token_partition) + 1;
1103
1104
0
          pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1105
1106
0
          for (i = 0; i < num_partitions; ++i) {
1107
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1108
            pkt.data.frame.buf = cpi->partition_d[i];
1109
#else
1110
0
            pkt.data.frame.buf = cx_data;
1111
0
            cx_data += cpi->partition_sz[i];
1112
0
            cx_data_sz -= cpi->partition_sz[i];
1113
0
#endif
1114
0
            pkt.data.frame.sz = cpi->partition_sz[i];
1115
0
            pkt.data.frame.partition_id = i;
1116
            /* don't set the fragment bit for the last partition */
1117
0
            if (i == (num_partitions - 1)) {
1118
0
              pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
1119
0
            }
1120
0
            vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1121
0
          }
1122
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1123
          /* In lagged mode the encoder can buffer multiple frames.
1124
           * We don't want this in partitioned output because
1125
           * partitions are spread all over the output buffer.
1126
           * So, force an exit!
1127
           */
1128
          cx_data_sz -= ctx->cx_data_sz / 2;
1129
#endif
1130
116k
        } else {
1131
116k
          pkt.data.frame.buf = cx_data;
1132
116k
          pkt.data.frame.sz = size;
1133
116k
          pkt.data.frame.partition_id = -1;
1134
116k
          vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1135
116k
          cx_data += size;
1136
116k
          cx_data_sz -= size;
1137
116k
        }
1138
116k
      }
1139
116k
    }
1140
123k
    ctx->cpi->common.error.setjmp = 0;
1141
123k
  }
1142
1143
123k
  return res;
1144
123k
}
1145
1146
static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1147
243k
                                                 vpx_codec_iter_t *iter) {
1148
243k
  return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1149
243k
}
1150
1151
static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
1152
0
                                          va_list args) {
1153
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1154
1155
0
  if (data) {
1156
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1157
0
    YV12_BUFFER_CONFIG sd;
1158
1159
0
    image2yuvconfig(&frame->img, &sd);
1160
0
    vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1161
0
    return VPX_CODEC_OK;
1162
0
  } else {
1163
0
    return VPX_CODEC_INVALID_PARAM;
1164
0
  }
1165
0
}
1166
1167
static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
1168
0
                                          va_list args) {
1169
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1170
1171
0
  if (data) {
1172
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1173
0
    YV12_BUFFER_CONFIG sd;
1174
1175
0
    image2yuvconfig(&frame->img, &sd);
1176
0
    vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1177
0
    return VPX_CODEC_OK;
1178
0
  } else {
1179
0
    return VPX_CODEC_INVALID_PARAM;
1180
0
  }
1181
0
}
1182
1183
static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
1184
0
                                          va_list args) {
1185
0
#if CONFIG_POSTPROC
1186
0
  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1187
1188
0
  if (data) {
1189
0
    ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1190
0
    return VPX_CODEC_OK;
1191
0
  } else {
1192
0
    return VPX_CODEC_INVALID_PARAM;
1193
0
  }
1194
#else
1195
  (void)ctx;
1196
  (void)args;
1197
  return VPX_CODEC_INCAPABLE;
1198
#endif
1199
0
}
1200
1201
0
static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx) {
1202
0
  YV12_BUFFER_CONFIG sd;
1203
0
  vp8_ppflags_t flags;
1204
0
  vp8_zero(flags);
1205
1206
0
  if (ctx->preview_ppcfg.post_proc_flag) {
1207
0
    flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1208
0
    flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1209
0
    flags.noise_level = ctx->preview_ppcfg.noise_level;
1210
0
  }
1211
1212
0
  if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags)) {
1213
    /*
1214
    vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
1215
        sd.y_width + 2*VP8BORDERINPIXELS,
1216
        sd.y_height + 2*VP8BORDERINPIXELS,
1217
        1,
1218
        sd.buffer_alloc);
1219
    vpx_img_set_rect(&ctx->preview_img,
1220
        VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1221
        sd.y_width, sd.y_height);
1222
        */
1223
1224
0
    ctx->preview_img.bps = 12;
1225
0
    ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1226
0
    ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1227
0
    ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
1228
1229
0
    ctx->preview_img.fmt = VPX_IMG_FMT_I420;
1230
0
    ctx->preview_img.x_chroma_shift = 1;
1231
0
    ctx->preview_img.y_chroma_shift = 1;
1232
1233
0
    ctx->preview_img.d_w = sd.y_width;
1234
0
    ctx->preview_img.d_h = sd.y_height;
1235
0
    ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1236
0
    ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1237
0
    ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
1238
0
    ctx->preview_img.w = sd.y_width;
1239
0
    ctx->preview_img.h = sd.y_height;
1240
1241
0
    return &ctx->preview_img;
1242
0
  } else {
1243
0
    return NULL;
1244
0
  }
1245
0
}
1246
1247
static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1248
0
                                            va_list args) {
1249
0
  int frame_flags = va_arg(args, int);
1250
0
  ctx->control_frame_flags = frame_flags;
1251
0
  return set_reference_and_update(ctx, frame_flags);
1252
0
}
1253
1254
static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1255
0
                                                  va_list args) {
1256
0
  int layer_id = va_arg(args, int);
1257
0
  if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1258
0
    return VPX_CODEC_INVALID_PARAM;
1259
0
  }
1260
0
  ctx->cpi->temporal_layer_id = layer_id;
1261
0
  return VPX_CODEC_OK;
1262
0
}
1263
1264
static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
1265
0
                                        va_list args) {
1266
0
  vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1267
1268
0
  if (data) {
1269
0
    vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1270
1271
0
    if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols,
1272
0
                        roi->delta_q, roi->delta_lf, roi->static_threshold)) {
1273
0
      return VPX_CODEC_OK;
1274
0
    } else {
1275
0
      return VPX_CODEC_INVALID_PARAM;
1276
0
    }
1277
0
  } else {
1278
0
    return VPX_CODEC_INVALID_PARAM;
1279
0
  }
1280
0
}
1281
1282
static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
1283
0
                                          va_list args) {
1284
0
  vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1285
1286
0
  if (data) {
1287
0
    vpx_active_map_t *map = (vpx_active_map_t *)data;
1288
1289
0
    if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols)) {
1290
0
      return VPX_CODEC_OK;
1291
0
    } else {
1292
0
      return VPX_CODEC_INVALID_PARAM;
1293
0
    }
1294
0
  } else {
1295
0
    return VPX_CODEC_INVALID_PARAM;
1296
0
  }
1297
0
}
1298
1299
static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1300
0
                                          va_list args) {
1301
0
  vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1302
1303
0
  if (data) {
1304
0
    int res;
1305
0
    vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
1306
0
    res = vp8_set_internal_size(ctx->cpi, scalemode.h_scaling_mode,
1307
0
                                scalemode.v_scaling_mode);
1308
1309
0
    if (!res) {
1310
      /*force next frame a key frame to effect scaling mode */
1311
0
      ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1312
0
      return VPX_CODEC_OK;
1313
0
    } else {
1314
0
      return VPX_CODEC_INVALID_PARAM;
1315
0
    }
1316
0
  } else {
1317
0
    return VPX_CODEC_INVALID_PARAM;
1318
0
  }
1319
0
}
1320
1321
static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] = {
1322
  { VP8_SET_REFERENCE, vp8e_set_reference },
1323
  { VP8_COPY_REFERENCE, vp8e_get_reference },
1324
  { VP8_SET_POSTPROC, vp8e_set_previewpp },
1325
  { VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags },
1326
  { VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id },
1327
  { VP8E_SET_ROI_MAP, vp8e_set_roi_map },
1328
  { VP8E_SET_ACTIVEMAP, vp8e_set_activemap },
1329
  { VP8E_SET_SCALEMODE, vp8e_set_scalemode },
1330
  { VP8E_SET_CPUUSED, set_cpu_used },
1331
  { VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity },
1332
  { VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref },
1333
  { VP8E_SET_SHARPNESS, set_sharpness },
1334
  { VP8E_SET_STATIC_THRESHOLD, set_static_thresh },
1335
  { VP8E_SET_TOKEN_PARTITIONS, set_token_partitions },
1336
  { VP8E_GET_LAST_QUANTIZER, get_quantizer },
1337
  { VP8E_GET_LAST_QUANTIZER_64, get_quantizer64 },
1338
  { VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames },
1339
  { VP8E_SET_ARNR_STRENGTH, set_arnr_strength },
1340
  { VP8E_SET_ARNR_TYPE, set_arnr_type },
1341
  { VP8E_SET_TUNING, set_tuning },
1342
  { VP8E_SET_CQ_LEVEL, set_cq_level },
1343
  { VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct },
1344
  { VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode },
1345
  { VP8E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1346
  { VP8E_SET_RTC_EXTERNAL_RATECTRL, ctrl_set_rtc_external_ratectrl },
1347
  { -1, NULL },
1348
};
1349
1350
static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] = {
1351
  { 0,
1352
    {
1353
        0, /* g_usage (unused) */
1354
        0, /* g_threads */
1355
        0, /* g_profile */
1356
1357
        320,        /* g_width */
1358
        240,        /* g_height */
1359
        VPX_BITS_8, /* g_bit_depth */
1360
        8,          /* g_input_bit_depth */
1361
1362
        { 1, 30 }, /* g_timebase */
1363
1364
        0, /* g_error_resilient */
1365
1366
        VPX_RC_ONE_PASS, /* g_pass */
1367
1368
        0, /* g_lag_in_frames */
1369
1370
        0,  /* rc_dropframe_thresh */
1371
        0,  /* rc_resize_allowed */
1372
        1,  /* rc_scaled_width */
1373
        1,  /* rc_scaled_height */
1374
        60, /* rc_resize_down_thresh */
1375
        30, /* rc_resize_up_thresh */
1376
1377
        VPX_VBR,     /* rc_end_usage */
1378
        { NULL, 0 }, /* rc_twopass_stats_in */
1379
        { NULL, 0 }, /* rc_firstpass_mb_stats_in */
1380
        256,         /* rc_target_bitrate */
1381
        4,           /* rc_min_quantizer */
1382
        63,          /* rc_max_quantizer */
1383
        100,         /* rc_undershoot_pct */
1384
        100,         /* rc_overshoot_pct */
1385
1386
        6000, /* rc_max_buffer_size */
1387
        4000, /* rc_buffer_initial_size; */
1388
        5000, /* rc_buffer_optimal_size; */
1389
1390
        50,  /* rc_two_pass_vbrbias  */
1391
        0,   /* rc_two_pass_vbrmin_section */
1392
        400, /* rc_two_pass_vbrmax_section */
1393
        0,   // rc_2pass_vbr_corpus_complexity (only has meaningfull for VP9)
1394
1395
        /* keyframing settings (kf) */
1396
        VPX_KF_AUTO, /* g_kfmode*/
1397
        0,           /* kf_min_dist */
1398
        128,         /* kf_max_dist */
1399
1400
        VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
1401
        { 0 },
1402
        { 0 },    /* ss_target_bitrate */
1403
        1,        /* ts_number_layers */
1404
        { 0 },    /* ts_target_bitrate */
1405
        { 0 },    /* ts_rate_decimator */
1406
        0,        /* ts_periodicity */
1407
        { 0 },    /* ts_layer_id */
1408
        { 0 },    /* layer_target_bitrate */
1409
        0,        /* temporal_layering_mode */
1410
        0,        /* use_vizier_rc_params */
1411
        { 1, 1 }, /* active_wq_factor */
1412
        { 1, 1 }, /* err_per_mb_factor */
1413
        { 1, 1 }, /* sr_default_decay_limit */
1414
        { 1, 1 }, /* sr_diff_factor */
1415
        { 1, 1 }, /* kf_err_per_mb_factor */
1416
        { 1, 1 }, /* kf_frame_min_boost_factor */
1417
        { 1, 1 }, /* kf_frame_max_boost_first_factor */
1418
        { 1, 1 }, /* kf_frame_max_boost_subs_factor */
1419
        { 1, 1 }, /* kf_max_total_boost_factor */
1420
        { 1, 1 }, /* gf_max_total_boost_factor */
1421
        { 1, 1 }, /* gf_frame_max_boost_factor */
1422
        { 1, 1 }, /* zm_factor */
1423
        { 1, 1 }, /* rd_mult_inter_qp_fac */
1424
        { 1, 1 }, /* rd_mult_arf_qp_fac */
1425
        { 1, 1 }, /* rd_mult_key_qp_fac */
1426
    } },
1427
};
1428
1429
#ifndef VERSION_STRING
1430
#define VERSION_STRING
1431
#endif
1432
CODEC_INTERFACE(vpx_codec_vp8_cx) = {
1433
  "WebM Project VP8 Encoder" VERSION_STRING,
1434
  VPX_CODEC_INTERNAL_ABI_VERSION,
1435
  VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR | VPX_CODEC_CAP_OUTPUT_PARTITION,
1436
  /* vpx_codec_caps_t          caps; */
1437
  vp8e_init,     /* vpx_codec_init_fn_t       init; */
1438
  vp8e_destroy,  /* vpx_codec_destroy_fn_t    destroy; */
1439
  vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1440
  {
1441
      NULL, /* vpx_codec_peek_si_fn_t    peek_si; */
1442
      NULL, /* vpx_codec_get_si_fn_t     get_si; */
1443
      NULL, /* vpx_codec_decode_fn_t     decode; */
1444
      NULL, /* vpx_codec_frame_get_fn_t  frame_get; */
1445
      NULL, /* vpx_codec_set_fb_fn_t     set_fb_fn; */
1446
  },
1447
  {
1448
      1,                  /* 1 cfg map */
1449
      vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    cfg_maps; */
1450
      vp8e_encode,        /* vpx_codec_encode_fn_t      encode; */
1451
      vp8e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   get_cx_data; */
1452
      vp8e_set_config,
1453
      NULL,
1454
      vp8e_get_preview,
1455
      vp8e_mr_alloc_mem,
1456
      vp8e_mr_free_mem,
1457
  } /* encoder functions */
1458
};