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.

215 lines
9.9KB

  1. /*
  2. * AC-3 encoder & E-AC-3 encoder common header
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * AC-3 encoder & E-AC-3 encoder common header
  25. */
  26. #ifndef AVCODEC_AC3ENC_H
  27. #define AVCODEC_AC3ENC_H
  28. #include <stdint.h>
  29. #include "ac3.h"
  30. #include "ac3dsp.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "put_bits.h"
  34. #include "fft.h"
  35. #ifndef CONFIG_AC3ENC_FLOAT
  36. #define CONFIG_AC3ENC_FLOAT 0
  37. #endif
  38. #if CONFIG_AC3ENC_FLOAT
  39. #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
  40. typedef float SampleType;
  41. typedef float CoefType;
  42. typedef float CoefSumType;
  43. #else
  44. #define MAC_COEF(d,a,b) MAC64(d,a,b)
  45. typedef int16_t SampleType;
  46. typedef int32_t CoefType;
  47. typedef int64_t CoefSumType;
  48. #endif
  49. typedef struct AC3MDCTContext {
  50. const SampleType *window; ///< MDCT window function
  51. FFTContext fft; ///< FFT context for MDCT calculation
  52. } AC3MDCTContext;
  53. /**
  54. * Encoding Options used by AVOption.
  55. */
  56. typedef struct AC3EncOptions {
  57. /* AC-3 metadata options*/
  58. int dialogue_level;
  59. int bitstream_mode;
  60. float center_mix_level;
  61. float surround_mix_level;
  62. int dolby_surround_mode;
  63. int audio_production_info;
  64. int mixing_level;
  65. int room_type;
  66. int copyright;
  67. int original;
  68. int extended_bsi_1;
  69. int preferred_stereo_downmix;
  70. float ltrt_center_mix_level;
  71. float ltrt_surround_mix_level;
  72. float loro_center_mix_level;
  73. float loro_surround_mix_level;
  74. int extended_bsi_2;
  75. int dolby_surround_ex_mode;
  76. int dolby_headphone_mode;
  77. int ad_converter_type;
  78. /* other encoding options */
  79. int allow_per_frame_metadata;
  80. int stereo_rematrixing;
  81. int channel_coupling;
  82. int cpl_start;
  83. } AC3EncOptions;
  84. /**
  85. * Data for a single audio block.
  86. */
  87. typedef struct AC3Block {
  88. CoefType **mdct_coef; ///< MDCT coefficients
  89. int32_t **fixed_coef; ///< fixed-point MDCT coefficients
  90. uint8_t **exp; ///< original exponents
  91. uint8_t **grouped_exp; ///< grouped exponents
  92. int16_t **psd; ///< psd per frequency bin
  93. int16_t **band_psd; ///< psd per critical band
  94. int16_t **mask; ///< masking curve
  95. uint16_t **qmant; ///< quantized mantissas
  96. uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
  97. uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
  98. uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
  99. uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
  100. int num_rematrixing_bands; ///< number of rematrixing bands
  101. uint8_t rematrixing_flags[4]; ///< rematrixing flags
  102. int new_cpl_strategy; ///< send new coupling strategy
  103. int cpl_in_use; ///< coupling in use for this block (cplinu)
  104. uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
  105. int num_cpl_channels; ///< number of channels in coupling
  106. uint8_t new_cpl_coords; ///< send new coupling coordinates (cplcoe)
  107. uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
  108. int new_snr_offsets; ///< send new SNR offsets
  109. int new_cpl_leak; ///< send new coupling leak info
  110. int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
  111. } AC3Block;
  112. /**
  113. * AC-3 encoder private context.
  114. */
  115. typedef struct AC3EncodeContext {
  116. AVClass *av_class; ///< AVClass used for AVOption
  117. AC3EncOptions options; ///< encoding options
  118. PutBitContext pb; ///< bitstream writer context
  119. DSPContext dsp;
  120. AC3DSPContext ac3dsp; ///< AC-3 optimized functions
  121. AC3MDCTContext mdct; ///< MDCT context
  122. AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
  123. int eac3; ///< indicates if this is E-AC-3 vs. AC-3
  124. int bitstream_id; ///< bitstream id (bsid)
  125. int bitstream_mode; ///< bitstream mode (bsmod)
  126. int bit_rate; ///< target bit rate, in bits-per-second
  127. int sample_rate; ///< sampling frequency, in Hz
  128. int frame_size_min; ///< minimum frame size in case rounding is necessary
  129. int frame_size; ///< current frame size in bytes
  130. int frame_size_code; ///< frame size code (frmsizecod)
  131. uint16_t crc_inv[2];
  132. int64_t bits_written; ///< bit count (used to avg. bitrate)
  133. int64_t samples_written; ///< sample count (used to avg. bitrate)
  134. int fbw_channels; ///< number of full-bandwidth channels (nfchans)
  135. int channels; ///< total number of channels (nchans)
  136. int lfe_on; ///< indicates if there is an LFE channel (lfeon)
  137. int lfe_channel; ///< channel index of the LFE channel
  138. int has_center; ///< indicates if there is a center channel
  139. int has_surround; ///< indicates if there are one or more surround channels
  140. int channel_mode; ///< channel mode (acmod)
  141. const uint8_t *channel_map; ///< channel map used to reorder channels
  142. int center_mix_level; ///< center mix level code
  143. int surround_mix_level; ///< surround mix level code
  144. int ltrt_center_mix_level; ///< Lt/Rt center mix level code
  145. int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
  146. int loro_center_mix_level; ///< Lo/Ro center mix level code
  147. int loro_surround_mix_level; ///< Lo/Ro surround mix level code
  148. int cutoff; ///< user-specified cutoff frequency, in Hz
  149. int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
  150. int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
  151. int cpl_end_freq; ///< coupling channel end frequency bin
  152. int cpl_on; ///< coupling turned on for this frame
  153. int cpl_enabled; ///< coupling enabled for all frames
  154. int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
  155. int num_cpl_bands; ///< number of coupling bands (ncplbnd)
  156. uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
  157. int rematrixing_enabled; ///< stereo rematrixing enabled
  158. /* bitrate allocation control */
  159. int slow_gain_code; ///< slow gain code (sgaincod)
  160. int slow_decay_code; ///< slow decay code (sdcycod)
  161. int fast_decay_code; ///< fast decay code (fdcycod)
  162. int db_per_bit_code; ///< dB/bit code (dbpbcod)
  163. int floor_code; ///< floor code (floorcod)
  164. AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
  165. int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
  166. int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
  167. int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
  168. int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
  169. int frame_bits; ///< all frame bits except exponents and mantissas
  170. int exponent_bits; ///< number of bits used for exponents
  171. SampleType **planar_samples;
  172. uint8_t *bap_buffer;
  173. uint8_t *bap1_buffer;
  174. CoefType *mdct_coef_buffer;
  175. int32_t *fixed_coef_buffer;
  176. uint8_t *exp_buffer;
  177. uint8_t *grouped_exp_buffer;
  178. int16_t *psd_buffer;
  179. int16_t *band_psd_buffer;
  180. int16_t *mask_buffer;
  181. uint16_t *qmant_buffer;
  182. uint8_t *cpl_coord_exp_buffer;
  183. uint8_t *cpl_coord_mant_buffer;
  184. uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
  185. uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
  186. uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
  187. int ref_bap_set; ///< indicates if ref_bap pointers have been set
  188. DECLARE_ALIGNED(32, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
  189. } AC3EncodeContext;
  190. #endif /* AVCODEC_AC3ENC_H */