JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
jevois::Camera Class Reference

#include <jevois/Core/Camera.H>

JeVois camera driver class - grabs frames from a Video4Linux camera sensor.

On the platform hardware, the Camera class provides access to the camera sensor on the parallel camera bus (CSI). On other hardware, it can provide access to any Video4Linux2 camera (e.g., USB webcam), although with some limitations, such as:

  • cannot read/write raw camera registers.
  • usually cannot select any frames/s but instead limited to a few fixed values like 15fps, 30fps.
  • usually limited to 30fps max (no 60fps or 120fps modes like on JeVois platform hardware)
  • usually limited number of controls (exposure, gain, etc) available.

The camera runs a thread that handles capturing frames automatically. Users can access the next grabbed frame using get(), which may block until the frame is fully captured. Once they are finished with a frame obtained through get(), users should hand it back to the Camera by calling done(). When using the JeVois Engine and a Module, get() and done() are automatically called in an exception-safe manner through the use of the InputFrame class. Indeed, video grabbing uses a fixed set of image buffers that are allocated by the Linux kernel when a video format is configured, and the camera hardware then cycles through the buffers. On the platform, the camera image buffers are allocated in the ION carveout memory area, which is a reserved memory zone configured in the Linux kernel, and the USB output buffers are also allocated in that zone. To avoid reserving too much memory that will not be used, the ION carveout reservation is quite small by default, but is sufficient for 3 1280x1024 YUYV buffers.

Camera implements a zero-copy, zero-wait access to input video frames, that is:

  • the pixel data of the image you obtain via get() is directly the memory-mapped pixel buffer that the silicon hardware on the JeVois chip uses via direct-memory-access (DMA) to stream the pixel data from the camera chip to processor memory;
  • as soon as an image is captured by the camera hardware, get() unblocks and returns it (as opposed to having a fixed, regular interval at which images may be available). Camera has several image buffers, allowing one to be captured while another is being handed over for processing via get(). These buffers are recycled, i.e., once done() is called, the underlying buffer is sent back to the camera hardware for future capture.

Camera will drop frames if they are not processed by user code fast enough. That is, if all buffers have been grabbed already and not yet handed over through get() and then returned by done(), then the next camera sensor frame will be dropped.

Most programmers will never use Camera directly, instead using Engine and InputFrame.

Definition at line 62 of file Camera.H.

Inheritance diagram for jevois::Camera:
Collaboration diagram for jevois::Camera:

Public Member Functions

 Camera (std::string const &devname, jevois::CameraSensor s=jevois::CameraSensor::any, unsigned int const nbufs=0)
 Construct and open the device. More...
 
 ~Camera ()
 Close the device and free all resources. More...
 
void streamOn () override
 Start streaming. More...
 
void abortStream () override
 Abort streaming. More...
 
void streamOff () override
 Stop streaming. More...
 
void get (RawImage &img) override
 Get the next captured buffer. More...
 
bool hasScaledImage () const override
 Check whether a second input image scaled by the JeVoisPro Platform ISP is available. More...
 
void get2 (RawImage &img) override
 Get the next captured buffer, for second ISP-scaled image. More...
 
void done (RawImage &img) override
 Indicate that user processing is done with an image previously obtained via get() More...
 
void done2 (RawImage &img) override
 Indicate that user processing is done with an image previously obtained via get2() More...
 
void queryControl (struct v4l2_queryctrl &qc) const override
 Get information about a control, throw if unsupported by hardware. More...
 
void queryMenu (struct v4l2_querymenu &qm) const override
 Get the available menu entry names for a menu-type control, throw if unsupported by hardware. More...
 
void getControl (struct v4l2_control &ctrl) const
 Get a control's current value, throw if unsupported by hardware. More...
 
void setControl (struct v4l2_control const &ctrl) override
 Set a control, throw if control not supported or the hardware rejects the value. More...
 
void setFormat (VideoMapping const &m) override
 Set the video format and frame rate. More...
 
void writeRegister (unsigned short reg, unsigned short val)
 Write a value to one of the camera's registers. More...
 
unsigned short readRegister (unsigned short reg)
 Read a value from one of the camera's registers. More...
 
int lock ()
 Lock the camera and return its file descriptor. More...
 
void unlock ()
 Unlock the camera that was previously locked by lock() More...
 
- Public Member Functions inherited from jevois::VideoInput
 VideoInput (std::string const &devname, unsigned int const nbufs=0)
 Constructor. More...
 
