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.

1203 lines
40KB

  1. /*
  2. * AC-3 Audio Decoder
  3. * This code is developed as part of Google Summer of Code 2006 Program.
  4. *
  5. * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
  6. * Copyright (c) 2007 Justin Ruggles
  7. *
  8. * Portions of this code are derived from liba52
  9. * http://liba52.sourceforge.net
  10. * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  11. * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  12. *
  13. * This file is part of FFmpeg.
  14. *
  15. * FFmpeg is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2 of the License, or (at your option) any later version.
  19. *
  20. * FFmpeg is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public
  26. * License along with FFmpeg; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <stdio.h>
  30. #include <stddef.h>
  31. #include <math.h>
  32. #include <string.h>
  33. #include "libavutil/crc.h"
  34. #include "libavutil/random.h"
  35. #include "avcodec.h"
  36. #include "ac3_parser.h"
  37. #include "bitstream.h"
  38. #include "dsputil.h"
  39. #include "ac3dec.h"
  40. #include "ac3dec_data.h"
  41. /** Maximum possible frame size when the specification limit is ignored */
  42. #define AC3_MAX_FRAME_SIZE 21695
  43. /** table for grouping exponents */
  44. static uint8_t exp_ungroup_tab[128][3];
  45. /** tables for ungrouping mantissas */
  46. static int b1_mantissas[32][3];
  47. static int b2_mantissas[128][3];
  48. static int b3_mantissas[8];
  49. static int b4_mantissas[128][2];
  50. static int b5_mantissas[16];
  51. /**
  52. * Quantization table: levels for symmetric. bits for asymmetric.
  53. * reference: Table 7.18 Mapping of bap to Quantizer
  54. */
  55. static const uint8_t quantization_tab[16] = {
  56. 0, 3, 5, 7, 11, 15,
  57. 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
  58. };
  59. /** dynamic range table. converts codes to scale factors. */
  60. static float dynamic_range_tab[256];
  61. /** Adjustments in dB gain */
  62. #define LEVEL_PLUS_3DB 1.4142135623730950
  63. #define LEVEL_PLUS_1POINT5DB 1.1892071150027209
  64. #define LEVEL_MINUS_1POINT5DB 0.8408964152537145
  65. #define LEVEL_MINUS_3DB 0.7071067811865476
  66. #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
  67. #define LEVEL_MINUS_6DB 0.5000000000000000
  68. #define LEVEL_MINUS_9DB 0.3535533905932738
  69. #define LEVEL_ZERO 0.0000000000000000
  70. #define LEVEL_ONE 1.0000000000000000
  71. static const float gain_levels[9] = {
  72. LEVEL_PLUS_3DB,
  73. LEVEL_PLUS_1POINT5DB,
  74. LEVEL_ONE,
  75. LEVEL_MINUS_1POINT5DB,
  76. LEVEL_MINUS_3DB,
  77. LEVEL_MINUS_4POINT5DB,
  78. LEVEL_MINUS_6DB,
  79. LEVEL_ZERO,
  80. LEVEL_MINUS_9DB
  81. };
  82. /**
  83. * Table for center mix levels
  84. * reference: Section 5.4.2.4 cmixlev
  85. */
  86. static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
  87. /**
  88. * Table for surround mix levels
  89. * reference: Section 5.4.2.5 surmixlev
  90. */
  91. static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
  92. /**
  93. * Table for default stereo downmixing coefficients
  94. * reference: Section 7.8.2 Downmixing Into Two Channels
  95. */
  96. static const uint8_t ac3_default_coeffs[8][5][2] = {
  97. { { 2, 7 }, { 7, 2 }, },
  98. { { 4, 4 }, },
  99. { { 2, 7 }, { 7, 2 }, },
  100. { { 2, 7 }, { 5, 5 }, { 7, 2 }, },
  101. { { 2, 7 }, { 7, 2 }, { 6, 6 }, },
  102. { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 8, 8 }, },
  103. { { 2, 7 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
  104. { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
  105. };
  106. /**
  107. * Symmetrical Dequantization
  108. * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
  109. * Tables 7.19 to 7.23
  110. */
  111. static inline int
  112. symmetric_dequant(int code, int levels)
  113. {
  114. return ((code - (levels >> 1)) << 24) / levels;
  115. }
  116. /*
  117. * Initialize tables at runtime.
  118. */
  119. static av_cold void ac3_tables_init(void)
  120. {
  121. int i;
  122. /* generate grouped mantissa tables
  123. reference: Section 7.3.5 Ungrouping of Mantissas */
  124. for(i=0; i<32; i++) {
  125. /* bap=1 mantissas */
  126. b1_mantissas[i][0] = symmetric_dequant( i / 9 , 3);
  127. b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
  128. b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
  129. }
  130. for(i=0; i<128; i++) {
  131. /* bap=2 mantissas */
  132. b2_mantissas[i][0] = symmetric_dequant( i / 25 , 5);
  133. b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
  134. b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
  135. /* bap=4 mantissas */
  136. b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
  137. b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
  138. }
  139. /* generate ungrouped mantissa tables
  140. reference: Tables 7.21 and 7.23 */
  141. for(i=0; i<7; i++) {
  142. /* bap=3 mantissas */
  143. b3_mantissas[i] = symmetric_dequant(i, 7);
  144. }
  145. for(i=0; i<15; i++) {
  146. /* bap=5 mantissas */
  147. b5_mantissas[i] = symmetric_dequant(i, 15);
  148. }
  149. /* generate dynamic range table
  150. reference: Section 7.7.1 Dynamic Range Control */
  151. for(i=0; i<256; i++) {
  152. int v = (i >> 5) - ((i >> 7) << 3) - 5;
  153. dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
  154. }
  155. /* generate exponent tables
  156. reference: Section 7.1.3 Exponent Decoding */
  157. for(i=0; i<128; i++) {
  158. exp_ungroup_tab[i][0] = i / 25;
  159. exp_ungroup_tab[i][1] = (i % 25) / 5;
  160. exp_ungroup_tab[i][2] = (i % 25) % 5;
  161. }
  162. }
  163. /**
  164. * AVCodec initialization
  165. */
  166. static av_cold int ac3_decode_init(AVCodecContext *avctx)
  167. {
  168. AC3DecodeContext *s = avctx->priv_data;
  169. s->avctx = avctx;
  170. ac3_common_init();
  171. ac3_tables_init();
  172. ff_mdct_init(&s->imdct_256, 8, 1);
  173. ff_mdct_init(&s->imdct_512, 9, 1);
  174. ff_kbd_window_init(s->window, 5.0, 256);
  175. dsputil_init(&s->dsp, avctx);
  176. av_init_random(0, &s->dith_state);
  177. /* set bias values for float to int16 conversion */
  178. if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
  179. s->add_bias = 385.0f;
  180. s->mul_bias = 1.0f;
  181. } else {
  182. s->add_bias = 0.0f;
  183. s->mul_bias = 32767.0f;
  184. }
  185. /* allow downmixing to stereo or mono */
  186. if (avctx->channels > 0 && avctx->request_channels > 0 &&
  187. avctx->request_channels < avctx->channels &&
  188. avctx->request_channels <= 2) {
  189. avctx->channels = avctx->request_channels;
  190. }
  191. s->downmixed = 1;
  192. /* allocate context input buffer */
  193. if (avctx->error_resilience >= FF_ER_CAREFUL) {
  194. s->input_buffer = av_mallocz(AC3_MAX_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  195. if (!s->input_buffer)
  196. return AVERROR_NOMEM;
  197. }
  198. return 0;
  199. }
  200. /**
  201. * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream.
  202. * GetBitContext within AC3DecodeContext must point to
  203. * start of the synchronized ac3 bitstream.
  204. */
  205. static int ac3_parse_header(AC3DecodeContext *s)
  206. {
  207. GetBitContext *gbc = &s->gbc;
  208. int i;
  209. /* read the rest of the bsi. read twice for dual mono mode. */
  210. i = !(s->channel_mode);
  211. do {
  212. skip_bits(gbc, 5); // skip dialog normalization
  213. if (get_bits1(gbc))
  214. skip_bits(gbc, 8); //skip compression
  215. if (get_bits1(gbc))
  216. skip_bits(gbc, 8); //skip language code
  217. if (get_bits1(gbc))
  218. skip_bits(gbc, 7); //skip audio production information
  219. } while (i--);
  220. skip_bits(gbc, 2); //skip copyright bit and original bitstream bit
  221. /* skip the timecodes (or extra bitstream information for Alternate Syntax)
  222. TODO: read & use the xbsi1 downmix levels */
  223. if (get_bits1(gbc))
  224. skip_bits(gbc, 14); //skip timecode1 / xbsi1
  225. if (get_bits1(gbc))
  226. skip_bits(gbc, 14); //skip timecode2 / xbsi2
  227. /* skip additional bitstream info */
  228. if (get_bits1(gbc)) {
  229. i = get_bits(gbc, 6);
  230. do {
  231. skip_bits(gbc, 8);
  232. } while(i--);
  233. }
  234. return 0;
  235. }
  236. /**
  237. * Common function to parse AC3 or E-AC3 frame header
  238. */
  239. static int parse_frame_header(AC3DecodeContext *s)
  240. {
  241. AC3HeaderInfo hdr;
  242. GetBitContext *gbc = &s->gbc;
  243. int err;
  244. err = ff_ac3_parse_header(gbc, &hdr);
  245. if(err)
  246. return err;
  247. if(hdr.bitstream_id > 10)
  248. return AC3_PARSE_ERROR_BSID;
  249. /* get decoding parameters from header info */
  250. s->bit_alloc_params.sr_code = hdr.sr_code;
  251. s->channel_mode = hdr.channel_mode;
  252. s->lfe_on = hdr.lfe_on;
  253. s->bit_alloc_params.sr_shift = hdr.sr_shift;
  254. s->sample_rate = hdr.sample_rate;
  255. s->bit_rate = hdr.bit_rate;
  256. s->channels = hdr.channels;
  257. s->fbw_channels = s->channels - s->lfe_on;
  258. s->lfe_ch = s->fbw_channels + 1;
  259. s->frame_size = hdr.frame_size;
  260. s->center_mix_level = hdr.center_mix_level;
  261. s->surround_mix_level = hdr.surround_mix_level;
  262. s->num_blocks = hdr.num_blocks;
  263. s->frame_type = hdr.frame_type;
  264. s->substreamid = hdr.substreamid;
  265. if(s->lfe_on) {
  266. s->start_freq[s->lfe_ch] = 0;
  267. s->end_freq[s->lfe_ch] = 7;
  268. s->num_exp_groups[s->lfe_ch] = 2;
  269. s->channel_in_cpl[s->lfe_ch] = 0;
  270. }
  271. return ac3_parse_header(s);
  272. }
  273. /**
  274. * Set stereo downmixing coefficients based on frame header info.
  275. * reference: Section 7.8.2 Downmixing Into Two Channels
  276. */
  277. static void set_downmix_coeffs(AC3DecodeContext *s)
  278. {
  279. int i;
  280. float cmix = gain_levels[center_levels[s->center_mix_level]];
  281. float smix = gain_levels[surround_levels[s->surround_mix_level]];
  282. for(i=0; i<s->fbw_channels; i++) {
  283. s->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
  284. s->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
  285. }
  286. if(s->channel_mode > 1 && s->channel_mode & 1) {
  287. s->downmix_coeffs[1][0] = s->downmix_coeffs[1][1] = cmix;
  288. }
  289. if(s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
  290. int nf = s->channel_mode - 2;
  291. s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf][1] = smix * LEVEL_MINUS_3DB;
  292. }
  293. if(s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
  294. int nf = s->channel_mode - 4;
  295. s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf+1][1] = smix;
  296. }
  297. /* calculate adjustment needed for each channel to avoid clipping */
  298. s->downmix_coeff_adjust[0] = s->downmix_coeff_adjust[1] = 0.0f;
  299. for(i=0; i<s->fbw_channels; i++) {
  300. s->downmix_coeff_adjust[0] += s->downmix_coeffs[i][0];
  301. s->downmix_coeff_adjust[1] += s->downmix_coeffs[i][1];
  302. }
  303. s->downmix_coeff_adjust[0] = 1.0f / s->downmix_coeff_adjust[0];
  304. s->downmix_coeff_adjust[1] = 1.0f / s->downmix_coeff_adjust[1];
  305. }
  306. /**
  307. * Decode the grouped exponents according to exponent strategy.
  308. * reference: Section 7.1.3 Exponent Decoding
  309. */
  310. static void decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
  311. uint8_t absexp, int8_t *dexps)
  312. {
  313. int i, j, grp, group_size;
  314. int dexp[256];
  315. int expacc, prevexp;
  316. /* unpack groups */
  317. group_size = exp_strategy + (exp_strategy == EXP_D45);
  318. for(grp=0,i=0; grp<ngrps; grp++) {
  319. expacc = get_bits(gbc, 7);
  320. dexp[i++] = exp_ungroup_tab[expacc][0];
  321. dexp[i++] = exp_ungroup_tab[expacc][1];
  322. dexp[i++] = exp_ungroup_tab[expacc][2];
  323. }
  324. /* convert to absolute exps and expand groups */
  325. prevexp = absexp;
  326. for(i=0; i<ngrps*3; i++) {
  327. prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
  328. for(j=0; j<group_size; j++) {
  329. dexps[(i*group_size)+j] = prevexp;
  330. }
  331. }
  332. }
  333. /**
  334. * Generate transform coefficients for each coupled channel in the coupling
  335. * range using the coupling coefficients and coupling coordinates.
  336. * reference: Section 7.4.3 Coupling Coordinate Format
  337. */
  338. static void uncouple_channels(AC3DecodeContext *s)
  339. {
  340. int i, j, ch, bnd, subbnd;
  341. subbnd = -1;
  342. i = s->start_freq[CPL_CH];
  343. for(bnd=0; bnd<s->num_cpl_bands; bnd++) {
  344. do {
  345. subbnd++;
  346. for(j=0; j<12; j++) {
  347. for(ch=1; ch<=s->fbw_channels; ch++) {
  348. if(s->channel_in_cpl[ch]) {
  349. s->fixed_coeffs[ch][i] = ((int64_t)s->fixed_coeffs[CPL_CH][i] * (int64_t)s->cpl_coords[ch][bnd]) >> 23;
  350. if (ch == 2 && s->phase_flags[bnd])
  351. s->fixed_coeffs[ch][i] = -s->fixed_coeffs[ch][i];
  352. }
  353. }
  354. i++;
  355. }
  356. } while(s->cpl_band_struct[subbnd]);
  357. }
  358. }
  359. /**
  360. * Grouped mantissas for 3-level 5-level and 11-level quantization
  361. */
  362. typedef struct {
  363. int b1_mant[3];
  364. int b2_mant[3];
  365. int b4_mant[2];
  366. int b1ptr;
  367. int b2ptr;
  368. int b4ptr;
  369. } mant_groups;
  370. /**
  371. * Get the transform coefficients for a particular channel
  372. * reference: Section 7.3 Quantization and Decoding of Mantissas
  373. */
  374. static void get_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
  375. {
  376. GetBitContext *gbc = &s->gbc;
  377. int i, gcode, tbap, start, end;
  378. uint8_t *exps;
  379. uint8_t *bap;
  380. int *coeffs;
  381. exps = s->dexps[ch_index];
  382. bap = s->bap[ch_index];
  383. coeffs = s->fixed_coeffs[ch_index];
  384. start = s->start_freq[ch_index];
  385. end = s->end_freq[ch_index];
  386. for (i = start; i < end; i++) {
  387. tbap = bap[i];
  388. switch (tbap) {
  389. case 0:
  390. coeffs[i] = (av_random(&s->dith_state) & 0x7FFFFF) - 4194304;
  391. break;
  392. case 1:
  393. if(m->b1ptr > 2) {
  394. gcode = get_bits(gbc, 5);
  395. m->b1_mant[0] = b1_mantissas[gcode][0];
  396. m->b1_mant[1] = b1_mantissas[gcode][1];
  397. m->b1_mant[2] = b1_mantissas[gcode][2];
  398. m->b1ptr = 0;
  399. }
  400. coeffs[i] = m->b1_mant[m->b1ptr++];
  401. break;
  402. case 2:
  403. if(m->b2ptr > 2) {
  404. gcode = get_bits(gbc, 7);
  405. m->b2_mant[0] = b2_mantissas[gcode][0];
  406. m->b2_mant[1] = b2_mantissas[gcode][1];
  407. m->b2_mant[2] = b2_mantissas[gcode][2];
  408. m->b2ptr = 0;
  409. }
  410. coeffs[i] = m->b2_mant[m->b2ptr++];
  411. break;
  412. case 3:
  413. coeffs[i] = b3_mantissas[get_bits(gbc, 3)];
  414. break;
  415. case 4:
  416. if(m->b4ptr > 1) {
  417. gcode = get_bits(gbc, 7);
  418. m->b4_mant[0] = b4_mantissas[gcode][0];
  419. m->b4_mant[1] = b4_mantissas[gcode][1];
  420. m->b4ptr = 0;
  421. }
  422. coeffs[i] = m->b4_mant[m->b4ptr++];
  423. break;
  424. case 5:
  425. coeffs[i] = b5_mantissas[get_bits(gbc, 4)];
  426. break;
  427. default: {
  428. /* asymmetric dequantization */
  429. int qlevel = quantization_tab[tbap];
  430. coeffs[i] = get_sbits(gbc, qlevel) << (24 - qlevel);
  431. break;
  432. }
  433. }
  434. coeffs[i] >>= exps[i];
  435. }
  436. }
  437. /**
  438. * Remove random dithering from coefficients with zero-bit mantissas
  439. * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
  440. */
  441. static void remove_dithering(AC3DecodeContext *s) {
  442. int ch, i;
  443. int end=0;
  444. int *coeffs;
  445. uint8_t *bap;
  446. for(ch=1; ch<=s->fbw_channels; ch++) {
  447. if(!s->dither_flag[ch]) {
  448. coeffs = s->fixed_coeffs[ch];
  449. bap = s->bap[ch];
  450. if(s->channel_in_cpl[ch])
  451. end = s->start_freq[CPL_CH];
  452. else
  453. end = s->end_freq[ch];
  454. for(i=0; i<end; i++) {
  455. if(!bap[i])
  456. coeffs[i] = 0;
  457. }
  458. if(s->channel_in_cpl[ch]) {
  459. bap = s->bap[CPL_CH];
  460. for(; i<s->end_freq[CPL_CH]; i++) {
  461. if(!bap[i])
  462. coeffs[i] = 0;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * Get the transform coefficients.
  470. */
  471. static void get_transform_coeffs(AC3DecodeContext *s)
  472. {
  473. int ch, end;
  474. int got_cplchan = 0;
  475. mant_groups m;
  476. m.b1ptr = m.b2ptr = m.b4ptr = 3;
  477. for (ch = 1; ch <= s->channels; ch++) {
  478. /* transform coefficients for full-bandwidth channel */
  479. get_transform_coeffs_ch(s, ch, &m);
  480. /* tranform coefficients for coupling channel come right after the
  481. coefficients for the first coupled channel*/
  482. if (s->channel_in_cpl[ch]) {
  483. if (!got_cplchan) {
  484. get_transform_coeffs_ch(s, CPL_CH, &m);
  485. uncouple_channels(s);
  486. got_cplchan = 1;
  487. }
  488. end = s->end_freq[CPL_CH];
  489. } else {
  490. end = s->end_freq[ch];
  491. }
  492. do
  493. s->fixed_coeffs[ch][end] = 0;
  494. while(++end < 256);
  495. }
  496. /* if any channel doesn't use dithering, zero appropriate coefficients */
  497. if(!s->dither_all)
  498. remove_dithering(s);
  499. }
  500. /**
  501. * Stereo rematrixing.
  502. * reference: Section 7.5.4 Rematrixing : Decoding Technique
  503. */
  504. static void do_rematrixing(AC3DecodeContext *s)
  505. {
  506. int bnd, i;
  507. int end, bndend;
  508. int tmp0, tmp1;
  509. end = FFMIN(s->end_freq[1], s->end_freq[2]);
  510. for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) {
  511. if(s->rematrixing_flags[bnd]) {
  512. bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd+1]);
  513. for(i=ff_ac3_rematrix_band_tab[bnd]; i<bndend; i++) {
  514. tmp0 = s->fixed_coeffs[1][i];
  515. tmp1 = s->fixed_coeffs[2][i];
  516. s->fixed_coeffs[1][i] = tmp0 + tmp1;
  517. s->fixed_coeffs[2][i] = tmp0 - tmp1;
  518. }
  519. }
  520. }
  521. }
  522. /**
  523. * Perform the 256-point IMDCT
  524. */
  525. static void do_imdct_256(AC3DecodeContext *s, int chindex)
  526. {
  527. int i, k;
  528. DECLARE_ALIGNED_16(float, x[128]);
  529. FFTComplex z[2][64];
  530. float *o_ptr = s->tmp_output;
  531. for(i=0; i<2; i++) {
  532. /* de-interleave coefficients */
  533. for(k=0; k<128; k++) {
  534. x[k] = s->transform_coeffs[chindex][2*k+i];
  535. }
  536. /* run standard IMDCT */
  537. s->imdct_256.fft.imdct_calc(&s->imdct_256, o_ptr, x, s->tmp_imdct);
  538. /* reverse the post-rotation & reordering from standard IMDCT */
  539. for(k=0; k<32; k++) {
  540. z[i][32+k].re = -o_ptr[128+2*k];
  541. z[i][32+k].im = -o_ptr[2*k];
  542. z[i][31-k].re = o_ptr[2*k+1];
  543. z[i][31-k].im = o_ptr[128+2*k+1];
  544. }
  545. }
  546. /* apply AC-3 post-rotation & reordering */
  547. for(k=0; k<64; k++) {
  548. o_ptr[ 2*k ] = -z[0][ k].im;
  549. o_ptr[ 2*k+1] = z[0][63-k].re;
  550. o_ptr[128+2*k ] = -z[0][ k].re;
  551. o_ptr[128+2*k+1] = z[0][63-k].im;
  552. o_ptr[256+2*k ] = -z[1][ k].re;
  553. o_ptr[256+2*k+1] = z[1][63-k].im;
  554. o_ptr[384+2*k ] = z[1][ k].im;
  555. o_ptr[384+2*k+1] = -z[1][63-k].re;
  556. }
  557. }
  558. /**
  559. * Inverse MDCT Transform.
  560. * Convert frequency domain coefficients to time-domain audio samples.
  561. * reference: Section 7.9.4 Transformation Equations
  562. */
  563. static inline void do_imdct(AC3DecodeContext *s, int channels)
  564. {
  565. int ch;
  566. for (ch=1; ch<=channels; ch++) {
  567. if (s->block_switch[ch]) {
  568. do_imdct_256(s, ch);
  569. } else {
  570. s->imdct_512.fft.imdct_calc(&s->imdct_512, s->tmp_output,
  571. s->transform_coeffs[ch], s->tmp_imdct);
  572. }
  573. /* For the first half of the block, apply the window, add the delay
  574. from the previous block, and send to output */
  575. s->dsp.vector_fmul_add_add(s->output[ch-1], s->tmp_output,
  576. s->window, s->delay[ch-1], 0, 256, 1);
  577. /* For the second half of the block, apply the window and store the
  578. samples to delay, to be combined with the next block */
  579. s->dsp.vector_fmul_reverse(s->delay[ch-1], s->tmp_output+256,
  580. s->window, 256);
  581. }
  582. }
  583. /**
  584. * Downmix the output to mono or stereo.
  585. */
  586. static void ac3_downmix(AC3DecodeContext *s,
  587. float samples[AC3_MAX_CHANNELS][256], int ch_offset)
  588. {
  589. int i, j;
  590. float v0, v1;
  591. for(i=0; i<256; i++) {
  592. v0 = v1 = 0.0f;
  593. for(j=0; j<s->fbw_channels; j++) {
  594. v0 += samples[j+ch_offset][i] * s->downmix_coeffs[j][0];
  595. v1 += samples[j+ch_offset][i] * s->downmix_coeffs[j][1];
  596. }
  597. v0 *= s->downmix_coeff_adjust[0];
  598. v1 *= s->downmix_coeff_adjust[1];
  599. if(s->output_mode == AC3_CHMODE_MONO) {
  600. samples[ch_offset][i] = (v0 + v1) * LEVEL_MINUS_3DB;
  601. } else if(s->output_mode == AC3_CHMODE_STEREO) {
  602. samples[ ch_offset][i] = v0;
  603. samples[1+ch_offset][i] = v1;
  604. }
  605. }
  606. }
  607. /**
  608. * Upmix delay samples from stereo to original channel layout.
  609. */
  610. static void ac3_upmix_delay(AC3DecodeContext *s)
  611. {
  612. int channel_data_size = sizeof(s->delay[0]);
  613. switch(s->channel_mode) {
  614. case AC3_CHMODE_DUALMONO:
  615. case AC3_CHMODE_STEREO:
  616. /* upmix mono to stereo */
  617. memcpy(s->delay[1], s->delay[0], channel_data_size);
  618. break;
  619. case AC3_CHMODE_2F2R:
  620. memset(s->delay[3], 0, channel_data_size);
  621. case AC3_CHMODE_2F1R:
  622. memset(s->delay[2], 0, channel_data_size);
  623. break;
  624. case AC3_CHMODE_3F2R:
  625. memset(s->delay[4], 0, channel_data_size);
  626. case AC3_CHMODE_3F1R:
  627. memset(s->delay[3], 0, channel_data_size);
  628. case AC3_CHMODE_3F:
  629. memcpy(s->delay[2], s->delay[1], channel_data_size);
  630. memset(s->delay[1], 0, channel_data_size);
  631. break;
  632. }
  633. }
  634. /**
  635. * Parse an audio block from AC-3 bitstream.
  636. */
  637. static int ac3_parse_audio_block(AC3DecodeContext *s, int blk)
  638. {
  639. int fbw_channels = s->fbw_channels;
  640. int channel_mode = s->channel_mode;
  641. int i, bnd, seg, ch;
  642. int different_transforms;
  643. int downmix_output;
  644. int cpl_in_use;
  645. GetBitContext *gbc = &s->gbc;
  646. uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
  647. memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
  648. /* block switch flags */
  649. different_transforms = 0;
  650. for (ch = 1; ch <= fbw_channels; ch++) {
  651. s->block_switch[ch] = get_bits1(gbc);
  652. if(ch > 1 && s->block_switch[ch] != s->block_switch[1])
  653. different_transforms = 1;
  654. }
  655. /* dithering flags */
  656. s->dither_all = 1;
  657. for (ch = 1; ch <= fbw_channels; ch++) {
  658. s->dither_flag[ch] = get_bits1(gbc);
  659. if(!s->dither_flag[ch])
  660. s->dither_all = 0;
  661. }
  662. /* dynamic range */
  663. i = !(s->channel_mode);
  664. do {
  665. if(get_bits1(gbc)) {
  666. s->dynamic_range[i] = ((dynamic_range_tab[get_bits(gbc, 8)]-1.0) *
  667. s->avctx->drc_scale)+1.0;
  668. } else if(blk == 0) {
  669. s->dynamic_range[i] = 1.0f;
  670. }
  671. } while(i--);
  672. /* coupling strategy */
  673. if (get_bits1(gbc)) {
  674. memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
  675. cpl_in_use = get_bits1(gbc);
  676. if (cpl_in_use) {
  677. /* coupling in use */
  678. int cpl_begin_freq, cpl_end_freq;
  679. if (channel_mode < AC3_CHMODE_STEREO) {
  680. av_log(s->avctx, AV_LOG_ERROR, "coupling not allowed in mono or dual-mono\n");
  681. return -1;
  682. }
  683. /* determine which channels are coupled */
  684. for (ch = 1; ch <= fbw_channels; ch++)
  685. s->channel_in_cpl[ch] = get_bits1(gbc);
  686. /* phase flags in use */
  687. if (channel_mode == AC3_CHMODE_STEREO)
  688. s->phase_flags_in_use = get_bits1(gbc);
  689. /* coupling frequency range and band structure */
  690. cpl_begin_freq = get_bits(gbc, 4);
  691. cpl_end_freq = get_bits(gbc, 4);
  692. if (3 + cpl_end_freq - cpl_begin_freq < 0) {
  693. av_log(s->avctx, AV_LOG_ERROR, "3+cplendf = %d < cplbegf = %d\n", 3+cpl_end_freq, cpl_begin_freq);
  694. return -1;
  695. }
  696. s->num_cpl_bands = s->num_cpl_subbands = 3 + cpl_end_freq - cpl_begin_freq;
  697. s->start_freq[CPL_CH] = cpl_begin_freq * 12 + 37;
  698. s->end_freq[CPL_CH] = cpl_end_freq * 12 + 73;
  699. for (bnd = 0; bnd < s->num_cpl_subbands - 1; bnd++) {
  700. if (get_bits1(gbc)) {
  701. s->cpl_band_struct[bnd] = 1;
  702. s->num_cpl_bands--;
  703. }
  704. }
  705. s->cpl_band_struct[s->num_cpl_subbands-1] = 0;
  706. } else {
  707. /* coupling not in use */
  708. for (ch = 1; ch <= fbw_channels; ch++)
  709. s->channel_in_cpl[ch] = 0;
  710. }
  711. } else if (!blk) {
  712. av_log(s->avctx, AV_LOG_ERROR, "new coupling strategy must be present in block 0\n");
  713. return -1;
  714. } else {
  715. cpl_in_use = s->cpl_in_use[blk-1];
  716. }
  717. s->cpl_in_use[blk] = cpl_in_use;
  718. /* coupling coordinates */
  719. if (cpl_in_use) {
  720. int cpl_coords_exist = 0;
  721. for (ch = 1; ch <= fbw_channels; ch++) {
  722. if (s->channel_in_cpl[ch]) {
  723. if (get_bits1(gbc)) {
  724. int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
  725. cpl_coords_exist = 1;
  726. master_cpl_coord = 3 * get_bits(gbc, 2);
  727. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  728. cpl_coord_exp = get_bits(gbc, 4);
  729. cpl_coord_mant = get_bits(gbc, 4);
  730. if (cpl_coord_exp == 15)
  731. s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
  732. else
  733. s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
  734. s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
  735. }
  736. } else if (!blk) {
  737. av_log(s->avctx, AV_LOG_ERROR, "new coupling coordinates must be present in block 0\n");
  738. return -1;
  739. }
  740. }
  741. }
  742. /* phase flags */
  743. if (channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
  744. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  745. s->phase_flags[bnd] = s->phase_flags_in_use? get_bits1(gbc) : 0;
  746. }
  747. }
  748. }
  749. /* stereo rematrixing strategy and band structure */
  750. if (channel_mode == AC3_CHMODE_STEREO) {
  751. if (get_bits1(gbc)) {
  752. s->num_rematrixing_bands = 4;
  753. if(cpl_in_use && s->start_freq[CPL_CH] <= 61)
  754. s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
  755. for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
  756. s->rematrixing_flags[bnd] = get_bits1(gbc);
  757. } else if (!blk) {
  758. av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n");
  759. return -1;
  760. }
  761. }
  762. /* exponent strategies for each channel */
  763. s->exp_strategy[blk][CPL_CH] = EXP_REUSE;
  764. s->exp_strategy[blk][s->lfe_ch] = EXP_REUSE;
  765. for (ch = !cpl_in_use; ch <= s->channels; ch++) {
  766. s->exp_strategy[blk][ch] = get_bits(gbc, 2 - (ch == s->lfe_ch));
  767. if(s->exp_strategy[blk][ch] != EXP_REUSE)
  768. bit_alloc_stages[ch] = 3;
  769. }
  770. /* channel bandwidth */
  771. for (ch = 1; ch <= fbw_channels; ch++) {
  772. s->start_freq[ch] = 0;
  773. if (s->exp_strategy[blk][ch] != EXP_REUSE) {
  774. int group_size;
  775. int prev = s->end_freq[ch];
  776. if (s->channel_in_cpl[ch])
  777. s->end_freq[ch] = s->start_freq[CPL_CH];
  778. else {
  779. int bandwidth_code = get_bits(gbc, 6);
  780. if (bandwidth_code > 60) {
  781. av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60", bandwidth_code);
  782. return -1;
  783. }
  784. s->end_freq[ch] = bandwidth_code * 3 + 73;
  785. }
  786. group_size = 3 << (s->exp_strategy[blk][ch] - 1);
  787. s->num_exp_groups[ch] = (s->end_freq[ch]+group_size-4) / group_size;
  788. if(blk > 0 && s->end_freq[ch] != prev)
  789. memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
  790. }
  791. }
  792. if (cpl_in_use && s->exp_strategy[blk][CPL_CH] != EXP_REUSE) {
  793. s->num_exp_groups[CPL_CH] = (s->end_freq[CPL_CH] - s->start_freq[CPL_CH]) /
  794. (3 << (s->exp_strategy[blk][CPL_CH] - 1));
  795. }
  796. /* decode exponents for each channel */
  797. for (ch = !cpl_in_use; ch <= s->channels; ch++) {
  798. if (s->exp_strategy[blk][ch] != EXP_REUSE) {
  799. s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
  800. decode_exponents(gbc, s->exp_strategy[blk][ch],
  801. s->num_exp_groups[ch], s->dexps[ch][0],
  802. &s->dexps[ch][s->start_freq[ch]+!!ch]);
  803. if(ch != CPL_CH && ch != s->lfe_ch)
  804. skip_bits(gbc, 2); /* skip gainrng */
  805. }
  806. }
  807. /* bit allocation information */
  808. if (get_bits1(gbc)) {
  809. s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
  810. s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
  811. s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
  812. s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
  813. s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)];
  814. for(ch=!cpl_in_use; ch<=s->channels; ch++)
  815. bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
  816. } else if (!blk) {
  817. av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must be present in block 0\n");
  818. return -1;
  819. }
  820. /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
  821. if (get_bits1(gbc)) {
  822. int csnr;
  823. csnr = (get_bits(gbc, 6) - 15) << 4;
  824. for (ch = !cpl_in_use; ch <= s->channels; ch++) { /* snr offset and fast gain */
  825. s->snr_offset[ch] = (csnr + get_bits(gbc, 4)) << 2;
  826. s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
  827. }
  828. memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
  829. } else if (!blk) {
  830. av_log(s->avctx, AV_LOG_ERROR, "new snr offsets must be present in block 0\n");
  831. return -1;
  832. }
  833. /* coupling leak information */
  834. if (cpl_in_use) {
  835. if (get_bits1(gbc)) {
  836. s->bit_alloc_params.cpl_fast_leak = get_bits(gbc, 3);
  837. s->bit_alloc_params.cpl_slow_leak = get_bits(gbc, 3);
  838. bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
  839. } else if (!blk) {
  840. av_log(s->avctx, AV_LOG_ERROR, "new coupling leak info must be present in block 0\n");
  841. return -1;
  842. }
  843. }
  844. /* delta bit allocation information */
  845. if (get_bits1(gbc)) {
  846. /* delta bit allocation exists (strategy) */
  847. for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
  848. s->dba_mode[ch] = get_bits(gbc, 2);
  849. if (s->dba_mode[ch] == DBA_RESERVED) {
  850. av_log(s->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
  851. return -1;
  852. }
  853. bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
  854. }
  855. /* channel delta offset, len and bit allocation */
  856. for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
  857. if (s->dba_mode[ch] == DBA_NEW) {
  858. s->dba_nsegs[ch] = get_bits(gbc, 3);
  859. for (seg = 0; seg <= s->dba_nsegs[ch]; seg++) {
  860. s->dba_offsets[ch][seg] = get_bits(gbc, 5);
  861. s->dba_lengths[ch][seg] = get_bits(gbc, 4);
  862. s->dba_values[ch][seg] = get_bits(gbc, 3);
  863. }
  864. /* run last 2 bit allocation stages if new dba values */
  865. bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
  866. }
  867. }
  868. } else if(blk == 0) {
  869. for(ch=0; ch<=s->channels; ch++) {
  870. s->dba_mode[ch] = DBA_NONE;
  871. }
  872. }
  873. /* Bit allocation */
  874. for(ch=!cpl_in_use; ch<=s->channels; ch++) {
  875. if(bit_alloc_stages[ch] > 2) {
  876. /* Exponent mapping into PSD and PSD integration */
  877. ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
  878. s->start_freq[ch], s->end_freq[ch],
  879. s->psd[ch], s->band_psd[ch]);
  880. }
  881. if(bit_alloc_stages[ch] > 1) {
  882. /* Compute excitation function, Compute masking curve, and
  883. Apply delta bit allocation */
  884. ff_ac3_bit_alloc_calc_mask(&s->bit_alloc_params, s->band_psd[ch],
  885. s->start_freq[ch], s->end_freq[ch],
  886. s->fast_gain[ch], (ch == s->lfe_ch),
  887. s->dba_mode[ch], s->dba_nsegs[ch],
  888. s->dba_offsets[ch], s->dba_lengths[ch],
  889. s->dba_values[ch], s->mask[ch]);
  890. }
  891. if(bit_alloc_stages[ch] > 0) {
  892. /* Compute bit allocation */
  893. ff_ac3_bit_alloc_calc_bap(s->mask[ch], s->psd[ch],
  894. s->start_freq[ch], s->end_freq[ch],
  895. s->snr_offset[ch],
  896. s->bit_alloc_params.floor,
  897. ff_ac3_bap_tab, s->bap[ch]);
  898. }
  899. }
  900. /* unused dummy data */
  901. if (get_bits1(gbc)) {
  902. int skipl = get_bits(gbc, 9);
  903. while(skipl--)
  904. skip_bits(gbc, 8);
  905. }
  906. /* unpack the transform coefficients
  907. this also uncouples channels if coupling is in use. */
  908. get_transform_coeffs(s);
  909. /* recover coefficients if rematrixing is in use */
  910. if(s->channel_mode == AC3_CHMODE_STEREO)
  911. do_rematrixing(s);
  912. /* apply scaling to coefficients (headroom, dynrng) */
  913. for(ch=1; ch<=s->channels; ch++) {
  914. float gain = s->mul_bias / 4194304.0f;
  915. if(s->channel_mode == AC3_CHMODE_DUALMONO) {
  916. gain *= s->dynamic_range[ch-1];
  917. } else {
  918. gain *= s->dynamic_range[0];
  919. }
  920. for(i=0; i<256; i++) {
  921. s->transform_coeffs[ch][i] = s->fixed_coeffs[ch][i] * gain;
  922. }
  923. }
  924. /* downmix and MDCT. order depends on whether block switching is used for
  925. any channel in this block. this is because coefficients for the long
  926. and short transforms cannot be mixed. */
  927. downmix_output = s->channels != s->out_channels &&
  928. !((s->output_mode & AC3_OUTPUT_LFEON) &&
  929. s->fbw_channels == s->out_channels);
  930. if(different_transforms) {
  931. /* the delay samples have already been downmixed, so we upmix the delay
  932. samples in order to reconstruct all channels before downmixing. */
  933. if(s->downmixed) {
  934. s->downmixed = 0;
  935. ac3_upmix_delay(s);
  936. }
  937. do_imdct(s, s->channels);
  938. if(downmix_output) {
  939. ac3_downmix(s, s->output, 0);
  940. }
  941. } else {
  942. if(downmix_output) {
  943. ac3_downmix(s, s->transform_coeffs, 1);
  944. }
  945. if(!s->downmixed) {
  946. s->downmixed = 1;
  947. ac3_downmix(s, s->delay, 0);
  948. }
  949. do_imdct(s, s->out_channels);
  950. }
  951. /* convert float to 16-bit integer */
  952. for(ch=0; ch<s->out_channels; ch++) {
  953. for(i=0; i<256; i++) {
  954. s->output[ch][i] += s->add_bias;
  955. }
  956. s->dsp.float_to_int16(s->int_output[ch], s->output[ch], 256);
  957. }
  958. return 0;
  959. }
  960. /**
  961. * Decode a single AC-3 frame.
  962. */
  963. static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
  964. const uint8_t *buf, int buf_size)
  965. {
  966. AC3DecodeContext *s = avctx->priv_data;
  967. int16_t *out_samples = (int16_t *)data;
  968. int i, blk, ch, err;
  969. /* initialize the GetBitContext with the start of valid AC-3 Frame */
  970. if (s->input_buffer) {
  971. /* copy input buffer to decoder context to avoid reading past the end
  972. of the buffer, which can be caused by a damaged input stream. */
  973. memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_MAX_FRAME_SIZE));
  974. init_get_bits(&s->gbc, s->input_buffer, buf_size * 8);
  975. } else {
  976. init_get_bits(&s->gbc, buf, buf_size * 8);
  977. }
  978. /* parse the syncinfo */
  979. *data_size = 0;
  980. err = parse_frame_header(s);
  981. /* check that reported frame size fits in input buffer */
  982. if(s->frame_size > buf_size) {
  983. av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
  984. err = AC3_PARSE_ERROR_FRAME_SIZE;
  985. }
  986. /* check for crc mismatch */
  987. if(err != AC3_PARSE_ERROR_FRAME_SIZE && avctx->error_resilience >= FF_ER_CAREFUL) {
  988. if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {
  989. av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
  990. err = AC3_PARSE_ERROR_CRC;
  991. }
  992. }
  993. if(err && err != AC3_PARSE_ERROR_CRC) {
  994. switch(err) {
  995. case AC3_PARSE_ERROR_SYNC:
  996. av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
  997. return -1;
  998. case AC3_PARSE_ERROR_BSID:
  999. av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
  1000. break;
  1001. case AC3_PARSE_ERROR_SAMPLE_RATE:
  1002. av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
  1003. break;
  1004. case AC3_PARSE_ERROR_FRAME_SIZE:
  1005. av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
  1006. break;
  1007. case AC3_PARSE_ERROR_FRAME_TYPE:
  1008. /* skip frame if CRC is ok. otherwise use error concealment. */
  1009. /* TODO: add support for substreams and dependent frames */
  1010. if(s->frame_type == EAC3_FRAME_TYPE_DEPENDENT || s->substreamid) {
  1011. av_log(avctx, AV_LOG_ERROR, "unsupported frame type : skipping frame\n");
  1012. return s->frame_size;
  1013. } else {
  1014. av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
  1015. }
  1016. break;
  1017. default:
  1018. av_log(avctx, AV_LOG_ERROR, "invalid header\n");
  1019. break;
  1020. }
  1021. }
  1022. /* if frame is ok, set audio parameters */
  1023. if (!err) {
  1024. avctx->sample_rate = s->sample_rate;
  1025. avctx->bit_rate = s->bit_rate;
  1026. /* channel config */
  1027. s->out_channels = s->channels;
  1028. s->output_mode = s->channel_mode;
  1029. if(s->lfe_on)
  1030. s->output_mode |= AC3_OUTPUT_LFEON;
  1031. if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&
  1032. avctx->request_channels < s->channels) {
  1033. s->out_channels = avctx->request_channels;
  1034. s->output_mode = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
  1035. }
  1036. avctx->channels = s->out_channels;
  1037. /* set downmixing coefficients if needed */
  1038. if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
  1039. s->fbw_channels == s->out_channels)) {
  1040. set_downmix_coeffs(s);
  1041. }
  1042. } else if (!s->out_channels) {
  1043. s->out_channels = avctx->channels;
  1044. if(s->out_channels < s->channels)
  1045. s->output_mode = s->out_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
  1046. }
  1047. /* parse the audio blocks */
  1048. for (blk = 0; blk < s->num_blocks; blk++) {
  1049. if (!err && ac3_parse_audio_block(s, blk)) {
  1050. av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
  1051. }
  1052. /* interleave output samples */
  1053. for (i = 0; i < 256; i++)
  1054. for (ch = 0; ch < s->out_channels; ch++)
  1055. *(out_samples++) = s->int_output[ch][i];
  1056. }
  1057. *data_size = s->num_blocks * 256 * avctx->channels * sizeof (int16_t);
  1058. return s->frame_size;
  1059. }
  1060. /**
  1061. * Uninitialize the AC-3 decoder.
  1062. */
  1063. static av_cold int ac3_decode_end(AVCodecContext *avctx)
  1064. {
  1065. AC3DecodeContext *s = avctx->priv_data;
  1066. ff_mdct_end(&s->imdct_512);
  1067. ff_mdct_end(&s->imdct_256);
  1068. av_freep(&s->input_buffer);
  1069. return 0;
  1070. }
  1071. AVCodec ac3_decoder = {
  1072. .name = "ac3",
  1073. .type = CODEC_TYPE_AUDIO,
  1074. .id = CODEC_ID_AC3,
  1075. .priv_data_size = sizeof (AC3DecodeContext),
  1076. .init = ac3_decode_init,
  1077. .close = ac3_decode_end,
  1078. .decode = ac3_decode_frame,
  1079. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 / AC-3"),
  1080. };