Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign of CL/VA interop initialization #22780

Open
wants to merge 12 commits into
base: 4.x
Choose a base branch
from

Conversation

kallaballa
Copy link
Contributor

The pertaining issue: #22779

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

Copy link
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 for the contribution!

I need a more time for launching of your code with reproducer (no working configuration with VA at this moment).

BTW, could you try to check your reproducer with OpenCV without PR #19755 (e.g. commit bb92eb5 )

modules/videoio/src/cap_ffmpeg_hw.hpp Outdated Show resolved Hide resolved
modules/core/src/va_intel.cpp Outdated Show resolved Hide resolved
@kallaballa
Copy link
Contributor Author

I need a more time for launching of your code with reproducer (no working configuration with VA at this moment).

BTW, could you try to check your reproducer with OpenCV without PR #19755 (e.g. commit bb92eb5 )

Alright

@kallaballa kallaballa closed this Nov 19, 2022
@kallaballa kallaballa force-pushed the vaapi_encode_decode_at_the_same_time branch from d57a32a to 1ba0984 Compare November 19, 2022 21:00
@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 19, 2022

I don't know what exactly happened. I force pushed to my own branch.. and then this

@kallaballa kallaballa reopened this Nov 19, 2022
@kallaballa
Copy link
Contributor Author

I pushed an empty branch which made it auto close. now things should be back to fine

@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 19, 2022

I drafted a fallback-less version of VA initialization that does not work with my reproducer. 2875b0b
Same error. Trying to analyze the situation.

@kallaballa
Copy link
Contributor Author

It after all still boils down to

va_intel::ocl::initializeContextFromVA(va_display);

being called twice and failing.

@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 20, 2022

Apart from fixing an error in my system setup, I think i have the behavior like you wanted. That also required changes to my reproducer which now works (but is code-wise a little cumbersome). This needs a better design, especially in cases where different interop-systems are used at the same time.

Reproducer before (fails) : vaapi-decode-encode.cpp

Reproducer now (succeeds) : vaapi-decode-encode.cpp

Anyway as I mentioned above i have a proposal for how this could work, based on my 4.x fork: https://github.com/kallaballa/GCV/blob/main/src/video/video-demo.cpp

I isolated the dirty hack that made my demo work in a separate branch

Now... how to do it properly?

@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 22, 2022

Apart from fixing an error in my system setup, I think i have the behavior like you wanted. That also required changes to my reproducer which now works (but is code-wise a little cumbersome). This needs a better design, especially in cases where different interop-systems are used at the same time.

Reproducer before (fails) : vaapi-decode-encode.cpp

Reproducer now (succeeds) : vaapi-decode-encode.cpp

Reproducer works now as initially proposed, except now there isn't an interop fallback anymore: vaapi-decode-encode.cpp

Anyway as I mentioned above i have a proposal for how this could work, based on my 4.x fork: https://github.com/kallaballa/GCV/blob/main/src/video/video-demo.cpp

