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 "filters.h" | ||
26 | #include "formats.h" | ||
27 | |||
28 | typedef struct ExtraStereoContext { | ||
29 | const AVClass *class; | ||
30 | float mult; | ||
31 | int clip; | ||
32 | } ExtraStereoContext; | ||
33 | |||
34 | #define OFFSET(x) offsetof(ExtraStereoContext, x) | ||
35 | #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
36 | |||
37 | static const AVOption extrastereo_options[] = { | ||
38 | { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A }, | ||
39 | { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A }, | ||
40 | { NULL } | ||
41 | }; | ||
42 | |||
43 | AVFILTER_DEFINE_CLASS(extrastereo); | ||
44 | |||
45 | 1 | static int query_formats(const AVFilterContext *ctx, | |
46 | AVFilterFormatsConfig **cfg_in, | ||
47 | AVFilterFormatsConfig **cfg_out) | ||
48 | { | ||
49 | static const enum AVSampleFormat formats[] = { | ||
50 | AV_SAMPLE_FMT_FLT, | ||
51 | AV_SAMPLE_FMT_NONE, | ||
52 | }; | ||
53 | static const AVChannelLayout layouts[] = { | ||
54 | AV_CHANNEL_LAYOUT_STEREO, | ||
55 | { .nb_channels = 0 }, | ||
56 | }; | ||
57 | |||
58 | int ret; | ||
59 | |||
60 | 1 | ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats); | |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
62 | ✗ | return ret; | |
63 | |||
64 | 1 | ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts); | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
66 | ✗ | return ret; | |
67 | |||
68 | 1 | return 0; | |
69 | } | ||
70 | |||
71 | 20 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
72 | { | ||
73 | 20 | AVFilterContext *ctx = inlink->dst; | |
74 | 20 | AVFilterLink *outlink = ctx->outputs[0]; | |
75 | 20 | ExtraStereoContext *s = ctx->priv; | |
76 | 20 | const float *src = (const float *)in->data[0]; | |
77 | 20 | const float mult = s->mult; | |
78 | AVFrame *out; | ||
79 | float *dst; | ||
80 | int n; | ||
81 | |||
82 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | if (av_frame_is_writable(in)) { |
83 | 20 | out = in; | |
84 | } else { | ||
85 | ✗ | out = ff_get_audio_buffer(outlink, in->nb_samples); | |
86 | ✗ | if (!out) { | |
87 | ✗ | av_frame_free(&in); | |
88 | ✗ | return AVERROR(ENOMEM); | |
89 | } | ||
90 | ✗ | av_frame_copy_props(out, in); | |
91 | } | ||
92 | 20 | dst = (float *)out->data[0]; | |
93 | |||
94 |
2/2✓ Branch 0 taken 81920 times.
✓ Branch 1 taken 20 times.
|
81940 | for (n = 0; n < in->nb_samples; n++) { |
95 | float average, left, right; | ||
96 | |||
97 | 81920 | left = src[n * 2 ]; | |
98 | 81920 | right = src[n * 2 + 1]; | |
99 | 81920 | average = (left + right) / 2.; | |
100 | 81920 | left = average + mult * (left - average); | |
101 | 81920 | right = average + mult * (right - average); | |
102 | |||
103 |
1/2✓ Branch 0 taken 81920 times.
✗ Branch 1 not taken.
|
81920 | if (s->clip) { |
104 | 81920 | left = av_clipf(left, -1, 1); | |
105 | 81920 | right = av_clipf(right, -1, 1); | |
106 | } | ||
107 | |||
108 | 81920 | dst[n * 2 ] = left; | |
109 | 81920 | dst[n * 2 + 1] = right; | |
110 | } | ||
111 | |||
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (out != in) |
113 | ✗ | av_frame_free(&in); | |
114 | 20 | return ff_filter_frame(outlink, out); | |
115 | } | ||
116 | |||
117 | static const AVFilterPad inputs[] = { | ||
118 | { | ||
119 | .name = "default", | ||
120 | .type = AVMEDIA_TYPE_AUDIO, | ||
121 | .filter_frame = filter_frame, | ||
122 | }, | ||
123 | }; | ||
124 | |||
125 | const AVFilter ff_af_extrastereo = { | ||
126 | .name = "extrastereo", | ||
127 | .description = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."), | ||
128 | .priv_size = sizeof(ExtraStereoContext), | ||
129 | .priv_class = &extrastereo_class, | ||
130 | FILTER_INPUTS(inputs), | ||
131 | FILTER_OUTPUTS(ff_audio_default_filterpad), | ||
132 | FILTER_QUERY_FUNC2(query_formats), | ||
133 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
134 | .process_command = ff_filter_process_command, | ||
135 | }; | ||
136 |