ac3enc.h 13 KB

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