Skip to content

Commit

Permalink
feat(*): switched to spdlog for logging and organized headers (#1183)
Browse files Browse the repository at this point in the history
* feat: updated PKGBUILD description
* feat: updated workflow syntax and dependencies
* feat: switched logging to spdlog
* chore: adjusted library defaults
* ci: fixed spdlog format string issues
* docs: fixed docs for libvideo2x functions
* feat: organized header files
* fix: fixed header installation directory
* feat: link spdlog statically if compiled from source
* feat: adjusted libvideo2x log level enum names
* feat: added version.h header

Signed-off-by: k4yt3x <i@k4yt3x.com>
  • Loading branch information
k4yt3x authored Oct 14, 2024
1 parent ecbc512 commit cd2006b
Show file tree
Hide file tree
Showing 26 changed files with 294 additions and 139 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- dev
pull_request: {}
workflow_dispatch: {}

jobs:
ubuntu:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -33,7 +34,7 @@ jobs:
- name: Build Video2X
run: |
mkdir -p /tmp/build /tmp/install
cmake -B /tmp/build -S . -DUSE_SYSTEM_NCNN=OFF \
cmake -B /tmp/build -S . -DUSE_SYSTEM_NCNN=OFF -DUSE_SYSTEM_SPDLOG=OFF \
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/tmp/install \
-DINSTALL_BIN_DESTINATION=. -DINSTALL_INCLUDE_DESTINATION=include \
Expand All @@ -44,6 +45,7 @@ jobs:
with:
name: video2x-nightly-linux-amd64
path: /tmp/install

windows:
runs-on: windows-latest
steps:
Expand Down Expand Up @@ -73,7 +75,7 @@ jobs:
Rename-Item -Path "third_party/ncnn-$ncnnVersion-windows-vs2022-shared" -NewName ncnn-shared
- name: Build Video2X
run: |
cmake -S . -B build -DUSE_SYSTEM_NCNN=OFF -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=build/video2x_install
cmake -S . -B build -DUSE_SYSTEM_NCNN=OFF -DUSE_SYSTEM_SPDLOG=OFF -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=build/video2x_install
cmake --build build --config Debug --parallel --target install
- name: Upload artifacts
uses: actions/upload-artifact@v4
Expand Down
22 changes: 12 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ on:
tags:
- "*"

permissions:
contents: write

jobs:
setup:
if: github.event.base_ref == 'refs/heads/master'
name: Setup
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.get_tag.outputs.tag }}
version: ${{ steps.get_version.outputs.version }}
steps:
- name: Get tag
id: get_tag
run: echo ::set-output name=tag::${GITHUB_REF/refs\/tags\//}
- name: Get version
id: get_version
run: echo version=${GITHUB_REF/refs\/tags\//} >> $GITHUB_OUTPUT

container:
name: Build and upload container
Expand All @@ -32,7 +35,7 @@ jobs:
password: ${{ secrets.GHCR_TOKEN }}
dockerfile: Dockerfile
image: video2x
tags: latest, ${{ needs.setup.outputs.tag }}
tags: latest, ${{ needs.setup.outputs.version }}

create-release:
name: Create release
Expand All @@ -45,11 +48,10 @@ jobs:
steps:
- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.setup.outputs.tag }}
release_name: Video2X ${{ needs.setup.outputs.tag }}
token: ${{ secrets.GITHUB_TOKEN }}
tag_name: ${{ needs.setup.outputs.version }}
release_name: Video2X ${{ needs.setup.outputs.version }}
draft: true
prerelease: false
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "third_party/ncnn"]
path = third_party/ncnn
url = https://github.com/Tencent/ncnn.git
[submodule "third_party/spdlog"]
path = third_party/spdlog
url = https://github.com/gabime/spdlog.git
41 changes: 34 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,31 @@ endif()

# Build options
option(BUILD_SHARED_LIBS "Build libvideo2x as a shared library" ON)
option(BUILD_VIDEO2X "Build the video2x executable" ON)
option(BUILD_VIDEO2X_CLI "Build the video2x executable" ON)
option(USE_SYSTEM_SPDLOG "Use system spdlog library" ON)
option(USE_SYSTEM_NCNN "Use system ncnn library" ON)

# Generate the version header file
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/include/libvideo2x/version.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/libvideo2x/version.h"
@ONLY
)

# Find the required packages
set(ALL_INCLUDE_DIRS)
set(ALL_LIBRARIES)

# spdlog
if (USE_SYSTEM_SPDLOG)
find_package(spdlog REQUIRED)
list(APPEND ALL_LIBRARIES spdlog::spdlog)
else()
add_subdirectory(third_party/spdlog)
list(APPEND ALL_LIBRARIES spdlog::spdlog_header_only)
endif()

