Refactor nuttx v4l2

Adjust the v4l2 framework to support both capture and v4l2m2m,
and can easily add other v4l2 features.

Signed-off-by: shizhenghui <shizhenghui@xiaomi.com>
This commit is contained in:
shizhenghui 2023-12-07 16:28:02 +08:00 committed by Xiang Xiao
parent c3a3f79741
commit 255090d594
8 changed files with 3098 additions and 2342 deletions

@ -47,7 +47,7 @@
#include <nuttx/serial/uart_rpmsg.h>
#include <nuttx/timers/oneshot.h>
#include <nuttx/video/fb.h>
#include <nuttx/video/video.h>
#include <nuttx/video/v4l2_cap.h>
#include <nuttx/timers/oneshot.h>
#include <nuttx/wireless/pktradio.h>
#include <nuttx/wireless/bluetooth/bt_null.h>
@ -300,10 +300,10 @@ int sim_bringup(void)
sim_camera_initialize();
ret = video_initialize(CONFIG_SIM_CAMERA_DEV_PATH);
ret = capture_initialize(CONFIG_SIM_CAMERA_DEV_PATH);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: video_initialize() failed: %d\n", ret);
syslog(LOG_ERR, "ERROR: capture_initialize() failed: %d\n", ret);
}
#endif

@ -29,7 +29,7 @@ if(CONFIG_DRIVERS_VIDEO)
endif()
if(CONFIG_VIDEO_STREAM)
list(APPEND SRCS video.c video_framebuff.c)
list(APPEND SRCS v4l2_core.c video_framebuff.c v4l2_cap.c)
endif()
# These video drivers depend on I2C support

@ -27,7 +27,7 @@ ifeq ($(CONFIG_VIDEO_FB),y)
endif
ifeq ($(CONFIG_VIDEO_STREAM),y)
CSRCS += video.c video_framebuff.c
CSRCS += v4l2_core.c video_framebuff.c v4l2_cap.c
endif
# These video drivers depend on I2C support

