FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
float_dsp.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "config.h"
20 
21 #include <float.h>
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h> /* for getopt */
28 #endif
29 #if !HAVE_GETOPT
30 #include "compat/getopt.c"
31 #endif
32 
33 #include "libavutil/common.h"
34 #include "libavutil/cpu.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/lfg.h"
37 #include "libavutil/log.h"
38 #include "libavutil/random_seed.h"
39 #include "libavutil/float_dsp.h"
40 
41 #define LEN 240
42 
43 static void fill_float_array(AVLFG *lfg, float *a, int len)
44 {
45  int i;
46  double bmg[2], stddev = 10.0, mean = 0.0;
47 
48  for (i = 0; i < len; i += 2) {
49  av_bmg_get(lfg, bmg);
50  a[i] = bmg[0] * stddev + mean;
51  a[i + 1] = bmg[1] * stddev + mean;
52  }
53 }
54 static int compare_floats(const float *a, const float *b, int len,
55  float max_diff)
56 {
57  int i;
58  for (i = 0; i < len; i++) {
59  if (fabsf(a[i] - b[i]) > max_diff) {
60  av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
61  i, a[i], b[i], a[i] - b[i]);
62  return -1;
63  }
64  }
65  return 0;
66 }
67 
68 static void fill_double_array(AVLFG *lfg, double *a, int len)
69 {
70  int i;
71  double bmg[2], stddev = 10.0, mean = 0.0;
72 
73  for (i = 0; i < len; i += 2) {
74  av_bmg_get(lfg, bmg);
75  a[i] = bmg[0] * stddev + mean;
76  a[i + 1] = bmg[1] * stddev + mean;
77  }
78 }
79 
80 static int compare_doubles(const double *a, const double *b, int len,
81  double max_diff)
82 {
83  int i;
84 
85  for (i = 0; i < len; i++) {
86  if (fabs(a[i] - b[i]) > max_diff) {
87  av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
88  i, a[i], b[i], a[i] - b[i]);
89  return -1;
90  }
91  }
92  return 0;
93 }
94 
96  const float *v1, const float *v2)
97 {
98  LOCAL_ALIGNED(32, float, cdst, [LEN]);
99  LOCAL_ALIGNED(32, float, odst, [LEN]);
100  int ret;
101 
102  cdsp->vector_fmul(cdst, v1, v2, LEN);
103  fdsp->vector_fmul(odst, v1, v2, LEN);
104 
105  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
106  av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
107 
108  return ret;
109 }
110 
111 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
113  const float *v1, const float *src0, float scale)
114 {
115  LOCAL_ALIGNED(32, float, cdst, [LEN]);
116  LOCAL_ALIGNED(32, float, odst, [LEN]);
117  int ret;
118 
119  memcpy(cdst, v1, LEN * sizeof(*v1));
120  memcpy(odst, v1, LEN * sizeof(*v1));
121 
122  cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
123  fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
124 
125  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
126  av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
127 
128  return ret;
129 }
130 
132  const float *v1, float scale)
133 {
134  LOCAL_ALIGNED(32, float, cdst, [LEN]);
135  LOCAL_ALIGNED(32, float, odst, [LEN]);
136  int ret;
137 
138  cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
139  fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
140 
141  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
142  av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
143 
144  return ret;
145 }
146 
148  const double *v1, double scale)
149 {
150  LOCAL_ALIGNED(32, double, cdst, [LEN]);
151  LOCAL_ALIGNED(32, double, odst, [LEN]);
152  int ret;
153 
154  cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
155  fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
156 
157  if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
158  av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
159 
160  return ret;
161 }
162 
163 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
165  const float *v1, const float *v2, const float *v3)
166 {
167  LOCAL_ALIGNED(32, float, cdst, [LEN]);
168  LOCAL_ALIGNED(32, float, odst, [LEN]);
169  int ret;
170 
171  cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
172  fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
173 
174  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
175  av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
176 
177  return ret;
178 }
179 
180 #define ARBITRARY_FMUL_ADD_CONST 0.005
182  const float *v1, const float *v2, const float *v3)
183 {
184  LOCAL_ALIGNED(32, float, cdst, [LEN]);
185  LOCAL_ALIGNED(32, float, odst, [LEN]);
186  int ret;
187 
188  cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
189  fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
190 
191  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
192  av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
193 
194  return ret;
195 }
196 
198  const float *v1, const float *v2)
199 {
200  LOCAL_ALIGNED(32, float, cdst, [LEN]);
201  LOCAL_ALIGNED(32, float, odst, [LEN]);
202  int ret;
203 
204  cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
205  fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
206 
207  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
208  av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
209 
210  return ret;
211 }
212 
214  const float *v1, const float *v2)
215 {
216  LOCAL_ALIGNED(32, float, cv1, [LEN]);
217  LOCAL_ALIGNED(32, float, cv2, [LEN]);
218  LOCAL_ALIGNED(32, float, ov1, [LEN]);
219  LOCAL_ALIGNED(32, float, ov2, [LEN]);
220  int ret;
221 
222  memcpy(cv1, v1, LEN * sizeof(*v1));
223  memcpy(cv2, v2, LEN * sizeof(*v2));
224  memcpy(ov1, v1, LEN * sizeof(*v1));
225  memcpy(ov2, v2, LEN * sizeof(*v2));
226 
227  cdsp->butterflies_float(cv1, cv2, LEN);
228  fdsp->butterflies_float(ov1, ov2, LEN);
229 
230  if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
231  (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
232  av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
233 
234  return ret;
235 }
236 
237 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
239  const float *v1, const float *v2)
240 {
241  float cprod, oprod;
242  int ret;
243 
244  cprod = cdsp->scalarproduct_float(v1, v2, LEN);
245  oprod = fdsp->scalarproduct_float(v1, v2, LEN);
246 
247  if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
248  av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
249 
250  return ret;
251 }
252 
253 int main(int argc, char **argv)
254 {
255  int ret = 0, seeded = 0;
256  uint32_t seed;
257  AVFloatDSPContext *fdsp, *cdsp;
258  AVLFG lfg;
259 
260  LOCAL_ALIGNED(32, float, src0, [LEN]);
261  LOCAL_ALIGNED(32, float, src1, [LEN]);
262  LOCAL_ALIGNED(32, float, src2, [LEN]);
263  LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
264  LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
265 
266  for (;;) {
267  int arg = getopt(argc, argv, "s:c:");
268  if (arg == -1)
269  break;
270  switch (arg) {
271  case 's':
272  seed = strtoul(optarg, NULL, 10);
273  seeded = 1;
274  break;
275  case 'c':
276  {
277  int cpuflags = av_get_cpu_flags();
278 
279  if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
280  return 1;
281 
282  av_force_cpu_flags(cpuflags);
283  break;
284  }
285  }
286  }
287  if (!seeded)
288  seed = av_get_random_seed();
289 
290  av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
291 
292  fdsp = avpriv_float_dsp_alloc(1);
294  cdsp = avpriv_float_dsp_alloc(1);
295 
296  if (!fdsp || !cdsp) {
297  ret = 1;
298  goto end;
299  }
300 
301  av_lfg_init(&lfg, seed);
302 
303  fill_float_array(&lfg, src0, LEN);
304  fill_float_array(&lfg, src1, LEN);
305  fill_float_array(&lfg, src2, LEN);
306 
307  fill_double_array(&lfg, dbl_src0, LEN);
308  fill_double_array(&lfg, dbl_src1, LEN);
309 
310  if (test_vector_fmul(fdsp, cdsp, src0, src1))
311  ret -= 1 << 0;
312  if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
313  ret -= 1 << 1;
314  if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
315  ret -= 1 << 2;
316  if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
317  ret -= 1 << 3;
318  if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
319  ret -= 1 << 4;
320  if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
321  ret -= 1 << 5;
322  if (test_butterflies_float(fdsp, cdsp, src0, src1))
323  ret -= 1 << 6;
324  if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
325  ret -= 1 << 7;
326  if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
327  ret -= 1 << 8;
328 
329 end:
330  av_freep(&fdsp);
331  av_freep(&cdsp);
332  return ret;
333 }
Definition: lfg.h:27
#define NULL
Definition: coverity.c:32
#define ARBITRARY_FMUL_WINDOW_CONST
Definition: float_dsp.c:163
const char * b
Definition: vf_curves.c:113
static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:238
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
int main(int argc, char **argv)
Definition: float_dsp.c:253
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:138
static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:95
static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *src0, float scale)
Definition: float_dsp.c:112
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_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:103
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, float scale)
Definition: float_dsp.c:131
#define ARBITRARY_SCALARPRODUCT_CONST
Definition: float_dsp.c:237
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:49
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
void(* butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:148
const char * arg
Definition: jacosubdec.c:66
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
int av_parse_cpu_caps(unsigned *flags, const char *s)
Parse CPU caps from a string and update the given AV_CPU_* flags based on that.
Definition: cpu.c:185
common internal API header
static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const double *v1, double scale)
Definition: float_dsp.c:147
static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:197
#define LEN
Definition: float_dsp.c:41
static int compare_floats(const float *a, const float *b, int len, float max_diff)
Definition: float_dsp.c:54
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
static int compare_doubles(const double *a, const double *b, int len, double max_diff)
Definition: float_dsp.c:80
#define src1
Definition: h264pred.c:139
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static void fill_double_array(AVLFG *lfg, double *a, int len)
Definition: float_dsp.c:68
static unsigned int seed
Definition: videogen.c:78
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:41
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the entry wise product of two vectors of floats, add a third vector of floats and store the...
Definition: float_dsp.h:121
#define src0
Definition: h264pred.c:138
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2, const float *v3)
Definition: float_dsp.c:164
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:119
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:89
static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2, const float *v3)
Definition: float_dsp.c:181
common internal and external API header
#define ARBITRARY_FMUL_ADD_CONST
Definition: float_dsp.c:180
#define LOCAL_ALIGNED(a, t, v,...)
Definition: internal.h:113
static char * optarg
Definition: getopt.c:39
int len
#define ARBITRARY_FMAC_SCALAR_CONST
Definition: float_dsp.c:111
#define av_freep(p)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
Definition: cpu.c:63
static void fill_float_array(AVLFG *lfg, float *a, int len)
Definition: float_dsp.c:43
static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:213