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

📄 huffman.c

📁 faac-1.25.rar音频编解码器demo
💻 C
📖 第 1 页 / 共 4 页
字号:
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                } else
                if(quant[i+j] < 0) {  /* send out '1' if a negative value */
                    data[counter] = 1;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                }
            }
#ifdef DRM
            coderInfo->iLenReordSpData += cur_cw_len;
            if (cur_cw_len > coderInfo->iLenLongestCW)
                coderInfo->iLenLongestCW = cur_cw_len;

            coderInfo->cur_cw++;
#endif
        }
        coderInfo->spectral_count = counter;  /* send the current count back to the outside world */
        return(bits);
    case 9:
        for(i=offset;i<offset+length;i=i+2){
            index = 13*ABS(quant[i]) + ABS(quant[i+1]);
            codebook = huff9[index][LASTINTAB];
            tmp = huff9[index][FIRSTINTAB];
            bits = bits + tmp;
            data[counter] = codebook;
            len[counter++] = tmp;
#ifdef DRM
            num_data[coderInfo->cur_cw] = 1;
            cur_cw_len = tmp;
#endif
            for(j=0;j<2;j++){
                if(quant[i+j] > 0) {  /* send out '0' if a positive value */
                    data[counter] = 0;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                } else
                if(quant[i+j] < 0) {  /* send out '1' if a negative value */
                    data[counter] = 1;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                }
            }
#ifdef DRM
            coderInfo->iLenReordSpData += cur_cw_len;
            if (cur_cw_len > coderInfo->iLenLongestCW)
                coderInfo->iLenLongestCW = cur_cw_len;

            coderInfo->cur_cw++;
#endif
        }
        coderInfo->spectral_count = counter;  /* send the current count back to the outside world */
        return(bits);
    case 10:
        for(i=offset;i<offset+length;i=i+2){
            index = 13*ABS(quant[i]) + ABS(quant[i+1]);
            codebook = huff10[index][LASTINTAB];
            tmp = huff10[index][FIRSTINTAB];
            bits = bits + tmp;
            data[counter] = codebook;
            len[counter++] = tmp;
#ifdef DRM
            num_data[coderInfo->cur_cw] = 1;
            cur_cw_len = tmp;
#endif
            for(j=0;j<2;j++){
                if(quant[i+j] > 0) {  /* send out '0' if a positive value */
                    data[counter] = 0;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                } else
                if(quant[i+j] < 0) {  /* send out '1' if a negative value */
                    data[counter] = 1;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                }
            }
#ifdef DRM
            coderInfo->iLenReordSpData += cur_cw_len;
            if (cur_cw_len > coderInfo->iLenLongestCW)
                coderInfo->iLenLongestCW = cur_cw_len;

            coderInfo->cur_cw++;
#endif
        }
        coderInfo->spectral_count = counter;  /* send the current count back to the outside world */
        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 */
            tmp = huff11[index][FIRSTINTAB];
            codebook = huff11[index][LASTINTAB];
            bits += tmp;
            data[counter] = codebook;
            len[counter++] = tmp;
#ifdef DRM
            num_data[coderInfo->cur_cw] = 1;
            cur_cw_len = tmp;
#endif

            /* Take care of the sign bits */
            for(j=0;j<2;j++){
                if(quant[i+j] > 0) {  /* send out '0' if a positive value */
                    data[counter] = 0;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                } else
                if(quant[i+j] < 0) {  /* send out '1' if a negative value */
                    data[counter] = 1;
                    len[counter++] = 1;
                    bits += 1;
#ifdef DRM
                    num_data[coderInfo->cur_cw]++;
                    cur_cw_len += 1;
#endif
                }
            }

            /* 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 */
                esc_sequence = CalculateEscSequence(quant[i],&len_esc);
                bits += len_esc;
                data[counter] = esc_sequence;
                len[counter++] = len_esc;
#ifdef DRM
                num_data[coderInfo->cur_cw]++;
                cur_cw_len += len_esc;

                if (esc_sequence > max_esc_sequ)
                    max_esc_sequ = esc_sequence;
