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 "libavutil/channel_layout.h"
00039 #include "libavutil/libm.h"
00040 #include "avcodec.h"
00041 #include "get_bits.h"
00042 #include "dsputil.h"
00043 #include "fft.h"
00044 #include "internal.h"
00045 #include "sinewin.h"
00046
00047 #include "imcdata.h"
00048
00049 #define IMC_BLOCK_SIZE 64
00050 #define IMC_FRAME_ID 0x21
00051 #define BANDS 32
00052 #define COEFFS 256
00053
00054 typedef struct IMCChannel {
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
00064 int bandWidthT[BANDS];
00065 int bitsBandT[BANDS];
00066 int CWlengthT[COEFFS];
00067 int levlCoeffBuf[BANDS];
00068 int bandFlagsBuf[BANDS];
00069 int sumLenArr[BANDS];
00070 int skipFlagRaw[BANDS];
00071 int skipFlagBits[BANDS];
00072 int skipFlagCount[BANDS];
00073 int skipFlags[COEFFS];
00074 int codewords[COEFFS];
00075
00076 float last_fft_im[COEFFS];
00077
00078 int decoder_reset;
00079 } IMCChannel;
00080
00081 typedef struct {
00082 AVFrame frame;
00083
00084 IMCChannel chctx[2];
00085
00088 float mdct_sine_window[COEFFS];
00089 float post_cos[COEFFS];
00090 float post_sin[COEFFS];
00091 float pre_coef1[COEFFS];
00092 float pre_coef2[COEFFS];
00094
00095 float sqrt_tab[30];
00096 GetBitContext gb;
00097
00098 DSPContext dsp;
00099 FFTContext fft;
00100 DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
00101 float *out_samples;
00102
00103 int8_t cyclTab[32], cyclTab2[32];
00104 float weights1[31], weights2[31];
00105 } IMCContext;
00106
00107 static VLC huffman_vlc[4][4];
00108
00109 #define VLC_TABLES_SIZE 9512
00110
00111 static const int vlc_offsets[17] = {
00112 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
00113 4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE
00114 };
00115
00116 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
00117
00118 static inline double freq2bark(double freq)
00119 {
00120 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
00121 }
00122
00123 static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
00124 {
00125 double freqmin[32], freqmid[32], freqmax[32];
00126 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
00127 double nyquist_freq = sampling_rate * 0.5;
00128 double freq, bark, prev_bark = 0, tf, tb;
00129 int i, j;
00130
00131 for (i = 0; i < 32; i++) {
00132 freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
00133 bark = freq2bark(freq);
00134
00135 if (i > 0) {
00136 tb = bark - prev_bark;
00137 q->weights1[i - 1] = pow(10.0, -1.0 * tb);
00138 q->weights2[i - 1] = pow(10.0, -2.7 * tb);
00139 }
00140 prev_bark = bark;
00141
00142 freqmid[i] = freq;
00143
00144 tf = freq;
00145 while (tf < nyquist_freq) {
00146 tf += 0.5;
00147 tb = freq2bark(tf);
00148 if (tb > bark + 0.5)
00149 break;
00150 }
00151 freqmax[i] = tf;
00152
00153 tf = freq;
00154 while (tf > 0.0) {
00155 tf -= 0.5;
00156 tb = freq2bark(tf);
00157 if (tb <= bark - 0.5)
00158 break;
00159 }
00160 freqmin[i] = tf;
00161 }
00162
00163 for (i = 0; i < 32; i++) {
00164 freq = freqmax[i];
00165 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
00166 q->cyclTab[i] = j + 1;
00167
00168 freq = freqmin[i];
00169 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
00170 q->cyclTab2[i] = j - 1;
00171 }
00172 }
00173
00174 static av_cold int imc_decode_init(AVCodecContext *avctx)
00175 {
00176 int i, j, ret;
00177 IMCContext *q = avctx->priv_data;
00178 double r1, r2;
00179
00180 if (avctx->codec_id == AV_CODEC_ID_IMC)
00181 avctx->channels = 1;
00182
00183 if (avctx->channels > 2) {
00184 av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
00185 return AVERROR_PATCHWELCOME;
00186 }
00187
00188 for (j = 0; j < avctx->channels; j++) {
00189 q->chctx[j].decoder_reset = 1;
00190
00191 for (i = 0; i < BANDS; i++)
00192 q->chctx[j].old_floor[i] = 1.0;
00193
00194 for (i = 0; i < COEFFS / 2; i++)
00195 q->chctx[j].last_fft_im[i] = 0;
00196 }
00197
00198
00199 ff_sine_window_init(q->mdct_sine_window, COEFFS);
00200 for (i = 0; i < COEFFS; i++)
00201 q->mdct_sine_window[i] *= sqrt(2.0);
00202 for (i = 0; i < COEFFS / 2; i++) {
00203 q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
00204 q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
00205
00206 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
00207 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
00208
00209 if (i & 0x1) {
00210 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
00211 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
00212 } else {
00213 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
00214 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
00215 }
00216 }
00217
00218
00219
00220 for (i = 0; i < 30; i++)
00221 q->sqrt_tab[i] = sqrt(i);
00222
00223
00224 for (i = 0; i < 4 ; i++) {
00225 for (j = 0; j < 4; j++) {
00226 huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
00227 huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
00228 init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
00229 imc_huffman_lens[i][j], 1, 1,
00230 imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
00231 }
00232 }
00233
00234 if (avctx->codec_id == AV_CODEC_ID_IAC) {
00235 iac_generate_tabs(q, avctx->sample_rate);
00236 } else {
00237 memcpy(q->cyclTab, cyclTab, sizeof(cyclTab));
00238 memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
00239 memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
00240 memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
00241 }
00242
00243 if ((ret = ff_fft_init(&q->fft, 7, 1))) {
00244 av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
00245 return ret;
00246 }
00247 ff_dsputil_init(&q->dsp, avctx);
00248 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
00249 avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
00250 : AV_CH_LAYOUT_STEREO;
00251
00252 avcodec_get_frame_defaults(&q->frame);
00253 avctx->coded_frame = &q->frame;
00254
00255 return 0;
00256 }
00257
00258 static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
00259 float *flcoeffs2, int *bandWidthT,
00260 float *flcoeffs3, float *flcoeffs5)
00261 {
00262 float workT1[BANDS];
00263 float workT2[BANDS];
00264 float workT3[BANDS];
00265 float snr_limit = 1.e-30;
00266 float accum = 0.0;
00267 int i, cnt2;
00268
00269 for (i = 0; i < BANDS; i++) {
00270 flcoeffs5[i] = workT2[i] = 0.0;
00271 if (bandWidthT[i]) {
00272 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
00273 flcoeffs3[i] = 2.0 * flcoeffs2[i];
00274 } else {
00275 workT1[i] = 0.0;
00276 flcoeffs3[i] = -30000.0;
00277 }
00278 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
00279 if (workT3[i] <= snr_limit)
00280 workT3[i] = 0.0;
00281 }
00282
00283 for (i = 0; i < BANDS; i++) {
00284 for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
00285 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
00286 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
00287 }
00288
00289 for (i = 1; i < BANDS; i++) {
00290 accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
00291 flcoeffs5[i] += accum;
00292 }
00293
00294 for (i = 0; i < BANDS; i++)
00295 workT2[i] = 0.0;
00296
00297 for (i = 0; i < BANDS; i++) {
00298 for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
00299 flcoeffs5[cnt2] += workT3[i];
00300 workT2[cnt2+1] += workT3[i];
00301 }
00302
00303 accum = 0.0;
00304
00305 for (i = BANDS-2; i >= 0; i--) {
00306 accum = (workT2[i+1] + accum) * q->weights2[i];
00307 flcoeffs5[i] += accum;
00308
00309 }
00310 }
00311
00312
00313 static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
00314 int *levlCoeffs)
00315 {
00316 int i;
00317 VLC *hufftab[4];
00318 int start = 0;
00319 const uint8_t *cb_sel;
00320 int s;
00321
00322 s = stream_format_code >> 1;
00323 hufftab[0] = &huffman_vlc[s][0];
00324 hufftab[1] = &huffman_vlc[s][1];
00325 hufftab[2] = &huffman_vlc[s][2];
00326 hufftab[3] = &huffman_vlc[s][3];
00327 cb_sel = imc_cb_select[s];
00328
00329 if (stream_format_code & 4)
00330 start = 1;
00331 if (start)
00332 levlCoeffs[0] = get_bits(&q->gb, 7);
00333 for (i = start; i < BANDS; i++) {
00334 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
00335 hufftab[cb_sel[i]]->bits, 2);
00336 if (levlCoeffs[i] == 17)
00337 levlCoeffs[i] += get_bits(&q->gb, 4);
00338 }
00339 }
00340
00341 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
00342 float *flcoeffs1, float *flcoeffs2)
00343 {
00344 int i, level;
00345 float tmp, tmp2;
00346
00347
00348 flcoeffs1[0] = 20000.0 / exp2 (levlCoeffBuf[0] * 0.18945);
00349 flcoeffs2[0] = log2f(flcoeffs1[0]);
00350 tmp = flcoeffs1[0];
00351 tmp2 = flcoeffs2[0];
00352
00353 for (i = 1; i < BANDS; i++) {
00354 level = levlCoeffBuf[i];
00355 if (level == 16) {
00356 flcoeffs1[i] = 1.0;
00357 flcoeffs2[i] = 0.0;
00358 } else {
00359 if (level < 17)
00360 level -= 7;
00361 else if (level <= 24)
00362 level -= 32;
00363 else
00364 level -= 16;
00365
00366 tmp *= imc_exp_tab[15 + level];
00367 tmp2 += 0.83048 * level;
00368 flcoeffs1[i] = tmp;
00369 flcoeffs2[i] = tmp2;
00370 }
00371 }
00372 }
00373
00374
00375 static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
00376 float *old_floor, float *flcoeffs1,
00377 float *flcoeffs2)
00378 {
00379 int i;
00380
00381
00382
00383
00384 for (i = 0; i < BANDS; i++) {
00385 flcoeffs1[i] = 0;
00386 if (levlCoeffBuf[i] < 16) {
00387 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
00388 flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i];
00389 } else {
00390 flcoeffs1[i] = old_floor[i];
00391 }
00392 }
00393 }
00394
00398 static int bit_allocation(IMCContext *q, IMCChannel *chctx,
00399 int stream_format_code, int freebits, int flag)
00400 {
00401 int i, j;
00402 const float limit = -1.e20;
00403 float highest = 0.0;
00404 int indx;
00405 int t1 = 0;
00406 int t2 = 1;
00407 float summa = 0.0;
00408 int iacc = 0;
00409 int summer = 0;
00410 int rres, cwlen;
00411 float lowest = 1.e10;
00412 int low_indx = 0;
00413 float workT[32];
00414 int flg;
00415 int found_indx = 0;
00416
00417 for (i = 0; i < BANDS; i++)
00418 highest = FFMAX(highest, chctx->flcoeffs1[i]);
00419
00420 for (i = 0; i < BANDS - 1; i++)
00421 chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
00422 chctx->flcoeffs4[BANDS - 1] = limit;
00423
00424 highest = highest * 0.25;
00425
00426 for (i = 0; i < BANDS; i++) {
00427 indx = -1;
00428 if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
00429 indx = 0;
00430
00431 if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
00432 indx = 1;
00433
00434 if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
00435 indx = 2;
00436
00437 if (indx == -1)
00438 return AVERROR_INVALIDDATA;
00439
00440 chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
00441 }
00442
00443 if (stream_format_code & 0x2) {
00444 chctx->flcoeffs4[0] = limit;
00445 chctx->flcoeffs4[1] = limit;
00446 chctx->flcoeffs4[2] = limit;
00447 chctx->flcoeffs4[3] = limit;
00448 }
00449
00450 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
00451 iacc += chctx->bandWidthT[i];
00452 summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
00453 }
00454 chctx->bandWidthT[BANDS - 1] = 0;
00455 summa = (summa * 0.5 - freebits) / iacc;
00456
00457
00458 for (i = 0; i < BANDS / 2; i++) {
00459 rres = summer - freebits;
00460 if ((rres >= -8) && (rres <= 8))
00461 break;
00462
00463 summer = 0;
00464 iacc = 0;
00465
00466 for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
00467 cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
00468
00469 chctx->bitsBandT[j] = cwlen;
00470 summer += chctx->bandWidthT[j] * cwlen;
00471
00472 if (cwlen > 0)
00473 iacc += chctx->bandWidthT[j];
00474 }
00475
00476 flg = t2;
00477 t2 = 1;
00478 if (freebits < summer)
00479 t2 = -1;
00480 if (i == 0)
00481 flg = t2;
00482 if (flg != t2)
00483 t1++;
00484
00485 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
00486 }
00487
00488 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
00489 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
00490 chctx->CWlengthT[j] = chctx->bitsBandT[i];
00491 }
00492
00493 if (freebits > summer) {
00494 for (i = 0; i < BANDS; i++) {
00495 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
00496 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
00497 }
00498
00499 highest = 0.0;
00500
00501 do {
00502 if (highest <= -1.e20)
00503 break;
00504
00505 found_indx = 0;
00506 highest = -1.e20;
00507
00508 for (i = 0; i < BANDS; i++) {
00509 if (workT[i] > highest) {
00510 highest = workT[i];
00511 found_indx = i;
00512 }
00513 }
00514
00515 if (highest > -1.e20) {
00516 workT[found_indx] -= 2.0;
00517 if (++chctx->bitsBandT[found_indx] == 6)
00518 workT[found_indx] = -1.e20;
00519
00520 for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
00521 chctx->CWlengthT[j]++;
00522 summer++;
00523 }
00524 }
00525 } while (freebits > summer);
00526 }
00527 if (freebits < summer) {
00528 for (i = 0; i < BANDS; i++) {
00529 workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
00530 : 1.e20;
00531 }
00532 if (stream_format_code & 0x2) {
00533 workT[0] = 1.e20;
00534 workT[1] = 1.e20;
00535 workT[2] = 1.e20;
00536 workT[3] = 1.e20;
00537 }
00538 while (freebits < summer) {
00539 lowest = 1.e10;
00540 low_indx = 0;
00541 for (i = 0; i < BANDS; i++) {
00542 if (workT[i] < lowest) {
00543 lowest = workT[i];
00544 low_indx = i;
00545 }
00546 }
00547
00548
00549 workT[low_indx] = lowest + 2.0;
00550
00551 if (!--chctx->bitsBandT[low_indx])
00552 workT[low_indx] = 1.e20;
00553
00554 for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
00555 if (chctx->CWlengthT[j] > 0) {
00556 chctx->CWlengthT[j]--;
00557 summer--;
00558 }
00559 }
00560 }
00561 }
00562 return 0;
00563 }
00564
00565 static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
00566 {
00567 int i, j;
00568
00569 memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
00570 memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
00571 for (i = 0; i < BANDS; i++) {
00572 if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
00573 continue;
00574
00575 if (!chctx->skipFlagRaw[i]) {
00576 chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
00577
00578 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
00579 chctx->skipFlags[j] = get_bits1(&q->gb);
00580 if (chctx->skipFlags[j])
00581 chctx->skipFlagCount[i]++;
00582 }
00583 } else {
00584 for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
00585 if (!get_bits1(&q->gb)) {
00586 chctx->skipFlagBits[i]++;
00587 chctx->skipFlags[j] = 1;
00588 chctx->skipFlags[j + 1] = 1;
00589 chctx->skipFlagCount[i] += 2;
00590 } else {
00591 if (get_bits1(&q->gb)) {
00592 chctx->skipFlagBits[i] += 2;
00593 chctx->skipFlags[j] = 0;
00594 chctx->skipFlags[j + 1] = 1;
00595 chctx->skipFlagCount[i]++;
00596 } else {
00597 chctx->skipFlagBits[i] += 3;
00598 chctx->skipFlags[j + 1] = 0;
00599 if (!get_bits1(&q->gb)) {
00600 chctx->skipFlags[j] = 1;
00601 chctx->skipFlagCount[i]++;
00602 } else {
00603 chctx->skipFlags[j] = 0;
00604 }
00605 }
00606 }
00607 }
00608
00609 if (j < band_tab[i + 1]) {
00610 chctx->skipFlagBits[i]++;
00611 if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
00612 chctx->skipFlagCount[i]++;
00613 }
00614 }
00615 }
00616 }
00617
00621 static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
00622 int summer)
00623 {
00624 float workT[32];
00625 int corrected = 0;
00626 int i, j;
00627 float highest = 0;
00628 int found_indx = 0;
00629
00630 for (i = 0; i < BANDS; i++) {
00631 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
00632 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
00633 }
00634
00635 while (corrected < summer) {
00636 if (highest <= -1.e20)
00637 break;
00638
00639 highest = -1.e20;
00640
00641 for (i = 0; i < BANDS; i++) {
00642 if (workT[i] > highest) {
00643 highest = workT[i];
00644 found_indx = i;
00645 }
00646 }
00647
00648 if (highest > -1.e20) {
00649 workT[found_indx] -= 2.0;
00650 if (++(chctx->bitsBandT[found_indx]) == 6)
00651 workT[found_indx] = -1.e20;
00652
00653 for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
00654 if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
00655 chctx->CWlengthT[j]++;
00656 corrected++;
00657 }
00658 }
00659 }
00660 }
00661 }
00662
00663 static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
00664 {
00665 int i;
00666 float re, im;
00667 float *dst1 = q->out_samples;
00668 float *dst2 = q->out_samples + (COEFFS - 1);
00669
00670
00671 for (i = 0; i < COEFFS / 2; i++) {
00672 q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
00673 (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
00674 q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
00675 (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
00676 }
00677
00678
00679 q->fft.fft_permute(&q->fft, q->samples);
00680 q->fft.fft_calc(&q->fft, q->samples);
00681
00682
00683 for (i = 0; i < COEFFS / 2; i++) {
00684 re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
00685 im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
00686 *dst1 = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
00687 + (q->mdct_sine_window[i * 2] * re);
00688 *dst2 = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
00689 - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
00690 dst1 += 2;
00691 dst2 -= 2;
00692 chctx->last_fft_im[i] = im;
00693 }
00694 }
00695
00696 static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
00697 int stream_format_code)
00698 {
00699 int i, j;
00700 int middle_value, cw_len, max_size;
00701 const float *quantizer;
00702
00703 for (i = 0; i < BANDS; i++) {
00704 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
00705 chctx->CWdecoded[j] = 0;
00706 cw_len = chctx->CWlengthT[j];
00707
00708 if (cw_len <= 0 || chctx->skipFlags[j])
00709 continue;
00710
00711 max_size = 1 << cw_len;
00712 middle_value = max_size >> 1;
00713
00714 if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
00715 return AVERROR_INVALIDDATA;
00716
00717 if (cw_len >= 4) {
00718 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
00719 if (chctx->codewords[j] >= middle_value)
00720 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
00721 else
00722 chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
00723 }else{
00724 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
00725 if (chctx->codewords[j] >= middle_value)
00726 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
00727 else
00728 chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
00729 }
00730 }
00731 }
00732 return 0;
00733 }
00734
00735
00736 static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
00737 {
00738 int i, j, cw_len, cw;
00739
00740 for (i = 0; i < BANDS; i++) {
00741 if (!chctx->sumLenArr[i])
00742 continue;
00743 if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
00744 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
00745 cw_len = chctx->CWlengthT[j];
00746 cw = 0;
00747
00748 if (get_bits_count(&q->gb) + cw_len > 512) {
00749 av_dlog(NULL, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
00750 return AVERROR_INVALIDDATA;
00751 }
00752
00753 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
00754 cw = get_bits(&q->gb, cw_len);
00755
00756 chctx->codewords[j] = cw;
00757 }
00758 }
00759 }
00760 return 0;
00761 }
00762
00763 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
00764 {
00765 int stream_format_code;
00766 int imc_hdr, i, j, ret;
00767 int flag;
00768 int bits, summer;
00769 int counter, bitscount;
00770 IMCChannel *chctx = q->chctx + ch;
00771
00772
00773
00774 imc_hdr = get_bits(&q->gb, 9);
00775 if (imc_hdr & 0x18) {
00776 av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
00777 av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
00778 return AVERROR_INVALIDDATA;
00779 }
00780 stream_format_code = get_bits(&q->gb, 3);
00781
00782 if (stream_format_code & 1) {
00783 av_log_ask_for_sample(avctx, "Stream format %X is not supported\n",
00784 stream_format_code);
00785 return AVERROR_PATCHWELCOME;
00786 }
00787
00788 if (stream_format_code & 0x04)
00789 chctx->decoder_reset = 1;
00790
00791 if (chctx->decoder_reset) {
00792 for (i = 0; i < BANDS; i++)
00793 chctx->old_floor[i] = 1.0;
00794 for (i = 0; i < COEFFS; i++)
00795 chctx->CWdecoded[i] = 0;
00796 chctx->decoder_reset = 0;
00797 }
00798
00799 flag = get_bits1(&q->gb);
00800 imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
00801
00802 if (stream_format_code & 0x4)
00803 imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
00804 chctx->flcoeffs1, chctx->flcoeffs2);
00805 else
00806 imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
00807 chctx->flcoeffs1, chctx->flcoeffs2);
00808
00809 for(i=0; i<BANDS; i++) {
00810 if(chctx->flcoeffs1[i] > INT_MAX) {
00811 av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n");
00812 return AVERROR_INVALIDDATA;
00813 }
00814 }
00815
00816 memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
00817
00818 counter = 0;
00819 for (i = 0; i < BANDS; i++) {
00820 if (chctx->levlCoeffBuf[i] == 16) {
00821 chctx->bandWidthT[i] = 0;
00822 counter++;
00823 } else
00824 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
00825 }
00826 memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
00827 for (i = 0; i < BANDS - 1; i++) {
00828 if (chctx->bandWidthT[i])
00829 chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
00830 }
00831
00832 imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2, chctx->bandWidthT, chctx->flcoeffs3, chctx->flcoeffs5);
00833
00834 bitscount = 0;
00835
00836 if (stream_format_code & 0x2) {
00837 bitscount += 15;
00838
00839 chctx->bitsBandT[0] = 5;
00840 chctx->CWlengthT[0] = 5;
00841 chctx->CWlengthT[1] = 5;
00842 chctx->CWlengthT[2] = 5;
00843 for (i = 1; i < 4; i++) {
00844 bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
00845 chctx->bitsBandT[i] = bits;
00846 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
00847 chctx->CWlengthT[j] = bits;
00848 bitscount += bits;
00849 }
00850 }
00851 }
00852 if (avctx->codec_id == AV_CODEC_ID_IAC) {
00853 bitscount += !!chctx->bandWidthT[BANDS - 1];
00854 if (!(stream_format_code & 0x2))
00855 bitscount += 16;
00856 }
00857
00858 if ((ret = bit_allocation(q, chctx, stream_format_code,
00859 512 - bitscount - get_bits_count(&q->gb),
00860 flag)) < 0) {
00861 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
00862 chctx->decoder_reset = 1;
00863 return ret;
00864 }
00865
00866 for (i = 0; i < BANDS; i++) {
00867 chctx->sumLenArr[i] = 0;
00868 chctx->skipFlagRaw[i] = 0;
00869 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
00870 chctx->sumLenArr[i] += chctx->CWlengthT[j];
00871 if (chctx->bandFlagsBuf[i])
00872 if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
00873 chctx->skipFlagRaw[i] = 1;
00874 }
00875
00876 imc_get_skip_coeff(q, chctx);
00877
00878 for (i = 0; i < BANDS; i++) {
00879 chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
00880
00881 if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
00882 chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
00883 q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
00884 }
00885 }
00886
00887
00888 bits = summer = 0;
00889
00890 for (i = 0; i < BANDS; i++) {
00891 if (chctx->bandFlagsBuf[i]) {
00892 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
00893 if (chctx->skipFlags[j]) {
00894 summer += chctx->CWlengthT[j];
00895 chctx->CWlengthT[j] = 0;
00896 }
00897 }
00898 bits += chctx->skipFlagBits[i];
00899 summer -= chctx->skipFlagBits[i];
00900 }
00901 }
00902 imc_adjust_bit_allocation(q, chctx, summer);
00903
00904 for (i = 0; i < BANDS; i++) {
00905 chctx->sumLenArr[i] = 0;
00906
00907 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
00908 if (!chctx->skipFlags[j])
00909 chctx->sumLenArr[i] += chctx->CWlengthT[j];
00910 }
00911
00912 memset(chctx->codewords, 0, sizeof(chctx->codewords));
00913
00914 if (imc_get_coeffs(q, chctx) < 0) {
00915 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
00916 chctx->decoder_reset = 1;
00917 return AVERROR_INVALIDDATA;
00918 }
00919
00920 if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
00921 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
00922 chctx->decoder_reset = 1;
00923 return AVERROR_INVALIDDATA;
00924 }
00925
00926 memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
00927
00928 imc_imdct256(q, chctx, avctx->channels);
00929
00930 return 0;
00931 }
00932
00933 static int imc_decode_frame(AVCodecContext *avctx, void *data,
00934 int *got_frame_ptr, AVPacket *avpkt)
00935 {
00936 const uint8_t *buf = avpkt->data;
00937 int buf_size = avpkt->size;
00938 int ret, i;
00939
00940 IMCContext *q = avctx->priv_data;
00941
00942 LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
00943
00944 if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
00945 av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
00946 return AVERROR_INVALIDDATA;
00947 }
00948
00949
00950 q->frame.nb_samples = COEFFS;
00951 if ((ret = ff_get_buffer(avctx, &q->frame)) < 0) {
00952 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00953 return ret;
00954 }
00955
00956 for (i = 0; i < avctx->channels; i++) {
00957 q->out_samples = (float *)q->frame.extended_data[i];
00958
00959 q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
00960
00961 init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
00962
00963 buf += IMC_BLOCK_SIZE;
00964
00965 if ((ret = imc_decode_block(avctx, q, i)) < 0)
00966 return ret;
00967 }
00968
00969 if (avctx->channels == 2) {
00970 q->dsp.butterflies_float((float *)q->frame.extended_data[0],
00971 (float *)q->frame.extended_data[1], COEFFS);
00972 }
00973
00974 *got_frame_ptr = 1;
00975 *(AVFrame *)data = q->frame;
00976
00977 return IMC_BLOCK_SIZE * avctx->channels;
00978 }
00979
00980
00981 static av_cold int imc_decode_close(AVCodecContext * avctx)
00982 {
00983 IMCContext *q = avctx->priv_data;
00984
00985 ff_fft_end(&q->fft);
00986
00987 return 0;
00988 }
00989
00990 static av_cold void flush(AVCodecContext *avctx)
00991 {
00992 IMCContext *q = avctx->priv_data;
00993
00994 q->chctx[0].decoder_reset =
00995 q->chctx[1].decoder_reset = 1;
00996 }
00997
00998 #if CONFIG_IMC_DECODER
00999 AVCodec ff_imc_decoder = {
01000 .name = "imc",
01001 .type = AVMEDIA_TYPE_AUDIO,
01002 .id = AV_CODEC_ID_IMC,
01003 .priv_data_size = sizeof(IMCContext),
01004 .init = imc_decode_init,
01005 .close = imc_decode_close,
01006 .decode = imc_decode_frame,
01007 .flush = flush,
01008 .capabilities = CODEC_CAP_DR1,
01009 .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
01010 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
01011 AV_SAMPLE_FMT_NONE },
01012 };
01013 #endif
01014 #if CONFIG_IAC_DECODER
01015 AVCodec ff_iac_decoder = {
01016 .name = "iac",
01017 .type = AVMEDIA_TYPE_AUDIO,
01018 .id = AV_CODEC_ID_IAC,
01019 .priv_data_size = sizeof(IMCContext),
01020 .init = imc_decode_init,
01021 .close = imc_decode_close,
01022 .decode = imc_decode_frame,
01023 .flush = flush,
01024 .capabilities = CODEC_CAP_DR1,
01025 .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
01026 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
01027 AV_SAMPLE_FMT_NONE },
01028 };
01029 #endif