FFmpeg
Loading...
Searching...
No Matches
vf_remap_opencl.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22#include "libavutil/pixdesc.h"
23#include "libavutil/opt.h"
24#include "avfilter.h"
25#include "drawutils.h"
26#include "filters.h"
27#include "framesync.h"
28#include "opencl.h"
29#include "opencl_source.h"
30#include "video.h"
31
32typedef struct RemapOpenCLContext {
34
36 int interp;
37 uint8_t fill_rgba[4];
38 cl_float4 cl_fill_color;
39
41 cl_kernel kernel;
42 cl_command_queue command_queue;
43
46
47#define OFFSET(x) offsetof(RemapOpenCLContext, x)
48#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49
51 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, .unit = "interp" },
52 { "near", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "interp" },
53 { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "interp" },
54 { "fill", "set the color of the unmapped pixels", OFFSET(fill_rgba), AV_OPT_TYPE_COLOR, {.str="black"}, .flags = FLAGS },
55 { NULL }
56};
57
59
61{
62 return ff_opencl_filter_init(avctx);
63}
64
65static const char *kernels[] = { "remap_near", "remap_linear" };
66
68 enum AVPixelFormat main_format,
69 enum AVPixelFormat xmap_format,
70 enum AVPixelFormat ymap_format)
71{
72 RemapOpenCLContext *ctx = avctx->priv;
73 cl_int cle;
74 const char *source = ff_source_remap_cl;
75 const char *kernel = kernels[ctx->interp];
76 const AVPixFmtDescriptor *main_desc;
77 int err, main_planes;
78 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(main_format);
79 int is_rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
80 const float scale = 1.f / 255.f;
81 uint8_t rgba_map[4];
82
83 ff_fill_rgba_map(rgba_map, main_format);
84
85 if (is_rgb) {
86 ctx->cl_fill_color.s[rgba_map[0]] = ctx->fill_rgba[0] * scale;
87 ctx->cl_fill_color.s[rgba_map[1]] = ctx->fill_rgba[1] * scale;
88 ctx->cl_fill_color.s[rgba_map[2]] = ctx->fill_rgba[2] * scale;
89 ctx->cl_fill_color.s[rgba_map[3]] = ctx->fill_rgba[3] * scale;
90 } else {
91 ctx->cl_fill_color.s[0] = RGB_TO_Y_BT709(ctx->fill_rgba[0], ctx->fill_rgba[1], ctx->fill_rgba[2]) * scale;
92 ctx->cl_fill_color.s[1] = RGB_TO_U_BT709(ctx->fill_rgba[0], ctx->fill_rgba[1], ctx->fill_rgba[2], 0) * scale;
93 ctx->cl_fill_color.s[2] = RGB_TO_V_BT709(ctx->fill_rgba[0], ctx->fill_rgba[1], ctx->fill_rgba[2], 0) * scale;
94 ctx->cl_fill_color.s[3] = ctx->fill_rgba[3] * scale;
95 }
96
97 main_desc = av_pix_fmt_desc_get(main_format);
98
99 main_planes = 0;
100 for (int i = 0; i < main_desc->nb_components; i++)
101 main_planes = FFMAX(main_planes,
102 main_desc->comp[i].plane + 1);
103
104 ctx->nb_planes = main_planes;
105
106 err = ff_opencl_filter_load_program(avctx, &source, 1);
107 if (err < 0)
108 goto fail;
109
110 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
111 ctx->ocf.hwctx->device_id,
112 0, &cle);
113 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
114 "command queue %d.\n", cle);
115
116 ctx->kernel = clCreateKernel(ctx->ocf.program, kernel, &cle);
117 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
118
119 ctx->initialised = 1;
120 return 0;
121
122fail:
123 if (ctx->command_queue)
124 clReleaseCommandQueue(ctx->command_queue);
125 if (ctx->kernel)
126 clReleaseKernel(ctx->kernel);
127 return err;
128}
129
131{
132 AVFilterContext *avctx = fs->parent;
133 AVFilterLink *outlink = avctx->outputs[0];
134 RemapOpenCLContext *ctx = avctx->priv;
135 AVFrame *input_main, *input_xmap, *input_ymap;
136 AVFrame *output;
137 cl_mem mem;
138 cl_int cle;
139 size_t global_work[2];
140 int kernel_arg = 0;
141 int err, plane;
142
143 err = ff_framesync_get_frame(fs, 0, &input_main, 0);
144 if (err < 0)
145 return err;
146 err = ff_framesync_get_frame(fs, 1, &input_xmap, 0);
147 if (err < 0)
148 return err;
149 err = ff_framesync_get_frame(fs, 2, &input_ymap, 0);
150 if (err < 0)
151 return err;
152
153 if (!ctx->initialised) {
154 AVHWFramesContext *main_fc =
155 (AVHWFramesContext*)input_main->hw_frames_ctx->data;
156 AVHWFramesContext *xmap_fc =
157 (AVHWFramesContext*)input_xmap->hw_frames_ctx->data;
158 AVHWFramesContext *ymap_fc =
159 (AVHWFramesContext*)input_ymap->hw_frames_ctx->data;
160
161 err = remap_opencl_load(avctx, main_fc->sw_format,
162 xmap_fc->sw_format,
163 ymap_fc->sw_format);
164 if (err < 0)
165 return err;
166 }
167
168 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
169 if (!output) {
170 err = AVERROR(ENOMEM);
171 goto fail;
172 }
173
174 for (plane = 0; plane < ctx->nb_planes; plane++) {
175 cl_float4 cl_fill_color;
176 kernel_arg = 0;
177
178 if (ctx->nb_planes == 1)
179 cl_fill_color = ctx->cl_fill_color;
180 else
181 cl_fill_color.s[0] = ctx->cl_fill_color.s[plane];
182
183 mem = (cl_mem)output->data[plane];
184 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
185 kernel_arg++;
186
187 mem = (cl_mem)input_main->data[plane];
188 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
189 kernel_arg++;
190
191 mem = (cl_mem)input_xmap->data[0];
192 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
193 kernel_arg++;
194
195 mem = (cl_mem)input_ymap->data[0];
196 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
197 kernel_arg++;
198
199 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_float4, &cl_fill_color);
200 kernel_arg++;
201
202 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
203 output, plane, 0);
204 if (err < 0)
205 goto fail;
206
207 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
208 global_work, NULL, 0, NULL, NULL);
209 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue remap kernel "
210 "for plane %d: %d.\n", plane, cle);
211 }
212
213 cle = clFinish(ctx->command_queue);
214 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
215
216 err = av_frame_copy_props(output, input_main);
217
218 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
220 output->width, output->height, output->pts);
221
222 return ff_filter_frame(outlink, output);
223
224fail:
225 av_frame_free(&output);
226 return err;
227}
228
229static int config_output(AVFilterLink *outlink)
230{
231 AVFilterContext *ctx = outlink->src;
232 RemapOpenCLContext *s = ctx->priv;
233 AVFilterLink *srclink = ctx->inputs[0];
234 AVFilterLink *xlink = ctx->inputs[1];
235 AVFilterLink *ylink = ctx->inputs[2];
236 FilterLink *il = ff_filter_link(srclink);
237 FilterLink *ol = ff_filter_link(outlink);
238 FFFrameSyncIn *in;
239 int ret;
240
241 if (xlink->w != ylink->w || xlink->h != ylink->h) {
242 av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
243 "(size %dx%d) do not match the corresponding "
244 "third input link %s parameters (%dx%d)\n",
245 ctx->input_pads[1].name, xlink->w, xlink->h,
246 ctx->input_pads[2].name, ylink->w, ylink->h);
247 return AVERROR(EINVAL);
248 }
249
250 outlink->w = xlink->w;
251 outlink->h = xlink->h;
252 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
253 ol->frame_rate = il->frame_rate;
254
255 ret = ff_framesync_init(&s->fs, ctx, 3);
256 if (ret < 0)
257 return ret;
258
259 in = s->fs.in;
260 in[0].time_base = srclink->time_base;
261 in[1].time_base = xlink->time_base;
262 in[2].time_base = ylink->time_base;
263 in[0].sync = 2;
264 in[0].before = EXT_STOP;
265 in[0].after = EXT_STOP;
266 in[1].sync = 1;
267 in[1].before = EXT_NULL;
268 in[1].after = EXT_INFINITY;
269 in[2].sync = 1;
270 in[2].before = EXT_NULL;
271 in[2].after = EXT_INFINITY;
272 s->fs.opaque = s;
273 s->fs.on_event = remap_opencl_process_frame;
274
275 ret = ff_framesync_configure(&s->fs);
276 outlink->time_base = s->fs.time_base;
277 if (ret < 0)
278 return ret;
279
280 s->ocf.output_width = outlink->w;
281 s->ocf.output_height = outlink->h;
282
283 return ff_opencl_filter_config_output(outlink);
284}
285
287{
288 RemapOpenCLContext *s = ctx->priv;
289 return ff_framesync_activate(&s->fs);
290}
291
293{
294 RemapOpenCLContext *ctx = avctx->priv;
295 cl_int cle;
296
297 if (ctx->kernel) {
298 cle = clReleaseKernel(ctx->kernel);
299 if (cle != CL_SUCCESS)
300 av_log(avctx, AV_LOG_ERROR, "Failed to release "
301 "kernel: %d.\n", cle);
302 }
303
304 if (ctx->command_queue) {
305 cle = clReleaseCommandQueue(ctx->command_queue);
306 if (cle != CL_SUCCESS)
307 av_log(avctx, AV_LOG_ERROR, "Failed to release "
308 "command queue: %d.\n", cle);
309 }
310
312
314}
315
317 {
318 .name = "source",
319 .type = AVMEDIA_TYPE_VIDEO,
320 .config_props = &ff_opencl_filter_config_input,
321 },
322 {
323 .name = "xmap",
324 .type = AVMEDIA_TYPE_VIDEO,
325 .config_props = &ff_opencl_filter_config_input,
326 },
327 {
328 .name = "ymap",
329 .type = AVMEDIA_TYPE_VIDEO,
330 .config_props = &ff_opencl_filter_config_input,
331 },
332};
333
335 {
336 .name = "default",
337 .type = AVMEDIA_TYPE_VIDEO,
338 .config_props = config_output,
339 },
340};
341
343 .p.name = "remap_opencl",
344 .p.description = NULL_IF_CONFIG_SMALL("Remap pixels using OpenCL."),
345 .p.priv_class = &remap_opencl_class,
346 .p.flags = AVFILTER_FLAG_HWDEVICE,
347 .priv_size = sizeof(RemapOpenCLContext),
354 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
355};
const FFFilter ff_vf_remap_opencl
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition drawutils.c:80
misc drawing utilities
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition framesync.c:269
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition framesync.c:86
@ EXT_INFINITY
Extend the frame to infinity.
Definition framesync.h:75
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
Various defines for YUV<->RGB conversion.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
#define FFMAX(a, b)
Definition macros.h:47
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition opencl.c:144
int ff_opencl_filter_load_program(AVFilterContext *avctx, const char **program_source_array, int nb_strings)
Load a new OpenCL program from strings in memory.
Definition opencl.c:159
int ff_opencl_filter_config_input(AVFilterLink *inlink)
Check that the input link contains a suitable hardware frames context and extract the device from it.
Definition opencl.c:46
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition opencl.c:135
int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
Find the work size needed needed for a given plane of an image.
Definition opencl.c:266
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition opencl.c:83
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition opencl.h:61
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition opencl.h:74
const char * ff_source_remap_cl
AVOptions.
@ EXT_STOP
Completely stop all streams with this one.
Definition packetsync.h:62
@ EXT_NULL
Ignore this stream and continue processing the other ones.
Definition packetsync.h:67
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition pixfmt.h:358
uint8_t * data
The data buffer.
Definition buffer.h:90
int plane
Which of the 4 planes contains the component.
Definition pixdesc.h:34
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:769
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
Input stream structure.
Definition framesync.h:102
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition framesync.h:112
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition framesync.h:107
AVRational time_base
Time base for the incoming frames.
Definition framesync.h:117
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition framesync.h:160
Frame sync structure.
Definition framesync.h:168
OpenCLFilterContext ocf
cl_command_queue command_queue
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
interp
Definition vf_curves.c:62
#define RGB_TO_Y_BT709(r, g, b)
#define RGB_TO_U_BT709(r1, g1, b1, max)
#define RGB_TO_V_BT709(r1, g1, b1, max)
static av_cold int remap_opencl_init(AVFilterContext *avctx)
static int remap_opencl_process_frame(FFFrameSync *fs)
static int remap_opencl_load(AVFilterContext *avctx, enum AVPixelFormat main_format, enum AVPixelFormat xmap_format, enum AVPixelFormat ymap_format)
static const AVOption remap_opencl_options[]
static const AVFilterPad remap_opencl_inputs[]
static const AVFilterPad remap_opencl_outputs[]
static const char * kernels[]
static av_cold void remap_opencl_uninit(AVFilterContext *avctx)
static int activate(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89