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