virtual ~VideoInput ()
 Virtual destructor for save inheritance. More...
 

Protected Types

enum  Flags { JEVOIS_SENSOR_COLOR = 0x00, JEVOIS_SENSOR_MONO = 0x01, JEVOIS_SENSOR_ICM20948 = 0x02 }
 Sensor flags. More...
 

Protected Member Functions

Flags readFlags ()
 Get the sensor flags. More...
 

Additional Inherited Members

- Protected Attributes inherited from jevois::VideoInput
const std::string itsDevName
 Our device or movie file name. More...
 
unsigned const int itsNbufs
 Our number of buffers. More...
 

Member Enumeration Documentation

◆ Flags

enum jevois::Camera::Flags
protected

Sensor flags.

Keep this in sync with jevois-sdk/linux-3.4/drivers/media/video/sunxi-vfe/device/camera.h

Enumerator
JEVOIS_SENSOR_COLOR 
JEVOIS_SENSOR_MONO 
JEVOIS_SENSOR_ICM20948 

Definition at line 163 of file Camera.H.

Constructor & Destructor Documentation

◆ Camera()

jevois::Camera::Camera ( std::string const &  devname,
jevois::CameraSensor  s = jevois::CameraSensor::any,
unsigned int const  nbufs = 0 
)

Construct and open the device.

Parameters
devnamedevice name, e.g., /dev/video0
nbufsnumber of video grab buffers, or 0 for automatic.

Definition at line 34 of file Camera.C.

References JEVOIS_SENSOR_ICM20948, JEVOIS_SENSOR_MONO, JEVOIS_TRACE, LDEBUG, and readFlags().

◆ ~Camera()

jevois::Camera::~Camera ( )

Close the device and free all resources.

Definition at line 151 of file Camera.C.

References JEVOIS_TRACE, and jevois::warnAndIgnoreException().

Member Function Documentation

◆ abortStream()

void jevois::Camera::abortStream ( )
overridevirtual

Abort streaming.

This only cancels future get() and done() calls, one should still call streamOff() to turn off streaming.

Implements jevois::VideoInput.

Definition at line 172 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().

◆ done()

void jevois::Camera::done ( jevois::RawImage img)
overridevirtual

Indicate that user processing is done with an image previously obtained via get()

You should call this as soon after get() as possible, once you are finished with the RawImage data so that it can be recycled.

Calling done() on a RawImage invalidates the image and in particular its pixel buffer. Users should make sure that no attempt to use the image or the pixel buffer will be made after done() is called.

Implements jevois::VideoInput.

Definition at line 215 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.

Referenced by main().

◆ done2()

void jevois::Camera::done2 ( jevois::RawImage img)
overridevirtual

Indicate that user processing is done with an image previously obtained via get2()

You should call this as soon after get2() as possible, once you are finished with the RawImage data so that it can be recycled.

Calling done2() on a RawImage invalidates the image and in particular its pixel buffer. Users should make sure that no attempt to use the image or the pixel buffer will be made after done2() is called.

Reimplemented from jevois::VideoInput.

Definition at line 224 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.

◆ get()

void jevois::Camera::get ( jevois::RawImage img)
overridevirtual

Get the next captured buffer.

Throws if we are not streaming or blocks until an image is available (has been captured). img should have been created by the caller (with no pixel buffer allocated) and will be filled in by what we receive from the device here.

Implements jevois::VideoInput.

Definition at line 190 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.

Referenced by main().

◆ get2()

void jevois::Camera::get2 ( jevois::RawImage img)
overridevirtual

Get the next captured buffer, for second ISP-scaled image.

On JeVois-Pro Platform only, the camera ISP can output 2 frames: 1) raw from sensor, 2) scaled by ISP. This function is to access the ISP scaled frame. Throws if not JeVois-Pro Platform or the camera stream type is not jevois::StreamType::RawAndScaled. Throws if we are not streaming or blocks until an image is available (has been captured). img should have been created by the caller (with no pixel buffer allocated) and will be filled in by what we receive from the device here.

Reimplemented from jevois::VideoInput.

Definition at line 205 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.

◆ getControl()

void jevois::Camera::getControl ( struct v4l2_control &  ctrl) const
virtual

Get a control's current value, throw if unsupported by hardware.

This is just a pass-through to VIDIOC_G_CTRL, users should zero-out ctrl and then set in ctrl.id the desired control ID.

