1
mirror of https://git.videolan.org/git/ffmpeg.git synced 2024-07-19 18:54:13 +02:00
ffmpeg/libavfilter/vf_colorkey_opencl.c
Andreas Rheinhardt b4f5201967 avfilter: Replace query_formats callback with union of list and callback
If one looks at the many query_formats callbacks in existence,
one will immediately recognize that there is one type of default
callback for video and a slightly different default callback for
audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);"
for video with a filter-specific pix_fmts list. For audio, it is
the same with a filter-specific sample_fmts list together with
ff_set_common_all_samplerates() and ff_set_common_all_channel_counts().

This commit allows to remove the boilerplate query_formats callbacks
by replacing said callback with a union consisting the old callback
and pointers for pixel and sample format arrays. For the not uncommon
case in which these lists only contain a single entry (besides the
sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also
added to the union to store them directly in the AVFilter,
thereby avoiding a relocation.

The state of said union will be contained in a new, dedicated AVFilter
field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t
in order to create a hole for this new field; this is no problem, as
the maximum of all the nb_inputs is four; for nb_outputs it is only
two).

The state's default value coincides with the earlier default of
query_formats being unset, namely that the filter accepts all formats
(and also sample rates and channel counts/layouts for audio)
provided that these properties agree coincide for all inputs and
outputs.

By using different union members for audio and video filters
the type-unsafety of using the same functions for audio and video
lists will furthermore be more confined to formats.c than before.

When the new fields are used, they will also avoid allocations:
Currently something nearly equivalent to ff_default_query_formats()
is called after every successful call to a query_formats callback;
yet in the common case that the newly allocated AVFilterFormats
are not used at all (namely if there are no free links) these newly
allocated AVFilterFormats are freed again without ever being used.
Filters no longer using the callback will not exhibit this any more.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-10-05 17:48:25 +02:00

243 lines
7.9 KiB
C

