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.

156 lines
5.5KB

  1. /*
  2. * Common code between AC3 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 ac3.h
  23. * Common code between AC3 encoder and decoder.
  24. */
  25. #ifndef AC3_H
  26. #define AC3_H
  27. #include "ac3tab.h"
  28. #define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
  29. #define AC3_MAX_CHANNELS 6 /* including LFE channel */
  30. #define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
  31. #define AC3_FRAME_SIZE (NB_BLOCKS * 256)
  32. /* exponent encoding strategy */
  33. #define EXP_REUSE 0
  34. #define EXP_NEW 1
  35. #define EXP_D15 1
  36. #define EXP_D25 2
  37. #define EXP_D45 3
  38. typedef struct AC3BitAllocParameters {
  39. int fscod; /* frequency */
  40. int halfratecod;
  41. int sgain, sdecay, fdecay, dbknee, floor;
  42. int cplfleak, cplsleak;
  43. } AC3BitAllocParameters;
  44. /**
  45. * @struct AC3HeaderInfo
  46. * Coded AC-3 header values up to the lfeon element, plus derived values.
  47. */
  48. typedef struct {
  49. /** @defgroup coded Coded elements
  50. * @{
  51. */
  52. uint16_t sync_word;
  53. uint16_t crc1;
  54. uint8_t fscod;
  55. uint8_t frmsizecod;
  56. uint8_t bsid;
  57. uint8_t bsmod;
  58. uint8_t acmod;
  59. uint8_t cmixlev;
  60. uint8_t surmixlev;
  61. uint8_t dsurmod;
  62. uint8_t lfeon;
  63. /** @} */
  64. /** @defgroup derived Derived values
  65. * @{
  66. */
  67. uint8_t halfratecod;
  68. uint16_t sample_rate;
  69. uint32_t bit_rate;
  70. uint8_t channels;
  71. uint16_t frame_size;
  72. /** @} */
  73. } AC3HeaderInfo;
  74. void ac3_common_init(void);
  75. /**
  76. * Calculates the log power-spectral density of the input signal.
  77. * This gives a rough estimate of signal power in the frequency domain by using
  78. * the spectral envelope (exponents). The psd is also separately grouped
  79. * into critical bands for use in the calculating the masking curve.
  80. * 128 units in psd = -6 dB. The dbknee parameter in AC3BitAllocParameters
  81. * determines the reference level.
  82. *
  83. * @param[in] exp frequency coefficient exponents
  84. * @param[in] start starting bin location
  85. * @param[in] end ending bin location
  86. * @param[out] psd signal power for each frequency bin
  87. * @param[out] bndpsd signal power for each critical band
  88. */
  89. void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
  90. int16_t *bndpsd);
  91. /**
  92. * Calculates the masking curve.
  93. * First, the excitation is calculated using parameters in \p s and the signal
  94. * power in each critical band. The excitation is compared with a predefined
  95. * hearing threshold table to produce the masking curve. If delta bit
  96. * allocation information is provided, it is used for adjusting the masking
  97. * curve, usually to give a closer match to a better psychoacoustic model.
  98. *
  99. * @param[in] s adjustable bit allocation parameters
  100. * @param[in] bndpsd signal power for each critical band
  101. * @param[in] start starting bin location
  102. * @param[in] end ending bin location
  103. * @param[in] fgain fast gain (estimated signal-to-mask ratio)
  104. * @param[in] is_lfe whether or not the channel being processed is the LFE
  105. * @param[in] deltbae delta bit allocation exists (none, reuse, or new)
  106. * @param[in] deltnseg number of delta segments
  107. * @param[in] deltoffst location offsets for each segment
  108. * @param[in] deltlen length of each segment
  109. * @param[in] deltba delta bit allocation for each segment
  110. * @param[out] mask calculated masking curve
  111. */
  112. void ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *bndpsd,
  113. int start, int end, int fgain, int is_lfe,
  114. int deltbae, int deltnseg, uint8_t *deltoffst,
  115. uint8_t *deltlen, uint8_t *deltba,
  116. int16_t *mask);
  117. /**
  118. * Calculates bit allocation pointers.
  119. * The SNR is the difference between the masking curve and the signal. AC-3
  120. * uses this value for each frequency bin to allocate bits. The \p snroffset
  121. * parameter is a global adjustment to the SNR for all bins.
  122. *
  123. * @param[in] mask masking curve
  124. * @param[in] psd signal power for each frequency bin
  125. * @param[in] start starting bin location
  126. * @param[in] end ending bin location
  127. * @param[in] snroffset SNR adjustment
  128. * @param[in] floor noise floor
  129. * @param[out] bap bit allocation pointers
  130. */
  131. void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
  132. int snroffset, int floor, uint8_t *bap);
  133. void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
  134. int8_t *exp, int start, int end,
  135. int snroffset, int fgain, int is_lfe,
  136. int deltbae,int deltnseg,
  137. uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba);
  138. #endif /* AC3_H */