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