Commit 37feab74 authored by Thiago Santini's avatar Thiago Santini

Adds yuyv/yuy2 suport

parent 5e6d0a7c
...@@ -164,15 +164,21 @@ void UVCCameraSession::callback(uvc_frame_t *frame) ...@@ -164,15 +164,21 @@ void UVCCameraSession::callback(uvc_frame_t *frame)
QMutexLocker locker(&surfaceMutex); QMutexLocker locker(&surfaceMutex);
if (m_surface) { if (m_surface) {
QVideoFrame qFrame; QVideoFrame qFrame;
switch(frame->frame_format) { switch(frame->frame_format) {
case UVC_FRAME_FORMAT_MJPEG: case UVC_FRAME_FORMAT_MJPEG:
qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), 0, QVideoFrame::Format_Jpeg); qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), 0, QVideoFrame::Format_Jpeg);
qFrame.map(QAbstractVideoBuffer::WriteOnly); qFrame.map(QAbstractVideoBuffer::WriteOnly);
memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now
qFrame.unmap(); qFrame.unmap();
break; break;
default: case UVC_FRAME_FORMAT_YUYV:
qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), 0, QVideoFrame::Format_YUYV);
qFrame.map(QAbstractVideoBuffer::WriteOnly);
memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now
qFrame.unmap();
break;
default:
return; return;
} }
#ifdef BLOCKING_FRAME_GRABBING #ifdef BLOCKING_FRAME_GRABBING
...@@ -290,9 +296,9 @@ bool UVCCameraSession::startPreview() ...@@ -290,9 +296,9 @@ bool UVCCameraSession::startPreview()
} }
#ifdef USE_CUSTOM_CALLBACK #ifdef USE_CUSTOM_CALLBACK
res = uvc_stream_start(strmh, NULL, (void*) this, bandwidthFactor, 0); res = uvc_stream_start(strmh, NULL, (void*) this, getBandwidthFactor(), 0);
#else #else
res = uvc_stream_start(strmh, cb, (void*) this, bandwidthFactor, 0); res = uvc_stream_start(strmh, cb, (void*) this, getBandwidthFactor(), 0);
#endif #endif
if (res != UVC_SUCCESS) { if (res != UVC_SUCCESS) {
qWarning() << "uvc_stream_start" << uvc_strerror(res); qWarning() << "uvc_stream_start" << uvc_strerror(res);
...@@ -308,16 +314,42 @@ bool UVCCameraSession::startPreview() ...@@ -308,16 +314,42 @@ bool UVCCameraSession::startPreview()
return true; return true;
} }
double UVCCameraSession::getBandwidthFactor()
{
/*
* This really is the bpp. For reference see:
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
*
* In general:
* 4:4:4 Formats, 32 Bits per Pixel
* 4:2:2 Formats, 16 Bits per Pixel
* 4:2:0 Formats, 16 Bits per Pixel
* 4:2:0 Formats, 12 Bits per Pixel
*/
switch(settings.pixelFormat()) {
// compressed; user can change it through the default settings
case QVideoFrame::Format_Jpeg:
return bandwidthFactor;
// 4:2:2
case QVideoFrame::Format_YUYV:
case QVideoFrame::Format_UYVY:
return 2;
// assume worst case if we don't know it
default:
return 4.0;
}
}
bool UVCCameraSession::qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat) bool UVCCameraSession::qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat)
{ {
switch(qFormat) { switch(qFormat) {
case QVideoFrame::Format_Jpeg: case QVideoFrame::Format_Jpeg:
uvcFormat = UVC_FRAME_FORMAT_MJPEG; uvcFormat = UVC_FRAME_FORMAT_MJPEG;
return true; return true;
// TODO: support for YUYV. See: case QVideoFrame::Format_YUYV:
// bDescriptorSubtype (UVC_VS_FORMAT_UNCOMPRESSED) uvcFormat = UVC_FRAME_FORMAT_YUYV;
// guidFormat return true;
default: default:
return false; return false;
} }
} }
...@@ -332,12 +364,19 @@ void UVCCameraSession::updateSourceCapabilities() ...@@ -332,12 +364,19 @@ void UVCCameraSession::updateSourceCapabilities()
const uvc_format_desc_t* formatDesc = uvc_get_format_descs(devh); const uvc_format_desc_t* formatDesc = uvc_get_format_descs(devh);
while (formatDesc != NULL) { while (formatDesc != NULL) {
QVideoFrame::PixelFormat qFormat; QVideoFrame::PixelFormat qFormat;
switch (formatDesc->bDescriptorSubtype) { switch (formatDesc->bDescriptorSubtype) {
case UVC_VS_FORMAT_MJPEG: case UVC_VS_FORMAT_MJPEG:
qFormat = QVideoFrame::Format_Jpeg; qFormat = QVideoFrame::Format_Jpeg;
break; break;
default: // format not supported, next! case UVC_VS_FORMAT_UNCOMPRESSED:
qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, QString());
if ( qFormat != QVideoFrame::Format_Invalid )
break;
default: // format not supported, next!
QString formatStr;
qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, formatStr);
qInfo() << "UVCEngine: format not supported:" << formatDesc->bDescriptorSubtype << formatStr;
formatDesc = formatDesc->next; formatDesc = formatDesc->next;
continue; continue;
} }
...@@ -435,7 +474,6 @@ bool UVCCameraSession::isImageProcessingParameterValueSupported( QCameraImagePro ...@@ -435,7 +474,6 @@ bool UVCCameraSession::isImageProcessingParameterValueSupported( QCameraImagePro
QVariant UVCCameraSession::imageProcessingParameter( QCameraImageProcessingControl::ProcessingParameter parameter) const QVariant UVCCameraSession::imageProcessingParameter( QCameraImageProcessingControl::ProcessingParameter parameter) const
{ {
//qDebug() << parameter;
switch (parameter) { switch (parameter) {
case QCameraImageProcessingControl::BrightnessAdjustment: case QCameraImageProcessingControl::BrightnessAdjustment:
return uvc_get(devh, uvc_get_brightness); return uvc_get(devh, uvc_get_brightness);
...@@ -455,7 +493,6 @@ QVariant UVCCameraSession::imageProcessingParameter( QCameraImageProcessingContr ...@@ -455,7 +493,6 @@ QVariant UVCCameraSession::imageProcessingParameter( QCameraImageProcessingContr
void UVCCameraSession::setImageProcessingParameter( QCameraImageProcessingControl::ProcessingParameter parameter, const QVariant &value) void UVCCameraSession::setImageProcessingParameter( QCameraImageProcessingControl::ProcessingParameter parameter, const QVariant &value)
{ {
//qDebug() << parameter;
switch (parameter) { switch (parameter) {
case QCameraImageProcessingControl::BrightnessAdjustment: case QCameraImageProcessingControl::BrightnessAdjustment:
uvc_set(devh, uvc_set_brightness, value, uvc_get_brightness); uvc_set(devh, uvc_set_brightness, value, uvc_get_brightness);
......
...@@ -161,6 +161,7 @@ private: ...@@ -161,6 +161,7 @@ private:
void updateSourceCapabilities(); void updateSourceCapabilities();
QCameraViewfinderSettings settings; QCameraViewfinderSettings settings;
bool qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat); bool qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat);
double getBandwidthFactor();
static QMutex sessionMutex; static QMutex sessionMutex;
...@@ -172,6 +173,19 @@ private: ...@@ -172,6 +173,19 @@ private:
std::map<QString,DefaultParameters> defaults; std::map<QString,DefaultParameters> defaults;
void setDefaultParameters(); void setDefaultParameters();
QVideoFrame::PixelFormat fourcc2PixelFormat(const uint8_t *fourcc, QString &str) {
str = QString("%1%2%3%4").arg(
(char) fourcc[0]).arg(
(char) fourcc[1]).arg(
(char) fourcc[2]).arg(
(char) fourcc[3]);
if ( str.compare("yuyv", Qt::CaseInsensitive) == 0 )
return QVideoFrame::Format_YUYV;
if ( str.compare("yuy2", Qt::CaseInsensitive) == 0 ) // duplicate of yuyv http://www.fourcc.org/pixel-format/yuv-yuy2/
return QVideoFrame::Format_YUYV;
return QVideoFrame::Format_Invalid;
}
}; };
QT_END_NAMESPACE QT_END_NAMESPACE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment