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 // ------------------------------------------------------------------------
24 #include "ebu_r128_proc.h"
27 float Ebu_r128_hist::_bin_power [100] = { 0.0f };
28 float Ebu_r128_proc::_chan_gain [5] = { 1.0f, 1.0f, 1.0f, 1.41f, 1.41f };
31 Ebu_r128_hist::Ebu_r128_hist (void)
33 _histc = new int [751];
39 Ebu_r128_hist::~Ebu_r128_hist (void)
45 void Ebu_r128_hist::reset (void)
47 memset (_histc, 0, 751 * sizeof (float));
53 void Ebu_r128_hist::initstat (void)
57 if (_bin_power [0]) return;
58 for (i = 0; i < 100; i++)
60 _bin_power [i] = powf (10.0f, i / 100.0f);
65 void Ebu_r128_hist::addpoint (float v)
69 k = (int) floorf (10 * v + 700.5f);
81 float Ebu_r128_hist::integrate (int i)
93 s += k * _bin_power [j++];
104 void Ebu_r128_hist::calc_integ (float *vi, float *th)
115 // Original threshold was -8 dB below result of first integration
116 // if (th) *th = 10 * log10f (s) - 8.0f;
117 // k = (int)(floorf (100 * log10f (s) + 0.5f)) + 620;
118 // Threshold redefined to -10 dB below result of first integration
119 if (th) *th = 10 * log10f (s) - 10.0f;
120 k = (int)(floorf (100 * log10f (s) + 0.5f)) + 600;
123 *vi = 10 * log10f (s);
127 void Ebu_r128_hist::calc_range (float *v0, float *v1, float *th)
139 if (th) *th = 10 * log10f (s) - 20.0f;
140 k = (int)(floorf (100 * log10f (s) + 0.5)) + 500;
142 for (i = k, n = 0; i <= 750; i++) n += _histc [i];
145 for (i = k, s = 0; s < a; i++) s += _histc [i];
146 for (j = 750, s = n; s > b; j--) s -= _histc [j];
147 *v0 = (i - 701) / 10.0f;
148 *v1 = (j - 699) / 10.0f;
154 Ebu_r128_proc::Ebu_r128_proc (void)
160 Ebu_r128_proc::~Ebu_r128_proc (void)
165 void Ebu_r128_proc::init (int nchan, float fsamp)
169 _fragm = (int) fsamp / 20;
170 detect_init (_fsamp);
175 void Ebu_r128_proc::reset (void)
183 _loudness_M = -200.0f;
184 _loudness_S = -200.0f;
185 memset (_power, 0, 64 * sizeof (float));
191 void Ebu_r128_proc::integr_reset (void)
195 _maxloudn_M = -200.0f;
196 _maxloudn_S = -200.0f;
197 _integrated = -200.0f;
198 _integ_thr = -200.0f;
199 _range_min = -200.0f;
200 _range_max = -200.0f;
201 _range_thr = -200.0f;
206 void Ebu_r128_proc::process (int nfram, float *input [])
210 for (i = 0; i < _nchan; i++) _ipp [i] = input [i];
213 k = (_frcnt < nfram) ? _frcnt : nfram;
214 _frpwr += detect_process (k);
218 _power [_wrind++] = _frpwr / _fragm;
222 _loudness_M = addfrags (8);
223 _loudness_S = addfrags (60);
224 if (_loudness_M > _maxloudn_M) _maxloudn_M = _loudness_M;
225 if (_loudness_S > _maxloudn_S) _maxloudn_S = _loudness_S;
230 _hist_M.addpoint (_loudness_M);
235 _hist_S.addpoint (_loudness_S);
237 _hist_M.calc_integ (&_integrated, &_integ_thr);
238 _hist_S.calc_range (&_range_min, &_range_max, &_range_thr);
242 for (i = 0; i < _nchan; i++) _ipp [i] += k;
248 float Ebu_r128_proc::addfrags (int nfrag)
254 k = (_wrind - nfrag) & 63;
255 for (i = 0; i < nfrag; i++) s += _power [(i + k) & 63];
256 return -0.6976f + 10 * log10f (s / nfrag);
260 void Ebu_r128_proc::detect_init (float fsamp)
262 float a, b, c, d, r, u1, u2, w1, w2;
264 r = 1 / tan (4712.3890f / fsamp);
267 u1 = u2 = 1.4085f + 210.0f / fsamp;
273 _a0 = (1 + c + d) / r;
274 _a1 = (2 - 2 * d) / r;
275 _a2 = (1 - c + d) / r;
276 _b1 = (2 - 2 * b) / r;
277 _b2 = (1 - a + b) / r;
280 b = 6.2298014f * r * r;
293 void Ebu_r128_proc::detect_reset (void)
295 for (int i = 0; i < MAXCH; i++) _fst [i].reset ();
299 float Ebu_r128_proc::detect_process (int nfram)
303 float x, y, z1, z2, z3, z4;
308 for (i = 0, S = _fst; i < _nchan; i++, S++)
316 for (j = 0; j < nfram; j++)
318 x = p [j] - _b1 * z1 - _b2 * z2 + 1e-15f;
319 y = _a0 * x + _a1 * z1 + _a2 * z2 - _c3 * z3 - _c4 * z4;
326 if (_nchan == 1) si = 2 * sj;
327 else si += _chan_gain [i] * sj;