ac3.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Common code between the AC-3 encoder and decoder
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Common code between the AC-3 encoder and decoder.
  24. */
  25. #ifndef AVCODEC_AC3_H
  26. #define AVCODEC_AC3_H
  27. #define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
  28. #define EAC3_MAX_CHANNELS 16 /**< maximum number of channels in EAC3 */
  29. #define AC3_MAX_CHANNELS 7 /**< maximum number of channels, including coupling channel */
  30. #define CPL_CH 0 /**< coupling channel index */
  31. #define AC3_MAX_COEFS 256
  32. #define AC3_BLOCK_SIZE 256
  33. #define AC3_MAX_BLOCKS 6
  34. #define AC3_FRAME_SIZE (AC3_MAX_BLOCKS * 256)
  35. #define AC3_WINDOW_SIZE (AC3_BLOCK_SIZE * 2)
  36. #define AC3_CRITICAL_BANDS 50
  37. #define AC3_MAX_CPL_BANDS 18
  38. #include "libavutil/opt.h"
  39. #include "avcodec.h"
  40. #include "ac3tab.h"
  41. /* exponent encoding strategy */
  42. #define EXP_REUSE 0
  43. #define EXP_NEW 1
  44. #define EXP_D15 1
  45. #define EXP_D25 2
  46. #define EXP_D45 3
  47. #ifndef USE_FIXED
  48. #define USE_FIXED 0
  49. #endif
  50. #if USE_FIXED
  51. #define FFT_FLOAT 0
  52. #define FIXR(a) ((int)((a) * 0 + 0.5))
  53. #define FIXR12(a) ((int)((a) * 4096 + 0.5))
  54. #define FIXR15(a) ((int)((a) * 32768 + 0.5))
  55. #define ROUND15(x) ((x) + 16384) >> 15
  56. #define AC3_RENAME(x) x ## _fixed
  57. #define AC3_NORM(norm) (1<<24)/(norm)
  58. #define AC3_MUL(a,b) ((((int64_t) (a)) * (b))>>12)
  59. #define AC3_RANGE(x) ((x)|(((x)&128)<<1))
  60. #define AC3_HEAVY_RANGE(x) ((x)<<1)
  61. #define AC3_DYNAMIC_RANGE(x) (x)
  62. #define AC3_SPX_BLEND(x) (x)
  63. #define AC3_DYNAMIC_RANGE1 0
  64. typedef int INTFLOAT;
  65. typedef int16_t SHORTFLOAT;
  66. #else /* USE_FIXED */
  67. #define FIXR(x) ((float)(x))
  68. #define FIXR12(x) ((float)(x))
  69. #define FIXR15(x) ((float)(x))
  70. #define ROUND15(x) (x)
  71. #define AC3_RENAME(x) x
  72. #define AC3_NORM(norm) (1.0f/(norm))
  73. #define AC3_MUL(a,b) ((a) * (b))
  74. #define AC3_RANGE(x) (dynamic_range_tab[(x)])
  75. #define AC3_HEAVY_RANGE(x) (ff_ac3_heavy_dynamic_range_tab[(x)])
  76. #define AC3_DYNAMIC_RANGE(x) (powf(x, s->drc_scale))
  77. #define AC3_SPX_BLEND(x) (x)* (1.0f/32)
  78. #define AC3_DYNAMIC_RANGE1 1.0f
  79. typedef float INTFLOAT;
  80. typedef float SHORTFLOAT;
  81. #endif /* USE_FIXED */
  82. #define AC3_LEVEL(x) ROUND15((x) * FIXR15(M_SQRT1_2))
  83. /* pre-defined gain values */
  84. #define LEVEL_PLUS_3DB M_SQRT2
  85. #define LEVEL_PLUS_1POINT5DB 1.1892071150027209
  86. #define LEVEL_MINUS_1POINT5DB 0.8408964152537145
  87. #define LEVEL_MINUS_3DB M_SQRT1_2
  88. #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
  89. #define LEVEL_MINUS_6DB 0.5000000000000000
  90. #define LEVEL_MINUS_9DB 0.3535533905932738
  91. #define LEVEL_ZERO 0.0000000000000000
  92. #define LEVEL_ONE 1.0000000000000000
  93. /** Delta bit allocation strategy */
  94. typedef enum {
  95. DBA_REUSE = 0,
  96. DBA_NEW,
  97. DBA_NONE,
  98. DBA_RESERVED
  99. } AC3DeltaStrategy;
  100. /** Channel mode (audio coding mode) */
  101. typedef enum {
  102. AC3_CHMODE_DUALMONO = 0,
  103. AC3_CHMODE_MONO,
  104. AC3_CHMODE_STEREO,
  105. AC3_CHMODE_3F,
  106. AC3_CHMODE_2F1R,
  107. AC3_CHMODE_3F1R,
  108. AC3_CHMODE_2F2R,
  109. AC3_CHMODE_3F2R
  110. } AC3ChannelMode;
  111. /** Dolby Surround mode */
  112. typedef enum AC3DolbySurroundMode {
  113. AC3_DSURMOD_NOTINDICATED = 0,
  114. AC3_DSURMOD_OFF,
  115. AC3_DSURMOD_ON,
  116. AC3_DSURMOD_RESERVED
  117. } AC3DolbySurroundMode;
  118. /** Dolby Surround EX mode */
  119. typedef enum AC3DolbySurroundEXMode {
  120. AC3_DSUREXMOD_NOTINDICATED = 0,
  121. AC3_DSUREXMOD_OFF,
  122. AC3_DSUREXMOD_ON,
  123. AC3_DSUREXMOD_PLIIZ
  124. } AC3DolbySurroundEXMode;
  125. /** Dolby Headphone mode */
  126. typedef enum AC3DolbyHeadphoneMode {
  127. AC3_DHEADPHONMOD_NOTINDICATED = 0,
  128. AC3_DHEADPHONMOD_OFF,
  129. AC3_DHEADPHONMOD_ON,
  130. AC3_DHEADPHONMOD_RESERVED
  131. } AC3DolbyHeadphoneMode;
  132. /** Preferred Stereo Downmix mode */
  133. typedef enum AC3PreferredStereoDownmixMode {
  134. AC3_DMIXMOD_NOTINDICATED = 0,
  135. AC3_DMIXMOD_LTRT,
  136. AC3_DMIXMOD_LORO,
  137. AC3_DMIXMOD_DPLII // reserved value in A/52, but used by encoders to indicate DPL2
  138. } AC3PreferredStereoDownmixMode;
  139. typedef struct AC3BitAllocParameters {
  140. int sr_code;
  141. int sr_shift;
  142. int slow_gain, slow_decay, fast_decay, db_per_bit, floor;
  143. int cpl_fast_leak, cpl_slow_leak;
  144. } AC3BitAllocParameters;
  145. /**
  146. * @struct AC3HeaderInfo
  147. * Coded AC-3 header values up to the lfeon element, plus derived values.
  148. */
  149. typedef struct AC3HeaderInfo {
  150. /** @name Coded elements
  151. * @{
  152. */
  153. uint16_t sync_word;
  154. uint16_t crc1;
  155. uint8_t sr_code;
  156. uint8_t bitstream_id;
  157. uint8_t bitstream_mode;
  158. uint8_t channel_mode;
  159. uint8_t lfe_on;
  160. uint8_t frame_type;
  161. int substreamid; ///< substream identification
  162. int center_mix_level; ///< Center mix level index
  163. int surround_mix_level; ///< Surround mix level index
  164. uint16_t channel_map;
  165. int num_blocks; ///< number of audio blocks
  166. int dolby_surround_mode;
  167. /** @} */
  168. /** @name Derived values
  169. * @{
  170. */
  171. uint8_t sr_shift;
  172. uint16_t sample_rate;
  173. uint32_t bit_rate;
  174. uint8_t channels;
  175. uint16_t frame_size;
  176. uint64_t channel_layout;
  177. /** @} */
  178. } AC3HeaderInfo;
  179. typedef enum {
  180. EAC3_FRAME_TYPE_INDEPENDENT = 0,
  181. EAC3_FRAME_TYPE_DEPENDENT,
  182. EAC3_FRAME_TYPE_AC3_CONVERT,
  183. EAC3_FRAME_TYPE_RESERVED
  184. } EAC3FrameType;
  185. void ff_ac3_common_init(void);
  186. /**
  187. * Calculate the log power-spectral density of the input signal.
  188. * This gives a rough estimate of signal power in the frequency domain by using
  189. * the spectral envelope (exponents). The psd is also separately grouped
  190. * into critical bands for use in the calculating the masking curve.
  191. * 128 units in psd = -6 dB. The dbknee parameter in AC3BitAllocParameters
  192. * determines the reference level.
  193. *
  194. * @param[in] exp frequency coefficient exponents
  195. * @param[in] start starting bin location
  196. * @param[in] end ending bin location
  197. * @param[out] psd signal power for each frequency bin
  198. * @param[out] band_psd signal power for each critical band
  199. */
  200. void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
  201. int16_t *band_psd);
  202. /**
  203. * Calculate the masking curve.
  204. * First, the excitation is calculated using parameters in s and the signal
  205. * power in each critical band. The excitation is compared with a predefined
  206. * hearing threshold table to produce the masking curve. If delta bit
  207. * allocation information is provided, it is used for adjusting the masking
  208. * curve, usually to give a closer match to a better psychoacoustic model.
  209. *
  210. * @param[in] s adjustable bit allocation parameters
  211. * @param[in] band_psd signal power for each critical band
  212. * @param[in] start starting bin location
  213. * @param[in] end ending bin location
  214. * @param[in] fast_gain fast gain (estimated signal-to-mask ratio)
  215. * @param[in] is_lfe whether or not the channel being processed is the LFE
  216. * @param[in] dba_mode delta bit allocation mode (none, reuse, or new)
  217. * @param[in] dba_nsegs number of delta segments
  218. * @param[in] dba_offsets location offsets for each segment
  219. * @param[in] dba_lengths length of each segment
  220. * @param[in] dba_values delta bit allocation for each segment
  221. * @param[out] mask calculated masking curve
  222. * @return returns 0 for success, non-zero for error
  223. */
  224. int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
  225. int start, int end, int fast_gain, int is_lfe,
  226. int dba_mode, int dba_nsegs, uint8_t *dba_offsets,
  227. uint8_t *dba_lengths, uint8_t *dba_values,
  228. int16_t *mask);
  229. #endif /* AVCODEC_AC3_H */