@ -34,6 +34,7 @@
#include <nuttx/video/imgsensor.h>
#include <nuttx/video/imgdata.h>
#include <nuttx/video/video.h>
#include <nuttx/video/v4l2_cap.h>
/****************************************************************************
* Pre-processor Definitions
@ -779,10 +780,7 @@ int goldfish_camera_initialize(void)
snprintf(devpath, sizeof(devpath), "/dev/video%zd", i);
}
video_register(devpath,
&priv[i]->data,
&sensor,
1);
capture_register(devpath, &priv[i]->data, &sensor, 1);
}
return 0;

File diff suppressed because it is too large Load Diff

533
drivers/video/v4l2_core.c Normal file

@ -0,0 +1,533 @@
/****************************************************************************
* drivers/video/v4l2_core.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/video/video.h>
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Character driver methods. */
static int v4l2_open(FAR struct file *filep);
static int v4l2_close(FAR struct file *filep);
static ssize_t v4l2_read(FAR struct file *filep,
FAR char *buffer, size_t buflen);
static ssize_t v4l2_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
static int v4l2_ioctl(FAR struct file *filep,
int cmd, unsigned long arg);
static int v4l2_mmap(FAR struct file *filep,
FAR struct mm_map_entry_s *map);
static int v4l2_poll(FAR struct file *filep,
FAR struct pollfd *fds, bool setup);
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int v4l2_unlink(FAR struct inode *inode);
#endif
/****************************************************************************
* Public Data
****************************************************************************/
static const struct file_operations g_v4l2_fops =
{
v4l2_open, /* open */
v4l2_close, /* close */
v4l2_read, /* read */
v4l2_write, /* write */
NULL, /* seek */
v4l2_ioctl, /* ioctl */
v4l2_mmap, /* mmap */
NULL, /* truncate */
v4l2_poll, /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
v4l2_unlink, /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int v4l2_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->open != NULL)
{
return v4l2->fops->open(filep);
}
return -ENOTSUP;
}
static int v4l2_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->close != NULL)
{
return v4l2->fops->close(filep);
}
return -ENOTSUP;
}
static ssize_t v4l2_read(FAR struct file *filep,
FAR char *buffer, size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->read != NULL)
{
return v4l2->fops->read(filep, buffer, buflen);
}
return -ENOTSUP;
}
static ssize_t v4l2_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->write != NULL)
{
return v4l2->fops->write(filep, buffer, buflen);
}
return -ENOTSUP;
}
static int v4l2_ioctl(FAR struct file *filep,
int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
switch (cmd)
{
case VIDIOC_QUERYCAP:
if (v4l2->vops->querycap == NULL)
{
break;
}
return v4l2->vops->querycap(v4l2,
(FAR struct v4l2_capability *)arg);
case VIDIOC_G_INPUT:
if (v4l2->vops->g_input == NULL)
{
break;
}
return v4l2->vops->g_input((FAR int *)arg);
case VIDIOC_ENUMINPUT:
if (v4l2->vops->enum_input == NULL)
{
break;
}
return v4l2->vops->enum_input(v4l2,
(FAR struct v4l2_input *)arg);
case VIDIOC_REQBUFS:
if (v4l2->vops->reqbufs == NULL)
{
break;
}
return v4l2->vops->reqbufs(v4l2,
(FAR struct v4l2_requestbuffers *)arg);
case VIDIOC_QUERYBUF:
if (v4l2->vops->querybuf == NULL)
{
break;
}
return v4l2->vops->querybuf(v4l2,
(FAR struct v4l2_buffer *)arg);
case VIDIOC_QBUF:
if (v4l2->vops->qbuf == NULL)
{
break;
}
return v4l2->vops->qbuf(v4l2,
(FAR struct v4l2_buffer *)arg);
break;
case VIDIOC_DQBUF:
if (v4l2->vops->dqbuf == NULL)
{
break;
}
return v4l2->vops->dqbuf(v4l2,
(FAR struct v4l2_buffer *)arg,
filep->f_oflags);
case VIDIOC_CANCEL_DQBUF:
if (v4l2->vops->cancel_dqbuf == NULL)
{
break;
}
return v4l2->vops->cancel_dqbuf(v4l2,
(FAR enum v4l2_buf_type)arg);
case VIDIOC_STREAMON:
if (v4l2->vops->streamon == NULL)
{
break;
}
return v4l2->vops->streamon(v4l2,
(FAR enum v4l2_buf_type *)arg);
case VIDIOC_STREAMOFF:
if (v4l2->vops->streamoff == NULL)
{
break;
}
return v4l2->vops->streamoff(v4l2,
(FAR enum v4l2_buf_type *)arg);
case VIDIOC_DO_HALFPUSH:
if (v4l2->vops->do_halfpush == NULL)
{
break;
}
return v4l2->vops->do_halfpush(v4l2, arg);
case VIDIOC_TAKEPICT_START:
if (v4l2->vops->takepict_start == NULL)
{
break;
}
return v4l2->vops->takepict_start(v4l2, (int32_t)arg);
case VIDIOC_TAKEPICT_STOP:
if (v4l2->vops->takepict_stop == NULL)
{
break;
}
return v4l2->vops->takepict_stop(v4l2, arg);
case VIDIOC_S_SELECTION:
if (v4l2->vops->s_selection == NULL)
{
break;
}
return v4l2->vops->s_selection(v4l2,
(FAR struct v4l2_selection *)arg);
case VIDIOC_G_SELECTION:
if (v4l2->vops->g_selection == NULL)
{
break;
}
return v4l2->vops->g_selection(v4l2,
(FAR struct v4l2_selection *)arg);
case VIDIOC_TRY_FMT:
if (v4l2->vops->try_fmt == NULL)
{
break;
}
return v4l2->vops->try_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_G_FMT:
if (v4l2->vops->g_fmt == NULL)
{
break;
}
return v4l2->vops->g_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_S_FMT:
if (v4l2->vops->s_fmt == NULL)
{
break;
}
return v4l2->vops->s_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_S_PARM:
if (v4l2->vops->s_parm == NULL)
{
break;
}
return v4l2->vops->s_parm(v4l2,
(FAR struct v4l2_streamparm *)arg);
case VIDIOC_G_PARM:
if (v4l2->vops->g_parm == NULL)
{
break;
}
return v4l2->vops->g_parm(v4l2,
(FAR struct v4l2_streamparm *)arg);
case VIDIOC_QUERYCTRL:
if (v4l2->vops->queryctrl == NULL)
{
break;
}
return v4l2->vops->queryctrl(v4l2,
(FAR struct v4l2_queryctrl *)arg);
case VIDIOC_QUERY_EXT_CTRL:
if (v4l2->vops->query_ext_ctrl == NULL)
{
break;
}
return v4l2->vops->query_ext_ctrl(v4l2,
(FAR struct v4l2_query_ext_ctrl *)arg);
case VIDIOC_QUERYMENU:
if (v4l2->vops->querymenu == NULL)
{
break;
}
return v4l2->vops->querymenu(v4l2,
(FAR struct v4l2_querymenu *)arg);
case VIDIOC_G_CTRL:
if (v4l2->vops->g_ctrl == NULL)
{
break;
}
return v4l2->vops->g_ctrl(v4l2,
(FAR struct v4l2_control *)arg);
case VIDIOC_S_CTRL:
if (v4l2->vops->s_ctrl == NULL)
{
break;
}
return v4l2->vops->s_ctrl(v4l2,
(FAR struct v4l2_control *)arg);
case VIDIOC_G_EXT_CTRLS:
if (v4l2->vops->g_ext_ctrls == NULL)
{
break;
}
return v4l2->vops->g_ext_ctrls(v4l2,
(FAR struct v4l2_ext_controls *)arg);
case VIDIOC_S_EXT_CTRLS:
if (v4l2->vops->s_ext_ctrls == NULL)
{
break;
}
return v4l2->vops->s_ext_ctrls(v4l2,
(FAR struct v4l2_ext_controls *)arg);
case VIDIOC_G_STD:
break;
case VIDIOC_S_STD:
break;
case V4SIOC_QUERY_EXT_CTRL_SCENE:
if (v4l2->vops->query_ext_ctrl_scene == NULL)
{
break;
}
return v4l2->vops->query_ext_ctrl_scene(v4l2,
(FAR struct v4s_query_ext_ctrl_scene *)arg);
case V4SIOC_QUERYMENU_SCENE:
if (v4l2->vops->querymenu_scene == NULL)
{
break;
}
return v4l2->vops->querymenu_scene(v4l2,
(FAR struct v4s_querymenu_scene *)arg);
case V4SIOC_G_EXT_CTRLS_SCENE:
if (v4l2->vops->g_ext_ctrls_scene == NULL)
{
break;
}
return v4l2->vops->g_ext_ctrls_scene(v4l2,
(FAR struct v4s_ext_controls_scene *)arg);
case V4SIOC_S_EXT_CTRLS_SCENE:
if (v4l2->vops->s_ext_ctrls_scene == NULL)
{
break;
}
return v4l2->vops->s_ext_ctrls_scene(v4l2,
(FAR struct v4s_ext_controls_scene *)arg);
case VIDIOC_ENUM_FMT:
if (v4l2->vops->enum_fmt == NULL)
{
break;
}
return v4l2->vops->enum_fmt(v4l2,
(FAR struct v4l2_fmtdesc *)arg);
break;
case VIDIOC_ENUM_FRAMEINTERVALS:
if (v4l2->vops->enum_frminterval == NULL)
{
break;
}
return v4l2->vops->enum_frminterval(v4l2,
(FAR struct v4l2_frmivalenum *)arg);
break;
case VIDIOC_ENUM_FRAMESIZES:
if (v4l2->vops->enum_frmsize == NULL)
{
break;
}
return v4l2->vops->enum_frmsize(v4l2,
(FAR struct v4l2_frmsizeenum *)arg);
break;
default:
verr("Unrecognized cmd: %d\n", cmd);
break;
}
return -ENOTTY;
}
static int v4l2_mmap(FAR struct file *filep,
FAR struct mm_map_entry_s *map)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->mmap != NULL)
{
return v4l2->fops->mmap(filep, map);
}
return -ENOTSUP;
}
static int v4l2_poll(FAR struct file *filep,
FAR struct pollfd *fds, bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->poll != NULL)
{
return v4l2->fops->poll(filep, fds, setup);
}
return -ENOTSUP;
}
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int v4l2_unlink(FAR struct inode *inode)
{
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->unlink != NULL)
{
return v4l2->fops->unlink(inode);
}
return -ENOTSUP;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
int video_register(FAR const char *devpath, FAR struct v4l2_s *v4l2)
{
int ret;
/* Input devpath Error Check */
if (devpath[0] != '/')
{
return -EINVAL;
}
/* Register the character driver */
ret = register_driver(devpath, &g_v4l2_fops, 0666, v4l2);
if (ret < 0)
{
verr("Failed to register driver: %d\n", ret);
return ret;
}
return OK;
}
int video_unregister(FAR const char *devpath)
{
return unregister_driver(devpath);
}

