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 
147 #define ARBITRARY_DMAC_SCALAR_CONST 0.005
149  const double *v1, const double *src0, double scale)
150 {
151  LOCAL_ALIGNED(32, double, cdst, [LEN]);
152  LOCAL_ALIGNED(32, double, odst, [LEN]);
153  int ret;
154 
155  memcpy(cdst, v1, LEN * sizeof(*v1));
156  memcpy(odst, v1, LEN * sizeof(*v1));
157 
158  cdsp->vector_dmac_scalar(cdst, src0, scale, LEN);
159  fdsp->vector_dmac_scalar(odst, src0, scale, LEN);
160 
161  if (ret = compare_doubles(cdst, odst, LEN, ARBITRARY_DMAC_SCALAR_CONST))
162  av_log(NULL, AV_LOG_ERROR, "vector_dmac_scalar failed\n");
163 
164  return ret;
165 }
166 
168  const double *v1, double scale)
169 {
170  LOCAL_ALIGNED(32, double, cdst, [LEN]);
171  LOCAL_ALIGNED(32, double, odst, [LEN]);
172  int ret;
173 
174  cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
175  fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
176 
177  if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
178  av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
179 
180  return ret;
181 }
182 
183 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
185  const float *v1, const float *v2, const float *v3)
186 {
187  LOCAL_ALIGNED(32, float, cdst, [LEN]);
188  LOCAL_ALIGNED(32, float, odst, [LEN]);
189  int ret;
190 
191  cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
192  fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
193 
194  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
195  av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
196 
197  return ret;
198 }
199 
200 #define ARBITRARY_FMUL_ADD_CONST 0.005
202  const float *v1, const float *v2, const float *v3)
203 {
204  LOCAL_ALIGNED(32, float, cdst, [LEN]);
205  LOCAL_ALIGNED(32, float, odst, [LEN]);
206  int ret;
207 
208  cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
209  fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
210 
211  if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
212  av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
213 
214  return ret;
215 }
216 
218  const float *v1, const float *v2)
219 {
220  LOCAL_ALIGNED(32, float, cdst, [LEN]);
221  LOCAL_ALIGNED(32, float, odst, [LEN]);
222  int ret;
223 
224  cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
225  fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
226 
227  if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
228  av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
229 
230  return ret;
231 }
232 
234  const float *v1, const float *v2)
235 {
236  LOCAL_ALIGNED(32, float, cv1, [LEN]);
237  LOCAL_ALIGNED(32, float, cv2, [LEN]);
238  LOCAL_ALIGNED(32, float, ov1, [LEN]);
239  LOCAL_ALIGNED(32, float, ov2, [LEN]);
240  int ret;
241 
242  memcpy(cv1, v1, LEN * sizeof(*v1));
243  memcpy(cv2, v2, LEN * sizeof(*v2));
244  memcpy(ov1, v1, LEN * sizeof(*v1));
245  memcpy(ov2, v2, LEN * sizeof(*v2));
246 
247  cdsp->butterflies_float(cv1, cv2, LEN);
248  fdsp->butterflies_float(ov1, ov2, LEN);
249 
250  if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
251  (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
252  av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
253 
254  return ret;
255 }
256 
257 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
259  const float *v1, const float *v2)
260 {
261  float cprod, oprod;
262  int ret;
263 
264  cprod = cdsp->scalarproduct_float(v1, v2, LEN);
265  oprod = fdsp->scalarproduct_float(v1, v2, LEN);
266 
267  if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
268  av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
269 
270  return ret;
271 }
272 
273 int main(int argc, char **argv)
274 {
275  int ret = 0, seeded = 0;
276  uint32_t seed;
277  AVFloatDSPContext *fdsp, *cdsp;
278  AVLFG lfg;
279 
280  LOCAL_ALIGNED(32, float, src0, [LEN]);
281  LOCAL_ALIGNED(32, float, src1, [LEN]);
282  LOCAL_ALIGNED(32, float, src2, [LEN]);
283  LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
284  LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
285  LOCAL_ALIGNED(32, double, dbl_src2, [LEN]);
286 
287  for (;;) {
288  int arg = getopt(argc, argv, "s:c:");
289  if (arg == -1)
290  break;
291  switch (arg) {
292  case 's':
293  seed = strtoul(optarg, NULL, 10);
294  seeded = 1;
295  break;
296  case 'c':
297  {
298  int cpuflags = av_get_cpu_flags();
299 
300  if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
301  return 1;
302 
303  av_force_cpu_flags(cpuflags);
304  break;
305  }
306  }
307  }
308  if (!seeded)
309  seed = av_get_random_seed();
310 
311  av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
312 
313  fdsp = avpriv_float_dsp_alloc(1);
315  cdsp = avpriv_float_dsp_alloc(1);
316 
317  if (!fdsp || !cdsp) {
318  ret = 1;
319  goto end;
320  }
321 
322  av_lfg_init(&lfg, seed);
323 
324  fill_float_array(&lfg, src0, LEN);
325  fill_float_array(&lfg, src1, LEN);
326  fill_float_array(&lfg, src2, LEN);
327 
328  fill_double_array(&lfg, dbl_src0, LEN);
329  fill_double_array(&lfg, dbl_src1, LEN);
330  fill_double_array(&lfg, dbl_src2, LEN);
331 
332  if (test_vector_fmul(fdsp, cdsp, src0, src1))
333  ret -= 1 << 0;
334  if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
335  ret -= 1 << 1;
336  if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
337  ret -= 1 << 2;
338  if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
339  ret -= 1 << 3;
340  if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
341  ret -= 1 << 4;
342  if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
343  ret -= 1 << 5;
344  if (test_butterflies_float(fdsp, cdsp, src0, src1))
345  ret -= 1 << 6;
346  if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
347  ret -= 1 << 7;
348  if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
349  ret -= 1 << 8;
350  if (test_vector_dmac_scalar(fdsp, cdsp, dbl_src2, dbl_src0, dbl_src1[0]))
351  ret -= 1 << 9;
352 
353 end:
354  av_freep(&fdsp);
355  av_freep(&cdsp);
356  return ret;
357 }
Definition: lfg.h:27
#define NULL
Definition: coverity.c:32
#define ARBITRARY_FMUL_WINDOW_CONST
Definition: float_dsp.c:183
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:258
float(* scalarproduct_float)(const float *v1, const float *v2, int len)
Calculate the scalar product of two vectors of floats.
Definition: float_dsp.h:175
int main(int argc, char **argv)
Definition: float_dsp.c:273
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:154
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:119
#define ARBITRARY_DMAC_SCALAR_CONST
Definition: float_dsp.c:147
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:257
void(* vector_dmac_scalar)(double *dst, const double *src, double mul, int len)
Multiply a vector of doubles by a scalar double and add to destination vector.
Definition: float_dsp.h:70
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:164
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:100
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:167
static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const float *v1, const float *v2)
Definition: float_dsp.c:217
#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:85
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:137
#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:184
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:127
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:201
common internal and external API header
#define ARBITRARY_FMUL_ADD_CONST
Definition: float_dsp.c:200
#define LOCAL_ALIGNED(a, t, v,...)
Definition: internal.h:113
static int test_vector_dmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, const double *v1, const double *src0, double scale)
Definition: float_dsp.c:148
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:233