mirror of
https://github.com/k4yt3x/video2x.git
synced 2024-12-28 23:19:11 +00:00
feat(libplacebo): auto-detect the buffer filter's supported options
Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
parent
c912bfaffc
commit
a8b952c3ad
@ -100,9 +100,8 @@ int init_encoder(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the CRF and preset for any codecs that support it
|
// Set the CRF and preset for any codecs that support it
|
||||||
char crf_str[16];
|
std::string crf_str = std::to_string(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.c_str(), 0);
|
||||||
av_opt_set(codec_ctx->priv_data, "crf", crf_str, 0);
|
|
||||||
av_opt_set(codec_ctx->priv_data, "preset", encoder_config->preset, 0);
|
av_opt_set(codec_ctx->priv_data, "preset", encoder_config->preset, 0);
|
||||||
|
|
||||||
if (fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
|
if (fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
|
||||||
|
@ -3,6 +3,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <libavutil/opt.h>
|
||||||
|
}
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include "fsutils.h"
|
#include "fsutils.h"
|
||||||
@ -17,7 +21,6 @@ int init_libplacebo(
|
|||||||
int out_height,
|
int out_height,
|
||||||
const std::filesystem::path &shader_path
|
const std::filesystem::path &shader_path
|
||||||
) {
|
) {
|
||||||
char args[512];
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
AVFilterGraph *graph = avfilter_graph_alloc();
|
AVFilterGraph *graph = avfilter_graph_alloc();
|
||||||
@ -28,25 +31,43 @@ int init_libplacebo(
|
|||||||
|
|
||||||
// Create buffer source
|
// Create buffer source
|
||||||
const AVFilter *buffersrc = avfilter_get_by_name("buffer");
|
const AVFilter *buffersrc = avfilter_get_by_name("buffer");
|
||||||
snprintf(
|
if (!buffersrc) {
|
||||||
args,
|
spdlog::error("Filter 'buffer' not found.");
|
||||||
sizeof(args),
|
avfilter_graph_free(&graph);
|
||||||
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:frame_rate=%d/%d:"
|
return AVERROR_FILTER_NOT_FOUND;
|
||||||
"pixel_aspect=%d/%d:colorspace=%d:range=%d",
|
}
|
||||||
dec_ctx->width,
|
|
||||||
dec_ctx->height,
|
|
||||||
dec_ctx->pix_fmt,
|
|
||||||
dec_ctx->time_base.num,
|
|
||||||
dec_ctx->time_base.den,
|
|
||||||
dec_ctx->framerate.num,
|
|
||||||
dec_ctx->framerate.den,
|
|
||||||
dec_ctx->sample_aspect_ratio.num,
|
|
||||||
dec_ctx->sample_aspect_ratio.den,
|
|
||||||
dec_ctx->colorspace,
|
|
||||||
dec_ctx->color_range
|
|
||||||
);
|
|
||||||
|
|
||||||
ret = avfilter_graph_create_filter(buffersrc_ctx, buffersrc, "in", args, NULL, graph);
|
// Start building the arguments string
|
||||||
|
std::string args = "video_size=" + std::to_string(dec_ctx->width) + "x" +
|
||||||
|
std::to_string(dec_ctx->height) +
|
||||||
|
":pix_fmt=" + std::to_string(dec_ctx->pix_fmt) +
|
||||||
|
":time_base=" + std::to_string(dec_ctx->time_base.num) + "/" +
|
||||||
|
std::to_string(dec_ctx->time_base.den) +
|
||||||
|
":frame_rate=" + std::to_string(dec_ctx->framerate.num) + "/" +
|
||||||
|
std::to_string(dec_ctx->framerate.den) +
|
||||||
|
":pixel_aspect=" + std::to_string(dec_ctx->sample_aspect_ratio.num) + "/" +
|
||||||
|
std::to_string(dec_ctx->sample_aspect_ratio.den);
|
||||||
|
|
||||||
|
// Make a copy of the AVClass on the stack
|
||||||
|
AVClass priv_class_copy = *buffersrc->priv_class;
|
||||||
|
AVClass *priv_class_copy_ptr = &priv_class_copy;
|
||||||
|
|
||||||
|
// Check if the colorspace option is supported
|
||||||
|
if (av_opt_find(&priv_class_copy_ptr, "colorspace", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
|
||||||
|
args += ":colorspace=" + std::to_string(dec_ctx->colorspace);
|
||||||
|
} else {
|
||||||
|
spdlog::warn("Option 'colorspace' is not supported by the buffer filter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the range option is supported
|
||||||
|
if (av_opt_find(&priv_class_copy_ptr, "range", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
|
||||||
|
args += ":range=" + std::to_string(dec_ctx->color_range);
|
||||||
|
} else {
|
||||||
|
spdlog::warn("Option 'range' is not supported by the buffer filter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
spdlog::debug("Buffer source args: {}", args);
|
||||||
|
ret = avfilter_graph_create_filter(buffersrc_ctx, buffersrc, "in", args.c_str(), NULL, graph);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
spdlog::error("Cannot create buffer source.");
|
spdlog::error("Cannot create buffer source.");
|
||||||
avfilter_graph_free(&graph);
|
avfilter_graph_free(&graph);
|
||||||
@ -72,26 +93,13 @@ int init_libplacebo(
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Prepare the filter arguments
|
// Prepare the filter arguments
|
||||||
char filter_args[4096];
|
std::string filter_args = "w=" + std::to_string(out_width) +
|
||||||
int filter_args_size = snprintf(
|
":h=" + std::to_string(out_height) + ":custom_shader_path='" +
|
||||||
filter_args,
|
shader_path_string + "'";
|
||||||
sizeof(filter_args),
|
|
||||||
"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;
|
AVFilterContext *libplacebo_ctx;
|
||||||
ret = avfilter_graph_create_filter(
|
ret = avfilter_graph_create_filter(
|
||||||
&libplacebo_ctx, libplacebo_filter, "libplacebo", filter_args, NULL, graph
|
&libplacebo_ctx, libplacebo_filter, "libplacebo", filter_args.c_str(), NULL, graph
|
||||||
);
|
);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
spdlog::error("Cannot create libplacebo filter.");
|
spdlog::error("Cannot create libplacebo filter.");
|
||||||
|
Loading…
Reference in New Issue
Block a user