FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_stereowiden.c
Date: 2024-04-24 02:45:42
Exec Total Coverage
Lines: 0 55 0.0%
Functions: 0 4 0.0%
Branches: 0 24 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2012 VLC authors and VideoLAN
3 * Author : Sukrit Sangwan < sukritsangwan at gmail dot com >
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27 #include "formats.h"
28
29 typedef struct StereoWidenContext {
30 const AVClass *class;
31
32 float delay;
33 float feedback;
34 float crossfeed;
35 float drymix;
36
37 float *buffer;
38 float *cur;
39 int length;
40 } StereoWidenContext;
41
42 #define OFFSET(x) offsetof(StereoWidenContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
45
46 static const AVOption stereowiden_options[] = {
47 { "delay", "set delay time", OFFSET(delay), AV_OPT_TYPE_FLOAT, {.dbl=20}, 1, 100, A },
48 { "feedback", "set feedback gain", OFFSET(feedback), AV_OPT_TYPE_FLOAT, {.dbl=.3}, 0, 0.9, AT },
49 { "crossfeed", "set cross feed", OFFSET(crossfeed), AV_OPT_TYPE_FLOAT, {.dbl=.3}, 0, 0.8, AT },
50 { "drymix", "set dry-mix", OFFSET(drymix), AV_OPT_TYPE_FLOAT, {.dbl=.8}, 0, 1.0, AT },
51 { NULL }
52 };
53
54 AVFILTER_DEFINE_CLASS(stereowiden);
55
56 static int query_formats(AVFilterContext *ctx)
57 {
58 AVFilterFormats *formats = NULL;
59 AVFilterChannelLayouts *layout = NULL;
60 int ret;
61
62 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
63 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
64 (ret = ff_add_channel_layout (&layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
65 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
66 return ret;
67
68 return ff_set_common_all_samplerates(ctx);
69 }
70
71 static int config_input(AVFilterLink *inlink)
72 {
73 AVFilterContext *ctx = inlink->dst;
74 StereoWidenContext *s = ctx->priv;
75
76 s->length = lrintf(s->delay * inlink->sample_rate / 1000);
77 s->length *= 2;
78 if (s->length == 0)
79 return AVERROR(EINVAL);
80 s->buffer = av_calloc(s->length, sizeof(*s->buffer));
81 if (!s->buffer)
82 return AVERROR(ENOMEM);
83 s->cur = s->buffer;
84
85 return 0;
86 }
87
88 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
89 {
90 AVFilterContext *ctx = inlink->dst;
91 AVFilterLink *outlink = ctx->outputs[0];
92 StereoWidenContext *s = ctx->priv;
93 const float *src = (const float *)in->data[0];
94 const float drymix = s->drymix;
95 const float crossfeed = s->crossfeed;
96 const float feedback = s->feedback;
97 AVFrame *out;
98 float *dst;
99 int n;
100
101 if (av_frame_is_writable(in)) {
102 out = in;
103 } else {
104 out = ff_get_audio_buffer(outlink, in->nb_samples);
105 if (!out) {
106 av_frame_free(&in);
107 return AVERROR(ENOMEM);
108 }
109 av_frame_copy_props(out, in);
110 }
111 dst = (float *)out->data[0];
112
113 for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2, s->cur += 2) {
114 const float left = src[0], right = src[1];
115
116 if (s->cur == s->buffer + s->length)
117 s->cur = s->buffer;
118
119 if (ctx->is_disabled) {
120 dst[0] = left;
121 dst[1] = right;
122 } else {
123 dst[0] = drymix * left - crossfeed * right - feedback * s->cur[1];
124 dst[1] = drymix * right - crossfeed * left - feedback * s->cur[0];
125 }
126
127 s->cur[0] = left;
128 s->cur[1] = right;
129 }
130
131 if (out != in)
132 av_frame_free(&in);
133 return ff_filter_frame(outlink, out);
134 }
135
136 static av_cold void uninit(AVFilterContext *ctx)
137 {
138 StereoWidenContext *s = ctx->priv;
139
140 av_freep(&s->buffer);
141 }
142
143 static const AVFilterPad inputs[] = {
144 {
145 .name = "default",
146 .type = AVMEDIA_TYPE_AUDIO,
147 .filter_frame = filter_frame,
148 .config_props = config_input,
149 },
150 };
151
152 const AVFilter ff_af_stereowiden = {
153 .name = "stereowiden",
154 .description = NULL_IF_CONFIG_SMALL("Apply stereo widening effect."),
155 .priv_size = sizeof(StereoWidenContext),
156 .priv_class = &stereowiden_class,
157 .uninit = uninit,
158 FILTER_INPUTS(inputs),
159 FILTER_OUTPUTS(ff_audio_default_filterpad),
160 FILTER_QUERY_FUNC(query_formats),
161 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
162 .process_command = ff_filter_process_command,
163 };
164