fix: VideoReader crash on video-only file at EOF and dead bool return

- Guard swr_close/swr_init at EOF inside if (has_audio) — calling
  swr_init(nullptr) on a video-only file crashed at first loop
- Change open_file from bool to void — it never returned false, only
  threw, so the if (!open_file()) check in the constructor was dead code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
JohannesItten
2026-07-05 12:46:00 +03:00
parent 0298cf9b48
commit fe6b7b10ed
+8 -10
View File
@@ -41,9 +41,7 @@ public:
bool has_video = false;
explicit VideoReader(const std::string& filename) {
if (!open_file(filename))
return;
if (!has_video && !has_audio) return;
open_file(filename);
get_source_info();
if (has_video) allocate_video_conversion_buffers();
if (has_audio) allocate_audio_conversion_buffers();
@@ -134,9 +132,11 @@ public:
// EOF — loop back to start
avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD);
if (has_video) avcodec_flush_buffers(video_codec_context);
if (has_audio) avcodec_flush_buffers(audio_codec_context);
swr_close(swr_audio_ctx);
swr_init(swr_audio_ctx);
if (has_audio) {
avcodec_flush_buffers(audio_codec_context);
swr_close(swr_audio_ctx);
swr_init(swr_audio_ctx);
}
continue;
}
@@ -166,7 +166,7 @@ private:
AVFrame* audio_frame = av_frame_alloc();
SwrContext* swr_audio_ctx = nullptr;
bool open_file(const std::string& filename) {
void open_file(const std::string& filename) {
if (avformat_open_input(&format_context, filename.c_str(), nullptr, nullptr) != 0)
throw std::runtime_error("Could not open file: " + filename);
@@ -186,12 +186,10 @@ private:
}
}
if (video_stream_index == -1 && audio_stream_index == -1) {
if (!has_video && !has_audio) {
avformat_close_input(&format_context);
throw std::runtime_error("No audio/video stream found in: " + filename);
}
return true;
}
void get_source_info() {