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 "vpx_config.h" 13 #include "vp9/common/vp9_filter.h" 14 #include "vp9/common/vp9_onyxc_int.h" 15 #include "vp9/common/vp9_reconinter.h" 16 #include "vp9/encoder/vp9_onyx_int.h" 17 #include "vp9/common/vp9_systemdependent.h" 18 #include "vp9/encoder/vp9_quantize.h" 19 #include "vp9/common/vp9_alloccommon.h" 20 #include "vp9/encoder/vp9_mcomp.h" 21 #include "vp9/encoder/vp9_firstpass.h" 22 #include "vp9/encoder/vp9_psnr.h" 23 #include "vpx_scale/vpx_scale.h" 24 #include "vp9/common/vp9_extend.h" 25 #include "vp9/encoder/vp9_ratectrl.h" 26 #include "vp9/common/vp9_quant_common.h" 27 #include "vp9/common/vp9_tile_common.h" 28 #include "vp9/encoder/vp9_segmentation.h" 29 #include "./vp9_rtcd.h" 30 #include "./vpx_scale_rtcd.h" 31 #if CONFIG_VP9_POSTPROC 32 #include "vp9/common/vp9_postproc.h" 33 #endif 34 #include "vpx_mem/vpx_mem.h" 35 #include "vpx_ports/vpx_timer.h" 36 37 #include "vp9/common/vp9_seg_common.h" 38 #include "vp9/encoder/vp9_mbgraph.h" 39 #include "vp9/common/vp9_pred_common.h" 40 #include "vp9/encoder/vp9_rdopt.h" 41 #include "vp9/encoder/vp9_bitstream.h" 42 #include "vp9/encoder/vp9_picklpf.h" 43 #include "vp9/common/vp9_mvref_common.h" 44 #include "vp9/encoder/vp9_temporal_filter.h" 45 46 #include <math.h> 47 #include <stdio.h> 48 #include <limits.h> 49 50 extern void print_tree_update_probs(); 51 52 static void set_default_lf_deltas(struct loopfilter *lf); 53 54 #define DEFAULT_INTERP_FILTER SWITCHABLE 55 56 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ 57 58 #define ALTREF_HIGH_PRECISION_MV 1 /* whether to use high precision mv 59 for altref computation */ 60 #define HIGH_PRECISION_MV_QTHRESH 200 /* Q threshold for use of high precision 61 mv. Choose a very high value for 62 now so that HIGH_PRECISION is always 63 chosen */ 64 65 #if CONFIG_INTERNAL_STATS 66 #include "math.h" 67 68 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source, 69 YV12_BUFFER_CONFIG *dest, int lumamask, 70 double *weight); 71 72 73 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source, 74 YV12_BUFFER_CONFIG *dest, double *ssim_y, 75 double *ssim_u, double *ssim_v); 76 77 78 #endif 79 80 // #define OUTPUT_YUV_REC 81 82 #ifdef OUTPUT_YUV_SRC 83 FILE *yuv_file; 84 #endif 85 #ifdef OUTPUT_YUV_REC 86 FILE *yuv_rec_file; 87 #endif 88 89 #if 0 90 FILE *framepsnr; 91 FILE *kf_list; 92 FILE *keyfile; 93 #endif 94 95 96 #ifdef ENTROPY_STATS 97 extern int intra_mode_stats[INTRA_MODES] 98 [INTRA_MODES] 99 [INTRA_MODES]; 100 #endif 101 102 #ifdef MODE_STATS 103 extern void init_tx_count_stats(); 104 extern void write_tx_count_stats(); 105 extern void init_switchable_interp_stats(); 106 extern void write_switchable_interp_stats(); 107 #endif 108 109 #ifdef SPEEDSTATS 110 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 111 #endif 112 113 #if defined(SECTIONBITS_OUTPUT) 114 extern unsigned __int64 Sectionbits[500]; 115 #endif 116 117 extern void vp9_init_quantizer(VP9_COMP *cpi); 118 119 // Tables relating active max Q to active min Q 120 static int kf_low_motion_minq[QINDEX_RANGE]; 121 static int kf_high_motion_minq[QINDEX_RANGE]; 122 static int gf_low_motion_minq[QINDEX_RANGE]; 123 static int gf_high_motion_minq[QINDEX_RANGE]; 124 static int inter_minq[QINDEX_RANGE]; 125 126 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) { 127 switch (mode) { 128 case NORMAL: 129 *hr = 1; 130 *hs = 1; 131 break; 132 case FOURFIVE: 133 *hr = 4; 134 *hs = 5; 135 break; 136 case THREEFIVE: 137 *hr = 3; 138 *hs = 5; 139 break; 140 case ONETWO: 141 *hr = 1; 142 *hs = 2; 143 break; 144 default: 145 *hr = 1; 146 *hs = 1; 147 assert(0); 148 break; 149 } 150 } 151 152 // Functions to compute the active minq lookup table entries based on a 153 // formulaic approach to facilitate easier adjustment of the Q tables. 154 // The formulae were derived from computing a 3rd order polynomial best 155 // fit to the original data (after plotting real maxq vs minq (not q index)) 156 static int calculate_minq_index(double maxq, 157 double x3, double x2, double x1, double c) { 158 int i; 159 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, 160 maxq); 161 162 // Special case handling to deal with the step from q2.0 163 // down to lossless mode represented by q 1.0. 164 if (minqtarget <= 2.0) 165 return 0; 166 167 for (i = 0; i < QINDEX_RANGE; i++) { 168 if (minqtarget <= vp9_convert_qindex_to_q(i)) 169 return i; 170 } 171 172 return QINDEX_RANGE - 1; 173 } 174 175 static void init_minq_luts(void) { 176 int i; 177 178 for (i = 0; i < QINDEX_RANGE; i++) { 179 const double maxq = vp9_convert_qindex_to_q(i); 180 181 182 kf_low_motion_minq[i] = calculate_minq_index(maxq, 183 0.000001, 184 -0.0004, 185 0.15, 186 0.0); 187 kf_high_motion_minq[i] = calculate_minq_index(maxq, 188 0.000002, 189 -0.0012, 190 0.5, 191 0.0); 192 193 gf_low_motion_minq[i] = calculate_minq_index(maxq, 194 0.0000015, 195 -0.0009, 196 0.33, 197 0.0); 198 gf_high_motion_minq[i] = calculate_minq_index(maxq, 199 0.0000021, 200 -0.00125, 201 0.45, 202 0.0); 203 inter_minq[i] = calculate_minq_index(maxq, 204 0.00000271, 205 -0.00113, 206 0.697, 207 0.0); 208 209 } 210 } 211 212 static void set_mvcost(MACROBLOCK *mb) { 213 if (mb->e_mbd.allow_high_precision_mv) { 214 mb->mvcost = mb->nmvcost_hp; 215 mb->mvsadcost = mb->nmvsadcost_hp; 216 } else { 217 mb->mvcost = mb->nmvcost; 218 mb->mvsadcost = mb->nmvsadcost; 219 } 220 } 221 222 void vp9_initialize_enc() { 223 static int init_done = 0; 224 225 if (!init_done) { 226 vp9_initialize_common(); 227 vp9_tokenize_initialize(); 228 vp9_init_quant_tables(); 229 vp9_init_me_luts(); 230 init_minq_luts(); 231 // init_base_skip_probs(); 232 init_done = 1; 233 } 234 } 235 236 static void setup_features(VP9_COMMON *cm) { 237 struct loopfilter *const lf = &cm->lf; 238 struct segmentation *const seg = &cm->seg; 239 240 // Set up default state for MB feature flags 241 seg->enabled = 0; 242 243 seg->update_map = 0; 244 seg->update_data = 0; 245 vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs)); 246 247 vp9_clearall_segfeatures(seg); 248 249 lf->mode_ref_delta_enabled = 0; 250 lf->mode_ref_delta_update = 0; 251 vp9_zero(lf->ref_deltas); 252 vp9_zero(lf->mode_deltas); 253 vp9_zero(lf->last_ref_deltas); 254 vp9_zero(lf->last_mode_deltas); 255 256 set_default_lf_deltas(lf); 257 } 258 259 static void dealloc_compressor_data(VP9_COMP *cpi) { 260 // Delete sementation map 261 vpx_free(cpi->segmentation_map); 262 cpi->segmentation_map = 0; 263 vpx_free(cpi->common.last_frame_seg_map); 264 cpi->common.last_frame_seg_map = 0; 265 vpx_free(cpi->coding_context.last_frame_seg_map_copy); 266 cpi->coding_context.last_frame_seg_map_copy = 0; 267 268 vpx_free(cpi->active_map); 269 cpi->active_map = 0; 270 271 vp9_free_frame_buffers(&cpi->common); 272 273 vp9_free_frame_buffer(&cpi->last_frame_uf); 274 vp9_free_frame_buffer(&cpi->scaled_source); 275 vp9_free_frame_buffer(&cpi->alt_ref_buffer); 276 vp9_lookahead_destroy(cpi->lookahead); 277 278 vpx_free(cpi->tok); 279 cpi->tok = 0; 280 281 // Activity mask based per mb zbin adjustments 282 vpx_free(cpi->mb_activity_map); 283 cpi->mb_activity_map = 0; 284 vpx_free(cpi->mb_norm_activity_map); 285 cpi->mb_norm_activity_map = 0; 286 287 vpx_free(cpi->mb.pip); 288 cpi->mb.pip = 0; 289 } 290 291 // Computes a q delta (in "q index" terms) to get from a starting q value 292 // to a target value 293 // target q value 294 static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) { 295 int i; 296 int start_index = cpi->worst_quality; 297 int target_index = cpi->worst_quality; 298 299 // Convert the average q value to an index. 300 for (i = cpi->best_quality; i < cpi->worst_quality; i++) { 301 start_index = i; 302 if (vp9_convert_qindex_to_q(i) >= qstart) 303 break; 304 } 305 306 // Convert the q target to an index 307 for (i = cpi->best_quality; i < cpi->worst_quality; i++) { 308 target_index = i; 309 if (vp9_convert_qindex_to_q(i) >= qtarget) 310 break; 311 } 312 313 return target_index - start_index; 314 } 315 316 static void configure_static_seg_features(VP9_COMP *cpi) { 317 VP9_COMMON *cm = &cpi->common; 318 struct segmentation *seg = &cm->seg; 319 320 int high_q = (int)(cpi->avg_q > 48.0); 321 int qi_delta; 322 323 // Disable and clear down for KF 324 if (cm->frame_type == KEY_FRAME) { 325 // Clear down the global segmentation map 326 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 327 seg->update_map = 0; 328 seg->update_data = 0; 329 cpi->static_mb_pct = 0; 330 331 // Disable segmentation 332 vp9_disable_segmentation((VP9_PTR)cpi); 333 334 // Clear down the segment features. 335 vp9_clearall_segfeatures(seg); 336 } else if (cpi->refresh_alt_ref_frame) { 337 // If this is an alt ref frame 338 // Clear down the global segmentation map 339 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 340 seg->update_map = 0; 341 seg->update_data = 0; 342 cpi->static_mb_pct = 0; 343 344 // Disable segmentation and individual segment features by default 345 vp9_disable_segmentation((VP9_PTR)cpi); 346 vp9_clearall_segfeatures(seg); 347 348 // Scan frames from current to arf frame. 349 // This function re-enables segmentation if appropriate. 350 vp9_update_mbgraph_stats(cpi); 351 352 // If segmentation was enabled set those features needed for the 353 // arf itself. 354 if (seg->enabled) { 355 seg->update_map = 1; 356 seg->update_data = 1; 357 358 qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875)); 359 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2)); 360 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 361 362 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 363 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 364 365 // Where relevant assume segment data is delta data 366 seg->abs_delta = SEGMENT_DELTADATA; 367 368 } 369 } else if (seg->enabled) { 370 // All other frames if segmentation has been enabled 371 372 // First normal frame in a valid gf or alt ref group 373 if (cpi->frames_since_golden == 0) { 374 // Set up segment features for normal frames in an arf group 375 if (cpi->source_alt_ref_active) { 376 seg->update_map = 0; 377 seg->update_data = 1; 378 seg->abs_delta = SEGMENT_DELTADATA; 379 380 qi_delta = compute_qdelta(cpi, cpi->avg_q, 381 (cpi->avg_q * 1.125)); 382 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2)); 383 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 384 385 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 386 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 387 388 // Segment coding disabled for compred testing 389 if (high_q || (cpi->static_mb_pct == 100)) { 390 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 391 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 392 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 393 } 394 } else { 395 // Disable segmentation and clear down features if alt ref 396 // is not active for this group 397 398 vp9_disable_segmentation((VP9_PTR)cpi); 399 400 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 401 402 seg->update_map = 0; 403 seg->update_data = 0; 404 405 vp9_clearall_segfeatures(seg); 406 } 407 } else if (cpi->is_src_frame_alt_ref) { 408 // Special case where we are coding over the top of a previous 409 // alt ref frame. 410 // Segment coding disabled for compred testing 411 412 // Enable ref frame features for segment 0 as well 413 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME); 414 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 415 416 // All mbs should use ALTREF_FRAME 417 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME); 418 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME); 419 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME); 420 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 421 422 // Skip all MBs if high Q (0,0 mv and skip coeffs) 423 if (high_q) { 424 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP); 425 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 426 } 427 // Enable data update 428 seg->update_data = 1; 429 } else { 430 // All other frames. 431 432 // No updates.. leave things as they are. 433 seg->update_map = 0; 434 seg->update_data = 0; 435 } 436 } 437 } 438 439 #ifdef ENTROPY_STATS 440 void vp9_update_mode_context_stats(VP9_COMP *cpi) { 441 VP9_COMMON *cm = &cpi->common; 442 int i, j; 443 unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] = 444 cm->fc.inter_mode_counts; 445 int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats; 446 FILE *f; 447 448 // Read the past stats counters 449 f = fopen("mode_context.bin", "rb"); 450 if (!f) { 451 vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats)); 452 } else { 453 fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); 454 fclose(f); 455 } 456 457 // Add in the values for this frame 458 for (i = 0; i < INTER_MODE_CONTEXTS; i++) { 459 for (j = 0; j < INTER_MODES - 1; j++) { 460 mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0]; 461 mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1]; 462 } 463 } 464 465 // Write back the accumulated stats 466 f = fopen("mode_context.bin", "wb"); 467 fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); 468 fclose(f); 469 } 470 471 void print_mode_context(VP9_COMP *cpi) { 472 FILE *f = fopen("vp9_modecont.c", "a"); 473 int i, j; 474 475 fprintf(f, "#include \"vp9_entropy.h\"\n"); 476 fprintf( 477 f, 478 "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] ="); 479 fprintf(f, "{\n"); 480 for (j = 0; j < INTER_MODE_CONTEXTS; j++) { 481 fprintf(f, " {/* %d */ ", j); 482 fprintf(f, " "); 483 for (i = 0; i < INTER_MODES - 1; i++) { 484 int this_prob; 485 int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1]; 486 if (count) 487 this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count; 488 else 489 this_prob = 128; 490 491 // context probs 492 fprintf(f, "%5d, ", this_prob); 493 } 494 fprintf(f, " },\n"); 495 } 496 497 fprintf(f, "};\n"); 498 fclose(f); 499 } 500 #endif // ENTROPY_STATS 501 502 // DEBUG: Print out the segment id of each MB in the current frame. 503 static void print_seg_map(VP9_COMP *cpi) { 504 VP9_COMMON *cm = &cpi->common; 505 int row, col; 506 int map_index = 0; 507 FILE *statsfile = fopen("segmap.stt", "a"); 508 509 fprintf(statsfile, "%10d\n", cm->current_video_frame); 510 511 for (row = 0; row < cpi->common.mi_rows; row++) { 512 for (col = 0; col < cpi->common.mi_cols; col++) { 513 fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]); 514 map_index++; 515 } 516 fprintf(statsfile, "\n"); 517 } 518 fprintf(statsfile, "\n"); 519 520 fclose(statsfile); 521 } 522 523 static void update_reference_segmentation_map(VP9_COMP *cpi) { 524 VP9_COMMON *const cm = &cpi->common; 525 int row, col; 526 MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible; 527 uint8_t *cache_ptr = cm->last_frame_seg_map, *cache; 528 529 for (row = 0; row < cm->mi_rows; row++) { 530 mi_8x8 = mi_8x8_ptr; 531 cache = cache_ptr; 532 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++) 533 cache[0] = mi_8x8[0]->mbmi.segment_id; 534 mi_8x8_ptr += cm->mode_info_stride; 535 cache_ptr += cm->mi_cols; 536 } 537 } 538 539 static void set_default_lf_deltas(struct loopfilter *lf) { 540 lf->mode_ref_delta_enabled = 1; 541 lf->mode_ref_delta_update = 1; 542 543 vp9_zero(lf->ref_deltas); 544 vp9_zero(lf->mode_deltas); 545 546 // Test of ref frame deltas 547 lf->ref_deltas[INTRA_FRAME] = 2; 548 lf->ref_deltas[LAST_FRAME] = 0; 549 lf->ref_deltas[GOLDEN_FRAME] = -2; 550 lf->ref_deltas[ALTREF_FRAME] = -2; 551 552 lf->mode_deltas[0] = 0; // Zero 553 lf->mode_deltas[1] = 0; // New mv 554 } 555 556 static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) { 557 SPEED_FEATURES *sf = &cpi->sf; 558 int i; 559 560 // Set baseline threshold values 561 for (i = 0; i < MAX_MODES; ++i) 562 sf->thresh_mult[i] = mode == 0 ? -500 : 0; 563 564 sf->thresh_mult[THR_NEARESTMV] = 0; 565 sf->thresh_mult[THR_NEARESTG] = 0; 566 sf->thresh_mult[THR_NEARESTA] = 0; 567 568 sf->thresh_mult[THR_NEWMV] += 1000; 569 sf->thresh_mult[THR_COMP_NEARESTLA] += 1000; 570 sf->thresh_mult[THR_NEARMV] += 1000; 571 sf->thresh_mult[THR_COMP_NEARESTGA] += 1000; 572 573 sf->thresh_mult[THR_DC] += 1000; 574 575 sf->thresh_mult[THR_NEWG] += 1000; 576 sf->thresh_mult[THR_NEWA] += 1000; 577 sf->thresh_mult[THR_NEARA] += 1000; 578 579 sf->thresh_mult[THR_TM] += 1000; 580 581 sf->thresh_mult[THR_COMP_NEARLA] += 1500; 582 sf->thresh_mult[THR_COMP_NEWLA] += 2000; 583 sf->thresh_mult[THR_NEARG] += 1000; 584 sf->thresh_mult[THR_COMP_NEARGA] += 1500; 585 sf->thresh_mult[THR_COMP_NEWGA] += 2000; 586 587 sf->thresh_mult[THR_SPLITMV] += 2500; 588 sf->thresh_mult[THR_SPLITG] += 2500; 589 sf->thresh_mult[THR_SPLITA] += 2500; 590 sf->thresh_mult[THR_COMP_SPLITLA] += 4500; 591 sf->thresh_mult[THR_COMP_SPLITGA] += 4500; 592 593 sf->thresh_mult[THR_ZEROMV] += 2000; 594 sf->thresh_mult[THR_ZEROG] += 2000; 595 sf->thresh_mult[THR_ZEROA] += 2000; 596 sf->thresh_mult[THR_COMP_ZEROLA] += 2500; 597 sf->thresh_mult[THR_COMP_ZEROGA] += 2500; 598 599 sf->thresh_mult[THR_B_PRED] += 2500; 600 sf->thresh_mult[THR_H_PRED] += 2000; 601 sf->thresh_mult[THR_V_PRED] += 2000; 602 sf->thresh_mult[THR_D45_PRED ] += 2500; 603 sf->thresh_mult[THR_D135_PRED] += 2500; 604 sf->thresh_mult[THR_D117_PRED] += 2500; 605 sf->thresh_mult[THR_D153_PRED] += 2500; 606 sf->thresh_mult[THR_D207_PRED] += 2500; 607 sf->thresh_mult[THR_D63_PRED] += 2500; 608 609 if (cpi->sf.skip_lots_of_modes) { 610 for (i = 0; i < MAX_MODES; ++i) 611 sf->thresh_mult[i] = INT_MAX; 612 613 sf->thresh_mult[THR_DC] = 2000; 614 sf->thresh_mult[THR_TM] = 2000; 615 sf->thresh_mult[THR_NEWMV] = 4000; 616 sf->thresh_mult[THR_NEWG] = 4000; 617 sf->thresh_mult[THR_NEWA] = 4000; 618 sf->thresh_mult[THR_NEARESTMV] = 0; 619 sf->thresh_mult[THR_NEARESTG] = 0; 620 sf->thresh_mult[THR_NEARESTA] = 0; 621 sf->thresh_mult[THR_NEARMV] = 2000; 622 sf->thresh_mult[THR_NEARG] = 2000; 623 sf->thresh_mult[THR_NEARA] = 2000; 624 sf->thresh_mult[THR_COMP_NEARESTLA] = 2000; 625 sf->thresh_mult[THR_SPLITMV] = 2500; 626 sf->thresh_mult[THR_SPLITG] = 2500; 627 sf->thresh_mult[THR_SPLITA] = 2500; 628 sf->recode_loop = 0; 629 } 630 631 /* disable frame modes if flags not set */ 632 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) { 633 sf->thresh_mult[THR_NEWMV ] = INT_MAX; 634 sf->thresh_mult[THR_NEARESTMV] = INT_MAX; 635 sf->thresh_mult[THR_ZEROMV ] = INT_MAX; 636 sf->thresh_mult[THR_NEARMV ] = INT_MAX; 637 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 638 } 639 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) { 640 sf->thresh_mult[THR_NEARESTG ] = INT_MAX; 641 sf->thresh_mult[THR_ZEROG ] = INT_MAX; 642 sf->thresh_mult[THR_NEARG ] = INT_MAX; 643 sf->thresh_mult[THR_NEWG ] = INT_MAX; 644 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 645 } 646 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) { 647 sf->thresh_mult[THR_NEARESTA ] = INT_MAX; 648 sf->thresh_mult[THR_ZEROA ] = INT_MAX; 649 sf->thresh_mult[THR_NEARA ] = INT_MAX; 650 sf->thresh_mult[THR_NEWA ] = INT_MAX; 651 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 652 } 653 654 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != 655 (VP9_LAST_FLAG | VP9_ALT_FLAG)) { 656 sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX; 657 sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX; 658 sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX; 659 sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX; 660 sf->thresh_mult[THR_COMP_SPLITLA ] = INT_MAX; 661 } 662 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != 663 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) { 664 sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX; 665 sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX; 666 sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX; 667 sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX; 668 sf->thresh_mult[THR_COMP_SPLITGA ] = INT_MAX; 669 } 670 671 if (sf->disable_splitmv == 1) { 672 sf->thresh_mult[THR_SPLITMV ] = INT_MAX; 673 sf->thresh_mult[THR_SPLITG ] = INT_MAX; 674 sf->thresh_mult[THR_SPLITA ] = INT_MAX; 675 676 sf->thresh_mult[THR_COMP_SPLITLA ] = INT_MAX; 677 sf->thresh_mult[THR_COMP_SPLITGA ] = INT_MAX; 678 } 679 } 680 681 void vp9_set_speed_features(VP9_COMP *cpi) { 682 SPEED_FEATURES *sf = &cpi->sf; 683 int mode = cpi->compressor_speed; 684 int speed = cpi->speed; 685 int i; 686 687 // Only modes 0 and 1 supported for now in experimental code basae 688 if (mode > 1) 689 mode = 1; 690 691 // Initialise default mode frequency sampling variables 692 for (i = 0; i < MAX_MODES; i ++) { 693 cpi->mode_check_freq[i] = 0; 694 cpi->mode_test_hit_counts[i] = 0; 695 cpi->mode_chosen_counts[i] = 0; 696 } 697 698 // best quality defaults 699 sf->RD = 1; 700 sf->search_method = NSTEP; 701 sf->auto_filter = 1; 702 sf->recode_loop = 1; 703 sf->subpel_search_method = SUBPEL_TREE; 704 sf->subpel_iters_per_step = 2; 705 sf->optimize_coefficients = !cpi->oxcf.lossless; 706 sf->reduce_first_step_size = 0; 707 sf->auto_mv_step_size = 0; 708 sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 709 sf->comp_inter_joint_search_thresh = BLOCK_4X4; 710 sf->adaptive_rd_thresh = 0; 711 sf->use_lastframe_partitioning = 0; 712 sf->tx_size_search_method = USE_FULL_RD; 713 sf->use_lp32x32fdct = 0; 714 sf->adaptive_motion_search = 0; 715 sf->use_avoid_tested_higherror = 0; 716 sf->reference_masking = 0; 717 sf->skip_lots_of_modes = 0; 718 sf->partition_by_variance = 0; 719 sf->use_one_partition_size_always = 0; 720 sf->less_rectangular_check = 0; 721 sf->use_square_partition_only = 0; 722 sf->auto_min_max_partition_size = 0; 723 sf->auto_min_max_partition_interval = 0; 724 sf->auto_min_max_partition_count = 0; 725 sf->max_partition_size = BLOCK_64X64; 726 sf->min_partition_size = BLOCK_4X4; 727 sf->adjust_partitioning_from_last_frame = 0; 728 sf->last_partitioning_redo_frequency = 4; 729 sf->disable_splitmv = 0; 730 sf->mode_search_skip_flags = 0; 731 sf->disable_split_var_thresh = 0; 732 sf->disable_filter_search_var_thresh = 0; 733 sf->intra_y_mode_mask = ALL_INTRA_MODES; 734 sf->intra_uv_mode_mask = ALL_INTRA_MODES; 735 sf->use_rd_breakout = 0; 736 sf->skip_encode_sb = 0; 737 sf->use_uv_intra_rd_estimate = 0; 738 sf->use_fast_lpf_pick = 0; 739 sf->use_fast_coef_updates = 0; 740 sf->using_small_partition_info = 0; 741 sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set 742 743 #if CONFIG_MULTIPLE_ARF 744 // Switch segmentation off. 745 sf->static_segmentation = 0; 746 #else 747 sf->static_segmentation = 0; 748 #endif 749 750 switch (mode) { 751 case 0: // best quality mode 752 break; 753 754 case 1: 755 #if CONFIG_MULTIPLE_ARF 756 // Switch segmentation off. 757 sf->static_segmentation = 0; 758 #else 759 sf->static_segmentation = 0; 760 #endif 761 sf->use_avoid_tested_higherror = 1; 762 sf->adaptive_rd_thresh = MIN((speed + 1), 4); 763 764 if (speed == 1) { 765 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 766 sf->less_rectangular_check = 1; 767 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME || 768 cpi->common.intra_only || 769 cpi->common.show_frame == 0) ? 770 USE_FULL_RD : 771 USE_LARGESTALL); 772 sf->use_square_partition_only = !(cpi->common.frame_type == KEY_FRAME || 773 cpi->common.intra_only || 774 cpi->common.show_frame == 0); 775 sf->disable_splitmv = 776 (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0; 777 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 778 FLAG_SKIP_INTRA_BESTINTER | 779 FLAG_SKIP_COMP_BESTINTRA | 780 FLAG_SKIP_INTRA_LOWVAR; 781 sf->use_uv_intra_rd_estimate = 1; 782 sf->use_rd_breakout = 1; 783 sf->skip_encode_sb = 1; 784 sf->use_lp32x32fdct = 1; 785 sf->adaptive_motion_search = 1; 786 sf->auto_mv_step_size = 1; 787 788 sf->auto_min_max_partition_size = 1; 789 sf->auto_min_max_partition_interval = 1; 790 // FIXME(jingning): temporarily turn off disable_split_var_thresh 791 // during refactoring process. will get this back after finishing 792 // the main framework of partition search type. 793 sf->disable_split_var_thresh = 0; 794 sf->disable_filter_search_var_thresh = 16; 795 796 sf->intra_y_mode_mask = INTRA_DC_TM_H_V; 797 sf->intra_uv_mode_mask = INTRA_DC_TM_H_V; 798 sf->use_fast_coef_updates = 1; 799 sf->mode_skip_start = 9; 800 } 801 if (speed == 2) { 802 sf->less_rectangular_check = 1; 803 sf->use_square_partition_only = 1; 804 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 805 sf->use_lastframe_partitioning = 1; 806 sf->adjust_partitioning_from_last_frame = 1; 807 sf->last_partitioning_redo_frequency = 3; 808 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME || 809 cpi->common.intra_only || 810 cpi->common.show_frame == 0) ? 811 USE_FULL_RD : 812 USE_LARGESTALL); 813 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 814 FLAG_SKIP_INTRA_BESTINTER | 815 FLAG_SKIP_COMP_BESTINTRA | 816 FLAG_SKIP_COMP_REFMISMATCH | 817 FLAG_SKIP_INTRA_LOWVAR | 818 FLAG_EARLY_TERMINATE; 819 sf->intra_y_mode_mask = INTRA_DC_TM; 820 sf->intra_uv_mode_mask = INTRA_DC_TM; 821 sf->use_uv_intra_rd_estimate = 1; 822 sf->use_rd_breakout = 1; 823 sf->skip_encode_sb = 1; 824 sf->use_lp32x32fdct = 1; 825 sf->adaptive_motion_search = 1; 826 sf->using_small_partition_info = 0; 827 sf->disable_splitmv = 828 (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0; 829 sf->auto_mv_step_size = 1; 830 sf->search_method = SQUARE; 831 sf->subpel_iters_per_step = 1; 832 sf->use_fast_lpf_pick = 1; 833 sf->auto_min_max_partition_size = 1; 834 sf->auto_min_max_partition_interval = 2; 835 sf->disable_split_var_thresh = 32; 836 sf->disable_filter_search_var_thresh = 32; 837 sf->use_fast_coef_updates = 2; 838 sf->mode_skip_start = 9; 839 } 840 if (speed == 3) { 841 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 842 sf->partition_by_variance = 1; 843 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME || 844 cpi->common.intra_only || 845 cpi->common.show_frame == 0) ? 846 USE_FULL_RD : 847 USE_LARGESTALL); 848 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 849 FLAG_SKIP_INTRA_BESTINTER | 850 FLAG_SKIP_COMP_BESTINTRA | 851 FLAG_SKIP_COMP_REFMISMATCH | 852 FLAG_SKIP_INTRA_LOWVAR | 853 FLAG_EARLY_TERMINATE; 854 sf->use_rd_breakout = 1; 855 sf->skip_encode_sb = 1; 856 sf->use_lp32x32fdct = 1; 857 sf->disable_splitmv = 1; 858 sf->auto_mv_step_size = 1; 859 sf->search_method = BIGDIA; 860 sf->subpel_iters_per_step = 1; 861 sf->disable_split_var_thresh = 64; 862 sf->disable_filter_search_var_thresh = 64; 863 sf->intra_y_mode_mask = INTRA_DC_ONLY; 864 sf->intra_uv_mode_mask = INTRA_DC_ONLY; 865 sf->use_fast_coef_updates = 2; 866 sf->mode_skip_start = 9; 867 } 868 if (speed == 4) { 869 sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 870 sf->use_one_partition_size_always = 1; 871 sf->always_this_block_size = BLOCK_16X16; 872 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME || 873 cpi->common.intra_only || 874 cpi->common.show_frame == 0) ? 875 USE_FULL_RD : 876 USE_LARGESTALL); 877 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 878 FLAG_SKIP_INTRA_BESTINTER | 879 FLAG_SKIP_COMP_BESTINTRA | 880 FLAG_SKIP_COMP_REFMISMATCH | 881 FLAG_SKIP_INTRA_LOWVAR | 882 FLAG_EARLY_TERMINATE; 883 sf->use_rd_breakout = 1; 884 sf->use_lp32x32fdct = 1; 885 sf->optimize_coefficients = 0; 886 sf->auto_mv_step_size = 1; 887 // sf->reduce_first_step_size = 1; 888 // sf->reference_masking = 1; 889 890 sf->disable_splitmv = 1; 891 sf->search_method = HEX; 892 sf->subpel_iters_per_step = 1; 893 sf->disable_split_var_thresh = 64; 894 sf->disable_filter_search_var_thresh = 96; 895 sf->intra_y_mode_mask = INTRA_DC_ONLY; 896 sf->intra_uv_mode_mask = INTRA_DC_ONLY; 897 sf->use_fast_coef_updates = 2; 898 sf->mode_skip_start = 9; 899 } 900 break; 901 902 }; /* switch */ 903 904 // Set rd thresholds based on mode and speed setting 905 set_rd_speed_thresholds(cpi, mode); 906 907 // Slow quant, dct and trellis not worthwhile for first pass 908 // so make sure they are always turned off. 909 if (cpi->pass == 1) { 910 sf->optimize_coefficients = 0; 911 } 912 913 cpi->mb.fwd_txm16x16 = vp9_short_fdct16x16; 914 cpi->mb.fwd_txm8x8 = vp9_short_fdct8x8; 915 cpi->mb.fwd_txm8x4 = vp9_short_fdct8x4; 916 cpi->mb.fwd_txm4x4 = vp9_short_fdct4x4; 917 if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) { 918 cpi->mb.fwd_txm8x4 = vp9_short_walsh8x4; 919 cpi->mb.fwd_txm4x4 = vp9_short_walsh4x4; 920 } 921 922 cpi->mb.quantize_b_4x4 = vp9_regular_quantize_b_4x4; 923 924 if (cpi->sf.subpel_search_method == SUBPEL_ITERATIVE) { 925 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_iterative; 926 cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_iterative; 927 } else if (cpi->sf.subpel_search_method == SUBPEL_TREE) { 928 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree; 929 cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree; 930 } 931 932 cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1; 933 934 #ifdef SPEEDSTATS 935 frames_at_speed[cpi->speed]++; 936 #endif 937 } 938 939 static void alloc_raw_frame_buffers(VP9_COMP *cpi) { 940 VP9_COMMON *cm = &cpi->common; 941 942 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height, 943 cm->subsampling_x, cm->subsampling_y, 944 cpi->oxcf.lag_in_frames); 945 if (!cpi->lookahead) 946 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 947 "Failed to allocate lag buffers"); 948 949 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, 950 cpi->oxcf.width, cpi->oxcf.height, 951 cm->subsampling_x, cm->subsampling_y, 952 VP9BORDERINPIXELS)) 953 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 954 "Failed to allocate altref buffer"); 955 } 956 957 static int alloc_partition_data(VP9_COMP *cpi) { 958 vpx_free(cpi->mb.pip); 959 960 cpi->mb.pip = vpx_calloc(cpi->common.mode_info_stride * 961 (cpi->common.mi_rows + MI_BLOCK_SIZE), 962 sizeof(PARTITION_INFO)); 963 if (!cpi->mb.pip) 964 return 1; 965 966 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1; 967 968 return 0; 969 } 970 971 void vp9_alloc_compressor_data(VP9_COMP *cpi) { 972 VP9_COMMON *cm = &cpi->common; 973 974 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) 975 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 976 "Failed to allocate frame buffers"); 977 978 if (alloc_partition_data(cpi)) 979 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 980 "Failed to allocate partition data"); 981 982 if (vp9_alloc_frame_buffer(&cpi->last_frame_uf, 983 cm->width, cm->height, 984 cm->subsampling_x, cm->subsampling_y, 985 VP9BORDERINPIXELS)) 986 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 987 "Failed to allocate last frame buffer"); 988 989 if (vp9_alloc_frame_buffer(&cpi->scaled_source, 990 cm->width, cm->height, 991 cm->subsampling_x, cm->subsampling_y, 992 VP9BORDERINPIXELS)) 993 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 994 "Failed to allocate scaled source buffer"); 995 996 vpx_free(cpi->tok); 997 998 { 999 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); 1000 1001 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); 1002 } 1003 1004 // Data used for real time vc mode to see if gf needs refreshing 1005 cpi->inter_zz_count = 0; 1006 cpi->gf_bad_count = 0; 1007 cpi->gf_update_recommended = 0; 1008 1009 vpx_free(cpi->mb_activity_map); 1010 CHECK_MEM_ERROR(cm, cpi->mb_activity_map, 1011 vpx_calloc(sizeof(unsigned int), 1012 cm->mb_rows * cm->mb_cols)); 1013 1014 vpx_free(cpi->mb_norm_activity_map); 1015 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, 1016 vpx_calloc(sizeof(unsigned int), 1017 cm->mb_rows * cm->mb_cols)); 1018 } 1019 1020 1021 static void update_frame_size(VP9_COMP *cpi) { 1022 VP9_COMMON *cm = &cpi->common; 1023 1024 vp9_update_frame_size(cm); 1025 1026 // Update size of buffers local to this frame 1027 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, 1028 cm->width, cm->height, 1029 cm->subsampling_x, cm->subsampling_y, 1030 VP9BORDERINPIXELS)) 1031 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1032 "Failed to reallocate last frame buffer"); 1033 1034 if (vp9_realloc_frame_buffer(&cpi->scaled_source, 1035 cm->width, cm->height, 1036 cm->subsampling_x, cm->subsampling_y, 1037 VP9BORDERINPIXELS)) 1038 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1039 "Failed to reallocate scaled source buffer"); 1040 1041 { 1042 int y_stride = cpi->scaled_source.y_stride; 1043 1044 if (cpi->sf.search_method == NSTEP) { 1045 vp9_init3smotion_compensation(&cpi->mb, y_stride); 1046 } else if (cpi->sf.search_method == DIAMOND) { 1047 vp9_init_dsmotion_compensation(&cpi->mb, y_stride); 1048 } 1049 } 1050 } 1051 1052 1053 // TODO perhaps change number of steps expose to outside world when setting 1054 // max and min limits. Also this will likely want refining for the extended Q 1055 // range. 1056 // 1057 // Table that converts 0-63 Q range values passed in outside to the Qindex 1058 // range used internally. 1059 static const int q_trans[] = { 1060 0, 4, 8, 12, 16, 20, 24, 28, 1061 32, 36, 40, 44, 48, 52, 56, 60, 1062 64, 68, 72, 76, 80, 84, 88, 92, 1063 96, 100, 104, 108, 112, 116, 120, 124, 1064 128, 132, 136, 140, 144, 148, 152, 156, 1065 160, 164, 168, 172, 176, 180, 184, 188, 1066 192, 196, 200, 204, 208, 212, 216, 220, 1067 224, 228, 232, 236, 240, 244, 249, 255, 1068 }; 1069 1070 int vp9_reverse_trans(int x) { 1071 int i; 1072 1073 for (i = 0; i < 64; i++) 1074 if (q_trans[i] >= x) 1075 return i; 1076 1077 return 63; 1078 }; 1079 void vp9_new_framerate(VP9_COMP *cpi, double framerate) { 1080 if (framerate < 0.1) 1081 framerate = 30; 1082 1083 cpi->oxcf.framerate = framerate; 1084 cpi->output_framerate = cpi->oxcf.framerate; 1085 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate); 1086 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate); 1087 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100); 1088 1089 1090 cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS); 1091 1092 // Set Maximum gf/arf interval 1093 cpi->max_gf_interval = 16; 1094 1095 // Extended interval for genuinely static scenes 1096 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; 1097 1098 // Special conditions when alt ref frame enabled in lagged compress mode 1099 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) { 1100 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1101 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1102 1103 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1104 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1105 } 1106 1107 if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) 1108 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval; 1109 } 1110 1111 static int64_t rescale(int val, int64_t num, int denom) { 1112 int64_t llnum = num; 1113 int64_t llden = denom; 1114 int64_t llval = val; 1115 1116 return (llval * llnum / llden); 1117 } 1118 1119 static void set_tile_limits(VP9_COMP *cpi) { 1120 VP9_COMMON *const cm = &cpi->common; 1121 1122 int min_log2_tile_cols, max_log2_tile_cols; 1123 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); 1124 1125 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, 1126 min_log2_tile_cols, max_log2_tile_cols); 1127 cm->log2_tile_rows = cpi->oxcf.tile_rows; 1128 } 1129 1130 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { 1131 VP9_COMP *cpi = (VP9_COMP *)(ptr); 1132 VP9_COMMON *const cm = &cpi->common; 1133 int i; 1134 1135 cpi->oxcf = *oxcf; 1136 cpi->goldfreq = 7; 1137 1138 cm->version = oxcf->version; 1139 1140 cm->width = oxcf->width; 1141 cm->height = oxcf->height; 1142 cm->subsampling_x = 0; 1143 cm->subsampling_y = 0; 1144 vp9_alloc_compressor_data(cpi); 1145 1146 // change includes all joint functionality 1147 vp9_change_config(ptr, oxcf); 1148 1149 // Initialize active best and worst q and average q values. 1150 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1151 cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1152 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1153 1154 // Initialise the starting buffer levels 1155 cpi->buffer_level = cpi->oxcf.starting_buffer_level; 1156 cpi->bits_off_target = cpi->oxcf.starting_buffer_level; 1157 1158 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; 1159 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; 1160 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; 1161 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; 1162 1163 cpi->total_actual_bits = 0; 1164 cpi->total_target_vs_actual = 0; 1165 1166 cpi->static_mb_pct = 0; 1167 1168 cpi->lst_fb_idx = 0; 1169 cpi->gld_fb_idx = 1; 1170 cpi->alt_fb_idx = 2; 1171 1172 cpi->current_layer = 0; 1173 cpi->use_svc = 0; 1174 1175 set_tile_limits(cpi); 1176 1177 cpi->fixed_divide[0] = 0; 1178 for (i = 1; i < 512; i++) 1179 cpi->fixed_divide[i] = 0x80000 / i; 1180 } 1181 1182 1183 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { 1184 VP9_COMP *cpi = (VP9_COMP *)(ptr); 1185 VP9_COMMON *const cm = &cpi->common; 1186 1187 if (!cpi || !oxcf) 1188 return; 1189 1190 if (cm->version != oxcf->version) { 1191 cm->version = oxcf->version; 1192 } 1193 1194 cpi->oxcf = *oxcf; 1195 1196 switch (cpi->oxcf.Mode) { 1197 // Real time and one pass deprecated in test code base 1198 case MODE_FIRSTPASS: 1199 cpi->pass = 1; 1200 cpi->compressor_speed = 1; 1201 break; 1202 1203 case MODE_SECONDPASS: 1204 cpi->pass = 2; 1205 cpi->compressor_speed = 1; 1206 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); 1207 break; 1208 1209 case MODE_SECONDPASS_BEST: 1210 cpi->pass = 2; 1211 cpi->compressor_speed = 0; 1212 break; 1213 } 1214 1215 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; 1216 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; 1217 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; 1218 1219 cpi->oxcf.lossless = oxcf->lossless; 1220 if (cpi->oxcf.lossless) { 1221 cpi->mb.e_mbd.inv_txm4x4_1_add = vp9_short_iwalsh4x4_1_add; 1222 cpi->mb.e_mbd.inv_txm4x4_add = vp9_short_iwalsh4x4_add; 1223 } else { 1224 cpi->mb.e_mbd.inv_txm4x4_1_add = vp9_short_idct4x4_1_add; 1225 cpi->mb.e_mbd.inv_txm4x4_add = vp9_short_idct4x4_add; 1226 } 1227 1228 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 1229 1230 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 1231 1232 // cpi->use_golden_frame_only = 0; 1233 // cpi->use_last_frame_only = 0; 1234 cpi->refresh_golden_frame = 0; 1235 cpi->refresh_last_frame = 1; 1236 cm->refresh_frame_context = 1; 1237 cm->reset_frame_context = 0; 1238 1239 setup_features(cm); 1240 cpi->mb.e_mbd.allow_high_precision_mv = 0; // Default mv precision adaptation 1241 set_mvcost(&cpi->mb); 1242 1243 { 1244 int i; 1245 1246 for (i = 0; i < MAX_SEGMENTS; i++) 1247 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 1248 } 1249 1250 // At the moment the first order values may not be > MAXQ 1251 cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ); 1252 1253 // local file playback mode == really big buffer 1254 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) { 1255 cpi->oxcf.starting_buffer_level = 60000; 1256 cpi->oxcf.optimal_buffer_level = 60000; 1257 cpi->oxcf.maximum_buffer_size = 240000; 1258 } 1259 1260 // Convert target bandwidth from Kbit/s to Bit/s 1261 cpi->oxcf.target_bandwidth *= 1000; 1262 1263 cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level, 1264 cpi->oxcf.target_bandwidth, 1000); 1265 1266 // Set or reset optimal and maximum buffer levels. 1267 if (cpi->oxcf.optimal_buffer_level == 0) 1268 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 1269 else 1270 cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level, 1271 cpi->oxcf.target_bandwidth, 1000); 1272 1273 if (cpi->oxcf.maximum_buffer_size == 0) 1274 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 1275 else 1276 cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size, 1277 cpi->oxcf.target_bandwidth, 1000); 1278 1279 // Set up frame rate and related parameters rate control values. 1280 vp9_new_framerate(cpi, cpi->oxcf.framerate); 1281 1282 // Set absolute upper and lower quality limits 1283 cpi->worst_quality = cpi->oxcf.worst_allowed_q; 1284 cpi->best_quality = cpi->oxcf.best_allowed_q; 1285 1286 // active values should only be modified if out of new range 1287 cpi->active_worst_quality = clamp(cpi->active_worst_quality, 1288 cpi->oxcf.best_allowed_q, 1289 cpi->oxcf.worst_allowed_q); 1290 1291 cpi->active_best_quality = clamp(cpi->active_best_quality, 1292 cpi->oxcf.best_allowed_q, 1293 cpi->oxcf.worst_allowed_q); 1294 1295 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0; 1296 1297 cpi->cq_target_quality = cpi->oxcf.cq_level; 1298 1299 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; 1300 1301 cpi->target_bandwidth = cpi->oxcf.target_bandwidth; 1302 1303 cm->display_width = cpi->oxcf.width; 1304 cm->display_height = cpi->oxcf.height; 1305 1306 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) 1307 cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness); 1308 1309 cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness; 1310 1311 if (cpi->initial_width) { 1312 // Increasing the size of the frame beyond the first seen frame, or some 1313 // otherwise signalled maximum size, is not supported. 1314 // TODO(jkoleszar): exit gracefully. 1315 assert(cm->width <= cpi->initial_width); 1316 assert(cm->height <= cpi->initial_height); 1317 } 1318 update_frame_size(cpi); 1319 1320 if (cpi->oxcf.fixed_q >= 0) { 1321 cpi->last_q[0] = cpi->oxcf.fixed_q; 1322 cpi->last_q[1] = cpi->oxcf.fixed_q; 1323 cpi->last_boosted_qindex = cpi->oxcf.fixed_q; 1324 } 1325 1326 cpi->speed = cpi->oxcf.cpu_used; 1327 1328 if (cpi->oxcf.lag_in_frames == 0) { 1329 // force to allowlag to 0 if lag_in_frames is 0; 1330 cpi->oxcf.allow_lag = 0; 1331 } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) { 1332 // Limit on lag buffers as these are not currently dynamically allocated 1333 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 1334 } 1335 1336 // YX Temp 1337 #if CONFIG_MULTIPLE_ARF 1338 vp9_zero(cpi->alt_ref_source); 1339 #else 1340 cpi->alt_ref_source = NULL; 1341 #endif 1342 cpi->is_src_frame_alt_ref = 0; 1343 1344 #if 0 1345 // Experimental RD Code 1346 cpi->frame_distortion = 0; 1347 cpi->last_frame_distortion = 0; 1348 #endif 1349 1350 set_tile_limits(cpi); 1351 } 1352 1353 #define M_LOG2_E 0.693147180559945309417 1354 #define log2f(x) (log (x) / (float) M_LOG2_E) 1355 1356 static void cal_nmvjointsadcost(int *mvjointsadcost) { 1357 mvjointsadcost[0] = 600; 1358 mvjointsadcost[1] = 300; 1359 mvjointsadcost[2] = 300; 1360 mvjointsadcost[0] = 300; 1361 } 1362 1363 static void cal_nmvsadcosts(int *mvsadcost[2]) { 1364 int i = 1; 1365 1366 mvsadcost[0][0] = 0; 1367 mvsadcost[1][0] = 0; 1368 1369 do { 1370 double z = 256 * (2 * (log2f(8 * i) + .6)); 1371 mvsadcost[0][i] = (int)z; 1372 mvsadcost[1][i] = (int)z; 1373 mvsadcost[0][-i] = (int)z; 1374 mvsadcost[1][-i] = (int)z; 1375 } while (++i <= MV_MAX); 1376 } 1377 1378 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) { 1379 int i = 1; 1380 1381 mvsadcost[0][0] = 0; 1382 mvsadcost[1][0] = 0; 1383 1384 do { 1385 double z = 256 * (2 * (log2f(8 * i) + .6)); 1386 mvsadcost[0][i] = (int)z; 1387 mvsadcost[1][i] = (int)z; 1388 mvsadcost[0][-i] = (int)z; 1389 mvsadcost[1][-i] = (int)z; 1390 } while (++i <= MV_MAX); 1391 } 1392 1393 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { 1394 int i, j; 1395 volatile union { 1396 VP9_COMP *cpi; 1397 VP9_PTR ptr; 1398 } ctx; 1399 1400 VP9_COMP *cpi; 1401 VP9_COMMON *cm; 1402 1403 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP)); 1404 // Check that the CPI instance is valid 1405 if (!cpi) 1406 return 0; 1407 1408 cm = &cpi->common; 1409 1410 vp9_zero(*cpi); 1411 1412 if (setjmp(cm->error.jmp)) { 1413 VP9_PTR ptr = ctx.ptr; 1414 1415 ctx.cpi->common.error.setjmp = 0; 1416 vp9_remove_compressor(&ptr); 1417 return 0; 1418 } 1419 1420 cm->error.setjmp = 1; 1421 1422 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), 1423 (MAX_MVSEARCH_STEPS * 8) + 1)); 1424 1425 vp9_create_common(cm); 1426 1427 init_config((VP9_PTR)cpi, oxcf); 1428 1429 cm->current_video_frame = 0; 1430 cpi->kf_overspend_bits = 0; 1431 cpi->kf_bitrate_adjustment = 0; 1432 cpi->frames_till_gf_update_due = 0; 1433 cpi->gf_overspend_bits = 0; 1434 cpi->non_gf_bitrate_adjustment = 0; 1435 1436 // Set reference frame sign bias for ALTREF frame to 1 (for now) 1437 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; 1438 1439 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 1440 1441 cpi->gold_is_last = 0; 1442 cpi->alt_is_last = 0; 1443 cpi->gold_is_alt = 0; 1444 1445 // Spatial scalability 1446 cpi->number_spatial_layers = oxcf->ss_number_layers; 1447 1448 // Create the encoder segmentation map and set all entries to 0 1449 CHECK_MEM_ERROR(cm, cpi->segmentation_map, 1450 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1451 1452 // And a place holder structure is the coding context 1453 // for use if we want to save and restore it 1454 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, 1455 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1456 1457 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); 1458 vpx_memset(cpi->active_map, 1, cm->MBs); 1459 cpi->active_map_enabled = 0; 1460 1461 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / 1462 sizeof(cpi->mbgraph_stats[0])); i++) { 1463 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats, 1464 vpx_calloc(cm->MBs * 1465 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1)); 1466 } 1467 1468 #ifdef ENTROPY_STATS 1469 if (cpi->pass != 1) 1470 init_context_counters(); 1471 #endif 1472 1473 #ifdef MODE_STATS 1474 init_tx_count_stats(); 1475 init_switchable_interp_stats(); 1476 #endif 1477 1478 /*Initialize the feed-forward activity masking.*/ 1479 cpi->activity_avg = 90 << 12; 1480 1481 cpi->frames_since_key = 8; // Give a sensible default for the first frame. 1482 cpi->key_frame_frequency = cpi->oxcf.key_freq; 1483 cpi->this_key_frame_forced = 0; 1484 cpi->next_key_frame_forced = 0; 1485 1486 cpi->source_alt_ref_pending = 0; 1487 cpi->source_alt_ref_active = 0; 1488 cpi->refresh_alt_ref_frame = 0; 1489 1490 #if CONFIG_MULTIPLE_ARF 1491 // Turn multiple ARF usage on/off. This is a quick hack for the initial test 1492 // version. It should eventually be set via the codec API. 1493 cpi->multi_arf_enabled = 1; 1494 1495 if (cpi->multi_arf_enabled) { 1496 cpi->sequence_number = 0; 1497 cpi->frame_coding_order_period = 0; 1498 vp9_zero(cpi->frame_coding_order); 1499 vp9_zero(cpi->arf_buffer_idx); 1500 } 1501 #endif 1502 1503 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS; 1504 #if CONFIG_INTERNAL_STATS 1505 cpi->b_calculate_ssimg = 0; 1506 1507 cpi->count = 0; 1508 cpi->bytes = 0; 1509 1510 if (cpi->b_calculate_psnr) { 1511 cpi->total_sq_error = 0.0; 1512 cpi->total_sq_error2 = 0.0; 1513 cpi->total_y = 0.0; 1514 cpi->total_u = 0.0; 1515 cpi->total_v = 0.0; 1516 cpi->total = 0.0; 1517 cpi->totalp_y = 0.0; 1518 cpi->totalp_u = 0.0; 1519 cpi->totalp_v = 0.0; 1520 cpi->totalp = 0.0; 1521 cpi->tot_recode_hits = 0; 1522 cpi->summed_quality = 0; 1523 cpi->summed_weights = 0; 1524 cpi->summedp_quality = 0; 1525 cpi->summedp_weights = 0; 1526 } 1527 1528 if (cpi->b_calculate_ssimg) { 1529 cpi->total_ssimg_y = 0; 1530 cpi->total_ssimg_u = 0; 1531 cpi->total_ssimg_v = 0; 1532 cpi->total_ssimg_all = 0; 1533 } 1534 1535 #endif 1536 1537 cpi->first_time_stamp_ever = INT64_MAX; 1538 1539 cpi->frames_till_gf_update_due = 0; 1540 cpi->key_frame_count = 1; 1541 1542 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; 1543 cpi->ni_tot_qi = 0; 1544 cpi->ni_frames = 0; 1545 cpi->tot_q = 0.0; 1546 cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q); 1547 cpi->total_byte_count = 0; 1548 1549 cpi->rate_correction_factor = 1.0; 1550 cpi->key_frame_rate_correction_factor = 1.0; 1551 cpi->gf_rate_correction_factor = 1.0; 1552 cpi->twopass.est_max_qcorrection_factor = 1.0; 1553 1554 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); 1555 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; 1556 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; 1557 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; 1558 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; 1559 cal_nmvsadcosts(cpi->mb.nmvsadcost); 1560 1561 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; 1562 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; 1563 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; 1564 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; 1565 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); 1566 1567 for (i = 0; i < KEY_FRAME_CONTEXT; i++) 1568 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate; 1569 1570 #ifdef OUTPUT_YUV_SRC 1571 yuv_file = fopen("bd.yuv", "ab"); 1572 #endif 1573 #ifdef OUTPUT_YUV_REC 1574 yuv_rec_file = fopen("rec.yuv", "wb"); 1575 #endif 1576 1577 #if 0 1578 framepsnr = fopen("framepsnr.stt", "a"); 1579 kf_list = fopen("kf_list.stt", "w"); 1580 #endif 1581 1582 cpi->output_pkt_list = oxcf->output_pkt_list; 1583 1584 cpi->enable_encode_breakout = 1; 1585 1586 if (cpi->pass == 1) { 1587 vp9_init_first_pass(cpi); 1588 } else if (cpi->pass == 2) { 1589 size_t packet_sz = sizeof(FIRSTPASS_STATS); 1590 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz); 1591 1592 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf; 1593 cpi->twopass.stats_in = cpi->twopass.stats_in_start; 1594 cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in 1595 + (packets - 1) * packet_sz); 1596 vp9_init_second_pass(cpi); 1597 } 1598 1599 vp9_set_speed_features(cpi); 1600 1601 // Default rd threshold factors for mode selection 1602 for (i = 0; i < BLOCK_SIZES; ++i) 1603 for (j = 0; j < MAX_MODES; ++j) 1604 cpi->rd_thresh_freq_fact[i][j] = 32; 1605 1606 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \ 1607 SDX3F, SDX8F, SDX4DF)\ 1608 cpi->fn_ptr[BT].sdf = SDF; \ 1609 cpi->fn_ptr[BT].sdaf = SDAF; \ 1610 cpi->fn_ptr[BT].vf = VF; \ 1611 cpi->fn_ptr[BT].svf = SVF; \ 1612 cpi->fn_ptr[BT].svaf = SVAF; \ 1613 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \ 1614 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \ 1615 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \ 1616 cpi->fn_ptr[BT].sdx3f = SDX3F; \ 1617 cpi->fn_ptr[BT].sdx8f = SDX8F; \ 1618 cpi->fn_ptr[BT].sdx4df = SDX4DF; 1619 1620 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg, 1621 vp9_variance32x16, vp9_sub_pixel_variance32x16, 1622 vp9_sub_pixel_avg_variance32x16, NULL, NULL, 1623 NULL, NULL, NULL, 1624 vp9_sad32x16x4d) 1625 1626 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg, 1627 vp9_variance16x32, vp9_sub_pixel_variance16x32, 1628 vp9_sub_pixel_avg_variance16x32, NULL, NULL, 1629 NULL, NULL, NULL, 1630 vp9_sad16x32x4d) 1631 1632 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg, 1633 vp9_variance64x32, vp9_sub_pixel_variance64x32, 1634 vp9_sub_pixel_avg_variance64x32, NULL, NULL, 1635 NULL, NULL, NULL, 1636 vp9_sad64x32x4d) 1637 1638 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg, 1639 vp9_variance32x64, vp9_sub_pixel_variance32x64, 1640 vp9_sub_pixel_avg_variance32x64, NULL, NULL, 1641 NULL, NULL, NULL, 1642 vp9_sad32x64x4d) 1643 1644 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg, 1645 vp9_variance32x32, vp9_sub_pixel_variance32x32, 1646 vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h, 1647 vp9_variance_halfpixvar32x32_v, 1648 vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8, 1649 vp9_sad32x32x4d) 1650 1651 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg, 1652 vp9_variance64x64, vp9_sub_pixel_variance64x64, 1653 vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h, 1654 vp9_variance_halfpixvar64x64_v, 1655 vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8, 1656 vp9_sad64x64x4d) 1657 1658 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg, 1659 vp9_variance16x16, vp9_sub_pixel_variance16x16, 1660 vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h, 1661 vp9_variance_halfpixvar16x16_v, 1662 vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8, 1663 vp9_sad16x16x4d) 1664 1665 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg, 1666 vp9_variance16x8, vp9_sub_pixel_variance16x8, 1667 vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL, 1668 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d) 1669 1670 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg, 1671 vp9_variance8x16, vp9_sub_pixel_variance8x16, 1672 vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL, 1673 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d) 1674 1675 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg, 1676 vp9_variance8x8, vp9_sub_pixel_variance8x8, 1677 vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL, 1678 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d) 1679 1680 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg, 1681 vp9_variance8x4, vp9_sub_pixel_variance8x4, 1682 vp9_sub_pixel_avg_variance8x4, NULL, NULL, 1683 NULL, NULL, vp9_sad8x4x8, 1684 vp9_sad8x4x4d) 1685 1686 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg, 1687 vp9_variance4x8, vp9_sub_pixel_variance4x8, 1688 vp9_sub_pixel_avg_variance4x8, NULL, NULL, 1689 NULL, NULL, vp9_sad4x8x8, 1690 vp9_sad4x8x4d) 1691 1692 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg, 1693 vp9_variance4x4, vp9_sub_pixel_variance4x4, 1694 vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL, 1695 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d) 1696 1697 cpi->full_search_sad = vp9_full_search_sad; 1698 cpi->diamond_search_sad = vp9_diamond_search_sad; 1699 cpi->refining_search_sad = vp9_refining_search_sad; 1700 1701 // make sure frame 1 is okay 1702 cpi->error_bins[0] = cpi->common.MBs; 1703 1704 /* vp9_init_quantizer() is first called here. Add check in 1705 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only 1706 * called later when needed. This will avoid unnecessary calls of 1707 * vp9_init_quantizer() for every frame. 1708 */ 1709 vp9_init_quantizer(cpi); 1710 1711 vp9_loop_filter_init(cm); 1712 1713 cpi->common.error.setjmp = 0; 1714 1715 vp9_zero(cpi->y_uv_mode_count) 1716 1717 #ifdef MODE_TEST_HIT_STATS 1718 vp9_zero(cpi->mode_test_hits) 1719 #endif 1720 1721 return (VP9_PTR) cpi; 1722 } 1723 1724 void vp9_remove_compressor(VP9_PTR *ptr) { 1725 VP9_COMP *cpi = (VP9_COMP *)(*ptr); 1726 int i; 1727 1728 if (!cpi) 1729 return; 1730 1731 if (cpi && (cpi->common.current_video_frame > 0)) { 1732 if (cpi->pass == 2) { 1733 vp9_end_second_pass(cpi); 1734 } 1735 1736 #ifdef ENTROPY_STATS 1737 if (cpi->pass != 1) { 1738 print_context_counters(); 1739 print_tree_update_probs(); 1740 print_mode_context(cpi); 1741 } 1742 #endif 1743 1744 #ifdef MODE_STATS 1745 if (cpi->pass != 1) { 1746 write_tx_count_stats(); 1747 write_switchable_interp_stats(); 1748 } 1749 #endif 1750 1751 #if CONFIG_INTERNAL_STATS 1752 1753 vp9_clear_system_state(); 1754 1755 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count); 1756 if (cpi->pass != 1) { 1757 FILE *f = fopen("opsnr.stt", "a"); 1758 double time_encoded = (cpi->last_end_time_stamp_seen 1759 - cpi->first_time_stamp_ever) / 10000000.000; 1760 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000; 1761 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded; 1762 1763 if (cpi->b_calculate_psnr) { 1764 YV12_BUFFER_CONFIG *lst_yv12 = 1765 &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]]; 1766 double samples = 3.0 / 2 * cpi->count * 1767 lst_yv12->y_width * lst_yv12->y_height; 1768 double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error); 1769 double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2); 1770 double total_ssim = 100 * pow(cpi->summed_quality / 1771 cpi->summed_weights, 8.0); 1772 double total_ssimp = 100 * pow(cpi->summedp_quality / 1773 cpi->summedp_weights, 8.0); 1774 1775 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t" 1776 "VPXSSIM\tVPSSIMP\t Time(ms)\n"); 1777 fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n", 1778 dr, cpi->total / cpi->count, total_psnr, 1779 cpi->totalp / cpi->count, total_psnr2, total_ssim, total_ssimp, 1780 total_encode_time); 1781 // fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f %10ld\n", 1782 // dr, cpi->total / cpi->count, total_psnr, 1783 // cpi->totalp / cpi->count, total_psnr2, total_ssim, 1784 // total_encode_time, cpi->tot_recode_hits); 1785 } 1786 1787 if (cpi->b_calculate_ssimg) { 1788 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n"); 1789 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, 1790 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count, 1791 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time); 1792 // fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f %10ld\n", dr, 1793 // cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count, 1794 // cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time, cpi->tot_recode_hits); 1795 } 1796 1797 fclose(f); 1798 } 1799 1800 #endif 1801 1802 #ifdef MODE_TEST_HIT_STATS 1803 if (cpi->pass != 1) { 1804 double norm_per_pixel_mode_tests = 0; 1805 double norm_counts[BLOCK_SIZES]; 1806 int i; 1807 int sb64_per_frame; 1808 int norm_factors[BLOCK_SIZES] = 1809 {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1}; 1810 FILE *f = fopen("mode_hit_stats.stt", "a"); 1811 1812 // On average, how many mode tests do we do 1813 for (i = 0; i < BLOCK_SIZES; ++i) { 1814 norm_counts[i] = (double)cpi->mode_test_hits[i] / 1815 (double)norm_factors[i]; 1816 norm_per_pixel_mode_tests += norm_counts[i]; 1817 } 1818 // Convert to a number per 64x64 and per frame 1819 sb64_per_frame = ((cpi->common.height + 63) / 64) * 1820 ((cpi->common.width + 63) / 64); 1821 norm_per_pixel_mode_tests = 1822 norm_per_pixel_mode_tests / 1823 (double)(cpi->common.current_video_frame * sb64_per_frame); 1824 1825 fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests); 1826 fclose(f); 1827 } 1828 #endif 1829 1830 #ifdef ENTROPY_STATS 1831 { 1832 int i, j, k; 1833 FILE *fmode = fopen("vp9_modecontext.c", "w"); 1834 1835 fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n"); 1836 fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts "); 1837 fprintf(fmode, "[INTRA_MODES][INTRA_MODES]" 1838 "[INTRA_MODES] =\n{\n"); 1839 1840 for (i = 0; i < INTRA_MODES; i++) { 1841 1842 fprintf(fmode, " { // Above Mode : %d\n", i); 1843 1844 for (j = 0; j < INTRA_MODES; j++) { 1845 1846 fprintf(fmode, " {"); 1847 1848 for (k = 0; k < INTRA_MODES; k++) { 1849 if (!intra_mode_stats[i][j][k]) 1850 fprintf(fmode, " %5d, ", 1); 1851 else 1852 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]); 1853 } 1854 1855 fprintf(fmode, "}, // left_mode %d\n", j); 1856 1857 } 1858 1859 fprintf(fmode, " },\n"); 1860 1861 } 1862 1863 fprintf(fmode, "};\n"); 1864 fclose(fmode); 1865 } 1866 #endif 1867 1868 1869 #if defined(SECTIONBITS_OUTPUT) 1870 1871 if (0) { 1872 int i; 1873 FILE *f = fopen("tokenbits.stt", "a"); 1874 1875 for (i = 0; i < 28; i++) 1876 fprintf(f, "%8d", (int)(Sectionbits[i] / 256)); 1877 1878 fprintf(f, "\n"); 1879 fclose(f); 1880 } 1881 1882 #endif 1883 1884 #if 0 1885 { 1886 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); 1887 printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); 1888 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, 1889 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000, 1890 cpi->time_compress_data / 1000, 1891 (cpi->time_receive_data + cpi->time_compress_data) / 1000); 1892 } 1893 #endif 1894 1895 } 1896 1897 dealloc_compressor_data(cpi); 1898 vpx_free(cpi->mb.ss); 1899 vpx_free(cpi->tok); 1900 1901 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]); i++) { 1902 vpx_free(cpi->mbgraph_stats[i].mb_stats); 1903 } 1904 1905 vp9_remove_common(&cpi->common); 1906 vpx_free(cpi); 1907 *ptr = 0; 1908 1909 #ifdef OUTPUT_YUV_SRC 1910 fclose(yuv_file); 1911 #endif 1912 #ifdef OUTPUT_YUV_REC 1913 fclose(yuv_rec_file); 1914 #endif 1915 1916 #if 0 1917 1918 if (keyfile) 1919 fclose(keyfile); 1920 1921 if (framepsnr) 1922 fclose(framepsnr); 1923 1924 if (kf_list) 1925 fclose(kf_list); 1926 1927 #endif 1928 1929 } 1930 1931 1932 static uint64_t calc_plane_error(uint8_t *orig, int orig_stride, 1933 uint8_t *recon, int recon_stride, 1934 unsigned int cols, unsigned int rows) { 1935 unsigned int row, col; 1936 uint64_t total_sse = 0; 1937 int diff; 1938 1939 for (row = 0; row + 16 <= rows; row += 16) { 1940 for (col = 0; col + 16 <= cols; col += 16) { 1941 unsigned int sse; 1942 1943 vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse); 1944 total_sse += sse; 1945 } 1946 1947 /* Handle odd-sized width */ 1948 if (col < cols) { 1949 unsigned int border_row, border_col; 1950 uint8_t *border_orig = orig; 1951 uint8_t *border_recon = recon; 1952 1953 for (border_row = 0; border_row < 16; border_row++) { 1954 for (border_col = col; border_col < cols; border_col++) { 1955 diff = border_orig[border_col] - border_recon[border_col]; 1956 total_sse += diff * diff; 1957 } 1958 1959 border_orig += orig_stride; 1960 border_recon += recon_stride; 1961 } 1962 } 1963 1964 orig += orig_stride * 16; 1965 recon += recon_stride * 16; 1966 } 1967 1968 /* Handle odd-sized height */ 1969 for (; row < rows; row++) { 1970 for (col = 0; col < cols; col++) { 1971 diff = orig[col] - recon[col]; 1972 total_sse += diff * diff; 1973 } 1974 1975 orig += orig_stride; 1976 recon += recon_stride; 1977 } 1978 1979 return total_sse; 1980 } 1981 1982 1983 static void generate_psnr_packet(VP9_COMP *cpi) { 1984 YV12_BUFFER_CONFIG *orig = cpi->Source; 1985 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 1986 struct vpx_codec_cx_pkt pkt; 1987 uint64_t sse; 1988 int i; 1989 unsigned int width = orig->y_crop_width; 1990 unsigned int height = orig->y_crop_height; 1991 1992 pkt.kind = VPX_CODEC_PSNR_PKT; 1993 sse = calc_plane_error(orig->y_buffer, orig->y_stride, 1994 recon->y_buffer, recon->y_stride, 1995 width, height); 1996 pkt.data.psnr.sse[0] = sse; 1997 pkt.data.psnr.sse[1] = sse; 1998 pkt.data.psnr.samples[0] = width * height; 1999 pkt.data.psnr.samples[1] = width * height; 2000 2001 width = orig->uv_crop_width; 2002 height = orig->uv_crop_height; 2003 2004 sse = calc_plane_error(orig->u_buffer, orig->uv_stride, 2005 recon->u_buffer, recon->uv_stride, 2006 width, height); 2007 pkt.data.psnr.sse[0] += sse; 2008 pkt.data.psnr.sse[2] = sse; 2009 pkt.data.psnr.samples[0] += width * height; 2010 pkt.data.psnr.samples[2] = width * height; 2011 2012 sse = calc_plane_error(orig->v_buffer, orig->uv_stride, 2013 recon->v_buffer, recon->uv_stride, 2014 width, height); 2015 pkt.data.psnr.sse[0] += sse; 2016 pkt.data.psnr.sse[3] = sse; 2017 pkt.data.psnr.samples[0] += width * height; 2018 pkt.data.psnr.samples[3] = width * height; 2019 2020 for (i = 0; i < 4; i++) 2021 pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0, 2022 (double)pkt.data.psnr.sse[i]); 2023 2024 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); 2025 } 2026 2027 2028 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) { 2029 VP9_COMP *cpi = (VP9_COMP *)(ptr); 2030 2031 if (ref_frame_flags > 7) 2032 return -1; 2033 2034 cpi->ref_frame_flags = ref_frame_flags; 2035 return 0; 2036 } 2037 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) { 2038 VP9_COMP *cpi = (VP9_COMP *)(ptr); 2039 2040 if (ref_frame_flags > 7) 2041 return -1; 2042 2043 cpi->refresh_golden_frame = 0; 2044 cpi->refresh_alt_ref_frame = 0; 2045 cpi->refresh_last_frame = 0; 2046 2047 if (ref_frame_flags & VP9_LAST_FLAG) 2048 cpi->refresh_last_frame = 1; 2049 2050 if (ref_frame_flags & VP9_GOLD_FLAG) 2051 cpi->refresh_golden_frame = 1; 2052 2053 if (ref_frame_flags & VP9_ALT_FLAG) 2054 cpi->refresh_alt_ref_frame = 1; 2055 2056 return 0; 2057 } 2058 2059 int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, 2060 YV12_BUFFER_CONFIG *sd) { 2061 VP9_COMP *cpi = (VP9_COMP *)(ptr); 2062 VP9_COMMON *cm = &cpi->common; 2063 int ref_fb_idx; 2064 2065 if (ref_frame_flag == VP9_LAST_FLAG) 2066 ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; 2067 else if (ref_frame_flag == VP9_GOLD_FLAG) 2068 ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; 2069 else if (ref_frame_flag == VP9_ALT_FLAG) 2070 ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; 2071 else 2072 return -1; 2073 2074 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); 2075 2076 return 0; 2077 } 2078 2079 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { 2080 VP9_COMP *cpi = (VP9_COMP *)(ptr); 2081 VP9_COMMON *cm = &cpi->common; 2082 2083 if (index < 0 || index >= NUM_REF_FRAMES) 2084 return -1; 2085 2086 *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; 2087 return 0; 2088 } 2089 2090 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, 2091 YV12_BUFFER_CONFIG *sd) { 2092 VP9_COMP *cpi = (VP9_COMP *)(ptr); 2093 VP9_COMMON *cm = &cpi->common; 2094 2095 int ref_fb_idx; 2096 2097 if (ref_frame_flag == VP9_LAST_FLAG) 2098 ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; 2099 else if (ref_frame_flag == VP9_GOLD_FLAG) 2100 ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; 2101 else if (ref_frame_flag == VP9_ALT_FLAG) 2102 ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; 2103 else 2104 return -1; 2105 2106 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]); 2107 2108 return 0; 2109 } 2110 int vp9_update_entropy(VP9_PTR comp, int update) { 2111 ((VP9_COMP *)comp)->common.refresh_frame_context = update; 2112 return 0; 2113 } 2114 2115 2116 #ifdef OUTPUT_YUV_SRC 2117 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) { 2118 uint8_t *src = s->y_buffer; 2119 int h = s->y_height; 2120 2121 do { 2122 fwrite(src, s->y_width, 1, yuv_file); 2123 src += s->y_stride; 2124 } while (--h); 2125 2126 src = s->u_buffer; 2127 h = s->uv_height; 2128 2129 do { 2130 fwrite(src, s->uv_width, 1, yuv_file); 2131 src += s->uv_stride; 2132 } while (--h); 2133 2134 src = s->v_buffer; 2135 h = s->uv_height; 2136 2137 do { 2138 fwrite(src, s->uv_width, 1, yuv_file); 2139 src += s->uv_stride; 2140 } while (--h); 2141 } 2142 #endif 2143 2144 #ifdef OUTPUT_YUV_REC 2145 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) { 2146 YV12_BUFFER_CONFIG *s = cm->frame_to_show; 2147 uint8_t *src = s->y_buffer; 2148 int h = cm->height; 2149 2150 do { 2151 fwrite(src, s->y_width, 1, yuv_rec_file); 2152 src += s->y_stride; 2153 } while (--h); 2154 2155 src = s->u_buffer; 2156 h = s->uv_height; 2157 2158 do { 2159 fwrite(src, s->uv_width, 1, yuv_rec_file); 2160 src += s->uv_stride; 2161 } while (--h); 2162 2163 src = s->v_buffer; 2164 h = s->uv_height; 2165 2166 do { 2167 fwrite(src, s->uv_width, 1, yuv_rec_file); 2168 src += s->uv_stride; 2169 } while (--h); 2170 2171 #if CONFIG_ALPHA 2172 if (s->alpha_buffer) { 2173 src = s->alpha_buffer; 2174 h = s->alpha_height; 2175 do { 2176 fwrite(src, s->alpha_width, 1, yuv_rec_file); 2177 src += s->alpha_stride; 2178 } while (--h); 2179 } 2180 #endif 2181 2182 fflush(yuv_rec_file); 2183 } 2184 #endif 2185 2186 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb, 2187 YV12_BUFFER_CONFIG *dst_fb) { 2188 const int in_w = src_fb->y_crop_width; 2189 const int in_h = src_fb->y_crop_height; 2190 const int out_w = dst_fb->y_crop_width; 2191 const int out_h = dst_fb->y_crop_height; 2192 int x, y, i; 2193 2194 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer, 2195 src_fb->alpha_buffer}; 2196 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride, 2197 src_fb->alpha_stride}; 2198 2199 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer, 2200 dst_fb->alpha_buffer}; 2201 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride, 2202 dst_fb->alpha_stride}; 2203 2204 for (y = 0; y < out_h; y += 16) { 2205 for (x = 0; x < out_w; x += 16) { 2206 for (i = 0; i < MAX_MB_PLANE; ++i) { 2207 const int factor = i == 0 ? 1 : 2; 2208 const int x_q4 = x * (16 / factor) * in_w / out_w; 2209 const int y_q4 = y * (16 / factor) * in_h / out_h; 2210 const int src_stride = src_strides[i]; 2211 const int dst_stride = dst_strides[i]; 2212 uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride + 2213 x / factor * in_w / out_w; 2214 uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor; 2215 2216 vp9_convolve8(src, src_stride, dst, dst_stride, 2217 vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w, 2218 vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h, 2219 16 / factor, 16 / factor); 2220 } 2221 } 2222 } 2223 2224 vp8_yv12_extend_frame_borders(dst_fb); 2225 } 2226 2227 2228 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { 2229 // this frame refreshes means next frames don't unless specified by user 2230 cpi->frames_since_golden = 0; 2231 2232 #if CONFIG_MULTIPLE_ARF 2233 if (!cpi->multi_arf_enabled) 2234 #endif 2235 // Clear the alternate reference update pending flag. 2236 cpi->source_alt_ref_pending = 0; 2237 2238 // Set the alternate reference frame active flag 2239 cpi->source_alt_ref_active = 1; 2240 } 2241 static void update_golden_frame_stats(VP9_COMP *cpi) { 2242 // Update the Golden frame usage counts. 2243 if (cpi->refresh_golden_frame) { 2244 // this frame refreshes means next frames don't unless specified by user 2245 cpi->refresh_golden_frame = 0; 2246 cpi->frames_since_golden = 0; 2247 2248 // ******** Fixed Q test code only ************ 2249 // If we are going to use the ALT reference for the next group of frames set a flag to say so. 2250 if (cpi->oxcf.fixed_q >= 0 && 2251 cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) { 2252 cpi->source_alt_ref_pending = 1; 2253 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 2254 2255 // TODO(ivan): for SVC encoder, GF automatic update is disabled by using a 2256 // large GF_interval 2257 if (cpi->use_svc) { 2258 cpi->frames_till_gf_update_due = INT_MAX; 2259 } 2260 } 2261 2262 if (!cpi->source_alt_ref_pending) 2263 cpi->source_alt_ref_active = 0; 2264 2265 // Decrement count down till next gf 2266 if (cpi->frames_till_gf_update_due > 0) 2267 cpi->frames_till_gf_update_due--; 2268 2269 } else if (!cpi->refresh_alt_ref_frame) { 2270 // Decrement count down till next gf 2271 if (cpi->frames_till_gf_update_due > 0) 2272 cpi->frames_till_gf_update_due--; 2273 2274 if (cpi->frames_till_alt_ref_frame) 2275 cpi->frames_till_alt_ref_frame--; 2276 2277 cpi->frames_since_golden++; 2278 } 2279 } 2280 2281 static int find_fp_qindex() { 2282 int i; 2283 2284 for (i = 0; i < QINDEX_RANGE; i++) { 2285 if (vp9_convert_qindex_to_q(i) >= 30.0) { 2286 break; 2287 } 2288 } 2289 2290 if (i == QINDEX_RANGE) 2291 i--; 2292 2293 return i; 2294 } 2295 2296 static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) { 2297 (void) size; 2298 (void) dest; 2299 (void) frame_flags; 2300 2301 2302 vp9_set_quantizer(cpi, find_fp_qindex()); 2303 vp9_first_pass(cpi); 2304 } 2305 2306 #define WRITE_RECON_BUFFER 0 2307 #if WRITE_RECON_BUFFER 2308 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { 2309 2310 // write the frame 2311 FILE *yframe; 2312 int i; 2313 char filename[255]; 2314 2315 sprintf(filename, "cx\\y%04d.raw", this_frame); 2316 yframe = fopen(filename, "wb"); 2317 2318 for (i = 0; i < frame->y_height; i++) 2319 fwrite(frame->y_buffer + i * frame->y_stride, 2320 frame->y_width, 1, yframe); 2321 2322 fclose(yframe); 2323 sprintf(filename, "cx\\u%04d.raw", this_frame); 2324 yframe = fopen(filename, "wb"); 2325 2326 for (i = 0; i < frame->uv_height; i++) 2327 fwrite(frame->u_buffer + i * frame->uv_stride, 2328 frame->uv_width, 1, yframe); 2329 2330 fclose(yframe); 2331 sprintf(filename, "cx\\v%04d.raw", this_frame); 2332 yframe = fopen(filename, "wb"); 2333 2334 for (i = 0; i < frame->uv_height; i++) 2335 fwrite(frame->v_buffer + i * frame->uv_stride, 2336 frame->uv_width, 1, yframe); 2337 2338 fclose(yframe); 2339 } 2340 #endif 2341 2342 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) { 2343 #define EDGE_THRESH 128 2344 int i, j; 2345 int num_edge_pels = 0; 2346 int num_pels = (frame->y_height - 2) * (frame->y_width - 2); 2347 uint8_t *prev = frame->y_buffer + 1; 2348 uint8_t *curr = frame->y_buffer + 1 + frame->y_stride; 2349 uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride; 2350 for (i = 1; i < frame->y_height - 1; i++) { 2351 for (j = 1; j < frame->y_width - 1; j++) { 2352 /* Sobel hor and ver gradients */ 2353 int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) + (next[1] - next[-1]); 2354 int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) + (prev[-1] - next[-1]); 2355 h = (h < 0 ? -h : h); 2356 v = (v < 0 ? -v : v); 2357 if (h > EDGE_THRESH || v > EDGE_THRESH) 2358 num_edge_pels++; 2359 curr++; 2360 prev++; 2361 next++; 2362 } 2363 curr += frame->y_stride - frame->y_width + 2; 2364 prev += frame->y_stride - frame->y_width + 2; 2365 next += frame->y_stride - frame->y_width + 2; 2366 } 2367 return (double)num_edge_pels / num_pels; 2368 } 2369 2370 // Function to test for conditions that indicate we should loop 2371 // back and recode a frame. 2372 static int recode_loop_test(VP9_COMP *cpi, 2373 int high_limit, int low_limit, 2374 int q, int maxq, int minq) { 2375 int force_recode = 0; 2376 VP9_COMMON *cm = &cpi->common; 2377 2378 // Is frame recode allowed at all 2379 // Yes if either recode mode 1 is selected or mode two is selected 2380 // and the frame is a key frame. golden frame or alt_ref_frame 2381 if ((cpi->sf.recode_loop == 1) || 2382 ((cpi->sf.recode_loop == 2) && 2383 ((cm->frame_type == KEY_FRAME) || 2384 cpi->refresh_golden_frame || 2385 cpi->refresh_alt_ref_frame))) { 2386 // General over and under shoot tests 2387 if (((cpi->projected_frame_size > high_limit) && (q < maxq)) || 2388 ((cpi->projected_frame_size < low_limit) && (q > minq))) { 2389 force_recode = 1; 2390 } 2391 // Special Constrained quality tests 2392 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { 2393 // Undershoot and below auto cq level 2394 if (q > cpi->cq_target_quality && 2395 cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) { 2396 force_recode = 1; 2397 } else if (q > cpi->oxcf.cq_level && 2398 cpi->projected_frame_size < cpi->min_frame_bandwidth && 2399 cpi->active_best_quality > cpi->oxcf.cq_level) { 2400 // Severe undershoot and between auto and user cq level 2401 force_recode = 1; 2402 cpi->active_best_quality = cpi->oxcf.cq_level; 2403 } 2404 } 2405 } 2406 2407 return force_recode; 2408 } 2409 2410 static void update_reference_frames(VP9_COMP * const cpi) { 2411 VP9_COMMON * const cm = &cpi->common; 2412 2413 // At this point the new frame has been encoded. 2414 // If any buffer copy / swapping is signaled it should be done here. 2415 if (cm->frame_type == KEY_FRAME) { 2416 ref_cnt_fb(cm->fb_idx_ref_cnt, 2417 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 2418 ref_cnt_fb(cm->fb_idx_ref_cnt, 2419 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 2420 } 2421 #if CONFIG_MULTIPLE_ARF 2422 else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame && 2423 !cpi->refresh_alt_ref_frame) { 2424 #else 2425 else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame && 2426 !cpi->use_svc) { 2427 #endif 2428 /* Preserve the previously existing golden frame and update the frame in 2429 * the alt ref slot instead. This is highly specific to the current use of 2430 * alt-ref as a forward reference, and this needs to be generalized as 2431 * other uses are implemented (like RTC/temporal scaling) 2432 * 2433 * The update to the buffer in the alt ref slot was signaled in 2434 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated 2435 * as the golden frame next time. 2436 */ 2437 int tmp; 2438 2439 ref_cnt_fb(cm->fb_idx_ref_cnt, 2440 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 2441 2442 tmp = cpi->alt_fb_idx; 2443 cpi->alt_fb_idx = cpi->gld_fb_idx; 2444 cpi->gld_fb_idx = tmp; 2445 } else { /* For non key/golden frames */ 2446 if (cpi->refresh_alt_ref_frame) { 2447 int arf_idx = cpi->alt_fb_idx; 2448 #if CONFIG_MULTIPLE_ARF 2449 if (cpi->multi_arf_enabled) { 2450 arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1]; 2451 } 2452 #endif 2453 ref_cnt_fb(cm->fb_idx_ref_cnt, 2454 &cm->ref_frame_map[arf_idx], cm->new_fb_idx); 2455 } 2456 2457 if (cpi->refresh_golden_frame) { 2458 ref_cnt_fb(cm->fb_idx_ref_cnt, 2459 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 2460 } 2461 } 2462 2463 if (cpi->refresh_last_frame) { 2464 ref_cnt_fb(cm->fb_idx_ref_cnt, 2465 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx); 2466 } 2467 } 2468 2469 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) { 2470 MACROBLOCKD *xd = &cpi->mb.e_mbd; 2471 struct loopfilter *lf = &cm->lf; 2472 if (xd->lossless) { 2473 lf->filter_level = 0; 2474 } else { 2475 struct vpx_usec_timer timer; 2476 2477 vp9_clear_system_state(); 2478 2479 vpx_usec_timer_start(&timer); 2480 2481 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick); 2482 2483 vpx_usec_timer_mark(&timer); 2484 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); 2485 } 2486 2487 if (lf->filter_level > 0) { 2488 vp9_set_alt_lf_level(cpi, lf->filter_level); 2489 vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); 2490 } 2491 2492 vp9_extend_frame_inner_borders(cm->frame_to_show, 2493 cm->subsampling_x, cm->subsampling_y); 2494 } 2495 2496 static void scale_references(VP9_COMP *cpi) { 2497 VP9_COMMON *cm = &cpi->common; 2498 int i; 2499 int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx, 2500 cpi->alt_fb_idx}; 2501 2502 for (i = 0; i < 3; i++) { 2503 YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]]; 2504 2505 if (ref->y_crop_width != cm->width || 2506 ref->y_crop_height != cm->height) { 2507 int new_fb = get_free_fb(cm); 2508 2509 vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb], 2510 cm->width, cm->height, 2511 cm->subsampling_x, cm->subsampling_y, 2512 VP9BORDERINPIXELS); 2513 scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]); 2514 cpi->scaled_ref_idx[i] = new_fb; 2515 } else { 2516 cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]]; 2517 cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++; 2518 } 2519 } 2520 } 2521 2522 static void release_scaled_references(VP9_COMP *cpi) { 2523 VP9_COMMON *cm = &cpi->common; 2524 int i; 2525 2526 for (i = 0; i < 3; i++) 2527 cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--; 2528 } 2529 2530 static void full_to_model_count(unsigned int *model_count, 2531 unsigned int *full_count) { 2532 int n; 2533 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; 2534 model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; 2535 model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; 2536 for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n) 2537 model_count[TWO_TOKEN] += full_count[n]; 2538 model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN]; 2539 } 2540 2541 static void full_to_model_counts( 2542 vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) { 2543 int i, j, k, l; 2544 for (i = 0; i < BLOCK_TYPES; ++i) 2545 for (j = 0; j < REF_TYPES; ++j) 2546 for (k = 0; k < COEF_BANDS; ++k) 2547 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) { 2548 if (l >= 3 && k == 0) 2549 continue; 2550 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); 2551 } 2552 } 2553 2554 2555 static void encode_frame_to_data_rate(VP9_COMP *cpi, 2556 unsigned long *size, 2557 unsigned char *dest, 2558 unsigned int *frame_flags) { 2559 VP9_COMMON *cm = &cpi->common; 2560 MACROBLOCKD *xd = &cpi->mb.e_mbd; 2561 TX_SIZE t; 2562 int q; 2563 int frame_over_shoot_limit; 2564 int frame_under_shoot_limit; 2565 2566 int loop = 0; 2567 int loop_count; 2568 2569 int q_low; 2570 int q_high; 2571 2572 int top_index; 2573 int bottom_index; 2574 int active_worst_qchanged = 0; 2575 2576 int overshoot_seen = 0; 2577 int undershoot_seen = 0; 2578 2579 SPEED_FEATURES *sf = &cpi->sf; 2580 unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height); 2581 struct segmentation *seg = &cm->seg; 2582 2583 /* Scale the source buffer, if required */ 2584 if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width || 2585 cm->mi_rows * 8 != cpi->un_scaled_source->y_height) { 2586 scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source); 2587 cpi->Source = &cpi->scaled_source; 2588 } else { 2589 cpi->Source = cpi->un_scaled_source; 2590 } 2591 2592 scale_references(cpi); 2593 2594 // Clear down mmx registers to allow floating point in what follows 2595 vp9_clear_system_state(); 2596 2597 2598 // For an alt ref frame in 2 pass we skip the call to the second 2599 // pass function that sets the target bandwidth so must set it here 2600 if (cpi->refresh_alt_ref_frame) { 2601 // Per frame bit target for the alt ref frame 2602 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; 2603 // per second target bitrate 2604 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * 2605 cpi->output_framerate); 2606 } 2607 2608 // Clear zbin over-quant value and mode boost values. 2609 cpi->zbin_mode_boost = 0; 2610 2611 // Enable or disable mode based tweaking of the zbin 2612 // For 2 Pass Only used where GF/ARF prediction quality 2613 // is above a threshold 2614 cpi->zbin_mode_boost = 0; 2615 2616 // if (cpi->oxcf.lossless) 2617 cpi->zbin_mode_boost_enabled = 0; 2618 // else 2619 // cpi->zbin_mode_boost_enabled = 1; 2620 2621 // Current default encoder behaviour for the altref sign bias 2622 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = cpi->source_alt_ref_active; 2623 2624 // Check to see if a key frame is signaled 2625 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass. 2626 if ((cm->current_video_frame == 0) || 2627 (cm->frame_flags & FRAMEFLAGS_KEY) || 2628 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0))) { 2629 // Key frame from VFW/auto-keyframe/first frame 2630 cm->frame_type = KEY_FRAME; 2631 } 2632 2633 // Set default state for segment based loop filter update flags 2634 cm->lf.mode_ref_delta_update = 0; 2635 2636 // Initialize cpi->mv_step_param to default based on max resolution 2637 cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def); 2638 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate. 2639 if (sf->auto_mv_step_size) { 2640 if ((cpi->common.frame_type == KEY_FRAME) || cpi->common.intra_only) { 2641 // initialize max_mv_magnitude for use in the first INTER frame 2642 // after a key/intra-only frame 2643 cpi->max_mv_magnitude = max_mv_def; 2644 } else { 2645 if (cm->show_frame) 2646 // allow mv_steps to correspond to twice the max mv magnitude found 2647 // in the previous frame, capped by the default max_mv_magnitude based 2648 // on resolution 2649 cpi->mv_step_param = vp9_init_search_range( 2650 cpi, MIN(max_mv_def, 2 * cpi->max_mv_magnitude)); 2651 cpi->max_mv_magnitude = 0; 2652 } 2653 } 2654 2655 // Set various flags etc to special state if it is a key frame 2656 if (cm->frame_type == KEY_FRAME) { 2657 // Reset the loop filter deltas and segmentation map 2658 setup_features(cm); 2659 2660 // If segmentation is enabled force a map update for key frames 2661 if (seg->enabled) { 2662 seg->update_map = 1; 2663 seg->update_data = 1; 2664 } 2665 2666 // The alternate reference frame cannot be active for a key frame 2667 cpi->source_alt_ref_active = 0; 2668 2669 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0); 2670 cm->frame_parallel_decoding_mode = 2671 (cpi->oxcf.frame_parallel_decoding_mode != 0); 2672 if (cm->error_resilient_mode) { 2673 cm->frame_parallel_decoding_mode = 1; 2674 cm->reset_frame_context = 0; 2675 cm->refresh_frame_context = 0; 2676 } 2677 } 2678 2679 // Configure experimental use of segmentation for enhanced coding of 2680 // static regions if indicated. 2681 // Only allowed for now in second pass of two pass (as requires lagged coding) 2682 // and if the relevant speed feature flag is set. 2683 if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) { 2684 configure_static_seg_features(cpi); 2685 } 2686 2687 // Decide how big to make the frame 2688 vp9_pick_frame_size(cpi); 2689 2690 vp9_clear_system_state(); 2691 2692 // Set an active best quality and if necessary active worst quality 2693 q = cpi->active_worst_quality; 2694 2695 if (cm->frame_type == KEY_FRAME) { 2696 #if !CONFIG_MULTIPLE_ARF 2697 // Special case for key frames forced because we have reached 2698 // the maximum key frame interval. Here force the Q to a range 2699 // based on the ambient Q to reduce the risk of popping 2700 if (cpi->this_key_frame_forced) { 2701 int delta_qindex; 2702 int qindex = cpi->last_boosted_qindex; 2703 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 2704 2705 delta_qindex = compute_qdelta(cpi, last_boosted_q, 2706 (last_boosted_q * 0.75)); 2707 2708 cpi->active_best_quality = MAX(qindex + delta_qindex, 2709 cpi->best_quality); 2710 } else { 2711 int high = 5000; 2712 int low = 400; 2713 double q_adj_factor = 1.0; 2714 double q_val; 2715 2716 // Baseline value derived from cpi->active_worst_quality and kf boost 2717 if (cpi->kf_boost > high) { 2718 cpi->active_best_quality = kf_low_motion_minq[q]; 2719 } else if (cpi->kf_boost < low) { 2720 cpi->active_best_quality = kf_high_motion_minq[q]; 2721 } else { 2722 const int gap = high - low; 2723 const int offset = high - cpi->kf_boost; 2724 const int qdiff = kf_high_motion_minq[q] - kf_low_motion_minq[q]; 2725 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 2726 2727 cpi->active_best_quality = kf_low_motion_minq[q] + adjustment; 2728 } 2729 2730 // Allow somewhat lower kf minq with small image formats. 2731 if ((cm->width * cm->height) <= (352 * 288)) { 2732 q_adj_factor -= 0.25; 2733 } 2734 2735 // Make a further adjustment based on the kf zero motion measure. 2736 q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct); 2737 2738 // Convert the adjustment factor to a qindex delta 2739 // on active_best_quality. 2740 q_val = vp9_convert_qindex_to_q(cpi->active_best_quality); 2741 cpi->active_best_quality += 2742 compute_qdelta(cpi, q_val, (q_val * q_adj_factor)); 2743 } 2744 #else 2745 double current_q; 2746 // Force the KF quantizer to be 30% of the active_worst_quality. 2747 current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); 2748 cpi->active_best_quality = cpi->active_worst_quality 2749 + compute_qdelta(cpi, current_q, current_q * 0.3); 2750 #endif 2751 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { 2752 int high = 2000; 2753 int low = 400; 2754 2755 // Use the lower of cpi->active_worst_quality and recent 2756 // average Q as basis for GF/ARF Q limit unless last frame was 2757 // a key frame. 2758 if (cpi->frames_since_key > 1 && 2759 cpi->avg_frame_qindex < cpi->active_worst_quality) { 2760 q = cpi->avg_frame_qindex; 2761 } 2762 // For constrained quality dont allow Q less than the cq level 2763 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && 2764 q < cpi->cq_target_quality) { 2765 q = cpi->cq_target_quality; 2766 } 2767 if (cpi->gfu_boost > high) { 2768 cpi->active_best_quality = gf_low_motion_minq[q]; 2769 } else if (cpi->gfu_boost < low) { 2770 cpi->active_best_quality = gf_high_motion_minq[q]; 2771 } else { 2772 const int gap = high - low; 2773 const int offset = high - cpi->gfu_boost; 2774 const int qdiff = gf_high_motion_minq[q] - gf_low_motion_minq[q]; 2775 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 2776 2777 cpi->active_best_quality = gf_low_motion_minq[q] + adjustment; 2778 } 2779 2780 // Constrained quality use slightly lower active best. 2781 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) 2782 cpi->active_best_quality = cpi->active_best_quality * 15 / 16; 2783 2784 // TODO(debargha): Refine the logic below 2785 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 2786 if (!cpi->refresh_alt_ref_frame) { 2787 cpi->active_best_quality = cpi->cq_target_quality; 2788 } else { 2789 if (cpi->frames_since_key > 1) { 2790 if (cpi->gfu_boost > high) { 2791 cpi->active_best_quality = cpi->cq_target_quality * 6 / 16; 2792 } else if (cpi->gfu_boost < low) { 2793 cpi->active_best_quality = cpi->cq_target_quality * 11 / 16; 2794 } else { 2795 const int gap = high - low; 2796 const int offset = high - cpi->gfu_boost; 2797 const int qdiff = cpi->cq_target_quality * 5 / 16; 2798 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 2799 cpi->active_best_quality = cpi->cq_target_quality * 6 / 16 2800 + adjustment; 2801 } 2802 } 2803 } 2804 } 2805 } else { 2806 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 2807 cpi->active_best_quality = cpi->cq_target_quality; 2808 } else { 2809 #ifdef ONE_SHOT_Q_ESTIMATE 2810 #ifdef STRICT_ONE_SHOT_Q 2811 cpi->active_best_quality = q; 2812 #else 2813 cpi->active_best_quality = inter_minq[q]; 2814 #endif 2815 #else 2816 cpi->active_best_quality = inter_minq[q]; 2817 #endif 2818 2819 // For the constant/constrained quality mode we don't want 2820 // q to fall below the cq level. 2821 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 2822 (cpi->active_best_quality < cpi->cq_target_quality)) { 2823 // If we are strongly undershooting the target rate in the last 2824 // frames then use the user passed in cq value not the auto 2825 // cq value. 2826 if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) 2827 cpi->active_best_quality = cpi->oxcf.cq_level; 2828 else 2829 cpi->active_best_quality = cpi->cq_target_quality; 2830 } 2831 } 2832 } 2833 2834 // Clip the active best and worst quality values to limits 2835 if (cpi->active_worst_quality > cpi->worst_quality) 2836 cpi->active_worst_quality = cpi->worst_quality; 2837 2838 if (cpi->active_best_quality < cpi->best_quality) 2839 cpi->active_best_quality = cpi->best_quality; 2840 2841 if (cpi->active_best_quality > cpi->worst_quality) 2842 cpi->active_best_quality = cpi->worst_quality; 2843 2844 if (cpi->active_worst_quality < cpi->active_best_quality) 2845 cpi->active_worst_quality = cpi->active_best_quality; 2846 2847 // Special case code to try and match quality with forced key frames 2848 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 2849 q = cpi->active_best_quality; 2850 } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { 2851 q = cpi->last_boosted_qindex; 2852 } else { 2853 // Determine initial Q to try 2854 q = vp9_regulate_q(cpi, cpi->this_frame_target); 2855 } 2856 2857 vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, 2858 &frame_over_shoot_limit); 2859 2860 #if CONFIG_MULTIPLE_ARF 2861 // Force the quantizer determined by the coding order pattern. 2862 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 2863 cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { 2864 double new_q; 2865 double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); 2866 int level = cpi->this_frame_weight; 2867 assert(level >= 0); 2868 2869 // Set quantizer steps at 10% increments. 2870 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 2871 q = cpi->active_worst_quality + compute_qdelta(cpi, current_q, new_q); 2872 2873 bottom_index = q; 2874 top_index = q; 2875 q_low = q; 2876 q_high = q; 2877 2878 printf("frame:%d q:%d\n", cm->current_video_frame, q); 2879 } else { 2880 #endif 2881 // Limit Q range for the adaptive loop. 2882 bottom_index = cpi->active_best_quality; 2883 top_index = cpi->active_worst_quality; 2884 q_low = cpi->active_best_quality; 2885 q_high = cpi->active_worst_quality; 2886 #if CONFIG_MULTIPLE_ARF 2887 } 2888 #endif 2889 loop_count = 0; 2890 vp9_zero(cpi->rd_tx_select_threshes); 2891 2892 if (cm->frame_type != KEY_FRAME) { 2893 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; 2894 /* TODO: Decide this more intelligently */ 2895 xd->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH; 2896 set_mvcost(&cpi->mb); 2897 } 2898 2899 #if CONFIG_VP9_POSTPROC 2900 2901 if (cpi->oxcf.noise_sensitivity > 0) { 2902 int l = 0; 2903 2904 switch (cpi->oxcf.noise_sensitivity) { 2905 case 1: 2906 l = 20; 2907 break; 2908 case 2: 2909 l = 40; 2910 break; 2911 case 3: 2912 l = 60; 2913 break; 2914 case 4: 2915 case 5: 2916 l = 100; 2917 break; 2918 case 6: 2919 l = 150; 2920 break; 2921 } 2922 2923 vp9_denoise(cpi->Source, cpi->Source, l); 2924 } 2925 2926 #endif 2927 2928 #ifdef OUTPUT_YUV_SRC 2929 vp9_write_yuv_frame(cpi->Source); 2930 #endif 2931 2932 do { 2933 vp9_clear_system_state(); // __asm emms; 2934 2935 vp9_set_quantizer(cpi, q); 2936 2937 if (loop_count == 0) { 2938 2939 // Set up entropy depending on frame type. 2940 if (cm->frame_type == KEY_FRAME) { 2941 /* Choose which entropy context to use. When using a forward reference 2942 * frame, it immediately follows the keyframe, and thus benefits from 2943 * using the same entropy context established by the keyframe. 2944 * Otherwise, use the default context 0. 2945 */ 2946 cm->frame_context_idx = cpi->oxcf.play_alternate; 2947 vp9_setup_key_frame(cpi); 2948 } else { 2949 /* Choose which entropy context to use. Currently there are only two 2950 * contexts used, one for normal frames and one for alt ref frames. 2951 */ 2952 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; 2953 vp9_setup_inter_frame(cpi); 2954 } 2955 } 2956 2957 // transform / motion compensation build reconstruction frame 2958 2959 vp9_encode_frame(cpi); 2960 2961 // Update the skip mb flag probabilities based on the distribution 2962 // seen in the last encoder iteration. 2963 // update_base_skip_probs(cpi); 2964 2965 vp9_clear_system_state(); // __asm emms; 2966 2967 // Dummy pack of the bitstream using up to date stats to get an 2968 // accurate estimate of output frame size to determine if we need 2969 // to recode. 2970 vp9_save_coding_context(cpi); 2971 cpi->dummy_packing = 1; 2972 vp9_pack_bitstream(cpi, dest, size); 2973 cpi->projected_frame_size = (*size) << 3; 2974 vp9_restore_coding_context(cpi); 2975 2976 if (frame_over_shoot_limit == 0) 2977 frame_over_shoot_limit = 1; 2978 active_worst_qchanged = 0; 2979 2980 // Special case handling for forced key frames 2981 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 2982 loop = 0; 2983 } else { 2984 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { 2985 int last_q = q; 2986 int kf_err = vp9_calc_ss_err(cpi->Source, 2987 &cm->yv12_fb[cm->new_fb_idx]); 2988 2989 int high_err_target = cpi->ambient_err; 2990 int low_err_target = cpi->ambient_err >> 1; 2991 2992 // Prevent possible divide by zero error below for perfect KF 2993 kf_err += !kf_err; 2994 2995 // The key frame is not good enough or we can afford 2996 // to make it better without undue risk of popping. 2997 if ((kf_err > high_err_target && 2998 cpi->projected_frame_size <= frame_over_shoot_limit) || 2999 (kf_err > low_err_target && 3000 cpi->projected_frame_size <= frame_under_shoot_limit)) { 3001 // Lower q_high 3002 q_high = q > q_low ? q - 1 : q_low; 3003 3004 // Adjust Q 3005 q = (q * high_err_target) / kf_err; 3006 q = MIN(q, (q_high + q_low) >> 1); 3007 } else if (kf_err < low_err_target && 3008 cpi->projected_frame_size >= frame_under_shoot_limit) { 3009 // The key frame is much better than the previous frame 3010 // Raise q_low 3011 q_low = q < q_high ? q + 1 : q_high; 3012 3013 // Adjust Q 3014 q = (q * low_err_target) / kf_err; 3015 q = MIN(q, (q_high + q_low + 1) >> 1); 3016 } 3017 3018 // Clamp Q to upper and lower limits: 3019 q = clamp(q, q_low, q_high); 3020 3021 loop = q != last_q; 3022 } else if (recode_loop_test( 3023 cpi, frame_over_shoot_limit, frame_under_shoot_limit, 3024 q, top_index, bottom_index)) { 3025 // Is the projected frame size out of range and are we allowed 3026 // to attempt to recode. 3027 int last_q = q; 3028 int retries = 0; 3029 3030 // Frame size out of permitted range: 3031 // Update correction factor & compute new Q to try... 3032 3033 // Frame is too large 3034 if (cpi->projected_frame_size > cpi->this_frame_target) { 3035 // Raise Qlow as to at least the current value 3036 q_low = q < q_high ? q + 1 : q_high; 3037 3038 if (undershoot_seen || loop_count > 1) { 3039 // Update rate_correction_factor unless 3040 // cpi->active_worst_quality has changed. 3041 if (!active_worst_qchanged) 3042 vp9_update_rate_correction_factors(cpi, 1); 3043 3044 q = (q_high + q_low + 1) / 2; 3045 } else { 3046 // Update rate_correction_factor unless 3047 // cpi->active_worst_quality has changed. 3048 if (!active_worst_qchanged) 3049 vp9_update_rate_correction_factors(cpi, 0); 3050 3051 q = vp9_regulate_q(cpi, cpi->this_frame_target); 3052 3053 while (q < q_low && retries < 10) { 3054 vp9_update_rate_correction_factors(cpi, 0); 3055 q = vp9_regulate_q(cpi, cpi->this_frame_target); 3056 retries++; 3057 } 3058 } 3059 3060 overshoot_seen = 1; 3061 } else { 3062 // Frame is too small 3063 q_high = q > q_low ? q - 1 : q_low; 3064 3065 if (overshoot_seen || loop_count > 1) { 3066 // Update rate_correction_factor unless 3067 // cpi->active_worst_quality has changed. 3068 if (!active_worst_qchanged) 3069 vp9_update_rate_correction_factors(cpi, 1); 3070 3071 q = (q_high + q_low) / 2; 3072 } else { 3073 // Update rate_correction_factor unless 3074 // cpi->active_worst_quality has changed. 3075 if (!active_worst_qchanged) 3076 vp9_update_rate_correction_factors(cpi, 0); 3077 3078 q = vp9_regulate_q(cpi, cpi->this_frame_target); 3079 3080 // Special case reset for qlow for constrained quality. 3081 // This should only trigger where there is very substantial 3082 // undershoot on a frame and the auto cq level is above 3083 // the user passsed in value. 3084 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) { 3085 q_low = q; 3086 } 3087 3088 while (q > q_high && retries < 10) { 3089 vp9_update_rate_correction_factors(cpi, 0); 3090 q = vp9_regulate_q(cpi, cpi->this_frame_target); 3091 retries++; 3092 } 3093 } 3094 3095 undershoot_seen = 1; 3096 } 3097 3098 // Clamp Q to upper and lower limits: 3099 q = clamp(q, q_low, q_high); 3100 3101 loop = q != last_q; 3102 } else { 3103 loop = 0; 3104 } 3105 } 3106 3107 if (cpi->is_src_frame_alt_ref) 3108 loop = 0; 3109 3110 if (loop) { 3111 loop_count++; 3112 3113 #if CONFIG_INTERNAL_STATS 3114 cpi->tot_recode_hits++; 3115 #endif 3116 } 3117 } while (loop); 3118 3119 // Special case code to reduce pulsing when key frames are forced at a 3120 // fixed interval. Note the reconstruction error if it is the frame before 3121 // the force key frame 3122 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) { 3123 cpi->ambient_err = vp9_calc_ss_err(cpi->Source, 3124 &cm->yv12_fb[cm->new_fb_idx]); 3125 } 3126 3127 if (cm->frame_type == KEY_FRAME) 3128 cpi->refresh_last_frame = 1; 3129 3130 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx]; 3131 3132 #if WRITE_RECON_BUFFER 3133 if (cm->show_frame) 3134 write_cx_frame_to_file(cm->frame_to_show, 3135 cm->current_video_frame); 3136 else 3137 write_cx_frame_to_file(cm->frame_to_show, 3138 cm->current_video_frame + 1000); 3139 #endif 3140 3141 // Pick the loop filter level for the frame. 3142 loopfilter_frame(cpi, cm); 3143 3144 #if WRITE_RECON_BUFFER 3145 if (cm->show_frame) 3146 write_cx_frame_to_file(cm->frame_to_show, 3147 cm->current_video_frame + 2000); 3148 else 3149 write_cx_frame_to_file(cm->frame_to_show, 3150 cm->current_video_frame + 3000); 3151 #endif 3152 3153 // build the bitstream 3154 cpi->dummy_packing = 0; 3155 vp9_pack_bitstream(cpi, dest, size); 3156 3157 if (cm->seg.update_map) 3158 update_reference_segmentation_map(cpi); 3159 3160 release_scaled_references(cpi); 3161 update_reference_frames(cpi); 3162 3163 for (t = TX_4X4; t <= TX_32X32; t++) 3164 full_to_model_counts(cpi->common.counts.coef[t], 3165 cpi->coef_counts[t]); 3166 if (!cpi->common.error_resilient_mode && 3167 !cpi->common.frame_parallel_decoding_mode) { 3168 vp9_adapt_coef_probs(&cpi->common); 3169 } 3170 3171 if (cpi->common.frame_type != KEY_FRAME) { 3172 FRAME_COUNTS *counts = &cpi->common.counts; 3173 3174 vp9_copy(counts->y_mode, cpi->y_mode_count); 3175 vp9_copy(counts->uv_mode, cpi->y_uv_mode_count); 3176 vp9_copy(counts->partition, cpi->partition_count); 3177 vp9_copy(counts->intra_inter, cpi->intra_inter_count); 3178 vp9_copy(counts->comp_inter, cpi->comp_inter_count); 3179 vp9_copy(counts->single_ref, cpi->single_ref_count); 3180 vp9_copy(counts->comp_ref, cpi->comp_ref_count); 3181 counts->mv = cpi->NMVcount; 3182 if (!cpi->common.error_resilient_mode && 3183 !cpi->common.frame_parallel_decoding_mode) { 3184 vp9_adapt_mode_probs(&cpi->common); 3185 vp9_adapt_mv_probs(&cpi->common, cpi->mb.e_mbd.allow_high_precision_mv); 3186 } 3187 } 3188 3189 #ifdef ENTROPY_STATS 3190 vp9_update_mode_context_stats(cpi); 3191 #endif 3192 3193 /* Move storing frame_type out of the above loop since it is also 3194 * needed in motion search besides loopfilter */ 3195 cm->last_frame_type = cm->frame_type; 3196 3197 // Update rate control heuristics 3198 cpi->total_byte_count += (*size); 3199 cpi->projected_frame_size = (*size) << 3; 3200 3201 if (!active_worst_qchanged) 3202 vp9_update_rate_correction_factors(cpi, 2); 3203 3204 cpi->last_q[cm->frame_type] = cm->base_qindex; 3205 3206 // Keep record of last boosted (KF/KF/ARF) Q value. 3207 // If the current frame is coded at a lower Q then we also update it. 3208 // If all mbs in this group are skipped only update if the Q value is 3209 // better than that already stored. 3210 // This is used to help set quality in forced key frames to reduce popping 3211 if ((cm->base_qindex < cpi->last_boosted_qindex) || 3212 ((cpi->static_mb_pct < 100) && 3213 ((cm->frame_type == KEY_FRAME) || 3214 cpi->refresh_alt_ref_frame || 3215 (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) { 3216 cpi->last_boosted_qindex = cm->base_qindex; 3217 } 3218 3219 if (cm->frame_type == KEY_FRAME) { 3220 vp9_adjust_key_frame_context(cpi); 3221 } 3222 3223 // Keep a record of ambient average Q. 3224 if (cm->frame_type != KEY_FRAME) 3225 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2; 3226 3227 // Keep a record from which we can calculate the average Q excluding GF updates and key frames 3228 if (cm->frame_type != KEY_FRAME && 3229 !cpi->refresh_golden_frame && 3230 !cpi->refresh_alt_ref_frame) { 3231 cpi->ni_frames++; 3232 cpi->tot_q += vp9_convert_qindex_to_q(q); 3233 cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames; 3234 3235 // Calculate the average Q for normal inter frames (not key or GFU frames). 3236 cpi->ni_tot_qi += q; 3237 cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames; 3238 } 3239 3240 // Update the buffer level variable. 3241 // Non-viewable frames are a special case and are treated as pure overhead. 3242 if (!cm->show_frame) 3243 cpi->bits_off_target -= cpi->projected_frame_size; 3244 else 3245 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size; 3246 3247 // Clip the buffer level at the maximum buffer size 3248 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) 3249 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; 3250 3251 // Rolling monitors of whether we are over or underspending used to help 3252 // regulate min and Max Q in two pass. 3253 if (cm->frame_type != KEY_FRAME) { 3254 cpi->rolling_target_bits = 3255 ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; 3256 cpi->rolling_actual_bits = 3257 ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; 3258 cpi->long_rolling_target_bits = 3259 ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; 3260 cpi->long_rolling_actual_bits = 3261 ((cpi->long_rolling_actual_bits * 31) + 3262 cpi->projected_frame_size + 16) / 32; 3263 } 3264 3265 // Actual bits spent 3266 cpi->total_actual_bits += cpi->projected_frame_size; 3267 3268 // Debug stats 3269 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size); 3270 3271 cpi->buffer_level = cpi->bits_off_target; 3272 3273 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames 3274 if (cm->frame_type == KEY_FRAME) { 3275 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 3276 3277 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); 3278 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { 3279 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 3280 3281 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); 3282 } 3283 3284 // Update the skip mb flag probabilities based on the distribution seen 3285 // in this frame. 3286 // update_base_skip_probs(cpi); 3287 3288 #if CONFIG_INTERNAL_STATS 3289 { 3290 FILE *f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); 3291 int recon_err; 3292 3293 vp9_clear_system_state(); // __asm emms; 3294 3295 recon_err = vp9_calc_ss_err(cpi->Source, 3296 &cm->yv12_fb[cm->new_fb_idx]); 3297 3298 if (cpi->twopass.total_left_stats.coded_error != 0.0) 3299 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" 3300 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" 3301 "%6d %6d %5d %5d %5d %8.2f %10d %10.3f" 3302 "%10.3f %8d %10d %10d %10d\n", 3303 cpi->common.current_video_frame, cpi->this_frame_target, 3304 cpi->projected_frame_size, 0, //loop_size_estimate, 3305 (cpi->projected_frame_size - cpi->this_frame_target), 3306 (int)cpi->total_target_vs_actual, 3307 (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target), 3308 (int)cpi->total_actual_bits, 3309 cm->base_qindex, 3310 vp9_convert_qindex_to_q(cm->base_qindex), 3311 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, 3312 vp9_convert_qindex_to_q(cpi->active_best_quality), 3313 vp9_convert_qindex_to_q(cpi->active_worst_quality), 3314 cpi->avg_q, 3315 vp9_convert_qindex_to_q(cpi->ni_av_qi), 3316 vp9_convert_qindex_to_q(cpi->cq_target_quality), 3317 cpi->refresh_last_frame, 3318 cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame, 3319 cm->frame_type, cpi->gfu_boost, 3320 cpi->twopass.est_max_qcorrection_factor, 3321 (int)cpi->twopass.bits_left, 3322 cpi->twopass.total_left_stats.coded_error, 3323 (double)cpi->twopass.bits_left / 3324 cpi->twopass.total_left_stats.coded_error, 3325 cpi->tot_recode_hits, recon_err, cpi->kf_boost, 3326 cpi->kf_zeromotion_pct); 3327 else 3328 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" 3329 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" 3330 "%5d %5d %5d %8d %8d %8.2f %10d %10.3f" 3331 "%8d %10d %10d %10d\n", 3332 cpi->common.current_video_frame, 3333 cpi->this_frame_target, cpi->projected_frame_size, 3334 0, //loop_size_estimate, 3335 (cpi->projected_frame_size - cpi->this_frame_target), 3336 (int)cpi->total_target_vs_actual, 3337 (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target), 3338 (int)cpi->total_actual_bits, 3339 cm->base_qindex, 3340 vp9_convert_qindex_to_q(cm->base_qindex), 3341 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, 3342 vp9_convert_qindex_to_q(cpi->active_best_quality), 3343 vp9_convert_qindex_to_q(cpi->active_worst_quality), 3344 cpi->avg_q, 3345 vp9_convert_qindex_to_q(cpi->ni_av_qi), 3346 vp9_convert_qindex_to_q(cpi->cq_target_quality), 3347 cpi->refresh_last_frame, 3348 cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame, 3349 cm->frame_type, cpi->gfu_boost, 3350 cpi->twopass.est_max_qcorrection_factor, 3351 (int)cpi->twopass.bits_left, 3352 cpi->twopass.total_left_stats.coded_error, 3353 cpi->tot_recode_hits, recon_err, cpi->kf_boost, 3354 cpi->kf_zeromotion_pct); 3355 3356 fclose(f); 3357 3358 if (0) { 3359 FILE *fmodes = fopen("Modes.stt", "a"); 3360 int i; 3361 3362 fprintf(fmodes, "%6d:%1d:%1d:%1d ", 3363 cpi->common.current_video_frame, 3364 cm->frame_type, cpi->refresh_golden_frame, 3365 cpi->refresh_alt_ref_frame); 3366 3367 for (i = 0; i < MAX_MODES; i++) 3368 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); 3369 3370 fprintf(fmodes, "\n"); 3371 3372 fclose(fmodes); 3373 } 3374 } 3375 3376 #endif 3377 3378 #if 0 3379 // Debug stats for segment feature experiments. 3380 print_seg_map(cpi); 3381 #endif 3382 3383 // If this was a kf or Gf note the Q 3384 if ((cm->frame_type == KEY_FRAME) 3385 || cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) 3386 cm->last_kf_gf_q = cm->base_qindex; 3387 3388 if (cpi->refresh_golden_frame == 1) 3389 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; 3390 else 3391 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; 3392 3393 if (cpi->refresh_alt_ref_frame == 1) 3394 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF; 3395 else 3396 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF; 3397 3398 3399 if (cpi->refresh_last_frame & cpi->refresh_golden_frame) 3400 cpi->gold_is_last = 1; 3401 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame) 3402 cpi->gold_is_last = 0; 3403 3404 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame) 3405 cpi->alt_is_last = 1; 3406 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame) 3407 cpi->alt_is_last = 0; 3408 3409 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame) 3410 cpi->gold_is_alt = 1; 3411 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame) 3412 cpi->gold_is_alt = 0; 3413 3414 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 3415 3416 if (cpi->gold_is_last) 3417 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG; 3418 3419 if (cpi->alt_is_last) 3420 cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 3421 3422 if (cpi->gold_is_alt) 3423 cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 3424 3425 if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame 3426 && (cm->frame_type != KEY_FRAME)) 3427 // Update the alternate reference frame stats as appropriate. 3428 update_alt_ref_frame_stats(cpi); 3429 else 3430 // Update the Golden frame stats as appropriate. 3431 update_golden_frame_stats(cpi); 3432 3433 if (cm->frame_type == KEY_FRAME) { 3434 // Tell the caller that the frame was coded as a key frame 3435 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; 3436 3437 #if CONFIG_MULTIPLE_ARF 3438 // Reset the sequence number. 3439 if (cpi->multi_arf_enabled) { 3440 cpi->sequence_number = 0; 3441 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 3442 cpi->new_frame_coding_order_period = -1; 3443 } 3444 #endif 3445 3446 // As this frame is a key frame the next defaults to an inter frame. 3447 cm->frame_type = INTER_FRAME; 3448 } else { 3449 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; 3450 3451 #if CONFIG_MULTIPLE_ARF 3452 /* Increment position in the coded frame sequence. */ 3453 if (cpi->multi_arf_enabled) { 3454 ++cpi->sequence_number; 3455 if (cpi->sequence_number >= cpi->frame_coding_order_period) { 3456 cpi->sequence_number = 0; 3457 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 3458 cpi->new_frame_coding_order_period = -1; 3459 } 3460 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number]; 3461 assert(cpi->this_frame_weight >= 0); 3462 } 3463 #endif 3464 } 3465 3466 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas. 3467 cm->seg.update_map = 0; 3468 cm->seg.update_data = 0; 3469 cm->lf.mode_ref_delta_update = 0; 3470 3471 // keep track of the last coded dimensions 3472 cm->last_width = cm->width; 3473 cm->last_height = cm->height; 3474 3475 // reset to normal state now that we are done. 3476 cm->last_show_frame = cm->show_frame; 3477 if (cm->show_frame) { 3478 // current mip will be the prev_mip for the next frame 3479 MODE_INFO *temp = cm->prev_mip; 3480 MODE_INFO **temp2 = cm->prev_mi_grid_base; 3481 cm->prev_mip = cm->mip; 3482 cm->mip = temp; 3483 cm->prev_mi_grid_base = cm->mi_grid_base; 3484 cm->mi_grid_base = temp2; 3485 3486 // update the upper left visible macroblock ptrs 3487 cm->mi = cm->mip + cm->mode_info_stride + 1; 3488 cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1; 3489 3490 // Don't increment frame counters if this was an altref buffer 3491 // update not a real frame 3492 ++cm->current_video_frame; 3493 ++cpi->frames_since_key; 3494 } 3495 // restore prev_mi 3496 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; 3497 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; 3498 3499 #if 0 3500 { 3501 char filename[512]; 3502 FILE *recon_file; 3503 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); 3504 recon_file = fopen(filename, "wb"); 3505 fwrite(cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].buffer_alloc, 3506 cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].frame_size, 3507 1, recon_file); 3508 fclose(recon_file); 3509 } 3510 #endif 3511 #ifdef OUTPUT_YUV_REC 3512 vp9_write_yuv_rec_frame(cm); 3513 #endif 3514 3515 } 3516 3517 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size, 3518 unsigned char *dest, unsigned int *frame_flags) { 3519 3520 cpi->enable_encode_breakout = 1; 3521 3522 if (!cpi->refresh_alt_ref_frame) 3523 vp9_second_pass(cpi); 3524 3525 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 3526 // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt"); 3527 #ifdef DISABLE_RC_LONG_TERM_MEM 3528 cpi->twopass.bits_left -= cpi->this_frame_target; 3529 #else 3530 cpi->twopass.bits_left -= 8 * *size; 3531 #endif 3532 3533 if (!cpi->refresh_alt_ref_frame) { 3534 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; 3535 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth 3536 * cpi->oxcf.two_pass_vbrmin_section / 100); 3537 3538 if (two_pass_min_rate < lower_bounds_min_rate) 3539 two_pass_min_rate = lower_bounds_min_rate; 3540 3541 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->oxcf.framerate); 3542 } 3543 } 3544 3545 static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) { 3546 VP9_COMMON *cm = &cpi->common; 3547 if (!cpi->initial_width) { 3548 // TODO(jkoleszar): Support 1/4 subsampling? 3549 cm->subsampling_x = (sd != NULL) && sd->uv_width < sd->y_width; 3550 cm->subsampling_y = (sd != NULL) && sd->uv_height < sd->y_height; 3551 alloc_raw_frame_buffers(cpi); 3552 3553 cpi->initial_width = cm->width; 3554 cpi->initial_height = cm->height; 3555 } 3556 } 3557 3558 3559 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags, 3560 YV12_BUFFER_CONFIG *sd, int64_t time_stamp, 3561 int64_t end_time) { 3562 VP9_COMP *cpi = (VP9_COMP *) ptr; 3563 struct vpx_usec_timer timer; 3564 int res = 0; 3565 3566 check_initial_width(cpi, sd); 3567 vpx_usec_timer_start(&timer); 3568 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags, 3569 cpi->active_map_enabled ? cpi->active_map : NULL)) 3570 res = -1; 3571 vpx_usec_timer_mark(&timer); 3572 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); 3573 3574 return res; 3575 } 3576 3577 3578 static int frame_is_reference(const VP9_COMP *cpi) { 3579 const VP9_COMMON *cm = &cpi->common; 3580 3581 return cm->frame_type == KEY_FRAME || 3582 cpi->refresh_last_frame || 3583 cpi->refresh_golden_frame || 3584 cpi->refresh_alt_ref_frame || 3585 cm->refresh_frame_context || 3586 cm->lf.mode_ref_delta_update || 3587 cm->seg.update_map || 3588 cm->seg.update_data; 3589 } 3590 3591 #if CONFIG_MULTIPLE_ARF 3592 int is_next_frame_arf(VP9_COMP *cpi) { 3593 // Negative entry in frame_coding_order indicates an ARF at this position. 3594 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; 3595 } 3596 #endif 3597 3598 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, 3599 unsigned long *size, unsigned char *dest, 3600 int64_t *time_stamp, int64_t *time_end, int flush) { 3601 VP9_COMP *cpi = (VP9_COMP *) ptr; 3602 VP9_COMMON *cm = &cpi->common; 3603 struct vpx_usec_timer cmptimer; 3604 YV12_BUFFER_CONFIG *force_src_buffer = NULL; 3605 int i; 3606 // FILE *fp_out = fopen("enc_frame_type.txt", "a"); 3607 3608 if (!cpi) 3609 return -1; 3610 3611 vpx_usec_timer_start(&cmptimer); 3612 3613 cpi->source = NULL; 3614 3615 cpi->mb.e_mbd.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV; 3616 set_mvcost(&cpi->mb); 3617 3618 // Should we code an alternate reference frame. 3619 if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) { 3620 int frames_to_arf; 3621 3622 #if CONFIG_MULTIPLE_ARF 3623 assert(!cpi->multi_arf_enabled || 3624 cpi->frame_coding_order[cpi->sequence_number] < 0); 3625 3626 if (cpi->multi_arf_enabled && (cpi->pass == 2)) 3627 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) 3628 - cpi->next_frame_in_order; 3629 else 3630 #endif 3631 frames_to_arf = cpi->frames_till_gf_update_due; 3632 3633 assert(frames_to_arf < cpi->twopass.frames_to_key); 3634 3635 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { 3636 #if CONFIG_MULTIPLE_ARF 3637 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; 3638 #else 3639 cpi->alt_ref_source = cpi->source; 3640 #endif 3641 3642 if (cpi->oxcf.arnr_max_frames > 0) { 3643 // Produce the filtered ARF frame. 3644 // TODO(agrange) merge these two functions. 3645 configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf, 3646 cpi->gfu_boost); 3647 vp9_temporal_filter_prepare(cpi, frames_to_arf); 3648 vp9_extend_frame_borders(&cpi->alt_ref_buffer, 3649 cm->subsampling_x, cm->subsampling_y); 3650 force_src_buffer = &cpi->alt_ref_buffer; 3651 } 3652 3653 cm->show_frame = 0; 3654 cm->intra_only = 0; 3655 cpi->refresh_alt_ref_frame = 1; 3656 cpi->refresh_golden_frame = 0; 3657 cpi->refresh_last_frame = 0; 3658 cpi->is_src_frame_alt_ref = 0; 3659 3660 // TODO(agrange) This needs to vary depending on where the next ARF is. 3661 cpi->frames_till_alt_ref_frame = frames_to_arf; 3662 3663 #if CONFIG_MULTIPLE_ARF 3664 if (!cpi->multi_arf_enabled) 3665 #endif 3666 cpi->source_alt_ref_pending = 0; // Clear Pending altf Ref flag. 3667 } 3668 } 3669 3670 if (!cpi->source) { 3671 #if CONFIG_MULTIPLE_ARF 3672 int i; 3673 #endif 3674 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) { 3675 cm->show_frame = 1; 3676 3677 #if CONFIG_MULTIPLE_ARF 3678 // Is this frame the ARF overlay. 3679 cpi->is_src_frame_alt_ref = 0; 3680 for (i = 0; i < cpi->arf_buffered; ++i) { 3681 if (cpi->source == cpi->alt_ref_source[i]) { 3682 cpi->is_src_frame_alt_ref = 1; 3683 cpi->refresh_golden_frame = 1; 3684 break; 3685 } 3686 } 3687 #else 3688 cpi->is_src_frame_alt_ref = cpi->alt_ref_source 3689 && (cpi->source == cpi->alt_ref_source); 3690 #endif 3691 if (cpi->is_src_frame_alt_ref) { 3692 // Current frame is an ARF overlay frame. 3693 #if CONFIG_MULTIPLE_ARF 3694 cpi->alt_ref_source[i] = NULL; 3695 #else 3696 cpi->alt_ref_source = NULL; 3697 #endif 3698 // Don't refresh the last buffer for an ARF overlay frame. It will 3699 // become the GF so preserve last as an alternative prediction option. 3700 cpi->refresh_last_frame = 0; 3701 } 3702 #if CONFIG_MULTIPLE_ARF 3703 ++cpi->next_frame_in_order; 3704 #endif 3705 } 3706 } 3707 3708 if (cpi->source) { 3709 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer 3710 : &cpi->source->img; 3711 *time_stamp = cpi->source->ts_start; 3712 *time_end = cpi->source->ts_end; 3713 *frame_flags = cpi->source->flags; 3714 3715 // fprintf(fp_out, " Frame:%d", cm->current_video_frame); 3716 #if CONFIG_MULTIPLE_ARF 3717 if (cpi->multi_arf_enabled) { 3718 // fprintf(fp_out, " seq_no:%d this_frame_weight:%d", 3719 // cpi->sequence_number, cpi->this_frame_weight); 3720 } else { 3721 // fprintf(fp_out, "\n"); 3722 } 3723 #else 3724 // fprintf(fp_out, "\n"); 3725 #endif 3726 3727 #if CONFIG_MULTIPLE_ARF 3728 if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2)) 3729 cpi->source_alt_ref_pending = is_next_frame_arf(cpi); 3730 #endif 3731 } else { 3732 *size = 0; 3733 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) { 3734 vp9_end_first_pass(cpi); /* get last stats packet */ 3735 cpi->twopass.first_pass_done = 1; 3736 } 3737 3738 // fclose(fp_out); 3739 return -1; 3740 } 3741 3742 if (cpi->source->ts_start < cpi->first_time_stamp_ever) { 3743 cpi->first_time_stamp_ever = cpi->source->ts_start; 3744 cpi->last_end_time_stamp_seen = cpi->source->ts_start; 3745 } 3746 3747 // adjust frame rates based on timestamps given 3748 if (!cpi->refresh_alt_ref_frame) { 3749 int64_t this_duration; 3750 int step = 0; 3751 3752 if (cpi->source->ts_start == cpi->first_time_stamp_ever) { 3753 this_duration = cpi->source->ts_end - cpi->source->ts_start; 3754 step = 1; 3755 } else { 3756 int64_t last_duration = cpi->last_end_time_stamp_seen 3757 - cpi->last_time_stamp_seen; 3758 3759 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen; 3760 3761 // do a step update if the duration changes by 10% 3762 if (last_duration) 3763 step = (int)((this_duration - last_duration) * 10 / last_duration); 3764 } 3765 3766 if (this_duration) { 3767 if (step) { 3768 vp9_new_framerate(cpi, 10000000.0 / this_duration); 3769 } else { 3770 // Average this frame's rate into the last second's average 3771 // frame rate. If we haven't seen 1 second yet, then average 3772 // over the whole interval seen. 3773 const double interval = MIN((double)(cpi->source->ts_end 3774 - cpi->first_time_stamp_ever), 10000000.0); 3775 double avg_duration = 10000000.0 / cpi->oxcf.framerate; 3776 avg_duration *= (interval - avg_duration + this_duration); 3777 avg_duration /= interval; 3778 3779 vp9_new_framerate(cpi, 10000000.0 / avg_duration); 3780 } 3781 } 3782 3783 cpi->last_time_stamp_seen = cpi->source->ts_start; 3784 cpi->last_end_time_stamp_seen = cpi->source->ts_end; 3785 } 3786 3787 // start with a 0 size frame 3788 *size = 0; 3789 3790 // Clear down mmx registers 3791 vp9_clear_system_state(); // __asm emms; 3792 3793 /* find a free buffer for the new frame, releasing the reference previously 3794 * held. 3795 */ 3796 cm->fb_idx_ref_cnt[cm->new_fb_idx]--; 3797 cm->new_fb_idx = get_free_fb(cm); 3798 3799 #if CONFIG_MULTIPLE_ARF 3800 /* Set up the correct ARF frame. */ 3801 if (cpi->refresh_alt_ref_frame) { 3802 ++cpi->arf_buffered; 3803 } 3804 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 3805 (cpi->pass == 2)) { 3806 cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number]; 3807 } 3808 #endif 3809 3810 /* Get the mapping of L/G/A to the reference buffer pool */ 3811 cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx]; 3812 cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx]; 3813 cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx]; 3814 3815 #if 0 // CONFIG_MULTIPLE_ARF 3816 if (cpi->multi_arf_enabled) { 3817 fprintf(fp_out, " idx(%d, %d, %d, %d) active(%d, %d, %d)", 3818 cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx, 3819 cm->active_ref_idx[0], cm->active_ref_idx[1], cm->active_ref_idx[2]); 3820 if (cpi->refresh_alt_ref_frame) 3821 fprintf(fp_out, " type:ARF"); 3822 if (cpi->is_src_frame_alt_ref) 3823 fprintf(fp_out, " type:OVERLAY[%d]", cpi->alt_fb_idx); 3824 fprintf(fp_out, "\n"); 3825 } 3826 #endif 3827 3828 cm->frame_type = INTER_FRAME; 3829 cm->frame_flags = *frame_flags; 3830 3831 // Reset the frame pointers to the current frame size 3832 vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx], 3833 cm->width, cm->height, 3834 cm->subsampling_x, cm->subsampling_y, 3835 VP9BORDERINPIXELS); 3836 3837 // Calculate scaling factors for each of the 3 available references 3838 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) 3839 vp9_setup_scale_factors(cm, i); 3840 3841 vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm); 3842 3843 if (cpi->pass == 1) { 3844 Pass1Encode(cpi, size, dest, frame_flags); 3845 } else if (cpi->pass == 2) { 3846 Pass2Encode(cpi, size, dest, frame_flags); 3847 } else { 3848 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 3849 } 3850 3851 if (cm->refresh_frame_context) 3852 cm->frame_contexts[cm->frame_context_idx] = cm->fc; 3853 3854 if (*size > 0) { 3855 // if its a dropped frame honor the requests on subsequent frames 3856 cpi->droppable = !frame_is_reference(cpi); 3857 3858 // return to normal state 3859 cm->reset_frame_context = 0; 3860 cm->refresh_frame_context = 1; 3861 cpi->refresh_alt_ref_frame = 0; 3862 cpi->refresh_golden_frame = 0; 3863 cpi->refresh_last_frame = 1; 3864 cm->frame_type = INTER_FRAME; 3865 } 3866 3867 vpx_usec_timer_mark(&cmptimer); 3868 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); 3869 3870 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) 3871 generate_psnr_packet(cpi); 3872 3873 #if CONFIG_INTERNAL_STATS 3874 3875 if (cpi->pass != 1) { 3876 cpi->bytes += *size; 3877 3878 if (cm->show_frame) { 3879 3880 cpi->count++; 3881 3882 if (cpi->b_calculate_psnr) { 3883 double ye, ue, ve; 3884 double frame_psnr; 3885 YV12_BUFFER_CONFIG *orig = cpi->Source; 3886 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 3887 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer; 3888 int y_samples = orig->y_height * orig->y_width; 3889 int uv_samples = orig->uv_height * orig->uv_width; 3890 int t_samples = y_samples + 2 * uv_samples; 3891 double sq_error; 3892 3893 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, 3894 recon->y_buffer, recon->y_stride, 3895 orig->y_crop_width, orig->y_crop_height); 3896 3897 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, 3898 recon->u_buffer, recon->uv_stride, 3899 orig->uv_crop_width, orig->uv_crop_height); 3900 3901 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, 3902 recon->v_buffer, recon->uv_stride, 3903 orig->uv_crop_width, orig->uv_crop_height); 3904 3905 sq_error = ye + ue + ve; 3906 3907 frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error); 3908 3909 cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye); 3910 cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue); 3911 cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve); 3912 cpi->total_sq_error += sq_error; 3913 cpi->total += frame_psnr; 3914 { 3915 double frame_psnr2, frame_ssim2 = 0; 3916 double weight = 0; 3917 #if CONFIG_VP9_POSTPROC 3918 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer, 3919 cm->lf.filter_level * 10 / 6); 3920 #endif 3921 vp9_clear_system_state(); 3922 3923 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, 3924 pp->y_buffer, pp->y_stride, 3925 orig->y_crop_width, orig->y_crop_height); 3926 3927 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, 3928 pp->u_buffer, pp->uv_stride, 3929 orig->uv_crop_width, orig->uv_crop_height); 3930 3931 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, 3932 pp->v_buffer, pp->uv_stride, 3933 orig->uv_crop_width, orig->uv_crop_height); 3934 3935 sq_error = ye + ue + ve; 3936 3937 frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error); 3938 3939 cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye); 3940 cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue); 3941 cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve); 3942 cpi->total_sq_error2 += sq_error; 3943 cpi->totalp += frame_psnr2; 3944 3945 frame_ssim2 = vp9_calc_ssim(cpi->Source, 3946 recon, 1, &weight); 3947 3948 cpi->summed_quality += frame_ssim2 * weight; 3949 cpi->summed_weights += weight; 3950 3951 frame_ssim2 = vp9_calc_ssim(cpi->Source, 3952 &cm->post_proc_buffer, 1, &weight); 3953 3954 cpi->summedp_quality += frame_ssim2 * weight; 3955 cpi->summedp_weights += weight; 3956 #if 0 3957 { 3958 FILE *f = fopen("q_used.stt", "a"); 3959 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n", 3960 cpi->common.current_video_frame, y2, u2, v2, 3961 frame_psnr2, frame_ssim2); 3962 fclose(f); 3963 } 3964 #endif 3965 } 3966 } 3967 3968 if (cpi->b_calculate_ssimg) { 3969 double y, u, v, frame_all; 3970 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, 3971 &y, &u, &v); 3972 cpi->total_ssimg_y += y; 3973 cpi->total_ssimg_u += u; 3974 cpi->total_ssimg_v += v; 3975 cpi->total_ssimg_all += frame_all; 3976 } 3977 } 3978 } 3979 3980 #endif 3981 // fclose(fp_out); 3982 return 0; 3983 } 3984 3985 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest, 3986 vp9_ppflags_t *flags) { 3987 VP9_COMP *cpi = (VP9_COMP *) comp; 3988 3989 if (!cpi->common.show_frame) 3990 return -1; 3991 else { 3992 int ret; 3993 #if CONFIG_VP9_POSTPROC 3994 ret = vp9_post_proc_frame(&cpi->common, dest, flags); 3995 #else 3996 3997 if (cpi->common.frame_to_show) { 3998 *dest = *cpi->common.frame_to_show; 3999 dest->y_width = cpi->common.width; 4000 dest->y_height = cpi->common.height; 4001 dest->uv_height = cpi->common.height / 2; 4002 ret = 0; 4003 } else { 4004 ret = -1; 4005 } 4006 4007 #endif // !CONFIG_VP9_POSTPROC 4008 vp9_clear_system_state(); 4009 return ret; 4010 } 4011 } 4012 4013 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows, 4014 unsigned int cols, int delta_q[MAX_SEGMENTS], 4015 int delta_lf[MAX_SEGMENTS], 4016 unsigned int threshold[MAX_SEGMENTS]) { 4017 VP9_COMP *cpi = (VP9_COMP *) comp; 4018 signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS]; 4019 struct segmentation *seg = &cpi->common.seg; 4020 int i; 4021 4022 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) 4023 return -1; 4024 4025 if (!map) { 4026 vp9_disable_segmentation((VP9_PTR)cpi); 4027 return 0; 4028 } 4029 4030 // Set the segmentation Map 4031 vp9_set_segmentation_map((VP9_PTR)cpi, map); 4032 4033 // Activate segmentation. 4034 vp9_enable_segmentation((VP9_PTR)cpi); 4035 4036 // Set up the quant, LF and breakout threshold segment data 4037 for (i = 0; i < MAX_SEGMENTS; i++) { 4038 feature_data[SEG_LVL_ALT_Q][i] = delta_q[i]; 4039 feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i]; 4040 cpi->segment_encode_breakout[i] = threshold[i]; 4041 } 4042 4043 // Enable the loop and quant changes in the feature mask 4044 for (i = 0; i < MAX_SEGMENTS; i++) { 4045 if (delta_q[i]) 4046 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q); 4047 else 4048 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q); 4049 4050 if (delta_lf[i]) 4051 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF); 4052 else 4053 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF); 4054 } 4055 4056 // Initialize the feature data structure 4057 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 4058 vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA); 4059 4060 return 0; 4061 } 4062 4063 int vp9_set_active_map(VP9_PTR comp, unsigned char *map, 4064 unsigned int rows, unsigned int cols) { 4065 VP9_COMP *cpi = (VP9_COMP *) comp; 4066 4067 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) { 4068 if (map) { 4069 vpx_memcpy(cpi->active_map, map, rows * cols); 4070 cpi->active_map_enabled = 1; 4071 } else { 4072 cpi->active_map_enabled = 0; 4073 } 4074 4075 return 0; 4076 } else { 4077 // cpi->active_map_enabled = 0; 4078 return -1; 4079 } 4080 } 4081 4082 int vp9_set_internal_size(VP9_PTR comp, 4083 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) { 4084 VP9_COMP *cpi = (VP9_COMP *) comp; 4085 VP9_COMMON *cm = &cpi->common; 4086 int hr = 0, hs = 0, vr = 0, vs = 0; 4087 4088 if (horiz_mode > ONETWO || vert_mode > ONETWO) 4089 return -1; 4090 4091 Scale2Ratio(horiz_mode, &hr, &hs); 4092 Scale2Ratio(vert_mode, &vr, &vs); 4093 4094 // always go to the next whole number 4095 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs; 4096 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs; 4097 4098 assert(cm->width <= cpi->initial_width); 4099 assert(cm->height <= cpi->initial_height); 4100 update_frame_size(cpi); 4101 return 0; 4102 } 4103 4104 int vp9_set_size_literal(VP9_PTR comp, unsigned int width, 4105 unsigned int height) { 4106 VP9_COMP *cpi = (VP9_COMP *)comp; 4107 VP9_COMMON *cm = &cpi->common; 4108 4109 check_initial_width(cpi, NULL); 4110 4111 if (width) { 4112 cm->width = width; 4113 if (cm->width * 5 < cpi->initial_width) { 4114 cm->width = cpi->initial_width / 5 + 1; 4115 printf("Warning: Desired width too small, changed to %d \n", cm->width); 4116 } 4117 if (cm->width > cpi->initial_width) { 4118 cm->width = cpi->initial_width; 4119 printf("Warning: Desired width too large, changed to %d \n", cm->width); 4120 } 4121 } 4122 4123 if (height) { 4124 cm->height = height; 4125 if (cm->height * 5 < cpi->initial_height) { 4126 cm->height = cpi->initial_height / 5 + 1; 4127 printf("Warning: Desired height too small, changed to %d \n", cm->height); 4128 } 4129 if (cm->height > cpi->initial_height) { 4130 cm->height = cpi->initial_height; 4131 printf("Warning: Desired height too large, changed to %d \n", cm->height); 4132 } 4133 } 4134 4135 assert(cm->width <= cpi->initial_width); 4136 assert(cm->height <= cpi->initial_height); 4137 update_frame_size(cpi); 4138 return 0; 4139 } 4140 4141 int vp9_switch_layer(VP9_PTR comp, int layer) { 4142 VP9_COMP *cpi = (VP9_COMP *)comp; 4143 4144 if (cpi->use_svc) { 4145 cpi->current_layer = layer; 4146 4147 // Use buffer i for layer i LST 4148 cpi->lst_fb_idx = layer; 4149 4150 // Use buffer i-1 for layer i Alt (Inter-layer prediction) 4151 if (layer != 0) cpi->alt_fb_idx = layer - 1; 4152 4153 // Use the rest for Golden 4154 if (layer < 2 * cpi->number_spatial_layers - NUM_REF_FRAMES) 4155 cpi->gld_fb_idx = cpi->lst_fb_idx; 4156 else 4157 cpi->gld_fb_idx = 2 * cpi->number_spatial_layers - 1 - layer; 4158 4159 printf("Switching to layer %d:\n", layer); 4160 printf("Using references: LST/GLD/ALT [%d|%d|%d]\n", cpi->lst_fb_idx, 4161 cpi->gld_fb_idx, cpi->alt_fb_idx); 4162 } else { 4163 printf("Switching layer not supported. Enable SVC first \n"); 4164 } 4165 return 0; 4166 } 4167 4168 void vp9_set_svc(VP9_PTR comp, int use_svc) { 4169 VP9_COMP *cpi = (VP9_COMP *)comp; 4170 cpi->use_svc = use_svc; 4171 if (cpi->use_svc) printf("Enabled SVC encoder \n"); 4172 return; 4173 } 4174 4175 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) { 4176 int i, j; 4177 int total = 0; 4178 4179 uint8_t *src = source->y_buffer; 4180 uint8_t *dst = dest->y_buffer; 4181 4182 // Loop through the Y plane raw and reconstruction data summing 4183 // (square differences) 4184 for (i = 0; i < source->y_height; i += 16) { 4185 for (j = 0; j < source->y_width; j += 16) { 4186 unsigned int sse; 4187 total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, 4188 &sse); 4189 } 4190 4191 src += 16 * source->y_stride; 4192 dst += 16 * dest->y_stride; 4193 } 4194 4195 return total; 4196 } 4197 4198 4199 int vp9_get_quantizer(VP9_PTR c) { 4200 return ((VP9_COMP *)c)->common.base_qindex; 4201 } 4202