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 E2AE745AF4 for ; Sun, 16 Jul 2023 09:14:15 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3A94A68C5DF; Sun, 16 Jul 2023 12:13:56 +0300 (EEST) Received: from out162-62-57-137.mail.qq.com (out162-62-57-137.mail.qq.com [162.62.57.137]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D51FD68C558 for ; Sun, 16 Jul 2023 12:13:47 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1689498818; bh=kN5c9TUdsquVs6BkfcgBruc2HY7xGpIF3wteXJXKQqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pHk+RL7ZK3G07uV/YJYlD0Y2XI4o6Sf3Ck3KKoND89VOxq9cPnMyYHV1hm5HduN5O 0MaozXUJ5qpkDjof6ICbto6mfjdSPw0PLT2UN7gYXbWNT2Jaem+dySoCiRFxZZJj2d PQNxVjss0jHFrATJfxhOhgsc2eeUTibweyIggT1k= 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: xmsmtpt1689498818tlva1cyiy Message-ID: X-QQ-XMAILINFO: Nt+cTZuZCMyihw59lb6nkgbFty+1RgY1xotTRcQcBhTbcx++21/o/AVkxEMZcI VJ5DBiUeZWJGfHSO3ditM7pZyFv4PPK+hEqL+u+Nubadzdo5r3UIZO+ihK5e+7YmHJnOEwbfXM7T fJKA/o+N3NBa0IKvXRq0AMKnimU0cy+rFkAZbSGm0bNDrTIhTAv7JUKCuFzqVfZQdj/6Uc76rcOH uM7qyFF0mMrfq6+cEgptKcJZReTWYw3CC8hysvwaNkkmGbzqas5NFU8f4wQxcp/NI9S+AtnMx80k L8jImf3gr12KRPDmBClDkOtjhmCsEIB5fg/xREX7f6zHsVEt8wLWCcupfOZaA2IoFtUM6l3DY1/d SSgqV+7Fy+xkLZE+Bkc/2xgAoQomAAnkO15hJYSMpH0+ROTaWR4f3BefwyuET/mqQ/jmTcQXdbha xTpLi0//5AqQcCJWpiwxZpnvbT2sw930BCSl7rjD2QCA5oGWKMb96yYW05SqqpNGu7ZMDfBGWewz L+wBnLy1aKb2SZdMP369GqXSwxxf92Eg46cMPkkBP+CiaNbY+JaYuWpdhpPuQGGbaxr98uyHnqu9 OO8T4EgAKZvufuPN/4HpjePfepdSSxATe1SQ5NrprbfG0qslKLuVPLkMg7IKzKjbNZKm7LYT1ugT 2exQfPUMkNrMk7IkO1ARdNfgwK1esAJmiXuZNcpZnNqcPezd4sedqU0qKVRCGwyNLN/ZK6ynCLa1 E+M3vqsRgQyo/euYwLcKVJ03XexzKG5aVquecSCrcLn7Uux+jLgn5aGeUB7Qr3u+e3fBvO4IbuLr 51PHFRCeTnDRrp1iATIbDg4pDFs5i6z+N2p17P5ryQBzAtlHL2tLiPSYQ3sTdTB7rpK9Oc26SekM RKWEAZTUOx/ge4RivSXKMwI+FBvXj8SuPpTysmOY0mCrrX+IFBIcgUN+HeEuybvtb0j71wLXp5Uk oQrrSpJUEEeBAL8kKHxjxV9kGVVHD1vniGBQ6gfFubp6Qrj6wneQ== X-QQ-XMRINFO: OD9hHCdaPRBwq3WW+NvGbIU= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sun, 16 Jul 2023 17:13:28 +0800 X-OQ-MSGID: <20230716091329.28797-2-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 2/3] avfilter: add scale_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 For example, ./ffmpeg -hwaccel videotoolbox \ -hwaccel_output_format videotoolbox_vld \ -i ios-265.mov \ -c:v hevc_videotoolbox \ -profile:v main \ -b:v 3M \ -vf scale_vt=w=iw/2:h=ih/2:color_matrix=bt709:color_primaries=bt709:color_transfer=bt709 \ -c:a copy \ -tag:v hvc1 \ /tmp/test.mp4 Input: hevc (Main 10) (hvc1 / 0x31637668), yuv420p10le(tv, bt2020nc/bt2020/arib-std-b67), 3840x2160 Output: hevc (Main) (hvc1 / 0x31637668), yuv420p(tv, bt709, progressive), 1920x1080 --- Changelog | 1 + configure | 1 + doc/filters.texi | 21 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 4 +- libavfilter/vf_scale_vt.c | 244 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 libavfilter/vf_scale_vt.c diff --git a/Changelog b/Changelog index 3876082844..dadaf15de6 100644 --- a/Changelog +++ b/Changelog @@ -25,6 +25,7 @@ version : - Raw VVC bitstream parser, muxer and demuxer - Bitstream filter for editing metadata in VVC streams - Bitstream filter for converting VVC from MP4 to Annex B +- scale_vt filter for videotoolbox version 6.0: - Radiance HDR image support diff --git a/configure b/configure index dab3fb9036..bef6a6cb21 100755 --- a/configure +++ b/configure @@ -3835,6 +3835,7 @@ zmq_filter_deps="libzmq" zoompan_filter_deps="swscale" zscale_filter_deps="libzimg const_nan" scale_vaapi_filter_deps="vaapi" +scale_vt_filter_deps="videotoolbox" scale_vulkan_filter_deps="vulkan spirv_compiler" vpp_qsv_filter_deps="libmfx" vpp_qsv_filter_select="qsvvpp" diff --git a/doc/filters.texi b/doc/filters.texi index e5325c7410..500f3ac3cf 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -21175,6 +21175,27 @@ Scale a logo to 1/10th the height of a video, while preserving its display aspec @end example @end itemize +@section scale_vt + +Scale and convert the color parameters using VTPixelTransferSession. + +The filter accepts the following options: +@table @option +@item w +@item h +Set the output video dimension expression. Default value is the input dimension. + +@item color_matrix +Set the output colorspace matrix. + +@item color_primaries +Set the output color primaries. + +@item color_transfer +Set the output transfer characteristics. + +@end table + @section scharr Apply scharr operator to input video stream. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 9b7813575a..ef2c87104f 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -457,6 +457,7 @@ OBJS-$(CONFIG_SCALE_CUDA_FILTER) += vf_scale_cuda.o scale_eval.o \ OBJS-$(CONFIG_SCALE_NPP_FILTER) += vf_scale_npp.o scale_eval.o OBJS-$(CONFIG_SCALE_QSV_FILTER) += vf_vpp_qsv.o OBJS-$(CONFIG_SCALE_VAAPI_FILTER) += vf_scale_vaapi.o scale_eval.o vaapi_vpp.o +OBJS-$(CONFIG_SCALE_VT_FILTER) += vf_scale_vt.o scale_eval.o OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vf_scale_vulkan.o vulkan.o vulkan_filter.o OBJS-$(CONFIG_SCALE2REF_FILTER) += vf_scale.o scale_eval.o OBJS-$(CONFIG_SCALE2REF_NPP_FILTER) += vf_scale_npp.o scale_eval.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 9a7fadc58d..fefaa94d2b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -427,6 +427,7 @@ extern const AVFilter ff_vf_scale_cuda; extern const AVFilter ff_vf_scale_npp; extern const AVFilter ff_vf_scale_qsv; extern const AVFilter ff_vf_scale_vaapi; +extern const AVFilter ff_vf_scale_vt; extern const AVFilter ff_vf_scale_vulkan; extern const AVFilter ff_vf_scale2ref; extern const AVFilter ff_vf_scale2ref_npp; diff --git a/libavfilter/version.h b/libavfilter/version.h index c001693e3c..77f38cb9b4 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -31,8 +31,8 @@ #include "version_major.h" -#define LIBAVFILTER_VERSION_MINOR 8 -#define LIBAVFILTER_VERSION_MICRO 102 +#define LIBAVFILTER_VERSION_MINOR 9 +#define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ diff --git a/libavfilter/vf_scale_vt.c b/libavfilter/vf_scale_vt.c new file mode 100644 index 0000000000..a7f72c8de9 --- /dev/null +++ b/libavfilter/vf_scale_vt.c @@ -0,0 +1,244 @@ +/* + * 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 "scale_eval.h" + +typedef struct ScaleVtContext { + AVClass *class; + + VTPixelTransferSessionRef transfer; + int output_width; + int output_height; + char *w_expr; + char *h_expr; + + enum AVColorPrimaries colour_primaries; + enum AVColorTransferCharacteristic colour_transfer; + enum AVColorSpace colour_matrix; + char *colour_primaries_string; + char *colour_transfer_string; + char *colour_matrix_string; +} ScaleVtContext; + +static av_cold int scale_vt_init(AVFilterContext *avctx) +{ + ScaleVtContext *s = avctx->priv; + int ret; + CFStringRef value; + + ret = VTPixelTransferSessionCreate(kCFAllocatorDefault, &s->transfer); + if (ret != noErr) { + av_log(avctx, AV_LOG_ERROR, "transfer session create failed, %d\n", ret); + return AVERROR_EXTERNAL; + } + +#define STRING_OPTION(var_name, func_name, default_value) \ + do { \ + if (s->var_name##_string) { \ + int var = av_##func_name##_from_name(s->var_name##_string); \ + if (var < 0) { \ + av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \ + return AVERROR(EINVAL); \ + } \ + s->var_name = var; \ + } else { \ + s->var_name = default_value; \ + } \ + } while (0) + + STRING_OPTION(colour_primaries, color_primaries, AVCOL_PRI_UNSPECIFIED); + STRING_OPTION(colour_transfer, color_transfer, AVCOL_TRC_UNSPECIFIED); + STRING_OPTION(colour_matrix, color_space, AVCOL_SPC_UNSPECIFIED); + + if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED) { + value = av_map_videotoolbox_color_primaries_from_av(s->colour_primaries); + if (!value) { + av_log(avctx, AV_LOG_ERROR, + "Doesn't support converting to colour primaries %s\n", + s->colour_primaries_string); + return AVERROR(ENOTSUP); + } + VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationColorPrimaries, value); + } + + if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED) { + value = av_map_videotoolbox_color_trc_from_av(s->colour_transfer); + if (!value) { + av_log(avctx, AV_LOG_ERROR, + "Doesn't support converting to trc %s\n", + s->colour_transfer_string); + return AVERROR(ENOTSUP); + } + VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationTransferFunction, value); + } + + if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED) { + value = av_map_videotoolbox_color_matrix_from_av(s->colour_matrix); + if (!value) { + av_log(avctx, AV_LOG_ERROR, + "Doesn't support converting to colorspace %s\n", + s->colour_matrix_string); + return AVERROR(ENOTSUP); + } + VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationYCbCrMatrix, value); + } + + return 0; +} + +static av_cold void scale_vt_uninit(AVFilterContext *avctx) +{ + ScaleVtContext *s = avctx->priv; + + if (s->transfer) { + VTPixelTransferSessionInvalidate(s->transfer); + CFRelease(s->transfer); + s->transfer = NULL; + } +} + +static int scale_vt_filter_frame(AVFilterLink *link, AVFrame *in) +{ + int ret; + AVFilterContext *ctx = link->dst; + ScaleVtContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + CVPixelBufferRef src; + CVPixelBufferRef dst; + + 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; + + av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den, + (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w, + (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h, + INT_MAX); + if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED) + out->color_primaries = s->colour_primaries; + if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED) + out->color_trc = s->colour_transfer; + if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED) + out->colorspace = s->colour_matrix; + + src = (CVPixelBufferRef)in->data[3]; + dst = (CVPixelBufferRef)out->data[3]; + ret = VTPixelTransferSessionTransferImage(s->transfer, 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 scale_vt_config_output(AVFilterLink *outlink) +{ + int err; + AVFilterContext *avctx = outlink->src; + ScaleVtContext *s = avctx->priv; + AVFilterLink *inlink = outlink->src->inputs[0]; + + err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink, + &s->output_width, + &s->output_height); + if (err < 0) + return err; + + outlink->w = s->output_width; + outlink->h = s->output_height; + + if (inlink->sample_aspect_ratio.num) { + AVRational r = {outlink->h * inlink->w, outlink->w * inlink->h}; + outlink->sample_aspect_ratio = av_mul_q(r, inlink->sample_aspect_ratio); + } else { + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + } + + return 0; +} + +#define OFFSET(x) offsetof(ScaleVtContext, x) +#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM) +static const AVOption scale_vt_options[] = { + { "w", "Output video width", + OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS }, + { "h", "Output video height", + OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS }, + { "color_matrix", "Output colour matrix coefficient set", + OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, + { "color_primaries", "Output colour primaries", + OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, + { "color_transfer", "Output colour transfer characteristics", + OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, + { NULL }, +}; + +AVFILTER_DEFINE_CLASS(scale_vt); + +static const AVFilterPad scale_vt_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &scale_vt_filter_frame, + }, +}; + +static const AVFilterPad scale_vt_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = &scale_vt_config_output, + }, +}; + +const AVFilter ff_vf_scale_vt = { + .name = "scale_vt", + .description = NULL_IF_CONFIG_SMALL("Scale Videotoolbox frames"), + .priv_size = sizeof(ScaleVtContext), + .init = scale_vt_init, + .uninit = scale_vt_uninit, + FILTER_INPUTS(scale_vt_inputs), + FILTER_OUTPUTS(scale_vt_outputs), + FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VIDEOTOOLBOX), + .priv_class = &scale_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".