From eb7d411f49487ec1859c1bf3204485abbef8a270 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Tue, 22 Oct 2024 00:00:00 +0000 Subject: [PATCH] perf(libvideo2x): removed unnecessary read of nb_frames Signed-off-by: k4yt3x --- src/libvideo2x.cpp | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index b587c1d..3034eda 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -47,31 +47,15 @@ int process_frames( std::vector flushed_frames; char errbuf[AV_ERROR_MAX_STRING_SIZE]; - // Get the total number of frames in the video - AVStream *video_stream = ifmt_ctx->streams[vstream_idx]; - proc_ctx->total_frames = video_stream->nb_frames; - - // If nb_frames is not set, estimate total frames using duration and frame rate - if (proc_ctx->total_frames == 0) { - spdlog::debug("`nb_frames` is not set; estimating total frames with duration*framerate"); - int64_t duration = video_stream->duration; - AVRational frame_rate = video_stream->avg_frame_rate; - if (duration != AV_NOPTS_VALUE && frame_rate.num != 0 && frame_rate.den != 0) { - proc_ctx->total_frames = duration * frame_rate.num / frame_rate.den; - } - } - - // If total_frames is still 0, read the total number of frames with OpenCV - if (proc_ctx->total_frames == 0) { - spdlog::debug("Unable to estimate total number of frames; reading with OpenCV"); - cv::VideoCapture cap(ifmt_ctx->url); - if (!cap.isOpened()) { - spdlog::error("Failed to open video file with OpenCV"); - return -1; - } - proc_ctx->total_frames = cap.get(cv::CAP_PROP_FRAME_COUNT); - cap.release(); + // Get the total number of frames in the video with OpenCV + spdlog::debug("Unable to estimate total number of frames; reading with OpenCV"); + cv::VideoCapture cap(ifmt_ctx->url); + if (!cap.isOpened()) { + spdlog::error("Failed to open video file with OpenCV"); + return -1; } + proc_ctx->total_frames = cap.get(cv::CAP_PROP_FRAME_COUNT); + cap.release(); // Check if the total number of frames is still 0 if (proc_ctx->total_frames == 0) {