-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDlgDisplay.cpp
256 lines (230 loc) · 6.08 KB
/
DlgDisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// DlgDisplay.cpp : implementation file
//
#include "stdafx.h"
#include "ddrawYUV.h"
#include "DlgDisplay.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "ddraw.h"
#include "mmsystem.h"
#pragma comment(lib,"ddraw.lib")
/////////////////////////////////////////////////////////////////////////////
// CDlgDisplay dialog
CDlgDisplay::CDlgDisplay(CWnd* pParent /*=NULL*/)
: CDialog(CDlgDisplay::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgDisplay)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDlgDisplay::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgDisplay)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgDisplay, CDialog)
//{{AFX_MSG_MAP(CDlgDisplay)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgDisplay message handlers
CWnd *pDdrawDisplay;
FILE *pFileYUV=NULL;
#define HEIGHT 480
#define WIDTH 720
#define DRAW_TOP 0
#define DRAW_LEFT 0
#define DRAW_HEIGHT 480
#define DRAW_WIDTH 720
HWND hWnd;
HANDLE hEvent;
HANDLE hThread;
DWORD dwThreadID;
BOOL DrawYV12();
BOOL CDlgDisplay::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
hThread=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)DrawYV12,
NULL,
CREATE_SUSPENDED,//0,
&dwThreadID);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgDisplay::OnStart()
{
// TODO: Add your control notification handler code here
SetEvent(hEvent);
ResumeThread(hThread);
}
void CDlgDisplay::OnStop()
{
// TODO: Add your control notification handler code here
ResetEvent(hEvent);
SuspendThread(hThread);
}
BOOL DrawYV12()
{
CString strFrameNum;
LPDIRECTDRAW lpDD; //DirectDraw对象指针
LPDIRECTDRAWSURFACE lpDDSPrimary; //DirectDraw主表面指针
LPDIRECTDRAWSURFACE lpDDSOffScr; //DirectDraw离屏表面指针
DDSURFACEDESC ddsd; //DirectDraw表面描述
RECT rctDest; //目标区域
RECT rctSour; //源区域
HRESULT ddRval; //DirectDraw函数返回值
hWnd=pDdrawDisplay->GetSafeHwnd();
//创建DirectDraw对象
ddRval=DirectDrawCreate(NULL,&lpDD,NULL);
if(DD_OK!=ddRval)
{
AfxMessageBox("DirectDraw对象创建不成功!");
return FALSE;
}
//设置协作层
ddRval=lpDD->SetCooperativeLevel(hWnd,DDSCL_NORMAL|DDSCL_NOWINDOWCHANGES);
if(DD_OK!=ddRval)
{
AfxMessageBox("协作层设置不成功!");
return FALSE;
}
//创建主表面
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
ddRval=lpDD->CreateSurface(&ddsd,&lpDDSPrimary,NULL);
if(DD_OK!=ddRval)
{
AfxMessageBox("主表面创建不成功!");
return FALSE;
}
//创建离屏表面对象
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.ddsCaps.dwCaps=DDSCAPS_OVERLAY;//DDSCAPS_OFFSCREENPLAIN|DDSCAPS_VIDEOMEMORY
ddsd.dwFlags=DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
ddsd.dwWidth=DRAW_WIDTH;
ddsd.dwHeight=DRAW_HEIGHT;
ddsd.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
ddsd.ddpfPixelFormat.dwFlags=DDPF_FOURCC|DDPF_YUV;
ddsd.ddpfPixelFormat.dwFourCC=MAKEFOURCC('Y','V','1','2');//('Y','U','Y','2');
ddsd.ddpfPixelFormat.dwYUVBitCount=8;
ddRval=lpDD->CreateSurface(&ddsd,&lpDDSOffScr,NULL);
if(DD_OK!=ddRval)
{
AfxMessageBox("离屏表面创建不成功!");
return FALSE;
}
//加载yv12图象文件
FILE *fYUV=fopen("E://directdraw//YUVPlayer//testdata//D1.yuv","rb+");
LPBYTE lpAllYUV=new BYTE[WIDTH*HEIGHT*3/2];
for(int f=0;f<500;f++)
{
WaitForSingleObject(hEvent,INFINITE);
strFrameNum.Format("%d",f);
pDdrawDisplay->SetWindowText(strFrameNum);
UINT iLen=fread(lpAllYUV,1,WIDTH*HEIGHT*3/2,fYUV);
if(0==iLen)
{
AfxMessageBox("Can not find the YUV input file!");
}
LPBYTE lpYUV=lpAllYUV;
LPBYTE lpY=lpYUV;
LPBYTE lpV=lpY+WIDTH*HEIGHT;//lpYUV+WIDTH*HEIGHT;
LPBYTE lpU=lpV+WIDTH*HEIGHT/4;//lpYUV+WIDTH*HEIGHT*3/2;
ddRval=lpDDSOffScr->Lock(NULL,&ddsd,DDLOCK_WAIT|DDLOCK_WRITEONLY,NULL);
while(DDERR_WASSTILLDRAWING==ddRval);
if(DD_OK!=ddRval)
{
AfxMessageBox("离屏表面锁定不成功!");
return FALSE;
}
LPBYTE lpSurf=(LPBYTE)ddsd.lpSurface;
LPBYTE lpY1=lpSurf;
LPBYTE lpV1=lpY1+ddsd.lPitch*HEIGHT;
LPBYTE lpU1=lpV1+ddsd.lPitch*HEIGHT/4;
int nOffset=DRAW_TOP*WIDTH+DRAW_LEFT;
//填充离屏表面
if(lpSurf)
{
int i=0;
//fill Y data
lpY+=nOffset;
for(i=0;i<ddsd.dwHeight;i++)
{
memcpy(lpSurf,lpY,ddsd.dwWidth);
lpY+=WIDTH;
lpSurf+=ddsd.lPitch;
}
//fill V data
lpV+=DRAW_TOP*WIDTH/4+DRAW_LEFT/2;
for(i=0;i<ddsd.dwHeight/2;i++)
{
memcpy(lpSurf,lpV,ddsd.dwWidth/2);
lpV+=WIDTH/2;
lpSurf+=ddsd.lPitch/2;
}
//file U data
lpU+=DRAW_TOP*WIDTH/4+DRAW_LEFT/2;
for(i=0;i<ddsd.dwHeight/2;i++)
{
memcpy(lpSurf,lpU,ddsd.dwWidth/2);
lpU+=WIDTH/2;
lpSurf+=ddsd.lPitch/2;
}
}
lpDDSOffScr->Unlock(NULL);
//Blt到主表面
rctSour.left=0;
rctSour.top=0;
rctSour.right=ddsd.dwWidth;
rctSour.bottom=ddsd.dwHeight;
GetClientRect(hWnd,&rctDest);
ClientToScreen(hWnd,(LPPOINT)&rctDest.left);
ClientToScreen(hWnd,(LPPOINT)&rctDest.right);
ddRval=lpDDSPrimary->Blt(&rctDest,lpDDSOffScr,&rctSour,DDBLT_WAIT,NULL);
while(DDERR_WASSTILLDRAWING==ddRval);
if(DD_OK!=ddRval)
{
AfxMessageBox("Blt到主表面不成功!");
return FALSE;
}
/*else
{
AfxMessageBox("Blt到主表面成功!");
}*/
Sleep(30);
SetEvent(hEvent);
}
//释放DirectDraw对象
fclose(fYUV);
delete lpAllYUV;
if(lpDD!=NULL)
{
if(lpDDSPrimary!=NULL)
{
lpDDSPrimary->Release();
lpDDSPrimary=NULL;
}
if(lpDDSOffScr!=NULL)
{
lpDDSOffScr->Release();
lpDDSOffScr=NULL;
}
lpDD->Release();
lpDD=NULL;
}
return TRUE;
}