📄 encode.c
字号:
/* ******************************************************************************** * * This material contains proprietary software of Entropic Speech, Inc. * Any reproduction, distribution, or publication without the the prior * written permission of Entropic Speech, Inc. is strictly prohibited. * Any public distribution of copies of this work authorized in writing * by Entropic Speech, Inc. must bear the notice: * * "Copyright (c) 1987 Entropic Speech, Inc.; all rights reserved" * * Program: encode * * Written by: Jim Elliott * * Produces serial bitstream file and coding statistics for quantized * FEA_ANA file. * ******************************************************************************** *//* * SCCS program and date keywords. */#ifndef lintstatic char *sccs_id = "@(#)encode.c 1.4 11/25/87 ESI";#endif/* * System include files. */#include <stdio.h>/* * ESPS include files. */#include <esps/esps.h>#include <esps/anafea.h>#include <esps/encode.h>#include <esps/fea.h>#include <esps/fea2kb.h>#include <esps/feahuff.h>#include <esps/feaqhist.h>#include <esps/feaquant.h>/* * Defines. */#define ERROR_EXIT( text ) \{ \ Fprintf( stderr, "encode: %s - exiting\n", text ); \ exit( 1 ); \}#define SYNTAX USAGE \( \ "encode -D debug_file -P param_file -d -h hist_file -x debug_level infile.fea\ huffile.fea outfile.fea" \)/* * System functions and variables. */char *strcpy();int atoi(), getopt(), strcmp();void exit(), perror();extern optind;extern char *optarg;/* * ESPS functions and variables. */char *get_cmd_line();struct zfunc *get_genzfunc();/* * Internal functions and variables. */struct tbl_entry huff_tbl[ MAX_TBL*MAX_TBLSIZ ]; /* Concatenated Huffman tables */struct key key_tbl[ MAX_TBL ]; /* Table of offsets in big table *//* ******************************************************************************** * Main program. ******************************************************************************** */main( argc, argv )char *argv[];int argc;{ char *cmd_line, /* String for command line */ *date = "11/25/87", *dfn = NULL, /* Debug file name */ dump = NO, /* Flag to dump frame statistics */ *hfn = "encode.his", /* History file name */ *ifn = NULL, /* Input file name */ *lsf_quant, /* LSF quantization method */ *ofn = NULL, /* Output file name */ *pfn = "params", /* Parameter file name */ *pitch_quant, /* Pitch quantization method */ *power_quant, /* Power quantization method */ *tfn = NULL, /* Huffman table file name */ *version = "1.4"; FILE *dfp = stderr, /* Debug file pointer */ *hfp = NULL, /* History file pointer */ *ifp = stdin, /* Input file pointer */ *ofp = stdout, /* Output file pointer */ *tfp = NULL; /* Huffman table file pointer */ float chan_rate, /* Channel rate */ samp_freq; /* Sample frequency */ int c, /* For getopt() return */ debug = 0, /* Flag to control debug output */ frm_cnt = 0, /* Frame counter */ i, /* Loop variable */ j, /* Loop variable */ max_delay = 0, /* Maximum buffer delay */ re_sync, /* Frame re-synchronization flag */ sync_cnt = 1, /* Sync down-counter */ sync_intv, /* Frame-sync interval */ sync_len, /* Sync length: 0111...10 */ tim = 0, /* Elapsed time */ type = NONE, /* Type of Huffman table */ uvced_frmlen; /* Standard unvoiced frame length */ long order_vcd, /* Voiced filter order */ order_unvcd; /* Unvoiced filter order */ short *avg, /* Pointer to LSF average in header */ comb_frq, /* Flag for combined frequency tables */ comb_vcg, /* Flag for combined voicing tables */ cont_pwr, /* Flag for continuous coding of power */ cont_spc, /* Flag for continuous coding of spectrum */ err_rate = 0, /* Channel error rate */ max_steps, /* Clipping range in 1.5 dB power table */ prev_type = NONE, /* Previous frame type */ u_avg[ MAX_ORD ], /* Unvoiced LSF averages */ unvoiced_steps, /* Steps per LSF octave (unvoiced) */ v_avg[ MAX_ORD ], /* Voiced LSF averages */ voiced_steps; /* Steps per LSF octave (voiced) */ struct anafea *anafea_rec; /* FEA_ANA record */ struct auxana *auxana_rec; /* Auxiliary FEA_ANA record */ struct auxqhist *auxqhist_rec; /* Auxiliary FEA_QHIST record */ struct fea2kb *fea2kb_rec; /* FEA_2KB record */ struct feaqhist *feaqhist_rec; /* FEA_QHIST record */ struct header *ih, /* Input file header */ *oh; /* Output file header */ static struct bit_stats bs; /* Bit statistics: static => {0,...} */ static int buf_hist[11], /* Histogram of buffer delay */ u_vect[ MAX_ORD ], /* Array for unvoiced spectral breakdown */ v_vect[ MAX_ORD ]; /* Array for voiced spectral breakdown *//* * Read command line and process command line options. */ cmd_line = get_cmd_line( argc, argv ); while ( ( c = getopt( argc, argv, "D:P:dh:x:" ) ) != EOF ) { switch ( c ) { case 'D': dfn = optarg; break; case 'P': pfn = optarg; break; case 'd': dump = YES; break; case 'h': hfn = optarg; break; case 'x': debug = atoi( optarg ); break; default: SYNTAX; } }/* * Process file arguments. */ if ( optind < argc ) { ifn = argv[ optind++ ]; if ( strcmp( ifn, "-" ) == 0 ) ifn = "<stdin>"; else TRYOPEN( argv[0], ifn, "r", ifp ); } else { Fprintf( stderr, "encode: No input file specified\n" ); SYNTAX; } if ( optind < argc ) { tfn = argv[ optind++ ]; if ( strcmp( tfn, "-" ) == 0 ) { ERROR_EXIT( "<stdin> not allowed for Huffman table file" ); } else TRYOPEN( argv[0], tfn, "r", tfp ); } else { Fprintf( stderr, "encode: No Huffman table file specified\n" ); SYNTAX; } if ( optind < argc ) { ofn = argv[ optind++ ]; if ( strcmp( ofn, "-" ) == 0 ) ofn = "<stdout>"; else TRYOPEN( argv[0], ofn, "w", ofp ); } else { Fprintf( stderr, "encode: No output file specified\n" ); SYNTAX; } if ( dfn ) TRYOPEN( argv[0], dfn, "w", dfp ); TRYOPEN( argv[0], hfn, "w", hfp );/* * Read parameter file. */ if ( read_params( pfn, SC_NOCOMMON, NULL ) != 0 ) ERROR_EXIT( "Error reading parameter file" ); chan_rate = (float) getsym_i( "chan_rate" ); sync_intv = getsym_i( "sync_intv" ); sync_len = getsym_i( "sync_len" ); symerr_exit();/* * Read and check header of Huffman table file, allocate storage for FEA_QHIST * and auxiliary records, and read in Huffman code tables. */ if ( ( ih = read_header( tfp ) ) == NULL ) NOTSPS( argv[0], tfn ); if ( ih->common.type != FT_FEA ) ERROR_EXIT( "Huffman table file is not a FEA file" ); if ( ih->hd.fea->fea_type != FEA_QHIST ) ERROR_EXIT( "Huffman table file is not FEA_QHIST type" ); comb_vcg = *(short *) get_genhd( "comb_vcg", ih ); comb_frq = *(short *) get_genhd( "comb_frq", ih ); cont_pwr = *(short *) get_genhd( "cont_pwr", ih ); cont_spc = *(short *) get_genhd( "cont_spc", ih ); lsf_quant = get_genhd( "lsf_quant", ih ); max_steps = *(short *) get_genhd( "max_steps", ih ); pitch_quant = get_genhd( "pitch_quant", ih ); power_quant = get_genhd( "power_quant", ih ); unvoiced_steps = *(short *) get_genhd( "unvoiced_steps", ih ); voiced_steps = *(short *) get_genhd( "voiced_steps", ih ); avg = (short *) get_genhd( "u_avg", ih ); for ( i = 0; i < MAX_ORD; i++ ) u_avg[i] = avg[i]; avg = (short *) get_genhd( "v_avg", ih ); for ( i = 0; i < MAX_ORD; i++ ) v_avg[i] = avg[i]; feaqhist_rec = allo_feaqhist_rec( ih ); auxqhist_rec = allo_auxqhist_rec( ih, feaqhist_rec ); i = 0; j = 0; while ( get_feaqhist_rec( feaqhist_rec, ih, tfp ) != EOF ) { if ( type != *feaqhist_rec->hist_type ) { key_tbl[j].type = *feaqhist_rec->hist_type; key_tbl[j].offset = i; j++; } type = *feaqhist_rec->hist_type; huff_tbl[i].type = *feaqhist_rec->data_type; huff_tbl[i].code = *auxqhist_rec->code; huff_tbl[i].length = *auxqhist_rec->length; i++; }/* * Read and check values from header of quantized FEA_ANA file. */ if ( ( ih = read_header( ifp ) ) == NULL ) NOTSPS( argv[0], ifn ); if ( ih->common.type != FT_FEA ) ERROR_EXIT( "Input file is not a FEA file" ); if ( ih->hd.fea->fea_type != FEA_ANA ) ERROR_EXIT( "Input file is not FEA_ANA type" ); if ( get_genhd( "lsf_quant", ih ) == NULL || get_genhd( "pitch_quant", ih ) == NULL || get_genhd( "power_quant", ih ) == NULL ) ERROR_EXIT( "Input file is not quantized" ); if ( strcmp( lsf_quant, get_genhd( "lsf_quant", ih ) ) || strcmp( pitch_quant, get_genhd( "pitch_quant", ih ) ) || strcmp( power_quant, get_genhd( "power_quant", ih ) ) || unvoiced_steps != *(short *) get_genhd( "unvoiced_steps", ih ) || voiced_steps != *(short *) get_genhd( "voiced_steps", ih ) ) ERROR_EXIT( "Inconsistent quantization: infile.fea, huffile.fea" ); if ( strcmp( power_quant, "1.5_dB" ) == 0 ) if ( max_steps != *(short *) get_genhd( "max_steps", ih ) ) ERROR_EXIT( "Inconsistent power quantization: infile.fea, huffile.fea" ); order_unvcd = *(long *) get_genhd( "order_unvcd", ih ); order_vcd = *(long *) get_genhd( "order_vcd", ih ); samp_freq = *(float *) get_genhd( "src_sf", ih ); uvced_frmlen = *(int *) get_genhd( "uvced_frmlen", ih );/* * Create header for output file. */ oh = new_header( FT_FEA ); add_source_file( oh, ifn, ih ); (void) strcpy( oh->common.prog, "encode" ); (void) strcpy( oh->common.vers, version ); (void) strcpy( oh->common.progdate, date ); add_comment( oh, cmd_line ); oh->common.tag = NO; oh->variable.refer = ifn;/* * Allocate record fields, add generic header items, and write output file * header. */ if ( init_fea2kb_hd( oh, "NO", get_genhd( "avg_power", ih ) ? get_genhd( "avg_power", ih ) : "N/A", lsf_quant, pitch_quant, power_quant, get_genzfunc( "pre", ih ), get_genhd( "preemp_uv", ih ), u_avg, v_avg ) ) ERROR_EXIT( "Error filling FEA_2KB header" ); *(float *) get_genhd( "chan_rate", oh ) = chan_rate; *(short *) get_genhd( "comb_vcg", oh ) = comb_vcg; *(short *) get_genhd( "comb_frq", oh ) = comb_frq; *(short *) get_genhd( "cont_pwr", oh ) = cont_pwr; *(short *) get_genhd( "comb_spc", oh ) = cont_spc; *(short *) get_genhd( "err_rate", oh ) = err_rate; *(short *) get_genhd( "frmlen", oh ) = *(short *) get_genhd( "frmlen", ih ); *(short *) get_genhd( "max_steps", oh ) = max_steps; *(long *) get_genhd( "maxlpc", oh ) = *(long *) get_genhd( "maxlpc", ih ); *(long *) get_genhd( "maxpulses", oh ) = *(long *) get_genhd( "maxpulses", ih ); *(long *) get_genhd( "maxraw", oh ) = *(long *) get_genhd( "maxraw", ih ); *(short *) get_genhd( "n_uv_pieces", oh ) = get_genhd( "n_uv_pieces", ih ) ? *(short *) get_genhd( "n_uv_pieces", ih ) : -1; *(long *) get_genhd( "nan", oh ) = *(long *) get_genhd( "nan", ih ); *(long *) get_genhd( "order_unvcd", oh ) = order_unvcd; *(long *) get_genhd( "order_vcd", oh ) = order_vcd; *(short *) get_genhd( "psynch", oh ) = *(short *) get_genhd( "psynch", ih ); *(short *) get_genhd( "sinc_flg", oh ) = *(short *) get_genhd( "sinc_flg", ih ); *(short *) get_genhd( "spec_rep", oh ) = *(short *) get_genhd( "spec_rep", ih ); *(float *) get_genhd( "src_sf", oh ) = samp_freq; *(long *) get_genhd( "start", oh ) = *(long *) get_genhd( "start", ih ); *(short *) get_genhd( "sync_intv", oh ) = (short) sync_intv; *(short *) get_genhd( "sync_len", oh ) = (short) sync_len; *(short *) get_genhd( "unvoiced_steps", oh ) = unvoiced_steps; *(long *) get_genhd( "uvced_frmlen", oh ) = (long) uvced_frmlen; *(short *) get_genhd( "voiced_steps", oh ) = voiced_steps; write_header( oh, ofp );/* * Allocate storage for input and output data records. */ anafea_rec = allo_anafea_rec( ih ); auxana_rec = allo_auxana_rec( ih, anafea_rec ); fea2kb_rec = allo_fea2kb_rec( oh );/* * Main program loop. */ print_header( dump, hfp, ifn, tfn, ofn, version, date, chan_rate, comb_frq, comb_vcg, cont_pwr, cont_spc, sync_intv, sync_len ); while ( get_anafea_rec( anafea_rec, ih, ifp ) != EOF ) { tim += *anafea_rec->frame_len; if ( debug >= 1 ) { frm_cnt++; Fprintf( dfp, "\nFrame = %d; Tag = %d; Type = %s\n", frm_cnt, *anafea_rec->tag, frame_types[ *anafea_rec->frame_type ] ); } if ( sync_intv ) sync_cnt--; if ( sync_cnt == 0 ) { sync_cnt = sync_intv; cnt_sync( sync_len, debug, dfp, NO, &bs, fea2kb_rec, oh, ofp ); bs.frm_bits = sync_len; re_sync = YES; } else { bs.frm_bits = 0; re_sync = NO; } cnt_type_ext( anafea_rec, prev_type, uvced_frmlen, re_sync, debug, dfp, &bs, fea2kb_rec, oh, ofp ); cnt_pwr( anafea_rec, auxana_rec, cont_pwr, prev_type, re_sync, debug, dfp, &bs, fea2kb_rec, oh, ofp ); cnt_len( anafea_rec, auxana_rec, prev_type, uvced_frmlen, re_sync, debug, dfp, &bs, fea2kb_rec, oh, ofp ); 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 ); bs.tot += bs.frm_bits; if ( debug >= 1 ) Fprintf( dfp, "Bits this frame = %d; Cumulative bits = %d\n", bs.frm_bits, bs.tot ); update_buffer( &bs, buf_hist, &max_delay, samp_freq, chan_rate, anafea_rec ); if ( dump ) print_frm_stats( bs, samp_freq, anafea_rec, prev_type, hfp ); prev_type = *anafea_rec->frame_type; } if ( sync_intv ) sync_cnt--; if ( sync_cnt == 0 ) cnt_sync( sync_len, debug, dfp, YES, &bs, fea2kb_rec, oh, ofp ); code_eof( debug, dfp, fea2kb_rec, oh, ofp ); print_cum_stats( bs, u_vect, v_vect, order_unvcd, order_vcd, tim, samp_freq, buf_hist, max_delay, hfp ); exit( 0 );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -