FFmpeg: libavfilter/af_alimiter.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3 * Copyright (c) 2015 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
22 /**
23 * @file
24 * Lookahead limiter filter
25 */
26
32
36
41
44
57
63
69
71
77
78 #define OFFSET(x) offsetof(AudioLimiterContext, x)
79 #define AF AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
80
92 };
93
95
97 {
99
104 s->asc_coeff = pow(0.5,
s->asc_coeff - 0.5) * 2 * -1;
105
106 return 0;
107 }
108
110 double peak,
double limit,
double patt,
int asc)
111 {
112 double rdelta = (1.0 -
patt) / (sample_rate * release);
113
114 if (asc &&
s->auto_release &&
s->asc_c > 0) {
115 double a_att =
limit / (
s->asc_coeff *
s->asc) * (
double)
s->asc_c;
116
118 double delta =
FFMAX((a_att -
patt) / (sample_rate * release), rdelta / 10);
119
122 }
123 }
124
125 return rdelta;
126 }
127
129 {
133 const double *
src = (
const double *)in->
data[0];
135 const int buffer_size =
s->buffer_size;
137 const double release =
s->release;
138 const double limit =
s->limit;
139 double *nextdelta =
s->nextdelta;
141 const double level_out =
s->level_out;
142 const double level_in =
s->level_in;
143 int *nextpos =
s->nextpos;
145 double *buf;
147 int new_out_samples;
152
155 } else {
160 }
162 }
163 dst = (
double *)
out->data[0];
164
166 double peak = 0;
167
170
173 }
174
175 if (
s->auto_release && peak >
limit) {
178 }
179
185 int found = 0;
186
187 if (delta < s->
delta) {
190 nextpos[1] = -1;
191 nextdelta[0] = rdelta;
194 } else {
195 for (
i =
s->nextiter; i < s->nextiter +
s->nextlen;
i++) {
196 int j =
i % buffer_size;
197 double ppeak = 0, pdelta;
198
199 if (nextpos[j] >= 0)
202 }
203 pdelta = (
limit / peak -
limit / ppeak) / (((buffer_size - nextpos[j] +
s->pos) % buffer_size) /
channels);
204 if (pdelta < nextdelta[j]) {
205 nextdelta[j] = pdelta;
206 found = 1;
207 break;
208 }
209 }
210 if (found) {
211 s->nextlen =
i -
s->nextiter + 1;
212 nextpos[(
s->nextiter +
s->nextlen) % buffer_size] =
s->pos;
213 nextdelta[(
s->nextiter +
s->nextlen) % buffer_size] = rdelta;
214 nextpos[(
s->nextiter +
s->nextlen + 1) % buffer_size] = -1;
216 }
217 }
218 }
219
220 buf = &
s->buffer[(
s->pos +
channels) % buffer_size];
221 peak = 0;
224
226 }
227
228 if (
s->pos ==
s->asc_pos && !
s->asc_changed)
230
231 if (
s->auto_release &&
s->asc_pos == -1 && peak >
limit) {
234 }
235
237
240
241 if ((
s->pos +
channels) % buffer_size == nextpos[
s->nextiter]) {
242 if (
s->auto_release) {
245 if (
s->nextlen > 1) {
246 double ppeak = 0, pdelta;
247 int pnextpos = nextpos[(
s->nextiter + 1) % buffer_size];
248
251 }
252 pdelta = (
limit / ppeak -
s->att) /
253 (((buffer_size + pnextpos -
256 if (pdelta < s->
delta)
258 }
259 } else {
260 s->delta = nextdelta[
s->nextiter];
262 }
263
265 nextpos[
s->nextiter] = -1;
266 s->nextiter = (
s->nextiter + 1) % buffer_size;
267 }
268
274 nextpos[0] = -1;
275 }
276
278 s->att = 0.0000000000001;
279 s->delta = (1.0 -
s->att) / (
inlink->sample_rate * release);
280 }
281
282 if (
s->att != 1. && (1. -
s->att) < 0.0000000000001)
284
285 if (
s->delta != 0. &&
fabs(
s->delta) < 0.00000000000001)
287
290
294 }
295
302
303 new_out_samples =
out->nb_samples;
304 if (
s->in_trim > 0) {
305 int trim =
FFMIN(new_out_samples,
s->in_trim);
306 new_out_samples -= trim;
308 }
309
310 if (new_out_samples <= 0) {
312 return 0;
313 } else if (new_out_samples < out->nb_samples) {
314 int offset =
out->nb_samples - new_out_samples;
315 memmove(
out->extended_data[0],
out->extended_data[0] +
sizeof(
double) *
offset *
out->ch_layout.nb_channels,
316 sizeof(
double) * new_out_samples *
out->ch_layout.nb_channels);
317 out->nb_samples = new_out_samples;
319 }
320
322
325 in_pts = meta.pts;
326
329 out->pts =
s->next_out_pts;
330 } else {
332 }
333 s->next_in_pts = in_pts + in_duration;
334 s->next_out_pts =
out->pts + out_duration;
335
337 }
338
340 {
344
346
351
352 s->out_pad -=
frame->nb_samples;
353 frame->pts =
s->next_in_pts;
355 }
357 }
358
360 {
363 int obuffer_size;
364
365 obuffer_size =
inlink->sample_rate *
inlink->ch_layout.nb_channels * 100 / 1000. +
inlink->ch_layout.nb_channels;
366 if (obuffer_size < inlink->ch_layout.nb_channels)
368
369 s->buffer =
av_calloc(obuffer_size,
sizeof(*
s->buffer));
370 s->nextdelta =
av_calloc(obuffer_size,
sizeof(*
s->nextdelta));
372 if (!
s->buffer || !
s->nextdelta || !
s->nextpos)
374
375 memset(
s->nextpos, -1, obuffer_size *
sizeof(*
s->nextpos));
376 s->buffer_size =
inlink->sample_rate *
s->attack *
inlink->ch_layout.nb_channels;
377 s->buffer_size -=
s->buffer_size %
inlink->ch_layout.nb_channels;
379 s->in_trim =
s->out_pad =
s->buffer_size /
inlink->ch_layout.nb_channels - 1;
382
386 }
387
388 if (
s->buffer_size <= 0) {
391 }
392
393 return 0;
394 }
395
397 {
399
403
405 }
406
408 {
413 },
414 };
415
417 {
421 },
422 };
423
425 .
p.
name =
"alimiter",
427 .p.priv_class = &alimiter_class,
436 };
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
static const AVOption alimiter_options[]
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static av_cold int init(AVFilterContext *ctx)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static const AVFilterPad alimiter_inputs[]
static const AVFilterPad alimiter_outputs[]
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
const FFFilter ff_af_alimiter
const char * name
Filter name.
A link between two filters.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
A filter pad used for either input or output.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
static av_cold void uninit(AVFilterContext *ctx)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define FILTER_OUTPUTS(array)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Describe the class of an AVClass context structure.
static __device__ float fabs(float a)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
int sample_rate
Sample rate of the audio data.
static AVRational av_make_q(int num, int den)
Create an AVRational.
#define AV_NOPTS_VALUE
Undefined timestamp value.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
AVFilterContext * src
source filter
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
#define av_malloc_array(a, b)
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
static double limit(double x)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
static int request_frame(AVFilterLink *outlink)
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
static const int8_t patt[4]
static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate, double peak, double limit, double patt, int asc)
AVFilter p
The public AVFilter.
AVFILTER_DEFINE_CLASS(alimiter)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
static int config_input(AVFilterLink *inlink)
@ AV_SAMPLE_FMT_DBL
double
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Generated on Tue Nov 18 2025 19:22:59 for FFmpeg by
doxygen
1.8.17