使用d3d9绘制YUV 420时出现一些问题

时间:2019-03-13 14:33:24

标签: c++ video-processing direct3d d3dx

我正在使用以下代码显示YUV(1920 * 1080)视频文件,但无法正常工作,在Qt Creator中运行它时崩溃了。以下代码有什么问题,以及如何更正以下代码以显示YUV视频? 代码如下,还有一些我省略的代码:

......

const int pixel_w=1920,pixel_h=1080;
FILE *fileYuv=NULL;
//Bit per Pixel
const int bpp=32;

unsigned char buffer[pixel_w*pixel_h*bpp/8];


int Init( HWND hwnd, unsigned int lWidth, unsigned int lHeight, bool isYuv )


bool Render(char *buffer)
{
    LRESULT lRet;

    if(buffer == NULL || m_pDirect3DDevice == NULL)
        return false;
    //Clears one or more surfaces
    lRet = m_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
//    lRet = m_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    D3DLOCKED_RECT d3d_rect;
    //Locks a rectangle on a texture resource.
    //And then we can manipulate pixel data in it.
    lRet = m_pDirect3DTexture->LockRect( 0, &d3d_rect, 0, 0 );
    if ( FAILED(lRet) ){
        return false;
    }
    // Copy pixel data to texture
    byte *pSrc = (byte *)buffer;
    byte *pDest = (byte *)d3d_rect.pBits;
    int stride = d3d_rect.Pitch;

    unsigned long i = 0;

    for(i = 0;i < pixel_h;i ++){
        memcpy(pDest + i * stride,pSrc + i * pixel_w, pixel_w);
    }
    for(i = 0;i < pixel_h/2;i ++){
        memcpy(pDest + stride * pixel_h + i * stride / 2,pSrc + pixel_w * pixel_h + pixel_w * pixel_h / 4 + i * pixel_w / 2, pixel_w / 2);
    }
    for(i = 0;i < pixel_h/2;i ++){
        memcpy(pDest + stride * pixel_h + stride * pixel_h / 4 + i * stride / 2,pSrc + pixel_w * pixel_h + i * pixel_w / 2, pixel_w / 2);
    }


    m_pDirect3DTexture->UnlockRect( 0 );

    //Begin the scene
    if ( FAILED(m_pDirect3DDevice->BeginScene()) ){
        return false;
    }

    lRet = m_pDirect3DDevice->SetTexture( 0, m_pDirect3DTexture );

    m_pDirect3DDevice->SetStreamSource( 0, m_pDirect3DVertexBuffer,
        0, sizeof(CUSTOMVERTEX) );

    lRet = m_pDirect3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

    m_pDirect3DDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
    m_pDirect3DDevice->EndScene();

    m_pDirect3DDevice->Present( NULL, NULL, NULL, NULL );
    return true;
}

0 个答案:

没有答案