FFmpeg: libavfilter/af_bs2b.c Source File

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

Generated on Mon Feb 15 2016 15:20:44 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /