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