/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "opencl.h"
#include "opencl_source.h"
#include "video.h"
typedef struct ColorkeyOpenCLContext {
OpenCLFilterContext ocf;
// Whether or not the above `OpenCLFilterContext` has been initialized
int initialized;
cl_command_queue command_queue;
cl_kernel kernel_colorkey;
// The color we are supposed to replace with transparency
uint8_t colorkey_rgba[4];
// Stored as a normalized float for passing to the OpenCL kernel
cl_float4 colorkey_rgba_float;
// Similarity percentage compared to `colorkey_rgba`, ranging from `0.01` to `1.0`
// where `0.01` matches only the key color and `1.0` matches all colors
float similarity;
// Blending percentage where `0.0` results in fully transparent pixels, `1.0` results
// in fully opaque pixels, and numbers in between result in transparency that varies
// based on the similarity to the key color
float blend;
} ColorkeyOpenCLContext;
static int colorkey_opencl_init(AVFilterContext *avctx)
{
ColorkeyOpenCLContext *ctx = avctx->priv;
cl_int cle;
int err;
err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_colorkey, 1);
if (err < 0)
goto fail;
ctx->command_queue = clCreateCommandQueue(
ctx->ocf.hwctx->context,
ctx->ocf.hwctx->device_id,
0,
&cle
);
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL command queue %d.\n", cle);
if (ctx->blend > 0.0001) {
ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey_blend", &cle);
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey_blend kernel: %d.\n", cle);
} else {
ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey", &cle);
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey kernel: %d.\n", cle);
}
for (int i = 0; i < 4; ++i) {
ctx->colorkey_rgba_float.s[i] = (float)ctx->colorkey_rgba[i] / 255.0;
}
ctx->initialized = 1;
return 0;
fail:
if (ctx->command_queue)
clReleaseCommandQueue(ctx->command_queue);
if (ctx->kernel_colorkey)
clReleaseKernel(ctx->kernel_colorkey);
return err;
}
static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
{
AVFilterContext *avctx = link->dst;
AVFilterLink *outlink = avctx->outputs[0];
ColorkeyOpenCLContext *colorkey_ctx = avctx->priv;
AVFrame *output_frame = NULL;
int err;
cl_int cle;
size_t global_work[2];
cl_mem src, dst;
if (!input_frame->hw_frames_ctx)
return AVERROR(EINVAL);
if (!colorkey_ctx->initialized) {
AVHWFramesContext *input_frames_ctx =
(AVHWFramesContext*)input_frame->hw_frames_ctx->data;
int fmt = input_frames_ctx->sw_format;
// Make sure the input is a format we support
if (fmt != AV_PIX_FMT_ARGB &&
fmt != AV_PIX_FMT_RGBA &&
fmt != AV_PIX_FMT_ABGR &&
fmt != AV_PIX_FMT_BGRA
) {
av_log(avctx, AV_LOG_ERROR, "unsupported (non-RGB) format in colorkey_opencl.\n");
err = AVERROR(ENOSYS);
goto fail;
}
err = colorkey_opencl_init(avctx);
if (err < 0)
goto fail;
}
// This filter only operates on RGB data and we know that will be on the first plane
src = (cl_mem)input_frame->data[0];
output_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!output_frame) {
err = AVERROR(ENOMEM);
goto fail;
}
dst = (cl_mem)output_frame->data[0];
CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 0, cl_mem, &src);
CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 1, cl_mem, &dst);
CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 2, cl_float4, &colorkey_ctx->colorkey_rgba_float);
CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 3, float, &colorkey_ctx->similarity);
if (colorkey_ctx->blend > 0.0001) {
CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 4, float, &colorkey_ctx->blend);
}
err = ff_opencl_filter_work_size_from_image(avctx, global_work, input_frame, 0, 0);
if (err < 0)
goto fail;
cle = clEnqueueNDRangeKernel(
colorkey_ctx->command_queue,
colorkey_ctx->kernel_colorkey,
2,
NULL,
global_work,
NULL,
0,
NULL,
NULL
);
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue colorkey kernel: %d.\n", cle);
// Run queued kernel
cle = clFinish(colorkey_ctx->command_queue);
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
err = av_frame_copy_props(output_frame, input_frame);
if (err < 0)
goto fail;
av_frame_free(&input_frame);
return ff_filter_frame(outlink, output_frame);
fail:
clFinish(colorkey_ctx->command_queue);
av_frame_free(&input_frame);
av_frame_free(&output_frame);
return err;
}
static av_cold void colorkey_opencl_uninit(AVFilterContext *avctx)
{
ColorkeyOpenCLContext *ctx = avctx->priv;
cl_int cle;
if (ctx->kernel_colorkey) {
cle = clReleaseKernel(ctx->kernel_colorkey);
if (cle != CL_SUCCESS)
av_log(avctx, AV_LOG_ERROR, "Failed to release "
"kernel: %d.\n", cle);
}
if (ctx->command_queue) {
cle = clReleaseCommandQueue(ctx->command_queue);
if (cle != CL_SUCCESS)
av_log(avctx, AV_LOG_ERROR, "Failed to release "
"command queue: %d.\n", cle);
}
ff_opencl_filter_uninit(avctx);
}
static const AVFilterPad colorkey_opencl_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
.config_props = &ff_opencl_filter_config_input,
},
};
static const AVFilterPad colorkey_opencl_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = &ff_opencl_filter_config_output,
},
};
#define OFFSET(x) offsetof(ColorkeyOpenCLContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption colorkey_opencl_options[] = {
{ "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
{ "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
{ "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
{ NULL }
};
AVFILTER_DEFINE_CLASS(colorkey_opencl);
const AVFilter ff_vf_colorkey_opencl = {
.name = "colorkey_opencl",
.description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
.priv_size = sizeof(ColorkeyOpenCLContext),
.priv_class = &colorkey_opencl_class,
.init = &ff_opencl_filter_init,
.uninit = &colorkey_opencl_uninit,
FILTER_INPUTS(colorkey_opencl_inputs),
FILTER_OUTPUTS(colorkey_opencl_outputs),
FILTER_QUERY_FUNC(&ff_opencl_filter_query_formats),
.flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE
};