⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dap_process.c

📁 ZPAV (H265) PC(X86) demo ZPAV (H265) 是 音视频 压缩解压 协议
💻 C
字号:

/*
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//   Copyright (C) 2006-2008  Beijing,  pengzhen (pengzhenxp@yahoo.com.cn)   //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
*/

#include "dap_demo.h"

static Dap_Functions  m_Dap_Functions = 0 ;

static dap_ctx  m_dap_ctx  = {0} ; 
static avi_context m_write_avi = {0} ; 

static FILE *m_fp_in_a    = 0 ;
static FILE *m_file_out_a = 0 ;
#ifdef __OUT_RAW__
static FILE *m_file_out_raw_a = 0 ;
#endif /* __OUT_RAW__ */

static FILE *m_file_trace_a = 0 ;

/* WAVE */
#define RIFF_SAMPLE_RATE m_dap_ctx.sps 
#define RIFF_CHANNELS  m_dap_ctx.ch 

int m_iWaveFileHeadSize =0, m_iWaveFileSize =0 ;
#include "..\wave\RIFF.c"

/**
** dap functions 
*/

void Get_Dap_Functions( int function_id )
{
	switch( function_id )
	{

	/* Compression */

	default : 
	case DAP_FID_H265P_A8_Compression :  
		m_Dap_Functions = (Dap_Functions)dap_encode_frame ; 
		break ;

	/* Decompression */

	case DAP_FID_H265P_A8_Decompression :  
		m_Dap_Functions = (Dap_Functions)dap_decode_frame ; 
		break ;

	/* Denoise */

	case DAP_FID_Denoise :  
		m_Dap_Functions = (Dap_Functions)dap_denoise  ; 
		break ;

	}

}

/**
** dap functions print
*/

static void Print_Dap_Functions( int function_id )
{

	switch( function_id )
	{
	
	/* Compression */

	case DAP_FID_H265P_A8_Compression : 
		printf( "\n digital audio H265+ (ZPA8) compression \n" ) ;
		break ;
	
	/* Decompression */

	case DAP_FID_H265P_A8_Decompression :  
		printf( "\n digital audio H265+ (ZPA8) decopmress \n" ) ;
		break ;

	/* Denoise */

	case DAP_FID_Denoise :  
		printf( "\n digital audio (sound) denoise \n" ) ;
		break ;

	}

	printf( "\n written by pengzhenxp@yahoo.com.cn \n\n" ) ;

}

/**
** dap open 
*/

static void dap_init( void* dap, int function_id )
{

	if( Dap_Compression(function_id) ) /* dap encoder */
	{
		dap_ctx *lp_dap_enc = (dap_ctx*)(dap) ;

		//lp_dap_enc->frame_number = 0 ;
		lp_dap_enc->m_handle = 0 ; 
		dap_encode_open( lp_dap_enc ) ;
		
	}
	else if( Dap_Decompression(function_id) ) /* dap decoder */
	{
		dap_ctx *lp_dap_dec = (dap_ctx*)(dap) ;
		
		//lp_dap_dec->frame_number = 0 ;
		lp_dap_dec->m_handle = 0 ; 
		dap_decode_open( lp_dap_dec ) ;
		
	}
	else /* dap system */
	{
		dap_ctx* lp_dap = (dap_ctx*)( dap ) ;

		//lp_dap->frame_number = 0 ;
		lp_dap->m_handle = 0 ; 
		//dap_open( lp_dap ) ;
	}

}

/**
** dap close 
*/

static void dap_exit( void* dap, int function_id )
{
	if( Dap_Compression(function_id) ) /* dap encoder */
	{
		dap_ctx *lp_dap_enc = (dap_ctx*)(dap) ;

		dap_encode_close( lp_dap_enc ) ;
		
	}
	else if( Dap_Decompression(function_id) ) /* dap decoder */
	{
		dap_ctx *lp_dap_dec = (dap_ctx*)(dap) ;
		
		dap_decode_close( lp_dap_dec ) ;
		
	}
	else /* dap system */
	{
		dap_ctx* lp_dap = (dap_ctx*)( dap ) ;

		//dap_close( lp_dap ) ;
	}

}

