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_ports/vpx_once.h"
17 #include "vpx_ports/system_state.h"
18 #include "vpx/internal/vpx_codec_internal.h"
19 #include "./vpx_version.h"
20 #include "vp9/encoder/vp9_encoder.h"
21 #include "vpx/vp8cx.h"
22 #include "vp9/encoder/vp9_firstpass.h"
23 #include "vp9/vp9_iface_common.h"
24
25 struct vp9_extracfg {
26 int cpu_used; // available cpu percentage in 1/16
27 unsigned int enable_auto_alt_ref;
28 unsigned int noise_sensitivity;
29 unsigned int sharpness;
30 unsigned int static_thresh;
31 unsigned int tile_columns;
32 unsigned int tile_rows;
33 unsigned int arnr_max_frames;
34 unsigned int arnr_strength;
35 unsigned int min_gf_interval;
36 unsigned int max_gf_interval;
37 vp8e_tuning tuning;
38 unsigned int cq_level; // constrained quality level
39 unsigned int rc_max_intra_bitrate_pct;
40 unsigned int rc_max_inter_bitrate_pct;
41 unsigned int gf_cbr_boost_pct;
42 unsigned int lossless;
43 unsigned int target_level;
44 unsigned int frame_parallel_decoding_mode;
45 AQ_MODE aq_mode;
46 int alt_ref_aq;
47 unsigned int frame_periodic_boost;
48 vpx_bit_depth_t bit_depth;
49 vp9e_tune_content content;
50 vpx_color_space_t color_space;
51 vpx_color_range_t color_range;
52 int render_width;
53 int render_height;
54 };
55
56 static struct vp9_extracfg default_extra_cfg = {
57 0, // cpu_used
58 1, // enable_auto_alt_ref
59 0, // noise_sensitivity
60 0, // sharpness
61 0, // static_thresh
62 6, // tile_columns
63 0, // tile_rows
64 7, // arnr_max_frames
65 5, // arnr_strength
66 0, // min_gf_interval; 0 -> default decision
67 0, // max_gf_interval; 0 -> default decision
68 VP8_TUNE_PSNR, // tuning
69 10, // cq_level
70 0, // rc_max_intra_bitrate_pct
71 0, // rc_max_inter_bitrate_pct
72 0, // gf_cbr_boost_pct
73 0, // lossless
74 255, // target_level
75 1, // frame_parallel_decoding_mode
76 NO_AQ, // aq_mode
77 0, // alt_ref_aq
78 0, // frame_periodic_delta_q
79 VPX_BITS_8, // Bit depth
80 VP9E_CONTENT_DEFAULT, // content
81 VPX_CS_UNKNOWN, // color space
82 0, // color range
83 0, // render width
84 0, // render height
85 };
86
87 struct vpx_codec_alg_priv {
88 vpx_codec_priv_t base;
89 vpx_codec_enc_cfg_t cfg;
90 struct vp9_extracfg extra_cfg;
91 VP9EncoderConfig oxcf;
92 VP9_COMP *cpi;
93 unsigned char *cx_data;
94 size_t cx_data_sz;
95 unsigned char *pending_cx_data;
96 size_t pending_cx_data_sz;
97 int pending_frame_count;
98 size_t pending_frame_sizes[8];
99 size_t pending_frame_magnitude;
100 vpx_image_t preview_img;
101 vpx_enc_frame_flags_t next_frame_flags;
102 vp8_postproc_cfg_t preview_ppcfg;
103 vpx_codec_pkt_list_decl(256) pkt_list;
104 unsigned int fixed_kf_cntr;
105 vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
106 // BufferPool that holds all reference frames.
107 BufferPool *buffer_pool;
108 };
109
update_error_state(vpx_codec_alg_priv_t * ctx,const struct vpx_internal_error_info * error)110 static vpx_codec_err_t update_error_state(
111 vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
112 const vpx_codec_err_t res = error->error_code;
113
114 if (res != VPX_CODEC_OK)
115 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
116
117 return res;
118 }
119
120 #undef ERROR
121 #define ERROR(str) \
122 do { \
123 ctx->base.err_detail = str; \
124 return VPX_CODEC_INVALID_PARAM; \
125 } while (0)
126
127 #define RANGE_CHECK(p, memb, lo, hi) \
128 do { \
129 if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
130 ERROR(#memb " out of range [" #lo ".." #hi "]"); \
131 } while (0)
132
133 #define RANGE_CHECK_HI(p, memb, hi) \
134 do { \
135 if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
136 } while (0)
137
138 #define RANGE_CHECK_LO(p, memb, lo) \
139 do { \
140 if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
141 } while (0)
142
143 #define RANGE_CHECK_BOOL(p, memb) \
144 do { \
145 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
146 } while (0)
147
validate_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg,const struct vp9_extracfg * extra_cfg)148 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
149 const vpx_codec_enc_cfg_t *cfg,
150 const struct vp9_extracfg *extra_cfg) {
151 RANGE_CHECK(cfg, g_w, 1, 65535); // 16 bits available
152 RANGE_CHECK(cfg, g_h, 1, 65535); // 16 bits available
153 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
154 RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
155 RANGE_CHECK_HI(cfg, g_profile, 3);
156
157 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
158 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
159 RANGE_CHECK_BOOL(extra_cfg, lossless);
160 RANGE_CHECK_BOOL(extra_cfg, frame_parallel_decoding_mode);
161 RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 2);
162 RANGE_CHECK(extra_cfg, alt_ref_aq, 0, 1);
163 RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
164 RANGE_CHECK_HI(cfg, g_threads, 64);
165 RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
166 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
167 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
168 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
169 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
170 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
171 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
172 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
173 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
174 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
175 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
176 RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
177 RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
178 if (extra_cfg->max_gf_interval > 0) {
179 RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
180 }
181 if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
182 RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
183 (MAX_LAG_BUFFERS - 1));
184 }
185
186 if (cfg->rc_resize_allowed == 1) {
187 RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
188 RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
189 }
190
191 RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
192 RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
193
194 {
195 unsigned int level = extra_cfg->target_level;
196 if (level != LEVEL_1 && level != LEVEL_1_1 && level != LEVEL_2 &&
197 level != LEVEL_2_1 && level != LEVEL_3 && level != LEVEL_3_1 &&
198 level != LEVEL_4 && level != LEVEL_4_1 && level != LEVEL_5 &&
199 level != LEVEL_5_1 && level != LEVEL_5_2 && level != LEVEL_6 &&
200 level != LEVEL_6_1 && level != LEVEL_6_2 && level != LEVEL_UNKNOWN &&
201 level != LEVEL_MAX)
202 ERROR("target_level is invalid");
203 }
204
205 if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
206 ERROR("ss_number_layers * ts_number_layers is out of range");
207 if (cfg->ts_number_layers > 1) {
208 unsigned int sl, tl;
209 for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
210 for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
211 const int layer = LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
212 if (cfg->layer_target_bitrate[layer] <
213 cfg->layer_target_bitrate[layer - 1])
214 ERROR("ts_target_bitrate entries are not increasing");
215 }
216 }
217
218 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
219 for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
220 if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
221 ERROR("ts_rate_decimator factors are not powers of 2");
222 }
223
224 #if CONFIG_SPATIAL_SVC
225
226 if ((cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) &&
227 cfg->g_pass == VPX_RC_LAST_PASS) {
228 unsigned int i, alt_ref_sum = 0;
229 for (i = 0; i < cfg->ss_number_layers; ++i) {
230 if (cfg->ss_enable_auto_alt_ref[i]) ++alt_ref_sum;
231 }
232 if (alt_ref_sum > REF_FRAMES - cfg->ss_number_layers)
233 ERROR("Not enough ref buffers for svc alt ref frames");
234 if (cfg->ss_number_layers * cfg->ts_number_layers > 3 &&
235 cfg->g_error_resilient == 0)
236 ERROR("Multiple frame context are not supported for more than 3 layers");
237 }
238 #endif
239
240 // VP9 does not support a lower bound on the keyframe interval in
241 // automatic keyframe placement mode.
242 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
243 cfg->kf_min_dist > 0)
244 ERROR(
245 "kf_min_dist not supported in auto mode, use 0 "
246 "or kf_max_dist instead.");
247
248 RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, 2);
249 RANGE_CHECK(extra_cfg, cpu_used, -8, 8);
250 RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
251 RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
252 RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
253 RANGE_CHECK_HI(extra_cfg, sharpness, 7);
254 RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
255 RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
256 RANGE_CHECK(extra_cfg, cq_level, 0, 63);
257 RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
258 RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
259 RANGE_CHECK(extra_cfg, content, VP9E_CONTENT_DEFAULT,
260 VP9E_CONTENT_INVALID - 1);
261
262 // TODO(yaowu): remove this when ssim tuning is implemented for vp9
263 if (extra_cfg->tuning == VP8_TUNE_SSIM)
264 ERROR("Option --tune=ssim is not currently supported in VP9.");
265
266 if (cfg->g_pass == VPX_RC_LAST_PASS) {
267 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
268 const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
269 const FIRSTPASS_STATS *stats;
270
271 if (cfg->rc_twopass_stats_in.buf == NULL)
272 ERROR("rc_twopass_stats_in.buf not set.");
273
274 if (cfg->rc_twopass_stats_in.sz % packet_sz)
275 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
276
277 if (cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) {
278 int i;
279 unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = { 0 };
280
281 stats = cfg->rc_twopass_stats_in.buf;
282 for (i = 0; i < n_packets; ++i) {
283 const int layer_id = (int)stats[i].spatial_layer_id;
284 if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
285 ++n_packets_per_layer[layer_id];
286 }
287 }
288
289 for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
290 unsigned int layer_id;
291 if (n_packets_per_layer[i] < 2) {
292 ERROR(
293 "rc_twopass_stats_in requires at least two packets for each "
294 "layer.");
295 }
296
297 stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
298 n_packets - cfg->ss_number_layers + i;
299 layer_id = (int)stats->spatial_layer_id;
300
301 if (layer_id >= cfg->ss_number_layers ||
302 (unsigned int)(stats->count + 0.5) !=
303 n_packets_per_layer[layer_id] - 1)
304 ERROR("rc_twopass_stats_in missing EOS stats packet");
305 }
306 } else {
307 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
308 ERROR("rc_twopass_stats_in requires at least two packets.");
309
310 stats =
311 (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
312
313 if ((int)(stats->count + 0.5) != n_packets - 1)
314 ERROR("rc_twopass_stats_in missing EOS stats packet");
315 }
316 }
317
318 #if !CONFIG_VP9_HIGHBITDEPTH
319 if (cfg->g_profile > (unsigned int)PROFILE_1) {
320 ERROR("Profile > 1 not supported in this build configuration");
321 }
322 #endif
323 if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
324 cfg->g_bit_depth > VPX_BITS_8) {
325 ERROR("Codec high bit-depth not supported in profile < 2");
326 }
327 if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
328 ERROR("Source high bit-depth not supported in profile < 2");
329 }
330 if (cfg->g_profile > (unsigned int)PROFILE_1 &&
331 cfg->g_bit_depth == VPX_BITS_8) {
332 ERROR("Codec bit-depth 8 not supported in profile > 1");
333 }
334 RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
335 RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
336 return VPX_CODEC_OK;
337 }
338
validate_img(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img)339 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
340 const vpx_image_t *img) {
341 switch (img->fmt) {
342 case VPX_IMG_FMT_YV12:
343 case VPX_IMG_FMT_I420:
344 case VPX_IMG_FMT_I42016: break;
345 case VPX_IMG_FMT_I422:
346 case VPX_IMG_FMT_I444:
347 case VPX_IMG_FMT_I440:
348 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
349 ERROR(
350 "Invalid image format. I422, I444, I440 images are "
351 "not supported in profile.");
352 }
353 break;
354 case VPX_IMG_FMT_I42216:
355 case VPX_IMG_FMT_I44416:
356 case VPX_IMG_FMT_I44016:
357 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
358 ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
359 ERROR(
360 "Invalid image format. 16-bit I422, I444, I440 images are "
361 "not supported in profile.");
362 }
363 break;
364 default:
365 ERROR(
366 "Invalid image format. Only YV12, I420, I422, I444 images are "
367 "supported.");
368 break;
369 }
370
371 if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
372 ERROR("Image size must match encoder init configuration size");
373
374 return VPX_CODEC_OK;
375 }
376
get_image_bps(const vpx_image_t * img)377 static int get_image_bps(const vpx_image_t *img) {
378 switch (img->fmt) {
379 case VPX_IMG_FMT_YV12:
380 case VPX_IMG_FMT_I420: return 12;
381 case VPX_IMG_FMT_I422: return 16;
382 case VPX_IMG_FMT_I444: return 24;
383 case VPX_IMG_FMT_I440: return 16;
384 case VPX_IMG_FMT_I42016: return 24;
385 case VPX_IMG_FMT_I42216: return 32;
386 case VPX_IMG_FMT_I44416: return 48;
387 case VPX_IMG_FMT_I44016: return 32;
388 default: assert(0 && "Invalid image format"); break;
389 }
390 return 0;
391 }
392
393 // Modify the encoder config for the target level.
config_target_level(VP9EncoderConfig * oxcf)394 static void config_target_level(VP9EncoderConfig *oxcf) {
395 double max_average_bitrate; // in bits per second
396 int max_over_shoot_pct;
397 const int target_level_index = get_level_index(oxcf->target_level);
398
399 vpx_clear_system_state();
400 assert(target_level_index >= 0);
401 assert(target_level_index < VP9_LEVELS);
402
403 // Maximum target bit-rate is level_limit * 80%.
404 max_average_bitrate =
405 vp9_level_defs[target_level_index].average_bitrate * 800.0;
406 if ((double)oxcf->target_bandwidth > max_average_bitrate)
407 oxcf->target_bandwidth = (int64_t)(max_average_bitrate);
408 if (oxcf->ss_number_layers == 1 && oxcf->pass != 0)
409 oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
410
411 // Adjust max over-shoot percentage.
412 max_over_shoot_pct =
413 (int)((max_average_bitrate * 1.10 - (double)oxcf->target_bandwidth) *
414 100 / (double)(oxcf->target_bandwidth));
415 if (oxcf->over_shoot_pct > max_over_shoot_pct)
416 oxcf->over_shoot_pct = max_over_shoot_pct;
417
418 // Adjust worst allowed quantizer.
419 oxcf->worst_allowed_q = vp9_quantizer_to_qindex(63);
420
421 // Adjust minimum art-ref distance.
422 if (oxcf->min_gf_interval <
423 (int)vp9_level_defs[target_level_index].min_altref_distance)
424 oxcf->min_gf_interval =
425 (int)vp9_level_defs[target_level_index].min_altref_distance;
426
427 // Adjust maximum column tiles.
428 if (vp9_level_defs[target_level_index].max_col_tiles <
429 (1 << oxcf->tile_columns)) {
430 while (oxcf->tile_columns > 0 &&
431 vp9_level_defs[target_level_index].max_col_tiles <
432 (1 << oxcf->tile_columns))
433 --oxcf->tile_columns;
434 }
435 }
436
set_encoder_config(VP9EncoderConfig * oxcf,const vpx_codec_enc_cfg_t * cfg,const struct vp9_extracfg * extra_cfg)437 static vpx_codec_err_t set_encoder_config(
438 VP9EncoderConfig *oxcf, const vpx_codec_enc_cfg_t *cfg,
439 const struct vp9_extracfg *extra_cfg) {
440 const int is_vbr = cfg->rc_end_usage == VPX_VBR;
441 int sl, tl;
442 oxcf->profile = cfg->g_profile;
443 oxcf->max_threads = (int)cfg->g_threads;
444 oxcf->width = cfg->g_w;
445 oxcf->height = cfg->g_h;
446 oxcf->bit_depth = cfg->g_bit_depth;
447 oxcf->input_bit_depth = cfg->g_input_bit_depth;
448 // guess a frame rate if out of whack, use 30
449 oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
450 if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
451
452 oxcf->mode = GOOD;
453
454 switch (cfg->g_pass) {
455 case VPX_RC_ONE_PASS: oxcf->pass = 0; break;
456 case VPX_RC_FIRST_PASS: oxcf->pass = 1; break;
457 case VPX_RC_LAST_PASS: oxcf->pass = 2; break;
458 }
459
460 oxcf->lag_in_frames =
461 cfg->g_pass == VPX_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
462 oxcf->rc_mode = cfg->rc_end_usage;
463
464 // Convert target bandwidth from Kbit/s to Bit/s
465 oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
466 oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
467 oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
468 oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
469
470 oxcf->best_allowed_q =
471 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
472 oxcf->worst_allowed_q =
473 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
474 oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
475 oxcf->fixed_q = -1;
476
477 oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
478 oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
479
480 oxcf->scaled_frame_width = cfg->rc_scaled_width;
481 oxcf->scaled_frame_height = cfg->rc_scaled_height;
482 if (cfg->rc_resize_allowed == 1) {
483 oxcf->resize_mode =
484 (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
485 ? RESIZE_DYNAMIC
486 : RESIZE_FIXED;
487 } else {
488 oxcf->resize_mode = RESIZE_NONE;
489 }
490
491 oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
492 oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
493 oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
494
495 oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
496
497 oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
498 oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
499 oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
500
501 oxcf->auto_key =
502 cfg->kf_mode == VPX_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
503
504 oxcf->key_freq = cfg->kf_max_dist;
505
506 oxcf->speed = abs(extra_cfg->cpu_used);
507 oxcf->encode_breakout = extra_cfg->static_thresh;
508 oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
509 oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
510 oxcf->sharpness = extra_cfg->sharpness;
511
512 oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
513
514 #if CONFIG_FP_MB_STATS
515 oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
516 #endif
517
518 oxcf->color_space = extra_cfg->color_space;
519 oxcf->color_range = extra_cfg->color_range;
520 oxcf->render_width = extra_cfg->render_width;
521 oxcf->render_height = extra_cfg->render_height;
522 oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
523 oxcf->arnr_strength = extra_cfg->arnr_strength;
524 oxcf->min_gf_interval = extra_cfg->min_gf_interval;
525 oxcf->max_gf_interval = extra_cfg->max_gf_interval;
526
527 oxcf->tuning = extra_cfg->tuning;
528 oxcf->content = extra_cfg->content;
529
530 oxcf->tile_columns = extra_cfg->tile_columns;
531
532 // TODO(yunqing): The dependencies between row tiles cause error in multi-
533 // threaded encoding. For now, tile_rows is forced to be 0 in this case.
534 // The further fix can be done by adding synchronizations after a tile row
535 // is encoded. But this will hurt multi-threaded encoder performance. So,
536 // it is recommended to use tile-rows=0 while encoding with threads > 1.
537 if (oxcf->max_threads > 1 && oxcf->tile_columns > 0)
538 oxcf->tile_rows = 0;
539 else
540 oxcf->tile_rows = extra_cfg->tile_rows;
541
542 oxcf->error_resilient_mode = cfg->g_error_resilient;
543 oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
544
545 oxcf->aq_mode = extra_cfg->aq_mode;
546 oxcf->alt_ref_aq = extra_cfg->alt_ref_aq;
547
548 oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
549
550 oxcf->ss_number_layers = cfg->ss_number_layers;
551 oxcf->ts_number_layers = cfg->ts_number_layers;
552 oxcf->temporal_layering_mode =
553 (enum vp9e_temporal_layering_mode)cfg->temporal_layering_mode;
554
555 oxcf->target_level = extra_cfg->target_level;
556
557 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
558 #if CONFIG_SPATIAL_SVC
559 oxcf->ss_enable_auto_arf[sl] = cfg->ss_enable_auto_alt_ref[sl];
560 #endif
561 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
562 oxcf->layer_target_bitrate[sl * oxcf->ts_number_layers + tl] =
563 1000 * cfg->layer_target_bitrate[sl * oxcf->ts_number_layers + tl];
564 }
565 }
566 if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
567 oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
568 #if CONFIG_SPATIAL_SVC
569 oxcf->ss_enable_auto_arf[0] = extra_cfg->enable_auto_alt_ref;
570 #endif
571 }
572 if (oxcf->ts_number_layers > 1) {
573 for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
574 oxcf->ts_rate_decimator[tl] =
575 cfg->ts_rate_decimator[tl] ? cfg->ts_rate_decimator[tl] : 1;
576 }
577 } else if (oxcf->ts_number_layers == 1) {
578 oxcf->ts_rate_decimator[0] = 1;
579 }
580
581 if (get_level_index(oxcf->target_level) >= 0) config_target_level(oxcf);
582 /*
583 printf("Current VP9 Settings: \n");
584 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
585 printf("target_level: %d\n", oxcf->target_level);
586 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
587 printf("sharpness: %d\n", oxcf->sharpness);
588 printf("cpu_used: %d\n", oxcf->cpu_used);
589 printf("Mode: %d\n", oxcf->mode);
590 printf("auto_key: %d\n", oxcf->auto_key);
591 printf("key_freq: %d\n", oxcf->key_freq);
592 printf("end_usage: %d\n", oxcf->end_usage);
593 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
594 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
595 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
596 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
597 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
598 printf("fixed_q: %d\n", oxcf->fixed_q);
599 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
600 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
601 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
602 printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
603 printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
604 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
605 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
606 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
607 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
608 printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
609 printf("Version: %d\n", oxcf->Version);
610 printf("encode_breakout: %d\n", oxcf->encode_breakout);
611 printf("error resilient: %d\n", oxcf->error_resilient_mode);
612 printf("frame parallel detokenization: %d\n",
613 oxcf->frame_parallel_decoding_mode);
614 */
615 return VPX_CODEC_OK;
616 }
617
encoder_set_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg)618 static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
619 const vpx_codec_enc_cfg_t *cfg) {
620 vpx_codec_err_t res;
621 int force_key = 0;
622
623 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
624 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
625 ERROR("Cannot change width or height after initialization");
626 if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
627 (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
628 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
629 force_key = 1;
630 }
631
632 // Prevent increasing lag_in_frames. This check is stricter than it needs
633 // to be -- the limit is not increasing past the first lag_in_frames
634 // value, but we don't track the initial config, only the last successful
635 // config.
636 if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
637 ERROR("Cannot increase lag_in_frames");
638
639 res = validate_config(ctx, cfg, &ctx->extra_cfg);
640
641 if (res == VPX_CODEC_OK) {
642 ctx->cfg = *cfg;
643 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
644 // On profile change, request a key frame
645 force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
646 vp9_change_config(ctx->cpi, &ctx->oxcf);
647 }
648
649 if (force_key) ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
650
651 return res;
652 }
653
ctrl_get_quantizer(vpx_codec_alg_priv_t * ctx,va_list args)654 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
655 va_list args) {
656 int *const arg = va_arg(args, int *);
657 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
658 *arg = vp9_get_quantizer(ctx->cpi);
659 return VPX_CODEC_OK;
660 }
661
ctrl_get_quantizer64(vpx_codec_alg_priv_t * ctx,va_list args)662 static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
663 va_list args) {
664 int *const arg = va_arg(args, int *);
665 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
666 *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
667 return VPX_CODEC_OK;
668 }
669
update_extra_cfg(vpx_codec_alg_priv_t * ctx,const struct vp9_extracfg * extra_cfg)670 static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
671 const struct vp9_extracfg *extra_cfg) {
672 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
673 if (res == VPX_CODEC_OK) {
674 ctx->extra_cfg = *extra_cfg;
675 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
676 vp9_change_config(ctx->cpi, &ctx->oxcf);
677 }
678 return res;
679 }
680
ctrl_set_cpuused(vpx_codec_alg_priv_t * ctx,va_list args)681 static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
682 va_list args) {
683 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
684 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
685 return update_extra_cfg(ctx, &extra_cfg);
686 }
687
ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t * ctx,va_list args)688 static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
689 va_list args) {
690 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
691 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
692 return update_extra_cfg(ctx, &extra_cfg);
693 }
694
ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t * ctx,va_list args)695 static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
696 va_list args) {
697 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
698 extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
699 return update_extra_cfg(ctx, &extra_cfg);
700 }
701
ctrl_set_sharpness(vpx_codec_alg_priv_t * ctx,va_list args)702 static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
703 va_list args) {
704 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
705 extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
706 return update_extra_cfg(ctx, &extra_cfg);
707 }
708
ctrl_set_static_thresh(vpx_codec_alg_priv_t * ctx,va_list args)709 static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
710 va_list args) {
711 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
712 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
713 return update_extra_cfg(ctx, &extra_cfg);
714 }
715
ctrl_set_tile_columns(vpx_codec_alg_priv_t * ctx,va_list args)716 static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
717 va_list args) {
718 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
719 extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
720 return update_extra_cfg(ctx, &extra_cfg);
721 }
722
ctrl_set_tile_rows(vpx_codec_alg_priv_t * ctx,va_list args)723 static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
724 va_list args) {
725 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
726 extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
727 return update_extra_cfg(ctx, &extra_cfg);
728 }
729
ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t * ctx,va_list args)730 static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
731 va_list args) {
732 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
733 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
734 return update_extra_cfg(ctx, &extra_cfg);
735 }
736
ctrl_set_arnr_strength(vpx_codec_alg_priv_t * ctx,va_list args)737 static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
738 va_list args) {
739 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
740 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
741 return update_extra_cfg(ctx, &extra_cfg);
742 }
743
ctrl_set_arnr_type(vpx_codec_alg_priv_t * ctx,va_list args)744 static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
745 va_list args) {
746 (void)ctx;
747 (void)args;
748 return VPX_CODEC_OK;
749 }
750
ctrl_set_tuning(vpx_codec_alg_priv_t * ctx,va_list args)751 static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
752 va_list args) {
753 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
754 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
755 return update_extra_cfg(ctx, &extra_cfg);
756 }
757
ctrl_set_cq_level(vpx_codec_alg_priv_t * ctx,va_list args)758 static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
759 va_list args) {
760 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
761 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
762 return update_extra_cfg(ctx, &extra_cfg);
763 }
764
ctrl_set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t * ctx,va_list args)765 static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
766 vpx_codec_alg_priv_t *ctx, va_list args) {
767 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
768 extra_cfg.rc_max_intra_bitrate_pct =
769 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
770 return update_extra_cfg(ctx, &extra_cfg);
771 }
772
ctrl_set_rc_max_inter_bitrate_pct(vpx_codec_alg_priv_t * ctx,va_list args)773 static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
774 vpx_codec_alg_priv_t *ctx, va_list args) {
775 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
776 extra_cfg.rc_max_inter_bitrate_pct =
777 CAST(VP8E_SET_MAX_INTER_BITRATE_PCT, args);
778 return update_extra_cfg(ctx, &extra_cfg);
779 }
780
ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t * ctx,va_list args)781 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
782 va_list args) {
783 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
784 extra_cfg.gf_cbr_boost_pct = CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
785 return update_extra_cfg(ctx, &extra_cfg);
786 }
787
ctrl_set_lossless(vpx_codec_alg_priv_t * ctx,va_list args)788 static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
789 va_list args) {
790 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
791 extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
792 return update_extra_cfg(ctx, &extra_cfg);
793 }
794
ctrl_set_frame_parallel_decoding_mode(vpx_codec_alg_priv_t * ctx,va_list args)795 static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
796 vpx_codec_alg_priv_t *ctx, va_list args) {
797 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
798 extra_cfg.frame_parallel_decoding_mode =
799 CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
800 return update_extra_cfg(ctx, &extra_cfg);
801 }
802
ctrl_set_aq_mode(vpx_codec_alg_priv_t * ctx,va_list args)803 static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
804 va_list args) {
805 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
806 extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
807 return update_extra_cfg(ctx, &extra_cfg);
808 }
809
ctrl_set_alt_ref_aq(vpx_codec_alg_priv_t * ctx,va_list args)810 static vpx_codec_err_t ctrl_set_alt_ref_aq(vpx_codec_alg_priv_t *ctx,
811 va_list args) {
812 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
813 extra_cfg.alt_ref_aq = CAST(VP9E_SET_ALT_REF_AQ, args);
814 return update_extra_cfg(ctx, &extra_cfg);
815 }
816
ctrl_set_min_gf_interval(vpx_codec_alg_priv_t * ctx,va_list args)817 static vpx_codec_err_t ctrl_set_min_gf_interval(vpx_codec_alg_priv_t *ctx,
818 va_list args) {
819 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
820 extra_cfg.min_gf_interval = CAST(VP9E_SET_MIN_GF_INTERVAL, args);
821 return update_extra_cfg(ctx, &extra_cfg);
822 }
823
ctrl_set_max_gf_interval(vpx_codec_alg_priv_t * ctx,va_list args)824 static vpx_codec_err_t ctrl_set_max_gf_interval(vpx_codec_alg_priv_t *ctx,
825 va_list args) {
826 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
827 extra_cfg.max_gf_interval = CAST(VP9E_SET_MAX_GF_INTERVAL, args);
828 return update_extra_cfg(ctx, &extra_cfg);
829 }
830
ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t * ctx,va_list args)831 static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
832 va_list args) {
833 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
834 extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
835 return update_extra_cfg(ctx, &extra_cfg);
836 }
837
ctrl_set_target_level(vpx_codec_alg_priv_t * ctx,va_list args)838 static vpx_codec_err_t ctrl_set_target_level(vpx_codec_alg_priv_t *ctx,
839 va_list args) {
840 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
841 extra_cfg.target_level = CAST(VP9E_SET_TARGET_LEVEL, args);
842 return update_extra_cfg(ctx, &extra_cfg);
843 }
844
ctrl_get_level(vpx_codec_alg_priv_t * ctx,va_list args)845 static vpx_codec_err_t ctrl_get_level(vpx_codec_alg_priv_t *ctx, va_list args) {
846 int *const arg = va_arg(args, int *);
847 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
848 *arg = (int)vp9_get_level(&ctx->cpi->level_info.level_spec);
849 return VPX_CODEC_OK;
850 }
851
encoder_init(vpx_codec_ctx_t * ctx,vpx_codec_priv_enc_mr_cfg_t * data)852 static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
853 vpx_codec_priv_enc_mr_cfg_t *data) {
854 vpx_codec_err_t res = VPX_CODEC_OK;
855 (void)data;
856
857 if (ctx->priv == NULL) {
858 vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
859 if (priv == NULL) return VPX_CODEC_MEM_ERROR;
860
861 ctx->priv = (vpx_codec_priv_t *)priv;
862 ctx->priv->init_flags = ctx->init_flags;
863 ctx->priv->enc.total_encoders = 1;
864 priv->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
865 if (priv->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
866
867 #if CONFIG_MULTITHREAD
868 if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
869 return VPX_CODEC_MEM_ERROR;
870 }
871 #endif
872
873 if (ctx->config.enc) {
874 // Update the reference to the config structure to an internal copy.
875 priv->cfg = *ctx->config.enc;
876 ctx->config.enc = &priv->cfg;
877 }
878
879 priv->extra_cfg = default_extra_cfg;
880 once(vp9_initialize_enc);
881
882 res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
883
884 if (res == VPX_CODEC_OK) {
885 set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
886 #if CONFIG_VP9_HIGHBITDEPTH
887 priv->oxcf.use_highbitdepth =
888 (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
889 #endif
890 priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
891 if (priv->cpi == NULL)
892 res = VPX_CODEC_MEM_ERROR;
893 else
894 priv->cpi->output_pkt_list = &priv->pkt_list.head;
895 }
896 }
897
898 return res;
899 }
900
encoder_destroy(vpx_codec_alg_priv_t * ctx)901 static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
902 free(ctx->cx_data);
903 vp9_remove_compressor(ctx->cpi);
904 #if CONFIG_MULTITHREAD
905 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
906 #endif
907 vpx_free(ctx->buffer_pool);
908 vpx_free(ctx);
909 return VPX_CODEC_OK;
910 }
911
pick_quickcompress_mode(vpx_codec_alg_priv_t * ctx,unsigned long duration,unsigned long deadline)912 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
913 unsigned long duration,
914 unsigned long deadline) {
915 MODE new_mode = BEST;
916
917 switch (ctx->cfg.g_pass) {
918 case VPX_RC_ONE_PASS:
919 if (deadline > 0) {
920 const vpx_codec_enc_cfg_t *const cfg = &ctx->cfg;
921
922 // Convert duration parameter from stream timebase to microseconds.
923 const uint64_t duration_us = (uint64_t)duration * 1000000 *
924 (uint64_t)cfg->g_timebase.num /
925 (uint64_t)cfg->g_timebase.den;
926
927 // If the deadline is more that the duration this frame is to be shown,
928 // use good quality mode. Otherwise use realtime mode.
929 new_mode = (deadline > duration_us) ? GOOD : REALTIME;
930 } else {
931 new_mode = BEST;
932 }
933 break;
934 case VPX_RC_FIRST_PASS: break;
935 case VPX_RC_LAST_PASS: new_mode = deadline > 0 ? GOOD : BEST; break;
936 }
937
938 if (deadline == VPX_DL_REALTIME) {
939 ctx->oxcf.pass = 0;
940 new_mode = REALTIME;
941 }
942
943 if (ctx->oxcf.mode != new_mode) {
944 ctx->oxcf.mode = new_mode;
945 vp9_change_config(ctx->cpi, &ctx->oxcf);
946 }
947 }
948
949 // Turn on to test if supplemental superframe data breaks decoding
950 // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
write_superframe_index(vpx_codec_alg_priv_t * ctx)951 static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
952 uint8_t marker = 0xc0;
953 unsigned int mask;
954 int mag, index_sz;
955
956 assert(ctx->pending_frame_count);
957 assert(ctx->pending_frame_count <= 8);
958
959 // Add the number of frames to the marker byte
960 marker |= ctx->pending_frame_count - 1;
961
962 // Choose the magnitude
963 for (mag = 0, mask = 0xff; mag < 4; mag++) {
964 if (ctx->pending_frame_magnitude < mask) break;
965 mask <<= 8;
966 mask |= 0xff;
967 }
968 marker |= mag << 3;
969
970 // Write the index
971 index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
972 if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
973 uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
974 int i, j;
975 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
976 uint8_t marker_test = 0xc0;
977 int mag_test = 2; // 1 - 4
978 int frames_test = 4; // 1 - 8
979 int index_sz_test = 2 + mag_test * frames_test;
980 marker_test |= frames_test - 1;
981 marker_test |= (mag_test - 1) << 3;
982 *x++ = marker_test;
983 for (i = 0; i < mag_test * frames_test; ++i)
984 *x++ = 0; // fill up with arbitrary data
985 *x++ = marker_test;
986 ctx->pending_cx_data_sz += index_sz_test;
987 printf("Added supplemental superframe data\n");
988 #endif
989
990 *x++ = marker;
991 for (i = 0; i < ctx->pending_frame_count; i++) {
992 unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
993
994 for (j = 0; j <= mag; j++) {
995 *x++ = this_sz & 0xff;
996 this_sz >>= 8;
997 }
998 }
999 *x++ = marker;
1000 ctx->pending_cx_data_sz += index_sz;
1001 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1002 index_sz += index_sz_test;
1003 #endif
1004 }
1005 return index_sz;
1006 }
1007
timebase_units_to_ticks(const vpx_rational_t * timebase,int64_t n)1008 static int64_t timebase_units_to_ticks(const vpx_rational_t *timebase,
1009 int64_t n) {
1010 return n * TICKS_PER_SEC * timebase->num / timebase->den;
1011 }
1012
ticks_to_timebase_units(const vpx_rational_t * timebase,int64_t n)1013 static int64_t ticks_to_timebase_units(const vpx_rational_t *timebase,
1014 int64_t n) {
1015 const int64_t round = (int64_t)TICKS_PER_SEC * timebase->num / 2 - 1;
1016 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
1017 }
1018
get_frame_pkt_flags(const VP9_COMP * cpi,unsigned int lib_flags)1019 static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
1020 unsigned int lib_flags) {
1021 vpx_codec_frame_flags_t flags = lib_flags << 16;
1022
1023 if (lib_flags & FRAMEFLAGS_KEY ||
1024 (cpi->use_svc &&
1025 cpi->svc
1026 .layer_context[cpi->svc.spatial_layer_id *
1027 cpi->svc.number_temporal_layers +
1028 cpi->svc.temporal_layer_id]
1029 .is_key_frame))
1030 flags |= VPX_FRAME_IS_KEY;
1031
1032 if (cpi->droppable) flags |= VPX_FRAME_IS_DROPPABLE;
1033
1034 return flags;
1035 }
1036
1037 const size_t kMinCompressedSize = 8192;
encoder_encode(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t enc_flags,unsigned long deadline)1038 static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t *ctx,
1039 const vpx_image_t *img,
1040 vpx_codec_pts_t pts,
1041 unsigned long duration,
1042 vpx_enc_frame_flags_t enc_flags,
1043 unsigned long deadline) {
1044 volatile vpx_codec_err_t res = VPX_CODEC_OK;
1045 volatile vpx_enc_frame_flags_t flags = enc_flags;
1046 VP9_COMP *const cpi = ctx->cpi;
1047 const vpx_rational_t *const timebase = &ctx->cfg.g_timebase;
1048 size_t data_sz;
1049
1050 if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
1051
1052 if (cpi->oxcf.pass == 2 && cpi->level_constraint.level_index >= 0 &&
1053 !cpi->level_constraint.rc_config_updated) {
1054 SVC *const svc = &cpi->svc;
1055 const int is_two_pass_svc =
1056 (svc->number_spatial_layers > 1) || (svc->number_temporal_layers > 1);
1057 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1058 TWO_PASS *const twopass = &cpi->twopass;
1059 FIRSTPASS_STATS *stats = &twopass->total_stats;
1060 if (is_two_pass_svc) {
1061 const double frame_rate = 10000000.0 * stats->count / stats->duration;
1062 vp9_update_spatial_layer_framerate(cpi, frame_rate);
1063 twopass->bits_left =
1064 (int64_t)(stats->duration *
1065 svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1066 10000000.0);
1067 } else {
1068 twopass->bits_left =
1069 (int64_t)(stats->duration * oxcf->target_bandwidth / 10000000.0);
1070 }
1071 cpi->level_constraint.rc_config_updated = 1;
1072 }
1073
1074 if (img != NULL) {
1075 res = validate_img(ctx, img);
1076 if (res == VPX_CODEC_OK) {
1077 // There's no codec control for multiple alt-refs so check the encoder
1078 // instance for its status to determine the compressed data size.
1079 data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
1080 (cpi->multi_arf_allowed ? 8 : 2);
1081 if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
1082 if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1083 ctx->cx_data_sz = data_sz;
1084 free(ctx->cx_data);
1085 ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1086 if (ctx->cx_data == NULL) {
1087 return VPX_CODEC_MEM_ERROR;
1088 }
1089 }
1090 }
1091 }
1092
1093 pick_quickcompress_mode(ctx, duration, deadline);
1094 vpx_codec_pkt_list_init(&ctx->pkt_list);
1095
1096 // Handle Flags
1097 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
1098 ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
1099 ctx->base.err_detail = "Conflicting flags.";
1100 return VPX_CODEC_INVALID_PARAM;
1101 }
1102
1103 if (setjmp(cpi->common.error.jmp)) {
1104 cpi->common.error.setjmp = 0;
1105 res = update_error_state(ctx, &cpi->common.error);
1106 vpx_clear_system_state();
1107 return res;
1108 }
1109 cpi->common.error.setjmp = 1;
1110
1111 vp9_apply_encoding_flags(cpi, flags);
1112
1113 // Handle fixed keyframe intervals
1114 if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
1115 ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1116 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1117 flags |= VPX_EFLAG_FORCE_KF;
1118 ctx->fixed_kf_cntr = 1;
1119 }
1120 }
1121
1122 if (res == VPX_CODEC_OK) {
1123 unsigned int lib_flags = 0;
1124 YV12_BUFFER_CONFIG sd;
1125 int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
1126 int64_t dst_end_time_stamp =
1127 timebase_units_to_ticks(timebase, pts + duration);
1128 size_t size, cx_data_sz;
1129 unsigned char *cx_data;
1130
1131 // Set up internal flags
1132 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
1133
1134 if (img != NULL) {
1135 res = image2yuvconfig(img, &sd);
1136
1137 if (sd.y_width != ctx->cfg.g_w || sd.y_height != ctx->cfg.g_h) {
1138 /* from vpx_encoder.h for g_w/g_h:
1139 "Note that the frames passed as input to the encoder must have this
1140 resolution"
1141 */
1142 ctx->base.err_detail = "Invalid input frame resolution";
1143 res = VPX_CODEC_INVALID_PARAM;
1144 } else {
1145 // Store the original flags in to the frame buffer. Will extract the
1146 // key frame flag when we actually encode this frame.
1147 if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1148 dst_time_stamp, dst_end_time_stamp)) {
1149 res = update_error_state(ctx, &cpi->common.error);
1150 }
1151 }
1152 ctx->next_frame_flags = 0;
1153 }
1154
1155 cx_data = ctx->cx_data;
1156 cx_data_sz = ctx->cx_data_sz;
1157
1158 /* Any pending invisible frames? */
1159 if (ctx->pending_cx_data) {
1160 memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1161 ctx->pending_cx_data = cx_data;
1162 cx_data += ctx->pending_cx_data_sz;
1163 cx_data_sz -= ctx->pending_cx_data_sz;
1164
1165 /* TODO: this is a minimal check, the underlying codec doesn't respect
1166 * the buffer size anyway.
1167 */
1168 if (cx_data_sz < ctx->cx_data_sz / 2) {
1169 vpx_internal_error(&cpi->common.error, VPX_CODEC_ERROR,
1170 "Compressed data buffer too small");
1171 return VPX_CODEC_ERROR;
1172 }
1173 }
1174
1175 while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1176 -1 != vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1177 &dst_time_stamp, &dst_end_time_stamp,
1178 !img)) {
1179 if (size) {
1180 vpx_codec_cx_pkt_t pkt;
1181
1182 #if CONFIG_SPATIAL_SVC
1183 if (cpi->use_svc)
1184 cpi->svc
1185 .layer_context[cpi->svc.spatial_layer_id *
1186 cpi->svc.number_temporal_layers]
1187 .layer_size += size;
1188 #endif
1189
1190 // Pack invisible frames with the next visible frame
1191 if (!cpi->common.show_frame ||
1192 (cpi->use_svc &&
1193 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)) {
1194 if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
1195 ctx->pending_cx_data_sz += size;
1196 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1197 ctx->pending_frame_magnitude |= size;
1198 cx_data += size;
1199 cx_data_sz -= size;
1200
1201 if (ctx->output_cx_pkt_cb.output_cx_pkt) {
1202 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1203 pkt.data.frame.pts =
1204 ticks_to_timebase_units(timebase, dst_time_stamp);
1205 pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1206 timebase, dst_end_time_stamp - dst_time_stamp);
1207 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1208 pkt.data.frame.buf = ctx->pending_cx_data;
1209 pkt.data.frame.sz = size;
1210 ctx->pending_cx_data = NULL;
1211 ctx->pending_cx_data_sz = 0;
1212 ctx->pending_frame_count = 0;
1213 ctx->pending_frame_magnitude = 0;
1214 ctx->output_cx_pkt_cb.output_cx_pkt(
1215 &pkt, ctx->output_cx_pkt_cb.user_priv);
1216 }
1217 continue;
1218 }
1219
1220 // Add the frame packet to the list of returned packets.
1221 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1222 pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
1223 pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1224 timebase, dst_end_time_stamp - dst_time_stamp);
1225 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1226
1227 if (ctx->pending_cx_data) {
1228 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1229 ctx->pending_frame_magnitude |= size;
1230 ctx->pending_cx_data_sz += size;
1231 // write the superframe only for the case when
1232 if (!ctx->output_cx_pkt_cb.output_cx_pkt)
1233 size += write_superframe_index(ctx);
1234 pkt.data.frame.buf = ctx->pending_cx_data;
1235 pkt.data.frame.sz = ctx->pending_cx_data_sz;
1236 ctx->pending_cx_data = NULL;
1237 ctx->pending_cx_data_sz = 0;
1238 ctx->pending_frame_count = 0;
1239 ctx->pending_frame_magnitude = 0;
1240 } else {
1241 pkt.data.frame.buf = cx_data;
1242 pkt.data.frame.sz = size;
1243 }
1244 pkt.data.frame.partition_id = -1;
1245
1246 if (ctx->output_cx_pkt_cb.output_cx_pkt)
1247 ctx->output_cx_pkt_cb.output_cx_pkt(&pkt,
1248 ctx->output_cx_pkt_cb.user_priv);
1249 else
1250 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1251
1252 cx_data += size;
1253 cx_data_sz -= size;
1254 #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
1255 #if CONFIG_SPATIAL_SVC
1256 if (cpi->use_svc && !ctx->output_cx_pkt_cb.output_cx_pkt) {
1257 vpx_codec_cx_pkt_t pkt_sizes, pkt_psnr;
1258 int sl;
1259 vp9_zero(pkt_sizes);
1260 vp9_zero(pkt_psnr);
1261 pkt_sizes.kind = VPX_CODEC_SPATIAL_SVC_LAYER_SIZES;
1262 pkt_psnr.kind = VPX_CODEC_SPATIAL_SVC_LAYER_PSNR;
1263 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1264 LAYER_CONTEXT *lc =
1265 &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
1266 pkt_sizes.data.layer_sizes[sl] = lc->layer_size;
1267 pkt_psnr.data.layer_psnr[sl] = lc->psnr_pkt;
1268 lc->layer_size = 0;
1269 }
1270
1271 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_sizes);
1272
1273 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_psnr);
1274 }
1275 #endif
1276 #endif
1277 if (is_one_pass_cbr_svc(cpi) &&
1278 (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
1279 // Encoded all spatial layers; exit loop.
1280 break;
1281 }
1282 }
1283 }
1284 }
1285
1286 cpi->common.error.setjmp = 0;
1287 return res;
1288 }
1289
encoder_get_cxdata(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)1290 static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1291 vpx_codec_iter_t *iter) {
1292 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1293 }
1294
ctrl_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)1295 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
1296 va_list args) {
1297 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1298
1299 if (frame != NULL) {
1300 YV12_BUFFER_CONFIG sd;
1301
1302 image2yuvconfig(&frame->img, &sd);
1303 vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
1304 &sd);
1305 return VPX_CODEC_OK;
1306 } else {
1307 return VPX_CODEC_INVALID_PARAM;
1308 }
1309 }
1310
ctrl_copy_reference(vpx_codec_alg_priv_t * ctx,va_list args)1311 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
1312 va_list args) {
1313 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1314
1315 if (frame != NULL) {
1316 YV12_BUFFER_CONFIG sd;
1317
1318 image2yuvconfig(&frame->img, &sd);
1319 vp9_copy_reference_enc(ctx->cpi,
1320 ref_frame_to_vp9_reframe(frame->frame_type), &sd);
1321 return VPX_CODEC_OK;
1322 } else {
1323 return VPX_CODEC_INVALID_PARAM;
1324 }
1325 }
1326
ctrl_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)1327 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
1328 va_list args) {
1329 vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
1330
1331 if (frame != NULL) {
1332 YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
1333 if (fb == NULL) return VPX_CODEC_ERROR;
1334
1335 yuvconfig2image(&frame->img, fb, NULL);
1336 return VPX_CODEC_OK;
1337 } else {
1338 return VPX_CODEC_INVALID_PARAM;
1339 }
1340 }
1341
ctrl_set_previewpp(vpx_codec_alg_priv_t * ctx,va_list args)1342 static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
1343 va_list args) {
1344 #if CONFIG_VP9_POSTPROC
1345 vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
1346 if (config != NULL) {
1347 ctx->preview_ppcfg = *config;
1348 return VPX_CODEC_OK;
1349 } else {
1350 return VPX_CODEC_INVALID_PARAM;
1351 }
1352 #else
1353 (void)ctx;
1354 (void)args;
1355 return VPX_CODEC_INCAPABLE;
1356 #endif
1357 }
1358
encoder_get_preview(vpx_codec_alg_priv_t * ctx)1359 static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
1360 YV12_BUFFER_CONFIG sd;
1361 vp9_ppflags_t flags;
1362 vp9_zero(flags);
1363
1364 if (ctx->preview_ppcfg.post_proc_flag) {
1365 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1366 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1367 flags.noise_level = ctx->preview_ppcfg.noise_level;
1368 }
1369
1370 if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
1371 yuvconfig2image(&ctx->preview_img, &sd, NULL);
1372 return &ctx->preview_img;
1373 } else {
1374 return NULL;
1375 }
1376 }
1377
ctrl_set_roi_map(vpx_codec_alg_priv_t * ctx,va_list args)1378 static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
1379 va_list args) {
1380 (void)ctx;
1381 (void)args;
1382
1383 // TODO(yaowu): Need to re-implement and test for VP9.
1384 return VPX_CODEC_INVALID_PARAM;
1385 }
1386
ctrl_set_active_map(vpx_codec_alg_priv_t * ctx,va_list args)1387 static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
1388 va_list args) {
1389 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1390
1391 if (map) {
1392 if (!vp9_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
1393 (int)map->cols))
1394 return VPX_CODEC_OK;
1395 else
1396 return VPX_CODEC_INVALID_PARAM;
1397 } else {
1398 return VPX_CODEC_INVALID_PARAM;
1399 }
1400 }
1401
ctrl_get_active_map(vpx_codec_alg_priv_t * ctx,va_list args)1402 static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
1403 va_list args) {
1404 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1405
1406 if (map) {
1407 if (!vp9_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
1408 (int)map->cols))
1409 return VPX_CODEC_OK;
1410 else
1411 return VPX_CODEC_INVALID_PARAM;
1412 } else {
1413 return VPX_CODEC_INVALID_PARAM;
1414 }
1415 }
1416
ctrl_set_scale_mode(vpx_codec_alg_priv_t * ctx,va_list args)1417 static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
1418 va_list args) {
1419 vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
1420
1421 if (mode) {
1422 const int res =
1423 vp9_set_internal_size(ctx->cpi, (VPX_SCALING)mode->h_scaling_mode,
1424 (VPX_SCALING)mode->v_scaling_mode);
1425 return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
1426 } else {
1427 return VPX_CODEC_INVALID_PARAM;
1428 }
1429 }
1430
ctrl_set_svc(vpx_codec_alg_priv_t * ctx,va_list args)1431 static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
1432 int data = va_arg(args, int);
1433 const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
1434 // Both one-pass and two-pass RC are supported now.
1435 // User setting this has to make sure of the following.
1436 // In two-pass setting: either (but not both)
1437 // cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1438 // In one-pass setting:
1439 // either or both cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1440
1441 vp9_set_svc(ctx->cpi, data);
1442
1443 if (data == 1 &&
1444 (cfg->g_pass == VPX_RC_FIRST_PASS || cfg->g_pass == VPX_RC_LAST_PASS) &&
1445 cfg->ss_number_layers > 1 && cfg->ts_number_layers > 1) {
1446 return VPX_CODEC_INVALID_PARAM;
1447 }
1448 return VPX_CODEC_OK;
1449 }
1450
ctrl_set_svc_layer_id(vpx_codec_alg_priv_t * ctx,va_list args)1451 static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1452 va_list args) {
1453 vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
1454 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1455 SVC *const svc = &cpi->svc;
1456
1457 svc->first_spatial_layer_to_encode = data->spatial_layer_id;
1458 svc->spatial_layer_to_encode = data->spatial_layer_id;
1459 svc->temporal_layer_id = data->temporal_layer_id;
1460 // Checks on valid layer_id input.
1461 if (svc->temporal_layer_id < 0 ||
1462 svc->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
1463 return VPX_CODEC_INVALID_PARAM;
1464 }
1465 if (svc->first_spatial_layer_to_encode < 0 ||
1466 svc->first_spatial_layer_to_encode >= (int)ctx->cfg.ss_number_layers) {
1467 return VPX_CODEC_INVALID_PARAM;
1468 }
1469 // First spatial layer to encode not implemented for two-pass.
1470 if (is_two_pass_svc(cpi) && svc->first_spatial_layer_to_encode > 0)
1471 return VPX_CODEC_INVALID_PARAM;
1472 return VPX_CODEC_OK;
1473 }
1474
ctrl_get_svc_layer_id(vpx_codec_alg_priv_t * ctx,va_list args)1475 static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1476 va_list args) {
1477 vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
1478 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1479 SVC *const svc = &cpi->svc;
1480
1481 data->spatial_layer_id = svc->spatial_layer_id;
1482 data->temporal_layer_id = svc->temporal_layer_id;
1483
1484 return VPX_CODEC_OK;
1485 }
1486
ctrl_set_svc_parameters(vpx_codec_alg_priv_t * ctx,va_list args)1487 static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1488 va_list args) {
1489 VP9_COMP *const cpi = ctx->cpi;
1490 vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
1491 int sl, tl;
1492
1493 // Number of temporal layers and number of spatial layers have to be set
1494 // properly before calling this control function.
1495 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1496 for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
1497 const int layer =
1498 LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
1499 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1500 lc->max_q = params->max_quantizers[layer];
1501 lc->min_q = params->min_quantizers[layer];
1502 lc->scaling_factor_num = params->scaling_factor_num[sl];
1503 lc->scaling_factor_den = params->scaling_factor_den[sl];
1504 lc->speed = params->speed_per_layer[sl];
1505 }
1506 }
1507
1508 return VPX_CODEC_OK;
1509 }
1510
ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t * ctx,va_list args)1511 static vpx_codec_err_t ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
1512 va_list args) {
1513 VP9_COMP *const cpi = ctx->cpi;
1514 vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
1515 int sl;
1516 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1517 cpi->svc.ext_frame_flags[sl] = data->frame_flags[sl];
1518 cpi->svc.ext_lst_fb_idx[sl] = data->lst_fb_idx[sl];
1519 cpi->svc.ext_gld_fb_idx[sl] = data->gld_fb_idx[sl];
1520 cpi->svc.ext_alt_fb_idx[sl] = data->alt_fb_idx[sl];
1521 }
1522 return VPX_CODEC_OK;
1523 }
1524
ctrl_register_cx_callback(vpx_codec_alg_priv_t * ctx,va_list args)1525 static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
1526 va_list args) {
1527 vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
1528 (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
1529 ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
1530 ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
1531
1532 return VPX_CODEC_OK;
1533 }
1534
ctrl_set_tune_content(vpx_codec_alg_priv_t * ctx,va_list args)1535 static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
1536 va_list args) {
1537 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1538 extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
1539 return update_extra_cfg(ctx, &extra_cfg);
1540 }
1541
ctrl_set_color_space(vpx_codec_alg_priv_t * ctx,va_list args)1542 static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
1543 va_list args) {
1544 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1545 extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
1546 return update_extra_cfg(ctx, &extra_cfg);
1547 }
1548
ctrl_set_color_range(vpx_codec_alg_priv_t * ctx,va_list args)1549 static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
1550 va_list args) {
1551 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1552 extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
1553 return update_extra_cfg(ctx, &extra_cfg);
1554 }
1555
ctrl_set_render_size(vpx_codec_alg_priv_t * ctx,va_list args)1556 static vpx_codec_err_t ctrl_set_render_size(vpx_codec_alg_priv_t *ctx,
1557 va_list args) {
1558 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1559 int *const render_size = va_arg(args, int *);
1560 extra_cfg.render_width = render_size[0];
1561 extra_cfg.render_height = render_size[1];
1562 return update_extra_cfg(ctx, &extra_cfg);
1563 }
1564
1565 static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1566 { VP8_COPY_REFERENCE, ctrl_copy_reference },
1567
1568 // Setters
1569 { VP8_SET_REFERENCE, ctrl_set_reference },
1570 { VP8_SET_POSTPROC, ctrl_set_previewpp },
1571 { VP8E_SET_ROI_MAP, ctrl_set_roi_map },
1572 { VP8E_SET_ACTIVEMAP, ctrl_set_active_map },
1573 { VP8E_SET_SCALEMODE, ctrl_set_scale_mode },
1574 { VP8E_SET_CPUUSED, ctrl_set_cpuused },
1575 { VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
1576 { VP8E_SET_SHARPNESS, ctrl_set_sharpness },
1577 { VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
1578 { VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
1579 { VP9E_SET_TILE_ROWS, ctrl_set_tile_rows },
1580 { VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
1581 { VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
1582 { VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type },
1583 { VP8E_SET_TUNING, ctrl_set_tuning },
1584 { VP8E_SET_CQ_LEVEL, ctrl_set_cq_level },
1585 { VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
1586 { VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
1587 { VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1588 { VP9E_SET_LOSSLESS, ctrl_set_lossless },
1589 { VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
1590 { VP9E_SET_AQ_MODE, ctrl_set_aq_mode },
1591 { VP9E_SET_ALT_REF_AQ, ctrl_set_alt_ref_aq },
1592 { VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
1593 { VP9E_SET_SVC, ctrl_set_svc },
1594 { VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters },
1595 { VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback },
1596 { VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id },
1597 { VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content },
1598 { VP9E_SET_COLOR_SPACE, ctrl_set_color_space },
1599 { VP9E_SET_COLOR_RANGE, ctrl_set_color_range },
1600 { VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
1601 { VP9E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
1602 { VP9E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
1603 { VP9E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
1604 { VP9E_SET_RENDER_SIZE, ctrl_set_render_size },
1605 { VP9E_SET_TARGET_LEVEL, ctrl_set_target_level },
1606
1607 // Getters
1608 { VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer },
1609 { VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
1610 { VP9_GET_REFERENCE, ctrl_get_reference },
1611 { VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id },
1612 { VP9E_GET_ACTIVEMAP, ctrl_get_active_map },
1613 { VP9E_GET_LEVEL, ctrl_get_level },
1614
1615 { -1, NULL },
1616 };
1617
1618 static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
1619 { 0,
1620 {
1621 // NOLINT
1622 0, // g_usage
1623 8, // g_threads
1624 0, // g_profile
1625
1626 320, // g_width
1627 240, // g_height
1628 VPX_BITS_8, // g_bit_depth
1629 8, // g_input_bit_depth
1630
1631 { 1, 30 }, // g_timebase
1632
1633 0, // g_error_resilient
1634
1635 VPX_RC_ONE_PASS, // g_pass
1636
1637 25, // g_lag_in_frames
1638
1639 0, // rc_dropframe_thresh
1640 0, // rc_resize_allowed
1641 0, // rc_scaled_width
1642 0, // rc_scaled_height
1643 60, // rc_resize_down_thresold
1644 30, // rc_resize_up_thresold
1645
1646 VPX_VBR, // rc_end_usage
1647 { NULL, 0 }, // rc_twopass_stats_in
1648 { NULL, 0 }, // rc_firstpass_mb_stats_in
1649 256, // rc_target_bandwidth
1650 0, // rc_min_quantizer
1651 63, // rc_max_quantizer
1652 25, // rc_undershoot_pct
1653 25, // rc_overshoot_pct
1654
1655 6000, // rc_max_buffer_size
1656 4000, // rc_buffer_initial_size
1657 5000, // rc_buffer_optimal_size
1658
1659 50, // rc_two_pass_vbrbias
1660 0, // rc_two_pass_vbrmin_section
1661 2000, // rc_two_pass_vbrmax_section
1662
1663 // keyframing settings (kf)
1664 VPX_KF_AUTO, // g_kfmode
1665 0, // kf_min_dist
1666 128, // kf_max_dist
1667
1668 VPX_SS_DEFAULT_LAYERS, // ss_number_layers
1669 { 0 },
1670 { 0 }, // ss_target_bitrate
1671 1, // ts_number_layers
1672 { 0 }, // ts_target_bitrate
1673 { 0 }, // ts_rate_decimator
1674 0, // ts_periodicity
1675 { 0 }, // ts_layer_id
1676 { 0 }, // layer_taget_bitrate
1677 0 // temporal_layering_mode
1678 } },
1679 };
1680
1681 #ifndef VERSION_STRING
1682 #define VERSION_STRING
1683 #endif
1684 CODEC_INTERFACE(vpx_codec_vp9_cx) = {
1685 "WebM Project VP9 Encoder" VERSION_STRING,
1686 VPX_CODEC_INTERNAL_ABI_VERSION,
1687 #if CONFIG_VP9_HIGHBITDEPTH
1688 VPX_CODEC_CAP_HIGHBITDEPTH |
1689 #endif
1690 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR, // vpx_codec_caps_t
1691 encoder_init, // vpx_codec_init_fn_t
1692 encoder_destroy, // vpx_codec_destroy_fn_t
1693 encoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1694 {
1695 // NOLINT
1696 NULL, // vpx_codec_peek_si_fn_t
1697 NULL, // vpx_codec_get_si_fn_t
1698 NULL, // vpx_codec_decode_fn_t
1699 NULL, // vpx_codec_frame_get_fn_t
1700 NULL // vpx_codec_set_fb_fn_t
1701 },
1702 {
1703 // NOLINT
1704 1, // 1 cfg map
1705 encoder_usage_cfg_map, // vpx_codec_enc_cfg_map_t
1706 encoder_encode, // vpx_codec_encode_fn_t
1707 encoder_get_cxdata, // vpx_codec_get_cx_data_fn_t
1708 encoder_set_config, // vpx_codec_enc_config_set_fn_t
1709 NULL, // vpx_codec_get_global_headers_fn_t
1710 encoder_get_preview, // vpx_codec_get_preview_frame_fn_t
1711 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t
1712 }
1713 };
1714