This will be useful for Indeo Audio decoder which is almost the same but supports stereo.
Unified · Split
| @@ -49,9 +49,7 @@ · 48 unmodified lines | ||
| 49 | 49 | #define BANDS 32 |
| 50 | 50 | #define COEFFS 256 |
| 51 | 51 | |
| 52 | −typedef struct { | |
| 53 | − AVFrame frame; | |
| 54 | − | |
| 52 | +typedef struct IMCChannel { | |
| 55 | 53 | float old_floor[BANDS]; |
| 56 | 54 | float flcoeffs1[BANDS]; |
| 57 | 55 | float flcoeffs2[BANDS]; |
| @@ -61,16 +59,6 @@ typedef struct { · 3 unmodified lines | ||
| 61 | 59 | float flcoeffs6[BANDS]; |
| 62 | 60 | float CWdecoded[COEFFS]; |
| 63 | 61 | |
| 64 | − /** MDCT tables */ | |
| 65 | − //@{ | |
| 66 | − float mdct_sine_window[COEFFS]; | |
| 67 | − float post_cos[COEFFS]; | |
| 68 | − float post_sin[COEFFS]; | |
| 69 | − float pre_coef1[COEFFS]; | |
| 70 | − float pre_coef2[COEFFS]; | |
| 71 | − float last_fft_im[COEFFS]; | |
| 72 | − //@} | |
| 73 | − | |
| 74 | 62 | int bandWidthT[BANDS]; ///< codewords per band |
| 75 | 63 | int bitsBandT[BANDS]; ///< how many bits per codeword in band |
| 76 | 64 | int CWlengthT[COEFFS]; ///< how many bits in each codeword |
| @@ -82,9 +70,28 @@ typedef struct { · 5 unmodified lines | ||
| 82 | 70 | int skipFlagCount[BANDS]; ///< skipped coeffients per band |
| 83 | 71 | int skipFlags[COEFFS]; ///< skip coefficient decoding or not |
| 84 | 72 | int codewords[COEFFS]; ///< raw codewords read from bitstream |
| 73 | + | |
| 74 | + float last_fft_im[COEFFS]; | |
| 75 | + | |
| 76 | + int decoder_reset; | |
| 77 | +} IMCChannel; | |
| 78 | + | |
| 79 | +typedef struct { | |
| 80 | + AVFrame frame; | |
| 81 | + | |
| 82 | + IMCChannel chctx[1]; | |
| 83 | + | |
| 84 | + /** MDCT tables */ | |
| 85 | + //@{ | |
| 86 | + float mdct_sine_window[COEFFS]; | |
| 87 | + float post_cos[COEFFS]; | |
| 88 | + float post_sin[COEFFS]; | |
| 89 | + float pre_coef1[COEFFS]; | |
| 90 | + float pre_coef2[COEFFS]; | |
| 91 | + //@} | |
| 92 | + | |
| 85 | 93 | float sqrt_tab[30]; |
| 86 | 94 | GetBitContext gb; |
| 87 | − int decoder_reset; | |
| 88 | 95 | float one_div_log2; |
| 89 | 96 | |
| 90 | 97 | DSPContext dsp; |
| @@ -115,10 +122,15 @@ static av_cold int imc_decode_init(AVCodecContext *avctx) · 24 unmodified lines | ||
| 115 | 122 | return AVERROR_PATCHWELCOME; |
| 116 | 123 | } |
| 117 | 124 | |
| 118 | − q->decoder_reset = 1; | |
| 125 | + for (j = 0; j < avctx->channels; j++) { | |
| 126 | + q->chctx[j].decoder_reset = 1; | |
| 119 | 127 | |
| 120 | − for (i = 0; i < BANDS; i++) | |
| 121 | − q->old_floor[i] = 1.0; | |
| 128 | + for (i = 0; i < BANDS; i++) | |
| 129 | + q->chctx[j].old_floor[i] = 1.0; | |
| 130 | + | |
| 131 | + for (i = 0; i < COEFFS / 2; i++) | |
| 132 | + q->chctx[j].last_fft_im[i] = 0; | |
| 133 | + } | |
| 122 | 134 | |
| 123 | 135 | /* Build mdct window, a simple sine window normalized with sqrt(2) */ |
| 124 | 136 | ff_sine_window_init(q->mdct_sine_window, COEFFS); |
| @@ -138,8 +150,6 @@ static av_cold int imc_decode_init(AVCodecContext *avctx) · 13 unmodified lines | ||
| 138 | 150 | q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0); |
| 139 | 151 | q->pre_coef2[i] = (r1 - r2) * sqrt(2.0); |
| 140 | 152 | } |
| 141 | − | |
| 142 | − q->last_fft_im[i] = 0; | |
| 143 | 153 | } |
| 144 | 154 | |
| 145 | 155 | /* Generate a square root table */ |
| @@ -164,8 +174,9 @@ static av_cold int imc_decode_init(AVCodecContext *avctx) · 18 unmodified lines | ||
| 164 | 174 | return ret; |
| 165 | 175 | } |
| 166 | 176 | ff_dsputil_init(&q->dsp, avctx); |
| 167 | − avctx->sample_fmt = AV_SAMPLE_FMT_FLT; | |
| 168 | − avctx->channel_layout = AV_CH_LAYOUT_MONO; | |
| 177 | + avctx->sample_fmt = AV_SAMPLE_FMT_FLT; | |
| 178 | + avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO | |
| 179 | + : AV_CH_LAYOUT_STEREO; | |
| 169 | 180 | |
| 170 | 181 | avcodec_get_frame_defaults(&q->frame); |
| 171 | 182 | avctx->coded_frame = &q->frame; |
| @@ -313,8 +324,8 @@ static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf, · 141 unmodified lines | ||
| 313 | 324 | /** |
| 314 | 325 | * Perform bit allocation depending on bits available |
| 315 | 326 | */ |
| 316 | −static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, | |
| 317 | − int flag) | |
| 327 | +static int bit_allocation(IMCContext *q, IMCChannel *chctx, | |
| 328 | + int stream_format_code, int freebits, int flag) | |
| 318 | 329 | { |
| 319 | 330 | int i, j; |
| 320 | 331 | const float limit = -1.e20; |
| @@ -333,43 +344,43 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 12 unmodified lines | ||
| 333 | 344 | int found_indx = 0; |
| 334 | 345 | |
| 335 | 346 | for (i = 0; i < BANDS; i++) |
| 336 | − highest = FFMAX(highest, q->flcoeffs1[i]); | |
| 347 | + highest = FFMAX(highest, chctx->flcoeffs1[i]); | |
| 337 | 348 | |
| 338 | 349 | for (i = 0; i < BANDS - 1; i++) |
| 339 | − q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i]) / log(2); | |
| 340 | − q->flcoeffs4[BANDS - 1] = limit; | |
| 350 | + chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log(chctx->flcoeffs5[i]) / log(2); | |
| 351 | + chctx->flcoeffs4[BANDS - 1] = limit; | |
| 341 | 352 | |
| 342 | 353 | highest = highest * 0.25; |
| 343 | 354 | |
| 344 | 355 | for (i = 0; i < BANDS; i++) { |
| 345 | 356 | indx = -1; |
| 346 | − if ((band_tab[i + 1] - band_tab[i]) == q->bandWidthT[i]) | |
| 357 | + if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i]) | |
| 347 | 358 | indx = 0; |
| 348 | 359 | |
| 349 | − if ((band_tab[i + 1] - band_tab[i]) > q->bandWidthT[i]) | |
| 360 | + if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i]) | |
| 350 | 361 | indx = 1; |
| 351 | 362 | |
| 352 | − if (((band_tab[i + 1] - band_tab[i]) / 2) >= q->bandWidthT[i]) | |
| 363 | + if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i]) | |
| 353 | 364 | indx = 2; |
| 354 | 365 | |
| 355 | 366 | if (indx == -1) |
| 356 | 367 | return AVERROR_INVALIDDATA; |
| 357 | 368 | |
| 358 | − q->flcoeffs4[i] += xTab[(indx * 2 + (q->flcoeffs1[i] < highest)) * 2 + flag]; | |
| 369 | + chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag]; | |
| 359 | 370 | } |
| 360 | 371 | |
| 361 | 372 | if (stream_format_code & 0x2) { |
| 362 | − q->flcoeffs4[0] = limit; | |
| 363 | − q->flcoeffs4[1] = limit; | |
| 364 | − q->flcoeffs4[2] = limit; | |
| 365 | − q->flcoeffs4[3] = limit; | |
| 373 | + chctx->flcoeffs4[0] = limit; | |
| 374 | + chctx->flcoeffs4[1] = limit; | |
| 375 | + chctx->flcoeffs4[2] = limit; | |
| 376 | + chctx->flcoeffs4[3] = limit; | |
| 366 | 377 | } |
| 367 | 378 | |
| 368 | 379 | for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) { |
| 369 | − iacc += q->bandWidthT[i]; | |
| 370 | − summa += q->bandWidthT[i] * q->flcoeffs4[i]; | |
| 380 | + iacc += chctx->bandWidthT[i]; | |
| 381 | + summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i]; | |
| 371 | 382 | } |
| 372 | − q->bandWidthT[BANDS - 1] = 0; | |
| 383 | + chctx->bandWidthT[BANDS - 1] = 0; | |
| 373 | 384 | summa = (summa * 0.5 - freebits) / iacc; |
| 374 | 385 | |
| 375 | 386 | |
| @@ -382,13 +393,13 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 6 unmodified lines | ||
| 382 | 393 | iacc = 0; |
| 383 | 394 | |
| 384 | 395 | for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) { |
| 385 | − cwlen = av_clipf(((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6); | |
| 396 | + cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6); | |
| 386 | 397 | |
| 387 | − q->bitsBandT[j] = cwlen; | |
| 388 | − summer += q->bandWidthT[j] * cwlen; | |
| 398 | + chctx->bitsBandT[j] = cwlen; | |
| 399 | + summer += chctx->bandWidthT[j] * cwlen; | |
| 389 | 400 | |
| 390 | 401 | if (cwlen > 0) |
| 391 | − iacc += q->bandWidthT[j]; | |
| 402 | + iacc += chctx->bandWidthT[j]; | |
| 392 | 403 | } |
| 393 | 404 | |
| 394 | 405 | flg = t2; |
| @@ -405,13 +416,13 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 10 unmodified lines | ||
| 405 | 416 | |
| 406 | 417 | for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) { |
| 407 | 418 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) |
| 408 | − q->CWlengthT[j] = q->bitsBandT[i]; | |
| 419 | + chctx->CWlengthT[j] = chctx->bitsBandT[i]; | |
| 409 | 420 | } |
| 410 | 421 | |
| 411 | 422 | if (freebits > summer) { |
| 412 | 423 | for (i = 0; i < BANDS; i++) { |
| 413 | − workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 | |
| 414 | − : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415); | |
| 424 | + workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20 | |
| 425 | + : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415); | |
| 415 | 426 | } |
| 416 | 427 | |
| 417 | 428 | highest = 0.0; |
| @@ -432,11 +443,11 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 14 unmodified lines | ||
| 432 | 443 | |
| 433 | 444 | if (highest > -1.e20) { |
| 434 | 445 | workT[found_indx] -= 2.0; |
| 435 | − if (++q->bitsBandT[found_indx] == 6) | |
| 446 | + if (++chctx->bitsBandT[found_indx] == 6) | |
| 436 | 447 | workT[found_indx] = -1.e20; |
| 437 | 448 | |
| 438 | 449 | for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) { |
| 439 | − q->CWlengthT[j]++; | |
| 450 | + chctx->CWlengthT[j]++; | |
| 440 | 451 | summer++; |
| 441 | 452 | } |
| 442 | 453 | } |
| @@ -444,7 +455,7 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 1 unmodified line | ||
| 444 | 455 | } |
| 445 | 456 | if (freebits < summer) { |
| 446 | 457 | for (i = 0; i < BANDS; i++) { |
| 447 | − workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) | |
| 458 | + workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585) | |
| 448 | 459 | : 1.e20; |
| 449 | 460 | } |
| 450 | 461 | if (stream_format_code & 0x2) { |
| @@ -466,12 +477,12 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 15 unmodified lines | ||
| 466 | 477 | // break; |
| 467 | 478 | workT[low_indx] = lowest + 2.0; |
| 468 | 479 | |
| 469 | − if (!--q->bitsBandT[low_indx]) | |
| 480 | + if (!--chctx->bitsBandT[low_indx]) | |
| 470 | 481 | workT[low_indx] = 1.e20; |
| 471 | 482 | |
| 472 | 483 | for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) { |
| 473 | − if (q->CWlengthT[j] > 0) { | |
| 474 | − q->CWlengthT[j]--; | |
| 484 | + if (chctx->CWlengthT[j] > 0) { | |
| 485 | + chctx->CWlengthT[j]--; | |
| 475 | 486 | summer--; |
| 476 | 487 | } |
| 477 | 488 | } |
| @@ -480,54 +491,54 @@ static int bit_allocation(IMCContext *q, int stream_format_code, int freebits, · 2 unmodified lines | ||
| 480 | 491 | return 0; |
| 481 | 492 | } |
| 482 | 493 | |
| 483 | −static void imc_get_skip_coeff(IMCContext *q) | |
| 494 | +static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx) | |
| 484 | 495 | { |
| 485 | 496 | int i, j; |
| 486 | 497 | |
| 487 | − memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits)); | |
| 488 | − memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount)); | |
| 498 | + memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits)); | |
| 499 | + memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount)); | |
| 489 | 500 | for (i = 0; i < BANDS; i++) { |
| 490 | − if (!q->bandFlagsBuf[i] || !q->bandWidthT[i]) | |
| 501 | + if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i]) | |
| 491 | 502 | continue; |
| 492 | 503 | |
| 493 | − if (!q->skipFlagRaw[i]) { | |
| 494 | − q->skipFlagBits[i] = band_tab[i + 1] - band_tab[i]; | |
| 504 | + if (!chctx->skipFlagRaw[i]) { | |
| 505 | + chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i]; | |
| 495 | 506 | |
| 496 | 507 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) { |
| 497 | − q->skipFlags[j] = get_bits1(&q->gb); | |
| 498 | − if (q->skipFlags[j]) | |
| 499 | − q->skipFlagCount[i]++; | |
| 508 | + chctx->skipFlags[j] = get_bits1(&q->gb); | |
| 509 | + if (chctx->skipFlags[j]) | |
| 510 | + chctx->skipFlagCount[i]++; | |
| 500 | 511 | } |
| 501 | 512 | } else { |
| 502 | 513 | for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) { |
| 503 | 514 | if (!get_bits1(&q->gb)) { // 0 |
| 504 | − q->skipFlagBits[i]++; | |
| 505 | − q->skipFlags[j] = 1; | |
| 506 | − q->skipFlags[j + 1] = 1; | |
| 507 | − q->skipFlagCount[i] += 2; | |
| 515 | + chctx->skipFlagBits[i]++; | |
| 516 | + chctx->skipFlags[j] = 1; | |
| 517 | + chctx->skipFlags[j + 1] = 1; | |
| 518 | + chctx->skipFlagCount[i] += 2; | |
| 508 | 519 | } else { |
| 509 | 520 | if (get_bits1(&q->gb)) { // 11 |
| 510 | − q->skipFlagBits[i] += 2; | |
| 511 | − q->skipFlags[j] = 0; | |
| 512 | − q->skipFlags[j + 1] = 1; | |
| 513 | − q->skipFlagCount[i]++; | |
| 521 | + chctx->skipFlagBits[i] += 2; | |
| 522 | + chctx->skipFlags[j] = 0; | |
| 523 | + chctx->skipFlags[j + 1] = 1; | |
| 524 | + chctx->skipFlagCount[i]++; | |
| 514 | 525 | } else { |
| 515 | − q->skipFlagBits[i] += 3; | |
| 516 | − q->skipFlags[j + 1] = 0; | |
| 526 | + chctx->skipFlagBits[i] += 3; | |
| 527 | + chctx->skipFlags[j + 1] = 0; | |
| 517 | 528 | if (!get_bits1(&q->gb)) { // 100 |
| 518 | − q->skipFlags[j] = 1; | |
| 519 | − q->skipFlagCount[i]++; | |
| 529 | + chctx->skipFlags[j] = 1; | |
| 530 | + chctx->skipFlagCount[i]++; | |
| 520 | 531 | } else { // 101 |
| 521 | − q->skipFlags[j] = 0; | |
| 532 | + chctx->skipFlags[j] = 0; | |
| 522 | 533 | } |
| 523 | 534 | } |
| 524 | 535 | } |
| 525 | 536 | } |
| 526 | 537 | |
| 527 | 538 | if (j < band_tab[i + 1]) { |
| 528 | − q->skipFlagBits[i]++; | |
| 529 | − if ((q->skipFlags[j] = get_bits1(&q->gb))) | |
| 530 | − q->skipFlagCount[i]++; | |
| 539 | + chctx->skipFlagBits[i]++; | |
| 540 | + if ((chctx->skipFlags[j] = get_bits1(&q->gb))) | |
| 541 | + chctx->skipFlagCount[i]++; | |
| 531 | 542 | } |
| 532 | 543 | } |
| 533 | 544 | } |
| @@ -536,7 +547,8 @@ static void imc_get_skip_coeff(IMCContext *q) · 2 unmodified lines | ||
| 536 | 547 | /** |
| 537 | 548 | * Increase highest' band coefficient sizes as some bits won't be used |
| 538 | 549 | */ |
| 539 | −static void imc_adjust_bit_allocation(IMCContext *q, int summer) | |
| 550 | +static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx, | |
| 551 | + int summer) | |
| 540 | 552 | { |
| 541 | 553 | float workT[32]; |
| 542 | 554 | int corrected = 0; |
| @@ -545,8 +557,8 @@ static void imc_adjust_bit_allocation(IMCContext *q, int summer) · 2 unmodified lines | ||
| 545 | 557 | int found_indx = 0; |
| 546 | 558 | |
| 547 | 559 | for (i = 0; i < BANDS; i++) { |
| 548 | − workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 | |
| 549 | − : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415); | |
| 560 | + workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20 | |
| 561 | + : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415); | |
| 550 | 562 | } |
| 551 | 563 | |
| 552 | 564 | while (corrected < summer) { |
| @@ -564,12 +576,12 @@ static void imc_adjust_bit_allocation(IMCContext *q, int summer) · 11 unmodified lines | ||
| 564 | 576 | |
| 565 | 577 | if (highest > -1.e20) { |
| 566 | 578 | workT[found_indx] -= 2.0; |
| 567 | − if (++(q->bitsBandT[found_indx]) == 6) | |
| 579 | + if (++(chctx->bitsBandT[found_indx]) == 6) | |
| 568 | 580 | workT[found_indx] = -1.e20; |
| 569 | 581 | |
| 570 | 582 | for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) { |
| 571 | − if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) { | |
| 572 | − q->CWlengthT[j]++; | |
| 583 | + if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) { | |
| 584 | + chctx->CWlengthT[j]++; | |
| 573 | 585 | corrected++; |
| 574 | 586 | } |
| 575 | 587 | } |
| @@ -577,17 +589,17 @@ static void imc_adjust_bit_allocation(IMCContext *q, int summer) · 1 unmodified line | ||
| 577 | 589 | } |
| 578 | 590 | } |
| 579 | 591 | |
| 580 | −static void imc_imdct256(IMCContext *q) | |
| 592 | +static void imc_imdct256(IMCContext *q, IMCChannel *chctx) | |
| 581 | 593 | { |
| 582 | 594 | int i; |
| 583 | 595 | float re, im; |
| 584 | 596 | |
| 585 | 597 | /* prerotation */ |
| 586 | 598 | for (i = 0; i < COEFFS / 2; i++) { |
| 587 | − q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS - 1 - i * 2]) - | |
| 588 | − (q->pre_coef2[i] * q->CWdecoded[i * 2]); | |
| 589 | − q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS - 1 - i * 2]) - | |
| 590 | − (q->pre_coef1[i] * q->CWdecoded[i * 2]); | |
| 599 | + q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) - | |
| 600 | + (q->pre_coef2[i] * chctx->CWdecoded[i * 2]); | |
| 601 | + q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) - | |
| 602 | + (q->pre_coef1[i] * chctx->CWdecoded[i * 2]); | |
| 591 | 603 | } |
| 592 | 604 | |
| 593 | 605 | /* FFT */ |
| @@ -598,15 +610,16 @@ static void imc_imdct256(IMCContext *q) · 4 unmodified lines | ||
| 598 | 610 | for (i = 0; i < COEFFS / 2; i++) { |
| 599 | 611 | re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]); |
| 600 | 612 | im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]); |
| 601 | − q->out_samples[i * 2] = (q->mdct_sine_window[COEFFS - 1 - i * 2] * q->last_fft_im[i]) | |
| 613 | + q->out_samples[i * 2] = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i]) | |
| 602 | 614 | + (q->mdct_sine_window[i * 2] * re); |
| 603 | − q->out_samples[COEFFS - 1 - i * 2] = (q->mdct_sine_window[i * 2] * q->last_fft_im[i]) | |
| 615 | + q->out_samples[COEFFS - 1 - i * 2] = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i]) | |
| 604 | 616 | - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re); |
| 605 | − q->last_fft_im[i] = im; | |
| 617 | + chctx->last_fft_im[i] = im; | |
| 606 | 618 | } |
| 607 | 619 | } |
| 608 | 620 | |
| 609 | −static int inverse_quant_coeff(IMCContext *q, int stream_format_code) | |
| 621 | +static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx, | |
| 622 | + int stream_format_code) | |
| 610 | 623 | { |
| 611 | 624 | int i, j; |
| 612 | 625 | int middle_value, cw_len, max_size; |
| @@ -614,30 +627,30 @@ static int inverse_quant_coeff(IMCContext *q, int stream_format_code) · 1 unmodified line | ||
| 614 | 627 | |
| 615 | 628 | for (i = 0; i < BANDS; i++) { |
| 616 | 629 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) { |
| 617 | − q->CWdecoded[j] = 0; | |
| 618 | − cw_len = q->CWlengthT[j]; | |
| 630 | + chctx->CWdecoded[j] = 0; | |
| 631 | + cw_len = chctx->CWlengthT[j]; | |
| 619 | 632 | |
| 620 | − if (cw_len <= 0 || q->skipFlags[j]) | |
| 633 | + if (cw_len <= 0 || chctx->skipFlags[j]) | |
| 621 | 634 | continue; |
| 622 | 635 | |
| 623 | 636 | max_size = 1 << cw_len; |
| 624 | 637 | middle_value = max_size >> 1; |
| 625 | 638 | |
| 626 | − if (q->codewords[j] >= max_size || q->codewords[j] < 0) | |
| 639 | + if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0) | |
| 627 | 640 | return AVERROR_INVALIDDATA; |
| 628 | 641 | |
| 629 | 642 | if (cw_len >= 4) { |
| 630 | 643 | quantizer = imc_quantizer2[(stream_format_code & 2) >> 1]; |
| 631 | − if (q->codewords[j] >= middle_value) | |
| 632 | − q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i]; | |
| 644 | + if (chctx->codewords[j] >= middle_value) | |
| 645 | + chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i]; | |
| 633 | 646 | else |
| 634 | − q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i]; | |
| 647 | + chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i]; | |
| 635 | 648 | }else{ |
| 636 | − quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)]; | |
| 637 | − if (q->codewords[j] >= middle_value) | |
| 638 | − q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i]; | |
| 649 | + quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)]; | |
| 650 | + if (chctx->codewords[j] >= middle_value) | |
| 651 | + chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i]; | |
| 639 | 652 | else |
| 640 | − q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i]; | |
| 653 | + chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i]; | |
| 641 | 654 | } |
| 642 | 655 | } |
| 643 | 656 | } |
| @@ -645,16 +658,16 @@ static int inverse_quant_coeff(IMCContext *q, int stream_format_code) · 1 unmodified line | ||
| 645 | 658 | } |
| 646 | 659 | |
| 647 | 660 | |
| 648 | −static int imc_get_coeffs(IMCContext *q) | |
| 661 | +static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx) | |
| 649 | 662 | { |
| 650 | 663 | int i, j, cw_len, cw; |
| 651 | 664 | |
| 652 | 665 | for (i = 0; i < BANDS; i++) { |
| 653 | − if (!q->sumLenArr[i]) | |
| 666 | + if (!chctx->sumLenArr[i]) | |
| 654 | 667 | continue; |
| 655 | − if (q->bandFlagsBuf[i] || q->bandWidthT[i]) { | |
| 668 | + if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) { | |
| 656 | 669 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) { |
| 657 | − cw_len = q->CWlengthT[j]; | |
| 670 | + cw_len = chctx->CWlengthT[j]; | |
| 658 | 671 | cw = 0; |
| 659 | 672 | |
| 660 | 673 | if (get_bits_count(&q->gb) + cw_len > 512) { |
| @@ -662,47 +675,25 @@ static int imc_get_coeffs(IMCContext *q) · 1 unmodified line | ||
| 662 | 675 | return AVERROR_INVALIDDATA; |
| 663 | 676 | } |
| 664 | 677 | |
| 665 | − if (cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j])) | |
| 678 | + if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) | |
| 666 | 679 | cw = get_bits(&q->gb, cw_len); |
| 667 | 680 | |
| 668 | − q->codewords[j] = cw; | |
| 681 | + chctx->codewords[j] = cw; | |
| 669 | 682 | } |
| 670 | 683 | } |
| 671 | 684 | } |
| 672 | 685 | return 0; |
| 673 | 686 | } |
| 674 | 687 | |
| 675 | −static int imc_decode_frame(AVCodecContext *avctx, void *data, | |
| 676 | − int *got_frame_ptr, AVPacket *avpkt) | |
| 688 | +static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch) | |
| 677 | 689 | { |
| 678 | − const uint8_t *buf = avpkt->data; | |
| 679 | − int buf_size = avpkt->size; | |
| 680 | − | |
| 681 | − IMCContext *q = avctx->priv_data; | |
| 682 | − | |
| 683 | 690 | int stream_format_code; |
| 684 | 691 | int imc_hdr, i, j, ret; |
| 685 | 692 | int flag; |
| 686 | 693 | int bits, summer; |
| 687 | 694 | int counter, bitscount; |
| 688 | − LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]); | |
| 695 | + IMCChannel *chctx = q->chctx + ch; | |
| 689 | 696 | |
| 690 | − if (buf_size < IMC_BLOCK_SIZE) { | |
| 691 | − av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n"); | |
| 692 | − return AVERROR_INVALIDDATA; | |
| 693 | − } | |
| 694 | − | |
| 695 | − /* get output buffer */ | |
| 696 | − q->frame.nb_samples = COEFFS; | |
| 697 | − if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) { | |
| 698 | − av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 699 | − return ret; | |
| 700 | − } | |
| 701 | − q->out_samples = (float*)q->frame.data[0]; | |
| 702 | − | |
| 703 | − q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2); | |
| 704 | − | |
| 705 | − init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8); | |
| 706 | 697 | |
| 707 | 698 | /* Check the frame header */ |
| 708 | 699 | imc_hdr = get_bits(&q->gb, 9); |
| @@ -721,90 +712,90 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, · 12 unmodified lines | ||
| 721 | 712 | // av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code); |
| 722 | 713 | |
| 723 | 714 | if (stream_format_code & 0x04) |
| 724 | − q->decoder_reset = 1; | |
| 715 | + chctx->decoder_reset = 1; | |
| 725 | 716 | |
| 726 | − if (q->decoder_reset) { | |
| 717 | + if (chctx->decoder_reset) { | |
| 727 | 718 | memset(q->out_samples, 0, sizeof(q->out_samples)); |
| 728 | 719 | for (i = 0; i < BANDS; i++) |
| 729 | − q->old_floor[i] = 1.0; | |
| 720 | + chctx->old_floor[i] = 1.0; | |
| 730 | 721 | for (i = 0; i < COEFFS; i++) |
| 731 | − q->CWdecoded[i] = 0; | |
| 732 | − q->decoder_reset = 0; | |
| 722 | + chctx->CWdecoded[i] = 0; | |
| 723 | + chctx->decoder_reset = 0; | |
| 733 | 724 | } |
| 734 | 725 | |
| 735 | 726 | flag = get_bits1(&q->gb); |
| 736 | − imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf); | |
| 727 | + imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf); | |
| 737 | 728 | |
| 738 | 729 | if (stream_format_code & 0x4) |
| 739 | − imc_decode_level_coefficients(q, q->levlCoeffBuf, | |
| 740 | − q->flcoeffs1, q->flcoeffs2); | |
| 730 | + imc_decode_level_coefficients(q, chctx->levlCoeffBuf, | |
| 731 | + chctx->flcoeffs1, chctx->flcoeffs2); | |
| 741 | 732 | else |
| 742 | − imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, | |
| 743 | − q->flcoeffs1, q->flcoeffs2); | |
| 733 | + imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor, | |
| 734 | + chctx->flcoeffs1, chctx->flcoeffs2); | |
| 744 | 735 | |
| 745 | − memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float)); | |
| 736 | + memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float)); | |
| 746 | 737 | |
| 747 | 738 | counter = 0; |
| 748 | 739 | for (i = 0; i < BANDS; i++) { |
| 749 | − if (q->levlCoeffBuf[i] == 16) { | |
| 750 | − q->bandWidthT[i] = 0; | |
| 740 | + if (chctx->levlCoeffBuf[i] == 16) { | |
| 741 | + chctx->bandWidthT[i] = 0; | |
| 751 | 742 | counter++; |
| 752 | 743 | } else |
| 753 | − q->bandWidthT[i] = band_tab[i + 1] - band_tab[i]; | |
| 744 | + chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i]; | |
| 754 | 745 | } |
| 755 | − memset(q->bandFlagsBuf, 0, BANDS * sizeof(int)); | |
| 746 | + memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int)); | |
| 756 | 747 | for (i = 0; i < BANDS - 1; i++) { |
| 757 | − if (q->bandWidthT[i]) | |
| 758 | − q->bandFlagsBuf[i] = get_bits1(&q->gb); | |
| 748 | + if (chctx->bandWidthT[i]) | |
| 749 | + chctx->bandFlagsBuf[i] = get_bits1(&q->gb); | |
| 759 | 750 | } |
| 760 | 751 | |
| 761 | − imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5); | |
| 752 | + imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2, chctx->bandWidthT, chctx->flcoeffs3, chctx->flcoeffs5); | |
| 762 | 753 | |
| 763 | 754 | bitscount = 0; |
| 764 | 755 | /* first 4 bands will be assigned 5 bits per coefficient */ |
| 765 | 756 | if (stream_format_code & 0x2) { |
| 766 | 757 | bitscount += 15; |
| 767 | 758 | |
| 768 | − q->bitsBandT[0] = 5; | |
| 769 | − q->CWlengthT[0] = 5; | |
| 770 | − q->CWlengthT[1] = 5; | |
| 771 | − q->CWlengthT[2] = 5; | |
| 759 | + chctx->bitsBandT[0] = 5; | |
| 760 | + chctx->CWlengthT[0] = 5; | |
| 761 | + chctx->CWlengthT[1] = 5; | |
| 762 | + chctx->CWlengthT[2] = 5; | |
| 772 | 763 | for (i = 1; i < 4; i++) { |
| 773 | − bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5; | |
| 774 | − q->bitsBandT[i] = bits; | |
| 764 | + bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5; | |
| 765 | + chctx->bitsBandT[i] = bits; | |
| 775 | 766 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) { |
| 776 | − q->CWlengthT[j] = bits; | |
| 767 | + chctx->CWlengthT[j] = bits; | |
| 777 | 768 | bitscount += bits; |
| 778 | 769 | } |
| 779 | 770 | } |
| 780 | 771 | } |
| 781 | 772 | |
| 782 | − if ((ret = bit_allocation(q, stream_format_code, | |
| 773 | + if ((ret = bit_allocation(q, chctx, stream_format_code, | |
| 783 | 774 | 512 - bitscount - get_bits_count(&q->gb), |
| 784 | 775 | flag)) < 0) { |
| 785 | 776 | av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n"); |
| 786 | − q->decoder_reset = 1; | |
| 777 | + chctx->decoder_reset = 1; | |
| 787 | 778 | return ret; |
| 788 | 779 | } |
| 789 | 780 | |
| 790 | 781 | for (i = 0; i < BANDS; i++) { |
| 791 | − q->sumLenArr[i] = 0; | |
| 792 | − q->skipFlagRaw[i] = 0; | |
| 782 | + chctx->sumLenArr[i] = 0; | |
| 783 | + chctx->skipFlagRaw[i] = 0; | |
| 793 | 784 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) |
| 794 | − q->sumLenArr[i] += q->CWlengthT[j]; | |
| 795 | − if (q->bandFlagsBuf[i]) | |
| 796 | − if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0)) | |
| 797 | − q->skipFlagRaw[i] = 1; | |
| 785 | + chctx->sumLenArr[i] += chctx->CWlengthT[j]; | |
| 786 | + if (chctx->bandFlagsBuf[i]) | |
| 787 | + if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0)) | |
| 788 | + chctx->skipFlagRaw[i] = 1; | |
| 798 | 789 | } |
| 799 | 790 | |
| 800 | − imc_get_skip_coeff(q); | |
| 791 | + imc_get_skip_coeff(q, chctx); | |
| 801 | 792 | |
| 802 | 793 | for (i = 0; i < BANDS; i++) { |
| 803 | − q->flcoeffs6[i] = q->flcoeffs1[i]; | |
| 794 | + chctx->flcoeffs6[i] = chctx->flcoeffs1[i]; | |
| 804 | 795 | /* band has flag set and at least one coded coefficient */ |
| 805 | − if (q->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != q->skipFlagCount[i]) { | |
| 806 | − q->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] / | |
| 807 | − q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - q->skipFlagCount[i])]; | |
| 796 | + if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) { | |
| 797 | + chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] / | |
| 798 | + q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])]; | |
| 808 | 799 | } |
| 809 | 800 | } |
| 810 | 801 | |
| @@ -812,49 +803,88 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, · 1 unmodified line | ||
| 812 | 803 | bits = summer = 0; |
| 813 | 804 | |
| 814 | 805 | for (i = 0; i < BANDS; i++) { |
| 815 | − if (q->bandFlagsBuf[i]) { | |
| 806 | + if (chctx->bandFlagsBuf[i]) { | |
| 816 | 807 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) { |
| 817 | − if (q->skipFlags[j]) { | |
| 818 | − summer += q->CWlengthT[j]; | |
| 819 | − q->CWlengthT[j] = 0; | |
| 808 | + if (chctx->skipFlags[j]) { | |
| 809 | + summer += chctx->CWlengthT[j]; | |
| 810 | + chctx->CWlengthT[j] = 0; | |
| 820 | 811 | } |
| 821 | 812 | } |
| 822 | − bits += q->skipFlagBits[i]; | |
| 823 | − summer -= q->skipFlagBits[i]; | |
| 813 | + bits += chctx->skipFlagBits[i]; | |
| 814 | + summer -= chctx->skipFlagBits[i]; | |
| 824 | 815 | } |
| 825 | 816 | } |
| 826 | − imc_adjust_bit_allocation(q, summer); | |
| 817 | + imc_adjust_bit_allocation(q, chctx, summer); | |
| 827 | 818 | |
| 828 | 819 | for (i = 0; i < BANDS; i++) { |
| 829 | − q->sumLenArr[i] = 0; | |
| 820 | + chctx->sumLenArr[i] = 0; | |
| 830 | 821 | |
| 831 | 822 | for (j = band_tab[i]; j < band_tab[i + 1]; j++) |
| 832 | − if (!q->skipFlags[j]) | |
| 833 | − q->sumLenArr[i] += q->CWlengthT[j]; | |
| 823 | + if (!chctx->skipFlags[j]) | |
| 824 | + chctx->sumLenArr[i] += chctx->CWlengthT[j]; | |
| 834 | 825 | } |
| 835 | 826 | |
| 836 | − memset(q->codewords, 0, sizeof(q->codewords)); | |
| 827 | + memset(chctx->codewords, 0, sizeof(chctx->codewords)); | |
| 837 | 828 | |
| 838 | − if (imc_get_coeffs(q) < 0) { | |
| 829 | + if (imc_get_coeffs(q, chctx) < 0) { | |
| 839 | 830 | av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n"); |
| 840 | − q->decoder_reset = 1; | |
| 831 | + chctx->decoder_reset = 1; | |
| 841 | 832 | return AVERROR_INVALIDDATA; |
| 842 | 833 | } |
| 843 | 834 | |
| 844 | − if (inverse_quant_coeff(q, stream_format_code) < 0) { | |
| 835 | + if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) { | |
| 845 | 836 | av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n"); |
| 846 | − q->decoder_reset = 1; | |
| 837 | + chctx->decoder_reset = 1; | |
| 838 | + return AVERROR_INVALIDDATA; | |
| 839 | + } | |
| 840 | + | |
| 841 | + memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags)); | |
| 842 | + | |
| 843 | + imc_imdct256(q, chctx); | |
| 844 | + | |
| 845 | + return 0; | |
| 846 | +} | |
| 847 | + | |
| 848 | +static int imc_decode_frame(AVCodecContext *avctx, void *data, | |
| 849 | + int *got_frame_ptr, AVPacket *avpkt) | |
| 850 | +{ | |
| 851 | + const uint8_t *buf = avpkt->data; | |
| 852 | + int buf_size = avpkt->size; | |
| 853 | + int ret, i; | |
| 854 | + | |
| 855 | + IMCContext *q = avctx->priv_data; | |
| 856 | + | |
| 857 | + LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]); | |
| 858 | + | |
| 859 | + if (buf_size < IMC_BLOCK_SIZE) { | |
| 860 | + av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n"); | |
| 847 | 861 | return AVERROR_INVALIDDATA; |
| 848 | 862 | } |
| 849 | 863 | |
| 850 | − memset(q->skipFlags, 0, sizeof(q->skipFlags)); | |
| 864 | + /* get output buffer */ | |
| 865 | + q->frame.nb_samples = COEFFS; | |
| 866 | + if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) { | |
| 867 | + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 868 | + return ret; | |
| 869 | + } | |
| 851 | 870 | |
| 852 | − imc_imdct256(q); | |
| 871 | + for (i = 0; i < avctx->channels; i++) { | |
| 872 | + q->out_samples = (float*)q->frame.data[0] + i; | |
| 873 | + | |
| 874 | + q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2); | |
| 875 | + | |
| 876 | + init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8); | |
| 877 | + | |
| 878 | + buf += IMC_BLOCK_SIZE; | |
| 879 | + | |
| 880 | + if ((ret = imc_decode_block(avctx, q, i)) < 0) | |
| 881 | + return ret; | |
| 882 | + } | |
| 853 | 883 | |
| 854 | 884 | *got_frame_ptr = 1; |
| 855 | 885 | *(AVFrame *)data = q->frame; |
| 856 | 886 | |
| 857 | − return IMC_BLOCK_SIZE; | |
| 887 | + return IMC_BLOCK_SIZE * avctx->channels; | |
| 858 | 888 | } |
| 859 | 889 | |
| 860 | 890 | |