Implements jevois::VideoInput.

Definition at line 249 of file Camera.C.

References JEVOIS_TIMED_LOCK, and LFATAL.

◆ hasScaledImage()

bool jevois::Camera::hasScaledImage ( ) const
overridevirtual

Check whether a second input image scaled by the JeVoisPro Platform ISP is available.

Returns false unless we are on JeVois-Pro Platform and the camera format modifier jevois::CropType::CropScale is currently in use.

Reimplemented from jevois::VideoInput.

Definition at line 199 of file Camera.C.

◆ lock()

int jevois::Camera::lock ( )

Lock the camera and return its file descriptor.

Used by IMUi2c to access the IMU registers over the I2C bus shared with the camera sensor. Use with caution and make sure you catch exceptions so you can guarantee that you will call unlock(), otherwise your camera will be stuck and enable to stream.

Definition at line 310 of file Camera.C.

References LFATAL.

◆ queryControl()

void jevois::Camera::queryControl ( struct v4l2_queryctrl &  qc) const
overridevirtual

Get information about a control, throw if unsupported by hardware.

Caller should zero-out qc and then set the id field to the desired control id. See VIDIOC_QUERYCTRL for more information.

Implements jevois::VideoInput.

Definition at line 233 of file Camera.C.

References JEVOIS_TIMED_LOCK, and LFATAL.

◆ queryMenu()

void jevois::Camera::queryMenu ( struct v4l2_querymenu &  qm) const
overridevirtual

Get the available menu entry names for a menu-type control, throw if unsupported by hardware.

Caller should zero-out qm and then set the id and index fields to the desired control id and menu item index. See VIDIOC_QUERYMENU for more information.

Implements jevois::VideoInput.

Definition at line 241 of file Camera.C.

References JEVOIS_TIMED_LOCK, and LFATAL.

◆ readFlags()

jevois::Camera::Flags jevois::Camera::readFlags ( )
protected

Get the sensor flags.

Definition at line 324 of file Camera.C.

References JEVOIS_SENSOR_COLOR, and LERROR.

Referenced by Camera().

◆ readRegister()

unsigned short jevois::Camera::readRegister ( unsigned short  reg)

Read a value from one of the camera's registers.

This very low-level access is for development of optimal camera settings only and should not be used in normal operation, it can crash your system.

Definition at line 289 of file Camera.C.

References JEVOIS_TIMED_LOCK, LDEBUG, LFATAL, and jevois::sformat().

◆ setControl()

void jevois::Camera::setControl ( struct v4l2_control const &  ctrl)
overridevirtual

Set a control, throw if control not supported or the hardware rejects the value.

This is just a pass-through to VIDIOC_S_CTRL

Implements jevois::VideoInput.

Definition at line 261 of file Camera.C.

References JEVOIS_TIMED_LOCK, and LFATAL.

◆ setFormat()

void jevois::Camera::setFormat ( VideoMapping const &  m)
overridevirtual

Set the video format and frame rate.

Implements jevois::VideoInput.

Definition at line 49 of file Camera.C.

References jevois::Crop(), ISP_META_HEIGHT, ISP_META_WIDTH, ISP_V4L2_PIX_FMT_META, JEVOIS_TIMED_LOCK, JEVOIS_TRACE, LFATAL, LINFO, jevois::sensorPrepareSetFormat(), and jevois::to_string().

Referenced by main().

◆ streamOff()

void jevois::Camera::streamOff ( )
overridevirtual

Stop streaming.

Implements jevois::VideoInput.

Definition at line 181 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().

Referenced by main().

◆ streamOn()

void jevois::Camera::streamOn ( )
overridevirtual

Start streaming.

Implements jevois::VideoInput.

Definition at line 163 of file Camera.C.

References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().

Referenced by main().

◆ unlock()

void jevois::Camera::unlock ( )

Unlock the camera that was previously locked by lock()

Definition at line 318 of file Camera.C.

◆ writeRegister()

void jevois::Camera::writeRegister ( unsigned short  reg,
unsigned short  val 
)

Write a value to one of the camera's registers.

This very low-level access is for development of optimal camera settings only and should not be used in normal operation, it can crash your system.

Definition at line 273 of file Camera.C.

References JEVOIS_TIMED_LOCK, LDEBUG, LFATAL, and jevois::sformat().


The documentation for this class was generated from the following files: