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