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.

1887 lines
56KB

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