[FFmpeg-devel] [PATCH V1] libavfilter: add transpose_vaapi filter
Zachary Zhou
zachary.zhou at hotmail.com
Tue Nov 13 09:31:55 EET 2018
From: Zachary Zhou <zachary.zhou at intel.com>
It supports clockwise rotation by 0/90/180/270 degrees and mirroring by
horizontal/vertical. video size is changed when rotation by 90/270.
ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128
-hwaccel_output_format vaapi -i input.264 -vf "transpose_vaapi=rotate=90:mirror=1"
-c:v h264_vaapi output.h264
Signed-off-by: Zachary Zhou <zachary.zhou at intel.com>
Signed-off-by: Zachary Zhou <zachary.zhou at hotmail.com>
---
configure | 2 +
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_transpose_vaapi.c | 337 +++++++++++++++++++++++++++++++
4 files changed, 341 insertions(+)
create mode 100644 libavfilter/vf_transpose_vaapi.c
diff --git a/configure b/configure
index 85d5dd5962..f62d2a27ca 100755
--- a/configure
+++ b/configure
@@ -3439,6 +3439,7 @@ scale_filter_deps="swscale"
scale_qsv_filter_deps="libmfx"
select_filter_select="pixelutils"
sharpness_vaapi_filter_deps="vaapi"
+transpose_vaapi_filter_deps="vaapi VAProcPipelineCaps_rotation_flags"
showcqt_filter_deps="avcodec avformat swscale"
showcqt_filter_suggest="libfontconfig libfreetype"
showcqt_filter_select="fft"
@@ -5961,6 +5962,7 @@ check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602
check_type "va/va.h va/va_dec_hevc.h" "VAPictureParameterBufferHEVC"
check_struct "va/va.h" "VADecPictureParameterBufferVP9" bit_depth
+check_struct "va/va.h" "VAProcPipelineCaps" rotation_flags
check_type "va/va.h va/va_enc_hevc.h" "VAEncPictureParameterBufferHEVC"
check_type "va/va.h va/va_enc_jpeg.h" "VAEncPictureParameterBufferJPEG"
check_type "va/va.h va/va_enc_vp8.h" "VAEncPictureParameterBufferVP8"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 62cc2f561f..813b174ddd 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -346,6 +346,7 @@ OBJS-$(CONFIG_SETRANGE_FILTER) += vf_setparams.o
OBJS-$(CONFIG_SETSAR_FILTER) += vf_aspect.o
OBJS-$(CONFIG_SETTB_FILTER) += settb.o
OBJS-$(CONFIG_SHARPNESS_VAAPI_FILTER) += vf_misc_vaapi.o vaapi_vpp.o
+OBJS-$(CONFIG_TRANSPOSE_VAAPI_FILTER) += vf_transpose_vaapi.o vaapi_vpp.o
OBJS-$(CONFIG_SHOWINFO_FILTER) += vf_showinfo.o
OBJS-$(CONFIG_SHOWPALETTE_FILTER) += vf_showpalette.o
OBJS-$(CONFIG_SHUFFLEFRAMES_FILTER) += vf_shuffleframes.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 5e72803b13..e5b0571aa8 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -330,6 +330,7 @@ extern AVFilter ff_vf_setrange;
extern AVFilter ff_vf_setsar;
extern AVFilter ff_vf_settb;
extern AVFilter ff_vf_sharpness_vaapi;
+extern AVFilter ff_vf_transpose_vaapi;
extern AVFilter ff_vf_showinfo;
extern AVFilter ff_vf_showpalette;
extern AVFilter ff_vf_shuffleframes;
diff --git a/libavfilter/vf_transpose_vaapi.c b/libavfilter/vf_transpose_vaapi.c
new file mode 100644
index 0000000000..dc58982688
--- /dev/null
+++ b/libavfilter/vf_transpose_vaapi.c
@@ -0,0 +1,337 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <string.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "vaapi_vpp.h"
+
+// Rotation angle values
+enum RotationAngle {
+ ROTATION_0 = 0,
+ ROTATION_90 = 90,
+ ROTATION_180 = 180,
+ ROTATION_270 = 270,
+
+ ROTATION_MIN = ROTATION_0,
+ ROTATION_MAX = ROTATION_270,
+ ROTATION_DEFAULT = ROTATION_0,
+};
+
+// Mirroring directions
+enum MirroringDirections {
+ MIRROR_NONE = 0,
+ MIRROR_HORIZONTAL = 1,
+ MIRROR_VERTICAL = 2,
+
+ MIRROR_MIN = MIRROR_NONE,
+ MIRROR_MAX = MIRROR_VERTICAL,
+ MIRROR_DEFAULT = MIRROR_NONE,
+};
+
+typedef struct TransposeVAAPIContext {
+ VAAPIVPPContext vpp_ctx; // must be the first field
+
+ int rotate;
+ int mirror;
+
+ int rotation_state;
+ int mirror_state;
+} TransposeVAAPIContext;
+
+static int transpose_vaapi_build_filter_params(AVFilterContext *avctx)
+{
+ VAAPIVPPContext *vpp_ctx = avctx->priv;
+ TransposeVAAPIContext *ctx = avctx->priv;
+
+ VAStatus vas;
+ int support_flag;
+
+ VAProcPipelineCaps pipeline_caps;
+
+ memset(&pipeline_caps, 0, sizeof(pipeline_caps));
+ vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
+ vpp_ctx->va_context,
+ NULL, 0,
+ &pipeline_caps);
+ if (vas != VA_STATUS_SUCCESS) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
+ "caps: %d (%s).\n", vas, vaErrorStr(vas));
+ return AVERROR(EIO);
+ }
+
+ if (!pipeline_caps.rotation_flags) {
+ av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support rotation\n");
+ return AVERROR(EINVAL);
+ }
+
+ switch (ctx->rotate) {
+ case ROTATION_0:
+ ctx->rotation_state = VA_ROTATION_NONE;
+ break;
+ case ROTATION_90:
+ ctx->rotation_state = VA_ROTATION_90;
+ break;
+ case ROTATION_180:
+ ctx->rotation_state = VA_ROTATION_180;
+ break;
+ case ROTATION_270:
+ ctx->rotation_state = VA_ROTATION_270;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Failed to set rotation_state to %d. "
+ "Clockwise %d, %d, %d and %d are only supported\n",
+ ctx->rotate,
+ ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270);
+ return AVERROR(EINVAL);
+ }
+
+ support_flag = pipeline_caps.rotation_flags & (1 << ctx->rotation_state);
+ if (!support_flag) {
+ av_log(avctx, AV_LOG_ERROR, "VAAPI driver doesn't support %d\n",
+ ctx->rotate);
+ return AVERROR(EINVAL);
+ }
+
+ switch (ctx->mirror) {
+ case MIRROR_NONE:
+ ctx->mirror_state = VA_MIRROR_NONE;
+ break;
+ case MIRROR_HORIZONTAL:
+ ctx->mirror_state = VA_MIRROR_HORIZONTAL;
+ break;
+ case MIRROR_VERTICAL:
+ ctx->mirror_state = VA_MIRROR_VERTICAL;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Failed to set mirroring_state to %d. "
+ "mirroring %d %d and %d are only supported\n",
+ ctx->mirror, MIRROR_NONE, MIRROR_HORIZONTAL, MIRROR_VERTICAL);
+ return AVERROR(EINVAL);
+ }
+
+ return 0;
+}
+
+static int transpose_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
+{
+ AVFilterContext *avctx = inlink->dst;
+ AVFilterLink *outlink = avctx->outputs[0];
+ VAAPIVPPContext *vpp_ctx = avctx->priv;
+ TransposeVAAPIContext *ctx = avctx->priv;
+ AVFrame *output_frame = NULL;
+ VASurfaceID input_surface, output_surface;
+ VARectangle input_region, output_region;
+
+ VAProcPipelineParameterBuffer params;
+ int err;
+
+ av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
+ av_get_pix_fmt_name(input_frame->format),
+ input_frame->width, input_frame->height, input_frame->pts);
+
+ if (vpp_ctx->va_context == VA_INVALID_ID)
+ return AVERROR(EINVAL);
+
+ input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
+ av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for transpose vpp input.\n",
+ input_surface);
+
+ output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
+ vpp_ctx->output_height);
+ if (!output_frame) {
+ err = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
+ av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for transpose vpp output.\n",
+ output_surface);
+ memset(¶ms, 0, sizeof(params));
+ input_region = (VARectangle) {
+ .x = 0,
+ .y = 0,
+ .width = input_frame->width,
+ .height = input_frame->height,
+ };
+
+ output_region = (VARectangle) {
+ .x = 0,
+ .y = 0,
+ .width = output_frame->width,
+ .height = output_frame->height,
+ };
+
+ switch (ctx->rotation_state) {
+ case VA_ROTATION_NONE:
+ case VA_ROTATION_90:
+ case VA_ROTATION_180:
+ case VA_ROTATION_270:
+ params.rotation_state = ctx->rotation_state;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "VAAPI doesn't support rotation %d\n",
+ ctx->rotation_state);
+ goto fail;
+ }
+
+ switch (ctx->mirror_state) {
+ case VA_MIRROR_HORIZONTAL:
+ case VA_MIRROR_VERTICAL:
+ case VA_MIRROR_NONE:
+ params.mirror_state = ctx->mirror_state;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "VAAPI doesn't support mirroring %d\n",
+ ctx->mirror_state);
+ goto fail;
+ }
+
+ if (vpp_ctx->nb_filter_buffers) {
+ params.filters = &vpp_ctx->filter_buffers[0];
+ params.num_filters = vpp_ctx->nb_filter_buffers;
+ }
+ params.surface = input_surface;
+ params.surface_region = &input_region;
+ params.surface_color_standard =
+ ff_vaapi_vpp_colour_standard(input_frame->colorspace);
+
+ params.output_region = &output_region;
+ params.output_background_color = 0xff000000;
+ params.output_color_standard = params.surface_color_standard;
+
+ params.pipeline_flags = 0;
+ params.filter_flags = VA_FRAME_PICTURE;
+
+ err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
+ if (err < 0)
+ goto fail;
+
+ err = av_frame_copy_props(output_frame, input_frame);
+ if (err < 0)
+ goto fail;
+ av_frame_free(&input_frame);
+
+ av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
+ av_get_pix_fmt_name(output_frame->format),
+ output_frame->width, output_frame->height, output_frame->pts);
+
+ return ff_filter_frame(outlink, output_frame);
+
+fail:
+ av_frame_free(&input_frame);
+ av_frame_free(&output_frame);
+ return err;
+}
+
+static av_cold int transpose_vaapi_init(AVFilterContext *avctx)
+{
+ VAAPIVPPContext *vpp_ctx = avctx->priv;
+
+ ff_vaapi_vpp_ctx_init(avctx);
+ vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
+ vpp_ctx->build_filter_params = transpose_vaapi_build_filter_params;
+ vpp_ctx->output_format = AV_PIX_FMT_NONE;
+
+ return 0;
+}
+
+static void transpose_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
+{
+ return ff_vaapi_vpp_ctx_uninit(avctx);
+}
+
+static int transpose_vaapi_vpp_query_formats(AVFilterContext *avctx)
+{
+ return ff_vaapi_vpp_query_formats(avctx);
+}
+
+static int transpose_vaapi_vpp_config_input(AVFilterLink *inlink)
+{
+ return ff_vaapi_vpp_config_input(inlink);
+}
+
+static int transpose_vaapi_vpp_config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *avctx = outlink->src;
+ VAAPIVPPContext *vpp_ctx = avctx->priv;
+ TransposeVAAPIContext *ctx = avctx->priv;
+
+ switch (ctx->rotate) {
+ case ROTATION_90:
+ case ROTATION_270:
+ vpp_ctx->output_width = avctx->inputs[0]->h;
+ vpp_ctx->output_height = avctx->inputs[0]->w;
+ av_log(avctx, AV_LOG_DEBUG, "swap width and height for 90/270 rotation\n");
+ break;
+ default:
+ break;
+ }
+
+ return ff_vaapi_vpp_config_output(outlink);
+}
+
+#define ROFFSET(x) offsetof(TransposeVAAPIContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
+static const AVOption transpose_vaapi_options[] = {
+ { "rotate", "set roation direction", ROFFSET(rotate), AV_OPT_TYPE_INT, { .i64 = ROTATION_DEFAULT }, ROTATION_MIN, ROTATION_MAX, FLAGS, "rotate" },
+ { "mirror", "set mirroring directions", ROFFSET(mirror), AV_OPT_TYPE_INT, { .i64 = MIRROR_DEFAULT }, MIRROR_MIN, MIRROR_MAX, FLAGS, "mirror" },
+ { "hflip", "horizontal mirroring", 0, AV_OPT_TYPE_CONST, { .i64 = MIRROR_HORIZONTAL }, .flags=FLAGS, .unit = "mirror" },
+ { "vflip", "vertical mirroring", 0, AV_OPT_TYPE_CONST, { .i64 = MIRROR_VERTICAL }, .flags=FLAGS, .unit = "mirror" },
+ { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(transpose_vaapi);
+
+static const AVFilterPad transpose_vaapi_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = &transpose_vaapi_filter_frame,
+ .config_props = &transpose_vaapi_vpp_config_input,
+ },
+ { NULL }
+};
+
+static const AVFilterPad transpose_vaapi_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = &transpose_vaapi_vpp_config_output,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_transpose_vaapi = {
+ .name = "transpose_vaapi",
+ .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for rotation"),
+ .priv_size = sizeof(TransposeVAAPIContext),
+ .init = &transpose_vaapi_init,
+ .uninit = &transpose_vaapi_vpp_ctx_uninit,
+ .query_formats = &transpose_vaapi_vpp_query_formats,
+ .inputs = transpose_vaapi_inputs,
+ .outputs = transpose_vaapi_outputs,
+ .priv_class = &transpose_vaapi_class,
+ .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
--
2.17.1
More information about the ffmpeg-devel
mailing list