FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/afir_template.c
Date: 2026-09-19 14:27:34
Exec Total Coverage
Lines: 100 158 63.3%
Functions: 6 12 50.0%
Branches: 20 66 30.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2017 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/tx.h"
22 #include "avfilter.h"
23 #include "audio.h"
24
25 #undef ctype
26 #undef ftype
27 #undef SQRT
28 #undef HYPOT
29 #undef SAMPLE_FORMAT
30 #undef TX_TYPE
31 #undef FABS
32 #undef POW
33 #if DEPTH == 32
34 #define SAMPLE_FORMAT float
35 #define SQRT sqrtf
36 #define HYPOT hypotf
37 #define ctype AVComplexFloat
38 #define ftype float
39 #define TX_TYPE AV_TX_FLOAT_RDFT
40 #define FABS fabsf
41 #define POW powf
42 #else
43 #define SAMPLE_FORMAT double
44 #define SQRT sqrt
45 #define HYPOT hypot
46 #define ctype AVComplexDouble
47 #define ftype double
48 #define TX_TYPE AV_TX_DOUBLE_RDFT
49 #define FABS fabs
50 #define POW pow
51 #endif
52
53 #define fn3(a,b) a##_##b
54 #define fn2(a,b) fn3(a,b)
55 #define fn(a) fn2(a, SAMPLE_FORMAT)
56
57 4 static ftype fn(ir_gain)(AVFilterContext *ctx, AudioFIRContext *s,
58 int cur_nb_taps, const ftype *time)
59 {
60 4 ftype ch_gain, sum = 0;
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 if (s->ir_norm < 0.f) {
63 ch_gain = 1;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 } else if (s->ir_norm == 0.f) {
65 for (int i = 0; i < cur_nb_taps; i++)
66 sum += time[i];
67 ch_gain = 1. / sum;
68 } else {
69 4 ftype ir_norm = s->ir_norm;
70
71
2/2
✓ Branch 0 taken 4410 times.
✓ Branch 1 taken 2 times.
8824 for (int i = 0; i < cur_nb_taps; i++)
72 8820 sum += POW(FABS(time[i]), ir_norm);
73 4 ch_gain = 1. / POW(sum, 1. / ir_norm);
74 }
75
76 4 return ch_gain;
77 }
78
79 4 static void fn(ir_scale)(AVFilterContext *ctx, AudioFIRContext *s,
80 int cur_nb_taps, int ch,
81 ftype *time, ftype ch_gain)
82 {
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 if (!isfinite(ch_gain))
84 ch_gain = 1;
85
86
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (ch_gain != 1. || s->ir_gain != 1.) {
87 4 ftype gain = ch_gain * s->ir_gain;
88
89 4 av_log(ctx, AV_LOG_DEBUG, "ch%d gain %f\n", ch, gain);
90 #if DEPTH == 32
91 4 s->fdsp->vector_fmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 4));
92 #else
93 s->fdsp->vector_dmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 8));
94 #endif
95 }
96 4 }
97
98 4 static void fn(convert_channel)(AVFilterContext *ctx, AudioFIRContext *s, int ch,
99 AudioFIRSegment *seg, int coeff_partition, int selir)
100 {
101 4 const int coffset = coeff_partition * seg->coeff_size;
102 4 const int nb_taps = s->nb_taps[selir];
103 4 ftype *time = (ftype *)s->norm_ir[selir]->extended_data[ch];
104 4 ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
105 4 ftype *tempout = (ftype *)seg->tempout->extended_data[ch];
106 4 ctype *coeff = (ctype *)seg->coeff->extended_data[ch];
107 4 const int remaining = nb_taps - (seg->input_offset + coeff_partition * seg->part_size);
108 4 const int size = remaining >= seg->part_size ? seg->part_size : remaining;
109
110 4 memset(tempin + size, 0, sizeof(*tempin) * (seg->block_size - size));
111 4 memcpy(tempin, time + seg->input_offset + coeff_partition * seg->part_size,
112 size * sizeof(*tempin));
113 4 seg->ctx_fn(seg->ctx[ch], tempout, tempin, sizeof(*tempin));
114 4 memcpy(coeff + coffset, tempout, seg->coeff_size * sizeof(*coeff));
115
116 4 av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
117 4 av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", seg->nb_partitions);
118 4 av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", seg->part_size);
119 4 av_log(ctx, AV_LOG_DEBUG, "block size: %d\n", seg->block_size);
120 4 av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", seg->fft_length);
121 4 av_log(ctx, AV_LOG_DEBUG, "coeff_size: %d\n", seg->coeff_size);
122 4 av_log(ctx, AV_LOG_DEBUG, "input_size: %d\n", seg->input_size);
123 4 av_log(ctx, AV_LOG_DEBUG, "input_offset: %d\n", seg->input_offset);
124 4 }
125
126 8 static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
127 {
128
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 if ((nb_samples & 15) == 0 && nb_samples >= 8) {
129 #if DEPTH == 32
130 8 s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
131 #else
132 s->fdsp->vector_dmac_scalar(dst, src, 1.0, nb_samples);
133 #endif
134 } else {
135 for (int n = 0; n < nb_samples; n++)
136 dst[n] += src[n];
137 }
138 8 }
139
140 4 static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int ioffset, int offset, int selir)
141 {
142 4 AudioFIRContext *s = ctx->priv;
143 4 const ftype *in = (const ftype *)s->in->extended_data[ch] + ioffset;
144 4 ftype *blockout, *ptr = (ftype *)out->extended_data[ch] + offset;
145 4 const int min_part_size = s->min_part_size;
146 4 const int nb_samples = FFMIN(min_part_size, s->in->nb_samples - ioffset);
147 4 const int nb_segments = s->nb_segments[selir];
148 4 const float dry_gain = s->dry_gain;
149 4 const float wet_gain = s->wet_gain;
150
151
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
8 for (int segment = 0; segment < nb_segments; segment++) {
152 4 AudioFIRSegment *seg = &s->seg[selir][segment];
153 4 ftype *src = (ftype *)seg->input->extended_data[ch];
154 4 ftype *dst = (ftype *)seg->output->extended_data[ch];
155 4 ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
156 4 ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
157 4 ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
158 4 ftype *buf = (ftype *)seg->buffer->extended_data[ch];
159 4 int *output_offset = &seg->output_offset[ch];
160 4 const int nb_partitions = seg->nb_partitions;
161 4 const int input_offset = seg->input_offset;
162 4 const int part_size = seg->part_size;
163 int j;
164
165 4 seg->part_index[ch] = seg->part_index[ch] % nb_partitions;
166
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 if (dry_gain == 1.f) {
167 4 memcpy(src + input_offset, in, nb_samples * sizeof(*src));
168 } else if (min_part_size >= 8) {
169 #if DEPTH == 32
170 s->fdsp->vector_fmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 4));
171 #else
172 s->fdsp->vector_dmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 8));
173 #endif
174 } else {
175 ftype *src2 = src + input_offset;
176 for (int n = 0; n < nb_samples; n++)
177 src2[n] = in[n] * dry_gain;
178 }
179
180 4 output_offset[0] += min_part_size;
181
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 if (output_offset[0] >= part_size) {
182 4 output_offset[0] = 0;
183 } else {
184 memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
185
186 dst += output_offset[0];
187 fn(fir_fadd)(s, ptr, dst, nb_samples);
188 continue;
189 }
190
191 4 memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
192
193 4 blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
194 4 memset(tempin + part_size, 0, sizeof(*tempin) * (seg->block_size - part_size));
195 4 memcpy(tempin, src, sizeof(*src) * part_size);
196 4 seg->tx_fn(seg->tx[ch], blockout, tempin, sizeof(ftype));
197
198 4 j = seg->part_index[ch];
199
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < nb_partitions; i++) {
200 4 const int input_partition = j;
201 4 const int coeff_partition = i;
202 4 const int coffset = coeff_partition * seg->coeff_size;
203 4 const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
204 4 const ctype *coeff = ((const ctype *)seg->coeff->extended_data[ch]) + coffset;
205
206
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 if (j == 0)
207 4 j = nb_partitions;
208 4 j--;
209
210 #if DEPTH == 32
211 4 s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
212 #else
213 s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
214 #endif
215 }
216
217 4 seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
218
219 4 fn(fir_fadd)(s, buf, sumout, part_size);
220 4 memcpy(dst, buf, part_size * sizeof(*dst));
221 4 memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
222
223 4 fn(fir_fadd)(s, ptr, dst, nb_samples);
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
4 if (part_size != min_part_size)
226 memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
227
228 4 seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
229 }
230
231
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 if (wet_gain == 1.f)
232 4 return 0;
233
234 if (min_part_size >= 8) {
235 #if DEPTH == 32
236 s->fdsp->vector_fmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 4));
237 #else
238 s->fdsp->vector_dmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 8));
239 #endif
240 } else {
241 for (int n = 0; n < nb_samples; n++)
242 ptr[n] *= wet_gain;
243 }
244
245 return 0;
246 }
247
248 4 static void fn(fir_quantums)(AVFilterContext *ctx, AudioFIRContext *s, AVFrame *out,
249 int min_part_size, int ch, int offset,
250 int prev_selir, int selir)
251 {
252 4 const int nb_samples = FFMIN(min_part_size, s->in->nb_samples - offset);
253
254
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if (ctx->is_disabled || s->prev_is_disabled) {
255 const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
256 const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
257 const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
258 ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
259 ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
260 ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
261
262 if (ctx->is_disabled && !s->prev_is_disabled) {
263 memset(src0, 0, min_part_size * sizeof(ftype));
264 fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, selir);
265 for (int n = 0; n < nb_samples; n++)
266 dst[n] = xfade1[n] * src0[n] + xfade0[n] * in[n];
267 } else if (!ctx->is_disabled && s->prev_is_disabled) {
268 memset(src1, 0, min_part_size * sizeof(ftype));
269 fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
270 for (int n = 0; n < nb_samples; n++)
271 dst[n] = xfade1[n] * in[n] + xfade0[n] * src1[n];
272 } else {
273 memcpy(dst, in, sizeof(ftype) * nb_samples);
274 }
275
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 } else if (prev_selir != selir && s->loading[ch] != 0) {
276 const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
277 const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
278 ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
279 ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
280 ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
281
282 memset(src0, 0, min_part_size * sizeof(ftype));
283 memset(src1, 0, min_part_size * sizeof(ftype));
284
285 fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, prev_selir);
286 fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
287
288 if (s->loading[ch] > s->max_offset[selir]) {
289 for (int n = 0; n < nb_samples; n++)
290 dst[n] = xfade1[n] * src0[n] + xfade0[n] * src1[n];
291 s->loading[ch] = 0;
292 } else {
293 memcpy(dst, src0, nb_samples * sizeof(ftype));
294 }
295 } else {
296 4 fn(fir_quantum)(ctx, out, ch, offset, offset, selir);
297 }
298 4 }
299