⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videorendererrgb.cpp

📁 <VC++视频音频开发>一书的光盘资料。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************************
 *                                                                                    *
 *                                                                                    *
 **************************************************************************************/

#include "VideoRendererRGB.h"

/*
 * 包括直接显示
 *
 */

#include <ddraw.h>

/*
 * RGB视频补偿类
 */


MediaVideoRendererRGB::MediaVideoRendererRGB()
{
	this->subtitler    = NULL;

	this->lpdd         = NULL;
	this->lpddClipper  = NULL;
	this->lpddsPrimary = NULL;
	this->lpddsBack    = NULL;

	this->bpp          = 0;
	this->videoMode    = VIDEO_MODE_NONE;
}

MediaVideoRendererRGB::~MediaVideoRendererRGB()
{

}

/*
 * 媒体项方法
 */

media_type_t  MediaVideoRendererRGB::GetType()
{
	return MEDIA_TYPE_VIDEO_RENDERER;
}

char         *MediaVideoRendererRGB::GetName()
{
	return "Standard RGB Video Renderer";
}

MP_RESULT MediaVideoRendererRGB::Connect(MediaItem *item)
{
	/*
	 * 只接受字幕
	 *
	 */

	if(item && item->GetType() == MEDIA_TYPE_SUBTITLER) {

		/*
		 * 接受字幕源
		 */

		this->subtitler = (MediaItemSubtitler *) item;

		return MP_RESULT_OK;
	}

	return MP_RESULT_ERROR;
}

MP_RESULT MediaVideoRendererRGB::ReleaseConnections()
{
	if(this->subtitler)
		this->subtitler = NULL;

	return MP_RESULT_OK;
}


DWORD         MediaVideoRendererRGB::GetCaps()
{
	return 0;
}

MP_RESULT     MediaVideoRendererRGB::Configure(HINSTANCE hInstance, HWND hwnd)
{
	return MP_RESULT_ERROR;
}

/*
 * 视频补偿方法
 */

MP_RESULT MediaVideoRendererRGB::Init(HWND hwnd, unsigned int width, unsigned int height)
{
	HRESULT        ddrval;
	DDSURFACEDESC2 ddsd;

	if(hwnd && width > 0 && height > 0) {

		/*
		 * 产生主要的直接显示对象
		 */

		ddrval = DirectDrawCreateEx(NULL, (void **) &this->lpdd, IID_IDirectDraw7, NULL);

		if(FAILED(ddrval)) {
	
			return MP_RESULT_ERROR;
		}

		ddrval = lpdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);

		if(FAILED(ddrval)) {

			this->lpdd->Release();
			this->lpdd = NULL;
			
			return MP_RESULT_ERROR;
		}
	
		/*
		 * 产生主界面Create the primary surface
		 */

		memset( &ddsd, 0, sizeof(ddsd) );
	    ddsd.dwSize     = sizeof( ddsd );

	    ddsd.dwFlags           = DDSD_CAPS;
		ddsd.ddsCaps.dwCaps    = DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY;

	    ddrval = this->lpdd->CreateSurface(&ddsd, &this->lpddsPrimary, NULL);

		if(FAILED(ddrval)) {

			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
		}

		/*
		 * 设置剪辑
		 */

		ddrval = this->lpdd->CreateClipper(0, &this->lpddClipper, NULL);

		if(FAILED(ddrval)) {

			this->lpddsPrimary->Release();
			this->lpddsPrimary = NULL;
			
			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
		}

	    ddrval = this->lpddClipper->SetHWnd(0, hwnd);

	    if(FAILED(ddrval)) {

			this->lpddsPrimary->Release();
			this->lpddsPrimary = NULL;
			
			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
		}

		ddrval = this->lpddsPrimary->SetClipper(this->lpddClipper);

		if(ddrval != DD_OK) {

			this->lpddsPrimary->Release();
			this->lpddsPrimary = NULL;
			
			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
		}
	
		/*
		 * 最后产生背景Finally Create Back Surface
		 */
	
		ZeroMemory(&ddsd, sizeof(ddsd));
    	ddsd.dwSize     = sizeof(ddsd);
			
		ddsd.dwFlags        = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
		ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
		
		ddsd.dwWidth  = width;
		ddsd.dwHeight = height;

		if(this->lpdd->CreateSurface(&ddsd, &this->lpddsBack, NULL) != DD_OK) {
			this->lpddsPrimary->Release();
			this->lpddsPrimary = NULL;
			
			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
			
		}

		this->width  = width;
		this->height = height;

		this->hwndPlayback = hwnd;

		/*
		 * 获取视频模式
		 */

		ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
		ddsd.dwSize     = sizeof(DDSURFACEDESC2);
		
		ddrval = this->lpddsBack->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL);

		if(FAILED(ddrval)) {

			return MP_RESULT_ERROR;
		}

		this->lpddsBack->Unlock(NULL);

		switch(ddsd.ddpfPixelFormat.dwRGBBitCount) {

		case 8:
			return MP_RESULT_ERROR;
			break;

		case 16:
			this->bpp       = 2;
			this->videoMode = VIDEO_MODE_RGB16;
			break;

		case 24:
			this->bpp       = 3;
			this->videoMode = VIDEO_MODE_RGB24;
			break;

		case 32:
			this->bpp       = 4;
			this->videoMode = VIDEO_MODE_RGB32;
			break;
		}

		return MP_RESULT_OK;
	}

	return MP_RESULT_ERROR;
}

