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

📄 huffdec2.c

📁 AAC音频解码算法程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************* MPEG-2 NBC Audio Decoder **************************
 *                                                                           *
"This software module was originally developed by 
AT&T, Dolby Laboratories, Fraunhofer Gesellschaft IIS and edited by
Yoshiaki Oikawa (Sony Corporation),
Mitsuyuki Hatanaka (Sony Corporation)
in the course of development of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-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-2 NBC/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 and his/her company, 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 his/her  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(c)1996.
 *                                                                           *
 ****************************************************************************/

#include <math.h>
#include "all.h"

Info only_long_info;

typedef int DINT_DATATYPE ;

void
deinterleave(DINT_DATATYPE inptr[], DINT_DATATYPE outptr[], short ngroups,
    short nsubgroups[], int ncells[], short cellsize[])
{
    int i, j, k, l;
    DINT_DATATYPE *start_inptr, *start_subgroup_ptr, *subgroup_ptr;
    short cell_inc, subgroup_inc;

    start_subgroup_ptr = outptr;

    for (i = 0; i < ngroups; i++)
    {
	cell_inc = 0;
	start_inptr = inptr;

	/* Compute the increment size for the subgroup pointer */

	subgroup_inc = 0;
	for (j = 0; j < ncells[i]; j++) {
	    subgroup_inc += cellsize[j];
	}

	/* Perform the deinterleaving across all subgroups in a group */

	for (j = 0; j < ncells[i]; j++) {
	    subgroup_ptr = start_subgroup_ptr;

	    for (k = 0; k < nsubgroups[i]; k++) {
		outptr = subgroup_ptr + cell_inc;
		for (l = 0; l < cellsize[j]; l++) {
		    *outptr++ = *inptr++;
		}
		subgroup_ptr += subgroup_inc;
	    }
	    cell_inc += cellsize[j];
	}
	start_subgroup_ptr += (inptr - start_inptr);
    }
}

void 
calc_gsfb_table(Info *info, byte *group)
{
    int group_offset;
    int group_idx;
    int offset;
    short * group_offset_p;
    int sfb,len;
    /* first calc the group length*/
    if (info->islong){
	return;
    } else {
	group_offset = 0;
	group_idx =0;
	do  {
	    info->group_len[group_idx]=group[group_idx]-group_offset;
	    group_offset=group[group_idx];
	    group_idx++;
	} while (group_offset<8);
	info->num_groups=group_idx;
	group_offset_p = info->bk_sfb_top;
	offset=0;
	for (group_idx=0;group_idx<info->num_groups;group_idx++){
	    len = info->group_len[group_idx];
	    for (sfb=0;sfb<info->sfb_per_sbk[group_idx];sfb++){
		offset += info->sfb_width_128[sfb] * len;
		*group_offset_p++ = offset;
	    }
	}
    }
}

void
getgroup(Info *info, byte *group)
{
    int i, j, first_short;

    first_short=1;
    for (i = 0; i < info->nsbk; i++) {
	if (info->bins_per_sbk[i] > SN2) {
	    /* non-short windows are always their own group */
	    *group++ = i+1;
	}
	else {
	    /* only short-window sequences are grouped! */
	    if (first_short) {
		/* first short window is always a new group */
		first_short=0;
	    }
	    else {
		if((j = getbits(1)) == 0) {
		    *group++ = i;
		}
	    }
	}
    }
    *group = i;
}

/*
 * read a synthesis mask
 *  uses EXTENDED_MS_MASK
 *  and grouped mask 
 */
int
getmask(Info *info, byte *group, byte max_sfb, byte *mask)
{
    int b, i, mp;

    mp = getbits(LEN_MASK_PRES);

    /* special EXTENDED_MS_MASK cases */
    if(mp == 0) { /* no ms at all */
	return 0;
    }
    if(mp == 2) {/* MS for whole spectrum on, mask bits set to 1 */
	for(b = 0; b < info->nsbk; b = *group++)
	    for(i = 0; i < info->sfb_per_sbk[b]; i ++)
		*mask++ = 1;
	return 2;
    }

    /* otherwise get mask */
    for(b = 0; b < info->nsbk; b = *group++){
	for(i = 0; i < max_sfb; i ++) {
	    *mask = (byte)getbits(LEN_MASK);
	    mask++;
	}
	for( ; i < info->sfb_per_sbk[b]; i++){
	    *mask = 0;
	    mask++;
	}
    }
    return 1;
}

void
clr_tns( Info *info, TNS_frame_info *tns_frame_info )
{
    int s;

    tns_frame_info->n_subblocks = info->nsbk;
    for (s=0; s<tns_frame_info->n_subblocks; s++)
	tns_frame_info->info[s].n_filt = 0;
}

