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 #ifndef VPX_VP9_ENCODER_VP9_ENCODER_H_ 12 #define VPX_VP9_ENCODER_VP9_ENCODER_H_ 13 14 #include <stdio.h> 15 16 #include "./vpx_config.h" 17 #include "vpx/internal/vpx_codec_internal.h" 18 #include "vpx/vp8cx.h" 19 #if CONFIG_INTERNAL_STATS 20 #include "vpx_dsp/ssim.h" 21 #endif 22 #include "vpx_dsp/variance.h" 23 #include "vpx_ports/system_state.h" 24 #include "vpx_util/vpx_thread.h" 25 26 #include "vp9/common/vp9_alloccommon.h" 27 #include "vp9/common/vp9_ppflags.h" 28 #include "vp9/common/vp9_entropymode.h" 29 #include "vp9/common/vp9_thread_common.h" 30 #include "vp9/common/vp9_onyxc_int.h" 31 32 #include "vp9/encoder/vp9_alt_ref_aq.h" 33 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" 34 #include "vp9/encoder/vp9_context_tree.h" 35 #include "vp9/encoder/vp9_encodemb.h" 36 #include "vp9/encoder/vp9_ethread.h" 37 #include "vp9/encoder/vp9_firstpass.h" 38 #include "vp9/encoder/vp9_job_queue.h" 39 #include "vp9/encoder/vp9_lookahead.h" 40 #include "vp9/encoder/vp9_mbgraph.h" 41 #include "vp9/encoder/vp9_mcomp.h" 42 #include "vp9/encoder/vp9_noise_estimate.h" 43 #include "vp9/encoder/vp9_quantize.h" 44 #include "vp9/encoder/vp9_ratectrl.h" 45 #include "vp9/encoder/vp9_rd.h" 46 #include "vp9/encoder/vp9_speed_features.h" 47 #include "vp9/encoder/vp9_svc_layercontext.h" 48 #include "vp9/encoder/vp9_tokenize.h" 49 50 #if CONFIG_VP9_TEMPORAL_DENOISING 51 #include "vp9/encoder/vp9_denoiser.h" 52 #endif 53 54 #ifdef __cplusplus 55 extern "C" { 56 #endif 57 58 // vp9 uses 10,000,000 ticks/second as time stamp 59 #define TICKS_PER_SEC 10000000 60 61 typedef struct { 62 int nmvjointcost[MV_JOINTS]; 63 int nmvcosts[2][MV_VALS]; 64 int nmvcosts_hp[2][MV_VALS]; 65 66 vpx_prob segment_pred_probs[PREDICTION_PROBS]; 67 68 unsigned char *last_frame_seg_map_copy; 69 70 // 0 = Intra, Last, GF, ARF 71 signed char last_ref_lf_deltas[MAX_REF_LF_DELTAS]; 72 // 0 = ZERO_MV, MV 73 signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS]; 74 75 FRAME_CONTEXT fc; 76 } CODING_CONTEXT; 77 78 typedef enum { 79 // encode_breakout is disabled. 80 ENCODE_BREAKOUT_DISABLED = 0, 81 // encode_breakout is enabled. 82 ENCODE_BREAKOUT_ENABLED = 1, 83 // encode_breakout is enabled with small max_thresh limit. 84 ENCODE_BREAKOUT_LIMITED = 2 85 } ENCODE_BREAKOUT_TYPE; 86 87 typedef enum { 88 NORMAL = 0, 89 FOURFIVE = 1, 90 THREEFIVE = 2, 91 ONETWO = 3 92 } VPX_SCALING; 93 94 typedef enum { 95 // Good Quality Fast Encoding. The encoder balances quality with the amount of 96 // time it takes to encode the output. Speed setting controls how fast. 97 GOOD, 98 99 // The encoder places priority on the quality of the output over encoding 100 // speed. The output is compressed at the highest possible quality. This 101 // option takes the longest amount of time to encode. Speed setting ignored. 102 BEST, 103 104 // Realtime/Live Encoding. This mode is optimized for realtime encoding (for 105 // example, capturing a television signal or feed from a live camera). Speed 106 // setting controls how fast. 107 REALTIME 108 } MODE; 109 110 typedef enum { 111 FRAMEFLAGS_KEY = 1 << 0, 112 FRAMEFLAGS_GOLDEN = 1 << 1, 113 FRAMEFLAGS_ALTREF = 1 << 2, 114 } FRAMETYPE_FLAGS; 115 116 typedef enum { 117 NO_AQ = 0, 118 VARIANCE_AQ = 1, 119 COMPLEXITY_AQ = 2, 120 CYCLIC_REFRESH_AQ = 3, 121 EQUATOR360_AQ = 4, 122 PSNR_AQ = 5, 123 // AQ based on lookahead temporal 124 // variance (only valid for altref frames) 125 LOOKAHEAD_AQ = 6, 126 AQ_MODE_COUNT // This should always be the last member of the enum 127 } AQ_MODE; 128 129 typedef enum { 130 RESIZE_NONE = 0, // No frame resizing allowed (except for SVC). 131 RESIZE_FIXED = 1, // All frames are coded at the specified dimension. 132 RESIZE_DYNAMIC = 2 // Coded size of each frame is determined by the codec. 133 } RESIZE_TYPE; 134 135 typedef enum { 136 kInvalid = 0, 137 kLowSadLowSumdiff = 1, 138 kLowSadHighSumdiff = 2, 139 kHighSadLowSumdiff = 3, 140 kHighSadHighSumdiff = 4, 141 kLowVarHighSumdiff = 5, 142 kVeryHighSad = 6, 143 } CONTENT_STATE_SB; 144 145 typedef struct VP9EncoderConfig { 146 BITSTREAM_PROFILE profile; 147 vpx_bit_depth_t bit_depth; // Codec bit-depth. 148 int width; // width of data passed to the compressor 149 int height; // height of data passed to the compressor 150 unsigned int input_bit_depth; // Input bit depth. 151 double init_framerate; // set to passed in framerate 152 int64_t target_bandwidth; // bandwidth to be used in bits per second 153 154 int noise_sensitivity; // pre processing blur: recommendation 0 155 int sharpness; // sharpening output: recommendation 0: 156 int speed; 157 // maximum allowed bitrate for any intra frame in % of bitrate target. 158 unsigned int rc_max_intra_bitrate_pct; 159 // maximum allowed bitrate for any inter frame in % of bitrate target. 160 unsigned int rc_max_inter_bitrate_pct; 161 // percent of rate boost for golden frame in CBR mode. 162 unsigned int gf_cbr_boost_pct; 163 164 MODE mode; 165 int pass; 166 167 // Key Framing Operations 168 int auto_key; // autodetect cut scenes and set the keyframes 169 int key_freq; // maximum distance to key frame. 170 171 int lag_in_frames; // how many frames lag before we start encoding 172 173 // ---------------------------------------------------------------- 174 // DATARATE CONTROL OPTIONS 175 176 // vbr, cbr, constrained quality or constant quality 177 enum vpx_rc_mode rc_mode; 178 179 // buffer targeting aggressiveness 180 int under_shoot_pct; 181 int over_shoot_pct; 182 183 // buffering parameters 184 int64_t starting_buffer_level_ms; 185 int64_t optimal_buffer_level_ms; 186 int64_t maximum_buffer_size_ms; 187 188 // Frame drop threshold. 189 int drop_frames_water_mark; 190 191 // controlling quality 192 int fixed_q; 193 int worst_allowed_q; 194 int best_allowed_q; 195 int cq_level; 196 AQ_MODE aq_mode; // Adaptive Quantization mode 197 198 // Special handling of Adaptive Quantization for AltRef frames 199 int alt_ref_aq; 200 201 // Internal frame size scaling. 202 RESIZE_TYPE resize_mode; 203 int scaled_frame_width; 204 int scaled_frame_height; 205 206 // Enable feature to reduce the frame quantization every x frames. 207 int frame_periodic_boost; 208 209 // two pass datarate control 210 int two_pass_vbrbias; // two pass datarate control tweaks 211 int two_pass_vbrmin_section; 212 int two_pass_vbrmax_section; 213 int vbr_corpus_complexity; // 0 indicates corpus vbr disabled 214 // END DATARATE CONTROL OPTIONS 215 // ---------------------------------------------------------------- 216 217 // Spatial and temporal scalability. 218 int ss_number_layers; // Number of spatial layers. 219 int ts_number_layers; // Number of temporal layers. 220 // Bitrate allocation for spatial layers. 221 int layer_target_bitrate[VPX_MAX_LAYERS]; 222 int ss_target_bitrate[VPX_SS_MAX_LAYERS]; 223 int ss_enable_auto_arf[VPX_SS_MAX_LAYERS]; 224 // Bitrate allocation (CBR mode) and framerate factor, for temporal layers. 225 int ts_rate_decimator[VPX_TS_MAX_LAYERS]; 226 227 int enable_auto_arf; 228 229 int encode_breakout; // early breakout : for video conf recommend 800 230 231 /* Bitfield defining the error resiliency features to enable. 232 * Can provide decodable frames after losses in previous 233 * frames and decodable partitions after losses in the same frame. 234 */ 235 unsigned int error_resilient_mode; 236 237 /* Bitfield defining the parallel decoding mode where the 238 * decoding in successive frames may be conducted in parallel 239 * just by decoding the frame headers. 240 */ 241 unsigned int frame_parallel_decoding_mode; 242 243 int arnr_max_frames; 244 int arnr_strength; 245 246 int min_gf_interval; 247 int max_gf_interval; 248 249 int tile_columns; 250 int tile_rows; 251 252 int enable_tpl_model; 253 254 int max_threads; 255 256 unsigned int target_level; 257 258 vpx_fixed_buf_t two_pass_stats_in; 259 struct vpx_codec_pkt_list *output_pkt_list; 260 261 #if CONFIG_FP_MB_STATS 262 vpx_fixed_buf_t firstpass_mb_stats_in; 263 #endif 264 265 vp8e_tuning tuning; 266 vp9e_tune_content content; 267 #if CONFIG_VP9_HIGHBITDEPTH 268 int use_highbitdepth; 269 #endif 270 vpx_color_space_t color_space; 271 vpx_color_range_t color_range; 272 int render_width; 273 int render_height; 274 VP9E_TEMPORAL_LAYERING_MODE temporal_layering_mode; 275 276 int row_mt; 277 unsigned int motion_vector_unit_test; 278 } VP9EncoderConfig; 279 280 static INLINE int is_lossless_requested(const VP9EncoderConfig *cfg) { 281 return cfg->best_allowed_q == 0 && cfg->worst_allowed_q == 0; 282 } 283 284 typedef struct TplDepStats { 285 int64_t intra_cost; 286 int64_t inter_cost; 287 int64_t mc_flow; 288 int64_t mc_dep_cost; 289 int64_t mc_ref_cost; 290 291 int ref_frame_index; 292 int_mv mv; 293 294 #if CONFIG_NON_GREEDY_MV 295 int ready[3]; 296 double mv_dist[3]; 297 double mv_cost[3]; 298 int64_t inter_cost_arr[3]; 299 int64_t recon_error_arr[3]; 300 int64_t sse_arr[3]; 301 double feature_score; 302 #endif 303 } TplDepStats; 304 305 #if CONFIG_NON_GREEDY_MV 306 #define SQUARE_BLOCK_SIZES 4 307 #endif 308 309 typedef struct TplDepFrame { 310 uint8_t is_valid; 311 TplDepStats *tpl_stats_ptr; 312 int stride; 313 int width; 314 int height; 315 int mi_rows; 316 int mi_cols; 317 int base_qindex; 318 #if CONFIG_NON_GREEDY_MV 319 double lambda; 320 double mv_dist_sum[3]; 321 double mv_cost_sum[3]; 322 int_mv *pyramid_mv_arr[3][SQUARE_BLOCK_SIZES]; 323 #endif 324 } TplDepFrame; 325 326 #if CONFIG_NON_GREEDY_MV 327 static INLINE int get_square_block_idx(BLOCK_SIZE bsize) { 328 if (bsize == BLOCK_4X4) { 329 return 0; 330 } 331 if (bsize == BLOCK_8X8) { 332 return 1; 333 } 334 if (bsize == BLOCK_16X16) { 335 return 2; 336 } 337 if (bsize == BLOCK_32X32) { 338 return 3; 339 } 340 printf("ERROR: non-square block size\n"); 341 assert(0); 342 return -1; 343 } 344 345 static INLINE BLOCK_SIZE square_block_idx_to_bsize(int square_block_idx) { 346 if (square_block_idx == 0) { 347 return BLOCK_4X4; 348 } 349 if (square_block_idx == 1) { 350 return BLOCK_8X8; 351 } 352 if (square_block_idx == 2) { 353 return BLOCK_16X16; 354 } 355 if (square_block_idx == 3) { 356 return BLOCK_32X32; 357 } 358 printf("ERROR: invalid square_block_idx\n"); 359 assert(0); 360 return BLOCK_INVALID; 361 } 362 363 static INLINE int_mv *get_pyramid_mv(const TplDepFrame *tpl_frame, int rf_idx, 364 BLOCK_SIZE bsize, int mi_row, int mi_col) { 365 return &tpl_frame->pyramid_mv_arr[rf_idx][get_square_block_idx(bsize)] 366 [mi_row * tpl_frame->stride + mi_col]; 367 } 368 #endif 369 370 #define TPL_DEP_COST_SCALE_LOG2 4 371 372 // TODO(jingning) All spatially adaptive variables should go to TileDataEnc. 373 typedef struct TileDataEnc { 374 TileInfo tile_info; 375 int thresh_freq_fact[BLOCK_SIZES][MAX_MODES]; 376 #if CONFIG_CONSISTENT_RECODE 377 int thresh_freq_fact_prev[BLOCK_SIZES][MAX_MODES]; 378 #endif 379 int8_t mode_map[BLOCK_SIZES][MAX_MODES]; 380 FIRSTPASS_DATA fp_data; 381 VP9RowMTSync row_mt_sync; 382 383 // Used for adaptive_rd_thresh with row multithreading 384 int *row_base_thresh_freq_fact; 385 } TileDataEnc; 386 387 typedef struct RowMTInfo { 388 JobQueueHandle job_queue_hdl; 389 #if CONFIG_MULTITHREAD 390 pthread_mutex_t job_mutex; 391 #endif 392 } RowMTInfo; 393 394 typedef struct { 395 TOKENEXTRA *start; 396 TOKENEXTRA *stop; 397 unsigned int count; 398 } TOKENLIST; 399 400 typedef struct MultiThreadHandle { 401 int allocated_tile_rows; 402 int allocated_tile_cols; 403 int allocated_vert_unit_rows; 404 405 // Frame level params 406 int num_tile_vert_sbs[MAX_NUM_TILE_ROWS]; 407 408 // Job Queue structure and handles 409 JobQueue *job_queue; 410 411 int jobs_per_tile_col; 412 413 RowMTInfo row_mt_info[MAX_NUM_TILE_COLS]; 414 int thread_id_to_tile_id[MAX_NUM_THREADS]; // Mapping of threads to tiles 415 } MultiThreadHandle; 416 417 typedef struct RD_COUNTS { 418 vp9_coeff_count coef_counts[TX_SIZES][PLANE_TYPES]; 419 int64_t comp_pred_diff[REFERENCE_MODES]; 420 int64_t filter_diff[SWITCHABLE_FILTER_CONTEXTS]; 421 } RD_COUNTS; 422 423 typedef struct ThreadData { 424 MACROBLOCK mb; 425 RD_COUNTS rd_counts; 426 FRAME_COUNTS *counts; 427 428 PICK_MODE_CONTEXT *leaf_tree; 429 PC_TREE *pc_tree; 430 PC_TREE *pc_root; 431 } ThreadData; 432 433 struct EncWorkerData; 434 435 typedef struct ActiveMap { 436 int enabled; 437 int update; 438 unsigned char *map; 439 } ActiveMap; 440 441 typedef enum { Y, U, V, ALL } STAT_TYPE; 442 443 typedef struct IMAGE_STAT { 444 double stat[ALL + 1]; 445 double worst; 446 } ImageStat; 447 448 // Kf noise filtering currently disabled by default in build. 449 // #define ENABLE_KF_DENOISE 1 450 451 #define CPB_WINDOW_SIZE 4 452 #define FRAME_WINDOW_SIZE 128 453 #define SAMPLE_RATE_GRACE_P 0.015 454 #define VP9_LEVELS 14 455 456 typedef enum { 457 LEVEL_UNKNOWN = 0, 458 LEVEL_AUTO = 1, 459 LEVEL_1 = 10, 460 LEVEL_1_1 = 11, 461 LEVEL_2 = 20, 462 LEVEL_2_1 = 21, 463 LEVEL_3 = 30, 464 LEVEL_3_1 = 31, 465 LEVEL_4 = 40, 466 LEVEL_4_1 = 41, 467 LEVEL_5 = 50, 468 LEVEL_5_1 = 51, 469 LEVEL_5_2 = 52, 470 LEVEL_6 = 60, 471 LEVEL_6_1 = 61, 472 LEVEL_6_2 = 62, 473 LEVEL_MAX = 255 474 } VP9_LEVEL; 475 476 typedef struct { 477 VP9_LEVEL level; 478 uint64_t max_luma_sample_rate; 479 uint32_t max_luma_picture_size; 480 uint32_t max_luma_picture_breadth; 481 double average_bitrate; // in kilobits per second 482 double max_cpb_size; // in kilobits 483 double compression_ratio; 484 uint8_t max_col_tiles; 485 uint32_t min_altref_distance; 486 uint8_t max_ref_frame_buffers; 487 } Vp9LevelSpec; 488 489 extern const Vp9LevelSpec vp9_level_defs[VP9_LEVELS]; 490 491 typedef struct { 492 int64_t ts; // timestamp 493 uint32_t luma_samples; 494 uint32_t size; // in bytes 495 } FrameRecord; 496 497 typedef struct { 498 FrameRecord buf[FRAME_WINDOW_SIZE]; 499 uint8_t start; 500 uint8_t len; 501 } FrameWindowBuffer; 502 503 typedef struct { 504 uint8_t seen_first_altref; 505 uint32_t frames_since_last_altref; 506 uint64_t total_compressed_size; 507 uint64_t total_uncompressed_size; 508 double time_encoded; // in seconds 509 FrameWindowBuffer frame_window_buffer; 510 int ref_refresh_map; 511 } Vp9LevelStats; 512 513 typedef struct { 514 Vp9LevelStats level_stats; 515 Vp9LevelSpec level_spec; 516 } Vp9LevelInfo; 517 518 typedef enum { 519 BITRATE_TOO_LARGE = 0, 520 LUMA_PIC_SIZE_TOO_LARGE, 521 LUMA_PIC_BREADTH_TOO_LARGE, 522 LUMA_SAMPLE_RATE_TOO_LARGE, 523 CPB_TOO_LARGE, 524 COMPRESSION_RATIO_TOO_SMALL, 525 TOO_MANY_COLUMN_TILE, 526 ALTREF_DIST_TOO_SMALL, 527 TOO_MANY_REF_BUFFER, 528 TARGET_LEVEL_FAIL_IDS 529 } TARGET_LEVEL_FAIL_ID; 530 531 typedef struct { 532 int8_t level_index; 533 uint8_t rc_config_updated; 534 uint8_t fail_flag; 535 int max_frame_size; // in bits 536 double max_cpb_size; // in bits 537 } LevelConstraint; 538 539 typedef struct ARNRFilterData { 540 YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS]; 541 int strength; 542 int frame_count; 543 int alt_ref_index; 544 struct scale_factors sf; 545 } ARNRFilterData; 546 547 typedef struct EncFrameBuf { 548 int mem_valid; 549 int released; 550 YV12_BUFFER_CONFIG frame; 551 } EncFrameBuf; 552 553 // Maximum operating frame buffer size needed for a GOP using ARF reference. 554 #define MAX_ARF_GOP_SIZE (2 * MAX_LAG_BUFFERS) 555 #if CONFIG_NON_GREEDY_MV 556 typedef struct FEATURE_SCORE_LOC { 557 int visited; 558 double feature_score; 559 int mi_row; 560 int mi_col; 561 } FEATURE_SCORE_LOC; 562 #endif 563 564 typedef struct VP9_COMP { 565 QUANTS quants; 566 ThreadData td; 567 MB_MODE_INFO_EXT *mbmi_ext_base; 568 DECLARE_ALIGNED(16, int16_t, y_dequant[QINDEX_RANGE][8]); 569 DECLARE_ALIGNED(16, int16_t, uv_dequant[QINDEX_RANGE][8]); 570 VP9_COMMON common; 571 VP9EncoderConfig oxcf; 572 struct lookahead_ctx *lookahead; 573 struct lookahead_entry *alt_ref_source; 574 575 YV12_BUFFER_CONFIG *Source; 576 YV12_BUFFER_CONFIG *Last_Source; // NULL for first frame and alt_ref frames 577 YV12_BUFFER_CONFIG *un_scaled_source; 578 YV12_BUFFER_CONFIG scaled_source; 579 YV12_BUFFER_CONFIG *unscaled_last_source; 580 YV12_BUFFER_CONFIG scaled_last_source; 581 #ifdef ENABLE_KF_DENOISE 582 YV12_BUFFER_CONFIG raw_unscaled_source; 583 YV12_BUFFER_CONFIG raw_scaled_source; 584 #endif 585 YV12_BUFFER_CONFIG *raw_source_frame; 586 587 TplDepFrame tpl_stats[MAX_ARF_GOP_SIZE]; 588 YV12_BUFFER_CONFIG *tpl_recon_frames[REF_FRAMES]; 589 EncFrameBuf enc_frame_buf[REF_FRAMES]; 590 #if CONFIG_NON_GREEDY_MV 591 int feature_score_loc_alloc; 592 FEATURE_SCORE_LOC *feature_score_loc_arr; 593 FEATURE_SCORE_LOC **feature_score_loc_sort; 594 FEATURE_SCORE_LOC **feature_score_loc_heap; 595 #endif 596 597 TileDataEnc *tile_data; 598 int allocated_tiles; // Keep track of memory allocated for tiles. 599 600 // For a still frame, this flag is set to 1 to skip partition search. 601 int partition_search_skippable_frame; 602 603 int scaled_ref_idx[REFS_PER_FRAME]; 604 int lst_fb_idx; 605 int gld_fb_idx; 606 int alt_fb_idx; 607 608 int ref_fb_idx[REF_FRAMES]; 609 610 int refresh_last_frame; 611 int refresh_golden_frame; 612 int refresh_alt_ref_frame; 613 614 int ext_refresh_frame_flags_pending; 615 int ext_refresh_last_frame; 616 int ext_refresh_golden_frame; 617 int ext_refresh_alt_ref_frame; 618 619 int ext_refresh_frame_context_pending; 620 int ext_refresh_frame_context; 621 622 YV12_BUFFER_CONFIG last_frame_uf; 623 624 TOKENEXTRA *tile_tok[4][1 << 6]; 625 TOKENLIST *tplist[4][1 << 6]; 626 627 // Ambient reconstruction err target for force key frames 628 int64_t ambient_err; 629 630 RD_OPT rd; 631 632 CODING_CONTEXT coding_context; 633 634 int *nmvcosts[2]; 635 int *nmvcosts_hp[2]; 636 int *nmvsadcosts[2]; 637 int *nmvsadcosts_hp[2]; 638 639 int64_t last_time_stamp_seen; 640 int64_t last_end_time_stamp_seen; 641 int64_t first_time_stamp_ever; 642 643 RATE_CONTROL rc; 644 double framerate; 645 646 int interp_filter_selected[REF_FRAMES][SWITCHABLE]; 647 648 struct vpx_codec_pkt_list *output_pkt_list; 649 650 MBGRAPH_FRAME_STATS mbgraph_stats[MAX_LAG_BUFFERS]; 651 int mbgraph_n_frames; // number of frames filled in the above 652 int static_mb_pct; // % forced skip mbs by segmentation 653 int ref_frame_flags; 654 655 SPEED_FEATURES sf; 656 657 uint32_t max_mv_magnitude; 658 int mv_step_param; 659 660 int allow_comp_inter_inter; 661 662 // Default value is 1. From first pass stats, encode_breakout may be disabled. 663 ENCODE_BREAKOUT_TYPE allow_encode_breakout; 664 665 // Get threshold from external input. A suggested threshold is 800 for HD 666 // clips, and 300 for < HD clips. 667 int encode_breakout; 668 669 uint8_t *segmentation_map; 670 671 uint8_t *skin_map; 672 673 // segment threashold for encode breakout 674 int segment_encode_breakout[MAX_SEGMENTS]; 675 676 CYCLIC_REFRESH *cyclic_refresh; 677 ActiveMap active_map; 678 679 fractional_mv_step_fp *find_fractional_mv_step; 680 struct scale_factors me_sf; 681 vp9_diamond_search_fn_t diamond_search_sad; 682 vp9_variance_fn_ptr_t fn_ptr[BLOCK_SIZES]; 683 uint64_t time_receive_data; 684 uint64_t time_compress_data; 685 uint64_t time_pick_lpf; 686 uint64_t time_encode_sb_row; 687 688 #if CONFIG_FP_MB_STATS 689 int use_fp_mb_stats; 690 #endif 691 692 TWO_PASS twopass; 693 694 // Force recalculation of segment_ids for each mode info 695 uint8_t force_update_segmentation; 696 697 YV12_BUFFER_CONFIG alt_ref_buffer; 698 699 // class responsible for adaptive 700 // quantization of altref frames 701 struct ALT_REF_AQ *alt_ref_aq; 702 703 #if CONFIG_INTERNAL_STATS 704 unsigned int mode_chosen_counts[MAX_MODES]; 705 706 int count; 707 uint64_t total_sq_error; 708 uint64_t total_samples; 709 ImageStat psnr; 710 711 uint64_t totalp_sq_error; 712 uint64_t totalp_samples; 713 ImageStat psnrp; 714 715 double total_blockiness; 716 double worst_blockiness; 717 718 int bytes; 719 double summed_quality; 720 double summed_weights; 721 double summedp_quality; 722 double summedp_weights; 723 unsigned int tot_recode_hits; 724 double worst_ssim; 725 726 ImageStat ssimg; 727 ImageStat fastssim; 728 ImageStat psnrhvs; 729 730 int b_calculate_ssimg; 731 int b_calculate_blockiness; 732 733 int b_calculate_consistency; 734 735 double total_inconsistency; 736 double worst_consistency; 737 Ssimv *ssim_vars; 738 Metrics metrics; 739 #endif 740 int b_calculate_psnr; 741 742 int droppable; 743 744 int initial_width; 745 int initial_height; 746 int initial_mbs; // Number of MBs in the full-size frame; to be used to 747 // normalize the firstpass stats. This will differ from the 748 // number of MBs in the current frame when the frame is 749 // scaled. 750 751 int use_svc; 752 753 SVC svc; 754 755 // Store frame variance info in SOURCE_VAR_BASED_PARTITION search type. 756 diff *source_diff_var; 757 // The threshold used in SOURCE_VAR_BASED_PARTITION search type. 758 unsigned int source_var_thresh; 759 int frames_till_next_var_check; 760 761 int frame_flags; 762 763 search_site_config ss_cfg; 764 765 int mbmode_cost[INTRA_MODES]; 766 unsigned int inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES]; 767 int intra_uv_mode_cost[FRAME_TYPES][INTRA_MODES][INTRA_MODES]; 768 int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES]; 769 int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS]; 770 int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES]; 771 // Indices are: max_tx_size-1, tx_size_ctx, tx_size 772 int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES]; 773 774 #if CONFIG_VP9_TEMPORAL_DENOISING 775 VP9_DENOISER denoiser; 776 #endif 777 778 int resize_pending; 779 RESIZE_STATE resize_state; 780 int external_resize; 781 int resize_scale_num; 782 int resize_scale_den; 783 int resize_avg_qp; 784 int resize_buffer_underflow; 785 int resize_count; 786 787 int use_skin_detection; 788 789 int target_level; 790 791 NOISE_ESTIMATE noise_estimate; 792 793 // Count on how many consecutive times a block uses small/zeromv for encoding. 794 uint8_t *consec_zero_mv; 795 796 // VAR_BASED_PARTITION thresholds 797 // 0 - threshold_64x64; 1 - threshold_32x32; 798 // 2 - threshold_16x16; 3 - vbp_threshold_8x8; 799 int64_t vbp_thresholds[4]; 800 int64_t vbp_threshold_minmax; 801 int64_t vbp_threshold_sad; 802 // Threshold used for partition copy 803 int64_t vbp_threshold_copy; 804 BLOCK_SIZE vbp_bsize_min; 805 806 // Multi-threading 807 int num_workers; 808 VPxWorker *workers; 809 struct EncWorkerData *tile_thr_data; 810 VP9LfSync lf_row_sync; 811 struct VP9BitstreamWorkerData *vp9_bitstream_worker_data; 812 813 int keep_level_stats; 814 Vp9LevelInfo level_info; 815 MultiThreadHandle multi_thread_ctxt; 816 void (*row_mt_sync_read_ptr)(VP9RowMTSync *const, int, int); 817 void (*row_mt_sync_write_ptr)(VP9RowMTSync *const, int, int, const int); 818 ARNRFilterData arnr_filter_data; 819 820 int row_mt; 821 unsigned int row_mt_bit_exact; 822 823 // Previous Partition Info 824 BLOCK_SIZE *prev_partition; 825 int8_t *prev_segment_id; 826 // Used to save the status of whether a block has a low variance in 827 // choose_partitioning. 0 for 64x64, 1~2 for 64x32, 3~4 for 32x64, 5~8 for 828 // 32x32, 9~24 for 16x16. 829 // This is for the last frame and is copied to the current frame 830 // when partition copy happens. 831 uint8_t *prev_variance_low; 832 uint8_t *copied_frame_cnt; 833 uint8_t max_copied_frame; 834 // If the last frame is dropped, we don't copy partition. 835 uint8_t last_frame_dropped; 836 837 // For each superblock: keeps track of the last time (in frame distance) the 838 // the superblock did not have low source sad. 839 uint8_t *content_state_sb_fd; 840 841 int compute_source_sad_onepass; 842 843 LevelConstraint level_constraint; 844 845 uint8_t *count_arf_frame_usage; 846 uint8_t *count_lastgolden_frame_usage; 847 848 int multi_layer_arf; 849 vpx_roi_map_t roi; 850 } VP9_COMP; 851 852 void vp9_initialize_enc(void); 853 854 struct VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf, 855 BufferPool *const pool); 856 void vp9_remove_compressor(VP9_COMP *cpi); 857 858 void vp9_change_config(VP9_COMP *cpi, const VP9EncoderConfig *oxcf); 859 860 // receive a frames worth of data. caller can assume that a copy of this 861 // frame is made and not just a copy of the pointer.. 862 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags, 863 YV12_BUFFER_CONFIG *sd, int64_t time_stamp, 864 int64_t end_time); 865 866 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags, 867 size_t *size, uint8_t *dest, int64_t *time_stamp, 868 int64_t *time_end, int flush); 869 870 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest, 871 vp9_ppflags_t *flags); 872 873 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags); 874 875 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags); 876 877 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag, 878 YV12_BUFFER_CONFIG *sd); 879 880 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag, 881 YV12_BUFFER_CONFIG *sd); 882 883 int vp9_update_entropy(VP9_COMP *cpi, int update); 884 885 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows, 886 int cols); 887 888 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows, 889 int cols); 890 891 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode, 892 VPX_SCALING vert_mode); 893 894 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width, 895 unsigned int height); 896 897 void vp9_set_svc(VP9_COMP *cpi, int use_svc); 898 899 static INLINE int stack_pop(int *stack, int stack_size) { 900 int idx; 901 const int r = stack[0]; 902 for (idx = 1; idx < stack_size; ++idx) stack[idx - 1] = stack[idx]; 903 904 return r; 905 } 906 907 static INLINE int stack_top(const int *stack) { return stack[0]; } 908 909 static INLINE void stack_push(int *stack, int new_item, int stack_size) { 910 int idx; 911 for (idx = stack_size; idx > 0; --idx) stack[idx] = stack[idx - 1]; 912 stack[0] = new_item; 913 } 914 915 static INLINE void stack_init(int *stack, int length) { 916 int idx; 917 for (idx = 0; idx < length; ++idx) stack[idx] = -1; 918 } 919 920 int vp9_get_quantizer(struct VP9_COMP *cpi); 921 922 static INLINE int frame_is_kf_gf_arf(const VP9_COMP *cpi) { 923 return frame_is_intra_only(&cpi->common) || cpi->refresh_alt_ref_frame || 924 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref); 925 } 926 927 static INLINE int get_ref_frame_map_idx(const VP9_COMP *cpi, 928 MV_REFERENCE_FRAME ref_frame) { 929 if (ref_frame == LAST_FRAME) { 930 return cpi->lst_fb_idx; 931 } else if (ref_frame == GOLDEN_FRAME) { 932 return cpi->gld_fb_idx; 933 } else { 934 return cpi->alt_fb_idx; 935 } 936 } 937 938 static INLINE int get_ref_frame_buf_idx(const VP9_COMP *const cpi, 939 int ref_frame) { 940 const VP9_COMMON *const cm = &cpi->common; 941 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame); 942 return (map_idx != INVALID_IDX) ? cm->ref_frame_map[map_idx] : INVALID_IDX; 943 } 944 945 static INLINE RefCntBuffer *get_ref_cnt_buffer(VP9_COMMON *cm, int fb_idx) { 946 return fb_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[fb_idx] : NULL; 947 } 948 949 static INLINE YV12_BUFFER_CONFIG *get_ref_frame_buffer( 950 VP9_COMP *cpi, MV_REFERENCE_FRAME ref_frame) { 951 VP9_COMMON *const cm = &cpi->common; 952 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame); 953 return buf_idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[buf_idx].buf 954 : NULL; 955 } 956 957 static INLINE int get_token_alloc(int mb_rows, int mb_cols) { 958 // TODO(JBB): double check we can't exceed this token count if we have a 959 // 32x32 transform crossing a boundary at a multiple of 16. 960 // mb_rows, cols are in units of 16 pixels. We assume 3 planes all at full 961 // resolution. We assume up to 1 token per pixel, and then allow 962 // a head room of 4. 963 return mb_rows * mb_cols * (16 * 16 * 3 + 4); 964 } 965 966 // Get the allocated token size for a tile. It does the same calculation as in 967 // the frame token allocation. 968 static INLINE int allocated_tokens(TileInfo tile) { 969 int tile_mb_rows = (tile.mi_row_end - tile.mi_row_start + 1) >> 1; 970 int tile_mb_cols = (tile.mi_col_end - tile.mi_col_start + 1) >> 1; 971 972 return get_token_alloc(tile_mb_rows, tile_mb_cols); 973 } 974 975 static INLINE void get_start_tok(VP9_COMP *cpi, int tile_row, int tile_col, 976 int mi_row, TOKENEXTRA **tok) { 977 VP9_COMMON *const cm = &cpi->common; 978 const int tile_cols = 1 << cm->log2_tile_cols; 979 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col]; 980 const TileInfo *const tile_info = &this_tile->tile_info; 981 982 int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1; 983 const int mb_row = (mi_row - tile_info->mi_row_start) >> 1; 984 985 *tok = 986 cpi->tile_tok[tile_row][tile_col] + get_token_alloc(mb_row, tile_mb_cols); 987 } 988 989 int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b); 990 #if CONFIG_VP9_HIGHBITDEPTH 991 int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a, 992 const YV12_BUFFER_CONFIG *b); 993 #endif // CONFIG_VP9_HIGHBITDEPTH 994 995 void vp9_scale_references(VP9_COMP *cpi); 996 997 void vp9_update_reference_frames(VP9_COMP *cpi); 998 999 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv); 1000 1001 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale( 1002 VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled, 1003 YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type, 1004 int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2); 1005 1006 YV12_BUFFER_CONFIG *vp9_scale_if_required( 1007 VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled, 1008 int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler); 1009 1010 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags); 1011 1012 static INLINE int is_one_pass_cbr_svc(const struct VP9_COMP *const cpi) { 1013 return (cpi->use_svc && cpi->oxcf.pass == 0); 1014 } 1015 1016 #if CONFIG_VP9_TEMPORAL_DENOISING 1017 static INLINE int denoise_svc(const struct VP9_COMP *const cpi) { 1018 return (!cpi->use_svc || (cpi->use_svc && cpi->svc.spatial_layer_id >= 1019 cpi->svc.first_layer_denoise)); 1020 } 1021 #endif 1022 1023 #define MIN_LOOKAHEAD_FOR_ARFS 4 1024 static INLINE int is_altref_enabled(const VP9_COMP *const cpi) { 1025 return !(cpi->oxcf.mode == REALTIME && cpi->oxcf.rc_mode == VPX_CBR) && 1026 cpi->oxcf.lag_in_frames >= MIN_LOOKAHEAD_FOR_ARFS && 1027 cpi->oxcf.enable_auto_arf; 1028 } 1029 1030 static INLINE void set_ref_ptrs(VP9_COMMON *cm, MACROBLOCKD *xd, 1031 MV_REFERENCE_FRAME ref0, 1032 MV_REFERENCE_FRAME ref1) { 1033 xd->block_refs[0] = 1034 &cm->frame_refs[ref0 >= LAST_FRAME ? ref0 - LAST_FRAME : 0]; 1035 xd->block_refs[1] = 1036 &cm->frame_refs[ref1 >= LAST_FRAME ? ref1 - LAST_FRAME : 0]; 1037 } 1038 1039 static INLINE int get_chessboard_index(const int frame_index) { 1040 return frame_index & 0x1; 1041 } 1042 1043 static INLINE int *cond_cost_list(const struct VP9_COMP *cpi, int *cost_list) { 1044 return cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? cost_list : NULL; 1045 } 1046 1047 static INLINE int get_num_vert_units(TileInfo tile, int shift) { 1048 int num_vert_units = 1049 (tile.mi_row_end - tile.mi_row_start + (1 << shift) - 1) >> shift; 1050 return num_vert_units; 1051 } 1052 1053 static INLINE int get_num_cols(TileInfo tile, int shift) { 1054 int num_cols = 1055 (tile.mi_col_end - tile.mi_col_start + (1 << shift) - 1) >> shift; 1056 return num_cols; 1057 } 1058 1059 static INLINE int get_level_index(VP9_LEVEL level) { 1060 int i; 1061 for (i = 0; i < VP9_LEVELS; ++i) { 1062 if (level == vp9_level_defs[i].level) return i; 1063 } 1064 return -1; 1065 } 1066 1067 // Return the log2 value of max column tiles corresponding to the level that 1068 // the picture size fits into. 1069 static INLINE int log_tile_cols_from_picsize_level(uint32_t width, 1070 uint32_t height) { 1071 int i; 1072 const uint32_t pic_size = width * height; 1073 const uint32_t pic_breadth = VPXMAX(width, height); 1074 for (i = LEVEL_1; i < LEVEL_MAX; ++i) { 1075 if (vp9_level_defs[i].max_luma_picture_size >= pic_size && 1076 vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) { 1077 return get_msb(vp9_level_defs[i].max_col_tiles); 1078 } 1079 } 1080 return INT_MAX; 1081 } 1082 1083 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec); 1084 1085 int vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map, unsigned int rows, 1086 unsigned int cols, int delta_q[8], int delta_lf[8], 1087 int skip[8], int ref_frame[8]); 1088 1089 void vp9_new_framerate(VP9_COMP *cpi, double framerate); 1090 1091 void vp9_set_row_mt(VP9_COMP *cpi); 1092 1093 #define LAYER_IDS_TO_IDX(sl, tl, num_tl) ((sl) * (num_tl) + (tl)) 1094 1095 #ifdef __cplusplus 1096 } // extern "C" 1097 #endif 1098 1099 #endif // VPX_VP9_ENCODER_VP9_ENCODER_H_ 1100