00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00033 #include <math.h>
00034 #include <stddef.h>
00035 #include <stdio.h>
00036
00037 #define ALT_BITSTREAM_READER
00038 #include "avcodec.h"
00039 #include "bitstream.h"
00040 #include "dsputil.h"
00041
00042 #include "imcdata.h"
00043
00044 #define IMC_FRAME_ID 0x21
00045 #define BANDS 32
00046 #define COEFFS 256
00047
00048 typedef struct {
00049 float old_floor[BANDS];
00050 float flcoeffs1[BANDS];
00051 float flcoeffs2[BANDS];
00052 float flcoeffs3[BANDS];
00053 float flcoeffs4[BANDS];
00054 float flcoeffs5[BANDS];
00055 float flcoeffs6[BANDS];
00056 float CWdecoded[COEFFS];
00057
00060 float mdct_sine_window[COEFFS];
00061 float post_cos[COEFFS];
00062 float post_sin[COEFFS];
00063 float pre_coef1[COEFFS];
00064 float pre_coef2[COEFFS];
00065 float last_fft_im[COEFFS];
00067
00068 int bandWidthT[BANDS];
00069 int bitsBandT[BANDS];
00070 int CWlengthT[COEFFS];
00071 int levlCoeffBuf[BANDS];
00072 int bandFlagsBuf[BANDS];
00073 int sumLenArr[BANDS];
00074 int skipFlagRaw[BANDS];
00075 int skipFlagBits[BANDS];
00076 int skipFlagCount[BANDS];
00077 int skipFlags[COEFFS];
00078 int codewords[COEFFS];
00079 float sqrt_tab[30];
00080 GetBitContext gb;
00081 VLC huffman_vlc[4][4];
00082 int decoder_reset;
00083 float one_div_log2;
00084
00085 DSPContext dsp;
00086 FFTContext fft;
00087 DECLARE_ALIGNED_16(FFTComplex, samples[COEFFS/2]);
00088 DECLARE_ALIGNED_16(float, out_samples[COEFFS]);
00089 } IMCContext;
00090
00091
00092 static int imc_decode_init(AVCodecContext * avctx)
00093 {
00094 int i, j;
00095 IMCContext *q = avctx->priv_data;
00096 double r1, r2;
00097
00098 q->decoder_reset = 1;
00099
00100 for(i = 0; i < BANDS; i++)
00101 q->old_floor[i] = 1.0;
00102
00103
00104 for(i = 0; i < COEFFS; i++)
00105 q->mdct_sine_window[i] = sin((i + 0.5) / 512.0 * M_PI) * sqrt(2.0);
00106 for(i = 0; i < COEFFS/2; i++){
00107 q->post_cos[i] = cos(i / 256.0 * M_PI);
00108 q->post_sin[i] = sin(i / 256.0 * M_PI);
00109
00110 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
00111 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
00112
00113 if (i & 0x1)
00114 {
00115 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
00116 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
00117 }
00118 else
00119 {
00120 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
00121 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
00122 }
00123
00124 q->last_fft_im[i] = 0;
00125 }
00126
00127
00128
00129 for(i = 0; i < 30; i++) {
00130 q->sqrt_tab[i] = sqrt(i);
00131 }
00132
00133
00134 for(i = 0; i < 4 ; i++) {
00135 for(j = 0; j < 4; j++) {
00136 init_vlc (&q->huffman_vlc[i][j], 9, imc_huffman_sizes[i],
00137 imc_huffman_lens[i][j], 1, 1,
00138 imc_huffman_bits[i][j], 2, 2, 1);
00139 }
00140 }
00141 q->one_div_log2 = 1/log(2);
00142
00143 ff_fft_init(&q->fft, 7, 1);
00144 dsputil_init(&q->dsp, avctx);
00145 return 0;
00146 }
00147
00148 static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT,
00149 float* flcoeffs3, float* flcoeffs5)
00150 {
00151 float workT1[BANDS];
00152 float workT2[BANDS];
00153 float workT3[BANDS];
00154 float snr_limit = 1.e-30;
00155 float accum = 0.0;
00156 int i, cnt2;
00157
00158 for(i = 0; i < BANDS; i++) {
00159 flcoeffs5[i] = workT2[i] = 0.0;
00160 if (bandWidthT[i]){
00161 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
00162 flcoeffs3[i] = 2.0 * flcoeffs2[i];
00163 } else {
00164 workT1[i] = 0.0;
00165 flcoeffs3[i] = -30000.0;
00166 }
00167 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
00168 if (workT3[i] <= snr_limit)
00169 workT3[i] = 0.0;
00170 }
00171
00172 for(i = 0; i < BANDS; i++) {
00173 for(cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
00174 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
00175 workT2[cnt2-1] = workT2[cnt2-1] + workT3[i];
00176 }
00177
00178 for(i = 1; i < BANDS; i++) {
00179 accum = (workT2[i-1] + accum) * imc_weights1[i-1];
00180 flcoeffs5[i] += accum;
00181 }
00182
00183 for(i = 0; i < BANDS; i++)
00184 workT2[i] = 0.0;
00185
00186 for(i = 0; i < BANDS; i++) {
00187 for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--)
00188 flcoeffs5[cnt2] += workT3[i];
00189 workT2[cnt2+1] += workT3[i];
00190 }
00191
00192 accum = 0.0;
00193
00194 for(i = BANDS-2; i >= 0; i--) {
00195 accum = (workT2[i+1] + accum) * imc_weights2[i];
00196 flcoeffs5[i] += accum;
00197
00198 }
00199 }
00200
00201
00202 static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs)
00203 {
00204 int i;
00205 VLC *hufftab[4];
00206 int start = 0;
00207 const uint8_t *cb_sel;
00208 int s;
00209
00210 s = stream_format_code >> 1;
00211 hufftab[0] = &q->huffman_vlc[s][0];
00212 hufftab[1] = &q->huffman_vlc[s][1];
00213 hufftab[2] = &q->huffman_vlc[s][2];
00214 hufftab[3] = &q->huffman_vlc[s][3];
00215 cb_sel = imc_cb_select[s];
00216
00217 if(stream_format_code & 4)
00218 start = 1;
00219 if(start)
00220 levlCoeffs[0] = get_bits(&q->gb, 7);
00221 for(i = start; i < BANDS; i++){
00222 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2);
00223 if(levlCoeffs[i] == 17)
00224 levlCoeffs[i] += get_bits(&q->gb, 4);
00225 }
00226 }
00227
00228 static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1,
00229 float* flcoeffs2)
00230 {
00231 int i, level;
00232 float tmp, tmp2;
00233
00234
00235 flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945);
00236 flcoeffs2[0] = log(flcoeffs1[0])/log(2);
00237 tmp = flcoeffs1[0];
00238 tmp2 = flcoeffs2[0];
00239
00240 for(i = 1; i < BANDS; i++) {
00241 level = levlCoeffBuf[i];
00242 if (level == 16) {
00243 flcoeffs1[i] = 1.0;
00244 flcoeffs2[i] = 0.0;
00245 } else {
00246 if (level < 17)
00247 level -=7;
00248 else if (level <= 24)
00249 level -=32;
00250 else
00251 level -=16;
00252
00253 tmp *= imc_exp_tab[15 + level];
00254 tmp2 += 0.83048 * level;
00255 flcoeffs1[i] = tmp;
00256 flcoeffs2[i] = tmp2;
00257 }
00258 }
00259 }
00260
00261
00262 static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1,
00263 float* flcoeffs2) {
00264 int i;
00265
00266
00267
00268 for(i = 0; i < BANDS; i++) {
00269 flcoeffs1[i] = 0;
00270 if(levlCoeffBuf[i] < 16) {
00271 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
00272 flcoeffs2[i] = (levlCoeffBuf[i]-7) * 0.83048 + flcoeffs2[i];
00273 } else {
00274 flcoeffs1[i] = old_floor[i];
00275 }
00276 }
00277 }
00278
00282 static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) {
00283 int i, j;
00284 const float limit = -1.e20;
00285 float highest = 0.0;
00286 int indx;
00287 int t1 = 0;
00288 int t2 = 1;
00289 float summa = 0.0;
00290 int iacc = 0;
00291 int summer = 0;
00292 int rres, cwlen;
00293 float lowest = 1.e10;
00294 int low_indx = 0;
00295 float workT[32];
00296 int flg;
00297 int found_indx = 0;
00298
00299 for(i = 0; i < BANDS; i++)
00300 highest = FFMAX(highest, q->flcoeffs1[i]);
00301
00302 for(i = 0; i < BANDS-1; i++) {
00303 q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i])/log(2);
00304 }
00305 q->flcoeffs4[BANDS - 1] = limit;
00306
00307 highest = highest * 0.25;
00308
00309 for(i = 0; i < BANDS; i++) {
00310 indx = -1;
00311 if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i])
00312 indx = 0;
00313
00314 if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i])
00315 indx = 1;
00316
00317 if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i])
00318 indx = 2;
00319
00320 if (indx == -1)
00321 return -1;
00322
00323 q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
00324 }
00325
00326 if (stream_format_code & 0x2) {
00327 q->flcoeffs4[0] = limit;
00328 q->flcoeffs4[1] = limit;
00329 q->flcoeffs4[2] = limit;
00330 q->flcoeffs4[3] = limit;
00331 }
00332
00333 for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) {
00334 iacc += q->bandWidthT[i];
00335 summa += q->bandWidthT[i] * q->flcoeffs4[i];
00336 }
00337 q->bandWidthT[BANDS-1] = 0;
00338 summa = (summa * 0.5 - freebits) / iacc;
00339
00340
00341 for(i = 0; i < BANDS/2; i++) {
00342 rres = summer - freebits;
00343 if((rres >= -8) && (rres <= 8)) break;
00344
00345 summer = 0;
00346 iacc = 0;
00347
00348 for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
00349 cwlen = av_clip((int)((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
00350
00351 q->bitsBandT[j] = cwlen;
00352 summer += q->bandWidthT[j] * cwlen;
00353
00354 if (cwlen > 0)
00355 iacc += q->bandWidthT[j];
00356 }
00357
00358 flg = t2;
00359 t2 = 1;
00360 if (freebits < summer)
00361 t2 = -1;
00362 if (i == 0)
00363 flg = t2;
00364 if(flg != t2)
00365 t1++;
00366
00367 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
00368 }
00369
00370 for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) {
00371 for(j = band_tab[i]; j < band_tab[i+1]; j++)
00372 q->CWlengthT[j] = q->bitsBandT[i];
00373 }
00374
00375 if (freebits > summer) {
00376 for(i = 0; i < BANDS; i++) {
00377 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
00378 }
00379
00380 highest = 0.0;
00381
00382 do{
00383 if (highest <= -1.e20)
00384 break;
00385
00386 found_indx = 0;
00387 highest = -1.e20;
00388
00389 for(i = 0; i < BANDS; i++) {
00390 if (workT[i] > highest) {
00391 highest = workT[i];
00392 found_indx = i;
00393 }
00394 }
00395
00396 if (highest > -1.e20) {
00397 workT[found_indx] -= 2.0;
00398 if (++(q->bitsBandT[found_indx]) == 6)
00399 workT[found_indx] = -1.e20;
00400
00401 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
00402 q->CWlengthT[j]++;
00403 summer++;
00404 }
00405 }
00406 }while (freebits > summer);
00407 }
00408 if (freebits < summer) {
00409 for(i = 0; i < BANDS; i++) {
00410 workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
00411 }
00412 if (stream_format_code & 0x2) {
00413 workT[0] = 1.e20;
00414 workT[1] = 1.e20;
00415 workT[2] = 1.e20;
00416 workT[3] = 1.e20;
00417 }
00418 while (freebits < summer){
00419 lowest = 1.e10;
00420 low_indx = 0;
00421 for(i = 0; i < BANDS; i++) {
00422 if (workT[i] < lowest) {
00423 lowest = workT[i];
00424 low_indx = i;
00425 }
00426 }
00427
00428 workT[low_indx] = lowest + 2.0;
00429
00430 if (!(--q->bitsBandT[low_indx]))
00431 workT[low_indx] = 1.e20;
00432
00433 for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
00434 if(q->CWlengthT[j] > 0){
00435 q->CWlengthT[j]--;
00436 summer--;
00437 }
00438 }
00439 }
00440 }
00441 return 0;
00442 }
00443
00444 static void imc_get_skip_coeff(IMCContext* q) {
00445 int i, j;
00446
00447 memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
00448 memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
00449 for(i = 0; i < BANDS; i++) {
00450 if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
00451 continue;
00452
00453 if (!q->skipFlagRaw[i]) {
00454 q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
00455
00456 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00457 if ((q->skipFlags[j] = get_bits1(&q->gb)))
00458 q->skipFlagCount[i]++;
00459 }
00460 } else {
00461 for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
00462 if(!get_bits1(&q->gb)){
00463 q->skipFlagBits[i]++;
00464 q->skipFlags[j]=1;
00465 q->skipFlags[j+1]=1;
00466 q->skipFlagCount[i] += 2;
00467 }else{
00468 if(get_bits1(&q->gb)){
00469 q->skipFlagBits[i] +=2;
00470 q->skipFlags[j]=0;
00471 q->skipFlags[j+1]=1;
00472 q->skipFlagCount[i]++;
00473 }else{
00474 q->skipFlagBits[i] +=3;
00475 q->skipFlags[j+1]=0;
00476 if(!get_bits1(&q->gb)){
00477 q->skipFlags[j]=1;
00478 q->skipFlagCount[i]++;
00479 }else{
00480 q->skipFlags[j]=0;
00481 }
00482 }
00483 }
00484 }
00485
00486 if (j < band_tab[i+1]) {
00487 q->skipFlagBits[i]++;
00488 if ((q->skipFlags[j] = get_bits1(&q->gb)))
00489 q->skipFlagCount[i]++;
00490 }
00491 }
00492 }
00493 }
00494
00498 static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
00499 float workT[32];
00500 int corrected = 0;
00501 int i, j;
00502 float highest = 0;
00503 int found_indx=0;
00504
00505 for(i = 0; i < BANDS; i++) {
00506 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
00507 }
00508
00509 while (corrected < summer) {
00510 if(highest <= -1.e20)
00511 break;
00512
00513 highest = -1.e20;
00514
00515 for(i = 0; i < BANDS; i++) {
00516 if (workT[i] > highest) {
00517 highest = workT[i];
00518 found_indx = i;
00519 }
00520 }
00521
00522 if (highest > -1.e20) {
00523 workT[found_indx] -= 2.0;
00524 if (++(q->bitsBandT[found_indx]) == 6)
00525 workT[found_indx] = -1.e20;
00526
00527 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
00528 if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
00529 q->CWlengthT[j]++;
00530 corrected++;
00531 }
00532 }
00533 }
00534 }
00535 }
00536
00537 static void imc_imdct256(IMCContext *q) {
00538 int i;
00539 float re, im;
00540
00541
00542 for(i=0; i < COEFFS/2; i++){
00543 q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
00544 (q->pre_coef2[i] * q->CWdecoded[i*2]);
00545 q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
00546 (q->pre_coef1[i] * q->CWdecoded[i*2]);
00547 }
00548
00549
00550 ff_fft_permute(&q->fft, q->samples);
00551 ff_fft_calc (&q->fft, q->samples);
00552
00553
00554 for(i = 0; i < COEFFS/2; i++){
00555 re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
00556 im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
00557 q->out_samples[i*2] = (q->mdct_sine_window[COEFFS-1-i*2] * q->last_fft_im[i]) + (q->mdct_sine_window[i*2] * re);
00558 q->out_samples[COEFFS-1-i*2] = (q->mdct_sine_window[i*2] * q->last_fft_im[i]) - (q->mdct_sine_window[COEFFS-1-i*2] * re);
00559 q->last_fft_im[i] = im;
00560 }
00561 }
00562
00563 static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
00564 int i, j;
00565 int middle_value, cw_len, max_size;
00566 const float* quantizer;
00567
00568 for(i = 0; i < BANDS; i++) {
00569 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00570 q->CWdecoded[j] = 0;
00571 cw_len = q->CWlengthT[j];
00572
00573 if (cw_len <= 0 || q->skipFlags[j])
00574 continue;
00575
00576 max_size = 1 << cw_len;
00577 middle_value = max_size >> 1;
00578
00579 if (q->codewords[j] >= max_size || q->codewords[j] < 0)
00580 return -1;
00581
00582 if (cw_len >= 4){
00583 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
00584 if (q->codewords[j] >= middle_value)
00585 q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
00586 else
00587 q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
00588 }else{
00589 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
00590 if (q->codewords[j] >= middle_value)
00591 q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
00592 else
00593 q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
00594 }
00595 }
00596 }
00597 return 0;
00598 }
00599
00600
00601 static int imc_get_coeffs (IMCContext* q) {
00602 int i, j, cw_len, cw;
00603
00604 for(i = 0; i < BANDS; i++) {
00605 if(!q->sumLenArr[i]) continue;
00606 if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
00607 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00608 cw_len = q->CWlengthT[j];
00609 cw = 0;
00610
00611 if (get_bits_count(&q->gb) + cw_len > 512){
00612
00613 return -1;
00614 }
00615
00616 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
00617 cw = get_bits(&q->gb, cw_len);
00618
00619 q->codewords[j] = cw;
00620 }
00621 }
00622 }
00623 return 0;
00624 }
00625
00626 static int imc_decode_frame(AVCodecContext * avctx,
00627 void *data, int *data_size,
00628 uint8_t * buf, int buf_size)
00629 {
00630
00631 IMCContext *q = avctx->priv_data;
00632
00633 int stream_format_code;
00634 int imc_hdr, i, j;
00635 int flag;
00636 int bits, summer;
00637 int counter, bitscount;
00638 uint16_t *buf16 = (uint16_t *) buf;
00639
00640
00641 for(i = 0; i < FFMIN(buf_size, avctx->block_align) / 2; i++)
00642 buf16[i] = bswap_16(buf16[i]);
00643
00644 init_get_bits(&q->gb, buf, 512);
00645
00646
00647 imc_hdr = get_bits(&q->gb, 9);
00648 if (imc_hdr != IMC_FRAME_ID) {
00649 av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
00650 av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
00651 return -1;
00652 }
00653 stream_format_code = get_bits(&q->gb, 3);
00654
00655 if(stream_format_code & 1){
00656 av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
00657 return -1;
00658 }
00659
00660
00661
00662 if (stream_format_code & 0x04)
00663 q->decoder_reset = 1;
00664
00665 if(q->decoder_reset) {
00666 memset(q->out_samples, 0, sizeof(q->out_samples));
00667 for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
00668 for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
00669 q->decoder_reset = 0;
00670 }
00671
00672 flag = get_bits1(&q->gb);
00673 imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
00674
00675 if (stream_format_code & 0x4)
00676 imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2);
00677 else
00678 imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2);
00679
00680 memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
00681
00682 counter = 0;
00683 for (i=0 ; i<BANDS ; i++) {
00684 if (q->levlCoeffBuf[i] == 16) {
00685 q->bandWidthT[i] = 0;
00686 counter++;
00687 } else
00688 q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
00689 }
00690 memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
00691 for(i = 0; i < BANDS-1; i++) {
00692 if (q->bandWidthT[i])
00693 q->bandFlagsBuf[i] = get_bits1(&q->gb);
00694 }
00695
00696 imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5);
00697
00698 bitscount = 0;
00699
00700 if (stream_format_code & 0x2) {
00701 bitscount += 15;
00702
00703 q->bitsBandT[0] = 5;
00704 q->CWlengthT[0] = 5;
00705 q->CWlengthT[1] = 5;
00706 q->CWlengthT[2] = 5;
00707 for(i = 1; i < 4; i++){
00708 bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
00709 q->bitsBandT[i] = bits;
00710 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00711 q->CWlengthT[j] = bits;
00712 bitscount += bits;
00713 }
00714 }
00715 }
00716
00717 if(bit_allocation (q, stream_format_code, 512 - bitscount - get_bits_count(&q->gb), flag) < 0) {
00718 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
00719 q->decoder_reset = 1;
00720 return -1;
00721 }
00722
00723 for(i = 0; i < BANDS; i++) {
00724 q->sumLenArr[i] = 0;
00725 q->skipFlagRaw[i] = 0;
00726 for(j = band_tab[i]; j < band_tab[i+1]; j++)
00727 q->sumLenArr[i] += q->CWlengthT[j];
00728 if (q->bandFlagsBuf[i])
00729 if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
00730 q->skipFlagRaw[i] = 1;
00731 }
00732
00733 imc_get_skip_coeff(q);
00734
00735 for(i = 0; i < BANDS; i++) {
00736 q->flcoeffs6[i] = q->flcoeffs1[i];
00737
00738 if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
00739 q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
00740 q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
00741 }
00742 }
00743
00744
00745 bits = summer = 0;
00746
00747 for(i = 0; i < BANDS; i++) {
00748 if (q->bandFlagsBuf[i]) {
00749 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00750 if(q->skipFlags[j]) {
00751 summer += q->CWlengthT[j];
00752 q->CWlengthT[j] = 0;
00753 }
00754 }
00755 bits += q->skipFlagBits[i];
00756 summer -= q->skipFlagBits[i];
00757 }
00758 }
00759 imc_adjust_bit_allocation(q, summer);
00760
00761 for(i = 0; i < BANDS; i++) {
00762 q->sumLenArr[i] = 0;
00763
00764 for(j = band_tab[i]; j < band_tab[i+1]; j++)
00765 if (!q->skipFlags[j])
00766 q->sumLenArr[i] += q->CWlengthT[j];
00767 }
00768
00769 memset(q->codewords, 0, sizeof(q->codewords));
00770
00771 if(imc_get_coeffs(q) < 0) {
00772 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
00773 q->decoder_reset = 1;
00774 return 0;
00775 }
00776
00777 if(inverse_quant_coeff(q, stream_format_code) < 0) {
00778 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
00779 q->decoder_reset = 1;
00780 return 0;
00781 }
00782
00783 memset(q->skipFlags, 0, sizeof(q->skipFlags));
00784
00785 imc_imdct256(q);
00786
00787 q->dsp.float_to_int16(data, q->out_samples, COEFFS);
00788
00789 *data_size = COEFFS * sizeof(int16_t);
00790
00791 return avctx->block_align;
00792 }
00793
00794
00795 static int imc_decode_close(AVCodecContext * avctx)
00796 {
00797 IMCContext *q = avctx->priv_data;
00798
00799 ff_fft_end(&q->fft);
00800 return 0;
00801 }
00802
00803
00804 AVCodec imc_decoder = {
00805 .name = "imc",
00806 .type = CODEC_TYPE_AUDIO,
00807 .id = CODEC_ID_IMC,
00808 .priv_data_size = sizeof(IMCContext),
00809 .init = imc_decode_init,
00810 .close = imc_decode_close,
00811 .decode = imc_decode_frame,
00812 };