From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 8AF7246EAF for ; Sun, 16 Jul 2023 09:13:57 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C306068C5AE; Sun, 16 Jul 2023 12:13:53 +0300 (EEST) Received: from out162-62-57-64.mail.qq.com (out162-62-57-64.mail.qq.com [162.62.57.64]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4EFB168C418 for ; Sun, 16 Jul 2023 12:13:46 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1689498819; bh=5kg/eSmFkB6kDhEGgnq/IxShGLaHc2PjwkQeWcu+k6o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cm1zBBdMK5XwawUn5a5v5VbaCEyqVpNqbyb/09lihWXuAINQuDwUCNjOc2G50woqT +lmBXp6y3fdJp7yxST7GZAmFSiOc68BVrhJzD0BSxGOzCO3kzCfUyI2vETLhn2se8R gcTrL72in6JNLgCMn7KI02y6uz+53RjI+qG8uJ/4= Received: from ZHILIZHAO-MB2.tencent.com ([113.116.31.61]) by newxmesmtplogicsvrsza7-0.qq.com (NewEsmtp) with SMTP id 35F220E3; Sun, 16 Jul 2023 17:13:31 +0800 X-QQ-mid: xmsmtpt1689498818ti6jasu7b Message-ID: X-QQ-XMAILINFO: N7/wgsuYk0Vx80objxImoajtWwLseQbt+JhFBTmL1Y7XKjL/XjSlLFAcYhxEo7 C/Ubb8SKjZsgTbn7CmCn9gylPKPBQu7gTvLqIEXVpIWKJuJ1mqpGayMBqh10FQtf8KlxZ/Hi/Aol eOlexL+nFE0mng275pjzVyuRGKOQbGcZ6+oid6/emxxPBjdOFjYss0B/4X4N4qE+YgCUrd0ZSvdL 8fzWBfPrxkafFuylLx6Fm1NvF6kU+CgE69XE5jRDllErEGInrL2vo/MRwTRyoOKlj+JRkh62/0fC gMk9Q/zc2zQXB8Q9migbu0RCmzUOlTDFndMqjomvV1/EtajGyp8FpKHRuMesGrdMZbRHMg4QWTPO UJms23stYocZAFizOCgYb2VmhN+uHGAMxbFSrGxWd2i3G9qveX5MzFCTgDPiLJlWfpmYCu2XFQf/ x+tQZI4LZ1dl5dodwCmp3Z75HUOZAJy0VSEwwc4C4HlJQX3+YEsDy3RqoLZ/0PCoT9CZJJo+MKji hZsyCE+yCAt7y53/4V3GLqV11iyyF3JbLedLH0kIMhlUK9lYy5bkVXHduQvRqyJb+s9x1JFTCrcz bDmUTpeXdBNs+4EFdb7wwZxYRGo//SGCpfMJllZXSbku6637Au5FjO6phS8p4CjlCu+o4s4yaBaM kP8Tr6RvPzC58hM3cwR3U9p3vX6/qfvYerKikx5SLWxUhfNLsc1l9LTxS+8TTvZoe/ChHqPvSlvh iwD25PReC82EaiOOwKRg5T96J3WbTMdxaVzBmJSEwO4ifQ25oN51j5PDPhWeH6GMiXvl8EW+9Xil mAh7GgcYvgaEV6jc0rBshUP0kfdfNH708bDZsm7944kDc+RCk68RyOxRZ1t2Pl5ytBOb9qLbEVyz jD9YslSmPZ6j/23zbtlGBTPybmaoeBKA/uoJ7ZCKwy6dZTDtthuXDRcJnZvEieX3T/4TYZBTqjoR VHGH8TcuoFiCwHIGJ0pR2OFJZW7lPGlgQyjV4Ig5nuNbrpS3M6J1698Kway1KcbiZUak2//fc= X-QQ-XMRINFO: NyFYKkN4Ny6FSmKK/uo/jdU= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sun, 16 Jul 2023 17:13:29 +0800 X-OQ-MSGID: <20230716091329.28797-3-quinkblack@foxmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230716091329.28797-1-quinkblack@foxmail.com> References: <20230716091329.28797-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avfilter: add transpose_vt for videotoolbox pix_fmt X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zhao Zhili Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Zhao Zhili --- Changelog | 1 + configure | 2 + doc/filters.texi | 48 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 2 +- libavfilter/vf_transpose_vt.c | 246 ++++++++++++++++++++++++++++++++++ 7 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 libavfilter/vf_transpose_vt.c diff --git a/Changelog b/Changelog index dadaf15de6..bbda4f4fd4 100644 --- a/Changelog +++ b/Changelog @@ -26,6 +26,7 @@ version : - Bitstream filter for editing metadata in VVC streams - Bitstream filter for converting VVC from MP4 to Annex B - scale_vt filter for videotoolbox +- transpose_vt filter for videotoolbox version 6.0: - Radiance HDR image support diff --git a/configure b/configure index bef6a6cb21..e446a912c9 100755 --- a/configure +++ b/configure @@ -3823,6 +3823,7 @@ tonemap_vaapi_filter_deps="vaapi VAProcFilterParameterBufferHDRToneMapping" tonemap_opencl_filter_deps="opencl const_nan" transpose_opencl_filter_deps="opencl" transpose_vaapi_filter_deps="vaapi VAProcPipelineCaps_rotation_flags" +transpose_vt_filter_deps="videotoolbox VTPixelRotationSessionCreate" transpose_vulkan_filter_deps="vulkan spirv_compiler" unsharp_opencl_filter_deps="opencl" uspp_filter_deps="gpl avcodec" @@ -6467,6 +6468,7 @@ check_headers termios.h check_headers unistd.h check_headers valgrind/valgrind.h check_func_headers VideoToolbox/VTCompressionSession.h VTCompressionSessionPrepareToEncodeFrames -framework VideoToolbox +check_func_headers VideoToolbox/VideoToolbox.h VTPixelRotationSessionCreate -framework VideoToolbox check_headers windows.h check_headers asm/types.h diff --git a/doc/filters.texi b/doc/filters.texi index 500f3ac3cf..1c9bd38cb2 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -27661,6 +27661,54 @@ Default value is @code{0}. @end table +@section transpose_vt + +Transpose rows with columns in the input video and optionally flip it. +For more in depth examples see the @ref{transpose} video filter, which shares mostly the same options. + +It accepts the following parameters: + +@table @option + +@item dir +Specify the transposition direction. + +Can assume the following values: +@table @samp +@item cclock_flip +Rotate by 90 degrees counterclockwise and vertically flip. (default) + +@item clock +Rotate by 90 degrees clockwise. + +@item cclock +Rotate by 90 degrees counterclockwise. + +@item clock_flip +Rotate by 90 degrees clockwise and vertically flip. + +@item hflip +Flip the input video horizontally. + +@item vflip +Flip the input video vertically. + +@end table + +@item passthrough +Do not apply the transposition if the input geometry matches the one +specified by the specified value. It accepts the following values: +@table @samp +@item none +Always apply transposition. (default) +@item portrait +Preserve portrait geometry (when @var{height} >= @var{width}). +@item landscape +Preserve landscape geometry (when @var{width} >= @var{height}). +@end table + +@end table + @section transpose_vulkan Transpose rows with columns in the input video and optionally flip it. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index ef2c87104f..30a0e22ef8 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -525,6 +525,7 @@ OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o OBJS-$(CONFIG_TRANSPOSE_NPP_FILTER) += vf_transpose_npp.o OBJS-$(CONFIG_TRANSPOSE_OPENCL_FILTER) += vf_transpose_opencl.o opencl.o opencl/transpose.o OBJS-$(CONFIG_TRANSPOSE_VAAPI_FILTER) += vf_transpose_vaapi.o vaapi_vpp.o +OBJS-$(CONFIG_TRANSPOSE_VT_FILTER) += vf_transpose_vt.o OBJS-$(CONFIG_TRANSPOSE_VULKAN_FILTER) += vf_transpose_vulkan.o vulkan.o vulkan_filter.o OBJS-$(CONFIG_TRIM_FILTER) += trim.o OBJS-$(CONFIG_UNPREMULTIPLY_FILTER) += vf_premultiply.o framesync.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index fefaa94d2b..089ad3a0ed 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -492,6 +492,7 @@ extern const AVFilter ff_vf_transpose; extern const AVFilter ff_vf_transpose_npp; extern const AVFilter ff_vf_transpose_opencl; extern const AVFilter ff_vf_transpose_vaapi; +extern const AVFilter ff_vf_transpose_vt; extern const AVFilter ff_vf_transpose_vulkan; extern const AVFilter ff_vf_trim; extern const AVFilter ff_vf_unpremultiply; diff --git a/libavfilter/version.h b/libavfilter/version.h index 77f38cb9b4..4a69d6be98 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFILTER_VERSION_MINOR 9 +#define LIBAVFILTER_VERSION_MINOR 10 #define LIBAVFILTER_VERSION_MICRO 100 diff --git a/libavfilter/vf_transpose_vt.c b/libavfilter/vf_transpose_vt.c new file mode 100644 index 0000000000..197ea5707c --- /dev/null +++ b/libavfilter/vf_transpose_vt.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2023 Zhao Zhili + * + * 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 + +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_videotoolbox.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "internal.h" +#include "transpose.h" + +typedef struct TransposeVtContext { + AVClass *class; + + VTPixelRotationSessionRef session; + int dir; + int passthrough; +} TransposeVtContext; + +static av_cold int transpose_vt_init(AVFilterContext *avctx) +{ + TransposeVtContext *s = avctx->priv; + int ret; + + ret = VTPixelRotationSessionCreate(kCFAllocatorDefault, &s->session); + if (ret != noErr) { + av_log(avctx, AV_LOG_ERROR, "Rotation session create failed, %d\n", ret); + return AVERROR_EXTERNAL; + } + + return 0; +} + +static av_cold void transpose_vt_uninit(AVFilterContext *avctx) +{ + TransposeVtContext *s = avctx->priv; + + if (s->session) { + VTPixelRotationSessionInvalidate(s->session); + CFRelease(s->session); + s->session = NULL; + } +} + +static int transpose_vt_filter_frame(AVFilterLink *link, AVFrame *in) +{ + int ret; + AVFilterContext *ctx = link->dst; + TransposeVtContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + CVPixelBufferRef src; + CVPixelBufferRef dst; + + if (s->passthrough) + return ff_filter_frame(outlink, in); + + AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = av_frame_copy_props(out, in); + if (ret < 0) + goto fail; + + src = (CVPixelBufferRef)in->data[3]; + dst = (CVPixelBufferRef)out->data[3]; + ret = VTPixelRotationSessionRotateImage(s->session, src, dst); + if (ret != noErr) { + av_log(ctx, AV_LOG_ERROR, "transfer image failed, %d\n", ret); + ret = AVERROR_EXTERNAL; + goto fail; + } + + av_frame_free(&in); + + return ff_filter_frame(outlink, out); + +fail: + av_frame_free(&in); + av_frame_free(&out); + return ret; +} + +static int transpose_vt_config_output(AVFilterLink *outlink) +{ + int err; + AVFilterContext *avctx = outlink->src; + TransposeVtContext *s = avctx->priv; + AVFilterLink *inlink = outlink->src->inputs[0]; + CFStringRef rotation = kVTRotation_0; + CFBooleanRef vflip = kCFBooleanFalse; + CFBooleanRef hflip = kCFBooleanFalse; + int swap_w_h = 0; + + if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) || + (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) { + av_log(avctx, AV_LOG_VERBOSE, + "w:%d h:%d -> w:%d h:%d (passthrough mode)\n", + inlink->w, inlink->h, inlink->w, inlink->h); + return 0; + } + + s->passthrough = TRANSPOSE_PT_TYPE_NONE; + + switch (s->dir) { + case TRANSPOSE_CCLOCK_FLIP: + rotation = kVTRotation_CCW90; + vflip = kCFBooleanTrue; + swap_w_h = 1; + break; + case TRANSPOSE_CCLOCK: + rotation = kVTRotation_CCW90; + swap_w_h = 1; + break; + case TRANSPOSE_CLOCK: + rotation = kVTRotation_CW90; + swap_w_h = 1; + break; + case TRANSPOSE_CLOCK_FLIP: + rotation = kVTRotation_CW90; + vflip = kCFBooleanTrue; + swap_w_h = 1; + break; + case TRANSPOSE_REVERSAL: + rotation = kVTRotation_180; + break; + case TRANSPOSE_HFLIP: + hflip = kCFBooleanTrue; + break; + case TRANSPOSE_VFLIP: + vflip = kCFBooleanTrue; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Failed to set direction to %d\n", s->dir); + return AVERROR(EINVAL); + } + + err = VTSessionSetProperty(s->session, kVTPixelRotationPropertyKey_Rotation, + rotation); + if (err != noErr) { + av_log(avctx, AV_LOG_ERROR, "Set rotation property failed, %d\n", err); + return AVERROR_EXTERNAL; + } + err = VTSessionSetProperty(s->session, kVTPixelRotationPropertyKey_FlipVerticalOrientation, + vflip); + if (err != noErr) { + av_log(avctx, AV_LOG_ERROR, "Set vertical flip property failed, %d\n", err); + return AVERROR_EXTERNAL; + } + err = VTSessionSetProperty(s->session, kVTPixelRotationPropertyKey_FlipHorizontalOrientation, + hflip); + if (err != noErr) { + av_log(avctx, AV_LOG_ERROR, "Set horizontal flip property failed, %d\n", err); + return AVERROR_EXTERNAL; + } + + if (swap_w_h) { + outlink->w = inlink->h; + outlink->h = inlink->w; + } + + return 0; +} + +#define OFFSET(x) offsetof(TransposeVtContext, x) +#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM) +static const AVOption transpose_vt_options[] = { + { "dir", "set transpose direction", + OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 6, FLAGS, "dir" }, + { "cclock_flip", "rotate counter-clockwise with vertical flip", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "dir" }, + { "clock", "rotate clockwise", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" }, + { "cclock", "rotate counter-clockwise", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" }, + { "clock_flip", "rotate clockwise with vertical flip", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" }, + { "reversal", "rotate by half-turn", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_REVERSAL }, .flags=FLAGS, .unit = "dir" }, + { "hflip", "flip horizontally", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_HFLIP }, .flags=FLAGS, .unit = "dir" }, + { "vflip", "flip vertically", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_VFLIP }, .flags=FLAGS, .unit = "dir" }, + + { "passthrough", "do not apply transposition if the input matches the specified geometry", + OFFSET(passthrough), AV_OPT_TYPE_INT, { .i64=TRANSPOSE_PT_TYPE_NONE }, 0, INT_MAX, FLAGS, "passthrough" }, + { "none", "always apply transposition", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_PT_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + { "portrait", "preserve portrait geometry", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_PT_TYPE_PORTRAIT }, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + { "landscape", "preserve landscape geometry", + 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_PT_TYPE_LANDSCAPE }, INT_MIN, INT_MAX, FLAGS, "passthrough" }, + + { NULL } +}; + +AVFILTER_DEFINE_CLASS(transpose_vt); + +static const AVFilterPad transpose_vt_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &transpose_vt_filter_frame, + }, +}; + +static const AVFilterPad transpose_vt_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = &transpose_vt_config_output, + }, +}; + +const AVFilter ff_vf_transpose_vt = { + .name = "transpose_vt", + .description = NULL_IF_CONFIG_SMALL("Transpose Videotoolbox frames"), + .priv_size = sizeof(TransposeVtContext), + .init = transpose_vt_init, + .uninit = transpose_vt_uninit, + FILTER_INPUTS(transpose_vt_inputs), + FILTER_OUTPUTS(transpose_vt_outputs), + FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VIDEOTOOLBOX), + .priv_class = &transpose_vt_class, + .flags = AVFILTER_FLAG_HWDEVICE, +}; -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".