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