Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

videoio(ffmpeg): fix AVFrame lifetime handling on early return#28221

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dheeraj25406 wants to merge2 commits intoopencv:4.x
base:4.x
Choose a base branch
Loading
fromdheeraj25406:fix-ffmpeg-hwframe-lifetime

Conversation

@dheeraj25406
Copy link

This PR fixes a memory leak in CvCapture_FFMPEG::retrieveFrame when
hardware-accelerated frames are used.

If av_hwframe_transfer_data() succeeds but retrieveFrame exits early
due to missing data, the allocated AVFrame was not freed.

The fix ensures:

  • AVFrame allocation failure is handled
  • Allocated AVFrame is released on all early-return paths
  • No behavior change for software decoding paths

Related to#25997

@asmorkalovasmorkalov self-assigned thisDec 18, 2025
@asmorkalovasmorkalov added this to the4.13.0 milestoneDec 18, 2025
@dheeraj25406
Copy link
Author

Thanks for the review!

The additional frame reallocation change is not essential.
This PR intentionally focuses only on fixing AVFrame lifetime handling
on early return paths for HW-accelerated decoding.

Happy to adjust if you’d prefer splitting or dropping commits.

@asmorkalov
Copy link
Contributor

Yes, drop not relevant changes please.

@dheeraj25406dheeraj25406force-pushed thefix-ffmpeg-hwframe-lifetime branch from8c29aff toea043cdCompareDecember 18, 2025 10:32
@dheeraj25406
Copy link
Author

Yes, drop not relevant changes please.

Done.
Dropped the unrelated frame reallocation commit and kept only the AVFrame lifetime fix.
History is now a single commit based on 4.x.

@asmorkalov
Copy link
Contributor

asmorkalov commentedDec 18, 2025
edited
Loading

There are more cases in the function bellow with early exit. Please handle them too. M.b. use some smart pointer here?

…FrameEnsure hardware-decoded frames are transferred to system memorywith proper lifetime handling in retrieveFrame().Enforce UMat output requirement in retrieveHWFrame() to avoidinvalid CPU-side usage for GPU-to-GPU transfers.
@dheeraj25406
Copy link
Author

There are more cases in the function bellow with early exit. Please handle them too. M.b. use some smart pointer here?

I’ve addressed both points:

  • Added explicit HW→SW transfer with proper frame lifetime handling in retrieveFrame()
  • Enforced UMat requirement in retrieveHWFrame() to ensure GPU→GPU path correctness

Changes are minimal and limited to cap_ffmpeg_impl.hpp.
Please let me know if any additional adjustments are needed.

Comment on lines +2016 to +2018
// must be UMat for GPU->GPU copy
if (!output.isUMat())
returnfalse;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why?

Copy link
Author

Choose a reason for hiding this comment

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

retrieveHWFrame() performs a GPU to GPU path using hw_copy_frame_to_umat() which writes into an OpenCL backed UMat.

Allowing Mat here would either:

  • silently fall back to an implicit GPU to CPU transfer
    OR
  • require duplicating logic already handled by retrieveFrame()

Enforcing UMat makes the contract explicit and avoids ambiguous behavior or hidden performance penalties.

If you prefer, I can alternatively add a clear error log instead of returning false or redirect non-UMat output to retrieveFrame() for HW to SW transfer.

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

Reviewers

@asmorkalovasmorkalovasmorkalov left review comments

@opencv-alalekopencv-alalekAwaiting requested review from opencv-alalek

Assignees

@asmorkalovasmorkalov

Projects

None yet

Milestone

4.13.0

Development

Successfully merging this pull request may close these issues.

2 participants

@dheeraj25406@asmorkalov

[8]ページ先頭

©2009-2025 Movatter.jp