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.

2256 lines
77KB

  1. /*
  2. * The simplest AC-3 encoder
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
  5. * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * The simplest AC-3 encoder.
  26. */
  27. //#define DEBUG
  28. //#define ASSERT_LEVEL 2
  29. #include "libavutil/audioconvert.h"
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/opt.h"
  33. #include "avcodec.h"
  34. #include "put_bits.h"
  35. #include "dsputil.h"
  36. #include "ac3dsp.h"
  37. #include "ac3.h"
  38. #include "audioconvert.h"
  39. #ifndef CONFIG_AC3ENC_FLOAT
  40. #define CONFIG_AC3ENC_FLOAT 0
  41. #endif
  42. /** Maximum number of exponent groups. +1 for separate DC exponent. */
  43. #define AC3_MAX_EXP_GROUPS 85
  44. /* stereo rematrixing algorithms */
  45. #define AC3_REMATRIXING_IS_STATIC 0x1
  46. #define AC3_REMATRIXING_SUMS 0
  47. #define AC3_REMATRIXING_NONE 1
  48. #define AC3_REMATRIXING_ALWAYS 3
  49. /** Scale a float value by 2^bits and convert to an integer. */
  50. #define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
  51. #if CONFIG_AC3ENC_FLOAT
  52. #include "ac3enc_float.h"
  53. #else
  54. #include "ac3enc_fixed.h"
  55. #endif
  56. /**
  57. * Encoding Options used by AVOption.
  58. */
  59. typedef struct AC3EncOptions {
  60. /* AC-3 metadata options*/
  61. int dialogue_level;
  62. int bitstream_mode;
  63. float center_mix_level;
  64. float surround_mix_level;
  65. int dolby_surround_mode;
  66. int audio_production_info;
  67. int mixing_level;
  68. int room_type;
  69. int copyright;
  70. int original;
  71. int extended_bsi_1;
  72. int preferred_stereo_downmix;
  73. float ltrt_center_mix_level;
  74. float ltrt_surround_mix_level;
  75. float loro_center_mix_level;
  76. float loro_surround_mix_level;
  77. int extended_bsi_2;
  78. int dolby_surround_ex_mode;
  79. int dolby_headphone_mode;
  80. int ad_converter_type;
  81. /* other encoding options */
  82. int allow_per_frame_metadata;
  83. } AC3EncOptions;
  84. /**
  85. * Data for a single audio block.
  86. */
  87. typedef struct AC3Block {
  88. uint8_t **bap; ///< bit allocation pointers (bap)
  89. CoefType **mdct_coef; ///< MDCT coefficients
  90. int32_t **fixed_coef; ///< fixed-point MDCT coefficients
  91. uint8_t **exp; ///< original exponents
  92. uint8_t **grouped_exp; ///< grouped exponents
  93. int16_t **psd; ///< psd per frequency bin
  94. int16_t **band_psd; ///< psd per critical band
  95. int16_t **mask; ///< masking curve
  96. uint16_t **qmant; ///< quantized mantissas
  97. uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values
  98. uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
  99. uint8_t rematrixing_flags[4]; ///< rematrixing flags
  100. struct AC3Block *exp_ref_block[AC3_MAX_CHANNELS]; ///< reference blocks for EXP_REUSE
  101. } AC3Block;
  102. /**
  103. * AC-3 encoder private context.
  104. */
  105. typedef struct AC3EncodeContext {
  106. AVClass *av_class; ///< AVClass used for AVOption
  107. AC3EncOptions options; ///< encoding options
  108. PutBitContext pb; ///< bitstream writer context
  109. DSPContext dsp;
  110. AC3DSPContext ac3dsp; ///< AC-3 optimized functions
  111. AC3MDCTContext mdct; ///< MDCT context
  112. AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
  113. int bitstream_id; ///< bitstream id (bsid)
  114. int bitstream_mode; ///< bitstream mode (bsmod)
  115. int bit_rate; ///< target bit rate, in bits-per-second
  116. int sample_rate; ///< sampling frequency, in Hz
  117. int frame_size_min; ///< minimum frame size in case rounding is necessary
  118. int frame_size; ///< current frame size in bytes
  119. int frame_size_code; ///< frame size code (frmsizecod)
  120. uint16_t crc_inv[2];
  121. int bits_written; ///< bit count (used to avg. bitrate)
  122. int samples_written; ///< sample count (used to avg. bitrate)
  123. int fbw_channels; ///< number of full-bandwidth channels (nfchans)
  124. int channels; ///< total number of channels (nchans)
  125. int lfe_on; ///< indicates if there is an LFE channel (lfeon)
  126. int lfe_channel; ///< channel index of the LFE channel
  127. int has_center; ///< indicates if there is a center channel
  128. int has_surround; ///< indicates if there are one or more surround channels
  129. int channel_mode; ///< channel mode (acmod)
  130. const uint8_t *channel_map; ///< channel map used to reorder channels
  131. int center_mix_level; ///< center mix level code
  132. int surround_mix_level; ///< surround mix level code
  133. int ltrt_center_mix_level; ///< Lt/Rt center mix level code
  134. int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
  135. int loro_center_mix_level; ///< Lo/Ro center mix level code
  136. int loro_surround_mix_level; ///< Lo/Ro surround mix level code
  137. int cutoff; ///< user-specified cutoff frequency, in Hz
  138. int bandwidth_code[AC3_MAX_CHANNELS]; ///< bandwidth code (0 to 60) (chbwcod)
  139. int nb_coefs[AC3_MAX_CHANNELS];
  140. int rematrixing; ///< determines how rematrixing strategy is calculated
  141. int num_rematrixing_bands; ///< number of rematrixing bands
  142. /* bitrate allocation control */
  143. int slow_gain_code; ///< slow gain code (sgaincod)
  144. int slow_decay_code; ///< slow decay code (sdcycod)
  145. int fast_decay_code; ///< fast decay code (fdcycod)
  146. int db_per_bit_code; ///< dB/bit code (dbpbcod)
  147. int floor_code; ///< floor code (floorcod)
  148. AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
  149. int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
  150. int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
  151. int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
  152. int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
  153. int frame_bits; ///< all frame bits except exponents and mantissas
  154. int exponent_bits; ///< number of bits used for exponents
  155. SampleType **planar_samples;
  156. uint8_t *bap_buffer;
  157. uint8_t *bap1_buffer;
  158. CoefType *mdct_coef_buffer;
  159. int32_t *fixed_coef_buffer;
  160. uint8_t *exp_buffer;
  161. uint8_t *grouped_exp_buffer;
  162. int16_t *psd_buffer;
  163. int16_t *band_psd_buffer;
  164. int16_t *mask_buffer;
  165. uint16_t *qmant_buffer;
  166. uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
  167. DECLARE_ALIGNED(16, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
  168. } AC3EncodeContext;
  169. typedef struct AC3Mant {
  170. uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
  171. int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4
  172. } AC3Mant;
  173. #define CMIXLEV_NUM_OPTIONS 3
  174. static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
  175. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
  176. };
  177. #define SURMIXLEV_NUM_OPTIONS 3
  178. static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
  179. LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  180. };
  181. #define EXTMIXLEV_NUM_OPTIONS 8
  182. static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
  183. LEVEL_PLUS_3DB, LEVEL_PLUS_1POINT5DB, LEVEL_ONE, LEVEL_MINUS_4POINT5DB,
  184. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  185. };
  186. #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
  187. #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  188. static const AVOption options[] = {
  189. /* Metadata Options */
  190. {"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
  191. /* downmix levels */
  192. {"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_4POINT5DB, 0.0, 1.0, AC3ENC_PARAM},
  193. {"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), FF_OPT_TYPE_FLOAT, LEVEL_MINUS_6DB, 0.0, 1.0, AC3ENC_PARAM},
  194. /* audio production information */
  195. {"mixing_level", "Mixing Level", OFFSET(mixing_level), FF_OPT_TYPE_INT, -1, -1, 111, AC3ENC_PARAM},
  196. {"room_type", "Room Type", OFFSET(room_type), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "room_type"},
  197. {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  198. {"large", "Large Room", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  199. {"small", "Small Room", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  200. /* other metadata options */
  201. {"copyright", "Copyright Bit", OFFSET(copyright), FF_OPT_TYPE_INT, 0, 0, 1, AC3ENC_PARAM},
  202. {"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), FF_OPT_TYPE_INT, -31, -31, -1, AC3ENC_PARAM},
  203. {"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), FF_OPT_TYPE_INT, 0, 0, 2, AC3ENC_PARAM, "dsur_mode"},
  204. {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  205. {"on", "Dolby Surround Encoded", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  206. {"off", "Not Dolby Surround Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  207. {"original", "Original Bit Stream", OFFSET(original), FF_OPT_TYPE_INT, 1, 0, 1, AC3ENC_PARAM},
  208. /* extended bitstream information */
  209. {"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dmix_mode"},
  210. {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  211. {"ltrt", "Lt/Rt Downmix Preferred", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  212. {"loro", "Lo/Ro Downmix Preferred", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  213. {"ltrt_cmixlev", "Lt/Rt Center Mix Level", OFFSET(ltrt_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
  214. {"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
  215. {"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
  216. {"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), FF_OPT_TYPE_FLOAT, -1.0, -1.0, 2.0, AC3ENC_PARAM},
  217. {"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dsurex_mode"},
  218. {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  219. {"on", "Dolby Surround EX Encoded", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  220. {"off", "Not Dolby Surround EX Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  221. {"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), FF_OPT_TYPE_INT, -1, -1, 2, AC3ENC_PARAM, "dheadphone_mode"},
  222. {"notindicated", "Not Indicated (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  223. {"on", "Dolby Headphone Encoded", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  224. {"off", "Not Dolby Headphone Encoded", 0, FF_OPT_TYPE_CONST, 2, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  225. {"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), FF_OPT_TYPE_INT, -1, -1, 1, AC3ENC_PARAM, "ad_conv_type"},
  226. {"standard", "Standard (default)", 0, FF_OPT_TYPE_CONST, 0, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
  227. {"hdcd", "HDCD", 0, FF_OPT_TYPE_CONST, 1, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
  228. {NULL}
  229. };
  230. #if CONFIG_AC3ENC_FLOAT
  231. static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
  232. options, LIBAVUTIL_VERSION_INT };
  233. #else
  234. static AVClass ac3enc_class = { "Fixed-Point AC-3 Encoder", av_default_item_name,
  235. options, LIBAVUTIL_VERSION_INT };
  236. #endif
  237. /* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
  238. static av_cold void mdct_end(AC3MDCTContext *mdct);
  239. static av_cold int mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
  240. int nbits);
  241. static void mdct512(AC3MDCTContext *mdct, CoefType *out, SampleType *in);
  242. static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
  243. const SampleType *window, unsigned int len);
  244. static int normalize_samples(AC3EncodeContext *s);
  245. static void scale_coefficients(AC3EncodeContext *s);
  246. /**
  247. * LUT for number of exponent groups.
  248. * exponent_group_tab[exponent strategy-1][number of coefficients]
  249. */
  250. static uint8_t exponent_group_tab[3][256];
  251. /**
  252. * List of supported channel layouts.
  253. */
  254. static const int64_t ac3_channel_layouts[] = {
  255. AV_CH_LAYOUT_MONO,
  256. AV_CH_LAYOUT_STEREO,
  257. AV_CH_LAYOUT_2_1,
  258. AV_CH_LAYOUT_SURROUND,
  259. AV_CH_LAYOUT_2_2,
  260. AV_CH_LAYOUT_QUAD,
  261. AV_CH_LAYOUT_4POINT0,
  262. AV_CH_LAYOUT_5POINT0,
  263. AV_CH_LAYOUT_5POINT0_BACK,
  264. (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY),
  265. (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY),
  266. (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY),
  267. (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
  268. (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY),
  269. (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY),
  270. (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY),
  271. AV_CH_LAYOUT_5POINT1,
  272. AV_CH_LAYOUT_5POINT1_BACK,
  273. 0
  274. };
  275. /**
  276. * Adjust the frame size to make the average bit rate match the target bit rate.
  277. * This is only needed for 11025, 22050, and 44100 sample rates.
  278. */
  279. static void adjust_frame_size(AC3EncodeContext *s)
  280. {
  281. while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
  282. s->bits_written -= s->bit_rate;
  283. s->samples_written -= s->sample_rate;
  284. }
  285. s->frame_size = s->frame_size_min +
  286. 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
  287. s->bits_written += s->frame_size * 8;
  288. s->samples_written += AC3_FRAME_SIZE;
  289. }
  290. /**
  291. * Deinterleave input samples.
  292. * Channels are reordered from FFmpeg's default order to AC-3 order.
  293. */
  294. static void deinterleave_input_samples(AC3EncodeContext *s,
  295. const SampleType *samples)
  296. {
  297. int ch, i;
  298. /* deinterleave and remap input samples */
  299. for (ch = 0; ch < s->channels; ch++) {
  300. const SampleType *sptr;
  301. int sinc;
  302. /* copy last 256 samples of previous frame to the start of the current frame */
  303. memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
  304. AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
  305. /* deinterleave */
  306. sinc = s->channels;
  307. sptr = samples + s->channel_map[ch];
  308. for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
  309. s->planar_samples[ch][i] = *sptr;
  310. sptr += sinc;
  311. }
  312. }
  313. }
  314. /**
  315. * Apply the MDCT to input samples to generate frequency coefficients.
  316. * This applies the KBD window and normalizes the input to reduce precision
  317. * loss due to fixed-point calculations.
  318. */
  319. static void apply_mdct(AC3EncodeContext *s)
  320. {
  321. int blk, ch;
  322. for (ch = 0; ch < s->channels; ch++) {
  323. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  324. AC3Block *block = &s->blocks[blk];
  325. const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
  326. apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
  327. block->coeff_shift[ch] = normalize_samples(s);
  328. mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples);
  329. }
  330. }
  331. }
  332. /**
  333. * Initialize stereo rematrixing.
  334. * If the strategy does not change for each frame, set the rematrixing flags.
  335. */
  336. static void rematrixing_init(AC3EncodeContext *s)
  337. {
  338. if (s->channel_mode == AC3_CHMODE_STEREO)
  339. s->rematrixing = AC3_REMATRIXING_SUMS;
  340. else
  341. s->rematrixing = AC3_REMATRIXING_NONE;
  342. /* NOTE: AC3_REMATRIXING_ALWAYS might be used in
  343. the future in conjunction with channel coupling. */
  344. if (s->rematrixing & AC3_REMATRIXING_IS_STATIC) {
  345. int flag = (s->rematrixing == AC3_REMATRIXING_ALWAYS);
  346. s->blocks[0].new_rematrixing_strategy = 1;
  347. memset(s->blocks[0].rematrixing_flags, flag,
  348. sizeof(s->blocks[0].rematrixing_flags));
  349. }
  350. }
  351. /**
  352. * Determine rematrixing flags for each block and band.
  353. */
  354. static void compute_rematrixing_strategy(AC3EncodeContext *s)
  355. {
  356. int nb_coefs;
  357. int blk, bnd, i;
  358. AC3Block *block, *block0;
  359. s->num_rematrixing_bands = 4;
  360. if (s->rematrixing & AC3_REMATRIXING_IS_STATIC)
  361. return;
  362. nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
  363. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  364. block = &s->blocks[blk];
  365. block->new_rematrixing_strategy = !blk;
  366. for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
  367. /* calculate calculate sum of squared coeffs for one band in one block */
  368. int start = ff_ac3_rematrix_band_tab[bnd];
  369. int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  370. CoefSumType sum[4] = {0,};
  371. for (i = start; i < end; i++) {
  372. CoefType lt = block->mdct_coef[0][i];
  373. CoefType rt = block->mdct_coef[1][i];
  374. CoefType md = lt + rt;
  375. CoefType sd = lt - rt;
  376. MAC_COEF(sum[0], lt, lt);
  377. MAC_COEF(sum[1], rt, rt);
  378. MAC_COEF(sum[2], md, md);
  379. MAC_COEF(sum[3], sd, sd);
  380. }
  381. /* compare sums to determine if rematrixing will be used for this band */
  382. if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
  383. block->rematrixing_flags[bnd] = 1;
  384. else
  385. block->rematrixing_flags[bnd] = 0;
  386. /* determine if new rematrixing flags will be sent */
  387. if (blk &&
  388. block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
  389. block->new_rematrixing_strategy = 1;
  390. }
  391. }
  392. block0 = block;
  393. }
  394. }
  395. /**
  396. * Apply stereo rematrixing to coefficients based on rematrixing flags.
  397. */
  398. static void apply_rematrixing(AC3EncodeContext *s)
  399. {
  400. int nb_coefs;
  401. int blk, bnd, i;
  402. int start, end;
  403. uint8_t *flags;
  404. if (s->rematrixing == AC3_REMATRIXING_NONE)
  405. return;
  406. nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
  407. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  408. AC3Block *block = &s->blocks[blk];
  409. if (block->new_rematrixing_strategy)
  410. flags = block->rematrixing_flags;
  411. for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
  412. if (flags[bnd]) {
  413. start = ff_ac3_rematrix_band_tab[bnd];
  414. end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  415. for (i = start; i < end; i++) {
  416. int32_t lt = block->fixed_coef[0][i];
  417. int32_t rt = block->fixed_coef[1][i];
  418. block->fixed_coef[0][i] = (lt + rt) >> 1;
  419. block->fixed_coef[1][i] = (lt - rt) >> 1;
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * Initialize exponent tables.
  427. */
  428. static av_cold void exponent_init(AC3EncodeContext *s)
  429. {
  430. int i;
  431. for (i = 73; i < 256; i++) {
  432. exponent_group_tab[0][i] = (i - 1) / 3;
  433. exponent_group_tab[1][i] = (i + 2) / 6;
  434. exponent_group_tab[2][i] = (i + 8) / 12;
  435. }
  436. /* LFE */
  437. exponent_group_tab[0][7] = 2;
  438. }
  439. /**
  440. * Extract exponents from the MDCT coefficients.
  441. * This takes into account the normalization that was done to the input samples
  442. * by adjusting the exponents by the exponent shift values.
  443. */
  444. static void extract_exponents(AC3EncodeContext *s)
  445. {
  446. int blk, ch, i;
  447. for (ch = 0; ch < s->channels; ch++) {
  448. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  449. AC3Block *block = &s->blocks[blk];
  450. uint8_t *exp = block->exp[ch];
  451. int32_t *coef = block->fixed_coef[ch];
  452. for (i = 0; i < AC3_MAX_COEFS; i++) {
  453. int e;
  454. int v = abs(coef[i]);
  455. if (v == 0)
  456. e = 24;
  457. else {
  458. e = 23 - av_log2(v);
  459. if (e >= 24) {
  460. e = 24;
  461. coef[i] = 0;
  462. }
  463. av_assert2(e >= 0);
  464. }
  465. exp[i] = e;
  466. }
  467. }
  468. }
  469. }
  470. /**
  471. * Exponent Difference Threshold.
  472. * New exponents are sent if their SAD exceed this number.
  473. */
  474. #define EXP_DIFF_THRESHOLD 500
  475. /**
  476. * Calculate exponent strategies for all blocks in a single channel.
  477. */
  478. static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
  479. uint8_t *exp)
  480. {
  481. int blk, blk1;
  482. int exp_diff;
  483. /* estimate if the exponent variation & decide if they should be
  484. reused in the next frame */
  485. exp_strategy[0] = EXP_NEW;
  486. exp += AC3_MAX_COEFS;
  487. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
  488. exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
  489. if (exp_diff > EXP_DIFF_THRESHOLD)
  490. exp_strategy[blk] = EXP_NEW;
  491. else
  492. exp_strategy[blk] = EXP_REUSE;
  493. exp += AC3_MAX_COEFS;
  494. }
  495. /* now select the encoding strategy type : if exponents are often
  496. recoded, we use a coarse encoding */
  497. blk = 0;
  498. while (blk < AC3_MAX_BLOCKS) {
  499. blk1 = blk + 1;
  500. while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
  501. blk1++;
  502. switch (blk1 - blk) {
  503. case 1: exp_strategy[blk] = EXP_D45; break;
  504. case 2:
  505. case 3: exp_strategy[blk] = EXP_D25; break;
  506. default: exp_strategy[blk] = EXP_D15; break;
  507. }
  508. blk = blk1;
  509. }
  510. }
  511. /**
  512. * Calculate exponent strategies for all channels.
  513. * Array arrangement is reversed to simplify the per-channel calculation.
  514. */
  515. static void compute_exp_strategy(AC3EncodeContext *s)
  516. {
  517. int ch, blk;
  518. for (ch = 0; ch < s->fbw_channels; ch++) {
  519. compute_exp_strategy_ch(s, s->exp_strategy[ch], s->blocks[0].exp[ch]);
  520. }
  521. if (s->lfe_on) {
  522. ch = s->lfe_channel;
  523. s->exp_strategy[ch][0] = EXP_D15;
  524. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
  525. s->exp_strategy[ch][blk] = EXP_REUSE;
  526. }
  527. }
  528. /**
  529. * Update the exponents so that they are the ones the decoder will decode.
  530. */
  531. static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
  532. {
  533. int nb_groups, i, k;
  534. nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
  535. /* for each group, compute the minimum exponent */
  536. switch(exp_strategy) {
  537. case EXP_D25:
  538. for (i = 1, k = 1; i <= nb_groups; i++) {
  539. uint8_t exp_min = exp[k];
  540. if (exp[k+1] < exp_min)
  541. exp_min = exp[k+1];
  542. exp[i] = exp_min;
  543. k += 2;
  544. }
  545. break;
  546. case EXP_D45:
  547. for (i = 1, k = 1; i <= nb_groups; i++) {
  548. uint8_t exp_min = exp[k];
  549. if (exp[k+1] < exp_min)
  550. exp_min = exp[k+1];
  551. if (exp[k+2] < exp_min)
  552. exp_min = exp[k+2];
  553. if (exp[k+3] < exp_min)
  554. exp_min = exp[k+3];
  555. exp[i] = exp_min;
  556. k += 4;
  557. }
  558. break;
  559. }
  560. /* constraint for DC exponent */
  561. if (exp[0] > 15)
  562. exp[0] = 15;
  563. /* decrease the delta between each groups to within 2 so that they can be
  564. differentially encoded */
  565. for (i = 1; i <= nb_groups; i++)
  566. exp[i] = FFMIN(exp[i], exp[i-1] + 2);
  567. i--;
  568. while (--i >= 0)
  569. exp[i] = FFMIN(exp[i], exp[i+1] + 2);
  570. /* now we have the exponent values the decoder will see */
  571. switch (exp_strategy) {
  572. case EXP_D25:
  573. for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
  574. uint8_t exp1 = exp[i];
  575. exp[k--] = exp1;
  576. exp[k--] = exp1;
  577. }
  578. break;
  579. case EXP_D45:
  580. for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
  581. exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
  582. k -= 4;
  583. }
  584. break;
  585. }
  586. }
  587. /**
  588. * Encode exponents from original extracted form to what the decoder will see.
  589. * This copies and groups exponents based on exponent strategy and reduces
  590. * deltas between adjacent exponent groups so that they can be differentially
  591. * encoded.
  592. */
  593. static void encode_exponents(AC3EncodeContext *s)
  594. {
  595. int blk, blk1, ch;
  596. uint8_t *exp, *exp_strategy;
  597. int nb_coefs, num_reuse_blocks;
  598. for (ch = 0; ch < s->channels; ch++) {
  599. exp = s->blocks[0].exp[ch];
  600. exp_strategy = s->exp_strategy[ch];
  601. nb_coefs = s->nb_coefs[ch];
  602. blk = 0;
  603. while (blk < AC3_MAX_BLOCKS) {
  604. blk1 = blk + 1;
  605. /* count the number of EXP_REUSE blocks after the current block
  606. and set exponent reference block pointers */
  607. s->blocks[blk].exp_ref_block[ch] = &s->blocks[blk];
  608. while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE) {
  609. s->blocks[blk1].exp_ref_block[ch] = &s->blocks[blk];
  610. blk1++;
  611. }
  612. num_reuse_blocks = blk1 - blk - 1;
  613. /* for the EXP_REUSE case we select the min of the exponents */
  614. s->ac3dsp.ac3_exponent_min(exp, num_reuse_blocks, nb_coefs);
  615. encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);
  616. exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
  617. blk = blk1;
  618. }
  619. }
  620. }
  621. /**
  622. * Group exponents.
  623. * 3 delta-encoded exponents are in each 7-bit group. The number of groups
  624. * varies depending on exponent strategy and bandwidth.
  625. */
  626. static void group_exponents(AC3EncodeContext *s)
  627. {
  628. int blk, ch, i;
  629. int group_size, nb_groups, bit_count;
  630. uint8_t *p;
  631. int delta0, delta1, delta2;
  632. int exp0, exp1;
  633. bit_count = 0;
  634. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  635. AC3Block *block = &s->blocks[blk];
  636. for (ch = 0; ch < s->channels; ch++) {
  637. int exp_strategy = s->exp_strategy[ch][blk];
  638. if (exp_strategy == EXP_REUSE)
  639. continue;
  640. group_size = exp_strategy + (exp_strategy == EXP_D45);
  641. nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
  642. bit_count += 4 + (nb_groups * 7);
  643. p = block->exp[ch];
  644. /* DC exponent */
  645. exp1 = *p++;
  646. block->grouped_exp[ch][0] = exp1;
  647. /* remaining exponents are delta encoded */
  648. for (i = 1; i <= nb_groups; i++) {
  649. /* merge three delta in one code */
  650. exp0 = exp1;
  651. exp1 = p[0];
  652. p += group_size;
  653. delta0 = exp1 - exp0 + 2;
  654. av_assert2(delta0 >= 0 && delta0 <= 4);
  655. exp0 = exp1;
  656. exp1 = p[0];
  657. p += group_size;
  658. delta1 = exp1 - exp0 + 2;
  659. av_assert2(delta1 >= 0 && delta1 <= 4);
  660. exp0 = exp1;
  661. exp1 = p[0];
  662. p += group_size;
  663. delta2 = exp1 - exp0 + 2;
  664. av_assert2(delta2 >= 0 && delta2 <= 4);
  665. block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
  666. }
  667. }
  668. }
  669. s->exponent_bits = bit_count;
  670. }
  671. /**
  672. * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
  673. * Extract exponents from MDCT coefficients, calculate exponent strategies,
  674. * and encode final exponents.
  675. */
  676. static void process_exponents(AC3EncodeContext *s)
  677. {
  678. extract_exponents(s);
  679. compute_exp_strategy(s);
  680. encode_exponents(s);
  681. group_exponents(s);
  682. emms_c();
  683. }
  684. /**
  685. * Count frame bits that are based solely on fixed parameters.
  686. * This only has to be run once when the encoder is initialized.
  687. */
  688. static void count_frame_bits_fixed(AC3EncodeContext *s)
  689. {
  690. static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
  691. int blk;
  692. int frame_bits;
  693. /* assumptions:
  694. * no dynamic range codes
  695. * no channel coupling
  696. * bit allocation parameters do not change between blocks
  697. * SNR offsets do not change between blocks
  698. * no delta bit allocation
  699. * no skipped data
  700. * no auxilliary data
  701. */
  702. /* header size */
  703. frame_bits = 65;
  704. frame_bits += frame_bits_inc[s->channel_mode];
  705. /* audio blocks */
  706. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  707. frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
  708. if (s->channel_mode == AC3_CHMODE_STEREO) {
  709. frame_bits++; /* rematstr */
  710. }
  711. frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
  712. if (s->lfe_on)
  713. frame_bits++; /* lfeexpstr */
  714. frame_bits++; /* baie */
  715. frame_bits++; /* snr */
  716. frame_bits += 2; /* delta / skip */
  717. }
  718. frame_bits++; /* cplinu for block 0 */
  719. /* bit alloc info */
  720. /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
  721. /* csnroffset[6] */
  722. /* (fsnoffset[4] + fgaincod[4]) * c */
  723. frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
  724. /* auxdatae, crcrsv */
  725. frame_bits += 2;
  726. /* CRC */
  727. frame_bits += 16;
  728. s->frame_bits_fixed = frame_bits;
  729. }
  730. /**
  731. * Initialize bit allocation.
  732. * Set default parameter codes and calculate parameter values.
  733. */
  734. static void bit_alloc_init(AC3EncodeContext *s)
  735. {
  736. int ch;
  737. /* init default parameters */
  738. s->slow_decay_code = 2;
  739. s->fast_decay_code = 1;
  740. s->slow_gain_code = 1;
  741. s->db_per_bit_code = 3;
  742. s->floor_code = 7;
  743. for (ch = 0; ch < s->channels; ch++)
  744. s->fast_gain_code[ch] = 4;
  745. /* initial snr offset */
  746. s->coarse_snr_offset = 40;
  747. /* compute real values */
  748. /* currently none of these values change during encoding, so we can just
  749. set them once at initialization */
  750. s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
  751. s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
  752. s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
  753. s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
  754. s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
  755. count_frame_bits_fixed(s);
  756. }
  757. /**
  758. * Count the bits used to encode the frame, minus exponents and mantissas.
  759. * Bits based on fixed parameters have already been counted, so now we just
  760. * have to add the bits based on parameters that change during encoding.
  761. */
  762. static void count_frame_bits(AC3EncodeContext *s)
  763. {
  764. AC3EncOptions *opt = &s->options;
  765. int blk, ch;
  766. int frame_bits = 0;
  767. if (opt->audio_production_info)
  768. frame_bits += 7;
  769. if (s->bitstream_id == 6) {
  770. if (opt->extended_bsi_1)
  771. frame_bits += 14;
  772. if (opt->extended_bsi_2)
  773. frame_bits += 14;
  774. }
  775. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  776. /* stereo rematrixing */
  777. if (s->channel_mode == AC3_CHMODE_STEREO &&
  778. s->blocks[blk].new_rematrixing_strategy) {
  779. frame_bits += s->num_rematrixing_bands;
  780. }
  781. for (ch = 0; ch < s->fbw_channels; ch++) {
  782. if (s->exp_strategy[ch][blk] != EXP_REUSE)
  783. frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
  784. }
  785. }
  786. s->frame_bits = s->frame_bits_fixed + frame_bits;
  787. }
  788. /**
  789. * Finalize the mantissa bit count by adding in the grouped mantissas.
  790. */
  791. static int compute_mantissa_size_final(int mant_cnt[5])
  792. {
  793. // bap=1 : 3 mantissas in 5 bits
  794. int bits = (mant_cnt[1] / 3) * 5;
  795. // bap=2 : 3 mantissas in 7 bits
  796. // bap=4 : 2 mantissas in 7 bits
  797. bits += ((mant_cnt[2] / 3) + (mant_cnt[4] >> 1)) * 7;
  798. // bap=3 : each mantissa is 3 bits
  799. bits += mant_cnt[3] * 3;
  800. return bits;
  801. }
  802. /**
  803. * Calculate masking curve based on the final exponents.
  804. * Also calculate the power spectral densities to use in future calculations.
  805. */
  806. static void bit_alloc_masking(AC3EncodeContext *s)
  807. {
  808. int blk, ch;
  809. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  810. AC3Block *block = &s->blocks[blk];
  811. for (ch = 0; ch < s->channels; ch++) {
  812. /* We only need psd and mask for calculating bap.
  813. Since we currently do not calculate bap when exponent
  814. strategy is EXP_REUSE we do not need to calculate psd or mask. */
  815. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  816. ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
  817. s->nb_coefs[ch],
  818. block->psd[ch], block->band_psd[ch]);
  819. ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
  820. 0, s->nb_coefs[ch],
  821. ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
  822. ch == s->lfe_channel,
  823. DBA_NONE, 0, NULL, NULL, NULL,
  824. block->mask[ch]);
  825. }
  826. }
  827. }
  828. }
  829. /**
  830. * Ensure that bap for each block and channel point to the current bap_buffer.
  831. * They may have been switched during the bit allocation search.
  832. */
  833. static void reset_block_bap(AC3EncodeContext *s)
  834. {
  835. int blk, ch;
  836. if (s->blocks[0].bap[0] == s->bap_buffer)
  837. return;
  838. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  839. for (ch = 0; ch < s->channels; ch++) {
  840. s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
  841. }
  842. }
  843. }
  844. /**
  845. * Run the bit allocation with a given SNR offset.
  846. * This calculates the bit allocation pointers that will be used to determine
  847. * the quantization of each mantissa.
  848. * @return the number of bits needed for mantissas if the given SNR offset is
  849. * is used.
  850. */
  851. static int bit_alloc(AC3EncodeContext *s, int snr_offset)
  852. {
  853. int blk, ch;
  854. int mantissa_bits;
  855. int mant_cnt[5];
  856. snr_offset = (snr_offset - 240) << 2;
  857. reset_block_bap(s);
  858. mantissa_bits = 0;
  859. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  860. AC3Block *block;
  861. // initialize grouped mantissa counts. these are set so that they are
  862. // padded to the next whole group size when bits are counted in
  863. // compute_mantissa_size_final
  864. mant_cnt[0] = mant_cnt[3] = 0;
  865. mant_cnt[1] = mant_cnt[2] = 2;
  866. mant_cnt[4] = 1;
  867. for (ch = 0; ch < s->channels; ch++) {
  868. /* Currently the only bit allocation parameters which vary across
  869. blocks within a frame are the exponent values. We can take
  870. advantage of that by reusing the bit allocation pointers
  871. whenever we reuse exponents. */
  872. block = s->blocks[blk].exp_ref_block[ch];
  873. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  874. s->ac3dsp.bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
  875. s->nb_coefs[ch], snr_offset,
  876. s->bit_alloc.floor, ff_ac3_bap_tab,
  877. block->bap[ch]);
  878. }
  879. mantissa_bits += s->ac3dsp.compute_mantissa_size(mant_cnt, block->bap[ch], s->nb_coefs[ch]);
  880. }
  881. mantissa_bits += compute_mantissa_size_final(mant_cnt);
  882. }
  883. return mantissa_bits;
  884. }
  885. /**
  886. * Constant bitrate bit allocation search.
  887. * Find the largest SNR offset that will allow data to fit in the frame.
  888. */
  889. static int cbr_bit_allocation(AC3EncodeContext *s)
  890. {
  891. int ch;
  892. int bits_left;
  893. int snr_offset, snr_incr;
  894. bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
  895. av_assert2(bits_left >= 0);
  896. snr_offset = s->coarse_snr_offset << 4;
  897. /* if previous frame SNR offset was 1023, check if current frame can also
  898. use SNR offset of 1023. if so, skip the search. */
  899. if ((snr_offset | s->fine_snr_offset[0]) == 1023) {
  900. if (bit_alloc(s, 1023) <= bits_left)
  901. return 0;
  902. }
  903. while (snr_offset >= 0 &&
  904. bit_alloc(s, snr_offset) > bits_left) {
  905. snr_offset -= 64;
  906. }
  907. if (snr_offset < 0)
  908. return AVERROR(EINVAL);
  909. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  910. for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
  911. while (snr_offset + snr_incr <= 1023 &&
  912. bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
  913. snr_offset += snr_incr;
  914. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  915. }
  916. }
  917. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  918. reset_block_bap(s);
  919. s->coarse_snr_offset = snr_offset >> 4;
  920. for (ch = 0; ch < s->channels; ch++)
  921. s->fine_snr_offset[ch] = snr_offset & 0xF;
  922. return 0;
  923. }
  924. /**
  925. * Downgrade exponent strategies to reduce the bits used by the exponents.
  926. * This is a fallback for when bit allocation fails with the normal exponent
  927. * strategies. Each time this function is run it only downgrades the
  928. * strategy in 1 channel of 1 block.
  929. * @return non-zero if downgrade was unsuccessful
  930. */
  931. static int downgrade_exponents(AC3EncodeContext *s)
  932. {
  933. int ch, blk;
  934. for (ch = 0; ch < s->fbw_channels; ch++) {
  935. for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
  936. if (s->exp_strategy[ch][blk] == EXP_D15) {
  937. s->exp_strategy[ch][blk] = EXP_D25;
  938. return 0;
  939. }
  940. }
  941. }
  942. for (ch = 0; ch < s->fbw_channels; ch++) {
  943. for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
  944. if (s->exp_strategy[ch][blk] == EXP_D25) {
  945. s->exp_strategy[ch][blk] = EXP_D45;
  946. return 0;
  947. }
  948. }
  949. }
  950. for (ch = 0; ch < s->fbw_channels; ch++) {
  951. /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
  952. the block number > 0 */
  953. for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
  954. if (s->exp_strategy[ch][blk] > EXP_REUSE) {
  955. s->exp_strategy[ch][blk] = EXP_REUSE;
  956. return 0;
  957. }
  958. }
  959. }
  960. return -1;
  961. }
  962. /**
  963. * Reduce the bandwidth to reduce the number of bits used for a given SNR offset.
  964. * This is a second fallback for when bit allocation still fails after exponents
  965. * have been downgraded.
  966. * @return non-zero if bandwidth reduction was unsuccessful
  967. */
  968. static int reduce_bandwidth(AC3EncodeContext *s, int min_bw_code)
  969. {
  970. int ch;
  971. if (s->bandwidth_code[0] > min_bw_code) {
  972. for (ch = 0; ch < s->fbw_channels; ch++) {
  973. s->bandwidth_code[ch]--;
  974. s->nb_coefs[ch] = s->bandwidth_code[ch] * 3 + 73;
  975. }
  976. return 0;
  977. }
  978. return -1;
  979. }
  980. /**
  981. * Perform bit allocation search.
  982. * Finds the SNR offset value that maximizes quality and fits in the specified
  983. * frame size. Output is the SNR offset and a set of bit allocation pointers
  984. * used to quantize the mantissas.
  985. */
  986. static int compute_bit_allocation(AC3EncodeContext *s)
  987. {
  988. int ret;
  989. count_frame_bits(s);
  990. bit_alloc_masking(s);
  991. ret = cbr_bit_allocation(s);
  992. while (ret) {
  993. /* fallback 1: downgrade exponents */
  994. if (!downgrade_exponents(s)) {
  995. extract_exponents(s);
  996. encode_exponents(s);
  997. group_exponents(s);
  998. ret = compute_bit_allocation(s);
  999. continue;
  1000. }
  1001. /* fallback 2: reduce bandwidth */
  1002. /* only do this if the user has not specified a specific cutoff
  1003. frequency */
  1004. if (!s->cutoff && !reduce_bandwidth(s, 0)) {
  1005. process_exponents(s);
  1006. ret = compute_bit_allocation(s);
  1007. continue;
  1008. }
  1009. /* fallbacks were not enough... */
  1010. break;
  1011. }
  1012. return ret;
  1013. }
  1014. /**
  1015. * Symmetric quantization on 'levels' levels.
  1016. */
  1017. static inline int sym_quant(int c, int e, int levels)
  1018. {
  1019. int v = (((levels * c) >> (24 - e)) + levels) >> 1;
  1020. av_assert2(v >= 0 && v < levels);
  1021. return v;
  1022. }
  1023. /**
  1024. * Asymmetric quantization on 2^qbits levels.
  1025. */
  1026. static inline int asym_quant(int c, int e, int qbits)
  1027. {
  1028. int lshift, m, v;
  1029. lshift = e + qbits - 24;
  1030. if (lshift >= 0)
  1031. v = c << lshift;
  1032. else
  1033. v = c >> (-lshift);
  1034. /* rounding */
  1035. v = (v + 1) >> 1;
  1036. m = (1 << (qbits-1));
  1037. if (v >= m)
  1038. v = m - 1;
  1039. av_assert2(v >= -m);
  1040. return v & ((1 << qbits)-1);
  1041. }
  1042. /**
  1043. * Quantize a set of mantissas for a single channel in a single block.
  1044. */
  1045. static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
  1046. uint8_t *exp,
  1047. uint8_t *bap, uint16_t *qmant, int n)
  1048. {
  1049. int i;
  1050. for (i = 0; i < n; i++) {
  1051. int v;
  1052. int c = fixed_coef[i];
  1053. int e = exp[i];
  1054. int b = bap[i];
  1055. switch (b) {
  1056. case 0:
  1057. v = 0;
  1058. break;
  1059. case 1:
  1060. v = sym_quant(c, e, 3);
  1061. switch (s->mant1_cnt) {
  1062. case 0:
  1063. s->qmant1_ptr = &qmant[i];
  1064. v = 9 * v;
  1065. s->mant1_cnt = 1;
  1066. break;
  1067. case 1:
  1068. *s->qmant1_ptr += 3 * v;
  1069. s->mant1_cnt = 2;
  1070. v = 128;
  1071. break;
  1072. default:
  1073. *s->qmant1_ptr += v;
  1074. s->mant1_cnt = 0;
  1075. v = 128;
  1076. break;
  1077. }
  1078. break;
  1079. case 2:
  1080. v = sym_quant(c, e, 5);
  1081. switch (s->mant2_cnt) {
  1082. case 0:
  1083. s->qmant2_ptr = &qmant[i];
  1084. v = 25 * v;
  1085. s->mant2_cnt = 1;
  1086. break;
  1087. case 1:
  1088. *s->qmant2_ptr += 5 * v;
  1089. s->mant2_cnt = 2;
  1090. v = 128;
  1091. break;
  1092. default:
  1093. *s->qmant2_ptr += v;
  1094. s->mant2_cnt = 0;
  1095. v = 128;
  1096. break;
  1097. }
  1098. break;
  1099. case 3:
  1100. v = sym_quant(c, e, 7);
  1101. break;
  1102. case 4:
  1103. v = sym_quant(c, e, 11);
  1104. switch (s->mant4_cnt) {
  1105. case 0:
  1106. s->qmant4_ptr = &qmant[i];
  1107. v = 11 * v;
  1108. s->mant4_cnt = 1;
  1109. break;
  1110. default:
  1111. *s->qmant4_ptr += v;
  1112. s->mant4_cnt = 0;
  1113. v = 128;
  1114. break;
  1115. }
  1116. break;
  1117. case 5:
  1118. v = sym_quant(c, e, 15);
  1119. break;
  1120. case 14:
  1121. v = asym_quant(c, e, 14);
  1122. break;
  1123. case 15:
  1124. v = asym_quant(c, e, 16);
  1125. break;
  1126. default:
  1127. v = asym_quant(c, e, b - 1);
  1128. break;
  1129. }
  1130. qmant[i] = v;
  1131. }
  1132. }
  1133. /**
  1134. * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
  1135. */
  1136. static void quantize_mantissas(AC3EncodeContext *s)
  1137. {
  1138. int blk, ch;
  1139. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1140. AC3Block *block = &s->blocks[blk];
  1141. AC3Block *ref_block;
  1142. AC3Mant m = { 0 };
  1143. for (ch = 0; ch < s->channels; ch++) {
  1144. ref_block = block->exp_ref_block[ch];
  1145. quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
  1146. ref_block->exp[ch], ref_block->bap[ch],
  1147. block->qmant[ch], s->nb_coefs[ch]);
  1148. }
  1149. }
  1150. }
  1151. /**
  1152. * Write the AC-3 frame header to the output bitstream.
  1153. */
  1154. static void output_frame_header(AC3EncodeContext *s)
  1155. {
  1156. AC3EncOptions *opt = &s->options;
  1157. put_bits(&s->pb, 16, 0x0b77); /* frame header */
  1158. put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
  1159. put_bits(&s->pb, 2, s->bit_alloc.sr_code);
  1160. put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
  1161. put_bits(&s->pb, 5, s->bitstream_id);
  1162. put_bits(&s->pb, 3, s->bitstream_mode);
  1163. put_bits(&s->pb, 3, s->channel_mode);
  1164. if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
  1165. put_bits(&s->pb, 2, s->center_mix_level);
  1166. if (s->channel_mode & 0x04)
  1167. put_bits(&s->pb, 2, s->surround_mix_level);
  1168. if (s->channel_mode == AC3_CHMODE_STEREO)
  1169. put_bits(&s->pb, 2, opt->dolby_surround_mode);
  1170. put_bits(&s->pb, 1, s->lfe_on); /* LFE */
  1171. put_bits(&s->pb, 5, -opt->dialogue_level);
  1172. put_bits(&s->pb, 1, 0); /* no compression control word */
  1173. put_bits(&s->pb, 1, 0); /* no lang code */
  1174. put_bits(&s->pb, 1, opt->audio_production_info);
  1175. if (opt->audio_production_info) {
  1176. put_bits(&s->pb, 5, opt->mixing_level - 80);
  1177. put_bits(&s->pb, 2, opt->room_type);
  1178. }
  1179. put_bits(&s->pb, 1, opt->copyright);
  1180. put_bits(&s->pb, 1, opt->original);
  1181. if (s->bitstream_id == 6) {
  1182. /* alternate bit stream syntax */
  1183. put_bits(&s->pb, 1, opt->extended_bsi_1);
  1184. if (opt->extended_bsi_1) {
  1185. put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
  1186. put_bits(&s->pb, 3, s->ltrt_center_mix_level);
  1187. put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
  1188. put_bits(&s->pb, 3, s->loro_center_mix_level);
  1189. put_bits(&s->pb, 3, s->loro_surround_mix_level);
  1190. }
  1191. put_bits(&s->pb, 1, opt->extended_bsi_2);
  1192. if (opt->extended_bsi_2) {
  1193. put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
  1194. put_bits(&s->pb, 2, opt->dolby_headphone_mode);
  1195. put_bits(&s->pb, 1, opt->ad_converter_type);
  1196. put_bits(&s->pb, 9, 0); /* xbsi2 and encinfo : reserved */
  1197. }
  1198. } else {
  1199. put_bits(&s->pb, 1, 0); /* no time code 1 */
  1200. put_bits(&s->pb, 1, 0); /* no time code 2 */
  1201. }
  1202. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  1203. }
  1204. /**
  1205. * Write one audio block to the output bitstream.
  1206. */
  1207. static void output_audio_block(AC3EncodeContext *s, int blk)
  1208. {
  1209. int ch, i, baie, rbnd;
  1210. AC3Block *block = &s->blocks[blk];
  1211. /* block switching */
  1212. for (ch = 0; ch < s->fbw_channels; ch++)
  1213. put_bits(&s->pb, 1, 0);
  1214. /* dither flags */
  1215. for (ch = 0; ch < s->fbw_channels; ch++)
  1216. put_bits(&s->pb, 1, 1);
  1217. /* dynamic range codes */
  1218. put_bits(&s->pb, 1, 0);
  1219. /* channel coupling */
  1220. if (!blk) {
  1221. put_bits(&s->pb, 1, 1); /* coupling strategy present */
  1222. put_bits(&s->pb, 1, 0); /* no coupling strategy */
  1223. } else {
  1224. put_bits(&s->pb, 1, 0); /* no new coupling strategy */
  1225. }
  1226. /* stereo rematrixing */
  1227. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1228. put_bits(&s->pb, 1, block->new_rematrixing_strategy);
  1229. if (block->new_rematrixing_strategy) {
  1230. /* rematrixing flags */
  1231. for (rbnd = 0; rbnd < s->num_rematrixing_bands; rbnd++)
  1232. put_bits(&s->pb, 1, block->rematrixing_flags[rbnd]);
  1233. }
  1234. }
  1235. /* exponent strategy */
  1236. for (ch = 0; ch < s->fbw_channels; ch++)
  1237. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  1238. if (s->lfe_on)
  1239. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  1240. /* bandwidth */
  1241. for (ch = 0; ch < s->fbw_channels; ch++) {
  1242. if (s->exp_strategy[ch][blk] != EXP_REUSE)
  1243. put_bits(&s->pb, 6, s->bandwidth_code[ch]);
  1244. }
  1245. /* exponents */
  1246. for (ch = 0; ch < s->channels; ch++) {
  1247. int nb_groups;
  1248. if (s->exp_strategy[ch][blk] == EXP_REUSE)
  1249. continue;
  1250. /* DC exponent */
  1251. put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
  1252. /* exponent groups */
  1253. nb_groups = exponent_group_tab[s->exp_strategy[ch][blk]-1][s->nb_coefs[ch]];
  1254. for (i = 1; i <= nb_groups; i++)
  1255. put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
  1256. /* gain range info */
  1257. if (ch != s->lfe_channel)
  1258. put_bits(&s->pb, 2, 0);
  1259. }
  1260. /* bit allocation info */
  1261. baie = (blk == 0);
  1262. put_bits(&s->pb, 1, baie);
  1263. if (baie) {
  1264. put_bits(&s->pb, 2, s->slow_decay_code);
  1265. put_bits(&s->pb, 2, s->fast_decay_code);
  1266. put_bits(&s->pb, 2, s->slow_gain_code);
  1267. put_bits(&s->pb, 2, s->db_per_bit_code);
  1268. put_bits(&s->pb, 3, s->floor_code);
  1269. }
  1270. /* snr offset */
  1271. put_bits(&s->pb, 1, baie);
  1272. if (baie) {
  1273. put_bits(&s->pb, 6, s->coarse_snr_offset);
  1274. for (ch = 0; ch < s->channels; ch++) {
  1275. put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
  1276. put_bits(&s->pb, 3, s->fast_gain_code[ch]);
  1277. }
  1278. }
  1279. put_bits(&s->pb, 1, 0); /* no delta bit allocation */
  1280. put_bits(&s->pb, 1, 0); /* no data to skip */
  1281. /* mantissas */
  1282. for (ch = 0; ch < s->channels; ch++) {
  1283. int b, q;
  1284. AC3Block *ref_block = block->exp_ref_block[ch];
  1285. for (i = 0; i < s->nb_coefs[ch]; i++) {
  1286. q = block->qmant[ch][i];
  1287. b = ref_block->bap[ch][i];
  1288. switch (b) {
  1289. case 0: break;
  1290. case 1: if (q != 128) put_bits(&s->pb, 5, q); break;
  1291. case 2: if (q != 128) put_bits(&s->pb, 7, q); break;
  1292. case 3: put_bits(&s->pb, 3, q); break;
  1293. case 4: if (q != 128) put_bits(&s->pb, 7, q); break;
  1294. case 14: put_bits(&s->pb, 14, q); break;
  1295. case 15: put_bits(&s->pb, 16, q); break;
  1296. default: put_bits(&s->pb, b-1, q); break;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. /** CRC-16 Polynomial */
  1302. #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
  1303. static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
  1304. {
  1305. unsigned int c;
  1306. c = 0;
  1307. while (a) {
  1308. if (a & 1)
  1309. c ^= b;
  1310. a = a >> 1;
  1311. b = b << 1;
  1312. if (b & (1 << 16))
  1313. b ^= poly;
  1314. }
  1315. return c;
  1316. }
  1317. static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
  1318. {
  1319. unsigned int r;
  1320. r = 1;
  1321. while (n) {
  1322. if (n & 1)
  1323. r = mul_poly(r, a, poly);
  1324. a = mul_poly(a, a, poly);
  1325. n >>= 1;
  1326. }
  1327. return r;
  1328. }
  1329. /**
  1330. * Fill the end of the frame with 0's and compute the two CRCs.
  1331. */
  1332. static void output_frame_end(AC3EncodeContext *s)
  1333. {
  1334. const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
  1335. int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
  1336. uint8_t *frame;
  1337. frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
  1338. /* pad the remainder of the frame with zeros */
  1339. av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18);
  1340. flush_put_bits(&s->pb);
  1341. frame = s->pb.buf;
  1342. pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
  1343. av_assert2(pad_bytes >= 0);
  1344. if (pad_bytes > 0)
  1345. memset(put_bits_ptr(&s->pb), 0, pad_bytes);
  1346. /* compute crc1 */
  1347. /* this is not so easy because it is at the beginning of the data... */
  1348. crc1 = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
  1349. crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
  1350. crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
  1351. AV_WB16(frame + 2, crc1);
  1352. /* compute crc2 */
  1353. crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
  1354. s->frame_size - frame_size_58 - 3);
  1355. crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
  1356. /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
  1357. if (crc2 == 0x770B) {
  1358. frame[s->frame_size - 3] ^= 0x1;
  1359. crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
  1360. }
  1361. crc2 = av_bswap16(crc2);
  1362. AV_WB16(frame + s->frame_size - 2, crc2);
  1363. }
  1364. /**
  1365. * Write the frame to the output bitstream.
  1366. */
  1367. static void output_frame(AC3EncodeContext *s, unsigned char *frame)
  1368. {
  1369. int blk;
  1370. init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
  1371. output_frame_header(s);
  1372. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  1373. output_audio_block(s, blk);
  1374. output_frame_end(s);
  1375. }
  1376. static void dprint_options(AVCodecContext *avctx)
  1377. {
  1378. #ifdef DEBUG
  1379. AC3EncodeContext *s = avctx->priv_data;
  1380. AC3EncOptions *opt = &s->options;
  1381. char strbuf[32];
  1382. switch (s->bitstream_id) {
  1383. case 6: strncpy(strbuf, "AC-3 (alt syntax)", 32); break;
  1384. case 8: strncpy(strbuf, "AC-3 (standard)", 32); break;
  1385. case 9: strncpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
  1386. case 10: strncpy(strbuf, "AC-3 (dnet quater-rate", 32); break;
  1387. default: snprintf(strbuf, 32, "ERROR");
  1388. }
  1389. av_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
  1390. av_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
  1391. av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
  1392. av_dlog(avctx, "channel_layout: %s\n", strbuf);
  1393. av_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
  1394. av_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
  1395. if (s->cutoff)
  1396. av_dlog(avctx, "cutoff: %d\n", s->cutoff);
  1397. av_dlog(avctx, "per_frame_metadata: %s\n",
  1398. opt->allow_per_frame_metadata?"on":"off");
  1399. if (s->has_center)
  1400. av_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
  1401. s->center_mix_level);
  1402. else
  1403. av_dlog(avctx, "center_mixlev: {not written}\n");
  1404. if (s->has_surround)
  1405. av_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
  1406. s->surround_mix_level);
  1407. else
  1408. av_dlog(avctx, "surround_mixlev: {not written}\n");
  1409. if (opt->audio_production_info) {
  1410. av_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
  1411. switch (opt->room_type) {
  1412. case 0: strncpy(strbuf, "notindicated", 32); break;
  1413. case 1: strncpy(strbuf, "large", 32); break;
  1414. case 2: strncpy(strbuf, "small", 32); break;
  1415. default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
  1416. }
  1417. av_dlog(avctx, "room_type: %s\n", strbuf);
  1418. } else {
  1419. av_dlog(avctx, "mixing_level: {not written}\n");
  1420. av_dlog(avctx, "room_type: {not written}\n");
  1421. }
  1422. av_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
  1423. av_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
  1424. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1425. switch (opt->dolby_surround_mode) {
  1426. case 0: strncpy(strbuf, "notindicated", 32); break;
  1427. case 1: strncpy(strbuf, "on", 32); break;
  1428. case 2: strncpy(strbuf, "off", 32); break;
  1429. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
  1430. }
  1431. av_dlog(avctx, "dsur_mode: %s\n", strbuf);
  1432. } else {
  1433. av_dlog(avctx, "dsur_mode: {not written}\n");
  1434. }
  1435. av_dlog(avctx, "original: %s\n", opt->original?"on":"off");
  1436. if (s->bitstream_id == 6) {
  1437. if (opt->extended_bsi_1) {
  1438. switch (opt->preferred_stereo_downmix) {
  1439. case 0: strncpy(strbuf, "notindicated", 32); break;
  1440. case 1: strncpy(strbuf, "ltrt", 32); break;
  1441. case 2: strncpy(strbuf, "loro", 32); break;
  1442. default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
  1443. }
  1444. av_dlog(avctx, "dmix_mode: %s\n", strbuf);
  1445. av_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
  1446. opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
  1447. av_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
  1448. opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
  1449. av_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
  1450. opt->loro_center_mix_level, s->loro_center_mix_level);
  1451. av_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
  1452. opt->loro_surround_mix_level, s->loro_surround_mix_level);
  1453. } else {
  1454. av_dlog(avctx, "extended bitstream info 1: {not written}\n");
  1455. }
  1456. if (opt->extended_bsi_2) {
  1457. switch (opt->dolby_surround_ex_mode) {
  1458. case 0: strncpy(strbuf, "notindicated", 32); break;
  1459. case 1: strncpy(strbuf, "on", 32); break;
  1460. case 2: strncpy(strbuf, "off", 32); break;
  1461. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
  1462. }
  1463. av_dlog(avctx, "dsurex_mode: %s\n", strbuf);
  1464. switch (opt->dolby_headphone_mode) {
  1465. case 0: strncpy(strbuf, "notindicated", 32); break;
  1466. case 1: strncpy(strbuf, "on", 32); break;
  1467. case 2: strncpy(strbuf, "off", 32); break;
  1468. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
  1469. }
  1470. av_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
  1471. switch (opt->ad_converter_type) {
  1472. case 0: strncpy(strbuf, "standard", 32); break;
  1473. case 1: strncpy(strbuf, "hdcd", 32); break;
  1474. default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
  1475. }
  1476. av_dlog(avctx, "ad_conv_type: %s\n", strbuf);
  1477. } else {
  1478. av_dlog(avctx, "extended bitstream info 2: {not written}\n");
  1479. }
  1480. }
  1481. #endif
  1482. }
  1483. #define FLT_OPTION_THRESHOLD 0.01
  1484. static int validate_float_option(float v, const float *v_list, int v_list_size)
  1485. {
  1486. int i;
  1487. for (i = 0; i < v_list_size; i++) {
  1488. if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
  1489. v > (v_list[i] - FLT_OPTION_THRESHOLD))
  1490. break;
  1491. }
  1492. if (i == v_list_size)
  1493. return -1;
  1494. return i;
  1495. }
  1496. static void validate_mix_level(void *log_ctx, const char *opt_name,
  1497. float *opt_param, const float *list,
  1498. int list_size, int default_value, int min_value,
  1499. int *ctx_param)
  1500. {
  1501. int mixlev = validate_float_option(*opt_param, list, list_size);
  1502. if (mixlev < min_value) {
  1503. mixlev = default_value;
  1504. if (*opt_param >= 0.0) {
  1505. av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
  1506. "default value: %0.3f\n", opt_name, list[mixlev]);
  1507. }
  1508. }
  1509. *opt_param = list[mixlev];
  1510. *ctx_param = mixlev;
  1511. }
  1512. /**
  1513. * Validate metadata options as set by AVOption system.
  1514. * These values can optionally be changed per-frame.
  1515. */
  1516. static int validate_metadata(AVCodecContext *avctx)
  1517. {
  1518. AC3EncodeContext *s = avctx->priv_data;
  1519. AC3EncOptions *opt = &s->options;
  1520. /* validate mixing levels */
  1521. if (s->has_center) {
  1522. validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
  1523. cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
  1524. &s->center_mix_level);
  1525. }
  1526. if (s->has_surround) {
  1527. validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
  1528. surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
  1529. &s->surround_mix_level);
  1530. }
  1531. /* set audio production info flag */
  1532. if (opt->mixing_level >= 0 || opt->room_type >= 0) {
  1533. if (opt->mixing_level < 0) {
  1534. av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
  1535. "room_type is set\n");
  1536. return AVERROR(EINVAL);
  1537. }
  1538. if (opt->mixing_level < 80) {
  1539. av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
  1540. "80dB and 111dB\n");
  1541. return AVERROR(EINVAL);
  1542. }
  1543. /* default room type */
  1544. if (opt->room_type < 0)
  1545. opt->room_type = 0;
  1546. opt->audio_production_info = 1;
  1547. } else {
  1548. opt->audio_production_info = 0;
  1549. }
  1550. /* set extended bsi 1 flag */
  1551. if ((s->has_center || s->has_surround) &&
  1552. (opt->preferred_stereo_downmix >= 0 ||
  1553. opt->ltrt_center_mix_level >= 0 ||
  1554. opt->ltrt_surround_mix_level >= 0 ||
  1555. opt->loro_center_mix_level >= 0 ||
  1556. opt->loro_surround_mix_level >= 0)) {
  1557. /* default preferred stereo downmix */
  1558. if (opt->preferred_stereo_downmix < 0)
  1559. opt->preferred_stereo_downmix = 0;
  1560. /* validate Lt/Rt center mix level */
  1561. validate_mix_level(avctx, "ltrt_center_mix_level",
  1562. &opt->ltrt_center_mix_level, extmixlev_options,
  1563. EXTMIXLEV_NUM_OPTIONS, 5, 0,
  1564. &s->ltrt_center_mix_level);
  1565. /* validate Lt/Rt surround mix level */
  1566. validate_mix_level(avctx, "ltrt_surround_mix_level",
  1567. &opt->ltrt_surround_mix_level, extmixlev_options,
  1568. EXTMIXLEV_NUM_OPTIONS, 6, 3,
  1569. &s->ltrt_surround_mix_level);
  1570. /* validate Lo/Ro center mix level */
  1571. validate_mix_level(avctx, "loro_center_mix_level",
  1572. &opt->loro_center_mix_level, extmixlev_options,
  1573. EXTMIXLEV_NUM_OPTIONS, 5, 0,
  1574. &s->loro_center_mix_level);
  1575. /* validate Lo/Ro surround mix level */
  1576. validate_mix_level(avctx, "loro_surround_mix_level",
  1577. &opt->loro_surround_mix_level, extmixlev_options,
  1578. EXTMIXLEV_NUM_OPTIONS, 6, 3,
  1579. &s->loro_surround_mix_level);
  1580. opt->extended_bsi_1 = 1;
  1581. } else {
  1582. opt->extended_bsi_1 = 0;
  1583. }
  1584. /* set extended bsi 2 flag */
  1585. if (opt->dolby_surround_ex_mode >= 0 ||
  1586. opt->dolby_headphone_mode >= 0 ||
  1587. opt->ad_converter_type >= 0) {
  1588. /* default dolby surround ex mode */
  1589. if (opt->dolby_surround_ex_mode < 0)
  1590. opt->dolby_surround_ex_mode = 0;
  1591. /* default dolby headphone mode */
  1592. if (opt->dolby_headphone_mode < 0)
  1593. opt->dolby_headphone_mode = 0;
  1594. /* default A/D converter type */
  1595. if (opt->ad_converter_type < 0)
  1596. opt->ad_converter_type = 0;
  1597. opt->extended_bsi_2 = 1;
  1598. } else {
  1599. opt->extended_bsi_2 = 0;
  1600. }
  1601. /* set bitstream id for alternate bitstream syntax */
  1602. if (opt->extended_bsi_1 || opt->extended_bsi_2) {
  1603. if (s->bitstream_id > 8 && s->bitstream_id < 11) {
  1604. static int warn_once = 1;
  1605. if (warn_once) {
  1606. av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
  1607. "not compatible with reduced samplerates. writing of "
  1608. "extended bitstream information will be disabled.\n");
  1609. warn_once = 0;
  1610. }
  1611. } else {
  1612. s->bitstream_id = 6;
  1613. }
  1614. }
  1615. return 0;
  1616. }
  1617. /**
  1618. * Encode a single AC-3 frame.
  1619. */
  1620. static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
  1621. int buf_size, void *data)
  1622. {
  1623. AC3EncodeContext *s = avctx->priv_data;
  1624. const SampleType *samples = data;
  1625. int ret;
  1626. if (s->options.allow_per_frame_metadata) {
  1627. ret = validate_metadata(avctx);
  1628. if (ret)
  1629. return ret;
  1630. }
  1631. if (s->bit_alloc.sr_code == 1)
  1632. adjust_frame_size(s);
  1633. deinterleave_input_samples(s, samples);
  1634. apply_mdct(s);
  1635. scale_coefficients(s);
  1636. compute_rematrixing_strategy(s);
  1637. apply_rematrixing(s);
  1638. process_exponents(s);
  1639. ret = compute_bit_allocation(s);
  1640. if (ret) {
  1641. av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
  1642. return ret;
  1643. }
  1644. quantize_mantissas(s);
  1645. output_frame(s, frame);
  1646. return s->frame_size;
  1647. }
  1648. /**
  1649. * Finalize encoding and free any memory allocated by the encoder.
  1650. */
  1651. static av_cold int ac3_encode_close(AVCodecContext *avctx)
  1652. {
  1653. int blk, ch;
  1654. AC3EncodeContext *s = avctx->priv_data;
  1655. for (ch = 0; ch < s->channels; ch++)
  1656. av_freep(&s->planar_samples[ch]);
  1657. av_freep(&s->planar_samples);
  1658. av_freep(&s->bap_buffer);
  1659. av_freep(&s->bap1_buffer);
  1660. av_freep(&s->mdct_coef_buffer);
  1661. av_freep(&s->fixed_coef_buffer);
  1662. av_freep(&s->exp_buffer);
  1663. av_freep(&s->grouped_exp_buffer);
  1664. av_freep(&s->psd_buffer);
  1665. av_freep(&s->band_psd_buffer);
  1666. av_freep(&s->mask_buffer);
  1667. av_freep(&s->qmant_buffer);
  1668. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1669. AC3Block *block = &s->blocks[blk];
  1670. av_freep(&block->bap);
  1671. av_freep(&block->mdct_coef);
  1672. av_freep(&block->fixed_coef);
  1673. av_freep(&block->exp);
  1674. av_freep(&block->grouped_exp);
  1675. av_freep(&block->psd);
  1676. av_freep(&block->band_psd);
  1677. av_freep(&block->mask);
  1678. av_freep(&block->qmant);
  1679. }
  1680. mdct_end(&s->mdct);
  1681. av_freep(&avctx->coded_frame);
  1682. return 0;
  1683. }
  1684. /**
  1685. * Set channel information during initialization.
  1686. */
  1687. static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
  1688. int64_t *channel_layout)
  1689. {
  1690. int ch_layout;
  1691. if (channels < 1 || channels > AC3_MAX_CHANNELS)
  1692. return AVERROR(EINVAL);
  1693. if ((uint64_t)*channel_layout > 0x7FF)
  1694. return AVERROR(EINVAL);
  1695. ch_layout = *channel_layout;
  1696. if (!ch_layout)
  1697. ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
  1698. if (av_get_channel_layout_nb_channels(ch_layout) != channels)
  1699. return AVERROR(EINVAL);
  1700. s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);
  1701. s->channels = channels;
  1702. s->fbw_channels = channels - s->lfe_on;
  1703. s->lfe_channel = s->lfe_on ? s->fbw_channels : -1;
  1704. if (s->lfe_on)
  1705. ch_layout -= AV_CH_LOW_FREQUENCY;
  1706. switch (ch_layout) {
  1707. case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
  1708. case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
  1709. case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
  1710. case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
  1711. case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
  1712. case AV_CH_LAYOUT_QUAD:
  1713. case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
  1714. case AV_CH_LAYOUT_5POINT0:
  1715. case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
  1716. default:
  1717. return AVERROR(EINVAL);
  1718. }
  1719. s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
  1720. s->has_surround = s->channel_mode & 0x04;
  1721. s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
  1722. *channel_layout = ch_layout;
  1723. if (s->lfe_on)
  1724. *channel_layout |= AV_CH_LOW_FREQUENCY;
  1725. return 0;
  1726. }
  1727. static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
  1728. {
  1729. int i, ret;
  1730. /* validate channel layout */
  1731. if (!avctx->channel_layout) {
  1732. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  1733. "encoder will guess the layout, but it "
  1734. "might be incorrect.\n");
  1735. }
  1736. ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
  1737. if (ret) {
  1738. av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
  1739. return ret;
  1740. }
  1741. /* validate sample rate */
  1742. for (i = 0; i < 9; i++) {
  1743. if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
  1744. break;
  1745. }
  1746. if (i == 9) {
  1747. av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
  1748. return AVERROR(EINVAL);
  1749. }
  1750. s->sample_rate = avctx->sample_rate;
  1751. s->bit_alloc.sr_shift = i % 3;
  1752. s->bit_alloc.sr_code = i / 3;
  1753. s->bitstream_id = 8 + s->bit_alloc.sr_shift;
  1754. /* validate bit rate */
  1755. for (i = 0; i < 19; i++) {
  1756. if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
  1757. break;
  1758. }
  1759. if (i == 19) {
  1760. av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
  1761. return AVERROR(EINVAL);
  1762. }
  1763. s->bit_rate = avctx->bit_rate;
  1764. s->frame_size_code = i << 1;
  1765. /* validate cutoff */
  1766. if (avctx->cutoff < 0) {
  1767. av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
  1768. return AVERROR(EINVAL);
  1769. }
  1770. s->cutoff = avctx->cutoff;
  1771. if (s->cutoff > (s->sample_rate >> 1))
  1772. s->cutoff = s->sample_rate >> 1;
  1773. /* validate audio service type / channels combination */
  1774. if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&
  1775. avctx->channels == 1) ||
  1776. ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||
  1777. avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||
  1778. avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)
  1779. && avctx->channels > 1)) {
  1780. av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
  1781. "specified number of channels\n");
  1782. return AVERROR(EINVAL);
  1783. }
  1784. ret = validate_metadata(avctx);
  1785. if (ret)
  1786. return ret;
  1787. return 0;
  1788. }
  1789. /**
  1790. * Set bandwidth for all channels.
  1791. * The user can optionally supply a cutoff frequency. Otherwise an appropriate
  1792. * default value will be used.
  1793. */
  1794. static av_cold void set_bandwidth(AC3EncodeContext *s)
  1795. {
  1796. int ch, bw_code;
  1797. if (s->cutoff) {
  1798. /* calculate bandwidth based on user-specified cutoff frequency */
  1799. int fbw_coeffs;
  1800. fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
  1801. bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
  1802. } else {
  1803. /* use default bandwidth setting */
  1804. /* XXX: should compute the bandwidth according to the frame
  1805. size, so that we avoid annoying high frequency artifacts */
  1806. bw_code = 50;
  1807. }
  1808. /* set number of coefficients for each channel */
  1809. for (ch = 0; ch < s->fbw_channels; ch++) {
  1810. s->bandwidth_code[ch] = bw_code;
  1811. s->nb_coefs[ch] = bw_code * 3 + 73;
  1812. }
  1813. if (s->lfe_on)
  1814. s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
  1815. }
  1816. static av_cold int allocate_buffers(AVCodecContext *avctx)
  1817. {
  1818. int blk, ch;
  1819. AC3EncodeContext *s = avctx->priv_data;
  1820. FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
  1821. alloc_fail);
  1822. for (ch = 0; ch < s->channels; ch++) {
  1823. FF_ALLOCZ_OR_GOTO(avctx, s->planar_samples[ch],
  1824. (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
  1825. alloc_fail);
  1826. }
  1827. FF_ALLOC_OR_GOTO(avctx, s->bap_buffer, AC3_MAX_BLOCKS * s->channels *
  1828. AC3_MAX_COEFS * sizeof(*s->bap_buffer), alloc_fail);
  1829. FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
  1830. AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
  1831. FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
  1832. AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
  1833. FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
  1834. AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
  1835. FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
  1836. 128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
  1837. FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
  1838. AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
  1839. FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
  1840. 64 * sizeof(*s->band_psd_buffer), alloc_fail);
  1841. FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
  1842. 64 * sizeof(*s->mask_buffer), alloc_fail);
  1843. FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
  1844. AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
  1845. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1846. AC3Block *block = &s->blocks[blk];
  1847. FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
  1848. alloc_fail);
  1849. FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
  1850. alloc_fail);
  1851. FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
  1852. alloc_fail);
  1853. FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
  1854. alloc_fail);
  1855. FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
  1856. alloc_fail);
  1857. FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
  1858. alloc_fail);
  1859. FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
  1860. alloc_fail);
  1861. FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
  1862. alloc_fail);
  1863. for (ch = 0; ch < s->channels; ch++) {
  1864. /* arrangement: block, channel, coeff */
  1865. block->bap[ch] = &s->bap_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1866. block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1867. block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * s->channels + ch)];
  1868. block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1869. block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * s->channels + ch)];
  1870. block->mask[ch] = &s->mask_buffer [64 * (blk * s->channels + ch)];
  1871. block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1872. /* arrangement: channel, block, coeff */
  1873. block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
  1874. }
  1875. }
  1876. if (CONFIG_AC3ENC_FLOAT) {
  1877. FF_ALLOC_OR_GOTO(avctx, s->fixed_coef_buffer, AC3_MAX_BLOCKS * s->channels *
  1878. AC3_MAX_COEFS * sizeof(*s->fixed_coef_buffer), alloc_fail);
  1879. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1880. AC3Block *block = &s->blocks[blk];
  1881. FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
  1882. sizeof(*block->fixed_coef), alloc_fail);
  1883. for (ch = 0; ch < s->channels; ch++)
  1884. block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
  1885. }
  1886. } else {
  1887. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1888. AC3Block *block = &s->blocks[blk];
  1889. FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
  1890. sizeof(*block->fixed_coef), alloc_fail);
  1891. for (ch = 0; ch < s->channels; ch++)
  1892. block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
  1893. }
  1894. }
  1895. return 0;
  1896. alloc_fail:
  1897. return AVERROR(ENOMEM);
  1898. }
  1899. /**
  1900. * Initialize the encoder.
  1901. */
  1902. static av_cold int ac3_encode_init(AVCodecContext *avctx)
  1903. {
  1904. AC3EncodeContext *s = avctx->priv_data;
  1905. int ret, frame_size_58;
  1906. avctx->frame_size = AC3_FRAME_SIZE;
  1907. ff_ac3_common_init();
  1908. ret = validate_options(avctx, s);
  1909. if (ret)
  1910. return ret;
  1911. s->bitstream_mode = avctx->audio_service_type;
  1912. if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
  1913. s->bitstream_mode = 0x7;
  1914. s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
  1915. s->bits_written = 0;
  1916. s->samples_written = 0;
  1917. s->frame_size = s->frame_size_min;
  1918. /* calculate crc_inv for both possible frame sizes */
  1919. frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
  1920. s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
  1921. if (s->bit_alloc.sr_code == 1) {
  1922. frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
  1923. s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
  1924. }
  1925. set_bandwidth(s);
  1926. rematrixing_init(s);
  1927. exponent_init(s);
  1928. bit_alloc_init(s);
  1929. ret = mdct_init(avctx, &s->mdct, 9);
  1930. if (ret)
  1931. goto init_fail;
  1932. ret = allocate_buffers(avctx);
  1933. if (ret)
  1934. goto init_fail;
  1935. avctx->coded_frame= avcodec_alloc_frame();
  1936. dsputil_init(&s->dsp, avctx);
  1937. ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
  1938. dprint_options(avctx);
  1939. return 0;
  1940. init_fail:
  1941. ac3_encode_close(avctx);
  1942. return ret;
  1943. }