summaryrefslogtreecommitdiff
path: root/libavfilter/af_extrastereo.c (plain)
blob: a746006f7107feb1712b56a6f8c98ff29bba8e8e
1/*
2 * Copyright (c) 2015 The FFmpeg Project
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/opt.h"
23#include "avfilter.h"
24#include "audio.h"
25#include "formats.h"
26
27typedef struct ExtraStereoContext {
28 const AVClass *class;
29 float mult;
30 int clip;
31} ExtraStereoContext;
32
33#define OFFSET(x) offsetof(ExtraStereoContext, x)
34#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
35
36static 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 },
39 { NULL }
40};
41
42AVFILTER_DEFINE_CLASS(extrastereo);
43
44static int query_formats(AVFilterContext *ctx)
45{
46 AVFilterFormats *formats = NULL;
47 AVFilterChannelLayouts *layout = NULL;
48 int ret;
49
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)
54 return ret;
55
56 formats = ff_all_samplerates();
57 return ff_set_common_samplerates(ctx, formats);
58}
59
60static int filter_frame(AVFilterLink *inlink, AVFrame *in)
61{
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;
67 AVFrame *out;
68 float *dst;
69 int n;
70
71 if (av_frame_is_writable(in)) {
72 out = in;
73 } else {
74 out = ff_get_audio_buffer(inlink, in->nb_samples);
75 if (!out) {
76 av_frame_free(&in);
77 return AVERROR(ENOMEM);
78 }
79 av_frame_copy_props(out, in);
80 }
81 dst = (float *)out->data[0];
82
83 for (n = 0; n < in->nb_samples; n++) {
84 float average, left, right;
85
86 left = src[n * 2 ];
87 right = src[n * 2 + 1];
88 average = (left + right) / 2.;
89 left = average + mult * (left - average);
90 right = average + mult * (right - average);
91
92 if (s->clip) {
93 dst[n * 2 ] = av_clipf(left, -1, 1);
94 dst[n * 2 + 1] = av_clipf(right, -1, 1);
95 }
96 }
97
98 if (out != in)
99 av_frame_free(&in);
100 return ff_filter_frame(outlink, out);
101}
102
103static const AVFilterPad inputs[] = {
104 {
105 .name = "default",
106 .type = AVMEDIA_TYPE_AUDIO,
107 .filter_frame = filter_frame,
108 },
109 { NULL }
110};
111
112static const AVFilterPad outputs[] = {
113 {
114 .name = "default",
115 .type = AVMEDIA_TYPE_AUDIO,
116 },
117 { NULL }
118};
119
120AVFilter ff_af_extrastereo = {
121 .name = "extrastereo",
122 .description = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
123 .query_formats = query_formats,
124 .priv_size = sizeof(ExtraStereoContext),
125 .priv_class = &extrastereo_class,
126 .inputs = inputs,
127 .outputs = outputs,
128};
129