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