32 #define FF_BUFQUEUE_SIZE 302
76 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
77 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
138 const int frame_size =
round((
double)sample_rate * (frame_len_msec / 1000.0));
139 return frame_size + (frame_size % 2);
144 const double step_size = 1.0 / frame_len;
147 for (pos = 0; pos < frame_len; pos++) {
148 fade_factors[0][pos] = 1.0 - (step_size * (pos + 1.0));
149 fade_factors[1][pos] = 1.0 - fade_factors[0][pos];
230 static const double s_pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
234 double total_weight = 0.0;
235 const double sigma = (((s->
filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
241 const double c1 = 1.0 / (sigma * sqrt(2.0 *
s_pi));
242 const double c2 = 2.0 * pow(sigma, 2.0);
248 s->
weights[i] = c1 * exp(-(pow(x, 2.0) / c2));
253 adjust = 1.0 / total_weight;
287 for (c = 0; c < inlink->
channels; c++) {
314 static inline double fade(
double prev,
double next,
int pos,
315 double *fade_factors[2])
317 return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
322 return value *
value;
325 static inline double bound(
const double threshold,
const double val)
327 const double CONST = 0.8862269254527580136490837416705725913987747280611935;
328 return erf(CONST * (val / threshold)) * threshold;
333 double max = DBL_EPSILON;
341 max =
FFMAX(max, fabs(data_ptr[i]));
347 max =
FFMAX(max, fabs(data_ptr[i]));
355 double rms_value = 0.0;
363 rms_value +=
pow2(data_ptr[i]);
369 const double *data_ptr = (
double *)frame->
extended_data[channel];
371 rms_value +=
pow2(data_ptr[i]);
377 return FFMAX(sqrt(rms_value), DBL_EPSILON);
390 double min = DBL_MAX;
413 double current_gain_factor)
453 static inline double update_value(
double new,
double old,
double aggressiveness)
455 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
456 return aggressiveness *
new + (1.0 - aggressiveness) * old;
467 double current_average_value = 0.0;
471 current_average_value += dst_ptr[i] * diff;
484 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
485 double current_threshold = threshold;
486 double step_size = 1.0;
488 while (step_size > DBL_EPSILON) {
489 while ((current_threshold + step_size > current_threshold) &&
490 (
bound(current_threshold + step_size, 1.0) <= threshold)) {
491 current_threshold += step_size;
497 return current_threshold;
506 double variance = 0.0;
514 variance +=
pow2(data_ptr[i]);
519 const double *data_ptr = (
double *)frame->
extended_data[channel];
522 variance +=
pow2(data_ptr[i]);
527 return FFMAX(sqrt(variance), DBL_EPSILON);
539 const double prev_value = is_first_frame ? current_threshold : s->
compress_threshold[0];
540 double prev_actual_thresh, curr_actual_thresh;
549 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh, i, s->
fade_factors);
550 dst_ptr[i] = copysign(
bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
559 double prev_actual_thresh, curr_actual_thresh;
568 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh, i, s->
fade_factors);
569 dst_ptr[i] = copysign(
bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
605 double current_amplification_factor;
611 current_amplification_factor, i,
614 dst_ptr[i] *= amplification_factor;
617 dst_ptr[i] = copysign(s->
peak_value, dst_ptr[i]);
659 dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
731 .
name =
"dynaudnorm",
737 .
inputs = avfilter_af_dynaudnorm_inputs,
738 .
outputs = avfilter_af_dynaudnorm_outputs,
739 .priv_class = &dynaudnorm_class,
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
static const AVFilterPad avfilter_af_dynaudnorm_inputs[]
static double bound(const double threshold, const double val)
const char const char void * val
static double compute_frame_rms(AVFrame *frame, int channel)
This structure describes decoded (raw) audio or video data.
static int cqueue_empty(cqueue *q)
static const AVFilterPad avfilter_af_dynaudnorm_outputs[]
static const AVFilterPad outputs[]
Main libavfilter public API header.
cqueue ** gain_history_smoothed
int max_samples
Maximum number of samples to filter at once.
static int cqueue_size(cqueue *q)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static double get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static enum AVSampleFormat formats[]
static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void precalculate_fade_factors(double *fade_factors[2], int frame_len)
int is_disabled
the enabled state from the last expression evaluation
double * prev_amplification_factor
static int request_frame(AVFilterLink *outlink)
static int config_input(AVFilterLink *inlink)
Structure holding the queue.
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q)
double * compress_threshold
#define AVERROR_EOF
End of file.
static av_cold void uninit(AVFilterContext *ctx)
cqueue ** gain_history_minimum
static void cqueue_free(cqueue *q)
A filter pad used for either input or output.
A link between two filters.
cqueue ** gain_history_original
static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
static int query_formats(AVFilterContext *ctx)
static double cqueue_peek(cqueue *q, int index)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned flags
Link processing flags.
#define CONST(name, help, val, unit)
static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
AVFILTER_DEFINE_CLASS(dynaudnorm)
int min_samples
Minimum number of samples to filter at once.
static double pow2(const double value)
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
simple assert() macros that are a bit more flexible than ISO C assert().
static av_always_inline av_const double round(double x)
static int config_output(AVFilterLink *outlink)
static const uint8_t offset[127][2]
GLsizei GLboolean const GLfloat * value
static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
AVFilterContext * src
source filter
int partial_buf_size
Size of the partial buffer to allocate.
Frame requests may need to loop in order to be fulfilled.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
A list of supported channel layouts.
AVFilter ff_af_dynaudnorm
static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, double current_gain_factor)
AVSampleFormat
Audio sample formats.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, AVFrame *frame, int channel)
static av_cold int init(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
int av_frame_get_channels(const AVFrame *frame)
static const AVFilterPad inputs[]
double * dc_correction_value
const char * name
Filter name.
static double setup_compress_thresh(double threshold)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
static double find_peak_magnitude(AVFrame *frame, int channel)
static int cqueue_pop(cqueue *q)
static double minimum_filter(cqueue *q)
int channels
Number of channels.
static int cqueue_enqueue(cqueue *q, double element)
static double fade(double prev, double next, int pos, double *fade_factors[2])
static av_always_inline int diff(const uint32_t a, const uint32_t b)
AVFilterContext * dst
dest filter
static double update_value(double new, double old, double aggressiveness)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static enum AVSampleFormat sample_fmts[]
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
static cqueue * cqueue_create(int size)
static const AVOption dynaudnorm_options[]
static int cqueue_dequeue(cqueue *q, double *element)
uint8_t ** extended_data
pointers to the data planes/channels.
static int frame_size(int sample_rate, int frame_len_msec)
int nb_samples
number of audio samples (per channel) described by this frame
static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)