FFmpeg
av_tx.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 modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "libavutil/mem_internal.h"
20 #include "libavutil/tx.h"
21 #include "libavutil/error.h"
22 
23 #include "checkasm.h"
24 
25 #include <stdlib.h>
26 
27 #define EPS 0.00005
28 
29 #define SCALE_NOOP(x) (x)
30 #define SCALE_INT20(x) (av_clip64(lrintf((x) * 2147483648.0), INT32_MIN, INT32_MAX) >> 12)
31 
32 #define randomize_complex(BUF, LEN, TYPE, SCALE) \
33  do { \
34  TYPE *buf = (TYPE *)BUF; \
35  for (int i = 0; i < LEN; i++) { \
36  double fre = (double)rnd() / UINT_MAX; \
37  double fim = (double)rnd() / UINT_MAX; \
38  buf[i] = (TYPE){ SCALE(fre), SCALE(fim) }; \
39  } \
40  } while (0)
41 
42 static const int check_lens[] = {
43  2, 4, 8, 16, 32, 64, 1024, 16384,
44 };
45 
46 static AVTXContext *tx_refs[6 /*AVTXType*/][FF_ARRAY_ELEMS(check_lens)];
47 static int init = 0;
48 
49 static void free_tx_refs(void)
50 {
51  for (int i = 0; i < FF_ARRAY_ELEMS(tx_refs); i++)
52  for (int j = 0; j < FF_ARRAY_ELEMS(*tx_refs); j++)
53  av_tx_uninit(&tx_refs[i][j]);
54 }
55 
56 #define CHECK_TEMPLATE(PREFIX, TYPE, DATA_TYPE, SCALE, LENGTHS, CHECK_EXPRESSION) \
57  do { \
58  int err; \
59  AVTXContext *tx; \
60  av_tx_fn fn; \
61  int num_checks = 0; \
62  int last_check = 0; \
63  const void *scale = &SCALE; \
64  \
65  for (int i = 0; i < FF_ARRAY_ELEMS(LENGTHS); i++) { \
66  int len = LENGTHS[i]; \
67  \
68  if ((err = av_tx_init(&tx, &fn, TYPE, 0, len, &scale, 0x0)) < 0) { \
69  fprintf(stderr, "av_tx: %s\n", av_err2str(err)); \
70  return; \
71  } \
72  \
73  if (check_func(fn, PREFIX "_%i", len)) { \
74  AVTXContext *tx_ref = tx_refs[TYPE][i]; \
75  if (!tx_ref) \
76  tx_ref = tx; \
77  num_checks++; \
78  last_check = len; \
79  call_ref(tx_ref, out_ref, in, sizeof(DATA_TYPE)); \
80  call_new(tx, out_new, in, sizeof(DATA_TYPE)); \
81  if (CHECK_EXPRESSION) { \
82  fail(); \
83  av_tx_uninit(&tx); \
84  break; \
85  } \
86  bench_new(tx, out_new, in, sizeof(DATA_TYPE)); \
87  av_tx_uninit(&tx_refs[TYPE][i]); \
88  tx_refs[TYPE][i] = tx; \
89  } else { \
90  av_tx_uninit(&tx); \
91  } \
92  } \
93  \
94  if (num_checks == 1) \
95  report(PREFIX "_%i", last_check); \
96  else if (num_checks) \
97  report(PREFIX); \
98  } while (0)
99 
101 {
102  const float scale_float = 1.0f;
103  const double scale_double = 1.0f;
104 
105  declare_func(void, AVTXContext *tx, void *out, void *in, ptrdiff_t stride);
106 
107  void *in = av_malloc(16384*2*8);
108  void *out_ref = av_malloc(16384*2*8);
109  void *out_new = av_malloc(16384*2*8);
110 
112  CHECK_TEMPLATE("float_fft", AV_TX_FLOAT_FFT, AVComplexFloat, scale_float, check_lens,
113  !float_near_abs_eps_array(out_ref, out_new, EPS, len*2));
114 
117  !double_near_abs_eps_array(out_ref, out_new, EPS, len*2));
118 
119  av_free(in);
120  av_free(out_ref);
121  av_free(out_new);
122 
123  if (!init) {
124  init = 1;
125  atexit(free_tx_refs);
126  }
127 }
scale_double
static int scale_double(void *log, double d, double m, int *r)
Definition: sbgdec.c:212
mem_internal.h
out
FILE * out
Definition: movenc.c:54
float_near_abs_eps_array
int float_near_abs_eps_array(const float *a, const float *b, float eps, unsigned len)
Definition: checkasm.c:345
AVTXContext
Definition: tx_priv.h:201
CHECK_TEMPLATE
#define CHECK_TEMPLATE(PREFIX, TYPE, DATA_TYPE, SCALE, LENGTHS, CHECK_EXPRESSION)
Definition: av_tx.c:56
AV_TX_DOUBLE_FFT
@ AV_TX_DOUBLE_FFT
Definition: tx.h:48
AVComplexFloat
Definition: tx.h:27
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
init
static int init
Definition: av_tx.c:47
checkasm.h
free_tx_refs
static void free_tx_refs(void)
Definition: av_tx.c:49
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AV_TX_FLOAT_FFT
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type of AVComplexFloat, AVComplexDouble or AVComplex...
Definition: tx.h:47
double_near_abs_eps_array
int double_near_abs_eps_array(const double *a, const double *b, double eps, unsigned len)
Definition: checkasm.c:381
check_lens
static const int check_lens[]
Definition: av_tx.c:42
error.h
EPS
#define EPS
Definition: av_tx.c:27
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:251
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
SCALE_NOOP
#define SCALE_NOOP(x)
Definition: av_tx.c:29
len
int len
Definition: vorbis_enc_data.h:426
AVComplexDouble
Definition: tx.h:31
stride
#define stride
Definition: h264pred_template.c:537
tx_refs
static AVTXContext * tx_refs[6][FF_ARRAY_ELEMS(check_lens)]
Definition: av_tx.c:46
checkasm_check_av_tx
void checkasm_check_av_tx(void)
Definition: av_tx.c:100
randomize_complex
#define randomize_complex(BUF, LEN, TYPE, SCALE)
Definition: av_tx.c:32
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:126
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
tx.h