mirror of
https://github.com/k4yt3x/video2x.git
synced 2024-12-28 06:59:11 +00:00
refactor(libvideo2x): extracted video frame count estimation into a function
Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
parent
6a218ebd9c
commit
bc8ae34dde
@ -309,12 +309,13 @@ list(REMOVE_DUPLICATES ALL_LIBRARIES)
|
|||||||
|
|
||||||
# Create the shared library 'libvideo2x'
|
# Create the shared library 'libvideo2x'
|
||||||
add_library(libvideo2x
|
add_library(libvideo2x
|
||||||
|
src/avutils.cpp
|
||||||
src/conversions.cpp
|
src/conversions.cpp
|
||||||
src/decoder.cpp
|
src/decoder.cpp
|
||||||
src/encoder.cpp
|
src/encoder.cpp
|
||||||
src/fsutils.cpp
|
src/fsutils.cpp
|
||||||
src/libplacebo_filter.cpp
|
|
||||||
src/libplacebo.cpp
|
src/libplacebo.cpp
|
||||||
|
src/libplacebo_filter.cpp
|
||||||
src/libvideo2x.cpp
|
src/libvideo2x.cpp
|
||||||
src/realesrgan_filter.cpp
|
src/realesrgan_filter.cpp
|
||||||
)
|
)
|
||||||
|
10
include/libvideo2x/avutils.h
Normal file
10
include/libvideo2x/avutils.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef AVUTILS_H
|
||||||
|
#define AVUTILS_H
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <libavformat/avformat.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t get_video_frame_count(AVFormatContext *ifmt_ctx, int in_vstream_idx);
|
||||||
|
|
||||||
|
#endif // AVUTILS_H
|
50
src/avutils.cpp
Normal file
50
src/avutils.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "avutils.h"
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
int64_t get_video_frame_count(AVFormatContext *ifmt_ctx, int in_vstream_idx) {
|
||||||
|
// Use the 'nb_frames' field if it is available
|
||||||
|
int64_t nb_frames = ifmt_ctx->streams[in_vstream_idx]->nb_frames;
|
||||||
|
if (nb_frames != AV_NOPTS_VALUE && nb_frames > 0) {
|
||||||
|
spdlog::debug("Read total number of frames from 'nb_frames': {}", nb_frames);
|
||||||
|
return nb_frames;
|
||||||
|
}
|
||||||
|
spdlog::warn("Estimating the total number of frames from duration * fps");
|
||||||
|
|
||||||
|
// Get the duration of the video
|
||||||
|
double duration_secs = 0.0;
|
||||||
|
if (ifmt_ctx->duration != AV_NOPTS_VALUE) {
|
||||||
|
duration_secs = static_cast<double>(ifmt_ctx->duration) / static_cast<double>(AV_TIME_BASE);
|
||||||
|
} else if (ifmt_ctx->streams[in_vstream_idx]->duration != AV_NOPTS_VALUE) {
|
||||||
|
duration_secs = static_cast<double>(ifmt_ctx->streams[in_vstream_idx]->duration) *
|
||||||
|
av_q2d(ifmt_ctx->streams[in_vstream_idx]->time_base);
|
||||||
|
}
|
||||||
|
if (duration_secs <= 0) {
|
||||||
|
spdlog::warn("Unable to determine the video's duration");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
spdlog::debug("Video duration: {}s", duration_secs);
|
||||||
|
|
||||||
|
// Calculate average FPS
|
||||||
|
double fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->avg_frame_rate);
|
||||||
|
if (fps <= 0) {
|
||||||
|
spdlog::debug("Unable to read the average frame rate from 'avg_frame_rate'");
|
||||||
|
fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->r_frame_rate);
|
||||||
|
}
|
||||||
|
if (fps <= 0) {
|
||||||
|
spdlog::debug("Unable to read the average frame rate from 'r_frame_rate'");
|
||||||
|
fps = av_q2d(av_guess_frame_rate(ifmt_ctx, ifmt_ctx->streams[in_vstream_idx], nullptr));
|
||||||
|
}
|
||||||
|
if (fps <= 0) {
|
||||||
|
spdlog::debug("Unable to estimate the average frame rate with 'av_guess_frame_rate'");
|
||||||
|
fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->time_base);
|
||||||
|
}
|
||||||
|
if (fps <= 0) {
|
||||||
|
spdlog::warn("Unable to estimate the video's average frame rate");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
spdlog::debug("Video average frame rate: {}", fps);
|
||||||
|
|
||||||
|
// Estimate and return the total number of frames
|
||||||
|
return static_cast<int64_t>(duration_secs * fps);
|
||||||
|
}
|
@ -11,6 +11,7 @@ extern "C" {
|
|||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "avutils.h"
|
||||||
#include "decoder.h"
|
#include "decoder.h"
|
||||||
#include "encoder.h"
|
#include "encoder.h"
|
||||||
#include "filter.h"
|
#include "filter.h"
|
||||||
@ -37,46 +38,7 @@ static int process_frames(
|
|||||||
|
|
||||||
// Get the total number of frames in the video with OpenCV
|
// Get the total number of frames in the video with OpenCV
|
||||||
spdlog::debug("Reading total number of frames");
|
spdlog::debug("Reading total number of frames");
|
||||||
proc_ctx->total_frames = ifmt_ctx->streams[in_vstream_idx]->nb_frames;
|
proc_ctx->total_frames = get_video_frame_count(ifmt_ctx, in_vstream_idx);
|
||||||
if (proc_ctx->total_frames > 0) {
|
|
||||||
spdlog::debug("Read total number of frames from 'nb_frames': {}", proc_ctx->total_frames);
|
|
||||||
} else {
|
|
||||||
spdlog::warn("Estimating the total number of frames from duration * fps");
|
|
||||||
|
|
||||||
// Get the duration of the video
|
|
||||||
double duration_secs = 0.0;
|
|
||||||
if (ifmt_ctx->duration != AV_NOPTS_VALUE) {
|
|
||||||
duration_secs =
|
|
||||||
static_cast<double>(ifmt_ctx->duration) / static_cast<double>(AV_TIME_BASE);
|
|
||||||
} else if (ifmt_ctx->streams[in_vstream_idx]->duration != AV_NOPTS_VALUE) {
|
|
||||||
duration_secs = static_cast<double>(ifmt_ctx->streams[in_vstream_idx]->duration) *
|
|
||||||
av_q2d(ifmt_ctx->streams[in_vstream_idx]->time_base);
|
|
||||||
} else {
|
|
||||||
spdlog::warn("Unable to determine video duration");
|
|
||||||
}
|
|
||||||
spdlog::debug("Video duration: {}s", duration_secs);
|
|
||||||
|
|
||||||
// Calculate average FPS
|
|
||||||
double fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->avg_frame_rate);
|
|
||||||
if (fps <= 0) {
|
|
||||||
spdlog::debug("Unable to read the average frame rate from 'avg_frame_rate'");
|
|
||||||
fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->r_frame_rate);
|
|
||||||
}
|
|
||||||
if (fps <= 0) {
|
|
||||||
spdlog::debug("Unable to read the average frame rate from 'r_frame_rate'");
|
|
||||||
fps = av_q2d(av_guess_frame_rate(ifmt_ctx, ifmt_ctx->streams[in_vstream_idx], nullptr));
|
|
||||||
}
|
|
||||||
if (fps <= 0) {
|
|
||||||
spdlog::debug("Unable to estimate the average frame rate with 'av_guess_frame_rate'");
|
|
||||||
fps = av_q2d(ifmt_ctx->streams[in_vstream_idx]->time_base);
|
|
||||||
}
|
|
||||||
if (fps <= 0 || duration_secs <= 0) {
|
|
||||||
spdlog::warn("Unable to estimate the video's average frame rate");
|
|
||||||
} else {
|
|
||||||
// Calculate total frames
|
|
||||||
proc_ctx->total_frames = static_cast<int64_t>(duration_secs * fps);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the total number of frames is still 0
|
// Check if the total number of frames is still 0
|
||||||
if (proc_ctx->total_frames <= 0) {
|
if (proc_ctx->total_frames <= 0) {
|
||||||
|
@ -676,7 +676,7 @@ int main(int argc, char **argv) {
|
|||||||
total_frames = proc_ctx.total_frames;
|
total_frames = proc_ctx.total_frames;
|
||||||
pause = proc_ctx.pause;
|
pause = proc_ctx.pause;
|
||||||
}
|
}
|
||||||
if (!pause && total_frames > 0) {
|
if (!pause && (total_frames > 0 || processed_frames > 0)) {
|
||||||
double percentage = total_frames > 0 ? static_cast<double>(processed_frames) *
|
double percentage = total_frames > 0 ? static_cast<double>(processed_frames) *
|
||||||
100.0 / static_cast<double>(total_frames)
|
100.0 / static_cast<double>(total_frames)
|
||||||
: 0.0;
|
: 0.0;
|
||||||
|
Loading…
Reference in New Issue
Block a user