00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "mpeg3audio.h"
00025
00026
00027
00028 static short mpeg3_q_1[3] =
00029 {
00030 (-2 << 15) / 3,
00031 0,
00032 (2 << 15) / 3
00033 };
00034
00035 static short mpeg3_q_2[5] =
00036 {
00037 (-4 << 15) / 5,
00038 ((-2 << 15) / 5) << 1,
00039 0,
00040 (2 << 15) / 5,
00041 ((4 << 15) / 5) << 1
00042 };
00043
00044 static short mpeg3_q_3[7] =
00045 {
00046 (-6 << 15) / 7,
00047 (-4 << 15) / 7,
00048 (-2 << 15) / 7,
00049 0,
00050 (2 << 15) / 7,
00051 (4 << 15) / 7,
00052 (6 << 15) / 7
00053 };
00054
00055 static short mpeg3_q_4[11] =
00056 {
00057 (-10 << 15) / 11,
00058 (-8 << 15) / 11,
00059 (-6 << 15) / 11,
00060 (-4 << 15) / 11,
00061 (-2 << 15) / 11,
00062 0,
00063 ( 2 << 15) / 11,
00064 ( 4 << 15) / 11,
00065 ( 6 << 15) / 11,
00066 ( 8 << 15) / 11,
00067 (10 << 15) / 11
00068 };
00069
00070 static short mpeg3_q_5[15] =
00071 {
00072 (-14 << 15) / 15,
00073 (-12 << 15) / 15,
00074 (-10 << 15) / 15,
00075 (-8 << 15) / 15,
00076 (-6 << 15) / 15,
00077 (-4 << 15) / 15,
00078 (-2 << 15) / 15,
00079 0,
00080 ( 2 << 15) / 15,
00081 ( 4 << 15) / 15,
00082 ( 6 << 15) / 15,
00083 ( 8 << 15) / 15,
00084 (10 << 15) / 15,
00085 (12 << 15) / 15,
00086 (14 << 15) / 15
00087 };
00088
00089 static short mpeg3_qnttztab[16] = {0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16};
00090
00091
00092
00093
00094
00095
00096 static const u_int32_t mpeg3_scale_factors[25] =
00097 {
00098 0x38000000,
00099 0x37800000,
00100 0x37000000,
00101 0x36800000,
00102 0x36000000,
00103 0x35800000,
00104 0x35000000,
00105 0x34800000,
00106 0x34000000,
00107 0x33800000,
00108 0x33000000,
00109 0x32800000,
00110 0x32000000,
00111 0x31800000,
00112 0x31000000,
00113 0x30800000,
00114 0x30000000,
00115 0x2f800000,
00116 0x2f000000,
00117 0x2e800000,
00118 0x2e000000,
00119 0x2d800000,
00120 0x2d000000,
00121 0x2c800000,
00122 0x2c000000
00123 };
00124
00125 static MPEG3_FLOAT32 *mpeg3_scale_factor = (MPEG3_FLOAT32*)mpeg3_scale_factors;
00126
00127 #define CLIP(x, y, z) ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
00128
00129 static float mpeg3audio_ac3_tofloat(unsigned short exponent, int mantissa)
00130 {
00131 float x;
00132
00133 x = mantissa * mpeg3_scale_factor[CLIP(exponent, 0, 25)];
00134
00135
00136 return x;
00137 }
00138
00139 static void mpeg3audio_ac3_mantissa_reset(mpeg3_ac3_mantissa_t *mantissa)
00140 {
00141 mantissa->m_1[2] = mantissa->m_1[1] = mantissa->m_1[0] = 0;
00142 mantissa->m_2[2] = mantissa->m_2[1] = mantissa->m_2[0] = 0;
00143 mantissa->m_4[1] = mantissa->m_4[0] = 0;
00144
00145 mantissa->m_1_pointer = mantissa->m_2_pointer = mantissa->m_4_pointer = 3;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 static unsigned int mpeg3audio_ac3_dither_gen(mpeg3audio_t *audio)
00157 {
00158 int i;
00159 unsigned int state;
00160
00161
00162
00163 state = audio->ac3_lfsr_state;
00164
00165
00166 for(i = 0; i < 8; i++)
00167 {
00168 state <<= 1;
00169
00170 if(state & 0x10000)
00171 state ^= 0xa011;
00172 }
00173
00174 audio->ac3_lfsr_state = state;
00175 return (((((int)state << 8) >> 8) * (int)(0.707106f * 256.0f)) >> 16);
00176 }
00177
00178
00179
00180 static unsigned short mpeg3audio_ac3_mantissa_get(mpeg3audio_t *audio,
00181 unsigned short bap,
00182 unsigned short dithflag)
00183 {
00184 unsigned short mantissa;
00185 unsigned int group_code;
00186 mpeg3_ac3_mantissa_t *mantissa_struct = &(audio->ac3_mantissa);
00187
00188
00189 switch(bap)
00190 {
00191 case 0:
00192 if(dithflag)
00193 mantissa = mpeg3audio_ac3_dither_gen(audio);
00194 else
00195 mantissa = 0;
00196 break;
00197
00198 case 1:
00199 if(mantissa_struct->m_1_pointer > 2)
00200 {
00201 group_code = mpeg3bits_getbits(audio->astream, 5);
00202
00203 if(group_code > 26)
00204 {
00205
00206
00207 return 0;
00208 }
00209
00210 mantissa_struct->m_1[0] = group_code / 9;
00211 mantissa_struct->m_1[1] = (group_code % 9) / 3;
00212 mantissa_struct->m_1[2] = (group_code % 9) % 3;
00213 mantissa_struct->m_1_pointer = 0;
00214 }
00215 mantissa = mantissa_struct->m_1[mantissa_struct->m_1_pointer++];
00216 mantissa = mpeg3_q_1[mantissa];
00217 break;
00218
00219 case 2:
00220 if(mantissa_struct->m_2_pointer > 2)
00221 {
00222 group_code = mpeg3bits_getbits(audio->astream, 7);
00223
00224 if(group_code > 124)
00225 {
00226
00227 return 0;
00228 }
00229
00230 mantissa_struct->m_2[0] = group_code / 25;
00231 mantissa_struct->m_2[1] = (group_code % 25) / 5;
00232 mantissa_struct->m_2[2] = (group_code % 25) % 5;
00233 mantissa_struct->m_2_pointer = 0;
00234 }
00235 mantissa = mantissa_struct->m_2[mantissa_struct->m_2_pointer++];
00236 mantissa = mpeg3_q_2[mantissa];
00237 break;
00238
00239 case 3:
00240 mantissa = mpeg3bits_getbits(audio->astream, 3);
00241
00242 if(mantissa > 6)
00243 {
00244
00245 return 0;
00246 }
00247
00248 mantissa = mpeg3_q_3[mantissa];
00249 break;
00250
00251 case 4:
00252 if(mantissa_struct->m_4_pointer > 1)
00253 {
00254 group_code = mpeg3bits_getbits(audio->astream, 7);
00255
00256 if(group_code > 120)
00257 {
00258
00259 return 0;
00260 }
00261
00262 mantissa_struct->m_4[0] = group_code / 11;
00263 mantissa_struct->m_4[1] = group_code % 11;
00264 mantissa_struct->m_4_pointer = 0;
00265 }
00266 mantissa = mantissa_struct->m_4[mantissa_struct->m_4_pointer++];
00267 mantissa = mpeg3_q_4[mantissa];
00268 break;
00269
00270 case 5:
00271 mantissa = mpeg3bits_getbits(audio->astream, 4);
00272
00273 if(mantissa > 14)
00274 {
00275
00276
00277 return 0;
00278 }
00279
00280 mantissa = mpeg3_q_5[mantissa];
00281 break;
00282
00283 default:
00284 mantissa = mpeg3bits_getbits(audio->astream, mpeg3_qnttztab[bap]);
00285 mantissa <<= 16 - mpeg3_qnttztab[bap];
00286 }
00287 return mantissa;
00288 }
00289
00290 void mpeg3audio_ac3_uncouple_channel(mpeg3audio_t *audio,
00291 float samples[],
00292 mpeg3_ac3bsi_t *bsi,
00293 mpeg3_ac3audblk_t *audblk,
00294 unsigned int ch)
00295 {
00296 unsigned int bnd = 0;
00297 unsigned int sub_bnd = 0;
00298 unsigned int i, j;
00299 MPEG3_FLOAT32 cpl_coord = 1.0;
00300 unsigned int cpl_exp_tmp;
00301 unsigned int cpl_mant_tmp;
00302 short mantissa;
00303
00304 for(i = audblk->cplstrtmant; i < audblk->cplendmant; )
00305 {
00306 if(!audblk->cplbndstrc[sub_bnd++])
00307 {
00308 cpl_exp_tmp = audblk->cplcoexp[ch][bnd] + 3 * audblk->mstrcplco[ch];
00309 if(audblk->cplcoexp[ch][bnd] == 15)
00310 cpl_mant_tmp = (audblk->cplcomant[ch][bnd]) << 11;
00311 else
00312 cpl_mant_tmp = ((0x10) | audblk->cplcomant[ch][bnd]) << 10;
00313
00314 cpl_coord = mpeg3audio_ac3_tofloat(cpl_exp_tmp, cpl_mant_tmp) * 8.0f;
00315
00316
00317 if(bsi->acmod == 0x2 && audblk->phsflginu && ch == 1 && audblk->phsflg[bnd])
00318 cpl_coord *= -1;
00319
00320 bnd++;
00321 }
00322
00323 for(j = 0; j < 12; j++)
00324 {
00325
00326
00327 if(audblk->dithflag[ch] && audblk->cpl_bap[i] == 0)
00328 mantissa = mpeg3audio_ac3_dither_gen(audio);
00329 else
00330 mantissa = audblk->cplmant[i];
00331
00332 samples[i] = cpl_coord * mpeg3audio_ac3_tofloat(audblk->cpl_exp[i], mantissa);;
00333
00334 i++;
00335 }
00336 }
00337 return;
00338 }
00339
00340 int mpeg3audio_ac3_coeff_unpack(mpeg3audio_t *audio,
00341 mpeg3_ac3bsi_t *bsi,
00342 mpeg3_ac3audblk_t *audblk,
00343 mpeg3ac3_stream_samples_t samples)
00344 {
00345 int i, j;
00346 int done_cpl = 0;
00347 short mantissa;
00348
00349 mpeg3audio_ac3_mantissa_reset(&(audio->ac3_mantissa));
00350
00351 for(i = 0; i < bsi->nfchans && !mpeg3bits_error(audio->astream); i++)
00352 {
00353 for(j = 0; j < audblk->endmant[i] && !mpeg3bits_error(audio->astream); j++)
00354 {
00355 mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->fbw_bap[i][j], audblk->dithflag[i]);
00356 samples[i][j] = mpeg3audio_ac3_tofloat(audblk->fbw_exp[i][j], mantissa);
00357 }
00358
00359 if(audblk->cplinu && audblk->chincpl[i] && !(done_cpl) && !mpeg3bits_error(audio->astream))
00360 {
00361
00362
00363
00364 for(j = audblk->cplstrtmant;
00365 j < audblk->cplendmant && !mpeg3bits_error(audio->astream);
00366 j++)
00367 {
00368 audblk->cplmant[j] = mpeg3audio_ac3_mantissa_get(audio, audblk->cpl_bap[j], 0);
00369 }
00370 done_cpl = 1;
00371 }
00372 }
00373
00374
00375 if(audblk->cplinu)
00376 {
00377 if(audblk->chincpl[i])
00378 mpeg3audio_ac3_uncouple_channel(audio, samples[i], bsi, audblk, i);
00379 }
00380
00381 if(bsi->lfeon && !mpeg3bits_error(audio->astream))
00382 {
00383
00384 for(j = 0; j < 7 && !mpeg3bits_error(audio->astream); j++)
00385 {
00386 mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->lfe_bap[j], 0);
00387 samples[5][j] = mpeg3audio_ac3_tofloat(audblk->lfe_exp[j], mantissa);
00388
00389 }
00390
00391 }
00392
00393 return mpeg3bits_error(audio->astream);
00394 }