Line data Source code
1 : /*
2 : * Copyright (c) 2014 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 : #include <stdlib.h>
11 :
12 : #include "./vpx_dsp_rtcd.h"
13 : #include "vpx_ports/mem.h"
14 :
15 0 : unsigned int vpx_avg_8x8_c(const uint8_t *s, int p) {
16 : int i, j;
17 0 : int sum = 0;
18 0 : for (i = 0; i < 8; ++i, s += p)
19 0 : for (j = 0; j < 8; sum += s[j], ++j) {
20 : }
21 :
22 0 : return (sum + 32) >> 6;
23 : }
24 :
25 0 : unsigned int vpx_avg_4x4_c(const uint8_t *s, int p) {
26 : int i, j;
27 0 : int sum = 0;
28 0 : for (i = 0; i < 4; ++i, s += p)
29 0 : for (j = 0; j < 4; sum += s[j], ++j) {
30 : }
31 :
32 0 : return (sum + 8) >> 4;
33 : }
34 :
35 : // src_diff: first pass, 9 bit, dynamic range [-255, 255]
36 : // second pass, 12 bit, dynamic range [-2040, 2040]
37 0 : static void hadamard_col8(const int16_t *src_diff, int src_stride,
38 : int16_t *coeff) {
39 0 : int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
40 0 : int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
41 0 : int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
42 0 : int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
43 0 : int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
44 0 : int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
45 0 : int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
46 0 : int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
47 :
48 0 : int16_t c0 = b0 + b2;
49 0 : int16_t c1 = b1 + b3;
50 0 : int16_t c2 = b0 - b2;
51 0 : int16_t c3 = b1 - b3;
52 0 : int16_t c4 = b4 + b6;
53 0 : int16_t c5 = b5 + b7;
54 0 : int16_t c6 = b4 - b6;
55 0 : int16_t c7 = b5 - b7;
56 :
57 0 : coeff[0] = c0 + c4;
58 0 : coeff[7] = c1 + c5;
59 0 : coeff[3] = c2 + c6;
60 0 : coeff[4] = c3 + c7;
61 0 : coeff[2] = c0 - c4;
62 0 : coeff[6] = c1 - c5;
63 0 : coeff[1] = c2 - c6;
64 0 : coeff[5] = c3 - c7;
65 0 : }
66 :
67 : // The order of the output coeff of the hadamard is not important. For
68 : // optimization purposes the final transpose may be skipped.
69 0 : void vpx_hadamard_8x8_c(const int16_t *src_diff, int src_stride,
70 : int16_t *coeff) {
71 : int idx;
72 : int16_t buffer[64];
73 0 : int16_t *tmp_buf = &buffer[0];
74 0 : for (idx = 0; idx < 8; ++idx) {
75 0 : hadamard_col8(src_diff, src_stride, tmp_buf); // src_diff: 9 bit
76 : // dynamic range [-255, 255]
77 0 : tmp_buf += 8;
78 0 : ++src_diff;
79 : }
80 :
81 0 : tmp_buf = &buffer[0];
82 0 : for (idx = 0; idx < 8; ++idx) {
83 0 : hadamard_col8(tmp_buf, 8, coeff); // tmp_buf: 12 bit
84 : // dynamic range [-2040, 2040]
85 0 : coeff += 8; // coeff: 15 bit
86 : // dynamic range [-16320, 16320]
87 0 : ++tmp_buf;
88 : }
89 0 : }
90 :
91 : // In place 16x16 2D Hadamard transform
92 0 : void vpx_hadamard_16x16_c(const int16_t *src_diff, int src_stride,
93 : int16_t *coeff) {
94 : int idx;
95 0 : for (idx = 0; idx < 4; ++idx) {
96 : // src_diff: 9 bit, dynamic range [-255, 255]
97 0 : const int16_t *src_ptr =
98 0 : src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
99 0 : vpx_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
100 : }
101 :
102 : // coeff: 15 bit, dynamic range [-16320, 16320]
103 0 : for (idx = 0; idx < 64; ++idx) {
104 0 : int16_t a0 = coeff[0];
105 0 : int16_t a1 = coeff[64];
106 0 : int16_t a2 = coeff[128];
107 0 : int16_t a3 = coeff[192];
108 :
109 0 : int16_t b0 = (a0 + a1) >> 1; // (a0 + a1): 16 bit, [-32640, 32640]
110 0 : int16_t b1 = (a0 - a1) >> 1; // b0-b3: 15 bit, dynamic range
111 0 : int16_t b2 = (a2 + a3) >> 1; // [-16320, 16320]
112 0 : int16_t b3 = (a2 - a3) >> 1;
113 :
114 0 : coeff[0] = b0 + b2; // 16 bit, [-32640, 32640]
115 0 : coeff[64] = b1 + b3;
116 0 : coeff[128] = b0 - b2;
117 0 : coeff[192] = b1 - b3;
118 :
119 0 : ++coeff;
120 : }
121 0 : }
122 :
123 : // coeff: 16 bits, dynamic range [-32640, 32640].
124 : // length: value range {16, 64, 256, 1024}.
125 0 : int vpx_satd_c(const int16_t *coeff, int length) {
126 : int i;
127 0 : int satd = 0;
128 0 : for (i = 0; i < length; ++i) satd += abs(coeff[i]);
129 :
130 : // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
131 0 : return satd;
132 : }
133 :
134 : // Integer projection onto row vectors.
135 : // height: value range {16, 32, 64}.
136 0 : void vpx_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref,
137 : const int ref_stride, const int height) {
138 : int idx;
139 0 : const int norm_factor = height >> 1;
140 0 : for (idx = 0; idx < 16; ++idx) {
141 : int i;
142 0 : hbuf[idx] = 0;
143 : // hbuf[idx]: 14 bit, dynamic range [0, 16320].
144 0 : for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
145 : // hbuf[idx]: 9 bit, dynamic range [0, 510].
146 0 : hbuf[idx] /= norm_factor;
147 0 : ++ref;
148 : }
149 0 : }
150 :
151 : // width: value range {16, 32, 64}.
152 0 : int16_t vpx_int_pro_col_c(const uint8_t *ref, const int width) {
153 : int idx;
154 0 : int16_t sum = 0;
155 : // sum: 14 bit, dynamic range [0, 16320]
156 0 : for (idx = 0; idx < width; ++idx) sum += ref[idx];
157 0 : return sum;
158 : }
159 :
160 : // ref: [0 - 510]
161 : // src: [0 - 510]
162 : // bwl: {2, 3, 4}
163 0 : int vpx_vector_var_c(const int16_t *ref, const int16_t *src, const int bwl) {
164 : int i;
165 0 : int width = 4 << bwl;
166 0 : int sse = 0, mean = 0, var;
167 :
168 0 : for (i = 0; i < width; ++i) {
169 0 : int diff = ref[i] - src[i]; // diff: dynamic range [-510, 510], 10 bits.
170 0 : mean += diff; // mean: dynamic range 16 bits.
171 0 : sse += diff * diff; // sse: dynamic range 26 bits.
172 : }
173 :
174 : // (mean * mean): dynamic range 31 bits.
175 0 : var = sse - ((mean * mean) >> (bwl + 2));
176 0 : return var;
177 : }
178 :
179 0 : void vpx_minmax_8x8_c(const uint8_t *s, int p, const uint8_t *d, int dp,
180 : int *min, int *max) {
181 : int i, j;
182 0 : *min = 255;
183 0 : *max = 0;
184 0 : for (i = 0; i < 8; ++i, s += p, d += dp) {
185 0 : for (j = 0; j < 8; ++j) {
186 0 : int diff = abs(s[j] - d[j]);
187 0 : *min = diff < *min ? diff : *min;
188 0 : *max = diff > *max ? diff : *max;
189 : }
190 : }
191 0 : }
192 :
193 : #if CONFIG_VP9_HIGHBITDEPTH
194 : unsigned int vpx_highbd_avg_8x8_c(const uint8_t *s8, int p) {
195 : int i, j;
196 : int sum = 0;
197 : const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
198 : for (i = 0; i < 8; ++i, s += p)
199 : for (j = 0; j < 8; sum += s[j], ++j) {
200 : }
201 :
202 : return (sum + 32) >> 6;
203 : }
204 :
205 : unsigned int vpx_highbd_avg_4x4_c(const uint8_t *s8, int p) {
206 : int i, j;
207 : int sum = 0;
208 : const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
209 : for (i = 0; i < 4; ++i, s += p)
210 : for (j = 0; j < 4; sum += s[j], ++j) {
211 : }
212 :
213 : return (sum + 8) >> 4;
214 : }
215 :
216 : void vpx_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
217 : int dp, int *min, int *max) {
218 : int i, j;
219 : const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
220 : const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
221 : *min = 255;
222 : *max = 0;
223 : for (i = 0; i < 8; ++i, s += p, d += dp) {
224 : for (j = 0; j < 8; ++j) {
225 : int diff = abs(s[j] - d[j]);
226 : *min = diff < *min ? diff : *min;
227 : *max = diff > *max ? diff : *max;
228 : }
229 : }
230 : }
231 : #endif // CONFIG_VP9_HIGHBITDEPTH
|