DxDecoder.h.bk 2.52 KB
#ifndef __DxDecoder__DxDecoder__
#define __DxDecoder__DxDecoder__
#include "DxDecoderInterface.h"
#include <queue>
#include <stdio.h>

#ifdef _MSC_VER
#include <Windows.h>

#pragma comment( lib, "avcodec.lib" )
#pragma comment( lib, "avdevice.lib" )
#pragma comment( lib, "avfilter.lib" )
#pragma comment( lib, "avformat.lib" )
#pragma comment( lib, "avutil.lib" )
#pragma comment( lib, "postproc.lib" )
#pragma comment( lib, "swresample.lib" )
#pragma comment( lib, "swscale.lib" )
#endif

extern "C"
{
	#include <libavcodec/avcodec.h> 
	#include <libavdevice/avdevice.h> 
	#include <libavformat/avformat.h> 
	#include <libavfilter/avfilter.h> 
	#include <libavutil/avutil.h> 
	#include <libswscale/swscale.h>
}



#include <cuda.h>
#include <cuda_runtime.h>
#include <builtin_types.h>
#include <cuviddec.h>

#include <helper_functions.h>
#include <helper_cuda_drvapi.h>    // helper file for CUDA Driver API calls and error checking

#include "FrameQueue.h"
#include "VideoParser.h"
#include "VideoDecoder.h"

#include "cuda_kernels.h"


#define CUDA_UNKNOW_CODEC ( ( cudaVideoCodec )-1 )

cudaVideoCreateFlags getDecodeMode(int iMode);
cudaVideoCodec getCodecId( unsigned int id );
cudaVideoChromaFormat getColorFmt( unsigned int fmt );


typedef struct DxCUDAContext
{
	int width;
	int height;
	CUcontext cxt;
	CUdevice devId;
	DxVideoConfig cfg;
	CUVIDEOFORMAT fmt;
	CUvideoctxlock lock;
	FrameQueue * frames;
	VideoParser * parser;
	VideoDecoder * decoder;
	cudaVideoCreateFlags flags;
}DxCUDAContext;

typedef struct DxCUDACoreContext
{
	int devCount;
	CUcontext * cxt;
	unsigned int * refs;
	CRITICAL_SECTION criCxt;
}DxCUDACoreContext;

class DxDecoder: public DxDecoderInterface
{
public:
	DxDecoder( const DxDecoderConfig * cfg );
	virtual ~DxDecoder();

	virtual int OpenDecoder( const char * uri );
	virtual int CloseDecoder();
 
 	static int InitDecoder();
	static void UninitDecoder();
 
private:
	int InitCUDA();
	int UninitCUDA();
	int StreamProbe();
	int CheckCUDAProperty( int devId );

#ifdef DX_WINDOWS
	static DWORD __stdcall StreamParseThread( void * params );
	static DWORD __stdcall CUDAWorkThread( void * params );
#endif

#ifdef DX_LINUX
	static void * __stdcall StreamParseThread( void * params );
	static void * __stdcall CUDAWorkThread( void * params );
#endif

private:
	BOOL m_bRun;
	char m_uri[4096];
	HANDLE m_hThread;
	DxDecoderConfig m_cfg;
	DxCUDAContext m_cudaCxt;
	CRITICAL_SECTION m_criTms;
	unsigned int m_parseFrame;
	unsigned int m_decodeFrame;

	std::queue < unsigned long long > m_tms;
 
 static DxCUDACoreContext * s_coreCxt;
};

#endif