LBCCODEC.C
资源名称:G711-729.rar [点击查看]
上传用户:meifeng08
上传日期:2013-06-18
资源大小:5304k
文件大小:8k
源码类别:
语音压缩
开发平台:
C/C++
- /*
- **
- ** File: "lbccodec.c"
- **
- ** Description: Top-level source code for G.723 dual-rate codec
- **
- ** Functions: main
- ** Process_files()
- **
- **
- */
- /*
- ITU-T G.723 Speech Coder ANSI-C Source Code Version 5.00
- copyright (c) 1995, AudioCodes, DSP Group, France Telecom,
- Universite de Sherbrooke. All rights reserved.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "typedef.h"
- #include "basop.h"
- #include "cst_lbc.h"
- #include "tab_lbc.h"
- #include "lbccodec.h"
- #include "coder.h"
- #include "decod.h"
- #include "exc_lbc.h"
- #include "util_lbc.h"
- #include "cod_cng.h"
- #include "dec_cng.h"
- #include "vad.h"
- /* Global variables */
- enum Wmode WrkMode = Both ;
- enum Crate WrkRate = Rate63 ;
- int PackedFrameSize[2] = {
- 24 ,
- 20
- } ;
- Flag UseHp = True ;
- Flag UsePf = True ;
- Flag UseVx = False ;
- Flag UsePr = True ;
- char SignOn[] = "ACL/USH/FT/DSPG ANSI C CODEC ITU LBC Ver 5.00n" ;
- int main( int argc, char *argv[] )
- {
- FILE *Ifp, *Ofp ; /* I/O File pointers */
- FILE *Fep = NULL; /* Frame erasures file pointer */
- FILE *Ratp = NULL; /* Rate file pointer */
- long FrCnt = 0 ;
- long FlLen ;
- Word16 DataBuff[Frame] ;
- Word16 Crc ;
- char Rate_Rd;
- char Line[24] ;
- printf("%s", SignOn ) ;
- /* Process arguments and open I/O files */
- FlLen = Process_Files( &Ifp, &Ofp, &Fep, &Ratp, argc, argv ) ;
- /*
- Init coder and the decoder
- */
- Init_Coder( ) ;
- Init_Decod( ) ;
- /* Init Comfort Noise Functions */
- if( UseVx ) {
- Init_Vad();
- Init_Cod_Cng( );
- }
- Init_Dec_Cng( );
- /* Process all the input file */
- do {
- switch ( WrkMode ) {
- case Both:
- if(Ratp != NULL) {
- fread((char *)&Rate_Rd, sizeof(char), 1, Ratp);
- WrkRate = (enum Crate)Rate_Rd;
- }
- if ( WrkRate == Rate53) reset_max_time();
- Read_lbc( DataBuff, Frame, Ifp ) ;
- Coder( DataBuff, Line ) ;
- Decod( DataBuff, Line, (Word16) 0 ) ;
- Write_lbc( DataBuff, Frame, Ofp ) ;
- break ;
- case Cod :
- if(Ratp != NULL) {
- fread((char *)&Rate_Rd, sizeof(char), 1, Ratp);
- WrkRate = (enum Crate)Rate_Rd;
- }
- if ( WrkRate == Rate53) reset_max_time();
- Read_lbc( DataBuff, Frame, Ifp ) ;
- Coder( DataBuff, Line ) ;
- Line_Wr( Line, Ofp ) ;
- break ;
- case Dec :
- if(Line_Rd( Line, Ifp ) == (-1)) {
- FlLen = FrCnt;
- break;
- }
- if ( Fep == NULL )
- Crc = (Word16) 0 ;
- else
- fread( (char *)&Crc, sizeof(Word16), 1, Fep ) ;
- Decod( DataBuff, Line, Crc ) ;
- Write_lbc( DataBuff, Frame, Ofp ) ;
- break ;
- }
- FrCnt ++ ;
- if( UsePr) {
- if( WrkMode == Dec) {
- if(FrCnt < FlLen) {
- fprintf( stdout, "Done : %6ldr", FrCnt) ;
- }
- }
- else {
- fprintf( stdout, "Done : %6ld %3ldr", FrCnt, FrCnt*100/FlLen ) ;
- }
- fflush(stdout);
- }
- } while ( FrCnt < FlLen ) ;
- if(Ifp) { (void)fclose(Ifp); }
- if(Ofp) { (void)fclose(Ofp); }
- if(Fep) { (void)fclose(Fep); }
- if(Ratp) { (void)fclose(Ratp); }
- return 0 ;
- }
- /*
- This function processes the argument parameters. The function
- opens the IO files, and sets the global arguments accordingly
- to the command line parameters.
- */
- long Process_Files( FILE **Ifp, FILE **Ofp, FILE **Fep, FILE **Ratp,
- int Argc, char *Argv[] )
- {
- int i ;
- long Flen ;
- char *FerFileName = NULL ;
- char *RateFileName = NULL ;
- /*
- Process the argument list, if any
- */
- if (Argc < 3 ) {
- printf("Usage: %s [options] inputfile outputfile n", Argv[0]);
- exit(1);
- }
- for ( i = 1 ; i < Argc-2 ; i ++ ) {
- /* Check the coder rate */
- if ( ! strncmp( "-r", Argv[i], 2) ) {
- if ( ! strcmp("63", Argv[i]+2) ) {
- WrkRate = Rate63 ;
- continue ;
- }
- else if ( ! strcmp("53", Argv[i]+2) ) {
- WrkRate = Rate53 ;
- continue ;
- }
- else {
- RateFileName = &Argv[i][2] ;
- continue ;
- }
- }
- /* Check Working mode */
- if ( ! strcmp("-b", Argv[i]) ) {
- WrkMode = Both ;
- continue ;
- }
- if ( ! strcmp("-c", Argv[i]) ) {
- WrkMode = Cod ;
- continue ;
- }
- if ( ! strcmp("-d", Argv[i]) ) {
- WrkMode = Dec ;
- continue ;
- }
- if ( ! strcmp("-v", Argv[i]) ) {
- UseVx = True ;
- continue ;
- }
- if ( ! strcmp("-Noh", Argv[i]) ) {
- UseHp = False;
- continue ;
- }
- if ( ! strcmp("-Nop", Argv[i]) ) {
- UsePf = False;
- continue ;
- }
- if ( ! strncmp( "-f", Argv[i], 2) ) {
- FerFileName = &Argv[i][2] ;
- continue ;
- }
- if ( ! strcmp("-n", Argv[i]) ) {
- UsePr = False;
- continue ;
- }
- fprintf(stderr, "Illegal argument, %sn", Argv[i] ) ;
- exit(1) ;
- }
- *Ifp = fopen( Argv[Argc-2], "rb") ;
- if ( *Ifp == NULL ) {
- fprintf(stderr, "Invalid input file name: %sn", Argv[Argc-2] ) ;
- exit(1) ;
- }
- if ( UsePr )
- printf("Input file: %sn", Argv[Argc-2] ) ;
- *Ofp = fopen( Argv[Argc-1], "wb") ;
- if ( *Ofp == NULL ) {
- fprintf(stderr, "Can't open output file: %sn", Argv[Argc-1] ) ;
- exit(1) ;
- }
- if ( UsePr )
- printf("Output file: %sn", Argv[Argc-1] ) ;
- /* Open Fer file if required */
- if ( WrkMode == Dec ) {
- if ( FerFileName != NULL ) {
- *Fep = fopen( FerFileName, "rb" ) ;
- if ( *Fep == NULL ) {
- fprintf(stderr, "Can't open FER file: %sn", FerFileName ) ;
- exit(1) ;
- }
- if ( UsePr )
- printf("FER file: %sn", FerFileName ) ;
- }
- }
- else {
- if ( RateFileName != NULL ) {
- *Ratp = fopen( RateFileName, "rb" ) ;
- if ( *Ratp == NULL ) {
- fprintf(stderr, "Can't open Rate file: %sn", RateFileName ) ;
- exit(1) ;
- }
- if ( UsePr )
- printf("Rate file: %sn", RateFileName ) ;
- }
- }
- /* Options report */
- if ( UsePr ) {
- printf("Options:n");
- if (WrkMode == Both )
- printf("Encoder/Decodern");
- else if (WrkMode == Cod )
- printf("Encodern");
- else
- printf("Decodern");
- if( WrkMode != Cod ) {
- if (UsePf == 0 )
- printf("Postfilter disabledn");
- else
- printf("Postfilter enabledn");
- }
- if( WrkMode <= Cod ) {
- if(*Ratp == NULL) {
- if (WrkRate == Rate63 )
- printf("Rate 6.3 kb/sn");
- else
- printf("Rate 5.3 kb/sn");
- }
- if (UseHp == 0 )
- printf("Highpassfilter disabledn");
- else
- printf("Highpassfilter enabledn");
- if (UseVx == 0 )
- printf("VAD/CNG disabledn");
- else
- printf("VAD/CNG enabledn");
- }
- }
- /*
- Compute the file length
- */
- fseek( *Ifp, 0L, SEEK_END ) ;
- Flen = ftell( *Ifp ) ;
- rewind( *Ifp ) ;
- if ( WrkMode == Dec )
- Flen = 0x7fffffffL ;
- else
- Flen /= sizeof(Word16)*Frame ;
- return Flen ;
- }