当前位置:编程学习 > C/C++ >>

利用ffmpeg解码h264流的代码

这里也直接给出代码:

h264dec.h:

[cpp] 
#pragma once 
#include "tdll.h" 
#include "avcodec.h" 
#include "postprocess.h" 
//#include "EMVideoCodec.h" 
 
class h264dec /*: public IH264Decoder*/ 

public: 
    virtual bool InitH264Deocder(int width,int height); 
    virtual bool H264Decode(unsigned char * inbuf, const int & inlen,unsigned char * outbuf,int & outlen); 
    virtual void StopH264Decoder(); 
    virtual void ReleaseConnection(); 
 
public: 
    h264dec(void); 
    virtual ~h264dec(void); 
private: 
    Tdll *dll; 
 
    bool LoadDllFun(); 
    void (*avcodec_init)(void); 
    void (*avcodec_register_all)(void); 
    AVCodecContext* (*avcodec_alloc_context)(void); 
    AVFrame* (*avcodec_alloc_frame)(void); 
    AVCodec *(*avcodec_find_decoder)(enum CodecID id); 
    int (*avcodec_decode_video)(AVCodecContext *avctx, AVFrame *picture,int *got_picture_ptr, 
                                uint8_t *buf, int buf_size); 
    int  (*avcodec_open)(AVCodecContext *avctx, AVCodec *codec); 
    int  (*avcodec_close)(AVCodecContext *avctx); 
    void (*av_free)(void *ptr);      
     
    bool InitPostproc(int w,int h); 
    void ClosePostproc(); 
    pp_context_t *(*pp_get_context)(int width, int height, int flags); 
    void (*pp_free_context)(pp_context_t *ppContext); 
    void (*pp_free_mode)(pp_mode_t *mode); 
    pp_mode_t *(*pp_get_mode_by_name_and_quality)(char *name, int quality); 
    void  (*pp_postprocess)(uint8_t * src[3], int srcStride[3], 
                 uint8_t * dst[3], int dstStride[3], 
                 int horizontalSize, int verticalSize, 
                 QP_STORE_T *QP_store,  int QP_stride, 
         pp_mode_t *mode, pp_context_t *ppContext, int pict_type); 
private: 
    AVCodec         *pdec; 
    AVCodecContext  *pdecContext; 
    AVFrame         *pdecFrame; 
    int             m_width; 
    int             m_height; 
 
    Tdll* prodll; 
    pp_context_t *pp_context; 
    pp_mode_t    *pp_mode; 
}; 

h264dec.cpp:
[cpp] view plaincopy
#include "StdAfx.h" 
#include ".\h264dec.h" 
 
h264dec::h264dec(void) 
:dll(NULL) 
,pdec(NULL) 
,pdecContext(NULL) 
,pdecFrame(NULL) 
,pp_context(NULL) 
,pp_mode(NULL) 
,prodll(NULL) 


 
h264dec::~h264dec(void) 


 
bool h264dec::LoadDllFun() 

    dll=new Tdll(L"libavcodec.dll"); 
    dll->loadFunction((void**)&avcodec_init,"avcodec_init"); 
    dll->loadFunction((void**)&avcodec_register_all,"avcodec_register_all"); 
    dll->loadFunction((void**)&avcodec_alloc_context,"avcodec_alloc_context"); 
    dll->loadFunction((void**)&avcodec_alloc_frame,"avcodec_alloc_frame"); 
    dll->loadFunction((void**)&avcodec_find_decoder,"avcodec_find_decoder"); 
    dll->loadFunction((void**)&avcodec_open,"avcodec_open");  
    dll->loadFunction((void**)&avcodec_decode_video,"avcodec_decode_video"); 
    dll->loadFunction((void**)&avcodec_close,"avcodec_close"); 
    dll->loadFunction((void**)&av_free,"av_free"); 
    if (!dll->ok) 
        return false; 
 
    prodll = new Tdll(L"postproc.dll"); 
    prodll->loadFunction((void**)&pp_get_context,"pp_get_context"); 
    prodll->loadFunction((void**)&pp_free_context,"pp_free_context"); 
    prodll->loadFunction((void**)&pp_free_mode,"pp_free_mode"); 
    prodll->loadFunction((void**)&pp_get_mode_by_name_and_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) 

  

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,