2 * Copyright (c) 2015 The FFmpeg Project
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
27 typedef struct ExtraStereoContext {
33 #define OFFSET(x) offsetof(ExtraStereoContext, x)
34 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
36 static const AVOption extrastereo_options[] = {
37 { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A },
38 { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
42 AVFILTER_DEFINE_CLASS(extrastereo);
44 static int query_formats(AVFilterContext *ctx)
46 AVFilterFormats *formats = NULL;
47 AVFilterChannelLayouts *layout = NULL;
50 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
51 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
52 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
53 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
56 formats = ff_all_samplerates();
57 return ff_set_common_samplerates(ctx, formats);
60 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
62 AVFilterContext *ctx = inlink->dst;
63 AVFilterLink *outlink = ctx->outputs[0];
64 ExtraStereoContext *s = ctx->priv;
65 const float *src = (const float *)in->data[0];
66 const float mult = s->mult;
71 if (av_frame_is_writable(in)) {
74 out = ff_get_audio_buffer(outlink, in->nb_samples);
77 return AVERROR(ENOMEM);
79 av_frame_copy_props(out, in);
81 dst = (float *)out->data[0];
83 for (n = 0; n < in->nb_samples; n++) {
84 float average, left, right;
87 right = src[n * 2 + 1];
88 average = (left + right) / 2.;
89 left = average + mult * (left - average);
90 right = average + mult * (right - average);
93 left = av_clipf(left, -1, 1);
94 right = av_clipf(right, -1, 1);
98 dst[n * 2 + 1] = right;
103 return ff_filter_frame(outlink, out);
106 static const AVFilterPad inputs[] = {
109 .type = AVMEDIA_TYPE_AUDIO,
110 .filter_frame = filter_frame,
115 static const AVFilterPad outputs[] = {
118 .type = AVMEDIA_TYPE_AUDIO,
123 const AVFilter ff_af_extrastereo = {
124 .name = "extrastereo",
125 .description = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
126 .query_formats = query_formats,
127 .priv_size = sizeof(ExtraStereoContext),
128 .priv_class = &extrastereo_class,
131 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
132 .process_command = ff_filter_process_command,