FFmpeg
af_earwax.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Mina Nagy Zaki
3  * Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
4  * This source code is freely redistributable and may be used for any purpose.
5  * This copyright notice must be maintained. Edward Beingessner And Sundry
6  * Contributors are not responsible for the consequences of using this
7  * software.
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 /**
27  * @file
28  * Stereo Widening Effect. Adds audio cues to move stereo image in
29  * front of the listener. Adapted from the libsox earwax effect.
30  */
31 
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "formats.h"
36 
37 #define NUMTAPS 32
38 
39 static const int8_t filt[NUMTAPS * 2] = {
40 /* 30° 330° */
41  4, -6, /* 32 tap stereo FIR filter. */
42  4, -11, /* One side filters as if the */
43  -1, -5, /* signal was from 30 degrees */
44  3, 3, /* from the ear, the other as */
45  -2, 5, /* if 330 degrees. */
46  -5, 0,
47  9, 1,
48  6, 3, /* Input */
49  -4, -1, /* Left Right */
50  -5, -3, /* __________ __________ */
51  -2, -5, /* | | | | */
52  -7, 1, /* .---| Hh,0(f) | | Hh,0(f) |---. */
53  6, -7, /* / |__________| |__________| \ */
54  30, -29, /* / \ / \ */
55  12, -3, /* / X \ */
56  -11, 4, /* / / \ \ */
57  -3, 7, /* ____V_____ __________V V__________ _____V____ */
58  -20, 23, /* | | | | | | | | */
59  2, 0, /* | Hh,30(f) | | Hh,330(f)| | Hh,330(f)| | Hh,30(f) | */
60  1, -6, /* |__________| |__________| |__________| |__________| */
61  -14, -5, /* \ ___ / \ ___ / */
62  15, -18, /* \ / \ / _____ \ / \ / */
63  6, 7, /* `->| + |<--' / \ `-->| + |<-' */
64  15, -10, /* \___/ _/ \_ \___/ */
65  -14, 22, /* \ / \ / \ / */
66  -7, -2, /* `--->| | | |<---' */
67  -4, 9, /* \_/ \_/ */
68  6, -12, /* */
69  6, -6, /* Headphones */
70  0, -11,
71  0, -5,
72  4, 0};
73 
74 typedef struct EarwaxContext {
75  int16_t filter[2][NUMTAPS];
76  int16_t taps[4][NUMTAPS * 2];
77 
80 
82 {
83  static const int sample_rates[] = { 44100, -1 };
84  int ret;
85 
88 
89  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16P )) < 0 ||
90  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
94  return ret;
95 
96  return 0;
97 }
98 
99 //FIXME: replace with DSPContext.scalarproduct_int16
100 static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin,
101  const int16_t *filt, int16_t *out)
102 {
103  int32_t sample;
104  int16_t j;
105 
106  while (in < endin) {
107  sample = 0;
108  for (j = 0; j < NUMTAPS; j++)
109  sample += in[j] * filt[j];
110  *out = av_clip_int16(sample >> 7);
111  out++;
112  in++;
113  }
114 
115  return out;
116 }
117 
119 {
120  EarwaxContext *s = inlink->dst->priv;
121 
122  for (int i = 0; i < NUMTAPS; i++) {
123  s->filter[0][i] = filt[i * 2];
124  s->filter[1][i] = filt[i * 2 + 1];
125  }
126 
127  return 0;
128 }
129 
131  int input_ch, int output_ch,
132  int filter_ch, int tap_ch)
133 {
134  EarwaxContext *s = ctx->priv;
135  int16_t *taps, *endin, *dst, *src;
136  int len;
137 
138  taps = s->taps[tap_ch];
139  dst = (int16_t *)s->frame[input_ch]->data[output_ch];
140  src = (int16_t *)in->data[input_ch];
141 
142  len = FFMIN(NUMTAPS, in->nb_samples);
143  // copy part of new input and process with saved input
144  memcpy(taps+NUMTAPS, src, len * sizeof(*taps));
145  dst = scalarproduct(taps, taps + len, s->filter[filter_ch], dst);
146 
147  // process current input
148  if (in->nb_samples >= NUMTAPS) {
149  endin = src + in->nb_samples - NUMTAPS;
150  scalarproduct(src, endin, s->filter[filter_ch], dst);
151 
152  // save part of input for next round
153  memcpy(taps, endin, NUMTAPS * sizeof(*taps));
154  } else {
155  memmove(taps, taps + in->nb_samples, NUMTAPS * sizeof(*taps));
156  }
157 }
158 
160  int output_ch, int f0, int f1, int i0, int i1)
161 {
162  EarwaxContext *s = ctx->priv;
163  const int16_t *srcl = (const int16_t *)s->frame[f0]->data[i0];
164  const int16_t *srcr = (const int16_t *)s->frame[f1]->data[i1];
165  int16_t *dst = (int16_t *)out->data[output_ch];
166 
167  for (int n = 0; n < out->nb_samples; n++)
168  dst[n] = av_clip_int16(srcl[n] + srcr[n]);
169 }
170 
172 {
173  AVFilterContext *ctx = inlink->dst;
174  EarwaxContext *s = ctx->priv;
175  AVFilterLink *outlink = ctx->outputs[0];
176  AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples);
177 
178  for (int ch = 0; ch < 2; ch++) {
179  if (!s->frame[ch] || s->frame[ch]->nb_samples < in->nb_samples) {
180  av_frame_free(&s->frame[ch]);
181  s->frame[ch] = ff_get_audio_buffer(outlink, in->nb_samples);
182  if (!s->frame[ch]) {
183  av_frame_free(&in);
184  av_frame_free(&out);
185  return AVERROR(ENOMEM);
186  }
187  }
188  }
189 
190  if (!out) {
191  av_frame_free(&in);
192  return AVERROR(ENOMEM);
193  }
195 
196  convolve(ctx, in, 0, 0, 0, 0);
197  convolve(ctx, in, 0, 1, 1, 1);
198  convolve(ctx, in, 1, 0, 0, 2);
199  convolve(ctx, in, 1, 1, 1, 3);
200 
201  mix(ctx, out, 0, 0, 1, 1, 0);
202  mix(ctx, out, 1, 0, 1, 0, 1);
203 
204  av_frame_free(&in);
205  return ff_filter_frame(outlink, out);
206 }
207 
209 {
210  EarwaxContext *s = ctx->priv;
211 
212  av_frame_free(&s->frame[0]);
213  av_frame_free(&s->frame[1]);
214 }
215 
216 static const AVFilterPad earwax_inputs[] = {
217  {
218  .name = "default",
219  .type = AVMEDIA_TYPE_AUDIO,
220  .filter_frame = filter_frame,
221  .config_props = config_input,
222  },
223 };
224 
225 static const AVFilterPad earwax_outputs[] = {
226  {
227  .name = "default",
228  .type = AVMEDIA_TYPE_AUDIO,
229  },
230 };
231 
233  .name = "earwax",
234  .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
235  .priv_size = sizeof(EarwaxContext),
236  .uninit = uninit,
240 };
formats
formats
Definition: signature.h:48
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:100
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
ff_af_earwax
const AVFilter ff_af_earwax
Definition: af_earwax.c:232
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
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
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
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:733
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:354
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
EarwaxContext::taps
int16_t taps[4][NUMTAPS *2]
Definition: af_earwax.c:76
earwax_inputs
static const AVFilterPad earwax_inputs[]
Definition: af_earwax.c:216
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
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:749
scalarproduct
static int16_t * scalarproduct(const int16_t *in, const int16_t *endin, const int16_t *filt, int16_t *out)
Definition: af_earwax.c:100
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
earwax_outputs
static const AVFilterPad earwax_outputs[]
Definition: af_earwax.c:225
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
av_clip_int16
#define av_clip_int16
Definition: common.h:110
NULL
#define NULL
Definition: coverity.c:32
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:596
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_earwax.c:171
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:449
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_earwax.c:208
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:466
NUMTAPS
#define NUMTAPS
Definition: af_earwax.c:37
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
EarwaxContext::frame
AVFrame * frame[2]
Definition: af_earwax.c:78
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_earwax.c:118
sample
#define sample
Definition: flacdsp_template.c:44
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
sample_rates
sample_rates
Definition: ffmpeg_filter.c:153
layout
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 layout
Definition: filter_design.txt:18
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:405
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_earwax.c:81
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
len
int len
Definition: vorbis_enc_data.h:426
filt
static const int8_t filt[NUMTAPS *2]
Definition: af_earwax.c:39
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
mix
static void mix(AVFilterContext *ctx, AVFrame *out, int output_ch, int f0, int f1, int i0, int i1)
Definition: af_earwax.c:159
AVFilter
Filter definition.
Definition: avfilter.h:171
ret
ret
Definition: filter_design.txt:187
channel_layout.h
EarwaxContext
Definition: af_earwax.c:74
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
convolve
static void convolve(AVFilterContext *ctx, AVFrame *in, int input_ch, int output_ch, int filter_ch, int tap_ch)
Definition: af_earwax.c:130
int32_t
int32_t
Definition: audioconvert.c:56
EarwaxContext::filter
int16_t filter[2][NUMTAPS]
Definition: af_earwax.c:75
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats() which set all free audio links to the same list of channel layouts/sample...
Definition: formats.c:708