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_dsp/psnr.h"
17 #include "vpx_ports/vpx_once.h"
18 #include "vpx_ports/static_assert.h"
19 #include "vpx_ports/system_state.h"
20 #include "vpx_util/vpx_timestamp.h"
21 #include "vpx/internal/vpx_codec_internal.h"
22 #include "./vpx_version.h"
23 #include "vp9/encoder/vp9_encoder.h"
24 #include "vpx/vp8cx.h"
25 #include "vp9/common/vp9_alloccommon.h"
26 #include "vp9/vp9_cx_iface.h"
27 #include "vp9/encoder/vp9_firstpass.h"
28 #include "vp9/encoder/vp9_lookahead.h"
29 #include "vp9/vp9_cx_iface.h"
30 #include "vp9/vp9_iface_common.h"
32 typedef struct vp9_extracfg {
33 int cpu_used; // available cpu percentage in 1/16
34 unsigned int enable_auto_alt_ref;
35 unsigned int noise_sensitivity;
36 unsigned int sharpness;
37 unsigned int static_thresh;
38 unsigned int tile_columns;
39 unsigned int tile_rows;
40 unsigned int enable_tpl_model;
41 unsigned int arnr_max_frames;
42 unsigned int arnr_strength;
43 unsigned int min_gf_interval;
44 unsigned int max_gf_interval;
46 unsigned int cq_level; // constrained quality level
47 unsigned int rc_max_intra_bitrate_pct;
48 unsigned int rc_max_inter_bitrate_pct;
49 unsigned int gf_cbr_boost_pct;
50 unsigned int lossless;
51 unsigned int target_level;
52 unsigned int frame_parallel_decoding_mode;
55 unsigned int frame_periodic_boost;
56 vpx_bit_depth_t bit_depth;
57 vp9e_tune_content content;
58 vpx_color_space_t color_space;
59 vpx_color_range_t color_range;
63 unsigned int motion_vector_unit_test;
67 static struct vp9_extracfg default_extra_cfg = {
69 1, // enable_auto_alt_ref
70 0, // noise_sensitivity
75 1, // enable_tpl_model
78 0, // min_gf_interval; 0 -> default decision
79 0, // max_gf_interval; 0 -> default decision
80 VP8_TUNE_PSNR, // tuning
82 0, // rc_max_intra_bitrate_pct
83 0, // rc_max_inter_bitrate_pct
84 0, // gf_cbr_boost_pct
87 1, // frame_parallel_decoding_mode
90 0, // frame_periodic_delta_q
91 VPX_BITS_8, // Bit depth
92 VP9E_CONTENT_DEFAULT, // content
93 VPX_CS_UNKNOWN, // color space
98 0, // motion_vector_unit_test
102 struct vpx_codec_alg_priv {
103 vpx_codec_priv_t base;
104 vpx_codec_enc_cfg_t cfg;
105 struct vp9_extracfg extra_cfg;
106 vpx_rational64_t timestamp_ratio;
107 vpx_codec_pts_t pts_offset;
108 unsigned char pts_offset_initialized;
109 VP9EncoderConfig oxcf;
111 unsigned char *cx_data;
113 unsigned char *pending_cx_data;
114 size_t pending_cx_data_sz;
115 int pending_frame_count;
116 size_t pending_frame_sizes[8];
117 size_t pending_frame_magnitude;
118 vpx_image_t preview_img;
119 vpx_enc_frame_flags_t next_frame_flags;
120 vp8_postproc_cfg_t preview_ppcfg;
121 vpx_codec_pkt_list_decl(256) pkt_list;
122 unsigned int fixed_kf_cntr;
123 vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
124 // BufferPool that holds all reference frames.
125 BufferPool *buffer_pool;
128 static vpx_codec_err_t update_error_state(
129 vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
130 const vpx_codec_err_t res = error->error_code;
132 if (res != VPX_CODEC_OK)
133 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
141 ctx->base.err_detail = str; \
142 return VPX_CODEC_INVALID_PARAM; \
145 #define RANGE_CHECK(p, memb, lo, hi) \
147 if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
148 ERROR(#memb " out of range [" #lo ".." #hi "]"); \
151 #define RANGE_CHECK_HI(p, memb, hi) \
153 if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
156 #define RANGE_CHECK_LO(p, memb, lo) \
158 if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
161 #define RANGE_CHECK_BOOL(p, memb) \
163 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
166 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
167 const vpx_codec_enc_cfg_t *cfg,
168 const struct vp9_extracfg *extra_cfg) {
169 RANGE_CHECK(cfg, g_w, 1, 65535); // 16 bits available
170 RANGE_CHECK(cfg, g_h, 1, 65535); // 16 bits available
171 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
172 RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
173 RANGE_CHECK_HI(cfg, g_profile, 3);
175 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
176 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
177 RANGE_CHECK_BOOL(extra_cfg, lossless);
178 RANGE_CHECK_BOOL(extra_cfg, frame_parallel_decoding_mode);
179 RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 2);
180 RANGE_CHECK(extra_cfg, alt_ref_aq, 0, 1);
181 RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
182 RANGE_CHECK_HI(cfg, g_threads, 64);
183 RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
184 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
185 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
186 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
187 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
188 RANGE_CHECK(cfg, rc_2pass_vbr_corpus_complexity, 0, 10000);
189 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
190 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
191 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
192 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
193 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
194 #if CONFIG_REALTIME_ONLY
195 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
197 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
199 RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
200 RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
201 if (extra_cfg->max_gf_interval > 0) {
202 RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
204 if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
205 RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
206 (MAX_LAG_BUFFERS - 1));
209 // For formation of valid ARF groups lag_in _frames should be 0 or greater
210 // than the max_gf_interval + 2
211 if (cfg->g_lag_in_frames > 0 && extra_cfg->max_gf_interval > 0 &&
212 cfg->g_lag_in_frames < extra_cfg->max_gf_interval + 2) {
213 ERROR("Set lag in frames to 0 (low delay) or >= (max-gf-interval + 2)");
216 if (cfg->rc_resize_allowed == 1) {
217 RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
218 RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
221 RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
222 RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
225 unsigned int level = extra_cfg->target_level;
226 if (level != LEVEL_1 && level != LEVEL_1_1 && level != LEVEL_2 &&
227 level != LEVEL_2_1 && level != LEVEL_3 && level != LEVEL_3_1 &&
228 level != LEVEL_4 && level != LEVEL_4_1 && level != LEVEL_5 &&
229 level != LEVEL_5_1 && level != LEVEL_5_2 && level != LEVEL_6 &&
230 level != LEVEL_6_1 && level != LEVEL_6_2 && level != LEVEL_UNKNOWN &&
231 level != LEVEL_AUTO && level != LEVEL_MAX)
232 ERROR("target_level is invalid");
235 if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
236 ERROR("ss_number_layers * ts_number_layers is out of range");
237 if (cfg->ts_number_layers > 1) {
239 for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
240 for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
241 const int layer = LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
242 if (cfg->layer_target_bitrate[layer] <
243 cfg->layer_target_bitrate[layer - 1])
244 ERROR("ts_target_bitrate entries are not increasing");
248 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
249 for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
250 if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
251 ERROR("ts_rate_decimator factors are not powers of 2");
254 // VP9 does not support a lower bound on the keyframe interval in
255 // automatic keyframe placement mode.
256 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
257 cfg->kf_min_dist > 0)
259 "kf_min_dist not supported in auto mode, use 0 "
260 "or kf_max_dist instead.");
262 RANGE_CHECK(extra_cfg, row_mt, 0, 1);
263 RANGE_CHECK(extra_cfg, motion_vector_unit_test, 0, 2);
264 RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, MAX_ARF_LAYERS);
265 RANGE_CHECK(extra_cfg, cpu_used, -9, 9);
266 RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
267 RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
268 RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
269 RANGE_CHECK_HI(extra_cfg, sharpness, 7);
270 RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
271 RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
272 RANGE_CHECK(extra_cfg, cq_level, 0, 63);
273 RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
274 RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
275 RANGE_CHECK(extra_cfg, content, VP9E_CONTENT_DEFAULT,
276 VP9E_CONTENT_INVALID - 1);
278 #if !CONFIG_REALTIME_ONLY
279 if (cfg->g_pass == VPX_RC_LAST_PASS) {
280 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
281 const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
282 const FIRSTPASS_STATS *stats;
284 if (cfg->rc_twopass_stats_in.buf == NULL)
285 ERROR("rc_twopass_stats_in.buf not set.");
287 if (cfg->rc_twopass_stats_in.sz % packet_sz)
288 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
290 if (cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) {
292 unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = { 0 };
294 stats = cfg->rc_twopass_stats_in.buf;
295 for (i = 0; i < n_packets; ++i) {
296 const int layer_id = (int)stats[i].spatial_layer_id;
297 if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
298 ++n_packets_per_layer[layer_id];
302 for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
303 unsigned int layer_id;
304 if (n_packets_per_layer[i] < 2) {
306 "rc_twopass_stats_in requires at least two packets for each "
310 stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
311 n_packets - cfg->ss_number_layers + i;
312 layer_id = (int)stats->spatial_layer_id;
314 if (layer_id >= cfg->ss_number_layers ||
315 (unsigned int)(stats->count + 0.5) !=
316 n_packets_per_layer[layer_id] - 1)
317 ERROR("rc_twopass_stats_in missing EOS stats packet");
320 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
321 ERROR("rc_twopass_stats_in requires at least two packets.");
324 (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
326 if ((int)(stats->count + 0.5) != n_packets - 1)
327 ERROR("rc_twopass_stats_in missing EOS stats packet");
330 #endif // !CONFIG_REALTIME_ONLY
332 #if !CONFIG_VP9_HIGHBITDEPTH
333 if (cfg->g_profile > (unsigned int)PROFILE_1) {
334 ERROR("Profile > 1 not supported in this build configuration");
337 if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
338 cfg->g_bit_depth > VPX_BITS_8) {
339 ERROR("Codec high bit-depth not supported in profile < 2");
341 if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
342 ERROR("Source high bit-depth not supported in profile < 2");
344 if (cfg->g_profile > (unsigned int)PROFILE_1 &&
345 cfg->g_bit_depth == VPX_BITS_8) {
346 ERROR("Codec bit-depth 8 not supported in profile > 1");
348 RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
349 RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
353 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
354 const vpx_image_t *img) {
356 case VPX_IMG_FMT_YV12:
357 case VPX_IMG_FMT_I420:
358 case VPX_IMG_FMT_I42016:
359 case VPX_IMG_FMT_NV12: break;
360 case VPX_IMG_FMT_I422:
361 case VPX_IMG_FMT_I444:
362 case VPX_IMG_FMT_I440:
363 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
365 "Invalid image format. I422, I444, I440, NV12 images are "
366 "not supported in profile.");
369 case VPX_IMG_FMT_I42216:
370 case VPX_IMG_FMT_I44416:
371 case VPX_IMG_FMT_I44016:
372 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
373 ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
375 "Invalid image format. 16-bit I422, I444, I440 images are "
376 "not supported in profile.");
381 "Invalid image format. Only YV12, I420, I422, I444 images are "
386 if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
387 ERROR("Image size must match encoder init configuration size");
392 static int get_image_bps(const vpx_image_t *img) {
394 case VPX_IMG_FMT_YV12:
395 case VPX_IMG_FMT_NV12:
396 case VPX_IMG_FMT_I420: return 12;
397 case VPX_IMG_FMT_I422: return 16;
398 case VPX_IMG_FMT_I444: return 24;
399 case VPX_IMG_FMT_I440: return 16;
400 case VPX_IMG_FMT_I42016: return 24;
401 case VPX_IMG_FMT_I42216: return 32;
402 case VPX_IMG_FMT_I44416: return 48;
403 case VPX_IMG_FMT_I44016: return 32;
404 default: assert(0 && "Invalid image format"); break;
409 // Modify the encoder config for the target level.
410 static void config_target_level(VP9EncoderConfig *oxcf) {
411 double max_average_bitrate; // in bits per second
412 int max_over_shoot_pct;
413 const int target_level_index = get_level_index(oxcf->target_level);
415 vpx_clear_system_state();
416 assert(target_level_index >= 0);
417 assert(target_level_index < VP9_LEVELS);
419 // Maximum target bit-rate is level_limit * 80%.
420 max_average_bitrate =
421 vp9_level_defs[target_level_index].average_bitrate * 800.0;
422 if ((double)oxcf->target_bandwidth > max_average_bitrate)
423 oxcf->target_bandwidth = (int64_t)(max_average_bitrate);
424 if (oxcf->ss_number_layers == 1 && oxcf->pass != 0)
425 oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
427 // Adjust max over-shoot percentage.
429 (int)((max_average_bitrate * 1.10 - (double)oxcf->target_bandwidth) *
430 100 / (double)(oxcf->target_bandwidth));
431 if (oxcf->over_shoot_pct > max_over_shoot_pct)
432 oxcf->over_shoot_pct = max_over_shoot_pct;
434 // Adjust worst allowed quantizer.
435 oxcf->worst_allowed_q = vp9_quantizer_to_qindex(63);
437 // Adjust minimum art-ref distance.
438 // min_gf_interval should be no less than min_altref_distance + 1,
439 // as the encoder may produce bitstream with alt-ref distance being
440 // min_gf_interval - 1.
441 if (oxcf->min_gf_interval <=
442 (int)vp9_level_defs[target_level_index].min_altref_distance) {
443 oxcf->min_gf_interval =
444 (int)vp9_level_defs[target_level_index].min_altref_distance + 1;
445 // If oxcf->max_gf_interval == 0, it will be assigned with a default value
446 // in vp9_rc_set_gf_interval_range().
447 if (oxcf->max_gf_interval != 0) {
448 oxcf->max_gf_interval =
449 VPXMAX(oxcf->max_gf_interval, oxcf->min_gf_interval);
453 // Adjust maximum column tiles.
454 if (vp9_level_defs[target_level_index].max_col_tiles <
455 (1 << oxcf->tile_columns)) {
456 while (oxcf->tile_columns > 0 &&
457 vp9_level_defs[target_level_index].max_col_tiles <
458 (1 << oxcf->tile_columns))
459 --oxcf->tile_columns;
463 static vpx_rational64_t get_g_timebase_in_ts(vpx_rational_t g_timebase) {
464 vpx_rational64_t g_timebase_in_ts;
465 g_timebase_in_ts.den = g_timebase.den;
466 g_timebase_in_ts.num = g_timebase.num;
467 g_timebase_in_ts.num *= TICKS_PER_SEC;
468 reduce_ratio(&g_timebase_in_ts);
469 return g_timebase_in_ts;
472 static vpx_codec_err_t set_encoder_config(
473 VP9EncoderConfig *oxcf, vpx_codec_enc_cfg_t *cfg,
474 const struct vp9_extracfg *extra_cfg) {
475 const int is_vbr = cfg->rc_end_usage == VPX_VBR;
477 unsigned int raw_target_rate;
478 oxcf->profile = cfg->g_profile;
479 oxcf->max_threads = (int)cfg->g_threads;
480 oxcf->width = cfg->g_w;
481 oxcf->height = cfg->g_h;
482 oxcf->bit_depth = cfg->g_bit_depth;
483 oxcf->input_bit_depth = cfg->g_input_bit_depth;
484 // TODO(angiebird): Figure out if we can just use g_timebase to indicate the
485 // inverse of framerate
486 // guess a frame rate if out of whack, use 30
487 oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
488 if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
489 oxcf->g_timebase = cfg->g_timebase;
490 oxcf->g_timebase_in_ts = get_g_timebase_in_ts(oxcf->g_timebase);
494 switch (cfg->g_pass) {
495 case VPX_RC_ONE_PASS: oxcf->pass = 0; break;
496 case VPX_RC_FIRST_PASS: oxcf->pass = 1; break;
497 case VPX_RC_LAST_PASS: oxcf->pass = 2; break;
500 oxcf->lag_in_frames =
501 cfg->g_pass == VPX_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
502 oxcf->rc_mode = cfg->rc_end_usage;
505 (unsigned int)((int64_t)oxcf->width * oxcf->height * oxcf->bit_depth * 3 *
506 oxcf->init_framerate / 1000);
507 // Cap target bitrate to raw rate
508 cfg->rc_target_bitrate = VPXMIN(raw_target_rate, cfg->rc_target_bitrate);
510 // Convert target bandwidth from Kbit/s to Bit/s
511 oxcf->target_bandwidth = 1000 * (int64_t)cfg->rc_target_bitrate;
512 oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
513 oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
514 oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
516 oxcf->best_allowed_q =
517 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
518 oxcf->worst_allowed_q =
519 extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
520 oxcf->cq_level = vp9_quantizer_to_qindex(extra_cfg->cq_level);
523 oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
524 oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
526 oxcf->scaled_frame_width = cfg->rc_scaled_width;
527 oxcf->scaled_frame_height = cfg->rc_scaled_height;
528 if (cfg->rc_resize_allowed == 1) {
530 (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
534 oxcf->resize_mode = RESIZE_NONE;
537 oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
538 oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
539 oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
541 oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
543 oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
544 oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
545 oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
546 oxcf->vbr_corpus_complexity = cfg->rc_2pass_vbr_corpus_complexity;
549 cfg->kf_mode == VPX_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
551 oxcf->key_freq = cfg->kf_max_dist;
553 oxcf->speed = abs(extra_cfg->cpu_used);
554 oxcf->encode_breakout = extra_cfg->static_thresh;
555 oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
556 if (oxcf->bit_depth == VPX_BITS_8) {
557 oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
559 // Disable denoiser for high bitdepth since vp9_denoiser_filter only works
561 oxcf->noise_sensitivity = 0;
563 oxcf->sharpness = extra_cfg->sharpness;
565 vp9_set_first_pass_stats(oxcf, &cfg->rc_twopass_stats_in);
567 #if CONFIG_FP_MB_STATS
568 oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
571 oxcf->color_space = extra_cfg->color_space;
572 oxcf->color_range = extra_cfg->color_range;
573 oxcf->render_width = extra_cfg->render_width;
574 oxcf->render_height = extra_cfg->render_height;
575 oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
576 oxcf->arnr_strength = extra_cfg->arnr_strength;
577 oxcf->min_gf_interval = extra_cfg->min_gf_interval;
578 oxcf->max_gf_interval = extra_cfg->max_gf_interval;
580 oxcf->tuning = extra_cfg->tuning;
581 oxcf->content = extra_cfg->content;
583 oxcf->tile_columns = extra_cfg->tile_columns;
585 oxcf->enable_tpl_model = extra_cfg->enable_tpl_model;
587 // TODO(yunqing): The dependencies between row tiles cause error in multi-
588 // threaded encoding. For now, tile_rows is forced to be 0 in this case.
589 // The further fix can be done by adding synchronizations after a tile row
590 // is encoded. But this will hurt multi-threaded encoder performance. So,
591 // it is recommended to use tile-rows=0 while encoding with threads > 1.
592 if (oxcf->max_threads > 1 && oxcf->tile_columns > 0)
595 oxcf->tile_rows = extra_cfg->tile_rows;
597 oxcf->error_resilient_mode = cfg->g_error_resilient;
598 oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
600 oxcf->aq_mode = extra_cfg->aq_mode;
601 oxcf->alt_ref_aq = extra_cfg->alt_ref_aq;
603 oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
605 oxcf->ss_number_layers = cfg->ss_number_layers;
606 oxcf->ts_number_layers = cfg->ts_number_layers;
607 oxcf->temporal_layering_mode =
608 (enum vp9e_temporal_layering_mode)cfg->temporal_layering_mode;
610 oxcf->target_level = extra_cfg->target_level;
612 oxcf->row_mt = extra_cfg->row_mt;
613 oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
615 oxcf->delta_q_uv = extra_cfg->delta_q_uv;
617 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
618 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
619 oxcf->layer_target_bitrate[sl * oxcf->ts_number_layers + tl] =
620 1000 * cfg->layer_target_bitrate[sl * oxcf->ts_number_layers + tl];
623 if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
624 oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
626 if (oxcf->ts_number_layers > 1) {
627 for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
628 oxcf->ts_rate_decimator[tl] =
629 cfg->ts_rate_decimator[tl] ? cfg->ts_rate_decimator[tl] : 1;
631 } else if (oxcf->ts_number_layers == 1) {
632 oxcf->ts_rate_decimator[0] = 1;
635 if (get_level_index(oxcf->target_level) >= 0) config_target_level(oxcf);
636 // vp9_dump_encoder_config(oxcf);
640 static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
641 const vpx_codec_enc_cfg_t *cfg) {
645 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
646 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
647 ERROR("Cannot change width or height after initialization");
648 if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
649 (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
650 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
654 // Prevent increasing lag_in_frames. This check is stricter than it needs
655 // to be -- the limit is not increasing past the first lag_in_frames
656 // value, but we don't track the initial config, only the last successful
658 if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
659 ERROR("Cannot increase lag_in_frames");
661 res = validate_config(ctx, cfg, &ctx->extra_cfg);
663 if (res == VPX_CODEC_OK) {
665 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
666 // On profile change, request a key frame
667 force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
668 vp9_change_config(ctx->cpi, &ctx->oxcf);
671 if (force_key) ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
676 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
678 int *const arg = va_arg(args, int *);
679 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
680 *arg = vp9_get_quantizer(ctx->cpi);
684 static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
686 int *const arg = va_arg(args, int *);
687 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
688 *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
692 static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
693 const struct vp9_extracfg *extra_cfg) {
694 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
695 if (res == VPX_CODEC_OK) {
696 ctx->extra_cfg = *extra_cfg;
697 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
698 vp9_change_config(ctx->cpi, &ctx->oxcf);
703 static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
705 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
706 // Use fastest speed setting (speed 9 or -9) if it's set beyond the range.
707 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
708 extra_cfg.cpu_used = VPXMIN(9, extra_cfg.cpu_used);
709 extra_cfg.cpu_used = VPXMAX(-9, extra_cfg.cpu_used);
710 #if CONFIG_REALTIME_ONLY
711 if (extra_cfg.cpu_used > -5 && extra_cfg.cpu_used < 5)
712 extra_cfg.cpu_used = (extra_cfg.cpu_used > 0) ? 5 : -5;
714 return update_extra_cfg(ctx, &extra_cfg);
717 static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
719 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
720 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
721 return update_extra_cfg(ctx, &extra_cfg);
724 static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
726 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
727 extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
728 return update_extra_cfg(ctx, &extra_cfg);
731 static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
733 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
734 extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
735 return update_extra_cfg(ctx, &extra_cfg);
738 static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
740 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
741 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
742 return update_extra_cfg(ctx, &extra_cfg);
745 static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
747 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
748 extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
749 return update_extra_cfg(ctx, &extra_cfg);
752 static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
754 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
755 extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
756 return update_extra_cfg(ctx, &extra_cfg);
759 static vpx_codec_err_t ctrl_set_tpl_model(vpx_codec_alg_priv_t *ctx,
761 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
762 extra_cfg.enable_tpl_model = CAST(VP9E_SET_TPL, args);
763 return update_extra_cfg(ctx, &extra_cfg);
766 static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
768 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
769 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
770 return update_extra_cfg(ctx, &extra_cfg);
773 static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
775 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
776 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
777 return update_extra_cfg(ctx, &extra_cfg);
780 static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
787 static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
789 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
790 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
791 return update_extra_cfg(ctx, &extra_cfg);
794 static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
796 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
797 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
798 return update_extra_cfg(ctx, &extra_cfg);
801 static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
802 vpx_codec_alg_priv_t *ctx, va_list args) {
803 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
804 extra_cfg.rc_max_intra_bitrate_pct =
805 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
806 return update_extra_cfg(ctx, &extra_cfg);
809 static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
810 vpx_codec_alg_priv_t *ctx, va_list args) {
811 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
812 extra_cfg.rc_max_inter_bitrate_pct =
813 CAST(VP9E_SET_MAX_INTER_BITRATE_PCT, args);
814 return update_extra_cfg(ctx, &extra_cfg);
817 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
819 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
820 extra_cfg.gf_cbr_boost_pct = CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
821 return update_extra_cfg(ctx, &extra_cfg);
824 static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
826 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
827 extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
828 return update_extra_cfg(ctx, &extra_cfg);
831 static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
832 vpx_codec_alg_priv_t *ctx, va_list args) {
833 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
834 extra_cfg.frame_parallel_decoding_mode =
835 CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
836 return update_extra_cfg(ctx, &extra_cfg);
839 static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
841 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
842 extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
843 return update_extra_cfg(ctx, &extra_cfg);
846 static vpx_codec_err_t ctrl_set_alt_ref_aq(vpx_codec_alg_priv_t *ctx,
848 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
849 extra_cfg.alt_ref_aq = CAST(VP9E_SET_ALT_REF_AQ, args);
850 return update_extra_cfg(ctx, &extra_cfg);
853 static vpx_codec_err_t ctrl_set_min_gf_interval(vpx_codec_alg_priv_t *ctx,
855 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
856 extra_cfg.min_gf_interval = CAST(VP9E_SET_MIN_GF_INTERVAL, args);
857 return update_extra_cfg(ctx, &extra_cfg);
860 static vpx_codec_err_t ctrl_set_max_gf_interval(vpx_codec_alg_priv_t *ctx,
862 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
863 extra_cfg.max_gf_interval = CAST(VP9E_SET_MAX_GF_INTERVAL, args);
864 return update_extra_cfg(ctx, &extra_cfg);
867 static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
869 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
870 extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
871 return update_extra_cfg(ctx, &extra_cfg);
874 static vpx_codec_err_t ctrl_set_target_level(vpx_codec_alg_priv_t *ctx,
876 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
877 extra_cfg.target_level = CAST(VP9E_SET_TARGET_LEVEL, args);
878 return update_extra_cfg(ctx, &extra_cfg);
881 static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
883 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
884 extra_cfg.row_mt = CAST(VP9E_SET_ROW_MT, args);
885 return update_extra_cfg(ctx, &extra_cfg);
888 static vpx_codec_err_t ctrl_enable_motion_vector_unit_test(
889 vpx_codec_alg_priv_t *ctx, va_list args) {
890 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
891 extra_cfg.motion_vector_unit_test =
892 CAST(VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
893 return update_extra_cfg(ctx, &extra_cfg);
896 static vpx_codec_err_t ctrl_get_level(vpx_codec_alg_priv_t *ctx, va_list args) {
897 int *const arg = va_arg(args, int *);
898 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
899 *arg = (int)vp9_get_level(&ctx->cpi->level_info.level_spec);
903 static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
904 vpx_codec_priv_enc_mr_cfg_t *data) {
905 vpx_codec_err_t res = VPX_CODEC_OK;
908 if (ctx->priv == NULL) {
909 vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
910 if (priv == NULL) return VPX_CODEC_MEM_ERROR;
912 ctx->priv = (vpx_codec_priv_t *)priv;
913 ctx->priv->init_flags = ctx->init_flags;
914 ctx->priv->enc.total_encoders = 1;
915 priv->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
916 if (priv->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
918 if (ctx->config.enc) {
919 // Update the reference to the config structure to an internal copy.
920 priv->cfg = *ctx->config.enc;
921 ctx->config.enc = &priv->cfg;
924 priv->extra_cfg = default_extra_cfg;
925 once(vp9_initialize_enc);
927 res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
929 if (res == VPX_CODEC_OK) {
930 priv->pts_offset_initialized = 0;
931 // TODO(angiebird): Replace priv->timestamp_ratio by
932 // oxcf->g_timebase_in_ts
933 priv->timestamp_ratio = get_g_timebase_in_ts(priv->cfg.g_timebase);
935 set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
936 #if CONFIG_VP9_HIGHBITDEPTH
937 priv->oxcf.use_highbitdepth =
938 (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
940 priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
941 if (priv->cpi == NULL) res = VPX_CODEC_MEM_ERROR;
948 static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
950 vp9_remove_compressor(ctx->cpi);
951 vpx_free(ctx->buffer_pool);
956 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
957 unsigned long duration,
958 unsigned long deadline) {
959 MODE new_mode = BEST;
961 #if CONFIG_REALTIME_ONLY
963 deadline = VPX_DL_REALTIME;
965 switch (ctx->cfg.g_pass) {
966 case VPX_RC_ONE_PASS:
968 // Convert duration parameter from stream timebase to microseconds.
969 uint64_t duration_us;
971 VPX_STATIC_ASSERT(TICKS_PER_SEC > 1000000 &&
972 (TICKS_PER_SEC % 1000000) == 0);
974 duration_us = duration * (uint64_t)ctx->timestamp_ratio.num /
975 (ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
977 // If the deadline is more that the duration this frame is to be shown,
978 // use good quality mode. Otherwise use realtime mode.
979 new_mode = (deadline > duration_us) ? GOOD : REALTIME;
984 case VPX_RC_FIRST_PASS: break;
985 case VPX_RC_LAST_PASS: new_mode = deadline > 0 ? GOOD : BEST; break;
987 #endif // CONFIG_REALTIME_ONLY
989 if (deadline == VPX_DL_REALTIME) {
994 if (ctx->oxcf.mode != new_mode) {
995 ctx->oxcf.mode = new_mode;
996 vp9_change_config(ctx->cpi, &ctx->oxcf);
1000 // Turn on to test if supplemental superframe data breaks decoding
1001 // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1002 static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
1003 uint8_t marker = 0xc0;
1007 assert(ctx->pending_frame_count);
1008 assert(ctx->pending_frame_count <= 8);
1010 // Add the number of frames to the marker byte
1011 marker |= ctx->pending_frame_count - 1;
1013 // Choose the magnitude
1014 for (mag = 0, mask = 0xff; mag < 4; mag++) {
1015 if (ctx->pending_frame_magnitude < mask) break;
1022 index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
1023 if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
1024 uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
1026 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1027 uint8_t marker_test = 0xc0;
1028 int mag_test = 2; // 1 - 4
1029 int frames_test = 4; // 1 - 8
1030 int index_sz_test = 2 + mag_test * frames_test;
1031 marker_test |= frames_test - 1;
1032 marker_test |= (mag_test - 1) << 3;
1034 for (i = 0; i < mag_test * frames_test; ++i)
1035 *x++ = 0; // fill up with arbitrary data
1037 ctx->pending_cx_data_sz += index_sz_test;
1038 printf("Added supplemental superframe data\n");
1042 for (i = 0; i < ctx->pending_frame_count; i++) {
1043 unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
1045 for (j = 0; j <= mag; j++) {
1046 *x++ = this_sz & 0xff;
1051 ctx->pending_cx_data_sz += index_sz;
1052 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1053 index_sz += index_sz_test;
1059 static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
1060 unsigned int lib_flags) {
1061 vpx_codec_frame_flags_t flags = lib_flags << 16;
1063 if (lib_flags & FRAMEFLAGS_KEY ||
1064 (cpi->use_svc && cpi->svc
1065 .layer_context[cpi->svc.spatial_layer_id *
1066 cpi->svc.number_temporal_layers +
1067 cpi->svc.temporal_layer_id]
1069 flags |= VPX_FRAME_IS_KEY;
1071 if (cpi->droppable) flags |= VPX_FRAME_IS_DROPPABLE;
1076 static INLINE vpx_codec_cx_pkt_t get_psnr_pkt(const PSNR_STATS *psnr) {
1077 vpx_codec_cx_pkt_t pkt;
1078 pkt.kind = VPX_CODEC_PSNR_PKT;
1079 pkt.data.psnr = *psnr;
1083 #if !CONFIG_REALTIME_ONLY
1084 static INLINE vpx_codec_cx_pkt_t
1085 get_first_pass_stats_pkt(FIRSTPASS_STATS *stats) {
1086 // WARNNING: This function assumes that stats will
1087 // exist and not be changed until the packet is processed
1088 // TODO(angiebird): Refactor the code to avoid using the assumption.
1089 vpx_codec_cx_pkt_t pkt;
1090 pkt.kind = VPX_CODEC_STATS_PKT;
1091 pkt.data.twopass_stats.buf = stats;
1092 pkt.data.twopass_stats.sz = sizeof(*stats);
1097 const size_t kMinCompressedSize = 8192;
1098 static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t *ctx,
1099 const vpx_image_t *img,
1100 vpx_codec_pts_t pts_val,
1101 unsigned long duration,
1102 vpx_enc_frame_flags_t enc_flags,
1103 unsigned long deadline) {
1104 volatile vpx_codec_err_t res = VPX_CODEC_OK;
1105 volatile vpx_enc_frame_flags_t flags = enc_flags;
1106 volatile vpx_codec_pts_t pts = pts_val;
1107 VP9_COMP *const cpi = ctx->cpi;
1108 const vpx_rational64_t *const timestamp_ratio = &ctx->timestamp_ratio;
1110 vpx_codec_cx_pkt_t pkt;
1111 memset(&pkt, 0, sizeof(pkt));
1113 if (cpi == NULL) return VPX_CODEC_INVALID_PARAM;
1116 res = validate_img(ctx, img);
1117 if (res == VPX_CODEC_OK) {
1118 // There's no codec control for multiple alt-refs so check the encoder
1119 // instance for its status to determine the compressed data size.
1120 data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
1121 (cpi->multi_layer_arf ? 8 : 2);
1122 if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
1123 if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1124 ctx->cx_data_sz = data_sz;
1126 ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1127 if (ctx->cx_data == NULL) {
1128 return VPX_CODEC_MEM_ERROR;
1134 if (!ctx->pts_offset_initialized) {
1135 ctx->pts_offset = pts;
1136 ctx->pts_offset_initialized = 1;
1138 pts -= ctx->pts_offset;
1140 pick_quickcompress_mode(ctx, duration, deadline);
1141 vpx_codec_pkt_list_init(&ctx->pkt_list);
1144 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
1145 ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
1146 ctx->base.err_detail = "Conflicting flags.";
1147 return VPX_CODEC_INVALID_PARAM;
1150 if (setjmp(cpi->common.error.jmp)) {
1151 cpi->common.error.setjmp = 0;
1152 res = update_error_state(ctx, &cpi->common.error);
1153 vpx_clear_system_state();
1156 cpi->common.error.setjmp = 1;
1158 if (res == VPX_CODEC_OK) vp9_apply_encoding_flags(cpi, flags);
1160 // Handle fixed keyframe intervals
1161 if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
1162 ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1163 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1164 flags |= VPX_EFLAG_FORCE_KF;
1165 ctx->fixed_kf_cntr = 1;
1169 if (res == VPX_CODEC_OK) {
1170 unsigned int lib_flags = 0;
1171 YV12_BUFFER_CONFIG sd;
1172 int64_t dst_time_stamp = timebase_units_to_ticks(timestamp_ratio, pts);
1173 int64_t dst_end_time_stamp =
1174 timebase_units_to_ticks(timestamp_ratio, pts + duration);
1175 size_t size, cx_data_sz;
1176 unsigned char *cx_data;
1178 cpi->svc.timebase_fac = timebase_units_to_ticks(timestamp_ratio, 1);
1179 cpi->svc.time_stamp_superframe = dst_time_stamp;
1181 // Set up internal flags
1182 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
1185 res = image2yuvconfig(img, &sd);
1187 // Store the original flags in to the frame buffer. Will extract the
1188 // key frame flag when we actually encode this frame.
1189 if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1190 dst_time_stamp, dst_end_time_stamp)) {
1191 res = update_error_state(ctx, &cpi->common.error);
1193 ctx->next_frame_flags = 0;
1196 cx_data = ctx->cx_data;
1197 cx_data_sz = ctx->cx_data_sz;
1199 /* Any pending invisible frames? */
1200 if (ctx->pending_cx_data) {
1201 memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1202 ctx->pending_cx_data = cx_data;
1203 cx_data += ctx->pending_cx_data_sz;
1204 cx_data_sz -= ctx->pending_cx_data_sz;
1206 /* TODO: this is a minimal check, the underlying codec doesn't respect
1207 * the buffer size anyway.
1209 if (cx_data_sz < ctx->cx_data_sz / 2) {
1210 vpx_internal_error(&cpi->common.error, VPX_CODEC_ERROR,
1211 "Compressed data buffer too small");
1212 return VPX_CODEC_ERROR;
1216 if (cpi->oxcf.pass == 1 && !cpi->use_svc) {
1217 #if !CONFIG_REALTIME_ONLY
1218 // compute first pass stats
1221 vpx_codec_cx_pkt_t fps_pkt;
1222 ENCODE_FRAME_RESULT encode_frame_result;
1223 vp9_init_encode_frame_result(&encode_frame_result);
1224 // TODO(angiebird): Call vp9_first_pass directly
1225 ret = vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1226 &dst_time_stamp, &dst_end_time_stamp,
1227 !img, &encode_frame_result);
1228 assert(size == 0); // There is no compressed data in the first pass
1231 fps_pkt = get_first_pass_stats_pkt(&cpi->twopass.this_frame_stats);
1232 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &fps_pkt);
1234 if (!cpi->twopass.first_pass_done) {
1235 vpx_codec_cx_pkt_t fps_pkt;
1236 vp9_end_first_pass(cpi);
1237 fps_pkt = get_first_pass_stats_pkt(&cpi->twopass.total_stats);
1238 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &fps_pkt);
1241 #else // !CONFIG_REALTIME_ONLY
1243 #endif // !CONFIG_REALTIME_ONLY
1245 ENCODE_FRAME_RESULT encode_frame_result;
1246 vp9_init_encode_frame_result(&encode_frame_result);
1247 while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1248 -1 != vp9_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1249 &dst_time_stamp, &dst_end_time_stamp,
1250 !img, &encode_frame_result)) {
1252 if (size > 0 && !cpi->use_svc) {
1253 // TODO(angiebird): Figure out while we don't need psnr pkt when
1256 if (vp9_get_psnr(cpi, &psnr)) {
1257 vpx_codec_cx_pkt_t psnr_pkt = get_psnr_pkt(&psnr);
1258 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &psnr_pkt);
1262 if (size || (cpi->use_svc && cpi->svc.skip_enhancement_layer)) {
1263 // Pack invisible frames with the next visible frame
1264 if (!cpi->common.show_frame ||
1265 (cpi->use_svc && cpi->svc.spatial_layer_id <
1266 cpi->svc.number_spatial_layers - 1)) {
1267 if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
1268 ctx->pending_cx_data_sz += size;
1270 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1271 ctx->pending_frame_magnitude |= size;
1274 pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
1275 pkt.data.frame.height[cpi->svc.spatial_layer_id] =
1277 pkt.data.frame.spatial_layer_encoded[cpi->svc.spatial_layer_id] =
1278 1 - cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id];
1280 if (ctx->output_cx_pkt_cb.output_cx_pkt) {
1281 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1282 pkt.data.frame.pts =
1283 ticks_to_timebase_units(timestamp_ratio, dst_time_stamp) +
1285 pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1286 timestamp_ratio, dst_end_time_stamp - dst_time_stamp);
1287 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1288 pkt.data.frame.buf = ctx->pending_cx_data;
1289 pkt.data.frame.sz = size;
1290 ctx->pending_cx_data = NULL;
1291 ctx->pending_cx_data_sz = 0;
1292 ctx->pending_frame_count = 0;
1293 ctx->pending_frame_magnitude = 0;
1294 ctx->output_cx_pkt_cb.output_cx_pkt(
1295 &pkt, ctx->output_cx_pkt_cb.user_priv);
1300 // Add the frame packet to the list of returned packets.
1301 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1302 pkt.data.frame.pts =
1303 ticks_to_timebase_units(timestamp_ratio, dst_time_stamp) +
1305 pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1306 timestamp_ratio, dst_end_time_stamp - dst_time_stamp);
1307 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1308 pkt.data.frame.width[cpi->svc.spatial_layer_id] = cpi->common.width;
1309 pkt.data.frame.height[cpi->svc.spatial_layer_id] = cpi->common.height;
1310 pkt.data.frame.spatial_layer_encoded[cpi->svc.spatial_layer_id] =
1311 1 - cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id];
1313 if (ctx->pending_cx_data) {
1315 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1316 ctx->pending_frame_magnitude |= size;
1317 ctx->pending_cx_data_sz += size;
1318 // write the superframe only for the case when
1319 if (!ctx->output_cx_pkt_cb.output_cx_pkt)
1320 size += write_superframe_index(ctx);
1321 pkt.data.frame.buf = ctx->pending_cx_data;
1322 pkt.data.frame.sz = ctx->pending_cx_data_sz;
1323 ctx->pending_cx_data = NULL;
1324 ctx->pending_cx_data_sz = 0;
1325 ctx->pending_frame_count = 0;
1326 ctx->pending_frame_magnitude = 0;
1328 pkt.data.frame.buf = cx_data;
1329 pkt.data.frame.sz = size;
1331 pkt.data.frame.partition_id = -1;
1333 if (ctx->output_cx_pkt_cb.output_cx_pkt)
1334 ctx->output_cx_pkt_cb.output_cx_pkt(
1335 &pkt, ctx->output_cx_pkt_cb.user_priv);
1337 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1341 if (is_one_pass_cbr_svc(cpi) &&
1342 (cpi->svc.spatial_layer_id ==
1343 cpi->svc.number_spatial_layers - 1)) {
1344 // Encoded all spatial layers; exit loop.
1352 cpi->common.error.setjmp = 0;
1356 static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1357 vpx_codec_iter_t *iter) {
1358 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1361 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
1363 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1365 if (frame != NULL) {
1366 YV12_BUFFER_CONFIG sd;
1368 image2yuvconfig(&frame->img, &sd);
1369 vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
1371 return VPX_CODEC_OK;
1373 return VPX_CODEC_INVALID_PARAM;
1376 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
1378 vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1380 if (frame != NULL) {
1381 YV12_BUFFER_CONFIG sd;
1383 image2yuvconfig(&frame->img, &sd);
1384 vp9_copy_reference_enc(ctx->cpi,
1385 ref_frame_to_vp9_reframe(frame->frame_type), &sd);
1386 return VPX_CODEC_OK;
1388 return VPX_CODEC_INVALID_PARAM;
1391 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
1393 vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
1395 if (frame != NULL) {
1396 const int fb_idx = ctx->cpi->common.cur_show_frame_fb_idx;
1397 YV12_BUFFER_CONFIG *fb = get_buf_frame(&ctx->cpi->common, fb_idx);
1398 if (fb == NULL) return VPX_CODEC_ERROR;
1399 yuvconfig2image(&frame->img, fb, NULL);
1400 return VPX_CODEC_OK;
1402 return VPX_CODEC_INVALID_PARAM;
1405 static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
1407 #if CONFIG_VP9_POSTPROC
1408 vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
1409 if (config != NULL) {
1410 ctx->preview_ppcfg = *config;
1411 return VPX_CODEC_OK;
1413 return VPX_CODEC_INVALID_PARAM;
1417 return VPX_CODEC_INCAPABLE;
1421 static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
1422 YV12_BUFFER_CONFIG sd;
1423 vp9_ppflags_t flags;
1426 if (ctx->preview_ppcfg.post_proc_flag) {
1427 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1428 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1429 flags.noise_level = ctx->preview_ppcfg.noise_level;
1432 if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
1433 yuvconfig2image(&ctx->preview_img, &sd, NULL);
1434 return &ctx->preview_img;
1439 static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
1441 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1444 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1446 if (!vp9_set_roi_map(ctx->cpi, roi->roi_map, roi->rows, roi->cols,
1447 roi->delta_q, roi->delta_lf, roi->skip,
1449 return VPX_CODEC_OK;
1451 return VPX_CODEC_INVALID_PARAM;
1453 return VPX_CODEC_INVALID_PARAM;
1456 static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
1458 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1461 if (!vp9_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
1463 return VPX_CODEC_OK;
1465 return VPX_CODEC_INVALID_PARAM;
1467 return VPX_CODEC_INVALID_PARAM;
1470 static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
1472 vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1475 if (!vp9_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
1477 return VPX_CODEC_OK;
1479 return VPX_CODEC_INVALID_PARAM;
1481 return VPX_CODEC_INVALID_PARAM;
1484 static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
1486 vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
1490 vp9_set_internal_size(ctx->cpi, (VPX_SCALING)mode->h_scaling_mode,
1491 (VPX_SCALING)mode->v_scaling_mode);
1492 return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
1494 return VPX_CODEC_INVALID_PARAM;
1497 static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
1498 int data = va_arg(args, int);
1499 const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
1500 // Both one-pass and two-pass RC are supported now.
1501 // User setting this has to make sure of the following.
1502 // In two-pass setting: either (but not both)
1503 // cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1504 // In one-pass setting:
1505 // either or both cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1507 vp9_set_svc(ctx->cpi, data);
1510 (cfg->g_pass == VPX_RC_FIRST_PASS || cfg->g_pass == VPX_RC_LAST_PASS) &&
1511 cfg->ss_number_layers > 1 && cfg->ts_number_layers > 1) {
1512 return VPX_CODEC_INVALID_PARAM;
1515 vp9_set_row_mt(ctx->cpi);
1517 return VPX_CODEC_OK;
1520 static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1522 vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
1523 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1524 SVC *const svc = &cpi->svc;
1527 svc->spatial_layer_to_encode = data->spatial_layer_id;
1528 svc->first_spatial_layer_to_encode = data->spatial_layer_id;
1529 // TODO(jianj): Deprecated to be removed.
1530 svc->temporal_layer_id = data->temporal_layer_id;
1531 // Allow for setting temporal layer per spatial layer for superframe.
1532 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1533 svc->temporal_layer_id_per_spatial[sl] =
1534 data->temporal_layer_id_per_spatial[sl];
1536 // Checks on valid layer_id input.
1537 if (svc->temporal_layer_id < 0 ||
1538 svc->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
1539 return VPX_CODEC_INVALID_PARAM;
1542 return VPX_CODEC_OK;
1545 static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1547 vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
1548 VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1549 SVC *const svc = &cpi->svc;
1551 data->spatial_layer_id = svc->spatial_layer_id;
1552 data->temporal_layer_id = svc->temporal_layer_id;
1554 return VPX_CODEC_OK;
1557 static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1559 VP9_COMP *const cpi = ctx->cpi;
1560 vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
1563 // Number of temporal layers and number of spatial layers have to be set
1564 // properly before calling this control function.
1565 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1566 for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
1568 LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
1569 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1570 lc->max_q = params->max_quantizers[layer];
1571 lc->min_q = params->min_quantizers[layer];
1572 lc->scaling_factor_num = params->scaling_factor_num[sl];
1573 lc->scaling_factor_den = params->scaling_factor_den[sl];
1574 lc->speed = params->speed_per_layer[sl];
1578 return VPX_CODEC_OK;
1581 static vpx_codec_err_t ctrl_get_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
1583 VP9_COMP *const cpi = ctx->cpi;
1584 vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
1586 for (sl = 0; sl <= cpi->svc.spatial_layer_id; sl++) {
1587 data->update_buffer_slot[sl] = cpi->svc.update_buffer_slot[sl];
1588 data->reference_last[sl] = cpi->svc.reference_last[sl];
1589 data->reference_golden[sl] = cpi->svc.reference_golden[sl];
1590 data->reference_alt_ref[sl] = cpi->svc.reference_altref[sl];
1591 data->lst_fb_idx[sl] = cpi->svc.lst_fb_idx[sl];
1592 data->gld_fb_idx[sl] = cpi->svc.gld_fb_idx[sl];
1593 data->alt_fb_idx[sl] = cpi->svc.alt_fb_idx[sl];
1594 // TODO(jianj): Remove these 3, deprecated.
1595 data->update_last[sl] = cpi->svc.update_last[sl];
1596 data->update_golden[sl] = cpi->svc.update_golden[sl];
1597 data->update_alt_ref[sl] = cpi->svc.update_altref[sl];
1599 return VPX_CODEC_OK;
1602 static vpx_codec_err_t ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
1604 VP9_COMP *const cpi = ctx->cpi;
1605 vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
1607 cpi->svc.use_set_ref_frame_config = 1;
1608 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1609 cpi->svc.update_buffer_slot[sl] = data->update_buffer_slot[sl];
1610 cpi->svc.reference_last[sl] = data->reference_last[sl];
1611 cpi->svc.reference_golden[sl] = data->reference_golden[sl];
1612 cpi->svc.reference_altref[sl] = data->reference_alt_ref[sl];
1613 cpi->svc.lst_fb_idx[sl] = data->lst_fb_idx[sl];
1614 cpi->svc.gld_fb_idx[sl] = data->gld_fb_idx[sl];
1615 cpi->svc.alt_fb_idx[sl] = data->alt_fb_idx[sl];
1616 cpi->svc.duration[sl] = data->duration[sl];
1618 return VPX_CODEC_OK;
1621 static vpx_codec_err_t ctrl_set_svc_inter_layer_pred(vpx_codec_alg_priv_t *ctx,
1623 const int data = va_arg(args, int);
1624 VP9_COMP *const cpi = ctx->cpi;
1625 cpi->svc.disable_inter_layer_pred = data;
1626 return VPX_CODEC_OK;
1629 static vpx_codec_err_t ctrl_set_svc_frame_drop_layer(vpx_codec_alg_priv_t *ctx,
1631 VP9_COMP *const cpi = ctx->cpi;
1632 vpx_svc_frame_drop_t *data = va_arg(args, vpx_svc_frame_drop_t *);
1634 cpi->svc.framedrop_mode = data->framedrop_mode;
1635 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl)
1636 cpi->svc.framedrop_thresh[sl] = data->framedrop_thresh[sl];
1637 // Don't allow max_consec_drop values below 1.
1638 cpi->svc.max_consec_drop = VPXMAX(1, data->max_consec_drop);
1639 return VPX_CODEC_OK;
1642 static vpx_codec_err_t ctrl_set_svc_gf_temporal_ref(vpx_codec_alg_priv_t *ctx,
1644 VP9_COMP *const cpi = ctx->cpi;
1645 const unsigned int data = va_arg(args, unsigned int);
1646 cpi->svc.use_gf_temporal_ref = data;
1647 return VPX_CODEC_OK;
1650 static vpx_codec_err_t ctrl_set_svc_spatial_layer_sync(
1651 vpx_codec_alg_priv_t *ctx, va_list args) {
1652 VP9_COMP *const cpi = ctx->cpi;
1653 vpx_svc_spatial_layer_sync_t *data =
1654 va_arg(args, vpx_svc_spatial_layer_sync_t *);
1656 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl)
1657 cpi->svc.spatial_layer_sync[sl] = data->spatial_layer_sync[sl];
1658 cpi->svc.set_intra_only_frame = data->base_layer_intra_only;
1659 return VPX_CODEC_OK;
1662 static vpx_codec_err_t ctrl_set_delta_q_uv(vpx_codec_alg_priv_t *ctx,
1664 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1665 int data = va_arg(args, int);
1666 data = VPXMIN(VPXMAX(data, -15), 15);
1667 extra_cfg.delta_q_uv = data;
1668 return update_extra_cfg(ctx, &extra_cfg);
1671 static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
1673 vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
1674 (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
1675 ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
1676 ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
1678 return VPX_CODEC_OK;
1681 static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
1683 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1684 extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
1685 return update_extra_cfg(ctx, &extra_cfg);
1688 static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
1690 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1691 extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
1692 return update_extra_cfg(ctx, &extra_cfg);
1695 static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
1697 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1698 extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
1699 return update_extra_cfg(ctx, &extra_cfg);
1702 static vpx_codec_err_t ctrl_set_render_size(vpx_codec_alg_priv_t *ctx,
1704 struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1705 int *const render_size = va_arg(args, int *);
1706 extra_cfg.render_width = render_size[0];
1707 extra_cfg.render_height = render_size[1];
1708 return update_extra_cfg(ctx, &extra_cfg);
1711 static vpx_codec_err_t ctrl_set_postencode_drop(vpx_codec_alg_priv_t *ctx,
1713 VP9_COMP *const cpi = ctx->cpi;
1714 const unsigned int data = va_arg(args, unsigned int);
1715 cpi->rc.ext_use_post_encode_drop = data;
1716 return VPX_CODEC_OK;
1719 static vpx_codec_err_t ctrl_set_disable_overshoot_maxq_cbr(
1720 vpx_codec_alg_priv_t *ctx, va_list args) {
1721 VP9_COMP *const cpi = ctx->cpi;
1722 const unsigned int data = va_arg(args, unsigned int);
1723 cpi->rc.disable_overshoot_maxq_cbr = data;
1724 return VPX_CODEC_OK;
1727 static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1728 { VP8_COPY_REFERENCE, ctrl_copy_reference },
1731 { VP8_SET_REFERENCE, ctrl_set_reference },
1732 { VP8_SET_POSTPROC, ctrl_set_previewpp },
1733 { VP9E_SET_ROI_MAP, ctrl_set_roi_map },
1734 { VP8E_SET_ACTIVEMAP, ctrl_set_active_map },
1735 { VP8E_SET_SCALEMODE, ctrl_set_scale_mode },
1736 { VP8E_SET_CPUUSED, ctrl_set_cpuused },
1737 { VP8E_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
1738 { VP8E_SET_SHARPNESS, ctrl_set_sharpness },
1739 { VP8E_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
1740 { VP9E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
1741 { VP9E_SET_TILE_ROWS, ctrl_set_tile_rows },
1742 { VP9E_SET_TPL, ctrl_set_tpl_model },
1743 { VP8E_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
1744 { VP8E_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
1745 { VP8E_SET_ARNR_TYPE, ctrl_set_arnr_type },
1746 { VP8E_SET_TUNING, ctrl_set_tuning },
1747 { VP8E_SET_CQ_LEVEL, ctrl_set_cq_level },
1748 { VP8E_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
1749 { VP9E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
1750 { VP9E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1751 { VP9E_SET_LOSSLESS, ctrl_set_lossless },
1752 { VP9E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
1753 { VP9E_SET_AQ_MODE, ctrl_set_aq_mode },
1754 { VP9E_SET_ALT_REF_AQ, ctrl_set_alt_ref_aq },
1755 { VP9E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
1756 { VP9E_SET_SVC, ctrl_set_svc },
1757 { VP9E_SET_SVC_PARAMETERS, ctrl_set_svc_parameters },
1758 { VP9E_REGISTER_CX_CALLBACK, ctrl_register_cx_callback },
1759 { VP9E_SET_SVC_LAYER_ID, ctrl_set_svc_layer_id },
1760 { VP9E_SET_TUNE_CONTENT, ctrl_set_tune_content },
1761 { VP9E_SET_COLOR_SPACE, ctrl_set_color_space },
1762 { VP9E_SET_COLOR_RANGE, ctrl_set_color_range },
1763 { VP9E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
1764 { VP9E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
1765 { VP9E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
1766 { VP9E_SET_SVC_REF_FRAME_CONFIG, ctrl_set_svc_ref_frame_config },
1767 { VP9E_SET_RENDER_SIZE, ctrl_set_render_size },
1768 { VP9E_SET_TARGET_LEVEL, ctrl_set_target_level },
1769 { VP9E_SET_ROW_MT, ctrl_set_row_mt },
1770 { VP9E_SET_POSTENCODE_DROP, ctrl_set_postencode_drop },
1771 { VP9E_SET_DISABLE_OVERSHOOT_MAXQ_CBR, ctrl_set_disable_overshoot_maxq_cbr },
1772 { VP9E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
1773 { VP9E_SET_SVC_INTER_LAYER_PRED, ctrl_set_svc_inter_layer_pred },
1774 { VP9E_SET_SVC_FRAME_DROP_LAYER, ctrl_set_svc_frame_drop_layer },
1775 { VP9E_SET_SVC_GF_TEMPORAL_REF, ctrl_set_svc_gf_temporal_ref },
1776 { VP9E_SET_SVC_SPATIAL_LAYER_SYNC, ctrl_set_svc_spatial_layer_sync },
1777 { VP9E_SET_DELTA_Q_UV, ctrl_set_delta_q_uv },
1780 { VP8E_GET_LAST_QUANTIZER, ctrl_get_quantizer },
1781 { VP8E_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
1782 { VP9_GET_REFERENCE, ctrl_get_reference },
1783 { VP9E_GET_SVC_LAYER_ID, ctrl_get_svc_layer_id },
1784 { VP9E_GET_ACTIVEMAP, ctrl_get_active_map },
1785 { VP9E_GET_LEVEL, ctrl_get_level },
1786 { VP9E_GET_SVC_REF_FRAME_CONFIG, ctrl_get_svc_ref_frame_config },
1791 static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
1795 0, // g_usage (unused)
1801 VPX_BITS_8, // g_bit_depth
1802 8, // g_input_bit_depth
1804 { 1, 30 }, // g_timebase
1806 0, // g_error_resilient
1808 VPX_RC_ONE_PASS, // g_pass
1810 25, // g_lag_in_frames
1812 0, // rc_dropframe_thresh
1813 0, // rc_resize_allowed
1814 0, // rc_scaled_width
1815 0, // rc_scaled_height
1816 60, // rc_resize_down_thresold
1817 30, // rc_resize_up_thresold
1819 VPX_VBR, // rc_end_usage
1820 { NULL, 0 }, // rc_twopass_stats_in
1821 { NULL, 0 }, // rc_firstpass_mb_stats_in
1822 256, // rc_target_bitrate
1823 0, // rc_min_quantizer
1824 63, // rc_max_quantizer
1825 25, // rc_undershoot_pct
1826 25, // rc_overshoot_pct
1828 6000, // rc_max_buffer_size
1829 4000, // rc_buffer_initial_size
1830 5000, // rc_buffer_optimal_size
1832 50, // rc_two_pass_vbrbias
1833 0, // rc_two_pass_vbrmin_section
1834 2000, // rc_two_pass_vbrmax_section
1835 0, // rc_2pass_vbr_corpus_complexity (non 0 for corpus vbr)
1837 // keyframing settings (kf)
1838 VPX_KF_AUTO, // g_kfmode
1842 VPX_SS_DEFAULT_LAYERS, // ss_number_layers
1844 { 0 }, // ss_target_bitrate
1845 1, // ts_number_layers
1846 { 0 }, // ts_target_bitrate
1847 { 0 }, // ts_rate_decimator
1848 0, // ts_periodicity
1849 { 0 }, // ts_layer_id
1850 { 0 }, // layer_taget_bitrate
1851 0 // temporal_layering_mode
1855 #ifndef VERSION_STRING
1856 #define VERSION_STRING
1858 CODEC_INTERFACE(vpx_codec_vp9_cx) = {
1859 "WebM Project VP9 Encoder" VERSION_STRING,
1860 VPX_CODEC_INTERNAL_ABI_VERSION,
1861 #if CONFIG_VP9_HIGHBITDEPTH
1862 VPX_CODEC_CAP_HIGHBITDEPTH |
1864 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR, // vpx_codec_caps_t
1865 encoder_init, // vpx_codec_init_fn_t
1866 encoder_destroy, // vpx_codec_destroy_fn_t
1867 encoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1870 NULL, // vpx_codec_peek_si_fn_t
1871 NULL, // vpx_codec_get_si_fn_t
1872 NULL, // vpx_codec_decode_fn_t
1873 NULL, // vpx_codec_frame_get_fn_t
1874 NULL // vpx_codec_set_fb_fn_t
1879 encoder_usage_cfg_map, // vpx_codec_enc_cfg_map_t
1880 encoder_encode, // vpx_codec_encode_fn_t
1881 encoder_get_cxdata, // vpx_codec_get_cx_data_fn_t
1882 encoder_set_config, // vpx_codec_enc_config_set_fn_t
1883 NULL, // vpx_codec_get_global_headers_fn_t
1884 encoder_get_preview, // vpx_codec_get_preview_frame_fn_t
1885 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t
1889 static vpx_codec_enc_cfg_t get_enc_cfg(int frame_width, int frame_height,
1890 vpx_rational_t frame_rate,
1892 vpx_enc_pass enc_pass) {
1893 vpx_codec_enc_cfg_t enc_cfg = encoder_usage_cfg_map[0].cfg;
1894 enc_cfg.g_w = frame_width;
1895 enc_cfg.g_h = frame_height;
1896 enc_cfg.rc_target_bitrate = target_bitrate;
1897 enc_cfg.g_pass = enc_pass;
1898 // g_timebase is the inverse of frame_rate
1899 enc_cfg.g_timebase.num = frame_rate.den;
1900 enc_cfg.g_timebase.den = frame_rate.num;
1904 static vp9_extracfg get_extra_cfg() {
1905 vp9_extracfg extra_cfg = default_extra_cfg;
1909 VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
1910 vpx_rational_t frame_rate,
1911 int target_bitrate, int encode_speed,
1912 vpx_enc_pass enc_pass) {
1913 /* This function will generate the same VP9EncoderConfig used by the
1914 * vpxenc command given below.
1915 * The configs in the vpxenc command corresponds to parameters of
1916 * vp9_get_encoder_config() as follows.
1918 * WIDTH: frame_width
1919 * HEIGHT: frame_height
1921 * BITRATE: target_bitrate
1922 * CPU_USED:encode_speed
1924 * INPUT, OUTPUT, LIMIT will not affect VP9EncoderConfig
1928 * OUTPUT=output.webm
1935 * ./vpxenc --limit=$LIMIT --width=$WIDTH --height=$HEIGHT --fps=$FPS
1936 * --lag-in-frames=25 \
1937 * --codec=vp9 --good --cpu-used=CPU_USED --threads=0 --profile=0 \
1938 * --min-q=0 --max-q=63 --auto-alt-ref=1 --passes=2 --kf-max-dist=150 \
1939 * --kf-min-dist=0 --drop-frame=0 --static-thresh=0 --bias-pct=50 \
1940 * --minsection-pct=0 --maxsection-pct=150 --arnr-maxframes=7 --psnr \
1941 * --arnr-strength=5 --sharpness=0 --undershoot-pct=100 --overshoot-pct=100 \
1942 * --frame-parallel=0 --tile-columns=0 --cpu-used=0 --end-usage=vbr \
1943 * --target-bitrate=$BITRATE -o $OUTPUT $INPUT
1946 VP9EncoderConfig oxcf;
1947 vp9_extracfg extra_cfg = get_extra_cfg();
1948 vpx_codec_enc_cfg_t enc_cfg = get_enc_cfg(
1949 frame_width, frame_height, frame_rate, target_bitrate, enc_pass);
1950 set_encoder_config(&oxcf, &enc_cfg, &extra_cfg);
1952 // These settings are made to match the settings of the vpxenc command.
1953 oxcf.key_freq = 150;
1954 oxcf.under_shoot_pct = 100;
1955 oxcf.over_shoot_pct = 100;
1956 oxcf.max_threads = 0;
1957 oxcf.tile_columns = 0;
1958 oxcf.frame_parallel_decoding_mode = 0;
1959 oxcf.two_pass_vbrmax_section = 150;
1960 oxcf.speed = abs(encode_speed);
1964 #define DUMP_STRUCT_VALUE(struct, value) \
1965 printf(#value " %" PRId64 "\n", (int64_t)(struct)->value)
1967 void vp9_dump_encoder_config(const VP9EncoderConfig *oxcf) {
1968 DUMP_STRUCT_VALUE(oxcf, profile);
1969 DUMP_STRUCT_VALUE(oxcf, bit_depth);
1970 DUMP_STRUCT_VALUE(oxcf, width);
1971 DUMP_STRUCT_VALUE(oxcf, height);
1972 DUMP_STRUCT_VALUE(oxcf, input_bit_depth);
1973 DUMP_STRUCT_VALUE(oxcf, init_framerate);
1974 // TODO(angiebird): dump g_timebase
1975 // TODO(angiebird): dump g_timebase_in_ts
1977 DUMP_STRUCT_VALUE(oxcf, target_bandwidth);
1979 DUMP_STRUCT_VALUE(oxcf, noise_sensitivity);
1980 DUMP_STRUCT_VALUE(oxcf, sharpness);
1981 DUMP_STRUCT_VALUE(oxcf, speed);
1982 DUMP_STRUCT_VALUE(oxcf, rc_max_intra_bitrate_pct);
1983 DUMP_STRUCT_VALUE(oxcf, rc_max_inter_bitrate_pct);
1984 DUMP_STRUCT_VALUE(oxcf, gf_cbr_boost_pct);
1986 DUMP_STRUCT_VALUE(oxcf, mode);
1987 DUMP_STRUCT_VALUE(oxcf, pass);
1989 // Key Framing Operations
1990 DUMP_STRUCT_VALUE(oxcf, auto_key);
1991 DUMP_STRUCT_VALUE(oxcf, key_freq);
1993 DUMP_STRUCT_VALUE(oxcf, lag_in_frames);
1995 // ----------------------------------------------------------------
1996 // DATARATE CONTROL OPTIONS
1998 // vbr, cbr, constrained quality or constant quality
1999 DUMP_STRUCT_VALUE(oxcf, rc_mode);
2001 // buffer targeting aggressiveness
2002 DUMP_STRUCT_VALUE(oxcf, under_shoot_pct);
2003 DUMP_STRUCT_VALUE(oxcf, over_shoot_pct);
2005 // buffering parameters
2006 // TODO(angiebird): dump tarting_buffer_level_ms
2007 // TODO(angiebird): dump ptimal_buffer_level_ms
2008 // TODO(angiebird): dump maximum_buffer_size_ms
2010 // Frame drop threshold.
2011 DUMP_STRUCT_VALUE(oxcf, drop_frames_water_mark);
2013 // controlling quality
2014 DUMP_STRUCT_VALUE(oxcf, fixed_q);
2015 DUMP_STRUCT_VALUE(oxcf, worst_allowed_q);
2016 DUMP_STRUCT_VALUE(oxcf, best_allowed_q);
2017 DUMP_STRUCT_VALUE(oxcf, cq_level);
2018 DUMP_STRUCT_VALUE(oxcf, aq_mode);
2020 // Special handling of Adaptive Quantization for AltRef frames
2021 DUMP_STRUCT_VALUE(oxcf, alt_ref_aq);
2023 // Internal frame size scaling.
2024 DUMP_STRUCT_VALUE(oxcf, resize_mode);
2025 DUMP_STRUCT_VALUE(oxcf, scaled_frame_width);
2026 DUMP_STRUCT_VALUE(oxcf, scaled_frame_height);
2028 // Enable feature to reduce the frame quantization every x frames.
2029 DUMP_STRUCT_VALUE(oxcf, frame_periodic_boost);
2031 // two pass datarate control
2032 DUMP_STRUCT_VALUE(oxcf, two_pass_vbrbias);
2033 DUMP_STRUCT_VALUE(oxcf, two_pass_vbrmin_section);
2034 DUMP_STRUCT_VALUE(oxcf, two_pass_vbrmax_section);
2035 DUMP_STRUCT_VALUE(oxcf, vbr_corpus_complexity);
2036 // END DATARATE CONTROL OPTIONS
2037 // ----------------------------------------------------------------
2039 // Spatial and temporal scalability.
2040 DUMP_STRUCT_VALUE(oxcf, ss_number_layers);
2041 DUMP_STRUCT_VALUE(oxcf, ts_number_layers);
2043 // Bitrate allocation for spatial layers.
2044 // TODO(angiebird): dump layer_target_bitrate[VPX_MAX_LAYERS]
2045 // TODO(angiebird): dump ss_target_bitrate[VPX_SS_MAX_LAYERS]
2046 // TODO(angiebird): dump ss_enable_auto_arf[VPX_SS_MAX_LAYERS]
2047 // TODO(angiebird): dump ts_rate_decimator[VPX_TS_MAX_LAYERS]
2049 DUMP_STRUCT_VALUE(oxcf, enable_auto_arf);
2050 DUMP_STRUCT_VALUE(oxcf, encode_breakout);
2051 DUMP_STRUCT_VALUE(oxcf, error_resilient_mode);
2052 DUMP_STRUCT_VALUE(oxcf, frame_parallel_decoding_mode);
2054 DUMP_STRUCT_VALUE(oxcf, arnr_max_frames);
2055 DUMP_STRUCT_VALUE(oxcf, arnr_strength);
2057 DUMP_STRUCT_VALUE(oxcf, min_gf_interval);
2058 DUMP_STRUCT_VALUE(oxcf, max_gf_interval);
2060 DUMP_STRUCT_VALUE(oxcf, tile_columns);
2061 DUMP_STRUCT_VALUE(oxcf, tile_rows);
2063 DUMP_STRUCT_VALUE(oxcf, enable_tpl_model);
2065 DUMP_STRUCT_VALUE(oxcf, max_threads);
2067 DUMP_STRUCT_VALUE(oxcf, target_level);
2069 // TODO(angiebird): dump two_pass_stats_in
2071 #if CONFIG_FP_MB_STATS
2072 // TODO(angiebird): dump firstpass_mb_stats_in
2075 DUMP_STRUCT_VALUE(oxcf, tuning);
2076 DUMP_STRUCT_VALUE(oxcf, content);
2077 #if CONFIG_VP9_HIGHBITDEPTH
2078 DUMP_STRUCT_VALUE(oxcf, use_highbitdepth);
2080 DUMP_STRUCT_VALUE(oxcf, color_space);
2081 DUMP_STRUCT_VALUE(oxcf, color_range);
2082 DUMP_STRUCT_VALUE(oxcf, render_width);
2083 DUMP_STRUCT_VALUE(oxcf, render_height);
2084 DUMP_STRUCT_VALUE(oxcf, temporal_layering_mode);
2086 DUMP_STRUCT_VALUE(oxcf, row_mt);
2087 DUMP_STRUCT_VALUE(oxcf, motion_vector_unit_test);
2090 FRAME_INFO vp9_get_frame_info(const VP9EncoderConfig *oxcf) {
2091 FRAME_INFO frame_info;
2093 frame_info.frame_width = oxcf->width;
2094 frame_info.frame_height = oxcf->height;
2095 frame_info.render_frame_width = oxcf->width;
2096 frame_info.render_frame_height = oxcf->height;
2097 frame_info.bit_depth = oxcf->bit_depth;
2098 vp9_set_mi_size(&frame_info.mi_rows, &frame_info.mi_cols, &dummy,
2099 frame_info.frame_width, frame_info.frame_height);
2100 vp9_set_mb_size(&frame_info.mb_rows, &frame_info.mb_cols, &frame_info.num_mbs,
2101 frame_info.mi_rows, frame_info.mi_cols);
2102 // TODO(angiebird): Figure out how to get subsampling_x/y here
2106 void vp9_set_first_pass_stats(VP9EncoderConfig *oxcf,
2107 const vpx_fixed_buf_t *stats) {
2108 oxcf->two_pass_stats_in = *stats;