Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 : #include <stdlib.h>
12 :
13 : #include "./aom_dsp_rtcd.h"
14 : #include "aom_ports/mem.h"
15 :
16 : // src_diff: first pass, 9 bit, dynamic range [-255, 255]
17 : // second pass, 12 bit, dynamic range [-2040, 2040]
18 0 : static void hadamard_col8(const int16_t *src_diff, int src_stride,
19 : int16_t *coeff) {
20 0 : int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
21 0 : int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
22 0 : int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
23 0 : int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
24 0 : int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
25 0 : int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
26 0 : int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
27 0 : int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
28 :
29 0 : int16_t c0 = b0 + b2;
30 0 : int16_t c1 = b1 + b3;
31 0 : int16_t c2 = b0 - b2;
32 0 : int16_t c3 = b1 - b3;
33 0 : int16_t c4 = b4 + b6;
34 0 : int16_t c5 = b5 + b7;
35 0 : int16_t c6 = b4 - b6;
36 0 : int16_t c7 = b5 - b7;
37 :
38 0 : coeff[0] = c0 + c4;
39 0 : coeff[7] = c1 + c5;
40 0 : coeff[3] = c2 + c6;
41 0 : coeff[4] = c3 + c7;
42 0 : coeff[2] = c0 - c4;
43 0 : coeff[6] = c1 - c5;
44 0 : coeff[1] = c2 - c6;
45 0 : coeff[5] = c3 - c7;
46 0 : }
47 :
48 : // The order of the output coeff of the hadamard is not important. For
49 : // optimization purposes the final transpose may be skipped.
50 0 : void aom_hadamard_8x8_c(const int16_t *src_diff, int src_stride,
51 : int16_t *coeff) {
52 : int idx;
53 : int16_t buffer[64];
54 0 : int16_t *tmp_buf = &buffer[0];
55 0 : for (idx = 0; idx < 8; ++idx) {
56 0 : hadamard_col8(src_diff, src_stride, tmp_buf); // src_diff: 9 bit
57 : // dynamic range [-255, 255]
58 0 : tmp_buf += 8;
59 0 : ++src_diff;
60 : }
61 :
62 0 : tmp_buf = &buffer[0];
63 0 : for (idx = 0; idx < 8; ++idx) {
64 0 : hadamard_col8(tmp_buf, 8, coeff); // tmp_buf: 12 bit
65 : // dynamic range [-2040, 2040]
66 0 : coeff += 8; // coeff: 15 bit
67 : // dynamic range [-16320, 16320]
68 0 : ++tmp_buf;
69 : }
70 0 : }
71 :
72 : // In place 16x16 2D Hadamard transform
73 0 : void aom_hadamard_16x16_c(const int16_t *src_diff, int src_stride,
74 : int16_t *coeff) {
75 : int idx;
76 0 : for (idx = 0; idx < 4; ++idx) {
77 : // src_diff: 9 bit, dynamic range [-255, 255]
78 0 : const int16_t *src_ptr =
79 0 : src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
80 0 : aom_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
81 : }
82 :
83 : // coeff: 15 bit, dynamic range [-16320, 16320]
84 0 : for (idx = 0; idx < 64; ++idx) {
85 0 : int16_t a0 = coeff[0];
86 0 : int16_t a1 = coeff[64];
87 0 : int16_t a2 = coeff[128];
88 0 : int16_t a3 = coeff[192];
89 :
90 0 : int16_t b0 = (a0 + a1) >> 1; // (a0 + a1): 16 bit, [-32640, 32640]
91 0 : int16_t b1 = (a0 - a1) >> 1; // b0-b3: 15 bit, dynamic range
92 0 : int16_t b2 = (a2 + a3) >> 1; // [-16320, 16320]
93 0 : int16_t b3 = (a2 - a3) >> 1;
94 :
95 0 : coeff[0] = b0 + b2; // 16 bit, [-32640, 32640]
96 0 : coeff[64] = b1 + b3;
97 0 : coeff[128] = b0 - b2;
98 0 : coeff[192] = b1 - b3;
99 :
100 0 : ++coeff;
101 : }
102 0 : }
103 :
104 : // coeff: 16 bits, dynamic range [-32640, 32640].
105 : // length: value range {16, 64, 256, 1024}.
106 0 : int aom_satd_c(const int16_t *coeff, int length) {
107 : int i;
108 0 : int satd = 0;
109 0 : for (i = 0; i < length; ++i) satd += abs(coeff[i]);
110 :
111 : // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
112 0 : return satd;
113 : }
114 :
115 : // Integer projection onto row vectors.
116 : // height: value range {16, 32, 64}.
117 0 : void aom_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref, int ref_stride,
118 : int height) {
119 : int idx;
120 0 : const int norm_factor = height >> 1;
121 0 : for (idx = 0; idx < 16; ++idx) {
122 : int i;
123 0 : hbuf[idx] = 0;
124 : // hbuf[idx]: 14 bit, dynamic range [0, 16320].
125 0 : for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
126 : // hbuf[idx]: 9 bit, dynamic range [0, 510].
127 0 : hbuf[idx] /= norm_factor;
128 0 : ++ref;
129 : }
130 0 : }
131 :
132 : // width: value range {16, 32, 64}.
133 0 : int16_t aom_int_pro_col_c(const uint8_t *ref, int width) {
134 : int idx;
135 0 : int16_t sum = 0;
136 : // sum: 14 bit, dynamic range [0, 16320]
137 0 : for (idx = 0; idx < width; ++idx) sum += ref[idx];
138 0 : return sum;
139 : }
140 :
141 : // ref: [0 - 510]
142 : // src: [0 - 510]
143 : // bwl: {2, 3, 4}
144 0 : int aom_vector_var_c(const int16_t *ref, const int16_t *src, int bwl) {
145 : int i;
146 0 : int width = 4 << bwl;
147 0 : int sse = 0, mean = 0, var;
148 :
149 0 : for (i = 0; i < width; ++i) {
150 0 : int diff = ref[i] - src[i]; // diff: dynamic range [-510, 510], 10 bits.
151 0 : mean += diff; // mean: dynamic range 16 bits.
152 0 : sse += diff * diff; // sse: dynamic range 26 bits.
153 : }
154 :
155 : // (mean * mean): dynamic range 31 bits.
156 0 : var = sse - ((mean * mean) >> (bwl + 2));
157 0 : return var;
158 : }
159 :
160 0 : void aom_minmax_8x8_c(const uint8_t *src, int src_stride, const uint8_t *ref,
161 : int ref_stride, int *min, int *max) {
162 : int i, j;
163 0 : *min = 255;
164 0 : *max = 0;
165 0 : for (i = 0; i < 8; ++i, src += src_stride, ref += ref_stride) {
166 0 : for (j = 0; j < 8; ++j) {
167 0 : int diff = abs(src[j] - ref[j]);
168 0 : *min = diff < *min ? diff : *min;
169 0 : *max = diff > *max ? diff : *max;
170 : }
171 : }
172 0 : }
173 :
174 : #if CONFIG_HIGHBITDEPTH
175 0 : void aom_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
176 : int dp, int *min, int *max) {
177 : int i, j;
178 0 : const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
179 0 : const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
180 0 : *min = 255;
181 0 : *max = 0;
182 0 : for (i = 0; i < 8; ++i, s += p, d += dp) {
183 0 : for (j = 0; j < 8; ++j) {
184 0 : int diff = abs(s[j] - d[j]);
185 0 : *min = diff < *min ? diff : *min;
186 0 : *max = diff > *max ? diff : *max;
187 : }
188 : }
189 0 : }
190 : #endif // CONFIG_HIGHBITDEPTH
|