comparison alsdec.c @ 11148:2d2780630361 libavcodec

Support arithmetic decoding in ALS.
author thilo.borgmann
date Sat, 13 Feb 2010 18:24:13 +0000
parents 327019a2d156
children 625a076b8038
comparison
equal deleted inserted replaced
11147:9673c7b54598 11148:2d2780630361
32 #include "avcodec.h" 32 #include "avcodec.h"
33 #include "get_bits.h" 33 #include "get_bits.h"
34 #include "unary.h" 34 #include "unary.h"
35 #include "mpeg4audio.h" 35 #include "mpeg4audio.h"
36 #include "bytestream.h" 36 #include "bytestream.h"
37 #include "bgmc.h"
37 38
38 #include <stdint.h> 39 #include <stdint.h>
39 40
40 /** Rice parameters and corresponding index offsets for decoding the 41 /** Rice parameters and corresponding index offsets for decoding the
41 * indices of scaled PARCOR values. The table choosen is set globally 42 * indices of scaled PARCOR values. The table choosen is set globally
118 0, -12, -25, -38, -51, -64, -76, -89, 119 0, -12, -25, -38, -51, -64, -76, -89,
119 -102, -115, -128, -140, -153, -166, -179, -192 120 -102, -115, -128, -140, -153, -166, -179, -192
120 }; 121 };
121 122
122 123
124 /** Tail codes used in arithmetic coding using block Gilbert-Moore codes.
125 */
126 static const uint8_t tail_code[16][6] = {
127 { 74, 44, 25, 13, 7, 3},
128 { 68, 42, 24, 13, 7, 3},
129 { 58, 39, 23, 13, 7, 3},
130 {126, 70, 37, 19, 10, 5},
131 {132, 70, 37, 20, 10, 5},
132 {124, 70, 38, 20, 10, 5},
133 {120, 69, 37, 20, 11, 5},
134 {116, 67, 37, 20, 11, 5},
135 {108, 66, 36, 20, 10, 5},
136 {102, 62, 36, 20, 10, 5},
137 { 88, 58, 34, 19, 10, 5},
138 {162, 89, 49, 25, 13, 7},
139 {156, 87, 49, 26, 14, 7},
140 {150, 86, 47, 26, 14, 7},
141 {142, 84, 47, 26, 14, 7},
142 {131, 79, 46, 26, 14, 7}
143 };
144
145
123 enum RA_Flag { 146 enum RA_Flag {
124 RA_FLAG_NONE, 147 RA_FLAG_NONE,
125 RA_FLAG_FRAMES, 148 RA_FLAG_FRAMES,
126 RA_FLAG_HEADER 149 RA_FLAG_HEADER
127 }; 150 };
167 GetBitContext gb; 190 GetBitContext gb;
168 unsigned int cur_frame_length; ///< length of the current frame to decode 191 unsigned int cur_frame_length; ///< length of the current frame to decode
169 unsigned int frame_id; ///< the frame ID / number of the current frame 192 unsigned int frame_id; ///< the frame ID / number of the current frame
170 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced 193 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
171 unsigned int num_blocks; ///< number of blocks used in the current frame 194 unsigned int num_blocks; ///< number of blocks used in the current frame
195 uint8_t *bgmc_lut; ///< pointer at lookup tables used for BGMC
196 unsigned int *bgmc_lut_status; ///< pointer at lookup table status flags used for BGMC
172 int ltp_lag_length; ///< number of bits used for ltp lag value 197 int ltp_lag_length; ///< number of bits used for ltp lag value
173 int *use_ltp; ///< contains use_ltp flags for all channels 198 int *use_ltp; ///< contains use_ltp flags for all channels
174 int *ltp_lag; ///< contains ltp lag values for all channels 199 int *ltp_lag; ///< contains ltp lag values for all channels
175 int **ltp_gain; ///< gain values for ltp 5-tap filter for a channel 200 int **ltp_gain; ///< gain values for ltp 5-tap filter for a channel
176 int *ltp_gain_buffer; ///< contains all gain values for ltp 5-tap filter 201 int *ltp_gain_buffer; ///< contains all gain values for ltp 5-tap filter
381 error = errval; \ 406 error = errval; \
382 } \ 407 } \
383 } 408 }
384 409
385 MISSING_ERR(sconf->floating, "Floating point decoding", -1); 410 MISSING_ERR(sconf->floating, "Floating point decoding", -1);
386 MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1);
387 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1); 411 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
388 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0); 412 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
389 413
390 return error; 414 return error;
391 } 415 }
552 ALSSpecificConfig *sconf = &ctx->sconf; 576 ALSSpecificConfig *sconf = &ctx->sconf;
553 AVCodecContext *avctx = ctx->avctx; 577 AVCodecContext *avctx = ctx->avctx;
554 GetBitContext *gb = &ctx->gb; 578 GetBitContext *gb = &ctx->gb;
555 unsigned int k; 579 unsigned int k;
556 unsigned int s[8]; 580 unsigned int s[8];
581 unsigned int sx[8];
557 unsigned int sub_blocks, log2_sub_blocks, sb_length; 582 unsigned int sub_blocks, log2_sub_blocks, sb_length;
558 unsigned int start = 0; 583 unsigned int start = 0;
559 unsigned int opt_order; 584 unsigned int opt_order;
560 int sb; 585 int sb;
561 int32_t *quant_cof = bd->quant_cof; 586 int32_t *quant_cof = bd->quant_cof;
587 int32_t *current_res;
562 588
563 589
564 // ensure variable block decoding by reusing this field 590 // ensure variable block decoding by reusing this field
565 bd->const_block = 0; 591 bd->const_block = 0;
566 592
589 return -1; 615 return -1;
590 } 616 }
591 617
592 sb_length = bd->block_length >> log2_sub_blocks; 618 sb_length = bd->block_length >> log2_sub_blocks;
593 619
594
595 if (sconf->bgmc) { 620 if (sconf->bgmc) {
596 // TODO: BGMC mode 621 s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
622 for (k = 1; k < sub_blocks; k++)
623 s[k] = s[k - 1] + decode_rice(gb, 2);
624
625 for (k = 0; k < sub_blocks; k++) {
626 sx[k] = s[k] & 0x0F;
627 s [k] >>= 4;
628 }
597 } else { 629 } else {
598 s[0] = get_bits(gb, 4 + (sconf->resolution > 1)); 630 s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
599 for (k = 1; k < sub_blocks; k++) 631 for (k = 1; k < sub_blocks; k++)
600 s[k] = s[k - 1] + decode_rice(gb, 0); 632 s[k] = s[k - 1] + decode_rice(gb, 0);
601 } 633 }
695 start = FFMIN(opt_order, 3); 727 start = FFMIN(opt_order, 3);
696 } 728 }
697 729
698 // read all residuals 730 // read all residuals
699 if (sconf->bgmc) { 731 if (sconf->bgmc) {
700 // TODO: BGMC mode 732 unsigned int delta[sub_blocks];
733 unsigned int k [sub_blocks];
734 unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
735 unsigned int i = start;
736
737 // read most significant bits
738 unsigned int high;
739 unsigned int low;
740 unsigned int value;
741
742 ff_bgmc_decode_init(gb, &high, &low, &value);
743
744 current_res = bd->raw_samples + start;
745
746 for (sb = 0; sb < sub_blocks; sb++, i = 0) {
747 k [sb] = s[sb] > b ? s[sb] - b : 0;
748 delta[sb] = 5 - s[sb] + k[sb];
749
750 ff_bgmc_decode(gb, sb_length, current_res,
751 delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
752
753 current_res += sb_length;
754 }
755
756 ff_bgmc_decode_end(gb);
757
758
759 // read least significant bits and tails
760 i = start;
761 current_res = bd->raw_samples + start;
762
763 for (sb = 0; sb < sub_blocks; sb++, i = 0) {
764 unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
765 unsigned int cur_k = k[sb];
766 unsigned int cur_s = s[sb];
767
768 for (; i < sb_length; i++) {
769 int32_t res = *current_res;
770
771 if (res == cur_tail_code) {
772 unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10))
773 << (5 - delta[sb]);
774
775 res = decode_rice(gb, cur_s);
776
777 if (res >= 0) {
778 res += (max_msb ) << cur_k;
779 } else {
780 res -= (max_msb - 1) << cur_k;
781 }
782 } else {
783 if (res > cur_tail_code)
784 res--;
785
786 if (res & 1)
787 res = -res;
788
789 res >>= 1;
790
791 if (cur_k) {
792 res <<= cur_k;
793 res |= get_bits_long(gb, cur_k);
794 }
795 }
796
797 *current_res++ = res;
798 }
799 }
701 } else { 800 } else {
702 int32_t *current_res = bd->raw_samples + start; 801 current_res = bd->raw_samples + start;
703 802
704 for (sb = 0; sb < sub_blocks; sb++, start = 0) 803 for (sb = 0; sb < sub_blocks; sb++, start = 0)
705 for (; start < sb_length; start++) 804 for (; start < sb_length; start++)
706 *current_res++ = decode_rice(gb, s[sb]); 805 *current_res++ = decode_rice(gb, s[sb]);
707 } 806 }
1345 static av_cold int decode_end(AVCodecContext *avctx) 1444 static av_cold int decode_end(AVCodecContext *avctx)
1346 { 1445 {
1347 ALSDecContext *ctx = avctx->priv_data; 1446 ALSDecContext *ctx = avctx->priv_data;
1348 1447
1349 av_freep(&ctx->sconf.chan_pos); 1448 av_freep(&ctx->sconf.chan_pos);
1449
1450 ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
1350 1451
1351 av_freep(&ctx->use_ltp); 1452 av_freep(&ctx->use_ltp);
1352 av_freep(&ctx->ltp_lag); 1453 av_freep(&ctx->ltp_lag);
1353 av_freep(&ctx->ltp_gain); 1454 av_freep(&ctx->ltp_gain);
1354 av_freep(&ctx->ltp_gain_buffer); 1455 av_freep(&ctx->ltp_gain_buffer);
1393 if (check_specific_config(ctx)) { 1494 if (check_specific_config(ctx)) {
1394 decode_end(avctx); 1495 decode_end(avctx);
1395 return -1; 1496 return -1;
1396 } 1497 }
1397 1498
1499 if (sconf->bgmc)
1500 ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
1501
1398 if (sconf->floating) { 1502 if (sconf->floating) {
1399 avctx->sample_fmt = SAMPLE_FMT_FLT; 1503 avctx->sample_fmt = SAMPLE_FMT_FLT;
1400 avctx->bits_per_raw_sample = 32; 1504 avctx->bits_per_raw_sample = 32;
1401 } else { 1505 } else {
1402 avctx->sample_fmt = sconf->resolution > 1 1506 avctx->sample_fmt = sconf->resolution > 1