使用的sdl库及头文件已上传至http://download.csdn.net/source/1775400
sdl是开源的跨平台多媒体开发包, 主要用在快速的视频格式转换和显示,以及封装了键盘/鼠标/手机按键/触屏等各个平台的用户交互响应,广泛应用在游戏,多媒体播放器等应用中。目前支持windows,linux, wince,mac os,非正式地支持symbian os。
一般视频解码器输出图像的是yuv420格式,而屏幕显示大都是rgb42规格。 mobile上画面显示一般经由decode>>yuv2rgb>>scale>>directDraw直接写屏。 采用SDL可以加速这一过程,且更方便响应全屏缩放等用户操作。
下面介绍mobile上用sdl来加速显示视频的全过程。
1. 把主对话框的窗口句柄传给显示类。
2. 初始化sdl,设置显示视频模式
SDL_Surface *screen;
SDL_Rect sdl_rect;
SDL_Overlay *bmp;
bool InitSdl()
{
char sdl_var[64];
sprintf(sdl_var, "SDL_WINDOWID=0x%lx", m_hWnd);//主窗口句柄
SDL_putenv(sdl_var);
char *myvalue = SDL_getenv("SDL_WINDOWID");
atexit(SDL_Quit);
//根据解码后的视频尺寸来初始化sdl
screen = SDL_SetVideoMode(ImageWidth, ImageHeight, 0,SDL_ANYFORMAT|SDL_RESIZABLE|SDL_DOUBLEBUF|SDL_HWSURFACE);//注意用到的参数flags
if(!screen)
{
TRACE(L"error SDL SetVideoMode!");
return false;
}
bmp = SDL_CreateYUVOverlay(ImageWidth, ImageHeight, ,SDL_YV12_OVERLAY,screen);
if(!bmp)
{
TRACE(L"error SDL CreateYUVOverlay!");
return false;
}
return TRUE;
}
3. 在屏幕上绘制像素
void SdlDisplayFrame()
{
AVPicture pict;
pict.data[0] = bmp->pixels[0];
pict.data[1] = bmp->pixels[2];
pict.data[2] = bmp->pixels[1];
pict.linesize[0] = bmp->pitches[0];
pict.linesize[1] = bmp->pitches[2];
pict.linesize[2] = bmp->pitches[1];
//pFrame是ffmpeg存放解码后yuv数据的struct
img_convert(&pict, PIX_FMT_YUV420P, (AVPicture *)pFrame, pContext->pix_fmt, iImage_Width, iImage_Height);
SDL_LockSurface(screen);
SDL_LockYUVOverlay(bmp);
{
sdl_rect.x = 0;
sdl_rect.y = 0;
sdl_rect.w = m_width; //pContext->width;
sdl_rect.h = m_height; //pContext->height;
}
SDL_UnlockYUVOverlay(bmp);
SDL_UnlockSurface(screen);
SDL_DisplayYUVOverlay(bmp, &sdl_rect);//显示图片到屏幕
}
4. 事件轮询和按键响应
void SdlEvent()
{
SDL_Event event;
while ( SDL_PollEvent(&event))
{
TRACE(L"========SDL VIDEORESIZE EVENT START");
switch (event.type)
{
case SDL_VIDEORESIZE:
// 响应图像缩放事件
if (m_iImage_Width == Image_Primary_Width && m_iImage_Height == Image_Primary_Height)
{
TRACE(L"event.resize.w = %d",event.resize.w);
TRACE(L"event.resize.h = %d",event.resize.h);
m_width = event.resize.w;
m_height = event.resize.h;
SDL_FreeYUVOverlay(bmp);
bmp = NULL;
SDL_FreeSurface(screen);
screen = NULL;
if (!IsQcif)
{
m_height = m_height * 2;
m_width = m_width * 2;
screen = SDL_SetVideoMode(MobileFullScreenWidth,MobileFullScreenHeight,
0,SDL_ANYFORMAT|SDL_RESIZABLE|SDL_DOUBLEBUF|SDL_HWSURFACE);
if (screen == NULL)
{
TRACE(L"screen= NULL");
return;
}
bmp = SDL_CreateYUVOverlay(MobileFullScreenWidth,MobileFullScreenHeight,
SDL_YV12_OVERLAY,screen);
if (bmp == NULL)
{
TRACE(L"bm1 = NULL");
return;
}
IsQcif = true;
}
else
{
screen = SDL_SetVideoMode(Image_Primary_Width,Image_Primary_Height,
0,SDL_ANYFORMAT|SDL_RESIZABLE|SDL_DOUBLEBUF|SDL_HWSURFACE);
if (screen == NULL)
{
TRACE(L"screen1= NULL");
return;
}
bmp = SDL_CreateYUVOverlay(Image_Primary_Width,Image_Primary_Height,
SDL_YV12_OVERLAY,screen);
if (bmp == NULL)
{
TRACE(L"bm1 = NULL");
return;
}
IsQcif = false;
}
break;
}
case SDL_MOUSEBUTTONDOWN:
//响应触屏笔点击事件
break;
}
case SDL_QUIT:
//响应退出事件
break;
default:
break;
}
}
}