• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 typedef 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|AV_OPT_FLAG_RUNTIME_PARAM
35 
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 },
39     { NULL }
40 };
41 
42 AVFILTER_DEFINE_CLASS(extrastereo);
43 
query_formats(AVFilterContext * ctx)44 static 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 , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
53         (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0)
54         return ret;
55 
56     return ff_set_common_all_samplerates(ctx);
57 }
58 
filter_frame(AVFilterLink * inlink,AVFrame * in)59 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
60 {
61     AVFilterContext *ctx = inlink->dst;
62     AVFilterLink *outlink = ctx->outputs[0];
63     ExtraStereoContext *s = ctx->priv;
64     const float *src = (const float *)in->data[0];
65     const float mult = s->mult;
66     AVFrame *out;
67     float *dst;
68     int n;
69 
70     if (av_frame_is_writable(in)) {
71         out = in;
72     } else {
73         out = ff_get_audio_buffer(outlink, in->nb_samples);
74         if (!out) {
75             av_frame_free(&in);
76             return AVERROR(ENOMEM);
77         }
78         av_frame_copy_props(out, in);
79     }
80     dst = (float *)out->data[0];
81 
82     for (n = 0; n < in->nb_samples; n++) {
83         float average, left, right;
84 
85         left    = src[n * 2    ];
86         right   = src[n * 2 + 1];
87         average = (left + right) / 2.;
88         left    = average + mult * (left  - average);
89         right   = average + mult * (right - average);
90 
91         if (s->clip) {
92             left  = av_clipf(left,  -1, 1);
93             right = av_clipf(right, -1, 1);
94         }
95 
96         dst[n * 2    ] = left;
97         dst[n * 2 + 1] = right;
98     }
99 
100     if (out != in)
101         av_frame_free(&in);
102     return ff_filter_frame(outlink, out);
103 }
104 
105 static const AVFilterPad inputs[] = {
106     {
107         .name         = "default",
108         .type         = AVMEDIA_TYPE_AUDIO,
109         .filter_frame = filter_frame,
110     },
111 };
112 
113 static const AVFilterPad outputs[] = {
114     {
115         .name = "default",
116         .type = AVMEDIA_TYPE_AUDIO,
117     },
118 };
119 
120 const AVFilter ff_af_extrastereo = {
121     .name           = "extrastereo",
122     .description    = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
123     .priv_size      = sizeof(ExtraStereoContext),
124     .priv_class     = &extrastereo_class,
125     FILTER_INPUTS(inputs),
126     FILTER_OUTPUTS(outputs),
127     FILTER_QUERY_FUNC(query_formats),
128     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
129     .process_command = ff_filter_process_command,
130 };
131