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