comparison ac3dec.c @ 5515:b499866a3397 libavcodec

comments modification: add, clean up, Doxygenize, and reword
author jbr
date Thu, 09 Aug 2007 00:36:49 +0000
parents 838ba4dafaae
children da33495f0621
comparison
equal deleted inserted replaced
5514:e2dbd1623e1d 5515:b499866a3397
42 * Table of bin locations for rematrixing bands 42 * Table of bin locations for rematrixing bands
43 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions 43 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
44 */ 44 */
45 static const uint8_t rematrix_band_tbl[5] = { 13, 25, 37, 61, 253 }; 45 static const uint8_t rematrix_band_tbl[5] = { 13, 25, 37, 61, 253 };
46 46
47 /* table for exponent to scale_factor mapping 47 /**
48 * scale_factor[i] = 2 ^ -(i + 15) 48 * table for exponent to scale_factor mapping
49 * scale_factors[i] = 2 ^ -i
49 */ 50 */
50 static float scale_factors[25]; 51 static float scale_factors[25];
51 52
52 /** table for grouping exponents */ 53 /** table for grouping exponents */
53 static uint8_t exp_ungroup_tbl[128][3]; 54 static uint8_t exp_ungroup_tbl[128][3];
73 static float dynrng_tbl[256]; 74 static float dynrng_tbl[256];
74 75
75 /** dialogue normalization table */ 76 /** dialogue normalization table */
76 static float dialnorm_tbl[32]; 77 static float dialnorm_tbl[32];
77 78
78 /* Adjustmens in dB gain */ 79 /** Adjustments in dB gain */
79 #define LEVEL_MINUS_3DB 0.7071067811865476 80 #define LEVEL_MINUS_3DB 0.7071067811865476
80 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605 81 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
81 #define LEVEL_MINUS_6DB 0.5000000000000000 82 #define LEVEL_MINUS_6DB 0.5000000000000000
82 #define LEVEL_MINUS_9DB 0.3535533905932738 83 #define LEVEL_MINUS_9DB 0.3535533905932738
83 #define LEVEL_ZERO 0.0000000000000000 84 #define LEVEL_ZERO 0.0000000000000000
125 #define CPL_CH 0 126 #define CPL_CH 0
126 127
127 #define AC3_OUTPUT_LFEON 8 128 #define AC3_OUTPUT_LFEON 8
128 129
129 typedef struct { 130 typedef struct {
130 int acmod; 131 int acmod; ///< audio coding mode
131 int dsurmod; 132 int dsurmod; ///< dolby surround mode
132 int blksw[AC3_MAX_CHANNELS]; 133 int blksw[AC3_MAX_CHANNELS]; ///< block switch flags
133 int dithflag[AC3_MAX_CHANNELS]; 134 int dithflag[AC3_MAX_CHANNELS]; ///< dither flags
134 int dither_all; 135 int dither_all; ///< true if all channels are dithered
135 int cplinu; 136 int cplinu; ///< coupling in use
136 int chincpl[AC3_MAX_CHANNELS]; 137 int chincpl[AC3_MAX_CHANNELS]; ///< channel in coupling
137 int phsflginu; 138 int phsflginu; ///< phase flags in use
138 int cplbndstrc[18]; 139 int cplbndstrc[18]; ///< coupling band structure
139 int rematstr; 140 int rematstr; ///< rematrixing strategy
140 int nrematbnd; 141 int nrematbnd; ///< number of rematrixing bands
141 int rematflg[4]; 142 int rematflg[4]; ///< rematrixing flags
142 int expstr[AC3_MAX_CHANNELS]; 143 int expstr[AC3_MAX_CHANNELS]; ///< exponent strategies
143 int snroffst[AC3_MAX_CHANNELS]; 144 int snroffst[AC3_MAX_CHANNELS]; ///< signal-to-noise ratio offsets
144 int fgain[AC3_MAX_CHANNELS]; 145 int fgain[AC3_MAX_CHANNELS]; ///< fast gain values (signal-to-mask ratio)
145 int deltbae[AC3_MAX_CHANNELS]; 146 int deltbae[AC3_MAX_CHANNELS]; ///< delta bit allocation exists
146 int deltnseg[AC3_MAX_CHANNELS]; 147 int deltnseg[AC3_MAX_CHANNELS]; ///< number of delta segments
147 uint8_t deltoffst[AC3_MAX_CHANNELS][8]; 148 uint8_t deltoffst[AC3_MAX_CHANNELS][8]; ///< delta segment offsets
148 uint8_t deltlen[AC3_MAX_CHANNELS][8]; 149 uint8_t deltlen[AC3_MAX_CHANNELS][8]; ///< delta segment lengths
149 uint8_t deltba[AC3_MAX_CHANNELS][8]; 150 uint8_t deltba[AC3_MAX_CHANNELS][8]; ///< delta values for each segment
150 151
151 /* Derived Attributes. */ 152 int sampling_rate; ///< sample frequency, in Hz
152 int sampling_rate; 153 int bit_rate; ///< stream bit rate, in bits-per-second
153 int bit_rate; 154 int frame_size; ///< current frame size, in bytes
154 int frame_size; 155
155 156 int nchans; ///< number of total channels
156 int nchans; //number of total channels 157 int nfchans; ///< number of full-bandwidth channels
157 int nfchans; //number of full-bandwidth channels 158 int lfeon; ///< lfe channel in use
158 int lfeon; //lfe channel in use
159 int lfe_ch; ///< index of LFE channel 159 int lfe_ch; ///< index of LFE channel
160 int output_mode; ///< output channel configuration 160 int output_mode; ///< output channel configuration
161 int out_channels; ///< number of output channels 161 int out_channels; ///< number of output channels
162 162
163 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients 163 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients
164 float dialnorm[2]; ///< dialogue normalization 164 float dialnorm[2]; ///< dialogue normalization
165 float dynrng[2]; ///< dynamic range 165 float dynrng[2]; ///< dynamic range
166 float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates 166 float cplco[AC3_MAX_CHANNELS][18]; ///< coupling coordinates
167 int ncplbnd; //number of coupling bands 167 int ncplbnd; ///< number of coupling bands
168 int ncplsubnd; //number of coupling sub bands 168 int ncplsubnd; ///< number of coupling sub bands
169 int startmant[AC3_MAX_CHANNELS]; ///< start frequency bin 169 int startmant[AC3_MAX_CHANNELS]; ///< start frequency bin
170 int endmant[AC3_MAX_CHANNELS]; //channel end mantissas 170 int endmant[AC3_MAX_CHANNELS]; ///< end frequency bin
171 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters 171 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
172 172
173 int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents 173 int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents
174 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers 174 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers
175 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents 175 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents
176 int16_t bndpsd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents 176 int16_t bndpsd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents
177 int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values 177 int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values
178 178
179 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients 179 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); ///< transform coefficients
180 180
181 /* For IMDCT. */ 181 /* For IMDCT. */
182 MDCTContext imdct_512; //for 512 sample imdct transform 182 MDCTContext imdct_512; ///< for 512 sample IMDCT
183 MDCTContext imdct_256; //for 256 sample imdct transform 183 MDCTContext imdct_256; ///< for 256 sample IMDCT
184 DSPContext dsp; //for optimization 184 DSPContext dsp; ///< for optimization
185 float add_bias; ///< offset for float_to_int16 conversion 185 float add_bias; ///< offset for float_to_int16 conversion
186 float mul_bias; ///< scaling for float_to_int16 conversion 186 float mul_bias; ///< scaling for float_to_int16 conversion
187 187
188 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]); //output after imdct transform and windowing 188 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]); ///< output after imdct transform and windowing
189 DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS-1][256]); ///< final 16-bit integer output 189 DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS-1][256]); ///< final 16-bit integer output
190 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]); //delay - added to the next block 190 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]); ///< delay - added to the next block
191 DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform 191 DECLARE_ALIGNED_16(float, tmp_imdct[256]); ///< temporary storage for imdct transform
192 DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing 192 DECLARE_ALIGNED_16(float, tmp_output[512]); ///< temporary storage for output before windowing
193 DECLARE_ALIGNED_16(float, window[256]); //window coefficients 193 DECLARE_ALIGNED_16(float, window[256]); ///< window coefficients
194 194
195 /* Miscellaneous. */ 195 /* Miscellaneous. */
196 GetBitContext gb; 196 GetBitContext gb; ///< bitstream reader
197 AVRandomState dith_state; //for dither generation 197 AVRandomState dith_state; ///< for dither generation
198 AVCodecContext *avctx; ///< parent context 198 AVCodecContext *avctx; ///< parent context
199 } AC3DecodeContext; 199 } AC3DecodeContext;
200 200
201 /** 201 /**
202 * Generate a Kaiser-Bessel Derived Window. 202 * Generate a Kaiser-Bessel Derived Window.
209 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0); 209 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
210 210
211 for (i = 0; i < 256; i++) { 211 for (i = 0; i < 256; i++) {
212 tmp = i * (256 - i) * alpha2; 212 tmp = i * (256 - i) * alpha2;
213 bessel = 1.0; 213 bessel = 1.0;
214 for (j = 100; j > 0; j--) /* defaul to 100 iterations */ 214 for (j = 100; j > 0; j--) /* default to 100 iterations */
215 bessel = bessel * tmp / (j * j) + 1; 215 bessel = bessel * tmp / (j * j) + 1;
216 sum += bessel; 216 sum += bessel;
217 local_window[i] = sum; 217 local_window[i] = sum;
218 } 218 }
219 219
220 sum++; 220 sum++;
221 for (i = 0; i < 256; i++) 221 for (i = 0; i < 256; i++)
222 window[i] = sqrt(local_window[i] / sum); 222 window[i] = sqrt(local_window[i] / sum);
223 } 223 }
224 224
225 /**
226 * Symmetrical Dequantization
227 * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
228 * Tables 7.19 to 7.23
229 */
225 static inline float 230 static inline float
226 symmetric_dequant(int code, int levels) 231 symmetric_dequant(int code, int levels)
227 { 232 {
228 return (code - (levels >> 1)) * (2.0f / levels); 233 return (code - (levels >> 1)) * (2.0f / levels);
229 } 234 }
277 for(i=1; i<32; i++) { 282 for(i=1; i<32; i++) {
278 dialnorm_tbl[i] = expf((i-31) * M_LN10 / 20.0f); 283 dialnorm_tbl[i] = expf((i-31) * M_LN10 / 20.0f);
279 } 284 }
280 dialnorm_tbl[0] = dialnorm_tbl[31]; 285 dialnorm_tbl[0] = dialnorm_tbl[31];
281 286
282 //generate scale factors 287 /* generate scale factors for exponents and asymmetrical dequantization
288 reference: Section 7.3.2 Expansion of Mantissas for Asymmetric Quantization */
283 for (i = 0; i < 25; i++) 289 for (i = 0; i < 25; i++)
284 scale_factors[i] = pow(2.0, -i); 290 scale_factors[i] = pow(2.0, -i);
285 291
286 /* generate exponent tables 292 /* generate exponent tables
287 reference: Section 7.1.3 Exponent Decoding */ 293 reference: Section 7.1.3 Exponent Decoding */
291 exp_ungroup_tbl[i][2] = (i % 25) % 5; 297 exp_ungroup_tbl[i][2] = (i % 25) % 5;
292 } 298 }
293 } 299 }
294 300
295 301
302 /**
303 * AVCodec initialization
304 */
296 static int ac3_decode_init(AVCodecContext *avctx) 305 static int ac3_decode_init(AVCodecContext *avctx)
297 { 306 {
298 AC3DecodeContext *ctx = avctx->priv_data; 307 AC3DecodeContext *ctx = avctx->priv_data;
299 ctx->avctx = avctx; 308 ctx->avctx = avctx;
300 309
304 ff_mdct_init(&ctx->imdct_512, 9, 1); 313 ff_mdct_init(&ctx->imdct_512, 9, 1);
305 ac3_window_init(ctx->window); 314 ac3_window_init(ctx->window);
306 dsputil_init(&ctx->dsp, avctx); 315 dsputil_init(&ctx->dsp, avctx);
307 av_init_random(0, &ctx->dith_state); 316 av_init_random(0, &ctx->dith_state);
308 317
318 /* set bias values for float to int16 conversion */
309 if(ctx->dsp.float_to_int16 == ff_float_to_int16_c) { 319 if(ctx->dsp.float_to_int16 == ff_float_to_int16_c) {
310 ctx->add_bias = 385.0f; 320 ctx->add_bias = 385.0f;
311 ctx->mul_bias = 1.0f; 321 ctx->mul_bias = 1.0f;
312 } else { 322 } else {
313 ctx->add_bias = 0.0f; 323 ctx->add_bias = 0.0f;
316 326
317 return 0; 327 return 0;
318 } 328 }
319 329
320 /** 330 /**
321 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream. 331 * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream.
322 * GetBitContext within AC3DecodeContext must point to 332 * GetBitContext within AC3DecodeContext must point to
323 * start of the synchronized ac3 bitstream. 333 * start of the synchronized ac3 bitstream.
324 */ 334 */
325 static int ac3_parse_header(AC3DecodeContext *ctx) 335 static int ac3_parse_header(AC3DecodeContext *ctx)
326 { 336 {
353 ctx->output_mode = ctx->acmod; 363 ctx->output_mode = ctx->acmod;
354 if(ctx->lfeon) 364 if(ctx->lfeon)
355 ctx->output_mode |= AC3_OUTPUT_LFEON; 365 ctx->output_mode |= AC3_OUTPUT_LFEON;
356 366
357 /* skip over portion of header which has already been read */ 367 /* skip over portion of header which has already been read */
358 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16); 368 skip_bits(gb, 16); // skip the sync_word
359 skip_bits(gb, 16); // skip crc1 369 skip_bits(gb, 16); // skip crc1
360 skip_bits(gb, 8); // skip fscod and frmsizecod 370 skip_bits(gb, 8); // skip fscod and frmsizecod
361 skip_bits(gb, 11); // skip bsid, bsmod, and acmod 371 skip_bits(gb, 11); // skip bsid, bsmod, and acmod
362 if(ctx->acmod == AC3_ACMOD_STEREO) { 372 if(ctx->acmod == AC3_ACMOD_STEREO) {
363 skip_bits(gb, 2); // skip dsurmod 373 skip_bits(gb, 2); // skip dsurmod
381 skip_bits(gb, 7); //skip audio production information 391 skip_bits(gb, 7); //skip audio production information
382 } while (i--); 392 } while (i--);
383 393
384 skip_bits(gb, 2); //skip copyright bit and original bitstream bit 394 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
385 395
386 /* FIXME: read & use the xbsi1 downmix levels */ 396 /* skip the timecodes (or extra bitstream information for Alternate Syntax)
397 TODO: read & use the xbsi1 downmix levels */
387 if (get_bits1(gb)) 398 if (get_bits1(gb))
388 skip_bits(gb, 14); //skip timecode1 399 skip_bits(gb, 14); //skip timecode1 / xbsi1
389 if (get_bits1(gb)) 400 if (get_bits1(gb))
390 skip_bits(gb, 14); //skip timecode2 401 skip_bits(gb, 14); //skip timecode2 / xbsi2
391 402
403 /* skip additional bitstream info */
392 if (get_bits1(gb)) { 404 if (get_bits1(gb)) {
393 i = get_bits(gb, 6); //additional bsi length 405 i = get_bits(gb, 6);
394 do { 406 do {
395 skip_bits(gb, 8); 407 skip_bits(gb, 8);
396 } while(i--); 408 } while(i--);
397 } 409 }
398 410
416 428
417 return 0; 429 return 0;
418 } 430 }
419 431
420 /** 432 /**
421 * Decodes the grouped exponents. 433 * Decode the grouped exponents according to exponent strategy.
422 * This function decodes the coded exponents according to exponent strategy 434 * reference: Section 7.1.3 Exponent Decoding
423 * and stores them in the decoded exponents buffer.
424 *
425 * @param[in] gb GetBitContext which points to start of coded exponents
426 * @param[in] expstr Exponent coding strategy
427 * @param[in] ngrps Number of grouped exponents
428 * @param[in] absexp Absolute exponent or DC exponent
429 * @param[out] dexps Decoded exponents are stored in dexps
430 */ 435 */
431 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps, 436 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
432 uint8_t absexp, int8_t *dexps) 437 uint8_t absexp, int8_t *dexps)
433 { 438 {
434 int i, j, grp, grpsize; 439 int i, j, grp, grpsize;
453 } 458 }
454 } 459 }
455 } 460 }
456 461
457 /** 462 /**
458 * Generates transform coefficients for each coupled channel in the coupling 463 * Generate transform coefficients for each coupled channel in the coupling
459 * range using the coupling coefficients and coupling coordinates. 464 * range using the coupling coefficients and coupling coordinates.
460 * reference: Section 7.4.3 Coupling Coordinate Format 465 * reference: Section 7.4.3 Coupling Coordinate Format
461 */ 466 */
462 static void uncouple_channels(AC3DecodeContext *ctx) 467 static void uncouple_channels(AC3DecodeContext *ctx)
463 { 468 {
477 } 482 }
478 } while(ctx->cplbndstrc[subbnd]); 483 } while(ctx->cplbndstrc[subbnd]);
479 } 484 }
480 } 485 }
481 486
482 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */ 487 /**
488 * Grouped mantissas for 3-level 5-level and 11-level quantization
489 */
490 typedef struct {
483 float b1_mant[3]; 491 float b1_mant[3];
484 float b2_mant[3]; 492 float b2_mant[3];
485 float b4_mant[2]; 493 float b4_mant[2];
486 int b1ptr; 494 int b1ptr;
487 int b2ptr; 495 int b2ptr;
488 int b4ptr; 496 int b4ptr;
489 } mant_groups; 497 } mant_groups;
490 498
491 /* Get the transform coefficients for particular channel */ 499 /**
500 * Get the transform coefficients for a particular channel
501 * reference: Section 7.3 Quantization and Decoding of Mantissas
502 */
492 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m) 503 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
493 { 504 {
494 GetBitContext *gb = &ctx->gb; 505 GetBitContext *gb = &ctx->gb;
495 int i, gcode, tbap, start, end; 506 int i, gcode, tbap, start, end;
496 uint8_t *exps; 507 uint8_t *exps;
549 case 5: 560 case 5:
550 coeffs[i] = b5_mantissas[get_bits(gb, 4)]; 561 coeffs[i] = b5_mantissas[get_bits(gb, 4)];
551 break; 562 break;
552 563
553 default: 564 default:
565 /* asymmetric dequantization */
554 coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1]; 566 coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];
555 break; 567 break;
556 } 568 }
557 coeffs[i] *= scale_factors[exps[i]]; 569 coeffs[i] *= scale_factors[exps[i]];
558 } 570 }
559 571
560 return 0; 572 return 0;
561 } 573 }
562 574
563 /** 575 /**
564 * Removes random dithering from coefficients with zero-bit mantissas 576 * Remove random dithering from coefficients with zero-bit mantissas
565 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0) 577 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
566 */ 578 */
567 static void remove_dithering(AC3DecodeContext *ctx) { 579 static void remove_dithering(AC3DecodeContext *ctx) {
568 int ch, i; 580 int ch, i;
569 int end=0; 581 int end=0;
591 } 603 }
592 } 604 }
593 } 605 }
594 } 606 }
595 607
596 /* Get the transform coefficients. 608 /**
597 * This function extracts the tranform coefficients form the ac3 bitstream. 609 * Get the transform coefficients.
598 * This function is called after bit allocation is performed.
599 */ 610 */
600 static int get_transform_coeffs(AC3DecodeContext * ctx) 611 static int get_transform_coeffs(AC3DecodeContext * ctx)
601 { 612 {
602 int ch, end; 613 int ch, end;
603 int got_cplchan = 0; 614 int got_cplchan = 0;
604 mant_groups m; 615 mant_groups m;
605 616
606 m.b1ptr = m.b2ptr = m.b4ptr = 3; 617 m.b1ptr = m.b2ptr = m.b4ptr = 3;
607 618
608 for (ch = 1; ch <= ctx->nchans; ch++) { 619 for (ch = 1; ch <= ctx->nchans; ch++) {
609 /* transform coefficients for individual channel */ 620 /* transform coefficients for full-bandwidth channel */
610 if (get_transform_coeffs_ch(ctx, ch, &m)) 621 if (get_transform_coeffs_ch(ctx, ch, &m))
611 return -1; 622 return -1;
612 /* tranform coefficients for coupling channels */ 623 /* tranform coefficients for coupling channel come right after the
624 coefficients for the first coupled channel*/
613 if (ctx->chincpl[ch]) { 625 if (ctx->chincpl[ch]) {
614 if (!got_cplchan) { 626 if (!got_cplchan) {
615 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) { 627 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
616 av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n"); 628 av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n");
617 return -1; 629 return -1;
634 646
635 return 0; 647 return 0;
636 } 648 }
637 649
638 /** 650 /**
639 * Performs stereo rematrixing. 651 * Stereo rematrixing.
640 * reference: Section 7.5.4 Rematrixing : Decoding Technique 652 * reference: Section 7.5.4 Rematrixing : Decoding Technique
641 */ 653 */
642 static void do_rematrixing(AC3DecodeContext *ctx) 654 static void do_rematrixing(AC3DecodeContext *ctx)
643 { 655 {
644 int bnd, i; 656 int bnd, i;
658 } 670 }
659 } 671 }
660 } 672 }
661 } 673 }
662 674
663 /* This function performs the imdct on 256 sample transform 675 /**
664 * coefficients. 676 * Perform the 256-point IMDCT
665 */ 677 */
666 static void do_imdct_256(AC3DecodeContext *ctx, int chindex) 678 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
667 { 679 {
668 int i, k; 680 int i, k;
669 DECLARE_ALIGNED_16(float, x[128]); 681 DECLARE_ALIGNED_16(float, x[128]);
699 o_ptr[384+2*k ] = z[1][ k].im; 711 o_ptr[384+2*k ] = z[1][ k].im;
700 o_ptr[384+2*k+1] = -z[1][63-k].re; 712 o_ptr[384+2*k+1] = -z[1][63-k].re;
701 } 713 }
702 } 714 }
703 715
704 /* IMDCT Transform. */ 716 /**
717 * Inverse MDCT Transform.
718 * Convert frequency domain coefficients to time-domain audio samples.
719 * reference: Section 7.9.4 Transformation Equations
720 */
705 static inline void do_imdct(AC3DecodeContext *ctx) 721 static inline void do_imdct(AC3DecodeContext *ctx)
706 { 722 {
707 int ch; 723 int ch;
708 int nchans; 724 int nchans;
709 725
726 /* Don't perform the IMDCT on the LFE channel unless it's used in the output */
710 nchans = ctx->nfchans; 727 nchans = ctx->nfchans;
711 if(ctx->output_mode & AC3_OUTPUT_LFEON) 728 if(ctx->output_mode & AC3_OUTPUT_LFEON)
712 nchans++; 729 nchans++;
713 730
714 for (ch=1; ch<=nchans; ch++) { 731 for (ch=1; ch<=nchans; ch++) {
717 } else { 734 } else {
718 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 735 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
719 ctx->transform_coeffs[ch], 736 ctx->transform_coeffs[ch],
720 ctx->tmp_imdct); 737 ctx->tmp_imdct);
721 } 738 }
739 /* For the first half of the block, apply the window, add the delay
740 from the previous block, and send to output */
722 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output, 741 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
723 ctx->window, ctx->delay[ch-1], 0, 256, 1); 742 ctx->window, ctx->delay[ch-1], 0, 256, 1);
743 /* For the second half of the block, apply the window and store the
744 samples to delay, to be combined with the next block */
724 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256, 745 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
725 ctx->window, 256); 746 ctx->window, 256);
726 } 747 }
727 } 748 }
728 749
729 /** 750 /**
730 * Downmixes the output to stereo. 751 * Downmix the output to mono or stereo.
731 */ 752 */
732 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans, 753 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans,
733 int output_mode, float coef[AC3_MAX_CHANNELS][2]) 754 int output_mode, float coef[AC3_MAX_CHANNELS][2])
734 { 755 {
735 int i, j; 756 int i, j;
752 samples[1][i] = v1; 773 samples[1][i] = v1;
753 } 774 }
754 } 775 }
755 } 776 }
756 777
757 /* Parse the audio block from ac3 bitstream. 778 /**
758 * This function extract the audio block from the ac3 bitstream 779 * Parse an audio block from AC-3 bitstream.
759 * and produces the output for the block. This function must
760 * be called for each of the six audio block in the ac3 bitstream.
761 */ 780 */
762 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk) 781 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
763 { 782 {
764 int nfchans = ctx->nfchans; 783 int nfchans = ctx->nfchans;
765 int acmod = ctx->acmod; 784 int acmod = ctx->acmod;
767 GetBitContext *gb = &ctx->gb; 786 GetBitContext *gb = &ctx->gb;
768 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS]; 787 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
769 788
770 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS); 789 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
771 790
772 for (ch = 1; ch <= nfchans; ch++) /*block switch flag */ 791 /* block switch flags */
792 for (ch = 1; ch <= nfchans; ch++)
773 ctx->blksw[ch] = get_bits1(gb); 793 ctx->blksw[ch] = get_bits1(gb);
774 794
795 /* dithering flags */
775 ctx->dither_all = 1; 796 ctx->dither_all = 1;
776 for (ch = 1; ch <= nfchans; ch++) { /* dithering flag */ 797 for (ch = 1; ch <= nfchans; ch++) {
777 ctx->dithflag[ch] = get_bits1(gb); 798 ctx->dithflag[ch] = get_bits1(gb);
778 if(!ctx->dithflag[ch]) 799 if(!ctx->dithflag[ch])
779 ctx->dither_all = 0; 800 ctx->dither_all = 0;
780 } 801 }
781 802
787 } else if(blk == 0) { 808 } else if(blk == 0) {
788 ctx->dynrng[i] = 1.0f; 809 ctx->dynrng[i] = 1.0f;
789 } 810 }
790 } while(i--); 811 } while(i--);
791 812
792 if (get_bits1(gb)) { /* coupling strategy */ 813 /* coupling strategy */
814 if (get_bits1(gb)) {
793 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 815 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
794 ctx->cplinu = get_bits1(gb); 816 ctx->cplinu = get_bits1(gb);
795 if (ctx->cplinu) { /* coupling in use */ 817 if (ctx->cplinu) {
818 /* coupling in use */
796 int cplbegf, cplendf; 819 int cplbegf, cplendf;
797 820
821 /* determine which channels are coupled */
798 for (ch = 1; ch <= nfchans; ch++) 822 for (ch = 1; ch <= nfchans; ch++)
799 ctx->chincpl[ch] = get_bits1(gb); 823 ctx->chincpl[ch] = get_bits1(gb);
800 824
825 /* phase flags in use */
801 if (acmod == AC3_ACMOD_STEREO) 826 if (acmod == AC3_ACMOD_STEREO)
802 ctx->phsflginu = get_bits1(gb); //phase flag in use 827 ctx->phsflginu = get_bits1(gb);
803 828
829 /* coupling frequency range and band structure */
804 cplbegf = get_bits(gb, 4); 830 cplbegf = get_bits(gb, 4);
805 cplendf = get_bits(gb, 4); 831 cplendf = get_bits(gb, 4);
806
807 if (3 + cplendf - cplbegf < 0) { 832 if (3 + cplendf - cplbegf < 0) {
808 av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf); 833 av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
809 return -1; 834 return -1;
810 } 835 }
811
812 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf; 836 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
813 ctx->startmant[CPL_CH] = cplbegf * 12 + 37; 837 ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
814 ctx->endmant[CPL_CH] = cplendf * 12 + 73; 838 ctx->endmant[CPL_CH] = cplendf * 12 + 73;
815 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */ 839 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) {
816 if (get_bits1(gb)) { 840 if (get_bits1(gb)) {
817 ctx->cplbndstrc[bnd] = 1; 841 ctx->cplbndstrc[bnd] = 1;
818 ctx->ncplbnd--; 842 ctx->ncplbnd--;
819 } 843 }
820 } 844 }
821 } else { 845 } else {
846 /* coupling not in use */
822 for (ch = 1; ch <= nfchans; ch++) 847 for (ch = 1; ch <= nfchans; ch++)
823 ctx->chincpl[ch] = 0; 848 ctx->chincpl[ch] = 0;
824 } 849 }
825 } 850 }
826 851
852 /* coupling coordinates */
827 if (ctx->cplinu) { 853 if (ctx->cplinu) {
828 int cplcoe = 0; 854 int cplcoe = 0;
829 855
830 for (ch = 1; ch <= nfchans; ch++) { 856 for (ch = 1; ch <= nfchans; ch++) {
831 if (ctx->chincpl[ch]) { 857 if (ctx->chincpl[ch]) {
832 if (get_bits1(gb)) { /* coupling co-ordinates */ 858 if (get_bits1(gb)) {
833 int mstrcplco, cplcoexp, cplcomant; 859 int mstrcplco, cplcoexp, cplcomant;
834 cplcoe = 1; 860 cplcoe = 1;
835 mstrcplco = 3 * get_bits(gb, 2); 861 mstrcplco = 3 * get_bits(gb, 2);
836 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 862 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
837 cplcoexp = get_bits(gb, 4); 863 cplcoexp = get_bits(gb, 4);
843 ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco]; 869 ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco];
844 } 870 }
845 } 871 }
846 } 872 }
847 } 873 }
848 874 /* phase flags */
849 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) { 875 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
850 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 876 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
851 if (get_bits1(gb)) 877 if (get_bits1(gb))
852 ctx->cplco[2][bnd] = -ctx->cplco[2][bnd]; 878 ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
853 } 879 }
854 } 880 }
855 } 881 }
856 882
857 if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */ 883 /* stereo rematrixing strategy and band structure */
884 if (acmod == AC3_ACMOD_STEREO) {
858 ctx->rematstr = get_bits1(gb); 885 ctx->rematstr = get_bits1(gb);
859 if (ctx->rematstr) { 886 if (ctx->rematstr) {
860 ctx->nrematbnd = 4; 887 ctx->nrematbnd = 4;
861 if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61) 888 if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
862 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37); 889 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
863 for(bnd=0; bnd<ctx->nrematbnd; bnd++) 890 for(bnd=0; bnd<ctx->nrematbnd; bnd++)
864 ctx->rematflg[bnd] = get_bits1(gb); 891 ctx->rematflg[bnd] = get_bits1(gb);
865 } 892 }
866 } 893 }
867 894
895 /* exponent strategies for each channel */
868 ctx->expstr[CPL_CH] = EXP_REUSE; 896 ctx->expstr[CPL_CH] = EXP_REUSE;
869 ctx->expstr[ctx->lfe_ch] = EXP_REUSE; 897 ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
870 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { 898 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
871 if(ch == ctx->lfe_ch) 899 if(ch == ctx->lfe_ch)
872 ctx->expstr[ch] = get_bits(gb, 1); 900 ctx->expstr[ch] = get_bits(gb, 1);
874 ctx->expstr[ch] = get_bits(gb, 2); 902 ctx->expstr[ch] = get_bits(gb, 2);
875 if(ctx->expstr[ch] != EXP_REUSE) 903 if(ctx->expstr[ch] != EXP_REUSE)
876 bit_alloc_stages[ch] = 3; 904 bit_alloc_stages[ch] = 3;
877 } 905 }
878 906
879 for (ch = 1; ch <= nfchans; ch++) { /* channel bandwidth code */ 907 /* channel bandwidth */
908 for (ch = 1; ch <= nfchans; ch++) {
880 ctx->startmant[ch] = 0; 909 ctx->startmant[ch] = 0;
881 if (ctx->expstr[ch] != EXP_REUSE) { 910 if (ctx->expstr[ch] != EXP_REUSE) {
882 int prev = ctx->endmant[ch]; 911 int prev = ctx->endmant[ch];
883 if (ctx->chincpl[ch]) 912 if (ctx->chincpl[ch])
884 ctx->endmant[ch] = ctx->startmant[CPL_CH]; 913 ctx->endmant[ch] = ctx->startmant[CPL_CH];
895 } 924 }
896 } 925 }
897 ctx->startmant[ctx->lfe_ch] = 0; 926 ctx->startmant[ctx->lfe_ch] = 0;
898 ctx->endmant[ctx->lfe_ch] = 7; 927 ctx->endmant[ctx->lfe_ch] = 7;
899 928
929 /* decode exponents for each channel */
900 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { 930 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
901 if (ctx->expstr[ch] != EXP_REUSE) { 931 if (ctx->expstr[ch] != EXP_REUSE) {
902 int grpsize, ngrps; 932 int grpsize, ngrps;
903 grpsize = 3 << (ctx->expstr[ch] - 1); 933 grpsize = 3 << (ctx->expstr[ch] - 1);
904 if(ch == CPL_CH) 934 if(ch == CPL_CH)
913 if(ch != CPL_CH && ch != ctx->lfe_ch) 943 if(ch != CPL_CH && ch != ctx->lfe_ch)
914 skip_bits(gb, 2); /* skip gainrng */ 944 skip_bits(gb, 2); /* skip gainrng */
915 } 945 }
916 } 946 }
917 947
918 if (get_bits1(gb)) { /* bit allocation information */ 948 /* bit allocation information */
949 if (get_bits1(gb)) {
919 ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)]; 950 ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
920 ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)]; 951 ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
921 ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)]; 952 ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)];
922 ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)]; 953 ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
923 ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)]; 954 ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)];
924 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) { 955 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
925 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 956 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
926 } 957 }
927 } 958 }
928 959
929 if (get_bits1(gb)) { /* snroffset */ 960 /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
961 if (get_bits1(gb)) {
930 int csnr; 962 int csnr;
931 csnr = (get_bits(gb, 6) - 15) << 4; 963 csnr = (get_bits(gb, 6) - 15) << 4;
932 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */ 964 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */
933 ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2; 965 ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
934 ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)]; 966 ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
935 } 967 }
936 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 968 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
937 } 969 }
938 970
939 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */ 971 /* coupling leak information */
972 if (ctx->cplinu && get_bits1(gb)) {
940 ctx->bit_alloc_params.cplfleak = get_bits(gb, 3); 973 ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
941 ctx->bit_alloc_params.cplsleak = get_bits(gb, 3); 974 ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
942 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2); 975 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
943 } 976 }
944 977
945 if (get_bits1(gb)) { /* delta bit allocation information */ 978 /* delta bit allocation information */
979 if (get_bits1(gb)) {
980 /* delta bit allocation exists (strategy) */
946 for (ch = !ctx->cplinu; ch <= nfchans; ch++) { 981 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
947 ctx->deltbae[ch] = get_bits(gb, 2); 982 ctx->deltbae[ch] = get_bits(gb, 2);
948 if (ctx->deltbae[ch] == DBA_RESERVED) { 983 if (ctx->deltbae[ch] == DBA_RESERVED) {
949 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n"); 984 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
950 return -1; 985 return -1;
951 } 986 }
952 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 987 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
953 } 988 }
989 /* channel delta offset, len and bit allocation */
954 for (ch = !ctx->cplinu; ch <= nfchans; ch++) { 990 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
955 if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */ 991 if (ctx->deltbae[ch] == DBA_NEW) {
956 ctx->deltnseg[ch] = get_bits(gb, 3); 992 ctx->deltnseg[ch] = get_bits(gb, 3);
957 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) { 993 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
958 ctx->deltoffst[ch][seg] = get_bits(gb, 5); 994 ctx->deltoffst[ch][seg] = get_bits(gb, 5);
959 ctx->deltlen[ch][seg] = get_bits(gb, 4); 995 ctx->deltlen[ch][seg] = get_bits(gb, 4);
960 ctx->deltba[ch][seg] = get_bits(gb, 3); 996 ctx->deltba[ch][seg] = get_bits(gb, 3);
965 for(ch=0; ch<=ctx->nchans; ch++) { 1001 for(ch=0; ch<=ctx->nchans; ch++) {
966 ctx->deltbae[ch] = DBA_NONE; 1002 ctx->deltbae[ch] = DBA_NONE;
967 } 1003 }
968 } 1004 }
969 1005
1006 /* Bit allocation */
970 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) { 1007 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
971 if(bit_alloc_stages[ch] > 2) { 1008 if(bit_alloc_stages[ch] > 2) {
972 /* Exponent mapping into PSD and PSD integration */ 1009 /* Exponent mapping into PSD and PSD integration */
973 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch], 1010 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
974 ctx->startmant[ch], ctx->endmant[ch], 1011 ctx->startmant[ch], ctx->endmant[ch],
992 ctx->bit_alloc_params.floor, 1029 ctx->bit_alloc_params.floor,
993 ctx->bap[ch]); 1030 ctx->bap[ch]);
994 } 1031 }
995 } 1032 }
996 1033
997 if (get_bits1(gb)) { /* unused dummy data */ 1034 /* unused dummy data */
1035 if (get_bits1(gb)) {
998 int skipl = get_bits(gb, 9); 1036 int skipl = get_bits(gb, 9);
999 while(skipl--) 1037 while(skipl--)
1000 skip_bits(gb, 8); 1038 skip_bits(gb, 8);
1001 } 1039 }
1002 1040
1003 /* unpack the transform coefficients 1041 /* unpack the transform coefficients
1004 * * this also uncouples channels if coupling is in use. 1042 this also uncouples channels if coupling is in use. */
1005 */
1006 if (get_transform_coeffs(ctx)) { 1043 if (get_transform_coeffs(ctx)) {
1007 av_log(ctx->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n"); 1044 av_log(ctx->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1008 return -1; 1045 return -1;
1009 } 1046 }
1010 1047
1043 } 1080 }
1044 1081
1045 return 0; 1082 return 0;
1046 } 1083 }
1047 1084
1048 /* Decode ac3 frame. 1085 /**
1049 * 1086 * Decode a single AC-3 frame.
1050 * @param avctx Pointer to AVCodecContext
1051 * @param data Pointer to pcm smaples
1052 * @param data_size Set to number of pcm samples produced by decoding
1053 * @param buf Data to be decoded
1054 * @param buf_size Size of the buffer
1055 */ 1087 */
1056 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size) 1088 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1057 { 1089 {
1058 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data; 1090 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1059 int16_t *out_samples = (int16_t *)data; 1091 int16_t *out_samples = (int16_t *)data;
1060 int i, blk, ch; 1092 int i, blk, ch;
1061 1093
1062 //Initialize the GetBitContext with the start of valid AC3 Frame. 1094 /* initialize the GetBitContext with the start of valid AC-3 Frame */
1063 init_get_bits(&ctx->gb, buf, buf_size * 8); 1095 init_get_bits(&ctx->gb, buf, buf_size * 8);
1064 1096
1065 //Parse the syncinfo. 1097 /* parse the syncinfo */
1066 if (ac3_parse_header(ctx)) { 1098 if (ac3_parse_header(ctx)) {
1067 av_log(avctx, AV_LOG_ERROR, "\n"); 1099 av_log(avctx, AV_LOG_ERROR, "\n");
1068 *data_size = 0; 1100 *data_size = 0;
1069 return buf_size; 1101 return buf_size;
1070 } 1102 }
1090 ctx->out_channels, avctx->channels); 1122 ctx->out_channels, avctx->channels);
1091 return -1; 1123 return -1;
1092 } 1124 }
1093 ctx->out_channels = avctx->channels; 1125 ctx->out_channels = avctx->channels;
1094 1126
1095 //Parse the Audio Blocks. 1127 /* parse the audio blocks */
1096 for (blk = 0; blk < NB_BLOCKS; blk++) { 1128 for (blk = 0; blk < NB_BLOCKS; blk++) {
1097 if (ac3_parse_audio_block(ctx, blk)) { 1129 if (ac3_parse_audio_block(ctx, blk)) {
1098 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); 1130 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1099 *data_size = 0; 1131 *data_size = 0;
1100 return ctx->frame_size; 1132 return ctx->frame_size;
1105 } 1137 }
1106 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t); 1138 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
1107 return ctx->frame_size; 1139 return ctx->frame_size;
1108 } 1140 }
1109 1141
1110 /* Uninitialize ac3 decoder. 1142 /**
1143 * Uninitialize the AC-3 decoder.
1111 */ 1144 */
1112 static int ac3_decode_end(AVCodecContext *avctx) 1145 static int ac3_decode_end(AVCodecContext *avctx)
1113 { 1146 {
1114 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data; 1147 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1115 ff_mdct_end(&ctx->imdct_512); 1148 ff_mdct_end(&ctx->imdct_512);