Skip to content

V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties. Singlethread as always#12893

Merged
alalek merged 20 commits intoopencv:3.4from
aleksey-nikolaev:cap-update
Nov 8, 2018
Merged

V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties. Singlethread as always#12893
alalek merged 20 commits intoopencv:3.4from
aleksey-nikolaev:cap-update

Conversation

@aleksey-nikolaev
Copy link
Copy Markdown
Contributor

@aleksey-nikolaev aleksey-nikolaev commented Oct 22, 2018

I made this changes because when I tried to get the exposure property I got the INF. Also, all values for my cam on windows are not same on linux.

This pullrequest changes

  1. Added backward compatibility mode:
    • Conversion parameters to/from [0,1] range with backward compatibility mode.
    • Initial mode set by the environment variable OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED.
    • Turn off backward compatibility mode by cv::CAP_PROP_MODE = 1 (enable normalized properties).
    • Revert backward compatibility mode by cv::CAP_PROP_MODE = 0.
  2. Added all known conversion from V4L2_CID_* to cv::CAP_PROP_*
  3. Added new property cv::CAP_PROP_CHANNEL for all captures for selecting video input channel. Default value -1 - the channels not changed.
  4. Fixed use of video buffer index.
  5. Removed extra memcopy for grab image.
  6. Removed open / close device when changing capture settings.
  7. Removed write permission on the device mapped memory and attempts to write to this area.
  8. Added conversion to RGB from formats: NV12, NV21, Y8

Some code refactoring:

  1. Removed device closing from autosetup_capture_mode_v4l2
  2. Refactoring of Convert To RGB, Set/Get of properties, frame capture initialization.
  3. Added cap_properties verbalization.
  4. Removed extra code, like querying or enumerating all device parameters or an extra {}.
  5. Removed extra wrappers over cvtColor

I tried to keep the code format unchanged. It will be nice to have .clang-format on the OpenCV project.

Aleksey Nikolaev added 5 commits October 22, 2018 17:32
1 Added cap_properties verbalization.
2 Set Get of properties elementary refactoring.
3 Removed converting parameters to/from [0,1] range.
4 Added all known conversion from V4L2_CID_* to CV_CAP_PROP_*
1. Removed all query for parameters range.
2. Refactored capture initialization.
3. Added selecting input channel by CV_CAP_PROP_MODE. Default value -1 the channels not changed.
1. Refactoring of Convert To RGB
1. Fixed use of video buffer index.
2. Removed extra memcopy for grab image.
3. Removed device closing from autosetup_capture_mode_v4l2
1. The `goto` was eliminated
2. Fixed use of temporary buffer index for V4L2_PIX_FMT_SN9C10X
3. Fixed use of the bufferIndex
4. Removed trailing spaces and unused variables.
@aleksey-nikolaev aleksey-nikolaev changed the title V4L-2: Added camera properties mostly the same as on MSMF. V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties Oct 24, 2018
1. Alias for capture->buffers[capture->bufferIndex]
2. Reduced size of data for memcpy: bytesused instead of length
3. Refactoring. Code duplication. More info for debug
Aleksey Nikolaev added 4 commits October 25, 2018 14:37
1. Added the ability to grab and retrieveFrame independently several times
1. Not need to close/open device for new capture parameters applying.
2. Removed using of device name as a flag that the capture is closed. Added sufficient function.
3. Refactoring. Added requestBuffers and createBuffers
1. Added tryIoctl with `select` like was in mainloop_v4l2.
2. Fixed buffer request for device without closing the device.
3. Some static function moved to CvCaptureCAM_V4L
4. Removed unused defines
1. Thread-safe now
@aleksey-nikolaev aleksey-nikolaev changed the title V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties. Thread-safe Oct 26, 2018
Aleksey Nikolaev added 2 commits October 26, 2018 17:20
1. Fixed thread-safe destructor
2. Fixed FPS setting
@aleksey-nikolaev aleksey-nikolaev changed the base branch from 3.4 to master October 29, 2018 15:54
@aleksey-nikolaev aleksey-nikolaev changed the title V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties. Thread-safe V4L (V4L2): Refactoring. Added missed camera properties. Fixed getting INF for some properties. Singlethread as always Oct 29, 2018
@aleksey-nikolaev
Copy link
Copy Markdown
Contributor Author

