Skip to content

fix(ffmpeg/vulkan): close CBS context in Vulkan encoder teardown - #727

Merged
ReenigneArcher merged 1 commit into
LizardByte:masterfrom
neatnoise:fix-vulkan-cbs-leak
Jul 13, 2026
Merged

ReenigneArcher merged 1 commit into
LizardByte:masterfrom
neatnoise:fix-vulkan-cbs-leak

Conversation

@neatnoise

Copy link
Copy Markdown
Contributor

Description

FFmpeg's Vulkan H.264/H.265/AV1 encoders never call ff_cbs_close(&enc->cbs) in their close functions, leaking CBS internal state (~1 MB H.264, ~8 MB H.265) per encoder session. This causes unbounded memory growth in applications that repeatedly create/destroy encoder contexts.

Fix adds ff_cbs_fragment_free() and ff_cbs_close() to all three Vulkan encoder close functions.

Verified with heaptrack:

  • Before: 44 MB leaked
  • After: 811 KB (normal overhead)

Issues Fixed or Closed

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Unit tests have been added or updated for any new or modified functionality

AI Usage

  • None: No AI tools were used in creating this PR
  • Light: AI provided minor assistance (formatting, simple suggestions)
  • Moderate: AI helped with code generation or debugging specific parts
  • Heavy: AI generated most or all of the code changes

The Vulkan H.264/H.265/AV1 encoders initialize a CBS context
(enc->cbs) during init but never close it in their close functions.
This leaks CBS internal state including cloned SPS/VPS/PPS structs
(~1 MB for H.264, ~8 MB for H.265 per encoder session).

Add ff_cbs_fragment_free() and ff_cbs_close() calls to all three
Vulkan encoder close functions.
@sonarqubecloud

Copy link
Copy Markdown

@ReenigneArcher

Copy link
Copy Markdown
Member

Can you submit this patch to FFmpeg as well?

@ReenigneArcher
ReenigneArcher merged commit d4dc18b into LizardByte:master Jul 13, 2026
21 checks passed
@neatnoise

Copy link
Copy Markdown
Contributor Author

I can't create PR's in FFmpeg project, created the issue https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/23794

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants