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.

725 lines
28KB

  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(double, 1.0)
  333. static double ebur128_energy_to_loudness(double energy)
  334. {
  335. return 10 * log10(energy) - 0.691;
  336. }
  337. static size_t find_histogram_index(double energy)
  338. {
  339. size_t index_min = 0;
  340. size_t index_max = 1000;
  341. size_t index_mid;
  342. do {
  343. index_mid = (index_min + index_max) / 2;
  344. if (energy >= histogram_energy_boundaries[index_mid]) {
  345. index_min = index_mid;
  346. } else {
  347. index_max = index_mid;
  348. }
  349. } while (index_max - index_min != 1);
  350. return index_min;
  351. }
  352. static void ebur128_calc_gating_block(FFEBUR128State * st,
  353. size_t frames_per_block,
  354. double *optional_output)
  355. {
  356. size_t i, c;
  357. double sum = 0.0;
  358. double channel_sum;
  359. for (c = 0; c < st->channels; ++c) {
  360. if (st->d->channel_map[c] == FF_EBUR128_UNUSED)
  361. continue;
  362. channel_sum = 0.0;
  363. if (st->d->audio_data_index < frames_per_block * st->channels) {
  364. for (i = 0; i < st->d->audio_data_index / st->channels; ++i) {
  365. channel_sum += st->d->audio_data[i * st->channels + c] *
  366. st->d->audio_data[i * st->channels + c];
  367. }
  368. for (i = st->d->audio_data_frames -
  369. (frames_per_block -
  370. st->d->audio_data_index / st->channels);
  371. i < st->d->audio_data_frames; ++i) {
  372. channel_sum += st->d->audio_data[i * st->channels + c] *
  373. st->d->audio_data[i * st->channels + c];
  374. }
  375. } else {
  376. for (i =
  377. st->d->audio_data_index / st->channels - frames_per_block;
  378. i < st->d->audio_data_index / st->channels; ++i) {
  379. channel_sum +=
  380. st->d->audio_data[i * st->channels +
  381. c] * st->d->audio_data[i *
  382. st->channels +
  383. c];
  384. }
  385. }
  386. if (st->d->channel_map[c] == FF_EBUR128_Mp110 ||
  387. st->d->channel_map[c] == FF_EBUR128_Mm110 ||
  388. st->d->channel_map[c] == FF_EBUR128_Mp060 ||
  389. st->d->channel_map[c] == FF_EBUR128_Mm060 ||
  390. st->d->channel_map[c] == FF_EBUR128_Mp090 ||
  391. st->d->channel_map[c] == FF_EBUR128_Mm090) {
  392. channel_sum *= 1.41;
  393. } else if (st->d->channel_map[c] == FF_EBUR128_DUAL_MONO) {
  394. channel_sum *= 2.0;
  395. }
  396. sum += channel_sum;
  397. }
  398. sum /= (double) frames_per_block;
  399. if (optional_output) {
  400. *optional_output = sum;
  401. } else if (sum >= histogram_energy_boundaries[0]) {
  402. ++st->d->block_energy_histogram[find_histogram_index(sum)];
  403. }
  404. }
  405. int ff_ebur128_set_channel(FFEBUR128State * st,
  406. unsigned int channel_number, int value)
  407. {
  408. if (channel_number >= st->channels) {
  409. return 1;
  410. }
  411. if (value == FF_EBUR128_DUAL_MONO &&
  412. (st->channels != 1 || channel_number != 0)) {
  413. return 1;
  414. }
  415. st->d->channel_map[channel_number] = value;
  416. return 0;
  417. }
  418. static int ebur128_energy_shortterm(FFEBUR128State * st, double *out);
  419. #define EBUR128_ADD_FRAMES_PLANAR(type) \
  420. static void ebur128_add_frames_planar_##type(FFEBUR128State* st, const type** srcs, \
  421. size_t frames, int stride) { \
  422. size_t src_index = 0; \
  423. while (frames > 0) { \
  424. if (frames >= st->d->needed_frames) { \
  425. ebur128_filter_##type(st, srcs, src_index, st->d->needed_frames, stride); \
  426. src_index += st->d->needed_frames * stride; \
  427. frames -= st->d->needed_frames; \
  428. st->d->audio_data_index += st->d->needed_frames * st->channels; \
  429. /* calculate the new gating block */ \
  430. if ((st->mode & FF_EBUR128_MODE_I) == FF_EBUR128_MODE_I) { \
  431. ebur128_calc_gating_block(st, st->d->samples_in_100ms * 4, NULL); \
  432. } \
  433. if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
  434. st->d->short_term_frame_counter += st->d->needed_frames; \
  435. if (st->d->short_term_frame_counter == st->d->samples_in_100ms * 30) { \
  436. double st_energy; \
  437. ebur128_energy_shortterm(st, &st_energy); \
  438. if (st_energy >= histogram_energy_boundaries[0]) { \
  439. ++st->d->short_term_block_energy_histogram[ \
  440. find_histogram_index(st_energy)]; \
  441. } \
  442. st->d->short_term_frame_counter = st->d->samples_in_100ms * 20; \
  443. } \
  444. } \
  445. /* 100ms are needed for all blocks besides the first one */ \
  446. st->d->needed_frames = st->d->samples_in_100ms; \
  447. /* reset audio_data_index when buffer full */ \
  448. if (st->d->audio_data_index == st->d->audio_data_frames * st->channels) { \
  449. st->d->audio_data_index = 0; \
  450. } \
  451. } else { \
  452. ebur128_filter_##type(st, srcs, src_index, frames, stride); \
  453. st->d->audio_data_index += frames * st->channels; \
  454. if ((st->mode & FF_EBUR128_MODE_LRA) == FF_EBUR128_MODE_LRA) { \
  455. st->d->short_term_frame_counter += frames; \
  456. } \
  457. st->d->needed_frames -= frames; \
  458. frames = 0; \
  459. } \
  460. } \
  461. }
  462. EBUR128_ADD_FRAMES_PLANAR(double)
  463. #define FF_EBUR128_ADD_FRAMES(type) \
  464. void ff_ebur128_add_frames_##type(FFEBUR128State* st, const type* src, \
  465. size_t frames) { \
  466. int i; \
  467. const type **buf = (const type**)st->d->data_ptrs; \
  468. for (i = 0; i < st->channels; i++) \
  469. buf[i] = src + i; \
  470. ebur128_add_frames_planar_##type(st, buf, frames, st->channels); \
  471. }
  472. FF_EBUR128_ADD_FRAMES(double)
  473. static int ebur128_calc_relative_threshold(FFEBUR128State **sts, size_t size,
  474. double *relative_threshold)
  475. {
  476. size_t i, j;
  477. int above_thresh_counter = 0;
  478. *relative_threshold = 0.0;
  479. for (i = 0; i < size; i++) {
  480. unsigned long *block_energy_histogram = sts[i]->d->block_energy_histogram;
  481. for (j = 0; j < 1000; ++j) {
  482. *relative_threshold += block_energy_histogram[j] * histogram_energies[j];
  483. above_thresh_counter += block_energy_histogram[j];
  484. }
  485. }
  486. if (above_thresh_counter != 0) {
  487. *relative_threshold /= (double)above_thresh_counter;
  488. *relative_threshold *= RELATIVE_GATE_FACTOR;
  489. }
  490. return above_thresh_counter;
  491. }
  492. static int ebur128_gated_loudness(FFEBUR128State ** sts, size_t size,
  493. double *out)
  494. {
  495. double gated_loudness = 0.0;
  496. double relative_threshold;
  497. size_t above_thresh_counter;
  498. size_t i, j, start_index;
  499. for (i = 0; i < size; i++)
  500. if ((sts[i]->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I)
  501. return AVERROR(EINVAL);
  502. if (!ebur128_calc_relative_threshold(sts, size, &relative_threshold)) {
  503. *out = -HUGE_VAL;
  504. return 0;
  505. }
  506. above_thresh_counter = 0;
  507. if (relative_threshold < histogram_energy_boundaries[0]) {
  508. start_index = 0;
  509. } else {
  510. start_index = find_histogram_index(relative_threshold);
  511. if (relative_threshold > histogram_energies[start_index]) {
  512. ++start_index;
  513. }
  514. }
  515. for (i = 0; i < size; i++) {
  516. for (j = start_index; j < 1000; ++j) {
  517. gated_loudness += sts[i]->d->block_energy_histogram[j] *
  518. histogram_energies[j];
  519. above_thresh_counter += sts[i]->d->block_energy_histogram[j];
  520. }
  521. }
  522. if (!above_thresh_counter) {
  523. *out = -HUGE_VAL;
  524. return 0;
  525. }
  526. gated_loudness /= (double) above_thresh_counter;
  527. *out = ebur128_energy_to_loudness(gated_loudness);
  528. return 0;
  529. }
  530. int ff_ebur128_relative_threshold(FFEBUR128State * st, double *out)
  531. {
  532. double relative_threshold;
  533. if ((st->mode & FF_EBUR128_MODE_I) != FF_EBUR128_MODE_I)
  534. return AVERROR(EINVAL);
  535. if (!ebur128_calc_relative_threshold(&st, 1, &relative_threshold)) {
  536. *out = -70.0;
  537. return 0;
  538. }
  539. *out = ebur128_energy_to_loudness(relative_threshold);
  540. return 0;
  541. }
  542. int ff_ebur128_loudness_global(FFEBUR128State * st, double *out)
  543. {
  544. return ebur128_gated_loudness(&st, 1, out);
  545. }
  546. static int ebur128_energy_in_interval(FFEBUR128State * st,
  547. size_t interval_frames, double *out)
  548. {
  549. if (interval_frames > st->d->audio_data_frames) {
  550. return AVERROR(EINVAL);
  551. }
  552. ebur128_calc_gating_block(st, interval_frames, out);
  553. return 0;
  554. }
  555. static int ebur128_energy_shortterm(FFEBUR128State * st, double *out)
  556. {
  557. return ebur128_energy_in_interval(st, st->d->samples_in_100ms * 30,
  558. out);
  559. }
  560. int ff_ebur128_loudness_shortterm(FFEBUR128State * st, double *out)
  561. {
  562. double energy;
  563. int error = ebur128_energy_shortterm(st, &energy);
  564. if (error) {
  565. return error;
  566. } else if (energy <= 0.0) {
  567. *out = -HUGE_VAL;
  568. return 0;
  569. }
  570. *out = ebur128_energy_to_loudness(energy);
  571. return 0;
  572. }
  573. /* EBU - TECH 3342 */
  574. int ff_ebur128_loudness_range_multiple(FFEBUR128State ** sts, size_t size,
  575. double *out)
  576. {
  577. size_t i, j;
  578. size_t stl_size;
  579. double stl_power, stl_integrated;
  580. /* High and low percentile energy */
  581. double h_en, l_en;
  582. unsigned long hist[1000] = { 0 };
  583. size_t percentile_low, percentile_high;
  584. size_t index;
  585. for (i = 0; i < size; ++i) {
  586. if (sts[i]) {
  587. if ((sts[i]->mode & FF_EBUR128_MODE_LRA) !=
  588. FF_EBUR128_MODE_LRA) {
  589. return AVERROR(EINVAL);
  590. }
  591. }
  592. }
  593. stl_size = 0;
  594. stl_power = 0.0;
  595. for (i = 0; i < size; ++i) {
  596. if (!sts[i])
  597. continue;
  598. for (j = 0; j < 1000; ++j) {
  599. hist[j] += sts[i]->d->short_term_block_energy_histogram[j];
  600. stl_size += sts[i]->d->short_term_block_energy_histogram[j];
  601. stl_power += sts[i]->d->short_term_block_energy_histogram[j]
  602. * histogram_energies[j];
  603. }
  604. }
  605. if (!stl_size) {
  606. *out = 0.0;
  607. return 0;
  608. }
  609. stl_power /= stl_size;
  610. stl_integrated = MINUS_20DB * stl_power;
  611. if (stl_integrated < histogram_energy_boundaries[0]) {
  612. index = 0;
  613. } else {
  614. index = find_histogram_index(stl_integrated);
  615. if (stl_integrated > histogram_energies[index]) {
  616. ++index;
  617. }
  618. }
  619. stl_size = 0;
  620. for (j = index; j < 1000; ++j) {
  621. stl_size += hist[j];
  622. }
  623. if (!stl_size) {
  624. *out = 0.0;
  625. return 0;
  626. }
  627. percentile_low = (size_t) ((stl_size - 1) * 0.1 + 0.5);
  628. percentile_high = (size_t) ((stl_size - 1) * 0.95 + 0.5);
  629. stl_size = 0;
  630. j = index;
  631. while (stl_size <= percentile_low) {
  632. stl_size += hist[j++];
  633. }
  634. l_en = histogram_energies[j - 1];
  635. while (stl_size <= percentile_high) {
  636. stl_size += hist[j++];
  637. }
  638. h_en = histogram_energies[j - 1];
  639. *out =
  640. ebur128_energy_to_loudness(h_en) -
  641. ebur128_energy_to_loudness(l_en);
  642. return 0;
  643. }
  644. int ff_ebur128_loudness_range(FFEBUR128State * st, double *out)
  645. {
  646. return ff_ebur128_loudness_range_multiple(&st, 1, out);
  647. }
  648. int ff_ebur128_sample_peak(FFEBUR128State * st,
  649. unsigned int channel_number, double *out)
  650. {
  651. if ((st->mode & FF_EBUR128_MODE_SAMPLE_PEAK) !=
  652. FF_EBUR128_MODE_SAMPLE_PEAK) {
  653. return AVERROR(EINVAL);
  654. } else if (channel_number >= st->channels) {
  655. return AVERROR(EINVAL);
  656. }
  657. *out = st->d->sample_peak[channel_number];
  658. return 0;
  659. }