Skip to content

Commit

Permalink
feat: added benchmark mode
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Oct 8, 2024
1 parent 7c20feb commit 4e9cdf3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
2 changes: 2 additions & 0 deletions include/libvideo2x.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef LIBVIDEO2X_H
#define LIBVIDEO2X_H

#include <stdbool.h>
#include <stdint.h>
#include <time.h>

Expand Down Expand Up @@ -75,6 +76,7 @@ struct ProcessingStatus {
LIBVIDEO2X_API int process_video(
const char *input_filename,
const char *output_filename,
bool benchmark,
enum AVHWDeviceType hw_device_type,
const struct FilterConfig *filter_config,
struct EncoderConfig *encoder_config,
Expand Down
7 changes: 1 addition & 6 deletions src/libplacebo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ extern "C" {
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavformat/avformat.h>
#include <libavutil/buffer.h>
#include <libavutil/avutil.h>
#include <libavutil/hwcontext.h>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/rational.h>
#include <libswscale/swscale.h>
}

#include "fsutils.h"
Expand Down
22 changes: 14 additions & 8 deletions src/libvideo2x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ int process_frames(
AVCodecContext *dec_ctx,
AVCodecContext *enc_ctx,
Filter *filter,
int video_stream_index
int video_stream_index,
bool benchmark = false
) {
int ret;
AVPacket packet;
Expand Down Expand Up @@ -100,12 +101,14 @@ int process_frames(
ret = filter->process_frame(frame, &processed_frame);
if (ret == 0 && processed_frame != nullptr) {
// Encode and write the processed frame
ret = encode_and_write_frame(processed_frame, enc_ctx, ofmt_ctx);
if (ret < 0) {
av_strerror(ret, errbuf, sizeof(errbuf));
fprintf(stderr, "Error encoding/writing frame: %s\n", errbuf);
av_frame_free(&processed_frame);
goto end;
if (!benchmark) {
ret = encode_and_write_frame(processed_frame, enc_ctx, ofmt_ctx);
if (ret < 0) {
av_strerror(ret, errbuf, sizeof(errbuf));
fprintf(stderr, "Error encoding/writing frame: %s\n", errbuf);
av_frame_free(&processed_frame);
goto end;
}
}

av_frame_free(&processed_frame);
Expand Down Expand Up @@ -221,6 +224,7 @@ void cleanup(
extern "C" int process_video(
const char *input_filename,
const char *output_filename,
bool benchmark,
AVHWDeviceType hw_type,
const FilterConfig *filter_config,
EncoderConfig *encoder_config,
Expand Down Expand Up @@ -344,7 +348,9 @@ extern "C" int process_video(
}

// Process frames
ret = process_frames(status, ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, filter, video_stream_index);
ret = process_frames(
status, ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, filter, video_stream_index, benchmark
);
if (ret < 0) {
fprintf(stderr, "Error processing frames\n");
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, filter);
Expand Down
50 changes: 44 additions & 6 deletions src/video2x.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -19,8 +20,9 @@ static struct option long_options[] = {
{"output", required_argument, NULL, 'o'},
{"filter", required_argument, NULL, 'f'},
{"hwaccel", required_argument, NULL, 'a'},
{"benchmark", no_argument, NULL, 0},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, '?'},
{"help", no_argument, NULL, 0},

// Encoder options
{"codec", required_argument, NULL, 'c'},
Expand Down Expand Up @@ -48,6 +50,7 @@ struct arguments {
const char *output_filename;
const char *filter_type;
const char *hwaccel;
bool benchmark;

// Encoder options
const char *codec;
Expand Down Expand Up @@ -92,6 +95,7 @@ void print_help() {
printf(" -o, --output Output video file path\n");
printf(" -f, --filter Filter to use: 'libplacebo' or 'realesrgan'\n");
printf(" -a, --hwaccel Hardware acceleration method (default: none)\n");
printf(" --benchmark Discard processed frames and calculate average FPS\n");
printf(" -v, --version Print program version\n");
printf(" -?, --help Display this help page\n");

Expand All @@ -111,6 +115,10 @@ void print_help() {
printf(" -g, --gpuid Vulkan GPU ID (default: 0)\n");
printf(" -m, --model Name of the model to use\n");
printf(" -r, --scale Scaling factor (2, 3, or 4)\n");

printf("\nExamples:\n");
printf(" video2x -i in.mp4 -o out.mp4 -f libplacebo -s anime4k-mode-a -w 3840 -h 2160\n");
printf(" video2x -i in.mp4 -o out.mp4 -f realesrgan -m realesr-animevideov3 -r 4\n");
}

void parse_arguments(int argc, char **argv, struct arguments *arguments) {
Expand All @@ -122,6 +130,7 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
arguments->output_filename = NULL;
arguments->filter_type = NULL;
arguments->hwaccel = "none";
arguments->benchmark = false;

// Encoder options
arguments->codec = "libx264";
Expand Down Expand Up @@ -218,12 +227,14 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
}
break;
case 'v':
printf("video2x %s\n", VIDEO2X_VERSION);
printf("Video2X v%s\n", VIDEO2X_VERSION);
exit(0);
case 0: // Long-only options without short equivalents (e.g., help)
case 0: // Long-only options without short equivalents
if (strcmp(long_options[option_index].name, "help") == 0) {
print_help();
exit(0);
} else if (strcmp(long_options[option_index].name, "benchmark") == 0) {
arguments->benchmark = true;
}
break;
default:
Expand All @@ -233,8 +244,13 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
}

// Check for required arguments
if (!arguments->input_filename || !arguments->output_filename) {
fprintf(stderr, "Error: Input and output files are required.\n");
if (!arguments->input_filename) {
fprintf(stderr, "Error: Input file path is required.\n");
exit(1);
}

if (!arguments->output_filename && !arguments->benchmark) {
fprintf(stderr, "Error: Output file path is required.\n");
exit(1);
}

Expand Down Expand Up @@ -263,6 +279,13 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
}

int main(int argc, char **argv) {
// Print help if no arguments are provided
if (argc < 2) {
print_help();
return 1;
}

// Parse command line arguments
struct arguments arguments;
parse_arguments(argc, argv, &arguments);

Expand Down Expand Up @@ -331,6 +354,7 @@ int main(int argc, char **argv) {
if (process_video(
arguments.input_filename,
arguments.output_filename,
arguments.benchmark,
hw_device_type,
&filter_config,
&encoder_config,
Expand All @@ -340,11 +364,25 @@ int main(int argc, char **argv) {
return 1;
}

// Calculate statistics
time_t time_elapsed = time(NULL) - status.start_time;
float speed_fps = (float)status.processed_frames / time_elapsed;

// Print processing summary
if (arguments.benchmark) {
printf("====== Video2X Benchmark summary ======\n");
printf("Video file processed: %s\n", arguments.input_filename);
printf("Total frames processed: %ld\n", status.processed_frames);
printf("Total time taken: %lds\n", time_elapsed);
printf("Average processing speed: %.2f FPS\n", speed_fps);
return 0;
}

printf("====== Video2X Processing summary ======\n");
printf("Video file processed: %s\n", arguments.input_filename);
printf("Total frames processed: %ld\n", status.processed_frames);
printf("Total time taken: %lds\n", time(NULL) - status.start_time);
printf("Total time taken: %lds\n", time_elapsed);
printf("Average processing speed: %.2f FPS\n", speed_fps);
printf("Output written to: %s\n", arguments.output_filename);
return 0;
}

0 comments on commit 4e9cdf3

Please sign in to comment.