# Platform-specific dependencies
if(WIN32)
# Define base paths for FFmpeg and ncnn
set(FFMPEG_BASE_PATH ${PROJECT_SOURCE_DIR}/third_party/ffmpeg-shared)
Expand Down Expand Up @@ -223,7 +242,9 @@ add_dependencies(libvideo2x realesrgan)
# Include directories for the shared library
target_include_directories(libvideo2x PRIVATE
${ALL_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/include/libvideo2x
${PROJECT_SOURCE_DIR}/third_party/libreal_esrgan_ncnn_vulkan/src
)

Expand Down Expand Up @@ -254,13 +275,14 @@ if(NOT WIN32)
endif()

# Create the executable 'video2x'
if (BUILD_VIDEO2X)
if (BUILD_VIDEO2X_CLI)
add_executable(video2x src/video2x.c src/getopt.c)
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
)

Expand All @@ -277,12 +299,12 @@ endif()
# Define the default installation directories
if(WIN32)
set(BIN_DESTINATION_DEFAULT ".")
set(INCLUDE_DESTINATION_DEFAULT "include")
set(INCLUDE_DESTINATION_DEFAULT "include/libvideo2x")
set(LIB_DESTINATION_DEFAULT ".")
set(MODEL_DESTINATION_DEFAULT ".")
else()
set(BIN_DESTINATION_DEFAULT "bin")
set(INCLUDE_DESTINATION_DEFAULT "include")
set(INCLUDE_DESTINATION_DEFAULT "include/libvideo2x")
set(LIB_DESTINATION_DEFAULT "lib")
set(MODEL_DESTINATION_DEFAULT "share/video2x")
endif()
Expand All @@ -306,13 +328,18 @@ install(TARGETS libvideo2x
# Install model files
install(DIRECTORY ${CMAKE_SOURCE_DIR}/models DESTINATION ${INSTALL_MODEL_DESTINATION})

# Install the executable if BUILD_VIDEO2X is enabled
if(BUILD_VIDEO2X)
# Install the executable if BUILD_VIDEO2X_CLI is enabled
if(BUILD_VIDEO2X_CLI)
install(TARGETS video2x RUNTIME DESTINATION ${INSTALL_BIN_DESTINATION})
endif()

# Install the header file
install(FILES ${PROJECT_SOURCE_DIR}/include/libvideo2x.h DESTINATION ${INSTALL_INCLUDE_DESTINATION})
install(FILES ${PROJECT_SOURCE_DIR}/include/libvideo2x/libvideo2x.h
DESTINATION ${INSTALL_INCLUDE_DESTINATION}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libvideo2x/version.h
DESTINATION ${INSTALL_INCLUDE_DESTINATION}
)

# Platform-specific installation rules
if(WIN32)
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ static:
-DCMAKE_C_COMPILER=$(CC) \
-DCMAKE_CXX_COMPILER=$(CXX) \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DUSE_SYSTEM_NCNN=OFF
-DBUILD_SHARED_LIBS=OFF
cmake --build $(BINDIR) --config Release --parallel
cp $(BINDIR)/compile_commands.json .

Expand All @@ -45,7 +44,8 @@ debian:
libswscale-dev \
libvulkan-dev \
glslang-tools \
libomp-dev
libomp-dev \
libspdlog-dev
cmake -B /tmp/build -S . -DUSE_SYSTEM_NCNN=OFF \
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/install \
Expand Down
6 changes: 3 additions & 3 deletions PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pkgname=video2x
pkgver=r862.f590ead
pkgver=r840.ecbc512
pkgrel=1
pkgdesc="A lossless video super resolution framework"
pkgdesc="A machine learning-based lossless video super resolution framework"
arch=('x86_64')
url="https://github.com/k4yt3x/video2x"
license=('AGPL3')
depends=('ffmpeg' 'ncnn' 'vulkan-driver')
makedepends=('git' 'cmake' 'make' 'clang' 'pkgconf' 'vulkan-headers' 'openmp')
makedepends=('git' 'cmake' 'make' 'clang' 'pkgconf' 'vulkan-headers' 'openmp' 'spdlog')

pkgver() {
printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions include/libvideo2x.h → include/libvideo2x/libvideo2x.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ enum FilterType {
FILTER_REALESRGAN
};

// Enum to specify log level
enum Libvideo2xLogLevel {
LIBVIDEO2X_LOG_LEVEL_TRACE,
LIBVIDEO2X_LOG_LEVEL_DEBUG,
LIBVIDEO2X_LOG_LEVEL_INFO,
LIBVIDEO2X_LOG_LEVEL_WARNING,
LIBVIDEO2X_LOG_LEVEL_ERROR,
LIBVIDEO2X_LOG_LEVEL_CRITICAL,
LIBVIDEO2X_LOG_LEVEL_OFF
};

// Configuration for Libplacebo filter
struct LibplaceboConfig {
int output_width;
Expand Down Expand Up @@ -78,6 +89,7 @@ struct VideoProcessingContext {
LIBVIDEO2X_API int process_video(
const char *input_filename,
const char *output_filename,
enum Libvideo2xLogLevel log_level,
bool benchmark,
enum AVHWDeviceType hw_device_type,
const struct FilterConfig *filter_config,
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions include/libvideo2x/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VERSION_H
#define VERSION_H

#define LIBVIDEO2X_VERSION_STRING "@PROJECT_VERSION@"

#endif // VERSION_H
22 changes: 12 additions & 10 deletions src/conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include <cstdio>

#include <spdlog/spdlog.h>

// Convert AVFrame format
AVFrame *convert_avframe_pix_fmt(AVFrame *src_frame, AVPixelFormat pix_fmt) {
AVFrame *dst_frame = av_frame_alloc();
if (dst_frame == nullptr) {
fprintf(stderr, "Failed to allocate destination AVFrame.\n");
spdlog::error("Failed to allocate destination AVFrame.");
return nullptr;
}

Expand All @@ -16,7 +18,7 @@ AVFrame *convert_avframe_pix_fmt(AVFrame *src_frame, AVPixelFormat pix_fmt) {

// Allocate memory for the converted frame
if (av_frame_get_buffer(dst_frame, 32) < 0) {
fprintf(stderr, "Failed to allocate memory for AVFrame.\n");
spdlog::error("Failed to allocate memory for AVFrame.");
av_frame_free(&dst_frame);
return nullptr;
}
Expand All @@ -36,7 +38,7 @@ AVFrame *convert_avframe_pix_fmt(AVFrame *src_frame, AVPixelFormat pix_fmt) {
);

if (sws_ctx == nullptr) {
fprintf(stderr, "Failed to initialize swscale context.\n");
spdlog::error("Failed to initialize swscale context.");
av_frame_free(&dst_frame);
return nullptr;
}
Expand Down Expand Up @@ -66,7 +68,7 @@ ncnn::Mat avframe_to_ncnn_mat(AVFrame *frame) {
if (frame->format != AV_PIX_FMT_BGR24) {
converted_frame = convert_avframe_pix_fmt(frame, AV_PIX_FMT_BGR24);
if (!converted_frame) {
fprintf(stderr, "Failed to convert AVFrame to BGR24.\n");
spdlog::error("Failed to convert AVFrame to BGR24.");
return ncnn::Mat();
}
} else {
Expand Down Expand Up @@ -102,7 +104,7 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {
// Step 1: Allocate a destination AVFrame for the specified pixel format
AVFrame *dst_frame = av_frame_alloc();
if (!dst_frame) {
fprintf(stderr, "Failed to allocate destination AVFrame.\n");
spdlog::error("Failed to allocate destination AVFrame.");
return nullptr;
}

Expand All @@ -112,15 +114,15 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {

// Allocate memory for the frame buffer
if (av_frame_get_buffer(dst_frame, 32) < 0) {
fprintf(stderr, "Failed to allocate memory for destination AVFrame.\n");
spdlog::error("Failed to allocate memory for destination AVFrame.");
av_frame_free(&dst_frame);
return nullptr;
}

// Step 2: Convert ncnn::Mat to BGR AVFrame
AVFrame *bgr_frame = av_frame_alloc();
if (!bgr_frame) {
fprintf(stderr, "Failed to allocate intermediate BGR AVFrame.\n");
spdlog::error("Failed to allocate intermediate BGR AVFrame.");
av_frame_free(&dst_frame);
return nullptr;
}
Expand All @@ -131,7 +133,7 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {

// Allocate memory for the intermediate BGR frame
if (av_frame_get_buffer(bgr_frame, 32) < 0) {
fprintf(stderr, "Failed to allocate memory for BGR AVFrame.\n");
spdlog::error("Failed to allocate memory for BGR AVFrame.");
av_frame_free(&dst_frame);
av_frame_free(&bgr_frame);
return nullptr;
Expand Down Expand Up @@ -159,7 +161,7 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {
);

if (sws_ctx == nullptr) {
fprintf(stderr, "Failed to initialize swscale context.\n");
spdlog::error("Failed to initialize swscale context.");
av_frame_free(&bgr_frame);
av_frame_free(&dst_frame);
return nullptr;
Expand All @@ -181,7 +183,7 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {
av_frame_free(&bgr_frame);

if (ret != dst_frame->height) {
fprintf(stderr, "Failed to convert BGR AVFrame to destination pixel format.\n");
spdlog::error("Failed to convert BGR AVFrame to destination pixel format.");
av_frame_free(&dst_frame);
return nullptr;
}
Expand Down
Loading

0 comments on commit cd2006b

Please sign in to comment.