Skip to content

VideoCapture: remove decoder initialization when demuxing#24012

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
cudawarped:videocapture_raw_read
Aug 11, 2023
Merged

VideoCapture: remove decoder initialization when demuxing#24012
asmorkalov merged 1 commit intoopencv:4.xfrom
cudawarped:videocapture_raw_read

Conversation

@cudawarped
Copy link
Copy Markdown
Contributor

@cudawarped cudawarped commented Jul 19, 2023

As discussed in opencv/opencv_contrib#3488 (comment) decoder initialization when VideoCapture is only used for demuxing (CAP_PROP_FORMAT== -1, rawMode == true) is unecessary. Additionally if video sources can be parsed but not decoded, VideoCapture returns an error on decoder initialization preventing it from being used for demuxing.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Comment on lines +1159 to +1175
if(rawMode)
return true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It breaks some properties like CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT, information about camera rotation. Also video_st pointer is not initialized.

Copy link
Copy Markdown
Contributor Author

@cudawarped cudawarped Jul 19, 2023

Choose a reason for hiding this comment

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

Yes sorry I thought the change would be simple but I was using the the wrong test case from cudacodec where CAP_PROP_FORMAT is set after initialization. I'll push the new changes a bit later after some more testing.

@cudawarped cudawarped force-pushed the videocapture_raw_read branch from 67baadc to 66c9e34 Compare July 19, 2023 11:55
@asmorkalov
Copy link
Copy Markdown
Contributor

[ RUN      ] videoio/videoio_synthetic.write_read_position/2, where GetParam() = (640x480, FOURCC(IYUV), .avi, FFMPEG, 30dB)
CAP_PROP_FRAME_COUNT is not supported by backend. Assume 50 frames.
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:305: Failure
Expected: (psnr) > (PSNR_GT), actual: 6.35095 vs 30
frame 0
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:97: progressive seek
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:40: Failure
Expected equality of these values:
  idx
    Which is: 6
  (int)cap.get(CAP_PROP_POS_FRAMES)
    Which is: 0
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:111: random seek
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:305: Failure
Expected: (psnr) > (PSNR_GT), actual: 9.30242 vs 30
frame 6
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:111: random seek
[  FAILED  ] videoio/videoio_synthetic.write_read_position/2, where GetParam() = (640x480, FOURCC(IYUV), .avi, FFMPEG, 30dB) (388 ms)

@asmorkalov
Copy link
Copy Markdown
Contributor

[ RUN      ] videoio/videoio_synthetic.write_read_position/18, where GetParam() = (976x768, FOURCC(IYUV), .avi, FFMPEG, 30dB)
CAP_PROP_FRAME_COUNT is not supported by backend. Assume 50 frames.
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=23
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=24
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=25
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=26
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=27
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=28
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=29
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=30
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:72: consecutive read
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=0
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:97: progressive seek
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:40: Failure
Expected equality of these values:
  idx
    Which is: 43
  (int)cap.get(CAP_PROP_POS_FRAMES)
    Which is: 0
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:111: random seek
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:28: Failure
Value of: img.empty()
  Actual: true
Expected: false
idx=43
Google Test trace:
/home/alexander/Projects/OpenCV/opencv-master/modules/videoio/test/test_video_io.cpp:111: random seek
[  FAILED  ] videoio/videoio_synthetic.write_read_position/18, where GetParam() = (976x768, FOURCC(IYUV), .avi, FFMPEG, 30dB) (523 ms)

@cudawarped
Copy link
Copy Markdown
Contributor Author

@asmorkalov can you let me know which version of FFmpeg you are using please?

@asmorkalov
Copy link
Copy Markdown
Contributor

It's default FFmpeg instance in Ubuntu 18.04:

"    FFMPEG:                      YES\n"
"      avcodec:                   YES (57.107.100)\n"
"      avformat:                  YES (57.83.100)\n"
"      avutil:                    YES (55.78.100)\n"
"      swscale:                   YES (4.8.100)\n"
"      avresample:                YES (3.7.0)\n"
"    GStreamer:                   YES (1.14.5)\n"

@cudawarped
Copy link
Copy Markdown
Contributor Author

cudawarped commented Jul 28, 2023

@asmorkalov I have built OpenCV against the same version of FFmpeg (3.4.13), but on my version of Ubuntu (20.04) all the FFmpeg write_read_position tests pass. Specifically I don't see

CAP_PROP_FRAME_COUNT is not supported by backend. Assume 50 frames.

caused by

cap.get(CAP_PROP_FRAME_COUNT) == 0

which "looks" to be unrelated to my changes, I assumed was down to the version of FFmpeg but it must be something different.

Do the tests still fail if you run them in isolation i.e.

--gtest_filter=videoio/videoio_synthetic.write_read_position/2

and do they still fail on your set up without my changes?

I have noticed that seeking, which is not currently tested, won't work at the moment and I have left some debug output in the test. I am addressing both these issues at the moment but it would be good to fix your test failure at the same time.

@cudawarped cudawarped force-pushed the videocapture_raw_read branch from 66c9e34 to 72cea1b Compare August 1, 2023 14:34
@cudawarped
Copy link
Copy Markdown
Contributor Author

@asmorkalov Fixed seek functionality but I still can't recreate your errors using the same version of FFmpeg.

@cudawarped cudawarped force-pushed the videocapture_raw_read branch from 72cea1b to 974151b Compare August 1, 2023 14:51
@asmorkalov
Copy link
Copy Markdown
Contributor

@cudawarped Thanks for the patch. All tests passed with the lat commit.

@asmorkalov
Copy link
Copy Markdown
Contributor

@cudawarped FFmpeg for Windows is not built in CI and updated before release. You have to support the configuration with old version of cap_ffmpeg_impl.hpp and wrap new tests in #ifndef WIN32 check.

@asmorkalov asmorkalov self-assigned this Aug 2, 2023
frame.height = video_st->codecpar->height;
frame.width = video_st->codecpar->width;
#endif
return true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I propose to add get_rotation_angle() too. The encoded stream may contain rotation meta. In this case cuda codec has to handle it too (w and h may be swapped).

Copy link
Copy Markdown
Contributor Author

@cudawarped cudawarped Aug 2, 2023

Choose a reason for hiding this comment

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

As rotation isn't supported by the Nvidia Video Codec SDK and RAW streams returned by VideoCapture will be encoded without the rotation shouldn't we just return the encoded width and height? This should be automatic as the rotation_angle == 0 in raw mode.

    case CAP_PROP_FRAME_WIDTH:
        return (double)((rotation_auto && ((rotation_angle%180) != 0)) ? frame.height : frame.width);
    case CAP_PROP_FRAME_HEIGHT:
        return (double)((rotation_auto && ((rotation_angle%180) != 0)) ? frame.width : frame.height);

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

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

👍

@asmorkalov
Copy link
Copy Markdown
Contributor

@opencv-alalek could you take a look too?

@asmorkalov asmorkalov merged commit 5b41134 into opencv:4.x Aug 11, 2023
@asmorkalov asmorkalov mentioned this pull request Sep 11, 2023
asmorkalov pushed a commit to opencv/opencv_contrib that referenced this pull request Nov 16, 2023
cudacodec::VideoReader: allow frame seek on initialization #3542

Allow seeking of video source on initialization of `cudacodec::VideoReader` when new variable `VideoReaderInitParams::iFirstFrame` != 0.

Dependant on opencv/opencv#24012

Fixes #3541.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
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.

2 participants