www.pudn.com > av3dec_20050318.zip > main.c


/* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2004,  Audio Video Coding Standard, Part III 
* 
* This software module was originally developed by 
* edited by 
* 
* DISCLAIMER OF WARRANTY 
* 
* These software programs are available to the users without any 
* license fee or royalty on an "as is" basis. The AVS disclaims 
* any and all warranties, whether express, implied, or statutory, 
* including any implied warranties of merchantability or of fitness 
* for a particular purpose. In no event shall the contributors or  
* the AVS be liable for any incidental, punitive, or consequential 
* damages of any kind whatsoever arising from the use of this program. 
* 
* This disclaimer of warranty extends to the user of this program 
* and user's customers, employees, agents, transferees, successors, 
* and assigns. 
* 
* The AVS does not represent or warrant that the program furnished 
* hereunder are free of infringement of any third-party patents. 
* Commercial implementations of AVS, including shareware, may be 
* subject to royalty fees to patent holders. Information regarding 
* the AVS patent policy is available from the AVS Web site at 
* http://www.avs.org.cn 
* 
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. 
************************************************************************ 
*/ 
 
 
#ifdef _WIN32 
#define WIN32_LEAN_AND_MEAN 
#include  
#define off_t __int64 
#endif 
 
#include  
#include  
#include  
 
#include "AFpar.h" 
 
#include "av3dec.h" 
#include "getcmdarg.h" 
#include "sam_decode.h" 
#include "funcs.h" 
 
/* globals */ 
char *progName; 
 
char *file_ext[] = 
{ 
    NULL, 
    ".wav", 
    ".aif", 
    NULL 
}; 
 
void usage(void) 
{ 
    fprintf(stdout, "\nUsage:\n"); 
    fprintf(stdout, "%s [options] infile.av3\n", progName); 
    fprintf(stdout, "Options(- or /):\n"); 
    fprintf(stdout, " -h or -help Show this help screen.\n"); 
    fprintf(stdout, " -i          Show info about the input file.\n"); 
    fprintf(stdout, " -o X        Set output filename.\n"); 
    fprintf(stdout, " -f X        Set output format. Valid values for X are:\n"); 
    fprintf(stdout, "             1:  Microsoft WAV format(*.wav, default).\n"); 
    fprintf(stdout, "             2:  RAW PCM data(*.aif).\n"); 
    fprintf(stdout, " -b X        Set output sample format. Valid values for X are:\n"); 
    fprintf(stdout, "             1:  16 bit PCM data (default).\n"); 
    fprintf(stdout, "             2:  24 bit PCM data.\n"); 
    fprintf(stdout, "             3:  32 bit PCM data.\n");    
    fprintf(stdout, "             4:  32 bit FLOAT data.\n");    
	fprintf(stdout, " -r X        Set decoding bitrate. Valid values range for X are [16:64].\n"); 
 
    fprintf(stdout, "Example:\n");     
    fprintf(stdout, "       %s infile.av3\n", progName); 
    fprintf(stdout, "       %s -o outfile.wav infile.av3\n", progName); 
    return; 
} 
 
