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 "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     /* Build mdct window, a simple sine window normalized with sqrt(2) */
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     /* Generate a square root table */
00148 
00149     for(i = 0; i < 30; i++) {
00150         q->sqrt_tab[i] = sqrt(i);
00151     }
00152 
00153     /* initialize the VLC tables */
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         //there is missing code here, but it seems to never be triggered
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     //maybe some frequency division thingy
00265 
00266     flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
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;  // 0.83048 = log2(10) * 0.25
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         //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
00297         //      and flcoeffs2 old scale factors
00298         //      might be incomplete due to a missing table that is in the binary code
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]; // 0.83048 = log2(10) * 0.25
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     q->bandWidthT[BANDS-1] = 0;
00369     summa = (summa * 0.5 - freebits) / iacc;
00370 
00371 
00372     for(i = 0; i < BANDS/2; i++) {
00373         rres = summer - freebits;
00374         if((rres >= -8) && (rres <= 8)) break;
00375 
00376         summer = 0;
00377         iacc = 0;
00378 
00379         for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
00380             cwlen = av_clipf(((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
00381 
00382             q->bitsBandT[j] = cwlen;
00383             summer += q->bandWidthT[j] * cwlen;
00384 
00385             if (cwlen > 0)
00386                 iacc += q->bandWidthT[j];
00387         }
00388 
00389         flg = t2;
00390         t2 = 1;
00391         if (freebits < summer)
00392             t2 = -1;
00393         if (i == 0)
00394             flg = t2;
00395         if(flg != t2)
00396             t1++;
00397 
00398         summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
00399     }
00400 
00401     for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) {
00402         for(j = band_tab[i]; j < band_tab[i+1]; j++)
00403             q->CWlengthT[j] = q->bitsBandT[i];
00404     }
00405 
00406     if (freebits > summer) {
00407         for(i = 0; i < BANDS; i++) {
00408             workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
00409         }
00410 
00411         highest = 0.0;
00412 
00413         do{
00414             if (highest <= -1.e20)
00415                 break;
00416 
00417             found_indx = 0;
00418             highest = -1.e20;
00419 
00420             for(i = 0; i < BANDS; i++) {
00421                 if (workT[i] > highest) {
00422                     highest = workT[i];
00423                     found_indx = i;
00424                 }
00425             }
00426 
00427             if (highest > -1.e20) {
00428                 workT[found_indx] -= 2.0;
00429                 if (++(q->bitsBandT[found_indx]) == 6)
00430                     workT[found_indx] = -1.e20;
00431 
00432                 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
00433                     q->CWlengthT[j]++;
00434                     summer++;
00435                 }
00436             }
00437         }while (freebits > summer);
00438     }
00439     if (freebits < summer) {
00440         for(i = 0; i < BANDS; i++) {
00441             workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
00442         }
00443         if (stream_format_code & 0x2) {
00444             workT[0] = 1.e20;
00445             workT[1] = 1.e20;
00446             workT[2] = 1.e20;
00447             workT[3] = 1.e20;
00448         }
00449         while (freebits < summer){
00450             lowest = 1.e10;
00451             low_indx = 0;
00452             for(i = 0; i < BANDS; i++) {
00453                 if (workT[i] < lowest) {
00454                     lowest = workT[i];
00455                     low_indx = i;
00456                 }
00457             }
00458             //if(lowest >= 1.e10) break;
00459             workT[low_indx] = lowest + 2.0;
00460 
00461             if (!(--q->bitsBandT[low_indx]))
00462                 workT[low_indx] = 1.e20;
00463 
00464             for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
00465                 if(q->CWlengthT[j] > 0){
00466                     q->CWlengthT[j]--;
00467                     summer--;
00468                 }
00469             }
00470         }
00471     }
00472     return 0;
00473 }
00474 
00475 static void imc_get_skip_coeff(IMCContext* q) {
00476     int i, j;
00477 
00478     memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
00479     memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
00480     for(i = 0; i < BANDS; i++) {
00481         if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
00482             continue;
00483 
00484         if (!q->skipFlagRaw[i]) {
00485             q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
00486 
00487             for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00488                 if ((q->skipFlags[j] = get_bits1(&q->gb)))
00489                     q->skipFlagCount[i]++;
00490             }
00491         } else {
00492             for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
00493                 if(!get_bits1(&q->gb)){//0
00494                     q->skipFlagBits[i]++;
00495                     q->skipFlags[j]=1;
00496                     q->skipFlags[j+1]=1;
00497                     q->skipFlagCount[i] += 2;
00498                 }else{
00499                     if(get_bits1(&q->gb)){//11
00500                         q->skipFlagBits[i] +=2;
00501                         q->skipFlags[j]=0;
00502                         q->skipFlags[j+1]=1;
00503                         q->skipFlagCount[i]++;
00504                     }else{
00505                         q->skipFlagBits[i] +=3;
00506                         q->skipFlags[j+1]=0;
00507                         if(!get_bits1(&q->gb)){//100
00508                             q->skipFlags[j]=1;
00509                             q->skipFlagCount[i]++;
00510                         }else{//101
00511                             q->skipFlags[j]=0;
00512                         }
00513                     }
00514                 }
00515             }
00516 
00517             if (j < band_tab[i+1]) {
00518                 q->skipFlagBits[i]++;
00519                 if ((q->skipFlags[j] = get_bits1(&q->gb)))
00520                     q->skipFlagCount[i]++;
00521             }
00522         }
00523     }
00524 }
00525 
00529 static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
00530     float workT[32];
00531     int corrected = 0;
00532     int i, j;
00533     float highest = 0;
00534     int found_indx=0;
00535 
00536     for(i = 0; i < BANDS; i++) {
00537         workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
00538     }
00539 
00540     while (corrected < summer) {
00541         if(highest <= -1.e20)
00542             break;
00543 
00544         highest = -1.e20;
00545 
00546         for(i = 0; i < BANDS; i++) {
00547             if (workT[i] > highest) {
00548                 highest = workT[i];
00549                 found_indx = i;
00550             }
00551         }
00552 
00553         if (highest > -1.e20) {
00554             workT[found_indx] -= 2.0;
00555             if (++(q->bitsBandT[found_indx]) == 6)
00556                 workT[found_indx] = -1.e20;
00557 
00558             for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
00559                 if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
00560                     q->CWlengthT[j]++;
00561                     corrected++;
00562                 }
00563             }
00564         }
00565     }
00566 }
00567 
00568 static void imc_imdct256(IMCContext *q) {
00569     int i;
00570     float re, im;
00571 
00572     /* prerotation */
00573     for(i=0; i < COEFFS/2; i++){
00574         q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
00575                            (q->pre_coef2[i] * q->CWdecoded[i*2]);
00576         q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
00577                            (q->pre_coef1[i] * q->CWdecoded[i*2]);
00578     }
00579 
00580     /* FFT */
00581     q->fft.fft_permute(&q->fft, q->samples);
00582     q->fft.fft_calc   (&q->fft, q->samples);
00583 
00584     /* postrotation, window and reorder */
00585     for(i = 0; i < COEFFS/2; i++){
00586         re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
00587         im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
00588         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);
00589         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);
00590         q->last_fft_im[i] = im;
00591     }
00592 }
00593 
00594 static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
00595     int i, j;
00596     int middle_value, cw_len, max_size;
00597     const float* quantizer;
00598 
00599     for(i = 0; i < BANDS; i++) {
00600         for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00601             q->CWdecoded[j] = 0;
00602             cw_len = q->CWlengthT[j];
00603 
00604             if (cw_len <= 0 || q->skipFlags[j])
00605                 continue;
00606 
00607             max_size = 1 << cw_len;
00608             middle_value = max_size >> 1;
00609 
00610             if (q->codewords[j] >= max_size || q->codewords[j] < 0)
00611                 return AVERROR_INVALIDDATA;
00612 
00613             if (cw_len >= 4){
00614                 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
00615                 if (q->codewords[j] >= middle_value)
00616                     q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
00617                 else
00618                     q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
00619             }else{
00620                 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
00621                 if (q->codewords[j] >= middle_value)
00622                     q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
00623                 else
00624                     q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
00625             }
00626         }
00627     }
00628     return 0;
00629 }
00630 
00631 
00632 static int imc_get_coeffs (IMCContext* q) {
00633     int i, j, cw_len, cw;
00634 
00635     for(i = 0; i < BANDS; i++) {
00636         if(!q->sumLenArr[i]) continue;
00637         if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
00638             for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00639                 cw_len = q->CWlengthT[j];
00640                 cw = 0;
00641 
00642                 if (get_bits_count(&q->gb) + cw_len > 512){
00643 //av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
00644                     return AVERROR_INVALIDDATA;
00645                 }
00646 
00647                 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
00648                     cw = get_bits(&q->gb, cw_len);
00649 
00650                 q->codewords[j] = cw;
00651             }
00652         }
00653     }
00654     return 0;
00655 }
00656 
00657 static int imc_decode_frame(AVCodecContext * avctx, void *data,
00658                             int *got_frame_ptr, AVPacket *avpkt)
00659 {
00660     const uint8_t *buf = avpkt->data;
00661     int buf_size = avpkt->size;
00662 
00663     IMCContext *q = avctx->priv_data;
00664 
00665     int stream_format_code;
00666     int imc_hdr, i, j, ret;
00667     int flag;
00668     int bits, summer;
00669     int counter, bitscount;
00670     LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
00671 
00672     if (buf_size < IMC_BLOCK_SIZE) {
00673         av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
00674         return AVERROR_INVALIDDATA;
00675     }
00676 
00677     /* get output buffer */
00678     q->frame.nb_samples = COEFFS;
00679     if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
00680         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00681         return ret;
00682     }
00683     q->out_samples = (float *)q->frame.data[0];
00684 
00685     q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
00686 
00687     init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
00688 
00689     /* Check the frame header */
00690     imc_hdr = get_bits(&q->gb, 9);
00691     if (imc_hdr != IMC_FRAME_ID) {
00692         av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
00693         av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
00694         return AVERROR_INVALIDDATA;
00695     }
00696     stream_format_code = get_bits(&q->gb, 3);
00697 
00698     if(stream_format_code & 1){
00699         av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
00700         return AVERROR_INVALIDDATA;
00701     }
00702 
00703 //    av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
00704 
00705     if (stream_format_code & 0x04)
00706         q->decoder_reset = 1;
00707 
00708     if(q->decoder_reset) {
00709         memset(q->out_samples, 0, sizeof(q->out_samples));
00710         for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
00711         for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
00712         q->decoder_reset = 0;
00713     }
00714 
00715     flag = get_bits1(&q->gb);
00716     imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
00717 
00718     if (stream_format_code & 0x4)
00719         imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2);
00720     else
00721         imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2);
00722 
00723     memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
00724 
00725     counter = 0;
00726     for (i=0 ; i<BANDS ; i++) {
00727         if (q->levlCoeffBuf[i] == 16) {
00728             q->bandWidthT[i] = 0;
00729             counter++;
00730         } else
00731             q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
00732     }
00733     memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
00734     for(i = 0; i < BANDS-1; i++) {
00735         if (q->bandWidthT[i])
00736             q->bandFlagsBuf[i] = get_bits1(&q->gb);
00737     }
00738 
00739     imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5);
00740 
00741     bitscount = 0;
00742     /* first 4 bands will be assigned 5 bits per coefficient */
00743     if (stream_format_code & 0x2) {
00744         bitscount += 15;
00745 
00746         q->bitsBandT[0] = 5;
00747         q->CWlengthT[0] = 5;
00748         q->CWlengthT[1] = 5;
00749         q->CWlengthT[2] = 5;
00750         for(i = 1; i < 4; i++){
00751             bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
00752             q->bitsBandT[i] = bits;
00753             for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00754                 q->CWlengthT[j] = bits;
00755                 bitscount += bits;
00756             }
00757         }
00758     }
00759 
00760     if((ret = bit_allocation (q, stream_format_code,
00761                               512 - bitscount - get_bits_count(&q->gb), flag)) < 0) {
00762         av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
00763         q->decoder_reset = 1;
00764         return ret;
00765     }
00766 
00767     for(i = 0; i < BANDS; i++) {
00768         q->sumLenArr[i] = 0;
00769         q->skipFlagRaw[i] = 0;
00770         for(j = band_tab[i]; j < band_tab[i+1]; j++)
00771             q->sumLenArr[i] += q->CWlengthT[j];
00772         if (q->bandFlagsBuf[i])
00773             if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
00774                 q->skipFlagRaw[i] = 1;
00775     }
00776 
00777     imc_get_skip_coeff(q);
00778 
00779     for(i = 0; i < BANDS; i++) {
00780         q->flcoeffs6[i] = q->flcoeffs1[i];
00781         /* band has flag set and at least one coded coefficient */
00782         if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
00783                 q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
00784                                    q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
00785         }
00786     }
00787 
00788     /* calculate bits left, bits needed and adjust bit allocation */
00789     bits = summer = 0;
00790 
00791     for(i = 0; i < BANDS; i++) {
00792         if (q->bandFlagsBuf[i]) {
00793             for(j = band_tab[i]; j < band_tab[i+1]; j++) {
00794                 if(q->skipFlags[j]) {
00795                     summer += q->CWlengthT[j];
00796                     q->CWlengthT[j] = 0;
00797                 }
00798             }
00799             bits += q->skipFlagBits[i];
00800             summer -= q->skipFlagBits[i];
00801         }
00802     }
00803     imc_adjust_bit_allocation(q, summer);
00804 
00805     for(i = 0; i < BANDS; i++) {
00806         q->sumLenArr[i] = 0;
00807 
00808         for(j = band_tab[i]; j < band_tab[i+1]; j++)
00809             if (!q->skipFlags[j])
00810                 q->sumLenArr[i] += q->CWlengthT[j];
00811     }
00812 
00813     memset(q->codewords, 0, sizeof(q->codewords));
00814 
00815     if(imc_get_coeffs(q) < 0) {
00816         av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
00817         q->decoder_reset = 1;
00818         return AVERROR_INVALIDDATA;
00819     }
00820 
00821     if(inverse_quant_coeff(q, stream_format_code) < 0) {
00822         av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
00823         q->decoder_reset = 1;
00824         return AVERROR_INVALIDDATA;
00825     }
00826 
00827     memset(q->skipFlags, 0, sizeof(q->skipFlags));
00828 
00829     imc_imdct256(q);
00830 
00831     *got_frame_ptr   = 1;
00832     *(AVFrame *)data = q->frame;
00833 
00834     return IMC_BLOCK_SIZE;
00835 }
00836 
00837 
00838 static av_cold int imc_decode_close(AVCodecContext * avctx)
00839 {
00840     IMCContext *q = avctx->priv_data;
00841 
00842     ff_fft_end(&q->fft);
00843 
00844     return 0;
00845 }
00846 
00847 
00848 AVCodec ff_imc_decoder = {
00849     .name = "imc",
00850     .type = AVMEDIA_TYPE_AUDIO,
00851     .id = CODEC_ID_IMC,
00852     .priv_data_size = sizeof(IMCContext),
00853     .init = imc_decode_init,
00854     .close = imc_decode_close,
00855     .decode = imc_decode_frame,
00856     .capabilities = CODEC_CAP_DR1,
00857     .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
00858 };