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 
aom_minmax_8x8_c(const uint8_t * s,int p,const uint8_t * d,int dp,int * min,int * max)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 
aom_avg_4x4_c(const uint8_t * s,int p)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 
aom_avg_8x8_c(const uint8_t * s,int p)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 
51 #if CONFIG_AV1_HIGHBITDEPTH
aom_highbd_avg_8x8_c(const uint8_t * s8,int p)52 unsigned int aom_highbd_avg_8x8_c(const uint8_t *s8, int p) {
53   int i, j;
54   int sum = 0;
55   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
56   for (i = 0; i < 8; ++i, s += p)
57     for (j = 0; j < 8; sum += s[j], ++j) {
58     }
59 
60   return (sum + 32) >> 6;
61 }
62 
aom_highbd_avg_4x4_c(const uint8_t * s8,int p)63 unsigned int aom_highbd_avg_4x4_c(const uint8_t *s8, int p) {
64   int i, j;
65   int sum = 0;
66   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
67   for (i = 0; i < 4; ++i, s += p)
68     for (j = 0; j < 4; sum += s[j], ++j) {
69     }
70 
71   return (sum + 8) >> 4;
72 }
73 
aom_highbd_minmax_8x8_c(const uint8_t * s8,int p,const uint8_t * d8,int dp,int * min,int * max)74 void aom_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
75                              int dp, int *min, int *max) {
76   int i, j;
77   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
78   const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
79   *min = 255;
80   *max = 0;
81   for (i = 0; i < 8; ++i, s += p, d += dp) {
82     for (j = 0; j < 8; ++j) {
83       int diff = abs(s[j] - d[j]);
84       *min = diff < *min ? diff : *min;
85       *max = diff > *max ? diff : *max;
86     }
87   }
88 }
89 #endif  // CONFIG_AV1_HIGHBITDEPTH
90 
hadamard_col4(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)91 static void hadamard_col4(const int16_t *src_diff, ptrdiff_t src_stride,
92                           int16_t *coeff) {
93   int16_t b0 = (src_diff[0 * src_stride] + src_diff[1 * src_stride]) >> 1;
94   int16_t b1 = (src_diff[0 * src_stride] - src_diff[1 * src_stride]) >> 1;
95   int16_t b2 = (src_diff[2 * src_stride] + src_diff[3 * src_stride]) >> 1;
96   int16_t b3 = (src_diff[2 * src_stride] - src_diff[3 * src_stride]) >> 1;
97 
98   coeff[0] = b0 + b2;
99   coeff[1] = b1 + b3;
100   coeff[2] = b0 - b2;
101   coeff[3] = b1 - b3;
102 }
103 
aom_hadamard_4x4_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)104 void aom_hadamard_4x4_c(const int16_t *src_diff, ptrdiff_t src_stride,
105                         tran_low_t *coeff) {
106   int idx;
107   int16_t buffer[16];
108   int16_t buffer2[16];
109   int16_t *tmp_buf = &buffer[0];
110   for (idx = 0; idx < 4; ++idx) {
111     hadamard_col4(src_diff, src_stride, tmp_buf);  // src_diff: 9 bit
112                                                    // dynamic range [-255, 255]
113     tmp_buf += 4;
114     ++src_diff;
115   }
116 
117   tmp_buf = &buffer[0];
118   for (idx = 0; idx < 4; ++idx) {
119     hadamard_col4(tmp_buf, 4, buffer2 + 4 * idx);  // tmp_buf: 12 bit
120     // dynamic range [-2040, 2040]
121     // buffer2: 15 bit
122     // dynamic range [-16320, 16320]
123     ++tmp_buf;
124   }
125 
126   for (idx = 0; idx < 16; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
127 }
128 
129 // src_diff: first pass, 9 bit, dynamic range [-255, 255]
130 //           second pass, 12 bit, dynamic range [-2040, 2040]
hadamard_col8(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)131 static void hadamard_col8(const int16_t *src_diff, ptrdiff_t src_stride,
132                           int16_t *coeff) {
133   int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
134   int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
135   int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
136   int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
137   int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
138   int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
139   int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
140   int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
141 
142   int16_t c0 = b0 + b2;
143   int16_t c1 = b1 + b3;
144   int16_t c2 = b0 - b2;
145   int16_t c3 = b1 - b3;
146   int16_t c4 = b4 + b6;
147   int16_t c5 = b5 + b7;
148   int16_t c6 = b4 - b6;
149   int16_t c7 = b5 - b7;
150 
151   coeff[0] = c0 + c4;
152   coeff[7] = c1 + c5;
153   coeff[3] = c2 + c6;
154   coeff[4] = c3 + c7;
155   coeff[2] = c0 - c4;
156   coeff[6] = c1 - c5;
157   coeff[1] = c2 - c6;
158   coeff[5] = c3 - c7;
159 }
160 
161 // The order of the output coeff of the hadamard is not important. For
162 // optimization purposes the final transpose may be skipped.
aom_hadamard_8x8_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)163 void aom_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
164                         tran_low_t *coeff) {
165   int idx;
166   int16_t buffer[64];
167   int16_t buffer2[64];
168   int16_t *tmp_buf = &buffer[0];
169   for (idx = 0; idx < 8; ++idx) {
170     hadamard_col8(src_diff, src_stride, tmp_buf);  // src_diff: 9 bit
171                                                    // dynamic range [-255, 255]
172     tmp_buf += 8;
173     ++src_diff;
174   }
175 
176   tmp_buf = &buffer[0];
177   for (idx = 0; idx < 8; ++idx) {
178     hadamard_col8(tmp_buf, 8, buffer2 + 8 * idx);  // tmp_buf: 12 bit
179     // dynamic range [-2040, 2040]
180     // buffer2: 15 bit
181     // dynamic range [-16320, 16320]
182     ++tmp_buf;
183   }
184 
185   for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
186 }
187 
aom_hadamard_lp_8x8_c(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)188 void aom_hadamard_lp_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
189                            int16_t *coeff) {
190   int16_t buffer[64];
191   int16_t buffer2[64];
192   int16_t *tmp_buf = &buffer[0];
193   for (int idx = 0; idx < 8; ++idx) {
194     hadamard_col8(src_diff, src_stride, tmp_buf);  // src_diff: 9 bit
195                                                    // dynamic range [-255, 255]
196     tmp_buf += 8;
197     ++src_diff;
198   }
199 
200   tmp_buf = &buffer[0];
201   for (int idx = 0; idx < 8; ++idx) {
202     hadamard_col8(tmp_buf, 8, buffer2 + 8 * idx);  // tmp_buf: 12 bit
203     // dynamic range [-2040, 2040]
204     // buffer2: 15 bit
205     // dynamic range [-16320, 16320]
206     ++tmp_buf;
207   }
208 
209   for (int idx = 0; idx < 64; ++idx) coeff[idx] = buffer2[idx];
210 }
211 
212 // In place 16x16 2D Hadamard transform
aom_hadamard_16x16_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)213 void aom_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
214                           tran_low_t *coeff) {
215   int idx;
216   for (idx = 0; idx < 4; ++idx) {
217     // src_diff: 9 bit, dynamic range [-255, 255]
218     const int16_t *src_ptr =
219         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
220     aom_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
221   }
222 
223   // coeff: 15 bit, dynamic range [-16320, 16320]
224   for (idx = 0; idx < 64; ++idx) {
225     tran_low_t a0 = coeff[0];
226     tran_low_t a1 = coeff[64];
227     tran_low_t a2 = coeff[128];
228     tran_low_t a3 = coeff[192];
229 
230     tran_low_t b0 = (a0 + a1) >> 1;  // (a0 + a1): 16 bit, [-32640, 32640]
231     tran_low_t b1 = (a0 - a1) >> 1;  // b0-b3: 15 bit, dynamic range
232     tran_low_t b2 = (a2 + a3) >> 1;  // [-16320, 16320]
233     tran_low_t b3 = (a2 - a3) >> 1;
234 
235     coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
236     coeff[64] = b1 + b3;
237     coeff[128] = b0 - b2;
238     coeff[192] = b1 - b3;
239 
240     ++coeff;
241   }
242 }
243 
aom_hadamard_lp_16x16_c(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)244 void aom_hadamard_lp_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
245                              int16_t *coeff) {
246   for (int idx = 0; idx < 4; ++idx) {
247     // src_diff: 9 bit, dynamic range [-255, 255]
248     const int16_t *src_ptr =
249         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
250     aom_hadamard_lp_8x8_c(src_ptr, src_stride, coeff + idx * 64);
251   }
252 
253   for (int idx = 0; idx < 64; ++idx) {
254     int16_t a0 = coeff[0];
255     int16_t a1 = coeff[64];
256     int16_t a2 = coeff[128];
257     int16_t a3 = coeff[192];
258 
259     int16_t b0 = (a0 + a1) >> 1;  // (a0 + a1): 16 bit, [-32640, 32640]
260     int16_t b1 = (a0 - a1) >> 1;  // b0-b3: 15 bit, dynamic range
261     int16_t b2 = (a2 + a3) >> 1;  // [-16320, 16320]
262     int16_t b3 = (a2 - a3) >> 1;
263 
264     coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
265     coeff[64] = b1 + b3;
266     coeff[128] = b0 - b2;
267     coeff[192] = b1 - b3;
268 
269     ++coeff;
270   }
271 }
272 
aom_hadamard_32x32_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)273 void aom_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
274                           tran_low_t *coeff) {
275   int idx;
276   for (idx = 0; idx < 4; ++idx) {
277     // src_diff: 9 bit, dynamic range [-255, 255]
278     const int16_t *src_ptr =
279         src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
280     aom_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
281   }
282 
283   // coeff: 15 bit, dynamic range [-16320, 16320]
284   for (idx = 0; idx < 256; ++idx) {
285     tran_low_t a0 = coeff[0];
286     tran_low_t a1 = coeff[256];
287     tran_low_t a2 = coeff[512];
288     tran_low_t a3 = coeff[768];
289 
290     tran_low_t b0 = (a0 + a1) >> 2;  // (a0 + a1): 16 bit, [-32640, 32640]
291     tran_low_t b1 = (a0 - a1) >> 2;  // b0-b3: 15 bit, dynamic range
292     tran_low_t b2 = (a2 + a3) >> 2;  // [-16320, 16320]
293     tran_low_t b3 = (a2 - a3) >> 2;
294 
295     coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
296     coeff[256] = b1 + b3;
297     coeff[512] = b0 - b2;
298     coeff[768] = b1 - b3;
299 
300     ++coeff;
301   }
302 }
303 
304 #if CONFIG_AV1_HIGHBITDEPTH
hadamard_highbd_col8_first_pass(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)305 static void hadamard_highbd_col8_first_pass(const int16_t *src_diff,
306                                             ptrdiff_t src_stride,
307                                             int16_t *coeff) {
308   int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
309   int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
310   int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
311   int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
312   int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
313   int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
314   int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
315   int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
316 
317   int16_t c0 = b0 + b2;
318   int16_t c1 = b1 + b3;
319   int16_t c2 = b0 - b2;
320   int16_t c3 = b1 - b3;
321   int16_t c4 = b4 + b6;
322   int16_t c5 = b5 + b7;
323   int16_t c6 = b4 - b6;
324   int16_t c7 = b5 - b7;
325 
326   coeff[0] = c0 + c4;
327   coeff[7] = c1 + c5;
328   coeff[3] = c2 + c6;
329   coeff[4] = c3 + c7;
330   coeff[2] = c0 - c4;
331   coeff[6] = c1 - c5;
332   coeff[1] = c2 - c6;
333   coeff[5] = c3 - c7;
334 }
335 
336 // src_diff: 16 bit, dynamic range [-32760, 32760]
337 // coeff: 19 bit
hadamard_highbd_col8_second_pass(const int16_t * src_diff,ptrdiff_t src_stride,int32_t * coeff)338 static void hadamard_highbd_col8_second_pass(const int16_t *src_diff,
339                                              ptrdiff_t src_stride,
340                                              int32_t *coeff) {
341   int32_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
342   int32_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
343   int32_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
344   int32_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
345   int32_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
346   int32_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
347   int32_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
348   int32_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
349 
350   int32_t c0 = b0 + b2;
351   int32_t c1 = b1 + b3;
352   int32_t c2 = b0 - b2;
353   int32_t c3 = b1 - b3;
354   int32_t c4 = b4 + b6;
355   int32_t c5 = b5 + b7;
356   int32_t c6 = b4 - b6;
357   int32_t c7 = b5 - b7;
358 
359   coeff[0] = c0 + c4;
360   coeff[7] = c1 + c5;
361   coeff[3] = c2 + c6;
362   coeff[4] = c3 + c7;
363   coeff[2] = c0 - c4;
364   coeff[6] = c1 - c5;
365   coeff[1] = c2 - c6;
366   coeff[5] = c3 - c7;
367 }
368 
369 // The order of the output coeff of the hadamard is not important. For
370 // optimization purposes the final transpose may be skipped.
aom_highbd_hadamard_8x8_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)371 void aom_highbd_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
372                                tran_low_t *coeff) {
373   int idx;
374   int16_t buffer[64];
375   int32_t buffer2[64];
376   int16_t *tmp_buf = &buffer[0];
377   for (idx = 0; idx < 8; ++idx) {
378     // src_diff: 13 bit
379     // buffer: 16 bit, dynamic range [-32760, 32760]
380     hadamard_highbd_col8_first_pass(src_diff, src_stride, tmp_buf);
381     tmp_buf += 8;
382     ++src_diff;
383   }
384 
385   tmp_buf = &buffer[0];
386   for (idx = 0; idx < 8; ++idx) {
387     // buffer: 16 bit
388     // buffer2: 19 bit, dynamic range [-262080, 262080]
389     hadamard_highbd_col8_second_pass(tmp_buf, 8, buffer2 + 8 * idx);
390     ++tmp_buf;
391   }
392 
393   for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
394 }
395 
396 // In place 16x16 2D Hadamard transform
aom_highbd_hadamard_16x16_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)397 void aom_highbd_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
398                                  tran_low_t *coeff) {
399   int idx;
400   for (idx = 0; idx < 4; ++idx) {
401     // src_diff: 13 bit, dynamic range [-4095, 4095]
402     const int16_t *src_ptr =
403         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
404     aom_highbd_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
405   }
406 
407   // coeff: 19 bit, dynamic range [-262080, 262080]
408   for (idx = 0; idx < 64; ++idx) {
409     tran_low_t a0 = coeff[0];
410     tran_low_t a1 = coeff[64];
411     tran_low_t a2 = coeff[128];
412     tran_low_t a3 = coeff[192];
413 
414     tran_low_t b0 = (a0 + a1) >> 1;
415     tran_low_t b1 = (a0 - a1) >> 1;
416     tran_low_t b2 = (a2 + a3) >> 1;
417     tran_low_t b3 = (a2 - a3) >> 1;
418 
419     // new coeff dynamic range: 20 bit
420     coeff[0] = b0 + b2;
421     coeff[64] = b1 + b3;
422     coeff[128] = b0 - b2;
423     coeff[192] = b1 - b3;
424 
425     ++coeff;
426   }
427 }
428 
aom_highbd_hadamard_32x32_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)429 void aom_highbd_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
430                                  tran_low_t *coeff) {
431   int idx;
432   for (idx = 0; idx < 4; ++idx) {
433     // src_diff: 13 bit, dynamic range [-4095, 4095]
434     const int16_t *src_ptr =
435         src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
436     aom_highbd_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
437   }
438 
439   // coeff: 20 bit
440   for (idx = 0; idx < 256; ++idx) {
441     tran_low_t a0 = coeff[0];
442     tran_low_t a1 = coeff[256];
443     tran_low_t a2 = coeff[512];
444     tran_low_t a3 = coeff[768];
445 
446     tran_low_t b0 = (a0 + a1) >> 2;
447     tran_low_t b1 = (a0 - a1) >> 2;
448     tran_low_t b2 = (a2 + a3) >> 2;
449     tran_low_t b3 = (a2 - a3) >> 2;
450 
451     // new coeff dynamic range: 20 bit
452     coeff[0] = b0 + b2;
453     coeff[256] = b1 + b3;
454     coeff[512] = b0 - b2;
455     coeff[768] = b1 - b3;
456 
457     ++coeff;
458   }
459 }
460 #endif  // CONFIG_AV1_HIGHBITDEPTH
461 
462 // coeff: 16 bits, dynamic range [-32640, 32640].
463 // length: value range {16, 64, 256, 1024}.
aom_satd_c(const tran_low_t * coeff,int length)464 int aom_satd_c(const tran_low_t *coeff, int length) {
465   int i;
466   int satd = 0;
467   for (i = 0; i < length; ++i) satd += abs(coeff[i]);
468 
469   // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
470   return satd;
471 }
472 
aom_satd_lp_c(const int16_t * coeff,int length)473 int aom_satd_lp_c(const int16_t *coeff, int length) {
474   int satd = 0;
475   for (int i = 0; i < length; ++i) satd += abs(coeff[i]);
476 
477   // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
478   return satd;
479 }
480 
481 // Integer projection onto row vectors.
482 // height: value range {16, 32, 64, 128}.
aom_int_pro_row_c(int16_t hbuf[16],const uint8_t * ref,const int ref_stride,const int height)483 void aom_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref,
484                        const int ref_stride, const int height) {
485   int idx;
486   const int norm_factor = height >> 1;
487   for (idx = 0; idx < 16; ++idx) {
488     int i;
489     hbuf[idx] = 0;
490     // hbuf[idx]: 14 bit, dynamic range [0, 32640].
491     for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
492     // hbuf[idx]: 9 bit, dynamic range [0, 1020].
493     hbuf[idx] /= norm_factor;
494     ++ref;
495   }
496 }
497 
498 // width: value range {16, 32, 64, 128}.
aom_int_pro_col_c(const uint8_t * ref,const int width)499 int16_t aom_int_pro_col_c(const uint8_t *ref, const int width) {
500   int idx;
501   int16_t sum = 0;
502   // sum: 14 bit, dynamic range [0, 32640]
503   for (idx = 0; idx < width; ++idx) sum += ref[idx];
504   return sum;
505 }
506 
507 // ref: [0 - 510]
508 // src: [0 - 510]
509 // bwl: {2, 3, 4, 5}
aom_vector_var_c(const int16_t * ref,const int16_t * src,const int bwl)510 int aom_vector_var_c(const int16_t *ref, const int16_t *src, const int bwl) {
511   int i;
512   int width = 4 << bwl;
513   int sse = 0, mean = 0, var;
514 
515   for (i = 0; i < width; ++i) {
516     int diff = ref[i] - src[i];  // diff: dynamic range [-510, 510], 10 bits.
517     mean += diff;                // mean: dynamic range 16 bits.
518     sse += diff * diff;          // sse:  dynamic range 26 bits.
519   }
520 
521   // (mean * mean): dynamic range 31 bits.
522   var = sse - ((mean * mean) >> (bwl + 2));
523   return var;
524 }
525