Revision 5066f515
libavcodec/ac3dec.c  

44  44 
*/ 
45  45 
static const uint8_t rematrix_band_tbl[5] = { 13, 25, 37, 61, 253 }; 
46  46  
47 
/* table for exponent to scale_factor mapping 

48 
* scale_factor[i] = 2 ^ (i + 15) 

47 
/** 

48 
* table for exponent to scale_factor mapping 

49 
* scale_factors[i] = 2 ^ i 

49  50 
*/ 
50  51 
static float scale_factors[25]; 
51  52  
...  ...  
75  76 
/** dialogue normalization table */ 
76  77 
static float dialnorm_tbl[32]; 
77  78  
78 
/* Adjustmens in dB gain */


79 
/** Adjustments in dB gain */


79  80 
#define LEVEL_MINUS_3DB 0.7071067811865476 
80  81 
#define LEVEL_MINUS_4POINT5DB 0.5946035575013605 
81  82 
#define LEVEL_MINUS_6DB 0.5000000000000000 
...  ...  
127  128 
#define AC3_OUTPUT_LFEON 8 
128  129  
129  130 
typedef struct { 
130 
int acmod; 

131 
int dsurmod; 

132 
int blksw[AC3_MAX_CHANNELS]; 

133 
int dithflag[AC3_MAX_CHANNELS]; 

134 
int dither_all; 

135 
int cplinu; 

136 
int chincpl[AC3_MAX_CHANNELS]; 

137 
int phsflginu; 

138 
int cplbndstrc[18]; 

139 
int rematstr; 

140 
int nrematbnd; 

141 
int rematflg[4]; 

142 
int expstr[AC3_MAX_CHANNELS]; 

143 
int snroffst[AC3_MAX_CHANNELS]; 

144 
int fgain[AC3_MAX_CHANNELS]; 

145 
int deltbae[AC3_MAX_CHANNELS]; 

146 
int deltnseg[AC3_MAX_CHANNELS]; 

147 
uint8_t deltoffst[AC3_MAX_CHANNELS][8]; 

148 
uint8_t deltlen[AC3_MAX_CHANNELS][8]; 

149 
uint8_t deltba[AC3_MAX_CHANNELS][8]; 

150  
151 
/* Derived Attributes. */ 

152 
int sampling_rate; 

153 
int bit_rate; 

154 
int frame_size; 

155  
156 
int nchans; //number of total channels 

157 
int nfchans; //number of fullbandwidth channels 

158 
int lfeon; //lfe channel in use 

131 
int acmod; ///< audio coding mode 

132 
int dsurmod; ///< dolby surround mode 

133 
int blksw[AC3_MAX_CHANNELS]; ///< block switch flags 

134 
int dithflag[AC3_MAX_CHANNELS]; ///< dither flags 

135 
int dither_all; ///< true if all channels are dithered 

136 
int cplinu; ///< coupling in use 

137 
int chincpl[AC3_MAX_CHANNELS]; ///< channel in coupling 

138 
int phsflginu; ///< phase flags in use 

139 
int cplbndstrc[18]; ///< coupling band structure 

140 
int rematstr; ///< rematrixing strategy 

141 
int nrematbnd; ///< number of rematrixing bands 

142 
int rematflg[4]; ///< rematrixing flags 

143 
int expstr[AC3_MAX_CHANNELS]; ///< exponent strategies 

144 
int snroffst[AC3_MAX_CHANNELS]; ///< signaltonoise ratio offsets 

145 
int fgain[AC3_MAX_CHANNELS]; ///< fast gain values (signaltomask ratio) 

146 
int deltbae[AC3_MAX_CHANNELS]; ///< delta bit allocation exists 

147 
int deltnseg[AC3_MAX_CHANNELS]; ///< number of delta segments 

148 
uint8_t deltoffst[AC3_MAX_CHANNELS][8]; ///< delta segment offsets 

149 
uint8_t deltlen[AC3_MAX_CHANNELS][8]; ///< delta segment lengths 

150 
uint8_t deltba[AC3_MAX_CHANNELS][8]; ///< delta values for each segment 

151  
152 
int sampling_rate; ///< sample frequency, in Hz 

153 
int bit_rate; ///< stream bit rate, in bitspersecond 

154 
int frame_size; ///< current frame size, in bytes 

155  
156 
int nchans; ///< number of total channels 

157 
int nfchans; ///< number of fullbandwidth channels 

158 
int lfeon; ///< lfe channel in use 

159  159 
int lfe_ch; ///< index of LFE channel 
160  160 
int output_mode; ///< output channel configuration 
161  161 
int out_channels; ///< number of output channels 
...  ...  
163  163 
float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients 
164  164 
float dialnorm[2]; ///< dialogue normalization 
165  165 
float dynrng[2]; ///< dynamic range 
166 
float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates


167 
int ncplbnd; //number of coupling bands


168 
int ncplsubnd; //number of coupling sub bands


166 
float cplco[AC3_MAX_CHANNELS][18]; ///< coupling coordinates


167 
int ncplbnd; ///< number of coupling bands


168 
int ncplsubnd; ///< number of coupling sub bands


169  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  171 
AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters 
172  172  
173  173 
int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents 
...  ...  
176  176 
int16_t bndpsd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents 
177  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  181 
/* For IMDCT. */ 
182 
MDCTContext imdct_512; //for 512 sample imdct transform


183 
MDCTContext imdct_256; //for 256 sample imdct transform


184 
DSPContext dsp; //for optimization


182 
MDCTContext imdct_512; ///< for 512 sample IMDCT


183 
MDCTContext imdct_256; ///< for 256 sample IMDCT


184 
DSPContext dsp; ///< for optimization


185  185 
float add_bias; ///< offset for float_to_int16 conversion 
186  186 
float mul_bias; ///< scaling for float_to_int16 conversion 
187  187  
188 
DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS1][256]); //output after imdct transform and windowing


