FFmpeg
arls_template.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 #undef ZERO
20 #undef HALF
21 #undef ONE
22 #undef ftype
23 #undef SAMPLE_FORMAT
24 #if DEPTH == 32
25 #define SAMPLE_FORMAT float
26 #define ftype float
27 #define ONE 1.f
28 #define HALF 0.5f
29 #define ZERO 0.f
30 #else
31 #define SAMPLE_FORMAT double
32 #define ftype double
33 #define ONE 1.0
34 #define HALF 0.5
35 #define ZERO 0.0
36 #endif
37 
38 #define fn3(a,b) a##_##b
39 #define fn2(a,b) fn3(a,b)
40 #define fn(a) fn2(a, SAMPLE_FORMAT)
41 
43  ftype *coeffs, ftype *tmp, int *offset)
44 {
45  const int order = s->order;
46  ftype output;
47 
48  delay[*offset] = sample;
49 
50  memcpy(tmp, coeffs + order - *offset, order * sizeof(ftype));
51 
52 #if DEPTH == 32
53  output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
54 #else
55  output = s->fdsp->scalarproduct_double(delay, tmp, s->kernel_size);
56 #endif
57 
58  if (--(*offset) < 0)
59  *offset = order - 1;
60 
61  return output;
62 }
63 
64 static ftype fn(process_sample)(AudioRLSContext *s, ftype input, ftype desired, int ch)
65 {
66  ftype *coeffs = (ftype *)s->coeffs->extended_data[ch];
67  ftype *delay = (ftype *)s->delay->extended_data[ch];
68  ftype *gains = (ftype *)s->gains->extended_data[ch];
69  ftype *tmp = (ftype *)s->tmp->extended_data[ch];
70  ftype *u = (ftype *)s->u->extended_data[ch];
71  ftype *p = (ftype *)s->p->extended_data[ch];
72  ftype *dp = (ftype *)s->dp->extended_data[ch];
73  int *offsetp = (int *)s->offset->extended_data[ch];
74  const int kernel_size = s->kernel_size;
75  const int order = s->order;
76  const ftype lambda = s->lambda;
77  int offset = *offsetp;
78  ftype g = lambda;
79  ftype output, e;
80 
81  delay[offset + order] = input;
82 
83  output = fn(fir_sample)(s, input, delay, coeffs, tmp, offsetp);
84  e = desired - output;
85 
86  for (int i = 0, pos = offset; i < order; i++, pos++) {
87  const int ikernel_size = i * kernel_size;
88 
89  u[i] = ZERO;
90  for (int k = 0, pos = offset; k < order; k++, pos++)
91  u[i] += p[ikernel_size + k] * delay[pos];
92 
93  g += u[i] * delay[pos];
94  }
95 
96  g = ONE / g;
97 
98  for (int i = 0; i < order; i++) {
99  const int ikernel_size = i * kernel_size;
100 
101  gains[i] = u[i] * g;
102  coeffs[i] = coeffs[order + i] = coeffs[i] + gains[i] * e;
103  tmp[i] = ZERO;
104  for (int k = 0, pos = offset; k < order; k++, pos++)
105  tmp[i] += p[ikernel_size + k] * delay[pos];
106  }
107 
108  for (int i = 0; i < order; i++) {
109  const int ikernel_size = i * kernel_size;
110 
111  for (int k = 0; k < order; k++)
112  dp[ikernel_size + k] = gains[i] * tmp[k];
113  }
114 
115  for (int i = 0; i < order; i++) {
116  const int ikernel_size = i * kernel_size;
117 
118  for (int k = 0; k < order; k++)
119  p[ikernel_size + k] = (p[ikernel_size + k] - (dp[ikernel_size + k] + dp[kernel_size * k + i]) * HALF) * lambda;
120  }
121 
122  switch (s->output_mode) {
123  case IN_MODE: output = input; break;
124  case DESIRED_MODE: output = desired; break;
125  case OUT_MODE: output = desired - output; break;
126  case NOISE_MODE: output = input - output; break;
127  case ERROR_MODE: break;
128  }
129  return output;
130 }
131 
132 static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
133 {
134  AudioRLSContext *s = ctx->priv;
135  AVFrame *out = arg;
136  const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
137  const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
138 
139  for (int c = start; c < end; c++) {
140  const ftype *input = (const ftype *)s->frame[0]->extended_data[c];
141  const ftype *desired = (const ftype *)s->frame[1]->extended_data[c];
142  ftype *output = (ftype *)out->extended_data[c];
143 
144  for (int n = 0; n < out->nb_samples; n++) {
145  output[n] = fn(process_sample)(s, input[n], desired[n], c);
146  if (ctx->is_disabled)
147  output[n] = input[n];
148  }
149  }
150 
151  return 0;
152 }
out
FILE * out
Definition: movenc.c:55
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
fir_sample
static ftype fn() fir_sample(AudioRLSContext *s, ftype sample, ftype *delay, ftype *coeffs, ftype *tmp, int *offset)
Definition: arls_template.c:42
HALF
#define HALF
Definition: arls_template.c:34
ftype
#define ftype
Definition: arls_template.c:32
process_sample
static ftype fn() process_sample(AudioRLSContext *s, ftype input, ftype desired, int ch)
Definition: arls_template.c:64
ERROR_MODE
@ ERROR_MODE
Definition: af_aap.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
g
const char * g
Definition: vf_curves.c:128
fn
#define fn(a)
Definition: arls_template.c:40
ctx
AVFormatContext * ctx
Definition: movenc.c:49
DESIRED_MODE
@ DESIRED_MODE
Definition: af_aap.c:34
arg
const char * arg
Definition: jacosubdec.c:67
ZERO
#define ZERO
Definition: arls_template.c:35
ONE
#define ONE
Definition: arls_template.c:33
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
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
sample
#define sample
Definition: flacdsp_template.c:44
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
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
filter_channels
static int fn() filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: arls_template.c:132
NOISE_MODE
@ NOISE_MODE
Definition: af_aap.c:36
pos
unsigned int pos
Definition: spdifenc.c:414
IN_MODE
@ IN_MODE
Definition: af_aap.c:33
OUT_MODE
@ OUT_MODE
Definition: af_aap.c:35
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AudioRLSContext
Definition: af_arls.c:40