1 // ------------------------------------------------------------------------
3 // Copyright (C) 2010-2011 Fons Adriaensen <fons@linuxaudio.org>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 // ------------------------------------------------------------------------
22 #include "ebu_r128_proc.h"
28 float Ebu_r128_hist::_bin_power [100] = { 0.0f };
29 float Ebu_r128_proc::_chan_gain [5] = { 1.0f, 1.0f, 1.0f, 1.41f, 1.41f };
32 Ebu_r128_hist::Ebu_r128_hist (void)
34 _histc = new int [751];
40 Ebu_r128_hist::~Ebu_r128_hist (void)
46 void Ebu_r128_hist::reset (void)
48 memset (_histc, 0, 751 * sizeof (float));
54 void Ebu_r128_hist::initstat (void)
58 if (_bin_power [0]) return;
59 for (i = 0; i < 100; i++)
61 _bin_power [i] = powf (10.0f, i / 100.0f);
66 void Ebu_r128_hist::addpoint (float v)
70 k = (int) floorf (10 * v + 700.5f);
82 float Ebu_r128_hist::integrate (int i)
94 s += k * _bin_power [j++];
105 void Ebu_r128_hist::calc_integ (float *vi, float *th)
116 // Original threshold was -8 dB below result of first integration
117 // if (th) *th = 10 * log10f (s) - 8.0f;
118 // k = (int)(floorf (100 * log10f (s) + 0.5f)) + 620;
119 // Threshold redefined to -10 dB below result of first integration
120 if (th) *th = 10 * log10f (s) - 10.0f;
121 k = (int)(floorf (100 * log10f (s) + 0.5f)) + 600;
124 *vi = 10 * log10f (s);
128 void Ebu_r128_hist::calc_range (float *v0, float *v1, float *th)
140 if (th) *th = 10 * log10f (s) - 20.0f;
141 k = (int)(floorf (100 * log10f (s) + 0.5)) + 500;
143 for (i = k, n = 0; i <= 750; i++) n += _histc [i];
146 for (i = k, s = 0; s < a; i++) s += _histc [i];
147 for (j = 750, s = n; s > b; j--) s -= _histc [j];
148 *v0 = (i - 701) / 10.0f;
149 *v1 = (j - 699) / 10.0f;
155 Ebu_r128_proc::Ebu_r128_proc (void)
162 Ebu_r128_proc::~Ebu_r128_proc (void)
167 void Ebu_r128_proc::init (int nchan, float fsamp)
171 _fragm = (int) fsamp / 20;
172 detect_init (_fsamp);
177 void Ebu_r128_proc::reset (void)
185 _loudness_M = -200.0f;
186 _loudness_S = -200.0f;
187 memset (_power, 0, 64 * sizeof (float));
193 void Ebu_r128_proc::integr_reset (void)
197 _maxloudn_M = -200.0f;
198 _maxloudn_S = -200.0f;
199 _integrated = -200.0f;
200 _integ_thr = -200.0f;
201 _range_min = -200.0f;
202 _range_max = -200.0f;
203 _range_thr = -200.0f;
208 void Ebu_r128_proc::process (int nfram, float *input [])
212 for (i = 0; i < _nchan; i++) _ipp [i] = input [i];
215 k = (_frcnt < nfram) ? _frcnt : nfram;
216 _frpwr += detect_process (k);
220 _power [_wrind++] = _frpwr / _fragm;
224 _loudness_M = addfrags (8);
225 _loudness_S = addfrags (60);
226 if (_loudness_M > _maxloudn_M) _maxloudn_M = _loudness_M;
227 if (_loudness_S > _maxloudn_S) _maxloudn_S = _loudness_S;
232 _hist_M.addpoint (_loudness_M);
237 _hist_S.addpoint (_loudness_S);
239 _hist_M.calc_integ (&_integrated, &_integ_thr);
240 _hist_S.calc_range (&_range_min, &_range_max, &_range_thr);
244 for (i = 0; i < _nchan; i++) _ipp [i] += k;
250 float Ebu_r128_proc::addfrags (int nfrag)
256 k = (_wrind - nfrag) & 63;
257 for (i = 0; i < nfrag; i++) s += _power [(i + k) & 63];
258 return -0.6976f + 10 * log10f (s / nfrag);
262 void Ebu_r128_proc::detect_init (float fsamp)
264 float a, b, c, d, r, u1, u2, w1, w2;
266 r = 1 / tan (4712.3890f / fsamp);
269 u1 = u2 = 1.4085f + 210.0f / fsamp;
275 _a0 = (1 + c + d) / r;
276 _a1 = (2 - 2 * d) / r;
277 _a2 = (1 - c + d) / r;
278 _b1 = (2 - 2 * b) / r;
279 _b2 = (1 - a + b) / r;
282 b = 6.2298014f * r * r;
295 void Ebu_r128_proc::detect_reset (void)
297 for (int i = 0; i < MAXCH; i++) _fst [i].reset ();
301 float Ebu_r128_proc::detect_process (int nfram)
305 float x, y, z1, z2, z3, z4;
310 for (i = 0, S = _fst; i < _nchan; i++, S++)
318 for (j = 0; j < nfram; j++)
320 x = p [j] - _b1 * z1 - _b2 * z2 + 1e-15f;
321 y = _a0 * x + _a1 * z1 + _a2 * z2 - _c3 * z3 - _c4 * z4;
328 if (_nchan == 1) si = 2 * sj;
329 else si += _chan_gain [i] * sj;