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 "vpx_ports/mem.h"
14 #include "vpx_ports/system_state.h"
15 
16 #include "vp9/encoder/vp9_aq_variance.h"
17 
18 #include "vp9/common/vp9_seg_common.h"
19 
20 #include "vp9/encoder/vp9_ratectrl.h"
21 #include "vp9/encoder/vp9_rd.h"
22 #include "vp9/encoder/vp9_encodeframe.h"
23 #include "vp9/encoder/vp9_segmentation.h"
24 
25 #define ENERGY_MIN (-4)
26 #define ENERGY_MAX (1)
27 #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1)
28 #define ENERGY_IN_BOUNDS(energy) \
29   assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
30 
31 static const double rate_ratio[MAX_SEGMENTS] = { 2.5,  2.0, 1.5, 1.0,
32                                                  0.75, 1.0, 1.0, 1.0 };
33 static const int segment_id[ENERGY_SPAN] = { 0, 1, 1, 2, 3, 4 };
34 
35 #define SEGMENT_ID(i) segment_id[(i)-ENERGY_MIN]
36 
37 DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = { 0 };
38 #if CONFIG_VP9_HIGHBITDEPTH
39 DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = { 0 };
40 #endif
41 
vp9_vaq_segment_id(int energy)42 unsigned int vp9_vaq_segment_id(int energy) {
43   ENERGY_IN_BOUNDS(energy);
44   return SEGMENT_ID(energy);
45 }
46 
vp9_vaq_frame_setup(VP9_COMP * cpi)47 void vp9_vaq_frame_setup(VP9_COMP *cpi) {
48   VP9_COMMON *cm = &cpi->common;
49   struct segmentation *seg = &cm->seg;
50   int i;
51 
52   if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
53       cpi->refresh_alt_ref_frame || cpi->force_update_segmentation ||
54       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
55     vp9_enable_segmentation(seg);
56     vp9_clearall_segfeatures(seg);
57 
58     seg->abs_delta = SEGMENT_DELTADATA;
59 
60     vpx_clear_system_state();
61 
62     for (i = 0; i < MAX_SEGMENTS; ++i) {
63       int qindex_delta =
64           vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
65                                      rate_ratio[i], cm->bit_depth);
66 
67       // We don't allow qindex 0 in a segment if the base value is not 0.
68       // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
69       // Q delta is sometimes applied without going back around the rd loop.
70       // This could lead to an illegal combination of partition size and q.
71       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
72         qindex_delta = -cm->base_qindex + 1;
73       }
74 
75       // No need to enable SEG_LVL_ALT_Q for this segment.
76       if (rate_ratio[i] == 1.0) {
77         continue;
78       }
79 
80       vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
81       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
82     }
83   }
84 }
85 
86 /* TODO(agrange, paulwilkins): The block_variance calls the unoptimized versions
87  * of variance() and highbd_8_variance(). It should not.
88  */
aq_variance(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int w,int h,unsigned int * sse,int * sum)89 static void aq_variance(const uint8_t *a, int a_stride, const uint8_t *b,
90                         int b_stride, int w, int h, unsigned int *sse,
91                         int *sum) {
92   int i, j;
93 
94   *sum = 0;
95   *sse = 0;
96 
97   for (i = 0; i < h; i++) {
98     for (j = 0; j < w; j++) {
99       const int diff = a[j] - b[j];
100       *sum += diff;
101       *sse += diff * diff;
102     }
103 
104     a += a_stride;
105     b += b_stride;
106   }
107 }
108 
109 #if CONFIG_VP9_HIGHBITDEPTH
aq_highbd_variance64(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h,uint64_t * sse,int64_t * sum)110 static void aq_highbd_variance64(const uint8_t *a8, int a_stride,
111                                  const uint8_t *b8, int b_stride, int w, int h,
112                                  uint64_t *sse, int64_t *sum) {
113   int i, j;
114 
115   uint16_t *a = CONVERT_TO_SHORTPTR(a8);
116   uint16_t *b = CONVERT_TO_SHORTPTR(b8);
117   *sum = 0;
118   *sse = 0;
119 
120   for (i = 0; i < h; i++) {
121     for (j = 0; j < w; j++) {
122       const int diff = a[j] - b[j];
123       *sum += diff;
124       *sse += diff * diff;
125     }
126     a += a_stride;
127     b += b_stride;
128   }
129 }
130 
131 #endif  // CONFIG_VP9_HIGHBITDEPTH
132 
block_variance(VP9_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bs)133 static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
134                                    BLOCK_SIZE bs) {
135   MACROBLOCKD *xd = &x->e_mbd;
136   unsigned int var, sse;
137   int right_overflow =
138       (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
139   int bottom_overflow =
140       (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
141 
142   if (right_overflow || bottom_overflow) {
143     const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
144     const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
145     int avg;
146 #if CONFIG_VP9_HIGHBITDEPTH
147     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
148       uint64_t sse64 = 0;
149       int64_t sum64 = 0;
150       aq_highbd_variance64(x->plane[0].src.buf, x->plane[0].src.stride,
151                            CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, bw, bh,
152                            &sse64, &sum64);
153       sse = (unsigned int)(sse64 >> (2 * (xd->bd - 8)));
154       avg = (int)(sum64 >> (xd->bd - 8));
155     } else {
156       aq_variance(x->plane[0].src.buf, x->plane[0].src.stride, vp9_64_zeros, 0,
157                   bw, bh, &sse, &avg);
158     }
159 #else
160     aq_variance(x->plane[0].src.buf, x->plane[0].src.stride, vp9_64_zeros, 0,
161                 bw, bh, &sse, &avg);
162 #endif  // CONFIG_VP9_HIGHBITDEPTH
163     var = sse - (unsigned int)(((int64_t)avg * avg) / (bw * bh));
164     return (unsigned int)(((uint64_t)256 * var) / (bw * bh));
165   } else {
166 #if CONFIG_VP9_HIGHBITDEPTH
167     if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
168       var =
169           cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
170                              CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, &sse);
171     } else {
172       var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
173                                vp9_64_zeros, 0, &sse);
174     }
175 #else
176     var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
177                              vp9_64_zeros, 0, &sse);
178 #endif  // CONFIG_VP9_HIGHBITDEPTH
179     return (unsigned int)(((uint64_t)256 * var) >> num_pels_log2_lookup[bs]);
180   }
181 }
182 
vp9_log_block_var(VP9_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bs)183 double vp9_log_block_var(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
184   unsigned int var = block_variance(cpi, x, bs);
185   vpx_clear_system_state();
186   return log(var + 1.0);
187 }
188 
189 // Get the range of sub block energy values;
vp9_get_sub_block_energy(VP9_COMP * cpi,MACROBLOCK * mb,int mi_row,int mi_col,BLOCK_SIZE bsize,int * min_e,int * max_e)190 void vp9_get_sub_block_energy(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row,
191                               int mi_col, BLOCK_SIZE bsize, int *min_e,
192                               int *max_e) {
193   VP9_COMMON *const cm = &cpi->common;
194   const int bw = num_8x8_blocks_wide_lookup[bsize];
195   const int bh = num_8x8_blocks_high_lookup[bsize];
196   const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
197   const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
198   int x, y;
199 
200   if (xmis < bw || ymis < bh) {
201     vp9_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
202     *min_e = vp9_block_energy(cpi, mb, bsize);
203     *max_e = *min_e;
204   } else {
205     int energy;
206     *min_e = ENERGY_MAX;
207     *max_e = ENERGY_MIN;
208 
209     for (y = 0; y < ymis; ++y) {
210       for (x = 0; x < xmis; ++x) {
211         vp9_setup_src_planes(mb, cpi->Source, mi_row + y, mi_col + x);
212         energy = vp9_block_energy(cpi, mb, BLOCK_8X8);
213         *min_e = VPXMIN(*min_e, energy);
214         *max_e = VPXMAX(*max_e, energy);
215       }
216     }
217   }
218 
219   // Re-instate source pointers back to what they should have been on entry.
220   vp9_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
221 }
222 
223 #define DEFAULT_E_MIDPOINT 10.0
vp9_block_energy(VP9_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bs)224 int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
225   double energy;
226   double energy_midpoint;
227   vpx_clear_system_state();
228   energy_midpoint =
229       (cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_E_MIDPOINT;
230   energy = vp9_log_block_var(cpi, x, bs) - energy_midpoint;
231   return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
232 }
233