1/* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#include <math.h> 12#include <stdio.h> 13#include <limits.h> 14 15#include "./vpx_config.h" 16#include "./vpx_scale_rtcd.h" 17#include "vpx/internal/vpx_psnr.h" 18#include "vpx_ports/vpx_timer.h" 19 20#include "vp9/common/vp9_alloccommon.h" 21#include "vp9/common/vp9_filter.h" 22#include "vp9/common/vp9_idct.h" 23#if CONFIG_VP9_POSTPROC 24#include "vp9/common/vp9_postproc.h" 25#endif 26#include "vp9/common/vp9_reconinter.h" 27#include "vp9/common/vp9_systemdependent.h" 28#include "vp9/common/vp9_tile_common.h" 29 30#include "vp9/encoder/vp9_aq_complexity.h" 31#include "vp9/encoder/vp9_aq_cyclicrefresh.h" 32#include "vp9/encoder/vp9_aq_variance.h" 33#include "vp9/encoder/vp9_bitstream.h" 34#include "vp9/encoder/vp9_encodeframe.h" 35#include "vp9/encoder/vp9_encodemv.h" 36#include "vp9/encoder/vp9_firstpass.h" 37#include "vp9/encoder/vp9_mbgraph.h" 38#include "vp9/encoder/vp9_onyx_int.h" 39#include "vp9/encoder/vp9_picklpf.h" 40#include "vp9/encoder/vp9_ratectrl.h" 41#include "vp9/encoder/vp9_rdopt.h" 42#include "vp9/encoder/vp9_segmentation.h" 43#include "vp9/encoder/vp9_speed_features.h" 44#if CONFIG_INTERNAL_STATS 45#include "vp9/encoder/vp9_ssim.h" 46#endif 47#include "vp9/encoder/vp9_temporal_filter.h" 48#include "vp9/encoder/vp9_resize.h" 49#include "vp9/encoder/vp9_svc_layercontext.h" 50 51void vp9_coef_tree_initialize(); 52 53#define DEFAULT_INTERP_FILTER SWITCHABLE 54 55#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ 56 57#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv 58 // for altref computation. 59#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision 60 // mv. Choose a very high value for 61 // now so that HIGH_PRECISION is always 62 // chosen. 63 64// Max rate target for 1080P and below encodes under normal circumstances 65// (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB 66#define MAX_MB_RATE 250 67#define MAXRATE_1080P 2025000 68 69// #define OUTPUT_YUV_REC 70 71#ifdef OUTPUT_YUV_SRC 72FILE *yuv_file; 73#endif 74#ifdef OUTPUT_YUV_REC 75FILE *yuv_rec_file; 76#endif 77 78#if 0 79FILE *framepsnr; 80FILE *kf_list; 81FILE *keyfile; 82#endif 83 84void vp9_init_quantizer(VP9_COMP *cpi); 85 86static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) { 87 switch (mode) { 88 case NORMAL: 89 *hr = 1; 90 *hs = 1; 91 break; 92 case FOURFIVE: 93 *hr = 4; 94 *hs = 5; 95 break; 96 case THREEFIVE: 97 *hr = 3; 98 *hs = 5; 99 break; 100 case ONETWO: 101 *hr = 1; 102 *hs = 2; 103 break; 104 default: 105 *hr = 1; 106 *hs = 1; 107 assert(0); 108 break; 109 } 110} 111 112static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) { 113 MACROBLOCK *const mb = &cpi->mb; 114 cpi->common.allow_high_precision_mv = allow_high_precision_mv; 115 if (cpi->common.allow_high_precision_mv) { 116 mb->mvcost = mb->nmvcost_hp; 117 mb->mvsadcost = mb->nmvsadcost_hp; 118 } else { 119 mb->mvcost = mb->nmvcost; 120 mb->mvsadcost = mb->nmvsadcost; 121 } 122} 123 124static void setup_key_frame(VP9_COMP *cpi) { 125 vp9_setup_past_independence(&cpi->common); 126 127 // All buffers are implicitly updated on key frames. 128 cpi->refresh_golden_frame = 1; 129 cpi->refresh_alt_ref_frame = 1; 130} 131 132static void setup_inter_frame(VP9_COMMON *cm) { 133 if (cm->error_resilient_mode || cm->intra_only) 134 vp9_setup_past_independence(cm); 135 136 assert(cm->frame_context_idx < FRAME_CONTEXTS); 137 cm->fc = cm->frame_contexts[cm->frame_context_idx]; 138} 139 140void vp9_initialize_enc() { 141 static int init_done = 0; 142 143 if (!init_done) { 144 vp9_init_neighbors(); 145 vp9_init_quant_tables(); 146 147 vp9_coef_tree_initialize(); 148 vp9_tokenize_initialize(); 149 vp9_init_me_luts(); 150 vp9_rc_init_minq_luts(); 151 vp9_entropy_mv_init(); 152 vp9_entropy_mode_init(); 153 init_done = 1; 154 } 155} 156 157static void dealloc_compressor_data(VP9_COMP *cpi) { 158 VP9_COMMON *const cm = &cpi->common; 159 int i; 160 161 // Delete sementation map 162 vpx_free(cpi->segmentation_map); 163 cpi->segmentation_map = NULL; 164 vpx_free(cm->last_frame_seg_map); 165 cm->last_frame_seg_map = NULL; 166 vpx_free(cpi->coding_context.last_frame_seg_map_copy); 167 cpi->coding_context.last_frame_seg_map_copy = NULL; 168 169 vpx_free(cpi->complexity_map); 170 cpi->complexity_map = NULL; 171 172 vp9_cyclic_refresh_free(cpi->cyclic_refresh); 173 cpi->cyclic_refresh = NULL; 174 175 vpx_free(cpi->active_map); 176 cpi->active_map = NULL; 177 178 vp9_free_frame_buffers(cm); 179 180 vp9_free_frame_buffer(&cpi->last_frame_uf); 181 vp9_free_frame_buffer(&cpi->scaled_source); 182 vp9_free_frame_buffer(&cpi->scaled_last_source); 183 vp9_free_frame_buffer(&cpi->alt_ref_buffer); 184 vp9_lookahead_destroy(cpi->lookahead); 185 186 vpx_free(cpi->tok); 187 cpi->tok = 0; 188 189 // Activity mask based per mb zbin adjustments 190 vpx_free(cpi->mb_activity_map); 191 cpi->mb_activity_map = 0; 192 vpx_free(cpi->mb_norm_activity_map); 193 cpi->mb_norm_activity_map = 0; 194 195 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) { 196 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i]; 197 vpx_free(lc->rc_twopass_stats_in.buf); 198 lc->rc_twopass_stats_in.buf = NULL; 199 lc->rc_twopass_stats_in.sz = 0; 200 } 201} 202 203static void save_coding_context(VP9_COMP *cpi) { 204 CODING_CONTEXT *const cc = &cpi->coding_context; 205 VP9_COMMON *cm = &cpi->common; 206 207 // Stores a snapshot of key state variables which can subsequently be 208 // restored with a call to vp9_restore_coding_context. These functions are 209 // intended for use in a re-code loop in vp9_compress_frame where the 210 // quantizer value is adjusted between loop iterations. 211 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost); 212 vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts); 213 vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp); 214 215 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs); 216 217 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy, 218 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols)); 219 220 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas); 221 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas); 222 223 cc->fc = cm->fc; 224} 225 226static void restore_coding_context(VP9_COMP *cpi) { 227 CODING_CONTEXT *const cc = &cpi->coding_context; 228 VP9_COMMON *cm = &cpi->common; 229 230 // Restore key state variables to the snapshot state stored in the 231 // previous call to vp9_save_coding_context. 232 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost); 233 vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts); 234 vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp); 235 236 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs); 237 238 vpx_memcpy(cm->last_frame_seg_map, 239 cpi->coding_context.last_frame_seg_map_copy, 240 (cm->mi_rows * cm->mi_cols)); 241 242 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas); 243 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas); 244 245 cm->fc = cc->fc; 246} 247 248static void configure_static_seg_features(VP9_COMP *cpi) { 249 VP9_COMMON *const cm = &cpi->common; 250 const RATE_CONTROL *const rc = &cpi->rc; 251 struct segmentation *const seg = &cm->seg; 252 253 int high_q = (int)(rc->avg_q > 48.0); 254 int qi_delta; 255 256 // Disable and clear down for KF 257 if (cm->frame_type == KEY_FRAME) { 258 // Clear down the global segmentation map 259 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 260 seg->update_map = 0; 261 seg->update_data = 0; 262 cpi->static_mb_pct = 0; 263 264 // Disable segmentation 265 vp9_disable_segmentation(seg); 266 267 // Clear down the segment features. 268 vp9_clearall_segfeatures(seg); 269 } else if (cpi->refresh_alt_ref_frame) { 270 // If this is an alt ref frame 271 // Clear down the global segmentation map 272 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 273 seg->update_map = 0; 274 seg->update_data = 0; 275 cpi->static_mb_pct = 0; 276 277 // Disable segmentation and individual segment features by default 278 vp9_disable_segmentation(seg); 279 vp9_clearall_segfeatures(seg); 280 281 // Scan frames from current to arf frame. 282 // This function re-enables segmentation if appropriate. 283 vp9_update_mbgraph_stats(cpi); 284 285 // If segmentation was enabled set those features needed for the 286 // arf itself. 287 if (seg->enabled) { 288 seg->update_map = 1; 289 seg->update_data = 1; 290 291 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875); 292 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2); 293 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 294 295 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 296 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 297 298 // Where relevant assume segment data is delta data 299 seg->abs_delta = SEGMENT_DELTADATA; 300 } 301 } else if (seg->enabled) { 302 // All other frames if segmentation has been enabled 303 304 // First normal frame in a valid gf or alt ref group 305 if (rc->frames_since_golden == 0) { 306 // Set up segment features for normal frames in an arf group 307 if (rc->source_alt_ref_active) { 308 seg->update_map = 0; 309 seg->update_data = 1; 310 seg->abs_delta = SEGMENT_DELTADATA; 311 312 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125); 313 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2); 314 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 315 316 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 317 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 318 319 // Segment coding disabled for compred testing 320 if (high_q || (cpi->static_mb_pct == 100)) { 321 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 322 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 323 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 324 } 325 } else { 326 // Disable segmentation and clear down features if alt ref 327 // is not active for this group 328 329 vp9_disable_segmentation(seg); 330 331 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 332 333 seg->update_map = 0; 334 seg->update_data = 0; 335 336 vp9_clearall_segfeatures(seg); 337 } 338 } else if (rc->is_src_frame_alt_ref) { 339 // Special case where we are coding over the top of a previous 340 // alt ref frame. 341 // Segment coding disabled for compred testing 342 343 // Enable ref frame features for segment 0 as well 344 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME); 345 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 346 347 // All mbs should use ALTREF_FRAME 348 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME); 349 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME); 350 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME); 351 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 352 353 // Skip all MBs if high Q (0,0 mv and skip coeffs) 354 if (high_q) { 355 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP); 356 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 357 } 358 // Enable data update 359 seg->update_data = 1; 360 } else { 361 // All other frames. 362 363 // No updates.. leave things as they are. 364 seg->update_map = 0; 365 seg->update_data = 0; 366 } 367 } 368} 369 370// DEBUG: Print out the segment id of each MB in the current frame. 371static void print_seg_map(VP9_COMP *cpi) { 372 VP9_COMMON *cm = &cpi->common; 373 int row, col; 374 int map_index = 0; 375 FILE *statsfile = fopen("segmap.stt", "a"); 376 377 fprintf(statsfile, "%10d\n", cm->current_video_frame); 378 379 for (row = 0; row < cpi->common.mi_rows; row++) { 380 for (col = 0; col < cpi->common.mi_cols; col++) { 381 fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]); 382 map_index++; 383 } 384 fprintf(statsfile, "\n"); 385 } 386 fprintf(statsfile, "\n"); 387 388 fclose(statsfile); 389} 390 391static void update_reference_segmentation_map(VP9_COMP *cpi) { 392 VP9_COMMON *const cm = &cpi->common; 393 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible; 394 uint8_t *cache_ptr = cm->last_frame_seg_map; 395 int row, col; 396 397 for (row = 0; row < cm->mi_rows; row++) { 398 MODE_INFO **mi_8x8 = mi_8x8_ptr; 399 uint8_t *cache = cache_ptr; 400 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++) 401 cache[0] = mi_8x8[0]->mbmi.segment_id; 402 mi_8x8_ptr += cm->mi_stride; 403 cache_ptr += cm->mi_cols; 404 } 405} 406static int is_slowest_mode(int mode) { 407 return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY); 408} 409 410static void set_rd_speed_thresholds(VP9_COMP *cpi) { 411 int i; 412 413 // Set baseline threshold values 414 for (i = 0; i < MAX_MODES; ++i) 415 cpi->rd_thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0; 416 417 cpi->rd_thresh_mult[THR_NEARESTMV] = 0; 418 cpi->rd_thresh_mult[THR_NEARESTG] = 0; 419 cpi->rd_thresh_mult[THR_NEARESTA] = 0; 420 421 cpi->rd_thresh_mult[THR_DC] += 1000; 422 423 cpi->rd_thresh_mult[THR_NEWMV] += 1000; 424 cpi->rd_thresh_mult[THR_NEWA] += 1000; 425 cpi->rd_thresh_mult[THR_NEWG] += 1000; 426 427 cpi->rd_thresh_mult[THR_NEARMV] += 1000; 428 cpi->rd_thresh_mult[THR_NEARA] += 1000; 429 cpi->rd_thresh_mult[THR_COMP_NEARESTLA] += 1000; 430 cpi->rd_thresh_mult[THR_COMP_NEARESTGA] += 1000; 431 432 cpi->rd_thresh_mult[THR_TM] += 1000; 433 434 cpi->rd_thresh_mult[THR_COMP_NEARLA] += 1500; 435 cpi->rd_thresh_mult[THR_COMP_NEWLA] += 2000; 436 cpi->rd_thresh_mult[THR_NEARG] += 1000; 437 cpi->rd_thresh_mult[THR_COMP_NEARGA] += 1500; 438 cpi->rd_thresh_mult[THR_COMP_NEWGA] += 2000; 439 440 cpi->rd_thresh_mult[THR_ZEROMV] += 2000; 441 cpi->rd_thresh_mult[THR_ZEROG] += 2000; 442 cpi->rd_thresh_mult[THR_ZEROA] += 2000; 443 cpi->rd_thresh_mult[THR_COMP_ZEROLA] += 2500; 444 cpi->rd_thresh_mult[THR_COMP_ZEROGA] += 2500; 445 446 cpi->rd_thresh_mult[THR_H_PRED] += 2000; 447 cpi->rd_thresh_mult[THR_V_PRED] += 2000; 448 cpi->rd_thresh_mult[THR_D45_PRED ] += 2500; 449 cpi->rd_thresh_mult[THR_D135_PRED] += 2500; 450 cpi->rd_thresh_mult[THR_D117_PRED] += 2500; 451 cpi->rd_thresh_mult[THR_D153_PRED] += 2500; 452 cpi->rd_thresh_mult[THR_D207_PRED] += 2500; 453 cpi->rd_thresh_mult[THR_D63_PRED] += 2500; 454 455 /* disable frame modes if flags not set */ 456 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) { 457 cpi->rd_thresh_mult[THR_NEWMV ] = INT_MAX; 458 cpi->rd_thresh_mult[THR_NEARESTMV] = INT_MAX; 459 cpi->rd_thresh_mult[THR_ZEROMV ] = INT_MAX; 460 cpi->rd_thresh_mult[THR_NEARMV ] = INT_MAX; 461 } 462 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) { 463 cpi->rd_thresh_mult[THR_NEARESTG ] = INT_MAX; 464 cpi->rd_thresh_mult[THR_ZEROG ] = INT_MAX; 465 cpi->rd_thresh_mult[THR_NEARG ] = INT_MAX; 466 cpi->rd_thresh_mult[THR_NEWG ] = INT_MAX; 467 } 468 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) { 469 cpi->rd_thresh_mult[THR_NEARESTA ] = INT_MAX; 470 cpi->rd_thresh_mult[THR_ZEROA ] = INT_MAX; 471 cpi->rd_thresh_mult[THR_NEARA ] = INT_MAX; 472 cpi->rd_thresh_mult[THR_NEWA ] = INT_MAX; 473 } 474 475 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != 476 (VP9_LAST_FLAG | VP9_ALT_FLAG)) { 477 cpi->rd_thresh_mult[THR_COMP_ZEROLA ] = INT_MAX; 478 cpi->rd_thresh_mult[THR_COMP_NEARESTLA] = INT_MAX; 479 cpi->rd_thresh_mult[THR_COMP_NEARLA ] = INT_MAX; 480 cpi->rd_thresh_mult[THR_COMP_NEWLA ] = INT_MAX; 481 } 482 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != 483 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) { 484 cpi->rd_thresh_mult[THR_COMP_ZEROGA ] = INT_MAX; 485 cpi->rd_thresh_mult[THR_COMP_NEARESTGA] = INT_MAX; 486 cpi->rd_thresh_mult[THR_COMP_NEARGA ] = INT_MAX; 487 cpi->rd_thresh_mult[THR_COMP_NEWGA ] = INT_MAX; 488 } 489} 490 491static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) { 492 const SPEED_FEATURES *const sf = &cpi->sf; 493 int i; 494 495 for (i = 0; i < MAX_REFS; ++i) 496 cpi->rd_thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0; 497 498 cpi->rd_thresh_mult_sub8x8[THR_LAST] += 2500; 499 cpi->rd_thresh_mult_sub8x8[THR_GOLD] += 2500; 500 cpi->rd_thresh_mult_sub8x8[THR_ALTR] += 2500; 501 cpi->rd_thresh_mult_sub8x8[THR_INTRA] += 2500; 502 cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] += 4500; 503 cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] += 4500; 504 505 // Check for masked out split cases. 506 for (i = 0; i < MAX_REFS; i++) 507 if (sf->disable_split_mask & (1 << i)) 508 cpi->rd_thresh_mult_sub8x8[i] = INT_MAX; 509 510 // disable mode test if frame flag is not set 511 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) 512 cpi->rd_thresh_mult_sub8x8[THR_LAST] = INT_MAX; 513 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) 514 cpi->rd_thresh_mult_sub8x8[THR_GOLD] = INT_MAX; 515 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) 516 cpi->rd_thresh_mult_sub8x8[THR_ALTR] = INT_MAX; 517 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != 518 (VP9_LAST_FLAG | VP9_ALT_FLAG)) 519 cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX; 520 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != 521 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) 522 cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX; 523} 524 525static void set_speed_features(VP9_COMP *cpi) { 526#if CONFIG_INTERNAL_STATS 527 int i; 528 for (i = 0; i < MAX_MODES; ++i) 529 cpi->mode_chosen_counts[i] = 0; 530#endif 531 532 vp9_set_speed_features(cpi); 533 534 // Set rd thresholds based on mode and speed setting 535 set_rd_speed_thresholds(cpi); 536 set_rd_speed_thresholds_sub8x8(cpi); 537 538 cpi->mb.fwd_txm4x4 = vp9_fdct4x4; 539 if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) { 540 cpi->mb.fwd_txm4x4 = vp9_fwht4x4; 541 } 542} 543 544static void alloc_raw_frame_buffers(VP9_COMP *cpi) { 545 VP9_COMMON *cm = &cpi->common; 546 const VP9_CONFIG *oxcf = &cpi->oxcf; 547 548 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height, 549 cm->subsampling_x, cm->subsampling_y, 550 oxcf->lag_in_frames); 551 if (!cpi->lookahead) 552 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 553 "Failed to allocate lag buffers"); 554 555 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, 556 oxcf->width, oxcf->height, 557 cm->subsampling_x, cm->subsampling_y, 558 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) 559 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 560 "Failed to allocate altref buffer"); 561} 562 563void vp9_alloc_compressor_data(VP9_COMP *cpi) { 564 VP9_COMMON *cm = &cpi->common; 565 566 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) 567 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 568 "Failed to allocate frame buffers"); 569 570 if (vp9_alloc_frame_buffer(&cpi->last_frame_uf, 571 cm->width, cm->height, 572 cm->subsampling_x, cm->subsampling_y, 573 VP9_ENC_BORDER_IN_PIXELS)) 574 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 575 "Failed to allocate last frame buffer"); 576 577 if (vp9_alloc_frame_buffer(&cpi->scaled_source, 578 cm->width, cm->height, 579 cm->subsampling_x, cm->subsampling_y, 580 VP9_ENC_BORDER_IN_PIXELS)) 581 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 582 "Failed to allocate scaled source buffer"); 583 584 if (vp9_alloc_frame_buffer(&cpi->scaled_last_source, 585 cm->width, cm->height, 586 cm->subsampling_x, cm->subsampling_y, 587 VP9_ENC_BORDER_IN_PIXELS)) 588 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 589 "Failed to allocate scaled last source buffer"); 590 591 vpx_free(cpi->tok); 592 593 { 594 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); 595 596 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); 597 } 598 599 vpx_free(cpi->mb_activity_map); 600 CHECK_MEM_ERROR(cm, cpi->mb_activity_map, 601 vpx_calloc(sizeof(unsigned int), 602 cm->mb_rows * cm->mb_cols)); 603 604 vpx_free(cpi->mb_norm_activity_map); 605 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, 606 vpx_calloc(sizeof(unsigned int), 607 cm->mb_rows * cm->mb_cols)); 608} 609 610 611static void update_frame_size(VP9_COMP *cpi) { 612 VP9_COMMON *const cm = &cpi->common; 613 MACROBLOCKD *const xd = &cpi->mb.e_mbd; 614 615 vp9_update_frame_size(cm); 616 617 // Update size of buffers local to this frame 618 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, 619 cm->width, cm->height, 620 cm->subsampling_x, cm->subsampling_y, 621 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) 622 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 623 "Failed to reallocate last frame buffer"); 624 625 if (vp9_realloc_frame_buffer(&cpi->scaled_source, 626 cm->width, cm->height, 627 cm->subsampling_x, cm->subsampling_y, 628 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) 629 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 630 "Failed to reallocate scaled source buffer"); 631 632 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source, 633 cm->width, cm->height, 634 cm->subsampling_x, cm->subsampling_y, 635 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) 636 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 637 "Failed to reallocate scaled last source buffer"); 638 639 { 640 int y_stride = cpi->scaled_source.y_stride; 641 642 if (cpi->sf.search_method == NSTEP) { 643 vp9_init3smotion_compensation(&cpi->mb, y_stride); 644 } else if (cpi->sf.search_method == DIAMOND) { 645 vp9_init_dsmotion_compensation(&cpi->mb, y_stride); 646 } 647 } 648 649 init_macroblockd(cm, xd); 650} 651 652// Table that converts 0-63 Q range values passed in outside to the Qindex 653// range used internally. 654const int q_trans[] = { 655 0, 4, 8, 12, 16, 20, 24, 28, 656 32, 36, 40, 44, 48, 52, 56, 60, 657 64, 68, 72, 76, 80, 84, 88, 92, 658 96, 100, 104, 108, 112, 116, 120, 124, 659 128, 132, 136, 140, 144, 148, 152, 156, 660 160, 164, 168, 172, 176, 180, 184, 188, 661 192, 196, 200, 204, 208, 212, 216, 220, 662 224, 228, 232, 236, 240, 244, 249, 255, 663}; 664 665int vp9_reverse_trans(int x) { 666 int i; 667 668 for (i = 0; i < 64; i++) 669 if (q_trans[i] >= x) 670 return i; 671 672 return 63; 673}; 674 675void vp9_new_framerate(VP9_COMP *cpi, double framerate) { 676 VP9_COMMON *const cm = &cpi->common; 677 RATE_CONTROL *const rc = &cpi->rc; 678 VP9_CONFIG *const oxcf = &cpi->oxcf; 679 int vbr_max_bits; 680 681 oxcf->framerate = framerate < 0.1 ? 30 : framerate; 682 cpi->output_framerate = cpi->oxcf.framerate; 683 rc->av_per_frame_bandwidth = (int)(oxcf->target_bandwidth / 684 cpi->output_framerate); 685 rc->min_frame_bandwidth = (int)(rc->av_per_frame_bandwidth * 686 oxcf->two_pass_vbrmin_section / 100); 687 688 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); 689 690 // A maximum bitrate for a frame is defined. 691 // The baseline for this aligns with HW implementations that 692 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits 693 // per 16x16 MB (averaged over a frame). However this limit is extended if 694 // a very high rate is given on the command line or the the rate cannnot 695 // be acheived because of a user specificed max q (e.g. when the user 696 // specifies lossless encode. 697 // 698 vbr_max_bits = (int)(((int64_t)rc->av_per_frame_bandwidth * 699 oxcf->two_pass_vbrmax_section) / 100); 700 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 701 vbr_max_bits); 702 703 // Set Maximum gf/arf interval 704 rc->max_gf_interval = 16; 705 706 // Extended interval for genuinely static scenes 707 rc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; 708 709 // Special conditions when alt ref frame enabled in lagged compress mode 710 if (oxcf->play_alternate && oxcf->lag_in_frames) { 711 if (rc->max_gf_interval > oxcf->lag_in_frames - 1) 712 rc->max_gf_interval = oxcf->lag_in_frames - 1; 713 714 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) 715 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; 716 } 717 718 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) 719 rc->max_gf_interval = rc->static_scene_max_gf_interval; 720} 721 722int64_t vp9_rescale(int64_t val, int64_t num, int denom) { 723 int64_t llnum = num; 724 int64_t llden = denom; 725 int64_t llval = val; 726 727 return (llval * llnum / llden); 728} 729 730static void set_tile_limits(VP9_COMP *cpi) { 731 VP9_COMMON *const cm = &cpi->common; 732 733 int min_log2_tile_cols, max_log2_tile_cols; 734 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); 735 736 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, 737 min_log2_tile_cols, max_log2_tile_cols); 738 cm->log2_tile_rows = cpi->oxcf.tile_rows; 739} 740 741static void init_config(struct VP9_COMP *cpi, VP9_CONFIG *oxcf) { 742 VP9_COMMON *const cm = &cpi->common; 743 int i; 744 745 cpi->oxcf = *oxcf; 746 747 cm->profile = oxcf->profile; 748 cm->bit_depth = oxcf->bit_depth; 749 750 cm->width = oxcf->width; 751 cm->height = oxcf->height; 752 cm->subsampling_x = 0; 753 cm->subsampling_y = 0; 754 vp9_alloc_compressor_data(cpi); 755 756 // Spatial scalability. 757 cpi->svc.number_spatial_layers = oxcf->ss_number_layers; 758 // Temporal scalability. 759 cpi->svc.number_temporal_layers = oxcf->ts_number_layers; 760 761 if ((cpi->svc.number_temporal_layers > 1 && 762 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) || 763 (cpi->svc.number_spatial_layers > 1 && 764 cpi->oxcf.mode == MODE_SECONDPASS_BEST)) { 765 vp9_init_layer_context(cpi); 766 } 767 768 // change includes all joint functionality 769 vp9_change_config(cpi, oxcf); 770 771 cpi->static_mb_pct = 0; 772 773 cpi->lst_fb_idx = 0; 774 cpi->gld_fb_idx = 1; 775 cpi->alt_fb_idx = 2; 776 777 set_tile_limits(cpi); 778 779 cpi->fixed_divide[0] = 0; 780 for (i = 1; i < 512; i++) 781 cpi->fixed_divide[i] = 0x80000 / i; 782} 783 784void vp9_change_config(struct VP9_COMP *cpi, const VP9_CONFIG *oxcf) { 785 VP9_COMMON *const cm = &cpi->common; 786 RATE_CONTROL *const rc = &cpi->rc; 787 788 if (cm->profile != oxcf->profile) 789 cm->profile = oxcf->profile; 790 cm->bit_depth = oxcf->bit_depth; 791 792 if (cm->profile <= PROFILE_1) 793 assert(cm->bit_depth == BITS_8); 794 else 795 assert(cm->bit_depth > BITS_8); 796 797 cpi->oxcf = *oxcf; 798 799 if (cpi->oxcf.cpu_used == -6) 800 cpi->oxcf.play_alternate = 0; 801 802 switch (cpi->oxcf.mode) { 803 // Real time and one pass deprecated in test code base 804 case MODE_GOODQUALITY: 805 cpi->pass = 0; 806 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); 807 break; 808 809 case MODE_BESTQUALITY: 810 cpi->pass = 0; 811 break; 812 813 case MODE_FIRSTPASS: 814 cpi->pass = 1; 815 break; 816 817 case MODE_SECONDPASS: 818 cpi->pass = 2; 819 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); 820 break; 821 822 case MODE_SECONDPASS_BEST: 823 cpi->pass = 2; 824 break; 825 826 case MODE_REALTIME: 827 cpi->pass = 0; 828 break; 829 } 830 831 cpi->oxcf.lossless = oxcf->lossless; 832 if (cpi->oxcf.lossless) { 833 // In lossless mode, make sure right quantizer range and correct transform 834 // is set. 835 cpi->oxcf.worst_allowed_q = 0; 836 cpi->oxcf.best_allowed_q = 0; 837 cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add; 838 } else { 839 cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add; 840 } 841 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; 842 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 843 844 cpi->refresh_golden_frame = 0; 845 cpi->refresh_last_frame = 1; 846 cm->refresh_frame_context = 1; 847 cm->reset_frame_context = 0; 848 849 vp9_reset_segment_features(&cm->seg); 850 set_high_precision_mv(cpi, 0); 851 852 { 853 int i; 854 855 for (i = 0; i < MAX_SEGMENTS; i++) 856 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 857 } 858 cpi->encode_breakout = cpi->oxcf.encode_breakout; 859 860 // local file playback mode == really big buffer 861 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) { 862 cpi->oxcf.starting_buffer_level = 60000; 863 cpi->oxcf.optimal_buffer_level = 60000; 864 cpi->oxcf.maximum_buffer_size = 240000; 865 } 866 867 // Convert target bandwidth from Kbit/s to Bit/s 868 cpi->oxcf.target_bandwidth *= 1000; 869 870 cpi->oxcf.starting_buffer_level = 871 vp9_rescale(cpi->oxcf.starting_buffer_level, 872 cpi->oxcf.target_bandwidth, 1000); 873 874 // Set or reset optimal and maximum buffer levels. 875 if (cpi->oxcf.optimal_buffer_level == 0) 876 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 877 else 878 cpi->oxcf.optimal_buffer_level = 879 vp9_rescale(cpi->oxcf.optimal_buffer_level, 880 cpi->oxcf.target_bandwidth, 1000); 881 882 if (cpi->oxcf.maximum_buffer_size == 0) 883 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 884 else 885 cpi->oxcf.maximum_buffer_size = 886 vp9_rescale(cpi->oxcf.maximum_buffer_size, 887 cpi->oxcf.target_bandwidth, 1000); 888 // Under a configuration change, where maximum_buffer_size may change, 889 // keep buffer level clipped to the maximum allowed buffer size. 890 rc->bits_off_target = MIN(rc->bits_off_target, cpi->oxcf.maximum_buffer_size); 891 rc->buffer_level = MIN(rc->buffer_level, cpi->oxcf.maximum_buffer_size); 892 893 // Set up frame rate and related parameters rate control values. 894 vp9_new_framerate(cpi, cpi->oxcf.framerate); 895 896 // Set absolute upper and lower quality limits 897 rc->worst_quality = cpi->oxcf.worst_allowed_q; 898 rc->best_quality = cpi->oxcf.best_allowed_q; 899 900 // active values should only be modified if out of new range 901 902 cpi->cq_target_quality = cpi->oxcf.cq_level; 903 904 cm->interp_filter = DEFAULT_INTERP_FILTER; 905 906 cm->display_width = cpi->oxcf.width; 907 cm->display_height = cpi->oxcf.height; 908 909 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) 910 cpi->oxcf.sharpness = MIN(7, cpi->oxcf.sharpness); 911 912 cpi->common.lf.sharpness_level = cpi->oxcf.sharpness; 913 914 if (cpi->initial_width) { 915 // Increasing the size of the frame beyond the first seen frame, or some 916 // otherwise signaled maximum size, is not supported. 917 // TODO(jkoleszar): exit gracefully. 918 assert(cm->width <= cpi->initial_width); 919 assert(cm->height <= cpi->initial_height); 920 } 921 update_frame_size(cpi); 922 923 if ((cpi->svc.number_temporal_layers > 1 && 924 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) || 925 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) { 926 vp9_update_layer_context_change_config(cpi, 927 (int)cpi->oxcf.target_bandwidth); 928 } 929 930 cpi->speed = abs(cpi->oxcf.cpu_used); 931 932 // Limit on lag buffers as these are not currently dynamically allocated. 933 if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) 934 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 935 936#if CONFIG_MULTIPLE_ARF 937 vp9_zero(cpi->alt_ref_source); 938#else 939 cpi->alt_ref_source = NULL; 940#endif 941 rc->is_src_frame_alt_ref = 0; 942 943#if 0 944 // Experimental RD Code 945 cpi->frame_distortion = 0; 946 cpi->last_frame_distortion = 0; 947#endif 948 949 set_tile_limits(cpi); 950 951 cpi->ext_refresh_frame_flags_pending = 0; 952 cpi->ext_refresh_frame_context_pending = 0; 953} 954 955#define M_LOG2_E 0.693147180559945309417 956#define log2f(x) (log (x) / (float) M_LOG2_E) 957 958static void cal_nmvjointsadcost(int *mvjointsadcost) { 959 mvjointsadcost[0] = 600; 960 mvjointsadcost[1] = 300; 961 mvjointsadcost[2] = 300; 962 mvjointsadcost[3] = 300; 963} 964 965static void cal_nmvsadcosts(int *mvsadcost[2]) { 966 int i = 1; 967 968 mvsadcost[0][0] = 0; 969 mvsadcost[1][0] = 0; 970 971 do { 972 double z = 256 * (2 * (log2f(8 * i) + .6)); 973 mvsadcost[0][i] = (int)z; 974 mvsadcost[1][i] = (int)z; 975 mvsadcost[0][-i] = (int)z; 976 mvsadcost[1][-i] = (int)z; 977 } while (++i <= MV_MAX); 978} 979 980static void cal_nmvsadcosts_hp(int *mvsadcost[2]) { 981 int i = 1; 982 983 mvsadcost[0][0] = 0; 984 mvsadcost[1][0] = 0; 985 986 do { 987 double z = 256 * (2 * (log2f(8 * i) + .6)); 988 mvsadcost[0][i] = (int)z; 989 mvsadcost[1][i] = (int)z; 990 mvsadcost[0][-i] = (int)z; 991 mvsadcost[1][-i] = (int)z; 992 } while (++i <= MV_MAX); 993} 994 995static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk, 996 PICK_MODE_CONTEXT *ctx) { 997 int num_pix = num_4x4_blk << 4; 998 int i, k; 999 ctx->num_4x4_blk = num_4x4_blk; 1000 1001 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, 1002 vpx_calloc(num_4x4_blk, sizeof(uint8_t))); 1003 for (i = 0; i < MAX_MB_PLANE; ++i) { 1004 for (k = 0; k < 3; ++k) { 1005 CHECK_MEM_ERROR(cm, ctx->coeff[i][k], 1006 vpx_memalign(16, num_pix * sizeof(int16_t))); 1007 CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k], 1008 vpx_memalign(16, num_pix * sizeof(int16_t))); 1009 CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k], 1010 vpx_memalign(16, num_pix * sizeof(int16_t))); 1011 CHECK_MEM_ERROR(cm, ctx->eobs[i][k], 1012 vpx_memalign(16, num_pix * sizeof(uint16_t))); 1013 ctx->coeff_pbuf[i][k] = ctx->coeff[i][k]; 1014 ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k]; 1015 ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k]; 1016 ctx->eobs_pbuf[i][k] = ctx->eobs[i][k]; 1017 } 1018 } 1019} 1020 1021static void free_mode_context(PICK_MODE_CONTEXT *ctx) { 1022 int i, k; 1023 vpx_free(ctx->zcoeff_blk); 1024 ctx->zcoeff_blk = 0; 1025 for (i = 0; i < MAX_MB_PLANE; ++i) { 1026 for (k = 0; k < 3; ++k) { 1027 vpx_free(ctx->coeff[i][k]); 1028 ctx->coeff[i][k] = 0; 1029 vpx_free(ctx->qcoeff[i][k]); 1030 ctx->qcoeff[i][k] = 0; 1031 vpx_free(ctx->dqcoeff[i][k]); 1032 ctx->dqcoeff[i][k] = 0; 1033 vpx_free(ctx->eobs[i][k]); 1034 ctx->eobs[i][k] = 0; 1035 } 1036 } 1037} 1038 1039static void init_pick_mode_context(VP9_COMP *cpi) { 1040 int i; 1041 VP9_COMMON *const cm = &cpi->common; 1042 MACROBLOCK *const x = &cpi->mb; 1043 1044 for (i = 0; i < BLOCK_SIZES; ++i) { 1045 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; 1046 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; 1047 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); 1048 if (i < BLOCK_16X16) { 1049 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1050 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { 1051 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { 1052 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1053 alloc_mode_context(cm, num_4x4_blk, ctx); 1054 } 1055 } 1056 } 1057 } else if (i < BLOCK_32X32) { 1058 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1059 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { 1060 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1061 ctx->num_4x4_blk = num_4x4_blk; 1062 alloc_mode_context(cm, num_4x4_blk, ctx); 1063 } 1064 } 1065 } else if (i < BLOCK_64X64) { 1066 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { 1067 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1068 ctx->num_4x4_blk = num_4x4_blk; 1069 alloc_mode_context(cm, num_4x4_blk, ctx); 1070 } 1071 } else { 1072 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1073 ctx->num_4x4_blk = num_4x4_blk; 1074 alloc_mode_context(cm, num_4x4_blk, ctx); 1075 } 1076 } 1077} 1078 1079static void free_pick_mode_context(MACROBLOCK *x) { 1080 int i; 1081 1082 for (i = 0; i < BLOCK_SIZES; ++i) { 1083 const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; 1084 const int num_4x4_h = num_4x4_blocks_high_lookup[i]; 1085 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); 1086 if (i < BLOCK_16X16) { 1087 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1088 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { 1089 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { 1090 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1091 free_mode_context(ctx); 1092 } 1093 } 1094 } 1095 } else if (i < BLOCK_32X32) { 1096 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1097 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { 1098 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1099 free_mode_context(ctx); 1100 } 1101 } 1102 } else if (i < BLOCK_64X64) { 1103 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { 1104 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1105 free_mode_context(ctx); 1106 } 1107 } else { 1108 PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1109 free_mode_context(ctx); 1110 } 1111 } 1112} 1113 1114VP9_COMP *vp9_create_compressor(VP9_CONFIG *oxcf) { 1115 int i, j; 1116 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP)); 1117 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL; 1118 1119 if (!cm) 1120 return NULL; 1121 1122 vp9_zero(*cpi); 1123 1124 if (setjmp(cm->error.jmp)) { 1125 cm->error.setjmp = 0; 1126 vp9_remove_compressor(cpi); 1127 return 0; 1128 } 1129 1130 cm->error.setjmp = 1; 1131 1132 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), 1133 (MAX_MVSEARCH_STEPS * 8) + 1)); 1134 1135 vp9_rtcd(); 1136 1137 cpi->use_svc = 0; 1138 1139 init_config(cpi, oxcf); 1140 vp9_rc_init(&cpi->oxcf, cpi->pass, &cpi->rc); 1141 init_pick_mode_context(cpi); 1142 1143 cm->current_video_frame = 0; 1144 1145 // Set reference frame sign bias for ALTREF frame to 1 (for now) 1146 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; 1147 1148 cpi->gold_is_last = 0; 1149 cpi->alt_is_last = 0; 1150 cpi->gold_is_alt = 0; 1151 1152 // Create the encoder segmentation map and set all entries to 0 1153 CHECK_MEM_ERROR(cm, cpi->segmentation_map, 1154 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1155 1156 // Create a complexity map used for rd adjustment 1157 CHECK_MEM_ERROR(cm, cpi->complexity_map, 1158 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1159 1160 // Create a map used for cyclic background refresh. 1161 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh, 1162 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols)); 1163 1164 // And a place holder structure is the coding context 1165 // for use if we want to save and restore it 1166 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, 1167 vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1168 1169 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); 1170 vpx_memset(cpi->active_map, 1, cm->MBs); 1171 cpi->active_map_enabled = 0; 1172 1173 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / 1174 sizeof(cpi->mbgraph_stats[0])); i++) { 1175 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats, 1176 vpx_calloc(cm->MBs * 1177 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1)); 1178 } 1179 1180 /*Initialize the feed-forward activity masking.*/ 1181 cpi->activity_avg = 90 << 12; 1182 cpi->key_frame_frequency = cpi->oxcf.key_freq; 1183 cpi->refresh_alt_ref_frame = 0; 1184 1185#if CONFIG_MULTIPLE_ARF 1186 // Turn multiple ARF usage on/off. This is a quick hack for the initial test 1187 // version. It should eventually be set via the codec API. 1188 cpi->multi_arf_enabled = 1; 1189 1190 if (cpi->multi_arf_enabled) { 1191 cpi->sequence_number = 0; 1192 cpi->frame_coding_order_period = 0; 1193 vp9_zero(cpi->frame_coding_order); 1194 vp9_zero(cpi->arf_buffer_idx); 1195 } 1196#endif 1197 1198 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS; 1199#if CONFIG_INTERNAL_STATS 1200 cpi->b_calculate_ssimg = 0; 1201 1202 cpi->count = 0; 1203 cpi->bytes = 0; 1204 1205 if (cpi->b_calculate_psnr) { 1206 cpi->total_y = 0.0; 1207 cpi->total_u = 0.0; 1208 cpi->total_v = 0.0; 1209 cpi->total = 0.0; 1210 cpi->total_sq_error = 0; 1211 cpi->total_samples = 0; 1212 1213 cpi->totalp_y = 0.0; 1214 cpi->totalp_u = 0.0; 1215 cpi->totalp_v = 0.0; 1216 cpi->totalp = 0.0; 1217 cpi->totalp_sq_error = 0; 1218 cpi->totalp_samples = 0; 1219 1220 cpi->tot_recode_hits = 0; 1221 cpi->summed_quality = 0; 1222 cpi->summed_weights = 0; 1223 cpi->summedp_quality = 0; 1224 cpi->summedp_weights = 0; 1225 } 1226 1227 if (cpi->b_calculate_ssimg) { 1228 cpi->total_ssimg_y = 0; 1229 cpi->total_ssimg_u = 0; 1230 cpi->total_ssimg_v = 0; 1231 cpi->total_ssimg_all = 0; 1232 } 1233 1234#endif 1235 1236 cpi->first_time_stamp_ever = INT64_MAX; 1237 1238 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); 1239 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; 1240 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; 1241 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; 1242 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; 1243 cal_nmvsadcosts(cpi->mb.nmvsadcost); 1244 1245 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; 1246 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; 1247 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; 1248 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; 1249 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); 1250 1251#ifdef OUTPUT_YUV_SRC 1252 yuv_file = fopen("bd.yuv", "ab"); 1253#endif 1254#ifdef OUTPUT_YUV_REC 1255 yuv_rec_file = fopen("rec.yuv", "wb"); 1256#endif 1257 1258#if 0 1259 framepsnr = fopen("framepsnr.stt", "a"); 1260 kf_list = fopen("kf_list.stt", "w"); 1261#endif 1262 1263 cpi->output_pkt_list = oxcf->output_pkt_list; 1264 1265 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED; 1266 1267 if (cpi->pass == 1) { 1268 vp9_init_first_pass(cpi); 1269 } else if (cpi->pass == 2) { 1270 const size_t packet_sz = sizeof(FIRSTPASS_STATS); 1271 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz); 1272 1273 if (cpi->svc.number_spatial_layers > 1 1274 && cpi->svc.number_temporal_layers == 1) { 1275 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf; 1276 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0}; 1277 int i; 1278 1279 for (i = 0; i < oxcf->ss_number_layers; ++i) { 1280 FIRSTPASS_STATS *const last_packet_for_layer = 1281 &stats[packets - oxcf->ss_number_layers + i]; 1282 const int layer_id = (int)last_packet_for_layer->spatial_layer_id; 1283 const int packets_in_layer = (int)last_packet_for_layer->count + 1; 1284 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) { 1285 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id]; 1286 1287 vpx_free(lc->rc_twopass_stats_in.buf); 1288 1289 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz; 1290 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf, 1291 vpx_malloc(lc->rc_twopass_stats_in.sz)); 1292 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf; 1293 lc->twopass.stats_in = lc->twopass.stats_in_start; 1294 lc->twopass.stats_in_end = lc->twopass.stats_in_start 1295 + packets_in_layer - 1; 1296 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf; 1297 } 1298 } 1299 1300 for (i = 0; i < packets; ++i) { 1301 const int layer_id = (int)stats[i].spatial_layer_id; 1302 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers 1303 && stats_copy[layer_id] != NULL) { 1304 *stats_copy[layer_id] = stats[i]; 1305 ++stats_copy[layer_id]; 1306 } 1307 } 1308 1309 vp9_init_second_pass_spatial_svc(cpi); 1310 } else { 1311 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf; 1312 cpi->twopass.stats_in = cpi->twopass.stats_in_start; 1313 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1]; 1314 1315 vp9_init_second_pass(cpi); 1316 } 1317 } 1318 1319 set_speed_features(cpi); 1320 1321 // Default rd threshold factors for mode selection 1322 for (i = 0; i < BLOCK_SIZES; ++i) { 1323 for (j = 0; j < MAX_MODES; ++j) 1324 cpi->rd_thresh_freq_fact[i][j] = 32; 1325 for (j = 0; j < MAX_REFS; ++j) 1326 cpi->rd_thresh_freq_sub8x8[i][j] = 32; 1327 } 1328 1329#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \ 1330 SDX3F, SDX8F, SDX4DF)\ 1331 cpi->fn_ptr[BT].sdf = SDF; \ 1332 cpi->fn_ptr[BT].sdaf = SDAF; \ 1333 cpi->fn_ptr[BT].vf = VF; \ 1334 cpi->fn_ptr[BT].svf = SVF; \ 1335 cpi->fn_ptr[BT].svaf = SVAF; \ 1336 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \ 1337 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \ 1338 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \ 1339 cpi->fn_ptr[BT].sdx3f = SDX3F; \ 1340 cpi->fn_ptr[BT].sdx8f = SDX8F; \ 1341 cpi->fn_ptr[BT].sdx4df = SDX4DF; 1342 1343 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg, 1344 vp9_variance32x16, vp9_sub_pixel_variance32x16, 1345 vp9_sub_pixel_avg_variance32x16, NULL, NULL, 1346 NULL, NULL, NULL, 1347 vp9_sad32x16x4d) 1348 1349 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg, 1350 vp9_variance16x32, vp9_sub_pixel_variance16x32, 1351 vp9_sub_pixel_avg_variance16x32, NULL, NULL, 1352 NULL, NULL, NULL, 1353 vp9_sad16x32x4d) 1354 1355 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg, 1356 vp9_variance64x32, vp9_sub_pixel_variance64x32, 1357 vp9_sub_pixel_avg_variance64x32, NULL, NULL, 1358 NULL, NULL, NULL, 1359 vp9_sad64x32x4d) 1360 1361 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg, 1362 vp9_variance32x64, vp9_sub_pixel_variance32x64, 1363 vp9_sub_pixel_avg_variance32x64, NULL, NULL, 1364 NULL, NULL, NULL, 1365 vp9_sad32x64x4d) 1366 1367 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg, 1368 vp9_variance32x32, vp9_sub_pixel_variance32x32, 1369 vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h, 1370 vp9_variance_halfpixvar32x32_v, 1371 vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8, 1372 vp9_sad32x32x4d) 1373 1374 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg, 1375 vp9_variance64x64, vp9_sub_pixel_variance64x64, 1376 vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h, 1377 vp9_variance_halfpixvar64x64_v, 1378 vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8, 1379 vp9_sad64x64x4d) 1380 1381 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg, 1382 vp9_variance16x16, vp9_sub_pixel_variance16x16, 1383 vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h, 1384 vp9_variance_halfpixvar16x16_v, 1385 vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8, 1386 vp9_sad16x16x4d) 1387 1388 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg, 1389 vp9_variance16x8, vp9_sub_pixel_variance16x8, 1390 vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL, 1391 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d) 1392 1393 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg, 1394 vp9_variance8x16, vp9_sub_pixel_variance8x16, 1395 vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL, 1396 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d) 1397 1398 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg, 1399 vp9_variance8x8, vp9_sub_pixel_variance8x8, 1400 vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL, 1401 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d) 1402 1403 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg, 1404 vp9_variance8x4, vp9_sub_pixel_variance8x4, 1405 vp9_sub_pixel_avg_variance8x4, NULL, NULL, 1406 NULL, NULL, vp9_sad8x4x8, 1407 vp9_sad8x4x4d) 1408 1409 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg, 1410 vp9_variance4x8, vp9_sub_pixel_variance4x8, 1411 vp9_sub_pixel_avg_variance4x8, NULL, NULL, 1412 NULL, NULL, vp9_sad4x8x8, 1413 vp9_sad4x8x4d) 1414 1415 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg, 1416 vp9_variance4x4, vp9_sub_pixel_variance4x4, 1417 vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL, 1418 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d) 1419 1420 cpi->full_search_sad = vp9_full_search_sad; 1421 cpi->diamond_search_sad = vp9_diamond_search_sad; 1422 cpi->refining_search_sad = vp9_refining_search_sad; 1423 1424 /* vp9_init_quantizer() is first called here. Add check in 1425 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only 1426 * called later when needed. This will avoid unnecessary calls of 1427 * vp9_init_quantizer() for every frame. 1428 */ 1429 vp9_init_quantizer(cpi); 1430 1431 vp9_loop_filter_init(cm); 1432 1433 cm->error.setjmp = 0; 1434 1435 vp9_zero(cpi->common.counts.uv_mode); 1436 1437#ifdef MODE_TEST_HIT_STATS 1438 vp9_zero(cpi->mode_test_hits); 1439#endif 1440 1441 return cpi; 1442} 1443 1444void vp9_remove_compressor(VP9_COMP *cpi) { 1445 int i; 1446 1447 if (!cpi) 1448 return; 1449 1450 if (cpi && (cpi->common.current_video_frame > 0)) { 1451#if CONFIG_INTERNAL_STATS 1452 1453 vp9_clear_system_state(); 1454 1455 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count); 1456 if (cpi->pass != 1) { 1457 FILE *f = fopen("opsnr.stt", "a"); 1458 double time_encoded = (cpi->last_end_time_stamp_seen 1459 - cpi->first_time_stamp_ever) / 10000000.000; 1460 double total_encode_time = (cpi->time_receive_data + 1461 cpi->time_compress_data) / 1000.000; 1462 double dr = (double)cpi->bytes * (double) 8 / (double)1000 1463 / time_encoded; 1464 1465 if (cpi->b_calculate_psnr) { 1466 const double total_psnr = 1467 vpx_sse_to_psnr((double)cpi->total_samples, 255.0, 1468 (double)cpi->total_sq_error); 1469 const double totalp_psnr = 1470 vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0, 1471 (double)cpi->totalp_sq_error); 1472 const double total_ssim = 100 * pow(cpi->summed_quality / 1473 cpi->summed_weights, 8.0); 1474 const double totalp_ssim = 100 * pow(cpi->summedp_quality / 1475 cpi->summedp_weights, 8.0); 1476 1477 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t" 1478 "VPXSSIM\tVPSSIMP\t Time(ms)\n"); 1479 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", 1480 dr, cpi->total / cpi->count, total_psnr, 1481 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim, 1482 total_encode_time); 1483 } 1484 1485 if (cpi->b_calculate_ssimg) { 1486 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n"); 1487 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, 1488 cpi->total_ssimg_y / cpi->count, 1489 cpi->total_ssimg_u / cpi->count, 1490 cpi->total_ssimg_v / cpi->count, 1491 cpi->total_ssimg_all / cpi->count, total_encode_time); 1492 } 1493 1494 fclose(f); 1495 } 1496 1497#endif 1498 1499#ifdef MODE_TEST_HIT_STATS 1500 if (cpi->pass != 1) { 1501 double norm_per_pixel_mode_tests = 0; 1502 double norm_counts[BLOCK_SIZES]; 1503 int i; 1504 int sb64_per_frame; 1505 int norm_factors[BLOCK_SIZES] = 1506 {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1}; 1507 FILE *f = fopen("mode_hit_stats.stt", "a"); 1508 1509 // On average, how many mode tests do we do 1510 for (i = 0; i < BLOCK_SIZES; ++i) { 1511 norm_counts[i] = (double)cpi->mode_test_hits[i] / 1512 (double)norm_factors[i]; 1513 norm_per_pixel_mode_tests += norm_counts[i]; 1514 } 1515 // Convert to a number per 64x64 and per frame 1516 sb64_per_frame = ((cpi->common.height + 63) / 64) * 1517 ((cpi->common.width + 63) / 64); 1518 norm_per_pixel_mode_tests = 1519 norm_per_pixel_mode_tests / 1520 (double)(cpi->common.current_video_frame * sb64_per_frame); 1521 1522 fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests); 1523 fclose(f); 1524 } 1525#endif 1526 1527#if 0 1528 { 1529 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); 1530 printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); 1531 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, 1532 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000, 1533 cpi->time_compress_data / 1000, 1534 (cpi->time_receive_data + cpi->time_compress_data) / 1000); 1535 } 1536#endif 1537 } 1538 1539 free_pick_mode_context(&cpi->mb); 1540 dealloc_compressor_data(cpi); 1541 vpx_free(cpi->mb.ss); 1542 vpx_free(cpi->tok); 1543 1544 for (i = 0; i < sizeof(cpi->mbgraph_stats) / 1545 sizeof(cpi->mbgraph_stats[0]); ++i) { 1546 vpx_free(cpi->mbgraph_stats[i].mb_stats); 1547 } 1548 1549 vp9_remove_common(&cpi->common); 1550 vpx_free(cpi); 1551 1552#ifdef OUTPUT_YUV_SRC 1553 fclose(yuv_file); 1554#endif 1555#ifdef OUTPUT_YUV_REC 1556 fclose(yuv_rec_file); 1557#endif 1558 1559#if 0 1560 1561 if (keyfile) 1562 fclose(keyfile); 1563 1564 if (framepsnr) 1565 fclose(framepsnr); 1566 1567 if (kf_list) 1568 fclose(kf_list); 1569 1570#endif 1571} 1572static int64_t get_sse(const uint8_t *a, int a_stride, 1573 const uint8_t *b, int b_stride, 1574 int width, int height) { 1575 const int dw = width % 16; 1576 const int dh = height % 16; 1577 int64_t total_sse = 0; 1578 unsigned int sse = 0; 1579 int sum = 0; 1580 int x, y; 1581 1582 if (dw > 0) { 1583 variance(&a[width - dw], a_stride, &b[width - dw], b_stride, 1584 dw, height, &sse, &sum); 1585 total_sse += sse; 1586 } 1587 1588 if (dh > 0) { 1589 variance(&a[(height - dh) * a_stride], a_stride, 1590 &b[(height - dh) * b_stride], b_stride, 1591 width - dw, dh, &sse, &sum); 1592 total_sse += sse; 1593 } 1594 1595 for (y = 0; y < height / 16; ++y) { 1596 const uint8_t *pa = a; 1597 const uint8_t *pb = b; 1598 for (x = 0; x < width / 16; ++x) { 1599 vp9_mse16x16(pa, a_stride, pb, b_stride, &sse); 1600 total_sse += sse; 1601 1602 pa += 16; 1603 pb += 16; 1604 } 1605 1606 a += 16 * a_stride; 1607 b += 16 * b_stride; 1608 } 1609 1610 return total_sse; 1611} 1612 1613typedef struct { 1614 double psnr[4]; // total/y/u/v 1615 uint64_t sse[4]; // total/y/u/v 1616 uint32_t samples[4]; // total/y/u/v 1617} PSNR_STATS; 1618 1619static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b, 1620 PSNR_STATS *psnr) { 1621 const int widths[3] = {a->y_width, a->uv_width, a->uv_width }; 1622 const int heights[3] = {a->y_height, a->uv_height, a->uv_height}; 1623 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer }; 1624 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride}; 1625 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer }; 1626 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride}; 1627 int i; 1628 uint64_t total_sse = 0; 1629 uint32_t total_samples = 0; 1630 1631 for (i = 0; i < 3; ++i) { 1632 const int w = widths[i]; 1633 const int h = heights[i]; 1634 const uint32_t samples = w * h; 1635 const uint64_t sse = get_sse(a_planes[i], a_strides[i], 1636 b_planes[i], b_strides[i], 1637 w, h); 1638 psnr->sse[1 + i] = sse; 1639 psnr->samples[1 + i] = samples; 1640 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse); 1641 1642 total_sse += sse; 1643 total_samples += samples; 1644 } 1645 1646 psnr->sse[0] = total_sse; 1647 psnr->samples[0] = total_samples; 1648 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0, 1649 (double)total_sse); 1650} 1651 1652static void generate_psnr_packet(VP9_COMP *cpi) { 1653 struct vpx_codec_cx_pkt pkt; 1654 int i; 1655 PSNR_STATS psnr; 1656 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr); 1657 for (i = 0; i < 4; ++i) { 1658 pkt.data.psnr.samples[i] = psnr.samples[i]; 1659 pkt.data.psnr.sse[i] = psnr.sse[i]; 1660 pkt.data.psnr.psnr[i] = psnr.psnr[i]; 1661 } 1662 pkt.kind = VPX_CODEC_PSNR_PKT; 1663 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); 1664} 1665 1666int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) { 1667 if (ref_frame_flags > 7) 1668 return -1; 1669 1670 cpi->ref_frame_flags = ref_frame_flags; 1671 return 0; 1672} 1673 1674void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) { 1675 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0; 1676 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0; 1677 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0; 1678 cpi->ext_refresh_frame_flags_pending = 1; 1679} 1680 1681static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi, 1682 VP9_REFFRAME ref_frame_flag) { 1683 MV_REFERENCE_FRAME ref_frame = NONE; 1684 if (ref_frame_flag == VP9_LAST_FLAG) 1685 ref_frame = LAST_FRAME; 1686 else if (ref_frame_flag == VP9_GOLD_FLAG) 1687 ref_frame = GOLDEN_FRAME; 1688 else if (ref_frame_flag == VP9_ALT_FLAG) 1689 ref_frame = ALTREF_FRAME; 1690 1691 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame); 1692} 1693 1694int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag, 1695 YV12_BUFFER_CONFIG *sd) { 1696 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag); 1697 if (cfg) { 1698 vp8_yv12_copy_frame(cfg, sd); 1699 return 0; 1700 } else { 1701 return -1; 1702 } 1703} 1704 1705int vp9_get_reference_enc(VP9_COMP *cpi, int index, YV12_BUFFER_CONFIG **fb) { 1706 VP9_COMMON *cm = &cpi->common; 1707 1708 if (index < 0 || index >= REF_FRAMES) 1709 return -1; 1710 1711 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf; 1712 return 0; 1713} 1714 1715int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag, 1716 YV12_BUFFER_CONFIG *sd) { 1717 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag); 1718 if (cfg) { 1719 vp8_yv12_copy_frame(sd, cfg); 1720 return 0; 1721 } else { 1722 return -1; 1723 } 1724} 1725 1726int vp9_update_entropy(VP9_COMP * cpi, int update) { 1727 cpi->ext_refresh_frame_context = update; 1728 cpi->ext_refresh_frame_context_pending = 1; 1729 return 0; 1730} 1731 1732 1733#ifdef OUTPUT_YUV_SRC 1734void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) { 1735 uint8_t *src = s->y_buffer; 1736 int h = s->y_height; 1737 1738 do { 1739 fwrite(src, s->y_width, 1, yuv_file); 1740 src += s->y_stride; 1741 } while (--h); 1742 1743 src = s->u_buffer; 1744 h = s->uv_height; 1745 1746 do { 1747 fwrite(src, s->uv_width, 1, yuv_file); 1748 src += s->uv_stride; 1749 } while (--h); 1750 1751 src = s->v_buffer; 1752 h = s->uv_height; 1753 1754 do { 1755 fwrite(src, s->uv_width, 1, yuv_file); 1756 src += s->uv_stride; 1757 } while (--h); 1758} 1759#endif 1760 1761#ifdef OUTPUT_YUV_REC 1762void vp9_write_yuv_rec_frame(VP9_COMMON *cm) { 1763 YV12_BUFFER_CONFIG *s = cm->frame_to_show; 1764 uint8_t *src = s->y_buffer; 1765 int h = cm->height; 1766 1767 do { 1768 fwrite(src, s->y_width, 1, yuv_rec_file); 1769 src += s->y_stride; 1770 } while (--h); 1771 1772 src = s->u_buffer; 1773 h = s->uv_height; 1774 1775 do { 1776 fwrite(src, s->uv_width, 1, yuv_rec_file); 1777 src += s->uv_stride; 1778 } while (--h); 1779 1780 src = s->v_buffer; 1781 h = s->uv_height; 1782 1783 do { 1784 fwrite(src, s->uv_width, 1, yuv_rec_file); 1785 src += s->uv_stride; 1786 } while (--h); 1787 1788#if CONFIG_ALPHA 1789 if (s->alpha_buffer) { 1790 src = s->alpha_buffer; 1791 h = s->alpha_height; 1792 do { 1793 fwrite(src, s->alpha_width, 1, yuv_rec_file); 1794 src += s->alpha_stride; 1795 } while (--h); 1796 } 1797#endif 1798 1799 fflush(yuv_rec_file); 1800} 1801#endif 1802 1803static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG *src_fb, 1804 YV12_BUFFER_CONFIG *dst_fb) { 1805 const int in_w = src_fb->y_crop_width; 1806 const int in_h = src_fb->y_crop_height; 1807 const int out_w = dst_fb->y_crop_width; 1808 const int out_h = dst_fb->y_crop_height; 1809 const int in_w_uv = src_fb->uv_crop_width; 1810 const int in_h_uv = src_fb->uv_crop_height; 1811 const int out_w_uv = dst_fb->uv_crop_width; 1812 const int out_h_uv = dst_fb->uv_crop_height; 1813 int i; 1814 1815 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer, 1816 src_fb->alpha_buffer}; 1817 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride, 1818 src_fb->alpha_stride}; 1819 1820 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer, 1821 dst_fb->alpha_buffer}; 1822 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride, 1823 dst_fb->alpha_stride}; 1824 1825 for (i = 0; i < MAX_MB_PLANE; ++i) { 1826 if (i == 0 || i == 3) { 1827 // Y and alpha planes 1828 vp9_resize_plane(srcs[i], in_h, in_w, src_strides[i], 1829 dsts[i], out_h, out_w, dst_strides[i]); 1830 } else { 1831 // Chroma planes 1832 vp9_resize_plane(srcs[i], in_h_uv, in_w_uv, src_strides[i], 1833 dsts[i], out_h_uv, out_w_uv, dst_strides[i]); 1834 } 1835 } 1836 vp8_yv12_extend_frame_borders(dst_fb); 1837} 1838 1839static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb, 1840 YV12_BUFFER_CONFIG *dst_fb) { 1841 const int in_w = src_fb->y_crop_width; 1842 const int in_h = src_fb->y_crop_height; 1843 const int out_w = dst_fb->y_crop_width; 1844 const int out_h = dst_fb->y_crop_height; 1845 int x, y, i; 1846 1847 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer, 1848 src_fb->alpha_buffer}; 1849 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride, 1850 src_fb->alpha_stride}; 1851 1852 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer, 1853 dst_fb->alpha_buffer}; 1854 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride, 1855 dst_fb->alpha_stride}; 1856 1857 for (y = 0; y < out_h; y += 16) { 1858 for (x = 0; x < out_w; x += 16) { 1859 for (i = 0; i < MAX_MB_PLANE; ++i) { 1860 const int factor = (i == 0 || i == 3 ? 1 : 2); 1861 const int x_q4 = x * (16 / factor) * in_w / out_w; 1862 const int y_q4 = y * (16 / factor) * in_h / out_h; 1863 const int src_stride = src_strides[i]; 1864 const int dst_stride = dst_strides[i]; 1865 uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride + 1866 x / factor * in_w / out_w; 1867 uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor; 1868 1869 vp9_convolve8(src, src_stride, dst, dst_stride, 1870 vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w, 1871 vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h, 1872 16 / factor, 16 / factor); 1873 } 1874 } 1875 } 1876 1877 vp8_yv12_extend_frame_borders(dst_fb); 1878} 1879 1880static int find_fp_qindex() { 1881 int i; 1882 1883 for (i = 0; i < QINDEX_RANGE; i++) { 1884 if (vp9_convert_qindex_to_q(i) >= 30.0) { 1885 break; 1886 } 1887 } 1888 1889 if (i == QINDEX_RANGE) 1890 i--; 1891 1892 return i; 1893} 1894 1895#define WRITE_RECON_BUFFER 0 1896#if WRITE_RECON_BUFFER 1897void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { 1898 FILE *yframe; 1899 int i; 1900 char filename[255]; 1901 1902 snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame); 1903 yframe = fopen(filename, "wb"); 1904 1905 for (i = 0; i < frame->y_height; i++) 1906 fwrite(frame->y_buffer + i * frame->y_stride, 1907 frame->y_width, 1, yframe); 1908 1909 fclose(yframe); 1910 snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame); 1911 yframe = fopen(filename, "wb"); 1912 1913 for (i = 0; i < frame->uv_height; i++) 1914 fwrite(frame->u_buffer + i * frame->uv_stride, 1915 frame->uv_width, 1, yframe); 1916 1917 fclose(yframe); 1918 snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame); 1919 yframe = fopen(filename, "wb"); 1920 1921 for (i = 0; i < frame->uv_height; i++) 1922 fwrite(frame->v_buffer + i * frame->uv_stride, 1923 frame->uv_width, 1, yframe); 1924 1925 fclose(yframe); 1926} 1927#endif 1928 1929// Function to test for conditions that indicate we should loop 1930// back and recode a frame. 1931static int recode_loop_test(const VP9_COMP *cpi, 1932 int high_limit, int low_limit, 1933 int q, int maxq, int minq) { 1934 const VP9_COMMON *const cm = &cpi->common; 1935 const RATE_CONTROL *const rc = &cpi->rc; 1936 int force_recode = 0; 1937 1938 // Special case trap if maximum allowed frame size exceeded. 1939 if (rc->projected_frame_size > rc->max_frame_bandwidth) { 1940 force_recode = 1; 1941 1942 // Is frame recode allowed. 1943 // Yes if either recode mode 1 is selected or mode 2 is selected 1944 // and the frame is a key frame, golden frame or alt_ref_frame 1945 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) || 1946 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) && 1947 (cm->frame_type == KEY_FRAME || 1948 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { 1949 // General over and under shoot tests 1950 if ((rc->projected_frame_size > high_limit && q < maxq) || 1951 (rc->projected_frame_size < low_limit && q > minq)) { 1952 force_recode = 1; 1953 } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { 1954 // Deal with frame undershoot and whether or not we are 1955 // below the automatically set cq level. 1956 if (q > cpi->cq_target_quality && 1957 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) { 1958 force_recode = 1; 1959 } 1960 } 1961 } 1962 return force_recode; 1963} 1964 1965void vp9_update_reference_frames(VP9_COMP *cpi) { 1966 VP9_COMMON * const cm = &cpi->common; 1967 1968 // At this point the new frame has been encoded. 1969 // If any buffer copy / swapping is signaled it should be done here. 1970 if (cm->frame_type == KEY_FRAME) { 1971 ref_cnt_fb(cm->frame_bufs, 1972 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 1973 ref_cnt_fb(cm->frame_bufs, 1974 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 1975 } 1976#if CONFIG_MULTIPLE_ARF 1977 else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame && 1978 !cpi->refresh_alt_ref_frame) { 1979#else 1980 else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame && 1981 !cpi->use_svc) { 1982#endif 1983 /* Preserve the previously existing golden frame and update the frame in 1984 * the alt ref slot instead. This is highly specific to the current use of 1985 * alt-ref as a forward reference, and this needs to be generalized as 1986 * other uses are implemented (like RTC/temporal scaling) 1987 * 1988 * The update to the buffer in the alt ref slot was signaled in 1989 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated 1990 * as the golden frame next time. 1991 */ 1992 int tmp; 1993 1994 ref_cnt_fb(cm->frame_bufs, 1995 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 1996 1997 tmp = cpi->alt_fb_idx; 1998 cpi->alt_fb_idx = cpi->gld_fb_idx; 1999 cpi->gld_fb_idx = tmp; 2000 } else { /* For non key/golden frames */ 2001 if (cpi->refresh_alt_ref_frame) { 2002 int arf_idx = cpi->alt_fb_idx; 2003#if CONFIG_MULTIPLE_ARF 2004 if (cpi->multi_arf_enabled) { 2005 arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1]; 2006 } 2007#endif 2008 ref_cnt_fb(cm->frame_bufs, 2009 &cm->ref_frame_map[arf_idx], cm->new_fb_idx); 2010 } 2011 2012 if (cpi->refresh_golden_frame) { 2013 ref_cnt_fb(cm->frame_bufs, 2014 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 2015 } 2016 } 2017 2018 if (cpi->refresh_last_frame) { 2019 ref_cnt_fb(cm->frame_bufs, 2020 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx); 2021 } 2022} 2023 2024static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) { 2025 MACROBLOCKD *xd = &cpi->mb.e_mbd; 2026 struct loopfilter *lf = &cm->lf; 2027 if (xd->lossless) { 2028 lf->filter_level = 0; 2029 } else { 2030 struct vpx_usec_timer timer; 2031 2032 vp9_clear_system_state(); 2033 2034 vpx_usec_timer_start(&timer); 2035 2036 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick); 2037 2038 vpx_usec_timer_mark(&timer); 2039 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); 2040 } 2041 2042 if (lf->filter_level > 0) { 2043 vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); 2044 } 2045 2046 vp9_extend_frame_inner_borders(cm->frame_to_show); 2047} 2048 2049void vp9_scale_references(VP9_COMP *cpi) { 2050 VP9_COMMON *cm = &cpi->common; 2051 MV_REFERENCE_FRAME ref_frame; 2052 2053 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 2054 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)]; 2055 YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf; 2056 2057 if (ref->y_crop_width != cm->width || 2058 ref->y_crop_height != cm->height) { 2059 const int new_fb = get_free_fb(cm); 2060 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf, 2061 cm->width, cm->height, 2062 cm->subsampling_x, cm->subsampling_y, 2063 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL); 2064 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf); 2065 cpi->scaled_ref_idx[ref_frame - 1] = new_fb; 2066 } else { 2067 cpi->scaled_ref_idx[ref_frame - 1] = idx; 2068 cm->frame_bufs[idx].ref_count++; 2069 } 2070 } 2071} 2072 2073static void release_scaled_references(VP9_COMP *cpi) { 2074 VP9_COMMON *cm = &cpi->common; 2075 int i; 2076 2077 for (i = 0; i < 3; i++) 2078 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--; 2079} 2080 2081static void full_to_model_count(unsigned int *model_count, 2082 unsigned int *full_count) { 2083 int n; 2084 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; 2085 model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; 2086 model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; 2087 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n) 2088 model_count[TWO_TOKEN] += full_count[n]; 2089 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN]; 2090} 2091 2092static void full_to_model_counts(vp9_coeff_count_model *model_count, 2093 vp9_coeff_count *full_count) { 2094 int i, j, k, l; 2095 2096 for (i = 0; i < PLANE_TYPES; ++i) 2097 for (j = 0; j < REF_TYPES; ++j) 2098 for (k = 0; k < COEF_BANDS; ++k) 2099 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) 2100 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); 2101} 2102 2103#if 0 && CONFIG_INTERNAL_STATS 2104static void output_frame_level_debug_stats(VP9_COMP *cpi) { 2105 VP9_COMMON *const cm = &cpi->common; 2106 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); 2107 int recon_err; 2108 2109 vp9_clear_system_state(); 2110 2111 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm)); 2112 2113 if (cpi->twopass.total_left_stats.coded_error != 0.0) 2114 fprintf(f, "%10u %10d %10d %10d %10d %10d " 2115 "%10"PRId64" %10"PRId64" %10d " 2116 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf" 2117 "%6d %6d %5d %5d %5d " 2118 "%10"PRId64" %10.3lf" 2119 "%10lf %8u %10d %10d %10d\n", 2120 cpi->common.current_video_frame, cpi->rc.this_frame_target, 2121 cpi->rc.projected_frame_size, 2122 cpi->rc.projected_frame_size / cpi->common.MBs, 2123 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target), 2124 cpi->rc.total_target_vs_actual, 2125 (cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target), 2126 cpi->rc.total_actual_bits, cm->base_qindex, 2127 vp9_convert_qindex_to_q(cm->base_qindex), 2128 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, 2129 cpi->rc.avg_q, 2130 vp9_convert_qindex_to_q(cpi->rc.ni_av_qi), 2131 vp9_convert_qindex_to_q(cpi->cq_target_quality), 2132 cpi->refresh_last_frame, cpi->refresh_golden_frame, 2133 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost, 2134 cpi->twopass.bits_left, 2135 cpi->twopass.total_left_stats.coded_error, 2136 cpi->twopass.bits_left / 2137 (1 + cpi->twopass.total_left_stats.coded_error), 2138 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost, 2139 cpi->twopass.kf_zeromotion_pct); 2140 2141 fclose(f); 2142 2143 if (0) { 2144 FILE *const fmodes = fopen("Modes.stt", "a"); 2145 int i; 2146 2147 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, 2148 cm->frame_type, cpi->refresh_golden_frame, 2149 cpi->refresh_alt_ref_frame); 2150 2151 for (i = 0; i < MAX_MODES; ++i) 2152 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); 2153 2154 fprintf(fmodes, "\n"); 2155 2156 fclose(fmodes); 2157 } 2158} 2159#endif 2160 2161static void encode_without_recode_loop(VP9_COMP *cpi, 2162 size_t *size, 2163 uint8_t *dest, 2164 int q) { 2165 VP9_COMMON *const cm = &cpi->common; 2166 vp9_clear_system_state(); 2167 vp9_set_quantizer(cm, q); 2168 2169 // Set up entropy context depending on frame type. The decoder mandates 2170 // the use of the default context, index 0, for keyframes and inter 2171 // frames where the error_resilient_mode or intra_only flag is set. For 2172 // other inter-frames the encoder currently uses only two contexts; 2173 // context 1 for ALTREF frames and context 0 for the others. 2174 if (cm->frame_type == KEY_FRAME) { 2175 setup_key_frame(cpi); 2176 } else { 2177 if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc) 2178 cm->frame_context_idx = cpi->refresh_alt_ref_frame; 2179 2180 setup_inter_frame(cm); 2181 } 2182 // Variance adaptive and in frame q adjustment experiments are mutually 2183 // exclusive. 2184 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 2185 vp9_vaq_frame_setup(cpi); 2186 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) { 2187 vp9_setup_in_frame_q_adj(cpi); 2188 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) { 2189 vp9_cyclic_refresh_setup(cpi); 2190 } 2191 // transform / motion compensation build reconstruction frame 2192 vp9_encode_frame(cpi); 2193 2194 // Update the skip mb flag probabilities based on the distribution 2195 // seen in the last encoder iteration. 2196 // update_base_skip_probs(cpi); 2197 vp9_clear_system_state(); 2198} 2199 2200static void encode_with_recode_loop(VP9_COMP *cpi, 2201 size_t *size, 2202 uint8_t *dest, 2203 int q, 2204 int bottom_index, 2205 int top_index) { 2206 VP9_COMMON *const cm = &cpi->common; 2207 RATE_CONTROL *const rc = &cpi->rc; 2208 int loop_count = 0; 2209 int loop = 0; 2210 int overshoot_seen = 0; 2211 int undershoot_seen = 0; 2212 int q_low = bottom_index, q_high = top_index; 2213 int frame_over_shoot_limit; 2214 int frame_under_shoot_limit; 2215 2216 // Decide frame size bounds 2217 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target, 2218 &frame_under_shoot_limit, 2219 &frame_over_shoot_limit); 2220 2221 do { 2222 vp9_clear_system_state(); 2223 2224 vp9_set_quantizer(cm, q); 2225 2226 if (loop_count == 0) { 2227 // Set up entropy context depending on frame type. The decoder mandates 2228 // the use of the default context, index 0, for keyframes and inter 2229 // frames where the error_resilient_mode or intra_only flag is set. For 2230 // other inter-frames the encoder currently uses only two contexts; 2231 // context 1 for ALTREF frames and context 0 for the others. 2232 if (cm->frame_type == KEY_FRAME) { 2233 setup_key_frame(cpi); 2234 } else { 2235 if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc) 2236 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; 2237 2238 setup_inter_frame(cm); 2239 } 2240 } 2241 2242 // Variance adaptive and in frame q adjustment experiments are mutually 2243 // exclusive. 2244 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 2245 vp9_vaq_frame_setup(cpi); 2246 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) { 2247 vp9_setup_in_frame_q_adj(cpi); 2248 } 2249 2250 // transform / motion compensation build reconstruction frame 2251 vp9_encode_frame(cpi); 2252 2253 // Update the skip mb flag probabilities based on the distribution 2254 // seen in the last encoder iteration. 2255 // update_base_skip_probs(cpi); 2256 2257 vp9_clear_system_state(); 2258 2259 // Dummy pack of the bitstream using up to date stats to get an 2260 // accurate estimate of output frame size to determine if we need 2261 // to recode. 2262 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) { 2263 save_coding_context(cpi); 2264 cpi->dummy_packing = 1; 2265 if (!cpi->sf.use_nonrd_pick_mode) 2266 vp9_pack_bitstream(cpi, dest, size); 2267 2268 rc->projected_frame_size = (int)(*size) << 3; 2269 restore_coding_context(cpi); 2270 2271 if (frame_over_shoot_limit == 0) 2272 frame_over_shoot_limit = 1; 2273 } 2274 2275 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 2276 loop = 0; 2277 } else { 2278 if ((cm->frame_type == KEY_FRAME) && 2279 rc->this_key_frame_forced && 2280 (rc->projected_frame_size < rc->max_frame_bandwidth)) { 2281 int last_q = q; 2282 int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm)); 2283 2284 int high_err_target = cpi->ambient_err; 2285 int low_err_target = cpi->ambient_err >> 1; 2286 2287 // Prevent possible divide by zero error below for perfect KF 2288 kf_err += !kf_err; 2289 2290 // The key frame is not good enough or we can afford 2291 // to make it better without undue risk of popping. 2292 if ((kf_err > high_err_target && 2293 rc->projected_frame_size <= frame_over_shoot_limit) || 2294 (kf_err > low_err_target && 2295 rc->projected_frame_size <= frame_under_shoot_limit)) { 2296 // Lower q_high 2297 q_high = q > q_low ? q - 1 : q_low; 2298 2299 // Adjust Q 2300 q = (q * high_err_target) / kf_err; 2301 q = MIN(q, (q_high + q_low) >> 1); 2302 } else if (kf_err < low_err_target && 2303 rc->projected_frame_size >= frame_under_shoot_limit) { 2304 // The key frame is much better than the previous frame 2305 // Raise q_low 2306 q_low = q < q_high ? q + 1 : q_high; 2307 2308 // Adjust Q 2309 q = (q * low_err_target) / kf_err; 2310 q = MIN(q, (q_high + q_low + 1) >> 1); 2311 } 2312 2313 // Clamp Q to upper and lower limits: 2314 q = clamp(q, q_low, q_high); 2315 2316 loop = q != last_q; 2317 } else if (recode_loop_test( 2318 cpi, frame_over_shoot_limit, frame_under_shoot_limit, 2319 q, MAX(q_high, top_index), bottom_index)) { 2320 // Is the projected frame size out of range and are we allowed 2321 // to attempt to recode. 2322 int last_q = q; 2323 int retries = 0; 2324 2325 // Frame size out of permitted range: 2326 // Update correction factor & compute new Q to try... 2327 2328 // Frame is too large 2329 if (rc->projected_frame_size > rc->this_frame_target) { 2330 // Special case if the projected size is > the max allowed. 2331 if (rc->projected_frame_size >= rc->max_frame_bandwidth) 2332 q_high = rc->worst_quality; 2333 2334 // Raise Qlow as to at least the current value 2335 q_low = q < q_high ? q + 1 : q_high; 2336 2337 if (undershoot_seen || loop_count > 1) { 2338 // Update rate_correction_factor unless 2339 vp9_rc_update_rate_correction_factors(cpi, 1); 2340 2341 q = (q_high + q_low + 1) / 2; 2342 } else { 2343 // Update rate_correction_factor unless 2344 vp9_rc_update_rate_correction_factors(cpi, 0); 2345 2346 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 2347 bottom_index, MAX(q_high, top_index)); 2348 2349 while (q < q_low && retries < 10) { 2350 vp9_rc_update_rate_correction_factors(cpi, 0); 2351 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 2352 bottom_index, MAX(q_high, top_index)); 2353 retries++; 2354 } 2355 } 2356 2357 overshoot_seen = 1; 2358 } else { 2359 // Frame is too small 2360 q_high = q > q_low ? q - 1 : q_low; 2361 2362 if (overshoot_seen || loop_count > 1) { 2363 vp9_rc_update_rate_correction_factors(cpi, 1); 2364 q = (q_high + q_low) / 2; 2365 } else { 2366 vp9_rc_update_rate_correction_factors(cpi, 0); 2367 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 2368 bottom_index, top_index); 2369 // Special case reset for qlow for constrained quality. 2370 // This should only trigger where there is very substantial 2371 // undershoot on a frame and the auto cq level is above 2372 // the user passsed in value. 2373 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && 2374 q < q_low) { 2375 q_low = q; 2376 } 2377 2378 while (q > q_high && retries < 10) { 2379 vp9_rc_update_rate_correction_factors(cpi, 0); 2380 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 2381 bottom_index, top_index); 2382 retries++; 2383 } 2384 } 2385 2386 undershoot_seen = 1; 2387 } 2388 2389 // Clamp Q to upper and lower limits: 2390 q = clamp(q, q_low, q_high); 2391 2392 loop = q != last_q; 2393 } else { 2394 loop = 0; 2395 } 2396 } 2397 2398 // Special case for overlay frame. 2399 if (rc->is_src_frame_alt_ref && 2400 rc->projected_frame_size < rc->max_frame_bandwidth) 2401 loop = 0; 2402 2403 if (loop) { 2404 loop_count++; 2405 2406#if CONFIG_INTERNAL_STATS 2407 cpi->tot_recode_hits++; 2408#endif 2409 } 2410 } while (loop); 2411} 2412 2413static void get_ref_frame_flags(VP9_COMP *cpi) { 2414 if (cpi->refresh_last_frame & cpi->refresh_golden_frame) 2415 cpi->gold_is_last = 1; 2416 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame) 2417 cpi->gold_is_last = 0; 2418 2419 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame) 2420 cpi->alt_is_last = 1; 2421 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame) 2422 cpi->alt_is_last = 0; 2423 2424 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame) 2425 cpi->gold_is_alt = 1; 2426 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame) 2427 cpi->gold_is_alt = 0; 2428 2429 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 2430 2431 if (cpi->gold_is_last) 2432 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG; 2433 2434 if (cpi->rc.frames_till_gf_update_due == INT_MAX) 2435 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG; 2436 2437 if (cpi->alt_is_last) 2438 cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 2439 2440 if (cpi->gold_is_alt) 2441 cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 2442} 2443 2444static void set_ext_overrides(VP9_COMP *cpi) { 2445 // Overrides the defaults with the externally supplied values with 2446 // vp9_update_reference() and vp9_update_entropy() calls 2447 // Note: The overrides are valid only for the next frame passed 2448 // to encode_frame_to_data_rate() function 2449 if (cpi->ext_refresh_frame_context_pending) { 2450 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context; 2451 cpi->ext_refresh_frame_context_pending = 0; 2452 } 2453 if (cpi->ext_refresh_frame_flags_pending) { 2454 cpi->refresh_last_frame = cpi->ext_refresh_last_frame; 2455 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame; 2456 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame; 2457 cpi->ext_refresh_frame_flags_pending = 0; 2458 } 2459} 2460 2461static void encode_frame_to_data_rate(VP9_COMP *cpi, 2462 size_t *size, 2463 uint8_t *dest, 2464 unsigned int *frame_flags) { 2465 VP9_COMMON *const cm = &cpi->common; 2466 TX_SIZE t; 2467 int q; 2468 int top_index; 2469 int bottom_index; 2470 2471 const SPEED_FEATURES *const sf = &cpi->sf; 2472 const unsigned int max_mv_def = MIN(cm->width, cm->height); 2473 struct segmentation *const seg = &cm->seg; 2474 2475 set_ext_overrides(cpi); 2476 2477 /* Scale the source buffer, if required. */ 2478 if (cm->mi_cols * MI_SIZE != cpi->un_scaled_source->y_width || 2479 cm->mi_rows * MI_SIZE != cpi->un_scaled_source->y_height) { 2480 scale_and_extend_frame_nonnormative(cpi->un_scaled_source, 2481 &cpi->scaled_source); 2482 cpi->Source = &cpi->scaled_source; 2483 } else { 2484 cpi->Source = cpi->un_scaled_source; 2485 } 2486 2487 // Scale the last source buffer, if required. 2488 if (cpi->unscaled_last_source != NULL) { 2489 if (cm->mi_cols * MI_SIZE != cpi->unscaled_last_source->y_width || 2490 cm->mi_rows * MI_SIZE != cpi->unscaled_last_source->y_height) { 2491 scale_and_extend_frame_nonnormative(cpi->unscaled_last_source, 2492 &cpi->scaled_last_source); 2493 cpi->Last_Source = &cpi->scaled_last_source; 2494 } else { 2495 cpi->Last_Source = cpi->unscaled_last_source; 2496 } 2497 } 2498 2499 vp9_scale_references(cpi); 2500 2501 vp9_clear_system_state(); 2502 2503 // Enable or disable mode based tweaking of the zbin. 2504 // For 2 pass only used where GF/ARF prediction quality 2505 // is above a threshold. 2506 cpi->zbin_mode_boost = 0; 2507 cpi->zbin_mode_boost_enabled = 0; 2508 2509 // Current default encoder behavior for the altref sign bias. 2510 cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active; 2511 2512 // Set default state for segment based loop filter update flags. 2513 cm->lf.mode_ref_delta_update = 0; 2514 2515 // Initialize cpi->mv_step_param to default based on max resolution. 2516 cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def); 2517 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate. 2518 if (sf->auto_mv_step_size) { 2519 if (frame_is_intra_only(cm)) { 2520 // Initialize max_mv_magnitude for use in the first INTER frame 2521 // after a key/intra-only frame. 2522 cpi->max_mv_magnitude = max_mv_def; 2523 } else { 2524 if (cm->show_frame) 2525 // Allow mv_steps to correspond to twice the max mv magnitude found 2526 // in the previous frame, capped by the default max_mv_magnitude based 2527 // on resolution. 2528 cpi->mv_step_param = vp9_init_search_range(cpi, MIN(max_mv_def, 2 * 2529 cpi->max_mv_magnitude)); 2530 cpi->max_mv_magnitude = 0; 2531 } 2532 } 2533 2534 // Set various flags etc to special state if it is a key frame. 2535 if (frame_is_intra_only(cm)) { 2536 setup_key_frame(cpi); 2537 // Reset the loop filter deltas and segmentation map. 2538 vp9_reset_segment_features(&cm->seg); 2539 2540 // If segmentation is enabled force a map update for key frames. 2541 if (seg->enabled) { 2542 seg->update_map = 1; 2543 seg->update_data = 1; 2544 } 2545 2546 // The alternate reference frame cannot be active for a key frame. 2547 cpi->rc.source_alt_ref_active = 0; 2548 2549 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0); 2550 cm->frame_parallel_decoding_mode = 2551 (cpi->oxcf.frame_parallel_decoding_mode != 0); 2552 2553 // By default, encoder assumes decoder can use prev_mi. 2554 cm->coding_use_prev_mi = 1; 2555 if (cm->error_resilient_mode) { 2556 cm->coding_use_prev_mi = 0; 2557 cm->frame_parallel_decoding_mode = 1; 2558 cm->reset_frame_context = 0; 2559 cm->refresh_frame_context = 0; 2560 } else if (cm->intra_only) { 2561 // Only reset the current context. 2562 cm->reset_frame_context = 2; 2563 } 2564 } 2565 2566 // Configure experimental use of segmentation for enhanced coding of 2567 // static regions if indicated. 2568 // Only allowed in second pass of two pass (as requires lagged coding) 2569 // and if the relevant speed feature flag is set. 2570 if (cpi->pass == 2 && cpi->sf.static_segmentation) 2571 configure_static_seg_features(cpi); 2572 2573 // For 1 pass CBR, check if we are dropping this frame. 2574 // Never drop on key frame. 2575 if (cpi->pass == 0 && 2576 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER && 2577 cm->frame_type != KEY_FRAME) { 2578 if (vp9_rc_drop_frame(cpi)) { 2579 vp9_rc_postencode_update_drop_frame(cpi); 2580 ++cm->current_video_frame; 2581 return; 2582 } 2583 } 2584 2585 vp9_clear_system_state(); 2586 2587 vp9_zero(cpi->rd_tx_select_threshes); 2588 2589#if CONFIG_VP9_POSTPROC 2590 if (cpi->oxcf.noise_sensitivity > 0) { 2591 int l = 0; 2592 switch (cpi->oxcf.noise_sensitivity) { 2593 case 1: 2594 l = 20; 2595 break; 2596 case 2: 2597 l = 40; 2598 break; 2599 case 3: 2600 l = 60; 2601 break; 2602 case 4: 2603 case 5: 2604 l = 100; 2605 break; 2606 case 6: 2607 l = 150; 2608 break; 2609 } 2610 vp9_denoise(cpi->Source, cpi->Source, l); 2611 } 2612#endif 2613 2614#ifdef OUTPUT_YUV_SRC 2615 vp9_write_yuv_frame(cpi->Source); 2616#endif 2617 2618 set_speed_features(cpi); 2619 2620 // Decide q and q bounds. 2621 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index); 2622 2623 if (!frame_is_intra_only(cm)) { 2624 cm->interp_filter = DEFAULT_INTERP_FILTER; 2625 /* TODO: Decide this more intelligently */ 2626 set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH); 2627 } 2628 2629 if (cpi->sf.recode_loop == DISALLOW_RECODE) { 2630 encode_without_recode_loop(cpi, size, dest, q); 2631 } else { 2632 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index); 2633 } 2634 2635 // Special case code to reduce pulsing when key frames are forced at a 2636 // fixed interval. Note the reconstruction error if it is the frame before 2637 // the force key frame 2638 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) { 2639 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm)); 2640 } 2641 2642 // If the encoder forced a KEY_FRAME decision 2643 if (cm->frame_type == KEY_FRAME) 2644 cpi->refresh_last_frame = 1; 2645 2646 cm->frame_to_show = get_frame_new_buffer(cm); 2647 2648#if WRITE_RECON_BUFFER 2649 if (cm->show_frame) 2650 write_cx_frame_to_file(cm->frame_to_show, 2651 cm->current_video_frame); 2652 else 2653 write_cx_frame_to_file(cm->frame_to_show, 2654 cm->current_video_frame + 1000); 2655#endif 2656 2657 // Pick the loop filter level for the frame. 2658 loopfilter_frame(cpi, cm); 2659 2660#if WRITE_RECON_BUFFER 2661 if (cm->show_frame) 2662 write_cx_frame_to_file(cm->frame_to_show, 2663 cm->current_video_frame + 2000); 2664 else 2665 write_cx_frame_to_file(cm->frame_to_show, 2666 cm->current_video_frame + 3000); 2667#endif 2668 2669 // build the bitstream 2670 cpi->dummy_packing = 0; 2671 vp9_pack_bitstream(cpi, dest, size); 2672 2673 if (cm->seg.update_map) 2674 update_reference_segmentation_map(cpi); 2675 2676 release_scaled_references(cpi); 2677 vp9_update_reference_frames(cpi); 2678 2679 for (t = TX_4X4; t <= TX_32X32; t++) 2680 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]); 2681 2682 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) 2683 vp9_adapt_coef_probs(cm); 2684 2685 if (!frame_is_intra_only(cm)) { 2686 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) { 2687 vp9_adapt_mode_probs(cm); 2688 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv); 2689 } 2690 } 2691 2692#if 0 2693 output_frame_level_debug_stats(cpi); 2694#endif 2695 if (cpi->refresh_golden_frame == 1) 2696 cm->frame_flags |= FRAMEFLAGS_GOLDEN; 2697 else 2698 cm->frame_flags &= ~FRAMEFLAGS_GOLDEN; 2699 2700 if (cpi->refresh_alt_ref_frame == 1) 2701 cm->frame_flags |= FRAMEFLAGS_ALTREF; 2702 else 2703 cm->frame_flags &= ~FRAMEFLAGS_ALTREF; 2704 2705 get_ref_frame_flags(cpi); 2706 2707 vp9_rc_postencode_update(cpi, *size); 2708 2709 if (cm->frame_type == KEY_FRAME) { 2710 // Tell the caller that the frame was coded as a key frame 2711 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; 2712 2713#if CONFIG_MULTIPLE_ARF 2714 // Reset the sequence number. 2715 if (cpi->multi_arf_enabled) { 2716 cpi->sequence_number = 0; 2717 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 2718 cpi->new_frame_coding_order_period = -1; 2719 } 2720#endif 2721 } else { 2722 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; 2723 2724#if CONFIG_MULTIPLE_ARF 2725 /* Increment position in the coded frame sequence. */ 2726 if (cpi->multi_arf_enabled) { 2727 ++cpi->sequence_number; 2728 if (cpi->sequence_number >= cpi->frame_coding_order_period) { 2729 cpi->sequence_number = 0; 2730 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 2731 cpi->new_frame_coding_order_period = -1; 2732 } 2733 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number]; 2734 assert(cpi->this_frame_weight >= 0); 2735 } 2736#endif 2737 } 2738 2739 // Clear the one shot update flags for segmentation map and mode/ref loop 2740 // filter deltas. 2741 cm->seg.update_map = 0; 2742 cm->seg.update_data = 0; 2743 cm->lf.mode_ref_delta_update = 0; 2744 2745 // keep track of the last coded dimensions 2746 cm->last_width = cm->width; 2747 cm->last_height = cm->height; 2748 2749 // reset to normal state now that we are done. 2750 if (!cm->show_existing_frame) 2751 cm->last_show_frame = cm->show_frame; 2752 2753 if (cm->show_frame) { 2754 vp9_swap_mi_and_prev_mi(cm); 2755 2756 // Don't increment frame counters if this was an altref buffer 2757 // update not a real frame 2758 ++cm->current_video_frame; 2759 if (cpi->use_svc) 2760 vp9_inc_frame_in_layer(&cpi->svc); 2761 } 2762 2763 // restore prev_mi 2764 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1; 2765 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1; 2766} 2767 2768static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest, 2769 unsigned int *frame_flags) { 2770 vp9_rc_get_svc_params(cpi); 2771 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 2772} 2773 2774static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest, 2775 unsigned int *frame_flags) { 2776 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 2777 vp9_rc_get_one_pass_cbr_params(cpi); 2778 } else { 2779 vp9_rc_get_one_pass_vbr_params(cpi); 2780 } 2781 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 2782} 2783 2784static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest, 2785 unsigned int *frame_flags) { 2786 (void) size; 2787 (void) dest; 2788 (void) frame_flags; 2789 2790 vp9_rc_get_first_pass_params(cpi); 2791 vp9_set_quantizer(&cpi->common, find_fp_qindex()); 2792 vp9_first_pass(cpi); 2793} 2794 2795static void Pass2Encode(VP9_COMP *cpi, size_t *size, 2796 uint8_t *dest, unsigned int *frame_flags) { 2797 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED; 2798 2799 vp9_rc_get_second_pass_params(cpi); 2800 encode_frame_to_data_rate(cpi, size, dest, frame_flags); 2801 2802 vp9_twopass_postencode_update(cpi); 2803} 2804 2805static void check_initial_width(VP9_COMP *cpi, int subsampling_x, 2806 int subsampling_y) { 2807 VP9_COMMON *const cm = &cpi->common; 2808 2809 if (!cpi->initial_width) { 2810 cm->subsampling_x = subsampling_x; 2811 cm->subsampling_y = subsampling_y; 2812 alloc_raw_frame_buffers(cpi); 2813 cpi->initial_width = cm->width; 2814 cpi->initial_height = cm->height; 2815 } 2816} 2817 2818 2819int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags, 2820 YV12_BUFFER_CONFIG *sd, int64_t time_stamp, 2821 int64_t end_time) { 2822 VP9_COMMON *cm = &cpi->common; 2823 struct vpx_usec_timer timer; 2824 int res = 0; 2825 const int subsampling_x = sd->uv_width < sd->y_width; 2826 const int subsampling_y = sd->uv_height < sd->y_height; 2827 2828 check_initial_width(cpi, subsampling_x, subsampling_y); 2829 vpx_usec_timer_start(&timer); 2830 if (vp9_lookahead_push(cpi->lookahead, 2831 sd, time_stamp, end_time, frame_flags)) 2832 res = -1; 2833 vpx_usec_timer_mark(&timer); 2834 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); 2835 2836 if (cm->profile == PROFILE_0 && (subsampling_x != 1 || subsampling_y != 1)) { 2837 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM, 2838 "Non-4:2:0 color space requires profile >= 1"); 2839 res = -1; 2840 } 2841 2842 return res; 2843} 2844 2845 2846static int frame_is_reference(const VP9_COMP *cpi) { 2847 const VP9_COMMON *cm = &cpi->common; 2848 2849 return cm->frame_type == KEY_FRAME || 2850 cpi->refresh_last_frame || 2851 cpi->refresh_golden_frame || 2852 cpi->refresh_alt_ref_frame || 2853 cm->refresh_frame_context || 2854 cm->lf.mode_ref_delta_update || 2855 cm->seg.update_map || 2856 cm->seg.update_data; 2857} 2858 2859#if CONFIG_MULTIPLE_ARF 2860int is_next_frame_arf(VP9_COMP *cpi) { 2861 // Negative entry in frame_coding_order indicates an ARF at this position. 2862 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; 2863} 2864#endif 2865 2866void adjust_frame_rate(VP9_COMP *cpi) { 2867 int64_t this_duration; 2868 int step = 0; 2869 2870 if (cpi->source->ts_start == cpi->first_time_stamp_ever) { 2871 this_duration = cpi->source->ts_end - cpi->source->ts_start; 2872 step = 1; 2873 } else { 2874 int64_t last_duration = cpi->last_end_time_stamp_seen 2875 - cpi->last_time_stamp_seen; 2876 2877 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen; 2878 2879 // do a step update if the duration changes by 10% 2880 if (last_duration) 2881 step = (int)((this_duration - last_duration) * 10 / last_duration); 2882 } 2883 2884 if (this_duration) { 2885 if (step) { 2886 vp9_new_framerate(cpi, 10000000.0 / this_duration); 2887 } else { 2888 // Average this frame's rate into the last second's average 2889 // frame rate. If we haven't seen 1 second yet, then average 2890 // over the whole interval seen. 2891 const double interval = MIN((double)(cpi->source->ts_end 2892 - cpi->first_time_stamp_ever), 10000000.0); 2893 double avg_duration = 10000000.0 / cpi->oxcf.framerate; 2894 avg_duration *= (interval - avg_duration + this_duration); 2895 avg_duration /= interval; 2896 2897 vp9_new_framerate(cpi, 10000000.0 / avg_duration); 2898 } 2899 } 2900 cpi->last_time_stamp_seen = cpi->source->ts_start; 2901 cpi->last_end_time_stamp_seen = cpi->source->ts_end; 2902} 2903 2904int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags, 2905 size_t *size, uint8_t *dest, 2906 int64_t *time_stamp, int64_t *time_end, int flush) { 2907 VP9_COMMON *const cm = &cpi->common; 2908 MACROBLOCKD *const xd = &cpi->mb.e_mbd; 2909 RATE_CONTROL *const rc = &cpi->rc; 2910 struct vpx_usec_timer cmptimer; 2911 YV12_BUFFER_CONFIG *force_src_buffer = NULL; 2912 MV_REFERENCE_FRAME ref_frame; 2913 2914 if (!cpi) 2915 return -1; 2916 2917 if (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2) { 2918 vp9_restore_layer_context(cpi); 2919 } 2920 2921 vpx_usec_timer_start(&cmptimer); 2922 2923 cpi->source = NULL; 2924 cpi->last_source = NULL; 2925 2926 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV); 2927 2928 // Normal defaults 2929 cm->reset_frame_context = 0; 2930 cm->refresh_frame_context = 1; 2931 cpi->refresh_last_frame = 1; 2932 cpi->refresh_golden_frame = 0; 2933 cpi->refresh_alt_ref_frame = 0; 2934 2935 // Should we code an alternate reference frame. 2936 if (cpi->oxcf.play_alternate && rc->source_alt_ref_pending) { 2937 int frames_to_arf; 2938 2939#if CONFIG_MULTIPLE_ARF 2940 assert(!cpi->multi_arf_enabled || 2941 cpi->frame_coding_order[cpi->sequence_number] < 0); 2942 2943 if (cpi->multi_arf_enabled && (cpi->pass == 2)) 2944 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) 2945 - cpi->next_frame_in_order; 2946 else 2947#endif 2948 frames_to_arf = rc->frames_till_gf_update_due; 2949 2950 assert(frames_to_arf <= rc->frames_to_key); 2951 2952 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { 2953#if CONFIG_MULTIPLE_ARF 2954 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; 2955#else 2956 cpi->alt_ref_source = cpi->source; 2957#endif 2958 2959 if (cpi->oxcf.arnr_max_frames > 0) { 2960 // Produce the filtered ARF frame. 2961 // TODO(agrange) merge these two functions. 2962 vp9_configure_arnr_filter(cpi, frames_to_arf, rc->gfu_boost); 2963 vp9_temporal_filter_prepare(cpi, frames_to_arf); 2964 vp9_extend_frame_borders(&cpi->alt_ref_buffer); 2965 force_src_buffer = &cpi->alt_ref_buffer; 2966 } 2967 2968 cm->show_frame = 0; 2969 cpi->refresh_alt_ref_frame = 1; 2970 cpi->refresh_golden_frame = 0; 2971 cpi->refresh_last_frame = 0; 2972 rc->is_src_frame_alt_ref = 0; 2973 2974#if CONFIG_MULTIPLE_ARF 2975 if (!cpi->multi_arf_enabled) 2976#endif 2977 rc->source_alt_ref_pending = 0; 2978 } else { 2979 rc->source_alt_ref_pending = 0; 2980 } 2981 } 2982 2983 if (!cpi->source) { 2984#if CONFIG_MULTIPLE_ARF 2985 int i; 2986#endif 2987 2988 // Get last frame source. 2989 if (cm->current_video_frame > 0) { 2990 if ((cpi->last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL) 2991 return -1; 2992 } 2993 2994 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) { 2995 cm->show_frame = 1; 2996 cm->intra_only = 0; 2997 2998#if CONFIG_MULTIPLE_ARF 2999 // Is this frame the ARF overlay. 3000 rc->is_src_frame_alt_ref = 0; 3001 for (i = 0; i < cpi->arf_buffered; ++i) { 3002 if (cpi->source == cpi->alt_ref_source[i]) { 3003 rc->is_src_frame_alt_ref = 1; 3004 cpi->refresh_golden_frame = 1; 3005 break; 3006 } 3007 } 3008#else 3009 rc->is_src_frame_alt_ref = cpi->alt_ref_source && 3010 (cpi->source == cpi->alt_ref_source); 3011#endif 3012 if (rc->is_src_frame_alt_ref) { 3013 // Current frame is an ARF overlay frame. 3014#if CONFIG_MULTIPLE_ARF 3015 cpi->alt_ref_source[i] = NULL; 3016#else 3017 cpi->alt_ref_source = NULL; 3018#endif 3019 // Don't refresh the last buffer for an ARF overlay frame. It will 3020 // become the GF so preserve last as an alternative prediction option. 3021 cpi->refresh_last_frame = 0; 3022 } 3023#if CONFIG_MULTIPLE_ARF 3024 ++cpi->next_frame_in_order; 3025#endif 3026 } 3027 } 3028 3029 if (cpi->source) { 3030 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer 3031 : &cpi->source->img; 3032 3033 if (cpi->last_source != NULL) { 3034 cpi->unscaled_last_source = &cpi->last_source->img; 3035 } else { 3036 cpi->unscaled_last_source = NULL; 3037 } 3038 3039 *time_stamp = cpi->source->ts_start; 3040 *time_end = cpi->source->ts_end; 3041 *frame_flags = cpi->source->flags; 3042 3043#if CONFIG_MULTIPLE_ARF 3044 if (cm->frame_type != KEY_FRAME && cpi->pass == 2) 3045 rc->source_alt_ref_pending = is_next_frame_arf(cpi); 3046#endif 3047 } else { 3048 *size = 0; 3049 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) { 3050 vp9_end_first_pass(cpi); /* get last stats packet */ 3051 cpi->twopass.first_pass_done = 1; 3052 } 3053 return -1; 3054 } 3055 3056 if (cpi->source->ts_start < cpi->first_time_stamp_ever) { 3057 cpi->first_time_stamp_ever = cpi->source->ts_start; 3058 cpi->last_end_time_stamp_seen = cpi->source->ts_start; 3059 } 3060 3061 // adjust frame rates based on timestamps given 3062 if (cm->show_frame) { 3063 adjust_frame_rate(cpi); 3064 } 3065 3066 if (cpi->svc.number_temporal_layers > 1 && 3067 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { 3068 vp9_update_temporal_layer_framerate(cpi); 3069 vp9_restore_layer_context(cpi); 3070 } 3071 3072 // start with a 0 size frame 3073 *size = 0; 3074 3075 // Clear down mmx registers 3076 vp9_clear_system_state(); 3077 3078 /* find a free buffer for the new frame, releasing the reference previously 3079 * held. 3080 */ 3081 cm->frame_bufs[cm->new_fb_idx].ref_count--; 3082 cm->new_fb_idx = get_free_fb(cm); 3083 3084#if CONFIG_MULTIPLE_ARF 3085 /* Set up the correct ARF frame. */ 3086 if (cpi->refresh_alt_ref_frame) { 3087 ++cpi->arf_buffered; 3088 } 3089 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 3090 (cpi->pass == 2)) { 3091 cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number]; 3092 } 3093#endif 3094 3095 cm->frame_flags = *frame_flags; 3096 3097 // Reset the frame pointers to the current frame size 3098 vp9_realloc_frame_buffer(get_frame_new_buffer(cm), 3099 cm->width, cm->height, 3100 cm->subsampling_x, cm->subsampling_y, 3101 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL); 3102 3103 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { 3104 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)]; 3105 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf; 3106 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1]; 3107 ref_buf->buf = buf; 3108 ref_buf->idx = idx; 3109 vp9_setup_scale_factors_for_frame(&ref_buf->sf, 3110 buf->y_crop_width, buf->y_crop_height, 3111 cm->width, cm->height); 3112 3113 if (vp9_is_scaled(&ref_buf->sf)) 3114 vp9_extend_frame_borders(buf); 3115 } 3116 3117 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME); 3118 3119 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 3120 vp9_vaq_init(); 3121 } 3122 3123 if (cpi->pass == 1 && 3124 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) { 3125 Pass1Encode(cpi, size, dest, frame_flags); 3126 } else if (cpi->pass == 2 && 3127 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) { 3128 Pass2Encode(cpi, size, dest, frame_flags); 3129 } else if (cpi->use_svc) { 3130 SvcEncode(cpi, size, dest, frame_flags); 3131 } else { 3132 // One pass encode 3133 Pass0Encode(cpi, size, dest, frame_flags); 3134 } 3135 3136 if (cm->refresh_frame_context) 3137 cm->frame_contexts[cm->frame_context_idx] = cm->fc; 3138 3139 // Frame was dropped, release scaled references. 3140 if (*size == 0) { 3141 release_scaled_references(cpi); 3142 } 3143 3144 if (*size > 0) { 3145 cpi->droppable = !frame_is_reference(cpi); 3146 } 3147 3148 // Save layer specific state. 3149 if ((cpi->svc.number_temporal_layers > 1 && 3150 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) || 3151 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) { 3152 vp9_save_layer_context(cpi); 3153 } 3154 3155 vpx_usec_timer_mark(&cmptimer); 3156 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); 3157 3158 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) 3159 generate_psnr_packet(cpi); 3160 3161#if CONFIG_INTERNAL_STATS 3162 3163 if (cpi->pass != 1) { 3164 cpi->bytes += (int)(*size); 3165 3166 if (cm->show_frame) { 3167 cpi->count++; 3168 3169 if (cpi->b_calculate_psnr) { 3170 YV12_BUFFER_CONFIG *orig = cpi->Source; 3171 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 3172 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer; 3173 PSNR_STATS psnr; 3174 calc_psnr(orig, recon, &psnr); 3175 3176 cpi->total += psnr.psnr[0]; 3177 cpi->total_y += psnr.psnr[1]; 3178 cpi->total_u += psnr.psnr[2]; 3179 cpi->total_v += psnr.psnr[3]; 3180 cpi->total_sq_error += psnr.sse[0]; 3181 cpi->total_samples += psnr.samples[0]; 3182 3183 { 3184 PSNR_STATS psnr2; 3185 double frame_ssim2 = 0, weight = 0; 3186#if CONFIG_VP9_POSTPROC 3187 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer, 3188 cm->lf.filter_level * 10 / 6); 3189#endif 3190 vp9_clear_system_state(); 3191 3192 calc_psnr(orig, pp, &psnr2); 3193 3194 cpi->totalp += psnr2.psnr[0]; 3195 cpi->totalp_y += psnr2.psnr[1]; 3196 cpi->totalp_u += psnr2.psnr[2]; 3197 cpi->totalp_v += psnr2.psnr[3]; 3198 cpi->totalp_sq_error += psnr2.sse[0]; 3199 cpi->totalp_samples += psnr2.samples[0]; 3200 3201 frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight); 3202 3203 cpi->summed_quality += frame_ssim2 * weight; 3204 cpi->summed_weights += weight; 3205 3206 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight); 3207 3208 cpi->summedp_quality += frame_ssim2 * weight; 3209 cpi->summedp_weights += weight; 3210#if 0 3211 { 3212 FILE *f = fopen("q_used.stt", "a"); 3213 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n", 3214 cpi->common.current_video_frame, y2, u2, v2, 3215 frame_psnr2, frame_ssim2); 3216 fclose(f); 3217 } 3218#endif 3219 } 3220 } 3221 3222 if (cpi->b_calculate_ssimg) { 3223 double y, u, v, frame_all; 3224 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v); 3225 cpi->total_ssimg_y += y; 3226 cpi->total_ssimg_u += u; 3227 cpi->total_ssimg_v += v; 3228 cpi->total_ssimg_all += frame_all; 3229 } 3230 } 3231 } 3232 3233#endif 3234 return 0; 3235} 3236 3237int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest, 3238 vp9_ppflags_t *flags) { 3239 VP9_COMMON *cm = &cpi->common; 3240 3241 if (!cm->show_frame) { 3242 return -1; 3243 } else { 3244 int ret; 3245#if CONFIG_VP9_POSTPROC 3246 ret = vp9_post_proc_frame(cm, dest, flags); 3247#else 3248 3249 if (cm->frame_to_show) { 3250 *dest = *cm->frame_to_show; 3251 dest->y_width = cm->width; 3252 dest->y_height = cm->height; 3253 dest->uv_width = cm->width >> cm->subsampling_x; 3254 dest->uv_height = cm->height >> cm->subsampling_y; 3255 ret = 0; 3256 } else { 3257 ret = -1; 3258 } 3259 3260#endif // !CONFIG_VP9_POSTPROC 3261 vp9_clear_system_state(); 3262 return ret; 3263 } 3264} 3265 3266int vp9_set_roimap(VP9_COMP *cpi, unsigned char *map, unsigned int rows, 3267 unsigned int cols, int delta_q[MAX_SEGMENTS], 3268 int delta_lf[MAX_SEGMENTS], 3269 unsigned int threshold[MAX_SEGMENTS]) { 3270 signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS]; 3271 struct segmentation *seg = &cpi->common.seg; 3272 int i; 3273 3274 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) 3275 return -1; 3276 3277 if (!map) { 3278 vp9_disable_segmentation(seg); 3279 return 0; 3280 } 3281 3282 // Set the segmentation Map 3283 vp9_set_segmentation_map(cpi, map); 3284 3285 // Activate segmentation. 3286 vp9_enable_segmentation(seg); 3287 3288 // Set up the quant, LF and breakout threshold segment data 3289 for (i = 0; i < MAX_SEGMENTS; i++) { 3290 feature_data[SEG_LVL_ALT_Q][i] = delta_q[i]; 3291 feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i]; 3292 cpi->segment_encode_breakout[i] = threshold[i]; 3293 } 3294 3295 // Enable the loop and quant changes in the feature mask 3296 for (i = 0; i < MAX_SEGMENTS; i++) { 3297 if (delta_q[i]) 3298 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q); 3299 else 3300 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q); 3301 3302 if (delta_lf[i]) 3303 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF); 3304 else 3305 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF); 3306 } 3307 3308 // Initialize the feature data structure 3309 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 3310 vp9_set_segment_data(seg, &feature_data[0][0], SEGMENT_DELTADATA); 3311 3312 return 0; 3313} 3314 3315int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, 3316 unsigned int rows, unsigned int cols) { 3317 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) { 3318 if (map) { 3319 vpx_memcpy(cpi->active_map, map, rows * cols); 3320 cpi->active_map_enabled = 1; 3321 } else { 3322 cpi->active_map_enabled = 0; 3323 } 3324 3325 return 0; 3326 } else { 3327 // cpi->active_map_enabled = 0; 3328 return -1; 3329 } 3330} 3331 3332int vp9_set_internal_size(VP9_COMP *cpi, 3333 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) { 3334 VP9_COMMON *cm = &cpi->common; 3335 int hr = 0, hs = 0, vr = 0, vs = 0; 3336 3337 if (horiz_mode > ONETWO || vert_mode > ONETWO) 3338 return -1; 3339 3340 Scale2Ratio(horiz_mode, &hr, &hs); 3341 Scale2Ratio(vert_mode, &vr, &vs); 3342 3343 // always go to the next whole number 3344 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs; 3345 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs; 3346 3347 assert(cm->width <= cpi->initial_width); 3348 assert(cm->height <= cpi->initial_height); 3349 update_frame_size(cpi); 3350 return 0; 3351} 3352 3353int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width, 3354 unsigned int height) { 3355 VP9_COMMON *cm = &cpi->common; 3356 3357 check_initial_width(cpi, 1, 1); 3358 3359 if (width) { 3360 cm->width = width; 3361 if (cm->width * 5 < cpi->initial_width) { 3362 cm->width = cpi->initial_width / 5 + 1; 3363 printf("Warning: Desired width too small, changed to %d\n", cm->width); 3364 } 3365 if (cm->width > cpi->initial_width) { 3366 cm->width = cpi->initial_width; 3367 printf("Warning: Desired width too large, changed to %d\n", cm->width); 3368 } 3369 } 3370 3371 if (height) { 3372 cm->height = height; 3373 if (cm->height * 5 < cpi->initial_height) { 3374 cm->height = cpi->initial_height / 5 + 1; 3375 printf("Warning: Desired height too small, changed to %d\n", cm->height); 3376 } 3377 if (cm->height > cpi->initial_height) { 3378 cm->height = cpi->initial_height; 3379 printf("Warning: Desired height too large, changed to %d\n", cm->height); 3380 } 3381 } 3382 3383 assert(cm->width <= cpi->initial_width); 3384 assert(cm->height <= cpi->initial_height); 3385 update_frame_size(cpi); 3386 return 0; 3387} 3388 3389void vp9_set_svc(VP9_COMP *cpi, int use_svc) { 3390 cpi->use_svc = use_svc; 3391 return; 3392} 3393 3394int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) { 3395 assert(a->y_crop_width == b->y_crop_width); 3396 assert(a->y_crop_height == b->y_crop_height); 3397 3398 return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride, 3399 a->y_crop_width, a->y_crop_height); 3400} 3401 3402 3403int vp9_get_quantizer(VP9_COMP *cpi) { 3404 return cpi->common.base_qindex; 3405} 3406