directdraw显示yuv420(YV12)
height=width=widthBytes=0;
m_screen.SetWindowPos(&CWnd::wndBottom,0,0,720,576, SWP_NOMOVE | SWP_SHOWWINDOW);
UpdateWindow();
main_window_handle = m_screen.GetSafeHwnd();
if(DD_OK!=(DirectDrawCreateEx(NULL, (void **)&lpdd7, IID_IDirectDraw7, NULL)))
{
return ;
}
// set the cooperative level for full-screen mode
if(DD_OK != lpdd7->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DDSCL_NORMAL))
{
return ;
}
/*设置控制级时,如果应用程序请求了 DDSCL_NORMAL 模式(表明应用程序以普通窗口的形式运行),则不需要提供一个指定窗口的句柄.给窗口句柄参数为 NULL, 所有的窗口都可以被设置为普通的控制级. */
// set the display mode to 640x480x256
// clear ddsd and set size
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
// enable valid fields
ddsd.dwFlags=DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
// create the primary surface
if(DD_OK!=(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
{
return ;
}
// 创建裁剪器
if (FAILED(lpdd7->CreateClipper(0, &lpDDClipper, NULL)))
return ;
// 与窗口工作区关联
if (FAILED(lpDDClipper->SetHWnd(0, main_window_handle)))
{
lpDDClipper->Release();
return ;
}
if (FAILED(lpddsprimary->SetClipper(lpDDClipper)))
{
lpDDClipper->Release();
return ;
}
//
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
//DDPIXELFORMAT camdispPixelFormat = {sizeof(DDPIXELFORMAT), DDPF_FOURCC, mmioFOURCC('Y','U','1','2'), 0,0,0,0,0};
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
ddsd.dwWidth = 720;
ddsd.dwHeight = 576;
ddsd.dwBackBufferCount = 0;//忽略 //忽略
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN| DDSCAPS_VIDEOMEMORY;
ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC | DDPF_YUV ;
ddsd.ddpfPixelFormat.dwFourCC = MAKEFOURCC('Y','V','1','2');
ddsd.ddpfPixelFormat.dwYUVBitCount = 8;
//
if(DD_OK!=(lpdd7->CreateSurface(&ddsd, &lpddsmypage[0], NULL)))
{
return ;
}
POINT p;
p.x = 0; p.y = 0;
m_screen.ClientToScreen(&p);//获取屏幕顶点
// m_screen.GetClientRect(&rcRectDest);
rcRectDest.left = 0; rcRectDest.top=0; rcRectDest.right = 720; rcRectDest.bottom = 576;
OffsetRect(&rcRectDest, p.x, p.y);//把窗口区域转化为屏幕区域坐标
SetRect(&rcRectSrc, 0, 0, 720, 576);//初始化窗口区域
/////////////////////////////////////////////////////////////
FILE *fp;
buf[0] = new BYTE[720*576];
buf[1] = new BYTE[720*576/4];
buf[2] = new BYTE[720*576/4];
fp = fopen("d:\\temp\\test.yuv","rb+");
while(!feof(fp))
{
if(DD_OK != lpddsmypage[0]->Lock(NULL,&ddsd,DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL))
{
return ;
}
UCHAR *lp_buffer = (UCHAR *)ddsd.lpSurface;
///////////////////////////提取数据/////////////////////////////////////////////
fread(buf[0],720*576,1,fp);
fread(buf[1],720*576/4,1,fp);
fread(buf[2],720*576/4,1,fp);
LPBYTE lpY = lp_buffer;
LPBYTE lpV = lp_buffer + ddsd.lPitch * 576;
LPBYTE lpU = lp_buffer + ddsd.lPitch * 576 * 5 / 4;
for (int k=0;k<576;k++)
{
memcpy(lpY + k*ddsd.lPitch,buf[0]+720*k,720);
}
for (int k=0;k<576/2;k++)
{
memcpy(lpU+ k*ddsd.lPitch/2 ,buf[1]+720*k/2,720/2);
memcpy(lpV+ k*ddsd.lPitch/2 ,buf[2]+720*k/2,720/2);
}
Sleep(40);
//////////////////////////////////////////////////////////////////////////
//memcpy(bmp_buffer,buf[0],720*576*2);
if(DD_OK != lpddsmypage[0]->Unlock(NULL))
{
return ;
}
HRESULT ddRval;
ddRval= lpddsprimary->Blt( &rcRectDest, lpddsmypage[0], &rcRectSrc, DDBLT_WAIT, NULL);
while(ddRval == DDERR_WASSTILLDRAWING);
if(DD_OK != ddRval)
{
return ;
}
}
fclose(fp);
if(lpddsmypage)
{
lpddsmypage[0]->Release();
lpddsmypage[0]=NULL;
}
MessageBox(_T("over"));