mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Refactor MediaFoundation (Part 1)
This commit is contained in:
parent
4a79d3f143
commit
d5717af2df
@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
// Windows include
|
||||
#include <Windows.h>
|
||||
|
||||
// COM includes
|
||||
#include <Guiddef.h>
|
||||
|
||||
@ -56,7 +59,7 @@ public:
|
||||
MFGrabber(const QString & device, const unsigned width, const unsigned height, const unsigned fps, const unsigned input, int pixelDecimation);
|
||||
~MFGrabber() override;
|
||||
|
||||
void receive_image(const void *frameImageBuffer, int size, QString message);
|
||||
void receive_image(const void *frameImageBuffer, int size);
|
||||
QRectF getSignalDetectionOffset() const { return QRectF(_x_frac_min, _y_frac_min, _x_frac_max, _y_frac_max); }
|
||||
bool getSignalDetectionEnabled() const { return _signalDetectionEnabled; }
|
||||
bool getCecDetectionEnabled() const { return _cecDetectionEnabled; }
|
||||
@ -88,24 +91,17 @@ signals:
|
||||
void newFrame(const Image<ColorRgb> & image);
|
||||
|
||||
private:
|
||||
struct buffer
|
||||
{
|
||||
void *start;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
bool init();
|
||||
void uninit();
|
||||
bool init_device(QString device, DevicePropertiesItem props);
|
||||
HRESULT init_device(QString device, DevicePropertiesItem props);
|
||||
void uninit_device();
|
||||
void enumVideoCaptureDevices();
|
||||
void start_capturing();
|
||||
bool process_image(const void *frameImageBuffer, int size);
|
||||
void process_image(const void *frameImageBuffer, int size);
|
||||
void checkSignalDetectionEnabled(Image<ColorRgb> image);
|
||||
|
||||
QString _deviceName;
|
||||
QMap<QString, MFGrabber::DeviceProperties> _deviceProperties;
|
||||
std::vector<buffer> _buffers;
|
||||
HRESULT _hr;
|
||||
SourceReaderCB* _sourceReaderCB;
|
||||
PixelFormat _pixelFormat;
|
||||
|
@ -78,7 +78,7 @@ class MFThreadManager : public QObject
|
||||
public:
|
||||
MFThreadManager() : _threads(nullptr)
|
||||
{
|
||||
_maxThreads = qBound(1, ((QThread::idealThreadCount() * 3) / 2), 12);
|
||||
_maxThreads = qBound(1, (QThread::idealThreadCount() > 4 ? (QThread::idealThreadCount() - 1) : QThread::idealThreadCount()), 8);
|
||||
}
|
||||
|
||||
~MFThreadManager()
|
||||
|
@ -1,22 +1,9 @@
|
||||
#include "MFSourceReaderCB.h"
|
||||
#include "grabber/MFGrabber.h"
|
||||
|
||||
static PixelFormat GetPixelFormatForGuid(const GUID guid)
|
||||
{
|
||||
if (IsEqualGUID(guid, MFVideoFormat_RGB32)) return PixelFormat::RGB32;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::BGR24;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_YUY2)) return PixelFormat::YUYV;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_UYVY)) return PixelFormat::UYVY;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_MJPG)) return PixelFormat::MJPEG;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_NV12)) return PixelFormat::NV12;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_I420)) return PixelFormat::I420;
|
||||
return PixelFormat::NO_CHANGE;
|
||||
};
|
||||
|
||||
MFGrabber::MFGrabber(const QString & device, unsigned width, unsigned height, unsigned fps, unsigned input, int pixelDecimation)
|
||||
: Grabber("V4L2:"+device)
|
||||
, _deviceName(device)
|
||||
, _buffers()
|
||||
, _hr(S_FALSE)
|
||||
, _sourceReader(nullptr)
|
||||
, _pixelDecimation(pixelDecimation)
|
||||
@ -153,7 +140,7 @@ bool MFGrabber::init()
|
||||
|
||||
if(foundIndex>=0)
|
||||
{
|
||||
if (init_device(foundDevice, dev.valid[foundIndex]))
|
||||
if(SUCCEEDED(init_device(foundDevice, dev.valid[foundIndex])))
|
||||
_initialized = true;
|
||||
}
|
||||
else
|
||||
@ -173,56 +160,60 @@ void MFGrabber::uninit()
|
||||
}
|
||||
}
|
||||
|
||||
bool MFGrabber::init_device(QString deviceName, DevicePropertiesItem props)
|
||||
HRESULT MFGrabber::init_device(QString deviceName, DevicePropertiesItem props)
|
||||
{
|
||||
bool setStreamParamOK = false;
|
||||
PixelFormat pixelformat = GetPixelFormatForGuid(props.guid);
|
||||
QString error, guid = _deviceProperties[deviceName].name;
|
||||
HRESULT hr,hr1,hr2;
|
||||
|
||||
Debug(_log, "Init %s, %d x %d @ %d fps (%s) => %s", QSTRING_CSTR(deviceName), props.x, props.y, props.fps, QSTRING_CSTR(pixelFormatToString(pixelformat)), QSTRING_CSTR(guid));
|
||||
|
||||
IMFMediaSource* device = nullptr;
|
||||
IMFAttributes* attr;
|
||||
hr = MFCreateAttributes(&attr, 2);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
int size = guid.length() + 1024;
|
||||
wchar_t *name = new wchar_t[size];
|
||||
memset(name, 0, size);
|
||||
guid.toWCharArray(name);
|
||||
IMFMediaSource* device = nullptr;
|
||||
IMFAttributes* deviceAttributes = nullptr, *sourceReaderAttributes = nullptr;
|
||||
IAMVideoProcAmp *pProcAmp = nullptr;
|
||||
IMFMediaType* type = nullptr;
|
||||
HRESULT hr = S_OK;
|
||||
|
||||
if (SUCCEEDED(attr->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, (LPCWSTR)name)) && _sourceReaderCB)
|
||||
{
|
||||
hr = MFCreateDeviceSource(attr, &device);
|
||||
Debug(_log, "Init %s, %d x %d @ %d fps (%s) => %s", QSTRING_CSTR(deviceName), props.x, props.y, props.fps, QSTRING_CSTR(pixelFormatToString(pixelformat)), QSTRING_CSTR(guid));
|
||||
|
||||
hr = MFCreateAttributes(&deviceAttributes, 2);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
SAFE_RELEASE(device);;
|
||||
error = QString("MFCreateDeviceSource %1").arg(hr);
|
||||
}
|
||||
}
|
||||
else
|
||||
error = QString("IMFAttributes_SetString_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK %1").arg(hr);
|
||||
|
||||
delete[] name;
|
||||
}
|
||||
SAFE_RELEASE(attr);
|
||||
}
|
||||
else
|
||||
{
|
||||
SAFE_RELEASE(attr);
|
||||
error = QString("MFCreateAttributes_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE %1").arg(hr);
|
||||
error = QString("Could not create device attributes (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (device)
|
||||
hr = deviceAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("SetGUID_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(FAILED(deviceAttributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, (LPCWSTR)name)) && _sourceReaderCB)
|
||||
{
|
||||
error = QString("IMFAttributes_SetString_MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = MFCreateDeviceSource(deviceAttributes, &device);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("MFCreateDeviceSource (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(!device)
|
||||
{
|
||||
error = QString("Could not open device (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
Debug(_log, "Device opened");
|
||||
|
||||
// Set Brightness/Contrast/Saturation/Hue
|
||||
if (_brightness != 0 || _contrast != 0 || _saturation != 0 || _hue != 0)
|
||||
{
|
||||
IAMVideoProcAmp *pProcAmp = NULL;
|
||||
if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&pProcAmp))))
|
||||
{
|
||||
long lMin, lMax, lStep, lDefault, lCaps, Val;
|
||||
@ -301,84 +292,90 @@ bool MFGrabber::init_device(QString deviceName, DevicePropertiesItem props)
|
||||
else
|
||||
Error(_log, "Hue is not supported by the grabber");
|
||||
}
|
||||
|
||||
pProcAmp->Release();
|
||||
}
|
||||
}
|
||||
|
||||
IMFAttributes* pAttributes;
|
||||
hr1 = MFCreateAttributes(&pAttributes, 1);
|
||||
|
||||
if (SUCCEEDED(hr1))
|
||||
hr2 = pAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, (IMFSourceReaderCallback *)_sourceReaderCB);
|
||||
|
||||
if (SUCCEEDED(hr1) && SUCCEEDED(hr2))
|
||||
hr = MFCreateSourceReaderFromMediaSource(device, pAttributes, &_sourceReader);
|
||||
else
|
||||
hr = E_INVALIDARG;
|
||||
|
||||
if (SUCCEEDED(hr1))
|
||||
pAttributes->Release();
|
||||
|
||||
device->Release();
|
||||
|
||||
if (SUCCEEDED(hr))
|
||||
hr = MFCreateAttributes(&sourceReaderAttributes, 1);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
IMFMediaType* type;
|
||||
error = QString("Could not create Source Reader attributes (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = sourceReaderAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, (IMFSourceReaderCallback *)_sourceReaderCB);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not set stream parameter: SetUnknown_MF_SOURCE_READER_ASYNC_CALLBACK (%1)").arg(hr);
|
||||
hr = E_INVALIDARG;
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = MFCreateSourceReaderFromMediaSource(device, sourceReaderAttributes, &_sourceReader);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not create the Source Reader (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = MFCreateMediaType(&type);
|
||||
if (SUCCEEDED(hr))
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not create an empty media type (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = type->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
|
||||
if (SUCCEEDED(hr))
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not set stream parameter: SetGUID_MF_MT_MAJOR_TYPE (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = type->SetGUID(MF_MT_SUBTYPE, props.guid);
|
||||
if (SUCCEEDED(hr))
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not set stream parameter: SetGUID_MF_MT_SUBTYPE (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = MFSetAttributeSize(type, MF_MT_FRAME_SIZE, props.x, props.y);
|
||||
if (SUCCEEDED(hr))
|
||||
if(FAILED(hr))
|
||||
{
|
||||
error = QString("Could not set stream parameter: SMFSetAttributeSize_MF_MT_FRAME_SIZE (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = MFSetAttributeSize(type, MF_MT_FRAME_RATE, props.fps_a, props.fps_b);
|
||||
if (SUCCEEDED(hr))
|
||||
if(FAILED(hr))
|
||||
{
|
||||
MFSetAttributeRatio(type, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
|
||||
error = QString("Could not set stream parameter: MFSetAttributeSize_MF_MT_FRAME_RATE (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = _sourceReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, type);
|
||||
if (SUCCEEDED(hr))
|
||||
hr = MFSetAttributeRatio(type, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
setStreamParamOK = true;
|
||||
error = QString("Could not set stream parameter: MFSetAttributeRatio_MF_MT_PIXEL_ASPECT_RATIO (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
error = QString("SetCurrentMediaType %1").arg(hr);
|
||||
}
|
||||
else
|
||||
error = QString("MFSetAttributeSize_MF_MT_FRAME_RATE %1").arg(hr);
|
||||
}
|
||||
else
|
||||
error = QString("SMFSetAttributeSize_MF_MT_FRAME_SIZE %1").arg(hr);
|
||||
}
|
||||
else
|
||||
error = QString("SetGUID_MF_MT_SUBTYPE %1").arg(hr);
|
||||
}
|
||||
else
|
||||
error = QString("SetGUID_MF_MT_MAJOR_TYPE %1").arg(hr);
|
||||
|
||||
type->Release();
|
||||
}
|
||||
else
|
||||
error = QString("IMFAttributes_SetString %1").arg(hr);
|
||||
|
||||
if (!setStreamParamOK)
|
||||
Error(_log, "Could not stream set params (%s)", QSTRING_CSTR(error));
|
||||
}
|
||||
else
|
||||
Error(_log, "MFCreateSourceReaderFromMediaSource (%i)", hr);
|
||||
}
|
||||
else
|
||||
Error(_log, "Could not open device (%s)", QSTRING_CSTR(error));
|
||||
|
||||
if (!setStreamParamOK)
|
||||
hr = _sourceReaderCB->InitializeVideoEncoder(type, pixelformat);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
error = QString("Failed to initialize the Video Encoder (%1)").arg(hr);
|
||||
goto done;
|
||||
}
|
||||
|
||||
hr = _sourceReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, type);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
error = QString("Failed to set media type on Source Reader (%1)").arg(hr);
|
||||
}
|
||||
|
||||
done:
|
||||
if(FAILED(hr))
|
||||
{
|
||||
Error(_log, "%s", QSTRING_CSTR(error));
|
||||
SAFE_RELEASE(_sourceReader);
|
||||
}
|
||||
else
|
||||
@ -386,49 +383,19 @@ bool MFGrabber::init_device(QString deviceName, DevicePropertiesItem props)
|
||||
_pixelFormat = props.pf;
|
||||
_width = props.x;
|
||||
_height = props.y;
|
||||
|
||||
switch (_pixelFormat)
|
||||
{
|
||||
case PixelFormat::UYVY:
|
||||
case PixelFormat::YUYV:
|
||||
{
|
||||
_frameByteSize = props.x * props.y * 2;
|
||||
_lineLength = props.x * 2;
|
||||
}
|
||||
break;
|
||||
|
||||
case PixelFormat::BGR24:
|
||||
case PixelFormat::MJPEG:
|
||||
{
|
||||
_frameByteSize = props.x * props.y * 3;
|
||||
_lineLength = props.x * 3;
|
||||
}
|
||||
break;
|
||||
|
||||
case PixelFormat::RGB32:
|
||||
{
|
||||
_frameByteSize = props.x * props.y * 4;
|
||||
_lineLength = props.x * 4;
|
||||
}
|
||||
break;
|
||||
// Cleanup
|
||||
SAFE_RELEASE(deviceAttributes);
|
||||
delete[] name;
|
||||
SAFE_RELEASE(device);
|
||||
SAFE_RELEASE(pProcAmp);
|
||||
SAFE_RELEASE(type);
|
||||
SAFE_RELEASE(sourceReaderAttributes);
|
||||
|
||||
case PixelFormat::NV12:
|
||||
{
|
||||
_frameByteSize = (6 * props.x * props.y) / 4;
|
||||
_lineLength = props.x;
|
||||
}
|
||||
break;
|
||||
|
||||
case PixelFormat::I420:
|
||||
{
|
||||
_frameByteSize = (6 * props.x * props.y) / 4;
|
||||
_lineLength = props.x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return setStreamParamOK;
|
||||
return hr;
|
||||
}
|
||||
|
||||
void MFGrabber::uninit_device()
|
||||
@ -529,18 +496,22 @@ void MFGrabber::enumVideoCaptureDevices()
|
||||
}
|
||||
pSource->Release();
|
||||
}
|
||||
|
||||
properties.displayResolutions.sort();
|
||||
properties.framerates.sort();
|
||||
_deviceProperties.insert(dev, properties);
|
||||
}
|
||||
|
||||
CoTaskMemFree(symlink);
|
||||
}
|
||||
|
||||
CoTaskMemFree(name);
|
||||
devices[i]->Release();
|
||||
}
|
||||
|
||||
CoTaskMemFree(devices);
|
||||
}
|
||||
|
||||
attr->Release();
|
||||
}
|
||||
}
|
||||
@ -550,26 +521,23 @@ void MFGrabber::start_capturing()
|
||||
{
|
||||
if (_sourceReader)
|
||||
{
|
||||
HRESULT hr = _sourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM,
|
||||
0, NULL, NULL, NULL, NULL);
|
||||
HRESULT hr = _sourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, NULL, NULL, NULL);
|
||||
if (!SUCCEEDED(hr))
|
||||
Error(_log, "ReadSample (%i)", hr);
|
||||
}
|
||||
}
|
||||
|
||||
bool MFGrabber::process_image(const void *frameImageBuffer, int size)
|
||||
void MFGrabber::process_image(const void *frameImageBuffer, int size)
|
||||
{
|
||||
bool frameSend = false;
|
||||
|
||||
unsigned int processFrameIndex = _currentFrame++;
|
||||
|
||||
// frame skipping
|
||||
if((processFrameIndex % _fpsSoftwareDecimation != 0) && (_fpsSoftwareDecimation > 1))
|
||||
return frameSend;
|
||||
return;
|
||||
|
||||
// CEC detection
|
||||
if(_cecDetectionEnabled)
|
||||
return frameSend;
|
||||
return;
|
||||
|
||||
// We do want a new frame...
|
||||
if (size < _frameByteSize && _pixelFormat != PixelFormat::MJPEG)
|
||||
@ -593,6 +561,7 @@ bool MFGrabber::process_image(const void *frameImageBuffer, int size)
|
||||
for (unsigned int i=0;_threadManager.isActive() && i < _threadManager._maxThreads && _threadManager._threads != nullptr; i++)
|
||||
{
|
||||
if ((_threadManager._threads[i]->isFinished() || !_threadManager._threads[i]->isRunning()))
|
||||
{
|
||||
// aquire lock
|
||||
if ( _threadManager._threads[i]->isBusy() == false)
|
||||
{
|
||||
@ -602,14 +571,12 @@ bool MFGrabber::process_image(const void *frameImageBuffer, int size)
|
||||
if (_threadManager._maxThreads > 1)
|
||||
_threadManager._threads[i]->start();
|
||||
|
||||
frameSend = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return frameSend;
|
||||
}
|
||||
}
|
||||
|
||||
void MFGrabber::setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold)
|
||||
@ -669,17 +636,9 @@ void MFGrabber::stop()
|
||||
}
|
||||
}
|
||||
|
||||
void MFGrabber::receive_image(const void *frameImageBuffer, int size, QString message)
|
||||
void MFGrabber::receive_image(const void *frameImageBuffer, int size)
|
||||
{
|
||||
if (frameImageBuffer == NULL || size ==0)
|
||||
Error(_log, "Received empty image frame: %s", QSTRING_CSTR(message));
|
||||
else
|
||||
{
|
||||
if (!message.isEmpty())
|
||||
Debug(_log, "Received image frame: %s", QSTRING_CSTR(message));
|
||||
process_image(frameImageBuffer, size);
|
||||
}
|
||||
|
||||
start_capturing();
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
#include <mfapi.h>
|
||||
#include <mftransform.h>
|
||||
#include <dmo.h>
|
||||
#include <wmcodecdsp.h>
|
||||
#include <mfidl.h>
|
||||
#include <mfreadwrite.h>
|
||||
#include <shlwapi.h>
|
||||
@ -13,11 +15,26 @@
|
||||
#pragma comment (lib, "mfplat.lib")
|
||||
#pragma comment (lib, "mfuuid.lib")
|
||||
#pragma comment (lib, "mfreadwrite.lib")
|
||||
#pragma comment (lib, "strmiids.lib")
|
||||
#pragma comment (lib, "wmcodecdspuuid.lib")
|
||||
|
||||
#include <grabber/MFGrabber.h>
|
||||
|
||||
#define SAFE_RELEASE(x) if(x) { x->Release(); x = nullptr; }
|
||||
|
||||
// Need more supported formats? Visit https://docs.microsoft.com/en-us/windows/win32/medfound/colorconverter
|
||||
static PixelFormat GetPixelFormatForGuid(const GUID guid)
|
||||
{
|
||||
if (IsEqualGUID(guid, MFVideoFormat_RGB32)) return PixelFormat::RGB32;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::BGR24;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_YUY2)) return PixelFormat::YUYV;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_UYVY)) return PixelFormat::UYVY;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_MJPG)) return PixelFormat::MJPEG;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_NV12)) return PixelFormat::NV12;
|
||||
if (IsEqualGUID(guid, MFVideoFormat_I420)) return PixelFormat::I420;
|
||||
return PixelFormat::NO_CHANGE;
|
||||
};
|
||||
|
||||
class SourceReaderCB : public IMFSourceReaderCallback
|
||||
{
|
||||
public:
|
||||
@ -57,20 +74,53 @@ public:
|
||||
}
|
||||
|
||||
// IMFSourceReaderCallback methods
|
||||
STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex,
|
||||
STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD /*dwStreamIndex*/,
|
||||
DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample)
|
||||
{
|
||||
EnterCriticalSection(&_critsec);
|
||||
|
||||
if(dwStreamFlags & MF_SOURCE_READERF_STREAMTICK)
|
||||
{
|
||||
Debug(_grabber->_log, "Skipping stream gap");
|
||||
LeaveCriticalSection(&_critsec);
|
||||
_grabber->_sourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, NULL, NULL, NULL);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
if (dwStreamFlags & MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED)
|
||||
{
|
||||
IMFMediaType *type = nullptr;
|
||||
GUID format;
|
||||
_grabber->_sourceReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, MF_SOURCE_READER_CURRENT_TYPE_INDEX, &type);
|
||||
type->GetGUID(MF_MT_SUBTYPE, &format);
|
||||
Debug(_grabber->_log, "Native media type changed");
|
||||
InitializeVideoEncoder(type, GetPixelFormatForGuid(format));
|
||||
SAFE_RELEASE(type);
|
||||
}
|
||||
|
||||
if (dwStreamFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
|
||||
{
|
||||
IMFMediaType *type = nullptr;
|
||||
GUID format;
|
||||
_grabber->_sourceReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &type);
|
||||
type->GetGUID(MF_MT_SUBTYPE, &format);
|
||||
Debug(_grabber->_log, "Current media type changed");
|
||||
InitializeVideoEncoder(type, GetPixelFormatForGuid(format));
|
||||
SAFE_RELEASE(type);
|
||||
}
|
||||
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
QString error = "";
|
||||
bool frameSend = false;
|
||||
|
||||
if (pSample)
|
||||
{
|
||||
IMFMediaBuffer* buffer;
|
||||
//variables declaration
|
||||
IMFMediaBuffer* buffer = nullptr, *mediaBuffer = nullptr;
|
||||
IMFSample* sampleOut = nullptr;
|
||||
|
||||
if(_pixelformat == PixelFormat::MJPEG || _pixelformat == PixelFormat::NO_CHANGE)
|
||||
{
|
||||
hrStatus = pSample->ConvertToContiguousBuffer(&buffer);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
@ -80,25 +130,89 @@ public:
|
||||
hrStatus = buffer->Lock(&data, &maxLength, ¤tLength);
|
||||
if(SUCCEEDED(hrStatus))
|
||||
{
|
||||
frameSend = true;
|
||||
_grabber->receive_image(data,currentLength,error);
|
||||
|
||||
_grabber->receive_image(data,currentLength);
|
||||
buffer->Unlock();
|
||||
}
|
||||
else
|
||||
error = QString("buffer->Lock failed => %1").arg(hrStatus);
|
||||
|
||||
}
|
||||
else
|
||||
error = QString("pSample->ConvertToContiguousBuffer failed => %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send our input sample to the transform
|
||||
_transform->ProcessInput(0, pSample, 0);
|
||||
|
||||
MFT_OUTPUT_STREAM_INFO streamInfo;
|
||||
hrStatus = _transform->GetOutputStreamInfo(0, &streamInfo);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
hrStatus = MFCreateMemoryBuffer(streamInfo.cbSize, &buffer);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
hrStatus = MFCreateSample(&sampleOut);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
hrStatus = sampleOut->AddBuffer(buffer);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
MFT_OUTPUT_DATA_BUFFER outputDataBuffer = {0};
|
||||
memset(&outputDataBuffer, 0, sizeof outputDataBuffer);
|
||||
outputDataBuffer.dwStreamID = 0;
|
||||
outputDataBuffer.dwStatus = 0;
|
||||
outputDataBuffer.pEvents = nullptr;
|
||||
outputDataBuffer.pSample = sampleOut;
|
||||
|
||||
DWORD status = 0;
|
||||
hrStatus = _transform->ProcessOutput(0, 1, &outputDataBuffer, &status);
|
||||
if (hrStatus == MF_E_TRANSFORM_NEED_MORE_INPUT)
|
||||
{
|
||||
SAFE_RELEASE(sampleOut);
|
||||
SAFE_RELEASE(buffer);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
hrStatus = sampleOut->ConvertToContiguousBuffer(&mediaBuffer);
|
||||
if (SUCCEEDED(hrStatus))
|
||||
{
|
||||
BYTE* data = nullptr;
|
||||
DWORD currentLength = 0;
|
||||
|
||||
hrStatus = mediaBuffer->Lock(&data, 0, ¤tLength);
|
||||
if(SUCCEEDED(hrStatus))
|
||||
{
|
||||
_grabber->receive_image(data, currentLength);
|
||||
mediaBuffer->Unlock();
|
||||
}
|
||||
else
|
||||
error = QString("mediaBuffer->Lock failed => %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("sampleOut->ConvertToContiguousBuffer failed => %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("AddBuffer failed %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("MFCreateSample failed %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("MFCreateMemoryBuffer failed %1").arg(hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("GetOutputStreamInfo failed %1").arg(hrStatus);
|
||||
}
|
||||
|
||||
SAFE_RELEASE(buffer);
|
||||
SAFE_RELEASE(mediaBuffer);
|
||||
SAFE_RELEASE(sampleOut);
|
||||
}
|
||||
else
|
||||
error = "pSample is NULL";
|
||||
|
||||
if (!frameSend)
|
||||
_grabber->receive_image(NULL,0,error);
|
||||
if (!error.isEmpty())
|
||||
Error(_grabber->_log, "%s", QSTRING_CSTR(error));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -111,12 +225,108 @@ public:
|
||||
// Reached the end of the stream.
|
||||
_bEOS = TRUE;
|
||||
}
|
||||
|
||||
_hrStatus = hrStatus;
|
||||
|
||||
LeaveCriticalSection(&_critsec);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT SourceReaderCB::InitializeVideoEncoder(IMFMediaType* type, PixelFormat format)
|
||||
{
|
||||
_pixelformat = format;
|
||||
if (format == PixelFormat::MJPEG || format == PixelFormat::NO_CHANGE)
|
||||
return S_OK;
|
||||
|
||||
// Variable declaration
|
||||
IMFMediaType *output = nullptr;
|
||||
DWORD mftStatus = 0;
|
||||
QString error = "";
|
||||
|
||||
// Create instance of IMFTransform interface pointer as CColorConvertDMO
|
||||
if (SUCCEEDED(CoCreateInstance(CLSID_CColorConvertDMO, nullptr, CLSCTX_INPROC_SERVER, IID_IMFTransform, (void**)&_transform)))
|
||||
{
|
||||
// Set input type as media type of our input stream
|
||||
_hrStatus = _transform->SetInputType(0, type, 0);
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
// Create new media type
|
||||
_hrStatus = MFCreateMediaType(&output);
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
// Copy data from input type to output type
|
||||
_hrStatus = type->CopyAllItems(output);
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
UINT32 width, height;
|
||||
UINT32 numerator, denominator;
|
||||
|
||||
// Fill the missing attributes
|
||||
output->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
|
||||
output->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB24);
|
||||
output->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, TRUE);
|
||||
output->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
|
||||
output->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
|
||||
MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width, &height);
|
||||
MFSetAttributeSize(output, MF_MT_FRAME_SIZE, width, height);
|
||||
MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator);
|
||||
MFSetAttributeRatio(output, MF_MT_FRAME_RATE, numerator, denominator);
|
||||
MFSetAttributeRatio(output, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
|
||||
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
// Set transform output type
|
||||
_hrStatus = _transform->SetOutputType(0, output, 0);
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
// Check if encoder parameters set properly
|
||||
_hrStatus = _transform->GetInputStatus(0, &mftStatus);
|
||||
if (SUCCEEDED(_hrStatus))
|
||||
{
|
||||
if (MFT_INPUT_STATUS_ACCEPT_DATA == mftStatus)
|
||||
{
|
||||
// Notify the transform we are about to begin streaming data
|
||||
if (FAILED(_transform->ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)) ||
|
||||
FAILED(_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)) ||
|
||||
FAILED(_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0)))
|
||||
{
|
||||
error = QString("ProcessMessage failed %1").arg(_hrStatus);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_hrStatus = S_FALSE;
|
||||
error = QString("GetInputStatus failed %1").arg(_hrStatus);
|
||||
}
|
||||
}
|
||||
else
|
||||
error = QString("GetInputStatus failed %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("SetOutputType failed %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("Can not set output attributes %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("CopyAllItems failed %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("MFCreateMediaType failed %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("SetInputType failed %1").arg(_hrStatus);
|
||||
}
|
||||
else
|
||||
error = QString("CoCreateInstance failed %1").arg(_hrStatus);
|
||||
|
||||
if (!error.isEmpty())
|
||||
Error(_grabber->_log, "%s", QSTRING_CSTR(error));
|
||||
|
||||
SAFE_RELEASE(output);
|
||||
return _hrStatus;
|
||||
}
|
||||
|
||||
STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *) { return S_OK; }
|
||||
STDMETHODIMP OnFlush(DWORD) { return S_OK; }
|
||||
|
||||
@ -130,4 +340,6 @@ private:
|
||||
MFGrabber* _grabber;
|
||||
BOOL _bEOS;
|
||||
HRESULT _hrStatus;
|
||||
IMFTransform* _transform;
|
||||
PixelFormat _pixelformat;
|
||||
};
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "grabber/MFThread.h"
|
||||
#include <QDebug>
|
||||
|
||||
volatile bool MFThread::_isActive = false;
|
||||
|
||||
@ -52,7 +51,7 @@ void MFThread::setup(
|
||||
_imageResampler.setCropping(cropLeft, cropRight, cropTop, cropBottom);
|
||||
_imageResampler.setHorizontalPixelDecimation(_pixelDecimation);
|
||||
_imageResampler.setVerticalPixelDecimation(_pixelDecimation);
|
||||
_imageResampler.setFlipMode(FlipMode::HORIZONTAL);
|
||||
_imageResampler.setFlipMode(FlipMode::NO_CHANGE);
|
||||
|
||||
if (size > _localDataSize)
|
||||
{
|
||||
@ -79,7 +78,7 @@ void MFThread::run()
|
||||
else
|
||||
{
|
||||
Image<ColorRgb> image = Image<ColorRgb>();
|
||||
_imageResampler.processImage(_localData, _width, _height, _lineLength, _pixelFormat, image);
|
||||
_imageResampler.processImage(_localData, _width, _height, _lineLength, PixelFormat::BGR24, image);
|
||||
emit newFrame(_workerIndex, image, _currentFrame);
|
||||
}
|
||||
}
|
||||
|
@ -57,8 +57,8 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
|
||||
}
|
||||
else if (pixelFormat == PixelFormat::I420)
|
||||
{
|
||||
uOffset = height * lineLength + ((ySource * lineLength) / 4);
|
||||
vOffset = ((5 * height * lineLength) * 4) + ((ySource * lineLength) / 4);
|
||||
uOffset = (lineLength * (5 * height + ySource) / 4);
|
||||
vOffset = (lineLength * (4 * height + ySource)) * 4;
|
||||
}
|
||||
|
||||
for (int xDest = 0, xSource = _cropLeft + (_horizontalDecimation >> 1); xDest < outputWidth; xSource += _horizontalDecimation, ++xDest)
|
||||
@ -150,8 +150,8 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
|
||||
{
|
||||
int index = yOffset + xSource;
|
||||
uint8_t y = data[index];
|
||||
uint8_t u = data[uOffset + (xSource >> 1)];
|
||||
uint8_t v = data[vOffset + (xSource >> 1)];
|
||||
uint8_t u = data[uOffset + xSource];
|
||||
uint8_t v = data[vOffset + xSource];
|
||||
ColorSys::yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
|
||||
break;
|
||||
}
|
||||
|
@ -87,12 +87,33 @@ void SysTray::createTrayIcon()
|
||||
_trayIconEfxMenu = new QMenu(_trayIconMenu);
|
||||
_trayIconEfxMenu->setTitle(tr("Effects"));
|
||||
_trayIconEfxMenu->setIcon(QPixmap(":/effects.svg"));
|
||||
|
||||
// custom effects
|
||||
for (auto efx : efxs)
|
||||
{
|
||||
if (efx.file.mid(0, 1) != ":")
|
||||
{
|
||||
qDebug() << efx.file;
|
||||
QAction *efxAction = new QAction(efx.name, this);
|
||||
connect(efxAction, SIGNAL(triggered()), this, SLOT(setEffect()));
|
||||
_trayIconEfxMenu->addAction(efxAction);
|
||||
}
|
||||
}
|
||||
|
||||
// add seperator if custom effects exists
|
||||
if (!_trayIconEfxMenu->isEmpty())
|
||||
_trayIconEfxMenu->addSeparator();
|
||||
|
||||
// build in effects
|
||||
for (auto efx : efxs)
|
||||
{
|
||||
if (efx.file.mid(0, 1) == ":")
|
||||
{
|
||||
QAction *efxAction = new QAction(efx.name, this);
|
||||
connect(efxAction, SIGNAL(triggered()), this, SLOT(setEffect()));
|
||||
_trayIconEfxMenu->addAction(efxAction);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
autorunAction = new QAction(tr("&Disable autostart"), this);
|
||||
|
Loading…
x
Reference in New Issue
Block a user