1/* 2 * Copyright (c) 2013 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 13#include "vp9/encoder/vp9_aq_variance.h" 14 15#include "vp9/common/vp9_seg_common.h" 16 17#include "vp9/encoder/vp9_ratectrl.h" 18#include "vp9/encoder/vp9_rd.h" 19#include "vp9/encoder/vp9_segmentation.h" 20#include "vp9/common/vp9_systemdependent.h" 21 22#define ENERGY_MIN (-1) 23#define ENERGY_MAX (1) 24#define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) 25#define ENERGY_IN_BOUNDS(energy)\ 26 assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) 27 28static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; 29static double rdmult_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; 30static int segment_id[MAX_SEGMENTS] = { 5, 3, 1, 0, 2, 4, 6, 7 }; 31 32#define Q_RATIO(i) q_ratio[(i) - ENERGY_MIN] 33#define RDMULT_RATIO(i) rdmult_ratio[(i) - ENERGY_MIN] 34#define SEGMENT_ID(i) segment_id[(i) - ENERGY_MIN] 35 36DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0}; 37 38unsigned int vp9_vaq_segment_id(int energy) { 39 ENERGY_IN_BOUNDS(energy); 40 41 return SEGMENT_ID(energy); 42} 43 44double vp9_vaq_rdmult_ratio(int energy) { 45 ENERGY_IN_BOUNDS(energy); 46 47 vp9_clear_system_state(); 48 49 return RDMULT_RATIO(energy); 50} 51 52double vp9_vaq_inv_q_ratio(int energy) { 53 ENERGY_IN_BOUNDS(energy); 54 55 vp9_clear_system_state(); 56 57 return Q_RATIO(-energy); 58} 59 60void vp9_vaq_init() { 61 int i; 62 double base_ratio; 63 64 assert(ENERGY_SPAN <= MAX_SEGMENTS); 65 66 vp9_clear_system_state(); 67 68 base_ratio = 1.5; 69 70 for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { 71 Q_RATIO(i) = pow(base_ratio, i/3.0); 72 } 73} 74 75void vp9_vaq_frame_setup(VP9_COMP *cpi) { 76 VP9_COMMON *cm = &cpi->common; 77 struct segmentation *seg = &cm->seg; 78 const double base_q = vp9_convert_qindex_to_q(cm->base_qindex); 79 const int base_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + 80 cm->y_dc_delta_q); 81 int i; 82 83 if (cm->frame_type == KEY_FRAME || 84 cpi->refresh_alt_ref_frame || 85 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) { 86 vp9_enable_segmentation(seg); 87 vp9_clearall_segfeatures(seg); 88 89 seg->abs_delta = SEGMENT_DELTADATA; 90 91 vp9_clear_system_state(); 92 93 for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { 94 int qindex_delta, segment_rdmult; 95 96 if (Q_RATIO(i) == 1) { 97 // No need to enable SEG_LVL_ALT_Q for this segment 98 RDMULT_RATIO(i) = 1; 99 continue; 100 } 101 102 qindex_delta = vp9_compute_qdelta(&cpi->rc, base_q, base_q * Q_RATIO(i)); 103 vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta); 104 vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q); 105 106 segment_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + qindex_delta + 107 cm->y_dc_delta_q); 108 109 RDMULT_RATIO(i) = (double) segment_rdmult / base_rdmult; 110 } 111 } 112} 113 114 115static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x, 116 BLOCK_SIZE bs) { 117 MACROBLOCKD *xd = &x->e_mbd; 118 unsigned int var, sse; 119 int right_overflow = (xd->mb_to_right_edge < 0) ? 120 ((-xd->mb_to_right_edge) >> 3) : 0; 121 int bottom_overflow = (xd->mb_to_bottom_edge < 0) ? 122 ((-xd->mb_to_bottom_edge) >> 3) : 0; 123 124 if (right_overflow || bottom_overflow) { 125 const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow; 126 const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow; 127 int avg; 128 variance(x->plane[0].src.buf, x->plane[0].src.stride, 129 vp9_64_zeros, 0, bw, bh, &sse, &avg); 130 var = sse - (((int64_t)avg * avg) / (bw * bh)); 131 return (256 * var) / (bw * bh); 132 } else { 133 var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, 134 x->plane[0].src.stride, 135 vp9_64_zeros, 0, &sse); 136 return (256 * var) >> num_pels_log2_lookup[bs]; 137 } 138} 139 140int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { 141 double energy; 142 unsigned int var = block_variance(cpi, x, bs); 143 144 vp9_clear_system_state(); 145 146 energy = 0.9 * (log(var + 1.0) - 10.0); 147 return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX); 148} 149