利用ffmpeg解码h264流的代码(二)

2014-11-24 11:43:29 · 作者: · 浏览: 7
_quality,"pp_get_mode_by_name_and_quality");
prodll->loadFunction((void**)&pp_postprocess,"pp_postprocess");
if(!prodll->ok)
return false;

avcodec_init();
avcodec_register_all();
return true;
}

bool h264dec::InitH264Deocder(int width,int height)
{
if(!LoadDllFun())
return false;
if(!InitPostproc(width,height))
return false;

m_width=width;
m_height=height;
pdec = avcodec_find_decoder(CODEC_ID_H264);
if (pdec == NULL )
return false;

pdecContext = avcodec_alloc_context();
pdecFrame = avcodec_alloc_frame();

pdecContext->width = width;
pdecContext->height = height;
pdecContext->pix_fmt = PIX_FMT_YUV420P;
/* open it */
if (avcodec_open(pdecContext, pdec) < 0)
{
return false;
}
return true;
}

bool h264dec::InitPostproc(int w,int h)
{
int i_flags = 0;
i_flags |= PP_CPU_CAPS_MMX | PP_CPU_CAPS_MMX2 | PP_FORMAT_420;
pp_context = pp_get_context( w, h, i_flags );
if(!pp_context)
return false;
pp_mode = pp_get_mode_by_name_and_quality( "default", 6 );
if(!pp_mode)
return false;
return true;
}

bool h264dec::H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen)
{
int got_frame;
BYTE* showImage[3];
int showheight[3],showLx[3];

int len;
len=avcodec_decode_video(pdecContext, pdecFrame, &got_frame, inbuf, inlen);
if(len < 0)
return false;

if(got_frame)
{
showImage[0]=outbuf;
showImage[1]=showImage[0]+m_width*m_height;
showImage[2]=showImage[1]+m_width*m_height/4;
showLx[0]=m_width;showLx[1]=m_width>>1;showLx[2]=m_width>>1;
showheight[0]=m_height;showheight[1]=m_height>>1;showheight[2]=m_height>>1;
pp_postprocess(pdecFrame->data,pdecFrame->linesize,showImage,showLx,m_width,m_height,pdecFrame->qscale_table,
pdecFrame->qstride,pp_mode,pp_context,pdecFrame->pict_type);
//GetImage( pdecFrame->data,
// showImage,
// pdecFrame->linesize,
// showLx,
// showheight);
outlen=m_width*m_height*3/2;
}
else
{
outlen = 0;
}

return true;
}

void h264dec::StopH264Decoder()
{
if (pdecContext != NULL)
{
avcodec_close(pdecContext);
av_free( pdecContext );
pdecContext = NULL;
if(pdecFrame){
av_free(pdecFrame);
pdecFrame = NULL;
}
}
if(dll){
delete dll;
dll=0;
}

ClosePostproc();
}

void h264dec::ClosePostproc()
{
if(pp_mode){
pp_free_mode( pp_mode );
pp_mode=0;
}
if(pp_context){
pp_free_context(pp_context);
pp_context=0;
}
if(prodll){
delete prodll;
prodll=0;
}
}

void h264dec::ReleaseConnection()
{
delete this;
}

tdll.h:
[cpp]
#ifndef _TDLL_
#define _TDLL_

class Tdll
{
private:
HMODULE hdll;
void loadDll(const char *dllName);
public:
bool ok;
Tdll(const TCHAR *dllName1)
{
hdll=LoadLibrary(dllName1);
if (!hdll)
{
hdll=NULL;
}
ok=(hdll!=NU