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 12 #ifndef __INC_VP8_INT_H 13 #define __INC_VP8_INT_H 14 15 #include <stdio.h> 16 #include "vpx_ports/config.h" 17 #include "onyx.h" 18 #include "treewriter.h" 19 #include "tokenize.h" 20 #include "onyxc_int.h" 21 #include "preproc.h" 22 #include "variance.h" 23 #include "dct.h" 24 #include "encodemb.h" 25 #include "quantize.h" 26 #include "entropy.h" 27 #include "threading.h" 28 #include "vpx_ports/mem.h" 29 #include "vpx/internal/vpx_codec_internal.h" 30 #include "mcomp.h" 31 32 #define INTRARDOPT 33 //#define SPEEDSTATS 1 34 #define MIN_GF_INTERVAL 4 35 #define DEFAULT_GF_INTERVAL 7 36 37 #define KEY_FRAME_CONTEXT 5 38 39 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25) 40 41 #define AF_THRESH 25 42 #define AF_THRESH2 100 43 #define ARF_DECAY_THRESH 12 44 #define MAX_MODES 20 45 46 #define MIN_THRESHMULT 32 47 #define MAX_THRESHMULT 512 48 49 #define GF_ZEROMV_ZBIN_BOOST 24 50 #define ZBIN_OQ_MAX 192 51 52 #define VP8_TEMPORAL_ALT_REF 1 53 54 typedef struct 55 { 56 int kf_indicated; 57 unsigned int frames_since_key; 58 unsigned int frames_since_golden; 59 int filter_level; 60 int frames_till_gf_update_due; 61 int recent_ref_frame_usage[MAX_REF_FRAMES]; 62 63 MV_CONTEXT mvc[2]; 64 int mvcosts[2][MVvals+1]; 65 66 #ifdef MODE_STATS 67 // Stats 68 int y_modes[5]; 69 int uv_modes[4]; 70 int b_modes[10]; 71 int inter_y_modes[10]; 72 int inter_uv_modes[4]; 73 int inter_b_modes[10]; 74 #endif 75 76 vp8_prob ymode_prob[4], uv_mode_prob[3]; /* interframe intra mode probs */ 77 vp8_prob kf_ymode_prob[4], kf_uv_mode_prob[3]; /* keyframe "" */ 78 79 int ymode_count[5], uv_mode_count[4]; /* intra MB type cts this frame */ 80 81 int count_mb_ref_frame_usage[MAX_REF_FRAMES]; 82 83 int this_frame_percent_intra; 84 int last_frame_percent_intra; 85 86 87 } CODING_CONTEXT; 88 89 typedef struct 90 { 91 double frame; 92 double intra_error; 93 double coded_error; 94 double ssim_weighted_pred_err; 95 double pcnt_inter; 96 double pcnt_motion; 97 double pcnt_second_ref; 98 double MVr; 99 double mvr_abs; 100 double MVc; 101 double mvc_abs; 102 double MVrv; 103 double MVcv; 104 double mv_in_out_count; 105 double duration; 106 double count; 107 } 108 FIRSTPASS_STATS; 109 110 typedef struct 111 { 112 int frames_so_far; 113 double frame_intra_error; 114 double frame_coded_error; 115 double frame_pcnt_inter; 116 double frame_pcnt_motion; 117 double frame_mvr; 118 double frame_mvr_abs; 119 double frame_mvc; 120 double frame_mvc_abs; 121 122 } ONEPASS_FRAMESTATS; 123 124 125 typedef enum 126 { 127 THR_ZEROMV = 0, 128 THR_DC = 1, 129 130 THR_NEARESTMV = 2, 131 THR_NEARMV = 3, 132 133 THR_ZEROG = 4, 134 THR_NEARESTG = 5, 135 136 THR_ZEROA = 6, 137 THR_NEARESTA = 7, 138 139 THR_NEARG = 8, 140 THR_NEARA = 9, 141 142 THR_V_PRED = 10, 143 THR_H_PRED = 11, 144 THR_TM = 12, 145 146 THR_NEWMV = 13, 147 THR_NEWG = 14, 148 THR_NEWA = 15, 149 150 THR_SPLITMV = 16, 151 THR_SPLITG = 17, 152 THR_SPLITA = 18, 153 154 THR_B_PRED = 19, 155 } 156 THR_MODES; 157 158 typedef enum 159 { 160 DIAMOND = 0, 161 NSTEP = 1, 162 HEX = 2 163 } SEARCH_METHODS; 164 165 typedef struct 166 { 167 int RD; 168 SEARCH_METHODS search_method; 169 int improved_quant; 170 int improved_dct; 171 int auto_filter; 172 int recode_loop; 173 int iterative_sub_pixel; 174 int half_pixel_search; 175 int quarter_pixel_search; 176 int thresh_mult[MAX_MODES]; 177 int full_freq[2]; 178 int min_fs_radius; 179 int max_fs_radius; 180 int max_step_search_steps; 181 int first_step; 182 int optimize_coefficients; 183 184 } SPEED_FEATURES; 185 186 typedef struct 187 { 188 MACROBLOCK mb; 189 int mb_row; 190 TOKENEXTRA *tp; 191 int segment_counts[MAX_MB_SEGMENTS]; 192 int totalrate; 193 int current_mb_col; 194 } MB_ROW_COMP; 195 196 typedef struct 197 { 198 TOKENEXTRA *start; 199 TOKENEXTRA *stop; 200 } TOKENLIST; 201 202 typedef struct 203 { 204 int ithread; 205 void *ptr1; 206 void *ptr2; 207 } ENCODETHREAD_DATA; 208 typedef struct 209 { 210 int ithread; 211 void *ptr1; 212 } LPFTHREAD_DATA; 213 214 typedef struct 215 { 216 INT64 source_time_stamp; 217 INT64 source_end_time_stamp; 218 219 DECLARE_ALIGNED(16, YV12_BUFFER_CONFIG, source_buffer); 220 unsigned int source_frame_flags; 221 } SOURCE_SAMPLE; 222 223 typedef struct VP8_ENCODER_RTCD 224 { 225 VP8_COMMON_RTCD *common; 226 vp8_variance_rtcd_vtable_t variance; 227 vp8_fdct_rtcd_vtable_t fdct; 228 vp8_encodemb_rtcd_vtable_t encodemb; 229 vp8_quantize_rtcd_vtable_t quantize; 230 vp8_search_rtcd_vtable_t search; 231 } VP8_ENCODER_RTCD; 232 233 typedef struct 234 { 235 236 DECLARE_ALIGNED(16, short, Y1quant[QINDEX_RANGE][4][4]); 237 DECLARE_ALIGNED(16, short, Y1quant_shift[QINDEX_RANGE][4][4]); 238 DECLARE_ALIGNED(16, short, Y1zbin[QINDEX_RANGE][4][4]); 239 DECLARE_ALIGNED(16, short, Y1round[QINDEX_RANGE][4][4]); 240 241 DECLARE_ALIGNED(16, short, Y2quant[QINDEX_RANGE][4][4]); 242 DECLARE_ALIGNED(16, short, Y2quant_shift[QINDEX_RANGE][4][4]); 243 DECLARE_ALIGNED(16, short, Y2zbin[QINDEX_RANGE][4][4]); 244 DECLARE_ALIGNED(16, short, Y2round[QINDEX_RANGE][4][4]); 245 246 DECLARE_ALIGNED(16, short, UVquant[QINDEX_RANGE][4][4]); 247 DECLARE_ALIGNED(16, short, UVquant_shift[QINDEX_RANGE][4][4]); 248 DECLARE_ALIGNED(16, short, UVzbin[QINDEX_RANGE][4][4]); 249 DECLARE_ALIGNED(16, short, UVround[QINDEX_RANGE][4][4]); 250 251 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y1[QINDEX_RANGE][16]); 252 DECLARE_ALIGNED(16, short, zrun_zbin_boost_y2[QINDEX_RANGE][16]); 253 DECLARE_ALIGNED(16, short, zrun_zbin_boost_uv[QINDEX_RANGE][16]); 254 255 256 MACROBLOCK mb; 257 VP8_COMMON common; 258 vp8_writer bc, bc2; 259 // bool_writer *bc2; 260 261 VP8_CONFIG oxcf; 262 263 YV12_BUFFER_CONFIG *Source; 264 YV12_BUFFER_CONFIG *un_scaled_source; 265 INT64 source_time_stamp; 266 INT64 source_end_time_stamp; 267 unsigned int source_frame_flags; 268 YV12_BUFFER_CONFIG scaled_source; 269 270 int source_buffer_count; 271 int source_encode_index; 272 int source_alt_ref_pending; 273 int source_alt_ref_active; 274 275 int last_alt_ref_sei; 276 int is_src_frame_alt_ref; 277 278 int gold_is_last; // golden frame same as last frame ( short circuit gold searches) 279 int alt_is_last; // Alt reference frame same as last ( short circuit altref search) 280 int gold_is_alt; // don't do both alt and gold search ( just do gold). 281 282 //int refresh_alt_ref_frame; 283 SOURCE_SAMPLE src_buffer[MAX_LAG_BUFFERS]; 284 285 YV12_BUFFER_CONFIG last_frame_uf; 286 287 char *Dest; 288 289 TOKENEXTRA *tok; 290 unsigned int tok_count; 291 292 293 unsigned int frames_since_key; 294 unsigned int key_frame_frequency; 295 unsigned int next_key; 296 297 unsigned int mode_check_freq[MAX_MODES]; 298 unsigned int mode_test_hit_counts[MAX_MODES]; 299 unsigned int mode_chosen_counts[MAX_MODES]; 300 unsigned int mbs_tested_so_far; 301 302 unsigned int check_freq[2]; 303 unsigned int do_full[2]; 304 305 int rd_thresh_mult[MAX_MODES]; 306 int rd_baseline_thresh[MAX_MODES]; 307 int rd_threshes[MAX_MODES]; 308 int mvcostbase; 309 int mvcostmultiplier; 310 int subseqblockweight; 311 int errthresh; 312 313 #ifdef INTRARDOPT 314 int RDMULT; 315 int RDDIV ; 316 317 TOKENEXTRA *rdtok; 318 int intra_rd_opt; 319 vp8_writer rdbc; 320 int intra_mode_costs[10]; 321 #endif 322 323 324 CODING_CONTEXT coding_context; 325 326 // Rate targetting variables 327 long long prediction_error; 328 long long last_prediction_error; 329 long long intra_error; 330 long long last_intra_error; 331 long long last_auto_filter_prediction_error; 332 333 #if 0 334 // Experimental RD code 335 long long frame_distortion; 336 long long last_frame_distortion; 337 #endif 338 339 int last_mb_distortion; 340 341 int frames_since_auto_filter; 342 343 int this_frame_target; 344 int projected_frame_size; 345 int last_q[2]; // Separate values for Intra/Inter 346 int target_bits_per_mb; 347 348 double rate_correction_factor; 349 double key_frame_rate_correction_factor; 350 double gf_rate_correction_factor; 351 double est_max_qcorrection_factor; 352 353 int frames_till_gf_update_due; // Count down till next GF 354 int current_gf_interval; // GF interval chosen when we coded the last GF 355 356 int gf_overspend_bits; // Total bits overspent becasue of GF boost (cumulative) 357 358 int gf_group_bits; // Projected Bits available for a group of frames including 1 GF or ARF 359 int gf_bits; // Bits for the golden frame or ARF - 2 pass only 360 int mid_gf_extra_bits; // A few extra bits for the frame half way between two gfs. 361 362 // Projected total bits available for a key frame group of frames 363 long long kf_group_bits; 364 365 // Error score of frames still to be coded in kf group 366 long long kf_group_error_left; 367 368 // Bits for the key frame in a key frame group - 2 pass only 369 int kf_bits; 370 371 int non_gf_bitrate_adjustment; // Used in the few frames following a GF to recover the extra bits spent in that GF 372 int initial_gf_use; // percentage use of gf 2 frames after gf 373 374 int gf_group_error_left; // Remaining error from uncoded frames in a gf group. Two pass use only 375 376 int kf_overspend_bits; // Extra bits spent on key frames that need to be recovered on inter frames 377 int kf_bitrate_adjustment; // Current number of bit s to try and recover on each inter frame. 378 int max_gf_interval; 379 int baseline_gf_interval; 380 int gf_decay_rate; 381 382 INT64 key_frame_count; 383 INT64 tot_key_frame_bits; 384 int prior_key_frame_size[KEY_FRAME_CONTEXT]; 385 int prior_key_frame_distance[KEY_FRAME_CONTEXT]; 386 int per_frame_bandwidth; // Current section per frame bandwidth target 387 int av_per_frame_bandwidth; // Average frame size target for clip 388 int min_frame_bandwidth; // Minimum allocation that should be used for any frame 389 int last_key_frame_size; 390 int intra_frame_target; 391 int inter_frame_target; 392 double output_frame_rate; 393 long long last_time_stamp_seen; 394 long long first_time_stamp_ever; 395 396 int ni_av_qi; 397 int ni_tot_qi; 398 int ni_frames; 399 int avg_frame_qindex; 400 401 int zbin_over_quant; 402 int zbin_mode_boost; 403 int zbin_mode_boost_enabled; 404 405 INT64 total_byte_count; 406 407 int buffered_mode; 408 409 int buffer_level; 410 int bits_off_target; 411 412 int rolling_target_bits; 413 int rolling_actual_bits; 414 415 int long_rolling_target_bits; 416 int long_rolling_actual_bits; 417 418 long long total_actual_bits; 419 int total_target_vs_actual; // debug stats 420 421 int worst_quality; 422 int active_worst_quality; 423 int best_quality; 424 int active_best_quality; 425 426 int drop_frames_allowed; // Are we permitted to drop frames? 427 int drop_frame; // Drop this frame? 428 int drop_count; // How many frames have we dropped? 429 int max_drop_count; // How many frames should we drop? 430 int max_consec_dropped_frames; // Limit number of consecutive frames that can be dropped. 431 432 433 int ymode_count [VP8_YMODES]; /* intra MB type cts this frame */ 434 int uv_mode_count[VP8_UV_MODES]; /* intra MB type cts this frame */ 435 436 unsigned int MVcount [2] [MVvals]; /* (row,col) MV cts this frame */ 437 438 unsigned int coef_counts [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]; /* for this frame */ 439 //DECLARE_ALIGNED(16, int, coef_counts_backup [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens]); //not used any more 440 //save vp8_tree_probs_from_distribution result for each frame to avoid repeat calculation 441 vp8_prob frame_coef_probs [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]; 442 unsigned int frame_branch_ct [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1][2]; 443 444 /* Second compressed data partition contains coefficient data. */ 445 446 unsigned char *output_partition2; 447 size_t output_partition2size; 448 449 pre_proc_instance ppi; 450 451 int frames_to_key; 452 int gfu_boost; 453 int kf_boost; 454 int last_boost; 455 double total_error_left; 456 double total_intra_error_left; 457 double total_coded_error_left; 458 double start_tot_err_left; 459 double min_error; 460 461 double modified_total_error_left; 462 double avg_iiratio; 463 464 int target_bandwidth; 465 long long bits_left; 466 FIRSTPASS_STATS total_stats; 467 FIRSTPASS_STATS this_frame_stats; 468 FIRSTPASS_STATS *stats_in, *stats_in_end; 469 struct vpx_codec_pkt_list *output_pkt_list; 470 int first_pass_done; 471 unsigned char *fp_motion_map; 472 FILE *fp_motion_mapfile; 473 int fpmm_pos; 474 475 #if 0 476 // Experimental code for lagged and one pass 477 ONEPASS_FRAMESTATS one_pass_frame_stats[MAX_LAG_BUFFERS]; 478 int one_pass_frame_index; 479 #endif 480 481 int decimation_factor; 482 int decimation_count; 483 484 // for real time encoding 485 int avg_encode_time; //microsecond 486 int avg_pick_mode_time; //microsecond 487 int Speed; 488 unsigned int cpu_freq; //Mhz 489 int compressor_speed; 490 491 int interquantizer; 492 int auto_gold; 493 int auto_adjust_gold_quantizer; 494 int goldquantizer; 495 int goldfreq; 496 int auto_adjust_key_quantizer; 497 int keyquantizer; 498 int auto_worst_q; 499 int filter_type; 500 int cpu_used; 501 int chroma_boost; 502 int horiz_scale; 503 int vert_scale; 504 int pass; 505 506 507 int prob_intra_coded; 508 int prob_last_coded; 509 int prob_gf_coded; 510 int prob_skip_false; 511 int last_skip_false_probs[3]; 512 int last_skip_probs_q[3]; 513 int recent_ref_frame_usage[MAX_REF_FRAMES]; 514 515 int count_mb_ref_frame_usage[MAX_REF_FRAMES]; 516 int this_frame_percent_intra; 517 int last_frame_percent_intra; 518 519 int last_key_frame_q; 520 int last_kffilt_lvl; 521 522 int ref_frame_flags; 523 524 int exp[512]; 525 526 SPEED_FEATURES sf; 527 int error_bins[1024]; 528 529 int inter_lvl; 530 int intra_lvl; 531 int motion_lvl; 532 int motion_speed; 533 int motion_var; 534 unsigned int next_iiratio; 535 unsigned int this_iiratio; 536 int this_frame_modified_error; 537 538 double norm_intra_err_per_mb; 539 double norm_inter_err_per_mb; 540 double norm_iidiff_per_mb; 541 542 int last_best_mode_index; // Record of mode index chosen for previous macro block. 543 int last_auto_filt_val; 544 int last_auto_filt_q; 545 546 // Data used for real time conferencing mode to help determine if it would be good to update the gf 547 int inter_zz_count; 548 int gf_bad_count; 549 int gf_update_recommended; 550 int skip_true_count; 551 int skip_false_count; 552 553 int alt_qcount; 554 555 int ready_for_new_frame; 556 557 unsigned char *segmentation_map; 558 signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; // Segment data (can be deltas or absolute values) 559 int segment_encode_breakout[MAX_MB_SEGMENTS]; // segment threashold for encode breakout 560 561 unsigned char *active_map; 562 unsigned int active_map_enabled; 563 // Video conferencing cyclic refresh mode flags etc 564 // This is a mode designed to clean up the background over time in live encoding scenarious. It uses segmentation 565 int cyclic_refresh_mode_enabled; 566 int cyclic_refresh_mode_max_mbs_perframe; 567 int cyclic_refresh_mode_index; 568 int cyclic_refresh_q; 569 signed char *cyclic_refresh_map; 570 571 // multithread data 572 int current_mb_col_main; 573 int processor_core_count; 574 int b_multi_threaded; 575 int encoding_thread_count; 576 577 #if CONFIG_MULTITHREAD 578 pthread_t *h_encoding_thread; 579 #endif 580 MB_ROW_COMP *mb_row_ei; 581 ENCODETHREAD_DATA *en_thread_data; 582 583 #if CONFIG_MULTITHREAD 584 //events 585 sem_t *h_event_mbrencoding; 586 sem_t h_event_main; 587 #endif 588 589 TOKENLIST *tplist; 590 // end of multithread data 591 592 593 fractional_mv_step_fp *find_fractional_mv_step; 594 vp8_full_search_fn_t full_search_sad; 595 vp8_diamond_search_fn_t diamond_search_sad; 596 vp8_variance_fn_ptr_t fn_ptr; 597 unsigned int time_receive_data; 598 unsigned int time_compress_data; 599 unsigned int time_pick_lpf; 600 unsigned int time_encode_mb_row; 601 602 unsigned int tempdata1; 603 unsigned int tempdata2; 604 605 int base_skip_false_prob[128]; 606 unsigned int section_is_low_motion; 607 unsigned int section_benefits_from_aggresive_q; 608 unsigned int section_is_fast_motion; 609 unsigned int section_intra_rating; 610 611 double section_max_qfactor; 612 613 614 #if CONFIG_RUNTIME_CPU_DETECT 615 VP8_ENCODER_RTCD rtcd; 616 #endif 617 #if VP8_TEMPORAL_ALT_REF 618 SOURCE_SAMPLE alt_ref_buffer; 619 unsigned char *frames[MAX_LAG_BUFFERS]; 620 int fixed_divide[255]; 621 #endif 622 623 #if CONFIG_PSNR 624 int count; 625 double total_y; 626 double total_u; 627 double total_v; 628 double total ; 629 double total_sq_error; 630 double totalp_y; 631 double totalp_u; 632 double totalp_v; 633 double totalp; 634 double total_sq_error2; 635 int bytes; 636 double summed_quality; 637 double summed_weights; 638 unsigned int tot_recode_hits; 639 640 641 double total_ssimg_y; 642 double total_ssimg_u; 643 double total_ssimg_v; 644 double total_ssimg_all; 645 646 int b_calculate_ssimg; 647 #endif 648 int b_calculate_psnr; 649 650 651 unsigned char *gf_active_flags; // Record of which MBs still refer to last golden frame either directly or through 0,0 652 int gf_active_count; 653 654 655 } VP8_COMP; 656 657 void control_data_rate(VP8_COMP *cpi); 658 659 void vp8_encode_frame(VP8_COMP *cpi); 660 661 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size); 662 663 int rd_cost_intra_mb(MACROBLOCKD *x); 664 665 void vp8_tokenize_mb(VP8_COMP *, MACROBLOCKD *, TOKENEXTRA **); 666 667 void vp8_set_speed_features(VP8_COMP *cpi); 668 669 #if CONFIG_DEBUG 670 #define CHECK_MEM_ERROR(lval,expr) do {\ 671 lval = (expr); \ 672 if(!lval) \ 673 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\ 674 "Failed to allocate "#lval" at %s:%d", \ 675 __FILE__,__LINE__);\ 676 } while(0) 677 #else 678 #define CHECK_MEM_ERROR(lval,expr) do {\ 679 lval = (expr); \ 680 if(!lval) \ 681 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,\ 682 "Failed to allocate "#lval);\ 683 } while(0) 684 #endif 685 #endif 686