blob: f732224fd9fc28246a36ab14a8bc7ca8b794f23c [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11#include <stdlib.h>
12
Yaowu Xuf883b422016-08-30 14:01:10 -070013#include "./aom_dsp_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070014#include "aom_ports/mem.h"
15
Yaowu Xuc27fc142016-08-22 16:08:15 -070016// src_diff: first pass, 9 bit, dynamic range [-255, 255]
17// second pass, 12 bit, dynamic range [-2040, 2040]
18static void hadamard_col8(const int16_t *src_diff, int src_stride,
19 int16_t *coeff) {
20 int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
21 int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
22 int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
23 int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
24 int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
25 int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
26 int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
27 int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
28
29 int16_t c0 = b0 + b2;
30 int16_t c1 = b1 + b3;
31 int16_t c2 = b0 - b2;
32 int16_t c3 = b1 - b3;
33 int16_t c4 = b4 + b6;
34 int16_t c5 = b5 + b7;
35 int16_t c6 = b4 - b6;
36 int16_t c7 = b5 - b7;
37
38 coeff[0] = c0 + c4;
39 coeff[7] = c1 + c5;
40 coeff[3] = c2 + c6;
41 coeff[4] = c3 + c7;
42 coeff[2] = c0 - c4;
43 coeff[6] = c1 - c5;
44 coeff[1] = c2 - c6;
45 coeff[5] = c3 - c7;
46}
47
48// The order of the output coeff of the hadamard is not important. For
49// optimization purposes the final transpose may be skipped.
Yaowu Xuf883b422016-08-30 14:01:10 -070050void aom_hadamard_8x8_c(const int16_t *src_diff, int src_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -070051 int16_t *coeff) {
52 int idx;
53 int16_t buffer[64];
54 int16_t *tmp_buf = &buffer[0];
55 for (idx = 0; idx < 8; ++idx) {
56 hadamard_col8(src_diff, src_stride, tmp_buf); // src_diff: 9 bit
57 // dynamic range [-255, 255]
58 tmp_buf += 8;
59 ++src_diff;
60 }
61
62 tmp_buf = &buffer[0];
63 for (idx = 0; idx < 8; ++idx) {
64 hadamard_col8(tmp_buf, 8, coeff); // tmp_buf: 12 bit
65 // dynamic range [-2040, 2040]
66 coeff += 8; // coeff: 15 bit
67 // dynamic range [-16320, 16320]
68 ++tmp_buf;
69 }
70}
71
72// In place 16x16 2D Hadamard transform
Yaowu Xuf883b422016-08-30 14:01:10 -070073void aom_hadamard_16x16_c(const int16_t *src_diff, int src_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -070074 int16_t *coeff) {
75 int idx;
76 for (idx = 0; idx < 4; ++idx) {
77 // src_diff: 9 bit, dynamic range [-255, 255]
78 const int16_t *src_ptr =
79 src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
Yaowu Xuf883b422016-08-30 14:01:10 -070080 aom_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
Yaowu Xuc27fc142016-08-22 16:08:15 -070081 }
82
83 // coeff: 15 bit, dynamic range [-16320, 16320]
84 for (idx = 0; idx < 64; ++idx) {
85 int16_t a0 = coeff[0];
86 int16_t a1 = coeff[64];
87 int16_t a2 = coeff[128];
88 int16_t a3 = coeff[192];
89
90 int16_t b0 = (a0 + a1) >> 1; // (a0 + a1): 16 bit, [-32640, 32640]
91 int16_t b1 = (a0 - a1) >> 1; // b0-b3: 15 bit, dynamic range
92 int16_t b2 = (a2 + a3) >> 1; // [-16320, 16320]
93 int16_t b3 = (a2 - a3) >> 1;
94
95 coeff[0] = b0 + b2; // 16 bit, [-32640, 32640]
96 coeff[64] = b1 + b3;
97 coeff[128] = b0 - b2;
98 coeff[192] = b1 - b3;
99
100 ++coeff;
101 }
102}
103
104// coeff: 16 bits, dynamic range [-32640, 32640].
105// length: value range {16, 64, 256, 1024}.
Yaowu Xuf883b422016-08-30 14:01:10 -0700106int aom_satd_c(const int16_t *coeff, int length) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700107 int i;
108 int satd = 0;
109 for (i = 0; i < length; ++i) satd += abs(coeff[i]);
110
111 // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
112 return satd;
113}
114
115// Integer projection onto row vectors.
116// height: value range {16, 32, 64}.
Yaowu Xu4ff59b52017-04-24 12:41:56 -0700117void aom_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref, int ref_stride,
118 int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 int idx;
120 const int norm_factor = height >> 1;
121 for (idx = 0; idx < 16; ++idx) {
122 int i;
123 hbuf[idx] = 0;
124 // hbuf[idx]: 14 bit, dynamic range [0, 16320].
125 for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
126 // hbuf[idx]: 9 bit, dynamic range [0, 510].
127 hbuf[idx] /= norm_factor;
128 ++ref;
129 }
130}
131
132// width: value range {16, 32, 64}.
Yaowu Xu4ff59b52017-04-24 12:41:56 -0700133int16_t aom_int_pro_col_c(const uint8_t *ref, int width) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 int idx;
135 int16_t sum = 0;
136 // sum: 14 bit, dynamic range [0, 16320]
137 for (idx = 0; idx < width; ++idx) sum += ref[idx];
138 return sum;
139}
140
141// ref: [0 - 510]
142// src: [0 - 510]
143// bwl: {2, 3, 4}
Yaowu Xu4ff59b52017-04-24 12:41:56 -0700144int aom_vector_var_c(const int16_t *ref, const int16_t *src, int bwl) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 int i;
146 int width = 4 << bwl;
147 int sse = 0, mean = 0, var;
148
149 for (i = 0; i < width; ++i) {
150 int diff = ref[i] - src[i]; // diff: dynamic range [-510, 510], 10 bits.
151 mean += diff; // mean: dynamic range 16 bits.
152 sse += diff * diff; // sse: dynamic range 26 bits.
153 }
154
155 // (mean * mean): dynamic range 31 bits.
156 var = sse - ((mean * mean) >> (bwl + 2));
157 return var;
158}
159
Yaowu Xuf883b422016-08-30 14:01:10 -0700160void aom_minmax_8x8_c(const uint8_t *src, int src_stride, const uint8_t *ref,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161 int ref_stride, int *min, int *max) {
162 int i, j;
163 *min = 255;
164 *max = 0;
165 for (i = 0; i < 8; ++i, src += src_stride, ref += ref_stride) {
166 for (j = 0; j < 8; ++j) {
167 int diff = abs(src[j] - ref[j]);
168 *min = diff < *min ? diff : *min;
169 *max = diff > *max ? diff : *max;
170 }
171 }
172}
173
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200174#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700175void aom_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 int dp, int *min, int *max) {
177 int i, j;
178 const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
179 const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
180 *min = 255;
181 *max = 0;
182 for (i = 0; i < 8; ++i, s += p, d += dp) {
183 for (j = 0; j < 8; ++j) {
184 int diff = abs(s[j] - d[j]);
185 *min = diff < *min ? diff : *min;
186 *max = diff > *max ? diff : *max;
187 }
188 }
189}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200190#endif // CONFIG_HIGHBITDEPTH