Line |
Branch |
Exec |
Source |
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 |
|
|
|
21 |
|
|
#include "libavutil/channel_layout.h" |
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 |
|
|
|
31 |
|
|
typedef struct HaasContext { |
32 |
|
|
const AVClass *class; |
33 |
|
|
|
34 |
|
|
int par_m_source; |
35 |
|
|
double par_delay0; |
36 |
|
|
double par_delay1; |
37 |
|
|
int par_phase0; |
38 |
|
|
int par_phase1; |
39 |
|
|
int par_middle_phase; |
40 |
|
|
double par_side_gain; |
41 |
|
|
double par_gain0; |
42 |
|
|
double par_gain1; |
43 |
|
|
double par_balance0; |
44 |
|
|
double par_balance1; |
45 |
|
|
double level_in; |
46 |
|
|
double level_out; |
47 |
|
|
|
48 |
|
|
double *buffer; |
49 |
|
|
size_t buffer_size; |
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; |
56 |
|
|
} HaasContext; |
57 |
|
|
|
58 |
|
|
#define OFFSET(x) offsetof(HaasContext, x) |
59 |
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
60 |
|
|
|
61 |
|
|
static 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 |
|
|
|
82 |
|
|
AVFILTER_DEFINE_CLASS(haas); |
83 |
|
|
|
84 |
|
✗ |
static int query_formats(AVFilterContext *ctx) |
85 |
|
|
{ |
86 |
|
✗ |
AVFilterFormats *formats = NULL; |
87 |
|
✗ |
AVFilterChannelLayouts *layout = NULL; |
88 |
|
|
int ret; |
89 |
|
|
|
90 |
|
✗ |
if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBL )) < 0 || |
91 |
|
✗ |
(ret = ff_set_common_formats (ctx , formats )) < 0 || |
92 |
|
✗ |
(ret = ff_add_channel_layout (&layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 || |
93 |
|
✗ |
(ret = ff_set_common_channel_layouts (ctx , layout )) < 0) |
94 |
|
✗ |
return ret; |
95 |
|
|
|
96 |
|
✗ |
return ff_set_common_all_samplerates(ctx); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
100 |
|
|
{ |
101 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
102 |
|
✗ |
HaasContext *s = ctx->priv; |
103 |
|
✗ |
size_t min_buf_size = (size_t)(inlink->sample_rate * MAX_HAAS_DELAY * 0.001); |
104 |
|
✗ |
size_t new_buf_size = 1; |
105 |
|
|
|
106 |
|
✗ |
while (new_buf_size < min_buf_size) |
107 |
|
✗ |
new_buf_size <<= 1; |
108 |
|
|
|
109 |
|
✗ |
av_freep(&s->buffer); |
110 |
|
✗ |
s->buffer = av_calloc(new_buf_size, sizeof(*s->buffer)); |
111 |
|
✗ |
if (!s->buffer) |
112 |
|
✗ |
return AVERROR(ENOMEM); |
113 |
|
|
|
114 |
|
✗ |
s->buffer_size = new_buf_size; |
115 |
|
✗ |
s->write_ptr = 0; |
116 |
|
|
|
117 |
|
✗ |
s->delay[0] = (uint32_t)(s->par_delay0 * 0.001 * inlink->sample_rate); |
118 |
|
✗ |
s->delay[1] = (uint32_t)(s->par_delay1 * 0.001 * inlink->sample_rate); |
119 |
|
|
|
120 |
|
✗ |
s->phase0 = s->par_phase0 ? 1.0 : -1.0; |
121 |
|
✗ |
s->phase1 = s->par_phase1 ? 1.0 : -1.0; |
122 |
|
|
|
123 |
|
✗ |
s->balance_l[0] = (s->par_balance0 + 1) / 2 * s->par_gain0 * s->phase0; |
124 |
|
✗ |
s->balance_r[0] = (1.0 - (s->par_balance0 + 1) / 2) * (s->par_gain0) * s->phase0; |
125 |
|
✗ |
s->balance_l[1] = (s->par_balance1 + 1) / 2 * s->par_gain1 * s->phase1; |
126 |
|
✗ |
s->balance_r[1] = (1.0 - (s->par_balance1 + 1) / 2) * (s->par_gain1) * s->phase1; |
127 |
|
|
|
128 |
|
✗ |
return 0; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
132 |
|
|
{ |
133 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
134 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
135 |
|
✗ |
HaasContext *s = ctx->priv; |
136 |
|
✗ |
const double *src = (const double *)in->data[0]; |
137 |
|
✗ |
const double level_in = s->level_in; |
138 |
|
✗ |
const double level_out = s->level_out; |
139 |
|
✗ |
const uint32_t mask = s->buffer_size - 1; |
140 |
|
✗ |
double *buffer = s->buffer; |
141 |
|
|
AVFrame *out; |
142 |
|
|
double *dst; |
143 |
|
|
int n; |
144 |
|
|
|
145 |
|
✗ |
if (av_frame_is_writable(in)) { |
146 |
|
✗ |
out = in; |
147 |
|
|
} else { |
148 |
|
✗ |
out = ff_get_audio_buffer(outlink, in->nb_samples); |
149 |
|
✗ |
if (!out) { |
150 |
|
✗ |
av_frame_free(&in); |
151 |
|
✗ |
return AVERROR(ENOMEM); |
152 |
|
|
} |
153 |
|
✗ |
av_frame_copy_props(out, in); |
154 |
|
|
} |
155 |
|
✗ |
dst = (double *)out->data[0]; |
156 |
|
|
|
157 |
|
✗ |
for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) { |
158 |
|
|
double mid, side[2], side_l, side_r; |
159 |
|
|
uint32_t s0_ptr, s1_ptr; |
160 |
|
|
|
161 |
|
✗ |
switch (s->par_m_source) { |
162 |
|
✗ |
case 0: mid = src[0]; break; |
163 |
|
✗ |
case 1: mid = src[1]; break; |
164 |
|
✗ |
case 2: mid = (src[0] + src[1]) * 0.5; break; |
165 |
|
✗ |
case 3: mid = (src[0] - src[1]) * 0.5; break; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
✗ |
mid *= level_in; |
169 |
|
|
|
170 |
|
✗ |
buffer[s->write_ptr] = mid; |
171 |
|
|
|
172 |
|
✗ |
s0_ptr = (s->write_ptr + s->buffer_size - s->delay[0]) & mask; |
173 |
|
✗ |
s1_ptr = (s->write_ptr + s->buffer_size - s->delay[1]) & mask; |
174 |
|
|
|
175 |
|
✗ |
if (s->par_middle_phase) |
176 |
|
✗ |
mid = -mid; |
177 |
|
|
|
178 |
|
✗ |
side[0] = buffer[s0_ptr] * s->par_side_gain; |
179 |
|
✗ |
side[1] = buffer[s1_ptr] * s->par_side_gain; |
180 |
|
✗ |
side_l = side[0] * s->balance_l[0] - side[1] * s->balance_l[1]; |
181 |
|
✗ |
side_r = side[1] * s->balance_r[1] - side[0] * s->balance_r[0]; |
182 |
|
|
|
183 |
|
✗ |
dst[0] = (mid + side_l) * level_out; |
184 |
|
✗ |
dst[1] = (mid + side_r) * level_out; |
185 |
|
|
|
186 |
|
✗ |
s->write_ptr = (s->write_ptr + 1) & mask; |
187 |
|
|
} |
188 |
|
|
|
189 |
|
✗ |
if (out != in) |
190 |
|
✗ |
av_frame_free(&in); |
191 |
|
✗ |
return ff_filter_frame(outlink, out); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
195 |
|
|
{ |
196 |
|
✗ |
HaasContext *s = ctx->priv; |
197 |
|
|
|
198 |
|
✗ |
av_freep(&s->buffer); |
199 |
|
✗ |
s->buffer_size = 0; |
200 |
|
✗ |
} |
201 |
|
|
|
202 |
|
|
static const AVFilterPad inputs[] = { |
203 |
|
|
{ |
204 |
|
|
.name = "default", |
205 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
206 |
|
|
.filter_frame = filter_frame, |
207 |
|
|
.config_props = config_input, |
208 |
|
|
}, |
209 |
|
|
}; |
210 |
|
|
|
211 |
|
|
const AVFilter ff_af_haas = { |
212 |
|
|
.name = "haas", |
213 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply Haas Stereo Enhancer."), |
214 |
|
|
.priv_size = sizeof(HaasContext), |
215 |
|
|
.priv_class = &haas_class, |
216 |
|
|
.uninit = uninit, |
217 |
|
|
FILTER_INPUTS(inputs), |
218 |
|
|
FILTER_OUTPUTS(ff_audio_default_filterpad), |
219 |
|
|
FILTER_QUERY_FUNC(query_formats), |
220 |
|
|
}; |
221 |
|
|
|