FFmpeg
af_crystalizer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 The FFmpeg Project
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 
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26 
27 typedef struct CrystalizerContext {
28  const AVClass *class;
29  float mult;
30  int clip;
32  int (*filter[2][2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
34 
35 #define OFFSET(x) offsetof(CrystalizerContext, x)
36 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
37 
38 static const AVOption crystalizer_options[] = {
39  { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0},-10, 10, A },
40  { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
41  { NULL }
42 };
43 
44 AVFILTER_DEFINE_CLASS(crystalizer);
45 
46 typedef struct ThreadData {
47  void **d;
48  void **p;
49  const void **s;
50  int nb_samples;
51  int channels;
52  float mult;
53 } ThreadData;
54 
56  int jobnr, int nb_jobs,
57  int inverse, int clip)
58 {
59  ThreadData *td = arg;
60  void **d = td->d;
61  void **p = td->p;
62  const void **s = td->s;
63  const int nb_samples = td->nb_samples;
64  const int channels = td->channels;
65  const float mult = td->mult;
66  const float scale = 1.f / (-mult + 1.f);
67  const int start = (channels * jobnr) / nb_jobs;
68  const int end = (channels * (jobnr+1)) / nb_jobs;
69  float *prv = p[0];
70  int n, c;
71 
72  for (c = start; c < end; c++) {
73  const float *src = s[0];
74  float *dst = d[0];
75 
76  for (n = 0; n < nb_samples; n++) {
77  float current = src[c];
78 
79  if (inverse) {
80  dst[c] = (current - prv[c] * mult) * scale;
81  prv[c] = dst[c];
82  } else {
83  dst[c] = current + (current - prv[c]) * mult;
84  prv[c] = current;
85  }
86  if (clip) {
87  dst[c] = av_clipf(dst[c], -1.f, 1.f);
88  }
89 
90  dst += channels;
91  src += channels;
92  }
93  }
94 
95  return 0;
96 }
97 
99  int jobnr, int nb_jobs,
100  int inverse, int clip)
101 {
102  ThreadData *td = arg;
103  void **d = td->d;
104  void **p = td->p;
105  const void **s = td->s;
106  const int nb_samples = td->nb_samples;
107  const int channels = td->channels;
108  const double mult = td->mult;
109  const double scale = 1.0 / (-mult + 1.0);
110  const int start = (channels * jobnr) / nb_jobs;
111  const int end = (channels * (jobnr+1)) / nb_jobs;
112  double *prv = p[0];
113  int n, c;
114 
115  for (c = start; c < end; c++) {
116  const double *src = s[0];
117  double *dst = d[0];
118 
119  for (n = 0; n < nb_samples; n++) {
120  double current = src[c];
121 
122  if (inverse) {
123  dst[c] = (current - prv[c] * mult) * scale;
124  prv[c] = dst[c];
125  } else {
126  dst[c] = current + (current - prv[c]) * mult;
127  prv[c] = current;
128  }
129  if (clip) {
130  dst[c] = av_clipd(dst[c], -1., 1.);
131  }
132 
133  dst += channels;
134  src += channels;
135  }
136  }
137 
138  return 0;
139 }
140 
142  int jobnr, int nb_jobs,
143  int inverse, int clip)
144 {
145  ThreadData *td = arg;
146  void **d = td->d;
147  void **p = td->p;
148  const void **s = td->s;
149  const int nb_samples = td->nb_samples;
150  const int channels = td->channels;
151  const float mult = td->mult;
152  const float scale = 1.f / (-mult + 1.f);
153  const int start = (channels * jobnr) / nb_jobs;
154  const int end = (channels * (jobnr+1)) / nb_jobs;
155  int n, c;
156 
157  for (c = start; c < end; c++) {
158  const float *src = s[c];
159  float *dst = d[c];
160  float *prv = p[c];
161 
162  for (n = 0; n < nb_samples; n++) {
163  float current = src[n];
164 
165  if (inverse) {
166  dst[n] = (current - prv[0] * mult) * scale;
167  prv[0] = dst[n];
168  } else {
169  dst[n] = current + (current - prv[0]) * mult;
170  prv[0] = current;
171  }
172  if (clip) {
173  dst[n] = av_clipf(dst[n], -1.f, 1.f);
174  }
175  }
176  }
177 
178  return 0;
179 }
180 
182  int jobnr, int nb_jobs,
183  int inverse, int clip)
184 {
185  ThreadData *td = arg;
186  void **d = td->d;
187  void **p = td->p;
188  const void **s = td->s;
189  const int nb_samples = td->nb_samples;
190  const int channels = td->channels;
191  const double mult = td->mult;
192  const double scale = 1.0 / (-mult + 1.0);
193  const int start = (channels * jobnr) / nb_jobs;
194  const int end = (channels * (jobnr+1)) / nb_jobs;
195  int n, c;
196 
197  for (c = start; c < end; c++) {
198  const double *src = s[c];
199  double *dst = d[c];
200  double *prv = p[c];
201 
202  for (n = 0; n < nb_samples; n++) {
203  double current = src[n];
204 
205  if (inverse) {
206  dst[n] = (current - prv[0] * mult) * scale;
207  prv[0] = dst[n];
208  } else {
209  dst[n] = current + (current - prv[0]) * mult;
210  prv[0] = current;
211  }
212  if (clip) {
213  dst[n] = av_clipd(dst[n], -1., 1.);
214  }
215  }
216  }
217 
218  return 0;
219 }
220 
221 #define filters(fmt, inverse, clip, i, c) \
222 static int filter_## inverse ##_## fmt ##_## clip(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
223 { \
224  return filter_## fmt(ctx, arg, jobnr, nb_jobs, i, c); \
225 }
226 
227 filters(flt, inverse, noclip, 1, 0)
228 filters(flt, inverse, clip, 1, 1)
229 filters(flt, noinverse, noclip, 0, 0)
230 filters(flt, noinverse, clip, 0, 1)
231 
232 filters(fltp, inverse, noclip, 1, 0)
233 filters(fltp, inverse, clip, 1, 1)
234 filters(fltp, noinverse, noclip, 0, 0)
235 filters(fltp, noinverse, clip, 0, 1)
236 
237 filters(dbl, inverse, noclip, 1, 0)
238 filters(dbl, inverse, clip, 1, 1)
239 filters(dbl, noinverse, noclip, 0, 0)
240 filters(dbl, noinverse, clip, 0, 1)
241 
242 filters(dblp, inverse, noclip, 1, 0)
243 filters(dblp, inverse, clip, 1, 1)
244 filters(dblp, noinverse, noclip, 0, 0)
245 filters(dblp, noinverse, clip, 0, 1)
246 
247 static int config_input(AVFilterLink *inlink)
248 {
249  AVFilterContext *ctx = inlink->dst;
250  CrystalizerContext *s = ctx->priv;
251 
252  switch (inlink->format) {
253  case AV_SAMPLE_FMT_FLT:
254  s->filter[0][0] = filter_inverse_flt_noclip;
255  s->filter[1][0] = filter_noinverse_flt_noclip;
256  s->filter[0][1] = filter_inverse_flt_clip;
257  s->filter[1][1] = filter_noinverse_flt_clip;
258  break;
259  case AV_SAMPLE_FMT_FLTP:
260  s->filter[0][0] = filter_inverse_fltp_noclip;
261  s->filter[1][0] = filter_noinverse_fltp_noclip;
262  s->filter[0][1] = filter_inverse_fltp_clip;
263  s->filter[1][1] = filter_noinverse_fltp_clip;
264  break;
265  case AV_SAMPLE_FMT_DBL:
266  s->filter[0][0] = filter_inverse_dbl_noclip;
267  s->filter[1][0] = filter_noinverse_dbl_noclip;
268  s->filter[0][1] = filter_inverse_dbl_clip;
269  s->filter[1][1] = filter_noinverse_dbl_clip;
270  break;
271  case AV_SAMPLE_FMT_DBLP:
272  s->filter[0][0] = filter_inverse_dblp_noclip;
273  s->filter[1][0] = filter_noinverse_dblp_noclip;
274  s->filter[0][1] = filter_inverse_dblp_clip;
275  s->filter[1][1] = filter_noinverse_dblp_clip;
276  break;
277  default:
278  return AVERROR_BUG;
279  }
280 
281  return 0;
282 }
283 
285 {
286  AVFilterContext *ctx = inlink->dst;
287  AVFilterLink *outlink = ctx->outputs[0];
288  CrystalizerContext *s = ctx->priv;
289  AVFrame *out;
290  ThreadData td;
291 
292  if (!s->prev) {
293  s->prev = ff_get_audio_buffer(inlink, 1);
294  if (!s->prev) {
295  av_frame_free(&in);
296  return AVERROR(ENOMEM);
297  }
298  }
299 
300  if (av_frame_is_writable(in)) {
301  out = in;
302  } else {
303  out = ff_get_audio_buffer(outlink, in->nb_samples);
304  if (!out) {
305  av_frame_free(&in);
306  return AVERROR(ENOMEM);
307  }
309  }
310 
311  td.d = (void **)out->extended_data;
312  td.s = (const void **)in->extended_data;
313  td.p = (void **)s->prev->extended_data;
314  td.nb_samples = in->nb_samples;
315  td.channels = in->channels;
316  td.mult = ctx->is_disabled ? 0.f : s->mult;
317  ff_filter_execute(ctx, s->filter[td.mult >= 0.f][s->clip], &td, NULL,
319 
320  if (out != in)
321  av_frame_free(&in);
322 
323  return ff_filter_frame(outlink, out);
324 }
325 
327 {
328  CrystalizerContext *s = ctx->priv;
329 
330  av_frame_free(&s->prev);
331 }
332 
333 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
334  char *res, int res_len, int flags)
335 {
336  int ret;
337 
338  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
339  if (ret < 0)
340  return ret;
341 
342  return config_input(ctx->inputs[0]);
343 }
344 
345 static const AVFilterPad inputs[] = {
346  {
347  .name = "default",
348  .type = AVMEDIA_TYPE_AUDIO,
349  .filter_frame = filter_frame,
350  .config_props = config_input,
351  },
352 };
353 
354 static const AVFilterPad outputs[] = {
355  {
356  .name = "default",
357  .type = AVMEDIA_TYPE_AUDIO,
358  },
359 };
360 
362  .name = "crystalizer",
363  .description = NULL_IF_CONFIG_SMALL("Simple audio noise sharpening filter."),
364  .priv_size = sizeof(CrystalizerContext),
365  .priv_class = &crystalizer_class,
366  .uninit = uninit,
371  .process_command = process_command,
374 };
outputs
static const AVFilterPad outputs[]
Definition: af_crystalizer.c:354
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:88
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
td
#define td
Definition: regdef.h:70
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
CrystalizerContext::filter
int(* filter[2][2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_crystalizer.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
CrystalizerContext::clip
int clip
Definition: af_crystalizer.c:30
AVOption
AVOption.
Definition: opt.h:247
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_crystalizer.c:326
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ThreadData::channels
int channels
Definition: af_asoftclip.c:402
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(crystalizer)
formats.h
filter_dblp
static av_always_inline int filter_dblp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs, int inverse, int clip)
Definition: af_crystalizer.c:181
OFFSET
#define OFFSET(x)
Definition: af_crystalizer.c:35
filter_dbl
static av_always_inline int filter_dbl(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs, int inverse, int clip)
Definition: af_crystalizer.c:98
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1377
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
filters
#define filters(fmt, inverse, clip, i, c)
Definition: af_crystalizer.c:221
mult
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:56
av_cold
#define av_cold
Definition: attributes.h:90
filter_flt
static av_always_inline int filter_flt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs, int inverse, int clip)
Definition: af_crystalizer.c:55
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:592
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
CrystalizerContext::mult
float mult
Definition: af_crystalizer.c:29
ThreadData::nb_samples
int nb_samples
Definition: af_asoftclip.c:401
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:33
f
#define f(width, name)
Definition: cbs_vp9.c:255
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
filter_fltp
static av_always_inline int filter_fltp(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs, int inverse, int clip)
Definition: af_crystalizer.c:141
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ThreadData::mult
float mult
Definition: af_crystalizer.c:52
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_crystalizer.c:333
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_acontrast.c:121
av_clipf
#define av_clipf
Definition: common.h:144
src
#define src
Definition: vp8dsp.c:255
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
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_crystalizer.c:284
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_clipd
#define av_clipd
Definition: common.h:147
CrystalizerContext::prev
AVFrame * prev
Definition: af_crystalizer.c:31
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:882
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
ThreadData::d
void ** d
Definition: af_crystalizer.c:47
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
inputs
static const AVFilterPad inputs[]
Definition: af_crystalizer.c:345
crystalizer_options
static const AVOption crystalizer_options[]
Definition: af_crystalizer.c:38
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
CrystalizerContext
Definition: af_crystalizer.c:27
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
A
#define A
Definition: af_crystalizer.c:36
channel_layout.h
inverse
static int inverse(AudioFWTDNContext *s, double **in, int *in_length, double *out, int out_length, int ch, uint64_t sn)
Definition: af_afwtdn.c:762
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
ThreadData::s
const void ** s
Definition: af_crystalizer.c:49
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
audio.h
ThreadData::p
void ** p
Definition: af_crystalizer.c:48
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
d
d
Definition: ffmpeg_filter.c:156
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:138
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
ff_af_crystalizer
const AVFilter ff_af_crystalizer
Definition: af_crystalizer.c:361
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
int
int
Definition: ffmpeg_filter.c:156
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
clip
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.
Definition: vf_lut.c:159
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:179