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