1 /*
2 * Copyright (c) 2018 Danil Iashchenko
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
21 #include "config_components.h"
22
30
31
37
40
44
46
52
56
58
60 {
62 const char *kernel_name;
63 cl_int cle;
64 int err;
65
67 if (err < 0)
69
70 ctx->command_queue = clCreateCommandQueue(
ctx->ocf.hwctx->context,
71 ctx->ocf.hwctx->device_id,
72 0, &cle);
74 "command queue %d.\n", cle);
75
76 if (!strcmp(avctx->
filter->
name,
"convolution_opencl")) {
77 kernel_name = "convolution_global";
78 }
else if (!strcmp(avctx->
filter->
name,
"sobel_opencl")) {
79 kernel_name = "sobel_global";
80 }
else if (!strcmp(avctx->
filter->
name,
"prewitt_opencl")){
81 kernel_name = "prewitt_global";
82 }
else if (!strcmp(avctx->
filter->
name,
"roberts_opencl")){
83 kernel_name = "roberts_global";
84 } else {
86 }
87 ctx->kernel = clCreateKernel(
ctx->ocf.program, kernel_name, &cle);
89 "kernel %d.\n", cle);
90
92 return 0;
93
95 if (
ctx->command_queue)
96 clReleaseCommandQueue(
ctx->command_queue);
98 clReleaseKernel(
ctx->kernel);
99 return err;
100 }
101
102
103
105 {
108 size_t matrix_bytes;
110 cl_int cle;
112 int sscanf_err;
113 char *p, *
arg, *saveptr =
NULL;
114 float input_matrix[4][49];
115
116 for (
i = 0;
i < 4;
i++) {
117 ctx->biases[
i] =
ctx->biases[
i] / 255.0;
118 }
119
120 for (
i = 0;
i < 4;
i++) {
121 p =
ctx->matrix_str[
i];
122 while (
ctx->matrix_sizes[
i] < 49) {
125 break;
126 }
128 sscanf_err = sscanf(
arg,
"%f", &input_matrix[
i][
ctx->matrix_sizes[
i]]);
129 if (sscanf_err != 1) {
132 }
133 ctx->matrix_sizes[
i]++;
134 }
135 if (
ctx->matrix_sizes[
i] == 9) {
137 }
else if (
ctx->matrix_sizes[
i] == 25) {
139 }
else if (
ctx->matrix_sizes[
i] == 49) {
141 } else {
144 }
145
146 }
147
148 for (j = 0; j < 4; j++) {
149 matrix_bytes =
sizeof(
float)*
ctx->matrix_sizes[j];
154 }
155
156 for (
i = 0;
i <
ctx->matrix_sizes[j];
i++)
158
159 buffer = clCreateBuffer(
ctx->ocf.hwctx->context,
160 CL_MEM_READ_ONLY |
161 CL_MEM_COPY_HOST_PTR |
162 CL_MEM_HOST_NO_ACCESS,
163 matrix_bytes,
matrix, &cle);
166 "%d.\n", cle);
169 }
172 }
173
174 return 0;
175 }
176
178 {
183 cl_int cle;
184 size_t global_work[2];
186 int err, p;
187 size_t origin[3] = {0, 0, 0};
188 size_t region[3] = {0, 0, 1};
189
193
194 if (!
input->hw_frames_ctx)
196
197 if (!
ctx->initialised) {
199 if (err < 0)
201
202 if (!strcmp(avctx->
filter->
name,
"convolution_opencl")) {
204 if (err < 0)
206 } else {
208 }
209
210 }
211
216 }
217
221
223 break;
224
225 if (!strcmp(avctx->
filter->
name,
"convolution_opencl")) {
232
234 if (err < 0)
236
239 p, global_work[0], global_work[1]);
240
241 cle = clEnqueueNDRangeKernel(
ctx->command_queue,
ctx->kernel, 2,
NULL,
245 "kernel: %d.\n", cle);
246 } else {
247 if (!(
ctx->planes & (1 << p))) {
249 if (err < 0)
251
252 cle = clEnqueueCopyImage(
ctx->command_queue,
src,
dst,
253 origin, origin, region, 0,
NULL,
NULL);
255 p, cle);
256 } else {
261
263 if (err < 0)
265
268 p, global_work[0], global_work[1]);
269
270 cle = clEnqueueNDRangeKernel(
ctx->command_queue,
ctx->kernel, 2,
NULL,
274 "kernel: %d.\n", cle);
275 }
276 }
277 }
278
279 cle = clFinish(
ctx->command_queue);
281
283 if (err < 0)
285
287
291
293
295 clFinish(
ctx->command_queue);
298 return err;
299 }
300
302 {
304 cl_int cle;
306
307 for (
i = 0;
i < 4;
i++) {
308 clReleaseMemObject(
ctx->matrix[
i]);
309 }
310
312 cle = clReleaseKernel(
ctx->kernel);
313 if (cle != CL_SUCCESS)
315 "kernel: %d.\n", cle);
316 }
317
318 if (
ctx->command_queue) {
319 cle = clReleaseCommandQueue(
ctx->command_queue);
320 if (cle != CL_SUCCESS)
322 "command queue: %d.\n", cle);
323 }
324
326 }
327
329 {
334 },
335 };
336
338 {
342 },
343 };
344
345 #define OFFSET(x) offsetof(ConvolutionOpenCLContext, x)
346 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
347
348 #if CONFIG_CONVOLUTION_OPENCL_FILTER
349
350 static const AVOption convolution_opencl_options[] = {
364 };
365
367
369 .
name =
"convolution_opencl",
372 .priv_class = &convolution_opencl_class,
380 };
381
382 #endif /* CONFIG_CONVOLUTION_OPENCL_FILTER */
383
384 #if CONFIG_SOBEL_OPENCL_FILTER
385
386 static const AVOption sobel_opencl_options[] = {
391 };
392
394
396 .
name =
"sobel_opencl",
399 .priv_class = &sobel_opencl_class,
407 };
408
409 #endif /* CONFIG_SOBEL_OPENCL_FILTER */
410
411 #if CONFIG_PREWITT_OPENCL_FILTER
412
413 static const AVOption prewitt_opencl_options[] = {
418 };
419
421
423 .
name =
"prewitt_opencl",
426 .priv_class = &prewitt_opencl_class,
434 };
435
436 #endif /* CONFIG_PREWITT_OPENCL_FILTER */
437
438 #if CONFIG_ROBERTS_OPENCL_FILTER
439
440 static const AVOption roberts_opencl_options[] = {
445 };
446
448
450 .
name =
"roberts_opencl",
453 .priv_class = &roberts_opencl_class,
461 };
462
463 #endif /* CONFIG_ROBERTS_OPENCL_FILTER */