/**
** dap init 
*/

void write_avi_init() 
{

	avi_context * lp_write_avi = & m_write_avi ;
	dap_ctx * lp_dap_ctx = & m_dap_ctx ;  

	memset( lp_write_avi, 0x00, sizeof(avi_context) ) ;

	lp_write_avi->audio_tag = FOURCC_AUDIO ; 
	lp_write_avi->audio_Handler = FOURCC_ZPA8 ; /* */

	lp_write_avi->sample_rate = lp_dap_ctx->sps ;
	lp_write_avi->bit_rate    = lp_dap_ctx->bps ;
	lp_write_avi->channels    = lp_dap_ctx->ch ;

}

/**
** dap read 
*/

static int dap_read_file( dap_ctx* dap, int function_id, unsigned int frame_length )
{

	int f_flag = 0 ;

	if( Dap_Compression(function_id) ) /* Compression */ 
	{
		f_flag = fread( dap->sample, sizeof(char), frame_length,   m_fp_in_a) == frame_length ; 

	}
	else if( Dap_Decompression(function_id) ) /* Decompression */
	{
		f_flag = fread( & dap->stream_length , 1, sizeof(int), m_fp_in_a)  == sizeof(int) ;
		f_flag = fread( dap->stream , 1, dap->stream_length, m_fp_in_a ) == dap->stream_length ;		
	}
	
	return f_flag ;
}

/**
** dap write 
*/

static void dap_write_file( dap_ctx* dap, int function_id, unsigned int frame_length )
{
	
	if( Dap_Compression(function_id) ) /* Compression */ 
	{

#ifdef __OUT_RAW__
		if(m_file_out_raw_a)
		{
			fwrite( & dap->stream_length , 1, sizeof(int), m_file_out_raw_a) ;
			fwrite( dap->stream , 1, dap->stream_length, m_file_out_raw_a ) ;
		}
#endif /* __OUT_RAW__ */

		write_avi_packet( &m_write_avi, m_write_avi.audio_tag, 
			              (char*)(dap->stream)-8 , dap->stream_length ) ;
		
		m_write_avi.frame_number++ ;
		m_write_avi.total_bytes += dap->stream_length ;

	}
	else if( Dap_Decompression(function_id) ) /* Decompression */
	{
		fwrite( dap->sample, sizeof(char), frame_length, m_file_out_a); 

		m_iWaveFileSize += frame_length ; 
	}

}

/**
** dap trace (average) 
*/

static void dap_write_trace_avg( dap_ctx* dap, unsigned int sum_length, 
							     unsigned int frame_length, int process_frames )
{
	
	fprintf( m_file_trace_a, "\n" );
	
	fprintf( m_file_trace_a, "|----------------------------------| \n" ) ;
	fprintf( m_file_trace_a, "|             average              | \n" ) ; 
	fprintf( m_file_trace_a, "|----------------------------------| \n" ) ;
	fprintf( m_file_trace_a, "| bitrate (Kbits/second) |  ratio  | \n" ) ; 
	fprintf( m_file_trace_a, "|------------------------|---------| \n" ) ;
	
#define my_dap_fps ( dap->sps * dap->ch * 2.0 / frame_length ) /* 2.0 = sizeof(short) */
	
	fprintf( m_file_trace_a, "|%11d (kbps)      |%7d  |\n", 
		     (int)( (double)(sum_length)*my_dap_fps*8.0 /(process_frames*1000.0)), 
		     (int)( (double)(process_frames)*frame_length /(sum_length))  ) ;
	
#undef my_dap_fps
	
	fprintf( m_file_trace_a, "|----------------------------------| \n" ) ;  
	
	fprintf( m_file_trace_a, "\n" );
	
}

