98 #define OFFSET(x) offsetof(LoudNormContext, x)
99 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
129 const int frame_size =
round((
double)sample_rate * (frame_len_msec / 1000.0));
130 return frame_size + (frame_size % 2);
135 double total_weight = 0.0;
136 const double sigma = 3.5;
140 const int offset = 21 / 2;
141 const double c1 = 1.0 / (sigma * sqrt(2.0 *
M_PI));
142 const double c2 = 2.0 * pow(sigma, 2.0);
144 for (i = 0; i < 21; i++) {
146 s->
weights[i] = c1 *
exp(-(pow(x, 2.0) / c2));
150 adjust = 1.0 / total_weight;
151 for (i = 0; i < 21; i++)
160 index = index - 10 > 0 ? index - 10 : index + 20;
161 for (i = 0; i < 21; i++)
162 result += s->
delta[((index + i) < 30) ? (index + i) : (index + i - 30)] * s->
weights[i];
182 for (c = 0; c < channels; c++)
183 s->
prev_smp[c] = fabs(buf[index + c - channels]);
186 for (n = 0; n < nb_samples; n++) {
187 for (c = 0; c < channels; c++) {
188 double this, next, max_peak;
193 if ((s->
prev_smp[c] <=
this) && (next <=
this) && (
this > ceiling) && (n > 0)) {
197 for (i = 2; i < 12; i++) {
208 for (c = 0; c < channels; c++) {
209 if (c == 0 || fabs(buf[index + c]) > max_peak)
210 max_peak = fabs(buf[index + c]);
217 *peak_value = max_peak;
232 int n,
c,
index, peak_delta, smp_cnt;
233 double ceiling, peak_value;
245 for (n = 0; n < 1920; n++) {
246 for (c = 0; c < channels; c++) {
247 max = fabs(buf[c]) > max ? fabs(buf[c]) : max;
257 for (n = 0; n < 1920; n++) {
258 for (c = 0; c < channels; c++) {
274 detect_peak(s, smp_cnt, nb_samples - smp_cnt, channels, &peak_delta, &peak_value);
275 if (peak_delta != -1) {
291 smp_cnt = nb_samples;
297 for (c = 0; c < channels; c++) {
308 if (smp_cnt >= nb_samples) {
314 if (smp_cnt < nb_samples) {
322 detect_peak(s, smp_cnt, nb_samples, channels, &peak_delta, &peak_value);
323 if (peak_delta == -1) {
330 double gain_reduction;
331 gain_reduction = ceiling / peak_value;
333 if (gain_reduction < s->gain_reduction[1]) {
347 for (c = 0; c < channels; c++) {
358 if (smp_cnt >= nb_samples) {
368 for (c = 0; c < channels; c++) {
379 if (smp_cnt >= nb_samples) {
385 if (smp_cnt < nb_samples) {
393 }
while (smp_cnt < nb_samples);
395 for (n = 0; n < nb_samples; n++) {
396 for (c = 0; c < channels; c++) {
397 out[
c] = buf[index +
c];
398 if (fabs(out[c]) > ceiling) {
399 out[
c] = ceiling * (out[
c] < 0 ? -1 : 1);
419 int i,
n,
c, subframe_length, src_index;
420 double gain, gain_next, env_global, env_shortterm,
421 global, shortterm, lra, relative_threshold;
435 src = (
const double *)in->
data[0];
436 dst = (
double *)out->
data[0];
443 double offset, offset_tp, true_peak;
445 ebur128_loudness_global(s->
r128_in, &global);
446 for (c = 0; c < inlink->
channels; c++) {
448 ebur128_sample_peak(s->
r128_in, c, &tmp);
449 if (c == 0 || tmp > true_peak)
454 offset_tp = true_peak +
offset;
463 for (c = 0; c < inlink->
channels; c++) {
470 ebur128_loudness_shortterm(s->
r128_in, &shortterm);
472 if (shortterm < s->measured_thresh) {
477 env_shortterm = shortterm <= -70. ? 0. : s->
target_i - shortterm;
480 for (n = 0; n < 30; n++)
481 s->
delta[n] = pow(10., env_shortterm / 20.);
488 for (c = 0; c < inlink->
channels; c++) {
500 ebur128_add_frames_double(s->
r128_out, dst, subframe_length);
516 for (c = 0; c < inlink->
channels; c++) {
541 ebur128_loudness_range(s->
r128_in, &lra);
542 ebur128_loudness_global(s->
r128_in, &global);
543 ebur128_loudness_shortterm(s->
r128_in, &shortterm);
544 ebur128_relative_threshold(s->
r128_in, &relative_threshold);
547 double shortterm_out;
552 ebur128_loudness_shortterm(s->
r128_out, &shortterm_out);
557 if (shortterm < relative_threshold || shortterm <= -70. || s->above_threshold == 0) {
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.);
579 for (c = 0; c < inlink->
channels; c++) {
590 for (i = 0; i < in->
nb_samples / subframe_length; i++) {
593 for (n = 0; n < subframe_length; n++) {
594 for (c = 0; c < inlink->
channels; c++) {
610 dst += (subframe_length * inlink->
channels);
613 dst = (
double *)out->
data[0];
619 for (c = 0; c < inlink->
channels; c++) {
626 dst = (
double *)out->
data[0];
661 src = (
double *)frame->
data[0];
667 for (n = 0; n < nb_samples; n++) {
668 for (c = 0; c < inlink->
channels; c++) {
689 static const int input_srate[] = {192000, -1};
728 s->
r128_in = ebur128_init(inlink->
channels, inlink->
sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
732 s->
r128_out = ebur128_init(inlink->
channels, inlink->
sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
737 ebur128_set_channel(s->
r128_in, 0, EBUR128_DUAL_MONO);
738 ebur128_set_channel(s->
r128_out, 0, EBUR128_DUAL_MONO);
796 double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
802 ebur128_loudness_range(s->
r128_in, &lra_in);
803 ebur128_loudness_global(s->
r128_in, &i_in);
804 ebur128_relative_threshold(s->
r128_in, &thresh_in);
807 ebur128_sample_peak(s->
r128_in, c, &tmp);
808 if ((c == 0) || (tmp > tp_in))
812 ebur128_loudness_range(s->
r128_out, &lra_out);
813 ebur128_loudness_global(s->
r128_out, &i_out);
814 ebur128_relative_threshold(s->
r128_out, &thresh_out);
817 ebur128_sample_peak(s->
r128_out, c, &tmp);
818 if ((c == 0) || (tmp > tp_out))
829 "\t\"input_i\" : \"%.2f\",\n"
830 "\t\"input_tp\" : \"%.2f\",\n"
831 "\t\"input_lra\" : \"%.2f\",\n"
832 "\t\"input_thresh\" : \"%.2f\",\n"
833 "\t\"output_i\" : \"%.2f\",\n"
834 "\t\"output_tp\" : \"%+.2f\",\n"
835 "\t\"output_lra\" : \"%.2f\",\n"
836 "\t\"output_thresh\" : \"%.2f\",\n"
837 "\t\"normalization_type\" : \"%s\",\n"
838 "\t\"target_offset\" : \"%.2f\"\n"
856 "Input Integrated: %+6.1f LUFS\n"
857 "Input True Peak: %+6.1f dBTP\n"
858 "Input LRA: %6.1f LU\n"
859 "Input Threshold: %+6.1f LUFS\n"
861 "Output Integrated: %+6.1f LUFS\n"
862 "Output True Peak: %+6.1f dBTP\n"
863 "Output LRA: %6.1f LU\n"
864 "Output Threshold: %+6.1f LUFS\n"
866 "Normalization Type: %s\n"
867 "Target Offset: %+6.1f LU\n",
915 .priv_class = &loudnorm_class,
918 .
inputs = avfilter_af_loudnorm_inputs,
919 .
outputs = avfilter_af_loudnorm_outputs,
This structure describes decoded (raw) audio or video data.
static int linear(InterplayACMContext *s, unsigned ind, unsigned col)
Main libavfilter public API header.
int max_samples
Maximum number of samples to filter at once.
static char * print_format
static enum AVSampleFormat formats[]
static int config_input(AVFilterLink *inlink)
AVFILTER_DEFINE_CLASS(loudnorm)
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
static const AVOption loudnorm_options[]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold int end(AVCodecContext *avctx)
static void detect_peak(LoudNormContext *s, int offset, int nb_samples, int channels, int *peak_delta, double *peak_value)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AVERROR_EOF
End of file.
enum PrintFormat print_format
A filter pad used for either input or output.
A link between two filters.
FrameType
G723.1 frame types.
static int request_frame(AVFilterLink *outlink)
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.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#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
static av_always_inline av_const double round(double x)
static const uint8_t offset[127][2]
static const AVFilterPad avfilter_af_loudnorm_inputs[]
enum LimiterState limiter_state
AVFilterContext * src
source filter
int partial_buf_size
Size of the partial buffer to allocate.
static const AVFilterPad outputs[]
static double gaussian_filter(LoudNormContext *s, int index)
AVFilterFormats * out_samplerates
A list of supported channel layouts.
#define AV_LOG_INFO
Standard information.
static const AVFilterPad inputs[]
AVFilterFormats * in_samplerates
Lists of channel layouts and sample rates used for automatic negotiation.
AVSampleFormat
Audio sample formats.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
static void true_peak_limiter(LoudNormContext *s, double *out, int nb_samples, int channels)
enum FrameType frame_type
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 av_cold void uninit(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
const char * name
Filter name.
static int frame_size(int sample_rate, int frame_len_msec)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int query_formats(AVFilterContext *ctx)
int channels
Number of channels.
static void init_gaussian_filter(LoudNormContext *s)
AVFilterContext * dst
dest filter
static enum AVSampleFormat sample_fmts[]
static const AVFilterPad avfilter_af_loudnorm_outputs[]
#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 int filter_frame(AVFilterLink *inlink, AVFrame *in)
int nb_samples
number of audio samples (per channel) described by this frame
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.