]> git.sesse.net Git - ffmpeg/blob - libavfilter/opencl.c
Merge commit 'b843b343d8a3210ae37a2342b1904a5bd1e5fc6e'
[ffmpeg] / libavfilter / 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 <stdio.h>
20 #include <string.h>
21
22 #include "libavutil/hwcontext.h"
23 #include "libavutil/hwcontext_opencl.h"
24 #include "libavutil/mem.h"
25
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "opencl.h"
29
30 int ff_opencl_filter_query_formats(AVFilterContext *avctx)
31 {
32     const static enum AVPixelFormat pix_fmts[] = {
33         AV_PIX_FMT_OPENCL,
34         AV_PIX_FMT_NONE,
35     };
36     AVFilterFormats *formats;
37
38     formats = ff_make_format_list(pix_fmts);
39     if (!formats)
40         return AVERROR(ENOMEM);
41
42     return ff_set_common_formats(avctx, formats);
43 }
44
45 int ff_opencl_filter_config_input(AVFilterLink *inlink)
46 {
47     AVFilterContext   *avctx = inlink->dst;
48     OpenCLFilterContext *ctx = avctx->priv;
49     AVHWFramesContext *input_frames;
50
51     if (!inlink->hw_frames_ctx) {
52         av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
53                "hardware frames context on the input.\n");
54         return AVERROR(EINVAL);
55     }
56
57     // Extract the device and default output format from the first input.
58     if (avctx->inputs[0] != inlink)
59         return 0;
60
61     input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
62
63     if (input_frames->format != AV_PIX_FMT_OPENCL)
64         return AVERROR(EINVAL);
65
66     ctx->device_ref = av_buffer_ref(input_frames->device_ref);
67     if (!ctx->device_ref)
68         return AVERROR(ENOMEM);
69     ctx->device = input_frames->device_ctx;
70     ctx->hwctx  = ctx->device->hwctx;
71
72     // Default output parameters match input parameters.
73     if (ctx->output_format == AV_PIX_FMT_NONE)
74         ctx->output_format = input_frames->sw_format;
75     if (!ctx->output_width)
76         ctx->output_width  = inlink->w;
77     if (!ctx->output_height)
78         ctx->output_height = inlink->h;
79
80     return 0;
81 }
82
83 int ff_opencl_filter_config_output(AVFilterLink *outlink)
84 {
85     AVFilterContext   *avctx = outlink->src;
86     OpenCLFilterContext *ctx = avctx->priv;
87     AVBufferRef       *output_frames_ref = NULL;
88     AVHWFramesContext *output_frames;
89     int err;
90
91     av_buffer_unref(&outlink->hw_frames_ctx);
92
93     output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
94     if (!output_frames_ref) {
95         err = AVERROR(ENOMEM);
96         goto fail;
97     }
98     output_frames = (AVHWFramesContext*)output_frames_ref->data;
99
100     output_frames->format    = AV_PIX_FMT_OPENCL;
101     output_frames->sw_format = ctx->output_format;
102     output_frames->width     = ctx->output_width;
103     output_frames->height    = ctx->output_height;
104
105     err = av_hwframe_ctx_init(output_frames_ref);
106     if (err < 0) {
107         av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
108                "frames: %d.\n", err);
109         goto fail;
110     }
111
112     outlink->hw_frames_ctx = output_frames_ref;
113     outlink->w = ctx->output_width;
114     outlink->h = ctx->output_height;
115
116     return 0;
117 fail:
118     av_buffer_unref(&output_frames_ref);
119     return err;
120 }
121
122 int ff_opencl_filter_init(AVFilterContext *avctx)
123 {
124     OpenCLFilterContext *ctx = avctx->priv;
125
126     ctx->output_format = AV_PIX_FMT_NONE;
127
128     return 0;
129 }
130
131 void ff_opencl_filter_uninit(AVFilterContext *avctx)
132 {
133     OpenCLFilterContext *ctx = avctx->priv;
134     cl_int cle;
135
136     if (ctx->program) {
137         cle = clReleaseProgram(ctx->program);
138         if (cle != CL_SUCCESS)
139             av_log(avctx, AV_LOG_ERROR, "Failed to release "
140                    "program: %d.\n", cle);
141     }
142
143     av_buffer_unref(&ctx->device_ref);
144 }
145
146 int ff_opencl_filter_load_program(AVFilterContext *avctx,
147                                   const char **program_source_array,
148                                   int nb_strings)
149 {
150     OpenCLFilterContext *ctx = avctx->priv;
151     cl_int cle;
152
153     ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
154                                              program_source_array,
155                                              NULL, &cle);
156     if (!ctx->program) {
157         av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
158         return AVERROR(EIO);
159     }
160
161     cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
162                          NULL, NULL, NULL);
163     if (cle != CL_SUCCESS) {
164         av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
165
166         if (cle == CL_BUILD_PROGRAM_FAILURE) {
167             char *log;
168             size_t log_length;
169
170             clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
171                                   CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
172
173             log = av_malloc(log_length);
174             if (log) {
175                 cle = clGetProgramBuildInfo(ctx->program,
176                                             ctx->hwctx->device_id,
177                                             CL_PROGRAM_BUILD_LOG,
178                                             log_length, log, NULL);
179                 if (cle == CL_SUCCESS)
180                     av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
181             }
182
183             av_free(log);
184         }
185
186         clReleaseProgram(ctx->program);
187         ctx->program = NULL;
188         return AVERROR(EIO);
189     }
190
191     return 0;
192 }
193
194 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
195                                             const char *filename)
196 {
197     FILE *file;
198     char *src = NULL;
199     size_t pos, len, rb;
200     const char *src_const;
201     int err;
202
203     file = fopen(filename, "r");
204     if (!file) {
205         av_log(avctx, AV_LOG_ERROR, "Unable to open program "
206                "source file \"%s\".\n", filename);
207         return AVERROR(ENOENT);
208     }
209
210     len = 1 << 16;
211     pos = 0;
212
213     err = av_reallocp(&src, len);
214     if (err < 0)
215         goto fail;
216
217     err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
218     if (err < 0) {
219         err = AVERROR(errno);
220         goto fail;
221     }
222     if (err > len / 2) {
223         err = AVERROR(EINVAL);
224         goto fail;
225     }
226     pos = err;
227
228     while (1) {
229         rb = fread(src + pos, 1, len - pos - 1, file);
230         if (rb == 0 && ferror(file)) {
231             err = AVERROR(EIO);
232             goto fail;
233         }
234         pos += rb;
235         if (pos < len)
236             break;
237         len <<= 1;
238         err = av_reallocp(&src, len);
239         if (err < 0)
240             goto fail;
241     }
242     src[pos] = 0;
243
244     src_const = src;
245
246     err = ff_opencl_filter_load_program(avctx, &src_const, 1);
247 fail:
248     fclose(file);
249     av_freep(&src);
250     return err;
251 }