#endif

                /* then code and transmit the second escape_sequence */
                esc_sequence = CalculateEscSequence(quant[i+1],&len_esc);
                bits += len_esc;
                data[counter] = esc_sequence;
                len[counter++] = len_esc;
#ifdef DRM
                num_data[coderInfo->cur_cw]++;
                cur_cw_len += len_esc;

                if (esc_sequence > max_esc_sequ)
                    max_esc_sequ = esc_sequence;
#endif
            }
            else if (ABS(quant[i]) >= 16) {  /* the first codeword was above 16, not the second one */
                /* code and transmit the escape_sequence */
                esc_sequence = CalculateEscSequence(quant[i],&len_esc);
                bits += len_esc;
                data[counter] = esc_sequence;
                len[counter++] = len_esc;
#ifdef DRM
                num_data[coderInfo->cur_cw]++;
                cur_cw_len += len_esc;

                if (esc_sequence > max_esc_sequ)
                    max_esc_sequ = esc_sequence;
#endif
            }
            else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
                /* code and transmit the escape_sequence */
                esc_sequence = CalculateEscSequence(quant[i+1],&len_esc);
                bits += len_esc;
                data[counter] = esc_sequence;
                len[counter++] = len_esc;
#ifdef DRM
                num_data[coderInfo->cur_cw]++;
                cur_cw_len += len_esc;

                if (esc_sequence > max_esc_sequ)
                    max_esc_sequ = esc_sequence;
#endif
            }
#ifdef DRM
            coderInfo->iLenReordSpData += cur_cw_len;
            if (cur_cw_len > coderInfo->iLenLongestCW)
                coderInfo->iLenLongestCW = cur_cw_len;

            coderInfo->cur_cw++;
#endif
        }
        coderInfo->spectral_count = counter;  /* send the current count back to the outside world */

#ifdef DRM
        /* VCB11: check which codebook should be used using max escape sequence */
        /* 8.5.3.1.3, table 157 */
        if (max_esc_sequ <= 15)
            *book = 16;
        else if (max_esc_sequ <= 31)
            *book = 17;
        else if (max_esc_sequ <= 47)
            *book = 18;
        else if (max_esc_sequ <= 63)
            *book = 19;
        else if (max_esc_sequ <= 95)
            *book = 20;
        else if (max_esc_sequ <= 127)
            *book = 21;
        else if (max_esc_sequ <= 159)
            *book = 22;
        else if (max_esc_sequ <= 191)
            *book = 23;
        else if (max_esc_sequ <= 223)
            *book = 24;
        else if (max_esc_sequ <= 255)
            *book = 25;
        else if (max_esc_sequ <= 319)
            *book = 26;
        else if (max_esc_sequ <= 383)
            *book = 27;
        else if (max_esc_sequ <= 511)
            *book = 28;
        else if (max_esc_sequ <= 767)
            *book = 29;
        else if (max_esc_sequ <= 1023)
            *book = 30;
        else if (max_esc_sequ <= 2047)
            *book = 31;
        /* else: codebook 11 -> it is already 11 */
#endif

        return(bits);
    }
    return 0;
}

int CalcBits(CoderInfo *coderInfo,
             int book,
             int *quant,
             int offset,
             int length)
{
  /*
     This function inputs
     - a specific codebook number, 'book'
     - the quantized spectral data, 'quant[]'
     - the offset into the spectral data to begin scanning, 'offset'
     - the 'length' of the segment to huffman code
     -> therefore, the segment quant[offset] to quant[offset+length-1]
     is huffman coded.

     This function outputs
     - the number of bits required, 'bits'  using the prescribed codebook, book applied to
     the given segment of spectral data.

   */

    int len_esc;
    int index;
    int bits = 0;
    int i, j;

    switch (book) {
    case 1:
        for(i=offset;i<offset+length;i=i+4){
            index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
            bits += huff1[index][FIRSTINTAB];
        }
        return (bits);
    case 2:
        for(i=offset;i<offset+length;i=i+4){
            index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
            bits += huff2[index][FIRSTINTAB];
        }

⌨️ 快捷键说明

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