56#define CHECK_ERROR(condition, errorcode, goto_point) \
58 errcode = (errorcode); \
62#define ALMOST_ZERO 0.000001
64#define RELATIVE_GATE (-10.0)
65#define RELATIVE_GATE_FACTOR pow(10.0, RELATIVE_GATE / 10.0)
66#define MINUS_20DB pow(10.0, -20.0 / 10.0)
110 double f0 = 1681.974450955533;
111 double G = 3.999843853973347;
112 double Q = 0.7071752369554196;
115 double Vh = pow(10.0,
G / 20.0);
116 double Vb = pow(Vh, 0.4996667741545416);
118 double pb[3] = { 0.0, 0.0, 0.0 };
119 double pa[3] = { 1.0, 0.0, 0.0 };
120 double rb[3] = { 1.0, -2.0, 1.0 };
121 double ra[3] = { 1.0, 0.0, 0.0 };
123 double a0 = 1.0 +
K /
Q +
K *
K;
124 pb[0] = (Vh + Vb *
K /
Q +
K *
K) /
a0;
125 pb[1] = 2.0 * (
K *
K - Vh) /
a0;
126 pb[2] = (Vh - Vb *
K /
Q +
K *
K) /
a0;
127 pa[1] = 2.0 * (
K *
K - 1.0) /
a0;
128 pa[2] = (1.0 -
K /
Q +
K *
K) /
a0;
130 f0 = 38.13547087602444;
131 Q = 0.5003270373238773;
134 ra[1] = 2.0 * (
K *
K - 1.0) / (1.0 +
K /
Q +
K *
K);
135 ra[2] = (1.0 -
K /
Q +
K *
K) / (1.0 +
K /
Q +
K *
K);
137 st->
d->
b[0] = pb[0] * rb[0];
138 st->
d->
b[1] = pb[0] * rb[1] + pb[1] * rb[0];
139 st->
d->
b[2] = pb[0] * rb[2] + pb[1] * rb[1] + pb[2] * rb[0];
140 st->
d->
b[3] = pb[1] * rb[2] + pb[2] * rb[1];
141 st->
d->
b[4] = pb[2] * rb[2];
143 st->
d->
a[0] = pa[0] * ra[0];
144 st->
d->
a[1] = pa[0] * ra[1] + pa[1] * ra[0];
145 st->
d->
a[2] = pa[0] * ra[2] + pa[1] * ra[1] + pa[2] * ra[0];
146 st->
d->
a[3] = pa[1] * ra[2] + pa[2] * ra[1];
147 st->
d->
a[4] = pa[2] * ra[2];
149 for (
i = 0;
i < 5; ++
i) {
150 for (j = 0; j < 5; ++j) {
151 st->
d->
v[
i][j] = 0.0;
209 for (
i = 0;
i < 1000; ++
i) {
211 pow(10.0, ((
double)
i / 10.0 - 69.95 + 0.691) / 10.0);
213 for (
i = 1;
i < 1001; ++
i) {
215 pow(10.0, ((
double)
i / 10.0 - 70.0 + 0.691) / 10.0);
220 unsigned long samplerate,
247 goto free_sample_peak;
269 free_block_energy_histogram)
278 goto free_short_term_block_energy_histogram;
282 free_short_term_block_energy_histogram);
286free_short_term_block_energy_histogram:
288free_block_energy_histogram:
306 av_free((*st)->d->block_energy_histogram);
307 av_free((*st)->d->short_term_block_energy_histogram);
309 av_free((*st)->d->channel_map);
310 av_free((*st)->d->sample_peak);
317#define EBUR128_FILTER(type, scaling_factor) \
318static void ebur128_filter_##type(FFEBUR128State* st, const type** srcs, \
319 size_t src_index, size_t frames, \
321 double* audio_data = st->d->audio_data + st->d->audio_data_index; \
324 if ((st->mode & FF_EBUR128_MODE_SAMPLE_PEAK) == FF_EBUR128_MODE_SAMPLE_PEAK) { \
325 for (c = 0; c < st->channels; ++c) { \
327 for (i = 0; i < frames; ++i) { \
328 type v = srcs[c][src_index + i * stride]; \
331 } else if (-v > max) { \
335 max /= scaling_factor; \
336 if (max > st->d->sample_peak[c]) st->d->sample_peak[c] = max; \
339 for (c = 0; c < st->channels; ++c) { \
340 int ci = st->d->channel_map[c] - 1; \
341 if (ci < 0) continue; \
342 else if (ci == FF_EBUR128_DUAL_MONO - 1) ci = 0; \
343 for (i = 0; i < frames; ++i) { \
344 st->d->v[ci][0] = (double) (srcs[c][src_index + i * stride] / scaling_factor) \
345 - st->d->a[1] * st->d->v[ci][1] \
346 - st->d->a[2] * st->d->v[ci][2] \
347 - st->d->a[3] * st->d->v[ci][3] \
348 - st->d->a[4] * st->d->v[ci][4]; \
349 audio_data[i * st->channels + c] = \
350 st->d->b[0] * st->d->v[ci][0] \
351 + st->d->b[1] * st->d->v[ci][1] \
352 + st->d->b[2] * st->d->v[ci][2] \
353 + st->d->b[3] * st->d->v[ci][3] \
354 + st->d->b[4] * st->d->v[ci][4]; \
355 st->d->v[ci][4] = st->d->v[ci][3]; \
356 st->d->v[ci][3] = st->d->v[ci][2]; \
357 st->d->v[ci][2] = st->d->v[ci][1]; \
358 st->d->v[ci][1] = st->d->v[ci][0]; \
360 st->d->v[ci][4] = fabs(st->d->v[ci][4]) < DBL_MIN ? 0.0 : st->d->v[ci][4]; \
361 st->d->v[ci][3] = fabs(st->d->v[ci][3]) < DBL_MIN ? 0.0 : st->d->v[ci][3]; \
362 st->d->v[ci][2] = fabs(st->d->v[ci][2]) < DBL_MIN ? 0.0 : st->d->v[ci][2]; \
363 st->d->v[ci][1] = fabs(st->d->v[ci][1]) < DBL_MIN ? 0.0 : st->d->v[ci][1]; \
370 return 10 * log10(energy) - 0.691;
375 size_t index_min = 0;
376 size_t index_max = 1000;
380 index_mid = (index_min + index_max) / 2;
382 index_min = index_mid;
384 index_max = index_mid;
386 }
while (index_max - index_min != 1);
392 size_t frames_per_block,
393 double *optional_output)
437 sum /= (
double) frames_per_block;
438 if (optional_output) {
439 *optional_output = sum;
446 unsigned int channel_number,
int value)
448 if (channel_number >= st->
channels) {
452 (st->
channels != 1 || channel_number != 0)) {
460#define EBUR128_ADD_FRAMES_PLANAR(type) \
461static void ebur128_add_frames_planar_##type(FFEBUR128State* st, const type** srcs, \
462 size_t frames, int stride) { \
463 size_t src_index = 0; \
464 while (frames > 0) { \
465 if (frames >= st->d->needed_frames) { \
466 ebur128_filter_##type(st, srcs, src_index, st->d->needed_frames, stride); \
467 src_index += st->d->needed_frames * stride; \
468 frames -= st->d->needed_frames; \
469 st->d->audio_data_index += st->d->needed_frames * st->channels; \
471 if ((st->mode & FF_EBUR128_MODE_I) == FF_EBUR128_MODE_I) { \
472 ebur128_calc_gating_block(st, st->d->samples_in_100ms * 4, NULL); \
474 if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
475 st->d->short_term_frame_counter += st->d->needed_frames; \
476 if (st->d->short_term_frame_counter == st->d->samples_in_100ms * 30) { \
478 ebur128_energy_shortterm(st, &st_energy); \
479 if (st_energy >= histogram_energy_boundaries[0]) { \
480 ++st->d->short_term_block_energy_histogram[ \
481 find_histogram_index(st_energy)]; \
483 st->d->short_term_frame_counter = st->d->samples_in_100ms * 20; \
487 st->d->needed_frames = st->d->samples_in_100ms; \
489 if (st->d->audio_data_index == st->d->audio_data_frames * st->channels) { \
490 st->d->audio_data_index = 0; \
493 ebur128_filter_##type(st, srcs, src_index, frames, stride); \
494 st->d->audio_data_index += frames * st->channels; \
495 if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
496 st->d->short_term_frame_counter += frames; \
498 st->d->needed_frames -= frames; \
504#define FF_EBUR128_ADD_FRAMES(type) \
505void ff_ebur128_add_frames_##type(FFEBUR128State* st, const type* src, \
508 const type **buf = (const type**)st->d->data_ptrs; \
509 for (i = 0; i < st->channels; i++) \
511 ebur128_add_frames_planar_##type(st, buf, frames, st->channels); \
516 double *relative_threshold)
519 int above_thresh_counter = 0;
520 *relative_threshold = 0.0;
524 for (j = 0; j < 1000; ++j) {
530 if (above_thresh_counter != 0) {
531 *relative_threshold /= (
double)above_thresh_counter;
535 return above_thresh_counter;
541 double gated_loudness = 0.0;
542 double relative_threshold;
543 size_t above_thresh_counter;
544 size_t i, j, start_index;
555 above_thresh_counter = 0;
565 for (j = start_index; j < 1000; ++j) {
571 if (!above_thresh_counter) {
575 gated_loudness /= (
double) above_thresh_counter;
582 double relative_threshold;
602 size_t interval_frames,
double *
out)
623 }
else if (energy <= 0.0) {
637 double stl_power, stl_integrated;
640 unsigned long hist[1000] = { 0 };
641 size_t percentile_low, percentile_high;
658 for (j = 0; j < 1000; ++j) {
670 stl_power /= stl_size;
682 for (j =
index; j < 1000; ++j) {
690 percentile_low = (size_t) ((stl_size - 1) * 0.1 + 0.5);
691 percentile_high = (size_t) ((stl_size - 1) * 0.95 + 0.5);
695 while (stl_size <= percentile_low) {
696 stl_size += hist[j++];
699 while (stl_size <= percentile_high) {
700 stl_size += hist[j++];
715 unsigned int channel_number,
double *
out)
720 }
else if (channel_number >= st->
channels) {
#define i(width, name, range_min, range_max)
int ff_ebur128_loudness_range_multiple(FFEBUR128State **sts, size_t size, double *out)
Get loudness range (LRA) in LU across multiple instances.
int ff_ebur128_set_channel(FFEBUR128State *st, unsigned int channel_number, int value)
Set channel type.
static int ebur128_calc_relative_threshold(FFEBUR128State **sts, size_t size, double *relative_threshold)
static void ebur128_init_filter(FFEBUR128State *st)
static double ebur128_energy_to_loudness(double energy)
int ff_ebur128_loudness_range(FFEBUR128State *st, double *out)
Get loudness range (LRA) of programme in LU.
#define FF_EBUR128_ADD_FRAMES(type)
int ff_ebur128_sample_peak(FFEBUR128State *st, unsigned int channel_number, double *out)
Get maximum sample peak of selected channel in float format.
int ff_ebur128_relative_threshold(FFEBUR128State *st, double *out)
Get relative threshold in LUFS.
static double histogram_energies[1000]
FFEBUR128State * ff_ebur128_init(unsigned int channels, unsigned long samplerate, unsigned long window, int mode)
Initialize library state.
static AVOnce histogram_init
void ff_ebur128_destroy(FFEBUR128State **st)
Destroy library state.
#define RELATIVE_GATE_FACTOR
static int ebur128_gated_loudness(FFEBUR128State **sts, size_t size, double *out)
int ff_ebur128_loudness_global(FFEBUR128State *st, double *out)
Get global integrated loudness in LUFS.
static int ebur128_energy_shortterm(FFEBUR128State *st, double *out)
#define CHECK_ERROR(condition, errorcode, goto_point)
int ff_ebur128_loudness_shortterm(FFEBUR128State *st, double *out)
Get short-term loudness (last 3s) in LUFS.
#define EBUR128_ADD_FRAMES_PLANAR(type)
#define EBUR128_FILTER(type, scaling_factor)
static void ebur128_calc_gating_block(FFEBUR128State *st, size_t frames_per_block, double *optional_output)
static void init_histogram(void)
static int ebur128_init_channel_map(FFEBUR128State *st)
static size_t find_histogram_index(double energy)
static int ebur128_energy_in_interval(FFEBUR128State *st, size_t interval_frames, double *out)
static double histogram_energy_boundaries[1001]
libebur128 - a library for loudness measurement according to the EBU R128 standard.
@ FF_EBUR128_MODE_I
can call ff_ebur128_loudness_global_* and ff_ebur128_relative_threshold
@ FF_EBUR128_MODE_M
can resurrrect and call ff_ebur128_loudness_momentary
@ FF_EBUR128_MODE_LRA
can call ff_ebur128_loudness_range
@ FF_EBUR128_MODE_S
can call ff_ebur128_loudness_shortterm
@ FF_EBUR128_MODE_SAMPLE_PEAK
can call ff_ebur128_sample_peak
@ FF_EBUR128_UNUSED
unused channel (for example LFE channel)
@ FF_EBUR128_Mm060
itu M-060
@ FF_EBUR128_Mm110
itu M-110
@ FF_EBUR128_Mp110
itu M+110
@ FF_EBUR128_DUAL_MONO
a channel that is counted twice
@ FF_EBUR128_Mp090
itu M+090
@ FF_EBUR128_Mm090
itu M-090
@ FF_EBUR128_LEFT_SURROUND
@ FF_EBUR128_RIGHT_SURROUND
@ FF_EBUR128_Mp060
itu M+060
static SDL_Window * window
static int ff_thread_once(char *control, void(*routine)(void))
Utility Preprocessor macros.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
double b[5]
BS.1770 filter coefficients (nominator).
size_t audio_data_index
Current index for audio_data.
size_t short_term_frame_counter
Keeps track of when a new short term block is needed.
unsigned long * short_term_block_energy_histogram
double a[5]
BS.1770 filter coefficients (denominator).
double * sample_peak
Maximum sample peak, one per channel.
unsigned long * block_energy_histogram
Histograms, used to calculate LRA.
double v[5][5]
BS.1770 filter state.
unsigned long samples_in_100ms
How many samples fit in 100ms (rounded).
void ** data_ptrs
Data pointer array for interleaved data.
double * audio_data
Filtered audio data (used as ring buffer).
unsigned long window
The maximum window duration in ms.
int * channel_map
The channel map.
size_t audio_data_frames
Size of audio_data array.
unsigned long needed_frames
How many frames are needed for a gating block.
Contains information about the state of a loudness measurement.
unsigned int channels
The number of channels.
int mode
The current mode.
struct FFEBUR128StateInternal * d
Internal state.
unsigned long samplerate
The sample rate.
#define av_malloc_array(a, b)
static void error(const char *err)
static double a0(void *priv, double x, double y)