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