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