188 
DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS1][256]); ///< output after imdct transform and windowing


189  189 
DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS1][256]); ///< final 16bit integer output 
190 
DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS1][256]); //delay  added to the next block


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


193 
DECLARE_ALIGNED_16(float, window[256]); //window coefficients


190 
DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS1][256]); ///< delay  added to the next block


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


193 
DECLARE_ALIGNED_16(float, window[256]); ///< window coefficients


194  194  
195  195 
/* Miscellaneous. */ 
196 
GetBitContext gb; 

197 
AVRandomState dith_state; //for dither generation


196 
GetBitContext gb; ///< bitstream reader


197 
AVRandomState dith_state; ///< for dither generation


198  198 
AVCodecContext *avctx; ///< parent context 
199  199 
} AC3DecodeContext; 
200  200  
...  ...  
211  211 
for (i = 0; i < 256; i++) { 
212  212 
tmp = i * (256  i) * alpha2; 
213  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  215 
bessel = bessel * tmp / (j * j) + 1; 
216  216 
sum += bessel; 
217  217 
local_window[i] = sum; 
...  ...  
222  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  230 
static inline float 
226  231 
symmetric_dequant(int code, int levels) 
227  232 
{ 
...  ...  
279  284 
} 
280  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  289 
for (i = 0; i < 25; i++) 
284  290 
scale_factors[i] = pow(2.0, i); 
285  291  
...  ...  
293  299 
} 
294  300  
295  301  
302 
/** 

303 
* AVCodec initialization 

304 
*/ 

