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.

1902 lines
57KB

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