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.

2261 lines
76KB

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