www.pudn.com > av3dec_20050318.zip > decoder.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. 
************************************************************************ 
*/ 
 
#include  
#include  
 
#include "common.h" 
#include "av3dec.h" 
#include "funcs.h" 
 
int fill_buffer(av3_buffer *b) 
{ 
    int bread; 
 
    if (b->bytes_consumed > 0) 
    { 
        if (b->bytes_into_buffer) 
        { 
            memmove((void*)b->buffer, (void*)(b->buffer + b->bytes_consumed), 
                b->bytes_into_buffer*sizeof(unsigned char)); 
        } 
 
        if (!b->at_eof) 
        { 
            bread = fread((void*)(b->buffer + b->bytes_into_buffer), 1, 
                b->bytes_consumed, b->infile); 
 
            if (bread != b->bytes_consumed) 
                b->at_eof = 1; 
 
            b->bytes_into_buffer += bread; 
        } 
 
        b->bytes_consumed = 0; 
 
        if (b->bytes_into_buffer > 3) 
        { 
            if (memcmp(b->buffer, "TAG", 3) == 0) 
                b->bytes_into_buffer = 0; 
        } 
        if (b->bytes_into_buffer > 11) 
        { 
            if (memcmp(b->buffer, "LYRICSBEGIN", 11) == 0) 
                b->bytes_into_buffer = 0; 
        } 
        if (b->bytes_into_buffer > 8) 
        { 
            if (memcmp(b->buffer, "APETAGEX", 8) == 0) 
                b->bytes_into_buffer = 0; 
        } 
    } 
 
    return 1; 
} 
 
void advance_buffer(av3_buffer *b, int bytes) 
{ 
    b->file_offset += bytes; 
    b->bytes_consumed = bytes; 
    b->bytes_into_buffer -= bytes; 
} 
 
AV3DecFrameInfoPtr  AV3DecOpen(void) 
{ 
    AV3DecFrameInfoPtr hDecoder = NULL; 
 
    if ((hDecoder = (AV3DecFrameInfoPtr)av3_malloc(sizeof(AV3DecFrameInfo))) == NULL) 
        return NULL; 
 
    memset(hDecoder, 0, sizeof(AV3DecFrameInfo)); 
 
    hDecoder->config.outputFormat  = AV3_PCM_16BIT; 
	hDecoder->config.av3ObjectType = AV3_BASIC_LEVEL;	/* now only basic level*/ 
	hDecoder->config.header_type = AV3_HEAD_DEFAULT; 
    hDecoder->config.defSampleRate = 44100;             /* Default: 44.1kHz */  
    hDecoder->frameLength = 1024; 
    hDecoder->frame = 0; 
    hDecoder->sample_buffer = NULL; 
    return hDecoder; 
} 
 
AV3DecCfgPtr  AV3DecGetCurrentConfiguration(AV3DecFrameInfoPtr hDecoder) 
{ 
    if (hDecoder) 
    { 
        AV3DecCfgPtr config = &(hDecoder->config); 
        return config; 
    } 
    return NULL; 
} 
 
uint8_t  AV3DecSetConfiguration(AV3DecFrameInfoPtr hDecoder,AV3DecCfgPtr config) 
{ 
    if (hDecoder && config) 
    { 
        /* check if we can decode this object type */ 
		if(config->av3ObjectType!=AV3_BASIC_LEVEL) 
		{ 
			fprintf(stdout,"Object type error!\n"); 
			return 0; 
		} 
        hDecoder->config.av3ObjectType = config->av3ObjectType; 
		hDecoder->object_type = hDecoder->config.av3ObjectType; 
 
        /* samplerate: anything but 0 should be possible */ 
        if (config->defSampleRate == 0) 
		{ 
			fprintf(stdout,"Object type error!\n"); 
			return 0; 
		} 
        hDecoder->config.defSampleRate = config->defSampleRate; 
 
        /* check output file format */ 
        if ((config->outputFormat < 1) || (config->outputFormat > 2)) 
   		{ 
			fprintf(stdout,"Output file format error!\n"); 
			return 0; 
		} 
        hDecoder->config.outputFormat = config->outputFormat; 
 
		/* check output pcm data format */ 
        if ((config->pcmFormat < 1) || (config->pcmFormat > 4)) 
   		{ 
			fprintf(stdout,"Output pcm data format error!\n"); 
			return 0; 
		} 
        hDecoder->config.pcmFormat = config->pcmFormat; 
		 
		/* check decoding bitrate */ 
        if((config->dec_target < 16) || (config->dec_target > 64)) 
		{ 
			fprintf(stdout,"Decoding bitrate error!\n"); 
			return 0; 
		} 
        hDecoder->config.dec_target = config->dec_target; 
 
		/* check header type */ 
		if( config->header_type != AV3_HEAD_DEFAULT) 
		{ 
			fprintf(stdout,"Header type error!\n"); 
			return 0; 
		} 
		hDecoder->config.header_type = config->header_type; 
 
        /* OK */ 
        return 1; 
    } 
    return 0; 
} 
 
void  AV3DecClose(AV3DecFrameInfoPtr hDecoder) 
{ 
    if (hDecoder == NULL) 
        return; 
 
    if (hDecoder->sample_buffer) av3_free(hDecoder->sample_buffer); 
    if (hDecoder) av3_free(hDecoder); 
}