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