📄 cntbits.c
字号:
code, /* Huffman code */ i, /* Loop variable */ np, /* Number of voiced pulses */ stuff = 0, /* Accumulated number of stuffed bits */ temp; /* Temporary variable */ static long last_u; /* Last unvoiced frame length coded */ static short last_v; /* Last voiced pulse length coded */ if ( *anafea_rec->frame_type == UNVOICED ) { if ( *anafea_rec->frame_len == uvced_frmlen ) cnt = 0; else if ( !re_sync && prev_type == UNVOICED && last_u == *anafea_rec->frame_len ) cnt = 0; else { code = *anafea_rec->frame_len; cnt = RAW_LEN; if ( debug >= 2 ) debug_print( "LENGTH", (int) *anafea_rec->frame_len, code, cnt, "RAW", dfp ); } if ( cnt ) pack_bits( code, cnt, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); last_u = *anafea_rec->frame_len; bs->frm_len = cnt; bs->frm_bits += cnt + stuff; bs->u_len += cnt; bs->stuff += stuff; return; } np = *anafea_rec->num_pulses; if ( !re_sync && prev_type == VOICED ) huff( auxana_rec->pulse_len_idx[0] - last_v, LENGTH, MAX_LEN, RAW_PULSE, debug, dfp, &code, &cnt ); else { code = auxana_rec->pulse_len_idx[0]; cnt = RAW_PULSE; if ( debug >= 2 ) debug_print( "LENGTH", auxana_rec->pulse_len_idx[0], code, cnt, "RAW", dfp ); } pack_bits( code, cnt, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); for ( i = 1; i < np; i++ ) { huff( auxana_rec->pulse_len_idx[i] - auxana_rec->pulse_len_idx[i-1], LENGTH, MAX_LEN, RAW_PULSE, debug, dfp, &code, &temp ); pack_bits( code, temp, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); cnt += temp; } last_v = auxana_rec->pulse_len_idx[np-1]; bs->frm_len = cnt; bs->frm_bits += cnt + stuff; bs->v_len += cnt; bs->stuff += stuff;}/* ******************************************************************************** * Subroutine to encode spectral parameters. ******************************************************************************** */cnt_spec( anafea_rec, auxana_rec, order_unvcd, order_vcd, u_avg, v_avg, comb_frq, comb_vcg, cont_spc, prev_type, u_vect, v_vect, re_sync, debug, dfp, bs, fea2kb_rec, oh, ofp )FILE *dfp, *ofp;int debug, re_sync, *u_vect, *v_vect;long order_vcd, order_unvcd;short comb_frq, comb_vcg, cont_spc, prev_type, *u_avg, *v_avg;struct auxana *auxana_rec;struct anafea *anafea_rec;struct bit_stats *bs;struct fea2kb *fea2kb_rec;struct header *oh;{ int cnt = 0, /* Bit count */ code, /* Huffman code */ i, /* Loop variable */ offset, /* Offset in type array */ same_type, /* Logical flag */ stuff = 0, /* Accumulated number of stuffed bits */ temp, /* Temporary variable */ *vect; /* Spectral breakdown array */ long order; /* Analysis order */ short *avg, /* Static average values */ *prev_index; /* Previous index array */ static char init = YES; /* Flag for initial entry */ static int *type, /* Array of table types */ type_vf[ 2*MAX_ORD ] = {SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC, SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC,SPEC}, type_0f[ 2*MAX_ORD ] = {VSPEC,VSPEC,VSPEC,VSPEC,VSPEC,VSPEC,VSPEC,VSPEC, VSPEC,VSPEC,USPEC,USPEC,USPEC,USPEC,USPEC,USPEC, USPEC,USPEC,USPEC,USPEC}, type_v0[ 2*MAX_ORD ] = {CF12,OF12,CF34,OF34,CF56,OF56,CF78,OF78,CF90,OF90, CF12,OF12,CF34,OF34,CF56,OF56,CF78,OF78,CF90,OF90}, type_00[ 2*MAX_ORD ] = {VCF12,VOF12,VCF34,VOF34,VCF56,VOF56,VCF78,VOF78, VCF90,VOF90,UCF12,UOF12,UCF34,UOF34,UCF56,UOF56,0,0,0,0}; static short prev_frm_index[ MAX_ORD ], /* Index array - previous frame */ prev_same_index[ MAX_ORD ]; /* Index array - same frame type *//* * On initial entry, set flag false and assign pointer to type array. */ if ( init ) { init = NO; if ( comb_vcg == NO && comb_frq == NO ) type = type_00; else if ( comb_vcg == NO && comb_frq == YES ) type = type_0f; else if ( comb_vcg == YES && comb_frq == NO ) type = type_v0; else if ( comb_vcg == YES && comb_frq == YES ) type = type_vf; } if ( *anafea_rec->frame_type == VOICED ) { offset = 0; order = order_vcd; vect = v_vect; avg = v_avg; } else { offset = MAX_ORD; order = order_unvcd; vect = u_vect; avg = u_avg; } same_type = ( *anafea_rec->frame_type == prev_type ); if ( !re_sync && ( cont_spc == YES || same_type ) ) { if ( same_type ) prev_index = prev_frm_index; else prev_index = prev_same_index; for ( i = 0; i < order; i++ ) { huff( auxana_rec->spec_param_idx[i] - prev_index[i], type[ i + offset ] + INTER, MAX_SPEC, RAW_SPEC, debug, dfp , &code, &temp ); pack_bits( code, temp, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); cnt += temp; vect[i] += temp; } if ( !same_type ) for ( i = 0; i < order; i++ ) prev_same_index[i] = auxana_rec->spec_param_idx[i]; } else for ( i = 0; i < order; i++ ) { huff( auxana_rec->spec_param_idx[i] - avg[i], type[ i + offset ] + INTRA, MAX_SPEC, RAW_SPEC, debug, dfp, &code, &temp ); pack_bits( code, temp, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); cnt += temp; vect[i] += temp; } for ( i = 0; i < order; i++ ) prev_frm_index[i] = auxana_rec->spec_param_idx[i]; bs->frm_spec = cnt; bs->frm_bits += cnt + stuff; if ( *anafea_rec->frame_type == VOICED ) bs->v_spec += cnt; else bs->u_spec += cnt; bs->stuff += stuff;}/* ******************************************************************************** * Subroutine to transmit bogus type code to denote end-of-file. The output * bit buffer is also flushed. Note that stuffed bits are ignored. ******************************************************************************** */code_eof( debug, dfp, fea2kb_rec, oh, ofp )FILE *dfp, *ofp;int debug;struct fea2kb *fea2kb_rec;struct header *oh;{ int code, cnt, stuff; if ( debug >= 2 ) Fprintf( dfp, "\n*** Packing EOF code, flushing buffer:\n" ); huff( -1, TYPE, MAX_TYPE, 0, debug, dfp, &code, &cnt ); pack_bits( code, cnt, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff ); pack_bits( 0, 0, debug, dfp, fea2kb_rec, oh, ofp, NO, &stuff );}/* ******************************************************************************** * Subroutine to look up Huffman code and length. ******************************************************************************** */huff( i, state, max, raw_len, debug, dfp, code, length )FILE *dfp;int *code, debug, i, *length, max, raw_len, state;{ char *mode = NULL; int datum, j, mask; datum = i; j = 0; while ( key_tbl[j].type != state ) j++; i += max; if ( i < 0 ) i = 0; if ( i > 2*max ) i = 2*max; i += key_tbl[j].offset; *code = huff_tbl[i].code; *length = huff_tbl[i].length; if ( huff_tbl[i].type == AMP_ESC ) { mask = 0; for ( j = 1; j <= raw_len; j++ ) mask = mask << 1 | 1; *code = *code << raw_len | ( datum & mask ); *length += raw_len; mode = "HUFF_ESC + RAW"; } if ( debug >= 2 ) debug_print( hist_types[ state ], datum, *code, *length, mode, dfp );}/* ******************************************************************************** * Subroutine to print debug information. ******************************************************************************** */debug_print( state, datum, code, length, mode, dfp )FILE *dfp;char *state, *mode;int code, datum, length;{ Fprintf( dfp, "\tState = %s;", state ); Fprintf( dfp, " Datum = %d;", datum ); Fprintf( dfp, " Code = 0x%x;", code ); Fprintf( dfp, " Length = %d", length ); if ( mode == NULL ) Fprintf( dfp, "\n" ); else Fprintf( dfp, "; Mode = %s\n", mode );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -