⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffman.c

📁 faac-1.25.rar音频编解码器demo
💻 C
📖 第 1 页 / 共 4 页
字号:
        return (bits);
    case 3:
        for(i=offset;i<offset+length;i=i+4){
            index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
            bits += huff3[index][FIRSTINTAB];
            for(j=0;j<4;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 4:
        for(i=offset;i<offset+length;i=i+4){
            index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
            bits += huff4[index][FIRSTINTAB];
            for(j=0;j<4;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 5:
        for(i=offset;i<offset+length;i=i+2){
            index = 9*(quant[i]) + (quant[i+1]) + 40;
            bits += huff5[index][FIRSTINTAB];
        }
        return (bits);
    case 6:
        for(i=offset;i<offset+length;i=i+2){
            index = 9*(quant[i]) + (quant[i+1]) + 40;
            bits += huff6[index][FIRSTINTAB];
        }
        return (bits);
    case 7:
        for(i=offset;i<offset+length;i=i+2){
            index = 8*ABS(quant[i]) + ABS(quant[i+1]);
            bits += huff7[index][FIRSTINTAB];
            for(j=0;j<2;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 8:
        for(i=offset;i<offset+length;i=i+2){
            index = 8*ABS(quant[i]) + ABS(quant[i+1]);
            bits += huff8[index][FIRSTINTAB];
            for(j=0;j<2;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 9:
        for(i=offset;i<offset+length;i=i+2){
            index = 13*ABS(quant[i]) + ABS(quant[i+1]);
            bits += huff9[index][FIRSTINTAB];
            for(j=0;j<2;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 10:
        for(i=offset;i<offset+length;i=i+2){
            index = 13*ABS(quant[i]) + ABS(quant[i+1]);
            bits += huff10[index][FIRSTINTAB];
            for(j=0;j<2;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }
        }
        return (bits);
    case 11:
        /* First, calculate the indecies into the huffman tables */
        for(i=offset;i<offset+length;i=i+2){
            if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) {  /* both codewords were above 16 */
                /* first, code the orignal pair, with the larger value saturated to +/- 16 */
                index = 17*16 + 16;
            } else if (ABS(quant[i]) >= 16) {  /* the first codeword was above 16, not the second one */
                /* first, code the orignal pair, with the larger value saturated to +/- 16 */
                index = 17*16 + ABS(quant[i+1]);
            } else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
                index = 17*ABS(quant[i]) + 16;
            } else {  /* there were no values above 16, so no escape sequences */
                index = 17*ABS(quant[i]) + ABS(quant[i+1]);
            }

            /* write out the codewords */
            bits += huff11[index][FIRSTINTAB];

            /* Take care of the sign bits */
            for(j=0;j<2;j++){
                if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
            }

            /* write out the escape sequences */
            if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) {  /* both codewords were above 16 */
                /* code and transmit the first escape_sequence */
                CalculateEscSequence(quant[i],&len_esc);
                bits += len_esc;

                /* then code and transmit the second escape_sequence */
                CalculateEscSequence(quant[i+1],&len_esc);
                bits += len_esc;
            } else if (ABS(quant[i]) >= 16) {  /* the first codeword was above 16, not the second one */
                /* code and transmit the escape_sequence */
                CalculateEscSequence(quant[i],&len_esc);
                bits += len_esc;
            } else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
                /* code and transmit the escape_sequence */
                CalculateEscSequence(quant[i+1],&len_esc);
                bits += len_esc;
            }
        }
        return (bits);
    }
    return 0;
}

int SortBookNumbers(CoderInfo *coderInfo,
                    BitStream *bitStream,
                    int writeFlag)
{
  /*
    This function inputs the vector, 'book_vector[]', which is of length MAX_SCFAC_BANDS,
    and contains the optimal huffman tables of each sfb.  It returns the vector, 'output_book_vector[]', which
    has it's elements formatted for the encoded bit stream.  It's syntax is:

    {sect_cb[0], length_segment[0], ... ,sect_cb[num_of_sections], length_segment[num_of_sections]}

    The above syntax is true, unless there is an escape sequence.  An
    escape sequence occurs when a section is longer than 2 ^ (bit_len)
    long in units of scalefactor bands.  Also, the integer returned from
    this function is the number of bits written in the bitstream,
    'bit_count'.

    This function supports both long and short blocks.
    */

    int i;
    int repeat_counter;
    int bit_count = 0;
    int previous;
    int max, bit_len/*,sfbs*/;
    int max_sfb,g,band;
	int sect_cb_bits = 4;

    /* Set local pointers to coderInfo elements */
    int* book_vector = coderInfo->book_vector;

#ifdef DRM
    sect_cb_bits = 5; /* 5 bits in case of VCB11 */
#endif

    if (coderInfo->block_type == ONLY_SHORT_WINDOW){
        max = 7;
        bit_len = 3;
    } else {  /* the block_type is a long,start, or stop window */
        max = 31;
        bit_len = 5;
    }

    /* Compute number of scalefactor bands */
    max_sfb = coderInfo->nr_of_sfb / coderInfo->num_window_groups;


    for (g = 0; g < coderInfo->num_window_groups; g++) {
        band=g*max_sfb;

        repeat_counter=1;

        previous = book_vector[band];
        if (writeFlag) {
            PutBit(bitStream,book_vector[band],sect_cb_bits);
        }
        bit_count += sect_cb_bits;

        for (i=band+1;i<band+max_sfb;i++) {
#ifdef DRM
            /* sect_len is not transmitted in case the codebook for a */
            /* section is 11 or in the range of 16 and 31 */
            if ((previous == 11) ||
                ((previous >= 16) && (previous <= 32)))
            {
                if (writeFlag)
                    PutBit(bitStream,book_vector[i],sect_cb_bits);
                bit_count += sect_cb_bits;
                previous = book_vector[i];
                repeat_counter=1;

            } else
#endif
            if( (book_vector[i] != previous)) {
                if (writeFlag) {
                    PutBit(bitStream,repeat_counter,bit_len);
                }
                bit_count += bit_len;

                if (repeat_counter == max){  /* in case you need to terminate an escape sequence */
                    if (writeFlag)
                        PutBit(bitStream,0,bit_len);
                    bit_count += bit_len;
                }

                if (writeFlag)
                    PutBit(bitStream,book_vector[i],sect_cb_bits);
                bit_count += sect_cb_bits;
                previous = book_vector[i];
                repeat_counter=1;
            }
            /* if the length of the section is longer than the amount of bits available in */
            /* the bitsream, "max", then start up an escape sequence */
            else if ((book_vector[i] == previous) && (repeat_counter == max)) {
                if (writeFlag) {
                    PutBit(bitStream,repeat_counter,bit_len);
                }
                bit_count += bit_len;
                repeat_counter = 1;
            }
            else {
                repeat_counter++;
            }
        }

#ifdef DRM
        if (!((previous == 11) || ((previous >= 16) && (previous <= 32))))
#endif
        {
            if (writeFlag)
                PutBit(bitStream,repeat_counter,bit_len);
            bit_count += bit_len;

            if (repeat_counter == max) {  /* special case if the last section length is an */
                /* escape sequence */
                if (writeFlag)
                    PutBit(bitStream,0,bit_len);
                bit_count += bit_len;
            }
        }
    }  /* Bottom of group iteration */

    return bit_count;
}

int WriteScalefactors(CoderInfo *coderInfo,
                      BitStream *bitStream,
                      int writeFlag)

{
    /* this function takes care of counting the number of bits necessary */
    /* to encode the scalefactors.  In addition, if the writeFlag == 1, */
    /* then the scalefactors are written out the bitStream output bit */
    /* stream.  it returns k, the number of bits written to the bitstream*/

    int i,j,bit_count=0;
    int diff,length,codeword;
    int previous_scale_factor;
    int previous_is_factor;       /* Intensity stereo */
    int index = 0;
    int nr_of_sfb_per_group;

    /* set local pointer to coderInfo elements */
    int* scale_factors = coderInfo->scale_factor;

    if (coderInfo->block_type == ONLY_SHORT_WINDOW) { /* short windows */
        nr_of_sfb_per_group = coderInfo->nr_of_sfb/coderInfo->num_window_groups;
    } else {
        nr_of_sfb_per_group = coderInfo->nr_of_sfb;
        coderInfo->num_window_groups = 1;
        coderInfo->window_group_length[0] = 1;
    }

    previous_scale_factor = coderInfo->global_gain;
    previous_is_factor = 0;

    for(j=0; j<coderInfo->num_window_groups; j++){
        for(i=0;i<nr_of_sfb_per_group;i++) {
            /* test to see if any codebooks in a group are zero */
            if ( (coderInfo->book_vector[index]==INTENSITY_HCB) ||
                (coderInfo->book_vector[index]==INTENSITY_HCB2) ) {
                /* only send scalefactors if using non-zero codebooks */
                diff = scale_factors[index] - previous_is_factor;
                if ((diff < 60)&&(diff >= -60))
                    length = huff12[diff+60][FIRSTINTAB];
                else length = 0;
                bit_count+=length;
                previous_is_factor = scale_factors[index];
                if (writeFlag == 1 ) {
                    codeword = huff12[diff+60][LASTINTAB];
                    PutBit(bitStream,codeword,length);
                }
            } else if (coderInfo->book_vector[index]) {
                /* only send scalefactors if using non-zero codebooks */
                diff = scale_factors[index] - previous_scale_factor;
                if ((diff < 60)&&(diff >= -60))
                    length = huff12[diff+60][FIRSTINTAB];
                else length = 0;
                bit_count+=length;
                previous_scale_factor = scale_factors[index];
                if (writeFlag == 1 ) {
                    codeword = huff12[diff+60][LASTINTAB];
                    PutBit(bitStream,codeword,length);
                }
            }
            index++;
        }
    }
    return bit_count;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -