Skip to content

Commit

Permalink
feat(video2x): improved the timer
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Nov 3, 2024
1 parent d6f27b3 commit fbe3b44
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,15 @@ endif()

# Create the executable 'video2x'
if (BUILD_VIDEO2X_CLI)
add_executable(video2x src/video2x.cpp)
add_executable(video2x src/video2x.cpp src/timer.cpp)
set_target_properties(video2x PROPERTIES OUTPUT_NAME video2x)

# Include directories for the executable
target_include_directories(video2x PRIVATE
${ALL_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/include/libvideo2x
)

# Compile options for the executable
Expand Down
34 changes: 34 additions & 0 deletions include/libvideo2x/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef TIMER_H
#define TIMER_H

#include <atomic>
#include <chrono>
#include <cstdint>
#include <thread>

class Timer {
public:
Timer();
~Timer();

void start();
void pause();
void resume();
void stop();

bool is_running() const;
bool is_paused() const;
int64_t get_elapsed_time() const;

private:
std::atomic<bool> running;
std::atomic<bool> paused;
std::thread timer_thread;
int64_t elapsed_time;
std::chrono::steady_clock::time_point start_time;
std::chrono::steady_clock::time_point pause_start_time;

void update_elapsed_time();
};

#endif // TIMER_H
76 changes: 76 additions & 0 deletions src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "timer.h"

#include <sys/types.h>

Timer::Timer() : running(false), paused(false), elapsed_time(0) {}

Timer::~Timer() {
stop();
}

void Timer::start() {
if (running) {
return;
}

running = true;
paused = false;
elapsed_time = 0;
start_time = std::chrono::steady_clock::now();

timer_thread = std::thread([this]() {
while (running) {
if (!paused) {
update_elapsed_time();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
});
}

void Timer::pause() {
if (running && !paused) {
paused = true;
pause_start_time = std::chrono::steady_clock::now();
}
}

void Timer::resume() {
if (running && paused) {
paused = false;
auto pause_end_time = std::chrono::steady_clock::now();
auto pause_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(pause_end_time - pause_start_time)
.count();
start_time += std::chrono::milliseconds(pause_duration);
}
}

void Timer::stop() {
running = false;
if (timer_thread.joinable()) {
timer_thread.join();
}
update_elapsed_time();
}

bool Timer::is_running() const {
return running;
}

bool Timer::is_paused() const {
return paused;
}

int64_t Timer::get_elapsed_time() const {
return elapsed_time;
}

void Timer::update_elapsed_time() {
if (running && !paused) {
auto current_time = std::chrono::steady_clock::now();
elapsed_time =
std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)
.count();
}
}
26 changes: 10 additions & 16 deletions src/video2x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extern "C" {
#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include "libvideo2x/timer.h"

// Indicate if a newline needs to be printed before the next output
std::atomic<bool> newline_required = false;

Expand Down Expand Up @@ -565,11 +567,9 @@ int main(int argc, char **argv) {
);
spdlog::info("Press SPACE to pause/resume, 'q' to abort.");

// Setup variables to track processing time
auto start_time = std::chrono::steady_clock::now();
auto paused_start = std::chrono::steady_clock::time_point();
std::chrono::seconds total_paused_duration(0);
long long time_elapsed = 0;
// Setup timer
Timer timer;
timer.start();

// Enable non-blocking input
#ifndef _WIN32
Expand Down Expand Up @@ -607,12 +607,10 @@ int main(int argc, char **argv) {
if (proc_ctx.pause) {
putchar('\n');
spdlog::info("Processing paused. Press SPACE to resume, 'q' to abort.");
paused_start = std::chrono::steady_clock::now();
timer.pause();
} else {
spdlog::info("Resuming processing...");
total_paused_duration += std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now() - paused_start
);
timer.resume();
}
}
} else if (ch == 'q' || ch == 'Q') {
Expand Down Expand Up @@ -641,12 +639,7 @@ int main(int argc, char **argv) {
double percentage = total_frames > 0 ? static_cast<double>(processed_frames) *
100.0 / static_cast<double>(total_frames)
: 0.0;
auto now = std::chrono::steady_clock::now();
time_elapsed = std::chrono::duration_cast<std::chrono::seconds>(
now - start_time - total_paused_duration
)
.count();

int64_t time_elapsed = timer.get_elapsed_time() / 1000;
std::cout << "\rProcessing frame " << processed_frames << "/" << total_frames
<< " (" << percentage << "%); time elapsed: " << time_elapsed << "s";
std::cout.flush();
Expand Down Expand Up @@ -693,14 +686,15 @@ int main(int argc, char **argv) {
std::lock_guard<std::mutex> lock(proc_ctx_mutex);
processed_frames = proc_ctx.processed_frames;
}
int64_t time_elapsed = timer.get_elapsed_time() / 1000;
float average_speed_fps = static_cast<float>(processed_frames) /
(time_elapsed > 0 ? static_cast<float>(time_elapsed) : 1);

// Print processing summary
printf("====== Video2X %s summary ======\n", arguments.benchmark ? "Benchmark" : "Processing");
printf("Video file processed: %s\n", arguments.in_fname.u8string().c_str());
printf("Total frames processed: %ld\n", proc_ctx.processed_frames);
printf("Total time taken: %llds\n", time_elapsed);
printf("Total time taken: %ld s\n", time_elapsed);
printf("Average processing speed: %.2f FPS\n", average_speed_fps);

// Print additional information if not in benchmark mode
Expand Down

0 comments on commit fbe3b44

Please sign in to comment.