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 
26 typedef struct CrystalizerContext {
27  const AVClass *class;
28  float mult;
29  int clip;
31  int (*filter[2][2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
33 
34 #define OFFSET(x) offsetof(CrystalizerContext, x)
35 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
36 
37 static const AVOption crystalizer_options[] = {
38  { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0},-10, 10, A },
39  { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
40  { NULL }
41 };
42 
43 AVFILTER_DEFINE_CLASS(crystalizer);
44 
45 typedef struct ThreadData {
46  void **d;
47  void **p;
48  const void **s;
49  int nb_samples;
50  int channels;
51  float mult;
52 } ThreadData;
53 
54 #define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed) \
55 static int filter_## inverse ##_## fmt ##_## clp(AVFilterContext *ctx, \
56  void *arg, int jobnr,\
57  int nb_jobs) \
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 type mult = td->mult; \
66  const type scale = one / (-mult + one); \
67  const int start = (channels * jobnr) / nb_jobs; \
68  const int end = (channels * (jobnr+1)) / nb_jobs; \
69  \
70  if (packed) { \
71  type *prv = p[0]; \
72  for (int c = start; c < end; c++) { \
73  const type *src = s[0]; \
74  type *dst = d[0]; \
75  \
76  for (int n = 0; n < nb_samples; n++) { \
77  type current = src[c]; \
78  \
79  if (inverset) { \
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] = clip_fn(dst[c], -one, one); \
88  } \
89  \
90  dst += channels; \
91  src += channels; \
92  } \
93  } \
94  } else { \
95  for (int c = start; c < end; c++) { \
96  const type *src = s[c]; \
97  type *dst = d[c]; \
98  type *prv = p[c]; \
99  \
100  for (int n = 0; n < nb_samples; n++) { \
101  type current = src[n]; \
102  \
103  if (inverset) { \
104  dst[n] = (current - prv[0] * mult) * scale; \
105  prv[0] = dst[n]; \
106  } else { \
107  dst[n] = current + (current - prv[0]) * mult; \
108  prv[0] = current; \
109  } \
110  if (clip) { \
111  dst[n] = clip_fn(dst[n], -one, one); \
112  } \
113  } \
114  } \
115  } \
116  \
117  return 0; \
118 }
119 
120 filters(flt, float, inverse, noclip, 1, 0, 1.f, av_clipf, 1)
121 filters(flt, float, inverse, clip, 1, 1, 1.f, av_clipf, 1)
122 filters(flt, float, noinverse, noclip, 0, 0, 1.f, av_clipf, 1)
123 filters(flt, float, noinverse, clip, 0, 1, 1.f, av_clipf, 1)
124 
125 filters(fltp, float, inverse, noclip, 1, 0, 1.f, av_clipf, 0)
126 filters(fltp, float, inverse, clip, 1, 1, 1.f, av_clipf, 0)
127 filters(fltp, float, noinverse, noclip, 0, 0, 1.f, av_clipf, 0)
128 filters(fltp, float, noinverse, clip, 0, 1, 1.f, av_clipf, 0)
129 
130 filters(dbl, double, inverse, noclip, 1, 0, 1.0, av_clipd, 1)
131 filters(dbl, double, inverse, clip, 1, 1, 1.0, av_clipd, 1)
132 filters(dbl, double, noinverse, noclip, 0, 0, 1.0, av_clipd, 1)
133 filters(dbl, double, noinverse, clip, 0, 1, 1.0, av_clipd, 1)
134 
135 filters(dblp, double, inverse, noclip, 1, 0, 1.0, av_clipd, 0)
136 filters(dblp, double, inverse, clip, 1, 1, 1.0, av_clipd, 0)
137 filters(dblp, double, noinverse, noclip, 0, 0, 1.0, av_clipd, 0)
138 filters(dblp, double, noinverse, clip, 0, 1, 1.0, av_clipd, 0)
139 
141 {
142  AVFilterContext *ctx = inlink->dst;
143  CrystalizerContext *s = ctx->priv;
144 
145  switch (inlink->format) {
146  case AV_SAMPLE_FMT_FLT:
147  s->filter[0][0] = filter_inverse_flt_noclip;
148  s->filter[1][0] = filter_noinverse_flt_noclip;
149  s->filter[0][1] = filter_inverse_flt_clip;
150  s->filter[1][1] = filter_noinverse_flt_clip;
151  break;
152  case AV_SAMPLE_FMT_FLTP:
153  s->filter[0][0] = filter_inverse_fltp_noclip;
154  s->filter[1][0] = filter_noinverse_fltp_noclip;
155  s->filter[0][1] = filter_inverse_fltp_clip;
156  s->filter[1][1] = filter_noinverse_fltp_clip;
157  break;
158  case AV_SAMPLE_FMT_DBL:
159  s->filter[0][0] = filter_inverse_dbl_noclip;
160  s->filter[1][0] = filter_noinverse_dbl_noclip;
161  s->filter[0][1] = filter_inverse_dbl_clip;
162  s->filter[1][1] = filter_noinverse_dbl_clip;
163  break;
164  case AV_SAMPLE_FMT_DBLP:
165  s->filter[0][0] = filter_inverse_dblp_noclip;
166  s->filter[1][0] = filter_noinverse_dblp_noclip;
167  s->filter[0][1] = filter_inverse_dblp_clip;
168  s->filter[1][1] = filter_noinverse_dblp_clip;
169  break;
170  default:
171  return AVERROR_BUG;
172  }
173 
174  return 0;
175 }
176 
178 {
179  AVFilterContext *ctx = inlink->dst;
180  AVFilterLink *outlink = ctx->outputs[0];
181  CrystalizerContext *s = ctx->priv;
182  AVFrame *out;
183  ThreadData td;
184 
185  if (!s->prev) {
186  s->prev = ff_get_audio_buffer(inlink, 1);
187  if (!s->prev) {
188  av_frame_free(&in);
189  return AVERROR(ENOMEM);
190  }
191  }
192 
193  if (av_frame_is_writable(in)) {
194  out = in;
195  } else {
196  out = ff_get_audio_buffer(outlink, in->nb_samples);
197  if (!out) {
198  av_frame_free(&in);
199  return AVERROR(ENOMEM);
200  }
202  }
203 
204  td.d = (void **)out->extended_data;
205  td.s = (const void **)in->extended_data;
206  td.p = (void **)s->prev->extended_data;
207  td.nb_samples = in->nb_samples;
208  td.channels = in->ch_layout.nb_channels;
209  td.mult = ctx->is_disabled ? 0.f : s->mult;
210  ff_filter_execute(ctx, s->filter[td.mult >= 0.f][s->clip], &td, NULL,
211  FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
212 
213  if (out != in)
214  av_frame_free(&in);
215 
216  return ff_filter_frame(outlink, out);
217 }
218 
220 {
221  CrystalizerContext *s = ctx->priv;
222 
223  av_frame_free(&s->prev);
224 }
225 
226 static const AVFilterPad inputs[] = {
227  {
228  .name = "default",
229  .type = AVMEDIA_TYPE_AUDIO,
230  .filter_frame = filter_frame,
231  .config_props = config_input,
232  },
233 };
234 
236  .name = "crystalizer",
237  .description = NULL_IF_CONFIG_SMALL("Simple audio noise sharpening filter."),
238  .priv_size = sizeof(CrystalizerContext),
239  .priv_class = &crystalizer_class,
240  .uninit = uninit,
245  .process_command = ff_filter_process_command,
248 };
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:97
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
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
inverse
inverse
Definition: af_crystalizer.c:121
CrystalizerContext::filter
int(* filter[2][2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_crystalizer.c:31
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_crystalizer.c:140
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
CrystalizerContext::clip
int clip
Definition: af_crystalizer.c:29
AVOption
AVOption.
Definition: opt.h:346
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_crystalizer.c:219
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
ThreadData::channels
int channels
Definition: af_asoftclip.c:401
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(crystalizer)
OFFSET
#define OFFSET(x)
Definition: af_crystalizer.c:34
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:776
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
mult
static int16_t mult(Float11 *f1, Float11 *f2)
Definition: g726.c:60
av_cold
#define av_cold
Definition: attributes.h:90
clip
clip
Definition: af_crystalizer.c:121
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
CrystalizerContext::mult
float mult
Definition: af_crystalizer.c:28
filters
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
Definition: af_crystalizer.c:54
ThreadData::nb_samples
int nb_samples
Definition: af_asoftclip.c:400
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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:51
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:709
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:33
av_clipf
av_clipf
Definition: af_crystalizer.c:121
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_crystalizer.c:177
f
f
Definition: af_crystalizer.c:121
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:106
CrystalizerContext::prev
AVFrame * prev
Definition: af_crystalizer.c:30
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
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:890
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
ThreadData::d
void ** d
Definition: af_crystalizer.c:46
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:455
inputs
static const AVFilterPad inputs[]
Definition: af_crystalizer.c:226
crystalizer_options
static const AVOption crystalizer_options[]
Definition: af_crystalizer.c:37
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:436
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:825
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
CrystalizerContext
Definition: af_crystalizer.c:26
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
A
#define A
Definition: af_crystalizer.c:35
channel_layout.h
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
ThreadData::s
const void ** s
Definition: af_crystalizer.c:48
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:117
audio.h
ThreadData::p
void ** p
Definition: af_crystalizer.c:47
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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:155
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:235
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
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:134
int
int
Definition: ffmpeg_filter.c:409
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
noinverse
noinverse
Definition: af_crystalizer.c:123
av_clipd
av_clipd
Definition: af_crystalizer.c:131
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: internal.h:170