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