Go to the documentation of this file.
33 #define MIN_FILTER_SIZE 3
34 #define MAX_FILTER_SIZE 301
36 #define FF_BUFQUEUE_SIZE (MAX_FILTER_SIZE + 1)
96 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
97 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
133 if (!(
s->filter_size & 1)) {
234 for (
int i = 0;
i < side;
i++)
238 int count = (q->
size - new_size + 1) / 2;
249 double total_weight = 0.0;
250 const double sigma = (((
s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
254 const int offset =
s->filter_size / 2;
255 const double c1 = 1.0 / (sigma * sqrt(2.0 *
M_PI));
256 const double c2 = 2.0 * sigma * sigma;
259 for (
int i = 0;
i <
s->filter_size;
i++) {
263 total_weight +=
s->weights[
i];
267 adjust = 1.0 / total_weight;
268 for (
int i = 0;
i <
s->filter_size;
i++) {
281 for (
int c = 0;
c <
s->channels;
c++) {
282 if (
s->gain_history_original)
284 if (
s->gain_history_minimum)
286 if (
s->gain_history_smoothed)
288 if (
s->threshold_history)
298 s->is_enabled =
NULL;
316 s->channels =
inlink->ch_layout.nb_channels;
320 s->prev_amplification_factor =
av_malloc_array(
inlink->ch_layout.nb_channels,
sizeof(*
s->prev_amplification_factor));
321 s->dc_correction_value =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->dc_correction_value));
322 s->compress_threshold =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->compress_threshold));
323 s->gain_history_original =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->gain_history_original));
324 s->gain_history_minimum =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->gain_history_minimum));
325 s->gain_history_smoothed =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->gain_history_smoothed));
326 s->threshold_history =
av_calloc(
inlink->ch_layout.nb_channels,
sizeof(*
s->threshold_history));
329 if (!
s->prev_amplification_factor || !
s->dc_correction_value ||
330 !
s->compress_threshold ||
331 !
s->gain_history_original || !
s->gain_history_minimum ||
332 !
s->gain_history_smoothed || !
s->threshold_history ||
333 !
s->is_enabled || !
s->weights)
336 for (
int c = 0;
c <
inlink->ch_layout.nb_channels;
c++) {
337 s->prev_amplification_factor[
c] = 1.0;
344 if (!
s->gain_history_original[
c] || !
s->gain_history_minimum[
c] ||
345 !
s->gain_history_smoothed[
c] || !
s->threshold_history[
c])
354 s->sample_advance =
FFMAX(1,
lrint(
s->frame_len * (1. -
s->overlap)));
359 static inline double fade(
double prev,
double next,
int pos,
int length)
361 const double step_size = 1.0 / length;
362 const double f0 = 1.0 - (step_size * (
pos + 1.0));
363 const double f1 = 1.0 - f0;
364 return f0 * prev + f1 * next;
372 static inline double bound(
const double threshold,
const double val)
374 const double CONST = 0.8862269254527580136490837416705725913987747280611935;
380 double max = DBL_EPSILON;
383 for (
int c = 0;
c <
frame->ch_layout.nb_channels;
c++) {
384 double *data_ptr = (
double *)
frame->extended_data[
c];
390 double *data_ptr = (
double *)
frame->extended_data[
channel];
401 double rms_value = 0.0;
404 for (
int c = 0;
c <
frame->ch_layout.nb_channels;
c++) {
405 const double *data_ptr = (
double *)
frame->extended_data[
c];
408 rms_value +=
pow_2(data_ptr[
i]);
412 rms_value /=
frame->nb_samples *
frame->ch_layout.nb_channels;
414 const double *data_ptr = (
double *)
frame->extended_data[
channel];
416 rms_value +=
pow_2(data_ptr[
i]);
419 rms_value /=
frame->nb_samples;
422 return fmax(sqrt(rms_value), DBL_EPSILON);
429 const double maximum_gain =
s->peak_value / peak_magnitude;
433 gain.
threshold = peak_magnitude >
s->threshold;
434 gain.max_gain =
bound(
s->max_amplification,
fmin(maximum_gain, rms_gain));
441 double min = DBL_MAX;
453 double result = 0.0, tsum = 0.0;
473 const int pre_fill_size =
s->filter_size / 2;
476 s->prev_amplification_factor[
channel] = initial_value;
490 const int pre_fill_size =
s->filter_size / 2;
491 double initial_value =
s->alt_boundary_mode ?
cqueue_peek(
s->gain_history_original[
channel], 0) : 1.0;
492 int input = pre_fill_size;
511 double smoothed,
limit;
524 static inline double update_value(
double new,
double old,
double aggressiveness)
526 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
527 return aggressiveness *
new + (1.0 - aggressiveness) * old;
539 const double diff = 1.0 /
frame->nb_samples;
540 int is_first_frame =
cqueue_empty(
s->gain_history_original[0]);
542 for (
int c = 0;
c <
s->channels;
c++) {
544 double *dst_ptr = (
double *)
frame->extended_data[
c];
545 double current_average_value = 0.0;
549 current_average_value += dst_ptr[
i] *
diff;
551 prev_value = is_first_frame ? current_average_value :
s->dc_correction_value[
c];
552 s->dc_correction_value[
c] = is_first_frame ? current_average_value :
update_value(current_average_value,
s->dc_correction_value[
c], 0.1);
554 for (
int i = 0;
i <
frame->nb_samples && !bypass;
i++) {
555 dst_ptr[
i] -=
fade(prev_value,
s->dc_correction_value[
c],
i,
frame->nb_samples);
562 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
563 double current_threshold = threshold;
564 double step_size = 1.0;
566 while (step_size > DBL_EPSILON) {
567 while ((
llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
568 llrint(current_threshold * (UINT64_C(1) << 63))) &&
569 (
bound(current_threshold + step_size, 1.0) <= threshold)) {
570 current_threshold += step_size;
576 return current_threshold;
585 double variance = 0.0;
588 for (
int c = 0;
c <
s->channels;
c++) {
589 const double *data_ptr = (
double *)
frame->extended_data[
c];
592 variance +=
pow_2(data_ptr[
i]);
595 variance /= (
s->channels *
frame->nb_samples) - 1;
597 const double *data_ptr = (
double *)
frame->extended_data[
channel];
600 variance +=
pow_2(data_ptr[
i]);
602 variance /=
frame->nb_samples - 1;
605 return fmax(sqrt(variance), DBL_EPSILON);
610 int is_first_frame =
cqueue_empty(
s->gain_history_original[0]);
612 if (
s->channels_coupled) {
614 const double current_threshold =
fmin(1.0,
s->compress_factor * standard_deviation);
616 const double prev_value = is_first_frame ? current_threshold :
s->compress_threshold[0];
617 double prev_actual_thresh, curr_actual_thresh;
618 s->compress_threshold[0] = is_first_frame ? current_threshold :
update_value(current_threshold,
s->compress_threshold[0], (1.0/3.0));
623 for (
int c = 0;
c <
s->channels;
c++) {
624 double *
const dst_ptr = (
double *)
frame->extended_data[
c];
630 for (
int i = 0;
i <
frame->nb_samples;
i++) {
631 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh,
i,
frame->nb_samples);
636 for (
int c = 0;
c <
s->channels;
c++) {
640 const double prev_value = is_first_frame ? current_threshold :
s->compress_threshold[
c];
641 double prev_actual_thresh, curr_actual_thresh;
644 s->compress_threshold[
c] = is_first_frame ? current_threshold :
update_value(current_threshold,
s->compress_threshold[
c], 1.0/3.0);
649 dst_ptr = (
double *)
frame->extended_data[
c];
650 for (
int i = 0;
i <
frame->nb_samples && !bypass;
i++) {
651 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh,
i,
frame->nb_samples);
662 if (
s->dc_correction ||
s->compress_factor > DBL_EPSILON) {
690 if (
s->dc_correction)
693 if (
s->compress_factor > DBL_EPSILON)
696 if (
s->frame_len !=
s->sample_advance) {
697 const int offset =
s->frame_len -
s->sample_advance;
699 for (
int c = 0;
c <
s->channels;
c++) {
700 double *
src = (
double *)
s->window->extended_data[
c];
702 memmove(
src, &
src[
s->sample_advance],
offset *
sizeof(
double));
703 memcpy(&
src[
offset], (*frame)->extended_data[
c], (*frame)->nb_samples *
sizeof(
double));
704 memset(&
src[
offset + (*frame)->nb_samples], 0, (
s->sample_advance - (*frame)->nb_samples) *
sizeof(
double));
710 s->frame_len, (*frame)->ch_layout.nb_channels, (*frame)->format);
714 if (
s->channels_coupled) {
716 for (
int c = 0;
c <
s->channels;
c++)
719 for (
int c = 0;
c <
s->channels;
c++)
729 for (
int c = 0;
c <
s->channels;
c++) {
732 double *dst_ptr = (
double *)
frame->extended_data[
c];
733 double current_amplification_factor;
737 for (
int i = 0;
i <
frame->nb_samples && enabled && !bypass;
i++) {
738 const double amplification_factor =
fade(
s->prev_amplification_factor[
c],
739 current_amplification_factor,
i,
742 dst_ptr[
i] = src_ptr[
i] * amplification_factor;
745 s->prev_amplification_factor[
c] = current_amplification_factor;
756 while (((
s->queue.available >=
s->filter_size) ||
757 (
s->eof &&
s->queue.available)) &&
807 for (
int c = 0;
c <
s->channels;
c++) {
808 double *dst_ptr = (
double *)
out->extended_data[
c];
810 for (
int i = 0;
i <
out->nb_samples;
i++) {
811 dst_ptr[
i] =
s->alt_boundary_mode ? DBL_EPSILON : ((
s->target_rms > DBL_EPSILON) ?
fmin(
s->peak_value,
s->target_rms) :
s->peak_value);
812 if (
s->dc_correction) {
813 dst_ptr[
i] *= ((
i % 2) == 1) ? -1 : 1;
814 dst_ptr[
i] +=
s->dc_correction_value[
c];
831 }
else if (
s->queue.available) {
854 if (strcmp(
s->channels_to_filter,
"all"))
880 if (
s->eof &&
s->queue.available)
881 return flush(outlink);
883 if (
s->eof && !
s->queue.available) {
895 char *res,
int res_len,
int flags)
899 int prev_filter_size =
s->filter_size;
907 if (prev_filter_size !=
s->filter_size) {
910 for (
int c = 0;
c <
s->channels;
c++) {
918 s->sample_advance =
FFMAX(1,
lrint(
s->frame_len * (1. -
s->overlap)));
939 .
name =
"dynaudnorm",
948 .priv_class = &dynaudnorm_class,
static int config_input(AVFilterLink *inlink)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
AVFILTER_DEFINE_CLASS(dynaudnorm)
static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *in, AVFrame *frame, int enabled)
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.
static av_cold int init(AVFilterContext *ctx)
double * dc_correction_value
static void cqueue_resize(cqueue *q, int new_size)
const char * name
Filter name.
A link between two filters.
static const AVFilterPad avfilter_af_dynaudnorm_inputs[]
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
static double find_peak_magnitude(AVFrame *frame, int channel)
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
static int analyze_frame(DynamicAudioNormalizerContext *s, AVFilterLink *outlink, AVFrame **frame)
static double val(void *priv, double ch)
char * channels_to_filter
static cqueue * cqueue_create(int size, int max_size)
static int activate(AVFilterContext *ctx)
static double update_value(double new, double old, double aggressiveness)
A filter pad used for either input or output.
static const AVFilterPad avfilter_af_dynaudnorm_outputs[]
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
static int frame_size(int sample_rate, int frame_len_msec)
static double minimum_filter(cqueue *q)
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
AVChannelLayout ch_layout
static int cqueue_empty(cqueue *q)
static int adjust(int x, int size)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static av_always_inline double copysign(double x, double y)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
static int cqueue_size(cqueue *q)
static av_cold void uninit(AVFilterContext *ctx)
#define FILTER_INPUTS(array)
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.
static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static double pow_2(const double value)
static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static int flush(AVFilterLink *outlink)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
cqueue ** threshold_history
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 cqueue_enqueue(cqueue *q, double element)
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
static float minimum(float src0, float src1)
static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
An AVChannelLayout holds information about the channel layout of audio data.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
double * prev_amplification_factor
double fmin(double, double)
static AVRational av_make_q(int num, int den)
Create an AVRational.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
static int cqueue_pop(cqueue *q)
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.
static void cqueue_free(cqueue *q)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
const AVFilter ff_af_dynaudnorm
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)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
double * compress_threshold
static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, local_gain gain)
int sample_rate
samples per second
int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
Structure holding the queue.
uint8_t ** extended_data
pointers to the data planes/channels.
#define av_malloc_array(a, b)
static const int weights[]
cqueue ** gain_history_minimum
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 default value
static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
const char * name
Pad name.
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
void * av_calloc(size_t nmemb, size_t size)
static double erf(double z)
erf function Algorithm taken from the Boost project, source: http://www.boost.org/doc/libs/1_46_1/boo...
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
static double limit(double x)
static double bound(const double threshold, const double val)
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 double cqueue_peek(cqueue *q, int index)
static double compute_frame_rms(AVFrame *frame, int channel)
double fmax(double, double)
static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
static const AVOption dynaudnorm_options[]
cqueue ** gain_history_original
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
@ AV_SAMPLE_FMT_DBLP
double, planar
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
#define CONST(name, help, val, unit)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int bypass_channel(DynamicAudioNormalizerContext *s, AVFrame *frame, int ch)
#define FILTER_OUTPUTS(array)
static double fade(double prev, double next, int pos, int length)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
#define flags(name, subs,...)
static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
static double setup_compress_thresh(double threshold)
cqueue ** gain_history_smoothed
static int cqueue_dequeue(cqueue *q, double *element)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.