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