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