blob: 086549a93d13f83a1c52b36456f1fb883dee5bce
1 | /* |
2 | * Copyright (c) 2016 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 CrystalizerContext { |
28 | const AVClass *class; |
29 | float mult; |
30 | int clip; |
31 | AVFrame *prev; |
32 | } CrystalizerContext; |
33 | |
34 | #define OFFSET(x) offsetof(CrystalizerContext, x) |
35 | #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
36 | |
37 | static const AVOption crystalizer_options[] = { |
38 | { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 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(crystalizer); |
44 | |
45 | static int query_formats(AVFilterContext *ctx) |
46 | { |
47 | AVFilterFormats *formats = NULL; |
48 | AVFilterChannelLayouts *layouts = NULL; |
49 | int ret; |
50 | |
51 | if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 || |
52 | (ret = ff_set_common_formats(ctx , formats )) < 0) |
53 | return ret; |
54 | |
55 | layouts = ff_all_channel_counts(); |
56 | if (!layouts) |
57 | return AVERROR(ENOMEM); |
58 | |
59 | ret = ff_set_common_channel_layouts(ctx, layouts); |
60 | if (ret < 0) |
61 | return ret; |
62 | |
63 | formats = ff_all_samplerates(); |
64 | return ff_set_common_samplerates(ctx, formats); |
65 | } |
66 | |
67 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
68 | { |
69 | AVFilterContext *ctx = inlink->dst; |
70 | AVFilterLink *outlink = ctx->outputs[0]; |
71 | CrystalizerContext *s = ctx->priv; |
72 | const float *src = (const float *)in->data[0]; |
73 | const float mult = s->mult; |
74 | AVFrame *out; |
75 | float *dst, *prv; |
76 | int n, c; |
77 | |
78 | if (!s->prev) { |
79 | s->prev = ff_get_audio_buffer(inlink, 1); |
80 | if (!s->prev) { |
81 | av_frame_free(&in); |
82 | return AVERROR(ENOMEM); |
83 | } |
84 | } |
85 | |
86 | if (av_frame_is_writable(in)) { |
87 | out = in; |
88 | } else { |
89 | out = ff_get_audio_buffer(inlink, in->nb_samples); |
90 | if (!out) { |
91 | av_frame_free(&in); |
92 | return AVERROR(ENOMEM); |
93 | } |
94 | av_frame_copy_props(out, in); |
95 | } |
96 | |
97 | dst = (float *)out->data[0]; |
98 | prv = (float *)s->prev->data[0]; |
99 | |
100 | for (n = 0; n < in->nb_samples; n++) { |
101 | for (c = 0; c < in->channels; c++) { |
102 | float current = src[c]; |
103 | |
104 | dst[c] = current + (current - prv[c]) * mult; |
105 | prv[c] = current; |
106 | if (s->clip) { |
107 | dst[c] = av_clipf(dst[c], -1, 1); |
108 | } |
109 | } |
110 | dst += c; |
111 | src += c; |
112 | } |
113 | |
114 | if (out != in) |
115 | av_frame_free(&in); |
116 | |
117 | return ff_filter_frame(outlink, out); |
118 | } |
119 | |
120 | static av_cold void uninit(AVFilterContext *ctx) |
121 | { |
122 | CrystalizerContext *s = ctx->priv; |
123 | |
124 | av_frame_free(&s->prev); |
125 | } |
126 | |
127 | static const AVFilterPad inputs[] = { |
128 | { |
129 | .name = "default", |
130 | .type = AVMEDIA_TYPE_AUDIO, |
131 | .filter_frame = filter_frame, |
132 | }, |
133 | { NULL } |
134 | }; |
135 | |
136 | static const AVFilterPad outputs[] = { |
137 | { |
138 | .name = "default", |
139 | .type = AVMEDIA_TYPE_AUDIO, |
140 | }, |
141 | { NULL } |
142 | }; |
143 | |
144 | AVFilter ff_af_crystalizer = { |
145 | .name = "crystalizer", |
146 | .description = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."), |
147 | .query_formats = query_formats, |
148 | .priv_size = sizeof(CrystalizerContext), |
149 | .priv_class = &crystalizer_class, |
150 | .uninit = uninit, |
151 | .inputs = inputs, |
152 | .outputs = outputs, |
153 | }; |
154 |