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 #include "onyxc_int.h" 13 #include "onyx_int.h" 14 #include "systemdependent.h" 15 #include "quantize.h" 16 #include "alloccommon.h" 17 #include "mcomp.h" 18 #include "firstpass.h" 19 #include "psnr.h" 20 #include "vpx_scale/vpxscale.h" 21 #include "extend.h" 22 #include "ratectrl.h" 23 #include "quant_common.h" 24 #include "segmentation.h" 25 #include "g_common.h" 26 #include "vpx_scale/yv12extend.h" 27 #include "postproc.h" 28 #include "vpx_mem/vpx_mem.h" 29 #include "swapyv12buffer.h" 30 #include "threading.h" 31 #include "vpx_ports/vpx_timer.h" 32 #include "vpxerrors.h" 33 34 #include <math.h> 35 #include <stdio.h> 36 #include <limits.h> 37 38 #if CONFIG_RUNTIME_CPU_DETECT 39 #define IF_RTCD(x) (x) 40 #define RTCD(x) &cpi->common.rtcd.x 41 #else 42 #define IF_RTCD(x) NULL 43 #define RTCD(x) NULL 44 #endif 45 46 extern void vp8cx_init_mv_bits_sadcost(); 47 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi); 48 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val); 49 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi); 50 51 extern void vp8_init_loop_filter(VP8_COMMON *cm); 52 extern void vp8_loop_filter_frame(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val); 53 extern void vp8_loop_filter_frame_yonly(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val, int sharpness_lvl); 54 extern void vp8_dmachine_specific_config(VP8_COMP *cpi); 55 extern void vp8_cmachine_specific_config(VP8_COMP *cpi); 56 extern void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi); 57 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag); 58 extern void print_parms(VP8_CONFIG *ocf, char *filenam); 59 extern unsigned int vp8_get_processor_freq(); 60 extern void print_tree_update_probs(); 61 extern void vp8cx_create_encoder_threads(VP8_COMP *cpi); 62 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi); 63 #if HAVE_ARMV7 64 extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc); 65 extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc); 66 #endif 67 68 int vp8_estimate_entropy_savings(VP8_COMP *cpi); 69 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd); 70 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd); 71 72 73 static void mode_ref_lf_test_function(VP8_COMP *cpi); 74 75 extern const int vp8_gf_interval_table[101]; 76 77 #if CONFIG_PSNR 78 #include "math.h" 79 80 extern double vp8_calc_ssim 81 ( 82 YV12_BUFFER_CONFIG *source, 83 YV12_BUFFER_CONFIG *dest, 84 int lumamask, 85 double *weight 86 ); 87 88 extern double vp8_calc_ssimg 89 ( 90 YV12_BUFFER_CONFIG *source, 91 YV12_BUFFER_CONFIG *dest, 92 double *ssim_y, 93 double *ssim_u, 94 double *ssim_v 95 ); 96 97 98 #endif 99 100 101 #ifdef OUTPUT_YUV_SRC 102 FILE *yuv_file; 103 #endif 104 105 #if 0 106 FILE *framepsnr; 107 FILE *kf_list; 108 FILE *keyfile; 109 #endif 110 111 #if 0 112 extern int skip_true_count; 113 extern int skip_false_count; 114 #endif 115 116 117 #ifdef ENTROPY_STATS 118 extern int intra_mode_stats[10][10][10]; 119 #endif 120 121 #ifdef SPEEDSTATS 122 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 123 unsigned int tot_pm = 0; 124 unsigned int cnt_pm = 0; 125 unsigned int tot_ef = 0; 126 unsigned int cnt_ef = 0; 127 #endif 128 129 #ifdef MODE_STATS 130 extern unsigned __int64 Sectionbits[50]; 131 extern int y_modes[5] ; 132 extern int uv_modes[4] ; 133 extern int b_modes[10] ; 134 135 extern int inter_y_modes[10] ; 136 extern int inter_uv_modes[4] ; 137 extern unsigned int inter_b_modes[15]; 138 #endif 139 140 extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch); 141 extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch); 142 143 extern const int vp8_bits_per_mb[2][QINDEX_RANGE]; 144 145 extern const int qrounding_factors[129]; 146 extern const int qzbin_factors[129]; 147 extern void vp8cx_init_quantizer(VP8_COMP *cpi); 148 extern const int vp8cx_base_skip_false_prob[128]; 149 150 151 void vp8_initialize() 152 { 153 static int init_done = 0; 154 155 if (!init_done) 156 { 157 vp8_scale_machine_specific_config(); 158 vp8_initialize_common(); 159 //vp8_dmachine_specific_config(); 160 vp8_tokenize_initialize(); 161 162 vp8cx_init_mv_bits_sadcost(); 163 init_done = 1; 164 } 165 } 166 #ifdef PACKET_TESTING 167 extern FILE *vpxlogc; 168 #endif 169 170 static void setup_features(VP8_COMP *cpi) 171 { 172 // Set up default state for MB feature flags 173 cpi->mb.e_mbd.segmentation_enabled = 0; 174 cpi->mb.e_mbd.update_mb_segmentation_map = 0; 175 cpi->mb.e_mbd.update_mb_segmentation_data = 0; 176 vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs)); 177 vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data)); 178 179 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0; 180 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0; 181 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas)); 182 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas)); 183 184 // jbb trial ! 185 mode_ref_lf_test_function(cpi); 186 187 } 188 189 190 void vp8_dealloc_compressor_data(VP8_COMP *cpi) 191 { 192 193 // Delete sementation map 194 if (cpi->segmentation_map != 0) 195 vpx_free(cpi->segmentation_map); 196 197 cpi->segmentation_map = 0; 198 199 if (cpi->active_map != 0) 200 vpx_free(cpi->active_map); 201 202 cpi->active_map = 0; 203 204 // Delete first pass motion map 205 if (cpi->fp_motion_map != 0) 206 vpx_free(cpi->fp_motion_map); 207 208 cpi->fp_motion_map = 0; 209 210 vp8_de_alloc_frame_buffers(&cpi->common); 211 212 vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf); 213 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source); 214 #if VP8_TEMPORAL_ALT_REF 215 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer); 216 #endif 217 { 218 int i; 219 220 for (i = 0; i < MAX_LAG_BUFFERS; i++) 221 vp8_yv12_de_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer); 222 223 cpi->source_buffer_count = 0; 224 } 225 226 vpx_free(cpi->tok); 227 cpi->tok = 0; 228 229 // Structure used to minitor GF useage 230 if (cpi->gf_active_flags != 0) 231 vpx_free(cpi->gf_active_flags); 232 233 cpi->gf_active_flags = 0; 234 235 if(cpi->mb.pip) 236 vpx_free(cpi->mb.pip); 237 238 cpi->mb.pip = 0; 239 240 } 241 242 static void enable_segmentation(VP8_PTR ptr) 243 { 244 VP8_COMP *cpi = (VP8_COMP *)(ptr); 245 246 // Set the appropriate feature bit 247 cpi->mb.e_mbd.segmentation_enabled = 1; 248 cpi->mb.e_mbd.update_mb_segmentation_map = 1; 249 cpi->mb.e_mbd.update_mb_segmentation_data = 1; 250 } 251 static void disable_segmentation(VP8_PTR ptr) 252 { 253 VP8_COMP *cpi = (VP8_COMP *)(ptr); 254 255 // Clear the appropriate feature bit 256 cpi->mb.e_mbd.segmentation_enabled = 0; 257 } 258 259 // Valid values for a segment are 0 to 3 260 // Segmentation map is arrange as [Rows][Columns] 261 static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map) 262 { 263 VP8_COMP *cpi = (VP8_COMP *)(ptr); 264 265 // Copy in the new segmentation map 266 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols)); 267 268 // Signal that the map should be updated. 269 cpi->mb.e_mbd.update_mb_segmentation_map = 1; 270 cpi->mb.e_mbd.update_mb_segmentation_data = 1; 271 } 272 273 // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values. 274 // 275 // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF) 276 // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF) 277 // 278 // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given). 279 // 280 // 281 static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta) 282 { 283 VP8_COMP *cpi = (VP8_COMP *)(ptr); 284 285 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta; 286 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data)); 287 } 288 289 290 static void segmentation_test_function(VP8_PTR ptr) 291 { 292 VP8_COMP *cpi = (VP8_COMP *)(ptr); 293 294 unsigned char *seg_map; 295 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 296 297 // Create a temporary map for segmentation data. 298 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1)); 299 300 // MB loop to set local segmentation map 301 /*for ( i = 0; i < cpi->common.mb_rows; i++ ) 302 { 303 for ( j = 0; j < cpi->common.mb_cols; j++ ) 304 { 305 //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2); 306 //if ( j < cpi->common.mb_cols/2 ) 307 308 // Segment 1 around the edge else 0 309 if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) ) 310 seg_map[(i*cpi->common.mb_cols) + j] = 1; 311 //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) ) 312 // seg_map[(i*cpi->common.mb_cols) + j] = 2; 313 //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) ) 314 // seg_map[(i*cpi->common.mb_cols) + j] = 3; 315 else 316 seg_map[(i*cpi->common.mb_cols) + j] = 0; 317 } 318 }*/ 319 320 // Set the segmentation Map 321 set_segmentation_map(ptr, seg_map); 322 323 // Activate segmentation. 324 enable_segmentation(ptr); 325 326 // Set up the quant segment data 327 feature_data[MB_LVL_ALT_Q][0] = 0; 328 feature_data[MB_LVL_ALT_Q][1] = 4; 329 feature_data[MB_LVL_ALT_Q][2] = 0; 330 feature_data[MB_LVL_ALT_Q][3] = 0; 331 // Set up the loop segment data 332 feature_data[MB_LVL_ALT_LF][0] = 0; 333 feature_data[MB_LVL_ALT_LF][1] = 0; 334 feature_data[MB_LVL_ALT_LF][2] = 0; 335 feature_data[MB_LVL_ALT_LF][3] = 0; 336 337 // Initialise the feature data structure 338 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 339 set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA); 340 341 // Delete sementation map 342 if (seg_map != 0) 343 vpx_free(seg_map); 344 345 seg_map = 0; 346 347 } 348 349 // A simple function to cyclically refresh the background at a lower Q 350 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment) 351 { 352 unsigned char *seg_map; 353 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 354 int i; 355 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe; 356 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols; 357 358 // Create a temporary map for segmentation data. 359 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1)); 360 361 cpi->cyclic_refresh_q = Q; 362 363 for (i = Q; i > 0; i--) 364 { 365 if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64)) 366 //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) ) 367 { 368 break; 369 } 370 } 371 372 cpi->cyclic_refresh_q = i; 373 374 // Only update for inter frames 375 if (cpi->common.frame_type != KEY_FRAME) 376 { 377 // Cycle through the macro_block rows 378 // MB loop to set local segmentation map 379 for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++) 380 { 381 // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1) 382 // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0 383 // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks. 384 if (cpi->cyclic_refresh_map[i] == 0) 385 { 386 seg_map[i] = 1; 387 } 388 else 389 { 390 seg_map[i] = 0; 391 392 // Skip blocks that have been refreshed recently anyway. 393 if (cpi->cyclic_refresh_map[i] < 0) 394 //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16; 395 cpi->cyclic_refresh_map[i]++; 396 } 397 398 399 if (block_count > 0) 400 block_count--; 401 else 402 break; 403 404 } 405 406 // If we have gone through the frame reset to the start 407 cpi->cyclic_refresh_mode_index = i; 408 409 if (cpi->cyclic_refresh_mode_index >= mbs_in_frame) 410 cpi->cyclic_refresh_mode_index = 0; 411 } 412 413 // Set the segmentation Map 414 set_segmentation_map((VP8_PTR)cpi, seg_map); 415 416 // Activate segmentation. 417 enable_segmentation((VP8_PTR)cpi); 418 419 // Set up the quant segment data 420 feature_data[MB_LVL_ALT_Q][0] = 0; 421 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q); 422 feature_data[MB_LVL_ALT_Q][2] = 0; 423 feature_data[MB_LVL_ALT_Q][3] = 0; 424 425 // Set up the loop segment data 426 feature_data[MB_LVL_ALT_LF][0] = 0; 427 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment; 428 feature_data[MB_LVL_ALT_LF][2] = 0; 429 feature_data[MB_LVL_ALT_LF][3] = 0; 430 431 // Initialise the feature data structure 432 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 433 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA); 434 435 // Delete sementation map 436 if (seg_map != 0) 437 vpx_free(seg_map); 438 439 seg_map = 0; 440 441 } 442 443 static void mode_ref_lf_test_function(VP8_COMP *cpi) 444 { 445 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1; 446 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1; 447 448 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas)); 449 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas)); 450 451 // Test of ref frame deltas 452 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2; 453 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0; 454 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2; 455 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2; 456 457 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED 458 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero 459 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv 460 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv 461 } 462 463 void vp8_set_speed_features(VP8_COMP *cpi) 464 { 465 SPEED_FEATURES *sf = &cpi->sf; 466 int Mode = cpi->compressor_speed; 467 int Speed = cpi->Speed; 468 int i; 469 VP8_COMMON *cm = &cpi->common; 470 471 // Initialise default mode frequency sampling variables 472 for (i = 0; i < MAX_MODES; i ++) 473 { 474 cpi->mode_check_freq[i] = 0; 475 cpi->mode_test_hit_counts[i] = 0; 476 cpi->mode_chosen_counts[i] = 0; 477 } 478 479 cpi->mbs_tested_so_far = 0; 480 481 // best quality 482 sf->RD = 1; 483 sf->search_method = NSTEP; 484 sf->improved_quant = 1; 485 sf->improved_dct = 1; 486 sf->auto_filter = 1; 487 sf->recode_loop = 1; 488 sf->quarter_pixel_search = 1; 489 sf->half_pixel_search = 1; 490 sf->full_freq[0] = 7; 491 sf->full_freq[1] = 7; 492 sf->min_fs_radius = 8; 493 sf->max_fs_radius = 32; 494 sf->iterative_sub_pixel = 1; 495 sf->optimize_coefficients = 1; 496 497 sf->first_step = 0; 498 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 499 500 cpi->do_full[0] = 0; 501 cpi->do_full[1] = 0; 502 503 // default thresholds to 0 504 for (i = 0; i < MAX_MODES; i++) 505 sf->thresh_mult[i] = 0; 506 507 switch (Mode) 508 { 509 #if !(CONFIG_REALTIME_ONLY) 510 case 0: // best quality mode 511 sf->thresh_mult[THR_ZEROMV ] = 0; 512 sf->thresh_mult[THR_ZEROG ] = 0; 513 sf->thresh_mult[THR_ZEROA ] = 0; 514 sf->thresh_mult[THR_NEARESTMV] = 0; 515 sf->thresh_mult[THR_NEARESTG ] = 0; 516 sf->thresh_mult[THR_NEARESTA ] = 0; 517 sf->thresh_mult[THR_NEARMV ] = 0; 518 sf->thresh_mult[THR_NEARG ] = 0; 519 sf->thresh_mult[THR_NEARA ] = 0; 520 521 sf->thresh_mult[THR_DC ] = 0; 522 523 sf->thresh_mult[THR_V_PRED ] = 1000; 524 sf->thresh_mult[THR_H_PRED ] = 1000; 525 sf->thresh_mult[THR_B_PRED ] = 2000; 526 sf->thresh_mult[THR_TM ] = 1000; 527 528 sf->thresh_mult[THR_NEWMV ] = 1000; 529 sf->thresh_mult[THR_NEWG ] = 1000; 530 sf->thresh_mult[THR_NEWA ] = 1000; 531 532 sf->thresh_mult[THR_SPLITMV ] = 2500; 533 sf->thresh_mult[THR_SPLITG ] = 5000; 534 sf->thresh_mult[THR_SPLITA ] = 5000; 535 536 sf->full_freq[0] = 7; 537 sf->full_freq[1] = 15; 538 539 sf->first_step = 0; 540 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 541 542 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG)) 543 { 544 sf->thresh_mult[THR_NEWMV ] = INT_MAX; 545 sf->thresh_mult[THR_NEARESTMV] = INT_MAX; 546 sf->thresh_mult[THR_ZEROMV ] = INT_MAX; 547 sf->thresh_mult[THR_NEARMV ] = INT_MAX; 548 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 549 } 550 551 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG)) 552 { 553 sf->thresh_mult[THR_NEARESTG ] = INT_MAX; 554 sf->thresh_mult[THR_ZEROG ] = INT_MAX; 555 sf->thresh_mult[THR_NEARG ] = INT_MAX; 556 sf->thresh_mult[THR_NEWG ] = INT_MAX; 557 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 558 } 559 560 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG)) 561 { 562 sf->thresh_mult[THR_NEARESTA ] = INT_MAX; 563 sf->thresh_mult[THR_ZEROA ] = INT_MAX; 564 sf->thresh_mult[THR_NEARA ] = INT_MAX; 565 sf->thresh_mult[THR_NEWA ] = INT_MAX; 566 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 567 } 568 569 break; 570 case 1: 571 case 3: 572 sf->optimize_coefficients = 0; 573 sf->thresh_mult[THR_NEARESTMV] = 0; 574 sf->thresh_mult[THR_ZEROMV ] = 0; 575 sf->thresh_mult[THR_DC ] = 0; 576 sf->thresh_mult[THR_NEARMV ] = 0; 577 sf->thresh_mult[THR_V_PRED ] = 1000; 578 sf->thresh_mult[THR_H_PRED ] = 1000; 579 sf->thresh_mult[THR_B_PRED ] = 2500; 580 sf->thresh_mult[THR_TM ] = 1000; 581 582 sf->thresh_mult[THR_NEARESTG ] = 1000; 583 sf->thresh_mult[THR_NEARESTA ] = 1000; 584 585 sf->thresh_mult[THR_ZEROG ] = 1000; 586 sf->thresh_mult[THR_ZEROA ] = 1000; 587 sf->thresh_mult[THR_NEARG ] = 1000; 588 sf->thresh_mult[THR_NEARA ] = 1000; 589 590 sf->thresh_mult[THR_NEWMV ] = 1500; 591 sf->thresh_mult[THR_NEWG ] = 1500; 592 sf->thresh_mult[THR_NEWA ] = 1500; 593 594 sf->thresh_mult[THR_SPLITMV ] = 5000; 595 sf->thresh_mult[THR_SPLITG ] = 10000; 596 sf->thresh_mult[THR_SPLITA ] = 10000; 597 598 sf->full_freq[0] = 15; 599 sf->full_freq[1] = 31; 600 601 sf->first_step = 0; 602 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 603 604 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG)) 605 { 606 sf->thresh_mult[THR_NEWMV ] = INT_MAX; 607 sf->thresh_mult[THR_NEARESTMV] = INT_MAX; 608 sf->thresh_mult[THR_ZEROMV ] = INT_MAX; 609 sf->thresh_mult[THR_NEARMV ] = INT_MAX; 610 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 611 } 612 613 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG)) 614 { 615 sf->thresh_mult[THR_NEARESTG ] = INT_MAX; 616 sf->thresh_mult[THR_ZEROG ] = INT_MAX; 617 sf->thresh_mult[THR_NEARG ] = INT_MAX; 618 sf->thresh_mult[THR_NEWG ] = INT_MAX; 619 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 620 } 621 622 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG)) 623 { 624 sf->thresh_mult[THR_NEARESTA ] = INT_MAX; 625 sf->thresh_mult[THR_ZEROA ] = INT_MAX; 626 sf->thresh_mult[THR_NEARA ] = INT_MAX; 627 sf->thresh_mult[THR_NEWA ] = INT_MAX; 628 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 629 } 630 631 if (Speed > 0) 632 { 633 cpi->mode_check_freq[THR_SPLITG] = 4; 634 cpi->mode_check_freq[THR_SPLITA] = 4; 635 cpi->mode_check_freq[THR_SPLITMV] = 2; 636 637 sf->thresh_mult[THR_TM ] = 1500; 638 sf->thresh_mult[THR_V_PRED ] = 1500; 639 sf->thresh_mult[THR_H_PRED ] = 1500; 640 sf->thresh_mult[THR_B_PRED ] = 5000; 641 642 if (cpi->ref_frame_flags & VP8_LAST_FLAG) 643 { 644 sf->thresh_mult[THR_NEWMV ] = 2000; 645 sf->thresh_mult[THR_SPLITMV ] = 10000; 646 } 647 648 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 649 { 650 sf->thresh_mult[THR_NEARESTG ] = 1500; 651 sf->thresh_mult[THR_ZEROG ] = 1500; 652 sf->thresh_mult[THR_NEARG ] = 1500; 653 sf->thresh_mult[THR_NEWG ] = 2000; 654 sf->thresh_mult[THR_SPLITG ] = 20000; 655 } 656 657 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 658 { 659 sf->thresh_mult[THR_NEARESTA ] = 1500; 660 sf->thresh_mult[THR_ZEROA ] = 1500; 661 sf->thresh_mult[THR_NEARA ] = 1500; 662 sf->thresh_mult[THR_NEWA ] = 2000; 663 sf->thresh_mult[THR_SPLITA ] = 20000; 664 } 665 666 sf->improved_quant = 0; 667 sf->improved_dct = 0; 668 669 sf->first_step = 1; 670 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 671 } 672 673 if (Speed > 1) 674 { 675 cpi->mode_check_freq[THR_SPLITG] = 15; 676 cpi->mode_check_freq[THR_SPLITA] = 15; 677 cpi->mode_check_freq[THR_SPLITMV] = 7; 678 679 sf->thresh_mult[THR_TM ] = 2000; 680 sf->thresh_mult[THR_V_PRED ] = 2000; 681 sf->thresh_mult[THR_H_PRED ] = 2000; 682 sf->thresh_mult[THR_B_PRED ] = 7500; 683 684 if (cpi->ref_frame_flags & VP8_LAST_FLAG) 685 { 686 sf->thresh_mult[THR_NEWMV ] = 2000; 687 sf->thresh_mult[THR_SPLITMV ] = 25000; 688 } 689 690 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 691 { 692 sf->thresh_mult[THR_NEARESTG ] = 2000; 693 sf->thresh_mult[THR_ZEROG ] = 2000; 694 sf->thresh_mult[THR_NEARG ] = 2000; 695 sf->thresh_mult[THR_NEWG ] = 2500; 696 sf->thresh_mult[THR_SPLITG ] = 50000; 697 } 698 699 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 700 { 701 sf->thresh_mult[THR_NEARESTA ] = 2000; 702 sf->thresh_mult[THR_ZEROA ] = 2000; 703 sf->thresh_mult[THR_NEARA ] = 2000; 704 sf->thresh_mult[THR_NEWA ] = 2500; 705 sf->thresh_mult[THR_SPLITA ] = 50000; 706 } 707 708 // Only do recode loop on key frames and golden frames 709 sf->recode_loop = 2; 710 711 sf->full_freq[0] = 31; 712 sf->full_freq[1] = 63; 713 714 } 715 716 if (Speed > 2) 717 { 718 sf->auto_filter = 0; // Faster selection of loop filter 719 cpi->mode_check_freq[THR_V_PRED] = 2; 720 cpi->mode_check_freq[THR_H_PRED] = 2; 721 cpi->mode_check_freq[THR_B_PRED] = 2; 722 723 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 724 { 725 cpi->mode_check_freq[THR_NEARG] = 2; 726 cpi->mode_check_freq[THR_NEWG] = 4; 727 } 728 729 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 730 { 731 cpi->mode_check_freq[THR_NEARA] = 2; 732 cpi->mode_check_freq[THR_NEWA] = 4; 733 } 734 735 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 736 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 737 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 738 739 sf->full_freq[0] = 63; 740 sf->full_freq[1] = 127; 741 } 742 743 if (Speed > 3) 744 { 745 cpi->mode_check_freq[THR_V_PRED] = 0; 746 cpi->mode_check_freq[THR_H_PRED] = 0; 747 cpi->mode_check_freq[THR_B_PRED] = 0; 748 cpi->mode_check_freq[THR_NEARG] = 0; 749 cpi->mode_check_freq[THR_NEWG] = 0; 750 cpi->mode_check_freq[THR_NEARA] = 0; 751 cpi->mode_check_freq[THR_NEWA] = 0; 752 753 sf->auto_filter = 1; 754 sf->recode_loop = 0; // recode loop off 755 sf->RD = 0; // Turn rd off 756 sf->full_freq[0] = INT_MAX; 757 sf->full_freq[1] = INT_MAX; 758 } 759 760 if (Speed > 4) 761 { 762 sf->auto_filter = 0; // Faster selection of loop filter 763 764 cpi->mode_check_freq[THR_V_PRED] = 2; 765 cpi->mode_check_freq[THR_H_PRED] = 2; 766 cpi->mode_check_freq[THR_B_PRED] = 2; 767 768 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 769 { 770 cpi->mode_check_freq[THR_NEARG] = 2; 771 cpi->mode_check_freq[THR_NEWG] = 4; 772 } 773 774 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 775 { 776 cpi->mode_check_freq[THR_NEARA] = 2; 777 cpi->mode_check_freq[THR_NEWA] = 4; 778 } 779 780 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 781 { 782 sf->thresh_mult[THR_NEARESTG ] = 2000; 783 sf->thresh_mult[THR_ZEROG ] = 2000; 784 sf->thresh_mult[THR_NEARG ] = 2000; 785 sf->thresh_mult[THR_NEWG ] = 4000; 786 } 787 788 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 789 { 790 sf->thresh_mult[THR_NEARESTA ] = 2000; 791 sf->thresh_mult[THR_ZEROA ] = 2000; 792 sf->thresh_mult[THR_NEARA ] = 2000; 793 sf->thresh_mult[THR_NEWA ] = 4000; 794 } 795 } 796 797 break; 798 #endif 799 case 2: 800 sf->optimize_coefficients = 0; 801 sf->recode_loop = 0; 802 sf->auto_filter = 1; 803 sf->iterative_sub_pixel = 1; 804 sf->thresh_mult[THR_NEARESTMV] = 0; 805 sf->thresh_mult[THR_ZEROMV ] = 0; 806 sf->thresh_mult[THR_DC ] = 0; 807 sf->thresh_mult[THR_TM ] = 0; 808 sf->thresh_mult[THR_NEARMV ] = 0; 809 sf->thresh_mult[THR_V_PRED ] = 1000; 810 sf->thresh_mult[THR_H_PRED ] = 1000; 811 sf->thresh_mult[THR_B_PRED ] = 2500; 812 sf->thresh_mult[THR_NEARESTG ] = 1000; 813 sf->thresh_mult[THR_ZEROG ] = 1000; 814 sf->thresh_mult[THR_NEARG ] = 1000; 815 sf->thresh_mult[THR_NEARESTA ] = 1000; 816 sf->thresh_mult[THR_ZEROA ] = 1000; 817 sf->thresh_mult[THR_NEARA ] = 1000; 818 sf->thresh_mult[THR_NEWMV ] = 2000; 819 sf->thresh_mult[THR_NEWG ] = 2000; 820 sf->thresh_mult[THR_NEWA ] = 2000; 821 sf->thresh_mult[THR_SPLITMV ] = 5000; 822 sf->thresh_mult[THR_SPLITG ] = 10000; 823 sf->thresh_mult[THR_SPLITA ] = 10000; 824 sf->full_freq[0] = 15; 825 sf->full_freq[1] = 31; 826 sf->search_method = NSTEP; 827 828 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG)) 829 { 830 sf->thresh_mult[THR_NEWMV ] = INT_MAX; 831 sf->thresh_mult[THR_NEARESTMV] = INT_MAX; 832 sf->thresh_mult[THR_ZEROMV ] = INT_MAX; 833 sf->thresh_mult[THR_NEARMV ] = INT_MAX; 834 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 835 } 836 837 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG)) 838 { 839 sf->thresh_mult[THR_NEARESTG ] = INT_MAX; 840 sf->thresh_mult[THR_ZEROG ] = INT_MAX; 841 sf->thresh_mult[THR_NEARG ] = INT_MAX; 842 sf->thresh_mult[THR_NEWG ] = INT_MAX; 843 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 844 } 845 846 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG)) 847 { 848 sf->thresh_mult[THR_NEARESTA ] = INT_MAX; 849 sf->thresh_mult[THR_ZEROA ] = INT_MAX; 850 sf->thresh_mult[THR_NEARA ] = INT_MAX; 851 sf->thresh_mult[THR_NEWA ] = INT_MAX; 852 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 853 } 854 855 if (Speed > 0) 856 { 857 cpi->mode_check_freq[THR_SPLITG] = 4; 858 cpi->mode_check_freq[THR_SPLITA] = 4; 859 cpi->mode_check_freq[THR_SPLITMV] = 2; 860 861 sf->thresh_mult[THR_DC ] = 0; 862 sf->thresh_mult[THR_TM ] = 1000; 863 sf->thresh_mult[THR_V_PRED ] = 2000; 864 sf->thresh_mult[THR_H_PRED ] = 2000; 865 sf->thresh_mult[THR_B_PRED ] = 5000; 866 867 if (cpi->ref_frame_flags & VP8_LAST_FLAG) 868 { 869 sf->thresh_mult[THR_NEARESTMV] = 0; 870 sf->thresh_mult[THR_ZEROMV ] = 0; 871 sf->thresh_mult[THR_NEARMV ] = 0; 872 sf->thresh_mult[THR_NEWMV ] = 2000; 873 sf->thresh_mult[THR_SPLITMV ] = 10000; 874 } 875 876 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 877 { 878 sf->thresh_mult[THR_NEARESTG ] = 1000; 879 sf->thresh_mult[THR_ZEROG ] = 1000; 880 sf->thresh_mult[THR_NEARG ] = 1000; 881 sf->thresh_mult[THR_NEWG ] = 2000; 882 sf->thresh_mult[THR_SPLITG ] = 20000; 883 } 884 885 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 886 { 887 sf->thresh_mult[THR_NEARESTA ] = 1000; 888 sf->thresh_mult[THR_ZEROA ] = 1000; 889 sf->thresh_mult[THR_NEARA ] = 1000; 890 sf->thresh_mult[THR_NEWA ] = 2000; 891 sf->thresh_mult[THR_SPLITA ] = 20000; 892 } 893 894 sf->improved_quant = 0; 895 sf->improved_dct = 0; 896 } 897 898 if (Speed > 1) 899 { 900 cpi->mode_check_freq[THR_SPLITMV] = 7; 901 cpi->mode_check_freq[THR_SPLITG] = 15; 902 cpi->mode_check_freq[THR_SPLITA] = 15; 903 904 sf->thresh_mult[THR_TM ] = 2000; 905 sf->thresh_mult[THR_V_PRED ] = 2000; 906 sf->thresh_mult[THR_H_PRED ] = 2000; 907 sf->thresh_mult[THR_B_PRED ] = 5000; 908 909 if (cpi->ref_frame_flags & VP8_LAST_FLAG) 910 { 911 sf->thresh_mult[THR_NEWMV ] = 2000; 912 sf->thresh_mult[THR_SPLITMV ] = 25000; 913 } 914 915 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 916 { 917 sf->thresh_mult[THR_NEARESTG ] = 2000; 918 sf->thresh_mult[THR_ZEROG ] = 2000; 919 sf->thresh_mult[THR_NEARG ] = 2000; 920 sf->thresh_mult[THR_NEWG ] = 2500; 921 sf->thresh_mult[THR_SPLITG ] = 50000; 922 } 923 924 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 925 { 926 sf->thresh_mult[THR_NEARESTA ] = 2000; 927 sf->thresh_mult[THR_ZEROA ] = 2000; 928 sf->thresh_mult[THR_NEARA ] = 2000; 929 sf->thresh_mult[THR_NEWA ] = 2500; 930 sf->thresh_mult[THR_SPLITA ] = 50000; 931 } 932 933 sf->full_freq[0] = 31; 934 sf->full_freq[1] = 63; 935 } 936 937 if (Speed > 2) 938 { 939 sf->auto_filter = 0; // Faster selection of loop filter 940 941 cpi->mode_check_freq[THR_V_PRED] = 2; 942 cpi->mode_check_freq[THR_H_PRED] = 2; 943 cpi->mode_check_freq[THR_B_PRED] = 2; 944 945 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 946 { 947 cpi->mode_check_freq[THR_NEARG] = 2; 948 cpi->mode_check_freq[THR_NEWG] = 4; 949 } 950 951 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 952 { 953 cpi->mode_check_freq[THR_NEARA] = 2; 954 cpi->mode_check_freq[THR_NEWA] = 4; 955 } 956 957 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 958 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 959 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 960 961 sf->full_freq[0] = 63; 962 sf->full_freq[1] = 127; 963 } 964 965 if (Speed > 3) 966 { 967 sf->RD = 0; 968 sf->full_freq[0] = INT_MAX; 969 sf->full_freq[1] = INT_MAX; 970 971 sf->auto_filter = 1; 972 } 973 974 if (Speed > 4) 975 { 976 sf->auto_filter = 0; // Faster selection of loop filter 977 978 #if CONFIG_REALTIME_ONLY 979 sf->search_method = HEX; 980 #else 981 sf->search_method = DIAMOND; 982 #endif 983 984 cpi->mode_check_freq[THR_V_PRED] = 4; 985 cpi->mode_check_freq[THR_H_PRED] = 4; 986 cpi->mode_check_freq[THR_B_PRED] = 4; 987 988 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 989 { 990 cpi->mode_check_freq[THR_NEARG] = 2; 991 cpi->mode_check_freq[THR_NEWG] = 4; 992 } 993 994 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 995 { 996 cpi->mode_check_freq[THR_NEARA] = 2; 997 cpi->mode_check_freq[THR_NEWA] = 4; 998 } 999 1000 sf->thresh_mult[THR_TM ] = 2000; 1001 sf->thresh_mult[THR_B_PRED ] = 5000; 1002 1003 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 1004 { 1005 sf->thresh_mult[THR_NEARESTG ] = 2000; 1006 sf->thresh_mult[THR_ZEROG ] = 2000; 1007 sf->thresh_mult[THR_NEARG ] = 2000; 1008 sf->thresh_mult[THR_NEWG ] = 4000; 1009 } 1010 1011 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 1012 { 1013 sf->thresh_mult[THR_NEARESTA ] = 2000; 1014 sf->thresh_mult[THR_ZEROA ] = 2000; 1015 sf->thresh_mult[THR_NEARA ] = 2000; 1016 sf->thresh_mult[THR_NEWA ] = 4000; 1017 } 1018 } 1019 1020 if (Speed > 5) 1021 { 1022 // Disable split MB intra prediction mode 1023 sf->thresh_mult[THR_B_PRED] = INT_MAX; 1024 } 1025 1026 if (Speed > 6) 1027 { 1028 unsigned int i, sum = 0; 1029 unsigned int total_mbs = cm->MBs; 1030 int thresh; 1031 int total_skip; 1032 1033 int min = 2000; 1034 sf->iterative_sub_pixel = 0; 1035 1036 if (cpi->oxcf.encode_breakout > 2000) 1037 min = cpi->oxcf.encode_breakout; 1038 1039 min >>= 7; 1040 1041 for (i = 0; i < min; i++) 1042 { 1043 sum += cpi->error_bins[i]; 1044 } 1045 1046 total_skip = sum; 1047 sum = 0; 1048 1049 // i starts from 2 to make sure thresh started from 2048 1050 for (; i < 1024; i++) 1051 { 1052 sum += cpi->error_bins[i]; 1053 1054 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip)) 1055 break; 1056 } 1057 1058 i--; 1059 thresh = (i << 7); 1060 1061 if (thresh < 2000) 1062 thresh = 2000; 1063 1064 if (cpi->ref_frame_flags & VP8_LAST_FLAG) 1065 { 1066 sf->thresh_mult[THR_NEWMV] = thresh; 1067 sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1; 1068 sf->thresh_mult[THR_NEARMV ] = thresh >> 1; 1069 } 1070 1071 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 1072 { 1073 sf->thresh_mult[THR_NEWG] = thresh << 1; 1074 sf->thresh_mult[THR_NEARESTG ] = thresh; 1075 sf->thresh_mult[THR_NEARG ] = thresh; 1076 } 1077 1078 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 1079 { 1080 sf->thresh_mult[THR_NEWA] = thresh << 1; 1081 sf->thresh_mult[THR_NEARESTA ] = thresh; 1082 sf->thresh_mult[THR_NEARA ] = thresh; 1083 } 1084 1085 // Disable other intra prediction modes 1086 sf->thresh_mult[THR_TM] = INT_MAX; 1087 sf->thresh_mult[THR_V_PRED] = INT_MAX; 1088 sf->thresh_mult[THR_H_PRED] = INT_MAX; 1089 1090 } 1091 1092 if (Speed > 8) 1093 { 1094 sf->quarter_pixel_search = 0; 1095 } 1096 1097 if (Speed > 9) 1098 { 1099 int Tmp = cpi->Speed - 8; 1100 1101 if (Tmp > 4) 1102 Tmp = 4; 1103 1104 if (cpi->ref_frame_flags & VP8_GOLD_FLAG) 1105 { 1106 cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1); 1107 cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1); 1108 cpi->mode_check_freq[THR_NEARG] = 1 << Tmp; 1109 cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1); 1110 } 1111 1112 if (cpi->ref_frame_flags & VP8_ALT_FLAG) 1113 { 1114 cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1); 1115 cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1); 1116 cpi->mode_check_freq[THR_NEARA] = 1 << Tmp; 1117 cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1); 1118 } 1119 1120 cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1); 1121 } 1122 1123 cm->filter_type = NORMAL_LOOPFILTER; 1124 1125 if (Speed >= 14) 1126 cm->filter_type = SIMPLE_LOOPFILTER; 1127 1128 if (Speed >= 15) 1129 { 1130 sf->half_pixel_search = 0; // This has a big hit on quality. Last resort 1131 } 1132 1133 vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins)); 1134 1135 }; 1136 1137 if (cpi->sf.search_method == NSTEP) 1138 { 1139 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride); 1140 } 1141 else if (cpi->sf.search_method == DIAMOND) 1142 { 1143 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride); 1144 } 1145 1146 if (cpi->sf.improved_dct) 1147 { 1148 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4); 1149 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4); 1150 } 1151 else 1152 { 1153 cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4); 1154 cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4); 1155 } 1156 1157 cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4); 1158 1159 if (cpi->sf.improved_quant) 1160 { 1161 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, quantb); 1162 } 1163 else 1164 { 1165 cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize, fastquantb); 1166 } 1167 1168 #if CONFIG_RUNTIME_CPU_DETECT 1169 cpi->mb.e_mbd.rtcd = &cpi->common.rtcd; 1170 #endif 1171 1172 if (cpi->sf.iterative_sub_pixel == 1) 1173 { 1174 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively; 1175 } 1176 else if (cpi->sf.quarter_pixel_search) 1177 { 1178 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step; 1179 } 1180 else if (cpi->sf.half_pixel_search) 1181 { 1182 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step; 1183 } 1184 else 1185 { 1186 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step; 1187 } 1188 1189 if (cpi->sf.optimize_coefficients == 1) 1190 cpi->mb.optimize = 1; 1191 else 1192 cpi->mb.optimize = 0; 1193 1194 if (cpi->common.full_pixel) 1195 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step; 1196 1197 #ifdef SPEEDSTATS 1198 frames_at_speed[cpi->Speed]++; 1199 #endif 1200 } 1201 static void alloc_raw_frame_buffers(VP8_COMP *cpi) 1202 { 1203 int i, buffers; 1204 1205 buffers = cpi->oxcf.lag_in_frames; 1206 1207 if (buffers > MAX_LAG_BUFFERS) 1208 buffers = MAX_LAG_BUFFERS; 1209 1210 if (buffers < 1) 1211 buffers = 1; 1212 1213 for (i = 0; i < buffers; i++) 1214 if (vp8_yv12_alloc_frame_buffer(&cpi->src_buffer[i].source_buffer, 1215 cpi->oxcf.Width, cpi->oxcf.Height, 1216 16)) 1217 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1218 "Failed to allocate lag buffer"); 1219 1220 #if VP8_TEMPORAL_ALT_REF 1221 1222 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer.source_buffer, 1223 cpi->oxcf.Width, cpi->oxcf.Height, 16)) 1224 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1225 "Failed to allocate altref buffer"); 1226 1227 #endif 1228 1229 cpi->source_buffer_count = 0; 1230 } 1231 1232 static int vp8_alloc_partition_data(VP8_COMP *cpi) 1233 { 1234 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) * 1235 (cpi->common.mb_rows + 1), 1236 sizeof(PARTITION_INFO)); 1237 if(!cpi->mb.pip) 1238 return ALLOC_FAILURE; 1239 1240 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1; 1241 1242 return 0; 1243 } 1244 1245 void vp8_alloc_compressor_data(VP8_COMP *cpi) 1246 { 1247 VP8_COMMON *cm = & cpi->common; 1248 1249 int width = cm->Width; 1250 int height = cm->Height; 1251 1252 if (vp8_alloc_frame_buffers(cm, width, height)) 1253 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1254 "Failed to allocate frame buffers"); 1255 1256 if (vp8_alloc_partition_data(cpi)) 1257 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1258 "Failed to allocate partition data"); 1259 1260 1261 if ((width & 0xf) != 0) 1262 width += 16 - (width & 0xf); 1263 1264 if ((height & 0xf) != 0) 1265 height += 16 - (height & 0xf); 1266 1267 1268 if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf, 1269 width, height, VP8BORDERINPIXELS)) 1270 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1271 "Failed to allocate last frame buffer"); 1272 1273 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height, 16)) 1274 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1275 "Failed to allocate scaled source buffer"); 1276 1277 1278 if (cpi->tok != 0) 1279 vpx_free(cpi->tok); 1280 1281 { 1282 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16; 1283 1284 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); 1285 } 1286 1287 // Data used for real time vc mode to see if gf needs refreshing 1288 cpi->inter_zz_count = 0; 1289 cpi->gf_bad_count = 0; 1290 cpi->gf_update_recommended = 0; 1291 1292 1293 // Structures used to minitor GF usage 1294 if (cpi->gf_active_flags != 0) 1295 vpx_free(cpi->gf_active_flags); 1296 1297 CHECK_MEM_ERROR(cpi->gf_active_flags, vpx_calloc(1, cm->mb_rows * cm->mb_cols)); 1298 1299 cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 1300 } 1301 1302 1303 // Quant MOD 1304 static const int q_trans[] = 1305 { 1306 0, 1, 2, 3, 4, 5, 7, 8, 1307 9, 10, 12, 13, 15, 17, 18, 19, 1308 20, 21, 23, 24, 25, 26, 27, 28, 1309 29, 30, 31, 33, 35, 37, 39, 41, 1310 43, 45, 47, 49, 51, 53, 55, 57, 1311 59, 61, 64, 67, 70, 73, 76, 79, 1312 82, 85, 88, 91, 94, 97, 100, 103, 1313 106, 109, 112, 115, 118, 121, 124, 127, 1314 }; 1315 1316 int vp8_reverse_trans(int x) 1317 { 1318 int i; 1319 1320 for (i = 0; i < 64; i++) 1321 if (q_trans[i] >= x) 1322 return i; 1323 1324 return 63; 1325 }; 1326 void vp8_new_frame_rate(VP8_COMP *cpi, double framerate) 1327 { 1328 cpi->oxcf.frame_rate = framerate; 1329 cpi->output_frame_rate = cpi->oxcf.frame_rate; 1330 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate); 1331 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate); 1332 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); 1333 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; 1334 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; 1335 1336 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; 1337 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; 1338 cpi->max_gf_interval = (int)(cpi->output_frame_rate / 2) + 2; 1339 1340 //cpi->max_gf_interval = (int)(cpi->output_frame_rate * 2 / 3) + 1; 1341 //cpi->max_gf_interval = 24; 1342 1343 if (cpi->max_gf_interval < 12) 1344 cpi->max_gf_interval = 12; 1345 1346 1347 // Special conditions when altr ref frame enabled in lagged compress mode 1348 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) 1349 { 1350 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1351 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1352 } 1353 } 1354 1355 1356 static int 1357 rescale(int val, int num, int denom) 1358 { 1359 int64_t llnum = num; 1360 int64_t llden = denom; 1361 int64_t llval = val; 1362 1363 return llval * llnum / llden; 1364 } 1365 1366 1367 void vp8_init_config(VP8_PTR ptr, VP8_CONFIG *oxcf) 1368 { 1369 VP8_COMP *cpi = (VP8_COMP *)(ptr); 1370 VP8_COMMON *cm = &cpi->common; 1371 1372 if (!cpi) 1373 return; 1374 1375 cpi->auto_gold = 1; 1376 cpi->auto_adjust_gold_quantizer = 1; 1377 cpi->goldquantizer = 1; 1378 cpi->goldfreq = 7; 1379 cpi->auto_adjust_key_quantizer = 1; 1380 cpi->keyquantizer = 1; 1381 1382 cm->version = oxcf->Version; 1383 vp8_setup_version(cm); 1384 1385 if (oxcf == 0) 1386 { 1387 cpi->pass = 0; 1388 1389 cpi->auto_worst_q = 0; 1390 cpi->oxcf.best_allowed_q = MINQ; 1391 cpi->oxcf.worst_allowed_q = MAXQ; 1392 1393 cpi->oxcf.end_usage = USAGE_STREAM_FROM_SERVER; 1394 cpi->oxcf.starting_buffer_level = 4000; 1395 cpi->oxcf.optimal_buffer_level = 5000; 1396 cpi->oxcf.maximum_buffer_size = 6000; 1397 cpi->oxcf.under_shoot_pct = 90; 1398 cpi->oxcf.allow_df = 0; 1399 cpi->oxcf.drop_frames_water_mark = 20; 1400 1401 cpi->oxcf.allow_spatial_resampling = 0; 1402 cpi->oxcf.resample_down_water_mark = 40; 1403 cpi->oxcf.resample_up_water_mark = 60; 1404 1405 cpi->oxcf.fixed_q = cpi->interquantizer; 1406 1407 cpi->filter_type = NORMAL_LOOPFILTER; 1408 1409 if (cm->simpler_lpf) 1410 cpi->filter_type = SIMPLE_LOOPFILTER; 1411 1412 cpi->compressor_speed = 1; 1413 cpi->horiz_scale = 0; 1414 cpi->vert_scale = 0; 1415 cpi->oxcf.two_pass_vbrbias = 50; 1416 cpi->oxcf.two_pass_vbrmax_section = 400; 1417 cpi->oxcf.two_pass_vbrmin_section = 0; 1418 1419 cpi->oxcf.Sharpness = 0; 1420 cpi->oxcf.noise_sensitivity = 0; 1421 } 1422 else 1423 cpi->oxcf = *oxcf; 1424 1425 1426 switch (cpi->oxcf.Mode) 1427 { 1428 1429 case MODE_REALTIME: 1430 cpi->pass = 0; 1431 cpi->compressor_speed = 2; 1432 1433 if (cpi->oxcf.cpu_used < -16) 1434 { 1435 cpi->oxcf.cpu_used = -16; 1436 } 1437 1438 if (cpi->oxcf.cpu_used > 16) 1439 cpi->oxcf.cpu_used = 16; 1440 1441 break; 1442 1443 #if !(CONFIG_REALTIME_ONLY) 1444 case MODE_GOODQUALITY: 1445 cpi->pass = 0; 1446 cpi->compressor_speed = 1; 1447 1448 if (cpi->oxcf.cpu_used < -5) 1449 { 1450 cpi->oxcf.cpu_used = -5; 1451 } 1452 1453 if (cpi->oxcf.cpu_used > 5) 1454 cpi->oxcf.cpu_used = 5; 1455 1456 break; 1457 1458 case MODE_BESTQUALITY: 1459 cpi->pass = 0; 1460 cpi->compressor_speed = 0; 1461 break; 1462 1463 case MODE_FIRSTPASS: 1464 cpi->pass = 1; 1465 cpi->compressor_speed = 1; 1466 break; 1467 case MODE_SECONDPASS: 1468 cpi->pass = 2; 1469 cpi->compressor_speed = 1; 1470 1471 if (cpi->oxcf.cpu_used < -5) 1472 { 1473 cpi->oxcf.cpu_used = -5; 1474 } 1475 1476 if (cpi->oxcf.cpu_used > 5) 1477 cpi->oxcf.cpu_used = 5; 1478 1479 break; 1480 case MODE_SECONDPASS_BEST: 1481 cpi->pass = 2; 1482 cpi->compressor_speed = 0; 1483 break; 1484 #endif 1485 } 1486 1487 if (cpi->pass == 0) 1488 cpi->auto_worst_q = 1; 1489 1490 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; 1491 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; 1492 1493 if (oxcf->fixed_q >= 0) 1494 { 1495 if (oxcf->worst_allowed_q < 0) 1496 cpi->oxcf.fixed_q = q_trans[0]; 1497 else 1498 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q]; 1499 1500 if (oxcf->alt_q < 0) 1501 cpi->oxcf.alt_q = q_trans[0]; 1502 else 1503 cpi->oxcf.alt_q = q_trans[oxcf->alt_q]; 1504 1505 if (oxcf->key_q < 0) 1506 cpi->oxcf.key_q = q_trans[0]; 1507 else 1508 cpi->oxcf.key_q = q_trans[oxcf->key_q]; 1509 1510 if (oxcf->gold_q < 0) 1511 cpi->oxcf.gold_q = q_trans[0]; 1512 else 1513 cpi->oxcf.gold_q = q_trans[oxcf->gold_q]; 1514 1515 } 1516 1517 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL; 1518 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG; 1519 1520 //cpi->use_golden_frame_only = 0; 1521 //cpi->use_last_frame_only = 0; 1522 cm->refresh_golden_frame = 0; 1523 cm->refresh_last_frame = 1; 1524 cm->refresh_entropy_probs = 1; 1525 1526 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) 1527 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions; 1528 1529 setup_features(cpi); 1530 1531 { 1532 int i; 1533 1534 for (i = 0; i < MAX_MB_SEGMENTS; i++) 1535 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 1536 } 1537 1538 // At the moment the first order values may not be > MAXQ 1539 if (cpi->oxcf.fixed_q > MAXQ) 1540 cpi->oxcf.fixed_q = MAXQ; 1541 1542 // local file playback mode == really big buffer 1543 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) 1544 { 1545 cpi->oxcf.starting_buffer_level = 60000; 1546 cpi->oxcf.optimal_buffer_level = 60000; 1547 cpi->oxcf.maximum_buffer_size = 240000; 1548 1549 } 1550 1551 1552 // Convert target bandwidth from Kbit/s to Bit/s 1553 cpi->oxcf.target_bandwidth *= 1000; 1554 cpi->oxcf.starting_buffer_level = 1555 rescale(cpi->oxcf.starting_buffer_level, 1556 cpi->oxcf.target_bandwidth, 1000); 1557 1558 if (cpi->oxcf.optimal_buffer_level == 0) 1559 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 1560 else 1561 cpi->oxcf.optimal_buffer_level = 1562 rescale(cpi->oxcf.optimal_buffer_level, 1563 cpi->oxcf.target_bandwidth, 1000); 1564 1565 if (cpi->oxcf.maximum_buffer_size == 0) 1566 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 1567 else 1568 cpi->oxcf.maximum_buffer_size = 1569 rescale(cpi->oxcf.maximum_buffer_size, 1570 cpi->oxcf.target_bandwidth, 1000); 1571 1572 cpi->buffer_level = cpi->oxcf.starting_buffer_level; 1573 cpi->bits_off_target = cpi->oxcf.starting_buffer_level; 1574 1575 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate); 1576 cpi->worst_quality = cpi->oxcf.worst_allowed_q; 1577 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1578 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1579 cpi->best_quality = cpi->oxcf.best_allowed_q; 1580 cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1581 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE; 1582 1583 1584 cpi->total_actual_bits = 0; 1585 cpi->total_target_vs_actual = 0; 1586 1587 // Only allow dropped frames in buffered mode 1588 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode; 1589 1590 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type; 1591 1592 if (!cm->use_bilinear_mc_filter) 1593 cm->mcomp_filter_type = SIXTAP; 1594 else 1595 cm->mcomp_filter_type = BILINEAR; 1596 1597 cpi->target_bandwidth = cpi->oxcf.target_bandwidth; 1598 1599 cm->Width = cpi->oxcf.Width ; 1600 cm->Height = cpi->oxcf.Height ; 1601 1602 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8 1603 1604 cm->horiz_scale = cpi->horiz_scale; 1605 cm->vert_scale = cpi->vert_scale ; 1606 1607 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) 1608 if (cpi->oxcf.Sharpness > 7) 1609 cpi->oxcf.Sharpness = 7; 1610 1611 cm->sharpness_level = cpi->oxcf.Sharpness; 1612 1613 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) 1614 { 1615 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 1616 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 1617 1618 Scale2Ratio(cm->horiz_scale, &hr, &hs); 1619 Scale2Ratio(cm->vert_scale, &vr, &vs); 1620 1621 // always go to the next whole number 1622 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs; 1623 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs; 1624 } 1625 1626 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width || 1627 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height || 1628 cm->yv12_fb[cm->lst_fb_idx].y_width == 0) 1629 { 1630 alloc_raw_frame_buffers(cpi); 1631 vp8_alloc_compressor_data(cpi); 1632 } 1633 1634 // Clamp KF frame size to quarter of data rate 1635 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2) 1636 cpi->intra_frame_target = cpi->target_bandwidth >> 2; 1637 1638 if (cpi->oxcf.fixed_q >= 0) 1639 { 1640 cpi->last_q[0] = cpi->oxcf.fixed_q; 1641 cpi->last_q[1] = cpi->oxcf.fixed_q; 1642 } 1643 1644 cpi->Speed = cpi->oxcf.cpu_used; 1645 1646 // force to allowlag to 0 if lag_in_frames is 0; 1647 if (cpi->oxcf.lag_in_frames == 0) 1648 { 1649 cpi->oxcf.allow_lag = 0; 1650 } 1651 // Limit on lag buffers as these are not currently dynamically allocated 1652 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) 1653 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 1654 1655 // YX Temp 1656 cpi->last_alt_ref_sei = -1; 1657 cpi->is_src_frame_alt_ref = 0; 1658 1659 #if 0 1660 // Experimental RD Code 1661 cpi->frame_distortion = 0; 1662 cpi->last_frame_distortion = 0; 1663 #endif 1664 1665 #if VP8_TEMPORAL_ALT_REF 1666 { 1667 int i; 1668 1669 cpi->fixed_divide[0] = 0; 1670 1671 for (i = 1; i < 255; i++) 1672 cpi->fixed_divide[i] = 0x10000 / i; 1673 } 1674 #endif 1675 } 1676 1677 /* 1678 * This function needs more clean up, i.e. be more tuned torwards 1679 * change_config rather than init_config !!!!!!!!!!!!!!!! 1680 * YX - 5/28/2009 1681 * 1682 */ 1683 1684 void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf) 1685 { 1686 VP8_COMP *cpi = (VP8_COMP *)(ptr); 1687 VP8_COMMON *cm = &cpi->common; 1688 1689 if (!cpi) 1690 return; 1691 1692 if (!oxcf) 1693 return; 1694 1695 if (cm->version != oxcf->Version) 1696 { 1697 cm->version = oxcf->Version; 1698 vp8_setup_version(cm); 1699 } 1700 1701 cpi->oxcf = *oxcf; 1702 1703 switch (cpi->oxcf.Mode) 1704 { 1705 1706 case MODE_REALTIME: 1707 cpi->pass = 0; 1708 cpi->compressor_speed = 2; 1709 1710 if (cpi->oxcf.cpu_used < -16) 1711 { 1712 cpi->oxcf.cpu_used = -16; 1713 } 1714 1715 if (cpi->oxcf.cpu_used > 16) 1716 cpi->oxcf.cpu_used = 16; 1717 1718 break; 1719 1720 #if !(CONFIG_REALTIME_ONLY) 1721 case MODE_GOODQUALITY: 1722 cpi->pass = 0; 1723 cpi->compressor_speed = 1; 1724 1725 if (cpi->oxcf.cpu_used < -5) 1726 { 1727 cpi->oxcf.cpu_used = -5; 1728 } 1729 1730 if (cpi->oxcf.cpu_used > 5) 1731 cpi->oxcf.cpu_used = 5; 1732 1733 break; 1734 1735 case MODE_BESTQUALITY: 1736 cpi->pass = 0; 1737 cpi->compressor_speed = 0; 1738 break; 1739 1740 case MODE_FIRSTPASS: 1741 cpi->pass = 1; 1742 cpi->compressor_speed = 1; 1743 break; 1744 case MODE_SECONDPASS: 1745 cpi->pass = 2; 1746 cpi->compressor_speed = 1; 1747 1748 if (cpi->oxcf.cpu_used < -5) 1749 { 1750 cpi->oxcf.cpu_used = -5; 1751 } 1752 1753 if (cpi->oxcf.cpu_used > 5) 1754 cpi->oxcf.cpu_used = 5; 1755 1756 break; 1757 case MODE_SECONDPASS_BEST: 1758 cpi->pass = 2; 1759 cpi->compressor_speed = 0; 1760 break; 1761 #endif 1762 } 1763 1764 if (cpi->pass == 0) 1765 cpi->auto_worst_q = 1; 1766 1767 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; 1768 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; 1769 1770 if (oxcf->fixed_q >= 0) 1771 { 1772 if (oxcf->worst_allowed_q < 0) 1773 cpi->oxcf.fixed_q = q_trans[0]; 1774 else 1775 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q]; 1776 1777 if (oxcf->alt_q < 0) 1778 cpi->oxcf.alt_q = q_trans[0]; 1779 else 1780 cpi->oxcf.alt_q = q_trans[oxcf->alt_q]; 1781 1782 if (oxcf->key_q < 0) 1783 cpi->oxcf.key_q = q_trans[0]; 1784 else 1785 cpi->oxcf.key_q = q_trans[oxcf->key_q]; 1786 1787 if (oxcf->gold_q < 0) 1788 cpi->oxcf.gold_q = q_trans[0]; 1789 else 1790 cpi->oxcf.gold_q = q_trans[oxcf->gold_q]; 1791 1792 } 1793 1794 cpi->baseline_gf_interval = cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL; 1795 1796 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG; 1797 1798 //cpi->use_golden_frame_only = 0; 1799 //cpi->use_last_frame_only = 0; 1800 cm->refresh_golden_frame = 0; 1801 cm->refresh_last_frame = 1; 1802 cm->refresh_entropy_probs = 1; 1803 1804 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) 1805 cm->multi_token_partition = (TOKEN_PARTITION) cpi->oxcf.token_partitions; 1806 1807 setup_features(cpi); 1808 1809 { 1810 int i; 1811 1812 for (i = 0; i < MAX_MB_SEGMENTS; i++) 1813 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 1814 } 1815 1816 // At the moment the first order values may not be > MAXQ 1817 if (cpi->oxcf.fixed_q > MAXQ) 1818 cpi->oxcf.fixed_q = MAXQ; 1819 1820 // local file playback mode == really big buffer 1821 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) 1822 { 1823 cpi->oxcf.starting_buffer_level = 60000; 1824 cpi->oxcf.optimal_buffer_level = 60000; 1825 cpi->oxcf.maximum_buffer_size = 240000; 1826 1827 } 1828 1829 // Convert target bandwidth from Kbit/s to Bit/s 1830 cpi->oxcf.target_bandwidth *= 1000; 1831 1832 cpi->oxcf.starting_buffer_level = 1833 rescale(cpi->oxcf.starting_buffer_level, 1834 cpi->oxcf.target_bandwidth, 1000); 1835 1836 if (cpi->oxcf.optimal_buffer_level == 0) 1837 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 1838 else 1839 cpi->oxcf.optimal_buffer_level = 1840 rescale(cpi->oxcf.optimal_buffer_level, 1841 cpi->oxcf.target_bandwidth, 1000); 1842 1843 if (cpi->oxcf.maximum_buffer_size == 0) 1844 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 1845 else 1846 cpi->oxcf.maximum_buffer_size = 1847 rescale(cpi->oxcf.maximum_buffer_size, 1848 cpi->oxcf.target_bandwidth, 1000); 1849 1850 cpi->buffer_level = cpi->oxcf.starting_buffer_level; 1851 cpi->bits_off_target = cpi->oxcf.starting_buffer_level; 1852 1853 vp8_new_frame_rate(cpi, cpi->oxcf.frame_rate); 1854 cpi->worst_quality = cpi->oxcf.worst_allowed_q; 1855 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1856 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1857 cpi->best_quality = cpi->oxcf.best_allowed_q; 1858 cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1859 cpi->buffered_mode = (cpi->oxcf.optimal_buffer_level > 0) ? TRUE : FALSE; 1860 1861 1862 cpi->total_actual_bits = 0; 1863 cpi->total_target_vs_actual = 0; 1864 1865 // Only allow dropped frames in buffered mode 1866 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode; 1867 1868 cm->filter_type = (LOOPFILTERTYPE) cpi->filter_type; 1869 1870 if (!cm->use_bilinear_mc_filter) 1871 cm->mcomp_filter_type = SIXTAP; 1872 else 1873 cm->mcomp_filter_type = BILINEAR; 1874 1875 cpi->target_bandwidth = cpi->oxcf.target_bandwidth; 1876 1877 cm->Width = cpi->oxcf.Width ; 1878 cm->Height = cpi->oxcf.Height ; 1879 1880 cm->horiz_scale = cpi->horiz_scale; 1881 cm->vert_scale = cpi->vert_scale ; 1882 1883 cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000; // As per VP8 1884 1885 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) 1886 if (cpi->oxcf.Sharpness > 7) 1887 cpi->oxcf.Sharpness = 7; 1888 1889 cm->sharpness_level = cpi->oxcf.Sharpness; 1890 1891 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) 1892 { 1893 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 1894 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 1895 1896 Scale2Ratio(cm->horiz_scale, &hr, &hs); 1897 Scale2Ratio(cm->vert_scale, &vr, &vs); 1898 1899 // always go to the next whole number 1900 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs; 1901 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs; 1902 } 1903 1904 if (((cm->Width + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_width || 1905 ((cm->Height + 15) & 0xfffffff0) != cm->yv12_fb[cm->lst_fb_idx].y_height || 1906 cm->yv12_fb[cm->lst_fb_idx].y_width == 0) 1907 { 1908 alloc_raw_frame_buffers(cpi); 1909 vp8_alloc_compressor_data(cpi); 1910 } 1911 1912 // Clamp KF frame size to quarter of data rate 1913 if (cpi->intra_frame_target > cpi->target_bandwidth >> 2) 1914 cpi->intra_frame_target = cpi->target_bandwidth >> 2; 1915 1916 if (cpi->oxcf.fixed_q >= 0) 1917 { 1918 cpi->last_q[0] = cpi->oxcf.fixed_q; 1919 cpi->last_q[1] = cpi->oxcf.fixed_q; 1920 } 1921 1922 cpi->Speed = cpi->oxcf.cpu_used; 1923 1924 // force to allowlag to 0 if lag_in_frames is 0; 1925 if (cpi->oxcf.lag_in_frames == 0) 1926 { 1927 cpi->oxcf.allow_lag = 0; 1928 } 1929 // Limit on lag buffers as these are not currently dynamically allocated 1930 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) 1931 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 1932 1933 // YX Temp 1934 cpi->last_alt_ref_sei = -1; 1935 cpi->is_src_frame_alt_ref = 0; 1936 1937 #if 0 1938 // Experimental RD Code 1939 cpi->frame_distortion = 0; 1940 cpi->last_frame_distortion = 0; 1941 #endif 1942 1943 } 1944 1945 #define M_LOG2_E 0.693147180559945309417 1946 #define log2f(x) (log (x) / (float) M_LOG2_E) 1947 static void cal_mvsadcosts(int *mvsadcost[2]) 1948 { 1949 int i = 1; 1950 1951 mvsadcost [0] [0] = 300; 1952 mvsadcost [1] [0] = 300; 1953 1954 do 1955 { 1956 double z = 256 * (2 * (log2f(2 * i) + .6)); 1957 mvsadcost [0][i] = (int) z; 1958 mvsadcost [1][i] = (int) z; 1959 mvsadcost [0][-i] = (int) z; 1960 mvsadcost [1][-i] = (int) z; 1961 } 1962 while (++i <= mv_max); 1963 } 1964 1965 VP8_PTR vp8_create_compressor(VP8_CONFIG *oxcf) 1966 { 1967 int i; 1968 volatile union 1969 { 1970 VP8_COMP *cpi; 1971 VP8_PTR ptr; 1972 } ctx; 1973 1974 VP8_COMP *cpi; 1975 VP8_COMMON *cm; 1976 1977 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP8_COMP)); 1978 // Check that the CPI instance is valid 1979 if (!cpi) 1980 return 0; 1981 1982 cm = &cpi->common; 1983 1984 vpx_memset(cpi, 0, sizeof(VP8_COMP)); 1985 1986 if (setjmp(cm->error.jmp)) 1987 { 1988 VP8_PTR ptr = ctx.ptr; 1989 1990 ctx.cpi->common.error.setjmp = 0; 1991 vp8_remove_compressor(&ptr); 1992 return 0; 1993 } 1994 1995 cpi->common.error.setjmp = 1; 1996 1997 CHECK_MEM_ERROR(cpi->rdtok, vpx_calloc(256 * 3 / 2, sizeof(TOKENEXTRA))); 1998 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1)); 1999 2000 vp8_cmachine_specific_config(cpi); 2001 vp8_create_common(&cpi->common); 2002 2003 vp8_init_config((VP8_PTR)cpi, oxcf); 2004 2005 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob)); 2006 cpi->common.current_video_frame = 0; 2007 cpi->kf_overspend_bits = 0; 2008 cpi->kf_bitrate_adjustment = 0; 2009 cpi->frames_till_gf_update_due = 0; 2010 cpi->gf_overspend_bits = 0; 2011 cpi->non_gf_bitrate_adjustment = 0; 2012 cpi->prob_last_coded = 128; 2013 cpi->prob_gf_coded = 128; 2014 cpi->prob_intra_coded = 63; 2015 2016 // Prime the recent reference frame useage counters. 2017 // Hereafter they will be maintained as a sort of moving average 2018 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1; 2019 cpi->recent_ref_frame_usage[LAST_FRAME] = 1; 2020 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1; 2021 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1; 2022 2023 // Set reference frame sign bias for ALTREF frame to 1 (for now) 2024 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1; 2025 2026 cpi->gf_decay_rate = 0; 2027 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 2028 2029 cpi->gold_is_last = 0 ; 2030 cpi->alt_is_last = 0 ; 2031 cpi->gold_is_alt = 0 ; 2032 2033 2034 2035 // Create the encoder segmentation map and set all entries to 0 2036 CHECK_MEM_ERROR(cpi->segmentation_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1)); 2037 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1)); 2038 vpx_memset(cpi->active_map , 1, (cpi->common.mb_rows * cpi->common.mb_cols)); 2039 cpi->active_map_enabled = 0; 2040 2041 // Create the first pass motion map structure and set to 0 2042 CHECK_MEM_ERROR(cpi->fp_motion_map, vpx_calloc(cpi->common.MBs, 1)); 2043 2044 #if 0 2045 // Experimental code for lagged and one pass 2046 // Initialise one_pass GF frames stats 2047 // Update stats used for GF selection 2048 if (cpi->pass == 0) 2049 { 2050 cpi->one_pass_frame_index = 0; 2051 2052 for (i = 0; i < MAX_LAG_BUFFERS; i++) 2053 { 2054 cpi->one_pass_frame_stats[i].frames_so_far = 0; 2055 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0; 2056 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0; 2057 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0; 2058 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0; 2059 cpi->one_pass_frame_stats[i].frame_mvr = 0.0; 2060 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0; 2061 cpi->one_pass_frame_stats[i].frame_mvc = 0.0; 2062 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0; 2063 } 2064 } 2065 #endif 2066 2067 // Should we use the cyclic refresh method. 2068 // Currently this is tied to error resilliant mode 2069 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode; 2070 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 40; 2071 cpi->cyclic_refresh_mode_index = 0; 2072 cpi->cyclic_refresh_q = 32; 2073 2074 if (cpi->cyclic_refresh_mode_enabled) 2075 { 2076 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1)); 2077 } 2078 else 2079 cpi->cyclic_refresh_map = (signed char *) NULL; 2080 2081 // Test function for segmentation 2082 //segmentation_test_function((VP8_PTR) cpi); 2083 2084 // Loop filter mode / ref deltas test function 2085 //mode_ref_lf_test_function(cpi); 2086 2087 #ifdef ENTROPY_STATS 2088 init_context_counters(); 2089 #endif 2090 2091 2092 #ifdef INTRARDOPT 2093 cpi->intra_rd_opt = 1; 2094 2095 #endif 2096 2097 cpi->frames_since_key = 8; // Give a sensible default for the first frame. 2098 cpi->key_frame_frequency = cpi->oxcf.key_freq; 2099 2100 cpi->source_alt_ref_pending = FALSE; 2101 cpi->source_alt_ref_active = FALSE; 2102 cpi->common.refresh_alt_ref_frame = 0; 2103 2104 cpi->b_calculate_psnr = CONFIG_PSNR; 2105 #if CONFIG_PSNR 2106 cpi->b_calculate_ssimg = 0; 2107 2108 cpi->count = 0; 2109 cpi->bytes = 0; 2110 2111 if (cpi->b_calculate_psnr) 2112 { 2113 cpi->total_sq_error = 0.0; 2114 cpi->total_sq_error2 = 0.0; 2115 cpi->total_y = 0.0; 2116 cpi->total_u = 0.0; 2117 cpi->total_v = 0.0; 2118 cpi->total = 0.0; 2119 cpi->totalp_y = 0.0; 2120 cpi->totalp_u = 0.0; 2121 cpi->totalp_v = 0.0; 2122 cpi->totalp = 0.0; 2123 cpi->tot_recode_hits = 0; 2124 cpi->summed_quality = 0; 2125 cpi->summed_weights = 0; 2126 } 2127 2128 if (cpi->b_calculate_ssimg) 2129 { 2130 cpi->total_ssimg_y = 0; 2131 cpi->total_ssimg_u = 0; 2132 cpi->total_ssimg_v = 0; 2133 cpi->total_ssimg_all = 0; 2134 } 2135 2136 #ifndef LLONG_MAX 2137 #define LLONG_MAX 9223372036854775807LL 2138 #endif 2139 cpi->first_time_stamp_ever = LLONG_MAX; 2140 2141 #endif 2142 2143 cpi->frames_till_gf_update_due = 0; 2144 cpi->key_frame_count = 1; 2145 cpi->tot_key_frame_bits = 0; 2146 2147 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; 2148 cpi->ni_tot_qi = 0; 2149 cpi->ni_frames = 0; 2150 cpi->total_byte_count = 0; 2151 2152 cpi->drop_frame = 0; 2153 cpi->drop_count = 0; 2154 cpi->max_drop_count = 0; 2155 cpi->max_consec_dropped_frames = 4; 2156 2157 cpi->rate_correction_factor = 1.0; 2158 cpi->key_frame_rate_correction_factor = 1.0; 2159 cpi->gf_rate_correction_factor = 1.0; 2160 cpi->est_max_qcorrection_factor = 1.0; 2161 2162 cpi->mb.mvcost[0] = &cpi->mb.mvcosts[0][mv_max+1]; 2163 cpi->mb.mvcost[1] = &cpi->mb.mvcosts[1][mv_max+1]; 2164 cpi->mb.mvsadcost[0] = &cpi->mb.mvsadcosts[0][mv_max+1]; 2165 cpi->mb.mvsadcost[1] = &cpi->mb.mvsadcosts[1][mv_max+1]; 2166 2167 cal_mvsadcosts(cpi->mb.mvsadcost); 2168 2169 for (i = 0; i < KEY_FRAME_CONTEXT; i++) 2170 { 2171 cpi->prior_key_frame_size[i] = cpi->intra_frame_target; 2172 cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate; 2173 } 2174 2175 cpi->check_freq[0] = 15; 2176 cpi->check_freq[1] = 15; 2177 2178 #ifdef OUTPUT_YUV_SRC 2179 yuv_file = fopen("bd.yuv", "ab"); 2180 #endif 2181 2182 #if 0 2183 framepsnr = fopen("framepsnr.stt", "a"); 2184 kf_list = fopen("kf_list.stt", "w"); 2185 #endif 2186 2187 cpi->output_pkt_list = oxcf->output_pkt_list; 2188 2189 #if !(CONFIG_REALTIME_ONLY) 2190 2191 if (cpi->pass == 1) 2192 { 2193 vp8_init_first_pass(cpi); 2194 } 2195 else if (cpi->pass == 2) 2196 { 2197 cpi->stats_in = oxcf->two_pass_stats_in.buf; 2198 cpi->stats_in_end = cpi->stats_in 2199 + oxcf->two_pass_stats_in.sz / sizeof(FIRSTPASS_STATS) 2200 - 1; 2201 vp8_init_second_pass(cpi); 2202 } 2203 2204 #endif 2205 2206 if (cpi->compressor_speed == 2) 2207 { 2208 cpi->cpu_freq = 0; //vp8_get_processor_freq(); 2209 cpi->avg_encode_time = 0; 2210 cpi->avg_pick_mode_time = 0; 2211 } 2212 2213 vp8_set_speed_features(cpi); 2214 2215 // Set starting values of RD threshold multipliers (128 = *1) 2216 for (i = 0; i < MAX_MODES; i++) 2217 { 2218 cpi->rd_thresh_mult[i] = 128; 2219 } 2220 2221 #ifdef ENTROPY_STATS 2222 init_mv_ref_counts(); 2223 #endif 2224 2225 vp8cx_create_encoder_threads(cpi); 2226 2227 cpi->fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16); 2228 cpi->fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x16); 2229 cpi->fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x16); 2230 cpi->fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x3); 2231 cpi->fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x16x4d); 2232 2233 #if !(CONFIG_REALTIME_ONLY) 2234 cpi->full_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, full_search); 2235 #endif 2236 cpi->diamond_search_sad = SEARCH_INVOKE(&cpi->rtcd.search, diamond_search); 2237 2238 cpi->ready_for_new_frame = 1; 2239 2240 cpi->source_encode_index = 0; 2241 2242 // make sure frame 1 is okay 2243 cpi->error_bins[0] = cpi->common.MBs; 2244 2245 //vp8cx_init_quantizer() is first called here. Add check in vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only called later 2246 //when needed. This will avoid unnecessary calls of vp8cx_init_quantizer() for every frame. 2247 vp8cx_init_quantizer(cpi); 2248 { 2249 vp8_init_loop_filter(cm); 2250 cm->last_frame_type = KEY_FRAME; 2251 cm->last_filter_type = cm->filter_type; 2252 cm->last_sharpness_level = cm->sharpness_level; 2253 } 2254 cpi->common.error.setjmp = 0; 2255 return (VP8_PTR) cpi; 2256 2257 } 2258 2259 2260 void vp8_remove_compressor(VP8_PTR *ptr) 2261 { 2262 VP8_COMP *cpi = (VP8_COMP *)(*ptr); 2263 2264 if (!cpi) 2265 return; 2266 2267 if (cpi && (cpi->common.current_video_frame > 0)) 2268 { 2269 #if !(CONFIG_REALTIME_ONLY) 2270 2271 if (cpi->pass == 2) 2272 { 2273 vp8_end_second_pass(cpi); 2274 } 2275 2276 #endif 2277 2278 #ifdef ENTROPY_STATS 2279 print_context_counters(); 2280 print_tree_update_probs(); 2281 print_mode_context(); 2282 #endif 2283 2284 #if CONFIG_PSNR 2285 2286 if (cpi->pass != 1) 2287 { 2288 FILE *f = fopen("opsnr.stt", "a"); 2289 double time_encoded = (cpi->source_end_time_stamp - cpi->first_time_stamp_ever) / 10000000.000; 2290 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000; 2291 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded; 2292 2293 if (cpi->b_calculate_psnr) 2294 { 2295 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx]; 2296 double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height; 2297 double total_psnr = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error); 2298 double total_psnr2 = vp8_mse2psnr(samples, 255.0, cpi->total_sq_error2); 2299 double total_ssim = 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0); 2300 2301 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\tVPXSSIM\t Time(us)\n"); 2302 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f %8.0f\n", 2303 dr, cpi->total / cpi->count, total_psnr, cpi->totalp / cpi->count, total_psnr2, total_ssim, 2304 total_encode_time); 2305 } 2306 2307 if (cpi->b_calculate_ssimg) 2308 { 2309 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(us)\n"); 2310 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, 2311 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count, 2312 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time); 2313 } 2314 2315 fclose(f); 2316 #if 0 2317 f = fopen("qskip.stt", "a"); 2318 fprintf(f, "minq:%d -maxq:%d skipture:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount); 2319 fclose(f); 2320 #endif 2321 2322 } 2323 2324 #endif 2325 2326 2327 #ifdef SPEEDSTATS 2328 2329 if (cpi->compressor_speed == 2) 2330 { 2331 int i; 2332 FILE *f = fopen("cxspeed.stt", "a"); 2333 cnt_pm /= cpi->common.MBs; 2334 2335 for (i = 0; i < 16; i++) 2336 fprintf(f, "%5d", frames_at_speed[i]); 2337 2338 fprintf(f, "\n"); 2339 //fprintf(f, "%10d PM %10d %10d %10d EF %10d %10d %10d\n", cpi->Speed, cpi->avg_pick_mode_time, (tot_pm/cnt_pm), cnt_pm, cpi->avg_encode_time, 0, 0); 2340 fclose(f); 2341 } 2342 2343 #endif 2344 2345 2346 #ifdef MODE_STATS 2347 { 2348 extern int count_mb_seg[4]; 2349 FILE *f = fopen("modes.stt", "a"); 2350 double dr = (double)cpi->oxcf.frame_rate * (double)bytes * (double)8 / (double)count / (double)1000 ; 2351 fprintf(f, "intra_mode in Intra Frames:\n"); 2352 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]); 2353 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]); 2354 fprintf(f, "B: "); 2355 { 2356 int i; 2357 2358 for (i = 0; i < 10; i++) 2359 fprintf(f, "%8d, ", b_modes[i]); 2360 2361 fprintf(f, "\n"); 2362 2363 } 2364 2365 fprintf(f, "Modes in Inter Frames:\n"); 2366 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n", 2367 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4], 2368 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]); 2369 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]); 2370 fprintf(f, "B: "); 2371 { 2372 int i; 2373 2374 for (i = 0; i < 15; i++) 2375 fprintf(f, "%8d, ", inter_b_modes[i]); 2376 2377 fprintf(f, "\n"); 2378 2379 } 2380 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]); 2381 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]); 2382 2383 2384 2385 fclose(f); 2386 } 2387 #endif 2388 2389 #ifdef ENTROPY_STATS 2390 { 2391 int i, j, k; 2392 FILE *fmode = fopen("modecontext.c", "w"); 2393 2394 fprintf(fmode, "\n#include \"entropymode.h\"\n\n"); 2395 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts "); 2396 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n"); 2397 2398 for (i = 0; i < 10; i++) 2399 { 2400 2401 fprintf(fmode, " { //Above Mode : %d\n", i); 2402 2403 for (j = 0; j < 10; j++) 2404 { 2405 2406 fprintf(fmode, " {"); 2407 2408 for (k = 0; k < 10; k++) 2409 { 2410 if (!intra_mode_stats[i][j][k]) 2411 fprintf(fmode, " %5d, ", 1); 2412 else 2413 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]); 2414 } 2415 2416 fprintf(fmode, "}, // left_mode %d\n", j); 2417 2418 } 2419 2420 fprintf(fmode, " },\n"); 2421 2422 } 2423 2424 fprintf(fmode, "};\n"); 2425 } 2426 #endif 2427 2428 2429 #if defined(SECTIONBITS_OUTPUT) 2430 2431 if (0) 2432 { 2433 int i; 2434 FILE *f = fopen("tokenbits.stt", "a"); 2435 2436 for (i = 0; i < 28; i++) 2437 fprintf(f, "%8d", (int)(Sectionbits[i] / 256)); 2438 2439 fprintf(f, "\n"); 2440 fclose(f); 2441 } 2442 2443 #endif 2444 2445 #if 0 2446 { 2447 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); 2448 printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); 2449 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000); 2450 } 2451 #endif 2452 2453 } 2454 2455 vp8cx_remove_encoder_threads(cpi); 2456 2457 vp8_dealloc_compressor_data(cpi); 2458 vpx_free(cpi->mb.ss); 2459 vpx_free(cpi->tok); 2460 vpx_free(cpi->rdtok); 2461 vpx_free(cpi->cyclic_refresh_map); 2462 2463 vp8_remove_common(&cpi->common); 2464 vpx_free(cpi); 2465 *ptr = 0; 2466 2467 #ifdef OUTPUT_YUV_SRC 2468 fclose(yuv_file); 2469 #endif 2470 2471 #if 0 2472 2473 if (keyfile) 2474 fclose(keyfile); 2475 2476 if (framepsnr) 2477 fclose(framepsnr); 2478 2479 if (kf_list) 2480 fclose(kf_list); 2481 2482 #endif 2483 2484 } 2485 2486 2487 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride, 2488 unsigned char *recon, int recon_stride, 2489 unsigned int cols, unsigned int rows, 2490 vp8_variance_rtcd_vtable_t *rtcd) 2491 { 2492 unsigned int row, col; 2493 uint64_t total_sse = 0; 2494 int diff; 2495 2496 for (row = 0; row + 16 <= rows; row += 16) 2497 { 2498 for (col = 0; col + 16 <= cols; col += 16) 2499 { 2500 unsigned int sse; 2501 2502 VARIANCE_INVOKE(rtcd, mse16x16)(orig + col, orig_stride, 2503 recon + col, recon_stride, 2504 &sse); 2505 total_sse += sse; 2506 } 2507 2508 /* Handle odd-sized width */ 2509 if (col < cols) 2510 { 2511 unsigned int border_row, border_col; 2512 unsigned char *border_orig = orig; 2513 unsigned char *border_recon = recon; 2514 2515 for (border_row = 0; border_row < 16; border_row++) 2516 { 2517 for (border_col = col; border_col < cols; border_col++) 2518 { 2519 diff = border_orig[border_col] - border_recon[border_col]; 2520 total_sse += diff * diff; 2521 } 2522 2523 border_orig += orig_stride; 2524 border_recon += recon_stride; 2525 } 2526 } 2527 2528 orig += orig_stride * 16; 2529 recon += recon_stride * 16; 2530 } 2531 2532 /* Handle odd-sized height */ 2533 for (; row < rows; row++) 2534 { 2535 for (col = 0; col < cols; col++) 2536 { 2537 diff = orig[col] - recon[col]; 2538 total_sse += diff * diff; 2539 } 2540 2541 orig += orig_stride; 2542 recon += recon_stride; 2543 } 2544 2545 return total_sse; 2546 } 2547 2548 2549 static void generate_psnr_packet(VP8_COMP *cpi) 2550 { 2551 YV12_BUFFER_CONFIG *orig = cpi->Source; 2552 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 2553 struct vpx_codec_cx_pkt pkt; 2554 uint64_t sse; 2555 int i; 2556 unsigned int width = cpi->common.Width; 2557 unsigned int height = cpi->common.Height; 2558 2559 pkt.kind = VPX_CODEC_PSNR_PKT; 2560 sse = calc_plane_error(orig->y_buffer, orig->y_stride, 2561 recon->y_buffer, recon->y_stride, 2562 width, height, 2563 IF_RTCD(&cpi->rtcd.variance)); 2564 pkt.data.psnr.sse[0] = sse; 2565 pkt.data.psnr.sse[1] = sse; 2566 pkt.data.psnr.samples[0] = width * height; 2567 pkt.data.psnr.samples[1] = width * height; 2568 2569 width = (width + 1) / 2; 2570 height = (height + 1) / 2; 2571 2572 sse = calc_plane_error(orig->u_buffer, orig->uv_stride, 2573 recon->u_buffer, recon->uv_stride, 2574 width, height, 2575 IF_RTCD(&cpi->rtcd.variance)); 2576 pkt.data.psnr.sse[0] += sse; 2577 pkt.data.psnr.sse[2] = sse; 2578 pkt.data.psnr.samples[0] += width * height; 2579 pkt.data.psnr.samples[2] = width * height; 2580 2581 sse = calc_plane_error(orig->v_buffer, orig->uv_stride, 2582 recon->v_buffer, recon->uv_stride, 2583 width, height, 2584 IF_RTCD(&cpi->rtcd.variance)); 2585 pkt.data.psnr.sse[0] += sse; 2586 pkt.data.psnr.sse[3] = sse; 2587 pkt.data.psnr.samples[0] += width * height; 2588 pkt.data.psnr.samples[3] = width * height; 2589 2590 for (i = 0; i < 4; i++) 2591 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0, 2592 pkt.data.psnr.sse[i]); 2593 2594 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); 2595 } 2596 2597 2598 int vp8_use_as_reference(VP8_PTR ptr, int ref_frame_flags) 2599 { 2600 VP8_COMP *cpi = (VP8_COMP *)(ptr); 2601 2602 if (ref_frame_flags > 7) 2603 return -1 ; 2604 2605 cpi->ref_frame_flags = ref_frame_flags; 2606 return 0; 2607 } 2608 int vp8_update_reference(VP8_PTR ptr, int ref_frame_flags) 2609 { 2610 VP8_COMP *cpi = (VP8_COMP *)(ptr); 2611 2612 if (ref_frame_flags > 7) 2613 return -1 ; 2614 2615 cpi->common.refresh_golden_frame = 0; 2616 cpi->common.refresh_alt_ref_frame = 0; 2617 cpi->common.refresh_last_frame = 0; 2618 2619 if (ref_frame_flags & VP8_LAST_FLAG) 2620 cpi->common.refresh_last_frame = 1; 2621 2622 if (ref_frame_flags & VP8_GOLD_FLAG) 2623 cpi->common.refresh_golden_frame = 1; 2624 2625 if (ref_frame_flags & VP8_ALT_FLAG) 2626 cpi->common.refresh_alt_ref_frame = 1; 2627 2628 return 0; 2629 } 2630 2631 int vp8_get_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd) 2632 { 2633 VP8_COMP *cpi = (VP8_COMP *)(ptr); 2634 VP8_COMMON *cm = &cpi->common; 2635 int ref_fb_idx; 2636 2637 if (ref_frame_flag == VP8_LAST_FLAG) 2638 ref_fb_idx = cm->lst_fb_idx; 2639 else if (ref_frame_flag == VP8_GOLD_FLAG) 2640 ref_fb_idx = cm->gld_fb_idx; 2641 else if (ref_frame_flag == VP8_ALT_FLAG) 2642 ref_fb_idx = cm->alt_fb_idx; 2643 else 2644 return -1; 2645 2646 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd); 2647 2648 return 0; 2649 } 2650 int vp8_set_reference(VP8_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd) 2651 { 2652 VP8_COMP *cpi = (VP8_COMP *)(ptr); 2653 VP8_COMMON *cm = &cpi->common; 2654 2655 int ref_fb_idx; 2656 2657 if (ref_frame_flag == VP8_LAST_FLAG) 2658 ref_fb_idx = cm->lst_fb_idx; 2659 else if (ref_frame_flag == VP8_GOLD_FLAG) 2660 ref_fb_idx = cm->gld_fb_idx; 2661 else if (ref_frame_flag == VP8_ALT_FLAG) 2662 ref_fb_idx = cm->alt_fb_idx; 2663 else 2664 return -1; 2665 2666 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]); 2667 2668 return 0; 2669 } 2670 int vp8_update_entropy(VP8_PTR comp, int update) 2671 { 2672 VP8_COMP *cpi = (VP8_COMP *) comp; 2673 VP8_COMMON *cm = &cpi->common; 2674 cm->refresh_entropy_probs = update; 2675 2676 return 0; 2677 } 2678 2679 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s) 2680 { 2681 FILE *yuv_file = fopen(name, "ab"); 2682 unsigned char *src = s->y_buffer; 2683 int h = s->y_height; 2684 2685 do 2686 { 2687 fwrite(src, s->y_width, 1, yuv_file); 2688 src += s->y_stride; 2689 } 2690 while (--h); 2691 2692 src = s->u_buffer; 2693 h = s->uv_height; 2694 2695 do 2696 { 2697 fwrite(src, s->uv_width, 1, yuv_file); 2698 src += s->uv_stride; 2699 } 2700 while (--h); 2701 2702 src = s->v_buffer; 2703 h = s->uv_height; 2704 2705 do 2706 { 2707 fwrite(src, s->uv_width, 1, yuv_file); 2708 src += s->uv_stride; 2709 } 2710 while (--h); 2711 2712 fclose(yuv_file); 2713 } 2714 2715 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) 2716 { 2717 VP8_COMMON *cm = &cpi->common; 2718 2719 // are we resizing the image 2720 if (cm->horiz_scale != 0 || cm->vert_scale != 0) 2721 { 2722 #if CONFIG_SPATIAL_RESAMPLING 2723 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 2724 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 2725 int tmp_height; 2726 2727 if (cm->vert_scale == 3) 2728 tmp_height = 9; 2729 else 2730 tmp_height = 11; 2731 2732 Scale2Ratio(cm->horiz_scale, &hr, &hs); 2733 Scale2Ratio(cm->vert_scale, &vr, &vs); 2734 2735 vp8_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer, 2736 tmp_height, hs, hr, vs, vr, 0); 2737 2738 cpi->Source = &cpi->scaled_source; 2739 #endif 2740 } 2741 // we may need to copy to a buffer so we can extend the image... 2742 else if (cm->Width != cm->yv12_fb[cm->lst_fb_idx].y_width || 2743 cm->Height != cm->yv12_fb[cm->lst_fb_idx].y_height) 2744 { 2745 //vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source); 2746 #if HAVE_ARMV7 2747 vp8_yv12_copy_src_frame_func_neon(sd, &cpi->scaled_source); 2748 #else 2749 vp8_yv12_copy_frame_ptr(sd, &cpi->scaled_source); 2750 #endif 2751 2752 cpi->Source = &cpi->scaled_source; 2753 } 2754 2755 vp8_extend_to_multiple_of16(cpi->Source, cm->Width, cm->Height); 2756 2757 } 2758 static void resize_key_frame(VP8_COMP *cpi) 2759 { 2760 #if CONFIG_SPATIAL_RESAMPLING 2761 VP8_COMMON *cm = &cpi->common; 2762 2763 // Do we need to apply resampling for one pass cbr. 2764 // In one pass this is more limited than in two pass cbr 2765 // The test and any change is only made one per key frame sequence 2766 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) 2767 { 2768 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 2769 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 2770 int new_width, new_height; 2771 2772 // If we are below the resample DOWN watermark then scale down a notch. 2773 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) 2774 { 2775 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO; 2776 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO; 2777 } 2778 // Should we now start scaling back up 2779 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)) 2780 { 2781 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL; 2782 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL; 2783 } 2784 2785 // Get the new hieght and width 2786 Scale2Ratio(cm->horiz_scale, &hr, &hs); 2787 Scale2Ratio(cm->vert_scale, &vr, &vs); 2788 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs; 2789 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs; 2790 2791 // If the image size has changed we need to reallocate the buffers 2792 // and resample the source image 2793 if ((cm->Width != new_width) || (cm->Height != new_height)) 2794 { 2795 cm->Width = new_width; 2796 cm->Height = new_height; 2797 vp8_alloc_compressor_data(cpi); 2798 scale_and_extend_source(cpi->un_scaled_source, cpi); 2799 } 2800 } 2801 2802 #endif 2803 } 2804 // return of 0 means drop frame 2805 static int pick_frame_size(VP8_COMP *cpi) 2806 { 2807 VP8_COMMON *cm = &cpi->common; 2808 2809 // First Frame is a special case 2810 if (cm->current_video_frame == 0) 2811 { 2812 #if !(CONFIG_REALTIME_ONLY) 2813 2814 if (cpi->pass == 2) 2815 vp8_calc_auto_iframe_target_size(cpi); 2816 2817 // 1 Pass there is no information on which to base size so use bandwidth per second * fixed fraction 2818 else 2819 #endif 2820 cpi->this_frame_target = cpi->oxcf.target_bandwidth / 2; 2821 2822 // in error resilient mode the first frame is bigger since it likely contains 2823 // all the static background 2824 if (cpi->oxcf.error_resilient_mode == 1 || (cpi->compressor_speed == 2)) 2825 { 2826 cpi->this_frame_target *= 3; // 5; 2827 } 2828 2829 // Key frame from VFW/auto-keyframe/first frame 2830 cm->frame_type = KEY_FRAME; 2831 2832 } 2833 // Special case for forced key frames 2834 // The frame sizing here is still far from ideal for 2 pass. 2835 else if (cm->frame_flags & FRAMEFLAGS_KEY) 2836 { 2837 cm->frame_type = KEY_FRAME; 2838 resize_key_frame(cpi); 2839 vp8_calc_iframe_target_size(cpi); 2840 } 2841 else if (cm->frame_type == KEY_FRAME) 2842 { 2843 vp8_calc_auto_iframe_target_size(cpi); 2844 } 2845 else 2846 { 2847 // INTER frame: compute target frame size 2848 cm->frame_type = INTER_FRAME; 2849 vp8_calc_pframe_target_size(cpi); 2850 2851 // Check if we're dropping the frame: 2852 if (cpi->drop_frame) 2853 { 2854 cpi->drop_frame = FALSE; 2855 cpi->drop_count++; 2856 return 0; 2857 } 2858 } 2859 2860 // Note target_size in bits * 256 per MB 2861 cpi->target_bits_per_mb = (cpi->this_frame_target * 256) / cpi->common.MBs; 2862 2863 return 1; 2864 } 2865 static void set_quantizer(VP8_COMP *cpi, int Q) 2866 { 2867 VP8_COMMON *cm = &cpi->common; 2868 MACROBLOCKD *mbd = &cpi->mb.e_mbd; 2869 2870 cm->base_qindex = Q; 2871 2872 cm->y1dc_delta_q = 0; 2873 cm->y2dc_delta_q = 0; 2874 cm->y2ac_delta_q = 0; 2875 cm->uvdc_delta_q = 0; 2876 cm->uvac_delta_q = 0; 2877 2878 // Set Segment specific quatizers 2879 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0]; 2880 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1]; 2881 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2]; 2882 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3]; 2883 } 2884 2885 static void update_alt_ref_frame_and_stats(VP8_COMP *cpi) 2886 { 2887 VP8_COMMON *cm = &cpi->common; 2888 2889 // Update the golden frame buffer 2890 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]); 2891 2892 // Select an interval before next GF or altref 2893 if (!cpi->auto_gold) 2894 cpi->frames_till_gf_update_due = cpi->goldfreq; 2895 2896 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due) 2897 { 2898 cpi->current_gf_interval = cpi->frames_till_gf_update_due; 2899 2900 // Set the bits per frame that we should try and recover in subsequent inter frames 2901 // to account for the extra GF spend... note that his does not apply for GF updates 2902 // that occur coincident with a key frame as the extra cost of key frames is dealt 2903 // with elsewhere. 2904 2905 cpi->gf_overspend_bits += cpi->projected_frame_size; 2906 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due; 2907 } 2908 2909 // Update data structure that monitors level of reference to last GF 2910 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols)); 2911 cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 2912 // this frame refreshes means next frames don't unless specified by user 2913 2914 cpi->common.frames_since_golden = 0; 2915 2916 // Clear the alternate reference update pending flag. 2917 cpi->source_alt_ref_pending = FALSE; 2918 2919 // Set the alternate refernce frame active flag 2920 cpi->source_alt_ref_active = TRUE; 2921 2922 2923 } 2924 static void update_golden_frame_and_stats(VP8_COMP *cpi) 2925 { 2926 VP8_COMMON *cm = &cpi->common; 2927 2928 // Update the Golden frame reconstruction buffer if signalled and the GF usage counts. 2929 if (cm->refresh_golden_frame) 2930 { 2931 // Update the golden frame buffer 2932 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]); 2933 2934 // Select an interval before next GF 2935 if (!cpi->auto_gold) 2936 cpi->frames_till_gf_update_due = cpi->goldfreq; 2937 2938 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0)) 2939 { 2940 cpi->current_gf_interval = cpi->frames_till_gf_update_due; 2941 2942 // Set the bits per frame that we should try and recover in subsequent inter frames 2943 // to account for the extra GF spend... note that his does not apply for GF updates 2944 // that occur coincident with a key frame as the extra cost of key frames is dealt 2945 // with elsewhere. 2946 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active) 2947 { 2948 // Calcluate GF bits to be recovered 2949 // Projected size - av frame bits available for inter frames for clip as a whole 2950 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target); 2951 } 2952 2953 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due; 2954 2955 } 2956 2957 // Update data structure that monitors level of reference to last GF 2958 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols)); 2959 cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 2960 2961 // this frame refreshes means next frames don't unless specified by user 2962 cm->refresh_golden_frame = 0; 2963 cpi->common.frames_since_golden = 0; 2964 2965 //if ( cm->frame_type == KEY_FRAME ) 2966 //{ 2967 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1; 2968 cpi->recent_ref_frame_usage[LAST_FRAME] = 1; 2969 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1; 2970 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1; 2971 //} 2972 //else 2973 //{ 2974 // // Carry a potrtion of count over to begining of next gf sequence 2975 // cpi->recent_ref_frame_usage[INTRA_FRAME] >>= 5; 2976 // cpi->recent_ref_frame_usage[LAST_FRAME] >>= 5; 2977 // cpi->recent_ref_frame_usage[GOLDEN_FRAME] >>= 5; 2978 // cpi->recent_ref_frame_usage[ALTREF_FRAME] >>= 5; 2979 //} 2980 2981 // ******** Fixed Q test code only ************ 2982 // If we are going to use the ALT reference for the next group of frames set a flag to say so. 2983 if (cpi->oxcf.fixed_q >= 0 && 2984 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame) 2985 { 2986 cpi->source_alt_ref_pending = TRUE; 2987 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 2988 } 2989 2990 if (!cpi->source_alt_ref_pending) 2991 cpi->source_alt_ref_active = FALSE; 2992 2993 // Decrement count down till next gf 2994 if (cpi->frames_till_gf_update_due > 0) 2995 cpi->frames_till_gf_update_due--; 2996 2997 } 2998 else if (!cpi->common.refresh_alt_ref_frame) 2999 { 3000 // Decrement count down till next gf 3001 if (cpi->frames_till_gf_update_due > 0) 3002 cpi->frames_till_gf_update_due--; 3003 3004 if (cpi->common.frames_till_alt_ref_frame) 3005 cpi->common.frames_till_alt_ref_frame --; 3006 3007 cpi->common.frames_since_golden ++; 3008 3009 if (cpi->common.frames_since_golden > 1) 3010 { 3011 cpi->recent_ref_frame_usage[INTRA_FRAME] += cpi->count_mb_ref_frame_usage[INTRA_FRAME]; 3012 cpi->recent_ref_frame_usage[LAST_FRAME] += cpi->count_mb_ref_frame_usage[LAST_FRAME]; 3013 cpi->recent_ref_frame_usage[GOLDEN_FRAME] += cpi->count_mb_ref_frame_usage[GOLDEN_FRAME]; 3014 cpi->recent_ref_frame_usage[ALTREF_FRAME] += cpi->count_mb_ref_frame_usage[ALTREF_FRAME]; 3015 } 3016 } 3017 } 3018 3019 // This function updates the reference frame probability estimates that 3020 // will be used during mode selection 3021 static void update_rd_ref_frame_probs(VP8_COMP *cpi) 3022 { 3023 VP8_COMMON *cm = &cpi->common; 3024 3025 #if 0 3026 const int *const rfct = cpi->recent_ref_frame_usage; 3027 const int rf_intra = rfct[INTRA_FRAME]; 3028 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; 3029 3030 if (cm->frame_type == KEY_FRAME) 3031 { 3032 cpi->prob_intra_coded = 255; 3033 cpi->prob_last_coded = 128; 3034 cpi->prob_gf_coded = 128; 3035 } 3036 else if (!(rf_intra + rf_inter)) 3037 { 3038 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank. 3039 cpi->prob_intra_coded = 63; 3040 cpi->prob_last_coded = 128; 3041 cpi->prob_gf_coded = 128; 3042 } 3043 else 3044 { 3045 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter); 3046 3047 if (cpi->prob_intra_coded < 1) 3048 cpi->prob_intra_coded = 1; 3049 3050 if ((cm->frames_since_golden > 0) || cpi->source_alt_ref_active) 3051 { 3052 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; 3053 3054 if (cpi->prob_last_coded < 1) 3055 cpi->prob_last_coded = 1; 3056 3057 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) 3058 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; 3059 3060 if (cpi->prob_gf_coded < 1) 3061 cpi->prob_gf_coded = 1; 3062 } 3063 } 3064 3065 #else 3066 const int *const rfct = cpi->count_mb_ref_frame_usage; 3067 const int rf_intra = rfct[INTRA_FRAME]; 3068 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; 3069 3070 if (cm->frame_type == KEY_FRAME) 3071 { 3072 cpi->prob_intra_coded = 255; 3073 cpi->prob_last_coded = 128; 3074 cpi->prob_gf_coded = 128; 3075 } 3076 else if (!(rf_intra + rf_inter)) 3077 { 3078 // This is a trap in case this function is called with cpi->recent_ref_frame_usage[] blank. 3079 cpi->prob_intra_coded = 63; 3080 cpi->prob_last_coded = 128; 3081 cpi->prob_gf_coded = 128; 3082 } 3083 else 3084 { 3085 cpi->prob_intra_coded = (rf_intra * 255) / (rf_intra + rf_inter); 3086 3087 if (cpi->prob_intra_coded < 1) 3088 cpi->prob_intra_coded = 1; 3089 3090 cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; 3091 3092 if (cpi->prob_last_coded < 1) 3093 cpi->prob_last_coded = 1; 3094 3095 cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) 3096 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; 3097 3098 if (cpi->prob_gf_coded < 1) 3099 cpi->prob_gf_coded = 1; 3100 } 3101 3102 // update reference frame costs since we can do better than what we got last frame. 3103 3104 if (cpi->common.refresh_alt_ref_frame) 3105 { 3106 cpi->prob_intra_coded += 40; 3107 cpi->prob_last_coded = 200; 3108 cpi->prob_gf_coded = 1; 3109 } 3110 else if (cpi->common.frames_since_golden == 0) 3111 { 3112 cpi->prob_last_coded = 214; 3113 cpi->prob_gf_coded = 1; 3114 } 3115 else if (cpi->common.frames_since_golden == 1) 3116 { 3117 cpi->prob_last_coded = 192; 3118 cpi->prob_gf_coded = 220; 3119 } 3120 else if (cpi->source_alt_ref_active) 3121 { 3122 //int dist = cpi->common.frames_till_alt_ref_frame + cpi->common.frames_since_golden; 3123 cpi->prob_gf_coded -= 20; 3124 3125 if (cpi->prob_gf_coded < 10) 3126 cpi->prob_gf_coded = 10; 3127 } 3128 3129 #endif 3130 } 3131 3132 3133 // 1 = key, 0 = inter 3134 static int decide_key_frame(VP8_COMP *cpi) 3135 { 3136 VP8_COMMON *cm = &cpi->common; 3137 3138 int code_key_frame = FALSE; 3139 3140 cpi->kf_boost = 0; 3141 3142 if (cpi->Speed > 11) 3143 return FALSE; 3144 3145 // Clear down mmx registers 3146 vp8_clear_system_state(); //__asm emms; 3147 3148 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0)) 3149 { 3150 double change = 1.0 * abs((int)(cpi->intra_error - cpi->last_intra_error)) / (1 + cpi->last_intra_error); 3151 double change2 = 1.0 * abs((int)(cpi->prediction_error - cpi->last_prediction_error)) / (1 + cpi->last_prediction_error); 3152 double minerror = cm->MBs * 256; 3153 3154 #if 0 3155 3156 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15 3157 && cpi->prediction_error > minerror 3158 && (change > .25 || change2 > .25)) 3159 { 3160 FILE *f = fopen("intra_inter.stt", "a"); 3161 3162 if (cpi->prediction_error <= 0) 3163 cpi->prediction_error = 1; 3164 3165 fprintf(f, "%d %d %d %d %14.4f\n", 3166 cm->current_video_frame, 3167 (int) cpi->prediction_error, 3168 (int) cpi->intra_error, 3169 (int)((10 * cpi->intra_error) / cpi->prediction_error), 3170 change); 3171 3172 fclose(f); 3173 } 3174 3175 #endif 3176 3177 cpi->last_intra_error = cpi->intra_error; 3178 cpi->last_prediction_error = cpi->prediction_error; 3179 3180 if (10 * cpi->intra_error / (1 + cpi->prediction_error) < 15 3181 && cpi->prediction_error > minerror 3182 && (change > .25 || change2 > .25)) 3183 { 3184 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/ 3185 return TRUE; 3186 } 3187 3188 return FALSE; 3189 3190 } 3191 3192 // If the following are true we might as well code a key frame 3193 if (((cpi->this_frame_percent_intra == 100) && 3194 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) || 3195 ((cpi->this_frame_percent_intra > 95) && 3196 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5)))) 3197 { 3198 code_key_frame = TRUE; 3199 } 3200 // in addition if the following are true and this is not a golden frame then code a key frame 3201 // Note that on golden frames there often seems to be a pop in intra useage anyway hence this 3202 // restriction is designed to prevent spurious key frames. The Intra pop needs to be investigated. 3203 else if (((cpi->this_frame_percent_intra > 60) && 3204 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) || 3205 ((cpi->this_frame_percent_intra > 75) && 3206 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) || 3207 ((cpi->this_frame_percent_intra > 90) && 3208 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10)))) 3209 { 3210 if (!cm->refresh_golden_frame) 3211 code_key_frame = TRUE; 3212 } 3213 3214 return code_key_frame; 3215 3216 } 3217 3218 #if !(CONFIG_REALTIME_ONLY) 3219 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) 3220 { 3221 (void) size; 3222 (void) dest; 3223 (void) frame_flags; 3224 set_quantizer(cpi, 26); 3225 3226 scale_and_extend_source(cpi->un_scaled_source, cpi); 3227 vp8_first_pass(cpi); 3228 } 3229 #endif 3230 3231 #if 0 3232 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) 3233 { 3234 3235 // write the frame 3236 FILE *yframe; 3237 int i; 3238 char filename[255]; 3239 3240 sprintf(filename, "cx\\y%04d.raw", this_frame); 3241 yframe = fopen(filename, "wb"); 3242 3243 for (i = 0; i < frame->y_height; i++) 3244 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe); 3245 3246 fclose(yframe); 3247 sprintf(filename, "cx\\u%04d.raw", this_frame); 3248 yframe = fopen(filename, "wb"); 3249 3250 for (i = 0; i < frame->uv_height; i++) 3251 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe); 3252 3253 fclose(yframe); 3254 sprintf(filename, "cx\\v%04d.raw", this_frame); 3255 yframe = fopen(filename, "wb"); 3256 3257 for (i = 0; i < frame->uv_height; i++) 3258 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe); 3259 3260 fclose(yframe); 3261 } 3262 #endif 3263 // return of 0 means drop frame 3264 #define USE_FILTER_LUT 1 3265 #if VP8_TEMPORAL_ALT_REF 3266 3267 #if USE_FILTER_LUT 3268 static int modifier_lut[7][19] = 3269 { 3270 // Strength=0 3271 {16, 13, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3272 // Strength=1 3273 {16, 15, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3274 // Strength=2 3275 {16, 15, 13, 9, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3276 // Strength=3 3277 {16, 16, 15, 13, 10, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3278 // Strength=4 3279 {16, 16, 15, 14, 13, 11, 9, 7, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 3280 // Strength=5 3281 {16, 16, 16, 15, 15, 14, 13, 11, 10, 8, 7, 5, 3, 0, 0, 0, 0, 0, 0}, 3282 // Strength=6 3283 {16, 16, 16, 16, 15, 15, 14, 14, 13, 12, 11, 10, 9, 8, 7, 5, 4, 2, 1} 3284 }; 3285 #endif 3286 static void vp8cx_temp_blur1_c 3287 ( 3288 VP8_COMP *cpi, 3289 unsigned char **frames, 3290 int frame_count, 3291 unsigned char *src, 3292 unsigned char *dst, 3293 int width, 3294 int stride, 3295 int height, 3296 int strength, 3297 int *fixed_divide, 3298 unsigned char *motion_map_ptr, 3299 unsigned char block_size 3300 ) 3301 { 3302 int byte = 0; // Buffer offset for current pixel being filtered 3303 int frame = 0; 3304 int modifier = 0; 3305 int i, j, k; 3306 int block_ofset; 3307 int cols; 3308 unsigned char Shift = (block_size == 16) ? 4 : 3; 3309 #if USE_FILTER_LUT 3310 int *lut = modifier_lut[strength]; 3311 #endif 3312 3313 cols = cpi->common.mb_cols; 3314 3315 for (i = 0; i < height; i++) 3316 { 3317 block_ofset = (i >> Shift) * cols; 3318 3319 for (j = 0; j < cols; j ++) 3320 { 3321 if (motion_map_ptr[block_ofset] > 2) 3322 { 3323 vpx_memcpy(&dst[byte], &src[byte], block_size); 3324 byte += block_size; 3325 } 3326 else 3327 { 3328 for (k = 0; k < block_size; k++) 3329 { 3330 int accumulator = 0; 3331 int count = 0; 3332 int src_byte = src[byte]; 3333 3334 for (frame = 0; frame < frame_count; frame++) 3335 { 3336 // get current frame pixel value 3337 int pixel_value = frames[frame][byte]; 3338 #if USE_FILTER_LUT 3339 // LUT implementation -- 3340 // improves precision of filter 3341 modifier = abs(src_byte-pixel_value); 3342 modifier = modifier>18 ? 0 : lut[modifier]; 3343 #else 3344 modifier = src_byte; 3345 modifier -= pixel_value; 3346 modifier *= modifier; 3347 modifier >>= strength; 3348 modifier *= 3; 3349 3350 if (modifier > 16) 3351 modifier = 16; 3352 3353 modifier = 16 - modifier; 3354 #endif 3355 accumulator += modifier * pixel_value; 3356 3357 count += modifier; 3358 } 3359 3360 accumulator += (count >> 1); 3361 accumulator *= fixed_divide[count]; 3362 accumulator >>= 16; 3363 3364 dst[byte] = accumulator; 3365 3366 // move to next pixel 3367 byte++; 3368 } 3369 } 3370 3371 block_ofset++; 3372 } 3373 3374 // Step byte on over the UMV border to the start of the next line 3375 byte += stride - width; 3376 } 3377 } 3378 3379 static void vp8cx_temp_filter_c 3380 ( 3381 VP8_COMP *cpi 3382 ) 3383 { 3384 YV12_BUFFER_CONFIG *temp_source_buffer; 3385 int *fixed_divide = cpi->fixed_divide; 3386 3387 int frame = 0; 3388 int max_frames = 11; 3389 3390 int num_frames_backward = 0; 3391 int num_frames_forward = 0; 3392 int frames_to_blur_backward = 0; 3393 int frames_to_blur_forward = 0; 3394 int frames_to_blur = 0; 3395 int start_frame = 0; 3396 3397 int strength = cpi->oxcf.arnr_strength; 3398 3399 int blur_type = cpi->oxcf.arnr_type; 3400 3401 int new_max_frames = cpi->oxcf.arnr_max_frames; 3402 3403 if (new_max_frames > 0) 3404 max_frames = new_max_frames; 3405 3406 num_frames_backward = cpi->last_alt_ref_sei - cpi->source_encode_index; 3407 3408 if (num_frames_backward < 0) 3409 num_frames_backward += cpi->oxcf.lag_in_frames; 3410 3411 num_frames_forward = cpi->oxcf.lag_in_frames - (num_frames_backward + 1); 3412 3413 switch (blur_type) 3414 { 3415 case 1: 3416 ///////////////////////////////////////// 3417 // Backward Blur 3418 3419 frames_to_blur_backward = num_frames_backward; 3420 3421 if (frames_to_blur_backward >= max_frames) 3422 frames_to_blur_backward = max_frames - 1; 3423 3424 frames_to_blur = frames_to_blur_backward + 1; 3425 break; 3426 3427 case 2: 3428 ///////////////////////////////////////// 3429 // Forward Blur 3430 3431 frames_to_blur_forward = num_frames_forward; 3432 3433 if (frames_to_blur_forward >= max_frames) 3434 frames_to_blur_forward = max_frames - 1; 3435 3436 frames_to_blur = frames_to_blur_forward + 1; 3437 break; 3438 3439 case 3: 3440 ///////////////////////////////////////// 3441 // Center Blur 3442 frames_to_blur_forward = num_frames_forward; 3443 frames_to_blur_backward = num_frames_backward; 3444 3445 if (frames_to_blur_forward > frames_to_blur_backward) 3446 frames_to_blur_forward = frames_to_blur_backward; 3447 3448 if (frames_to_blur_backward > frames_to_blur_forward) 3449 frames_to_blur_backward = frames_to_blur_forward; 3450 3451 if (frames_to_blur_forward > (max_frames / 2)) 3452 frames_to_blur_forward = (max_frames / 2); 3453 3454 if (frames_to_blur_backward > (max_frames / 2)) 3455 frames_to_blur_backward = (max_frames / 2); 3456 3457 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1; 3458 break; 3459 3460 default: 3461 ///////////////////////////////////////// 3462 // At most 4 frames forward Blur 3463 frames_to_blur_forward = 4; 3464 frames_to_blur_backward = num_frames_backward; 3465 3466 if (max_frames > 5) 3467 { 3468 if ((frames_to_blur_backward + frames_to_blur_forward) >= max_frames) 3469 { 3470 frames_to_blur_backward 3471 = max_frames - frames_to_blur_forward - 1; 3472 } 3473 } 3474 else 3475 { 3476 frames_to_blur_forward = max_frames - 1; 3477 frames_to_blur_backward = 0; 3478 } 3479 3480 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1; 3481 break; 3482 } 3483 3484 start_frame = (cpi->last_alt_ref_sei + frames_to_blur_forward) % cpi->oxcf.lag_in_frames; 3485 3486 #ifdef DEBUGFWG 3487 // DEBUG FWG 3488 printf("max:%d FBCK:%d FFWD:%d ftb:%d ftbbck:%d ftbfwd:%d sei:%d lasei:%d start:%d" 3489 , max_frames 3490 , num_frames_backward 3491 , num_frames_forward 3492 , frames_to_blur 3493 , frames_to_blur_backward 3494 , frames_to_blur_forward 3495 , cpi->source_encode_index 3496 , cpi->last_alt_ref_sei 3497 , start_frame); 3498 #endif 3499 3500 for (frame = 0; frame < frames_to_blur; frame++) 3501 { 3502 int which_buffer = start_frame - frame; 3503 3504 if (which_buffer < 0) 3505 which_buffer += cpi->oxcf.lag_in_frames; 3506 3507 cpi->frames[frame] = cpi->src_buffer[which_buffer].source_buffer.y_buffer; 3508 } 3509 3510 temp_source_buffer = &cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer; 3511 3512 // Blur Y 3513 vp8cx_temp_blur1_c( 3514 cpi, 3515 cpi->frames, 3516 frames_to_blur, 3517 temp_source_buffer->y_buffer, // cpi->Source->y_buffer, 3518 cpi->alt_ref_buffer.source_buffer.y_buffer, // cpi->Source->y_buffer, 3519 temp_source_buffer->y_width, 3520 temp_source_buffer->y_stride, 3521 temp_source_buffer->y_height, 3522 //temp_source_buffer->y_height * temp_source_buffer->y_stride, 3523 strength, 3524 fixed_divide, 3525 cpi->fp_motion_map, 16); 3526 3527 for (frame = 0; frame < frames_to_blur; frame++) 3528 { 3529 int which_buffer = start_frame - frame; 3530 3531 if (which_buffer < 0) 3532 which_buffer += cpi->oxcf.lag_in_frames; 3533 3534 cpi->frames[frame] = cpi->src_buffer[which_buffer].source_buffer.u_buffer; 3535 } 3536 3537 // Blur U 3538 vp8cx_temp_blur1_c( 3539 cpi, 3540 cpi->frames, 3541 frames_to_blur, 3542 temp_source_buffer->u_buffer, 3543 cpi->alt_ref_buffer.source_buffer.u_buffer, // cpi->Source->u_buffer, 3544 temp_source_buffer->uv_width, 3545 temp_source_buffer->uv_stride, 3546 temp_source_buffer->uv_height, 3547 //temp_source_buffer->uv_height * temp_source_buffer->uv_stride, 3548 strength, 3549 fixed_divide, 3550 cpi->fp_motion_map, 8); 3551 3552 for (frame = 0; frame < frames_to_blur; frame++) 3553 { 3554 int which_buffer = start_frame - frame; 3555 3556 if (which_buffer < 0) 3557 which_buffer += cpi->oxcf.lag_in_frames; 3558 3559 cpi->frames[frame] = cpi->src_buffer[which_buffer].source_buffer.v_buffer; 3560 } 3561 3562 // Blur V 3563 vp8cx_temp_blur1_c( 3564 cpi, 3565 cpi->frames, 3566 frames_to_blur, 3567 temp_source_buffer->v_buffer, 3568 cpi->alt_ref_buffer.source_buffer.v_buffer, // cpi->Source->v_buffer, 3569 temp_source_buffer->uv_width, 3570 temp_source_buffer->uv_stride, 3571 //temp_source_buffer->uv_height * temp_source_buffer->uv_stride, 3572 temp_source_buffer->uv_height, 3573 strength, 3574 fixed_divide, 3575 cpi->fp_motion_map, 8); 3576 } 3577 #endif 3578 3579 3580 static void encode_frame_to_data_rate(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) 3581 { 3582 int Q; 3583 int frame_over_shoot_limit; 3584 int frame_under_shoot_limit; 3585 3586 int Loop = FALSE; 3587 int loop_count; 3588 int this_q; 3589 int last_zbin_oq; 3590 3591 int q_low; 3592 int q_high; 3593 int zbin_oq_high; 3594 int zbin_oq_low = 0; 3595 int top_index; 3596 int bottom_index; 3597 VP8_COMMON *cm = &cpi->common; 3598 int active_worst_qchanged = FALSE; 3599 3600 int overshoot_seen = FALSE; 3601 int undershoot_seen = FALSE; 3602 int drop_mark = cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100; 3603 int drop_mark75 = drop_mark * 2 / 3; 3604 int drop_mark50 = drop_mark / 4; 3605 int drop_mark25 = drop_mark / 8; 3606 3607 // Clear down mmx registers to allow floating point in what follows 3608 vp8_clear_system_state(); 3609 3610 // Test code for segmentation of gf/arf (0,0) 3611 //segmentation_test_function((VP8_PTR) cpi); 3612 3613 // For an alt ref frame in 2 pass we skip the call to the second pass function that sets the target bandwidth 3614 #if !(CONFIG_REALTIME_ONLY) 3615 3616 if (cpi->pass == 2) 3617 { 3618 if (cpi->common.refresh_alt_ref_frame) 3619 { 3620 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for the alt ref frame 3621 cpi->target_bandwidth = cpi->gf_bits * cpi->output_frame_rate; // per second target bitrate 3622 } 3623 } 3624 else 3625 #endif 3626 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_frame_rate); 3627 3628 // Default turn off buffer to buffer copying 3629 cm->copy_buffer_to_gf = 0; 3630 cm->copy_buffer_to_arf = 0; 3631 3632 // Clear zbin over-quant value and mode boost values. 3633 cpi->zbin_over_quant = 0; 3634 cpi->zbin_mode_boost = 0; 3635 3636 // Enable mode based tweaking of the zbin 3637 cpi->zbin_mode_boost_enabled = TRUE; 3638 3639 // Current default encoder behaviour for the altref sign bias 3640 if (cpi->source_alt_ref_active) 3641 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1; 3642 else 3643 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0; 3644 3645 // Check to see if a key frame is signalled 3646 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass. 3647 if ((cm->current_video_frame == 0) || 3648 (cm->frame_flags & FRAMEFLAGS_KEY) || 3649 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0))) 3650 { 3651 // Key frame from VFW/auto-keyframe/first frame 3652 cm->frame_type = KEY_FRAME; 3653 } 3654 3655 // Set default state for segment and mode based loop filter update flags 3656 cpi->mb.e_mbd.update_mb_segmentation_map = 0; 3657 cpi->mb.e_mbd.update_mb_segmentation_data = 0; 3658 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0; 3659 3660 // Set various flags etc to special state if it is a key frame 3661 if (cm->frame_type == KEY_FRAME) 3662 { 3663 int i; 3664 3665 // If segmentation is enabled force a map update for key frames 3666 if (cpi->mb.e_mbd.segmentation_enabled) 3667 { 3668 cpi->mb.e_mbd.update_mb_segmentation_map = 1; 3669 cpi->mb.e_mbd.update_mb_segmentation_data = 1; 3670 } 3671 3672 // If mode or reference frame based loop filter deltas are enabled then force an update for key frames. 3673 if (cpi->mb.e_mbd.mode_ref_lf_delta_enabled) 3674 { 3675 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1; 3676 } 3677 3678 // The alternate reference frame cannot be active for a key frame 3679 cpi->source_alt_ref_active = FALSE; 3680 3681 // Reset the RD threshold multipliers to default of * 1 (128) 3682 for (i = 0; i < MAX_MODES; i++) 3683 { 3684 cpi->rd_thresh_mult[i] = 128; 3685 } 3686 } 3687 3688 // Test code for segmentation 3689 //if ( (cm->frame_type == KEY_FRAME) || ((cm->current_video_frame % 2) == 0)) 3690 //if ( (cm->current_video_frame % 2) == 0 ) 3691 // enable_segmentation((VP8_PTR)cpi); 3692 //else 3693 // disable_segmentation((VP8_PTR)cpi); 3694 3695 #if 0 3696 // Experimental code for lagged compress and one pass 3697 // Initialise one_pass GF frames stats 3698 // Update stats used for GF selection 3699 //if ( cpi->pass == 0 ) 3700 { 3701 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS; 3702 3703 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0; 3704 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0; 3705 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0; 3706 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0; 3707 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0; 3708 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0; 3709 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0; 3710 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0; 3711 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0; 3712 } 3713 #endif 3714 3715 update_rd_ref_frame_probs(cpi); 3716 3717 if (cpi->drop_frames_allowed) 3718 { 3719 // The reset to decimation 0 is only done here for one pass. 3720 // Once it is set two pass leaves decimation on till the next kf. 3721 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0)) 3722 cpi->decimation_factor --; 3723 3724 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0) 3725 cpi->decimation_factor = 1; 3726 3727 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3)) 3728 { 3729 cpi->decimation_factor = 3; 3730 } 3731 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2)) 3732 { 3733 cpi->decimation_factor = 2; 3734 } 3735 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1)) 3736 { 3737 cpi->decimation_factor = 1; 3738 } 3739 3740 //vpx_log("Encoder: Decimation Factor: %d \n",cpi->decimation_factor); 3741 } 3742 3743 // The following decimates the frame rate according to a regular pattern (i.e. to 1/2 or 2/3 frame rate) 3744 // This can be used to help prevent buffer under-run in CBR mode. Alternatively it might be desirable in 3745 // some situations to drop frame rate but throw more bits at each frame. 3746 // 3747 // Note that dropping a key frame can be problematic if spatial resampling is also active 3748 if (cpi->decimation_factor > 0) 3749 { 3750 switch (cpi->decimation_factor) 3751 { 3752 case 1: 3753 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2; 3754 break; 3755 case 2: 3756 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4; 3757 break; 3758 case 3: 3759 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4; 3760 break; 3761 } 3762 3763 // Note that we should not throw out a key frame (especially when spatial resampling is enabled). 3764 if ((cm->frame_type == KEY_FRAME)) // && cpi->oxcf.allow_spatial_resampling ) 3765 { 3766 cpi->decimation_count = cpi->decimation_factor; 3767 } 3768 else if (cpi->decimation_count > 0) 3769 { 3770 cpi->decimation_count --; 3771 cpi->bits_off_target += cpi->av_per_frame_bandwidth; 3772 cm->current_video_frame++; 3773 cpi->frames_since_key++; 3774 3775 #if CONFIG_PSNR 3776 cpi->count ++; 3777 #endif 3778 3779 cpi->buffer_level = cpi->bits_off_target; 3780 3781 return; 3782 } 3783 else 3784 cpi->decimation_count = cpi->decimation_factor; 3785 } 3786 3787 // Decide how big to make the frame 3788 if (!pick_frame_size(cpi)) 3789 { 3790 cm->current_video_frame++; 3791 cpi->frames_since_key++; 3792 return; 3793 } 3794 3795 // Reduce active_worst_allowed_q for CBR if our buffer is getting too full. 3796 // This has a knock on effect on active best quality as well. 3797 // For CBR if the buffer reaches its maximum level then we can no longer 3798 // save up bits for later frames so we might as well use them up 3799 // on the current frame. 3800 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && 3801 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode) 3802 { 3803 int Adjustment = cpi->active_worst_quality / 4; // Max adjustment is 1/4 3804 3805 if (Adjustment) 3806 { 3807 int buff_lvl_step; 3808 int tmp_lvl = cpi->buffer_level; 3809 3810 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size) 3811 { 3812 buff_lvl_step = (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level) / Adjustment; 3813 3814 if (buff_lvl_step) 3815 { 3816 Adjustment = (cpi->buffer_level - cpi->oxcf.optimal_buffer_level) / buff_lvl_step; 3817 cpi->active_worst_quality -= Adjustment; 3818 } 3819 } 3820 else 3821 { 3822 cpi->active_worst_quality -= Adjustment; 3823 } 3824 } 3825 } 3826 3827 // Set an active best quality and if necessary active worst quality 3828 if (cpi->pass == 2 || (cm->current_video_frame > 150)) 3829 { 3830 //if ( (cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ) 3831 int Q; 3832 int i; 3833 int bpm_target; 3834 3835 Q = cpi->active_worst_quality; 3836 3837 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame) 3838 { 3839 vp8_clear_system_state(); 3840 3841 if (cm->frame_type != KEY_FRAME) 3842 { 3843 // Where a gf overlays an existing arf then allow active max Q to drift to highest allowed value. 3844 //if ( cpi->common.refresh_golden_frame && cpi->source_alt_ref_active ) 3845 //cpi->active_worst_quality = cpi->worst_quality; 3846 3847 if (cpi->avg_frame_qindex < cpi->active_worst_quality) 3848 Q = cpi->avg_frame_qindex; 3849 3850 if (cpi->section_is_low_motion) 3851 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * ((Q * 3 / 2) + 128)) / 64; 3852 else if (cpi->section_is_fast_motion) 3853 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * (Q + 128)) / 64; 3854 else 3855 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * ((Q * 5 / 4) + 128)) / 64; 3856 } 3857 // KEY FRAMES 3858 else 3859 { 3860 if (cpi->section_is_low_motion) 3861 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * (Q + 240)) / 64; // Approx 2.5 to 4.5 where Q has the range 0-127 3862 else 3863 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * (Q + 160)) / 64; 3864 } 3865 3866 for (i = Q; i > 0; i--) 3867 { 3868 if (bpm_target <= vp8_bits_per_mb[cm->frame_type][i]) 3869 break; 3870 } 3871 3872 cpi->active_best_quality = i; 3873 3874 // this entire section could be replaced by a look up table 3875 #if 0 3876 { 3877 int Q, best_q[128]; 3878 3879 for (Q = 0; Q < 128; Q++) 3880 { 3881 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * (Q + 160)) / 64; // Approx 2.5 to 4.5 where Q has the range 0-127 3882 3883 for (i = Q; i > 0; i--) 3884 { 3885 if (bpm_target <= vp8_bits_per_mb[cm->frame_type][i]) 3886 break; 3887 } 3888 3889 best_q[Q] = i; 3890 } 3891 3892 Q += 0; 3893 } 3894 #endif 3895 3896 } 3897 else 3898 { 3899 vp8_clear_system_state(); 3900 3901 //bpm_target = (vp8_bits_per_mb[cm->frame_type][Q]*(Q+128))/64; // Approx 2 to 4 where Q has the range 0-127 3902 bpm_target = (vp8_bits_per_mb[cm->frame_type][Q] * (Q + 192)) / 128; // Approx * 1.5 to 2.5 where Q has range 0-127 3903 3904 for (i = Q; i > 0; i--) 3905 { 3906 if (bpm_target <= vp8_bits_per_mb[cm->frame_type][i]) 3907 break; 3908 } 3909 3910 cpi->active_best_quality = i; 3911 } 3912 3913 // If CBR and the buffer is as full then it is reasonable to allow higher quality on the frames 3914 // to prevent bits just going to waste. 3915 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 3916 { 3917 // Note that the use of >= here elliminates the risk of a devide by 0 error in the else if clause 3918 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size) 3919 cpi->active_best_quality = cpi->best_quality; 3920 3921 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level) 3922 { 3923 int Fraction = ((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) / (cpi->oxcf.maximum_buffer_size - cpi->oxcf.optimal_buffer_level); 3924 int min_qadjustment = ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128; 3925 3926 cpi->active_best_quality -= min_qadjustment; 3927 } 3928 3929 } 3930 } 3931 3932 // Clip the active best and worst quality values to limits 3933 if (cpi->active_worst_quality > cpi->worst_quality) 3934 cpi->active_worst_quality = cpi->worst_quality; 3935 3936 if (cpi->active_best_quality < cpi->best_quality) 3937 cpi->active_best_quality = cpi->best_quality; 3938 else if (cpi->active_best_quality > cpi->active_worst_quality) 3939 cpi->active_best_quality = cpi->active_worst_quality; 3940 3941 // Determine initial Q to try 3942 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 3943 last_zbin_oq = cpi->zbin_over_quant; 3944 3945 // Set highest allowed value for Zbin over quant 3946 if (cm->frame_type == KEY_FRAME) 3947 zbin_oq_high = 0; //ZBIN_OQ_MAX/16 3948 else if (cm->refresh_alt_ref_frame || (cm->refresh_golden_frame && !cpi->source_alt_ref_active)) 3949 zbin_oq_high = 16; 3950 else 3951 zbin_oq_high = ZBIN_OQ_MAX; 3952 3953 // Setup background Q adjustment for error resilliant mode 3954 if (cpi->cyclic_refresh_mode_enabled) 3955 cyclic_background_refresh(cpi, Q, 0); 3956 3957 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit); 3958 3959 // Limit Q range for the adaptive loop (Values not clipped to range 20-60 as in VP8). 3960 bottom_index = cpi->active_best_quality; 3961 top_index = cpi->active_worst_quality; 3962 3963 vp8_save_coding_context(cpi); 3964 3965 loop_count = 0; 3966 3967 q_low = cpi->best_quality; 3968 q_high = cpi->worst_quality; 3969 3970 3971 scale_and_extend_source(cpi->un_scaled_source, cpi); 3972 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC 3973 3974 if (cpi->oxcf.noise_sensitivity > 0) 3975 { 3976 unsigned char *src; 3977 int l = 0; 3978 3979 switch (cpi->oxcf.noise_sensitivity) 3980 { 3981 case 1: 3982 l = 20; 3983 break; 3984 case 2: 3985 l = 40; 3986 break; 3987 case 3: 3988 l = 60; 3989 break; 3990 case 4: 3991 l = 80; 3992 break; 3993 case 5: 3994 l = 100; 3995 break; 3996 case 6: 3997 l = 150; 3998 break; 3999 } 4000 4001 4002 if (cm->frame_type == KEY_FRAME) 4003 { 4004 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc)); 4005 cpi->ppi.frame = 0; 4006 } 4007 else 4008 { 4009 vp8_de_noise(cpi->Source, cpi->Source, l , 1, 0, RTCD(postproc)); 4010 4011 src = cpi->Source->y_buffer; 4012 4013 if (cpi->Source->y_stride < 0) 4014 { 4015 src += cpi->Source->y_stride * (cpi->Source->y_height - 1); 4016 } 4017 4018 //temp_filter(&cpi->ppi,src,src, 4019 // cm->last_frame.y_width * cm->last_frame.y_height, 4020 // cpi->oxcf.noise_sensitivity); 4021 } 4022 } 4023 4024 #endif 4025 4026 #ifdef OUTPUT_YUV_SRC 4027 vp8_write_yuv_frame(cpi->Source); 4028 #endif 4029 4030 do 4031 { 4032 vp8_clear_system_state(); //__asm emms; 4033 4034 /* 4035 if(cpi->is_src_frame_alt_ref) 4036 Q = 127; 4037 */ 4038 4039 set_quantizer(cpi, Q); 4040 this_q = Q; 4041 4042 // setup skip prob for costing in mode/mv decision 4043 if (cpi->common.mb_no_coeff_skip) 4044 { 4045 cpi->prob_skip_false = cpi->base_skip_false_prob[Q]; 4046 4047 if (cm->frame_type != KEY_FRAME) 4048 { 4049 if (cpi->common.refresh_alt_ref_frame) 4050 { 4051 if (cpi->last_skip_false_probs[2] != 0) 4052 cpi->prob_skip_false = cpi->last_skip_false_probs[2]; 4053 4054 /* 4055 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 ) 4056 cpi->prob_skip_false = cpi->last_skip_false_probs[2]; 4057 else if (cpi->last_skip_false_probs[2]!=0) 4058 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2; 4059 */ 4060 } 4061 else if (cpi->common.refresh_golden_frame) 4062 { 4063 if (cpi->last_skip_false_probs[1] != 0) 4064 cpi->prob_skip_false = cpi->last_skip_false_probs[1]; 4065 4066 /* 4067 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 ) 4068 cpi->prob_skip_false = cpi->last_skip_false_probs[1]; 4069 else if (cpi->last_skip_false_probs[1]!=0) 4070 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2; 4071 */ 4072 } 4073 else 4074 { 4075 if (cpi->last_skip_false_probs[0] != 0) 4076 cpi->prob_skip_false = cpi->last_skip_false_probs[0]; 4077 4078 /* 4079 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 ) 4080 cpi->prob_skip_false = cpi->last_skip_false_probs[0]; 4081 else if(cpi->last_skip_false_probs[0]!=0) 4082 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2; 4083 */ 4084 } 4085 4086 //as this is for cost estimate, let's make sure it does not go extreme eitehr way 4087 if (cpi->prob_skip_false < 5) 4088 cpi->prob_skip_false = 5; 4089 4090 if (cpi->prob_skip_false > 250) 4091 cpi->prob_skip_false = 250; 4092 4093 if (cpi->is_src_frame_alt_ref) 4094 cpi->prob_skip_false = 1; 4095 4096 4097 } 4098 4099 #if 0 4100 4101 if (cpi->pass != 1) 4102 { 4103 FILE *f = fopen("skip.stt", "a"); 4104 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false); 4105 fclose(f); 4106 } 4107 4108 #endif 4109 4110 } 4111 4112 if (cm->frame_type == KEY_FRAME) 4113 vp8_setup_key_frame(cpi); 4114 4115 // transform / motion compensation build reconstruction frame 4116 4117 vp8_encode_frame(cpi); 4118 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi); 4119 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0; 4120 4121 vp8_clear_system_state(); //__asm emms; 4122 4123 // Test to see if the stats generated for this frame indicate that we should have coded a key frame 4124 // (assuming that we didn't)! 4125 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME) 4126 { 4127 if (decide_key_frame(cpi)) 4128 { 4129 vp8_calc_auto_iframe_target_size(cpi); 4130 4131 // Reset all our sizing numbers and recode 4132 cm->frame_type = KEY_FRAME; 4133 4134 // Clear the Alt reference frame active flag when we have a key frame 4135 cpi->source_alt_ref_active = FALSE; 4136 4137 // If segmentation is enabled force a map update for key frames 4138 if (cpi->mb.e_mbd.segmentation_enabled) 4139 { 4140 cpi->mb.e_mbd.update_mb_segmentation_map = 1; 4141 cpi->mb.e_mbd.update_mb_segmentation_data = 1; 4142 } 4143 4144 // If mode or reference frame based loop filter deltas are enabled then force an update for key frames. 4145 if (cpi->mb.e_mbd.mode_ref_lf_delta_enabled) 4146 { 4147 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1; 4148 } 4149 4150 vp8_restore_coding_context(cpi); 4151 4152 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 4153 4154 q_low = cpi->best_quality; 4155 q_high = cpi->worst_quality; 4156 4157 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit); 4158 4159 // Limit Q range for the adaptive loop (Values not clipped to range 20-60 as in VP8). 4160 bottom_index = cpi->active_best_quality; 4161 top_index = cpi->active_worst_quality; 4162 4163 4164 loop_count++; 4165 Loop = TRUE; 4166 4167 resize_key_frame(cpi); 4168 continue; 4169 } 4170 } 4171 4172 vp8_clear_system_state(); 4173 4174 if (frame_over_shoot_limit == 0) 4175 frame_over_shoot_limit = 1; 4176 4177 // Are we are overshooting and up against the limit of active max Q. 4178 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) && 4179 (Q == cpi->active_worst_quality) && 4180 (cpi->active_worst_quality < cpi->worst_quality) && 4181 (cpi->projected_frame_size > frame_over_shoot_limit)) 4182 { 4183 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit; 4184 4185 // If so is there any scope for relaxing it 4186 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0)) 4187 { 4188 cpi->active_worst_quality++; 4189 top_index = cpi->active_worst_quality; 4190 over_size_percent = (int)(over_size_percent * 0.96); // Assume 1 qstep = about 4% on frame size. 4191 } 4192 4193 // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop. 4194 active_worst_qchanged = TRUE; 4195 } 4196 else 4197 active_worst_qchanged = FALSE; 4198 4199 #if !(CONFIG_REALTIME_ONLY) 4200 4201 // Is the projected frame size out of range and are we allowed to attempt to recode. 4202 if (((cpi->sf.recode_loop == 1) || 4203 ((cpi->sf.recode_loop == 2) && (cm->refresh_golden_frame || (cm->frame_type == KEY_FRAME)))) && 4204 (((cpi->projected_frame_size > frame_over_shoot_limit) && (Q < top_index)) || 4205 //((cpi->projected_frame_size > frame_over_shoot_limit ) && (Q == top_index) && (cpi->zbin_over_quant < ZBIN_OQ_MAX)) || 4206 ((cpi->projected_frame_size < frame_under_shoot_limit) && (Q > bottom_index))) 4207 ) 4208 { 4209 int last_q = Q; 4210 int Retries = 0; 4211 4212 // Frame size out of permitted range: 4213 // Update correction factor & compute new Q to try... 4214 if (cpi->projected_frame_size > frame_over_shoot_limit) 4215 { 4216 //if ( cpi->zbin_over_quant == 0 ) 4217 q_low = (Q < q_high) ? (Q + 1) : q_high; // Raise Qlow as to at least the current value 4218 4219 if (cpi->zbin_over_quant > 0) // If we are using over quant do the same for zbin_oq_low 4220 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high; 4221 4222 //if ( undershoot_seen || (Q == MAXQ) ) 4223 if (undershoot_seen) 4224 { 4225 // Update rate_correction_factor unless cpi->active_worst_quality has changed. 4226 if (!active_worst_qchanged) 4227 vp8_update_rate_correction_factors(cpi, 1); 4228 4229 Q = (q_high + q_low + 1) / 2; 4230 4231 // Adjust cpi->zbin_over_quant (only allowed when Q is max) 4232 if (Q < MAXQ) 4233 cpi->zbin_over_quant = 0; 4234 else 4235 { 4236 zbin_oq_low = (cpi->zbin_over_quant < zbin_oq_high) ? (cpi->zbin_over_quant + 1) : zbin_oq_high; 4237 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2; 4238 } 4239 } 4240 else 4241 { 4242 // Update rate_correction_factor unless cpi->active_worst_quality has changed. 4243 if (!active_worst_qchanged) 4244 vp8_update_rate_correction_factors(cpi, 0); 4245 4246 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 4247 4248 while (((Q < q_low) || (cpi->zbin_over_quant < zbin_oq_low)) && (Retries < 10)) 4249 { 4250 vp8_update_rate_correction_factors(cpi, 0); 4251 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 4252 Retries ++; 4253 } 4254 } 4255 4256 overshoot_seen = TRUE; 4257 } 4258 else 4259 { 4260 if (cpi->zbin_over_quant == 0) 4261 q_high = (Q > q_low) ? (Q - 1) : q_low; // Lower q_high if not using over quant 4262 else // else lower zbin_oq_high 4263 zbin_oq_high = (cpi->zbin_over_quant > zbin_oq_low) ? (cpi->zbin_over_quant - 1) : zbin_oq_low; 4264 4265 if (overshoot_seen) 4266 { 4267 // Update rate_correction_factor unless cpi->active_worst_quality has changed. 4268 if (!active_worst_qchanged) 4269 vp8_update_rate_correction_factors(cpi, 1); 4270 4271 Q = (q_high + q_low) / 2; 4272 4273 // Adjust cpi->zbin_over_quant (only allowed when Q is max) 4274 if (Q < MAXQ) 4275 cpi->zbin_over_quant = 0; 4276 else 4277 cpi->zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2; 4278 } 4279 else 4280 { 4281 // Update rate_correction_factor unless cpi->active_worst_quality has changed. 4282 if (!active_worst_qchanged) 4283 vp8_update_rate_correction_factors(cpi, 0); 4284 4285 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 4286 4287 while (((Q > q_high) || (cpi->zbin_over_quant > zbin_oq_high)) && (Retries < 10)) 4288 { 4289 vp8_update_rate_correction_factors(cpi, 0); 4290 Q = vp8_regulate_q(cpi, cpi->this_frame_target); 4291 Retries ++; 4292 } 4293 } 4294 4295 undershoot_seen = TRUE; 4296 } 4297 4298 // Clamp Q to upper and lower limits: 4299 if (Q > q_high) 4300 Q = q_high; 4301 else if (Q < q_low) 4302 Q = q_low; 4303 4304 // Clamp cpi->zbin_over_quant 4305 cpi->zbin_over_quant = (cpi->zbin_over_quant < zbin_oq_low) ? zbin_oq_low : (cpi->zbin_over_quant > zbin_oq_high) ? zbin_oq_high : cpi->zbin_over_quant; 4306 4307 //Loop = ((Q != last_q) || (last_zbin_oq != cpi->zbin_over_quant)) ? TRUE : FALSE; 4308 Loop = ((Q != last_q)) ? TRUE : FALSE; 4309 last_zbin_oq = cpi->zbin_over_quant; 4310 } 4311 else 4312 #endif 4313 Loop = FALSE; 4314 4315 if (cpi->is_src_frame_alt_ref) 4316 Loop = FALSE; 4317 4318 if (Loop == TRUE) 4319 { 4320 vp8_restore_coding_context(cpi); 4321 loop_count++; 4322 #if CONFIG_PSNR 4323 cpi->tot_recode_hits++; 4324 #endif 4325 } 4326 } 4327 while (Loop == TRUE); 4328 4329 #if 0 4330 // Experimental code for lagged and one pass 4331 // Update stats used for one pass GF selection 4332 { 4333 /* 4334 int frames_so_far; 4335 double frame_intra_error; 4336 double frame_coded_error; 4337 double frame_pcnt_inter; 4338 double frame_pcnt_motion; 4339 double frame_mvr; 4340 double frame_mvr_abs; 4341 double frame_mvc; 4342 double frame_mvc_abs; 4343 */ 4344 4345 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error; 4346 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error; 4347 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0; 4348 } 4349 #endif 4350 4351 // Update the GF useage maps. 4352 // This is done after completing the compression of a frame when all modes etc. are finalized but before loop filter 4353 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb); 4354 4355 if (cm->frame_type == KEY_FRAME) 4356 cm->refresh_last_frame = 1; 4357 4358 if (0) 4359 { 4360 FILE *f = fopen("gfactive.stt", "a"); 4361 fprintf(f, "%8d %8d %8d %8d %8d\n", cm->current_video_frame, (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols), cpi->this_iiratio, cpi->next_iiratio, cm->refresh_golden_frame); 4362 fclose(f); 4363 } 4364 4365 // For inter frames the current default behaviour is that when cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer 4366 // This is purely an encoder descision at present. 4367 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame) 4368 cm->copy_buffer_to_arf = 2; 4369 else 4370 cm->copy_buffer_to_arf = 0; 4371 4372 if (cm->refresh_last_frame) 4373 { 4374 vp8_swap_yv12_buffer(&cm->yv12_fb[cm->lst_fb_idx], &cm->yv12_fb[cm->new_fb_idx]); 4375 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx]; 4376 } 4377 else 4378 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx]; 4379 4380 4381 4382 //#pragma omp parallel sections 4383 { 4384 4385 //#pragma omp section 4386 { 4387 4388 struct vpx_usec_timer timer; 4389 4390 vpx_usec_timer_start(&timer); 4391 4392 if (cpi->sf.auto_filter == 0) 4393 vp8cx_pick_filter_level_fast(cpi->Source, cpi); 4394 else 4395 vp8cx_pick_filter_level(cpi->Source, cpi); 4396 4397 vpx_usec_timer_mark(&timer); 4398 4399 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); 4400 4401 if (cm->no_lpf) 4402 cm->filter_level = 0; 4403 4404 if (cm->filter_level > 0) 4405 { 4406 vp8cx_set_alt_lf_level(cpi, cm->filter_level); 4407 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, cm->filter_level); 4408 cm->last_frame_type = cm->frame_type; 4409 cm->last_filter_type = cm->filter_type; 4410 cm->last_sharpness_level = cm->sharpness_level; 4411 } 4412 4413 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show); 4414 4415 if (cpi->oxcf.error_resilient_mode == 1) 4416 { 4417 cm->refresh_entropy_probs = 0; 4418 } 4419 4420 } 4421 //#pragma omp section 4422 { 4423 // build the bitstream 4424 vp8_pack_bitstream(cpi, dest, size); 4425 } 4426 } 4427 4428 { 4429 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; 4430 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; 4431 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; 4432 YV12_BUFFER_CONFIG *alt_yv12 = &cm->yv12_fb[cm->alt_fb_idx]; 4433 // At this point the new frame has been encoded coded. 4434 // If any buffer copy / swaping is signalled it should be done here. 4435 if (cm->frame_type == KEY_FRAME) 4436 { 4437 vp8_yv12_copy_frame_ptr(cm->frame_to_show, gld_yv12); 4438 vp8_yv12_copy_frame_ptr(cm->frame_to_show, alt_yv12); 4439 } 4440 else // For non key frames 4441 { 4442 // Code to copy between reference buffers 4443 if (cm->copy_buffer_to_arf) 4444 { 4445 if (cm->copy_buffer_to_arf == 1) 4446 { 4447 if (cm->refresh_last_frame) 4448 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set. 4449 vp8_yv12_copy_frame_ptr(new_yv12, alt_yv12); 4450 else 4451 vp8_yv12_copy_frame_ptr(lst_yv12, alt_yv12); 4452 } 4453 else if (cm->copy_buffer_to_arf == 2) 4454 vp8_yv12_copy_frame_ptr(gld_yv12, alt_yv12); 4455 } 4456 4457 if (cm->copy_buffer_to_gf) 4458 { 4459 if (cm->copy_buffer_to_gf == 1) 4460 { 4461 if (cm->refresh_last_frame) 4462 // We copy new_frame here because last and new buffers will already have been swapped if cm->refresh_last_frame is set. 4463 vp8_yv12_copy_frame_ptr(new_yv12, gld_yv12); 4464 else 4465 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); 4466 } 4467 else if (cm->copy_buffer_to_gf == 2) 4468 vp8_yv12_copy_frame_ptr(alt_yv12, gld_yv12); 4469 } 4470 } 4471 } 4472 4473 // Update rate control heuristics 4474 cpi->total_byte_count += (*size); 4475 cpi->projected_frame_size = (*size) << 3; 4476 4477 if (!active_worst_qchanged) 4478 vp8_update_rate_correction_factors(cpi, 2); 4479 4480 cpi->last_q[cm->frame_type] = cm->base_qindex; 4481 4482 if (cm->frame_type == KEY_FRAME) 4483 { 4484 vp8_adjust_key_frame_context(cpi); 4485 } 4486 4487 // Keep a record of ambient average Q. 4488 if (cm->frame_type == KEY_FRAME) 4489 cpi->avg_frame_qindex = cm->base_qindex; 4490 else 4491 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2; 4492 4493 // Keep a record from which we can calculate the average Q excluding GF updates and key frames 4494 if ((cm->frame_type != KEY_FRAME) && !cm->refresh_golden_frame && !cm->refresh_alt_ref_frame) 4495 { 4496 cpi->ni_frames++; 4497 4498 // Calculate the average Q for normal inter frames (not key or GFU frames) 4499 // This is used as a basis for setting active worst quality. 4500 if (cpi->ni_frames > 150) 4501 { 4502 cpi->ni_tot_qi += Q; 4503 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames); 4504 } 4505 // Early in the clip ... average the current frame Q value with the default 4506 // entered by the user as a dampening measure 4507 else 4508 { 4509 cpi->ni_tot_qi += Q; 4510 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2; 4511 } 4512 4513 // If the average Q is higher than what was used in the last frame 4514 // (after going through the recode loop to keep the frame size within range) 4515 // then use the last frame value - 1. 4516 // The -1 is designed to stop Q and hence the data rate, from progressively 4517 // falling away during difficult sections, but at the same time reduce the number of 4518 // itterations around the recode loop. 4519 if (Q > cpi->ni_av_qi) 4520 cpi->ni_av_qi = Q - 1; 4521 4522 } 4523 4524 #if 0 4525 4526 // If the frame was massively oversize and we are below optimal buffer level drop next frame 4527 if ((cpi->drop_frames_allowed) && 4528 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && 4529 (cpi->buffer_level < cpi->oxcf.drop_frames_water_mark * cpi->oxcf.optimal_buffer_level / 100) && 4530 (cpi->projected_frame_size > (4 * cpi->this_frame_target))) 4531 { 4532 cpi->drop_frame = TRUE; 4533 } 4534 4535 #endif 4536 4537 // Set the count for maximum consequative dropped frames based upon the ratio of 4538 // this frame size to the target average per frame bandwidth. 4539 // (cpi->av_per_frame_bandwidth > 0) is just a sanity check to prevent / 0. 4540 if (cpi->drop_frames_allowed && (cpi->av_per_frame_bandwidth > 0)) 4541 { 4542 cpi->max_drop_count = cpi->projected_frame_size / cpi->av_per_frame_bandwidth; 4543 4544 if (cpi->max_drop_count > cpi->max_consec_dropped_frames) 4545 cpi->max_drop_count = cpi->max_consec_dropped_frames; 4546 } 4547 4548 // Update the buffer level variable. 4549 if (cpi->common.refresh_alt_ref_frame) 4550 cpi->bits_off_target -= cpi->projected_frame_size; 4551 else 4552 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size; 4553 4554 // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass. 4555 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; 4556 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; 4557 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; 4558 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32; 4559 4560 // Actual bits spent 4561 cpi->total_actual_bits += cpi->projected_frame_size; 4562 4563 // Debug stats 4564 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size); 4565 4566 cpi->buffer_level = cpi->bits_off_target; 4567 4568 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames 4569 if (cm->frame_type == KEY_FRAME) 4570 { 4571 cpi->kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 4572 4573 if (cpi->kf_group_bits < 0) 4574 cpi->kf_group_bits = 0 ; 4575 } 4576 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) 4577 { 4578 cpi->gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 4579 4580 if (cpi->gf_group_bits < 0) 4581 cpi->gf_group_bits = 0 ; 4582 } 4583 4584 if (cm->frame_type != KEY_FRAME) 4585 { 4586 if (cpi->common.refresh_alt_ref_frame) 4587 { 4588 cpi->last_skip_false_probs[2] = cpi->prob_skip_false; 4589 cpi->last_skip_probs_q[2] = cm->base_qindex; 4590 } 4591 else if (cpi->common.refresh_golden_frame) 4592 { 4593 cpi->last_skip_false_probs[1] = cpi->prob_skip_false; 4594 cpi->last_skip_probs_q[1] = cm->base_qindex; 4595 } 4596 else 4597 { 4598 cpi->last_skip_false_probs[0] = cpi->prob_skip_false; 4599 cpi->last_skip_probs_q[0] = cm->base_qindex; 4600 4601 //update the baseline 4602 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false; 4603 4604 } 4605 } 4606 4607 #if CONFIG_PSNR 4608 4609 if (0) 4610 { 4611 FILE *f = fopen("tmp.stt", "a"); 4612 4613 vp8_clear_system_state(); //__asm emms; 4614 4615 if (cpi->total_coded_error_left != 0.0) 4616 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f %10.3f %8ld\n", cpi->common.current_video_frame, cpi->this_frame_target, cpi->projected_frame_size, (cpi->projected_frame_size - cpi->this_frame_target), (int)cpi->total_target_vs_actual, (cpi->oxcf.starting_buffer_level - cpi->bits_off_target), (int)cpi->total_actual_bits, cm->base_qindex, cpi->active_best_quality, cpi->active_worst_quality, cpi->avg_frame_qindex, cpi->zbin_over_quant, cm->refresh_golden_frame, cm->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost, cpi->est_max_qcorrection_factor, (int)cpi->bits_left, cpi->total_coded_error_left, (double)cpi->bits_left / cpi->total_coded_error_left, cpi->tot_recode_hits); 4617 else 4618 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f %8ld\n", cpi->common.current_video_frame, cpi->this_frame_target, cpi->projected_frame_size, (cpi->projected_frame_size - cpi->this_frame_target), (int)cpi->total_target_vs_actual, (cpi->oxcf.starting_buffer_level - cpi->bits_off_target), (int)cpi->total_actual_bits, cm->base_qindex, cpi->active_best_quality, cpi->active_worst_quality, cpi->avg_frame_qindex, cpi->zbin_over_quant, cm->refresh_golden_frame, cm->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost, cpi->est_max_qcorrection_factor, (int)cpi->bits_left, cpi->total_coded_error_left, cpi->tot_recode_hits); 4619 4620 fclose(f); 4621 4622 { 4623 FILE *fmodes = fopen("Modes.stt", "a"); 4624 int i; 4625 4626 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, cm->frame_type, cm->refresh_golden_frame, cm->refresh_alt_ref_frame); 4627 4628 for (i = 0; i < MAX_MODES; i++) 4629 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); 4630 4631 fprintf(fmodes, "\n"); 4632 4633 fclose(fmodes); 4634 } 4635 } 4636 4637 #endif 4638 4639 // If this was a kf or Gf note the Q 4640 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame || cm->refresh_alt_ref_frame) 4641 cm->last_kf_gf_q = cm->base_qindex; 4642 4643 if (cm->refresh_golden_frame == 1) 4644 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; 4645 else 4646 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; 4647 4648 if (cm->refresh_alt_ref_frame == 1) 4649 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF; 4650 else 4651 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF; 4652 4653 4654 if (cm->refresh_last_frame & cm->refresh_golden_frame) // both refreshed 4655 cpi->gold_is_last = 1; 4656 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other 4657 cpi->gold_is_last = 0; 4658 4659 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) // both refreshed 4660 cpi->alt_is_last = 1; 4661 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) // 1 refreshed but not the other 4662 cpi->alt_is_last = 0; 4663 4664 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) // both refreshed 4665 cpi->gold_is_alt = 1; 4666 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) // 1 refreshed but not the other 4667 cpi->gold_is_alt = 0; 4668 4669 cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG; 4670 4671 if (cpi->gold_is_last) 4672 cpi->ref_frame_flags &= ~VP8_GOLD_FLAG; 4673 4674 if (cpi->alt_is_last) 4675 cpi->ref_frame_flags &= ~VP8_ALT_FLAG; 4676 4677 if (cpi->gold_is_alt) 4678 cpi->ref_frame_flags &= ~VP8_ALT_FLAG; 4679 4680 4681 if (cpi->oxcf.error_resilient_mode) 4682 { 4683 // Is this an alternate reference update 4684 if (cpi->common.refresh_alt_ref_frame) 4685 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->alt_fb_idx]); 4686 4687 if (cpi->common.refresh_golden_frame) 4688 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cm->yv12_fb[cm->gld_fb_idx]); 4689 } 4690 else 4691 { 4692 if (cpi->oxcf.play_alternate && cpi->common.refresh_alt_ref_frame) 4693 // Update the alternate reference frame and stats as appropriate. 4694 update_alt_ref_frame_and_stats(cpi); 4695 else 4696 // Update the Golden frame and golden frame and stats as appropriate. 4697 update_golden_frame_and_stats(cpi); 4698 } 4699 4700 if (cm->frame_type == KEY_FRAME) 4701 { 4702 // Tell the caller that the frame was coded as a key frame 4703 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; 4704 4705 // As this frame is a key frame the next defaults to an inter frame. 4706 cm->frame_type = INTER_FRAME; 4707 4708 cpi->last_frame_percent_intra = 100; 4709 } 4710 else 4711 { 4712 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; 4713 4714 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra; 4715 } 4716 4717 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas. 4718 cpi->mb.e_mbd.update_mb_segmentation_map = 0; 4719 cpi->mb.e_mbd.update_mb_segmentation_data = 0; 4720 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0; 4721 4722 4723 // Dont increment frame counters if this was an altref buffer update not a real frame 4724 if (cm->show_frame) 4725 { 4726 cm->current_video_frame++; 4727 cpi->frames_since_key++; 4728 } 4729 4730 // reset to normal state now that we are done. 4731 4732 4733 4734 if (0) 4735 { 4736 char filename[512]; 4737 FILE *recon_file; 4738 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); 4739 recon_file = fopen(filename, "wb"); 4740 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc, 4741 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file); 4742 fclose(recon_file); 4743 } 4744 4745 // DEBUG 4746 //vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); 4747 4748 4749 } 4750 4751 int vp8_is_gf_update_needed(VP8_PTR ptr) 4752 { 4753 VP8_COMP *cpi = (VP8_COMP *) ptr; 4754 int ret_val; 4755 4756 ret_val = cpi->gf_update_recommended; 4757 cpi->gf_update_recommended = 0; 4758 4759 return ret_val; 4760 } 4761 4762 void vp8_check_gf_quality(VP8_COMP *cpi) 4763 { 4764 VP8_COMMON *cm = &cpi->common; 4765 int gf_active_pct = (100 * cpi->gf_active_count) / (cm->mb_rows * cm->mb_cols); 4766 int gf_ref_usage_pct = (cpi->count_mb_ref_frame_usage[GOLDEN_FRAME] * 100) / (cm->mb_rows * cm->mb_cols); 4767 int last_ref_zz_useage = (cpi->inter_zz_count * 100) / (cm->mb_rows * cm->mb_cols); 4768 4769 // Gf refresh is not currently being signalled 4770 if (cpi->gf_update_recommended == 0) 4771 { 4772 if (cpi->common.frames_since_golden > 7) 4773 { 4774 // Low use of gf 4775 if ((gf_active_pct < 10) || ((gf_active_pct + gf_ref_usage_pct) < 15)) 4776 { 4777 // ...but last frame zero zero usage is reasonbable so a new gf might be appropriate 4778 if (last_ref_zz_useage >= 25) 4779 { 4780 cpi->gf_bad_count ++; 4781 4782 if (cpi->gf_bad_count >= 8) // Check that the condition is stable 4783 { 4784 cpi->gf_update_recommended = 1; 4785 cpi->gf_bad_count = 0; 4786 } 4787 } 4788 else 4789 cpi->gf_bad_count = 0; // Restart count as the background is not stable enough 4790 } 4791 else 4792 cpi->gf_bad_count = 0; // Gf useage has picked up so reset count 4793 } 4794 } 4795 // If the signal is set but has not been read should we cancel it. 4796 else if (last_ref_zz_useage < 15) 4797 { 4798 cpi->gf_update_recommended = 0; 4799 cpi->gf_bad_count = 0; 4800 } 4801 4802 #if 0 4803 4804 if (0) 4805 { 4806 FILE *f = fopen("gfneeded.stt", "a"); 4807 fprintf(f, "%10d %10d %10d %10d %10ld \n", 4808 cm->current_video_frame, 4809 cpi->common.frames_since_golden, 4810 gf_active_pct, gf_ref_usage_pct, 4811 cpi->gf_update_recommended); 4812 fclose(f); 4813 } 4814 4815 #endif 4816 } 4817 4818 #if !(CONFIG_REALTIME_ONLY) 4819 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) 4820 { 4821 4822 if (!cpi->common.refresh_alt_ref_frame) 4823 vp8_second_pass(cpi); 4824 4825 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 4826 cpi->bits_left -= 8 * *size; 4827 4828 if (!cpi->common.refresh_alt_ref_frame) 4829 { 4830 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth 4831 *cpi->oxcf.two_pass_vbrmin_section / 100); 4832 cpi->bits_left += (long long)(two_pass_min_rate / cpi->oxcf.frame_rate); 4833 } 4834 } 4835 #endif 4836 4837 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us. 4838 #if HAVE_ARMV7 4839 extern void vp8_push_neon(INT64 *store); 4840 extern void vp8_pop_neon(INT64 *store); 4841 static INT64 store_reg[8]; 4842 #endif 4843 int vp8_receive_raw_frame(VP8_PTR ptr, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, INT64 time_stamp, INT64 end_time) 4844 { 4845 VP8_COMP *cpi = (VP8_COMP *) ptr; 4846 VP8_COMMON *cm = &cpi->common; 4847 struct vpx_usec_timer timer; 4848 4849 if (!cpi) 4850 return -1; 4851 4852 #if HAVE_ARMV7 4853 vp8_push_neon(store_reg); 4854 #endif 4855 4856 vpx_usec_timer_start(&timer); 4857 4858 // no more room for frames; 4859 if (cpi->source_buffer_count != 0 && cpi->source_buffer_count >= cpi->oxcf.lag_in_frames) 4860 { 4861 #if HAVE_ARMV7 4862 vp8_pop_neon(store_reg); 4863 #endif 4864 return -1; 4865 } 4866 4867 //printf("in-cpi->source_buffer_count: %d\n", cpi->source_buffer_count); 4868 4869 cm->clr_type = sd->clrtype; 4870 4871 // make a copy of the frame for use later... 4872 #if !(CONFIG_REALTIME_ONLY) 4873 4874 if (cpi->oxcf.allow_lag) 4875 { 4876 int which_buffer = cpi->source_encode_index - 1; 4877 SOURCE_SAMPLE *s; 4878 4879 if (which_buffer == -1) 4880 which_buffer = cpi->oxcf.lag_in_frames - 1; 4881 4882 if (cpi->source_buffer_count < cpi->oxcf.lag_in_frames - 1) 4883 which_buffer = cpi->source_buffer_count; 4884 4885 s = &cpi->src_buffer[which_buffer]; 4886 4887 s->source_time_stamp = time_stamp; 4888 s->source_end_time_stamp = end_time; 4889 s->source_frame_flags = frame_flags; 4890 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer); 4891 4892 cpi->source_buffer_count ++; 4893 } 4894 else 4895 #endif 4896 { 4897 SOURCE_SAMPLE *s; 4898 s = &cpi->src_buffer[0]; 4899 s->source_end_time_stamp = end_time; 4900 s->source_time_stamp = time_stamp; 4901 s->source_frame_flags = frame_flags; 4902 #if HAVE_ARMV7 4903 vp8_yv12_copy_src_frame_func_neon(sd, &s->source_buffer); 4904 #else 4905 vp8_yv12_copy_frame_ptr(sd, &s->source_buffer); 4906 #endif 4907 cpi->source_buffer_count = 1; 4908 } 4909 4910 vpx_usec_timer_mark(&timer); 4911 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); 4912 4913 #if HAVE_ARMV7 4914 vp8_pop_neon(store_reg); 4915 #endif 4916 4917 return 0; 4918 } 4919 int vp8_get_compressed_data(VP8_PTR ptr, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, INT64 *time_stamp, INT64 *time_end, int flush) 4920 { 4921 4922 VP8_COMP *cpi = (VP8_COMP *) ptr; 4923 VP8_COMMON *cm = &cpi->common; 4924 struct vpx_usec_timer tsctimer; 4925 struct vpx_usec_timer ticktimer; 4926 struct vpx_usec_timer cmptimer; 4927 4928 if (!cpi) 4929 return -1; 4930 4931 #if HAVE_ARMV7 4932 vp8_push_neon(store_reg); 4933 #endif 4934 4935 vpx_usec_timer_start(&cmptimer); 4936 4937 4938 // flush variable tells us that even though we have less than 10 frames 4939 // in our buffer we need to start producing compressed frames. 4940 // Probably because we are at the end of a file.... 4941 if ((cpi->source_buffer_count == cpi->oxcf.lag_in_frames && cpi->oxcf.lag_in_frames > 0) 4942 || (!cpi->oxcf.allow_lag && cpi->source_buffer_count > 0) 4943 || (flush && cpi->source_buffer_count > 0)) 4944 { 4945 4946 SOURCE_SAMPLE *s; 4947 4948 s = &cpi->src_buffer[cpi->source_encode_index]; 4949 cpi->source_time_stamp = s->source_time_stamp; 4950 cpi->source_end_time_stamp = s->source_end_time_stamp; 4951 4952 #if !(CONFIG_REALTIME_ONLY) 4953 4954 // Should we code an alternate reference frame 4955 if (cpi->oxcf.error_resilient_mode == 0 && 4956 cpi->oxcf.play_alternate && 4957 cpi->source_alt_ref_pending && 4958 (cpi->frames_till_gf_update_due < cpi->source_buffer_count) && 4959 cpi->oxcf.lag_in_frames != 0) 4960 { 4961 cpi->last_alt_ref_sei = (cpi->source_encode_index + cpi->frames_till_gf_update_due) % cpi->oxcf.lag_in_frames; 4962 4963 #if VP8_TEMPORAL_ALT_REF 4964 4965 if (cpi->oxcf.arnr_max_frames > 0) 4966 { 4967 #if 0 4968 // my attempt at a loop that tests the results of strength filter. 4969 int start_frame = cpi->last_alt_ref_sei - 3; 4970 4971 int i, besti = -1, pastin = cpi->oxcf.arnr_strength; 4972 4973 int besterr; 4974 4975 if (start_frame < 0) 4976 start_frame += cpi->oxcf.lag_in_frames; 4977 4978 besterr = vp8_calc_low_ss_err(&cpi->src_buffer[cpi->last_alt_ref_sei].source_buffer, 4979 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance)); 4980 4981 for (i = 0; i < 7; i++) 4982 { 4983 int thiserr; 4984 cpi->oxcf.arnr_strength = i; 4985 vp8cx_temp_filter_c(cpi); 4986 4987 thiserr = vp8_calc_low_ss_err(&cpi->alt_ref_buffer.source_buffer, 4988 &cpi->src_buffer[start_frame].source_buffer, IF_RTCD(&cpi->rtcd.variance)); 4989 4990 if (10 * thiserr < besterr * 8) 4991 { 4992 besterr = thiserr; 4993 besti = i; 4994 } 4995 } 4996 4997 if (besti != -1) 4998 { 4999 cpi->oxcf.arnr_strength = besti; 5000 vp8cx_temp_filter_c(cpi); 5001 s = &cpi->alt_ref_buffer; 5002 5003 // FWG not sure if I need to copy this data for the Alt Ref frame 5004 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp; 5005 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp; 5006 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags; 5007 } 5008 else 5009 s = &cpi->src_buffer[cpi->last_alt_ref_sei]; 5010 5011 #else 5012 vp8cx_temp_filter_c(cpi); 5013 s = &cpi->alt_ref_buffer; 5014 5015 // FWG not sure if I need to copy this data for the Alt Ref frame 5016 s->source_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_time_stamp; 5017 s->source_end_time_stamp = cpi->src_buffer[cpi->last_alt_ref_sei].source_end_time_stamp; 5018 s->source_frame_flags = cpi->src_buffer[cpi->last_alt_ref_sei].source_frame_flags; 5019 5020 #endif 5021 } 5022 else 5023 #endif 5024 s = &cpi->src_buffer[cpi->last_alt_ref_sei]; 5025 5026 cm->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due; 5027 cm->refresh_alt_ref_frame = 1; 5028 cm->refresh_golden_frame = 0; 5029 cm->refresh_last_frame = 0; 5030 cm->show_frame = 0; 5031 cpi->source_alt_ref_pending = FALSE; // Clear Pending altf Ref flag. 5032 cpi->is_src_frame_alt_ref = 0; 5033 } 5034 else 5035 #endif 5036 { 5037 cm->show_frame = 1; 5038 #if !(CONFIG_REALTIME_ONLY) 5039 5040 if (cpi->oxcf.allow_lag) 5041 { 5042 if (cpi->source_encode_index == cpi->last_alt_ref_sei) 5043 { 5044 cpi->is_src_frame_alt_ref = 1; 5045 cpi->last_alt_ref_sei = -1; 5046 } 5047 else 5048 cpi->is_src_frame_alt_ref = 0; 5049 5050 cpi->source_encode_index = (cpi->source_encode_index + 1) % cpi->oxcf.lag_in_frames; 5051 } 5052 5053 #endif 5054 cpi->source_buffer_count--; 5055 } 5056 5057 cpi->un_scaled_source = &s->source_buffer; 5058 cpi->Source = &s->source_buffer; 5059 cpi->source_frame_flags = s->source_frame_flags; 5060 5061 *time_stamp = cpi->source_time_stamp; 5062 *time_end = cpi->source_end_time_stamp; 5063 } 5064 else 5065 { 5066 *size = 0; 5067 #if !(CONFIG_REALTIME_ONLY) 5068 5069 if (flush && cpi->pass == 1 && !cpi->first_pass_done) 5070 { 5071 vp8_end_first_pass(cpi); /* get last stats packet */ 5072 cpi->first_pass_done = 1; 5073 } 5074 5075 #endif 5076 5077 #if HAVE_ARMV7 5078 vp8_pop_neon(store_reg); 5079 #endif 5080 return -1; 5081 } 5082 5083 *frame_flags = cpi->source_frame_flags; 5084 5085 #if CONFIG_PSNR 5086 5087 if (cpi->source_time_stamp < cpi->first_time_stamp_ever) 5088 cpi->first_time_stamp_ever = cpi->source_time_stamp; 5089 5090 #endif 5091 5092 // adjust frame rates based on timestamps given 5093 if (!cm->refresh_alt_ref_frame) 5094 { 5095 if (cpi->last_time_stamp_seen == 0) 5096 { 5097 double this_fps = 10000000.000 / (cpi->source_end_time_stamp - cpi->source_time_stamp); 5098 5099 vp8_new_frame_rate(cpi, this_fps); 5100 } 5101 else 5102 { 5103 long long nanosecs = cpi->source_time_stamp - cpi->last_time_stamp_seen; 5104 double this_fps = 10000000.000 / nanosecs; 5105 5106 vp8_new_frame_rate(cpi, (7 * cpi->oxcf.frame_rate + this_fps) / 8); 5107 5108 } 5109 5110 cpi->last_time_stamp_seen = cpi->source_time_stamp; 5111 } 5112 5113 if (cpi->compressor_speed == 2) 5114 { 5115 vp8_check_gf_quality(cpi); 5116 } 5117 5118 if (!cpi) 5119 { 5120 #if HAVE_ARMV7 5121 vp8_pop_neon(store_reg); 5122 #endif 5123 return 0; 5124 } 5125 5126 if (cpi->compressor_speed == 2) 5127 { 5128 vpx_usec_timer_start(&tsctimer); 5129 vpx_usec_timer_start(&ticktimer); 5130 } 5131 5132 // start with a 0 size frame 5133 *size = 0; 5134 5135 // Clear down mmx registers 5136 vp8_clear_system_state(); //__asm emms; 5137 5138 cm->frame_type = INTER_FRAME; 5139 cm->frame_flags = *frame_flags; 5140 5141 #if 0 5142 5143 if (cm->refresh_alt_ref_frame) 5144 { 5145 //cm->refresh_golden_frame = 1; 5146 cm->refresh_golden_frame = 0; 5147 cm->refresh_last_frame = 0; 5148 } 5149 else 5150 { 5151 cm->refresh_golden_frame = 0; 5152 cm->refresh_last_frame = 1; 5153 } 5154 5155 #endif 5156 5157 #if !(CONFIG_REALTIME_ONLY) 5158 5159 if (cpi->pass == 1) 5160 { 5161 Pass1Encode(cpi, size, dest, frame_flags); 5162 } 5163 else if (cpi->pass == 2) 5164 { 5165 Pass2Encode(cpi, size, dest, frame_flags); 5166 } 5167 else 5168 #endif 5169 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 5170 5171 if (cpi->compressor_speed == 2) 5172 { 5173 unsigned int duration, duration2; 5174 vpx_usec_timer_mark(&tsctimer); 5175 vpx_usec_timer_mark(&ticktimer); 5176 5177 duration = vpx_usec_timer_elapsed(&ticktimer); 5178 duration2 = (unsigned int)((double)duration / 2); 5179 5180 if (cm->frame_type != KEY_FRAME) 5181 { 5182 if (cpi->avg_encode_time == 0) 5183 cpi->avg_encode_time = duration; 5184 else 5185 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3; 5186 } 5187 5188 if (duration2) 5189 { 5190 //if(*frame_flags!=1) 5191 { 5192 5193 if (cpi->avg_pick_mode_time == 0) 5194 cpi->avg_pick_mode_time = duration2; 5195 else 5196 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3; 5197 } 5198 } 5199 5200 } 5201 5202 if (cm->refresh_entropy_probs == 0) 5203 { 5204 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc)); 5205 } 5206 5207 // if its a dropped frame honor the requests on subsequent frames 5208 if (*size > 0) 5209 { 5210 5211 // return to normal state 5212 cm->refresh_entropy_probs = 1; 5213 cm->refresh_alt_ref_frame = 0; 5214 cm->refresh_golden_frame = 0; 5215 cm->refresh_last_frame = 1; 5216 cm->frame_type = INTER_FRAME; 5217 5218 } 5219 5220 cpi->ready_for_new_frame = 1; 5221 5222 vpx_usec_timer_mark(&cmptimer); 5223 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); 5224 5225 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) 5226 generate_psnr_packet(cpi); 5227 5228 #if CONFIG_PSNR 5229 5230 if (cpi->pass != 1) 5231 { 5232 cpi->bytes += *size; 5233 5234 if (cm->show_frame) 5235 { 5236 5237 cpi->count ++; 5238 5239 if (cpi->b_calculate_psnr) 5240 { 5241 double y, u, v; 5242 double sq_error; 5243 double frame_psnr = vp8_calc_psnr(cpi->Source, cm->frame_to_show, &y, &u, &v, &sq_error); 5244 5245 cpi->total_y += y; 5246 cpi->total_u += u; 5247 cpi->total_v += v; 5248 cpi->total_sq_error += sq_error; 5249 cpi->total += frame_psnr; 5250 { 5251 double y2, u2, v2, frame_psnr2, frame_ssim2 = 0; 5252 double weight = 0; 5253 5254 vp8_deblock(cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0, IF_RTCD(&cm->rtcd.postproc)); 5255 vp8_clear_system_state(); 5256 frame_psnr2 = vp8_calc_psnr(cpi->Source, &cm->post_proc_buffer, &y2, &u2, &v2, &sq_error); 5257 frame_ssim2 = vp8_calc_ssim(cpi->Source, &cm->post_proc_buffer, 1, &weight); 5258 5259 cpi->summed_quality += frame_ssim2 * weight; 5260 cpi->summed_weights += weight; 5261 5262 cpi->totalp_y += y2; 5263 cpi->totalp_u += u2; 5264 cpi->totalp_v += v2; 5265 cpi->totalp += frame_psnr2; 5266 cpi->total_sq_error2 += sq_error; 5267 5268 } 5269 } 5270 5271 if (cpi->b_calculate_ssimg) 5272 { 5273 double y, u, v, frame_all; 5274 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v); 5275 cpi->total_ssimg_y += y; 5276 cpi->total_ssimg_u += u; 5277 cpi->total_ssimg_v += v; 5278 cpi->total_ssimg_all += frame_all; 5279 } 5280 5281 } 5282 } 5283 5284 #if 0 5285 5286 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q) 5287 { 5288 skiptruecount += cpi->skip_true_count; 5289 skipfalsecount += cpi->skip_false_count; 5290 } 5291 5292 #endif 5293 #if 0 5294 5295 if (cpi->pass != 1) 5296 { 5297 FILE *f = fopen("skip.stt", "a"); 5298 fprintf(f, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi->common.current_video_frame, *frame_flags, cpi->common.base_qindex, cpi->prob_skip_false, *size); 5299 5300 if (cpi->is_src_frame_alt_ref == 1) 5301 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size); 5302 5303 fclose(f); 5304 } 5305 5306 #endif 5307 #endif 5308 5309 #if HAVE_ARMV7 5310 vp8_pop_neon(store_reg); 5311 #endif 5312 5313 return 0; 5314 } 5315 5316 int vp8_get_preview_raw_frame(VP8_PTR comp, YV12_BUFFER_CONFIG *dest, int deblock_level, int noise_level, int flags) 5317 { 5318 VP8_COMP *cpi = (VP8_COMP *) comp; 5319 5320 if (cpi->common.refresh_alt_ref_frame) 5321 return -1; 5322 else 5323 { 5324 int ret; 5325 #if CONFIG_POSTPROC 5326 ret = vp8_post_proc_frame(&cpi->common, dest, deblock_level, noise_level, flags); 5327 #else 5328 5329 if (cpi->common.frame_to_show) 5330 { 5331 *dest = *cpi->common.frame_to_show; 5332 dest->y_width = cpi->common.Width; 5333 dest->y_height = cpi->common.Height; 5334 dest->uv_height = cpi->common.Height / 2; 5335 ret = 0; 5336 } 5337 else 5338 { 5339 ret = -1; 5340 } 5341 5342 #endif //!CONFIG_POSTPROC 5343 vp8_clear_system_state(); 5344 return ret; 5345 } 5346 } 5347 5348 int vp8_set_roimap(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4]) 5349 { 5350 VP8_COMP *cpi = (VP8_COMP *) comp; 5351 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 5352 5353 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) 5354 return -1; 5355 5356 if (!map) 5357 { 5358 disable_segmentation((VP8_PTR)cpi); 5359 return 0; 5360 } 5361 5362 // Set the segmentation Map 5363 set_segmentation_map((VP8_PTR)cpi, map); 5364 5365 // Activate segmentation. 5366 enable_segmentation((VP8_PTR)cpi); 5367 5368 // Set up the quant segment data 5369 feature_data[MB_LVL_ALT_Q][0] = delta_q[0]; 5370 feature_data[MB_LVL_ALT_Q][1] = delta_q[1]; 5371 feature_data[MB_LVL_ALT_Q][2] = delta_q[2]; 5372 feature_data[MB_LVL_ALT_Q][3] = delta_q[3]; 5373 5374 // Set up the loop segment data s 5375 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0]; 5376 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1]; 5377 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2]; 5378 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3]; 5379 5380 cpi->segment_encode_breakout[0] = threshold[0]; 5381 cpi->segment_encode_breakout[1] = threshold[1]; 5382 cpi->segment_encode_breakout[2] = threshold[2]; 5383 cpi->segment_encode_breakout[3] = threshold[3]; 5384 5385 // Initialise the feature data structure 5386 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 5387 set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA); 5388 5389 return 0; 5390 } 5391 5392 int vp8_set_active_map(VP8_PTR comp, unsigned char *map, unsigned int rows, unsigned int cols) 5393 { 5394 VP8_COMP *cpi = (VP8_COMP *) comp; 5395 5396 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) 5397 { 5398 if (map) 5399 { 5400 vpx_memcpy(cpi->active_map, map, rows * cols); 5401 cpi->active_map_enabled = 1; 5402 } 5403 else 5404 cpi->active_map_enabled = 0; 5405 5406 return 0; 5407 } 5408 else 5409 { 5410 //cpi->active_map_enabled = 0; 5411 return -1 ; 5412 } 5413 } 5414 5415 int vp8_set_internal_size(VP8_PTR comp, VPX_SCALING horiz_mode, VPX_SCALING vert_mode) 5416 { 5417 VP8_COMP *cpi = (VP8_COMP *) comp; 5418 5419 if (horiz_mode >= NORMAL && horiz_mode <= ONETWO) 5420 cpi->common.horiz_scale = horiz_mode; 5421 else 5422 return -1; 5423 5424 if (vert_mode >= NORMAL && vert_mode <= ONETWO) 5425 cpi->common.vert_scale = vert_mode; 5426 else 5427 return -1; 5428 5429 return 0; 5430 } 5431 5432 5433 5434 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd) 5435 { 5436 int i, j; 5437 int Total = 0; 5438 5439 unsigned char *src = source->y_buffer; 5440 unsigned char *dst = dest->y_buffer; 5441 (void)rtcd; 5442 5443 // Loop through the Y plane raw and reconstruction data summing (square differences) 5444 for (i = 0; i < source->y_height; i += 16) 5445 { 5446 for (j = 0; j < source->y_width; j += 16) 5447 { 5448 unsigned int sse; 5449 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse); 5450 } 5451 5452 src += 16 * source->y_stride; 5453 dst += 16 * dest->y_stride; 5454 } 5455 5456 return Total; 5457 } 5458 int vp8_calc_low_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd) 5459 { 5460 int i, j; 5461 int Total = 0; 5462 5463 unsigned char *src = source->y_buffer; 5464 unsigned char *dst = dest->y_buffer; 5465 (void)rtcd; 5466 5467 // Loop through the Y plane raw and reconstruction data summing (square differences) 5468 for (i = 0; i < source->y_height; i += 16) 5469 { 5470 for (j = 0; j < source->y_width; j += 16) 5471 { 5472 unsigned int sse; 5473 VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse); 5474 5475 if (sse < 8096) 5476 Total += sse; 5477 } 5478 5479 src += 16 * source->y_stride; 5480 dst += 16 * dest->y_stride; 5481 } 5482 5483 return Total; 5484 } 5485 5486 int vp8_get_speed(VP8_PTR c) 5487 { 5488 VP8_COMP *cpi = (VP8_COMP *) c; 5489 return cpi->Speed; 5490 } 5491 int vp8_get_quantizer(VP8_PTR c) 5492 { 5493 VP8_COMP *cpi = (VP8_COMP *) c; 5494 return cpi->common.base_qindex; 5495 } 5496