xref: /aosp_15_r20/external/libaom/aom_dsp/sum_squares.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
aom_sum_squares_2d_i16_c(const int16_t * src,int src_stride,int width,int height)17*77c1e3ccSAndroid Build Coastguard Worker uint64_t aom_sum_squares_2d_i16_c(const int16_t *src, int src_stride, int width,
18*77c1e3ccSAndroid Build Coastguard Worker                                   int height) {
19*77c1e3ccSAndroid Build Coastguard Worker   int r, c;
20*77c1e3ccSAndroid Build Coastguard Worker   uint64_t ss = 0;
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker   for (r = 0; r < height; r++) {
23*77c1e3ccSAndroid Build Coastguard Worker     for (c = 0; c < width; c++) {
24*77c1e3ccSAndroid Build Coastguard Worker       const int16_t v = src[c];
25*77c1e3ccSAndroid Build Coastguard Worker       ss += v * v;
26*77c1e3ccSAndroid Build Coastguard Worker     }
27*77c1e3ccSAndroid Build Coastguard Worker     src += src_stride;
28*77c1e3ccSAndroid Build Coastguard Worker   }
29*77c1e3ccSAndroid Build Coastguard Worker 
30*77c1e3ccSAndroid Build Coastguard Worker   return ss;
31*77c1e3ccSAndroid Build Coastguard Worker }
32*77c1e3ccSAndroid Build Coastguard Worker 
aom_sum_squares_i16_c(const int16_t * src,uint32_t n)33*77c1e3ccSAndroid Build Coastguard Worker uint64_t aom_sum_squares_i16_c(const int16_t *src, uint32_t n) {
34*77c1e3ccSAndroid Build Coastguard Worker   uint64_t ss = 0;
35*77c1e3ccSAndroid Build Coastguard Worker   do {
36*77c1e3ccSAndroid Build Coastguard Worker     const int16_t v = *src++;
37*77c1e3ccSAndroid Build Coastguard Worker     ss += v * v;
38*77c1e3ccSAndroid Build Coastguard Worker   } while (--n);
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   return ss;
41*77c1e3ccSAndroid Build Coastguard Worker }
42*77c1e3ccSAndroid Build Coastguard Worker 
aom_var_2d_u8_c(uint8_t * src,int src_stride,int width,int height)43*77c1e3ccSAndroid Build Coastguard Worker uint64_t aom_var_2d_u8_c(uint8_t *src, int src_stride, int width, int height) {
44*77c1e3ccSAndroid Build Coastguard Worker   int r, c;
45*77c1e3ccSAndroid Build Coastguard Worker   uint64_t ss = 0, s = 0;
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker   for (r = 0; r < height; r++) {
48*77c1e3ccSAndroid Build Coastguard Worker     for (c = 0; c < width; c++) {
49*77c1e3ccSAndroid Build Coastguard Worker       const uint8_t v = src[c];
50*77c1e3ccSAndroid Build Coastguard Worker       ss += v * v;
51*77c1e3ccSAndroid Build Coastguard Worker       s += v;
52*77c1e3ccSAndroid Build Coastguard Worker     }
53*77c1e3ccSAndroid Build Coastguard Worker     src += src_stride;
54*77c1e3ccSAndroid Build Coastguard Worker   }
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker   return (ss - s * s / (width * height));
57*77c1e3ccSAndroid Build Coastguard Worker }
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
aom_var_2d_u16_c(uint8_t * src,int src_stride,int width,int height)60*77c1e3ccSAndroid Build Coastguard Worker uint64_t aom_var_2d_u16_c(uint8_t *src, int src_stride, int width, int height) {
61*77c1e3ccSAndroid Build Coastguard Worker   uint16_t *srcp = CONVERT_TO_SHORTPTR(src);
62*77c1e3ccSAndroid Build Coastguard Worker   int r, c;
63*77c1e3ccSAndroid Build Coastguard Worker   uint64_t ss = 0, s = 0;
64*77c1e3ccSAndroid Build Coastguard Worker 
65*77c1e3ccSAndroid Build Coastguard Worker   for (r = 0; r < height; r++) {
66*77c1e3ccSAndroid Build Coastguard Worker     for (c = 0; c < width; c++) {
67*77c1e3ccSAndroid Build Coastguard Worker       const uint16_t v = srcp[c];
68*77c1e3ccSAndroid Build Coastguard Worker       ss += v * v;
69*77c1e3ccSAndroid Build Coastguard Worker       s += v;
70*77c1e3ccSAndroid Build Coastguard Worker     }
71*77c1e3ccSAndroid Build Coastguard Worker     srcp += src_stride;
72*77c1e3ccSAndroid Build Coastguard Worker   }
73*77c1e3ccSAndroid Build Coastguard Worker 
74*77c1e3ccSAndroid Build Coastguard Worker   return (ss - s * s / (width * height));
75*77c1e3ccSAndroid Build Coastguard Worker }
76*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_HIGHBITDEPTH
77*77c1e3ccSAndroid Build Coastguard Worker 
aom_sum_sse_2d_i16_c(const int16_t * src,int src_stride,int width,int height,int * sum)78*77c1e3ccSAndroid Build Coastguard Worker uint64_t aom_sum_sse_2d_i16_c(const int16_t *src, int src_stride, int width,
79*77c1e3ccSAndroid Build Coastguard Worker                               int height, int *sum) {
80*77c1e3ccSAndroid Build Coastguard Worker   int r, c;
81*77c1e3ccSAndroid Build Coastguard Worker   int16_t *srcp = (int16_t *)src;
82*77c1e3ccSAndroid Build Coastguard Worker   int64_t ss = 0;
83*77c1e3ccSAndroid Build Coastguard Worker 
84*77c1e3ccSAndroid Build Coastguard Worker   for (r = 0; r < height; r++) {
85*77c1e3ccSAndroid Build Coastguard Worker     for (c = 0; c < width; c++) {
86*77c1e3ccSAndroid Build Coastguard Worker       const int16_t v = srcp[c];
87*77c1e3ccSAndroid Build Coastguard Worker       ss += v * v;
88*77c1e3ccSAndroid Build Coastguard Worker       *sum += v;
89*77c1e3ccSAndroid Build Coastguard Worker     }
90*77c1e3ccSAndroid Build Coastguard Worker     srcp += src_stride;
91*77c1e3ccSAndroid Build Coastguard Worker   }
92*77c1e3ccSAndroid Build Coastguard Worker   return ss;
93*77c1e3ccSAndroid Build Coastguard Worker }
94