32 #define MIN_FILTER_SIZE 3
33 #define MAX_FILTER_SIZE 301
35 #define FF_BUFQUEUE_SIZE (MAX_FILTER_SIZE + 1)
89 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
90 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
122 if (!(
s->filter_size & 1)) {
253 for (
int i = 0;
i < side;
i++)
257 int count = (q->
size - new_size + 1) / 2;
268 double total_weight = 0.0;
269 const double sigma = (((
s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
274 const int offset =
s->filter_size / 2;
275 const double c1 = 1.0 / (sigma * sqrt(2.0 *
M_PI));
276 const double c2 = 2.0 * sigma * sigma;
279 for (
i = 0;
i <
s->filter_size;
i++) {
283 total_weight +=
s->weights[
i];
287 adjust = 1.0 / total_weight;
288 for (
i = 0;
i <
s->filter_size;
i++) {
302 for (
c = 0;
c <
s->channels;
c++) {
303 if (
s->gain_history_original)
305 if (
s->gain_history_minimum)
307 if (
s->gain_history_smoothed)
309 if (
s->threshold_history)
319 s->is_enabled =
NULL;
347 if (!
s->prev_amplification_factor || !
s->dc_correction_value ||
348 !
s->compress_threshold ||
349 !
s->gain_history_original || !
s->gain_history_minimum ||
350 !
s->gain_history_smoothed || !
s->threshold_history ||
351 !
s->is_enabled || !
s->weights)
355 s->prev_amplification_factor[
c] = 1.0;
362 if (!
s->gain_history_original[
c] || !
s->gain_history_minimum[
c] ||
363 !
s->gain_history_smoothed[
c] || !
s->threshold_history[
c])
372 static inline double fade(
double prev,
double next,
int pos,
int length)
374 const double step_size = 1.0 / length;
375 const double f0 = 1.0 - (step_size * (
pos + 1.0));
376 const double f1 = 1.0 - f0;
377 return f0 * prev + f1 * next;
385 static inline double bound(
const double threshold,
const double val)
387 const double CONST = 0.8862269254527580136490837416705725913987747280611935;
393 double max = DBL_EPSILON;
415 double rms_value = 0.0;
423 rms_value +=
pow_2(data_ptr[
i]);
431 rms_value +=
pow_2(data_ptr[
i]);
437 return FFMAX(sqrt(rms_value), DBL_EPSILON);
444 const double maximum_gain =
s->peak_value / peak_magnitude;
448 gain.
threshold = peak_magnitude >
s->threshold;
449 gain.max_gain =
bound(
s->max_amplification,
FFMIN(maximum_gain, rms_gain));
456 double min = DBL_MAX;
468 double result = 0.0, tsum = 0.0;
486 const int pre_fill_size =
s->filter_size / 2;
487 const double initial_value =
s->alt_boundary_mode ? gain.
max_gain :
s->peak_value;
489 s->prev_amplification_factor[
channel] = initial_value;
503 const int pre_fill_size =
s->filter_size / 2;
504 double initial_value =
s->alt_boundary_mode ?
cqueue_peek(
s->gain_history_original[
channel], 0) : 1.0;
505 int input = pre_fill_size;
524 double smoothed, limit;
528 smoothed =
FFMIN(smoothed, limit);
537 static inline double update_value(
double new,
double old,
double aggressiveness)
539 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
540 return aggressiveness *
new + (1.0 - aggressiveness) * old;
546 int is_first_frame =
cqueue_empty(
s->gain_history_original[0]);
549 for (
c = 0;
c <
s->channels;
c++) {
551 double current_average_value = 0.0;
555 current_average_value += dst_ptr[
i] *
diff;
557 prev_value = is_first_frame ? current_average_value :
s->dc_correction_value[
c];
558 s->dc_correction_value[
c] = is_first_frame ? current_average_value :
update_value(current_average_value,
s->dc_correction_value[
c], 0.1);
568 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
569 double current_threshold = threshold;
570 double step_size = 1.0;
572 while (step_size > DBL_EPSILON) {
573 while ((
llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
574 llrint(current_threshold * (UINT64_C(1) << 63))) &&
575 (
bound(current_threshold + step_size, 1.0) <= threshold)) {
576 current_threshold += step_size;
582 return current_threshold;
591 double variance = 0.0;
595 for (
c = 0;
c <
s->channels;
c++) {
599 variance +=
pow_2(data_ptr[
i]);
607 variance +=
pow_2(data_ptr[
i]);
612 return FFMAX(sqrt(variance), DBL_EPSILON);
617 int is_first_frame =
cqueue_empty(
s->gain_history_original[0]);
620 if (
s->channels_coupled) {
622 const double current_threshold =
FFMIN(1.0,
s->compress_factor * standard_deviation);
624 const double prev_value = is_first_frame ? current_threshold :
s->compress_threshold[0];
625 double prev_actual_thresh, curr_actual_thresh;
626 s->compress_threshold[0] = is_first_frame ? current_threshold :
update_value(current_threshold,
s->compress_threshold[0], (1.0/3.0));
631 for (
c = 0;
c <
s->channels;
c++) {
639 for (
c = 0;
c <
s->channels;
c++) {
643 const double prev_value = is_first_frame ? current_threshold :
s->compress_threshold[
c];
644 double prev_actual_thresh, curr_actual_thresh;
646 s->compress_threshold[
c] = is_first_frame ? current_threshold :
update_value(current_threshold,
s->compress_threshold[
c], 1.0/3.0);
662 if (
s->dc_correction) {
666 if (
s->compress_factor > DBL_EPSILON) {
670 if (
s->channels_coupled) {
674 for (
c = 0;
c <
s->channels;
c++)
679 for (
c = 0;
c <
s->channels;
c++)
688 for (
c = 0;
c <
s->channels;
c++) {
690 double current_amplification_factor;
695 const double amplification_factor =
fade(
s->prev_amplification_factor[
c],
696 current_amplification_factor,
i,
699 dst_ptr[
i] *= amplification_factor;
702 s->prev_amplification_factor[
c] = current_amplification_factor;
713 while (((
s->queue.available >=
s->filter_size) ||
714 (
s->eof &&
s->queue.available)) &&
722 s->pts =
out->pts +
out->nb_samples;
747 for (
c = 0;
c <
s->channels;
c++) {
748 double *dst_ptr = (
double *)
out->extended_data[
c];
751 dst_ptr[
i] =
s->alt_boundary_mode ? DBL_EPSILON : ((
s->target_rms > DBL_EPSILON) ?
FFMIN(
s->peak_value,
s->target_rms) :
s->peak_value);
752 if (
s->dc_correction) {
753 dst_ptr[
i] *= ((
i % 2) == 1) ? -1 : 1;
754 dst_ptr[
i] +=
s->dc_correction_value[
c];
770 }
else if (
s->queue.available) {
773 s->pts =
out->pts +
out->nb_samples;
812 if (
s->eof &&
s->queue.available)
813 return flush(outlink);
815 if (
s->eof && !
s->queue.available) {
827 char *res,
int res_len,
int flags)
831 int prev_filter_size =
s->filter_size;
839 if (prev_filter_size !=
s->filter_size) {
842 for (
int c = 0;
c <
s->channels;
c++) {
872 .
name =
"dynaudnorm",
881 .priv_class = &dynaudnorm_class,
static enum AVSampleFormat sample_fmts[]
static double val(void *priv, double ch)
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
static int flush(AVFilterLink *outlink)
static int cqueue_pop(cqueue *q)
static void cqueue_resize(cqueue *q, int new_size)
AVFilter ff_af_dynaudnorm
static cqueue * cqueue_create(int size, int max_size)
static double bound(const double threshold, const double val)
static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
static int cqueue_empty(cqueue *q)
static double update_value(double new, double old, double aggressiveness)
static double compute_frame_rms(AVFrame *frame, int channel)
static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
static int cqueue_enqueue(cqueue *q, double element)
static const AVOption dynaudnorm_options[]
static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
static double setup_compress_thresh(double threshold)
static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static int cqueue_size(cqueue *q)
static double minimum_filter(cqueue *q)
static double cqueue_peek(cqueue *q, int index)
static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, local_gain gain)
static int query_formats(AVFilterContext *ctx)
static void cqueue_free(cqueue *q)
static double find_peak_magnitude(AVFrame *frame, int channel)
static int config_input(AVFilterLink *inlink)
static int frame_size(int sample_rate, int frame_len_msec)
static double pow_2(const double value)
static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVFilterPad avfilter_af_dynaudnorm_outputs[]
static int cqueue_dequeue(cqueue *q, double *element)
AVFILTER_DEFINE_CLASS(dynaudnorm)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static double fade(double prev, double next, int pos, int length)
static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame, int enabled)
static const AVFilterPad avfilter_af_dynaudnorm_inputs[]
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next 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.
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.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Main libavfilter public API header.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
#define flags(name, subs,...)
static __device__ float fabs(float a)
static float minimum(float src0, float src1)
channel
Use these values when setting the channel map with ebur128_set_channel().
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
#define FFERROR_NOT_READY
Filters implementation helper functions.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_DBLP
double, planar
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static double erf(double z)
erf function Algorithm taken from the Boost project, source: http://www.boost.org/doc/libs/1_46_1/boo...
static av_always_inline double copysign(double x, double y)
static int adjust(int x, int size)
enum MovChannelLayoutTag * layouts
Describe the class of an AVClass context structure.
A list of supported channel layouts.
A link between two filters.
int channels
Number of channels.
AVFilterContext * src
source filter
int sample_rate
samples per second
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int channels
number of audio channels, only used for audio.
uint8_t ** extended_data
pointers to the data planes/channels.
cqueue ** gain_history_minimum
cqueue ** threshold_history
double * prev_amplification_factor
double * dc_correction_value
cqueue ** gain_history_smoothed
double * compress_threshold
cqueue ** gain_history_original
Structure holding the queue.
#define av_malloc_array(a, b)
#define CONST(name, help, val, unit)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]