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.

299 lines
13KB

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