Repository navigation
Preserve selected codec across a failed CodecContext.open() - #2506
Merged
Merged
Conversation
WyattBlue
force-pushed
the
preserve-codec-on-failed-open
branch
3 times, most recently
from
November 4, 2026 23:26
962ef1a to
ae84d1e
Compare
A failed avcodec_open2() runs FFmpeg's cleanup (ff_codec_close), which sets avctx->codec to NULL. That left the context in a broken state: codec, name, is_encoder, and is_decoder would raise or crash, and the context could not be reused. Capture the codec pointer before avcodec_open2() and restore it on failure, in open() alone, so every reader of ptr.codec stays correct and the context can be reopened after fixing the offending parameters. The failed open still resets other fields (notably layout drops to 0 channels), so a retry must re-apply the parameters set before; this is documented in the open() docstring.
WyattBlue
force-pushed
the
preserve-codec-on-failed-open
branch
from
November 4, 2026 23:28
ae84d1e to
203da84
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A failed
avcodec_open2()runs FFmpeg's cleanup path (free_and_end:→ff_codec_close()), which setsavctx->codec = NULL. After a failedCodecContext.open(), the context is left broken:Capture the codec pointer before
avcodec_open2()and restore it on failure, inopen()alone. Every reader ofptr.codecthen stays correct, and the context can be reopened after fixing the offending parameters. This fixes the root cause in one place rather than guarding each reader.AVCodecstructs are static globals in FFmpeg, so holding and restoring the pointer has no lifetime risk.Why the pointer, not
codec_id?codec_idsurvives the failed open, but it identifies a format, not an implementation:aacandaac_atshare the same id, as doh264/libx264/h264_videotoolbox. Re-deriving viaavcodec_find_encoder(codec_id)could silently swap in a different codec than the caller selected. Restoring the pointer preserves the exact codec.