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