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.

210 lines
11KB

  1. /*
  2. * Common code between the AC-3 and E-AC-3 decoders
  3. * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
  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 the AC-3 and E-AC-3 decoders.
  24. */
  25. #ifndef AVCODEC_AC3DEC_H
  26. #define AVCODEC_AC3DEC_H
  27. #include "libavutil/lfg.h"
  28. #include "ac3.h"
  29. #include "bitstream.h"
  30. #include "dsputil.h"
  31. /* override ac3.h to include coupling channel */
  32. #undef AC3_MAX_CHANNELS
  33. #define AC3_MAX_CHANNELS 7
  34. #define CPL_CH 0
  35. #define AC3_OUTPUT_LFEON 8
  36. #define AC3_MAX_COEFS 256
  37. #define AC3_BLOCK_SIZE 256
  38. #define MAX_BLOCKS 6
  39. #define SPX_MAX_BANDS 17
  40. #define INT24_MIN -8388608
  41. #define INT24_MAX 8388607
  42. #define M_SQRT_POW2_15 181
  43. typedef struct {
  44. AVCodecContext *avctx; ///< parent context
  45. GetBitContext gbc; ///< bitstream reader
  46. uint8_t *input_buffer; ///< temp buffer to prevent overread
  47. ///@defgroup bsi bit stream information
  48. ///@{
  49. int frame_type; ///< frame type (strmtyp)
  50. int substreamid; ///< substream identification
  51. int frame_size; ///< current frame size, in bytes
  52. int bit_rate; ///< stream bit rate, in bits-per-second
  53. int sample_rate; ///< sample frequency, in Hz
  54. int num_blocks; ///< number of audio blocks
  55. int channel_mode; ///< channel mode (acmod)
  56. int lfe_on; ///< lfe channel in use
  57. int channel_map; ///< custom channel map
  58. int center_mix_level; ///< Center mix level index
  59. int surround_mix_level; ///< Surround mix level index
  60. int eac3; ///< indicates if current frame is E-AC-3
  61. ///@}
  62. ///@defgroup audfrm frame syntax parameters
  63. int snr_offset_strategy; ///< SNR offset strategy (snroffststr)
  64. int block_switch_syntax; ///< block switch syntax enabled (blkswe)
  65. int dither_flag_syntax; ///< dither flag syntax enabled (dithflage)
  66. int bit_allocation_syntax; ///< bit allocation model syntax enabled (bamode)
  67. int fast_gain_syntax; ///< fast gain codes enabled (frmfgaincode)
  68. int dba_syntax; ///< delta bit allocation syntax enabled (dbaflde)
  69. int skip_syntax; ///< skip field syntax enabled (skipflde)
  70. ///@}
  71. ///@defgroup cpl standard coupling
  72. int cpl_in_use[MAX_BLOCKS]; ///< coupling in use (cplinu)
  73. int cpl_strategy_exists[MAX_BLOCKS]; ///< coupling strategy exists (cplstre)
  74. int channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
  75. int phase_flags_in_use; ///< phase flags in use (phsflginu)
  76. int phase_flags[18]; ///< phase flags (phsflg)
  77. int num_cpl_subbands; ///< number of coupling sub bands (ncplsubnd)
  78. int num_cpl_bands; ///< number of coupling bands (ncplbnd)
  79. uint8_t cpl_band_struct[18]; ///< coupling band structure (cplbndstrc)
  80. int firstchincpl; ///< first channel in coupling
  81. int first_cpl_coords[AC3_MAX_CHANNELS]; ///< first coupling coordinates states (firstcplcos)
  82. int cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates (cplco)
  83. ///@}
  84. ///@defgroup spx spectral extension
  85. ///@{
  86. int spx_in_use[MAX_BLOCKS]; ///< spectral extension in use (spxinu)
  87. int channel_in_spx[AC3_MAX_CHANNELS]; ///< channel in spectral extension (chinspx)
  88. int spx_atten_code[AC3_MAX_CHANNELS]; ///< spx attenuation code (spxattencod)
  89. int spx_coords_exist[AC3_MAX_CHANNELS]; ///< indicates if a channel has spx coords (spxcoe)
  90. int spx_start_subband; ///< spx beginning frequency band (spxbegf)
  91. int spx_start_freq; ///< spx start frequency bin
  92. int spx_end_freq; ///< spx end frequency bin
  93. int spx_copy_start_freq; ///< spx starting frequency for copying (copystartmant)
  94. int num_spx_subbands; ///< number of spectral extension subbands
  95. int num_spx_bands; ///< number of spectral extension bands (nspxbnds)
  96. uint8_t spx_band_struct[SPX_MAX_BANDS]; ///< spectral extension band structure (spxbndstrc)
  97. int spx_band_sizes[SPX_MAX_BANDS]; ///< number of bins in each band (spxbndsztab)
  98. int first_spx_coords[AC3_MAX_CHANNELS]; ///< first spx coordinates states (firstspxcos)
  99. int spx_noise_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS]; ///< spx noise blending factor (nblendfact)
  100. int spx_signal_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS]; ///< spx signal blending factor (sblendfact)
  101. int spx_coords[AC3_MAX_CHANNELS][SPX_MAX_BANDS]; ///< spectral extension coordinates (spxco)
  102. ///@}
  103. ///@defgroup aht adaptive hybrid transform
  104. int channel_uses_aht[AC3_MAX_CHANNELS]; ///< channel AHT in use (chahtinu)
  105. int pre_mantissa[AC3_MAX_CHANNELS][AC3_MAX_COEFS][MAX_BLOCKS]; ///< pre-IDCT mantissas
  106. ///@}
  107. ///@defgroup channel channel
  108. int fbw_channels; ///< number of full-bandwidth channels
  109. int channels; ///< number of total channels
  110. int lfe_ch; ///< index of LFE channel
  111. float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients
  112. int downmixed; ///< indicates if coeffs are currently downmixed
  113. int output_mode; ///< output channel configuration
  114. int out_channels; ///< number of output channels
  115. ///@}
  116. ///@defgroup dynrng dynamic range
  117. float dynamic_range[2]; ///< dynamic range
  118. ///@}
  119. ///@defgroup bandwidth bandwidth
  120. int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
  121. int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
  122. ///@}
  123. ///@defgroup rematrixing rematrixing
  124. int num_rematrixing_bands; ///< number of rematrixing bands (nrematbnd)
  125. int rematrixing_flags[4]; ///< rematrixing flags (rematflg)
  126. ///@}
  127. ///@defgroup exponents exponents
  128. int num_exp_groups[AC3_MAX_CHANNELS]; ///< Number of exponent groups (nexpgrp)
  129. int8_t dexps[AC3_MAX_CHANNELS][AC3_MAX_COEFS]; ///< decoded exponents
  130. int exp_strategy[MAX_BLOCKS][AC3_MAX_CHANNELS]; ///< exponent strategies (expstr)
  131. ///@}
  132. ///@defgroup bitalloc bit allocation
  133. AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
  134. int first_cpl_leak; ///< first coupling leak state (firstcplleak)
  135. int snr_offset[AC3_MAX_CHANNELS]; ///< signal-to-noise ratio offsets (snroffst)
  136. int fast_gain[AC3_MAX_CHANNELS]; ///< fast gain values/SMR's (fgain)
  137. uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS]; ///< bit allocation pointers
  138. int16_t psd[AC3_MAX_CHANNELS][AC3_MAX_COEFS]; ///< scaled exponents
  139. int16_t band_psd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents
  140. int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values
  141. int dba_mode[AC3_MAX_CHANNELS]; ///< delta bit allocation mode
  142. int dba_nsegs[AC3_MAX_CHANNELS]; ///< number of delta segments
  143. uint8_t dba_offsets[AC3_MAX_CHANNELS][8]; ///< delta segment offsets
  144. uint8_t dba_lengths[AC3_MAX_CHANNELS][8]; ///< delta segment lengths
  145. uint8_t dba_values[AC3_MAX_CHANNELS][8]; ///< delta values for each segment
  146. ///@}
  147. ///@defgroup dithering zero-mantissa dithering
  148. int dither_flag[AC3_MAX_CHANNELS]; ///< dither flags (dithflg)
  149. AVLFG dith_state; ///< for dither generation
  150. ///@}
  151. ///@defgroup imdct IMDCT
  152. int block_switch[AC3_MAX_CHANNELS]; ///< block switch flags (blksw)
  153. MDCTContext imdct_512; ///< for 512 sample IMDCT
  154. MDCTContext imdct_256; ///< for 256 sample IMDCT
  155. ///@}
  156. ///@defgroup opt optimization
  157. DSPContext dsp; ///< for optimization
  158. float add_bias; ///< offset for float_to_int16 conversion
  159. float mul_bias; ///< scaling for float_to_int16 conversion
  160. ///@}
  161. DECLARE_ALIGNED_16(int, fixed_coeffs[AC3_MAX_CHANNELS][AC3_MAX_COEFS]); ///> fixed-point transform coefficients
  162. ///@defgroup arrays aligned arrays
  163. DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][AC3_MAX_COEFS]); ///< transform coefficients
  164. DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]); ///< delay - added to the next block
  165. DECLARE_ALIGNED_16(float, window[AC3_BLOCK_SIZE]); ///< window coefficients
  166. DECLARE_ALIGNED_16(float, tmp_output[AC3_BLOCK_SIZE]); ///< temporary storage for output before windowing
  167. DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]); ///< output after imdct transform and windowing
  168. ///@}
  169. } AC3DecodeContext;
  170. /**
  171. * Parse the E-AC-3 frame header.
  172. * This parses both the bit stream info and audio frame header.
  173. */
  174. int ff_eac3_parse_header(AC3DecodeContext *s);
  175. /**
  176. * Decode mantissas in a single channel for the entire frame.
  177. * This is used when AHT mode is enabled.
  178. */
  179. void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch);
  180. void ff_eac3_apply_spectral_extension(AC3DecodeContext *s);
  181. #endif /* AVCODEC_AC3DEC_H */