FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_dcshift.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 23 41 56.1%
Functions: 2 2 100.0%
Branches: 9 24 37.5%

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 20 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
56 {
57 20 AVFilterContext *ctx = inlink->dst;
58 20 AVFilterLink *outlink = ctx->outputs[0];
59 AVFrame *out;
60 20 DCShiftContext *s = ctx->priv;
61 int i, j;
62 20 double dcshift = s->dcshift;
63
64
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (av_frame_is_writable(in)) {
65 20 out = in;
66 } else {
67 out = ff_get_audio_buffer(outlink, in->nb_samples);
68 if (!out) {
69 av_frame_free(&in);
70 return AVERROR(ENOMEM);
71 }
72 av_frame_copy_props(out, in);
73 }
74
75
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (s->limitergain > 0) {
76
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
60 for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
77 40 const int32_t *src = (int32_t *)in->extended_data[i];
78 40 int32_t *dst = (int32_t *)out->extended_data[i];
79
80
2/2
✓ Branch 0 taken 40960 times.
✓ Branch 1 taken 40 times.
41000 for (j = 0; j < in->nb_samples; j++) {
81 double d;
82
83 40960 d = src[j];
84
85
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40960 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
40960 if (d > s->limiterthreshold && dcshift > 0) {
86 d = (d - s->limiterthreshold) * s->limitergain /
87 (INT32_MAX - s->limiterthreshold) +
88 s->limiterthreshold + dcshift;
89
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40960 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
40960 } else if (d < -s->limiterthreshold && dcshift < 0) {
90 d = (d + s->limiterthreshold) * s->limitergain /
91 (INT32_MAX - s->limiterthreshold) -
92 s->limiterthreshold + dcshift;
93 } else {
94 40960 d = dcshift * INT32_MAX + d;
95 }
96
97 40960 dst[j] = av_clipl_int32(d);
98 }
99 }
100 } else {
101 for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
102 const int32_t *src = (int32_t *)in->extended_data[i];
103 int32_t *dst = (int32_t *)out->extended_data[i];
104
105 for (j = 0; j < in->nb_samples; j++) {
106 double d = dcshift * (INT32_MAX + 1.) + src[j];
107
108 dst[j] = av_clipl_int32(d);
109 }
110 }
111 }
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (out != in)
114 av_frame_free(&in);
115 20 return ff_filter_frame(outlink, out);
116 }
117 static const AVFilterPad dcshift_inputs[] = {
118 {
119 .name = "default",
120 .type = AVMEDIA_TYPE_AUDIO,
121 .filter_frame = filter_frame,
122 },
123 };
124
125 static const AVFilterPad dcshift_outputs[] = {
126 {
127 .name = "default",
128 .type = AVMEDIA_TYPE_AUDIO,
129 },
130 };
131
132 const AVFilter ff_af_dcshift = {
133 .name = "dcshift",
134 .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
135 .priv_size = sizeof(DCShiftContext),
136 .priv_class = &dcshift_class,
137 .init = init,
138 FILTER_INPUTS(dcshift_inputs),
139 FILTER_OUTPUTS(dcshift_outputs),
140 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_S32P),
141 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
142 };
143