fix(video): check encoder started up before trying to flush frames on destruction#5257
fix(video): check encoder started up before trying to flush frames on destruction#5257Kishi85 wants to merge 1 commit into
Conversation
ec0fcc7 to
4ccc01b
Compare
Bundle ReportBundle size has no change ✅ |
|
This flushing was a workaround to address a memory leak each time streaming is started and stopped on AMD AMF encoders, so please test that to make sure you aren't reintroducing that. I agree that it shouldn't be necessary, but it also shouldn't be crashing either. Flushing is a totally valid thing to do here. The crash probably means there's a deeper use-after-free or race condition that we're just hiding by removing the flushing code. |
I'm also thinking that this is some kind of race condition that occurs when the hw context is torn down (by closing the capture instance as the specific problem here is accessing What would be interesting is to know if we have a way to ensure that this context is still valid when the frame flushing is validated (as that is where it segfaults) or if the fix can be done solely in FFmpeg. |
4ccc01b to
3731283
Compare
|
|
I think I've found a better solution and also finally the root cause for this issue which is due to the encoder never having seen a single frame (avcodec_ctx->frame_num being 0) when trying to flush frames. This does also explain why ctx->pic_end is NULL when the segfault hits as there was never a frame that could have be compared to. So my updated solution is to check if the encoder has started up before trying to flush frames and otherwise skip this step. Checking if the encoder has started up is done by checking if // Fix timestamps if we hit end-of-stream before the initial decode
// delay has elapsed.This seems to fix the issue without having to remove the frame flushing fully leaving the workaround for AMD AMF intact, except for when it has not seen enough frames when the encoder is starting up but the delay seems to be 0 for the encoders I was able to test here (so it's just checking if the encoder has seen a frame at all in order to do the flushing). @cgutman Does this look like a better solution to you? |



Description
This PR aims to fix #4943 by removing the frame flushing on
~avcodec_encode_session_t()for cases where the encoder has not stared up yet. Checking this is done by checking ifavcodec_ctx->frame_num > avcodec_ctx->delay. This check was specifically chosen because of the following comment in the problematic section that was segfaulting before:https://github.com/FFmpeg/FFmpeg/blob/15504610b0dc12c56e5e9f94ff06c873382368f5/libavcodec/hw_base_encode.c#L508:
This code sequence is expecting
ctx->pic_end != NULLbut if the encoder has not started up and seen enough frames (delay is usually 0 from my tests so far, so we're checking for at least one frame) then this expectation will not be fulfilled causing a segfault.Screenshot
Issues Fixed or Closed
Roadmap Issues
Type of Change
Checklist
AI Usage