mirror of
https://github.com/k4yt3x/video2x.git
synced 2026-02-09 14:24:46 +08:00
Compare commits
16 Commits
6.0.0-beta
...
6.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31e606bd4c | ||
|
|
d50cf54f2a | ||
|
|
613b75ffec | ||
|
|
9d342c51a2 | ||
|
|
48119a30eb | ||
|
|
9d09d8570f | ||
|
|
ebef5f54cb | ||
|
|
747d85cf9b | ||
|
|
f89b263487 | ||
|
|
ebd5e8eb3d | ||
|
|
50d5cb16d1 | ||
|
|
8eac1a7393 | ||
|
|
f0f3166d92 | ||
|
|
482e82f9c4 | ||
|
|
58ea9e4b35 | ||
|
|
e077849a2c |
2
.gitattributes
vendored
2
.gitattributes
vendored
@@ -1 +1 @@
|
||||
models/* linguist-vendored
|
||||
models/** linguist-vendored
|
||||
|
||||
10
.github/FUNDING.yml
vendored
10
.github/FUNDING.yml
vendored
@@ -1,12 +1,2 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: k4yt3x
|
||||
patreon: k4yt3x
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
|
||||
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@@ -52,6 +52,6 @@ jobs:
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
tag_name: ${{ needs.setup.outputs.version }}
|
||||
release_name: Video2X ${{ needs.setup.outputs.version }}
|
||||
name: ${{ needs.setup.outputs.version }}
|
||||
draft: true
|
||||
prerelease: false
|
||||
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,6 +1,6 @@
|
||||
[submodule "third_party/libreal_esrgan_ncnn_vulkan"]
|
||||
path = third_party/libreal_esrgan_ncnn_vulkan
|
||||
url = https://github.com/k4yt3x/libreal-esrgan-ncnn-vulkan.git
|
||||
[submodule "third_party/librealesrgan_ncnn_vulkan"]
|
||||
path = third_party/librealesrgan_ncnn_vulkan
|
||||
url = https://github.com/k4yt3x/librealesrgan-ncnn-vulkan.git
|
||||
[submodule "third_party/ncnn"]
|
||||
path = third_party/ncnn
|
||||
url = https://github.com/Tencent/ncnn.git
|
||||
|
||||
@@ -28,6 +28,13 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Set global compile options for all targets
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
add_compile_options(/W4 /permissive-)
|
||||
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic -Wconversion -Wshadow)
|
||||
endif()
|
||||
|
||||
# Build options
|
||||
option(BUILD_SHARED_LIBS "Build libvideo2x as a shared library" ON)
|
||||
option(BUILD_VIDEO2X_CLI "Build the video2x executable" ON)
|
||||
@@ -115,7 +122,6 @@ else()
|
||||
|
||||
# OpenCV
|
||||
if (USE_SYSTEM_OPENCV)
|
||||
cmake_policy(SET CMP0146 OLD)
|
||||
find_package(OpenCV REQUIRED)
|
||||
list(APPEND ALL_INCLUDE_DIRS ${OpenCV_INCLUDE_DIRS}/opencv2)
|
||||
list(APPEND ALL_LIBRARIES opencv_core opencv_videoio)
|
||||
@@ -265,10 +271,10 @@ endif()
|
||||
# Include ExternalProject module
|
||||
include(ExternalProject)
|
||||
|
||||
# Add libreal-esrgan-ncnn-vulkan as an external project
|
||||
# Add librealesrgan-ncnn-vulkan as an external project
|
||||
ExternalProject_Add(
|
||||
realesrgan
|
||||
SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/libreal_esrgan_ncnn_vulkan/src
|
||||
SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/librealesrgan_ncnn_vulkan/src
|
||||
CMAKE_ARGS
|
||||
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
|
||||
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/realesrgan_install
|
||||
@@ -303,12 +309,11 @@ target_include_directories(libvideo2x PRIVATE
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${PROJECT_SOURCE_DIR}/include/libvideo2x
|
||||
${PROJECT_SOURCE_DIR}/third_party/libreal_esrgan_ncnn_vulkan/src
|
||||
${PROJECT_SOURCE_DIR}/third_party/librealesrgan_ncnn_vulkan/src
|
||||
)
|
||||
|
||||
# Compile options for the shared library
|
||||
target_compile_options(libvideo2x PRIVATE
|
||||
-Wall
|
||||
-fPIC
|
||||
$<$<CONFIG:Release>:-Ofast>
|
||||
$<$<CONFIG:Debug>:-g -DDEBUG>
|
||||
@@ -345,10 +350,7 @@ if (BUILD_VIDEO2X_CLI)
|
||||
)
|
||||
|
||||
# Compile options for the executable
|
||||
target_compile_options(video2x PRIVATE
|
||||
-Wall
|
||||
$<$<CONFIG:Debug>:-g -DDEBUG>
|
||||
)
|
||||
target_compile_options(video2x PRIVATE $<$<CONFIG:Debug>:-g -DDEBUG>)
|
||||
|
||||
# Link the executable with the shared library
|
||||
target_link_libraries(video2x PRIVATE ${ALL_LIBRARIES} libvideo2x)
|
||||
|
||||
8
Makefile
8
Makefile
@@ -61,7 +61,7 @@ debian:
|
||||
cmake --build /tmp/build --config Release --target install --parallel
|
||||
|
||||
clean:
|
||||
rm -rf $(BINDIR)
|
||||
rm -vrf $(BINDIR) data/output*.* heaptrack*.zst valgrind.log
|
||||
|
||||
test-realesrgan:
|
||||
LD_LIBRARY_PATH=$(BINDIR) $(BINDIR)/video2x -i $(TEST_VIDEO) -o $(TEST_OUTPUT) \
|
||||
@@ -69,7 +69,7 @@ test-realesrgan:
|
||||
|
||||
test-libplacebo:
|
||||
LD_LIBRARY_PATH=$(BINDIR) $(BINDIR)/video2x -i $(TEST_VIDEO) -o $(TEST_OUTPUT) \
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-mode-a
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-v4-a
|
||||
|
||||
memcheck-realesrgan:
|
||||
LD_LIBRARY_PATH=$(BINDIR) valgrind \
|
||||
@@ -94,7 +94,7 @@ memcheck-libplacebo:
|
||||
--verbose --log-file="valgrind.log" \
|
||||
$(BINDIR)/video2x \
|
||||
-i $(TEST_VIDEO) -o $(TEST_OUTPUT) \
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-mode-a \
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-v4-a \
|
||||
-p veryfast -b 1000000 -q 30
|
||||
|
||||
heaptrack-realesrgan:
|
||||
@@ -108,5 +108,5 @@ heaptrack-libplacebo:
|
||||
LD_LIBRARY_PATH=$(BINDIR) HEAPTRACK_ENABLE_DEBUGINFOD=1 heaptrack \
|
||||
$(BINDIR)/video2x \
|
||||
-i $(TEST_VIDEO) -o $(TEST_OUTPUT) \
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-mode-a \
|
||||
-f libplacebo -w 1920 -h 1080 -s anime4k-v4-a \
|
||||
-p veryfast -b 1000000 -q 30
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
|
||||
## 🌟 Version 6.0.0 Preview
|
||||
|
||||
**[Direct download link for Windows (Installer)](https://github.com/k4yt3x/video2x/releases/download/6.0.0-beta.3/video2x-qt6-windows-amd64-installer.exe)**
|
||||
**[Direct download link for Windows (Installer)](https://github.com/k4yt3x/video2x/releases/download/6.0.0-beta.5/video2x-qt6-windows-amd64-installer.exe)**
|
||||
|
||||

|
||||

|
||||
|
||||
Version 6.0.0 is a complete rewrite of this project in C/C++. It:
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ int init_encoder(
|
||||
int **stream_map
|
||||
);
|
||||
|
||||
int encode_and_write_frame(
|
||||
int write_frame(
|
||||
AVFrame *frame,
|
||||
AVCodecContext *enc_ctx,
|
||||
AVFormatContext *ofmt_ctx,
|
||||
|
||||
@@ -15,7 +15,7 @@ class Filter {
|
||||
virtual ~Filter() = default;
|
||||
virtual int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) = 0;
|
||||
virtual int process_frame(AVFrame *in_frame, AVFrame **out_frame) = 0;
|
||||
virtual int flush(std::vector<AVFrame *> &flushed_frames) { return 0; }
|
||||
virtual int flush(std::vector<AVFrame *> &_) { return 0; }
|
||||
};
|
||||
|
||||
#endif // FILTER_H
|
||||
|
||||
@@ -28,7 +28,7 @@ class LibplaceboFilter : public Filter {
|
||||
LibplaceboFilter(int width, int height, const std::filesystem::path &shader_path);
|
||||
|
||||
// Destructor
|
||||
virtual ~LibplaceboFilter();
|
||||
virtual ~LibplaceboFilter() override;
|
||||
|
||||
// Initializes the filter with decoder and encoder contexts
|
||||
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) override;
|
||||
|
||||
@@ -36,7 +36,7 @@ class RealesrganFilter : public Filter {
|
||||
);
|
||||
|
||||
// Destructor
|
||||
virtual ~RealesrganFilter();
|
||||
virtual ~RealesrganFilter() override;
|
||||
|
||||
// Initializes the filter with decoder and encoder contexts
|
||||
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) override;
|
||||
|
||||
2585
models/libplacebo/anime4k-v4-a+a.glsl
vendored
Normal file
2585
models/libplacebo/anime4k-v4-a+a.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2585
models/libplacebo/anime4k-v4-b+b.glsl
vendored
Normal file
2585
models/libplacebo/anime4k-v4-b+b.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2309
models/libplacebo/anime4k-v4-b.glsl
vendored
Normal file
2309
models/libplacebo/anime4k-v4-b.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1711
models/libplacebo/anime4k-v4-c+a.glsl
vendored
Normal file
1711
models/libplacebo/anime4k-v4-c+a.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1435
models/libplacebo/anime4k-v4-c.glsl
vendored
Normal file
1435
models/libplacebo/anime4k-v4-c.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
9173
models/libplacebo/anime4k-v4.1-gan.glsl
vendored
Normal file
9173
models/libplacebo/anime4k-v4.1-gan.glsl
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,46 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
ANIME4K_COMMIT = "master"
|
||||
GITHUB_GLSL_ROOT = (
|
||||
f"https://raw.githubusercontent.com/bloc97/Anime4K/{ANIME4K_COMMIT}/glsl"
|
||||
)
|
||||
SHADERS_DIR = Path(__file__).parent.parent / "data"
|
||||
|
||||
|
||||
def download_and_combine_files():
|
||||
|
||||
modes = {
|
||||
"ModeA": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
]
|
||||
}
|
||||
|
||||
for mode in modes:
|
||||
file_contents = ""
|
||||
for file in modes[mode]:
|
||||
response = requests.get(file, timeout=5)
|
||||
response.raise_for_status()
|
||||
file_contents += response.text + "\n"
|
||||
|
||||
with (SHADERS_DIR / Path(f"Anime4K_{mode}.glsl")).open("w") as output_file:
|
||||
output_file.write(file_contents)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# clear shaders directory
|
||||
if SHADERS_DIR.exists():
|
||||
shutil.rmtree(SHADERS_DIR)
|
||||
SHADERS_DIR.mkdir(exist_ok=True)
|
||||
|
||||
# download and combine shaders
|
||||
download_and_combine_files()
|
||||
98
scripts/download_merge_anime4k_glsl.py
Executable file
98
scripts/download_merge_anime4k_glsl.py
Executable file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
ANIME4K_COMMIT = "master"
|
||||
GITHUB_GLSL_ROOT = (
|
||||
f"https://raw.githubusercontent.com/bloc97/Anime4K/{ANIME4K_COMMIT}/glsl"
|
||||
)
|
||||
SHADERS_DIR = Path(__file__).parent.parent / "models" / "libplacebo"
|
||||
|
||||
|
||||
def download_and_combine_files():
|
||||
modes = {
|
||||
"a": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"b": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_Soft_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"c": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale+Denoise/Anime4K_Upscale_Denoise_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"a+a": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_M.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"b+b": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_Soft_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_Soft_M.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"c+a": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Clamp_Highlights.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale+Denoise/Anime4K_Upscale_Denoise_CNN_x2_VL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x2.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_AutoDownscalePre_x4.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_M.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
"gan": [
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_GAN_UUL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_GAN_x4_UUL.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Restore/Anime4K_Restore_CNN_Soft_M.glsl",
|
||||
f"{GITHUB_GLSL_ROOT}/Upscale/Anime4K_Upscale_CNN_x2_M.glsl",
|
||||
],
|
||||
}
|
||||
|
||||
for mode in modes:
|
||||
file_contents = ""
|
||||
for file in modes[mode]:
|
||||
response = requests.get(file, timeout=5)
|
||||
response.raise_for_status()
|
||||
file_contents += response.text + "\n"
|
||||
|
||||
version = "v4"
|
||||
if mode == "gan":
|
||||
version = "v4.1"
|
||||
|
||||
with (SHADERS_DIR / Path(f"anime4k-{version}-{mode}.glsl")).open(
|
||||
"w"
|
||||
) as output_file:
|
||||
output_file.write(file_contents)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# clear shaders directory
|
||||
if SHADERS_DIR.exists():
|
||||
shutil.rmtree(SHADERS_DIR)
|
||||
SHADERS_DIR.mkdir(exist_ok=True)
|
||||
|
||||
# download and combine shaders
|
||||
download_and_combine_files()
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "conversions.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -79,14 +80,16 @@ ncnn::Mat avframe_to_ncnn_mat(AVFrame *frame) {
|
||||
// Allocate a new ncnn::Mat and copy the data
|
||||
int width = converted_frame->width;
|
||||
int height = converted_frame->height;
|
||||
ncnn::Mat ncnn_image = ncnn::Mat(width, height, (size_t)3, 3); // BGR has 3 channels
|
||||
ncnn::Mat ncnn_image = ncnn::Mat(width, height, static_cast<size_t>(3), 3);
|
||||
|
||||
// Manually copy the pixel data from AVFrame to the new ncnn::Mat
|
||||
const uint8_t *src_data = converted_frame->data[0];
|
||||
for (int y = 0; y < height; y++) {
|
||||
uint8_t *dst_row = ncnn_image.row<uint8_t>(y);
|
||||
const uint8_t *src_row = src_data + y * converted_frame->linesize[0];
|
||||
memcpy(dst_row, src_row, width * 3); // Copy 3 channels (BGR) per pixel
|
||||
|
||||
// Copy 3 channels (BGR) per pixel
|
||||
memcpy(dst_row, src_row, static_cast<size_t>(width) * 3);
|
||||
}
|
||||
|
||||
// If we allocated a converted frame, free it
|
||||
@@ -143,7 +146,9 @@ AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt) {
|
||||
for (int y = 0; y < mat.h; y++) {
|
||||
uint8_t *dst_row = bgr_frame->data[0] + y * bgr_frame->linesize[0];
|
||||
const uint8_t *src_row = mat.row<const uint8_t>(y);
|
||||
memcpy(dst_row, src_row, mat.w * 3); // Copy 3 channels (BGR) per pixel
|
||||
|
||||
// Copy 3 channels (BGR) per pixel
|
||||
memcpy(dst_row, src_row, static_cast<size_t>(mat.w) * 3);
|
||||
}
|
||||
|
||||
// Step 3: Convert the BGR frame to the desired pixel format
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
static enum AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE;
|
||||
|
||||
// Callback function to choose the hardware-accelerated pixel format
|
||||
static enum AVPixelFormat get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) {
|
||||
static enum AVPixelFormat get_hw_format(AVCodecContext *_, const enum AVPixelFormat *pix_fmts) {
|
||||
for (const enum AVPixelFormat *p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
|
||||
if (*p == hw_pix_fmt) {
|
||||
return *p;
|
||||
@@ -55,7 +55,8 @@ int init_decoder(
|
||||
const AVCodec *decoder = avcodec_find_decoder(video_stream->codecpar->codec_id);
|
||||
if (!decoder) {
|
||||
spdlog::error(
|
||||
"Failed to find decoder for codec ID {}", (int)video_stream->codecpar->codec_id
|
||||
"Failed to find decoder for codec ID {}",
|
||||
static_cast<int>(video_stream->codecpar->codec_id)
|
||||
);
|
||||
return AVERROR_DECODER_NOT_FOUND;
|
||||
}
|
||||
|
||||
@@ -85,15 +85,23 @@ int init_encoder(
|
||||
}
|
||||
}
|
||||
|
||||
// Set the time base
|
||||
codec_ctx->time_base = av_inv_q(dec_ctx->framerate);
|
||||
if (codec_ctx->time_base.num == 0 || codec_ctx->time_base.den == 0) {
|
||||
// Set the output video's time base
|
||||
if (dec_ctx->time_base.num > 0 && dec_ctx->time_base.den > 0) {
|
||||
codec_ctx->time_base = dec_ctx->time_base;
|
||||
} else {
|
||||
codec_ctx->time_base = av_inv_q(av_guess_frame_rate(ifmt_ctx, out_stream, NULL));
|
||||
}
|
||||
|
||||
// Set the output video's frame rate
|
||||
if (dec_ctx->framerate.num > 0 && dec_ctx->framerate.den > 0) {
|
||||
codec_ctx->framerate = dec_ctx->framerate;
|
||||
} else {
|
||||
codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, out_stream, NULL);
|
||||
}
|
||||
|
||||
// Set the CRF and preset for any codecs that support it
|
||||
char crf_str[16];
|
||||
snprintf(crf_str, sizeof(crf_str), "%.f", encoder_config->crf);
|
||||
snprintf(crf_str, sizeof(crf_str), "%.f", static_cast<double>(encoder_config->crf));
|
||||
av_opt_set(codec_ctx->priv_data, "crf", crf_str, 0);
|
||||
av_opt_set(codec_ctx->priv_data, "preset", encoder_config->preset, 0);
|
||||
|
||||
@@ -113,10 +121,13 @@ int init_encoder(
|
||||
}
|
||||
|
||||
out_stream->time_base = codec_ctx->time_base;
|
||||
out_stream->avg_frame_rate = codec_ctx->framerate;
|
||||
out_stream->r_frame_rate = codec_ctx->framerate;
|
||||
|
||||
if (encoder_config->copy_streams) {
|
||||
// Allocate the stream map
|
||||
*stream_map = (int *)av_malloc_array(ifmt_ctx->nb_streams, sizeof(**stream_map));
|
||||
*stream_map =
|
||||
reinterpret_cast<int *>(av_malloc_array(ifmt_ctx->nb_streams, sizeof(**stream_map)));
|
||||
if (!*stream_map) {
|
||||
spdlog::error("Could not allocate stream mapping");
|
||||
return AVERROR(ENOMEM);
|
||||
@@ -126,7 +137,7 @@ int init_encoder(
|
||||
(*stream_map)[vstream_idx] = stream_index++;
|
||||
|
||||
// Loop through each stream in the input file
|
||||
for (int i = 0; i < ifmt_ctx->nb_streams; i++) {
|
||||
for (int i = 0; i < static_cast<int>(ifmt_ctx->nb_streams); i++) {
|
||||
AVStream *in_stream = ifmt_ctx->streams[i];
|
||||
AVCodecParameters *in_codecpar = in_stream->codecpar;
|
||||
|
||||
@@ -142,21 +153,21 @@ int init_encoder(
|
||||
}
|
||||
|
||||
// Create corresponding output stream
|
||||
AVStream *out_stream = avformat_new_stream(fmt_ctx, NULL);
|
||||
if (!out_stream) {
|
||||
AVStream *out_copied_stream = avformat_new_stream(fmt_ctx, NULL);
|
||||
if (!out_copied_stream) {
|
||||
spdlog::error("Failed allocating output stream");
|
||||
return AVERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
|
||||
ret = avcodec_parameters_copy(out_copied_stream->codecpar, in_codecpar);
|
||||
if (ret < 0) {
|
||||
spdlog::error("Failed to copy codec parameters");
|
||||
return ret;
|
||||
}
|
||||
out_stream->codecpar->codec_tag = 0;
|
||||
out_copied_stream->codecpar->codec_tag = 0;
|
||||
|
||||
// Copy time base
|
||||
out_stream->time_base = in_stream->time_base;
|
||||
out_copied_stream->time_base = in_stream->time_base;
|
||||
|
||||
(*stream_map)[i] = stream_index++;
|
||||
}
|
||||
@@ -177,7 +188,7 @@ int init_encoder(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int encode_and_write_frame(
|
||||
int write_frame(
|
||||
AVFrame *frame,
|
||||
AVCodecContext *enc_ctx,
|
||||
AVFormatContext *ofmt_ctx,
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#if _WIN32
|
||||
std::filesystem::path get_executable_directory() {
|
||||
static std::filesystem::path get_executable_directory() {
|
||||
std::vector<wchar_t> filepath(MAX_PATH);
|
||||
|
||||
// Get the executable path, expanding the buffer if necessary
|
||||
@@ -36,7 +36,7 @@ std::filesystem::path get_executable_directory() {
|
||||
return execpath.parent_path();
|
||||
}
|
||||
#else // _WIN32
|
||||
std::filesystem::path get_executable_directory() {
|
||||
static std::filesystem::path get_executable_directory() {
|
||||
std::error_code ec;
|
||||
std::filesystem::path filepath = std::filesystem::read_symlink("/proc/self/exe", ec);
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ int getopt_long(
|
||||
return (-1);
|
||||
}
|
||||
if ((has_equal = strchr(current_argv, '=')) != NULL) {
|
||||
current_argv_len = has_equal - current_argv;
|
||||
current_argv_len = (size_t)(has_equal - current_argv);
|
||||
has_equal++;
|
||||
} else {
|
||||
current_argv_len = strlen(current_argv);
|
||||
|
||||
@@ -72,16 +72,23 @@ int init_libplacebo(
|
||||
#endif
|
||||
|
||||
// Prepare the filter arguments
|
||||
char filter_args[512];
|
||||
snprintf(
|
||||
char filter_args[4096];
|
||||
int filter_args_size = snprintf(
|
||||
filter_args,
|
||||
sizeof(filter_args),
|
||||
"w=%d:h=%d:upscaler=ewa_lanczos:custom_shader_path=%s",
|
||||
"w=%d:h=%d:custom_shader_path='%s'",
|
||||
out_width,
|
||||
out_height,
|
||||
shader_path_string.c_str()
|
||||
);
|
||||
|
||||
// Check if the filter arguments are too long
|
||||
if (filter_args_size < 0 || filter_args_size >= static_cast<int>(sizeof(filter_args))) {
|
||||
spdlog::error("libplacebo filter arguments too long.");
|
||||
avfilter_graph_free(&graph);
|
||||
return AVERROR(EINVAL);
|
||||
}
|
||||
|
||||
AVFilterContext *libplacebo_ctx;
|
||||
ret = avfilter_graph_create_filter(
|
||||
&libplacebo_ctx, libplacebo_filter, "libplacebo", filter_args, NULL, graph
|
||||
|
||||
@@ -42,8 +42,9 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
|
||||
shader_full_path = shader_path;
|
||||
} else {
|
||||
// Construct the fallback path using std::filesystem
|
||||
shader_full_path =
|
||||
find_resource_file(std::filesystem::path("models") / (shader_path.string() + ".glsl"));
|
||||
shader_full_path = find_resource_file(
|
||||
std::filesystem::path("models") / "libplacebo" / (shader_path.string() + ".glsl")
|
||||
);
|
||||
}
|
||||
|
||||
// Check if the shader file exists
|
||||
@@ -56,7 +57,8 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
|
||||
in_time_base = dec_ctx->time_base;
|
||||
out_time_base = enc_ctx->time_base;
|
||||
|
||||
return init_libplacebo(
|
||||
// Initialize the libplacebo filter
|
||||
int ret = init_libplacebo(
|
||||
hw_ctx,
|
||||
&filter_graph,
|
||||
&buffersrc_ctx,
|
||||
@@ -66,6 +68,14 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
|
||||
out_height,
|
||||
shader_full_path
|
||||
);
|
||||
|
||||
// Set these resources to nullptr since they are already freed by `avfilter_graph_free`
|
||||
if (ret < 0) {
|
||||
buffersrc_ctx = nullptr;
|
||||
buffersink_ctx = nullptr;
|
||||
filter_graph = nullptr;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int LibplaceboFilter::process_frame(AVFrame *in_frame, AVFrame **out_frame) {
|
||||
@@ -82,6 +92,7 @@ int LibplaceboFilter::process_frame(AVFrame *in_frame, AVFrame **out_frame) {
|
||||
ret = av_buffersrc_add_frame(buffersrc_ctx, in_frame);
|
||||
if (ret < 0) {
|
||||
spdlog::error("Error while feeding the filter graph");
|
||||
av_frame_free(out_frame);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -30,7 +29,7 @@
|
||||
* @param[in] benchmark Flag to enable benchmarking mode
|
||||
* @return int 0 on success, negative value on error
|
||||
*/
|
||||
int process_frames(
|
||||
static int process_frames(
|
||||
EncoderConfig *encoder_config,
|
||||
VideoProcessingContext *proc_ctx,
|
||||
AVFormatContext *ifmt_ctx,
|
||||
@@ -43,18 +42,17 @@ int process_frames(
|
||||
bool benchmark = false
|
||||
) {
|
||||
int ret;
|
||||
AVPacket packet;
|
||||
std::vector<AVFrame *> flushed_frames;
|
||||
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
||||
std::vector<AVFrame *> flushed_frames;
|
||||
|
||||
// Get the total number of frames in the video with OpenCV
|
||||
spdlog::debug("Unable to estimate total number of frames; reading with OpenCV");
|
||||
spdlog::debug("Reading total number of frames 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);
|
||||
proc_ctx->total_frames = static_cast<int64_t>(cap.get(cv::CAP_PROP_FRAME_COUNT));
|
||||
cap.release();
|
||||
|
||||
// Check if the total number of frames is still 0
|
||||
@@ -73,12 +71,31 @@ int process_frames(
|
||||
AVFrame *frame = av_frame_alloc();
|
||||
if (frame == nullptr) {
|
||||
ret = AVERROR(ENOMEM);
|
||||
goto end;
|
||||
return ret;
|
||||
}
|
||||
|
||||
AVPacket *packet = av_packet_alloc();
|
||||
if (packet == nullptr) {
|
||||
spdlog::error("Could not allocate AVPacket");
|
||||
av_frame_free(&frame);
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
// Lambda function for cleaning up resources
|
||||
auto cleanup = [&]() {
|
||||
av_frame_free(&frame);
|
||||
av_packet_free(&packet);
|
||||
for (AVFrame *&flushed_frame : flushed_frames) {
|
||||
if (flushed_frame) {
|
||||
av_frame_free(&flushed_frame);
|
||||
flushed_frame = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Read frames from the input file
|
||||
while (!proc_ctx->abort) {
|
||||
ret = av_read_frame(ifmt_ctx, &packet);
|
||||
ret = av_read_frame(ifmt_ctx, packet);
|
||||
if (ret < 0) {
|
||||
if (ret == AVERROR_EOF) {
|
||||
spdlog::debug("Reached end of file");
|
||||
@@ -86,22 +103,21 @@ int process_frames(
|
||||
}
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error reading packet: {}", errbuf);
|
||||
goto end;
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (packet.stream_index == vstream_idx) {
|
||||
// Send the packet to the decoder
|
||||
ret = avcodec_send_packet(dec_ctx, &packet);
|
||||
if (packet->stream_index == vstream_idx) {
|
||||
ret = avcodec_send_packet(dec_ctx, packet);
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error sending packet to decoder: {}", errbuf);
|
||||
av_packet_unref(&packet);
|
||||
goto end;
|
||||
av_packet_unref(packet);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Receive and process frames from the decoder
|
||||
while (!proc_ctx->abort) {
|
||||
// Check if the processing is paused
|
||||
if (proc_ctx->pause) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
continue;
|
||||
@@ -114,30 +130,33 @@ int process_frames(
|
||||
} else if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error decoding video frame: {}", errbuf);
|
||||
goto end;
|
||||
av_packet_unref(packet);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Process the frame using the selected filter
|
||||
AVFrame *processed_frame = nullptr;
|
||||
ret = filter->process_frame(frame, &processed_frame);
|
||||
if (ret == 0 && processed_frame != nullptr) {
|
||||
// Encode and write the processed frame
|
||||
if (ret < 0 && ret != AVERROR(EAGAIN)) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
av_frame_free(&processed_frame);
|
||||
av_packet_unref(packet);
|
||||
cleanup();
|
||||
return ret;
|
||||
} else if (ret == 0 && processed_frame != nullptr) {
|
||||
if (!benchmark) {
|
||||
ret =
|
||||
encode_and_write_frame(processed_frame, enc_ctx, ofmt_ctx, vstream_idx);
|
||||
ret = write_frame(processed_frame, enc_ctx, ofmt_ctx, vstream_idx);
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error encoding/writing frame: {}", errbuf);
|
||||
av_frame_free(&processed_frame);
|
||||
goto end;
|
||||
av_packet_unref(packet);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
av_frame_free(&processed_frame);
|
||||
proc_ctx->processed_frames++;
|
||||
} else if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
||||
spdlog::error("Filter returned an error");
|
||||
goto end;
|
||||
}
|
||||
|
||||
av_frame_unref(frame);
|
||||
@@ -145,25 +164,24 @@ int process_frames(
|
||||
"Processed frame {}/{}", proc_ctx->processed_frames, proc_ctx->total_frames
|
||||
);
|
||||
}
|
||||
} else if (encoder_config->copy_streams && stream_map[packet.stream_index] >= 0) {
|
||||
AVStream *in_stream = ifmt_ctx->streams[packet.stream_index];
|
||||
int out_stream_index = stream_map[packet.stream_index];
|
||||
} else if (encoder_config->copy_streams && stream_map[packet->stream_index] >= 0) {
|
||||
AVStream *in_stream = ifmt_ctx->streams[packet->stream_index];
|
||||
int out_stream_index = stream_map[packet->stream_index];
|
||||
AVStream *out_stream = ofmt_ctx->streams[out_stream_index];
|
||||
|
||||
// Rescale packet timestamps
|
||||
av_packet_rescale_ts(&packet, in_stream->time_base, out_stream->time_base);
|
||||
packet.stream_index = out_stream_index;
|
||||
av_packet_rescale_ts(packet, in_stream->time_base, out_stream->time_base);
|
||||
packet->stream_index = out_stream_index;
|
||||
|
||||
// If copy streams is enabled, copy the packet to the output
|
||||
ret = av_interleaved_write_frame(ofmt_ctx, &packet);
|
||||
ret = av_interleaved_write_frame(ofmt_ctx, packet);
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error muxing packet: {}", errbuf);
|
||||
av_packet_unref(&packet);
|
||||
av_packet_unref(packet);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
av_packet_unref(&packet);
|
||||
av_packet_unref(packet);
|
||||
}
|
||||
|
||||
// Flush the filter
|
||||
@@ -171,21 +189,24 @@ int process_frames(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error flushing filter: {}", errbuf);
|
||||
goto end;
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Encode and write all flushed frames
|
||||
for (AVFrame *&flushed_frame : flushed_frames) {
|
||||
ret = encode_and_write_frame(flushed_frame, enc_ctx, ofmt_ctx, vstream_idx);
|
||||
ret = write_frame(flushed_frame, enc_ctx, ofmt_ctx, vstream_idx);
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error encoding/writing flushed frame: {}", errbuf);
|
||||
av_frame_free(&flushed_frame);
|
||||
flushed_frame = nullptr;
|
||||
goto end;
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
av_frame_free(&flushed_frame);
|
||||
flushed_frame = nullptr;
|
||||
proc_ctx->processed_frames++;
|
||||
}
|
||||
|
||||
// Flush the encoder
|
||||
@@ -193,56 +214,14 @@ int process_frames(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error flushing encoder: {}", errbuf);
|
||||
goto end;
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
end:
|
||||
av_frame_free(&frame);
|
||||
// Free any flushed frames not yet freed
|
||||
for (AVFrame *flushed_frame : flushed_frames) {
|
||||
if (flushed_frame) {
|
||||
av_frame_free(&flushed_frame);
|
||||
}
|
||||
}
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Cleanup resources after processing the video
|
||||
void cleanup(
|
||||
AVFormatContext *ifmt_ctx,
|
||||
AVFormatContext *ofmt_ctx,
|
||||
AVCodecContext *dec_ctx,
|
||||
AVCodecContext *enc_ctx,
|
||||
AVBufferRef *hw_ctx,
|
||||
int *stream_map,
|
||||
Filter *filter
|
||||
) {
|
||||
if (ifmt_ctx) {
|
||||
avformat_close_input(&ifmt_ctx);
|
||||
}
|
||||
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
|
||||
avio_closep(&ofmt_ctx->pb);
|
||||
}
|
||||
if (ofmt_ctx) {
|
||||
avformat_free_context(ofmt_ctx);
|
||||
}
|
||||
if (dec_ctx) {
|
||||
avcodec_free_context(&dec_ctx);
|
||||
}
|
||||
if (enc_ctx) {
|
||||
avcodec_free_context(&enc_ctx);
|
||||
}
|
||||
if (hw_ctx) {
|
||||
av_buffer_unref(&hw_ctx);
|
||||
}
|
||||
if (stream_map) {
|
||||
av_free(stream_map);
|
||||
}
|
||||
if (filter) {
|
||||
delete filter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Process a video file using the selected filter and encoder settings.
|
||||
*
|
||||
@@ -277,6 +256,42 @@ extern "C" int process_video(
|
||||
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
||||
int ret = 0;
|
||||
|
||||
// Lambda function for cleaning up resources
|
||||
auto cleanup = [&]() {
|
||||
if (ifmt_ctx) {
|
||||
avformat_close_input(&ifmt_ctx);
|
||||
ifmt_ctx = nullptr;
|
||||
}
|
||||
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
|
||||
avio_closep(&ofmt_ctx->pb);
|
||||
ofmt_ctx->pb = nullptr;
|
||||
}
|
||||
if (ofmt_ctx) {
|
||||
avformat_free_context(ofmt_ctx);
|
||||
ofmt_ctx = nullptr;
|
||||
}
|
||||
if (dec_ctx) {
|
||||
avcodec_free_context(&dec_ctx);
|
||||
dec_ctx = nullptr;
|
||||
}
|
||||
if (enc_ctx) {
|
||||
avcodec_free_context(&enc_ctx);
|
||||
enc_ctx = nullptr;
|
||||
}
|
||||
if (hw_ctx) {
|
||||
av_buffer_unref(&hw_ctx);
|
||||
hw_ctx = nullptr;
|
||||
}
|
||||
if (stream_map) {
|
||||
av_free(stream_map);
|
||||
stream_map = nullptr;
|
||||
}
|
||||
if (filter) {
|
||||
delete filter;
|
||||
filter = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
// Set the log level for FFmpeg and spdlog (libvideo2x)
|
||||
switch (log_level) {
|
||||
case LIBVIDEO2X_LOG_LEVEL_TRACE:
|
||||
@@ -319,6 +334,7 @@ extern "C" int process_video(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error initializing hardware device context: {}", errbuf);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -328,11 +344,11 @@ extern "C" int process_video(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Failed to initialize decoder: {}", errbuf);
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Initialize output based on Libplacebo or RealESRGAN configuration
|
||||
// Initialize output dimensions based on filter configuration
|
||||
int output_width = 0, output_height = 0;
|
||||
switch (filter_config->filter_type) {
|
||||
case FILTER_LIBPLACEBO:
|
||||
@@ -340,9 +356,13 @@ extern "C" int process_video(
|
||||
output_height = filter_config->config.libplacebo.out_height;
|
||||
break;
|
||||
case FILTER_REALESRGAN:
|
||||
// Calculate the output dimensions based on the scaling factor
|
||||
output_width = dec_ctx->width * filter_config->config.realesrgan.scaling_factor;
|
||||
output_height = dec_ctx->height * filter_config->config.realesrgan.scaling_factor;
|
||||
break;
|
||||
default:
|
||||
spdlog::error("Unknown filter type");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
spdlog::info("Output video dimensions: {}x{}", output_width, output_height);
|
||||
|
||||
@@ -363,7 +383,7 @@ extern "C" int process_video(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Failed to initialize encoder: {}", errbuf);
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -372,68 +392,49 @@ extern "C" int process_video(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error occurred when opening output file: {}", errbuf);
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Create and initialize the appropriate filter
|
||||
switch (filter_config->filter_type) {
|
||||
case FILTER_LIBPLACEBO: {
|
||||
const auto &config = filter_config->config.libplacebo;
|
||||
|
||||
// Validate shader path
|
||||
if (!config.shader_path) {
|
||||
spdlog::error("Shader path must be provided for the libplacebo filter");
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Validate output dimensions
|
||||
if (config.out_width <= 0 || config.out_height <= 0) {
|
||||
spdlog::error("Output dimensions must be provided for the libplacebo filter");
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
filter = new LibplaceboFilter{
|
||||
config.out_width, config.out_height, std::filesystem::path(config.shader_path)
|
||||
};
|
||||
break;
|
||||
}
|
||||
case FILTER_REALESRGAN: {
|
||||
const auto &config = filter_config->config.realesrgan;
|
||||
|
||||
// Validate model name
|
||||
if (!config.model) {
|
||||
spdlog::error("Model name must be provided for the RealESRGAN filter");
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Validate scaling factor
|
||||
if (config.scaling_factor <= 0) {
|
||||
spdlog::error("Scaling factor must be provided for the RealESRGAN filter");
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
filter = new RealesrganFilter{
|
||||
config.gpuid, config.tta_mode, config.scaling_factor, config.model
|
||||
};
|
||||
break;
|
||||
}
|
||||
default:
|
||||
spdlog::error("Unknown filter type");
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
if (filter_config->filter_type == FILTER_LIBPLACEBO) {
|
||||
const auto &config = filter_config->config.libplacebo;
|
||||
if (!config.shader_path) {
|
||||
spdlog::error("Shader path must be provided for the libplacebo filter");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
filter = new LibplaceboFilter{
|
||||
config.out_width, config.out_height, std::filesystem::path(config.shader_path)
|
||||
};
|
||||
} else if (filter_config->filter_type == FILTER_REALESRGAN) {
|
||||
const auto &config = filter_config->config.realesrgan;
|
||||
if (!config.model) {
|
||||
spdlog::error("Model name must be provided for the RealESRGAN filter");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
filter = new RealesrganFilter{
|
||||
config.gpuid, config.tta_mode, config.scaling_factor, config.model
|
||||
};
|
||||
} else {
|
||||
spdlog::error("Unknown filter type");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Check if the filter instance was created successfully
|
||||
if (filter == nullptr) {
|
||||
spdlog::error("Failed to create filter instance");
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Initialize the filter
|
||||
ret = filter->init(dec_ctx, enc_ctx, hw_ctx);
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Failed to initialize filter: {}", errbuf);
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
spdlog::error("Failed to initialize filter");
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -453,7 +454,7 @@ extern "C" int process_video(
|
||||
if (ret < 0) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
spdlog::error("Error processing frames: {}", errbuf);
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -461,7 +462,7 @@ extern "C" int process_video(
|
||||
av_write_trailer(ofmt_ctx);
|
||||
|
||||
// Cleanup before returning
|
||||
cleanup(ifmt_ctx, ofmt_ctx, dec_ctx, enc_ctx, hw_ctx, stream_map, filter);
|
||||
cleanup();
|
||||
|
||||
if (ret < 0 && ret != AVERROR_EOF) {
|
||||
av_strerror(ret, errbuf, sizeof(errbuf));
|
||||
|
||||
@@ -32,16 +32,16 @@ RealesrganFilter::~RealesrganFilter() {
|
||||
}
|
||||
}
|
||||
|
||||
int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) {
|
||||
int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *_) {
|
||||
// Construct the model paths using std::filesystem
|
||||
std::filesystem::path model_param_path;
|
||||
std::filesystem::path model_bin_path;
|
||||
|
||||
if (model) {
|
||||
// Find the model paths by model name if provided
|
||||
model_param_path = std::filesystem::path("models") /
|
||||
model_param_path = std::filesystem::path("models") / "realesrgan" /
|
||||
(std::string(model) + "-x" + std::to_string(scaling_factor) + ".param");
|
||||
model_bin_path = std::filesystem::path("models") /
|
||||
model_bin_path = std::filesystem::path("models") / "realesrgan" /
|
||||
(std::string(model) + "-x" + std::to_string(scaling_factor) + ".bin");
|
||||
} else if (!custom_model_param_path.empty() && !custom_model_bin_path.empty()) {
|
||||
// Use the custom model paths if provided
|
||||
@@ -113,7 +113,7 @@ int RealesrganFilter::process_frame(AVFrame *in_frame, AVFrame **out_frame) {
|
||||
// Allocate space for ouptut ncnn::Mat
|
||||
int output_width = in_mat.w * realesrgan->scale;
|
||||
int output_height = in_mat.h * realesrgan->scale;
|
||||
ncnn::Mat out_mat = ncnn::Mat(output_width, output_height, (size_t)3, 3);
|
||||
ncnn::Mat out_mat = ncnn::Mat(output_width, output_height, static_cast<size_t>(3), 3);
|
||||
|
||||
ret = realesrgan->process(in_mat, out_mat);
|
||||
if (ret != 0) {
|
||||
|
||||
@@ -111,7 +111,7 @@ void set_nonblocking_input(bool enable) {
|
||||
if (enable) {
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ICANON | ECHO);
|
||||
newt.c_lflag &= ~(tcflag_t)(ICANON | ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
|
||||
} else {
|
||||
@@ -134,7 +134,9 @@ int is_valid_realesrgan_model(const char *model) {
|
||||
if (!model) {
|
||||
return 0;
|
||||
}
|
||||
for (int i = 0; i < sizeof(valid_realesrgan_models) / sizeof(valid_realesrgan_models[0]); i++) {
|
||||
for (unsigned long i = 0;
|
||||
i < sizeof(valid_realesrgan_models) / sizeof(valid_realesrgan_models[0]);
|
||||
i++) {
|
||||
if (strcmp(model, valid_realesrgan_models[i]) == 0) {
|
||||
return 1;
|
||||
}
|
||||
@@ -142,12 +144,11 @@ int is_valid_realesrgan_model(const char *model) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_help() {
|
||||
void print_help(void) {
|
||||
printf("Usage: video2x [OPTIONS]\n");
|
||||
printf("\nOptions:\n");
|
||||
printf(
|
||||
" --loglevel Set log level (trace, debug, info, warn, error, critical, none)\n"
|
||||
);
|
||||
printf(" --loglevel Set log level \n");
|
||||
printf(" (trace, debug, info, warn, error, critical, none)\n");
|
||||
printf(" --noprogress Do not display the progress bar\n");
|
||||
printf(" -v, --version Print program version\n");
|
||||
printf(" -?, --help Display this help page\n");
|
||||
@@ -167,7 +168,9 @@ void print_help() {
|
||||
printf(" -q, --crf Constant Rate Factor (default: 20.0)\n");
|
||||
|
||||
printf("\nlibplacebo Options:\n");
|
||||
printf(" -s, --shader Name or path to custom GLSL shader file\n");
|
||||
printf(" -s, --shader Name or path of the GLSL shader file to use \n");
|
||||
printf(" (built-in: 'anime4k-a', 'anime4k-b', 'anime4k-c',\n");
|
||||
printf(" 'anime4k-a+a', 'anime4k-b+b', 'anime4k-c+a')\n");
|
||||
printf(" -w, --width Output width\n");
|
||||
printf(" -h, --height Output height\n");
|
||||
|
||||
@@ -177,7 +180,7 @@ void print_help() {
|
||||
printf(" -r, --scale Scaling factor (2, 3, or 4)\n");
|
||||
|
||||
printf("\nExamples Usage:\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 libplacebo -s anime4k-a+a -w 3840 -h 2160\n");
|
||||
printf(" video2x -i in.mp4 -o out.mp4 -f realesrgan -m realesr-animevideov3 -r 4\n");
|
||||
}
|
||||
|
||||
@@ -213,7 +216,7 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
|
||||
arguments->scaling_factor = 0;
|
||||
|
||||
while ((c = getopt_long(
|
||||
argc, argv, "i:o:f:a:c:x:p:b:q:s:w:h:r:m:v", long_options, &option_index
|
||||
argc, argv, "i:o:f:a:c:x:p:b:q:s:w:h:g:m:r:v", long_options, &option_index
|
||||
)) != -1) {
|
||||
switch (c) {
|
||||
case 'i':
|
||||
@@ -245,7 +248,7 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
arguments->crf = atof(optarg);
|
||||
arguments->crf = (float)atof(optarg);
|
||||
if (arguments->crf < 0.0 || arguments->crf > 51.0) {
|
||||
fprintf(stderr, "Error: CRF must be between 0 and 51.\n");
|
||||
exit(1);
|
||||
@@ -334,7 +337,7 @@ void parse_arguments(int argc, char **argv, struct arguments *arguments) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"Error: For libplacebo, shader name/path (-s), width (-w), "
|
||||
"and height (-e) are required.\n"
|
||||
"and height (-h) are required.\n"
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
@@ -538,7 +541,7 @@ int main(int argc, char **argv) {
|
||||
proc_ctx.processed_frames,
|
||||
proc_ctx.total_frames,
|
||||
proc_ctx.total_frames > 0
|
||||
? proc_ctx.processed_frames * 100.0 / proc_ctx.total_frames
|
||||
? (double)proc_ctx.processed_frames * 100.0 / (double)proc_ctx.total_frames
|
||||
: 0.0,
|
||||
time(NULL) - proc_ctx.start_time
|
||||
);
|
||||
@@ -578,7 +581,7 @@ int main(int argc, char **argv) {
|
||||
// Calculate statistics
|
||||
time_t time_elapsed = time(NULL) - proc_ctx.start_time;
|
||||
float average_speed_fps =
|
||||
(float)proc_ctx.processed_frames / (time_elapsed > 0 ? time_elapsed : 1);
|
||||
(float)proc_ctx.processed_frames / (time_elapsed > 0 ? (float)time_elapsed : 1);
|
||||
|
||||
// Print processing summary
|
||||
printf("====== Video2X %s summary ======\n", arguments.benchmark ? "Benchmark" : "Processing");
|
||||
|
||||
1
third_party/libreal_esrgan_ncnn_vulkan
vendored
1
third_party/libreal_esrgan_ncnn_vulkan
vendored
Submodule third_party/libreal_esrgan_ncnn_vulkan deleted from 3e633ddb4f
1
third_party/librealesrgan_ncnn_vulkan
vendored
Submodule
1
third_party/librealesrgan_ncnn_vulkan
vendored
Submodule
Submodule third_party/librealesrgan_ncnn_vulkan added at cd68df6f98
Reference in New Issue
Block a user