FFmpeg
Loading...
Searching...
No Matches
af_stereowiden.c
Go to the documentation of this file.
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
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
30typedef 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;
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
47static 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
56
58 AVFilterFormatsConfig **cfg_in,
59 AVFilterFormatsConfig **cfg_out)
60{
61 static const enum AVSampleFormat formats[] = {
64 };
65 static const AVChannelLayout layouts[] = {
67 { .nb_channels = 0 },
68 };
69
70 int ret;
71
72 ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, formats);
73 if (ret < 0)
74 return ret;
75
77 if (ret < 0)
78 return ret;
79
80 return 0;
81}
82
83static 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
100static 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 }
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
149{
150 StereoWidenContext *s = ctx->priv;
151
152 av_freep(&s->buffer);
153}
154
155static 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
165 .p.name = "stereowiden",
166 .p.description = NULL_IF_CONFIG_SMALL("Apply stereo widening effect."),
167 .p.priv_class = &stereowiden_class,
169 .priv_size = sizeof(StereoWidenContext),
170 .uninit = uninit,
174 .process_command = ff_filter_process_command,
175};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
#define AT
Definition af_aap.c:76
static int config_input(AVFilterLink *inlink)
static int config_input(AVFilterLink *inlink)
static const AVOption stereowiden_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
const FFFilter ff_af_stereowiden
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
Main libavfilter public API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AV_CHANNEL_LAYOUT_STEREO
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define lrintf(x)
Definition libm_mips.h:72
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
AVOptions.
formats
Definition signature.h:47
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49