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