/libavcodec/ac3enc.c

http://github.com/FFmpeg/FFmpeg · C · 2495 lines · 1792 code · 307 blank · 396 comment · 518 complexity · 691dd5f7fb64670d0ef49c8127af5a86 MD5 · raw file

Large files are truncated click here to view the full file

  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. #include <stdint.h>
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/opt.h"
  35. #include "avcodec.h"
  36. #include "internal.h"
  37. #include "me_cmp.h"
  38. #include "put_bits.h"
  39. #include "audiodsp.h"
  40. #include "ac3dsp.h"
  41. #include "ac3.h"
  42. #include "fft.h"
  43. #include "ac3enc.h"
  44. #include "eac3enc.h"
  45. typedef struct AC3Mant {
  46. int16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
  47. int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4
  48. } AC3Mant;
  49. #define CMIXLEV_NUM_OPTIONS 3
  50. static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
  51. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
  52. };
  53. #define SURMIXLEV_NUM_OPTIONS 3
  54. static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
  55. LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  56. };
  57. #define EXTMIXLEV_NUM_OPTIONS 8
  58. static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
  59. LEVEL_PLUS_3DB, LEVEL_PLUS_1POINT5DB, LEVEL_ONE, LEVEL_MINUS_1POINT5DB,
  60. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  61. };
  62. /**
  63. * LUT for number of exponent groups.
  64. * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
  65. */
  66. static uint8_t exponent_group_tab[2][3][256];
  67. /**
  68. * List of supported channel layouts.
  69. */
  70. const uint64_t ff_ac3_channel_layouts[19] = {
  71. AV_CH_LAYOUT_MONO,
  72. AV_CH_LAYOUT_STEREO,
  73. AV_CH_LAYOUT_2_1,
  74. AV_CH_LAYOUT_SURROUND,
  75. AV_CH_LAYOUT_2_2,
  76. AV_CH_LAYOUT_QUAD,
  77. AV_CH_LAYOUT_4POINT0,
  78. AV_CH_LAYOUT_5POINT0,
  79. AV_CH_LAYOUT_5POINT0_BACK,
  80. (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY),
  81. (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY),
  82. (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY),
  83. (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
  84. (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY),
  85. (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY),
  86. (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY),
  87. AV_CH_LAYOUT_5POINT1,
  88. AV_CH_LAYOUT_5POINT1_BACK,
  89. 0
  90. };
  91. /**
  92. * LUT to select the bandwidth code based on the bit rate, sample rate, and
  93. * number of full-bandwidth channels.
  94. * bandwidth_tab[fbw_channels-1][sample rate code][bit rate code]
  95. */
  96. static const uint8_t ac3_bandwidth_tab[5][3][19] = {
  97. // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
  98. { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
  99. { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
  100. { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
  101. { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
  102. { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
  103. { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
  104. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
  105. { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
  106. { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
  107. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
  108. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
  109. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
  110. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
  111. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
  112. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
  113. };
  114. /**
  115. * LUT to select the coupling start band based on the bit rate, sample rate, and
  116. * number of full-bandwidth channels. -1 = coupling off
  117. * ac3_coupling_start_tab[channel_mode-2][sample rate code][bit rate code]
  118. *
  119. * TODO: more testing for optimal parameters.
  120. * multi-channel tests at 44.1kHz and 32kHz.
  121. */
  122. static const int8_t ac3_coupling_start_tab[6][3][19] = {
  123. // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
  124. // 2/0
  125. { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
  126. { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
  127. { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  128. // 3/0
  129. { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  130. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  131. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  132. // 2/1 - untested
  133. { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  134. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  135. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  136. // 3/1
  137. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  138. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  139. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  140. // 2/2 - untested
  141. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  142. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  143. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  144. // 3/2
  145. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
  146. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
  147. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  148. };
  149. /**
  150. * Adjust the frame size to make the average bit rate match the target bit rate.
  151. * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
  152. *
  153. * @param s AC-3 encoder private context
  154. */
  155. void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
  156. {
  157. while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
  158. s->bits_written -= s->bit_rate;
  159. s->samples_written -= s->sample_rate;
  160. }
  161. s->frame_size = s->frame_size_min +
  162. 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
  163. s->bits_written += s->frame_size * 8;
  164. s->samples_written += AC3_BLOCK_SIZE * s->num_blocks;
  165. }
  166. /**
  167. * Set the initial coupling strategy parameters prior to coupling analysis.
  168. *
  169. * @param s AC-3 encoder private context
  170. */
  171. void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
  172. {
  173. int blk, ch;
  174. int got_cpl_snr;
  175. int num_cpl_blocks;
  176. /* set coupling use flags for each block/channel */
  177. /* TODO: turn coupling on/off and adjust start band based on bit usage */
  178. for (blk = 0; blk < s->num_blocks; blk++) {
  179. AC3Block *block = &s->blocks[blk];
  180. for (ch = 1; ch <= s->fbw_channels; ch++)
  181. block->channel_in_cpl[ch] = s->cpl_on;
  182. }
  183. /* enable coupling for each block if at least 2 channels have coupling
  184. enabled for that block */
  185. got_cpl_snr = 0;
  186. num_cpl_blocks = 0;
  187. for (blk = 0; blk < s->num_blocks; blk++) {
  188. AC3Block *block = &s->blocks[blk];
  189. block->num_cpl_channels = 0;
  190. for (ch = 1; ch <= s->fbw_channels; ch++)
  191. block->num_cpl_channels += block->channel_in_cpl[ch];
  192. block->cpl_in_use = block->num_cpl_channels > 1;
  193. num_cpl_blocks += block->cpl_in_use;
  194. if (!block->cpl_in_use) {
  195. block->num_cpl_channels = 0;
  196. for (ch = 1; ch <= s->fbw_channels; ch++)
  197. block->channel_in_cpl[ch] = 0;
  198. }
  199. block->new_cpl_strategy = !blk;
  200. if (blk) {
  201. for (ch = 1; ch <= s->fbw_channels; ch++) {
  202. if (block->channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
  203. block->new_cpl_strategy = 1;
  204. break;
  205. }
  206. }
  207. }
  208. block->new_cpl_leak = block->new_cpl_strategy;
  209. if (!blk || (block->cpl_in_use && !got_cpl_snr)) {
  210. block->new_snr_offsets = 1;
  211. if (block->cpl_in_use)
  212. got_cpl_snr = 1;
  213. } else {
  214. block->new_snr_offsets = 0;
  215. }
  216. }
  217. if (!num_cpl_blocks)
  218. s->cpl_on = 0;
  219. /* set bandwidth for each channel */
  220. for (blk = 0; blk < s->num_blocks; blk++) {
  221. AC3Block *block = &s->blocks[blk];
  222. for (ch = 1; ch <= s->fbw_channels; ch++) {
  223. if (block->channel_in_cpl[ch])
  224. block->end_freq[ch] = s->start_freq[CPL_CH];
  225. else
  226. block->end_freq[ch] = s->bandwidth_code * 3 + 73;
  227. }
  228. }
  229. }
  230. /**
  231. * Apply stereo rematrixing to coefficients based on rematrixing flags.
  232. *
  233. * @param s AC-3 encoder private context
  234. */
  235. void ff_ac3_apply_rematrixing(AC3EncodeContext *s)
  236. {
  237. int nb_coefs;
  238. int blk, bnd, i;
  239. int start, end;
  240. uint8_t *flags = NULL;
  241. if (!s->rematrixing_enabled)
  242. return;
  243. for (blk = 0; blk < s->num_blocks; blk++) {
  244. AC3Block *block = &s->blocks[blk];
  245. if (block->new_rematrixing_strategy)
  246. flags = block->rematrixing_flags;
  247. nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
  248. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
  249. if (flags[bnd]) {
  250. start = ff_ac3_rematrix_band_tab[bnd];
  251. end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  252. for (i = start; i < end; i++) {
  253. int32_t lt = block->fixed_coef[1][i];
  254. int32_t rt = block->fixed_coef[2][i];
  255. block->fixed_coef[1][i] = (lt + rt) >> 1;
  256. block->fixed_coef[2][i] = (lt - rt) >> 1;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /*
  263. * Initialize exponent tables.
  264. */
  265. static av_cold void exponent_init(AC3EncodeContext *s)
  266. {
  267. int expstr, i, grpsize;
  268. for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
  269. grpsize = 3 << expstr;
  270. for (i = 12; i < 256; i++) {
  271. exponent_group_tab[0][expstr][i] = (i + grpsize - 4) / grpsize;
  272. exponent_group_tab[1][expstr][i] = (i ) / grpsize;
  273. }
  274. }
  275. /* LFE */
  276. exponent_group_tab[0][0][7] = 2;
  277. if (CONFIG_EAC3_ENCODER && s->eac3)
  278. ff_eac3_exponent_init();
  279. }
  280. /*
  281. * Extract exponents from the MDCT coefficients.
  282. */
  283. static void extract_exponents(AC3EncodeContext *s)
  284. {
  285. int ch = !s->cpl_on;
  286. int chan_size = AC3_MAX_COEFS * s->num_blocks * (s->channels - ch + 1);
  287. AC3Block *block = &s->blocks[0];
  288. s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch], chan_size);
  289. }
  290. /**
  291. * Exponent Difference Threshold.
  292. * New exponents are sent if their SAD exceed this number.
  293. */
  294. #define EXP_DIFF_THRESHOLD 500
  295. /**
  296. * Table used to select exponent strategy based on exponent reuse block interval.
  297. */
  298. static const uint8_t exp_strategy_reuse_tab[4][6] = {
  299. { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  300. { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  301. { EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  302. { EXP_D45, EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15 }
  303. };
  304. /*
  305. * Calculate exponent strategies for all channels.
  306. * Array arrangement is reversed to simplify the per-channel calculation.
  307. */
  308. static void compute_exp_strategy(AC3EncodeContext *s)
  309. {
  310. int ch, blk, blk1;
  311. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
  312. uint8_t *exp_strategy = s->exp_strategy[ch];
  313. uint8_t *exp = s->blocks[0].exp[ch];
  314. int exp_diff;
  315. /* estimate if the exponent variation & decide if they should be
  316. reused in the next frame */
  317. exp_strategy[0] = EXP_NEW;
  318. exp += AC3_MAX_COEFS;
  319. for (blk = 1; blk < s->num_blocks; blk++, exp += AC3_MAX_COEFS) {
  320. if (ch == CPL_CH) {
  321. if (!s->blocks[blk-1].cpl_in_use) {
  322. exp_strategy[blk] = EXP_NEW;
  323. continue;
  324. } else if (!s->blocks[blk].cpl_in_use) {
  325. exp_strategy[blk] = EXP_REUSE;
  326. continue;
  327. }
  328. } else if (s->blocks[blk].channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
  329. exp_strategy[blk] = EXP_NEW;
  330. continue;
  331. }
  332. exp_diff = s->mecc.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
  333. exp_strategy[blk] = EXP_REUSE;
  334. if (ch == CPL_CH && exp_diff > (EXP_DIFF_THRESHOLD * (s->blocks[blk].end_freq[ch] - s->start_freq[ch]) / AC3_MAX_COEFS))
  335. exp_strategy[blk] = EXP_NEW;
  336. else if (ch > CPL_CH && exp_diff > EXP_DIFF_THRESHOLD)
  337. exp_strategy[blk] = EXP_NEW;
  338. }
  339. /* now select the encoding strategy type : if exponents are often
  340. recoded, we use a coarse encoding */
  341. blk = 0;
  342. while (blk < s->num_blocks) {
  343. blk1 = blk + 1;
  344. while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE)
  345. blk1++;
  346. exp_strategy[blk] = exp_strategy_reuse_tab[s->num_blks_code][blk1-blk-1];
  347. blk = blk1;
  348. }
  349. }
  350. if (s->lfe_on) {
  351. ch = s->lfe_channel;
  352. s->exp_strategy[ch][0] = EXP_D15;
  353. for (blk = 1; blk < s->num_blocks; blk++)
  354. s->exp_strategy[ch][blk] = EXP_REUSE;
  355. }
  356. /* for E-AC-3, determine frame exponent strategy */
  357. if (CONFIG_EAC3_ENCODER && s->eac3)
  358. ff_eac3_get_frame_exp_strategy(s);
  359. }
  360. /**
  361. * Update the exponents so that they are the ones the decoder will decode.
  362. *
  363. * @param[in,out] exp array of exponents for 1 block in 1 channel
  364. * @param nb_exps number of exponents in active bandwidth
  365. * @param exp_strategy exponent strategy for the block
  366. * @param cpl indicates if the block is in the coupling channel
  367. */
  368. static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy,
  369. int cpl)
  370. {
  371. int nb_groups, i, k;
  372. nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_exps] * 3;
  373. /* for each group, compute the minimum exponent */
  374. switch(exp_strategy) {
  375. case EXP_D25:
  376. for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
  377. uint8_t exp_min = exp[k];
  378. if (exp[k+1] < exp_min)
  379. exp_min = exp[k+1];
  380. exp[i-cpl] = exp_min;
  381. k += 2;
  382. }
  383. break;
  384. case EXP_D45:
  385. for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
  386. uint8_t exp_min = exp[k];
  387. if (exp[k+1] < exp_min)
  388. exp_min = exp[k+1];
  389. if (exp[k+2] < exp_min)
  390. exp_min = exp[k+2];
  391. if (exp[k+3] < exp_min)
  392. exp_min = exp[k+3];
  393. exp[i-cpl] = exp_min;
  394. k += 4;
  395. }
  396. break;
  397. }
  398. /* constraint for DC exponent */
  399. if (!cpl && exp[0] > 15)
  400. exp[0] = 15;
  401. /* decrease the delta between each groups to within 2 so that they can be
  402. differentially encoded */
  403. for (i = 1; i <= nb_groups; i++)
  404. exp[i] = FFMIN(exp[i], exp[i-1] + 2);
  405. i--;
  406. while (--i >= 0)
  407. exp[i] = FFMIN(exp[i], exp[i+1] + 2);
  408. if (cpl)
  409. exp[-1] = exp[0] & ~1;
  410. /* now we have the exponent values the decoder will see */
  411. switch (exp_strategy) {
  412. case EXP_D25:
  413. for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
  414. uint8_t exp1 = exp[i-cpl];
  415. exp[k--] = exp1;
  416. exp[k--] = exp1;
  417. }
  418. break;
  419. case EXP_D45:
  420. for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
  421. exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
  422. k -= 4;
  423. }
  424. break;
  425. }
  426. }
  427. /*
  428. * Encode exponents from original extracted form to what the decoder will see.
  429. * This copies and groups exponents based on exponent strategy and reduces
  430. * deltas between adjacent exponent groups so that they can be differentially
  431. * encoded.
  432. */
  433. static void encode_exponents(AC3EncodeContext *s)
  434. {
  435. int blk, blk1, ch, cpl;
  436. uint8_t *exp, *exp_strategy;
  437. int nb_coefs, num_reuse_blocks;
  438. for (ch = !s->cpl_on; ch <= s->channels; ch++) {
  439. exp = s->blocks[0].exp[ch] + s->start_freq[ch];
  440. exp_strategy = s->exp_strategy[ch];
  441. cpl = (ch == CPL_CH);
  442. blk = 0;
  443. while (blk < s->num_blocks) {
  444. AC3Block *block = &s->blocks[blk];
  445. if (cpl && !block->cpl_in_use) {
  446. exp += AC3_MAX_COEFS;
  447. blk++;
  448. continue;
  449. }
  450. nb_coefs = block->end_freq[ch] - s->start_freq[ch];
  451. blk1 = blk + 1;
  452. /* count the number of EXP_REUSE blocks after the current block
  453. and set exponent reference block numbers */
  454. s->exp_ref_block[ch][blk] = blk;
  455. while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE) {
  456. s->exp_ref_block[ch][blk1] = blk;
  457. blk1++;
  458. }
  459. num_reuse_blocks = blk1 - blk - 1;
  460. /* for the EXP_REUSE case we select the min of the exponents */
  461. s->ac3dsp.ac3_exponent_min(exp-s->start_freq[ch], num_reuse_blocks,
  462. AC3_MAX_COEFS);
  463. encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk], cpl);
  464. exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
  465. blk = blk1;
  466. }
  467. }
  468. /* reference block numbers have been changed, so reset ref_bap_set */
  469. s->ref_bap_set = 0;
  470. }
  471. /*
  472. * Count exponent bits based on bandwidth, coupling, and exponent strategies.
  473. */
  474. static int count_exponent_bits(AC3EncodeContext *s)
  475. {
  476. int blk, ch;
  477. int nb_groups, bit_count;
  478. bit_count = 0;
  479. for (blk = 0; blk < s->num_blocks; blk++) {
  480. AC3Block *block = &s->blocks[blk];
  481. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  482. int exp_strategy = s->exp_strategy[ch][blk];
  483. int cpl = (ch == CPL_CH);
  484. int nb_coefs = block->end_freq[ch] - s->start_freq[ch];
  485. if (exp_strategy == EXP_REUSE)
  486. continue;
  487. nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_coefs];
  488. bit_count += 4 + (nb_groups * 7);
  489. }
  490. }
  491. return bit_count;
  492. }
  493. /**
  494. * Group exponents.
  495. * 3 delta-encoded exponents are in each 7-bit group. The number of groups
  496. * varies depending on exponent strategy and bandwidth.
  497. *
  498. * @param s AC-3 encoder private context
  499. */
  500. void ff_ac3_group_exponents(AC3EncodeContext *s)
  501. {
  502. int blk, ch, i, cpl;
  503. int group_size, nb_groups;
  504. uint8_t *p;
  505. int delta0, delta1, delta2;
  506. int exp0, exp1;
  507. for (blk = 0; blk < s->num_blocks; blk++) {
  508. AC3Block *block = &s->blocks[blk];
  509. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  510. int exp_strategy = s->exp_strategy[ch][blk];
  511. if (exp_strategy == EXP_REUSE)
  512. continue;
  513. cpl = (ch == CPL_CH);
  514. group_size = exp_strategy + (exp_strategy == EXP_D45);
  515. nb_groups = exponent_group_tab[cpl][exp_strategy-1][block->end_freq[ch]-s->start_freq[ch]];
  516. p = block->exp[ch] + s->start_freq[ch] - cpl;
  517. /* DC exponent */
  518. exp1 = *p++;
  519. block->grouped_exp[ch][0] = exp1;
  520. /* remaining exponents are delta encoded */
  521. for (i = 1; i <= nb_groups; i++) {
  522. /* merge three delta in one code */
  523. exp0 = exp1;
  524. exp1 = p[0];
  525. p += group_size;
  526. delta0 = exp1 - exp0 + 2;
  527. av_assert2(delta0 >= 0 && delta0 <= 4);
  528. exp0 = exp1;
  529. exp1 = p[0];
  530. p += group_size;
  531. delta1 = exp1 - exp0 + 2;
  532. av_assert2(delta1 >= 0 && delta1 <= 4);
  533. exp0 = exp1;
  534. exp1 = p[0];
  535. p += group_size;
  536. delta2 = exp1 - exp0 + 2;
  537. av_assert2(delta2 >= 0 && delta2 <= 4);
  538. block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
  539. }
  540. }
  541. }
  542. }
  543. /**
  544. * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
  545. * Extract exponents from MDCT coefficients, calculate exponent strategies,
  546. * and encode final exponents.
  547. *
  548. * @param s AC-3 encoder private context
  549. */
  550. void ff_ac3_process_exponents(AC3EncodeContext *s)
  551. {
  552. extract_exponents(s);
  553. compute_exp_strategy(s);
  554. encode_exponents(s);
  555. emms_c();
  556. }
  557. /*
  558. * Count frame bits that are based solely on fixed parameters.
  559. * This only has to be run once when the encoder is initialized.
  560. */
  561. static void count_frame_bits_fixed(AC3EncodeContext *s)
  562. {
  563. static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
  564. int blk;
  565. int frame_bits;
  566. /* assumptions:
  567. * no dynamic range codes
  568. * bit allocation parameters do not change between blocks
  569. * no delta bit allocation
  570. * no skipped data
  571. * no auxiliary data
  572. * no E-AC-3 metadata
  573. */
  574. /* header */
  575. frame_bits = 16; /* sync info */
  576. if (s->eac3) {
  577. /* bitstream info header */
  578. frame_bits += 35;
  579. frame_bits += 1 + 1;
  580. if (s->num_blocks != 0x6)
  581. frame_bits++;
  582. frame_bits++;
  583. /* audio frame header */
  584. if (s->num_blocks == 6)
  585. frame_bits += 2;
  586. frame_bits += 10;
  587. /* exponent strategy */
  588. if (s->use_frame_exp_strategy)
  589. frame_bits += 5 * s->fbw_channels;
  590. else
  591. frame_bits += s->num_blocks * 2 * s->fbw_channels;
  592. if (s->lfe_on)
  593. frame_bits += s->num_blocks;
  594. /* converter exponent strategy */
  595. if (s->num_blks_code != 0x3)
  596. frame_bits++;
  597. else
  598. frame_bits += s->fbw_channels * 5;
  599. /* snr offsets */
  600. frame_bits += 10;
  601. /* block start info */
  602. if (s->num_blocks != 1)
  603. frame_bits++;
  604. } else {
  605. frame_bits += 49;
  606. frame_bits += frame_bits_inc[s->channel_mode];
  607. }
  608. /* audio blocks */
  609. for (blk = 0; blk < s->num_blocks; blk++) {
  610. if (!s->eac3) {
  611. /* block switch flags */
  612. frame_bits += s->fbw_channels;
  613. /* dither flags */
  614. frame_bits += s->fbw_channels;
  615. }
  616. /* dynamic range */
  617. frame_bits++;
  618. /* spectral extension */
  619. if (s->eac3)
  620. frame_bits++;
  621. if (!s->eac3) {
  622. /* exponent strategy */
  623. frame_bits += 2 * s->fbw_channels;
  624. if (s->lfe_on)
  625. frame_bits++;
  626. /* bit allocation params */
  627. frame_bits++;
  628. if (!blk)
  629. frame_bits += 2 + 2 + 2 + 2 + 3;
  630. }
  631. /* converter snr offset */
  632. if (s->eac3)
  633. frame_bits++;
  634. if (!s->eac3) {
  635. /* delta bit allocation */
  636. frame_bits++;
  637. /* skipped data */
  638. frame_bits++;
  639. }
  640. }
  641. /* auxiliary data */
  642. frame_bits++;
  643. /* CRC */
  644. frame_bits += 1 + 16;
  645. s->frame_bits_fixed = frame_bits;
  646. }
  647. /*
  648. * Initialize bit allocation.
  649. * Set default parameter codes and calculate parameter values.
  650. */
  651. static av_cold void bit_alloc_init(AC3EncodeContext *s)
  652. {
  653. int ch;
  654. /* init default parameters */
  655. s->slow_decay_code = 2;
  656. s->fast_decay_code = 1;
  657. s->slow_gain_code = 1;
  658. s->db_per_bit_code = s->eac3 ? 2 : 3;
  659. s->floor_code = 7;
  660. for (ch = 0; ch <= s->channels; ch++)
  661. s->fast_gain_code[ch] = 4;
  662. /* initial snr offset */
  663. s->coarse_snr_offset = 40;
  664. /* compute real values */
  665. /* currently none of these values change during encoding, so we can just
  666. set them once at initialization */
  667. s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
  668. s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
  669. s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
  670. s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
  671. s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
  672. s->bit_alloc.cpl_fast_leak = 0;
  673. s->bit_alloc.cpl_slow_leak = 0;
  674. count_frame_bits_fixed(s);
  675. }
  676. /*
  677. * Count the bits used to encode the frame, minus exponents and mantissas.
  678. * Bits based on fixed parameters have already been counted, so now we just
  679. * have to add the bits based on parameters that change during encoding.
  680. */
  681. static void count_frame_bits(AC3EncodeContext *s)
  682. {
  683. AC3EncOptions *opt = &s->options;
  684. int blk, ch;
  685. int frame_bits = 0;
  686. /* header */
  687. if (s->eac3) {
  688. if (opt->eac3_mixing_metadata) {
  689. if (s->channel_mode > AC3_CHMODE_STEREO)
  690. frame_bits += 2;
  691. if (s->has_center)
  692. frame_bits += 6;
  693. if (s->has_surround)
  694. frame_bits += 6;
  695. frame_bits += s->lfe_on;
  696. frame_bits += 1 + 1 + 2;
  697. if (s->channel_mode < AC3_CHMODE_STEREO)
  698. frame_bits++;
  699. frame_bits++;
  700. }
  701. if (opt->eac3_info_metadata) {
  702. frame_bits += 3 + 1 + 1;
  703. if (s->channel_mode == AC3_CHMODE_STEREO)
  704. frame_bits += 2 + 2;
  705. if (s->channel_mode >= AC3_CHMODE_2F2R)
  706. frame_bits += 2;
  707. frame_bits++;
  708. if (opt->audio_production_info)
  709. frame_bits += 5 + 2 + 1;
  710. frame_bits++;
  711. }
  712. /* coupling */
  713. if (s->channel_mode > AC3_CHMODE_MONO) {
  714. frame_bits++;
  715. for (blk = 1; blk < s->num_blocks; blk++) {
  716. AC3Block *block = &s->blocks[blk];
  717. frame_bits++;
  718. if (block->new_cpl_strategy)
  719. frame_bits++;
  720. }
  721. }
  722. /* coupling exponent strategy */
  723. if (s->cpl_on) {
  724. if (s->use_frame_exp_strategy) {
  725. frame_bits += 5 * s->cpl_on;
  726. } else {
  727. for (blk = 0; blk < s->num_blocks; blk++)
  728. frame_bits += 2 * s->blocks[blk].cpl_in_use;
  729. }
  730. }
  731. } else {
  732. if (opt->audio_production_info)
  733. frame_bits += 7;
  734. if (s->bitstream_id == 6) {
  735. if (opt->extended_bsi_1)
  736. frame_bits += 14;
  737. if (opt->extended_bsi_2)
  738. frame_bits += 14;
  739. }
  740. }
  741. /* audio blocks */
  742. for (blk = 0; blk < s->num_blocks; blk++) {
  743. AC3Block *block = &s->blocks[blk];
  744. /* coupling strategy */
  745. if (!s->eac3)
  746. frame_bits++;
  747. if (block->new_cpl_strategy) {
  748. if (!s->eac3)
  749. frame_bits++;
  750. if (block->cpl_in_use) {
  751. if (s->eac3)
  752. frame_bits++;
  753. if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO)
  754. frame_bits += s->fbw_channels;
  755. if (s->channel_mode == AC3_CHMODE_STEREO)
  756. frame_bits++;
  757. frame_bits += 4 + 4;
  758. if (s->eac3)
  759. frame_bits++;
  760. else
  761. frame_bits += s->num_cpl_subbands - 1;
  762. }
  763. }
  764. /* coupling coordinates */
  765. if (block->cpl_in_use) {
  766. for (ch = 1; ch <= s->fbw_channels; ch++) {
  767. if (block->channel_in_cpl[ch]) {
  768. if (!s->eac3 || block->new_cpl_coords[ch] != 2)
  769. frame_bits++;
  770. if (block->new_cpl_coords[ch]) {
  771. frame_bits += 2;
  772. frame_bits += (4 + 4) * s->num_cpl_bands;
  773. }
  774. }
  775. }
  776. }
  777. /* stereo rematrixing */
  778. if (s->channel_mode == AC3_CHMODE_STEREO) {
  779. if (!s->eac3 || blk > 0)
  780. frame_bits++;
  781. if (s->blocks[blk].new_rematrixing_strategy)
  782. frame_bits += block->num_rematrixing_bands;
  783. }
  784. /* bandwidth codes & gain range */
  785. for (ch = 1; ch <= s->fbw_channels; ch++) {
  786. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  787. if (!block->channel_in_cpl[ch])
  788. frame_bits += 6;
  789. frame_bits += 2;
  790. }
  791. }
  792. /* coupling exponent strategy */
  793. if (!s->eac3 && block->cpl_in_use)
  794. frame_bits += 2;
  795. /* snr offsets and fast gain codes */
  796. if (!s->eac3) {
  797. frame_bits++;
  798. if (block->new_snr_offsets)
  799. frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
  800. }
  801. /* coupling leak info */
  802. if (block->cpl_in_use) {
  803. if (!s->eac3 || block->new_cpl_leak != 2)
  804. frame_bits++;
  805. if (block->new_cpl_leak)
  806. frame_bits += 3 + 3;
  807. }
  808. }
  809. s->frame_bits = s->frame_bits_fixed + frame_bits;
  810. }
  811. /*
  812. * Calculate masking curve based on the final exponents.
  813. * Also calculate the power spectral densities to use in future calculations.
  814. */
  815. static void bit_alloc_masking(AC3EncodeContext *s)
  816. {
  817. int blk, ch;
  818. for (blk = 0; blk < s->num_blocks; blk++) {
  819. AC3Block *block = &s->blocks[blk];
  820. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  821. /* We only need psd and mask for calculating bap.
  822. Since we currently do not calculate bap when exponent
  823. strategy is EXP_REUSE we do not need to calculate psd or mask. */
  824. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  825. ff_ac3_bit_alloc_calc_psd(block->exp[ch], s->start_freq[ch],
  826. block->end_freq[ch], block->psd[ch],
  827. block->band_psd[ch]);
  828. ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
  829. s->start_freq[ch], block->end_freq[ch],
  830. ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
  831. ch == s->lfe_channel,
  832. DBA_NONE, 0, NULL, NULL, NULL,
  833. block->mask[ch]);
  834. }
  835. }
  836. }
  837. }
  838. /*
  839. * Ensure that bap for each block and channel point to the current bap_buffer.
  840. * They may have been switched during the bit allocation search.
  841. */
  842. static void reset_block_bap(AC3EncodeContext *s)
  843. {
  844. int blk, ch;
  845. uint8_t *ref_bap;
  846. if (s->ref_bap[0][0] == s->bap_buffer && s->ref_bap_set)
  847. return;
  848. ref_bap = s->bap_buffer;
  849. for (ch = 0; ch <= s->channels; ch++) {
  850. for (blk = 0; blk < s->num_blocks; blk++)
  851. s->ref_bap[ch][blk] = ref_bap + AC3_MAX_COEFS * s->exp_ref_block[ch][blk];
  852. ref_bap += AC3_MAX_COEFS * s->num_blocks;
  853. }
  854. s->ref_bap_set = 1;
  855. }
  856. /**
  857. * Initialize mantissa counts.
  858. * These are set so that they are padded to the next whole group size when bits
  859. * are counted in compute_mantissa_size.
  860. *
  861. * @param[in,out] mant_cnt running counts for each bap value for each block
  862. */
  863. static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
  864. {
  865. int blk;
  866. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  867. memset(mant_cnt[blk], 0, sizeof(mant_cnt[blk]));
  868. mant_cnt[blk][1] = mant_cnt[blk][2] = 2;
  869. mant_cnt[blk][4] = 1;
  870. }
  871. }
  872. /**
  873. * Update mantissa bit counts for all blocks in 1 channel in a given bandwidth
  874. * range.
  875. *
  876. * @param s AC-3 encoder private context
  877. * @param ch channel index
  878. * @param[in,out] mant_cnt running counts for each bap value for each block
  879. * @param start starting coefficient bin
  880. * @param end ending coefficient bin
  881. */
  882. static void count_mantissa_bits_update_ch(AC3EncodeContext *s, int ch,
  883. uint16_t mant_cnt[AC3_MAX_BLOCKS][16],
  884. int start, int end)
  885. {
  886. int blk;
  887. for (blk = 0; blk < s->num_blocks; blk++) {
  888. AC3Block *block = &s->blocks[blk];
  889. if (ch == CPL_CH && !block->cpl_in_use)
  890. continue;
  891. s->ac3dsp.update_bap_counts(mant_cnt[blk],
  892. s->ref_bap[ch][blk] + start,
  893. FFMIN(end, block->end_freq[ch]) - start);
  894. }
  895. }
  896. /*
  897. * Count the number of mantissa bits in the frame based on the bap values.
  898. */
  899. static int count_mantissa_bits(AC3EncodeContext *s)
  900. {
  901. int ch, max_end_freq;
  902. LOCAL_ALIGNED_16(uint16_t, mant_cnt, [AC3_MAX_BLOCKS], [16]);
  903. count_mantissa_bits_init(mant_cnt);
  904. max_end_freq = s->bandwidth_code * 3 + 73;
  905. for (ch = !s->cpl_enabled; ch <= s->channels; ch++)
  906. count_mantissa_bits_update_ch(s, ch, mant_cnt, s->start_freq[ch],
  907. max_end_freq);
  908. return s->ac3dsp.compute_mantissa_size(mant_cnt);
  909. }
  910. /**
  911. * Run the bit allocation with a given SNR offset.
  912. * This calculates the bit allocation pointers that will be used to determine
  913. * the quantization of each mantissa.
  914. *
  915. * @param s AC-3 encoder private context
  916. * @param snr_offset SNR offset, 0 to 1023
  917. * @return the number of bits needed for mantissas if the given SNR offset is
  918. * is used.
  919. */
  920. static int bit_alloc(AC3EncodeContext *s, int snr_offset)
  921. {
  922. int blk, ch;
  923. snr_offset = (snr_offset - 240) * 4;
  924. reset_block_bap(s);
  925. for (blk = 0; blk < s->num_blocks; blk++) {
  926. AC3Block *block = &s->blocks[blk];
  927. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  928. /* Currently the only bit allocation parameters which vary across
  929. blocks within a frame are the exponent values. We can take
  930. advantage of that by reusing the bit allocation pointers
  931. whenever we reuse exponents. */
  932. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  933. s->ac3dsp.bit_alloc_calc_bap(block->mask[ch], block->psd[ch],
  934. s->start_freq[ch], block->end_freq[ch],
  935. snr_offset, s->bit_alloc.floor,
  936. ff_ac3_bap_tab, s->ref_bap[ch][blk]);
  937. }
  938. }
  939. }
  940. return count_mantissa_bits(s);
  941. }
  942. /*
  943. * Constant bitrate bit allocation search.
  944. * Find the largest SNR offset that will allow data to fit in the frame.
  945. */
  946. static int cbr_bit_allocation(AC3EncodeContext *s)
  947. {
  948. int ch;
  949. int bits_left;
  950. int snr_offset, snr_incr;
  951. bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
  952. if (bits_left < 0)
  953. return AVERROR(EINVAL);
  954. snr_offset = s->coarse_snr_offset << 4;
  955. /* if previous frame SNR offset was 1023, check if current frame can also
  956. use SNR offset of 1023. if so, skip the search. */
  957. if ((snr_offset | s->fine_snr_offset[1]) == 1023) {
  958. if (bit_alloc(s, 1023) <= bits_left)
  959. return 0;
  960. }
  961. while (snr_offset >= 0 &&
  962. bit_alloc(s, snr_offset) > bits_left) {
  963. snr_offset -= 64;
  964. }
  965. if (snr_offset < 0)
  966. return AVERROR(EINVAL);
  967. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  968. for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
  969. while (snr_offset + snr_incr <= 1023 &&
  970. bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
  971. snr_offset += snr_incr;
  972. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  973. }
  974. }
  975. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  976. reset_block_bap(s);
  977. s->coarse_snr_offset = snr_offset >> 4;
  978. for (ch = !s->cpl_on; ch <= s->channels; ch++)
  979. s->fine_snr_offset[ch] = snr_offset & 0xF;
  980. return 0;
  981. }
  982. /*
  983. * Perform bit allocation search.
  984. * Finds the SNR offset value that maximizes quality and fits in the specified
  985. * frame size. Output is the SNR offset and a set of bit allocation pointers
  986. * used to quantize the mantissas.
  987. */
  988. int ff_ac3_compute_bit_allocation(AC3EncodeContext *s)
  989. {
  990. count_frame_bits(s);
  991. s->exponent_bits = count_exponent_bits(s);
  992. bit_alloc_masking(s);
  993. return cbr_bit_allocation(s);
  994. }
  995. /**
  996. * Symmetric quantization on 'levels' levels.
  997. *
  998. * @param c unquantized coefficient
  999. * @param e exponent
  1000. * @param levels number of quantization levels
  1001. * @return quantized coefficient
  1002. */
  1003. static inline int sym_quant(int c, int e, int levels)
  1004. {
  1005. int v = (((levels * c) >> (24 - e)) + levels) >> 1;
  1006. av_assert2(v >= 0 && v < levels);
  1007. return v;
  1008. }
  1009. /**
  1010. * Asymmetric quantization on 2^qbits levels.
  1011. *
  1012. * @param c unquantized coefficient
  1013. * @param e exponent
  1014. * @param qbits number of quantization bits
  1015. * @return quantized coefficient
  1016. */
  1017. static inline int asym_quant(int c, int e, int qbits)
  1018. {
  1019. int m;
  1020. c = (((c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
  1021. m = (1 << (qbits-1));
  1022. if (c >= m)
  1023. c = m - 1;
  1024. av_assert2(c >= -m);
  1025. return c;
  1026. }
  1027. /**
  1028. * Quantize a set of mantissas for a single channel in a single block.
  1029. *
  1030. * @param s Mantissa count context
  1031. * @param fixed_coef unquantized fixed-point coefficients
  1032. * @param exp exponents
  1033. * @param bap bit allocation pointer indices
  1034. * @param[out] qmant quantized coefficients
  1035. * @param start_freq starting coefficient bin
  1036. * @param end_freq ending coefficient bin
  1037. */
  1038. static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
  1039. uint8_t *exp, uint8_t *bap,
  1040. int16_t *qmant, int start_freq,
  1041. int end_freq)
  1042. {
  1043. int i;
  1044. for (i = start_freq; i < end_freq; i++) {
  1045. int c = fixed_coef[i];
  1046. int e = exp[i];
  1047. int v = bap[i];
  1048. if (v)
  1049. switch (v) {
  1050. case 1:
  1051. v = sym_quant(c, e, 3);
  1052. switch (s->mant1_cnt) {
  1053. case 0:
  1054. s->qmant1_ptr = &qmant[i];
  1055. v = 9 * v;
  1056. s->mant1_cnt = 1;
  1057. break;
  1058. case 1:
  1059. *s->qmant1_ptr += 3 * v;
  1060. s->mant1_cnt = 2;
  1061. v = 128;
  1062. break;
  1063. default:
  1064. *s->qmant1_ptr += v;
  1065. s->mant1_cnt = 0;
  1066. v = 128;
  1067. break;
  1068. }
  1069. break;
  1070. case 2:
  1071. v = sym_quant(c, e, 5);
  1072. switch (s->mant2_cnt) {
  1073. case 0:
  1074. s->qmant2_ptr = &qmant[i];
  1075. v = 25 * v;
  1076. s->mant2_cnt = 1;
  1077. break;
  1078. case 1:
  1079. *s->qmant2_ptr += 5 * v;
  1080. s->mant2_cnt = 2;
  1081. v = 128;
  1082. break;
  1083. default:
  1084. *s->qmant2_ptr += v;
  1085. s->mant2_cnt = 0;
  1086. v = 128;
  1087. break;
  1088. }
  1089. break;
  1090. case 3:
  1091. v = sym_quant(c, e, 7);
  1092. break;
  1093. case 4:
  1094. v = sym_quant(c, e, 11);
  1095. switch (s->mant4_cnt) {
  1096. case 0:
  1097. s->qmant4_ptr = &qmant[i];
  1098. v = 11 * v;
  1099. s->mant4_cnt = 1;
  1100. break;
  1101. default:
  1102. *s->qmant4_ptr += v;
  1103. s->mant4_cnt = 0;
  1104. v = 128;
  1105. break;
  1106. }
  1107. break;
  1108. case 5:
  1109. v = sym_quant(c, e, 15);
  1110. break;
  1111. case 14:
  1112. v = asym_quant(c, e, 14);
  1113. break;
  1114. case 15:
  1115. v = asym_quant(c, e, 16);
  1116. break;
  1117. default:
  1118. v = asym_quant(c, e, v - 1);
  1119. break;
  1120. }
  1121. qmant[i] = v;
  1122. }
  1123. }
  1124. /**
  1125. * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
  1126. *
  1127. * @param s AC-3 encoder private context
  1128. */
  1129. void ff_ac3_quantize_mantissas(AC3EncodeContext *s)
  1130. {
  1131. int blk, ch, ch0=0, got_cpl;
  1132. for (blk = 0; blk < s->num_blocks; blk++) {
  1133. AC3Block *block = &s->blocks[blk];
  1134. AC3Mant m = { 0 };
  1135. got_cpl = !block->cpl_in_use;
  1136. for (ch = 1; ch <= s->channels; ch++) {
  1137. if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
  1138. ch0 = ch - 1;
  1139. ch = CPL_CH;
  1140. got_cpl = 1;
  1141. }
  1142. quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
  1143. s->blocks[s->exp_ref_block[ch][blk]].exp[ch],
  1144. s->ref_bap[ch][blk], block->qmant[ch],
  1145. s->start_freq[ch], block->end_freq[ch]);
  1146. if (ch == CPL_CH)
  1147. ch = ch0;
  1148. }
  1149. }
  1150. }
  1151. /*
  1152. * Write the AC-3 frame header to the output bitstream.
  1153. */
  1154. static void ac3_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, bnd, got_cpl, av_uninit(ch0);
  1210. AC3Block *block = &s->blocks[blk];
  1211. /* block switching */
  1212. if (!s->eac3) {
  1213. for (ch = 0; ch < s->fbw_channels; ch++)
  1214. put_bits(&s->pb, 1, 0);
  1215. }
  1216. /* dither flags */
  1217. if (!s->eac3) {
  1218. for (ch = 0; ch < s->fbw_channels; ch++)
  1219. put_bits(&s->pb, 1, 1);
  1220. }
  1221. /* dynamic range codes */
  1222. put_bits(&s->pb, 1, 0);
  1223. /* spectral extension */
  1224. if (s->eac3)
  1225. put_bits(&s->pb, 1, 0);
  1226. /* channel coupling */
  1227. if (!s->eac3)
  1228. put_bits(&s->pb, 1, block->new_cpl_strategy);
  1229. if (block->new_cpl_strategy) {
  1230. if (!s->eac3)
  1231. put_bits(&s->pb, 1, block->cpl_in_use);
  1232. if (block->cpl_in_use) {
  1233. int start_sub, end_sub;
  1234. if (s->eac3)
  1235. put_bits(&s->pb, 1, 0); /* enhanced coupling */
  1236. if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
  1237. for (ch = 1; ch <= s->fbw_channels; ch++)
  1238. put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
  1239. }
  1240. if (s->channel_mode == AC3_CHMODE_STEREO)
  1241. put_bits(&s->pb, 1, 0); /* phase flags in use */
  1242. start_sub = (s->start_freq[CPL_CH] - 37) / 12;
  1243. end_sub = (s->cpl_end_freq - 37) / 12;
  1244. put_bits(&s->pb, 4, start_sub);
  1245. put_bits(&s->pb, 4, end_sub - 3);
  1246. /* coupling band structure */
  1247. if (s->eac3) {
  1248. put_bits(&s->pb, 1, 0); /* use default */
  1249. } else {
  1250. for (bnd = start_sub+1; bnd < end_sub; bnd++)
  1251. put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
  1252. }
  1253. }
  1254. }
  1255. /* coupling coordinates */
  1256. if (block->cpl_in_use) {
  1257. for (ch = 1; ch <= s->fbw_channels; ch++) {
  1258. if (block->channel_in_cpl[ch]) {
  1259. if (!s->eac3 || block->new_cpl_coords[ch] != 2)
  1260. put_bits(&s->pb, 1, block->new_cpl_coords[ch]);
  1261. if (block->new_cpl_coords[ch]) {
  1262. put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
  1263. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  1264. put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
  1265. put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. }
  1271. /* stereo rematrixing */
  1272. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1273. if (!s->eac3 || blk > 0)
  1274. put_bits(&s->pb, 1, block->new_rematrixing_strategy);
  1275. if (block->new_rematrixing_strategy) {
  1276. /* rematrixing flags */
  1277. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
  1278. put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
  1279. }
  1280. }
  1281. /* exponent strategy */
  1282. if (!s->eac3) {
  1283. for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
  1284. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  1285. if (s->lfe_on)
  1286. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  1287. }
  1288. /* bandwidth */
  1289. for (ch = 1; ch <= s->fbw_channels; ch++) {
  1290. if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
  1291. put_bits(&s->pb, 6, s->bandwidth_code);
  1292. }
  1293. /* exponents */
  1294. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  1295. int nb_groups;
  1296. int cpl = (ch == CPL_CH);
  1297. if (s->exp_strategy[ch][blk] == EXP_REUSE)
  1298. continue;
  1299. /* DC exponent */
  1300. put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
  1301. /* exponent groups */
  1302. nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
  1303. for (i = 1; i <= nb_groups; i++)
  1304. put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
  1305. /* gain range info */
  1306. if (ch != s->lfe_channel && !cpl)
  1307. put_bits(&s->pb, 2, 0);
  1308. }
  1309. /* bit allocation info */
  1310. if (!s->eac3) {
  1311. baie = (blk == 0);
  1312. put_bits(&s->pb, 1, baie);
  1313. if (baie) {
  1314. put_bits(&s->pb, 2, s->slow_decay_code);
  1315. put_bits(&s->pb, 2, s->fast_decay_code);
  1316. put_bits(&s->pb, 2, s->slow_gain_code);
  1317. put_bits(&s->pb, 2, s->db_per_bit_code);
  1318. put_bits(&s->pb, 3, s->floor_code);
  1319. }
  1320. }
  1321. /* snr offset */
  1322. if (!s->eac3) {
  1323. put_bits(&s->pb, 1, block->new_snr_offsets);
  1324. if (block->new_snr_offsets) {
  1325. put_bits(&s->pb, 6, s->coarse_snr_offset);
  1326. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  1327. put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
  1328. put_bits(&s->pb, 3, s->fast_gain_code[ch]);
  1329. }
  1330. }
  1331. } else {
  1332. put_bits(&s->pb, 1, 0); /* no converter snr offset */
  1333. }
  1334. /* coupling leak */
  1335. if (block->cpl_in_use) {
  1336. if (!s->eac3 || block->new_cpl_leak != 2)
  1337. put_bits(&s->pb, 1, block->new_cpl_leak);
  1338. if (block->new_cpl_leak) {
  1339. put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
  1340. put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
  1341. }
  1342. }