michael@0: /* michael@0: * Copyright (c) 2013 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "vp9/encoder/vp9_vaq.h" michael@0: michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: michael@0: #include "vp9/encoder/vp9_ratectrl.h" michael@0: #include "vp9/encoder/vp9_rdopt.h" michael@0: #include "vp9/encoder/vp9_segmentation.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: michael@0: #define ENERGY_MIN (-3) michael@0: #define ENERGY_MAX (3) michael@0: #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1) michael@0: #define ENERGY_IN_BOUNDS(energy)\ michael@0: assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX) michael@0: michael@0: static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; michael@0: static double rdmult_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 }; michael@0: static int segment_id[MAX_SEGMENTS] = { 5, 3, 1, 0, 2, 4, 6, 7 }; michael@0: michael@0: #define Q_RATIO(i) q_ratio[(i) - ENERGY_MIN] michael@0: #define RDMULT_RATIO(i) rdmult_ratio[(i) - ENERGY_MIN] michael@0: #define SEGMENT_ID(i) segment_id[(i) - ENERGY_MIN] michael@0: michael@0: DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0}; michael@0: michael@0: unsigned int vp9_vaq_segment_id(int energy) { michael@0: ENERGY_IN_BOUNDS(energy); michael@0: michael@0: return SEGMENT_ID(energy); michael@0: } michael@0: michael@0: double vp9_vaq_rdmult_ratio(int energy) { michael@0: ENERGY_IN_BOUNDS(energy); michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: michael@0: return RDMULT_RATIO(energy); michael@0: } michael@0: michael@0: double vp9_vaq_inv_q_ratio(int energy) { michael@0: ENERGY_IN_BOUNDS(energy); michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: michael@0: return Q_RATIO(-energy); michael@0: } michael@0: michael@0: void vp9_vaq_init() { michael@0: int i; michael@0: double base_ratio; michael@0: michael@0: assert(ENERGY_SPAN <= MAX_SEGMENTS); michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: michael@0: base_ratio = 1.8; michael@0: michael@0: for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { michael@0: Q_RATIO(i) = pow(base_ratio, i/3.0); michael@0: } michael@0: } michael@0: michael@0: void vp9_vaq_frame_setup(VP9_COMP *cpi) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: struct segmentation *seg = &cm->seg; michael@0: int base_q = vp9_convert_qindex_to_q(cm->base_qindex); michael@0: int base_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + michael@0: cm->y_dc_delta_q); michael@0: int i; michael@0: michael@0: vp9_enable_segmentation((VP9_PTR)cpi); michael@0: vp9_clearall_segfeatures(seg); michael@0: michael@0: seg->abs_delta = SEGMENT_DELTADATA; michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: michael@0: for (i = ENERGY_MIN; i <= ENERGY_MAX; i++) { michael@0: int qindex_delta, segment_rdmult; michael@0: michael@0: if (Q_RATIO(i) == 1) { michael@0: // No need to enable SEG_LVL_ALT_Q for this segment michael@0: RDMULT_RATIO(i) = 1; michael@0: continue; michael@0: } michael@0: michael@0: qindex_delta = vp9_compute_qdelta(cpi, base_q, base_q * Q_RATIO(i)); michael@0: vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta); michael@0: vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q); michael@0: michael@0: segment_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + qindex_delta + michael@0: cm->y_dc_delta_q); michael@0: michael@0: RDMULT_RATIO(i) = (double) segment_rdmult / base_rdmult; michael@0: } michael@0: } michael@0: michael@0: michael@0: static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x, michael@0: BLOCK_SIZE bs) { michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: unsigned int var, sse; michael@0: int right_overflow = (xd->mb_to_right_edge < 0) ? michael@0: ((-xd->mb_to_right_edge) >> 3) : 0; michael@0: int bottom_overflow = (xd->mb_to_bottom_edge < 0) ? michael@0: ((-xd->mb_to_bottom_edge) >> 3) : 0; michael@0: michael@0: if (right_overflow || bottom_overflow) { michael@0: const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow; michael@0: const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow; michael@0: int avg; michael@0: variance(x->plane[0].src.buf, x->plane[0].src.stride, michael@0: vp9_64_zeros, 0, bw, bh, &sse, &avg); michael@0: var = sse - (((int64_t)avg * avg) / (bw * bh)); michael@0: return (256 * var) / (bw * bh); michael@0: } else { michael@0: var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, michael@0: x->plane[0].src.stride, michael@0: vp9_64_zeros, 0, &sse); michael@0: return (256 * var) >> num_pels_log2_lookup[bs]; michael@0: } michael@0: } michael@0: michael@0: int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) { michael@0: double energy; michael@0: unsigned int var = block_variance(cpi, x, bs); michael@0: michael@0: vp9_clear_system_state(); // __asm emms; michael@0: michael@0: // if (var <= 1000) michael@0: // return 0; michael@0: michael@0: energy = 0.9*(logf(var + 1) - 10.0); michael@0: return clamp(round(energy), ENERGY_MIN, ENERGY_MAX); michael@0: }