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

📄 huffman.c

📁 faac-1.25.rar音频编解码器demo
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********

This software module was originally developed by Dolby
Laboratories and edited by Sony Corporation
in the course of development of the MPEG-2 NBC/MPEG-4
Audio standard ISO/IEC13818-7, 14496-1, 2 and 3. This software module is an
implementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio tools as
specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC  gives users of the
MPEG-2NBC/MPEG-4 Audio standards free license to this software module
or modifications thereof for use in hardware or software products
claiming conformance to the MPEG-2 NBC/MPEG-4 Audio  standards. Those
intending to use this software module in hardware or software products
are advised that this use may infringe existing patents. The original
developer of this software module, the subsequent
editors and their companies, and ISO/IEC have no liability for use of
this software module or modifications thereof in an
implementation. Copyright is not released for non MPEG-2 NBC/MPEG-4
Audio conforming products. The original developer retains full right to
use the code for the developer's own purpose, assign or donate the code to a
third party and to inhibit third party from using the code for non
MPEG-2 NBC/MPEG-4 Audio conforming products. This copyright notice
must be included in all copies or derivative works. Copyright 1996.

***********/
/*
 * $Id: huffman.c,v 1.11 2005/02/02 07:53:20 sur Exp $
 */

#include <math.h>
#include <stdlib.h>

#include "huffman.h"
#include "coder.h"
#include "bitstream.h"
#include "util.h"

#include "hufftab.h"

void HuffmanInit(CoderInfo *coderInfo, unsigned int numChannels)
{
    unsigned int channel;

    for (channel = 0; channel < numChannels; channel++) {
        coderInfo[channel].data = (int*)AllocMemory(5*FRAME_LEN*sizeof(int));
        coderInfo[channel].len = (int*)AllocMemory(5*FRAME_LEN*sizeof(int));

#ifdef DRM
        coderInfo[channel].num_data_cw = (int*)AllocMemory(FRAME_LEN*sizeof(int));
#endif
    }
}

void HuffmanEnd(CoderInfo *coderInfo, unsigned int numChannels)
{
    unsigned int channel;

    for (channel = 0; channel < numChannels; channel++) {
        if (coderInfo[channel].data) FreeMemory(coderInfo[channel].data);
        if (coderInfo[channel].len) FreeMemory(coderInfo[channel].len);

#ifdef DRM
        if (coderInfo[channel].num_data_cw) FreeMemory(coderInfo[channel].num_data_cw);
#endif
    }
}

int BitSearch(CoderInfo *coderInfo,
              int *quant)  /* Quantized spectral values */
  /*
  This function inputs a vector of quantized spectral data, quant[][], and returns a vector,
  'book_vector[]' that describes how to group together the scalefactor bands into a smaller
  number of sections.  There are MAX_SCFAC_BANDS elements in book_vector (equal to 49 in the
  case of long blocks and 112 for short blocks), and each element has a huffman codebook
  number assigned to it.

  For a quick and simple algorithm, this function performs a binary
  search across the sfb's (scale factor bands).  On the first approach, it calculates the
  needed amount of bits if every sfb were its own section and transmitted its own huffman
  codebook value side information (equal to 9 bits for a long block, 7 for a short).  The
  next iteration combines adjacent sfb's, and calculates the bit rate for length two sfb
  sections.  If any wider two-sfb section requires fewer bits than the sum of the two
  single-sfb sections (below it in the binary tree), then the wider section will be chosen.
  This process occurs until the sections are split into three uniform parts, each with an
  equal amount of sfb's contained.

  The binary tree is stored as a two-dimensional array.  Since this tree is not full, (there
  are only 49 nodes, not 2^6 = 64), the numbering is a little complicated.  If the tree were
  full, the top node would be 1.  It's children would be 2 and 3.  But, since this tree
  is not full, the top row of three nodes are numbered {4,5,6}.  The row below it is
  {8,9,10,11,12,13}, and so on.

  The binary tree is called bit_stats[112][3].  There are 112 total nodes (some are not
  used since it's not full).  bit_stats[x][0] holds the bit totals needed for the sfb sectioning
  strategy represented by the node x in the tree.  bit_stats[x][1] holds the optimal huffman
  codebook table that minimizes the bit rate, given the sectioning boundaries dictated by node x.
*/

