Coverage Report

Created: 2026-07-25 07:52

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