libcamera: v4l2_subdevice: Extend [gs]etFormat() to specify format type

The V4L2 subdevice API exposes ACTIVE and TRY formats, but the
V4L2Subdevice getFormat() and setFormat() operations only apply on
ACTIVE formats. Extend them to support TRY formats as well.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2020-03-09 01:22:08 +02:00
parent 79ce121b6b
commit 639cc1ad7a
2 changed files with 30 additions and 6 deletions

View file

@ -32,6 +32,11 @@ struct V4L2SubdeviceFormat {
class V4L2Subdevice : public V4L2Device class V4L2Subdevice : public V4L2Device
{ {
public: public:
enum Whence {
ActiveFormat,
TryFormat,
};
explicit V4L2Subdevice(const MediaEntity *entity); explicit V4L2Subdevice(const MediaEntity *entity);
V4L2Subdevice(const V4L2Subdevice &) = delete; V4L2Subdevice(const V4L2Subdevice &) = delete;
V4L2Subdevice &operator=(const V4L2Subdevice &) = delete; V4L2Subdevice &operator=(const V4L2Subdevice &) = delete;
@ -46,8 +51,10 @@ public:
ImageFormats formats(unsigned int pad); ImageFormats formats(unsigned int pad);
int getFormat(unsigned int pad, V4L2SubdeviceFormat *format); int getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format); Whence whence = ActiveFormat);
int setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
Whence whence = ActiveFormat);
static V4L2Subdevice *fromEntityName(const MediaDevice *media, static V4L2Subdevice *fromEntityName(const MediaDevice *media,
const std::string &entity); const std::string &entity);

View file

@ -95,6 +95,15 @@ const std::string V4L2SubdeviceFormat::toString() const
* any device left open will be closed, and any resources released. * any device left open will be closed, and any resources released.
*/ */
/**
* \enum V4L2Subdevice::Whence
* \brief Specify the type of format for getFormat() and setFormat() operations
* \var V4L2Subdevice::ActiveFormat
* \brief The format operation applies to ACTIVE formats
* \var V4L2Subdevice::TryFormat
* \brief The format operation applies to TRY formats
*/
/** /**
* \brief Create a V4L2 subdevice from a MediaEntity using its device node * \brief Create a V4L2 subdevice from a MediaEntity using its device node
* path * path
@ -184,12 +193,16 @@ ImageFormats V4L2Subdevice::formats(unsigned int pad)
* \brief Retrieve the image format set on one of the V4L2 subdevice pads * \brief Retrieve the image format set on one of the V4L2 subdevice pads
* \param[in] pad The 0-indexed pad number the format is to be retrieved from * \param[in] pad The 0-indexed pad number the format is to be retrieved from
* \param[out] format The image bus format * \param[out] format The image bus format
* \param[in] whence The format to get, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
* \return 0 on success or a negative error code otherwise * \return 0 on success or a negative error code otherwise
*/ */
int V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format) int V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
Whence whence)
{ {
struct v4l2_subdev_format subdevFmt = {}; struct v4l2_subdev_format subdevFmt = {};
subdevFmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; subdevFmt.which = whence == ActiveFormat ? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY;
subdevFmt.pad = pad; subdevFmt.pad = pad;
int ret = ioctl(VIDIOC_SUBDEV_G_FMT, &subdevFmt); int ret = ioctl(VIDIOC_SUBDEV_G_FMT, &subdevFmt);
@ -211,6 +224,8 @@ int V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format)
* \brief Set an image format on one of the V4L2 subdevice pads * \brief Set an image format on one of the V4L2 subdevice pads
* \param[in] pad The 0-indexed pad number the format is to be applied to * \param[in] pad The 0-indexed pad number the format is to be applied to
* \param[inout] format The image bus format to apply to the subdevice's pad * \param[inout] format The image bus format to apply to the subdevice's pad
* \param[in] whence The format to set, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
* *
* Apply the requested image format to the desired media pad and return the * Apply the requested image format to the desired media pad and return the
* actually applied format parameters, as \ref V4L2Subdevice::getFormat would * actually applied format parameters, as \ref V4L2Subdevice::getFormat would
@ -218,10 +233,12 @@ int V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format)
* *
* \return 0 on success or a negative error code otherwise * \return 0 on success or a negative error code otherwise
*/ */
int V4L2Subdevice::setFormat(unsigned int pad, V4L2SubdeviceFormat *format) int V4L2Subdevice::setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
Whence whence)
{ {
struct v4l2_subdev_format subdevFmt = {}; struct v4l2_subdev_format subdevFmt = {};
subdevFmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; subdevFmt.which = whence == ActiveFormat ? V4L2_SUBDEV_FORMAT_ACTIVE
: V4L2_SUBDEV_FORMAT_TRY;
subdevFmt.pad = pad; subdevFmt.pad = pad;
subdevFmt.format.width = format->size.width; subdevFmt.format.width = format->size.width;
subdevFmt.format.height = format->size.height; subdevFmt.format.height = format->size.height;