FFmpeg
ac3dsp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Institue of Software Chinese Academy of Sciences (ISCAS).
3  * Copyright (c) 2024 Geoff Hill <geoff@geoffhill.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <stdint.h>
23 #include <string.h>
24 
25 #include "libavutil/mem.h"
26 #include "libavutil/mem_internal.h"
27 
28 #include "libavcodec/ac3dsp.h"
29 
30 #include "checkasm.h"
31 
32 #define randomize_exp(buf, len) \
33  do { \
34  int i; \
35  for (i = 0; i < len; i++) { \
36  buf[i] = (uint8_t)rnd(); \
37  } \
38  } while (0)
39 
40 #define randomize_i24(buf, len) \
41  do { \
42  int i; \
43  for (i = 0; i < len; i++) { \
44  int32_t v = (int32_t)rnd(); \
45  int32_t u = (v & 0xFFFFFF); \
46  buf[i] = (v < 0) ? -u : u; \
47  } \
48  } while (0)
49 
50 #define randomize_float(buf, len) \
51  do { \
52  int i; \
53  for (i = 0; i < len; i++) { \
54  float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f; \
55  buf[i] = f; \
56  } \
57  } while (0)
58 
60 #define MAX_COEFS 256
61 #define MAX_CTXT 6
62 #define EXP_SIZE (MAX_CTXT * MAX_COEFS)
63 
64  LOCAL_ALIGNED_16(uint8_t, src, [EXP_SIZE]);
65  LOCAL_ALIGNED_16(uint8_t, v1, [EXP_SIZE]);
66  LOCAL_ALIGNED_16(uint8_t, v2, [EXP_SIZE]);
67  int n;
68 
69  declare_func(void, uint8_t *, int, int);
70 
71  for (n = 0; n < MAX_CTXT; ++n) {
72  if (check_func(c->ac3_exponent_min, "ac3_exponent_min_reuse%d", n)) {
74 
75  memcpy(v1, src, EXP_SIZE);
76  memcpy(v2, src, EXP_SIZE);
77 
78  call_ref(v1, n, MAX_COEFS);
79  call_new(v2, n, MAX_COEFS);
80 
81  if (memcmp(v1, v2, EXP_SIZE) != 0)
82  fail();
83 
84  bench_new(v2, n, MAX_COEFS);
85  }
86  }
87 
88  report("ac3_exponent_min");
89 }
90 
92 #define MAX_EXPS 3072
94  LOCAL_ALIGNED_16(uint8_t, v1, [MAX_EXPS]);
95  LOCAL_ALIGNED_16(uint8_t, v2, [MAX_EXPS]);
96  int n;
97 
98  declare_func(void, uint8_t *, int32_t *, int);
99 
100  for (n = 512; n <= MAX_EXPS; n += 256) {
101  if (check_func(c->extract_exponents, "ac3_extract_exponents_n%d", n)) {
102  randomize_i24(src, n);
103 
104  call_ref(v1, src, n);
105  call_new(v2, src, n);
106 
107  if (memcmp(v1, v2, n) != 0)
108  fail();
109 
110  bench_new(v1, src, n);
111  }
112  }
113 
114  report("ac3_extract_exponents");
115 }
116 
118 #define BUF_SIZE 1024
119  LOCAL_ALIGNED_32(float, src, [BUF_SIZE]);
120 
121  declare_func(void, int32_t *, const float *, size_t);
122 
124 
125  if (check_func(c->float_to_fixed24, "float_to_fixed24")) {
128 
130  call_new(dst2, src, BUF_SIZE);
131 
132  if (memcmp(dst, dst2, BUF_SIZE) != 0)
133  fail();
134 
136  }
137 
138 
139  report("float_to_fixed24");
140 }
141 
143 #define ELEMS 240
146  LOCAL_ALIGNED_16(uint64_t, v1, [4]);
147  LOCAL_ALIGNED_16(uint64_t, v2, [4]);
148 
149  declare_func(void, int64_t[4], const int32_t *, const int32_t *, int);
150 
151  randomize_i24(lt, ELEMS);
152  randomize_i24(rt, ELEMS);
153 
154  if (check_func(c->sum_square_butterfly_int32,
155  "ac3_sum_square_bufferfly_int32")) {
156  call_ref(v1, lt, rt, ELEMS);
157  call_new(v2, lt, rt, ELEMS);
158 
159  if (memcmp(v1, v2, sizeof(int64_t[4])) != 0)
160  fail();
161 
162  bench_new(v2, lt, rt, ELEMS);
163  }
164 
165  report("ac3_sum_square_butterfly_int32");
166 }
167 
169  LOCAL_ALIGNED_32(float, lt, [ELEMS]);
170  LOCAL_ALIGNED_32(float, rt, [ELEMS]);
171  LOCAL_ALIGNED_16(float, v1, [4]);
172  LOCAL_ALIGNED_16(float, v2, [4]);
173 
174  declare_func(void, float[4], const float *, const float *, int);
175 
176  randomize_float(lt, ELEMS);
177  randomize_float(rt, ELEMS);
178 
179  if (check_func(c->sum_square_butterfly_float,
180  "ac3_sum_square_bufferfly_float")) {
181  call_ref(v1, lt, rt, ELEMS);
182  call_new(v2, lt, rt, ELEMS);
183 
184  if (!float_near_ulp_array(v1, v2, 11, 4))
185  fail();
186 
187  bench_new(v2, lt, rt, ELEMS);
188  }
189 
190  report("ac3_sum_square_butterfly_float");
191 }
192 
194 {
196  ff_ac3dsp_init(&c);
197 
203 }
MAX_COEFS
#define MAX_COEFS
mem_internal.h
check_ac3_exponent_min
static void check_ac3_exponent_min(AC3DSPContext *c)
Definition: ac3dsp.c:59
int64_t
long long int64_t
Definition: coverity.c:34
AC3DSPContext
Definition: ac3dsp.h:34
EXP_SIZE
#define EXP_SIZE
ELEMS
#define ELEMS
check_func
#define check_func(func,...)
Definition: checkasm.h:180
BUF_SIZE
#define BUF_SIZE
check_ac3_extract_exponents
static void check_ac3_extract_exponents(AC3DSPContext *c)
Definition: ac3dsp.c:91
check_ac3_sum_square_butterfly_float
static void check_ac3_sum_square_butterfly_float(AC3DSPContext *c)
Definition: ac3dsp.c:168
call_ref
#define call_ref(...)
Definition: checkasm.h:195
ff_ac3dsp_init
av_cold void ff_ac3dsp_init(AC3DSPContext *c)
Definition: ac3dsp.c:377
fail
#define fail()
Definition: checkasm.h:189
checkasm.h
check_ac3_sum_square_butterfly_int32
static void check_ac3_sum_square_butterfly_int32(AC3DSPContext *c)
Definition: ac3dsp.c:142
LOCAL_ALIGNED_16
#define LOCAL_ALIGNED_16(t, v,...)
Definition: mem_internal.h:124
call_new
#define call_new(...)
Definition: checkasm.h:298
randomize_exp
#define randomize_exp(buf, len)
Definition: ac3dsp.c:32
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:126
ac3dsp.h
MAX_CTXT
#define MAX_CTXT
MAX_EXPS
#define MAX_EXPS
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
report
#define report
Definition: checkasm.h:192
randomize_i24
#define randomize_i24(buf, len)
Definition: ac3dsp.c:40
bench_new
#define bench_new(...)
Definition: checkasm.h:369
checkasm_check_ac3dsp
void checkasm_check_ac3dsp(void)
Definition: ac3dsp.c:193
check_float_to_fixed24
static void check_float_to_fixed24(AC3DSPContext *c)
Definition: ac3dsp.c:117
float_near_ulp_array
int float_near_ulp_array(const float *a, const float *b, unsigned max_ulp, unsigned len)
Definition: checkasm.c:437
randomize_float
#define randomize_float(buf, len)
Definition: ac3dsp.c:50
mem.h
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:184
int32_t
int32_t
Definition: audioconvert.c:56
src
#define src
Definition: vp8dsp.c:248