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