2 * This file is part of FFmpeg.
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.
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.
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
20 #include "libavutil/avassert.h"
21 #include "libavutil/mem.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
28 #include "transpose.h"
29 #include "vaapi_vpp.h"
31 typedef struct TransposeVAAPIContext {
32 VAAPIVPPContext vpp_ctx; // must be the first field
33 int passthrough; // PassthroughType, landscape passthrough mode enabled
34 int dir; // TransposeDir
38 } TransposeVAAPIContext;
40 static int transpose_vaapi_build_filter_params(AVFilterContext *avctx)
42 VAAPIVPPContext *vpp_ctx = avctx->priv;
43 TransposeVAAPIContext *ctx = avctx->priv;
46 VAProcPipelineCaps pipeline_caps;
48 memset(&pipeline_caps, 0, sizeof(pipeline_caps));
49 vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
53 if (vas != VA_STATUS_SUCCESS) {
54 av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
55 "caps: %d (%s).\n", vas, vaErrorStr(vas));
59 if (!pipeline_caps.rotation_flags) {
60 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support transpose\n");
61 return AVERROR(EINVAL);
65 case TRANSPOSE_CCLOCK_FLIP:
66 ctx->rotation_state = VA_ROTATION_270;
67 ctx->mirror_state = VA_MIRROR_VERTICAL;
70 ctx->rotation_state = VA_ROTATION_90;
71 ctx->mirror_state = VA_MIRROR_NONE;
73 case TRANSPOSE_CCLOCK:
74 ctx->rotation_state = VA_ROTATION_270;
75 ctx->mirror_state = VA_MIRROR_NONE;
77 case TRANSPOSE_CLOCK_FLIP:
78 ctx->rotation_state = VA_ROTATION_90;
79 ctx->mirror_state = VA_MIRROR_VERTICAL;
81 case TRANSPOSE_REVERSAL:
82 ctx->rotation_state = VA_ROTATION_180;
83 ctx->mirror_state = VA_MIRROR_NONE;
86 ctx->rotation_state = VA_ROTATION_NONE;
87 ctx->mirror_state = VA_MIRROR_HORIZONTAL;
90 ctx->rotation_state = VA_ROTATION_NONE;
91 ctx->mirror_state = VA_MIRROR_VERTICAL;
94 av_log(avctx, AV_LOG_ERROR, "Failed to set direction to %d\n", ctx->dir);
95 return AVERROR(EINVAL);
98 if (VA_ROTATION_NONE != ctx->rotation_state) {
99 support_flag = pipeline_caps.rotation_flags & (1 << ctx->rotation_state);
101 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support rotation %d\n",
102 ctx->rotation_state);
103 return AVERROR(EINVAL);
107 if (VA_MIRROR_NONE != ctx->mirror_state) {
108 support_flag = pipeline_caps.mirror_flags & ctx->mirror_state;
110 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support mirror %d\n",
112 return AVERROR(EINVAL);
119 static int transpose_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
121 AVFilterContext *avctx = inlink->dst;
122 AVFilterLink *outlink = avctx->outputs[0];
123 VAAPIVPPContext *vpp_ctx = avctx->priv;
124 TransposeVAAPIContext *ctx = avctx->priv;
125 AVFrame *output_frame = NULL;
126 VASurfaceID input_surface, output_surface;
127 VARectangle input_region, output_region;
129 VAProcPipelineParameterBuffer params;
132 if (ctx->passthrough)
133 return ff_filter_frame(outlink, input_frame);
135 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
136 av_get_pix_fmt_name(input_frame->format),
137 input_frame->width, input_frame->height, input_frame->pts);
139 if (vpp_ctx->va_context == VA_INVALID_ID)
140 return AVERROR(EINVAL);
142 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
143 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for transpose vpp input.\n",
146 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
147 vpp_ctx->output_height);
149 err = AVERROR(ENOMEM);
153 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
154 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for transpose vpp output.\n",
156 memset(¶ms, 0, sizeof(params));
157 input_region = (VARectangle) {
160 .width = input_frame->width,
161 .height = input_frame->height,
164 output_region = (VARectangle) {
167 .width = output_frame->width,
168 .height = output_frame->height,
171 params.rotation_state = ctx->rotation_state;
172 params.mirror_state = ctx->mirror_state;
174 params.filters = &vpp_ctx->filter_buffers[0];
175 params.num_filters = vpp_ctx->nb_filter_buffers;
177 params.surface = input_surface;
178 params.surface_region = &input_region;
179 params.surface_color_standard =
180 ff_vaapi_vpp_colour_standard(input_frame->colorspace);
182 params.output_region = &output_region;
183 params.output_background_color = VAAPI_VPP_BACKGROUND_BLACK;
184 params.output_color_standard = params.surface_color_standard;
186 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
190 err = av_frame_copy_props(output_frame, input_frame);
193 av_frame_free(&input_frame);
195 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
196 av_get_pix_fmt_name(output_frame->format),
197 output_frame->width, output_frame->height, output_frame->pts);
199 return ff_filter_frame(outlink, output_frame);
202 av_frame_free(&input_frame);
203 av_frame_free(&output_frame);
207 static av_cold int transpose_vaapi_init(AVFilterContext *avctx)
209 VAAPIVPPContext *vpp_ctx = avctx->priv;
211 ff_vaapi_vpp_ctx_init(avctx);
212 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
213 vpp_ctx->build_filter_params = transpose_vaapi_build_filter_params;
214 vpp_ctx->output_format = AV_PIX_FMT_NONE;
219 static int transpose_vaapi_vpp_config_output(AVFilterLink *outlink)
221 AVFilterContext *avctx = outlink->src;
222 VAAPIVPPContext *vpp_ctx = avctx->priv;
223 TransposeVAAPIContext *ctx = avctx->priv;
224 AVFilterLink *inlink = avctx->inputs[0];
226 if ((inlink->w >= inlink->h && ctx->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
227 (inlink->w <= inlink->h && ctx->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
228 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
229 if (!outlink->hw_frames_ctx)
230 return AVERROR(ENOMEM);
231 av_log(avctx, AV_LOG_VERBOSE,
232 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
233 inlink->w, inlink->h, inlink->w, inlink->h);
237 ctx->passthrough = TRANSPOSE_PT_TYPE_NONE;
240 case TRANSPOSE_CCLOCK_FLIP:
241 case TRANSPOSE_CCLOCK:
242 case TRANSPOSE_CLOCK:
243 case TRANSPOSE_CLOCK_FLIP:
244 vpp_ctx->output_width = avctx->inputs[0]->h;
245 vpp_ctx->output_height = avctx->inputs[0]->w;
246 av_log(avctx, AV_LOG_DEBUG, "swap width and height for clock/cclock rotation\n");
252 return ff_vaapi_vpp_config_output(outlink);
255 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
257 TransposeVAAPIContext *ctx = inlink->dst->priv;
259 return ctx->passthrough ?
260 ff_null_get_video_buffer(inlink, w, h) :
261 ff_default_get_video_buffer(inlink, w, h);
264 #define OFFSET(x) offsetof(TransposeVAAPIContext, x)
265 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
266 static const AVOption transpose_vaapi_options[] = {
267 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 6, FLAGS, "dir" },
268 { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
269 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
270 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
271 { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
272 { "reversal", "rotate by half-turn", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_REVERSAL }, .flags=FLAGS, .unit = "dir" },
273 { "hflip", "flip horizontally", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_HFLIP }, .flags=FLAGS, .unit = "dir" },
274 { "vflip", "flip vertically", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_VFLIP }, .flags=FLAGS, .unit = "dir" },
276 { "passthrough", "do not apply transposition if the input matches the specified geometry",
277 OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
278 { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
279 { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
280 { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
286 AVFILTER_DEFINE_CLASS(transpose_vaapi);
288 static const AVFilterPad transpose_vaapi_inputs[] = {
291 .type = AVMEDIA_TYPE_VIDEO,
292 .filter_frame = &transpose_vaapi_filter_frame,
293 .get_video_buffer = get_video_buffer,
294 .config_props = &ff_vaapi_vpp_config_input,
299 static const AVFilterPad transpose_vaapi_outputs[] = {
302 .type = AVMEDIA_TYPE_VIDEO,
303 .config_props = &transpose_vaapi_vpp_config_output,
308 AVFilter ff_vf_transpose_vaapi = {
309 .name = "transpose_vaapi",
310 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for transpose"),
311 .priv_size = sizeof(TransposeVAAPIContext),
312 .init = &transpose_vaapi_init,
313 .uninit = &ff_vaapi_vpp_ctx_uninit,
314 .query_formats = &ff_vaapi_vpp_query_formats,
315 .inputs = transpose_vaapi_inputs,
316 .outputs = transpose_vaapi_outputs,
317 .priv_class = &transpose_vaapi_class,
318 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,