FFmpeg
Loading...
Searching...
No Matches
af_acontrast.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Rob Sykes
3 * Copyright (c) 2017 Paul B Mahol
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
23#include "libavutil/opt.h"
24#include "avfilter.h"
25#include "audio.h"
26#include "filters.h"
27
28typedef struct AudioContrastContext {
29 const AVClass *class;
30 float contrast;
31 void (*filter)(void **dst, const void **src,
32 int nb_samples, int channels, float contrast);
34
35#define OFFSET(x) offsetof(AudioContrastContext, x)
36#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
37
38static const AVOption acontrast_options[] = {
39 { "contrast", "set contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, {.dbl=33}, 0, 100, A },
40 { NULL }
41};
42
44
45static void filter_flt(void **d, const void **s,
46 int nb_samples, int channels,
47 float contrast)
48{
49 const float *src = s[0];
50 float *dst = d[0];
51 int n, c;
52
53 for (n = 0; n < nb_samples; n++) {
54 for (c = 0; c < channels; c++) {
55 float d = src[c] * M_PI_2;
56
57 dst[c] = sinf(d + contrast * sinf(d * 4));
58 }
59
60 dst += c;
61 src += c;
62 }
63}
64
65static void filter_dbl(void **d, const void **s,
66 int nb_samples, int channels,
67 float contrast)
68{
69 const double *src = s[0];
70 double *dst = d[0];
71 int n, c;
72
73 for (n = 0; n < nb_samples; n++) {
74 for (c = 0; c < channels; c++) {
75 double d = src[c] * M_PI_2;
76
77 dst[c] = sin(d + contrast * sin(d * 4));
78 }
79
80 dst += c;
81 src += c;
82 }
83}
84
85static void filter_fltp(void **d, const void **s,
86 int nb_samples, int channels,
87 float contrast)
88{
89 int n, c;
90
91 for (c = 0; c < channels; c++) {
92 const float *src = s[c];
93 float *dst = d[c];
94
95 for (n = 0; n < nb_samples; n++) {
96 float d = src[n] * M_PI_2;
97
98 dst[n] = sinf(d + contrast * sinf(d * 4));
99 }
100 }
101}
102
103static void filter_dblp(void **d, const void **s,
104 int nb_samples, int channels,
105 float contrast)
106{
107 int n, c;
108
109 for (c = 0; c < channels; c++) {
110 const double *src = s[c];
111 double *dst = d[c];
112
113 for (n = 0; n < nb_samples; n++) {
114 double d = src[n] * M_PI_2;
115
116 dst[n] = sin(d + contrast * sin(d * 4));
117 }
118 }
119}
120
121static int config_input(AVFilterLink *inlink)
122{
123 AVFilterContext *ctx = inlink->dst;
124 AudioContrastContext *s = ctx->priv;
125
126 switch (inlink->format) {
127 case AV_SAMPLE_FMT_FLT: s->filter = filter_flt; break;
128 case AV_SAMPLE_FMT_DBL: s->filter = filter_dbl; break;
129 case AV_SAMPLE_FMT_FLTP: s->filter = filter_fltp; break;
130 case AV_SAMPLE_FMT_DBLP: s->filter = filter_dblp; break;
131 }
132
133 return 0;
134}
135
136static int filter_frame(AVFilterLink *inlink, AVFrame *in)
137{
138 AVFilterContext *ctx = inlink->dst;
139 AVFilterLink *outlink = ctx->outputs[0];
140 AudioContrastContext *s = ctx->priv;
141 AVFrame *out;
142
143 if (av_frame_is_writable(in)) {
144 out = in;
145 } else {
146 out = ff_get_audio_buffer(outlink, in->nb_samples);
147 if (!out) {
148 av_frame_free(&in);
149 return AVERROR(ENOMEM);
150 }
152 }
153
154 s->filter((void **)out->extended_data, (const void **)in->extended_data,
155 in->nb_samples, in->ch_layout.nb_channels, s->contrast / 750);
156
157 if (out != in)
158 av_frame_free(&in);
159
160 return ff_filter_frame(outlink, out);
161}
162
163static const AVFilterPad inputs[] = {
164 {
165 .name = "default",
166 .type = AVMEDIA_TYPE_AUDIO,
167 .filter_frame = filter_frame,
168 .config_props = config_input,
169 },
170};
171
173 .p.name = "acontrast",
174 .p.description = NULL_IF_CONFIG_SMALL("Simple audio dynamic range compression/expansion filter."),
175 .p.priv_class = &acontrast_class,
176 .priv_size = sizeof(AudioContrastContext),
181};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const AVFilterPad inputs[]
Definition af_aap.c:299
static void filter_dblp(void **d, const void **s, int nb_samples, int channels, float contrast)
static int config_input(AVFilterLink *inlink)
static void filter_dbl(void **d, const void **s, int nb_samples, int channels, float contrast)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVOption acontrast_options[]
#define OFFSET(x)
const FFFilter ff_af_acontrast
static void filter_flt(void **d, const void **s, int nb_samples, int channels, float contrast)
static void filter_fltp(void **d, const void **s, int nb_samples, int channels, float contrast)
static FILE * out
static AVFormatContext * ctx
channels
Definition aptx.h:31
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define sinf(x)
Definition libm.h:421
#define M_PI_2
Definition mathematics.h:73
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
void(* filter)(void **dst, const void **src, int nb_samples, int channels, float contrast)
#define src
Definition vp8dsp.c:248
static double c[64]