int
get_tns( Info *info, TNS_frame_info *tns_frame_info )
{
    int                       f, t, top, res, res2, compress;
    int                       short_flag, s;
    short                     *sp, tmp, s_mask, n_mask;
    TNSfilt                   *tns_filt;
    TNSinfo                   *tns_info;
    static int                sgn_mask[] = { 0x2, 0x4, 0x8 };
    static int                neg_mask[] = { 0xfffc, 0xfff8, 0xfff0 };


    short_flag = (!info->islong);
    tns_frame_info->n_subblocks = info->nsbk;

    for (s=0; s<tns_frame_info->n_subblocks; s++) {
	tns_info = &tns_frame_info->info[s];

	if (!(tns_info->n_filt = getbits( short_flag ? 1 : 2 )))
	    continue;
	    
	tns_info -> coef_res = res = getbits( 1 ) + 3;
	top = info->sfb_per_sbk[s];
	tns_filt = &tns_info->filt[ 0 ];
	for (f=tns_info->n_filt; f>0; f--)  {
	    tns_filt->stop_band = top;
	    top = tns_filt->start_band = top - getbits( short_flag ? 4 : 6 );
	    tns_filt->order = getbits( short_flag ? 3 : 5 );

	    if (tns_filt->order)  {
		tns_filt->direction = getbits( 1 );
		compress = getbits( 1 );

		res2 = res - compress;
		s_mask = sgn_mask[ res2 - 2 ];
		n_mask = neg_mask[ res2 - 2 ];

		sp = tns_filt -> coef;
		for (t=tns_filt->order; t>0; t--)  {
		    tmp = (short)getbits( res2 );
		    *sp++ = (tmp & s_mask) ? (short)(tmp | n_mask) : (short)tmp;
		}
	    }
	    tns_filt++;
	}
    }   /* subblock loop */
    return 1;
}

/*
 * NEC noiseless coding
 */
struct Nec_Info
{
    int pulse_data_present;
    int number_pulse;
    int pulse_start_sfb;
    int pulse_position[NUM_NEC_LINES];
    int pulse_offset[NUM_NEC_LINES];
    int pulse_amp[NUM_NEC_LINES];
};
struct Nec_Info nec_info;

void
get_nec_nc(struct Nec_Info *nec_info)
{
    int i;
    nec_info->number_pulse = getbits(LEN_NEC_NPULSE);
    nec_info->pulse_start_sfb = getbits(LEN_NEC_ST_SFB);
    for(i=0; i<nec_info->number_pulse+1; i++) {
	nec_info->pulse_offset[i] = getbits(LEN_NEC_POFF);
	nec_info->pulse_amp[i] = getbits(LEN_NEC_PAMP);
    }
}

void
nec_nc(int *coef, struct Nec_Info *nec_info)
{
    int i, k;

    k = only_long_info.sbk_sfb_top[0][nec_info->pulse_start_sfb];

    for(i=0; i<=nec_info->number_pulse; i++) {
		k += nec_info->pulse_offset[i];
		if (coef[k]>0)
			coef[k] += nec_info->pulse_amp[i];
		else
			coef[k] -= nec_info->pulse_amp[i];
    }
}

int
getics(Info *info, int common_window, byte *win, byte *wshape, 
    byte *group, byte *max_sfb, int *lpflag, int *prstflag, 
    byte *cb_map, Float *coef, short *global_gain, 
    short *factors,
    NOK_LT_PRED_STATUS *nok_ltp_status,
    TNS_frame_info *tns
	   )
{
    int nsect, i, cb, top, bot, tot_sfb;
    byte sect[ 2*(MAXBANDS+1) ];

    /*
	* global gain
	*/
    *global_gain = (short)getbits(LEN_SCL_PCM);

    if (!common_window)
		get_ics_info(win, wshape, group, max_sfb, lpflag, prstflag
		, nok_ltp_status
		);
    memcpy(info, winmap[*win], sizeof(Info));

    /* calculate total number of sfb for this grouping */
    if (*max_sfb == 0) {
		tot_sfb = 0;
    }
    else {
		i=0;
		tot_sfb = info->sfb_per_sbk[0];
		while (group[i++] < info->nsbk) {
			tot_sfb += info->sfb_per_sbk[0];
		}
    }

    /* 
	* section data
	*/
    nsect = huffcb(sect, info->sectbits, tot_sfb, info->sfb_per_sbk[0], *max_sfb);
    if(nsect==0 && *max_sfb>0)
		return 0;

		/* generate "linear" description from section info
		* stored as codebook for each scalefactor band and group
	*/
    if (nsect) {
		bot = 0;
		for (i=0; i<nsect; i++) {
			cb = sect[2*i];
			top = sect[2*i + 1];
			for (; bot<top; bot++)
				*cb_map++ = cb;
			bot = top;
		}
    }  else {
		for (i=0; i<MAXBANDS; i++)
			cb_map[i] = 0;
    }

    /* calculate band offsets
	* (because of grouping and interleaving this cannot be
	* a constant: store it in info.bk_sfb_top)
	*/
    calc_gsfb_table(info, group);

    /*
	* scale factor data
	*/
    if(!hufffac(info, group, nsect, sect, *global_gain, factors))
		return 0;

	/*
	* NEC noiseless coding
	*/
    if ((nec_info.pulse_data_present = getbits(LEN_PULSE_PRES))) {
		if (info->islong) {
			get_nec_nc(&nec_info);
		}
		else {
			CommonExit(1,"Pulse data not allowed for short blocks");
		}

⌨️ 快捷键说明

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