2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/log.h"
20 #include "libavutil/mem.h"
21 #include "libavutil/opt.h"
22 #include "libavutil/pixdesc.h"
28 #include "opencl_source.h"
31 enum XFadeTransitions {
45 typedef struct XFadeOpenCLContext {
46 OpenCLFilterContext ocf;
49 const char *source_file;
50 const char *kernel_name;
56 cl_command_queue command_queue;
71 static int xfade_opencl_load(AVFilterContext *avctx,
72 enum AVPixelFormat main_format,
73 enum AVPixelFormat xfade_format)
75 XFadeOpenCLContext *ctx = avctx->priv;
77 const AVPixFmtDescriptor *main_desc;
79 const char *kernel_name;
81 main_desc = av_pix_fmt_desc_get(main_format);
82 if (main_format != xfade_format) {
83 av_log(avctx, AV_LOG_ERROR, "Input formats are not same.\n");
84 return AVERROR(EINVAL);
88 for (int i = 0; i < main_desc->nb_components; i++)
89 main_planes = FFMAX(main_planes,
90 main_desc->comp[i].plane + 1);
92 ctx->nb_planes = main_planes;
94 if (ctx->transition == CUSTOM) {
95 err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
97 err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_xfade, 1);
102 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
103 ctx->ocf.hwctx->device_id,
105 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
106 "command queue %d.\n", cle);
108 switch (ctx->transition) {
109 case CUSTOM: kernel_name = ctx->kernel_name; break;
110 case FADE: kernel_name = "fade"; break;
111 case WIPELEFT: kernel_name = "wipeleft"; break;
112 case WIPERIGHT: kernel_name = "wiperight"; break;
113 case WIPEUP: kernel_name = "wipeup"; break;
114 case WIPEDOWN: kernel_name = "wipedown"; break;
115 case SLIDELEFT: kernel_name = "slideleft"; break;
116 case SLIDERIGHT: kernel_name = "slideright"; break;
117 case SLIDEUP: kernel_name = "slideup"; break;
118 case SLIDEDOWN: kernel_name = "slidedown"; break;
124 ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
125 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
127 ctx->initialised = 1;
132 if (ctx->command_queue)
133 clReleaseCommandQueue(ctx->command_queue);
135 clReleaseKernel(ctx->kernel);
139 static int xfade_frame(AVFilterContext *avctx, AVFrame *a, AVFrame *b)
141 AVFilterLink *outlink = avctx->outputs[0];
142 XFadeOpenCLContext *ctx = avctx->priv;
145 cl_float progress = av_clipf(1.f - ((cl_float)(ctx->pts - ctx->first_pts - ctx->offset_pts) / ctx->duration_pts), 0.f, 1.f);
146 size_t global_work[2];
150 if (!ctx->initialised) {
151 AVHWFramesContext *main_fc =
152 (AVHWFramesContext*)a->hw_frames_ctx->data;
153 AVHWFramesContext *xfade_fc =
154 (AVHWFramesContext*)b->hw_frames_ctx->data;
156 err = xfade_opencl_load(avctx, main_fc->sw_format,
157 xfade_fc->sw_format);
162 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
164 err = AVERROR(ENOMEM);
168 for (plane = 0; plane < ctx->nb_planes; plane++) {
172 mem = (cl_mem)output->data[plane];
173 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
176 mem = (cl_mem)ctx->xf[0]->data[plane];
177 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
180 mem = (cl_mem)ctx->xf[1]->data[plane];
181 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
184 CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_float, &progress);
187 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
192 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
193 global_work, NULL, 0, NULL, NULL);
194 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue xfade kernel "
195 "for plane %d: %d.\n", plane, cle);
198 cle = clFinish(ctx->command_queue);
199 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
201 err = av_frame_copy_props(output, ctx->xf[0]);
205 output->pts = ctx->pts;
207 return ff_filter_frame(outlink, output);
210 av_frame_free(&output);
214 static int xfade_opencl_config_output(AVFilterLink *outlink)
216 AVFilterContext *avctx = outlink->src;
217 XFadeOpenCLContext *ctx = avctx->priv;
218 AVFilterLink *inlink0 = avctx->inputs[0];
219 AVFilterLink *inlink1 = avctx->inputs[1];
222 err = ff_opencl_filter_config_output(outlink);
226 if (inlink0->w != inlink1->w || inlink0->h != inlink1->h) {
227 av_log(avctx, AV_LOG_ERROR, "First input link %s parameters "
228 "(size %dx%d) do not match the corresponding "
229 "second input link %s parameters (size %dx%d)\n",
230 avctx->input_pads[0].name, inlink0->w, inlink0->h,
231 avctx->input_pads[1].name, inlink1->w, inlink1->h);
232 return AVERROR(EINVAL);
235 if (inlink0->time_base.num != inlink1->time_base.num ||
236 inlink0->time_base.den != inlink1->time_base.den) {
237 av_log(avctx, AV_LOG_ERROR, "First input link %s timebase "
238 "(%d/%d) do not match the corresponding "
239 "second input link %s timebase (%d/%d)\n",
240 avctx->input_pads[0].name, inlink0->time_base.num, inlink0->time_base.den,
241 avctx->input_pads[1].name, inlink1->time_base.num, inlink1->time_base.den);
242 return AVERROR(EINVAL);
245 ctx->first_pts = ctx->last_pts = ctx->pts = AV_NOPTS_VALUE;
247 outlink->time_base = inlink0->time_base;
248 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
249 outlink->frame_rate = inlink0->frame_rate;
252 ctx->duration_pts = av_rescale_q(ctx->duration, AV_TIME_BASE_Q, outlink->time_base);
254 ctx->offset_pts = av_rescale_q(ctx->offset, AV_TIME_BASE_Q, outlink->time_base);
259 static int xfade_opencl_activate(AVFilterContext *avctx)
261 XFadeOpenCLContext *ctx = avctx->priv;
262 AVFilterLink *outlink = avctx->outputs[0];
267 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, avctx);
269 if (ctx->xfade_is_over) {
270 ret = ff_inlink_consume_frame(avctx->inputs[1], &in);
273 } else if (ret > 0) {
274 in->pts = (in->pts - ctx->last_pts) + ctx->pts;
275 return ff_filter_frame(outlink, in);
276 } else if (ff_inlink_acknowledge_status(avctx->inputs[1], &status, &pts)) {
277 ff_outlink_set_status(outlink, status, ctx->pts);
280 if (ff_outlink_frame_wanted(outlink)) {
281 ff_inlink_request_frame(avctx->inputs[1]);
287 if (ff_inlink_queued_frames(avctx->inputs[0]) > 0) {
288 ctx->xf[0] = ff_inlink_peek_frame(avctx->inputs[0], 0);
290 if (ctx->first_pts == AV_NOPTS_VALUE) {
291 ctx->first_pts = ctx->xf[0]->pts;
293 ctx->pts = ctx->xf[0]->pts;
294 if (ctx->first_pts + ctx->offset_pts > ctx->xf[0]->pts) {
296 ctx->need_second = 0;
297 ff_inlink_consume_frame(avctx->inputs[0], &in);
298 return ff_filter_frame(outlink, in);
301 ctx->need_second = 1;
305 if (ctx->xf[0] && ff_inlink_queued_frames(avctx->inputs[1]) > 0) {
306 ff_inlink_consume_frame(avctx->inputs[0], &ctx->xf[0]);
307 ff_inlink_consume_frame(avctx->inputs[1], &ctx->xf[1]);
309 ctx->last_pts = ctx->xf[1]->pts;
310 ctx->pts = ctx->xf[0]->pts;
311 if (ctx->xf[0]->pts - (ctx->first_pts + ctx->offset_pts) > ctx->duration_pts)
312 ctx->xfade_is_over = 1;
313 ret = xfade_frame(avctx, ctx->xf[0], ctx->xf[1]);
314 av_frame_free(&ctx->xf[0]);
315 av_frame_free(&ctx->xf[1]);
319 if (ff_inlink_queued_frames(avctx->inputs[0]) > 0 &&
320 ff_inlink_queued_frames(avctx->inputs[1]) > 0) {
321 ff_filter_set_ready(avctx, 100);
325 if (ff_outlink_frame_wanted(outlink)) {
326 if (!ctx->eof[0] && ff_outlink_get_status(avctx->inputs[0])) {
328 ctx->xfade_is_over = 1;
330 if (!ctx->eof[1] && ff_outlink_get_status(avctx->inputs[1])) {
333 if (!ctx->eof[0] && !ctx->xf[0])
334 ff_inlink_request_frame(avctx->inputs[0]);
335 if (!ctx->eof[1] && (ctx->need_second || ctx->eof[0]))
336 ff_inlink_request_frame(avctx->inputs[1]);
337 if (ctx->eof[0] && ctx->eof[1] && (
338 ff_inlink_queued_frames(avctx->inputs[0]) <= 0 ||
339 ff_inlink_queued_frames(avctx->inputs[1]) <= 0))
340 ff_outlink_set_status(outlink, AVERROR_EOF, AV_NOPTS_VALUE);
344 return FFERROR_NOT_READY;
347 static av_cold void xfade_opencl_uninit(AVFilterContext *avctx)
349 XFadeOpenCLContext *ctx = avctx->priv;
353 cle = clReleaseKernel(ctx->kernel);
354 if (cle != CL_SUCCESS)
355 av_log(avctx, AV_LOG_ERROR, "Failed to release "
356 "kernel: %d.\n", cle);
359 if (ctx->command_queue) {
360 cle = clReleaseCommandQueue(ctx->command_queue);
361 if (cle != CL_SUCCESS)
362 av_log(avctx, AV_LOG_ERROR, "Failed to release "
363 "command queue: %d.\n", cle);
366 ff_opencl_filter_uninit(avctx);
369 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
371 XFadeOpenCLContext *s = inlink->dst->priv;
373 return s->xfade_is_over || !s->need_second ?
374 ff_null_get_video_buffer (inlink, w, h) :
375 ff_default_get_video_buffer(inlink, w, h);
378 #define OFFSET(x) offsetof(XFadeOpenCLContext, x)
379 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
381 static const AVOption xfade_opencl_options[] = {
382 { "transition", "set cross fade transition", OFFSET(transition), AV_OPT_TYPE_INT, {.i64=1}, 0, NB_TRANSITIONS-1, FLAGS, "transition" },
383 { "custom", "custom transition", 0, AV_OPT_TYPE_CONST, {.i64=CUSTOM}, 0, 0, FLAGS, "transition" },
384 { "fade", "fade transition", 0, AV_OPT_TYPE_CONST, {.i64=FADE}, 0, 0, FLAGS, "transition" },
385 { "wipeleft", "wipe left transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPELEFT}, 0, 0, FLAGS, "transition" },
386 { "wiperight", "wipe right transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPERIGHT}, 0, 0, FLAGS, "transition" },
387 { "wipeup", "wipe up transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEUP}, 0, 0, FLAGS, "transition" },
388 { "wipedown", "wipe down transition", 0, AV_OPT_TYPE_CONST, {.i64=WIPEDOWN}, 0, 0, FLAGS, "transition" },
389 { "slideleft", "slide left transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDELEFT}, 0, 0, FLAGS, "transition" },
390 { "slideright", "slide right transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDERIGHT}, 0, 0, FLAGS, "transition" },
391 { "slideup", "slide up transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEUP}, 0, 0, FLAGS, "transition" },
392 { "slidedown", "slide down transition", 0, AV_OPT_TYPE_CONST, {.i64=SLIDEDOWN}, 0, 0, FLAGS, "transition" },
393 { "source", "set OpenCL program source file for custom transition", OFFSET(source_file), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
394 { "kernel", "set kernel name in program file for custom transition", OFFSET(kernel_name), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
395 { "duration", "set cross fade duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=1000000}, 0, 60000000, FLAGS },
396 { "offset", "set cross fade start relative to first input stream", OFFSET(offset), AV_OPT_TYPE_DURATION, {.i64=0}, INT64_MIN, INT64_MAX, FLAGS },
400 AVFILTER_DEFINE_CLASS(xfade_opencl);
402 static const AVFilterPad xfade_opencl_inputs[] = {
405 .type = AVMEDIA_TYPE_VIDEO,
406 .get_video_buffer = get_video_buffer,
407 .config_props = &ff_opencl_filter_config_input,
411 .type = AVMEDIA_TYPE_VIDEO,
412 .get_video_buffer = get_video_buffer,
413 .config_props = &ff_opencl_filter_config_input,
418 static const AVFilterPad xfade_opencl_outputs[] = {
421 .type = AVMEDIA_TYPE_VIDEO,
422 .config_props = &xfade_opencl_config_output,
427 const AVFilter ff_vf_xfade_opencl = {
428 .name = "xfade_opencl",
429 .description = NULL_IF_CONFIG_SMALL("Cross fade one video with another video."),
430 .priv_size = sizeof(XFadeOpenCLContext),
431 .priv_class = &xfade_opencl_class,
432 .init = &ff_opencl_filter_init,
433 .uninit = &xfade_opencl_uninit,
434 .query_formats = &ff_opencl_filter_query_formats,
435 .activate = &xfade_opencl_activate,
436 .inputs = xfade_opencl_inputs,
437 .outputs = xfade_opencl_outputs,
438 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,