{
    int i,j,k;
    int hop;
    int min_book_choice[112][3];
    int bit_stats[240][3];
    int total_bit_count;
    int levels;
    int pow2levels;
    int fracpow2lev;

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

    levels = (int) ((log((double)coderInfo->nr_of_sfb)/log((double)2.0))+1);

/* #define SLOW */

#ifdef SLOW
    for(i = 0; i < 5; i++) {
#else
        i = 0;
#endif
        hop = 1 << i;

        NoiselessBitCount(coderInfo, quant, hop, min_book_choice);

        /* load up the (not-full) binary search tree with the min_book_choice values */
        k=0;
        total_bit_count = 0;

	pow2levels = 1 << (levels - i);
	fracpow2lev = pow2levels + (coderInfo->nr_of_sfb >> i);

        for (j=pow2levels; j < fracpow2lev; j++)
        {
            bit_stats[j][0] = min_book_choice[k][0]; /* the minimum bit cost for this section */
            bit_stats[j][1] = min_book_choice[k][1]; /* used with this huffman book number */

#ifdef SLOW
            if (i>0){  /* not on the lowest level, grouping more than one signle scalefactor band per section*/
                if  (bit_stats[j][0] < bit_stats[2*j][0] + bit_stats[2*j+1][0]){

                    /* it is cheaper to combine surrounding sfb secionts into one larger huffman book section */
                    for(n=k;n<k+hop;n++) { /* write the optimal huffman book value for the new larger section */
                        if ( (book_vector[n]!=INTENSITY_HCB)&&(book_vector[n]!=INTENSITY_HCB2) ) { /* Don't merge with IS bands */
                            book_vector[n] = bit_stats[j][1];
                        }
                    }
                } else {  /* it was cheaper to transmit the smaller huffman table sections */
                    bit_stats[j][0] = bit_stats[2*j][0] + bit_stats[2*j+1][0];
                }
	    } else
#endif
	    {  /* during the first stage of the iteration, all sfb's are individual sections */
                if ( (book_vector[k]!=INTENSITY_HCB)&&(book_vector[k]!=INTENSITY_HCB2) ) {
                    book_vector[k] = bit_stats[j][1];  /* initially, set all sfb's to their own optimal section table values */
                }
            }
            total_bit_count = total_bit_count +  bit_stats[j][0];
            k=k+hop;
        }
#ifdef SLOW
    }
#endif
    /*   book_vector[k] = book_vector[k-1]; */
    return(total_bit_count);
}


