gpt4 book ai didi

c++ - 将位图设置为 IMediaSample

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:13 25 4
gpt4 key购买 nike

例如,IMediaSample 由随机数据填充以测试 DirectShow 视频源过滤器:

HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
{
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;

rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);

BYTE *pData;
long lDataLen;
pms->GetPointer(&pData);
lDataLen = pms->GetSize();
for(int i = 0; i < lDataLen; ++i) pData[i] = rand();
}

相反,我想用从文件系统加载的位图填充缓冲区,这就是我想要做的:

AM_MEDIA_TYPE *mt;
HRESULT hr;
hr = pms->GetMediaType(&mt);
if (FAILED(hr)) return hr;

VIDEOINFOHEADER *pVih;
if ((mt->formattype == FORMAT_VideoInfo) &&
(mt->cbFormat >= sizeof(VIDEOINFOHEADER)) &&
(mt->pbFormat != NULL))
{
pVih = (VIDEOINFOHEADER*)mt->pbFormat;
}
else
{
FreeMediaType(*mt);
return VFW_E_INVALIDMEDIATYPE;
}

DWORD dwBmpSize = ((640 * 3 + 31) / 32) * 4 * 480; // 640x480x24bit
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
HBITMAP hBMP = (HBITMAP)LoadImage(NULL, "path/to/my/image.png", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hdc = GetDC(NULL);
GetDIBits(hdc, hBMP, 0, 480, lpbitmap, (BITMAPINFO*)&pVih->bmiHeader, DIB_RGB_COLORS);

SetDIBitsToDevice(
hdc, 0, 0,
pVih->bmiHeader.biWidth,
pVih->bmiHeader.biHeight,
0, 0,
0,
pVih->bmiHeader.biHeight,
lpbitmap,
(BITMAPINFO*)&pVih->bmiHeader,
DIB_RGB_COLORS
);

但是当我尝试使用 Skype 时,它​​只是崩溃了......关于上面代码的任何提示,我应该如何调试这样的过滤器?

最佳答案

这里是用位图填充 IMediaSample 的工作代码:

HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
{
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;

rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);

BYTE *pData;
long lDataLen;
pms->GetPointer(&pData);
lDataLen = pms->GetSize();

HDC hdc = GetDC(NULL);
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "path/to/image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bmp;
GetObject(hBmp, sizeof(BITMAP), &bmp);

BITMAPINFOHEADER bi;

bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmp.bmWidth;
bi.biHeight = bmp.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

DWORD dwBmpSize = ((bmp.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmp.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);

GetDIBits(hdc, hBmp, 0, (UINT)bmp.bmHeight, lpbitmap, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
memcpy(pData, lpbitmap, lDataLen);

return NOERROR;
} // FillBuffer

关于c++ - 将位图设置为 IMediaSample,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42947231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com