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)
161 Ebu_r128_proc::~Ebu_r128_proc (void)
166 void Ebu_r128_proc::init (int nchan, float fsamp)
170 _fragm = (int) fsamp / 20;
171 detect_init (_fsamp);
176 void Ebu_r128_proc::reset (void)
184 _loudness_M = -200.0f;
185 _loudness_S = -200.0f;
186 memset (_power, 0, 64 * sizeof (float));
192 void Ebu_r128_proc::integr_reset (void)
196 _maxloudn_M = -200.0f;
197 _maxloudn_S = -200.0f;
198 _integrated = -200.0f;
199 _integ_thr = -200.0f;
200 _range_min = -200.0f;
201 _range_max = -200.0f;
202 _range_thr = -200.0f;
207 void Ebu_r128_proc::process (int nfram, float *input [])
211 for (i = 0; i < _nchan; i++) _ipp [i] = input [i];
214 k = (_frcnt < nfram) ? _frcnt : nfram;
215 _frpwr += detect_process (k);
219 _power [_wrind++] = _frpwr / _fragm;
223 _loudness_M = addfrags (8);
224 _loudness_S = addfrags (60);
225 if (_loudness_M > _maxloudn_M) _maxloudn_M = _loudness_M;
226 if (_loudness_S > _maxloudn_S) _maxloudn_S = _loudness_S;
231 _hist_M.addpoint (_loudness_M);
236 _hist_S.addpoint (_loudness_S);
238 _hist_M.calc_integ (&_integrated, &_integ_thr);
239 _hist_S.calc_range (&_range_min, &_range_max, &_range_thr);
243 for (i = 0; i < _nchan; i++) _ipp [i] += k;
249 float Ebu_r128_proc::addfrags (int nfrag)
255 k = (_wrind - nfrag) & 63;
256 for (i = 0; i < nfrag; i++) s += _power [(i + k) & 63];
257 return -0.6976f + 10 * log10f (s / nfrag);
261 void Ebu_r128_proc::detect_init (float fsamp)
263 float a, b, c, d, r, u1, u2, w1, w2;
265 r = 1 / tan (4712.3890f / fsamp);
268 u1 = u2 = 1.4085f + 210.0f / fsamp;
274 _a0 = (1 + c + d) / r;
275 _a1 = (2 - 2 * d) / r;
276 _a2 = (1 - c + d) / r;
277 _b1 = (2 - 2 * b) / r;
278 _b2 = (1 - a + b) / r;
281 b = 6.2298014f * r * r;
294 void Ebu_r128_proc::detect_reset (void)
296 for (int i = 0; i < MAXCH; i++) _fst [i].reset ();
300 float Ebu_r128_proc::detect_process (int nfram)
304 float x, y, z1, z2, z3, z4;
309 for (i = 0, S = _fst; i < _nchan; i++, S++)
317 for (j = 0; j < nfram; j++)
319 x = p [j] - _b1 * z1 - _b2 * z2 + 1e-15f;
320 y = _a0 * x + _a1 * z1 + _a2 * z2 - _c3 * z3 - _c4 * z4;
327 if (_nchan == 1) si = 2 * sj;
328 else si += _chan_gain [i] * sj;