FFmpeg  4.0
af_bs2b.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 /**
20  * @file
21  * Bauer stereo-to-binaural filter
22  */
23 
24 #include <bs2b.h>
25 
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 
30 #include "audio.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 
35 typedef void (*filter_func)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
36 
37 typedef struct Bs2bContext {
38  const AVClass *class;
39 
40  int profile;
41  int fcut;
42  int feed;
43 
44  t_bs2bdp bs2bp;
45 
47 } Bs2bContext;
48 
49 #define OFFSET(x) offsetof(Bs2bContext, x)
50 #define A AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
51 
52 static const AVOption bs2b_options[] = {
53  { "profile", "Apply a pre-defined crossfeed level",
54  OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, INT_MAX, A, "profile" },
55  { "default", "default profile", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, 0, A, "profile" },
56  { "cmoy", "Chu Moy circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_CMOY_CLEVEL }, 0, 0, A, "profile" },
57  { "jmeier", "Jan Meier circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_JMEIER_CLEVEL }, 0, 0, A, "profile" },
58  { "fcut", "Set cut frequency (in Hz)",
59  OFFSET(fcut), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFCUT, A },
60  { "feed", "Set feed level (in Hz)",
61  OFFSET(feed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFEED, A },
62  { NULL },
63 };
64 
66 
68 {
69  Bs2bContext *bs2b = ctx->priv;
70 
71  if (!(bs2b->bs2bp = bs2b_open()))
72  return AVERROR(ENOMEM);
73 
74  bs2b_set_level(bs2b->bs2bp, bs2b->profile);
75 
76  if (bs2b->fcut)
77  bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
78 
79  if (bs2b->feed)
80  bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
81 
82  return 0;
83 }
84 
86 {
87  Bs2bContext *bs2b = ctx->priv;
88 
89  if (bs2b->bs2bp)
90  bs2b_close(bs2b->bs2bp);
91 }
92 
94 {
97 
98  static const enum AVSampleFormat sample_fmts[] = {
105  };
106  int ret;
107 
108  if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
109  return AVERROR(ENOMEM);
110  ret = ff_set_common_channel_layouts(ctx, layouts);
111  if (ret < 0)
112  return ret;
113 
114  formats = ff_make_format_list(sample_fmts);
115  if (!formats)
116  return AVERROR(ENOMEM);
117  ret = ff_set_common_formats(ctx, formats);
118  if (ret < 0)
119  return ret;
120 
121  formats = ff_all_samplerates();
122  if (!formats)
123  return AVERROR(ENOMEM);
124  return ff_set_common_samplerates(ctx, formats);
125 }
126 
127 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
128 {
129  int ret;
130  AVFrame *out_frame;
131 
132  Bs2bContext *bs2b = inlink->dst->priv;
133  AVFilterLink *outlink = inlink->dst->outputs[0];
134 
135  if (av_frame_is_writable(frame)) {
136  out_frame = frame;
137  } else {
138  out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
139  if (!out_frame) {
140  av_frame_free(&frame);
141  return AVERROR(ENOMEM);
142  }
143  av_frame_copy(out_frame, frame);
144  ret = av_frame_copy_props(out_frame, frame);
145  if (ret < 0) {
146  av_frame_free(&out_frame);
147  av_frame_free(&frame);
148  return ret;
149  }
150  }
151 
152  bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
153 
154  if (frame != out_frame)
155  av_frame_free(&frame);
156 
157  return ff_filter_frame(outlink, out_frame);
158 }
159 
160 static int config_output(AVFilterLink *outlink)
161 {
162  AVFilterContext *ctx = outlink->src;
163  Bs2bContext *bs2b = ctx->priv;
164  AVFilterLink *inlink = ctx->inputs[0];
165 
166  int srate = inlink->sample_rate;
167 
168  switch (inlink->format) {
169  case AV_SAMPLE_FMT_U8:
170  bs2b->filter = (filter_func) bs2b_cross_feed_u8;
171  break;
172  case AV_SAMPLE_FMT_S16:
173  bs2b->filter = (filter_func) bs2b_cross_feed_s16;
174  break;
175  case AV_SAMPLE_FMT_S32:
176  bs2b->filter = (filter_func) bs2b_cross_feed_s32;
177  break;
178  case AV_SAMPLE_FMT_FLT:
179  bs2b->filter = (filter_func) bs2b_cross_feed_f;
180  break;
181  case AV_SAMPLE_FMT_DBL:
182  bs2b->filter = (filter_func) bs2b_cross_feed_d;
183  break;
184  default:
185  return AVERROR_BUG;
186  }
187 
188  if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
189  return AVERROR(ENOSYS);
190 
191  bs2b_set_srate(bs2b->bs2bp, srate);
192 
193  return 0;
194 }
195 
196 static const AVFilterPad bs2b_inputs[] = {
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_AUDIO,
200  .filter_frame = filter_frame,
201  },
202  { NULL }
203 };
204 
205 static const AVFilterPad bs2b_outputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_AUDIO,
209  .config_props = config_output,
210  },
211  { NULL }
212 };
213 
215  .name = "bs2b",
216  .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
217  .query_formats = query_formats,
218  .priv_size = sizeof(Bs2bContext),
219  .priv_class = &bs2b_class,
220  .init = init,
221  .uninit = uninit,
222  .inputs = bs2b_inputs,
223  .outputs = bs2b_outputs,
224 };
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
t_bs2bdp bs2bp
Definition: af_bs2b.c:44
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
static const AVFilterPad bs2b_inputs[]
Definition: af_bs2b.c:196
void(* filter_func)(t_bs2bdp bs2bdp, uint8_t *sample, int n)
Definition: af_bs2b.c:35
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_bs2b.c:127
filter_func filter
Definition: af_bs2b.c:46
#define AV_CH_LAYOUT_STEREO
#define sample
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
uint8_t
#define av_cold
Definition: attributes.h:82
AV_SAMPLE_FMT_U8
AVOptions.
static AVFrame * frame
static int query_formats(AVFilterContext *ctx)
Definition: af_bs2b.c:93
signed 32 bits
Definition: samplefmt.h:62
A filter pad used for either input or output.
Definition: internal.h:54
AVFilter ff_af_bs2b
Definition: af_bs2b.c:214
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:343
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:790
int feed
Definition: af_bs2b.c:42
audio channel layout utility functions
AVFormatContext * ctx
Definition: movenc.c:48
int n
Definition: avisynth_c.h:684
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int config_output(AVFilterLink *outlink)
Definition: af_bs2b.c:160
A list of supported channel layouts.
Definition: formats.h:85
int profile
Definition: af_bs2b.c:40
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
typedef void(RENAME(mix_any_func_type))
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:592
int fcut
Definition: af_bs2b.c:41
AVFILTER_DEFINE_CLASS(bs2b)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
#define OFFSET(x)
Definition: af_bs2b.c:49
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static const AVOption bs2b_options[]
Definition: af_bs2b.c:52
common internal and external API header
signed 16 bits
Definition: samplefmt.h:61
static const AVFilterPad bs2b_outputs[]
Definition: af_bs2b.c:205
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_bs2b.c:85
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
formats
Definition: signature.h:48
#define A
Definition: af_bs2b.c:50
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:265
static av_cold int init(AVFilterContext *ctx)
Definition: af_bs2b.c:67
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:284
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652