Libav
float_dsp.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <float.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "libavutil/cpu.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/lfg.h"
27 #include "libavutil/log.h"
28 #include "libavutil/random_seed.h"
29 #include "libavutil/float_dsp.h"
30 
31 #define LEN 240
32 
33 static void fill_float_array(AVLFG *lfg, float *a, int len)
34 {
35  int i;
36  double bmg[2], stddev = 10.0, mean = 0.0;
37 
38  for (i = 0; i < len; i += 2) {
39  av_bmg_get(lfg, bmg);
40  a[i] = bmg[0] * stddev + mean;
41  a[i + 1] = bmg[1] * stddev + mean;
42  }
43 }
44 static int compare_floats(const float *a, const float *b, int len,
45  float max_diff)
46 {
47  int i;
48  for (i = 0; i < len; i++) {
49  if (fabsf(a[i] - b[i]) > max_diff) {
50  av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
51  i, a[i], b[i], a[i] - b[i]);
52  return -1;
53  }
54  }
55  return 0;
56 }
57 
58 static void fill_double_array(AVLFG *lfg, double *a, int len)
59 {
60  int i;
61  double bmg[2], stddev = 10.0, mean = 0.0;
62 
63  for (i = 0; i < len; i += 2) {
64  av_bmg_get(lfg, bmg);
65  a[i] = bmg[0] * stddev + mean;
66  a[i + 1] = bmg[1] * stddev + mean;
67  }
68 }
69 
70 static int compare_doubles(const double *a, const double *b, int len,
71  double max_diff)
72 {
73  int i;
74 
75  for (i = 0; i < len; i++) {
76  if (fabs(a[i] - b[i]) > max_diff) {
77  av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
78  i, a[i], b[i], a[i] - b[i]);
79  return -1;
80  }
81  }
82  return 0;
83 }
84 
86  const float *v1, const float *v2)
87 {
88  LOCAL_ALIGNED(32, float, cdst, [LEN]);
89  LOCAL_ALIGNED(32, float, odst, [LEN]);
90  int ret;
91 
92  cdsp->vector_fmul(cdst, v1, v2, LEN);
93  fdsp->vector_fmul(odst, v1, v2, LEN);
94 
95  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
96  av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
97 
98  return ret;
99 }
100 
101 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
103  const float *v1, const float *src0, float scale)
104 {
105  LOCAL_ALIGNED(32, float, cdst, [LEN]);
106  LOCAL_ALIGNED(32, float, odst, [LEN]);
107  int ret;
108 
109  memcpy(cdst, v1, LEN * sizeof(*v1));
110  memcpy(odst, v1, LEN * sizeof(*v1));
111 
112  cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
113  fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
114 
115  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
116  av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
117 
118  return ret;
119 }
120 
122  const float *v1, float scale)
123 {
124  LOCAL_ALIGNED(32, float, cdst, [LEN]);
125  LOCAL_ALIGNED(32, float, odst, [LEN]);
126  int ret;
127 
128  cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
129  fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
130 
131  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
132  av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
133 
134  return ret;
135 }
136 
138  const double *v1, double scale)
139 {
140  LOCAL_ALIGNED(32, double, cdst, [LEN]);
141  LOCAL_ALIGNED(32, double, odst, [LEN]);
142  int ret;
143 
144  cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
145  fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
146 
147  if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
148  av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
149 
150  return ret;
151 }
152 
153 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
155  const float *v1, const float *v2, const float *v3)
156 {
157  LOCAL_ALIGNED(32, float, cdst, [LEN]);
158  LOCAL_ALIGNED(32, float, odst, [LEN]);
159  int ret;
160 
161  cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
162  fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
163 
164  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
165  av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
166 
167  return ret;
168 }
169 
170 #define ARBITRARY_FMUL_ADD_CONST 0.005
172  const float *v1, const float *v2, const float *v3)
173 {
174  LOCAL_ALIGNED(32, float, cdst, [LEN]);
175  LOCAL_ALIGNED(32, float, odst, [LEN]);
176  int ret;
177 
178  cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
179  fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
180 
181  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
182  av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
183 
184  return ret;
185 }
186 
188  const float *v1, const float *v2)
189 {
190  LOCAL_ALIGNED(32, float, cdst, [LEN]);
191  LOCAL_ALIGNED(32, float, odst, [LEN]);
192  int ret;
193 
194  cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
195  fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
196 
197  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
198  av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
199 
200  return ret;
201 }
202 
204  const float *v1, const float *v2)
205 {
206  LOCAL_ALIGNED(32, float, cv1, [LEN]);
207  LOCAL_ALIGNED(32, float, cv2, [LEN]);
208  LOCAL_ALIGNED(32, float, ov1, [LEN]);
209  LOCAL_ALIGNED(32, float, ov2, [LEN]);
210  int ret;
211 
212  memcpy(cv1, v1, LEN * sizeof(*v1));
213  memcpy(cv2, v2, LEN * sizeof(*v2));
214  memcpy(ov1, v1, LEN * sizeof(*v1));
215  memcpy(ov2, v2, LEN * sizeof(*v2));
216 
217  cdsp->butterflies_float(cv1, cv2, LEN);
218  fdsp->butterflies_float(ov1, ov2, LEN);
219 
220  if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
221  (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
222  av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
223 
224  return ret;
225 }
226 
227 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
229  const float *v1, const float *v2)
230 {
231  float cprod, oprod;
232  int ret;
233 
234  cprod = cdsp->scalarproduct_float(v1, v2, LEN);
235  oprod = fdsp->scalarproduct_float(v1, v2, LEN);
236 
237  if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
238  av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
239 
240  return ret;
241 }
242 
243 int main(int argc, char **argv)
244 {
245  int ret = 0;
246  uint32_t seed;
247  AVFloatDSPContext fdsp, cdsp;
248  AVLFG lfg;
249 
250  LOCAL_ALIGNED(32, float, src0, [LEN]);
251  LOCAL_ALIGNED(32, float, src1, [LEN]);
252  LOCAL_ALIGNED(32, float, src2, [LEN]);
253  LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
254  LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
255 
256  if (argc > 2 && !strcmp(argv[1], "-s"))
257  seed = strtoul(argv[2], NULL, 10);
258  else
259  seed = av_get_random_seed();
260 
261  av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
262 
263  av_lfg_init(&lfg, seed);
264 
265  fill_float_array(&lfg, src0, LEN);
266  fill_float_array(&lfg, src1, LEN);
267  fill_float_array(&lfg, src2, LEN);
268 
269  fill_double_array(&lfg, dbl_src0, LEN);
270  fill_double_array(&lfg, dbl_src1, LEN);
271 
272  avpriv_float_dsp_init(&fdsp, 1);
274  avpriv_float_dsp_init(&cdsp, 1);
275 
276  if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
277  ret -= 1 << 0;
278  if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
279  ret -= 1 << 1;
280  if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
281  ret -= 1 << 2;
282  if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
283  ret -= 1 << 3;
284  if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
285  ret -= 1 << 4;
286  if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
287  ret -= 1 << 5;
288  if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
289  ret -= 1 << 6;
290  if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
291  ret -= 1 << 7;
292  if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
293  ret -= 1 << 8;
294 
295  return ret;
296 }
Definition: lfg.h:25
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
Definition: float_dsp.h:159
#define ARBITRARY_FMUL_WINDOW_CONST
Definition: float_dsp.c:153
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:69
static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:228
int main(int argc, char **argv)
Definition: float_dsp.c:243
static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:85
static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *src0, float scale)
Definition: float_dsp.c:102
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, float scale)
Definition: float_dsp.c:121
#define ARBITRARY_SCALARPRODUCT_CONST
Definition: float_dsp.c:227
#define b
Definition: input.c:52
void av_bmg_get(AVLFG *lfg, double out[2])
Get the next two numbers generated by a Box-Muller Gaussian generator using the random numbers issued...
Definition: lfg.c:47
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:103
void(* vector_dmul_scalar)(double *dst, const double *src, double mul, int len)
Multiply a vector of double by a scalar double.
Definition: float_dsp.h:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
common internal API header
static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const double *v1, double scale)
Definition: float_dsp.c:137
static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:187
#define LEN
Definition: float_dsp.c:31
static int compare_floats(const float *a, const float *b, int len, float max_diff)
Definition: float_dsp.c:44
static int compare_doubles(const double *a, const double *b, int len, double max_diff)
Definition: float_dsp.c:70
NULL
Definition: eval.c:55
#define src1
Definition: h264pred.c:139
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
static void fill_double_array(AVLFG *lfg, double *a, int len)
Definition: float_dsp.c:58
static unsigned int seed
Definition: videogen.c:78
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:69
#define src0
Definition: h264pred.c:138
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2, const float *v3)
Definition: float_dsp.c:154
static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2, const float *v3)
Definition: float_dsp.c:171
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
#define ARBITRARY_FMUL_ADD_CONST
Definition: float_dsp.c:170
#define LOCAL_ALIGNED(a, t, v,...)
Definition: internal.h:100
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the product of two vectors of floats, add a third vector of floats and store the result in ...
Definition: float_dsp.h:121
int len
#define ARBITRARY_FMAC_SCALAR_CONST
Definition: float_dsp.c:101
void(* butterflies_float)(float *restrict v1, float *restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:148
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:95
static void fill_float_array(AVLFG *lfg, float *a, int len)
Definition: float_dsp.c:33
static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:203
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:138