Unfortunately, I need this on 3.4. The main problem is a backward compatibility: [0,1] ranges for parameters. I could revert the ranges conversion and add the capture mode or a build option for using real parameters instead of converted. Would that be an acceptable compromise?

@aleksey-nikolaev aleksey-nikolaev changed the base branch from master to 3.4 October 29, 2018 18:50
@aleksey-nikolaev aleksey-nikolaev changed the base branch from 3.4 to master October 29, 2018 18:51
@aleksey-nikolaev aleksey-nikolaev changed the base branch from master to 3.4 October 29, 2018 19:30
1. Reverted conversion parameters to/from [0,1] by default for backward compatibility.
2. Added setting for turn off compatibility mode: set CV_CAP_PROP_MODE to 65536
3. Most static functions moved to CvCaptureCAM_V4L
4. Refactoring of icvRetrieveFrameCAM_V4L and using of frame_allocated flag
@aleksey-nikolaev
Copy link
Copy Markdown
Contributor Author

@alalek , @paroj , I've already deleted the mutex and returned the parameter ranges, is that enough or am I missing something?

1. Added conversion to RGB from NV12, NV21
2. Refactoring. Removed wrappers for known format conversions.
CAP_V4L migrated to use VideoCaptureProperties.
// any other value greater than 32768 revert backward compatibility mode.
// Backward compatibility mode affects the following parameters:
// cv::CAP_PROP_*: BRIGHTNESS,CONTRAST,SATURATION,HUE,GAIN,EXPOSURE,FOCUS,AUTOFOCUS,AUTO_EXPOSURE.
bool compatibilityMode;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is outdated.
CAP_PROP_MODE is already used, so behavior should not be changed.

Alternative variant via environment variables:

compatibilityMode = !utils::getConfigurationParameterBool("OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED", false);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CAP_PROP_MODE was used for get - not for set

2. Environment variable `OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED` for setting default backward compatibility mode.
3. Revert getting of `CAP_PROP_MODE` as fourcc code in backward compatibility mode.
deviceName = _deviceName;
returnFrame = true;
compatibilityMode = utils::getConfigurationParameterBool("OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED", true);
compatibilityMode = !utils::getConfigurationParameterBool("OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED", false);
Copy link
Copy Markdown
Contributor

@paroj paroj Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was correct before:

compatibilityMode <=>  "converted to/from [0,1) range" <=> OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED <=> 1

maybe either rename either compatibilityMode or OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED to match each other.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Renamed to normalizePropRange.

Copy link
Copy Markdown
Member

@alalek alalek Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, need to fix this again.


Done


buffer() : start(NULL), length(0)
{
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v4l2_buf = v4l2_buffer() this initialization style is used in this cpp file. Please, don't mix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you care about variable names, I could bring everything in this file to a single style, including renaming ambiguous entities

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed type instead.
Using v4l2_buffer() initializer.

`v4l2-ctl --list-formats` should have 'MJPG' entry
to avoid "munmap: Invalid argument" messages
@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 8, 2018

@paroj @aleksey-nikolaev I believe it should be fine now. Any comments?

@aleksey-nikolaev
Copy link
Copy Markdown
Contributor Author

code style maybe. And what about the comments at the top of the file, they're no longer relevant, are they?

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@washley
Copy link
Copy Markdown

washley commented Nov 29, 2018

I think this broke builds on kernels < 3.5 (at least that, maybe an even higher version is required). See #13335

mizo added a commit to mizo/opencv that referenced this pull request Jun 27, 2022
The format of destination is BGR24. However, when it was NV[12|21], it
was set to RGB24.

Fixes: Commit 8396ae6 ("Merge pull request opencv#12893 from aleksey-nikolaev:cap-update")
mizo added a commit to mizo/opencv that referenced this pull request Jun 27, 2022
The format of destination is BGR24. However, when it was NV[12|21], it
was set to RGB24.

Fixes: Commit 8396ae6 ("Merge pull request opencv#12893 from aleksey-nikolaev:cap-update")
@alalek alalek mentioned this pull request Jun 27, 2022
6 tasks
return;
case V4L2_PIX_FMT_NV12:
cv::cvtColor(cv::Mat(imageSize.height * 3 / 2, imageSize.width, CV_8U, currentBuffer.start), destination,
COLOR_YUV2RGB_NV12);
Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COLOR_YUV2RGB_NV12

Bug is introduced here. Should be BGR by default.

Fixed here: #26081

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants