Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_aq_variance.c
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
28 static double q_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
29 static double rdmult_ratio[MAX_SEGMENTS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
30 static 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
36 DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = {0};
37
38 unsigned int vp9_vaq_segment_id(int energy) {
39   ENERGY_IN_BOUNDS(energy);
40
41   return SEGMENT_ID(energy);
42 }
43
44 double 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
52 double 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
60 void 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
75 void 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, cm->bit_depth);
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                                         cm->bit_depth);
104       vp9_set_segdata(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q, qindex_delta);
105       vp9_enable_segfeature(seg, SEGMENT_ID(i), SEG_LVL_ALT_Q);
106
107       segment_rdmult = vp9_compute_rd_mult(cpi, cm->base_qindex + qindex_delta +
108                                            cm->y_dc_delta_q);
109
110       RDMULT_RATIO(i) = (double) segment_rdmult / base_rdmult;
111     }
112   }
113 }
114
115
116 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
117                                    BLOCK_SIZE bs) {
118   MACROBLOCKD *xd = &x->e_mbd;
119   unsigned int var, sse;
120   int right_overflow = (xd->mb_to_right_edge < 0) ?
121       ((-xd->mb_to_right_edge) >> 3) : 0;
122   int bottom_overflow = (xd->mb_to_bottom_edge < 0) ?
123       ((-xd->mb_to_bottom_edge) >> 3) : 0;
124
125   if (right_overflow || bottom_overflow) {
126     const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
127     const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
128     int avg;
129     variance(x->plane[0].src.buf, x->plane[0].src.stride,
130              vp9_64_zeros, 0, bw, bh, &sse, &avg);
131     var = sse - (((int64_t)avg * avg) / (bw * bh));
132     return (256 * var) / (bw * bh);
133   } else {
134     var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
135                              x->plane[0].src.stride,
136                              vp9_64_zeros, 0, &sse);
137     return (256 * var) >> num_pels_log2_lookup[bs];
138   }
139 }
140
141 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
142   double energy;
143   unsigned int var = block_variance(cpi, x, bs);
144
145   vp9_clear_system_state();
146
147   energy = 0.9 * (log(var + 1.0) - 10.0);
148   return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
149 }