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.

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