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.

1925 lines
57KB

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