////////////////////////////////////////////////////////////////////  
// 
//  decodeAVSfile():  the main av3 decoding function. 
//  input parameter: 
//		av3file:   input coded audio file(*.av3) name 
//		sndfile:   output file name 
//		def_srate: default decoding bitrate 
//		object_type: object type, now only basic level supported 
//		outputFormat: the output file format(*.wav or *.aif) 
//		SampleFormat: output PCM data format,16bits/24bits/32bits integer or 32bits float 
//		dec_target:  decoding bitrate(16~64) 
//		header_type: header type, now just AV3_HEAD_DEFAULT supported 
//  output parameter: 
//		Zero 
//     
//////////////////////////////////////////////////////////////////// 
int decodeAVSfile(char *av3file, char *sndfile,  
                  int def_srate, int object_type, int outputFormat, int SampleFormat, 
				  int dec_target,int header_type 
				  ) 
{ 
    AV3DecFrameInfoPtr hDecoder; 
    AV3DecCfgPtr config; 
	int bread; 
	int normal; 
    int bitrate = 0; 
	int samplefmt=5; 
 
    av3_buffer b; 
    memset(&b, 0, sizeof(av3_buffer)); 
 
    b.infile = fopen(av3file, "rb"); 
    if (b.infile == NULL) 
    { 
        /* unable to open file */ 
        fprintf(stderr, "Error opening file: %s\n", av3file); 
        return 1; 
    } 
 
    if (!(b.buffer = (unsigned char*)av3_malloc(AV3DEC_MIN_IBUFSIZE*MAX_CHANNELS))) 
    { 
        fprintf(stderr, "Memory allocation error\n"); 
        return 0; 
    } 
    memset(b.buffer, 0, AV3DEC_MIN_IBUFSIZE*MAX_CHANNELS); 
 
    bread = fread(b.buffer, 1, AV3DEC_MIN_IBUFSIZE*MAX_CHANNELS, b.infile); 
    b.bytes_into_buffer = bread; 
    b.bytes_consumed = 0; 
    b.file_offset = 0; 
 
    if (bread != AV3DEC_MIN_IBUFSIZE*MAX_CHANNELS) 
        b.at_eof = 1; 
 
    hDecoder = AV3DecOpen(); 
 
    /* Set the default object type and samplerate */ 
    /* This is useful for RAW av3 files */ 
    config = AV3DecGetCurrentConfiguration(hDecoder); 
    if (def_srate) 
        config->defSampleRate = def_srate; 
    config->av3ObjectType = object_type; 
    config->outputFormat = outputFormat; 
	config->pcmFormat = SampleFormat; 
	config->dec_target = dec_target; 
    normal=AV3DecSetConfiguration(hDecoder, config);     
 
    fill_buffer(&b); 
	/* Check if some error initializing occured */ 
	if ((hDecoder == NULL)||(!normal)) 
	{         
        fprintf(stderr, "Error initializing decoder library.\n"); 
        if (b.buffer) 
            free(b.buffer); 
        AV3DecClose(hDecoder); 
        fclose(b.infile); 
        return 1; 
    } 
 
    /* print av3 file info */ 
	fprintf(stderr, "input file name: \t%s\n", av3file); 
    fprintf(stderr, "%s file info:\n", av3file); 
    switch (header_type) 
    { 
		case 0: 
			fprintf(stderr, "input file format:\t AV3_RAW\n"); 
			break; 
		default: 
			fprintf(stderr, "input file format:\t unknown format\n"); 
			break; 
    } 
	fclose(b.infile); 
 
	/* Open input bitstream file */ 
	sam_openbitstream(av3file); 
 
	switch(SampleFormat){ 
	case AV3_PCM_16BIT: 
		samplefmt = FD_INT16 + 256*2; 
		break; 
	case AV3_PCM_24BIT: 
		samplefmt = FD_INT24 + 256*2; 
		break; 
	case AV3_PCM_32BIT: 
		samplefmt = FD_INT32 + 256*2; 
		break; 
	case AV3_PCM_FLOAT: 
		samplefmt = FD_FLOAT32 + 256*2; 
		break;	 
	default: 
		samplefmt = FD_INT16 + 256*2; 
		break; 
	} 
 
	sam_decode(dec_target, sndfile, samplefmt, stderr, hDecoder); 
 
	sam_closebitstream(); 
 
    AV3DecClose(hDecoder); 
 
    fclose(b.infile); 
 
    if (b.buffer) 
        free(b.buffer); 
 
	return 0; 
} 
 
char  *format_param[]={"1","2","\0"}; 
char  *pcmfmt_param[]={"1","2","3","4","\0"}; 
 
