• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, 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 #ifndef AOM_AV1_ENCODER_MATHUTILS_H_
13 #define AOM_AV1_ENCODER_MATHUTILS_H_
14 
15 #include <memory.h>
16 #include <math.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <assert.h>
20 
21 static const double TINY_NEAR_ZERO = 1.0E-16;
22 
23 // Solves Ax = b, where x and b are column vectors of size nx1 and A is nxn
linsolve(int n,double * A,int stride,double * b,double * x)24 static INLINE int linsolve(int n, double *A, int stride, double *b, double *x) {
25   int i, j, k;
26   double c;
27   // Forward elimination
28   for (k = 0; k < n - 1; k++) {
29     // Bring the largest magnitude to the diagonal position
30     for (i = n - 1; i > k; i--) {
31       if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) {
32         for (j = 0; j < n; j++) {
33           c = A[i * stride + j];
34           A[i * stride + j] = A[(i - 1) * stride + j];
35           A[(i - 1) * stride + j] = c;
36         }
37         c = b[i];
38         b[i] = b[i - 1];
39         b[i - 1] = c;
40       }
41     }
42     for (i = k; i < n - 1; i++) {
43       if (fabs(A[k * stride + k]) < TINY_NEAR_ZERO) return 0;
44       c = A[(i + 1) * stride + k] / A[k * stride + k];
45       for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j];
46       b[i + 1] -= c * b[k];
47     }
48   }
49   // Backward substitution
50   for (i = n - 1; i >= 0; i--) {
51     if (fabs(A[i * stride + i]) < TINY_NEAR_ZERO) return 0;
52     c = 0;
53     for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j];
54     x[i] = (b[i] - c) / A[i * stride + i];
55   }
56 
57   return 1;
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 // Least-squares
62 // Solves for n-dim x in a least squares sense to minimize |Ax - b|^2
63 // The solution is simply x = (A'A)^-1 A'b or simply the solution for
64 // the system: A'A x = A'b
least_squares(int n,double * A,int rows,int stride,double * b,double * scratch,double * x)65 static INLINE int least_squares(int n, double *A, int rows, int stride,
66                                 double *b, double *scratch, double *x) {
67   int i, j, k;
68   double *scratch_ = NULL;
69   double *AtA, *Atb;
70   if (!scratch) {
71     scratch_ = (double *)aom_malloc(sizeof(*scratch) * n * (n + 1));
72     scratch = scratch_;
73   }
74   AtA = scratch;
75   Atb = scratch + n * n;
76 
77   for (i = 0; i < n; ++i) {
78     for (j = i; j < n; ++j) {
79       AtA[i * n + j] = 0.0;
80       for (k = 0; k < rows; ++k)
81         AtA[i * n + j] += A[k * stride + i] * A[k * stride + j];
82       AtA[j * n + i] = AtA[i * n + j];
83     }
84     Atb[i] = 0;
85     for (k = 0; k < rows; ++k) Atb[i] += A[k * stride + i] * b[k];
86   }
87   int ret = linsolve(n, AtA, n, Atb, x);
88   if (scratch_) aom_free(scratch_);
89   return ret;
90 }
91 
92 // Matrix multiply
multiply_mat(const double * m1,const double * m2,double * res,const int m1_rows,const int inner_dim,const int m2_cols)93 static INLINE void multiply_mat(const double *m1, const double *m2, double *res,
94                                 const int m1_rows, const int inner_dim,
95                                 const int m2_cols) {
96   double sum;
97 
98   int row, col, inner;
99   for (row = 0; row < m1_rows; ++row) {
100     for (col = 0; col < m2_cols; ++col) {
101       sum = 0;
102       for (inner = 0; inner < inner_dim; ++inner)
103         sum += m1[row * inner_dim + inner] * m2[inner * m2_cols + col];
104       *(res++) = sum;
105     }
106   }
107 }
108 
109 //
110 // The functions below are needed only for homography computation
111 // Remove if the homography models are not used.
112 //
113 ///////////////////////////////////////////////////////////////////////////////
114 // svdcmp
115 // Adopted from Numerical Recipes in C
116 
sign(double a,double b)117 static INLINE double sign(double a, double b) {
118   return ((b) >= 0 ? fabs(a) : -fabs(a));
119 }
120 
pythag(double a,double b)121 static INLINE double pythag(double a, double b) {
122   double ct;
123   const double absa = fabs(a);
124   const double absb = fabs(b);
125 
126   if (absa > absb) {
127     ct = absb / absa;
128     return absa * sqrt(1.0 + ct * ct);
129   } else {
130     ct = absa / absb;
131     return (absb == 0) ? 0 : absb * sqrt(1.0 + ct * ct);
132   }
133 }
134 
svdcmp(double ** u,int m,int n,double w[],double ** v)135 static INLINE int svdcmp(double **u, int m, int n, double w[], double **v) {
136   const int max_its = 30;
137   int flag, i, its, j, jj, k, l, nm;
138   double anorm, c, f, g, h, s, scale, x, y, z;
139   double *rv1 = (double *)aom_malloc(sizeof(*rv1) * (n + 1));
140   g = scale = anorm = 0.0;
141   for (i = 0; i < n; i++) {
142     l = i + 1;
143     rv1[i] = scale * g;
144     g = s = scale = 0.0;
145     if (i < m) {
146       for (k = i; k < m; k++) scale += fabs(u[k][i]);
147       if (scale != 0.) {
148         for (k = i; k < m; k++) {
149           u[k][i] /= scale;
150           s += u[k][i] * u[k][i];
151         }
152         f = u[i][i];
153         g = -sign(sqrt(s), f);
154         h = f * g - s;
155         u[i][i] = f - g;
156         for (j = l; j < n; j++) {
157           for (s = 0.0, k = i; k < m; k++) s += u[k][i] * u[k][j];
158           f = s / h;
159           for (k = i; k < m; k++) u[k][j] += f * u[k][i];
160         }
161         for (k = i; k < m; k++) u[k][i] *= scale;
162       }
163     }
164     w[i] = scale * g;
165     g = s = scale = 0.0;
166     if (i < m && i != n - 1) {
167       for (k = l; k < n; k++) scale += fabs(u[i][k]);
168       if (scale != 0.) {
169         for (k = l; k < n; k++) {
170           u[i][k] /= scale;
171           s += u[i][k] * u[i][k];
172         }
173         f = u[i][l];
174         g = -sign(sqrt(s), f);
175         h = f * g - s;
176         u[i][l] = f - g;
177         for (k = l; k < n; k++) rv1[k] = u[i][k] / h;
178         for (j = l; j < m; j++) {
179           for (s = 0.0, k = l; k < n; k++) s += u[j][k] * u[i][k];
180           for (k = l; k < n; k++) u[j][k] += s * rv1[k];
181         }
182         for (k = l; k < n; k++) u[i][k] *= scale;
183       }
184     }
185     anorm = fmax(anorm, (fabs(w[i]) + fabs(rv1[i])));
186   }
187 
188   for (i = n - 1; i >= 0; i--) {
189     if (i < n - 1) {
190       if (g != 0.) {
191         for (j = l; j < n; j++) v[j][i] = (u[i][j] / u[i][l]) / g;
192         for (j = l; j < n; j++) {
193           for (s = 0.0, k = l; k < n; k++) s += u[i][k] * v[k][j];
194           for (k = l; k < n; k++) v[k][j] += s * v[k][i];
195         }
196       }
197       for (j = l; j < n; j++) v[i][j] = v[j][i] = 0.0;
198     }
199     v[i][i] = 1.0;
200     g = rv1[i];
201     l = i;
202   }
203   for (i = AOMMIN(m, n) - 1; i >= 0; i--) {
204     l = i + 1;
205     g = w[i];
206     for (j = l; j < n; j++) u[i][j] = 0.0;
207     if (g != 0.) {
208       g = 1.0 / g;
209       for (j = l; j < n; j++) {
210         for (s = 0.0, k = l; k < m; k++) s += u[k][i] * u[k][j];
211         f = (s / u[i][i]) * g;
212         for (k = i; k < m; k++) u[k][j] += f * u[k][i];
213       }
214       for (j = i; j < m; j++) u[j][i] *= g;
215     } else {
216       for (j = i; j < m; j++) u[j][i] = 0.0;
217     }
218     ++u[i][i];
219   }
220   for (k = n - 1; k >= 0; k--) {
221     for (its = 0; its < max_its; its++) {
222       flag = 1;
223       for (l = k; l >= 0; l--) {
224         nm = l - 1;
225         if ((double)(fabs(rv1[l]) + anorm) == anorm || nm < 0) {
226           flag = 0;
227           break;
228         }
229         if ((double)(fabs(w[nm]) + anorm) == anorm) break;
230       }
231       if (flag) {
232         c = 0.0;
233         s = 1.0;
234         for (i = l; i <= k; i++) {
235           f = s * rv1[i];
236           rv1[i] = c * rv1[i];
237           if ((double)(fabs(f) + anorm) == anorm) break;
238           g = w[i];
239           h = pythag(f, g);
240           w[i] = h;
241           h = 1.0 / h;
242           c = g * h;
243           s = -f * h;
244           for (j = 0; j < m; j++) {
245             y = u[j][nm];
246             z = u[j][i];
247             u[j][nm] = y * c + z * s;
248             u[j][i] = z * c - y * s;
249           }
250         }
251       }
252       z = w[k];
253       if (l == k) {
254         if (z < 0.0) {
255           w[k] = -z;
256           for (j = 0; j < n; j++) v[j][k] = -v[j][k];
257         }
258         break;
259       }
260       if (its == max_its - 1) {
261         aom_free(rv1);
262         return 1;
263       }
264       assert(k > 0);
265       x = w[l];
266       nm = k - 1;
267       y = w[nm];
268       g = rv1[nm];
269       h = rv1[k];
270       f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
271       g = pythag(f, 1.0);
272       f = ((x - z) * (x + z) + h * ((y / (f + sign(g, f))) - h)) / x;
273       c = s = 1.0;
274       for (j = l; j <= nm; j++) {
275         i = j + 1;
276         g = rv1[i];
277         y = w[i];
278         h = s * g;
279         g = c * g;
280         z = pythag(f, h);
281         rv1[j] = z;
282         c = f / z;
283         s = h / z;
284         f = x * c + g * s;
285         g = g * c - x * s;
286         h = y * s;
287         y *= c;
288         for (jj = 0; jj < n; jj++) {
289           x = v[jj][j];
290           z = v[jj][i];
291           v[jj][j] = x * c + z * s;
292           v[jj][i] = z * c - x * s;
293         }
294         z = pythag(f, h);
295         w[j] = z;
296         if (z != 0.) {
297           z = 1.0 / z;
298           c = f * z;
299           s = h * z;
300         }
301         f = c * g + s * y;
302         x = c * y - s * g;
303         for (jj = 0; jj < m; jj++) {
304           y = u[jj][j];
305           z = u[jj][i];
306           u[jj][j] = y * c + z * s;
307           u[jj][i] = z * c - y * s;
308         }
309       }
310       rv1[l] = 0.0;
311       rv1[k] = f;
312       w[k] = x;
313     }
314   }
315   aom_free(rv1);
316   return 0;
317 }
318 
SVD(double * U,double * W,double * V,double * matx,int M,int N)319 static INLINE int SVD(double *U, double *W, double *V, double *matx, int M,
320                       int N) {
321   // Assumes allocation for U is MxN
322   double **nrU = (double **)aom_malloc((M) * sizeof(*nrU));
323   double **nrV = (double **)aom_malloc((N) * sizeof(*nrV));
324   int problem, i;
325 
326   problem = !(nrU && nrV);
327   if (!problem) {
328     for (i = 0; i < M; i++) {
329       nrU[i] = &U[i * N];
330     }
331     for (i = 0; i < N; i++) {
332       nrV[i] = &V[i * N];
333     }
334   } else {
335     if (nrU) aom_free(nrU);
336     if (nrV) aom_free(nrV);
337     return 1;
338   }
339 
340   /* copy from given matx into nrU */
341   for (i = 0; i < M; i++) {
342     memcpy(&(nrU[i][0]), matx + N * i, N * sizeof(*matx));
343   }
344 
345   /* HERE IT IS: do SVD */
346   if (svdcmp(nrU, M, N, W, nrV)) {
347     aom_free(nrU);
348     aom_free(nrV);
349     return 1;
350   }
351 
352   /* aom_free Numerical Recipes arrays */
353   aom_free(nrU);
354   aom_free(nrV);
355 
356   return 0;
357 }
358 
359 #endif  // AOM_AV1_ENCODER_MATHUTILS_H_
360