1 /* See COPYING file for copyright and license details. */
7 #include <math.h> /* You may have to define _USE_MATH_DEFINES if you use MSVC */
11 /* This can be replaced by any BSD-like queue implementation. */
14 #ifdef USE_SPEEX_RESAMPLER
15 #include <speex/speex_resampler.h>
18 #define CHECK_ERROR(condition, errorcode, goto_point) \
20 errcode = (errorcode); \
24 SLIST_HEAD(ebur128_double_queue, ebur128_dq_entry);
25 struct ebur128_dq_entry {
27 SLIST_ENTRY(ebur128_dq_entry) entries;
30 struct ebur128_state_internal {
31 /** Filtered audio data (used as ring buffer). */
33 /** Size of audio_data array. */
34 size_t audio_data_frames;
35 /** Current index for audio_data. */
36 size_t audio_data_index;
37 /** How many frames are needed for a gating block. Will correspond to 400ms
38 * of audio at initialization, and 100ms after the first block (75% overlap
39 * as specified in the 2011 revision of BS1770). */
40 unsigned long needed_frames;
41 /** The channel map. Has as many elements as there are channels. */
43 /** How many samples fit in 100ms (rounded). */
44 unsigned long samples_in_100ms;
45 /** BS.1770 filter coefficients (nominator). */
47 /** BS.1770 filter coefficients (denominator). */
49 /** BS.1770 filter state. */
51 /** Linked list of block energies. */
52 struct ebur128_double_queue block_list;
53 /** Linked list of 3s-block energies, used to calculate LRA. */
54 struct ebur128_double_queue short_term_block_list;
56 unsigned long *block_energy_histogram;
57 unsigned long *short_term_block_energy_histogram;
58 /** Keeps track of when a new short term block is needed. */
59 size_t short_term_frame_counter;
60 /** Maximum sample peak, one per channel */
62 /** Maximum true peak, one per channel */
64 #ifdef USE_SPEEX_RESAMPLER
65 SpeexResamplerState* resampler;
67 size_t oversample_factor;
68 float* resampler_buffer_input;
69 size_t resampler_buffer_input_frames;
70 float* resampler_buffer_output;
71 size_t resampler_buffer_output_frames;
74 static double relative_gate = -10.0;
76 /* Those will be calculated when initializing the library */
77 static double relative_gate_factor;
78 static double minus_twenty_decibels;
79 static double histogram_energies[1000];
80 static double histogram_energy_boundaries[1001];
82 static void ebur128_init_filter(ebur128_state* st) {
85 double f0 = 1681.974450955533;
86 double G = 3.999843853973347;
87 double Q = 0.7071752369554196;
89 double K = tan(M_PI * f0 / (double) st->samplerate);
90 double Vh = pow(10.0, G / 20.0);
91 double Vb = pow(Vh, 0.4996667741545416);
93 double pb[3] = {0.0, 0.0, 0.0};
94 double pa[3] = {1.0, 0.0, 0.0};
95 double rb[3] = {1.0, -2.0, 1.0};
96 double ra[3] = {1.0, 0.0, 0.0};
98 double a0 = 1.0 + K / Q + K * K ;
99 pb[0] = (Vh + Vb * K / Q + K * K) / a0;
100 pb[1] = 2.0 * (K * K - Vh) / a0;
101 pb[2] = (Vh - Vb * K / Q + K * K) / a0;
102 pa[1] = 2.0 * (K * K - 1.0) / a0;
103 pa[2] = (1.0 - K / Q + K * K) / a0;
105 /* fprintf(stderr, "%.14f %.14f %.14f %.14f %.14f\n",
106 b1[0], b1[1], b1[2], a1[1], a1[2]); */
108 f0 = 38.13547087602444;
109 Q = 0.5003270373238773;
110 K = tan(M_PI * f0 / (double) st->samplerate);
112 ra[1] = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
113 ra[2] = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
115 /* fprintf(stderr, "%.14f %.14f\n", a2[1], a2[2]); */
117 st->d->b[0] = pb[0] * rb[0];
118 st->d->b[1] = pb[0] * rb[1] + pb[1] * rb[0];
119 st->d->b[2] = pb[0] * rb[2] + pb[1] * rb[1] + pb[2] * rb[0];
120 st->d->b[3] = pb[1] * rb[2] + pb[2] * rb[1];
121 st->d->b[4] = pb[2] * rb[2];
123 st->d->a[0] = pa[0] * ra[0];
124 st->d->a[1] = pa[0] * ra[1] + pa[1] * ra[0];
125 st->d->a[2] = pa[0] * ra[2] + pa[1] * ra[1] + pa[2] * ra[0];
126 st->d->a[3] = pa[1] * ra[2] + pa[2] * ra[1];
127 st->d->a[4] = pa[2] * ra[2];
129 for (i = 0; i < 5; ++i) {
130 for (j = 0; j < 5; ++j) {
131 st->d->v[i][j] = 0.0;
136 static int ebur128_init_channel_map(ebur128_state* st) {
138 st->d->channel_map = (int*) malloc(st->channels * sizeof(int));
139 if (!st->d->channel_map) return EBUR128_ERROR_NOMEM;
140 if (st->channels == 4) {
141 st->d->channel_map[0] = EBUR128_LEFT;
142 st->d->channel_map[1] = EBUR128_RIGHT;
143 st->d->channel_map[2] = EBUR128_LEFT_SURROUND;
144 st->d->channel_map[3] = EBUR128_RIGHT_SURROUND;
145 } else if (st->channels == 5) {
146 st->d->channel_map[0] = EBUR128_LEFT;
147 st->d->channel_map[1] = EBUR128_RIGHT;
148 st->d->channel_map[2] = EBUR128_CENTER;
149 st->d->channel_map[3] = EBUR128_LEFT_SURROUND;
150 st->d->channel_map[4] = EBUR128_RIGHT_SURROUND;
152 for (i = 0; i < st->channels; ++i) {
154 case 0: st->d->channel_map[i] = EBUR128_LEFT; break;
155 case 1: st->d->channel_map[i] = EBUR128_RIGHT; break;
156 case 2: st->d->channel_map[i] = EBUR128_CENTER; break;
157 case 3: st->d->channel_map[i] = EBUR128_UNUSED; break;
158 case 4: st->d->channel_map[i] = EBUR128_LEFT_SURROUND; break;
159 case 5: st->d->channel_map[i] = EBUR128_RIGHT_SURROUND; break;
160 default: st->d->channel_map[i] = EBUR128_UNUSED; break;
164 return EBUR128_SUCCESS;
167 #ifdef USE_SPEEX_RESAMPLER
168 static int ebur128_init_resampler(ebur128_state* st) {
169 int errcode = EBUR128_SUCCESS;
171 if (st->samplerate < 96000) {
172 st->d->oversample_factor = 4;
173 } else if (st->samplerate < 192000) {
174 st->d->oversample_factor = 2;
176 st->d->oversample_factor = 1;
177 st->d->resampler_buffer_input = NULL;
178 st->d->resampler_buffer_output = NULL;
179 st->d->resampler = NULL;
182 st->d->resampler_buffer_input_frames = st->d->samples_in_100ms * 4;
183 st->d->resampler_buffer_input = malloc(st->d->resampler_buffer_input_frames *
186 CHECK_ERROR(!st->d->resampler_buffer_input, EBUR128_ERROR_NOMEM, exit)
188 st->d->resampler_buffer_output_frames =
189 st->d->resampler_buffer_input_frames *
190 st->d->oversample_factor;
191 st->d->resampler_buffer_output = malloc
192 (st->d->resampler_buffer_output_frames *
195 CHECK_ERROR(!st->d->resampler_buffer_output, EBUR128_ERROR_NOMEM, free_input)
197 st->d->resampler = speex_resampler_init
198 ((spx_uint32_t) st->channels,
199 (spx_uint32_t) st->samplerate,
200 (spx_uint32_t) (st->samplerate * st->d->oversample_factor),
202 CHECK_ERROR(!st->d->resampler, EBUR128_ERROR_NOMEM, free_output)
207 free(st->d->resampler_buffer_output);
208 st->d->resampler_buffer_output = NULL;
210 free(st->d->resampler_buffer_input);
211 st->d->resampler_buffer_input = NULL;
216 static void ebur128_destroy_resampler(ebur128_state* st) {
217 free(st->d->resampler_buffer_input);
218 st->d->resampler_buffer_input = NULL;
219 free(st->d->resampler_buffer_output);
220 st->d->resampler_buffer_output = NULL;
221 speex_resampler_destroy(st->d->resampler);
222 st->d->resampler = NULL;
226 void ebur128_get_version(int* major, int* minor, int* patch) {
227 *major = EBUR128_VERSION_MAJOR;
228 *minor = EBUR128_VERSION_MINOR;
229 *patch = EBUR128_VERSION_PATCH;
232 ebur128_state* ebur128_init(unsigned int channels,
233 unsigned long samplerate,
239 st = (ebur128_state*) malloc(sizeof(ebur128_state));
240 CHECK_ERROR(!st, 0, exit)
241 st->d = (struct ebur128_state_internal*)
242 malloc(sizeof(struct ebur128_state_internal));
243 CHECK_ERROR(!st->d, 0, free_state)
244 st->channels = channels;
245 errcode = ebur128_init_channel_map(st);
246 CHECK_ERROR(errcode, 0, free_internal)
248 st->d->sample_peak = (double*) malloc(channels * sizeof(double));
249 CHECK_ERROR(!st->d->sample_peak, 0, free_channel_map)
250 st->d->true_peak = (double*) malloc(channels * sizeof(double));
251 CHECK_ERROR(!st->d->true_peak, 0, free_sample_peak)
252 for (i = 0; i < channels; ++i) {
253 st->d->sample_peak[i] = 0.0;
254 st->d->true_peak[i] = 0.0;
257 st->d->use_histogram = mode & EBUR128_MODE_HISTOGRAM ? 1 : 0;
259 st->samplerate = samplerate;
260 st->d->samples_in_100ms = (st->samplerate + 5) / 10;
262 if ((mode & EBUR128_MODE_S) == EBUR128_MODE_S) {
263 st->d->audio_data_frames = st->d->samples_in_100ms * 30;
264 } else if ((mode & EBUR128_MODE_M) == EBUR128_MODE_M) {
265 st->d->audio_data_frames = st->d->samples_in_100ms * 4;
269 st->d->audio_data = (double*) malloc(st->d->audio_data_frames *
272 CHECK_ERROR(!st->d->audio_data, 0, free_true_peak)
273 ebur128_init_filter(st);
275 if (st->d->use_histogram) {
276 st->d->block_energy_histogram = malloc(1000 * sizeof(unsigned long));
277 CHECK_ERROR(!st->d->block_energy_histogram, 0, free_audio_data)
278 for (i = 0; i < 1000; ++i) {
279 st->d->block_energy_histogram[i] = 0;
282 st->d->block_energy_histogram = NULL;
284 if (st->d->use_histogram) {
285 st->d->short_term_block_energy_histogram = malloc(1000 * sizeof(unsigned long));
286 CHECK_ERROR(!st->d->short_term_block_energy_histogram, 0, free_block_energy_histogram)
287 for (i = 0; i < 1000; ++i) {
288 st->d->short_term_block_energy_histogram[i] = 0;
291 st->d->short_term_block_energy_histogram = NULL;
293 SLIST_INIT(&st->d->block_list);
294 SLIST_INIT(&st->d->short_term_block_list);
295 st->d->short_term_frame_counter = 0;
297 #ifdef USE_SPEEX_RESAMPLER
298 result = ebur128_init_resampler(st);
299 CHECK_ERROR(result, 0, free_short_term_block_energy_histogram)
302 /* the first block needs 400ms of audio data */
303 st->d->needed_frames = st->d->samples_in_100ms * 4;
304 /* start at the beginning of the buffer */
305 st->d->audio_data_index = 0;
307 /* initialize static constants */
308 relative_gate_factor = pow(10.0, relative_gate / 10.0);
309 minus_twenty_decibels = pow(10.0, -20.0 / 10.0);
310 histogram_energy_boundaries[0] = pow(10.0, (-70.0 + 0.691) / 10.0);
311 if (st->d->use_histogram) {
312 for (i = 0; i < 1000; ++i) {
313 histogram_energies[i] = pow(10.0, ((double) i / 10.0 - 69.95 + 0.691) / 10.0);
315 for (i = 1; i < 1001; ++i) {
316 histogram_energy_boundaries[i] = pow(10.0, ((double) i / 10.0 - 70.0 + 0.691) / 10.0);
322 free_short_term_block_energy_histogram:
323 free(st->d->short_term_block_energy_histogram);
324 free_block_energy_histogram:
325 free(st->d->block_energy_histogram);
327 free(st->d->audio_data);
329 free(st->d->true_peak);
331 free(st->d->sample_peak);
333 free(st->d->channel_map);
342 void ebur128_destroy(ebur128_state** st) {
343 struct ebur128_dq_entry* entry;
344 free((*st)->d->block_energy_histogram);
345 free((*st)->d->short_term_block_energy_histogram);
346 free((*st)->d->audio_data);
347 free((*st)->d->channel_map);
348 free((*st)->d->sample_peak);
349 free((*st)->d->true_peak);
350 while (!SLIST_EMPTY(&(*st)->d->block_list)) {
351 entry = SLIST_FIRST(&(*st)->d->block_list);
352 SLIST_REMOVE_HEAD(&(*st)->d->block_list, entries);
355 while (!SLIST_EMPTY(&(*st)->d->short_term_block_list)) {
356 entry = SLIST_FIRST(&(*st)->d->short_term_block_list);
357 SLIST_REMOVE_HEAD(&(*st)->d->short_term_block_list, entries);
360 #ifdef USE_SPEEX_RESAMPLER
361 ebur128_destroy_resampler(*st);
369 static int ebur128_use_speex_resampler(ebur128_state* st) {
370 #ifdef USE_SPEEX_RESAMPLER
371 return ((st->mode & EBUR128_MODE_TRUE_PEAK) == EBUR128_MODE_TRUE_PEAK);
378 static void ebur128_check_true_peak(ebur128_state* st, size_t frames) {
379 #ifdef USE_SPEEX_RESAMPLER
381 spx_uint32_t in_len = (spx_uint32_t) frames;
382 spx_uint32_t out_len = (spx_uint32_t) st->d->resampler_buffer_output_frames;
383 speex_resampler_process_interleaved_float(
385 st->d->resampler_buffer_input, &in_len,
386 st->d->resampler_buffer_output, &out_len);
387 for (c = 0; c < st->channels; ++c) {
388 for (i = 0; i < out_len; ++i) {
389 if (st->d->resampler_buffer_output[i * st->channels + c] >
390 st->d->true_peak[c]) {
391 st->d->true_peak[c] =
392 st->d->resampler_buffer_output[i * st->channels + c];
393 } else if (-st->d->resampler_buffer_output[i * st->channels + c] >
394 st->d->true_peak[c]) {
395 st->d->true_peak[c] =
396 -st->d->resampler_buffer_output[i * st->channels + c];
401 (void) st; (void) frames;
406 #include <xmmintrin.h>
407 #define TURN_ON_FTZ \
408 unsigned int mxcsr = _mm_getcsr(); \
409 _mm_setcsr(mxcsr | _MM_FLUSH_ZERO_ON);
410 #define TURN_OFF_FTZ _mm_setcsr(mxcsr);
411 #define FLUSH_MANUALLY
413 #warning "manual FTZ is being used, please enable SSE2 (-msse2 -mfpmath=sse)"
416 #define FLUSH_MANUALLY \
417 st->d->v[ci][4] = fabs(st->d->v[ci][4]) < DBL_MIN ? 0.0 : st->d->v[ci][4]; \
418 st->d->v[ci][3] = fabs(st->d->v[ci][3]) < DBL_MIN ? 0.0 : st->d->v[ci][3]; \
419 st->d->v[ci][2] = fabs(st->d->v[ci][2]) < DBL_MIN ? 0.0 : st->d->v[ci][2]; \
420 st->d->v[ci][1] = fabs(st->d->v[ci][1]) < DBL_MIN ? 0.0 : st->d->v[ci][1];
423 #define EBUR128_FILTER(type, min_scale, max_scale) \
424 static void ebur128_filter_##type(ebur128_state* st, const type* src, \
426 static double scaling_factor = -((double) min_scale) > (double) max_scale ? \
427 -((double) min_scale) : (double) max_scale; \
428 double* audio_data = st->d->audio_data + st->d->audio_data_index; \
433 if ((st->mode & EBUR128_MODE_SAMPLE_PEAK) == EBUR128_MODE_SAMPLE_PEAK) { \
434 for (c = 0; c < st->channels; ++c) { \
436 for (i = 0; i < frames; ++i) { \
437 if (src[i * st->channels + c] > max) { \
438 max = src[i * st->channels + c]; \
439 } else if (-src[i * st->channels + c] > max) { \
440 max = -1.0 * src[i * st->channels + c]; \
443 max /= scaling_factor; \
444 if (max > st->d->sample_peak[c]) st->d->sample_peak[c] = max; \
447 if (ebur128_use_speex_resampler(st)) { \
448 for (c = 0; c < st->channels; ++c) { \
449 for (i = 0; i < frames; ++i) { \
450 st->d->resampler_buffer_input[i * st->channels + c] = \
451 (float) (src[i * st->channels + c] / scaling_factor); \
454 ebur128_check_true_peak(st, frames); \
456 for (c = 0; c < st->channels; ++c) { \
457 int ci = st->d->channel_map[c] - 1; \
458 if (ci < 0) continue; \
459 else if (ci > 4) ci = 0; /* dual mono */ \
460 for (i = 0; i < frames; ++i) { \
461 st->d->v[ci][0] = (double) (src[i * st->channels + c] / scaling_factor) \
462 - st->d->a[1] * st->d->v[ci][1] \
463 - st->d->a[2] * st->d->v[ci][2] \
464 - st->d->a[3] * st->d->v[ci][3] \
465 - st->d->a[4] * st->d->v[ci][4]; \
466 audio_data[i * st->channels + c] = \
467 st->d->b[0] * st->d->v[ci][0] \
468 + st->d->b[1] * st->d->v[ci][1] \
469 + st->d->b[2] * st->d->v[ci][2] \
470 + st->d->b[3] * st->d->v[ci][3] \
471 + st->d->b[4] * st->d->v[ci][4]; \
472 st->d->v[ci][4] = st->d->v[ci][3]; \
473 st->d->v[ci][3] = st->d->v[ci][2]; \
474 st->d->v[ci][2] = st->d->v[ci][1]; \
475 st->d->v[ci][1] = st->d->v[ci][0]; \
481 EBUR128_FILTER(short, SHRT_MIN, SHRT_MAX)
482 EBUR128_FILTER(int, INT_MIN, INT_MAX)
483 EBUR128_FILTER(float, -1.0f, 1.0f)
484 EBUR128_FILTER(double, -1.0, 1.0)
486 static double ebur128_energy_to_loudness(double energy) {
487 return 10 * (log(energy) / log(10.0)) - 0.691;
490 static size_t find_histogram_index(double energy) {
491 size_t index_min = 0;
492 size_t index_max = 1000;
496 index_mid = (index_min + index_max) / 2;
497 if (energy >= histogram_energy_boundaries[index_mid]) {
498 index_min = index_mid;
500 index_max = index_mid;
502 } while (index_max - index_min != 1);
507 static int ebur128_calc_gating_block(ebur128_state* st, size_t frames_per_block,
508 double* optional_output) {
512 for (c = 0; c < st->channels; ++c) {
513 if (st->d->channel_map[c] == EBUR128_UNUSED) continue;
515 if (st->d->audio_data_index < frames_per_block * st->channels) {
516 for (i = 0; i < st->d->audio_data_index / st->channels; ++i) {
517 channel_sum += st->d->audio_data[i * st->channels + c] *
518 st->d->audio_data[i * st->channels + c];
520 for (i = st->d->audio_data_frames -
522 st->d->audio_data_index / st->channels);
523 i < st->d->audio_data_frames; ++i) {
524 channel_sum += st->d->audio_data[i * st->channels + c] *
525 st->d->audio_data[i * st->channels + c];
528 for (i = st->d->audio_data_index / st->channels - frames_per_block;
529 i < st->d->audio_data_index / st->channels;
531 channel_sum += st->d->audio_data[i * st->channels + c] *
532 st->d->audio_data[i * st->channels + c];
535 if (st->d->channel_map[c] == EBUR128_LEFT_SURROUND ||
536 st->d->channel_map[c] == EBUR128_RIGHT_SURROUND) {
538 } else if (st->d->channel_map[c] == EBUR128_DUAL_MONO) {
543 sum /= (double) frames_per_block;
544 if (optional_output) {
545 *optional_output = sum;
546 return EBUR128_SUCCESS;
547 } else if (sum >= histogram_energy_boundaries[0]) {
548 if (st->d->use_histogram) {
549 ++st->d->block_energy_histogram[find_histogram_index(sum)];
551 struct ebur128_dq_entry* block;
552 block = (struct ebur128_dq_entry*) malloc(sizeof(struct ebur128_dq_entry));
553 if (!block) return EBUR128_ERROR_NOMEM;
555 SLIST_INSERT_HEAD(&st->d->block_list, block, entries);
557 return EBUR128_SUCCESS;
559 return EBUR128_SUCCESS;
563 int ebur128_set_channel(ebur128_state* st,
564 unsigned int channel_number,
566 if (channel_number >= st->channels) {
569 if (value == EBUR128_DUAL_MONO &&
570 (st->channels != 1 || channel_number != 0)) {
571 fprintf(stderr, "EBUR128_DUAL_MONO only works with mono files!\n");
574 st->d->channel_map[channel_number] = value;
578 int ebur128_change_parameters(ebur128_state* st,
579 unsigned int channels,
580 unsigned long samplerate) {
582 if (channels == st->channels &&
583 samplerate == st->samplerate) {
586 free(st->d->audio_data);
587 st->d->audio_data = NULL;
589 if (channels != st->channels) {
592 free(st->d->channel_map); st->d->channel_map = NULL;
593 free(st->d->sample_peak); st->d->sample_peak = NULL;
594 free(st->d->true_peak); st->d->true_peak = NULL;
595 st->channels = channels;
597 #ifdef USE_SPEEX_RESAMPLER
598 ebur128_destroy_resampler(st);
599 ebur128_init_resampler(st);
602 errcode = ebur128_init_channel_map(st);
603 CHECK_ERROR(errcode, EBUR128_ERROR_NOMEM, exit)
605 st->d->sample_peak = (double*) malloc(channels * sizeof(double));
606 CHECK_ERROR(!st->d->sample_peak, EBUR128_ERROR_NOMEM, exit)
607 st->d->true_peak = (double*) malloc(channels * sizeof(double));
608 CHECK_ERROR(!st->d->true_peak, EBUR128_ERROR_NOMEM, exit)
609 for (i = 0; i < channels; ++i) {
610 st->d->sample_peak[i] = 0.0;
611 st->d->true_peak[i] = 0.0;
614 if (samplerate != st->samplerate) {
615 st->samplerate = samplerate;
616 ebur128_init_filter(st);
618 if ((st->mode & EBUR128_MODE_S) == EBUR128_MODE_S) {
619 st->d->audio_data_frames = st->d->samples_in_100ms * 30;
620 } else if ((st->mode & EBUR128_MODE_M) == EBUR128_MODE_M) {
621 st->d->audio_data_frames = st->d->samples_in_100ms * 4;
625 st->d->audio_data = (double*) malloc(st->d->audio_data_frames *
628 CHECK_ERROR(!st->d->audio_data, EBUR128_ERROR_NOMEM, exit)
630 /* the first block needs 400ms of audio data */
631 st->d->needed_frames = st->d->samples_in_100ms * 4;
632 /* start at the beginning of the buffer */
633 st->d->audio_data_index = 0;
634 /* reset short term frame counter */
635 st->d->short_term_frame_counter = 0;
644 static int ebur128_energy_shortterm(ebur128_state* st, double* out);
645 #define EBUR128_ADD_FRAMES(type) \
646 int ebur128_add_frames_##type(ebur128_state* st, \
647 const type* src, size_t frames) { \
648 size_t src_index = 0; \
649 while (frames > 0) { \
650 if (frames >= st->d->needed_frames) { \
651 ebur128_filter_##type(st, src + src_index, st->d->needed_frames); \
652 src_index += st->d->needed_frames * st->channels; \
653 frames -= st->d->needed_frames; \
654 st->d->audio_data_index += st->d->needed_frames * st->channels; \
655 /* calculate the new gating block */ \
656 if ((st->mode & EBUR128_MODE_I) == EBUR128_MODE_I) { \
657 if (ebur128_calc_gating_block(st, st->d->samples_in_100ms * 4, NULL)) {\
658 return EBUR128_ERROR_NOMEM; \
661 if ((st->mode & EBUR128_MODE_LRA) == EBUR128_MODE_LRA) { \
662 st->d->short_term_frame_counter += st->d->needed_frames; \
663 if (st->d->short_term_frame_counter == st->d->samples_in_100ms * 30) { \
664 struct ebur128_dq_entry* block; \
666 ebur128_energy_shortterm(st, &st_energy); \
667 if (st_energy >= histogram_energy_boundaries[0]) { \
668 if (st->d->use_histogram) { \
669 ++st->d->short_term_block_energy_histogram[ \
670 find_histogram_index(st_energy)];\
672 block = (struct ebur128_dq_entry*) \
673 malloc(sizeof(struct ebur128_dq_entry)); \
674 if (!block) return EBUR128_ERROR_NOMEM; \
675 block->z = st_energy; \
676 SLIST_INSERT_HEAD(&st->d->short_term_block_list, block, entries);\
679 st->d->short_term_frame_counter = st->d->samples_in_100ms * 20; \
682 /* 100ms are needed for all blocks besides the first one */ \
683 st->d->needed_frames = st->d->samples_in_100ms; \
684 /* reset audio_data_index when buffer full */ \
685 if (st->d->audio_data_index == st->d->audio_data_frames * st->channels) {\
686 st->d->audio_data_index = 0; \
689 ebur128_filter_##type(st, src + src_index, frames); \
690 st->d->audio_data_index += frames * st->channels; \
691 if ((st->mode & EBUR128_MODE_LRA) == EBUR128_MODE_LRA) { \
692 st->d->short_term_frame_counter += frames; \
694 st->d->needed_frames -= frames; \
698 return EBUR128_SUCCESS; \
700 EBUR128_ADD_FRAMES(short)
701 EBUR128_ADD_FRAMES(int)
702 EBUR128_ADD_FRAMES(float)
703 EBUR128_ADD_FRAMES(double)
705 static int ebur128_gated_loudness(ebur128_state** sts, size_t size,
707 struct ebur128_dq_entry* it;
708 double relative_threshold = 0.0;
709 double gated_loudness = 0.0;
710 size_t above_thresh_counter = 0;
711 size_t i, j, start_index;
713 for (i = 0; i < size; i++) {
714 if (sts[i] && (sts[i]->mode & EBUR128_MODE_I) != EBUR128_MODE_I) {
715 return EBUR128_ERROR_INVALID_MODE;
719 for (i = 0; i < size; i++) {
720 if (!sts[i]) continue;
721 if (sts[i]->d->use_histogram) {
722 for (j = 0; j < 1000; ++j) {
723 relative_threshold += sts[i]->d->block_energy_histogram[j] *
724 histogram_energies[j];
725 above_thresh_counter += sts[i]->d->block_energy_histogram[j];
728 SLIST_FOREACH(it, &sts[i]->d->block_list, entries) {
729 ++above_thresh_counter;
730 relative_threshold += it->z;
734 if (!above_thresh_counter) {
736 return EBUR128_SUCCESS;
738 relative_threshold /= (double) above_thresh_counter;
739 relative_threshold *= relative_gate_factor;
740 above_thresh_counter = 0;
741 if (relative_threshold < histogram_energy_boundaries[0]) {
744 start_index = find_histogram_index(relative_threshold);
745 if (relative_threshold > histogram_energies[start_index]) {
749 for (i = 0; i < size; i++) {
750 if (!sts[i]) continue;
751 if (sts[i]->d->use_histogram) {
752 for (j = start_index; j < 1000; ++j) {
753 gated_loudness += sts[i]->d->block_energy_histogram[j] *
754 histogram_energies[j];
755 above_thresh_counter += sts[i]->d->block_energy_histogram[j];
758 SLIST_FOREACH(it, &sts[i]->d->block_list, entries) {
759 if (it->z >= relative_threshold) {
760 ++above_thresh_counter;
761 gated_loudness += it->z;
766 if (!above_thresh_counter) {
768 return EBUR128_SUCCESS;
770 gated_loudness /= (double) above_thresh_counter;
771 *out = ebur128_energy_to_loudness(gated_loudness);
772 return EBUR128_SUCCESS;
775 int ebur128_loudness_global(ebur128_state* st, double* out) {
776 return ebur128_gated_loudness(&st, 1, out);
779 int ebur128_loudness_global_multiple(ebur128_state** sts, size_t size,
781 return ebur128_gated_loudness(sts, size, out);
784 static int ebur128_energy_in_interval(ebur128_state* st,
785 size_t interval_frames,
787 if (interval_frames > st->d->audio_data_frames) {
788 return EBUR128_ERROR_INVALID_MODE;
790 ebur128_calc_gating_block(st, interval_frames, out);
791 return EBUR128_SUCCESS;
794 static int ebur128_energy_shortterm(ebur128_state* st, double* out) {
795 return ebur128_energy_in_interval(st, st->d->samples_in_100ms * 30, out);
798 int ebur128_loudness_momentary(ebur128_state* st, double* out) {
800 int error = ebur128_energy_in_interval(st, st->d->samples_in_100ms * 4,
804 } else if (energy <= 0.0) {
806 return EBUR128_SUCCESS;
808 *out = ebur128_energy_to_loudness(energy);
809 return EBUR128_SUCCESS;
812 int ebur128_loudness_shortterm(ebur128_state* st, double* out) {
814 int error = ebur128_energy_shortterm(st, &energy);
817 } else if (energy <= 0.0) {
819 return EBUR128_SUCCESS;
821 *out = ebur128_energy_to_loudness(energy);
822 return EBUR128_SUCCESS;
825 static int ebur128_double_cmp(const void *p1, const void *p2) {
826 const double* d1 = (const double*) p1;
827 const double* d2 = (const double*) p2;
828 return (*d1 > *d2) - (*d1 < *d2);
831 /* EBU - TECH 3342 */
832 int ebur128_loudness_range_multiple(ebur128_state** sts, size_t size,
835 struct ebur128_dq_entry* it;
838 double* stl_relgated;
839 size_t stl_relgated_size;
840 double stl_power, stl_integrated;
841 /* High and low percentile energy */
843 int use_histogram = 0;
845 for (i = 0; i < size; ++i) {
847 if ((sts[i]->mode & EBUR128_MODE_LRA) != EBUR128_MODE_LRA) {
848 return EBUR128_ERROR_INVALID_MODE;
850 if (i == 0 && sts[i]->mode & EBUR128_MODE_HISTOGRAM) {
852 } else if (use_histogram != !!(sts[i]->mode & EBUR128_MODE_HISTOGRAM)) {
853 return EBUR128_ERROR_INVALID_MODE;
859 unsigned long hist[1000] = { 0 };
860 size_t percentile_low, percentile_high;
865 for (i = 0; i < size; ++i) {
866 if (!sts[i]) continue;
867 for (j = 0; j < 1000; ++j) {
868 hist[j] += sts[i]->d->short_term_block_energy_histogram[j];
869 stl_size += sts[i]->d->short_term_block_energy_histogram[j];
870 stl_power += sts[i]->d->short_term_block_energy_histogram[j]
871 * histogram_energies[j];
876 return EBUR128_SUCCESS;
879 stl_power /= stl_size;
880 stl_integrated = minus_twenty_decibels * stl_power;
882 if (stl_integrated < histogram_energy_boundaries[0]) {
885 index = find_histogram_index(stl_integrated);
886 if (stl_integrated > histogram_energies[index]) {
891 for (j = index; j < 1000; ++j) {
896 return EBUR128_SUCCESS;
899 percentile_low = (size_t) ((stl_size - 1) * 0.1 + 0.5);
900 percentile_high = (size_t) ((stl_size - 1) * 0.95 + 0.5);
904 while (stl_size <= percentile_low) {
905 stl_size += hist[j++];
907 l_en = histogram_energies[j - 1];
908 while (stl_size <= percentile_high) {
909 stl_size += hist[j++];
911 h_en = histogram_energies[j - 1];
912 *out = ebur128_energy_to_loudness(h_en) - ebur128_energy_to_loudness(l_en);
913 return EBUR128_SUCCESS;
917 for (i = 0; i < size; ++i) {
918 if (!sts[i]) continue;
919 SLIST_FOREACH(it, &sts[i]->d->short_term_block_list, entries) {
925 return EBUR128_SUCCESS;
927 stl_vector = (double*) malloc(stl_size * sizeof(double));
929 return EBUR128_ERROR_NOMEM;
931 for (j = 0, i = 0; i < size; ++i) {
932 if (!sts[i]) continue;
933 SLIST_FOREACH(it, &sts[i]->d->short_term_block_list, entries) {
934 stl_vector[j] = it->z;
938 qsort(stl_vector, stl_size, sizeof(double), ebur128_double_cmp);
940 for (i = 0; i < stl_size; ++i) {
941 stl_power += stl_vector[i];
943 stl_power /= (double) stl_size;
944 stl_integrated = minus_twenty_decibels * stl_power;
946 stl_relgated = stl_vector;
947 stl_relgated_size = stl_size;
948 while (stl_relgated_size > 0 && *stl_relgated < stl_integrated) {
953 if (stl_relgated_size) {
954 h_en = stl_relgated[(size_t) ((stl_relgated_size - 1) * 0.95 + 0.5)];
955 l_en = stl_relgated[(size_t) ((stl_relgated_size - 1) * 0.1 + 0.5)];
957 *out = ebur128_energy_to_loudness(h_en) - ebur128_energy_to_loudness(l_en);
958 return EBUR128_SUCCESS;
962 return EBUR128_SUCCESS;
967 int ebur128_loudness_range(ebur128_state* st, double* out) {
968 return ebur128_loudness_range_multiple(&st, 1, out);
971 int ebur128_sample_peak(ebur128_state* st,
972 unsigned int channel_number,
974 if ((st->mode & EBUR128_MODE_SAMPLE_PEAK) != EBUR128_MODE_SAMPLE_PEAK) {
975 return EBUR128_ERROR_INVALID_MODE;
976 } else if (channel_number >= st->channels) {
977 return EBUR128_ERROR_INVALID_CHANNEL_INDEX;
979 *out = st->d->sample_peak[channel_number];
980 return EBUR128_SUCCESS;
983 #ifdef USE_SPEEX_RESAMPLER
984 int ebur128_true_peak(ebur128_state* st,
985 unsigned int channel_number,
987 if ((st->mode & EBUR128_MODE_TRUE_PEAK) != EBUR128_MODE_TRUE_PEAK) {
988 return EBUR128_ERROR_INVALID_MODE;
989 } else if (channel_number >= st->channels) {
990 return EBUR128_ERROR_INVALID_CHANNEL_INDEX;
992 *out = st->d->true_peak[channel_number] > st->d->sample_peak[channel_number]
993 ? st->d->true_peak[channel_number]
994 : st->d->sample_peak[channel_number];
995 return EBUR128_SUCCESS;