@ -0,0 +1,87 @@
/****************************************************************************
* include/nuttx/video/v4l2_cap.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
****************************************************************************/
#ifndef __NUTTX_VIDEO_V4L2_CAP_H
#define __NUTTX_VIDEO_V4L2_CAP_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/video/imgdata.h>
#include <nuttx/video/imgsensor.h>
#ifdef __cplusplus
extern "C"
{
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Initialize capture driver.
*
* param [in] devpath: path to capture device
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int capture_initialize(FAR const char *devpath);
/* Uninitialize capture driver.
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int capture_uninitialize(FAR const char *devpath);
/* New API to register capture driver.
*
* param [in] devpath: path to capture device
* param [in] data: provide imgdata ops
* param [in] sensor: provide imgsensor ops array
* param [in] sensor_num: the number of imgsensor ops array
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int capture_register(FAR const char *devpath,
FAR struct imgdata_s *data,
FAR struct imgsensor_s **sensors,
size_t sensor_num);
/* New API to Unregister capture driver.
*
* param [in] devpath: path to capture device
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int capture_unregister(FAR const char *devpath);
#ifdef __cplusplus
}
#endif
#endif /* __NUTTX_VIDEO_V4L2_CAP_H */

@ -30,6 +30,7 @@
****************************************************************************/
#include <sys/videoio.h>
#include <nuttx/fs/fs.h>
#ifdef __cplusplus
extern "C"
@ -55,31 +56,100 @@ extern "C"
#define VIDEO_HSIZE_3M (2048) /* 3M horizontal size */
#define VIDEO_VSIZE_3M (1536) /* 3M vertical size */
/****************************************************************************
* Public Types
*****************************************************************************/
struct video_format_s
{
uint16_t width;
uint16_t height;
uint32_t pixelformat;
};
typedef struct video_format_s video_format_t;
struct v4l2_s
{
FAR const struct v4l2_ops_s *vops;
FAR const struct file_operations *fops;
};
struct v4l2_ops_s
{
CODE int (*querycap)(FAR struct v4l2_s *ctx,
FAR struct v4l2_capability *cap);
CODE int (*g_input)(FAR int *num);
CODE int (*enum_input)(FAR struct v4l2_s *ctx,
FAR struct v4l2_input *input);
CODE int (*reqbufs)(FAR struct v4l2_s *ctx,
FAR struct v4l2_requestbuffers *reqbufs);
CODE int (*querybuf)(FAR struct v4l2_s *ctx,
FAR struct v4l2_buffer *buf);
CODE int (*qbuf)(FAR struct v4l2_s *ctx,
FAR struct v4l2_buffer *buf);
CODE int (*dqbuf)(FAR struct v4l2_s *ctx,
FAR struct v4l2_buffer *buf, int oflags);
CODE int (*cancel_dqbuf)(FAR struct v4l2_s *ctx,
enum v4l2_buf_type type);
CODE int (*g_fmt)(FAR struct v4l2_s *ctx,
FAR struct v4l2_format *fmt);
CODE int (*s_fmt)(FAR struct v4l2_s *ctx,
FAR struct v4l2_format *fmt);
CODE int (*try_fmt)(FAR struct v4l2_s *ctx,
FAR struct v4l2_format *v4l2);
CODE int (*g_parm)(FAR struct v4l2_s *ctx,
FAR struct v4l2_streamparm *parm);
CODE int (*s_parm)(FAR struct v4l2_s *ctx,
FAR struct v4l2_streamparm *parm);
CODE int (*streamon)(FAR struct v4l2_s *ctx,
FAR enum v4l2_buf_type *type);
CODE int (*streamoff)(FAR struct v4l2_s *ctx,
FAR enum v4l2_buf_type *type);
CODE int (*do_halfpush)(FAR struct v4l2_s *ctx,
bool enable);
CODE int (*takepict_start)(FAR struct v4l2_s *ctx,
int32_t capture_num);
CODE int (*takepict_stop)(FAR struct v4l2_s *ctx,
bool halfpush);
CODE int (*s_selection)(FAR struct v4l2_s *ctx,
FAR struct v4l2_selection *clip);
CODE int (*g_selection)(FAR struct v4l2_s *ctx,
FAR struct v4l2_selection *clip);
CODE int (*queryctrl)(FAR struct v4l2_s *ctx,
FAR struct v4l2_queryctrl *ctrl);
CODE int (*query_ext_ctrl)(FAR struct v4l2_s *ctx,
FAR struct v4l2_query_ext_ctrl *ctrl);
CODE int (*querymenu)(FAR struct v4l2_s *ctx,
FAR struct v4l2_querymenu *menu);
CODE int (*g_ctrl)(FAR struct v4l2_s *ctx,
FAR struct v4l2_control *ctrl);
CODE int (*s_ctrl)(FAR struct v4l2_s *ctx,
FAR struct v4l2_control *ctrl);
CODE int (*g_ext_ctrls)(FAR struct v4l2_s *ctx,
FAR struct v4l2_ext_controls *ctrls);
CODE int (*s_ext_ctrls)(FAR struct v4l2_s *ctx,
FAR struct v4l2_ext_controls *ctrls);
CODE int (*query_ext_ctrl_scene)(FAR struct v4l2_s *ctx,
FAR struct v4s_query_ext_ctrl_scene *ctrl);
CODE int (*querymenu_scene)(FAR struct v4l2_s *ctx,
FAR struct v4s_querymenu_scene *menu);
CODE int (*g_ext_ctrls_scene)(FAR struct v4l2_s *ctx,
FAR struct v4s_ext_controls_scene *ctrls);
CODE int (*s_ext_ctrls_scene)(FAR struct v4l2_s *ctx,
FAR struct v4s_ext_controls_scene *ctrls);
CODE int (*enum_fmt)(FAR struct v4l2_s *ctx,
FAR struct v4l2_fmtdesc *f);
CODE int (*enum_frminterval)(FAR struct v4l2_s *ctx,
FAR struct v4l2_frmivalenum *f);
CODE int (*enum_frmsize)(FAR struct v4l2_s *ctx,
FAR struct v4l2_frmsizeenum *f);
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
struct imgdata_s;
struct imgsensor_s;
/* Initialize video driver.
*
* param [in] devpath: path to video device
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int video_initialize(FAR const char *devpath);
/* Uninitialize video driver.
*
* Return on success, 0 is returned. On failure,
* negative value is returned.
*/
int video_uninitialize(FAR const char *devpath);
/* New API to register video driver.
*
* param [in] devpath: path to video device
@ -92,9 +162,7 @@ int video_uninitialize(FAR const char *devpath);
*/
int video_register(FAR const char *devpath,
FAR struct imgdata_s *data,
FAR struct imgsensor_s **sensors,
size_t sensor_num);
FAR struct v4l2_s *ctx);
/* New API to Unregister video driver.
*