media/libvpx/vp9/encoder/vp9_vaq.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial