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