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