/**
** dap trace 
*/

static void dap_write_trace( dap_ctx* dap, int function_id, 
							 unsigned int frame_length, int process_frames, int last )
{
	if( Dap_Compression(function_id) ) 
	{
		unsigned int stream_length = dap->stream_length ;
		unsigned int ratio =  stream_length ? (frame_length+ (stream_length>>1))/ stream_length : 0 ;
			
		static unsigned int sum_length = 0 ; 

		if( ! last )
		{
			sum_length += stream_length ;
		}

		if( process_frames == 1 ) 
		{ 
			dap_write_trace_avg( dap, sum_length, frame_length, process_frames ) ; /* average */

			fprintf( m_file_trace_a, "|------------------------------| \n" ) ;
			fprintf( m_file_trace_a, "| frames |    length |   ratio | \n" ) ; 
			//fprintf( m_file_trace_a, "|--------|-----------|---------| \n" ) ;
		}
		
		else if( last )
		{
			fprintf( m_file_trace_a, "|------------------------------| \n" ) ;

			fprintf( m_file_trace_a, "\n" );
			
			fseek( m_file_trace_a, 0, SEEK_SET ) ; /* beginning of file  */

			dap_write_trace_avg( dap, sum_length, frame_length, process_frames ) ; /* average */

			return ;
		}

		fprintf( m_file_trace_a, "|--------|-----------|---------| \n" ) ;

		fprintf(m_file_trace_a, "|%4d    | %8d  |%7d  |\n", process_frames, stream_length, ratio ) ; 
	}
}

/**
** dap process 
*/

static void dap_process( dap_ctx* dap, int function_id, unsigned int frame_length )
{

	int process_frames = 0 ; 

	int64_t qwTotalCycles = 0 ;

	/* read file */

	while( dap_read_file( dap, function_id, frame_length ) )
	{

		int64_t t_start, t_end ;

		/* read cycles */ 
		
		read_cycles( t_start ) ;
		
		/* dap function_id */ 
		
		m_Dap_Functions( dap ) ;
		//if( m_Dap_Functions( dap ) == davp_success ) dap->frame_number++ ;

		/* read cycles */ 
		
		read_cycles( t_end ) ; qwTotalCycles += (int)( t_end - t_start ) ;
		
		/* write file */
		
		dap_write_file( dap, function_id, frame_length ) ;
		
		/* trace and report */ 

		if( !(process_frames % 64 /*dap->sps*/) ) 
			fprintf(stderr, " dap process frames %d \n", process_frames ) ; /* report frames  */

		process_frames ++ ; /* */

		if( m_file_trace_a )
		{
			dap_write_trace( dap, function_id, frame_length, process_frames, 0x00000000 ) ;
		}

		//fprintf(stderr, " dap process frames %d \r", process_frames ) ; /* report frames  */
	}

	if( m_file_trace_a )
	{
		dap_write_trace( dap, function_id, frame_length, process_frames, 0x00000001 ) ;
	}

	/* report */ 
	
	fprintf(stderr, " dap process frames %d \n", process_frames ) ; /* report frames  */

#define ff_dap_fps(X) ((float)(X->sps * X->ch)/DAP_FRAME_SIZE) 

	dap_report ( qwTotalCycles,  process_frames, ff_dap_fps(dap) ) ;

#undef ff_dap_fps /* */

}

/**
** dap main 
*/

