2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
14 #include "./vpx_config.h"
15 #include "vpx/vpx_encoder.h"
16 #include "vpx_ports/vpx_once.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "./vpx_version.h"
19 #include "vp9/encoder/vp9_encoder.h"
20 #include "vpx/vp8cx.h"
21 #include "vp9/encoder/vp9_firstpass.h"
22 #include "vp9/vp9_iface_common.h"
25 int cpu_used; // available cpu percentage in 1/16
26 unsigned int enable_auto_alt_ref;
27 unsigned int noise_sensitivity;
28 unsigned int sharpness;
29 unsigned int static_thresh;
30 unsigned int tile_columns;
31 unsigned int tile_rows;
32 unsigned int arnr_max_frames;
33 unsigned int arnr_strength;
35 unsigned int cq_level; // constrained quality level
36 unsigned int rc_max_intra_bitrate_pct;
37 unsigned int rc_max_inter_bitrate_pct;
38 unsigned int gf_cbr_boost_pct;
39 unsigned int lossless;
40 unsigned int frame_parallel_decoding_mode;
42 unsigned int frame_periodic_boost;
43 vpx_bit_depth_t bit_depth;
44 vp9e_tune_content content;
45 vpx_color_space_t color_space;
48 static struct vp9_extracfg default_extra_cfg = {
50 1, // enable_auto_alt_ref
51 0, // noise_sensitivity
58 VP8_TUNE_PSNR, // tuning
60 0, // rc_max_intra_bitrate_pct
61 0, // rc_max_inter_bitrate_pct
62 0, // gf_cbr_boost_pct
64 1, // frame_parallel_decoding_mode
66 0, // frame_periodic_delta_q
67 VPX_BITS_8, // Bit depth
68 VP9E_CONTENT_DEFAULT, // content
69 VPX_CS_UNKNOWN, // color space
72 struct vpx_codec_alg_priv {
73 vpx_codec_priv_t base;
74 vpx_codec_enc_cfg_t cfg;
75 struct vp9_extracfg extra_cfg;
76 VP9EncoderConfig oxcf;
78 unsigned char *cx_data;
80 unsigned char *pending_cx_data;
81 size_t pending_cx_data_sz;
82 int pending_frame_count;
83 size_t pending_frame_sizes[8];
84 size_t pending_frame_magnitude;
85 vpx_image_t preview_img;
86 vpx_enc_frame_flags_t next_frame_flags;
87 vp8_postproc_cfg_t preview_ppcfg;
88 vpx_codec_pkt_list_decl(256) pkt_list;
89 unsigned int fixed_kf_cntr;
90 vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
91 // BufferPool that holds all reference frames.
92 BufferPool *buffer_pool;
95 static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
100 return VP9_GOLD_FLAG;
104 assert(0 && "Invalid Reference Frame");
105 return VP9_LAST_FLAG;
108 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
109 const struct vpx_internal_error_info *error) {
110 const vpx_codec_err_t res = error->error_code;
112 if (res != VPX_CODEC_OK)
113 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
120 #define ERROR(str) do {\
121 ctx->base.err_detail = str;\
122 return VPX_CODEC_INVALID_PARAM;\
125 #define RANGE_CHECK(p, memb, lo, hi) do {\
126 if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
127 ERROR(#memb " out of range ["#lo".."#hi"]");\
130 #define RANGE_CHECK_HI(p, memb, hi) do {\
131 if (!((p)->memb <= (hi))) \
132 ERROR(#memb " out of range [.."#hi"]");\
135 #define RANGE_CHECK_LO(p, memb, lo) do {\
136 if (!((p)->memb >= (lo))) \
137 ERROR(#memb " out of range ["#lo"..]");\
140 #define RANGE_CHECK_BOOL(p, memb) do {\
141 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
144 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
145 const vpx_codec_enc_cfg_t *cfg,
146 const struct vp9_extracfg *extra_cfg) {
147 RANGE_CHECK(cfg, g_w, 1, 65535); // 16 bits available
148 RANGE_CHECK(cfg, g_h, 1, 65535); // 16 bits available
149 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
150 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
151 RANGE_CHECK_HI(cfg, g_profile, 3);
153 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
154 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
155 RANGE_CHECK_BOOL(extra_cfg, lossless);
156 RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 1);
157 RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
158 RANGE_CHECK_HI(cfg, g_threads, 64);
159 RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
160 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
161 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
162 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
163 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
164 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
165 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
166 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
167 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
168 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
169 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
171 if (cfg->rc_resize_allowed == 1) {
172 RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
173 RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
176 RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
177 RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
179 if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
180 ERROR("ss_number_layers * ts_number_layers is out of range");
181 if (cfg->ts_number_layers > 1) {
183 for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
184 for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
186 LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
187 if (cfg->layer_target_bitrate[layer] <
188 cfg->layer_target_bitrate[layer - 1])
189 ERROR("ts_target_bitrate entries are not increasing");
193 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
194 for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
195 if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
196 ERROR("ts_rate_decimator factors are not powers of 2");
199 #if CONFIG_SPATIAL_SVC
201 if ((cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) &&
202 cfg->g_pass == VPX_RC_LAST_PASS) {
203 unsigned int i, alt_ref_sum = 0;
204 for (i = 0; i < cfg->ss_number_layers; ++i) {
205 if (cfg->ss_enable_auto_alt_ref[i])
208 if (alt_ref_sum > REF_FRAMES - cfg->ss_number_layers)
209 ERROR("Not enough ref buffers for svc alt ref frames");
210 if (cfg->ss_number_layers * cfg->ts_number_layers > 3 &&
211 cfg->g_error_resilient == 0)
212 ERROR("Multiple frame context are not supported for more than 3 layers");
216 // VP9 does not support a lower bound on the keyframe interval in
217 // automatic keyframe placement mode.
218 if (cfg->kf_mode != VPX_KF_DISABLED &&
219 cfg->kf_min_dist != cfg->kf_max_dist &&
220 cfg->kf_min_dist > 0)
221 ERROR("kf_min_dist not supported in auto mode, use 0 "
222 "or kf_max_dist instead.");
224 RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, 2);
225 RANGE_CHECK(extra_cfg, cpu_used, -8, 8);
226 RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
227 RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
228 RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
229 RANGE_CHECK_HI(extra_cfg, sharpness, 7);
230 RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
231 RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
232 RANGE_CHECK(extra_cfg, cq_level, 0, 63);
233 RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
234 RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
235 RANGE_CHECK(extra_cfg, content,
236 VP9E_CONTENT_DEFAULT, VP9E_CONTENT_INVALID - 1);
238 // TODO(yaowu): remove this when ssim tuning is implemented for vp9
239 if (extra_cfg->tuning == VP8_TUNE_SSIM)
240 ERROR("Option --tune=ssim is not currently supported in VP9.");
242 if (cfg->g_pass == VPX_RC_LAST_PASS) {
243 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
244 const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
245 const FIRSTPASS_STATS *stats;
247 if (cfg->rc_twopass_stats_in.buf == NULL)
248 ERROR("rc_twopass_stats_in.buf not set.");
250 if (cfg->rc_twopass_stats_in.sz % packet_sz)
251 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
253 if (cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) {
255 unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = {0};
257 stats = cfg->rc_twopass_stats_in.buf;
258 for (i = 0; i < n_packets; ++i) {
259 const int layer_id = (int)stats[i].spatial_layer_id;
260 if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
261 ++n_packets_per_layer[layer_id];
265 for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
266 unsigned int layer_id;
267 if (n_packets_per_layer[i] < 2) {
268 ERROR("rc_twopass_stats_in requires at least two packets for each "
272 stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
273 n_packets - cfg->ss_number_layers + i;
274 layer_id = (int)stats->spatial_layer_id;
276 if (layer_id >= cfg->ss_number_layers
277 ||(unsigned int)(stats->count + 0.5) !=
278 n_packets_per_layer[layer_id] - 1)
279 ERROR("rc_twopass_stats_in missing EOS stats packet");
282 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
283 ERROR("rc_twopass_stats_in requires at least two packets.");
286 (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
288 if ((int)(stats->count + 0.5) != n_packets - 1)
289 ERROR("rc_twopass_stats_in missing EOS stats packet");
293 #if !CONFIG_VP9_HIGHBITDEPTH
294 if (cfg->g_profile > (unsigned int)PROFILE_1) {
295 ERROR("Profile > 1 not supported in this build configuration");
298 if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
299 cfg->g_bit_depth > VPX_BITS_8) {
300 ERROR("Codec high bit-depth not supported in profile < 2");
302 if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
303 cfg->g_input_bit_depth > 8) {
304 ERROR("Source high bit-depth not supported in profile < 2");
306 if (cfg->g_profile > (unsigned int)PROFILE_1 &&
307 cfg->g_bit_depth == VPX_BITS_8) {
308 ERROR("Codec bit-depth 8 not supported in profile > 1");
310 RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
314 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
315 const vpx_image_t *img) {
317 case VPX_IMG_FMT_YV12:
318 case VPX_IMG_FMT_I420:
319 case VPX_IMG_FMT_I42016:
321 case VPX_IMG_FMT_I422:
322 case VPX_IMG_FMT_I444:
323 case VPX_IMG_FMT_I440:
324 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
325 ERROR("Invalid image format. I422, I444, I440 images are "
326 "not supported in profile.");
329 case VPX_IMG_FMT_I42216:
330 case VPX_IMG_FMT_I44416:
331 case VPX_IMG_FMT_I44016:
332 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
333 ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
334 ERROR("Invalid image format. 16-bit I422, I444, I440 images are "
335 "not supported in profile.");
339 ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
344 if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
345 ERROR("Image size must match encoder init configuration size");
350 static int get_image_bps(const vpx_image_t *img) {
352 case VPX_IMG_FMT_YV12:
353 case VPX_IMG_FMT_I420: return 12;
354 case VPX_IMG_FMT_I422: return 16;
355 case VPX_IMG_FMT_I444: return 24;
356 case VPX_IMG_FMT_I440: return 16;
357 case VPX_IMG_FMT_I42016: return 24;
358 case VPX_IMG_FMT_I42216: return 32;
359 case VPX_IMG_FMT_I44416: return 48;
360 case VPX_IMG_FMT_I44016: return 32;
361 default: assert(0 && "Invalid image format"); break;
366 static vpx_codec_err_t set_encoder_config(
367 VP9EncoderConfig *oxcf,
368 const vpx_codec_enc_cfg_t *cfg,
369 const struct vp9_extracfg *extra_cfg) {
370 const int is_vbr = cfg->rc_end_usage == VPX_VBR;
372 oxcf->profile = cfg->g_profile;
373 oxcf->max_threads = (int)cfg->g_threads;
374 oxcf->width = cfg->g_w;
375 oxcf->height = cfg->g_h;
376 oxcf->bit_depth = cfg->g_bit_depth;
377 oxcf->input_bit_depth = cfg->g_input_bit_depth;
378 // guess a frame rate if out of whack, use 30
379 oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
380 if (oxcf->init_framerate > 180)
381 oxcf->init_framerate = 30;
385 switch (cfg->g_pass) {
386 case VPX_RC_ONE_PASS:
389 case VPX_RC_FIRST_PASS:
392 case VPX_RC_LAST_PASS:
397 oxcf->lag_in_frames = cfg->g_pass == VPX_RC_FIRST_PASS ? 0
398 : cfg->g_lag_in_frames;
399 oxcf->rc_mode = cfg->rc_end_usage;
401 // Convert target bandwidth from Kbit/s to Bit/s
402 oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
403 oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
404 oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
405 oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
407 oxcf->best_allowed_q =
408 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
409 oxcf->worst_allowed_q =
410 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
411 oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
414 oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
415 oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
417 oxcf->scaled_frame_width = cfg->rc_scaled_width;
418 oxcf->scaled_frame_height = cfg->rc_scaled_height;
419 if (cfg->rc_resize_allowed == 1) {
421 (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0) ?
422 RESIZE_DYNAMIC : RESIZE_FIXED;
424 oxcf->resize_mode = RESIZE_NONE;
427 oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
428 oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
429 oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
431 oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
433 oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
434 oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
435 oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
437 oxcf->auto_key = cfg->kf_mode == VPX_KF_AUTO &&
438 cfg->kf_min_dist != cfg->kf_max_dist;
440 oxcf->key_freq = cfg->kf_max_dist;
442 oxcf->speed = abs(extra_cfg->cpu_used);
443 oxcf->encode_breakout = extra_cfg->static_thresh;
444 oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
445 oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
446 oxcf->sharpness = extra_cfg->sharpness;
448 oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
450 #if CONFIG_FP_MB_STATS
451 oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
454 oxcf->color_space = extra_cfg->color_space;
455 oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
456 oxcf->arnr_strength = extra_cfg->arnr_strength;
458 oxcf->tuning = extra_cfg->tuning;
459 oxcf->content = extra_cfg->content;
461 oxcf->tile_columns = extra_cfg->tile_columns;
462 oxcf->tile_rows = extra_cfg->tile_rows;
464 oxcf->error_resilient_mode = cfg->g_error_resilient;
465 oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
467 oxcf->aq_mode = extra_cfg->aq_mode;
469 oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
471 oxcf->ss_number_layers = cfg->ss_number_layers;
472 oxcf->ts_number_layers = cfg->ts_number_layers;
473 oxcf->temporal_layering_mode = (enum vp9e_temporal_layering_mode)
474 cfg->temporal_layering_mode;
476 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
477 #if CONFIG_SPATIAL_SVC
478 oxcf->ss_enable_auto_arf[sl] = cfg->ss_enable_auto_alt_ref[sl];
480 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
481 oxcf->layer_target_bitrate[sl * oxcf->ts_number_layers + tl] =
482 1000 * cfg->layer_target_bitrate[sl * oxcf->ts_number_layers + tl];
485 if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
486 oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
487 #if CONFIG_SPATIAL_SVC
488 oxcf->ss_enable_auto_arf[0] = extra_cfg->enable_auto_alt_ref;
491 if (oxcf->ts_number_layers > 1) {
492 for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
493 oxcf->ts_rate_decimator[tl] = cfg->ts_rate_decimator[tl] ?
494 cfg->ts_rate_decimator[tl] : 1;
496 } else if (oxcf->ts_number_layers == 1) {
497 oxcf->ts_rate_decimator[0] = 1;
500 printf("Current VP9 Settings: \n");
501 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
502 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
503 printf("sharpness: %d\n", oxcf->sharpness);
504 printf("cpu_used: %d\n", oxcf->cpu_used);
505 printf("Mode: %d\n", oxcf->mode);
506 printf("auto_key: %d\n", oxcf->auto_key);
507 printf("key_freq: %d\n", oxcf->key_freq);
508 printf("end_usage: %d\n", oxcf->end_usage);
509 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
510 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
511 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
512 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
513 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
514 printf("fixed_q: %d\n", oxcf->fixed_q);
515 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
516 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
517 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
518 printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
519 printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
520 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
521 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
522 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
523 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
524 printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
525 printf("Version: %d\n", oxcf->Version);
526 printf("encode_breakout: %d\n", oxcf->encode_breakout);
527 printf("error resilient: %d\n", oxcf->error_resilient_mode);
528 printf("frame parallel detokenization: %d\n",
529 oxcf->frame_parallel_decoding_mode);
534 static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
535 const vpx_codec_enc_cfg_t *cfg) {
539 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
540 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
541 ERROR("Cannot change width or height after initialization");
542 if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
543 (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
544 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
548 // Prevent increasing lag_in_frames. This check is stricter than it needs
549 // to be -- the limit is not increasing past the first lag_in_frames
550 // value, but we don't track the initial config, only the last successful
552 if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
553 ERROR("Cannot increase lag_in_frames");
555 res = validate_config(ctx, cfg, &ctx->extra_cfg);
557 if (res == VPX_CODEC_OK) {
559 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
560 // On profile change, request a key frame
561 force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
562 vp9_change_config(ctx->cpi, &ctx->oxcf);
566 ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
571 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
573 int *const arg = va_arg(args, int *);
575 return VPX_CODEC_INVALID_PARAM;
576 *arg = vp9_get_quantizer(ctx->cpi);
580 static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
582 int *const arg = va_arg(args, int *);
584 return VPX_CODEC_INVALID_PARAM;
585 *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
589 static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
590 const struct vp9_extracfg *extra_cfg) {
591 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
592 if (res == VPX_CODEC_OK) {
593 ctx->extra_cfg = *extra_cfg;
594 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
595 vp9_change_config(ctx->cpi, &ctx->oxcf);
600 static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
602 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
603 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
604 return update_extra_cfg(ctx, &extra_cfg);
607 static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
609 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
610 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
611 return update_extra_cfg(ctx, &extra_cfg);
614 static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
616 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
617 extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
618 return update_extra_cfg(ctx, &extra_cfg);
621 static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
623 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
624 extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
625 return update_extra_cfg(ctx, &extra_cfg);
628 static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
630 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
631 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
632 return update_extra_cfg(ctx, &extra_cfg);
635 static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
637 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
638 extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
639 return update_extra_cfg(ctx, &extra_cfg);
642 static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
644 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
645 extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
646 return update_extra_cfg(ctx, &extra_cfg);
649 static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
651 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
652 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
653 return update_extra_cfg(ctx, &extra_cfg);
656 static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
658 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
659 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
660 return update_extra_cfg(ctx, &extra_cfg);
663 static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
670 static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
672 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
673 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
674 return update_extra_cfg(ctx, &extra_cfg);
677 static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
679 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
680 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
681 return update_extra_cfg(ctx, &extra_cfg);
684 static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
685 vpx_codec_alg_priv_t *ctx, va_list args) {
686 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
687 extra_cfg.rc_max_intra_bitrate_pct =
688 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
689 return update_extra_cfg(ctx, &extra_cfg);
692 static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
693 vpx_codec_alg_priv_t *ctx, va_list args) {
694 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
695 extra_cfg.rc_max_inter_bitrate_pct =
696 CAST(VP8E_SET_MAX_INTER_BITRATE_PCT, args);
697 return update_extra_cfg(ctx, &extra_cfg);
700 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(
701 vpx_codec_alg_priv_t *ctx, va_list args) {
702 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
703 extra_cfg.gf_cbr_boost_pct =
704 CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
705 return update_extra_cfg(ctx, &extra_cfg);
708 static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
710 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
711 extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
712 return update_extra_cfg(ctx, &extra_cfg);
715 static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
716 vpx_codec_alg_priv_t *ctx, va_list args) {
717 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
718 extra_cfg.frame_parallel_decoding_mode =
719 CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
720 return update_extra_cfg(ctx, &extra_cfg);
723 static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
725 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
726 extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
727 return update_extra_cfg(ctx, &extra_cfg);
730 static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
732 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
733 extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
734 return update_extra_cfg(ctx, &extra_cfg);
737 static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
738 vpx_codec_priv_enc_mr_cfg_t *data) {
739 vpx_codec_err_t res = VPX_CODEC_OK;
742 if (ctx->priv == NULL) {
743 vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
745 return VPX_CODEC_MEM_ERROR;
747 ctx->priv = (vpx_codec_priv_t *)priv;
748 ctx->priv->init_flags = ctx->init_flags;
749 ctx->priv->enc.total_encoders = 1;
751 (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
752 if (priv->buffer_pool == NULL)
753 return VPX_CODEC_MEM_ERROR;
755 #if CONFIG_MULTITHREAD
756 if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
757 return VPX_CODEC_MEM_ERROR;
761 if (ctx->config.enc) {
762 // Update the reference to the config structure to an internal copy.
763 priv->cfg = *ctx->config.enc;
764 ctx->config.enc = &priv->cfg;
767 priv->extra_cfg = default_extra_cfg;
768 once(vp9_initialize_enc);
770 res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
772 if (res == VPX_CODEC_OK) {
773 set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
774 #if CONFIG_VP9_HIGHBITDEPTH
775 priv->oxcf.use_highbitdepth =
776 (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
778 priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
779 if (priv->cpi == NULL)
780 res = VPX_CODEC_MEM_ERROR;
782 priv->cpi->output_pkt_list = &priv->pkt_list.head;
789 static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
791 vp9_remove_compressor(ctx->cpi);
792 #if CONFIG_MULTITHREAD
793 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
795 vpx_free(ctx->buffer_pool);
800 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
801 unsigned long duration,
802 unsigned long deadline) {
803 MODE new_mode = BEST;
805 switch (ctx->cfg.g_pass) {
806 case VPX_RC_ONE_PASS:
808 const vpx_codec_enc_cfg_t *const cfg = &ctx->cfg;
810 // Convert duration parameter from stream timebase to microseconds.
811 const uint64_t duration_us = (uint64_t)duration * 1000000 *
812 (uint64_t)cfg->g_timebase.num /(uint64_t)cfg->g_timebase.den;
814 // If the deadline is more that the duration this frame is to be shown,
815 // use good quality mode. Otherwise use realtime mode.
816 new_mode = (deadline > duration_us) ? GOOD : REALTIME;
821 case VPX_RC_FIRST_PASS:
823 case VPX_RC_LAST_PASS:
824 new_mode = deadline > 0 ? GOOD : BEST;
828 if (ctx->oxcf.mode != new_mode) {
829 ctx->oxcf.mode = new_mode;
830 vp9_change_config(ctx->cpi, &ctx->oxcf);
834 // Turn on to test if supplemental superframe data breaks decoding
835 // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
836 static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
837 uint8_t marker = 0xc0;
841 assert(ctx->pending_frame_count);
842 assert(ctx->pending_frame_count <= 8);
844 // Add the number of frames to the marker byte
845 marker |= ctx->pending_frame_count - 1;
847 // Choose the magnitude
848 for (mag = 0, mask = 0xff; mag < 4; mag++) {
849 if (ctx->pending_frame_magnitude < mask)
857 index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
858 if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
859 uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
861 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
862 uint8_t marker_test = 0xc0;
863 int mag_test = 2; // 1 - 4
864 int frames_test = 4; // 1 - 8
865 int index_sz_test = 2 + mag_test * frames_test;
866 marker_test |= frames_test - 1;
867 marker_test |= (mag_test - 1) << 3;
869 for (i = 0; i < mag_test * frames_test; ++i)
870 *x++ = 0; // fill up with arbitrary data
872 ctx->pending_cx_data_sz += index_sz_test;
873 printf("Added supplemental superframe data\n");
877 for (i = 0; i < ctx->pending_frame_count; i++) {
878 unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
880 for (j = 0; j <= mag; j++) {
881 *x++ = this_sz & 0xff;
886 ctx->pending_cx_data_sz += index_sz;
887 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
888 index_sz += index_sz_test;
894 // vp9 uses 10,000,000 ticks/second as time stamp
895 #define TICKS_PER_SEC 10000000LL
897 static int64_t timebase_units_to_ticks(const vpx_rational_t *timebase,
899 return n * TICKS_PER_SEC * timebase->num / timebase->den;
902 static int64_t ticks_to_timebase_units(const vpx_rational_t *timebase,
904 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
905 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
908 static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
909 unsigned int lib_flags) {
910 vpx_codec_frame_flags_t flags = lib_flags << 16;
912 if (lib_flags & FRAMEFLAGS_KEY ||
914 cpi->svc.layer_context[cpi->svc.spatial_layer_id *
915 cpi->svc.number_temporal_layers +
916 cpi->svc.temporal_layer_id].is_key_frame)
918 flags |= VPX_FRAME_IS_KEY;
921 flags |= VPX_FRAME_IS_DROPPABLE;
926 static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t *ctx,
927 const vpx_image_t *img,
929 unsigned long duration,
930 vpx_enc_frame_flags_t flags,
931 unsigned long deadline) {
932 vpx_codec_err_t res = VPX_CODEC_OK;
933 VP9_COMP *const cpi = ctx->cpi;
934 const vpx_rational_t *const timebase = &ctx->cfg.g_timebase;
938 res = validate_img(ctx, img);
939 // TODO(jzern) the checks related to cpi's validity should be treated as a
940 // failure condition, encoder setup is done fully in init() currently.
941 if (res == VPX_CODEC_OK && cpi != NULL) {
942 // There's no codec control for multiple alt-refs so check the encoder
943 // instance for its status to determine the compressed data size.
944 data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
945 (cpi->multi_arf_allowed ? 8 : 2);
948 if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
949 ctx->cx_data_sz = data_sz;
951 ctx->cx_data = (unsigned char*)malloc(ctx->cx_data_sz);
952 if (ctx->cx_data == NULL) {
953 return VPX_CODEC_MEM_ERROR;
959 pick_quickcompress_mode(ctx, duration, deadline);
960 vpx_codec_pkt_list_init(&ctx->pkt_list);
963 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
964 ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
965 ctx->base.err_detail = "Conflicting flags.";
966 return VPX_CODEC_INVALID_PARAM;
969 vp9_apply_encoding_flags(cpi, flags);
971 // Handle fixed keyframe intervals
972 if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
973 ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
974 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
975 flags |= VPX_EFLAG_FORCE_KF;
976 ctx->fixed_kf_cntr = 1;
980 // Initialize the encoder instance on the first frame.
981 if (res == VPX_CODEC_OK && cpi != NULL) {
982 unsigned int lib_flags = 0;
983 YV12_BUFFER_CONFIG sd;
984 int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
985 int64_t dst_end_time_stamp =
986 timebase_units_to_ticks(timebase, pts + duration);
987 size_t size, cx_data_sz;
988 unsigned char *cx_data;
990 // Set up internal flags
991 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
992 cpi->b_calculate_psnr = 1;
995 res = image2yuvconfig(img, &sd);
997 // Store the original flags in to the frame buffer. Will extract the
998 // key frame flag when we actually encode this frame.
999 if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags,
1000 &sd, dst_time_stamp, dst_end_time_stamp)) {
1001 res = update_error_state(ctx, &cpi->common.error);
1003 ctx->next_frame_flags = 0;
1006 cx_data = ctx->cx_data;
1007 cx_data_sz = ctx->cx_data_sz;
1009 /* Any pending invisible frames? */
1010 if (ctx->pending_cx_data) {
1011 memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1012 ctx->pending_cx_data = cx_data;
1013 cx_data += ctx->pending_cx_data_sz;
1014 cx_data_sz -= ctx->pending_cx_data_sz;
1016 /* TODO: this is a minimal check, the underlying codec doesn't respect
1017 * the buffer size anyway.
1019 if (cx_data_sz < ctx->cx_data_sz / 2) {
1020 ctx->base.err_detail = "Compressed data buffer too small";
1021 return VPX_CODEC_ERROR;
1025 while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1026 -1 != vp9_get_compressed_data(cpi, &lib_flags, &size,
1027 cx_data, &dst_time_stamp,
1028 &dst_end_time_stamp, !img)) {
1030 vpx_codec_cx_pkt_t pkt;
1032 #if CONFIG_SPATIAL_SVC
1034 cpi->svc.layer_context[cpi->svc.spatial_layer_id *
1035 cpi->svc.number_temporal_layers].layer_size += size;
1038 // Pack invisible frames with the next visible frame
1039 if (!cpi->common.show_frame ||
1041 cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)
1043 if (ctx->pending_cx_data == 0)
1044 ctx->pending_cx_data = cx_data;
1045 ctx->pending_cx_data_sz += size;
1046 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1047 ctx->pending_frame_magnitude |= size;
1051 if (ctx->output_cx_pkt_cb.output_cx_pkt) {
1052 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1053 pkt.data.frame.pts = ticks_to_timebase_units(timebase,
1055 pkt.data.frame.duration =
1056 (unsigned long)ticks_to_timebase_units(timebase,
1057 dst_end_time_stamp - dst_time_stamp);
1058 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1059 pkt.data.frame.buf = ctx->pending_cx_data;
1060 pkt.data.frame.sz = size;
1061 ctx->pending_cx_data = NULL;
1062 ctx->pending_cx_data_sz = 0;
1063 ctx->pending_frame_count = 0;
1064 ctx->pending_frame_magnitude = 0;
1065 ctx->output_cx_pkt_cb.output_cx_pkt(
1066 &pkt, ctx->output_cx_pkt_cb.user_priv);
1071 // Add the frame packet to the list of returned packets.
1072 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1073 pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
1074 pkt.data.frame.duration =
1075 (unsigned long)ticks_to_timebase_units(timebase,
1076 dst_end_time_stamp - dst_time_stamp);
1077 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1079 if (ctx->pending_cx_data) {
1080 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1081 ctx->pending_frame_magnitude |= size;
1082 ctx->pending_cx_data_sz += size;
1083 // write the superframe only for the case when
1084 if (!ctx->output_cx_pkt_cb.output_cx_pkt)
1085 size += write_superframe_index(ctx);
1086 pkt.data.frame.buf = ctx->pending_cx_data;
1087 pkt.data.frame.sz = ctx->pending_cx_data_sz;
1088 ctx->pending_cx_data = NULL;
1089 ctx->pending_cx_data_sz = 0;
1090 ctx->pending_frame_count = 0;
1091 ctx->pending_frame_magnitude = 0;
1093 pkt.data.frame.buf = cx_data;
1094 pkt.data.frame.sz = size;
1096 pkt.data.frame.partition_id = -1;
1098 if(ctx->output_cx_pkt_cb.output_cx_pkt)
1099 ctx->output_cx_pkt_cb.output_cx_pkt(&pkt,
1100 ctx->output_cx_pkt_cb.user_priv);
1102 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1106 #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
1107 #if CONFIG_SPATIAL_SVC
1108 if (cpi->use_svc && !ctx->output_cx_pkt_cb.output_cx_pkt) {
1109 vpx_codec_cx_pkt_t pkt_sizes, pkt_psnr;
1111 vp9_zero(pkt_sizes);
1113 pkt_sizes.kind = VPX_CODEC_SPATIAL_SVC_LAYER_SIZES;
1114 pkt_psnr.kind = VPX_CODEC_SPATIAL_SVC_LAYER_PSNR;
1115 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1117 &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
1118 pkt_sizes.data.layer_sizes[sl] = lc->layer_size;
1119 pkt_psnr.data.layer_psnr[sl] = lc->psnr_pkt;
1123 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_sizes);
1125 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_psnr);
1129 if (is_one_pass_cbr_svc(cpi) &&
1130 (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
1131 // Encoded all spatial layers; exit loop.
1141 static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1142 vpx_codec_iter_t *iter) {
1143 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1146 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
1148 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1150 if (frame != NULL) {
1151 YV12_BUFFER_CONFIG sd;
1153 image2yuvconfig(&frame->img, &sd);
1154 vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
1156 return VPX_CODEC_OK;
1158 return VPX_CODEC_INVALID_PARAM;
1162 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
1164 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1166 if (frame != NULL) {
1167 YV12_BUFFER_CONFIG sd;
1169 image2yuvconfig(&frame->img, &sd);
1170 vp9_copy_reference_enc(ctx->cpi,
1171 ref_frame_to_vp9_reframe(frame->frame_type), &sd);
1172 return VPX_CODEC_OK;
1174 return VPX_CODEC_INVALID_PARAM;
1178 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
1180 vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
1182 if (frame != NULL) {
1183 YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
1184 if (fb == NULL) return VPX_CODEC_ERROR;
1186 yuvconfig2image(&frame->img, fb, NULL);
1187 return VPX_CODEC_OK;
1189 return VPX_CODEC_INVALID_PARAM;
1193 static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
1195 #if CONFIG_VP9_POSTPROC
1196 vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
1197 if (config != NULL) {
1198 ctx->preview_ppcfg = *config;
1199 return VPX_CODEC_OK;
1201 return VPX_CODEC_INVALID_PARAM;
1206 return VPX_CODEC_INCAPABLE;
1211 static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
1212 YV12_BUFFER_CONFIG sd;
1213 vp9_ppflags_t flags;
1216 if (ctx->preview_ppcfg.post_proc_flag) {
1217 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1218 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1219 flags.noise_level = ctx->preview_ppcfg.noise_level;
1222 if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
1223 yuvconfig2image(&ctx->preview_img, &sd, NULL);
1224 return &ctx->preview_img;
1230 static vpx_codec_err_t ctrl_update_entropy(vpx_codec_alg_priv_t *ctx,
1232 const int update = va_arg(args, int);
1234 vp9_update_entropy(ctx->cpi, update);
1235 return VPX_CODEC_OK;
1238 static vpx_codec_err_t ctrl_update_reference(vpx_codec_alg_priv_t *ctx,
1240 const int ref_frame_flags = va_arg(args, int);
1242 vp9_update_reference(ctx->cpi, ref_frame_flags);
1243 return VPX_CODEC_OK;
1246 static vpx_codec_err_t ctrl_use_reference(vpx_codec_alg_priv_t *ctx,
1248 const int reference_flag = va_arg(args, int);
1250 vp9_use_as_reference(ctx->cpi, reference_flag);
1251 return VPX_CODEC_OK;
1254 static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
1259 // TODO(yaowu): Need to re-implement and test for VP9.
1260 return VPX_CODEC_INVALID_PARAM;
1264 static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
1266 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1269 if (!vp9_set_active_map(ctx->cpi, map->active_map,
1270 (int)map->rows, (int)map->cols))
1271 return VPX_CODEC_OK;
1273 return VPX_CODEC_INVALID_PARAM;
1275 return VPX_CODEC_INVALID_PARAM;
1279 static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
1281 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1284 if (!vp9_get_active_map(ctx->cpi, map->active_map,
1285 (int)map->rows, (int)map->cols))
1286 return VPX_CODEC_OK;
1288 return VPX_CODEC_INVALID_PARAM;
1290 return VPX_CODEC_INVALID_PARAM;
1294 static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
1296 vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
1299 const int res = vp9_set_internal_size(ctx->cpi,
1300 (VPX_SCALING)mode->h_scaling_mode,
1301 (VPX_SCALING)mode->v_scaling_mode);
1302 return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
1304 return VPX_CODEC_INVALID_PARAM;
1308 static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
1309 int data = va_arg(args, int);
1310 const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
1311 // Both one-pass and two-pass RC are supported now.
1312 // User setting this has to make sure of the following.
1313 // In two-pass setting: either (but not both)
1314 // cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1315 // In one-pass setting:
1316 // either or both cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1318 vp9_set_svc(ctx->cpi, data);
1321 (cfg->g_pass == VPX_RC_FIRST_PASS ||
1322 cfg->g_pass == VPX_RC_LAST_PASS) &&
1323 cfg->ss_number_layers > 1 &&
1324 cfg->ts_number_layers > 1) {
1325 return VPX_CODEC_INVALID_PARAM;
1327 return VPX_CODEC_OK;
1330 static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1332 vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
1333 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1334 SVC *const svc = &cpi->svc;
1336 svc->spatial_layer_id = data->spatial_layer_id;
1337 svc->temporal_layer_id = data->temporal_layer_id;
1338 // Checks on valid layer_id input.
1339 if (svc->temporal_layer_id < 0 ||
1340 svc->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
1341 return VPX_CODEC_INVALID_PARAM;
1343 if (svc->spatial_layer_id < 0 ||
1344 svc->spatial_layer_id >= (int)ctx->cfg.ss_number_layers) {
1345 return VPX_CODEC_INVALID_PARAM;
1347 return VPX_CODEC_OK;
1350 static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1352 vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
1353 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1354 SVC *const svc = &cpi->svc;
1356 data->spatial_layer_id = svc->spatial_layer_id;
1357 data->temporal_layer_id = svc->temporal_layer_id;
1359 return VPX_CODEC_OK;
1362 static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1364 VP9_COMP *const cpi = ctx->cpi;
1365 vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
1368 // Number of temporal layers and number of spatial layers have to be set
1369 // properly before calling this control function.
1370 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1371 for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
1373 LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
1375 &cpi->svc.layer_context[layer];
1376 lc->max_q = params->max_quantizers[sl];
1377 lc->min_q = params->min_quantizers[sl];
1378 lc->scaling_factor_num = params->scaling_factor_num[sl];
1379 lc->scaling_factor_den = params->scaling_factor_den[sl];
1383 return VPX_CODEC_OK;
1386 static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
1388 vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
1389 (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
1390 ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
1391 ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
1393 return VPX_CODEC_OK;
1396 static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
1398 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1399 extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
1400 return update_extra_cfg(ctx, &extra_cfg);
1403 static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
1405 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1406 extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
1407 return update_extra_cfg(ctx, &extra_cfg);
1410 static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1411 {VP8_COPY_REFERENCE, ctrl_copy_reference},
1412 {VP8E_UPD_ENTROPY, ctrl_update_entropy},
1413 {VP8E_UPD_REFERENCE, ctrl_update_reference},
1414 {VP8E_USE_REFERENCE, ctrl_use_reference},
1417 {VP8_SET_REFERENCE, ctrl_set_reference},
1418 {VP8_SET_POSTPROC, ctrl_set_previewpp},
1419 {VP8E_SET_ROI_MAP, ctrl_set_roi_map},
1420 {VP8E_SET_ACTIVEMAP, ctrl_set_active_map},
1421 {VP8E_SET_SCALEMODE, ctrl_set_scale_mode},
1422 {VP8E_SET_CPUUSED, ctrl_set_cpuused},
1423 {VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref},
1424 {VP8E_SET_SHARPNESS, ctrl_set_sharpness},
1425 {VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh},
1426 {VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns},
1427 {VP9E_SET_TILE_ROWS, ctrl_set_tile_rows},
1428 {VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames},
1429 {VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength},
1430 {VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type},
1431 {VP8E_SET_TUNING, ctrl_set_tuning},
1432 {VP8E_SET_CQ_LEVEL, ctrl_set_cq_level},
1433 {VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct},
1434 {VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct},
1435 {VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct},
1436 {VP9E_SET_LOSSLESS, ctrl_set_lossless},
1437 {VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode},
1438 {VP9E_SET_AQ_MODE, ctrl_set_aq_mode},
1439 {VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost},
1440 {VP9E_SET_SVC, ctrl_set_svc},
1441 {VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters},
1442 {VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback},
1443 {VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id},
1444 {VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content},
1445 {VP9E_SET_COLOR_SPACE, ctrl_set_color_space},
1446 {VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity},
1449 {VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer},
1450 {VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64},
1451 {VP9_GET_REFERENCE, ctrl_get_reference},
1452 {VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id},
1453 {VP9E_GET_ACTIVEMAP, ctrl_get_active_map},
1458 static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
1468 VPX_BITS_8, // g_bit_depth
1469 8, // g_input_bit_depth
1471 {1, 30}, // g_timebase
1473 0, // g_error_resilient
1475 VPX_RC_ONE_PASS, // g_pass
1477 25, // g_lag_in_frames
1479 0, // rc_dropframe_thresh
1480 0, // rc_resize_allowed
1481 0, // rc_scaled_width
1482 0, // rc_scaled_height
1483 60, // rc_resize_down_thresold
1484 30, // rc_resize_up_thresold
1486 VPX_VBR, // rc_end_usage
1487 {NULL, 0}, // rc_twopass_stats_in
1488 {NULL, 0}, // rc_firstpass_mb_stats_in
1489 256, // rc_target_bandwidth
1490 0, // rc_min_quantizer
1491 63, // rc_max_quantizer
1492 25, // rc_undershoot_pct
1493 25, // rc_overshoot_pct
1495 6000, // rc_max_buffer_size
1496 4000, // rc_buffer_initial_size
1497 5000, // rc_buffer_optimal_size
1499 50, // rc_two_pass_vbrbias
1500 0, // rc_two_pass_vbrmin_section
1501 2000, // rc_two_pass_vbrmax_section
1503 // keyframing settings (kf)
1504 VPX_KF_AUTO, // g_kfmode
1506 9999, // kf_max_dist
1508 VPX_SS_DEFAULT_LAYERS, // ss_number_layers
1510 {0}, // ss_target_bitrate
1511 1, // ts_number_layers
1512 {0}, // ts_target_bitrate
1513 {0}, // ts_rate_decimator
1514 0, // ts_periodicity
1516 {0}, // layer_taget_bitrate
1517 0 // temporal_layering_mode
1522 #ifndef VERSION_STRING
1523 #define VERSION_STRING
1525 CODEC_INTERFACE(vpx_codec_vp9_cx) = {
1526 "WebM Project VP9 Encoder" VERSION_STRING,
1527 VPX_CODEC_INTERNAL_ABI_VERSION,
1528 #if CONFIG_VP9_HIGHBITDEPTH
1529 VPX_CODEC_CAP_HIGHBITDEPTH |
1531 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR, // vpx_codec_caps_t
1532 encoder_init, // vpx_codec_init_fn_t
1533 encoder_destroy, // vpx_codec_destroy_fn_t
1534 encoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1536 NULL, // vpx_codec_peek_si_fn_t
1537 NULL, // vpx_codec_get_si_fn_t
1538 NULL, // vpx_codec_decode_fn_t
1539 NULL, // vpx_codec_frame_get_fn_t
1540 NULL // vpx_codec_set_fb_fn_t
1544 encoder_usage_cfg_map, // vpx_codec_enc_cfg_map_t
1545 encoder_encode, // vpx_codec_encode_fn_t
1546 encoder_get_cxdata, // vpx_codec_get_cx_data_fn_t
1547 encoder_set_config, // vpx_codec_enc_config_set_fn_t
1548 NULL, // vpx_codec_get_global_headers_fn_t
1549 encoder_get_preview, // vpx_codec_get_preview_frame_fn_t
1550 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t