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