/*
 * 为全屏显示初始化补偿器
 */

MP_RESULT MediaVideoRendererRGB::InitFullscreen(HWND hwnd, unsigned int width, unsigned int height)
{
	if(hwnd && width > 0 && height > 0) {
	
		HRESULT         ddrval;
	    DDSURFACEDESC2  ddsd;
    
	    ddrval = DirectDrawCreateEx(NULL, (VOID**)&this->lpdd, IID_IDirectDraw7, NULL);
		
		if( FAILED(ddrval))
			return MP_RESULT_ERROR;
    
	    /*
	     * 设置独占的合作模式Set Exclusive Cooperative Mode
	     */

		ddrval = this->lpdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL); //DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
		
		if( FAILED(ddrval)) {

	        this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
		}

		/*
		 * 全屏
		 *
		 */

		DDSURFACEDESC2 ddDesc;

		memset(&ddDesc, 0, sizeof(DDSURFACEDESC2));
		ddDesc.dwSize    = sizeof(DDSURFACEDESC2);


		ddrval = this->lpdd->GetDisplayMode(&ddDesc);

		if(FAILED(ddrval)) {

	        this->lpdd->Release();
			this->lpdd=NULL;
			return MP_RESULT_ERROR;;
		}

		/*
		 * 存贮全屏模式
		 */

		this->fullscreenWidth  = ddDesc.dwWidth;
		this->fullscreenHeight = ddDesc.dwHeight;

		this->fullscreenVideoHeight = height * this->fullscreenWidth / width;

		/*
		 * 产生主界面
		 */
   
	    ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
		
		ddsd.dwSize  = sizeof(DDSURFACEDESC2);
		ddsd.dwFlags = DDSD_CAPS;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY; 
    
		ddrval = this->lpdd->CreateSurface(&ddsd, &this->lpddsPrimary, NULL );

		if(FAILED(ddrval)) {

	        this->lpdd->Release();
			this->lpdd=NULL;
			return MP_RESULT_ERROR;;
		}

		/*
		 * 得到后缓冲
		 *
		 */

		ZeroMemory(&ddsd, sizeof(ddsd));
    	ddsd.dwSize     = sizeof(ddsd);
			
		ddsd.dwFlags        = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
		ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
		
		ddsd.dwWidth  = width;
		ddsd.dwHeight = height;

		if(this->lpdd->CreateSurface(&ddsd, &this->lpddsBack, NULL) != DD_OK) {
			this->lpddsPrimary->Release();
			this->lpddsPrimary = NULL;
			
			this->lpdd->Release();
			this->lpdd = NULL;

			return MP_RESULT_ERROR;
			
		}

		/*
		 * 为防止叠加继续停留在顶部产生一个剪辑
		 */

	    ddrval = this->lpdd->CreateClipper(0, &this->lpddClipper, NULL);

		if(FAILED(ddrval)) {

			return MP_RESULT_ERROR;
		}
	
	    ddrval = this->lpddClipper->SetHWnd(0, hwnd);

		if(FAILED(ddrval)) {

			return MP_RESULT_ERROR;
		}
	
	    ddrval = this->lpddsPrimary->SetClipper(this->lpddClipper);

		if(FAILED(ddrval)) {

			return MP_RESULT_ERROR;
		}
		/*
		 * 停止主的Black out the primary
		 */

		this->width  = width;
		this->height = height;

		this->hwndPlayback = hwnd;

		return MP_RESULT_OK;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -