FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_stereowiden.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 54 0.0%
Functions: 0 4 0.0%
Branches: 0 20 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 "filters.h"
28 #include "formats.h"
29
30 typedef struct StereoWidenContext {
31 const AVClass *class;
32
33 float delay;
34 float feedback;
35 float crossfeed;
36 float drymix;
37
38 float *buffer;
39 float *cur;
40 int length;
41 } StereoWidenContext;
42
43 #define OFFSET(x) offsetof(StereoWidenContext, x)
44 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
46
47 static const AVOption stereowiden_options[] = {
48 { "delay", "set delay time", OFFSET(delay), AV_OPT_TYPE_FLOAT, {.dbl=20}, 1, 100, A },
49 { "feedback", "set feedback gain", OFFSET(feedback), AV_OPT_TYPE_FLOAT, {.dbl=.3}, 0, 0.9, AT },
50 { "crossfeed", "set cross feed", OFFSET(crossfeed), AV_OPT_TYPE_FLOAT, {.dbl=.3}, 0, 0.8, AT },
51 { "drymix", "set dry-mix", OFFSET(drymix), AV_OPT_TYPE_FLOAT, {.dbl=.8}, 0, 1.0, AT },
52 { NULL }
53 };
54
55 AVFILTER_DEFINE_CLASS(stereowiden);
56
57 static int query_formats(const AVFilterContext *ctx,
58 AVFilterFormatsConfig **cfg_in,
59 AVFilterFormatsConfig **cfg_out)
60 {
61 static const enum AVSampleFormat formats[] = {
62 AV_SAMPLE_FMT_FLT,
63 AV_SAMPLE_FMT_NONE,
64 };
65 static const AVChannelLayout layouts[] = {
66 AV_CHANNEL_LAYOUT_STEREO,
67 { .nb_channels = 0 },
68 };
69
70 int ret;
71
72 ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);
73 if (ret < 0)
74 return ret;
75
76 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts);
77 if (ret < 0)
78 return ret;
79
80 return 0;
81 }
82
83 static int config_input(AVFilterLink *inlink)
84 {
85 AVFilterContext *ctx = inlink->dst;
86 StereoWidenContext *s = ctx->priv;
87
88 s->length = lrintf(s->delay * inlink->sample_rate / 1000);
89 s->length *= 2;
90 if (s->length == 0)
91 return AVERROR(EINVAL);
92 s->buffer = av_calloc(s->length, sizeof(*s->buffer));
93 if (!s->buffer)
94 return AVERROR(ENOMEM);
95 s->cur = s->buffer;
96
97 return 0;
98 }
99
100 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
101 {
102 AVFilterContext *ctx = inlink->dst;
103 AVFilterLink *outlink = ctx->outputs[0];
104 StereoWidenContext *s = ctx->priv;
105 const float *src = (const float *)in->data[0];
106 const float drymix = s->drymix;
107 const float crossfeed = s->crossfeed;
108 const float feedback = s->feedback;
109 AVFrame *out;
110 float *dst;
111 int n;
112
113 if (av_frame_is_writable(in)) {
114 out = in;
115 } else {
116 out = ff_get_audio_buffer(outlink, in->nb_samples);
117 if (!out) {
118 av_frame_free(&in);
119 return AVERROR(ENOMEM);
120 }
121 av_frame_copy_props(out, in);
122 }
123 dst = (float *)out->data[0];
124
125 for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2, s->cur += 2) {
126 const float left = src[0], right = src[1];
127
128 if (s->cur == s->buffer + s->length)
129 s->cur = s->buffer;
130
131 if (ctx->is_disabled) {
132 dst[0] = left;
133 dst[1] = right;
134 } else {
135 dst[0] = drymix * left - crossfeed * right - feedback * s->cur[1];
136 dst[1] = drymix * right - crossfeed * left - feedback * s->cur[0];
137 }
138
139 s->cur[0] = left;
140 s->cur[1] = right;
141 }
142
143 if (out != in)
144 av_frame_free(&in);
145 return ff_filter_frame(outlink, out);
146 }
147
148 static av_cold void uninit(AVFilterContext *ctx)
149 {
150 StereoWidenContext *s = ctx->priv;
151
152 av_freep(&s->buffer);
153 }
154
155 static const AVFilterPad inputs[] = {
156 {
157 .name = "default",
158 .type = AVMEDIA_TYPE_AUDIO,
159 .filter_frame = filter_frame,
160 .config_props = config_input,
161 },
162 };
163
164 const AVFilter ff_af_stereowiden = {
165 .name = "stereowiden",
166 .description = NULL_IF_CONFIG_SMALL("Apply stereo widening effect."),
167 .priv_size = sizeof(StereoWidenContext),
168 .priv_class = &stereowiden_class,
169 .uninit = uninit,
170 FILTER_INPUTS(inputs),
171 FILTER_OUTPUTS(ff_audio_default_filterpad),
172 FILTER_QUERY_FUNC2(query_formats),
173 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
174 .process_command = ff_filter_process_command,
175 };
176