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