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 64
38 
39 static const int8_t filt[NUMTAPS] = {
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 taps[NUMTAPS * 2];
77 
79 {
80  static const int sample_rates[] = { 44100, -1 };
81  int ret;
82 
85 
86  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||
87  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
88  (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||
89  (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
90  (ret = ff_set_common_samplerates (ctx , ff_make_format_list(sample_rates) )) < 0)
91  return ret;
92 
93  return 0;
94 }
95 
96 //FIXME: replace with DSPContext.scalarproduct_int16
97 static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
98 {
100  int16_t j;
101 
102  while (in < endin) {
103  sample = 0;
104  for (j = 0; j < NUMTAPS; j++)
105  sample += in[j] * filt[j];
106  *out = av_clip_int16(sample >> 6);
107  out++;
108  in++;
109  }
110 
111  return out;
112 }
113 
114 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
115 {
116  AVFilterLink *outlink = inlink->dst->outputs[0];
117  int16_t *taps, *endin, *in, *out;
118  AVFrame *outsamples = ff_get_audio_buffer(outlink, insamples->nb_samples);
119  int len;
120 
121  if (!outsamples) {
122  av_frame_free(&insamples);
123  return AVERROR(ENOMEM);
124  }
125  av_frame_copy_props(outsamples, insamples);
126 
127  taps = ((EarwaxContext *)inlink->dst->priv)->taps;
128  out = (int16_t *)outsamples->data[0];
129  in = (int16_t *)insamples ->data[0];
130 
131  len = FFMIN(NUMTAPS, 2*insamples->nb_samples);
132  // copy part of new input and process with saved input
133  memcpy(taps+NUMTAPS, in, len * sizeof(*taps));
134  out = scalarproduct(taps, taps + len, out);
135 
136  // process current input
137  if (2*insamples->nb_samples >= NUMTAPS ){
138  endin = in + insamples->nb_samples * 2 - NUMTAPS;
139  scalarproduct(in, endin, out);
140 
141  // save part of input for next round
142  memcpy(taps, endin, NUMTAPS * sizeof(*taps));
143  } else
144  memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps));
145 
146  av_frame_free(&insamples);
147  return ff_filter_frame(outlink, outsamples);
148 }
149 
150 static const AVFilterPad earwax_inputs[] = {
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_AUDIO,
154  .filter_frame = filter_frame,
155  },
156  { NULL }
157 };
158 
159 static const AVFilterPad earwax_outputs[] = {
160  {
161  .name = "default",
162  .type = AVMEDIA_TYPE_AUDIO,
163  },
164  { NULL }
165 };
166 
168  .name = "earwax",
169  .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
170  .query_formats = query_formats,
171  .priv_size = sizeof(EarwaxContext),
172  .inputs = earwax_inputs,
173  .outputs = earwax_outputs,
174 };
static const AVFilterPad earwax_outputs[]
Definition: af_earwax.c:159
#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:550
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
Main libavfilter public API header.
#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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
static const AVFilterPad earwax_inputs[]
Definition: af_earwax.c:150
static int query_formats(AVFilterContext *ctx)
Definition: af_earwax.c:78
A filter pad used for either input or output.
Definition: internal.h:54
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:569
static int16_t * scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
Definition: af_earwax.c:97
int16_t taps[NUMTAPS *2]
Definition: af_earwax.c:75
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:342
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
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 ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:336
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: af_earwax.c:114
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
AVFilter ff_af_earwax
Definition: af_earwax.c:167
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define NUMTAPS
Definition: af_earwax.c:37
A list of supported channel layouts.
Definition: formats.h:85
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static const int8_t filt[NUMTAPS]
Definition: af_earwax.c:39
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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
sample_rates
signed 16 bits
Definition: samplefmt.h:61
int len
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
formats
Definition: signature.h:48
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
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
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
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:557
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654