296  305 
static int ac3_decode_init(AVCodecContext *avctx) 
297  306 
{ 
298  307 
AC3DecodeContext *ctx = avctx>priv_data; 
...  ...  
306  315 
dsputil_init(&ctx>dsp, avctx); 
307  316 
av_init_random(0, &ctx>dith_state); 
308  317  
318 
/* set bias values for float to int16 conversion */ 

309  319 
if(ctx>dsp.float_to_int16 == ff_float_to_int16_c) { 
310  320 
ctx>add_bias = 385.0f; 
311  321 
ctx>mul_bias = 1.0f; 
...  ...  
318  328 
} 
319  329  
320  330 
/** 
321 
* Parses the 'sync info' and 'bit stream info' from the AC3 bitstream.


331 
* Parse the 'sync info' and 'bit stream info' from the AC3 bitstream. 

322  332 
* GetBitContext within AC3DecodeContext must point to 
323  333 
* start of the synchronized ac3 bitstream. 
324  334 
*/ 
...  ...  
355  365 
ctx>output_mode = AC3_OUTPUT_LFEON; 
356  366  
357  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  369 
skip_bits(gb, 16); // skip crc1 
360  370 
skip_bits(gb, 8); // skip fscod and frmsizecod 
361  371 
skip_bits(gb, 11); // skip bsid, bsmod, and acmod 
...  ...  
383  393  
384  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  398 
if (get_bits1(gb)) 
388 
skip_bits(gb, 14); //skip timecode1 

399 
skip_bits(gb, 14); //skip timecode1 / xbsi1


389  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  404 
if (get_bits1(gb)) { 
393 
i = get_bits(gb, 6); //additional bsi length


405 
i = get_bits(gb, 6); 

394  406 
do { 
395  407 
skip_bits(gb, 8); 
396  408 
} while(i); 
...  ...  
418  430 
} 
419  431  
420  432 
/** 
421 
* Decodes the grouped exponents. 

422 
* This function decodes the coded exponents according to exponent strategy 

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 

433 
* Decode the grouped exponents according to exponent strategy. 

434 
* reference: Section 7.1.3 Exponent Decoding 

430  435 
*/ 
431  436 
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps, 
432  437 
uint8_t absexp, int8_t *dexps) 
...  ...  
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  464 
* range using the coupling coefficients and coupling coordinates. 
460  465 
* reference: Section 7.4.3 Coupling Coordinate Format 
461  466 
*/ 
...  ...  
479  484 
} 
480  485 
} 
481  486  
482 
typedef struct { /* grouped mantissas for 3level 5leve and 11level quantization */ 

487 
/** 

488 
* Grouped mantissas for 3level 5level and 11level quantization 

489 
*/ 

490 
typedef struct { 

483  491 
float b1_mant[3]; 
484  492 
float b2_mant[3]; 
485  493 
float b4_mant[2]; 
...  ...  
488  496 
int b4ptr; 
489  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  503 
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m) 
493  504 
{ 
494  505 
GetBitContext *gb = &ctx>gb; 
...  ...  
551  562 
break; 
552  563  
553  564 
default: 
565 
/* asymmetric dequantization */ 

554  566 
coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]1]; 
555  567 
break; 
556  568 
} 
...  ...  
561  573 
} 
562  574  
563  575 
/** 
564 
* Removes random dithering from coefficients with zerobit mantissas


576 
* Remove random dithering from coefficients with zerobit mantissas 

565  577 
* reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0) 
566  578 
*/ 
567  579 
static void remove_dithering(AC3DecodeContext *ctx) { 
...  ...  
593  605 
} 
594  606 
} 
595  607  
596 
/* Get the transform coefficients. 

597 
* This function extracts the tranform coefficients form the ac3 bitstream. 

598 
* This function is called after bit allocation is performed. 

608 
/** 

609 
* Get the transform coefficients. 

599  610 
*/ 
600  611 
static int get_transform_coeffs(AC3DecodeContext * ctx) 
601  612 
{ 
...  ...  
606  617 
m.b1ptr = m.b2ptr = m.b4ptr = 3; 
607  618  
608  619 
for (ch = 1; ch <= ctx>nchans; ch++) { 
609 
/* transform coefficients for individual channel */


620 
/* transform coefficients for fullbandwidth channel */


610  621 
if (get_transform_coeffs_ch(ctx, ch, &m)) 
611  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  625 
if (ctx>chincpl[ch]) { 
614  626 
if (!got_cplchan) { 
615  627 
if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) { 
...  ...  
636  648 
} 
637  649  
638  650 
/** 
639 
* Performs stereo rematrixing.


651 
* Stereo rematrixing.


640  652 
* reference: Section 7.5.4 Rematrixing : Decoding Technique 
641  653 
*/ 
642  654 
static void do_rematrixing(AC3DecodeContext *ctx) 
...  ...  
660  672 
} 
661  673 
} 
662  674  
663 
/* This function performs the imdct on 256 sample transform


664 
* coefficients.


675 
/**


676 
* Perform the 256point IMDCT


665  677 
*/ 
666  678 
static void do_imdct_256(AC3DecodeContext *ctx, int chindex) 
667  679 
{ 
...  ...  
701  713 
} 
702  714 
} 
703  715  
704 
/* IMDCT Transform. */ 

716 
/** 

717 
* Inverse MDCT Transform. 

718 
* Convert frequency domain coefficients to timedomain audio samples. 

719 
* reference: Section 7.9.4 Transformation Equations 

720 
*/ 

