将视频帧作为位图绘制到MFC窗口

时间:2012-11-29 18:38:54

标签: video bitmap mfc

我正在尝试使用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();

1 个答案:

答案 0 :(得分:0)

BlackMagic附带DirectShow个视频源过滤器。使用GraphEditPlus生成渲染代码,将BlackMagic滤镜作为视频源。 Renderer过滤器可以链接到您选择的HWND。这应该是最好的表现。

我相信您当前的实现会导致更多RAMCPU用法,即使您使用Direct2D来缓存缓冲区。

相关问题