FFmpeg
vf_transpose_opencl.c
Go to the documentation of this file.
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 "filters.h"
27 #include "opencl.h"
28 #include "opencl_source.h"
29 #include "video.h"
30 #include "transpose.h"
31 
32 typedef struct TransposeOpenCLContext {
35  int passthrough; ///< PassthroughType, landscape passthrough mode enabled
36  int dir; ///< TransposeDir
37  cl_kernel kernel;
38  cl_command_queue command_queue;
40 
42 {
44  cl_int cle;
45  int err;
46 
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 
73 {
74  FilterLink *outl = ff_filter_link(outlink);
75  AVFilterContext *avctx = outlink->src;
76  TransposeOpenCLContext *s = avctx->priv;
77  AVFilterLink *inlink = avctx->inputs[0];
79  const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
80  int ret;
81 
82  if ((inlink->w >= inlink->h &&
83  s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
84  (inlink->w <= inlink->h &&
85  s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
86  if (inl->hw_frames_ctx) {
88  if (!outl->hw_frames_ctx)
89  return AVERROR(ENOMEM);
90  }
91  av_log(avctx, AV_LOG_VERBOSE,
92  "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
93  inlink->w, inlink->h, inlink->w, inlink->h);
94 
95  return 0;
96  } else {
97  s->passthrough = TRANSPOSE_PT_TYPE_NONE;
98  }
99 
100  if (desc_in->log2_chroma_w != desc_in->log2_chroma_h) {
101  av_log(avctx, AV_LOG_ERROR, "Input format %s not supported.\n",
102  desc_in->name);
103  return AVERROR(EINVAL);
104  }
105 
106  s->ocf.output_width = inlink->h;
107  s->ocf.output_height = inlink->w;
109  if (ret < 0)
110  return ret;
111 
112  if (inlink->sample_aspect_ratio.num)
113  outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
114  inlink->sample_aspect_ratio);
115  else
116  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
117 
118  av_log(avctx, AV_LOG_VERBOSE,
119  "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
120  inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
121  s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
122  s->dir == 0 || s->dir == 3);
123  return 0;
124 }
125 
127 {
128  TransposeOpenCLContext *s = inlink->dst->priv;
129 
130  return s->passthrough ?
133 }
134 
136 {
137  AVFilterContext *avctx = inlink->dst;
138  AVFilterLink *outlink = avctx->outputs[0];
139  TransposeOpenCLContext *ctx = avctx->priv;
140  AVFrame *output = NULL;
141  size_t global_work[2];
142  cl_mem src, dst;
143  cl_int cle;
144  int err, p;
145 
146  av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
147  av_get_pix_fmt_name(input->format),
148  input->width, input->height, input->pts);
149 
150  if (!input->hw_frames_ctx)
151  return AVERROR(EINVAL);
152 
153  if (ctx->passthrough)
154  return ff_filter_frame(outlink, input);
155 
156  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
157  if (!output) {
158  err = AVERROR(ENOMEM);
159  goto fail;
160  }
161 
163  if (err < 0)
164  goto fail;
165 
166  if (input->sample_aspect_ratio.num == 0) {
167  output->sample_aspect_ratio = input->sample_aspect_ratio;
168  } else {
169  output->sample_aspect_ratio.num = input->sample_aspect_ratio.den;
170  output->sample_aspect_ratio.den = input->sample_aspect_ratio.num;
171  }
172 
173  if (!ctx->initialised) {
174  err = transpose_opencl_init(avctx);
175  if (err < 0)
176  goto fail;
177  }
178 
179  for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
180  src = (cl_mem) input->data[p];
181  dst = (cl_mem) output->data[p];
182 
183  if (!dst)
184  break;
185  CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
186  CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
187  CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->dir);
188 
189  err = ff_opencl_filter_work_size_from_image(avctx, global_work, output,
190  p, 16);
191 
192  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
193  global_work, NULL,
194  0, NULL, NULL);
195  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
196  }
197  cle = clFinish(ctx->command_queue);
198  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
199 
201 
202  av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
203  av_get_pix_fmt_name(output->format),
204  output->width, output->height, output->pts);
205 
206  return ff_filter_frame(outlink, output);
207 
208 fail:
209  clFinish(ctx->command_queue);
212  return err;
213 }
214 
216 {
217  TransposeOpenCLContext *ctx = avctx->priv;
218  cl_int cle;
219 
220  if (ctx->kernel) {
221  cle = clReleaseKernel(ctx->kernel);
222  if (cle != CL_SUCCESS)
223  av_log(avctx, AV_LOG_ERROR, "Failed to release "
224  "kernel: %d.\n", cle);
225  }
226 
227  if (ctx->command_queue) {
228  cle = clReleaseCommandQueue(ctx->command_queue);
229  if (cle != CL_SUCCESS)
230  av_log(avctx, AV_LOG_ERROR, "Failed to release "
231  "command queue: %d.\n", cle);
232  }
233 
235 }
236 
237 #define OFFSET(x) offsetof(TransposeOpenCLContext, x)
238 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
240  { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, .unit = "dir" },
241  { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
242  { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
243  { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
244  { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
245 
246  { "passthrough", "do not apply transposition if the input matches the specified geometry",
247  OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, .unit = "passthrough" },
248  { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
249  { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
250  { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
251 
252  { NULL }
253 };
254 
255 AVFILTER_DEFINE_CLASS(transpose_opencl);
256 
258  {
259  .name = "default",
260  .type = AVMEDIA_TYPE_VIDEO,
261  .get_buffer.video = get_video_buffer,
262  .filter_frame = &transpose_opencl_filter_frame,
263  .config_props = &ff_opencl_filter_config_input,
264  },
265 };
266 
268  {
269  .name = "default",
270  .type = AVMEDIA_TYPE_VIDEO,
271  .config_props = &transpose_opencl_config_output,
272  },
273 };
274 
276  .name = "transpose_opencl",
277  .description = NULL_IF_CONFIG_SMALL("Transpose input video"),
278  .priv_size = sizeof(TransposeOpenCLContext),
279  .priv_class = &transpose_opencl_class,
285  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
286  .flags = AVFILTER_FLAG_HWDEVICE,
287 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
TransposeOpenCLContext::ocf
OpenCLFilterContext ocf
Definition: vf_transpose_opencl.c:33
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
CL_SET_KERNEL_ARG
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition: opencl.h:61
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
transpose_opencl_outputs
static const AVFilterPad transpose_opencl_outputs[]
Definition: vf_transpose_opencl.c:267
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
transpose_opencl_init
static int transpose_opencl_init(AVFilterContext *avctx)
Definition: vf_transpose_opencl.c:41
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
test::height
int height
Definition: vc1dsp.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
FLAGS
#define FLAGS
Definition: vf_transpose_opencl.c:238
w
uint8_t w
Definition: llviddspenc.c:38
ff_source_transpose_cl
const char * ff_source_transpose_cl
AVPixFmtDescriptor::name
const char * name
Definition: pixdesc.h:70
opencl.h
AVOption
AVOption.
Definition: opt.h:429
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
float.h
TRANSPOSE_CLOCK_FLIP
@ TRANSPOSE_CLOCK_FLIP
Definition: transpose.h:34
ff_opencl_filter_load_program
int ff_opencl_filter_load_program(AVFilterContext *avctx, const char **program_source_array, int nb_strings)
Load a new OpenCL program from strings in memory.
Definition: opencl.c:159
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
transpose_opencl_config_output
static int transpose_opencl_config_output(AVFilterLink *outlink)
Definition: vf_transpose_opencl.c:72
TRANSPOSE_CCLOCK
@ TRANSPOSE_CCLOCK
Definition: transpose.h:33
ff_default_get_video_buffer
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:111
ff_opencl_filter_work_size_from_image
int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
Find the work size needed needed for a given plane of an image.
Definition: opencl.c:266
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
get_video_buffer
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
Definition: vf_transpose_opencl.c:126
fail
#define fail()
Definition: checkasm.h:189
ff_opencl_filter_config_output
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition: opencl.c:83
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
TransposeOpenCLContext
Definition: vf_transpose_opencl.c:32
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
TransposeOpenCLContext::initialised
int initialised
Definition: vf_transpose_opencl.c:34
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:725
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
ff_vf_transpose_opencl
const AVFilter ff_vf_transpose_opencl
Definition: vf_transpose_opencl.c:275
AV_PIX_FMT_OPENCL
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition: pixfmt.h:358
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
FF_FILTER_FLAG_HWFRAME_AWARE
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: filters.h:206
test::width
int width
Definition: vc1dsp.c:39
TRANSPOSE_PT_TYPE_PORTRAIT
@ TRANSPOSE_PT_TYPE_PORTRAIT
Definition: transpose.h:27
transpose_opencl_inputs
static const AVFilterPad transpose_opencl_inputs[]
Definition: vf_transpose_opencl.c:257
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:173
TRANSPOSE_PT_TYPE_NONE
@ TRANSPOSE_PT_TYPE_NONE
Definition: transpose.h:25
TransposeOpenCLContext::kernel
cl_kernel kernel
Definition: vf_transpose_opencl.c:37
opencl_source.h
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
ff_opencl_filter_config_input
int ff_opencl_filter_config_input(AVFilterLink *inlink)
Check that the input link contains a suitable hardware frames context and extract the device from it.
Definition: opencl.c:46
ff_null_get_video_buffer
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:44
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
TransposeOpenCLContext::command_queue
cl_command_queue command_queue
Definition: vf_transpose_opencl.c:38
common.h
transpose_opencl_filter_frame
static int transpose_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
Definition: vf_transpose_opencl.c:135
TRANSPOSE_CLOCK
@ TRANSPOSE_CLOCK
Definition: transpose.h:32
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
TransposeOpenCLContext::dir
int dir
TransposeDir.
Definition: vf_transpose_opencl.c:36
AVFilter
Filter definition.
Definition: avfilter.h:201
ff_opencl_filter_init
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:135
ret
ret
Definition: filter_design.txt:187
TransposeOpenCLContext::passthrough
int passthrough
PassthroughType, landscape passthrough mode enabled.
Definition: vf_transpose_opencl.c:35
TRANSPOSE_CCLOCK_FLIP
@ TRANSPOSE_CCLOCK_FLIP
Definition: transpose.h:31
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
transpose.h
OpenCLFilterContext
Definition: opencl.h:36
ff_opencl_filter_uninit
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition: opencl.c:144
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
TRANSPOSE_PT_TYPE_LANDSCAPE
@ TRANSPOSE_PT_TYPE_LANDSCAPE
Definition: transpose.h:26
OFFSET
#define OFFSET(x)
Definition: vf_transpose_opencl.c:237
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(transpose_opencl)
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
CL_FAIL_ON_ERROR
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition: opencl.h:74
h
h
Definition: vp9dsp_template.c:2070
transpose_opencl_uninit
static av_cold void transpose_opencl_uninit(AVFilterContext *avctx)
Definition: vf_transpose_opencl.c:215
transpose_opencl_options
static const AVOption transpose_opencl_options[]
Definition: vf_transpose_opencl.c:239
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:252
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
src
#define src
Definition: vp8dsp.c:248
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3090
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:469