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.

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