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 #include <float.h>
19
20 #include "libavutil/common.h"
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "opencl.h"
28 #include "opencl_source.h"
29 #include "video.h"
30 #include "transpose.h"
31
32 typedef struct TransposeOpenCLContext {
33 OpenCLFilterContext ocf;
34 int initialised;
35 int passthrough; ///< PassthroughType, landscape passthrough mode enabled
36 int dir; ///< TransposeDir
37 cl_kernel kernel;
38 cl_command_queue command_queue;
39 } TransposeOpenCLContext;
40
transpose_opencl_init(AVFilterContext * avctx)41 static int transpose_opencl_init(AVFilterContext *avctx)
42 {
43 TransposeOpenCLContext *ctx = avctx->priv;
44 cl_int cle;
45 int err;
46
47 err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_transpose, 1);
48 if (err < 0)
49 goto fail;
50
51 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
52 ctx->ocf.hwctx->device_id,
53 0, &cle);
54 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
55 "command queue %d.\n", cle);
56
57 ctx->kernel = clCreateKernel(ctx->ocf.program, "transpose", &cle);
58 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
59
60
61 ctx->initialised = 1;
62 return 0;
63
64 fail:
65 if (ctx->command_queue)
66 clReleaseCommandQueue(ctx->command_queue);
67 if (ctx->kernel)
68 clReleaseKernel(ctx->kernel);
69 return err;
70 }
71
transpose_opencl_config_output(AVFilterLink * outlink)72 static int transpose_opencl_config_output(AVFilterLink *outlink)
73 {
74 AVFilterContext *avctx = outlink->src;
75 TransposeOpenCLContext *s = avctx->priv;
76 AVFilterLink *inlink = avctx->inputs[0];
77 const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
78 int ret;
79
80 if ((inlink->w >= inlink->h &&
81 s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
82 (inlink->w <= inlink->h &&
83 s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
84 if (inlink->hw_frames_ctx) {
85 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
86 if (!outlink->hw_frames_ctx)
87 return AVERROR(ENOMEM);
88 }
89 av_log(avctx, AV_LOG_VERBOSE,
90 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
91 inlink->w, inlink->h, inlink->w, inlink->h);
92
93 return 0;
94 } else {
95 s->passthrough = TRANSPOSE_PT_TYPE_NONE;
96 }
97
98 if (desc_in->log2_chroma_w != desc_in->log2_chroma_h) {
99 av_log(avctx, AV_LOG_ERROR, "Input format %s not supported.\n",
100 desc_in->name);
101 return AVERROR(EINVAL);
102 }
103
104 s->ocf.output_width = inlink->h;
105 s->ocf.output_height = inlink->w;
106 ret = ff_opencl_filter_config_output(outlink);
107 if (ret < 0)
108 return ret;
109
110 if (inlink->sample_aspect_ratio.num)
111 outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
112 inlink->sample_aspect_ratio);
113 else
114 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
115
116 av_log(avctx, AV_LOG_VERBOSE,
117 "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
118 inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
119 s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
120 s->dir == 0 || s->dir == 3);
121 return 0;
122 }
123
get_video_buffer(AVFilterLink * inlink,int w,int h)124 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
125 {
126 TransposeOpenCLContext *s = inlink->dst->priv;
127
128 return s->passthrough ?
129 ff_null_get_video_buffer (inlink, w, h) :
130 ff_default_get_video_buffer(inlink, w, h);
131 }
132
transpose_opencl_filter_frame(AVFilterLink * inlink,AVFrame * input)133 static int transpose_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
134 {
135 AVFilterContext *avctx = inlink->dst;
136 AVFilterLink *outlink = avctx->outputs[0];
137 TransposeOpenCLContext *ctx = avctx->priv;
138 AVFrame *output = NULL;
139 size_t global_work[2];
140 cl_mem src, dst;
141 cl_int cle;
142 int err, p;
143
144 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
145 av_get_pix_fmt_name(input->format),
146 input->width, input->height, input->pts);
147
148 if (!input->hw_frames_ctx)
149 return AVERROR(EINVAL);
150
151 if (ctx->passthrough)
152 return ff_filter_frame(outlink, input);
153
154 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
155 if (!output) {
156 err = AVERROR(ENOMEM);
157 goto fail;
158 }
159
160 err = av_frame_copy_props(output, input);
161 if (err < 0)
162 goto fail;
163
164 if (input->sample_aspect_ratio.num == 0) {
165 output->sample_aspect_ratio = input->sample_aspect_ratio;
166 } else {
167 output->sample_aspect_ratio.num = input->sample_aspect_ratio.den;
168 output->sample_aspect_ratio.den = input->sample_aspect_ratio.num;
169 }
170
171 if (!ctx->initialised) {
172 err = transpose_opencl_init(avctx);
173 if (err < 0)
174 goto fail;
175 }
176
177 for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
178 src = (cl_mem) input->data[p];
179 dst = (cl_mem) output->data[p];
180
181 if (!dst)
182 break;
183 CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
184 CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
185 CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->dir);
186
187 err = ff_opencl_filter_work_size_from_image(avctx, global_work, output,
188 p, 16);
189
190 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
191 global_work, NULL,
192 0, NULL, NULL);
193 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
194 }
195 cle = clFinish(ctx->command_queue);
196 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
197
198 av_frame_free(&input);
199
200 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
201 av_get_pix_fmt_name(output->format),
202 output->width, output->height, output->pts);
203
204 return ff_filter_frame(outlink, output);
205
206 fail:
207 clFinish(ctx->command_queue);
208 av_frame_free(&input);
209 av_frame_free(&output);
210 return err;
211 }
212
transpose_opencl_uninit(AVFilterContext * avctx)213 static av_cold void transpose_opencl_uninit(AVFilterContext *avctx)
214 {
215 TransposeOpenCLContext *ctx = avctx->priv;
216 cl_int cle;
217
218 if (ctx->kernel) {
219 cle = clReleaseKernel(ctx->kernel);
220 if (cle != CL_SUCCESS)
221 av_log(avctx, AV_LOG_ERROR, "Failed to release "
222 "kernel: %d.\n", cle);
223 }
224
225 if (ctx->command_queue) {
226 cle = clReleaseCommandQueue(ctx->command_queue);
227 if (cle != CL_SUCCESS)
228 av_log(avctx, AV_LOG_ERROR, "Failed to release "
229 "command queue: %d.\n", cle);
230 }
231
232 ff_opencl_filter_uninit(avctx);
233 }
234
235 #define OFFSET(x) offsetof(TransposeOpenCLContext, x)
236 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
237 static const AVOption transpose_opencl_options[] = {
238 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, "dir" },
239 { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
240 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
241 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
242 { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
243
244 { "passthrough", "do not apply transposition if the input matches the specified geometry",
245 OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
246 { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
247 { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
248 { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
249
250 { NULL }
251 };
252
253 AVFILTER_DEFINE_CLASS(transpose_opencl);
254
255 static const AVFilterPad transpose_opencl_inputs[] = {
256 {
257 .name = "default",
258 .type = AVMEDIA_TYPE_VIDEO,
259 .get_buffer.video = get_video_buffer,
260 .filter_frame = &transpose_opencl_filter_frame,
261 .config_props = &ff_opencl_filter_config_input,
262 },
263 };
264
265 static const AVFilterPad transpose_opencl_outputs[] = {
266 {
267 .name = "default",
268 .type = AVMEDIA_TYPE_VIDEO,
269 .config_props = &transpose_opencl_config_output,
270 },
271 };
272
273 const AVFilter ff_vf_transpose_opencl = {
274 .name = "transpose_opencl",
275 .description = NULL_IF_CONFIG_SMALL("Transpose input video"),
276 .priv_size = sizeof(TransposeOpenCLContext),
277 .priv_class = &transpose_opencl_class,
278 .init = &ff_opencl_filter_init,
279 .uninit = &transpose_opencl_uninit,
280 FILTER_INPUTS(transpose_opencl_inputs),
281 FILTER_OUTPUTS(transpose_opencl_outputs),
282 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_OPENCL),
283 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
284 };
285