GStreamer: Adjust the camera's manual color temperature through V4L2

GStreamer does not support setup of manual color temperature for the
camera. Now it is implemented through the V4L2 interface, where it is
works together with the GStreamer and covers the GStreamer's holes.

Change-Id: Icaeadeb4e21ec7865bcfa908bead318d4ead8ba5
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
This commit is contained in:
Denis Shienkov
2015-11-07 22:13:20 +03:00
committed by Yoann Lopes
parent 5916caae7a
commit ab403bc9ae
6 changed files with 428 additions and 16 deletions

View File

@@ -89,6 +89,15 @@ config_gstreamer_encodingprofiles {
DEFINES += HAVE_GST_ENCODING_PROFILES
}
config_linux_v4l: {
DEFINES += USE_V4L
HEADERS += \
$$PWD/camerabinv4limageprocessing.h
SOURCES += \
$$PWD/camerabinv4limageprocessing.cpp
}
OTHER_FILES += \
camerabin.json

View File

@@ -34,6 +34,10 @@
#include "camerabinimageprocessing.h"
#include "camerabinsession.h"
#ifdef USE_V4L
#include "camerabinv4limageprocessing.h"
#endif
#if GST_CHECK_VERSION(1,0,0)
# include <gst/video/colorbalance.h>
#else
@@ -43,9 +47,12 @@
QT_BEGIN_NAMESPACE
CameraBinImageProcessing::CameraBinImageProcessing(CameraBinSession *session)
:QCameraImageProcessingControl(session),
m_session(session),
m_whiteBalanceMode(QCameraImageProcessing::WhiteBalanceAuto)
: QCameraImageProcessingControl(session)
, m_session(session)
, m_whiteBalanceMode(QCameraImageProcessing::WhiteBalanceAuto)
#ifdef USE_V4L
, m_v4lImageControl(Q_NULLPTR)
#endif
{
#ifdef HAVE_GST_PHOTOGRAPHY
if (m_session->photography()) {
@@ -83,6 +90,12 @@ CameraBinImageProcessing::CameraBinImageProcessing(CameraBinSession *session)
#endif
#endif
#ifdef USE_V4L
m_v4lImageControl = new CameraBinV4LImageProcessing(m_session);
connect(m_session, &CameraBinSession::statusChanged,
m_v4lImageControl, &CameraBinV4LImageProcessing::updateParametersInfo);
#endif
updateColorBalanceValues();
}
@@ -160,7 +173,7 @@ QCameraImageProcessing::WhiteBalanceMode CameraBinImageProcessing::whiteBalanceM
return m_whiteBalanceMode;
}
void CameraBinImageProcessing::setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceMode mode)
bool CameraBinImageProcessing::setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceMode mode)
{
#ifdef HAVE_GST_PHOTOGRAPHY
if (isWhiteBalanceModeSupported(mode)) {
@@ -172,11 +185,13 @@ void CameraBinImageProcessing::setWhiteBalanceMode(QCameraImageProcessing::White
#endif
{
unlockWhiteBalance();
return true;
}
}
#else
Q_UNUSED(mode);
#endif
return false;
}
bool CameraBinImageProcessing::isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceMode mode) const
@@ -184,7 +199,8 @@ bool CameraBinImageProcessing::isWhiteBalanceModeSupported(QCameraImageProcessin
#ifdef HAVE_GST_PHOTOGRAPHY
return m_mappedWbValues.values().contains(mode);
#else
return mode == QCameraImageProcessing::WhiteBalanceAuto;
Q_UNUSED(mode);
return false;
#endif
}
@@ -192,16 +208,24 @@ bool CameraBinImageProcessing::isParameterSupported(QCameraImageProcessingContro
{
#ifdef HAVE_GST_PHOTOGRAPHY
if (parameter == QCameraImageProcessingControl::WhiteBalancePreset
|| parameter == QCameraImageProcessingControl::ColorFilter)
return m_session->photography();
|| parameter == QCameraImageProcessingControl::ColorFilter) {
if (m_session->photography())
return true;
}
#endif
if (parameter == QCameraImageProcessingControl::Contrast
|| parameter == QCameraImageProcessingControl::Brightness
|| parameter == QCameraImageProcessingControl::Saturation) {
return GST_IS_COLOR_BALANCE(m_session->cameraBin());
if (GST_IS_COLOR_BALANCE(m_session->cameraBin()))
return true;
}
#ifdef USE_V4L
if (m_v4lImageControl->isParameterSupported(parameter))
return true;
#endif
return false;
}
@@ -212,8 +236,23 @@ bool CameraBinImageProcessing::isParameterValueSupported(QCameraImageProcessingC
case BrightnessAdjustment:
case SaturationAdjustment:
return GST_IS_COLOR_BALANCE(m_session->cameraBin()) && qAbs(value.toReal()) <= 1.0;
case WhiteBalancePreset:
return isWhiteBalanceModeSupported(value.value<QCameraImageProcessing::WhiteBalanceMode>());
case WhiteBalancePreset: {
const QCameraImageProcessing::WhiteBalanceMode mode =
value.value<QCameraImageProcessing::WhiteBalanceMode>();
const bool isPhotographyWhiteBalanceSupported = isWhiteBalanceModeSupported(mode);
#ifdef USE_V4L
if (!isPhotographyWhiteBalanceSupported)
return m_v4lImageControl->isParameterValueSupported(parameter, value);
#endif
return isPhotographyWhiteBalanceSupported;
}
case ColorTemperature: {
#ifdef USE_V4L
return m_v4lImageControl->isParameterValueSupported(parameter, value);
#else
return false;
#endif
}
case ColorFilter: {
const QCameraImageProcessing::ColorFilter filter = value.value<QCameraImageProcessing::ColorFilter>();
#ifdef HAVE_GST_PHOTOGRAPHY
@@ -233,8 +272,23 @@ QVariant CameraBinImageProcessing::parameter(
QCameraImageProcessingControl::ProcessingParameter parameter) const
{
switch (parameter) {
case QCameraImageProcessingControl::WhiteBalancePreset:
return QVariant::fromValue<QCameraImageProcessing::WhiteBalanceMode>(whiteBalanceMode());
case QCameraImageProcessingControl::WhiteBalancePreset: {
const QCameraImageProcessing::WhiteBalanceMode mode = whiteBalanceMode();
#ifdef USE_V4L
if (mode == QCameraImageProcessing::WhiteBalanceAuto
|| mode == QCameraImageProcessing::WhiteBalanceManual) {
return m_v4lImageControl->parameter(parameter);
}
#endif
return QVariant::fromValue<QCameraImageProcessing::WhiteBalanceMode>(mode);
}
case QCameraImageProcessingControl::ColorTemperature: {
#ifdef USE_V4L
return m_v4lImageControl->parameter(parameter);
#else
return QVariant();
#endif
}
case QCameraImageProcessingControl::ColorFilter:
#ifdef HAVE_GST_PHOTOGRAPHY
if (GstPhotography *photography = m_session->photography()) {
@@ -269,9 +323,26 @@ void CameraBinImageProcessing::setParameter(QCameraImageProcessingControl::Proce
case SaturationAdjustment:
setColorBalanceValue("saturation", value.toReal());
break;
case WhiteBalancePreset:
setWhiteBalanceMode(value.value<QCameraImageProcessing::WhiteBalanceMode>());
case WhiteBalancePreset: {
if (!setWhiteBalanceMode(value.value<QCameraImageProcessing::WhiteBalanceMode>())) {
#ifdef USE_V4L
const QCameraImageProcessing::WhiteBalanceMode mode =
value.value<QCameraImageProcessing::WhiteBalanceMode>();
if (mode == QCameraImageProcessing::WhiteBalanceAuto
|| mode == QCameraImageProcessing::WhiteBalanceManual) {
m_v4lImageControl->setParameter(parameter, value);
return;
}
#endif
}
}
break;
case QCameraImageProcessingControl::ColorTemperature: {
#ifdef USE_V4L
m_v4lImageControl->setParameter(parameter, value);
#endif
break;
}
case QCameraImageProcessingControl::ColorFilter:
#ifdef HAVE_GST_PHOTOGRAPHY
if (GstPhotography *photography = m_session->photography()) {

View File

@@ -50,6 +50,10 @@ typedef GstColourToneMode GstPhotographyColorToneMode;
QT_BEGIN_NAMESPACE
#ifdef USE_V4L
class CameraBinV4LImageProcessing;
#endif
class CameraBinSession;
class CameraBinImageProcessing : public QCameraImageProcessingControl
@@ -61,7 +65,7 @@ public:
virtual ~CameraBinImageProcessing();
QCameraImageProcessing::WhiteBalanceMode whiteBalanceMode() const;
void setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceMode mode);
bool setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceMode mode);
bool isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceMode mode) const;
bool isParameterSupported(ProcessingParameter) const;
@@ -86,6 +90,10 @@ private:
QMap<QCameraImageProcessing::ColorFilter, GstPhotographyColorToneMode> m_filterMap;
#endif
QCameraImageProcessing::WhiteBalanceMode m_whiteBalanceMode;
#ifdef USE_V4L
CameraBinV4LImageProcessing *m_v4lImageControl;
#endif
};
QT_END_NAMESPACE

View File

@@ -153,6 +153,8 @@ public:
bool isMuted() const;
QString device() const { return m_inputDevice; }
bool processSyncMessage(const QGstreamerMessage &message);
bool processBusMessage(const QGstreamerMessage &message);

View File

@@ -0,0 +1,242 @@
/****************************************************************************
**
** Copyright (C) 2015 Denis Shienkov <denis.shienkov@gmail.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "camerabinv4limageprocessing.h"
#include "camerabinsession.h"
#include <QDebug>
#include <private/qcore_unix_p.h>
#include <linux/videodev2.h>
QT_BEGIN_NAMESPACE
CameraBinV4LImageProcessing::CameraBinV4LImageProcessing(CameraBinSession *session)
: QCameraImageProcessingControl(session)
, m_session(session)
{
}
CameraBinV4LImageProcessing::~CameraBinV4LImageProcessing()
{
}
bool CameraBinV4LImageProcessing::isParameterSupported(
ProcessingParameter parameter) const
{
return m_parametersInfo.contains(parameter);
}
bool CameraBinV4LImageProcessing::isParameterValueSupported(
ProcessingParameter parameter, const QVariant &value) const
{
QMap<ProcessingParameter, SourceParameterValueInfo>::const_iterator sourceValueInfo =
m_parametersInfo.constFind(parameter);
if (sourceValueInfo == m_parametersInfo.constEnd())
return false;
switch (parameter) {
case QCameraImageProcessingControl::WhiteBalancePreset: {
const QCameraImageProcessing::WhiteBalanceMode checkedValue =
value.value<QCameraImageProcessing::WhiteBalanceMode>();
const QCameraImageProcessing::WhiteBalanceMode firstAllowedValue =
(*sourceValueInfo).minimumValue ? QCameraImageProcessing::WhiteBalanceAuto
: QCameraImageProcessing::WhiteBalanceManual;
const QCameraImageProcessing::WhiteBalanceMode secondAllowedValue =
(*sourceValueInfo).maximumValue ? QCameraImageProcessing::WhiteBalanceAuto
: QCameraImageProcessing::WhiteBalanceManual;
if (checkedValue != firstAllowedValue
&& checkedValue != secondAllowedValue) {
return false;
}
}
break;
case QCameraImageProcessingControl::ColorTemperature: {
const qint32 checkedValue = value.toInt();
if (checkedValue < (*sourceValueInfo).minimumValue
|| checkedValue > (*sourceValueInfo).maximumValue) {
return false;
}
}
break;
default:
return false;
}
return true;
}
QVariant CameraBinV4LImageProcessing::parameter(
ProcessingParameter parameter) const
{
QMap<ProcessingParameter, SourceParameterValueInfo>::const_iterator sourceValueInfo =
m_parametersInfo.constFind(parameter);
if (sourceValueInfo == m_parametersInfo.constEnd()) {
qWarning() << "Unable to get the parameter value: the parameter is not supported.";
return QVariant();
}
const QString deviceName = m_session->device();
const int fd = qt_safe_open(deviceName.toLocal8Bit().constData(), O_RDONLY);
if (fd == -1) {
qWarning() << "Unable to open the camera" << deviceName
<< "for read to get the parameter value:" << qt_error_string(errno);
return QVariant();
}
struct v4l2_control control;
::memset(&control, 0, sizeof(control));
control.id = (*sourceValueInfo).cid;
const bool ret = (::ioctl(fd, VIDIOC_G_CTRL, &control) == 0);
qt_safe_close(fd);
if (!ret) {
qWarning() << "Unable to get the parameter value:" << qt_error_string(errno);
return QVariant();
}
switch (parameter) {
case QCameraImageProcessingControl::WhiteBalancePreset:
return QVariant::fromValue<QCameraImageProcessing::WhiteBalanceMode>(
control.value ? QCameraImageProcessing::WhiteBalanceAuto
: QCameraImageProcessing::WhiteBalanceManual);
case QCameraImageProcessingControl::ColorTemperature:
return QVariant::fromValue<qint32>(control.value);
default:
return QVariant();
}
}
void CameraBinV4LImageProcessing::setParameter(
ProcessingParameter parameter, const QVariant &value)
{
QMap<ProcessingParameter, SourceParameterValueInfo>::const_iterator sourceValueInfo =
m_parametersInfo.constFind(parameter);
if (sourceValueInfo == m_parametersInfo.constEnd()) {
qWarning() << "Unable to set the parameter value: the parameter is not supported.";
return;
}
const QString deviceName = m_session->device();
const int fd = qt_safe_open(deviceName.toLocal8Bit().constData(), O_WRONLY);
if (fd == -1) {
qWarning() << "Unable to open the camera" << deviceName
<< "for write to set the parameter value:" << qt_error_string(errno);
return;
}
struct v4l2_control control;
::memset(&control, 0, sizeof(control));
control.id = (*sourceValueInfo).cid;
switch (parameter) {
case QCameraImageProcessingControl::WhiteBalancePreset: {
const QCameraImageProcessing::WhiteBalanceMode m =
value.value<QCameraImageProcessing::WhiteBalanceMode>();
if (m != QCameraImageProcessing::WhiteBalanceAuto
&& m != QCameraImageProcessing::WhiteBalanceManual)
return;
control.value = (m == QCameraImageProcessing::WhiteBalanceAuto) ? true : false;
}
break;
case QCameraImageProcessingControl::ColorTemperature:
control.value = value.toInt();
break;
default:
return;
}
if (::ioctl(fd, VIDIOC_S_CTRL, &control) != 0)
qWarning() << "Unable to set the parameter value:" << qt_error_string(errno);
qt_safe_close(fd);
}
void CameraBinV4LImageProcessing::updateParametersInfo(
QCamera::Status cameraStatus)
{
if (cameraStatus == QCamera::UnloadedStatus)
m_parametersInfo.clear();
else if (cameraStatus == QCamera::LoadedStatus) {
const QString deviceName = m_session->device();
const int fd = qt_safe_open(deviceName.toLocal8Bit().constData(), O_RDONLY);
if (fd == -1) {
qWarning() << "Unable to open the camera" << deviceName
<< "for read to query the parameter info:" << qt_error_string(errno);
return;
}
static const struct SupportedParameterEntry {
quint32 cid;
QCameraImageProcessingControl::ProcessingParameter parameter;
} supportedParametersEntries[] = {
{ V4L2_CID_AUTO_WHITE_BALANCE, QCameraImageProcessingControl::WhiteBalancePreset },
{ V4L2_CID_WHITE_BALANCE_TEMPERATURE, QCameraImageProcessingControl::ColorTemperature }
};
for (int i = 0; i < int(sizeof(supportedParametersEntries) / sizeof(SupportedParameterEntry)); ++i) {
struct v4l2_queryctrl queryControl;
::memset(&queryControl, 0, sizeof(queryControl));
queryControl.id = supportedParametersEntries[i].cid;
if (::ioctl(fd, VIDIOC_QUERYCTRL, &queryControl) != 0) {
qWarning() << "Unable to query the parameter info:" << qt_error_string(errno);
continue;
}
SourceParameterValueInfo sourceValueInfo;
sourceValueInfo.cid = queryControl.id;
sourceValueInfo.defaultValue = queryControl.default_value;
sourceValueInfo.maximumValue = queryControl.maximum;
sourceValueInfo.minimumValue = queryControl.minimum;
m_parametersInfo.insert(supportedParametersEntries[i].parameter, sourceValueInfo);
}
qt_safe_close(fd);
}
}
QT_END_NAMESPACE

View File

@@ -0,0 +1,80 @@
/****************************************************************************
**
** Copyright (C) 2015 Denis Shienkov <denis.shienkov@gmail.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef CAMERABINV4LIMAGEPROCESSINGCONTROL_H
#define CAMERABINV4LIMAGEPROCESSINGCONTROL_H
#include <qcamera.h>
#include <qcameraimageprocessingcontrol.h>
QT_BEGIN_NAMESPACE
class CameraBinSession;
class CameraBinV4LImageProcessing : public QCameraImageProcessingControl
{
Q_OBJECT
public:
CameraBinV4LImageProcessing(CameraBinSession *session);
virtual ~CameraBinV4LImageProcessing();
bool isParameterSupported(ProcessingParameter) const;
bool isParameterValueSupported(ProcessingParameter parameter, const QVariant &value) const;
QVariant parameter(ProcessingParameter parameter) const;
void setParameter(ProcessingParameter parameter, const QVariant &value);
public slots:
void updateParametersInfo(QCamera::Status cameraStatus);
private:
struct SourceParameterValueInfo {
SourceParameterValueInfo()
: cid(0)
{
}
qint32 defaultValue;
qint32 minimumValue;
qint32 maximumValue;
quint32 cid; // V4L control id
};
private:
CameraBinSession *m_session;
QMap<ProcessingParameter, SourceParameterValueInfo> m_parametersInfo;
};
QT_END_NAMESPACE
#endif // CAMERABINV4LIMAGEPROCESSINGCONTROL_H