IMC decoder
[libav.git] / libavcodec / imc.c
CommitLineData
84ed36da
KS
1/*
2 * IMC compatible decoder
3 * Copyright (c) 2002-2004 Maxim Poliakovski
4 * Copyright (c) 2006 Benjamin Larsson
5 * Copyright (c) 2006 Konstantin Shishkov
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25/**
26 * @file imc.c IMC - Intel Music Coder
27 * A mdct based codec using a 256 points large transform
28 * divied into 32 bands with some mix of scale factors.
29 * Only mono is supported.
30 *
31 */
32
33
34#include <math.h>
35#include <stddef.h>
36#include <stdio.h>
37
38#define ALT_BITSTREAM_READER
39#include "avcodec.h"
40#include "bitstream.h"
41#include "dsputil.h"
42
43#include "imcdata.h"
44
45#define IMC_FRAME_ID 0x21
46#define BANDS 32
47#define COEFFS 256
48
49typedef struct {
50 float old_floor[BANDS];
51 float flcoeffs1[BANDS];
52 float flcoeffs2[BANDS];
53 float flcoeffs3[BANDS];
54 float flcoeffs4[BANDS];
55 float flcoeffs5[BANDS];
56 float flcoeffs6[BANDS];
57 float CWdecoded[COEFFS];
58
59 /** MDCT tables */
60 //@{
61 float mdct_sine_window[COEFFS];
62 float post_cos[COEFFS];
63 float post_sin[COEFFS];
64 float pre_coef1[COEFFS];
65 float pre_coef2[COEFFS];
66 float last_fft_im[COEFFS];
67 //@}
68
69 int bandWidthT[BANDS]; ///< codewords per band
70 int bitsBandT[BANDS]; ///< how many bits per codeword in band
71 int CWlengthT[COEFFS]; ///< how many bits in each codeword
72 int levlCoeffBuf[BANDS];
73 int bandFlagsBuf[BANDS]; ///< flags for each band
74 int sumLenArr[BANDS]; ///< bits for all coeffs in band
75 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
76 int skipFlagBits[BANDS]; ///< bits used to code skip flags
77 int skipFlagCount[BANDS]; ///< skipped coeffients per band
78 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
79 int codewords[COEFFS]; ///< raw codewords read from bitstream
80 float sqrt_tab[30];
81 GetBitContext gb;
82 VLC huffman_vlc[4][4];
83 float flcf1, flcf2;
84 int decoder_reset;
85 float one_div_log2;
86
87 DSPContext dsp;
88 FFTContext fft;
89 DECLARE_ALIGNED_16(FFTComplex, samples[COEFFS/2]);
90 DECLARE_ALIGNED_16(float, out_samples[COEFFS]);
91} IMCContext;
92
93
94static int imc_decode_init(AVCodecContext * avctx)
95{
96 int i, j;
97 IMCContext *q = avctx->priv_data;
98 double r1, r2;
99
100 q->decoder_reset = 1;
101
102 for(i = 0; i < BANDS; i++)
103 q->old_floor[i] = 1.0;
104
105 /* Build mdct window, a simple sine window normalized with sqrt(2) */
106 for(i = 0; i < COEFFS; i++)
107 q->mdct_sine_window[i] = sin((i + 0.5) / 512.0 * M_PI) * sqrt(2.0);
108 for(i = 0; i < COEFFS/2; i++){
109 q->post_cos[i] = cos(i / 256.0 * M_PI);
110 q->post_sin[i] = sin(i / 256.0 * M_PI);
111
112 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
113 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
114
115 if (i & 0x1)
116 {
117 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
118 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
119 }
120 else
121 {
122 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
123 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
124 }
125
126 q->last_fft_im[i] = 0;
127 }
128 q->flcf1 = log2(10) * 0.05703125;
129 q->flcf2 = log2(10) * 0.25;
130
131 /* Generate a square root table */
132
133 for(i = 0; i < 30; i++) {
134 q->sqrt_tab[i] = sqrt(i);
135 }
136
137 /* initialize the VLC tables */
138 for(i = 0; i < 4 ; i++) {
139 for(j = 0; j < 4; j++) {
140 init_vlc (&q->huffman_vlc[i][j], 9, imc_huffman_sizes[i],
141 imc_huffman_lens[i][j], 1, 1,
142 imc_huffman_bits[i][j], 2, 2, 0);
143 }
144 }
145 q->one_div_log2 = 1/log(2);
146
147 ff_fft_init(&q->fft, 7, 1);
148 dsputil_init(&q->dsp, avctx);
149 return 0;
150}
151
152static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT,
153 float* flcoeffs3, float* flcoeffs5)
154{
155 float workT1[BANDS];
156 float workT2[BANDS];
157 float workT3[BANDS];
158 float snr_limit = 1.e-30;
159 float accum = 0.0;
160 int i, cnt2;
161
162 for(i = 0; i < BANDS; i++) {
163 flcoeffs5[i] = workT2[i] = 0.0;
164 if (bandWidthT[i]){
165 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
166 flcoeffs3[i] = 2.0 * flcoeffs2[i];
167 } else {
168 workT1[i] = 0.0;
169 flcoeffs3[i] = -30000.0;
170 }
171 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
172 if (workT3[i] <= snr_limit)
173 workT3[i] = 0.0;
174 }
175
176 for(i = 0; i < BANDS; i++) {
177 for(cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
178 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
179 workT2[cnt2-1] = workT2[cnt2-1] + workT3[i];
180 }
181
182 for(i = 1; i < BANDS; i++) {
183 accum = (workT2[i-1] + accum) * imc_weights1[i-1];
184 flcoeffs5[i] += accum;
185 }
186
187 for(i = 0; i < BANDS; i++)
188 workT2[i] = 0.0;
189
190 for(i = 0; i < BANDS; i++) {
191 for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--)
192 flcoeffs5[cnt2] += workT3[i];
193 workT2[cnt2+1] += workT3[i];
194 }
195
196 accum = 0.0;
197
198 for(i = BANDS-2; i >= 0; i--) {
199 accum = (workT2[i+1] + accum) * imc_weights2[i];
200 flcoeffs5[i] += accum;
201 //there is missing code here, but it seems to never be triggered
202 }
203}
204
205
206static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs)
207{
208 int i;
209 VLC *hufftab[4];
210 int start = 0;
211 const uint8_t *cb_sel;
212 int s;
213
214 s = stream_format_code >> 1;
215 hufftab[0] = &q->huffman_vlc[s][0];
216 hufftab[1] = &q->huffman_vlc[s][1];
217 hufftab[2] = &q->huffman_vlc[s][2];
218 hufftab[3] = &q->huffman_vlc[s][3];
219 cb_sel = imc_cb_select[s];
220
221 if(stream_format_code & 4)
222 start = 1;
223 if(start)
224 levlCoeffs[0] = get_bits(&q->gb, 7);
225 for(i = start; i < BANDS; i++){
226 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2);
227 if(levlCoeffs[i] == 17)
228 levlCoeffs[i] += get_bits(&q->gb, 4);
229 }
230}
231
232static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1,
233 float* flcoeffs2)
234{
235 int i, level;
236 float tmp, tmp2;
237 //maybe some frequency division thingy
238
239 flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * q->flcf1);
240 flcoeffs2[0] = log2(flcoeffs1[0]);
241 tmp = flcoeffs1[0];
242 tmp2 = flcoeffs2[0];
243
244 for(i = 1; i < BANDS; i++) {
245 level = levlCoeffBuf[i];
246 if (level == 16) {
247 flcoeffs1[i] = 1.0;
248 flcoeffs2[i] = 0.0;
249 } else {
250 if (level < 17)
251 level -=7;
252 else if (level <= 24)
253 level -=32;
254 else
255 level -=16;
256
257 tmp *= imc_exp_tab[15 + level];
258 tmp2 += q->flcf2 * level;
259 flcoeffs1[i] = tmp;
260 flcoeffs2[i] = tmp2;
261 }
262 }
263}
264
265
266static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1,
267 float* flcoeffs2) {
268 int i;
269 //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
270 // and flcoeffs2 old scale factors
271 // might be incomplete due to a missing table that is in the binary code
272 for(i = 0; i < BANDS; i++) {
273 flcoeffs1[i] = 0;
274 if(levlCoeffBuf[i] < 16) {
275 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
276 flcoeffs2[i] = (levlCoeffBuf[i]-7) * q->flcf2 + flcoeffs2[i];
277 } else {
278 flcoeffs1[i] = old_floor[i];
279 }
280 }
281}
282
283/**
284 * Perform bit allocation depending on bits available
285 */
286static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) {
287 int i, j;
288 const float limit = -1.e20;
289 float highest = 0.0;
290 int indx;
291 int t1 = 0;
292 int t2 = 1;
293 float summa = 0.0;
294 int iacc = 0;
295 int summer = 0;
296 int rres, cwlen;
297 float lowest = 1.e10;
298 int low_indx = 0;
299 float workT[32];
300 int flg;
301 int found_indx = 0;
302
303 for(i = 0; i < BANDS; i++)
304 highest = FFMAX(highest, q->flcoeffs1[i]);
305
306 for(i = 0; i < BANDS-1; i++) {
307 q->flcoeffs4[i] = q->flcoeffs3[i] - log2(q->flcoeffs5[i]);
308 }
309 q->flcoeffs4[BANDS - 1] = limit;
310
311 highest = highest * 0.25;
312
313 for(i = 0; i < BANDS; i++) {
314 indx = -1;
315 if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i])
316 indx = 0;
317
318 if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i])
319 indx = 1;
320
321 if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i])
322 indx = 2;
323
324 if (indx == -1)
325 return -1;
326
327 q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
328 }
329
330 if (stream_format_code & 0x2) {
331 q->flcoeffs4[0] = limit;
332 q->flcoeffs4[1] = limit;
333 q->flcoeffs4[2] = limit;
334 q->flcoeffs4[3] = limit;
335 }
336
337 for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) {
338 iacc += q->bandWidthT[i];
339 summa += q->bandWidthT[i] * q->flcoeffs4[i];
340 }
341 q->bandWidthT[BANDS-1] = 0;
342 summa = (summa * 0.5 - freebits) / iacc;
343
344
345 for(i = 0; i < BANDS/2; i++) {
346 rres = summer - freebits;
347 if((rres >= -8) && (rres <= 8)) break;
348
349 summer = 0;
350 iacc = 0;
351
352 for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
353 cwlen = clip((int)((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
354
355 q->bitsBandT[j] = cwlen;
356 summer += q->bandWidthT[j] * cwlen;
357
358 if (cwlen > 0)
359 iacc += q->bandWidthT[j];
360 }
361
362 flg = t2;
363 t2 = 1;
364 if (freebits < summer)
365 t2 = -1;
366 if (i == 0)
367 flg = t2;
368 if(flg != t2)
369 t1++;
370
371 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
372 }
373
374 for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) {
375 for(j = band_tab[i]; j < band_tab[i+1]; j++)
376 q->CWlengthT[j] = q->bitsBandT[i];
377 }
378
379 if (freebits > summer) {
380 for(i = 0; i < BANDS; i++) {
381 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
382 }
383
384 highest = 0.0;
385
386 do{
387 if (highest <= -1.e20)
388 break;
389
390 found_indx = 0;
391 highest = -1.e20;
392
393 for(i = 0; i < BANDS; i++) {
394 if (workT[i] > highest) {
395 highest = workT[i];
396 found_indx = i;
397 }
398 }
399
400 if (highest > -1.e20) {
401 workT[found_indx] -= 2.0;
402 if (++(q->bitsBandT[found_indx]) == 6)
403 workT[found_indx] = -1.e20;
404
405 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
406 q->CWlengthT[j]++;
407 summer++;
408 }
409 }
410 }while (freebits > summer);
411 }
412 if (freebits < summer) {
413 for(i = 0; i < BANDS; i++) {
414 workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
415 }
416 if (stream_format_code & 0x2) {
417 workT[0] = 1.e20;
418 workT[1] = 1.e20;
419 workT[2] = 1.e20;
420 workT[3] = 1.e20;
421 }
422 while (freebits < summer){
423 lowest = 1.e10;
424 low_indx = 0;
425 for(i = 0; i < BANDS; i++) {
426 if (workT[i] < lowest) {
427 lowest = workT[i];
428 low_indx = i;
429 }
430 }
431 //if(lowest >= 1.e10) break;
432 workT[low_indx] = lowest + 2.0;
433
434 if (!(--q->bitsBandT[low_indx]))
435 workT[low_indx] = 1.e20;
436
437 for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
438 if(q->CWlengthT[j] > 0){
439 q->CWlengthT[j]--;
440 summer--;
441 }
442 }
443 }
444 }
445 return 0;
446}
447
448static void imc_get_skip_coeff(IMCContext* q) {
449 int i, j;
450
451 memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
452 memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
453 for(i = 0; i < BANDS; i++) {
454 if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
455 continue;
456
457 if (!q->skipFlagRaw[i]) {
458 q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
459
460 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
461 if ((q->skipFlags[j] = get_bits(&q->gb,1)))
462 q->skipFlagCount[i]++;
463 }
464 } else {
465 for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
466 if(!get_bits1(&q->gb)){//0
467 q->skipFlagBits[i]++;
468 q->skipFlags[j]=1;
469 q->skipFlags[j+1]=1;
470 q->skipFlagCount[i] += 2;
471 }else{
472 if(get_bits1(&q->gb)){//11
473 q->skipFlagBits[i] +=2;
474 q->skipFlags[j]=0;
475 q->skipFlags[j+1]=1;
476 q->skipFlagCount[i]++;
477 }else{
478 q->skipFlagBits[i] +=3;
479 q->skipFlags[j+1]=0;
480 if(!get_bits1(&q->gb)){//100
481 q->skipFlags[j]=1;
482 q->skipFlagCount[i]++;
483 }else{//101
484 q->skipFlags[j]=0;
485 }
486 }
487 }
488 }
489
490 if (j < band_tab[i+1]) {
491 q->skipFlagBits[i]++;
492 if ((q->skipFlags[j] = get_bits(&q->gb,1)))
493 q->skipFlagCount[i]++;
494 }
495 }
496 }
497}
498
499/**
500 * Increase highest' band coefficient sizes as some bits won't be used
501 */
502static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
503 float workT[32];
504 int corrected = 0;
505 int i, j;
506 float highest = 0;
507 int found_indx=0;
508
509 for(i = 0; i < BANDS; i++) {
510 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
511 }
512
513 while (corrected < summer) {
514 if(highest <= -1.e20)
515 break;
516
517 highest = -1.e20;
518
519 for(i = 0; i < BANDS; i++) {
520 if (workT[i] > highest) {
521 highest = workT[i];
522 found_indx = i;
523 }
524 }
525
526 if (highest > -1.e20) {
527 workT[found_indx] -= 2.0;
528 if (++(q->bitsBandT[found_indx]) == 6)
529 workT[found_indx] = -1.e20;
530
531 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
532 if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
533 q->CWlengthT[j]++;
534 corrected++;
535 }
536 }
537 }
538 }
539}
540
541void imc_imdct256(IMCContext *q) {
542 int i;
543 float re, im;
544
545 /* prerotation */
546 for(i=0; i < COEFFS/2; i++){
547 q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
548 (q->pre_coef2[i] * q->CWdecoded[i*2]);
549 q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
550 (q->pre_coef1[i] * q->CWdecoded[i*2]);
551 }
552
553 /* FFT */
554 ff_fft_permute(&q->fft, q->samples);
555 ff_fft_calc (&q->fft, q->samples);
556
557 /* postrotation, window and reorder */
558 for(i = 0; i < COEFFS/2; i++){
559 re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
560 im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
561 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);
562 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);
563 q->last_fft_im[i] = im;
564 }
565}
566
567static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
568 int i, j;
569 int middle_value, cw_len, max_size;
570 const float* quantizer;
571
572 for(i = 0; i < BANDS; i++) {
573 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
574 q->CWdecoded[j] = 0;
575 cw_len = q->CWlengthT[j];
576
577 if (cw_len <= 0 || q->skipFlags[j])
578 continue;
579
580 max_size = 1 << cw_len;
581 middle_value = max_size >> 1;
582
583 if (q->codewords[j] >= max_size || q->codewords[j] < 0)
584 return -1;
585
586 if (cw_len >= 4){
587 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
588 if (q->codewords[j] >= middle_value)
589 q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
590 else
591 q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
592 }else{
593 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
594 if (q->codewords[j] >= middle_value)
595 q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
596 else
597 q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
598 }
599 }
600 }
601 return 0;
602}
603
604
605static int imc_get_coeffs (IMCContext* q) {
606 int i, j, cw_len, cw;
607
608 for(i = 0; i < BANDS; i++) {
609 if(!q->sumLenArr[i]) continue;
610 if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
611 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
612 cw_len = q->CWlengthT[j];
613 cw = 0;
614
615 if (get_bits_count(&q->gb) + cw_len > 512){
616//av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
617 return -1;
618 }
619
620 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
621 cw = get_bits(&q->gb, cw_len);
622
623 q->codewords[j] = cw;
624 }
625 }
626 }
627 return 0;
628}
629
630static int imc_decode_frame(AVCodecContext * avctx,
631 void *data, int *data_size,
632 uint8_t * buf, int buf_size)
633{
634
635 IMCContext *q = avctx->priv_data;
636
637 int stream_format_code;
638 int imc_hdr, i, j;
639 int flag;
640 int bits, summer;
641 int counter, bitscount;
642 uint16_t *buf16 = (uint16_t *) buf;
643
644 /* FIXME: input should not be modified */
645 for(i = 0; i < FFMIN(buf_size, avctx->block_align) / 2; i++)
646 buf16[i] = bswap_16(buf16[i]);
647
648 init_get_bits(&q->gb, buf, 512);
649
650 /* Check the frame header */
651 imc_hdr = get_bits(&q->gb, 9);
652 if (imc_hdr != IMC_FRAME_ID) {
653 av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
654 av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
655 return -1;
656 }
657 stream_format_code = get_bits(&q->gb, 3);
658
659 if(stream_format_code & 1){
660 av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
661 return -1;
662 }
663
664// av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
665
666 if (stream_format_code & 0x04)
667 q->decoder_reset = 1;
668
669 if(q->decoder_reset) {
670 memset(q->out_samples, 0, sizeof(q->out_samples));
671 for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
672 for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
673 q->decoder_reset = 0;
674 }
675
676 flag = get_bits1(&q->gb);
677 imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
678
679 if (stream_format_code & 0x4)
680 imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2);
681 else
682 imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2);
683
684 memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
685
686 counter = 0;
687 for (i=0 ; i<BANDS ; i++) {
688 if (q->levlCoeffBuf[i] == 16) {
689 q->bandWidthT[i] = 0;
690 counter++;
691 } else
692 q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
693 }
694 memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
695 for(i = 0; i < BANDS-1; i++) {
696 if (q->bandWidthT[i])
697 q->bandFlagsBuf[i] = get_bits1(&q->gb);
698 }
699
700 imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5);
701
702 bitscount = 0;
703 /* first 4 bands will be assigned 5 bits per coefficient */
704 if (stream_format_code & 0x2) {
705 bitscount += 15;
706
707 q->bitsBandT[0] = 5;
708 q->CWlengthT[0] = 5;
709 q->CWlengthT[1] = 5;
710 q->CWlengthT[2] = 5;
711 for(i = 1; i < 4; i++){
712 bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
713 q->bitsBandT[i] = bits;
714 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
715 q->CWlengthT[j] = bits;
716 bitscount += bits;
717 }
718 }
719 }
720
721 if(bit_allocation (q, stream_format_code, 512 - bitscount - get_bits_count(&q->gb), flag) < 0) {
722 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
723 q->decoder_reset = 1;
724 return -1;
725 }
726
727 for(i = 0; i < BANDS; i++) {
728 q->sumLenArr[i] = 0;
729 q->skipFlagRaw[i] = 0;
730 for(j = band_tab[i]; j < band_tab[i+1]; j++)
731 q->sumLenArr[i] += q->CWlengthT[j];
732 if (q->bandFlagsBuf[i])
733 if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
734 q->skipFlagRaw[i] = 1;
735 }
736
737 imc_get_skip_coeff(q);
738
739 for(i = 0; i < BANDS; i++) {
740 q->flcoeffs6[i] = q->flcoeffs1[i];
741 /* band has flag set and at least one coded coefficient */
742 if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
743 q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
744 q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
745 }
746 }
747
748 /* calculate bits left, bits needed and adjust bit allocation */
749 bits = summer = 0;
750
751 for(i = 0; i < BANDS; i++) {
752 if (q->bandFlagsBuf[i]) {
753 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
754 if(q->skipFlags[j]) {
755 summer += q->CWlengthT[j];
756 q->CWlengthT[j] = 0;
757 }
758 }
759 bits += q->skipFlagBits[i];
760 summer -= q->skipFlagBits[i];
761 }
762 }
763 imc_adjust_bit_allocation(q, summer);
764
765 for(i = 0; i < BANDS; i++) {
766 q->sumLenArr[i] = 0;
767
768 for(j = band_tab[i]; j < band_tab[i+1]; j++)
769 if (!q->skipFlags[j])
770 q->sumLenArr[i] += q->CWlengthT[j];
771 }
772
773 memset(q->codewords, 0, sizeof(q->codewords));
774
775 if(imc_get_coeffs(q) < 0) {
776 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
777 q->decoder_reset = 1;
778 return 0;
779 }
780
781 if(inverse_quant_coeff(q, stream_format_code) < 0) {
782 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
783 q->decoder_reset = 1;
784 return 0;
785 }
786
787 memset(q->skipFlags, 0, sizeof(q->skipFlags));
788
789 imc_imdct256(q);
790
791 q->dsp.float_to_int16(data, q->out_samples, COEFFS);
792
793 *data_size = COEFFS * sizeof(int16_t);
794
795 return avctx->block_align;
796}
797
798
799static int imc_decode_close(AVCodecContext * avctx)
800{
801 IMCContext *q = avctx->priv_data;
802
803 ff_fft_end(&q->fft);
804 return 0;
805}
806
807
808AVCodec imc_decoder = {
809 .name = "imc",
810 .type = CODEC_TYPE_AUDIO,
811 .id = CODEC_ID_IMC,
812 .priv_data_size = sizeof(IMCContext),
813 .init = imc_decode_init,
814 .close = imc_decode_close,
815 .decode = imc_decode_frame,
816};