我正在尝试使用 BlackMagic SDK 编写预览应用程序,但播放时出现断断续续的情况。我正在使用 MFC 框架,并为我的视频预览窗口子类化 CWnd。
当每一帧视频到达时,我将颜色转换为 RGB,然后调用一个函数来显示 RGB 位图。
void VideoPreview::Display(int width, int height, byte* buffer)
{
__int64 begin = GetTickCount();
HRESULT hr;
CRect rcRect, statusBarRect;
GetClientRect (rcRect);
BITMAPINFO bmInfo;
ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biBitCount = 32;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biWidth = width;
bmInfo.bmiHeader.biHeight = -height;
dc->SetStretchBltMode(COLORONCOLOR);
int iResult = StretchDIBits(*dc,
rcRect.left, rcRect.top, rcRect.right, rcRect.bottom,
0, 0, width, height,
buffer, &bmInfo, 0, SRCCOPY);
DWORD dwError;
if (iResult == 0 || iResult == GDI_ERROR)
{
dwError = GetLastError();
}
else
fpsCount++;
procTimeCount += GetTickCount() - begin;
}
可以做些什么来创建更流畅的视频?
更新:
我最终选择了 Direct2D 而不是 GDI,并获得了更好的性能。下面的代码是我现在用于渲染的代码:
// initialization
HRESULT hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&pD2DFactory
);
// Obtain the size of the drawing area.
RECT rc;
GetClientRect(&rc);
// Create a Direct2D render target
hr = pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
this->GetSafeHwnd(),
D2D1::SizeU(
1280, 720
/*rc.right - rc.left,
rc.bottom - rc.top*/)
),
&pRT);
D2D1_BITMAP_PROPERTIES properties;
properties.pixelFormat = D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_IGNORE);
properties.dpiX = properties.dpiY = 96;
hr = pRT->CreateBitmap(D2D1::SizeU(1280, 720), properties, &pBitmap);
ASSERT(SUCCEEDED(hr));
// per frame code
// buffer is rgb frame
HRESULT hr;
pRT->BeginDraw();
pBitmap->CopyFromMemory(NULL, buffer, width*4);
pRT->DrawBitmap(pBitmap);
pRT->EndDraw();