You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

771 lines
30KB

  1. /*
  2. * Copyright (c) 2011 Jan Kokemüller
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * This file is based on libebur128 which is available at
  21. * https://github.com/jiixyj/libebur128/
  22. *
  23. * Libebur128 has the following copyright:
  24. *
  25. * Permission is hereby granted, free of charge, to any person obtaining a copy
  26. * of this software and associated documentation files (the "Software"), to deal
  27. * in the Software without restriction, including without limitation the rights
  28. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  29. * copies of the Software, and to permit persons to whom the Software is
  30. * furnished to do so, subject to the following conditions:
  31. *
  32. * The above copyright notice and this permission notice shall be included in
  33. * all copies or substantial portions of the Software.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  36. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  38. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  39. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  40. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  41. * THE SOFTWARE.
  42. */
  43. #include "ebur128.h"
  44. #include <float.h>
  45. #include <limits.h>
  46. #include <math.h> /* You may have to define _USE_MATH_DEFINES if you use MSVC */
  47. #include "libavutil/common.h"
  48. #include "libavutil/mem.h"
  49. #include "libavutil/mem_internal.h"
  50. #include "libavutil/thread.h"
  51. #define CHECK_ERROR(condition, errorcode, goto_point) \
  52. if ((condition)) { \
  53. errcode = (errorcode); \
  54. goto goto_point; \
  55. }
  56. #define ALMOST_ZERO 0.000001
  57. #define RELATIVE_GATE (-10.0)
  58. #define RELATIVE_GATE_FACTOR pow(10.0, RELATIVE_GATE / 10.0)
  59. #define MINUS_20DB pow(10.0, -20.0 / 10.0)
  60. struct FFEBUR128StateInternal {
  61. /** Filtered audio data (used as ring buffer). */
  62. double *audio_data;
  63. /** Size of audio_data array. */
  64. size_t audio_data_frames;
  65. /** Current index for audio_data. */
  66. size_t audio_data_index;
  67. /** How many frames are needed for a gating block. Will correspond to 400ms
  68. * of audio at initialization, and 100ms after the first block (75% overlap
  69. * as specified in the 2011 revision of BS1770). */
  70. unsigned long needed_frames;
  71. /** The channel map. Has as many elements as there are channels. */
  72. int *channel_map;
  73. /** How many samples fit in 100ms (rounded). */
  74. unsigned long samples_in_100ms;
  75. /** BS.1770 filter coefficients (nominator). */
  76. double b[5];
  77. /** BS.1770 filter coefficients (denominator). */
  78. double a[5];
  79. /** BS.1770 filter state. */
  80. double v[5][5];
  81. /** Histograms, used to calculate LRA. */
  82. unsigned long *block_energy_histogram;
  83. unsigned long *short_term_block_energy_histogram;
  84. /** Keeps track of when a new short term block is needed. */
  85. size_t short_term_frame_counter;
  86. /** Maximum sample peak, one per channel */
  87. double *sample_peak;
  88. /** The maximum window duration in ms. */
  89. unsigned long window;
  90. /** Data pointer array for interleaved data */
  91. void **data_ptrs;
  92. };
  93. static AVOnce histogram_init = AV_ONCE_INIT;
  94. static DECLARE_ALIGNED(32, double, histogram_energies)[1000];
  95. static DECLARE_ALIGNED(32, double, histogram_energy_boundaries)[1001];
  96. static void ebur128_init_filter(FFEBUR128State * st)
  97. {
  98. int i, j;
  99. double f0 = 1681.974450955533;
  100. double G = 3.999843853973347;
  101. double Q = 0.7071752369554196;
  102. double K = tan(M_PI * f0 / (double) st->samplerate);
  103. double Vh = pow(10.0, G / 20.0);
  104. double Vb = pow(Vh, 0.4996667741545416);
  105. double pb[3] = { 0.0, 0.0, 0.0 };
  106. double pa[3] = { 1.0, 0.0, 0.0 };
  107. double rb[3] = { 1.0, -2.0, 1.0 };
  108. double ra[3] = { 1.0, 0.0, 0.0 };
  109. double a0 = 1.0 + K / Q + K * K;
  110. pb[0] = (Vh + Vb * K / Q + K * K) / a0;
  111. pb[1] = 2.0 * (K * K - Vh) / a0;
  112. pb[2] = (Vh - Vb * K / Q + K * K) / a0;
  113. pa[1] = 2.0 * (K * K - 1.0) / a0;
  114. pa[2] = (1.0 - K / Q + K * K) / a0;
  115. f0 = 38.13547087602444;
  116. Q = 0.5003270373238773;
  117. K = tan(M_PI * f0 / (double) st->samplerate);
  118. ra[1] = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
  119. ra[2] = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
  120. st->d->b[0] = pb[0] * rb[0];
  121. st->d->b[1] = pb[0] * rb[1] + pb[1] * rb[0];
  122. st->d->b[2] = pb[0] * rb[2] + pb[1] * rb[1] + pb[2] * rb[0];
  123. st->d->b[3] = pb[1] * rb[2] + pb[2] * rb[1];
  124. st->d->b[4] = pb[2] * rb[2];
  125. st->d->a[0] = pa[0] * ra[0];
  126. st->d->a[1] = pa[0] * ra[1] + pa[1] * ra[0];
  127. st->d->a[2] = pa[0] * ra[2] + pa[1] * ra[1] + pa[2] * ra[0];
  128. st->d->a[3] = pa[1] * ra[2] + pa[2] * ra[1];
  129. st->d->a[4] = pa[2] * ra[2];
  130. for (i = 0; i < 5; ++i) {
  131. for (j = 0; j < 5; ++j) {
  132. st->d->v[i][j] = 0.0;
  133. }
  134. }
  135. }
  136. static int ebur128_init_channel_map(FFEBUR128State * st)
  137. {
  138. size_t i;
  139. st->d->channel_map =
  140. (int *) av_malloc_array(st->channels, sizeof(*st->d->channel_map));
  141. if (!st->d->channel_map)
  142. return AVERROR(ENOMEM);
  143. if (st->channels == 4) {
  144. st->d->channel_map[0] = FF_EBUR128_LEFT;
  145. st->d->channel_map[1] = FF_EBUR128_RIGHT;
  146. st->d->channel_map[2] = FF_EBUR128_LEFT_SURROUND;
  147. st->d->channel_map[3] = FF_EBUR128_RIGHT_SURROUND;
  148. } else if (st->channels == 5) {
  149. st->d->channel_map[0] = FF_EBUR128_LEFT;
  150. st->d->channel_map[1] = FF_EBUR128_RIGHT;
  151. st->d->channel_map[2] = FF_EBUR128_CENTER;
  152. st->d->channel_map[3] = FF_EBUR128_LEFT_SURROUND;
  153. st->d->channel_map[4] = FF_EBUR128_RIGHT_SURROUND;
  154. } else {
  155. for (i = 0; i < st->channels; ++i) {
  156. switch (i) {
  157. case 0:
  158. st->d->channel_map[i] = FF_EBUR128_LEFT;
  159. break;
  160. case 1:
  161. st->d->channel_map[i] = FF_EBUR128_RIGHT;
  162. break;
  163. case 2:
  164. st->d->channel_map[i] = FF_EBUR128_CENTER;
  165. break;
  166. case 3:
  167. st->d->channel_map[i] = FF_EBUR128_UNUSED;
  168. break;
  169. case 4:
  170. st->d->channel_map[i] = FF_EBUR128_LEFT_SURROUND;
  171. break;
  172. case 5:
  173. st->d->channel_map[i] = FF_EBUR128_RIGHT_SURROUND;
  174. break;
  175. default:
  176. st->d->channel_map[i] = FF_EBUR128_UNUSED;
  177. break;
  178. }
  179. }
  180. }
  181. return 0;
  182. }
  183. static inline void init_histogram(void)
  184. {
  185. int i;
  186. /* initialize static constants */
  187. histogram_energy_boundaries[0] = pow(10.0, (-70.0 + 0.691) / 10.0);
  188. for (i = 0; i < 1000; ++i) {
  189. histogram_energies[i] =
  190. pow(10.0, ((double) i / 10.0 - 69.95 + 0.691) / 10.0);
  191. }
  192. for (i = 1; i < 1001; ++i) {
  193. histogram_energy_boundaries[i] =
  194. pow(10.0, ((double) i / 10.0 - 70.0 + 0.691) / 10.0);
  195. }
  196. }
  197. FFEBUR128State *ff_ebur128_init(unsigned int channels,
  198. unsigned long samplerate,
  199. unsigned long window, int mode)
  200. {
  201. int errcode;
  202. FFEBUR128State *st;
  203. st = (FFEBUR128State *) av_malloc(sizeof(*st));
  204. CHECK_ERROR(!st, 0, exit)
  205. st->d = (struct FFEBUR128StateInternal *)
  206. av_malloc(sizeof(*st->d));
  207. CHECK_ERROR(!st->d, 0, free_state)
  208. st->channels = channels;
  209. errcode = ebur128_init_channel_map(st);
  210. CHECK_ERROR(errcode, 0, free_internal)
  211. st->d->sample_peak =
  212. (double *) av_mallocz_array(channels, sizeof(*st->d->sample_peak));
  213. CHECK_ERROR(!st->d->sample_peak, 0, free_channel_map)
  214. st->samplerate = samplerate;
  215. st->d->samples_in_100ms = (st->samplerate + 5) / 10;
  216. st->mode = mode;
  217. if ((mode & FF_EBUR128_MODE_S) == FF_EBUR128_MODE_S) {
  218. st->d->window = FFMAX(window, 3000);
  219. } else if ((mode & FF_EBUR128_MODE_M) == FF_EBUR128_MODE_M) {
  220. st->d->window = FFMAX(window, 400);
  221. } else {
  222. goto free_sample_peak;
  223. }
  224. st->d->audio_data_frames = st->samplerate * st->d->window / 1000;
  225. if (st->d->audio_data_frames % st->d->samples_in_100ms) {
  226. /* round up to multiple of samples_in_100ms */
  227. st->d->audio_data_frames = st->d->audio_data_frames
  228. + st->d->samples_in_100ms
  229. - (st->d->audio_data_frames % st->d->samples_in_100ms);
  230. }
  231. st->d->audio_data =
  232. (double *) av_mallocz_array(st->d->audio_data_frames,
  233. st->channels * sizeof(*st->d->audio_data));
  234. CHECK_ERROR(!st->d->audio_data, 0, free_sample_peak)
  235. ebur128_init_filter(st);
  236. st->d->block_energy_histogram =
  237. av_mallocz(1000 * sizeof(*st->d->block_energy_histogram));
  238. CHECK_ERROR(!st->d->block_energy_histogram, 0, free_audio_data)
  239. st->d->short_term_block_energy_histogram =
  240. av_mallocz(1000 * sizeof(*st->d->short_term_block_energy_histogram));
  241. CHECK_ERROR(!st->d->short_term_block_energy_histogram, 0,
  242. free_block_energy_histogram)
  243. st->d->short_term_frame_counter = 0;
  244. /* the first block needs 400ms of audio data */
  245. st->d->needed_frames = st->d->samples_in_100ms * 4;
  246. /* start at the beginning of the buffer */
  247. st->d->audio_data_index = 0;
  248. if (ff_thread_once(&histogram_init, &init_histogram) != 0)
  249. goto free_short_term_block_energy_histogram;
  250. st->d->data_ptrs = av_malloc_array(channels, sizeof(*st->d->data_ptrs));
  251. CHECK_ERROR(!st->d->data_ptrs, 0,
  252. free_short_term_block_energy_histogram);
  253. return st;
  254. free_short_term_block_energy_histogram:
  255. av_free(st->d->short_term_block_energy_histogram);
  256. free_block_energy_histogram:
  257. av_free(st->d->block_energy_histogram);
  258. free_audio_data:
  259. av_free(st->d->audio_data);
  260. free_sample_peak:
  261. av_free(st->d->sample_peak);
  262. free_channel_map:
  263. av_free(st->d->channel_map);
  264. free_internal:
  265. av_free(st->d);
  266. free_state:
  267. av_free(st);
  268. exit:
  269. return NULL;
  270. }
  271. void ff_ebur128_destroy(FFEBUR128State ** st)
  272. {
  273. av_free((*st)->d->block_energy_histogram);
  274. av_free((*st)->d->short_term_block_energy_histogram);
  275. av_free((*st)->d->audio_data);
  276. av_free((*st)->d->channel_map);
  277. av_free((*st)->d->sample_peak);
  278. av_free((*st)->d->data_ptrs);
  279. av_free((*st)->d);
  280. av_free(*st);
  281. *st = NULL;
  282. }
  283. #define EBUR128_FILTER(type, scaling_factor) \
  284. static void ebur128_filter_##type(FFEBUR128State* st, const type** srcs, \
  285. size_t src_index, size_t frames, \
  286. int stride) { \
  287. double* audio_data = st->d->audio_data + st->d->audio_data_index; \
  288. size_t i, c; \
  289. \
  290. if ((st->mode & FF_EBUR128_MODE_SAMPLE_PEAK) == FF_EBUR128_MODE_SAMPLE_PEAK) { \
  291. for (c = 0; c < st->channels; ++c) { \
  292. double max = 0.0; \
  293. for (i = 0; i < frames; ++i) { \
  294. type v = srcs[c][src_index + i * stride]; \
  295. if (v > max) { \
  296. max = v; \
  297. } else if (-v > max) { \
  298. max = -1.0 * v; \
  299. } \
  300. } \
  301. max /= scaling_factor; \
  302. if (max > st->d->sample_peak[c]) st->d->sample_peak[c] = max; \
  303. } \
  304. } \
  305. for (c = 0; c < st->channels; ++c) { \
  306. int ci = st->d->channel_map[c] - 1; \
  307. if (ci < 0) continue; \
  308. else if (ci == FF_EBUR128_DUAL_MONO - 1) ci = 0; /*dual mono */ \
  309. for (i = 0; i < frames; ++i) { \
  310. st->d->v[ci][0] = (double) (srcs[c][src_index + i * stride] / scaling_factor) \
  311. - st->d->a[1] * st->d->v[ci][1] \
  312. - st->d->a[2] * st->d->v[ci][2] \
  313. - st->d->a[3] * st->d->v[ci][3] \
  314. - st->d->a[4] * st->d->v[ci][4]; \
  315. audio_data[i * st->channels + c] = \
  316. st->d->b[0] * st->d->v[ci][0] \
  317. + st->d->b[1] * st->d->v[ci][1] \
  318. + st->d->b[2] * st->d->v[ci][2] \
  319. + st->d->b[3] * st->d->v[ci][3] \
  320. + st->d->b[4] * st->d->v[ci][4]; \
  321. st->d->v[ci][4] = st->d->v[ci][3]; \
  322. st->d->v[ci][3] = st->d->v[ci][2]; \
  323. st->d->v[ci][2] = st->d->v[ci][1]; \
  324. st->d->v[ci][1] = st->d->v[ci][0]; \
  325. } \
  326. st->d->v[ci][4] = fabs(st->d->v[ci][4]) < DBL_MIN ? 0.0 : st->d->v[ci][4]; \
  327. st->d->v[ci][3] = fabs(st->d->v[ci][3]) < DBL_MIN ? 0.0 : st->d->v[ci][3]; \
  328. st->d->v[ci][2] = fabs(st->d->v[ci][2]) < DBL_MIN ? 0.0 : st->d->v[ci][2]; \
  329. st->d->v[ci][1] = fabs(st->d->v[ci][1]) < DBL_MIN ? 0.0 : st->d->v[ci][1]; \
  330. } \
  331. }
  332. EBUR128_FILTER(short, -((double)SHRT_MIN))
  333. EBUR128_FILTER(int, -((double)INT_MIN))
  334. EBUR128_FILTER(float, 1.0)
  335. EBUR128_FILTER(double, 1.0)
  336. static double ebur128_energy_to_loudness(double energy)
  337. {
  338. return 10 * log10(energy) - 0.691;
  339. }
  340. static size_t find_histogram_index(double energy)
  341. {
  342. size_t index_min = 0;
  343. size_t index_max = 1000;
  344. size_t index_mid;
  345. do {
  346. index_mid = (index_min + index_max) / 2;
  347. if (energy >= histogram_energy_boundaries[index_mid]) {
  348. index_min = index_mid;
  349. } else {
  350. index_max = index_mid;
  351. }
  352. } while (index_max - index_min != 1);
  353. return index_min;
  354. }
  355. static void ebur128_calc_gating_block(FFEBUR128State * st,
  356. size_t frames_per_block,
  357. double *optional_output)
  358. {
  359. size_t i, c;
  360. double sum = 0.0;
  361. double channel_sum;
  362. for (c = 0; c < st->channels; ++c) {
  363. if (st->d->channel_map[c] == FF_EBUR128_UNUSED)
  364. continue;
  365. channel_sum = 0.0;
  366. if (st->d->audio_data_index < frames_per_block * st->channels) {
  367. for (i = 0; i < st->d->audio_data_index / st->channels; ++i) {
  368. channel_sum += st->d->audio_data[i * st->channels + c] *
  369. st->d->audio_data[i * st->channels + c];
  370. }
  371. for (i = st->d->audio_data_frames -
  372. (frames_per_block -
  373. st->d->audio_data_index / st->channels);
  374. i < st->d->audio_data_frames; ++i) {
  375. channel_sum += st->d->audio_data[i * st->channels + c] *
  376. st->d->audio_data[i * st->channels + c];
  377. }
  378. } else {
  379. for (i =
  380. st->d->audio_data_index / st->channels - frames_per_block;
  381. i < st->d->audio_data_index / st->channels; ++i) {
  382. channel_sum +=
  383. st->d->audio_data[i * st->channels +
  384. c] * st->d->audio_data[i *
  385. st->channels +
  386. c];
  387. }
  388. }
  389. if (st->d->channel_map[c] == FF_EBUR128_Mp110 ||
  390. st->d->channel_map[c] == FF_EBUR128_Mm110 ||
  391. st->d->channel_map[c] == FF_EBUR128_Mp060 ||
  392. st->d->channel_map[c] == FF_EBUR128_Mm060 ||
  393. st->d->channel_map[c] == FF_EBUR128_Mp090 ||
  394. st->d->channel_map[c] == FF_EBUR128_Mm090) {
  395. channel_sum *= 1.41;
  396. } else if (st->d->channel_map[c] == FF_EBUR128_DUAL_MONO) {
  397. channel_sum *= 2.0;
  398. }
  399. sum += channel_sum;
  400. }
  401. sum /= (double) frames_per_block;
  402. if (optional_output) {
  403. *optional_output = sum;
  404. } else if (sum >= histogram_energy_boundaries[0]) {
  405. ++st->d->block_energy_histogram[find_histogram_index(sum)];
  406. }
  407. }
  408. int ff_ebur128_set_channel(FFEBUR128State * st,
  409. unsigned int channel_number, int value)
  410. {
  411. if (channel_number >= st->channels) {
  412. return 1;
  413. }
  414. if (value == FF_EBUR128_DUAL_MONO &&
  415. (st->channels != 1 || channel_number != 0)) {
  416. return 1;
  417. }
  418. st->d->channel_map[channel_number] = value;
  419. return 0;
  420. }
  421. static int ebur128_energy_shortterm(FFEBUR128State * st, double *out);
  422. #define FF_EBUR128_ADD_FRAMES_PLANAR(type) \
  423. void ff_ebur128_add_frames_planar_##type(FFEBUR128State* st, const type** srcs, \
  424. size_t frames, int stride) { \
  425. size_t src_index = 0; \
  426. while (frames > 0) { \
  427. if (frames >= st->d->needed_frames) { \
  428. ebur128_filter_##type(st, srcs, src_index, st->d->needed_frames, stride); \
  429. src_index += st->d->needed_frames * stride; \
  430. frames -= st->d->needed_frames; \
  431. st->d->audio_data_index += st->d->needed_frames * st->channels; \
  432. /* calculate the new gating block */ \
  433. if ((st->mode & FF_EBUR128_MODE_I) == FF_EBUR128_MODE_I) { \
  434. ebur128_calc_gating_block(st, st->d->samples_in_100ms * 4, NULL); \
  435. } \
  436. if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
  437. st->d->short_term_frame_counter += st->d->needed_frames; \
  438. if (st->d->short_term_frame_counter == st->d->samples_in_100ms * 30) { \
  439. double st_energy; \
  440. ebur128_energy_shortterm(st, &st_energy); \
  441. if (st_energy >= histogram_energy_boundaries[0]) { \
  442. ++st->d->short_term_block_energy_histogram[ \
  443. find_histogram_index(st_energy)]; \
  444. } \
  445. st->d->short_term_frame_counter = st->d->samples_in_100ms * 20; \
  446. } \
  447. } \
  448. /* 100ms are needed for all blocks besides the first one */ \
  449. st->d->needed_frames = st->d->samples_in_100ms; \
  450. /* reset audio_data_index when buffer full */ \
  451. if (st->d->audio_data_index == st->d->audio_data_frames * st->channels) { \
  452. st->d->audio_data_index = 0; \
  453. } \
  454. } else { \
  455. ebur128_filter_##type(st, srcs, src_index, frames, stride); \
  456. st->d->audio_data_index += frames * st->channels; \
  457. if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
  458. st->d->short_term_frame_counter += frames; \
  459. } \
  460. st->d->needed_frames -= frames; \
  461. frames = 0; \
  462. } \
  463. } \
  464. }
  465. FF_EBUR128_ADD_FRAMES_PLANAR(short)
  466. FF_EBUR128_ADD_FRAMES_PLANAR(int)
  467. FF_EBUR128_ADD_FRAMES_PLANAR(float)
  468. FF_EBUR128_ADD_FRAMES_PLANAR(double)
  469. #define FF_EBUR128_ADD_FRAMES(type) \
  470. void ff_ebur128_add_frames_##type(FFEBUR128State* st, const type* src, \
  471. size_t frames) { \
  472. int i; \
  473. const type **buf = (const type**)st->d->data_ptrs; \
  474. for (i = 0; i < st->channels; i++) \
  475. buf[i] = src + i; \
  476. ff_ebur128_add_frames_planar_##type(st, buf, frames, st->channels); \
  477. }
  478. FF_EBUR128_ADD_FRAMES(short)
  479. FF_EBUR128_ADD_FRAMES(int)
  480. FF_EBUR128_ADD_FRAMES(float)
  481. FF_EBUR128_ADD_FRAMES(double)
  482. static int ebur128_calc_relative_threshold(FFEBUR128State **sts, size_t size,
  483. double *relative_threshold)
  484. {
  485. size_t i, j;
  486. int above_thresh_counter = 0;
  487. *relative_threshold = 0.0;
  488. for (i = 0; i < size; i++) {
  489. unsigned long *block_energy_histogram = sts[i]->d->block_energy_histogram;
  490. for (j = 0; j < 1000; ++j) {
  491. *relative_threshold += block_energy_histogram[j] * histogram_energies[j];
  492. above_thresh_counter += block_energy_histogram[j];
  493. }
  494. }
  495. if (above_thresh_counter != 0) {
  496. *relative_threshold /= (double)above_thresh_counter;
  497. *relative_threshold *= RELATIVE_GATE_FACTOR;
  498. }
  499. return above_thresh_counter;
  500. }
  501. static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size,
  502. double *out)
  503. {
  504. double gated_loudness = 0.0;
  505. double relative_threshold;
  506. size_t above_thresh_counter;
  507. size_t i, j, start_index;
  508. for (i = 0; i < size; i++)
  509. if ((sts[i]->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I)
  510. return AVERROR(EINVAL);
  511. if (!ebur128_calc_relative_threshold(sts, size, &relative_threshold)) {
  512. *out = -HUGE_VAL;
  513. return 0;
  514. }
  515. above_thresh_counter = 0;
  516. if (relative_threshold < histogram_energy_boundaries[0]) {
  517. start_index = 0;
  518. } else {
  519. start_index = find_histogram_index(relative_threshold);
  520. if (relative_threshold > histogram_energies[start_index]) {
  521. ++start_index;
  522. }
  523. }
  524. for (i = 0; i < size; i++) {
  525. for (j = start_index; j < 1000; ++j) {
  526. gated_loudness += sts[i]->d->block_energy_histogram[j] *
  527. histogram_energies[j];
  528. above_thresh_counter += sts[i]->d->block_energy_histogram[j];
  529. }
  530. }
  531. if (!above_thresh_counter) {
  532. *out = -HUGE_VAL;
  533. return 0;
  534. }
  535. gated_loudness /= (double) above_thresh_counter;
  536. *out = ebur128_energy_to_loudness(gated_loudness);
  537. return 0;
  538. }
  539. int ff_ebur128_relative_threshold(FFEBUR128State * st, double *out)
  540. {
  541. double relative_threshold;
  542. if ((st->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I)
  543. return AVERROR(EINVAL);
  544. if (!ebur128_calc_relative_threshold(&st, 1, &relative_threshold)) {
  545. *out = -70.0;
  546. return 0;
  547. }
  548. *out = ebur128_energy_to_loudness(relative_threshold);
  549. return 0;
  550. }
  551. int ff_ebur128_loudness_global(FFEBUR128State * st, double *out)
  552. {
  553. return ebur128_gated_loudness(&st, 1, out);
  554. }
  555. int ff_ebur128_loudness_global_multiple(FFEBUR128State ** sts, size_t size,
  556. double *out)
  557. {
  558. return ebur128_gated_loudness(sts, size, out);
  559. }
  560. static int ebur128_energy_in_interval(FFEBUR128State * st,
  561. size_t interval_frames, double *out)
  562. {
  563. if (interval_frames > st->d->audio_data_frames) {
  564. return AVERROR(EINVAL);
  565. }
  566. ebur128_calc_gating_block(st, interval_frames, out);
  567. return 0;
  568. }
  569. static int ebur128_energy_shortterm(FFEBUR128State * st, double *out)
  570. {
  571. return ebur128_energy_in_interval(st, st->d->samples_in_100ms * 30,
  572. out);
  573. }
  574. int ff_ebur128_loudness_momentary(FFEBUR128State * st, double *out)
  575. {
  576. double energy;
  577. int error = ebur128_energy_in_interval(st, st->d->samples_in_100ms * 4,
  578. &energy);
  579. if (error) {
  580. return error;
  581. } else if (energy <= 0.0) {
  582. *out = -HUGE_VAL;
  583. return 0;
  584. }
  585. *out = ebur128_energy_to_loudness(energy);
  586. return 0;
  587. }
  588. int ff_ebur128_loudness_shortterm(FFEBUR128State * st, double *out)
  589. {
  590. double energy;
  591. int error = ebur128_energy_shortterm(st, &energy);
  592. if (error) {
  593. return error;
  594. } else if (energy <= 0.0) {
  595. *out = -HUGE_VAL;
  596. return 0;
  597. }
  598. *out = ebur128_energy_to_loudness(energy);
  599. return 0;
  600. }
  601. int ff_ebur128_loudness_window(FFEBUR128State * st,
  602. unsigned long window, double *out)
  603. {
  604. double energy;
  605. size_t interval_frames = st->samplerate * window / 1000;
  606. int error = ebur128_energy_in_interval(st, interval_frames, &energy);
  607. if (error) {
  608. return error;
  609. } else if (energy <= 0.0) {
  610. *out = -HUGE_VAL;
  611. return 0;
  612. }
  613. *out = ebur128_energy_to_loudness(energy);
  614. return 0;
  615. }
  616. /* EBU - TECH 3342 */
  617. int ff_ebur128_loudness_range_multiple(FFEBUR128State ** sts, size_t size,
  618. double *out)
  619. {
  620. size_t i, j;
  621. size_t stl_size;
  622. double stl_power, stl_integrated;
  623. /* High and low percentile energy */
  624. double h_en, l_en;
  625. unsigned long hist[1000] = { 0 };
  626. size_t percentile_low, percentile_high;
  627. size_t index;
  628. for (i = 0; i < size; ++i) {
  629. if (sts[i]) {
  630. if ((sts[i]->mode & FF_EBUR128_MODE_LRA) !=
  631. FF_EBUR128_MODE_LRA) {
  632. return AVERROR(EINVAL);
  633. }
  634. }
  635. }
  636. stl_size = 0;
  637. stl_power = 0.0;
  638. for (i = 0; i < size; ++i) {
  639. if (!sts[i])
  640. continue;
  641. for (j = 0; j < 1000; ++j) {
  642. hist[j] += sts[i]->d->short_term_block_energy_histogram[j];
  643. stl_size += sts[i]->d->short_term_block_energy_histogram[j];
  644. stl_power += sts[i]->d->short_term_block_energy_histogram[j]
  645. * histogram_energies[j];
  646. }
  647. }
  648. if (!stl_size) {
  649. *out = 0.0;
  650. return 0;
  651. }
  652. stl_power /= stl_size;
  653. stl_integrated = MINUS_20DB * stl_power;
  654. if (stl_integrated < histogram_energy_boundaries[0]) {
  655. index = 0;
  656. } else {
  657. index = find_histogram_index(stl_integrated);
  658. if (stl_integrated > histogram_energies[index]) {
  659. ++index;
  660. }
  661. }
  662. stl_size = 0;
  663. for (j = index; j < 1000; ++j) {
  664. stl_size += hist[j];
  665. }
  666. if (!stl_size) {
  667. *out = 0.0;
  668. return 0;
  669. }
  670. percentile_low = (size_t) ((stl_size - 1) * 0.1 + 0.5);
  671. percentile_high = (size_t) ((stl_size - 1) * 0.95 + 0.5);
  672. stl_size = 0;
  673. j = index;
  674. while (stl_size <= percentile_low) {
  675. stl_size += hist[j++];
  676. }
  677. l_en = histogram_energies[j - 1];
  678. while (stl_size <= percentile_high) {
  679. stl_size += hist[j++];
  680. }
  681. h_en = histogram_energies[j - 1];
  682. *out =
  683. ebur128_energy_to_loudness(h_en) -
  684. ebur128_energy_to_loudness(l_en);
  685. return 0;
  686. }
  687. int ff_ebur128_loudness_range(FFEBUR128State * st, double *out)
  688. {
  689. return ff_ebur128_loudness_range_multiple(&st, 1, out);
  690. }
  691. int ff_ebur128_sample_peak(FFEBUR128State * st,
  692. unsigned int channel_number, double *out)
  693. {
  694. if ((st->mode & FF_EBUR128_MODE_SAMPLE_PEAK) !=
  695. FF_EBUR128_MODE_SAMPLE_PEAK) {
  696. return AVERROR(EINVAL);
  697. } else if (channel_number >= st->channels) {
  698. return AVERROR(EINVAL);
  699. }
  700. *out = st->d->sample_peak[channel_number];
  701. return 0;
  702. }