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