Go to the documentation of this file.
55 #define CHECK_ERROR(condition, errorcode, goto_point) \
57 errcode = (errorcode); \
61 #define ALMOST_ZERO 0.000001
63 #define RELATIVE_GATE (-10.0)
64 #define RELATIVE_GATE_FACTOR pow(10.0, RELATIVE_GATE / 10.0)
65 #define MINUS_20DB pow(10.0, -20.0 / 10.0)
109 double f0 = 1681.974450955533;
110 double G = 3.999843853973347;
111 double Q = 0.7071752369554196;
114 double Vh = pow(10.0,
G / 20.0);
115 double Vb = pow(Vh, 0.4996667741545416);
117 double pb[3] = { 0.0, 0.0, 0.0 };
118 double pa[3] = { 1.0, 0.0, 0.0 };
119 double rb[3] = { 1.0, -2.0, 1.0 };
120 double ra[3] = { 1.0, 0.0, 0.0 };
122 double a0 = 1.0 + K / Q + K * K;
123 pb[0] = (Vh + Vb * K / Q + K * K) /
a0;
124 pb[1] = 2.0 * (K * K - Vh) /
a0;
125 pb[2] = (Vh - Vb * K / Q + K * K) /
a0;
126 pa[1] = 2.0 * (K * K - 1.0) /
a0;
127 pa[2] = (1.0 - K / Q + K * K) /
a0;
129 f0 = 38.13547087602444;
130 Q = 0.5003270373238773;
133 ra[1] = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
134 ra[2] = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
136 st->
d->
b[0] = pb[0] * rb[0];
137 st->
d->
b[1] = pb[0] * rb[1] + pb[1] * rb[0];
138 st->
d->
b[2] = pb[0] * rb[2] + pb[1] * rb[1] + pb[2] * rb[0];
139 st->
d->
b[3] = pb[1] * rb[2] + pb[2] * rb[1];
140 st->
d->
b[4] = pb[2] * rb[2];
142 st->
d->
a[0] = pa[0] *
ra[0];
143 st->
d->
a[1] = pa[0] *
ra[1] + pa[1] *
ra[0];
144 st->
d->
a[2] = pa[0] *
ra[2] + pa[1] *
ra[1] + pa[2] *
ra[0];
145 st->
d->
a[3] = pa[1] *
ra[2] + pa[2] *
ra[1];
146 st->
d->
a[4] = pa[2] *
ra[2];
148 for (
i = 0;
i < 5; ++
i) {
149 for (j = 0; j < 5; ++j) {
150 st->
d->
v[
i][j] = 0.0;
208 for (
i = 0;
i < 1000; ++
i) {
210 pow(10.0, ((
double)
i / 10.0 - 69.95 + 0.691) / 10.0);
212 for (
i = 1;
i < 1001; ++
i) {
214 pow(10.0, ((
double)
i / 10.0 - 70.0 + 0.691) / 10.0);
219 unsigned long samplerate,
246 goto free_sample_peak;
268 free_block_energy_histogram)
277 goto free_short_term_block_energy_histogram;
281 free_short_term_block_energy_histogram);
285 free_short_term_block_energy_histogram:
287 free_block_energy_histogram:
305 av_free((*st)->d->block_energy_histogram);
306 av_free((*st)->d->short_term_block_energy_histogram);
308 av_free((*st)->d->channel_map);
309 av_free((*st)->d->sample_peak);
316 #define EBUR128_FILTER(type, scaling_factor) \
317 static void ebur128_filter_##type(FFEBUR128State* st, const type** srcs, \
318 size_t src_index, size_t frames, \
320 double* audio_data = st->d->audio_data + st->d->audio_data_index; \
323 if ((st->mode & FF_EBUR128_MODE_SAMPLE_PEAK) == FF_EBUR128_MODE_SAMPLE_PEAK) { \
324 for (c = 0; c < st->channels; ++c) { \
326 for (i = 0; i < frames; ++i) { \
327 type v = srcs[c][src_index + i * stride]; \
330 } else if (-v > max) { \
334 max /= scaling_factor; \
335 if (max > st->d->sample_peak[c]) st->d->sample_peak[c] = max; \
338 for (c = 0; c < st->channels; ++c) { \
339 int ci = st->d->channel_map[c] - 1; \
340 if (ci < 0) continue; \
341 else if (ci == FF_EBUR128_DUAL_MONO - 1) ci = 0; \
342 for (i = 0; i < frames; ++i) { \
343 st->d->v[ci][0] = (double) (srcs[c][src_index + i * stride] / scaling_factor) \
344 - st->d->a[1] * st->d->v[ci][1] \
345 - st->d->a[2] * st->d->v[ci][2] \
346 - st->d->a[3] * st->d->v[ci][3] \
347 - st->d->a[4] * st->d->v[ci][4]; \
348 audio_data[i * st->channels + c] = \
349 st->d->b[0] * st->d->v[ci][0] \
350 + st->d->b[1] * st->d->v[ci][1] \
351 + st->d->b[2] * st->d->v[ci][2] \
352 + st->d->b[3] * st->d->v[ci][3] \
353 + st->d->b[4] * st->d->v[ci][4]; \
354 st->d->v[ci][4] = st->d->v[ci][3]; \
355 st->d->v[ci][3] = st->d->v[ci][2]; \
356 st->d->v[ci][2] = st->d->v[ci][1]; \
357 st->d->v[ci][1] = st->d->v[ci][0]; \
359 st->d->v[ci][4] = fabs(st->d->v[ci][4]) < DBL_MIN ? 0.0 : st->d->v[ci][4]; \
360 st->d->v[ci][3] = fabs(st->d->v[ci][3]) < DBL_MIN ? 0.0 : st->d->v[ci][3]; \
361 st->d->v[ci][2] = fabs(st->d->v[ci][2]) < DBL_MIN ? 0.0 : st->d->v[ci][2]; \
362 st->d->v[ci][1] = fabs(st->d->v[ci][1]) < DBL_MIN ? 0.0 : st->d->v[ci][1]; \
369 return 10 * log10(energy) - 0.691;
374 size_t index_min = 0;
375 size_t index_max = 1000;
379 index_mid = (index_min + index_max) / 2;
381 index_min = index_mid;
383 index_max = index_mid;
385 }
while (index_max - index_min != 1);
391 size_t frames_per_block,
392 double *optional_output)
409 i < st->
d->audio_data_frames; ++
i) {
416 i < st->
d->audio_data_index / st->
channels; ++
i) {
436 sum /= (double) frames_per_block;
437 if (optional_output) {
438 *optional_output = sum;
445 unsigned int channel_number,
int value)
447 if (channel_number >= st->
channels) {
451 (st->
channels != 1 || channel_number != 0)) {
459 #define EBUR128_ADD_FRAMES_PLANAR(type) \
460 static void ebur128_add_frames_planar_##type(FFEBUR128State* st, const type** srcs, \
461 size_t frames, int stride) { \
462 size_t src_index = 0; \
463 while (frames > 0) { \
464 if (frames >= st->d->needed_frames) { \
465 ebur128_filter_##type(st, srcs, src_index, st->d->needed_frames, stride); \
466 src_index += st->d->needed_frames * stride; \
467 frames -= st->d->needed_frames; \
468 st->d->audio_data_index += st->d->needed_frames * st->channels; \
470 if ((st->mode & FF_EBUR128_MODE_I) == FF_EBUR128_MODE_I) { \
471 ebur128_calc_gating_block(st, st->d->samples_in_100ms * 4, NULL); \
473 if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
474 st->d->short_term_frame_counter += st->d->needed_frames; \
475 if (st->d->short_term_frame_counter == st->d->samples_in_100ms * 30) { \
477 ebur128_energy_shortterm(st, &st_energy); \
478 if (st_energy >= histogram_energy_boundaries[0]) { \
479 ++st->d->short_term_block_energy_histogram[ \
480 find_histogram_index(st_energy)]; \
482 st->d->short_term_frame_counter = st->d->samples_in_100ms * 20; \
486 st->d->needed_frames = st->d->samples_in_100ms; \
488 if (st->d->audio_data_index == st->d->audio_data_frames * st->channels) { \
489 st->d->audio_data_index = 0; \
492 ebur128_filter_##type(st, srcs, src_index, frames, stride); \
493 st->d->audio_data_index += frames * st->channels; \
494 if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
495 st->d->short_term_frame_counter += frames; \
497 st->d->needed_frames -= frames; \
503 #define FF_EBUR128_ADD_FRAMES(type) \
504 void ff_ebur128_add_frames_##type(FFEBUR128State* st, const type* src, \
507 const type **buf = (const type**)st->d->data_ptrs; \
508 for (i = 0; i < st->channels; i++) \
510 ebur128_add_frames_planar_##type(st, buf, frames, st->channels); \
515 double *relative_threshold)
518 int above_thresh_counter = 0;
519 *relative_threshold = 0.0;
523 for (j = 0; j < 1000; ++j) {
529 if (above_thresh_counter != 0) {
530 *relative_threshold /= (double)above_thresh_counter;
534 return above_thresh_counter;
540 double gated_loudness = 0.0;
541 double relative_threshold;
542 size_t above_thresh_counter;
543 size_t i, j, start_index;
554 above_thresh_counter = 0;
564 for (j = start_index; j < 1000; ++j) {
570 if (!above_thresh_counter) {
574 gated_loudness /= (double) above_thresh_counter;
581 double relative_threshold;
601 size_t interval_frames,
double *
out)
622 }
else if (energy <= 0.0) {
636 double stl_power, stl_integrated;
639 unsigned long hist[1000] = { 0 };
640 size_t percentile_low, percentile_high;
657 for (j = 0; j < 1000; ++j) {
669 stl_power /= stl_size;
681 for (j =
index; j < 1000; ++j) {
689 percentile_low = (size_t) ((stl_size - 1) * 0.1 + 0.5);
690 percentile_high = (size_t) ((stl_size - 1) * 0.95 + 0.5);
694 while (stl_size <= percentile_low) {
695 stl_size += hist[j++];
698 while (stl_size <= percentile_high) {
699 stl_size += hist[j++];
714 unsigned int channel_number,
double *
out)
719 }
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)
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
#define DECLARE_ALIGNED(n, t, v)
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.