705  721 
static inline void do_imdct(AC3DecodeContext *ctx) 
706  722 
{ 
707  723 
int ch; 
708  724 
int nchans; 
709  725  
726 
/* Don't perform the IMDCT on the LFE channel unless it's used in the output */ 

710  727 
nchans = ctx>nfchans; 
711  728 
if(ctx>output_mode & AC3_OUTPUT_LFEON) 
712  729 
nchans++; 
...  ...  
719  736 
ctx>transform_coeffs[ch], 
720  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  741 
ctx>dsp.vector_fmul_add_add(ctx>output[ch1], ctx>tmp_output, 
723  742 
ctx>window, ctx>delay[ch1], 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  745 
ctx>dsp.vector_fmul_reverse(ctx>delay[ch1], ctx>tmp_output+256, 
725  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  753 
static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans, 
733  754 
int output_mode, float coef[AC3_MAX_CHANNELS][2]) 
...  ...  
754  775 
} 
755  776 
} 
756  777  
757 
/* Parse the audio block from ac3 bitstream. 

758 
* This function extract the audio block from the ac3 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. 

778 
/** 

779 
* Parse an audio block from AC3 bitstream. 

761  780 
*/ 
762  781 
static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk) 
763  782 
{ 
...  ...  
769  788  
770  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  793 
ctx>blksw[ch] = get_bits1(gb); 
774  794  
795 
/* dithering flags */ 

775  796 
ctx>dither_all = 1; 
776 
for (ch = 1; ch <= nfchans; ch++) { /* dithering flag */


797 
for (ch = 1; ch <= nfchans; ch++) { 

777  798 
ctx>dithflag[ch] = get_bits1(gb); 
778  799 
if(!ctx>dithflag[ch]) 
779  800 
ctx>dither_all = 0; 
...  ...  
789  810 
} 
790  811 
} while(i); 
791  812  
792 
if (get_bits1(gb)) { /* coupling strategy */ 

813 
/* coupling strategy */ 

814 
if (get_bits1(gb)) { 

793  815 
memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 
794  816 
ctx>cplinu = get_bits1(gb); 
795 
if (ctx>cplinu) { /* coupling in use */ 

817 
if (ctx>cplinu) { 

818 
/* coupling in use */ 

796  819 
int cplbegf, cplendf; 
797  820  
821 
/* determine which channels are coupled */ 

798  822 
for (ch = 1; ch <= nfchans; ch++) 
799  823 
ctx>chincpl[ch] = get_bits1(gb); 
800  824  
825 
/* phase flags in use */ 

801  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  830 
cplbegf = get_bits(gb, 4); 
805  831 
cplendf = get_bits(gb, 4); 
806  
807  832 
if (3 + cplendf  cplbegf < 0) { 
808  833 
av_log(ctx>avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf); 
809  834 
return 1; 
810  835 
} 
811  
812  836 
ctx>ncplbnd = ctx>ncplsubnd = 3 + cplendf  cplbegf; 
813  837 
ctx>startmant[CPL_CH] = cplbegf * 12 + 37; 
814  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  840 
if (get_bits1(gb)) { 
817  841 
ctx>cplbndstrc[bnd] = 1; 
818  842 
ctx>ncplbnd; 
819  843 
} 
820  844 
} 
821  845 
} else { 
846 
/* coupling not in use */ 

822  847 
for (ch = 1; ch <= nfchans; ch++) 
823  848 
ctx>chincpl[ch] = 0; 
824  849 
} 
825  850 
} 
826  851  
852 
/* coupling coordinates */ 

827  853 
if (ctx>cplinu) { 
828  854 
int cplcoe = 0; 
829  855  
830  856 
for (ch = 1; ch <= nfchans; ch++) { 
831  857 
if (ctx>chincpl[ch]) { 
832 
if (get_bits1(gb)) { /* coupling coordinates */


858 
if (get_bits1(gb)) { 

833  859 
int mstrcplco, cplcoexp, cplcomant; 
834  860 
cplcoe = 1; 
835  861 
mstrcplco = 3 * get_bits(gb, 2); 
...  ...  
845  871 
} 
846  872 
} 
847  873 
} 
848  
874 
/* phase flags */ 

849  875 
if (acmod == AC3_ACMOD_STEREO && ctx>phsflginu && cplcoe) { 
850  876 
for (bnd = 0; bnd < ctx>ncplbnd; bnd++) { 
851  877 
if (get_bits1(gb)) 
...  ...  
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  885 
ctx>rematstr = get_bits1(gb); 
859  886 
if (ctx>rematstr) { 
860  887 
ctx>nrematbnd = 4; 
...  ...  
865  892 
} 
866  893 
} 
867  894  
895 
/* exponent strategies for each channel */ 

868  896 
ctx>expstr[CPL_CH] = EXP_REUSE; 
869  897 
ctx>expstr[ctx>lfe_ch] = EXP_REUSE; 
870  898 
for (ch = !ctx>cplinu; ch <= ctx>nchans; ch++) { 
...  ...  
876  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  909 
ctx>startmant[ch] = 0; 
881  910 
if (ctx>expstr[ch] != EXP_REUSE) { 
882  911 
int prev = ctx>endmant[ch]; 
...  ...  
897  926 
ctx>startmant[ctx>lfe_ch] = 0; 
898  927 
ctx>endmant[ctx>lfe_ch] = 7; 
899  928  
929 
/* decode exponents for each channel */ 

900  930 
for (ch = !ctx>cplinu; ch <= ctx>nchans; ch++) { 
901  931 
if (ctx>expstr[ch] != EXP_REUSE) { 
902  932 
int grpsize, ngrps; 
...  ...  
915  945 
} 
916  946 
} 
917  947  
918 
if (get_bits1(gb)) { /* bit allocation information */ 

948 
/* bit allocation information */ 

949 
if (get_bits1(gb)) { 

919  950 
ctx>bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)]; 
920  951 
ctx>bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)]; 
921  952 
ctx>bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)]; 
...  ...  
926  957 
} 
927  958 
} 
928  959  
929 
if (get_bits1(gb)) { /* snroffset */ 

960 
/* signaltonoise ratio offsets and fast gains (signaltomask ratios) */ 

961 
if (get_bits1(gb)) { 

930  962 
int csnr; 
931  963 
csnr = (get_bits(gb, 6)  15) << 4; 
932  964 
for (ch = !ctx>cplinu; ch <= ctx>nchans; ch++) { /* snr offset and fast gain */ 
...  ...  
936  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  973 
ctx>bit_alloc_params.cplfleak = get_bits(gb, 3); 
941  974 
ctx>bit_alloc_params.cplsleak = get_bits(gb, 3); 
942  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  981 
for (ch = !ctx>cplinu; ch <= nfchans; ch++) { 
947  982 
ctx>deltbae[ch] = get_bits(gb, 2); 
948  983 
if (ctx>deltbae[ch] == DBA_RESERVED) { 
...  ...  
951  986 
} 
952  987 
bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 
953  988 
} 
989 
/* channel delta offset, len and bit allocation */ 

954  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  992 
ctx>deltnseg[ch] = get_bits(gb, 3); 
957  993 
for (seg = 0; seg <= ctx>deltnseg[ch]; seg++) { 
958  994 
ctx>deltoffst[ch][seg] = get_bits(gb, 5); 
...  ...  
967  1003 
} 
968  1004 
} 
969  1005  
1006 
/* Bit allocation */ 

970  1007 
for(ch=!ctx>cplinu; ch<=ctx>nchans; ch++) { 
971  1008 
if(bit_alloc_stages[ch] > 2) { 
972  1009 
/* Exponent mapping into PSD and PSD integration */ 
...  ...  
994  1031 
} 
995  1032 
} 
996  1033  
997 
if (get_bits1(gb)) { /* unused dummy data */ 

1034 
/* unused dummy data */ 

1035 
if (get_bits1(gb)) { 

998  1036 
int skipl = get_bits(gb, 9); 
999  1037 
while(skipl) 
1000  1038 
skip_bits(gb, 8); 
1001  1039 
} 
1002  1040  
1003  1041 
/* unpack the transform coefficients 
1004 
* * this also uncouples channels if coupling is in use. 

1005 
*/ 

1042 
this also uncouples channels if coupling is in use. */ 

1006  1043 
if (get_transform_coeffs(ctx)) { 
1007  1044 
av_log(ctx>avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n"); 
1008  1045 
return 1; 
...  ...  
1045  1082 
return 0; 
1046  1083 
} 
1047  1084  
1048 
/* Decode ac3 frame. 

1049 
* 

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 

1085 
/** 

1086 
* Decode a single AC3 frame. 

1055  1087 
*/ 
1056  1088 
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size) 
1057  1089 
{ 
...  ...  
1059  1091 
int16_t *out_samples = (int16_t *)data; 
1060  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 AC3 Frame */


1063  1095 
init_get_bits(&ctx>gb, buf, buf_size * 8); 
1064  1096  
1065 
//Parse the syncinfo.


1097 
/* parse the syncinfo */


1066  1098 
if (ac3_parse_header(ctx)) { 
1067  1099 
av_log(avctx, AV_LOG_ERROR, "\n"); 
1068  1100 
*data_size = 0; 
...  ...  
1092  1124 
} 
1093  1125 
ctx>out_channels = avctx>channels; 
1094  1126  
1095 
//Parse the Audio Blocks.


1127 
/* parse the audio blocks */


1096  1128 
for (blk = 0; blk < NB_BLOCKS; blk++) { 
1097  1129 
if (ac3_parse_audio_block(ctx, blk)) { 
1098  1130 
av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); 
...  ...  
1107  1139 
return ctx>frame_size; 
1108  1140 
} 
1109  1141  
1110 
/* Uninitialize ac3 decoder. 

1142 
/** 

1143 
* Uninitialize the AC3 decoder. 

1111  1144 
*/ 
1112  1145 
static int ac3_decode_end(AVCodecContext *avctx) 
1113  1146 
{ 
Also available in: Unified diff