FFmpeg
af_haas.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2010 Vladimir Sadovnikov
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 #include "formats.h"
26 
27 #define MAX_HAAS_DELAY 40
28 
29 typedef struct HaasContext {
30  const AVClass *class;
31 
33  double par_delay0;
34  double par_delay1;
38  double par_side_gain;
39  double par_gain0;
40  double par_gain1;
41  double par_balance0;
42  double par_balance1;
43  double level_in;
44  double level_out;
45 
46  double *buffer;
47  size_t buffer_size;
48  uint32_t write_ptr;
49  uint32_t delay[2];
50  double balance_l[2];
51  double balance_r[2];
52  double phase0;
53  double phase1;
54 } HaasContext;
55 
56 #define OFFSET(x) offsetof(HaasContext, x)
57 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 
59 static const AVOption haas_options[] = {
60  { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
61  { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
62  { "side_gain", "set side gain", OFFSET(par_side_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
63  { "middle_source", "set middle source", OFFSET(par_m_source), AV_OPT_TYPE_INT, {.i64=2}, 0, 3, A, "source" },
64  { "left", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "source" },
65  { "right", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "source" },
66  { "mid", "L+R", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "source" },
67  { "side", "L-R", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, "source" },
68  { "middle_phase", "set middle phase", OFFSET(par_middle_phase), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
69  { "left_delay", "set left delay", OFFSET(par_delay0), AV_OPT_TYPE_DOUBLE, {.dbl=2.05}, 0, MAX_HAAS_DELAY, A },
70  { "left_balance", "set left balance", OFFSET(par_balance0), AV_OPT_TYPE_DOUBLE, {.dbl=-1.0}, -1, 1, A },
71  { "left_gain", "set left gain", OFFSET(par_gain0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
72  { "left_phase", "set left phase", OFFSET(par_phase0), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
73  { "right_delay", "set right delay", OFFSET(par_delay1), AV_OPT_TYPE_DOUBLE, {.dbl=2.12}, 0, MAX_HAAS_DELAY, A },
74  { "right_balance", "set right balance", OFFSET(par_balance1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, A },
75  { "right_gain", "set right gain", OFFSET(par_gain1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
76  { "right_phase", "set right phase", OFFSET(par_phase1), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
77  { NULL }
78 };
79 
81 
83 {
86  int ret;
87 
88  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 ||
89  (ret = ff_set_common_formats (ctx , formats )) < 0 ||
92  return ret;
93 
96 }
97 
99 {
100  AVFilterContext *ctx = inlink->dst;
101  HaasContext *s = ctx->priv;
102  size_t min_buf_size = (size_t)(inlink->sample_rate * MAX_HAAS_DELAY * 0.001);
103  size_t new_buf_size = 1;
104 
105  while (new_buf_size < min_buf_size)
106  new_buf_size <<= 1;
107 
108  av_freep(&s->buffer);
109  s->buffer = av_calloc(new_buf_size, sizeof(*s->buffer));
110  if (!s->buffer)
111  return AVERROR(ENOMEM);
112 
113  s->buffer_size = new_buf_size;
114  s->write_ptr = 0;
115 
116  s->delay[0] = (uint32_t)(s->par_delay0 * 0.001 * inlink->sample_rate);
117  s->delay[1] = (uint32_t)(s->par_delay1 * 0.001 * inlink->sample_rate);
118 
119  s->phase0 = s->par_phase0 ? 1.0 : -1.0;
120  s->phase1 = s->par_phase1 ? 1.0 : -1.0;
121 
122  s->balance_l[0] = (s->par_balance0 + 1) / 2 * s->par_gain0 * s->phase0;
123  s->balance_r[0] = (1.0 - (s->par_balance0 + 1) / 2) * (s->par_gain0) * s->phase0;
124  s->balance_l[1] = (s->par_balance1 + 1) / 2 * s->par_gain1 * s->phase1;
125  s->balance_r[1] = (1.0 - (s->par_balance1 + 1) / 2) * (s->par_gain1) * s->phase1;
126 
127  return 0;
128 }
129 
131 {
132  AVFilterContext *ctx = inlink->dst;
133  AVFilterLink *outlink = ctx->outputs[0];
134  HaasContext *s = ctx->priv;
135  const double *src = (const double *)in->data[0];
136  const double level_in = s->level_in;
137  const double level_out = s->level_out;
138  const uint32_t mask = s->buffer_size - 1;
139  double *buffer = s->buffer;
140  AVFrame *out;
141  double *dst;
142  int n;
143 
145  out = in;
146  } else {
147  out = ff_get_audio_buffer(outlink, in->nb_samples);
148  if (!out) {
149  av_frame_free(&in);
150  return AVERROR(ENOMEM);
151  }
153  }
154  dst = (double *)out->data[0];
155 
156  for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
157  double mid, side[2], side_l, side_r;
158  uint32_t s0_ptr, s1_ptr;
159 
160  switch (s->par_m_source) {
161  case 0: mid = src[0]; break;
162  case 1: mid = src[1]; break;
163  case 2: mid = (src[0] + src[1]) * 0.5; break;
164  case 3: mid = (src[0] - src[1]) * 0.5; break;
165  }
166 
167  mid *= level_in;
168 
169  buffer[s->write_ptr] = mid;
170 
171  s0_ptr = (s->write_ptr + s->buffer_size - s->delay[0]) & mask;
172  s1_ptr = (s->write_ptr + s->buffer_size - s->delay[1]) & mask;
173 
174  if (s->par_middle_phase)
175  mid = -mid;
176 
177  side[0] = buffer[s0_ptr] * s->par_side_gain;
178  side[1] = buffer[s1_ptr] * s->par_side_gain;
179  side_l = side[0] * s->balance_l[0] - side[1] * s->balance_l[1];
180  side_r = side[1] * s->balance_r[1] - side[0] * s->balance_r[0];
181 
182  dst[0] = (mid + side_l) * level_out;
183  dst[1] = (mid + side_r) * level_out;
184 
185  s->write_ptr = (s->write_ptr + 1) & mask;
186  }
187 
188  if (out != in)
189  av_frame_free(&in);
190  return ff_filter_frame(outlink, out);
191 }
192 
194 {
195  HaasContext *s = ctx->priv;
196 
197  av_freep(&s->buffer);
198  s->buffer_size = 0;
199 }
200 
201 static const AVFilterPad inputs[] = {
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_AUDIO,
205  .filter_frame = filter_frame,
206  .config_props = config_input,
207  },
208  { NULL }
209 };
210 
211 static const AVFilterPad outputs[] = {
212  {
213  .name = "default",
214  .type = AVMEDIA_TYPE_AUDIO,
215  },
216  { NULL }
217 };
218 
220  .name = "haas",
221  .description = NULL_IF_CONFIG_SMALL("Apply Haas Stereo Enhancer."),
222  .query_formats = query_formats,
223  .priv_size = sizeof(HaasContext),
224  .priv_class = &haas_class,
225  .uninit = uninit,
226  .inputs = inputs,
227  .outputs = outputs,
228 };
formats
formats
Definition: signature.h:48
HaasContext::par_delay0
double par_delay0
Definition: af_haas.c:33
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:86
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:86
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_haas.c:98
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
HaasContext::level_out
double level_out
Definition: af_haas.c:44
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
A
#define A
Definition: af_haas.c:57
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_haas.c:82
HaasContext::par_delay1
double par_delay1
Definition: af_haas.c:34
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
HaasContext::buffer
double * buffer
Definition: af_haas.c:46
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption
AVOption.
Definition: opt.h:248
HaasContext::balance_l
double balance_l[2]
Definition: af_haas.c:50
OFFSET
#define OFFSET(x)
Definition: af_haas.c:56
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_haas.c:130
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
HaasContext::write_ptr
uint32_t write_ptr
Definition: af_haas.c:48
ff_af_haas
AVFilter ff_af_haas
Definition: af_haas.c:219
MAX_HAAS_DELAY
#define MAX_HAAS_DELAY
Definition: af_haas.c:27
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
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:587
mask
static const uint16_t mask[17]
Definition: lzw.c:38
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:338
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
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:658
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(haas)
HaasContext::par_middle_phase
int par_middle_phase
Definition: af_haas.c:37
src
#define src
Definition: vp8dsp.c:255
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:332
HaasContext::par_m_source
int par_m_source
Definition: af_haas.c:32
HaasContext::par_phase1
int par_phase1
Definition: af_haas.c:36
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:594
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
HaasContext::par_balance1
double par_balance1
Definition: af_haas.c:42
in
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
Definition: audio_convert.c:326
HaasContext::delay
uint32_t delay[2]
Definition: af_haas.c:49
outputs
static const AVFilterPad outputs[]
Definition: af_haas.c:211
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
HaasContext::phase0
double phase0
Definition: af_haas.c:52
AVFilter
Filter definition.
Definition: avfilter.h:145
HaasContext::par_side_gain
double par_side_gain
Definition: af_haas.c:38
ret
ret
Definition: filter_design.txt:187
HaasContext::par_balance0
double par_balance0
Definition: af_haas.c:41
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:421
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
channel_layout.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
HaasContext::balance_r
double balance_r[2]
Definition: af_haas.c:51
HaasContext::par_gain0
double par_gain0
Definition: af_haas.c:39
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
audio.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_haas.c:193
inputs
static const AVFilterPad inputs[]
Definition: af_haas.c:201
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
HaasContext::phase1
double phase1
Definition: af_haas.c:53
HaasContext::par_phase0
int par_phase0
Definition: af_haas.c:35
haas_options
static const AVOption haas_options[]
Definition: af_haas.c:59
HaasContext
Definition: af_haas.c:29
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:575
HaasContext::buffer_size
size_t buffer_size
Definition: af_haas.c:47
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
HaasContext::par_gain1
double par_gain1
Definition: af_haas.c:40
HaasContext::level_in
double level_in
Definition: af_haas.c:43
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:568