static const cmd_switch  swtArr[] =  
{                                    
    {"f",     3, format_param,   0, 0},   /* output format */ 
    {"b",     3, pcmfmt_param,   0, 1},   /* sample bit format */ 
    {"r",     1, NULL,			 0, 2},   /* decoding bitrate */ 
    {"o",     2, NULL,			 0, 3},   /* output file name */ 
    {"i",     0, NULL,			 0, 4},   /* show input file information */ 
    {"h",     0, NULL,			 0, 6},   /* show help */ 
    {"help",  0, NULL,			 0, 5},   /* show help */ 
    {"\0",    2, NULL,			 0, 7}    /* necessary for ending */ 
}; 
 
int main(int argc, char *argv[])    
{ 
    int result; 
    int infoOnly = 0; 
    int object_type = AV3_BASIC_LEVEL; 
    int def_srate = 0; 
    char *fnp; 
	 
	int header_type = AV3_HEAD_DEFAULT; 
	int nch; 
	int	Sfreq, SfreqIdx; 
 
    int         outfile_set = 0; 
    int         narg; 
    cmd_params  param; 
    cmd_option* option; 
	char output_filename[80];	 
	int temp; 
 
    long begin; 
	progName = argv[0]; 
    fprintf(stderr, "\nAVS Audio decoder release ver1.0.\n"); 
 
    /* begin process command line */ 
	option = av3_malloc(argc*sizeof(cmd_option)); 
    memset(option, 0, argc*sizeof(cmd_option));     
     
    /* set default value */ 
    param.bitrate = 64; 
    param.outformat = 1; 
    param.sampleformat = 1; 
    param.outFile = NULL; 
    param.inFile = NULL; 
    param.showHelp = 0; 
	param.showinfile=0; 
     
    /*parse cmd line and get parameters*/     
    narg = parseCommandLine(option, swtArr, argc, argv); 
    outfile_set = getParam(¶m, option, narg); 
 
    
    /* Print help if requested or no infile */ 
    if ((param.inFile==NULL)||param.showHelp) 
    { 
        usage(); 
        return 1; 
    } 
	/* print av3 file info */ 
	if (param.inFile&¶m.showinfile) 
	{ 
		fprintf(stdout, "input file name: \t%s\n", param.inFile); 
        fprintf(stdout, "%s file info:\n", param.inFile); 
		switch (header_type) 
		{ 
		case AV3_HEAD_DEFAULT: 
			fprintf(stdout, "input file format:\t AV3_RAW\n"); 
			break; 
		default: 
			fprintf(stdout, "input file format:\t unknown format\n"); 
			break; 
		} 
		sam_openbitstream(param.inFile); 
		temp = sam_getbits(32);		 
		nch   = sam_getbits(4);   //3 -> 4 
	    SfreqIdx  = sam_getbits(4); 
	    Sfreq = sam_init_cbc(SfreqIdx); 
		fprintf(stdout,"Number of channels:%d\n",nch); 
		fprintf(stdout,"Sampling frequency:%d\n",Sfreq); 
		sam_closebitstream(); 
        return 0; 
    } 
	 
 
    //begin = GetTickCount(); 
 
    /* set default outfile name */ 
    if(!outfile_set) 
    { 
		strcpy(output_filename, param.inFile);   
 
        fnp = (char *)strrchr(output_filename,'.'); 
 
        if (fnp) 
            fnp[0] = '\0'; 
 
        strcat(output_filename, file_ext[param.outformat]); 
		param.outFile = output_filename; 
    } 
	/* decoding bit stream */ 
	result = decodeAVSfile(param.inFile, param.outFile,  
		def_srate, object_type,  param.outformat,param.sampleformat,  
		param.bitrate,header_type); 
 
 
    /*if (!result && !param.showinfile) 
    { 
        float dec_length = (float)(GetTickCount()-begin)/1000.0; 
        SetConsoleTitle("AV3decoder"); 
    }*/ 
    return 0; 
}