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. #if 0
  54. /**
  55. * Encoding Options used by AVOption.
  56. */
  57. typedef struct AC3EncOptions {
  58. /* AC-3 metadata options*/
  59. int dialogue_level;
  60. int bitstream_mode;
  61. float center_mix_level;
  62. float surround_mix_level;
  63. int dolby_surround_mode;
  64. int audio_production_info;
  65. int mixing_level;
  66. int room_type;
  67. int copyright;
  68. int original;
  69. int extended_bsi_1;
  70. int preferred_stereo_downmix;
  71. float ltrt_center_mix_level;
  72. float ltrt_surround_mix_level;
  73. float loro_center_mix_level;
  74. float loro_surround_mix_level;
  75. int extended_bsi_2;
  76. int dolby_surround_ex_mode;
  77. int dolby_headphone_mode;
  78. int ad_converter_type;
  79. /* other encoding options */
  80. int allow_per_frame_metadata;
  81. int stereo_rematrixing;
  82. int channel_coupling;
  83. int cpl_start;
  84. } AC3EncOptions;
  85. #endif
  86. /**
  87. * Data for a single audio block.
  88. */
  89. typedef struct AC3Block {
  90. CoefType **mdct_coef; ///< MDCT coefficients
  91. int32_t **fixed_coef; ///< fixed-point MDCT coefficients
  92. uint8_t **exp; ///< original exponents
  93. uint8_t **grouped_exp; ///< grouped exponents
  94. int16_t **psd; ///< psd per frequency bin
  95. int16_t **band_psd; ///< psd per critical band
  96. int16_t **mask; ///< masking curve
  97. uint16_t **qmant; ///< quantized mantissas
  98. uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
  99. uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
  100. uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
  101. uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
  102. int num_rematrixing_bands; ///< number of rematrixing bands
  103. uint8_t rematrixing_flags[4]; ///< rematrixing flags
  104. int new_cpl_strategy; ///< send new coupling strategy
  105. int cpl_in_use; ///< coupling in use for this block (cplinu)
  106. uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
  107. int num_cpl_channels; ///< number of channels in coupling
  108. uint8_t new_cpl_coords; ///< send new coupling coordinates (cplcoe)
  109. uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
  110. int new_snr_offsets; ///< send new SNR offsets
  111. int new_cpl_leak; ///< send new coupling leak info
  112. int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
  113. } AC3Block;
  114. /**
  115. * AC-3 encoder private context.
  116. */
  117. typedef struct AC3EncodeContext {
  118. AVClass *av_class; ///< AVClass used for AVOption
  119. AC3EncOptions options; ///< encoding options
  120. PutBitContext pb; ///< bitstream writer context
  121. DSPContext dsp;
  122. AC3DSPContext ac3dsp; ///< AC-3 optimized functions
  123. AC3MDCTContext mdct; ///< MDCT context
  124. AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
  125. int eac3; ///< indicates if this is E-AC-3 vs. AC-3
  126. int bitstream_id; ///< bitstream id (bsid)
  127. int bitstream_mode; ///< bitstream mode (bsmod)
  128. int bit_rate; ///< target bit rate, in bits-per-second
  129. int sample_rate; ///< sampling frequency, in Hz
  130. int frame_size_min; ///< minimum frame size in case rounding is necessary
  131. int frame_size; ///< current frame size in bytes
  132. int frame_size_code; ///< frame size code (frmsizecod)
  133. uint16_t crc_inv[2];
  134. int64_t bits_written; ///< bit count (used to avg. bitrate)
  135. int64_t samples_written; ///< sample count (used to avg. bitrate)
  136. int fbw_channels; ///< number of full-bandwidth channels (nfchans)
  137. int channels; ///< total number of channels (nchans)
  138. int lfe_on; ///< indicates if there is an LFE channel (lfeon)
  139. int lfe_channel; ///< channel index of the LFE channel
  140. int has_center; ///< indicates if there is a center channel
  141. int has_surround; ///< indicates if there are one or more surround channels
  142. int channel_mode; ///< channel mode (acmod)
  143. const uint8_t *channel_map; ///< channel map used to reorder channels
  144. int center_mix_level; ///< center mix level code
  145. int surround_mix_level; ///< surround mix level code
  146. int ltrt_center_mix_level; ///< Lt/Rt center mix level code
  147. int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
  148. int loro_center_mix_level; ///< Lo/Ro center mix level code
  149. int loro_surround_mix_level; ///< Lo/Ro surround mix level code
  150. int cutoff; ///< user-specified cutoff frequency, in Hz
  151. int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
  152. int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
  153. int cpl_end_freq; ///< coupling channel end frequency bin
  154. int cpl_on; ///< coupling turned on for this frame
  155. int cpl_enabled; ///< coupling enabled for all frames
  156. int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
  157. int num_cpl_bands; ///< number of coupling bands (ncplbnd)
  158. uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
  159. int rematrixing_enabled; ///< stereo rematrixing enabled
  160. /* bitrate allocation control */
  161. int slow_gain_code; ///< slow gain code (sgaincod)
  162. int slow_decay_code; ///< slow decay code (sdcycod)
  163. int fast_decay_code; ///< fast decay code (fdcycod)
  164. int db_per_bit_code; ///< dB/bit code (dbpbcod)
  165. int floor_code; ///< floor code (floorcod)
  166. AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
  167. int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
  168. int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
  169. int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
  170. int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
  171. int frame_bits; ///< all frame bits except exponents and mantissas
  172. int exponent_bits; ///< number of bits used for exponents
  173. SampleType **planar_samples;
  174. uint8_t *bap_buffer;
  175. uint8_t *bap1_buffer;
  176. CoefType *mdct_coef_buffer;
  177. int32_t *fixed_coef_buffer;
  178. uint8_t *exp_buffer;
  179. uint8_t *grouped_exp_buffer;
  180. int16_t *psd_buffer;
  181. int16_t *band_psd_buffer;
  182. int16_t *mask_buffer;
  183. uint16_t *qmant_buffer;
  184. uint8_t *cpl_coord_exp_buffer;
  185. uint8_t *cpl_coord_mant_buffer;
  186. uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
  187. uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
  188. uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
  189. int ref_bap_set; ///< indicates if ref_bap pointers have been set
  190. DECLARE_ALIGNED(32, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
  191. } AC3EncodeContext;
  192. #endif /* AVCODEC_AC3ENC_H */