static void dap_main(int argc,char **argv)
{	
	char *lpAudioData    = 0 ;
	char* lpAudioOutData = 0 ;

	int function_id  = 0 ;
	int frame_length = 0 ; 

	/* parameter */ 
	
	get_parameter( NULL , &m_dap_ctx , argc,  argv ) ; 

	/* dap function_id */

	function_id  = m_function_id ; 
	Get_Dap_Functions( function_id ) ;
	Print_Dap_Functions( function_id ) ;

	frame_length = DAP_FRAME_SIZE*m_dap_ctx.ch*sizeof(short) ; /*1152*channels*2 __AUDIO_STEREO__ = 1152*4 */
	
	/* file open */ 
	
	m_fp_in_a    = fopen(m_in_file_name,"rb") ;
	if( ! m_fp_in_a ) { printf("can't open file : %s \n", m_in_file_name ) ; return ; }

	m_file_out_a = fopen(m_out_file_name,"wb") ;
	if( ! m_file_out_a ) { printf("can't open file : %s \n", m_out_file_name ) ; return ; }

#ifdef __OUT_RAW__
	if( Dap_Compression( function_id ) )
	m_file_out_raw_a = fopen(m_out_raw_file_name,"wb") ; 
	/* if( ! m_file_out_raw_a ) printf("can't open file : %s \n", m_out_raw_file_name ) ; */
#endif /* __OUT_RAW__ */
		
	/* */
	if( m_trace )
	{
		char file_trace_name[100]; sprintf( file_trace_name, "%s.trace.txt", m_out_file_name ) ;
		m_file_trace_a = fopen(file_trace_name,"w") ; 
		/* if( ! m_file_trace_a ) printf("can't open file : %s \n", file_trace_name ) ; */
	}

	/* dap open */ 
	
	dap_init( &m_dap_ctx, function_id ) ;
	
	/* dap data pointor */ 
		
	lpAudioData    = (char *)dmp_memory_alloc( frame_length*2 + 128 ) ; 
	lpAudioOutData = (char *)dmp_memory_alloc( frame_length*2 + 128 ) ; 
	
	lpAudioData    +=  8 ; /* for write avi head */
	lpAudioOutData +=  8 ; /* for write avi head */
	
	/* dap stream pointor */ 

	m_dap_ctx.sample     = lpAudioData ; 
	m_dap_ctx.stream  = lpAudioOutData ; 

	/* write avi init */
	write_avi_init()  ;

	/* write avi header */
	
	if( Dap_Compression(function_id) ) 
	{
		write_avi_header( &m_write_avi , m_out_file_name )  ; /* Compression */ 
		/*if( m_fp_in_a )*/ fseek(m_fp_in_a, 44 , SEEK_CUR ) ; /* wave head size */
	}

	/* write RIFF header */

	if( Dap_Decompression(function_id) ) 
	{
		write_riff_head( m_file_out_a , 0 ) ; /* Decompression */
	}

/**
** dap process 
*/
	dap_process( &m_dap_ctx, function_id, frame_length ) ; 

	/* write avi trailer */
	
	if( Dap_Compression(function_id) ) 
	{
		/* bit rate */

		if( m_write_avi.frame_number )
		m_write_avi.bit_rate = (m_write_avi.total_bytes<<4)/m_write_avi.frame_number *
			                    m_write_avi.sample_rate / (DAP_FRAME_SIZE<<1) ;

		write_avi_trailer( &m_write_avi , m_out_file_name )  ; /* Compression */
	}

	/* write RIFF trailer */

	if( Dap_Decompression(function_id) ) 
		write_riff_head( m_file_out_a , m_iWaveFileSize ) ; /* Decompression */

	/* dap close */ 
	
	dap_exit( &m_dap_ctx, function_id ) ;
	
	/* dap data pointor */ 

	lpAudioData    -=  8 ; /* for write avi head */
	lpAudioOutData -=  8 ; 

	dmp_memory_free( lpAudioData ) ;  
	dmp_memory_free( lpAudioOutData ) ;  
	
	/* file close */ 
	
	if(m_fp_in_a) fclose(m_fp_in_a);
	if(m_file_out_a) fclose(m_file_out_a);
#ifdef __OUT_RAW__
	if(m_file_out_raw_a) fclose(m_file_out_raw_a);
#endif /* __OUT_RAW__ */

	if(m_file_trace_a) fclose(m_file_trace_a) ;

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -