FFmpeg
Loading...
Searching...
No Matches
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/mem.h"
23#include "libavutil/opt.h"
24#include "avfilter.h"
25#include "audio.h"
26#include "filters.h"
27#include "formats.h"
28
29#define MAX_HAAS_DELAY 40
30
31typedef struct HaasContext {
32 const AVClass *class;
33
35 double par_delay0;
36 double par_delay1;
41 double par_gain0;
42 double par_gain1;
45 double level_in;
46 double level_out;
47
48 double *buffer;
50 uint32_t write_ptr;
51 uint32_t delay[2];
52 double balance_l[2];
53 double balance_r[2];
54 double phase0;
55 double phase1;
57
58#define OFFSET(x) offsetof(HaasContext, x)
59#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60
61static const AVOption haas_options[] = {
62 { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
63 { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
64 { "side_gain", "set side gain", OFFSET(par_side_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
65 { "middle_source", "set middle source", OFFSET(par_m_source), AV_OPT_TYPE_INT, {.i64=2}, 0, 3, A, .unit = "source" },
66 { "left", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "source" },
67 { "right", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "source" },
68 { "mid", "L+R", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "source" },
69 { "side", "L-R", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, .unit = "source" },
70 { "middle_phase", "set middle phase", OFFSET(par_middle_phase), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
71 { "left_delay", "set left delay", OFFSET(par_delay0), AV_OPT_TYPE_DOUBLE, {.dbl=2.05}, 0, MAX_HAAS_DELAY, A },
72 { "left_balance", "set left balance", OFFSET(par_balance0), AV_OPT_TYPE_DOUBLE, {.dbl=-1.0}, -1, 1, A },
73 { "left_gain", "set left gain", OFFSET(par_gain0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
74 { "left_phase", "set left phase", OFFSET(par_phase0), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
75 { "right_delay", "set right delay", OFFSET(par_delay1), AV_OPT_TYPE_DOUBLE, {.dbl=2.12}, 0, MAX_HAAS_DELAY, A },
76 { "right_balance", "set right balance", OFFSET(par_balance1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, A },
77 { "right_gain", "set right gain", OFFSET(par_gain1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
78 { "right_phase", "set right phase", OFFSET(par_phase1), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
79 { NULL }
80};
81
83
85 AVFilterFormatsConfig **cfg_in,
86 AVFilterFormatsConfig **cfg_out)
87{
88 static const enum AVSampleFormat formats[] = {
91 };
92 static const AVChannelLayout layouts[] = {
94 { .nb_channels = 0 },
95 };
96 int ret;
97
98 ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, formats);
99 if (ret < 0)
100 return ret;
101
103 if (ret < 0)
104 return ret;
105
106 return 0;
107}
108
109static int config_input(AVFilterLink *inlink)
110{
111 AVFilterContext *ctx = inlink->dst;
112 HaasContext *s = ctx->priv;
113 size_t min_buf_size = (size_t)(inlink->sample_rate * MAX_HAAS_DELAY * 0.001);
114 size_t new_buf_size = 1;
115
116 while (new_buf_size < min_buf_size)
117 new_buf_size <<= 1;
118
119 av_freep(&s->buffer);
120 s->buffer = av_calloc(new_buf_size, sizeof(*s->buffer));
121 if (!s->buffer)
122 return AVERROR(ENOMEM);
123
124 s->buffer_size = new_buf_size;
125 s->write_ptr = 0;
126
127 s->delay[0] = (uint32_t)(s->par_delay0 * 0.001 * inlink->sample_rate);
128 s->delay[1] = (uint32_t)(s->par_delay1 * 0.001 * inlink->sample_rate);
129
130 s->phase0 = s->par_phase0 ? 1.0 : -1.0;
131 s->phase1 = s->par_phase1 ? 1.0 : -1.0;
132
133 s->balance_l[0] = (s->par_balance0 + 1) / 2 * s->par_gain0 * s->phase0;
134 s->balance_r[0] = (1.0 - (s->par_balance0 + 1) / 2) * (s->par_gain0) * s->phase0;
135 s->balance_l[1] = (s->par_balance1 + 1) / 2 * s->par_gain1 * s->phase1;
136 s->balance_r[1] = (1.0 - (s->par_balance1 + 1) / 2) * (s->par_gain1) * s->phase1;
137
138 return 0;
139}
140
141static int filter_frame(AVFilterLink *inlink, AVFrame *in)
142{
143 AVFilterContext *ctx = inlink->dst;
144 AVFilterLink *outlink = ctx->outputs[0];
145 HaasContext *s = ctx->priv;
146 const double *src = (const double *)in->data[0];
147 const double level_in = s->level_in;
148 const double level_out = s->level_out;
149 const uint32_t mask = s->buffer_size - 1;
150 double *buffer = s->buffer;
151 AVFrame *out;
152 double *dst;
153 int n;
154
155 if (av_frame_is_writable(in)) {
156 out = in;
157 } else {
158 out = ff_get_audio_buffer(outlink, in->nb_samples);
159 if (!out) {
160 av_frame_free(&in);
161 return AVERROR(ENOMEM);
162 }
164 }
165 dst = (double *)out->data[0];
166
167 for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
168 double mid, side[2], side_l, side_r;
169 uint32_t s0_ptr, s1_ptr;
170
171 switch (s->par_m_source) {
172 case 0: mid = src[0]; break;
173 case 1: mid = src[1]; break;
174 case 2: mid = (src[0] + src[1]) * 0.5; break;
175 case 3: mid = (src[0] - src[1]) * 0.5; break;
176 }
177
178 mid *= level_in;
179
180 buffer[s->write_ptr] = mid;
181
182 s0_ptr = (s->write_ptr + s->buffer_size - s->delay[0]) & mask;
183 s1_ptr = (s->write_ptr + s->buffer_size - s->delay[1]) & mask;
184
185 if (s->par_middle_phase)
186 mid = -mid;
187
188 side[0] = buffer[s0_ptr] * s->par_side_gain;
189 side[1] = buffer[s1_ptr] * s->par_side_gain;
190 side_l = side[0] * s->balance_l[0] - side[1] * s->balance_l[1];
191 side_r = side[1] * s->balance_r[1] - side[0] * s->balance_r[0];
192
193 dst[0] = (mid + side_l) * level_out;
194 dst[1] = (mid + side_r) * level_out;
195
196 s->write_ptr = (s->write_ptr + 1) & mask;
197 }
198
199 if (out != in)
200 av_frame_free(&in);
201 return ff_filter_frame(outlink, out);
202}
203
205{
206 HaasContext *s = ctx->priv;
207
208 av_freep(&s->buffer);
209 s->buffer_size = 0;
210}
211
212static const AVFilterPad inputs[] = {
213 {
214 .name = "default",
215 .type = AVMEDIA_TYPE_AUDIO,
216 .filter_frame = filter_frame,
217 .config_props = config_input,
218 },
219};
220
222 .p.name = "haas",
223 .p.description = NULL_IF_CONFIG_SMALL("Apply Haas Stereo Enhancer."),
224 .p.priv_class = &haas_class,
225 .priv_size = sizeof(HaasContext),
226 .uninit = uninit,
230};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
static const AVOption haas_options[]
Definition af_haas.c:61
#define MAX_HAAS_DELAY
Definition af_haas.c:29
static int config_input(AVFilterLink *inlink)
Definition af_haas.c:109
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition af_haas.c:141
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition af_haas.c:84
static av_cold void uninit(AVFilterContext *ctx)
Definition af_haas.c:204
#define OFFSET(x)
Definition af_haas.c:58
const FFFilter ff_af_haas
Definition af_haas.c:221
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CHANNEL_LAYOUT_STEREO
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static const uint16_t mask[17]
Definition lzw.c:38
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
AVOptions.
formats
Definition signature.h:47
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
uint32_t write_ptr
Definition af_haas.c:50
int par_phase0
Definition af_haas.c:37
double par_gain1
Definition af_haas.c:42
double par_balance1
Definition af_haas.c:44
double phase0
Definition af_haas.c:54
int par_middle_phase
Definition af_haas.c:39
double par_delay0
Definition af_haas.c:35
int par_m_source
Definition af_haas.c:34
double level_out
Definition af_haas.c:46
uint32_t delay[2]
Definition af_haas.c:51
double balance_r[2]
Definition af_haas.c:53
double balance_l[2]
Definition af_haas.c:52
double level_in
Definition af_haas.c:45
double par_delay1
Definition af_haas.c:36
size_t buffer_size
Definition af_haas.c:49
double * buffer
Definition af_haas.c:48
double par_balance0
Definition af_haas.c:43
double par_gain0
Definition af_haas.c:41
double phase1
Definition af_haas.c:55
double par_side_gain
Definition af_haas.c:40
int par_phase1
Definition af_haas.c:38
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32