int NoiselessBitCount(CoderInfo *coderInfo,
                      int *quant,
                      int hop,
                      int min_book_choice[112][3])
{
  int i,j,k;

  /*
     This function inputs:
     - the quantized spectral data, 'quant[]';
     - all of the huffman codebooks, 'huff[][]';
     - the size of the sections, in scalefactor bands (SFB's), 'hop';
     - an empty matrix, min_book_choice[][] passed to it;

     This function outputs:
     - the matrix, min_book_choice.  It is a two dimensional matrix, with its
     rows corresponding to spectral sections.  The 0th column corresponds to
     the bits needed to code a section with 'hop' scalefactors bands wide, all using
     the same huffman codebook.  The 1st column contains the huffman codebook number
     that allows the minimum number of bits to be used.

     Other notes:
     - Initally, the dynamic range is calculated for each spectral section.  The section
     can only be entropy coded with books that have an equal or greater dynamic range
     than the section's spectral data.  The exception to this is for the 11th ESC codebook.
     If the dynamic range is larger than 16, then an escape code is appended after the
     table 11 codeword which encodes the larger value explicity in a pseudo-non-uniform
     quantization method.

     */

    int max_sb_coeff;
    int book_choice[12][2];
    int total_bits_cost = 0;
    int offset, length, end;
    int q;

    /* set local pointer to sfb_offset */
    int *sfb_offset = coderInfo->sfb_offset;
    int nr_of_sfb = coderInfo->nr_of_sfb;

    /* each section is 'hop' scalefactor bands wide */
    for (i=0; i < nr_of_sfb; i=i+hop){
#ifdef SLOW
        if ((i+hop) > nr_of_sfb)
            q = nr_of_sfb;
        else
#endif
            q = i+hop;

        {

            /* find the maximum absolute value in the current spectral section, to see what tables are available to use */
            max_sb_coeff = 0;
            for (j=sfb_offset[i]; j<sfb_offset[q]; j++){  /* snl */
                if (ABS(quant[j]) > max_sb_coeff)
                    max_sb_coeff = ABS(quant[j]);
            }

            j = 0;
            offset = sfb_offset[i];
#ifdef SLOW
            if ((i+hop) > nr_of_sfb){
                end = sfb_offset[nr_of_sfb];
            } else
#endif
                end = sfb_offset[q];
            length = end - offset;

            /* all spectral coefficients in this section are zero */
            if (max_sb_coeff == 0) {
                book_choice[j][0] = CalcBits(coderInfo,0,quant,offset,length);
                book_choice[j++][1] = 0;

            }
            else {  /* if the section does have non-zero coefficients */
                if(max_sb_coeff < 2){
                    book_choice[j][0] = CalcBits(coderInfo,1,quant,offset,length);
                    book_choice[j++][1] = 1;
                    book_choice[j][0] = CalcBits(coderInfo,2,quant,offset,length);
                    book_choice[j++][1] = 2;
                    book_choice[j][0] = CalcBits(coderInfo,3,quant,offset,length);
                    book_choice[j++][1] = 3;
                }
                else if (max_sb_coeff < 3){
                    book_choice[j][0] = CalcBits(coderInfo,3,quant,offset,length);
                    book_choice[j++][1] = 3;
                    book_choice[j][0] = CalcBits(coderInfo,4,quant,offset,length);
                    book_choice[j++][1] = 4;
                    book_choice[j][0] = CalcBits(coderInfo,5,quant,offset,length);
                    book_choice[j++][1] = 5;
                }
                else if (max_sb_coeff < 5){
                    book_choice[j][0] = CalcBits(coderInfo,5,quant,offset,length);
                    book_choice[j++][1] = 5;
                    book_choice[j][0] = CalcBits(coderInfo,6,quant,offset,length);
                    book_choice[j++][1] = 6;
                    book_choice[j][0] = CalcBits(coderInfo,7,quant,offset,length);
                    book_choice[j++][1] = 7;
                }
                else if (max_sb_coeff < 8){
                    book_choice[j][0] = CalcBits(coderInfo,7,quant,offset,length);
                    book_choice[j++][1] = 7;
                    book_choice[j][0] = CalcBits(coderInfo,8,quant,offset,length);
                    book_choice[j++][1] = 8;
                    book_choice[j][0] = CalcBits(coderInfo,9,quant,offset,length);
                    book_choice[j++][1] = 9;
                }
                else if (max_sb_coeff < 13){
                    book_choice[j][0] = CalcBits(coderInfo,9,quant,offset,length);
                    book_choice[j++][1] = 9;
                    book_choice[j][0] = CalcBits(coderInfo,10,quant,offset,length);
                    book_choice[j++][1] = 10;
                }
                /* (max_sb_coeff >= 13), choose table 11 */
                else {
                    book_choice[j][0] = CalcBits(coderInfo,11,quant,offset,length);
                    book_choice[j++][1] = 11;
                }
            }

            /* find the minimum bit cost and table number for huffman coding this scalefactor section */
            min_book_choice[i][1] = book_choice[0][1];
            min_book_choice[i][0] = book_choice[0][0];

            for(k=1;k<j;k++){
                if (book_choice[k][0] < min_book_choice[i][0]){
                    min_book_choice[i][1] = book_choice[k][1];
                    min_book_choice[i][0] = book_choice[k][0];
                }
            }
            total_bits_cost += min_book_choice[i][0];
        }
    }
    return(total_bits_cost);
}



static int CalculateEscSequence(int input, int *len_esc_sequence)
/*

⌨️ 快捷键说明

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