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