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.

283 lines
12KB

  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. #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
  39. #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  40. #define AC3ENC_TYPE_AC3_FIXED 0
  41. #define AC3ENC_TYPE_AC3 1
  42. #define AC3ENC_TYPE_EAC3 2
  43. #if CONFIG_AC3ENC_FLOAT
  44. #define AC3_NAME(x) ff_ac3_float_ ## x
  45. #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
  46. #define COEF_MIN (-16777215.0/16777216.0)
  47. #define COEF_MAX ( 16777215.0/16777216.0)
  48. typedef float SampleType;
  49. typedef float CoefType;
  50. typedef float CoefSumType;
  51. #else
  52. #define AC3_NAME(x) ff_ac3_fixed_ ## x
  53. #define MAC_COEF(d,a,b) MAC64(d,a,b)
  54. #define COEF_MIN -16777215
  55. #define COEF_MAX 16777215
  56. typedef int16_t SampleType;
  57. typedef int32_t CoefType;
  58. typedef int64_t CoefSumType;
  59. #endif
  60. typedef struct AC3MDCTContext {
  61. const SampleType *window; ///< MDCT window function
  62. FFTContext fft; ///< FFT context for MDCT calculation
  63. } AC3MDCTContext;
  64. #if 0
  65. /**
  66. * Encoding Options used by AVOption.
  67. */
  68. typedef struct AC3EncOptions {
  69. /* AC-3 metadata options*/
  70. int dialogue_level;
  71. int bitstream_mode;
  72. float center_mix_level;
  73. float surround_mix_level;
  74. int dolby_surround_mode;
  75. int audio_production_info;
  76. int mixing_level;
  77. int room_type;
  78. int copyright;
  79. int original;
  80. int extended_bsi_1;
  81. int preferred_stereo_downmix;
  82. float ltrt_center_mix_level;
  83. float ltrt_surround_mix_level;
  84. float loro_center_mix_level;
  85. float loro_surround_mix_level;
  86. int extended_bsi_2;
  87. int dolby_surround_ex_mode;
  88. int dolby_headphone_mode;
  89. int ad_converter_type;
  90. /* other encoding options */
  91. int allow_per_frame_metadata;
  92. int stereo_rematrixing;
  93. int channel_coupling;
  94. int cpl_start;
  95. } AC3EncOptions;
  96. #endif
  97. /**
  98. * Data for a single audio block.
  99. */
  100. typedef struct AC3Block {
  101. CoefType **mdct_coef; ///< MDCT coefficients
  102. int32_t **fixed_coef; ///< fixed-point MDCT coefficients
  103. uint8_t **exp; ///< original exponents
  104. uint8_t **grouped_exp; ///< grouped exponents
  105. int16_t **psd; ///< psd per frequency bin
  106. int16_t **band_psd; ///< psd per critical band
  107. int16_t **mask; ///< masking curve
  108. uint16_t **qmant; ///< quantized mantissas
  109. uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
  110. uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
  111. uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
  112. uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
  113. int num_rematrixing_bands; ///< number of rematrixing bands
  114. uint8_t rematrixing_flags[4]; ///< rematrixing flags
  115. int new_cpl_strategy; ///< send new coupling strategy
  116. int cpl_in_use; ///< coupling in use for this block (cplinu)
  117. uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
  118. int num_cpl_channels; ///< number of channels in coupling
  119. uint8_t new_cpl_coords; ///< send new coupling coordinates (cplcoe)
  120. uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
  121. int new_snr_offsets; ///< send new SNR offsets
  122. int new_cpl_leak; ///< send new coupling leak info
  123. int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
  124. } AC3Block;
  125. /**
  126. * AC-3 encoder private context.
  127. */
  128. typedef struct AC3EncodeContext {
  129. AVClass *av_class; ///< AVClass used for AVOption
  130. AC3EncOptions options; ///< encoding options
  131. AVCodecContext *avctx; ///< parent AVCodecContext
  132. PutBitContext pb; ///< bitstream writer context
  133. DSPContext dsp;
  134. AC3DSPContext ac3dsp; ///< AC-3 optimized functions
  135. AC3MDCTContext *mdct; ///< MDCT context
  136. AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
  137. int fixed_point; ///< indicates if fixed-point encoder is being used
  138. int eac3; ///< indicates if this is E-AC-3 vs. AC-3
  139. int bitstream_id; ///< bitstream id (bsid)
  140. int bitstream_mode; ///< bitstream mode (bsmod)
  141. int bit_rate; ///< target bit rate, in bits-per-second
  142. int sample_rate; ///< sampling frequency, in Hz
  143. int frame_size_min; ///< minimum frame size in case rounding is necessary
  144. int frame_size; ///< current frame size in bytes
  145. int frame_size_code; ///< frame size code (frmsizecod)
  146. uint16_t crc_inv[2];
  147. int64_t bits_written; ///< bit count (used to avg. bitrate)
  148. int64_t samples_written; ///< sample count (used to avg. bitrate)
  149. int fbw_channels; ///< number of full-bandwidth channels (nfchans)
  150. int channels; ///< total number of channels (nchans)
  151. int lfe_on; ///< indicates if there is an LFE channel (lfeon)
  152. int lfe_channel; ///< channel index of the LFE channel
  153. int has_center; ///< indicates if there is a center channel
  154. int has_surround; ///< indicates if there are one or more surround channels
  155. int channel_mode; ///< channel mode (acmod)
  156. const uint8_t *channel_map; ///< channel map used to reorder channels
  157. int center_mix_level; ///< center mix level code
  158. int surround_mix_level; ///< surround mix level code
  159. int ltrt_center_mix_level; ///< Lt/Rt center mix level code
  160. int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
  161. int loro_center_mix_level; ///< Lo/Ro center mix level code
  162. int loro_surround_mix_level; ///< Lo/Ro surround mix level code
  163. int cutoff; ///< user-specified cutoff frequency, in Hz
  164. int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
  165. int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
  166. int cpl_end_freq; ///< coupling channel end frequency bin
  167. int cpl_on; ///< coupling turned on for this frame
  168. int cpl_enabled; ///< coupling enabled for all frames
  169. int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
  170. int num_cpl_bands; ///< number of coupling bands (ncplbnd)
  171. uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
  172. int rematrixing_enabled; ///< stereo rematrixing enabled
  173. /* bitrate allocation control */
  174. int slow_gain_code; ///< slow gain code (sgaincod)
  175. int slow_decay_code; ///< slow decay code (sdcycod)
  176. int fast_decay_code; ///< fast decay code (fdcycod)
  177. int db_per_bit_code; ///< dB/bit code (dbpbcod)
  178. int floor_code; ///< floor code (floorcod)
  179. AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
  180. int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
  181. int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
  182. int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
  183. int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
  184. int frame_bits; ///< all frame bits except exponents and mantissas
  185. int exponent_bits; ///< number of bits used for exponents
  186. SampleType *windowed_samples;
  187. SampleType **planar_samples;
  188. uint8_t *bap_buffer;
  189. uint8_t *bap1_buffer;
  190. CoefType *mdct_coef_buffer;
  191. int32_t *fixed_coef_buffer;
  192. uint8_t *exp_buffer;
  193. uint8_t *grouped_exp_buffer;
  194. int16_t *psd_buffer;
  195. int16_t *band_psd_buffer;
  196. int16_t *mask_buffer;
  197. int16_t *qmant_buffer;
  198. uint8_t *cpl_coord_exp_buffer;
  199. uint8_t *cpl_coord_mant_buffer;
  200. uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
  201. uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
  202. uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
  203. int ref_bap_set; ///< indicates if ref_bap pointers have been set
  204. /* fixed vs. float function pointers */
  205. void (*mdct_end)(AC3MDCTContext *mdct);
  206. int (*mdct_init)(AVCodecContext *avctx, AC3MDCTContext *mdct, int nbits);
  207. /* fixed vs. float templated function pointers */
  208. int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
  209. /* AC-3 vs. E-AC-3 function pointers */
  210. void (*output_frame_header)(struct AC3EncodeContext *s);
  211. } AC3EncodeContext;
  212. int ff_ac3_encode_init(AVCodecContext *avctx);
  213. int ff_ac3_encode_close(AVCodecContext *avctx);
  214. int ff_ac3_validate_metadata(AVCodecContext *avctx);
  215. void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
  216. void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
  217. void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
  218. void ff_ac3_process_exponents(AC3EncodeContext *s);
  219. int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
  220. void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
  221. void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
  222. /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
  223. void ff_ac3_fixed_mdct_end(AC3MDCTContext *mdct);
  224. void ff_ac3_float_mdct_end(AC3MDCTContext *mdct);
  225. int ff_ac3_fixed_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
  226. int nbits);
  227. int ff_ac3_float_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
  228. int nbits);
  229. /* prototypes for functions in ac3enc_template.c */
  230. int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
  231. int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
  232. int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, unsigned char *frame,
  233. int buf_size, void *data);
  234. int ff_ac3_float_encode_frame(AVCodecContext *avctx, unsigned char *frame,
  235. int buf_size, void *data);
  236. #endif /* AVCODEC_AC3ENC_H */