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.

1957 lines
58KB

  1. /*
  2. * The simplest AC-3 encoder
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
  5. * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * The simplest AC-3 encoder.
  26. */
  27. //#define DEBUG
  28. #include "libavcore/audioconvert.h"
  29. #include "libavutil/crc.h"
  30. #include "avcodec.h"
  31. #include "put_bits.h"
  32. #include "dsputil.h"
  33. #include "ac3.h"
  34. #include "audioconvert.h"
  35. #define MDCT_NBITS 9
  36. #define MDCT_SAMPLES (1 << MDCT_NBITS)
  37. /** Maximum number of exponent groups. +1 for separate DC exponent. */
  38. #define AC3_MAX_EXP_GROUPS 85
  39. /** Scale a float value by 2^bits and convert to an integer. */
  40. #define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
  41. /** Scale a float value by 2^15, convert to an integer, and clip to int16_t range. */
  42. #define FIX15(a) av_clip_int16(SCALE_FLOAT(a, 15))
  43. /**
  44. * Compex number.
  45. * Used in fixed-point MDCT calculation.
  46. */
  47. typedef struct IComplex {
  48. int16_t re,im;
  49. } IComplex;
  50. typedef struct AC3MDCTContext {
  51. AVCodecContext *avctx; ///< parent context for av_log()
  52. int16_t *rot_tmp; ///< temp buffer for pre-rotated samples
  53. IComplex *cplx_tmp; ///< temp buffer for complex pre-rotated samples
  54. } AC3MDCTContext;
  55. /**
  56. * Data for a single audio block.
  57. */
  58. typedef struct AC3Block {
  59. uint8_t **bap; ///< bit allocation pointers (bap)
  60. int32_t **mdct_coef; ///< MDCT coefficients
  61. uint8_t **exp; ///< original exponents
  62. uint8_t **grouped_exp; ///< grouped exponents
  63. int16_t **psd; ///< psd per frequency bin
  64. int16_t **band_psd; ///< psd per critical band
  65. int16_t **mask; ///< masking curve
  66. uint16_t **qmant; ///< quantized mantissas
  67. uint8_t exp_strategy[AC3_MAX_CHANNELS]; ///< exponent strategies
  68. int8_t exp_shift[AC3_MAX_CHANNELS]; ///< exponent shift values
  69. } AC3Block;
  70. /**
  71. * AC-3 encoder private context.
  72. */
  73. typedef struct AC3EncodeContext {
  74. PutBitContext pb; ///< bitstream writer context
  75. DSPContext dsp;
  76. AC3MDCTContext mdct; ///< MDCT context
  77. AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
  78. int bitstream_id; ///< bitstream id (bsid)
  79. int bitstream_mode; ///< bitstream mode (bsmod)
  80. int bit_rate; ///< target bit rate, in bits-per-second
  81. int sample_rate; ///< sampling frequency, in Hz
  82. int frame_size_min; ///< minimum frame size in case rounding is necessary
  83. int frame_size; ///< current frame size in bytes
  84. int frame_size_code; ///< frame size code (frmsizecod)
  85. int bits_written; ///< bit count (used to avg. bitrate)
  86. int samples_written; ///< sample count (used to avg. bitrate)
  87. int fbw_channels; ///< number of full-bandwidth channels (nfchans)
  88. int channels; ///< total number of channels (nchans)
  89. int lfe_on; ///< indicates if there is an LFE channel (lfeon)
  90. int lfe_channel; ///< channel index of the LFE channel
  91. int channel_mode; ///< channel mode (acmod)
  92. const uint8_t *channel_map; ///< channel map used to reorder channels
  93. int bandwidth_code[AC3_MAX_CHANNELS]; ///< bandwidth code (0 to 60) (chbwcod)
  94. int nb_coefs[AC3_MAX_CHANNELS];
  95. /* bitrate allocation control */
  96. int slow_gain_code; ///< slow gain code (sgaincod)
  97. int slow_decay_code; ///< slow decay code (sdcycod)
  98. int fast_decay_code; ///< fast decay code (fdcycod)
  99. int db_per_bit_code; ///< dB/bit code (dbpbcod)
  100. int floor_code; ///< floor code (floorcod)
  101. AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
  102. int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
  103. int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
  104. int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
  105. int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
  106. int frame_bits; ///< all frame bits except exponents and mantissas
  107. int exponent_bits; ///< number of bits used for exponents
  108. /* mantissa encoding */
  109. int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4
  110. uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
  111. int16_t **planar_samples;
  112. uint8_t *bap_buffer;
  113. uint8_t *bap1_buffer;
  114. int32_t *mdct_coef_buffer;
  115. uint8_t *exp_buffer;
  116. uint8_t *grouped_exp_buffer;
  117. int16_t *psd_buffer;
  118. int16_t *band_psd_buffer;
  119. int16_t *mask_buffer;
  120. uint16_t *qmant_buffer;
  121. DECLARE_ALIGNED(16, int16_t, windowed_samples)[AC3_WINDOW_SIZE];
  122. } AC3EncodeContext;
  123. /** MDCT and FFT tables */
  124. static int16_t costab[64];
  125. static int16_t sintab[64];
  126. static int16_t xcos1[128];
  127. static int16_t xsin1[128];
  128. /**
  129. * LUT for number of exponent groups.
  130. * exponent_group_tab[exponent strategy-1][number of coefficients]
  131. */
  132. uint8_t exponent_group_tab[3][256];
  133. /**
  134. * Adjust the frame size to make the average bit rate match the target bit rate.
  135. * This is only needed for 11025, 22050, and 44100 sample rates.
  136. */
  137. static void adjust_frame_size(AC3EncodeContext *s)
  138. {
  139. while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
  140. s->bits_written -= s->bit_rate;
  141. s->samples_written -= s->sample_rate;
  142. }
  143. s->frame_size = s->frame_size_min +
  144. 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
  145. s->bits_written += s->frame_size * 8;
  146. s->samples_written += AC3_FRAME_SIZE;
  147. }
  148. /**
  149. * Deinterleave input samples.
  150. * Channels are reordered from FFmpeg's default order to AC-3 order.
  151. */
  152. static void deinterleave_input_samples(AC3EncodeContext *s,
  153. const int16_t *samples)
  154. {
  155. int ch, i;
  156. /* deinterleave and remap input samples */
  157. for (ch = 0; ch < s->channels; ch++) {
  158. const int16_t *sptr;
  159. int sinc;
  160. /* copy last 256 samples of previous frame to the start of the current frame */
  161. memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
  162. AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
  163. /* deinterleave */
  164. sinc = s->channels;
  165. sptr = samples + s->channel_map[ch];
  166. for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
  167. s->planar_samples[ch][i] = *sptr;
  168. sptr += sinc;
  169. }
  170. }
  171. }
  172. /**
  173. * Finalize MDCT and free allocated memory.
  174. */
  175. static av_cold void mdct_end(AC3MDCTContext *mdct)
  176. {
  177. av_freep(&mdct->rot_tmp);
  178. av_freep(&mdct->cplx_tmp);
  179. }
  180. /**
  181. * Initialize FFT tables.
  182. * @param ln log2(FFT size)
  183. */
  184. static av_cold void fft_init(int ln)
  185. {
  186. int i, n, n2;
  187. float alpha;
  188. n = 1 << ln;
  189. n2 = n >> 1;
  190. for (i = 0; i < n2; i++) {
  191. alpha = 2.0 * M_PI * i / n;
  192. costab[i] = FIX15(cos(alpha));
  193. sintab[i] = FIX15(sin(alpha));
  194. }
  195. }
  196. /**
  197. * Initialize MDCT tables.
  198. * @param nbits log2(MDCT size)
  199. */
  200. static av_cold int mdct_init(AC3MDCTContext *mdct, int nbits)
  201. {
  202. int i, n, n4;
  203. n = 1 << nbits;
  204. n4 = n >> 2;
  205. fft_init(nbits - 2);
  206. FF_ALLOC_OR_GOTO(mdct->avctx, mdct->rot_tmp, n * sizeof(*mdct->rot_tmp),
  207. mdct_alloc_fail);
  208. FF_ALLOC_OR_GOTO(mdct->avctx, mdct->cplx_tmp, n4 * sizeof(*mdct->cplx_tmp),
  209. mdct_alloc_fail);
  210. for (i = 0; i < n4; i++) {
  211. float alpha = 2.0 * M_PI * (i + 1.0 / 8.0) / n;
  212. xcos1[i] = FIX15(-cos(alpha));
  213. xsin1[i] = FIX15(-sin(alpha));
  214. }
  215. return 0;
  216. mdct_alloc_fail:
  217. return AVERROR(ENOMEM);
  218. }
  219. /** Butterfly op */
  220. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  221. { \
  222. int ax, ay, bx, by; \
  223. bx = pre1; \
  224. by = pim1; \
  225. ax = qre1; \
  226. ay = qim1; \
  227. pre = (bx + ax) >> 1; \
  228. pim = (by + ay) >> 1; \
  229. qre = (bx - ax) >> 1; \
  230. qim = (by - ay) >> 1; \
  231. }
  232. /** Complex multiply */
  233. #define CMUL(pre, pim, are, aim, bre, bim) \
  234. { \
  235. pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15; \
  236. pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15; \
  237. }
  238. /**
  239. * Calculate a 2^n point complex FFT on 2^ln points.
  240. * @param z complex input/output samples
  241. * @param ln log2(FFT size)
  242. */
  243. static void fft(IComplex *z, int ln)
  244. {
  245. int j, l, np, np2;
  246. int nblocks, nloops;
  247. register IComplex *p,*q;
  248. int tmp_re, tmp_im;
  249. np = 1 << ln;
  250. /* reverse */
  251. for (j = 0; j < np; j++) {
  252. int k = av_reverse[j] >> (8 - ln);
  253. if (k < j)
  254. FFSWAP(IComplex, z[k], z[j]);
  255. }
  256. /* pass 0 */
  257. p = &z[0];
  258. j = np >> 1;
  259. do {
  260. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  261. p[0].re, p[0].im, p[1].re, p[1].im);
  262. p += 2;
  263. } while (--j);
  264. /* pass 1 */
  265. p = &z[0];
  266. j = np >> 2;
  267. do {
  268. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  269. p[0].re, p[0].im, p[2].re, p[2].im);
  270. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  271. p[1].re, p[1].im, p[3].im, -p[3].re);
  272. p+=4;
  273. } while (--j);
  274. /* pass 2 .. ln-1 */
  275. nblocks = np >> 3;
  276. nloops = 1 << 2;
  277. np2 = np >> 1;
  278. do {
  279. p = z;
  280. q = z + nloops;
  281. for (j = 0; j < nblocks; j++) {
  282. BF(p->re, p->im, q->re, q->im,
  283. p->re, p->im, q->re, q->im);
  284. p++;
  285. q++;
  286. for(l = nblocks; l < np2; l += nblocks) {
  287. CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
  288. BF(p->re, p->im, q->re, q->im,
  289. p->re, p->im, tmp_re, tmp_im);
  290. p++;
  291. q++;
  292. }
  293. p += nloops;
  294. q += nloops;
  295. }
  296. nblocks = nblocks >> 1;
  297. nloops = nloops << 1;
  298. } while (nblocks);
  299. }
  300. /**
  301. * Calculate a 512-point MDCT
  302. * @param out 256 output frequency coefficients
  303. * @param in 512 windowed input audio samples
  304. */
  305. static void mdct512(AC3MDCTContext *mdct, int32_t *out, int16_t *in)
  306. {
  307. int i, re, im;
  308. int16_t *rot = mdct->rot_tmp;
  309. IComplex *x = mdct->cplx_tmp;
  310. /* shift to simplify computations */
  311. for (i = 0; i < MDCT_SAMPLES/4; i++)
  312. rot[i] = -in[i + 3*MDCT_SAMPLES/4];
  313. memcpy(&rot[MDCT_SAMPLES/4], &in[0], 3*MDCT_SAMPLES/4*sizeof(*in));
  314. /* pre rotation */
  315. for (i = 0; i < MDCT_SAMPLES/4; i++) {
  316. re = ((int)rot[ 2*i] - (int)rot[MDCT_SAMPLES -1-2*i]) >> 1;
  317. im = -((int)rot[MDCT_SAMPLES/2+2*i] - (int)rot[MDCT_SAMPLES/2-1-2*i]) >> 1;
  318. CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
  319. }
  320. fft(x, MDCT_NBITS - 2);
  321. /* post rotation */
  322. for (i = 0; i < MDCT_SAMPLES/4; i++) {
  323. re = x[i].re;
  324. im = x[i].im;
  325. CMUL(out[MDCT_SAMPLES/2-1-2*i], out[2*i], re, im, xsin1[i], xcos1[i]);
  326. }
  327. }
  328. /**
  329. * Apply KBD window to input samples prior to MDCT.
  330. */
  331. static void apply_window(int16_t *output, const int16_t *input,
  332. const int16_t *window, int n)
  333. {
  334. int i;
  335. int n2 = n >> 1;
  336. for (i = 0; i < n2; i++) {
  337. output[i] = MUL16(input[i], window[i]) >> 15;
  338. output[n-i-1] = MUL16(input[n-i-1], window[i]) >> 15;
  339. }
  340. }
  341. /**
  342. * Calculate the log2() of the maximum absolute value in an array.
  343. * @param tab input array
  344. * @param n number of values in the array
  345. * @return log2(max(abs(tab[])))
  346. */
  347. static int log2_tab(int16_t *tab, int n)
  348. {
  349. int i, v;
  350. v = 0;
  351. for (i = 0; i < n; i++)
  352. v |= abs(tab[i]);
  353. return av_log2(v);
  354. }
  355. /**
  356. * Left-shift each value in an array by a specified amount.
  357. * @param tab input array
  358. * @param n number of values in the array
  359. * @param lshift left shift amount. a negative value means right shift.
  360. */
  361. static void lshift_tab(int16_t *tab, int n, int lshift)
  362. {
  363. int i;
  364. if (lshift > 0) {
  365. for (i = 0; i < n; i++)
  366. tab[i] <<= lshift;
  367. } else if (lshift < 0) {
  368. lshift = -lshift;
  369. for (i = 0; i < n; i++)
  370. tab[i] >>= lshift;
  371. }
  372. }
  373. /**
  374. * Normalize the input samples to use the maximum available precision.
  375. * This assumes signed 16-bit input samples. Exponents are reduced by 9 to
  376. * match the 24-bit internal precision for MDCT coefficients.
  377. *
  378. * @return exponent shift
  379. */
  380. static int normalize_samples(AC3EncodeContext *s)
  381. {
  382. int v = 14 - log2_tab(s->windowed_samples, AC3_WINDOW_SIZE);
  383. v = FFMAX(0, v);
  384. lshift_tab(s->windowed_samples, AC3_WINDOW_SIZE, v);
  385. return v - 9;
  386. }
  387. /**
  388. * Apply the MDCT to input samples to generate frequency coefficients.
  389. * This applies the KBD window and normalizes the input to reduce precision
  390. * loss due to fixed-point calculations.
  391. */
  392. static void apply_mdct(AC3EncodeContext *s)
  393. {
  394. int blk, ch;
  395. for (ch = 0; ch < s->channels; ch++) {
  396. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  397. AC3Block *block = &s->blocks[blk];
  398. const int16_t *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
  399. apply_window(s->windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE);
  400. block->exp_shift[ch] = normalize_samples(s);
  401. mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples);
  402. }
  403. }
  404. }
  405. /**
  406. * Initialize exponent tables.
  407. */
  408. static av_cold void exponent_init(AC3EncodeContext *s)
  409. {
  410. int i;
  411. for (i = 73; i < 256; i++) {
  412. exponent_group_tab[0][i] = (i - 1) / 3;
  413. exponent_group_tab[1][i] = (i + 2) / 6;
  414. exponent_group_tab[2][i] = (i + 8) / 12;
  415. }
  416. }
  417. /**
  418. * Extract exponents from the MDCT coefficients.
  419. * This takes into account the normalization that was done to the input samples
  420. * by adjusting the exponents by the exponent shift values.
  421. */
  422. static void extract_exponents(AC3EncodeContext *s)
  423. {
  424. int blk, ch, i;
  425. for (ch = 0; ch < s->channels; ch++) {
  426. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  427. AC3Block *block = &s->blocks[blk];
  428. for (i = 0; i < AC3_MAX_COEFS; i++) {
  429. int e;
  430. int v = abs(block->mdct_coef[ch][i]);
  431. if (v == 0)
  432. e = 24;
  433. else {
  434. e = 23 - av_log2(v) + block->exp_shift[ch];
  435. if (e >= 24) {
  436. e = 24;
  437. block->mdct_coef[ch][i] = 0;
  438. }
  439. }
  440. block->exp[ch][i] = e;
  441. }
  442. }
  443. }
  444. }
  445. /**
  446. * Exponent Difference Threshold.
  447. * New exponents are sent if their SAD exceed this number.
  448. */
  449. #define EXP_DIFF_THRESHOLD 1000
  450. /**
  451. * Calculate exponent strategies for all blocks in a single channel.
  452. */
  453. static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy, uint8_t **exp)
  454. {
  455. int blk, blk1;
  456. int exp_diff;
  457. /* estimate if the exponent variation & decide if they should be
  458. reused in the next frame */
  459. exp_strategy[0] = EXP_NEW;
  460. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
  461. exp_diff = s->dsp.sad[0](NULL, exp[blk], exp[blk-1], 16, 16);
  462. if (exp_diff > EXP_DIFF_THRESHOLD)
  463. exp_strategy[blk] = EXP_NEW;
  464. else
  465. exp_strategy[blk] = EXP_REUSE;
  466. }
  467. /* now select the encoding strategy type : if exponents are often
  468. recoded, we use a coarse encoding */
  469. blk = 0;
  470. while (blk < AC3_MAX_BLOCKS) {
  471. blk1 = blk + 1;
  472. while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
  473. blk1++;
  474. switch (blk1 - blk) {
  475. case 1: exp_strategy[blk] = EXP_D45; break;
  476. case 2:
  477. case 3: exp_strategy[blk] = EXP_D25; break;
  478. default: exp_strategy[blk] = EXP_D15; break;
  479. }
  480. blk = blk1;
  481. }
  482. }
  483. /**
  484. * Calculate exponent strategies for all channels.
  485. * Array arrangement is reversed to simplify the per-channel calculation.
  486. */
  487. static void compute_exp_strategy(AC3EncodeContext *s)
  488. {
  489. uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
  490. uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
  491. int ch, blk;
  492. for (ch = 0; ch < s->fbw_channels; ch++) {
  493. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  494. exp1[ch][blk] = s->blocks[blk].exp[ch];
  495. exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
  496. }
  497. compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
  498. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  499. s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
  500. }
  501. if (s->lfe_on) {
  502. ch = s->lfe_channel;
  503. s->blocks[0].exp_strategy[ch] = EXP_D15;
  504. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
  505. s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
  506. }
  507. }
  508. /**
  509. * Set each encoded exponent in a block to the minimum of itself and the
  510. * exponent in the same frequency bin of a following block.
  511. * exp[i] = min(exp[i], exp1[i]
  512. */
  513. static void exponent_min(uint8_t *exp, uint8_t *exp1, int n)
  514. {
  515. int i;
  516. for (i = 0; i < n; i++) {
  517. if (exp1[i] < exp[i])
  518. exp[i] = exp1[i];
  519. }
  520. }
  521. /**
  522. * Update the exponents so that they are the ones the decoder will decode.
  523. */
  524. static void encode_exponents_blk_ch(uint8_t *exp,
  525. int nb_exps, int exp_strategy)
  526. {
  527. int nb_groups, i, k;
  528. nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
  529. /* for each group, compute the minimum exponent */
  530. switch(exp_strategy) {
  531. case EXP_D25:
  532. for (i = 1, k = 1; i <= nb_groups; i++) {
  533. uint8_t exp_min = exp[k];
  534. if (exp[k+1] < exp_min)
  535. exp_min = exp[k+1];
  536. exp[i] = exp_min;
  537. k += 2;
  538. }
  539. break;
  540. case EXP_D45:
  541. for (i = 1, k = 1; i <= nb_groups; i++) {
  542. uint8_t exp_min = exp[k];
  543. if (exp[k+1] < exp_min)
  544. exp_min = exp[k+1];
  545. if (exp[k+2] < exp_min)
  546. exp_min = exp[k+2];
  547. if (exp[k+3] < exp_min)
  548. exp_min = exp[k+3];
  549. exp[i] = exp_min;
  550. k += 4;
  551. }
  552. break;
  553. }
  554. /* constraint for DC exponent */
  555. if (exp[0] > 15)
  556. exp[0] = 15;
  557. /* decrease the delta between each groups to within 2 so that they can be
  558. differentially encoded */
  559. for (i = 1; i <= nb_groups; i++)
  560. exp[i] = FFMIN(exp[i], exp[i-1] + 2);
  561. i--;
  562. while (--i >= 0)
  563. exp[i] = FFMIN(exp[i], exp[i+1] + 2);
  564. /* now we have the exponent values the decoder will see */
  565. switch (exp_strategy) {
  566. case EXP_D25:
  567. for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
  568. uint8_t exp1 = exp[i];
  569. exp[k--] = exp1;
  570. exp[k--] = exp1;
  571. }
  572. break;
  573. case EXP_D45:
  574. for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
  575. exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
  576. k -= 4;
  577. }
  578. break;
  579. }
  580. }
  581. /**
  582. * Encode exponents from original extracted form to what the decoder will see.
  583. * This copies and groups exponents based on exponent strategy and reduces
  584. * deltas between adjacent exponent groups so that they can be differentially
  585. * encoded.
  586. */
  587. static void encode_exponents(AC3EncodeContext *s)
  588. {
  589. int blk, blk1, blk2, ch;
  590. AC3Block *block, *block1, *block2;
  591. for (ch = 0; ch < s->channels; ch++) {
  592. blk = 0;
  593. block = &s->blocks[0];
  594. while (blk < AC3_MAX_BLOCKS) {
  595. blk1 = blk + 1;
  596. block1 = block + 1;
  597. /* for the EXP_REUSE case we select the min of the exponents */
  598. while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
  599. exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
  600. blk1++;
  601. block1++;
  602. }
  603. encode_exponents_blk_ch(block->exp[ch], s->nb_coefs[ch],
  604. block->exp_strategy[ch]);
  605. /* copy encoded exponents for reuse case */
  606. block2 = block + 1;
  607. for (blk2 = blk+1; blk2 < blk1; blk2++, block2++) {
  608. memcpy(block2->exp[ch], block->exp[ch],
  609. s->nb_coefs[ch] * sizeof(uint8_t));
  610. }
  611. blk = blk1;
  612. block = block1;
  613. }
  614. }
  615. }
  616. /**
  617. * Group exponents.
  618. * 3 delta-encoded exponents are in each 7-bit group. The number of groups
  619. * varies depending on exponent strategy and bandwidth.
  620. */
  621. static void group_exponents(AC3EncodeContext *s)
  622. {
  623. int blk, ch, i;
  624. int group_size, nb_groups, bit_count;
  625. uint8_t *p;
  626. int delta0, delta1, delta2;
  627. int exp0, exp1;
  628. bit_count = 0;
  629. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  630. AC3Block *block = &s->blocks[blk];
  631. for (ch = 0; ch < s->channels; ch++) {
  632. if (block->exp_strategy[ch] == EXP_REUSE) {
  633. continue;
  634. }
  635. group_size = block->exp_strategy[ch] + (block->exp_strategy[ch] == EXP_D45);
  636. nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
  637. bit_count += 4 + (nb_groups * 7);
  638. p = block->exp[ch];
  639. /* DC exponent */
  640. exp1 = *p++;
  641. block->grouped_exp[ch][0] = exp1;
  642. /* remaining exponents are delta encoded */
  643. for (i = 1; i <= nb_groups; i++) {
  644. /* merge three delta in one code */
  645. exp0 = exp1;
  646. exp1 = p[0];
  647. p += group_size;
  648. delta0 = exp1 - exp0 + 2;
  649. exp0 = exp1;
  650. exp1 = p[0];
  651. p += group_size;
  652. delta1 = exp1 - exp0 + 2;
  653. exp0 = exp1;
  654. exp1 = p[0];
  655. p += group_size;
  656. delta2 = exp1 - exp0 + 2;
  657. block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
  658. }
  659. }
  660. }
  661. s->exponent_bits = bit_count;
  662. }
  663. /**
  664. * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
  665. * Extract exponents from MDCT coefficients, calculate exponent strategies,
  666. * and encode final exponents.
  667. */
  668. static void process_exponents(AC3EncodeContext *s)
  669. {
  670. extract_exponents(s);
  671. compute_exp_strategy(s);
  672. encode_exponents(s);
  673. group_exponents(s);
  674. }
  675. /**
  676. * Count frame bits that are based solely on fixed parameters.
  677. * This only has to be run once when the encoder is initialized.
  678. */
  679. static void count_frame_bits_fixed(AC3EncodeContext *s)
  680. {
  681. static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
  682. int blk;
  683. int frame_bits;
  684. /* assumptions:
  685. * no dynamic range codes
  686. * no channel coupling
  687. * no rematrixing
  688. * bit allocation parameters do not change between blocks
  689. * SNR offsets do not change between blocks
  690. * no delta bit allocation
  691. * no skipped data
  692. * no auxilliary data
  693. */
  694. /* header size */
  695. frame_bits = 65;
  696. frame_bits += frame_bits_inc[s->channel_mode];
  697. /* audio blocks */
  698. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  699. frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
  700. if (s->channel_mode == AC3_CHMODE_STEREO) {
  701. frame_bits++; /* rematstr */
  702. if (!blk)
  703. frame_bits += 4;
  704. }
  705. frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
  706. if (s->lfe_on)
  707. frame_bits++; /* lfeexpstr */
  708. frame_bits++; /* baie */
  709. frame_bits++; /* snr */
  710. frame_bits += 2; /* delta / skip */
  711. }
  712. frame_bits++; /* cplinu for block 0 */
  713. /* bit alloc info */
  714. /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
  715. /* csnroffset[6] */
  716. /* (fsnoffset[4] + fgaincod[4]) * c */
  717. frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
  718. /* auxdatae, crcrsv */
  719. frame_bits += 2;
  720. /* CRC */
  721. frame_bits += 16;
  722. s->frame_bits_fixed = frame_bits;
  723. }
  724. /**
  725. * Initialize bit allocation.
  726. * Set default parameter codes and calculate parameter values.
  727. */
  728. static void bit_alloc_init(AC3EncodeContext *s)
  729. {
  730. int ch;
  731. /* init default parameters */
  732. s->slow_decay_code = 2;
  733. s->fast_decay_code = 1;
  734. s->slow_gain_code = 1;
  735. s->db_per_bit_code = 2;
  736. s->floor_code = 4;
  737. for (ch = 0; ch < s->channels; ch++)
  738. s->fast_gain_code[ch] = 4;
  739. /* initial snr offset */
  740. s->coarse_snr_offset = 40;
  741. /* compute real values */
  742. /* currently none of these values change during encoding, so we can just
  743. set them once at initialization */
  744. s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
  745. s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
  746. s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
  747. s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
  748. s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
  749. count_frame_bits_fixed(s);
  750. }
  751. /**
  752. * Count the bits used to encode the frame, minus exponents and mantissas.
  753. * Bits based on fixed parameters have already been counted, so now we just
  754. * have to add the bits based on parameters that change during encoding.
  755. */
  756. static void count_frame_bits(AC3EncodeContext *s)
  757. {
  758. int blk, ch;
  759. int frame_bits = 0;
  760. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  761. uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
  762. for (ch = 0; ch < s->fbw_channels; ch++) {
  763. if (exp_strategy[ch] != EXP_REUSE)
  764. frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
  765. }
  766. }
  767. s->frame_bits = s->frame_bits_fixed + frame_bits;
  768. }
  769. /**
  770. * Calculate the number of bits needed to encode a set of mantissas.
  771. */
  772. static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *bap, int nb_coefs)
  773. {
  774. int bits, b, i;
  775. bits = 0;
  776. for (i = 0; i < nb_coefs; i++) {
  777. b = bap[i];
  778. switch (b) {
  779. case 0:
  780. /* bap=0 mantissas are not encoded */
  781. break;
  782. case 1:
  783. /* 3 mantissas in 5 bits */
  784. if (s->mant1_cnt == 0)
  785. bits += 5;
  786. if (++s->mant1_cnt == 3)
  787. s->mant1_cnt = 0;
  788. break;
  789. case 2:
  790. /* 3 mantissas in 7 bits */
  791. if (s->mant2_cnt == 0)
  792. bits += 7;
  793. if (++s->mant2_cnt == 3)
  794. s->mant2_cnt = 0;
  795. break;
  796. case 3:
  797. bits += 3;
  798. break;
  799. case 4:
  800. /* 2 mantissas in 7 bits */
  801. if (s->mant4_cnt == 0)
  802. bits += 7;
  803. if (++s->mant4_cnt == 2)
  804. s->mant4_cnt = 0;
  805. break;
  806. case 14:
  807. bits += 14;
  808. break;
  809. case 15:
  810. bits += 16;
  811. break;
  812. default:
  813. bits += b - 1;
  814. break;
  815. }
  816. }
  817. return bits;
  818. }
  819. /**
  820. * Calculate masking curve based on the final exponents.
  821. * Also calculate the power spectral densities to use in future calculations.
  822. */
  823. static void bit_alloc_masking(AC3EncodeContext *s)
  824. {
  825. int blk, ch;
  826. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  827. AC3Block *block = &s->blocks[blk];
  828. for (ch = 0; ch < s->channels; ch++) {
  829. if (block->exp_strategy[ch] == EXP_REUSE) {
  830. AC3Block *block1 = &s->blocks[blk-1];
  831. memcpy(block->psd[ch], block1->psd[ch], AC3_MAX_COEFS*sizeof(block->psd[0][0]));
  832. memcpy(block->mask[ch], block1->mask[ch], AC3_CRITICAL_BANDS*sizeof(block->mask[0][0]));
  833. } else {
  834. ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
  835. s->nb_coefs[ch],
  836. block->psd[ch], block->band_psd[ch]);
  837. ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
  838. 0, s->nb_coefs[ch],
  839. ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
  840. ch == s->lfe_channel,
  841. DBA_NONE, 0, NULL, NULL, NULL,
  842. block->mask[ch]);
  843. }
  844. }
  845. }
  846. }
  847. /**
  848. * Ensure that bap for each block and channel point to the current bap_buffer.
  849. * They may have been switched during the bit allocation search.
  850. */
  851. static void reset_block_bap(AC3EncodeContext *s)
  852. {
  853. int blk, ch;
  854. if (s->blocks[0].bap[0] == s->bap_buffer)
  855. return;
  856. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  857. for (ch = 0; ch < s->channels; ch++) {
  858. s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
  859. }
  860. }
  861. }
  862. /**
  863. * Run the bit allocation with a given SNR offset.
  864. * This calculates the bit allocation pointers that will be used to determine
  865. * the quantization of each mantissa.
  866. * @return the number of bits needed for mantissas if the given SNR offset is
  867. * is used.
  868. */
  869. static int bit_alloc(AC3EncodeContext *s,
  870. int snr_offset)
  871. {
  872. int blk, ch;
  873. int mantissa_bits;
  874. snr_offset = (snr_offset - 240) << 2;
  875. reset_block_bap(s);
  876. mantissa_bits = 0;
  877. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  878. AC3Block *block = &s->blocks[blk];
  879. s->mant1_cnt = 0;
  880. s->mant2_cnt = 0;
  881. s->mant4_cnt = 0;
  882. for (ch = 0; ch < s->channels; ch++) {
  883. ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
  884. s->nb_coefs[ch], snr_offset,
  885. s->bit_alloc.floor, ff_ac3_bap_tab,
  886. block->bap[ch]);
  887. mantissa_bits += compute_mantissa_size(s, block->bap[ch], s->nb_coefs[ch]);
  888. }
  889. }
  890. return mantissa_bits;
  891. }
  892. /**
  893. * Constant bitrate bit allocation search.
  894. * Find the largest SNR offset that will allow data to fit in the frame.
  895. */
  896. static int cbr_bit_allocation(AC3EncodeContext *s)
  897. {
  898. int ch;
  899. int bits_left;
  900. int snr_offset;
  901. bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
  902. snr_offset = s->coarse_snr_offset << 4;
  903. while (snr_offset >= 0 &&
  904. bit_alloc(s, snr_offset) > bits_left) {
  905. snr_offset -= 64;
  906. }
  907. if (snr_offset < 0)
  908. return AVERROR(EINVAL);
  909. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  910. while (snr_offset + 64 <= 1023 &&
  911. bit_alloc(s, snr_offset + 64) <= bits_left) {
  912. snr_offset += 64;
  913. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  914. }
  915. while (snr_offset + 16 <= 1023 &&
  916. bit_alloc(s, snr_offset + 16) <= bits_left) {
  917. snr_offset += 16;
  918. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  919. }
  920. while (snr_offset + 4 <= 1023 &&
  921. bit_alloc(s, snr_offset + 4) <= bits_left) {
  922. snr_offset += 4;
  923. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  924. }
  925. while (snr_offset + 1 <= 1023 &&
  926. bit_alloc(s, snr_offset + 1) <= bits_left) {
  927. snr_offset++;
  928. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  929. }
  930. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  931. reset_block_bap(s);
  932. s->coarse_snr_offset = snr_offset >> 4;
  933. for (ch = 0; ch < s->channels; ch++)
  934. s->fine_snr_offset[ch] = snr_offset & 0xF;
  935. return 0;
  936. }
  937. /**
  938. * Perform bit allocation search.
  939. * Finds the SNR offset value that maximizes quality and fits in the specified
  940. * frame size. Output is the SNR offset and a set of bit allocation pointers
  941. * used to quantize the mantissas.
  942. */
  943. static int compute_bit_allocation(AC3EncodeContext *s)
  944. {
  945. count_frame_bits(s);
  946. bit_alloc_masking(s);
  947. return cbr_bit_allocation(s);
  948. }
  949. /**
  950. * Symmetric quantization on 'levels' levels.
  951. */
  952. static inline int sym_quant(int c, int e, int levels)
  953. {
  954. int v;
  955. if (c >= 0) {
  956. v = (levels * (c << e)) >> 24;
  957. v = (v + 1) >> 1;
  958. v = (levels >> 1) + v;
  959. } else {
  960. v = (levels * ((-c) << e)) >> 24;
  961. v = (v + 1) >> 1;
  962. v = (levels >> 1) - v;
  963. }
  964. assert(v >= 0 && v < levels);
  965. return v;
  966. }
  967. /**
  968. * Asymmetric quantization on 2^qbits levels.
  969. */
  970. static inline int asym_quant(int c, int e, int qbits)
  971. {
  972. int lshift, m, v;
  973. lshift = e + qbits - 24;
  974. if (lshift >= 0)
  975. v = c << lshift;
  976. else
  977. v = c >> (-lshift);
  978. /* rounding */
  979. v = (v + 1) >> 1;
  980. m = (1 << (qbits-1));
  981. if (v >= m)
  982. v = m - 1;
  983. assert(v >= -m);
  984. return v & ((1 << qbits)-1);
  985. }
  986. /**
  987. * Quantize a set of mantissas for a single channel in a single block.
  988. */
  989. static void quantize_mantissas_blk_ch(AC3EncodeContext *s,
  990. int32_t *mdct_coef, int8_t exp_shift,
  991. uint8_t *exp, uint8_t *bap,
  992. uint16_t *qmant, int n)
  993. {
  994. int i;
  995. for (i = 0; i < n; i++) {
  996. int v;
  997. int c = mdct_coef[i];
  998. int e = exp[i] - exp_shift;
  999. int b = bap[i];
  1000. switch (b) {
  1001. case 0:
  1002. v = 0;
  1003. break;
  1004. case 1:
  1005. v = sym_quant(c, e, 3);
  1006. switch (s->mant1_cnt) {
  1007. case 0:
  1008. s->qmant1_ptr = &qmant[i];
  1009. v = 9 * v;
  1010. s->mant1_cnt = 1;
  1011. break;
  1012. case 1:
  1013. *s->qmant1_ptr += 3 * v;
  1014. s->mant1_cnt = 2;
  1015. v = 128;
  1016. break;
  1017. default:
  1018. *s->qmant1_ptr += v;
  1019. s->mant1_cnt = 0;
  1020. v = 128;
  1021. break;
  1022. }
  1023. break;
  1024. case 2:
  1025. v = sym_quant(c, e, 5);
  1026. switch (s->mant2_cnt) {
  1027. case 0:
  1028. s->qmant2_ptr = &qmant[i];
  1029. v = 25 * v;
  1030. s->mant2_cnt = 1;
  1031. break;
  1032. case 1:
  1033. *s->qmant2_ptr += 5 * v;
  1034. s->mant2_cnt = 2;
  1035. v = 128;
  1036. break;
  1037. default:
  1038. *s->qmant2_ptr += v;
  1039. s->mant2_cnt = 0;
  1040. v = 128;
  1041. break;
  1042. }
  1043. break;
  1044. case 3:
  1045. v = sym_quant(c, e, 7);
  1046. break;
  1047. case 4:
  1048. v = sym_quant(c, e, 11);
  1049. switch (s->mant4_cnt) {
  1050. case 0:
  1051. s->qmant4_ptr = &qmant[i];
  1052. v = 11 * v;
  1053. s->mant4_cnt = 1;
  1054. break;
  1055. default:
  1056. *s->qmant4_ptr += v;
  1057. s->mant4_cnt = 0;
  1058. v = 128;
  1059. break;
  1060. }
  1061. break;
  1062. case 5:
  1063. v = sym_quant(c, e, 15);
  1064. break;
  1065. case 14:
  1066. v = asym_quant(c, e, 14);
  1067. break;
  1068. case 15:
  1069. v = asym_quant(c, e, 16);
  1070. break;
  1071. default:
  1072. v = asym_quant(c, e, b - 1);
  1073. break;
  1074. }
  1075. qmant[i] = v;
  1076. }
  1077. }
  1078. /**
  1079. * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
  1080. */
  1081. static void quantize_mantissas(AC3EncodeContext *s)
  1082. {
  1083. int blk, ch;
  1084. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1085. AC3Block *block = &s->blocks[blk];
  1086. s->mant1_cnt = s->mant2_cnt = s->mant4_cnt = 0;
  1087. s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL;
  1088. for (ch = 0; ch < s->channels; ch++) {
  1089. quantize_mantissas_blk_ch(s, block->mdct_coef[ch], block->exp_shift[ch],
  1090. block->exp[ch], block->bap[ch],
  1091. block->qmant[ch], s->nb_coefs[ch]);
  1092. }
  1093. }
  1094. }
  1095. /**
  1096. * Write the AC-3 frame header to the output bitstream.
  1097. */
  1098. static void output_frame_header(AC3EncodeContext *s)
  1099. {
  1100. put_bits(&s->pb, 16, 0x0b77); /* frame header */
  1101. put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
  1102. put_bits(&s->pb, 2, s->bit_alloc.sr_code);
  1103. put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
  1104. put_bits(&s->pb, 5, s->bitstream_id);
  1105. put_bits(&s->pb, 3, s->bitstream_mode);
  1106. put_bits(&s->pb, 3, s->channel_mode);
  1107. if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
  1108. put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
  1109. if (s->channel_mode & 0x04)
  1110. put_bits(&s->pb, 2, 1); /* XXX -6 dB */
  1111. if (s->channel_mode == AC3_CHMODE_STEREO)
  1112. put_bits(&s->pb, 2, 0); /* surround not indicated */
  1113. put_bits(&s->pb, 1, s->lfe_on); /* LFE */
  1114. put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
  1115. put_bits(&s->pb, 1, 0); /* no compression control word */
  1116. put_bits(&s->pb, 1, 0); /* no lang code */
  1117. put_bits(&s->pb, 1, 0); /* no audio production info */
  1118. put_bits(&s->pb, 1, 0); /* no copyright */
  1119. put_bits(&s->pb, 1, 1); /* original bitstream */
  1120. put_bits(&s->pb, 1, 0); /* no time code 1 */
  1121. put_bits(&s->pb, 1, 0); /* no time code 2 */
  1122. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  1123. }
  1124. /**
  1125. * Write one audio block to the output bitstream.
  1126. */
  1127. static void output_audio_block(AC3EncodeContext *s,
  1128. int block_num)
  1129. {
  1130. int ch, i, baie, rbnd;
  1131. AC3Block *block = &s->blocks[block_num];
  1132. /* block switching */
  1133. for (ch = 0; ch < s->fbw_channels; ch++)
  1134. put_bits(&s->pb, 1, 0);
  1135. /* dither flags */
  1136. for (ch = 0; ch < s->fbw_channels; ch++)
  1137. put_bits(&s->pb, 1, 1);
  1138. /* dynamic range codes */
  1139. put_bits(&s->pb, 1, 0);
  1140. /* channel coupling */
  1141. if (!block_num) {
  1142. put_bits(&s->pb, 1, 1); /* coupling strategy present */
  1143. put_bits(&s->pb, 1, 0); /* no coupling strategy */
  1144. } else {
  1145. put_bits(&s->pb, 1, 0); /* no new coupling strategy */
  1146. }
  1147. /* stereo rematrixing */
  1148. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1149. if (!block_num) {
  1150. /* first block must define rematrixing (rematstr) */
  1151. put_bits(&s->pb, 1, 1);
  1152. /* dummy rematrixing rematflg(1:4)=0 */
  1153. for (rbnd = 0; rbnd < 4; rbnd++)
  1154. put_bits(&s->pb, 1, 0);
  1155. } else {
  1156. /* no matrixing (but should be used in the future) */
  1157. put_bits(&s->pb, 1, 0);
  1158. }
  1159. }
  1160. /* exponent strategy */
  1161. for (ch = 0; ch < s->fbw_channels; ch++)
  1162. put_bits(&s->pb, 2, block->exp_strategy[ch]);
  1163. if (s->lfe_on)
  1164. put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
  1165. /* bandwidth */
  1166. for (ch = 0; ch < s->fbw_channels; ch++) {
  1167. if (block->exp_strategy[ch] != EXP_REUSE)
  1168. put_bits(&s->pb, 6, s->bandwidth_code[ch]);
  1169. }
  1170. /* exponents */
  1171. for (ch = 0; ch < s->channels; ch++) {
  1172. int nb_groups;
  1173. if (block->exp_strategy[ch] == EXP_REUSE)
  1174. continue;
  1175. /* DC exponent */
  1176. put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
  1177. /* exponent groups */
  1178. nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
  1179. for (i = 1; i <= nb_groups; i++)
  1180. put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
  1181. /* gain range info */
  1182. if (ch != s->lfe_channel)
  1183. put_bits(&s->pb, 2, 0);
  1184. }
  1185. /* bit allocation info */
  1186. baie = (block_num == 0);
  1187. put_bits(&s->pb, 1, baie);
  1188. if (baie) {
  1189. put_bits(&s->pb, 2, s->slow_decay_code);
  1190. put_bits(&s->pb, 2, s->fast_decay_code);
  1191. put_bits(&s->pb, 2, s->slow_gain_code);
  1192. put_bits(&s->pb, 2, s->db_per_bit_code);
  1193. put_bits(&s->pb, 3, s->floor_code);
  1194. }
  1195. /* snr offset */
  1196. put_bits(&s->pb, 1, baie);
  1197. if (baie) {
  1198. put_bits(&s->pb, 6, s->coarse_snr_offset);
  1199. for (ch = 0; ch < s->channels; ch++) {
  1200. put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
  1201. put_bits(&s->pb, 3, s->fast_gain_code[ch]);
  1202. }
  1203. }
  1204. put_bits(&s->pb, 1, 0); /* no delta bit allocation */
  1205. put_bits(&s->pb, 1, 0); /* no data to skip */
  1206. /* mantissas */
  1207. for (ch = 0; ch < s->channels; ch++) {
  1208. int b, q;
  1209. for (i = 0; i < s->nb_coefs[ch]; i++) {
  1210. q = block->qmant[ch][i];
  1211. b = block->bap[ch][i];
  1212. switch (b) {
  1213. case 0: break;
  1214. case 1: if (q != 128) put_bits(&s->pb, 5, q); break;
  1215. case 2: if (q != 128) put_bits(&s->pb, 7, q); break;
  1216. case 3: put_bits(&s->pb, 3, q); break;
  1217. case 4: if (q != 128) put_bits(&s->pb, 7, q); break;
  1218. case 14: put_bits(&s->pb, 14, q); break;
  1219. case 15: put_bits(&s->pb, 16, q); break;
  1220. default: put_bits(&s->pb, b-1, q); break;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. /** CRC-16 Polynomial */
  1226. #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
  1227. static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
  1228. {
  1229. unsigned int c;
  1230. c = 0;
  1231. while (a) {
  1232. if (a & 1)
  1233. c ^= b;
  1234. a = a >> 1;
  1235. b = b << 1;
  1236. if (b & (1 << 16))
  1237. b ^= poly;
  1238. }
  1239. return c;
  1240. }
  1241. static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
  1242. {
  1243. unsigned int r;
  1244. r = 1;
  1245. while (n) {
  1246. if (n & 1)
  1247. r = mul_poly(r, a, poly);
  1248. a = mul_poly(a, a, poly);
  1249. n >>= 1;
  1250. }
  1251. return r;
  1252. }
  1253. /**
  1254. * Fill the end of the frame with 0's and compute the two CRCs.
  1255. */
  1256. static void output_frame_end(AC3EncodeContext *s)
  1257. {
  1258. int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;
  1259. uint8_t *frame;
  1260. frame_size = s->frame_size;
  1261. frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1;
  1262. /* pad the remainder of the frame with zeros */
  1263. flush_put_bits(&s->pb);
  1264. frame = s->pb.buf;
  1265. pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
  1266. assert(pad_bytes >= 0);
  1267. if (pad_bytes > 0)
  1268. memset(put_bits_ptr(&s->pb), 0, pad_bytes);
  1269. /* compute crc1 */
  1270. /* this is not so easy because it is at the beginning of the data... */
  1271. crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
  1272. frame + 4, frame_size_58 - 4));
  1273. /* XXX: could precompute crc_inv */
  1274. crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
  1275. crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
  1276. AV_WB16(frame + 2, crc1);
  1277. /* compute crc2 */
  1278. crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
  1279. frame + frame_size_58,
  1280. frame_size - frame_size_58 - 2));
  1281. AV_WB16(frame + frame_size - 2, crc2);
  1282. }
  1283. /**
  1284. * Write the frame to the output bitstream.
  1285. */
  1286. static void output_frame(AC3EncodeContext *s,
  1287. unsigned char *frame)
  1288. {
  1289. int blk;
  1290. init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
  1291. output_frame_header(s);
  1292. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  1293. output_audio_block(s, blk);
  1294. output_frame_end(s);
  1295. }
  1296. /**
  1297. * Encode a single AC-3 frame.
  1298. */
  1299. static int ac3_encode_frame(AVCodecContext *avctx,
  1300. unsigned char *frame, int buf_size, void *data)
  1301. {
  1302. AC3EncodeContext *s = avctx->priv_data;
  1303. const int16_t *samples = data;
  1304. int ret;
  1305. if (s->bit_alloc.sr_code == 1)
  1306. adjust_frame_size(s);
  1307. deinterleave_input_samples(s, samples);
  1308. apply_mdct(s);
  1309. process_exponents(s);
  1310. ret = compute_bit_allocation(s);
  1311. if (ret) {
  1312. av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
  1313. return ret;
  1314. }
  1315. quantize_mantissas(s);
  1316. output_frame(s, frame);
  1317. return s->frame_size;
  1318. }
  1319. /**
  1320. * Finalize encoding and free any memory allocated by the encoder.
  1321. */
  1322. static av_cold int ac3_encode_close(AVCodecContext *avctx)
  1323. {
  1324. int blk, ch;
  1325. AC3EncodeContext *s = avctx->priv_data;
  1326. for (ch = 0; ch < s->channels; ch++)
  1327. av_freep(&s->planar_samples[ch]);
  1328. av_freep(&s->planar_samples);
  1329. av_freep(&s->bap_buffer);
  1330. av_freep(&s->bap1_buffer);
  1331. av_freep(&s->mdct_coef_buffer);
  1332. av_freep(&s->exp_buffer);
  1333. av_freep(&s->grouped_exp_buffer);
  1334. av_freep(&s->psd_buffer);
  1335. av_freep(&s->band_psd_buffer);
  1336. av_freep(&s->mask_buffer);
  1337. av_freep(&s->qmant_buffer);
  1338. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1339. AC3Block *block = &s->blocks[blk];
  1340. av_freep(&block->bap);
  1341. av_freep(&block->mdct_coef);
  1342. av_freep(&block->exp);
  1343. av_freep(&block->grouped_exp);
  1344. av_freep(&block->psd);
  1345. av_freep(&block->band_psd);
  1346. av_freep(&block->mask);
  1347. av_freep(&block->qmant);
  1348. }
  1349. mdct_end(&s->mdct);
  1350. av_freep(&avctx->coded_frame);
  1351. return 0;
  1352. }
  1353. /**
  1354. * Set channel information during initialization.
  1355. */
  1356. static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
  1357. int64_t *channel_layout)
  1358. {
  1359. int ch_layout;
  1360. if (channels < 1 || channels > AC3_MAX_CHANNELS)
  1361. return AVERROR(EINVAL);
  1362. if ((uint64_t)*channel_layout > 0x7FF)
  1363. return AVERROR(EINVAL);
  1364. ch_layout = *channel_layout;
  1365. if (!ch_layout)
  1366. ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
  1367. if (av_get_channel_layout_nb_channels(ch_layout) != channels)
  1368. return AVERROR(EINVAL);
  1369. s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);
  1370. s->channels = channels;
  1371. s->fbw_channels = channels - s->lfe_on;
  1372. s->lfe_channel = s->lfe_on ? s->fbw_channels : -1;
  1373. if (s->lfe_on)
  1374. ch_layout -= AV_CH_LOW_FREQUENCY;
  1375. switch (ch_layout) {
  1376. case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
  1377. case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
  1378. case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
  1379. case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
  1380. case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
  1381. case AV_CH_LAYOUT_QUAD:
  1382. case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
  1383. case AV_CH_LAYOUT_5POINT0:
  1384. case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
  1385. default:
  1386. return AVERROR(EINVAL);
  1387. }
  1388. s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
  1389. *channel_layout = ch_layout;
  1390. if (s->lfe_on)
  1391. *channel_layout |= AV_CH_LOW_FREQUENCY;
  1392. return 0;
  1393. }
  1394. static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
  1395. {
  1396. int i, ret;
  1397. /* validate channel layout */
  1398. if (!avctx->channel_layout) {
  1399. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  1400. "encoder will guess the layout, but it "
  1401. "might be incorrect.\n");
  1402. }
  1403. ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
  1404. if (ret) {
  1405. av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
  1406. return ret;
  1407. }
  1408. /* validate sample rate */
  1409. for (i = 0; i < 9; i++) {
  1410. if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
  1411. break;
  1412. }
  1413. if (i == 9) {
  1414. av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
  1415. return AVERROR(EINVAL);
  1416. }
  1417. s->sample_rate = avctx->sample_rate;
  1418. s->bit_alloc.sr_shift = i % 3;
  1419. s->bit_alloc.sr_code = i / 3;
  1420. /* validate bit rate */
  1421. for (i = 0; i < 19; i++) {
  1422. if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
  1423. break;
  1424. }
  1425. if (i == 19) {
  1426. av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
  1427. return AVERROR(EINVAL);
  1428. }
  1429. s->bit_rate = avctx->bit_rate;
  1430. s->frame_size_code = i << 1;
  1431. return 0;
  1432. }
  1433. /**
  1434. * Set bandwidth for all channels.
  1435. * The user can optionally supply a cutoff frequency. Otherwise an appropriate
  1436. * default value will be used.
  1437. */
  1438. static av_cold void set_bandwidth(AC3EncodeContext *s, int cutoff)
  1439. {
  1440. int ch, bw_code;
  1441. if (cutoff) {
  1442. /* calculate bandwidth based on user-specified cutoff frequency */
  1443. int fbw_coeffs;
  1444. cutoff = av_clip(cutoff, 1, s->sample_rate >> 1);
  1445. fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
  1446. bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
  1447. } else {
  1448. /* use default bandwidth setting */
  1449. /* XXX: should compute the bandwidth according to the frame
  1450. size, so that we avoid annoying high frequency artifacts */
  1451. bw_code = 50;
  1452. }
  1453. /* set number of coefficients for each channel */
  1454. for (ch = 0; ch < s->fbw_channels; ch++) {
  1455. s->bandwidth_code[ch] = bw_code;
  1456. s->nb_coefs[ch] = bw_code * 3 + 73;
  1457. }
  1458. if (s->lfe_on)
  1459. s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
  1460. }
  1461. static av_cold int allocate_buffers(AVCodecContext *avctx)
  1462. {
  1463. int blk, ch;
  1464. AC3EncodeContext *s = avctx->priv_data;
  1465. FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
  1466. alloc_fail);
  1467. for (ch = 0; ch < s->channels; ch++) {
  1468. FF_ALLOCZ_OR_GOTO(avctx, s->planar_samples[ch],
  1469. (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
  1470. alloc_fail);
  1471. }
  1472. FF_ALLOC_OR_GOTO(avctx, s->bap_buffer, AC3_MAX_BLOCKS * s->channels *
  1473. AC3_MAX_COEFS * sizeof(*s->bap_buffer), alloc_fail);
  1474. FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
  1475. AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
  1476. FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
  1477. AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
  1478. FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
  1479. AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
  1480. FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
  1481. 128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
  1482. FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
  1483. AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
  1484. FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
  1485. 64 * sizeof(*s->band_psd_buffer), alloc_fail);
  1486. FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
  1487. 64 * sizeof(*s->mask_buffer), alloc_fail);
  1488. FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
  1489. AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
  1490. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  1491. AC3Block *block = &s->blocks[blk];
  1492. FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
  1493. alloc_fail);
  1494. FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
  1495. alloc_fail);
  1496. FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
  1497. alloc_fail);
  1498. FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
  1499. alloc_fail);
  1500. FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
  1501. alloc_fail);
  1502. FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
  1503. alloc_fail);
  1504. FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
  1505. alloc_fail);
  1506. FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
  1507. alloc_fail);
  1508. for (ch = 0; ch < s->channels; ch++) {
  1509. block->bap[ch] = &s->bap_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1510. block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1511. block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1512. block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * s->channels + ch)];
  1513. block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1514. block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * s->channels + ch)];
  1515. block->mask[ch] = &s->mask_buffer [64 * (blk * s->channels + ch)];
  1516. block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
  1517. }
  1518. }
  1519. return 0;
  1520. alloc_fail:
  1521. return AVERROR(ENOMEM);
  1522. }
  1523. /**
  1524. * Initialize the encoder.
  1525. */
  1526. static av_cold int ac3_encode_init(AVCodecContext *avctx)
  1527. {
  1528. AC3EncodeContext *s = avctx->priv_data;
  1529. int ret;
  1530. avctx->frame_size = AC3_FRAME_SIZE;
  1531. ac3_common_init();
  1532. ret = validate_options(avctx, s);
  1533. if (ret)
  1534. return ret;
  1535. s->bitstream_id = 8 + s->bit_alloc.sr_shift;
  1536. s->bitstream_mode = 0; /* complete main audio service */
  1537. s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
  1538. s->bits_written = 0;
  1539. s->samples_written = 0;
  1540. s->frame_size = s->frame_size_min;
  1541. set_bandwidth(s, avctx->cutoff);
  1542. exponent_init(s);
  1543. bit_alloc_init(s);
  1544. s->mdct.avctx = avctx;
  1545. ret = mdct_init(&s->mdct, 9);
  1546. if (ret)
  1547. goto init_fail;
  1548. ret = allocate_buffers(avctx);
  1549. if (ret)
  1550. goto init_fail;
  1551. avctx->coded_frame= avcodec_alloc_frame();
  1552. dsputil_init(&s->dsp, avctx);
  1553. return 0;
  1554. init_fail:
  1555. ac3_encode_close(avctx);
  1556. return ret;
  1557. }
  1558. #ifdef TEST
  1559. /*************************************************************************/
  1560. /* TEST */
  1561. #include "libavutil/lfg.h"
  1562. #define FN (MDCT_SAMPLES/4)
  1563. static void fft_test(AVLFG *lfg)
  1564. {
  1565. IComplex in[FN], in1[FN];
  1566. int k, n, i;
  1567. float sum_re, sum_im, a;
  1568. for (i = 0; i < FN; i++) {
  1569. in[i].re = av_lfg_get(lfg) % 65535 - 32767;
  1570. in[i].im = av_lfg_get(lfg) % 65535 - 32767;
  1571. in1[i] = in[i];
  1572. }
  1573. fft(in, 7);
  1574. /* do it by hand */
  1575. for (k = 0; k < FN; k++) {
  1576. sum_re = 0;
  1577. sum_im = 0;
  1578. for (n = 0; n < FN; n++) {
  1579. a = -2 * M_PI * (n * k) / FN;
  1580. sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
  1581. sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
  1582. }
  1583. av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
  1584. k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
  1585. }
  1586. }
  1587. static void mdct_test(AVLFG *lfg)
  1588. {
  1589. int16_t input[MDCT_SAMPLES];
  1590. int32_t output[AC3_MAX_COEFS];
  1591. float input1[MDCT_SAMPLES];
  1592. float output1[AC3_MAX_COEFS];
  1593. float s, a, err, e, emax;
  1594. int i, k, n;
  1595. for (i = 0; i < MDCT_SAMPLES; i++) {
  1596. input[i] = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
  1597. input1[i] = input[i];
  1598. }
  1599. mdct512(output, input);
  1600. /* do it by hand */
  1601. for (k = 0; k < AC3_MAX_COEFS; k++) {
  1602. s = 0;
  1603. for (n = 0; n < MDCT_SAMPLES; n++) {
  1604. a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
  1605. s += input1[n] * cos(a);
  1606. }
  1607. output1[k] = -2 * s / MDCT_SAMPLES;
  1608. }
  1609. err = 0;
  1610. emax = 0;
  1611. for (i = 0; i < AC3_MAX_COEFS; i++) {
  1612. av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
  1613. e = output[i] - output1[i];
  1614. if (e > emax)
  1615. emax = e;
  1616. err += e * e;
  1617. }
  1618. av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
  1619. }
  1620. int main(void)
  1621. {
  1622. AVLFG lfg;
  1623. av_log_set_level(AV_LOG_DEBUG);
  1624. mdct_init(9);
  1625. fft_test(&lfg);
  1626. mdct_test(&lfg);
  1627. return 0;
  1628. }
  1629. #endif /* TEST */
  1630. AVCodec ac3_encoder = {
  1631. "ac3",
  1632. AVMEDIA_TYPE_AUDIO,
  1633. CODEC_ID_AC3,
  1634. sizeof(AC3EncodeContext),
  1635. ac3_encode_init,
  1636. ac3_encode_frame,
  1637. ac3_encode_close,
  1638. NULL,
  1639. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  1640. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
  1641. .channel_layouts = (const int64_t[]){
  1642. AV_CH_LAYOUT_MONO,
  1643. AV_CH_LAYOUT_STEREO,
  1644. AV_CH_LAYOUT_2_1,
  1645. AV_CH_LAYOUT_SURROUND,
  1646. AV_CH_LAYOUT_2_2,
  1647. AV_CH_LAYOUT_QUAD,
  1648. AV_CH_LAYOUT_4POINT0,
  1649. AV_CH_LAYOUT_5POINT0,
  1650. AV_CH_LAYOUT_5POINT0_BACK,
  1651. (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY),
  1652. (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY),
  1653. (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY),
  1654. (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
  1655. (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY),
  1656. (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY),
  1657. (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY),
  1658. AV_CH_LAYOUT_5POINT1,
  1659. AV_CH_LAYOUT_5POINT1_BACK,
  1660. 0 },
  1661. };