Go to the documentation of this file.
101 #define OFFSET(x) offsetof(LoudNormContext, x)
102 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
138 double total_weight = 0.0;
139 const double sigma = 3.5;
143 const int offset = 21 / 2;
144 const double c1 = 1.0 / (sigma * sqrt(2.0 *
M_PI));
145 const double c2 = 2.0 * pow(sigma, 2.0);
147 for (
i = 0;
i < 21;
i++) {
149 s->weights[
i] =
c1 *
exp(-(pow(x, 2.0) /
c2));
150 total_weight +=
s->weights[
i];
153 adjust = 1.0 / total_weight;
154 for (
i = 0;
i < 21;
i++)
164 for (
i = 0;
i < 21;
i++)
177 buf =
s->limiter_buf;
178 ceiling =
s->target_tp;
181 if (
index >=
s->limiter_buf_size)
182 index -=
s->limiter_buf_size;
189 for (n = 0; n < nb_samples; n++) {
191 double this, next, max_peak;
196 if ((
s->prev_smp[
c] <=
this) && (next <=
this) && (
this > ceiling) && (n > 0)) {
200 for (
i = 2;
i < 12;
i++) {
220 *peak_value = max_peak;
224 s->prev_smp[
c] =
this;
228 if (
index >=
s->limiter_buf_size)
229 index -=
s->limiter_buf_size;
235 int n,
c,
index, peak_delta, smp_cnt;
236 double ceiling, peak_value;
239 buf =
s->limiter_buf;
240 ceiling =
s->target_tp;
241 index =
s->limiter_buf_index;
248 for (n = 0; n < 1920; n++) {
256 s->gain_reduction[1] = ceiling /
max;
258 buf =
s->limiter_buf;
260 for (n = 0; n < 1920; n++) {
263 env =
s->gain_reduction[1];
270 buf =
s->limiter_buf;
275 switch(
s->limiter_state) {
278 if (peak_delta != -1) {
280 smp_cnt += (peak_delta -
s->attack_length);
281 s->gain_reduction[0] = 1.;
282 s->gain_reduction[1] = ceiling / peak_value;
285 s->env_index =
s->peak_index - (
s->attack_length *
channels);
286 if (
s->env_index < 0)
287 s->env_index +=
s->limiter_buf_size;
290 if (
s->env_index >
s->limiter_buf_size)
291 s->env_index -=
s->limiter_buf_size;
294 smp_cnt = nb_samples;
299 for (;
s->env_cnt <
s->attack_length;
s->env_cnt++) {
302 env =
s->gain_reduction[0] - ((
double)
s->env_cnt / (
s->attack_length - 1) * (
s->gain_reduction[0] -
s->gain_reduction[1]));
303 buf[
s->env_index +
c] *= env;
307 if (
s->env_index >=
s->limiter_buf_size)
308 s->env_index -=
s->limiter_buf_size;
311 if (smp_cnt >= nb_samples) {
317 if (smp_cnt < nb_samples) {
319 s->attack_length = 1920;
326 if (peak_delta == -1) {
328 s->gain_reduction[0] =
s->gain_reduction[1];
329 s->gain_reduction[1] = 1.;
333 double gain_reduction;
334 gain_reduction = ceiling / peak_value;
336 if (gain_reduction < s->gain_reduction[1]) {
339 s->attack_length = peak_delta;
340 if (
s->attack_length <= 1)
341 s->attack_length = 2;
343 s->gain_reduction[0] =
s->gain_reduction[1];
344 s->gain_reduction[1] = gain_reduction;
349 for (
s->env_cnt = 0;
s->env_cnt < peak_delta;
s->env_cnt++) {
352 env =
s->gain_reduction[1];
353 buf[
s->env_index +
c] *= env;
357 if (
s->env_index >=
s->limiter_buf_size)
358 s->env_index -=
s->limiter_buf_size;
361 if (smp_cnt >= nb_samples) {
370 for (;
s->env_cnt <
s->release_length;
s->env_cnt++) {
373 env =
s->gain_reduction[0] + (((
double)
s->env_cnt / (
s->release_length - 1)) * (
s->gain_reduction[1] -
s->gain_reduction[0]));
374 buf[
s->env_index +
c] *= env;
378 if (
s->env_index >=
s->limiter_buf_size)
379 s->env_index -=
s->limiter_buf_size;
382 if (smp_cnt >= nb_samples) {
388 if (smp_cnt < nb_samples) {
390 s->limiter_state =
OUT;
396 }
while (smp_cnt < nb_samples);
398 for (n = 0; n < nb_samples; n++) {
402 out[
c] = ceiling * (
out[
c] < 0 ? -1 : 1);
407 if (
index >=
s->limiter_buf_size)
408 index -=
s->limiter_buf_size;
422 int i, n,
c, subframe_length, src_index;
423 double gain, gain_next, env_global, env_shortterm,
424 global, shortterm, lra, relative_threshold;
437 out->pts =
s->pts[0];
440 src = (
const double *)in->
data[0];
441 dst = (
double *)
out->data[0];
443 limiter_buf =
s->limiter_buf;
448 double offset, offset_tp, true_peak;
451 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
454 if (
c == 0 ||
tmp > true_peak)
458 offset = pow(10., (
s->target_i - global) / 20.);
459 offset_tp = true_peak *
offset;
460 s->offset = offset_tp <
s->target_tp ?
offset :
s->target_tp / true_peak;
464 switch (
s->frame_type) {
467 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
468 buf[
s->buf_index +
c] =
src[
c];
471 s->buf_index +=
inlink->ch_layout.nb_channels;
476 if (shortterm < s->measured_thresh) {
477 s->above_threshold = 0;
478 env_shortterm = shortterm <= -70. ? 0. :
s->target_i -
s->measured_i;
480 s->above_threshold = 1;
481 env_shortterm = shortterm <= -70. ? 0. :
s->target_i - shortterm;
484 for (n = 0; n < 30; n++)
485 s->delta[n] = pow(10., env_shortterm / 20.);
486 s->prev_delta =
s->delta[
s->index];
489 s->limiter_buf_index = 0;
491 for (n = 0; n < (
s->limiter_buf_size /
inlink->ch_layout.nb_channels); n++) {
492 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
493 limiter_buf[
s->limiter_buf_index +
c] = buf[
s->buf_index +
c] *
s->delta[
s->index] *
s->offset;
495 s->limiter_buf_index +=
inlink->ch_layout.nb_channels;
496 if (
s->limiter_buf_index >=
s->limiter_buf_size)
497 s->limiter_buf_index -=
s->limiter_buf_size;
499 s->buf_index +=
inlink->ch_layout.nb_channels;
506 out->nb_samples = subframe_length;
516 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
517 buf[
s->prev_buf_index +
c] =
src[
c];
518 limiter_buf[
s->limiter_buf_index +
c] = buf[
s->buf_index +
c] * (gain + (((
double) n / in->
nb_samples) * (gain_next - gain))) *
s->offset;
522 s->limiter_buf_index +=
inlink->ch_layout.nb_channels;
523 if (
s->limiter_buf_index >=
s->limiter_buf_size)
524 s->limiter_buf_index -=
s->limiter_buf_size;
526 s->prev_buf_index +=
inlink->ch_layout.nb_channels;
527 if (
s->prev_buf_index >=
s->buf_size)
528 s->prev_buf_index -=
s->buf_size;
530 s->buf_index +=
inlink->ch_layout.nb_channels;
531 if (
s->buf_index >=
s->buf_size)
532 s->buf_index -=
s->buf_size;
536 s->limiter_buf_index =
s->limiter_buf_index + subframe_length < s->limiter_buf_size ?
s->limiter_buf_index + subframe_length :
s->limiter_buf_index + subframe_length -
s->limiter_buf_size;
546 if (
s->above_threshold == 0) {
547 double shortterm_out;
549 if (shortterm >
s->measured_thresh)
550 s->prev_delta *= 1.0058;
553 if (shortterm_out >=
s->target_i)
554 s->above_threshold = 1;
557 if (shortterm < relative_threshold || shortterm <= -70. || s->above_threshold == 0) {
558 s->delta[
s->index] =
s->prev_delta;
560 env_global =
fabs(shortterm - global) < (
s->target_lra / 2.) ? shortterm - global : (
s->target_lra / 2.) * ((shortterm - global) < 0 ? -1 : 1);
561 env_shortterm =
s->target_i - shortterm;
562 s->delta[
s->index] = pow(10., (env_global + env_shortterm) / 20.);
565 s->prev_delta =
s->delta[
s->index];
574 s->limiter_buf_index = 0;
577 for (n = 0; n <
s->limiter_buf_size /
inlink->ch_layout.nb_channels; n++) {
578 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
579 s->limiter_buf[
s->limiter_buf_index +
c] =
src[src_index +
c] * gain *
s->offset;
581 src_index +=
inlink->ch_layout.nb_channels;
583 s->limiter_buf_index +=
inlink->ch_layout.nb_channels;
584 if (
s->limiter_buf_index >=
s->limiter_buf_size)
585 s->limiter_buf_index -=
s->limiter_buf_size;
592 for (n = 0; n < subframe_length; n++) {
593 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
595 limiter_buf[
s->limiter_buf_index +
c] =
src[src_index +
c] * gain *
s->offset;
597 limiter_buf[
s->limiter_buf_index +
c] = 0.;
602 src_index +=
inlink->ch_layout.nb_channels;
604 s->limiter_buf_index +=
inlink->ch_layout.nb_channels;
605 if (
s->limiter_buf_index >=
s->limiter_buf_size)
606 s->limiter_buf_index -=
s->limiter_buf_size;
609 dst += (subframe_length *
inlink->ch_layout.nb_channels);
612 dst = (
double *)
out->data[0];
618 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
619 dst[
c] =
src[
c] *
s->offset;
622 dst +=
inlink->ch_layout.nb_channels;
625 dst = (
double *)
out->data[0];
648 nb_samples = (
s->buf_size /
inlink->ch_layout.nb_channels) -
s->prev_nb_samples;
654 frame->nb_samples = nb_samples;
659 offset = ((
s->limiter_buf_size /
inlink->ch_layout.nb_channels) -
s->prev_nb_samples) *
inlink->ch_layout.nb_channels;
661 s->buf_index =
s->buf_index - offset < 0 ? s->buf_index -
offset +
s->buf_size :
s->buf_index -
offset;
663 for (n = 0; n < nb_samples; n++) {
664 for (
c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
665 src[
c] = buf[
s->buf_index +
c];
668 s->buf_index +=
inlink->ch_layout.nb_channels;
669 if (
s->buf_index >=
s->buf_size)
670 s->buf_index -=
s->buf_size;
711 s->pts[
i] = in->
pts +
i * nb_samples;
735 static const int input_srate[] = {192000, -1};
768 if (
inlink->ch_layout.nb_channels == 1 &&
s->dual_mono) {
791 s->limiter_buf_index = 0;
792 s->channels =
inlink->ch_layout.nb_channels;
794 s->limiter_state =
OUT;
795 s->offset = pow(10.,
s->offset / 20.);
796 s->target_tp = pow(10.,
s->target_tp / 20.);
810 offset =
s->target_i -
s->measured_i;
811 offset_tp =
s->measured_tp +
offset;
813 if (
s->measured_tp != 99 &&
s->measured_thresh != -70 &&
s->measured_lra != 0 &&
s->measured_i != 0) {
814 if ((offset_tp <= s->target_tp) && (
s->measured_lra <=
s->target_lra)) {
827 double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
830 if (!
s->r128_in || !
s->r128_out)
836 for (
c = 0;
c <
s->channels;
c++) {
839 if ((
c == 0) || (
tmp > tp_in))
846 for (
c = 0;
c <
s->channels;
c++) {
849 if ((
c == 0) || (
tmp > tp_out))
853 switch(
s->print_format) {
860 "\t\"input_i\" : \"%.2f\",\n"
861 "\t\"input_tp\" : \"%.2f\",\n"
862 "\t\"input_lra\" : \"%.2f\",\n"
863 "\t\"input_thresh\" : \"%.2f\",\n"
864 "\t\"output_i\" : \"%.2f\",\n"
865 "\t\"output_tp\" : \"%+.2f\",\n"
866 "\t\"output_lra\" : \"%.2f\",\n"
867 "\t\"output_thresh\" : \"%.2f\",\n"
868 "\t\"normalization_type\" : \"%s\",\n"
869 "\t\"target_offset\" : \"%.2f\"\n"
887 "Input Integrated: %+6.1f LUFS\n"
888 "Input True Peak: %+6.1f dBTP\n"
889 "Input LRA: %6.1f LU\n"
890 "Input Threshold: %+6.1f LUFS\n"
892 "Output Integrated: %+6.1f LUFS\n"
893 "Output True Peak: %+6.1f dBTP\n"
894 "Output LRA: %6.1f LU\n"
895 "Output Threshold: %+6.1f LUFS\n"
897 "Normalization Type: %s\n"
898 "Target Offset: %+6.1f LU\n",
935 .priv_class = &loudnorm_class,
static av_cold int init(AVFilterContext *ctx)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
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 void uninit(AVFilterContext *ctx)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static enum AVSampleFormat sample_fmts[]
static int frame_size(int sample_rate, int frame_len_msec)
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.
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).
#define FILTER_QUERY_FUNC(func)
static int linear(InterplayACMContext *s, unsigned ind, unsigned col)
@ FF_EBUR128_MODE_I
can call ff_ebur128_loudness_global_* and ff_ebur128_relative_threshold
enum PrintFormat print_format
const char * name
Filter name.
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
int ff_ebur128_loudness_range(FFEBUR128State *st, double *out)
Get loudness range (LRA) of programme in LU.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
void ff_ebur128_destroy(FFEBUR128State **st)
Destroy library state.
static void true_peak_limiter(LoudNormContext *s, double *out, int nb_samples, int channels)
A filter pad used for either input or output.
@ FF_EBUR128_DUAL_MONO
a channel that is counted twice
static int flush_frame(AVFilterLink *outlink)
#define FF_ARRAY_ELEMS(a)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
@ FF_EBUR128_MODE_LRA
can call ff_ebur128_loudness_range
void ff_ebur128_add_frames_double(FFEBUR128State *st, const double *src, size_t frames)
Add frames to be processed.
static int adjust(int x, int size)
enum LimiterState limiter_state
#define FILTER_INPUTS(array)
FrameType
G723.1 frame types.
const AVFilter ff_af_loudnorm
Describe the class of an AVClass context structure.
and forward the result(frame or status change) to the corresponding input. If nothing is possible
static __device__ float fabs(float a)
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
int ff_ebur128_sample_peak(FFEBUR128State *st, unsigned int channel_number, double *out)
Get maximum sample peak of selected channel in float format.
static const AVOption loudnorm_options[]
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
static int activate(AVFilterContext *ctx)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
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
static int query_formats(AVFilterContext *ctx)
AVFILTER_DEFINE_CLASS(loudnorm)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_ebur128_loudness_shortterm(FFEBUR128State *st, double *out)
Get short-term loudness (last 3s) in LUFS.
static void init_gaussian_filter(LoudNormContext *s)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
AVFilterContext * src
source filter
@ FF_EBUR128_MODE_S
can call ff_ebur128_loudness_shortterm
FFEBUR128State * ff_ebur128_init(unsigned int channels, unsigned long samplerate, unsigned long window, int mode)
Initialize library state.
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
FF_FILTER_FORWARD_WANTED(outlink, inlink)
#define AV_LOG_INFO
Standard information.
enum FrameType frame_type
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
int ff_ebur128_set_channel(FFEBUR128State *st, unsigned int channel_number, int value)
Set channel type.
static av_always_inline av_const double round(double x)
libebur128 - a library for loudness measurement according to the EBU R128 standard.
#define av_malloc_array(a, b)
AVSampleFormat
Audio sample formats.
Contains information about the state of a loudness measurement.
const char * name
Pad name.
static const AVFilterPad avfilter_af_loudnorm_inputs[]
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 config_input(AVFilterLink *inlink)
int ff_ebur128_relative_threshold(FFEBUR128State *st, double *out)
Get relative threshold in LUFS.
#define FILTER_OUTPUTS(array)
@ FF_EBUR128_MODE_SAMPLE_PEAK
can call ff_ebur128_sample_peak
static void detect_peak(LoudNormContext *s, int offset, int nb_samples, int channels, int *peak_delta, double *peak_value)
static double gaussian_filter(LoudNormContext *s, int index)
FFEBUR128State * r128_out
@ AV_SAMPLE_FMT_DBL
double
int ff_ebur128_loudness_global(FFEBUR128State *st, double *out)
Get global integrated loudness in LUFS.