xref: /aosp_15_r20/external/libaom/aom_dsp/noise_util.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2017, 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 <math.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/noise_util.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/fft_common.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
22*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_psd_get_default_value(int block_size,float factor)23*77c1e3ccSAndroid Build Coastguard Worker float aom_noise_psd_get_default_value(int block_size, float factor) {
24*77c1e3ccSAndroid Build Coastguard Worker   return (factor * factor / 10000) * block_size * block_size / 8;
25*77c1e3ccSAndroid Build Coastguard Worker }
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker // Internal representation of noise transform. It keeps track of the
28*77c1e3ccSAndroid Build Coastguard Worker // transformed data and a temporary working buffer to use during the
29*77c1e3ccSAndroid Build Coastguard Worker // transform.
30*77c1e3ccSAndroid Build Coastguard Worker struct aom_noise_tx_t {
31*77c1e3ccSAndroid Build Coastguard Worker   float *tx_block;
32*77c1e3ccSAndroid Build Coastguard Worker   float *temp;
33*77c1e3ccSAndroid Build Coastguard Worker   int block_size;
34*77c1e3ccSAndroid Build Coastguard Worker   void (*fft)(const float *, float *, float *);
35*77c1e3ccSAndroid Build Coastguard Worker   void (*ifft)(const float *, float *, float *);
36*77c1e3ccSAndroid Build Coastguard Worker };
37*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_malloc(int block_size)38*77c1e3ccSAndroid Build Coastguard Worker struct aom_noise_tx_t *aom_noise_tx_malloc(int block_size) {
39*77c1e3ccSAndroid Build Coastguard Worker   struct aom_noise_tx_t *noise_tx =
40*77c1e3ccSAndroid Build Coastguard Worker       (struct aom_noise_tx_t *)aom_malloc(sizeof(struct aom_noise_tx_t));
41*77c1e3ccSAndroid Build Coastguard Worker   if (!noise_tx) return NULL;
42*77c1e3ccSAndroid Build Coastguard Worker   memset(noise_tx, 0, sizeof(*noise_tx));
43*77c1e3ccSAndroid Build Coastguard Worker   switch (block_size) {
44*77c1e3ccSAndroid Build Coastguard Worker     case 2:
45*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->fft = aom_fft2x2_float;
46*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->ifft = aom_ifft2x2_float;
47*77c1e3ccSAndroid Build Coastguard Worker       break;
48*77c1e3ccSAndroid Build Coastguard Worker     case 4:
49*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->fft = aom_fft4x4_float;
50*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->ifft = aom_ifft4x4_float;
51*77c1e3ccSAndroid Build Coastguard Worker       break;
52*77c1e3ccSAndroid Build Coastguard Worker     case 8:
53*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->fft = aom_fft8x8_float;
54*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->ifft = aom_ifft8x8_float;
55*77c1e3ccSAndroid Build Coastguard Worker       break;
56*77c1e3ccSAndroid Build Coastguard Worker     case 16:
57*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->fft = aom_fft16x16_float;
58*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->ifft = aom_ifft16x16_float;
59*77c1e3ccSAndroid Build Coastguard Worker       break;
60*77c1e3ccSAndroid Build Coastguard Worker     case 32:
61*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->fft = aom_fft32x32_float;
62*77c1e3ccSAndroid Build Coastguard Worker       noise_tx->ifft = aom_ifft32x32_float;
63*77c1e3ccSAndroid Build Coastguard Worker       break;
64*77c1e3ccSAndroid Build Coastguard Worker     default:
65*77c1e3ccSAndroid Build Coastguard Worker       aom_free(noise_tx);
66*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Unsupported block size %d\n", block_size);
67*77c1e3ccSAndroid Build Coastguard Worker       return NULL;
68*77c1e3ccSAndroid Build Coastguard Worker   }
69*77c1e3ccSAndroid Build Coastguard Worker   noise_tx->block_size = block_size;
70*77c1e3ccSAndroid Build Coastguard Worker   noise_tx->tx_block = (float *)aom_memalign(
71*77c1e3ccSAndroid Build Coastguard Worker       32, 2 * sizeof(*noise_tx->tx_block) * block_size * block_size);
72*77c1e3ccSAndroid Build Coastguard Worker   noise_tx->temp = (float *)aom_memalign(
73*77c1e3ccSAndroid Build Coastguard Worker       32, 2 * sizeof(*noise_tx->temp) * block_size * block_size);
74*77c1e3ccSAndroid Build Coastguard Worker   if (!noise_tx->tx_block || !noise_tx->temp) {
75*77c1e3ccSAndroid Build Coastguard Worker     aom_noise_tx_free(noise_tx);
76*77c1e3ccSAndroid Build Coastguard Worker     return NULL;
77*77c1e3ccSAndroid Build Coastguard Worker   }
78*77c1e3ccSAndroid Build Coastguard Worker   // Clear the buffers up front. Some outputs of the forward transform are
79*77c1e3ccSAndroid Build Coastguard Worker   // real only (the imaginary component will never be touched)
80*77c1e3ccSAndroid Build Coastguard Worker   memset(noise_tx->tx_block, 0,
81*77c1e3ccSAndroid Build Coastguard Worker          2 * sizeof(*noise_tx->tx_block) * block_size * block_size);
82*77c1e3ccSAndroid Build Coastguard Worker   memset(noise_tx->temp, 0,
83*77c1e3ccSAndroid Build Coastguard Worker          2 * sizeof(*noise_tx->temp) * block_size * block_size);
84*77c1e3ccSAndroid Build Coastguard Worker   return noise_tx;
85*77c1e3ccSAndroid Build Coastguard Worker }
86*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_forward(struct aom_noise_tx_t * noise_tx,const float * data)87*77c1e3ccSAndroid Build Coastguard Worker void aom_noise_tx_forward(struct aom_noise_tx_t *noise_tx, const float *data) {
88*77c1e3ccSAndroid Build Coastguard Worker   noise_tx->fft(data, noise_tx->temp, noise_tx->tx_block);
89*77c1e3ccSAndroid Build Coastguard Worker }
90*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_filter(struct aom_noise_tx_t * noise_tx,const float * psd)91*77c1e3ccSAndroid Build Coastguard Worker void aom_noise_tx_filter(struct aom_noise_tx_t *noise_tx, const float *psd) {
92*77c1e3ccSAndroid Build Coastguard Worker   const int block_size = noise_tx->block_size;
93*77c1e3ccSAndroid Build Coastguard Worker   const float kBeta = 1.1f;
94*77c1e3ccSAndroid Build Coastguard Worker   const float kEps = 1e-6f;
95*77c1e3ccSAndroid Build Coastguard Worker   for (int y = 0; y < block_size; ++y) {
96*77c1e3ccSAndroid Build Coastguard Worker     for (int x = 0; x < block_size; ++x) {
97*77c1e3ccSAndroid Build Coastguard Worker       int i = y * block_size + x;
98*77c1e3ccSAndroid Build Coastguard Worker       float *c = noise_tx->tx_block + 2 * i;
99*77c1e3ccSAndroid Build Coastguard Worker       const float c0 = AOMMAX((float)fabs(c[0]), 1e-8f);
100*77c1e3ccSAndroid Build Coastguard Worker       const float c1 = AOMMAX((float)fabs(c[1]), 1e-8f);
101*77c1e3ccSAndroid Build Coastguard Worker       const float p = c0 * c0 + c1 * c1;
102*77c1e3ccSAndroid Build Coastguard Worker       if (p > kBeta * psd[i] && p > 1e-6) {
103*77c1e3ccSAndroid Build Coastguard Worker         noise_tx->tx_block[2 * i + 0] *= (p - psd[i]) / AOMMAX(p, kEps);
104*77c1e3ccSAndroid Build Coastguard Worker         noise_tx->tx_block[2 * i + 1] *= (p - psd[i]) / AOMMAX(p, kEps);
105*77c1e3ccSAndroid Build Coastguard Worker       } else {
106*77c1e3ccSAndroid Build Coastguard Worker         noise_tx->tx_block[2 * i + 0] *= (kBeta - 1.0f) / kBeta;
107*77c1e3ccSAndroid Build Coastguard Worker         noise_tx->tx_block[2 * i + 1] *= (kBeta - 1.0f) / kBeta;
108*77c1e3ccSAndroid Build Coastguard Worker       }
109*77c1e3ccSAndroid Build Coastguard Worker     }
110*77c1e3ccSAndroid Build Coastguard Worker   }
111*77c1e3ccSAndroid Build Coastguard Worker }
112*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_inverse(struct aom_noise_tx_t * noise_tx,float * data)113*77c1e3ccSAndroid Build Coastguard Worker void aom_noise_tx_inverse(struct aom_noise_tx_t *noise_tx, float *data) {
114*77c1e3ccSAndroid Build Coastguard Worker   const int n = noise_tx->block_size * noise_tx->block_size;
115*77c1e3ccSAndroid Build Coastguard Worker   noise_tx->ifft(noise_tx->tx_block, noise_tx->temp, data);
116*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n; ++i) {
117*77c1e3ccSAndroid Build Coastguard Worker     data[i] /= n;
118*77c1e3ccSAndroid Build Coastguard Worker   }
119*77c1e3ccSAndroid Build Coastguard Worker }
120*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_add_energy(const struct aom_noise_tx_t * noise_tx,float * psd)121*77c1e3ccSAndroid Build Coastguard Worker void aom_noise_tx_add_energy(const struct aom_noise_tx_t *noise_tx,
122*77c1e3ccSAndroid Build Coastguard Worker                              float *psd) {
123*77c1e3ccSAndroid Build Coastguard Worker   const int block_size = noise_tx->block_size;
124*77c1e3ccSAndroid Build Coastguard Worker   for (int yb = 0; yb < block_size; ++yb) {
125*77c1e3ccSAndroid Build Coastguard Worker     for (int xb = 0; xb <= block_size / 2; ++xb) {
126*77c1e3ccSAndroid Build Coastguard Worker       float *c = noise_tx->tx_block + 2 * (yb * block_size + xb);
127*77c1e3ccSAndroid Build Coastguard Worker       psd[yb * block_size + xb] += c[0] * c[0] + c[1] * c[1];
128*77c1e3ccSAndroid Build Coastguard Worker     }
129*77c1e3ccSAndroid Build Coastguard Worker   }
130*77c1e3ccSAndroid Build Coastguard Worker }
131*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_tx_free(struct aom_noise_tx_t * noise_tx)132*77c1e3ccSAndroid Build Coastguard Worker void aom_noise_tx_free(struct aom_noise_tx_t *noise_tx) {
133*77c1e3ccSAndroid Build Coastguard Worker   if (!noise_tx) return;
134*77c1e3ccSAndroid Build Coastguard Worker   aom_free(noise_tx->tx_block);
135*77c1e3ccSAndroid Build Coastguard Worker   aom_free(noise_tx->temp);
136*77c1e3ccSAndroid Build Coastguard Worker   aom_free(noise_tx);
137*77c1e3ccSAndroid Build Coastguard Worker }
138*77c1e3ccSAndroid Build Coastguard Worker 
aom_normalized_cross_correlation(const double * a,const double * b,int n)139*77c1e3ccSAndroid Build Coastguard Worker double aom_normalized_cross_correlation(const double *a, const double *b,
140*77c1e3ccSAndroid Build Coastguard Worker                                         int n) {
141*77c1e3ccSAndroid Build Coastguard Worker   double c = 0;
142*77c1e3ccSAndroid Build Coastguard Worker   double a_len = 0;
143*77c1e3ccSAndroid Build Coastguard Worker   double b_len = 0;
144*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < n; ++i) {
145*77c1e3ccSAndroid Build Coastguard Worker     a_len += a[i] * a[i];
146*77c1e3ccSAndroid Build Coastguard Worker     b_len += b[i] * b[i];
147*77c1e3ccSAndroid Build Coastguard Worker     c += a[i] * b[i];
148*77c1e3ccSAndroid Build Coastguard Worker   }
149*77c1e3ccSAndroid Build Coastguard Worker   return c / (sqrt(a_len) * sqrt(b_len));
150*77c1e3ccSAndroid Build Coastguard Worker }
151*77c1e3ccSAndroid Build Coastguard Worker 
aom_noise_data_validate(const double * data,int w,int h)152*77c1e3ccSAndroid Build Coastguard Worker int aom_noise_data_validate(const double *data, int w, int h) {
153*77c1e3ccSAndroid Build Coastguard Worker   const double kVarianceThreshold = 2;
154*77c1e3ccSAndroid Build Coastguard Worker   const double kMeanThreshold = 2;
155*77c1e3ccSAndroid Build Coastguard Worker 
156*77c1e3ccSAndroid Build Coastguard Worker   int x = 0, y = 0;
157*77c1e3ccSAndroid Build Coastguard Worker   int ret_value = 1;
158*77c1e3ccSAndroid Build Coastguard Worker   double var = 0, mean = 0;
159*77c1e3ccSAndroid Build Coastguard Worker   double *mean_x, *mean_y, *var_x, *var_y;
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker   // Check that noise variance is not increasing in x or y
162*77c1e3ccSAndroid Build Coastguard Worker   // and that the data is zero mean.
163*77c1e3ccSAndroid Build Coastguard Worker   mean_x = (double *)aom_calloc(w, sizeof(*mean_x));
164*77c1e3ccSAndroid Build Coastguard Worker   var_x = (double *)aom_calloc(w, sizeof(*var_x));
165*77c1e3ccSAndroid Build Coastguard Worker   mean_y = (double *)aom_calloc(h, sizeof(*mean_x));
166*77c1e3ccSAndroid Build Coastguard Worker   var_y = (double *)aom_calloc(h, sizeof(*var_y));
167*77c1e3ccSAndroid Build Coastguard Worker   if (!(mean_x && var_x && mean_y && var_y)) {
168*77c1e3ccSAndroid Build Coastguard Worker     aom_free(mean_x);
169*77c1e3ccSAndroid Build Coastguard Worker     aom_free(mean_y);
170*77c1e3ccSAndroid Build Coastguard Worker     aom_free(var_x);
171*77c1e3ccSAndroid Build Coastguard Worker     aom_free(var_y);
172*77c1e3ccSAndroid Build Coastguard Worker     return 0;
173*77c1e3ccSAndroid Build Coastguard Worker   }
174*77c1e3ccSAndroid Build Coastguard Worker 
175*77c1e3ccSAndroid Build Coastguard Worker   for (y = 0; y < h; ++y) {
176*77c1e3ccSAndroid Build Coastguard Worker     for (x = 0; x < w; ++x) {
177*77c1e3ccSAndroid Build Coastguard Worker       const double d = data[y * w + x];
178*77c1e3ccSAndroid Build Coastguard Worker       var_x[x] += d * d;
179*77c1e3ccSAndroid Build Coastguard Worker       var_y[y] += d * d;
180*77c1e3ccSAndroid Build Coastguard Worker       mean_x[x] += d;
181*77c1e3ccSAndroid Build Coastguard Worker       mean_y[y] += d;
182*77c1e3ccSAndroid Build Coastguard Worker       var += d * d;
183*77c1e3ccSAndroid Build Coastguard Worker       mean += d;
184*77c1e3ccSAndroid Build Coastguard Worker     }
185*77c1e3ccSAndroid Build Coastguard Worker   }
186*77c1e3ccSAndroid Build Coastguard Worker   mean /= (w * h);
187*77c1e3ccSAndroid Build Coastguard Worker   var = var / (w * h) - mean * mean;
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker   for (y = 0; y < h; ++y) {
190*77c1e3ccSAndroid Build Coastguard Worker     mean_y[y] /= h;
191*77c1e3ccSAndroid Build Coastguard Worker     var_y[y] = var_y[y] / h - mean_y[y] * mean_y[y];
192*77c1e3ccSAndroid Build Coastguard Worker     if (fabs(var_y[y] - var) >= kVarianceThreshold) {
193*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Variance distance too large %f %f\n", var_y[y], var);
194*77c1e3ccSAndroid Build Coastguard Worker       ret_value = 0;
195*77c1e3ccSAndroid Build Coastguard Worker       break;
196*77c1e3ccSAndroid Build Coastguard Worker     }
197*77c1e3ccSAndroid Build Coastguard Worker     if (fabs(mean_y[y] - mean) >= kMeanThreshold) {
198*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Mean distance too large %f %f\n", mean_y[y], mean);
199*77c1e3ccSAndroid Build Coastguard Worker       ret_value = 0;
200*77c1e3ccSAndroid Build Coastguard Worker       break;
201*77c1e3ccSAndroid Build Coastguard Worker     }
202*77c1e3ccSAndroid Build Coastguard Worker   }
203*77c1e3ccSAndroid Build Coastguard Worker 
204*77c1e3ccSAndroid Build Coastguard Worker   for (x = 0; x < w; ++x) {
205*77c1e3ccSAndroid Build Coastguard Worker     mean_x[x] /= w;
206*77c1e3ccSAndroid Build Coastguard Worker     var_x[x] = var_x[x] / w - mean_x[x] * mean_x[x];
207*77c1e3ccSAndroid Build Coastguard Worker     if (fabs(var_x[x] - var) >= kVarianceThreshold) {
208*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Variance distance too large %f %f\n", var_x[x], var);
209*77c1e3ccSAndroid Build Coastguard Worker       ret_value = 0;
210*77c1e3ccSAndroid Build Coastguard Worker       break;
211*77c1e3ccSAndroid Build Coastguard Worker     }
212*77c1e3ccSAndroid Build Coastguard Worker     if (fabs(mean_x[x] - mean) >= kMeanThreshold) {
213*77c1e3ccSAndroid Build Coastguard Worker       fprintf(stderr, "Mean distance too large %f %f\n", mean_x[x], mean);
214*77c1e3ccSAndroid Build Coastguard Worker       ret_value = 0;
215*77c1e3ccSAndroid Build Coastguard Worker       break;
216*77c1e3ccSAndroid Build Coastguard Worker     }
217*77c1e3ccSAndroid Build Coastguard Worker   }
218*77c1e3ccSAndroid Build Coastguard Worker 
219*77c1e3ccSAndroid Build Coastguard Worker   aom_free(mean_x);
220*77c1e3ccSAndroid Build Coastguard Worker   aom_free(mean_y);
221*77c1e3ccSAndroid Build Coastguard Worker   aom_free(var_x);
222*77c1e3ccSAndroid Build Coastguard Worker   aom_free(var_y);
223*77c1e3ccSAndroid Build Coastguard Worker 
224*77c1e3ccSAndroid Build Coastguard Worker   return ret_value;
225*77c1e3ccSAndroid Build Coastguard Worker }
226