Skip to content

Redesign of CL/VA interop initialization - #22780

Open
kallaballa wants to merge 13 commits into
opencv:4.xfrom
kallaballa:vaapi_encode_decode_at_the_same_time
Open

kallaballa wants to merge 13 commits into
opencv:4.xfrom
kallaballa:vaapi_encode_decode_at_the_same_time

Conversation

@kallaballa

Copy link
Copy Markdown
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

@asmorkalov
asmorkalov requested a review from alalek November 14, 2022 06:50

@alalek alalek left a comment

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.

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 )

Comment thread modules/videoio/src/cap_ffmpeg_hw.hpp Resolved
Comment thread modules/core/src/va_intel.cpp Resolved
@kallaballa

Copy link
Copy Markdown
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

kallaballa commented Nov 19, 2022 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

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

@kallaballa

kallaballa commented Nov 19, 2022 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

It after all still boils down to

va_intel::ocl::initializeContextFromVA(va_display);

being called twice and failing.

@kallaballa

kallaballa commented Nov 20, 2022 •

Copy link
Copy Markdown
Contributor Author

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

kallaballa commented Nov 22, 2022 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

Simplified the reproducer

@kallaballa

kallaballa commented Nov 22, 2022 •

Copy link
Copy Markdown
Contributor Author

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

kallaballa commented Nov 24, 2022 •

Copy link
Copy Markdown
Contributor Author

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

@alalek alalek left a comment

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.

Thank you for the updates!

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

Comment thread modules/core/src/precomp.hpp 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@kallaballa kallaballa Nov 26, 2022 •

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.

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

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.

pability

w

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.

Can we come up with a way that will provide proper context handling (and therefore actual acceleration) while not introducing breaking changes? I value this PR a lot and I am willing to put considerably more effort into it.

@kallaballa

kallaballa commented Jan 14, 2023 •

Copy link
Copy Markdown
Contributor Author

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
@asmorkalov
asmorkalov self-requested a review July 26, 2024 07:28
@asmorkalov asmorkalov removed this from the 4.11.0 milestone Dec 17, 2024
@kallaballa

Copy link
Copy Markdown
Contributor Author

Please checkout https://github.com/opencv/opencv/pull/22780/files#r1031421488 for why this stalled. I'd love some direction on this one.

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

@asmorkalov asmorkalov added this to the 4.12.0 milestone Jan 10, 2025
@asmorkalov asmorkalov modified the milestones: 4.12.0, 4.13.0 Apr 24, 2025
@asmorkalov asmorkalov modified the milestones: 4.13.0, 4.14.0 Dec 31, 2025
@asmorkalov asmorkalov modified the milestones: 4.14.0, 4.15.0 Jul 9, 2026

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Status: 🔴 Red

Verdict: Refactor before merge.

Retain slow-copy and backend-probing fallbacks when VA contexts differ; context mismatch should not turn previously supported operations into hard failures.

Please address this in the current PR and add focused regression coverage where applicable.

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.

3 participants