FFmpeg
Loading...
Searching...
No Matches
vf_transpose_vaapi.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 <string.h>
19
20#include "libavutil/opt.h"
21#include "libavutil/pixdesc.h"
22
23#include "avfilter.h"
24#include "filters.h"
25#include "transpose.h"
26#include "vaapi_vpp.h"
27#include "video.h"
28
29typedef struct TransposeVAAPIContext {
30 VAAPIVPPContext vpp_ctx; // must be the first field
31 int passthrough; // PassthroughType, landscape passthrough mode enabled
32 int dir; // TransposeDir
33
37
39{
40 VAAPIVPPContext *vpp_ctx = avctx->priv;
42 VAStatus vas;
43 int support_flag;
44 VAProcPipelineCaps pipeline_caps;
45
46 memset(&pipeline_caps, 0, sizeof(pipeline_caps));
47 vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
48 vpp_ctx->va_context,
49 NULL, 0,
50 &pipeline_caps);
51 if (vas != VA_STATUS_SUCCESS) {
52 av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
53 "caps: %d (%s).\n", vas, vaErrorStr(vas));
54 return AVERROR(EIO);
55 }
56
57 if (!pipeline_caps.rotation_flags) {
58 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support transpose\n");
59 return AVERROR(EINVAL);
60 }
61
62 switch (ctx->dir) {
64 ctx->rotation_state = VA_ROTATION_270;
65 ctx->mirror_state = VA_MIRROR_VERTICAL;
66 break;
67 case TRANSPOSE_CLOCK:
68 ctx->rotation_state = VA_ROTATION_90;
69 ctx->mirror_state = VA_MIRROR_NONE;
70 break;
72 ctx->rotation_state = VA_ROTATION_270;
73 ctx->mirror_state = VA_MIRROR_NONE;
74 break;
76 ctx->rotation_state = VA_ROTATION_90;
77 ctx->mirror_state = VA_MIRROR_VERTICAL;
78 break;
80 ctx->rotation_state = VA_ROTATION_180;
81 ctx->mirror_state = VA_MIRROR_NONE;
82 break;
83 case TRANSPOSE_HFLIP:
84 ctx->rotation_state = VA_ROTATION_NONE;
85 ctx->mirror_state = VA_MIRROR_HORIZONTAL;
86 break;
87 case TRANSPOSE_VFLIP:
88 ctx->rotation_state = VA_ROTATION_NONE;
89 ctx->mirror_state = VA_MIRROR_VERTICAL;
90 break;
91 default:
92 av_log(avctx, AV_LOG_ERROR, "Failed to set direction to %d\n", ctx->dir);
93 return AVERROR(EINVAL);
94 }
95
96 if (VA_ROTATION_NONE != ctx->rotation_state) {
97 support_flag = pipeline_caps.rotation_flags & (1 << ctx->rotation_state);
98 if (!support_flag) {
99 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support rotation %d\n",
100 ctx->rotation_state);
101 return AVERROR(EINVAL);
102 }
103 }
104
105 if (VA_MIRROR_NONE != ctx->mirror_state) {
106 support_flag = pipeline_caps.mirror_flags & ctx->mirror_state;
107 if (!support_flag) {
108 av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support mirror %d\n",
109 ctx->mirror_state);
110 return AVERROR(EINVAL);
111 }
112 }
113
114 return 0;
115}
116
117static int transpose_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
118{
119 AVFilterContext *avctx = inlink->dst;
120 AVFilterLink *outlink = avctx->outputs[0];
121 VAAPIVPPContext *vpp_ctx = avctx->priv;
124 VAProcPipelineParameterBuffer params;
125 int err;
126
127 if (ctx->passthrough)
128 return ff_filter_frame(outlink, input_frame);
129
130 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
131 av_get_pix_fmt_name(input_frame->format),
132 input_frame->width, input_frame->height, input_frame->pts);
133
134 if (vpp_ctx->va_context == VA_INVALID_ID)
135 return AVERROR(EINVAL);
136
138 vpp_ctx->output_height);
139 if (!output_frame) {
140 err = AVERROR(ENOMEM);
141 goto fail;
142 }
143
144 err = av_frame_copy_props(output_frame, input_frame);
145 if (err < 0)
146 goto fail;
147
148 err = ff_vaapi_vpp_init_params(avctx, &params,
149 input_frame, output_frame);
150 if (err < 0)
151 goto fail;
152
153 params.rotation_state = ctx->rotation_state;
154 params.mirror_state = ctx->mirror_state;
155
157 if (err < 0)
158 goto fail;
159
160 av_frame_free(&input_frame);
161
162 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
164 output_frame->width, output_frame->height, output_frame->pts);
165
166 return ff_filter_frame(outlink, output_frame);
167
168fail:
169 av_frame_free(&input_frame);
171 return err;
172}
173
175{
176 VAAPIVPPContext *vpp_ctx = avctx->priv;
177
182
183 return 0;
184}
185
187{
188 FilterLink *outl = ff_filter_link(outlink);
189 AVFilterContext *avctx = outlink->src;
190 VAAPIVPPContext *vpp_ctx = avctx->priv;
192 AVFilterLink *inlink = avctx->inputs[0];
193 FilterLink *inl = ff_filter_link(inlink);
194
195 if ((inlink->w >= inlink->h && ctx->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
196 (inlink->w <= inlink->h && ctx->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
198 if (!outl->hw_frames_ctx)
199 return AVERROR(ENOMEM);
200 av_log(avctx, AV_LOG_VERBOSE,
201 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
202 inlink->w, inlink->h, inlink->w, inlink->h);
203 return 0;
204 }
205
206 ctx->passthrough = TRANSPOSE_PT_TYPE_NONE;
207
208 switch (ctx->dir) {
210 case TRANSPOSE_CCLOCK:
211 case TRANSPOSE_CLOCK:
213 vpp_ctx->output_width = avctx->inputs[0]->h;
214 vpp_ctx->output_height = avctx->inputs[0]->w;
215 av_log(avctx, AV_LOG_DEBUG, "swap width and height for clock/cclock rotation\n");
216 break;
217 default:
218 break;
219 }
220
221 return ff_vaapi_vpp_config_output(outlink);
222}
223
224static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
225{
226 TransposeVAAPIContext *ctx = inlink->dst->priv;
227
228 return ctx->passthrough ?
229 ff_null_get_video_buffer(inlink, w, h) :
231}
232
233#define OFFSET(x) offsetof(TransposeVAAPIContext, x)
234#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
236 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 6, FLAGS, .unit = "dir" },
237 { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
238 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" },
239 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" },
240 { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" },
241 { "reversal", "rotate by half-turn", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_REVERSAL }, .flags=FLAGS, .unit = "dir" },
242 { "hflip", "flip horizontally", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_HFLIP }, .flags=FLAGS, .unit = "dir" },
243 { "vflip", "flip vertically", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_VFLIP }, .flags=FLAGS, .unit = "dir" },
244
245 { "passthrough", "do not apply transposition if the input matches the specified geometry",
246 OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, .unit = "passthrough" },
247 { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
248 { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
249 { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, .unit = "passthrough" },
250
251 { NULL }
252};
253
254
255AVFILTER_DEFINE_CLASS(transpose_vaapi);
256
258 {
259 .name = "default",
260 .type = AVMEDIA_TYPE_VIDEO,
261 .filter_frame = &transpose_vaapi_filter_frame,
262 .get_buffer.video = get_video_buffer,
263 .config_props = &ff_vaapi_vpp_config_input,
264 },
265};
266
268 {
269 .name = "default",
270 .type = AVMEDIA_TYPE_VIDEO,
271 .config_props = &transpose_vaapi_vpp_config_output,
272 },
273};
274
276 .p.name = "transpose_vaapi",
277 .p.description = NULL_IF_CONFIG_SMALL("VAAPI VPP for transpose"),
278 .p.priv_class = &transpose_vaapi_class,
279 .priv_size = sizeof(TransposeVAAPIContext),
285 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
286};
SwsAArch64OpImplParams params
Definition ops.c:51
const FFFilter ff_vf_transpose_vaapi
static AVFormatContext * ctx
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
Definition avf_concat.c:205
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:860
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
AVOptions.
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:3380
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int width
Definition frame.h:544
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
VADisplay display
The VADisplay handle, to be filled by the user.
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition vaapi_vpp.h:63
enum AVPixelFormat output_format
Definition vaapi_vpp.h:52
int(* build_filter_params)(AVFilterContext *avctx)
Definition vaapi_vpp.h:61
VAContextID va_context
Definition vaapi_vpp.h:46
AVVAAPIDeviceContext * hwctx
Definition vaapi_vpp.h:41
#define av_log(a,...)
@ TRANSPOSE_CLOCK_FLIP
Definition transpose.h:34
@ TRANSPOSE_HFLIP
Definition transpose.h:36
@ TRANSPOSE_CCLOCK
Definition transpose.h:33
@ TRANSPOSE_CCLOCK_FLIP
Definition transpose.h:31
@ TRANSPOSE_VFLIP
Definition transpose.h:37
@ TRANSPOSE_REVERSAL
Definition transpose.h:35
@ TRANSPOSE_CLOCK
Definition transpose.h:32
@ TRANSPOSE_PT_TYPE_NONE
Definition transpose.h:25
@ TRANSPOSE_PT_TYPE_PORTRAIT
Definition transpose.h:27
@ TRANSPOSE_PT_TYPE_LANDSCAPE
Definition transpose.h:26
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition vaapi_vpp.c:97
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition vaapi_vpp.c:71
int ff_vaapi_vpp_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vaapi_vpp.c:29
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:45
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition vaapi_vpp.c:714
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition vaapi_vpp.c:533
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition vaapi_vpp.c:707
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:728
static const AVFilterPad transpose_vaapi_outputs[]
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
static int transpose_vaapi_vpp_config_output(AVFilterLink *outlink)
static int transpose_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
static const AVFilterPad transpose_vaapi_inputs[]
static int transpose_vaapi_build_filter_params(AVFilterContext *avctx)
#define OFFSET(x)
static const AVOption transpose_vaapi_options[]
static av_cold int transpose_vaapi_init(AVFilterContext *avctx)
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:44
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:84