@@ -971,7 +971,8 @@ with the fourcc and size attributes to apply directly to the capture device
node. The fourcc attribute is a `V4L2PixelFormat`_ and differs from the
``libcamera::PixelFormat``. Converting the format requires knowledge of the
plane configuration for multiplanar formats, so you must explicitly convert it
-using the helper ``V4L2PixelFormat::fromPixelFormat()``.
+using the helper ``V4L2VideoDevice::toV4L2PixelFormat()`` provided by the
+V4L2VideoDevice instance that the format will be applied on.
.. _V4L2DeviceFormat: https://libcamera.org/api-html/classlibcamera_1_1V4L2DeviceFormat.html
.. _V4L2PixelFormat: https://libcamera.org/api-html/classlibcamera_1_1V4L2PixelFormat.html
@@ -981,7 +982,7 @@ Add the following code beneath the code from above:
.. code-block:: cpp
V4L2DeviceFormat format = {};
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
Set the video device format defined above using the
@@ -1001,7 +1002,7 @@ Continue the implementation with the following code:
return ret;
if (format.size != cfg.size ||
- format.fourcc != V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat))
+ format.fourcc != data->video_->toV4L2PixelFormat(cfg.pixelFormat))
return -EINVAL;
Finally, store and set stream-specific data reflecting the state of the stream.
@@ -228,6 +228,8 @@ public:
static std::unique_ptr<V4L2VideoDevice>
fromEntityName(const MediaDevice *media, const std::string &entity);
+ V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat) const;
+
protected:
std::string logPrefix() const override;
@@ -203,7 +203,7 @@ int CIO2Device::configure(const Size &size, V4L2DeviceFormat *outputFormat)
if (itInfo == mbusCodesToPixelFormat.end())
return -EINVAL;
- outputFormat->fourcc = V4L2PixelFormat::fromPixelFormat(itInfo->second);
+ outputFormat->fourcc = output_->toV4L2PixelFormat(itInfo->second);
outputFormat->size = sensorFormat.size;
outputFormat->planesCount = 1;
@@ -558,7 +558,7 @@ int ImgUDevice::configureVideoDevice(V4L2VideoDevice *dev, unsigned int pad,
return 0;
*outputFormat = {};
- outputFormat->fourcc = V4L2PixelFormat::fromPixelFormat(formats::NV12);
+ outputFormat->fourcc = dev->toV4L2PixelFormat(formats::NV12);
outputFormat->size = cfg.size;
outputFormat->planesCount = 2;
@@ -90,13 +90,14 @@ PixelFormat mbusCodeToPixelFormat(unsigned int mbus_code,
return pix;
}
-V4L2DeviceFormat toV4L2DeviceFormat(const V4L2SubdeviceFormat &format,
+V4L2DeviceFormat toV4L2DeviceFormat(const V4L2VideoDevice *dev,
+ const V4L2SubdeviceFormat &format,
BayerFormat::Packing packingReq)
{
const PixelFormat pix = mbusCodeToPixelFormat(format.mbus_code, packingReq);
V4L2DeviceFormat deviceFormat;
- deviceFormat.fourcc = V4L2PixelFormat::fromPixelFormat(pix);
+ deviceFormat.fourcc = dev->toV4L2PixelFormat(pix);
deviceFormat.size = format.size;
deviceFormat.colorSpace = format.colorSpace;
return deviceFormat;
@@ -422,15 +423,15 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()
* Calculate the best sensor mode we can use based on
* the user request.
*/
+ V4L2VideoDevice *unicam = data_->unicam_[Unicam::Image].dev();
const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);
unsigned int bitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth;
V4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size, bitDepth);
BayerFormat::Packing packing = BayerFormat::Packing::CSI2;
if (info.isValid() && !info.packed)
packing = BayerFormat::Packing::None;
- V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorFormat,
- packing);
- int ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&unicamFormat);
+ V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(unicam, sensorFormat, packing);
+ int ret = unicam->tryFormat(&unicamFormat);
if (ret)
return Invalid;
@@ -516,14 +517,14 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()
V4L2VideoDevice::Formats fmts = dev->formats();
- if (fmts.find(V4L2PixelFormat::fromPixelFormat(cfgPixFmt)) == fmts.end()) {
+ if (fmts.find(dev->toV4L2PixelFormat(cfgPixFmt)) == fmts.end()) {
/* If we cannot find a native format, use a default one. */
cfgPixFmt = formats::NV12;
status = Adjusted;
}
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfgPixFmt);
+ format.fourcc = dev->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
format.colorSpace = cfg.colorSpace;
@@ -751,8 +752,9 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)
if (ret)
return ret;
- V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorFormat, packing);
- ret = data->unicam_[Unicam::Image].dev()->setFormat(&unicamFormat);
+ V4L2VideoDevice *unicam = data->unicam_[Unicam::Image].dev();
+ V4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(unicam, sensorFormat, packing);
+ ret = unicam->setFormat(&unicamFormat);
if (ret)
return ret;
@@ -783,7 +785,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)
RPi::Stream *stream = i == maxIndex ? &data->isp_[Isp::Output0]
: &data->isp_[Isp::Output1];
- V4L2PixelFormat fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ V4L2PixelFormat fourcc = stream->dev()->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
format.fourcc = fourcc;
format.colorSpace = cfg.colorSpace;
@@ -826,13 +828,15 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)
* statistics coming from the hardware.
*/
if (!output0Set) {
+ V4L2VideoDevice *dev = data->isp_[Isp::Output0].dev();
+
maxSize = Size(320, 240);
format = {};
format.size = maxSize;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(formats::YUV420);
+ format.fourcc = dev->toV4L2PixelFormat(formats::YUV420);
/* No one asked for output, so the color space doesn't matter. */
format.colorSpace = ColorSpace::Jpeg;
- ret = data->isp_[Isp::Output0].dev()->setFormat(&format);
+ ret = dev->setFormat(&format);
if (ret) {
LOG(RPI, Error)
<< "Failed to set default format on ISP Output0: "
@@ -856,18 +860,20 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)
* colour denoise will not run.
*/
if (!output1Set) {
+ V4L2VideoDevice *dev = data->isp_[Isp::Output1].dev();
+
V4L2DeviceFormat output1Format;
constexpr Size maxDimensions(1200, 1200);
const Size limit = maxDimensions.boundedToAspectRatio(format.size);
output1Format.size = (format.size / 2).boundedTo(limit).alignedDownTo(2, 2);
output1Format.colorSpace = format.colorSpace;
- output1Format.fourcc = V4L2PixelFormat::fromPixelFormat(formats::YUV420);
+ output1Format.fourcc = dev->toV4L2PixelFormat(formats::YUV420);
LOG(RPI, Debug) << "Setting ISP Output1 (internal) to "
<< output1Format;
- ret = data->isp_[Isp::Output1].dev()->setFormat(&output1Format);
+ ret = dev->setFormat(&output1Format);
if (ret) {
LOG(RPI, Error) << "Failed to set format on ISP Output1: "
<< ret;
@@ -81,7 +81,7 @@ CameraConfiguration::Status RkISP1Path::validate(StreamConfiguration *cfg)
cfg->bufferCount = RKISP1_BUFFER_COUNT;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg->pixelFormat);
+ format.fourcc = video_->toV4L2PixelFormat(cfg->pixelFormat);
format.size = cfg->size;
int ret = video_->tryFormat(&format);
@@ -147,7 +147,7 @@ int RkISP1Path::configure(const StreamConfiguration &config,
const PixelFormatInfo &info = PixelFormatInfo::info(config.pixelFormat);
V4L2DeviceFormat outputFormat;
- outputFormat.fourcc = V4L2PixelFormat::fromPixelFormat(config.pixelFormat);
+ outputFormat.fourcc = video_->toV4L2PixelFormat(config.pixelFormat);
outputFormat.size = config.size;
outputFormat.planesCount = info.numPlanes();
@@ -156,7 +156,7 @@ int RkISP1Path::configure(const StreamConfiguration &config,
return ret;
if (outputFormat.size != config.size ||
- outputFormat.fourcc != V4L2PixelFormat::fromPixelFormat(config.pixelFormat)) {
+ outputFormat.fourcc != video_->toV4L2PixelFormat(config.pixelFormat)) {
LOG(RkISP1, Error)
<< "Unable to configure capture in " << config.toString();
return -EINVAL;
@@ -46,7 +46,7 @@ int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg,
const StreamConfiguration &outputCfg)
{
V4L2PixelFormat videoFormat =
- V4L2PixelFormat::fromPixelFormat(inputCfg.pixelFormat);
+ m2m_->output()->toV4L2PixelFormat(inputCfg.pixelFormat);
V4L2DeviceFormat format;
format.fourcc = videoFormat;
@@ -71,7 +71,7 @@ int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg,
}
/* Set the pixel format and size on the output. */
- videoFormat = V4L2PixelFormat::fromPixelFormat(outputCfg.pixelFormat);
+ videoFormat = m2m_->capture()->toV4L2PixelFormat(outputCfg.pixelFormat);
format = {};
format.fourcc = videoFormat;
format.size = outputCfg.size;
@@ -210,7 +210,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
* enumerate the conversion capabilities on its output (V4L2 capture).
*/
V4L2DeviceFormat v4l2Format;
- v4l2Format.fourcc = V4L2PixelFormat::fromPixelFormat(input);
+ v4l2Format.fourcc = m2m_->output()->toV4L2PixelFormat(input);
v4l2Format.size = { 1, 1 };
int ret = m2m_->output()->setFormat(&v4l2Format);
@@ -220,7 +220,7 @@ std::vector<PixelFormat> SimpleConverter::formats(PixelFormat input)
return {};
}
- if (v4l2Format.fourcc != V4L2PixelFormat::fromPixelFormat(input)) {
+ if (v4l2Format.fourcc != m2m_->output()->toV4L2PixelFormat(input)) {
LOG(SimplePipeline, Debug)
<< "Input format " << input << " not supported.";
return {};
@@ -287,7 +287,7 @@ SimpleConverter::strideAndFrameSize(const PixelFormat &pixelFormat,
const Size &size)
{
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(pixelFormat);
+ format.fourcc = m2m_->capture()->toV4L2PixelFormat(pixelFormat);
format.size = size;
int ret = m2m_->capture()->tryFormat(&format);
@@ -918,7 +918,7 @@ CameraConfiguration::Status SimpleCameraConfiguration::validate()
return Invalid;
} else {
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data_->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
int ret = data_->video_->tryFormat(&format);
@@ -1028,7 +1028,7 @@ int SimplePipelineHandler::configure(Camera *camera, CameraConfiguration *c)
return ret;
/* Configure the video node. */
- V4L2PixelFormat videoFormat = V4L2PixelFormat::fromPixelFormat(pipeConfig->captureFormat);
+ V4L2PixelFormat videoFormat = video->toV4L2PixelFormat(pipeConfig->captureFormat);
V4L2DeviceFormat captureFormat;
captureFormat.fourcc = videoFormat;
@@ -149,7 +149,7 @@ CameraConfiguration::Status UVCCameraConfiguration::validate()
cfg.bufferCount = 4;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data_->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
int ret = data_->video_->tryFormat(&format);
@@ -205,7 +205,7 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config)
int ret;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
ret = data->video_->setFormat(&format);
@@ -213,7 +213,7 @@ int PipelineHandlerUVC::configure(Camera *camera, CameraConfiguration *config)
return ret;
if (format.size != cfg.size ||
- format.fourcc != V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat))
+ format.fourcc != data->video_->toV4L2PixelFormat(cfg.pixelFormat))
return -EINVAL;
cfg.setStream(&data->stream_);
@@ -171,7 +171,7 @@ CameraConfiguration::Status VimcCameraConfiguration::validate()
cfg.bufferCount = 4;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data_->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
int ret = data_->video_->tryFormat(&format);
@@ -275,7 +275,7 @@ int PipelineHandlerVimc::configure(Camera *camera, CameraConfiguration *config)
return ret;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);
+ format.fourcc = data->video_->toV4L2PixelFormat(cfg.pixelFormat);
format.size = cfg.size;
ret = data->video_->setFormat(&format);
@@ -283,7 +283,7 @@ int PipelineHandlerVimc::configure(Camera *camera, CameraConfiguration *config)
return ret;
if (format.size != cfg.size ||
- format.fourcc != V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat))
+ format.fourcc != data->video_->toV4L2PixelFormat(cfg.pixelFormat))
return -EINVAL;
/*
@@ -598,7 +598,7 @@ int VimcCameraData::allocateMockIPABuffers()
constexpr unsigned int kBufCount = 2;
V4L2DeviceFormat format;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(formats::BGR888);
+ format.fourcc = video_->toV4L2PixelFormat(formats::BGR888);
format.size = Size (160, 120);
int ret = video_->setFormat(&format);
@@ -1989,6 +1989,20 @@ V4L2VideoDevice::fromEntityName(const MediaDevice *media,
return std::make_unique<V4L2VideoDevice>(mediaEntity);
}
+/**
+ * \brief Convert \a PixelFormat to its corresponding V4L2 FourCC
+ * \param[in] pixelFormat The PixelFormat to convert
+ *
+ * The V4L2 format variant the function returns the contiguous version
+ * unconditionally.
+ *
+ * \return The V4L2_PIX_FMT_* pixel format code corresponding to \a pixelFormat
+ */
+V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const
+{
+ return V4L2PixelFormat::fromPixelFormat(pixelFormat);
+}
+
/**
* \class V4L2M2MDevice
* \brief Memory-to-Memory video device
@@ -72,7 +72,7 @@ int BufferSource::allocate(const StreamConfiguration &config)
}
format.size = config.size;
- format.fourcc = V4L2PixelFormat::fromPixelFormat(config.pixelFormat);
+ format.fourcc = video->toV4L2PixelFormat(config.pixelFormat);
if (video->setFormat(&format)) {
std::cout << "Failed to set format on output device" << std::endl;
return TestFail;