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

最佳答案

BlackMagic带有DirectShow视频源过滤器。使用GraphEditPlus以BlackMagic过滤器作为视频源生成渲染代码。 Renderer过滤器可以链接到您选择的HWND。这应该提供最佳性能。

我相信,即使您使用RAM来缓冲缓冲区,您当前的实现也会引起更多CPUDirect2D的使用。

关于video - 将视频帧作为位图绘制到MFC窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13632474/

10-14 17:29
查看更多