Ignore:
Timestamp:
12/18/06 20:52:54 (17 years ago)
Author:
r2d
Message:
  • changed entropy coding (all huffman tables are canonical)
  • scalefactor range now -6..121, no more clipping
  • compression gain is 2.36% on the test file
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpc/branches/r2d/libmpcdec/mpc_decoder.c

    r158 r161  
    3636#include <string.h>
    3737#include <mpcdec/mpcdec.h>
     38#include <mpc/minimax.h>
    3839#include "decoder.h"
    3940#include "huffman.h"
     
    4445mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits);
    4546mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table);
     47mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n);
     48mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max);
    4649
    4750//SV7 tables
     
    5053extern const mpc_huffman    mpc_table_HuffSCFI [ 4];
    5154extern const mpc_huffman    mpc_table_HuffDSCF [16];
     55
     56//SV8 tables
     57extern const mpc_huffman mpc_table_HuffRes [2][17];
     58extern const mpc_huffman * mpc_table_HuffQ_8 [2][8];
     59extern const mpc_huffman mpc_table_HuffQ9up_8 [256];
     60extern const mpc_huffman mpc_table_HuffBands [33];
     61extern const mpc_huffman mpc_table_HuffSCFI_1 [4];
     62extern const mpc_huffman mpc_table_HuffSCFI_2 [16];
     63extern const mpc_huffman mpc_table_HuffDSCF_1 [64];
     64extern const mpc_huffman mpc_table_HuffDSCF_2 [65];
    5265
    5366//------------------------------------------------------------------------------
     
    6881//------------------------------------------------------------------------------
    6982void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r);
     83void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r,
     84                                                                        mpc_bool_t is_key_frame);
    7085static void mpc_decoder_requantisierung(mpc_decoder *d);
    7186
     
    143158        }
    144159
    145         mpc_decoder_read_bitstream_sv7(d, r);
     160        if (d->stream_version == 8)
     161                mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
     162        else
     163                mpc_decoder_read_bitstream_sv7(d, r);
     164
    146165        if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
    147166                mpc_decoder_requantisierung(d);
     
    483502}
    484503
    485 
    486 
     504void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
     505{
     506    // these arrays hold decoding results for bundled quantizers (3- and 5-step)
     507        static const mpc_int8_t idx50[125] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
     508        static const mpc_int8_t idx51[125] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
     509        static const mpc_int8_t idx52[125] = {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
     510
     511        mpc_int32_t n, Max_used_Band;
     512        const mpc_huffman *Table, *Tables[2];
     513
     514        /***************************** Header *****************************/
     515
     516        if (is_key_frame == MPC_TRUE) {
     517                Max_used_Band = mpc_bits_log_dec(r, d->max_band);
     518        } else {
     519                Max_used_Band = d->last_max_band + mpc_bits_huff_dec(r, mpc_table_HuffBands);
     520                if (Max_used_Band > 32) Max_used_Band -= 33;
     521        }
     522        d->last_max_band = Max_used_Band;
     523
     524        if (Max_used_Band) {
     525                d->Res_L[Max_used_Band-1] = mpc_bits_huff_dec(r, mpc_table_HuffRes[0]);
     526                d->Res_R[Max_used_Band-1] = mpc_bits_huff_dec(r, mpc_table_HuffRes[0]);
     527                for ( n = Max_used_Band - 2; n >= 0; n--) {
     528                        d->Res_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffRes[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
     529                        if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
     530                        d->Res_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffRes[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
     531                        if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
     532                }
     533
     534                if (d->ms) {
     535                        int cnt = 0, tot = 0;
     536                        mpc_uint32_t tmp = 0;
     537                        for( n = 0; n < Max_used_Band; n++)
     538                                if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
     539                                        tot++;
     540                        cnt = mpc_bits_log_dec(r, tot);
     541                        if (cnt != 0 && cnt != tot)
     542                                tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
     543                        if (cnt * 2 > tot) tmp = ~tmp;
     544                        for( n = Max_used_Band - 1; n >= 0; n--)
     545                                if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
     546                                        d->MS_Flag[n] = tmp & 1;
     547                                        tmp >>= 1;
     548                                }
     549                }
     550        }
     551
     552        for( n = Max_used_Band; n <= d->max_band; n++)
     553                d->Res_L[n] = d->Res_R[n] = 0;
     554
     555
     556
     557        /****************************** SCFI ******************************/
     558        if (is_key_frame == MPC_TRUE){
     559                for( n = 0; n < 32; n++)
     560                        d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
     561        }
     562
     563        Tables[0] = mpc_table_HuffSCFI_1;
     564        Tables[1] = mpc_table_HuffSCFI_2;
     565        for ( n = 0; n < Max_used_Band; n++ ) {
     566                int tmp = 0, cnt = -1;
     567                if (d->Res_L[n]) cnt++;
     568                if (d->Res_R[n]) cnt++;
     569                if (cnt >= 0) {
     570                        tmp = mpc_bits_huff_dec(r, Tables[cnt]);
     571                        if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
     572                        if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
     573                }
     574        }
     575
     576        /**************************** SCF/DSCF ****************************/
     577
     578        for ( n = 0; n < Max_used_Band; n++ ) {
     579                mpc_int32_t * SCF = d->SCF_Index_L[n];
     580                mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
     581                mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
     582
     583                do {
     584                        if ( Res ) {
     585                                int m;
     586                                if (*DSCF_Flag == 1) {
     587                                        SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
     588                                        *DSCF_Flag = 0;
     589                                } else {
     590                                        mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_2);
     591                                        if (tmp == 64)
     592                                                tmp += mpc_bits_read(r, 6);
     593                                        SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
     594                                }
     595                                for( m = 0; m < 2; m++){
     596                                        if (((SCFI << m) & 2) == 0) {
     597                                                mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_1);
     598                                                if (tmp == 31)
     599                                                        tmp = 64 + mpc_bits_read(r, 6);
     600                                                SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
     601                                        } else
     602                                                SCF[m + 1] = SCF[m];
     603                                }
     604                        }
     605                        Res = d->Res_R[n];
     606                        SCFI = d->SCFI_R[n];
     607                        DSCF_Flag = &d->DSCF_Flag_R[n];
     608                } while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
     609        }
     610
     611        /***************************** Samples ****************************/
     612        for ( n = 0; n < Max_used_Band; n++ ) {
     613                mpc_int32_t *q = d->Q[n].L, Res = d->Res_L[n];
     614                static const unsigned int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
     615                static const mpc_int8_t HuffQ2_var[5*5*5] =
     616                {6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6};
     617
     618                do {
     619                        mpc_int32_t k = 0, idx = 1;
     620                        switch (Res) {
     621                                case -1:
     622                                        for ( ; k<36; k++ ) {
     623                                                mpc_uint32_t tmp = mpc_random_int(d);
     624                                                q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
     625                                        }
     626                                        break;
     627                                case 1:
     628                                        Table = mpc_table_HuffQ_8 [0][0];
     629                                        for( ; k < 36; ){
     630                                                int kmax = k + 18;
     631                                                mpc_uint_t cnt = mpc_bits_huff_dec(r, Table);
     632                                                idx = 0;
     633                                                if (cnt > 0 && cnt < 18)
     634                                                                idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
     635                                                if (cnt > 9) idx = ~idx;
     636                                                for ( ; k < kmax; k++) {
     637                                                        q[k] = 0;
     638                                                        if ( idx & (1 << 17) )
     639                                                                q[k] = (mpc_bits_read(r, 1) << 1) - 1;
     640                                                        idx <<= 1;
     641                                                }
     642                                        }
     643                                        break;
     644                                case 2:
     645                                        Tables[0] = mpc_table_HuffQ_8 [0][1];
     646                                        Tables[1] = mpc_table_HuffQ_8 [1][1];
     647                                        idx = 2 * thres[Res];
     648                                        for ( ; k < 36; k += 3) {
     649                                                int tmp = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
     650                                                q[k] = idx50[tmp];
     651                                                q[k + 1] = idx51[tmp];
     652                                                q[k + 2] = idx52[tmp];
     653                                                idx = (idx >> 1) + HuffQ2_var[tmp];
     654                                        }
     655                                        break;
     656                                case 3:
     657                                case 4:
     658                                        Table = mpc_table_HuffQ_8[0][Res - 1];
     659                                        for ( ; k < 36; k += 2 ) {
     660                                                union {
     661                                                        mpc_uint8_t sym;
     662                                                        struct { mpc_int16_t s1:4, s2:4; };
     663                                                } tmp;
     664                                                tmp.sym = (mpc_uint8_t) mpc_bits_huff_dec(r, Table);
     665                                                q[k] = tmp.s1;
     666                                                q[k + 1] = tmp.s2;
     667                                        }
     668                                        break;
     669                                case 5:
     670                                case 6:
     671                                case 7:
     672                                case 8:
     673                                        Tables[0] = mpc_table_HuffQ_8 [0][Res - 1];
     674                                        Tables[1] = mpc_table_HuffQ_8 [1][Res - 1];
     675                                        idx = 2 * thres[Res];
     676                                        for ( ; k < 36; k++ ) {
     677                                                q[k] = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
     678                                                idx = (idx >> 1) + absi(q[k]);
     679                                        }
     680                                        break;
     681                                case 9: case 10: case 11: case 12: case 13: case 14: case 15:
     682                                        for ( ; k < 36; k++ ) {
     683                                                q[k] = mpc_bits_huff_dec(r, mpc_table_HuffQ9up_8);
     684                                                if (Res != 9)
     685                                                        q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
     686                                                q[k] -= Dc[Res];
     687                                        }
     688                                        break;
     689                        }
     690
     691                        Res = d->Res_R[n];
     692                } while (q == d->Q[n].L && (q = d->Q[n].R));
     693        }
     694}
     695
Note: See TracChangeset for help on using the changeset viewer.