Also the demo would now work like that (except it requires #22704)

I isolated the dirty hack that made my demo work in a separate branch

Now... how to do it properly?

I moved cv::va_intel::VAAPIInterop into its own header so i can query for it like that and only initialize automatically if it isn't present (a254612): https://github.com/kallaballa/opencv/blob/a254612d65e385bc1f3882a576e9f39cf96d791e/modules/videoio/src/cap_ffmpeg_hw.hpp#L478-L487

On the other hand if the user decides to manually call initializeContextFromVA() multiple times to have different contexts, that is possible. But only if it doesn't use VideoCapture/VideoWriter

What do you think?

@kallaballa
Copy link
Contributor Author

Simplified the reproducer

@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 22, 2022

Updated the proposal/demo which demonstrates how CL/GL and CL/VA interop could be used together by copying and binding ocl_contexts as needed.

@kallaballa kallaballa changed the title Allow for diverging VADisplays Redesign of CL/VA interop intitialization Nov 22, 2022
@kallaballa kallaballa changed the title Redesign of CL/VA interop intitialization Redesign of CL/VA interop initialization Nov 22, 2022
@asmorkalov asmorkalov added this to the 4.7.0 milestone Nov 23, 2022
@kallaballa kallaballa force-pushed the vaapi_encode_decode_at_the_same_time branch from 7b723c5 to f161534 Compare November 24, 2022 10:42
@kallaballa
Copy link
Contributor Author

kallaballa commented Nov 24, 2022

On the other hand if the user decides to manually call initializeContextFromVA() multiple times to have different contexts, that is possible. But only if it doesn't use VideoCapture/VideoWriter

Ensured by throwing an error on VADisplay mismatch: 497a536

@kallaballa kallaballa force-pushed the vaapi_encode_decode_at_the_same_time branch from f161534 to 497a536 Compare November 24, 2022 10:57
Copy link
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 for the updates!

Sorry for delay. I'm a busy till the end of this week.

modules/core/include/opencv2/core/va_intel_interop.hpp Outdated Show resolved Hide resolved
modules/core/include/opencv2/core/va_intel_interop.hpp Outdated Show resolved Hide resolved
modules/core/include/opencv2/core/va_intel_interop.hpp Outdated Show resolved Hide resolved
modules/core/src/precomp.hpp Outdated Show resolved Hide resolved
{
VAAPIInterop* interop = ocl_context.getContext().getUserContext<VAAPIInterop>().get();
if(!context_display || context_display != display)
CV_Error_(cv::Error::StsBadArg, ("Can't interop with current OpenCL context - VA display mismatch: %p (context) vs %p (surface).\ndid you call initializeContextFromVA before using VideoCapture/VideoWriter?", context_display, (void*)display));
Copy link
Member

Choose a reason for hiding this comment

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

VideoCapture/VideoWriter

?
We should not have this in generic functions.


Why is CV_Error()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you are right about this. Should be debug? I'll test.

va_intel::ocl::initializeContextFromVA(va_display);
}
} else {
CV_LOG_DEBUG(NULL, "OpenCL/VA_INTEL: CL/VA interop already initialized. ")
Copy link
Member

Choose a reason for hiding this comment

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

If there is HW_ACCELERATION_USE_OPENCL=1 then we should initialize context anyway (as this is explicitly requested by user through dedicated property).


Is there a way to check if OpenCL context could serve for compatible (but different) VADisplays?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I try to find one but initializing anyway would lead to the situation we had with different VADisplays and no VA-capability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, when the user sets HW_ACCELERATION_USE_OPENCL=1 on both VideoCapture and VideoWriter in the same program tha would lead again to the situation with two different VADisplays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this wouldn't benefit from VA:

   cv::VideoCapture capture("in.mkv", cv::CAP_FFMPEG, {
            cv::CAP_PROP_HW_DEVICE, VA_HW_DEVICE_INDEX,
            cv::CAP_PROP_HW_ACCELERATION, cv::VIDEO_ACCELERATION_VAAPI,
            cv::CAP_PROP_HW_ACCELERATION_USE_OPENCL, 1
    });

    cv::VideoWriter writer("out.mkv", cv::CAP_FFMPEG, cv::VideoWriter::fourcc('V', 'P', '9', '0'), fps, cv::Size(width, height), {
            cv::VIDEOWRITER_PROP_HW_ACCELERATION, cv::VIDEO_ACCELERATION_VAAPI,
            cv::VIDEOWRITER_PROP_HW_ACCELERATION_USE_OPENCL, 1
    });

    cv::UMat frame;
    capture >> frame;
    writer << frame;

full example: https://github.com/kallaballa/OpenCV-Issues/blob/main/src/vaapi-decode-encode/vaapi-decode-encode.cpp

@kallaballa
Copy link
Contributor Author

kallaballa commented Jan 14, 2023

I've experimented with partly parallel implementations of the WASM versions of my demos and the current VA initialization pattern is really in the way because the native implementation differs greatly from the WASM implementation and suffers from the above mentioned defects. It would be great if we could outline requirements on an initialization pattern so i can create a proper pull request and resolve this issue.

@asmorkalov asmorkalov modified the milestones: 4.8.0, 4.9.0 Jun 23, 2023
@asmorkalov asmorkalov modified the milestones: 4.9.0, 4.10.0 Dec 19, 2023
@asmorkalov asmorkalov modified the milestones: 4.10.0, 4.11.0 Apr 27, 2024
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.

None yet

3 participants