ebur128.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. */
  24. #ifndef AVFILTER_EBUR128_H
  25. #define AVFILTER_EBUR128_H
  26. /** \file ebur128.h
  27. * \brief libebur128 - a library for loudness measurement according to
  28. * the EBU R128 standard.
  29. */
  30. #include <stddef.h> /* for size_t */
  31. /** \enum channel
  32. * Use these values when setting the channel map with ebur128_set_channel().
  33. * See definitions in ITU R-REC-BS 1770-4
  34. */
  35. enum channel {
  36. FF_EBUR128_UNUSED = 0, /**< unused channel (for example LFE channel) */
  37. FF_EBUR128_LEFT,
  38. FF_EBUR128_Mp030 = 1, /**< itu M+030 */
  39. FF_EBUR128_RIGHT,
  40. FF_EBUR128_Mm030 = 2, /**< itu M-030 */
  41. FF_EBUR128_CENTER,
  42. FF_EBUR128_Mp000 = 3, /**< itu M+000 */
  43. FF_EBUR128_LEFT_SURROUND,
  44. FF_EBUR128_Mp110 = 4, /**< itu M+110 */
  45. FF_EBUR128_RIGHT_SURROUND,
  46. FF_EBUR128_Mm110 = 5, /**< itu M-110 */
  47. FF_EBUR128_DUAL_MONO, /**< a channel that is counted twice */
  48. FF_EBUR128_MpSC, /**< itu M+SC */
  49. FF_EBUR128_MmSC, /**< itu M-SC */
  50. FF_EBUR128_Mp060, /**< itu M+060 */
  51. FF_EBUR128_Mm060, /**< itu M-060 */
  52. FF_EBUR128_Mp090, /**< itu M+090 */
  53. FF_EBUR128_Mm090, /**< itu M-090 */
  54. FF_EBUR128_Mp135, /**< itu M+135 */
  55. FF_EBUR128_Mm135, /**< itu M-135 */
  56. FF_EBUR128_Mp180, /**< itu M+180 */
  57. FF_EBUR128_Up000, /**< itu U+000 */
  58. FF_EBUR128_Up030, /**< itu U+030 */
  59. FF_EBUR128_Um030, /**< itu U-030 */
  60. FF_EBUR128_Up045, /**< itu U+045 */
  61. FF_EBUR128_Um045, /**< itu U-030 */
  62. FF_EBUR128_Up090, /**< itu U+090 */
  63. FF_EBUR128_Um090, /**< itu U-090 */
  64. FF_EBUR128_Up110, /**< itu U+110 */
  65. FF_EBUR128_Um110, /**< itu U-110 */
  66. FF_EBUR128_Up135, /**< itu U+135 */
  67. FF_EBUR128_Um135, /**< itu U-135 */
  68. FF_EBUR128_Up180, /**< itu U+180 */
  69. FF_EBUR128_Tp000, /**< itu T+000 */
  70. FF_EBUR128_Bp000, /**< itu B+000 */
  71. FF_EBUR128_Bp045, /**< itu B+045 */
  72. FF_EBUR128_Bm045 /**< itu B-045 */
  73. };
  74. /** \enum mode
  75. * Use these values in ebur128_init (or'ed). Try to use the lowest possible
  76. * modes that suit your needs, as performance will be better.
  77. */
  78. enum mode {
  79. /** can call ff_ebur128_loudness_momentary */
  80. FF_EBUR128_MODE_M = (1 << 0),
  81. /** can call ff_ebur128_loudness_shortterm */
  82. FF_EBUR128_MODE_S = (1 << 1) | FF_EBUR128_MODE_M,
  83. /** can call ff_ebur128_loudness_global_* and ff_ebur128_relative_threshold */
  84. FF_EBUR128_MODE_I = (1 << 2) | FF_EBUR128_MODE_M,
  85. /** can call ff_ebur128_loudness_range */
  86. FF_EBUR128_MODE_LRA = (1 << 3) | FF_EBUR128_MODE_S,
  87. /** can call ff_ebur128_sample_peak */
  88. FF_EBUR128_MODE_SAMPLE_PEAK = (1 << 4) | FF_EBUR128_MODE_M,
  89. };
  90. /** forward declaration of FFEBUR128StateInternal */
  91. struct FFEBUR128StateInternal;
  92. /** \brief Contains information about the state of a loudness measurement.
  93. *
  94. * You should not need to modify this struct directly.
  95. */
  96. typedef struct FFEBUR128State {
  97. int mode; /**< The current mode. */
  98. unsigned int channels; /**< The number of channels. */
  99. unsigned long samplerate; /**< The sample rate. */
  100. struct FFEBUR128StateInternal *d; /**< Internal state. */
  101. } FFEBUR128State;
  102. /** \brief Initialize library state.
  103. *
  104. * @param channels the number of channels.
  105. * @param samplerate the sample rate.
  106. * @param window set the maximum window size in ms, set to 0 for auto.
  107. * @param mode see the mode enum for possible values.
  108. * @return an initialized library state.
  109. */
  110. FFEBUR128State *ff_ebur128_init(unsigned int channels,
  111. unsigned long samplerate,
  112. unsigned long window, int mode);
  113. /** \brief Destroy library state.
  114. *
  115. * @param st pointer to a library state.
  116. */
  117. void ff_ebur128_destroy(FFEBUR128State ** st);
  118. /** \brief Set channel type.
  119. *
  120. * The default is:
  121. * - 0 -> FF_EBUR128_LEFT
  122. * - 1 -> FF_EBUR128_RIGHT
  123. * - 2 -> FF_EBUR128_CENTER
  124. * - 3 -> FF_EBUR128_UNUSED
  125. * - 4 -> FF_EBUR128_LEFT_SURROUND
  126. * - 5 -> FF_EBUR128_RIGHT_SURROUND
  127. *
  128. * @param st library state.
  129. * @param channel_number zero based channel index.
  130. * @param value channel type from the "channel" enum.
  131. * @return
  132. * - 0 on success.
  133. * - AVERROR(EINVAL) if invalid channel index.
  134. */
  135. int ff_ebur128_set_channel(FFEBUR128State * st,
  136. unsigned int channel_number, int value);
  137. /** \brief Add frames to be processed.
  138. *
  139. * @param st library state.
  140. * @param src array of source frames. Channels must be interleaved.
  141. * @param frames number of frames. Not number of samples!
  142. */
  143. void ff_ebur128_add_frames_short(FFEBUR128State * st,
  144. const short *src, size_t frames);
  145. /** \brief See \ref ebur128_add_frames_short */
  146. void ff_ebur128_add_frames_int(FFEBUR128State * st,
  147. const int *src, size_t frames);
  148. /** \brief See \ref ebur128_add_frames_short */
  149. void ff_ebur128_add_frames_float(FFEBUR128State * st,
  150. const float *src, size_t frames);
  151. /** \brief See \ref ebur128_add_frames_short */
  152. void ff_ebur128_add_frames_double(FFEBUR128State * st,
  153. const double *src, size_t frames);
  154. /** \brief Add frames to be processed.
  155. *
  156. * @param st library state.
  157. * @param srcs array of source frame channel data pointers
  158. * @param frames number of frames. Not number of samples!
  159. * @param stride number of samples to skip to for the next sample of the same channel
  160. */
  161. void ff_ebur128_add_frames_planar_short(FFEBUR128State * st,
  162. const short **srcs,
  163. size_t frames, int stride);
  164. /** \brief See \ref ebur128_add_frames_planar_short */
  165. void ff_ebur128_add_frames_planar_int(FFEBUR128State * st,
  166. const int **srcs,
  167. size_t frames, int stride);
  168. /** \brief See \ref ebur128_add_frames_planar_short */
  169. void ff_ebur128_add_frames_planar_float(FFEBUR128State * st,
  170. const float **srcs,
  171. size_t frames, int stride);
  172. /** \brief See \ref ebur128_add_frames_planar_short */
  173. void ff_ebur128_add_frames_planar_double(FFEBUR128State * st,
  174. const double **srcs,
  175. size_t frames, int stride);
  176. /** \brief Get global integrated loudness in LUFS.
  177. *
  178. * @param st library state.
  179. * @param out integrated loudness in LUFS. -HUGE_VAL if result is negative
  180. * infinity.
  181. * @return
  182. * - 0 on success.
  183. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_I" has not been set.
  184. */
  185. int ff_ebur128_loudness_global(FFEBUR128State * st, double *out);
  186. /** \brief Get global integrated loudness in LUFS across multiple instances.
  187. *
  188. * @param sts array of library states.
  189. * @param size length of sts
  190. * @param out integrated loudness in LUFS. -HUGE_VAL if result is negative
  191. * infinity.
  192. * @return
  193. * - 0 on success.
  194. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_I" has not been set.
  195. */
  196. int ff_ebur128_loudness_global_multiple(FFEBUR128State ** sts,
  197. size_t size, double *out);
  198. /** \brief Get momentary loudness (last 400ms) in LUFS.
  199. *
  200. * @param st library state.
  201. * @param out momentary loudness in LUFS. -HUGE_VAL if result is negative
  202. * infinity.
  203. * @return
  204. * - 0 on success.
  205. */
  206. int ff_ebur128_loudness_momentary(FFEBUR128State * st, double *out);
  207. /** \brief Get short-term loudness (last 3s) in LUFS.
  208. *
  209. * @param st library state.
  210. * @param out short-term loudness in LUFS. -HUGE_VAL if result is negative
  211. * infinity.
  212. * @return
  213. * - 0 on success.
  214. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_S" has not been set.
  215. */
  216. int ff_ebur128_loudness_shortterm(FFEBUR128State * st, double *out);
  217. /** \brief Get loudness of the specified window in LUFS.
  218. *
  219. * window must not be larger than the current window set in st.
  220. *
  221. * @param st library state.
  222. * @param window window in ms to calculate loudness.
  223. * @param out loudness in LUFS. -HUGE_VAL if result is negative infinity.
  224. * @return
  225. * - 0 on success.
  226. * - AVERROR(EINVAL) if window larger than current window in st.
  227. */
  228. int ff_ebur128_loudness_window(FFEBUR128State * st,
  229. unsigned long window, double *out);
  230. /** \brief Get loudness range (LRA) of programme in LU.
  231. *
  232. * Calculates loudness range according to EBU 3342.
  233. *
  234. * @param st library state.
  235. * @param out loudness range (LRA) in LU. Will not be changed in case of
  236. * error. AVERROR(EINVAL) will be returned in this case.
  237. * @return
  238. * - 0 on success.
  239. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_LRA" has not been set.
  240. */
  241. int ff_ebur128_loudness_range(FFEBUR128State * st, double *out);
  242. /** \brief Get loudness range (LRA) in LU across multiple instances.
  243. *
  244. * Calculates loudness range according to EBU 3342.
  245. *
  246. * @param sts array of library states.
  247. * @param size length of sts
  248. * @param out loudness range (LRA) in LU. Will not be changed in case of
  249. * error. AVERROR(EINVAL) will be returned in this case.
  250. * @return
  251. * - 0 on success.
  252. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_LRA" has not been set.
  253. */
  254. int ff_ebur128_loudness_range_multiple(FFEBUR128State ** sts,
  255. size_t size, double *out);
  256. /** \brief Get maximum sample peak of selected channel in float format.
  257. *
  258. * @param st library state
  259. * @param channel_number channel to analyse
  260. * @param out maximum sample peak in float format (1.0 is 0 dBFS)
  261. * @return
  262. * - 0 on success.
  263. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_SAMPLE_PEAK" has not been set.
  264. * - AVERROR(EINVAL) if invalid channel index.
  265. */
  266. int ff_ebur128_sample_peak(FFEBUR128State * st,
  267. unsigned int channel_number, double *out);
  268. /** \brief Get relative threshold in LUFS.
  269. *
  270. * @param st library state
  271. * @param out relative threshold in LUFS.
  272. * @return
  273. * - 0 on success.
  274. * - AVERROR(EINVAL) if mode "FF_EBUR128_MODE_I" has not been set.
  275. */
  276. int ff_ebur128_relative_threshold(FFEBUR128State * st, double *out);
  277. #endif /* AVFILTER_EBUR128_H */