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