使用D3D渲染YUV_RGB

下面我们封装一个类,使用d3d实现yuv,rgb最简单的渲染功能。只需传窗口句柄,数据大小即可完成初始化,然后就可以按照自己的频率往里送数据。

//JYD3DRender.h
#include <stdio.h>
#include <tchar.h>
#include <d3d9.h>

class JYD3DRender{

public:
    JYD3DRender();
    ~JYD3DRender();

public:
    int Init(HWND hwnd, unsigned int nWidth, unsigned int nHeight, bool isYuv);
    void Cleanup();
    bool Render(char *buffer);

private:
    bool                    m_bIsYuv;
    int                     m_nWidth;
    int                     m_nHeight;
    RECT                    m_rtViewport;
    CRITICAL_SECTION        m_critial;
    IDirect3D9              *m_pDirect3D9;
    IDirect3DDevice9        *m_pDirect3DDevice;
    IDirect3DSurface9       *m_pDirect3DSurfaceRender;
};

//JYD3DRender.cpp
#include "JYD3DRender.h"

JYD3DRender::JYD3DRender()
{
    m_pDirect3D9 = NULL;
    m_pDirect3DDevice = NULL;
    m_pDirect3DSurfaceRender = NULL;
}
JYD3DRender::~JYD3DRender()
{
}


int JYD3DRender::Init(HWND hwnd, unsigned int nWidth, unsigned int nHeight, bool isYuv){

    HRESULT lRet;
    InitializeCriticalSection(&m_critial);
    Cleanup();

    m_pDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
    if (m_pDirect3D9 == NULL)
        return -1;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    GetClientRect(hwnd, &m_rtViewport);

    lRet = m_pDirect3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pDirect3DDevice);
    if (FAILED(lRet))
        return -1;

    if (isYuv){
        lRet = m_pDirect3DDevice->CreateOffscreenPlainSurface(nWidth, nHeight, (D3DFORMAT)MAKEFOURCC('Y', 'V', '1', '2'), D3DPOOL_DEFAULT, &m_pDirect3DSurfaceRender, NULL);
        if (FAILED(lRet))
            return -1;
    }
    else{
        lRet = m_pDirect3DDevice->CreateOffscreenPlainSurface(nWidth, nHeight, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_pDirect3DSurfaceRender, NULL);
        if (FAILED(lRet))
            return -1;
    }
    
    m_nWidth = nWidth;
    m_nHeight = nHeight;
    m_bIsYuv = isYuv;

    return 0;
}

void JYD3DRender::Cleanup()
{
    EnterCriticalSection(&m_critial);
    if (m_pDirect3DSurfaceRender)
        m_pDirect3DSurfaceRender->Release();
    if (m_pDirect3DDevice)
        m_pDirect3DDevice->Release();
    if (m_pDirect3D9)
        m_pDirect3D9->Release();
    LeaveCriticalSection(&m_critial);
}


bool JYD3DRender::Render(char *buffer){

    if (!m_pDirect3DSurfaceRender || !buffer)
        return false;

    HRESULT lRet;
    D3DLOCKED_RECT d3d_rect;
    lRet = m_pDirect3DSurfaceRender->LockRect(&d3d_rect, NULL, D3DLOCK_DONOTWAIT);
    if (FAILED(lRet))
        return false;

    byte *pSrc = (byte *)buffer;
    byte * pDest = (BYTE *)d3d_rect.pBits;
    int stride = d3d_rect.Pitch;

    unsigned long i = 0;
    if (m_bIsYuv){  //如果送入的数据是YUV420P,需要注意它与YV12的区别(U,V的位置刚好相反)
        for (i = 0; i < m_nHeight; i++){
            memcpy(pDest + i * stride, pSrc + i * m_nWidth, m_nWidth);
        }
        for (i = 0; i < m_nHeight / 2; i++){
            memcpy(pDest + stride * m_nHeight + i * stride / 2, pSrc + m_nWidth * m_nHeight * 5 / 4 + i * m_nWidth / 2, m_nWidth / 2);
        }
        for (i = 0; i < m_nHeight / 2; i++){
            memcpy(pDest + stride * m_nHeight + stride * m_nHeight / 4 + i * stride / 2, pSrc + m_nWidth * m_nHeight + i * m_nWidth / 2, m_nWidth / 2);
        }
    }
    else{
        int pixel_w_size = m_nWidth * 4;
        for (i = 0; i< m_nHeight; i++){
            memcpy(pDest, pSrc, pixel_w_size);
            pDest += stride;
            pSrc += pixel_w_size;
        }
    }

    lRet = m_pDirect3DSurfaceRender->UnlockRect();
    if (FAILED(lRet))
        return false;

    if (m_pDirect3DDevice == NULL)
        return false;

    m_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    m_pDirect3DDevice->BeginScene();
    IDirect3DSurface9 * pBackBuffer = NULL;

    m_pDirect3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
    m_pDirect3DDevice->StretchRect(m_pDirect3DSurfaceRender, NULL, pBackBuffer, &m_rtViewport, D3DTEXF_LINEAR);
    m_pDirect3DDevice->EndScene();
    m_pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
    pBackBuffer->Release();

    return true;
}

下面是一个使用方法,在这里我用Qt窗口句柄

#include "JYD3DRender.h"
#include <windows.h>
#include <process.h>

#define WIDTH 1280
#define HEIGHT 720

static void video_frame_yuv(void *opaque)
{
    JYD3DRender pD3DRender;
    pD3DRender.Init((HWND)opaque, WIDTH, HEIGHT, true);

    FILE *fileYuv = fopen("1280x720.yuv", "rb");
    if (!fileYuv)
        return;
    int size = WIDTH * HEIGHT * 3 / 2;
    char *buffer = (char *)malloc(size * 3 / 2);

    while (1){
        if (fread(buffer, 1, size, fileYuv) != size){
            fseek(fileYuv, 0, 0);
            fread(buffer, 1, size, fileYuv);
        }
        pD3DRender.Render(buffer);
        Sleep(30);
    }
    fclose(fileYuv);
    fileYuv = NULL;
}

static void video_frame_rgb(void *opaque)
{
    JYD3DRender pD3DRender;
    pD3DRender.Init((HWND)opaque, WIDTH, HEIGHT, true);

    FILE *fileRGB = fopen("1280x720.rgb", "rb");
    if (!fileRGB)
        return;
    int size = WIDTH * HEIGHT * 4;
    char *buffer = (char *)malloc(size * 4);

    while (1){
        if (fread(buffer, 1, size, fileRGB) != size){
            fseek(fileRGB, 0, 0);
            fread(buffer, 1, size, fileRGB);
        }
        pD3DRender.Render(buffer);
        Sleep(30);
    }
    fclose(fileRGB);
    fileRGB = NULL;
}

int main(int argc, char **argv)
{
    QApplication a(argc, argv);

    QWidget *w = new QWidget();
    w->show();

    _beginthreadex(NULL, 0, (unsigned int(_stdcall*)(void*))video_frame_yuv, (void *)w->winId(), 0, NULL);

    return a.exec();
}

程序设置记得添加 D3D9.lib

展开阅读全文

没有更多推荐了,返回首页