运行不出结果,求解

kicker571 2011-07-27 10:54:01
#include <stdio.h>
#include <conio.h>
#include <sys/stat.h>
#include <ddraw.h>

unsigned char *y;
unsigned char *u;
unsigned char *v;

bool testsize(int w, int h, int s, int *pw, int *ph);

int main(int argc, char *argv[])
{
HRESULT hr;
LPDIRECTDRAW lpDD;
DDSURFACEDESC ddsd; //表面描述
LPDIRECTDRAWSURFACE lpPrimary; //主表面指针
LPDIRECTDRAWSURFACE lpOverlay; //离屏表面指针 以上为创建yuv表面指定像素格式,并指定FourCC码
RECT rcSrc;
FILE *fd;
int i, j, f;
char fname[256];
unsigned char *py, *pu, *pv;
unsigned int *dst;
int fcount, ms_per_frame;
int width, height, size;
int width2, height2, size2;
int t1, t2;
char c;
bool done, wait;

if (argc != 3 && argc != 5 && argc != 6)
{
fprintf(stderr, "yuv_disp filename Fcount width height ms_per_frame");//fprintf功能:传送格式化输出到一个文件中,和printf区别于它是用于文件操作
return -1;
}
sscanf(argv[1], "%s", fname);
printf ("the input file is %s\n",fname);
sscanf(argv[2], "%d", &fcount);
if (argc == 6)
{
sscanf(argv[5], "%d", &ms_per_frame);
}
else
{
ms_per_frame = 33;
}
if (argc >= 5)
{
sscanf(argv[3], "%d", &width);
sscanf(argv[4], "%d", &height);
}
else
{
struct _stat st;
//sprintf(name, "%s\n", fname);
if (_stat(fname, &st) != 0)
return -1;
size = st.st_size;
if (!(testsize( 176, 144, size, &width, &height)
|| testsize( 352, 240, size, &width, &height)
|| testsize( 352, 480, size, &width, &height)
|| testsize( 704, 480, size, &width, &height)
|| testsize( 720, 480, size, &width, &height)
|| testsize( 352, 288, size, &width, &height)
|| testsize( 352, 576, size, &width, &height)
|| testsize( 704, 576, size, &width, &height)
|| testsize( 720, 576, size, &width, &height)
|| testsize( 8, 8, size, &width, &height)
|| testsize( 16, 16, size, &width, &height)
|| testsize( 32, 32, size, &width, &height)
|| testsize( 64, 64, size, &width, &height)
|| testsize( 128, 128, size, &width, &height)
|| testsize( 256, 256, size, &width, &height)
|| testsize( 512, 512, size, &width, &height)
|| testsize(1024,1024, size, &width, &height)))
{
fprintf(stderr, "Nonstandard picture size: specify width and height\n");
return -1;
}
}
width2 = width/2;
height2 = height;
size = width*height;
size2 = width2*height2;
y = (unsigned char *)malloc(size);
u = (unsigned char *)malloc(size2);
v = (unsigned char *)malloc(size2);
if (y == NULL || u == NULL || v ==NULL)
{
fprintf(stderr, "Out of memory\n");
return -1;
}
hr = DirectDrawCreate(NULL, &lpDD, NULL); //通过此函数来创建DirectDraw的对象,lpDD就是创建的对象。
if (FAILED(hr)) { fprintf(stderr, "DirectDrawCreate failed\n"); return -1; }
hr = lpDD->SetCooperativeLevel(NULL, DDSCL_NORMAL); //通过调用lpDD->SetCooperativeLevel函数来程序的运行方式,比如全屏模式、窗口模式等
if (FAILED(hr)) { fprintf(stderr, "SetCooperativeLevel failed\n"); return -1; }
ZeroMemory(&ddsd, sizeof(ddsd)); //先将DDSURFACEDESC结构清空
ddsd.dwSize = sizeof(ddsd); // dwSize 成员被设为 DDSURFACEDESC 结构的大小,可以预防任何 DirectDraw 函数返回不可用成员错误
ddsd.dwFlags = DDSD_CAPS; //确定了DDSURFACEDESC 结构中的哪一个成员将被填充有效信息,这里为DDSD_CAPS
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = lpDD->CreateSurface(&ddsd, &lpPrimary, NULL);//调用此函数创建主表面
if (FAILED(hr)) { fprintf(stderr, "CreateSurface (primary) failed\n"); return -1; }
ZeroMemory(&ddsd, sizeof(ddsd));//创建YUV表面前需要重新填充DDSURFACEDESC结构,因此需要将之前已经填充的DDSURFACEDESC结构清空,重新填充
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
ddsd.ddpfPixelFormat.dwFourCC = 0x32595559; // YUY2 4:2:2
hr = lpDD->CreateSurface(&ddsd, &lpOverlay, NULL); //重新填充好以后,调用此函数创建YUV表面
if (FAILED(hr)) { fprintf(stderr, "CreateSurface (overlay) failed\n"); return -1; }
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
hr = lpOverlay->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR, NULL);//要获得对表面的使用权需要用到Lock函数来锁定需要的那片存储单元
if (FAILED(hr)) { fprintf(stderr, "Lock failed\n"); return -1; }
for (i=0; i<height; i++)
{
dst = (unsigned int *)(((unsigned char *)ddsd.lpSurface) + i * ddsd.lPitch);
for (j=0; j<width2; j++)
{
dst[j] = 0x00800080;
}
}
hr = lpOverlay->Unlock(NULL);//对应之前使用的lock函数,调用Unlock函数来解除对该片存储单元的控制,以免造成内存的泄漏
SetRect(&rcSrc, 0, 0, width, height);
hr = lpOverlay->UpdateOverlay(NULL, lpPrimary, NULL, DDOVER_SHOW, NULL);
if (FAILED(hr)) { fprintf(stderr, "UpdateOverlay failed\n"); return -1; }
done = false;
wait = false;
while (!done)
{
t1 = timeGetTime();
fd = fopen("D:\\news_cif_300.yuv", "rb");
for (f=0; f<fcount; f++)// the mail loop for displaying frames, the original programming though each file only has one frame!
{
//sprintf(name, argv[1], f);
//fd = fopen(name, "rb");
if (fd != NULL)
{
fread(y, 1, size, fd);//通过fread函数分别将读取到的y信息、u信息、v信息存到字符指针y、u、v中
fread(u, 1, size2, fd);
fread(v, 1, size2, fd);
//fclose(fd);
}
else
{
printf("open file fail\n");

}
if (fd != NULL)
{
t2 = t1 - timeGetTime();
if (t2 > 0) Sleep(t2);
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
hr = lpOverlay->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR, NULL);//用Lock函数来锁定需要的那片存储单元
if (SUCCEEDED(hr))
{
for (i=0; i<height; i++)
{
dst = (unsigned int *)(((unsigned char *)ddsd.lpSurface) + i * ddsd.lPitch);
py = &y[width*i];
pu = &u[width2*i];//(i>>1)]; for
pv = &v[width2*i];//(i>>1)];

for (j=0; j<width2; j++)
{
dst[j] = py[j<<1] | (pu[j]<<8) | (py[(j<<1)+1]<<16) | (pv[j]<<24);
}
}
hr = lpOverlay->Unlock(NULL);// Unlock函数来解除对该片存储单元的控制,以免造成内存的泄漏
}
if (wait || _kbhit())
{
c = _getch();
if (c=='q')
{
done = true;
fclose(fd);
break;
}
else if (c==' ')
wait = true;
else
{
wait = false;
t1 = timeGetTime();
}
}
}
t1 += ms_per_frame;
t2 = t1 - timeGetTime();
if (t2 > 0) Sleep(t2);
}
fclose(fd);
break;
//t2 = t1 - timeGetTime();
//if (t2 > 0) Sleep(t2);
}
hr = lpOverlay->UpdateOverlay(NULL, lpPrimary, NULL, DDOVER_HIDE, NULL);
lpOverlay->Release();
lpPrimary->Release();
lpDD->Release();

return 0;
}

bool testsize(int w, int h, int s, int *pw, int *ph)
{
if (s == w*h || s == (w*h*3)/2)//||--或
{
*pw = w;
*ph = h;
return true;
}

return false;
}


功能是播放一个yuv文件,但是编译没错却无播放的画面,求解!多谢!
...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kicker571 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 doctorwing 的回复:]

好多,你Show了吗,或者运行了看看进程里有吗
[/Quote]

进程里没有啊,程序并没有显示正在运行, 我是初学者 麻烦帮忙看看哪里有问题 多谢啦
我才是心翼 2011-07-27
  • 打赏
  • 举报
回复
好多,你Show了吗,或者运行了看看进程里有吗
fengbingchun 2011-07-27
  • 打赏
  • 举报
回复
从开始设个断点,一步一步看看相应变量的值是否都正确

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