GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_dcshift.c Lines: 35 58 60.3 %
Date: 2020-10-23 17:01:47 Branches: 14 34 41.2 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2000 Chris Ausbrooks <weed@bucket.pp.ualr.edu>
3
 * Copyright (c) 2000 Fabien COELHO <fabien@coelho.net>
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/opt.h"
23
#include "libavutil/samplefmt.h"
24
#include "avfilter.h"
25
#include "audio.h"
26
#include "internal.h"
27
28
typedef struct DCShiftContext {
29
    const AVClass *class;
30
    double dcshift;
31
    double limiterthreshold;
32
    double limitergain;
33
} DCShiftContext;
34
35
#define OFFSET(x) offsetof(DCShiftContext, x)
36
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
37
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 },
41
    { NULL }
42
};
43
44
AVFILTER_DEFINE_CLASS(dcshift);
45
46
1
static av_cold int init(AVFilterContext *ctx)
47
{
48
1
    DCShiftContext *s = ctx->priv;
49
50
1
    s->limiterthreshold = INT32_MAX * (1.0 - (fabs(s->dcshift) - s->limitergain));
51
52
1
    return 0;
53
}
54
55
1
static int query_formats(AVFilterContext *ctx)
56
{
57
    AVFilterChannelLayouts *layouts;
58
    AVFilterFormats *formats;
59
    static const enum AVSampleFormat sample_fmts[] = {
60
        AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
61
    };
62
    int ret;
63
64
1
    layouts = ff_all_channel_counts();
65
1
    if (!layouts)
66
        return AVERROR(ENOMEM);
67
1
    ret = ff_set_common_channel_layouts(ctx, layouts);
68
1
    if (ret < 0)
69
        return ret;
70
71
1
    formats = ff_make_format_list(sample_fmts);
72
1
    if (!formats)
73
        return AVERROR(ENOMEM);
74
1
    ret = ff_set_common_formats(ctx, formats);
75
1
    if (ret < 0)
76
        return ret;
77
78
1
    formats = ff_all_samplerates();
79
1
    if (!formats)
80
        return AVERROR(ENOMEM);
81
1
    return ff_set_common_samplerates(ctx, formats);
82
}
83
84
20
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
85
{
86
20
    AVFilterContext *ctx = inlink->dst;
87
20
    AVFilterLink *outlink = ctx->outputs[0];
88
    AVFrame *out;
89
20
    DCShiftContext *s = ctx->priv;
90
    int i, j;
91
20
    double dcshift = s->dcshift;
92
93
20
    if (av_frame_is_writable(in)) {
94
20
        out = in;
95
    } else {
96
        out = ff_get_audio_buffer(outlink, in->nb_samples);
97
        if (!out) {
98
            av_frame_free(&in);
99
            return AVERROR(ENOMEM);
100
        }
101
        av_frame_copy_props(out, in);
102
    }
103
104
20
    if (s->limitergain > 0) {
105
60
        for (i = 0; i < inlink->channels; i++) {
106
40
            const int32_t *src = (int32_t *)in->extended_data[i];
107
40
            int32_t *dst = (int32_t *)out->extended_data[i];
108
109
41000
            for (j = 0; j < in->nb_samples; j++) {
110
                double d;
111
112
40960
                d = src[j];
113
114

40960
                if (d > s->limiterthreshold && dcshift > 0) {
115
                    d = (d - s->limiterthreshold) * s->limitergain /
116
                             (INT32_MAX - s->limiterthreshold) +
117
                             s->limiterthreshold + dcshift;
118

40960
                } else if (d < -s->limiterthreshold && dcshift < 0) {
119
                    d = (d + s->limiterthreshold) * s->limitergain /
120
                             (INT32_MAX - s->limiterthreshold) -
121
                             s->limiterthreshold + dcshift;
122
                } else {
123
40960
                    d = dcshift * INT32_MAX + d;
124
                }
125
126
40960
                dst[j] = av_clipl_int32(d);
127
            }
128
        }
129
    } else {
130
        for (i = 0; i < inlink->channels; i++) {
131
            const int32_t *src = (int32_t *)in->extended_data[i];
132
            int32_t *dst = (int32_t *)out->extended_data[i];
133
134
            for (j = 0; j < in->nb_samples; j++) {
135
                double d = dcshift * (INT32_MAX + 1.) + src[j];
136
137
                dst[j] = av_clipl_int32(d);
138
            }
139
        }
140
    }
141
142
20
    if (out != in)
143
        av_frame_free(&in);
144
20
    return ff_filter_frame(outlink, out);
145
}
146
static const AVFilterPad dcshift_inputs[] = {
147
    {
148
        .name         = "default",
149
        .type         = AVMEDIA_TYPE_AUDIO,
150
        .filter_frame = filter_frame,
151
    },
152
    { NULL }
153
};
154
155
static const AVFilterPad dcshift_outputs[] = {
156
    {
157
        .name = "default",
158
        .type = AVMEDIA_TYPE_AUDIO,
159
    },
160
    { NULL }
161
};
162
163
AVFilter ff_af_dcshift = {
164
    .name           = "dcshift",
165
    .description    = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
166
    .query_formats  = query_formats,
167
    .priv_size      = sizeof(DCShiftContext),
168
    .priv_class     = &dcshift_class,
169
    .init           = init,
170
    .inputs         = dcshift_inputs,
171
    .outputs        = dcshift_outputs,
172
    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
173
};