FFmpeg
afir_template.c
Go to the documentation of this file.
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 "internal.h"
24 #include "audio.h"
25 
26 #undef ctype
27 #undef ftype
28 #undef SQRT
29 #undef HYPOT
30 #undef SAMPLE_FORMAT
31 #undef TX_TYPE
32 #undef FABS
33 #undef POW
34 #if DEPTH == 32
35 #define SAMPLE_FORMAT float
36 #define SQRT sqrtf
37 #define HYPOT hypotf
38 #define ctype AVComplexFloat
39 #define ftype float
40 #define TX_TYPE AV_TX_FLOAT_RDFT
41 #define FABS fabsf
42 #define POW powf
43 #else
44 #define SAMPLE_FORMAT double
45 #define SQRT sqrt
46 #define HYPOT hypot
47 #define ctype AVComplexDouble
48 #define ftype double
49 #define TX_TYPE AV_TX_DOUBLE_RDFT
50 #define FABS fabs
51 #define POW pow
52 #endif
53 
54 #define fn3(a,b) a##_##b
55 #define fn2(a,b) fn3(a,b)
56 #define fn(a) fn2(a, SAMPLE_FORMAT)
57 
59  int cur_nb_taps, const ftype *time)
60 {
61  ftype ch_gain, sum = 0;
62 
63  if (s->ir_norm < 0.f) {
64  ch_gain = 1;
65  } else if (s->ir_norm == 0.f) {
66  for (int i = 0; i < cur_nb_taps; i++)
67  sum += time[i];
68  ch_gain = 1. / sum;
69  } else {
70  ftype ir_norm = s->ir_norm;
71 
72  for (int i = 0; i < cur_nb_taps; i++)
73  sum += POW(FABS(time[i]), ir_norm);
74  ch_gain = 1. / POW(sum, 1. / ir_norm);
75  }
76 
77  return ch_gain;
78 }
79 
81  int cur_nb_taps, int ch,
82  ftype *time, ftype ch_gain)
83 {
84  if (ch_gain != 1. || s->ir_gain != 1.) {
85  ftype gain = ch_gain * s->ir_gain;
86 
87  av_log(ctx, AV_LOG_DEBUG, "ch%d gain %f\n", ch, gain);
88 #if DEPTH == 32
89  s->fdsp->vector_fmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 4));
90 #else
91  s->fdsp->vector_dmul_scalar(time, time, gain, FFALIGN(cur_nb_taps, 8));
92 #endif
93  }
94 }
95 
97  AudioFIRSegment *seg, int coeff_partition, int selir)
98 {
99  const int coffset = coeff_partition * seg->coeff_size;
100  const int nb_taps = s->nb_taps[selir];
101  ftype *time = (ftype *)s->norm_ir[selir]->extended_data[ch];
102  ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
103  ftype *tempout = (ftype *)seg->tempout->extended_data[ch];
104  ctype *coeff = (ctype *)seg->coeff->extended_data[ch];
105  const int remaining = nb_taps - (seg->input_offset + coeff_partition * seg->part_size);
106  const int size = remaining >= seg->part_size ? seg->part_size : remaining;
107 
108  memset(tempin + size, 0, sizeof(*tempin) * (seg->block_size - size));
109  memcpy(tempin, time + seg->input_offset + coeff_partition * seg->part_size,
110  size * sizeof(*tempin));
111  seg->ctx_fn(seg->ctx[ch], tempout, tempin, sizeof(*tempin));
112  memcpy(coeff + coffset, tempout, seg->coeff_size * sizeof(*coeff));
113 
114  av_log(ctx, AV_LOG_DEBUG, "channel: %d\n", ch);
115  av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", seg->nb_partitions);
116  av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", seg->part_size);
117  av_log(ctx, AV_LOG_DEBUG, "block size: %d\n", seg->block_size);
118  av_log(ctx, AV_LOG_DEBUG, "fft_length: %d\n", seg->fft_length);
119  av_log(ctx, AV_LOG_DEBUG, "coeff_size: %d\n", seg->coeff_size);
120  av_log(ctx, AV_LOG_DEBUG, "input_size: %d\n", seg->input_size);
121  av_log(ctx, AV_LOG_DEBUG, "input_offset: %d\n", seg->input_offset);
122 }
123 
124 static void fn(fir_fadd)(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
125 {
126  if ((nb_samples & 15) == 0 && nb_samples >= 8) {
127 #if DEPTH == 32
128  s->fdsp->vector_fmac_scalar(dst, src, 1.f, nb_samples);
129 #else
130  s->fdsp->vector_dmac_scalar(dst, src, 1.0, nb_samples);
131 #endif
132  } else {
133  for (int n = 0; n < nb_samples; n++)
134  dst[n] += src[n];
135  }
136 }
137 
138 static int fn(fir_quantum)(AVFilterContext *ctx, AVFrame *out, int ch, int ioffset, int offset, int selir)
139 {
140  AudioFIRContext *s = ctx->priv;
141  const ftype *in = (const ftype *)s->in->extended_data[ch] + ioffset;
142  ftype *blockout, *ptr = (ftype *)out->extended_data[ch] + offset;
143  const int min_part_size = s->min_part_size;
144  const int nb_samples = FFMIN(min_part_size, out->nb_samples - offset);
145  const int nb_segments = s->nb_segments[selir];
146  const float dry_gain = s->dry_gain;
147  const float wet_gain = s->wet_gain;
148 
149  for (int segment = 0; segment < nb_segments; segment++) {
150  AudioFIRSegment *seg = &s->seg[selir][segment];
151  ftype *src = (ftype *)seg->input->extended_data[ch];
152  ftype *dst = (ftype *)seg->output->extended_data[ch];
153  ftype *sumin = (ftype *)seg->sumin->extended_data[ch];
154  ftype *sumout = (ftype *)seg->sumout->extended_data[ch];
155  ftype *tempin = (ftype *)seg->tempin->extended_data[ch];
156  ftype *buf = (ftype *)seg->buffer->extended_data[ch];
157  int *output_offset = &seg->output_offset[ch];
158  const int nb_partitions = seg->nb_partitions;
159  const int input_offset = seg->input_offset;
160  const int part_size = seg->part_size;
161  int j;
162 
163  seg->part_index[ch] = seg->part_index[ch] % nb_partitions;
164  if (dry_gain == 1.f) {
165  memcpy(src + input_offset, in, nb_samples * sizeof(*src));
166  } else if (min_part_size >= 8) {
167 #if DEPTH == 32
168  s->fdsp->vector_fmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 4));
169 #else
170  s->fdsp->vector_dmul_scalar(src + input_offset, in, dry_gain, FFALIGN(nb_samples, 8));
171 #endif
172  } else {
173  ftype *src2 = src + input_offset;
174  for (int n = 0; n < nb_samples; n++)
175  src2[n] = in[n] * dry_gain;
176  }
177 
178  output_offset[0] += min_part_size;
179  if (output_offset[0] >= part_size) {
180  output_offset[0] = 0;
181  } else {
182  memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
183 
184  dst += output_offset[0];
185  fn(fir_fadd)(s, ptr, dst, nb_samples);
186  continue;
187  }
188 
189  memset(sumin, 0, sizeof(*sumin) * seg->fft_length);
190 
191  blockout = (ftype *)seg->blockout->extended_data[ch] + seg->part_index[ch] * seg->block_size;
192  memset(tempin + part_size, 0, sizeof(*tempin) * (seg->block_size - part_size));
193  memcpy(tempin, src, sizeof(*src) * part_size);
194  seg->tx_fn(seg->tx[ch], blockout, tempin, sizeof(ftype));
195 
196  j = seg->part_index[ch];
197  for (int i = 0; i < nb_partitions; i++) {
198  const int input_partition = j;
199  const int coeff_partition = i;
200  const int coffset = coeff_partition * seg->coeff_size;
201  const ftype *blockout = (const ftype *)seg->blockout->extended_data[ch] + input_partition * seg->block_size;
202  const ctype *coeff = ((const ctype *)seg->coeff->extended_data[ch]) + coffset;
203 
204  if (j == 0)
205  j = nb_partitions;
206  j--;
207 
208 #if DEPTH == 32
209  s->afirdsp.fcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
210 #else
211  s->afirdsp.dcmul_add(sumin, blockout, (const ftype *)coeff, part_size);
212 #endif
213  }
214 
215  seg->itx_fn(seg->itx[ch], sumout, sumin, sizeof(ctype));
216 
217  fn(fir_fadd)(s, buf, sumout, part_size);
218  memcpy(dst, buf, part_size * sizeof(*dst));
219  memcpy(buf, sumout + part_size, part_size * sizeof(*buf));
220 
221  fn(fir_fadd)(s, ptr, dst, nb_samples);
222 
223  if (part_size != min_part_size)
224  memmove(src, src + min_part_size, (seg->input_size - min_part_size) * sizeof(*src));
225 
226  seg->part_index[ch] = (seg->part_index[ch] + 1) % nb_partitions;
227  }
228 
229  if (wet_gain == 1.f)
230  return 0;
231 
232  if (min_part_size >= 8) {
233 #if DEPTH == 32
234  s->fdsp->vector_fmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 4));
235 #else
236  s->fdsp->vector_dmul_scalar(ptr, ptr, wet_gain, FFALIGN(nb_samples, 8));
237 #endif
238  } else {
239  for (int n = 0; n < nb_samples; n++)
240  ptr[n] *= wet_gain;
241  }
242 
243  return 0;
244 }
245 
247  int min_part_size, int ch, int offset,
248  int prev_selir, int selir)
249 {
250  if (ctx->is_disabled || s->prev_is_disabled) {
251  const ftype *in = (const ftype *)s->in->extended_data[ch] + offset;
252  const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
253  const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
254  ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
255  ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
256  ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
257 
258  if (ctx->is_disabled && !s->prev_is_disabled) {
259  memset(src0, 0, min_part_size * sizeof(ftype));
260  fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, selir);
261  for (int n = 0; n < min_part_size; n++)
262  dst[n] = xfade1[n] * src0[n] + xfade0[n] * in[n];
263  } else if (!ctx->is_disabled && s->prev_is_disabled) {
264  memset(src1, 0, min_part_size * sizeof(ftype));
265  fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
266  for (int n = 0; n < min_part_size; n++)
267  dst[n] = xfade1[n] * in[n] + xfade0[n] * src1[n];
268  } else {
269  memcpy(dst, in, sizeof(ftype) * min_part_size);
270  }
271  } else if (prev_selir != selir && s->loading[ch] != 0) {
272  const ftype *xfade0 = (const ftype *)s->xfade[0]->extended_data[ch];
273  const ftype *xfade1 = (const ftype *)s->xfade[1]->extended_data[ch];
274  ftype *src0 = (ftype *)s->fadein[0]->extended_data[ch];
275  ftype *src1 = (ftype *)s->fadein[1]->extended_data[ch];
276  ftype *dst = ((ftype *)out->extended_data[ch]) + offset;
277 
278  memset(src0, 0, min_part_size * sizeof(ftype));
279  memset(src1, 0, min_part_size * sizeof(ftype));
280 
281  fn(fir_quantum)(ctx, s->fadein[0], ch, offset, 0, prev_selir);
282  fn(fir_quantum)(ctx, s->fadein[1], ch, offset, 0, selir);
283 
284  if (s->loading[ch] > s->max_offset[selir]) {
285  for (int n = 0; n < min_part_size; n++)
286  dst[n] = xfade1[n] * src0[n] + xfade0[n] * src1[n];
287  s->loading[ch] = 0;
288  } else {
289  memcpy(dst, src0, min_part_size * sizeof(ftype));
290  }
291  } else {
292  fn(fir_quantum)(ctx, out, ch, offset, offset, selir);
293  }
294 }
convert_channel
static void fn() convert_channel(AVFilterContext *ctx, AudioFIRContext *s, int ch, AudioFIRSegment *seg, int coeff_partition, int selir)
Definition: afir_template.c:96
AudioFIRSegment::block_size
int block_size
Definition: af_afir.h:36
out
FILE * out
Definition: movenc.c:54
ctype
#define ctype
Definition: afir_template.c:47
src1
const pixel * src1
Definition: h264pred_template.c:421
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AudioFIRSegment::buffer
AVFrame * buffer
Definition: af_afir.h:50
AudioFIRSegment::input_offset
int input_offset
Definition: af_afir.h:40
AudioFIRSegment::tx_fn
av_tx_fn tx_fn
Definition: af_afir.h:56
AudioFIRSegment::part_size
int part_size
Definition: af_afir.h:35
AudioFIRSegment::input_size
int input_size
Definition: af_afir.h:39
FABS
#define FABS
Definition: afir_template.c:50
AudioFIRSegment::coeff
AVFrame * coeff
Definition: af_afir.h:51
fn
#define fn(a)
Definition: afir_template.c:56
AudioFIRSegment::blockout
AVFrame * blockout
Definition: af_afir.h:47
AudioFIRSegment
Definition: af_afir.h:33
AudioFIRSegment::tx
AVTXContext ** tx
Definition: af_afir.h:55
ftype
#define ftype
Definition: afir_template.c:48
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AudioFIRSegment::itx_fn
av_tx_fn itx_fn
Definition: af_afir.h:56
fir_quantums
static void fn() fir_quantums(AVFilterContext *ctx, AudioFIRContext *s, AVFrame *out, int min_part_size, int ch, int offset, int prev_selir, int selir)
Definition: afir_template.c:246
AudioFIRSegment::output
AVFrame * output
Definition: af_afir.h:53
f
f
Definition: af_crystalizer.c:121
size
int size
Definition: twinvq_data.h:10344
AudioFIRSegment::sumin
AVFrame * sumin
Definition: af_afir.h:45
fir_fadd
static void fn() fir_fadd(AudioFIRContext *s, ftype *dst, const ftype *src, int nb_samples)
Definition: afir_template.c:124
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
POW
#define POW
Definition: afir_template.c:51
AudioFIRSegment::tempin
AVFrame * tempin
Definition: af_afir.h:48
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AudioFIRSegment::input
AVFrame * input
Definition: af_afir.h:52
AudioFIRSegment::coeff_size
int coeff_size
Definition: af_afir.h:38
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:405
src2
const pixel * src2
Definition: h264pred_template.c:422
AudioFIRSegment::nb_partitions
int nb_partitions
Definition: af_afir.h:34
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AudioFIRSegment::itx
AVTXContext ** itx
Definition: af_afir.h:55
ir_gain
static ftype fn() ir_gain(AVFilterContext *ctx, AudioFIRContext *s, int cur_nb_taps, const ftype *time)
Definition: afir_template.c:58
AudioFIRSegment::fft_length
int fft_length
Definition: af_afir.h:37
AudioFIRSegment::sumout
AVFrame * sumout
Definition: af_afir.h:46
AudioFIRContext
Definition: af_afir.h:59
avfilter.h
segment
Definition: hls.c:76
src0
const pixel *const src0
Definition: h264pred_template.c:420
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
audio.h
ir_scale
static void fn() ir_scale(AVFilterContext *ctx, AudioFIRContext *s, int cur_nb_taps, int ch, ftype *time, ftype ch_gain)
Definition: afir_template.c:80
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AudioFIRSegment::output_offset
int * output_offset
Definition: af_afir.h:42
tx.h
fir_quantum
static int fn() fir_quantum(AVFilterContext *ctx, AVFrame *out, int ch, int ioffset, int offset, int selir)
Definition: afir_template.c:138
AudioFIRSegment::part_index
int * part_index
Definition: af_afir.h:43