Go to the documentation of this file.
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);
286 free_short_term_block_energy_histogram:
288 free_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) \
318 static 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)
410 i < st->
d->audio_data_frames; ++
i) {
417 i < st->
d->audio_data_index / st->
channels; ++
i) {
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) \
461 static 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) \
505 void 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) {
static void error(const char *err)
unsigned long samples_in_100ms
How many samples fit in 100ms (rounded).
@ FF_EBUR128_RIGHT_SURROUND
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
struct FFEBUR128StateInternal * d
Internal state.
#define FF_EBUR128_ADD_FRAMES(type)
static int ebur128_calc_relative_threshold(FFEBUR128State **sts, size_t size, double *relative_threshold)
static int ebur128_gated_loudness(FFEBUR128State **sts, size_t size, double *out)
int ff_ebur128_loudness_range_multiple(FFEBUR128State **sts, size_t size, double *out)
Get loudness range (LRA) in LU across multiple instances.
@ FF_EBUR128_LEFT_SURROUND
unsigned long * block_energy_histogram
Histograms, used to calculate LRA.
static int ebur128_energy_in_interval(FFEBUR128State *st, size_t interval_frames, double *out)
unsigned long needed_frames
How many frames are needed for a gating block.
static AVOnce histogram_init
size_t audio_data_index
Current index for audio_data.
static size_t find_histogram_index(double energy)
int * channel_map
The channel map.
@ FF_EBUR128_MODE_I
can call ff_ebur128_loudness_global_* and ff_ebur128_relative_threshold
static int ebur128_energy_shortterm(FFEBUR128State *st, double *out)
size_t audio_data_frames
Size of audio_data array.
int ff_ebur128_loudness_range(FFEBUR128State *st, double *out)
Get loudness range (LRA) of programme in LU.
static SDL_Window * window
double * audio_data
Filtered audio data (used as ring buffer).
void ff_ebur128_destroy(FFEBUR128State **st)
Destroy library state.
@ FF_EBUR128_UNUSED
unused channel (for example LFE channel)
double a[5]
BS.1770 filter coefficients (denominator).
@ FF_EBUR128_DUAL_MONO
a channel that is counted twice
unsigned long samplerate
The sample rate.
static int ff_thread_once(char *control, void(*routine)(void))
double v[5][5]
BS.1770 filter state.
@ FF_EBUR128_MODE_LRA
can call ff_ebur128_loudness_range
@ FF_EBUR128_Mm060
itu M-060
double b[5]
BS.1770 filter coefficients (nominator).
int mode
The current mode.
@ FF_EBUR128_Mm110
itu M-110
unsigned long * short_term_block_energy_histogram
static void ebur128_init_filter(FFEBUR128State *st)
void ** data_ptrs
Data pointer array for interleaved data.
static double histogram_energy_boundaries[1001]
static double histogram_energies[1000]
int ff_ebur128_sample_peak(FFEBUR128State *st, unsigned int channel_number, double *out)
Get maximum sample peak of selected channel in float format.
#define CHECK_ERROR(condition, errorcode, goto_point)
@ FF_EBUR128_Mp060
itu M+060
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static void ebur128_calc_gating_block(FFEBUR128State *st, size_t frames_per_block, double *optional_output)
static void init_histogram(void)
#define DECLARE_ALIGNED(n, t, v)
int ff_ebur128_loudness_shortterm(FFEBUR128State *st, double *out)
Get short-term loudness (last 3s) in LUFS.
@ FF_EBUR128_Mp090
itu M+090
@ FF_EBUR128_MODE_S
can call ff_ebur128_loudness_shortterm
FFEBUR128State * ff_ebur128_init(unsigned int channels, unsigned long samplerate, unsigned long window, int mode)
Initialize library state.
#define EBUR128_ADD_FRAMES_PLANAR(type)
@ FF_EBUR128_Mm090
itu M-090
unsigned int channels
The number of channels.
#define i(width, name, range_min, range_max)
int ff_ebur128_set_channel(FFEBUR128State *st, unsigned int channel_number, int value)
Set channel type.
libebur128 - a library for loudness measurement according to the EBU R128 standard.
@ FF_EBUR128_MODE_M
can resurrrect and call ff_ebur128_loudness_momentary
#define av_malloc_array(a, b)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Contains information about the state of a loudness measurement.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
size_t short_term_frame_counter
Keeps track of when a new short term block is needed.
void * av_calloc(size_t nmemb, size_t size)
double * sample_peak
Maximum sample peak, one per channel.
#define EBUR128_FILTER(type, scaling_factor)
static double ebur128_energy_to_loudness(double energy)
@ FF_EBUR128_Mp110
itu M+110
#define RELATIVE_GATE_FACTOR
int ff_ebur128_relative_threshold(FFEBUR128State *st, double *out)
Get relative threshold in LUFS.
static int ebur128_init_channel_map(FFEBUR128State *st)
@ FF_EBUR128_MODE_SAMPLE_PEAK
can call ff_ebur128_sample_peak
unsigned long window
The maximum window duration in ms.
int ff_ebur128_loudness_global(FFEBUR128State *st, double *out)
Get global integrated loudness in LUFS.