2 * Copyright (c) 2000 Chris Ausbrooks <weed@bucket.pp.ualr.edu>
3 * Copyright (c) 2000 Fabien COELHO <fabien@coelho.net>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/opt.h"
23 #include "libavutil/samplefmt.h"
28 typedef struct DCShiftContext {
31 double limiterthreshhold;
35 #define OFFSET(x) offsetof(DCShiftContext, x)
36 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
38 static const AVOption dcshift_options[] = {
39 { "shift", "set DC shift", OFFSET(dcshift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
40 { "limitergain", "set limiter gain", OFFSET(limitergain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, A },
44 AVFILTER_DEFINE_CLASS(dcshift);
46 static av_cold int init(AVFilterContext *ctx)
48 DCShiftContext *s = ctx->priv;
50 s->limiterthreshhold = INT32_MAX * (1.0 - (fabs(s->dcshift) - s->limitergain));
55 static int query_formats(AVFilterContext *ctx)
57 AVFilterChannelLayouts *layouts;
58 AVFilterFormats *formats;
59 static const enum AVSampleFormat sample_fmts[] = {
60 AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
63 layouts = ff_all_channel_layouts();
65 return AVERROR(ENOMEM);
66 ff_set_common_channel_layouts(ctx, layouts);
68 formats = ff_make_format_list(sample_fmts);
70 return AVERROR(ENOMEM);
71 ff_set_common_formats(ctx, formats);
73 formats = ff_all_samplerates();
75 return AVERROR(ENOMEM);
76 ff_set_common_samplerates(ctx, formats);
81 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
83 AVFilterContext *ctx = inlink->dst;
84 AVFilterLink *outlink = ctx->outputs[0];
85 AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
86 DCShiftContext *s = ctx->priv;
88 double dcshift = s->dcshift;
92 return AVERROR(ENOMEM);
94 av_frame_copy_props(out, in);
96 if (s->limitergain > 0) {
97 for (i = 0; i < inlink->channels; i++) {
98 const int32_t *src = (int32_t *)in->extended_data[i];
99 int32_t *dst = (int32_t *)out->extended_data[i];
101 for (j = 0; j < in->nb_samples; j++) {
106 if (d > s->limiterthreshhold && dcshift > 0) {
107 d = (d - s->limiterthreshhold) * s->limitergain /
108 (INT32_MAX - s->limiterthreshhold) +
109 s->limiterthreshhold + dcshift;
110 } else if (d < -s->limiterthreshhold && dcshift < 0) {
111 d = (d + s->limiterthreshhold) * s->limitergain /
112 (INT32_MAX - s->limiterthreshhold) -
113 s->limiterthreshhold + dcshift;
115 d = dcshift * INT32_MAX + d;
118 dst[j] = av_clipl_int32(d);
122 for (i = 0; i < inlink->channels; i++) {
123 const int32_t *src = (int32_t *)in->extended_data[i];
124 int32_t *dst = (int32_t *)out->extended_data[i];
126 for (j = 0; j < in->nb_samples; j++) {
127 double d = dcshift * (INT32_MAX + 1.) + src[j];
129 dst[j] = av_clipl_int32(d);
135 return ff_filter_frame(outlink, out);
137 static const AVFilterPad dcshift_inputs[] = {
140 .type = AVMEDIA_TYPE_AUDIO,
141 .filter_frame = filter_frame,
146 static const AVFilterPad dcshift_outputs[] = {
149 .type = AVMEDIA_TYPE_AUDIO,
154 AVFilter ff_af_dcshift = {
156 .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
157 .query_formats = query_formats,
158 .priv_size = sizeof(DCShiftContext),
159 .priv_class = &dcshift_class,
161 .inputs = dcshift_inputs,
162 .outputs = dcshift_outputs,
163 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,