]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_xfade_opencl.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / vf_xfade_opencl.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "libavutil/log.h"
20 #include "libavutil/mem.h"
21 #include "libavutil/opt.h"
22 #include "libavutil/pixdesc.h"
23
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "internal.h"
27 #include "opencl.h"
28 #include "opencl_source.h"
29 #include "video.h"
30
31 enum XFadeTransitions {
32     CUSTOM,
33     FADE,
34     WIPELEFT,
35     WIPERIGHT,
36     WIPEUP,
37     WIPEDOWN,
38     SLIDELEFT,
39     SLIDERIGHT,
40     SLIDEUP,
41     SLIDEDOWN,
42     NB_TRANSITIONS,
43 };
44
45 typedef struct XFadeOpenCLContext {
46     OpenCLFilterContext ocf;
47
48     int              transition;
49     const char      *source_file;
50     const char      *kernel_name;
51     int64_t          duration;
52     int64_t          offset;
53
54     int              initialised;
55     cl_kernel        kernel;
56     cl_command_queue command_queue;
57
58     int              nb_planes;
59
60     int64_t          duration_pts;
61     int64_t          offset_pts;
62     int64_t          first_pts;
63     int64_t          last_pts;
64     int64_t          pts;
65     int              xfade_is_over;
66     int              need_second;
67     int              eof[2];
68     AVFrame         *xf[2];
69 } XFadeOpenCLContext;
70
71 static int xfade_opencl_load(AVFilterContext *avctx,
72                              enum AVPixelFormat main_format,
73                              enum AVPixelFormat xfade_format)
74 {
75     XFadeOpenCLContext *ctx = avctx->priv;
76     cl_int cle;
77     const AVPixFmtDescriptor *main_desc;
78     int err, main_planes;
79     const char *kernel_name;
80
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);
85     }
86
87     main_planes = 0;
88     for (int i = 0; i < main_desc->nb_components; i++)
89         main_planes = FFMAX(main_planes,
90                             main_desc->comp[i].plane + 1);
91
92     ctx->nb_planes = main_planes;
93
94     if (ctx->transition == CUSTOM) {
95         err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
96     } else {
97         err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_xfade, 1);
98     }
99     if (err < 0)
100         return err;
101
102     ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
103                                               ctx->ocf.hwctx->device_id,
104                                               0, &cle);
105     CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
106                      "command queue %d.\n", cle);
107
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;
119     default:
120         err = AVERROR_BUG;
121         goto fail;
122     }
123
124     ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
125     CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
126
127     ctx->initialised = 1;
128
129     return 0;
130
131 fail:
132     if (ctx->command_queue)
133         clReleaseCommandQueue(ctx->command_queue);
134     if (ctx->kernel)
135         clReleaseKernel(ctx->kernel);
136     return err;
137 }
138
139 static int xfade_frame(AVFilterContext *avctx, AVFrame *a, AVFrame *b)
140 {
141     AVFilterLink *outlink = avctx->outputs[0];
142     XFadeOpenCLContext *ctx = avctx->priv;
143     AVFrame *output;
144     cl_int cle;
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];
147     int kernel_arg = 0;
148     int err, plane;
149
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;
155
156         err = xfade_opencl_load(avctx, main_fc->sw_format,
157                                 xfade_fc->sw_format);
158         if (err < 0)
159             return err;
160     }
161
162     output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
163     if (!output) {
164         err = AVERROR(ENOMEM);
165         goto fail;
166     }
167
168     for (plane = 0; plane < ctx->nb_planes; plane++) {
169         cl_mem mem;
170         kernel_arg = 0;
171
172         mem = (cl_mem)output->data[plane];
173         CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
174         kernel_arg++;
175
176         mem = (cl_mem)ctx->xf[0]->data[plane];
177         CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
178         kernel_arg++;
179
180         mem = (cl_mem)ctx->xf[1]->data[plane];
181         CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_mem, &mem);
182         kernel_arg++;
183
184         CL_SET_KERNEL_ARG(ctx->kernel, kernel_arg, cl_float, &progress);
185         kernel_arg++;
186
187         err = ff_opencl_filter_work_size_from_image(avctx, global_work,
188                                                     output, plane, 0);
189         if (err < 0)
190             goto fail;
191
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);
196     }
197
198     cle = clFinish(ctx->command_queue);
199     CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
200
201     err = av_frame_copy_props(output, ctx->xf[0]);
202     if (err < 0)
203         goto fail;
204
205     output->pts = ctx->pts;
206
207     return ff_filter_frame(outlink, output);
208
209 fail:
210     av_frame_free(&output);
211     return err;
212 }
213
214 static int xfade_opencl_config_output(AVFilterLink *outlink)
215 {
216     AVFilterContext *avctx = outlink->src;
217     XFadeOpenCLContext *ctx = avctx->priv;
218     AVFilterLink *inlink0 = avctx->inputs[0];
219     AVFilterLink *inlink1 = avctx->inputs[1];
220     int err;
221
222     err = ff_opencl_filter_config_output(outlink);
223     if (err < 0)
224         return err;
225
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);
233     }
234
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);
243     }
244
245     ctx->first_pts = ctx->last_pts = ctx->pts = AV_NOPTS_VALUE;
246
247     outlink->time_base = inlink0->time_base;
248     outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
249     outlink->frame_rate = inlink0->frame_rate;
250
251     if (ctx->duration)
252         ctx->duration_pts = av_rescale_q(ctx->duration, AV_TIME_BASE_Q, outlink->time_base);
253     if (ctx->offset)
254         ctx->offset_pts = av_rescale_q(ctx->offset, AV_TIME_BASE_Q, outlink->time_base);
255
256     return 0;
257 }
258
259 static int xfade_opencl_activate(AVFilterContext *avctx)
260 {
261     XFadeOpenCLContext *ctx = avctx->priv;
262     AVFilterLink *outlink = avctx->outputs[0];
263     AVFrame *in = NULL;
264     int ret = 0, status;
265     int64_t pts;
266
267     FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, avctx);
268
269     if (ctx->xfade_is_over) {
270         ret = ff_inlink_consume_frame(avctx->inputs[1], &in);
271         if (ret < 0) {
272             return ret;
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);
278             return 0;
279         } else if (!ret) {
280             if (ff_outlink_frame_wanted(outlink)) {
281                 ff_inlink_request_frame(avctx->inputs[1]);
282                 return 0;
283             }
284         }
285     }
286
287     if (ff_inlink_queued_frames(avctx->inputs[0]) > 0) {
288         ctx->xf[0] = ff_inlink_peek_frame(avctx->inputs[0], 0);
289         if (ctx->xf[0]) {
290             if (ctx->first_pts == AV_NOPTS_VALUE) {
291                 ctx->first_pts = ctx->xf[0]->pts;
292             }
293             ctx->pts = ctx->xf[0]->pts;
294             if (ctx->first_pts + ctx->offset_pts > ctx->xf[0]->pts) {
295                 ctx->xf[0] = NULL;
296                 ctx->need_second = 0;
297                 ff_inlink_consume_frame(avctx->inputs[0], &in);
298                 return ff_filter_frame(outlink, in);
299             }
300
301             ctx->need_second = 1;
302         }
303     }
304
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]);
308
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]);
316         return ret;
317     }
318
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);
322         return 0;
323     }
324
325     if (ff_outlink_frame_wanted(outlink)) {
326         if (!ctx->eof[0] && ff_outlink_get_status(avctx->inputs[0])) {
327             ctx->eof[0] = 1;
328             ctx->xfade_is_over = 1;
329         }
330         if (!ctx->eof[1] && ff_outlink_get_status(avctx->inputs[1])) {
331             ctx->eof[1] = 1;
332         }
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);
341         return 0;
342     }
343
344     return FFERROR_NOT_READY;
345 }
346
347 static av_cold void xfade_opencl_uninit(AVFilterContext *avctx)
348 {
349     XFadeOpenCLContext *ctx = avctx->priv;
350     cl_int cle;
351
352     if (ctx->kernel) {
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);
357     }
358
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);
364     }
365
366     ff_opencl_filter_uninit(avctx);
367 }
368
369 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
370 {
371     XFadeOpenCLContext *s = inlink->dst->priv;
372
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);
376 }
377
378 #define OFFSET(x) offsetof(XFadeOpenCLContext, x)
379 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
380
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 },
397     { NULL }
398 };
399
400 AVFILTER_DEFINE_CLASS(xfade_opencl);
401
402 static const AVFilterPad xfade_opencl_inputs[] = {
403     {
404         .name             = "main",
405         .type             = AVMEDIA_TYPE_VIDEO,
406         .get_video_buffer = get_video_buffer,
407         .config_props     = &ff_opencl_filter_config_input,
408     },
409     {
410         .name             = "xfade",
411         .type             = AVMEDIA_TYPE_VIDEO,
412         .get_video_buffer = get_video_buffer,
413         .config_props     = &ff_opencl_filter_config_input,
414     },
415     { NULL }
416 };
417
418 static const AVFilterPad xfade_opencl_outputs[] = {
419     {
420         .name          = "default",
421         .type          = AVMEDIA_TYPE_VIDEO,
422         .config_props  = &xfade_opencl_config_output,
423     },
424     { NULL }
425 };
426
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,
439 };