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 =
lrint((
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];
233 double total_weight = 0.0;
234 const double sigma = (((s->
filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
240 const double c1 = 1.0 / (sigma * sqrt(2.0 *
M_PI));
241 const double c2 = 2.0 * sigma * sigma;
252 adjust = 1.0 / total_weight;
317 for (c = 0; c < inlink->
channels; c++) {
338 static inline double fade(
double prev,
double next,
int pos,
339 double *fade_factors[2])
341 return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
346 return value *
value;
349 static inline double bound(
const double threshold,
const double val)
351 const double CONST = 0.8862269254527580136490837416705725913987747280611935;
352 return erf(CONST * (val / threshold)) * threshold;
357 double max = DBL_EPSILON;
365 max =
FFMAX(max, fabs(data_ptr[i]));
371 max =
FFMAX(max, fabs(data_ptr[i]));
379 double rms_value = 0.0;
387 rms_value +=
pow_2(data_ptr[i]);
393 const double *data_ptr = (
double *)frame->
extended_data[channel];
395 rms_value +=
pow_2(data_ptr[i]);
401 return FFMAX(sqrt(rms_value), DBL_EPSILON);
414 double min = DBL_MAX;
437 double current_gain_factor)
460 int input = pre_fill_size;
486 static inline double update_value(
double new,
double old,
double aggressiveness)
488 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
489 return aggressiveness *
new + (1.0 - aggressiveness) * old;
500 double current_average_value = 0.0;
504 current_average_value += dst_ptr[i] * diff;
517 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
518 double current_threshold = threshold;
519 double step_size = 1.0;
521 while (step_size > DBL_EPSILON) {
522 while ((
llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
523 llrint(current_threshold * (UINT64_C(1) << 63))) &&
524 (
bound(current_threshold + step_size, 1.0) <= threshold)) {
525 current_threshold += step_size;
531 return current_threshold;
540 double variance = 0.0;
548 variance +=
pow_2(data_ptr[i]);
553 const double *data_ptr = (
double *)frame->
extended_data[channel];
556 variance +=
pow_2(data_ptr[i]);
561 return FFMAX(sqrt(variance), DBL_EPSILON);
573 const double prev_value = is_first_frame ? current_threshold : s->
compress_threshold[0];
574 double prev_actual_thresh, curr_actual_thresh;
583 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh, i, s->
fade_factors);
584 dst_ptr[i] =
copysign(
bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
593 double prev_actual_thresh, curr_actual_thresh;
602 const double localThresh =
fade(prev_actual_thresh, curr_actual_thresh, i, s->
fade_factors);
603 dst_ptr[i] =
copysign(
bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
639 double current_amplification_factor;
645 current_amplification_factor, i,
648 dst_ptr[i] *= amplification_factor;
693 dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
745 .
name =
"dynaudnorm",
751 .
inputs = avfilter_af_dynaudnorm_inputs,
752 .
outputs = avfilter_af_dynaudnorm_outputs,
753 .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.
#define CONST(name, help, val, unit)
static int cqueue_empty(cqueue *q)
static const AVFilterPad avfilter_af_dynaudnorm_outputs[]
static double pow_2(const double value)
static double erf(double z)
erf function Algorithm taken from the Boost project, source: http://www.boost.org/doc/libs/1_46_1/boo...
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 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)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
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.
static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
AVFILTER_DEFINE_CLASS(dynaudnorm)
int min_samples
Minimum number of samples to filter at once.
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 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.
static const AVFilterPad outputs[]
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
A list of supported channel layouts.
static const AVFilterPad inputs[]
AVFilter ff_af_dynaudnorm
static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, double current_gain_factor)
AVSampleFormat
Audio sample formats.
unsigned short available
number of available buffers
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)
double * dc_correction_value
const char * name
Filter name.
static av_always_inline double copysign(double x, double y)
static double setup_compress_thresh(double threshold)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
static double find_peak_magnitude(AVFrame *frame, int channel)
static int cqueue_pop(cqueue *q)
static double minimum_filter(cqueue *q)
channel
Use these values when setting the channel map with ebur128_set_channel().
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)
static enum AVSampleFormat sample_fmts[]
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
#define av_malloc_array(a, b)
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)