FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_extrastereo.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 30 37 81.1%
Functions: 2 2 100.0%
Branches: 9 18 50.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 The FFmpeg Project
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26
27 typedef struct ExtraStereoContext {
28 const AVClass *class;
29 float mult;
30 int clip;
31 } ExtraStereoContext;
32
33 #define OFFSET(x) offsetof(ExtraStereoContext, x)
34 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
35
36 static const AVOption extrastereo_options[] = {
37 { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A },
38 { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
39 { NULL }
40 };
41
42 AVFILTER_DEFINE_CLASS(extrastereo);
43
44 1 static int query_formats(AVFilterContext *ctx)
45 {
46 1 AVFilterFormats *formats = NULL;
47 1 AVFilterChannelLayouts *layout = NULL;
48 int ret;
49
50
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
51
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
52
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
2 (ret = ff_add_channel_layout (&layout , &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
53 1 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0)
54 return ret;
55
56 1 return ff_set_common_all_samplerates(ctx);
57 }
58
59 20 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
60 {
61 20 AVFilterContext *ctx = inlink->dst;
62 20 AVFilterLink *outlink = ctx->outputs[0];
63 20 ExtraStereoContext *s = ctx->priv;
64 20 const float *src = (const float *)in->data[0];
65 20 const float mult = s->mult;
66 AVFrame *out;
67 float *dst;
68 int n;
69
70
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (av_frame_is_writable(in)) {
71 20 out = in;
72 } else {
73 out = ff_get_audio_buffer(outlink, in->nb_samples);
74 if (!out) {
75 av_frame_free(&in);
76 return AVERROR(ENOMEM);
77 }
78 av_frame_copy_props(out, in);
79 }
80 20 dst = (float *)out->data[0];
81
82
2/2
✓ Branch 0 taken 20480 times.
✓ Branch 1 taken 20 times.
20500 for (n = 0; n < in->nb_samples; n++) {
83 float average, left, right;
84
85 20480 left = src[n * 2 ];
86 20480 right = src[n * 2 + 1];
87 20480 average = (left + right) / 2.;
88 20480 left = average + mult * (left - average);
89 20480 right = average + mult * (right - average);
90
91
1/2
✓ Branch 0 taken 20480 times.
✗ Branch 1 not taken.
20480 if (s->clip) {
92 20480 left = av_clipf(left, -1, 1);
93 20480 right = av_clipf(right, -1, 1);
94 }
95
96 20480 dst[n * 2 ] = left;
97 20480 dst[n * 2 + 1] = right;
98 }
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (out != in)
101 av_frame_free(&in);
102 20 return ff_filter_frame(outlink, out);
103 }
104
105 static const AVFilterPad inputs[] = {
106 {
107 .name = "default",
108 .type = AVMEDIA_TYPE_AUDIO,
109 .filter_frame = filter_frame,
110 },
111 };
112
113 static const AVFilterPad outputs[] = {
114 {
115 .name = "default",
116 .type = AVMEDIA_TYPE_AUDIO,
117 },
118 };
119
120 const AVFilter ff_af_extrastereo = {
121 .name = "extrastereo",
122 .description = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
123 .priv_size = sizeof(ExtraStereoContext),
124 .priv_class = &extrastereo_class,
125 FILTER_INPUTS(inputs),
126 FILTER_OUTPUTS(outputs),
127 FILTER_QUERY_FUNC(query_formats),
128 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
129 .process_command = ff_filter_process_command,
130 };
131