.H文件如下:
/*
视频解码
*/
#ifndef __BLL_AUTELVIDEODECODE_H
#define __BLL_AUTELVIDEODECODE_H
#ifdef AUTELVIDEODECODE_EXPORTS
#define AUTELVIDEODECODE_API __declspec(dllexport)
#else
#define AUTELVIDEODECODE_API __declspec(dllimport)
#endif
#include "bll/UDPClient/UDPAsyncClient.h"
#include "bll/DataManager/DataManager.h"
#include "bll/Utils/CommonFuntion.h"
#include "bll/frame.h"
#include "bll/video_data_mgn.h"
#include "bll/h264.h"
extern "C"
{
//编码
#include "libavcodec/avcodec.h"
//封装格式处理
#include "libavformat/avformat.h"
//像素处理
#include "libswscale/swscale.h"
#include "libavutil/avutil.h"
#include "libavutil/imgutils.h"
};
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
#define VIDEO_UDP_PORT (1234)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAX_RECVBUF_SIZE (64 * 1024)
#define MAX_FRAME_SIZE (1024 * 1024)
#pragma pack(push, 1)
struct Decoder {
AVCodec *codec;
AVFrame *frame;
AVCodecContext *dec_ctx;
};
typedef struct {
uint8_t *data;
int size;
int64_t pts;
int keyframe;
int decode_only;
} VideoPacket;
typedef struct {
int width;
int height;
int64_t pts;
int keyframe;
int decode_only;
int opaque;
uint8_t *data;
int size;
} YuvFrame;
#pragma pack(pop)
typedef void (WINAPIV *yuvFrame_func)(YuvFrame *yuv);
class AUTELVIDEODECODE_API CAutelVideoDecode
{
public:
explicit CAutelVideoDecode(std::string strHost="", int nPort=VIDEO_UDP_PORT);
~CAutelVideoDecode(void);
inline void SetYuvframeCallback(yuvFrame_func pfn) {
m_pfnyuvFrame = pfn;
}
//functions
private:
//通过udp实时获取视频数据包线程
void StartRecvPacketThread();
void StopRecvPacketThread();
static int WINAPIV recv_video_data_func(char* buf, int length, void* ctx, void* pObj);
void DoRecvPacket();
static DWORD ThRecvPacket(LPVOID lpParam);
//从buf里面读取视频帧数据进行渲染线程
void StartRenderThread();
void StopRenderThread();
void DoRenderFrame();
static DWORD ThRenderFrame(LPVOID lpParam);
Decoder *Decoder_Create(void);
int Decoder_Destroy();
int Decoder_Decode(VideoPacket *inpkt, YuvFrame *frame);
int get_video_frame(AVFrame *frame, YuvFrame *yuv);
void PushFrame(YuvFrame frame);
YuvFrame FrontFrame();
//params
private:
//通过udp实时获取视频数据包线程
HANDLE m_hRecvVideoPacket;
bool m_bStopRecvVideoPacket;
//从buf里面读取视频帧数据进行渲染线程
HANDLE m_hRenderFrame;
bool m_bStopRender;
CUDPAsyncClient *m_pUdpClient;
std::string m_strHostAddr;
int m_udpPort;
Decoder *m_pDecoder;
yuvFrame_func m_pfnyuvFrame;
typedef std::queue<YuvFrame> QUEUE_YUVFRAME;
QUEUE_YUVFRAME m_que_yuvframe;
CRITICAL_SECTION m_cri_yuvFramequeLocker;
};
#endif
CPP文件
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "AutelVideoDecode.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C"
{
AUTELVIDEODECODE_API CAutelVideoDecode* AutelVideoDecode() { return new CAutelVideoDecode(); }
AUTELVIDEODECODE_API void SetYuvframeCallback(CAutelVideoDecode* pAutel, yuvFrame_func pfn) { pAutel->SetYuvframeCallback(pfn); }
}
#####编译成AutelVideoDecode.dll库后供python2.7程序调用。
####Python程序如下
import sys,pygame
import YUVFrame
import thread
from ctypes import *
ObjAutelvideodecodeDLL = windll.LoadLibrary(".\bin\autelvideodecode.dll")
class AutoTestAutelRealTimeVideo:
def init(self):
pygame.init()
self.size = width, height = 1280, 720
self.black = 0, 0, 0
self.YUVFrame = None
self.overlay =None
self.IYUV = pygame.IYUV_OVERLAY
self.lock = thread.allocate_lock()
CALLFUCTION = CFUNCTYPE(None, POINTER(YUVFrame.YUVFrame))
self.PrenderYuvframe = CALLFUCTION(self.renderYuvframe)
#ObjAutelvideodecodeDLL.AutelVideoDecode.argtype=c_char_p
ObjAutelvideodecodeDLL.AutelVideoDecode.restype = c_void_p
ObjAutelvideodecodeDLL.SetYuvframeCallback.argtypes = (c_void_p,c_void_p)
#ObjAutelvideodecodeDLL.SetYuvframeCallback.restype = None
self.AutelObj = ObjAutelvideodecodeDLL.AutelVideoDecode()
def ......
编译后出错:
Connected to pydev debugger (build 129.782)
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1481, in
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1124, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "E:/AutoTestAutelRealTimeVideo/AutoTestAutelRealTimeVideo.py", line 84, in
test()
File "E:/AutoTestAutelRealTimeVideo/AutoTestAutelRealTimeVideo.py", line 66, in test
test = AutoTestAutelRealTimeVideo()
File "E:/AutoTestAutelRealTimeVideo/AutoTestAutelRealTimeVideo.py", line 25, in init
self.AutelObj = ObjAutelvideodecodeDLL.AutelVideoDecode()
WindowsError: exception: access violation writing 0x00905A4D