📄 writeavi.cpp
字号:
/*
///////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2006-2008 Beijing, pengzhen (pengzhenxp@yahoo.com.cn) //
// //
///////////////////////////////////////////////////////////////////////////////
*/
#define FUNCC_HANDLER_VIDEO (FOURCC_ZPV8) /* fccHandler */
#define FUNCC_HANDLER_AUDIO (FOURCC_ZPA8) /* fccHandler */
void CH265VView::Write_AVI_Init_Video( )
{
AVISTREAMINFO strhdr0 ;
AVISTREAMINFO* strhdr= &strhdr0 ;
BITMAPINFOHEADER bfh0 ;
BITMAPINFOHEADER* bfh = &bfh0 ;
HRESULT hr ;
int dwScale = (DWORD)(1000000./m_dvp_ctx.fps + 0.5);
memset( strhdr , 0x00, sizeof(AVISTREAMINFO) ); /* zero */
memset( bfh , 0x00, sizeof(BITMAPINFOHEADER) );
#define Write_AVI_Init_Video0() { \
bfh->biSize = sizeof(BITMAPINFOHEADER); \
bfh->biWidth = m_dvp_ctx.width ; \
bfh->biHeight= m_dvp_ctx.height ; \
bfh->biPlanes= 1 ; \
bfh->biBitCount= 32; /*RGB32*/\
bfh->biCompression = FUNCC_HANDLER_VIDEO ;}
// init
Write_AVI_Init_Video0() ;
// Fill in the header for the video stream....
SetRect( &(strhdr->rcFrame), 0, 0, (int)bfh->biWidth, (int)bfh->biHeight );
strhdr->fccType = streamtypeVIDEO;// stream type
strhdr->fccHandler = FUNCC_HANDLER_VIDEO;
strhdr->dwScale = dwScale;
strhdr->dwRate = 1000000 ;
strhdr->dwSuggestedBufferSize = m_dvp_ctx.width * m_dvp_ctx.height * 4 ;
// And create the stream;
hr = AVIFileCreateStream( m_pAVI, &m_ps_Video, strhdr ) ;
if (hr != AVIERR_OK)
{
/*PRINTF_ERROR( "Error : AVIFileCreateStream " );*/
return ;
}
// stream format
hr = AVIStreamSetFormat( m_ps_Video, 0, bfh, sizeof(BITMAPINFOHEADER) ) ;
if (hr != AVIERR_OK)
{
/*PRINTF_ERROR( "Error : AVIStreamSetFormat " );*/
return ;
}
m_ps_av_flag = TRUE ;
}
void CH265VView::Write_AVI_Init_Audio( )
{
AVISTREAMINFO strhdr0 ;
AVISTREAMINFO* strhdr= &strhdr0 ;
WAVEFORMATEX wfx0 ;
WAVEFORMATEX* wfx= &wfx0 ;
HRESULT hr;
int sampsize = 1 ;
int byte_rate = (m_dap_ctx.bps)/8 ; /* bit_rate */
memset( strhdr , 0x00, sizeof(AVISTREAMINFO) ); /* zero */
memset( wfx , 0x00, sizeof(WAVEFORMATEX) );
#define Write_AVI_Init_Audio0() { \
wfx->cbSize = sizeof(wfx); \
wfx->wFormatTag = FUNCC_HANDLER_AUDIO ; \
wfx->nChannels = m_dap_ctx.ch ; \
wfx->nSamplesPerSec= m_dap_ctx.sps ; \
wfx->nAvgBytesPerSec= sampsize*byte_rate ; \
wfx->nBlockAlign= sampsize; \
wfx->wBitsPerSample= 16; }
// init
Write_AVI_Init_Audio0() ;
// Fill in the header for the Audio stream....
strhdr->fccType = streamtypeAUDIO; // stream type
strhdr->fccHandler = FUNCC_HANDLER_AUDIO;
strhdr->dwScale = sampsize ;
strhdr->dwRate = sampsize*byte_rate ;
strhdr->dwSuggestedBufferSize = DAP_FRAME_SIZE*sizeof(short)*m_dap_ctx.ch ; /* sizeof(short) */
strhdr->dwQuality = (int)-1 ;
strhdr->dwSampleSize = sampsize ;
// And create the stream;
hr = AVIFileCreateStream( m_pAVI, &m_ps_Audio, strhdr ) ;
if (hr != AVIERR_OK)
{
/*PRINTF_ERROR( "Error : AVIFileCreateStream " );*/
return ;
}
// stream format
hr = AVIStreamSetFormat( m_ps_Audio, 0, wfx, sizeof(WAVEFORMATEX) );
if (hr != AVIERR_OK)
{
/*PRINTF_ERROR( "Error : AVIStreamSetFormat " );*/
return ;
}
m_ps_av_flag = TRUE ;
}
void CH265VView::Write_AVI_Init_System( BOOL bIsVideo, BOOL bIsAudio )
{
HRESULT hr;
UINT iMode = OF_READWRITE | OF_CREATE | OF_SHARE_DENY_NONE ;
m_ps_av_flag = FALSE ;
//
// Open the movie file for writing....
//
hr = AVIFileOpen( &m_pAVI, avi_filename, iMode, NULL) ;
if (hr != AVIERR_OK)
{
/*PRINTF_ERROR( "Error : AVIFileOpen " );*/
return ;
}
//if ( m_pAVI )
{
if(bIsVideo) Write_AVI_Init_Video( ) ;
if(bIsAudio) Write_AVI_Init_Audio( ) ;
}
}
void CH265VView::Write_AVI_Finish_System( )
{
if (m_ps_Audio) AVIStreamClose(m_ps_Audio);
if (m_ps_Video) AVIStreamClose(m_ps_Video);
if (m_pAVI) AVIFileClose(m_pAVI);
if ( m_ps_Audio && m_dwAudioFrames ) Write_AVI_Set_Audio( ) ;
m_ps_av_flag = FALSE ;
}
void CH265VView::Write_AVI_Set_Audio(char* lpBuffer0, int cbBuffer )
{
char* lpBuffer =lpBuffer0 ;
int i = cbBuffer ;
int stream_audio = 0 ;
int sampsize = 1 ;
int byte_rate = ( (m_dwAudioBytes<<4)/(m_dwAudioFrames) *
m_dap_ctx.sps / (DAP_FRAME_SIZE<<1) ) >> 3 ; /* bit_rate */
while( i -- )
{
if( ! stream_audio )
{
if( streamtypeAUDIO == ((DWORD*)(lpBuffer))[0] )
{
AVISTREAMINFO* strhdr = (AVISTREAMINFO*)lpBuffer ;
strhdr->dwScale = sampsize ;
strhdr->dwRate = sampsize*byte_rate ;
stream_audio = 1 ; /* audio */
lpBuffer += ((DWORD*)(lpBuffer-4))[0] ; /* next */
}
}
else
{
if( FUNCC_HANDLER_AUDIO == ((WORD*)lpBuffer)[0] )
{
WAVEFORMATEX * wfx = (WAVEFORMATEX*)lpBuffer ;
if( (wfx->cbSize == sizeof(wfx)) &&
(wfx->wBitsPerSample == 16 || wfx->wBitsPerSample == 8 ) )
{
wfx->nAvgBytesPerSec= sampsize*byte_rate ;
}
break ;
}
}
lpBuffer ++ ; /* next */
}
}
void CH265VView::Write_AVI_Set_Audio( )
{
CFile lpFile( avi_filename, CFile::modeReadWrite | CFile::shareDenyNone | CFile::typeBinary );
if( ! lpFile.m_hFile ) return ;
char lpBuffer[AVI_HEADERSIZE+32] ;
int cbBuffer = AVI_HEADERSIZE ;
lpFile.Read(lpBuffer, cbBuffer ); /* read */
Write_AVI_Set_Audio(lpBuffer, cbBuffer ); /* set */
lpFile.SeekToBegin(); /* seek */
lpFile.Write(lpBuffer, cbBuffer ); /* write */
lpFile.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -